xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 5126ec72a094bd3a721941323c48cc80c60139d9)
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>
193ad1d171SVladimir 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 
60bef0746cSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
61bef0746cSVladimir Oltean {
62bef0746cSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
63bef0746cSVladimir Oltean 	int count, i;
64bef0746cSVladimir Oltean 
65bef0746cSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
66bef0746cSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
67bef0746cSVladimir Oltean 
68bef0746cSVladimir Oltean 	for (i = 0; i < count; i++)
69bef0746cSVladimir Oltean 		if (vlan[i].vlanid == vid)
70bef0746cSVladimir Oltean 			return i;
71bef0746cSVladimir Oltean 
72bef0746cSVladimir Oltean 	/* Return an invalid entry index if not found */
73bef0746cSVladimir Oltean 	return -1;
74bef0746cSVladimir Oltean }
75bef0746cSVladimir Oltean 
76bef0746cSVladimir Oltean static int sja1105_drop_untagged(struct dsa_switch *ds, int port, bool drop)
77bef0746cSVladimir Oltean {
78bef0746cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
79bef0746cSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
80bef0746cSVladimir Oltean 
81bef0746cSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
82bef0746cSVladimir Oltean 
83bef0746cSVladimir Oltean 	if (mac[port].drpuntag == drop)
84bef0746cSVladimir Oltean 		return 0;
85bef0746cSVladimir Oltean 
86bef0746cSVladimir Oltean 	mac[port].drpuntag = drop;
87bef0746cSVladimir Oltean 
88bef0746cSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
89bef0746cSVladimir Oltean 					    &mac[port], true);
90bef0746cSVladimir Oltean }
91bef0746cSVladimir Oltean 
92cde8078eSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
93cde8078eSVladimir Oltean {
94cde8078eSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
95cde8078eSVladimir Oltean 
96cde8078eSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
97cde8078eSVladimir Oltean 
98cde8078eSVladimir Oltean 	if (mac[port].vlanid == pvid)
99cde8078eSVladimir Oltean 		return 0;
100cde8078eSVladimir Oltean 
101cde8078eSVladimir Oltean 	mac[port].vlanid = pvid;
102cde8078eSVladimir Oltean 
103cde8078eSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
104cde8078eSVladimir Oltean 					    &mac[port], true);
105cde8078eSVladimir Oltean }
106cde8078eSVladimir Oltean 
107cde8078eSVladimir Oltean static int sja1105_commit_pvid(struct dsa_switch *ds, int port)
108cde8078eSVladimir Oltean {
109cde8078eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
110cde8078eSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
111bef0746cSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
112bef0746cSVladimir Oltean 	bool drop_untagged = false;
113bef0746cSVladimir Oltean 	int match, rc;
114cde8078eSVladimir Oltean 	u16 pvid;
115cde8078eSVladimir Oltean 
116cde8078eSVladimir Oltean 	if (dp->bridge_dev && br_vlan_enabled(dp->bridge_dev))
117cde8078eSVladimir Oltean 		pvid = priv->bridge_pvid[port];
118cde8078eSVladimir Oltean 	else
119cde8078eSVladimir Oltean 		pvid = priv->tag_8021q_pvid[port];
120cde8078eSVladimir Oltean 
121bef0746cSVladimir Oltean 	rc = sja1105_pvid_apply(priv, port, pvid);
122bef0746cSVladimir Oltean 	if (rc)
123bef0746cSVladimir Oltean 		return rc;
124bef0746cSVladimir Oltean 
125bef0746cSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
126bef0746cSVladimir Oltean 
127bef0746cSVladimir Oltean 	match = sja1105_is_vlan_configured(priv, pvid);
128bef0746cSVladimir Oltean 
129bef0746cSVladimir Oltean 	if (match < 0 || !(vlan[match].vmemb_port & BIT(port)))
130bef0746cSVladimir Oltean 		drop_untagged = true;
131bef0746cSVladimir Oltean 
132bef0746cSVladimir Oltean 	return sja1105_drop_untagged(ds, port, drop_untagged);
133cde8078eSVladimir Oltean }
134cde8078eSVladimir Oltean 
1358aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
1368aa9ebccSVladimir Oltean {
1378aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
1388aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
1398aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
1408aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
1418aa9ebccSVladimir Oltean 		 */
1428aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
1438aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
1448aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
1458aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
1468aa9ebccSVladimir Oltean 		.ifg = 0,
1478aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
1481fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
1498aa9ebccSVladimir Oltean 		 */
15041fed17fSVladimir Oltean 		.speed = priv->info->port_speed[SJA1105_SPEED_AUTO],
1518aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
1528aa9ebccSVladimir Oltean 		.tp_delin = 0,
1538aa9ebccSVladimir Oltean 		.tp_delout = 0,
1548aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
1558aa9ebccSVladimir Oltean 		.maxage = 0xFF,
1568aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
1578aa9ebccSVladimir Oltean 		.vlanprio = 0,
158e3502b82SVladimir Oltean 		.vlanid = 1,
1598aa9ebccSVladimir Oltean 		.ing_mirr = false,
1608aa9ebccSVladimir Oltean 		.egr_mirr = false,
1618aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
1628aa9ebccSVladimir Oltean 		.drpnona664 = false,
1638aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
1648aa9ebccSVladimir Oltean 		.drpdtag = false,
1658aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
1668aa9ebccSVladimir Oltean 		.drpuntag = false,
1678aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
1688aa9ebccSVladimir Oltean 		.retag = false,
169640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
170640f763fSVladimir Oltean 		 * STP will enable it.
171640f763fSVladimir Oltean 		 */
172640f763fSVladimir Oltean 		.dyn_learn = false,
1738aa9ebccSVladimir Oltean 		.egress = false,
1748aa9ebccSVladimir Oltean 		.ingress = false,
1758aa9ebccSVladimir Oltean 	};
1768aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
177542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
1788aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1795313a37bSVladimir Oltean 	struct dsa_port *dp;
1808aa9ebccSVladimir Oltean 
1818aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1828aa9ebccSVladimir Oltean 
1838aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1848aa9ebccSVladimir Oltean 	if (table->entry_count) {
1858aa9ebccSVladimir Oltean 		kfree(table->entries);
1868aa9ebccSVladimir Oltean 		table->entry_count = 0;
1878aa9ebccSVladimir Oltean 	}
1888aa9ebccSVladimir Oltean 
189fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
1908aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1918aa9ebccSVladimir Oltean 	if (!table->entries)
1928aa9ebccSVladimir Oltean 		return -ENOMEM;
1938aa9ebccSVladimir Oltean 
194fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
1958aa9ebccSVladimir Oltean 
1968aa9ebccSVladimir Oltean 	mac = table->entries;
1978aa9ebccSVladimir Oltean 
1985313a37bSVladimir Oltean 	list_for_each_entry(dp, &ds->dst->ports, list) {
1995313a37bSVladimir Oltean 		if (dp->ds != ds)
2005313a37bSVladimir Oltean 			continue;
2015313a37bSVladimir Oltean 
2025313a37bSVladimir Oltean 		mac[dp->index] = default_mac;
203b0b33b04SVladimir Oltean 
204b0b33b04SVladimir Oltean 		/* Let sja1105_bridge_stp_state_set() keep address learning
20581d45898SVladimir Oltean 		 * enabled for the DSA ports. CPU ports use software-assisted
20681d45898SVladimir Oltean 		 * learning to ensure that only FDB entries belonging to the
20781d45898SVladimir Oltean 		 * bridge are learned, and that they are learned towards all
20881d45898SVladimir Oltean 		 * CPU ports in a cross-chip topology if multiple CPU ports
20981d45898SVladimir Oltean 		 * exist.
210640f763fSVladimir Oltean 		 */
2115313a37bSVladimir Oltean 		if (dsa_port_is_dsa(dp))
2125313a37bSVladimir Oltean 			dp->learning = true;
213640f763fSVladimir Oltean 	}
2148aa9ebccSVladimir Oltean 
2158aa9ebccSVladimir Oltean 	return 0;
2168aa9ebccSVladimir Oltean }
2178aa9ebccSVladimir Oltean 
2185d645df9SVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv)
2198aa9ebccSVladimir Oltean {
2208aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
2218aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
222542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2238aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2248aa9ebccSVladimir Oltean 	int i;
2258aa9ebccSVladimir Oltean 
2268aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
2278aa9ebccSVladimir Oltean 
2288aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
2298aa9ebccSVladimir Oltean 	if (table->entry_count) {
2308aa9ebccSVladimir Oltean 		kfree(table->entries);
2318aa9ebccSVladimir Oltean 		table->entry_count = 0;
2328aa9ebccSVladimir Oltean 	}
2338aa9ebccSVladimir Oltean 
234fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
2358aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2368aa9ebccSVladimir Oltean 	if (!table->entries)
2378aa9ebccSVladimir Oltean 		return -ENOMEM;
2388aa9ebccSVladimir Oltean 
2391fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
240fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
2418aa9ebccSVladimir Oltean 
2428aa9ebccSVladimir Oltean 	mii = table->entries;
2438aa9ebccSVladimir Oltean 
244542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
2455d645df9SVladimir Oltean 		sja1105_mii_role_t role = XMII_MAC;
2465d645df9SVladimir Oltean 
247ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
248ee9d0cb6SVladimir Oltean 			continue;
249ee9d0cb6SVladimir Oltean 
2505d645df9SVladimir Oltean 		switch (priv->phy_mode[i]) {
2515a8f0974SVladimir Oltean 		case PHY_INTERFACE_MODE_INTERNAL:
2525a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_NO_PHY)
2535a8f0974SVladimir Oltean 				goto unsupported;
2545a8f0974SVladimir Oltean 
2555a8f0974SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
2565a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_PHY_BASE_TX)
2575a8f0974SVladimir Oltean 				mii->special[i] = true;
2585a8f0974SVladimir Oltean 
2595a8f0974SVladimir Oltean 			break;
2605d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVMII:
2615d645df9SVladimir Oltean 			role = XMII_PHY;
2625d645df9SVladimir Oltean 			fallthrough;
2638aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
26491a05078SVladimir Oltean 			if (!priv->info->supports_mii[i])
26591a05078SVladimir Oltean 				goto unsupported;
26691a05078SVladimir Oltean 
2678aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
2688aa9ebccSVladimir Oltean 			break;
2695d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVRMII:
2705d645df9SVladimir Oltean 			role = XMII_PHY;
2715d645df9SVladimir Oltean 			fallthrough;
2728aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
27391a05078SVladimir Oltean 			if (!priv->info->supports_rmii[i])
27491a05078SVladimir Oltean 				goto unsupported;
27591a05078SVladimir Oltean 
2768aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
2778aa9ebccSVladimir Oltean 			break;
2788aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
2798aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
2808aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
2818aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
28291a05078SVladimir Oltean 			if (!priv->info->supports_rgmii[i])
28391a05078SVladimir Oltean 				goto unsupported;
28491a05078SVladimir Oltean 
2858aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
2868aa9ebccSVladimir Oltean 			break;
287ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
28891a05078SVladimir Oltean 			if (!priv->info->supports_sgmii[i])
28991a05078SVladimir Oltean 				goto unsupported;
29091a05078SVladimir Oltean 
291ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
292ece578bcSVladimir Oltean 			mii->special[i] = true;
293ffe10e67SVladimir Oltean 			break;
29491a05078SVladimir Oltean 		case PHY_INTERFACE_MODE_2500BASEX:
29591a05078SVladimir Oltean 			if (!priv->info->supports_2500basex[i])
29691a05078SVladimir Oltean 				goto unsupported;
29791a05078SVladimir Oltean 
29891a05078SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
299ece578bcSVladimir Oltean 			mii->special[i] = true;
30091a05078SVladimir Oltean 			break;
30191a05078SVladimir Oltean unsupported:
3028aa9ebccSVladimir Oltean 		default:
30391a05078SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d!\n",
3045d645df9SVladimir Oltean 				phy_modes(priv->phy_mode[i]), i);
3056729188dSVladimir Oltean 			return -EINVAL;
3068aa9ebccSVladimir Oltean 		}
3078aa9ebccSVladimir Oltean 
3085d645df9SVladimir Oltean 		mii->phy_mac[i] = role;
3098aa9ebccSVladimir Oltean 	}
3108aa9ebccSVladimir Oltean 	return 0;
3118aa9ebccSVladimir Oltean }
3128aa9ebccSVladimir Oltean 
3138aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
3148aa9ebccSVladimir Oltean {
3154d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
3168aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3174d942354SVladimir Oltean 	int port;
3188aa9ebccSVladimir Oltean 
3198aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
3208aa9ebccSVladimir Oltean 
3214d942354SVladimir Oltean 	/* We only populate the FDB table through dynamic L2 Address Lookup
3224d942354SVladimir Oltean 	 * entries, except for a special entry at the end which is a catch-all
3234d942354SVladimir Oltean 	 * for unknown multicast and will be used to control flooding domain.
324291d1e72SVladimir Oltean 	 */
3258aa9ebccSVladimir Oltean 	if (table->entry_count) {
3268aa9ebccSVladimir Oltean 		kfree(table->entries);
3278aa9ebccSVladimir Oltean 		table->entry_count = 0;
3288aa9ebccSVladimir Oltean 	}
3294d942354SVladimir Oltean 
3304d942354SVladimir Oltean 	if (!priv->info->can_limit_mcast_flood)
3314d942354SVladimir Oltean 		return 0;
3324d942354SVladimir Oltean 
3334d942354SVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
3344d942354SVladimir Oltean 				 GFP_KERNEL);
3354d942354SVladimir Oltean 	if (!table->entries)
3364d942354SVladimir Oltean 		return -ENOMEM;
3374d942354SVladimir Oltean 
3384d942354SVladimir Oltean 	table->entry_count = 1;
3394d942354SVladimir Oltean 	l2_lookup = table->entries;
3404d942354SVladimir Oltean 
3414d942354SVladimir Oltean 	/* All L2 multicast addresses have an odd first octet */
3424d942354SVladimir Oltean 	l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST;
3434d942354SVladimir Oltean 	l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST;
3444d942354SVladimir Oltean 	l2_lookup[0].lockeds = true;
3454d942354SVladimir Oltean 	l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1;
3464d942354SVladimir Oltean 
3474d942354SVladimir Oltean 	/* Flood multicast to every port by default */
3484d942354SVladimir Oltean 	for (port = 0; port < priv->ds->num_ports; port++)
3494d942354SVladimir Oltean 		if (!dsa_is_unused_port(priv->ds, port))
3504d942354SVladimir Oltean 			l2_lookup[0].destports |= BIT(port);
3514d942354SVladimir Oltean 
3528aa9ebccSVladimir Oltean 	return 0;
3538aa9ebccSVladimir Oltean }
3548aa9ebccSVladimir Oltean 
3558aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
3568aa9ebccSVladimir Oltean {
3578aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
3588456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
3598456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
3608aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
3618aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
3621da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
3631da73821SVladimir Oltean 		.start_dynspc = 0,
3648aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
3658aa9ebccSVladimir Oltean 		.poly = 0x97,
3668aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
3678aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
3688aa9ebccSVladimir Oltean 		 */
3696d7c7d94SVladimir Oltean 		.shared_learn = true,
3708aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
3718aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
3728aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
3738aa9ebccSVladimir Oltean 		 */
3748aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
3758aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
3768aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
3778aa9ebccSVladimir Oltean 		 */
3788aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
3791da73821SVladimir Oltean 		/* P/Q/R/S only */
3801da73821SVladimir Oltean 		.use_static = true,
3811da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
3821da73821SVladimir Oltean 		 * dynamic FDB entries
3831da73821SVladimir Oltean 		 */
3841da73821SVladimir Oltean 		.owr_dyn = true,
3851da73821SVladimir Oltean 		.drpnolearn = true,
3868aa9ebccSVladimir Oltean 	};
387542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
388f238fef1SVladimir Oltean 	int port, num_used_ports = 0;
389542043e9SVladimir Oltean 	struct sja1105_table *table;
390542043e9SVladimir Oltean 	u64 max_fdb_entries;
391542043e9SVladimir Oltean 
392542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++)
393f238fef1SVladimir Oltean 		if (!dsa_is_unused_port(ds, port))
394f238fef1SVladimir Oltean 			num_used_ports++;
395f238fef1SVladimir Oltean 
396f238fef1SVladimir Oltean 	max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports;
397f238fef1SVladimir Oltean 
398f238fef1SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
399f238fef1SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
400f238fef1SVladimir Oltean 			continue;
401f238fef1SVladimir Oltean 
402542043e9SVladimir Oltean 		default_l2_lookup_params.maxaddrp[port] = max_fdb_entries;
403f238fef1SVladimir Oltean 	}
4048aa9ebccSVladimir Oltean 
4058aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
4068aa9ebccSVladimir Oltean 
4078aa9ebccSVladimir Oltean 	if (table->entry_count) {
4088aa9ebccSVladimir Oltean 		kfree(table->entries);
4098aa9ebccSVladimir Oltean 		table->entry_count = 0;
4108aa9ebccSVladimir Oltean 	}
4118aa9ebccSVladimir Oltean 
412fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4138aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4148aa9ebccSVladimir Oltean 	if (!table->entries)
4158aa9ebccSVladimir Oltean 		return -ENOMEM;
4168aa9ebccSVladimir Oltean 
417fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
4188aa9ebccSVladimir Oltean 
4198aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4208aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
4218aa9ebccSVladimir Oltean 				default_l2_lookup_params;
4228aa9ebccSVladimir Oltean 
4238aa9ebccSVladimir Oltean 	return 0;
4248aa9ebccSVladimir Oltean }
4258aa9ebccSVladimir Oltean 
426ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU
427ed040abcSVladimir Oltean  * using management routes (e.g. STP, PTP) as opposed to tag_8021q.
428ed040abcSVladimir Oltean  * All DT-defined ports are members of this VLAN, and there are no
429ed040abcSVladimir Oltean  * restrictions on forwarding (since the CPU selects the destination).
430ed040abcSVladimir Oltean  * Frames from this VLAN will always be transmitted as untagged, and
431ed040abcSVladimir Oltean  * neither the bridge nor the 8021q module cannot create this VLAN ID.
432ed040abcSVladimir Oltean  */
4338aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
4348aa9ebccSVladimir Oltean {
4358aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4368aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
4373e77e59bSVladimir Oltean 		.type_entry = SJA1110_VLAN_D_TAG,
4388aa9ebccSVladimir Oltean 		.ving_mirr = 0,
4398aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
4408aa9ebccSVladimir Oltean 		.vmemb_port = 0,
4418aa9ebccSVladimir Oltean 		.vlan_bc = 0,
4428aa9ebccSVladimir Oltean 		.tag_port = 0,
443ed040abcSVladimir Oltean 		.vlanid = SJA1105_DEFAULT_VLAN,
4448aa9ebccSVladimir Oltean 	};
445ec5ae610SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
446ec5ae610SVladimir Oltean 	int port;
4478aa9ebccSVladimir Oltean 
4488aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
4498aa9ebccSVladimir Oltean 
4508aa9ebccSVladimir Oltean 	if (table->entry_count) {
4518aa9ebccSVladimir Oltean 		kfree(table->entries);
4528aa9ebccSVladimir Oltean 		table->entry_count = 0;
4538aa9ebccSVladimir Oltean 	}
4548aa9ebccSVladimir Oltean 
455c75857b0SZheng Yongjun 	table->entries = kzalloc(table->ops->unpacked_entry_size,
4568aa9ebccSVladimir Oltean 				 GFP_KERNEL);
4578aa9ebccSVladimir Oltean 	if (!table->entries)
4588aa9ebccSVladimir Oltean 		return -ENOMEM;
4598aa9ebccSVladimir Oltean 
4608aa9ebccSVladimir Oltean 	table->entry_count = 1;
4618aa9ebccSVladimir Oltean 
462ec5ae610SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
463ec5ae610SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
464ec5ae610SVladimir Oltean 			continue;
465ec5ae610SVladimir Oltean 
466ec5ae610SVladimir Oltean 		pvid.vmemb_port |= BIT(port);
467ec5ae610SVladimir Oltean 		pvid.vlan_bc |= BIT(port);
468ec5ae610SVladimir Oltean 		pvid.tag_port &= ~BIT(port);
469ec5ae610SVladimir Oltean 
470c5130029SVladimir Oltean 		if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
4716dfd23d3SVladimir Oltean 			priv->tag_8021q_pvid[port] = SJA1105_DEFAULT_VLAN;
4726dfd23d3SVladimir Oltean 			priv->bridge_pvid[port] = SJA1105_DEFAULT_VLAN;
4736dfd23d3SVladimir Oltean 		}
4748aa9ebccSVladimir Oltean 	}
4758aa9ebccSVladimir Oltean 
4768aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
4778aa9ebccSVladimir Oltean 	return 0;
4788aa9ebccSVladimir Oltean }
4798aa9ebccSVladimir Oltean 
4808aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
4818aa9ebccSVladimir Oltean {
4828aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
483542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
4840f9b762cSVladimir Oltean 	struct dsa_switch_tree *dst;
4858aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4860f9b762cSVladimir Oltean 	struct dsa_link *dl;
4873fa21270SVladimir Oltean 	int port, tc;
4883fa21270SVladimir Oltean 	int from, to;
4898aa9ebccSVladimir Oltean 
4908aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
4918aa9ebccSVladimir Oltean 
4928aa9ebccSVladimir Oltean 	if (table->entry_count) {
4938aa9ebccSVladimir Oltean 		kfree(table->entries);
4948aa9ebccSVladimir Oltean 		table->entry_count = 0;
4958aa9ebccSVladimir Oltean 	}
4968aa9ebccSVladimir Oltean 
497fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4988aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4998aa9ebccSVladimir Oltean 	if (!table->entries)
5008aa9ebccSVladimir Oltean 		return -ENOMEM;
5018aa9ebccSVladimir Oltean 
502fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
5038aa9ebccSVladimir Oltean 
5048aa9ebccSVladimir Oltean 	l2fwd = table->entries;
5058aa9ebccSVladimir Oltean 
5063fa21270SVladimir Oltean 	/* First 5 entries in the L2 Forwarding Table define the forwarding
5073fa21270SVladimir Oltean 	 * rules and the VLAN PCP to ingress queue mapping.
5083fa21270SVladimir Oltean 	 * Set up the ingress queue mapping first.
5097f7ccdeaSVladimir Oltean 	 */
5103fa21270SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
5113fa21270SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
5128aa9ebccSVladimir Oltean 			continue;
5138aa9ebccSVladimir Oltean 
5143fa21270SVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
5153fa21270SVladimir Oltean 			l2fwd[port].vlan_pmap[tc] = tc;
5163fa21270SVladimir Oltean 	}
5174d942354SVladimir Oltean 
5183fa21270SVladimir Oltean 	/* Then manage the forwarding domain for user ports. These can forward
5193fa21270SVladimir Oltean 	 * only to the always-on domain (CPU port and DSA links)
5203fa21270SVladimir Oltean 	 */
5213fa21270SVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
5223fa21270SVladimir Oltean 		if (!dsa_is_user_port(ds, from))
5233fa21270SVladimir Oltean 			continue;
5244d942354SVladimir Oltean 
5253fa21270SVladimir Oltean 		for (to = 0; to < ds->num_ports; to++) {
5263fa21270SVladimir Oltean 			if (!dsa_is_cpu_port(ds, to) &&
5273fa21270SVladimir Oltean 			    !dsa_is_dsa_port(ds, to))
5283fa21270SVladimir Oltean 				continue;
5293fa21270SVladimir Oltean 
5303fa21270SVladimir Oltean 			l2fwd[from].bc_domain |= BIT(to);
5313fa21270SVladimir Oltean 			l2fwd[from].fl_domain |= BIT(to);
5323fa21270SVladimir Oltean 
5333fa21270SVladimir Oltean 			sja1105_port_allow_traffic(l2fwd, from, to, true);
5343fa21270SVladimir Oltean 		}
5353fa21270SVladimir Oltean 	}
5363fa21270SVladimir Oltean 
5373fa21270SVladimir Oltean 	/* Then manage the forwarding domain for DSA links and CPU ports (the
5383fa21270SVladimir Oltean 	 * always-on domain). These can send packets to any enabled port except
5393fa21270SVladimir Oltean 	 * themselves.
5403fa21270SVladimir Oltean 	 */
5413fa21270SVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
5423fa21270SVladimir Oltean 		if (!dsa_is_cpu_port(ds, from) && !dsa_is_dsa_port(ds, from))
5433fa21270SVladimir Oltean 			continue;
5443fa21270SVladimir Oltean 
5453fa21270SVladimir Oltean 		for (to = 0; to < ds->num_ports; to++) {
5463fa21270SVladimir Oltean 			if (dsa_is_unused_port(ds, to))
5473fa21270SVladimir Oltean 				continue;
5483fa21270SVladimir Oltean 
5493fa21270SVladimir Oltean 			if (from == to)
5503fa21270SVladimir Oltean 				continue;
5513fa21270SVladimir Oltean 
5523fa21270SVladimir Oltean 			l2fwd[from].bc_domain |= BIT(to);
5533fa21270SVladimir Oltean 			l2fwd[from].fl_domain |= BIT(to);
5543fa21270SVladimir Oltean 
5553fa21270SVladimir Oltean 			sja1105_port_allow_traffic(l2fwd, from, to, true);
5563fa21270SVladimir Oltean 		}
5573fa21270SVladimir Oltean 	}
5583fa21270SVladimir Oltean 
5590f9b762cSVladimir Oltean 	/* In odd topologies ("H" connections where there is a DSA link to
5600f9b762cSVladimir Oltean 	 * another switch which also has its own CPU port), TX packets can loop
5610f9b762cSVladimir Oltean 	 * back into the system (they are flooded from CPU port 1 to the DSA
5620f9b762cSVladimir Oltean 	 * link, and from there to CPU port 2). Prevent this from happening by
5630f9b762cSVladimir Oltean 	 * cutting RX from DSA links towards our CPU port, if the remote switch
5640f9b762cSVladimir Oltean 	 * has its own CPU port and therefore doesn't need ours for network
5650f9b762cSVladimir Oltean 	 * stack termination.
5660f9b762cSVladimir Oltean 	 */
5670f9b762cSVladimir Oltean 	dst = ds->dst;
5680f9b762cSVladimir Oltean 
5690f9b762cSVladimir Oltean 	list_for_each_entry(dl, &dst->rtable, list) {
5700f9b762cSVladimir Oltean 		if (dl->dp->ds != ds || dl->link_dp->cpu_dp == dl->dp->cpu_dp)
5710f9b762cSVladimir Oltean 			continue;
5720f9b762cSVladimir Oltean 
5730f9b762cSVladimir Oltean 		from = dl->dp->index;
5740f9b762cSVladimir Oltean 		to = dsa_upstream_port(ds, from);
5750f9b762cSVladimir Oltean 
5760f9b762cSVladimir Oltean 		dev_warn(ds->dev,
5770f9b762cSVladimir Oltean 			 "H topology detected, cutting RX from DSA link %d to CPU port %d to prevent TX packet loops\n",
5780f9b762cSVladimir Oltean 			 from, to);
5790f9b762cSVladimir Oltean 
5800f9b762cSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, from, to, false);
5810f9b762cSVladimir Oltean 
5820f9b762cSVladimir Oltean 		l2fwd[from].bc_domain &= ~BIT(to);
5830f9b762cSVladimir Oltean 		l2fwd[from].fl_domain &= ~BIT(to);
5840f9b762cSVladimir Oltean 	}
5850f9b762cSVladimir Oltean 
5863fa21270SVladimir Oltean 	/* Finally, manage the egress flooding domain. All ports start up with
5873fa21270SVladimir Oltean 	 * flooding enabled, including the CPU port and DSA links.
5883fa21270SVladimir Oltean 	 */
5893fa21270SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
5903fa21270SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
5913fa21270SVladimir Oltean 			continue;
5923fa21270SVladimir Oltean 
5933fa21270SVladimir Oltean 		priv->ucast_egress_floods |= BIT(port);
5943fa21270SVladimir Oltean 		priv->bcast_egress_floods |= BIT(port);
5958aa9ebccSVladimir Oltean 	}
596f238fef1SVladimir Oltean 
5978aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
5988aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
5998aa9ebccSVladimir Oltean 	 */
6003fa21270SVladimir Oltean 	for (tc = 0; tc < SJA1105_NUM_TC; tc++) {
6013fa21270SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
6023fa21270SVladimir Oltean 			if (dsa_is_unused_port(ds, port))
603f238fef1SVladimir Oltean 				continue;
604f238fef1SVladimir Oltean 
6053fa21270SVladimir Oltean 			l2fwd[ds->num_ports + tc].vlan_pmap[port] = tc;
606f238fef1SVladimir Oltean 		}
6073e77e59bSVladimir Oltean 
6083fa21270SVladimir Oltean 		l2fwd[ds->num_ports + tc].type_egrpcp2outputq = true;
6093e77e59bSVladimir Oltean 	}
6103e77e59bSVladimir Oltean 
6113e77e59bSVladimir Oltean 	return 0;
6123e77e59bSVladimir Oltean }
6133e77e59bSVladimir Oltean 
6143e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv)
6153e77e59bSVladimir Oltean {
6163e77e59bSVladimir Oltean 	struct sja1110_pcp_remapping_entry *pcp_remap;
6173e77e59bSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
6183e77e59bSVladimir Oltean 	struct sja1105_table *table;
6193e77e59bSVladimir Oltean 	int port, tc;
6203e77e59bSVladimir Oltean 
6213e77e59bSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING];
6223e77e59bSVladimir Oltean 
6233e77e59bSVladimir Oltean 	/* Nothing to do for SJA1105 */
6243e77e59bSVladimir Oltean 	if (!table->ops->max_entry_count)
6253e77e59bSVladimir Oltean 		return 0;
6263e77e59bSVladimir Oltean 
6273e77e59bSVladimir Oltean 	if (table->entry_count) {
6283e77e59bSVladimir Oltean 		kfree(table->entries);
6293e77e59bSVladimir Oltean 		table->entry_count = 0;
6303e77e59bSVladimir Oltean 	}
6313e77e59bSVladimir Oltean 
6323e77e59bSVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
6333e77e59bSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
6343e77e59bSVladimir Oltean 	if (!table->entries)
6353e77e59bSVladimir Oltean 		return -ENOMEM;
6363e77e59bSVladimir Oltean 
6373e77e59bSVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
6383e77e59bSVladimir Oltean 
6393e77e59bSVladimir Oltean 	pcp_remap = table->entries;
6403e77e59bSVladimir Oltean 
6413e77e59bSVladimir Oltean 	/* Repeat the configuration done for vlan_pmap */
6423e77e59bSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
6433e77e59bSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
6443e77e59bSVladimir Oltean 			continue;
6453e77e59bSVladimir Oltean 
6463e77e59bSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
6473e77e59bSVladimir Oltean 			pcp_remap[port].egrpcp[tc] = tc;
648f238fef1SVladimir Oltean 	}
6498aa9ebccSVladimir Oltean 
6508aa9ebccSVladimir Oltean 	return 0;
6518aa9ebccSVladimir Oltean }
6528aa9ebccSVladimir Oltean 
6538aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
6548aa9ebccSVladimir Oltean {
6551bf658eeSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2fwd_params;
6568aa9ebccSVladimir Oltean 	struct sja1105_table *table;
6578aa9ebccSVladimir Oltean 
6588aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
6598aa9ebccSVladimir Oltean 
6608aa9ebccSVladimir Oltean 	if (table->entry_count) {
6618aa9ebccSVladimir Oltean 		kfree(table->entries);
6628aa9ebccSVladimir Oltean 		table->entry_count = 0;
6638aa9ebccSVladimir Oltean 	}
6648aa9ebccSVladimir Oltean 
665fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
6668aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
6678aa9ebccSVladimir Oltean 	if (!table->entries)
6688aa9ebccSVladimir Oltean 		return -ENOMEM;
6698aa9ebccSVladimir Oltean 
670fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
6718aa9ebccSVladimir Oltean 
6728aa9ebccSVladimir Oltean 	/* This table only has a single entry */
6731bf658eeSVladimir Oltean 	l2fwd_params = table->entries;
6741bf658eeSVladimir Oltean 
6751bf658eeSVladimir Oltean 	/* Disallow dynamic reconfiguration of vlan_pmap */
6761bf658eeSVladimir Oltean 	l2fwd_params->max_dynp = 0;
6771bf658eeSVladimir Oltean 	/* Use a single memory partition for all ingress queues */
6781bf658eeSVladimir Oltean 	l2fwd_params->part_spc[0] = priv->info->max_frame_mem;
6798aa9ebccSVladimir Oltean 
6808aa9ebccSVladimir Oltean 	return 0;
6818aa9ebccSVladimir Oltean }
6828aa9ebccSVladimir Oltean 
683aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
684aaa270c6SVladimir Oltean {
685aaa270c6SVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
686aaa270c6SVladimir Oltean 	struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
687aaa270c6SVladimir Oltean 	struct sja1105_table *table;
688aaa270c6SVladimir Oltean 
689aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
690aaa270c6SVladimir Oltean 	l2_fwd_params = table->entries;
6910fac6aa0SVladimir Oltean 	l2_fwd_params->part_spc[0] = SJA1105_MAX_FRAME_MEMORY;
692aaa270c6SVladimir Oltean 
693aaa270c6SVladimir Oltean 	/* If we have any critical-traffic virtual links, we need to reserve
694aaa270c6SVladimir Oltean 	 * some frame buffer memory for them. At the moment, hardcode the value
695aaa270c6SVladimir Oltean 	 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
696aaa270c6SVladimir Oltean 	 * remaining for best-effort traffic. TODO: figure out a more flexible
697aaa270c6SVladimir Oltean 	 * way to perform the frame buffer partitioning.
698aaa270c6SVladimir Oltean 	 */
699aaa270c6SVladimir Oltean 	if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
700aaa270c6SVladimir Oltean 		return;
701aaa270c6SVladimir Oltean 
702aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
703aaa270c6SVladimir Oltean 	vl_fwd_params = table->entries;
704aaa270c6SVladimir Oltean 
705aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
706aaa270c6SVladimir Oltean 	vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
707aaa270c6SVladimir Oltean }
708aaa270c6SVladimir Oltean 
709ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values:
710ceec8bc0SVladimir Oltean  *
711ceec8bc0SVladimir Oltean  *      | 100 Mbps ports |  1Gbps ports  | 2.5Gbps ports | Disabled ports
712ceec8bc0SVladimir Oltean  * -----+----------------+---------------+---------------+---------------
713ceec8bc0SVladimir Oltean  *   0  |   0, [5:10]    |     [1:2]     |     [3:4]     |     retag
714ceec8bc0SVladimir Oltean  *   1  |0, [5:10], retag|     [1:2]     |     [3:4]     |       -
715ceec8bc0SVladimir Oltean  *   2  |   0, [5:10]    |  [1:3], retag |       4       |       -
716ceec8bc0SVladimir Oltean  *   3  |   0, [5:10]    |[1:2], 4, retag|       3       |       -
717ceec8bc0SVladimir Oltean  *   4  |  0, 2, [5:10]  |    1, retag   |     [3:4]     |       -
718ceec8bc0SVladimir Oltean  *   5  |  0, 1, [5:10]  |    2, retag   |     [3:4]     |       -
719ceec8bc0SVladimir Oltean  *  14  |   0, [5:10]    | [1:4], retag  |       -       |       -
720ceec8bc0SVladimir Oltean  *  15  |     [5:10]     | [0:4], retag  |       -       |       -
721ceec8bc0SVladimir Oltean  */
722ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv)
723ceec8bc0SVladimir Oltean {
724ceec8bc0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
725ceec8bc0SVladimir Oltean 	struct sja1105_table *table;
726ceec8bc0SVladimir Oltean 	bool port_1_is_base_tx;
727ceec8bc0SVladimir Oltean 	bool port_3_is_2500;
728ceec8bc0SVladimir Oltean 	bool port_4_is_2500;
729ceec8bc0SVladimir Oltean 	u64 tdmaconfigidx;
730ceec8bc0SVladimir Oltean 
731ceec8bc0SVladimir Oltean 	if (priv->info->device_id != SJA1110_DEVICE_ID)
732ceec8bc0SVladimir Oltean 		return;
733ceec8bc0SVladimir Oltean 
734ceec8bc0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
735ceec8bc0SVladimir Oltean 	general_params = table->entries;
736ceec8bc0SVladimir Oltean 
737ceec8bc0SVladimir Oltean 	/* All the settings below are "as opposed to SGMII", which is the
738ceec8bc0SVladimir Oltean 	 * other pinmuxing option.
739ceec8bc0SVladimir Oltean 	 */
740ceec8bc0SVladimir Oltean 	port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL;
741ceec8bc0SVladimir Oltean 	port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX;
742ceec8bc0SVladimir Oltean 	port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX;
743ceec8bc0SVladimir Oltean 
744ceec8bc0SVladimir Oltean 	if (port_1_is_base_tx)
745ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
746ceec8bc0SVladimir Oltean 		tdmaconfigidx = 5;
747ceec8bc0SVladimir Oltean 	else if (port_3_is_2500 && port_4_is_2500)
748ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 100 Mbps */
749ceec8bc0SVladimir Oltean 		tdmaconfigidx = 1;
750ceec8bc0SVladimir Oltean 	else if (port_3_is_2500)
751ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
752ceec8bc0SVladimir Oltean 		tdmaconfigidx = 3;
753ceec8bc0SVladimir Oltean 	else if (port_4_is_2500)
754ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
755ceec8bc0SVladimir Oltean 		tdmaconfigidx = 2;
756ceec8bc0SVladimir Oltean 	else
757ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
758ceec8bc0SVladimir Oltean 		tdmaconfigidx = 14;
759ceec8bc0SVladimir Oltean 
760ceec8bc0SVladimir Oltean 	general_params->tdmaconfigidx = tdmaconfigidx;
761ceec8bc0SVladimir Oltean }
762ceec8bc0SVladimir Oltean 
76330a100e6SVladimir Oltean static int sja1105_init_topology(struct sja1105_private *priv,
76430a100e6SVladimir Oltean 				 struct sja1105_general_params_entry *general_params)
76530a100e6SVladimir Oltean {
76630a100e6SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
76730a100e6SVladimir Oltean 	int port;
76830a100e6SVladimir Oltean 
76930a100e6SVladimir Oltean 	/* The host port is the destination for traffic matching mac_fltres1
77030a100e6SVladimir Oltean 	 * and mac_fltres0 on all ports except itself. Default to an invalid
77130a100e6SVladimir Oltean 	 * value.
77230a100e6SVladimir Oltean 	 */
77330a100e6SVladimir Oltean 	general_params->host_port = ds->num_ports;
77430a100e6SVladimir Oltean 
77530a100e6SVladimir Oltean 	/* Link-local traffic received on casc_port will be forwarded
77630a100e6SVladimir Oltean 	 * to host_port without embedding the source port and device ID
77730a100e6SVladimir Oltean 	 * info in the destination MAC address, and no RX timestamps will be
77830a100e6SVladimir Oltean 	 * taken either (presumably because it is a cascaded port and a
77930a100e6SVladimir Oltean 	 * downstream SJA switch already did that).
78030a100e6SVladimir Oltean 	 * To disable the feature, we need to do different things depending on
78130a100e6SVladimir Oltean 	 * switch generation. On SJA1105 we need to set an invalid port, while
78230a100e6SVladimir Oltean 	 * on SJA1110 which support multiple cascaded ports, this field is a
78330a100e6SVladimir Oltean 	 * bitmask so it must be left zero.
78430a100e6SVladimir Oltean 	 */
78530a100e6SVladimir Oltean 	if (!priv->info->multiple_cascade_ports)
78630a100e6SVladimir Oltean 		general_params->casc_port = ds->num_ports;
78730a100e6SVladimir Oltean 
78830a100e6SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
78930a100e6SVladimir Oltean 		bool is_upstream = dsa_is_upstream_port(ds, port);
79030a100e6SVladimir Oltean 		bool is_dsa_link = dsa_is_dsa_port(ds, port);
79130a100e6SVladimir Oltean 
79230a100e6SVladimir Oltean 		/* Upstream ports can be dedicated CPU ports or
79330a100e6SVladimir Oltean 		 * upstream-facing DSA links
79430a100e6SVladimir Oltean 		 */
79530a100e6SVladimir Oltean 		if (is_upstream) {
79630a100e6SVladimir Oltean 			if (general_params->host_port == ds->num_ports) {
79730a100e6SVladimir Oltean 				general_params->host_port = port;
79830a100e6SVladimir Oltean 			} else {
79930a100e6SVladimir Oltean 				dev_err(ds->dev,
80030a100e6SVladimir Oltean 					"Port %llu is already a host port, configuring %d as one too is not supported\n",
80130a100e6SVladimir Oltean 					general_params->host_port, port);
80230a100e6SVladimir Oltean 				return -EINVAL;
80330a100e6SVladimir Oltean 			}
80430a100e6SVladimir Oltean 		}
80530a100e6SVladimir Oltean 
80630a100e6SVladimir Oltean 		/* Cascade ports are downstream-facing DSA links */
80730a100e6SVladimir Oltean 		if (is_dsa_link && !is_upstream) {
80830a100e6SVladimir Oltean 			if (priv->info->multiple_cascade_ports) {
80930a100e6SVladimir Oltean 				general_params->casc_port |= BIT(port);
81030a100e6SVladimir Oltean 			} else if (general_params->casc_port == ds->num_ports) {
81130a100e6SVladimir Oltean 				general_params->casc_port = port;
81230a100e6SVladimir Oltean 			} else {
81330a100e6SVladimir Oltean 				dev_err(ds->dev,
81430a100e6SVladimir Oltean 					"Port %llu is already a cascade port, configuring %d as one too is not supported\n",
81530a100e6SVladimir Oltean 					general_params->casc_port, port);
81630a100e6SVladimir Oltean 				return -EINVAL;
81730a100e6SVladimir Oltean 			}
81830a100e6SVladimir Oltean 		}
81930a100e6SVladimir Oltean 	}
82030a100e6SVladimir Oltean 
82130a100e6SVladimir Oltean 	if (general_params->host_port == ds->num_ports) {
82230a100e6SVladimir Oltean 		dev_err(ds->dev, "No host port configured\n");
82330a100e6SVladimir Oltean 		return -EINVAL;
82430a100e6SVladimir Oltean 	}
82530a100e6SVladimir Oltean 
82630a100e6SVladimir Oltean 	return 0;
82730a100e6SVladimir Oltean }
82830a100e6SVladimir Oltean 
8298aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
8308aa9ebccSVladimir Oltean {
8318aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
832511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
833511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
8348aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
8355f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
8365f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
8375f06c63bSVladimir Oltean 		 */
83808fde09aSVladimir Oltean 		.hostprio = 7,
8398aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
8408aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
84142824463SVladimir Oltean 		.incl_srcpt1 = false,
8428aa9ebccSVladimir Oltean 		.send_meta1  = false,
8438aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
8448aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
84542824463SVladimir Oltean 		.incl_srcpt0 = false,
8468aa9ebccSVladimir Oltean 		.send_meta0  = false,
847511e6ca0SVladimir Oltean 		/* Default to an invalid value */
848542043e9SVladimir Oltean 		.mirr_port = priv->ds->num_ports,
8498aa9ebccSVladimir Oltean 		/* No TTEthernet */
850dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
8518aa9ebccSVladimir Oltean 		.vlmarker = 0,
8528aa9ebccSVladimir Oltean 		.vlmask = 0,
8538aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
8548aa9ebccSVladimir Oltean 		.ignore2stf = 0,
8556666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
8566666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
8576666cebcSVladimir Oltean 		 */
8586666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
8596666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
86029305260SVladimir Oltean 		/* Enable the TTEthernet engine on SJA1110 */
86129305260SVladimir Oltean 		.tte_en = true,
8624913b8ebSVladimir Oltean 		/* Set up the EtherType for control packets on SJA1110 */
8634913b8ebSVladimir Oltean 		.header_type = ETH_P_SJA1110,
8648aa9ebccSVladimir Oltean 	};
8656c0de59bSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
8668aa9ebccSVladimir Oltean 	struct sja1105_table *table;
86730a100e6SVladimir Oltean 	int rc;
868df2a81a3SVladimir Oltean 
86930a100e6SVladimir Oltean 	rc = sja1105_init_topology(priv, &default_general_params);
87030a100e6SVladimir Oltean 	if (rc)
87130a100e6SVladimir Oltean 		return rc;
8728aa9ebccSVladimir Oltean 
8738aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
8748aa9ebccSVladimir Oltean 
8758aa9ebccSVladimir Oltean 	if (table->entry_count) {
8768aa9ebccSVladimir Oltean 		kfree(table->entries);
8778aa9ebccSVladimir Oltean 		table->entry_count = 0;
8788aa9ebccSVladimir Oltean 	}
8798aa9ebccSVladimir Oltean 
880fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
8818aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
8828aa9ebccSVladimir Oltean 	if (!table->entries)
8838aa9ebccSVladimir Oltean 		return -ENOMEM;
8848aa9ebccSVladimir Oltean 
885fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
8868aa9ebccSVladimir Oltean 
8876c0de59bSVladimir Oltean 	general_params = table->entries;
8886c0de59bSVladimir Oltean 
8898aa9ebccSVladimir Oltean 	/* This table only has a single entry */
8906c0de59bSVladimir Oltean 	general_params[0] = default_general_params;
8918aa9ebccSVladimir Oltean 
892ceec8bc0SVladimir Oltean 	sja1110_select_tdmaconfigidx(priv);
893ceec8bc0SVladimir Oltean 
8948aa9ebccSVladimir Oltean 	return 0;
8958aa9ebccSVladimir Oltean }
8968aa9ebccSVladimir Oltean 
89779d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
89879d5511cSVladimir Oltean {
89979d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
90079d5511cSVladimir Oltean 	struct sja1105_table *table;
90179d5511cSVladimir Oltean 
90279d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
90379d5511cSVladimir Oltean 
90479d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
90579d5511cSVladimir Oltean 	if (table->entry_count) {
90679d5511cSVladimir Oltean 		kfree(table->entries);
90779d5511cSVladimir Oltean 		table->entry_count = 0;
90879d5511cSVladimir Oltean 	}
90979d5511cSVladimir Oltean 
910fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
91179d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
91279d5511cSVladimir Oltean 	if (!table->entries)
91379d5511cSVladimir Oltean 		return -ENOMEM;
91479d5511cSVladimir Oltean 
915fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
91679d5511cSVladimir Oltean 
91779d5511cSVladimir Oltean 	avb = table->entries;
91879d5511cSVladimir Oltean 
91979d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
92079d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
92179d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
922747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
923747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
924747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
925747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
926747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
927747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
928747e5eb3SVladimir Oltean 	 */
929747e5eb3SVladimir Oltean 	avb->cas_master = false;
93079d5511cSVladimir Oltean 
93179d5511cSVladimir Oltean 	return 0;
93279d5511cSVladimir Oltean }
93379d5511cSVladimir Oltean 
934a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
935a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
936a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
937a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
938a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
939a7cc081cSVladimir Oltean  * will be used for this frame.
940a7cc081cSVladimir Oltean  *
941a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
942a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
943a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
944a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
945a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
946a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
947a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
948a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
949a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
950a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
951a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
952a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
953a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
954a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
955a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
956a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
957a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
958a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
959a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
960a7cc081cSVladimir Oltean  * +------------+--------+
961a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
962a7cc081cSVladimir Oltean  * +------------+--------+
963a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
964a7cc081cSVladimir Oltean  * +------------+--------+
965a7cc081cSVladimir Oltean  *    ...                                  ...
966a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
967a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
968a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
969a7cc081cSVladimir Oltean  *
970a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
971a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
972a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
973a7cc081cSVladimir Oltean  * lookup) equal.
974a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
975a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
976a7cc081cSVladimir Oltean  */
9778aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
9788aa9ebccSVladimir Oltean 
9798aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
9808aa9ebccSVladimir Oltean {
9818aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
982542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
9838aa9ebccSVladimir Oltean 	struct sja1105_table *table;
984a7cc081cSVladimir Oltean 	int port, tc;
9858aa9ebccSVladimir Oltean 
9868aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
9878aa9ebccSVladimir Oltean 
9888aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
9898aa9ebccSVladimir Oltean 	if (table->entry_count) {
9908aa9ebccSVladimir Oltean 		kfree(table->entries);
9918aa9ebccSVladimir Oltean 		table->entry_count = 0;
9928aa9ebccSVladimir Oltean 	}
9938aa9ebccSVladimir Oltean 
994fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
9958aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
9968aa9ebccSVladimir Oltean 	if (!table->entries)
9978aa9ebccSVladimir Oltean 		return -ENOMEM;
9988aa9ebccSVladimir Oltean 
999fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
10008aa9ebccSVladimir Oltean 
10018aa9ebccSVladimir Oltean 	policing = table->entries;
10028aa9ebccSVladimir Oltean 
1003a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
1004542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
100538fbe91fSVladimir Oltean 		int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port;
1006542043e9SVladimir Oltean 		int bcast = (ds->num_ports * SJA1105_NUM_TC) + port;
1007a7cc081cSVladimir Oltean 
1008a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
1009a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
1010a7cc081cSVladimir Oltean 
1011a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
101238fbe91fSVladimir Oltean 		/* Only SJA1110 has multicast policers */
101338fbe91fSVladimir Oltean 		if (mcast <= table->ops->max_entry_count)
101438fbe91fSVladimir Oltean 			policing[mcast].sharindx = port;
1015a7cc081cSVladimir Oltean 	}
1016a7cc081cSVladimir Oltean 
1017a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
1018542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1019c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
1020c279c726SVladimir Oltean 
1021777e55e3SVladimir Oltean 		if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
1022c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
10238aa9ebccSVladimir Oltean 
1024a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
1025a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
1026a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
1027a7cc081cSVladimir Oltean 		policing[port].partition = 0;
10288aa9ebccSVladimir Oltean 	}
1029a7cc081cSVladimir Oltean 
10308aa9ebccSVladimir Oltean 	return 0;
10318aa9ebccSVladimir Oltean }
10328aa9ebccSVladimir Oltean 
10335d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv)
10348aa9ebccSVladimir Oltean {
10358aa9ebccSVladimir Oltean 	int rc;
10368aa9ebccSVladimir Oltean 
10378aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
10388aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
10398aa9ebccSVladimir Oltean 					priv->info->static_ops,
10408aa9ebccSVladimir Oltean 					priv->info->device_id);
10418aa9ebccSVladimir Oltean 	if (rc)
10428aa9ebccSVladimir Oltean 		return rc;
10438aa9ebccSVladimir Oltean 
10448aa9ebccSVladimir Oltean 	/* Build static configuration */
10458aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
10468aa9ebccSVladimir Oltean 	if (rc < 0)
10478aa9ebccSVladimir Oltean 		return rc;
10485d645df9SVladimir Oltean 	rc = sja1105_init_mii_settings(priv);
10498aa9ebccSVladimir Oltean 	if (rc < 0)
10508aa9ebccSVladimir Oltean 		return rc;
10518aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
10528aa9ebccSVladimir Oltean 	if (rc < 0)
10538aa9ebccSVladimir Oltean 		return rc;
10548aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
10558aa9ebccSVladimir Oltean 	if (rc < 0)
10568aa9ebccSVladimir Oltean 		return rc;
10578aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
10588aa9ebccSVladimir Oltean 	if (rc < 0)
10598aa9ebccSVladimir Oltean 		return rc;
10608aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
10618aa9ebccSVladimir Oltean 	if (rc < 0)
10628aa9ebccSVladimir Oltean 		return rc;
10638aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
10648aa9ebccSVladimir Oltean 	if (rc < 0)
10658aa9ebccSVladimir Oltean 		return rc;
10668aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
10678aa9ebccSVladimir Oltean 	if (rc < 0)
10688aa9ebccSVladimir Oltean 		return rc;
10698aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
10708aa9ebccSVladimir Oltean 	if (rc < 0)
10718aa9ebccSVladimir Oltean 		return rc;
107279d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
107379d5511cSVladimir Oltean 	if (rc < 0)
107479d5511cSVladimir Oltean 		return rc;
10753e77e59bSVladimir Oltean 	rc = sja1110_init_pcp_remapping(priv);
10763e77e59bSVladimir Oltean 	if (rc < 0)
10773e77e59bSVladimir Oltean 		return rc;
10788aa9ebccSVladimir Oltean 
10798aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
10808aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
10818aa9ebccSVladimir Oltean }
10828aa9ebccSVladimir Oltean 
108329afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv)
1084f5b8631cSVladimir Oltean {
1085542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
108629afb83aSVladimir Oltean 	int port;
1087f5b8631cSVladimir Oltean 
108829afb83aSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
108929afb83aSVladimir Oltean 		if (!priv->fixed_link[port])
1090f5b8631cSVladimir Oltean 			continue;
1091f5b8631cSVladimir Oltean 
109229afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID ||
109329afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
109429afb83aSVladimir Oltean 			priv->rgmii_rx_delay[port] = true;
1095f5b8631cSVladimir Oltean 
109629afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID ||
109729afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
109829afb83aSVladimir Oltean 			priv->rgmii_tx_delay[port] = true;
1099f5b8631cSVladimir Oltean 
110029afb83aSVladimir Oltean 		if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) &&
1101f5b8631cSVladimir Oltean 		    !priv->info->setup_rgmii_delay)
1102f5b8631cSVladimir Oltean 			return -EINVAL;
1103f5b8631cSVladimir Oltean 	}
1104f5b8631cSVladimir Oltean 	return 0;
1105f5b8631cSVladimir Oltean }
1106f5b8631cSVladimir Oltean 
11078aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
11088aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
11098aa9ebccSVladimir Oltean {
11108aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
11118aa9ebccSVladimir Oltean 	struct device_node *child;
11128aa9ebccSVladimir Oltean 
111327afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
11148aa9ebccSVladimir Oltean 		struct device_node *phy_node;
11150c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
11168aa9ebccSVladimir Oltean 		u32 index;
11170c65b2b9SAndrew Lunn 		int err;
11188aa9ebccSVladimir Oltean 
11198aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
11208aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
11218aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
11228aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
11237ba771e3SNishka Dasgupta 			of_node_put(child);
11248aa9ebccSVladimir Oltean 			return -ENODEV;
11258aa9ebccSVladimir Oltean 		}
11268aa9ebccSVladimir Oltean 
11278aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
11280c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
11290c65b2b9SAndrew Lunn 		if (err) {
11308aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
11318aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
11328aa9ebccSVladimir Oltean 				index);
11337ba771e3SNishka Dasgupta 			of_node_put(child);
11348aa9ebccSVladimir Oltean 			return -ENODEV;
11358aa9ebccSVladimir Oltean 		}
11368aa9ebccSVladimir Oltean 
11378aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
11388aa9ebccSVladimir Oltean 		if (!phy_node) {
11398aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
11408aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
11418aa9ebccSVladimir Oltean 					"properties missing!\n");
11427ba771e3SNishka Dasgupta 				of_node_put(child);
11438aa9ebccSVladimir Oltean 				return -ENODEV;
11448aa9ebccSVladimir Oltean 			}
11458aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
11468aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
11478aa9ebccSVladimir Oltean 			 */
114829afb83aSVladimir Oltean 			priv->fixed_link[index] = true;
11498aa9ebccSVladimir Oltean 		} else {
11508aa9ebccSVladimir Oltean 			of_node_put(phy_node);
11518aa9ebccSVladimir Oltean 		}
11528aa9ebccSVladimir Oltean 
1153bf4edf4aSVladimir Oltean 		priv->phy_mode[index] = phy_mode;
11548aa9ebccSVladimir Oltean 	}
11558aa9ebccSVladimir Oltean 
11568aa9ebccSVladimir Oltean 	return 0;
11578aa9ebccSVladimir Oltean }
11588aa9ebccSVladimir Oltean 
11595d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv)
11608aa9ebccSVladimir Oltean {
11618aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
11628aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
11638aa9ebccSVladimir Oltean 	struct device_node *ports_node;
11648aa9ebccSVladimir Oltean 	int rc;
11658aa9ebccSVladimir Oltean 
11668aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
116715074a36SVladimir Oltean 	if (!ports_node)
116815074a36SVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
11698aa9ebccSVladimir Oltean 	if (!ports_node) {
11708aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
11718aa9ebccSVladimir Oltean 		return -ENODEV;
11728aa9ebccSVladimir Oltean 	}
11738aa9ebccSVladimir Oltean 
11745d645df9SVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports_node);
11758aa9ebccSVladimir Oltean 	of_node_put(ports_node);
11768aa9ebccSVladimir Oltean 
11778aa9ebccSVladimir Oltean 	return rc;
11788aa9ebccSVladimir Oltean }
11798aa9ebccSVladimir Oltean 
1180c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
118141fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv,
118241fed17fSVladimir Oltean 					 u64 speed)
118341fed17fSVladimir Oltean {
118441fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS])
118541fed17fSVladimir Oltean 		return SPEED_10;
118641fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS])
118741fed17fSVladimir Oltean 		return SPEED_100;
118841fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS])
118941fed17fSVladimir Oltean 		return SPEED_1000;
119041fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS])
119141fed17fSVladimir Oltean 		return SPEED_2500;
119241fed17fSVladimir Oltean 	return SPEED_UNKNOWN;
119341fed17fSVladimir Oltean }
11948aa9ebccSVladimir Oltean 
11958400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
11968aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
11978400cff6SVladimir Oltean 				      int speed_mbps)
11988aa9ebccSVladimir Oltean {
11998aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
12008aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
120141fed17fSVladimir Oltean 	u64 speed;
12028aa9ebccSVladimir Oltean 	int rc;
12038aa9ebccSVladimir Oltean 
12048400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
12058400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
12068400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
12078400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
12088400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
12098400cff6SVladimir Oltean 	 */
12108aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
12118aa9ebccSVladimir Oltean 
1212f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
1213c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
1214a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
1215a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
1216a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
1217a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
1218a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
1219a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
1220a979a0abSVladimir Oltean 		 */
122141fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
1222f4cfcfbdSVladimir Oltean 		break;
1223c44d0535SVladimir Oltean 	case SPEED_10:
122441fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_10MBPS];
1225f4cfcfbdSVladimir Oltean 		break;
1226c44d0535SVladimir Oltean 	case SPEED_100:
122741fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_100MBPS];
1228f4cfcfbdSVladimir Oltean 		break;
1229c44d0535SVladimir Oltean 	case SPEED_1000:
123041fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
1231f4cfcfbdSVladimir Oltean 		break;
123256b63466SVladimir Oltean 	case SPEED_2500:
123356b63466SVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
123456b63466SVladimir Oltean 		break;
1235f4cfcfbdSVladimir Oltean 	default:
12368aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
12378aa9ebccSVladimir Oltean 		return -EINVAL;
12388aa9ebccSVladimir Oltean 	}
12398aa9ebccSVladimir Oltean 
12408400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
12418400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
12428400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
12438400cff6SVladimir Oltean 	 * we want auto during upload phase).
1244ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
1245ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
12468aa9ebccSVladimir Oltean 	 */
124791a05078SVladimir Oltean 	if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII)
124841fed17fSVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
124956b63466SVladimir Oltean 	else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX)
125056b63466SVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
1251ffe10e67SVladimir Oltean 	else
12528aa9ebccSVladimir Oltean 		mac[port].speed = speed;
12538aa9ebccSVladimir Oltean 
12548aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
12558400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
12568400cff6SVladimir Oltean 					  &mac[port], true);
12578aa9ebccSVladimir Oltean 	if (rc < 0) {
12588aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
12598aa9ebccSVladimir Oltean 		return rc;
12608aa9ebccSVladimir Oltean 	}
12618aa9ebccSVladimir Oltean 
12628aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
12638aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
12648aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
12658aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
12668aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
12678aa9ebccSVladimir Oltean 	 */
126891a05078SVladimir Oltean 	if (!phy_interface_mode_is_rgmii(priv->phy_mode[port]))
12698aa9ebccSVladimir Oltean 		return 0;
12708aa9ebccSVladimir Oltean 
12718aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
12728aa9ebccSVladimir Oltean }
12738aa9ebccSVladimir Oltean 
127439710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
127539710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
127639710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
127739710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
127839710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
127939710229SVladimir Oltean  * now.
128039710229SVladimir Oltean  */
128139710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
128239710229SVladimir Oltean 				      phy_interface_t interface)
128339710229SVladimir Oltean {
1284bf4edf4aSVladimir Oltean 	return priv->phy_mode[port] != interface;
128539710229SVladimir Oltean }
128639710229SVladimir Oltean 
1287af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
1288ffe10e67SVladimir Oltean 			       unsigned int mode,
1289af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
12908aa9ebccSVladimir Oltean {
12913ad1d171SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
12928aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12933ad1d171SVladimir Oltean 	struct dw_xpcs *xpcs;
12948aa9ebccSVladimir Oltean 
1295ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1296ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
1297ec8582d1SVladimir Oltean 			phy_modes(state->interface));
129839710229SVladimir Oltean 		return;
1299ec8582d1SVladimir Oltean 	}
130039710229SVladimir Oltean 
13013ad1d171SVladimir Oltean 	xpcs = priv->xpcs[port];
1302ffe10e67SVladimir Oltean 
13033ad1d171SVladimir Oltean 	if (xpcs)
13043ad1d171SVladimir Oltean 		phylink_set_pcs(dp->pl, &xpcs->pcs);
13058400cff6SVladimir Oltean }
13068400cff6SVladimir Oltean 
13078400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
13088400cff6SVladimir Oltean 				  unsigned int mode,
13098400cff6SVladimir Oltean 				  phy_interface_t interface)
13108400cff6SVladimir Oltean {
13118400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
13128400cff6SVladimir Oltean }
13138400cff6SVladimir Oltean 
13148400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
13158400cff6SVladimir Oltean 				unsigned int mode,
13168400cff6SVladimir Oltean 				phy_interface_t interface,
13175b502a7bSRussell King 				struct phy_device *phydev,
13185b502a7bSRussell King 				int speed, int duplex,
13195b502a7bSRussell King 				bool tx_pause, bool rx_pause)
13208400cff6SVladimir Oltean {
1321ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1322ec8582d1SVladimir Oltean 
1323ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1324ec8582d1SVladimir Oltean 
1325ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
13268aa9ebccSVladimir Oltean }
13278aa9ebccSVladimir Oltean 
1328ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1329ad9f299aSVladimir Oltean 				     unsigned long *supported,
1330ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
1331ad9f299aSVladimir Oltean {
1332ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
1333ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
1334ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
1335ad9f299aSVladimir Oltean 	 */
1336ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1337ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1338ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1339ad9f299aSVladimir Oltean 
1340ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1341ad9f299aSVladimir Oltean 
134239710229SVladimir Oltean 	/* include/linux/phylink.h says:
134339710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
134439710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
134539710229SVladimir Oltean 	 */
134639710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
134739710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
134839710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
134939710229SVladimir Oltean 		return;
135039710229SVladimir Oltean 	}
135139710229SVladimir Oltean 
1352ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1353ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1354ad9f299aSVladimir Oltean 	 */
1355ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
1356ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
1357ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
1358ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
1359ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
1360ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1361ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1362ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
136356b63466SVladimir Oltean 	if (priv->info->supports_2500basex[port]) {
136456b63466SVladimir Oltean 		phylink_set(mask, 2500baseT_Full);
136556b63466SVladimir Oltean 		phylink_set(mask, 2500baseX_Full);
136656b63466SVladimir Oltean 	}
1367ad9f299aSVladimir Oltean 
1368ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1369ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1370ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1371ad9f299aSVladimir Oltean }
1372ad9f299aSVladimir Oltean 
137360f6053fSVladimir Oltean static int
137460f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
137560f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
137660f6053fSVladimir Oltean {
137760f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
137860f6053fSVladimir Oltean 	struct sja1105_table *table;
137960f6053fSVladimir Oltean 	int i;
138060f6053fSVladimir Oltean 
138160f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
138260f6053fSVladimir Oltean 	l2_lookup = table->entries;
138360f6053fSVladimir Oltean 
138460f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
138560f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
138660f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
138760f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
138860f6053fSVladimir Oltean 			return i;
138960f6053fSVladimir Oltean 
139060f6053fSVladimir Oltean 	return -1;
139160f6053fSVladimir Oltean }
139260f6053fSVladimir Oltean 
139360f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
139460f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
139560f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
139660f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
139760f6053fSVladimir Oltean  */
139860f6053fSVladimir Oltean static int
139960f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
140060f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
140160f6053fSVladimir Oltean 			  bool keep)
140260f6053fSVladimir Oltean {
140360f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
140460f6053fSVladimir Oltean 	struct sja1105_table *table;
140560f6053fSVladimir Oltean 	int rc, match;
140660f6053fSVladimir Oltean 
140760f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
140860f6053fSVladimir Oltean 
140960f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
141060f6053fSVladimir Oltean 	if (match < 0) {
141160f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
141260f6053fSVladimir Oltean 		if (!keep)
141360f6053fSVladimir Oltean 			return 0;
141460f6053fSVladimir Oltean 
141560f6053fSVladimir Oltean 		/* No match => new entry */
141660f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
141760f6053fSVladimir Oltean 		if (rc)
141860f6053fSVladimir Oltean 			return rc;
141960f6053fSVladimir Oltean 
142060f6053fSVladimir Oltean 		match = table->entry_count - 1;
142160f6053fSVladimir Oltean 	}
142260f6053fSVladimir Oltean 
142360f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
142460f6053fSVladimir Oltean 	l2_lookup = table->entries;
142560f6053fSVladimir Oltean 
142660f6053fSVladimir Oltean 	/* We have a match.
142760f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
142860f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
142960f6053fSVladimir Oltean 	 * which we update it).
143060f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
143160f6053fSVladimir Oltean 	 */
143260f6053fSVladimir Oltean 	if (keep) {
143360f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
143460f6053fSVladimir Oltean 		return 0;
143560f6053fSVladimir Oltean 	}
143660f6053fSVladimir Oltean 
143760f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
143860f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
143960f6053fSVladimir Oltean 	 */
144060f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
144160f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
144260f6053fSVladimir Oltean }
144360f6053fSVladimir Oltean 
1444291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1445291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1446291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1447291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1448291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1449291d1e72SVladimir Oltean  */
145009c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1451291d1e72SVladimir Oltean {
1452291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1453291d1e72SVladimir Oltean }
1454291d1e72SVladimir Oltean 
14559dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1456291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1457291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1458291d1e72SVladimir Oltean 					 int *last_unused)
1459291d1e72SVladimir Oltean {
1460291d1e72SVladimir Oltean 	int way;
1461291d1e72SVladimir Oltean 
1462291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1463291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1464291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1465291d1e72SVladimir Oltean 
1466291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1467291d1e72SVladimir Oltean 		 * into the return value
1468291d1e72SVladimir Oltean 		 */
1469291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1470291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1471291d1e72SVladimir Oltean 			if (last_unused)
1472291d1e72SVladimir Oltean 				*last_unused = way;
1473291d1e72SVladimir Oltean 			continue;
1474291d1e72SVladimir Oltean 		}
1475291d1e72SVladimir Oltean 
1476291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1477291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1478291d1e72SVladimir Oltean 			if (match)
1479291d1e72SVladimir Oltean 				*match = l2_lookup;
1480291d1e72SVladimir Oltean 			return way;
1481291d1e72SVladimir Oltean 		}
1482291d1e72SVladimir Oltean 	}
1483291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1484291d1e72SVladimir Oltean 	return -1;
1485291d1e72SVladimir Oltean }
1486291d1e72SVladimir Oltean 
14879dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1488291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1489291d1e72SVladimir Oltean {
14906c5fc159SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp;
1491291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1492291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1493291d1e72SVladimir Oltean 	int last_unused = -1;
14946c5fc159SVladimir Oltean 	int start, end, i;
149560f6053fSVladimir Oltean 	int bin, way, rc;
1496291d1e72SVladimir Oltean 
14979dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1498291d1e72SVladimir Oltean 
14999dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1500291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1501291d1e72SVladimir Oltean 	if (way >= 0) {
1502291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1503291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1504291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1505291d1e72SVladimir Oltean 		 */
1506e11e865bSVladimir Oltean 		if ((l2_lookup.destports & BIT(port)) && l2_lookup.lockeds)
1507291d1e72SVladimir Oltean 			return 0;
1508291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1509291d1e72SVladimir Oltean 	} else {
1510291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1511291d1e72SVladimir Oltean 
1512291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1513291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1514291d1e72SVladimir Oltean 		 */
1515291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1516291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1517291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1518291d1e72SVladimir Oltean 
1519291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1520291d1e72SVladimir Oltean 			way = last_unused;
1521291d1e72SVladimir Oltean 		} else {
1522291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1523291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1524291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1525291d1e72SVladimir Oltean 			 * distribution function:
1526291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1527291d1e72SVladimir Oltean 			 */
1528291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1529291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1530291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1531291d1e72SVladimir Oltean 				 bin, addr, way);
1532291d1e72SVladimir Oltean 			/* Evict entry */
1533291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1534291d1e72SVladimir Oltean 						     index, NULL, false);
1535291d1e72SVladimir Oltean 		}
1536291d1e72SVladimir Oltean 	}
1537e11e865bSVladimir Oltean 	l2_lookup.lockeds = true;
1538291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1539291d1e72SVladimir Oltean 
154060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1541291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1542291d1e72SVladimir Oltean 					  true);
154360f6053fSVladimir Oltean 	if (rc < 0)
154460f6053fSVladimir Oltean 		return rc;
154560f6053fSVladimir Oltean 
15466c5fc159SVladimir Oltean 	/* Invalidate a dynamically learned entry if that exists */
15476c5fc159SVladimir Oltean 	start = sja1105et_fdb_index(bin, 0);
15486c5fc159SVladimir Oltean 	end = sja1105et_fdb_index(bin, way);
15496c5fc159SVladimir Oltean 
15506c5fc159SVladimir Oltean 	for (i = start; i < end; i++) {
15516c5fc159SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
15526c5fc159SVladimir Oltean 						 i, &tmp);
15536c5fc159SVladimir Oltean 		if (rc == -ENOENT)
15546c5fc159SVladimir Oltean 			continue;
15556c5fc159SVladimir Oltean 		if (rc)
15566c5fc159SVladimir Oltean 			return rc;
15576c5fc159SVladimir Oltean 
15586c5fc159SVladimir Oltean 		if (tmp.macaddr != ether_addr_to_u64(addr) || tmp.vlanid != vid)
15596c5fc159SVladimir Oltean 			continue;
15606c5fc159SVladimir Oltean 
15616c5fc159SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
15626c5fc159SVladimir Oltean 						  i, NULL, false);
15636c5fc159SVladimir Oltean 		if (rc)
15646c5fc159SVladimir Oltean 			return rc;
15656c5fc159SVladimir Oltean 
15666c5fc159SVladimir Oltean 		break;
15676c5fc159SVladimir Oltean 	}
15686c5fc159SVladimir Oltean 
156960f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1570291d1e72SVladimir Oltean }
1571291d1e72SVladimir Oltean 
15729dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1573291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1574291d1e72SVladimir Oltean {
1575291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1576291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
157760f6053fSVladimir Oltean 	int index, bin, way, rc;
1578291d1e72SVladimir Oltean 	bool keep;
1579291d1e72SVladimir Oltean 
15809dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
15819dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1582291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1583291d1e72SVladimir Oltean 	if (way < 0)
1584291d1e72SVladimir Oltean 		return 0;
1585291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1586291d1e72SVladimir Oltean 
1587291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1588291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1589291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1590291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1591291d1e72SVladimir Oltean 	 */
1592291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
15937752e937SVladimir Oltean 
1594291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1595291d1e72SVladimir Oltean 		keep = true;
1596291d1e72SVladimir Oltean 	else
1597291d1e72SVladimir Oltean 		keep = false;
1598291d1e72SVladimir Oltean 
159960f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1600291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
160160f6053fSVladimir Oltean 	if (rc < 0)
160260f6053fSVladimir Oltean 		return rc;
160360f6053fSVladimir Oltean 
160460f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1605291d1e72SVladimir Oltean }
1606291d1e72SVladimir Oltean 
16079dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
16089dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
16099dfa6911SVladimir Oltean {
16106c5fc159SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp;
16111da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16121da73821SVladimir Oltean 	int rc, i;
16131da73821SVladimir Oltean 
16141da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
16151da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
16161da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
16171da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
16181da73821SVladimir Oltean 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
16191da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
16201da73821SVladimir Oltean 
1621728db843SVladimir Oltean 	tmp = l2_lookup;
1622728db843SVladimir Oltean 
16231da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1624728db843SVladimir Oltean 					 SJA1105_SEARCH, &tmp);
1625728db843SVladimir Oltean 	if (rc == 0 && tmp.index != SJA1105_MAX_L2_LOOKUP_COUNT - 1) {
1626e11e865bSVladimir Oltean 		/* Found a static entry and this port is already in the entry's
16271da73821SVladimir Oltean 		 * port mask => job done
16281da73821SVladimir Oltean 		 */
1629728db843SVladimir Oltean 		if ((tmp.destports & BIT(port)) && tmp.lockeds)
16301da73821SVladimir Oltean 			return 0;
1631728db843SVladimir Oltean 
1632728db843SVladimir Oltean 		l2_lookup = tmp;
1633728db843SVladimir Oltean 
16341da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
16351da73821SVladimir Oltean 		 * found something.
16361da73821SVladimir Oltean 		 */
16371da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
16381da73821SVladimir Oltean 		goto skip_finding_an_index;
16391da73821SVladimir Oltean 	}
16401da73821SVladimir Oltean 
16411da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
16421da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
16431da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
16441da73821SVladimir Oltean 	 */
16451da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
16461da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
16471da73821SVladimir Oltean 						 i, NULL);
16481da73821SVladimir Oltean 		if (rc < 0)
16491da73821SVladimir Oltean 			break;
16501da73821SVladimir Oltean 	}
16511da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
16521da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
16531da73821SVladimir Oltean 		return -EINVAL;
16541da73821SVladimir Oltean 	}
16551da73821SVladimir Oltean 	l2_lookup.index = i;
16561da73821SVladimir Oltean 
16571da73821SVladimir Oltean skip_finding_an_index:
1658e11e865bSVladimir Oltean 	l2_lookup.lockeds = true;
1659e11e865bSVladimir Oltean 
166060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
16611da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
16621da73821SVladimir Oltean 					  true);
166360f6053fSVladimir Oltean 	if (rc < 0)
166460f6053fSVladimir Oltean 		return rc;
166560f6053fSVladimir Oltean 
16666c5fc159SVladimir Oltean 	/* The switch learns dynamic entries and looks up the FDB left to
16676c5fc159SVladimir Oltean 	 * right. It is possible that our addition was concurrent with the
16686c5fc159SVladimir Oltean 	 * dynamic learning of the same address, so now that the static entry
16696c5fc159SVladimir Oltean 	 * has been installed, we are certain that address learning for this
16706c5fc159SVladimir Oltean 	 * particular address has been turned off, so the dynamic entry either
16716c5fc159SVladimir Oltean 	 * is in the FDB at an index smaller than the static one, or isn't (it
16726c5fc159SVladimir Oltean 	 * can also be at a larger index, but in that case it is inactive
16736c5fc159SVladimir Oltean 	 * because the static FDB entry will match first, and the dynamic one
16746c5fc159SVladimir Oltean 	 * will eventually age out). Search for a dynamically learned address
16756c5fc159SVladimir Oltean 	 * prior to our static one and invalidate it.
16766c5fc159SVladimir Oltean 	 */
16776c5fc159SVladimir Oltean 	tmp = l2_lookup;
16786c5fc159SVladimir Oltean 
16796c5fc159SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
16806c5fc159SVladimir Oltean 					 SJA1105_SEARCH, &tmp);
16816c5fc159SVladimir Oltean 	if (rc < 0) {
16826c5fc159SVladimir Oltean 		dev_err(ds->dev,
16836c5fc159SVladimir Oltean 			"port %d failed to read back entry for %pM vid %d: %pe\n",
16846c5fc159SVladimir Oltean 			port, addr, vid, ERR_PTR(rc));
16856c5fc159SVladimir Oltean 		return rc;
16866c5fc159SVladimir Oltean 	}
16876c5fc159SVladimir Oltean 
16886c5fc159SVladimir Oltean 	if (tmp.index < l2_lookup.index) {
16896c5fc159SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
16906c5fc159SVladimir Oltean 						  tmp.index, NULL, false);
16916c5fc159SVladimir Oltean 		if (rc < 0)
16926c5fc159SVladimir Oltean 			return rc;
16936c5fc159SVladimir Oltean 	}
16946c5fc159SVladimir Oltean 
169560f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
16969dfa6911SVladimir Oltean }
16979dfa6911SVladimir Oltean 
16989dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
16999dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
17009dfa6911SVladimir Oltean {
17011da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
17021da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
17031da73821SVladimir Oltean 	bool keep;
17041da73821SVladimir Oltean 	int rc;
17051da73821SVladimir Oltean 
17061da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
17071da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
17081da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
17091da73821SVladimir Oltean 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
17101da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
17111da73821SVladimir Oltean 
17121da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
17131da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
17141da73821SVladimir Oltean 	if (rc < 0)
17151da73821SVladimir Oltean 		return 0;
17161da73821SVladimir Oltean 
17171da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
17181da73821SVladimir Oltean 
17191da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
17201da73821SVladimir Oltean 	 * or if we remove it completely.
17211da73821SVladimir Oltean 	 */
17221da73821SVladimir Oltean 	if (l2_lookup.destports)
17231da73821SVladimir Oltean 		keep = true;
17241da73821SVladimir Oltean 	else
17251da73821SVladimir Oltean 		keep = false;
17261da73821SVladimir Oltean 
172760f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
17281da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
172960f6053fSVladimir Oltean 	if (rc < 0)
173060f6053fSVladimir Oltean 		return rc;
173160f6053fSVladimir Oltean 
173260f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
17339dfa6911SVladimir Oltean }
17349dfa6911SVladimir Oltean 
17359dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
17369dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
17379dfa6911SVladimir Oltean {
17389dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1739b3ee526aSVladimir Oltean 
17406d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
17419dfa6911SVladimir Oltean }
17429dfa6911SVladimir Oltean 
17439dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
17449dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
17459dfa6911SVladimir Oltean {
17469dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
17479dfa6911SVladimir Oltean 
1748b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
17499dfa6911SVladimir Oltean }
17509dfa6911SVladimir Oltean 
1751291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1752291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1753291d1e72SVladimir Oltean {
1754291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1755291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1756291d1e72SVladimir Oltean 	int i;
1757291d1e72SVladimir Oltean 
1758291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1759291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1760291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1761291d1e72SVladimir Oltean 		int rc;
1762291d1e72SVladimir Oltean 
1763291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1764291d1e72SVladimir Oltean 						 i, &l2_lookup);
1765291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1766def84604SVladimir Oltean 		if (rc == -ENOENT)
1767291d1e72SVladimir Oltean 			continue;
1768291d1e72SVladimir Oltean 		if (rc) {
1769291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1770291d1e72SVladimir Oltean 			return rc;
1771291d1e72SVladimir Oltean 		}
1772291d1e72SVladimir Oltean 
1773291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1774291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1775291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1776291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1777291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1778291d1e72SVladimir Oltean 		 */
1779291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1780291d1e72SVladimir Oltean 			continue;
17814d942354SVladimir Oltean 
17824d942354SVladimir Oltean 		/* We need to hide the FDB entry for unknown multicast */
17834d942354SVladimir Oltean 		if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST &&
17844d942354SVladimir Oltean 		    l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
17854d942354SVladimir Oltean 			continue;
17864d942354SVladimir Oltean 
1787291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
178893647594SVladimir Oltean 
17896d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
17900fac6aa0SVladimir Oltean 		if (!priv->vlan_aware)
17916d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
179217ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1793291d1e72SVladimir Oltean 	}
1794291d1e72SVladimir Oltean 	return 0;
1795291d1e72SVladimir Oltean }
1796291d1e72SVladimir Oltean 
1797*5126ec72SVladimir Oltean static void sja1105_fast_age(struct dsa_switch *ds, int port)
1798*5126ec72SVladimir Oltean {
1799*5126ec72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1800*5126ec72SVladimir Oltean 	int i;
1801*5126ec72SVladimir Oltean 
1802*5126ec72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1803*5126ec72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1804*5126ec72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1805*5126ec72SVladimir Oltean 		int rc;
1806*5126ec72SVladimir Oltean 
1807*5126ec72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1808*5126ec72SVladimir Oltean 						 i, &l2_lookup);
1809*5126ec72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1810*5126ec72SVladimir Oltean 		if (rc == -ENOENT)
1811*5126ec72SVladimir Oltean 			continue;
1812*5126ec72SVladimir Oltean 		if (rc) {
1813*5126ec72SVladimir Oltean 			dev_err(ds->dev, "Failed to read FDB: %pe\n",
1814*5126ec72SVladimir Oltean 				ERR_PTR(rc));
1815*5126ec72SVladimir Oltean 			return;
1816*5126ec72SVladimir Oltean 		}
1817*5126ec72SVladimir Oltean 
1818*5126ec72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1819*5126ec72SVladimir Oltean 			continue;
1820*5126ec72SVladimir Oltean 
1821*5126ec72SVladimir Oltean 		/* Don't delete static FDB entries */
1822*5126ec72SVladimir Oltean 		if (l2_lookup.lockeds)
1823*5126ec72SVladimir Oltean 			continue;
1824*5126ec72SVladimir Oltean 
1825*5126ec72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
1826*5126ec72SVladimir Oltean 
1827*5126ec72SVladimir Oltean 		rc = sja1105_fdb_del(ds, port, macaddr, l2_lookup.vlanid);
1828*5126ec72SVladimir Oltean 		if (rc) {
1829*5126ec72SVladimir Oltean 			dev_err(ds->dev,
1830*5126ec72SVladimir Oltean 				"Failed to delete FDB entry %pM vid %lld: %pe\n",
1831*5126ec72SVladimir Oltean 				macaddr, l2_lookup.vlanid, ERR_PTR(rc));
1832*5126ec72SVladimir Oltean 			return;
1833*5126ec72SVladimir Oltean 		}
1834*5126ec72SVladimir Oltean 	}
1835*5126ec72SVladimir Oltean }
1836*5126ec72SVladimir Oltean 
1837a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port,
1838291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1839291d1e72SVladimir Oltean {
1840a52b2da7SVladimir Oltean 	return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1841291d1e72SVladimir Oltean }
1842291d1e72SVladimir Oltean 
1843291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1844291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1845291d1e72SVladimir Oltean {
1846291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1847291d1e72SVladimir Oltean }
1848291d1e72SVladimir Oltean 
18497f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration.
18507f7ccdeaSVladimir Oltean  * Flooding is configured between each {ingress, egress} port pair, and since
18517f7ccdeaSVladimir Oltean  * the bridge's semantics are those of "egress flooding", it means we must
18527f7ccdeaSVladimir Oltean  * enable flooding towards this port from all ingress ports that are in the
18537f7ccdeaSVladimir Oltean  * same forwarding domain.
18547f7ccdeaSVladimir Oltean  */
18557f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv)
18567f7ccdeaSVladimir Oltean {
18577f7ccdeaSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
18587f7ccdeaSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
18597f7ccdeaSVladimir Oltean 	int from, to, rc;
18607f7ccdeaSVladimir Oltean 
18617f7ccdeaSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
18627f7ccdeaSVladimir Oltean 
18637f7ccdeaSVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
18647f7ccdeaSVladimir Oltean 		u64 fl_domain = 0, bc_domain = 0;
18657f7ccdeaSVladimir Oltean 
18667f7ccdeaSVladimir Oltean 		for (to = 0; to < priv->ds->num_ports; to++) {
18677f7ccdeaSVladimir Oltean 			if (!sja1105_can_forward(l2_fwd, from, to))
18687f7ccdeaSVladimir Oltean 				continue;
18697f7ccdeaSVladimir Oltean 
18707f7ccdeaSVladimir Oltean 			if (priv->ucast_egress_floods & BIT(to))
18717f7ccdeaSVladimir Oltean 				fl_domain |= BIT(to);
18727f7ccdeaSVladimir Oltean 			if (priv->bcast_egress_floods & BIT(to))
18737f7ccdeaSVladimir Oltean 				bc_domain |= BIT(to);
18747f7ccdeaSVladimir Oltean 		}
18757f7ccdeaSVladimir Oltean 
18767f7ccdeaSVladimir Oltean 		/* Nothing changed, nothing to do */
18777f7ccdeaSVladimir Oltean 		if (l2_fwd[from].fl_domain == fl_domain &&
18787f7ccdeaSVladimir Oltean 		    l2_fwd[from].bc_domain == bc_domain)
18797f7ccdeaSVladimir Oltean 			continue;
18807f7ccdeaSVladimir Oltean 
18817f7ccdeaSVladimir Oltean 		l2_fwd[from].fl_domain = fl_domain;
18827f7ccdeaSVladimir Oltean 		l2_fwd[from].bc_domain = bc_domain;
18837f7ccdeaSVladimir Oltean 
18847f7ccdeaSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
18857f7ccdeaSVladimir Oltean 						  from, &l2_fwd[from], true);
18867f7ccdeaSVladimir Oltean 		if (rc < 0)
18877f7ccdeaSVladimir Oltean 			return rc;
18887f7ccdeaSVladimir Oltean 	}
18897f7ccdeaSVladimir Oltean 
18907f7ccdeaSVladimir Oltean 	return 0;
18917f7ccdeaSVladimir Oltean }
18927f7ccdeaSVladimir Oltean 
18938aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
18948aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
18958aa9ebccSVladimir Oltean {
18968aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
18978aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18988aa9ebccSVladimir Oltean 	int i, rc;
18998aa9ebccSVladimir Oltean 
19008aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
19018aa9ebccSVladimir Oltean 
1902542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
19038aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
19048aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
19058aa9ebccSVladimir Oltean 		 */
19068aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
19078aa9ebccSVladimir Oltean 			continue;
19088aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
19098aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
19108aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
19118aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
19128aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
19138aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
19148aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
19158aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
19168aa9ebccSVladimir Oltean 		 */
19178aa9ebccSVladimir Oltean 		if (i == port)
19188aa9ebccSVladimir Oltean 			continue;
19198aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
19208aa9ebccSVladimir Oltean 			continue;
19218aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
19228aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
19238aa9ebccSVladimir Oltean 
19248aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
19258aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
19268aa9ebccSVladimir Oltean 		if (rc < 0)
19278aa9ebccSVladimir Oltean 			return rc;
19288aa9ebccSVladimir Oltean 	}
19298aa9ebccSVladimir Oltean 
19307f7ccdeaSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
19318aa9ebccSVladimir Oltean 					  port, &l2_fwd[port], true);
19327f7ccdeaSVladimir Oltean 	if (rc)
19337f7ccdeaSVladimir Oltean 		return rc;
19347f7ccdeaSVladimir Oltean 
1935cde8078eSVladimir Oltean 	rc = sja1105_commit_pvid(ds, port);
1936cde8078eSVladimir Oltean 	if (rc)
1937cde8078eSVladimir Oltean 		return rc;
1938cde8078eSVladimir Oltean 
19397f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
19408aa9ebccSVladimir Oltean }
19418aa9ebccSVladimir Oltean 
1942640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1943640f763fSVladimir Oltean 					 u8 state)
1944640f763fSVladimir Oltean {
19455313a37bSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
1946640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1947640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1948640f763fSVladimir Oltean 
1949640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1950640f763fSVladimir Oltean 
1951640f763fSVladimir Oltean 	switch (state) {
1952640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1953640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1954640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1955640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1956640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1957640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1958640f763fSVladimir Oltean 		 */
1959640f763fSVladimir Oltean 		mac[port].ingress   = false;
1960640f763fSVladimir Oltean 		mac[port].egress    = false;
1961640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1962640f763fSVladimir Oltean 		break;
1963640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1964640f763fSVladimir Oltean 		mac[port].ingress   = true;
1965640f763fSVladimir Oltean 		mac[port].egress    = false;
1966640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1967640f763fSVladimir Oltean 		break;
1968640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1969640f763fSVladimir Oltean 		mac[port].ingress   = true;
1970640f763fSVladimir Oltean 		mac[port].egress    = false;
19715313a37bSVladimir Oltean 		mac[port].dyn_learn = dp->learning;
1972640f763fSVladimir Oltean 		break;
1973640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1974640f763fSVladimir Oltean 		mac[port].ingress   = true;
1975640f763fSVladimir Oltean 		mac[port].egress    = true;
19765313a37bSVladimir Oltean 		mac[port].dyn_learn = dp->learning;
1977640f763fSVladimir Oltean 		break;
1978640f763fSVladimir Oltean 	default:
1979640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1980640f763fSVladimir Oltean 		return;
1981640f763fSVladimir Oltean 	}
1982640f763fSVladimir Oltean 
1983640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1984640f763fSVladimir Oltean 				     &mac[port], true);
1985640f763fSVladimir Oltean }
1986640f763fSVladimir Oltean 
19878aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
19888aa9ebccSVladimir Oltean 			       struct net_device *br)
19898aa9ebccSVladimir Oltean {
19908aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
19918aa9ebccSVladimir Oltean }
19928aa9ebccSVladimir Oltean 
19938aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
19948aa9ebccSVladimir Oltean 				 struct net_device *br)
19958aa9ebccSVladimir Oltean {
19968aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
19978aa9ebccSVladimir Oltean }
19988aa9ebccSVladimir Oltean 
19994d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8)
20004d752508SVladimir Oltean 
20014d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
20024d752508SVladimir Oltean {
20034d752508SVladimir Oltean 	int i;
20044d752508SVladimir Oltean 
20054d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
20064d752508SVladimir Oltean 		if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
20074d752508SVladimir Oltean 			return i;
20084d752508SVladimir Oltean 
20094d752508SVladimir Oltean 	return -1;
20104d752508SVladimir Oltean }
20114d752508SVladimir Oltean 
20124d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
20134d752508SVladimir Oltean 				     int prio)
20144d752508SVladimir Oltean {
20154d752508SVladimir Oltean 	int i;
20164d752508SVladimir Oltean 
20174d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
20184d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
20194d752508SVladimir Oltean 
20204d752508SVladimir Oltean 		if (cbs->port == port && cbs->prio == prio) {
20214d752508SVladimir Oltean 			memset(cbs, 0, sizeof(*cbs));
20224d752508SVladimir Oltean 			return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
20234d752508SVladimir Oltean 							    i, cbs, true);
20244d752508SVladimir Oltean 		}
20254d752508SVladimir Oltean 	}
20264d752508SVladimir Oltean 
20274d752508SVladimir Oltean 	return 0;
20284d752508SVladimir Oltean }
20294d752508SVladimir Oltean 
20304d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
20314d752508SVladimir Oltean 				struct tc_cbs_qopt_offload *offload)
20324d752508SVladimir Oltean {
20334d752508SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
20344d752508SVladimir Oltean 	struct sja1105_cbs_entry *cbs;
20354d752508SVladimir Oltean 	int index;
20364d752508SVladimir Oltean 
20374d752508SVladimir Oltean 	if (!offload->enable)
20384d752508SVladimir Oltean 		return sja1105_delete_cbs_shaper(priv, port, offload->queue);
20394d752508SVladimir Oltean 
20404d752508SVladimir Oltean 	index = sja1105_find_unused_cbs_shaper(priv);
20414d752508SVladimir Oltean 	if (index < 0)
20424d752508SVladimir Oltean 		return -ENOSPC;
20434d752508SVladimir Oltean 
20444d752508SVladimir Oltean 	cbs = &priv->cbs[index];
20454d752508SVladimir Oltean 	cbs->port = port;
20464d752508SVladimir Oltean 	cbs->prio = offload->queue;
20474d752508SVladimir Oltean 	/* locredit and sendslope are negative by definition. In hardware,
20484d752508SVladimir Oltean 	 * positive values must be provided, and the negative sign is implicit.
20494d752508SVladimir Oltean 	 */
20504d752508SVladimir Oltean 	cbs->credit_hi = offload->hicredit;
20514d752508SVladimir Oltean 	cbs->credit_lo = abs(offload->locredit);
20524d752508SVladimir Oltean 	/* User space is in kbits/sec, hardware in bytes/sec */
20534d752508SVladimir Oltean 	cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
20544d752508SVladimir Oltean 	cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
20554d752508SVladimir Oltean 	/* Convert the negative values from 64-bit 2's complement
20564d752508SVladimir Oltean 	 * to 32-bit 2's complement (for the case of 0x80000000 whose
20574d752508SVladimir Oltean 	 * negative is still negative).
20584d752508SVladimir Oltean 	 */
20594d752508SVladimir Oltean 	cbs->credit_lo &= GENMASK_ULL(31, 0);
20604d752508SVladimir Oltean 	cbs->send_slope &= GENMASK_ULL(31, 0);
20614d752508SVladimir Oltean 
20624d752508SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
20634d752508SVladimir Oltean 					    true);
20644d752508SVladimir Oltean }
20654d752508SVladimir Oltean 
20664d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv)
20674d752508SVladimir Oltean {
20684d752508SVladimir Oltean 	int rc = 0, i;
20694d752508SVladimir Oltean 
2070be7f62eeSVladimir Oltean 	/* The credit based shapers are only allocated if
2071be7f62eeSVladimir Oltean 	 * CONFIG_NET_SCH_CBS is enabled.
2072be7f62eeSVladimir Oltean 	 */
2073be7f62eeSVladimir Oltean 	if (!priv->cbs)
2074be7f62eeSVladimir Oltean 		return 0;
2075be7f62eeSVladimir Oltean 
20764d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
20774d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
20784d752508SVladimir Oltean 
20794d752508SVladimir Oltean 		if (!cbs->idle_slope && !cbs->send_slope)
20804d752508SVladimir Oltean 			continue;
20814d752508SVladimir Oltean 
20824d752508SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
20834d752508SVladimir Oltean 						  true);
20844d752508SVladimir Oltean 		if (rc)
20854d752508SVladimir Oltean 			break;
20864d752508SVladimir Oltean 	}
20874d752508SVladimir Oltean 
20884d752508SVladimir Oltean 	return rc;
20894d752508SVladimir Oltean }
20904d752508SVladimir Oltean 
20912eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
20922eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
20932eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
20942eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
20952eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
2096c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
2097dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
20982eea1fa8SVladimir Oltean };
20992eea1fa8SVladimir Oltean 
21006666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
21016666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
21026666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
21036666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
21046666cebcSVladimir Oltean  * such that this operation is relatively seamless.
21056666cebcSVladimir Oltean  */
21062eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
21072eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
21086666cebcSVladimir Oltean {
21096cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
21106cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
211182760d7fSVladimir Oltean 	int speed_mbps[SJA1105_MAX_NUM_PORTS];
211284db00f2SVladimir Oltean 	u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0};
21136666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
21146cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
21156cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
21166cf99c13SVladimir Oltean 	s64 t12, t34;
21176666cebcSVladimir Oltean 	int rc, i;
21186cf99c13SVladimir Oltean 	s64 now;
21196666cebcSVladimir Oltean 
2120af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
2121af580ae2SVladimir Oltean 
21226666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
21236666cebcSVladimir Oltean 
21248400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
21258400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
21268400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
21278400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
21286666cebcSVladimir Oltean 	 */
2129542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
21303ad1d171SVladimir Oltean 		u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1);
21313ad1d171SVladimir Oltean 
213241fed17fSVladimir Oltean 		speed_mbps[i] = sja1105_port_speed_to_ethtool(priv,
213341fed17fSVladimir Oltean 							      mac[i].speed);
213441fed17fSVladimir Oltean 		mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
21356666cebcSVladimir Oltean 
21363ad1d171SVladimir Oltean 		if (priv->xpcs[i])
21373ad1d171SVladimir Oltean 			bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr);
213884db00f2SVladimir Oltean 	}
2139ffe10e67SVladimir Oltean 
21406cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
21416cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
21426cf99c13SVladimir Oltean 
21436cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
214461c77533SVladimir Oltean 	if (rc < 0) {
214561c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
214661c77533SVladimir Oltean 		goto out;
214761c77533SVladimir Oltean 	}
21486cf99c13SVladimir Oltean 
21496666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
21506666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
215161c77533SVladimir Oltean 	if (rc < 0) {
215261c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
215361c77533SVladimir Oltean 		goto out;
215461c77533SVladimir Oltean 	}
21556cf99c13SVladimir Oltean 
21566cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
215761c77533SVladimir Oltean 	if (rc < 0) {
215861c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
215961c77533SVladimir Oltean 		goto out;
216061c77533SVladimir Oltean 	}
21616cf99c13SVladimir Oltean 
21626cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
21636cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
21646cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
21656cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
21666cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
21676cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
21686cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
21696cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
21706cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
21716cf99c13SVladimir Oltean 	now += (t34 - t12);
21726cf99c13SVladimir Oltean 
21736cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
21746cf99c13SVladimir Oltean 
21756cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
21766666cebcSVladimir Oltean 
21772eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
21782eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
21792eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
21802eea1fa8SVladimir Oltean 
21816666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
21826666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
21836666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
21846666cebcSVladimir Oltean 	 */
2185cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
2186c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
21876666cebcSVladimir Oltean 		if (rc < 0)
21886666cebcSVladimir Oltean 			goto out;
2189cb5a82d2SVladimir Oltean 	}
21906666cebcSVladimir Oltean 
2191542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
21923ad1d171SVladimir Oltean 		struct dw_xpcs *xpcs = priv->xpcs[i];
21933ad1d171SVladimir Oltean 		unsigned int mode;
219484db00f2SVladimir Oltean 
21958400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
21966666cebcSVladimir Oltean 		if (rc < 0)
21976666cebcSVladimir Oltean 			goto out;
2198ffe10e67SVladimir Oltean 
21993ad1d171SVladimir Oltean 		if (!xpcs)
220084db00f2SVladimir Oltean 			continue;
2201ffe10e67SVladimir Oltean 
22023ad1d171SVladimir Oltean 		if (bmcr[i] & BMCR_ANENABLE)
22033ad1d171SVladimir Oltean 			mode = MLO_AN_INBAND;
22043ad1d171SVladimir Oltean 		else if (priv->fixed_link[i])
22053ad1d171SVladimir Oltean 			mode = MLO_AN_FIXED;
22063ad1d171SVladimir Oltean 		else
22073ad1d171SVladimir Oltean 			mode = MLO_AN_PHY;
220884db00f2SVladimir Oltean 
22093ad1d171SVladimir Oltean 		rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode);
22103ad1d171SVladimir Oltean 		if (rc < 0)
22113ad1d171SVladimir Oltean 			goto out;
2212ffe10e67SVladimir Oltean 
22133ad1d171SVladimir Oltean 		if (!phylink_autoneg_inband(mode)) {
2214ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
2215ffe10e67SVladimir Oltean 
221656b63466SVladimir Oltean 			if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX)
221756b63466SVladimir Oltean 				speed = SPEED_2500;
221856b63466SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED1000)
2219ffe10e67SVladimir Oltean 				speed = SPEED_1000;
222084db00f2SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED100)
2221ffe10e67SVladimir Oltean 				speed = SPEED_100;
2222053d8ad1SVladimir Oltean 			else
2223ffe10e67SVladimir Oltean 				speed = SPEED_10;
2224ffe10e67SVladimir Oltean 
22253ad1d171SVladimir Oltean 			xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i],
22263ad1d171SVladimir Oltean 				     speed, DUPLEX_FULL);
2227ffe10e67SVladimir Oltean 		}
2228ffe10e67SVladimir Oltean 	}
22294d752508SVladimir Oltean 
22304d752508SVladimir Oltean 	rc = sja1105_reload_cbs(priv);
22314d752508SVladimir Oltean 	if (rc < 0)
22324d752508SVladimir Oltean 		goto out;
22336666cebcSVladimir Oltean out:
2234af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
2235af580ae2SVladimir Oltean 
22366666cebcSVladimir Oltean 	return rc;
22376666cebcSVladimir Oltean }
22386666cebcSVladimir Oltean 
22398aa9ebccSVladimir Oltean static enum dsa_tag_protocol
22404d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
22414d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
22428aa9ebccSVladimir Oltean {
22434913b8ebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
22444913b8ebSVladimir Oltean 
22454913b8ebSVladimir Oltean 	return priv->info->tag_proto;
22468aa9ebccSVladimir Oltean }
22478aa9ebccSVladimir Oltean 
2248070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
2249070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
2250070ca3bbSVladimir Oltean  * So a switch reset is required.
2251070ca3bbSVladimir Oltean  */
225289153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
225389153ed6SVladimir Oltean 			   struct netlink_ext_ack *extack)
22546666cebcSVladimir Oltean {
22556d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2256070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
22576666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2258070ca3bbSVladimir Oltean 	struct sja1105_table *table;
2259dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
2260070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
22616666cebcSVladimir Oltean 	int rc;
22626666cebcSVladimir Oltean 
2263dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
2264dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
226589153ed6SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
226689153ed6SVladimir Oltean 					   "Cannot change VLAN filtering with active VL rules");
2267dfacc5a2SVladimir Oltean 			return -EBUSY;
2268dfacc5a2SVladimir Oltean 		}
2269dfacc5a2SVladimir Oltean 	}
2270dfacc5a2SVladimir Oltean 
2271070ca3bbSVladimir Oltean 	if (enabled) {
22726666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
227354fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
227454fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
2275070ca3bbSVladimir Oltean 	} else {
22766666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
2277070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
2278070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
2279070ca3bbSVladimir Oltean 	}
2280070ca3bbSVladimir Oltean 
228138b5beeaSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
228238b5beeaSVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
228338b5beeaSVladimir Oltean 
228438b5beeaSVladimir Oltean 		if (enabled)
228538b5beeaSVladimir Oltean 			sp->xmit_tpid = priv->info->qinq_tpid;
228638b5beeaSVladimir Oltean 		else
228738b5beeaSVladimir Oltean 			sp->xmit_tpid = ETH_P_SJA1105;
228838b5beeaSVladimir Oltean 	}
228938b5beeaSVladimir Oltean 
22900fac6aa0SVladimir Oltean 	if (priv->vlan_aware == enabled)
2291cfa36b1fSVladimir Oltean 		return 0;
2292cfa36b1fSVladimir Oltean 
22930fac6aa0SVladimir Oltean 	priv->vlan_aware = enabled;
22947f14937fSVladimir Oltean 
2295070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2296070ca3bbSVladimir Oltean 	general_params = table->entries;
2297f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
229854fa49eeSVladimir Oltean 	general_params->tpid = tpid;
229954fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2300070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
230142824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
230242824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
230342824463SVladimir Oltean 	 */
230442824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
230542824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
2306070ca3bbSVladimir Oltean 
23076d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
23082cafa72eSVladimir Oltean 	 * No VLAN filtering (or best effort) => shared VLAN learning.
23096d7c7d94SVladimir Oltean 	 *
23106d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
23116d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
23126d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
23136d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
23146d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
23156d7c7d94SVladimir Oltean 	 * forwarding decision.
23166d7c7d94SVladimir Oltean 	 *
23176d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
23186d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
23196d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
23206d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
23216d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
23226d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
23236d7c7d94SVladimir Oltean 	 * (all frames get flooded).
23246d7c7d94SVladimir Oltean 	 */
23256d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
23266d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
23270fac6aa0SVladimir Oltean 	l2_lookup_params->shared_learn = !priv->vlan_aware;
2328aaa270c6SVladimir Oltean 
23296dfd23d3SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
23306dfd23d3SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
23316dfd23d3SVladimir Oltean 			continue;
23326dfd23d3SVladimir Oltean 
23336dfd23d3SVladimir Oltean 		rc = sja1105_commit_pvid(ds, port);
2334aef31718SVladimir Oltean 		if (rc)
2335aef31718SVladimir Oltean 			return rc;
23366dfd23d3SVladimir Oltean 	}
2337aef31718SVladimir Oltean 
23382eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
23396666cebcSVladimir Oltean 	if (rc)
234089153ed6SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype");
23416666cebcSVladimir Oltean 
23420fac6aa0SVladimir Oltean 	return rc;
23436666cebcSVladimir Oltean }
23446666cebcSVladimir Oltean 
23456dfd23d3SVladimir Oltean static int sja1105_vlan_add(struct sja1105_private *priv, int port, u16 vid,
23466dfd23d3SVladimir Oltean 			    u16 flags)
23475899ee36SVladimir Oltean {
23486dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
23496dfd23d3SVladimir Oltean 	struct sja1105_table *table;
23506dfd23d3SVladimir Oltean 	int match, rc;
23515899ee36SVladimir Oltean 
23526dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
23536dfd23d3SVladimir Oltean 
23546dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
23556dfd23d3SVladimir Oltean 	if (match < 0) {
23566dfd23d3SVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
23576dfd23d3SVladimir Oltean 		if (rc)
23586dfd23d3SVladimir Oltean 			return rc;
23596dfd23d3SVladimir Oltean 		match = table->entry_count - 1;
23606dfd23d3SVladimir Oltean 	}
23616dfd23d3SVladimir Oltean 
23626dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
23636dfd23d3SVladimir Oltean 	vlan = table->entries;
23646dfd23d3SVladimir Oltean 
23656dfd23d3SVladimir Oltean 	vlan[match].type_entry = SJA1110_VLAN_D_TAG;
23666dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
23676dfd23d3SVladimir Oltean 	vlan[match].vlan_bc |= BIT(port);
23686dfd23d3SVladimir Oltean 	vlan[match].vmemb_port |= BIT(port);
23696dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
23706dfd23d3SVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
23716dfd23d3SVladimir Oltean 	else
23726dfd23d3SVladimir Oltean 		vlan[match].tag_port |= BIT(port);
23736dfd23d3SVladimir Oltean 
23746dfd23d3SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
23756dfd23d3SVladimir Oltean 					    &vlan[match], true);
23766dfd23d3SVladimir Oltean }
23776dfd23d3SVladimir Oltean 
23786dfd23d3SVladimir Oltean static int sja1105_vlan_del(struct sja1105_private *priv, int port, u16 vid)
23796dfd23d3SVladimir Oltean {
23806dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
23816dfd23d3SVladimir Oltean 	struct sja1105_table *table;
23826dfd23d3SVladimir Oltean 	bool keep = true;
23836dfd23d3SVladimir Oltean 	int match, rc;
23846dfd23d3SVladimir Oltean 
23856dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
23866dfd23d3SVladimir Oltean 
23876dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
23886dfd23d3SVladimir Oltean 	/* Can't delete a missing entry. */
23896dfd23d3SVladimir Oltean 	if (match < 0)
23905899ee36SVladimir Oltean 		return 0;
23915899ee36SVladimir Oltean 
23926dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
23936dfd23d3SVladimir Oltean 	vlan = table->entries;
23946dfd23d3SVladimir Oltean 
23956dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
23966dfd23d3SVladimir Oltean 	vlan[match].vlan_bc &= ~BIT(port);
23976dfd23d3SVladimir Oltean 	vlan[match].vmemb_port &= ~BIT(port);
23986dfd23d3SVladimir Oltean 	/* Also unset tag_port, just so we don't have a confusing bitmap
23996dfd23d3SVladimir Oltean 	 * (no practical purpose).
2400b38e659dSVladimir Oltean 	 */
24016dfd23d3SVladimir Oltean 	vlan[match].tag_port &= ~BIT(port);
2402b38e659dSVladimir Oltean 
24036dfd23d3SVladimir Oltean 	/* If there's no port left as member of this VLAN,
24046dfd23d3SVladimir Oltean 	 * it's time for it to go.
24056dfd23d3SVladimir Oltean 	 */
24066dfd23d3SVladimir Oltean 	if (!vlan[match].vmemb_port)
24076dfd23d3SVladimir Oltean 		keep = false;
24085899ee36SVladimir Oltean 
24096dfd23d3SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
24106dfd23d3SVladimir Oltean 					  &vlan[match], keep);
24116dfd23d3SVladimir Oltean 	if (rc < 0)
24126dfd23d3SVladimir Oltean 		return rc;
24135899ee36SVladimir Oltean 
24146dfd23d3SVladimir Oltean 	if (!keep)
24156dfd23d3SVladimir Oltean 		return sja1105_table_delete_entry(table, match);
24165899ee36SVladimir Oltean 
24175899ee36SVladimir Oltean 	return 0;
24185899ee36SVladimir Oltean }
24195899ee36SVladimir Oltean 
24206dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_add(struct dsa_switch *ds, int port,
242131046a5fSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan,
242231046a5fSVladimir Oltean 				   struct netlink_ext_ack *extack)
24236666cebcSVladimir Oltean {
24246666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2425884be12fSVladimir Oltean 	u16 flags = vlan->flags;
24266666cebcSVladimir Oltean 	int rc;
24276666cebcSVladimir Oltean 
24280fac6aa0SVladimir Oltean 	/* Be sure to deny alterations to the configuration done by tag_8021q.
24291958d581SVladimir Oltean 	 */
24300fac6aa0SVladimir Oltean 	if (vid_is_dsa_8021q(vlan->vid)) {
243131046a5fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
243231046a5fSVladimir Oltean 				   "Range 1024-3071 reserved for dsa_8021q operation");
24331958d581SVladimir Oltean 		return -EBUSY;
24341958d581SVladimir Oltean 	}
24351958d581SVladimir Oltean 
2436c5130029SVladimir Oltean 	/* Always install bridge VLANs as egress-tagged on CPU and DSA ports */
2437c5130029SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2438884be12fSVladimir Oltean 		flags = 0;
2439884be12fSVladimir Oltean 
2440884be12fSVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vlan->vid, flags);
24416dfd23d3SVladimir Oltean 	if (rc)
24421958d581SVladimir Oltean 		return rc;
2443ec5ae610SVladimir Oltean 
24446dfd23d3SVladimir Oltean 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
24456dfd23d3SVladimir Oltean 		priv->bridge_pvid[port] = vlan->vid;
2446ec5ae610SVladimir Oltean 
24476dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
24486666cebcSVladimir Oltean }
24496666cebcSVladimir Oltean 
24506dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port,
24516666cebcSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan)
24526666cebcSVladimir Oltean {
24536666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2454bef0746cSVladimir Oltean 	int rc;
24556666cebcSVladimir Oltean 
2456bef0746cSVladimir Oltean 	rc = sja1105_vlan_del(priv, port, vlan->vid);
2457bef0746cSVladimir Oltean 	if (rc)
2458bef0746cSVladimir Oltean 		return rc;
2459bef0746cSVladimir Oltean 
2460bef0746cSVladimir Oltean 	/* In case the pvid was deleted, make sure that untagged packets will
2461bef0746cSVladimir Oltean 	 * be dropped.
2462bef0746cSVladimir Oltean 	 */
2463bef0746cSVladimir Oltean 	return sja1105_commit_pvid(ds, port);
24646666cebcSVladimir Oltean }
24656666cebcSVladimir Oltean 
24665899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
24675899ee36SVladimir Oltean 				      u16 flags)
24685899ee36SVladimir Oltean {
24695899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
24705899ee36SVladimir Oltean 	int rc;
24715899ee36SVladimir Oltean 
24726dfd23d3SVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vid, flags);
24736dfd23d3SVladimir Oltean 	if (rc)
24745899ee36SVladimir Oltean 		return rc;
24755899ee36SVladimir Oltean 
24766dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_PVID)
24776dfd23d3SVladimir Oltean 		priv->tag_8021q_pvid[port] = vid;
24786dfd23d3SVladimir Oltean 
24796dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
24805899ee36SVladimir Oltean }
24815899ee36SVladimir Oltean 
24825899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
24835899ee36SVladimir Oltean {
24845899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
24855899ee36SVladimir Oltean 
24866dfd23d3SVladimir Oltean 	return sja1105_vlan_del(priv, port, vid);
24875899ee36SVladimir Oltean }
24885899ee36SVladimir Oltean 
24894fbc08bdSVladimir Oltean static int sja1105_prechangeupper(struct dsa_switch *ds, int port,
24904fbc08bdSVladimir Oltean 				  struct netdev_notifier_changeupper_info *info)
24914fbc08bdSVladimir Oltean {
24924fbc08bdSVladimir Oltean 	struct netlink_ext_ack *extack = info->info.extack;
24934fbc08bdSVladimir Oltean 	struct net_device *upper = info->upper_dev;
249419fa937aSVladimir Oltean 	struct dsa_switch_tree *dst = ds->dst;
249519fa937aSVladimir Oltean 	struct dsa_port *dp;
24964fbc08bdSVladimir Oltean 
24974fbc08bdSVladimir Oltean 	if (is_vlan_dev(upper)) {
24984fbc08bdSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "8021q uppers are not supported");
24994fbc08bdSVladimir Oltean 		return -EBUSY;
25004fbc08bdSVladimir Oltean 	}
25014fbc08bdSVladimir Oltean 
250219fa937aSVladimir Oltean 	if (netif_is_bridge_master(upper)) {
250319fa937aSVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
250419fa937aSVladimir Oltean 			if (dp->bridge_dev && dp->bridge_dev != upper &&
250519fa937aSVladimir Oltean 			    br_vlan_enabled(dp->bridge_dev)) {
250619fa937aSVladimir Oltean 				NL_SET_ERR_MSG_MOD(extack,
250719fa937aSVladimir Oltean 						   "Only one VLAN-aware bridge is supported");
250819fa937aSVladimir Oltean 				return -EBUSY;
250919fa937aSVladimir Oltean 			}
251019fa937aSVladimir Oltean 		}
251119fa937aSVladimir Oltean 	}
251219fa937aSVladimir Oltean 
25134fbc08bdSVladimir Oltean 	return 0;
25144fbc08bdSVladimir Oltean }
25154fbc08bdSVladimir Oltean 
25168aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
25178aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
25188aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
25198aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
25208aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
25218aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
25228aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
25238aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
25248aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
25258aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
25268aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
25278aa9ebccSVladimir Oltean  */
25288aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
25298aa9ebccSVladimir Oltean {
25308aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
25318aa9ebccSVladimir Oltean 	int rc;
25328aa9ebccSVladimir Oltean 
25335d645df9SVladimir Oltean 	rc = sja1105_parse_dt(priv);
25348aa9ebccSVladimir Oltean 	if (rc < 0) {
25358aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
25368aa9ebccSVladimir Oltean 		return rc;
25378aa9ebccSVladimir Oltean 	}
2538f5b8631cSVladimir Oltean 
2539f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
2540f5b8631cSVladimir Oltean 	 * and we can't apply them.
2541f5b8631cSVladimir Oltean 	 */
254229afb83aSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv);
2543f5b8631cSVladimir Oltean 	if (rc < 0) {
2544f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
2545f5b8631cSVladimir Oltean 		return rc;
2546f5b8631cSVladimir Oltean 	}
2547f5b8631cSVladimir Oltean 
254861c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
2549bb77f36aSVladimir Oltean 	if (rc < 0) {
2550bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
2551bb77f36aSVladimir Oltean 		return rc;
2552bb77f36aSVladimir Oltean 	}
25535a8f0974SVladimir Oltean 
25545a8f0974SVladimir Oltean 	rc = sja1105_mdiobus_register(ds);
25555a8f0974SVladimir Oltean 	if (rc < 0) {
25565a8f0974SVladimir Oltean 		dev_err(ds->dev, "Failed to register MDIO bus: %pe\n",
25575a8f0974SVladimir Oltean 			ERR_PTR(rc));
25585a8f0974SVladimir Oltean 		goto out_ptp_clock_unregister;
25595a8f0974SVladimir Oltean 	}
25605a8f0974SVladimir Oltean 
2561cb5a82d2SVladimir Oltean 	if (priv->info->disable_microcontroller) {
2562cb5a82d2SVladimir Oltean 		rc = priv->info->disable_microcontroller(priv);
2563cb5a82d2SVladimir Oltean 		if (rc < 0) {
2564cb5a82d2SVladimir Oltean 			dev_err(ds->dev,
2565cb5a82d2SVladimir Oltean 				"Failed to disable microcontroller: %pe\n",
2566cb5a82d2SVladimir Oltean 				ERR_PTR(rc));
2567cb5a82d2SVladimir Oltean 			goto out_mdiobus_unregister;
2568cb5a82d2SVladimir Oltean 		}
2569cb5a82d2SVladimir Oltean 	}
2570cb5a82d2SVladimir Oltean 
25718aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
25725d645df9SVladimir Oltean 	rc = sja1105_static_config_load(priv);
25738aa9ebccSVladimir Oltean 	if (rc < 0) {
25748aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
25755a8f0974SVladimir Oltean 		goto out_mdiobus_unregister;
25768aa9ebccSVladimir Oltean 	}
2577cb5a82d2SVladimir Oltean 
25788aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
2579cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
2580c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
25818aa9ebccSVladimir Oltean 		if (rc < 0) {
2582cb5a82d2SVladimir Oltean 			dev_err(ds->dev,
2583cb5a82d2SVladimir Oltean 				"Failed to configure MII clocking: %pe\n",
2584cb5a82d2SVladimir Oltean 				ERR_PTR(rc));
2585cec279a8SVladimir Oltean 			goto out_static_config_free;
25868aa9ebccSVladimir Oltean 		}
2587cb5a82d2SVladimir Oltean 	}
2588cb5a82d2SVladimir Oltean 
25896666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
25906666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
25916666cebcSVladimir Oltean 	 * EtherType is.
25926666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
25936666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
25946666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
25956666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
25966666cebcSVladimir Oltean 	 */
25976666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
2598884be12fSVladimir Oltean 	ds->untag_bridge_pvid = true;
2599b6ad86e6SVladimir Oltean 	/* tag_8021q has 3 bits for the VBID, and the value 0 is reserved */
2600b6ad86e6SVladimir Oltean 	ds->num_fwd_offloading_bridges = 7;
26018aa9ebccSVladimir Oltean 
26025f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
26035f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
26045f06c63bSVladimir Oltean 
2605c279c726SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
260681d45898SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
2607c279c726SVladimir Oltean 
26080a7bdbc2SVladimir Oltean 	rc = sja1105_devlink_setup(ds);
26092cafa72eSVladimir Oltean 	if (rc < 0)
2610cec279a8SVladimir Oltean 		goto out_static_config_free;
26112cafa72eSVladimir Oltean 
2612bbed0bbdSVladimir Oltean 	rtnl_lock();
2613328621f6SVladimir Oltean 	rc = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q));
2614bbed0bbdSVladimir Oltean 	rtnl_unlock();
2615cec279a8SVladimir Oltean 	if (rc)
2616cec279a8SVladimir Oltean 		goto out_devlink_teardown;
2617cec279a8SVladimir Oltean 
2618cec279a8SVladimir Oltean 	return 0;
2619cec279a8SVladimir Oltean 
2620cec279a8SVladimir Oltean out_devlink_teardown:
2621cec279a8SVladimir Oltean 	sja1105_devlink_teardown(ds);
26225a8f0974SVladimir Oltean out_mdiobus_unregister:
26235a8f0974SVladimir Oltean 	sja1105_mdiobus_unregister(ds);
2624cec279a8SVladimir Oltean out_ptp_clock_unregister:
2625cec279a8SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
2626cec279a8SVladimir Oltean out_static_config_free:
2627cec279a8SVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2628bbed0bbdSVladimir Oltean 
2629bbed0bbdSVladimir Oltean 	return rc;
2630227d07a0SVladimir Oltean }
2631227d07a0SVladimir Oltean 
2632f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
2633f3097be2SVladimir Oltean {
2634f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2635a68578c2SVladimir Oltean 	int port;
2636a68578c2SVladimir Oltean 
2637328621f6SVladimir Oltean 	rtnl_lock();
2638328621f6SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
2639328621f6SVladimir Oltean 	rtnl_unlock();
2640328621f6SVladimir Oltean 
2641542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
2642a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2643a68578c2SVladimir Oltean 
2644a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2645a68578c2SVladimir Oltean 			continue;
2646a68578c2SVladimir Oltean 
264752c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
2648a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
2649a68578c2SVladimir Oltean 	}
2650f3097be2SVladimir Oltean 
26510a7bdbc2SVladimir Oltean 	sja1105_devlink_teardown(ds);
2652a6af7763SVladimir Oltean 	sja1105_flower_teardown(ds);
2653317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
265461c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
26556cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2656f3097be2SVladimir Oltean }
2657f3097be2SVladimir Oltean 
2658a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
2659a68578c2SVladimir Oltean {
2660a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2661a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
2662a68578c2SVladimir Oltean 
2663a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2664a68578c2SVladimir Oltean 		return;
2665a68578c2SVladimir Oltean 
2666a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
2667a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
2668a68578c2SVladimir Oltean }
2669a68578c2SVladimir Oltean 
2670227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
267147ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2672227d07a0SVladimir Oltean {
2673227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2674227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2675227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2676227d07a0SVladimir Oltean 	int timeout = 10;
2677227d07a0SVladimir Oltean 	int rc;
2678227d07a0SVladimir Oltean 
2679227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2680227d07a0SVladimir Oltean 
2681227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2682227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2683227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
268447ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
268547ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2686227d07a0SVladimir Oltean 
2687227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2688227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2689227d07a0SVladimir Oltean 	if (rc < 0) {
2690227d07a0SVladimir Oltean 		kfree_skb(skb);
2691227d07a0SVladimir Oltean 		return rc;
2692227d07a0SVladimir Oltean 	}
2693227d07a0SVladimir Oltean 
2694227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
269568bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2696227d07a0SVladimir Oltean 
2697227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2698227d07a0SVladimir Oltean 	do {
2699227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2700227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2701227d07a0SVladimir Oltean 		if (rc < 0) {
2702227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2703227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2704227d07a0SVladimir Oltean 			continue;
2705227d07a0SVladimir Oltean 		}
2706227d07a0SVladimir Oltean 
2707227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2708227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2709227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2710227d07a0SVladimir Oltean 		 */
2711227d07a0SVladimir Oltean 		cpu_relax();
2712227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2713227d07a0SVladimir Oltean 
2714227d07a0SVladimir Oltean 	if (!timeout) {
2715227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2716227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
27172a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
27182a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2719227d07a0SVladimir Oltean 		 */
2720227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2721227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2722227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2723227d07a0SVladimir Oltean 	}
2724227d07a0SVladimir Oltean 
2725227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2726227d07a0SVladimir Oltean }
2727227d07a0SVladimir Oltean 
2728a68578c2SVladimir Oltean #define work_to_port(work) \
2729a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
2730a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
2731a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
2732a68578c2SVladimir Oltean 
2733227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2734227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2735227d07a0SVladimir Oltean  * lock on the bus)
2736227d07a0SVladimir Oltean  */
2737a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2738227d07a0SVladimir Oltean {
2739a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
2740a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
2741a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
2742a68578c2SVladimir Oltean 	int port = sp - priv->ports;
2743a68578c2SVladimir Oltean 	struct sk_buff *skb;
2744a68578c2SVladimir Oltean 
2745a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
2746c4b364ceSYangbo Lu 		struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
2747227d07a0SVladimir Oltean 
2748227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
2749227d07a0SVladimir Oltean 
2750a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
2751a68578c2SVladimir Oltean 
275247ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
2753a68578c2SVladimir Oltean 		if (clone)
2754a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
2755227d07a0SVladimir Oltean 
2756227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
2757a68578c2SVladimir Oltean 	}
27588aa9ebccSVladimir Oltean }
27598aa9ebccSVladimir Oltean 
27608456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
27618456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
27628456721dSVladimir Oltean  */
27638456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
27648456721dSVladimir Oltean 				   unsigned int ageing_time)
27658456721dSVladimir Oltean {
27668456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
27678456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
27688456721dSVladimir Oltean 	struct sja1105_table *table;
27698456721dSVladimir Oltean 	unsigned int maxage;
27708456721dSVladimir Oltean 
27718456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
27728456721dSVladimir Oltean 	l2_lookup_params = table->entries;
27738456721dSVladimir Oltean 
27748456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
27758456721dSVladimir Oltean 
27768456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
27778456721dSVladimir Oltean 		return 0;
27788456721dSVladimir Oltean 
27798456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
27808456721dSVladimir Oltean 
27812eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
27828456721dSVladimir Oltean }
27838456721dSVladimir Oltean 
2784c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
2785c279c726SVladimir Oltean {
2786c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2787c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2788c279c726SVladimir Oltean 
2789c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
2790c279c726SVladimir Oltean 
2791777e55e3SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2792c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
2793c279c726SVladimir Oltean 
2794c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2795c279c726SVladimir Oltean 
2796a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
2797c279c726SVladimir Oltean 		return 0;
2798c279c726SVladimir Oltean 
2799a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
2800c279c726SVladimir Oltean 
2801c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2802c279c726SVladimir Oltean }
2803c279c726SVladimir Oltean 
2804c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
2805c279c726SVladimir Oltean {
2806c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
2807c279c726SVladimir Oltean }
2808c279c726SVladimir Oltean 
2809317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2810317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2811317ab5b8SVladimir Oltean 				 void *type_data)
2812317ab5b8SVladimir Oltean {
2813317ab5b8SVladimir Oltean 	switch (type) {
2814317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2815317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
28164d752508SVladimir Oltean 	case TC_SETUP_QDISC_CBS:
28174d752508SVladimir Oltean 		return sja1105_setup_tc_cbs(ds, port, type_data);
2818317ab5b8SVladimir Oltean 	default:
2819317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2820317ab5b8SVladimir Oltean 	}
2821317ab5b8SVladimir Oltean }
2822317ab5b8SVladimir Oltean 
2823511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2824511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2825511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2826511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2827511e6ca0SVladimir Oltean  * mirroring rule that references it.
2828511e6ca0SVladimir Oltean  */
2829511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2830511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2831511e6ca0SVladimir Oltean {
2832511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2833511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2834542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2835511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2836511e6ca0SVladimir Oltean 	bool already_enabled;
2837511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2838511e6ca0SVladimir Oltean 	int rc;
2839511e6ca0SVladimir Oltean 
2840511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2841511e6ca0SVladimir Oltean 	general_params = table->entries;
2842511e6ca0SVladimir Oltean 
2843511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2844511e6ca0SVladimir Oltean 
2845542043e9SVladimir Oltean 	already_enabled = (general_params->mirr_port != ds->num_ports);
2846511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2847511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2848511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2849511e6ca0SVladimir Oltean 			general_params->mirr_port);
2850511e6ca0SVladimir Oltean 		return -EBUSY;
2851511e6ca0SVladimir Oltean 	}
2852511e6ca0SVladimir Oltean 
2853511e6ca0SVladimir Oltean 	new_mirr_port = to;
2854511e6ca0SVladimir Oltean 	if (!enabled) {
2855511e6ca0SVladimir Oltean 		bool keep = false;
2856511e6ca0SVladimir Oltean 		int port;
2857511e6ca0SVladimir Oltean 
2858511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2859542043e9SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
2860511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2861511e6ca0SVladimir Oltean 				keep = true;
2862511e6ca0SVladimir Oltean 				break;
2863511e6ca0SVladimir Oltean 			}
2864511e6ca0SVladimir Oltean 		}
2865511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2866511e6ca0SVladimir Oltean 		if (!keep)
2867542043e9SVladimir Oltean 			new_mirr_port = ds->num_ports;
2868511e6ca0SVladimir Oltean 	}
2869511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2870511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2871511e6ca0SVladimir Oltean 
2872511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2873511e6ca0SVladimir Oltean 						  0, general_params, true);
2874511e6ca0SVladimir Oltean 		if (rc < 0)
2875511e6ca0SVladimir Oltean 			return rc;
2876511e6ca0SVladimir Oltean 	}
2877511e6ca0SVladimir Oltean 
2878511e6ca0SVladimir Oltean 	if (ingress)
2879511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2880511e6ca0SVladimir Oltean 	else
2881511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2882511e6ca0SVladimir Oltean 
2883511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2884511e6ca0SVladimir Oltean 					    &mac[from], true);
2885511e6ca0SVladimir Oltean }
2886511e6ca0SVladimir Oltean 
2887511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2888511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2889511e6ca0SVladimir Oltean 			      bool ingress)
2890511e6ca0SVladimir Oltean {
2891511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2892511e6ca0SVladimir Oltean 				    ingress, true);
2893511e6ca0SVladimir Oltean }
2894511e6ca0SVladimir Oltean 
2895511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2896511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2897511e6ca0SVladimir Oltean {
2898511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2899511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2900511e6ca0SVladimir Oltean }
2901511e6ca0SVladimir Oltean 
2902a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
2903a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
2904a7cc081cSVladimir Oltean {
2905a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2906a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2907a7cc081cSVladimir Oltean 
2908a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2909a7cc081cSVladimir Oltean 
2910a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
2911a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
2912a7cc081cSVladimir Oltean 	 * bytes.
2913a7cc081cSVladimir Oltean 	 */
2914a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
2915a7cc081cSVladimir Oltean 				      1000000);
29165f035af7SPo Liu 	policing[port].smax = policer->burst;
2917a7cc081cSVladimir Oltean 
2918a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2919a7cc081cSVladimir Oltean }
2920a7cc081cSVladimir Oltean 
2921a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
2922a7cc081cSVladimir Oltean {
2923a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2924a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2925a7cc081cSVladimir Oltean 
2926a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2927a7cc081cSVladimir Oltean 
2928a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
2929a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
2930a7cc081cSVladimir Oltean 
2931a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2932a7cc081cSVladimir Oltean }
2933a7cc081cSVladimir Oltean 
29344d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port,
29354d942354SVladimir Oltean 				     bool enabled)
29364d942354SVladimir Oltean {
29374d942354SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
29384d942354SVladimir Oltean 
29394d942354SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
29404d942354SVladimir Oltean 
29414c44fc5eSVladimir Oltean 	mac[port].dyn_learn = enabled;
29424d942354SVladimir Oltean 
29435313a37bSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
29444d942354SVladimir Oltean 					    &mac[port], true);
29454d942354SVladimir Oltean }
29464d942354SVladimir Oltean 
29474d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to,
29484d942354SVladimir Oltean 					  struct switchdev_brport_flags flags)
29494d942354SVladimir Oltean {
29504d942354SVladimir Oltean 	if (flags.mask & BR_FLOOD) {
29514d942354SVladimir Oltean 		if (flags.val & BR_FLOOD)
29527f7ccdeaSVladimir Oltean 			priv->ucast_egress_floods |= BIT(to);
29534d942354SVladimir Oltean 		else
29546a5166e0SVladimir Oltean 			priv->ucast_egress_floods &= ~BIT(to);
29554d942354SVladimir Oltean 	}
29567f7ccdeaSVladimir Oltean 
29574d942354SVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD) {
29584d942354SVladimir Oltean 		if (flags.val & BR_BCAST_FLOOD)
29597f7ccdeaSVladimir Oltean 			priv->bcast_egress_floods |= BIT(to);
29604d942354SVladimir Oltean 		else
29616a5166e0SVladimir Oltean 			priv->bcast_egress_floods &= ~BIT(to);
29624d942354SVladimir Oltean 	}
29634d942354SVladimir Oltean 
29647f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
29654d942354SVladimir Oltean }
29664d942354SVladimir Oltean 
29674d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to,
29684d942354SVladimir Oltean 				    struct switchdev_brport_flags flags,
29694d942354SVladimir Oltean 				    struct netlink_ext_ack *extack)
29704d942354SVladimir Oltean {
29714d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
29724d942354SVladimir Oltean 	struct sja1105_table *table;
29734d942354SVladimir Oltean 	int match;
29744d942354SVladimir Oltean 
29754d942354SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
29764d942354SVladimir Oltean 	l2_lookup = table->entries;
29774d942354SVladimir Oltean 
29784d942354SVladimir Oltean 	for (match = 0; match < table->entry_count; match++)
29794d942354SVladimir Oltean 		if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST &&
29804d942354SVladimir Oltean 		    l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
29814d942354SVladimir Oltean 			break;
29824d942354SVladimir Oltean 
29834d942354SVladimir Oltean 	if (match == table->entry_count) {
29844d942354SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
29854d942354SVladimir Oltean 				   "Could not find FDB entry for unknown multicast");
29864d942354SVladimir Oltean 		return -ENOSPC;
29874d942354SVladimir Oltean 	}
29884d942354SVladimir Oltean 
29894d942354SVladimir Oltean 	if (flags.val & BR_MCAST_FLOOD)
29904d942354SVladimir Oltean 		l2_lookup[match].destports |= BIT(to);
29914d942354SVladimir Oltean 	else
29924d942354SVladimir Oltean 		l2_lookup[match].destports &= ~BIT(to);
29934d942354SVladimir Oltean 
29944d942354SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
29954d942354SVladimir Oltean 					    l2_lookup[match].index,
29964d942354SVladimir Oltean 					    &l2_lookup[match],
29974d942354SVladimir Oltean 					    true);
29984d942354SVladimir Oltean }
29994d942354SVladimir Oltean 
30004d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port,
30014d942354SVladimir Oltean 					 struct switchdev_brport_flags flags,
30024d942354SVladimir Oltean 					 struct netlink_ext_ack *extack)
30034d942354SVladimir Oltean {
30044d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
30054d942354SVladimir Oltean 
30064d942354SVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
30074d942354SVladimir Oltean 			   BR_BCAST_FLOOD))
30084d942354SVladimir Oltean 		return -EINVAL;
30094d942354SVladimir Oltean 
30104d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) &&
30114d942354SVladimir Oltean 	    !priv->info->can_limit_mcast_flood) {
30124d942354SVladimir Oltean 		bool multicast = !!(flags.val & BR_MCAST_FLOOD);
30134d942354SVladimir Oltean 		bool unicast = !!(flags.val & BR_FLOOD);
30144d942354SVladimir Oltean 
30154d942354SVladimir Oltean 		if (unicast != multicast) {
30164d942354SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
30174d942354SVladimir Oltean 					   "This chip cannot configure multicast flooding independently of unicast");
30184d942354SVladimir Oltean 			return -EINVAL;
30194d942354SVladimir Oltean 		}
30204d942354SVladimir Oltean 	}
30214d942354SVladimir Oltean 
30224d942354SVladimir Oltean 	return 0;
30234d942354SVladimir Oltean }
30244d942354SVladimir Oltean 
30254d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port,
30264d942354SVladimir Oltean 				     struct switchdev_brport_flags flags,
30274d942354SVladimir Oltean 				     struct netlink_ext_ack *extack)
30284d942354SVladimir Oltean {
30294d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
30304d942354SVladimir Oltean 	int rc;
30314d942354SVladimir Oltean 
30324d942354SVladimir Oltean 	if (flags.mask & BR_LEARNING) {
30334d942354SVladimir Oltean 		bool learn_ena = !!(flags.val & BR_LEARNING);
30344d942354SVladimir Oltean 
30354d942354SVladimir Oltean 		rc = sja1105_port_set_learning(priv, port, learn_ena);
30364d942354SVladimir Oltean 		if (rc)
30374d942354SVladimir Oltean 			return rc;
30384d942354SVladimir Oltean 	}
30394d942354SVladimir Oltean 
30404d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) {
30414d942354SVladimir Oltean 		rc = sja1105_port_ucast_bcast_flood(priv, port, flags);
30424d942354SVladimir Oltean 		if (rc)
30434d942354SVladimir Oltean 			return rc;
30444d942354SVladimir Oltean 	}
30454d942354SVladimir Oltean 
30464d942354SVladimir Oltean 	/* For chips that can't offload BR_MCAST_FLOOD independently, there
30474d942354SVladimir Oltean 	 * is nothing to do here, we ensured the configuration is in sync by
30484d942354SVladimir Oltean 	 * offloading BR_FLOOD.
30494d942354SVladimir Oltean 	 */
30504d942354SVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) {
30514d942354SVladimir Oltean 		rc = sja1105_port_mcast_flood(priv, port, flags,
30524d942354SVladimir Oltean 					      extack);
30534d942354SVladimir Oltean 		if (rc)
30544d942354SVladimir Oltean 			return rc;
30554d942354SVladimir Oltean 	}
30564d942354SVladimir Oltean 
30574d942354SVladimir Oltean 	return 0;
30584d942354SVladimir Oltean }
30594d942354SVladimir Oltean 
30608aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
30618aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
30628aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
3063f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
30648456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
3065c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
3066c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
3067ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
3068af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
30698400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
30708400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
307152c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
307252c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
307352c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
3074bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
3075a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
3076291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
3077291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
3078291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
3079*5126ec72SVladimir Oltean 	.port_fast_age		= sja1105_fast_age,
30808aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
30818aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
30824d942354SVladimir Oltean 	.port_pre_bridge_flags	= sja1105_port_pre_bridge_flags,
30834d942354SVladimir Oltean 	.port_bridge_flags	= sja1105_port_bridge_flags,
3084640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
30856666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
30866dfd23d3SVladimir Oltean 	.port_vlan_add		= sja1105_bridge_vlan_add,
30876dfd23d3SVladimir Oltean 	.port_vlan_del		= sja1105_bridge_vlan_del,
3088291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
3089291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
3090a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
3091a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
3092f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
309347ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
3094317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
3095511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
3096511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
3097a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
3098a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
3099a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
3100a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
3101834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
3102ff4cf8eaSVladimir Oltean 	.devlink_info_get	= sja1105_devlink_info_get,
31035da11eb4SVladimir Oltean 	.tag_8021q_vlan_add	= sja1105_dsa_8021q_vlan_add,
31045da11eb4SVladimir Oltean 	.tag_8021q_vlan_del	= sja1105_dsa_8021q_vlan_del,
31054fbc08bdSVladimir Oltean 	.port_prechangeupper	= sja1105_prechangeupper,
3106b6ad86e6SVladimir Oltean 	.port_bridge_tx_fwd_offload = dsa_tag_8021q_bridge_tx_fwd_offload,
3107b6ad86e6SVladimir Oltean 	.port_bridge_tx_fwd_unoffload = dsa_tag_8021q_bridge_tx_fwd_unoffload,
31088aa9ebccSVladimir Oltean };
31098aa9ebccSVladimir Oltean 
31100b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[];
31110b0e2997SVladimir Oltean 
31128aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
31138aa9ebccSVladimir Oltean {
31148aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
31158aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
31168aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
31170b0e2997SVladimir Oltean 	const struct of_device_id *match;
3118dff79620SVladimir Oltean 	u32 device_id;
31198aa9ebccSVladimir Oltean 	u64 part_no;
31208aa9ebccSVladimir Oltean 	int rc;
31218aa9ebccSVladimir Oltean 
312234d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
312334d76e9fSVladimir Oltean 			      NULL);
31248aa9ebccSVladimir Oltean 	if (rc < 0)
31258aa9ebccSVladimir Oltean 		return rc;
31268aa9ebccSVladimir Oltean 
31271bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
31281bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
31298aa9ebccSVladimir Oltean 	if (rc < 0)
31308aa9ebccSVladimir Oltean 		return rc;
31318aa9ebccSVladimir Oltean 
31328aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
31338aa9ebccSVladimir Oltean 
31345978fac0SNathan Chancellor 	for (match = sja1105_dt_ids; match->compatible[0]; match++) {
31350b0e2997SVladimir Oltean 		const struct sja1105_info *info = match->data;
31360b0e2997SVladimir Oltean 
31370b0e2997SVladimir Oltean 		/* Is what's been probed in our match table at all? */
31380b0e2997SVladimir Oltean 		if (info->device_id != device_id || info->part_no != part_no)
31390b0e2997SVladimir Oltean 			continue;
31400b0e2997SVladimir Oltean 
31410b0e2997SVladimir Oltean 		/* But is it what's in the device tree? */
31420b0e2997SVladimir Oltean 		if (priv->info->device_id != device_id ||
31430b0e2997SVladimir Oltean 		    priv->info->part_no != part_no) {
31440b0e2997SVladimir Oltean 			dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
31450b0e2997SVladimir Oltean 				 priv->info->name, info->name);
31460b0e2997SVladimir Oltean 			/* It isn't. No problem, pick that up. */
31470b0e2997SVladimir Oltean 			priv->info = info;
31488aa9ebccSVladimir Oltean 		}
31498aa9ebccSVladimir Oltean 
31508aa9ebccSVladimir Oltean 		return 0;
31518aa9ebccSVladimir Oltean 	}
31528aa9ebccSVladimir Oltean 
31530b0e2997SVladimir Oltean 	dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
31540b0e2997SVladimir Oltean 		device_id, part_no);
31550b0e2997SVladimir Oltean 
31560b0e2997SVladimir Oltean 	return -ENODEV;
31570b0e2997SVladimir Oltean }
31580b0e2997SVladimir Oltean 
31598aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
31608aa9ebccSVladimir Oltean {
3161844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
31628aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
31638aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
3164718bad0eSVladimir Oltean 	size_t max_xfer, max_msg;
31658aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
3166a68578c2SVladimir Oltean 	int rc, port;
31678aa9ebccSVladimir Oltean 
31688aa9ebccSVladimir Oltean 	if (!dev->of_node) {
31698aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
31708aa9ebccSVladimir Oltean 		return -EINVAL;
31718aa9ebccSVladimir Oltean 	}
31728aa9ebccSVladimir Oltean 
31738aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
31748aa9ebccSVladimir Oltean 	if (!priv)
31758aa9ebccSVladimir Oltean 		return -ENOMEM;
31768aa9ebccSVladimir Oltean 
31778aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
31788aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
31798aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
31808aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
31818aa9ebccSVladimir Oltean 	else
31828aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
31838aa9ebccSVladimir Oltean 
31848aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
31858aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
31868aa9ebccSVladimir Oltean 	 */
31878aa9ebccSVladimir Oltean 	priv->spidev = spi;
31888aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
31898aa9ebccSVladimir Oltean 
31908aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
31918aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
31928aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
31938aa9ebccSVladimir Oltean 	if (rc < 0) {
31948aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
31958aa9ebccSVladimir Oltean 		return rc;
31968aa9ebccSVladimir Oltean 	}
31978aa9ebccSVladimir Oltean 
3198718bad0eSVladimir Oltean 	/* In sja1105_xfer, we send spi_messages composed of two spi_transfers:
3199718bad0eSVladimir Oltean 	 * a small one for the message header and another one for the current
3200718bad0eSVladimir Oltean 	 * chunk of the packed buffer.
3201718bad0eSVladimir Oltean 	 * Check that the restrictions imposed by the SPI controller are
3202718bad0eSVladimir Oltean 	 * respected: the chunk buffer is smaller than the max transfer size,
3203718bad0eSVladimir Oltean 	 * and the total length of the chunk plus its message header is smaller
3204718bad0eSVladimir Oltean 	 * than the max message size.
3205718bad0eSVladimir Oltean 	 * We do that during probe time since the maximum transfer size is a
3206718bad0eSVladimir Oltean 	 * runtime invariant.
3207718bad0eSVladimir Oltean 	 */
3208718bad0eSVladimir Oltean 	max_xfer = spi_max_transfer_size(spi);
3209718bad0eSVladimir Oltean 	max_msg = spi_max_message_size(spi);
3210718bad0eSVladimir Oltean 
3211718bad0eSVladimir Oltean 	/* We need to send at least one 64-bit word of SPI payload per message
3212718bad0eSVladimir Oltean 	 * in order to be able to make useful progress.
3213718bad0eSVladimir Oltean 	 */
3214718bad0eSVladimir Oltean 	if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) {
3215718bad0eSVladimir Oltean 		dev_err(dev, "SPI master cannot send large enough buffers, aborting\n");
3216718bad0eSVladimir Oltean 		return -EINVAL;
3217718bad0eSVladimir Oltean 	}
3218718bad0eSVladimir Oltean 
3219718bad0eSVladimir Oltean 	priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN;
3220718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_xfer)
3221718bad0eSVladimir Oltean 		priv->max_xfer_len = max_xfer;
3222718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER)
3223718bad0eSVladimir Oltean 		priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER;
3224718bad0eSVladimir Oltean 
32258aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
32268aa9ebccSVladimir Oltean 
32278aa9ebccSVladimir Oltean 	/* Detect hardware device */
32288aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
32298aa9ebccSVladimir Oltean 	if (rc < 0) {
32308aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
32318aa9ebccSVladimir Oltean 		return rc;
32328aa9ebccSVladimir Oltean 	}
32338aa9ebccSVladimir Oltean 
32348aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
32358aa9ebccSVladimir Oltean 
32367e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
32378aa9ebccSVladimir Oltean 	if (!ds)
32388aa9ebccSVladimir Oltean 		return -ENOMEM;
32398aa9ebccSVladimir Oltean 
32407e99e347SVivien Didelot 	ds->dev = dev;
32413e77e59bSVladimir Oltean 	ds->num_ports = priv->info->num_ports;
32428aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
32438aa9ebccSVladimir Oltean 	ds->priv = priv;
32448aa9ebccSVladimir Oltean 	priv->ds = ds;
32458aa9ebccSVladimir Oltean 
3246844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
3247844d7edcSVladimir Oltean 
3248d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
3249d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
3250d5a619bfSVivien Didelot 
3251d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
3252a6af7763SVladimir Oltean 	sja1105_flower_setup(ds);
3253d5a619bfSVivien Didelot 
3254d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
3255d5a619bfSVivien Didelot 	if (rc)
3256328621f6SVladimir Oltean 		return rc;
3257d5a619bfSVivien Didelot 
32584d752508SVladimir Oltean 	if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
32594d752508SVladimir Oltean 		priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
32604d752508SVladimir Oltean 					 sizeof(struct sja1105_cbs_entry),
32614d752508SVladimir Oltean 					 GFP_KERNEL);
3262dc596e3fSVladimir Oltean 		if (!priv->cbs) {
3263dc596e3fSVladimir Oltean 			rc = -ENOMEM;
3264dc596e3fSVladimir Oltean 			goto out_unregister_switch;
3265dc596e3fSVladimir Oltean 		}
32664d752508SVladimir Oltean 	}
32674d752508SVladimir Oltean 
3268227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
3269542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
3270a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3271a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
3272a68578c2SVladimir Oltean 		struct net_device *slave;
3273227d07a0SVladimir Oltean 
3274a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3275a68578c2SVladimir Oltean 			continue;
3276a68578c2SVladimir Oltean 
3277a68578c2SVladimir Oltean 		dp->priv = sp;
3278a68578c2SVladimir Oltean 		sp->dp = dp;
3279844d7edcSVladimir Oltean 		sp->data = tagger_data;
3280a68578c2SVladimir Oltean 		slave = dp->slave;
3281a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
3282a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
3283a68578c2SVladimir Oltean 							slave->name);
3284a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
3285a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
3286a68578c2SVladimir Oltean 			dev_err(ds->dev,
3287a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
3288a68578c2SVladimir Oltean 				rc);
3289dc596e3fSVladimir Oltean 			goto out_destroy_workers;
3290a68578c2SVladimir Oltean 		}
3291a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
329238b5beeaSVladimir Oltean 		sp->xmit_tpid = ETH_P_SJA1105;
3293227d07a0SVladimir Oltean 	}
3294227d07a0SVladimir Oltean 
3295d5a619bfSVivien Didelot 	return 0;
3296dc596e3fSVladimir Oltean 
3297dc596e3fSVladimir Oltean out_destroy_workers:
3298a68578c2SVladimir Oltean 	while (port-- > 0) {
3299a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3300a68578c2SVladimir Oltean 
3301a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3302a68578c2SVladimir Oltean 			continue;
3303a68578c2SVladimir Oltean 
3304a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
3305a68578c2SVladimir Oltean 	}
3306dc596e3fSVladimir Oltean 
3307dc596e3fSVladimir Oltean out_unregister_switch:
3308dc596e3fSVladimir Oltean 	dsa_unregister_switch(ds);
3309dc596e3fSVladimir Oltean 
3310a68578c2SVladimir Oltean 	return rc;
33118aa9ebccSVladimir Oltean }
33128aa9ebccSVladimir Oltean 
33138aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
33148aa9ebccSVladimir Oltean {
33158aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
3316cedf4670SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
33178aa9ebccSVladimir Oltean 
3318cedf4670SVladimir Oltean 	dsa_unregister_switch(ds);
3319cedf4670SVladimir Oltean 
33208aa9ebccSVladimir Oltean 	return 0;
33218aa9ebccSVladimir Oltean }
33228aa9ebccSVladimir Oltean 
33238aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
33248aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
33258aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
33268aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
33278aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
33288aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
33298aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
33303e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110a", .data = &sja1110a_info },
33313e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110b", .data = &sja1110b_info },
33323e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110c", .data = &sja1110c_info },
33333e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110d", .data = &sja1110d_info },
33348aa9ebccSVladimir Oltean 	{ /* sentinel */ },
33358aa9ebccSVladimir Oltean };
33368aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
33378aa9ebccSVladimir Oltean 
33388aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
33398aa9ebccSVladimir Oltean 	.driver = {
33408aa9ebccSVladimir Oltean 		.name  = "sja1105",
33418aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
33428aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
33438aa9ebccSVladimir Oltean 	},
33448aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
33458aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
33468aa9ebccSVladimir Oltean };
33478aa9ebccSVladimir Oltean 
33488aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
33498aa9ebccSVladimir Oltean 
33508aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
33518aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
33528aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
33538aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
3354