xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 3ad1d171548e85fd582c8de8c0946875579aebe8)
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>
19*3ad1d171SVladimir Oltean #include <linux/pcs/pcs-xpcs.h>
208aa9ebccSVladimir Oltean #include <linux/netdev_features.h>
218aa9ebccSVladimir Oltean #include <linux/netdevice.h>
228aa9ebccSVladimir Oltean #include <linux/if_bridge.h>
238aa9ebccSVladimir Oltean #include <linux/if_ether.h>
24227d07a0SVladimir Oltean #include <linux/dsa/8021q.h>
258aa9ebccSVladimir Oltean #include "sja1105.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 static int sja1105_init_mac_settings(struct sja1105_private *priv)
618aa9ebccSVladimir Oltean {
628aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
638aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
648aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
658aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
668aa9ebccSVladimir Oltean 		 */
678aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
688aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
698aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
708aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
718aa9ebccSVladimir Oltean 		.ifg = 0,
728aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
731fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
748aa9ebccSVladimir Oltean 		 */
7541fed17fSVladimir Oltean 		.speed = priv->info->port_speed[SJA1105_SPEED_AUTO],
768aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
778aa9ebccSVladimir Oltean 		.tp_delin = 0,
788aa9ebccSVladimir Oltean 		.tp_delout = 0,
798aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
808aa9ebccSVladimir Oltean 		.maxage = 0xFF,
818aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
828aa9ebccSVladimir Oltean 		.vlanprio = 0,
83e3502b82SVladimir Oltean 		.vlanid = 1,
848aa9ebccSVladimir Oltean 		.ing_mirr = false,
858aa9ebccSVladimir Oltean 		.egr_mirr = false,
868aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
878aa9ebccSVladimir Oltean 		.drpnona664 = false,
888aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
898aa9ebccSVladimir Oltean 		.drpdtag = false,
908aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
918aa9ebccSVladimir Oltean 		.drpuntag = false,
928aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
938aa9ebccSVladimir Oltean 		.retag = false,
94640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
95640f763fSVladimir Oltean 		 * STP will enable it.
96640f763fSVladimir Oltean 		 */
97640f763fSVladimir Oltean 		.dyn_learn = false,
988aa9ebccSVladimir Oltean 		.egress = false,
998aa9ebccSVladimir Oltean 		.ingress = false,
1008aa9ebccSVladimir Oltean 	};
1018aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
102542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
1038aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1048aa9ebccSVladimir Oltean 	int i;
1058aa9ebccSVladimir Oltean 
1068aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1078aa9ebccSVladimir Oltean 
1088aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1098aa9ebccSVladimir Oltean 	if (table->entry_count) {
1108aa9ebccSVladimir Oltean 		kfree(table->entries);
1118aa9ebccSVladimir Oltean 		table->entry_count = 0;
1128aa9ebccSVladimir Oltean 	}
1138aa9ebccSVladimir Oltean 
114fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
1158aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1168aa9ebccSVladimir Oltean 	if (!table->entries)
1178aa9ebccSVladimir Oltean 		return -ENOMEM;
1188aa9ebccSVladimir Oltean 
119fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
1208aa9ebccSVladimir Oltean 
1218aa9ebccSVladimir Oltean 	mac = table->entries;
1228aa9ebccSVladimir Oltean 
123542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1248aa9ebccSVladimir Oltean 		mac[i] = default_mac;
125640f763fSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, i)) {
126640f763fSVladimir Oltean 			/* STP doesn't get called for CPU port, so we need to
127640f763fSVladimir Oltean 			 * set the I/O parameters statically.
128640f763fSVladimir Oltean 			 */
129640f763fSVladimir Oltean 			mac[i].dyn_learn = true;
130640f763fSVladimir Oltean 			mac[i].ingress = true;
131640f763fSVladimir Oltean 			mac[i].egress = true;
132640f763fSVladimir Oltean 		}
133640f763fSVladimir Oltean 	}
1348aa9ebccSVladimir Oltean 
1358aa9ebccSVladimir Oltean 	return 0;
1368aa9ebccSVladimir Oltean }
1378aa9ebccSVladimir Oltean 
1385d645df9SVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv)
1398aa9ebccSVladimir Oltean {
1408aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1418aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
142542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
1438aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1448aa9ebccSVladimir Oltean 	int i;
1458aa9ebccSVladimir Oltean 
1468aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1478aa9ebccSVladimir Oltean 
1488aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1498aa9ebccSVladimir Oltean 	if (table->entry_count) {
1508aa9ebccSVladimir Oltean 		kfree(table->entries);
1518aa9ebccSVladimir Oltean 		table->entry_count = 0;
1528aa9ebccSVladimir Oltean 	}
1538aa9ebccSVladimir Oltean 
154fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
1558aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1568aa9ebccSVladimir Oltean 	if (!table->entries)
1578aa9ebccSVladimir Oltean 		return -ENOMEM;
1588aa9ebccSVladimir Oltean 
1591fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
160fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
1618aa9ebccSVladimir Oltean 
1628aa9ebccSVladimir Oltean 	mii = table->entries;
1638aa9ebccSVladimir Oltean 
164542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1655d645df9SVladimir Oltean 		sja1105_mii_role_t role = XMII_MAC;
1665d645df9SVladimir Oltean 
167ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
168ee9d0cb6SVladimir Oltean 			continue;
169ee9d0cb6SVladimir Oltean 
1705d645df9SVladimir Oltean 		switch (priv->phy_mode[i]) {
1715a8f0974SVladimir Oltean 		case PHY_INTERFACE_MODE_INTERNAL:
1725a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_NO_PHY)
1735a8f0974SVladimir Oltean 				goto unsupported;
1745a8f0974SVladimir Oltean 
1755a8f0974SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1765a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_PHY_BASE_TX)
1775a8f0974SVladimir Oltean 				mii->special[i] = true;
1785a8f0974SVladimir Oltean 
1795a8f0974SVladimir Oltean 			break;
1805d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVMII:
1815d645df9SVladimir Oltean 			role = XMII_PHY;
1825d645df9SVladimir Oltean 			fallthrough;
1838aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
18491a05078SVladimir Oltean 			if (!priv->info->supports_mii[i])
18591a05078SVladimir Oltean 				goto unsupported;
18691a05078SVladimir Oltean 
1878aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1888aa9ebccSVladimir Oltean 			break;
1895d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVRMII:
1905d645df9SVladimir Oltean 			role = XMII_PHY;
1915d645df9SVladimir Oltean 			fallthrough;
1928aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
19391a05078SVladimir Oltean 			if (!priv->info->supports_rmii[i])
19491a05078SVladimir Oltean 				goto unsupported;
19591a05078SVladimir Oltean 
1968aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
1978aa9ebccSVladimir Oltean 			break;
1988aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
1998aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
2008aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
2018aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
20291a05078SVladimir Oltean 			if (!priv->info->supports_rgmii[i])
20391a05078SVladimir Oltean 				goto unsupported;
20491a05078SVladimir Oltean 
2058aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
2068aa9ebccSVladimir Oltean 			break;
207ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
20891a05078SVladimir Oltean 			if (!priv->info->supports_sgmii[i])
20991a05078SVladimir Oltean 				goto unsupported;
21091a05078SVladimir Oltean 
211ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
212ffe10e67SVladimir Oltean 			break;
21391a05078SVladimir Oltean 		case PHY_INTERFACE_MODE_2500BASEX:
21491a05078SVladimir Oltean 			if (!priv->info->supports_2500basex[i])
21591a05078SVladimir Oltean 				goto unsupported;
21691a05078SVladimir Oltean 
21791a05078SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
21891a05078SVladimir Oltean 			break;
21991a05078SVladimir Oltean unsupported:
2208aa9ebccSVladimir Oltean 		default:
22191a05078SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d!\n",
2225d645df9SVladimir Oltean 				phy_modes(priv->phy_mode[i]), i);
2236729188dSVladimir Oltean 			return -EINVAL;
2248aa9ebccSVladimir Oltean 		}
2258aa9ebccSVladimir Oltean 
2265d645df9SVladimir Oltean 		mii->phy_mac[i] = role;
2278aa9ebccSVladimir Oltean 	}
2288aa9ebccSVladimir Oltean 	return 0;
2298aa9ebccSVladimir Oltean }
2308aa9ebccSVladimir Oltean 
2318aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
2328aa9ebccSVladimir Oltean {
2334d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
2348aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2354d942354SVladimir Oltean 	int port;
2368aa9ebccSVladimir Oltean 
2378aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
2388aa9ebccSVladimir Oltean 
2394d942354SVladimir Oltean 	/* We only populate the FDB table through dynamic L2 Address Lookup
2404d942354SVladimir Oltean 	 * entries, except for a special entry at the end which is a catch-all
2414d942354SVladimir Oltean 	 * for unknown multicast and will be used to control flooding domain.
242291d1e72SVladimir Oltean 	 */
2438aa9ebccSVladimir Oltean 	if (table->entry_count) {
2448aa9ebccSVladimir Oltean 		kfree(table->entries);
2458aa9ebccSVladimir Oltean 		table->entry_count = 0;
2468aa9ebccSVladimir Oltean 	}
2474d942354SVladimir Oltean 
2484d942354SVladimir Oltean 	if (!priv->info->can_limit_mcast_flood)
2494d942354SVladimir Oltean 		return 0;
2504d942354SVladimir Oltean 
2514d942354SVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
2524d942354SVladimir Oltean 				 GFP_KERNEL);
2534d942354SVladimir Oltean 	if (!table->entries)
2544d942354SVladimir Oltean 		return -ENOMEM;
2554d942354SVladimir Oltean 
2564d942354SVladimir Oltean 	table->entry_count = 1;
2574d942354SVladimir Oltean 	l2_lookup = table->entries;
2584d942354SVladimir Oltean 
2594d942354SVladimir Oltean 	/* All L2 multicast addresses have an odd first octet */
2604d942354SVladimir Oltean 	l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST;
2614d942354SVladimir Oltean 	l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST;
2624d942354SVladimir Oltean 	l2_lookup[0].lockeds = true;
2634d942354SVladimir Oltean 	l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1;
2644d942354SVladimir Oltean 
2654d942354SVladimir Oltean 	/* Flood multicast to every port by default */
2664d942354SVladimir Oltean 	for (port = 0; port < priv->ds->num_ports; port++)
2674d942354SVladimir Oltean 		if (!dsa_is_unused_port(priv->ds, port))
2684d942354SVladimir Oltean 			l2_lookup[0].destports |= BIT(port);
2694d942354SVladimir Oltean 
2708aa9ebccSVladimir Oltean 	return 0;
2718aa9ebccSVladimir Oltean }
2728aa9ebccSVladimir Oltean 
2738aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
2748aa9ebccSVladimir Oltean {
2758aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
2768456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
2778456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
2788aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
2798aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
2801da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
2811da73821SVladimir Oltean 		.start_dynspc = 0,
2828aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
2838aa9ebccSVladimir Oltean 		.poly = 0x97,
2848aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
2858aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
2868aa9ebccSVladimir Oltean 		 */
2876d7c7d94SVladimir Oltean 		.shared_learn = true,
2888aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
2898aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
2908aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
2918aa9ebccSVladimir Oltean 		 */
2928aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
2938aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
2948aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
2958aa9ebccSVladimir Oltean 		 */
2968aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
2971da73821SVladimir Oltean 		/* P/Q/R/S only */
2981da73821SVladimir Oltean 		.use_static = true,
2991da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
3001da73821SVladimir Oltean 		 * dynamic FDB entries
3011da73821SVladimir Oltean 		 */
3021da73821SVladimir Oltean 		.owr_dyn = true,
3031da73821SVladimir Oltean 		.drpnolearn = true,
3048aa9ebccSVladimir Oltean 	};
305542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
306f238fef1SVladimir Oltean 	int port, num_used_ports = 0;
307542043e9SVladimir Oltean 	struct sja1105_table *table;
308542043e9SVladimir Oltean 	u64 max_fdb_entries;
309542043e9SVladimir Oltean 
310542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++)
311f238fef1SVladimir Oltean 		if (!dsa_is_unused_port(ds, port))
312f238fef1SVladimir Oltean 			num_used_ports++;
313f238fef1SVladimir Oltean 
314f238fef1SVladimir Oltean 	max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports;
315f238fef1SVladimir Oltean 
316f238fef1SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
317f238fef1SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
318f238fef1SVladimir Oltean 			continue;
319f238fef1SVladimir Oltean 
320542043e9SVladimir Oltean 		default_l2_lookup_params.maxaddrp[port] = max_fdb_entries;
321f238fef1SVladimir Oltean 	}
3228aa9ebccSVladimir Oltean 
3238aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
3248aa9ebccSVladimir Oltean 
3258aa9ebccSVladimir Oltean 	if (table->entry_count) {
3268aa9ebccSVladimir Oltean 		kfree(table->entries);
3278aa9ebccSVladimir Oltean 		table->entry_count = 0;
3288aa9ebccSVladimir Oltean 	}
3298aa9ebccSVladimir Oltean 
330fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
3318aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3328aa9ebccSVladimir Oltean 	if (!table->entries)
3338aa9ebccSVladimir Oltean 		return -ENOMEM;
3348aa9ebccSVladimir Oltean 
335fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
3368aa9ebccSVladimir Oltean 
3378aa9ebccSVladimir Oltean 	/* This table only has a single entry */
3388aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
3398aa9ebccSVladimir Oltean 				default_l2_lookup_params;
3408aa9ebccSVladimir Oltean 
3418aa9ebccSVladimir Oltean 	return 0;
3428aa9ebccSVladimir Oltean }
3438aa9ebccSVladimir Oltean 
344ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU
345ed040abcSVladimir Oltean  * using management routes (e.g. STP, PTP) as opposed to tag_8021q.
346ed040abcSVladimir Oltean  * All DT-defined ports are members of this VLAN, and there are no
347ed040abcSVladimir Oltean  * restrictions on forwarding (since the CPU selects the destination).
348ed040abcSVladimir Oltean  * Frames from this VLAN will always be transmitted as untagged, and
349ed040abcSVladimir Oltean  * neither the bridge nor the 8021q module cannot create this VLAN ID.
350ed040abcSVladimir Oltean  */
3518aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
3528aa9ebccSVladimir Oltean {
3538aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3548aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
3553e77e59bSVladimir Oltean 		.type_entry = SJA1110_VLAN_D_TAG,
3568aa9ebccSVladimir Oltean 		.ving_mirr = 0,
3578aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
3588aa9ebccSVladimir Oltean 		.vmemb_port = 0,
3598aa9ebccSVladimir Oltean 		.vlan_bc = 0,
3608aa9ebccSVladimir Oltean 		.tag_port = 0,
361ed040abcSVladimir Oltean 		.vlanid = SJA1105_DEFAULT_VLAN,
3628aa9ebccSVladimir Oltean 	};
363ec5ae610SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
364ec5ae610SVladimir Oltean 	int port;
3658aa9ebccSVladimir Oltean 
3668aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
3678aa9ebccSVladimir Oltean 
3688aa9ebccSVladimir Oltean 	if (table->entry_count) {
3698aa9ebccSVladimir Oltean 		kfree(table->entries);
3708aa9ebccSVladimir Oltean 		table->entry_count = 0;
3718aa9ebccSVladimir Oltean 	}
3728aa9ebccSVladimir Oltean 
373c75857b0SZheng Yongjun 	table->entries = kzalloc(table->ops->unpacked_entry_size,
3748aa9ebccSVladimir Oltean 				 GFP_KERNEL);
3758aa9ebccSVladimir Oltean 	if (!table->entries)
3768aa9ebccSVladimir Oltean 		return -ENOMEM;
3778aa9ebccSVladimir Oltean 
3788aa9ebccSVladimir Oltean 	table->entry_count = 1;
3798aa9ebccSVladimir Oltean 
380ec5ae610SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
381ec5ae610SVladimir Oltean 		struct sja1105_bridge_vlan *v;
382ec5ae610SVladimir Oltean 
383ec5ae610SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
384ec5ae610SVladimir Oltean 			continue;
385ec5ae610SVladimir Oltean 
386ec5ae610SVladimir Oltean 		pvid.vmemb_port |= BIT(port);
387ec5ae610SVladimir Oltean 		pvid.vlan_bc |= BIT(port);
388ec5ae610SVladimir Oltean 		pvid.tag_port &= ~BIT(port);
389ec5ae610SVladimir Oltean 
390ec5ae610SVladimir Oltean 		v = kzalloc(sizeof(*v), GFP_KERNEL);
391ec5ae610SVladimir Oltean 		if (!v)
392ec5ae610SVladimir Oltean 			return -ENOMEM;
393ec5ae610SVladimir Oltean 
394ec5ae610SVladimir Oltean 		v->port = port;
395ed040abcSVladimir Oltean 		v->vid = SJA1105_DEFAULT_VLAN;
396ec5ae610SVladimir Oltean 		v->untagged = true;
397ec5ae610SVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
398ec5ae610SVladimir Oltean 			v->pvid = true;
399ec5ae610SVladimir Oltean 		list_add(&v->list, &priv->dsa_8021q_vlans);
4008aa9ebccSVladimir Oltean 	}
4018aa9ebccSVladimir Oltean 
4028aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
4038aa9ebccSVladimir Oltean 	return 0;
4048aa9ebccSVladimir Oltean }
4058aa9ebccSVladimir Oltean 
4068aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
4078aa9ebccSVladimir Oltean {
4088aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
409542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
4108aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4118aa9ebccSVladimir Oltean 	int i, j;
4128aa9ebccSVladimir Oltean 
4138aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
4148aa9ebccSVladimir Oltean 
4158aa9ebccSVladimir Oltean 	if (table->entry_count) {
4168aa9ebccSVladimir Oltean 		kfree(table->entries);
4178aa9ebccSVladimir Oltean 		table->entry_count = 0;
4188aa9ebccSVladimir Oltean 	}
4198aa9ebccSVladimir Oltean 
420fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4218aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4228aa9ebccSVladimir Oltean 	if (!table->entries)
4238aa9ebccSVladimir Oltean 		return -ENOMEM;
4248aa9ebccSVladimir Oltean 
425fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
4268aa9ebccSVladimir Oltean 
4278aa9ebccSVladimir Oltean 	l2fwd = table->entries;
4288aa9ebccSVladimir Oltean 
4298aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
430542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
4318aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
4328aa9ebccSVladimir Oltean 
433f238fef1SVladimir Oltean 		if (dsa_is_unused_port(ds, i))
434f238fef1SVladimir Oltean 			continue;
435f238fef1SVladimir Oltean 
4368aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
4378aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
4388aa9ebccSVladimir Oltean 
4397f7ccdeaSVladimir Oltean 		/* All ports start up with egress flooding enabled,
4407f7ccdeaSVladimir Oltean 		 * including the CPU port.
4417f7ccdeaSVladimir Oltean 		 */
4427f7ccdeaSVladimir Oltean 		priv->ucast_egress_floods |= BIT(i);
4437f7ccdeaSVladimir Oltean 		priv->bcast_egress_floods |= BIT(i);
4447f7ccdeaSVladimir Oltean 
4458aa9ebccSVladimir Oltean 		if (i == upstream)
4468aa9ebccSVladimir Oltean 			continue;
4478aa9ebccSVladimir Oltean 
4488aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
4498aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
4504d942354SVladimir Oltean 
4514d942354SVladimir Oltean 		l2fwd[i].bc_domain = BIT(upstream);
4524d942354SVladimir Oltean 		l2fwd[i].fl_domain = BIT(upstream);
4534d942354SVladimir Oltean 
4544d942354SVladimir Oltean 		l2fwd[upstream].bc_domain |= BIT(i);
4554d942354SVladimir Oltean 		l2fwd[upstream].fl_domain |= BIT(i);
4568aa9ebccSVladimir Oltean 	}
457f238fef1SVladimir Oltean 
4588aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
4598aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
4608aa9ebccSVladimir Oltean 	 */
461f238fef1SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++) {
462f238fef1SVladimir Oltean 		for (j = 0; j < ds->num_ports; j++) {
463f238fef1SVladimir Oltean 			if (dsa_is_unused_port(ds, j))
464f238fef1SVladimir Oltean 				continue;
465f238fef1SVladimir Oltean 
466542043e9SVladimir Oltean 			l2fwd[ds->num_ports + i].vlan_pmap[j] = i;
467f238fef1SVladimir Oltean 		}
4683e77e59bSVladimir Oltean 
4693e77e59bSVladimir Oltean 		l2fwd[ds->num_ports + i].type_egrpcp2outputq = true;
4703e77e59bSVladimir Oltean 	}
4713e77e59bSVladimir Oltean 
4723e77e59bSVladimir Oltean 	return 0;
4733e77e59bSVladimir Oltean }
4743e77e59bSVladimir Oltean 
4753e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv)
4763e77e59bSVladimir Oltean {
4773e77e59bSVladimir Oltean 	struct sja1110_pcp_remapping_entry *pcp_remap;
4783e77e59bSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
4793e77e59bSVladimir Oltean 	struct sja1105_table *table;
4803e77e59bSVladimir Oltean 	int port, tc;
4813e77e59bSVladimir Oltean 
4823e77e59bSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING];
4833e77e59bSVladimir Oltean 
4843e77e59bSVladimir Oltean 	/* Nothing to do for SJA1105 */
4853e77e59bSVladimir Oltean 	if (!table->ops->max_entry_count)
4863e77e59bSVladimir Oltean 		return 0;
4873e77e59bSVladimir Oltean 
4883e77e59bSVladimir Oltean 	if (table->entry_count) {
4893e77e59bSVladimir Oltean 		kfree(table->entries);
4903e77e59bSVladimir Oltean 		table->entry_count = 0;
4913e77e59bSVladimir Oltean 	}
4923e77e59bSVladimir Oltean 
4933e77e59bSVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4943e77e59bSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4953e77e59bSVladimir Oltean 	if (!table->entries)
4963e77e59bSVladimir Oltean 		return -ENOMEM;
4973e77e59bSVladimir Oltean 
4983e77e59bSVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
4993e77e59bSVladimir Oltean 
5003e77e59bSVladimir Oltean 	pcp_remap = table->entries;
5013e77e59bSVladimir Oltean 
5023e77e59bSVladimir Oltean 	/* Repeat the configuration done for vlan_pmap */
5033e77e59bSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
5043e77e59bSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
5053e77e59bSVladimir Oltean 			continue;
5063e77e59bSVladimir Oltean 
5073e77e59bSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
5083e77e59bSVladimir Oltean 			pcp_remap[port].egrpcp[tc] = tc;
509f238fef1SVladimir Oltean 	}
5108aa9ebccSVladimir Oltean 
5118aa9ebccSVladimir Oltean 	return 0;
5128aa9ebccSVladimir Oltean }
5138aa9ebccSVladimir Oltean 
5148aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
5158aa9ebccSVladimir Oltean {
5161bf658eeSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2fwd_params;
5178aa9ebccSVladimir Oltean 	struct sja1105_table *table;
5188aa9ebccSVladimir Oltean 
5198aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
5208aa9ebccSVladimir Oltean 
5218aa9ebccSVladimir Oltean 	if (table->entry_count) {
5228aa9ebccSVladimir Oltean 		kfree(table->entries);
5238aa9ebccSVladimir Oltean 		table->entry_count = 0;
5248aa9ebccSVladimir Oltean 	}
5258aa9ebccSVladimir Oltean 
526fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
5278aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5288aa9ebccSVladimir Oltean 	if (!table->entries)
5298aa9ebccSVladimir Oltean 		return -ENOMEM;
5308aa9ebccSVladimir Oltean 
531fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
5328aa9ebccSVladimir Oltean 
5338aa9ebccSVladimir Oltean 	/* This table only has a single entry */
5341bf658eeSVladimir Oltean 	l2fwd_params = table->entries;
5351bf658eeSVladimir Oltean 
5361bf658eeSVladimir Oltean 	/* Disallow dynamic reconfiguration of vlan_pmap */
5371bf658eeSVladimir Oltean 	l2fwd_params->max_dynp = 0;
5381bf658eeSVladimir Oltean 	/* Use a single memory partition for all ingress queues */
5391bf658eeSVladimir Oltean 	l2fwd_params->part_spc[0] = priv->info->max_frame_mem;
5408aa9ebccSVladimir Oltean 
5418aa9ebccSVladimir Oltean 	return 0;
5428aa9ebccSVladimir Oltean }
5438aa9ebccSVladimir Oltean 
544aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
545aaa270c6SVladimir Oltean {
546aaa270c6SVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
547aaa270c6SVladimir Oltean 	struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
5481bf658eeSVladimir Oltean 	int max_mem = priv->info->max_frame_mem;
549aaa270c6SVladimir Oltean 	struct sja1105_table *table;
550aaa270c6SVladimir Oltean 
551aaa270c6SVladimir Oltean 	/* VLAN retagging is implemented using a loopback port that consumes
552aaa270c6SVladimir Oltean 	 * frame buffers. That leaves less for us.
553aaa270c6SVladimir Oltean 	 */
554aaa270c6SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_BEST_EFFORT)
5551bf658eeSVladimir Oltean 		max_mem -= SJA1105_FRAME_MEMORY_RETAGGING_OVERHEAD;
556aaa270c6SVladimir Oltean 
557aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
558aaa270c6SVladimir Oltean 	l2_fwd_params = table->entries;
559aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] = max_mem;
560aaa270c6SVladimir Oltean 
561aaa270c6SVladimir Oltean 	/* If we have any critical-traffic virtual links, we need to reserve
562aaa270c6SVladimir Oltean 	 * some frame buffer memory for them. At the moment, hardcode the value
563aaa270c6SVladimir Oltean 	 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
564aaa270c6SVladimir Oltean 	 * remaining for best-effort traffic. TODO: figure out a more flexible
565aaa270c6SVladimir Oltean 	 * way to perform the frame buffer partitioning.
566aaa270c6SVladimir Oltean 	 */
567aaa270c6SVladimir Oltean 	if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
568aaa270c6SVladimir Oltean 		return;
569aaa270c6SVladimir Oltean 
570aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
571aaa270c6SVladimir Oltean 	vl_fwd_params = table->entries;
572aaa270c6SVladimir Oltean 
573aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
574aaa270c6SVladimir Oltean 	vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
575aaa270c6SVladimir Oltean }
576aaa270c6SVladimir Oltean 
577ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values:
578ceec8bc0SVladimir Oltean  *
579ceec8bc0SVladimir Oltean  *      | 100 Mbps ports |  1Gbps ports  | 2.5Gbps ports | Disabled ports
580ceec8bc0SVladimir Oltean  * -----+----------------+---------------+---------------+---------------
581ceec8bc0SVladimir Oltean  *   0  |   0, [5:10]    |     [1:2]     |     [3:4]     |     retag
582ceec8bc0SVladimir Oltean  *   1  |0, [5:10], retag|     [1:2]     |     [3:4]     |       -
583ceec8bc0SVladimir Oltean  *   2  |   0, [5:10]    |  [1:3], retag |       4       |       -
584ceec8bc0SVladimir Oltean  *   3  |   0, [5:10]    |[1:2], 4, retag|       3       |       -
585ceec8bc0SVladimir Oltean  *   4  |  0, 2, [5:10]  |    1, retag   |     [3:4]     |       -
586ceec8bc0SVladimir Oltean  *   5  |  0, 1, [5:10]  |    2, retag   |     [3:4]     |       -
587ceec8bc0SVladimir Oltean  *  14  |   0, [5:10]    | [1:4], retag  |       -       |       -
588ceec8bc0SVladimir Oltean  *  15  |     [5:10]     | [0:4], retag  |       -       |       -
589ceec8bc0SVladimir Oltean  */
590ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv)
591ceec8bc0SVladimir Oltean {
592ceec8bc0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
593ceec8bc0SVladimir Oltean 	struct sja1105_table *table;
594ceec8bc0SVladimir Oltean 	bool port_1_is_base_tx;
595ceec8bc0SVladimir Oltean 	bool port_3_is_2500;
596ceec8bc0SVladimir Oltean 	bool port_4_is_2500;
597ceec8bc0SVladimir Oltean 	u64 tdmaconfigidx;
598ceec8bc0SVladimir Oltean 
599ceec8bc0SVladimir Oltean 	if (priv->info->device_id != SJA1110_DEVICE_ID)
600ceec8bc0SVladimir Oltean 		return;
601ceec8bc0SVladimir Oltean 
602ceec8bc0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
603ceec8bc0SVladimir Oltean 	general_params = table->entries;
604ceec8bc0SVladimir Oltean 
605ceec8bc0SVladimir Oltean 	/* All the settings below are "as opposed to SGMII", which is the
606ceec8bc0SVladimir Oltean 	 * other pinmuxing option.
607ceec8bc0SVladimir Oltean 	 */
608ceec8bc0SVladimir Oltean 	port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL;
609ceec8bc0SVladimir Oltean 	port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX;
610ceec8bc0SVladimir Oltean 	port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX;
611ceec8bc0SVladimir Oltean 
612ceec8bc0SVladimir Oltean 	if (port_1_is_base_tx)
613ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
614ceec8bc0SVladimir Oltean 		tdmaconfigidx = 5;
615ceec8bc0SVladimir Oltean 	else if (port_3_is_2500 && port_4_is_2500)
616ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 100 Mbps */
617ceec8bc0SVladimir Oltean 		tdmaconfigidx = 1;
618ceec8bc0SVladimir Oltean 	else if (port_3_is_2500)
619ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
620ceec8bc0SVladimir Oltean 		tdmaconfigidx = 3;
621ceec8bc0SVladimir Oltean 	else if (port_4_is_2500)
622ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
623ceec8bc0SVladimir Oltean 		tdmaconfigidx = 2;
624ceec8bc0SVladimir Oltean 	else
625ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
626ceec8bc0SVladimir Oltean 		tdmaconfigidx = 14;
627ceec8bc0SVladimir Oltean 
628ceec8bc0SVladimir Oltean 	general_params->tdmaconfigidx = tdmaconfigidx;
629ceec8bc0SVladimir Oltean }
630ceec8bc0SVladimir Oltean 
6318aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
6328aa9ebccSVladimir Oltean {
6338aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
634511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
635511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
6368aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
6375f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
6385f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
6395f06c63bSVladimir Oltean 		 */
64008fde09aSVladimir Oltean 		.hostprio = 7,
6418aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
6428aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
64342824463SVladimir Oltean 		.incl_srcpt1 = false,
6448aa9ebccSVladimir Oltean 		.send_meta1  = false,
6458aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
6468aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
64742824463SVladimir Oltean 		.incl_srcpt0 = false,
6488aa9ebccSVladimir Oltean 		.send_meta0  = false,
6498aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
6508aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
6518aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
6528aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
6538aa9ebccSVladimir Oltean 		 */
654df2a81a3SVladimir Oltean 		.host_port = priv->ds->num_ports,
655511e6ca0SVladimir Oltean 		/* Default to an invalid value */
656542043e9SVladimir Oltean 		.mirr_port = priv->ds->num_ports,
6578aa9ebccSVladimir Oltean 		/* No TTEthernet */
658dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
6598aa9ebccSVladimir Oltean 		.vlmarker = 0,
6608aa9ebccSVladimir Oltean 		.vlmask = 0,
6618aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
6628aa9ebccSVladimir Oltean 		.ignore2stf = 0,
6636666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
6646666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
6656666cebcSVladimir Oltean 		 */
6666666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
6676666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
66829305260SVladimir Oltean 		/* Enable the TTEthernet engine on SJA1110 */
66929305260SVladimir Oltean 		.tte_en = true,
6704913b8ebSVladimir Oltean 		/* Set up the EtherType for control packets on SJA1110 */
6714913b8ebSVladimir Oltean 		.header_type = ETH_P_SJA1110,
6728aa9ebccSVladimir Oltean 	};
6736c0de59bSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
674df2a81a3SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
6758aa9ebccSVladimir Oltean 	struct sja1105_table *table;
676df2a81a3SVladimir Oltean 	int port;
677df2a81a3SVladimir Oltean 
678df2a81a3SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
679df2a81a3SVladimir Oltean 		if (dsa_is_cpu_port(ds, port)) {
680df2a81a3SVladimir Oltean 			default_general_params.host_port = port;
681df2a81a3SVladimir Oltean 			break;
682df2a81a3SVladimir Oltean 		}
683df2a81a3SVladimir Oltean 	}
6848aa9ebccSVladimir Oltean 
6858aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
6868aa9ebccSVladimir Oltean 
6878aa9ebccSVladimir Oltean 	if (table->entry_count) {
6888aa9ebccSVladimir Oltean 		kfree(table->entries);
6898aa9ebccSVladimir Oltean 		table->entry_count = 0;
6908aa9ebccSVladimir Oltean 	}
6918aa9ebccSVladimir Oltean 
692fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
6938aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
6948aa9ebccSVladimir Oltean 	if (!table->entries)
6958aa9ebccSVladimir Oltean 		return -ENOMEM;
6968aa9ebccSVladimir Oltean 
697fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
6988aa9ebccSVladimir Oltean 
6996c0de59bSVladimir Oltean 	general_params = table->entries;
7006c0de59bSVladimir Oltean 
7018aa9ebccSVladimir Oltean 	/* This table only has a single entry */
7026c0de59bSVladimir Oltean 	general_params[0] = default_general_params;
7038aa9ebccSVladimir Oltean 
704ceec8bc0SVladimir Oltean 	sja1110_select_tdmaconfigidx(priv);
705ceec8bc0SVladimir Oltean 
7066c0de59bSVladimir Oltean 	/* Link-local traffic received on casc_port will be forwarded
7076c0de59bSVladimir Oltean 	 * to host_port without embedding the source port and device ID
7086c0de59bSVladimir Oltean 	 * info in the destination MAC address, and no RX timestamps will be
7096c0de59bSVladimir Oltean 	 * taken either (presumably because it is a cascaded port and a
7106c0de59bSVladimir Oltean 	 * downstream SJA switch already did that).
7116c0de59bSVladimir Oltean 	 * To disable the feature, we need to do different things depending on
7126c0de59bSVladimir Oltean 	 * switch generation. On SJA1105 we need to set an invalid port, while
7136c0de59bSVladimir Oltean 	 * on SJA1110 which support multiple cascaded ports, this field is a
7146c0de59bSVladimir Oltean 	 * bitmask so it must be left zero.
7156c0de59bSVladimir Oltean 	 */
7166c0de59bSVladimir Oltean 	if (!priv->info->multiple_cascade_ports)
7176c0de59bSVladimir Oltean 		general_params->casc_port = ds->num_ports;
7186c0de59bSVladimir Oltean 
7198aa9ebccSVladimir Oltean 	return 0;
7208aa9ebccSVladimir Oltean }
7218aa9ebccSVladimir Oltean 
72279d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
72379d5511cSVladimir Oltean {
72479d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
72579d5511cSVladimir Oltean 	struct sja1105_table *table;
72679d5511cSVladimir Oltean 
72779d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
72879d5511cSVladimir Oltean 
72979d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
73079d5511cSVladimir Oltean 	if (table->entry_count) {
73179d5511cSVladimir Oltean 		kfree(table->entries);
73279d5511cSVladimir Oltean 		table->entry_count = 0;
73379d5511cSVladimir Oltean 	}
73479d5511cSVladimir Oltean 
735fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
73679d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
73779d5511cSVladimir Oltean 	if (!table->entries)
73879d5511cSVladimir Oltean 		return -ENOMEM;
73979d5511cSVladimir Oltean 
740fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
74179d5511cSVladimir Oltean 
74279d5511cSVladimir Oltean 	avb = table->entries;
74379d5511cSVladimir Oltean 
74479d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
74579d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
74679d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
747747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
748747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
749747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
750747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
751747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
752747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
753747e5eb3SVladimir Oltean 	 */
754747e5eb3SVladimir Oltean 	avb->cas_master = false;
75579d5511cSVladimir Oltean 
75679d5511cSVladimir Oltean 	return 0;
75779d5511cSVladimir Oltean }
75879d5511cSVladimir Oltean 
759a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
760a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
761a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
762a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
763a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
764a7cc081cSVladimir Oltean  * will be used for this frame.
765a7cc081cSVladimir Oltean  *
766a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
767a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
768a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
769a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
770a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
771a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
772a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
773a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
774a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
775a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
776a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
777a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
778a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
779a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
780a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
781a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
782a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
783a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
784a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
785a7cc081cSVladimir Oltean  * +------------+--------+
786a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
787a7cc081cSVladimir Oltean  * +------------+--------+
788a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
789a7cc081cSVladimir Oltean  * +------------+--------+
790a7cc081cSVladimir Oltean  *    ...                                  ...
791a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
792a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
793a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
794a7cc081cSVladimir Oltean  *
795a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
796a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
797a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
798a7cc081cSVladimir Oltean  * lookup) equal.
799a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
800a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
801a7cc081cSVladimir Oltean  */
8028aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
8038aa9ebccSVladimir Oltean 
8048aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
8058aa9ebccSVladimir Oltean {
8068aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
807542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
8088aa9ebccSVladimir Oltean 	struct sja1105_table *table;
809a7cc081cSVladimir Oltean 	int port, tc;
8108aa9ebccSVladimir Oltean 
8118aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
8128aa9ebccSVladimir Oltean 
8138aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
8148aa9ebccSVladimir Oltean 	if (table->entry_count) {
8158aa9ebccSVladimir Oltean 		kfree(table->entries);
8168aa9ebccSVladimir Oltean 		table->entry_count = 0;
8178aa9ebccSVladimir Oltean 	}
8188aa9ebccSVladimir Oltean 
819fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
8208aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
8218aa9ebccSVladimir Oltean 	if (!table->entries)
8228aa9ebccSVladimir Oltean 		return -ENOMEM;
8238aa9ebccSVladimir Oltean 
824fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
8258aa9ebccSVladimir Oltean 
8268aa9ebccSVladimir Oltean 	policing = table->entries;
8278aa9ebccSVladimir Oltean 
828a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
829542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
83038fbe91fSVladimir Oltean 		int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port;
831542043e9SVladimir Oltean 		int bcast = (ds->num_ports * SJA1105_NUM_TC) + port;
832a7cc081cSVladimir Oltean 
833a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
834a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
835a7cc081cSVladimir Oltean 
836a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
83738fbe91fSVladimir Oltean 		/* Only SJA1110 has multicast policers */
83838fbe91fSVladimir Oltean 		if (mcast <= table->ops->max_entry_count)
83938fbe91fSVladimir Oltean 			policing[mcast].sharindx = port;
840a7cc081cSVladimir Oltean 	}
841a7cc081cSVladimir Oltean 
842a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
843542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
844c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
845c279c726SVladimir Oltean 
846a7cc081cSVladimir Oltean 		if (dsa_is_cpu_port(priv->ds, port))
847c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
8488aa9ebccSVladimir Oltean 
849a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
850a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
851a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
852a7cc081cSVladimir Oltean 		policing[port].partition = 0;
8538aa9ebccSVladimir Oltean 	}
854a7cc081cSVladimir Oltean 
8558aa9ebccSVladimir Oltean 	return 0;
8568aa9ebccSVladimir Oltean }
8578aa9ebccSVladimir Oltean 
8585d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv)
8598aa9ebccSVladimir Oltean {
8608aa9ebccSVladimir Oltean 	int rc;
8618aa9ebccSVladimir Oltean 
8628aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
8638aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
8648aa9ebccSVladimir Oltean 					priv->info->static_ops,
8658aa9ebccSVladimir Oltean 					priv->info->device_id);
8668aa9ebccSVladimir Oltean 	if (rc)
8678aa9ebccSVladimir Oltean 		return rc;
8688aa9ebccSVladimir Oltean 
8698aa9ebccSVladimir Oltean 	/* Build static configuration */
8708aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
8718aa9ebccSVladimir Oltean 	if (rc < 0)
8728aa9ebccSVladimir Oltean 		return rc;
8735d645df9SVladimir Oltean 	rc = sja1105_init_mii_settings(priv);
8748aa9ebccSVladimir Oltean 	if (rc < 0)
8758aa9ebccSVladimir Oltean 		return rc;
8768aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
8778aa9ebccSVladimir Oltean 	if (rc < 0)
8788aa9ebccSVladimir Oltean 		return rc;
8798aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
8808aa9ebccSVladimir Oltean 	if (rc < 0)
8818aa9ebccSVladimir Oltean 		return rc;
8828aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
8838aa9ebccSVladimir Oltean 	if (rc < 0)
8848aa9ebccSVladimir Oltean 		return rc;
8858aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
8868aa9ebccSVladimir Oltean 	if (rc < 0)
8878aa9ebccSVladimir Oltean 		return rc;
8888aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
8898aa9ebccSVladimir Oltean 	if (rc < 0)
8908aa9ebccSVladimir Oltean 		return rc;
8918aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
8928aa9ebccSVladimir Oltean 	if (rc < 0)
8938aa9ebccSVladimir Oltean 		return rc;
8948aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
8958aa9ebccSVladimir Oltean 	if (rc < 0)
8968aa9ebccSVladimir Oltean 		return rc;
89779d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
89879d5511cSVladimir Oltean 	if (rc < 0)
89979d5511cSVladimir Oltean 		return rc;
9003e77e59bSVladimir Oltean 	rc = sja1110_init_pcp_remapping(priv);
9013e77e59bSVladimir Oltean 	if (rc < 0)
9023e77e59bSVladimir Oltean 		return rc;
9038aa9ebccSVladimir Oltean 
9048aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
9058aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
9068aa9ebccSVladimir Oltean }
9078aa9ebccSVladimir Oltean 
90829afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv)
909f5b8631cSVladimir Oltean {
910542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
91129afb83aSVladimir Oltean 	int port;
912f5b8631cSVladimir Oltean 
91329afb83aSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
91429afb83aSVladimir Oltean 		if (!priv->fixed_link[port])
915f5b8631cSVladimir Oltean 			continue;
916f5b8631cSVladimir Oltean 
91729afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID ||
91829afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
91929afb83aSVladimir Oltean 			priv->rgmii_rx_delay[port] = true;
920f5b8631cSVladimir Oltean 
92129afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID ||
92229afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
92329afb83aSVladimir Oltean 			priv->rgmii_tx_delay[port] = true;
924f5b8631cSVladimir Oltean 
92529afb83aSVladimir Oltean 		if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) &&
926f5b8631cSVladimir Oltean 		    !priv->info->setup_rgmii_delay)
927f5b8631cSVladimir Oltean 			return -EINVAL;
928f5b8631cSVladimir Oltean 	}
929f5b8631cSVladimir Oltean 	return 0;
930f5b8631cSVladimir Oltean }
931f5b8631cSVladimir Oltean 
9328aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
9338aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
9348aa9ebccSVladimir Oltean {
9358aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
9368aa9ebccSVladimir Oltean 	struct device_node *child;
9378aa9ebccSVladimir Oltean 
93827afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
9398aa9ebccSVladimir Oltean 		struct device_node *phy_node;
9400c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
9418aa9ebccSVladimir Oltean 		u32 index;
9420c65b2b9SAndrew Lunn 		int err;
9438aa9ebccSVladimir Oltean 
9448aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
9458aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
9468aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
9478aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
9487ba771e3SNishka Dasgupta 			of_node_put(child);
9498aa9ebccSVladimir Oltean 			return -ENODEV;
9508aa9ebccSVladimir Oltean 		}
9518aa9ebccSVladimir Oltean 
9528aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
9530c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
9540c65b2b9SAndrew Lunn 		if (err) {
9558aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
9568aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
9578aa9ebccSVladimir Oltean 				index);
9587ba771e3SNishka Dasgupta 			of_node_put(child);
9598aa9ebccSVladimir Oltean 			return -ENODEV;
9608aa9ebccSVladimir Oltean 		}
9618aa9ebccSVladimir Oltean 
9628aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
9638aa9ebccSVladimir Oltean 		if (!phy_node) {
9648aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
9658aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
9668aa9ebccSVladimir Oltean 					"properties missing!\n");
9677ba771e3SNishka Dasgupta 				of_node_put(child);
9688aa9ebccSVladimir Oltean 				return -ENODEV;
9698aa9ebccSVladimir Oltean 			}
9708aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
9718aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
9728aa9ebccSVladimir Oltean 			 */
97329afb83aSVladimir Oltean 			priv->fixed_link[index] = true;
9748aa9ebccSVladimir Oltean 		} else {
9758aa9ebccSVladimir Oltean 			of_node_put(phy_node);
9768aa9ebccSVladimir Oltean 		}
9778aa9ebccSVladimir Oltean 
978bf4edf4aSVladimir Oltean 		priv->phy_mode[index] = phy_mode;
9798aa9ebccSVladimir Oltean 	}
9808aa9ebccSVladimir Oltean 
9818aa9ebccSVladimir Oltean 	return 0;
9828aa9ebccSVladimir Oltean }
9838aa9ebccSVladimir Oltean 
9845d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv)
9858aa9ebccSVladimir Oltean {
9868aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
9878aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
9888aa9ebccSVladimir Oltean 	struct device_node *ports_node;
9898aa9ebccSVladimir Oltean 	int rc;
9908aa9ebccSVladimir Oltean 
9918aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
99215074a36SVladimir Oltean 	if (!ports_node)
99315074a36SVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
9948aa9ebccSVladimir Oltean 	if (!ports_node) {
9958aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
9968aa9ebccSVladimir Oltean 		return -ENODEV;
9978aa9ebccSVladimir Oltean 	}
9988aa9ebccSVladimir Oltean 
9995d645df9SVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports_node);
10008aa9ebccSVladimir Oltean 	of_node_put(ports_node);
10018aa9ebccSVladimir Oltean 
10028aa9ebccSVladimir Oltean 	return rc;
10038aa9ebccSVladimir Oltean }
10048aa9ebccSVladimir Oltean 
1005c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
100641fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv,
100741fed17fSVladimir Oltean 					 u64 speed)
100841fed17fSVladimir Oltean {
100941fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS])
101041fed17fSVladimir Oltean 		return SPEED_10;
101141fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS])
101241fed17fSVladimir Oltean 		return SPEED_100;
101341fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS])
101441fed17fSVladimir Oltean 		return SPEED_1000;
101541fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS])
101641fed17fSVladimir Oltean 		return SPEED_2500;
101741fed17fSVladimir Oltean 	return SPEED_UNKNOWN;
101841fed17fSVladimir Oltean }
10198aa9ebccSVladimir Oltean 
10208400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
10218aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
10228400cff6SVladimir Oltean 				      int speed_mbps)
10238aa9ebccSVladimir Oltean {
10248aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
10258aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
102641fed17fSVladimir Oltean 	u64 speed;
10278aa9ebccSVladimir Oltean 	int rc;
10288aa9ebccSVladimir Oltean 
10298400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
10308400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
10318400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
10328400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
10338400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
10348400cff6SVladimir Oltean 	 */
10358aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
10368aa9ebccSVladimir Oltean 
1037f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
1038c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
1039a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
1040a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
1041a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
1042a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
1043a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
1044a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
1045a979a0abSVladimir Oltean 		 */
104641fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
1047f4cfcfbdSVladimir Oltean 		break;
1048c44d0535SVladimir Oltean 	case SPEED_10:
104941fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_10MBPS];
1050f4cfcfbdSVladimir Oltean 		break;
1051c44d0535SVladimir Oltean 	case SPEED_100:
105241fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_100MBPS];
1053f4cfcfbdSVladimir Oltean 		break;
1054c44d0535SVladimir Oltean 	case SPEED_1000:
105541fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
1056f4cfcfbdSVladimir Oltean 		break;
1057f4cfcfbdSVladimir Oltean 	default:
10588aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
10598aa9ebccSVladimir Oltean 		return -EINVAL;
10608aa9ebccSVladimir Oltean 	}
10618aa9ebccSVladimir Oltean 
10628400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
10638400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
10648400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
10658400cff6SVladimir Oltean 	 * we want auto during upload phase).
1066ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
1067ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
10688aa9ebccSVladimir Oltean 	 */
106991a05078SVladimir Oltean 	if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII)
107041fed17fSVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
1071ffe10e67SVladimir Oltean 	else
10728aa9ebccSVladimir Oltean 		mac[port].speed = speed;
10738aa9ebccSVladimir Oltean 
10748aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
10758400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
10768400cff6SVladimir Oltean 					  &mac[port], true);
10778aa9ebccSVladimir Oltean 	if (rc < 0) {
10788aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
10798aa9ebccSVladimir Oltean 		return rc;
10808aa9ebccSVladimir Oltean 	}
10818aa9ebccSVladimir Oltean 
10828aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
10838aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
10848aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
10858aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
10868aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
10878aa9ebccSVladimir Oltean 	 */
108891a05078SVladimir Oltean 	if (!phy_interface_mode_is_rgmii(priv->phy_mode[port]))
10898aa9ebccSVladimir Oltean 		return 0;
10908aa9ebccSVladimir Oltean 
10918aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
10928aa9ebccSVladimir Oltean }
10938aa9ebccSVladimir Oltean 
109439710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
109539710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
109639710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
109739710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
109839710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
109939710229SVladimir Oltean  * now.
110039710229SVladimir Oltean  */
110139710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
110239710229SVladimir Oltean 				      phy_interface_t interface)
110339710229SVladimir Oltean {
1104bf4edf4aSVladimir Oltean 	return priv->phy_mode[port] != interface;
110539710229SVladimir Oltean }
110639710229SVladimir Oltean 
1107af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
1108ffe10e67SVladimir Oltean 			       unsigned int mode,
1109af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
11108aa9ebccSVladimir Oltean {
1111*3ad1d171SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
11128aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1113*3ad1d171SVladimir Oltean 	struct dw_xpcs *xpcs;
11148aa9ebccSVladimir Oltean 
1115ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1116ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
1117ec8582d1SVladimir Oltean 			phy_modes(state->interface));
111839710229SVladimir Oltean 		return;
1119ec8582d1SVladimir Oltean 	}
112039710229SVladimir Oltean 
1121*3ad1d171SVladimir Oltean 	xpcs = priv->xpcs[port];
1122ffe10e67SVladimir Oltean 
1123*3ad1d171SVladimir Oltean 	if (xpcs)
1124*3ad1d171SVladimir Oltean 		phylink_set_pcs(dp->pl, &xpcs->pcs);
11258400cff6SVladimir Oltean }
11268400cff6SVladimir Oltean 
11278400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
11288400cff6SVladimir Oltean 				  unsigned int mode,
11298400cff6SVladimir Oltean 				  phy_interface_t interface)
11308400cff6SVladimir Oltean {
11318400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
11328400cff6SVladimir Oltean }
11338400cff6SVladimir Oltean 
11348400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
11358400cff6SVladimir Oltean 				unsigned int mode,
11368400cff6SVladimir Oltean 				phy_interface_t interface,
11375b502a7bSRussell King 				struct phy_device *phydev,
11385b502a7bSRussell King 				int speed, int duplex,
11395b502a7bSRussell King 				bool tx_pause, bool rx_pause)
11408400cff6SVladimir Oltean {
1141ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1142ec8582d1SVladimir Oltean 
1143ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1144ec8582d1SVladimir Oltean 
1145ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
11468aa9ebccSVladimir Oltean }
11478aa9ebccSVladimir Oltean 
1148ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1149ad9f299aSVladimir Oltean 				     unsigned long *supported,
1150ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
1151ad9f299aSVladimir Oltean {
1152ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
1153ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
1154ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
1155ad9f299aSVladimir Oltean 	 */
1156ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1157ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1158ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1159ad9f299aSVladimir Oltean 
1160ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1161ad9f299aSVladimir Oltean 
116239710229SVladimir Oltean 	/* include/linux/phylink.h says:
116339710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
116439710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
116539710229SVladimir Oltean 	 */
116639710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
116739710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
116839710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
116939710229SVladimir Oltean 		return;
117039710229SVladimir Oltean 	}
117139710229SVladimir Oltean 
1172ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1173ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1174ad9f299aSVladimir Oltean 	 */
1175ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
1176ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
1177ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
1178ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
1179ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
1180ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1181ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1182ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
1183ad9f299aSVladimir Oltean 
1184ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1185ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1186ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1187ad9f299aSVladimir Oltean }
1188ad9f299aSVladimir Oltean 
118960f6053fSVladimir Oltean static int
119060f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
119160f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
119260f6053fSVladimir Oltean {
119360f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
119460f6053fSVladimir Oltean 	struct sja1105_table *table;
119560f6053fSVladimir Oltean 	int i;
119660f6053fSVladimir Oltean 
119760f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
119860f6053fSVladimir Oltean 	l2_lookup = table->entries;
119960f6053fSVladimir Oltean 
120060f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
120160f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
120260f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
120360f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
120460f6053fSVladimir Oltean 			return i;
120560f6053fSVladimir Oltean 
120660f6053fSVladimir Oltean 	return -1;
120760f6053fSVladimir Oltean }
120860f6053fSVladimir Oltean 
120960f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
121060f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
121160f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
121260f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
121360f6053fSVladimir Oltean  */
121460f6053fSVladimir Oltean static int
121560f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
121660f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
121760f6053fSVladimir Oltean 			  bool keep)
121860f6053fSVladimir Oltean {
121960f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
122060f6053fSVladimir Oltean 	struct sja1105_table *table;
122160f6053fSVladimir Oltean 	int rc, match;
122260f6053fSVladimir Oltean 
122360f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
122460f6053fSVladimir Oltean 
122560f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
122660f6053fSVladimir Oltean 	if (match < 0) {
122760f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
122860f6053fSVladimir Oltean 		if (!keep)
122960f6053fSVladimir Oltean 			return 0;
123060f6053fSVladimir Oltean 
123160f6053fSVladimir Oltean 		/* No match => new entry */
123260f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
123360f6053fSVladimir Oltean 		if (rc)
123460f6053fSVladimir Oltean 			return rc;
123560f6053fSVladimir Oltean 
123660f6053fSVladimir Oltean 		match = table->entry_count - 1;
123760f6053fSVladimir Oltean 	}
123860f6053fSVladimir Oltean 
123960f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
124060f6053fSVladimir Oltean 	l2_lookup = table->entries;
124160f6053fSVladimir Oltean 
124260f6053fSVladimir Oltean 	/* We have a match.
124360f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
124460f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
124560f6053fSVladimir Oltean 	 * which we update it).
124660f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
124760f6053fSVladimir Oltean 	 */
124860f6053fSVladimir Oltean 	if (keep) {
124960f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
125060f6053fSVladimir Oltean 		return 0;
125160f6053fSVladimir Oltean 	}
125260f6053fSVladimir Oltean 
125360f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
125460f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
125560f6053fSVladimir Oltean 	 */
125660f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
125760f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
125860f6053fSVladimir Oltean }
125960f6053fSVladimir Oltean 
1260291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1261291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1262291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1263291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1264291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1265291d1e72SVladimir Oltean  */
126609c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1267291d1e72SVladimir Oltean {
1268291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1269291d1e72SVladimir Oltean }
1270291d1e72SVladimir Oltean 
12719dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1272291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1273291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1274291d1e72SVladimir Oltean 					 int *last_unused)
1275291d1e72SVladimir Oltean {
1276291d1e72SVladimir Oltean 	int way;
1277291d1e72SVladimir Oltean 
1278291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1279291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1280291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1281291d1e72SVladimir Oltean 
1282291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1283291d1e72SVladimir Oltean 		 * into the return value
1284291d1e72SVladimir Oltean 		 */
1285291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1286291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1287291d1e72SVladimir Oltean 			if (last_unused)
1288291d1e72SVladimir Oltean 				*last_unused = way;
1289291d1e72SVladimir Oltean 			continue;
1290291d1e72SVladimir Oltean 		}
1291291d1e72SVladimir Oltean 
1292291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1293291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1294291d1e72SVladimir Oltean 			if (match)
1295291d1e72SVladimir Oltean 				*match = l2_lookup;
1296291d1e72SVladimir Oltean 			return way;
1297291d1e72SVladimir Oltean 		}
1298291d1e72SVladimir Oltean 	}
1299291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1300291d1e72SVladimir Oltean 	return -1;
1301291d1e72SVladimir Oltean }
1302291d1e72SVladimir Oltean 
13039dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1304291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1305291d1e72SVladimir Oltean {
1306291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1307291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1308291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1309291d1e72SVladimir Oltean 	int last_unused = -1;
131060f6053fSVladimir Oltean 	int bin, way, rc;
1311291d1e72SVladimir Oltean 
13129dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1313291d1e72SVladimir Oltean 
13149dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1315291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1316291d1e72SVladimir Oltean 	if (way >= 0) {
1317291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1318291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1319291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1320291d1e72SVladimir Oltean 		 */
1321291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1322291d1e72SVladimir Oltean 			return 0;
1323291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1324291d1e72SVladimir Oltean 	} else {
1325291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1326291d1e72SVladimir Oltean 
1327291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1328291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1329291d1e72SVladimir Oltean 		 */
1330291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1331291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1332291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1333291d1e72SVladimir Oltean 
1334291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1335291d1e72SVladimir Oltean 			way = last_unused;
1336291d1e72SVladimir Oltean 		} else {
1337291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1338291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1339291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1340291d1e72SVladimir Oltean 			 * distribution function:
1341291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1342291d1e72SVladimir Oltean 			 */
1343291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1344291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1345291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1346291d1e72SVladimir Oltean 				 bin, addr, way);
1347291d1e72SVladimir Oltean 			/* Evict entry */
1348291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1349291d1e72SVladimir Oltean 						     index, NULL, false);
1350291d1e72SVladimir Oltean 		}
1351291d1e72SVladimir Oltean 	}
1352291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1353291d1e72SVladimir Oltean 
135460f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1355291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1356291d1e72SVladimir Oltean 					  true);
135760f6053fSVladimir Oltean 	if (rc < 0)
135860f6053fSVladimir Oltean 		return rc;
135960f6053fSVladimir Oltean 
136060f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1361291d1e72SVladimir Oltean }
1362291d1e72SVladimir Oltean 
13639dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1364291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1365291d1e72SVladimir Oltean {
1366291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1367291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
136860f6053fSVladimir Oltean 	int index, bin, way, rc;
1369291d1e72SVladimir Oltean 	bool keep;
1370291d1e72SVladimir Oltean 
13719dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
13729dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1373291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1374291d1e72SVladimir Oltean 	if (way < 0)
1375291d1e72SVladimir Oltean 		return 0;
1376291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1377291d1e72SVladimir Oltean 
1378291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1379291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1380291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1381291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1382291d1e72SVladimir Oltean 	 */
1383291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
13847752e937SVladimir Oltean 
1385291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1386291d1e72SVladimir Oltean 		keep = true;
1387291d1e72SVladimir Oltean 	else
1388291d1e72SVladimir Oltean 		keep = false;
1389291d1e72SVladimir Oltean 
139060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1391291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
139260f6053fSVladimir Oltean 	if (rc < 0)
139360f6053fSVladimir Oltean 		return rc;
139460f6053fSVladimir Oltean 
139560f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1396291d1e72SVladimir Oltean }
1397291d1e72SVladimir Oltean 
13989dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
13999dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
14009dfa6911SVladimir Oltean {
14011da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
14021da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14031da73821SVladimir Oltean 	int rc, i;
14041da73821SVladimir Oltean 
14051da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
14061da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
14071da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
14081da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
14091da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
14107f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
14111da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
14121da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
14136d7c7d94SVladimir Oltean 	} else {
14146d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
14156d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
14166d7c7d94SVladimir Oltean 	}
14171da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
14181da73821SVladimir Oltean 
14191da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
14201da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
14211da73821SVladimir Oltean 	if (rc == 0) {
14221da73821SVladimir Oltean 		/* Found and this port is already in the entry's
14231da73821SVladimir Oltean 		 * port mask => job done
14241da73821SVladimir Oltean 		 */
14251da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
14261da73821SVladimir Oltean 			return 0;
14271da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
14281da73821SVladimir Oltean 		 * found something.
14291da73821SVladimir Oltean 		 */
14301da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
14311da73821SVladimir Oltean 		goto skip_finding_an_index;
14321da73821SVladimir Oltean 	}
14331da73821SVladimir Oltean 
14341da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
14351da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
14361da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
14371da73821SVladimir Oltean 	 */
14381da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
14391da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
14401da73821SVladimir Oltean 						 i, NULL);
14411da73821SVladimir Oltean 		if (rc < 0)
14421da73821SVladimir Oltean 			break;
14431da73821SVladimir Oltean 	}
14441da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
14451da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
14461da73821SVladimir Oltean 		return -EINVAL;
14471da73821SVladimir Oltean 	}
144817ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
14491da73821SVladimir Oltean 	l2_lookup.index = i;
14501da73821SVladimir Oltean 
14511da73821SVladimir Oltean skip_finding_an_index:
145260f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
14531da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
14541da73821SVladimir Oltean 					  true);
145560f6053fSVladimir Oltean 	if (rc < 0)
145660f6053fSVladimir Oltean 		return rc;
145760f6053fSVladimir Oltean 
145860f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
14599dfa6911SVladimir Oltean }
14609dfa6911SVladimir Oltean 
14619dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
14629dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
14639dfa6911SVladimir Oltean {
14641da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
14651da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14661da73821SVladimir Oltean 	bool keep;
14671da73821SVladimir Oltean 	int rc;
14681da73821SVladimir Oltean 
14691da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
14701da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
14711da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
14721da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
14737f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
14741da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
14751da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
14766d7c7d94SVladimir Oltean 	} else {
14776d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
14786d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
14796d7c7d94SVladimir Oltean 	}
14801da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
14811da73821SVladimir Oltean 
14821da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
14831da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
14841da73821SVladimir Oltean 	if (rc < 0)
14851da73821SVladimir Oltean 		return 0;
14861da73821SVladimir Oltean 
14871da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
14881da73821SVladimir Oltean 
14891da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
14901da73821SVladimir Oltean 	 * or if we remove it completely.
14911da73821SVladimir Oltean 	 */
14921da73821SVladimir Oltean 	if (l2_lookup.destports)
14931da73821SVladimir Oltean 		keep = true;
14941da73821SVladimir Oltean 	else
14951da73821SVladimir Oltean 		keep = false;
14961da73821SVladimir Oltean 
149760f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
14981da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
149960f6053fSVladimir Oltean 	if (rc < 0)
150060f6053fSVladimir Oltean 		return rc;
150160f6053fSVladimir Oltean 
150260f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
15039dfa6911SVladimir Oltean }
15049dfa6911SVladimir Oltean 
15059dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
15069dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
15079dfa6911SVladimir Oltean {
15089dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1509b3ee526aSVladimir Oltean 
15106d7c7d94SVladimir Oltean 	/* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
15116d7c7d94SVladimir Oltean 	 * so the switch still does some VLAN processing internally.
15126d7c7d94SVladimir Oltean 	 * But Shared VLAN Learning (SVL) is also active, and it will take
15136d7c7d94SVladimir Oltean 	 * care of autonomous forwarding between the unique pvid's of each
15146d7c7d94SVladimir Oltean 	 * port.  Here we just make sure that users can't add duplicate FDB
15156d7c7d94SVladimir Oltean 	 * entries when in this mode - the actual VID doesn't matter except
15166d7c7d94SVladimir Oltean 	 * for what gets printed in 'bridge fdb show'.  In the case of zero,
15176d7c7d94SVladimir Oltean 	 * no VID gets printed at all.
151893647594SVladimir Oltean 	 */
15197f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
15206d7c7d94SVladimir Oltean 		vid = 0;
152193647594SVladimir Oltean 
15226d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
15239dfa6911SVladimir Oltean }
15249dfa6911SVladimir Oltean 
15259dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
15269dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
15279dfa6911SVladimir Oltean {
15289dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15299dfa6911SVladimir Oltean 
15307f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
15316d7c7d94SVladimir Oltean 		vid = 0;
15326d7c7d94SVladimir Oltean 
1533b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
15349dfa6911SVladimir Oltean }
15359dfa6911SVladimir Oltean 
1536291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1537291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1538291d1e72SVladimir Oltean {
1539291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1540291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1541291d1e72SVladimir Oltean 	int i;
1542291d1e72SVladimir Oltean 
1543291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1544291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1545291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1546291d1e72SVladimir Oltean 		int rc;
1547291d1e72SVladimir Oltean 
1548291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1549291d1e72SVladimir Oltean 						 i, &l2_lookup);
1550291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1551def84604SVladimir Oltean 		if (rc == -ENOENT)
1552291d1e72SVladimir Oltean 			continue;
1553291d1e72SVladimir Oltean 		if (rc) {
1554291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1555291d1e72SVladimir Oltean 			return rc;
1556291d1e72SVladimir Oltean 		}
1557291d1e72SVladimir Oltean 
1558291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1559291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1560291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1561291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1562291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1563291d1e72SVladimir Oltean 		 */
1564291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1565291d1e72SVladimir Oltean 			continue;
15664d942354SVladimir Oltean 
15674d942354SVladimir Oltean 		/* We need to hide the FDB entry for unknown multicast */
15684d942354SVladimir Oltean 		if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST &&
15694d942354SVladimir Oltean 		    l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
15704d942354SVladimir Oltean 			continue;
15714d942354SVladimir Oltean 
1572291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
157393647594SVladimir Oltean 
15746d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
15757f14937fSVladimir Oltean 		if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
15766d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
157717ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1578291d1e72SVladimir Oltean 	}
1579291d1e72SVladimir Oltean 	return 0;
1580291d1e72SVladimir Oltean }
1581291d1e72SVladimir Oltean 
1582a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port,
1583291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1584291d1e72SVladimir Oltean {
1585a52b2da7SVladimir Oltean 	return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1586291d1e72SVladimir Oltean }
1587291d1e72SVladimir Oltean 
1588291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1589291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1590291d1e72SVladimir Oltean {
1591291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1592291d1e72SVladimir Oltean }
1593291d1e72SVladimir Oltean 
15947f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration.
15957f7ccdeaSVladimir Oltean  * Flooding is configured between each {ingress, egress} port pair, and since
15967f7ccdeaSVladimir Oltean  * the bridge's semantics are those of "egress flooding", it means we must
15977f7ccdeaSVladimir Oltean  * enable flooding towards this port from all ingress ports that are in the
15987f7ccdeaSVladimir Oltean  * same forwarding domain.
15997f7ccdeaSVladimir Oltean  */
16007f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv)
16017f7ccdeaSVladimir Oltean {
16027f7ccdeaSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
16037f7ccdeaSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
16047f7ccdeaSVladimir Oltean 	int from, to, rc;
16057f7ccdeaSVladimir Oltean 
16067f7ccdeaSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
16077f7ccdeaSVladimir Oltean 
16087f7ccdeaSVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
16097f7ccdeaSVladimir Oltean 		u64 fl_domain = 0, bc_domain = 0;
16107f7ccdeaSVladimir Oltean 
16117f7ccdeaSVladimir Oltean 		for (to = 0; to < priv->ds->num_ports; to++) {
16127f7ccdeaSVladimir Oltean 			if (!sja1105_can_forward(l2_fwd, from, to))
16137f7ccdeaSVladimir Oltean 				continue;
16147f7ccdeaSVladimir Oltean 
16157f7ccdeaSVladimir Oltean 			if (priv->ucast_egress_floods & BIT(to))
16167f7ccdeaSVladimir Oltean 				fl_domain |= BIT(to);
16177f7ccdeaSVladimir Oltean 			if (priv->bcast_egress_floods & BIT(to))
16187f7ccdeaSVladimir Oltean 				bc_domain |= BIT(to);
16197f7ccdeaSVladimir Oltean 		}
16207f7ccdeaSVladimir Oltean 
16217f7ccdeaSVladimir Oltean 		/* Nothing changed, nothing to do */
16227f7ccdeaSVladimir Oltean 		if (l2_fwd[from].fl_domain == fl_domain &&
16237f7ccdeaSVladimir Oltean 		    l2_fwd[from].bc_domain == bc_domain)
16247f7ccdeaSVladimir Oltean 			continue;
16257f7ccdeaSVladimir Oltean 
16267f7ccdeaSVladimir Oltean 		l2_fwd[from].fl_domain = fl_domain;
16277f7ccdeaSVladimir Oltean 		l2_fwd[from].bc_domain = bc_domain;
16287f7ccdeaSVladimir Oltean 
16297f7ccdeaSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
16307f7ccdeaSVladimir Oltean 						  from, &l2_fwd[from], true);
16317f7ccdeaSVladimir Oltean 		if (rc < 0)
16327f7ccdeaSVladimir Oltean 			return rc;
16337f7ccdeaSVladimir Oltean 	}
16347f7ccdeaSVladimir Oltean 
16357f7ccdeaSVladimir Oltean 	return 0;
16367f7ccdeaSVladimir Oltean }
16377f7ccdeaSVladimir Oltean 
16388aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
16398aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
16408aa9ebccSVladimir Oltean {
16418aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
16428aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16438aa9ebccSVladimir Oltean 	int i, rc;
16448aa9ebccSVladimir Oltean 
16458aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
16468aa9ebccSVladimir Oltean 
1647542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
16488aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
16498aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
16508aa9ebccSVladimir Oltean 		 */
16518aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
16528aa9ebccSVladimir Oltean 			continue;
16538aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
16548aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
16558aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
16568aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
16578aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
16588aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
16598aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
16608aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
16618aa9ebccSVladimir Oltean 		 */
16628aa9ebccSVladimir Oltean 		if (i == port)
16638aa9ebccSVladimir Oltean 			continue;
16648aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
16658aa9ebccSVladimir Oltean 			continue;
16668aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
16678aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
16688aa9ebccSVladimir Oltean 
16698aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
16708aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
16718aa9ebccSVladimir Oltean 		if (rc < 0)
16728aa9ebccSVladimir Oltean 			return rc;
16738aa9ebccSVladimir Oltean 	}
16748aa9ebccSVladimir Oltean 
16757f7ccdeaSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
16768aa9ebccSVladimir Oltean 					  port, &l2_fwd[port], true);
16777f7ccdeaSVladimir Oltean 	if (rc)
16787f7ccdeaSVladimir Oltean 		return rc;
16797f7ccdeaSVladimir Oltean 
16807f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
16818aa9ebccSVladimir Oltean }
16828aa9ebccSVladimir Oltean 
1683640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1684640f763fSVladimir Oltean 					 u8 state)
1685640f763fSVladimir Oltean {
1686640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1687640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1688640f763fSVladimir Oltean 
1689640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1690640f763fSVladimir Oltean 
1691640f763fSVladimir Oltean 	switch (state) {
1692640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1693640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1694640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1695640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1696640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1697640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1698640f763fSVladimir Oltean 		 */
1699640f763fSVladimir Oltean 		mac[port].ingress   = false;
1700640f763fSVladimir Oltean 		mac[port].egress    = false;
1701640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1702640f763fSVladimir Oltean 		break;
1703640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1704640f763fSVladimir Oltean 		mac[port].ingress   = true;
1705640f763fSVladimir Oltean 		mac[port].egress    = false;
1706640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1707640f763fSVladimir Oltean 		break;
1708640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1709640f763fSVladimir Oltean 		mac[port].ingress   = true;
1710640f763fSVladimir Oltean 		mac[port].egress    = false;
17114d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1712640f763fSVladimir Oltean 		break;
1713640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1714640f763fSVladimir Oltean 		mac[port].ingress   = true;
1715640f763fSVladimir Oltean 		mac[port].egress    = true;
17164d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1717640f763fSVladimir Oltean 		break;
1718640f763fSVladimir Oltean 	default:
1719640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1720640f763fSVladimir Oltean 		return;
1721640f763fSVladimir Oltean 	}
1722640f763fSVladimir Oltean 
1723640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1724640f763fSVladimir Oltean 				     &mac[port], true);
1725640f763fSVladimir Oltean }
1726640f763fSVladimir Oltean 
17278aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
17288aa9ebccSVladimir Oltean 			       struct net_device *br)
17298aa9ebccSVladimir Oltean {
17308aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
17318aa9ebccSVladimir Oltean }
17328aa9ebccSVladimir Oltean 
17338aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
17348aa9ebccSVladimir Oltean 				 struct net_device *br)
17358aa9ebccSVladimir Oltean {
17368aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
17378aa9ebccSVladimir Oltean }
17388aa9ebccSVladimir Oltean 
17394d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8)
17404d752508SVladimir Oltean 
17414d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
17424d752508SVladimir Oltean {
17434d752508SVladimir Oltean 	int i;
17444d752508SVladimir Oltean 
17454d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
17464d752508SVladimir Oltean 		if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
17474d752508SVladimir Oltean 			return i;
17484d752508SVladimir Oltean 
17494d752508SVladimir Oltean 	return -1;
17504d752508SVladimir Oltean }
17514d752508SVladimir Oltean 
17524d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
17534d752508SVladimir Oltean 				     int prio)
17544d752508SVladimir Oltean {
17554d752508SVladimir Oltean 	int i;
17564d752508SVladimir Oltean 
17574d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
17584d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
17594d752508SVladimir Oltean 
17604d752508SVladimir Oltean 		if (cbs->port == port && cbs->prio == prio) {
17614d752508SVladimir Oltean 			memset(cbs, 0, sizeof(*cbs));
17624d752508SVladimir Oltean 			return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
17634d752508SVladimir Oltean 							    i, cbs, true);
17644d752508SVladimir Oltean 		}
17654d752508SVladimir Oltean 	}
17664d752508SVladimir Oltean 
17674d752508SVladimir Oltean 	return 0;
17684d752508SVladimir Oltean }
17694d752508SVladimir Oltean 
17704d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
17714d752508SVladimir Oltean 				struct tc_cbs_qopt_offload *offload)
17724d752508SVladimir Oltean {
17734d752508SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
17744d752508SVladimir Oltean 	struct sja1105_cbs_entry *cbs;
17754d752508SVladimir Oltean 	int index;
17764d752508SVladimir Oltean 
17774d752508SVladimir Oltean 	if (!offload->enable)
17784d752508SVladimir Oltean 		return sja1105_delete_cbs_shaper(priv, port, offload->queue);
17794d752508SVladimir Oltean 
17804d752508SVladimir Oltean 	index = sja1105_find_unused_cbs_shaper(priv);
17814d752508SVladimir Oltean 	if (index < 0)
17824d752508SVladimir Oltean 		return -ENOSPC;
17834d752508SVladimir Oltean 
17844d752508SVladimir Oltean 	cbs = &priv->cbs[index];
17854d752508SVladimir Oltean 	cbs->port = port;
17864d752508SVladimir Oltean 	cbs->prio = offload->queue;
17874d752508SVladimir Oltean 	/* locredit and sendslope are negative by definition. In hardware,
17884d752508SVladimir Oltean 	 * positive values must be provided, and the negative sign is implicit.
17894d752508SVladimir Oltean 	 */
17904d752508SVladimir Oltean 	cbs->credit_hi = offload->hicredit;
17914d752508SVladimir Oltean 	cbs->credit_lo = abs(offload->locredit);
17924d752508SVladimir Oltean 	/* User space is in kbits/sec, hardware in bytes/sec */
17934d752508SVladimir Oltean 	cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
17944d752508SVladimir Oltean 	cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
17954d752508SVladimir Oltean 	/* Convert the negative values from 64-bit 2's complement
17964d752508SVladimir Oltean 	 * to 32-bit 2's complement (for the case of 0x80000000 whose
17974d752508SVladimir Oltean 	 * negative is still negative).
17984d752508SVladimir Oltean 	 */
17994d752508SVladimir Oltean 	cbs->credit_lo &= GENMASK_ULL(31, 0);
18004d752508SVladimir Oltean 	cbs->send_slope &= GENMASK_ULL(31, 0);
18014d752508SVladimir Oltean 
18024d752508SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
18034d752508SVladimir Oltean 					    true);
18044d752508SVladimir Oltean }
18054d752508SVladimir Oltean 
18064d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv)
18074d752508SVladimir Oltean {
18084d752508SVladimir Oltean 	int rc = 0, i;
18094d752508SVladimir Oltean 
18104d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
18114d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
18124d752508SVladimir Oltean 
18134d752508SVladimir Oltean 		if (!cbs->idle_slope && !cbs->send_slope)
18144d752508SVladimir Oltean 			continue;
18154d752508SVladimir Oltean 
18164d752508SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
18174d752508SVladimir Oltean 						  true);
18184d752508SVladimir Oltean 		if (rc)
18194d752508SVladimir Oltean 			break;
18204d752508SVladimir Oltean 	}
18214d752508SVladimir Oltean 
18224d752508SVladimir Oltean 	return rc;
18234d752508SVladimir Oltean }
18244d752508SVladimir Oltean 
18252eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
18262eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
18272eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
18282eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
18292eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
1830c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1831dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
18322eea1fa8SVladimir Oltean };
18332eea1fa8SVladimir Oltean 
18346666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
18356666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
18366666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
18376666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
18386666cebcSVladimir Oltean  * such that this operation is relatively seamless.
18396666cebcSVladimir Oltean  */
18402eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
18412eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
18426666cebcSVladimir Oltean {
18436cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
18446cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
184582760d7fSVladimir Oltean 	int speed_mbps[SJA1105_MAX_NUM_PORTS];
184684db00f2SVladimir Oltean 	u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0};
18476666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
18486cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
18496cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
18506cf99c13SVladimir Oltean 	s64 t12, t34;
18516666cebcSVladimir Oltean 	int rc, i;
18526cf99c13SVladimir Oltean 	s64 now;
18536666cebcSVladimir Oltean 
1854af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1855af580ae2SVladimir Oltean 
18566666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
18576666cebcSVladimir Oltean 
18588400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
18598400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
18608400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
18618400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
18626666cebcSVladimir Oltean 	 */
1863542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1864*3ad1d171SVladimir Oltean 		u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1);
1865*3ad1d171SVladimir Oltean 
186641fed17fSVladimir Oltean 		speed_mbps[i] = sja1105_port_speed_to_ethtool(priv,
186741fed17fSVladimir Oltean 							      mac[i].speed);
186841fed17fSVladimir Oltean 		mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
18696666cebcSVladimir Oltean 
1870*3ad1d171SVladimir Oltean 		if (priv->xpcs[i])
1871*3ad1d171SVladimir Oltean 			bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr);
187284db00f2SVladimir Oltean 	}
1873ffe10e67SVladimir Oltean 
18746cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
18756cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
18766cf99c13SVladimir Oltean 
18776cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
18786cf99c13SVladimir Oltean 	if (rc < 0)
18796cf99c13SVladimir Oltean 		goto out_unlock_ptp;
18806cf99c13SVladimir Oltean 
18816666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
18826666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
18836666cebcSVladimir Oltean 	if (rc < 0)
18846cf99c13SVladimir Oltean 		goto out_unlock_ptp;
18856cf99c13SVladimir Oltean 
18866cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
18876cf99c13SVladimir Oltean 	if (rc < 0)
18886cf99c13SVladimir Oltean 		goto out_unlock_ptp;
18896cf99c13SVladimir Oltean 
18906cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
18916cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
18926cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
18936cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
18946cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
18956cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
18966cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
18976cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
18986cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
18996cf99c13SVladimir Oltean 	now += (t34 - t12);
19006cf99c13SVladimir Oltean 
19016cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
19026cf99c13SVladimir Oltean 
19036cf99c13SVladimir Oltean out_unlock_ptp:
19046cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
19056666cebcSVladimir Oltean 
19062eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
19072eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
19082eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
19092eea1fa8SVladimir Oltean 
19106666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
19116666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
19126666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
19136666cebcSVladimir Oltean 	 */
1914c5037678SVladimir Oltean 	rc = priv->info->clocking_setup(priv);
19156666cebcSVladimir Oltean 	if (rc < 0)
19166666cebcSVladimir Oltean 		goto out;
19176666cebcSVladimir Oltean 
1918542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1919*3ad1d171SVladimir Oltean 		struct dw_xpcs *xpcs = priv->xpcs[i];
1920*3ad1d171SVladimir Oltean 		unsigned int mode;
192184db00f2SVladimir Oltean 
19228400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
19236666cebcSVladimir Oltean 		if (rc < 0)
19246666cebcSVladimir Oltean 			goto out;
1925ffe10e67SVladimir Oltean 
1926*3ad1d171SVladimir Oltean 		if (!xpcs)
192784db00f2SVladimir Oltean 			continue;
1928ffe10e67SVladimir Oltean 
1929*3ad1d171SVladimir Oltean 		if (bmcr[i] & BMCR_ANENABLE)
1930*3ad1d171SVladimir Oltean 			mode = MLO_AN_INBAND;
1931*3ad1d171SVladimir Oltean 		else if (priv->fixed_link[i])
1932*3ad1d171SVladimir Oltean 			mode = MLO_AN_FIXED;
1933*3ad1d171SVladimir Oltean 		else
1934*3ad1d171SVladimir Oltean 			mode = MLO_AN_PHY;
193584db00f2SVladimir Oltean 
1936*3ad1d171SVladimir Oltean 		rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode);
1937*3ad1d171SVladimir Oltean 		if (rc < 0)
1938*3ad1d171SVladimir Oltean 			goto out;
1939ffe10e67SVladimir Oltean 
1940*3ad1d171SVladimir Oltean 		if (!phylink_autoneg_inband(mode)) {
1941ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
1942ffe10e67SVladimir Oltean 
194384db00f2SVladimir Oltean 			if (bmcr[i] & BMCR_SPEED1000)
1944ffe10e67SVladimir Oltean 				speed = SPEED_1000;
194584db00f2SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED100)
1946ffe10e67SVladimir Oltean 				speed = SPEED_100;
1947053d8ad1SVladimir Oltean 			else
1948ffe10e67SVladimir Oltean 				speed = SPEED_10;
1949ffe10e67SVladimir Oltean 
1950*3ad1d171SVladimir Oltean 			xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i],
1951*3ad1d171SVladimir Oltean 				     speed, DUPLEX_FULL);
1952ffe10e67SVladimir Oltean 		}
1953ffe10e67SVladimir Oltean 	}
19544d752508SVladimir Oltean 
19554d752508SVladimir Oltean 	rc = sja1105_reload_cbs(priv);
19564d752508SVladimir Oltean 	if (rc < 0)
19574d752508SVladimir Oltean 		goto out;
19586666cebcSVladimir Oltean out:
1959af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1960af580ae2SVladimir Oltean 
19616666cebcSVladimir Oltean 	return rc;
19626666cebcSVladimir Oltean }
19636666cebcSVladimir Oltean 
19646666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
19656666cebcSVladimir Oltean {
19666666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
19676666cebcSVladimir Oltean 
19686666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
19696666cebcSVladimir Oltean 
19706666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
19716666cebcSVladimir Oltean 
19726666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
19736666cebcSVladimir Oltean 					   &mac[port], true);
19746666cebcSVladimir Oltean }
19756666cebcSVladimir Oltean 
1976ac02a451SVladimir Oltean static int sja1105_crosschip_bridge_join(struct dsa_switch *ds,
1977ac02a451SVladimir Oltean 					 int tree_index, int sw_index,
1978ac02a451SVladimir Oltean 					 int other_port, struct net_device *br)
1979ac02a451SVladimir Oltean {
1980ac02a451SVladimir Oltean 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1981ac02a451SVladimir Oltean 	struct sja1105_private *other_priv = other_ds->priv;
1982ac02a451SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1983ac02a451SVladimir Oltean 	int port, rc;
1984ac02a451SVladimir Oltean 
1985ac02a451SVladimir Oltean 	if (other_ds->ops != &sja1105_switch_ops)
1986ac02a451SVladimir Oltean 		return 0;
1987ac02a451SVladimir Oltean 
1988ac02a451SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1989ac02a451SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
1990ac02a451SVladimir Oltean 			continue;
1991ac02a451SVladimir Oltean 		if (dsa_to_port(ds, port)->bridge_dev != br)
1992ac02a451SVladimir Oltean 			continue;
1993ac02a451SVladimir Oltean 
19945899ee36SVladimir Oltean 		rc = dsa_8021q_crosschip_bridge_join(priv->dsa_8021q_ctx,
19955899ee36SVladimir Oltean 						     port,
19965899ee36SVladimir Oltean 						     other_priv->dsa_8021q_ctx,
19975899ee36SVladimir Oltean 						     other_port);
1998ac02a451SVladimir Oltean 		if (rc)
1999ac02a451SVladimir Oltean 			return rc;
2000ac02a451SVladimir Oltean 
20015899ee36SVladimir Oltean 		rc = dsa_8021q_crosschip_bridge_join(other_priv->dsa_8021q_ctx,
20025899ee36SVladimir Oltean 						     other_port,
20035899ee36SVladimir Oltean 						     priv->dsa_8021q_ctx,
20045899ee36SVladimir Oltean 						     port);
2005ac02a451SVladimir Oltean 		if (rc)
2006ac02a451SVladimir Oltean 			return rc;
2007ac02a451SVladimir Oltean 	}
2008ac02a451SVladimir Oltean 
2009ac02a451SVladimir Oltean 	return 0;
2010ac02a451SVladimir Oltean }
2011ac02a451SVladimir Oltean 
2012ac02a451SVladimir Oltean static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds,
2013ac02a451SVladimir Oltean 					   int tree_index, int sw_index,
2014ac02a451SVladimir Oltean 					   int other_port,
2015ac02a451SVladimir Oltean 					   struct net_device *br)
2016ac02a451SVladimir Oltean {
2017ac02a451SVladimir Oltean 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
2018ac02a451SVladimir Oltean 	struct sja1105_private *other_priv = other_ds->priv;
2019ac02a451SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2020ac02a451SVladimir Oltean 	int port;
2021ac02a451SVladimir Oltean 
2022ac02a451SVladimir Oltean 	if (other_ds->ops != &sja1105_switch_ops)
2023ac02a451SVladimir Oltean 		return;
2024ac02a451SVladimir Oltean 
2025ac02a451SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
2026ac02a451SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2027ac02a451SVladimir Oltean 			continue;
2028ac02a451SVladimir Oltean 		if (dsa_to_port(ds, port)->bridge_dev != br)
2029ac02a451SVladimir Oltean 			continue;
2030ac02a451SVladimir Oltean 
20315899ee36SVladimir Oltean 		dsa_8021q_crosschip_bridge_leave(priv->dsa_8021q_ctx, port,
20325899ee36SVladimir Oltean 						 other_priv->dsa_8021q_ctx,
20335899ee36SVladimir Oltean 						 other_port);
2034ac02a451SVladimir Oltean 
20355899ee36SVladimir Oltean 		dsa_8021q_crosschip_bridge_leave(other_priv->dsa_8021q_ctx,
20365899ee36SVladimir Oltean 						 other_port,
20375899ee36SVladimir Oltean 						 priv->dsa_8021q_ctx, port);
2038ac02a451SVladimir Oltean 	}
2039ac02a451SVladimir Oltean }
2040ac02a451SVladimir Oltean 
2041227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
2042227d07a0SVladimir Oltean {
204360b33aebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
20447e092af2SVladimir Oltean 	int rc;
2045227d07a0SVladimir Oltean 
20465899ee36SVladimir Oltean 	rc = dsa_8021q_setup(priv->dsa_8021q_ctx, enabled);
20477e092af2SVladimir Oltean 	if (rc)
2048227d07a0SVladimir Oltean 		return rc;
2049ac02a451SVladimir Oltean 
2050227d07a0SVladimir Oltean 	dev_info(ds->dev, "%s switch tagging\n",
2051227d07a0SVladimir Oltean 		 enabled ? "Enabled" : "Disabled");
2052227d07a0SVladimir Oltean 	return 0;
2053227d07a0SVladimir Oltean }
2054227d07a0SVladimir Oltean 
20558aa9ebccSVladimir Oltean static enum dsa_tag_protocol
20564d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
20574d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
20588aa9ebccSVladimir Oltean {
20594913b8ebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
20604913b8ebSVladimir Oltean 
20614913b8ebSVladimir Oltean 	return priv->info->tag_proto;
20628aa9ebccSVladimir Oltean }
20638aa9ebccSVladimir Oltean 
20643f01c91aSVladimir Oltean static int sja1105_find_free_subvlan(u16 *subvlan_map, bool pvid)
20653f01c91aSVladimir Oltean {
20663f01c91aSVladimir Oltean 	int subvlan;
20673f01c91aSVladimir Oltean 
20683f01c91aSVladimir Oltean 	if (pvid)
20693f01c91aSVladimir Oltean 		return 0;
20703f01c91aSVladimir Oltean 
20713f01c91aSVladimir Oltean 	for (subvlan = 1; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
20723f01c91aSVladimir Oltean 		if (subvlan_map[subvlan] == VLAN_N_VID)
20733f01c91aSVladimir Oltean 			return subvlan;
20743f01c91aSVladimir Oltean 
20753f01c91aSVladimir Oltean 	return -1;
20763f01c91aSVladimir Oltean }
20773f01c91aSVladimir Oltean 
20783f01c91aSVladimir Oltean static int sja1105_find_subvlan(u16 *subvlan_map, u16 vid)
20793f01c91aSVladimir Oltean {
20803f01c91aSVladimir Oltean 	int subvlan;
20813f01c91aSVladimir Oltean 
20823f01c91aSVladimir Oltean 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
20833f01c91aSVladimir Oltean 		if (subvlan_map[subvlan] == vid)
20843f01c91aSVladimir Oltean 			return subvlan;
20853f01c91aSVladimir Oltean 
20863f01c91aSVladimir Oltean 	return -1;
20873f01c91aSVladimir Oltean }
20883f01c91aSVladimir Oltean 
20893f01c91aSVladimir Oltean static int sja1105_find_committed_subvlan(struct sja1105_private *priv,
20903f01c91aSVladimir Oltean 					  int port, u16 vid)
20913f01c91aSVladimir Oltean {
20923f01c91aSVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
20933f01c91aSVladimir Oltean 
20943f01c91aSVladimir Oltean 	return sja1105_find_subvlan(sp->subvlan_map, vid);
20953f01c91aSVladimir Oltean }
20963f01c91aSVladimir Oltean 
20973f01c91aSVladimir Oltean static void sja1105_init_subvlan_map(u16 *subvlan_map)
20983f01c91aSVladimir Oltean {
20993f01c91aSVladimir Oltean 	int subvlan;
21003f01c91aSVladimir Oltean 
21013f01c91aSVladimir Oltean 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
21023f01c91aSVladimir Oltean 		subvlan_map[subvlan] = VLAN_N_VID;
21033f01c91aSVladimir Oltean }
21043f01c91aSVladimir Oltean 
21053f01c91aSVladimir Oltean static void sja1105_commit_subvlan_map(struct sja1105_private *priv, int port,
21063f01c91aSVladimir Oltean 				       u16 *subvlan_map)
21073f01c91aSVladimir Oltean {
21083f01c91aSVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
21093f01c91aSVladimir Oltean 	int subvlan;
21103f01c91aSVladimir Oltean 
21113f01c91aSVladimir Oltean 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
21123f01c91aSVladimir Oltean 		sp->subvlan_map[subvlan] = subvlan_map[subvlan];
21133f01c91aSVladimir Oltean }
21143f01c91aSVladimir Oltean 
2115ec5ae610SVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
2116ec5ae610SVladimir Oltean {
2117ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
2118ec5ae610SVladimir Oltean 	int count, i;
2119ec5ae610SVladimir Oltean 
2120ec5ae610SVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
2121ec5ae610SVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
2122ec5ae610SVladimir Oltean 
2123ec5ae610SVladimir Oltean 	for (i = 0; i < count; i++)
2124ec5ae610SVladimir Oltean 		if (vlan[i].vlanid == vid)
2125ec5ae610SVladimir Oltean 			return i;
2126ec5ae610SVladimir Oltean 
2127ec5ae610SVladimir Oltean 	/* Return an invalid entry index if not found */
2128ec5ae610SVladimir Oltean 	return -1;
2129ec5ae610SVladimir Oltean }
2130ec5ae610SVladimir Oltean 
21313f01c91aSVladimir Oltean static int
21323f01c91aSVladimir Oltean sja1105_find_retagging_entry(struct sja1105_retagging_entry *retagging,
21333f01c91aSVladimir Oltean 			     int count, int from_port, u16 from_vid,
21343f01c91aSVladimir Oltean 			     u16 to_vid)
2135ec5ae610SVladimir Oltean {
21363f01c91aSVladimir Oltean 	int i;
21373f01c91aSVladimir Oltean 
21383f01c91aSVladimir Oltean 	for (i = 0; i < count; i++)
21393f01c91aSVladimir Oltean 		if (retagging[i].ing_port == BIT(from_port) &&
21403f01c91aSVladimir Oltean 		    retagging[i].vlan_ing == from_vid &&
21413f01c91aSVladimir Oltean 		    retagging[i].vlan_egr == to_vid)
21423f01c91aSVladimir Oltean 			return i;
21433f01c91aSVladimir Oltean 
21443f01c91aSVladimir Oltean 	/* Return an invalid entry index if not found */
21453f01c91aSVladimir Oltean 	return -1;
21463f01c91aSVladimir Oltean }
21473f01c91aSVladimir Oltean 
21483f01c91aSVladimir Oltean static int sja1105_commit_vlans(struct sja1105_private *priv,
21493f01c91aSVladimir Oltean 				struct sja1105_vlan_lookup_entry *new_vlan,
21503f01c91aSVladimir Oltean 				struct sja1105_retagging_entry *new_retagging,
21513f01c91aSVladimir Oltean 				int num_retagging)
21523f01c91aSVladimir Oltean {
21533f01c91aSVladimir Oltean 	struct sja1105_retagging_entry *retagging;
2154ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
2155ec5ae610SVladimir Oltean 	struct sja1105_table *table;
2156ec5ae610SVladimir Oltean 	int num_vlans = 0;
2157ec5ae610SVladimir Oltean 	int rc, i, k = 0;
2158ec5ae610SVladimir Oltean 
2159ec5ae610SVladimir Oltean 	/* VLAN table */
2160ec5ae610SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2161ec5ae610SVladimir Oltean 	vlan = table->entries;
2162ec5ae610SVladimir Oltean 
2163ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++) {
2164ec5ae610SVladimir Oltean 		int match = sja1105_is_vlan_configured(priv, i);
2165ec5ae610SVladimir Oltean 
2166ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid != VLAN_N_VID)
2167ec5ae610SVladimir Oltean 			num_vlans++;
2168ec5ae610SVladimir Oltean 
2169ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) {
2170ec5ae610SVladimir Oltean 			/* Was there before, no longer is. Delete */
2171ec5ae610SVladimir Oltean 			dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i);
2172ec5ae610SVladimir Oltean 			rc = sja1105_dynamic_config_write(priv,
2173ec5ae610SVladimir Oltean 							  BLK_IDX_VLAN_LOOKUP,
2174ec5ae610SVladimir Oltean 							  i, &vlan[match], false);
2175ec5ae610SVladimir Oltean 			if (rc < 0)
2176ec5ae610SVladimir Oltean 				return rc;
2177ec5ae610SVladimir Oltean 		} else if (new_vlan[i].vlanid != VLAN_N_VID) {
2178ec5ae610SVladimir Oltean 			/* Nothing changed, don't do anything */
2179ec5ae610SVladimir Oltean 			if (match >= 0 &&
2180ec5ae610SVladimir Oltean 			    vlan[match].vlanid == new_vlan[i].vlanid &&
2181ec5ae610SVladimir Oltean 			    vlan[match].tag_port == new_vlan[i].tag_port &&
2182ec5ae610SVladimir Oltean 			    vlan[match].vlan_bc == new_vlan[i].vlan_bc &&
2183ec5ae610SVladimir Oltean 			    vlan[match].vmemb_port == new_vlan[i].vmemb_port)
2184ec5ae610SVladimir Oltean 				continue;
2185ec5ae610SVladimir Oltean 			/* Update entry */
2186ec5ae610SVladimir Oltean 			dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i);
2187ec5ae610SVladimir Oltean 			rc = sja1105_dynamic_config_write(priv,
2188ec5ae610SVladimir Oltean 							  BLK_IDX_VLAN_LOOKUP,
2189ec5ae610SVladimir Oltean 							  i, &new_vlan[i],
2190ec5ae610SVladimir Oltean 							  true);
2191ec5ae610SVladimir Oltean 			if (rc < 0)
2192ec5ae610SVladimir Oltean 				return rc;
2193ec5ae610SVladimir Oltean 		}
2194ec5ae610SVladimir Oltean 	}
2195ec5ae610SVladimir Oltean 
2196ec5ae610SVladimir Oltean 	if (table->entry_count)
2197ec5ae610SVladimir Oltean 		kfree(table->entries);
2198ec5ae610SVladimir Oltean 
2199ec5ae610SVladimir Oltean 	table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size,
2200ec5ae610SVladimir Oltean 				 GFP_KERNEL);
2201ec5ae610SVladimir Oltean 	if (!table->entries)
2202ec5ae610SVladimir Oltean 		return -ENOMEM;
2203ec5ae610SVladimir Oltean 
2204ec5ae610SVladimir Oltean 	table->entry_count = num_vlans;
2205ec5ae610SVladimir Oltean 	vlan = table->entries;
2206ec5ae610SVladimir Oltean 
2207ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++) {
2208ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid == VLAN_N_VID)
2209ec5ae610SVladimir Oltean 			continue;
2210ec5ae610SVladimir Oltean 		vlan[k++] = new_vlan[i];
2211ec5ae610SVladimir Oltean 	}
2212ec5ae610SVladimir Oltean 
22133f01c91aSVladimir Oltean 	/* VLAN Retagging Table */
22143f01c91aSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_RETAGGING];
22153f01c91aSVladimir Oltean 	retagging = table->entries;
22163f01c91aSVladimir Oltean 
22173f01c91aSVladimir Oltean 	for (i = 0; i < table->entry_count; i++) {
22183f01c91aSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
22193f01c91aSVladimir Oltean 						  i, &retagging[i], false);
22203f01c91aSVladimir Oltean 		if (rc)
22213f01c91aSVladimir Oltean 			return rc;
22223f01c91aSVladimir Oltean 	}
22233f01c91aSVladimir Oltean 
22243f01c91aSVladimir Oltean 	if (table->entry_count)
22253f01c91aSVladimir Oltean 		kfree(table->entries);
22263f01c91aSVladimir Oltean 
22273f01c91aSVladimir Oltean 	table->entries = kcalloc(num_retagging, table->ops->unpacked_entry_size,
22283f01c91aSVladimir Oltean 				 GFP_KERNEL);
22293f01c91aSVladimir Oltean 	if (!table->entries)
22303f01c91aSVladimir Oltean 		return -ENOMEM;
22313f01c91aSVladimir Oltean 
22323f01c91aSVladimir Oltean 	table->entry_count = num_retagging;
22333f01c91aSVladimir Oltean 	retagging = table->entries;
22343f01c91aSVladimir Oltean 
22353f01c91aSVladimir Oltean 	for (i = 0; i < num_retagging; i++) {
22363f01c91aSVladimir Oltean 		retagging[i] = new_retagging[i];
22373f01c91aSVladimir Oltean 
22383f01c91aSVladimir Oltean 		/* Update entry */
22393f01c91aSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
22403f01c91aSVladimir Oltean 						  i, &retagging[i], true);
22413f01c91aSVladimir Oltean 		if (rc < 0)
22423f01c91aSVladimir Oltean 			return rc;
22433f01c91aSVladimir Oltean 	}
22443f01c91aSVladimir Oltean 
2245ec5ae610SVladimir Oltean 	return 0;
2246ec5ae610SVladimir Oltean }
2247ec5ae610SVladimir Oltean 
22483f01c91aSVladimir Oltean struct sja1105_crosschip_vlan {
22493f01c91aSVladimir Oltean 	struct list_head list;
22503f01c91aSVladimir Oltean 	u16 vid;
22513f01c91aSVladimir Oltean 	bool untagged;
22523f01c91aSVladimir Oltean 	int port;
22533f01c91aSVladimir Oltean 	int other_port;
22545899ee36SVladimir Oltean 	struct dsa_8021q_context *other_ctx;
22553f01c91aSVladimir Oltean };
22563f01c91aSVladimir Oltean 
2257ec5ae610SVladimir Oltean struct sja1105_crosschip_switch {
2258ec5ae610SVladimir Oltean 	struct list_head list;
22595899ee36SVladimir Oltean 	struct dsa_8021q_context *other_ctx;
2260ec5ae610SVladimir Oltean };
2261ec5ae610SVladimir Oltean 
2262ec5ae610SVladimir Oltean static int sja1105_commit_pvid(struct sja1105_private *priv)
2263ec5ae610SVladimir Oltean {
2264ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
2265ec5ae610SVladimir Oltean 	struct list_head *vlan_list;
2266ec5ae610SVladimir Oltean 	int rc = 0;
2267ec5ae610SVladimir Oltean 
2268ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2269ec5ae610SVladimir Oltean 		vlan_list = &priv->bridge_vlans;
2270ec5ae610SVladimir Oltean 	else
2271ec5ae610SVladimir Oltean 		vlan_list = &priv->dsa_8021q_vlans;
2272ec5ae610SVladimir Oltean 
2273ec5ae610SVladimir Oltean 	list_for_each_entry(v, vlan_list, list) {
2274ec5ae610SVladimir Oltean 		if (v->pvid) {
2275ec5ae610SVladimir Oltean 			rc = sja1105_pvid_apply(priv, v->port, v->vid);
2276ec5ae610SVladimir Oltean 			if (rc)
2277ec5ae610SVladimir Oltean 				break;
2278ec5ae610SVladimir Oltean 		}
2279ec5ae610SVladimir Oltean 	}
2280ec5ae610SVladimir Oltean 
2281ec5ae610SVladimir Oltean 	return rc;
2282ec5ae610SVladimir Oltean }
2283ec5ae610SVladimir Oltean 
2284ec5ae610SVladimir Oltean static int
2285ec5ae610SVladimir Oltean sja1105_build_bridge_vlans(struct sja1105_private *priv,
2286ec5ae610SVladimir Oltean 			   struct sja1105_vlan_lookup_entry *new_vlan)
2287ec5ae610SVladimir Oltean {
2288ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
2289ec5ae610SVladimir Oltean 
2290ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
2291ec5ae610SVladimir Oltean 		return 0;
2292ec5ae610SVladimir Oltean 
2293ec5ae610SVladimir Oltean 	list_for_each_entry(v, &priv->bridge_vlans, list) {
2294ec5ae610SVladimir Oltean 		int match = v->vid;
2295ec5ae610SVladimir Oltean 
2296ec5ae610SVladimir Oltean 		new_vlan[match].vlanid = v->vid;
2297ec5ae610SVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(v->port);
2298ec5ae610SVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(v->port);
2299ec5ae610SVladimir Oltean 		if (!v->untagged)
2300ec5ae610SVladimir Oltean 			new_vlan[match].tag_port |= BIT(v->port);
23013e77e59bSVladimir Oltean 		new_vlan[match].type_entry = SJA1110_VLAN_D_TAG;
2302ec5ae610SVladimir Oltean 	}
2303ec5ae610SVladimir Oltean 
2304ec5ae610SVladimir Oltean 	return 0;
2305ec5ae610SVladimir Oltean }
2306ec5ae610SVladimir Oltean 
2307ec5ae610SVladimir Oltean static int
2308ec5ae610SVladimir Oltean sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv,
2309ec5ae610SVladimir Oltean 			      struct sja1105_vlan_lookup_entry *new_vlan)
2310ec5ae610SVladimir Oltean {
2311ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
2312ec5ae610SVladimir Oltean 
2313ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2314ec5ae610SVladimir Oltean 		return 0;
2315ec5ae610SVladimir Oltean 
2316ec5ae610SVladimir Oltean 	list_for_each_entry(v, &priv->dsa_8021q_vlans, list) {
2317ec5ae610SVladimir Oltean 		int match = v->vid;
2318ec5ae610SVladimir Oltean 
2319ec5ae610SVladimir Oltean 		new_vlan[match].vlanid = v->vid;
2320ec5ae610SVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(v->port);
2321ec5ae610SVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(v->port);
2322ec5ae610SVladimir Oltean 		if (!v->untagged)
2323ec5ae610SVladimir Oltean 			new_vlan[match].tag_port |= BIT(v->port);
23243e77e59bSVladimir Oltean 		new_vlan[match].type_entry = SJA1110_VLAN_D_TAG;
2325ec5ae610SVladimir Oltean 	}
2326ec5ae610SVladimir Oltean 
2327ec5ae610SVladimir Oltean 	return 0;
2328ec5ae610SVladimir Oltean }
2329ec5ae610SVladimir Oltean 
23303f01c91aSVladimir Oltean static int sja1105_build_subvlans(struct sja1105_private *priv,
23313f01c91aSVladimir Oltean 				  u16 subvlan_map[][DSA_8021Q_N_SUBVLAN],
23323f01c91aSVladimir Oltean 				  struct sja1105_vlan_lookup_entry *new_vlan,
23333f01c91aSVladimir Oltean 				  struct sja1105_retagging_entry *new_retagging,
23343f01c91aSVladimir Oltean 				  int *num_retagging)
23353f01c91aSVladimir Oltean {
23363f01c91aSVladimir Oltean 	struct sja1105_bridge_vlan *v;
23373f01c91aSVladimir Oltean 	int k = *num_retagging;
23383f01c91aSVladimir Oltean 
23393f01c91aSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
23403f01c91aSVladimir Oltean 		return 0;
23413f01c91aSVladimir Oltean 
23423f01c91aSVladimir Oltean 	list_for_each_entry(v, &priv->bridge_vlans, list) {
23433f01c91aSVladimir Oltean 		int upstream = dsa_upstream_port(priv->ds, v->port);
23443f01c91aSVladimir Oltean 		int match, subvlan;
23453f01c91aSVladimir Oltean 		u16 rx_vid;
23463f01c91aSVladimir Oltean 
23473f01c91aSVladimir Oltean 		/* Only sub-VLANs on user ports need to be applied.
23483f01c91aSVladimir Oltean 		 * Bridge VLANs also include VLANs added automatically
23493f01c91aSVladimir Oltean 		 * by DSA on the CPU port.
23503f01c91aSVladimir Oltean 		 */
23513f01c91aSVladimir Oltean 		if (!dsa_is_user_port(priv->ds, v->port))
23523f01c91aSVladimir Oltean 			continue;
23533f01c91aSVladimir Oltean 
23543f01c91aSVladimir Oltean 		subvlan = sja1105_find_subvlan(subvlan_map[v->port],
23553f01c91aSVladimir Oltean 					       v->vid);
23563f01c91aSVladimir Oltean 		if (subvlan < 0) {
23573f01c91aSVladimir Oltean 			subvlan = sja1105_find_free_subvlan(subvlan_map[v->port],
23583f01c91aSVladimir Oltean 							    v->pvid);
23593f01c91aSVladimir Oltean 			if (subvlan < 0) {
23603f01c91aSVladimir Oltean 				dev_err(priv->ds->dev, "No more free subvlans\n");
23613f01c91aSVladimir Oltean 				return -ENOSPC;
23623f01c91aSVladimir Oltean 			}
23633f01c91aSVladimir Oltean 		}
23643f01c91aSVladimir Oltean 
23653f01c91aSVladimir Oltean 		rx_vid = dsa_8021q_rx_vid_subvlan(priv->ds, v->port, subvlan);
23663f01c91aSVladimir Oltean 
23673f01c91aSVladimir Oltean 		/* @v->vid on @v->port needs to be retagged to @rx_vid
23683f01c91aSVladimir Oltean 		 * on @upstream. Assume @v->vid on @v->port and on
23693f01c91aSVladimir Oltean 		 * @upstream was already configured by the previous
23703f01c91aSVladimir Oltean 		 * iteration over bridge_vlans.
23713f01c91aSVladimir Oltean 		 */
23723f01c91aSVladimir Oltean 		match = rx_vid;
23733f01c91aSVladimir Oltean 		new_vlan[match].vlanid = rx_vid;
23743f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(v->port);
23753f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(upstream);
23763f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(v->port);
23773f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(upstream);
23783f01c91aSVladimir Oltean 		/* The "untagged" flag is set the same as for the
23793f01c91aSVladimir Oltean 		 * original VLAN
23803f01c91aSVladimir Oltean 		 */
23813f01c91aSVladimir Oltean 		if (!v->untagged)
23823f01c91aSVladimir Oltean 			new_vlan[match].tag_port |= BIT(v->port);
23833f01c91aSVladimir Oltean 		/* But it's always tagged towards the CPU */
23843f01c91aSVladimir Oltean 		new_vlan[match].tag_port |= BIT(upstream);
23853e77e59bSVladimir Oltean 		new_vlan[match].type_entry = SJA1110_VLAN_D_TAG;
23863f01c91aSVladimir Oltean 
23873f01c91aSVladimir Oltean 		/* The Retagging Table generates packet *clones* with
23883f01c91aSVladimir Oltean 		 * the new VLAN. This is a very odd hardware quirk
23893f01c91aSVladimir Oltean 		 * which we need to suppress by dropping the original
23903f01c91aSVladimir Oltean 		 * packet.
23913f01c91aSVladimir Oltean 		 * Deny egress of the original VLAN towards the CPU
23923f01c91aSVladimir Oltean 		 * port. This will force the switch to drop it, and
23933f01c91aSVladimir Oltean 		 * we'll see only the retagged packets.
23943f01c91aSVladimir Oltean 		 */
23953f01c91aSVladimir Oltean 		match = v->vid;
23963f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc &= ~BIT(upstream);
23973f01c91aSVladimir Oltean 
23983f01c91aSVladimir Oltean 		/* And the retagging itself */
23993f01c91aSVladimir Oltean 		new_retagging[k].vlan_ing = v->vid;
24003f01c91aSVladimir Oltean 		new_retagging[k].vlan_egr = rx_vid;
24013f01c91aSVladimir Oltean 		new_retagging[k].ing_port = BIT(v->port);
24023f01c91aSVladimir Oltean 		new_retagging[k].egr_port = BIT(upstream);
24033f01c91aSVladimir Oltean 		if (k++ == SJA1105_MAX_RETAGGING_COUNT) {
24043f01c91aSVladimir Oltean 			dev_err(priv->ds->dev, "No more retagging rules\n");
24053f01c91aSVladimir Oltean 			return -ENOSPC;
24063f01c91aSVladimir Oltean 		}
24073f01c91aSVladimir Oltean 
24083f01c91aSVladimir Oltean 		subvlan_map[v->port][subvlan] = v->vid;
24093f01c91aSVladimir Oltean 	}
24103f01c91aSVladimir Oltean 
24113f01c91aSVladimir Oltean 	*num_retagging = k;
24123f01c91aSVladimir Oltean 
24133f01c91aSVladimir Oltean 	return 0;
24143f01c91aSVladimir Oltean }
24153f01c91aSVladimir Oltean 
24163f01c91aSVladimir Oltean /* Sadly, in crosschip scenarios where the CPU port is also the link to another
24173f01c91aSVladimir Oltean  * switch, we should retag backwards (the dsa_8021q vid to the original vid) on
24183f01c91aSVladimir Oltean  * the CPU port of neighbour switches.
24193f01c91aSVladimir Oltean  */
24203f01c91aSVladimir Oltean static int
24213f01c91aSVladimir Oltean sja1105_build_crosschip_subvlans(struct sja1105_private *priv,
24223f01c91aSVladimir Oltean 				 struct sja1105_vlan_lookup_entry *new_vlan,
24233f01c91aSVladimir Oltean 				 struct sja1105_retagging_entry *new_retagging,
24243f01c91aSVladimir Oltean 				 int *num_retagging)
24253f01c91aSVladimir Oltean {
24263f01c91aSVladimir Oltean 	struct sja1105_crosschip_vlan *tmp, *pos;
24273f01c91aSVladimir Oltean 	struct dsa_8021q_crosschip_link *c;
24283f01c91aSVladimir Oltean 	struct sja1105_bridge_vlan *v, *w;
24293f01c91aSVladimir Oltean 	struct list_head crosschip_vlans;
24303f01c91aSVladimir Oltean 	int k = *num_retagging;
24313f01c91aSVladimir Oltean 	int rc = 0;
24323f01c91aSVladimir Oltean 
24333f01c91aSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
24343f01c91aSVladimir Oltean 		return 0;
24353f01c91aSVladimir Oltean 
24363f01c91aSVladimir Oltean 	INIT_LIST_HEAD(&crosschip_vlans);
24373f01c91aSVladimir Oltean 
24385899ee36SVladimir Oltean 	list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
24395899ee36SVladimir Oltean 		struct sja1105_private *other_priv = c->other_ctx->ds->priv;
24403f01c91aSVladimir Oltean 
24413f01c91aSVladimir Oltean 		if (other_priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
24423f01c91aSVladimir Oltean 			continue;
24433f01c91aSVladimir Oltean 
24443f01c91aSVladimir Oltean 		/* Crosschip links are also added to the CPU ports.
24453f01c91aSVladimir Oltean 		 * Ignore those.
24463f01c91aSVladimir Oltean 		 */
24473f01c91aSVladimir Oltean 		if (!dsa_is_user_port(priv->ds, c->port))
24483f01c91aSVladimir Oltean 			continue;
24495899ee36SVladimir Oltean 		if (!dsa_is_user_port(c->other_ctx->ds, c->other_port))
24503f01c91aSVladimir Oltean 			continue;
24513f01c91aSVladimir Oltean 
24523f01c91aSVladimir Oltean 		/* Search for VLANs on the remote port */
24533f01c91aSVladimir Oltean 		list_for_each_entry(v, &other_priv->bridge_vlans, list) {
24543f01c91aSVladimir Oltean 			bool already_added = false;
24553f01c91aSVladimir Oltean 			bool we_have_it = false;
24563f01c91aSVladimir Oltean 
24573f01c91aSVladimir Oltean 			if (v->port != c->other_port)
24583f01c91aSVladimir Oltean 				continue;
24593f01c91aSVladimir Oltean 
24603f01c91aSVladimir Oltean 			/* If @v is a pvid on @other_ds, it does not need
24613f01c91aSVladimir Oltean 			 * re-retagging, because its SVL field is 0 and we
24623f01c91aSVladimir Oltean 			 * already allow that, via the dsa_8021q crosschip
24633f01c91aSVladimir Oltean 			 * links.
24643f01c91aSVladimir Oltean 			 */
24653f01c91aSVladimir Oltean 			if (v->pvid)
24663f01c91aSVladimir Oltean 				continue;
24673f01c91aSVladimir Oltean 
24683f01c91aSVladimir Oltean 			/* Search for the VLAN on our local port */
24693f01c91aSVladimir Oltean 			list_for_each_entry(w, &priv->bridge_vlans, list) {
24703f01c91aSVladimir Oltean 				if (w->port == c->port && w->vid == v->vid) {
24713f01c91aSVladimir Oltean 					we_have_it = true;
24723f01c91aSVladimir Oltean 					break;
24733f01c91aSVladimir Oltean 				}
24743f01c91aSVladimir Oltean 			}
24753f01c91aSVladimir Oltean 
24763f01c91aSVladimir Oltean 			if (!we_have_it)
24773f01c91aSVladimir Oltean 				continue;
24783f01c91aSVladimir Oltean 
24793f01c91aSVladimir Oltean 			list_for_each_entry(tmp, &crosschip_vlans, list) {
24803f01c91aSVladimir Oltean 				if (tmp->vid == v->vid &&
24813f01c91aSVladimir Oltean 				    tmp->untagged == v->untagged &&
24823f01c91aSVladimir Oltean 				    tmp->port == c->port &&
24833f01c91aSVladimir Oltean 				    tmp->other_port == v->port &&
24845899ee36SVladimir Oltean 				    tmp->other_ctx == c->other_ctx) {
24853f01c91aSVladimir Oltean 					already_added = true;
24863f01c91aSVladimir Oltean 					break;
24873f01c91aSVladimir Oltean 				}
24883f01c91aSVladimir Oltean 			}
24893f01c91aSVladimir Oltean 
24903f01c91aSVladimir Oltean 			if (already_added)
24913f01c91aSVladimir Oltean 				continue;
24923f01c91aSVladimir Oltean 
24933f01c91aSVladimir Oltean 			tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
24943f01c91aSVladimir Oltean 			if (!tmp) {
24953f01c91aSVladimir Oltean 				dev_err(priv->ds->dev, "Failed to allocate memory\n");
24963f01c91aSVladimir Oltean 				rc = -ENOMEM;
24973f01c91aSVladimir Oltean 				goto out;
24983f01c91aSVladimir Oltean 			}
24993f01c91aSVladimir Oltean 			tmp->vid = v->vid;
25003f01c91aSVladimir Oltean 			tmp->port = c->port;
25013f01c91aSVladimir Oltean 			tmp->other_port = v->port;
25025899ee36SVladimir Oltean 			tmp->other_ctx = c->other_ctx;
25033f01c91aSVladimir Oltean 			tmp->untagged = v->untagged;
25043f01c91aSVladimir Oltean 			list_add(&tmp->list, &crosschip_vlans);
25053f01c91aSVladimir Oltean 		}
25063f01c91aSVladimir Oltean 	}
25073f01c91aSVladimir Oltean 
25083f01c91aSVladimir Oltean 	list_for_each_entry(tmp, &crosschip_vlans, list) {
25095899ee36SVladimir Oltean 		struct sja1105_private *other_priv = tmp->other_ctx->ds->priv;
25103f01c91aSVladimir Oltean 		int upstream = dsa_upstream_port(priv->ds, tmp->port);
25113f01c91aSVladimir Oltean 		int match, subvlan;
25123f01c91aSVladimir Oltean 		u16 rx_vid;
25133f01c91aSVladimir Oltean 
25143f01c91aSVladimir Oltean 		subvlan = sja1105_find_committed_subvlan(other_priv,
25153f01c91aSVladimir Oltean 							 tmp->other_port,
25163f01c91aSVladimir Oltean 							 tmp->vid);
25173f01c91aSVladimir Oltean 		/* If this happens, it's a bug. The neighbour switch does not
25183f01c91aSVladimir Oltean 		 * have a subvlan for tmp->vid on tmp->other_port, but it
25193f01c91aSVladimir Oltean 		 * should, since we already checked for its vlan_state.
25203f01c91aSVladimir Oltean 		 */
25213f01c91aSVladimir Oltean 		if (WARN_ON(subvlan < 0)) {
25223f01c91aSVladimir Oltean 			rc = -EINVAL;
25233f01c91aSVladimir Oltean 			goto out;
25243f01c91aSVladimir Oltean 		}
25253f01c91aSVladimir Oltean 
25265899ee36SVladimir Oltean 		rx_vid = dsa_8021q_rx_vid_subvlan(tmp->other_ctx->ds,
25273f01c91aSVladimir Oltean 						  tmp->other_port,
25283f01c91aSVladimir Oltean 						  subvlan);
25293f01c91aSVladimir Oltean 
25303f01c91aSVladimir Oltean 		/* The @rx_vid retagged from @tmp->vid on
25313f01c91aSVladimir Oltean 		 * {@tmp->other_ds, @tmp->other_port} needs to be
25323f01c91aSVladimir Oltean 		 * re-retagged to @tmp->vid on the way back to us.
25333f01c91aSVladimir Oltean 		 *
25343f01c91aSVladimir Oltean 		 * Assume the original @tmp->vid is already configured
25353f01c91aSVladimir Oltean 		 * on this local switch, otherwise we wouldn't be
25363f01c91aSVladimir Oltean 		 * retagging its subvlan on the other switch in the
25373f01c91aSVladimir Oltean 		 * first place. We just need to add a reverse retagging
25383f01c91aSVladimir Oltean 		 * rule for @rx_vid and install @rx_vid on our ports.
25393f01c91aSVladimir Oltean 		 */
25403f01c91aSVladimir Oltean 		match = rx_vid;
25413f01c91aSVladimir Oltean 		new_vlan[match].vlanid = rx_vid;
25423f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(tmp->port);
25433f01c91aSVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(upstream);
25443f01c91aSVladimir Oltean 		/* The "untagged" flag is set the same as for the
25453f01c91aSVladimir Oltean 		 * original VLAN. And towards the CPU, it doesn't
25463f01c91aSVladimir Oltean 		 * really matter, because @rx_vid will only receive
25473f01c91aSVladimir Oltean 		 * traffic on that port. For consistency with other dsa_8021q
25483f01c91aSVladimir Oltean 		 * VLANs, we'll keep the CPU port tagged.
25493f01c91aSVladimir Oltean 		 */
25503f01c91aSVladimir Oltean 		if (!tmp->untagged)
25513f01c91aSVladimir Oltean 			new_vlan[match].tag_port |= BIT(tmp->port);
25523f01c91aSVladimir Oltean 		new_vlan[match].tag_port |= BIT(upstream);
25533e77e59bSVladimir Oltean 		new_vlan[match].type_entry = SJA1110_VLAN_D_TAG;
25543f01c91aSVladimir Oltean 		/* Deny egress of @rx_vid towards our front-panel port.
25553f01c91aSVladimir Oltean 		 * This will force the switch to drop it, and we'll see
25563f01c91aSVladimir Oltean 		 * only the re-retagged packets (having the original,
25573f01c91aSVladimir Oltean 		 * pre-initial-retagging, VLAN @tmp->vid).
25583f01c91aSVladimir Oltean 		 */
25593f01c91aSVladimir Oltean 		new_vlan[match].vlan_bc &= ~BIT(tmp->port);
25603f01c91aSVladimir Oltean 
25613f01c91aSVladimir Oltean 		/* On reverse retagging, the same ingress VLAN goes to multiple
25623f01c91aSVladimir Oltean 		 * ports. So we have an opportunity to create composite rules
25633f01c91aSVladimir Oltean 		 * to not waste the limited space in the retagging table.
25643f01c91aSVladimir Oltean 		 */
25653f01c91aSVladimir Oltean 		k = sja1105_find_retagging_entry(new_retagging, *num_retagging,
25663f01c91aSVladimir Oltean 						 upstream, rx_vid, tmp->vid);
25673f01c91aSVladimir Oltean 		if (k < 0) {
25683f01c91aSVladimir Oltean 			if (*num_retagging == SJA1105_MAX_RETAGGING_COUNT) {
25693f01c91aSVladimir Oltean 				dev_err(priv->ds->dev, "No more retagging rules\n");
25703f01c91aSVladimir Oltean 				rc = -ENOSPC;
25713f01c91aSVladimir Oltean 				goto out;
25723f01c91aSVladimir Oltean 			}
25733f01c91aSVladimir Oltean 			k = (*num_retagging)++;
25743f01c91aSVladimir Oltean 		}
25753f01c91aSVladimir Oltean 		/* And the retagging itself */
25763f01c91aSVladimir Oltean 		new_retagging[k].vlan_ing = rx_vid;
25773f01c91aSVladimir Oltean 		new_retagging[k].vlan_egr = tmp->vid;
25783f01c91aSVladimir Oltean 		new_retagging[k].ing_port = BIT(upstream);
25793f01c91aSVladimir Oltean 		new_retagging[k].egr_port |= BIT(tmp->port);
25803f01c91aSVladimir Oltean 	}
25813f01c91aSVladimir Oltean 
25823f01c91aSVladimir Oltean out:
25833f01c91aSVladimir Oltean 	list_for_each_entry_safe(tmp, pos, &crosschip_vlans, list) {
25843f01c91aSVladimir Oltean 		list_del(&tmp->list);
25853f01c91aSVladimir Oltean 		kfree(tmp);
25863f01c91aSVladimir Oltean 	}
25873f01c91aSVladimir Oltean 
25883f01c91aSVladimir Oltean 	return rc;
25893f01c91aSVladimir Oltean }
25903f01c91aSVladimir Oltean 
2591ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify);
2592ec5ae610SVladimir Oltean 
2593ec5ae610SVladimir Oltean static int sja1105_notify_crosschip_switches(struct sja1105_private *priv)
2594ec5ae610SVladimir Oltean {
2595ec5ae610SVladimir Oltean 	struct sja1105_crosschip_switch *s, *pos;
2596ec5ae610SVladimir Oltean 	struct list_head crosschip_switches;
2597ec5ae610SVladimir Oltean 	struct dsa_8021q_crosschip_link *c;
2598ec5ae610SVladimir Oltean 	int rc = 0;
2599ec5ae610SVladimir Oltean 
2600ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&crosschip_switches);
2601ec5ae610SVladimir Oltean 
26025899ee36SVladimir Oltean 	list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
2603ec5ae610SVladimir Oltean 		bool already_added = false;
2604ec5ae610SVladimir Oltean 
2605ec5ae610SVladimir Oltean 		list_for_each_entry(s, &crosschip_switches, list) {
26065899ee36SVladimir Oltean 			if (s->other_ctx == c->other_ctx) {
2607ec5ae610SVladimir Oltean 				already_added = true;
2608ec5ae610SVladimir Oltean 				break;
2609ec5ae610SVladimir Oltean 			}
2610ec5ae610SVladimir Oltean 		}
2611ec5ae610SVladimir Oltean 
2612ec5ae610SVladimir Oltean 		if (already_added)
2613ec5ae610SVladimir Oltean 			continue;
2614ec5ae610SVladimir Oltean 
2615ec5ae610SVladimir Oltean 		s = kzalloc(sizeof(*s), GFP_KERNEL);
2616ec5ae610SVladimir Oltean 		if (!s) {
2617ec5ae610SVladimir Oltean 			dev_err(priv->ds->dev, "Failed to allocate memory\n");
2618ec5ae610SVladimir Oltean 			rc = -ENOMEM;
2619ec5ae610SVladimir Oltean 			goto out;
2620ec5ae610SVladimir Oltean 		}
26215899ee36SVladimir Oltean 		s->other_ctx = c->other_ctx;
2622ec5ae610SVladimir Oltean 		list_add(&s->list, &crosschip_switches);
2623ec5ae610SVladimir Oltean 	}
2624ec5ae610SVladimir Oltean 
2625ec5ae610SVladimir Oltean 	list_for_each_entry(s, &crosschip_switches, list) {
26265899ee36SVladimir Oltean 		struct sja1105_private *other_priv = s->other_ctx->ds->priv;
2627ec5ae610SVladimir Oltean 
2628ec5ae610SVladimir Oltean 		rc = sja1105_build_vlan_table(other_priv, false);
2629ec5ae610SVladimir Oltean 		if (rc)
2630ec5ae610SVladimir Oltean 			goto out;
2631ec5ae610SVladimir Oltean 	}
2632ec5ae610SVladimir Oltean 
2633ec5ae610SVladimir Oltean out:
2634ec5ae610SVladimir Oltean 	list_for_each_entry_safe(s, pos, &crosschip_switches, list) {
2635ec5ae610SVladimir Oltean 		list_del(&s->list);
2636ec5ae610SVladimir Oltean 		kfree(s);
2637ec5ae610SVladimir Oltean 	}
2638ec5ae610SVladimir Oltean 
2639ec5ae610SVladimir Oltean 	return rc;
2640ec5ae610SVladimir Oltean }
2641ec5ae610SVladimir Oltean 
2642ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify)
2643ec5ae610SVladimir Oltean {
264482760d7fSVladimir Oltean 	u16 subvlan_map[SJA1105_MAX_NUM_PORTS][DSA_8021Q_N_SUBVLAN];
26453f01c91aSVladimir Oltean 	struct sja1105_retagging_entry *new_retagging;
2646ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *new_vlan;
2647ec5ae610SVladimir Oltean 	struct sja1105_table *table;
26483f01c91aSVladimir Oltean 	int i, num_retagging = 0;
2649ec5ae610SVladimir Oltean 	int rc;
2650ec5ae610SVladimir Oltean 
2651ec5ae610SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2652ec5ae610SVladimir Oltean 	new_vlan = kcalloc(VLAN_N_VID,
2653ec5ae610SVladimir Oltean 			   table->ops->unpacked_entry_size, GFP_KERNEL);
2654ec5ae610SVladimir Oltean 	if (!new_vlan)
2655ec5ae610SVladimir Oltean 		return -ENOMEM;
2656ec5ae610SVladimir Oltean 
26573f01c91aSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
26583f01c91aSVladimir Oltean 	new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT,
26593f01c91aSVladimir Oltean 				table->ops->unpacked_entry_size, GFP_KERNEL);
26603f01c91aSVladimir Oltean 	if (!new_retagging) {
26613f01c91aSVladimir Oltean 		kfree(new_vlan);
26623f01c91aSVladimir Oltean 		return -ENOMEM;
26633f01c91aSVladimir Oltean 	}
26643f01c91aSVladimir Oltean 
2665ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++)
2666ec5ae610SVladimir Oltean 		new_vlan[i].vlanid = VLAN_N_VID;
2667ec5ae610SVladimir Oltean 
26683f01c91aSVladimir Oltean 	for (i = 0; i < SJA1105_MAX_RETAGGING_COUNT; i++)
26693f01c91aSVladimir Oltean 		new_retagging[i].vlan_ing = VLAN_N_VID;
26703f01c91aSVladimir Oltean 
26713f01c91aSVladimir Oltean 	for (i = 0; i < priv->ds->num_ports; i++)
26723f01c91aSVladimir Oltean 		sja1105_init_subvlan_map(subvlan_map[i]);
26733f01c91aSVladimir Oltean 
2674ec5ae610SVladimir Oltean 	/* Bridge VLANs */
2675ec5ae610SVladimir Oltean 	rc = sja1105_build_bridge_vlans(priv, new_vlan);
2676ec5ae610SVladimir Oltean 	if (rc)
2677ec5ae610SVladimir Oltean 		goto out;
2678ec5ae610SVladimir Oltean 
2679ec5ae610SVladimir Oltean 	/* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c:
2680ec5ae610SVladimir Oltean 	 * - RX VLANs
2681ec5ae610SVladimir Oltean 	 * - TX VLANs
2682ec5ae610SVladimir Oltean 	 * - Crosschip links
2683ec5ae610SVladimir Oltean 	 */
2684ec5ae610SVladimir Oltean 	rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan);
2685ec5ae610SVladimir Oltean 	if (rc)
2686ec5ae610SVladimir Oltean 		goto out;
2687ec5ae610SVladimir Oltean 
26883f01c91aSVladimir Oltean 	/* Private VLANs necessary for dsa_8021q operation, which we need to
26893f01c91aSVladimir Oltean 	 * determine on our own:
26903f01c91aSVladimir Oltean 	 * - Sub-VLANs
26913f01c91aSVladimir Oltean 	 * - Sub-VLANs of crosschip switches
26923f01c91aSVladimir Oltean 	 */
26933f01c91aSVladimir Oltean 	rc = sja1105_build_subvlans(priv, subvlan_map, new_vlan, new_retagging,
26943f01c91aSVladimir Oltean 				    &num_retagging);
26953f01c91aSVladimir Oltean 	if (rc)
26963f01c91aSVladimir Oltean 		goto out;
26973f01c91aSVladimir Oltean 
26983f01c91aSVladimir Oltean 	rc = sja1105_build_crosschip_subvlans(priv, new_vlan, new_retagging,
26993f01c91aSVladimir Oltean 					      &num_retagging);
27003f01c91aSVladimir Oltean 	if (rc)
27013f01c91aSVladimir Oltean 		goto out;
27023f01c91aSVladimir Oltean 
27033f01c91aSVladimir Oltean 	rc = sja1105_commit_vlans(priv, new_vlan, new_retagging, num_retagging);
2704ec5ae610SVladimir Oltean 	if (rc)
2705ec5ae610SVladimir Oltean 		goto out;
2706ec5ae610SVladimir Oltean 
2707ec5ae610SVladimir Oltean 	rc = sja1105_commit_pvid(priv);
2708ec5ae610SVladimir Oltean 	if (rc)
2709ec5ae610SVladimir Oltean 		goto out;
2710ec5ae610SVladimir Oltean 
27113f01c91aSVladimir Oltean 	for (i = 0; i < priv->ds->num_ports; i++)
27123f01c91aSVladimir Oltean 		sja1105_commit_subvlan_map(priv, i, subvlan_map[i]);
27133f01c91aSVladimir Oltean 
2714ec5ae610SVladimir Oltean 	if (notify) {
2715ec5ae610SVladimir Oltean 		rc = sja1105_notify_crosschip_switches(priv);
2716ec5ae610SVladimir Oltean 		if (rc)
2717ec5ae610SVladimir Oltean 			goto out;
2718ec5ae610SVladimir Oltean 	}
2719ec5ae610SVladimir Oltean 
2720ec5ae610SVladimir Oltean out:
2721ec5ae610SVladimir Oltean 	kfree(new_vlan);
27223f01c91aSVladimir Oltean 	kfree(new_retagging);
2723ec5ae610SVladimir Oltean 
2724ec5ae610SVladimir Oltean 	return rc;
2725ec5ae610SVladimir Oltean }
2726ec5ae610SVladimir Oltean 
2727070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
2728070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
2729070ca3bbSVladimir Oltean  * So a switch reset is required.
2730070ca3bbSVladimir Oltean  */
273189153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
273289153ed6SVladimir Oltean 			   struct netlink_ext_ack *extack)
27336666cebcSVladimir Oltean {
27346d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2735070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
27366666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
27377f14937fSVladimir Oltean 	enum sja1105_vlan_state state;
2738070ca3bbSVladimir Oltean 	struct sja1105_table *table;
2739dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
27402cafa72eSVladimir Oltean 	bool want_tagging;
2741070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
27426666cebcSVladimir Oltean 	int rc;
27436666cebcSVladimir Oltean 
2744dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
2745dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
274689153ed6SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
274789153ed6SVladimir Oltean 					   "Cannot change VLAN filtering with active VL rules");
2748dfacc5a2SVladimir Oltean 			return -EBUSY;
2749dfacc5a2SVladimir Oltean 		}
2750dfacc5a2SVladimir Oltean 	}
2751dfacc5a2SVladimir Oltean 
2752070ca3bbSVladimir Oltean 	if (enabled) {
27536666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
275454fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
275554fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
2756070ca3bbSVladimir Oltean 	} else {
27576666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
2758070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
2759070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
2760070ca3bbSVladimir Oltean 	}
2761070ca3bbSVladimir Oltean 
276238b5beeaSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
276338b5beeaSVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
276438b5beeaSVladimir Oltean 
276538b5beeaSVladimir Oltean 		if (enabled)
276638b5beeaSVladimir Oltean 			sp->xmit_tpid = priv->info->qinq_tpid;
276738b5beeaSVladimir Oltean 		else
276838b5beeaSVladimir Oltean 			sp->xmit_tpid = ETH_P_SJA1105;
276938b5beeaSVladimir Oltean 	}
277038b5beeaSVladimir Oltean 
27717f14937fSVladimir Oltean 	if (!enabled)
27727f14937fSVladimir Oltean 		state = SJA1105_VLAN_UNAWARE;
27732cafa72eSVladimir Oltean 	else if (priv->best_effort_vlan_filtering)
27742cafa72eSVladimir Oltean 		state = SJA1105_VLAN_BEST_EFFORT;
27757f14937fSVladimir Oltean 	else
27767f14937fSVladimir Oltean 		state = SJA1105_VLAN_FILTERING_FULL;
27777f14937fSVladimir Oltean 
2778cfa36b1fSVladimir Oltean 	if (priv->vlan_state == state)
2779cfa36b1fSVladimir Oltean 		return 0;
2780cfa36b1fSVladimir Oltean 
27817f14937fSVladimir Oltean 	priv->vlan_state = state;
27822cafa72eSVladimir Oltean 	want_tagging = (state == SJA1105_VLAN_UNAWARE ||
27832cafa72eSVladimir Oltean 			state == SJA1105_VLAN_BEST_EFFORT);
27847f14937fSVladimir Oltean 
2785070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2786070ca3bbSVladimir Oltean 	general_params = table->entries;
2787f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
278854fa49eeSVladimir Oltean 	general_params->tpid = tpid;
278954fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2790070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
279142824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
279242824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
279342824463SVladimir Oltean 	 */
279442824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
279542824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
2796070ca3bbSVladimir Oltean 
27972cafa72eSVladimir Oltean 	want_tagging = priv->best_effort_vlan_filtering || !enabled;
27982cafa72eSVladimir Oltean 
27996d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
28002cafa72eSVladimir Oltean 	 * No VLAN filtering (or best effort) => shared VLAN learning.
28016d7c7d94SVladimir Oltean 	 *
28026d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
28036d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
28046d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
28056d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
28066d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
28076d7c7d94SVladimir Oltean 	 * forwarding decision.
28086d7c7d94SVladimir Oltean 	 *
28096d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
28106d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
28116d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
28126d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
28136d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
28146d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
28156d7c7d94SVladimir Oltean 	 * (all frames get flooded).
28166d7c7d94SVladimir Oltean 	 */
28176d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
28186d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
28192cafa72eSVladimir Oltean 	l2_lookup_params->shared_learn = want_tagging;
28206d7c7d94SVladimir Oltean 
2821aaa270c6SVladimir Oltean 	sja1105_frame_memory_partitioning(priv);
2822aaa270c6SVladimir Oltean 
2823aef31718SVladimir Oltean 	rc = sja1105_build_vlan_table(priv, false);
2824aef31718SVladimir Oltean 	if (rc)
2825aef31718SVladimir Oltean 		return rc;
2826aef31718SVladimir Oltean 
28272eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
28286666cebcSVladimir Oltean 	if (rc)
282989153ed6SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype");
28306666cebcSVladimir Oltean 
2831227d07a0SVladimir Oltean 	/* Switch port identification based on 802.1Q is only passable
2832227d07a0SVladimir Oltean 	 * if we are not under a vlan_filtering bridge. So make sure
28332cafa72eSVladimir Oltean 	 * the two configurations are mutually exclusive (of course, the
28342cafa72eSVladimir Oltean 	 * user may know better, i.e. best_effort_vlan_filtering).
2835227d07a0SVladimir Oltean 	 */
28362cafa72eSVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, want_tagging);
28376666cebcSVladimir Oltean }
28386666cebcSVladimir Oltean 
28395899ee36SVladimir Oltean /* Returns number of VLANs added (0 or 1) on success,
28405899ee36SVladimir Oltean  * or a negative error code.
28415899ee36SVladimir Oltean  */
28425899ee36SVladimir Oltean static int sja1105_vlan_add_one(struct dsa_switch *ds, int port, u16 vid,
28435899ee36SVladimir Oltean 				u16 flags, struct list_head *vlan_list)
28445899ee36SVladimir Oltean {
28455899ee36SVladimir Oltean 	bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED;
28465899ee36SVladimir Oltean 	bool pvid = flags & BRIDGE_VLAN_INFO_PVID;
28475899ee36SVladimir Oltean 	struct sja1105_bridge_vlan *v;
28485899ee36SVladimir Oltean 
2849b38e659dSVladimir Oltean 	list_for_each_entry(v, vlan_list, list) {
2850b38e659dSVladimir Oltean 		if (v->port == port && v->vid == vid) {
28515899ee36SVladimir Oltean 			/* Already added */
2852b38e659dSVladimir Oltean 			if (v->untagged == untagged && v->pvid == pvid)
2853b38e659dSVladimir Oltean 				/* Nothing changed */
28545899ee36SVladimir Oltean 				return 0;
28555899ee36SVladimir Oltean 
2856b38e659dSVladimir Oltean 			/* It's the same VLAN, but some of the flags changed
2857b38e659dSVladimir Oltean 			 * and the user did not bother to delete it first.
2858b38e659dSVladimir Oltean 			 * Update it and trigger sja1105_build_vlan_table.
2859b38e659dSVladimir Oltean 			 */
2860b38e659dSVladimir Oltean 			v->untagged = untagged;
2861b38e659dSVladimir Oltean 			v->pvid = pvid;
2862b38e659dSVladimir Oltean 			return 1;
2863b38e659dSVladimir Oltean 		}
2864b38e659dSVladimir Oltean 	}
2865b38e659dSVladimir Oltean 
28665899ee36SVladimir Oltean 	v = kzalloc(sizeof(*v), GFP_KERNEL);
28675899ee36SVladimir Oltean 	if (!v) {
28685899ee36SVladimir Oltean 		dev_err(ds->dev, "Out of memory while storing VLAN\n");
28695899ee36SVladimir Oltean 		return -ENOMEM;
28705899ee36SVladimir Oltean 	}
28715899ee36SVladimir Oltean 
28725899ee36SVladimir Oltean 	v->port = port;
28735899ee36SVladimir Oltean 	v->vid = vid;
28745899ee36SVladimir Oltean 	v->untagged = untagged;
28755899ee36SVladimir Oltean 	v->pvid = pvid;
28765899ee36SVladimir Oltean 	list_add(&v->list, vlan_list);
28775899ee36SVladimir Oltean 
28785899ee36SVladimir Oltean 	return 1;
28795899ee36SVladimir Oltean }
28805899ee36SVladimir Oltean 
28815899ee36SVladimir Oltean /* Returns number of VLANs deleted (0 or 1) */
28825899ee36SVladimir Oltean static int sja1105_vlan_del_one(struct dsa_switch *ds, int port, u16 vid,
28835899ee36SVladimir Oltean 				struct list_head *vlan_list)
28845899ee36SVladimir Oltean {
28855899ee36SVladimir Oltean 	struct sja1105_bridge_vlan *v, *n;
28865899ee36SVladimir Oltean 
28875899ee36SVladimir Oltean 	list_for_each_entry_safe(v, n, vlan_list, list) {
28885899ee36SVladimir Oltean 		if (v->port == port && v->vid == vid) {
28895899ee36SVladimir Oltean 			list_del(&v->list);
28905899ee36SVladimir Oltean 			kfree(v);
28915899ee36SVladimir Oltean 			return 1;
28925899ee36SVladimir Oltean 		}
28935899ee36SVladimir Oltean 	}
28945899ee36SVladimir Oltean 
28955899ee36SVladimir Oltean 	return 0;
28965899ee36SVladimir Oltean }
28975899ee36SVladimir Oltean 
28981958d581SVladimir Oltean static int sja1105_vlan_add(struct dsa_switch *ds, int port,
289931046a5fSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan,
290031046a5fSVladimir Oltean 			    struct netlink_ext_ack *extack)
29016666cebcSVladimir Oltean {
29026666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2903ec5ae610SVladimir Oltean 	bool vlan_table_changed = false;
29046666cebcSVladimir Oltean 	int rc;
29056666cebcSVladimir Oltean 
29061958d581SVladimir Oltean 	/* If the user wants best-effort VLAN filtering (aka vlan_filtering
29071958d581SVladimir Oltean 	 * bridge plus tagging), be sure to at least deny alterations to the
29081958d581SVladimir Oltean 	 * configuration done by dsa_8021q.
29091958d581SVladimir Oltean 	 */
29101958d581SVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL &&
29111958d581SVladimir Oltean 	    vid_is_dsa_8021q(vlan->vid)) {
291231046a5fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
291331046a5fSVladimir Oltean 				   "Range 1024-3071 reserved for dsa_8021q operation");
29141958d581SVladimir Oltean 		return -EBUSY;
29151958d581SVladimir Oltean 	}
29161958d581SVladimir Oltean 
2917b7a9e0daSVladimir Oltean 	rc = sja1105_vlan_add_one(ds, port, vlan->vid, vlan->flags,
29185899ee36SVladimir Oltean 				  &priv->bridge_vlans);
29195899ee36SVladimir Oltean 	if (rc < 0)
29201958d581SVladimir Oltean 		return rc;
29215899ee36SVladimir Oltean 	if (rc > 0)
2922ec5ae610SVladimir Oltean 		vlan_table_changed = true;
2923ec5ae610SVladimir Oltean 
2924ec5ae610SVladimir Oltean 	if (!vlan_table_changed)
29251958d581SVladimir Oltean 		return 0;
2926ec5ae610SVladimir Oltean 
29271958d581SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
29286666cebcSVladimir Oltean }
29296666cebcSVladimir Oltean 
29306666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
29316666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
29326666cebcSVladimir Oltean {
29336666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2934ec5ae610SVladimir Oltean 	bool vlan_table_changed = false;
29355899ee36SVladimir Oltean 	int rc;
29366666cebcSVladimir Oltean 
2937b7a9e0daSVladimir Oltean 	rc = sja1105_vlan_del_one(ds, port, vlan->vid, &priv->bridge_vlans);
29385899ee36SVladimir Oltean 	if (rc > 0)
2939ec5ae610SVladimir Oltean 		vlan_table_changed = true;
2940ec5ae610SVladimir Oltean 
2941ec5ae610SVladimir Oltean 	if (!vlan_table_changed)
29426666cebcSVladimir Oltean 		return 0;
2943ec5ae610SVladimir Oltean 
2944ec5ae610SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
29456666cebcSVladimir Oltean }
29466666cebcSVladimir Oltean 
29475899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
29485899ee36SVladimir Oltean 				      u16 flags)
29495899ee36SVladimir Oltean {
29505899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29515899ee36SVladimir Oltean 	int rc;
29525899ee36SVladimir Oltean 
29535899ee36SVladimir Oltean 	rc = sja1105_vlan_add_one(ds, port, vid, flags, &priv->dsa_8021q_vlans);
29545899ee36SVladimir Oltean 	if (rc <= 0)
29555899ee36SVladimir Oltean 		return rc;
29565899ee36SVladimir Oltean 
29575899ee36SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
29585899ee36SVladimir Oltean }
29595899ee36SVladimir Oltean 
29605899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
29615899ee36SVladimir Oltean {
29625899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29635899ee36SVladimir Oltean 	int rc;
29645899ee36SVladimir Oltean 
29655899ee36SVladimir Oltean 	rc = sja1105_vlan_del_one(ds, port, vid, &priv->dsa_8021q_vlans);
29665899ee36SVladimir Oltean 	if (!rc)
29675899ee36SVladimir Oltean 		return 0;
29685899ee36SVladimir Oltean 
29695899ee36SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
29705899ee36SVladimir Oltean }
29715899ee36SVladimir Oltean 
29725899ee36SVladimir Oltean static const struct dsa_8021q_ops sja1105_dsa_8021q_ops = {
29735899ee36SVladimir Oltean 	.vlan_add	= sja1105_dsa_8021q_vlan_add,
29745899ee36SVladimir Oltean 	.vlan_del	= sja1105_dsa_8021q_vlan_del,
29755899ee36SVladimir Oltean };
29765899ee36SVladimir Oltean 
29778aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
29788aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
29798aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
29808aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
29818aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
29828aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
29838aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
29848aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
29858aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
29868aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
29878aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
29888aa9ebccSVladimir Oltean  */
29898aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
29908aa9ebccSVladimir Oltean {
29918aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29928aa9ebccSVladimir Oltean 	int rc;
29938aa9ebccSVladimir Oltean 
29945d645df9SVladimir Oltean 	rc = sja1105_parse_dt(priv);
29958aa9ebccSVladimir Oltean 	if (rc < 0) {
29968aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
29978aa9ebccSVladimir Oltean 		return rc;
29988aa9ebccSVladimir Oltean 	}
2999f5b8631cSVladimir Oltean 
3000f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
3001f5b8631cSVladimir Oltean 	 * and we can't apply them.
3002f5b8631cSVladimir Oltean 	 */
300329afb83aSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv);
3004f5b8631cSVladimir Oltean 	if (rc < 0) {
3005f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
3006f5b8631cSVladimir Oltean 		return rc;
3007f5b8631cSVladimir Oltean 	}
3008f5b8631cSVladimir Oltean 
300961c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
3010bb77f36aSVladimir Oltean 	if (rc < 0) {
3011bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
3012bb77f36aSVladimir Oltean 		return rc;
3013bb77f36aSVladimir Oltean 	}
30145a8f0974SVladimir Oltean 
30155a8f0974SVladimir Oltean 	rc = sja1105_mdiobus_register(ds);
30165a8f0974SVladimir Oltean 	if (rc < 0) {
30175a8f0974SVladimir Oltean 		dev_err(ds->dev, "Failed to register MDIO bus: %pe\n",
30185a8f0974SVladimir Oltean 			ERR_PTR(rc));
30195a8f0974SVladimir Oltean 		goto out_ptp_clock_unregister;
30205a8f0974SVladimir Oltean 	}
30215a8f0974SVladimir Oltean 
30228aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
30235d645df9SVladimir Oltean 	rc = sja1105_static_config_load(priv);
30248aa9ebccSVladimir Oltean 	if (rc < 0) {
30258aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
30265a8f0974SVladimir Oltean 		goto out_mdiobus_unregister;
30278aa9ebccSVladimir Oltean 	}
30288aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
3029c5037678SVladimir Oltean 	rc = priv->info->clocking_setup(priv);
30308aa9ebccSVladimir Oltean 	if (rc < 0) {
30318aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
3032cec279a8SVladimir Oltean 		goto out_static_config_free;
30338aa9ebccSVladimir Oltean 	}
30346666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
30356666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
30366666cebcSVladimir Oltean 	 * EtherType is.
30376666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
30386666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
30396666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
30406666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
30416666cebcSVladimir Oltean 	 */
30426666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
30438aa9ebccSVladimir Oltean 
30445f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
30455f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
30465f06c63bSVladimir Oltean 
3047c279c726SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
3048c279c726SVladimir Oltean 
30498841f6e6SVladimir Oltean 	priv->best_effort_vlan_filtering = true;
30508841f6e6SVladimir Oltean 
30510a7bdbc2SVladimir Oltean 	rc = sja1105_devlink_setup(ds);
30522cafa72eSVladimir Oltean 	if (rc < 0)
3053cec279a8SVladimir Oltean 		goto out_static_config_free;
30542cafa72eSVladimir Oltean 
3055227d07a0SVladimir Oltean 	/* The DSA/switchdev model brings up switch ports in standalone mode by
3056227d07a0SVladimir Oltean 	 * default, and that means vlan_filtering is 0 since they're not under
3057227d07a0SVladimir Oltean 	 * a bridge, so it's safe to set up switch tagging at this time.
3058227d07a0SVladimir Oltean 	 */
3059bbed0bbdSVladimir Oltean 	rtnl_lock();
3060bbed0bbdSVladimir Oltean 	rc = sja1105_setup_8021q_tagging(ds, true);
3061bbed0bbdSVladimir Oltean 	rtnl_unlock();
3062cec279a8SVladimir Oltean 	if (rc)
3063cec279a8SVladimir Oltean 		goto out_devlink_teardown;
3064cec279a8SVladimir Oltean 
3065cec279a8SVladimir Oltean 	return 0;
3066cec279a8SVladimir Oltean 
3067cec279a8SVladimir Oltean out_devlink_teardown:
3068cec279a8SVladimir Oltean 	sja1105_devlink_teardown(ds);
30695a8f0974SVladimir Oltean out_mdiobus_unregister:
30705a8f0974SVladimir Oltean 	sja1105_mdiobus_unregister(ds);
3071cec279a8SVladimir Oltean out_ptp_clock_unregister:
3072cec279a8SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
3073cec279a8SVladimir Oltean out_static_config_free:
3074cec279a8SVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
3075bbed0bbdSVladimir Oltean 
3076bbed0bbdSVladimir Oltean 	return rc;
3077227d07a0SVladimir Oltean }
3078227d07a0SVladimir Oltean 
3079f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
3080f3097be2SVladimir Oltean {
3081f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3082ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v, *n;
3083a68578c2SVladimir Oltean 	int port;
3084a68578c2SVladimir Oltean 
3085542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
3086a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3087a68578c2SVladimir Oltean 
3088a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3089a68578c2SVladimir Oltean 			continue;
3090a68578c2SVladimir Oltean 
309152c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
3092a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
3093a68578c2SVladimir Oltean 	}
3094f3097be2SVladimir Oltean 
30950a7bdbc2SVladimir Oltean 	sja1105_devlink_teardown(ds);
3096a6af7763SVladimir Oltean 	sja1105_flower_teardown(ds);
3097317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
309861c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
30996cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
3100ec5ae610SVladimir Oltean 
3101ec5ae610SVladimir Oltean 	list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) {
3102ec5ae610SVladimir Oltean 		list_del(&v->list);
3103ec5ae610SVladimir Oltean 		kfree(v);
3104ec5ae610SVladimir Oltean 	}
3105ec5ae610SVladimir Oltean 
3106ec5ae610SVladimir Oltean 	list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) {
3107ec5ae610SVladimir Oltean 		list_del(&v->list);
3108ec5ae610SVladimir Oltean 		kfree(v);
3109ec5ae610SVladimir Oltean 	}
3110f3097be2SVladimir Oltean }
3111f3097be2SVladimir Oltean 
3112a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
3113a68578c2SVladimir Oltean {
3114a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3115a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
3116a68578c2SVladimir Oltean 
3117a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
3118a68578c2SVladimir Oltean 		return;
3119a68578c2SVladimir Oltean 
3120a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
3121a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
3122a68578c2SVladimir Oltean }
3123a68578c2SVladimir Oltean 
3124227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
312547ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
3126227d07a0SVladimir Oltean {
3127227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
3128227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3129227d07a0SVladimir Oltean 	struct ethhdr *hdr;
3130227d07a0SVladimir Oltean 	int timeout = 10;
3131227d07a0SVladimir Oltean 	int rc;
3132227d07a0SVladimir Oltean 
3133227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
3134227d07a0SVladimir Oltean 
3135227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
3136227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
3137227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
313847ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
313947ed985eSVladimir Oltean 	mgmt_route.takets = takets;
3140227d07a0SVladimir Oltean 
3141227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3142227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
3143227d07a0SVladimir Oltean 	if (rc < 0) {
3144227d07a0SVladimir Oltean 		kfree_skb(skb);
3145227d07a0SVladimir Oltean 		return rc;
3146227d07a0SVladimir Oltean 	}
3147227d07a0SVladimir Oltean 
3148227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
314968bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
3150227d07a0SVladimir Oltean 
3151227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
3152227d07a0SVladimir Oltean 	do {
3153227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
3154227d07a0SVladimir Oltean 						 slot, &mgmt_route);
3155227d07a0SVladimir Oltean 		if (rc < 0) {
3156227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
3157227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
3158227d07a0SVladimir Oltean 			continue;
3159227d07a0SVladimir Oltean 		}
3160227d07a0SVladimir Oltean 
3161227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
3162227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
3163227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
3164227d07a0SVladimir Oltean 		 */
3165227d07a0SVladimir Oltean 		cpu_relax();
3166227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
3167227d07a0SVladimir Oltean 
3168227d07a0SVladimir Oltean 	if (!timeout) {
3169227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
3170227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
31712a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
31722a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
3173227d07a0SVladimir Oltean 		 */
3174227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3175227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
3176227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
3177227d07a0SVladimir Oltean 	}
3178227d07a0SVladimir Oltean 
3179227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
3180227d07a0SVladimir Oltean }
3181227d07a0SVladimir Oltean 
3182a68578c2SVladimir Oltean #define work_to_port(work) \
3183a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
3184a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
3185a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
3186a68578c2SVladimir Oltean 
3187227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
3188227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
3189227d07a0SVladimir Oltean  * lock on the bus)
3190227d07a0SVladimir Oltean  */
3191a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
3192227d07a0SVladimir Oltean {
3193a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
3194a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
3195a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
3196a68578c2SVladimir Oltean 	int port = sp - priv->ports;
3197a68578c2SVladimir Oltean 	struct sk_buff *skb;
3198a68578c2SVladimir Oltean 
3199a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
3200c4b364ceSYangbo Lu 		struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
3201227d07a0SVladimir Oltean 
3202227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
3203227d07a0SVladimir Oltean 
3204a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
3205a68578c2SVladimir Oltean 
320647ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
3207a68578c2SVladimir Oltean 		if (clone)
3208a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
3209227d07a0SVladimir Oltean 
3210227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
3211a68578c2SVladimir Oltean 	}
32128aa9ebccSVladimir Oltean }
32138aa9ebccSVladimir Oltean 
32148456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
32158456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
32168456721dSVladimir Oltean  */
32178456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
32188456721dSVladimir Oltean 				   unsigned int ageing_time)
32198456721dSVladimir Oltean {
32208456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
32218456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
32228456721dSVladimir Oltean 	struct sja1105_table *table;
32238456721dSVladimir Oltean 	unsigned int maxage;
32248456721dSVladimir Oltean 
32258456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
32268456721dSVladimir Oltean 	l2_lookup_params = table->entries;
32278456721dSVladimir Oltean 
32288456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
32298456721dSVladimir Oltean 
32308456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
32318456721dSVladimir Oltean 		return 0;
32328456721dSVladimir Oltean 
32338456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
32348456721dSVladimir Oltean 
32352eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
32368456721dSVladimir Oltean }
32378456721dSVladimir Oltean 
3238c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
3239c279c726SVladimir Oltean {
3240c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
3241c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3242c279c726SVladimir Oltean 
3243c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
3244c279c726SVladimir Oltean 
3245c279c726SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
3246c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
3247c279c726SVladimir Oltean 
3248c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3249c279c726SVladimir Oltean 
3250a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
3251c279c726SVladimir Oltean 		return 0;
3252c279c726SVladimir Oltean 
3253a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
3254c279c726SVladimir Oltean 
3255c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3256c279c726SVladimir Oltean }
3257c279c726SVladimir Oltean 
3258c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
3259c279c726SVladimir Oltean {
3260c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
3261c279c726SVladimir Oltean }
3262c279c726SVladimir Oltean 
3263317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
3264317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
3265317ab5b8SVladimir Oltean 				 void *type_data)
3266317ab5b8SVladimir Oltean {
3267317ab5b8SVladimir Oltean 	switch (type) {
3268317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
3269317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
32704d752508SVladimir Oltean 	case TC_SETUP_QDISC_CBS:
32714d752508SVladimir Oltean 		return sja1105_setup_tc_cbs(ds, port, type_data);
3272317ab5b8SVladimir Oltean 	default:
3273317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
3274317ab5b8SVladimir Oltean 	}
3275317ab5b8SVladimir Oltean }
3276317ab5b8SVladimir Oltean 
3277511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
3278511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
3279511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
3280511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
3281511e6ca0SVladimir Oltean  * mirroring rule that references it.
3282511e6ca0SVladimir Oltean  */
3283511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
3284511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
3285511e6ca0SVladimir Oltean {
3286511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
3287511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
3288542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
3289511e6ca0SVladimir Oltean 	struct sja1105_table *table;
3290511e6ca0SVladimir Oltean 	bool already_enabled;
3291511e6ca0SVladimir Oltean 	u64 new_mirr_port;
3292511e6ca0SVladimir Oltean 	int rc;
3293511e6ca0SVladimir Oltean 
3294511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
3295511e6ca0SVladimir Oltean 	general_params = table->entries;
3296511e6ca0SVladimir Oltean 
3297511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
3298511e6ca0SVladimir Oltean 
3299542043e9SVladimir Oltean 	already_enabled = (general_params->mirr_port != ds->num_ports);
3300511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
3301511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
3302511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
3303511e6ca0SVladimir Oltean 			general_params->mirr_port);
3304511e6ca0SVladimir Oltean 		return -EBUSY;
3305511e6ca0SVladimir Oltean 	}
3306511e6ca0SVladimir Oltean 
3307511e6ca0SVladimir Oltean 	new_mirr_port = to;
3308511e6ca0SVladimir Oltean 	if (!enabled) {
3309511e6ca0SVladimir Oltean 		bool keep = false;
3310511e6ca0SVladimir Oltean 		int port;
3311511e6ca0SVladimir Oltean 
3312511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
3313542043e9SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
3314511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
3315511e6ca0SVladimir Oltean 				keep = true;
3316511e6ca0SVladimir Oltean 				break;
3317511e6ca0SVladimir Oltean 			}
3318511e6ca0SVladimir Oltean 		}
3319511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
3320511e6ca0SVladimir Oltean 		if (!keep)
3321542043e9SVladimir Oltean 			new_mirr_port = ds->num_ports;
3322511e6ca0SVladimir Oltean 	}
3323511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
3324511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
3325511e6ca0SVladimir Oltean 
3326511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
3327511e6ca0SVladimir Oltean 						  0, general_params, true);
3328511e6ca0SVladimir Oltean 		if (rc < 0)
3329511e6ca0SVladimir Oltean 			return rc;
3330511e6ca0SVladimir Oltean 	}
3331511e6ca0SVladimir Oltean 
3332511e6ca0SVladimir Oltean 	if (ingress)
3333511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
3334511e6ca0SVladimir Oltean 	else
3335511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
3336511e6ca0SVladimir Oltean 
3337511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
3338511e6ca0SVladimir Oltean 					    &mac[from], true);
3339511e6ca0SVladimir Oltean }
3340511e6ca0SVladimir Oltean 
3341511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
3342511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
3343511e6ca0SVladimir Oltean 			      bool ingress)
3344511e6ca0SVladimir Oltean {
3345511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3346511e6ca0SVladimir Oltean 				    ingress, true);
3347511e6ca0SVladimir Oltean }
3348511e6ca0SVladimir Oltean 
3349511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
3350511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
3351511e6ca0SVladimir Oltean {
3352511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3353511e6ca0SVladimir Oltean 			     mirror->ingress, false);
3354511e6ca0SVladimir Oltean }
3355511e6ca0SVladimir Oltean 
3356a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
3357a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
3358a7cc081cSVladimir Oltean {
3359a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
3360a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3361a7cc081cSVladimir Oltean 
3362a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3363a7cc081cSVladimir Oltean 
3364a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
3365a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
3366a7cc081cSVladimir Oltean 	 * bytes.
3367a7cc081cSVladimir Oltean 	 */
3368a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
3369a7cc081cSVladimir Oltean 				      1000000);
33705f035af7SPo Liu 	policing[port].smax = policer->burst;
3371a7cc081cSVladimir Oltean 
3372a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3373a7cc081cSVladimir Oltean }
3374a7cc081cSVladimir Oltean 
3375a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
3376a7cc081cSVladimir Oltean {
3377a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
3378a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3379a7cc081cSVladimir Oltean 
3380a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3381a7cc081cSVladimir Oltean 
3382a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
3383a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
3384a7cc081cSVladimir Oltean 
3385a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3386a7cc081cSVladimir Oltean }
3387a7cc081cSVladimir Oltean 
33884d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port,
33894d942354SVladimir Oltean 				     bool enabled)
33904d942354SVladimir Oltean {
33914d942354SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
33924d942354SVladimir Oltean 	int rc;
33934d942354SVladimir Oltean 
33944d942354SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
33954d942354SVladimir Oltean 
33964c44fc5eSVladimir Oltean 	mac[port].dyn_learn = enabled;
33974d942354SVladimir Oltean 
33984d942354SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
33994d942354SVladimir Oltean 					  &mac[port], true);
34004d942354SVladimir Oltean 	if (rc)
34014d942354SVladimir Oltean 		return rc;
34024d942354SVladimir Oltean 
34034d942354SVladimir Oltean 	if (enabled)
34044d942354SVladimir Oltean 		priv->learn_ena |= BIT(port);
34054d942354SVladimir Oltean 	else
34064d942354SVladimir Oltean 		priv->learn_ena &= ~BIT(port);
34074d942354SVladimir Oltean 
34084d942354SVladimir Oltean 	return 0;
34094d942354SVladimir Oltean }
34104d942354SVladimir Oltean 
34114d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to,
34124d942354SVladimir Oltean 					  struct switchdev_brport_flags flags)
34134d942354SVladimir Oltean {
34144d942354SVladimir Oltean 	if (flags.mask & BR_FLOOD) {
34154d942354SVladimir Oltean 		if (flags.val & BR_FLOOD)
34167f7ccdeaSVladimir Oltean 			priv->ucast_egress_floods |= BIT(to);
34174d942354SVladimir Oltean 		else
34186a5166e0SVladimir Oltean 			priv->ucast_egress_floods &= ~BIT(to);
34194d942354SVladimir Oltean 	}
34207f7ccdeaSVladimir Oltean 
34214d942354SVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD) {
34224d942354SVladimir Oltean 		if (flags.val & BR_BCAST_FLOOD)
34237f7ccdeaSVladimir Oltean 			priv->bcast_egress_floods |= BIT(to);
34244d942354SVladimir Oltean 		else
34256a5166e0SVladimir Oltean 			priv->bcast_egress_floods &= ~BIT(to);
34264d942354SVladimir Oltean 	}
34274d942354SVladimir Oltean 
34287f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
34294d942354SVladimir Oltean }
34304d942354SVladimir Oltean 
34314d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to,
34324d942354SVladimir Oltean 				    struct switchdev_brport_flags flags,
34334d942354SVladimir Oltean 				    struct netlink_ext_ack *extack)
34344d942354SVladimir Oltean {
34354d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
34364d942354SVladimir Oltean 	struct sja1105_table *table;
34374d942354SVladimir Oltean 	int match;
34384d942354SVladimir Oltean 
34394d942354SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
34404d942354SVladimir Oltean 	l2_lookup = table->entries;
34414d942354SVladimir Oltean 
34424d942354SVladimir Oltean 	for (match = 0; match < table->entry_count; match++)
34434d942354SVladimir Oltean 		if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST &&
34444d942354SVladimir Oltean 		    l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
34454d942354SVladimir Oltean 			break;
34464d942354SVladimir Oltean 
34474d942354SVladimir Oltean 	if (match == table->entry_count) {
34484d942354SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
34494d942354SVladimir Oltean 				   "Could not find FDB entry for unknown multicast");
34504d942354SVladimir Oltean 		return -ENOSPC;
34514d942354SVladimir Oltean 	}
34524d942354SVladimir Oltean 
34534d942354SVladimir Oltean 	if (flags.val & BR_MCAST_FLOOD)
34544d942354SVladimir Oltean 		l2_lookup[match].destports |= BIT(to);
34554d942354SVladimir Oltean 	else
34564d942354SVladimir Oltean 		l2_lookup[match].destports &= ~BIT(to);
34574d942354SVladimir Oltean 
34584d942354SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
34594d942354SVladimir Oltean 					    l2_lookup[match].index,
34604d942354SVladimir Oltean 					    &l2_lookup[match],
34614d942354SVladimir Oltean 					    true);
34624d942354SVladimir Oltean }
34634d942354SVladimir Oltean 
34644d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port,
34654d942354SVladimir Oltean 					 struct switchdev_brport_flags flags,
34664d942354SVladimir Oltean 					 struct netlink_ext_ack *extack)
34674d942354SVladimir Oltean {
34684d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
34694d942354SVladimir Oltean 
34704d942354SVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
34714d942354SVladimir Oltean 			   BR_BCAST_FLOOD))
34724d942354SVladimir Oltean 		return -EINVAL;
34734d942354SVladimir Oltean 
34744d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) &&
34754d942354SVladimir Oltean 	    !priv->info->can_limit_mcast_flood) {
34764d942354SVladimir Oltean 		bool multicast = !!(flags.val & BR_MCAST_FLOOD);
34774d942354SVladimir Oltean 		bool unicast = !!(flags.val & BR_FLOOD);
34784d942354SVladimir Oltean 
34794d942354SVladimir Oltean 		if (unicast != multicast) {
34804d942354SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
34814d942354SVladimir Oltean 					   "This chip cannot configure multicast flooding independently of unicast");
34824d942354SVladimir Oltean 			return -EINVAL;
34834d942354SVladimir Oltean 		}
34844d942354SVladimir Oltean 	}
34854d942354SVladimir Oltean 
34864d942354SVladimir Oltean 	return 0;
34874d942354SVladimir Oltean }
34884d942354SVladimir Oltean 
34894d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port,
34904d942354SVladimir Oltean 				     struct switchdev_brport_flags flags,
34914d942354SVladimir Oltean 				     struct netlink_ext_ack *extack)
34924d942354SVladimir Oltean {
34934d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
34944d942354SVladimir Oltean 	int rc;
34954d942354SVladimir Oltean 
34964d942354SVladimir Oltean 	if (flags.mask & BR_LEARNING) {
34974d942354SVladimir Oltean 		bool learn_ena = !!(flags.val & BR_LEARNING);
34984d942354SVladimir Oltean 
34994d942354SVladimir Oltean 		rc = sja1105_port_set_learning(priv, port, learn_ena);
35004d942354SVladimir Oltean 		if (rc)
35014d942354SVladimir Oltean 			return rc;
35024d942354SVladimir Oltean 	}
35034d942354SVladimir Oltean 
35044d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) {
35054d942354SVladimir Oltean 		rc = sja1105_port_ucast_bcast_flood(priv, port, flags);
35064d942354SVladimir Oltean 		if (rc)
35074d942354SVladimir Oltean 			return rc;
35084d942354SVladimir Oltean 	}
35094d942354SVladimir Oltean 
35104d942354SVladimir Oltean 	/* For chips that can't offload BR_MCAST_FLOOD independently, there
35114d942354SVladimir Oltean 	 * is nothing to do here, we ensured the configuration is in sync by
35124d942354SVladimir Oltean 	 * offloading BR_FLOOD.
35134d942354SVladimir Oltean 	 */
35144d942354SVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) {
35154d942354SVladimir Oltean 		rc = sja1105_port_mcast_flood(priv, port, flags,
35164d942354SVladimir Oltean 					      extack);
35174d942354SVladimir Oltean 		if (rc)
35184d942354SVladimir Oltean 			return rc;
35194d942354SVladimir Oltean 	}
35204d942354SVladimir Oltean 
35214d942354SVladimir Oltean 	return 0;
35224d942354SVladimir Oltean }
35234d942354SVladimir Oltean 
35248aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
35258aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
35268aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
3527f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
35288456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
3529c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
3530c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
3531ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
3532af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
35338400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
35348400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
353552c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
353652c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
353752c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
3538bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
3539a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
3540291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
3541291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
3542291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
35438aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
35448aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
35454d942354SVladimir Oltean 	.port_pre_bridge_flags	= sja1105_port_pre_bridge_flags,
35464d942354SVladimir Oltean 	.port_bridge_flags	= sja1105_port_bridge_flags,
3547640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
35486666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
35496666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
35506666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
3551291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
3552291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
3553a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
3554a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
3555f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
355647ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
3557317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
3558511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
3559511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
3560a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
3561a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
3562a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
3563a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
3564834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
3565ac02a451SVladimir Oltean 	.crosschip_bridge_join	= sja1105_crosschip_bridge_join,
3566ac02a451SVladimir Oltean 	.crosschip_bridge_leave	= sja1105_crosschip_bridge_leave,
35672cafa72eSVladimir Oltean 	.devlink_param_get	= sja1105_devlink_param_get,
35682cafa72eSVladimir Oltean 	.devlink_param_set	= sja1105_devlink_param_set,
3569ff4cf8eaSVladimir Oltean 	.devlink_info_get	= sja1105_devlink_info_get,
35708aa9ebccSVladimir Oltean };
35718aa9ebccSVladimir Oltean 
35720b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[];
35730b0e2997SVladimir Oltean 
35748aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
35758aa9ebccSVladimir Oltean {
35768aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
35778aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
35788aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
35790b0e2997SVladimir Oltean 	const struct of_device_id *match;
3580dff79620SVladimir Oltean 	u32 device_id;
35818aa9ebccSVladimir Oltean 	u64 part_no;
35828aa9ebccSVladimir Oltean 	int rc;
35838aa9ebccSVladimir Oltean 
358434d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
358534d76e9fSVladimir Oltean 			      NULL);
35868aa9ebccSVladimir Oltean 	if (rc < 0)
35878aa9ebccSVladimir Oltean 		return rc;
35888aa9ebccSVladimir Oltean 
35891bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
35901bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
35918aa9ebccSVladimir Oltean 	if (rc < 0)
35928aa9ebccSVladimir Oltean 		return rc;
35938aa9ebccSVladimir Oltean 
35948aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
35958aa9ebccSVladimir Oltean 
35965978fac0SNathan Chancellor 	for (match = sja1105_dt_ids; match->compatible[0]; match++) {
35970b0e2997SVladimir Oltean 		const struct sja1105_info *info = match->data;
35980b0e2997SVladimir Oltean 
35990b0e2997SVladimir Oltean 		/* Is what's been probed in our match table at all? */
36000b0e2997SVladimir Oltean 		if (info->device_id != device_id || info->part_no != part_no)
36010b0e2997SVladimir Oltean 			continue;
36020b0e2997SVladimir Oltean 
36030b0e2997SVladimir Oltean 		/* But is it what's in the device tree? */
36040b0e2997SVladimir Oltean 		if (priv->info->device_id != device_id ||
36050b0e2997SVladimir Oltean 		    priv->info->part_no != part_no) {
36060b0e2997SVladimir Oltean 			dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
36070b0e2997SVladimir Oltean 				 priv->info->name, info->name);
36080b0e2997SVladimir Oltean 			/* It isn't. No problem, pick that up. */
36090b0e2997SVladimir Oltean 			priv->info = info;
36108aa9ebccSVladimir Oltean 		}
36118aa9ebccSVladimir Oltean 
36128aa9ebccSVladimir Oltean 		return 0;
36138aa9ebccSVladimir Oltean 	}
36148aa9ebccSVladimir Oltean 
36150b0e2997SVladimir Oltean 	dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
36160b0e2997SVladimir Oltean 		device_id, part_no);
36170b0e2997SVladimir Oltean 
36180b0e2997SVladimir Oltean 	return -ENODEV;
36190b0e2997SVladimir Oltean }
36200b0e2997SVladimir Oltean 
36218aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
36228aa9ebccSVladimir Oltean {
3623844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
36248aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
36258aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
3626718bad0eSVladimir Oltean 	size_t max_xfer, max_msg;
36278aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
3628a68578c2SVladimir Oltean 	int rc, port;
36298aa9ebccSVladimir Oltean 
36308aa9ebccSVladimir Oltean 	if (!dev->of_node) {
36318aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
36328aa9ebccSVladimir Oltean 		return -EINVAL;
36338aa9ebccSVladimir Oltean 	}
36348aa9ebccSVladimir Oltean 
36358aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
36368aa9ebccSVladimir Oltean 	if (!priv)
36378aa9ebccSVladimir Oltean 		return -ENOMEM;
36388aa9ebccSVladimir Oltean 
36398aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
36408aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
36418aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
36428aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
36438aa9ebccSVladimir Oltean 	else
36448aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
36458aa9ebccSVladimir Oltean 
36468aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
36478aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
36488aa9ebccSVladimir Oltean 	 */
36498aa9ebccSVladimir Oltean 	priv->spidev = spi;
36508aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
36518aa9ebccSVladimir Oltean 
36528aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
36538aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
36548aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
36558aa9ebccSVladimir Oltean 	if (rc < 0) {
36568aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
36578aa9ebccSVladimir Oltean 		return rc;
36588aa9ebccSVladimir Oltean 	}
36598aa9ebccSVladimir Oltean 
3660718bad0eSVladimir Oltean 	/* In sja1105_xfer, we send spi_messages composed of two spi_transfers:
3661718bad0eSVladimir Oltean 	 * a small one for the message header and another one for the current
3662718bad0eSVladimir Oltean 	 * chunk of the packed buffer.
3663718bad0eSVladimir Oltean 	 * Check that the restrictions imposed by the SPI controller are
3664718bad0eSVladimir Oltean 	 * respected: the chunk buffer is smaller than the max transfer size,
3665718bad0eSVladimir Oltean 	 * and the total length of the chunk plus its message header is smaller
3666718bad0eSVladimir Oltean 	 * than the max message size.
3667718bad0eSVladimir Oltean 	 * We do that during probe time since the maximum transfer size is a
3668718bad0eSVladimir Oltean 	 * runtime invariant.
3669718bad0eSVladimir Oltean 	 */
3670718bad0eSVladimir Oltean 	max_xfer = spi_max_transfer_size(spi);
3671718bad0eSVladimir Oltean 	max_msg = spi_max_message_size(spi);
3672718bad0eSVladimir Oltean 
3673718bad0eSVladimir Oltean 	/* We need to send at least one 64-bit word of SPI payload per message
3674718bad0eSVladimir Oltean 	 * in order to be able to make useful progress.
3675718bad0eSVladimir Oltean 	 */
3676718bad0eSVladimir Oltean 	if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) {
3677718bad0eSVladimir Oltean 		dev_err(dev, "SPI master cannot send large enough buffers, aborting\n");
3678718bad0eSVladimir Oltean 		return -EINVAL;
3679718bad0eSVladimir Oltean 	}
3680718bad0eSVladimir Oltean 
3681718bad0eSVladimir Oltean 	priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN;
3682718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_xfer)
3683718bad0eSVladimir Oltean 		priv->max_xfer_len = max_xfer;
3684718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER)
3685718bad0eSVladimir Oltean 		priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER;
3686718bad0eSVladimir Oltean 
36878aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
36888aa9ebccSVladimir Oltean 
36898aa9ebccSVladimir Oltean 	/* Detect hardware device */
36908aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
36918aa9ebccSVladimir Oltean 	if (rc < 0) {
36928aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
36938aa9ebccSVladimir Oltean 		return rc;
36948aa9ebccSVladimir Oltean 	}
36958aa9ebccSVladimir Oltean 
36968aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
36978aa9ebccSVladimir Oltean 
36987e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
36998aa9ebccSVladimir Oltean 	if (!ds)
37008aa9ebccSVladimir Oltean 		return -ENOMEM;
37018aa9ebccSVladimir Oltean 
37027e99e347SVivien Didelot 	ds->dev = dev;
37033e77e59bSVladimir Oltean 	ds->num_ports = priv->info->num_ports;
37048aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
37058aa9ebccSVladimir Oltean 	ds->priv = priv;
37068aa9ebccSVladimir Oltean 	priv->ds = ds;
37078aa9ebccSVladimir Oltean 
3708844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
3709844d7edcSVladimir Oltean 
3710d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
3711d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
3712d5a619bfSVivien Didelot 
37135899ee36SVladimir Oltean 	priv->dsa_8021q_ctx = devm_kzalloc(dev, sizeof(*priv->dsa_8021q_ctx),
37145899ee36SVladimir Oltean 					   GFP_KERNEL);
37155899ee36SVladimir Oltean 	if (!priv->dsa_8021q_ctx)
37165899ee36SVladimir Oltean 		return -ENOMEM;
37175899ee36SVladimir Oltean 
37185899ee36SVladimir Oltean 	priv->dsa_8021q_ctx->ops = &sja1105_dsa_8021q_ops;
3719bbed0bbdSVladimir Oltean 	priv->dsa_8021q_ctx->proto = htons(ETH_P_8021Q);
37205899ee36SVladimir Oltean 	priv->dsa_8021q_ctx->ds = ds;
37215899ee36SVladimir Oltean 
37225899ee36SVladimir Oltean 	INIT_LIST_HEAD(&priv->dsa_8021q_ctx->crosschip_links);
3723ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&priv->bridge_vlans);
3724ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&priv->dsa_8021q_vlans);
3725ac02a451SVladimir Oltean 
3726d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
3727a6af7763SVladimir Oltean 	sja1105_flower_setup(ds);
3728d5a619bfSVivien Didelot 
3729d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
3730d5a619bfSVivien Didelot 	if (rc)
3731d5a619bfSVivien Didelot 		return rc;
3732d5a619bfSVivien Didelot 
37334d752508SVladimir Oltean 	if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
37344d752508SVladimir Oltean 		priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
37354d752508SVladimir Oltean 					 sizeof(struct sja1105_cbs_entry),
37364d752508SVladimir Oltean 					 GFP_KERNEL);
3737dc596e3fSVladimir Oltean 		if (!priv->cbs) {
3738dc596e3fSVladimir Oltean 			rc = -ENOMEM;
3739dc596e3fSVladimir Oltean 			goto out_unregister_switch;
3740dc596e3fSVladimir Oltean 		}
37414d752508SVladimir Oltean 	}
37424d752508SVladimir Oltean 
3743227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
3744542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
3745a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3746a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
3747a68578c2SVladimir Oltean 		struct net_device *slave;
374884eeb5d4SVladimir Oltean 		int subvlan;
3749227d07a0SVladimir Oltean 
3750a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3751a68578c2SVladimir Oltean 			continue;
3752a68578c2SVladimir Oltean 
3753a68578c2SVladimir Oltean 		dp->priv = sp;
3754a68578c2SVladimir Oltean 		sp->dp = dp;
3755844d7edcSVladimir Oltean 		sp->data = tagger_data;
3756a68578c2SVladimir Oltean 		slave = dp->slave;
3757a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
3758a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
3759a68578c2SVladimir Oltean 							slave->name);
3760a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
3761a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
3762a68578c2SVladimir Oltean 			dev_err(ds->dev,
3763a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
3764a68578c2SVladimir Oltean 				rc);
3765dc596e3fSVladimir Oltean 			goto out_destroy_workers;
3766a68578c2SVladimir Oltean 		}
3767a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
376838b5beeaSVladimir Oltean 		sp->xmit_tpid = ETH_P_SJA1105;
376984eeb5d4SVladimir Oltean 
377084eeb5d4SVladimir Oltean 		for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
377184eeb5d4SVladimir Oltean 			sp->subvlan_map[subvlan] = VLAN_N_VID;
3772227d07a0SVladimir Oltean 	}
3773227d07a0SVladimir Oltean 
3774d5a619bfSVivien Didelot 	return 0;
3775dc596e3fSVladimir Oltean 
3776dc596e3fSVladimir Oltean out_destroy_workers:
3777a68578c2SVladimir Oltean 	while (port-- > 0) {
3778a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3779a68578c2SVladimir Oltean 
3780a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3781a68578c2SVladimir Oltean 			continue;
3782a68578c2SVladimir Oltean 
3783a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
3784a68578c2SVladimir Oltean 	}
3785dc596e3fSVladimir Oltean 
3786dc596e3fSVladimir Oltean out_unregister_switch:
3787dc596e3fSVladimir Oltean 	dsa_unregister_switch(ds);
3788dc596e3fSVladimir Oltean 
3789a68578c2SVladimir Oltean 	return rc;
37908aa9ebccSVladimir Oltean }
37918aa9ebccSVladimir Oltean 
37928aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
37938aa9ebccSVladimir Oltean {
37948aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
37958aa9ebccSVladimir Oltean 
37968aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
37978aa9ebccSVladimir Oltean 	return 0;
37988aa9ebccSVladimir Oltean }
37998aa9ebccSVladimir Oltean 
38008aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
38018aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
38028aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
38038aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
38048aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
38058aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
38068aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
38073e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110a", .data = &sja1110a_info },
38083e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110b", .data = &sja1110b_info },
38093e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110c", .data = &sja1110c_info },
38103e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110d", .data = &sja1110d_info },
38118aa9ebccSVladimir Oltean 	{ /* sentinel */ },
38128aa9ebccSVladimir Oltean };
38138aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
38148aa9ebccSVladimir Oltean 
38158aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
38168aa9ebccSVladimir Oltean 	.driver = {
38178aa9ebccSVladimir Oltean 		.name  = "sja1105",
38188aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
38198aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
38208aa9ebccSVladimir Oltean 	},
38218aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
38228aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
38238aa9ebccSVladimir Oltean };
38248aa9ebccSVladimir Oltean 
38258aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
38268aa9ebccSVladimir Oltean 
38278aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
38288aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
38298aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
38308aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
3831