xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 3fa212707b8e6026cea6a92faea87f556e0cba9b)
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;
1798aa9ebccSVladimir Oltean 	int i;
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 
198542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1998aa9ebccSVladimir Oltean 		mac[i] = default_mac;
200b0b33b04SVladimir Oltean 
201b0b33b04SVladimir Oltean 		/* Let sja1105_bridge_stp_state_set() keep address learning
202b0b33b04SVladimir Oltean 		 * enabled for the CPU port.
203640f763fSVladimir Oltean 		 */
204b0b33b04SVladimir Oltean 		if (dsa_is_cpu_port(ds, i))
205b0b33b04SVladimir Oltean 			priv->learn_ena |= BIT(i);
206640f763fSVladimir Oltean 	}
2078aa9ebccSVladimir Oltean 
2088aa9ebccSVladimir Oltean 	return 0;
2098aa9ebccSVladimir Oltean }
2108aa9ebccSVladimir Oltean 
2115d645df9SVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv)
2128aa9ebccSVladimir Oltean {
2138aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
2148aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
215542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2168aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2178aa9ebccSVladimir Oltean 	int i;
2188aa9ebccSVladimir Oltean 
2198aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
2208aa9ebccSVladimir Oltean 
2218aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
2228aa9ebccSVladimir Oltean 	if (table->entry_count) {
2238aa9ebccSVladimir Oltean 		kfree(table->entries);
2248aa9ebccSVladimir Oltean 		table->entry_count = 0;
2258aa9ebccSVladimir Oltean 	}
2268aa9ebccSVladimir Oltean 
227fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
2288aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2298aa9ebccSVladimir Oltean 	if (!table->entries)
2308aa9ebccSVladimir Oltean 		return -ENOMEM;
2318aa9ebccSVladimir Oltean 
2321fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
233fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
2348aa9ebccSVladimir Oltean 
2358aa9ebccSVladimir Oltean 	mii = table->entries;
2368aa9ebccSVladimir Oltean 
237542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
2385d645df9SVladimir Oltean 		sja1105_mii_role_t role = XMII_MAC;
2395d645df9SVladimir Oltean 
240ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
241ee9d0cb6SVladimir Oltean 			continue;
242ee9d0cb6SVladimir Oltean 
2435d645df9SVladimir Oltean 		switch (priv->phy_mode[i]) {
2445a8f0974SVladimir Oltean 		case PHY_INTERFACE_MODE_INTERNAL:
2455a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_NO_PHY)
2465a8f0974SVladimir Oltean 				goto unsupported;
2475a8f0974SVladimir Oltean 
2485a8f0974SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
2495a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_PHY_BASE_TX)
2505a8f0974SVladimir Oltean 				mii->special[i] = true;
2515a8f0974SVladimir Oltean 
2525a8f0974SVladimir Oltean 			break;
2535d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVMII:
2545d645df9SVladimir Oltean 			role = XMII_PHY;
2555d645df9SVladimir Oltean 			fallthrough;
2568aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
25791a05078SVladimir Oltean 			if (!priv->info->supports_mii[i])
25891a05078SVladimir Oltean 				goto unsupported;
25991a05078SVladimir Oltean 
2608aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
2618aa9ebccSVladimir Oltean 			break;
2625d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVRMII:
2635d645df9SVladimir Oltean 			role = XMII_PHY;
2645d645df9SVladimir Oltean 			fallthrough;
2658aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
26691a05078SVladimir Oltean 			if (!priv->info->supports_rmii[i])
26791a05078SVladimir Oltean 				goto unsupported;
26891a05078SVladimir Oltean 
2698aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
2708aa9ebccSVladimir Oltean 			break;
2718aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
2728aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
2738aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
2748aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
27591a05078SVladimir Oltean 			if (!priv->info->supports_rgmii[i])
27691a05078SVladimir Oltean 				goto unsupported;
27791a05078SVladimir Oltean 
2788aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
2798aa9ebccSVladimir Oltean 			break;
280ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
28191a05078SVladimir Oltean 			if (!priv->info->supports_sgmii[i])
28291a05078SVladimir Oltean 				goto unsupported;
28391a05078SVladimir Oltean 
284ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
285ece578bcSVladimir Oltean 			mii->special[i] = true;
286ffe10e67SVladimir Oltean 			break;
28791a05078SVladimir Oltean 		case PHY_INTERFACE_MODE_2500BASEX:
28891a05078SVladimir Oltean 			if (!priv->info->supports_2500basex[i])
28991a05078SVladimir Oltean 				goto unsupported;
29091a05078SVladimir Oltean 
29191a05078SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
292ece578bcSVladimir Oltean 			mii->special[i] = true;
29391a05078SVladimir Oltean 			break;
29491a05078SVladimir Oltean unsupported:
2958aa9ebccSVladimir Oltean 		default:
29691a05078SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d!\n",
2975d645df9SVladimir Oltean 				phy_modes(priv->phy_mode[i]), i);
2986729188dSVladimir Oltean 			return -EINVAL;
2998aa9ebccSVladimir Oltean 		}
3008aa9ebccSVladimir Oltean 
3015d645df9SVladimir Oltean 		mii->phy_mac[i] = role;
3028aa9ebccSVladimir Oltean 	}
3038aa9ebccSVladimir Oltean 	return 0;
3048aa9ebccSVladimir Oltean }
3058aa9ebccSVladimir Oltean 
3068aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
3078aa9ebccSVladimir Oltean {
3084d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
3098aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3104d942354SVladimir Oltean 	int port;
3118aa9ebccSVladimir Oltean 
3128aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
3138aa9ebccSVladimir Oltean 
3144d942354SVladimir Oltean 	/* We only populate the FDB table through dynamic L2 Address Lookup
3154d942354SVladimir Oltean 	 * entries, except for a special entry at the end which is a catch-all
3164d942354SVladimir Oltean 	 * for unknown multicast and will be used to control flooding domain.
317291d1e72SVladimir Oltean 	 */
3188aa9ebccSVladimir Oltean 	if (table->entry_count) {
3198aa9ebccSVladimir Oltean 		kfree(table->entries);
3208aa9ebccSVladimir Oltean 		table->entry_count = 0;
3218aa9ebccSVladimir Oltean 	}
3224d942354SVladimir Oltean 
3234d942354SVladimir Oltean 	if (!priv->info->can_limit_mcast_flood)
3244d942354SVladimir Oltean 		return 0;
3254d942354SVladimir Oltean 
3264d942354SVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
3274d942354SVladimir Oltean 				 GFP_KERNEL);
3284d942354SVladimir Oltean 	if (!table->entries)
3294d942354SVladimir Oltean 		return -ENOMEM;
3304d942354SVladimir Oltean 
3314d942354SVladimir Oltean 	table->entry_count = 1;
3324d942354SVladimir Oltean 	l2_lookup = table->entries;
3334d942354SVladimir Oltean 
3344d942354SVladimir Oltean 	/* All L2 multicast addresses have an odd first octet */
3354d942354SVladimir Oltean 	l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST;
3364d942354SVladimir Oltean 	l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST;
3374d942354SVladimir Oltean 	l2_lookup[0].lockeds = true;
3384d942354SVladimir Oltean 	l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1;
3394d942354SVladimir Oltean 
3404d942354SVladimir Oltean 	/* Flood multicast to every port by default */
3414d942354SVladimir Oltean 	for (port = 0; port < priv->ds->num_ports; port++)
3424d942354SVladimir Oltean 		if (!dsa_is_unused_port(priv->ds, port))
3434d942354SVladimir Oltean 			l2_lookup[0].destports |= BIT(port);
3444d942354SVladimir Oltean 
3458aa9ebccSVladimir Oltean 	return 0;
3468aa9ebccSVladimir Oltean }
3478aa9ebccSVladimir Oltean 
3488aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
3498aa9ebccSVladimir Oltean {
3508aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
3518456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
3528456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
3538aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
3548aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
3551da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
3561da73821SVladimir Oltean 		.start_dynspc = 0,
3578aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
3588aa9ebccSVladimir Oltean 		.poly = 0x97,
3598aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
3608aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
3618aa9ebccSVladimir Oltean 		 */
3626d7c7d94SVladimir Oltean 		.shared_learn = true,
3638aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
3648aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
3658aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
3668aa9ebccSVladimir Oltean 		 */
3678aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
3688aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
3698aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
3708aa9ebccSVladimir Oltean 		 */
3718aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
3721da73821SVladimir Oltean 		/* P/Q/R/S only */
3731da73821SVladimir Oltean 		.use_static = true,
3741da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
3751da73821SVladimir Oltean 		 * dynamic FDB entries
3761da73821SVladimir Oltean 		 */
3771da73821SVladimir Oltean 		.owr_dyn = true,
3781da73821SVladimir Oltean 		.drpnolearn = true,
3798aa9ebccSVladimir Oltean 	};
380542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
381f238fef1SVladimir Oltean 	int port, num_used_ports = 0;
382542043e9SVladimir Oltean 	struct sja1105_table *table;
383542043e9SVladimir Oltean 	u64 max_fdb_entries;
384542043e9SVladimir Oltean 
385542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++)
386f238fef1SVladimir Oltean 		if (!dsa_is_unused_port(ds, port))
387f238fef1SVladimir Oltean 			num_used_ports++;
388f238fef1SVladimir Oltean 
389f238fef1SVladimir Oltean 	max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports;
390f238fef1SVladimir Oltean 
391f238fef1SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
392f238fef1SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
393f238fef1SVladimir Oltean 			continue;
394f238fef1SVladimir Oltean 
395542043e9SVladimir Oltean 		default_l2_lookup_params.maxaddrp[port] = max_fdb_entries;
396f238fef1SVladimir Oltean 	}
3978aa9ebccSVladimir Oltean 
3988aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
3998aa9ebccSVladimir Oltean 
4008aa9ebccSVladimir Oltean 	if (table->entry_count) {
4018aa9ebccSVladimir Oltean 		kfree(table->entries);
4028aa9ebccSVladimir Oltean 		table->entry_count = 0;
4038aa9ebccSVladimir Oltean 	}
4048aa9ebccSVladimir Oltean 
405fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4068aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4078aa9ebccSVladimir Oltean 	if (!table->entries)
4088aa9ebccSVladimir Oltean 		return -ENOMEM;
4098aa9ebccSVladimir Oltean 
410fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
4118aa9ebccSVladimir Oltean 
4128aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4138aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
4148aa9ebccSVladimir Oltean 				default_l2_lookup_params;
4158aa9ebccSVladimir Oltean 
4168aa9ebccSVladimir Oltean 	return 0;
4178aa9ebccSVladimir Oltean }
4188aa9ebccSVladimir Oltean 
419ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU
420ed040abcSVladimir Oltean  * using management routes (e.g. STP, PTP) as opposed to tag_8021q.
421ed040abcSVladimir Oltean  * All DT-defined ports are members of this VLAN, and there are no
422ed040abcSVladimir Oltean  * restrictions on forwarding (since the CPU selects the destination).
423ed040abcSVladimir Oltean  * Frames from this VLAN will always be transmitted as untagged, and
424ed040abcSVladimir Oltean  * neither the bridge nor the 8021q module cannot create this VLAN ID.
425ed040abcSVladimir Oltean  */
4268aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
4278aa9ebccSVladimir Oltean {
4288aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4298aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
4303e77e59bSVladimir Oltean 		.type_entry = SJA1110_VLAN_D_TAG,
4318aa9ebccSVladimir Oltean 		.ving_mirr = 0,
4328aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
4338aa9ebccSVladimir Oltean 		.vmemb_port = 0,
4348aa9ebccSVladimir Oltean 		.vlan_bc = 0,
4358aa9ebccSVladimir Oltean 		.tag_port = 0,
436ed040abcSVladimir Oltean 		.vlanid = SJA1105_DEFAULT_VLAN,
4378aa9ebccSVladimir Oltean 	};
438ec5ae610SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
439ec5ae610SVladimir Oltean 	int port;
4408aa9ebccSVladimir Oltean 
4418aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
4428aa9ebccSVladimir Oltean 
4438aa9ebccSVladimir Oltean 	if (table->entry_count) {
4448aa9ebccSVladimir Oltean 		kfree(table->entries);
4458aa9ebccSVladimir Oltean 		table->entry_count = 0;
4468aa9ebccSVladimir Oltean 	}
4478aa9ebccSVladimir Oltean 
448c75857b0SZheng Yongjun 	table->entries = kzalloc(table->ops->unpacked_entry_size,
4498aa9ebccSVladimir Oltean 				 GFP_KERNEL);
4508aa9ebccSVladimir Oltean 	if (!table->entries)
4518aa9ebccSVladimir Oltean 		return -ENOMEM;
4528aa9ebccSVladimir Oltean 
4538aa9ebccSVladimir Oltean 	table->entry_count = 1;
4548aa9ebccSVladimir Oltean 
455ec5ae610SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
456ec5ae610SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
457ec5ae610SVladimir Oltean 			continue;
458ec5ae610SVladimir Oltean 
459ec5ae610SVladimir Oltean 		pvid.vmemb_port |= BIT(port);
460ec5ae610SVladimir Oltean 		pvid.vlan_bc |= BIT(port);
461ec5ae610SVladimir Oltean 		pvid.tag_port &= ~BIT(port);
462ec5ae610SVladimir Oltean 
4636dfd23d3SVladimir Oltean 		if (dsa_is_cpu_port(ds, port)) {
4646dfd23d3SVladimir Oltean 			priv->tag_8021q_pvid[port] = SJA1105_DEFAULT_VLAN;
4656dfd23d3SVladimir Oltean 			priv->bridge_pvid[port] = SJA1105_DEFAULT_VLAN;
4666dfd23d3SVladimir Oltean 		}
4678aa9ebccSVladimir Oltean 	}
4688aa9ebccSVladimir Oltean 
4698aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
4708aa9ebccSVladimir Oltean 	return 0;
4718aa9ebccSVladimir Oltean }
4728aa9ebccSVladimir Oltean 
4738aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
4748aa9ebccSVladimir Oltean {
4758aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
476542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
4778aa9ebccSVladimir Oltean 	struct sja1105_table *table;
478*3fa21270SVladimir Oltean 	int port, tc;
479*3fa21270SVladimir Oltean 	int from, to;
4808aa9ebccSVladimir Oltean 
4818aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
4828aa9ebccSVladimir Oltean 
4838aa9ebccSVladimir Oltean 	if (table->entry_count) {
4848aa9ebccSVladimir Oltean 		kfree(table->entries);
4858aa9ebccSVladimir Oltean 		table->entry_count = 0;
4868aa9ebccSVladimir Oltean 	}
4878aa9ebccSVladimir Oltean 
488fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4898aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4908aa9ebccSVladimir Oltean 	if (!table->entries)
4918aa9ebccSVladimir Oltean 		return -ENOMEM;
4928aa9ebccSVladimir Oltean 
493fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
4948aa9ebccSVladimir Oltean 
4958aa9ebccSVladimir Oltean 	l2fwd = table->entries;
4968aa9ebccSVladimir Oltean 
497*3fa21270SVladimir Oltean 	/* First 5 entries in the L2 Forwarding Table define the forwarding
498*3fa21270SVladimir Oltean 	 * rules and the VLAN PCP to ingress queue mapping.
499*3fa21270SVladimir Oltean 	 * Set up the ingress queue mapping first.
5007f7ccdeaSVladimir Oltean 	 */
501*3fa21270SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
502*3fa21270SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
5038aa9ebccSVladimir Oltean 			continue;
5048aa9ebccSVladimir Oltean 
505*3fa21270SVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
506*3fa21270SVladimir Oltean 			l2fwd[port].vlan_pmap[tc] = tc;
507*3fa21270SVladimir Oltean 	}
5084d942354SVladimir Oltean 
509*3fa21270SVladimir Oltean 	/* Then manage the forwarding domain for user ports. These can forward
510*3fa21270SVladimir Oltean 	 * only to the always-on domain (CPU port and DSA links)
511*3fa21270SVladimir Oltean 	 */
512*3fa21270SVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
513*3fa21270SVladimir Oltean 		if (!dsa_is_user_port(ds, from))
514*3fa21270SVladimir Oltean 			continue;
5154d942354SVladimir Oltean 
516*3fa21270SVladimir Oltean 		for (to = 0; to < ds->num_ports; to++) {
517*3fa21270SVladimir Oltean 			if (!dsa_is_cpu_port(ds, to) &&
518*3fa21270SVladimir Oltean 			    !dsa_is_dsa_port(ds, to))
519*3fa21270SVladimir Oltean 				continue;
520*3fa21270SVladimir Oltean 
521*3fa21270SVladimir Oltean 			l2fwd[from].bc_domain |= BIT(to);
522*3fa21270SVladimir Oltean 			l2fwd[from].fl_domain |= BIT(to);
523*3fa21270SVladimir Oltean 
524*3fa21270SVladimir Oltean 			sja1105_port_allow_traffic(l2fwd, from, to, true);
525*3fa21270SVladimir Oltean 		}
526*3fa21270SVladimir Oltean 	}
527*3fa21270SVladimir Oltean 
528*3fa21270SVladimir Oltean 	/* Then manage the forwarding domain for DSA links and CPU ports (the
529*3fa21270SVladimir Oltean 	 * always-on domain). These can send packets to any enabled port except
530*3fa21270SVladimir Oltean 	 * themselves.
531*3fa21270SVladimir Oltean 	 */
532*3fa21270SVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
533*3fa21270SVladimir Oltean 		if (!dsa_is_cpu_port(ds, from) && !dsa_is_dsa_port(ds, from))
534*3fa21270SVladimir Oltean 			continue;
535*3fa21270SVladimir Oltean 
536*3fa21270SVladimir Oltean 		for (to = 0; to < ds->num_ports; to++) {
537*3fa21270SVladimir Oltean 			if (dsa_is_unused_port(ds, to))
538*3fa21270SVladimir Oltean 				continue;
539*3fa21270SVladimir Oltean 
540*3fa21270SVladimir Oltean 			if (from == to)
541*3fa21270SVladimir Oltean 				continue;
542*3fa21270SVladimir Oltean 
543*3fa21270SVladimir Oltean 			l2fwd[from].bc_domain |= BIT(to);
544*3fa21270SVladimir Oltean 			l2fwd[from].fl_domain |= BIT(to);
545*3fa21270SVladimir Oltean 
546*3fa21270SVladimir Oltean 			sja1105_port_allow_traffic(l2fwd, from, to, true);
547*3fa21270SVladimir Oltean 		}
548*3fa21270SVladimir Oltean 	}
549*3fa21270SVladimir Oltean 
550*3fa21270SVladimir Oltean 	/* Finally, manage the egress flooding domain. All ports start up with
551*3fa21270SVladimir Oltean 	 * flooding enabled, including the CPU port and DSA links.
552*3fa21270SVladimir Oltean 	 */
553*3fa21270SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
554*3fa21270SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
555*3fa21270SVladimir Oltean 			continue;
556*3fa21270SVladimir Oltean 
557*3fa21270SVladimir Oltean 		priv->ucast_egress_floods |= BIT(port);
558*3fa21270SVladimir Oltean 		priv->bcast_egress_floods |= BIT(port);
5598aa9ebccSVladimir Oltean 	}
560f238fef1SVladimir Oltean 
5618aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
5628aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
5638aa9ebccSVladimir Oltean 	 */
564*3fa21270SVladimir Oltean 	for (tc = 0; tc < SJA1105_NUM_TC; tc++) {
565*3fa21270SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
566*3fa21270SVladimir Oltean 			if (dsa_is_unused_port(ds, port))
567f238fef1SVladimir Oltean 				continue;
568f238fef1SVladimir Oltean 
569*3fa21270SVladimir Oltean 			l2fwd[ds->num_ports + tc].vlan_pmap[port] = tc;
570f238fef1SVladimir Oltean 		}
5713e77e59bSVladimir Oltean 
572*3fa21270SVladimir Oltean 		l2fwd[ds->num_ports + tc].type_egrpcp2outputq = true;
5733e77e59bSVladimir Oltean 	}
5743e77e59bSVladimir Oltean 
5753e77e59bSVladimir Oltean 	return 0;
5763e77e59bSVladimir Oltean }
5773e77e59bSVladimir Oltean 
5783e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv)
5793e77e59bSVladimir Oltean {
5803e77e59bSVladimir Oltean 	struct sja1110_pcp_remapping_entry *pcp_remap;
5813e77e59bSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
5823e77e59bSVladimir Oltean 	struct sja1105_table *table;
5833e77e59bSVladimir Oltean 	int port, tc;
5843e77e59bSVladimir Oltean 
5853e77e59bSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING];
5863e77e59bSVladimir Oltean 
5873e77e59bSVladimir Oltean 	/* Nothing to do for SJA1105 */
5883e77e59bSVladimir Oltean 	if (!table->ops->max_entry_count)
5893e77e59bSVladimir Oltean 		return 0;
5903e77e59bSVladimir Oltean 
5913e77e59bSVladimir Oltean 	if (table->entry_count) {
5923e77e59bSVladimir Oltean 		kfree(table->entries);
5933e77e59bSVladimir Oltean 		table->entry_count = 0;
5943e77e59bSVladimir Oltean 	}
5953e77e59bSVladimir Oltean 
5963e77e59bSVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
5973e77e59bSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5983e77e59bSVladimir Oltean 	if (!table->entries)
5993e77e59bSVladimir Oltean 		return -ENOMEM;
6003e77e59bSVladimir Oltean 
6013e77e59bSVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
6023e77e59bSVladimir Oltean 
6033e77e59bSVladimir Oltean 	pcp_remap = table->entries;
6043e77e59bSVladimir Oltean 
6053e77e59bSVladimir Oltean 	/* Repeat the configuration done for vlan_pmap */
6063e77e59bSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
6073e77e59bSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
6083e77e59bSVladimir Oltean 			continue;
6093e77e59bSVladimir Oltean 
6103e77e59bSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
6113e77e59bSVladimir Oltean 			pcp_remap[port].egrpcp[tc] = tc;
612f238fef1SVladimir Oltean 	}
6138aa9ebccSVladimir Oltean 
6148aa9ebccSVladimir Oltean 	return 0;
6158aa9ebccSVladimir Oltean }
6168aa9ebccSVladimir Oltean 
6178aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
6188aa9ebccSVladimir Oltean {
6191bf658eeSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2fwd_params;
6208aa9ebccSVladimir Oltean 	struct sja1105_table *table;
6218aa9ebccSVladimir Oltean 
6228aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
6238aa9ebccSVladimir Oltean 
6248aa9ebccSVladimir Oltean 	if (table->entry_count) {
6258aa9ebccSVladimir Oltean 		kfree(table->entries);
6268aa9ebccSVladimir Oltean 		table->entry_count = 0;
6278aa9ebccSVladimir Oltean 	}
6288aa9ebccSVladimir Oltean 
629fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
6308aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
6318aa9ebccSVladimir Oltean 	if (!table->entries)
6328aa9ebccSVladimir Oltean 		return -ENOMEM;
6338aa9ebccSVladimir Oltean 
634fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
6358aa9ebccSVladimir Oltean 
6368aa9ebccSVladimir Oltean 	/* This table only has a single entry */
6371bf658eeSVladimir Oltean 	l2fwd_params = table->entries;
6381bf658eeSVladimir Oltean 
6391bf658eeSVladimir Oltean 	/* Disallow dynamic reconfiguration of vlan_pmap */
6401bf658eeSVladimir Oltean 	l2fwd_params->max_dynp = 0;
6411bf658eeSVladimir Oltean 	/* Use a single memory partition for all ingress queues */
6421bf658eeSVladimir Oltean 	l2fwd_params->part_spc[0] = priv->info->max_frame_mem;
6438aa9ebccSVladimir Oltean 
6448aa9ebccSVladimir Oltean 	return 0;
6458aa9ebccSVladimir Oltean }
6468aa9ebccSVladimir Oltean 
647aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
648aaa270c6SVladimir Oltean {
649aaa270c6SVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
650aaa270c6SVladimir Oltean 	struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
651aaa270c6SVladimir Oltean 	struct sja1105_table *table;
652aaa270c6SVladimir Oltean 
653aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
654aaa270c6SVladimir Oltean 	l2_fwd_params = table->entries;
6550fac6aa0SVladimir Oltean 	l2_fwd_params->part_spc[0] = SJA1105_MAX_FRAME_MEMORY;
656aaa270c6SVladimir Oltean 
657aaa270c6SVladimir Oltean 	/* If we have any critical-traffic virtual links, we need to reserve
658aaa270c6SVladimir Oltean 	 * some frame buffer memory for them. At the moment, hardcode the value
659aaa270c6SVladimir Oltean 	 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
660aaa270c6SVladimir Oltean 	 * remaining for best-effort traffic. TODO: figure out a more flexible
661aaa270c6SVladimir Oltean 	 * way to perform the frame buffer partitioning.
662aaa270c6SVladimir Oltean 	 */
663aaa270c6SVladimir Oltean 	if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
664aaa270c6SVladimir Oltean 		return;
665aaa270c6SVladimir Oltean 
666aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
667aaa270c6SVladimir Oltean 	vl_fwd_params = table->entries;
668aaa270c6SVladimir Oltean 
669aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
670aaa270c6SVladimir Oltean 	vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
671aaa270c6SVladimir Oltean }
672aaa270c6SVladimir Oltean 
673ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values:
674ceec8bc0SVladimir Oltean  *
675ceec8bc0SVladimir Oltean  *      | 100 Mbps ports |  1Gbps ports  | 2.5Gbps ports | Disabled ports
676ceec8bc0SVladimir Oltean  * -----+----------------+---------------+---------------+---------------
677ceec8bc0SVladimir Oltean  *   0  |   0, [5:10]    |     [1:2]     |     [3:4]     |     retag
678ceec8bc0SVladimir Oltean  *   1  |0, [5:10], retag|     [1:2]     |     [3:4]     |       -
679ceec8bc0SVladimir Oltean  *   2  |   0, [5:10]    |  [1:3], retag |       4       |       -
680ceec8bc0SVladimir Oltean  *   3  |   0, [5:10]    |[1:2], 4, retag|       3       |       -
681ceec8bc0SVladimir Oltean  *   4  |  0, 2, [5:10]  |    1, retag   |     [3:4]     |       -
682ceec8bc0SVladimir Oltean  *   5  |  0, 1, [5:10]  |    2, retag   |     [3:4]     |       -
683ceec8bc0SVladimir Oltean  *  14  |   0, [5:10]    | [1:4], retag  |       -       |       -
684ceec8bc0SVladimir Oltean  *  15  |     [5:10]     | [0:4], retag  |       -       |       -
685ceec8bc0SVladimir Oltean  */
686ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv)
687ceec8bc0SVladimir Oltean {
688ceec8bc0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
689ceec8bc0SVladimir Oltean 	struct sja1105_table *table;
690ceec8bc0SVladimir Oltean 	bool port_1_is_base_tx;
691ceec8bc0SVladimir Oltean 	bool port_3_is_2500;
692ceec8bc0SVladimir Oltean 	bool port_4_is_2500;
693ceec8bc0SVladimir Oltean 	u64 tdmaconfigidx;
694ceec8bc0SVladimir Oltean 
695ceec8bc0SVladimir Oltean 	if (priv->info->device_id != SJA1110_DEVICE_ID)
696ceec8bc0SVladimir Oltean 		return;
697ceec8bc0SVladimir Oltean 
698ceec8bc0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
699ceec8bc0SVladimir Oltean 	general_params = table->entries;
700ceec8bc0SVladimir Oltean 
701ceec8bc0SVladimir Oltean 	/* All the settings below are "as opposed to SGMII", which is the
702ceec8bc0SVladimir Oltean 	 * other pinmuxing option.
703ceec8bc0SVladimir Oltean 	 */
704ceec8bc0SVladimir Oltean 	port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL;
705ceec8bc0SVladimir Oltean 	port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX;
706ceec8bc0SVladimir Oltean 	port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX;
707ceec8bc0SVladimir Oltean 
708ceec8bc0SVladimir Oltean 	if (port_1_is_base_tx)
709ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
710ceec8bc0SVladimir Oltean 		tdmaconfigidx = 5;
711ceec8bc0SVladimir Oltean 	else if (port_3_is_2500 && port_4_is_2500)
712ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 100 Mbps */
713ceec8bc0SVladimir Oltean 		tdmaconfigidx = 1;
714ceec8bc0SVladimir Oltean 	else if (port_3_is_2500)
715ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
716ceec8bc0SVladimir Oltean 		tdmaconfigidx = 3;
717ceec8bc0SVladimir Oltean 	else if (port_4_is_2500)
718ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
719ceec8bc0SVladimir Oltean 		tdmaconfigidx = 2;
720ceec8bc0SVladimir Oltean 	else
721ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
722ceec8bc0SVladimir Oltean 		tdmaconfigidx = 14;
723ceec8bc0SVladimir Oltean 
724ceec8bc0SVladimir Oltean 	general_params->tdmaconfigidx = tdmaconfigidx;
725ceec8bc0SVladimir Oltean }
726ceec8bc0SVladimir Oltean 
72730a100e6SVladimir Oltean static int sja1105_init_topology(struct sja1105_private *priv,
72830a100e6SVladimir Oltean 				 struct sja1105_general_params_entry *general_params)
72930a100e6SVladimir Oltean {
73030a100e6SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
73130a100e6SVladimir Oltean 	int port;
73230a100e6SVladimir Oltean 
73330a100e6SVladimir Oltean 	/* The host port is the destination for traffic matching mac_fltres1
73430a100e6SVladimir Oltean 	 * and mac_fltres0 on all ports except itself. Default to an invalid
73530a100e6SVladimir Oltean 	 * value.
73630a100e6SVladimir Oltean 	 */
73730a100e6SVladimir Oltean 	general_params->host_port = ds->num_ports;
73830a100e6SVladimir Oltean 
73930a100e6SVladimir Oltean 	/* Link-local traffic received on casc_port will be forwarded
74030a100e6SVladimir Oltean 	 * to host_port without embedding the source port and device ID
74130a100e6SVladimir Oltean 	 * info in the destination MAC address, and no RX timestamps will be
74230a100e6SVladimir Oltean 	 * taken either (presumably because it is a cascaded port and a
74330a100e6SVladimir Oltean 	 * downstream SJA switch already did that).
74430a100e6SVladimir Oltean 	 * To disable the feature, we need to do different things depending on
74530a100e6SVladimir Oltean 	 * switch generation. On SJA1105 we need to set an invalid port, while
74630a100e6SVladimir Oltean 	 * on SJA1110 which support multiple cascaded ports, this field is a
74730a100e6SVladimir Oltean 	 * bitmask so it must be left zero.
74830a100e6SVladimir Oltean 	 */
74930a100e6SVladimir Oltean 	if (!priv->info->multiple_cascade_ports)
75030a100e6SVladimir Oltean 		general_params->casc_port = ds->num_ports;
75130a100e6SVladimir Oltean 
75230a100e6SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
75330a100e6SVladimir Oltean 		bool is_upstream = dsa_is_upstream_port(ds, port);
75430a100e6SVladimir Oltean 		bool is_dsa_link = dsa_is_dsa_port(ds, port);
75530a100e6SVladimir Oltean 
75630a100e6SVladimir Oltean 		/* Upstream ports can be dedicated CPU ports or
75730a100e6SVladimir Oltean 		 * upstream-facing DSA links
75830a100e6SVladimir Oltean 		 */
75930a100e6SVladimir Oltean 		if (is_upstream) {
76030a100e6SVladimir Oltean 			if (general_params->host_port == ds->num_ports) {
76130a100e6SVladimir Oltean 				general_params->host_port = port;
76230a100e6SVladimir Oltean 			} else {
76330a100e6SVladimir Oltean 				dev_err(ds->dev,
76430a100e6SVladimir Oltean 					"Port %llu is already a host port, configuring %d as one too is not supported\n",
76530a100e6SVladimir Oltean 					general_params->host_port, port);
76630a100e6SVladimir Oltean 				return -EINVAL;
76730a100e6SVladimir Oltean 			}
76830a100e6SVladimir Oltean 		}
76930a100e6SVladimir Oltean 
77030a100e6SVladimir Oltean 		/* Cascade ports are downstream-facing DSA links */
77130a100e6SVladimir Oltean 		if (is_dsa_link && !is_upstream) {
77230a100e6SVladimir Oltean 			if (priv->info->multiple_cascade_ports) {
77330a100e6SVladimir Oltean 				general_params->casc_port |= BIT(port);
77430a100e6SVladimir Oltean 			} else if (general_params->casc_port == ds->num_ports) {
77530a100e6SVladimir Oltean 				general_params->casc_port = port;
77630a100e6SVladimir Oltean 			} else {
77730a100e6SVladimir Oltean 				dev_err(ds->dev,
77830a100e6SVladimir Oltean 					"Port %llu is already a cascade port, configuring %d as one too is not supported\n",
77930a100e6SVladimir Oltean 					general_params->casc_port, port);
78030a100e6SVladimir Oltean 				return -EINVAL;
78130a100e6SVladimir Oltean 			}
78230a100e6SVladimir Oltean 		}
78330a100e6SVladimir Oltean 	}
78430a100e6SVladimir Oltean 
78530a100e6SVladimir Oltean 	if (general_params->host_port == ds->num_ports) {
78630a100e6SVladimir Oltean 		dev_err(ds->dev, "No host port configured\n");
78730a100e6SVladimir Oltean 		return -EINVAL;
78830a100e6SVladimir Oltean 	}
78930a100e6SVladimir Oltean 
79030a100e6SVladimir Oltean 	return 0;
79130a100e6SVladimir Oltean }
79230a100e6SVladimir Oltean 
7938aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
7948aa9ebccSVladimir Oltean {
7958aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
796511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
797511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
7988aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
7995f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
8005f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
8015f06c63bSVladimir Oltean 		 */
80208fde09aSVladimir Oltean 		.hostprio = 7,
8038aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
8048aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
80542824463SVladimir Oltean 		.incl_srcpt1 = false,
8068aa9ebccSVladimir Oltean 		.send_meta1  = false,
8078aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
8088aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
80942824463SVladimir Oltean 		.incl_srcpt0 = false,
8108aa9ebccSVladimir Oltean 		.send_meta0  = false,
811511e6ca0SVladimir Oltean 		/* Default to an invalid value */
812542043e9SVladimir Oltean 		.mirr_port = priv->ds->num_ports,
8138aa9ebccSVladimir Oltean 		/* No TTEthernet */
814dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
8158aa9ebccSVladimir Oltean 		.vlmarker = 0,
8168aa9ebccSVladimir Oltean 		.vlmask = 0,
8178aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
8188aa9ebccSVladimir Oltean 		.ignore2stf = 0,
8196666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
8206666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
8216666cebcSVladimir Oltean 		 */
8226666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
8236666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
82429305260SVladimir Oltean 		/* Enable the TTEthernet engine on SJA1110 */
82529305260SVladimir Oltean 		.tte_en = true,
8264913b8ebSVladimir Oltean 		/* Set up the EtherType for control packets on SJA1110 */
8274913b8ebSVladimir Oltean 		.header_type = ETH_P_SJA1110,
8288aa9ebccSVladimir Oltean 	};
8296c0de59bSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
8308aa9ebccSVladimir Oltean 	struct sja1105_table *table;
83130a100e6SVladimir Oltean 	int rc;
832df2a81a3SVladimir Oltean 
83330a100e6SVladimir Oltean 	rc = sja1105_init_topology(priv, &default_general_params);
83430a100e6SVladimir Oltean 	if (rc)
83530a100e6SVladimir Oltean 		return rc;
8368aa9ebccSVladimir Oltean 
8378aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
8388aa9ebccSVladimir Oltean 
8398aa9ebccSVladimir Oltean 	if (table->entry_count) {
8408aa9ebccSVladimir Oltean 		kfree(table->entries);
8418aa9ebccSVladimir Oltean 		table->entry_count = 0;
8428aa9ebccSVladimir Oltean 	}
8438aa9ebccSVladimir Oltean 
844fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
8458aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
8468aa9ebccSVladimir Oltean 	if (!table->entries)
8478aa9ebccSVladimir Oltean 		return -ENOMEM;
8488aa9ebccSVladimir Oltean 
849fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
8508aa9ebccSVladimir Oltean 
8516c0de59bSVladimir Oltean 	general_params = table->entries;
8526c0de59bSVladimir Oltean 
8538aa9ebccSVladimir Oltean 	/* This table only has a single entry */
8546c0de59bSVladimir Oltean 	general_params[0] = default_general_params;
8558aa9ebccSVladimir Oltean 
856ceec8bc0SVladimir Oltean 	sja1110_select_tdmaconfigidx(priv);
857ceec8bc0SVladimir Oltean 
8588aa9ebccSVladimir Oltean 	return 0;
8598aa9ebccSVladimir Oltean }
8608aa9ebccSVladimir Oltean 
86179d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
86279d5511cSVladimir Oltean {
86379d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
86479d5511cSVladimir Oltean 	struct sja1105_table *table;
86579d5511cSVladimir Oltean 
86679d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
86779d5511cSVladimir Oltean 
86879d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
86979d5511cSVladimir Oltean 	if (table->entry_count) {
87079d5511cSVladimir Oltean 		kfree(table->entries);
87179d5511cSVladimir Oltean 		table->entry_count = 0;
87279d5511cSVladimir Oltean 	}
87379d5511cSVladimir Oltean 
874fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
87579d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
87679d5511cSVladimir Oltean 	if (!table->entries)
87779d5511cSVladimir Oltean 		return -ENOMEM;
87879d5511cSVladimir Oltean 
879fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
88079d5511cSVladimir Oltean 
88179d5511cSVladimir Oltean 	avb = table->entries;
88279d5511cSVladimir Oltean 
88379d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
88479d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
88579d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
886747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
887747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
888747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
889747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
890747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
891747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
892747e5eb3SVladimir Oltean 	 */
893747e5eb3SVladimir Oltean 	avb->cas_master = false;
89479d5511cSVladimir Oltean 
89579d5511cSVladimir Oltean 	return 0;
89679d5511cSVladimir Oltean }
89779d5511cSVladimir Oltean 
898a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
899a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
900a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
901a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
902a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
903a7cc081cSVladimir Oltean  * will be used for this frame.
904a7cc081cSVladimir Oltean  *
905a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
906a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
907a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
908a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
909a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
910a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
911a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
912a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
913a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
914a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
915a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
916a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
917a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
918a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
919a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
920a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
921a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
922a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
923a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
924a7cc081cSVladimir Oltean  * +------------+--------+
925a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
926a7cc081cSVladimir Oltean  * +------------+--------+
927a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
928a7cc081cSVladimir Oltean  * +------------+--------+
929a7cc081cSVladimir Oltean  *    ...                                  ...
930a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
931a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
932a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
933a7cc081cSVladimir Oltean  *
934a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
935a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
936a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
937a7cc081cSVladimir Oltean  * lookup) equal.
938a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
939a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
940a7cc081cSVladimir Oltean  */
9418aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
9428aa9ebccSVladimir Oltean 
9438aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
9448aa9ebccSVladimir Oltean {
9458aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
946542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
9478aa9ebccSVladimir Oltean 	struct sja1105_table *table;
948a7cc081cSVladimir Oltean 	int port, tc;
9498aa9ebccSVladimir Oltean 
9508aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
9518aa9ebccSVladimir Oltean 
9528aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
9538aa9ebccSVladimir Oltean 	if (table->entry_count) {
9548aa9ebccSVladimir Oltean 		kfree(table->entries);
9558aa9ebccSVladimir Oltean 		table->entry_count = 0;
9568aa9ebccSVladimir Oltean 	}
9578aa9ebccSVladimir Oltean 
958fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
9598aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
9608aa9ebccSVladimir Oltean 	if (!table->entries)
9618aa9ebccSVladimir Oltean 		return -ENOMEM;
9628aa9ebccSVladimir Oltean 
963fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
9648aa9ebccSVladimir Oltean 
9658aa9ebccSVladimir Oltean 	policing = table->entries;
9668aa9ebccSVladimir Oltean 
967a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
968542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
96938fbe91fSVladimir Oltean 		int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port;
970542043e9SVladimir Oltean 		int bcast = (ds->num_ports * SJA1105_NUM_TC) + port;
971a7cc081cSVladimir Oltean 
972a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
973a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
974a7cc081cSVladimir Oltean 
975a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
97638fbe91fSVladimir Oltean 		/* Only SJA1110 has multicast policers */
97738fbe91fSVladimir Oltean 		if (mcast <= table->ops->max_entry_count)
97838fbe91fSVladimir Oltean 			policing[mcast].sharindx = port;
979a7cc081cSVladimir Oltean 	}
980a7cc081cSVladimir Oltean 
981a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
982542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
983c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
984c279c726SVladimir Oltean 
985a7cc081cSVladimir Oltean 		if (dsa_is_cpu_port(priv->ds, port))
986c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
9878aa9ebccSVladimir Oltean 
988a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
989a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
990a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
991a7cc081cSVladimir Oltean 		policing[port].partition = 0;
9928aa9ebccSVladimir Oltean 	}
993a7cc081cSVladimir Oltean 
9948aa9ebccSVladimir Oltean 	return 0;
9958aa9ebccSVladimir Oltean }
9968aa9ebccSVladimir Oltean 
9975d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv)
9988aa9ebccSVladimir Oltean {
9998aa9ebccSVladimir Oltean 	int rc;
10008aa9ebccSVladimir Oltean 
10018aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
10028aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
10038aa9ebccSVladimir Oltean 					priv->info->static_ops,
10048aa9ebccSVladimir Oltean 					priv->info->device_id);
10058aa9ebccSVladimir Oltean 	if (rc)
10068aa9ebccSVladimir Oltean 		return rc;
10078aa9ebccSVladimir Oltean 
10088aa9ebccSVladimir Oltean 	/* Build static configuration */
10098aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
10108aa9ebccSVladimir Oltean 	if (rc < 0)
10118aa9ebccSVladimir Oltean 		return rc;
10125d645df9SVladimir Oltean 	rc = sja1105_init_mii_settings(priv);
10138aa9ebccSVladimir Oltean 	if (rc < 0)
10148aa9ebccSVladimir Oltean 		return rc;
10158aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
10168aa9ebccSVladimir Oltean 	if (rc < 0)
10178aa9ebccSVladimir Oltean 		return rc;
10188aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
10198aa9ebccSVladimir Oltean 	if (rc < 0)
10208aa9ebccSVladimir Oltean 		return rc;
10218aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
10228aa9ebccSVladimir Oltean 	if (rc < 0)
10238aa9ebccSVladimir Oltean 		return rc;
10248aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
10258aa9ebccSVladimir Oltean 	if (rc < 0)
10268aa9ebccSVladimir Oltean 		return rc;
10278aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
10288aa9ebccSVladimir Oltean 	if (rc < 0)
10298aa9ebccSVladimir Oltean 		return rc;
10308aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
10318aa9ebccSVladimir Oltean 	if (rc < 0)
10328aa9ebccSVladimir Oltean 		return rc;
10338aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
10348aa9ebccSVladimir Oltean 	if (rc < 0)
10358aa9ebccSVladimir Oltean 		return rc;
103679d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
103779d5511cSVladimir Oltean 	if (rc < 0)
103879d5511cSVladimir Oltean 		return rc;
10393e77e59bSVladimir Oltean 	rc = sja1110_init_pcp_remapping(priv);
10403e77e59bSVladimir Oltean 	if (rc < 0)
10413e77e59bSVladimir Oltean 		return rc;
10428aa9ebccSVladimir Oltean 
10438aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
10448aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
10458aa9ebccSVladimir Oltean }
10468aa9ebccSVladimir Oltean 
104729afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv)
1048f5b8631cSVladimir Oltean {
1049542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
105029afb83aSVladimir Oltean 	int port;
1051f5b8631cSVladimir Oltean 
105229afb83aSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
105329afb83aSVladimir Oltean 		if (!priv->fixed_link[port])
1054f5b8631cSVladimir Oltean 			continue;
1055f5b8631cSVladimir Oltean 
105629afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID ||
105729afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
105829afb83aSVladimir Oltean 			priv->rgmii_rx_delay[port] = true;
1059f5b8631cSVladimir Oltean 
106029afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID ||
106129afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
106229afb83aSVladimir Oltean 			priv->rgmii_tx_delay[port] = true;
1063f5b8631cSVladimir Oltean 
106429afb83aSVladimir Oltean 		if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) &&
1065f5b8631cSVladimir Oltean 		    !priv->info->setup_rgmii_delay)
1066f5b8631cSVladimir Oltean 			return -EINVAL;
1067f5b8631cSVladimir Oltean 	}
1068f5b8631cSVladimir Oltean 	return 0;
1069f5b8631cSVladimir Oltean }
1070f5b8631cSVladimir Oltean 
10718aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
10728aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
10738aa9ebccSVladimir Oltean {
10748aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
10758aa9ebccSVladimir Oltean 	struct device_node *child;
10768aa9ebccSVladimir Oltean 
107727afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
10788aa9ebccSVladimir Oltean 		struct device_node *phy_node;
10790c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
10808aa9ebccSVladimir Oltean 		u32 index;
10810c65b2b9SAndrew Lunn 		int err;
10828aa9ebccSVladimir Oltean 
10838aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
10848aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
10858aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
10868aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
10877ba771e3SNishka Dasgupta 			of_node_put(child);
10888aa9ebccSVladimir Oltean 			return -ENODEV;
10898aa9ebccSVladimir Oltean 		}
10908aa9ebccSVladimir Oltean 
10918aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
10920c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
10930c65b2b9SAndrew Lunn 		if (err) {
10948aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
10958aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
10968aa9ebccSVladimir Oltean 				index);
10977ba771e3SNishka Dasgupta 			of_node_put(child);
10988aa9ebccSVladimir Oltean 			return -ENODEV;
10998aa9ebccSVladimir Oltean 		}
11008aa9ebccSVladimir Oltean 
11018aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
11028aa9ebccSVladimir Oltean 		if (!phy_node) {
11038aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
11048aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
11058aa9ebccSVladimir Oltean 					"properties missing!\n");
11067ba771e3SNishka Dasgupta 				of_node_put(child);
11078aa9ebccSVladimir Oltean 				return -ENODEV;
11088aa9ebccSVladimir Oltean 			}
11098aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
11108aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
11118aa9ebccSVladimir Oltean 			 */
111229afb83aSVladimir Oltean 			priv->fixed_link[index] = true;
11138aa9ebccSVladimir Oltean 		} else {
11148aa9ebccSVladimir Oltean 			of_node_put(phy_node);
11158aa9ebccSVladimir Oltean 		}
11168aa9ebccSVladimir Oltean 
1117bf4edf4aSVladimir Oltean 		priv->phy_mode[index] = phy_mode;
11188aa9ebccSVladimir Oltean 	}
11198aa9ebccSVladimir Oltean 
11208aa9ebccSVladimir Oltean 	return 0;
11218aa9ebccSVladimir Oltean }
11228aa9ebccSVladimir Oltean 
11235d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv)
11248aa9ebccSVladimir Oltean {
11258aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
11268aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
11278aa9ebccSVladimir Oltean 	struct device_node *ports_node;
11288aa9ebccSVladimir Oltean 	int rc;
11298aa9ebccSVladimir Oltean 
11308aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
113115074a36SVladimir Oltean 	if (!ports_node)
113215074a36SVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
11338aa9ebccSVladimir Oltean 	if (!ports_node) {
11348aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
11358aa9ebccSVladimir Oltean 		return -ENODEV;
11368aa9ebccSVladimir Oltean 	}
11378aa9ebccSVladimir Oltean 
11385d645df9SVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports_node);
11398aa9ebccSVladimir Oltean 	of_node_put(ports_node);
11408aa9ebccSVladimir Oltean 
11418aa9ebccSVladimir Oltean 	return rc;
11428aa9ebccSVladimir Oltean }
11438aa9ebccSVladimir Oltean 
1144c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
114541fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv,
114641fed17fSVladimir Oltean 					 u64 speed)
114741fed17fSVladimir Oltean {
114841fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS])
114941fed17fSVladimir Oltean 		return SPEED_10;
115041fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS])
115141fed17fSVladimir Oltean 		return SPEED_100;
115241fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS])
115341fed17fSVladimir Oltean 		return SPEED_1000;
115441fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS])
115541fed17fSVladimir Oltean 		return SPEED_2500;
115641fed17fSVladimir Oltean 	return SPEED_UNKNOWN;
115741fed17fSVladimir Oltean }
11588aa9ebccSVladimir Oltean 
11598400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
11608aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
11618400cff6SVladimir Oltean 				      int speed_mbps)
11628aa9ebccSVladimir Oltean {
11638aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
11648aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
116541fed17fSVladimir Oltean 	u64 speed;
11668aa9ebccSVladimir Oltean 	int rc;
11678aa9ebccSVladimir Oltean 
11688400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
11698400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
11708400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
11718400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
11728400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
11738400cff6SVladimir Oltean 	 */
11748aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
11758aa9ebccSVladimir Oltean 
1176f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
1177c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
1178a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
1179a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
1180a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
1181a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
1182a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
1183a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
1184a979a0abSVladimir Oltean 		 */
118541fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
1186f4cfcfbdSVladimir Oltean 		break;
1187c44d0535SVladimir Oltean 	case SPEED_10:
118841fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_10MBPS];
1189f4cfcfbdSVladimir Oltean 		break;
1190c44d0535SVladimir Oltean 	case SPEED_100:
119141fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_100MBPS];
1192f4cfcfbdSVladimir Oltean 		break;
1193c44d0535SVladimir Oltean 	case SPEED_1000:
119441fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
1195f4cfcfbdSVladimir Oltean 		break;
119656b63466SVladimir Oltean 	case SPEED_2500:
119756b63466SVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
119856b63466SVladimir Oltean 		break;
1199f4cfcfbdSVladimir Oltean 	default:
12008aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
12018aa9ebccSVladimir Oltean 		return -EINVAL;
12028aa9ebccSVladimir Oltean 	}
12038aa9ebccSVladimir Oltean 
12048400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
12058400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
12068400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
12078400cff6SVladimir Oltean 	 * we want auto during upload phase).
1208ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
1209ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
12108aa9ebccSVladimir Oltean 	 */
121191a05078SVladimir Oltean 	if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII)
121241fed17fSVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
121356b63466SVladimir Oltean 	else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX)
121456b63466SVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
1215ffe10e67SVladimir Oltean 	else
12168aa9ebccSVladimir Oltean 		mac[port].speed = speed;
12178aa9ebccSVladimir Oltean 
12188aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
12198400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
12208400cff6SVladimir Oltean 					  &mac[port], true);
12218aa9ebccSVladimir Oltean 	if (rc < 0) {
12228aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
12238aa9ebccSVladimir Oltean 		return rc;
12248aa9ebccSVladimir Oltean 	}
12258aa9ebccSVladimir Oltean 
12268aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
12278aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
12288aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
12298aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
12308aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
12318aa9ebccSVladimir Oltean 	 */
123291a05078SVladimir Oltean 	if (!phy_interface_mode_is_rgmii(priv->phy_mode[port]))
12338aa9ebccSVladimir Oltean 		return 0;
12348aa9ebccSVladimir Oltean 
12358aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
12368aa9ebccSVladimir Oltean }
12378aa9ebccSVladimir Oltean 
123839710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
123939710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
124039710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
124139710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
124239710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
124339710229SVladimir Oltean  * now.
124439710229SVladimir Oltean  */
124539710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
124639710229SVladimir Oltean 				      phy_interface_t interface)
124739710229SVladimir Oltean {
1248bf4edf4aSVladimir Oltean 	return priv->phy_mode[port] != interface;
124939710229SVladimir Oltean }
125039710229SVladimir Oltean 
1251af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
1252ffe10e67SVladimir Oltean 			       unsigned int mode,
1253af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
12548aa9ebccSVladimir Oltean {
12553ad1d171SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
12568aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12573ad1d171SVladimir Oltean 	struct dw_xpcs *xpcs;
12588aa9ebccSVladimir Oltean 
1259ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1260ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
1261ec8582d1SVladimir Oltean 			phy_modes(state->interface));
126239710229SVladimir Oltean 		return;
1263ec8582d1SVladimir Oltean 	}
126439710229SVladimir Oltean 
12653ad1d171SVladimir Oltean 	xpcs = priv->xpcs[port];
1266ffe10e67SVladimir Oltean 
12673ad1d171SVladimir Oltean 	if (xpcs)
12683ad1d171SVladimir Oltean 		phylink_set_pcs(dp->pl, &xpcs->pcs);
12698400cff6SVladimir Oltean }
12708400cff6SVladimir Oltean 
12718400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
12728400cff6SVladimir Oltean 				  unsigned int mode,
12738400cff6SVladimir Oltean 				  phy_interface_t interface)
12748400cff6SVladimir Oltean {
12758400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
12768400cff6SVladimir Oltean }
12778400cff6SVladimir Oltean 
12788400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
12798400cff6SVladimir Oltean 				unsigned int mode,
12808400cff6SVladimir Oltean 				phy_interface_t interface,
12815b502a7bSRussell King 				struct phy_device *phydev,
12825b502a7bSRussell King 				int speed, int duplex,
12835b502a7bSRussell King 				bool tx_pause, bool rx_pause)
12848400cff6SVladimir Oltean {
1285ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1286ec8582d1SVladimir Oltean 
1287ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1288ec8582d1SVladimir Oltean 
1289ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
12908aa9ebccSVladimir Oltean }
12918aa9ebccSVladimir Oltean 
1292ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1293ad9f299aSVladimir Oltean 				     unsigned long *supported,
1294ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
1295ad9f299aSVladimir Oltean {
1296ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
1297ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
1298ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
1299ad9f299aSVladimir Oltean 	 */
1300ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1301ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1302ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1303ad9f299aSVladimir Oltean 
1304ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1305ad9f299aSVladimir Oltean 
130639710229SVladimir Oltean 	/* include/linux/phylink.h says:
130739710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
130839710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
130939710229SVladimir Oltean 	 */
131039710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
131139710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
131239710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
131339710229SVladimir Oltean 		return;
131439710229SVladimir Oltean 	}
131539710229SVladimir Oltean 
1316ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1317ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1318ad9f299aSVladimir Oltean 	 */
1319ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
1320ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
1321ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
1322ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
1323ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
1324ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1325ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1326ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
132756b63466SVladimir Oltean 	if (priv->info->supports_2500basex[port]) {
132856b63466SVladimir Oltean 		phylink_set(mask, 2500baseT_Full);
132956b63466SVladimir Oltean 		phylink_set(mask, 2500baseX_Full);
133056b63466SVladimir Oltean 	}
1331ad9f299aSVladimir Oltean 
1332ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1333ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1334ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1335ad9f299aSVladimir Oltean }
1336ad9f299aSVladimir Oltean 
133760f6053fSVladimir Oltean static int
133860f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
133960f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
134060f6053fSVladimir Oltean {
134160f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
134260f6053fSVladimir Oltean 	struct sja1105_table *table;
134360f6053fSVladimir Oltean 	int i;
134460f6053fSVladimir Oltean 
134560f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
134660f6053fSVladimir Oltean 	l2_lookup = table->entries;
134760f6053fSVladimir Oltean 
134860f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
134960f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
135060f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
135160f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
135260f6053fSVladimir Oltean 			return i;
135360f6053fSVladimir Oltean 
135460f6053fSVladimir Oltean 	return -1;
135560f6053fSVladimir Oltean }
135660f6053fSVladimir Oltean 
135760f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
135860f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
135960f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
136060f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
136160f6053fSVladimir Oltean  */
136260f6053fSVladimir Oltean static int
136360f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
136460f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
136560f6053fSVladimir Oltean 			  bool keep)
136660f6053fSVladimir Oltean {
136760f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
136860f6053fSVladimir Oltean 	struct sja1105_table *table;
136960f6053fSVladimir Oltean 	int rc, match;
137060f6053fSVladimir Oltean 
137160f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
137260f6053fSVladimir Oltean 
137360f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
137460f6053fSVladimir Oltean 	if (match < 0) {
137560f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
137660f6053fSVladimir Oltean 		if (!keep)
137760f6053fSVladimir Oltean 			return 0;
137860f6053fSVladimir Oltean 
137960f6053fSVladimir Oltean 		/* No match => new entry */
138060f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
138160f6053fSVladimir Oltean 		if (rc)
138260f6053fSVladimir Oltean 			return rc;
138360f6053fSVladimir Oltean 
138460f6053fSVladimir Oltean 		match = table->entry_count - 1;
138560f6053fSVladimir Oltean 	}
138660f6053fSVladimir Oltean 
138760f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
138860f6053fSVladimir Oltean 	l2_lookup = table->entries;
138960f6053fSVladimir Oltean 
139060f6053fSVladimir Oltean 	/* We have a match.
139160f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
139260f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
139360f6053fSVladimir Oltean 	 * which we update it).
139460f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
139560f6053fSVladimir Oltean 	 */
139660f6053fSVladimir Oltean 	if (keep) {
139760f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
139860f6053fSVladimir Oltean 		return 0;
139960f6053fSVladimir Oltean 	}
140060f6053fSVladimir Oltean 
140160f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
140260f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
140360f6053fSVladimir Oltean 	 */
140460f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
140560f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
140660f6053fSVladimir Oltean }
140760f6053fSVladimir Oltean 
1408291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1409291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1410291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1411291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1412291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1413291d1e72SVladimir Oltean  */
141409c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1415291d1e72SVladimir Oltean {
1416291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1417291d1e72SVladimir Oltean }
1418291d1e72SVladimir Oltean 
14199dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1420291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1421291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1422291d1e72SVladimir Oltean 					 int *last_unused)
1423291d1e72SVladimir Oltean {
1424291d1e72SVladimir Oltean 	int way;
1425291d1e72SVladimir Oltean 
1426291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1427291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1428291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1429291d1e72SVladimir Oltean 
1430291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1431291d1e72SVladimir Oltean 		 * into the return value
1432291d1e72SVladimir Oltean 		 */
1433291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1434291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1435291d1e72SVladimir Oltean 			if (last_unused)
1436291d1e72SVladimir Oltean 				*last_unused = way;
1437291d1e72SVladimir Oltean 			continue;
1438291d1e72SVladimir Oltean 		}
1439291d1e72SVladimir Oltean 
1440291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1441291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1442291d1e72SVladimir Oltean 			if (match)
1443291d1e72SVladimir Oltean 				*match = l2_lookup;
1444291d1e72SVladimir Oltean 			return way;
1445291d1e72SVladimir Oltean 		}
1446291d1e72SVladimir Oltean 	}
1447291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1448291d1e72SVladimir Oltean 	return -1;
1449291d1e72SVladimir Oltean }
1450291d1e72SVladimir Oltean 
14519dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1452291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1453291d1e72SVladimir Oltean {
1454291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1455291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1456291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1457291d1e72SVladimir Oltean 	int last_unused = -1;
145860f6053fSVladimir Oltean 	int bin, way, rc;
1459291d1e72SVladimir Oltean 
14609dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1461291d1e72SVladimir Oltean 
14629dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1463291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1464291d1e72SVladimir Oltean 	if (way >= 0) {
1465291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1466291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1467291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1468291d1e72SVladimir Oltean 		 */
1469291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1470291d1e72SVladimir Oltean 			return 0;
1471291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1472291d1e72SVladimir Oltean 	} else {
1473291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1474291d1e72SVladimir Oltean 
1475291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1476291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1477291d1e72SVladimir Oltean 		 */
1478291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1479291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1480291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1481291d1e72SVladimir Oltean 
1482291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1483291d1e72SVladimir Oltean 			way = last_unused;
1484291d1e72SVladimir Oltean 		} else {
1485291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1486291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1487291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1488291d1e72SVladimir Oltean 			 * distribution function:
1489291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1490291d1e72SVladimir Oltean 			 */
1491291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1492291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1493291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1494291d1e72SVladimir Oltean 				 bin, addr, way);
1495291d1e72SVladimir Oltean 			/* Evict entry */
1496291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1497291d1e72SVladimir Oltean 						     index, NULL, false);
1498291d1e72SVladimir Oltean 		}
1499291d1e72SVladimir Oltean 	}
1500291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1501291d1e72SVladimir Oltean 
150260f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1503291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1504291d1e72SVladimir Oltean 					  true);
150560f6053fSVladimir Oltean 	if (rc < 0)
150660f6053fSVladimir Oltean 		return rc;
150760f6053fSVladimir Oltean 
150860f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1509291d1e72SVladimir Oltean }
1510291d1e72SVladimir Oltean 
15119dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1512291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1513291d1e72SVladimir Oltean {
1514291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1515291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
151660f6053fSVladimir Oltean 	int index, bin, way, rc;
1517291d1e72SVladimir Oltean 	bool keep;
1518291d1e72SVladimir Oltean 
15199dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
15209dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1521291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1522291d1e72SVladimir Oltean 	if (way < 0)
1523291d1e72SVladimir Oltean 		return 0;
1524291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1525291d1e72SVladimir Oltean 
1526291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1527291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1528291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1529291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1530291d1e72SVladimir Oltean 	 */
1531291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
15327752e937SVladimir Oltean 
1533291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1534291d1e72SVladimir Oltean 		keep = true;
1535291d1e72SVladimir Oltean 	else
1536291d1e72SVladimir Oltean 		keep = false;
1537291d1e72SVladimir Oltean 
153860f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1539291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
154060f6053fSVladimir Oltean 	if (rc < 0)
154160f6053fSVladimir Oltean 		return rc;
154260f6053fSVladimir Oltean 
154360f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1544291d1e72SVladimir Oltean }
1545291d1e72SVladimir Oltean 
15469dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
15479dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
15489dfa6911SVladimir Oltean {
15491da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
15501da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15511da73821SVladimir Oltean 	int rc, i;
15521da73821SVladimir Oltean 
15531da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
15541da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
15551da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
15561da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
15571da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
15580fac6aa0SVladimir Oltean 	if (priv->vlan_aware) {
15591da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
15601da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
15616d7c7d94SVladimir Oltean 	} else {
15626d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
15636d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
15646d7c7d94SVladimir Oltean 	}
15651da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
15661da73821SVladimir Oltean 
15671da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
15681da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
15691da73821SVladimir Oltean 	if (rc == 0) {
15701da73821SVladimir Oltean 		/* Found and this port is already in the entry's
15711da73821SVladimir Oltean 		 * port mask => job done
15721da73821SVladimir Oltean 		 */
15731da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
15741da73821SVladimir Oltean 			return 0;
15751da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
15761da73821SVladimir Oltean 		 * found something.
15771da73821SVladimir Oltean 		 */
15781da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
15791da73821SVladimir Oltean 		goto skip_finding_an_index;
15801da73821SVladimir Oltean 	}
15811da73821SVladimir Oltean 
15821da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
15831da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
15841da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
15851da73821SVladimir Oltean 	 */
15861da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
15871da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
15881da73821SVladimir Oltean 						 i, NULL);
15891da73821SVladimir Oltean 		if (rc < 0)
15901da73821SVladimir Oltean 			break;
15911da73821SVladimir Oltean 	}
15921da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
15931da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
15941da73821SVladimir Oltean 		return -EINVAL;
15951da73821SVladimir Oltean 	}
159617ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
15971da73821SVladimir Oltean 	l2_lookup.index = i;
15981da73821SVladimir Oltean 
15991da73821SVladimir Oltean skip_finding_an_index:
160060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
16011da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
16021da73821SVladimir Oltean 					  true);
160360f6053fSVladimir Oltean 	if (rc < 0)
160460f6053fSVladimir Oltean 		return rc;
160560f6053fSVladimir Oltean 
160660f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
16079dfa6911SVladimir Oltean }
16089dfa6911SVladimir Oltean 
16099dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
16109dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
16119dfa6911SVladimir Oltean {
16121da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
16131da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16141da73821SVladimir Oltean 	bool keep;
16151da73821SVladimir Oltean 	int rc;
16161da73821SVladimir Oltean 
16171da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
16181da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
16191da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
16201da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
16210fac6aa0SVladimir Oltean 	if (priv->vlan_aware) {
16221da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
16231da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
16246d7c7d94SVladimir Oltean 	} else {
16256d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
16266d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
16276d7c7d94SVladimir Oltean 	}
16281da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
16291da73821SVladimir Oltean 
16301da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
16311da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
16321da73821SVladimir Oltean 	if (rc < 0)
16331da73821SVladimir Oltean 		return 0;
16341da73821SVladimir Oltean 
16351da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
16361da73821SVladimir Oltean 
16371da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
16381da73821SVladimir Oltean 	 * or if we remove it completely.
16391da73821SVladimir Oltean 	 */
16401da73821SVladimir Oltean 	if (l2_lookup.destports)
16411da73821SVladimir Oltean 		keep = true;
16421da73821SVladimir Oltean 	else
16431da73821SVladimir Oltean 		keep = false;
16441da73821SVladimir Oltean 
164560f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
16461da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
164760f6053fSVladimir Oltean 	if (rc < 0)
164860f6053fSVladimir Oltean 		return rc;
164960f6053fSVladimir Oltean 
165060f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
16519dfa6911SVladimir Oltean }
16529dfa6911SVladimir Oltean 
16539dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
16549dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
16559dfa6911SVladimir Oltean {
16569dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1657b3ee526aSVladimir Oltean 
16586d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
16599dfa6911SVladimir Oltean }
16609dfa6911SVladimir Oltean 
16619dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
16629dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
16639dfa6911SVladimir Oltean {
16649dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16659dfa6911SVladimir Oltean 
1666b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
16679dfa6911SVladimir Oltean }
16689dfa6911SVladimir Oltean 
1669291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1670291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1671291d1e72SVladimir Oltean {
1672291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1673291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1674291d1e72SVladimir Oltean 	int i;
1675291d1e72SVladimir Oltean 
1676291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1677291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1678291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1679291d1e72SVladimir Oltean 		int rc;
1680291d1e72SVladimir Oltean 
1681291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1682291d1e72SVladimir Oltean 						 i, &l2_lookup);
1683291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1684def84604SVladimir Oltean 		if (rc == -ENOENT)
1685291d1e72SVladimir Oltean 			continue;
1686291d1e72SVladimir Oltean 		if (rc) {
1687291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1688291d1e72SVladimir Oltean 			return rc;
1689291d1e72SVladimir Oltean 		}
1690291d1e72SVladimir Oltean 
1691291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1692291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1693291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1694291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1695291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1696291d1e72SVladimir Oltean 		 */
1697291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1698291d1e72SVladimir Oltean 			continue;
16994d942354SVladimir Oltean 
17004d942354SVladimir Oltean 		/* We need to hide the FDB entry for unknown multicast */
17014d942354SVladimir Oltean 		if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST &&
17024d942354SVladimir Oltean 		    l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
17034d942354SVladimir Oltean 			continue;
17044d942354SVladimir Oltean 
1705291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
170693647594SVladimir Oltean 
17076d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
17080fac6aa0SVladimir Oltean 		if (!priv->vlan_aware)
17096d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
171017ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1711291d1e72SVladimir Oltean 	}
1712291d1e72SVladimir Oltean 	return 0;
1713291d1e72SVladimir Oltean }
1714291d1e72SVladimir Oltean 
1715a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port,
1716291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1717291d1e72SVladimir Oltean {
1718a52b2da7SVladimir Oltean 	return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1719291d1e72SVladimir Oltean }
1720291d1e72SVladimir Oltean 
1721291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1722291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1723291d1e72SVladimir Oltean {
1724291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1725291d1e72SVladimir Oltean }
1726291d1e72SVladimir Oltean 
17277f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration.
17287f7ccdeaSVladimir Oltean  * Flooding is configured between each {ingress, egress} port pair, and since
17297f7ccdeaSVladimir Oltean  * the bridge's semantics are those of "egress flooding", it means we must
17307f7ccdeaSVladimir Oltean  * enable flooding towards this port from all ingress ports that are in the
17317f7ccdeaSVladimir Oltean  * same forwarding domain.
17327f7ccdeaSVladimir Oltean  */
17337f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv)
17347f7ccdeaSVladimir Oltean {
17357f7ccdeaSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
17367f7ccdeaSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
17377f7ccdeaSVladimir Oltean 	int from, to, rc;
17387f7ccdeaSVladimir Oltean 
17397f7ccdeaSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
17407f7ccdeaSVladimir Oltean 
17417f7ccdeaSVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
17427f7ccdeaSVladimir Oltean 		u64 fl_domain = 0, bc_domain = 0;
17437f7ccdeaSVladimir Oltean 
17447f7ccdeaSVladimir Oltean 		for (to = 0; to < priv->ds->num_ports; to++) {
17457f7ccdeaSVladimir Oltean 			if (!sja1105_can_forward(l2_fwd, from, to))
17467f7ccdeaSVladimir Oltean 				continue;
17477f7ccdeaSVladimir Oltean 
17487f7ccdeaSVladimir Oltean 			if (priv->ucast_egress_floods & BIT(to))
17497f7ccdeaSVladimir Oltean 				fl_domain |= BIT(to);
17507f7ccdeaSVladimir Oltean 			if (priv->bcast_egress_floods & BIT(to))
17517f7ccdeaSVladimir Oltean 				bc_domain |= BIT(to);
17527f7ccdeaSVladimir Oltean 		}
17537f7ccdeaSVladimir Oltean 
17547f7ccdeaSVladimir Oltean 		/* Nothing changed, nothing to do */
17557f7ccdeaSVladimir Oltean 		if (l2_fwd[from].fl_domain == fl_domain &&
17567f7ccdeaSVladimir Oltean 		    l2_fwd[from].bc_domain == bc_domain)
17577f7ccdeaSVladimir Oltean 			continue;
17587f7ccdeaSVladimir Oltean 
17597f7ccdeaSVladimir Oltean 		l2_fwd[from].fl_domain = fl_domain;
17607f7ccdeaSVladimir Oltean 		l2_fwd[from].bc_domain = bc_domain;
17617f7ccdeaSVladimir Oltean 
17627f7ccdeaSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
17637f7ccdeaSVladimir Oltean 						  from, &l2_fwd[from], true);
17647f7ccdeaSVladimir Oltean 		if (rc < 0)
17657f7ccdeaSVladimir Oltean 			return rc;
17667f7ccdeaSVladimir Oltean 	}
17677f7ccdeaSVladimir Oltean 
17687f7ccdeaSVladimir Oltean 	return 0;
17697f7ccdeaSVladimir Oltean }
17707f7ccdeaSVladimir Oltean 
17718aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
17728aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
17738aa9ebccSVladimir Oltean {
17748aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
17758aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
17768aa9ebccSVladimir Oltean 	int i, rc;
17778aa9ebccSVladimir Oltean 
17788aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
17798aa9ebccSVladimir Oltean 
1780542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
17818aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
17828aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
17838aa9ebccSVladimir Oltean 		 */
17848aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
17858aa9ebccSVladimir Oltean 			continue;
17868aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
17878aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
17888aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
17898aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
17908aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
17918aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
17928aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
17938aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
17948aa9ebccSVladimir Oltean 		 */
17958aa9ebccSVladimir Oltean 		if (i == port)
17968aa9ebccSVladimir Oltean 			continue;
17978aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
17988aa9ebccSVladimir Oltean 			continue;
17998aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
18008aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
18018aa9ebccSVladimir Oltean 
18028aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
18038aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
18048aa9ebccSVladimir Oltean 		if (rc < 0)
18058aa9ebccSVladimir Oltean 			return rc;
18068aa9ebccSVladimir Oltean 	}
18078aa9ebccSVladimir Oltean 
18087f7ccdeaSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
18098aa9ebccSVladimir Oltean 					  port, &l2_fwd[port], true);
18107f7ccdeaSVladimir Oltean 	if (rc)
18117f7ccdeaSVladimir Oltean 		return rc;
18127f7ccdeaSVladimir Oltean 
1813cde8078eSVladimir Oltean 	rc = sja1105_commit_pvid(ds, port);
1814cde8078eSVladimir Oltean 	if (rc)
1815cde8078eSVladimir Oltean 		return rc;
1816cde8078eSVladimir Oltean 
18177f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
18188aa9ebccSVladimir Oltean }
18198aa9ebccSVladimir Oltean 
1820640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1821640f763fSVladimir Oltean 					 u8 state)
1822640f763fSVladimir Oltean {
1823640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1824640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1825640f763fSVladimir Oltean 
1826640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1827640f763fSVladimir Oltean 
1828640f763fSVladimir Oltean 	switch (state) {
1829640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1830640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1831640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1832640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1833640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1834640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1835640f763fSVladimir Oltean 		 */
1836640f763fSVladimir Oltean 		mac[port].ingress   = false;
1837640f763fSVladimir Oltean 		mac[port].egress    = false;
1838640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1839640f763fSVladimir Oltean 		break;
1840640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1841640f763fSVladimir Oltean 		mac[port].ingress   = true;
1842640f763fSVladimir Oltean 		mac[port].egress    = false;
1843640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1844640f763fSVladimir Oltean 		break;
1845640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1846640f763fSVladimir Oltean 		mac[port].ingress   = true;
1847640f763fSVladimir Oltean 		mac[port].egress    = false;
18484d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1849640f763fSVladimir Oltean 		break;
1850640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1851640f763fSVladimir Oltean 		mac[port].ingress   = true;
1852640f763fSVladimir Oltean 		mac[port].egress    = true;
18534d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1854640f763fSVladimir Oltean 		break;
1855640f763fSVladimir Oltean 	default:
1856640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1857640f763fSVladimir Oltean 		return;
1858640f763fSVladimir Oltean 	}
1859640f763fSVladimir Oltean 
1860640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1861640f763fSVladimir Oltean 				     &mac[port], true);
1862640f763fSVladimir Oltean }
1863640f763fSVladimir Oltean 
18648aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
18658aa9ebccSVladimir Oltean 			       struct net_device *br)
18668aa9ebccSVladimir Oltean {
18678aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
18688aa9ebccSVladimir Oltean }
18698aa9ebccSVladimir Oltean 
18708aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
18718aa9ebccSVladimir Oltean 				 struct net_device *br)
18728aa9ebccSVladimir Oltean {
18738aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
18748aa9ebccSVladimir Oltean }
18758aa9ebccSVladimir Oltean 
18764d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8)
18774d752508SVladimir Oltean 
18784d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
18794d752508SVladimir Oltean {
18804d752508SVladimir Oltean 	int i;
18814d752508SVladimir Oltean 
18824d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
18834d752508SVladimir Oltean 		if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
18844d752508SVladimir Oltean 			return i;
18854d752508SVladimir Oltean 
18864d752508SVladimir Oltean 	return -1;
18874d752508SVladimir Oltean }
18884d752508SVladimir Oltean 
18894d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
18904d752508SVladimir Oltean 				     int prio)
18914d752508SVladimir Oltean {
18924d752508SVladimir Oltean 	int i;
18934d752508SVladimir Oltean 
18944d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
18954d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
18964d752508SVladimir Oltean 
18974d752508SVladimir Oltean 		if (cbs->port == port && cbs->prio == prio) {
18984d752508SVladimir Oltean 			memset(cbs, 0, sizeof(*cbs));
18994d752508SVladimir Oltean 			return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
19004d752508SVladimir Oltean 							    i, cbs, true);
19014d752508SVladimir Oltean 		}
19024d752508SVladimir Oltean 	}
19034d752508SVladimir Oltean 
19044d752508SVladimir Oltean 	return 0;
19054d752508SVladimir Oltean }
19064d752508SVladimir Oltean 
19074d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
19084d752508SVladimir Oltean 				struct tc_cbs_qopt_offload *offload)
19094d752508SVladimir Oltean {
19104d752508SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
19114d752508SVladimir Oltean 	struct sja1105_cbs_entry *cbs;
19124d752508SVladimir Oltean 	int index;
19134d752508SVladimir Oltean 
19144d752508SVladimir Oltean 	if (!offload->enable)
19154d752508SVladimir Oltean 		return sja1105_delete_cbs_shaper(priv, port, offload->queue);
19164d752508SVladimir Oltean 
19174d752508SVladimir Oltean 	index = sja1105_find_unused_cbs_shaper(priv);
19184d752508SVladimir Oltean 	if (index < 0)
19194d752508SVladimir Oltean 		return -ENOSPC;
19204d752508SVladimir Oltean 
19214d752508SVladimir Oltean 	cbs = &priv->cbs[index];
19224d752508SVladimir Oltean 	cbs->port = port;
19234d752508SVladimir Oltean 	cbs->prio = offload->queue;
19244d752508SVladimir Oltean 	/* locredit and sendslope are negative by definition. In hardware,
19254d752508SVladimir Oltean 	 * positive values must be provided, and the negative sign is implicit.
19264d752508SVladimir Oltean 	 */
19274d752508SVladimir Oltean 	cbs->credit_hi = offload->hicredit;
19284d752508SVladimir Oltean 	cbs->credit_lo = abs(offload->locredit);
19294d752508SVladimir Oltean 	/* User space is in kbits/sec, hardware in bytes/sec */
19304d752508SVladimir Oltean 	cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
19314d752508SVladimir Oltean 	cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
19324d752508SVladimir Oltean 	/* Convert the negative values from 64-bit 2's complement
19334d752508SVladimir Oltean 	 * to 32-bit 2's complement (for the case of 0x80000000 whose
19344d752508SVladimir Oltean 	 * negative is still negative).
19354d752508SVladimir Oltean 	 */
19364d752508SVladimir Oltean 	cbs->credit_lo &= GENMASK_ULL(31, 0);
19374d752508SVladimir Oltean 	cbs->send_slope &= GENMASK_ULL(31, 0);
19384d752508SVladimir Oltean 
19394d752508SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
19404d752508SVladimir Oltean 					    true);
19414d752508SVladimir Oltean }
19424d752508SVladimir Oltean 
19434d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv)
19444d752508SVladimir Oltean {
19454d752508SVladimir Oltean 	int rc = 0, i;
19464d752508SVladimir Oltean 
1947be7f62eeSVladimir Oltean 	/* The credit based shapers are only allocated if
1948be7f62eeSVladimir Oltean 	 * CONFIG_NET_SCH_CBS is enabled.
1949be7f62eeSVladimir Oltean 	 */
1950be7f62eeSVladimir Oltean 	if (!priv->cbs)
1951be7f62eeSVladimir Oltean 		return 0;
1952be7f62eeSVladimir Oltean 
19534d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
19544d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
19554d752508SVladimir Oltean 
19564d752508SVladimir Oltean 		if (!cbs->idle_slope && !cbs->send_slope)
19574d752508SVladimir Oltean 			continue;
19584d752508SVladimir Oltean 
19594d752508SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
19604d752508SVladimir Oltean 						  true);
19614d752508SVladimir Oltean 		if (rc)
19624d752508SVladimir Oltean 			break;
19634d752508SVladimir Oltean 	}
19644d752508SVladimir Oltean 
19654d752508SVladimir Oltean 	return rc;
19664d752508SVladimir Oltean }
19674d752508SVladimir Oltean 
19682eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
19692eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
19702eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
19712eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
19722eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
1973c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1974dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
19752eea1fa8SVladimir Oltean };
19762eea1fa8SVladimir Oltean 
19776666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
19786666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
19796666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
19806666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
19816666cebcSVladimir Oltean  * such that this operation is relatively seamless.
19826666cebcSVladimir Oltean  */
19832eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
19842eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
19856666cebcSVladimir Oltean {
19866cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
19876cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
198882760d7fSVladimir Oltean 	int speed_mbps[SJA1105_MAX_NUM_PORTS];
198984db00f2SVladimir Oltean 	u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0};
19906666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
19916cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
19926cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
19936cf99c13SVladimir Oltean 	s64 t12, t34;
19946666cebcSVladimir Oltean 	int rc, i;
19956cf99c13SVladimir Oltean 	s64 now;
19966666cebcSVladimir Oltean 
1997af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1998af580ae2SVladimir Oltean 
19996666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
20006666cebcSVladimir Oltean 
20018400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
20028400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
20038400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
20048400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
20056666cebcSVladimir Oltean 	 */
2006542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
20073ad1d171SVladimir Oltean 		u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1);
20083ad1d171SVladimir Oltean 
200941fed17fSVladimir Oltean 		speed_mbps[i] = sja1105_port_speed_to_ethtool(priv,
201041fed17fSVladimir Oltean 							      mac[i].speed);
201141fed17fSVladimir Oltean 		mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
20126666cebcSVladimir Oltean 
20133ad1d171SVladimir Oltean 		if (priv->xpcs[i])
20143ad1d171SVladimir Oltean 			bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr);
201584db00f2SVladimir Oltean 	}
2016ffe10e67SVladimir Oltean 
20176cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
20186cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
20196cf99c13SVladimir Oltean 
20206cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
202161c77533SVladimir Oltean 	if (rc < 0) {
202261c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
202361c77533SVladimir Oltean 		goto out;
202461c77533SVladimir Oltean 	}
20256cf99c13SVladimir Oltean 
20266666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
20276666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
202861c77533SVladimir Oltean 	if (rc < 0) {
202961c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
203061c77533SVladimir Oltean 		goto out;
203161c77533SVladimir Oltean 	}
20326cf99c13SVladimir Oltean 
20336cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
203461c77533SVladimir Oltean 	if (rc < 0) {
203561c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
203661c77533SVladimir Oltean 		goto out;
203761c77533SVladimir Oltean 	}
20386cf99c13SVladimir Oltean 
20396cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
20406cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
20416cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
20426cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
20436cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
20446cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
20456cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
20466cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
20476cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
20486cf99c13SVladimir Oltean 	now += (t34 - t12);
20496cf99c13SVladimir Oltean 
20506cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
20516cf99c13SVladimir Oltean 
20526cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
20536666cebcSVladimir Oltean 
20542eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
20552eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
20562eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
20572eea1fa8SVladimir Oltean 
20586666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
20596666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
20606666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
20616666cebcSVladimir Oltean 	 */
2062cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
2063c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
20646666cebcSVladimir Oltean 		if (rc < 0)
20656666cebcSVladimir Oltean 			goto out;
2066cb5a82d2SVladimir Oltean 	}
20676666cebcSVladimir Oltean 
2068542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
20693ad1d171SVladimir Oltean 		struct dw_xpcs *xpcs = priv->xpcs[i];
20703ad1d171SVladimir Oltean 		unsigned int mode;
207184db00f2SVladimir Oltean 
20728400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
20736666cebcSVladimir Oltean 		if (rc < 0)
20746666cebcSVladimir Oltean 			goto out;
2075ffe10e67SVladimir Oltean 
20763ad1d171SVladimir Oltean 		if (!xpcs)
207784db00f2SVladimir Oltean 			continue;
2078ffe10e67SVladimir Oltean 
20793ad1d171SVladimir Oltean 		if (bmcr[i] & BMCR_ANENABLE)
20803ad1d171SVladimir Oltean 			mode = MLO_AN_INBAND;
20813ad1d171SVladimir Oltean 		else if (priv->fixed_link[i])
20823ad1d171SVladimir Oltean 			mode = MLO_AN_FIXED;
20833ad1d171SVladimir Oltean 		else
20843ad1d171SVladimir Oltean 			mode = MLO_AN_PHY;
208584db00f2SVladimir Oltean 
20863ad1d171SVladimir Oltean 		rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode);
20873ad1d171SVladimir Oltean 		if (rc < 0)
20883ad1d171SVladimir Oltean 			goto out;
2089ffe10e67SVladimir Oltean 
20903ad1d171SVladimir Oltean 		if (!phylink_autoneg_inband(mode)) {
2091ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
2092ffe10e67SVladimir Oltean 
209356b63466SVladimir Oltean 			if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX)
209456b63466SVladimir Oltean 				speed = SPEED_2500;
209556b63466SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED1000)
2096ffe10e67SVladimir Oltean 				speed = SPEED_1000;
209784db00f2SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED100)
2098ffe10e67SVladimir Oltean 				speed = SPEED_100;
2099053d8ad1SVladimir Oltean 			else
2100ffe10e67SVladimir Oltean 				speed = SPEED_10;
2101ffe10e67SVladimir Oltean 
21023ad1d171SVladimir Oltean 			xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i],
21033ad1d171SVladimir Oltean 				     speed, DUPLEX_FULL);
2104ffe10e67SVladimir Oltean 		}
2105ffe10e67SVladimir Oltean 	}
21064d752508SVladimir Oltean 
21074d752508SVladimir Oltean 	rc = sja1105_reload_cbs(priv);
21084d752508SVladimir Oltean 	if (rc < 0)
21094d752508SVladimir Oltean 		goto out;
21106666cebcSVladimir Oltean out:
2111af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
2112af580ae2SVladimir Oltean 
21136666cebcSVladimir Oltean 	return rc;
21146666cebcSVladimir Oltean }
21156666cebcSVladimir Oltean 
21168aa9ebccSVladimir Oltean static enum dsa_tag_protocol
21174d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
21184d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
21198aa9ebccSVladimir Oltean {
21204913b8ebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
21214913b8ebSVladimir Oltean 
21224913b8ebSVladimir Oltean 	return priv->info->tag_proto;
21238aa9ebccSVladimir Oltean }
21248aa9ebccSVladimir Oltean 
2125070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
2126070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
2127070ca3bbSVladimir Oltean  * So a switch reset is required.
2128070ca3bbSVladimir Oltean  */
212989153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
213089153ed6SVladimir Oltean 			   struct netlink_ext_ack *extack)
21316666cebcSVladimir Oltean {
21326d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2133070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
21346666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2135070ca3bbSVladimir Oltean 	struct sja1105_table *table;
2136dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
2137070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
21386666cebcSVladimir Oltean 	int rc;
21396666cebcSVladimir Oltean 
2140dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
2141dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
214289153ed6SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
214389153ed6SVladimir Oltean 					   "Cannot change VLAN filtering with active VL rules");
2144dfacc5a2SVladimir Oltean 			return -EBUSY;
2145dfacc5a2SVladimir Oltean 		}
2146dfacc5a2SVladimir Oltean 	}
2147dfacc5a2SVladimir Oltean 
2148070ca3bbSVladimir Oltean 	if (enabled) {
21496666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
215054fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
215154fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
2152070ca3bbSVladimir Oltean 	} else {
21536666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
2154070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
2155070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
2156070ca3bbSVladimir Oltean 	}
2157070ca3bbSVladimir Oltean 
215838b5beeaSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
215938b5beeaSVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
216038b5beeaSVladimir Oltean 
216138b5beeaSVladimir Oltean 		if (enabled)
216238b5beeaSVladimir Oltean 			sp->xmit_tpid = priv->info->qinq_tpid;
216338b5beeaSVladimir Oltean 		else
216438b5beeaSVladimir Oltean 			sp->xmit_tpid = ETH_P_SJA1105;
216538b5beeaSVladimir Oltean 	}
216638b5beeaSVladimir Oltean 
21670fac6aa0SVladimir Oltean 	if (priv->vlan_aware == enabled)
2168cfa36b1fSVladimir Oltean 		return 0;
2169cfa36b1fSVladimir Oltean 
21700fac6aa0SVladimir Oltean 	priv->vlan_aware = enabled;
21717f14937fSVladimir Oltean 
2172070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2173070ca3bbSVladimir Oltean 	general_params = table->entries;
2174f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
217554fa49eeSVladimir Oltean 	general_params->tpid = tpid;
217654fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2177070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
217842824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
217942824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
218042824463SVladimir Oltean 	 */
218142824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
218242824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
2183070ca3bbSVladimir Oltean 
21846d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
21852cafa72eSVladimir Oltean 	 * No VLAN filtering (or best effort) => shared VLAN learning.
21866d7c7d94SVladimir Oltean 	 *
21876d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
21886d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
21896d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
21906d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
21916d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
21926d7c7d94SVladimir Oltean 	 * forwarding decision.
21936d7c7d94SVladimir Oltean 	 *
21946d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
21956d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
21966d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
21976d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
21986d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
21996d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
22006d7c7d94SVladimir Oltean 	 * (all frames get flooded).
22016d7c7d94SVladimir Oltean 	 */
22026d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
22036d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
22040fac6aa0SVladimir Oltean 	l2_lookup_params->shared_learn = !priv->vlan_aware;
2205aaa270c6SVladimir Oltean 
22066dfd23d3SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
22076dfd23d3SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
22086dfd23d3SVladimir Oltean 			continue;
22096dfd23d3SVladimir Oltean 
22106dfd23d3SVladimir Oltean 		rc = sja1105_commit_pvid(ds, port);
2211aef31718SVladimir Oltean 		if (rc)
2212aef31718SVladimir Oltean 			return rc;
22136dfd23d3SVladimir Oltean 	}
2214aef31718SVladimir Oltean 
22152eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
22166666cebcSVladimir Oltean 	if (rc)
221789153ed6SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype");
22186666cebcSVladimir Oltean 
22190fac6aa0SVladimir Oltean 	return rc;
22206666cebcSVladimir Oltean }
22216666cebcSVladimir Oltean 
22226dfd23d3SVladimir Oltean static int sja1105_vlan_add(struct sja1105_private *priv, int port, u16 vid,
22236dfd23d3SVladimir Oltean 			    u16 flags)
22245899ee36SVladimir Oltean {
22256dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
22266dfd23d3SVladimir Oltean 	struct sja1105_table *table;
22276dfd23d3SVladimir Oltean 	int match, rc;
22285899ee36SVladimir Oltean 
22296dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
22306dfd23d3SVladimir Oltean 
22316dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
22326dfd23d3SVladimir Oltean 	if (match < 0) {
22336dfd23d3SVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
22346dfd23d3SVladimir Oltean 		if (rc)
22356dfd23d3SVladimir Oltean 			return rc;
22366dfd23d3SVladimir Oltean 		match = table->entry_count - 1;
22376dfd23d3SVladimir Oltean 	}
22386dfd23d3SVladimir Oltean 
22396dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
22406dfd23d3SVladimir Oltean 	vlan = table->entries;
22416dfd23d3SVladimir Oltean 
22426dfd23d3SVladimir Oltean 	vlan[match].type_entry = SJA1110_VLAN_D_TAG;
22436dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
22446dfd23d3SVladimir Oltean 	vlan[match].vlan_bc |= BIT(port);
22456dfd23d3SVladimir Oltean 	vlan[match].vmemb_port |= BIT(port);
22466dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
22476dfd23d3SVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
22486dfd23d3SVladimir Oltean 	else
22496dfd23d3SVladimir Oltean 		vlan[match].tag_port |= BIT(port);
22506dfd23d3SVladimir Oltean 
22516dfd23d3SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
22526dfd23d3SVladimir Oltean 					    &vlan[match], true);
22536dfd23d3SVladimir Oltean }
22546dfd23d3SVladimir Oltean 
22556dfd23d3SVladimir Oltean static int sja1105_vlan_del(struct sja1105_private *priv, int port, u16 vid)
22566dfd23d3SVladimir Oltean {
22576dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
22586dfd23d3SVladimir Oltean 	struct sja1105_table *table;
22596dfd23d3SVladimir Oltean 	bool keep = true;
22606dfd23d3SVladimir Oltean 	int match, rc;
22616dfd23d3SVladimir Oltean 
22626dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
22636dfd23d3SVladimir Oltean 
22646dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
22656dfd23d3SVladimir Oltean 	/* Can't delete a missing entry. */
22666dfd23d3SVladimir Oltean 	if (match < 0)
22675899ee36SVladimir Oltean 		return 0;
22685899ee36SVladimir Oltean 
22696dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
22706dfd23d3SVladimir Oltean 	vlan = table->entries;
22716dfd23d3SVladimir Oltean 
22726dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
22736dfd23d3SVladimir Oltean 	vlan[match].vlan_bc &= ~BIT(port);
22746dfd23d3SVladimir Oltean 	vlan[match].vmemb_port &= ~BIT(port);
22756dfd23d3SVladimir Oltean 	/* Also unset tag_port, just so we don't have a confusing bitmap
22766dfd23d3SVladimir Oltean 	 * (no practical purpose).
2277b38e659dSVladimir Oltean 	 */
22786dfd23d3SVladimir Oltean 	vlan[match].tag_port &= ~BIT(port);
2279b38e659dSVladimir Oltean 
22806dfd23d3SVladimir Oltean 	/* If there's no port left as member of this VLAN,
22816dfd23d3SVladimir Oltean 	 * it's time for it to go.
22826dfd23d3SVladimir Oltean 	 */
22836dfd23d3SVladimir Oltean 	if (!vlan[match].vmemb_port)
22846dfd23d3SVladimir Oltean 		keep = false;
22855899ee36SVladimir Oltean 
22866dfd23d3SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
22876dfd23d3SVladimir Oltean 					  &vlan[match], keep);
22886dfd23d3SVladimir Oltean 	if (rc < 0)
22896dfd23d3SVladimir Oltean 		return rc;
22905899ee36SVladimir Oltean 
22916dfd23d3SVladimir Oltean 	if (!keep)
22926dfd23d3SVladimir Oltean 		return sja1105_table_delete_entry(table, match);
22935899ee36SVladimir Oltean 
22945899ee36SVladimir Oltean 	return 0;
22955899ee36SVladimir Oltean }
22965899ee36SVladimir Oltean 
22976dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_add(struct dsa_switch *ds, int port,
229831046a5fSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan,
229931046a5fSVladimir Oltean 				   struct netlink_ext_ack *extack)
23006666cebcSVladimir Oltean {
23016666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2302884be12fSVladimir Oltean 	u16 flags = vlan->flags;
23036666cebcSVladimir Oltean 	int rc;
23046666cebcSVladimir Oltean 
23050fac6aa0SVladimir Oltean 	/* Be sure to deny alterations to the configuration done by tag_8021q.
23061958d581SVladimir Oltean 	 */
23070fac6aa0SVladimir Oltean 	if (vid_is_dsa_8021q(vlan->vid)) {
230831046a5fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
230931046a5fSVladimir Oltean 				   "Range 1024-3071 reserved for dsa_8021q operation");
23101958d581SVladimir Oltean 		return -EBUSY;
23111958d581SVladimir Oltean 	}
23121958d581SVladimir Oltean 
2313884be12fSVladimir Oltean 	/* Always install bridge VLANs as egress-tagged on the CPU port. */
2314884be12fSVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
2315884be12fSVladimir Oltean 		flags = 0;
2316884be12fSVladimir Oltean 
2317884be12fSVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vlan->vid, flags);
23186dfd23d3SVladimir Oltean 	if (rc)
23191958d581SVladimir Oltean 		return rc;
2320ec5ae610SVladimir Oltean 
23216dfd23d3SVladimir Oltean 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
23226dfd23d3SVladimir Oltean 		priv->bridge_pvid[port] = vlan->vid;
2323ec5ae610SVladimir Oltean 
23246dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
23256666cebcSVladimir Oltean }
23266666cebcSVladimir Oltean 
23276dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port,
23286666cebcSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan)
23296666cebcSVladimir Oltean {
23306666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2331bef0746cSVladimir Oltean 	int rc;
23326666cebcSVladimir Oltean 
2333bef0746cSVladimir Oltean 	rc = sja1105_vlan_del(priv, port, vlan->vid);
2334bef0746cSVladimir Oltean 	if (rc)
2335bef0746cSVladimir Oltean 		return rc;
2336bef0746cSVladimir Oltean 
2337bef0746cSVladimir Oltean 	/* In case the pvid was deleted, make sure that untagged packets will
2338bef0746cSVladimir Oltean 	 * be dropped.
2339bef0746cSVladimir Oltean 	 */
2340bef0746cSVladimir Oltean 	return sja1105_commit_pvid(ds, port);
23416666cebcSVladimir Oltean }
23426666cebcSVladimir Oltean 
23435899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
23445899ee36SVladimir Oltean 				      u16 flags)
23455899ee36SVladimir Oltean {
23465899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
23475899ee36SVladimir Oltean 	int rc;
23485899ee36SVladimir Oltean 
23496dfd23d3SVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vid, flags);
23506dfd23d3SVladimir Oltean 	if (rc)
23515899ee36SVladimir Oltean 		return rc;
23525899ee36SVladimir Oltean 
23536dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_PVID)
23546dfd23d3SVladimir Oltean 		priv->tag_8021q_pvid[port] = vid;
23556dfd23d3SVladimir Oltean 
23566dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
23575899ee36SVladimir Oltean }
23585899ee36SVladimir Oltean 
23595899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
23605899ee36SVladimir Oltean {
23615899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
23625899ee36SVladimir Oltean 
23636dfd23d3SVladimir Oltean 	return sja1105_vlan_del(priv, port, vid);
23645899ee36SVladimir Oltean }
23655899ee36SVladimir Oltean 
23664fbc08bdSVladimir Oltean static int sja1105_prechangeupper(struct dsa_switch *ds, int port,
23674fbc08bdSVladimir Oltean 				  struct netdev_notifier_changeupper_info *info)
23684fbc08bdSVladimir Oltean {
23694fbc08bdSVladimir Oltean 	struct netlink_ext_ack *extack = info->info.extack;
23704fbc08bdSVladimir Oltean 	struct net_device *upper = info->upper_dev;
237119fa937aSVladimir Oltean 	struct dsa_switch_tree *dst = ds->dst;
237219fa937aSVladimir Oltean 	struct dsa_port *dp;
23734fbc08bdSVladimir Oltean 
23744fbc08bdSVladimir Oltean 	if (is_vlan_dev(upper)) {
23754fbc08bdSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "8021q uppers are not supported");
23764fbc08bdSVladimir Oltean 		return -EBUSY;
23774fbc08bdSVladimir Oltean 	}
23784fbc08bdSVladimir Oltean 
237919fa937aSVladimir Oltean 	if (netif_is_bridge_master(upper)) {
238019fa937aSVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
238119fa937aSVladimir Oltean 			if (dp->bridge_dev && dp->bridge_dev != upper &&
238219fa937aSVladimir Oltean 			    br_vlan_enabled(dp->bridge_dev)) {
238319fa937aSVladimir Oltean 				NL_SET_ERR_MSG_MOD(extack,
238419fa937aSVladimir Oltean 						   "Only one VLAN-aware bridge is supported");
238519fa937aSVladimir Oltean 				return -EBUSY;
238619fa937aSVladimir Oltean 			}
238719fa937aSVladimir Oltean 		}
238819fa937aSVladimir Oltean 	}
238919fa937aSVladimir Oltean 
23904fbc08bdSVladimir Oltean 	return 0;
23914fbc08bdSVladimir Oltean }
23924fbc08bdSVladimir Oltean 
23938aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
23948aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
23958aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
23968aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
23978aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
23988aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
23998aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
24008aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
24018aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
24028aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
24038aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
24048aa9ebccSVladimir Oltean  */
24058aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
24068aa9ebccSVladimir Oltean {
24078aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
24088aa9ebccSVladimir Oltean 	int rc;
24098aa9ebccSVladimir Oltean 
24105d645df9SVladimir Oltean 	rc = sja1105_parse_dt(priv);
24118aa9ebccSVladimir Oltean 	if (rc < 0) {
24128aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
24138aa9ebccSVladimir Oltean 		return rc;
24148aa9ebccSVladimir Oltean 	}
2415f5b8631cSVladimir Oltean 
2416f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
2417f5b8631cSVladimir Oltean 	 * and we can't apply them.
2418f5b8631cSVladimir Oltean 	 */
241929afb83aSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv);
2420f5b8631cSVladimir Oltean 	if (rc < 0) {
2421f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
2422f5b8631cSVladimir Oltean 		return rc;
2423f5b8631cSVladimir Oltean 	}
2424f5b8631cSVladimir Oltean 
242561c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
2426bb77f36aSVladimir Oltean 	if (rc < 0) {
2427bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
2428bb77f36aSVladimir Oltean 		return rc;
2429bb77f36aSVladimir Oltean 	}
24305a8f0974SVladimir Oltean 
24315a8f0974SVladimir Oltean 	rc = sja1105_mdiobus_register(ds);
24325a8f0974SVladimir Oltean 	if (rc < 0) {
24335a8f0974SVladimir Oltean 		dev_err(ds->dev, "Failed to register MDIO bus: %pe\n",
24345a8f0974SVladimir Oltean 			ERR_PTR(rc));
24355a8f0974SVladimir Oltean 		goto out_ptp_clock_unregister;
24365a8f0974SVladimir Oltean 	}
24375a8f0974SVladimir Oltean 
2438cb5a82d2SVladimir Oltean 	if (priv->info->disable_microcontroller) {
2439cb5a82d2SVladimir Oltean 		rc = priv->info->disable_microcontroller(priv);
2440cb5a82d2SVladimir Oltean 		if (rc < 0) {
2441cb5a82d2SVladimir Oltean 			dev_err(ds->dev,
2442cb5a82d2SVladimir Oltean 				"Failed to disable microcontroller: %pe\n",
2443cb5a82d2SVladimir Oltean 				ERR_PTR(rc));
2444cb5a82d2SVladimir Oltean 			goto out_mdiobus_unregister;
2445cb5a82d2SVladimir Oltean 		}
2446cb5a82d2SVladimir Oltean 	}
2447cb5a82d2SVladimir Oltean 
24488aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
24495d645df9SVladimir Oltean 	rc = sja1105_static_config_load(priv);
24508aa9ebccSVladimir Oltean 	if (rc < 0) {
24518aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
24525a8f0974SVladimir Oltean 		goto out_mdiobus_unregister;
24538aa9ebccSVladimir Oltean 	}
2454cb5a82d2SVladimir Oltean 
24558aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
2456cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
2457c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
24588aa9ebccSVladimir Oltean 		if (rc < 0) {
2459cb5a82d2SVladimir Oltean 			dev_err(ds->dev,
2460cb5a82d2SVladimir Oltean 				"Failed to configure MII clocking: %pe\n",
2461cb5a82d2SVladimir Oltean 				ERR_PTR(rc));
2462cec279a8SVladimir Oltean 			goto out_static_config_free;
24638aa9ebccSVladimir Oltean 		}
2464cb5a82d2SVladimir Oltean 	}
2465cb5a82d2SVladimir Oltean 
24666666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
24676666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
24686666cebcSVladimir Oltean 	 * EtherType is.
24696666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
24706666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
24716666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
24726666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
24736666cebcSVladimir Oltean 	 */
24746666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
2475884be12fSVladimir Oltean 	ds->untag_bridge_pvid = true;
2476b6ad86e6SVladimir Oltean 	/* tag_8021q has 3 bits for the VBID, and the value 0 is reserved */
2477b6ad86e6SVladimir Oltean 	ds->num_fwd_offloading_bridges = 7;
24788aa9ebccSVladimir Oltean 
24795f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
24805f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
24815f06c63bSVladimir Oltean 
2482c279c726SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
2483c279c726SVladimir Oltean 
24840a7bdbc2SVladimir Oltean 	rc = sja1105_devlink_setup(ds);
24852cafa72eSVladimir Oltean 	if (rc < 0)
2486cec279a8SVladimir Oltean 		goto out_static_config_free;
24872cafa72eSVladimir Oltean 
2488bbed0bbdSVladimir Oltean 	rtnl_lock();
2489328621f6SVladimir Oltean 	rc = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q));
2490bbed0bbdSVladimir Oltean 	rtnl_unlock();
2491cec279a8SVladimir Oltean 	if (rc)
2492cec279a8SVladimir Oltean 		goto out_devlink_teardown;
2493cec279a8SVladimir Oltean 
2494cec279a8SVladimir Oltean 	return 0;
2495cec279a8SVladimir Oltean 
2496cec279a8SVladimir Oltean out_devlink_teardown:
2497cec279a8SVladimir Oltean 	sja1105_devlink_teardown(ds);
24985a8f0974SVladimir Oltean out_mdiobus_unregister:
24995a8f0974SVladimir Oltean 	sja1105_mdiobus_unregister(ds);
2500cec279a8SVladimir Oltean out_ptp_clock_unregister:
2501cec279a8SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
2502cec279a8SVladimir Oltean out_static_config_free:
2503cec279a8SVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2504bbed0bbdSVladimir Oltean 
2505bbed0bbdSVladimir Oltean 	return rc;
2506227d07a0SVladimir Oltean }
2507227d07a0SVladimir Oltean 
2508f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
2509f3097be2SVladimir Oltean {
2510f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2511a68578c2SVladimir Oltean 	int port;
2512a68578c2SVladimir Oltean 
2513328621f6SVladimir Oltean 	rtnl_lock();
2514328621f6SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
2515328621f6SVladimir Oltean 	rtnl_unlock();
2516328621f6SVladimir Oltean 
2517542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
2518a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2519a68578c2SVladimir Oltean 
2520a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2521a68578c2SVladimir Oltean 			continue;
2522a68578c2SVladimir Oltean 
252352c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
2524a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
2525a68578c2SVladimir Oltean 	}
2526f3097be2SVladimir Oltean 
25270a7bdbc2SVladimir Oltean 	sja1105_devlink_teardown(ds);
2528a6af7763SVladimir Oltean 	sja1105_flower_teardown(ds);
2529317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
253061c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
25316cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2532f3097be2SVladimir Oltean }
2533f3097be2SVladimir Oltean 
2534a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
2535a68578c2SVladimir Oltean {
2536a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2537a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
2538a68578c2SVladimir Oltean 
2539a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2540a68578c2SVladimir Oltean 		return;
2541a68578c2SVladimir Oltean 
2542a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
2543a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
2544a68578c2SVladimir Oltean }
2545a68578c2SVladimir Oltean 
2546227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
254747ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2548227d07a0SVladimir Oltean {
2549227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2550227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2551227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2552227d07a0SVladimir Oltean 	int timeout = 10;
2553227d07a0SVladimir Oltean 	int rc;
2554227d07a0SVladimir Oltean 
2555227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2556227d07a0SVladimir Oltean 
2557227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2558227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2559227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
256047ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
256147ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2562227d07a0SVladimir Oltean 
2563227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2564227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2565227d07a0SVladimir Oltean 	if (rc < 0) {
2566227d07a0SVladimir Oltean 		kfree_skb(skb);
2567227d07a0SVladimir Oltean 		return rc;
2568227d07a0SVladimir Oltean 	}
2569227d07a0SVladimir Oltean 
2570227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
257168bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2572227d07a0SVladimir Oltean 
2573227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2574227d07a0SVladimir Oltean 	do {
2575227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2576227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2577227d07a0SVladimir Oltean 		if (rc < 0) {
2578227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2579227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2580227d07a0SVladimir Oltean 			continue;
2581227d07a0SVladimir Oltean 		}
2582227d07a0SVladimir Oltean 
2583227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2584227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2585227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2586227d07a0SVladimir Oltean 		 */
2587227d07a0SVladimir Oltean 		cpu_relax();
2588227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2589227d07a0SVladimir Oltean 
2590227d07a0SVladimir Oltean 	if (!timeout) {
2591227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2592227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
25932a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
25942a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2595227d07a0SVladimir Oltean 		 */
2596227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2597227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2598227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2599227d07a0SVladimir Oltean 	}
2600227d07a0SVladimir Oltean 
2601227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2602227d07a0SVladimir Oltean }
2603227d07a0SVladimir Oltean 
2604a68578c2SVladimir Oltean #define work_to_port(work) \
2605a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
2606a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
2607a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
2608a68578c2SVladimir Oltean 
2609227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2610227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2611227d07a0SVladimir Oltean  * lock on the bus)
2612227d07a0SVladimir Oltean  */
2613a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2614227d07a0SVladimir Oltean {
2615a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
2616a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
2617a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
2618a68578c2SVladimir Oltean 	int port = sp - priv->ports;
2619a68578c2SVladimir Oltean 	struct sk_buff *skb;
2620a68578c2SVladimir Oltean 
2621a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
2622c4b364ceSYangbo Lu 		struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
2623227d07a0SVladimir Oltean 
2624227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
2625227d07a0SVladimir Oltean 
2626a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
2627a68578c2SVladimir Oltean 
262847ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
2629a68578c2SVladimir Oltean 		if (clone)
2630a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
2631227d07a0SVladimir Oltean 
2632227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
2633a68578c2SVladimir Oltean 	}
26348aa9ebccSVladimir Oltean }
26358aa9ebccSVladimir Oltean 
26368456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
26378456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
26388456721dSVladimir Oltean  */
26398456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
26408456721dSVladimir Oltean 				   unsigned int ageing_time)
26418456721dSVladimir Oltean {
26428456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
26438456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
26448456721dSVladimir Oltean 	struct sja1105_table *table;
26458456721dSVladimir Oltean 	unsigned int maxage;
26468456721dSVladimir Oltean 
26478456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
26488456721dSVladimir Oltean 	l2_lookup_params = table->entries;
26498456721dSVladimir Oltean 
26508456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
26518456721dSVladimir Oltean 
26528456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
26538456721dSVladimir Oltean 		return 0;
26548456721dSVladimir Oltean 
26558456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
26568456721dSVladimir Oltean 
26572eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
26588456721dSVladimir Oltean }
26598456721dSVladimir Oltean 
2660c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
2661c279c726SVladimir Oltean {
2662c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2663c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2664c279c726SVladimir Oltean 
2665c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
2666c279c726SVladimir Oltean 
2667c279c726SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
2668c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
2669c279c726SVladimir Oltean 
2670c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2671c279c726SVladimir Oltean 
2672a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
2673c279c726SVladimir Oltean 		return 0;
2674c279c726SVladimir Oltean 
2675a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
2676c279c726SVladimir Oltean 
2677c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2678c279c726SVladimir Oltean }
2679c279c726SVladimir Oltean 
2680c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
2681c279c726SVladimir Oltean {
2682c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
2683c279c726SVladimir Oltean }
2684c279c726SVladimir Oltean 
2685317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2686317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2687317ab5b8SVladimir Oltean 				 void *type_data)
2688317ab5b8SVladimir Oltean {
2689317ab5b8SVladimir Oltean 	switch (type) {
2690317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2691317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
26924d752508SVladimir Oltean 	case TC_SETUP_QDISC_CBS:
26934d752508SVladimir Oltean 		return sja1105_setup_tc_cbs(ds, port, type_data);
2694317ab5b8SVladimir Oltean 	default:
2695317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2696317ab5b8SVladimir Oltean 	}
2697317ab5b8SVladimir Oltean }
2698317ab5b8SVladimir Oltean 
2699511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2700511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2701511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2702511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2703511e6ca0SVladimir Oltean  * mirroring rule that references it.
2704511e6ca0SVladimir Oltean  */
2705511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2706511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2707511e6ca0SVladimir Oltean {
2708511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2709511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2710542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2711511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2712511e6ca0SVladimir Oltean 	bool already_enabled;
2713511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2714511e6ca0SVladimir Oltean 	int rc;
2715511e6ca0SVladimir Oltean 
2716511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2717511e6ca0SVladimir Oltean 	general_params = table->entries;
2718511e6ca0SVladimir Oltean 
2719511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2720511e6ca0SVladimir Oltean 
2721542043e9SVladimir Oltean 	already_enabled = (general_params->mirr_port != ds->num_ports);
2722511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2723511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2724511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2725511e6ca0SVladimir Oltean 			general_params->mirr_port);
2726511e6ca0SVladimir Oltean 		return -EBUSY;
2727511e6ca0SVladimir Oltean 	}
2728511e6ca0SVladimir Oltean 
2729511e6ca0SVladimir Oltean 	new_mirr_port = to;
2730511e6ca0SVladimir Oltean 	if (!enabled) {
2731511e6ca0SVladimir Oltean 		bool keep = false;
2732511e6ca0SVladimir Oltean 		int port;
2733511e6ca0SVladimir Oltean 
2734511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2735542043e9SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
2736511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2737511e6ca0SVladimir Oltean 				keep = true;
2738511e6ca0SVladimir Oltean 				break;
2739511e6ca0SVladimir Oltean 			}
2740511e6ca0SVladimir Oltean 		}
2741511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2742511e6ca0SVladimir Oltean 		if (!keep)
2743542043e9SVladimir Oltean 			new_mirr_port = ds->num_ports;
2744511e6ca0SVladimir Oltean 	}
2745511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2746511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2747511e6ca0SVladimir Oltean 
2748511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2749511e6ca0SVladimir Oltean 						  0, general_params, true);
2750511e6ca0SVladimir Oltean 		if (rc < 0)
2751511e6ca0SVladimir Oltean 			return rc;
2752511e6ca0SVladimir Oltean 	}
2753511e6ca0SVladimir Oltean 
2754511e6ca0SVladimir Oltean 	if (ingress)
2755511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2756511e6ca0SVladimir Oltean 	else
2757511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2758511e6ca0SVladimir Oltean 
2759511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2760511e6ca0SVladimir Oltean 					    &mac[from], true);
2761511e6ca0SVladimir Oltean }
2762511e6ca0SVladimir Oltean 
2763511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2764511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2765511e6ca0SVladimir Oltean 			      bool ingress)
2766511e6ca0SVladimir Oltean {
2767511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2768511e6ca0SVladimir Oltean 				    ingress, true);
2769511e6ca0SVladimir Oltean }
2770511e6ca0SVladimir Oltean 
2771511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2772511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2773511e6ca0SVladimir Oltean {
2774511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2775511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2776511e6ca0SVladimir Oltean }
2777511e6ca0SVladimir Oltean 
2778a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
2779a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
2780a7cc081cSVladimir Oltean {
2781a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2782a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2783a7cc081cSVladimir Oltean 
2784a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2785a7cc081cSVladimir Oltean 
2786a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
2787a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
2788a7cc081cSVladimir Oltean 	 * bytes.
2789a7cc081cSVladimir Oltean 	 */
2790a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
2791a7cc081cSVladimir Oltean 				      1000000);
27925f035af7SPo Liu 	policing[port].smax = policer->burst;
2793a7cc081cSVladimir Oltean 
2794a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2795a7cc081cSVladimir Oltean }
2796a7cc081cSVladimir Oltean 
2797a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
2798a7cc081cSVladimir Oltean {
2799a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2800a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2801a7cc081cSVladimir Oltean 
2802a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2803a7cc081cSVladimir Oltean 
2804a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
2805a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
2806a7cc081cSVladimir Oltean 
2807a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2808a7cc081cSVladimir Oltean }
2809a7cc081cSVladimir Oltean 
28104d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port,
28114d942354SVladimir Oltean 				     bool enabled)
28124d942354SVladimir Oltean {
28134d942354SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
28144d942354SVladimir Oltean 	int rc;
28154d942354SVladimir Oltean 
28164d942354SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
28174d942354SVladimir Oltean 
28184c44fc5eSVladimir Oltean 	mac[port].dyn_learn = enabled;
28194d942354SVladimir Oltean 
28204d942354SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
28214d942354SVladimir Oltean 					  &mac[port], true);
28224d942354SVladimir Oltean 	if (rc)
28234d942354SVladimir Oltean 		return rc;
28244d942354SVladimir Oltean 
28254d942354SVladimir Oltean 	if (enabled)
28264d942354SVladimir Oltean 		priv->learn_ena |= BIT(port);
28274d942354SVladimir Oltean 	else
28284d942354SVladimir Oltean 		priv->learn_ena &= ~BIT(port);
28294d942354SVladimir Oltean 
28304d942354SVladimir Oltean 	return 0;
28314d942354SVladimir Oltean }
28324d942354SVladimir Oltean 
28334d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to,
28344d942354SVladimir Oltean 					  struct switchdev_brport_flags flags)
28354d942354SVladimir Oltean {
28364d942354SVladimir Oltean 	if (flags.mask & BR_FLOOD) {
28374d942354SVladimir Oltean 		if (flags.val & BR_FLOOD)
28387f7ccdeaSVladimir Oltean 			priv->ucast_egress_floods |= BIT(to);
28394d942354SVladimir Oltean 		else
28406a5166e0SVladimir Oltean 			priv->ucast_egress_floods &= ~BIT(to);
28414d942354SVladimir Oltean 	}
28427f7ccdeaSVladimir Oltean 
28434d942354SVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD) {
28444d942354SVladimir Oltean 		if (flags.val & BR_BCAST_FLOOD)
28457f7ccdeaSVladimir Oltean 			priv->bcast_egress_floods |= BIT(to);
28464d942354SVladimir Oltean 		else
28476a5166e0SVladimir Oltean 			priv->bcast_egress_floods &= ~BIT(to);
28484d942354SVladimir Oltean 	}
28494d942354SVladimir Oltean 
28507f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
28514d942354SVladimir Oltean }
28524d942354SVladimir Oltean 
28534d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to,
28544d942354SVladimir Oltean 				    struct switchdev_brport_flags flags,
28554d942354SVladimir Oltean 				    struct netlink_ext_ack *extack)
28564d942354SVladimir Oltean {
28574d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
28584d942354SVladimir Oltean 	struct sja1105_table *table;
28594d942354SVladimir Oltean 	int match;
28604d942354SVladimir Oltean 
28614d942354SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
28624d942354SVladimir Oltean 	l2_lookup = table->entries;
28634d942354SVladimir Oltean 
28644d942354SVladimir Oltean 	for (match = 0; match < table->entry_count; match++)
28654d942354SVladimir Oltean 		if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST &&
28664d942354SVladimir Oltean 		    l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
28674d942354SVladimir Oltean 			break;
28684d942354SVladimir Oltean 
28694d942354SVladimir Oltean 	if (match == table->entry_count) {
28704d942354SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
28714d942354SVladimir Oltean 				   "Could not find FDB entry for unknown multicast");
28724d942354SVladimir Oltean 		return -ENOSPC;
28734d942354SVladimir Oltean 	}
28744d942354SVladimir Oltean 
28754d942354SVladimir Oltean 	if (flags.val & BR_MCAST_FLOOD)
28764d942354SVladimir Oltean 		l2_lookup[match].destports |= BIT(to);
28774d942354SVladimir Oltean 	else
28784d942354SVladimir Oltean 		l2_lookup[match].destports &= ~BIT(to);
28794d942354SVladimir Oltean 
28804d942354SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
28814d942354SVladimir Oltean 					    l2_lookup[match].index,
28824d942354SVladimir Oltean 					    &l2_lookup[match],
28834d942354SVladimir Oltean 					    true);
28844d942354SVladimir Oltean }
28854d942354SVladimir Oltean 
28864d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port,
28874d942354SVladimir Oltean 					 struct switchdev_brport_flags flags,
28884d942354SVladimir Oltean 					 struct netlink_ext_ack *extack)
28894d942354SVladimir Oltean {
28904d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
28914d942354SVladimir Oltean 
28924d942354SVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
28934d942354SVladimir Oltean 			   BR_BCAST_FLOOD))
28944d942354SVladimir Oltean 		return -EINVAL;
28954d942354SVladimir Oltean 
28964d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) &&
28974d942354SVladimir Oltean 	    !priv->info->can_limit_mcast_flood) {
28984d942354SVladimir Oltean 		bool multicast = !!(flags.val & BR_MCAST_FLOOD);
28994d942354SVladimir Oltean 		bool unicast = !!(flags.val & BR_FLOOD);
29004d942354SVladimir Oltean 
29014d942354SVladimir Oltean 		if (unicast != multicast) {
29024d942354SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
29034d942354SVladimir Oltean 					   "This chip cannot configure multicast flooding independently of unicast");
29044d942354SVladimir Oltean 			return -EINVAL;
29054d942354SVladimir Oltean 		}
29064d942354SVladimir Oltean 	}
29074d942354SVladimir Oltean 
29084d942354SVladimir Oltean 	return 0;
29094d942354SVladimir Oltean }
29104d942354SVladimir Oltean 
29114d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port,
29124d942354SVladimir Oltean 				     struct switchdev_brport_flags flags,
29134d942354SVladimir Oltean 				     struct netlink_ext_ack *extack)
29144d942354SVladimir Oltean {
29154d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29164d942354SVladimir Oltean 	int rc;
29174d942354SVladimir Oltean 
29184d942354SVladimir Oltean 	if (flags.mask & BR_LEARNING) {
29194d942354SVladimir Oltean 		bool learn_ena = !!(flags.val & BR_LEARNING);
29204d942354SVladimir Oltean 
29214d942354SVladimir Oltean 		rc = sja1105_port_set_learning(priv, port, learn_ena);
29224d942354SVladimir Oltean 		if (rc)
29234d942354SVladimir Oltean 			return rc;
29244d942354SVladimir Oltean 	}
29254d942354SVladimir Oltean 
29264d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) {
29274d942354SVladimir Oltean 		rc = sja1105_port_ucast_bcast_flood(priv, port, flags);
29284d942354SVladimir Oltean 		if (rc)
29294d942354SVladimir Oltean 			return rc;
29304d942354SVladimir Oltean 	}
29314d942354SVladimir Oltean 
29324d942354SVladimir Oltean 	/* For chips that can't offload BR_MCAST_FLOOD independently, there
29334d942354SVladimir Oltean 	 * is nothing to do here, we ensured the configuration is in sync by
29344d942354SVladimir Oltean 	 * offloading BR_FLOOD.
29354d942354SVladimir Oltean 	 */
29364d942354SVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) {
29374d942354SVladimir Oltean 		rc = sja1105_port_mcast_flood(priv, port, flags,
29384d942354SVladimir Oltean 					      extack);
29394d942354SVladimir Oltean 		if (rc)
29404d942354SVladimir Oltean 			return rc;
29414d942354SVladimir Oltean 	}
29424d942354SVladimir Oltean 
29434d942354SVladimir Oltean 	return 0;
29444d942354SVladimir Oltean }
29454d942354SVladimir Oltean 
29468aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
29478aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
29488aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
2949f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
29508456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
2951c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
2952c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
2953ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
2954af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
29558400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
29568400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
295752c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
295852c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
295952c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
2960bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
2961a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
2962291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
2963291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
2964291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
29658aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
29668aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
29674d942354SVladimir Oltean 	.port_pre_bridge_flags	= sja1105_port_pre_bridge_flags,
29684d942354SVladimir Oltean 	.port_bridge_flags	= sja1105_port_bridge_flags,
2969640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
29706666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
29716dfd23d3SVladimir Oltean 	.port_vlan_add		= sja1105_bridge_vlan_add,
29726dfd23d3SVladimir Oltean 	.port_vlan_del		= sja1105_bridge_vlan_del,
2973291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
2974291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
2975a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
2976a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
2977f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
297847ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
2979317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
2980511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
2981511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
2982a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
2983a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
2984a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
2985a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
2986834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
2987ff4cf8eaSVladimir Oltean 	.devlink_info_get	= sja1105_devlink_info_get,
29885da11eb4SVladimir Oltean 	.tag_8021q_vlan_add	= sja1105_dsa_8021q_vlan_add,
29895da11eb4SVladimir Oltean 	.tag_8021q_vlan_del	= sja1105_dsa_8021q_vlan_del,
29904fbc08bdSVladimir Oltean 	.port_prechangeupper	= sja1105_prechangeupper,
2991b6ad86e6SVladimir Oltean 	.port_bridge_tx_fwd_offload = dsa_tag_8021q_bridge_tx_fwd_offload,
2992b6ad86e6SVladimir Oltean 	.port_bridge_tx_fwd_unoffload = dsa_tag_8021q_bridge_tx_fwd_unoffload,
29938aa9ebccSVladimir Oltean };
29948aa9ebccSVladimir Oltean 
29950b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[];
29960b0e2997SVladimir Oltean 
29978aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
29988aa9ebccSVladimir Oltean {
29998aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
30008aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
30018aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
30020b0e2997SVladimir Oltean 	const struct of_device_id *match;
3003dff79620SVladimir Oltean 	u32 device_id;
30048aa9ebccSVladimir Oltean 	u64 part_no;
30058aa9ebccSVladimir Oltean 	int rc;
30068aa9ebccSVladimir Oltean 
300734d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
300834d76e9fSVladimir Oltean 			      NULL);
30098aa9ebccSVladimir Oltean 	if (rc < 0)
30108aa9ebccSVladimir Oltean 		return rc;
30118aa9ebccSVladimir Oltean 
30121bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
30131bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
30148aa9ebccSVladimir Oltean 	if (rc < 0)
30158aa9ebccSVladimir Oltean 		return rc;
30168aa9ebccSVladimir Oltean 
30178aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
30188aa9ebccSVladimir Oltean 
30195978fac0SNathan Chancellor 	for (match = sja1105_dt_ids; match->compatible[0]; match++) {
30200b0e2997SVladimir Oltean 		const struct sja1105_info *info = match->data;
30210b0e2997SVladimir Oltean 
30220b0e2997SVladimir Oltean 		/* Is what's been probed in our match table at all? */
30230b0e2997SVladimir Oltean 		if (info->device_id != device_id || info->part_no != part_no)
30240b0e2997SVladimir Oltean 			continue;
30250b0e2997SVladimir Oltean 
30260b0e2997SVladimir Oltean 		/* But is it what's in the device tree? */
30270b0e2997SVladimir Oltean 		if (priv->info->device_id != device_id ||
30280b0e2997SVladimir Oltean 		    priv->info->part_no != part_no) {
30290b0e2997SVladimir Oltean 			dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
30300b0e2997SVladimir Oltean 				 priv->info->name, info->name);
30310b0e2997SVladimir Oltean 			/* It isn't. No problem, pick that up. */
30320b0e2997SVladimir Oltean 			priv->info = info;
30338aa9ebccSVladimir Oltean 		}
30348aa9ebccSVladimir Oltean 
30358aa9ebccSVladimir Oltean 		return 0;
30368aa9ebccSVladimir Oltean 	}
30378aa9ebccSVladimir Oltean 
30380b0e2997SVladimir Oltean 	dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
30390b0e2997SVladimir Oltean 		device_id, part_no);
30400b0e2997SVladimir Oltean 
30410b0e2997SVladimir Oltean 	return -ENODEV;
30420b0e2997SVladimir Oltean }
30430b0e2997SVladimir Oltean 
30448aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
30458aa9ebccSVladimir Oltean {
3046844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
30478aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
30488aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
3049718bad0eSVladimir Oltean 	size_t max_xfer, max_msg;
30508aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
3051a68578c2SVladimir Oltean 	int rc, port;
30528aa9ebccSVladimir Oltean 
30538aa9ebccSVladimir Oltean 	if (!dev->of_node) {
30548aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
30558aa9ebccSVladimir Oltean 		return -EINVAL;
30568aa9ebccSVladimir Oltean 	}
30578aa9ebccSVladimir Oltean 
30588aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
30598aa9ebccSVladimir Oltean 	if (!priv)
30608aa9ebccSVladimir Oltean 		return -ENOMEM;
30618aa9ebccSVladimir Oltean 
30628aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
30638aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
30648aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
30658aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
30668aa9ebccSVladimir Oltean 	else
30678aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
30688aa9ebccSVladimir Oltean 
30698aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
30708aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
30718aa9ebccSVladimir Oltean 	 */
30728aa9ebccSVladimir Oltean 	priv->spidev = spi;
30738aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
30748aa9ebccSVladimir Oltean 
30758aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
30768aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
30778aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
30788aa9ebccSVladimir Oltean 	if (rc < 0) {
30798aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
30808aa9ebccSVladimir Oltean 		return rc;
30818aa9ebccSVladimir Oltean 	}
30828aa9ebccSVladimir Oltean 
3083718bad0eSVladimir Oltean 	/* In sja1105_xfer, we send spi_messages composed of two spi_transfers:
3084718bad0eSVladimir Oltean 	 * a small one for the message header and another one for the current
3085718bad0eSVladimir Oltean 	 * chunk of the packed buffer.
3086718bad0eSVladimir Oltean 	 * Check that the restrictions imposed by the SPI controller are
3087718bad0eSVladimir Oltean 	 * respected: the chunk buffer is smaller than the max transfer size,
3088718bad0eSVladimir Oltean 	 * and the total length of the chunk plus its message header is smaller
3089718bad0eSVladimir Oltean 	 * than the max message size.
3090718bad0eSVladimir Oltean 	 * We do that during probe time since the maximum transfer size is a
3091718bad0eSVladimir Oltean 	 * runtime invariant.
3092718bad0eSVladimir Oltean 	 */
3093718bad0eSVladimir Oltean 	max_xfer = spi_max_transfer_size(spi);
3094718bad0eSVladimir Oltean 	max_msg = spi_max_message_size(spi);
3095718bad0eSVladimir Oltean 
3096718bad0eSVladimir Oltean 	/* We need to send at least one 64-bit word of SPI payload per message
3097718bad0eSVladimir Oltean 	 * in order to be able to make useful progress.
3098718bad0eSVladimir Oltean 	 */
3099718bad0eSVladimir Oltean 	if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) {
3100718bad0eSVladimir Oltean 		dev_err(dev, "SPI master cannot send large enough buffers, aborting\n");
3101718bad0eSVladimir Oltean 		return -EINVAL;
3102718bad0eSVladimir Oltean 	}
3103718bad0eSVladimir Oltean 
3104718bad0eSVladimir Oltean 	priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN;
3105718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_xfer)
3106718bad0eSVladimir Oltean 		priv->max_xfer_len = max_xfer;
3107718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER)
3108718bad0eSVladimir Oltean 		priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER;
3109718bad0eSVladimir Oltean 
31108aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
31118aa9ebccSVladimir Oltean 
31128aa9ebccSVladimir Oltean 	/* Detect hardware device */
31138aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
31148aa9ebccSVladimir Oltean 	if (rc < 0) {
31158aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
31168aa9ebccSVladimir Oltean 		return rc;
31178aa9ebccSVladimir Oltean 	}
31188aa9ebccSVladimir Oltean 
31198aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
31208aa9ebccSVladimir Oltean 
31217e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
31228aa9ebccSVladimir Oltean 	if (!ds)
31238aa9ebccSVladimir Oltean 		return -ENOMEM;
31248aa9ebccSVladimir Oltean 
31257e99e347SVivien Didelot 	ds->dev = dev;
31263e77e59bSVladimir Oltean 	ds->num_ports = priv->info->num_ports;
31278aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
31288aa9ebccSVladimir Oltean 	ds->priv = priv;
31298aa9ebccSVladimir Oltean 	priv->ds = ds;
31308aa9ebccSVladimir Oltean 
3131844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
3132844d7edcSVladimir Oltean 
3133d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
3134d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
3135d5a619bfSVivien Didelot 
3136d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
3137a6af7763SVladimir Oltean 	sja1105_flower_setup(ds);
3138d5a619bfSVivien Didelot 
3139d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
3140d5a619bfSVivien Didelot 	if (rc)
3141328621f6SVladimir Oltean 		return rc;
3142d5a619bfSVivien Didelot 
31434d752508SVladimir Oltean 	if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
31444d752508SVladimir Oltean 		priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
31454d752508SVladimir Oltean 					 sizeof(struct sja1105_cbs_entry),
31464d752508SVladimir Oltean 					 GFP_KERNEL);
3147dc596e3fSVladimir Oltean 		if (!priv->cbs) {
3148dc596e3fSVladimir Oltean 			rc = -ENOMEM;
3149dc596e3fSVladimir Oltean 			goto out_unregister_switch;
3150dc596e3fSVladimir Oltean 		}
31514d752508SVladimir Oltean 	}
31524d752508SVladimir Oltean 
3153227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
3154542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
3155a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3156a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
3157a68578c2SVladimir Oltean 		struct net_device *slave;
3158227d07a0SVladimir Oltean 
3159a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3160a68578c2SVladimir Oltean 			continue;
3161a68578c2SVladimir Oltean 
3162a68578c2SVladimir Oltean 		dp->priv = sp;
3163a68578c2SVladimir Oltean 		sp->dp = dp;
3164844d7edcSVladimir Oltean 		sp->data = tagger_data;
3165a68578c2SVladimir Oltean 		slave = dp->slave;
3166a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
3167a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
3168a68578c2SVladimir Oltean 							slave->name);
3169a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
3170a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
3171a68578c2SVladimir Oltean 			dev_err(ds->dev,
3172a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
3173a68578c2SVladimir Oltean 				rc);
3174dc596e3fSVladimir Oltean 			goto out_destroy_workers;
3175a68578c2SVladimir Oltean 		}
3176a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
317738b5beeaSVladimir Oltean 		sp->xmit_tpid = ETH_P_SJA1105;
3178227d07a0SVladimir Oltean 	}
3179227d07a0SVladimir Oltean 
3180d5a619bfSVivien Didelot 	return 0;
3181dc596e3fSVladimir Oltean 
3182dc596e3fSVladimir Oltean out_destroy_workers:
3183a68578c2SVladimir Oltean 	while (port-- > 0) {
3184a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3185a68578c2SVladimir Oltean 
3186a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3187a68578c2SVladimir Oltean 			continue;
3188a68578c2SVladimir Oltean 
3189a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
3190a68578c2SVladimir Oltean 	}
3191dc596e3fSVladimir Oltean 
3192dc596e3fSVladimir Oltean out_unregister_switch:
3193dc596e3fSVladimir Oltean 	dsa_unregister_switch(ds);
3194dc596e3fSVladimir Oltean 
3195a68578c2SVladimir Oltean 	return rc;
31968aa9ebccSVladimir Oltean }
31978aa9ebccSVladimir Oltean 
31988aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
31998aa9ebccSVladimir Oltean {
32008aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
3201cedf4670SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
32028aa9ebccSVladimir Oltean 
3203cedf4670SVladimir Oltean 	dsa_unregister_switch(ds);
3204cedf4670SVladimir Oltean 
32058aa9ebccSVladimir Oltean 	return 0;
32068aa9ebccSVladimir Oltean }
32078aa9ebccSVladimir Oltean 
32088aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
32098aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
32108aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
32118aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
32128aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
32138aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
32148aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
32153e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110a", .data = &sja1110a_info },
32163e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110b", .data = &sja1110b_info },
32173e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110c", .data = &sja1110c_info },
32183e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110d", .data = &sja1110d_info },
32198aa9ebccSVladimir Oltean 	{ /* sentinel */ },
32208aa9ebccSVladimir Oltean };
32218aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
32228aa9ebccSVladimir Oltean 
32238aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
32248aa9ebccSVladimir Oltean 	.driver = {
32258aa9ebccSVladimir Oltean 		.name  = "sja1105",
32268aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
32278aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
32288aa9ebccSVladimir Oltean 	},
32298aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
32308aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
32318aa9ebccSVladimir Oltean };
32328aa9ebccSVladimir Oltean 
32338aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
32348aa9ebccSVladimir Oltean 
32358aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
32368aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
32378aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
32388aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
3239