xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 5899ee367ab3fec885aa04d9a2b573bf2e464e7f)
18aa9ebccSVladimir Oltean // SPDX-License-Identifier: GPL-2.0
28aa9ebccSVladimir Oltean /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
38aa9ebccSVladimir Oltean  * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
48aa9ebccSVladimir Oltean  */
58aa9ebccSVladimir Oltean 
68aa9ebccSVladimir Oltean #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
78aa9ebccSVladimir Oltean 
88aa9ebccSVladimir Oltean #include <linux/delay.h>
98aa9ebccSVladimir Oltean #include <linux/module.h>
108aa9ebccSVladimir Oltean #include <linux/printk.h>
118aa9ebccSVladimir Oltean #include <linux/spi/spi.h>
128aa9ebccSVladimir Oltean #include <linux/errno.h>
138aa9ebccSVladimir Oltean #include <linux/gpio/consumer.h>
14ad9f299aSVladimir Oltean #include <linux/phylink.h>
158aa9ebccSVladimir Oltean #include <linux/of.h>
168aa9ebccSVladimir Oltean #include <linux/of_net.h>
178aa9ebccSVladimir Oltean #include <linux/of_mdio.h>
188aa9ebccSVladimir Oltean #include <linux/of_device.h>
198aa9ebccSVladimir Oltean #include <linux/netdev_features.h>
208aa9ebccSVladimir Oltean #include <linux/netdevice.h>
218aa9ebccSVladimir Oltean #include <linux/if_bridge.h>
228aa9ebccSVladimir Oltean #include <linux/if_ether.h>
23227d07a0SVladimir Oltean #include <linux/dsa/8021q.h>
248aa9ebccSVladimir Oltean #include "sja1105.h"
25ffe10e67SVladimir Oltean #include "sja1105_sgmii.h"
26317ab5b8SVladimir Oltean #include "sja1105_tas.h"
278aa9ebccSVladimir Oltean 
28ac02a451SVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops;
29ac02a451SVladimir Oltean 
308aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
318aa9ebccSVladimir Oltean 			     unsigned int startup_delay)
328aa9ebccSVladimir Oltean {
338aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
348aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
358aa9ebccSVladimir Oltean 	msleep(pulse_len);
368aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
378aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
388aa9ebccSVladimir Oltean 	msleep(startup_delay);
398aa9ebccSVladimir Oltean }
408aa9ebccSVladimir Oltean 
418aa9ebccSVladimir Oltean static void
428aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
438aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
448aa9ebccSVladimir Oltean {
458aa9ebccSVladimir Oltean 	if (allow) {
468aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  |= BIT(to);
478aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
488aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  |= BIT(to);
498aa9ebccSVladimir Oltean 	} else {
508aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  &= ~BIT(to);
518aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
528aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  &= ~BIT(to);
538aa9ebccSVladimir Oltean 	}
548aa9ebccSVladimir Oltean }
558aa9ebccSVladimir Oltean 
568aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree
578aa9ebccSVladimir Oltean  * settings into sja1105_setup
588aa9ebccSVladimir Oltean  */
598aa9ebccSVladimir Oltean struct sja1105_dt_port {
608aa9ebccSVladimir Oltean 	phy_interface_t phy_mode;
618aa9ebccSVladimir Oltean 	sja1105_mii_role_t role;
628aa9ebccSVladimir Oltean };
638aa9ebccSVladimir Oltean 
648aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
658aa9ebccSVladimir Oltean {
668aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
678aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
688aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
698aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
708aa9ebccSVladimir Oltean 		 */
718aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
728aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
738aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
748aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
758aa9ebccSVladimir Oltean 		.ifg = 0,
768aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
771fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
788aa9ebccSVladimir Oltean 		 */
798aa9ebccSVladimir Oltean 		.speed = SJA1105_SPEED_AUTO,
808aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
818aa9ebccSVladimir Oltean 		.tp_delin = 0,
828aa9ebccSVladimir Oltean 		.tp_delout = 0,
838aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
848aa9ebccSVladimir Oltean 		.maxage = 0xFF,
858aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
868aa9ebccSVladimir Oltean 		.vlanprio = 0,
87e3502b82SVladimir Oltean 		.vlanid = 1,
888aa9ebccSVladimir Oltean 		.ing_mirr = false,
898aa9ebccSVladimir Oltean 		.egr_mirr = false,
908aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
918aa9ebccSVladimir Oltean 		.drpnona664 = false,
928aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
938aa9ebccSVladimir Oltean 		.drpdtag = false,
948aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
958aa9ebccSVladimir Oltean 		.drpuntag = false,
968aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
978aa9ebccSVladimir Oltean 		.retag = false,
98640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
99640f763fSVladimir Oltean 		 * STP will enable it.
100640f763fSVladimir Oltean 		 */
101640f763fSVladimir Oltean 		.dyn_learn = false,
1028aa9ebccSVladimir Oltean 		.egress = false,
1038aa9ebccSVladimir Oltean 		.ingress = false,
1048aa9ebccSVladimir Oltean 	};
1058aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1068aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1078aa9ebccSVladimir Oltean 	int i;
1088aa9ebccSVladimir Oltean 
1098aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1108aa9ebccSVladimir Oltean 
1118aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1128aa9ebccSVladimir Oltean 	if (table->entry_count) {
1138aa9ebccSVladimir Oltean 		kfree(table->entries);
1148aa9ebccSVladimir Oltean 		table->entry_count = 0;
1158aa9ebccSVladimir Oltean 	}
1168aa9ebccSVladimir Oltean 
1178aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_NUM_PORTS,
1188aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1198aa9ebccSVladimir Oltean 	if (!table->entries)
1208aa9ebccSVladimir Oltean 		return -ENOMEM;
1218aa9ebccSVladimir Oltean 
1228aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_NUM_PORTS;
1238aa9ebccSVladimir Oltean 
1248aa9ebccSVladimir Oltean 	mac = table->entries;
1258aa9ebccSVladimir Oltean 
126640f763fSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1278aa9ebccSVladimir Oltean 		mac[i] = default_mac;
128640f763fSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, i)) {
129640f763fSVladimir Oltean 			/* STP doesn't get called for CPU port, so we need to
130640f763fSVladimir Oltean 			 * set the I/O parameters statically.
131640f763fSVladimir Oltean 			 */
132640f763fSVladimir Oltean 			mac[i].dyn_learn = true;
133640f763fSVladimir Oltean 			mac[i].ingress = true;
134640f763fSVladimir Oltean 			mac[i].egress = true;
135640f763fSVladimir Oltean 		}
136640f763fSVladimir Oltean 	}
1378aa9ebccSVladimir Oltean 
1388aa9ebccSVladimir Oltean 	return 0;
1398aa9ebccSVladimir Oltean }
1408aa9ebccSVladimir Oltean 
141ffe10e67SVladimir Oltean static bool sja1105_supports_sgmii(struct sja1105_private *priv, int port)
142ffe10e67SVladimir Oltean {
143ffe10e67SVladimir Oltean 	if (priv->info->part_no != SJA1105R_PART_NO &&
144ffe10e67SVladimir Oltean 	    priv->info->part_no != SJA1105S_PART_NO)
145ffe10e67SVladimir Oltean 		return false;
146ffe10e67SVladimir Oltean 
147ffe10e67SVladimir Oltean 	if (port != SJA1105_SGMII_PORT)
148ffe10e67SVladimir Oltean 		return false;
149ffe10e67SVladimir Oltean 
150ffe10e67SVladimir Oltean 	if (dsa_is_unused_port(priv->ds, port))
151ffe10e67SVladimir Oltean 		return false;
152ffe10e67SVladimir Oltean 
153ffe10e67SVladimir Oltean 	return true;
154ffe10e67SVladimir Oltean }
155ffe10e67SVladimir Oltean 
1568aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv,
1578aa9ebccSVladimir Oltean 				     struct sja1105_dt_port *ports)
1588aa9ebccSVladimir Oltean {
1598aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1608aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1618aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1628aa9ebccSVladimir Oltean 	int i;
1638aa9ebccSVladimir Oltean 
1648aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1658aa9ebccSVladimir Oltean 
1668aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1678aa9ebccSVladimir Oltean 	if (table->entry_count) {
1688aa9ebccSVladimir Oltean 		kfree(table->entries);
1698aa9ebccSVladimir Oltean 		table->entry_count = 0;
1708aa9ebccSVladimir Oltean 	}
1718aa9ebccSVladimir Oltean 
1728aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
1738aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1748aa9ebccSVladimir Oltean 	if (!table->entries)
1758aa9ebccSVladimir Oltean 		return -ENOMEM;
1768aa9ebccSVladimir Oltean 
1771fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
1788aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
1798aa9ebccSVladimir Oltean 
1808aa9ebccSVladimir Oltean 	mii = table->entries;
1818aa9ebccSVladimir Oltean 
1828aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
183ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
184ee9d0cb6SVladimir Oltean 			continue;
185ee9d0cb6SVladimir Oltean 
1868aa9ebccSVladimir Oltean 		switch (ports[i].phy_mode) {
1878aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
1888aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1898aa9ebccSVladimir Oltean 			break;
1908aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
1918aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
1928aa9ebccSVladimir Oltean 			break;
1938aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
1948aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
1958aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
1968aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
1978aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
1988aa9ebccSVladimir Oltean 			break;
199ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
200ffe10e67SVladimir Oltean 			if (!sja1105_supports_sgmii(priv, i))
201ffe10e67SVladimir Oltean 				return -EINVAL;
202ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
203ffe10e67SVladimir Oltean 			break;
2048aa9ebccSVladimir Oltean 		default:
2058aa9ebccSVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s!\n",
2068aa9ebccSVladimir Oltean 				phy_modes(ports[i].phy_mode));
2078aa9ebccSVladimir Oltean 		}
2088aa9ebccSVladimir Oltean 
209ffe10e67SVladimir Oltean 		/* Even though the SerDes port is able to drive SGMII autoneg
210ffe10e67SVladimir Oltean 		 * like a PHY would, from the perspective of the XMII tables,
211ffe10e67SVladimir Oltean 		 * the SGMII port should always be put in MAC mode.
212ffe10e67SVladimir Oltean 		 */
213ffe10e67SVladimir Oltean 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII)
214ffe10e67SVladimir Oltean 			mii->phy_mac[i] = XMII_MAC;
215ffe10e67SVladimir Oltean 		else
2168aa9ebccSVladimir Oltean 			mii->phy_mac[i] = ports[i].role;
2178aa9ebccSVladimir Oltean 	}
2188aa9ebccSVladimir Oltean 	return 0;
2198aa9ebccSVladimir Oltean }
2208aa9ebccSVladimir Oltean 
2218aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
2228aa9ebccSVladimir Oltean {
2238aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2248aa9ebccSVladimir Oltean 
2258aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
2268aa9ebccSVladimir Oltean 
227291d1e72SVladimir Oltean 	/* We only populate the FDB table through dynamic
228291d1e72SVladimir Oltean 	 * L2 Address Lookup entries
229291d1e72SVladimir Oltean 	 */
2308aa9ebccSVladimir Oltean 	if (table->entry_count) {
2318aa9ebccSVladimir Oltean 		kfree(table->entries);
2328aa9ebccSVladimir Oltean 		table->entry_count = 0;
2338aa9ebccSVladimir Oltean 	}
2348aa9ebccSVladimir Oltean 	return 0;
2358aa9ebccSVladimir Oltean }
2368aa9ebccSVladimir Oltean 
2378aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
2388aa9ebccSVladimir Oltean {
2398aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2406c56e167SVladimir Oltean 	u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
2418aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
2428456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
2438456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
2448aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
2458aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
2461da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
2471da73821SVladimir Oltean 		.start_dynspc = 0,
2486c56e167SVladimir Oltean 		.maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
2496c56e167SVladimir Oltean 			     max_fdb_entries, max_fdb_entries, },
2508aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
2518aa9ebccSVladimir Oltean 		.poly = 0x97,
2528aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
2538aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
2548aa9ebccSVladimir Oltean 		 */
2556d7c7d94SVladimir Oltean 		.shared_learn = true,
2568aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
2578aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
2588aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
2598aa9ebccSVladimir Oltean 		 */
2608aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
2618aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
2628aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
2638aa9ebccSVladimir Oltean 		 */
2648aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
2651da73821SVladimir Oltean 		/* P/Q/R/S only */
2661da73821SVladimir Oltean 		.use_static = true,
2671da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
2681da73821SVladimir Oltean 		 * dynamic FDB entries
2691da73821SVladimir Oltean 		 */
2701da73821SVladimir Oltean 		.owr_dyn = true,
2711da73821SVladimir Oltean 		.drpnolearn = true,
2728aa9ebccSVladimir Oltean 	};
2738aa9ebccSVladimir Oltean 
2748aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2758aa9ebccSVladimir Oltean 
2768aa9ebccSVladimir Oltean 	if (table->entry_count) {
2778aa9ebccSVladimir Oltean 		kfree(table->entries);
2788aa9ebccSVladimir Oltean 		table->entry_count = 0;
2798aa9ebccSVladimir Oltean 	}
2808aa9ebccSVladimir Oltean 
2818aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
2828aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2838aa9ebccSVladimir Oltean 	if (!table->entries)
2848aa9ebccSVladimir Oltean 		return -ENOMEM;
2858aa9ebccSVladimir Oltean 
2868aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
2878aa9ebccSVladimir Oltean 
2888aa9ebccSVladimir Oltean 	/* This table only has a single entry */
2898aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
2908aa9ebccSVladimir Oltean 				default_l2_lookup_params;
2918aa9ebccSVladimir Oltean 
2928aa9ebccSVladimir Oltean 	return 0;
2938aa9ebccSVladimir Oltean }
2948aa9ebccSVladimir Oltean 
2958aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
2968aa9ebccSVladimir Oltean {
2978aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2988aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
2998aa9ebccSVladimir Oltean 		.ving_mirr = 0,
3008aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
3018aa9ebccSVladimir Oltean 		.vmemb_port = 0,
3028aa9ebccSVladimir Oltean 		.vlan_bc = 0,
3038aa9ebccSVladimir Oltean 		.tag_port = 0,
304e3502b82SVladimir Oltean 		.vlanid = 1,
3058aa9ebccSVladimir Oltean 	};
306ec5ae610SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
307ec5ae610SVladimir Oltean 	int port;
3088aa9ebccSVladimir Oltean 
3098aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
3108aa9ebccSVladimir Oltean 
311e3502b82SVladimir Oltean 	/* The static VLAN table will only contain the initial pvid of 1.
3126666cebcSVladimir Oltean 	 * All other VLANs are to be configured through dynamic entries,
3136666cebcSVladimir Oltean 	 * and kept in the static configuration table as backing memory.
3148aa9ebccSVladimir Oltean 	 */
3158aa9ebccSVladimir Oltean 	if (table->entry_count) {
3168aa9ebccSVladimir Oltean 		kfree(table->entries);
3178aa9ebccSVladimir Oltean 		table->entry_count = 0;
3188aa9ebccSVladimir Oltean 	}
3198aa9ebccSVladimir Oltean 
3208aa9ebccSVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
3218aa9ebccSVladimir Oltean 				 GFP_KERNEL);
3228aa9ebccSVladimir Oltean 	if (!table->entries)
3238aa9ebccSVladimir Oltean 		return -ENOMEM;
3248aa9ebccSVladimir Oltean 
3258aa9ebccSVladimir Oltean 	table->entry_count = 1;
3268aa9ebccSVladimir Oltean 
327e3502b82SVladimir Oltean 	/* VLAN 1: all DT-defined ports are members; no restrictions on
328ec5ae610SVladimir Oltean 	 * forwarding; always transmit as untagged.
3298aa9ebccSVladimir Oltean 	 */
330ec5ae610SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
331ec5ae610SVladimir Oltean 		struct sja1105_bridge_vlan *v;
332ec5ae610SVladimir Oltean 
333ec5ae610SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
334ec5ae610SVladimir Oltean 			continue;
335ec5ae610SVladimir Oltean 
336ec5ae610SVladimir Oltean 		pvid.vmemb_port |= BIT(port);
337ec5ae610SVladimir Oltean 		pvid.vlan_bc |= BIT(port);
338ec5ae610SVladimir Oltean 		pvid.tag_port &= ~BIT(port);
339ec5ae610SVladimir Oltean 
340ec5ae610SVladimir Oltean 		/* Let traffic that don't need dsa_8021q (e.g. STP, PTP) be
341ec5ae610SVladimir Oltean 		 * transmitted as untagged.
342ec5ae610SVladimir Oltean 		 */
343ec5ae610SVladimir Oltean 		v = kzalloc(sizeof(*v), GFP_KERNEL);
344ec5ae610SVladimir Oltean 		if (!v)
345ec5ae610SVladimir Oltean 			return -ENOMEM;
346ec5ae610SVladimir Oltean 
347ec5ae610SVladimir Oltean 		v->port = port;
348ec5ae610SVladimir Oltean 		v->vid = 1;
349ec5ae610SVladimir Oltean 		v->untagged = true;
350ec5ae610SVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
351ec5ae610SVladimir Oltean 			v->pvid = true;
352ec5ae610SVladimir Oltean 		list_add(&v->list, &priv->dsa_8021q_vlans);
3538aa9ebccSVladimir Oltean 	}
3548aa9ebccSVladimir Oltean 
3558aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
3568aa9ebccSVladimir Oltean 	return 0;
3578aa9ebccSVladimir Oltean }
3588aa9ebccSVladimir Oltean 
3598aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
3608aa9ebccSVladimir Oltean {
3618aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
3628aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3638aa9ebccSVladimir Oltean 	int i, j;
3648aa9ebccSVladimir Oltean 
3658aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
3668aa9ebccSVladimir Oltean 
3678aa9ebccSVladimir Oltean 	if (table->entry_count) {
3688aa9ebccSVladimir Oltean 		kfree(table->entries);
3698aa9ebccSVladimir Oltean 		table->entry_count = 0;
3708aa9ebccSVladimir Oltean 	}
3718aa9ebccSVladimir Oltean 
3728aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
3738aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3748aa9ebccSVladimir Oltean 	if (!table->entries)
3758aa9ebccSVladimir Oltean 		return -ENOMEM;
3768aa9ebccSVladimir Oltean 
3778aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
3788aa9ebccSVladimir Oltean 
3798aa9ebccSVladimir Oltean 	l2fwd = table->entries;
3808aa9ebccSVladimir Oltean 
3818aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
3828aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3838aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
3848aa9ebccSVladimir Oltean 
3858aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
3868aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
3878aa9ebccSVladimir Oltean 
3888aa9ebccSVladimir Oltean 		if (i == upstream)
3898aa9ebccSVladimir Oltean 			continue;
3908aa9ebccSVladimir Oltean 
3918aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
3928aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
3938aa9ebccSVladimir Oltean 	}
3948aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
3958aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
3968aa9ebccSVladimir Oltean 	 */
3978aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++)
3988aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_PORTS; j++)
3998aa9ebccSVladimir Oltean 			l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
4008aa9ebccSVladimir Oltean 
4018aa9ebccSVladimir Oltean 	return 0;
4028aa9ebccSVladimir Oltean }
4038aa9ebccSVladimir Oltean 
4048aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
4058aa9ebccSVladimir Oltean {
4068aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
4078aa9ebccSVladimir Oltean 		/* Disallow dynamic reconfiguration of vlan_pmap */
4088aa9ebccSVladimir Oltean 		.max_dynp = 0,
4098aa9ebccSVladimir Oltean 		/* Use a single memory partition for all ingress queues */
4108aa9ebccSVladimir Oltean 		.part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
4118aa9ebccSVladimir Oltean 	};
4128aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4138aa9ebccSVladimir Oltean 
4148aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
4158aa9ebccSVladimir Oltean 
4168aa9ebccSVladimir Oltean 	if (table->entry_count) {
4178aa9ebccSVladimir Oltean 		kfree(table->entries);
4188aa9ebccSVladimir Oltean 		table->entry_count = 0;
4198aa9ebccSVladimir Oltean 	}
4208aa9ebccSVladimir Oltean 
4218aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
4228aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4238aa9ebccSVladimir Oltean 	if (!table->entries)
4248aa9ebccSVladimir Oltean 		return -ENOMEM;
4258aa9ebccSVladimir Oltean 
4268aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
4278aa9ebccSVladimir Oltean 
4288aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4298aa9ebccSVladimir Oltean 	((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
4308aa9ebccSVladimir Oltean 				default_l2fwd_params;
4318aa9ebccSVladimir Oltean 
4328aa9ebccSVladimir Oltean 	return 0;
4338aa9ebccSVladimir Oltean }
4348aa9ebccSVladimir Oltean 
435aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
436aaa270c6SVladimir Oltean {
437aaa270c6SVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
438aaa270c6SVladimir Oltean 	struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
439aaa270c6SVladimir Oltean 	struct sja1105_table *table;
440aaa270c6SVladimir Oltean 	int max_mem;
441aaa270c6SVladimir Oltean 
442aaa270c6SVladimir Oltean 	/* VLAN retagging is implemented using a loopback port that consumes
443aaa270c6SVladimir Oltean 	 * frame buffers. That leaves less for us.
444aaa270c6SVladimir Oltean 	 */
445aaa270c6SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_BEST_EFFORT)
446aaa270c6SVladimir Oltean 		max_mem = SJA1105_MAX_FRAME_MEMORY_RETAGGING;
447aaa270c6SVladimir Oltean 	else
448aaa270c6SVladimir Oltean 		max_mem = SJA1105_MAX_FRAME_MEMORY;
449aaa270c6SVladimir Oltean 
450aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
451aaa270c6SVladimir Oltean 	l2_fwd_params = table->entries;
452aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] = max_mem;
453aaa270c6SVladimir Oltean 
454aaa270c6SVladimir Oltean 	/* If we have any critical-traffic virtual links, we need to reserve
455aaa270c6SVladimir Oltean 	 * some frame buffer memory for them. At the moment, hardcode the value
456aaa270c6SVladimir Oltean 	 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
457aaa270c6SVladimir Oltean 	 * remaining for best-effort traffic. TODO: figure out a more flexible
458aaa270c6SVladimir Oltean 	 * way to perform the frame buffer partitioning.
459aaa270c6SVladimir Oltean 	 */
460aaa270c6SVladimir Oltean 	if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
461aaa270c6SVladimir Oltean 		return;
462aaa270c6SVladimir Oltean 
463aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
464aaa270c6SVladimir Oltean 	vl_fwd_params = table->entries;
465aaa270c6SVladimir Oltean 
466aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
467aaa270c6SVladimir Oltean 	vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
468aaa270c6SVladimir Oltean }
469aaa270c6SVladimir Oltean 
4708aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
4718aa9ebccSVladimir Oltean {
4728aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
473511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
474511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
4758aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
4765f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
4775f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
4785f06c63bSVladimir Oltean 		 */
47908fde09aSVladimir Oltean 		.hostprio = 7,
4808aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
4818aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
48242824463SVladimir Oltean 		.incl_srcpt1 = false,
4838aa9ebccSVladimir Oltean 		.send_meta1  = false,
4848aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
4858aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
48642824463SVladimir Oltean 		.incl_srcpt0 = false,
4878aa9ebccSVladimir Oltean 		.send_meta0  = false,
4888aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
4898aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
4908aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
4918aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
4928aa9ebccSVladimir Oltean 		 */
4938aa9ebccSVladimir Oltean 		.host_port = dsa_upstream_port(priv->ds, 0),
494511e6ca0SVladimir Oltean 		/* Default to an invalid value */
495511e6ca0SVladimir Oltean 		.mirr_port = SJA1105_NUM_PORTS,
4968aa9ebccSVladimir Oltean 		/* Link-local traffic received on casc_port will be forwarded
4978aa9ebccSVladimir Oltean 		 * to host_port without embedding the source port and device ID
4988aa9ebccSVladimir Oltean 		 * info in the destination MAC address (presumably because it
4998aa9ebccSVladimir Oltean 		 * is a cascaded port and a downstream SJA switch already did
5008aa9ebccSVladimir Oltean 		 * that). Default to an invalid port (to disable the feature)
5018aa9ebccSVladimir Oltean 		 * and overwrite this if we find any DSA (cascaded) ports.
5028aa9ebccSVladimir Oltean 		 */
5038aa9ebccSVladimir Oltean 		.casc_port = SJA1105_NUM_PORTS,
5048aa9ebccSVladimir Oltean 		/* No TTEthernet */
505dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
5068aa9ebccSVladimir Oltean 		.vlmarker = 0,
5078aa9ebccSVladimir Oltean 		.vlmask = 0,
5088aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
5098aa9ebccSVladimir Oltean 		.ignore2stf = 0,
5106666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
5116666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
5126666cebcSVladimir Oltean 		 */
5136666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
5146666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
5158aa9ebccSVladimir Oltean 	};
5168aa9ebccSVladimir Oltean 	struct sja1105_table *table;
5178aa9ebccSVladimir Oltean 
5188aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
5198aa9ebccSVladimir Oltean 
5208aa9ebccSVladimir Oltean 	if (table->entry_count) {
5218aa9ebccSVladimir Oltean 		kfree(table->entries);
5228aa9ebccSVladimir Oltean 		table->entry_count = 0;
5238aa9ebccSVladimir Oltean 	}
5248aa9ebccSVladimir Oltean 
5258aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
5268aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5278aa9ebccSVladimir Oltean 	if (!table->entries)
5288aa9ebccSVladimir Oltean 		return -ENOMEM;
5298aa9ebccSVladimir Oltean 
5308aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
5318aa9ebccSVladimir Oltean 
5328aa9ebccSVladimir Oltean 	/* This table only has a single entry */
5338aa9ebccSVladimir Oltean 	((struct sja1105_general_params_entry *)table->entries)[0] =
5348aa9ebccSVladimir Oltean 				default_general_params;
5358aa9ebccSVladimir Oltean 
5368aa9ebccSVladimir Oltean 	return 0;
5378aa9ebccSVladimir Oltean }
5388aa9ebccSVladimir Oltean 
53979d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
54079d5511cSVladimir Oltean {
54179d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
54279d5511cSVladimir Oltean 	struct sja1105_table *table;
54379d5511cSVladimir Oltean 
54479d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
54579d5511cSVladimir Oltean 
54679d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
54779d5511cSVladimir Oltean 	if (table->entry_count) {
54879d5511cSVladimir Oltean 		kfree(table->entries);
54979d5511cSVladimir Oltean 		table->entry_count = 0;
55079d5511cSVladimir Oltean 	}
55179d5511cSVladimir Oltean 
55279d5511cSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
55379d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
55479d5511cSVladimir Oltean 	if (!table->entries)
55579d5511cSVladimir Oltean 		return -ENOMEM;
55679d5511cSVladimir Oltean 
55779d5511cSVladimir Oltean 	table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
55879d5511cSVladimir Oltean 
55979d5511cSVladimir Oltean 	avb = table->entries;
56079d5511cSVladimir Oltean 
56179d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
56279d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
56379d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
564747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
565747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
566747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
567747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
568747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
569747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
570747e5eb3SVladimir Oltean 	 */
571747e5eb3SVladimir Oltean 	avb->cas_master = false;
57279d5511cSVladimir Oltean 
57379d5511cSVladimir Oltean 	return 0;
57479d5511cSVladimir Oltean }
57579d5511cSVladimir Oltean 
576a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
577a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
578a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
579a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
580a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
581a7cc081cSVladimir Oltean  * will be used for this frame.
582a7cc081cSVladimir Oltean  *
583a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
584a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
585a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
586a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
587a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
588a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
589a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
590a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
591a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
592a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
593a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
594a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
595a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
596a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
597a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
598a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
599a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
600a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
601a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
602a7cc081cSVladimir Oltean  * +------------+--------+
603a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
604a7cc081cSVladimir Oltean  * +------------+--------+
605a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
606a7cc081cSVladimir Oltean  * +------------+--------+
607a7cc081cSVladimir Oltean  *    ...                                  ...
608a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
609a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
610a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
611a7cc081cSVladimir Oltean  *
612a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
613a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
614a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
615a7cc081cSVladimir Oltean  * lookup) equal.
616a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
617a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
618a7cc081cSVladimir Oltean  */
6198aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
6208aa9ebccSVladimir Oltean 
6218aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
6228aa9ebccSVladimir Oltean {
6238aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
6248aa9ebccSVladimir Oltean 	struct sja1105_table *table;
625a7cc081cSVladimir Oltean 	int port, tc;
6268aa9ebccSVladimir Oltean 
6278aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
6288aa9ebccSVladimir Oltean 
6298aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
6308aa9ebccSVladimir Oltean 	if (table->entry_count) {
6318aa9ebccSVladimir Oltean 		kfree(table->entries);
6328aa9ebccSVladimir Oltean 		table->entry_count = 0;
6338aa9ebccSVladimir Oltean 	}
6348aa9ebccSVladimir Oltean 
6358aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
6368aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
6378aa9ebccSVladimir Oltean 	if (!table->entries)
6388aa9ebccSVladimir Oltean 		return -ENOMEM;
6398aa9ebccSVladimir Oltean 
6408aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
6418aa9ebccSVladimir Oltean 
6428aa9ebccSVladimir Oltean 	policing = table->entries;
6438aa9ebccSVladimir Oltean 
644a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
645a7cc081cSVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
646a7cc081cSVladimir Oltean 		int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + port;
647a7cc081cSVladimir Oltean 
648a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
649a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
650a7cc081cSVladimir Oltean 
651a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
652a7cc081cSVladimir Oltean 	}
653a7cc081cSVladimir Oltean 
654a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
655a7cc081cSVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
656c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
657c279c726SVladimir Oltean 
658a7cc081cSVladimir Oltean 		if (dsa_is_cpu_port(priv->ds, port))
659c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
6608aa9ebccSVladimir Oltean 
661a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
662a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
663a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
664a7cc081cSVladimir Oltean 		policing[port].partition = 0;
6658aa9ebccSVladimir Oltean 	}
666a7cc081cSVladimir Oltean 
6678aa9ebccSVladimir Oltean 	return 0;
6688aa9ebccSVladimir Oltean }
6698aa9ebccSVladimir Oltean 
6708aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv,
6718aa9ebccSVladimir Oltean 				      struct sja1105_dt_port *ports)
6728aa9ebccSVladimir Oltean {
6738aa9ebccSVladimir Oltean 	int rc;
6748aa9ebccSVladimir Oltean 
6758aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
6768aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
6778aa9ebccSVladimir Oltean 					priv->info->static_ops,
6788aa9ebccSVladimir Oltean 					priv->info->device_id);
6798aa9ebccSVladimir Oltean 	if (rc)
6808aa9ebccSVladimir Oltean 		return rc;
6818aa9ebccSVladimir Oltean 
6828aa9ebccSVladimir Oltean 	/* Build static configuration */
6838aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
6848aa9ebccSVladimir Oltean 	if (rc < 0)
6858aa9ebccSVladimir Oltean 		return rc;
6868aa9ebccSVladimir Oltean 	rc = sja1105_init_mii_settings(priv, ports);
6878aa9ebccSVladimir Oltean 	if (rc < 0)
6888aa9ebccSVladimir Oltean 		return rc;
6898aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
6908aa9ebccSVladimir Oltean 	if (rc < 0)
6918aa9ebccSVladimir Oltean 		return rc;
6928aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
6938aa9ebccSVladimir Oltean 	if (rc < 0)
6948aa9ebccSVladimir Oltean 		return rc;
6958aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
6968aa9ebccSVladimir Oltean 	if (rc < 0)
6978aa9ebccSVladimir Oltean 		return rc;
6988aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
6998aa9ebccSVladimir Oltean 	if (rc < 0)
7008aa9ebccSVladimir Oltean 		return rc;
7018aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
7028aa9ebccSVladimir Oltean 	if (rc < 0)
7038aa9ebccSVladimir Oltean 		return rc;
7048aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
7058aa9ebccSVladimir Oltean 	if (rc < 0)
7068aa9ebccSVladimir Oltean 		return rc;
7078aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
7088aa9ebccSVladimir Oltean 	if (rc < 0)
7098aa9ebccSVladimir Oltean 		return rc;
71079d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
71179d5511cSVladimir Oltean 	if (rc < 0)
71279d5511cSVladimir Oltean 		return rc;
7138aa9ebccSVladimir Oltean 
7148aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
7158aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
7168aa9ebccSVladimir Oltean }
7178aa9ebccSVladimir Oltean 
718f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
719f5b8631cSVladimir Oltean 				      const struct sja1105_dt_port *ports)
720f5b8631cSVladimir Oltean {
721f5b8631cSVladimir Oltean 	int i;
722f5b8631cSVladimir Oltean 
723f5b8631cSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
7249bca3a0aSOleksij Rempel 		if (ports[i].role == XMII_MAC)
725f5b8631cSVladimir Oltean 			continue;
726f5b8631cSVladimir Oltean 
7279bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
7289bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
729f5b8631cSVladimir Oltean 			priv->rgmii_rx_delay[i] = true;
730f5b8631cSVladimir Oltean 
7319bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
7329bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
733f5b8631cSVladimir Oltean 			priv->rgmii_tx_delay[i] = true;
734f5b8631cSVladimir Oltean 
735f5b8631cSVladimir Oltean 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
736f5b8631cSVladimir Oltean 		     !priv->info->setup_rgmii_delay)
737f5b8631cSVladimir Oltean 			return -EINVAL;
738f5b8631cSVladimir Oltean 	}
739f5b8631cSVladimir Oltean 	return 0;
740f5b8631cSVladimir Oltean }
741f5b8631cSVladimir Oltean 
7428aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
7438aa9ebccSVladimir Oltean 				    struct sja1105_dt_port *ports,
7448aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
7458aa9ebccSVladimir Oltean {
7468aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
7478aa9ebccSVladimir Oltean 	struct device_node *child;
7488aa9ebccSVladimir Oltean 
74927afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
7508aa9ebccSVladimir Oltean 		struct device_node *phy_node;
7510c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
7528aa9ebccSVladimir Oltean 		u32 index;
7530c65b2b9SAndrew Lunn 		int err;
7548aa9ebccSVladimir Oltean 
7558aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
7568aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
7578aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
7588aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
7597ba771e3SNishka Dasgupta 			of_node_put(child);
7608aa9ebccSVladimir Oltean 			return -ENODEV;
7618aa9ebccSVladimir Oltean 		}
7628aa9ebccSVladimir Oltean 
7638aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
7640c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
7650c65b2b9SAndrew Lunn 		if (err) {
7668aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
7678aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
7688aa9ebccSVladimir Oltean 				index);
7697ba771e3SNishka Dasgupta 			of_node_put(child);
7708aa9ebccSVladimir Oltean 			return -ENODEV;
7718aa9ebccSVladimir Oltean 		}
7728aa9ebccSVladimir Oltean 		ports[index].phy_mode = phy_mode;
7738aa9ebccSVladimir Oltean 
7748aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
7758aa9ebccSVladimir Oltean 		if (!phy_node) {
7768aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
7778aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
7788aa9ebccSVladimir Oltean 					"properties missing!\n");
7797ba771e3SNishka Dasgupta 				of_node_put(child);
7808aa9ebccSVladimir Oltean 				return -ENODEV;
7818aa9ebccSVladimir Oltean 			}
7828aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
7838aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
7848aa9ebccSVladimir Oltean 			 */
7858aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
7868aa9ebccSVladimir Oltean 		} else {
7878aa9ebccSVladimir Oltean 			/* phy-handle present => put port in MAC role */
7888aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
7898aa9ebccSVladimir Oltean 			of_node_put(phy_node);
7908aa9ebccSVladimir Oltean 		}
7918aa9ebccSVladimir Oltean 
7928aa9ebccSVladimir Oltean 		/* The MAC/PHY role can be overridden with explicit bindings */
7938aa9ebccSVladimir Oltean 		if (of_property_read_bool(child, "sja1105,role-mac"))
7948aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
7958aa9ebccSVladimir Oltean 		else if (of_property_read_bool(child, "sja1105,role-phy"))
7968aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
7978aa9ebccSVladimir Oltean 	}
7988aa9ebccSVladimir Oltean 
7998aa9ebccSVladimir Oltean 	return 0;
8008aa9ebccSVladimir Oltean }
8018aa9ebccSVladimir Oltean 
8028aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv,
8038aa9ebccSVladimir Oltean 			    struct sja1105_dt_port *ports)
8048aa9ebccSVladimir Oltean {
8058aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
8068aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
8078aa9ebccSVladimir Oltean 	struct device_node *ports_node;
8088aa9ebccSVladimir Oltean 	int rc;
8098aa9ebccSVladimir Oltean 
8108aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
8118aa9ebccSVladimir Oltean 	if (!ports_node) {
8128aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
8138aa9ebccSVladimir Oltean 		return -ENODEV;
8148aa9ebccSVladimir Oltean 	}
8158aa9ebccSVladimir Oltean 
8168aa9ebccSVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
8178aa9ebccSVladimir Oltean 	of_node_put(ports_node);
8188aa9ebccSVladimir Oltean 
8198aa9ebccSVladimir Oltean 	return rc;
8208aa9ebccSVladimir Oltean }
8218aa9ebccSVladimir Oltean 
822ffe10e67SVladimir Oltean static int sja1105_sgmii_read(struct sja1105_private *priv, int pcs_reg)
823ffe10e67SVladimir Oltean {
824ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
825ffe10e67SVladimir Oltean 	u32 val;
826ffe10e67SVladimir Oltean 	int rc;
827ffe10e67SVladimir Oltean 
828ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->sgmii + pcs_reg, &val,
829ffe10e67SVladimir Oltean 			      NULL);
830ffe10e67SVladimir Oltean 	if (rc < 0)
831ffe10e67SVladimir Oltean 		return rc;
832ffe10e67SVladimir Oltean 
833ffe10e67SVladimir Oltean 	return val;
834ffe10e67SVladimir Oltean }
835ffe10e67SVladimir Oltean 
836ffe10e67SVladimir Oltean static int sja1105_sgmii_write(struct sja1105_private *priv, int pcs_reg,
837ffe10e67SVladimir Oltean 			       u16 pcs_val)
838ffe10e67SVladimir Oltean {
839ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
840ffe10e67SVladimir Oltean 	u32 val = pcs_val;
841ffe10e67SVladimir Oltean 	int rc;
842ffe10e67SVladimir Oltean 
843ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->sgmii + pcs_reg, &val,
844ffe10e67SVladimir Oltean 			      NULL);
845ffe10e67SVladimir Oltean 	if (rc < 0)
846ffe10e67SVladimir Oltean 		return rc;
847ffe10e67SVladimir Oltean 
848ffe10e67SVladimir Oltean 	return val;
849ffe10e67SVladimir Oltean }
850ffe10e67SVladimir Oltean 
851ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_config(struct sja1105_private *priv,
852ffe10e67SVladimir Oltean 				     bool an_enabled, bool an_master)
853ffe10e67SVladimir Oltean {
854ffe10e67SVladimir Oltean 	u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII;
855ffe10e67SVladimir Oltean 
856ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
857ffe10e67SVladimir Oltean 	 * stop the clock during LPI mode, make the MAC reconfigure
858ffe10e67SVladimir Oltean 	 * autonomously after PCS autoneg is done, flush the internal FIFOs.
859ffe10e67SVladimir Oltean 	 */
860ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC1, SJA1105_DC1_EN_VSMMD1 |
861ffe10e67SVladimir Oltean 					       SJA1105_DC1_CLOCK_STOP_EN |
862ffe10e67SVladimir Oltean 					       SJA1105_DC1_MAC_AUTO_SW |
863ffe10e67SVladimir Oltean 					       SJA1105_DC1_INIT);
864ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
865ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC2, SJA1105_DC2_TX_POL_INV_DISABLE);
866ffe10e67SVladimir Oltean 	/* AUTONEG_CONTROL: Use SGMII autoneg */
867ffe10e67SVladimir Oltean 	if (an_master)
868ffe10e67SVladimir Oltean 		ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK;
869ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_AC, ac);
870ffe10e67SVladimir Oltean 	/* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
871ffe10e67SVladimir Oltean 	 * sja1105_sgmii_pcs_force_speed must be called later for the link
872ffe10e67SVladimir Oltean 	 * to become operational.
873ffe10e67SVladimir Oltean 	 */
874ffe10e67SVladimir Oltean 	if (an_enabled)
875ffe10e67SVladimir Oltean 		sja1105_sgmii_write(priv, MII_BMCR,
876ffe10e67SVladimir Oltean 				    BMCR_ANENABLE | BMCR_ANRESTART);
877ffe10e67SVladimir Oltean }
878ffe10e67SVladimir Oltean 
879ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv,
880ffe10e67SVladimir Oltean 					  int speed)
881ffe10e67SVladimir Oltean {
882ffe10e67SVladimir Oltean 	int pcs_speed;
883ffe10e67SVladimir Oltean 
884ffe10e67SVladimir Oltean 	switch (speed) {
885ffe10e67SVladimir Oltean 	case SPEED_1000:
886ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED1000;
887ffe10e67SVladimir Oltean 		break;
888ffe10e67SVladimir Oltean 	case SPEED_100:
889ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED100;
890ffe10e67SVladimir Oltean 		break;
891ffe10e67SVladimir Oltean 	case SPEED_10:
892ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED10;
893ffe10e67SVladimir Oltean 		break;
894ffe10e67SVladimir Oltean 	default:
895ffe10e67SVladimir Oltean 		dev_err(priv->ds->dev, "Invalid speed %d\n", speed);
896ffe10e67SVladimir Oltean 		return;
897ffe10e67SVladimir Oltean 	}
898ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, MII_BMCR, pcs_speed | BMCR_FULLDPLX);
899ffe10e67SVladimir Oltean }
900ffe10e67SVladimir Oltean 
901c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
9028aa9ebccSVladimir Oltean static int sja1105_speed[] = {
903c44d0535SVladimir Oltean 	[SJA1105_SPEED_AUTO]		= SPEED_UNKNOWN,
904c44d0535SVladimir Oltean 	[SJA1105_SPEED_10MBPS]		= SPEED_10,
905c44d0535SVladimir Oltean 	[SJA1105_SPEED_100MBPS]		= SPEED_100,
906c44d0535SVladimir Oltean 	[SJA1105_SPEED_1000MBPS]	= SPEED_1000,
9078aa9ebccSVladimir Oltean };
9088aa9ebccSVladimir Oltean 
9098400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
9108aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
9118400cff6SVladimir Oltean 				      int speed_mbps)
9128aa9ebccSVladimir Oltean {
9138aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
9148aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
9158aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
9168aa9ebccSVladimir Oltean 	sja1105_phy_interface_t phy_mode;
9178aa9ebccSVladimir Oltean 	sja1105_speed_t speed;
9188aa9ebccSVladimir Oltean 	int rc;
9198aa9ebccSVladimir Oltean 
9208400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
9218400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
9228400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
9238400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
9248400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
9258400cff6SVladimir Oltean 	 */
9268aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
9278400cff6SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
9288aa9ebccSVladimir Oltean 
929f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
930c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
931a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
932a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
933a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
934a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
935a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
936a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
937a979a0abSVladimir Oltean 		 */
938f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_AUTO;
939f4cfcfbdSVladimir Oltean 		break;
940c44d0535SVladimir Oltean 	case SPEED_10:
941f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_10MBPS;
942f4cfcfbdSVladimir Oltean 		break;
943c44d0535SVladimir Oltean 	case SPEED_100:
944f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_100MBPS;
945f4cfcfbdSVladimir Oltean 		break;
946c44d0535SVladimir Oltean 	case SPEED_1000:
947f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_1000MBPS;
948f4cfcfbdSVladimir Oltean 		break;
949f4cfcfbdSVladimir Oltean 	default:
9508aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
9518aa9ebccSVladimir Oltean 		return -EINVAL;
9528aa9ebccSVladimir Oltean 	}
9538aa9ebccSVladimir Oltean 
9548400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
9558400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
9568400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
9578400cff6SVladimir Oltean 	 * we want auto during upload phase).
958ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
959ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
9608aa9ebccSVladimir Oltean 	 */
961ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port))
962ffe10e67SVladimir Oltean 		mac[port].speed = SJA1105_SPEED_1000MBPS;
963ffe10e67SVladimir Oltean 	else
9648aa9ebccSVladimir Oltean 		mac[port].speed = speed;
9658aa9ebccSVladimir Oltean 
9668aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
9678400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
9688400cff6SVladimir Oltean 					  &mac[port], true);
9698aa9ebccSVladimir Oltean 	if (rc < 0) {
9708aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
9718aa9ebccSVladimir Oltean 		return rc;
9728aa9ebccSVladimir Oltean 	}
9738aa9ebccSVladimir Oltean 
9748aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
9758aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
9768aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
9778aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
9788aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
9798aa9ebccSVladimir Oltean 	 */
9808aa9ebccSVladimir Oltean 	phy_mode = mii->xmii_mode[port];
9818aa9ebccSVladimir Oltean 	if (phy_mode != XMII_MODE_RGMII)
9828aa9ebccSVladimir Oltean 		return 0;
9838aa9ebccSVladimir Oltean 
9848aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
9858aa9ebccSVladimir Oltean }
9868aa9ebccSVladimir Oltean 
98739710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
98839710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
98939710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
99039710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
99139710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
99239710229SVladimir Oltean  * now.
99339710229SVladimir Oltean  */
99439710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
99539710229SVladimir Oltean 				      phy_interface_t interface)
99639710229SVladimir Oltean {
99739710229SVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
99839710229SVladimir Oltean 	sja1105_phy_interface_t phy_mode;
99939710229SVladimir Oltean 
100039710229SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
100139710229SVladimir Oltean 	phy_mode = mii->xmii_mode[port];
100239710229SVladimir Oltean 
100339710229SVladimir Oltean 	switch (interface) {
100439710229SVladimir Oltean 	case PHY_INTERFACE_MODE_MII:
100539710229SVladimir Oltean 		return (phy_mode != XMII_MODE_MII);
100639710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RMII:
100739710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RMII);
100839710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII:
100939710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_ID:
101039710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_RXID:
101139710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_TXID:
101239710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RGMII);
1013ffe10e67SVladimir Oltean 	case PHY_INTERFACE_MODE_SGMII:
1014ffe10e67SVladimir Oltean 		return (phy_mode != XMII_MODE_SGMII);
101539710229SVladimir Oltean 	default:
101639710229SVladimir Oltean 		return true;
101739710229SVladimir Oltean 	}
101839710229SVladimir Oltean }
101939710229SVladimir Oltean 
1020af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
1021ffe10e67SVladimir Oltean 			       unsigned int mode,
1022af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
10238aa9ebccSVladimir Oltean {
10248aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1025ffe10e67SVladimir Oltean 	bool is_sgmii = sja1105_supports_sgmii(priv, port);
10268aa9ebccSVladimir Oltean 
1027ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1028ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
1029ec8582d1SVladimir Oltean 			phy_modes(state->interface));
103039710229SVladimir Oltean 		return;
1031ec8582d1SVladimir Oltean 	}
103239710229SVladimir Oltean 
1033ffe10e67SVladimir Oltean 	if (phylink_autoneg_inband(mode) && !is_sgmii) {
10349f971573SVladimir Oltean 		dev_err(ds->dev, "In-band AN not supported!\n");
10359f971573SVladimir Oltean 		return;
10369f971573SVladimir Oltean 	}
1037ffe10e67SVladimir Oltean 
1038ffe10e67SVladimir Oltean 	if (is_sgmii)
1039ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, phylink_autoneg_inband(mode),
1040ffe10e67SVladimir Oltean 					 false);
10418400cff6SVladimir Oltean }
10428400cff6SVladimir Oltean 
10438400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
10448400cff6SVladimir Oltean 				  unsigned int mode,
10458400cff6SVladimir Oltean 				  phy_interface_t interface)
10468400cff6SVladimir Oltean {
10478400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
10488400cff6SVladimir Oltean }
10498400cff6SVladimir Oltean 
10508400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
10518400cff6SVladimir Oltean 				unsigned int mode,
10528400cff6SVladimir Oltean 				phy_interface_t interface,
10535b502a7bSRussell King 				struct phy_device *phydev,
10545b502a7bSRussell King 				int speed, int duplex,
10555b502a7bSRussell King 				bool tx_pause, bool rx_pause)
10568400cff6SVladimir Oltean {
1057ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1058ec8582d1SVladimir Oltean 
1059ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1060ec8582d1SVladimir Oltean 
1061ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode))
1062ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_force_speed(priv, speed);
1063ffe10e67SVladimir Oltean 
1064ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
10658aa9ebccSVladimir Oltean }
10668aa9ebccSVladimir Oltean 
1067ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1068ad9f299aSVladimir Oltean 				     unsigned long *supported,
1069ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
1070ad9f299aSVladimir Oltean {
1071ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
1072ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
1073ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
1074ad9f299aSVladimir Oltean 	 */
1075ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1076ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1077ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1078ad9f299aSVladimir Oltean 
1079ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1080ad9f299aSVladimir Oltean 
108139710229SVladimir Oltean 	/* include/linux/phylink.h says:
108239710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
108339710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
108439710229SVladimir Oltean 	 */
108539710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
108639710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
108739710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
108839710229SVladimir Oltean 		return;
108939710229SVladimir Oltean 	}
109039710229SVladimir Oltean 
1091ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1092ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1093ad9f299aSVladimir Oltean 	 */
1094ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
1095ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
1096ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
1097ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
1098ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
1099ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1100ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1101ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
1102ad9f299aSVladimir Oltean 
1103ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1104ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1105ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1106ad9f299aSVladimir Oltean }
1107ad9f299aSVladimir Oltean 
1108ffe10e67SVladimir Oltean static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port,
1109ffe10e67SVladimir Oltean 				     struct phylink_link_state *state)
1110ffe10e67SVladimir Oltean {
1111ffe10e67SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1112ffe10e67SVladimir Oltean 	int ais;
1113ffe10e67SVladimir Oltean 
1114ffe10e67SVladimir Oltean 	/* Read the vendor-specific AUTONEG_INTR_STATUS register */
1115ffe10e67SVladimir Oltean 	ais = sja1105_sgmii_read(priv, SJA1105_AIS);
1116ffe10e67SVladimir Oltean 	if (ais < 0)
1117ffe10e67SVladimir Oltean 		return ais;
1118ffe10e67SVladimir Oltean 
1119ffe10e67SVladimir Oltean 	switch (SJA1105_AIS_SPEED(ais)) {
1120ffe10e67SVladimir Oltean 	case 0:
1121ffe10e67SVladimir Oltean 		state->speed = SPEED_10;
1122ffe10e67SVladimir Oltean 		break;
1123ffe10e67SVladimir Oltean 	case 1:
1124ffe10e67SVladimir Oltean 		state->speed = SPEED_100;
1125ffe10e67SVladimir Oltean 		break;
1126ffe10e67SVladimir Oltean 	case 2:
1127ffe10e67SVladimir Oltean 		state->speed = SPEED_1000;
1128ffe10e67SVladimir Oltean 		break;
1129ffe10e67SVladimir Oltean 	default:
1130ffe10e67SVladimir Oltean 		dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n",
1131ffe10e67SVladimir Oltean 			SJA1105_AIS_SPEED(ais));
1132ffe10e67SVladimir Oltean 	}
1133ffe10e67SVladimir Oltean 	state->duplex = SJA1105_AIS_DUPLEX_MODE(ais);
1134ffe10e67SVladimir Oltean 	state->an_complete = SJA1105_AIS_COMPLETE(ais);
1135ffe10e67SVladimir Oltean 	state->link = SJA1105_AIS_LINK_STATUS(ais);
1136ffe10e67SVladimir Oltean 
1137ffe10e67SVladimir Oltean 	return 0;
1138ffe10e67SVladimir Oltean }
1139ffe10e67SVladimir Oltean 
114060f6053fSVladimir Oltean static int
114160f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
114260f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
114360f6053fSVladimir Oltean {
114460f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
114560f6053fSVladimir Oltean 	struct sja1105_table *table;
114660f6053fSVladimir Oltean 	int i;
114760f6053fSVladimir Oltean 
114860f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
114960f6053fSVladimir Oltean 	l2_lookup = table->entries;
115060f6053fSVladimir Oltean 
115160f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
115260f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
115360f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
115460f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
115560f6053fSVladimir Oltean 			return i;
115660f6053fSVladimir Oltean 
115760f6053fSVladimir Oltean 	return -1;
115860f6053fSVladimir Oltean }
115960f6053fSVladimir Oltean 
116060f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
116160f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
116260f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
116360f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
116460f6053fSVladimir Oltean  */
116560f6053fSVladimir Oltean static int
116660f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
116760f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
116860f6053fSVladimir Oltean 			  bool keep)
116960f6053fSVladimir Oltean {
117060f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
117160f6053fSVladimir Oltean 	struct sja1105_table *table;
117260f6053fSVladimir Oltean 	int rc, match;
117360f6053fSVladimir Oltean 
117460f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
117560f6053fSVladimir Oltean 
117660f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
117760f6053fSVladimir Oltean 	if (match < 0) {
117860f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
117960f6053fSVladimir Oltean 		if (!keep)
118060f6053fSVladimir Oltean 			return 0;
118160f6053fSVladimir Oltean 
118260f6053fSVladimir Oltean 		/* No match => new entry */
118360f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
118460f6053fSVladimir Oltean 		if (rc)
118560f6053fSVladimir Oltean 			return rc;
118660f6053fSVladimir Oltean 
118760f6053fSVladimir Oltean 		match = table->entry_count - 1;
118860f6053fSVladimir Oltean 	}
118960f6053fSVladimir Oltean 
119060f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
119160f6053fSVladimir Oltean 	l2_lookup = table->entries;
119260f6053fSVladimir Oltean 
119360f6053fSVladimir Oltean 	/* We have a match.
119460f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
119560f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
119660f6053fSVladimir Oltean 	 * which we update it).
119760f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
119860f6053fSVladimir Oltean 	 */
119960f6053fSVladimir Oltean 	if (keep) {
120060f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
120160f6053fSVladimir Oltean 		return 0;
120260f6053fSVladimir Oltean 	}
120360f6053fSVladimir Oltean 
120460f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
120560f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
120660f6053fSVladimir Oltean 	 */
120760f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
120860f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
120960f6053fSVladimir Oltean }
121060f6053fSVladimir Oltean 
1211291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1212291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1213291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1214291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1215291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1216291d1e72SVladimir Oltean  */
121709c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1218291d1e72SVladimir Oltean {
1219291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1220291d1e72SVladimir Oltean }
1221291d1e72SVladimir Oltean 
12229dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1223291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1224291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1225291d1e72SVladimir Oltean 					 int *last_unused)
1226291d1e72SVladimir Oltean {
1227291d1e72SVladimir Oltean 	int way;
1228291d1e72SVladimir Oltean 
1229291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1230291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1231291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1232291d1e72SVladimir Oltean 
1233291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1234291d1e72SVladimir Oltean 		 * into the return value
1235291d1e72SVladimir Oltean 		 */
1236291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1237291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1238291d1e72SVladimir Oltean 			if (last_unused)
1239291d1e72SVladimir Oltean 				*last_unused = way;
1240291d1e72SVladimir Oltean 			continue;
1241291d1e72SVladimir Oltean 		}
1242291d1e72SVladimir Oltean 
1243291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1244291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1245291d1e72SVladimir Oltean 			if (match)
1246291d1e72SVladimir Oltean 				*match = l2_lookup;
1247291d1e72SVladimir Oltean 			return way;
1248291d1e72SVladimir Oltean 		}
1249291d1e72SVladimir Oltean 	}
1250291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1251291d1e72SVladimir Oltean 	return -1;
1252291d1e72SVladimir Oltean }
1253291d1e72SVladimir Oltean 
12549dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1255291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1256291d1e72SVladimir Oltean {
1257291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1258291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1259291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1260291d1e72SVladimir Oltean 	int last_unused = -1;
126160f6053fSVladimir Oltean 	int bin, way, rc;
1262291d1e72SVladimir Oltean 
12639dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1264291d1e72SVladimir Oltean 
12659dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1266291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1267291d1e72SVladimir Oltean 	if (way >= 0) {
1268291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1269291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1270291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1271291d1e72SVladimir Oltean 		 */
1272291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1273291d1e72SVladimir Oltean 			return 0;
1274291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1275291d1e72SVladimir Oltean 	} else {
1276291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1277291d1e72SVladimir Oltean 
1278291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1279291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1280291d1e72SVladimir Oltean 		 */
1281291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1282291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1283291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1284291d1e72SVladimir Oltean 
1285291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1286291d1e72SVladimir Oltean 			way = last_unused;
1287291d1e72SVladimir Oltean 		} else {
1288291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1289291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1290291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1291291d1e72SVladimir Oltean 			 * distribution function:
1292291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1293291d1e72SVladimir Oltean 			 */
1294291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1295291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1296291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1297291d1e72SVladimir Oltean 				 bin, addr, way);
1298291d1e72SVladimir Oltean 			/* Evict entry */
1299291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1300291d1e72SVladimir Oltean 						     index, NULL, false);
1301291d1e72SVladimir Oltean 		}
1302291d1e72SVladimir Oltean 	}
1303291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1304291d1e72SVladimir Oltean 
130560f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1306291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1307291d1e72SVladimir Oltean 					  true);
130860f6053fSVladimir Oltean 	if (rc < 0)
130960f6053fSVladimir Oltean 		return rc;
131060f6053fSVladimir Oltean 
131160f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1312291d1e72SVladimir Oltean }
1313291d1e72SVladimir Oltean 
13149dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1315291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1316291d1e72SVladimir Oltean {
1317291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1318291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
131960f6053fSVladimir Oltean 	int index, bin, way, rc;
1320291d1e72SVladimir Oltean 	bool keep;
1321291d1e72SVladimir Oltean 
13229dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
13239dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1324291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1325291d1e72SVladimir Oltean 	if (way < 0)
1326291d1e72SVladimir Oltean 		return 0;
1327291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1328291d1e72SVladimir Oltean 
1329291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1330291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1331291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1332291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1333291d1e72SVladimir Oltean 	 */
1334291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
13357752e937SVladimir Oltean 
1336291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1337291d1e72SVladimir Oltean 		keep = true;
1338291d1e72SVladimir Oltean 	else
1339291d1e72SVladimir Oltean 		keep = false;
1340291d1e72SVladimir Oltean 
134160f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1342291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
134360f6053fSVladimir Oltean 	if (rc < 0)
134460f6053fSVladimir Oltean 		return rc;
134560f6053fSVladimir Oltean 
134660f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1347291d1e72SVladimir Oltean }
1348291d1e72SVladimir Oltean 
13499dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
13509dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
13519dfa6911SVladimir Oltean {
13521da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
13531da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
13541da73821SVladimir Oltean 	int rc, i;
13551da73821SVladimir Oltean 
13561da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
13571da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
13581da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
13591da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
13601da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
13617f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
13621da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
13631da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
13646d7c7d94SVladimir Oltean 	} else {
13656d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
13666d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
13676d7c7d94SVladimir Oltean 	}
13681da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
13691da73821SVladimir Oltean 
13701da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13711da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
13721da73821SVladimir Oltean 	if (rc == 0) {
13731da73821SVladimir Oltean 		/* Found and this port is already in the entry's
13741da73821SVladimir Oltean 		 * port mask => job done
13751da73821SVladimir Oltean 		 */
13761da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
13771da73821SVladimir Oltean 			return 0;
13781da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
13791da73821SVladimir Oltean 		 * found something.
13801da73821SVladimir Oltean 		 */
13811da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
13821da73821SVladimir Oltean 		goto skip_finding_an_index;
13831da73821SVladimir Oltean 	}
13841da73821SVladimir Oltean 
13851da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
13861da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
13871da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
13881da73821SVladimir Oltean 	 */
13891da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
13901da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13911da73821SVladimir Oltean 						 i, NULL);
13921da73821SVladimir Oltean 		if (rc < 0)
13931da73821SVladimir Oltean 			break;
13941da73821SVladimir Oltean 	}
13951da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
13961da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
13971da73821SVladimir Oltean 		return -EINVAL;
13981da73821SVladimir Oltean 	}
139917ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
14001da73821SVladimir Oltean 	l2_lookup.index = i;
14011da73821SVladimir Oltean 
14021da73821SVladimir Oltean skip_finding_an_index:
140360f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
14041da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
14051da73821SVladimir Oltean 					  true);
140660f6053fSVladimir Oltean 	if (rc < 0)
140760f6053fSVladimir Oltean 		return rc;
140860f6053fSVladimir Oltean 
140960f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
14109dfa6911SVladimir Oltean }
14119dfa6911SVladimir Oltean 
14129dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
14139dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
14149dfa6911SVladimir Oltean {
14151da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
14161da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14171da73821SVladimir Oltean 	bool keep;
14181da73821SVladimir Oltean 	int rc;
14191da73821SVladimir Oltean 
14201da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
14211da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
14221da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
14231da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
14247f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
14251da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
14261da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
14276d7c7d94SVladimir Oltean 	} else {
14286d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
14296d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
14306d7c7d94SVladimir Oltean 	}
14311da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
14321da73821SVladimir Oltean 
14331da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
14341da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
14351da73821SVladimir Oltean 	if (rc < 0)
14361da73821SVladimir Oltean 		return 0;
14371da73821SVladimir Oltean 
14381da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
14391da73821SVladimir Oltean 
14401da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
14411da73821SVladimir Oltean 	 * or if we remove it completely.
14421da73821SVladimir Oltean 	 */
14431da73821SVladimir Oltean 	if (l2_lookup.destports)
14441da73821SVladimir Oltean 		keep = true;
14451da73821SVladimir Oltean 	else
14461da73821SVladimir Oltean 		keep = false;
14471da73821SVladimir Oltean 
144860f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
14491da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
145060f6053fSVladimir Oltean 	if (rc < 0)
145160f6053fSVladimir Oltean 		return rc;
145260f6053fSVladimir Oltean 
145360f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
14549dfa6911SVladimir Oltean }
14559dfa6911SVladimir Oltean 
14569dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
14579dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
14589dfa6911SVladimir Oltean {
14599dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1460b3ee526aSVladimir Oltean 
14616d7c7d94SVladimir Oltean 	/* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
14626d7c7d94SVladimir Oltean 	 * so the switch still does some VLAN processing internally.
14636d7c7d94SVladimir Oltean 	 * But Shared VLAN Learning (SVL) is also active, and it will take
14646d7c7d94SVladimir Oltean 	 * care of autonomous forwarding between the unique pvid's of each
14656d7c7d94SVladimir Oltean 	 * port.  Here we just make sure that users can't add duplicate FDB
14666d7c7d94SVladimir Oltean 	 * entries when in this mode - the actual VID doesn't matter except
14676d7c7d94SVladimir Oltean 	 * for what gets printed in 'bridge fdb show'.  In the case of zero,
14686d7c7d94SVladimir Oltean 	 * no VID gets printed at all.
146993647594SVladimir Oltean 	 */
14707f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
14716d7c7d94SVladimir Oltean 		vid = 0;
147293647594SVladimir Oltean 
14736d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
14749dfa6911SVladimir Oltean }
14759dfa6911SVladimir Oltean 
14769dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
14779dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
14789dfa6911SVladimir Oltean {
14799dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14809dfa6911SVladimir Oltean 
14817f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
14826d7c7d94SVladimir Oltean 		vid = 0;
14836d7c7d94SVladimir Oltean 
1484b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
14859dfa6911SVladimir Oltean }
14869dfa6911SVladimir Oltean 
1487291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1488291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1489291d1e72SVladimir Oltean {
1490291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1491291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1492291d1e72SVladimir Oltean 	int i;
1493291d1e72SVladimir Oltean 
1494291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1495291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1496291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1497291d1e72SVladimir Oltean 		int rc;
1498291d1e72SVladimir Oltean 
1499291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1500291d1e72SVladimir Oltean 						 i, &l2_lookup);
1501291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1502def84604SVladimir Oltean 		if (rc == -ENOENT)
1503291d1e72SVladimir Oltean 			continue;
1504291d1e72SVladimir Oltean 		if (rc) {
1505291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1506291d1e72SVladimir Oltean 			return rc;
1507291d1e72SVladimir Oltean 		}
1508291d1e72SVladimir Oltean 
1509291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1510291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1511291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1512291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1513291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1514291d1e72SVladimir Oltean 		 */
1515291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1516291d1e72SVladimir Oltean 			continue;
1517291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
151893647594SVladimir Oltean 
15196d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
15207f14937fSVladimir Oltean 		if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
15216d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
152217ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1523291d1e72SVladimir Oltean 	}
1524291d1e72SVladimir Oltean 	return 0;
1525291d1e72SVladimir Oltean }
1526291d1e72SVladimir Oltean 
1527291d1e72SVladimir Oltean /* This callback needs to be present */
1528291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1529291d1e72SVladimir Oltean 			       const struct switchdev_obj_port_mdb *mdb)
1530291d1e72SVladimir Oltean {
1531291d1e72SVladimir Oltean 	return 0;
1532291d1e72SVladimir Oltean }
1533291d1e72SVladimir Oltean 
1534291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1535291d1e72SVladimir Oltean 			    const struct switchdev_obj_port_mdb *mdb)
1536291d1e72SVladimir Oltean {
1537291d1e72SVladimir Oltean 	sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1538291d1e72SVladimir Oltean }
1539291d1e72SVladimir Oltean 
1540291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1541291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1542291d1e72SVladimir Oltean {
1543291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1544291d1e72SVladimir Oltean }
1545291d1e72SVladimir Oltean 
15468aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
15478aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
15488aa9ebccSVladimir Oltean {
15498aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
15508aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15518aa9ebccSVladimir Oltean 	int i, rc;
15528aa9ebccSVladimir Oltean 
15538aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
15548aa9ebccSVladimir Oltean 
15558aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
15568aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
15578aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
15588aa9ebccSVladimir Oltean 		 */
15598aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
15608aa9ebccSVladimir Oltean 			continue;
15618aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
15628aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
15638aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
15648aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
15658aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
15668aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
15678aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
15688aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
15698aa9ebccSVladimir Oltean 		 */
15708aa9ebccSVladimir Oltean 		if (i == port)
15718aa9ebccSVladimir Oltean 			continue;
15728aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
15738aa9ebccSVladimir Oltean 			continue;
15748aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
15758aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
15768aa9ebccSVladimir Oltean 
15778aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
15788aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
15798aa9ebccSVladimir Oltean 		if (rc < 0)
15808aa9ebccSVladimir Oltean 			return rc;
15818aa9ebccSVladimir Oltean 	}
15828aa9ebccSVladimir Oltean 
15838aa9ebccSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
15848aa9ebccSVladimir Oltean 					    port, &l2_fwd[port], true);
15858aa9ebccSVladimir Oltean }
15868aa9ebccSVladimir Oltean 
1587640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1588640f763fSVladimir Oltean 					 u8 state)
1589640f763fSVladimir Oltean {
1590640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1591640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1592640f763fSVladimir Oltean 
1593640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1594640f763fSVladimir Oltean 
1595640f763fSVladimir Oltean 	switch (state) {
1596640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1597640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1598640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1599640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1600640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1601640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1602640f763fSVladimir Oltean 		 */
1603640f763fSVladimir Oltean 		mac[port].ingress   = false;
1604640f763fSVladimir Oltean 		mac[port].egress    = false;
1605640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1606640f763fSVladimir Oltean 		break;
1607640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1608640f763fSVladimir Oltean 		mac[port].ingress   = true;
1609640f763fSVladimir Oltean 		mac[port].egress    = false;
1610640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1611640f763fSVladimir Oltean 		break;
1612640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1613640f763fSVladimir Oltean 		mac[port].ingress   = true;
1614640f763fSVladimir Oltean 		mac[port].egress    = false;
1615640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1616640f763fSVladimir Oltean 		break;
1617640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1618640f763fSVladimir Oltean 		mac[port].ingress   = true;
1619640f763fSVladimir Oltean 		mac[port].egress    = true;
1620640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1621640f763fSVladimir Oltean 		break;
1622640f763fSVladimir Oltean 	default:
1623640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1624640f763fSVladimir Oltean 		return;
1625640f763fSVladimir Oltean 	}
1626640f763fSVladimir Oltean 
1627640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1628640f763fSVladimir Oltean 				     &mac[port], true);
1629640f763fSVladimir Oltean }
1630640f763fSVladimir Oltean 
16318aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
16328aa9ebccSVladimir Oltean 			       struct net_device *br)
16338aa9ebccSVladimir Oltean {
16348aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
16358aa9ebccSVladimir Oltean }
16368aa9ebccSVladimir Oltean 
16378aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
16388aa9ebccSVladimir Oltean 				 struct net_device *br)
16398aa9ebccSVladimir Oltean {
16408aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
16418aa9ebccSVladimir Oltean }
16428aa9ebccSVladimir Oltean 
16434d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8)
16444d752508SVladimir Oltean 
16454d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
16464d752508SVladimir Oltean {
16474d752508SVladimir Oltean 	int i;
16484d752508SVladimir Oltean 
16494d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
16504d752508SVladimir Oltean 		if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
16514d752508SVladimir Oltean 			return i;
16524d752508SVladimir Oltean 
16534d752508SVladimir Oltean 	return -1;
16544d752508SVladimir Oltean }
16554d752508SVladimir Oltean 
16564d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
16574d752508SVladimir Oltean 				     int prio)
16584d752508SVladimir Oltean {
16594d752508SVladimir Oltean 	int i;
16604d752508SVladimir Oltean 
16614d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
16624d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
16634d752508SVladimir Oltean 
16644d752508SVladimir Oltean 		if (cbs->port == port && cbs->prio == prio) {
16654d752508SVladimir Oltean 			memset(cbs, 0, sizeof(*cbs));
16664d752508SVladimir Oltean 			return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
16674d752508SVladimir Oltean 							    i, cbs, true);
16684d752508SVladimir Oltean 		}
16694d752508SVladimir Oltean 	}
16704d752508SVladimir Oltean 
16714d752508SVladimir Oltean 	return 0;
16724d752508SVladimir Oltean }
16734d752508SVladimir Oltean 
16744d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
16754d752508SVladimir Oltean 				struct tc_cbs_qopt_offload *offload)
16764d752508SVladimir Oltean {
16774d752508SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16784d752508SVladimir Oltean 	struct sja1105_cbs_entry *cbs;
16794d752508SVladimir Oltean 	int index;
16804d752508SVladimir Oltean 
16814d752508SVladimir Oltean 	if (!offload->enable)
16824d752508SVladimir Oltean 		return sja1105_delete_cbs_shaper(priv, port, offload->queue);
16834d752508SVladimir Oltean 
16844d752508SVladimir Oltean 	index = sja1105_find_unused_cbs_shaper(priv);
16854d752508SVladimir Oltean 	if (index < 0)
16864d752508SVladimir Oltean 		return -ENOSPC;
16874d752508SVladimir Oltean 
16884d752508SVladimir Oltean 	cbs = &priv->cbs[index];
16894d752508SVladimir Oltean 	cbs->port = port;
16904d752508SVladimir Oltean 	cbs->prio = offload->queue;
16914d752508SVladimir Oltean 	/* locredit and sendslope are negative by definition. In hardware,
16924d752508SVladimir Oltean 	 * positive values must be provided, and the negative sign is implicit.
16934d752508SVladimir Oltean 	 */
16944d752508SVladimir Oltean 	cbs->credit_hi = offload->hicredit;
16954d752508SVladimir Oltean 	cbs->credit_lo = abs(offload->locredit);
16964d752508SVladimir Oltean 	/* User space is in kbits/sec, hardware in bytes/sec */
16974d752508SVladimir Oltean 	cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
16984d752508SVladimir Oltean 	cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
16994d752508SVladimir Oltean 	/* Convert the negative values from 64-bit 2's complement
17004d752508SVladimir Oltean 	 * to 32-bit 2's complement (for the case of 0x80000000 whose
17014d752508SVladimir Oltean 	 * negative is still negative).
17024d752508SVladimir Oltean 	 */
17034d752508SVladimir Oltean 	cbs->credit_lo &= GENMASK_ULL(31, 0);
17044d752508SVladimir Oltean 	cbs->send_slope &= GENMASK_ULL(31, 0);
17054d752508SVladimir Oltean 
17064d752508SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
17074d752508SVladimir Oltean 					    true);
17084d752508SVladimir Oltean }
17094d752508SVladimir Oltean 
17104d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv)
17114d752508SVladimir Oltean {
17124d752508SVladimir Oltean 	int rc = 0, i;
17134d752508SVladimir Oltean 
17144d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
17154d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
17164d752508SVladimir Oltean 
17174d752508SVladimir Oltean 		if (!cbs->idle_slope && !cbs->send_slope)
17184d752508SVladimir Oltean 			continue;
17194d752508SVladimir Oltean 
17204d752508SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
17214d752508SVladimir Oltean 						  true);
17224d752508SVladimir Oltean 		if (rc)
17234d752508SVladimir Oltean 			break;
17244d752508SVladimir Oltean 	}
17254d752508SVladimir Oltean 
17264d752508SVladimir Oltean 	return rc;
17274d752508SVladimir Oltean }
17284d752508SVladimir Oltean 
17292eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
17302eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
17312eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
17322eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
17332eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
1734c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1735dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
17362eea1fa8SVladimir Oltean };
17372eea1fa8SVladimir Oltean 
17386666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
17396666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
17406666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
17416666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
17426666cebcSVladimir Oltean  * such that this operation is relatively seamless.
17436666cebcSVladimir Oltean  */
17442eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
17452eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
17466666cebcSVladimir Oltean {
17476cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
17486cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
17496666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
17506666cebcSVladimir Oltean 	int speed_mbps[SJA1105_NUM_PORTS];
17516cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
17526cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
17536cf99c13SVladimir Oltean 	s64 t12, t34;
1754ffe10e67SVladimir Oltean 	u16 bmcr = 0;
17556666cebcSVladimir Oltean 	int rc, i;
17566cf99c13SVladimir Oltean 	s64 now;
17576666cebcSVladimir Oltean 
1758af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1759af580ae2SVladimir Oltean 
17606666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
17616666cebcSVladimir Oltean 
17628400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
17638400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
17648400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
17658400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
17666666cebcSVladimir Oltean 	 */
17676666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
17686666cebcSVladimir Oltean 		speed_mbps[i] = sja1105_speed[mac[i].speed];
17696666cebcSVladimir Oltean 		mac[i].speed = SJA1105_SPEED_AUTO;
17706666cebcSVladimir Oltean 	}
17716666cebcSVladimir Oltean 
1772ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT))
1773ffe10e67SVladimir Oltean 		bmcr = sja1105_sgmii_read(priv, MII_BMCR);
1774ffe10e67SVladimir Oltean 
17756cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
17766cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
17776cf99c13SVladimir Oltean 
17786cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
17796cf99c13SVladimir Oltean 	if (rc < 0)
17806cf99c13SVladimir Oltean 		goto out_unlock_ptp;
17816cf99c13SVladimir Oltean 
17826666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
17836666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
17846666cebcSVladimir Oltean 	if (rc < 0)
17856cf99c13SVladimir Oltean 		goto out_unlock_ptp;
17866cf99c13SVladimir Oltean 
17876cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
17886cf99c13SVladimir Oltean 	if (rc < 0)
17896cf99c13SVladimir Oltean 		goto out_unlock_ptp;
17906cf99c13SVladimir Oltean 
17916cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
17926cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
17936cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
17946cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
17956cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
17966cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
17976cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
17986cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
17996cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
18006cf99c13SVladimir Oltean 	now += (t34 - t12);
18016cf99c13SVladimir Oltean 
18026cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
18036cf99c13SVladimir Oltean 
18046cf99c13SVladimir Oltean out_unlock_ptp:
18056cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
18066666cebcSVladimir Oltean 
18072eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
18082eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
18092eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
18102eea1fa8SVladimir Oltean 
18116666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
18126666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
18136666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
18146666cebcSVladimir Oltean 	 */
18156666cebcSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
18166666cebcSVladimir Oltean 	if (rc < 0)
18176666cebcSVladimir Oltean 		goto out;
18186666cebcSVladimir Oltean 
18196666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
18208400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
18216666cebcSVladimir Oltean 		if (rc < 0)
18226666cebcSVladimir Oltean 			goto out;
18236666cebcSVladimir Oltean 	}
1824ffe10e67SVladimir Oltean 
1825ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) {
1826ffe10e67SVladimir Oltean 		bool an_enabled = !!(bmcr & BMCR_ANENABLE);
1827ffe10e67SVladimir Oltean 
1828ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, an_enabled, false);
1829ffe10e67SVladimir Oltean 
1830ffe10e67SVladimir Oltean 		if (!an_enabled) {
1831ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
1832ffe10e67SVladimir Oltean 
1833ffe10e67SVladimir Oltean 			if (bmcr & BMCR_SPEED1000)
1834ffe10e67SVladimir Oltean 				speed = SPEED_1000;
1835ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED100)
1836ffe10e67SVladimir Oltean 				speed = SPEED_100;
1837ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED10)
1838ffe10e67SVladimir Oltean 				speed = SPEED_10;
1839ffe10e67SVladimir Oltean 
1840ffe10e67SVladimir Oltean 			sja1105_sgmii_pcs_force_speed(priv, speed);
1841ffe10e67SVladimir Oltean 		}
1842ffe10e67SVladimir Oltean 	}
18434d752508SVladimir Oltean 
18444d752508SVladimir Oltean 	rc = sja1105_reload_cbs(priv);
18454d752508SVladimir Oltean 	if (rc < 0)
18464d752508SVladimir Oltean 		goto out;
18476666cebcSVladimir Oltean out:
1848af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1849af580ae2SVladimir Oltean 
18506666cebcSVladimir Oltean 	return rc;
18516666cebcSVladimir Oltean }
18526666cebcSVladimir Oltean 
18536666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
18546666cebcSVladimir Oltean {
18556666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
18566666cebcSVladimir Oltean 
18576666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
18586666cebcSVladimir Oltean 
18596666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
18606666cebcSVladimir Oltean 
18616666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
18626666cebcSVladimir Oltean 					   &mac[port], true);
18636666cebcSVladimir Oltean }
18646666cebcSVladimir Oltean 
1865ac02a451SVladimir Oltean static int sja1105_crosschip_bridge_join(struct dsa_switch *ds,
1866ac02a451SVladimir Oltean 					 int tree_index, int sw_index,
1867ac02a451SVladimir Oltean 					 int other_port, struct net_device *br)
1868ac02a451SVladimir Oltean {
1869ac02a451SVladimir Oltean 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1870ac02a451SVladimir Oltean 	struct sja1105_private *other_priv = other_ds->priv;
1871ac02a451SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1872ac02a451SVladimir Oltean 	int port, rc;
1873ac02a451SVladimir Oltean 
1874ac02a451SVladimir Oltean 	if (other_ds->ops != &sja1105_switch_ops)
1875ac02a451SVladimir Oltean 		return 0;
1876ac02a451SVladimir Oltean 
1877ac02a451SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1878ac02a451SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
1879ac02a451SVladimir Oltean 			continue;
1880ac02a451SVladimir Oltean 		if (dsa_to_port(ds, port)->bridge_dev != br)
1881ac02a451SVladimir Oltean 			continue;
1882ac02a451SVladimir Oltean 
1883*5899ee36SVladimir Oltean 		rc = dsa_8021q_crosschip_bridge_join(priv->dsa_8021q_ctx,
1884*5899ee36SVladimir Oltean 						     port,
1885*5899ee36SVladimir Oltean 						     other_priv->dsa_8021q_ctx,
1886*5899ee36SVladimir Oltean 						     other_port);
1887ac02a451SVladimir Oltean 		if (rc)
1888ac02a451SVladimir Oltean 			return rc;
1889ac02a451SVladimir Oltean 
1890*5899ee36SVladimir Oltean 		rc = dsa_8021q_crosschip_bridge_join(other_priv->dsa_8021q_ctx,
1891*5899ee36SVladimir Oltean 						     other_port,
1892*5899ee36SVladimir Oltean 						     priv->dsa_8021q_ctx,
1893*5899ee36SVladimir Oltean 						     port);
1894ac02a451SVladimir Oltean 		if (rc)
1895ac02a451SVladimir Oltean 			return rc;
1896ac02a451SVladimir Oltean 	}
1897ac02a451SVladimir Oltean 
1898ac02a451SVladimir Oltean 	return 0;
1899ac02a451SVladimir Oltean }
1900ac02a451SVladimir Oltean 
1901ac02a451SVladimir Oltean static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds,
1902ac02a451SVladimir Oltean 					   int tree_index, int sw_index,
1903ac02a451SVladimir Oltean 					   int other_port,
1904ac02a451SVladimir Oltean 					   struct net_device *br)
1905ac02a451SVladimir Oltean {
1906ac02a451SVladimir Oltean 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1907ac02a451SVladimir Oltean 	struct sja1105_private *other_priv = other_ds->priv;
1908ac02a451SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1909ac02a451SVladimir Oltean 	int port;
1910ac02a451SVladimir Oltean 
1911ac02a451SVladimir Oltean 	if (other_ds->ops != &sja1105_switch_ops)
1912ac02a451SVladimir Oltean 		return;
1913ac02a451SVladimir Oltean 
1914ac02a451SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1915ac02a451SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
1916ac02a451SVladimir Oltean 			continue;
1917ac02a451SVladimir Oltean 		if (dsa_to_port(ds, port)->bridge_dev != br)
1918ac02a451SVladimir Oltean 			continue;
1919ac02a451SVladimir Oltean 
1920*5899ee36SVladimir Oltean 		dsa_8021q_crosschip_bridge_leave(priv->dsa_8021q_ctx, port,
1921*5899ee36SVladimir Oltean 						 other_priv->dsa_8021q_ctx,
1922*5899ee36SVladimir Oltean 						 other_port);
1923ac02a451SVladimir Oltean 
1924*5899ee36SVladimir Oltean 		dsa_8021q_crosschip_bridge_leave(other_priv->dsa_8021q_ctx,
1925*5899ee36SVladimir Oltean 						 other_port,
1926*5899ee36SVladimir Oltean 						 priv->dsa_8021q_ctx, port);
1927ac02a451SVladimir Oltean 	}
1928ac02a451SVladimir Oltean }
1929ac02a451SVladimir Oltean 
1930227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1931227d07a0SVladimir Oltean {
193260b33aebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
19337e092af2SVladimir Oltean 	int rc;
1934227d07a0SVladimir Oltean 
1935*5899ee36SVladimir Oltean 	rc = dsa_8021q_setup(priv->dsa_8021q_ctx, enabled);
19367e092af2SVladimir Oltean 	if (rc)
1937227d07a0SVladimir Oltean 		return rc;
1938ac02a451SVladimir Oltean 
1939227d07a0SVladimir Oltean 	dev_info(ds->dev, "%s switch tagging\n",
1940227d07a0SVladimir Oltean 		 enabled ? "Enabled" : "Disabled");
1941227d07a0SVladimir Oltean 	return 0;
1942227d07a0SVladimir Oltean }
1943227d07a0SVladimir Oltean 
19448aa9ebccSVladimir Oltean static enum dsa_tag_protocol
19454d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
19464d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
19478aa9ebccSVladimir Oltean {
1948227d07a0SVladimir Oltean 	return DSA_TAG_PROTO_SJA1105;
19498aa9ebccSVladimir Oltean }
19508aa9ebccSVladimir Oltean 
19513f01c91aSVladimir Oltean static int sja1105_find_free_subvlan(u16 *subvlan_map, bool pvid)
19523f01c91aSVladimir Oltean {
19533f01c91aSVladimir Oltean 	int subvlan;
19543f01c91aSVladimir Oltean 
19553f01c91aSVladimir Oltean 	if (pvid)
19563f01c91aSVladimir Oltean 		return 0;
19573f01c91aSVladimir Oltean 
19583f01c91aSVladimir Oltean 	for (subvlan = 1; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
19593f01c91aSVladimir Oltean 		if (subvlan_map[subvlan] == VLAN_N_VID)
19603f01c91aSVladimir Oltean 			return subvlan;
19613f01c91aSVladimir Oltean 
19623f01c91aSVladimir Oltean 	return -1;
19633f01c91aSVladimir Oltean }
19643f01c91aSVladimir Oltean 
19653f01c91aSVladimir Oltean static int sja1105_find_subvlan(u16 *subvlan_map, u16 vid)
19663f01c91aSVladimir Oltean {
19673f01c91aSVladimir Oltean 	int subvlan;
19683f01c91aSVladimir Oltean 
19693f01c91aSVladimir Oltean 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
19703f01c91aSVladimir Oltean 		if (subvlan_map[subvlan] == vid)
19713f01c91aSVladimir Oltean 			return subvlan;
19723f01c91aSVladimir Oltean 
19733f01c91aSVladimir Oltean 	return -1;
19743f01c91aSVladimir Oltean }
19753f01c91aSVladimir Oltean 
19763f01c91aSVladimir Oltean static int sja1105_find_committed_subvlan(struct sja1105_private *priv,
19773f01c91aSVladimir Oltean 					  int port, u16 vid)
19783f01c91aSVladimir Oltean {
19793f01c91aSVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
19803f01c91aSVladimir Oltean 
19813f01c91aSVladimir Oltean 	return sja1105_find_subvlan(sp->subvlan_map, vid);
19823f01c91aSVladimir Oltean }
19833f01c91aSVladimir Oltean 
19843f01c91aSVladimir Oltean static void sja1105_init_subvlan_map(u16 *subvlan_map)
19853f01c91aSVladimir Oltean {
19863f01c91aSVladimir Oltean 	int subvlan;
19873f01c91aSVladimir Oltean 
19883f01c91aSVladimir Oltean 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
19893f01c91aSVladimir Oltean 		subvlan_map[subvlan] = VLAN_N_VID;
19903f01c91aSVladimir Oltean }
19913f01c91aSVladimir Oltean 
19923f01c91aSVladimir Oltean static void sja1105_commit_subvlan_map(struct sja1105_private *priv, int port,
19933f01c91aSVladimir Oltean 				       u16 *subvlan_map)
19943f01c91aSVladimir Oltean {
19953f01c91aSVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
19963f01c91aSVladimir Oltean 	int subvlan;
19973f01c91aSVladimir Oltean 
19983f01c91aSVladimir Oltean 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
19993f01c91aSVladimir Oltean 		sp->subvlan_map[subvlan] = subvlan_map[subvlan];
20003f01c91aSVladimir Oltean }
20013f01c91aSVladimir Oltean 
2002ec5ae610SVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
2003ec5ae610SVladimir Oltean {
2004ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
2005ec5ae610SVladimir Oltean 	int count, i;
2006ec5ae610SVladimir Oltean 
2007ec5ae610SVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
2008ec5ae610SVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
2009ec5ae610SVladimir Oltean 
2010ec5ae610SVladimir Oltean 	for (i = 0; i < count; i++)
2011ec5ae610SVladimir Oltean 		if (vlan[i].vlanid == vid)
2012ec5ae610SVladimir Oltean 			return i;
2013ec5ae610SVladimir Oltean 
2014ec5ae610SVladimir Oltean 	/* Return an invalid entry index if not found */
2015ec5ae610SVladimir Oltean 	return -1;
2016ec5ae610SVladimir Oltean }
2017ec5ae610SVladimir Oltean 
20183f01c91aSVladimir Oltean static int
20193f01c91aSVladimir Oltean sja1105_find_retagging_entry(struct sja1105_retagging_entry *retagging,
20203f01c91aSVladimir Oltean 			     int count, int from_port, u16 from_vid,
20213f01c91aSVladimir Oltean 			     u16 to_vid)
2022ec5ae610SVladimir Oltean {
20233f01c91aSVladimir Oltean 	int i;
20243f01c91aSVladimir Oltean 
20253f01c91aSVladimir Oltean 	for (i = 0; i < count; i++)
20263f01c91aSVladimir Oltean 		if (retagging[i].ing_port == BIT(from_port) &&
20273f01c91aSVladimir Oltean 		    retagging[i].vlan_ing == from_vid &&
20283f01c91aSVladimir Oltean 		    retagging[i].vlan_egr == to_vid)
20293f01c91aSVladimir Oltean 			return i;
20303f01c91aSVladimir Oltean 
20313f01c91aSVladimir Oltean 	/* Return an invalid entry index if not found */
20323f01c91aSVladimir Oltean 	return -1;
20333f01c91aSVladimir Oltean }
20343f01c91aSVladimir Oltean 
20353f01c91aSVladimir Oltean static int sja1105_commit_vlans(struct sja1105_private *priv,
20363f01c91aSVladimir Oltean 				struct sja1105_vlan_lookup_entry *new_vlan,
20373f01c91aSVladimir Oltean 				struct sja1105_retagging_entry *new_retagging,
20383f01c91aSVladimir Oltean 				int num_retagging)
20393f01c91aSVladimir Oltean {
20403f01c91aSVladimir Oltean 	struct sja1105_retagging_entry *retagging;
2041ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
2042ec5ae610SVladimir Oltean 	struct sja1105_table *table;
2043ec5ae610SVladimir Oltean 	int num_vlans = 0;
2044ec5ae610SVladimir Oltean 	int rc, i, k = 0;
2045ec5ae610SVladimir Oltean 
2046ec5ae610SVladimir Oltean 	/* VLAN table */
2047ec5ae610SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2048ec5ae610SVladimir Oltean 	vlan = table->entries;
2049ec5ae610SVladimir Oltean 
2050ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++) {
2051ec5ae610SVladimir Oltean 		int match = sja1105_is_vlan_configured(priv, i);
2052ec5ae610SVladimir Oltean 
2053ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid != VLAN_N_VID)
2054ec5ae610SVladimir Oltean 			num_vlans++;
2055ec5ae610SVladimir Oltean 
2056ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) {
2057ec5ae610SVladimir Oltean 			/* Was there before, no longer is. Delete */
2058ec5ae610SVladimir Oltean 			dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i);
2059ec5ae610SVladimir Oltean 			rc = sja1105_dynamic_config_write(priv,
2060ec5ae610SVladimir Oltean 							  BLK_IDX_VLAN_LOOKUP,
2061ec5ae610SVladimir Oltean 							  i, &vlan[match], false);
2062ec5ae610SVladimir Oltean 			if (rc < 0)
2063ec5ae610SVladimir Oltean 				return rc;
2064ec5ae610SVladimir Oltean 		} else if (new_vlan[i].vlanid != VLAN_N_VID) {
2065ec5ae610SVladimir Oltean 			/* Nothing changed, don't do anything */
2066ec5ae610SVladimir Oltean 			if (match >= 0 &&
2067ec5ae610SVladimir Oltean 			    vlan[match].vlanid == new_vlan[i].vlanid &&
2068ec5ae610SVladimir Oltean 			    vlan[match].tag_port == new_vlan[i].tag_port &&
2069ec5ae610SVladimir Oltean 			    vlan[match].vlan_bc == new_vlan[i].vlan_bc &&
2070ec5ae610SVladimir Oltean 			    vlan[match].vmemb_port == new_vlan[i].vmemb_port)
2071ec5ae610SVladimir Oltean 				continue;
2072ec5ae610SVladimir Oltean 			/* Update entry */
2073ec5ae610SVladimir Oltean 			dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i);
2074ec5ae610SVladimir Oltean 			rc = sja1105_dynamic_config_write(priv,
2075ec5ae610SVladimir Oltean 							  BLK_IDX_VLAN_LOOKUP,
2076ec5ae610SVladimir Oltean 							  i, &new_vlan[i],
2077ec5ae610SVladimir Oltean 							  true);
2078ec5ae610SVladimir Oltean 			if (rc < 0)
2079ec5ae610SVladimir Oltean 				return rc;
2080ec5ae610SVladimir Oltean 		}
2081ec5ae610SVladimir Oltean 	}
2082ec5ae610SVladimir Oltean 
2083ec5ae610SVladimir Oltean 	if (table->entry_count)
2084ec5ae610SVladimir Oltean 		kfree(table->entries);
2085ec5ae610SVladimir Oltean 
2086ec5ae610SVladimir Oltean 	table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size,
2087ec5ae610SVladimir Oltean 				 GFP_KERNEL);
2088ec5ae610SVladimir Oltean 	if (!table->entries)
2089ec5ae610SVladimir Oltean 		return -ENOMEM;
2090ec5ae610SVladimir Oltean 
2091ec5ae610SVladimir Oltean 	table->entry_count = num_vlans;
2092ec5ae610SVladimir Oltean 	vlan = table->entries;
2093ec5ae610SVladimir Oltean 
2094ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++) {
2095ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid == VLAN_N_VID)
2096ec5ae610SVladimir Oltean 			continue;
2097ec5ae610SVladimir Oltean 		vlan[k++] = new_vlan[i];
2098ec5ae610SVladimir Oltean 	}
2099ec5ae610SVladimir Oltean 
21003f01c91aSVladimir Oltean 	/* VLAN Retagging Table */
21013f01c91aSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_RETAGGING];
21023f01c91aSVladimir Oltean 	retagging = table->entries;
21033f01c91aSVladimir Oltean 
21043f01c91aSVladimir Oltean 	for (i = 0; i < table->entry_count; i++) {
21053f01c91aSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
21063f01c91aSVladimir Oltean 						  i, &retagging[i], false);
21073f01c91aSVladimir Oltean 		if (rc)
21083f01c91aSVladimir Oltean 			return rc;
21093f01c91aSVladimir Oltean 	}
21103f01c91aSVladimir Oltean 
21113f01c91aSVladimir Oltean 	if (table->entry_count)
21123f01c91aSVladimir Oltean 		kfree(table->entries);
21133f01c91aSVladimir Oltean 
21143f01c91aSVladimir Oltean 	table->entries = kcalloc(num_retagging, table->ops->unpacked_entry_size,
21153f01c91aSVladimir Oltean 				 GFP_KERNEL);
21163f01c91aSVladimir Oltean 	if (!table->entries)
21173f01c91aSVladimir Oltean 		return -ENOMEM;
21183f01c91aSVladimir Oltean 
21193f01c91aSVladimir Oltean 	table->entry_count = num_retagging;
21203f01c91aSVladimir Oltean 	retagging = table->entries;
21213f01c91aSVladimir Oltean 
21223f01c91aSVladimir Oltean 	for (i = 0; i < num_retagging; i++) {
21233f01c91aSVladimir Oltean 		retagging[i] = new_retagging[i];
21243f01c91aSVladimir Oltean 
21253f01c91aSVladimir Oltean 		/* Update entry */
21263f01c91aSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
21273f01c91aSVladimir Oltean 						  i, &retagging[i], true);
21283f01c91aSVladimir Oltean 		if (rc < 0)
21293f01c91aSVladimir Oltean 			return rc;
21303f01c91aSVladimir Oltean 	}
21313f01c91aSVladimir Oltean 
2132ec5ae610SVladimir Oltean 	return 0;
2133ec5ae610SVladimir Oltean }
2134ec5ae610SVladimir Oltean 
21353f01c91aSVladimir Oltean struct sja1105_crosschip_vlan {
21363f01c91aSVladimir Oltean 	struct list_head list;
21373f01c91aSVladimir Oltean 	u16 vid;
21383f01c91aSVladimir Oltean 	bool untagged;
21393f01c91aSVladimir Oltean 	int port;
21403f01c91aSVladimir Oltean 	int other_port;
2141*5899ee36SVladimir Oltean 	struct dsa_8021q_context *other_ctx;
21423f01c91aSVladimir Oltean };
21433f01c91aSVladimir Oltean 
2144ec5ae610SVladimir Oltean struct sja1105_crosschip_switch {
2145ec5ae610SVladimir Oltean 	struct list_head list;
2146*5899ee36SVladimir Oltean 	struct dsa_8021q_context *other_ctx;
2147ec5ae610SVladimir Oltean };
2148ec5ae610SVladimir Oltean 
2149ec5ae610SVladimir Oltean static int sja1105_commit_pvid(struct sja1105_private *priv)
2150ec5ae610SVladimir Oltean {
2151ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
2152ec5ae610SVladimir Oltean 	struct list_head *vlan_list;
2153ec5ae610SVladimir Oltean 	int rc = 0;
2154ec5ae610SVladimir Oltean 
2155ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2156ec5ae610SVladimir Oltean 		vlan_list = &priv->bridge_vlans;
2157ec5ae610SVladimir Oltean 	else
2158ec5ae610SVladimir Oltean 		vlan_list = &priv->dsa_8021q_vlans;
2159ec5ae610SVladimir Oltean 
2160ec5ae610SVladimir Oltean 	list_for_each_entry(v, vlan_list, list) {
2161ec5ae610SVladimir Oltean 		if (v->pvid) {
2162ec5ae610SVladimir Oltean 			rc = sja1105_pvid_apply(priv, v->port, v->vid);
2163ec5ae610SVladimir Oltean 			if (rc)
2164ec5ae610SVladimir Oltean 				break;
2165ec5ae610SVladimir Oltean 		}
2166ec5ae610SVladimir Oltean 	}
2167ec5ae610SVladimir Oltean 
2168ec5ae610SVladimir Oltean 	return rc;
2169ec5ae610SVladimir Oltean }
2170ec5ae610SVladimir Oltean 
2171ec5ae610SVladimir Oltean static int
2172ec5ae610SVladimir Oltean sja1105_build_bridge_vlans(struct sja1105_private *priv,
2173ec5ae610SVladimir Oltean 			   struct sja1105_vlan_lookup_entry *new_vlan)
2174ec5ae610SVladimir Oltean {
2175ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
2176ec5ae610SVladimir Oltean 
2177ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
2178ec5ae610SVladimir Oltean 		return 0;
2179ec5ae610SVladimir Oltean 
2180ec5ae610SVladimir Oltean 	list_for_each_entry(v, &priv->bridge_vlans, list) {
2181ec5ae610SVladimir Oltean 		int match = v->vid;
2182ec5ae610SVladimir Oltean 
2183ec5ae610SVladimir Oltean 		new_vlan[match].vlanid = v->vid;
2184ec5ae610SVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(v->port);
2185ec5ae610SVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(v->port);
2186ec5ae610SVladimir Oltean 		if (!v->untagged)
2187ec5ae610SVladimir Oltean 			new_vlan[match].tag_port |= BIT(v->port);
2188ec5ae610SVladimir Oltean 	}
2189ec5ae610SVladimir Oltean 
2190ec5ae610SVladimir Oltean 	return 0;
2191ec5ae610SVladimir Oltean }
2192ec5ae610SVladimir Oltean 
2193ec5ae610SVladimir Oltean static int
2194ec5ae610SVladimir Oltean sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv,
2195ec5ae610SVladimir Oltean 			      struct sja1105_vlan_lookup_entry *new_vlan)
2196ec5ae610SVladimir Oltean {
2197ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
2198ec5ae610SVladimir Oltean 
2199ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2200ec5ae610SVladimir Oltean 		return 0;
2201ec5ae610SVladimir Oltean 
2202ec5ae610SVladimir Oltean 	list_for_each_entry(v, &priv->dsa_8021q_vlans, list) {
2203ec5ae610SVladimir Oltean 		int match = v->vid;
2204ec5ae610SVladimir Oltean 
2205ec5ae610SVladimir Oltean 		new_vlan[match].vlanid = v->vid;
2206ec5ae610SVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(v->port);
2207ec5ae610SVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(v->port);
2208ec5ae610SVladimir Oltean 		if (!v->untagged)
2209ec5ae610SVladimir Oltean 			new_vlan[match].tag_port |= BIT(v->port);
2210ec5ae610SVladimir Oltean 	}
2211ec5ae610SVladimir Oltean 
2212ec5ae610SVladimir Oltean 	return 0;
2213ec5ae610SVladimir Oltean }
2214ec5ae610SVladimir Oltean 
22153f01c91aSVladimir Oltean static int sja1105_build_subvlans(struct sja1105_private *priv,
22163f01c91aSVladimir Oltean 				  u16 subvlan_map[][DSA_8021Q_N_SUBVLAN],
22173f01c91aSVladimir Oltean 				  struct sja1105_vlan_lookup_entry *new_vlan,
22183f01c91aSVladimir Oltean 				  struct sja1105_retagging_entry *new_retagging,
22193f01c91aSVladimir Oltean 				  int *num_retagging)
22203f01c91aSVladimir Oltean {
22213f01c91aSVladimir Oltean 	struct sja1105_bridge_vlan *v;
22223f01c91aSVladimir Oltean 	int k = *num_retagging;
22233f01c91aSVladimir Oltean 
22243f01c91aSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
22253f01c91aSVladimir Oltean 		return 0;
22263f01c91aSVladimir Oltean 
22273f01c91aSVladimir Oltean 	list_for_each_entry(v, &priv->bridge_vlans, list) {
22283f01c91aSVladimir Oltean 		int upstream = dsa_upstream_port(priv->ds, v->port);
22293f01c91aSVladimir Oltean 		int match, subvlan;
22303f01c91aSVladimir Oltean 		u16 rx_vid;
22313f01c91aSVladimir Oltean 
22323f01c91aSVladimir Oltean 		/* Only sub-VLANs on user ports need to be applied.
22333f01c91aSVladimir Oltean 		 * Bridge VLANs also include VLANs added automatically
22343f01c91aSVladimir Oltean 		 * by DSA on the CPU port.
22353f01c91aSVladimir Oltean 		 */
22363f01c91aSVladimir Oltean 		if (!dsa_is_user_port(priv->ds, v->port))
22373f01c91aSVladimir Oltean 			continue;
22383f01c91aSVladimir Oltean 
22393f01c91aSVladimir Oltean 		subvlan = sja1105_find_subvlan(subvlan_map[v->port],
22403f01c91aSVladimir Oltean 					       v->vid);
22413f01c91aSVladimir Oltean 		if (subvlan < 0) {
22423f01c91aSVladimir Oltean 			subvlan = sja1105_find_free_subvlan(subvlan_map[v->port],
22433f01c91aSVladimir Oltean 							    v->pvid);
22443f01c91aSVladimir Oltean 			if (subvlan < 0) {
22453f01c91aSVladimir Oltean 				dev_err(priv->ds->dev, "No more free subvlans\n");
22463f01c91aSVladimir Oltean 				return -ENOSPC;
22473f01c91aSVladimir Oltean 			}
22483f01c91aSVladimir Oltean 		}
22493f01c91aSVladimir Oltean 
22503f01c91aSVladimir Oltean 		rx_vid = dsa_8021q_rx_vid_subvlan(priv->ds, v->port, subvlan);
22513f01c91aSVladimir Oltean 
22523f01c91aSVladimir Oltean 		/* @v->vid on @v->port needs to be retagged to @rx_vid
22533f01c91aSVladimir Oltean 		 * on @upstream. Assume @v->vid on @v->port and on
22543f01c91aSVladimir Oltean 		 * @upstream was already configured by the previous
22553f01c91aSVladimir Oltean 		 * iteration over bridge_vlans.
22563f01c91aSVladimir Oltean 		 */
22573f01c91aSVladimir Oltean 		match = rx_vid;
22583f01c91aSVladimir Oltean 		new_vlan[match].vlanid = rx_vid;
22593f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(v->port);
22603f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(upstream);
22613f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(v->port);
22623f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(upstream);
22633f01c91aSVladimir Oltean 		/* The "untagged" flag is set the same as for the
22643f01c91aSVladimir Oltean 		 * original VLAN
22653f01c91aSVladimir Oltean 		 */
22663f01c91aSVladimir Oltean 		if (!v->untagged)
22673f01c91aSVladimir Oltean 			new_vlan[match].tag_port |= BIT(v->port);
22683f01c91aSVladimir Oltean 		/* But it's always tagged towards the CPU */
22693f01c91aSVladimir Oltean 		new_vlan[match].tag_port |= BIT(upstream);
22703f01c91aSVladimir Oltean 
22713f01c91aSVladimir Oltean 		/* The Retagging Table generates packet *clones* with
22723f01c91aSVladimir Oltean 		 * the new VLAN. This is a very odd hardware quirk
22733f01c91aSVladimir Oltean 		 * which we need to suppress by dropping the original
22743f01c91aSVladimir Oltean 		 * packet.
22753f01c91aSVladimir Oltean 		 * Deny egress of the original VLAN towards the CPU
22763f01c91aSVladimir Oltean 		 * port. This will force the switch to drop it, and
22773f01c91aSVladimir Oltean 		 * we'll see only the retagged packets.
22783f01c91aSVladimir Oltean 		 */
22793f01c91aSVladimir Oltean 		match = v->vid;
22803f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc &= ~BIT(upstream);
22813f01c91aSVladimir Oltean 
22823f01c91aSVladimir Oltean 		/* And the retagging itself */
22833f01c91aSVladimir Oltean 		new_retagging[k].vlan_ing = v->vid;
22843f01c91aSVladimir Oltean 		new_retagging[k].vlan_egr = rx_vid;
22853f01c91aSVladimir Oltean 		new_retagging[k].ing_port = BIT(v->port);
22863f01c91aSVladimir Oltean 		new_retagging[k].egr_port = BIT(upstream);
22873f01c91aSVladimir Oltean 		if (k++ == SJA1105_MAX_RETAGGING_COUNT) {
22883f01c91aSVladimir Oltean 			dev_err(priv->ds->dev, "No more retagging rules\n");
22893f01c91aSVladimir Oltean 			return -ENOSPC;
22903f01c91aSVladimir Oltean 		}
22913f01c91aSVladimir Oltean 
22923f01c91aSVladimir Oltean 		subvlan_map[v->port][subvlan] = v->vid;
22933f01c91aSVladimir Oltean 	}
22943f01c91aSVladimir Oltean 
22953f01c91aSVladimir Oltean 	*num_retagging = k;
22963f01c91aSVladimir Oltean 
22973f01c91aSVladimir Oltean 	return 0;
22983f01c91aSVladimir Oltean }
22993f01c91aSVladimir Oltean 
23003f01c91aSVladimir Oltean /* Sadly, in crosschip scenarios where the CPU port is also the link to another
23013f01c91aSVladimir Oltean  * switch, we should retag backwards (the dsa_8021q vid to the original vid) on
23023f01c91aSVladimir Oltean  * the CPU port of neighbour switches.
23033f01c91aSVladimir Oltean  */
23043f01c91aSVladimir Oltean static int
23053f01c91aSVladimir Oltean sja1105_build_crosschip_subvlans(struct sja1105_private *priv,
23063f01c91aSVladimir Oltean 				 struct sja1105_vlan_lookup_entry *new_vlan,
23073f01c91aSVladimir Oltean 				 struct sja1105_retagging_entry *new_retagging,
23083f01c91aSVladimir Oltean 				 int *num_retagging)
23093f01c91aSVladimir Oltean {
23103f01c91aSVladimir Oltean 	struct sja1105_crosschip_vlan *tmp, *pos;
23113f01c91aSVladimir Oltean 	struct dsa_8021q_crosschip_link *c;
23123f01c91aSVladimir Oltean 	struct sja1105_bridge_vlan *v, *w;
23133f01c91aSVladimir Oltean 	struct list_head crosschip_vlans;
23143f01c91aSVladimir Oltean 	int k = *num_retagging;
23153f01c91aSVladimir Oltean 	int rc = 0;
23163f01c91aSVladimir Oltean 
23173f01c91aSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
23183f01c91aSVladimir Oltean 		return 0;
23193f01c91aSVladimir Oltean 
23203f01c91aSVladimir Oltean 	INIT_LIST_HEAD(&crosschip_vlans);
23213f01c91aSVladimir Oltean 
2322*5899ee36SVladimir Oltean 	list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
2323*5899ee36SVladimir Oltean 		struct sja1105_private *other_priv = c->other_ctx->ds->priv;
23243f01c91aSVladimir Oltean 
23253f01c91aSVladimir Oltean 		if (other_priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
23263f01c91aSVladimir Oltean 			continue;
23273f01c91aSVladimir Oltean 
23283f01c91aSVladimir Oltean 		/* Crosschip links are also added to the CPU ports.
23293f01c91aSVladimir Oltean 		 * Ignore those.
23303f01c91aSVladimir Oltean 		 */
23313f01c91aSVladimir Oltean 		if (!dsa_is_user_port(priv->ds, c->port))
23323f01c91aSVladimir Oltean 			continue;
2333*5899ee36SVladimir Oltean 		if (!dsa_is_user_port(c->other_ctx->ds, c->other_port))
23343f01c91aSVladimir Oltean 			continue;
23353f01c91aSVladimir Oltean 
23363f01c91aSVladimir Oltean 		/* Search for VLANs on the remote port */
23373f01c91aSVladimir Oltean 		list_for_each_entry(v, &other_priv->bridge_vlans, list) {
23383f01c91aSVladimir Oltean 			bool already_added = false;
23393f01c91aSVladimir Oltean 			bool we_have_it = false;
23403f01c91aSVladimir Oltean 
23413f01c91aSVladimir Oltean 			if (v->port != c->other_port)
23423f01c91aSVladimir Oltean 				continue;
23433f01c91aSVladimir Oltean 
23443f01c91aSVladimir Oltean 			/* If @v is a pvid on @other_ds, it does not need
23453f01c91aSVladimir Oltean 			 * re-retagging, because its SVL field is 0 and we
23463f01c91aSVladimir Oltean 			 * already allow that, via the dsa_8021q crosschip
23473f01c91aSVladimir Oltean 			 * links.
23483f01c91aSVladimir Oltean 			 */
23493f01c91aSVladimir Oltean 			if (v->pvid)
23503f01c91aSVladimir Oltean 				continue;
23513f01c91aSVladimir Oltean 
23523f01c91aSVladimir Oltean 			/* Search for the VLAN on our local port */
23533f01c91aSVladimir Oltean 			list_for_each_entry(w, &priv->bridge_vlans, list) {
23543f01c91aSVladimir Oltean 				if (w->port == c->port && w->vid == v->vid) {
23553f01c91aSVladimir Oltean 					we_have_it = true;
23563f01c91aSVladimir Oltean 					break;
23573f01c91aSVladimir Oltean 				}
23583f01c91aSVladimir Oltean 			}
23593f01c91aSVladimir Oltean 
23603f01c91aSVladimir Oltean 			if (!we_have_it)
23613f01c91aSVladimir Oltean 				continue;
23623f01c91aSVladimir Oltean 
23633f01c91aSVladimir Oltean 			list_for_each_entry(tmp, &crosschip_vlans, list) {
23643f01c91aSVladimir Oltean 				if (tmp->vid == v->vid &&
23653f01c91aSVladimir Oltean 				    tmp->untagged == v->untagged &&
23663f01c91aSVladimir Oltean 				    tmp->port == c->port &&
23673f01c91aSVladimir Oltean 				    tmp->other_port == v->port &&
2368*5899ee36SVladimir Oltean 				    tmp->other_ctx == c->other_ctx) {
23693f01c91aSVladimir Oltean 					already_added = true;
23703f01c91aSVladimir Oltean 					break;
23713f01c91aSVladimir Oltean 				}
23723f01c91aSVladimir Oltean 			}
23733f01c91aSVladimir Oltean 
23743f01c91aSVladimir Oltean 			if (already_added)
23753f01c91aSVladimir Oltean 				continue;
23763f01c91aSVladimir Oltean 
23773f01c91aSVladimir Oltean 			tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
23783f01c91aSVladimir Oltean 			if (!tmp) {
23793f01c91aSVladimir Oltean 				dev_err(priv->ds->dev, "Failed to allocate memory\n");
23803f01c91aSVladimir Oltean 				rc = -ENOMEM;
23813f01c91aSVladimir Oltean 				goto out;
23823f01c91aSVladimir Oltean 			}
23833f01c91aSVladimir Oltean 			tmp->vid = v->vid;
23843f01c91aSVladimir Oltean 			tmp->port = c->port;
23853f01c91aSVladimir Oltean 			tmp->other_port = v->port;
2386*5899ee36SVladimir Oltean 			tmp->other_ctx = c->other_ctx;
23873f01c91aSVladimir Oltean 			tmp->untagged = v->untagged;
23883f01c91aSVladimir Oltean 			list_add(&tmp->list, &crosschip_vlans);
23893f01c91aSVladimir Oltean 		}
23903f01c91aSVladimir Oltean 	}
23913f01c91aSVladimir Oltean 
23923f01c91aSVladimir Oltean 	list_for_each_entry(tmp, &crosschip_vlans, list) {
2393*5899ee36SVladimir Oltean 		struct sja1105_private *other_priv = tmp->other_ctx->ds->priv;
23943f01c91aSVladimir Oltean 		int upstream = dsa_upstream_port(priv->ds, tmp->port);
23953f01c91aSVladimir Oltean 		int match, subvlan;
23963f01c91aSVladimir Oltean 		u16 rx_vid;
23973f01c91aSVladimir Oltean 
23983f01c91aSVladimir Oltean 		subvlan = sja1105_find_committed_subvlan(other_priv,
23993f01c91aSVladimir Oltean 							 tmp->other_port,
24003f01c91aSVladimir Oltean 							 tmp->vid);
24013f01c91aSVladimir Oltean 		/* If this happens, it's a bug. The neighbour switch does not
24023f01c91aSVladimir Oltean 		 * have a subvlan for tmp->vid on tmp->other_port, but it
24033f01c91aSVladimir Oltean 		 * should, since we already checked for its vlan_state.
24043f01c91aSVladimir Oltean 		 */
24053f01c91aSVladimir Oltean 		if (WARN_ON(subvlan < 0)) {
24063f01c91aSVladimir Oltean 			rc = -EINVAL;
24073f01c91aSVladimir Oltean 			goto out;
24083f01c91aSVladimir Oltean 		}
24093f01c91aSVladimir Oltean 
2410*5899ee36SVladimir Oltean 		rx_vid = dsa_8021q_rx_vid_subvlan(tmp->other_ctx->ds,
24113f01c91aSVladimir Oltean 						  tmp->other_port,
24123f01c91aSVladimir Oltean 						  subvlan);
24133f01c91aSVladimir Oltean 
24143f01c91aSVladimir Oltean 		/* The @rx_vid retagged from @tmp->vid on
24153f01c91aSVladimir Oltean 		 * {@tmp->other_ds, @tmp->other_port} needs to be
24163f01c91aSVladimir Oltean 		 * re-retagged to @tmp->vid on the way back to us.
24173f01c91aSVladimir Oltean 		 *
24183f01c91aSVladimir Oltean 		 * Assume the original @tmp->vid is already configured
24193f01c91aSVladimir Oltean 		 * on this local switch, otherwise we wouldn't be
24203f01c91aSVladimir Oltean 		 * retagging its subvlan on the other switch in the
24213f01c91aSVladimir Oltean 		 * first place. We just need to add a reverse retagging
24223f01c91aSVladimir Oltean 		 * rule for @rx_vid and install @rx_vid on our ports.
24233f01c91aSVladimir Oltean 		 */
24243f01c91aSVladimir Oltean 		match = rx_vid;
24253f01c91aSVladimir Oltean 		new_vlan[match].vlanid = rx_vid;
24263f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(tmp->port);
24273f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(upstream);
24283f01c91aSVladimir Oltean 		/* The "untagged" flag is set the same as for the
24293f01c91aSVladimir Oltean 		 * original VLAN. And towards the CPU, it doesn't
24303f01c91aSVladimir Oltean 		 * really matter, because @rx_vid will only receive
24313f01c91aSVladimir Oltean 		 * traffic on that port. For consistency with other dsa_8021q
24323f01c91aSVladimir Oltean 		 * VLANs, we'll keep the CPU port tagged.
24333f01c91aSVladimir Oltean 		 */
24343f01c91aSVladimir Oltean 		if (!tmp->untagged)
24353f01c91aSVladimir Oltean 			new_vlan[match].tag_port |= BIT(tmp->port);
24363f01c91aSVladimir Oltean 		new_vlan[match].tag_port |= BIT(upstream);
24373f01c91aSVladimir Oltean 		/* Deny egress of @rx_vid towards our front-panel port.
24383f01c91aSVladimir Oltean 		 * This will force the switch to drop it, and we'll see
24393f01c91aSVladimir Oltean 		 * only the re-retagged packets (having the original,
24403f01c91aSVladimir Oltean 		 * pre-initial-retagging, VLAN @tmp->vid).
24413f01c91aSVladimir Oltean 		 */
24423f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc &= ~BIT(tmp->port);
24433f01c91aSVladimir Oltean 
24443f01c91aSVladimir Oltean 		/* On reverse retagging, the same ingress VLAN goes to multiple
24453f01c91aSVladimir Oltean 		 * ports. So we have an opportunity to create composite rules
24463f01c91aSVladimir Oltean 		 * to not waste the limited space in the retagging table.
24473f01c91aSVladimir Oltean 		 */
24483f01c91aSVladimir Oltean 		k = sja1105_find_retagging_entry(new_retagging, *num_retagging,
24493f01c91aSVladimir Oltean 						 upstream, rx_vid, tmp->vid);
24503f01c91aSVladimir Oltean 		if (k < 0) {
24513f01c91aSVladimir Oltean 			if (*num_retagging == SJA1105_MAX_RETAGGING_COUNT) {
24523f01c91aSVladimir Oltean 				dev_err(priv->ds->dev, "No more retagging rules\n");
24533f01c91aSVladimir Oltean 				rc = -ENOSPC;
24543f01c91aSVladimir Oltean 				goto out;
24553f01c91aSVladimir Oltean 			}
24563f01c91aSVladimir Oltean 			k = (*num_retagging)++;
24573f01c91aSVladimir Oltean 		}
24583f01c91aSVladimir Oltean 		/* And the retagging itself */
24593f01c91aSVladimir Oltean 		new_retagging[k].vlan_ing = rx_vid;
24603f01c91aSVladimir Oltean 		new_retagging[k].vlan_egr = tmp->vid;
24613f01c91aSVladimir Oltean 		new_retagging[k].ing_port = BIT(upstream);
24623f01c91aSVladimir Oltean 		new_retagging[k].egr_port |= BIT(tmp->port);
24633f01c91aSVladimir Oltean 	}
24643f01c91aSVladimir Oltean 
24653f01c91aSVladimir Oltean out:
24663f01c91aSVladimir Oltean 	list_for_each_entry_safe(tmp, pos, &crosschip_vlans, list) {
24673f01c91aSVladimir Oltean 		list_del(&tmp->list);
24683f01c91aSVladimir Oltean 		kfree(tmp);
24693f01c91aSVladimir Oltean 	}
24703f01c91aSVladimir Oltean 
24713f01c91aSVladimir Oltean 	return rc;
24723f01c91aSVladimir Oltean }
24733f01c91aSVladimir Oltean 
2474ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify);
2475ec5ae610SVladimir Oltean 
2476ec5ae610SVladimir Oltean static int sja1105_notify_crosschip_switches(struct sja1105_private *priv)
2477ec5ae610SVladimir Oltean {
2478ec5ae610SVladimir Oltean 	struct sja1105_crosschip_switch *s, *pos;
2479ec5ae610SVladimir Oltean 	struct list_head crosschip_switches;
2480ec5ae610SVladimir Oltean 	struct dsa_8021q_crosschip_link *c;
2481ec5ae610SVladimir Oltean 	int rc = 0;
2482ec5ae610SVladimir Oltean 
2483ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&crosschip_switches);
2484ec5ae610SVladimir Oltean 
2485*5899ee36SVladimir Oltean 	list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
2486ec5ae610SVladimir Oltean 		bool already_added = false;
2487ec5ae610SVladimir Oltean 
2488ec5ae610SVladimir Oltean 		list_for_each_entry(s, &crosschip_switches, list) {
2489*5899ee36SVladimir Oltean 			if (s->other_ctx == c->other_ctx) {
2490ec5ae610SVladimir Oltean 				already_added = true;
2491ec5ae610SVladimir Oltean 				break;
2492ec5ae610SVladimir Oltean 			}
2493ec5ae610SVladimir Oltean 		}
2494ec5ae610SVladimir Oltean 
2495ec5ae610SVladimir Oltean 		if (already_added)
2496ec5ae610SVladimir Oltean 			continue;
2497ec5ae610SVladimir Oltean 
2498ec5ae610SVladimir Oltean 		s = kzalloc(sizeof(*s), GFP_KERNEL);
2499ec5ae610SVladimir Oltean 		if (!s) {
2500ec5ae610SVladimir Oltean 			dev_err(priv->ds->dev, "Failed to allocate memory\n");
2501ec5ae610SVladimir Oltean 			rc = -ENOMEM;
2502ec5ae610SVladimir Oltean 			goto out;
2503ec5ae610SVladimir Oltean 		}
2504*5899ee36SVladimir Oltean 		s->other_ctx = c->other_ctx;
2505ec5ae610SVladimir Oltean 		list_add(&s->list, &crosschip_switches);
2506ec5ae610SVladimir Oltean 	}
2507ec5ae610SVladimir Oltean 
2508ec5ae610SVladimir Oltean 	list_for_each_entry(s, &crosschip_switches, list) {
2509*5899ee36SVladimir Oltean 		struct sja1105_private *other_priv = s->other_ctx->ds->priv;
2510ec5ae610SVladimir Oltean 
2511ec5ae610SVladimir Oltean 		rc = sja1105_build_vlan_table(other_priv, false);
2512ec5ae610SVladimir Oltean 		if (rc)
2513ec5ae610SVladimir Oltean 			goto out;
2514ec5ae610SVladimir Oltean 	}
2515ec5ae610SVladimir Oltean 
2516ec5ae610SVladimir Oltean out:
2517ec5ae610SVladimir Oltean 	list_for_each_entry_safe(s, pos, &crosschip_switches, list) {
2518ec5ae610SVladimir Oltean 		list_del(&s->list);
2519ec5ae610SVladimir Oltean 		kfree(s);
2520ec5ae610SVladimir Oltean 	}
2521ec5ae610SVladimir Oltean 
2522ec5ae610SVladimir Oltean 	return rc;
2523ec5ae610SVladimir Oltean }
2524ec5ae610SVladimir Oltean 
2525ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify)
2526ec5ae610SVladimir Oltean {
25273f01c91aSVladimir Oltean 	u16 subvlan_map[SJA1105_NUM_PORTS][DSA_8021Q_N_SUBVLAN];
25283f01c91aSVladimir Oltean 	struct sja1105_retagging_entry *new_retagging;
2529ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *new_vlan;
2530ec5ae610SVladimir Oltean 	struct sja1105_table *table;
25313f01c91aSVladimir Oltean 	int i, num_retagging = 0;
2532ec5ae610SVladimir Oltean 	int rc;
2533ec5ae610SVladimir Oltean 
2534ec5ae610SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2535ec5ae610SVladimir Oltean 	new_vlan = kcalloc(VLAN_N_VID,
2536ec5ae610SVladimir Oltean 			   table->ops->unpacked_entry_size, GFP_KERNEL);
2537ec5ae610SVladimir Oltean 	if (!new_vlan)
2538ec5ae610SVladimir Oltean 		return -ENOMEM;
2539ec5ae610SVladimir Oltean 
25403f01c91aSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
25413f01c91aSVladimir Oltean 	new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT,
25423f01c91aSVladimir Oltean 				table->ops->unpacked_entry_size, GFP_KERNEL);
25433f01c91aSVladimir Oltean 	if (!new_retagging) {
25443f01c91aSVladimir Oltean 		kfree(new_vlan);
25453f01c91aSVladimir Oltean 		return -ENOMEM;
25463f01c91aSVladimir Oltean 	}
25473f01c91aSVladimir Oltean 
2548ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++)
2549ec5ae610SVladimir Oltean 		new_vlan[i].vlanid = VLAN_N_VID;
2550ec5ae610SVladimir Oltean 
25513f01c91aSVladimir Oltean 	for (i = 0; i < SJA1105_MAX_RETAGGING_COUNT; i++)
25523f01c91aSVladimir Oltean 		new_retagging[i].vlan_ing = VLAN_N_VID;
25533f01c91aSVladimir Oltean 
25543f01c91aSVladimir Oltean 	for (i = 0; i < priv->ds->num_ports; i++)
25553f01c91aSVladimir Oltean 		sja1105_init_subvlan_map(subvlan_map[i]);
25563f01c91aSVladimir Oltean 
2557ec5ae610SVladimir Oltean 	/* Bridge VLANs */
2558ec5ae610SVladimir Oltean 	rc = sja1105_build_bridge_vlans(priv, new_vlan);
2559ec5ae610SVladimir Oltean 	if (rc)
2560ec5ae610SVladimir Oltean 		goto out;
2561ec5ae610SVladimir Oltean 
2562ec5ae610SVladimir Oltean 	/* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c:
2563ec5ae610SVladimir Oltean 	 * - RX VLANs
2564ec5ae610SVladimir Oltean 	 * - TX VLANs
2565ec5ae610SVladimir Oltean 	 * - Crosschip links
2566ec5ae610SVladimir Oltean 	 */
2567ec5ae610SVladimir Oltean 	rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan);
2568ec5ae610SVladimir Oltean 	if (rc)
2569ec5ae610SVladimir Oltean 		goto out;
2570ec5ae610SVladimir Oltean 
25713f01c91aSVladimir Oltean 	/* Private VLANs necessary for dsa_8021q operation, which we need to
25723f01c91aSVladimir Oltean 	 * determine on our own:
25733f01c91aSVladimir Oltean 	 * - Sub-VLANs
25743f01c91aSVladimir Oltean 	 * - Sub-VLANs of crosschip switches
25753f01c91aSVladimir Oltean 	 */
25763f01c91aSVladimir Oltean 	rc = sja1105_build_subvlans(priv, subvlan_map, new_vlan, new_retagging,
25773f01c91aSVladimir Oltean 				    &num_retagging);
25783f01c91aSVladimir Oltean 	if (rc)
25793f01c91aSVladimir Oltean 		goto out;
25803f01c91aSVladimir Oltean 
25813f01c91aSVladimir Oltean 	rc = sja1105_build_crosschip_subvlans(priv, new_vlan, new_retagging,
25823f01c91aSVladimir Oltean 					      &num_retagging);
25833f01c91aSVladimir Oltean 	if (rc)
25843f01c91aSVladimir Oltean 		goto out;
25853f01c91aSVladimir Oltean 
25863f01c91aSVladimir Oltean 	rc = sja1105_commit_vlans(priv, new_vlan, new_retagging, num_retagging);
2587ec5ae610SVladimir Oltean 	if (rc)
2588ec5ae610SVladimir Oltean 		goto out;
2589ec5ae610SVladimir Oltean 
2590ec5ae610SVladimir Oltean 	rc = sja1105_commit_pvid(priv);
2591ec5ae610SVladimir Oltean 	if (rc)
2592ec5ae610SVladimir Oltean 		goto out;
2593ec5ae610SVladimir Oltean 
25943f01c91aSVladimir Oltean 	for (i = 0; i < priv->ds->num_ports; i++)
25953f01c91aSVladimir Oltean 		sja1105_commit_subvlan_map(priv, i, subvlan_map[i]);
25963f01c91aSVladimir Oltean 
2597ec5ae610SVladimir Oltean 	if (notify) {
2598ec5ae610SVladimir Oltean 		rc = sja1105_notify_crosschip_switches(priv);
2599ec5ae610SVladimir Oltean 		if (rc)
2600ec5ae610SVladimir Oltean 			goto out;
2601ec5ae610SVladimir Oltean 	}
2602ec5ae610SVladimir Oltean 
2603ec5ae610SVladimir Oltean out:
2604ec5ae610SVladimir Oltean 	kfree(new_vlan);
26053f01c91aSVladimir Oltean 	kfree(new_retagging);
2606ec5ae610SVladimir Oltean 
2607ec5ae610SVladimir Oltean 	return rc;
2608ec5ae610SVladimir Oltean }
2609ec5ae610SVladimir Oltean 
26106666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
26116666cebcSVladimir Oltean 				const struct switchdev_obj_port_vlan *vlan)
26126666cebcSVladimir Oltean {
261360b33aebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
261460b33aebSVladimir Oltean 	u16 vid;
261560b33aebSVladimir Oltean 
261660b33aebSVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
261760b33aebSVladimir Oltean 		return 0;
261860b33aebSVladimir Oltean 
261960b33aebSVladimir Oltean 	/* If the user wants best-effort VLAN filtering (aka vlan_filtering
262060b33aebSVladimir Oltean 	 * bridge plus tagging), be sure to at least deny alterations to the
262160b33aebSVladimir Oltean 	 * configuration done by dsa_8021q.
262260b33aebSVladimir Oltean 	 */
262360b33aebSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2624*5899ee36SVladimir Oltean 		if (vid_is_dsa_8021q(vid)) {
262560b33aebSVladimir Oltean 			dev_err(ds->dev, "Range 1024-3071 reserved for dsa_8021q operation\n");
262660b33aebSVladimir Oltean 			return -EBUSY;
262760b33aebSVladimir Oltean 		}
262860b33aebSVladimir Oltean 	}
262960b33aebSVladimir Oltean 
26306666cebcSVladimir Oltean 	return 0;
26316666cebcSVladimir Oltean }
26326666cebcSVladimir Oltean 
2633070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
2634070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
2635070ca3bbSVladimir Oltean  * So a switch reset is required.
2636070ca3bbSVladimir Oltean  */
26376666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
26386666cebcSVladimir Oltean {
26396d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2640070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
26416666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
26427f14937fSVladimir Oltean 	enum sja1105_vlan_state state;
2643070ca3bbSVladimir Oltean 	struct sja1105_table *table;
2644dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
26452cafa72eSVladimir Oltean 	bool want_tagging;
2646070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
26476666cebcSVladimir Oltean 	int rc;
26486666cebcSVladimir Oltean 
2649dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
2650dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
2651dfacc5a2SVladimir Oltean 			dev_err(ds->dev,
2652dfacc5a2SVladimir Oltean 				"Cannot change VLAN filtering state while VL rules are active\n");
2653dfacc5a2SVladimir Oltean 			return -EBUSY;
2654dfacc5a2SVladimir Oltean 		}
2655dfacc5a2SVladimir Oltean 	}
2656dfacc5a2SVladimir Oltean 
2657070ca3bbSVladimir Oltean 	if (enabled) {
26586666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
265954fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
266054fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
2661070ca3bbSVladimir Oltean 	} else {
26626666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
2663070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
2664070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
2665070ca3bbSVladimir Oltean 	}
2666070ca3bbSVladimir Oltean 
266738b5beeaSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
266838b5beeaSVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
266938b5beeaSVladimir Oltean 
267038b5beeaSVladimir Oltean 		if (enabled)
267138b5beeaSVladimir Oltean 			sp->xmit_tpid = priv->info->qinq_tpid;
267238b5beeaSVladimir Oltean 		else
267338b5beeaSVladimir Oltean 			sp->xmit_tpid = ETH_P_SJA1105;
267438b5beeaSVladimir Oltean 	}
267538b5beeaSVladimir Oltean 
26767f14937fSVladimir Oltean 	if (!enabled)
26777f14937fSVladimir Oltean 		state = SJA1105_VLAN_UNAWARE;
26782cafa72eSVladimir Oltean 	else if (priv->best_effort_vlan_filtering)
26792cafa72eSVladimir Oltean 		state = SJA1105_VLAN_BEST_EFFORT;
26807f14937fSVladimir Oltean 	else
26817f14937fSVladimir Oltean 		state = SJA1105_VLAN_FILTERING_FULL;
26827f14937fSVladimir Oltean 
2683cfa36b1fSVladimir Oltean 	if (priv->vlan_state == state)
2684cfa36b1fSVladimir Oltean 		return 0;
2685cfa36b1fSVladimir Oltean 
26867f14937fSVladimir Oltean 	priv->vlan_state = state;
26872cafa72eSVladimir Oltean 	want_tagging = (state == SJA1105_VLAN_UNAWARE ||
26882cafa72eSVladimir Oltean 			state == SJA1105_VLAN_BEST_EFFORT);
26897f14937fSVladimir Oltean 
2690070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2691070ca3bbSVladimir Oltean 	general_params = table->entries;
2692f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
269354fa49eeSVladimir Oltean 	general_params->tpid = tpid;
269454fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2695070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
269642824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
269742824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
269842824463SVladimir Oltean 	 */
269942824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
270042824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
2701070ca3bbSVladimir Oltean 
27022cafa72eSVladimir Oltean 	want_tagging = priv->best_effort_vlan_filtering || !enabled;
27032cafa72eSVladimir Oltean 
27046d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
27052cafa72eSVladimir Oltean 	 * No VLAN filtering (or best effort) => shared VLAN learning.
27066d7c7d94SVladimir Oltean 	 *
27076d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
27086d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
27096d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
27106d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
27116d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
27126d7c7d94SVladimir Oltean 	 * forwarding decision.
27136d7c7d94SVladimir Oltean 	 *
27146d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
27156d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
27166d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
27176d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
27186d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
27196d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
27206d7c7d94SVladimir Oltean 	 * (all frames get flooded).
27216d7c7d94SVladimir Oltean 	 */
27226d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
27236d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
27242cafa72eSVladimir Oltean 	l2_lookup_params->shared_learn = want_tagging;
27256d7c7d94SVladimir Oltean 
2726aaa270c6SVladimir Oltean 	sja1105_frame_memory_partitioning(priv);
2727aaa270c6SVladimir Oltean 
2728aef31718SVladimir Oltean 	rc = sja1105_build_vlan_table(priv, false);
2729aef31718SVladimir Oltean 	if (rc)
2730aef31718SVladimir Oltean 		return rc;
2731aef31718SVladimir Oltean 
27322eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
27336666cebcSVladimir Oltean 	if (rc)
27346666cebcSVladimir Oltean 		dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
27356666cebcSVladimir Oltean 
2736227d07a0SVladimir Oltean 	/* Switch port identification based on 802.1Q is only passable
2737227d07a0SVladimir Oltean 	 * if we are not under a vlan_filtering bridge. So make sure
27382cafa72eSVladimir Oltean 	 * the two configurations are mutually exclusive (of course, the
27392cafa72eSVladimir Oltean 	 * user may know better, i.e. best_effort_vlan_filtering).
2740227d07a0SVladimir Oltean 	 */
27412cafa72eSVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, want_tagging);
27426666cebcSVladimir Oltean }
27436666cebcSVladimir Oltean 
2744*5899ee36SVladimir Oltean /* Returns number of VLANs added (0 or 1) on success,
2745*5899ee36SVladimir Oltean  * or a negative error code.
2746*5899ee36SVladimir Oltean  */
2747*5899ee36SVladimir Oltean static int sja1105_vlan_add_one(struct dsa_switch *ds, int port, u16 vid,
2748*5899ee36SVladimir Oltean 				u16 flags, struct list_head *vlan_list)
2749*5899ee36SVladimir Oltean {
2750*5899ee36SVladimir Oltean 	bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED;
2751*5899ee36SVladimir Oltean 	bool pvid = flags & BRIDGE_VLAN_INFO_PVID;
2752*5899ee36SVladimir Oltean 	struct sja1105_bridge_vlan *v;
2753*5899ee36SVladimir Oltean 
2754*5899ee36SVladimir Oltean 	list_for_each_entry(v, vlan_list, list)
2755*5899ee36SVladimir Oltean 		if (v->port == port && v->vid == vid &&
2756*5899ee36SVladimir Oltean 		    v->untagged == untagged && v->pvid == pvid)
2757*5899ee36SVladimir Oltean 			/* Already added */
2758*5899ee36SVladimir Oltean 			return 0;
2759*5899ee36SVladimir Oltean 
2760*5899ee36SVladimir Oltean 	v = kzalloc(sizeof(*v), GFP_KERNEL);
2761*5899ee36SVladimir Oltean 	if (!v) {
2762*5899ee36SVladimir Oltean 		dev_err(ds->dev, "Out of memory while storing VLAN\n");
2763*5899ee36SVladimir Oltean 		return -ENOMEM;
2764*5899ee36SVladimir Oltean 	}
2765*5899ee36SVladimir Oltean 
2766*5899ee36SVladimir Oltean 	v->port = port;
2767*5899ee36SVladimir Oltean 	v->vid = vid;
2768*5899ee36SVladimir Oltean 	v->untagged = untagged;
2769*5899ee36SVladimir Oltean 	v->pvid = pvid;
2770*5899ee36SVladimir Oltean 	list_add(&v->list, vlan_list);
2771*5899ee36SVladimir Oltean 
2772*5899ee36SVladimir Oltean 	return 1;
2773*5899ee36SVladimir Oltean }
2774*5899ee36SVladimir Oltean 
2775*5899ee36SVladimir Oltean /* Returns number of VLANs deleted (0 or 1) */
2776*5899ee36SVladimir Oltean static int sja1105_vlan_del_one(struct dsa_switch *ds, int port, u16 vid,
2777*5899ee36SVladimir Oltean 				struct list_head *vlan_list)
2778*5899ee36SVladimir Oltean {
2779*5899ee36SVladimir Oltean 	struct sja1105_bridge_vlan *v, *n;
2780*5899ee36SVladimir Oltean 
2781*5899ee36SVladimir Oltean 	list_for_each_entry_safe(v, n, vlan_list, list) {
2782*5899ee36SVladimir Oltean 		if (v->port == port && v->vid == vid) {
2783*5899ee36SVladimir Oltean 			list_del(&v->list);
2784*5899ee36SVladimir Oltean 			kfree(v);
2785*5899ee36SVladimir Oltean 			return 1;
2786*5899ee36SVladimir Oltean 		}
2787*5899ee36SVladimir Oltean 	}
2788*5899ee36SVladimir Oltean 
2789*5899ee36SVladimir Oltean 	return 0;
2790*5899ee36SVladimir Oltean }
2791*5899ee36SVladimir Oltean 
27926666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port,
27936666cebcSVladimir Oltean 			     const struct switchdev_obj_port_vlan *vlan)
27946666cebcSVladimir Oltean {
27956666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2796ec5ae610SVladimir Oltean 	bool vlan_table_changed = false;
27976666cebcSVladimir Oltean 	u16 vid;
27986666cebcSVladimir Oltean 	int rc;
27996666cebcSVladimir Oltean 
28006666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2801*5899ee36SVladimir Oltean 		rc = sja1105_vlan_add_one(ds, port, vid, vlan->flags,
2802*5899ee36SVladimir Oltean 					  &priv->bridge_vlans);
2803*5899ee36SVladimir Oltean 		if (rc < 0)
28046666cebcSVladimir Oltean 			return;
2805*5899ee36SVladimir Oltean 		if (rc > 0)
2806ec5ae610SVladimir Oltean 			vlan_table_changed = true;
2807ec5ae610SVladimir Oltean 	}
2808ec5ae610SVladimir Oltean 
2809ec5ae610SVladimir Oltean 	if (!vlan_table_changed)
28106666cebcSVladimir Oltean 		return;
2811ec5ae610SVladimir Oltean 
2812ec5ae610SVladimir Oltean 	rc = sja1105_build_vlan_table(priv, true);
2813ec5ae610SVladimir Oltean 	if (rc)
2814ec5ae610SVladimir Oltean 		dev_err(ds->dev, "Failed to build VLAN table: %d\n", rc);
28156666cebcSVladimir Oltean }
28166666cebcSVladimir Oltean 
28176666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
28186666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
28196666cebcSVladimir Oltean {
28206666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2821ec5ae610SVladimir Oltean 	bool vlan_table_changed = false;
28226666cebcSVladimir Oltean 	u16 vid;
2823*5899ee36SVladimir Oltean 	int rc;
28246666cebcSVladimir Oltean 
28256666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2826*5899ee36SVladimir Oltean 		rc = sja1105_vlan_del_one(ds, port, vid, &priv->bridge_vlans);
2827*5899ee36SVladimir Oltean 		if (rc > 0)
2828ec5ae610SVladimir Oltean 			vlan_table_changed = true;
2829ec5ae610SVladimir Oltean 	}
2830ec5ae610SVladimir Oltean 
2831ec5ae610SVladimir Oltean 	if (!vlan_table_changed)
28326666cebcSVladimir Oltean 		return 0;
2833ec5ae610SVladimir Oltean 
2834ec5ae610SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
28356666cebcSVladimir Oltean }
28366666cebcSVladimir Oltean 
2837*5899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
2838*5899ee36SVladimir Oltean 				      u16 flags)
2839*5899ee36SVladimir Oltean {
2840*5899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2841*5899ee36SVladimir Oltean 	int rc;
2842*5899ee36SVladimir Oltean 
2843*5899ee36SVladimir Oltean 	rc = sja1105_vlan_add_one(ds, port, vid, flags, &priv->dsa_8021q_vlans);
2844*5899ee36SVladimir Oltean 	if (rc <= 0)
2845*5899ee36SVladimir Oltean 		return rc;
2846*5899ee36SVladimir Oltean 
2847*5899ee36SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
2848*5899ee36SVladimir Oltean }
2849*5899ee36SVladimir Oltean 
2850*5899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
2851*5899ee36SVladimir Oltean {
2852*5899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2853*5899ee36SVladimir Oltean 	int rc;
2854*5899ee36SVladimir Oltean 
2855*5899ee36SVladimir Oltean 	rc = sja1105_vlan_del_one(ds, port, vid, &priv->dsa_8021q_vlans);
2856*5899ee36SVladimir Oltean 	if (!rc)
2857*5899ee36SVladimir Oltean 		return 0;
2858*5899ee36SVladimir Oltean 
2859*5899ee36SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
2860*5899ee36SVladimir Oltean }
2861*5899ee36SVladimir Oltean 
2862*5899ee36SVladimir Oltean static const struct dsa_8021q_ops sja1105_dsa_8021q_ops = {
2863*5899ee36SVladimir Oltean 	.vlan_add	= sja1105_dsa_8021q_vlan_add,
2864*5899ee36SVladimir Oltean 	.vlan_del	= sja1105_dsa_8021q_vlan_del,
2865*5899ee36SVladimir Oltean };
2866*5899ee36SVladimir Oltean 
28672cafa72eSVladimir Oltean static int sja1105_best_effort_vlan_filtering_get(struct sja1105_private *priv,
28682cafa72eSVladimir Oltean 						  bool *be_vlan)
28692cafa72eSVladimir Oltean {
28702cafa72eSVladimir Oltean 	*be_vlan = priv->best_effort_vlan_filtering;
28712cafa72eSVladimir Oltean 
28722cafa72eSVladimir Oltean 	return 0;
28732cafa72eSVladimir Oltean }
28742cafa72eSVladimir Oltean 
28752cafa72eSVladimir Oltean static int sja1105_best_effort_vlan_filtering_set(struct sja1105_private *priv,
28762cafa72eSVladimir Oltean 						  bool be_vlan)
28772cafa72eSVladimir Oltean {
28782cafa72eSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
28792cafa72eSVladimir Oltean 	bool vlan_filtering;
28802cafa72eSVladimir Oltean 	int port;
28812cafa72eSVladimir Oltean 	int rc;
28822cafa72eSVladimir Oltean 
28832cafa72eSVladimir Oltean 	priv->best_effort_vlan_filtering = be_vlan;
28842cafa72eSVladimir Oltean 
28852cafa72eSVladimir Oltean 	rtnl_lock();
28862cafa72eSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
28872cafa72eSVladimir Oltean 		struct dsa_port *dp;
28882cafa72eSVladimir Oltean 
28892cafa72eSVladimir Oltean 		if (!dsa_is_user_port(ds, port))
28902cafa72eSVladimir Oltean 			continue;
28912cafa72eSVladimir Oltean 
28922cafa72eSVladimir Oltean 		dp = dsa_to_port(ds, port);
28932cafa72eSVladimir Oltean 		vlan_filtering = dsa_port_is_vlan_filtering(dp);
28942cafa72eSVladimir Oltean 
28952cafa72eSVladimir Oltean 		rc = sja1105_vlan_filtering(ds, port, vlan_filtering);
28962cafa72eSVladimir Oltean 		if (rc)
28972cafa72eSVladimir Oltean 			break;
28982cafa72eSVladimir Oltean 	}
28992cafa72eSVladimir Oltean 	rtnl_unlock();
29002cafa72eSVladimir Oltean 
29012cafa72eSVladimir Oltean 	return rc;
29022cafa72eSVladimir Oltean }
29032cafa72eSVladimir Oltean 
29042cafa72eSVladimir Oltean enum sja1105_devlink_param_id {
29052cafa72eSVladimir Oltean 	SJA1105_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
29062cafa72eSVladimir Oltean 	SJA1105_DEVLINK_PARAM_ID_BEST_EFFORT_VLAN_FILTERING,
29072cafa72eSVladimir Oltean };
29082cafa72eSVladimir Oltean 
29092cafa72eSVladimir Oltean static int sja1105_devlink_param_get(struct dsa_switch *ds, u32 id,
29102cafa72eSVladimir Oltean 				     struct devlink_param_gset_ctx *ctx)
29112cafa72eSVladimir Oltean {
29122cafa72eSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29132cafa72eSVladimir Oltean 	int err;
29142cafa72eSVladimir Oltean 
29152cafa72eSVladimir Oltean 	switch (id) {
29162cafa72eSVladimir Oltean 	case SJA1105_DEVLINK_PARAM_ID_BEST_EFFORT_VLAN_FILTERING:
29172cafa72eSVladimir Oltean 		err = sja1105_best_effort_vlan_filtering_get(priv,
29182cafa72eSVladimir Oltean 							     &ctx->val.vbool);
29192cafa72eSVladimir Oltean 		break;
29202cafa72eSVladimir Oltean 	default:
29212cafa72eSVladimir Oltean 		err = -EOPNOTSUPP;
29222cafa72eSVladimir Oltean 		break;
29232cafa72eSVladimir Oltean 	}
29242cafa72eSVladimir Oltean 
29252cafa72eSVladimir Oltean 	return err;
29262cafa72eSVladimir Oltean }
29272cafa72eSVladimir Oltean 
29282cafa72eSVladimir Oltean static int sja1105_devlink_param_set(struct dsa_switch *ds, u32 id,
29292cafa72eSVladimir Oltean 				     struct devlink_param_gset_ctx *ctx)
29302cafa72eSVladimir Oltean {
29312cafa72eSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29322cafa72eSVladimir Oltean 	int err;
29332cafa72eSVladimir Oltean 
29342cafa72eSVladimir Oltean 	switch (id) {
29352cafa72eSVladimir Oltean 	case SJA1105_DEVLINK_PARAM_ID_BEST_EFFORT_VLAN_FILTERING:
29362cafa72eSVladimir Oltean 		err = sja1105_best_effort_vlan_filtering_set(priv,
29372cafa72eSVladimir Oltean 							     ctx->val.vbool);
29382cafa72eSVladimir Oltean 		break;
29392cafa72eSVladimir Oltean 	default:
29402cafa72eSVladimir Oltean 		err = -EOPNOTSUPP;
29412cafa72eSVladimir Oltean 		break;
29422cafa72eSVladimir Oltean 	}
29432cafa72eSVladimir Oltean 
29442cafa72eSVladimir Oltean 	return err;
29452cafa72eSVladimir Oltean }
29462cafa72eSVladimir Oltean 
29472cafa72eSVladimir Oltean static const struct devlink_param sja1105_devlink_params[] = {
29482cafa72eSVladimir Oltean 	DSA_DEVLINK_PARAM_DRIVER(SJA1105_DEVLINK_PARAM_ID_BEST_EFFORT_VLAN_FILTERING,
29492cafa72eSVladimir Oltean 				 "best_effort_vlan_filtering",
29502cafa72eSVladimir Oltean 				 DEVLINK_PARAM_TYPE_BOOL,
29512cafa72eSVladimir Oltean 				 BIT(DEVLINK_PARAM_CMODE_RUNTIME)),
29522cafa72eSVladimir Oltean };
29532cafa72eSVladimir Oltean 
29542cafa72eSVladimir Oltean static int sja1105_setup_devlink_params(struct dsa_switch *ds)
29552cafa72eSVladimir Oltean {
29562cafa72eSVladimir Oltean 	return dsa_devlink_params_register(ds, sja1105_devlink_params,
29572cafa72eSVladimir Oltean 					   ARRAY_SIZE(sja1105_devlink_params));
29582cafa72eSVladimir Oltean }
29592cafa72eSVladimir Oltean 
29602cafa72eSVladimir Oltean static void sja1105_teardown_devlink_params(struct dsa_switch *ds)
29612cafa72eSVladimir Oltean {
29622cafa72eSVladimir Oltean 	dsa_devlink_params_unregister(ds, sja1105_devlink_params,
29632cafa72eSVladimir Oltean 				      ARRAY_SIZE(sja1105_devlink_params));
29642cafa72eSVladimir Oltean }
29652cafa72eSVladimir Oltean 
29668aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
29678aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
29688aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
29698aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
29708aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
29718aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
29728aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
29738aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
29748aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
29758aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
29768aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
29778aa9ebccSVladimir Oltean  */
29788aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
29798aa9ebccSVladimir Oltean {
29808aa9ebccSVladimir Oltean 	struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
29818aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29828aa9ebccSVladimir Oltean 	int rc;
29838aa9ebccSVladimir Oltean 
29848aa9ebccSVladimir Oltean 	rc = sja1105_parse_dt(priv, ports);
29858aa9ebccSVladimir Oltean 	if (rc < 0) {
29868aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
29878aa9ebccSVladimir Oltean 		return rc;
29888aa9ebccSVladimir Oltean 	}
2989f5b8631cSVladimir Oltean 
2990f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
2991f5b8631cSVladimir Oltean 	 * and we can't apply them.
2992f5b8631cSVladimir Oltean 	 */
2993f5b8631cSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv, ports);
2994f5b8631cSVladimir Oltean 	if (rc < 0) {
2995f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
2996f5b8631cSVladimir Oltean 		return rc;
2997f5b8631cSVladimir Oltean 	}
2998f5b8631cSVladimir Oltean 
299961c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
3000bb77f36aSVladimir Oltean 	if (rc < 0) {
3001bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
3002bb77f36aSVladimir Oltean 		return rc;
3003bb77f36aSVladimir Oltean 	}
30048aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
30058aa9ebccSVladimir Oltean 	rc = sja1105_static_config_load(priv, ports);
30068aa9ebccSVladimir Oltean 	if (rc < 0) {
30078aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
30088aa9ebccSVladimir Oltean 		return rc;
30098aa9ebccSVladimir Oltean 	}
30108aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
30118aa9ebccSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
30128aa9ebccSVladimir Oltean 	if (rc < 0) {
30138aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
30148aa9ebccSVladimir Oltean 		return rc;
30158aa9ebccSVladimir Oltean 	}
30166666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
30176666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
30186666cebcSVladimir Oltean 	 * EtherType is.
30196666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
30206666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
30216666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
30226666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
30236666cebcSVladimir Oltean 	 */
30246666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
30258aa9ebccSVladimir Oltean 
30265f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
30275f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
30285f06c63bSVladimir Oltean 
3029c279c726SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
3030c279c726SVladimir Oltean 
3031fa83e5d9SVladimir Oltean 	ds->configure_vlan_while_not_filtering = true;
3032fa83e5d9SVladimir Oltean 
30332cafa72eSVladimir Oltean 	rc = sja1105_setup_devlink_params(ds);
30342cafa72eSVladimir Oltean 	if (rc < 0)
30352cafa72eSVladimir Oltean 		return rc;
30362cafa72eSVladimir Oltean 
3037227d07a0SVladimir Oltean 	/* The DSA/switchdev model brings up switch ports in standalone mode by
3038227d07a0SVladimir Oltean 	 * default, and that means vlan_filtering is 0 since they're not under
3039227d07a0SVladimir Oltean 	 * a bridge, so it's safe to set up switch tagging at this time.
3040227d07a0SVladimir Oltean 	 */
3041227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, true);
3042227d07a0SVladimir Oltean }
3043227d07a0SVladimir Oltean 
3044f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
3045f3097be2SVladimir Oltean {
3046f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3047ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v, *n;
3048a68578c2SVladimir Oltean 	int port;
3049a68578c2SVladimir Oltean 
3050a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
3051a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3052a68578c2SVladimir Oltean 
3053a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3054a68578c2SVladimir Oltean 			continue;
3055a68578c2SVladimir Oltean 
305652c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
3057a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
3058a68578c2SVladimir Oltean 	}
3059f3097be2SVladimir Oltean 
30602cafa72eSVladimir Oltean 	sja1105_teardown_devlink_params(ds);
3061a6af7763SVladimir Oltean 	sja1105_flower_teardown(ds);
3062317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
306361c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
30646cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
3065ec5ae610SVladimir Oltean 
3066ec5ae610SVladimir Oltean 	list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) {
3067ec5ae610SVladimir Oltean 		list_del(&v->list);
3068ec5ae610SVladimir Oltean 		kfree(v);
3069ec5ae610SVladimir Oltean 	}
3070ec5ae610SVladimir Oltean 
3071ec5ae610SVladimir Oltean 	list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) {
3072ec5ae610SVladimir Oltean 		list_del(&v->list);
3073ec5ae610SVladimir Oltean 		kfree(v);
3074ec5ae610SVladimir Oltean 	}
3075f3097be2SVladimir Oltean }
3076f3097be2SVladimir Oltean 
3077e9bf9694SVladimir Oltean static int sja1105_port_enable(struct dsa_switch *ds, int port,
3078e9bf9694SVladimir Oltean 			       struct phy_device *phy)
3079e9bf9694SVladimir Oltean {
3080e9bf9694SVladimir Oltean 	struct net_device *slave;
3081e9bf9694SVladimir Oltean 
3082e9bf9694SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
3083e9bf9694SVladimir Oltean 		return 0;
3084e9bf9694SVladimir Oltean 
308568bb8ea8SVivien Didelot 	slave = dsa_to_port(ds, port)->slave;
3086e9bf9694SVladimir Oltean 
3087e9bf9694SVladimir Oltean 	slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3088e9bf9694SVladimir Oltean 
3089e9bf9694SVladimir Oltean 	return 0;
3090e9bf9694SVladimir Oltean }
3091e9bf9694SVladimir Oltean 
3092a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
3093a68578c2SVladimir Oltean {
3094a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3095a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
3096a68578c2SVladimir Oltean 
3097a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
3098a68578c2SVladimir Oltean 		return;
3099a68578c2SVladimir Oltean 
3100a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
3101a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
3102a68578c2SVladimir Oltean }
3103a68578c2SVladimir Oltean 
3104227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
310547ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
3106227d07a0SVladimir Oltean {
3107227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
3108227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3109227d07a0SVladimir Oltean 	struct ethhdr *hdr;
3110227d07a0SVladimir Oltean 	int timeout = 10;
3111227d07a0SVladimir Oltean 	int rc;
3112227d07a0SVladimir Oltean 
3113227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
3114227d07a0SVladimir Oltean 
3115227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
3116227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
3117227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
311847ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
311947ed985eSVladimir Oltean 	mgmt_route.takets = takets;
3120227d07a0SVladimir Oltean 
3121227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3122227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
3123227d07a0SVladimir Oltean 	if (rc < 0) {
3124227d07a0SVladimir Oltean 		kfree_skb(skb);
3125227d07a0SVladimir Oltean 		return rc;
3126227d07a0SVladimir Oltean 	}
3127227d07a0SVladimir Oltean 
3128227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
312968bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
3130227d07a0SVladimir Oltean 
3131227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
3132227d07a0SVladimir Oltean 	do {
3133227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
3134227d07a0SVladimir Oltean 						 slot, &mgmt_route);
3135227d07a0SVladimir Oltean 		if (rc < 0) {
3136227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
3137227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
3138227d07a0SVladimir Oltean 			continue;
3139227d07a0SVladimir Oltean 		}
3140227d07a0SVladimir Oltean 
3141227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
3142227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
3143227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
3144227d07a0SVladimir Oltean 		 */
3145227d07a0SVladimir Oltean 		cpu_relax();
3146227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
3147227d07a0SVladimir Oltean 
3148227d07a0SVladimir Oltean 	if (!timeout) {
3149227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
3150227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
31512a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
31522a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
3153227d07a0SVladimir Oltean 		 */
3154227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3155227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
3156227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
3157227d07a0SVladimir Oltean 	}
3158227d07a0SVladimir Oltean 
3159227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
3160227d07a0SVladimir Oltean }
3161227d07a0SVladimir Oltean 
3162a68578c2SVladimir Oltean #define work_to_port(work) \
3163a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
3164a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
3165a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
3166a68578c2SVladimir Oltean 
3167227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
3168227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
3169227d07a0SVladimir Oltean  * lock on the bus)
3170227d07a0SVladimir Oltean  */
3171a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
3172227d07a0SVladimir Oltean {
3173a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
3174a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
3175a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
3176a68578c2SVladimir Oltean 	int port = sp - priv->ports;
3177a68578c2SVladimir Oltean 	struct sk_buff *skb;
3178a68578c2SVladimir Oltean 
3179a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
3180a68578c2SVladimir Oltean 		struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
3181227d07a0SVladimir Oltean 
3182227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
3183227d07a0SVladimir Oltean 
3184a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
3185a68578c2SVladimir Oltean 
318647ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
3187a68578c2SVladimir Oltean 		if (clone)
3188a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
3189227d07a0SVladimir Oltean 
3190227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
3191a68578c2SVladimir Oltean 	}
31928aa9ebccSVladimir Oltean }
31938aa9ebccSVladimir Oltean 
31948456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
31958456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
31968456721dSVladimir Oltean  */
31978456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
31988456721dSVladimir Oltean 				   unsigned int ageing_time)
31998456721dSVladimir Oltean {
32008456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
32018456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
32028456721dSVladimir Oltean 	struct sja1105_table *table;
32038456721dSVladimir Oltean 	unsigned int maxage;
32048456721dSVladimir Oltean 
32058456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
32068456721dSVladimir Oltean 	l2_lookup_params = table->entries;
32078456721dSVladimir Oltean 
32088456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
32098456721dSVladimir Oltean 
32108456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
32118456721dSVladimir Oltean 		return 0;
32128456721dSVladimir Oltean 
32138456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
32148456721dSVladimir Oltean 
32152eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
32168456721dSVladimir Oltean }
32178456721dSVladimir Oltean 
3218c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
3219c279c726SVladimir Oltean {
3220c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
3221c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3222c279c726SVladimir Oltean 
3223c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
3224c279c726SVladimir Oltean 
3225c279c726SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
3226c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
3227c279c726SVladimir Oltean 
3228c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3229c279c726SVladimir Oltean 
3230a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
3231c279c726SVladimir Oltean 		return 0;
3232c279c726SVladimir Oltean 
3233a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
3234c279c726SVladimir Oltean 
3235c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3236c279c726SVladimir Oltean }
3237c279c726SVladimir Oltean 
3238c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
3239c279c726SVladimir Oltean {
3240c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
3241c279c726SVladimir Oltean }
3242c279c726SVladimir Oltean 
3243317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
3244317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
3245317ab5b8SVladimir Oltean 				 void *type_data)
3246317ab5b8SVladimir Oltean {
3247317ab5b8SVladimir Oltean 	switch (type) {
3248317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
3249317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
32504d752508SVladimir Oltean 	case TC_SETUP_QDISC_CBS:
32514d752508SVladimir Oltean 		return sja1105_setup_tc_cbs(ds, port, type_data);
3252317ab5b8SVladimir Oltean 	default:
3253317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
3254317ab5b8SVladimir Oltean 	}
3255317ab5b8SVladimir Oltean }
3256317ab5b8SVladimir Oltean 
3257511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
3258511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
3259511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
3260511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
3261511e6ca0SVladimir Oltean  * mirroring rule that references it.
3262511e6ca0SVladimir Oltean  */
3263511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
3264511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
3265511e6ca0SVladimir Oltean {
3266511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
3267511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
3268511e6ca0SVladimir Oltean 	struct sja1105_table *table;
3269511e6ca0SVladimir Oltean 	bool already_enabled;
3270511e6ca0SVladimir Oltean 	u64 new_mirr_port;
3271511e6ca0SVladimir Oltean 	int rc;
3272511e6ca0SVladimir Oltean 
3273511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
3274511e6ca0SVladimir Oltean 	general_params = table->entries;
3275511e6ca0SVladimir Oltean 
3276511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
3277511e6ca0SVladimir Oltean 
3278511e6ca0SVladimir Oltean 	already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS);
3279511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
3280511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
3281511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
3282511e6ca0SVladimir Oltean 			general_params->mirr_port);
3283511e6ca0SVladimir Oltean 		return -EBUSY;
3284511e6ca0SVladimir Oltean 	}
3285511e6ca0SVladimir Oltean 
3286511e6ca0SVladimir Oltean 	new_mirr_port = to;
3287511e6ca0SVladimir Oltean 	if (!enabled) {
3288511e6ca0SVladimir Oltean 		bool keep = false;
3289511e6ca0SVladimir Oltean 		int port;
3290511e6ca0SVladimir Oltean 
3291511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
3292511e6ca0SVladimir Oltean 		for (port = 0; port < SJA1105_NUM_PORTS; port++) {
3293511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
3294511e6ca0SVladimir Oltean 				keep = true;
3295511e6ca0SVladimir Oltean 				break;
3296511e6ca0SVladimir Oltean 			}
3297511e6ca0SVladimir Oltean 		}
3298511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
3299511e6ca0SVladimir Oltean 		if (!keep)
3300511e6ca0SVladimir Oltean 			new_mirr_port = SJA1105_NUM_PORTS;
3301511e6ca0SVladimir Oltean 	}
3302511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
3303511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
3304511e6ca0SVladimir Oltean 
3305511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
3306511e6ca0SVladimir Oltean 						  0, general_params, true);
3307511e6ca0SVladimir Oltean 		if (rc < 0)
3308511e6ca0SVladimir Oltean 			return rc;
3309511e6ca0SVladimir Oltean 	}
3310511e6ca0SVladimir Oltean 
3311511e6ca0SVladimir Oltean 	if (ingress)
3312511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
3313511e6ca0SVladimir Oltean 	else
3314511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
3315511e6ca0SVladimir Oltean 
3316511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
3317511e6ca0SVladimir Oltean 					    &mac[from], true);
3318511e6ca0SVladimir Oltean }
3319511e6ca0SVladimir Oltean 
3320511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
3321511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
3322511e6ca0SVladimir Oltean 			      bool ingress)
3323511e6ca0SVladimir Oltean {
3324511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3325511e6ca0SVladimir Oltean 				    ingress, true);
3326511e6ca0SVladimir Oltean }
3327511e6ca0SVladimir Oltean 
3328511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
3329511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
3330511e6ca0SVladimir Oltean {
3331511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3332511e6ca0SVladimir Oltean 			     mirror->ingress, false);
3333511e6ca0SVladimir Oltean }
3334511e6ca0SVladimir Oltean 
3335a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
3336a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
3337a7cc081cSVladimir Oltean {
3338a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
3339a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3340a7cc081cSVladimir Oltean 
3341a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3342a7cc081cSVladimir Oltean 
3343a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
3344a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
3345a7cc081cSVladimir Oltean 	 * bytes.
3346a7cc081cSVladimir Oltean 	 */
3347a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
3348a7cc081cSVladimir Oltean 				      1000000);
33495f035af7SPo Liu 	policing[port].smax = policer->burst;
3350a7cc081cSVladimir Oltean 
3351a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3352a7cc081cSVladimir Oltean }
3353a7cc081cSVladimir Oltean 
3354a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
3355a7cc081cSVladimir Oltean {
3356a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
3357a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3358a7cc081cSVladimir Oltean 
3359a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3360a7cc081cSVladimir Oltean 
3361a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
3362a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
3363a7cc081cSVladimir Oltean 
3364a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3365a7cc081cSVladimir Oltean }
3366a7cc081cSVladimir Oltean 
33678aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
33688aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
33698aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
3370f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
33718456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
3372c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
3373c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
3374ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
3375ffe10e67SVladimir Oltean 	.phylink_mac_link_state	= sja1105_mac_pcs_get_state,
3376af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
33778400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
33788400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
337952c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
338052c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
338152c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
3382bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
3383e9bf9694SVladimir Oltean 	.port_enable		= sja1105_port_enable,
3384a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
3385291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
3386291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
3387291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
33888aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
33898aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
3390640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
33916666cebcSVladimir Oltean 	.port_vlan_prepare	= sja1105_vlan_prepare,
33926666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
33936666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
33946666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
3395291d1e72SVladimir Oltean 	.port_mdb_prepare	= sja1105_mdb_prepare,
3396291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
3397291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
3398a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
3399a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
3400f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
340147ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
3402317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
3403511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
3404511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
3405a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
3406a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
3407a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
3408a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
3409834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
3410ac02a451SVladimir Oltean 	.crosschip_bridge_join	= sja1105_crosschip_bridge_join,
3411ac02a451SVladimir Oltean 	.crosschip_bridge_leave	= sja1105_crosschip_bridge_leave,
34122cafa72eSVladimir Oltean 	.devlink_param_get	= sja1105_devlink_param_get,
34132cafa72eSVladimir Oltean 	.devlink_param_set	= sja1105_devlink_param_set,
34148aa9ebccSVladimir Oltean };
34158aa9ebccSVladimir Oltean 
34160b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[];
34170b0e2997SVladimir Oltean 
34188aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
34198aa9ebccSVladimir Oltean {
34208aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
34218aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
34228aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
34230b0e2997SVladimir Oltean 	const struct of_device_id *match;
3424dff79620SVladimir Oltean 	u32 device_id;
34258aa9ebccSVladimir Oltean 	u64 part_no;
34268aa9ebccSVladimir Oltean 	int rc;
34278aa9ebccSVladimir Oltean 
342834d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
342934d76e9fSVladimir Oltean 			      NULL);
34308aa9ebccSVladimir Oltean 	if (rc < 0)
34318aa9ebccSVladimir Oltean 		return rc;
34328aa9ebccSVladimir Oltean 
34331bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
34341bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
34358aa9ebccSVladimir Oltean 	if (rc < 0)
34368aa9ebccSVladimir Oltean 		return rc;
34378aa9ebccSVladimir Oltean 
34388aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
34398aa9ebccSVladimir Oltean 
34405978fac0SNathan Chancellor 	for (match = sja1105_dt_ids; match->compatible[0]; match++) {
34410b0e2997SVladimir Oltean 		const struct sja1105_info *info = match->data;
34420b0e2997SVladimir Oltean 
34430b0e2997SVladimir Oltean 		/* Is what's been probed in our match table at all? */
34440b0e2997SVladimir Oltean 		if (info->device_id != device_id || info->part_no != part_no)
34450b0e2997SVladimir Oltean 			continue;
34460b0e2997SVladimir Oltean 
34470b0e2997SVladimir Oltean 		/* But is it what's in the device tree? */
34480b0e2997SVladimir Oltean 		if (priv->info->device_id != device_id ||
34490b0e2997SVladimir Oltean 		    priv->info->part_no != part_no) {
34500b0e2997SVladimir Oltean 			dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
34510b0e2997SVladimir Oltean 				 priv->info->name, info->name);
34520b0e2997SVladimir Oltean 			/* It isn't. No problem, pick that up. */
34530b0e2997SVladimir Oltean 			priv->info = info;
34548aa9ebccSVladimir Oltean 		}
34558aa9ebccSVladimir Oltean 
34568aa9ebccSVladimir Oltean 		return 0;
34578aa9ebccSVladimir Oltean 	}
34588aa9ebccSVladimir Oltean 
34590b0e2997SVladimir Oltean 	dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
34600b0e2997SVladimir Oltean 		device_id, part_no);
34610b0e2997SVladimir Oltean 
34620b0e2997SVladimir Oltean 	return -ENODEV;
34630b0e2997SVladimir Oltean }
34640b0e2997SVladimir Oltean 
34658aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
34668aa9ebccSVladimir Oltean {
3467844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
34688aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
34698aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
34708aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
3471a68578c2SVladimir Oltean 	int rc, port;
34728aa9ebccSVladimir Oltean 
34738aa9ebccSVladimir Oltean 	if (!dev->of_node) {
34748aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
34758aa9ebccSVladimir Oltean 		return -EINVAL;
34768aa9ebccSVladimir Oltean 	}
34778aa9ebccSVladimir Oltean 
34788aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
34798aa9ebccSVladimir Oltean 	if (!priv)
34808aa9ebccSVladimir Oltean 		return -ENOMEM;
34818aa9ebccSVladimir Oltean 
34828aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
34838aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
34848aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
34858aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
34868aa9ebccSVladimir Oltean 	else
34878aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
34888aa9ebccSVladimir Oltean 
34898aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
34908aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
34918aa9ebccSVladimir Oltean 	 */
34928aa9ebccSVladimir Oltean 	priv->spidev = spi;
34938aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
34948aa9ebccSVladimir Oltean 
34958aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
34968aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
34978aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
34988aa9ebccSVladimir Oltean 	if (rc < 0) {
34998aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
35008aa9ebccSVladimir Oltean 		return rc;
35018aa9ebccSVladimir Oltean 	}
35028aa9ebccSVladimir Oltean 
35038aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
35048aa9ebccSVladimir Oltean 
35058aa9ebccSVladimir Oltean 	/* Detect hardware device */
35068aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
35078aa9ebccSVladimir Oltean 	if (rc < 0) {
35088aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
35098aa9ebccSVladimir Oltean 		return rc;
35108aa9ebccSVladimir Oltean 	}
35118aa9ebccSVladimir Oltean 
35128aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
35138aa9ebccSVladimir Oltean 
35147e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
35158aa9ebccSVladimir Oltean 	if (!ds)
35168aa9ebccSVladimir Oltean 		return -ENOMEM;
35178aa9ebccSVladimir Oltean 
35187e99e347SVivien Didelot 	ds->dev = dev;
35197e99e347SVivien Didelot 	ds->num_ports = SJA1105_NUM_PORTS;
35208aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
35218aa9ebccSVladimir Oltean 	ds->priv = priv;
35228aa9ebccSVladimir Oltean 	priv->ds = ds;
35238aa9ebccSVladimir Oltean 
3524844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
3525844d7edcSVladimir Oltean 
3526d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
3527d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
3528d5a619bfSVivien Didelot 
3529*5899ee36SVladimir Oltean 	priv->dsa_8021q_ctx = devm_kzalloc(dev, sizeof(*priv->dsa_8021q_ctx),
3530*5899ee36SVladimir Oltean 					   GFP_KERNEL);
3531*5899ee36SVladimir Oltean 	if (!priv->dsa_8021q_ctx)
3532*5899ee36SVladimir Oltean 		return -ENOMEM;
3533*5899ee36SVladimir Oltean 
3534*5899ee36SVladimir Oltean 	priv->dsa_8021q_ctx->ops = &sja1105_dsa_8021q_ops;
3535*5899ee36SVladimir Oltean 	priv->dsa_8021q_ctx->ds = ds;
3536*5899ee36SVladimir Oltean 
3537*5899ee36SVladimir Oltean 	INIT_LIST_HEAD(&priv->dsa_8021q_ctx->crosschip_links);
3538ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&priv->bridge_vlans);
3539ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&priv->dsa_8021q_vlans);
3540ac02a451SVladimir Oltean 
3541d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
3542a6af7763SVladimir Oltean 	sja1105_flower_setup(ds);
3543d5a619bfSVivien Didelot 
3544d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
3545d5a619bfSVivien Didelot 	if (rc)
3546d5a619bfSVivien Didelot 		return rc;
3547d5a619bfSVivien Didelot 
35484d752508SVladimir Oltean 	if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
35494d752508SVladimir Oltean 		priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
35504d752508SVladimir Oltean 					 sizeof(struct sja1105_cbs_entry),
35514d752508SVladimir Oltean 					 GFP_KERNEL);
35524d752508SVladimir Oltean 		if (!priv->cbs)
35534d752508SVladimir Oltean 			return -ENOMEM;
35544d752508SVladimir Oltean 	}
35554d752508SVladimir Oltean 
3556227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
3557a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
3558a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3559a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
3560a68578c2SVladimir Oltean 		struct net_device *slave;
356184eeb5d4SVladimir Oltean 		int subvlan;
3562227d07a0SVladimir Oltean 
3563a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3564a68578c2SVladimir Oltean 			continue;
3565a68578c2SVladimir Oltean 
3566a68578c2SVladimir Oltean 		dp->priv = sp;
3567a68578c2SVladimir Oltean 		sp->dp = dp;
3568844d7edcSVladimir Oltean 		sp->data = tagger_data;
3569a68578c2SVladimir Oltean 		slave = dp->slave;
3570a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
3571a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
3572a68578c2SVladimir Oltean 							slave->name);
3573a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
3574a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
3575a68578c2SVladimir Oltean 			dev_err(ds->dev,
3576a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
3577a68578c2SVladimir Oltean 				rc);
3578a68578c2SVladimir Oltean 			goto out;
3579a68578c2SVladimir Oltean 		}
3580a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
358138b5beeaSVladimir Oltean 		sp->xmit_tpid = ETH_P_SJA1105;
358284eeb5d4SVladimir Oltean 
358384eeb5d4SVladimir Oltean 		for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
358484eeb5d4SVladimir Oltean 			sp->subvlan_map[subvlan] = VLAN_N_VID;
3585227d07a0SVladimir Oltean 	}
3586227d07a0SVladimir Oltean 
3587d5a619bfSVivien Didelot 	return 0;
3588a68578c2SVladimir Oltean out:
3589a68578c2SVladimir Oltean 	while (port-- > 0) {
3590a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3591a68578c2SVladimir Oltean 
3592a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3593a68578c2SVladimir Oltean 			continue;
3594a68578c2SVladimir Oltean 
3595a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
3596a68578c2SVladimir Oltean 	}
3597a68578c2SVladimir Oltean 	return rc;
35988aa9ebccSVladimir Oltean }
35998aa9ebccSVladimir Oltean 
36008aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
36018aa9ebccSVladimir Oltean {
36028aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
36038aa9ebccSVladimir Oltean 
36048aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
36058aa9ebccSVladimir Oltean 	return 0;
36068aa9ebccSVladimir Oltean }
36078aa9ebccSVladimir Oltean 
36088aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
36098aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
36108aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
36118aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
36128aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
36138aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
36148aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
36158aa9ebccSVladimir Oltean 	{ /* sentinel */ },
36168aa9ebccSVladimir Oltean };
36178aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
36188aa9ebccSVladimir Oltean 
36198aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
36208aa9ebccSVladimir Oltean 	.driver = {
36218aa9ebccSVladimir Oltean 		.name  = "sja1105",
36228aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
36238aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
36248aa9ebccSVladimir Oltean 	},
36258aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
36268aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
36278aa9ebccSVladimir Oltean };
36288aa9ebccSVladimir Oltean 
36298aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
36308aa9ebccSVladimir Oltean 
36318aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
36328aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
36338aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
36348aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
3635