xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision bf4edf4afb87e67bed8678c09a01110148830483)
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 
284d942354SVladimir Oltean #define SJA1105_UNKNOWN_MULTICAST	0x010000000000ull
29ed040abcSVladimir Oltean #define SJA1105_DEFAULT_VLAN		(VLAN_N_VID - 1)
304d942354SVladimir Oltean 
31ac02a451SVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops;
32ac02a451SVladimir Oltean 
338aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
348aa9ebccSVladimir Oltean 			     unsigned int startup_delay)
358aa9ebccSVladimir Oltean {
368aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
378aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
388aa9ebccSVladimir Oltean 	msleep(pulse_len);
398aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
408aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
418aa9ebccSVladimir Oltean 	msleep(startup_delay);
428aa9ebccSVladimir Oltean }
438aa9ebccSVladimir Oltean 
448aa9ebccSVladimir Oltean static void
458aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
468aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
478aa9ebccSVladimir Oltean {
484d942354SVladimir Oltean 	if (allow)
498aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
504d942354SVladimir Oltean 	else
518aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
528aa9ebccSVladimir Oltean }
538aa9ebccSVladimir Oltean 
547f7ccdeaSVladimir Oltean static bool sja1105_can_forward(struct sja1105_l2_forwarding_entry *l2_fwd,
557f7ccdeaSVladimir Oltean 				int from, int to)
567f7ccdeaSVladimir Oltean {
577f7ccdeaSVladimir Oltean 	return !!(l2_fwd[from].reach_port & BIT(to));
587f7ccdeaSVladimir Oltean }
597f7ccdeaSVladimir Oltean 
608aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree
618aa9ebccSVladimir Oltean  * settings into sja1105_setup
628aa9ebccSVladimir Oltean  */
638aa9ebccSVladimir Oltean struct sja1105_dt_port {
648aa9ebccSVladimir Oltean 	phy_interface_t phy_mode;
658aa9ebccSVladimir Oltean 	sja1105_mii_role_t role;
668aa9ebccSVladimir Oltean };
678aa9ebccSVladimir Oltean 
688aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
698aa9ebccSVladimir Oltean {
708aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
718aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
728aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
738aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
748aa9ebccSVladimir Oltean 		 */
758aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
768aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
778aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
788aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
798aa9ebccSVladimir Oltean 		.ifg = 0,
808aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
811fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
828aa9ebccSVladimir Oltean 		 */
838aa9ebccSVladimir Oltean 		.speed = SJA1105_SPEED_AUTO,
848aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
858aa9ebccSVladimir Oltean 		.tp_delin = 0,
868aa9ebccSVladimir Oltean 		.tp_delout = 0,
878aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
888aa9ebccSVladimir Oltean 		.maxage = 0xFF,
898aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
908aa9ebccSVladimir Oltean 		.vlanprio = 0,
91e3502b82SVladimir Oltean 		.vlanid = 1,
928aa9ebccSVladimir Oltean 		.ing_mirr = false,
938aa9ebccSVladimir Oltean 		.egr_mirr = false,
948aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
958aa9ebccSVladimir Oltean 		.drpnona664 = false,
968aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
978aa9ebccSVladimir Oltean 		.drpdtag = false,
988aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
998aa9ebccSVladimir Oltean 		.drpuntag = false,
1008aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
1018aa9ebccSVladimir Oltean 		.retag = false,
102640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
103640f763fSVladimir Oltean 		 * STP will enable it.
104640f763fSVladimir Oltean 		 */
105640f763fSVladimir Oltean 		.dyn_learn = false,
1068aa9ebccSVladimir Oltean 		.egress = false,
1078aa9ebccSVladimir Oltean 		.ingress = false,
1088aa9ebccSVladimir Oltean 	};
1098aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
110542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
1118aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1128aa9ebccSVladimir Oltean 	int i;
1138aa9ebccSVladimir Oltean 
1148aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1158aa9ebccSVladimir Oltean 
1168aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1178aa9ebccSVladimir Oltean 	if (table->entry_count) {
1188aa9ebccSVladimir Oltean 		kfree(table->entries);
1198aa9ebccSVladimir Oltean 		table->entry_count = 0;
1208aa9ebccSVladimir Oltean 	}
1218aa9ebccSVladimir Oltean 
122fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
1238aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1248aa9ebccSVladimir Oltean 	if (!table->entries)
1258aa9ebccSVladimir Oltean 		return -ENOMEM;
1268aa9ebccSVladimir Oltean 
127fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
1288aa9ebccSVladimir Oltean 
1298aa9ebccSVladimir Oltean 	mac = table->entries;
1308aa9ebccSVladimir Oltean 
131542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1328aa9ebccSVladimir Oltean 		mac[i] = default_mac;
133640f763fSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, i)) {
134640f763fSVladimir Oltean 			/* STP doesn't get called for CPU port, so we need to
135640f763fSVladimir Oltean 			 * set the I/O parameters statically.
136640f763fSVladimir Oltean 			 */
137640f763fSVladimir Oltean 			mac[i].dyn_learn = true;
138640f763fSVladimir Oltean 			mac[i].ingress = true;
139640f763fSVladimir Oltean 			mac[i].egress = true;
140640f763fSVladimir Oltean 		}
141640f763fSVladimir Oltean 	}
1428aa9ebccSVladimir Oltean 
1438aa9ebccSVladimir Oltean 	return 0;
1448aa9ebccSVladimir Oltean }
1458aa9ebccSVladimir Oltean 
146ffe10e67SVladimir Oltean static bool sja1105_supports_sgmii(struct sja1105_private *priv, int port)
147ffe10e67SVladimir Oltean {
148ffe10e67SVladimir Oltean 	if (priv->info->part_no != SJA1105R_PART_NO &&
149ffe10e67SVladimir Oltean 	    priv->info->part_no != SJA1105S_PART_NO)
150ffe10e67SVladimir Oltean 		return false;
151ffe10e67SVladimir Oltean 
152ffe10e67SVladimir Oltean 	if (port != SJA1105_SGMII_PORT)
153ffe10e67SVladimir Oltean 		return false;
154ffe10e67SVladimir Oltean 
155ffe10e67SVladimir Oltean 	if (dsa_is_unused_port(priv->ds, port))
156ffe10e67SVladimir Oltean 		return false;
157ffe10e67SVladimir Oltean 
158ffe10e67SVladimir Oltean 	return true;
159ffe10e67SVladimir Oltean }
160ffe10e67SVladimir Oltean 
1618aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv,
1628aa9ebccSVladimir Oltean 				     struct sja1105_dt_port *ports)
1638aa9ebccSVladimir Oltean {
1648aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1658aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
166542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
1678aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1688aa9ebccSVladimir Oltean 	int i;
1698aa9ebccSVladimir Oltean 
1708aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1718aa9ebccSVladimir Oltean 
1728aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1738aa9ebccSVladimir Oltean 	if (table->entry_count) {
1748aa9ebccSVladimir Oltean 		kfree(table->entries);
1758aa9ebccSVladimir Oltean 		table->entry_count = 0;
1768aa9ebccSVladimir Oltean 	}
1778aa9ebccSVladimir Oltean 
178fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
1798aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1808aa9ebccSVladimir Oltean 	if (!table->entries)
1818aa9ebccSVladimir Oltean 		return -ENOMEM;
1828aa9ebccSVladimir Oltean 
1831fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
184fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
1858aa9ebccSVladimir Oltean 
1868aa9ebccSVladimir Oltean 	mii = table->entries;
1878aa9ebccSVladimir Oltean 
188542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
189ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
190ee9d0cb6SVladimir Oltean 			continue;
191ee9d0cb6SVladimir Oltean 
1928aa9ebccSVladimir Oltean 		switch (ports[i].phy_mode) {
1938aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
1948aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1958aa9ebccSVladimir Oltean 			break;
1968aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
1978aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
1988aa9ebccSVladimir Oltean 			break;
1998aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
2008aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
2018aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
2028aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
2038aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
2048aa9ebccSVladimir Oltean 			break;
205ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
206ffe10e67SVladimir Oltean 			if (!sja1105_supports_sgmii(priv, i))
207ffe10e67SVladimir Oltean 				return -EINVAL;
208ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
209ffe10e67SVladimir Oltean 			break;
2108aa9ebccSVladimir Oltean 		default:
2118aa9ebccSVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s!\n",
2128aa9ebccSVladimir Oltean 				phy_modes(ports[i].phy_mode));
2136729188dSVladimir Oltean 			return -EINVAL;
2148aa9ebccSVladimir Oltean 		}
2158aa9ebccSVladimir Oltean 
216ffe10e67SVladimir Oltean 		/* Even though the SerDes port is able to drive SGMII autoneg
217ffe10e67SVladimir Oltean 		 * like a PHY would, from the perspective of the XMII tables,
218ffe10e67SVladimir Oltean 		 * the SGMII port should always be put in MAC mode.
219ffe10e67SVladimir Oltean 		 */
220ffe10e67SVladimir Oltean 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII)
221ffe10e67SVladimir Oltean 			mii->phy_mac[i] = XMII_MAC;
222ffe10e67SVladimir Oltean 		else
2238aa9ebccSVladimir Oltean 			mii->phy_mac[i] = ports[i].role;
2248aa9ebccSVladimir Oltean 	}
2258aa9ebccSVladimir Oltean 	return 0;
2268aa9ebccSVladimir Oltean }
2278aa9ebccSVladimir Oltean 
2288aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
2298aa9ebccSVladimir Oltean {
2304d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
2318aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2324d942354SVladimir Oltean 	int port;
2338aa9ebccSVladimir Oltean 
2348aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
2358aa9ebccSVladimir Oltean 
2364d942354SVladimir Oltean 	/* We only populate the FDB table through dynamic L2 Address Lookup
2374d942354SVladimir Oltean 	 * entries, except for a special entry at the end which is a catch-all
2384d942354SVladimir Oltean 	 * for unknown multicast and will be used to control flooding domain.
239291d1e72SVladimir Oltean 	 */
2408aa9ebccSVladimir Oltean 	if (table->entry_count) {
2418aa9ebccSVladimir Oltean 		kfree(table->entries);
2428aa9ebccSVladimir Oltean 		table->entry_count = 0;
2438aa9ebccSVladimir Oltean 	}
2444d942354SVladimir Oltean 
2454d942354SVladimir Oltean 	if (!priv->info->can_limit_mcast_flood)
2464d942354SVladimir Oltean 		return 0;
2474d942354SVladimir Oltean 
2484d942354SVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
2494d942354SVladimir Oltean 				 GFP_KERNEL);
2504d942354SVladimir Oltean 	if (!table->entries)
2514d942354SVladimir Oltean 		return -ENOMEM;
2524d942354SVladimir Oltean 
2534d942354SVladimir Oltean 	table->entry_count = 1;
2544d942354SVladimir Oltean 	l2_lookup = table->entries;
2554d942354SVladimir Oltean 
2564d942354SVladimir Oltean 	/* All L2 multicast addresses have an odd first octet */
2574d942354SVladimir Oltean 	l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST;
2584d942354SVladimir Oltean 	l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST;
2594d942354SVladimir Oltean 	l2_lookup[0].lockeds = true;
2604d942354SVladimir Oltean 	l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1;
2614d942354SVladimir Oltean 
2624d942354SVladimir Oltean 	/* Flood multicast to every port by default */
2634d942354SVladimir Oltean 	for (port = 0; port < priv->ds->num_ports; port++)
2644d942354SVladimir Oltean 		if (!dsa_is_unused_port(priv->ds, port))
2654d942354SVladimir Oltean 			l2_lookup[0].destports |= BIT(port);
2664d942354SVladimir Oltean 
2678aa9ebccSVladimir Oltean 	return 0;
2688aa9ebccSVladimir Oltean }
2698aa9ebccSVladimir Oltean 
2708aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
2718aa9ebccSVladimir Oltean {
2728aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
2738456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
2748456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
2758aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
2768aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
2771da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
2781da73821SVladimir Oltean 		.start_dynspc = 0,
2798aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
2808aa9ebccSVladimir Oltean 		.poly = 0x97,
2818aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
2828aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
2838aa9ebccSVladimir Oltean 		 */
2846d7c7d94SVladimir Oltean 		.shared_learn = true,
2858aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
2868aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
2878aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
2888aa9ebccSVladimir Oltean 		 */
2898aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
2908aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
2918aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
2928aa9ebccSVladimir Oltean 		 */
2938aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
2941da73821SVladimir Oltean 		/* P/Q/R/S only */
2951da73821SVladimir Oltean 		.use_static = true,
2961da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
2971da73821SVladimir Oltean 		 * dynamic FDB entries
2981da73821SVladimir Oltean 		 */
2991da73821SVladimir Oltean 		.owr_dyn = true,
3001da73821SVladimir Oltean 		.drpnolearn = true,
3018aa9ebccSVladimir Oltean 	};
302542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
303f238fef1SVladimir Oltean 	int port, num_used_ports = 0;
304542043e9SVladimir Oltean 	struct sja1105_table *table;
305542043e9SVladimir Oltean 	u64 max_fdb_entries;
306542043e9SVladimir Oltean 
307542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++)
308f238fef1SVladimir Oltean 		if (!dsa_is_unused_port(ds, port))
309f238fef1SVladimir Oltean 			num_used_ports++;
310f238fef1SVladimir Oltean 
311f238fef1SVladimir Oltean 	max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports;
312f238fef1SVladimir Oltean 
313f238fef1SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
314f238fef1SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
315f238fef1SVladimir Oltean 			continue;
316f238fef1SVladimir Oltean 
317542043e9SVladimir Oltean 		default_l2_lookup_params.maxaddrp[port] = max_fdb_entries;
318f238fef1SVladimir Oltean 	}
3198aa9ebccSVladimir Oltean 
3208aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
3218aa9ebccSVladimir Oltean 
3228aa9ebccSVladimir Oltean 	if (table->entry_count) {
3238aa9ebccSVladimir Oltean 		kfree(table->entries);
3248aa9ebccSVladimir Oltean 		table->entry_count = 0;
3258aa9ebccSVladimir Oltean 	}
3268aa9ebccSVladimir Oltean 
327fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
3288aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3298aa9ebccSVladimir Oltean 	if (!table->entries)
3308aa9ebccSVladimir Oltean 		return -ENOMEM;
3318aa9ebccSVladimir Oltean 
332fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
3338aa9ebccSVladimir Oltean 
3348aa9ebccSVladimir Oltean 	/* This table only has a single entry */
3358aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
3368aa9ebccSVladimir Oltean 				default_l2_lookup_params;
3378aa9ebccSVladimir Oltean 
3388aa9ebccSVladimir Oltean 	return 0;
3398aa9ebccSVladimir Oltean }
3408aa9ebccSVladimir Oltean 
341ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU
342ed040abcSVladimir Oltean  * using management routes (e.g. STP, PTP) as opposed to tag_8021q.
343ed040abcSVladimir Oltean  * All DT-defined ports are members of this VLAN, and there are no
344ed040abcSVladimir Oltean  * restrictions on forwarding (since the CPU selects the destination).
345ed040abcSVladimir Oltean  * Frames from this VLAN will always be transmitted as untagged, and
346ed040abcSVladimir Oltean  * neither the bridge nor the 8021q module cannot create this VLAN ID.
347ed040abcSVladimir Oltean  */
3488aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
3498aa9ebccSVladimir Oltean {
3508aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3518aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
3528aa9ebccSVladimir Oltean 		.ving_mirr = 0,
3538aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
3548aa9ebccSVladimir Oltean 		.vmemb_port = 0,
3558aa9ebccSVladimir Oltean 		.vlan_bc = 0,
3568aa9ebccSVladimir Oltean 		.tag_port = 0,
357ed040abcSVladimir Oltean 		.vlanid = SJA1105_DEFAULT_VLAN,
3588aa9ebccSVladimir Oltean 	};
359ec5ae610SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
360ec5ae610SVladimir Oltean 	int port;
3618aa9ebccSVladimir Oltean 
3628aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
3638aa9ebccSVladimir Oltean 
3648aa9ebccSVladimir Oltean 	if (table->entry_count) {
3658aa9ebccSVladimir Oltean 		kfree(table->entries);
3668aa9ebccSVladimir Oltean 		table->entry_count = 0;
3678aa9ebccSVladimir Oltean 	}
3688aa9ebccSVladimir Oltean 
369c75857b0SZheng Yongjun 	table->entries = kzalloc(table->ops->unpacked_entry_size,
3708aa9ebccSVladimir Oltean 				 GFP_KERNEL);
3718aa9ebccSVladimir Oltean 	if (!table->entries)
3728aa9ebccSVladimir Oltean 		return -ENOMEM;
3738aa9ebccSVladimir Oltean 
3748aa9ebccSVladimir Oltean 	table->entry_count = 1;
3758aa9ebccSVladimir Oltean 
376ec5ae610SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
377ec5ae610SVladimir Oltean 		struct sja1105_bridge_vlan *v;
378ec5ae610SVladimir Oltean 
379ec5ae610SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
380ec5ae610SVladimir Oltean 			continue;
381ec5ae610SVladimir Oltean 
382ec5ae610SVladimir Oltean 		pvid.vmemb_port |= BIT(port);
383ec5ae610SVladimir Oltean 		pvid.vlan_bc |= BIT(port);
384ec5ae610SVladimir Oltean 		pvid.tag_port &= ~BIT(port);
385ec5ae610SVladimir Oltean 
386ec5ae610SVladimir Oltean 		v = kzalloc(sizeof(*v), GFP_KERNEL);
387ec5ae610SVladimir Oltean 		if (!v)
388ec5ae610SVladimir Oltean 			return -ENOMEM;
389ec5ae610SVladimir Oltean 
390ec5ae610SVladimir Oltean 		v->port = port;
391ed040abcSVladimir Oltean 		v->vid = SJA1105_DEFAULT_VLAN;
392ec5ae610SVladimir Oltean 		v->untagged = true;
393ec5ae610SVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
394ec5ae610SVladimir Oltean 			v->pvid = true;
395ec5ae610SVladimir Oltean 		list_add(&v->list, &priv->dsa_8021q_vlans);
3968aa9ebccSVladimir Oltean 	}
3978aa9ebccSVladimir Oltean 
3988aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
3998aa9ebccSVladimir Oltean 	return 0;
4008aa9ebccSVladimir Oltean }
4018aa9ebccSVladimir Oltean 
4028aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
4038aa9ebccSVladimir Oltean {
4048aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
405542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
4068aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4078aa9ebccSVladimir Oltean 	int i, j;
4088aa9ebccSVladimir Oltean 
4098aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
4108aa9ebccSVladimir Oltean 
4118aa9ebccSVladimir Oltean 	if (table->entry_count) {
4128aa9ebccSVladimir Oltean 		kfree(table->entries);
4138aa9ebccSVladimir Oltean 		table->entry_count = 0;
4148aa9ebccSVladimir Oltean 	}
4158aa9ebccSVladimir Oltean 
416fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4178aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4188aa9ebccSVladimir Oltean 	if (!table->entries)
4198aa9ebccSVladimir Oltean 		return -ENOMEM;
4208aa9ebccSVladimir Oltean 
421fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
4228aa9ebccSVladimir Oltean 
4238aa9ebccSVladimir Oltean 	l2fwd = table->entries;
4248aa9ebccSVladimir Oltean 
4258aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
426542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
4278aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
4288aa9ebccSVladimir Oltean 
429f238fef1SVladimir Oltean 		if (dsa_is_unused_port(ds, i))
430f238fef1SVladimir Oltean 			continue;
431f238fef1SVladimir Oltean 
4328aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
4338aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
4348aa9ebccSVladimir Oltean 
4357f7ccdeaSVladimir Oltean 		/* All ports start up with egress flooding enabled,
4367f7ccdeaSVladimir Oltean 		 * including the CPU port.
4377f7ccdeaSVladimir Oltean 		 */
4387f7ccdeaSVladimir Oltean 		priv->ucast_egress_floods |= BIT(i);
4397f7ccdeaSVladimir Oltean 		priv->bcast_egress_floods |= BIT(i);
4407f7ccdeaSVladimir Oltean 
4418aa9ebccSVladimir Oltean 		if (i == upstream)
4428aa9ebccSVladimir Oltean 			continue;
4438aa9ebccSVladimir Oltean 
4448aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
4458aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
4464d942354SVladimir Oltean 
4474d942354SVladimir Oltean 		l2fwd[i].bc_domain = BIT(upstream);
4484d942354SVladimir Oltean 		l2fwd[i].fl_domain = BIT(upstream);
4494d942354SVladimir Oltean 
4504d942354SVladimir Oltean 		l2fwd[upstream].bc_domain |= BIT(i);
4514d942354SVladimir Oltean 		l2fwd[upstream].fl_domain |= BIT(i);
4528aa9ebccSVladimir Oltean 	}
453f238fef1SVladimir Oltean 
4548aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
4558aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
4568aa9ebccSVladimir Oltean 	 */
457f238fef1SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++) {
458f238fef1SVladimir Oltean 		for (j = 0; j < ds->num_ports; j++) {
459f238fef1SVladimir Oltean 			if (dsa_is_unused_port(ds, j))
460f238fef1SVladimir Oltean 				continue;
461f238fef1SVladimir Oltean 
462542043e9SVladimir Oltean 			l2fwd[ds->num_ports + i].vlan_pmap[j] = i;
463f238fef1SVladimir Oltean 		}
464f238fef1SVladimir Oltean 	}
4658aa9ebccSVladimir Oltean 
4668aa9ebccSVladimir Oltean 	return 0;
4678aa9ebccSVladimir Oltean }
4688aa9ebccSVladimir Oltean 
4698aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
4708aa9ebccSVladimir Oltean {
4711bf658eeSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2fwd_params;
4728aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4738aa9ebccSVladimir Oltean 
4748aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
4758aa9ebccSVladimir Oltean 
4768aa9ebccSVladimir Oltean 	if (table->entry_count) {
4778aa9ebccSVladimir Oltean 		kfree(table->entries);
4788aa9ebccSVladimir Oltean 		table->entry_count = 0;
4798aa9ebccSVladimir Oltean 	}
4808aa9ebccSVladimir Oltean 
481fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4828aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4838aa9ebccSVladimir Oltean 	if (!table->entries)
4848aa9ebccSVladimir Oltean 		return -ENOMEM;
4858aa9ebccSVladimir Oltean 
486fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
4878aa9ebccSVladimir Oltean 
4888aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4891bf658eeSVladimir Oltean 	l2fwd_params = table->entries;
4901bf658eeSVladimir Oltean 
4911bf658eeSVladimir Oltean 	/* Disallow dynamic reconfiguration of vlan_pmap */
4921bf658eeSVladimir Oltean 	l2fwd_params->max_dynp = 0;
4931bf658eeSVladimir Oltean 	/* Use a single memory partition for all ingress queues */
4941bf658eeSVladimir Oltean 	l2fwd_params->part_spc[0] = priv->info->max_frame_mem;
4958aa9ebccSVladimir Oltean 
4968aa9ebccSVladimir Oltean 	return 0;
4978aa9ebccSVladimir Oltean }
4988aa9ebccSVladimir Oltean 
499aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
500aaa270c6SVladimir Oltean {
501aaa270c6SVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
502aaa270c6SVladimir Oltean 	struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
5031bf658eeSVladimir Oltean 	int max_mem = priv->info->max_frame_mem;
504aaa270c6SVladimir Oltean 	struct sja1105_table *table;
505aaa270c6SVladimir Oltean 
506aaa270c6SVladimir Oltean 	/* VLAN retagging is implemented using a loopback port that consumes
507aaa270c6SVladimir Oltean 	 * frame buffers. That leaves less for us.
508aaa270c6SVladimir Oltean 	 */
509aaa270c6SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_BEST_EFFORT)
5101bf658eeSVladimir Oltean 		max_mem -= SJA1105_FRAME_MEMORY_RETAGGING_OVERHEAD;
511aaa270c6SVladimir Oltean 
512aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
513aaa270c6SVladimir Oltean 	l2_fwd_params = table->entries;
514aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] = max_mem;
515aaa270c6SVladimir Oltean 
516aaa270c6SVladimir Oltean 	/* If we have any critical-traffic virtual links, we need to reserve
517aaa270c6SVladimir Oltean 	 * some frame buffer memory for them. At the moment, hardcode the value
518aaa270c6SVladimir Oltean 	 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
519aaa270c6SVladimir Oltean 	 * remaining for best-effort traffic. TODO: figure out a more flexible
520aaa270c6SVladimir Oltean 	 * way to perform the frame buffer partitioning.
521aaa270c6SVladimir Oltean 	 */
522aaa270c6SVladimir Oltean 	if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
523aaa270c6SVladimir Oltean 		return;
524aaa270c6SVladimir Oltean 
525aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
526aaa270c6SVladimir Oltean 	vl_fwd_params = table->entries;
527aaa270c6SVladimir Oltean 
528aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
529aaa270c6SVladimir Oltean 	vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
530aaa270c6SVladimir Oltean }
531aaa270c6SVladimir Oltean 
5328aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
5338aa9ebccSVladimir Oltean {
5348aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
535511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
536511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
5378aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
5385f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
5395f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
5405f06c63bSVladimir Oltean 		 */
54108fde09aSVladimir Oltean 		.hostprio = 7,
5428aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
5438aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
54442824463SVladimir Oltean 		.incl_srcpt1 = false,
5458aa9ebccSVladimir Oltean 		.send_meta1  = false,
5468aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
5478aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
54842824463SVladimir Oltean 		.incl_srcpt0 = false,
5498aa9ebccSVladimir Oltean 		.send_meta0  = false,
5508aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
5518aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
5528aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
5538aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
5548aa9ebccSVladimir Oltean 		 */
555df2a81a3SVladimir Oltean 		.host_port = priv->ds->num_ports,
556511e6ca0SVladimir Oltean 		/* Default to an invalid value */
557542043e9SVladimir Oltean 		.mirr_port = priv->ds->num_ports,
5588aa9ebccSVladimir Oltean 		/* Link-local traffic received on casc_port will be forwarded
5598aa9ebccSVladimir Oltean 		 * to host_port without embedding the source port and device ID
5608aa9ebccSVladimir Oltean 		 * info in the destination MAC address (presumably because it
5618aa9ebccSVladimir Oltean 		 * is a cascaded port and a downstream SJA switch already did
5628aa9ebccSVladimir Oltean 		 * that). Default to an invalid port (to disable the feature)
5638aa9ebccSVladimir Oltean 		 * and overwrite this if we find any DSA (cascaded) ports.
5648aa9ebccSVladimir Oltean 		 */
565542043e9SVladimir Oltean 		.casc_port = priv->ds->num_ports,
5668aa9ebccSVladimir Oltean 		/* No TTEthernet */
567dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
5688aa9ebccSVladimir Oltean 		.vlmarker = 0,
5698aa9ebccSVladimir Oltean 		.vlmask = 0,
5708aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
5718aa9ebccSVladimir Oltean 		.ignore2stf = 0,
5726666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
5736666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
5746666cebcSVladimir Oltean 		 */
5756666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
5766666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
5778aa9ebccSVladimir Oltean 	};
578df2a81a3SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
5798aa9ebccSVladimir Oltean 	struct sja1105_table *table;
580df2a81a3SVladimir Oltean 	int port;
581df2a81a3SVladimir Oltean 
582df2a81a3SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
583df2a81a3SVladimir Oltean 		if (dsa_is_cpu_port(ds, port)) {
584df2a81a3SVladimir Oltean 			default_general_params.host_port = port;
585df2a81a3SVladimir Oltean 			break;
586df2a81a3SVladimir Oltean 		}
587df2a81a3SVladimir Oltean 	}
5888aa9ebccSVladimir Oltean 
5898aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
5908aa9ebccSVladimir Oltean 
5918aa9ebccSVladimir Oltean 	if (table->entry_count) {
5928aa9ebccSVladimir Oltean 		kfree(table->entries);
5938aa9ebccSVladimir Oltean 		table->entry_count = 0;
5948aa9ebccSVladimir Oltean 	}
5958aa9ebccSVladimir Oltean 
596fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
5978aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5988aa9ebccSVladimir Oltean 	if (!table->entries)
5998aa9ebccSVladimir Oltean 		return -ENOMEM;
6008aa9ebccSVladimir Oltean 
601fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
6028aa9ebccSVladimir Oltean 
6038aa9ebccSVladimir Oltean 	/* This table only has a single entry */
6048aa9ebccSVladimir Oltean 	((struct sja1105_general_params_entry *)table->entries)[0] =
6058aa9ebccSVladimir Oltean 				default_general_params;
6068aa9ebccSVladimir Oltean 
6078aa9ebccSVladimir Oltean 	return 0;
6088aa9ebccSVladimir Oltean }
6098aa9ebccSVladimir Oltean 
61079d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
61179d5511cSVladimir Oltean {
61279d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
61379d5511cSVladimir Oltean 	struct sja1105_table *table;
61479d5511cSVladimir Oltean 
61579d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
61679d5511cSVladimir Oltean 
61779d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
61879d5511cSVladimir Oltean 	if (table->entry_count) {
61979d5511cSVladimir Oltean 		kfree(table->entries);
62079d5511cSVladimir Oltean 		table->entry_count = 0;
62179d5511cSVladimir Oltean 	}
62279d5511cSVladimir Oltean 
623fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
62479d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
62579d5511cSVladimir Oltean 	if (!table->entries)
62679d5511cSVladimir Oltean 		return -ENOMEM;
62779d5511cSVladimir Oltean 
628fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
62979d5511cSVladimir Oltean 
63079d5511cSVladimir Oltean 	avb = table->entries;
63179d5511cSVladimir Oltean 
63279d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
63379d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
63479d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
635747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
636747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
637747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
638747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
639747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
640747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
641747e5eb3SVladimir Oltean 	 */
642747e5eb3SVladimir Oltean 	avb->cas_master = false;
64379d5511cSVladimir Oltean 
64479d5511cSVladimir Oltean 	return 0;
64579d5511cSVladimir Oltean }
64679d5511cSVladimir Oltean 
647a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
648a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
649a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
650a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
651a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
652a7cc081cSVladimir Oltean  * will be used for this frame.
653a7cc081cSVladimir Oltean  *
654a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
655a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
656a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
657a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
658a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
659a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
660a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
661a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
662a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
663a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
664a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
665a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
666a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
667a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
668a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
669a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
670a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
671a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
672a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
673a7cc081cSVladimir Oltean  * +------------+--------+
674a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
675a7cc081cSVladimir Oltean  * +------------+--------+
676a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
677a7cc081cSVladimir Oltean  * +------------+--------+
678a7cc081cSVladimir Oltean  *    ...                                  ...
679a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
680a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
681a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
682a7cc081cSVladimir Oltean  *
683a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
684a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
685a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
686a7cc081cSVladimir Oltean  * lookup) equal.
687a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
688a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
689a7cc081cSVladimir Oltean  */
6908aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
6918aa9ebccSVladimir Oltean 
6928aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
6938aa9ebccSVladimir Oltean {
6948aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
695542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
6968aa9ebccSVladimir Oltean 	struct sja1105_table *table;
697a7cc081cSVladimir Oltean 	int port, tc;
6988aa9ebccSVladimir Oltean 
6998aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
7008aa9ebccSVladimir Oltean 
7018aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
7028aa9ebccSVladimir Oltean 	if (table->entry_count) {
7038aa9ebccSVladimir Oltean 		kfree(table->entries);
7048aa9ebccSVladimir Oltean 		table->entry_count = 0;
7058aa9ebccSVladimir Oltean 	}
7068aa9ebccSVladimir Oltean 
707fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
7088aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
7098aa9ebccSVladimir Oltean 	if (!table->entries)
7108aa9ebccSVladimir Oltean 		return -ENOMEM;
7118aa9ebccSVladimir Oltean 
712fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
7138aa9ebccSVladimir Oltean 
7148aa9ebccSVladimir Oltean 	policing = table->entries;
7158aa9ebccSVladimir Oltean 
716a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
717542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
71838fbe91fSVladimir Oltean 		int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port;
719542043e9SVladimir Oltean 		int bcast = (ds->num_ports * SJA1105_NUM_TC) + port;
720a7cc081cSVladimir Oltean 
721a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
722a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
723a7cc081cSVladimir Oltean 
724a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
72538fbe91fSVladimir Oltean 		/* Only SJA1110 has multicast policers */
72638fbe91fSVladimir Oltean 		if (mcast <= table->ops->max_entry_count)
72738fbe91fSVladimir Oltean 			policing[mcast].sharindx = port;
728a7cc081cSVladimir Oltean 	}
729a7cc081cSVladimir Oltean 
730a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
731542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
732c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
733c279c726SVladimir Oltean 
734a7cc081cSVladimir Oltean 		if (dsa_is_cpu_port(priv->ds, port))
735c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
7368aa9ebccSVladimir Oltean 
737a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
738a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
739a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
740a7cc081cSVladimir Oltean 		policing[port].partition = 0;
7418aa9ebccSVladimir Oltean 	}
742a7cc081cSVladimir Oltean 
7438aa9ebccSVladimir Oltean 	return 0;
7448aa9ebccSVladimir Oltean }
7458aa9ebccSVladimir Oltean 
7468aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv,
7478aa9ebccSVladimir Oltean 				      struct sja1105_dt_port *ports)
7488aa9ebccSVladimir Oltean {
7498aa9ebccSVladimir Oltean 	int rc;
7508aa9ebccSVladimir Oltean 
7518aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
7528aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
7538aa9ebccSVladimir Oltean 					priv->info->static_ops,
7548aa9ebccSVladimir Oltean 					priv->info->device_id);
7558aa9ebccSVladimir Oltean 	if (rc)
7568aa9ebccSVladimir Oltean 		return rc;
7578aa9ebccSVladimir Oltean 
7588aa9ebccSVladimir Oltean 	/* Build static configuration */
7598aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
7608aa9ebccSVladimir Oltean 	if (rc < 0)
7618aa9ebccSVladimir Oltean 		return rc;
7628aa9ebccSVladimir Oltean 	rc = sja1105_init_mii_settings(priv, ports);
7638aa9ebccSVladimir Oltean 	if (rc < 0)
7648aa9ebccSVladimir Oltean 		return rc;
7658aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
7668aa9ebccSVladimir Oltean 	if (rc < 0)
7678aa9ebccSVladimir Oltean 		return rc;
7688aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
7698aa9ebccSVladimir Oltean 	if (rc < 0)
7708aa9ebccSVladimir Oltean 		return rc;
7718aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
7728aa9ebccSVladimir Oltean 	if (rc < 0)
7738aa9ebccSVladimir Oltean 		return rc;
7748aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
7758aa9ebccSVladimir Oltean 	if (rc < 0)
7768aa9ebccSVladimir Oltean 		return rc;
7778aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
7788aa9ebccSVladimir Oltean 	if (rc < 0)
7798aa9ebccSVladimir Oltean 		return rc;
7808aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
7818aa9ebccSVladimir Oltean 	if (rc < 0)
7828aa9ebccSVladimir Oltean 		return rc;
7838aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
7848aa9ebccSVladimir Oltean 	if (rc < 0)
7858aa9ebccSVladimir Oltean 		return rc;
78679d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
78779d5511cSVladimir Oltean 	if (rc < 0)
78879d5511cSVladimir Oltean 		return rc;
7898aa9ebccSVladimir Oltean 
7908aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
7918aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
7928aa9ebccSVladimir Oltean }
7938aa9ebccSVladimir Oltean 
794f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
795f5b8631cSVladimir Oltean 				      const struct sja1105_dt_port *ports)
796f5b8631cSVladimir Oltean {
797542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
798f5b8631cSVladimir Oltean 	int i;
799f5b8631cSVladimir Oltean 
800542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
8019bca3a0aSOleksij Rempel 		if (ports[i].role == XMII_MAC)
802f5b8631cSVladimir Oltean 			continue;
803f5b8631cSVladimir Oltean 
8049bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
8059bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
806f5b8631cSVladimir Oltean 			priv->rgmii_rx_delay[i] = true;
807f5b8631cSVladimir Oltean 
8089bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
8099bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
810f5b8631cSVladimir Oltean 			priv->rgmii_tx_delay[i] = true;
811f5b8631cSVladimir Oltean 
812f5b8631cSVladimir Oltean 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
813f5b8631cSVladimir Oltean 		     !priv->info->setup_rgmii_delay)
814f5b8631cSVladimir Oltean 			return -EINVAL;
815f5b8631cSVladimir Oltean 	}
816f5b8631cSVladimir Oltean 	return 0;
817f5b8631cSVladimir Oltean }
818f5b8631cSVladimir Oltean 
8198aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
8208aa9ebccSVladimir Oltean 				    struct sja1105_dt_port *ports,
8218aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
8228aa9ebccSVladimir Oltean {
8238aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
8248aa9ebccSVladimir Oltean 	struct device_node *child;
8258aa9ebccSVladimir Oltean 
82627afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
8278aa9ebccSVladimir Oltean 		struct device_node *phy_node;
8280c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
8298aa9ebccSVladimir Oltean 		u32 index;
8300c65b2b9SAndrew Lunn 		int err;
8318aa9ebccSVladimir Oltean 
8328aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
8338aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
8348aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
8358aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
8367ba771e3SNishka Dasgupta 			of_node_put(child);
8378aa9ebccSVladimir Oltean 			return -ENODEV;
8388aa9ebccSVladimir Oltean 		}
8398aa9ebccSVladimir Oltean 
8408aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
8410c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
8420c65b2b9SAndrew Lunn 		if (err) {
8438aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
8448aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
8458aa9ebccSVladimir Oltean 				index);
8467ba771e3SNishka Dasgupta 			of_node_put(child);
8478aa9ebccSVladimir Oltean 			return -ENODEV;
8488aa9ebccSVladimir Oltean 		}
8498aa9ebccSVladimir Oltean 		ports[index].phy_mode = phy_mode;
8508aa9ebccSVladimir Oltean 
8518aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
8528aa9ebccSVladimir Oltean 		if (!phy_node) {
8538aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
8548aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
8558aa9ebccSVladimir Oltean 					"properties missing!\n");
8567ba771e3SNishka Dasgupta 				of_node_put(child);
8578aa9ebccSVladimir Oltean 				return -ENODEV;
8588aa9ebccSVladimir Oltean 			}
8598aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
8608aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
8618aa9ebccSVladimir Oltean 			 */
8628aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
8638aa9ebccSVladimir Oltean 		} else {
8648aa9ebccSVladimir Oltean 			/* phy-handle present => put port in MAC role */
8658aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
8668aa9ebccSVladimir Oltean 			of_node_put(phy_node);
8678aa9ebccSVladimir Oltean 		}
8688aa9ebccSVladimir Oltean 
8698aa9ebccSVladimir Oltean 		/* The MAC/PHY role can be overridden with explicit bindings */
8708aa9ebccSVladimir Oltean 		if (of_property_read_bool(child, "sja1105,role-mac"))
8718aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
8728aa9ebccSVladimir Oltean 		else if (of_property_read_bool(child, "sja1105,role-phy"))
8738aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
874*bf4edf4aSVladimir Oltean 
875*bf4edf4aSVladimir Oltean 		priv->phy_mode[index] = phy_mode;
8768aa9ebccSVladimir Oltean 	}
8778aa9ebccSVladimir Oltean 
8788aa9ebccSVladimir Oltean 	return 0;
8798aa9ebccSVladimir Oltean }
8808aa9ebccSVladimir Oltean 
8818aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv,
8828aa9ebccSVladimir Oltean 			    struct sja1105_dt_port *ports)
8838aa9ebccSVladimir Oltean {
8848aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
8858aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
8868aa9ebccSVladimir Oltean 	struct device_node *ports_node;
8878aa9ebccSVladimir Oltean 	int rc;
8888aa9ebccSVladimir Oltean 
8898aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
89015074a36SVladimir Oltean 	if (!ports_node)
89115074a36SVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
8928aa9ebccSVladimir Oltean 	if (!ports_node) {
8938aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
8948aa9ebccSVladimir Oltean 		return -ENODEV;
8958aa9ebccSVladimir Oltean 	}
8968aa9ebccSVladimir Oltean 
8978aa9ebccSVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
8988aa9ebccSVladimir Oltean 	of_node_put(ports_node);
8998aa9ebccSVladimir Oltean 
9008aa9ebccSVladimir Oltean 	return rc;
9018aa9ebccSVladimir Oltean }
9028aa9ebccSVladimir Oltean 
9034c7ee010SVladimir Oltean static int sja1105_sgmii_read(struct sja1105_private *priv, int port, int mmd,
90484db00f2SVladimir Oltean 			      int pcs_reg)
905ffe10e67SVladimir Oltean {
9064c7ee010SVladimir Oltean 	u64 addr = (mmd << 16) | pcs_reg;
907ffe10e67SVladimir Oltean 	u32 val;
908ffe10e67SVladimir Oltean 	int rc;
909ffe10e67SVladimir Oltean 
91084db00f2SVladimir Oltean 	if (port != SJA1105_SGMII_PORT)
91184db00f2SVladimir Oltean 		return -ENODEV;
91284db00f2SVladimir Oltean 
9134c7ee010SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, addr, &val, NULL);
914ffe10e67SVladimir Oltean 	if (rc < 0)
915ffe10e67SVladimir Oltean 		return rc;
916ffe10e67SVladimir Oltean 
917ffe10e67SVladimir Oltean 	return val;
918ffe10e67SVladimir Oltean }
919ffe10e67SVladimir Oltean 
9204c7ee010SVladimir Oltean static int sja1105_sgmii_write(struct sja1105_private *priv, int port, int mmd,
92184db00f2SVladimir Oltean 			       int pcs_reg, u16 pcs_val)
922ffe10e67SVladimir Oltean {
9234c7ee010SVladimir Oltean 	u64 addr = (mmd << 16) | pcs_reg;
924ffe10e67SVladimir Oltean 	u32 val = pcs_val;
925ffe10e67SVladimir Oltean 	int rc;
926ffe10e67SVladimir Oltean 
92784db00f2SVladimir Oltean 	if (port != SJA1105_SGMII_PORT)
92884db00f2SVladimir Oltean 		return -ENODEV;
92984db00f2SVladimir Oltean 
9304c7ee010SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_WRITE, addr, &val, NULL);
931ffe10e67SVladimir Oltean 	if (rc < 0)
932ffe10e67SVladimir Oltean 		return rc;
933ffe10e67SVladimir Oltean 
934ffe10e67SVladimir Oltean 	return val;
935ffe10e67SVladimir Oltean }
936ffe10e67SVladimir Oltean 
93784db00f2SVladimir Oltean static void sja1105_sgmii_pcs_config(struct sja1105_private *priv, int port,
938ffe10e67SVladimir Oltean 				     bool an_enabled, bool an_master)
939ffe10e67SVladimir Oltean {
940ffe10e67SVladimir Oltean 	u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII;
941ffe10e67SVladimir Oltean 
942ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
943ffe10e67SVladimir Oltean 	 * stop the clock during LPI mode, make the MAC reconfigure
944ffe10e67SVladimir Oltean 	 * autonomously after PCS autoneg is done, flush the internal FIFOs.
945ffe10e67SVladimir Oltean 	 */
9464c7ee010SVladimir Oltean 	sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_DC1,
94784db00f2SVladimir Oltean 			    SJA1105_DC1_EN_VSMMD1 |
948ffe10e67SVladimir Oltean 			    SJA1105_DC1_CLOCK_STOP_EN |
949ffe10e67SVladimir Oltean 			    SJA1105_DC1_MAC_AUTO_SW |
950ffe10e67SVladimir Oltean 			    SJA1105_DC1_INIT);
951ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
9524c7ee010SVladimir Oltean 	sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_DC2,
95384db00f2SVladimir Oltean 			    SJA1105_DC2_TX_POL_INV_DISABLE);
954ffe10e67SVladimir Oltean 	/* AUTONEG_CONTROL: Use SGMII autoneg */
955ffe10e67SVladimir Oltean 	if (an_master)
956ffe10e67SVladimir Oltean 		ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK;
9574c7ee010SVladimir Oltean 	sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_AC, ac);
958ffe10e67SVladimir Oltean 	/* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
959ffe10e67SVladimir Oltean 	 * sja1105_sgmii_pcs_force_speed must be called later for the link
960ffe10e67SVladimir Oltean 	 * to become operational.
961ffe10e67SVladimir Oltean 	 */
962ffe10e67SVladimir Oltean 	if (an_enabled)
9634c7ee010SVladimir Oltean 		sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1,
964ffe10e67SVladimir Oltean 				    BMCR_ANENABLE | BMCR_ANRESTART);
965ffe10e67SVladimir Oltean }
966ffe10e67SVladimir Oltean 
967ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv,
96884db00f2SVladimir Oltean 					  int port, int speed)
969ffe10e67SVladimir Oltean {
970ffe10e67SVladimir Oltean 	int pcs_speed;
971ffe10e67SVladimir Oltean 
972ffe10e67SVladimir Oltean 	switch (speed) {
973ffe10e67SVladimir Oltean 	case SPEED_1000:
974ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED1000;
975ffe10e67SVladimir Oltean 		break;
976ffe10e67SVladimir Oltean 	case SPEED_100:
977ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED100;
978ffe10e67SVladimir Oltean 		break;
979ffe10e67SVladimir Oltean 	case SPEED_10:
980ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED10;
981ffe10e67SVladimir Oltean 		break;
982ffe10e67SVladimir Oltean 	default:
983ffe10e67SVladimir Oltean 		dev_err(priv->ds->dev, "Invalid speed %d\n", speed);
984ffe10e67SVladimir Oltean 		return;
985ffe10e67SVladimir Oltean 	}
9864c7ee010SVladimir Oltean 	sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1,
9874c7ee010SVladimir Oltean 			    pcs_speed | BMCR_FULLDPLX);
988ffe10e67SVladimir Oltean }
989ffe10e67SVladimir Oltean 
990c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
9918aa9ebccSVladimir Oltean static int sja1105_speed[] = {
992c44d0535SVladimir Oltean 	[SJA1105_SPEED_AUTO]		= SPEED_UNKNOWN,
993c44d0535SVladimir Oltean 	[SJA1105_SPEED_10MBPS]		= SPEED_10,
994c44d0535SVladimir Oltean 	[SJA1105_SPEED_100MBPS]		= SPEED_100,
995c44d0535SVladimir Oltean 	[SJA1105_SPEED_1000MBPS]	= SPEED_1000,
9968aa9ebccSVladimir Oltean };
9978aa9ebccSVladimir Oltean 
9988400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
9998aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
10008400cff6SVladimir Oltean 				      int speed_mbps)
10018aa9ebccSVladimir Oltean {
10028aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
10038aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
10048aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
10058aa9ebccSVladimir Oltean 	sja1105_phy_interface_t phy_mode;
10068aa9ebccSVladimir Oltean 	sja1105_speed_t speed;
10078aa9ebccSVladimir Oltean 	int rc;
10088aa9ebccSVladimir Oltean 
10098400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
10108400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
10118400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
10128400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
10138400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
10148400cff6SVladimir Oltean 	 */
10158aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
10168400cff6SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
10178aa9ebccSVladimir Oltean 
1018f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
1019c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
1020a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
1021a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
1022a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
1023a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
1024a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
1025a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
1026a979a0abSVladimir Oltean 		 */
1027f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_AUTO;
1028f4cfcfbdSVladimir Oltean 		break;
1029c44d0535SVladimir Oltean 	case SPEED_10:
1030f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_10MBPS;
1031f4cfcfbdSVladimir Oltean 		break;
1032c44d0535SVladimir Oltean 	case SPEED_100:
1033f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_100MBPS;
1034f4cfcfbdSVladimir Oltean 		break;
1035c44d0535SVladimir Oltean 	case SPEED_1000:
1036f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_1000MBPS;
1037f4cfcfbdSVladimir Oltean 		break;
1038f4cfcfbdSVladimir Oltean 	default:
10398aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
10408aa9ebccSVladimir Oltean 		return -EINVAL;
10418aa9ebccSVladimir Oltean 	}
10428aa9ebccSVladimir Oltean 
10438400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
10448400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
10458400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
10468400cff6SVladimir Oltean 	 * we want auto during upload phase).
1047ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
1048ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
10498aa9ebccSVladimir Oltean 	 */
1050ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port))
1051ffe10e67SVladimir Oltean 		mac[port].speed = SJA1105_SPEED_1000MBPS;
1052ffe10e67SVladimir Oltean 	else
10538aa9ebccSVladimir Oltean 		mac[port].speed = speed;
10548aa9ebccSVladimir Oltean 
10558aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
10568400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
10578400cff6SVladimir Oltean 					  &mac[port], true);
10588aa9ebccSVladimir Oltean 	if (rc < 0) {
10598aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
10608aa9ebccSVladimir Oltean 		return rc;
10618aa9ebccSVladimir Oltean 	}
10628aa9ebccSVladimir Oltean 
10638aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
10648aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
10658aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
10668aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
10678aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
10688aa9ebccSVladimir Oltean 	 */
10698aa9ebccSVladimir Oltean 	phy_mode = mii->xmii_mode[port];
10708aa9ebccSVladimir Oltean 	if (phy_mode != XMII_MODE_RGMII)
10718aa9ebccSVladimir Oltean 		return 0;
10728aa9ebccSVladimir Oltean 
10738aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
10748aa9ebccSVladimir Oltean }
10758aa9ebccSVladimir Oltean 
107639710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
107739710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
107839710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
107939710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
108039710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
108139710229SVladimir Oltean  * now.
108239710229SVladimir Oltean  */
108339710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
108439710229SVladimir Oltean 				      phy_interface_t interface)
108539710229SVladimir Oltean {
1086*bf4edf4aSVladimir Oltean 	return priv->phy_mode[port] != interface;
108739710229SVladimir Oltean }
108839710229SVladimir Oltean 
1089af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
1090ffe10e67SVladimir Oltean 			       unsigned int mode,
1091af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
10928aa9ebccSVladimir Oltean {
10938aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1094ffe10e67SVladimir Oltean 	bool is_sgmii = sja1105_supports_sgmii(priv, port);
10958aa9ebccSVladimir Oltean 
1096ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1097ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
1098ec8582d1SVladimir Oltean 			phy_modes(state->interface));
109939710229SVladimir Oltean 		return;
1100ec8582d1SVladimir Oltean 	}
110139710229SVladimir Oltean 
1102ffe10e67SVladimir Oltean 	if (phylink_autoneg_inband(mode) && !is_sgmii) {
11039f971573SVladimir Oltean 		dev_err(ds->dev, "In-band AN not supported!\n");
11049f971573SVladimir Oltean 		return;
11059f971573SVladimir Oltean 	}
1106ffe10e67SVladimir Oltean 
1107ffe10e67SVladimir Oltean 	if (is_sgmii)
110884db00f2SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, port,
110984db00f2SVladimir Oltean 					 phylink_autoneg_inband(mode),
1110ffe10e67SVladimir Oltean 					 false);
11118400cff6SVladimir Oltean }
11128400cff6SVladimir Oltean 
11138400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
11148400cff6SVladimir Oltean 				  unsigned int mode,
11158400cff6SVladimir Oltean 				  phy_interface_t interface)
11168400cff6SVladimir Oltean {
11178400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
11188400cff6SVladimir Oltean }
11198400cff6SVladimir Oltean 
11208400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
11218400cff6SVladimir Oltean 				unsigned int mode,
11228400cff6SVladimir Oltean 				phy_interface_t interface,
11235b502a7bSRussell King 				struct phy_device *phydev,
11245b502a7bSRussell King 				int speed, int duplex,
11255b502a7bSRussell King 				bool tx_pause, bool rx_pause)
11268400cff6SVladimir Oltean {
1127ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1128ec8582d1SVladimir Oltean 
1129ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1130ec8582d1SVladimir Oltean 
1131ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode))
113284db00f2SVladimir Oltean 		sja1105_sgmii_pcs_force_speed(priv, port, speed);
1133ffe10e67SVladimir Oltean 
1134ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
11358aa9ebccSVladimir Oltean }
11368aa9ebccSVladimir Oltean 
1137ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1138ad9f299aSVladimir Oltean 				     unsigned long *supported,
1139ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
1140ad9f299aSVladimir Oltean {
1141ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
1142ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
1143ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
1144ad9f299aSVladimir Oltean 	 */
1145ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1146ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1147ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1148ad9f299aSVladimir Oltean 
1149ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1150ad9f299aSVladimir Oltean 
115139710229SVladimir Oltean 	/* include/linux/phylink.h says:
115239710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
115339710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
115439710229SVladimir Oltean 	 */
115539710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
115639710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
115739710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
115839710229SVladimir Oltean 		return;
115939710229SVladimir Oltean 	}
116039710229SVladimir Oltean 
1161ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1162ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1163ad9f299aSVladimir Oltean 	 */
1164ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
1165ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
1166ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
1167ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
1168ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
1169ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1170ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1171ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
1172ad9f299aSVladimir Oltean 
1173ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1174ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1175ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1176ad9f299aSVladimir Oltean }
1177ad9f299aSVladimir Oltean 
1178ffe10e67SVladimir Oltean static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port,
1179ffe10e67SVladimir Oltean 				     struct phylink_link_state *state)
1180ffe10e67SVladimir Oltean {
1181ffe10e67SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1182ffe10e67SVladimir Oltean 	int ais;
1183ffe10e67SVladimir Oltean 
1184ffe10e67SVladimir Oltean 	/* Read the vendor-specific AUTONEG_INTR_STATUS register */
11854c7ee010SVladimir Oltean 	ais = sja1105_sgmii_read(priv, port, MDIO_MMD_VEND2, SJA1105_AIS);
1186ffe10e67SVladimir Oltean 	if (ais < 0)
1187ffe10e67SVladimir Oltean 		return ais;
1188ffe10e67SVladimir Oltean 
1189ffe10e67SVladimir Oltean 	switch (SJA1105_AIS_SPEED(ais)) {
1190ffe10e67SVladimir Oltean 	case 0:
1191ffe10e67SVladimir Oltean 		state->speed = SPEED_10;
1192ffe10e67SVladimir Oltean 		break;
1193ffe10e67SVladimir Oltean 	case 1:
1194ffe10e67SVladimir Oltean 		state->speed = SPEED_100;
1195ffe10e67SVladimir Oltean 		break;
1196ffe10e67SVladimir Oltean 	case 2:
1197ffe10e67SVladimir Oltean 		state->speed = SPEED_1000;
1198ffe10e67SVladimir Oltean 		break;
1199ffe10e67SVladimir Oltean 	default:
1200ffe10e67SVladimir Oltean 		dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n",
1201ffe10e67SVladimir Oltean 			SJA1105_AIS_SPEED(ais));
1202ffe10e67SVladimir Oltean 	}
1203ffe10e67SVladimir Oltean 	state->duplex = SJA1105_AIS_DUPLEX_MODE(ais);
1204ffe10e67SVladimir Oltean 	state->an_complete = SJA1105_AIS_COMPLETE(ais);
1205ffe10e67SVladimir Oltean 	state->link = SJA1105_AIS_LINK_STATUS(ais);
1206ffe10e67SVladimir Oltean 
1207ffe10e67SVladimir Oltean 	return 0;
1208ffe10e67SVladimir Oltean }
1209ffe10e67SVladimir Oltean 
121060f6053fSVladimir Oltean static int
121160f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
121260f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
121360f6053fSVladimir Oltean {
121460f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
121560f6053fSVladimir Oltean 	struct sja1105_table *table;
121660f6053fSVladimir Oltean 	int i;
121760f6053fSVladimir Oltean 
121860f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
121960f6053fSVladimir Oltean 	l2_lookup = table->entries;
122060f6053fSVladimir Oltean 
122160f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
122260f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
122360f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
122460f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
122560f6053fSVladimir Oltean 			return i;
122660f6053fSVladimir Oltean 
122760f6053fSVladimir Oltean 	return -1;
122860f6053fSVladimir Oltean }
122960f6053fSVladimir Oltean 
123060f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
123160f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
123260f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
123360f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
123460f6053fSVladimir Oltean  */
123560f6053fSVladimir Oltean static int
123660f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
123760f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
123860f6053fSVladimir Oltean 			  bool keep)
123960f6053fSVladimir Oltean {
124060f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
124160f6053fSVladimir Oltean 	struct sja1105_table *table;
124260f6053fSVladimir Oltean 	int rc, match;
124360f6053fSVladimir Oltean 
124460f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
124560f6053fSVladimir Oltean 
124660f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
124760f6053fSVladimir Oltean 	if (match < 0) {
124860f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
124960f6053fSVladimir Oltean 		if (!keep)
125060f6053fSVladimir Oltean 			return 0;
125160f6053fSVladimir Oltean 
125260f6053fSVladimir Oltean 		/* No match => new entry */
125360f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
125460f6053fSVladimir Oltean 		if (rc)
125560f6053fSVladimir Oltean 			return rc;
125660f6053fSVladimir Oltean 
125760f6053fSVladimir Oltean 		match = table->entry_count - 1;
125860f6053fSVladimir Oltean 	}
125960f6053fSVladimir Oltean 
126060f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
126160f6053fSVladimir Oltean 	l2_lookup = table->entries;
126260f6053fSVladimir Oltean 
126360f6053fSVladimir Oltean 	/* We have a match.
126460f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
126560f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
126660f6053fSVladimir Oltean 	 * which we update it).
126760f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
126860f6053fSVladimir Oltean 	 */
126960f6053fSVladimir Oltean 	if (keep) {
127060f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
127160f6053fSVladimir Oltean 		return 0;
127260f6053fSVladimir Oltean 	}
127360f6053fSVladimir Oltean 
127460f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
127560f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
127660f6053fSVladimir Oltean 	 */
127760f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
127860f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
127960f6053fSVladimir Oltean }
128060f6053fSVladimir Oltean 
1281291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1282291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1283291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1284291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1285291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1286291d1e72SVladimir Oltean  */
128709c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1288291d1e72SVladimir Oltean {
1289291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1290291d1e72SVladimir Oltean }
1291291d1e72SVladimir Oltean 
12929dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1293291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1294291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1295291d1e72SVladimir Oltean 					 int *last_unused)
1296291d1e72SVladimir Oltean {
1297291d1e72SVladimir Oltean 	int way;
1298291d1e72SVladimir Oltean 
1299291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1300291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1301291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1302291d1e72SVladimir Oltean 
1303291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1304291d1e72SVladimir Oltean 		 * into the return value
1305291d1e72SVladimir Oltean 		 */
1306291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1307291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1308291d1e72SVladimir Oltean 			if (last_unused)
1309291d1e72SVladimir Oltean 				*last_unused = way;
1310291d1e72SVladimir Oltean 			continue;
1311291d1e72SVladimir Oltean 		}
1312291d1e72SVladimir Oltean 
1313291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1314291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1315291d1e72SVladimir Oltean 			if (match)
1316291d1e72SVladimir Oltean 				*match = l2_lookup;
1317291d1e72SVladimir Oltean 			return way;
1318291d1e72SVladimir Oltean 		}
1319291d1e72SVladimir Oltean 	}
1320291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1321291d1e72SVladimir Oltean 	return -1;
1322291d1e72SVladimir Oltean }
1323291d1e72SVladimir Oltean 
13249dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1325291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1326291d1e72SVladimir Oltean {
1327291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1328291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1329291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1330291d1e72SVladimir Oltean 	int last_unused = -1;
133160f6053fSVladimir Oltean 	int bin, way, rc;
1332291d1e72SVladimir Oltean 
13339dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1334291d1e72SVladimir Oltean 
13359dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1336291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1337291d1e72SVladimir Oltean 	if (way >= 0) {
1338291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1339291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1340291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1341291d1e72SVladimir Oltean 		 */
1342291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1343291d1e72SVladimir Oltean 			return 0;
1344291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1345291d1e72SVladimir Oltean 	} else {
1346291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1347291d1e72SVladimir Oltean 
1348291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1349291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1350291d1e72SVladimir Oltean 		 */
1351291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1352291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1353291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1354291d1e72SVladimir Oltean 
1355291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1356291d1e72SVladimir Oltean 			way = last_unused;
1357291d1e72SVladimir Oltean 		} else {
1358291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1359291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1360291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1361291d1e72SVladimir Oltean 			 * distribution function:
1362291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1363291d1e72SVladimir Oltean 			 */
1364291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1365291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1366291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1367291d1e72SVladimir Oltean 				 bin, addr, way);
1368291d1e72SVladimir Oltean 			/* Evict entry */
1369291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1370291d1e72SVladimir Oltean 						     index, NULL, false);
1371291d1e72SVladimir Oltean 		}
1372291d1e72SVladimir Oltean 	}
1373291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1374291d1e72SVladimir Oltean 
137560f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1376291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1377291d1e72SVladimir Oltean 					  true);
137860f6053fSVladimir Oltean 	if (rc < 0)
137960f6053fSVladimir Oltean 		return rc;
138060f6053fSVladimir Oltean 
138160f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1382291d1e72SVladimir Oltean }
1383291d1e72SVladimir Oltean 
13849dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1385291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1386291d1e72SVladimir Oltean {
1387291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1388291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
138960f6053fSVladimir Oltean 	int index, bin, way, rc;
1390291d1e72SVladimir Oltean 	bool keep;
1391291d1e72SVladimir Oltean 
13929dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
13939dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1394291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1395291d1e72SVladimir Oltean 	if (way < 0)
1396291d1e72SVladimir Oltean 		return 0;
1397291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1398291d1e72SVladimir Oltean 
1399291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1400291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1401291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1402291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1403291d1e72SVladimir Oltean 	 */
1404291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
14057752e937SVladimir Oltean 
1406291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1407291d1e72SVladimir Oltean 		keep = true;
1408291d1e72SVladimir Oltean 	else
1409291d1e72SVladimir Oltean 		keep = false;
1410291d1e72SVladimir Oltean 
141160f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1412291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
141360f6053fSVladimir Oltean 	if (rc < 0)
141460f6053fSVladimir Oltean 		return rc;
141560f6053fSVladimir Oltean 
141660f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1417291d1e72SVladimir Oltean }
1418291d1e72SVladimir Oltean 
14199dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
14209dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
14219dfa6911SVladimir Oltean {
14221da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
14231da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14241da73821SVladimir Oltean 	int rc, i;
14251da73821SVladimir Oltean 
14261da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
14271da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
14281da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
14291da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
14301da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
14317f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
14321da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
14331da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
14346d7c7d94SVladimir Oltean 	} else {
14356d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
14366d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
14376d7c7d94SVladimir Oltean 	}
14381da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
14391da73821SVladimir Oltean 
14401da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
14411da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
14421da73821SVladimir Oltean 	if (rc == 0) {
14431da73821SVladimir Oltean 		/* Found and this port is already in the entry's
14441da73821SVladimir Oltean 		 * port mask => job done
14451da73821SVladimir Oltean 		 */
14461da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
14471da73821SVladimir Oltean 			return 0;
14481da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
14491da73821SVladimir Oltean 		 * found something.
14501da73821SVladimir Oltean 		 */
14511da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
14521da73821SVladimir Oltean 		goto skip_finding_an_index;
14531da73821SVladimir Oltean 	}
14541da73821SVladimir Oltean 
14551da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
14561da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
14571da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
14581da73821SVladimir Oltean 	 */
14591da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
14601da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
14611da73821SVladimir Oltean 						 i, NULL);
14621da73821SVladimir Oltean 		if (rc < 0)
14631da73821SVladimir Oltean 			break;
14641da73821SVladimir Oltean 	}
14651da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
14661da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
14671da73821SVladimir Oltean 		return -EINVAL;
14681da73821SVladimir Oltean 	}
146917ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
14701da73821SVladimir Oltean 	l2_lookup.index = i;
14711da73821SVladimir Oltean 
14721da73821SVladimir Oltean skip_finding_an_index:
147360f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
14741da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
14751da73821SVladimir Oltean 					  true);
147660f6053fSVladimir Oltean 	if (rc < 0)
147760f6053fSVladimir Oltean 		return rc;
147860f6053fSVladimir Oltean 
147960f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
14809dfa6911SVladimir Oltean }
14819dfa6911SVladimir Oltean 
14829dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
14839dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
14849dfa6911SVladimir Oltean {
14851da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
14861da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14871da73821SVladimir Oltean 	bool keep;
14881da73821SVladimir Oltean 	int rc;
14891da73821SVladimir Oltean 
14901da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
14911da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
14921da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
14931da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
14947f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
14951da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
14961da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
14976d7c7d94SVladimir Oltean 	} else {
14986d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
14996d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
15006d7c7d94SVladimir Oltean 	}
15011da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
15021da73821SVladimir Oltean 
15031da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
15041da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
15051da73821SVladimir Oltean 	if (rc < 0)
15061da73821SVladimir Oltean 		return 0;
15071da73821SVladimir Oltean 
15081da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
15091da73821SVladimir Oltean 
15101da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
15111da73821SVladimir Oltean 	 * or if we remove it completely.
15121da73821SVladimir Oltean 	 */
15131da73821SVladimir Oltean 	if (l2_lookup.destports)
15141da73821SVladimir Oltean 		keep = true;
15151da73821SVladimir Oltean 	else
15161da73821SVladimir Oltean 		keep = false;
15171da73821SVladimir Oltean 
151860f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
15191da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
152060f6053fSVladimir Oltean 	if (rc < 0)
152160f6053fSVladimir Oltean 		return rc;
152260f6053fSVladimir Oltean 
152360f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
15249dfa6911SVladimir Oltean }
15259dfa6911SVladimir Oltean 
15269dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
15279dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
15289dfa6911SVladimir Oltean {
15299dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1530b3ee526aSVladimir Oltean 
15316d7c7d94SVladimir Oltean 	/* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
15326d7c7d94SVladimir Oltean 	 * so the switch still does some VLAN processing internally.
15336d7c7d94SVladimir Oltean 	 * But Shared VLAN Learning (SVL) is also active, and it will take
15346d7c7d94SVladimir Oltean 	 * care of autonomous forwarding between the unique pvid's of each
15356d7c7d94SVladimir Oltean 	 * port.  Here we just make sure that users can't add duplicate FDB
15366d7c7d94SVladimir Oltean 	 * entries when in this mode - the actual VID doesn't matter except
15376d7c7d94SVladimir Oltean 	 * for what gets printed in 'bridge fdb show'.  In the case of zero,
15386d7c7d94SVladimir Oltean 	 * no VID gets printed at all.
153993647594SVladimir Oltean 	 */
15407f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
15416d7c7d94SVladimir Oltean 		vid = 0;
154293647594SVladimir Oltean 
15436d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
15449dfa6911SVladimir Oltean }
15459dfa6911SVladimir Oltean 
15469dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
15479dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
15489dfa6911SVladimir Oltean {
15499dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15509dfa6911SVladimir Oltean 
15517f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
15526d7c7d94SVladimir Oltean 		vid = 0;
15536d7c7d94SVladimir Oltean 
1554b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
15559dfa6911SVladimir Oltean }
15569dfa6911SVladimir Oltean 
1557291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1558291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1559291d1e72SVladimir Oltean {
1560291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1561291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1562291d1e72SVladimir Oltean 	int i;
1563291d1e72SVladimir Oltean 
1564291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1565291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1566291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1567291d1e72SVladimir Oltean 		int rc;
1568291d1e72SVladimir Oltean 
1569291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1570291d1e72SVladimir Oltean 						 i, &l2_lookup);
1571291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1572def84604SVladimir Oltean 		if (rc == -ENOENT)
1573291d1e72SVladimir Oltean 			continue;
1574291d1e72SVladimir Oltean 		if (rc) {
1575291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1576291d1e72SVladimir Oltean 			return rc;
1577291d1e72SVladimir Oltean 		}
1578291d1e72SVladimir Oltean 
1579291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1580291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1581291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1582291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1583291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1584291d1e72SVladimir Oltean 		 */
1585291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1586291d1e72SVladimir Oltean 			continue;
15874d942354SVladimir Oltean 
15884d942354SVladimir Oltean 		/* We need to hide the FDB entry for unknown multicast */
15894d942354SVladimir Oltean 		if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST &&
15904d942354SVladimir Oltean 		    l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
15914d942354SVladimir Oltean 			continue;
15924d942354SVladimir Oltean 
1593291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
159493647594SVladimir Oltean 
15956d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
15967f14937fSVladimir Oltean 		if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
15976d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
159817ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1599291d1e72SVladimir Oltean 	}
1600291d1e72SVladimir Oltean 	return 0;
1601291d1e72SVladimir Oltean }
1602291d1e72SVladimir Oltean 
1603a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port,
1604291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1605291d1e72SVladimir Oltean {
1606a52b2da7SVladimir Oltean 	return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1607291d1e72SVladimir Oltean }
1608291d1e72SVladimir Oltean 
1609291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1610291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1611291d1e72SVladimir Oltean {
1612291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1613291d1e72SVladimir Oltean }
1614291d1e72SVladimir Oltean 
16157f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration.
16167f7ccdeaSVladimir Oltean  * Flooding is configured between each {ingress, egress} port pair, and since
16177f7ccdeaSVladimir Oltean  * the bridge's semantics are those of "egress flooding", it means we must
16187f7ccdeaSVladimir Oltean  * enable flooding towards this port from all ingress ports that are in the
16197f7ccdeaSVladimir Oltean  * same forwarding domain.
16207f7ccdeaSVladimir Oltean  */
16217f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv)
16227f7ccdeaSVladimir Oltean {
16237f7ccdeaSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
16247f7ccdeaSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
16257f7ccdeaSVladimir Oltean 	int from, to, rc;
16267f7ccdeaSVladimir Oltean 
16277f7ccdeaSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
16287f7ccdeaSVladimir Oltean 
16297f7ccdeaSVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
16307f7ccdeaSVladimir Oltean 		u64 fl_domain = 0, bc_domain = 0;
16317f7ccdeaSVladimir Oltean 
16327f7ccdeaSVladimir Oltean 		for (to = 0; to < priv->ds->num_ports; to++) {
16337f7ccdeaSVladimir Oltean 			if (!sja1105_can_forward(l2_fwd, from, to))
16347f7ccdeaSVladimir Oltean 				continue;
16357f7ccdeaSVladimir Oltean 
16367f7ccdeaSVladimir Oltean 			if (priv->ucast_egress_floods & BIT(to))
16377f7ccdeaSVladimir Oltean 				fl_domain |= BIT(to);
16387f7ccdeaSVladimir Oltean 			if (priv->bcast_egress_floods & BIT(to))
16397f7ccdeaSVladimir Oltean 				bc_domain |= BIT(to);
16407f7ccdeaSVladimir Oltean 		}
16417f7ccdeaSVladimir Oltean 
16427f7ccdeaSVladimir Oltean 		/* Nothing changed, nothing to do */
16437f7ccdeaSVladimir Oltean 		if (l2_fwd[from].fl_domain == fl_domain &&
16447f7ccdeaSVladimir Oltean 		    l2_fwd[from].bc_domain == bc_domain)
16457f7ccdeaSVladimir Oltean 			continue;
16467f7ccdeaSVladimir Oltean 
16477f7ccdeaSVladimir Oltean 		l2_fwd[from].fl_domain = fl_domain;
16487f7ccdeaSVladimir Oltean 		l2_fwd[from].bc_domain = bc_domain;
16497f7ccdeaSVladimir Oltean 
16507f7ccdeaSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
16517f7ccdeaSVladimir Oltean 						  from, &l2_fwd[from], true);
16527f7ccdeaSVladimir Oltean 		if (rc < 0)
16537f7ccdeaSVladimir Oltean 			return rc;
16547f7ccdeaSVladimir Oltean 	}
16557f7ccdeaSVladimir Oltean 
16567f7ccdeaSVladimir Oltean 	return 0;
16577f7ccdeaSVladimir Oltean }
16587f7ccdeaSVladimir Oltean 
16598aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
16608aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
16618aa9ebccSVladimir Oltean {
16628aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
16638aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16648aa9ebccSVladimir Oltean 	int i, rc;
16658aa9ebccSVladimir Oltean 
16668aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
16678aa9ebccSVladimir Oltean 
1668542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
16698aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
16708aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
16718aa9ebccSVladimir Oltean 		 */
16728aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
16738aa9ebccSVladimir Oltean 			continue;
16748aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
16758aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
16768aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
16778aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
16788aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
16798aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
16808aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
16818aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
16828aa9ebccSVladimir Oltean 		 */
16838aa9ebccSVladimir Oltean 		if (i == port)
16848aa9ebccSVladimir Oltean 			continue;
16858aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
16868aa9ebccSVladimir Oltean 			continue;
16878aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
16888aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
16898aa9ebccSVladimir Oltean 
16908aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
16918aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
16928aa9ebccSVladimir Oltean 		if (rc < 0)
16938aa9ebccSVladimir Oltean 			return rc;
16948aa9ebccSVladimir Oltean 	}
16958aa9ebccSVladimir Oltean 
16967f7ccdeaSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
16978aa9ebccSVladimir Oltean 					  port, &l2_fwd[port], true);
16987f7ccdeaSVladimir Oltean 	if (rc)
16997f7ccdeaSVladimir Oltean 		return rc;
17007f7ccdeaSVladimir Oltean 
17017f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
17028aa9ebccSVladimir Oltean }
17038aa9ebccSVladimir Oltean 
1704640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1705640f763fSVladimir Oltean 					 u8 state)
1706640f763fSVladimir Oltean {
1707640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1708640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1709640f763fSVladimir Oltean 
1710640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1711640f763fSVladimir Oltean 
1712640f763fSVladimir Oltean 	switch (state) {
1713640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1714640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1715640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1716640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1717640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1718640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1719640f763fSVladimir Oltean 		 */
1720640f763fSVladimir Oltean 		mac[port].ingress   = false;
1721640f763fSVladimir Oltean 		mac[port].egress    = false;
1722640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1723640f763fSVladimir Oltean 		break;
1724640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1725640f763fSVladimir Oltean 		mac[port].ingress   = true;
1726640f763fSVladimir Oltean 		mac[port].egress    = false;
1727640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1728640f763fSVladimir Oltean 		break;
1729640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1730640f763fSVladimir Oltean 		mac[port].ingress   = true;
1731640f763fSVladimir Oltean 		mac[port].egress    = false;
17324d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1733640f763fSVladimir Oltean 		break;
1734640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1735640f763fSVladimir Oltean 		mac[port].ingress   = true;
1736640f763fSVladimir Oltean 		mac[port].egress    = true;
17374d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1738640f763fSVladimir Oltean 		break;
1739640f763fSVladimir Oltean 	default:
1740640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1741640f763fSVladimir Oltean 		return;
1742640f763fSVladimir Oltean 	}
1743640f763fSVladimir Oltean 
1744640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1745640f763fSVladimir Oltean 				     &mac[port], true);
1746640f763fSVladimir Oltean }
1747640f763fSVladimir Oltean 
17488aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
17498aa9ebccSVladimir Oltean 			       struct net_device *br)
17508aa9ebccSVladimir Oltean {
17518aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
17528aa9ebccSVladimir Oltean }
17538aa9ebccSVladimir Oltean 
17548aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
17558aa9ebccSVladimir Oltean 				 struct net_device *br)
17568aa9ebccSVladimir Oltean {
17578aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
17588aa9ebccSVladimir Oltean }
17598aa9ebccSVladimir Oltean 
17604d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8)
17614d752508SVladimir Oltean 
17624d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
17634d752508SVladimir Oltean {
17644d752508SVladimir Oltean 	int i;
17654d752508SVladimir Oltean 
17664d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
17674d752508SVladimir Oltean 		if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
17684d752508SVladimir Oltean 			return i;
17694d752508SVladimir Oltean 
17704d752508SVladimir Oltean 	return -1;
17714d752508SVladimir Oltean }
17724d752508SVladimir Oltean 
17734d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
17744d752508SVladimir Oltean 				     int prio)
17754d752508SVladimir Oltean {
17764d752508SVladimir Oltean 	int i;
17774d752508SVladimir Oltean 
17784d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
17794d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
17804d752508SVladimir Oltean 
17814d752508SVladimir Oltean 		if (cbs->port == port && cbs->prio == prio) {
17824d752508SVladimir Oltean 			memset(cbs, 0, sizeof(*cbs));
17834d752508SVladimir Oltean 			return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
17844d752508SVladimir Oltean 							    i, cbs, true);
17854d752508SVladimir Oltean 		}
17864d752508SVladimir Oltean 	}
17874d752508SVladimir Oltean 
17884d752508SVladimir Oltean 	return 0;
17894d752508SVladimir Oltean }
17904d752508SVladimir Oltean 
17914d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
17924d752508SVladimir Oltean 				struct tc_cbs_qopt_offload *offload)
17934d752508SVladimir Oltean {
17944d752508SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
17954d752508SVladimir Oltean 	struct sja1105_cbs_entry *cbs;
17964d752508SVladimir Oltean 	int index;
17974d752508SVladimir Oltean 
17984d752508SVladimir Oltean 	if (!offload->enable)
17994d752508SVladimir Oltean 		return sja1105_delete_cbs_shaper(priv, port, offload->queue);
18004d752508SVladimir Oltean 
18014d752508SVladimir Oltean 	index = sja1105_find_unused_cbs_shaper(priv);
18024d752508SVladimir Oltean 	if (index < 0)
18034d752508SVladimir Oltean 		return -ENOSPC;
18044d752508SVladimir Oltean 
18054d752508SVladimir Oltean 	cbs = &priv->cbs[index];
18064d752508SVladimir Oltean 	cbs->port = port;
18074d752508SVladimir Oltean 	cbs->prio = offload->queue;
18084d752508SVladimir Oltean 	/* locredit and sendslope are negative by definition. In hardware,
18094d752508SVladimir Oltean 	 * positive values must be provided, and the negative sign is implicit.
18104d752508SVladimir Oltean 	 */
18114d752508SVladimir Oltean 	cbs->credit_hi = offload->hicredit;
18124d752508SVladimir Oltean 	cbs->credit_lo = abs(offload->locredit);
18134d752508SVladimir Oltean 	/* User space is in kbits/sec, hardware in bytes/sec */
18144d752508SVladimir Oltean 	cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
18154d752508SVladimir Oltean 	cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
18164d752508SVladimir Oltean 	/* Convert the negative values from 64-bit 2's complement
18174d752508SVladimir Oltean 	 * to 32-bit 2's complement (for the case of 0x80000000 whose
18184d752508SVladimir Oltean 	 * negative is still negative).
18194d752508SVladimir Oltean 	 */
18204d752508SVladimir Oltean 	cbs->credit_lo &= GENMASK_ULL(31, 0);
18214d752508SVladimir Oltean 	cbs->send_slope &= GENMASK_ULL(31, 0);
18224d752508SVladimir Oltean 
18234d752508SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
18244d752508SVladimir Oltean 					    true);
18254d752508SVladimir Oltean }
18264d752508SVladimir Oltean 
18274d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv)
18284d752508SVladimir Oltean {
18294d752508SVladimir Oltean 	int rc = 0, i;
18304d752508SVladimir Oltean 
18314d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
18324d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
18334d752508SVladimir Oltean 
18344d752508SVladimir Oltean 		if (!cbs->idle_slope && !cbs->send_slope)
18354d752508SVladimir Oltean 			continue;
18364d752508SVladimir Oltean 
18374d752508SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
18384d752508SVladimir Oltean 						  true);
18394d752508SVladimir Oltean 		if (rc)
18404d752508SVladimir Oltean 			break;
18414d752508SVladimir Oltean 	}
18424d752508SVladimir Oltean 
18434d752508SVladimir Oltean 	return rc;
18444d752508SVladimir Oltean }
18454d752508SVladimir Oltean 
18462eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
18472eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
18482eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
18492eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
18502eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
1851c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1852dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
18532eea1fa8SVladimir Oltean };
18542eea1fa8SVladimir Oltean 
18556666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
18566666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
18576666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
18586666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
18596666cebcSVladimir Oltean  * such that this operation is relatively seamless.
18606666cebcSVladimir Oltean  */
18612eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
18622eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
18636666cebcSVladimir Oltean {
18646cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
18656cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
186682760d7fSVladimir Oltean 	int speed_mbps[SJA1105_MAX_NUM_PORTS];
186784db00f2SVladimir Oltean 	u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0};
18686666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
18696cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
18706cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
18716cf99c13SVladimir Oltean 	s64 t12, t34;
18726666cebcSVladimir Oltean 	int rc, i;
18736cf99c13SVladimir Oltean 	s64 now;
18746666cebcSVladimir Oltean 
1875af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1876af580ae2SVladimir Oltean 
18776666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
18786666cebcSVladimir Oltean 
18798400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
18808400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
18818400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
18828400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
18836666cebcSVladimir Oltean 	 */
1884542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
18856666cebcSVladimir Oltean 		speed_mbps[i] = sja1105_speed[mac[i].speed];
18866666cebcSVladimir Oltean 		mac[i].speed = SJA1105_SPEED_AUTO;
18876666cebcSVladimir Oltean 
188884db00f2SVladimir Oltean 		if (sja1105_supports_sgmii(priv, i))
18894c7ee010SVladimir Oltean 			bmcr[i] = sja1105_sgmii_read(priv, i,
18904c7ee010SVladimir Oltean 						     MDIO_MMD_VEND2,
18914c7ee010SVladimir Oltean 						     MDIO_CTRL1);
189284db00f2SVladimir Oltean 	}
1893ffe10e67SVladimir Oltean 
18946cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
18956cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
18966cf99c13SVladimir Oltean 
18976cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
18986cf99c13SVladimir Oltean 	if (rc < 0)
18996cf99c13SVladimir Oltean 		goto out_unlock_ptp;
19006cf99c13SVladimir Oltean 
19016666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
19026666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
19036666cebcSVladimir Oltean 	if (rc < 0)
19046cf99c13SVladimir Oltean 		goto out_unlock_ptp;
19056cf99c13SVladimir Oltean 
19066cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
19076cf99c13SVladimir Oltean 	if (rc < 0)
19086cf99c13SVladimir Oltean 		goto out_unlock_ptp;
19096cf99c13SVladimir Oltean 
19106cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
19116cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
19126cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
19136cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
19146cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
19156cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
19166cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
19176cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
19186cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
19196cf99c13SVladimir Oltean 	now += (t34 - t12);
19206cf99c13SVladimir Oltean 
19216cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
19226cf99c13SVladimir Oltean 
19236cf99c13SVladimir Oltean out_unlock_ptp:
19246cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
19256666cebcSVladimir Oltean 
19262eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
19272eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
19282eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
19292eea1fa8SVladimir Oltean 
19306666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
19316666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
19326666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
19336666cebcSVladimir Oltean 	 */
1934c5037678SVladimir Oltean 	rc = priv->info->clocking_setup(priv);
19356666cebcSVladimir Oltean 	if (rc < 0)
19366666cebcSVladimir Oltean 		goto out;
19376666cebcSVladimir Oltean 
1938542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
193984db00f2SVladimir Oltean 		bool an_enabled;
194084db00f2SVladimir Oltean 
19418400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
19426666cebcSVladimir Oltean 		if (rc < 0)
19436666cebcSVladimir Oltean 			goto out;
1944ffe10e67SVladimir Oltean 
194584db00f2SVladimir Oltean 		if (!sja1105_supports_sgmii(priv, i))
194684db00f2SVladimir Oltean 			continue;
1947ffe10e67SVladimir Oltean 
194884db00f2SVladimir Oltean 		an_enabled = !!(bmcr[i] & BMCR_ANENABLE);
194984db00f2SVladimir Oltean 
195084db00f2SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, i, an_enabled, false);
1951ffe10e67SVladimir Oltean 
1952ffe10e67SVladimir Oltean 		if (!an_enabled) {
1953ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
1954ffe10e67SVladimir Oltean 
195584db00f2SVladimir Oltean 			if (bmcr[i] & BMCR_SPEED1000)
1956ffe10e67SVladimir Oltean 				speed = SPEED_1000;
195784db00f2SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED100)
1958ffe10e67SVladimir Oltean 				speed = SPEED_100;
1959053d8ad1SVladimir Oltean 			else
1960ffe10e67SVladimir Oltean 				speed = SPEED_10;
1961ffe10e67SVladimir Oltean 
196284db00f2SVladimir Oltean 			sja1105_sgmii_pcs_force_speed(priv, i, speed);
1963ffe10e67SVladimir Oltean 		}
1964ffe10e67SVladimir Oltean 	}
19654d752508SVladimir Oltean 
19664d752508SVladimir Oltean 	rc = sja1105_reload_cbs(priv);
19674d752508SVladimir Oltean 	if (rc < 0)
19684d752508SVladimir Oltean 		goto out;
19696666cebcSVladimir Oltean out:
1970af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1971af580ae2SVladimir Oltean 
19726666cebcSVladimir Oltean 	return rc;
19736666cebcSVladimir Oltean }
19746666cebcSVladimir Oltean 
19756666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
19766666cebcSVladimir Oltean {
19776666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
19786666cebcSVladimir Oltean 
19796666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
19806666cebcSVladimir Oltean 
19816666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
19826666cebcSVladimir Oltean 
19836666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
19846666cebcSVladimir Oltean 					   &mac[port], true);
19856666cebcSVladimir Oltean }
19866666cebcSVladimir Oltean 
1987ac02a451SVladimir Oltean static int sja1105_crosschip_bridge_join(struct dsa_switch *ds,
1988ac02a451SVladimir Oltean 					 int tree_index, int sw_index,
1989ac02a451SVladimir Oltean 					 int other_port, struct net_device *br)
1990ac02a451SVladimir Oltean {
1991ac02a451SVladimir Oltean 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1992ac02a451SVladimir Oltean 	struct sja1105_private *other_priv = other_ds->priv;
1993ac02a451SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1994ac02a451SVladimir Oltean 	int port, rc;
1995ac02a451SVladimir Oltean 
1996ac02a451SVladimir Oltean 	if (other_ds->ops != &sja1105_switch_ops)
1997ac02a451SVladimir Oltean 		return 0;
1998ac02a451SVladimir Oltean 
1999ac02a451SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
2000ac02a451SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2001ac02a451SVladimir Oltean 			continue;
2002ac02a451SVladimir Oltean 		if (dsa_to_port(ds, port)->bridge_dev != br)
2003ac02a451SVladimir Oltean 			continue;
2004ac02a451SVladimir Oltean 
20055899ee36SVladimir Oltean 		rc = dsa_8021q_crosschip_bridge_join(priv->dsa_8021q_ctx,
20065899ee36SVladimir Oltean 						     port,
20075899ee36SVladimir Oltean 						     other_priv->dsa_8021q_ctx,
20085899ee36SVladimir Oltean 						     other_port);
2009ac02a451SVladimir Oltean 		if (rc)
2010ac02a451SVladimir Oltean 			return rc;
2011ac02a451SVladimir Oltean 
20125899ee36SVladimir Oltean 		rc = dsa_8021q_crosschip_bridge_join(other_priv->dsa_8021q_ctx,
20135899ee36SVladimir Oltean 						     other_port,
20145899ee36SVladimir Oltean 						     priv->dsa_8021q_ctx,
20155899ee36SVladimir Oltean 						     port);
2016ac02a451SVladimir Oltean 		if (rc)
2017ac02a451SVladimir Oltean 			return rc;
2018ac02a451SVladimir Oltean 	}
2019ac02a451SVladimir Oltean 
2020ac02a451SVladimir Oltean 	return 0;
2021ac02a451SVladimir Oltean }
2022ac02a451SVladimir Oltean 
2023ac02a451SVladimir Oltean static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds,
2024ac02a451SVladimir Oltean 					   int tree_index, int sw_index,
2025ac02a451SVladimir Oltean 					   int other_port,
2026ac02a451SVladimir Oltean 					   struct net_device *br)
2027ac02a451SVladimir Oltean {
2028ac02a451SVladimir Oltean 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
2029ac02a451SVladimir Oltean 	struct sja1105_private *other_priv = other_ds->priv;
2030ac02a451SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2031ac02a451SVladimir Oltean 	int port;
2032ac02a451SVladimir Oltean 
2033ac02a451SVladimir Oltean 	if (other_ds->ops != &sja1105_switch_ops)
2034ac02a451SVladimir Oltean 		return;
2035ac02a451SVladimir Oltean 
2036ac02a451SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
2037ac02a451SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2038ac02a451SVladimir Oltean 			continue;
2039ac02a451SVladimir Oltean 		if (dsa_to_port(ds, port)->bridge_dev != br)
2040ac02a451SVladimir Oltean 			continue;
2041ac02a451SVladimir Oltean 
20425899ee36SVladimir Oltean 		dsa_8021q_crosschip_bridge_leave(priv->dsa_8021q_ctx, port,
20435899ee36SVladimir Oltean 						 other_priv->dsa_8021q_ctx,
20445899ee36SVladimir Oltean 						 other_port);
2045ac02a451SVladimir Oltean 
20465899ee36SVladimir Oltean 		dsa_8021q_crosschip_bridge_leave(other_priv->dsa_8021q_ctx,
20475899ee36SVladimir Oltean 						 other_port,
20485899ee36SVladimir Oltean 						 priv->dsa_8021q_ctx, port);
2049ac02a451SVladimir Oltean 	}
2050ac02a451SVladimir Oltean }
2051ac02a451SVladimir Oltean 
2052227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
2053227d07a0SVladimir Oltean {
205460b33aebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
20557e092af2SVladimir Oltean 	int rc;
2056227d07a0SVladimir Oltean 
20575899ee36SVladimir Oltean 	rc = dsa_8021q_setup(priv->dsa_8021q_ctx, enabled);
20587e092af2SVladimir Oltean 	if (rc)
2059227d07a0SVladimir Oltean 		return rc;
2060ac02a451SVladimir Oltean 
2061227d07a0SVladimir Oltean 	dev_info(ds->dev, "%s switch tagging\n",
2062227d07a0SVladimir Oltean 		 enabled ? "Enabled" : "Disabled");
2063227d07a0SVladimir Oltean 	return 0;
2064227d07a0SVladimir Oltean }
2065227d07a0SVladimir Oltean 
20668aa9ebccSVladimir Oltean static enum dsa_tag_protocol
20674d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
20684d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
20698aa9ebccSVladimir Oltean {
2070227d07a0SVladimir Oltean 	return DSA_TAG_PROTO_SJA1105;
20718aa9ebccSVladimir Oltean }
20728aa9ebccSVladimir Oltean 
20733f01c91aSVladimir Oltean static int sja1105_find_free_subvlan(u16 *subvlan_map, bool pvid)
20743f01c91aSVladimir Oltean {
20753f01c91aSVladimir Oltean 	int subvlan;
20763f01c91aSVladimir Oltean 
20773f01c91aSVladimir Oltean 	if (pvid)
20783f01c91aSVladimir Oltean 		return 0;
20793f01c91aSVladimir Oltean 
20803f01c91aSVladimir Oltean 	for (subvlan = 1; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
20813f01c91aSVladimir Oltean 		if (subvlan_map[subvlan] == VLAN_N_VID)
20823f01c91aSVladimir Oltean 			return subvlan;
20833f01c91aSVladimir Oltean 
20843f01c91aSVladimir Oltean 	return -1;
20853f01c91aSVladimir Oltean }
20863f01c91aSVladimir Oltean 
20873f01c91aSVladimir Oltean static int sja1105_find_subvlan(u16 *subvlan_map, u16 vid)
20883f01c91aSVladimir Oltean {
20893f01c91aSVladimir Oltean 	int subvlan;
20903f01c91aSVladimir Oltean 
20913f01c91aSVladimir Oltean 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
20923f01c91aSVladimir Oltean 		if (subvlan_map[subvlan] == vid)
20933f01c91aSVladimir Oltean 			return subvlan;
20943f01c91aSVladimir Oltean 
20953f01c91aSVladimir Oltean 	return -1;
20963f01c91aSVladimir Oltean }
20973f01c91aSVladimir Oltean 
20983f01c91aSVladimir Oltean static int sja1105_find_committed_subvlan(struct sja1105_private *priv,
20993f01c91aSVladimir Oltean 					  int port, u16 vid)
21003f01c91aSVladimir Oltean {
21013f01c91aSVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
21023f01c91aSVladimir Oltean 
21033f01c91aSVladimir Oltean 	return sja1105_find_subvlan(sp->subvlan_map, vid);
21043f01c91aSVladimir Oltean }
21053f01c91aSVladimir Oltean 
21063f01c91aSVladimir Oltean static void sja1105_init_subvlan_map(u16 *subvlan_map)
21073f01c91aSVladimir Oltean {
21083f01c91aSVladimir Oltean 	int subvlan;
21093f01c91aSVladimir Oltean 
21103f01c91aSVladimir Oltean 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
21113f01c91aSVladimir Oltean 		subvlan_map[subvlan] = VLAN_N_VID;
21123f01c91aSVladimir Oltean }
21133f01c91aSVladimir Oltean 
21143f01c91aSVladimir Oltean static void sja1105_commit_subvlan_map(struct sja1105_private *priv, int port,
21153f01c91aSVladimir Oltean 				       u16 *subvlan_map)
21163f01c91aSVladimir Oltean {
21173f01c91aSVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
21183f01c91aSVladimir Oltean 	int subvlan;
21193f01c91aSVladimir Oltean 
21203f01c91aSVladimir Oltean 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
21213f01c91aSVladimir Oltean 		sp->subvlan_map[subvlan] = subvlan_map[subvlan];
21223f01c91aSVladimir Oltean }
21233f01c91aSVladimir Oltean 
2124ec5ae610SVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
2125ec5ae610SVladimir Oltean {
2126ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
2127ec5ae610SVladimir Oltean 	int count, i;
2128ec5ae610SVladimir Oltean 
2129ec5ae610SVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
2130ec5ae610SVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
2131ec5ae610SVladimir Oltean 
2132ec5ae610SVladimir Oltean 	for (i = 0; i < count; i++)
2133ec5ae610SVladimir Oltean 		if (vlan[i].vlanid == vid)
2134ec5ae610SVladimir Oltean 			return i;
2135ec5ae610SVladimir Oltean 
2136ec5ae610SVladimir Oltean 	/* Return an invalid entry index if not found */
2137ec5ae610SVladimir Oltean 	return -1;
2138ec5ae610SVladimir Oltean }
2139ec5ae610SVladimir Oltean 
21403f01c91aSVladimir Oltean static int
21413f01c91aSVladimir Oltean sja1105_find_retagging_entry(struct sja1105_retagging_entry *retagging,
21423f01c91aSVladimir Oltean 			     int count, int from_port, u16 from_vid,
21433f01c91aSVladimir Oltean 			     u16 to_vid)
2144ec5ae610SVladimir Oltean {
21453f01c91aSVladimir Oltean 	int i;
21463f01c91aSVladimir Oltean 
21473f01c91aSVladimir Oltean 	for (i = 0; i < count; i++)
21483f01c91aSVladimir Oltean 		if (retagging[i].ing_port == BIT(from_port) &&
21493f01c91aSVladimir Oltean 		    retagging[i].vlan_ing == from_vid &&
21503f01c91aSVladimir Oltean 		    retagging[i].vlan_egr == to_vid)
21513f01c91aSVladimir Oltean 			return i;
21523f01c91aSVladimir Oltean 
21533f01c91aSVladimir Oltean 	/* Return an invalid entry index if not found */
21543f01c91aSVladimir Oltean 	return -1;
21553f01c91aSVladimir Oltean }
21563f01c91aSVladimir Oltean 
21573f01c91aSVladimir Oltean static int sja1105_commit_vlans(struct sja1105_private *priv,
21583f01c91aSVladimir Oltean 				struct sja1105_vlan_lookup_entry *new_vlan,
21593f01c91aSVladimir Oltean 				struct sja1105_retagging_entry *new_retagging,
21603f01c91aSVladimir Oltean 				int num_retagging)
21613f01c91aSVladimir Oltean {
21623f01c91aSVladimir Oltean 	struct sja1105_retagging_entry *retagging;
2163ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
2164ec5ae610SVladimir Oltean 	struct sja1105_table *table;
2165ec5ae610SVladimir Oltean 	int num_vlans = 0;
2166ec5ae610SVladimir Oltean 	int rc, i, k = 0;
2167ec5ae610SVladimir Oltean 
2168ec5ae610SVladimir Oltean 	/* VLAN table */
2169ec5ae610SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2170ec5ae610SVladimir Oltean 	vlan = table->entries;
2171ec5ae610SVladimir Oltean 
2172ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++) {
2173ec5ae610SVladimir Oltean 		int match = sja1105_is_vlan_configured(priv, i);
2174ec5ae610SVladimir Oltean 
2175ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid != VLAN_N_VID)
2176ec5ae610SVladimir Oltean 			num_vlans++;
2177ec5ae610SVladimir Oltean 
2178ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) {
2179ec5ae610SVladimir Oltean 			/* Was there before, no longer is. Delete */
2180ec5ae610SVladimir Oltean 			dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i);
2181ec5ae610SVladimir Oltean 			rc = sja1105_dynamic_config_write(priv,
2182ec5ae610SVladimir Oltean 							  BLK_IDX_VLAN_LOOKUP,
2183ec5ae610SVladimir Oltean 							  i, &vlan[match], false);
2184ec5ae610SVladimir Oltean 			if (rc < 0)
2185ec5ae610SVladimir Oltean 				return rc;
2186ec5ae610SVladimir Oltean 		} else if (new_vlan[i].vlanid != VLAN_N_VID) {
2187ec5ae610SVladimir Oltean 			/* Nothing changed, don't do anything */
2188ec5ae610SVladimir Oltean 			if (match >= 0 &&
2189ec5ae610SVladimir Oltean 			    vlan[match].vlanid == new_vlan[i].vlanid &&
2190ec5ae610SVladimir Oltean 			    vlan[match].tag_port == new_vlan[i].tag_port &&
2191ec5ae610SVladimir Oltean 			    vlan[match].vlan_bc == new_vlan[i].vlan_bc &&
2192ec5ae610SVladimir Oltean 			    vlan[match].vmemb_port == new_vlan[i].vmemb_port)
2193ec5ae610SVladimir Oltean 				continue;
2194ec5ae610SVladimir Oltean 			/* Update entry */
2195ec5ae610SVladimir Oltean 			dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i);
2196ec5ae610SVladimir Oltean 			rc = sja1105_dynamic_config_write(priv,
2197ec5ae610SVladimir Oltean 							  BLK_IDX_VLAN_LOOKUP,
2198ec5ae610SVladimir Oltean 							  i, &new_vlan[i],
2199ec5ae610SVladimir Oltean 							  true);
2200ec5ae610SVladimir Oltean 			if (rc < 0)
2201ec5ae610SVladimir Oltean 				return rc;
2202ec5ae610SVladimir Oltean 		}
2203ec5ae610SVladimir Oltean 	}
2204ec5ae610SVladimir Oltean 
2205ec5ae610SVladimir Oltean 	if (table->entry_count)
2206ec5ae610SVladimir Oltean 		kfree(table->entries);
2207ec5ae610SVladimir Oltean 
2208ec5ae610SVladimir Oltean 	table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size,
2209ec5ae610SVladimir Oltean 				 GFP_KERNEL);
2210ec5ae610SVladimir Oltean 	if (!table->entries)
2211ec5ae610SVladimir Oltean 		return -ENOMEM;
2212ec5ae610SVladimir Oltean 
2213ec5ae610SVladimir Oltean 	table->entry_count = num_vlans;
2214ec5ae610SVladimir Oltean 	vlan = table->entries;
2215ec5ae610SVladimir Oltean 
2216ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++) {
2217ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid == VLAN_N_VID)
2218ec5ae610SVladimir Oltean 			continue;
2219ec5ae610SVladimir Oltean 		vlan[k++] = new_vlan[i];
2220ec5ae610SVladimir Oltean 	}
2221ec5ae610SVladimir Oltean 
22223f01c91aSVladimir Oltean 	/* VLAN Retagging Table */
22233f01c91aSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_RETAGGING];
22243f01c91aSVladimir Oltean 	retagging = table->entries;
22253f01c91aSVladimir Oltean 
22263f01c91aSVladimir Oltean 	for (i = 0; i < table->entry_count; i++) {
22273f01c91aSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
22283f01c91aSVladimir Oltean 						  i, &retagging[i], false);
22293f01c91aSVladimir Oltean 		if (rc)
22303f01c91aSVladimir Oltean 			return rc;
22313f01c91aSVladimir Oltean 	}
22323f01c91aSVladimir Oltean 
22333f01c91aSVladimir Oltean 	if (table->entry_count)
22343f01c91aSVladimir Oltean 		kfree(table->entries);
22353f01c91aSVladimir Oltean 
22363f01c91aSVladimir Oltean 	table->entries = kcalloc(num_retagging, table->ops->unpacked_entry_size,
22373f01c91aSVladimir Oltean 				 GFP_KERNEL);
22383f01c91aSVladimir Oltean 	if (!table->entries)
22393f01c91aSVladimir Oltean 		return -ENOMEM;
22403f01c91aSVladimir Oltean 
22413f01c91aSVladimir Oltean 	table->entry_count = num_retagging;
22423f01c91aSVladimir Oltean 	retagging = table->entries;
22433f01c91aSVladimir Oltean 
22443f01c91aSVladimir Oltean 	for (i = 0; i < num_retagging; i++) {
22453f01c91aSVladimir Oltean 		retagging[i] = new_retagging[i];
22463f01c91aSVladimir Oltean 
22473f01c91aSVladimir Oltean 		/* Update entry */
22483f01c91aSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
22493f01c91aSVladimir Oltean 						  i, &retagging[i], true);
22503f01c91aSVladimir Oltean 		if (rc < 0)
22513f01c91aSVladimir Oltean 			return rc;
22523f01c91aSVladimir Oltean 	}
22533f01c91aSVladimir Oltean 
2254ec5ae610SVladimir Oltean 	return 0;
2255ec5ae610SVladimir Oltean }
2256ec5ae610SVladimir Oltean 
22573f01c91aSVladimir Oltean struct sja1105_crosschip_vlan {
22583f01c91aSVladimir Oltean 	struct list_head list;
22593f01c91aSVladimir Oltean 	u16 vid;
22603f01c91aSVladimir Oltean 	bool untagged;
22613f01c91aSVladimir Oltean 	int port;
22623f01c91aSVladimir Oltean 	int other_port;
22635899ee36SVladimir Oltean 	struct dsa_8021q_context *other_ctx;
22643f01c91aSVladimir Oltean };
22653f01c91aSVladimir Oltean 
2266ec5ae610SVladimir Oltean struct sja1105_crosschip_switch {
2267ec5ae610SVladimir Oltean 	struct list_head list;
22685899ee36SVladimir Oltean 	struct dsa_8021q_context *other_ctx;
2269ec5ae610SVladimir Oltean };
2270ec5ae610SVladimir Oltean 
2271ec5ae610SVladimir Oltean static int sja1105_commit_pvid(struct sja1105_private *priv)
2272ec5ae610SVladimir Oltean {
2273ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
2274ec5ae610SVladimir Oltean 	struct list_head *vlan_list;
2275ec5ae610SVladimir Oltean 	int rc = 0;
2276ec5ae610SVladimir Oltean 
2277ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2278ec5ae610SVladimir Oltean 		vlan_list = &priv->bridge_vlans;
2279ec5ae610SVladimir Oltean 	else
2280ec5ae610SVladimir Oltean 		vlan_list = &priv->dsa_8021q_vlans;
2281ec5ae610SVladimir Oltean 
2282ec5ae610SVladimir Oltean 	list_for_each_entry(v, vlan_list, list) {
2283ec5ae610SVladimir Oltean 		if (v->pvid) {
2284ec5ae610SVladimir Oltean 			rc = sja1105_pvid_apply(priv, v->port, v->vid);
2285ec5ae610SVladimir Oltean 			if (rc)
2286ec5ae610SVladimir Oltean 				break;
2287ec5ae610SVladimir Oltean 		}
2288ec5ae610SVladimir Oltean 	}
2289ec5ae610SVladimir Oltean 
2290ec5ae610SVladimir Oltean 	return rc;
2291ec5ae610SVladimir Oltean }
2292ec5ae610SVladimir Oltean 
2293ec5ae610SVladimir Oltean static int
2294ec5ae610SVladimir Oltean sja1105_build_bridge_vlans(struct sja1105_private *priv,
2295ec5ae610SVladimir Oltean 			   struct sja1105_vlan_lookup_entry *new_vlan)
2296ec5ae610SVladimir Oltean {
2297ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
2298ec5ae610SVladimir Oltean 
2299ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
2300ec5ae610SVladimir Oltean 		return 0;
2301ec5ae610SVladimir Oltean 
2302ec5ae610SVladimir Oltean 	list_for_each_entry(v, &priv->bridge_vlans, list) {
2303ec5ae610SVladimir Oltean 		int match = v->vid;
2304ec5ae610SVladimir Oltean 
2305ec5ae610SVladimir Oltean 		new_vlan[match].vlanid = v->vid;
2306ec5ae610SVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(v->port);
2307ec5ae610SVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(v->port);
2308ec5ae610SVladimir Oltean 		if (!v->untagged)
2309ec5ae610SVladimir Oltean 			new_vlan[match].tag_port |= BIT(v->port);
2310ec5ae610SVladimir Oltean 	}
2311ec5ae610SVladimir Oltean 
2312ec5ae610SVladimir Oltean 	return 0;
2313ec5ae610SVladimir Oltean }
2314ec5ae610SVladimir Oltean 
2315ec5ae610SVladimir Oltean static int
2316ec5ae610SVladimir Oltean sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv,
2317ec5ae610SVladimir Oltean 			      struct sja1105_vlan_lookup_entry *new_vlan)
2318ec5ae610SVladimir Oltean {
2319ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
2320ec5ae610SVladimir Oltean 
2321ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2322ec5ae610SVladimir Oltean 		return 0;
2323ec5ae610SVladimir Oltean 
2324ec5ae610SVladimir Oltean 	list_for_each_entry(v, &priv->dsa_8021q_vlans, list) {
2325ec5ae610SVladimir Oltean 		int match = v->vid;
2326ec5ae610SVladimir Oltean 
2327ec5ae610SVladimir Oltean 		new_vlan[match].vlanid = v->vid;
2328ec5ae610SVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(v->port);
2329ec5ae610SVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(v->port);
2330ec5ae610SVladimir Oltean 		if (!v->untagged)
2331ec5ae610SVladimir Oltean 			new_vlan[match].tag_port |= BIT(v->port);
2332ec5ae610SVladimir Oltean 	}
2333ec5ae610SVladimir Oltean 
2334ec5ae610SVladimir Oltean 	return 0;
2335ec5ae610SVladimir Oltean }
2336ec5ae610SVladimir Oltean 
23373f01c91aSVladimir Oltean static int sja1105_build_subvlans(struct sja1105_private *priv,
23383f01c91aSVladimir Oltean 				  u16 subvlan_map[][DSA_8021Q_N_SUBVLAN],
23393f01c91aSVladimir Oltean 				  struct sja1105_vlan_lookup_entry *new_vlan,
23403f01c91aSVladimir Oltean 				  struct sja1105_retagging_entry *new_retagging,
23413f01c91aSVladimir Oltean 				  int *num_retagging)
23423f01c91aSVladimir Oltean {
23433f01c91aSVladimir Oltean 	struct sja1105_bridge_vlan *v;
23443f01c91aSVladimir Oltean 	int k = *num_retagging;
23453f01c91aSVladimir Oltean 
23463f01c91aSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
23473f01c91aSVladimir Oltean 		return 0;
23483f01c91aSVladimir Oltean 
23493f01c91aSVladimir Oltean 	list_for_each_entry(v, &priv->bridge_vlans, list) {
23503f01c91aSVladimir Oltean 		int upstream = dsa_upstream_port(priv->ds, v->port);
23513f01c91aSVladimir Oltean 		int match, subvlan;
23523f01c91aSVladimir Oltean 		u16 rx_vid;
23533f01c91aSVladimir Oltean 
23543f01c91aSVladimir Oltean 		/* Only sub-VLANs on user ports need to be applied.
23553f01c91aSVladimir Oltean 		 * Bridge VLANs also include VLANs added automatically
23563f01c91aSVladimir Oltean 		 * by DSA on the CPU port.
23573f01c91aSVladimir Oltean 		 */
23583f01c91aSVladimir Oltean 		if (!dsa_is_user_port(priv->ds, v->port))
23593f01c91aSVladimir Oltean 			continue;
23603f01c91aSVladimir Oltean 
23613f01c91aSVladimir Oltean 		subvlan = sja1105_find_subvlan(subvlan_map[v->port],
23623f01c91aSVladimir Oltean 					       v->vid);
23633f01c91aSVladimir Oltean 		if (subvlan < 0) {
23643f01c91aSVladimir Oltean 			subvlan = sja1105_find_free_subvlan(subvlan_map[v->port],
23653f01c91aSVladimir Oltean 							    v->pvid);
23663f01c91aSVladimir Oltean 			if (subvlan < 0) {
23673f01c91aSVladimir Oltean 				dev_err(priv->ds->dev, "No more free subvlans\n");
23683f01c91aSVladimir Oltean 				return -ENOSPC;
23693f01c91aSVladimir Oltean 			}
23703f01c91aSVladimir Oltean 		}
23713f01c91aSVladimir Oltean 
23723f01c91aSVladimir Oltean 		rx_vid = dsa_8021q_rx_vid_subvlan(priv->ds, v->port, subvlan);
23733f01c91aSVladimir Oltean 
23743f01c91aSVladimir Oltean 		/* @v->vid on @v->port needs to be retagged to @rx_vid
23753f01c91aSVladimir Oltean 		 * on @upstream. Assume @v->vid on @v->port and on
23763f01c91aSVladimir Oltean 		 * @upstream was already configured by the previous
23773f01c91aSVladimir Oltean 		 * iteration over bridge_vlans.
23783f01c91aSVladimir Oltean 		 */
23793f01c91aSVladimir Oltean 		match = rx_vid;
23803f01c91aSVladimir Oltean 		new_vlan[match].vlanid = rx_vid;
23813f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(v->port);
23823f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(upstream);
23833f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(v->port);
23843f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(upstream);
23853f01c91aSVladimir Oltean 		/* The "untagged" flag is set the same as for the
23863f01c91aSVladimir Oltean 		 * original VLAN
23873f01c91aSVladimir Oltean 		 */
23883f01c91aSVladimir Oltean 		if (!v->untagged)
23893f01c91aSVladimir Oltean 			new_vlan[match].tag_port |= BIT(v->port);
23903f01c91aSVladimir Oltean 		/* But it's always tagged towards the CPU */
23913f01c91aSVladimir Oltean 		new_vlan[match].tag_port |= BIT(upstream);
23923f01c91aSVladimir Oltean 
23933f01c91aSVladimir Oltean 		/* The Retagging Table generates packet *clones* with
23943f01c91aSVladimir Oltean 		 * the new VLAN. This is a very odd hardware quirk
23953f01c91aSVladimir Oltean 		 * which we need to suppress by dropping the original
23963f01c91aSVladimir Oltean 		 * packet.
23973f01c91aSVladimir Oltean 		 * Deny egress of the original VLAN towards the CPU
23983f01c91aSVladimir Oltean 		 * port. This will force the switch to drop it, and
23993f01c91aSVladimir Oltean 		 * we'll see only the retagged packets.
24003f01c91aSVladimir Oltean 		 */
24013f01c91aSVladimir Oltean 		match = v->vid;
24023f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc &= ~BIT(upstream);
24033f01c91aSVladimir Oltean 
24043f01c91aSVladimir Oltean 		/* And the retagging itself */
24053f01c91aSVladimir Oltean 		new_retagging[k].vlan_ing = v->vid;
24063f01c91aSVladimir Oltean 		new_retagging[k].vlan_egr = rx_vid;
24073f01c91aSVladimir Oltean 		new_retagging[k].ing_port = BIT(v->port);
24083f01c91aSVladimir Oltean 		new_retagging[k].egr_port = BIT(upstream);
24093f01c91aSVladimir Oltean 		if (k++ == SJA1105_MAX_RETAGGING_COUNT) {
24103f01c91aSVladimir Oltean 			dev_err(priv->ds->dev, "No more retagging rules\n");
24113f01c91aSVladimir Oltean 			return -ENOSPC;
24123f01c91aSVladimir Oltean 		}
24133f01c91aSVladimir Oltean 
24143f01c91aSVladimir Oltean 		subvlan_map[v->port][subvlan] = v->vid;
24153f01c91aSVladimir Oltean 	}
24163f01c91aSVladimir Oltean 
24173f01c91aSVladimir Oltean 	*num_retagging = k;
24183f01c91aSVladimir Oltean 
24193f01c91aSVladimir Oltean 	return 0;
24203f01c91aSVladimir Oltean }
24213f01c91aSVladimir Oltean 
24223f01c91aSVladimir Oltean /* Sadly, in crosschip scenarios where the CPU port is also the link to another
24233f01c91aSVladimir Oltean  * switch, we should retag backwards (the dsa_8021q vid to the original vid) on
24243f01c91aSVladimir Oltean  * the CPU port of neighbour switches.
24253f01c91aSVladimir Oltean  */
24263f01c91aSVladimir Oltean static int
24273f01c91aSVladimir Oltean sja1105_build_crosschip_subvlans(struct sja1105_private *priv,
24283f01c91aSVladimir Oltean 				 struct sja1105_vlan_lookup_entry *new_vlan,
24293f01c91aSVladimir Oltean 				 struct sja1105_retagging_entry *new_retagging,
24303f01c91aSVladimir Oltean 				 int *num_retagging)
24313f01c91aSVladimir Oltean {
24323f01c91aSVladimir Oltean 	struct sja1105_crosschip_vlan *tmp, *pos;
24333f01c91aSVladimir Oltean 	struct dsa_8021q_crosschip_link *c;
24343f01c91aSVladimir Oltean 	struct sja1105_bridge_vlan *v, *w;
24353f01c91aSVladimir Oltean 	struct list_head crosschip_vlans;
24363f01c91aSVladimir Oltean 	int k = *num_retagging;
24373f01c91aSVladimir Oltean 	int rc = 0;
24383f01c91aSVladimir Oltean 
24393f01c91aSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
24403f01c91aSVladimir Oltean 		return 0;
24413f01c91aSVladimir Oltean 
24423f01c91aSVladimir Oltean 	INIT_LIST_HEAD(&crosschip_vlans);
24433f01c91aSVladimir Oltean 
24445899ee36SVladimir Oltean 	list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
24455899ee36SVladimir Oltean 		struct sja1105_private *other_priv = c->other_ctx->ds->priv;
24463f01c91aSVladimir Oltean 
24473f01c91aSVladimir Oltean 		if (other_priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
24483f01c91aSVladimir Oltean 			continue;
24493f01c91aSVladimir Oltean 
24503f01c91aSVladimir Oltean 		/* Crosschip links are also added to the CPU ports.
24513f01c91aSVladimir Oltean 		 * Ignore those.
24523f01c91aSVladimir Oltean 		 */
24533f01c91aSVladimir Oltean 		if (!dsa_is_user_port(priv->ds, c->port))
24543f01c91aSVladimir Oltean 			continue;
24555899ee36SVladimir Oltean 		if (!dsa_is_user_port(c->other_ctx->ds, c->other_port))
24563f01c91aSVladimir Oltean 			continue;
24573f01c91aSVladimir Oltean 
24583f01c91aSVladimir Oltean 		/* Search for VLANs on the remote port */
24593f01c91aSVladimir Oltean 		list_for_each_entry(v, &other_priv->bridge_vlans, list) {
24603f01c91aSVladimir Oltean 			bool already_added = false;
24613f01c91aSVladimir Oltean 			bool we_have_it = false;
24623f01c91aSVladimir Oltean 
24633f01c91aSVladimir Oltean 			if (v->port != c->other_port)
24643f01c91aSVladimir Oltean 				continue;
24653f01c91aSVladimir Oltean 
24663f01c91aSVladimir Oltean 			/* If @v is a pvid on @other_ds, it does not need
24673f01c91aSVladimir Oltean 			 * re-retagging, because its SVL field is 0 and we
24683f01c91aSVladimir Oltean 			 * already allow that, via the dsa_8021q crosschip
24693f01c91aSVladimir Oltean 			 * links.
24703f01c91aSVladimir Oltean 			 */
24713f01c91aSVladimir Oltean 			if (v->pvid)
24723f01c91aSVladimir Oltean 				continue;
24733f01c91aSVladimir Oltean 
24743f01c91aSVladimir Oltean 			/* Search for the VLAN on our local port */
24753f01c91aSVladimir Oltean 			list_for_each_entry(w, &priv->bridge_vlans, list) {
24763f01c91aSVladimir Oltean 				if (w->port == c->port && w->vid == v->vid) {
24773f01c91aSVladimir Oltean 					we_have_it = true;
24783f01c91aSVladimir Oltean 					break;
24793f01c91aSVladimir Oltean 				}
24803f01c91aSVladimir Oltean 			}
24813f01c91aSVladimir Oltean 
24823f01c91aSVladimir Oltean 			if (!we_have_it)
24833f01c91aSVladimir Oltean 				continue;
24843f01c91aSVladimir Oltean 
24853f01c91aSVladimir Oltean 			list_for_each_entry(tmp, &crosschip_vlans, list) {
24863f01c91aSVladimir Oltean 				if (tmp->vid == v->vid &&
24873f01c91aSVladimir Oltean 				    tmp->untagged == v->untagged &&
24883f01c91aSVladimir Oltean 				    tmp->port == c->port &&
24893f01c91aSVladimir Oltean 				    tmp->other_port == v->port &&
24905899ee36SVladimir Oltean 				    tmp->other_ctx == c->other_ctx) {
24913f01c91aSVladimir Oltean 					already_added = true;
24923f01c91aSVladimir Oltean 					break;
24933f01c91aSVladimir Oltean 				}
24943f01c91aSVladimir Oltean 			}
24953f01c91aSVladimir Oltean 
24963f01c91aSVladimir Oltean 			if (already_added)
24973f01c91aSVladimir Oltean 				continue;
24983f01c91aSVladimir Oltean 
24993f01c91aSVladimir Oltean 			tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
25003f01c91aSVladimir Oltean 			if (!tmp) {
25013f01c91aSVladimir Oltean 				dev_err(priv->ds->dev, "Failed to allocate memory\n");
25023f01c91aSVladimir Oltean 				rc = -ENOMEM;
25033f01c91aSVladimir Oltean 				goto out;
25043f01c91aSVladimir Oltean 			}
25053f01c91aSVladimir Oltean 			tmp->vid = v->vid;
25063f01c91aSVladimir Oltean 			tmp->port = c->port;
25073f01c91aSVladimir Oltean 			tmp->other_port = v->port;
25085899ee36SVladimir Oltean 			tmp->other_ctx = c->other_ctx;
25093f01c91aSVladimir Oltean 			tmp->untagged = v->untagged;
25103f01c91aSVladimir Oltean 			list_add(&tmp->list, &crosschip_vlans);
25113f01c91aSVladimir Oltean 		}
25123f01c91aSVladimir Oltean 	}
25133f01c91aSVladimir Oltean 
25143f01c91aSVladimir Oltean 	list_for_each_entry(tmp, &crosschip_vlans, list) {
25155899ee36SVladimir Oltean 		struct sja1105_private *other_priv = tmp->other_ctx->ds->priv;
25163f01c91aSVladimir Oltean 		int upstream = dsa_upstream_port(priv->ds, tmp->port);
25173f01c91aSVladimir Oltean 		int match, subvlan;
25183f01c91aSVladimir Oltean 		u16 rx_vid;
25193f01c91aSVladimir Oltean 
25203f01c91aSVladimir Oltean 		subvlan = sja1105_find_committed_subvlan(other_priv,
25213f01c91aSVladimir Oltean 							 tmp->other_port,
25223f01c91aSVladimir Oltean 							 tmp->vid);
25233f01c91aSVladimir Oltean 		/* If this happens, it's a bug. The neighbour switch does not
25243f01c91aSVladimir Oltean 		 * have a subvlan for tmp->vid on tmp->other_port, but it
25253f01c91aSVladimir Oltean 		 * should, since we already checked for its vlan_state.
25263f01c91aSVladimir Oltean 		 */
25273f01c91aSVladimir Oltean 		if (WARN_ON(subvlan < 0)) {
25283f01c91aSVladimir Oltean 			rc = -EINVAL;
25293f01c91aSVladimir Oltean 			goto out;
25303f01c91aSVladimir Oltean 		}
25313f01c91aSVladimir Oltean 
25325899ee36SVladimir Oltean 		rx_vid = dsa_8021q_rx_vid_subvlan(tmp->other_ctx->ds,
25333f01c91aSVladimir Oltean 						  tmp->other_port,
25343f01c91aSVladimir Oltean 						  subvlan);
25353f01c91aSVladimir Oltean 
25363f01c91aSVladimir Oltean 		/* The @rx_vid retagged from @tmp->vid on
25373f01c91aSVladimir Oltean 		 * {@tmp->other_ds, @tmp->other_port} needs to be
25383f01c91aSVladimir Oltean 		 * re-retagged to @tmp->vid on the way back to us.
25393f01c91aSVladimir Oltean 		 *
25403f01c91aSVladimir Oltean 		 * Assume the original @tmp->vid is already configured
25413f01c91aSVladimir Oltean 		 * on this local switch, otherwise we wouldn't be
25423f01c91aSVladimir Oltean 		 * retagging its subvlan on the other switch in the
25433f01c91aSVladimir Oltean 		 * first place. We just need to add a reverse retagging
25443f01c91aSVladimir Oltean 		 * rule for @rx_vid and install @rx_vid on our ports.
25453f01c91aSVladimir Oltean 		 */
25463f01c91aSVladimir Oltean 		match = rx_vid;
25473f01c91aSVladimir Oltean 		new_vlan[match].vlanid = rx_vid;
25483f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(tmp->port);
25493f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(upstream);
25503f01c91aSVladimir Oltean 		/* The "untagged" flag is set the same as for the
25513f01c91aSVladimir Oltean 		 * original VLAN. And towards the CPU, it doesn't
25523f01c91aSVladimir Oltean 		 * really matter, because @rx_vid will only receive
25533f01c91aSVladimir Oltean 		 * traffic on that port. For consistency with other dsa_8021q
25543f01c91aSVladimir Oltean 		 * VLANs, we'll keep the CPU port tagged.
25553f01c91aSVladimir Oltean 		 */
25563f01c91aSVladimir Oltean 		if (!tmp->untagged)
25573f01c91aSVladimir Oltean 			new_vlan[match].tag_port |= BIT(tmp->port);
25583f01c91aSVladimir Oltean 		new_vlan[match].tag_port |= BIT(upstream);
25593f01c91aSVladimir Oltean 		/* Deny egress of @rx_vid towards our front-panel port.
25603f01c91aSVladimir Oltean 		 * This will force the switch to drop it, and we'll see
25613f01c91aSVladimir Oltean 		 * only the re-retagged packets (having the original,
25623f01c91aSVladimir Oltean 		 * pre-initial-retagging, VLAN @tmp->vid).
25633f01c91aSVladimir Oltean 		 */
25643f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc &= ~BIT(tmp->port);
25653f01c91aSVladimir Oltean 
25663f01c91aSVladimir Oltean 		/* On reverse retagging, the same ingress VLAN goes to multiple
25673f01c91aSVladimir Oltean 		 * ports. So we have an opportunity to create composite rules
25683f01c91aSVladimir Oltean 		 * to not waste the limited space in the retagging table.
25693f01c91aSVladimir Oltean 		 */
25703f01c91aSVladimir Oltean 		k = sja1105_find_retagging_entry(new_retagging, *num_retagging,
25713f01c91aSVladimir Oltean 						 upstream, rx_vid, tmp->vid);
25723f01c91aSVladimir Oltean 		if (k < 0) {
25733f01c91aSVladimir Oltean 			if (*num_retagging == SJA1105_MAX_RETAGGING_COUNT) {
25743f01c91aSVladimir Oltean 				dev_err(priv->ds->dev, "No more retagging rules\n");
25753f01c91aSVladimir Oltean 				rc = -ENOSPC;
25763f01c91aSVladimir Oltean 				goto out;
25773f01c91aSVladimir Oltean 			}
25783f01c91aSVladimir Oltean 			k = (*num_retagging)++;
25793f01c91aSVladimir Oltean 		}
25803f01c91aSVladimir Oltean 		/* And the retagging itself */
25813f01c91aSVladimir Oltean 		new_retagging[k].vlan_ing = rx_vid;
25823f01c91aSVladimir Oltean 		new_retagging[k].vlan_egr = tmp->vid;
25833f01c91aSVladimir Oltean 		new_retagging[k].ing_port = BIT(upstream);
25843f01c91aSVladimir Oltean 		new_retagging[k].egr_port |= BIT(tmp->port);
25853f01c91aSVladimir Oltean 	}
25863f01c91aSVladimir Oltean 
25873f01c91aSVladimir Oltean out:
25883f01c91aSVladimir Oltean 	list_for_each_entry_safe(tmp, pos, &crosschip_vlans, list) {
25893f01c91aSVladimir Oltean 		list_del(&tmp->list);
25903f01c91aSVladimir Oltean 		kfree(tmp);
25913f01c91aSVladimir Oltean 	}
25923f01c91aSVladimir Oltean 
25933f01c91aSVladimir Oltean 	return rc;
25943f01c91aSVladimir Oltean }
25953f01c91aSVladimir Oltean 
2596ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify);
2597ec5ae610SVladimir Oltean 
2598ec5ae610SVladimir Oltean static int sja1105_notify_crosschip_switches(struct sja1105_private *priv)
2599ec5ae610SVladimir Oltean {
2600ec5ae610SVladimir Oltean 	struct sja1105_crosschip_switch *s, *pos;
2601ec5ae610SVladimir Oltean 	struct list_head crosschip_switches;
2602ec5ae610SVladimir Oltean 	struct dsa_8021q_crosschip_link *c;
2603ec5ae610SVladimir Oltean 	int rc = 0;
2604ec5ae610SVladimir Oltean 
2605ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&crosschip_switches);
2606ec5ae610SVladimir Oltean 
26075899ee36SVladimir Oltean 	list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
2608ec5ae610SVladimir Oltean 		bool already_added = false;
2609ec5ae610SVladimir Oltean 
2610ec5ae610SVladimir Oltean 		list_for_each_entry(s, &crosschip_switches, list) {
26115899ee36SVladimir Oltean 			if (s->other_ctx == c->other_ctx) {
2612ec5ae610SVladimir Oltean 				already_added = true;
2613ec5ae610SVladimir Oltean 				break;
2614ec5ae610SVladimir Oltean 			}
2615ec5ae610SVladimir Oltean 		}
2616ec5ae610SVladimir Oltean 
2617ec5ae610SVladimir Oltean 		if (already_added)
2618ec5ae610SVladimir Oltean 			continue;
2619ec5ae610SVladimir Oltean 
2620ec5ae610SVladimir Oltean 		s = kzalloc(sizeof(*s), GFP_KERNEL);
2621ec5ae610SVladimir Oltean 		if (!s) {
2622ec5ae610SVladimir Oltean 			dev_err(priv->ds->dev, "Failed to allocate memory\n");
2623ec5ae610SVladimir Oltean 			rc = -ENOMEM;
2624ec5ae610SVladimir Oltean 			goto out;
2625ec5ae610SVladimir Oltean 		}
26265899ee36SVladimir Oltean 		s->other_ctx = c->other_ctx;
2627ec5ae610SVladimir Oltean 		list_add(&s->list, &crosschip_switches);
2628ec5ae610SVladimir Oltean 	}
2629ec5ae610SVladimir Oltean 
2630ec5ae610SVladimir Oltean 	list_for_each_entry(s, &crosschip_switches, list) {
26315899ee36SVladimir Oltean 		struct sja1105_private *other_priv = s->other_ctx->ds->priv;
2632ec5ae610SVladimir Oltean 
2633ec5ae610SVladimir Oltean 		rc = sja1105_build_vlan_table(other_priv, false);
2634ec5ae610SVladimir Oltean 		if (rc)
2635ec5ae610SVladimir Oltean 			goto out;
2636ec5ae610SVladimir Oltean 	}
2637ec5ae610SVladimir Oltean 
2638ec5ae610SVladimir Oltean out:
2639ec5ae610SVladimir Oltean 	list_for_each_entry_safe(s, pos, &crosschip_switches, list) {
2640ec5ae610SVladimir Oltean 		list_del(&s->list);
2641ec5ae610SVladimir Oltean 		kfree(s);
2642ec5ae610SVladimir Oltean 	}
2643ec5ae610SVladimir Oltean 
2644ec5ae610SVladimir Oltean 	return rc;
2645ec5ae610SVladimir Oltean }
2646ec5ae610SVladimir Oltean 
2647ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify)
2648ec5ae610SVladimir Oltean {
264982760d7fSVladimir Oltean 	u16 subvlan_map[SJA1105_MAX_NUM_PORTS][DSA_8021Q_N_SUBVLAN];
26503f01c91aSVladimir Oltean 	struct sja1105_retagging_entry *new_retagging;
2651ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *new_vlan;
2652ec5ae610SVladimir Oltean 	struct sja1105_table *table;
26533f01c91aSVladimir Oltean 	int i, num_retagging = 0;
2654ec5ae610SVladimir Oltean 	int rc;
2655ec5ae610SVladimir Oltean 
2656ec5ae610SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2657ec5ae610SVladimir Oltean 	new_vlan = kcalloc(VLAN_N_VID,
2658ec5ae610SVladimir Oltean 			   table->ops->unpacked_entry_size, GFP_KERNEL);
2659ec5ae610SVladimir Oltean 	if (!new_vlan)
2660ec5ae610SVladimir Oltean 		return -ENOMEM;
2661ec5ae610SVladimir Oltean 
26623f01c91aSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
26633f01c91aSVladimir Oltean 	new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT,
26643f01c91aSVladimir Oltean 				table->ops->unpacked_entry_size, GFP_KERNEL);
26653f01c91aSVladimir Oltean 	if (!new_retagging) {
26663f01c91aSVladimir Oltean 		kfree(new_vlan);
26673f01c91aSVladimir Oltean 		return -ENOMEM;
26683f01c91aSVladimir Oltean 	}
26693f01c91aSVladimir Oltean 
2670ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++)
2671ec5ae610SVladimir Oltean 		new_vlan[i].vlanid = VLAN_N_VID;
2672ec5ae610SVladimir Oltean 
26733f01c91aSVladimir Oltean 	for (i = 0; i < SJA1105_MAX_RETAGGING_COUNT; i++)
26743f01c91aSVladimir Oltean 		new_retagging[i].vlan_ing = VLAN_N_VID;
26753f01c91aSVladimir Oltean 
26763f01c91aSVladimir Oltean 	for (i = 0; i < priv->ds->num_ports; i++)
26773f01c91aSVladimir Oltean 		sja1105_init_subvlan_map(subvlan_map[i]);
26783f01c91aSVladimir Oltean 
2679ec5ae610SVladimir Oltean 	/* Bridge VLANs */
2680ec5ae610SVladimir Oltean 	rc = sja1105_build_bridge_vlans(priv, new_vlan);
2681ec5ae610SVladimir Oltean 	if (rc)
2682ec5ae610SVladimir Oltean 		goto out;
2683ec5ae610SVladimir Oltean 
2684ec5ae610SVladimir Oltean 	/* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c:
2685ec5ae610SVladimir Oltean 	 * - RX VLANs
2686ec5ae610SVladimir Oltean 	 * - TX VLANs
2687ec5ae610SVladimir Oltean 	 * - Crosschip links
2688ec5ae610SVladimir Oltean 	 */
2689ec5ae610SVladimir Oltean 	rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan);
2690ec5ae610SVladimir Oltean 	if (rc)
2691ec5ae610SVladimir Oltean 		goto out;
2692ec5ae610SVladimir Oltean 
26933f01c91aSVladimir Oltean 	/* Private VLANs necessary for dsa_8021q operation, which we need to
26943f01c91aSVladimir Oltean 	 * determine on our own:
26953f01c91aSVladimir Oltean 	 * - Sub-VLANs
26963f01c91aSVladimir Oltean 	 * - Sub-VLANs of crosschip switches
26973f01c91aSVladimir Oltean 	 */
26983f01c91aSVladimir Oltean 	rc = sja1105_build_subvlans(priv, subvlan_map, new_vlan, new_retagging,
26993f01c91aSVladimir Oltean 				    &num_retagging);
27003f01c91aSVladimir Oltean 	if (rc)
27013f01c91aSVladimir Oltean 		goto out;
27023f01c91aSVladimir Oltean 
27033f01c91aSVladimir Oltean 	rc = sja1105_build_crosschip_subvlans(priv, new_vlan, new_retagging,
27043f01c91aSVladimir Oltean 					      &num_retagging);
27053f01c91aSVladimir Oltean 	if (rc)
27063f01c91aSVladimir Oltean 		goto out;
27073f01c91aSVladimir Oltean 
27083f01c91aSVladimir Oltean 	rc = sja1105_commit_vlans(priv, new_vlan, new_retagging, num_retagging);
2709ec5ae610SVladimir Oltean 	if (rc)
2710ec5ae610SVladimir Oltean 		goto out;
2711ec5ae610SVladimir Oltean 
2712ec5ae610SVladimir Oltean 	rc = sja1105_commit_pvid(priv);
2713ec5ae610SVladimir Oltean 	if (rc)
2714ec5ae610SVladimir Oltean 		goto out;
2715ec5ae610SVladimir Oltean 
27163f01c91aSVladimir Oltean 	for (i = 0; i < priv->ds->num_ports; i++)
27173f01c91aSVladimir Oltean 		sja1105_commit_subvlan_map(priv, i, subvlan_map[i]);
27183f01c91aSVladimir Oltean 
2719ec5ae610SVladimir Oltean 	if (notify) {
2720ec5ae610SVladimir Oltean 		rc = sja1105_notify_crosschip_switches(priv);
2721ec5ae610SVladimir Oltean 		if (rc)
2722ec5ae610SVladimir Oltean 			goto out;
2723ec5ae610SVladimir Oltean 	}
2724ec5ae610SVladimir Oltean 
2725ec5ae610SVladimir Oltean out:
2726ec5ae610SVladimir Oltean 	kfree(new_vlan);
27273f01c91aSVladimir Oltean 	kfree(new_retagging);
2728ec5ae610SVladimir Oltean 
2729ec5ae610SVladimir Oltean 	return rc;
2730ec5ae610SVladimir Oltean }
2731ec5ae610SVladimir Oltean 
2732070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
2733070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
2734070ca3bbSVladimir Oltean  * So a switch reset is required.
2735070ca3bbSVladimir Oltean  */
273689153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
273789153ed6SVladimir Oltean 			   struct netlink_ext_ack *extack)
27386666cebcSVladimir Oltean {
27396d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2740070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
27416666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
27427f14937fSVladimir Oltean 	enum sja1105_vlan_state state;
2743070ca3bbSVladimir Oltean 	struct sja1105_table *table;
2744dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
27452cafa72eSVladimir Oltean 	bool want_tagging;
2746070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
27476666cebcSVladimir Oltean 	int rc;
27486666cebcSVladimir Oltean 
2749dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
2750dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
275189153ed6SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
275289153ed6SVladimir Oltean 					   "Cannot change VLAN filtering with active VL rules");
2753dfacc5a2SVladimir Oltean 			return -EBUSY;
2754dfacc5a2SVladimir Oltean 		}
2755dfacc5a2SVladimir Oltean 	}
2756dfacc5a2SVladimir Oltean 
2757070ca3bbSVladimir Oltean 	if (enabled) {
27586666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
275954fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
276054fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
2761070ca3bbSVladimir Oltean 	} else {
27626666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
2763070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
2764070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
2765070ca3bbSVladimir Oltean 	}
2766070ca3bbSVladimir Oltean 
276738b5beeaSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
276838b5beeaSVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
276938b5beeaSVladimir Oltean 
277038b5beeaSVladimir Oltean 		if (enabled)
277138b5beeaSVladimir Oltean 			sp->xmit_tpid = priv->info->qinq_tpid;
277238b5beeaSVladimir Oltean 		else
277338b5beeaSVladimir Oltean 			sp->xmit_tpid = ETH_P_SJA1105;
277438b5beeaSVladimir Oltean 	}
277538b5beeaSVladimir Oltean 
27767f14937fSVladimir Oltean 	if (!enabled)
27777f14937fSVladimir Oltean 		state = SJA1105_VLAN_UNAWARE;
27782cafa72eSVladimir Oltean 	else if (priv->best_effort_vlan_filtering)
27792cafa72eSVladimir Oltean 		state = SJA1105_VLAN_BEST_EFFORT;
27807f14937fSVladimir Oltean 	else
27817f14937fSVladimir Oltean 		state = SJA1105_VLAN_FILTERING_FULL;
27827f14937fSVladimir Oltean 
2783cfa36b1fSVladimir Oltean 	if (priv->vlan_state == state)
2784cfa36b1fSVladimir Oltean 		return 0;
2785cfa36b1fSVladimir Oltean 
27867f14937fSVladimir Oltean 	priv->vlan_state = state;
27872cafa72eSVladimir Oltean 	want_tagging = (state == SJA1105_VLAN_UNAWARE ||
27882cafa72eSVladimir Oltean 			state == SJA1105_VLAN_BEST_EFFORT);
27897f14937fSVladimir Oltean 
2790070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2791070ca3bbSVladimir Oltean 	general_params = table->entries;
2792f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
279354fa49eeSVladimir Oltean 	general_params->tpid = tpid;
279454fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2795070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
279642824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
279742824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
279842824463SVladimir Oltean 	 */
279942824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
280042824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
2801070ca3bbSVladimir Oltean 
28022cafa72eSVladimir Oltean 	want_tagging = priv->best_effort_vlan_filtering || !enabled;
28032cafa72eSVladimir Oltean 
28046d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
28052cafa72eSVladimir Oltean 	 * No VLAN filtering (or best effort) => shared VLAN learning.
28066d7c7d94SVladimir Oltean 	 *
28076d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
28086d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
28096d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
28106d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
28116d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
28126d7c7d94SVladimir Oltean 	 * forwarding decision.
28136d7c7d94SVladimir Oltean 	 *
28146d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
28156d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
28166d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
28176d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
28186d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
28196d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
28206d7c7d94SVladimir Oltean 	 * (all frames get flooded).
28216d7c7d94SVladimir Oltean 	 */
28226d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
28236d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
28242cafa72eSVladimir Oltean 	l2_lookup_params->shared_learn = want_tagging;
28256d7c7d94SVladimir Oltean 
2826aaa270c6SVladimir Oltean 	sja1105_frame_memory_partitioning(priv);
2827aaa270c6SVladimir Oltean 
2828aef31718SVladimir Oltean 	rc = sja1105_build_vlan_table(priv, false);
2829aef31718SVladimir Oltean 	if (rc)
2830aef31718SVladimir Oltean 		return rc;
2831aef31718SVladimir Oltean 
28322eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
28336666cebcSVladimir Oltean 	if (rc)
283489153ed6SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype");
28356666cebcSVladimir Oltean 
2836227d07a0SVladimir Oltean 	/* Switch port identification based on 802.1Q is only passable
2837227d07a0SVladimir Oltean 	 * if we are not under a vlan_filtering bridge. So make sure
28382cafa72eSVladimir Oltean 	 * the two configurations are mutually exclusive (of course, the
28392cafa72eSVladimir Oltean 	 * user may know better, i.e. best_effort_vlan_filtering).
2840227d07a0SVladimir Oltean 	 */
28412cafa72eSVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, want_tagging);
28426666cebcSVladimir Oltean }
28436666cebcSVladimir Oltean 
28445899ee36SVladimir Oltean /* Returns number of VLANs added (0 or 1) on success,
28455899ee36SVladimir Oltean  * or a negative error code.
28465899ee36SVladimir Oltean  */
28475899ee36SVladimir Oltean static int sja1105_vlan_add_one(struct dsa_switch *ds, int port, u16 vid,
28485899ee36SVladimir Oltean 				u16 flags, struct list_head *vlan_list)
28495899ee36SVladimir Oltean {
28505899ee36SVladimir Oltean 	bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED;
28515899ee36SVladimir Oltean 	bool pvid = flags & BRIDGE_VLAN_INFO_PVID;
28525899ee36SVladimir Oltean 	struct sja1105_bridge_vlan *v;
28535899ee36SVladimir Oltean 
2854b38e659dSVladimir Oltean 	list_for_each_entry(v, vlan_list, list) {
2855b38e659dSVladimir Oltean 		if (v->port == port && v->vid == vid) {
28565899ee36SVladimir Oltean 			/* Already added */
2857b38e659dSVladimir Oltean 			if (v->untagged == untagged && v->pvid == pvid)
2858b38e659dSVladimir Oltean 				/* Nothing changed */
28595899ee36SVladimir Oltean 				return 0;
28605899ee36SVladimir Oltean 
2861b38e659dSVladimir Oltean 			/* It's the same VLAN, but some of the flags changed
2862b38e659dSVladimir Oltean 			 * and the user did not bother to delete it first.
2863b38e659dSVladimir Oltean 			 * Update it and trigger sja1105_build_vlan_table.
2864b38e659dSVladimir Oltean 			 */
2865b38e659dSVladimir Oltean 			v->untagged = untagged;
2866b38e659dSVladimir Oltean 			v->pvid = pvid;
2867b38e659dSVladimir Oltean 			return 1;
2868b38e659dSVladimir Oltean 		}
2869b38e659dSVladimir Oltean 	}
2870b38e659dSVladimir Oltean 
28715899ee36SVladimir Oltean 	v = kzalloc(sizeof(*v), GFP_KERNEL);
28725899ee36SVladimir Oltean 	if (!v) {
28735899ee36SVladimir Oltean 		dev_err(ds->dev, "Out of memory while storing VLAN\n");
28745899ee36SVladimir Oltean 		return -ENOMEM;
28755899ee36SVladimir Oltean 	}
28765899ee36SVladimir Oltean 
28775899ee36SVladimir Oltean 	v->port = port;
28785899ee36SVladimir Oltean 	v->vid = vid;
28795899ee36SVladimir Oltean 	v->untagged = untagged;
28805899ee36SVladimir Oltean 	v->pvid = pvid;
28815899ee36SVladimir Oltean 	list_add(&v->list, vlan_list);
28825899ee36SVladimir Oltean 
28835899ee36SVladimir Oltean 	return 1;
28845899ee36SVladimir Oltean }
28855899ee36SVladimir Oltean 
28865899ee36SVladimir Oltean /* Returns number of VLANs deleted (0 or 1) */
28875899ee36SVladimir Oltean static int sja1105_vlan_del_one(struct dsa_switch *ds, int port, u16 vid,
28885899ee36SVladimir Oltean 				struct list_head *vlan_list)
28895899ee36SVladimir Oltean {
28905899ee36SVladimir Oltean 	struct sja1105_bridge_vlan *v, *n;
28915899ee36SVladimir Oltean 
28925899ee36SVladimir Oltean 	list_for_each_entry_safe(v, n, vlan_list, list) {
28935899ee36SVladimir Oltean 		if (v->port == port && v->vid == vid) {
28945899ee36SVladimir Oltean 			list_del(&v->list);
28955899ee36SVladimir Oltean 			kfree(v);
28965899ee36SVladimir Oltean 			return 1;
28975899ee36SVladimir Oltean 		}
28985899ee36SVladimir Oltean 	}
28995899ee36SVladimir Oltean 
29005899ee36SVladimir Oltean 	return 0;
29015899ee36SVladimir Oltean }
29025899ee36SVladimir Oltean 
29031958d581SVladimir Oltean static int sja1105_vlan_add(struct dsa_switch *ds, int port,
290431046a5fSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan,
290531046a5fSVladimir Oltean 			    struct netlink_ext_ack *extack)
29066666cebcSVladimir Oltean {
29076666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2908ec5ae610SVladimir Oltean 	bool vlan_table_changed = false;
29096666cebcSVladimir Oltean 	int rc;
29106666cebcSVladimir Oltean 
29111958d581SVladimir Oltean 	/* If the user wants best-effort VLAN filtering (aka vlan_filtering
29121958d581SVladimir Oltean 	 * bridge plus tagging), be sure to at least deny alterations to the
29131958d581SVladimir Oltean 	 * configuration done by dsa_8021q.
29141958d581SVladimir Oltean 	 */
29151958d581SVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL &&
29161958d581SVladimir Oltean 	    vid_is_dsa_8021q(vlan->vid)) {
291731046a5fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
291831046a5fSVladimir Oltean 				   "Range 1024-3071 reserved for dsa_8021q operation");
29191958d581SVladimir Oltean 		return -EBUSY;
29201958d581SVladimir Oltean 	}
29211958d581SVladimir Oltean 
2922b7a9e0daSVladimir Oltean 	rc = sja1105_vlan_add_one(ds, port, vlan->vid, vlan->flags,
29235899ee36SVladimir Oltean 				  &priv->bridge_vlans);
29245899ee36SVladimir Oltean 	if (rc < 0)
29251958d581SVladimir Oltean 		return rc;
29265899ee36SVladimir Oltean 	if (rc > 0)
2927ec5ae610SVladimir Oltean 		vlan_table_changed = true;
2928ec5ae610SVladimir Oltean 
2929ec5ae610SVladimir Oltean 	if (!vlan_table_changed)
29301958d581SVladimir Oltean 		return 0;
2931ec5ae610SVladimir Oltean 
29321958d581SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
29336666cebcSVladimir Oltean }
29346666cebcSVladimir Oltean 
29356666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
29366666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
29376666cebcSVladimir Oltean {
29386666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2939ec5ae610SVladimir Oltean 	bool vlan_table_changed = false;
29405899ee36SVladimir Oltean 	int rc;
29416666cebcSVladimir Oltean 
2942b7a9e0daSVladimir Oltean 	rc = sja1105_vlan_del_one(ds, port, vlan->vid, &priv->bridge_vlans);
29435899ee36SVladimir Oltean 	if (rc > 0)
2944ec5ae610SVladimir Oltean 		vlan_table_changed = true;
2945ec5ae610SVladimir Oltean 
2946ec5ae610SVladimir Oltean 	if (!vlan_table_changed)
29476666cebcSVladimir Oltean 		return 0;
2948ec5ae610SVladimir Oltean 
2949ec5ae610SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
29506666cebcSVladimir Oltean }
29516666cebcSVladimir Oltean 
29525899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
29535899ee36SVladimir Oltean 				      u16 flags)
29545899ee36SVladimir Oltean {
29555899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29565899ee36SVladimir Oltean 	int rc;
29575899ee36SVladimir Oltean 
29585899ee36SVladimir Oltean 	rc = sja1105_vlan_add_one(ds, port, vid, flags, &priv->dsa_8021q_vlans);
29595899ee36SVladimir Oltean 	if (rc <= 0)
29605899ee36SVladimir Oltean 		return rc;
29615899ee36SVladimir Oltean 
29625899ee36SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
29635899ee36SVladimir Oltean }
29645899ee36SVladimir Oltean 
29655899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
29665899ee36SVladimir Oltean {
29675899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29685899ee36SVladimir Oltean 	int rc;
29695899ee36SVladimir Oltean 
29705899ee36SVladimir Oltean 	rc = sja1105_vlan_del_one(ds, port, vid, &priv->dsa_8021q_vlans);
29715899ee36SVladimir Oltean 	if (!rc)
29725899ee36SVladimir Oltean 		return 0;
29735899ee36SVladimir Oltean 
29745899ee36SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
29755899ee36SVladimir Oltean }
29765899ee36SVladimir Oltean 
29775899ee36SVladimir Oltean static const struct dsa_8021q_ops sja1105_dsa_8021q_ops = {
29785899ee36SVladimir Oltean 	.vlan_add	= sja1105_dsa_8021q_vlan_add,
29795899ee36SVladimir Oltean 	.vlan_del	= sja1105_dsa_8021q_vlan_del,
29805899ee36SVladimir Oltean };
29815899ee36SVladimir Oltean 
29828aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
29838aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
29848aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
29858aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
29868aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
29878aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
29888aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
29898aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
29908aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
29918aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
29928aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
29938aa9ebccSVladimir Oltean  */
29948aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
29958aa9ebccSVladimir Oltean {
299682760d7fSVladimir Oltean 	struct sja1105_dt_port ports[SJA1105_MAX_NUM_PORTS];
29978aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29988aa9ebccSVladimir Oltean 	int rc;
29998aa9ebccSVladimir Oltean 
30008aa9ebccSVladimir Oltean 	rc = sja1105_parse_dt(priv, ports);
30018aa9ebccSVladimir Oltean 	if (rc < 0) {
30028aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
30038aa9ebccSVladimir Oltean 		return rc;
30048aa9ebccSVladimir Oltean 	}
3005f5b8631cSVladimir Oltean 
3006f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
3007f5b8631cSVladimir Oltean 	 * and we can't apply them.
3008f5b8631cSVladimir Oltean 	 */
3009f5b8631cSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv, ports);
3010f5b8631cSVladimir Oltean 	if (rc < 0) {
3011f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
3012f5b8631cSVladimir Oltean 		return rc;
3013f5b8631cSVladimir Oltean 	}
3014f5b8631cSVladimir Oltean 
301561c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
3016bb77f36aSVladimir Oltean 	if (rc < 0) {
3017bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
3018bb77f36aSVladimir Oltean 		return rc;
3019bb77f36aSVladimir Oltean 	}
30208aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
30218aa9ebccSVladimir Oltean 	rc = sja1105_static_config_load(priv, ports);
30228aa9ebccSVladimir Oltean 	if (rc < 0) {
30238aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
3024cec279a8SVladimir Oltean 		goto out_ptp_clock_unregister;
30258aa9ebccSVladimir Oltean 	}
30268aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
3027c5037678SVladimir Oltean 	rc = priv->info->clocking_setup(priv);
30288aa9ebccSVladimir Oltean 	if (rc < 0) {
30298aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
3030cec279a8SVladimir Oltean 		goto out_static_config_free;
30318aa9ebccSVladimir Oltean 	}
30326666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
30336666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
30346666cebcSVladimir Oltean 	 * EtherType is.
30356666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
30366666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
30376666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
30386666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
30396666cebcSVladimir Oltean 	 */
30406666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
30418aa9ebccSVladimir Oltean 
30425f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
30435f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
30445f06c63bSVladimir Oltean 
3045c279c726SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
3046c279c726SVladimir Oltean 
30478841f6e6SVladimir Oltean 	priv->best_effort_vlan_filtering = true;
30488841f6e6SVladimir Oltean 
30490a7bdbc2SVladimir Oltean 	rc = sja1105_devlink_setup(ds);
30502cafa72eSVladimir Oltean 	if (rc < 0)
3051cec279a8SVladimir Oltean 		goto out_static_config_free;
30522cafa72eSVladimir Oltean 
3053227d07a0SVladimir Oltean 	/* The DSA/switchdev model brings up switch ports in standalone mode by
3054227d07a0SVladimir Oltean 	 * default, and that means vlan_filtering is 0 since they're not under
3055227d07a0SVladimir Oltean 	 * a bridge, so it's safe to set up switch tagging at this time.
3056227d07a0SVladimir Oltean 	 */
3057bbed0bbdSVladimir Oltean 	rtnl_lock();
3058bbed0bbdSVladimir Oltean 	rc = sja1105_setup_8021q_tagging(ds, true);
3059bbed0bbdSVladimir Oltean 	rtnl_unlock();
3060cec279a8SVladimir Oltean 	if (rc)
3061cec279a8SVladimir Oltean 		goto out_devlink_teardown;
3062cec279a8SVladimir Oltean 
3063cec279a8SVladimir Oltean 	return 0;
3064cec279a8SVladimir Oltean 
3065cec279a8SVladimir Oltean out_devlink_teardown:
3066cec279a8SVladimir Oltean 	sja1105_devlink_teardown(ds);
3067cec279a8SVladimir Oltean out_ptp_clock_unregister:
3068cec279a8SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
3069cec279a8SVladimir Oltean out_static_config_free:
3070cec279a8SVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
3071bbed0bbdSVladimir Oltean 
3072bbed0bbdSVladimir Oltean 	return rc;
3073227d07a0SVladimir Oltean }
3074227d07a0SVladimir Oltean 
3075f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
3076f3097be2SVladimir Oltean {
3077f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3078ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v, *n;
3079a68578c2SVladimir Oltean 	int port;
3080a68578c2SVladimir Oltean 
3081542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
3082a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3083a68578c2SVladimir Oltean 
3084a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3085a68578c2SVladimir Oltean 			continue;
3086a68578c2SVladimir Oltean 
308752c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
3088a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
3089a68578c2SVladimir Oltean 	}
3090f3097be2SVladimir Oltean 
30910a7bdbc2SVladimir Oltean 	sja1105_devlink_teardown(ds);
3092a6af7763SVladimir Oltean 	sja1105_flower_teardown(ds);
3093317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
309461c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
30956cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
3096ec5ae610SVladimir Oltean 
3097ec5ae610SVladimir Oltean 	list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) {
3098ec5ae610SVladimir Oltean 		list_del(&v->list);
3099ec5ae610SVladimir Oltean 		kfree(v);
3100ec5ae610SVladimir Oltean 	}
3101ec5ae610SVladimir Oltean 
3102ec5ae610SVladimir Oltean 	list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) {
3103ec5ae610SVladimir Oltean 		list_del(&v->list);
3104ec5ae610SVladimir Oltean 		kfree(v);
3105ec5ae610SVladimir Oltean 	}
3106f3097be2SVladimir Oltean }
3107f3097be2SVladimir Oltean 
3108a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
3109a68578c2SVladimir Oltean {
3110a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3111a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
3112a68578c2SVladimir Oltean 
3113a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
3114a68578c2SVladimir Oltean 		return;
3115a68578c2SVladimir Oltean 
3116a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
3117a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
3118a68578c2SVladimir Oltean }
3119a68578c2SVladimir Oltean 
3120227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
312147ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
3122227d07a0SVladimir Oltean {
3123227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
3124227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3125227d07a0SVladimir Oltean 	struct ethhdr *hdr;
3126227d07a0SVladimir Oltean 	int timeout = 10;
3127227d07a0SVladimir Oltean 	int rc;
3128227d07a0SVladimir Oltean 
3129227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
3130227d07a0SVladimir Oltean 
3131227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
3132227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
3133227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
313447ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
313547ed985eSVladimir Oltean 	mgmt_route.takets = takets;
3136227d07a0SVladimir Oltean 
3137227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3138227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
3139227d07a0SVladimir Oltean 	if (rc < 0) {
3140227d07a0SVladimir Oltean 		kfree_skb(skb);
3141227d07a0SVladimir Oltean 		return rc;
3142227d07a0SVladimir Oltean 	}
3143227d07a0SVladimir Oltean 
3144227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
314568bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
3146227d07a0SVladimir Oltean 
3147227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
3148227d07a0SVladimir Oltean 	do {
3149227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
3150227d07a0SVladimir Oltean 						 slot, &mgmt_route);
3151227d07a0SVladimir Oltean 		if (rc < 0) {
3152227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
3153227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
3154227d07a0SVladimir Oltean 			continue;
3155227d07a0SVladimir Oltean 		}
3156227d07a0SVladimir Oltean 
3157227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
3158227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
3159227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
3160227d07a0SVladimir Oltean 		 */
3161227d07a0SVladimir Oltean 		cpu_relax();
3162227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
3163227d07a0SVladimir Oltean 
3164227d07a0SVladimir Oltean 	if (!timeout) {
3165227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
3166227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
31672a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
31682a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
3169227d07a0SVladimir Oltean 		 */
3170227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3171227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
3172227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
3173227d07a0SVladimir Oltean 	}
3174227d07a0SVladimir Oltean 
3175227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
3176227d07a0SVladimir Oltean }
3177227d07a0SVladimir Oltean 
3178a68578c2SVladimir Oltean #define work_to_port(work) \
3179a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
3180a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
3181a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
3182a68578c2SVladimir Oltean 
3183227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
3184227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
3185227d07a0SVladimir Oltean  * lock on the bus)
3186227d07a0SVladimir Oltean  */
3187a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
3188227d07a0SVladimir Oltean {
3189a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
3190a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
3191a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
3192a68578c2SVladimir Oltean 	int port = sp - priv->ports;
3193a68578c2SVladimir Oltean 	struct sk_buff *skb;
3194a68578c2SVladimir Oltean 
3195a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
3196c4b364ceSYangbo Lu 		struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
3197227d07a0SVladimir Oltean 
3198227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
3199227d07a0SVladimir Oltean 
3200a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
3201a68578c2SVladimir Oltean 
320247ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
3203a68578c2SVladimir Oltean 		if (clone)
3204a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
3205227d07a0SVladimir Oltean 
3206227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
3207a68578c2SVladimir Oltean 	}
32088aa9ebccSVladimir Oltean }
32098aa9ebccSVladimir Oltean 
32108456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
32118456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
32128456721dSVladimir Oltean  */
32138456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
32148456721dSVladimir Oltean 				   unsigned int ageing_time)
32158456721dSVladimir Oltean {
32168456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
32178456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
32188456721dSVladimir Oltean 	struct sja1105_table *table;
32198456721dSVladimir Oltean 	unsigned int maxage;
32208456721dSVladimir Oltean 
32218456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
32228456721dSVladimir Oltean 	l2_lookup_params = table->entries;
32238456721dSVladimir Oltean 
32248456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
32258456721dSVladimir Oltean 
32268456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
32278456721dSVladimir Oltean 		return 0;
32288456721dSVladimir Oltean 
32298456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
32308456721dSVladimir Oltean 
32312eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
32328456721dSVladimir Oltean }
32338456721dSVladimir Oltean 
3234c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
3235c279c726SVladimir Oltean {
3236c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
3237c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3238c279c726SVladimir Oltean 
3239c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
3240c279c726SVladimir Oltean 
3241c279c726SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
3242c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
3243c279c726SVladimir Oltean 
3244c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3245c279c726SVladimir Oltean 
3246a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
3247c279c726SVladimir Oltean 		return 0;
3248c279c726SVladimir Oltean 
3249a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
3250c279c726SVladimir Oltean 
3251c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3252c279c726SVladimir Oltean }
3253c279c726SVladimir Oltean 
3254c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
3255c279c726SVladimir Oltean {
3256c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
3257c279c726SVladimir Oltean }
3258c279c726SVladimir Oltean 
3259317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
3260317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
3261317ab5b8SVladimir Oltean 				 void *type_data)
3262317ab5b8SVladimir Oltean {
3263317ab5b8SVladimir Oltean 	switch (type) {
3264317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
3265317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
32664d752508SVladimir Oltean 	case TC_SETUP_QDISC_CBS:
32674d752508SVladimir Oltean 		return sja1105_setup_tc_cbs(ds, port, type_data);
3268317ab5b8SVladimir Oltean 	default:
3269317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
3270317ab5b8SVladimir Oltean 	}
3271317ab5b8SVladimir Oltean }
3272317ab5b8SVladimir Oltean 
3273511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
3274511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
3275511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
3276511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
3277511e6ca0SVladimir Oltean  * mirroring rule that references it.
3278511e6ca0SVladimir Oltean  */
3279511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
3280511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
3281511e6ca0SVladimir Oltean {
3282511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
3283511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
3284542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
3285511e6ca0SVladimir Oltean 	struct sja1105_table *table;
3286511e6ca0SVladimir Oltean 	bool already_enabled;
3287511e6ca0SVladimir Oltean 	u64 new_mirr_port;
3288511e6ca0SVladimir Oltean 	int rc;
3289511e6ca0SVladimir Oltean 
3290511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
3291511e6ca0SVladimir Oltean 	general_params = table->entries;
3292511e6ca0SVladimir Oltean 
3293511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
3294511e6ca0SVladimir Oltean 
3295542043e9SVladimir Oltean 	already_enabled = (general_params->mirr_port != ds->num_ports);
3296511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
3297511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
3298511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
3299511e6ca0SVladimir Oltean 			general_params->mirr_port);
3300511e6ca0SVladimir Oltean 		return -EBUSY;
3301511e6ca0SVladimir Oltean 	}
3302511e6ca0SVladimir Oltean 
3303511e6ca0SVladimir Oltean 	new_mirr_port = to;
3304511e6ca0SVladimir Oltean 	if (!enabled) {
3305511e6ca0SVladimir Oltean 		bool keep = false;
3306511e6ca0SVladimir Oltean 		int port;
3307511e6ca0SVladimir Oltean 
3308511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
3309542043e9SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
3310511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
3311511e6ca0SVladimir Oltean 				keep = true;
3312511e6ca0SVladimir Oltean 				break;
3313511e6ca0SVladimir Oltean 			}
3314511e6ca0SVladimir Oltean 		}
3315511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
3316511e6ca0SVladimir Oltean 		if (!keep)
3317542043e9SVladimir Oltean 			new_mirr_port = ds->num_ports;
3318511e6ca0SVladimir Oltean 	}
3319511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
3320511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
3321511e6ca0SVladimir Oltean 
3322511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
3323511e6ca0SVladimir Oltean 						  0, general_params, true);
3324511e6ca0SVladimir Oltean 		if (rc < 0)
3325511e6ca0SVladimir Oltean 			return rc;
3326511e6ca0SVladimir Oltean 	}
3327511e6ca0SVladimir Oltean 
3328511e6ca0SVladimir Oltean 	if (ingress)
3329511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
3330511e6ca0SVladimir Oltean 	else
3331511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
3332511e6ca0SVladimir Oltean 
3333511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
3334511e6ca0SVladimir Oltean 					    &mac[from], true);
3335511e6ca0SVladimir Oltean }
3336511e6ca0SVladimir Oltean 
3337511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
3338511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
3339511e6ca0SVladimir Oltean 			      bool ingress)
3340511e6ca0SVladimir Oltean {
3341511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3342511e6ca0SVladimir Oltean 				    ingress, true);
3343511e6ca0SVladimir Oltean }
3344511e6ca0SVladimir Oltean 
3345511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
3346511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
3347511e6ca0SVladimir Oltean {
3348511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3349511e6ca0SVladimir Oltean 			     mirror->ingress, false);
3350511e6ca0SVladimir Oltean }
3351511e6ca0SVladimir Oltean 
3352a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
3353a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
3354a7cc081cSVladimir Oltean {
3355a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
3356a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3357a7cc081cSVladimir Oltean 
3358a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3359a7cc081cSVladimir Oltean 
3360a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
3361a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
3362a7cc081cSVladimir Oltean 	 * bytes.
3363a7cc081cSVladimir Oltean 	 */
3364a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
3365a7cc081cSVladimir Oltean 				      1000000);
33665f035af7SPo Liu 	policing[port].smax = policer->burst;
3367a7cc081cSVladimir Oltean 
3368a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3369a7cc081cSVladimir Oltean }
3370a7cc081cSVladimir Oltean 
3371a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
3372a7cc081cSVladimir Oltean {
3373a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
3374a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3375a7cc081cSVladimir Oltean 
3376a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3377a7cc081cSVladimir Oltean 
3378a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
3379a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
3380a7cc081cSVladimir Oltean 
3381a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3382a7cc081cSVladimir Oltean }
3383a7cc081cSVladimir Oltean 
33844d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port,
33854d942354SVladimir Oltean 				     bool enabled)
33864d942354SVladimir Oltean {
33874d942354SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
33884d942354SVladimir Oltean 	int rc;
33894d942354SVladimir Oltean 
33904d942354SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
33914d942354SVladimir Oltean 
33924c44fc5eSVladimir Oltean 	mac[port].dyn_learn = enabled;
33934d942354SVladimir Oltean 
33944d942354SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
33954d942354SVladimir Oltean 					  &mac[port], true);
33964d942354SVladimir Oltean 	if (rc)
33974d942354SVladimir Oltean 		return rc;
33984d942354SVladimir Oltean 
33994d942354SVladimir Oltean 	if (enabled)
34004d942354SVladimir Oltean 		priv->learn_ena |= BIT(port);
34014d942354SVladimir Oltean 	else
34024d942354SVladimir Oltean 		priv->learn_ena &= ~BIT(port);
34034d942354SVladimir Oltean 
34044d942354SVladimir Oltean 	return 0;
34054d942354SVladimir Oltean }
34064d942354SVladimir Oltean 
34074d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to,
34084d942354SVladimir Oltean 					  struct switchdev_brport_flags flags)
34094d942354SVladimir Oltean {
34104d942354SVladimir Oltean 	if (flags.mask & BR_FLOOD) {
34114d942354SVladimir Oltean 		if (flags.val & BR_FLOOD)
34127f7ccdeaSVladimir Oltean 			priv->ucast_egress_floods |= BIT(to);
34134d942354SVladimir Oltean 		else
34146a5166e0SVladimir Oltean 			priv->ucast_egress_floods &= ~BIT(to);
34154d942354SVladimir Oltean 	}
34167f7ccdeaSVladimir Oltean 
34174d942354SVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD) {
34184d942354SVladimir Oltean 		if (flags.val & BR_BCAST_FLOOD)
34197f7ccdeaSVladimir Oltean 			priv->bcast_egress_floods |= BIT(to);
34204d942354SVladimir Oltean 		else
34216a5166e0SVladimir Oltean 			priv->bcast_egress_floods &= ~BIT(to);
34224d942354SVladimir Oltean 	}
34234d942354SVladimir Oltean 
34247f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
34254d942354SVladimir Oltean }
34264d942354SVladimir Oltean 
34274d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to,
34284d942354SVladimir Oltean 				    struct switchdev_brport_flags flags,
34294d942354SVladimir Oltean 				    struct netlink_ext_ack *extack)
34304d942354SVladimir Oltean {
34314d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
34324d942354SVladimir Oltean 	struct sja1105_table *table;
34334d942354SVladimir Oltean 	int match;
34344d942354SVladimir Oltean 
34354d942354SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
34364d942354SVladimir Oltean 	l2_lookup = table->entries;
34374d942354SVladimir Oltean 
34384d942354SVladimir Oltean 	for (match = 0; match < table->entry_count; match++)
34394d942354SVladimir Oltean 		if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST &&
34404d942354SVladimir Oltean 		    l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
34414d942354SVladimir Oltean 			break;
34424d942354SVladimir Oltean 
34434d942354SVladimir Oltean 	if (match == table->entry_count) {
34444d942354SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
34454d942354SVladimir Oltean 				   "Could not find FDB entry for unknown multicast");
34464d942354SVladimir Oltean 		return -ENOSPC;
34474d942354SVladimir Oltean 	}
34484d942354SVladimir Oltean 
34494d942354SVladimir Oltean 	if (flags.val & BR_MCAST_FLOOD)
34504d942354SVladimir Oltean 		l2_lookup[match].destports |= BIT(to);
34514d942354SVladimir Oltean 	else
34524d942354SVladimir Oltean 		l2_lookup[match].destports &= ~BIT(to);
34534d942354SVladimir Oltean 
34544d942354SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
34554d942354SVladimir Oltean 					    l2_lookup[match].index,
34564d942354SVladimir Oltean 					    &l2_lookup[match],
34574d942354SVladimir Oltean 					    true);
34584d942354SVladimir Oltean }
34594d942354SVladimir Oltean 
34604d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port,
34614d942354SVladimir Oltean 					 struct switchdev_brport_flags flags,
34624d942354SVladimir Oltean 					 struct netlink_ext_ack *extack)
34634d942354SVladimir Oltean {
34644d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
34654d942354SVladimir Oltean 
34664d942354SVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
34674d942354SVladimir Oltean 			   BR_BCAST_FLOOD))
34684d942354SVladimir Oltean 		return -EINVAL;
34694d942354SVladimir Oltean 
34704d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) &&
34714d942354SVladimir Oltean 	    !priv->info->can_limit_mcast_flood) {
34724d942354SVladimir Oltean 		bool multicast = !!(flags.val & BR_MCAST_FLOOD);
34734d942354SVladimir Oltean 		bool unicast = !!(flags.val & BR_FLOOD);
34744d942354SVladimir Oltean 
34754d942354SVladimir Oltean 		if (unicast != multicast) {
34764d942354SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
34774d942354SVladimir Oltean 					   "This chip cannot configure multicast flooding independently of unicast");
34784d942354SVladimir Oltean 			return -EINVAL;
34794d942354SVladimir Oltean 		}
34804d942354SVladimir Oltean 	}
34814d942354SVladimir Oltean 
34824d942354SVladimir Oltean 	return 0;
34834d942354SVladimir Oltean }
34844d942354SVladimir Oltean 
34854d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port,
34864d942354SVladimir Oltean 				     struct switchdev_brport_flags flags,
34874d942354SVladimir Oltean 				     struct netlink_ext_ack *extack)
34884d942354SVladimir Oltean {
34894d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
34904d942354SVladimir Oltean 	int rc;
34914d942354SVladimir Oltean 
34924d942354SVladimir Oltean 	if (flags.mask & BR_LEARNING) {
34934d942354SVladimir Oltean 		bool learn_ena = !!(flags.val & BR_LEARNING);
34944d942354SVladimir Oltean 
34954d942354SVladimir Oltean 		rc = sja1105_port_set_learning(priv, port, learn_ena);
34964d942354SVladimir Oltean 		if (rc)
34974d942354SVladimir Oltean 			return rc;
34984d942354SVladimir Oltean 	}
34994d942354SVladimir Oltean 
35004d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) {
35014d942354SVladimir Oltean 		rc = sja1105_port_ucast_bcast_flood(priv, port, flags);
35024d942354SVladimir Oltean 		if (rc)
35034d942354SVladimir Oltean 			return rc;
35044d942354SVladimir Oltean 	}
35054d942354SVladimir Oltean 
35064d942354SVladimir Oltean 	/* For chips that can't offload BR_MCAST_FLOOD independently, there
35074d942354SVladimir Oltean 	 * is nothing to do here, we ensured the configuration is in sync by
35084d942354SVladimir Oltean 	 * offloading BR_FLOOD.
35094d942354SVladimir Oltean 	 */
35104d942354SVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) {
35114d942354SVladimir Oltean 		rc = sja1105_port_mcast_flood(priv, port, flags,
35124d942354SVladimir Oltean 					      extack);
35134d942354SVladimir Oltean 		if (rc)
35144d942354SVladimir Oltean 			return rc;
35154d942354SVladimir Oltean 	}
35164d942354SVladimir Oltean 
35174d942354SVladimir Oltean 	return 0;
35184d942354SVladimir Oltean }
35194d942354SVladimir Oltean 
35208aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
35218aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
35228aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
3523f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
35248456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
3525c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
3526c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
3527ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
3528ffe10e67SVladimir Oltean 	.phylink_mac_link_state	= sja1105_mac_pcs_get_state,
3529af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
35308400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
35318400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
353252c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
353352c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
353452c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
3535bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
3536a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
3537291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
3538291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
3539291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
35408aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
35418aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
35424d942354SVladimir Oltean 	.port_pre_bridge_flags	= sja1105_port_pre_bridge_flags,
35434d942354SVladimir Oltean 	.port_bridge_flags	= sja1105_port_bridge_flags,
3544640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
35456666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
35466666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
35476666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
3548291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
3549291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
3550a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
3551a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
3552f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
355347ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
3554317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
3555511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
3556511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
3557a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
3558a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
3559a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
3560a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
3561834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
3562ac02a451SVladimir Oltean 	.crosschip_bridge_join	= sja1105_crosschip_bridge_join,
3563ac02a451SVladimir Oltean 	.crosschip_bridge_leave	= sja1105_crosschip_bridge_leave,
35642cafa72eSVladimir Oltean 	.devlink_param_get	= sja1105_devlink_param_get,
35652cafa72eSVladimir Oltean 	.devlink_param_set	= sja1105_devlink_param_set,
3566ff4cf8eaSVladimir Oltean 	.devlink_info_get	= sja1105_devlink_info_get,
35678aa9ebccSVladimir Oltean };
35688aa9ebccSVladimir Oltean 
35690b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[];
35700b0e2997SVladimir Oltean 
35718aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
35728aa9ebccSVladimir Oltean {
35738aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
35748aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
35758aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
35760b0e2997SVladimir Oltean 	const struct of_device_id *match;
3577dff79620SVladimir Oltean 	u32 device_id;
35788aa9ebccSVladimir Oltean 	u64 part_no;
35798aa9ebccSVladimir Oltean 	int rc;
35808aa9ebccSVladimir Oltean 
358134d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
358234d76e9fSVladimir Oltean 			      NULL);
35838aa9ebccSVladimir Oltean 	if (rc < 0)
35848aa9ebccSVladimir Oltean 		return rc;
35858aa9ebccSVladimir Oltean 
35861bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
35871bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
35888aa9ebccSVladimir Oltean 	if (rc < 0)
35898aa9ebccSVladimir Oltean 		return rc;
35908aa9ebccSVladimir Oltean 
35918aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
35928aa9ebccSVladimir Oltean 
35935978fac0SNathan Chancellor 	for (match = sja1105_dt_ids; match->compatible[0]; match++) {
35940b0e2997SVladimir Oltean 		const struct sja1105_info *info = match->data;
35950b0e2997SVladimir Oltean 
35960b0e2997SVladimir Oltean 		/* Is what's been probed in our match table at all? */
35970b0e2997SVladimir Oltean 		if (info->device_id != device_id || info->part_no != part_no)
35980b0e2997SVladimir Oltean 			continue;
35990b0e2997SVladimir Oltean 
36000b0e2997SVladimir Oltean 		/* But is it what's in the device tree? */
36010b0e2997SVladimir Oltean 		if (priv->info->device_id != device_id ||
36020b0e2997SVladimir Oltean 		    priv->info->part_no != part_no) {
36030b0e2997SVladimir Oltean 			dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
36040b0e2997SVladimir Oltean 				 priv->info->name, info->name);
36050b0e2997SVladimir Oltean 			/* It isn't. No problem, pick that up. */
36060b0e2997SVladimir Oltean 			priv->info = info;
36078aa9ebccSVladimir Oltean 		}
36088aa9ebccSVladimir Oltean 
36098aa9ebccSVladimir Oltean 		return 0;
36108aa9ebccSVladimir Oltean 	}
36118aa9ebccSVladimir Oltean 
36120b0e2997SVladimir Oltean 	dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
36130b0e2997SVladimir Oltean 		device_id, part_no);
36140b0e2997SVladimir Oltean 
36150b0e2997SVladimir Oltean 	return -ENODEV;
36160b0e2997SVladimir Oltean }
36170b0e2997SVladimir Oltean 
36188aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
36198aa9ebccSVladimir Oltean {
3620844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
36218aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
36228aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
3623718bad0eSVladimir Oltean 	size_t max_xfer, max_msg;
36248aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
3625a68578c2SVladimir Oltean 	int rc, port;
36268aa9ebccSVladimir Oltean 
36278aa9ebccSVladimir Oltean 	if (!dev->of_node) {
36288aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
36298aa9ebccSVladimir Oltean 		return -EINVAL;
36308aa9ebccSVladimir Oltean 	}
36318aa9ebccSVladimir Oltean 
36328aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
36338aa9ebccSVladimir Oltean 	if (!priv)
36348aa9ebccSVladimir Oltean 		return -ENOMEM;
36358aa9ebccSVladimir Oltean 
36368aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
36378aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
36388aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
36398aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
36408aa9ebccSVladimir Oltean 	else
36418aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
36428aa9ebccSVladimir Oltean 
36438aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
36448aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
36458aa9ebccSVladimir Oltean 	 */
36468aa9ebccSVladimir Oltean 	priv->spidev = spi;
36478aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
36488aa9ebccSVladimir Oltean 
36498aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
36508aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
36518aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
36528aa9ebccSVladimir Oltean 	if (rc < 0) {
36538aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
36548aa9ebccSVladimir Oltean 		return rc;
36558aa9ebccSVladimir Oltean 	}
36568aa9ebccSVladimir Oltean 
3657718bad0eSVladimir Oltean 	/* In sja1105_xfer, we send spi_messages composed of two spi_transfers:
3658718bad0eSVladimir Oltean 	 * a small one for the message header and another one for the current
3659718bad0eSVladimir Oltean 	 * chunk of the packed buffer.
3660718bad0eSVladimir Oltean 	 * Check that the restrictions imposed by the SPI controller are
3661718bad0eSVladimir Oltean 	 * respected: the chunk buffer is smaller than the max transfer size,
3662718bad0eSVladimir Oltean 	 * and the total length of the chunk plus its message header is smaller
3663718bad0eSVladimir Oltean 	 * than the max message size.
3664718bad0eSVladimir Oltean 	 * We do that during probe time since the maximum transfer size is a
3665718bad0eSVladimir Oltean 	 * runtime invariant.
3666718bad0eSVladimir Oltean 	 */
3667718bad0eSVladimir Oltean 	max_xfer = spi_max_transfer_size(spi);
3668718bad0eSVladimir Oltean 	max_msg = spi_max_message_size(spi);
3669718bad0eSVladimir Oltean 
3670718bad0eSVladimir Oltean 	/* We need to send at least one 64-bit word of SPI payload per message
3671718bad0eSVladimir Oltean 	 * in order to be able to make useful progress.
3672718bad0eSVladimir Oltean 	 */
3673718bad0eSVladimir Oltean 	if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) {
3674718bad0eSVladimir Oltean 		dev_err(dev, "SPI master cannot send large enough buffers, aborting\n");
3675718bad0eSVladimir Oltean 		return -EINVAL;
3676718bad0eSVladimir Oltean 	}
3677718bad0eSVladimir Oltean 
3678718bad0eSVladimir Oltean 	priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN;
3679718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_xfer)
3680718bad0eSVladimir Oltean 		priv->max_xfer_len = max_xfer;
3681718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER)
3682718bad0eSVladimir Oltean 		priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER;
3683718bad0eSVladimir Oltean 
36848aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
36858aa9ebccSVladimir Oltean 
36868aa9ebccSVladimir Oltean 	/* Detect hardware device */
36878aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
36888aa9ebccSVladimir Oltean 	if (rc < 0) {
36898aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
36908aa9ebccSVladimir Oltean 		return rc;
36918aa9ebccSVladimir Oltean 	}
36928aa9ebccSVladimir Oltean 
36938aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
36948aa9ebccSVladimir Oltean 
36957e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
36968aa9ebccSVladimir Oltean 	if (!ds)
36978aa9ebccSVladimir Oltean 		return -ENOMEM;
36988aa9ebccSVladimir Oltean 
36997e99e347SVivien Didelot 	ds->dev = dev;
370082760d7fSVladimir Oltean 	ds->num_ports = SJA1105_MAX_NUM_PORTS;
37018aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
37028aa9ebccSVladimir Oltean 	ds->priv = priv;
37038aa9ebccSVladimir Oltean 	priv->ds = ds;
37048aa9ebccSVladimir Oltean 
3705844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
3706844d7edcSVladimir Oltean 
3707d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
3708d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
3709d5a619bfSVivien Didelot 
37105899ee36SVladimir Oltean 	priv->dsa_8021q_ctx = devm_kzalloc(dev, sizeof(*priv->dsa_8021q_ctx),
37115899ee36SVladimir Oltean 					   GFP_KERNEL);
37125899ee36SVladimir Oltean 	if (!priv->dsa_8021q_ctx)
37135899ee36SVladimir Oltean 		return -ENOMEM;
37145899ee36SVladimir Oltean 
37155899ee36SVladimir Oltean 	priv->dsa_8021q_ctx->ops = &sja1105_dsa_8021q_ops;
3716bbed0bbdSVladimir Oltean 	priv->dsa_8021q_ctx->proto = htons(ETH_P_8021Q);
37175899ee36SVladimir Oltean 	priv->dsa_8021q_ctx->ds = ds;
37185899ee36SVladimir Oltean 
37195899ee36SVladimir Oltean 	INIT_LIST_HEAD(&priv->dsa_8021q_ctx->crosschip_links);
3720ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&priv->bridge_vlans);
3721ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&priv->dsa_8021q_vlans);
3722ac02a451SVladimir Oltean 
3723d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
3724a6af7763SVladimir Oltean 	sja1105_flower_setup(ds);
3725d5a619bfSVivien Didelot 
3726d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
3727d5a619bfSVivien Didelot 	if (rc)
3728d5a619bfSVivien Didelot 		return rc;
3729d5a619bfSVivien Didelot 
37304d752508SVladimir Oltean 	if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
37314d752508SVladimir Oltean 		priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
37324d752508SVladimir Oltean 					 sizeof(struct sja1105_cbs_entry),
37334d752508SVladimir Oltean 					 GFP_KERNEL);
3734dc596e3fSVladimir Oltean 		if (!priv->cbs) {
3735dc596e3fSVladimir Oltean 			rc = -ENOMEM;
3736dc596e3fSVladimir Oltean 			goto out_unregister_switch;
3737dc596e3fSVladimir Oltean 		}
37384d752508SVladimir Oltean 	}
37394d752508SVladimir Oltean 
3740227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
3741542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
3742a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3743a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
3744a68578c2SVladimir Oltean 		struct net_device *slave;
374584eeb5d4SVladimir Oltean 		int subvlan;
3746227d07a0SVladimir Oltean 
3747a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3748a68578c2SVladimir Oltean 			continue;
3749a68578c2SVladimir Oltean 
3750a68578c2SVladimir Oltean 		dp->priv = sp;
3751a68578c2SVladimir Oltean 		sp->dp = dp;
3752844d7edcSVladimir Oltean 		sp->data = tagger_data;
3753a68578c2SVladimir Oltean 		slave = dp->slave;
3754a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
3755a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
3756a68578c2SVladimir Oltean 							slave->name);
3757a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
3758a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
3759a68578c2SVladimir Oltean 			dev_err(ds->dev,
3760a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
3761a68578c2SVladimir Oltean 				rc);
3762dc596e3fSVladimir Oltean 			goto out_destroy_workers;
3763a68578c2SVladimir Oltean 		}
3764a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
376538b5beeaSVladimir Oltean 		sp->xmit_tpid = ETH_P_SJA1105;
376684eeb5d4SVladimir Oltean 
376784eeb5d4SVladimir Oltean 		for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
376884eeb5d4SVladimir Oltean 			sp->subvlan_map[subvlan] = VLAN_N_VID;
3769227d07a0SVladimir Oltean 	}
3770227d07a0SVladimir Oltean 
3771d5a619bfSVivien Didelot 	return 0;
3772dc596e3fSVladimir Oltean 
3773dc596e3fSVladimir Oltean out_destroy_workers:
3774a68578c2SVladimir Oltean 	while (port-- > 0) {
3775a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3776a68578c2SVladimir Oltean 
3777a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3778a68578c2SVladimir Oltean 			continue;
3779a68578c2SVladimir Oltean 
3780a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
3781a68578c2SVladimir Oltean 	}
3782dc596e3fSVladimir Oltean 
3783dc596e3fSVladimir Oltean out_unregister_switch:
3784dc596e3fSVladimir Oltean 	dsa_unregister_switch(ds);
3785dc596e3fSVladimir Oltean 
3786a68578c2SVladimir Oltean 	return rc;
37878aa9ebccSVladimir Oltean }
37888aa9ebccSVladimir Oltean 
37898aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
37908aa9ebccSVladimir Oltean {
37918aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
37928aa9ebccSVladimir Oltean 
37938aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
37948aa9ebccSVladimir Oltean 	return 0;
37958aa9ebccSVladimir Oltean }
37968aa9ebccSVladimir Oltean 
37978aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
37988aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
37998aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
38008aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
38018aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
38028aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
38038aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
38048aa9ebccSVladimir Oltean 	{ /* sentinel */ },
38058aa9ebccSVladimir Oltean };
38068aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
38078aa9ebccSVladimir Oltean 
38088aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
38098aa9ebccSVladimir Oltean 	.driver = {
38108aa9ebccSVladimir Oltean 		.name  = "sja1105",
38118aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
38128aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
38138aa9ebccSVladimir Oltean 	},
38148aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
38158aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
38168aa9ebccSVladimir Oltean };
38178aa9ebccSVladimir Oltean 
38188aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
38198aa9ebccSVladimir Oltean 
38208aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
38218aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
38228aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
38238aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
3824