xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 30a100e60cf36ade9902dc71610f93563d0bd7b0)
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;
4788aa9ebccSVladimir Oltean 	int i, j;
4798aa9ebccSVladimir Oltean 
4808aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
4818aa9ebccSVladimir Oltean 
4828aa9ebccSVladimir Oltean 	if (table->entry_count) {
4838aa9ebccSVladimir Oltean 		kfree(table->entries);
4848aa9ebccSVladimir Oltean 		table->entry_count = 0;
4858aa9ebccSVladimir Oltean 	}
4868aa9ebccSVladimir Oltean 
487fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4888aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4898aa9ebccSVladimir Oltean 	if (!table->entries)
4908aa9ebccSVladimir Oltean 		return -ENOMEM;
4918aa9ebccSVladimir Oltean 
492fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
4938aa9ebccSVladimir Oltean 
4948aa9ebccSVladimir Oltean 	l2fwd = table->entries;
4958aa9ebccSVladimir Oltean 
4968aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
497542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
4988aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
4998aa9ebccSVladimir Oltean 
500f238fef1SVladimir Oltean 		if (dsa_is_unused_port(ds, i))
501f238fef1SVladimir Oltean 			continue;
502f238fef1SVladimir Oltean 
5038aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
5048aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
5058aa9ebccSVladimir Oltean 
5067f7ccdeaSVladimir Oltean 		/* All ports start up with egress flooding enabled,
5077f7ccdeaSVladimir Oltean 		 * including the CPU port.
5087f7ccdeaSVladimir Oltean 		 */
5097f7ccdeaSVladimir Oltean 		priv->ucast_egress_floods |= BIT(i);
5107f7ccdeaSVladimir Oltean 		priv->bcast_egress_floods |= BIT(i);
5117f7ccdeaSVladimir Oltean 
5128aa9ebccSVladimir Oltean 		if (i == upstream)
5138aa9ebccSVladimir Oltean 			continue;
5148aa9ebccSVladimir Oltean 
5158aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
5168aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
5174d942354SVladimir Oltean 
5184d942354SVladimir Oltean 		l2fwd[i].bc_domain = BIT(upstream);
5194d942354SVladimir Oltean 		l2fwd[i].fl_domain = BIT(upstream);
5204d942354SVladimir Oltean 
5214d942354SVladimir Oltean 		l2fwd[upstream].bc_domain |= BIT(i);
5224d942354SVladimir Oltean 		l2fwd[upstream].fl_domain |= BIT(i);
5238aa9ebccSVladimir Oltean 	}
524f238fef1SVladimir Oltean 
5258aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
5268aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
5278aa9ebccSVladimir Oltean 	 */
528f238fef1SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++) {
529f238fef1SVladimir Oltean 		for (j = 0; j < ds->num_ports; j++) {
530f238fef1SVladimir Oltean 			if (dsa_is_unused_port(ds, j))
531f238fef1SVladimir Oltean 				continue;
532f238fef1SVladimir Oltean 
533542043e9SVladimir Oltean 			l2fwd[ds->num_ports + i].vlan_pmap[j] = i;
534f238fef1SVladimir Oltean 		}
5353e77e59bSVladimir Oltean 
5363e77e59bSVladimir Oltean 		l2fwd[ds->num_ports + i].type_egrpcp2outputq = true;
5373e77e59bSVladimir Oltean 	}
5383e77e59bSVladimir Oltean 
5393e77e59bSVladimir Oltean 	return 0;
5403e77e59bSVladimir Oltean }
5413e77e59bSVladimir Oltean 
5423e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv)
5433e77e59bSVladimir Oltean {
5443e77e59bSVladimir Oltean 	struct sja1110_pcp_remapping_entry *pcp_remap;
5453e77e59bSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
5463e77e59bSVladimir Oltean 	struct sja1105_table *table;
5473e77e59bSVladimir Oltean 	int port, tc;
5483e77e59bSVladimir Oltean 
5493e77e59bSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING];
5503e77e59bSVladimir Oltean 
5513e77e59bSVladimir Oltean 	/* Nothing to do for SJA1105 */
5523e77e59bSVladimir Oltean 	if (!table->ops->max_entry_count)
5533e77e59bSVladimir Oltean 		return 0;
5543e77e59bSVladimir Oltean 
5553e77e59bSVladimir Oltean 	if (table->entry_count) {
5563e77e59bSVladimir Oltean 		kfree(table->entries);
5573e77e59bSVladimir Oltean 		table->entry_count = 0;
5583e77e59bSVladimir Oltean 	}
5593e77e59bSVladimir Oltean 
5603e77e59bSVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
5613e77e59bSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5623e77e59bSVladimir Oltean 	if (!table->entries)
5633e77e59bSVladimir Oltean 		return -ENOMEM;
5643e77e59bSVladimir Oltean 
5653e77e59bSVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
5663e77e59bSVladimir Oltean 
5673e77e59bSVladimir Oltean 	pcp_remap = table->entries;
5683e77e59bSVladimir Oltean 
5693e77e59bSVladimir Oltean 	/* Repeat the configuration done for vlan_pmap */
5703e77e59bSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
5713e77e59bSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
5723e77e59bSVladimir Oltean 			continue;
5733e77e59bSVladimir Oltean 
5743e77e59bSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
5753e77e59bSVladimir Oltean 			pcp_remap[port].egrpcp[tc] = tc;
576f238fef1SVladimir Oltean 	}
5778aa9ebccSVladimir Oltean 
5788aa9ebccSVladimir Oltean 	return 0;
5798aa9ebccSVladimir Oltean }
5808aa9ebccSVladimir Oltean 
5818aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
5828aa9ebccSVladimir Oltean {
5831bf658eeSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2fwd_params;
5848aa9ebccSVladimir Oltean 	struct sja1105_table *table;
5858aa9ebccSVladimir Oltean 
5868aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
5878aa9ebccSVladimir Oltean 
5888aa9ebccSVladimir Oltean 	if (table->entry_count) {
5898aa9ebccSVladimir Oltean 		kfree(table->entries);
5908aa9ebccSVladimir Oltean 		table->entry_count = 0;
5918aa9ebccSVladimir Oltean 	}
5928aa9ebccSVladimir Oltean 
593fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
5948aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5958aa9ebccSVladimir Oltean 	if (!table->entries)
5968aa9ebccSVladimir Oltean 		return -ENOMEM;
5978aa9ebccSVladimir Oltean 
598fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
5998aa9ebccSVladimir Oltean 
6008aa9ebccSVladimir Oltean 	/* This table only has a single entry */
6011bf658eeSVladimir Oltean 	l2fwd_params = table->entries;
6021bf658eeSVladimir Oltean 
6031bf658eeSVladimir Oltean 	/* Disallow dynamic reconfiguration of vlan_pmap */
6041bf658eeSVladimir Oltean 	l2fwd_params->max_dynp = 0;
6051bf658eeSVladimir Oltean 	/* Use a single memory partition for all ingress queues */
6061bf658eeSVladimir Oltean 	l2fwd_params->part_spc[0] = priv->info->max_frame_mem;
6078aa9ebccSVladimir Oltean 
6088aa9ebccSVladimir Oltean 	return 0;
6098aa9ebccSVladimir Oltean }
6108aa9ebccSVladimir Oltean 
611aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
612aaa270c6SVladimir Oltean {
613aaa270c6SVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
614aaa270c6SVladimir Oltean 	struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
615aaa270c6SVladimir Oltean 	struct sja1105_table *table;
616aaa270c6SVladimir Oltean 
617aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
618aaa270c6SVladimir Oltean 	l2_fwd_params = table->entries;
6190fac6aa0SVladimir Oltean 	l2_fwd_params->part_spc[0] = SJA1105_MAX_FRAME_MEMORY;
620aaa270c6SVladimir Oltean 
621aaa270c6SVladimir Oltean 	/* If we have any critical-traffic virtual links, we need to reserve
622aaa270c6SVladimir Oltean 	 * some frame buffer memory for them. At the moment, hardcode the value
623aaa270c6SVladimir Oltean 	 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
624aaa270c6SVladimir Oltean 	 * remaining for best-effort traffic. TODO: figure out a more flexible
625aaa270c6SVladimir Oltean 	 * way to perform the frame buffer partitioning.
626aaa270c6SVladimir Oltean 	 */
627aaa270c6SVladimir Oltean 	if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
628aaa270c6SVladimir Oltean 		return;
629aaa270c6SVladimir Oltean 
630aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
631aaa270c6SVladimir Oltean 	vl_fwd_params = table->entries;
632aaa270c6SVladimir Oltean 
633aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
634aaa270c6SVladimir Oltean 	vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
635aaa270c6SVladimir Oltean }
636aaa270c6SVladimir Oltean 
637ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values:
638ceec8bc0SVladimir Oltean  *
639ceec8bc0SVladimir Oltean  *      | 100 Mbps ports |  1Gbps ports  | 2.5Gbps ports | Disabled ports
640ceec8bc0SVladimir Oltean  * -----+----------------+---------------+---------------+---------------
641ceec8bc0SVladimir Oltean  *   0  |   0, [5:10]    |     [1:2]     |     [3:4]     |     retag
642ceec8bc0SVladimir Oltean  *   1  |0, [5:10], retag|     [1:2]     |     [3:4]     |       -
643ceec8bc0SVladimir Oltean  *   2  |   0, [5:10]    |  [1:3], retag |       4       |       -
644ceec8bc0SVladimir Oltean  *   3  |   0, [5:10]    |[1:2], 4, retag|       3       |       -
645ceec8bc0SVladimir Oltean  *   4  |  0, 2, [5:10]  |    1, retag   |     [3:4]     |       -
646ceec8bc0SVladimir Oltean  *   5  |  0, 1, [5:10]  |    2, retag   |     [3:4]     |       -
647ceec8bc0SVladimir Oltean  *  14  |   0, [5:10]    | [1:4], retag  |       -       |       -
648ceec8bc0SVladimir Oltean  *  15  |     [5:10]     | [0:4], retag  |       -       |       -
649ceec8bc0SVladimir Oltean  */
650ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv)
651ceec8bc0SVladimir Oltean {
652ceec8bc0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
653ceec8bc0SVladimir Oltean 	struct sja1105_table *table;
654ceec8bc0SVladimir Oltean 	bool port_1_is_base_tx;
655ceec8bc0SVladimir Oltean 	bool port_3_is_2500;
656ceec8bc0SVladimir Oltean 	bool port_4_is_2500;
657ceec8bc0SVladimir Oltean 	u64 tdmaconfigidx;
658ceec8bc0SVladimir Oltean 
659ceec8bc0SVladimir Oltean 	if (priv->info->device_id != SJA1110_DEVICE_ID)
660ceec8bc0SVladimir Oltean 		return;
661ceec8bc0SVladimir Oltean 
662ceec8bc0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
663ceec8bc0SVladimir Oltean 	general_params = table->entries;
664ceec8bc0SVladimir Oltean 
665ceec8bc0SVladimir Oltean 	/* All the settings below are "as opposed to SGMII", which is the
666ceec8bc0SVladimir Oltean 	 * other pinmuxing option.
667ceec8bc0SVladimir Oltean 	 */
668ceec8bc0SVladimir Oltean 	port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL;
669ceec8bc0SVladimir Oltean 	port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX;
670ceec8bc0SVladimir Oltean 	port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX;
671ceec8bc0SVladimir Oltean 
672ceec8bc0SVladimir Oltean 	if (port_1_is_base_tx)
673ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
674ceec8bc0SVladimir Oltean 		tdmaconfigidx = 5;
675ceec8bc0SVladimir Oltean 	else if (port_3_is_2500 && port_4_is_2500)
676ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 100 Mbps */
677ceec8bc0SVladimir Oltean 		tdmaconfigidx = 1;
678ceec8bc0SVladimir Oltean 	else if (port_3_is_2500)
679ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
680ceec8bc0SVladimir Oltean 		tdmaconfigidx = 3;
681ceec8bc0SVladimir Oltean 	else if (port_4_is_2500)
682ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
683ceec8bc0SVladimir Oltean 		tdmaconfigidx = 2;
684ceec8bc0SVladimir Oltean 	else
685ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
686ceec8bc0SVladimir Oltean 		tdmaconfigidx = 14;
687ceec8bc0SVladimir Oltean 
688ceec8bc0SVladimir Oltean 	general_params->tdmaconfigidx = tdmaconfigidx;
689ceec8bc0SVladimir Oltean }
690ceec8bc0SVladimir Oltean 
691*30a100e6SVladimir Oltean static int sja1105_init_topology(struct sja1105_private *priv,
692*30a100e6SVladimir Oltean 				 struct sja1105_general_params_entry *general_params)
693*30a100e6SVladimir Oltean {
694*30a100e6SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
695*30a100e6SVladimir Oltean 	int port;
696*30a100e6SVladimir Oltean 
697*30a100e6SVladimir Oltean 	/* The host port is the destination for traffic matching mac_fltres1
698*30a100e6SVladimir Oltean 	 * and mac_fltres0 on all ports except itself. Default to an invalid
699*30a100e6SVladimir Oltean 	 * value.
700*30a100e6SVladimir Oltean 	 */
701*30a100e6SVladimir Oltean 	general_params->host_port = ds->num_ports;
702*30a100e6SVladimir Oltean 
703*30a100e6SVladimir Oltean 	/* Link-local traffic received on casc_port will be forwarded
704*30a100e6SVladimir Oltean 	 * to host_port without embedding the source port and device ID
705*30a100e6SVladimir Oltean 	 * info in the destination MAC address, and no RX timestamps will be
706*30a100e6SVladimir Oltean 	 * taken either (presumably because it is a cascaded port and a
707*30a100e6SVladimir Oltean 	 * downstream SJA switch already did that).
708*30a100e6SVladimir Oltean 	 * To disable the feature, we need to do different things depending on
709*30a100e6SVladimir Oltean 	 * switch generation. On SJA1105 we need to set an invalid port, while
710*30a100e6SVladimir Oltean 	 * on SJA1110 which support multiple cascaded ports, this field is a
711*30a100e6SVladimir Oltean 	 * bitmask so it must be left zero.
712*30a100e6SVladimir Oltean 	 */
713*30a100e6SVladimir Oltean 	if (!priv->info->multiple_cascade_ports)
714*30a100e6SVladimir Oltean 		general_params->casc_port = ds->num_ports;
715*30a100e6SVladimir Oltean 
716*30a100e6SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
717*30a100e6SVladimir Oltean 		bool is_upstream = dsa_is_upstream_port(ds, port);
718*30a100e6SVladimir Oltean 		bool is_dsa_link = dsa_is_dsa_port(ds, port);
719*30a100e6SVladimir Oltean 
720*30a100e6SVladimir Oltean 		/* Upstream ports can be dedicated CPU ports or
721*30a100e6SVladimir Oltean 		 * upstream-facing DSA links
722*30a100e6SVladimir Oltean 		 */
723*30a100e6SVladimir Oltean 		if (is_upstream) {
724*30a100e6SVladimir Oltean 			if (general_params->host_port == ds->num_ports) {
725*30a100e6SVladimir Oltean 				general_params->host_port = port;
726*30a100e6SVladimir Oltean 			} else {
727*30a100e6SVladimir Oltean 				dev_err(ds->dev,
728*30a100e6SVladimir Oltean 					"Port %llu is already a host port, configuring %d as one too is not supported\n",
729*30a100e6SVladimir Oltean 					general_params->host_port, port);
730*30a100e6SVladimir Oltean 				return -EINVAL;
731*30a100e6SVladimir Oltean 			}
732*30a100e6SVladimir Oltean 		}
733*30a100e6SVladimir Oltean 
734*30a100e6SVladimir Oltean 		/* Cascade ports are downstream-facing DSA links */
735*30a100e6SVladimir Oltean 		if (is_dsa_link && !is_upstream) {
736*30a100e6SVladimir Oltean 			if (priv->info->multiple_cascade_ports) {
737*30a100e6SVladimir Oltean 				general_params->casc_port |= BIT(port);
738*30a100e6SVladimir Oltean 			} else if (general_params->casc_port == ds->num_ports) {
739*30a100e6SVladimir Oltean 				general_params->casc_port = port;
740*30a100e6SVladimir Oltean 			} else {
741*30a100e6SVladimir Oltean 				dev_err(ds->dev,
742*30a100e6SVladimir Oltean 					"Port %llu is already a cascade port, configuring %d as one too is not supported\n",
743*30a100e6SVladimir Oltean 					general_params->casc_port, port);
744*30a100e6SVladimir Oltean 				return -EINVAL;
745*30a100e6SVladimir Oltean 			}
746*30a100e6SVladimir Oltean 		}
747*30a100e6SVladimir Oltean 	}
748*30a100e6SVladimir Oltean 
749*30a100e6SVladimir Oltean 	if (general_params->host_port == ds->num_ports) {
750*30a100e6SVladimir Oltean 		dev_err(ds->dev, "No host port configured\n");
751*30a100e6SVladimir Oltean 		return -EINVAL;
752*30a100e6SVladimir Oltean 	}
753*30a100e6SVladimir Oltean 
754*30a100e6SVladimir Oltean 	return 0;
755*30a100e6SVladimir Oltean }
756*30a100e6SVladimir Oltean 
7578aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
7588aa9ebccSVladimir Oltean {
7598aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
760511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
761511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
7628aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
7635f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
7645f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
7655f06c63bSVladimir Oltean 		 */
76608fde09aSVladimir Oltean 		.hostprio = 7,
7678aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
7688aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
76942824463SVladimir Oltean 		.incl_srcpt1 = false,
7708aa9ebccSVladimir Oltean 		.send_meta1  = false,
7718aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
7728aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
77342824463SVladimir Oltean 		.incl_srcpt0 = false,
7748aa9ebccSVladimir Oltean 		.send_meta0  = false,
775511e6ca0SVladimir Oltean 		/* Default to an invalid value */
776542043e9SVladimir Oltean 		.mirr_port = priv->ds->num_ports,
7778aa9ebccSVladimir Oltean 		/* No TTEthernet */
778dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
7798aa9ebccSVladimir Oltean 		.vlmarker = 0,
7808aa9ebccSVladimir Oltean 		.vlmask = 0,
7818aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
7828aa9ebccSVladimir Oltean 		.ignore2stf = 0,
7836666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
7846666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
7856666cebcSVladimir Oltean 		 */
7866666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
7876666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
78829305260SVladimir Oltean 		/* Enable the TTEthernet engine on SJA1110 */
78929305260SVladimir Oltean 		.tte_en = true,
7904913b8ebSVladimir Oltean 		/* Set up the EtherType for control packets on SJA1110 */
7914913b8ebSVladimir Oltean 		.header_type = ETH_P_SJA1110,
7928aa9ebccSVladimir Oltean 	};
7936c0de59bSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
7948aa9ebccSVladimir Oltean 	struct sja1105_table *table;
795*30a100e6SVladimir Oltean 	int rc;
796df2a81a3SVladimir Oltean 
797*30a100e6SVladimir Oltean 	rc = sja1105_init_topology(priv, &default_general_params);
798*30a100e6SVladimir Oltean 	if (rc)
799*30a100e6SVladimir Oltean 		return rc;
8008aa9ebccSVladimir Oltean 
8018aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
8028aa9ebccSVladimir Oltean 
8038aa9ebccSVladimir Oltean 	if (table->entry_count) {
8048aa9ebccSVladimir Oltean 		kfree(table->entries);
8058aa9ebccSVladimir Oltean 		table->entry_count = 0;
8068aa9ebccSVladimir Oltean 	}
8078aa9ebccSVladimir Oltean 
808fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
8098aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
8108aa9ebccSVladimir Oltean 	if (!table->entries)
8118aa9ebccSVladimir Oltean 		return -ENOMEM;
8128aa9ebccSVladimir Oltean 
813fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
8148aa9ebccSVladimir Oltean 
8156c0de59bSVladimir Oltean 	general_params = table->entries;
8166c0de59bSVladimir Oltean 
8178aa9ebccSVladimir Oltean 	/* This table only has a single entry */
8186c0de59bSVladimir Oltean 	general_params[0] = default_general_params;
8198aa9ebccSVladimir Oltean 
820ceec8bc0SVladimir Oltean 	sja1110_select_tdmaconfigidx(priv);
821ceec8bc0SVladimir Oltean 
8228aa9ebccSVladimir Oltean 	return 0;
8238aa9ebccSVladimir Oltean }
8248aa9ebccSVladimir Oltean 
82579d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
82679d5511cSVladimir Oltean {
82779d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
82879d5511cSVladimir Oltean 	struct sja1105_table *table;
82979d5511cSVladimir Oltean 
83079d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
83179d5511cSVladimir Oltean 
83279d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
83379d5511cSVladimir Oltean 	if (table->entry_count) {
83479d5511cSVladimir Oltean 		kfree(table->entries);
83579d5511cSVladimir Oltean 		table->entry_count = 0;
83679d5511cSVladimir Oltean 	}
83779d5511cSVladimir Oltean 
838fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
83979d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
84079d5511cSVladimir Oltean 	if (!table->entries)
84179d5511cSVladimir Oltean 		return -ENOMEM;
84279d5511cSVladimir Oltean 
843fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
84479d5511cSVladimir Oltean 
84579d5511cSVladimir Oltean 	avb = table->entries;
84679d5511cSVladimir Oltean 
84779d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
84879d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
84979d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
850747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
851747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
852747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
853747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
854747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
855747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
856747e5eb3SVladimir Oltean 	 */
857747e5eb3SVladimir Oltean 	avb->cas_master = false;
85879d5511cSVladimir Oltean 
85979d5511cSVladimir Oltean 	return 0;
86079d5511cSVladimir Oltean }
86179d5511cSVladimir Oltean 
862a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
863a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
864a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
865a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
866a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
867a7cc081cSVladimir Oltean  * will be used for this frame.
868a7cc081cSVladimir Oltean  *
869a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
870a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
871a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
872a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
873a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
874a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
875a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
876a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
877a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
878a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
879a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
880a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
881a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
882a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
883a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
884a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
885a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
886a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
887a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
888a7cc081cSVladimir Oltean  * +------------+--------+
889a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
890a7cc081cSVladimir Oltean  * +------------+--------+
891a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
892a7cc081cSVladimir Oltean  * +------------+--------+
893a7cc081cSVladimir Oltean  *    ...                                  ...
894a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
895a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
896a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
897a7cc081cSVladimir Oltean  *
898a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
899a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
900a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
901a7cc081cSVladimir Oltean  * lookup) equal.
902a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
903a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
904a7cc081cSVladimir Oltean  */
9058aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
9068aa9ebccSVladimir Oltean 
9078aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
9088aa9ebccSVladimir Oltean {
9098aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
910542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
9118aa9ebccSVladimir Oltean 	struct sja1105_table *table;
912a7cc081cSVladimir Oltean 	int port, tc;
9138aa9ebccSVladimir Oltean 
9148aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
9158aa9ebccSVladimir Oltean 
9168aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
9178aa9ebccSVladimir Oltean 	if (table->entry_count) {
9188aa9ebccSVladimir Oltean 		kfree(table->entries);
9198aa9ebccSVladimir Oltean 		table->entry_count = 0;
9208aa9ebccSVladimir Oltean 	}
9218aa9ebccSVladimir Oltean 
922fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
9238aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
9248aa9ebccSVladimir Oltean 	if (!table->entries)
9258aa9ebccSVladimir Oltean 		return -ENOMEM;
9268aa9ebccSVladimir Oltean 
927fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
9288aa9ebccSVladimir Oltean 
9298aa9ebccSVladimir Oltean 	policing = table->entries;
9308aa9ebccSVladimir Oltean 
931a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
932542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
93338fbe91fSVladimir Oltean 		int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port;
934542043e9SVladimir Oltean 		int bcast = (ds->num_ports * SJA1105_NUM_TC) + port;
935a7cc081cSVladimir Oltean 
936a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
937a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
938a7cc081cSVladimir Oltean 
939a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
94038fbe91fSVladimir Oltean 		/* Only SJA1110 has multicast policers */
94138fbe91fSVladimir Oltean 		if (mcast <= table->ops->max_entry_count)
94238fbe91fSVladimir Oltean 			policing[mcast].sharindx = port;
943a7cc081cSVladimir Oltean 	}
944a7cc081cSVladimir Oltean 
945a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
946542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
947c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
948c279c726SVladimir Oltean 
949a7cc081cSVladimir Oltean 		if (dsa_is_cpu_port(priv->ds, port))
950c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
9518aa9ebccSVladimir Oltean 
952a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
953a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
954a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
955a7cc081cSVladimir Oltean 		policing[port].partition = 0;
9568aa9ebccSVladimir Oltean 	}
957a7cc081cSVladimir Oltean 
9588aa9ebccSVladimir Oltean 	return 0;
9598aa9ebccSVladimir Oltean }
9608aa9ebccSVladimir Oltean 
9615d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv)
9628aa9ebccSVladimir Oltean {
9638aa9ebccSVladimir Oltean 	int rc;
9648aa9ebccSVladimir Oltean 
9658aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
9668aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
9678aa9ebccSVladimir Oltean 					priv->info->static_ops,
9688aa9ebccSVladimir Oltean 					priv->info->device_id);
9698aa9ebccSVladimir Oltean 	if (rc)
9708aa9ebccSVladimir Oltean 		return rc;
9718aa9ebccSVladimir Oltean 
9728aa9ebccSVladimir Oltean 	/* Build static configuration */
9738aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
9748aa9ebccSVladimir Oltean 	if (rc < 0)
9758aa9ebccSVladimir Oltean 		return rc;
9765d645df9SVladimir Oltean 	rc = sja1105_init_mii_settings(priv);
9778aa9ebccSVladimir Oltean 	if (rc < 0)
9788aa9ebccSVladimir Oltean 		return rc;
9798aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
9808aa9ebccSVladimir Oltean 	if (rc < 0)
9818aa9ebccSVladimir Oltean 		return rc;
9828aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
9838aa9ebccSVladimir Oltean 	if (rc < 0)
9848aa9ebccSVladimir Oltean 		return rc;
9858aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
9868aa9ebccSVladimir Oltean 	if (rc < 0)
9878aa9ebccSVladimir Oltean 		return rc;
9888aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
9898aa9ebccSVladimir Oltean 	if (rc < 0)
9908aa9ebccSVladimir Oltean 		return rc;
9918aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
9928aa9ebccSVladimir Oltean 	if (rc < 0)
9938aa9ebccSVladimir Oltean 		return rc;
9948aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
9958aa9ebccSVladimir Oltean 	if (rc < 0)
9968aa9ebccSVladimir Oltean 		return rc;
9978aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
9988aa9ebccSVladimir Oltean 	if (rc < 0)
9998aa9ebccSVladimir Oltean 		return rc;
100079d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
100179d5511cSVladimir Oltean 	if (rc < 0)
100279d5511cSVladimir Oltean 		return rc;
10033e77e59bSVladimir Oltean 	rc = sja1110_init_pcp_remapping(priv);
10043e77e59bSVladimir Oltean 	if (rc < 0)
10053e77e59bSVladimir Oltean 		return rc;
10068aa9ebccSVladimir Oltean 
10078aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
10088aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
10098aa9ebccSVladimir Oltean }
10108aa9ebccSVladimir Oltean 
101129afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv)
1012f5b8631cSVladimir Oltean {
1013542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
101429afb83aSVladimir Oltean 	int port;
1015f5b8631cSVladimir Oltean 
101629afb83aSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
101729afb83aSVladimir Oltean 		if (!priv->fixed_link[port])
1018f5b8631cSVladimir Oltean 			continue;
1019f5b8631cSVladimir Oltean 
102029afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID ||
102129afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
102229afb83aSVladimir Oltean 			priv->rgmii_rx_delay[port] = true;
1023f5b8631cSVladimir Oltean 
102429afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID ||
102529afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
102629afb83aSVladimir Oltean 			priv->rgmii_tx_delay[port] = true;
1027f5b8631cSVladimir Oltean 
102829afb83aSVladimir Oltean 		if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) &&
1029f5b8631cSVladimir Oltean 		    !priv->info->setup_rgmii_delay)
1030f5b8631cSVladimir Oltean 			return -EINVAL;
1031f5b8631cSVladimir Oltean 	}
1032f5b8631cSVladimir Oltean 	return 0;
1033f5b8631cSVladimir Oltean }
1034f5b8631cSVladimir Oltean 
10358aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
10368aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
10378aa9ebccSVladimir Oltean {
10388aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
10398aa9ebccSVladimir Oltean 	struct device_node *child;
10408aa9ebccSVladimir Oltean 
104127afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
10428aa9ebccSVladimir Oltean 		struct device_node *phy_node;
10430c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
10448aa9ebccSVladimir Oltean 		u32 index;
10450c65b2b9SAndrew Lunn 		int err;
10468aa9ebccSVladimir Oltean 
10478aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
10488aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
10498aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
10508aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
10517ba771e3SNishka Dasgupta 			of_node_put(child);
10528aa9ebccSVladimir Oltean 			return -ENODEV;
10538aa9ebccSVladimir Oltean 		}
10548aa9ebccSVladimir Oltean 
10558aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
10560c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
10570c65b2b9SAndrew Lunn 		if (err) {
10588aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
10598aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
10608aa9ebccSVladimir Oltean 				index);
10617ba771e3SNishka Dasgupta 			of_node_put(child);
10628aa9ebccSVladimir Oltean 			return -ENODEV;
10638aa9ebccSVladimir Oltean 		}
10648aa9ebccSVladimir Oltean 
10658aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
10668aa9ebccSVladimir Oltean 		if (!phy_node) {
10678aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
10688aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
10698aa9ebccSVladimir Oltean 					"properties missing!\n");
10707ba771e3SNishka Dasgupta 				of_node_put(child);
10718aa9ebccSVladimir Oltean 				return -ENODEV;
10728aa9ebccSVladimir Oltean 			}
10738aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
10748aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
10758aa9ebccSVladimir Oltean 			 */
107629afb83aSVladimir Oltean 			priv->fixed_link[index] = true;
10778aa9ebccSVladimir Oltean 		} else {
10788aa9ebccSVladimir Oltean 			of_node_put(phy_node);
10798aa9ebccSVladimir Oltean 		}
10808aa9ebccSVladimir Oltean 
1081bf4edf4aSVladimir Oltean 		priv->phy_mode[index] = phy_mode;
10828aa9ebccSVladimir Oltean 	}
10838aa9ebccSVladimir Oltean 
10848aa9ebccSVladimir Oltean 	return 0;
10858aa9ebccSVladimir Oltean }
10868aa9ebccSVladimir Oltean 
10875d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv)
10888aa9ebccSVladimir Oltean {
10898aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
10908aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
10918aa9ebccSVladimir Oltean 	struct device_node *ports_node;
10928aa9ebccSVladimir Oltean 	int rc;
10938aa9ebccSVladimir Oltean 
10948aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
109515074a36SVladimir Oltean 	if (!ports_node)
109615074a36SVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
10978aa9ebccSVladimir Oltean 	if (!ports_node) {
10988aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
10998aa9ebccSVladimir Oltean 		return -ENODEV;
11008aa9ebccSVladimir Oltean 	}
11018aa9ebccSVladimir Oltean 
11025d645df9SVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports_node);
11038aa9ebccSVladimir Oltean 	of_node_put(ports_node);
11048aa9ebccSVladimir Oltean 
11058aa9ebccSVladimir Oltean 	return rc;
11068aa9ebccSVladimir Oltean }
11078aa9ebccSVladimir Oltean 
1108c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
110941fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv,
111041fed17fSVladimir Oltean 					 u64 speed)
111141fed17fSVladimir Oltean {
111241fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS])
111341fed17fSVladimir Oltean 		return SPEED_10;
111441fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS])
111541fed17fSVladimir Oltean 		return SPEED_100;
111641fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS])
111741fed17fSVladimir Oltean 		return SPEED_1000;
111841fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS])
111941fed17fSVladimir Oltean 		return SPEED_2500;
112041fed17fSVladimir Oltean 	return SPEED_UNKNOWN;
112141fed17fSVladimir Oltean }
11228aa9ebccSVladimir Oltean 
11238400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
11248aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
11258400cff6SVladimir Oltean 				      int speed_mbps)
11268aa9ebccSVladimir Oltean {
11278aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
11288aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
112941fed17fSVladimir Oltean 	u64 speed;
11308aa9ebccSVladimir Oltean 	int rc;
11318aa9ebccSVladimir Oltean 
11328400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
11338400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
11348400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
11358400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
11368400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
11378400cff6SVladimir Oltean 	 */
11388aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
11398aa9ebccSVladimir Oltean 
1140f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
1141c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
1142a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
1143a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
1144a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
1145a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
1146a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
1147a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
1148a979a0abSVladimir Oltean 		 */
114941fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
1150f4cfcfbdSVladimir Oltean 		break;
1151c44d0535SVladimir Oltean 	case SPEED_10:
115241fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_10MBPS];
1153f4cfcfbdSVladimir Oltean 		break;
1154c44d0535SVladimir Oltean 	case SPEED_100:
115541fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_100MBPS];
1156f4cfcfbdSVladimir Oltean 		break;
1157c44d0535SVladimir Oltean 	case SPEED_1000:
115841fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
1159f4cfcfbdSVladimir Oltean 		break;
116056b63466SVladimir Oltean 	case SPEED_2500:
116156b63466SVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
116256b63466SVladimir Oltean 		break;
1163f4cfcfbdSVladimir Oltean 	default:
11648aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
11658aa9ebccSVladimir Oltean 		return -EINVAL;
11668aa9ebccSVladimir Oltean 	}
11678aa9ebccSVladimir Oltean 
11688400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
11698400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
11708400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
11718400cff6SVladimir Oltean 	 * we want auto during upload phase).
1172ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
1173ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
11748aa9ebccSVladimir Oltean 	 */
117591a05078SVladimir Oltean 	if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII)
117641fed17fSVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
117756b63466SVladimir Oltean 	else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX)
117856b63466SVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
1179ffe10e67SVladimir Oltean 	else
11808aa9ebccSVladimir Oltean 		mac[port].speed = speed;
11818aa9ebccSVladimir Oltean 
11828aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
11838400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
11848400cff6SVladimir Oltean 					  &mac[port], true);
11858aa9ebccSVladimir Oltean 	if (rc < 0) {
11868aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
11878aa9ebccSVladimir Oltean 		return rc;
11888aa9ebccSVladimir Oltean 	}
11898aa9ebccSVladimir Oltean 
11908aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
11918aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
11928aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
11938aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
11948aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
11958aa9ebccSVladimir Oltean 	 */
119691a05078SVladimir Oltean 	if (!phy_interface_mode_is_rgmii(priv->phy_mode[port]))
11978aa9ebccSVladimir Oltean 		return 0;
11988aa9ebccSVladimir Oltean 
11998aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
12008aa9ebccSVladimir Oltean }
12018aa9ebccSVladimir Oltean 
120239710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
120339710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
120439710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
120539710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
120639710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
120739710229SVladimir Oltean  * now.
120839710229SVladimir Oltean  */
120939710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
121039710229SVladimir Oltean 				      phy_interface_t interface)
121139710229SVladimir Oltean {
1212bf4edf4aSVladimir Oltean 	return priv->phy_mode[port] != interface;
121339710229SVladimir Oltean }
121439710229SVladimir Oltean 
1215af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
1216ffe10e67SVladimir Oltean 			       unsigned int mode,
1217af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
12188aa9ebccSVladimir Oltean {
12193ad1d171SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
12208aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12213ad1d171SVladimir Oltean 	struct dw_xpcs *xpcs;
12228aa9ebccSVladimir Oltean 
1223ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1224ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
1225ec8582d1SVladimir Oltean 			phy_modes(state->interface));
122639710229SVladimir Oltean 		return;
1227ec8582d1SVladimir Oltean 	}
122839710229SVladimir Oltean 
12293ad1d171SVladimir Oltean 	xpcs = priv->xpcs[port];
1230ffe10e67SVladimir Oltean 
12313ad1d171SVladimir Oltean 	if (xpcs)
12323ad1d171SVladimir Oltean 		phylink_set_pcs(dp->pl, &xpcs->pcs);
12338400cff6SVladimir Oltean }
12348400cff6SVladimir Oltean 
12358400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
12368400cff6SVladimir Oltean 				  unsigned int mode,
12378400cff6SVladimir Oltean 				  phy_interface_t interface)
12388400cff6SVladimir Oltean {
12398400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
12408400cff6SVladimir Oltean }
12418400cff6SVladimir Oltean 
12428400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
12438400cff6SVladimir Oltean 				unsigned int mode,
12448400cff6SVladimir Oltean 				phy_interface_t interface,
12455b502a7bSRussell King 				struct phy_device *phydev,
12465b502a7bSRussell King 				int speed, int duplex,
12475b502a7bSRussell King 				bool tx_pause, bool rx_pause)
12488400cff6SVladimir Oltean {
1249ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1250ec8582d1SVladimir Oltean 
1251ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1252ec8582d1SVladimir Oltean 
1253ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
12548aa9ebccSVladimir Oltean }
12558aa9ebccSVladimir Oltean 
1256ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1257ad9f299aSVladimir Oltean 				     unsigned long *supported,
1258ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
1259ad9f299aSVladimir Oltean {
1260ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
1261ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
1262ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
1263ad9f299aSVladimir Oltean 	 */
1264ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1265ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1266ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1267ad9f299aSVladimir Oltean 
1268ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1269ad9f299aSVladimir Oltean 
127039710229SVladimir Oltean 	/* include/linux/phylink.h says:
127139710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
127239710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
127339710229SVladimir Oltean 	 */
127439710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
127539710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
127639710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
127739710229SVladimir Oltean 		return;
127839710229SVladimir Oltean 	}
127939710229SVladimir Oltean 
1280ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1281ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1282ad9f299aSVladimir Oltean 	 */
1283ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
1284ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
1285ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
1286ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
1287ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
1288ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1289ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1290ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
129156b63466SVladimir Oltean 	if (priv->info->supports_2500basex[port]) {
129256b63466SVladimir Oltean 		phylink_set(mask, 2500baseT_Full);
129356b63466SVladimir Oltean 		phylink_set(mask, 2500baseX_Full);
129456b63466SVladimir Oltean 	}
1295ad9f299aSVladimir Oltean 
1296ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1297ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1298ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1299ad9f299aSVladimir Oltean }
1300ad9f299aSVladimir Oltean 
130160f6053fSVladimir Oltean static int
130260f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
130360f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
130460f6053fSVladimir Oltean {
130560f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
130660f6053fSVladimir Oltean 	struct sja1105_table *table;
130760f6053fSVladimir Oltean 	int i;
130860f6053fSVladimir Oltean 
130960f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
131060f6053fSVladimir Oltean 	l2_lookup = table->entries;
131160f6053fSVladimir Oltean 
131260f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
131360f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
131460f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
131560f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
131660f6053fSVladimir Oltean 			return i;
131760f6053fSVladimir Oltean 
131860f6053fSVladimir Oltean 	return -1;
131960f6053fSVladimir Oltean }
132060f6053fSVladimir Oltean 
132160f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
132260f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
132360f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
132460f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
132560f6053fSVladimir Oltean  */
132660f6053fSVladimir Oltean static int
132760f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
132860f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
132960f6053fSVladimir Oltean 			  bool keep)
133060f6053fSVladimir Oltean {
133160f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
133260f6053fSVladimir Oltean 	struct sja1105_table *table;
133360f6053fSVladimir Oltean 	int rc, match;
133460f6053fSVladimir Oltean 
133560f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
133660f6053fSVladimir Oltean 
133760f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
133860f6053fSVladimir Oltean 	if (match < 0) {
133960f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
134060f6053fSVladimir Oltean 		if (!keep)
134160f6053fSVladimir Oltean 			return 0;
134260f6053fSVladimir Oltean 
134360f6053fSVladimir Oltean 		/* No match => new entry */
134460f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
134560f6053fSVladimir Oltean 		if (rc)
134660f6053fSVladimir Oltean 			return rc;
134760f6053fSVladimir Oltean 
134860f6053fSVladimir Oltean 		match = table->entry_count - 1;
134960f6053fSVladimir Oltean 	}
135060f6053fSVladimir Oltean 
135160f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
135260f6053fSVladimir Oltean 	l2_lookup = table->entries;
135360f6053fSVladimir Oltean 
135460f6053fSVladimir Oltean 	/* We have a match.
135560f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
135660f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
135760f6053fSVladimir Oltean 	 * which we update it).
135860f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
135960f6053fSVladimir Oltean 	 */
136060f6053fSVladimir Oltean 	if (keep) {
136160f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
136260f6053fSVladimir Oltean 		return 0;
136360f6053fSVladimir Oltean 	}
136460f6053fSVladimir Oltean 
136560f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
136660f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
136760f6053fSVladimir Oltean 	 */
136860f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
136960f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
137060f6053fSVladimir Oltean }
137160f6053fSVladimir Oltean 
1372291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1373291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1374291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1375291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1376291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1377291d1e72SVladimir Oltean  */
137809c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1379291d1e72SVladimir Oltean {
1380291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1381291d1e72SVladimir Oltean }
1382291d1e72SVladimir Oltean 
13839dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1384291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1385291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1386291d1e72SVladimir Oltean 					 int *last_unused)
1387291d1e72SVladimir Oltean {
1388291d1e72SVladimir Oltean 	int way;
1389291d1e72SVladimir Oltean 
1390291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1391291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1392291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1393291d1e72SVladimir Oltean 
1394291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1395291d1e72SVladimir Oltean 		 * into the return value
1396291d1e72SVladimir Oltean 		 */
1397291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1398291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1399291d1e72SVladimir Oltean 			if (last_unused)
1400291d1e72SVladimir Oltean 				*last_unused = way;
1401291d1e72SVladimir Oltean 			continue;
1402291d1e72SVladimir Oltean 		}
1403291d1e72SVladimir Oltean 
1404291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1405291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1406291d1e72SVladimir Oltean 			if (match)
1407291d1e72SVladimir Oltean 				*match = l2_lookup;
1408291d1e72SVladimir Oltean 			return way;
1409291d1e72SVladimir Oltean 		}
1410291d1e72SVladimir Oltean 	}
1411291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1412291d1e72SVladimir Oltean 	return -1;
1413291d1e72SVladimir Oltean }
1414291d1e72SVladimir Oltean 
14159dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1416291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1417291d1e72SVladimir Oltean {
1418291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1419291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1420291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1421291d1e72SVladimir Oltean 	int last_unused = -1;
142260f6053fSVladimir Oltean 	int bin, way, rc;
1423291d1e72SVladimir Oltean 
14249dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1425291d1e72SVladimir Oltean 
14269dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1427291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1428291d1e72SVladimir Oltean 	if (way >= 0) {
1429291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1430291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1431291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1432291d1e72SVladimir Oltean 		 */
1433291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1434291d1e72SVladimir Oltean 			return 0;
1435291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1436291d1e72SVladimir Oltean 	} else {
1437291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1438291d1e72SVladimir Oltean 
1439291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1440291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1441291d1e72SVladimir Oltean 		 */
1442291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1443291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1444291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1445291d1e72SVladimir Oltean 
1446291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1447291d1e72SVladimir Oltean 			way = last_unused;
1448291d1e72SVladimir Oltean 		} else {
1449291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1450291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1451291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1452291d1e72SVladimir Oltean 			 * distribution function:
1453291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1454291d1e72SVladimir Oltean 			 */
1455291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1456291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1457291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1458291d1e72SVladimir Oltean 				 bin, addr, way);
1459291d1e72SVladimir Oltean 			/* Evict entry */
1460291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1461291d1e72SVladimir Oltean 						     index, NULL, false);
1462291d1e72SVladimir Oltean 		}
1463291d1e72SVladimir Oltean 	}
1464291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1465291d1e72SVladimir Oltean 
146660f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1467291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1468291d1e72SVladimir Oltean 					  true);
146960f6053fSVladimir Oltean 	if (rc < 0)
147060f6053fSVladimir Oltean 		return rc;
147160f6053fSVladimir Oltean 
147260f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1473291d1e72SVladimir Oltean }
1474291d1e72SVladimir Oltean 
14759dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1476291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1477291d1e72SVladimir Oltean {
1478291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1479291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
148060f6053fSVladimir Oltean 	int index, bin, way, rc;
1481291d1e72SVladimir Oltean 	bool keep;
1482291d1e72SVladimir Oltean 
14839dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
14849dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1485291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1486291d1e72SVladimir Oltean 	if (way < 0)
1487291d1e72SVladimir Oltean 		return 0;
1488291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1489291d1e72SVladimir Oltean 
1490291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1491291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1492291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1493291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1494291d1e72SVladimir Oltean 	 */
1495291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
14967752e937SVladimir Oltean 
1497291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1498291d1e72SVladimir Oltean 		keep = true;
1499291d1e72SVladimir Oltean 	else
1500291d1e72SVladimir Oltean 		keep = false;
1501291d1e72SVladimir Oltean 
150260f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1503291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
150460f6053fSVladimir Oltean 	if (rc < 0)
150560f6053fSVladimir Oltean 		return rc;
150660f6053fSVladimir Oltean 
150760f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1508291d1e72SVladimir Oltean }
1509291d1e72SVladimir Oltean 
15109dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
15119dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
15129dfa6911SVladimir Oltean {
15131da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
15141da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15151da73821SVladimir Oltean 	int rc, i;
15161da73821SVladimir Oltean 
15171da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
15181da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
15191da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
15201da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
15211da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
15220fac6aa0SVladimir Oltean 	if (priv->vlan_aware) {
15231da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
15241da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
15256d7c7d94SVladimir Oltean 	} else {
15266d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
15276d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
15286d7c7d94SVladimir Oltean 	}
15291da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
15301da73821SVladimir Oltean 
15311da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
15321da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
15331da73821SVladimir Oltean 	if (rc == 0) {
15341da73821SVladimir Oltean 		/* Found and this port is already in the entry's
15351da73821SVladimir Oltean 		 * port mask => job done
15361da73821SVladimir Oltean 		 */
15371da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
15381da73821SVladimir Oltean 			return 0;
15391da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
15401da73821SVladimir Oltean 		 * found something.
15411da73821SVladimir Oltean 		 */
15421da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
15431da73821SVladimir Oltean 		goto skip_finding_an_index;
15441da73821SVladimir Oltean 	}
15451da73821SVladimir Oltean 
15461da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
15471da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
15481da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
15491da73821SVladimir Oltean 	 */
15501da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
15511da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
15521da73821SVladimir Oltean 						 i, NULL);
15531da73821SVladimir Oltean 		if (rc < 0)
15541da73821SVladimir Oltean 			break;
15551da73821SVladimir Oltean 	}
15561da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
15571da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
15581da73821SVladimir Oltean 		return -EINVAL;
15591da73821SVladimir Oltean 	}
156017ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
15611da73821SVladimir Oltean 	l2_lookup.index = i;
15621da73821SVladimir Oltean 
15631da73821SVladimir Oltean skip_finding_an_index:
156460f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
15651da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
15661da73821SVladimir Oltean 					  true);
156760f6053fSVladimir Oltean 	if (rc < 0)
156860f6053fSVladimir Oltean 		return rc;
156960f6053fSVladimir Oltean 
157060f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
15719dfa6911SVladimir Oltean }
15729dfa6911SVladimir Oltean 
15739dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
15749dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
15759dfa6911SVladimir Oltean {
15761da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
15771da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15781da73821SVladimir Oltean 	bool keep;
15791da73821SVladimir Oltean 	int rc;
15801da73821SVladimir Oltean 
15811da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
15821da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
15831da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
15841da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
15850fac6aa0SVladimir Oltean 	if (priv->vlan_aware) {
15861da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
15871da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
15886d7c7d94SVladimir Oltean 	} else {
15896d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
15906d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
15916d7c7d94SVladimir Oltean 	}
15921da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
15931da73821SVladimir Oltean 
15941da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
15951da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
15961da73821SVladimir Oltean 	if (rc < 0)
15971da73821SVladimir Oltean 		return 0;
15981da73821SVladimir Oltean 
15991da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
16001da73821SVladimir Oltean 
16011da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
16021da73821SVladimir Oltean 	 * or if we remove it completely.
16031da73821SVladimir Oltean 	 */
16041da73821SVladimir Oltean 	if (l2_lookup.destports)
16051da73821SVladimir Oltean 		keep = true;
16061da73821SVladimir Oltean 	else
16071da73821SVladimir Oltean 		keep = false;
16081da73821SVladimir Oltean 
160960f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
16101da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
161160f6053fSVladimir Oltean 	if (rc < 0)
161260f6053fSVladimir Oltean 		return rc;
161360f6053fSVladimir Oltean 
161460f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
16159dfa6911SVladimir Oltean }
16169dfa6911SVladimir Oltean 
16179dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
16189dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
16199dfa6911SVladimir Oltean {
16209dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1621b3ee526aSVladimir Oltean 
16226d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
16239dfa6911SVladimir Oltean }
16249dfa6911SVladimir Oltean 
16259dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
16269dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
16279dfa6911SVladimir Oltean {
16289dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16299dfa6911SVladimir Oltean 
1630b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
16319dfa6911SVladimir Oltean }
16329dfa6911SVladimir Oltean 
1633291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1634291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1635291d1e72SVladimir Oltean {
1636291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1637291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1638291d1e72SVladimir Oltean 	int i;
1639291d1e72SVladimir Oltean 
1640291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1641291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1642291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1643291d1e72SVladimir Oltean 		int rc;
1644291d1e72SVladimir Oltean 
1645291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1646291d1e72SVladimir Oltean 						 i, &l2_lookup);
1647291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1648def84604SVladimir Oltean 		if (rc == -ENOENT)
1649291d1e72SVladimir Oltean 			continue;
1650291d1e72SVladimir Oltean 		if (rc) {
1651291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1652291d1e72SVladimir Oltean 			return rc;
1653291d1e72SVladimir Oltean 		}
1654291d1e72SVladimir Oltean 
1655291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1656291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1657291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1658291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1659291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1660291d1e72SVladimir Oltean 		 */
1661291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1662291d1e72SVladimir Oltean 			continue;
16634d942354SVladimir Oltean 
16644d942354SVladimir Oltean 		/* We need to hide the FDB entry for unknown multicast */
16654d942354SVladimir Oltean 		if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST &&
16664d942354SVladimir Oltean 		    l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
16674d942354SVladimir Oltean 			continue;
16684d942354SVladimir Oltean 
1669291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
167093647594SVladimir Oltean 
16716d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
16720fac6aa0SVladimir Oltean 		if (!priv->vlan_aware)
16736d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
167417ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1675291d1e72SVladimir Oltean 	}
1676291d1e72SVladimir Oltean 	return 0;
1677291d1e72SVladimir Oltean }
1678291d1e72SVladimir Oltean 
1679a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port,
1680291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1681291d1e72SVladimir Oltean {
1682a52b2da7SVladimir Oltean 	return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1683291d1e72SVladimir Oltean }
1684291d1e72SVladimir Oltean 
1685291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1686291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1687291d1e72SVladimir Oltean {
1688291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1689291d1e72SVladimir Oltean }
1690291d1e72SVladimir Oltean 
16917f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration.
16927f7ccdeaSVladimir Oltean  * Flooding is configured between each {ingress, egress} port pair, and since
16937f7ccdeaSVladimir Oltean  * the bridge's semantics are those of "egress flooding", it means we must
16947f7ccdeaSVladimir Oltean  * enable flooding towards this port from all ingress ports that are in the
16957f7ccdeaSVladimir Oltean  * same forwarding domain.
16967f7ccdeaSVladimir Oltean  */
16977f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv)
16987f7ccdeaSVladimir Oltean {
16997f7ccdeaSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
17007f7ccdeaSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
17017f7ccdeaSVladimir Oltean 	int from, to, rc;
17027f7ccdeaSVladimir Oltean 
17037f7ccdeaSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
17047f7ccdeaSVladimir Oltean 
17057f7ccdeaSVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
17067f7ccdeaSVladimir Oltean 		u64 fl_domain = 0, bc_domain = 0;
17077f7ccdeaSVladimir Oltean 
17087f7ccdeaSVladimir Oltean 		for (to = 0; to < priv->ds->num_ports; to++) {
17097f7ccdeaSVladimir Oltean 			if (!sja1105_can_forward(l2_fwd, from, to))
17107f7ccdeaSVladimir Oltean 				continue;
17117f7ccdeaSVladimir Oltean 
17127f7ccdeaSVladimir Oltean 			if (priv->ucast_egress_floods & BIT(to))
17137f7ccdeaSVladimir Oltean 				fl_domain |= BIT(to);
17147f7ccdeaSVladimir Oltean 			if (priv->bcast_egress_floods & BIT(to))
17157f7ccdeaSVladimir Oltean 				bc_domain |= BIT(to);
17167f7ccdeaSVladimir Oltean 		}
17177f7ccdeaSVladimir Oltean 
17187f7ccdeaSVladimir Oltean 		/* Nothing changed, nothing to do */
17197f7ccdeaSVladimir Oltean 		if (l2_fwd[from].fl_domain == fl_domain &&
17207f7ccdeaSVladimir Oltean 		    l2_fwd[from].bc_domain == bc_domain)
17217f7ccdeaSVladimir Oltean 			continue;
17227f7ccdeaSVladimir Oltean 
17237f7ccdeaSVladimir Oltean 		l2_fwd[from].fl_domain = fl_domain;
17247f7ccdeaSVladimir Oltean 		l2_fwd[from].bc_domain = bc_domain;
17257f7ccdeaSVladimir Oltean 
17267f7ccdeaSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
17277f7ccdeaSVladimir Oltean 						  from, &l2_fwd[from], true);
17287f7ccdeaSVladimir Oltean 		if (rc < 0)
17297f7ccdeaSVladimir Oltean 			return rc;
17307f7ccdeaSVladimir Oltean 	}
17317f7ccdeaSVladimir Oltean 
17327f7ccdeaSVladimir Oltean 	return 0;
17337f7ccdeaSVladimir Oltean }
17347f7ccdeaSVladimir Oltean 
17358aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
17368aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
17378aa9ebccSVladimir Oltean {
17388aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
17398aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
17408aa9ebccSVladimir Oltean 	int i, rc;
17418aa9ebccSVladimir Oltean 
17428aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
17438aa9ebccSVladimir Oltean 
1744542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
17458aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
17468aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
17478aa9ebccSVladimir Oltean 		 */
17488aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
17498aa9ebccSVladimir Oltean 			continue;
17508aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
17518aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
17528aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
17538aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
17548aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
17558aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
17568aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
17578aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
17588aa9ebccSVladimir Oltean 		 */
17598aa9ebccSVladimir Oltean 		if (i == port)
17608aa9ebccSVladimir Oltean 			continue;
17618aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
17628aa9ebccSVladimir Oltean 			continue;
17638aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
17648aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
17658aa9ebccSVladimir Oltean 
17668aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
17678aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
17688aa9ebccSVladimir Oltean 		if (rc < 0)
17698aa9ebccSVladimir Oltean 			return rc;
17708aa9ebccSVladimir Oltean 	}
17718aa9ebccSVladimir Oltean 
17727f7ccdeaSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
17738aa9ebccSVladimir Oltean 					  port, &l2_fwd[port], true);
17747f7ccdeaSVladimir Oltean 	if (rc)
17757f7ccdeaSVladimir Oltean 		return rc;
17767f7ccdeaSVladimir Oltean 
1777cde8078eSVladimir Oltean 	rc = sja1105_commit_pvid(ds, port);
1778cde8078eSVladimir Oltean 	if (rc)
1779cde8078eSVladimir Oltean 		return rc;
1780cde8078eSVladimir Oltean 
17817f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
17828aa9ebccSVladimir Oltean }
17838aa9ebccSVladimir Oltean 
1784640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1785640f763fSVladimir Oltean 					 u8 state)
1786640f763fSVladimir Oltean {
1787640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1788640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1789640f763fSVladimir Oltean 
1790640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1791640f763fSVladimir Oltean 
1792640f763fSVladimir Oltean 	switch (state) {
1793640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1794640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1795640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1796640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1797640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1798640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1799640f763fSVladimir Oltean 		 */
1800640f763fSVladimir Oltean 		mac[port].ingress   = false;
1801640f763fSVladimir Oltean 		mac[port].egress    = false;
1802640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1803640f763fSVladimir Oltean 		break;
1804640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1805640f763fSVladimir Oltean 		mac[port].ingress   = true;
1806640f763fSVladimir Oltean 		mac[port].egress    = false;
1807640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1808640f763fSVladimir Oltean 		break;
1809640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1810640f763fSVladimir Oltean 		mac[port].ingress   = true;
1811640f763fSVladimir Oltean 		mac[port].egress    = false;
18124d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1813640f763fSVladimir Oltean 		break;
1814640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1815640f763fSVladimir Oltean 		mac[port].ingress   = true;
1816640f763fSVladimir Oltean 		mac[port].egress    = true;
18174d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1818640f763fSVladimir Oltean 		break;
1819640f763fSVladimir Oltean 	default:
1820640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1821640f763fSVladimir Oltean 		return;
1822640f763fSVladimir Oltean 	}
1823640f763fSVladimir Oltean 
1824640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1825640f763fSVladimir Oltean 				     &mac[port], true);
1826640f763fSVladimir Oltean }
1827640f763fSVladimir Oltean 
18288aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
18298aa9ebccSVladimir Oltean 			       struct net_device *br)
18308aa9ebccSVladimir Oltean {
18318aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
18328aa9ebccSVladimir Oltean }
18338aa9ebccSVladimir Oltean 
18348aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
18358aa9ebccSVladimir Oltean 				 struct net_device *br)
18368aa9ebccSVladimir Oltean {
18378aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
18388aa9ebccSVladimir Oltean }
18398aa9ebccSVladimir Oltean 
18404d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8)
18414d752508SVladimir Oltean 
18424d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
18434d752508SVladimir Oltean {
18444d752508SVladimir Oltean 	int i;
18454d752508SVladimir Oltean 
18464d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
18474d752508SVladimir Oltean 		if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
18484d752508SVladimir Oltean 			return i;
18494d752508SVladimir Oltean 
18504d752508SVladimir Oltean 	return -1;
18514d752508SVladimir Oltean }
18524d752508SVladimir Oltean 
18534d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
18544d752508SVladimir Oltean 				     int prio)
18554d752508SVladimir Oltean {
18564d752508SVladimir Oltean 	int i;
18574d752508SVladimir Oltean 
18584d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
18594d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
18604d752508SVladimir Oltean 
18614d752508SVladimir Oltean 		if (cbs->port == port && cbs->prio == prio) {
18624d752508SVladimir Oltean 			memset(cbs, 0, sizeof(*cbs));
18634d752508SVladimir Oltean 			return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
18644d752508SVladimir Oltean 							    i, cbs, true);
18654d752508SVladimir Oltean 		}
18664d752508SVladimir Oltean 	}
18674d752508SVladimir Oltean 
18684d752508SVladimir Oltean 	return 0;
18694d752508SVladimir Oltean }
18704d752508SVladimir Oltean 
18714d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
18724d752508SVladimir Oltean 				struct tc_cbs_qopt_offload *offload)
18734d752508SVladimir Oltean {
18744d752508SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18754d752508SVladimir Oltean 	struct sja1105_cbs_entry *cbs;
18764d752508SVladimir Oltean 	int index;
18774d752508SVladimir Oltean 
18784d752508SVladimir Oltean 	if (!offload->enable)
18794d752508SVladimir Oltean 		return sja1105_delete_cbs_shaper(priv, port, offload->queue);
18804d752508SVladimir Oltean 
18814d752508SVladimir Oltean 	index = sja1105_find_unused_cbs_shaper(priv);
18824d752508SVladimir Oltean 	if (index < 0)
18834d752508SVladimir Oltean 		return -ENOSPC;
18844d752508SVladimir Oltean 
18854d752508SVladimir Oltean 	cbs = &priv->cbs[index];
18864d752508SVladimir Oltean 	cbs->port = port;
18874d752508SVladimir Oltean 	cbs->prio = offload->queue;
18884d752508SVladimir Oltean 	/* locredit and sendslope are negative by definition. In hardware,
18894d752508SVladimir Oltean 	 * positive values must be provided, and the negative sign is implicit.
18904d752508SVladimir Oltean 	 */
18914d752508SVladimir Oltean 	cbs->credit_hi = offload->hicredit;
18924d752508SVladimir Oltean 	cbs->credit_lo = abs(offload->locredit);
18934d752508SVladimir Oltean 	/* User space is in kbits/sec, hardware in bytes/sec */
18944d752508SVladimir Oltean 	cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
18954d752508SVladimir Oltean 	cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
18964d752508SVladimir Oltean 	/* Convert the negative values from 64-bit 2's complement
18974d752508SVladimir Oltean 	 * to 32-bit 2's complement (for the case of 0x80000000 whose
18984d752508SVladimir Oltean 	 * negative is still negative).
18994d752508SVladimir Oltean 	 */
19004d752508SVladimir Oltean 	cbs->credit_lo &= GENMASK_ULL(31, 0);
19014d752508SVladimir Oltean 	cbs->send_slope &= GENMASK_ULL(31, 0);
19024d752508SVladimir Oltean 
19034d752508SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
19044d752508SVladimir Oltean 					    true);
19054d752508SVladimir Oltean }
19064d752508SVladimir Oltean 
19074d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv)
19084d752508SVladimir Oltean {
19094d752508SVladimir Oltean 	int rc = 0, i;
19104d752508SVladimir Oltean 
1911be7f62eeSVladimir Oltean 	/* The credit based shapers are only allocated if
1912be7f62eeSVladimir Oltean 	 * CONFIG_NET_SCH_CBS is enabled.
1913be7f62eeSVladimir Oltean 	 */
1914be7f62eeSVladimir Oltean 	if (!priv->cbs)
1915be7f62eeSVladimir Oltean 		return 0;
1916be7f62eeSVladimir Oltean 
19174d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
19184d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
19194d752508SVladimir Oltean 
19204d752508SVladimir Oltean 		if (!cbs->idle_slope && !cbs->send_slope)
19214d752508SVladimir Oltean 			continue;
19224d752508SVladimir Oltean 
19234d752508SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
19244d752508SVladimir Oltean 						  true);
19254d752508SVladimir Oltean 		if (rc)
19264d752508SVladimir Oltean 			break;
19274d752508SVladimir Oltean 	}
19284d752508SVladimir Oltean 
19294d752508SVladimir Oltean 	return rc;
19304d752508SVladimir Oltean }
19314d752508SVladimir Oltean 
19322eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
19332eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
19342eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
19352eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
19362eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
1937c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1938dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
19392eea1fa8SVladimir Oltean };
19402eea1fa8SVladimir Oltean 
19416666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
19426666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
19436666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
19446666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
19456666cebcSVladimir Oltean  * such that this operation is relatively seamless.
19466666cebcSVladimir Oltean  */
19472eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
19482eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
19496666cebcSVladimir Oltean {
19506cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
19516cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
195282760d7fSVladimir Oltean 	int speed_mbps[SJA1105_MAX_NUM_PORTS];
195384db00f2SVladimir Oltean 	u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0};
19546666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
19556cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
19566cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
19576cf99c13SVladimir Oltean 	s64 t12, t34;
19586666cebcSVladimir Oltean 	int rc, i;
19596cf99c13SVladimir Oltean 	s64 now;
19606666cebcSVladimir Oltean 
1961af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1962af580ae2SVladimir Oltean 
19636666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
19646666cebcSVladimir Oltean 
19658400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
19668400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
19678400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
19688400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
19696666cebcSVladimir Oltean 	 */
1970542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
19713ad1d171SVladimir Oltean 		u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1);
19723ad1d171SVladimir Oltean 
197341fed17fSVladimir Oltean 		speed_mbps[i] = sja1105_port_speed_to_ethtool(priv,
197441fed17fSVladimir Oltean 							      mac[i].speed);
197541fed17fSVladimir Oltean 		mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
19766666cebcSVladimir Oltean 
19773ad1d171SVladimir Oltean 		if (priv->xpcs[i])
19783ad1d171SVladimir Oltean 			bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr);
197984db00f2SVladimir Oltean 	}
1980ffe10e67SVladimir Oltean 
19816cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
19826cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
19836cf99c13SVladimir Oltean 
19846cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
198561c77533SVladimir Oltean 	if (rc < 0) {
198661c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
198761c77533SVladimir Oltean 		goto out;
198861c77533SVladimir Oltean 	}
19896cf99c13SVladimir Oltean 
19906666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
19916666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
199261c77533SVladimir Oltean 	if (rc < 0) {
199361c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
199461c77533SVladimir Oltean 		goto out;
199561c77533SVladimir Oltean 	}
19966cf99c13SVladimir Oltean 
19976cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
199861c77533SVladimir Oltean 	if (rc < 0) {
199961c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
200061c77533SVladimir Oltean 		goto out;
200161c77533SVladimir Oltean 	}
20026cf99c13SVladimir Oltean 
20036cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
20046cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
20056cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
20066cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
20076cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
20086cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
20096cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
20106cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
20116cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
20126cf99c13SVladimir Oltean 	now += (t34 - t12);
20136cf99c13SVladimir Oltean 
20146cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
20156cf99c13SVladimir Oltean 
20166cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
20176666cebcSVladimir Oltean 
20182eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
20192eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
20202eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
20212eea1fa8SVladimir Oltean 
20226666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
20236666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
20246666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
20256666cebcSVladimir Oltean 	 */
2026cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
2027c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
20286666cebcSVladimir Oltean 		if (rc < 0)
20296666cebcSVladimir Oltean 			goto out;
2030cb5a82d2SVladimir Oltean 	}
20316666cebcSVladimir Oltean 
2032542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
20333ad1d171SVladimir Oltean 		struct dw_xpcs *xpcs = priv->xpcs[i];
20343ad1d171SVladimir Oltean 		unsigned int mode;
203584db00f2SVladimir Oltean 
20368400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
20376666cebcSVladimir Oltean 		if (rc < 0)
20386666cebcSVladimir Oltean 			goto out;
2039ffe10e67SVladimir Oltean 
20403ad1d171SVladimir Oltean 		if (!xpcs)
204184db00f2SVladimir Oltean 			continue;
2042ffe10e67SVladimir Oltean 
20433ad1d171SVladimir Oltean 		if (bmcr[i] & BMCR_ANENABLE)
20443ad1d171SVladimir Oltean 			mode = MLO_AN_INBAND;
20453ad1d171SVladimir Oltean 		else if (priv->fixed_link[i])
20463ad1d171SVladimir Oltean 			mode = MLO_AN_FIXED;
20473ad1d171SVladimir Oltean 		else
20483ad1d171SVladimir Oltean 			mode = MLO_AN_PHY;
204984db00f2SVladimir Oltean 
20503ad1d171SVladimir Oltean 		rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode);
20513ad1d171SVladimir Oltean 		if (rc < 0)
20523ad1d171SVladimir Oltean 			goto out;
2053ffe10e67SVladimir Oltean 
20543ad1d171SVladimir Oltean 		if (!phylink_autoneg_inband(mode)) {
2055ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
2056ffe10e67SVladimir Oltean 
205756b63466SVladimir Oltean 			if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX)
205856b63466SVladimir Oltean 				speed = SPEED_2500;
205956b63466SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED1000)
2060ffe10e67SVladimir Oltean 				speed = SPEED_1000;
206184db00f2SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED100)
2062ffe10e67SVladimir Oltean 				speed = SPEED_100;
2063053d8ad1SVladimir Oltean 			else
2064ffe10e67SVladimir Oltean 				speed = SPEED_10;
2065ffe10e67SVladimir Oltean 
20663ad1d171SVladimir Oltean 			xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i],
20673ad1d171SVladimir Oltean 				     speed, DUPLEX_FULL);
2068ffe10e67SVladimir Oltean 		}
2069ffe10e67SVladimir Oltean 	}
20704d752508SVladimir Oltean 
20714d752508SVladimir Oltean 	rc = sja1105_reload_cbs(priv);
20724d752508SVladimir Oltean 	if (rc < 0)
20734d752508SVladimir Oltean 		goto out;
20746666cebcSVladimir Oltean out:
2075af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
2076af580ae2SVladimir Oltean 
20776666cebcSVladimir Oltean 	return rc;
20786666cebcSVladimir Oltean }
20796666cebcSVladimir Oltean 
20808aa9ebccSVladimir Oltean static enum dsa_tag_protocol
20814d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
20824d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
20838aa9ebccSVladimir Oltean {
20844913b8ebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
20854913b8ebSVladimir Oltean 
20864913b8ebSVladimir Oltean 	return priv->info->tag_proto;
20878aa9ebccSVladimir Oltean }
20888aa9ebccSVladimir Oltean 
2089070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
2090070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
2091070ca3bbSVladimir Oltean  * So a switch reset is required.
2092070ca3bbSVladimir Oltean  */
209389153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
209489153ed6SVladimir Oltean 			   struct netlink_ext_ack *extack)
20956666cebcSVladimir Oltean {
20966d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2097070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
20986666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2099070ca3bbSVladimir Oltean 	struct sja1105_table *table;
2100dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
2101070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
21026666cebcSVladimir Oltean 	int rc;
21036666cebcSVladimir Oltean 
2104dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
2105dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
210689153ed6SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
210789153ed6SVladimir Oltean 					   "Cannot change VLAN filtering with active VL rules");
2108dfacc5a2SVladimir Oltean 			return -EBUSY;
2109dfacc5a2SVladimir Oltean 		}
2110dfacc5a2SVladimir Oltean 	}
2111dfacc5a2SVladimir Oltean 
2112070ca3bbSVladimir Oltean 	if (enabled) {
21136666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
211454fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
211554fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
2116070ca3bbSVladimir Oltean 	} else {
21176666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
2118070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
2119070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
2120070ca3bbSVladimir Oltean 	}
2121070ca3bbSVladimir Oltean 
212238b5beeaSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
212338b5beeaSVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
212438b5beeaSVladimir Oltean 
212538b5beeaSVladimir Oltean 		if (enabled)
212638b5beeaSVladimir Oltean 			sp->xmit_tpid = priv->info->qinq_tpid;
212738b5beeaSVladimir Oltean 		else
212838b5beeaSVladimir Oltean 			sp->xmit_tpid = ETH_P_SJA1105;
212938b5beeaSVladimir Oltean 	}
213038b5beeaSVladimir Oltean 
21310fac6aa0SVladimir Oltean 	if (priv->vlan_aware == enabled)
2132cfa36b1fSVladimir Oltean 		return 0;
2133cfa36b1fSVladimir Oltean 
21340fac6aa0SVladimir Oltean 	priv->vlan_aware = enabled;
21357f14937fSVladimir Oltean 
2136070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2137070ca3bbSVladimir Oltean 	general_params = table->entries;
2138f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
213954fa49eeSVladimir Oltean 	general_params->tpid = tpid;
214054fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2141070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
214242824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
214342824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
214442824463SVladimir Oltean 	 */
214542824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
214642824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
2147070ca3bbSVladimir Oltean 
21486d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
21492cafa72eSVladimir Oltean 	 * No VLAN filtering (or best effort) => shared VLAN learning.
21506d7c7d94SVladimir Oltean 	 *
21516d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
21526d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
21536d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
21546d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
21556d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
21566d7c7d94SVladimir Oltean 	 * forwarding decision.
21576d7c7d94SVladimir Oltean 	 *
21586d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
21596d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
21606d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
21616d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
21626d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
21636d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
21646d7c7d94SVladimir Oltean 	 * (all frames get flooded).
21656d7c7d94SVladimir Oltean 	 */
21666d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
21676d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
21680fac6aa0SVladimir Oltean 	l2_lookup_params->shared_learn = !priv->vlan_aware;
2169aaa270c6SVladimir Oltean 
21706dfd23d3SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
21716dfd23d3SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
21726dfd23d3SVladimir Oltean 			continue;
21736dfd23d3SVladimir Oltean 
21746dfd23d3SVladimir Oltean 		rc = sja1105_commit_pvid(ds, port);
2175aef31718SVladimir Oltean 		if (rc)
2176aef31718SVladimir Oltean 			return rc;
21776dfd23d3SVladimir Oltean 	}
2178aef31718SVladimir Oltean 
21792eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
21806666cebcSVladimir Oltean 	if (rc)
218189153ed6SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype");
21826666cebcSVladimir Oltean 
21830fac6aa0SVladimir Oltean 	return rc;
21846666cebcSVladimir Oltean }
21856666cebcSVladimir Oltean 
21866dfd23d3SVladimir Oltean static int sja1105_vlan_add(struct sja1105_private *priv, int port, u16 vid,
21876dfd23d3SVladimir Oltean 			    u16 flags)
21885899ee36SVladimir Oltean {
21896dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
21906dfd23d3SVladimir Oltean 	struct sja1105_table *table;
21916dfd23d3SVladimir Oltean 	int match, rc;
21925899ee36SVladimir Oltean 
21936dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
21946dfd23d3SVladimir Oltean 
21956dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
21966dfd23d3SVladimir Oltean 	if (match < 0) {
21976dfd23d3SVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
21986dfd23d3SVladimir Oltean 		if (rc)
21996dfd23d3SVladimir Oltean 			return rc;
22006dfd23d3SVladimir Oltean 		match = table->entry_count - 1;
22016dfd23d3SVladimir Oltean 	}
22026dfd23d3SVladimir Oltean 
22036dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
22046dfd23d3SVladimir Oltean 	vlan = table->entries;
22056dfd23d3SVladimir Oltean 
22066dfd23d3SVladimir Oltean 	vlan[match].type_entry = SJA1110_VLAN_D_TAG;
22076dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
22086dfd23d3SVladimir Oltean 	vlan[match].vlan_bc |= BIT(port);
22096dfd23d3SVladimir Oltean 	vlan[match].vmemb_port |= BIT(port);
22106dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
22116dfd23d3SVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
22126dfd23d3SVladimir Oltean 	else
22136dfd23d3SVladimir Oltean 		vlan[match].tag_port |= BIT(port);
22146dfd23d3SVladimir Oltean 
22156dfd23d3SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
22166dfd23d3SVladimir Oltean 					    &vlan[match], true);
22176dfd23d3SVladimir Oltean }
22186dfd23d3SVladimir Oltean 
22196dfd23d3SVladimir Oltean static int sja1105_vlan_del(struct sja1105_private *priv, int port, u16 vid)
22206dfd23d3SVladimir Oltean {
22216dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
22226dfd23d3SVladimir Oltean 	struct sja1105_table *table;
22236dfd23d3SVladimir Oltean 	bool keep = true;
22246dfd23d3SVladimir Oltean 	int match, rc;
22256dfd23d3SVladimir Oltean 
22266dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
22276dfd23d3SVladimir Oltean 
22286dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
22296dfd23d3SVladimir Oltean 	/* Can't delete a missing entry. */
22306dfd23d3SVladimir Oltean 	if (match < 0)
22315899ee36SVladimir Oltean 		return 0;
22325899ee36SVladimir Oltean 
22336dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
22346dfd23d3SVladimir Oltean 	vlan = table->entries;
22356dfd23d3SVladimir Oltean 
22366dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
22376dfd23d3SVladimir Oltean 	vlan[match].vlan_bc &= ~BIT(port);
22386dfd23d3SVladimir Oltean 	vlan[match].vmemb_port &= ~BIT(port);
22396dfd23d3SVladimir Oltean 	/* Also unset tag_port, just so we don't have a confusing bitmap
22406dfd23d3SVladimir Oltean 	 * (no practical purpose).
2241b38e659dSVladimir Oltean 	 */
22426dfd23d3SVladimir Oltean 	vlan[match].tag_port &= ~BIT(port);
2243b38e659dSVladimir Oltean 
22446dfd23d3SVladimir Oltean 	/* If there's no port left as member of this VLAN,
22456dfd23d3SVladimir Oltean 	 * it's time for it to go.
22466dfd23d3SVladimir Oltean 	 */
22476dfd23d3SVladimir Oltean 	if (!vlan[match].vmemb_port)
22486dfd23d3SVladimir Oltean 		keep = false;
22495899ee36SVladimir Oltean 
22506dfd23d3SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
22516dfd23d3SVladimir Oltean 					  &vlan[match], keep);
22526dfd23d3SVladimir Oltean 	if (rc < 0)
22536dfd23d3SVladimir Oltean 		return rc;
22545899ee36SVladimir Oltean 
22556dfd23d3SVladimir Oltean 	if (!keep)
22566dfd23d3SVladimir Oltean 		return sja1105_table_delete_entry(table, match);
22575899ee36SVladimir Oltean 
22585899ee36SVladimir Oltean 	return 0;
22595899ee36SVladimir Oltean }
22605899ee36SVladimir Oltean 
22616dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_add(struct dsa_switch *ds, int port,
226231046a5fSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan,
226331046a5fSVladimir Oltean 				   struct netlink_ext_ack *extack)
22646666cebcSVladimir Oltean {
22656666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2266884be12fSVladimir Oltean 	u16 flags = vlan->flags;
22676666cebcSVladimir Oltean 	int rc;
22686666cebcSVladimir Oltean 
22690fac6aa0SVladimir Oltean 	/* Be sure to deny alterations to the configuration done by tag_8021q.
22701958d581SVladimir Oltean 	 */
22710fac6aa0SVladimir Oltean 	if (vid_is_dsa_8021q(vlan->vid)) {
227231046a5fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
227331046a5fSVladimir Oltean 				   "Range 1024-3071 reserved for dsa_8021q operation");
22741958d581SVladimir Oltean 		return -EBUSY;
22751958d581SVladimir Oltean 	}
22761958d581SVladimir Oltean 
2277884be12fSVladimir Oltean 	/* Always install bridge VLANs as egress-tagged on the CPU port. */
2278884be12fSVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
2279884be12fSVladimir Oltean 		flags = 0;
2280884be12fSVladimir Oltean 
2281884be12fSVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vlan->vid, flags);
22826dfd23d3SVladimir Oltean 	if (rc)
22831958d581SVladimir Oltean 		return rc;
2284ec5ae610SVladimir Oltean 
22856dfd23d3SVladimir Oltean 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
22866dfd23d3SVladimir Oltean 		priv->bridge_pvid[port] = vlan->vid;
2287ec5ae610SVladimir Oltean 
22886dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
22896666cebcSVladimir Oltean }
22906666cebcSVladimir Oltean 
22916dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port,
22926666cebcSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan)
22936666cebcSVladimir Oltean {
22946666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2295bef0746cSVladimir Oltean 	int rc;
22966666cebcSVladimir Oltean 
2297bef0746cSVladimir Oltean 	rc = sja1105_vlan_del(priv, port, vlan->vid);
2298bef0746cSVladimir Oltean 	if (rc)
2299bef0746cSVladimir Oltean 		return rc;
2300bef0746cSVladimir Oltean 
2301bef0746cSVladimir Oltean 	/* In case the pvid was deleted, make sure that untagged packets will
2302bef0746cSVladimir Oltean 	 * be dropped.
2303bef0746cSVladimir Oltean 	 */
2304bef0746cSVladimir Oltean 	return sja1105_commit_pvid(ds, port);
23056666cebcSVladimir Oltean }
23066666cebcSVladimir Oltean 
23075899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
23085899ee36SVladimir Oltean 				      u16 flags)
23095899ee36SVladimir Oltean {
23105899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
23115899ee36SVladimir Oltean 	int rc;
23125899ee36SVladimir Oltean 
23136dfd23d3SVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vid, flags);
23146dfd23d3SVladimir Oltean 	if (rc)
23155899ee36SVladimir Oltean 		return rc;
23165899ee36SVladimir Oltean 
23176dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_PVID)
23186dfd23d3SVladimir Oltean 		priv->tag_8021q_pvid[port] = vid;
23196dfd23d3SVladimir Oltean 
23206dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
23215899ee36SVladimir Oltean }
23225899ee36SVladimir Oltean 
23235899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
23245899ee36SVladimir Oltean {
23255899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
23265899ee36SVladimir Oltean 
23276dfd23d3SVladimir Oltean 	return sja1105_vlan_del(priv, port, vid);
23285899ee36SVladimir Oltean }
23295899ee36SVladimir Oltean 
23304fbc08bdSVladimir Oltean static int sja1105_prechangeupper(struct dsa_switch *ds, int port,
23314fbc08bdSVladimir Oltean 				  struct netdev_notifier_changeupper_info *info)
23324fbc08bdSVladimir Oltean {
23334fbc08bdSVladimir Oltean 	struct netlink_ext_ack *extack = info->info.extack;
23344fbc08bdSVladimir Oltean 	struct net_device *upper = info->upper_dev;
233519fa937aSVladimir Oltean 	struct dsa_switch_tree *dst = ds->dst;
233619fa937aSVladimir Oltean 	struct dsa_port *dp;
23374fbc08bdSVladimir Oltean 
23384fbc08bdSVladimir Oltean 	if (is_vlan_dev(upper)) {
23394fbc08bdSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "8021q uppers are not supported");
23404fbc08bdSVladimir Oltean 		return -EBUSY;
23414fbc08bdSVladimir Oltean 	}
23424fbc08bdSVladimir Oltean 
234319fa937aSVladimir Oltean 	if (netif_is_bridge_master(upper)) {
234419fa937aSVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
234519fa937aSVladimir Oltean 			if (dp->bridge_dev && dp->bridge_dev != upper &&
234619fa937aSVladimir Oltean 			    br_vlan_enabled(dp->bridge_dev)) {
234719fa937aSVladimir Oltean 				NL_SET_ERR_MSG_MOD(extack,
234819fa937aSVladimir Oltean 						   "Only one VLAN-aware bridge is supported");
234919fa937aSVladimir Oltean 				return -EBUSY;
235019fa937aSVladimir Oltean 			}
235119fa937aSVladimir Oltean 		}
235219fa937aSVladimir Oltean 	}
235319fa937aSVladimir Oltean 
23544fbc08bdSVladimir Oltean 	return 0;
23554fbc08bdSVladimir Oltean }
23564fbc08bdSVladimir Oltean 
23578aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
23588aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
23598aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
23608aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
23618aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
23628aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
23638aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
23648aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
23658aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
23668aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
23678aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
23688aa9ebccSVladimir Oltean  */
23698aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
23708aa9ebccSVladimir Oltean {
23718aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
23728aa9ebccSVladimir Oltean 	int rc;
23738aa9ebccSVladimir Oltean 
23745d645df9SVladimir Oltean 	rc = sja1105_parse_dt(priv);
23758aa9ebccSVladimir Oltean 	if (rc < 0) {
23768aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
23778aa9ebccSVladimir Oltean 		return rc;
23788aa9ebccSVladimir Oltean 	}
2379f5b8631cSVladimir Oltean 
2380f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
2381f5b8631cSVladimir Oltean 	 * and we can't apply them.
2382f5b8631cSVladimir Oltean 	 */
238329afb83aSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv);
2384f5b8631cSVladimir Oltean 	if (rc < 0) {
2385f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
2386f5b8631cSVladimir Oltean 		return rc;
2387f5b8631cSVladimir Oltean 	}
2388f5b8631cSVladimir Oltean 
238961c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
2390bb77f36aSVladimir Oltean 	if (rc < 0) {
2391bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
2392bb77f36aSVladimir Oltean 		return rc;
2393bb77f36aSVladimir Oltean 	}
23945a8f0974SVladimir Oltean 
23955a8f0974SVladimir Oltean 	rc = sja1105_mdiobus_register(ds);
23965a8f0974SVladimir Oltean 	if (rc < 0) {
23975a8f0974SVladimir Oltean 		dev_err(ds->dev, "Failed to register MDIO bus: %pe\n",
23985a8f0974SVladimir Oltean 			ERR_PTR(rc));
23995a8f0974SVladimir Oltean 		goto out_ptp_clock_unregister;
24005a8f0974SVladimir Oltean 	}
24015a8f0974SVladimir Oltean 
2402cb5a82d2SVladimir Oltean 	if (priv->info->disable_microcontroller) {
2403cb5a82d2SVladimir Oltean 		rc = priv->info->disable_microcontroller(priv);
2404cb5a82d2SVladimir Oltean 		if (rc < 0) {
2405cb5a82d2SVladimir Oltean 			dev_err(ds->dev,
2406cb5a82d2SVladimir Oltean 				"Failed to disable microcontroller: %pe\n",
2407cb5a82d2SVladimir Oltean 				ERR_PTR(rc));
2408cb5a82d2SVladimir Oltean 			goto out_mdiobus_unregister;
2409cb5a82d2SVladimir Oltean 		}
2410cb5a82d2SVladimir Oltean 	}
2411cb5a82d2SVladimir Oltean 
24128aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
24135d645df9SVladimir Oltean 	rc = sja1105_static_config_load(priv);
24148aa9ebccSVladimir Oltean 	if (rc < 0) {
24158aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
24165a8f0974SVladimir Oltean 		goto out_mdiobus_unregister;
24178aa9ebccSVladimir Oltean 	}
2418cb5a82d2SVladimir Oltean 
24198aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
2420cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
2421c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
24228aa9ebccSVladimir Oltean 		if (rc < 0) {
2423cb5a82d2SVladimir Oltean 			dev_err(ds->dev,
2424cb5a82d2SVladimir Oltean 				"Failed to configure MII clocking: %pe\n",
2425cb5a82d2SVladimir Oltean 				ERR_PTR(rc));
2426cec279a8SVladimir Oltean 			goto out_static_config_free;
24278aa9ebccSVladimir Oltean 		}
2428cb5a82d2SVladimir Oltean 	}
2429cb5a82d2SVladimir Oltean 
24306666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
24316666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
24326666cebcSVladimir Oltean 	 * EtherType is.
24336666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
24346666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
24356666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
24366666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
24376666cebcSVladimir Oltean 	 */
24386666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
2439884be12fSVladimir Oltean 	ds->untag_bridge_pvid = true;
2440b6ad86e6SVladimir Oltean 	/* tag_8021q has 3 bits for the VBID, and the value 0 is reserved */
2441b6ad86e6SVladimir Oltean 	ds->num_fwd_offloading_bridges = 7;
24428aa9ebccSVladimir Oltean 
24435f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
24445f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
24455f06c63bSVladimir Oltean 
2446c279c726SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
2447c279c726SVladimir Oltean 
24480a7bdbc2SVladimir Oltean 	rc = sja1105_devlink_setup(ds);
24492cafa72eSVladimir Oltean 	if (rc < 0)
2450cec279a8SVladimir Oltean 		goto out_static_config_free;
24512cafa72eSVladimir Oltean 
2452bbed0bbdSVladimir Oltean 	rtnl_lock();
2453328621f6SVladimir Oltean 	rc = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q));
2454bbed0bbdSVladimir Oltean 	rtnl_unlock();
2455cec279a8SVladimir Oltean 	if (rc)
2456cec279a8SVladimir Oltean 		goto out_devlink_teardown;
2457cec279a8SVladimir Oltean 
2458cec279a8SVladimir Oltean 	return 0;
2459cec279a8SVladimir Oltean 
2460cec279a8SVladimir Oltean out_devlink_teardown:
2461cec279a8SVladimir Oltean 	sja1105_devlink_teardown(ds);
24625a8f0974SVladimir Oltean out_mdiobus_unregister:
24635a8f0974SVladimir Oltean 	sja1105_mdiobus_unregister(ds);
2464cec279a8SVladimir Oltean out_ptp_clock_unregister:
2465cec279a8SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
2466cec279a8SVladimir Oltean out_static_config_free:
2467cec279a8SVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2468bbed0bbdSVladimir Oltean 
2469bbed0bbdSVladimir Oltean 	return rc;
2470227d07a0SVladimir Oltean }
2471227d07a0SVladimir Oltean 
2472f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
2473f3097be2SVladimir Oltean {
2474f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2475a68578c2SVladimir Oltean 	int port;
2476a68578c2SVladimir Oltean 
2477328621f6SVladimir Oltean 	rtnl_lock();
2478328621f6SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
2479328621f6SVladimir Oltean 	rtnl_unlock();
2480328621f6SVladimir Oltean 
2481542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
2482a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2483a68578c2SVladimir Oltean 
2484a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2485a68578c2SVladimir Oltean 			continue;
2486a68578c2SVladimir Oltean 
248752c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
2488a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
2489a68578c2SVladimir Oltean 	}
2490f3097be2SVladimir Oltean 
24910a7bdbc2SVladimir Oltean 	sja1105_devlink_teardown(ds);
2492a6af7763SVladimir Oltean 	sja1105_flower_teardown(ds);
2493317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
249461c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
24956cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2496f3097be2SVladimir Oltean }
2497f3097be2SVladimir Oltean 
2498a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
2499a68578c2SVladimir Oltean {
2500a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2501a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
2502a68578c2SVladimir Oltean 
2503a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2504a68578c2SVladimir Oltean 		return;
2505a68578c2SVladimir Oltean 
2506a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
2507a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
2508a68578c2SVladimir Oltean }
2509a68578c2SVladimir Oltean 
2510227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
251147ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2512227d07a0SVladimir Oltean {
2513227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2514227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2515227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2516227d07a0SVladimir Oltean 	int timeout = 10;
2517227d07a0SVladimir Oltean 	int rc;
2518227d07a0SVladimir Oltean 
2519227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2520227d07a0SVladimir Oltean 
2521227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2522227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2523227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
252447ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
252547ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2526227d07a0SVladimir Oltean 
2527227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2528227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2529227d07a0SVladimir Oltean 	if (rc < 0) {
2530227d07a0SVladimir Oltean 		kfree_skb(skb);
2531227d07a0SVladimir Oltean 		return rc;
2532227d07a0SVladimir Oltean 	}
2533227d07a0SVladimir Oltean 
2534227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
253568bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2536227d07a0SVladimir Oltean 
2537227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2538227d07a0SVladimir Oltean 	do {
2539227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2540227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2541227d07a0SVladimir Oltean 		if (rc < 0) {
2542227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2543227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2544227d07a0SVladimir Oltean 			continue;
2545227d07a0SVladimir Oltean 		}
2546227d07a0SVladimir Oltean 
2547227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2548227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2549227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2550227d07a0SVladimir Oltean 		 */
2551227d07a0SVladimir Oltean 		cpu_relax();
2552227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2553227d07a0SVladimir Oltean 
2554227d07a0SVladimir Oltean 	if (!timeout) {
2555227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2556227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
25572a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
25582a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2559227d07a0SVladimir Oltean 		 */
2560227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2561227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2562227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2563227d07a0SVladimir Oltean 	}
2564227d07a0SVladimir Oltean 
2565227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2566227d07a0SVladimir Oltean }
2567227d07a0SVladimir Oltean 
2568a68578c2SVladimir Oltean #define work_to_port(work) \
2569a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
2570a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
2571a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
2572a68578c2SVladimir Oltean 
2573227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2574227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2575227d07a0SVladimir Oltean  * lock on the bus)
2576227d07a0SVladimir Oltean  */
2577a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2578227d07a0SVladimir Oltean {
2579a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
2580a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
2581a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
2582a68578c2SVladimir Oltean 	int port = sp - priv->ports;
2583a68578c2SVladimir Oltean 	struct sk_buff *skb;
2584a68578c2SVladimir Oltean 
2585a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
2586c4b364ceSYangbo Lu 		struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
2587227d07a0SVladimir Oltean 
2588227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
2589227d07a0SVladimir Oltean 
2590a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
2591a68578c2SVladimir Oltean 
259247ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
2593a68578c2SVladimir Oltean 		if (clone)
2594a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
2595227d07a0SVladimir Oltean 
2596227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
2597a68578c2SVladimir Oltean 	}
25988aa9ebccSVladimir Oltean }
25998aa9ebccSVladimir Oltean 
26008456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
26018456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
26028456721dSVladimir Oltean  */
26038456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
26048456721dSVladimir Oltean 				   unsigned int ageing_time)
26058456721dSVladimir Oltean {
26068456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
26078456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
26088456721dSVladimir Oltean 	struct sja1105_table *table;
26098456721dSVladimir Oltean 	unsigned int maxage;
26108456721dSVladimir Oltean 
26118456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
26128456721dSVladimir Oltean 	l2_lookup_params = table->entries;
26138456721dSVladimir Oltean 
26148456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
26158456721dSVladimir Oltean 
26168456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
26178456721dSVladimir Oltean 		return 0;
26188456721dSVladimir Oltean 
26198456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
26208456721dSVladimir Oltean 
26212eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
26228456721dSVladimir Oltean }
26238456721dSVladimir Oltean 
2624c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
2625c279c726SVladimir Oltean {
2626c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2627c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2628c279c726SVladimir Oltean 
2629c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
2630c279c726SVladimir Oltean 
2631c279c726SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
2632c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
2633c279c726SVladimir Oltean 
2634c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2635c279c726SVladimir Oltean 
2636a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
2637c279c726SVladimir Oltean 		return 0;
2638c279c726SVladimir Oltean 
2639a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
2640c279c726SVladimir Oltean 
2641c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2642c279c726SVladimir Oltean }
2643c279c726SVladimir Oltean 
2644c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
2645c279c726SVladimir Oltean {
2646c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
2647c279c726SVladimir Oltean }
2648c279c726SVladimir Oltean 
2649317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2650317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2651317ab5b8SVladimir Oltean 				 void *type_data)
2652317ab5b8SVladimir Oltean {
2653317ab5b8SVladimir Oltean 	switch (type) {
2654317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2655317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
26564d752508SVladimir Oltean 	case TC_SETUP_QDISC_CBS:
26574d752508SVladimir Oltean 		return sja1105_setup_tc_cbs(ds, port, type_data);
2658317ab5b8SVladimir Oltean 	default:
2659317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2660317ab5b8SVladimir Oltean 	}
2661317ab5b8SVladimir Oltean }
2662317ab5b8SVladimir Oltean 
2663511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2664511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2665511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2666511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2667511e6ca0SVladimir Oltean  * mirroring rule that references it.
2668511e6ca0SVladimir Oltean  */
2669511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2670511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2671511e6ca0SVladimir Oltean {
2672511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2673511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2674542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2675511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2676511e6ca0SVladimir Oltean 	bool already_enabled;
2677511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2678511e6ca0SVladimir Oltean 	int rc;
2679511e6ca0SVladimir Oltean 
2680511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2681511e6ca0SVladimir Oltean 	general_params = table->entries;
2682511e6ca0SVladimir Oltean 
2683511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2684511e6ca0SVladimir Oltean 
2685542043e9SVladimir Oltean 	already_enabled = (general_params->mirr_port != ds->num_ports);
2686511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2687511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2688511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2689511e6ca0SVladimir Oltean 			general_params->mirr_port);
2690511e6ca0SVladimir Oltean 		return -EBUSY;
2691511e6ca0SVladimir Oltean 	}
2692511e6ca0SVladimir Oltean 
2693511e6ca0SVladimir Oltean 	new_mirr_port = to;
2694511e6ca0SVladimir Oltean 	if (!enabled) {
2695511e6ca0SVladimir Oltean 		bool keep = false;
2696511e6ca0SVladimir Oltean 		int port;
2697511e6ca0SVladimir Oltean 
2698511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2699542043e9SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
2700511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2701511e6ca0SVladimir Oltean 				keep = true;
2702511e6ca0SVladimir Oltean 				break;
2703511e6ca0SVladimir Oltean 			}
2704511e6ca0SVladimir Oltean 		}
2705511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2706511e6ca0SVladimir Oltean 		if (!keep)
2707542043e9SVladimir Oltean 			new_mirr_port = ds->num_ports;
2708511e6ca0SVladimir Oltean 	}
2709511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2710511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2711511e6ca0SVladimir Oltean 
2712511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2713511e6ca0SVladimir Oltean 						  0, general_params, true);
2714511e6ca0SVladimir Oltean 		if (rc < 0)
2715511e6ca0SVladimir Oltean 			return rc;
2716511e6ca0SVladimir Oltean 	}
2717511e6ca0SVladimir Oltean 
2718511e6ca0SVladimir Oltean 	if (ingress)
2719511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2720511e6ca0SVladimir Oltean 	else
2721511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2722511e6ca0SVladimir Oltean 
2723511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2724511e6ca0SVladimir Oltean 					    &mac[from], true);
2725511e6ca0SVladimir Oltean }
2726511e6ca0SVladimir Oltean 
2727511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2728511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2729511e6ca0SVladimir Oltean 			      bool ingress)
2730511e6ca0SVladimir Oltean {
2731511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2732511e6ca0SVladimir Oltean 				    ingress, true);
2733511e6ca0SVladimir Oltean }
2734511e6ca0SVladimir Oltean 
2735511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2736511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2737511e6ca0SVladimir Oltean {
2738511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2739511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2740511e6ca0SVladimir Oltean }
2741511e6ca0SVladimir Oltean 
2742a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
2743a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
2744a7cc081cSVladimir Oltean {
2745a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2746a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2747a7cc081cSVladimir Oltean 
2748a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2749a7cc081cSVladimir Oltean 
2750a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
2751a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
2752a7cc081cSVladimir Oltean 	 * bytes.
2753a7cc081cSVladimir Oltean 	 */
2754a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
2755a7cc081cSVladimir Oltean 				      1000000);
27565f035af7SPo Liu 	policing[port].smax = policer->burst;
2757a7cc081cSVladimir Oltean 
2758a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2759a7cc081cSVladimir Oltean }
2760a7cc081cSVladimir Oltean 
2761a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
2762a7cc081cSVladimir Oltean {
2763a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2764a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2765a7cc081cSVladimir Oltean 
2766a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2767a7cc081cSVladimir Oltean 
2768a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
2769a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
2770a7cc081cSVladimir Oltean 
2771a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2772a7cc081cSVladimir Oltean }
2773a7cc081cSVladimir Oltean 
27744d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port,
27754d942354SVladimir Oltean 				     bool enabled)
27764d942354SVladimir Oltean {
27774d942354SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
27784d942354SVladimir Oltean 	int rc;
27794d942354SVladimir Oltean 
27804d942354SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
27814d942354SVladimir Oltean 
27824c44fc5eSVladimir Oltean 	mac[port].dyn_learn = enabled;
27834d942354SVladimir Oltean 
27844d942354SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
27854d942354SVladimir Oltean 					  &mac[port], true);
27864d942354SVladimir Oltean 	if (rc)
27874d942354SVladimir Oltean 		return rc;
27884d942354SVladimir Oltean 
27894d942354SVladimir Oltean 	if (enabled)
27904d942354SVladimir Oltean 		priv->learn_ena |= BIT(port);
27914d942354SVladimir Oltean 	else
27924d942354SVladimir Oltean 		priv->learn_ena &= ~BIT(port);
27934d942354SVladimir Oltean 
27944d942354SVladimir Oltean 	return 0;
27954d942354SVladimir Oltean }
27964d942354SVladimir Oltean 
27974d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to,
27984d942354SVladimir Oltean 					  struct switchdev_brport_flags flags)
27994d942354SVladimir Oltean {
28004d942354SVladimir Oltean 	if (flags.mask & BR_FLOOD) {
28014d942354SVladimir Oltean 		if (flags.val & BR_FLOOD)
28027f7ccdeaSVladimir Oltean 			priv->ucast_egress_floods |= BIT(to);
28034d942354SVladimir Oltean 		else
28046a5166e0SVladimir Oltean 			priv->ucast_egress_floods &= ~BIT(to);
28054d942354SVladimir Oltean 	}
28067f7ccdeaSVladimir Oltean 
28074d942354SVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD) {
28084d942354SVladimir Oltean 		if (flags.val & BR_BCAST_FLOOD)
28097f7ccdeaSVladimir Oltean 			priv->bcast_egress_floods |= BIT(to);
28104d942354SVladimir Oltean 		else
28116a5166e0SVladimir Oltean 			priv->bcast_egress_floods &= ~BIT(to);
28124d942354SVladimir Oltean 	}
28134d942354SVladimir Oltean 
28147f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
28154d942354SVladimir Oltean }
28164d942354SVladimir Oltean 
28174d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to,
28184d942354SVladimir Oltean 				    struct switchdev_brport_flags flags,
28194d942354SVladimir Oltean 				    struct netlink_ext_ack *extack)
28204d942354SVladimir Oltean {
28214d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
28224d942354SVladimir Oltean 	struct sja1105_table *table;
28234d942354SVladimir Oltean 	int match;
28244d942354SVladimir Oltean 
28254d942354SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
28264d942354SVladimir Oltean 	l2_lookup = table->entries;
28274d942354SVladimir Oltean 
28284d942354SVladimir Oltean 	for (match = 0; match < table->entry_count; match++)
28294d942354SVladimir Oltean 		if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST &&
28304d942354SVladimir Oltean 		    l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
28314d942354SVladimir Oltean 			break;
28324d942354SVladimir Oltean 
28334d942354SVladimir Oltean 	if (match == table->entry_count) {
28344d942354SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
28354d942354SVladimir Oltean 				   "Could not find FDB entry for unknown multicast");
28364d942354SVladimir Oltean 		return -ENOSPC;
28374d942354SVladimir Oltean 	}
28384d942354SVladimir Oltean 
28394d942354SVladimir Oltean 	if (flags.val & BR_MCAST_FLOOD)
28404d942354SVladimir Oltean 		l2_lookup[match].destports |= BIT(to);
28414d942354SVladimir Oltean 	else
28424d942354SVladimir Oltean 		l2_lookup[match].destports &= ~BIT(to);
28434d942354SVladimir Oltean 
28444d942354SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
28454d942354SVladimir Oltean 					    l2_lookup[match].index,
28464d942354SVladimir Oltean 					    &l2_lookup[match],
28474d942354SVladimir Oltean 					    true);
28484d942354SVladimir Oltean }
28494d942354SVladimir Oltean 
28504d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port,
28514d942354SVladimir Oltean 					 struct switchdev_brport_flags flags,
28524d942354SVladimir Oltean 					 struct netlink_ext_ack *extack)
28534d942354SVladimir Oltean {
28544d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
28554d942354SVladimir Oltean 
28564d942354SVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
28574d942354SVladimir Oltean 			   BR_BCAST_FLOOD))
28584d942354SVladimir Oltean 		return -EINVAL;
28594d942354SVladimir Oltean 
28604d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) &&
28614d942354SVladimir Oltean 	    !priv->info->can_limit_mcast_flood) {
28624d942354SVladimir Oltean 		bool multicast = !!(flags.val & BR_MCAST_FLOOD);
28634d942354SVladimir Oltean 		bool unicast = !!(flags.val & BR_FLOOD);
28644d942354SVladimir Oltean 
28654d942354SVladimir Oltean 		if (unicast != multicast) {
28664d942354SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
28674d942354SVladimir Oltean 					   "This chip cannot configure multicast flooding independently of unicast");
28684d942354SVladimir Oltean 			return -EINVAL;
28694d942354SVladimir Oltean 		}
28704d942354SVladimir Oltean 	}
28714d942354SVladimir Oltean 
28724d942354SVladimir Oltean 	return 0;
28734d942354SVladimir Oltean }
28744d942354SVladimir Oltean 
28754d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port,
28764d942354SVladimir Oltean 				     struct switchdev_brport_flags flags,
28774d942354SVladimir Oltean 				     struct netlink_ext_ack *extack)
28784d942354SVladimir Oltean {
28794d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
28804d942354SVladimir Oltean 	int rc;
28814d942354SVladimir Oltean 
28824d942354SVladimir Oltean 	if (flags.mask & BR_LEARNING) {
28834d942354SVladimir Oltean 		bool learn_ena = !!(flags.val & BR_LEARNING);
28844d942354SVladimir Oltean 
28854d942354SVladimir Oltean 		rc = sja1105_port_set_learning(priv, port, learn_ena);
28864d942354SVladimir Oltean 		if (rc)
28874d942354SVladimir Oltean 			return rc;
28884d942354SVladimir Oltean 	}
28894d942354SVladimir Oltean 
28904d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) {
28914d942354SVladimir Oltean 		rc = sja1105_port_ucast_bcast_flood(priv, port, flags);
28924d942354SVladimir Oltean 		if (rc)
28934d942354SVladimir Oltean 			return rc;
28944d942354SVladimir Oltean 	}
28954d942354SVladimir Oltean 
28964d942354SVladimir Oltean 	/* For chips that can't offload BR_MCAST_FLOOD independently, there
28974d942354SVladimir Oltean 	 * is nothing to do here, we ensured the configuration is in sync by
28984d942354SVladimir Oltean 	 * offloading BR_FLOOD.
28994d942354SVladimir Oltean 	 */
29004d942354SVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) {
29014d942354SVladimir Oltean 		rc = sja1105_port_mcast_flood(priv, port, flags,
29024d942354SVladimir Oltean 					      extack);
29034d942354SVladimir Oltean 		if (rc)
29044d942354SVladimir Oltean 			return rc;
29054d942354SVladimir Oltean 	}
29064d942354SVladimir Oltean 
29074d942354SVladimir Oltean 	return 0;
29084d942354SVladimir Oltean }
29094d942354SVladimir Oltean 
29108aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
29118aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
29128aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
2913f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
29148456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
2915c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
2916c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
2917ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
2918af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
29198400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
29208400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
292152c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
292252c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
292352c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
2924bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
2925a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
2926291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
2927291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
2928291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
29298aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
29308aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
29314d942354SVladimir Oltean 	.port_pre_bridge_flags	= sja1105_port_pre_bridge_flags,
29324d942354SVladimir Oltean 	.port_bridge_flags	= sja1105_port_bridge_flags,
2933640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
29346666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
29356dfd23d3SVladimir Oltean 	.port_vlan_add		= sja1105_bridge_vlan_add,
29366dfd23d3SVladimir Oltean 	.port_vlan_del		= sja1105_bridge_vlan_del,
2937291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
2938291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
2939a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
2940a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
2941f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
294247ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
2943317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
2944511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
2945511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
2946a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
2947a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
2948a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
2949a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
2950834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
2951ff4cf8eaSVladimir Oltean 	.devlink_info_get	= sja1105_devlink_info_get,
29525da11eb4SVladimir Oltean 	.tag_8021q_vlan_add	= sja1105_dsa_8021q_vlan_add,
29535da11eb4SVladimir Oltean 	.tag_8021q_vlan_del	= sja1105_dsa_8021q_vlan_del,
29544fbc08bdSVladimir Oltean 	.port_prechangeupper	= sja1105_prechangeupper,
2955b6ad86e6SVladimir Oltean 	.port_bridge_tx_fwd_offload = dsa_tag_8021q_bridge_tx_fwd_offload,
2956b6ad86e6SVladimir Oltean 	.port_bridge_tx_fwd_unoffload = dsa_tag_8021q_bridge_tx_fwd_unoffload,
29578aa9ebccSVladimir Oltean };
29588aa9ebccSVladimir Oltean 
29590b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[];
29600b0e2997SVladimir Oltean 
29618aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
29628aa9ebccSVladimir Oltean {
29638aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
29648aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
29658aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
29660b0e2997SVladimir Oltean 	const struct of_device_id *match;
2967dff79620SVladimir Oltean 	u32 device_id;
29688aa9ebccSVladimir Oltean 	u64 part_no;
29698aa9ebccSVladimir Oltean 	int rc;
29708aa9ebccSVladimir Oltean 
297134d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
297234d76e9fSVladimir Oltean 			      NULL);
29738aa9ebccSVladimir Oltean 	if (rc < 0)
29748aa9ebccSVladimir Oltean 		return rc;
29758aa9ebccSVladimir Oltean 
29761bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
29771bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
29788aa9ebccSVladimir Oltean 	if (rc < 0)
29798aa9ebccSVladimir Oltean 		return rc;
29808aa9ebccSVladimir Oltean 
29818aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
29828aa9ebccSVladimir Oltean 
29835978fac0SNathan Chancellor 	for (match = sja1105_dt_ids; match->compatible[0]; match++) {
29840b0e2997SVladimir Oltean 		const struct sja1105_info *info = match->data;
29850b0e2997SVladimir Oltean 
29860b0e2997SVladimir Oltean 		/* Is what's been probed in our match table at all? */
29870b0e2997SVladimir Oltean 		if (info->device_id != device_id || info->part_no != part_no)
29880b0e2997SVladimir Oltean 			continue;
29890b0e2997SVladimir Oltean 
29900b0e2997SVladimir Oltean 		/* But is it what's in the device tree? */
29910b0e2997SVladimir Oltean 		if (priv->info->device_id != device_id ||
29920b0e2997SVladimir Oltean 		    priv->info->part_no != part_no) {
29930b0e2997SVladimir Oltean 			dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
29940b0e2997SVladimir Oltean 				 priv->info->name, info->name);
29950b0e2997SVladimir Oltean 			/* It isn't. No problem, pick that up. */
29960b0e2997SVladimir Oltean 			priv->info = info;
29978aa9ebccSVladimir Oltean 		}
29988aa9ebccSVladimir Oltean 
29998aa9ebccSVladimir Oltean 		return 0;
30008aa9ebccSVladimir Oltean 	}
30018aa9ebccSVladimir Oltean 
30020b0e2997SVladimir Oltean 	dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
30030b0e2997SVladimir Oltean 		device_id, part_no);
30040b0e2997SVladimir Oltean 
30050b0e2997SVladimir Oltean 	return -ENODEV;
30060b0e2997SVladimir Oltean }
30070b0e2997SVladimir Oltean 
30088aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
30098aa9ebccSVladimir Oltean {
3010844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
30118aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
30128aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
3013718bad0eSVladimir Oltean 	size_t max_xfer, max_msg;
30148aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
3015a68578c2SVladimir Oltean 	int rc, port;
30168aa9ebccSVladimir Oltean 
30178aa9ebccSVladimir Oltean 	if (!dev->of_node) {
30188aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
30198aa9ebccSVladimir Oltean 		return -EINVAL;
30208aa9ebccSVladimir Oltean 	}
30218aa9ebccSVladimir Oltean 
30228aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
30238aa9ebccSVladimir Oltean 	if (!priv)
30248aa9ebccSVladimir Oltean 		return -ENOMEM;
30258aa9ebccSVladimir Oltean 
30268aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
30278aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
30288aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
30298aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
30308aa9ebccSVladimir Oltean 	else
30318aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
30328aa9ebccSVladimir Oltean 
30338aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
30348aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
30358aa9ebccSVladimir Oltean 	 */
30368aa9ebccSVladimir Oltean 	priv->spidev = spi;
30378aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
30388aa9ebccSVladimir Oltean 
30398aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
30408aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
30418aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
30428aa9ebccSVladimir Oltean 	if (rc < 0) {
30438aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
30448aa9ebccSVladimir Oltean 		return rc;
30458aa9ebccSVladimir Oltean 	}
30468aa9ebccSVladimir Oltean 
3047718bad0eSVladimir Oltean 	/* In sja1105_xfer, we send spi_messages composed of two spi_transfers:
3048718bad0eSVladimir Oltean 	 * a small one for the message header and another one for the current
3049718bad0eSVladimir Oltean 	 * chunk of the packed buffer.
3050718bad0eSVladimir Oltean 	 * Check that the restrictions imposed by the SPI controller are
3051718bad0eSVladimir Oltean 	 * respected: the chunk buffer is smaller than the max transfer size,
3052718bad0eSVladimir Oltean 	 * and the total length of the chunk plus its message header is smaller
3053718bad0eSVladimir Oltean 	 * than the max message size.
3054718bad0eSVladimir Oltean 	 * We do that during probe time since the maximum transfer size is a
3055718bad0eSVladimir Oltean 	 * runtime invariant.
3056718bad0eSVladimir Oltean 	 */
3057718bad0eSVladimir Oltean 	max_xfer = spi_max_transfer_size(spi);
3058718bad0eSVladimir Oltean 	max_msg = spi_max_message_size(spi);
3059718bad0eSVladimir Oltean 
3060718bad0eSVladimir Oltean 	/* We need to send at least one 64-bit word of SPI payload per message
3061718bad0eSVladimir Oltean 	 * in order to be able to make useful progress.
3062718bad0eSVladimir Oltean 	 */
3063718bad0eSVladimir Oltean 	if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) {
3064718bad0eSVladimir Oltean 		dev_err(dev, "SPI master cannot send large enough buffers, aborting\n");
3065718bad0eSVladimir Oltean 		return -EINVAL;
3066718bad0eSVladimir Oltean 	}
3067718bad0eSVladimir Oltean 
3068718bad0eSVladimir Oltean 	priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN;
3069718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_xfer)
3070718bad0eSVladimir Oltean 		priv->max_xfer_len = max_xfer;
3071718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER)
3072718bad0eSVladimir Oltean 		priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER;
3073718bad0eSVladimir Oltean 
30748aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
30758aa9ebccSVladimir Oltean 
30768aa9ebccSVladimir Oltean 	/* Detect hardware device */
30778aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
30788aa9ebccSVladimir Oltean 	if (rc < 0) {
30798aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
30808aa9ebccSVladimir Oltean 		return rc;
30818aa9ebccSVladimir Oltean 	}
30828aa9ebccSVladimir Oltean 
30838aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
30848aa9ebccSVladimir Oltean 
30857e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
30868aa9ebccSVladimir Oltean 	if (!ds)
30878aa9ebccSVladimir Oltean 		return -ENOMEM;
30888aa9ebccSVladimir Oltean 
30897e99e347SVivien Didelot 	ds->dev = dev;
30903e77e59bSVladimir Oltean 	ds->num_ports = priv->info->num_ports;
30918aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
30928aa9ebccSVladimir Oltean 	ds->priv = priv;
30938aa9ebccSVladimir Oltean 	priv->ds = ds;
30948aa9ebccSVladimir Oltean 
3095844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
3096844d7edcSVladimir Oltean 
3097d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
3098d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
3099d5a619bfSVivien Didelot 
3100d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
3101a6af7763SVladimir Oltean 	sja1105_flower_setup(ds);
3102d5a619bfSVivien Didelot 
3103d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
3104d5a619bfSVivien Didelot 	if (rc)
3105328621f6SVladimir Oltean 		return rc;
3106d5a619bfSVivien Didelot 
31074d752508SVladimir Oltean 	if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
31084d752508SVladimir Oltean 		priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
31094d752508SVladimir Oltean 					 sizeof(struct sja1105_cbs_entry),
31104d752508SVladimir Oltean 					 GFP_KERNEL);
3111dc596e3fSVladimir Oltean 		if (!priv->cbs) {
3112dc596e3fSVladimir Oltean 			rc = -ENOMEM;
3113dc596e3fSVladimir Oltean 			goto out_unregister_switch;
3114dc596e3fSVladimir Oltean 		}
31154d752508SVladimir Oltean 	}
31164d752508SVladimir Oltean 
3117227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
3118542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
3119a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3120a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
3121a68578c2SVladimir Oltean 		struct net_device *slave;
3122227d07a0SVladimir Oltean 
3123a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3124a68578c2SVladimir Oltean 			continue;
3125a68578c2SVladimir Oltean 
3126a68578c2SVladimir Oltean 		dp->priv = sp;
3127a68578c2SVladimir Oltean 		sp->dp = dp;
3128844d7edcSVladimir Oltean 		sp->data = tagger_data;
3129a68578c2SVladimir Oltean 		slave = dp->slave;
3130a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
3131a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
3132a68578c2SVladimir Oltean 							slave->name);
3133a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
3134a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
3135a68578c2SVladimir Oltean 			dev_err(ds->dev,
3136a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
3137a68578c2SVladimir Oltean 				rc);
3138dc596e3fSVladimir Oltean 			goto out_destroy_workers;
3139a68578c2SVladimir Oltean 		}
3140a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
314138b5beeaSVladimir Oltean 		sp->xmit_tpid = ETH_P_SJA1105;
3142227d07a0SVladimir Oltean 	}
3143227d07a0SVladimir Oltean 
3144d5a619bfSVivien Didelot 	return 0;
3145dc596e3fSVladimir Oltean 
3146dc596e3fSVladimir Oltean out_destroy_workers:
3147a68578c2SVladimir Oltean 	while (port-- > 0) {
3148a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3149a68578c2SVladimir Oltean 
3150a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3151a68578c2SVladimir Oltean 			continue;
3152a68578c2SVladimir Oltean 
3153a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
3154a68578c2SVladimir Oltean 	}
3155dc596e3fSVladimir Oltean 
3156dc596e3fSVladimir Oltean out_unregister_switch:
3157dc596e3fSVladimir Oltean 	dsa_unregister_switch(ds);
3158dc596e3fSVladimir Oltean 
3159a68578c2SVladimir Oltean 	return rc;
31608aa9ebccSVladimir Oltean }
31618aa9ebccSVladimir Oltean 
31628aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
31638aa9ebccSVladimir Oltean {
31648aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
3165cedf4670SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
31668aa9ebccSVladimir Oltean 
3167cedf4670SVladimir Oltean 	dsa_unregister_switch(ds);
3168cedf4670SVladimir Oltean 
31698aa9ebccSVladimir Oltean 	return 0;
31708aa9ebccSVladimir Oltean }
31718aa9ebccSVladimir Oltean 
31728aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
31738aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
31748aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
31758aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
31768aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
31778aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
31788aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
31793e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110a", .data = &sja1110a_info },
31803e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110b", .data = &sja1110b_info },
31813e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110c", .data = &sja1110c_info },
31823e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110d", .data = &sja1110d_info },
31838aa9ebccSVladimir Oltean 	{ /* sentinel */ },
31848aa9ebccSVladimir Oltean };
31858aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
31868aa9ebccSVladimir Oltean 
31878aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
31888aa9ebccSVladimir Oltean 	.driver = {
31898aa9ebccSVladimir Oltean 		.name  = "sja1105",
31908aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
31918aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
31928aa9ebccSVladimir Oltean 	},
31938aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
31948aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
31958aa9ebccSVladimir Oltean };
31968aa9ebccSVladimir Oltean 
31978aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
31988aa9ebccSVladimir Oltean 
31998aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
32008aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
32018aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
32028aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
3203