xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision bef0746cf4cce238b1943df5d5b8f3103da92ead)
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 
60*bef0746cSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
61*bef0746cSVladimir Oltean {
62*bef0746cSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
63*bef0746cSVladimir Oltean 	int count, i;
64*bef0746cSVladimir Oltean 
65*bef0746cSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
66*bef0746cSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
67*bef0746cSVladimir Oltean 
68*bef0746cSVladimir Oltean 	for (i = 0; i < count; i++)
69*bef0746cSVladimir Oltean 		if (vlan[i].vlanid == vid)
70*bef0746cSVladimir Oltean 			return i;
71*bef0746cSVladimir Oltean 
72*bef0746cSVladimir Oltean 	/* Return an invalid entry index if not found */
73*bef0746cSVladimir Oltean 	return -1;
74*bef0746cSVladimir Oltean }
75*bef0746cSVladimir Oltean 
76*bef0746cSVladimir Oltean static int sja1105_drop_untagged(struct dsa_switch *ds, int port, bool drop)
77*bef0746cSVladimir Oltean {
78*bef0746cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
79*bef0746cSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
80*bef0746cSVladimir Oltean 
81*bef0746cSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
82*bef0746cSVladimir Oltean 
83*bef0746cSVladimir Oltean 	if (mac[port].drpuntag == drop)
84*bef0746cSVladimir Oltean 		return 0;
85*bef0746cSVladimir Oltean 
86*bef0746cSVladimir Oltean 	mac[port].drpuntag = drop;
87*bef0746cSVladimir Oltean 
88*bef0746cSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
89*bef0746cSVladimir Oltean 					    &mac[port], true);
90*bef0746cSVladimir Oltean }
91*bef0746cSVladimir 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;
111*bef0746cSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
112*bef0746cSVladimir Oltean 	bool drop_untagged = false;
113*bef0746cSVladimir 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 
121*bef0746cSVladimir Oltean 	rc = sja1105_pvid_apply(priv, port, pvid);
122*bef0746cSVladimir Oltean 	if (rc)
123*bef0746cSVladimir Oltean 		return rc;
124*bef0746cSVladimir Oltean 
125*bef0746cSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
126*bef0746cSVladimir Oltean 
127*bef0746cSVladimir Oltean 	match = sja1105_is_vlan_configured(priv, pvid);
128*bef0746cSVladimir Oltean 
129*bef0746cSVladimir Oltean 	if (match < 0 || !(vlan[match].vmemb_port & BIT(port)))
130*bef0746cSVladimir Oltean 		drop_untagged = true;
131*bef0746cSVladimir Oltean 
132*bef0746cSVladimir 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 
6918aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
6928aa9ebccSVladimir Oltean {
6938aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
694511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
695511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
6968aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
6975f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
6985f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
6995f06c63bSVladimir Oltean 		 */
70008fde09aSVladimir Oltean 		.hostprio = 7,
7018aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
7028aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
70342824463SVladimir Oltean 		.incl_srcpt1 = false,
7048aa9ebccSVladimir Oltean 		.send_meta1  = false,
7058aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
7068aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
70742824463SVladimir Oltean 		.incl_srcpt0 = false,
7088aa9ebccSVladimir Oltean 		.send_meta0  = false,
7098aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
7108aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
7118aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
7128aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
7138aa9ebccSVladimir Oltean 		 */
714df2a81a3SVladimir Oltean 		.host_port = priv->ds->num_ports,
715511e6ca0SVladimir Oltean 		/* Default to an invalid value */
716542043e9SVladimir Oltean 		.mirr_port = priv->ds->num_ports,
7178aa9ebccSVladimir Oltean 		/* No TTEthernet */
718dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
7198aa9ebccSVladimir Oltean 		.vlmarker = 0,
7208aa9ebccSVladimir Oltean 		.vlmask = 0,
7218aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
7228aa9ebccSVladimir Oltean 		.ignore2stf = 0,
7236666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
7246666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
7256666cebcSVladimir Oltean 		 */
7266666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
7276666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
72829305260SVladimir Oltean 		/* Enable the TTEthernet engine on SJA1110 */
72929305260SVladimir Oltean 		.tte_en = true,
7304913b8ebSVladimir Oltean 		/* Set up the EtherType for control packets on SJA1110 */
7314913b8ebSVladimir Oltean 		.header_type = ETH_P_SJA1110,
7328aa9ebccSVladimir Oltean 	};
7336c0de59bSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
734df2a81a3SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
7358aa9ebccSVladimir Oltean 	struct sja1105_table *table;
736df2a81a3SVladimir Oltean 	int port;
737df2a81a3SVladimir Oltean 
738df2a81a3SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
739df2a81a3SVladimir Oltean 		if (dsa_is_cpu_port(ds, port)) {
740df2a81a3SVladimir Oltean 			default_general_params.host_port = port;
741df2a81a3SVladimir Oltean 			break;
742df2a81a3SVladimir Oltean 		}
743df2a81a3SVladimir Oltean 	}
7448aa9ebccSVladimir Oltean 
7458aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
7468aa9ebccSVladimir Oltean 
7478aa9ebccSVladimir Oltean 	if (table->entry_count) {
7488aa9ebccSVladimir Oltean 		kfree(table->entries);
7498aa9ebccSVladimir Oltean 		table->entry_count = 0;
7508aa9ebccSVladimir Oltean 	}
7518aa9ebccSVladimir Oltean 
752fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
7538aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
7548aa9ebccSVladimir Oltean 	if (!table->entries)
7558aa9ebccSVladimir Oltean 		return -ENOMEM;
7568aa9ebccSVladimir Oltean 
757fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
7588aa9ebccSVladimir Oltean 
7596c0de59bSVladimir Oltean 	general_params = table->entries;
7606c0de59bSVladimir Oltean 
7618aa9ebccSVladimir Oltean 	/* This table only has a single entry */
7626c0de59bSVladimir Oltean 	general_params[0] = default_general_params;
7638aa9ebccSVladimir Oltean 
764ceec8bc0SVladimir Oltean 	sja1110_select_tdmaconfigidx(priv);
765ceec8bc0SVladimir Oltean 
7666c0de59bSVladimir Oltean 	/* Link-local traffic received on casc_port will be forwarded
7676c0de59bSVladimir Oltean 	 * to host_port without embedding the source port and device ID
7686c0de59bSVladimir Oltean 	 * info in the destination MAC address, and no RX timestamps will be
7696c0de59bSVladimir Oltean 	 * taken either (presumably because it is a cascaded port and a
7706c0de59bSVladimir Oltean 	 * downstream SJA switch already did that).
7716c0de59bSVladimir Oltean 	 * To disable the feature, we need to do different things depending on
7726c0de59bSVladimir Oltean 	 * switch generation. On SJA1105 we need to set an invalid port, while
7736c0de59bSVladimir Oltean 	 * on SJA1110 which support multiple cascaded ports, this field is a
7746c0de59bSVladimir Oltean 	 * bitmask so it must be left zero.
7756c0de59bSVladimir Oltean 	 */
7766c0de59bSVladimir Oltean 	if (!priv->info->multiple_cascade_ports)
7776c0de59bSVladimir Oltean 		general_params->casc_port = ds->num_ports;
7786c0de59bSVladimir Oltean 
7798aa9ebccSVladimir Oltean 	return 0;
7808aa9ebccSVladimir Oltean }
7818aa9ebccSVladimir Oltean 
78279d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
78379d5511cSVladimir Oltean {
78479d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
78579d5511cSVladimir Oltean 	struct sja1105_table *table;
78679d5511cSVladimir Oltean 
78779d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
78879d5511cSVladimir Oltean 
78979d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
79079d5511cSVladimir Oltean 	if (table->entry_count) {
79179d5511cSVladimir Oltean 		kfree(table->entries);
79279d5511cSVladimir Oltean 		table->entry_count = 0;
79379d5511cSVladimir Oltean 	}
79479d5511cSVladimir Oltean 
795fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
79679d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
79779d5511cSVladimir Oltean 	if (!table->entries)
79879d5511cSVladimir Oltean 		return -ENOMEM;
79979d5511cSVladimir Oltean 
800fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
80179d5511cSVladimir Oltean 
80279d5511cSVladimir Oltean 	avb = table->entries;
80379d5511cSVladimir Oltean 
80479d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
80579d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
80679d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
807747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
808747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
809747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
810747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
811747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
812747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
813747e5eb3SVladimir Oltean 	 */
814747e5eb3SVladimir Oltean 	avb->cas_master = false;
81579d5511cSVladimir Oltean 
81679d5511cSVladimir Oltean 	return 0;
81779d5511cSVladimir Oltean }
81879d5511cSVladimir Oltean 
819a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
820a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
821a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
822a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
823a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
824a7cc081cSVladimir Oltean  * will be used for this frame.
825a7cc081cSVladimir Oltean  *
826a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
827a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
828a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
829a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
830a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
831a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
832a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
833a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
834a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
835a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
836a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
837a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
838a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
839a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
840a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
841a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
842a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
843a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
844a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
845a7cc081cSVladimir Oltean  * +------------+--------+
846a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
847a7cc081cSVladimir Oltean  * +------------+--------+
848a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
849a7cc081cSVladimir Oltean  * +------------+--------+
850a7cc081cSVladimir Oltean  *    ...                                  ...
851a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
852a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
853a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
854a7cc081cSVladimir Oltean  *
855a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
856a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
857a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
858a7cc081cSVladimir Oltean  * lookup) equal.
859a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
860a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
861a7cc081cSVladimir Oltean  */
8628aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
8638aa9ebccSVladimir Oltean 
8648aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
8658aa9ebccSVladimir Oltean {
8668aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
867542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
8688aa9ebccSVladimir Oltean 	struct sja1105_table *table;
869a7cc081cSVladimir Oltean 	int port, tc;
8708aa9ebccSVladimir Oltean 
8718aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
8728aa9ebccSVladimir Oltean 
8738aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
8748aa9ebccSVladimir Oltean 	if (table->entry_count) {
8758aa9ebccSVladimir Oltean 		kfree(table->entries);
8768aa9ebccSVladimir Oltean 		table->entry_count = 0;
8778aa9ebccSVladimir Oltean 	}
8788aa9ebccSVladimir Oltean 
879fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
8808aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
8818aa9ebccSVladimir Oltean 	if (!table->entries)
8828aa9ebccSVladimir Oltean 		return -ENOMEM;
8838aa9ebccSVladimir Oltean 
884fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
8858aa9ebccSVladimir Oltean 
8868aa9ebccSVladimir Oltean 	policing = table->entries;
8878aa9ebccSVladimir Oltean 
888a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
889542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
89038fbe91fSVladimir Oltean 		int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port;
891542043e9SVladimir Oltean 		int bcast = (ds->num_ports * SJA1105_NUM_TC) + port;
892a7cc081cSVladimir Oltean 
893a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
894a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
895a7cc081cSVladimir Oltean 
896a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
89738fbe91fSVladimir Oltean 		/* Only SJA1110 has multicast policers */
89838fbe91fSVladimir Oltean 		if (mcast <= table->ops->max_entry_count)
89938fbe91fSVladimir Oltean 			policing[mcast].sharindx = port;
900a7cc081cSVladimir Oltean 	}
901a7cc081cSVladimir Oltean 
902a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
903542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
904c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
905c279c726SVladimir Oltean 
906a7cc081cSVladimir Oltean 		if (dsa_is_cpu_port(priv->ds, port))
907c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
9088aa9ebccSVladimir Oltean 
909a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
910a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
911a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
912a7cc081cSVladimir Oltean 		policing[port].partition = 0;
9138aa9ebccSVladimir Oltean 	}
914a7cc081cSVladimir Oltean 
9158aa9ebccSVladimir Oltean 	return 0;
9168aa9ebccSVladimir Oltean }
9178aa9ebccSVladimir Oltean 
9185d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv)
9198aa9ebccSVladimir Oltean {
9208aa9ebccSVladimir Oltean 	int rc;
9218aa9ebccSVladimir Oltean 
9228aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
9238aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
9248aa9ebccSVladimir Oltean 					priv->info->static_ops,
9258aa9ebccSVladimir Oltean 					priv->info->device_id);
9268aa9ebccSVladimir Oltean 	if (rc)
9278aa9ebccSVladimir Oltean 		return rc;
9288aa9ebccSVladimir Oltean 
9298aa9ebccSVladimir Oltean 	/* Build static configuration */
9308aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
9318aa9ebccSVladimir Oltean 	if (rc < 0)
9328aa9ebccSVladimir Oltean 		return rc;
9335d645df9SVladimir Oltean 	rc = sja1105_init_mii_settings(priv);
9348aa9ebccSVladimir Oltean 	if (rc < 0)
9358aa9ebccSVladimir Oltean 		return rc;
9368aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
9378aa9ebccSVladimir Oltean 	if (rc < 0)
9388aa9ebccSVladimir Oltean 		return rc;
9398aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
9408aa9ebccSVladimir Oltean 	if (rc < 0)
9418aa9ebccSVladimir Oltean 		return rc;
9428aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
9438aa9ebccSVladimir Oltean 	if (rc < 0)
9448aa9ebccSVladimir Oltean 		return rc;
9458aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
9468aa9ebccSVladimir Oltean 	if (rc < 0)
9478aa9ebccSVladimir Oltean 		return rc;
9488aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
9498aa9ebccSVladimir Oltean 	if (rc < 0)
9508aa9ebccSVladimir Oltean 		return rc;
9518aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
9528aa9ebccSVladimir Oltean 	if (rc < 0)
9538aa9ebccSVladimir Oltean 		return rc;
9548aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
9558aa9ebccSVladimir Oltean 	if (rc < 0)
9568aa9ebccSVladimir Oltean 		return rc;
95779d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
95879d5511cSVladimir Oltean 	if (rc < 0)
95979d5511cSVladimir Oltean 		return rc;
9603e77e59bSVladimir Oltean 	rc = sja1110_init_pcp_remapping(priv);
9613e77e59bSVladimir Oltean 	if (rc < 0)
9623e77e59bSVladimir Oltean 		return rc;
9638aa9ebccSVladimir Oltean 
9648aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
9658aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
9668aa9ebccSVladimir Oltean }
9678aa9ebccSVladimir Oltean 
96829afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv)
969f5b8631cSVladimir Oltean {
970542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
97129afb83aSVladimir Oltean 	int port;
972f5b8631cSVladimir Oltean 
97329afb83aSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
97429afb83aSVladimir Oltean 		if (!priv->fixed_link[port])
975f5b8631cSVladimir Oltean 			continue;
976f5b8631cSVladimir Oltean 
97729afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID ||
97829afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
97929afb83aSVladimir Oltean 			priv->rgmii_rx_delay[port] = true;
980f5b8631cSVladimir Oltean 
98129afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID ||
98229afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
98329afb83aSVladimir Oltean 			priv->rgmii_tx_delay[port] = true;
984f5b8631cSVladimir Oltean 
98529afb83aSVladimir Oltean 		if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) &&
986f5b8631cSVladimir Oltean 		    !priv->info->setup_rgmii_delay)
987f5b8631cSVladimir Oltean 			return -EINVAL;
988f5b8631cSVladimir Oltean 	}
989f5b8631cSVladimir Oltean 	return 0;
990f5b8631cSVladimir Oltean }
991f5b8631cSVladimir Oltean 
9928aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
9938aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
9948aa9ebccSVladimir Oltean {
9958aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
9968aa9ebccSVladimir Oltean 	struct device_node *child;
9978aa9ebccSVladimir Oltean 
99827afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
9998aa9ebccSVladimir Oltean 		struct device_node *phy_node;
10000c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
10018aa9ebccSVladimir Oltean 		u32 index;
10020c65b2b9SAndrew Lunn 		int err;
10038aa9ebccSVladimir Oltean 
10048aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
10058aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
10068aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
10078aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
10087ba771e3SNishka Dasgupta 			of_node_put(child);
10098aa9ebccSVladimir Oltean 			return -ENODEV;
10108aa9ebccSVladimir Oltean 		}
10118aa9ebccSVladimir Oltean 
10128aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
10130c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
10140c65b2b9SAndrew Lunn 		if (err) {
10158aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
10168aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
10178aa9ebccSVladimir Oltean 				index);
10187ba771e3SNishka Dasgupta 			of_node_put(child);
10198aa9ebccSVladimir Oltean 			return -ENODEV;
10208aa9ebccSVladimir Oltean 		}
10218aa9ebccSVladimir Oltean 
10228aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
10238aa9ebccSVladimir Oltean 		if (!phy_node) {
10248aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
10258aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
10268aa9ebccSVladimir Oltean 					"properties missing!\n");
10277ba771e3SNishka Dasgupta 				of_node_put(child);
10288aa9ebccSVladimir Oltean 				return -ENODEV;
10298aa9ebccSVladimir Oltean 			}
10308aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
10318aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
10328aa9ebccSVladimir Oltean 			 */
103329afb83aSVladimir Oltean 			priv->fixed_link[index] = true;
10348aa9ebccSVladimir Oltean 		} else {
10358aa9ebccSVladimir Oltean 			of_node_put(phy_node);
10368aa9ebccSVladimir Oltean 		}
10378aa9ebccSVladimir Oltean 
1038bf4edf4aSVladimir Oltean 		priv->phy_mode[index] = phy_mode;
10398aa9ebccSVladimir Oltean 	}
10408aa9ebccSVladimir Oltean 
10418aa9ebccSVladimir Oltean 	return 0;
10428aa9ebccSVladimir Oltean }
10438aa9ebccSVladimir Oltean 
10445d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv)
10458aa9ebccSVladimir Oltean {
10468aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
10478aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
10488aa9ebccSVladimir Oltean 	struct device_node *ports_node;
10498aa9ebccSVladimir Oltean 	int rc;
10508aa9ebccSVladimir Oltean 
10518aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
105215074a36SVladimir Oltean 	if (!ports_node)
105315074a36SVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
10548aa9ebccSVladimir Oltean 	if (!ports_node) {
10558aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
10568aa9ebccSVladimir Oltean 		return -ENODEV;
10578aa9ebccSVladimir Oltean 	}
10588aa9ebccSVladimir Oltean 
10595d645df9SVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports_node);
10608aa9ebccSVladimir Oltean 	of_node_put(ports_node);
10618aa9ebccSVladimir Oltean 
10628aa9ebccSVladimir Oltean 	return rc;
10638aa9ebccSVladimir Oltean }
10648aa9ebccSVladimir Oltean 
1065c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
106641fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv,
106741fed17fSVladimir Oltean 					 u64 speed)
106841fed17fSVladimir Oltean {
106941fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS])
107041fed17fSVladimir Oltean 		return SPEED_10;
107141fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS])
107241fed17fSVladimir Oltean 		return SPEED_100;
107341fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS])
107441fed17fSVladimir Oltean 		return SPEED_1000;
107541fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS])
107641fed17fSVladimir Oltean 		return SPEED_2500;
107741fed17fSVladimir Oltean 	return SPEED_UNKNOWN;
107841fed17fSVladimir Oltean }
10798aa9ebccSVladimir Oltean 
10808400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
10818aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
10828400cff6SVladimir Oltean 				      int speed_mbps)
10838aa9ebccSVladimir Oltean {
10848aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
10858aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
108641fed17fSVladimir Oltean 	u64 speed;
10878aa9ebccSVladimir Oltean 	int rc;
10888aa9ebccSVladimir Oltean 
10898400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
10908400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
10918400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
10928400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
10938400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
10948400cff6SVladimir Oltean 	 */
10958aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
10968aa9ebccSVladimir Oltean 
1097f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
1098c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
1099a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
1100a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
1101a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
1102a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
1103a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
1104a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
1105a979a0abSVladimir Oltean 		 */
110641fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
1107f4cfcfbdSVladimir Oltean 		break;
1108c44d0535SVladimir Oltean 	case SPEED_10:
110941fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_10MBPS];
1110f4cfcfbdSVladimir Oltean 		break;
1111c44d0535SVladimir Oltean 	case SPEED_100:
111241fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_100MBPS];
1113f4cfcfbdSVladimir Oltean 		break;
1114c44d0535SVladimir Oltean 	case SPEED_1000:
111541fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
1116f4cfcfbdSVladimir Oltean 		break;
111756b63466SVladimir Oltean 	case SPEED_2500:
111856b63466SVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
111956b63466SVladimir Oltean 		break;
1120f4cfcfbdSVladimir Oltean 	default:
11218aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
11228aa9ebccSVladimir Oltean 		return -EINVAL;
11238aa9ebccSVladimir Oltean 	}
11248aa9ebccSVladimir Oltean 
11258400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
11268400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
11278400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
11288400cff6SVladimir Oltean 	 * we want auto during upload phase).
1129ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
1130ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
11318aa9ebccSVladimir Oltean 	 */
113291a05078SVladimir Oltean 	if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII)
113341fed17fSVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
113456b63466SVladimir Oltean 	else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX)
113556b63466SVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
1136ffe10e67SVladimir Oltean 	else
11378aa9ebccSVladimir Oltean 		mac[port].speed = speed;
11388aa9ebccSVladimir Oltean 
11398aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
11408400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
11418400cff6SVladimir Oltean 					  &mac[port], true);
11428aa9ebccSVladimir Oltean 	if (rc < 0) {
11438aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
11448aa9ebccSVladimir Oltean 		return rc;
11458aa9ebccSVladimir Oltean 	}
11468aa9ebccSVladimir Oltean 
11478aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
11488aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
11498aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
11508aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
11518aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
11528aa9ebccSVladimir Oltean 	 */
115391a05078SVladimir Oltean 	if (!phy_interface_mode_is_rgmii(priv->phy_mode[port]))
11548aa9ebccSVladimir Oltean 		return 0;
11558aa9ebccSVladimir Oltean 
11568aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
11578aa9ebccSVladimir Oltean }
11588aa9ebccSVladimir Oltean 
115939710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
116039710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
116139710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
116239710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
116339710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
116439710229SVladimir Oltean  * now.
116539710229SVladimir Oltean  */
116639710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
116739710229SVladimir Oltean 				      phy_interface_t interface)
116839710229SVladimir Oltean {
1169bf4edf4aSVladimir Oltean 	return priv->phy_mode[port] != interface;
117039710229SVladimir Oltean }
117139710229SVladimir Oltean 
1172af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
1173ffe10e67SVladimir Oltean 			       unsigned int mode,
1174af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
11758aa9ebccSVladimir Oltean {
11763ad1d171SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
11778aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
11783ad1d171SVladimir Oltean 	struct dw_xpcs *xpcs;
11798aa9ebccSVladimir Oltean 
1180ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1181ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
1182ec8582d1SVladimir Oltean 			phy_modes(state->interface));
118339710229SVladimir Oltean 		return;
1184ec8582d1SVladimir Oltean 	}
118539710229SVladimir Oltean 
11863ad1d171SVladimir Oltean 	xpcs = priv->xpcs[port];
1187ffe10e67SVladimir Oltean 
11883ad1d171SVladimir Oltean 	if (xpcs)
11893ad1d171SVladimir Oltean 		phylink_set_pcs(dp->pl, &xpcs->pcs);
11908400cff6SVladimir Oltean }
11918400cff6SVladimir Oltean 
11928400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
11938400cff6SVladimir Oltean 				  unsigned int mode,
11948400cff6SVladimir Oltean 				  phy_interface_t interface)
11958400cff6SVladimir Oltean {
11968400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
11978400cff6SVladimir Oltean }
11988400cff6SVladimir Oltean 
11998400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
12008400cff6SVladimir Oltean 				unsigned int mode,
12018400cff6SVladimir Oltean 				phy_interface_t interface,
12025b502a7bSRussell King 				struct phy_device *phydev,
12035b502a7bSRussell King 				int speed, int duplex,
12045b502a7bSRussell King 				bool tx_pause, bool rx_pause)
12058400cff6SVladimir Oltean {
1206ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1207ec8582d1SVladimir Oltean 
1208ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1209ec8582d1SVladimir Oltean 
1210ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
12118aa9ebccSVladimir Oltean }
12128aa9ebccSVladimir Oltean 
1213ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1214ad9f299aSVladimir Oltean 				     unsigned long *supported,
1215ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
1216ad9f299aSVladimir Oltean {
1217ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
1218ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
1219ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
1220ad9f299aSVladimir Oltean 	 */
1221ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1222ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1223ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1224ad9f299aSVladimir Oltean 
1225ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1226ad9f299aSVladimir Oltean 
122739710229SVladimir Oltean 	/* include/linux/phylink.h says:
122839710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
122939710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
123039710229SVladimir Oltean 	 */
123139710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
123239710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
123339710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
123439710229SVladimir Oltean 		return;
123539710229SVladimir Oltean 	}
123639710229SVladimir Oltean 
1237ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1238ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1239ad9f299aSVladimir Oltean 	 */
1240ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
1241ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
1242ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
1243ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
1244ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
1245ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1246ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1247ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
124856b63466SVladimir Oltean 	if (priv->info->supports_2500basex[port]) {
124956b63466SVladimir Oltean 		phylink_set(mask, 2500baseT_Full);
125056b63466SVladimir Oltean 		phylink_set(mask, 2500baseX_Full);
125156b63466SVladimir Oltean 	}
1252ad9f299aSVladimir Oltean 
1253ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1254ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1255ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1256ad9f299aSVladimir Oltean }
1257ad9f299aSVladimir Oltean 
125860f6053fSVladimir Oltean static int
125960f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
126060f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
126160f6053fSVladimir Oltean {
126260f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
126360f6053fSVladimir Oltean 	struct sja1105_table *table;
126460f6053fSVladimir Oltean 	int i;
126560f6053fSVladimir Oltean 
126660f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
126760f6053fSVladimir Oltean 	l2_lookup = table->entries;
126860f6053fSVladimir Oltean 
126960f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
127060f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
127160f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
127260f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
127360f6053fSVladimir Oltean 			return i;
127460f6053fSVladimir Oltean 
127560f6053fSVladimir Oltean 	return -1;
127660f6053fSVladimir Oltean }
127760f6053fSVladimir Oltean 
127860f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
127960f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
128060f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
128160f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
128260f6053fSVladimir Oltean  */
128360f6053fSVladimir Oltean static int
128460f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
128560f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
128660f6053fSVladimir Oltean 			  bool keep)
128760f6053fSVladimir Oltean {
128860f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
128960f6053fSVladimir Oltean 	struct sja1105_table *table;
129060f6053fSVladimir Oltean 	int rc, match;
129160f6053fSVladimir Oltean 
129260f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
129360f6053fSVladimir Oltean 
129460f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
129560f6053fSVladimir Oltean 	if (match < 0) {
129660f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
129760f6053fSVladimir Oltean 		if (!keep)
129860f6053fSVladimir Oltean 			return 0;
129960f6053fSVladimir Oltean 
130060f6053fSVladimir Oltean 		/* No match => new entry */
130160f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
130260f6053fSVladimir Oltean 		if (rc)
130360f6053fSVladimir Oltean 			return rc;
130460f6053fSVladimir Oltean 
130560f6053fSVladimir Oltean 		match = table->entry_count - 1;
130660f6053fSVladimir Oltean 	}
130760f6053fSVladimir Oltean 
130860f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
130960f6053fSVladimir Oltean 	l2_lookup = table->entries;
131060f6053fSVladimir Oltean 
131160f6053fSVladimir Oltean 	/* We have a match.
131260f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
131360f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
131460f6053fSVladimir Oltean 	 * which we update it).
131560f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
131660f6053fSVladimir Oltean 	 */
131760f6053fSVladimir Oltean 	if (keep) {
131860f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
131960f6053fSVladimir Oltean 		return 0;
132060f6053fSVladimir Oltean 	}
132160f6053fSVladimir Oltean 
132260f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
132360f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
132460f6053fSVladimir Oltean 	 */
132560f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
132660f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
132760f6053fSVladimir Oltean }
132860f6053fSVladimir Oltean 
1329291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1330291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1331291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1332291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1333291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1334291d1e72SVladimir Oltean  */
133509c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1336291d1e72SVladimir Oltean {
1337291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1338291d1e72SVladimir Oltean }
1339291d1e72SVladimir Oltean 
13409dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1341291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1342291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1343291d1e72SVladimir Oltean 					 int *last_unused)
1344291d1e72SVladimir Oltean {
1345291d1e72SVladimir Oltean 	int way;
1346291d1e72SVladimir Oltean 
1347291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1348291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1349291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1350291d1e72SVladimir Oltean 
1351291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1352291d1e72SVladimir Oltean 		 * into the return value
1353291d1e72SVladimir Oltean 		 */
1354291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1355291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1356291d1e72SVladimir Oltean 			if (last_unused)
1357291d1e72SVladimir Oltean 				*last_unused = way;
1358291d1e72SVladimir Oltean 			continue;
1359291d1e72SVladimir Oltean 		}
1360291d1e72SVladimir Oltean 
1361291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1362291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1363291d1e72SVladimir Oltean 			if (match)
1364291d1e72SVladimir Oltean 				*match = l2_lookup;
1365291d1e72SVladimir Oltean 			return way;
1366291d1e72SVladimir Oltean 		}
1367291d1e72SVladimir Oltean 	}
1368291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1369291d1e72SVladimir Oltean 	return -1;
1370291d1e72SVladimir Oltean }
1371291d1e72SVladimir Oltean 
13729dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1373291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1374291d1e72SVladimir Oltean {
1375291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1376291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1377291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1378291d1e72SVladimir Oltean 	int last_unused = -1;
137960f6053fSVladimir Oltean 	int bin, way, rc;
1380291d1e72SVladimir Oltean 
13819dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1382291d1e72SVladimir Oltean 
13839dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1384291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1385291d1e72SVladimir Oltean 	if (way >= 0) {
1386291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1387291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1388291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1389291d1e72SVladimir Oltean 		 */
1390291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1391291d1e72SVladimir Oltean 			return 0;
1392291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1393291d1e72SVladimir Oltean 	} else {
1394291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1395291d1e72SVladimir Oltean 
1396291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1397291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1398291d1e72SVladimir Oltean 		 */
1399291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1400291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1401291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1402291d1e72SVladimir Oltean 
1403291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1404291d1e72SVladimir Oltean 			way = last_unused;
1405291d1e72SVladimir Oltean 		} else {
1406291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1407291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1408291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1409291d1e72SVladimir Oltean 			 * distribution function:
1410291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1411291d1e72SVladimir Oltean 			 */
1412291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1413291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1414291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1415291d1e72SVladimir Oltean 				 bin, addr, way);
1416291d1e72SVladimir Oltean 			/* Evict entry */
1417291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1418291d1e72SVladimir Oltean 						     index, NULL, false);
1419291d1e72SVladimir Oltean 		}
1420291d1e72SVladimir Oltean 	}
1421291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1422291d1e72SVladimir Oltean 
142360f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1424291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1425291d1e72SVladimir Oltean 					  true);
142660f6053fSVladimir Oltean 	if (rc < 0)
142760f6053fSVladimir Oltean 		return rc;
142860f6053fSVladimir Oltean 
142960f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1430291d1e72SVladimir Oltean }
1431291d1e72SVladimir Oltean 
14329dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1433291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1434291d1e72SVladimir Oltean {
1435291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1436291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
143760f6053fSVladimir Oltean 	int index, bin, way, rc;
1438291d1e72SVladimir Oltean 	bool keep;
1439291d1e72SVladimir Oltean 
14409dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
14419dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1442291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1443291d1e72SVladimir Oltean 	if (way < 0)
1444291d1e72SVladimir Oltean 		return 0;
1445291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1446291d1e72SVladimir Oltean 
1447291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1448291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1449291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1450291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1451291d1e72SVladimir Oltean 	 */
1452291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
14537752e937SVladimir Oltean 
1454291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1455291d1e72SVladimir Oltean 		keep = true;
1456291d1e72SVladimir Oltean 	else
1457291d1e72SVladimir Oltean 		keep = false;
1458291d1e72SVladimir Oltean 
145960f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1460291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
146160f6053fSVladimir Oltean 	if (rc < 0)
146260f6053fSVladimir Oltean 		return rc;
146360f6053fSVladimir Oltean 
146460f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1465291d1e72SVladimir Oltean }
1466291d1e72SVladimir Oltean 
14679dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
14689dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
14699dfa6911SVladimir Oltean {
14701da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
14711da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14721da73821SVladimir Oltean 	int rc, i;
14731da73821SVladimir Oltean 
14741da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
14751da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
14761da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
14771da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
14781da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
14790fac6aa0SVladimir Oltean 	if (priv->vlan_aware) {
14801da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
14811da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
14826d7c7d94SVladimir Oltean 	} else {
14836d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
14846d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
14856d7c7d94SVladimir Oltean 	}
14861da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
14871da73821SVladimir Oltean 
14881da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
14891da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
14901da73821SVladimir Oltean 	if (rc == 0) {
14911da73821SVladimir Oltean 		/* Found and this port is already in the entry's
14921da73821SVladimir Oltean 		 * port mask => job done
14931da73821SVladimir Oltean 		 */
14941da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
14951da73821SVladimir Oltean 			return 0;
14961da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
14971da73821SVladimir Oltean 		 * found something.
14981da73821SVladimir Oltean 		 */
14991da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
15001da73821SVladimir Oltean 		goto skip_finding_an_index;
15011da73821SVladimir Oltean 	}
15021da73821SVladimir Oltean 
15031da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
15041da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
15051da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
15061da73821SVladimir Oltean 	 */
15071da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
15081da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
15091da73821SVladimir Oltean 						 i, NULL);
15101da73821SVladimir Oltean 		if (rc < 0)
15111da73821SVladimir Oltean 			break;
15121da73821SVladimir Oltean 	}
15131da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
15141da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
15151da73821SVladimir Oltean 		return -EINVAL;
15161da73821SVladimir Oltean 	}
151717ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
15181da73821SVladimir Oltean 	l2_lookup.index = i;
15191da73821SVladimir Oltean 
15201da73821SVladimir Oltean skip_finding_an_index:
152160f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
15221da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
15231da73821SVladimir Oltean 					  true);
152460f6053fSVladimir Oltean 	if (rc < 0)
152560f6053fSVladimir Oltean 		return rc;
152660f6053fSVladimir Oltean 
152760f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
15289dfa6911SVladimir Oltean }
15299dfa6911SVladimir Oltean 
15309dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
15319dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
15329dfa6911SVladimir Oltean {
15331da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
15341da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15351da73821SVladimir Oltean 	bool keep;
15361da73821SVladimir Oltean 	int rc;
15371da73821SVladimir Oltean 
15381da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
15391da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
15401da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
15411da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
15420fac6aa0SVladimir Oltean 	if (priv->vlan_aware) {
15431da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
15441da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
15456d7c7d94SVladimir Oltean 	} else {
15466d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
15476d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
15486d7c7d94SVladimir Oltean 	}
15491da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
15501da73821SVladimir Oltean 
15511da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
15521da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
15531da73821SVladimir Oltean 	if (rc < 0)
15541da73821SVladimir Oltean 		return 0;
15551da73821SVladimir Oltean 
15561da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
15571da73821SVladimir Oltean 
15581da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
15591da73821SVladimir Oltean 	 * or if we remove it completely.
15601da73821SVladimir Oltean 	 */
15611da73821SVladimir Oltean 	if (l2_lookup.destports)
15621da73821SVladimir Oltean 		keep = true;
15631da73821SVladimir Oltean 	else
15641da73821SVladimir Oltean 		keep = false;
15651da73821SVladimir Oltean 
156660f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
15671da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
156860f6053fSVladimir Oltean 	if (rc < 0)
156960f6053fSVladimir Oltean 		return rc;
157060f6053fSVladimir Oltean 
157160f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
15729dfa6911SVladimir Oltean }
15739dfa6911SVladimir Oltean 
15749dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
15759dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
15769dfa6911SVladimir Oltean {
15779dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1578b3ee526aSVladimir Oltean 
15796d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
15809dfa6911SVladimir Oltean }
15819dfa6911SVladimir Oltean 
15829dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
15839dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
15849dfa6911SVladimir Oltean {
15859dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15869dfa6911SVladimir Oltean 
1587b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
15889dfa6911SVladimir Oltean }
15899dfa6911SVladimir Oltean 
1590291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1591291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1592291d1e72SVladimir Oltean {
1593291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1594291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1595291d1e72SVladimir Oltean 	int i;
1596291d1e72SVladimir Oltean 
1597291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1598291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1599291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1600291d1e72SVladimir Oltean 		int rc;
1601291d1e72SVladimir Oltean 
1602291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1603291d1e72SVladimir Oltean 						 i, &l2_lookup);
1604291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1605def84604SVladimir Oltean 		if (rc == -ENOENT)
1606291d1e72SVladimir Oltean 			continue;
1607291d1e72SVladimir Oltean 		if (rc) {
1608291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1609291d1e72SVladimir Oltean 			return rc;
1610291d1e72SVladimir Oltean 		}
1611291d1e72SVladimir Oltean 
1612291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1613291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1614291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1615291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1616291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1617291d1e72SVladimir Oltean 		 */
1618291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1619291d1e72SVladimir Oltean 			continue;
16204d942354SVladimir Oltean 
16214d942354SVladimir Oltean 		/* We need to hide the FDB entry for unknown multicast */
16224d942354SVladimir Oltean 		if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST &&
16234d942354SVladimir Oltean 		    l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
16244d942354SVladimir Oltean 			continue;
16254d942354SVladimir Oltean 
1626291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
162793647594SVladimir Oltean 
16286d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
16290fac6aa0SVladimir Oltean 		if (!priv->vlan_aware)
16306d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
163117ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1632291d1e72SVladimir Oltean 	}
1633291d1e72SVladimir Oltean 	return 0;
1634291d1e72SVladimir Oltean }
1635291d1e72SVladimir Oltean 
1636a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port,
1637291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1638291d1e72SVladimir Oltean {
1639a52b2da7SVladimir Oltean 	return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1640291d1e72SVladimir Oltean }
1641291d1e72SVladimir Oltean 
1642291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1643291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1644291d1e72SVladimir Oltean {
1645291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1646291d1e72SVladimir Oltean }
1647291d1e72SVladimir Oltean 
16487f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration.
16497f7ccdeaSVladimir Oltean  * Flooding is configured between each {ingress, egress} port pair, and since
16507f7ccdeaSVladimir Oltean  * the bridge's semantics are those of "egress flooding", it means we must
16517f7ccdeaSVladimir Oltean  * enable flooding towards this port from all ingress ports that are in the
16527f7ccdeaSVladimir Oltean  * same forwarding domain.
16537f7ccdeaSVladimir Oltean  */
16547f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv)
16557f7ccdeaSVladimir Oltean {
16567f7ccdeaSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
16577f7ccdeaSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
16587f7ccdeaSVladimir Oltean 	int from, to, rc;
16597f7ccdeaSVladimir Oltean 
16607f7ccdeaSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
16617f7ccdeaSVladimir Oltean 
16627f7ccdeaSVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
16637f7ccdeaSVladimir Oltean 		u64 fl_domain = 0, bc_domain = 0;
16647f7ccdeaSVladimir Oltean 
16657f7ccdeaSVladimir Oltean 		for (to = 0; to < priv->ds->num_ports; to++) {
16667f7ccdeaSVladimir Oltean 			if (!sja1105_can_forward(l2_fwd, from, to))
16677f7ccdeaSVladimir Oltean 				continue;
16687f7ccdeaSVladimir Oltean 
16697f7ccdeaSVladimir Oltean 			if (priv->ucast_egress_floods & BIT(to))
16707f7ccdeaSVladimir Oltean 				fl_domain |= BIT(to);
16717f7ccdeaSVladimir Oltean 			if (priv->bcast_egress_floods & BIT(to))
16727f7ccdeaSVladimir Oltean 				bc_domain |= BIT(to);
16737f7ccdeaSVladimir Oltean 		}
16747f7ccdeaSVladimir Oltean 
16757f7ccdeaSVladimir Oltean 		/* Nothing changed, nothing to do */
16767f7ccdeaSVladimir Oltean 		if (l2_fwd[from].fl_domain == fl_domain &&
16777f7ccdeaSVladimir Oltean 		    l2_fwd[from].bc_domain == bc_domain)
16787f7ccdeaSVladimir Oltean 			continue;
16797f7ccdeaSVladimir Oltean 
16807f7ccdeaSVladimir Oltean 		l2_fwd[from].fl_domain = fl_domain;
16817f7ccdeaSVladimir Oltean 		l2_fwd[from].bc_domain = bc_domain;
16827f7ccdeaSVladimir Oltean 
16837f7ccdeaSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
16847f7ccdeaSVladimir Oltean 						  from, &l2_fwd[from], true);
16857f7ccdeaSVladimir Oltean 		if (rc < 0)
16867f7ccdeaSVladimir Oltean 			return rc;
16877f7ccdeaSVladimir Oltean 	}
16887f7ccdeaSVladimir Oltean 
16897f7ccdeaSVladimir Oltean 	return 0;
16907f7ccdeaSVladimir Oltean }
16917f7ccdeaSVladimir Oltean 
16928aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
16938aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
16948aa9ebccSVladimir Oltean {
16958aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
16968aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16978aa9ebccSVladimir Oltean 	int i, rc;
16988aa9ebccSVladimir Oltean 
16998aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
17008aa9ebccSVladimir Oltean 
1701542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
17028aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
17038aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
17048aa9ebccSVladimir Oltean 		 */
17058aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
17068aa9ebccSVladimir Oltean 			continue;
17078aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
17088aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
17098aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
17108aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
17118aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
17128aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
17138aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
17148aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
17158aa9ebccSVladimir Oltean 		 */
17168aa9ebccSVladimir Oltean 		if (i == port)
17178aa9ebccSVladimir Oltean 			continue;
17188aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
17198aa9ebccSVladimir Oltean 			continue;
17208aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
17218aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
17228aa9ebccSVladimir Oltean 
17238aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
17248aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
17258aa9ebccSVladimir Oltean 		if (rc < 0)
17268aa9ebccSVladimir Oltean 			return rc;
17278aa9ebccSVladimir Oltean 	}
17288aa9ebccSVladimir Oltean 
17297f7ccdeaSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
17308aa9ebccSVladimir Oltean 					  port, &l2_fwd[port], true);
17317f7ccdeaSVladimir Oltean 	if (rc)
17327f7ccdeaSVladimir Oltean 		return rc;
17337f7ccdeaSVladimir Oltean 
1734cde8078eSVladimir Oltean 	rc = sja1105_commit_pvid(ds, port);
1735cde8078eSVladimir Oltean 	if (rc)
1736cde8078eSVladimir Oltean 		return rc;
1737cde8078eSVladimir Oltean 
17387f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
17398aa9ebccSVladimir Oltean }
17408aa9ebccSVladimir Oltean 
1741640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1742640f763fSVladimir Oltean 					 u8 state)
1743640f763fSVladimir Oltean {
1744640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1745640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1746640f763fSVladimir Oltean 
1747640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1748640f763fSVladimir Oltean 
1749640f763fSVladimir Oltean 	switch (state) {
1750640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1751640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1752640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1753640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1754640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1755640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1756640f763fSVladimir Oltean 		 */
1757640f763fSVladimir Oltean 		mac[port].ingress   = false;
1758640f763fSVladimir Oltean 		mac[port].egress    = false;
1759640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1760640f763fSVladimir Oltean 		break;
1761640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1762640f763fSVladimir Oltean 		mac[port].ingress   = true;
1763640f763fSVladimir Oltean 		mac[port].egress    = false;
1764640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1765640f763fSVladimir Oltean 		break;
1766640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1767640f763fSVladimir Oltean 		mac[port].ingress   = true;
1768640f763fSVladimir Oltean 		mac[port].egress    = false;
17694d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1770640f763fSVladimir Oltean 		break;
1771640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1772640f763fSVladimir Oltean 		mac[port].ingress   = true;
1773640f763fSVladimir Oltean 		mac[port].egress    = true;
17744d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1775640f763fSVladimir Oltean 		break;
1776640f763fSVladimir Oltean 	default:
1777640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1778640f763fSVladimir Oltean 		return;
1779640f763fSVladimir Oltean 	}
1780640f763fSVladimir Oltean 
1781640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1782640f763fSVladimir Oltean 				     &mac[port], true);
1783640f763fSVladimir Oltean }
1784640f763fSVladimir Oltean 
17858aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
17868aa9ebccSVladimir Oltean 			       struct net_device *br)
17878aa9ebccSVladimir Oltean {
17888aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
17898aa9ebccSVladimir Oltean }
17908aa9ebccSVladimir Oltean 
17918aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
17928aa9ebccSVladimir Oltean 				 struct net_device *br)
17938aa9ebccSVladimir Oltean {
17948aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
17958aa9ebccSVladimir Oltean }
17968aa9ebccSVladimir Oltean 
17974d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8)
17984d752508SVladimir Oltean 
17994d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
18004d752508SVladimir Oltean {
18014d752508SVladimir Oltean 	int i;
18024d752508SVladimir Oltean 
18034d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
18044d752508SVladimir Oltean 		if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
18054d752508SVladimir Oltean 			return i;
18064d752508SVladimir Oltean 
18074d752508SVladimir Oltean 	return -1;
18084d752508SVladimir Oltean }
18094d752508SVladimir Oltean 
18104d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
18114d752508SVladimir Oltean 				     int prio)
18124d752508SVladimir Oltean {
18134d752508SVladimir Oltean 	int i;
18144d752508SVladimir Oltean 
18154d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
18164d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
18174d752508SVladimir Oltean 
18184d752508SVladimir Oltean 		if (cbs->port == port && cbs->prio == prio) {
18194d752508SVladimir Oltean 			memset(cbs, 0, sizeof(*cbs));
18204d752508SVladimir Oltean 			return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
18214d752508SVladimir Oltean 							    i, cbs, true);
18224d752508SVladimir Oltean 		}
18234d752508SVladimir Oltean 	}
18244d752508SVladimir Oltean 
18254d752508SVladimir Oltean 	return 0;
18264d752508SVladimir Oltean }
18274d752508SVladimir Oltean 
18284d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
18294d752508SVladimir Oltean 				struct tc_cbs_qopt_offload *offload)
18304d752508SVladimir Oltean {
18314d752508SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18324d752508SVladimir Oltean 	struct sja1105_cbs_entry *cbs;
18334d752508SVladimir Oltean 	int index;
18344d752508SVladimir Oltean 
18354d752508SVladimir Oltean 	if (!offload->enable)
18364d752508SVladimir Oltean 		return sja1105_delete_cbs_shaper(priv, port, offload->queue);
18374d752508SVladimir Oltean 
18384d752508SVladimir Oltean 	index = sja1105_find_unused_cbs_shaper(priv);
18394d752508SVladimir Oltean 	if (index < 0)
18404d752508SVladimir Oltean 		return -ENOSPC;
18414d752508SVladimir Oltean 
18424d752508SVladimir Oltean 	cbs = &priv->cbs[index];
18434d752508SVladimir Oltean 	cbs->port = port;
18444d752508SVladimir Oltean 	cbs->prio = offload->queue;
18454d752508SVladimir Oltean 	/* locredit and sendslope are negative by definition. In hardware,
18464d752508SVladimir Oltean 	 * positive values must be provided, and the negative sign is implicit.
18474d752508SVladimir Oltean 	 */
18484d752508SVladimir Oltean 	cbs->credit_hi = offload->hicredit;
18494d752508SVladimir Oltean 	cbs->credit_lo = abs(offload->locredit);
18504d752508SVladimir Oltean 	/* User space is in kbits/sec, hardware in bytes/sec */
18514d752508SVladimir Oltean 	cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
18524d752508SVladimir Oltean 	cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
18534d752508SVladimir Oltean 	/* Convert the negative values from 64-bit 2's complement
18544d752508SVladimir Oltean 	 * to 32-bit 2's complement (for the case of 0x80000000 whose
18554d752508SVladimir Oltean 	 * negative is still negative).
18564d752508SVladimir Oltean 	 */
18574d752508SVladimir Oltean 	cbs->credit_lo &= GENMASK_ULL(31, 0);
18584d752508SVladimir Oltean 	cbs->send_slope &= GENMASK_ULL(31, 0);
18594d752508SVladimir Oltean 
18604d752508SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
18614d752508SVladimir Oltean 					    true);
18624d752508SVladimir Oltean }
18634d752508SVladimir Oltean 
18644d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv)
18654d752508SVladimir Oltean {
18664d752508SVladimir Oltean 	int rc = 0, i;
18674d752508SVladimir Oltean 
1868be7f62eeSVladimir Oltean 	/* The credit based shapers are only allocated if
1869be7f62eeSVladimir Oltean 	 * CONFIG_NET_SCH_CBS is enabled.
1870be7f62eeSVladimir Oltean 	 */
1871be7f62eeSVladimir Oltean 	if (!priv->cbs)
1872be7f62eeSVladimir Oltean 		return 0;
1873be7f62eeSVladimir Oltean 
18744d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
18754d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
18764d752508SVladimir Oltean 
18774d752508SVladimir Oltean 		if (!cbs->idle_slope && !cbs->send_slope)
18784d752508SVladimir Oltean 			continue;
18794d752508SVladimir Oltean 
18804d752508SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
18814d752508SVladimir Oltean 						  true);
18824d752508SVladimir Oltean 		if (rc)
18834d752508SVladimir Oltean 			break;
18844d752508SVladimir Oltean 	}
18854d752508SVladimir Oltean 
18864d752508SVladimir Oltean 	return rc;
18874d752508SVladimir Oltean }
18884d752508SVladimir Oltean 
18892eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
18902eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
18912eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
18922eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
18932eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
1894c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1895dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
18962eea1fa8SVladimir Oltean };
18972eea1fa8SVladimir Oltean 
18986666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
18996666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
19006666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
19016666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
19026666cebcSVladimir Oltean  * such that this operation is relatively seamless.
19036666cebcSVladimir Oltean  */
19042eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
19052eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
19066666cebcSVladimir Oltean {
19076cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
19086cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
190982760d7fSVladimir Oltean 	int speed_mbps[SJA1105_MAX_NUM_PORTS];
191084db00f2SVladimir Oltean 	u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0};
19116666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
19126cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
19136cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
19146cf99c13SVladimir Oltean 	s64 t12, t34;
19156666cebcSVladimir Oltean 	int rc, i;
19166cf99c13SVladimir Oltean 	s64 now;
19176666cebcSVladimir Oltean 
1918af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1919af580ae2SVladimir Oltean 
19206666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
19216666cebcSVladimir Oltean 
19228400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
19238400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
19248400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
19258400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
19266666cebcSVladimir Oltean 	 */
1927542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
19283ad1d171SVladimir Oltean 		u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1);
19293ad1d171SVladimir Oltean 
193041fed17fSVladimir Oltean 		speed_mbps[i] = sja1105_port_speed_to_ethtool(priv,
193141fed17fSVladimir Oltean 							      mac[i].speed);
193241fed17fSVladimir Oltean 		mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
19336666cebcSVladimir Oltean 
19343ad1d171SVladimir Oltean 		if (priv->xpcs[i])
19353ad1d171SVladimir Oltean 			bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr);
193684db00f2SVladimir Oltean 	}
1937ffe10e67SVladimir Oltean 
19386cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
19396cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
19406cf99c13SVladimir Oltean 
19416cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
194261c77533SVladimir Oltean 	if (rc < 0) {
194361c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
194461c77533SVladimir Oltean 		goto out;
194561c77533SVladimir Oltean 	}
19466cf99c13SVladimir Oltean 
19476666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
19486666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
194961c77533SVladimir Oltean 	if (rc < 0) {
195061c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
195161c77533SVladimir Oltean 		goto out;
195261c77533SVladimir Oltean 	}
19536cf99c13SVladimir Oltean 
19546cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
195561c77533SVladimir Oltean 	if (rc < 0) {
195661c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
195761c77533SVladimir Oltean 		goto out;
195861c77533SVladimir Oltean 	}
19596cf99c13SVladimir Oltean 
19606cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
19616cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
19626cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
19636cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
19646cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
19656cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
19666cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
19676cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
19686cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
19696cf99c13SVladimir Oltean 	now += (t34 - t12);
19706cf99c13SVladimir Oltean 
19716cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
19726cf99c13SVladimir Oltean 
19736cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
19746666cebcSVladimir Oltean 
19752eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
19762eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
19772eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
19782eea1fa8SVladimir Oltean 
19796666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
19806666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
19816666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
19826666cebcSVladimir Oltean 	 */
1983cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
1984c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
19856666cebcSVladimir Oltean 		if (rc < 0)
19866666cebcSVladimir Oltean 			goto out;
1987cb5a82d2SVladimir Oltean 	}
19886666cebcSVladimir Oltean 
1989542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
19903ad1d171SVladimir Oltean 		struct dw_xpcs *xpcs = priv->xpcs[i];
19913ad1d171SVladimir Oltean 		unsigned int mode;
199284db00f2SVladimir Oltean 
19938400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
19946666cebcSVladimir Oltean 		if (rc < 0)
19956666cebcSVladimir Oltean 			goto out;
1996ffe10e67SVladimir Oltean 
19973ad1d171SVladimir Oltean 		if (!xpcs)
199884db00f2SVladimir Oltean 			continue;
1999ffe10e67SVladimir Oltean 
20003ad1d171SVladimir Oltean 		if (bmcr[i] & BMCR_ANENABLE)
20013ad1d171SVladimir Oltean 			mode = MLO_AN_INBAND;
20023ad1d171SVladimir Oltean 		else if (priv->fixed_link[i])
20033ad1d171SVladimir Oltean 			mode = MLO_AN_FIXED;
20043ad1d171SVladimir Oltean 		else
20053ad1d171SVladimir Oltean 			mode = MLO_AN_PHY;
200684db00f2SVladimir Oltean 
20073ad1d171SVladimir Oltean 		rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode);
20083ad1d171SVladimir Oltean 		if (rc < 0)
20093ad1d171SVladimir Oltean 			goto out;
2010ffe10e67SVladimir Oltean 
20113ad1d171SVladimir Oltean 		if (!phylink_autoneg_inband(mode)) {
2012ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
2013ffe10e67SVladimir Oltean 
201456b63466SVladimir Oltean 			if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX)
201556b63466SVladimir Oltean 				speed = SPEED_2500;
201656b63466SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED1000)
2017ffe10e67SVladimir Oltean 				speed = SPEED_1000;
201884db00f2SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED100)
2019ffe10e67SVladimir Oltean 				speed = SPEED_100;
2020053d8ad1SVladimir Oltean 			else
2021ffe10e67SVladimir Oltean 				speed = SPEED_10;
2022ffe10e67SVladimir Oltean 
20233ad1d171SVladimir Oltean 			xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i],
20243ad1d171SVladimir Oltean 				     speed, DUPLEX_FULL);
2025ffe10e67SVladimir Oltean 		}
2026ffe10e67SVladimir Oltean 	}
20274d752508SVladimir Oltean 
20284d752508SVladimir Oltean 	rc = sja1105_reload_cbs(priv);
20294d752508SVladimir Oltean 	if (rc < 0)
20304d752508SVladimir Oltean 		goto out;
20316666cebcSVladimir Oltean out:
2032af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
2033af580ae2SVladimir Oltean 
20346666cebcSVladimir Oltean 	return rc;
20356666cebcSVladimir Oltean }
20366666cebcSVladimir Oltean 
20378aa9ebccSVladimir Oltean static enum dsa_tag_protocol
20384d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
20394d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
20408aa9ebccSVladimir Oltean {
20414913b8ebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
20424913b8ebSVladimir Oltean 
20434913b8ebSVladimir Oltean 	return priv->info->tag_proto;
20448aa9ebccSVladimir Oltean }
20458aa9ebccSVladimir Oltean 
2046070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
2047070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
2048070ca3bbSVladimir Oltean  * So a switch reset is required.
2049070ca3bbSVladimir Oltean  */
205089153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
205189153ed6SVladimir Oltean 			   struct netlink_ext_ack *extack)
20526666cebcSVladimir Oltean {
20536d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2054070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
20556666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2056070ca3bbSVladimir Oltean 	struct sja1105_table *table;
2057dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
2058070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
20596666cebcSVladimir Oltean 	int rc;
20606666cebcSVladimir Oltean 
2061dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
2062dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
206389153ed6SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
206489153ed6SVladimir Oltean 					   "Cannot change VLAN filtering with active VL rules");
2065dfacc5a2SVladimir Oltean 			return -EBUSY;
2066dfacc5a2SVladimir Oltean 		}
2067dfacc5a2SVladimir Oltean 	}
2068dfacc5a2SVladimir Oltean 
2069070ca3bbSVladimir Oltean 	if (enabled) {
20706666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
207154fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
207254fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
2073070ca3bbSVladimir Oltean 	} else {
20746666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
2075070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
2076070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
2077070ca3bbSVladimir Oltean 	}
2078070ca3bbSVladimir Oltean 
207938b5beeaSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
208038b5beeaSVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
208138b5beeaSVladimir Oltean 
208238b5beeaSVladimir Oltean 		if (enabled)
208338b5beeaSVladimir Oltean 			sp->xmit_tpid = priv->info->qinq_tpid;
208438b5beeaSVladimir Oltean 		else
208538b5beeaSVladimir Oltean 			sp->xmit_tpid = ETH_P_SJA1105;
208638b5beeaSVladimir Oltean 	}
208738b5beeaSVladimir Oltean 
20880fac6aa0SVladimir Oltean 	if (priv->vlan_aware == enabled)
2089cfa36b1fSVladimir Oltean 		return 0;
2090cfa36b1fSVladimir Oltean 
20910fac6aa0SVladimir Oltean 	priv->vlan_aware = enabled;
20927f14937fSVladimir Oltean 
2093070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2094070ca3bbSVladimir Oltean 	general_params = table->entries;
2095f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
209654fa49eeSVladimir Oltean 	general_params->tpid = tpid;
209754fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2098070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
209942824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
210042824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
210142824463SVladimir Oltean 	 */
210242824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
210342824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
2104070ca3bbSVladimir Oltean 
21056d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
21062cafa72eSVladimir Oltean 	 * No VLAN filtering (or best effort) => shared VLAN learning.
21076d7c7d94SVladimir Oltean 	 *
21086d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
21096d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
21106d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
21116d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
21126d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
21136d7c7d94SVladimir Oltean 	 * forwarding decision.
21146d7c7d94SVladimir Oltean 	 *
21156d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
21166d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
21176d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
21186d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
21196d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
21206d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
21216d7c7d94SVladimir Oltean 	 * (all frames get flooded).
21226d7c7d94SVladimir Oltean 	 */
21236d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
21246d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
21250fac6aa0SVladimir Oltean 	l2_lookup_params->shared_learn = !priv->vlan_aware;
2126aaa270c6SVladimir Oltean 
21276dfd23d3SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
21286dfd23d3SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
21296dfd23d3SVladimir Oltean 			continue;
21306dfd23d3SVladimir Oltean 
21316dfd23d3SVladimir Oltean 		rc = sja1105_commit_pvid(ds, port);
2132aef31718SVladimir Oltean 		if (rc)
2133aef31718SVladimir Oltean 			return rc;
21346dfd23d3SVladimir Oltean 	}
2135aef31718SVladimir Oltean 
21362eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
21376666cebcSVladimir Oltean 	if (rc)
213889153ed6SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype");
21396666cebcSVladimir Oltean 
21400fac6aa0SVladimir Oltean 	return rc;
21416666cebcSVladimir Oltean }
21426666cebcSVladimir Oltean 
21436dfd23d3SVladimir Oltean static int sja1105_vlan_add(struct sja1105_private *priv, int port, u16 vid,
21446dfd23d3SVladimir Oltean 			    u16 flags)
21455899ee36SVladimir Oltean {
21466dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
21476dfd23d3SVladimir Oltean 	struct sja1105_table *table;
21486dfd23d3SVladimir Oltean 	int match, rc;
21495899ee36SVladimir Oltean 
21506dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
21516dfd23d3SVladimir Oltean 
21526dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
21536dfd23d3SVladimir Oltean 	if (match < 0) {
21546dfd23d3SVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
21556dfd23d3SVladimir Oltean 		if (rc)
21566dfd23d3SVladimir Oltean 			return rc;
21576dfd23d3SVladimir Oltean 		match = table->entry_count - 1;
21586dfd23d3SVladimir Oltean 	}
21596dfd23d3SVladimir Oltean 
21606dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
21616dfd23d3SVladimir Oltean 	vlan = table->entries;
21626dfd23d3SVladimir Oltean 
21636dfd23d3SVladimir Oltean 	vlan[match].type_entry = SJA1110_VLAN_D_TAG;
21646dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
21656dfd23d3SVladimir Oltean 	vlan[match].vlan_bc |= BIT(port);
21666dfd23d3SVladimir Oltean 	vlan[match].vmemb_port |= BIT(port);
21676dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
21686dfd23d3SVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
21696dfd23d3SVladimir Oltean 	else
21706dfd23d3SVladimir Oltean 		vlan[match].tag_port |= BIT(port);
21716dfd23d3SVladimir Oltean 
21726dfd23d3SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
21736dfd23d3SVladimir Oltean 					    &vlan[match], true);
21746dfd23d3SVladimir Oltean }
21756dfd23d3SVladimir Oltean 
21766dfd23d3SVladimir Oltean static int sja1105_vlan_del(struct sja1105_private *priv, int port, u16 vid)
21776dfd23d3SVladimir Oltean {
21786dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
21796dfd23d3SVladimir Oltean 	struct sja1105_table *table;
21806dfd23d3SVladimir Oltean 	bool keep = true;
21816dfd23d3SVladimir Oltean 	int match, rc;
21826dfd23d3SVladimir Oltean 
21836dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
21846dfd23d3SVladimir Oltean 
21856dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
21866dfd23d3SVladimir Oltean 	/* Can't delete a missing entry. */
21876dfd23d3SVladimir Oltean 	if (match < 0)
21885899ee36SVladimir Oltean 		return 0;
21895899ee36SVladimir Oltean 
21906dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
21916dfd23d3SVladimir Oltean 	vlan = table->entries;
21926dfd23d3SVladimir Oltean 
21936dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
21946dfd23d3SVladimir Oltean 	vlan[match].vlan_bc &= ~BIT(port);
21956dfd23d3SVladimir Oltean 	vlan[match].vmemb_port &= ~BIT(port);
21966dfd23d3SVladimir Oltean 	/* Also unset tag_port, just so we don't have a confusing bitmap
21976dfd23d3SVladimir Oltean 	 * (no practical purpose).
2198b38e659dSVladimir Oltean 	 */
21996dfd23d3SVladimir Oltean 	vlan[match].tag_port &= ~BIT(port);
2200b38e659dSVladimir Oltean 
22016dfd23d3SVladimir Oltean 	/* If there's no port left as member of this VLAN,
22026dfd23d3SVladimir Oltean 	 * it's time for it to go.
22036dfd23d3SVladimir Oltean 	 */
22046dfd23d3SVladimir Oltean 	if (!vlan[match].vmemb_port)
22056dfd23d3SVladimir Oltean 		keep = false;
22065899ee36SVladimir Oltean 
22076dfd23d3SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
22086dfd23d3SVladimir Oltean 					  &vlan[match], keep);
22096dfd23d3SVladimir Oltean 	if (rc < 0)
22106dfd23d3SVladimir Oltean 		return rc;
22115899ee36SVladimir Oltean 
22126dfd23d3SVladimir Oltean 	if (!keep)
22136dfd23d3SVladimir Oltean 		return sja1105_table_delete_entry(table, match);
22145899ee36SVladimir Oltean 
22155899ee36SVladimir Oltean 	return 0;
22165899ee36SVladimir Oltean }
22175899ee36SVladimir Oltean 
22186dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_add(struct dsa_switch *ds, int port,
221931046a5fSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan,
222031046a5fSVladimir Oltean 				   struct netlink_ext_ack *extack)
22216666cebcSVladimir Oltean {
22226666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2223884be12fSVladimir Oltean 	u16 flags = vlan->flags;
22246666cebcSVladimir Oltean 	int rc;
22256666cebcSVladimir Oltean 
22260fac6aa0SVladimir Oltean 	/* Be sure to deny alterations to the configuration done by tag_8021q.
22271958d581SVladimir Oltean 	 */
22280fac6aa0SVladimir Oltean 	if (vid_is_dsa_8021q(vlan->vid)) {
222931046a5fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
223031046a5fSVladimir Oltean 				   "Range 1024-3071 reserved for dsa_8021q operation");
22311958d581SVladimir Oltean 		return -EBUSY;
22321958d581SVladimir Oltean 	}
22331958d581SVladimir Oltean 
2234884be12fSVladimir Oltean 	/* Always install bridge VLANs as egress-tagged on the CPU port. */
2235884be12fSVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
2236884be12fSVladimir Oltean 		flags = 0;
2237884be12fSVladimir Oltean 
2238884be12fSVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vlan->vid, flags);
22396dfd23d3SVladimir Oltean 	if (rc)
22401958d581SVladimir Oltean 		return rc;
2241ec5ae610SVladimir Oltean 
22426dfd23d3SVladimir Oltean 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
22436dfd23d3SVladimir Oltean 		priv->bridge_pvid[port] = vlan->vid;
2244ec5ae610SVladimir Oltean 
22456dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
22466666cebcSVladimir Oltean }
22476666cebcSVladimir Oltean 
22486dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port,
22496666cebcSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan)
22506666cebcSVladimir Oltean {
22516666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2252*bef0746cSVladimir Oltean 	int rc;
22536666cebcSVladimir Oltean 
2254*bef0746cSVladimir Oltean 	rc = sja1105_vlan_del(priv, port, vlan->vid);
2255*bef0746cSVladimir Oltean 	if (rc)
2256*bef0746cSVladimir Oltean 		return rc;
2257*bef0746cSVladimir Oltean 
2258*bef0746cSVladimir Oltean 	/* In case the pvid was deleted, make sure that untagged packets will
2259*bef0746cSVladimir Oltean 	 * be dropped.
2260*bef0746cSVladimir Oltean 	 */
2261*bef0746cSVladimir Oltean 	return sja1105_commit_pvid(ds, port);
22626666cebcSVladimir Oltean }
22636666cebcSVladimir Oltean 
22645899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
22655899ee36SVladimir Oltean 				      u16 flags)
22665899ee36SVladimir Oltean {
22675899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
22685899ee36SVladimir Oltean 	int rc;
22695899ee36SVladimir Oltean 
22706dfd23d3SVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vid, flags);
22716dfd23d3SVladimir Oltean 	if (rc)
22725899ee36SVladimir Oltean 		return rc;
22735899ee36SVladimir Oltean 
22746dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_PVID)
22756dfd23d3SVladimir Oltean 		priv->tag_8021q_pvid[port] = vid;
22766dfd23d3SVladimir Oltean 
22776dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
22785899ee36SVladimir Oltean }
22795899ee36SVladimir Oltean 
22805899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
22815899ee36SVladimir Oltean {
22825899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
22835899ee36SVladimir Oltean 
22846dfd23d3SVladimir Oltean 	return sja1105_vlan_del(priv, port, vid);
22855899ee36SVladimir Oltean }
22865899ee36SVladimir Oltean 
22874fbc08bdSVladimir Oltean static int sja1105_prechangeupper(struct dsa_switch *ds, int port,
22884fbc08bdSVladimir Oltean 				  struct netdev_notifier_changeupper_info *info)
22894fbc08bdSVladimir Oltean {
22904fbc08bdSVladimir Oltean 	struct netlink_ext_ack *extack = info->info.extack;
22914fbc08bdSVladimir Oltean 	struct net_device *upper = info->upper_dev;
229219fa937aSVladimir Oltean 	struct dsa_switch_tree *dst = ds->dst;
229319fa937aSVladimir Oltean 	struct dsa_port *dp;
22944fbc08bdSVladimir Oltean 
22954fbc08bdSVladimir Oltean 	if (is_vlan_dev(upper)) {
22964fbc08bdSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "8021q uppers are not supported");
22974fbc08bdSVladimir Oltean 		return -EBUSY;
22984fbc08bdSVladimir Oltean 	}
22994fbc08bdSVladimir Oltean 
230019fa937aSVladimir Oltean 	if (netif_is_bridge_master(upper)) {
230119fa937aSVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
230219fa937aSVladimir Oltean 			if (dp->bridge_dev && dp->bridge_dev != upper &&
230319fa937aSVladimir Oltean 			    br_vlan_enabled(dp->bridge_dev)) {
230419fa937aSVladimir Oltean 				NL_SET_ERR_MSG_MOD(extack,
230519fa937aSVladimir Oltean 						   "Only one VLAN-aware bridge is supported");
230619fa937aSVladimir Oltean 				return -EBUSY;
230719fa937aSVladimir Oltean 			}
230819fa937aSVladimir Oltean 		}
230919fa937aSVladimir Oltean 	}
231019fa937aSVladimir Oltean 
23114fbc08bdSVladimir Oltean 	return 0;
23124fbc08bdSVladimir Oltean }
23134fbc08bdSVladimir Oltean 
23148aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
23158aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
23168aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
23178aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
23188aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
23198aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
23208aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
23218aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
23228aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
23238aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
23248aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
23258aa9ebccSVladimir Oltean  */
23268aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
23278aa9ebccSVladimir Oltean {
23288aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
23298aa9ebccSVladimir Oltean 	int rc;
23308aa9ebccSVladimir Oltean 
23315d645df9SVladimir Oltean 	rc = sja1105_parse_dt(priv);
23328aa9ebccSVladimir Oltean 	if (rc < 0) {
23338aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
23348aa9ebccSVladimir Oltean 		return rc;
23358aa9ebccSVladimir Oltean 	}
2336f5b8631cSVladimir Oltean 
2337f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
2338f5b8631cSVladimir Oltean 	 * and we can't apply them.
2339f5b8631cSVladimir Oltean 	 */
234029afb83aSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv);
2341f5b8631cSVladimir Oltean 	if (rc < 0) {
2342f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
2343f5b8631cSVladimir Oltean 		return rc;
2344f5b8631cSVladimir Oltean 	}
2345f5b8631cSVladimir Oltean 
234661c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
2347bb77f36aSVladimir Oltean 	if (rc < 0) {
2348bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
2349bb77f36aSVladimir Oltean 		return rc;
2350bb77f36aSVladimir Oltean 	}
23515a8f0974SVladimir Oltean 
23525a8f0974SVladimir Oltean 	rc = sja1105_mdiobus_register(ds);
23535a8f0974SVladimir Oltean 	if (rc < 0) {
23545a8f0974SVladimir Oltean 		dev_err(ds->dev, "Failed to register MDIO bus: %pe\n",
23555a8f0974SVladimir Oltean 			ERR_PTR(rc));
23565a8f0974SVladimir Oltean 		goto out_ptp_clock_unregister;
23575a8f0974SVladimir Oltean 	}
23585a8f0974SVladimir Oltean 
2359cb5a82d2SVladimir Oltean 	if (priv->info->disable_microcontroller) {
2360cb5a82d2SVladimir Oltean 		rc = priv->info->disable_microcontroller(priv);
2361cb5a82d2SVladimir Oltean 		if (rc < 0) {
2362cb5a82d2SVladimir Oltean 			dev_err(ds->dev,
2363cb5a82d2SVladimir Oltean 				"Failed to disable microcontroller: %pe\n",
2364cb5a82d2SVladimir Oltean 				ERR_PTR(rc));
2365cb5a82d2SVladimir Oltean 			goto out_mdiobus_unregister;
2366cb5a82d2SVladimir Oltean 		}
2367cb5a82d2SVladimir Oltean 	}
2368cb5a82d2SVladimir Oltean 
23698aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
23705d645df9SVladimir Oltean 	rc = sja1105_static_config_load(priv);
23718aa9ebccSVladimir Oltean 	if (rc < 0) {
23728aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
23735a8f0974SVladimir Oltean 		goto out_mdiobus_unregister;
23748aa9ebccSVladimir Oltean 	}
2375cb5a82d2SVladimir Oltean 
23768aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
2377cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
2378c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
23798aa9ebccSVladimir Oltean 		if (rc < 0) {
2380cb5a82d2SVladimir Oltean 			dev_err(ds->dev,
2381cb5a82d2SVladimir Oltean 				"Failed to configure MII clocking: %pe\n",
2382cb5a82d2SVladimir Oltean 				ERR_PTR(rc));
2383cec279a8SVladimir Oltean 			goto out_static_config_free;
23848aa9ebccSVladimir Oltean 		}
2385cb5a82d2SVladimir Oltean 	}
2386cb5a82d2SVladimir Oltean 
23876666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
23886666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
23896666cebcSVladimir Oltean 	 * EtherType is.
23906666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
23916666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
23926666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
23936666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
23946666cebcSVladimir Oltean 	 */
23956666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
2396884be12fSVladimir Oltean 	ds->untag_bridge_pvid = true;
2397b6ad86e6SVladimir Oltean 	/* tag_8021q has 3 bits for the VBID, and the value 0 is reserved */
2398b6ad86e6SVladimir Oltean 	ds->num_fwd_offloading_bridges = 7;
23998aa9ebccSVladimir Oltean 
24005f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
24015f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
24025f06c63bSVladimir Oltean 
2403c279c726SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
2404c279c726SVladimir Oltean 
24050a7bdbc2SVladimir Oltean 	rc = sja1105_devlink_setup(ds);
24062cafa72eSVladimir Oltean 	if (rc < 0)
2407cec279a8SVladimir Oltean 		goto out_static_config_free;
24082cafa72eSVladimir Oltean 
2409bbed0bbdSVladimir Oltean 	rtnl_lock();
2410328621f6SVladimir Oltean 	rc = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q));
2411bbed0bbdSVladimir Oltean 	rtnl_unlock();
2412cec279a8SVladimir Oltean 	if (rc)
2413cec279a8SVladimir Oltean 		goto out_devlink_teardown;
2414cec279a8SVladimir Oltean 
2415cec279a8SVladimir Oltean 	return 0;
2416cec279a8SVladimir Oltean 
2417cec279a8SVladimir Oltean out_devlink_teardown:
2418cec279a8SVladimir Oltean 	sja1105_devlink_teardown(ds);
24195a8f0974SVladimir Oltean out_mdiobus_unregister:
24205a8f0974SVladimir Oltean 	sja1105_mdiobus_unregister(ds);
2421cec279a8SVladimir Oltean out_ptp_clock_unregister:
2422cec279a8SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
2423cec279a8SVladimir Oltean out_static_config_free:
2424cec279a8SVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2425bbed0bbdSVladimir Oltean 
2426bbed0bbdSVladimir Oltean 	return rc;
2427227d07a0SVladimir Oltean }
2428227d07a0SVladimir Oltean 
2429f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
2430f3097be2SVladimir Oltean {
2431f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2432a68578c2SVladimir Oltean 	int port;
2433a68578c2SVladimir Oltean 
2434328621f6SVladimir Oltean 	rtnl_lock();
2435328621f6SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
2436328621f6SVladimir Oltean 	rtnl_unlock();
2437328621f6SVladimir Oltean 
2438542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
2439a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2440a68578c2SVladimir Oltean 
2441a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2442a68578c2SVladimir Oltean 			continue;
2443a68578c2SVladimir Oltean 
244452c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
2445a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
2446a68578c2SVladimir Oltean 	}
2447f3097be2SVladimir Oltean 
24480a7bdbc2SVladimir Oltean 	sja1105_devlink_teardown(ds);
2449a6af7763SVladimir Oltean 	sja1105_flower_teardown(ds);
2450317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
245161c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
24526cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2453f3097be2SVladimir Oltean }
2454f3097be2SVladimir Oltean 
2455a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
2456a68578c2SVladimir Oltean {
2457a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2458a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
2459a68578c2SVladimir Oltean 
2460a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2461a68578c2SVladimir Oltean 		return;
2462a68578c2SVladimir Oltean 
2463a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
2464a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
2465a68578c2SVladimir Oltean }
2466a68578c2SVladimir Oltean 
2467227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
246847ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2469227d07a0SVladimir Oltean {
2470227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2471227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2472227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2473227d07a0SVladimir Oltean 	int timeout = 10;
2474227d07a0SVladimir Oltean 	int rc;
2475227d07a0SVladimir Oltean 
2476227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2477227d07a0SVladimir Oltean 
2478227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2479227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2480227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
248147ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
248247ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2483227d07a0SVladimir Oltean 
2484227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2485227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2486227d07a0SVladimir Oltean 	if (rc < 0) {
2487227d07a0SVladimir Oltean 		kfree_skb(skb);
2488227d07a0SVladimir Oltean 		return rc;
2489227d07a0SVladimir Oltean 	}
2490227d07a0SVladimir Oltean 
2491227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
249268bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2493227d07a0SVladimir Oltean 
2494227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2495227d07a0SVladimir Oltean 	do {
2496227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2497227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2498227d07a0SVladimir Oltean 		if (rc < 0) {
2499227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2500227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2501227d07a0SVladimir Oltean 			continue;
2502227d07a0SVladimir Oltean 		}
2503227d07a0SVladimir Oltean 
2504227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2505227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2506227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2507227d07a0SVladimir Oltean 		 */
2508227d07a0SVladimir Oltean 		cpu_relax();
2509227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2510227d07a0SVladimir Oltean 
2511227d07a0SVladimir Oltean 	if (!timeout) {
2512227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2513227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
25142a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
25152a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2516227d07a0SVladimir Oltean 		 */
2517227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2518227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2519227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2520227d07a0SVladimir Oltean 	}
2521227d07a0SVladimir Oltean 
2522227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2523227d07a0SVladimir Oltean }
2524227d07a0SVladimir Oltean 
2525a68578c2SVladimir Oltean #define work_to_port(work) \
2526a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
2527a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
2528a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
2529a68578c2SVladimir Oltean 
2530227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2531227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2532227d07a0SVladimir Oltean  * lock on the bus)
2533227d07a0SVladimir Oltean  */
2534a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2535227d07a0SVladimir Oltean {
2536a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
2537a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
2538a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
2539a68578c2SVladimir Oltean 	int port = sp - priv->ports;
2540a68578c2SVladimir Oltean 	struct sk_buff *skb;
2541a68578c2SVladimir Oltean 
2542a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
2543c4b364ceSYangbo Lu 		struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
2544227d07a0SVladimir Oltean 
2545227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
2546227d07a0SVladimir Oltean 
2547a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
2548a68578c2SVladimir Oltean 
254947ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
2550a68578c2SVladimir Oltean 		if (clone)
2551a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
2552227d07a0SVladimir Oltean 
2553227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
2554a68578c2SVladimir Oltean 	}
25558aa9ebccSVladimir Oltean }
25568aa9ebccSVladimir Oltean 
25578456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
25588456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
25598456721dSVladimir Oltean  */
25608456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
25618456721dSVladimir Oltean 				   unsigned int ageing_time)
25628456721dSVladimir Oltean {
25638456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
25648456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
25658456721dSVladimir Oltean 	struct sja1105_table *table;
25668456721dSVladimir Oltean 	unsigned int maxage;
25678456721dSVladimir Oltean 
25688456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
25698456721dSVladimir Oltean 	l2_lookup_params = table->entries;
25708456721dSVladimir Oltean 
25718456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
25728456721dSVladimir Oltean 
25738456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
25748456721dSVladimir Oltean 		return 0;
25758456721dSVladimir Oltean 
25768456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
25778456721dSVladimir Oltean 
25782eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
25798456721dSVladimir Oltean }
25808456721dSVladimir Oltean 
2581c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
2582c279c726SVladimir Oltean {
2583c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2584c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2585c279c726SVladimir Oltean 
2586c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
2587c279c726SVladimir Oltean 
2588c279c726SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
2589c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
2590c279c726SVladimir Oltean 
2591c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2592c279c726SVladimir Oltean 
2593a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
2594c279c726SVladimir Oltean 		return 0;
2595c279c726SVladimir Oltean 
2596a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
2597c279c726SVladimir Oltean 
2598c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2599c279c726SVladimir Oltean }
2600c279c726SVladimir Oltean 
2601c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
2602c279c726SVladimir Oltean {
2603c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
2604c279c726SVladimir Oltean }
2605c279c726SVladimir Oltean 
2606317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2607317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2608317ab5b8SVladimir Oltean 				 void *type_data)
2609317ab5b8SVladimir Oltean {
2610317ab5b8SVladimir Oltean 	switch (type) {
2611317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2612317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
26134d752508SVladimir Oltean 	case TC_SETUP_QDISC_CBS:
26144d752508SVladimir Oltean 		return sja1105_setup_tc_cbs(ds, port, type_data);
2615317ab5b8SVladimir Oltean 	default:
2616317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2617317ab5b8SVladimir Oltean 	}
2618317ab5b8SVladimir Oltean }
2619317ab5b8SVladimir Oltean 
2620511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2621511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2622511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2623511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2624511e6ca0SVladimir Oltean  * mirroring rule that references it.
2625511e6ca0SVladimir Oltean  */
2626511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2627511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2628511e6ca0SVladimir Oltean {
2629511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2630511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2631542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2632511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2633511e6ca0SVladimir Oltean 	bool already_enabled;
2634511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2635511e6ca0SVladimir Oltean 	int rc;
2636511e6ca0SVladimir Oltean 
2637511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2638511e6ca0SVladimir Oltean 	general_params = table->entries;
2639511e6ca0SVladimir Oltean 
2640511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2641511e6ca0SVladimir Oltean 
2642542043e9SVladimir Oltean 	already_enabled = (general_params->mirr_port != ds->num_ports);
2643511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2644511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2645511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2646511e6ca0SVladimir Oltean 			general_params->mirr_port);
2647511e6ca0SVladimir Oltean 		return -EBUSY;
2648511e6ca0SVladimir Oltean 	}
2649511e6ca0SVladimir Oltean 
2650511e6ca0SVladimir Oltean 	new_mirr_port = to;
2651511e6ca0SVladimir Oltean 	if (!enabled) {
2652511e6ca0SVladimir Oltean 		bool keep = false;
2653511e6ca0SVladimir Oltean 		int port;
2654511e6ca0SVladimir Oltean 
2655511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2656542043e9SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
2657511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2658511e6ca0SVladimir Oltean 				keep = true;
2659511e6ca0SVladimir Oltean 				break;
2660511e6ca0SVladimir Oltean 			}
2661511e6ca0SVladimir Oltean 		}
2662511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2663511e6ca0SVladimir Oltean 		if (!keep)
2664542043e9SVladimir Oltean 			new_mirr_port = ds->num_ports;
2665511e6ca0SVladimir Oltean 	}
2666511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2667511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2668511e6ca0SVladimir Oltean 
2669511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2670511e6ca0SVladimir Oltean 						  0, general_params, true);
2671511e6ca0SVladimir Oltean 		if (rc < 0)
2672511e6ca0SVladimir Oltean 			return rc;
2673511e6ca0SVladimir Oltean 	}
2674511e6ca0SVladimir Oltean 
2675511e6ca0SVladimir Oltean 	if (ingress)
2676511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2677511e6ca0SVladimir Oltean 	else
2678511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2679511e6ca0SVladimir Oltean 
2680511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2681511e6ca0SVladimir Oltean 					    &mac[from], true);
2682511e6ca0SVladimir Oltean }
2683511e6ca0SVladimir Oltean 
2684511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2685511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2686511e6ca0SVladimir Oltean 			      bool ingress)
2687511e6ca0SVladimir Oltean {
2688511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2689511e6ca0SVladimir Oltean 				    ingress, true);
2690511e6ca0SVladimir Oltean }
2691511e6ca0SVladimir Oltean 
2692511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2693511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2694511e6ca0SVladimir Oltean {
2695511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2696511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2697511e6ca0SVladimir Oltean }
2698511e6ca0SVladimir Oltean 
2699a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
2700a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
2701a7cc081cSVladimir Oltean {
2702a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2703a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2704a7cc081cSVladimir Oltean 
2705a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2706a7cc081cSVladimir Oltean 
2707a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
2708a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
2709a7cc081cSVladimir Oltean 	 * bytes.
2710a7cc081cSVladimir Oltean 	 */
2711a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
2712a7cc081cSVladimir Oltean 				      1000000);
27135f035af7SPo Liu 	policing[port].smax = policer->burst;
2714a7cc081cSVladimir Oltean 
2715a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2716a7cc081cSVladimir Oltean }
2717a7cc081cSVladimir Oltean 
2718a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
2719a7cc081cSVladimir Oltean {
2720a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2721a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2722a7cc081cSVladimir Oltean 
2723a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2724a7cc081cSVladimir Oltean 
2725a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
2726a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
2727a7cc081cSVladimir Oltean 
2728a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2729a7cc081cSVladimir Oltean }
2730a7cc081cSVladimir Oltean 
27314d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port,
27324d942354SVladimir Oltean 				     bool enabled)
27334d942354SVladimir Oltean {
27344d942354SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
27354d942354SVladimir Oltean 	int rc;
27364d942354SVladimir Oltean 
27374d942354SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
27384d942354SVladimir Oltean 
27394c44fc5eSVladimir Oltean 	mac[port].dyn_learn = enabled;
27404d942354SVladimir Oltean 
27414d942354SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
27424d942354SVladimir Oltean 					  &mac[port], true);
27434d942354SVladimir Oltean 	if (rc)
27444d942354SVladimir Oltean 		return rc;
27454d942354SVladimir Oltean 
27464d942354SVladimir Oltean 	if (enabled)
27474d942354SVladimir Oltean 		priv->learn_ena |= BIT(port);
27484d942354SVladimir Oltean 	else
27494d942354SVladimir Oltean 		priv->learn_ena &= ~BIT(port);
27504d942354SVladimir Oltean 
27514d942354SVladimir Oltean 	return 0;
27524d942354SVladimir Oltean }
27534d942354SVladimir Oltean 
27544d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to,
27554d942354SVladimir Oltean 					  struct switchdev_brport_flags flags)
27564d942354SVladimir Oltean {
27574d942354SVladimir Oltean 	if (flags.mask & BR_FLOOD) {
27584d942354SVladimir Oltean 		if (flags.val & BR_FLOOD)
27597f7ccdeaSVladimir Oltean 			priv->ucast_egress_floods |= BIT(to);
27604d942354SVladimir Oltean 		else
27616a5166e0SVladimir Oltean 			priv->ucast_egress_floods &= ~BIT(to);
27624d942354SVladimir Oltean 	}
27637f7ccdeaSVladimir Oltean 
27644d942354SVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD) {
27654d942354SVladimir Oltean 		if (flags.val & BR_BCAST_FLOOD)
27667f7ccdeaSVladimir Oltean 			priv->bcast_egress_floods |= BIT(to);
27674d942354SVladimir Oltean 		else
27686a5166e0SVladimir Oltean 			priv->bcast_egress_floods &= ~BIT(to);
27694d942354SVladimir Oltean 	}
27704d942354SVladimir Oltean 
27717f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
27724d942354SVladimir Oltean }
27734d942354SVladimir Oltean 
27744d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to,
27754d942354SVladimir Oltean 				    struct switchdev_brport_flags flags,
27764d942354SVladimir Oltean 				    struct netlink_ext_ack *extack)
27774d942354SVladimir Oltean {
27784d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
27794d942354SVladimir Oltean 	struct sja1105_table *table;
27804d942354SVladimir Oltean 	int match;
27814d942354SVladimir Oltean 
27824d942354SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
27834d942354SVladimir Oltean 	l2_lookup = table->entries;
27844d942354SVladimir Oltean 
27854d942354SVladimir Oltean 	for (match = 0; match < table->entry_count; match++)
27864d942354SVladimir Oltean 		if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST &&
27874d942354SVladimir Oltean 		    l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
27884d942354SVladimir Oltean 			break;
27894d942354SVladimir Oltean 
27904d942354SVladimir Oltean 	if (match == table->entry_count) {
27914d942354SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
27924d942354SVladimir Oltean 				   "Could not find FDB entry for unknown multicast");
27934d942354SVladimir Oltean 		return -ENOSPC;
27944d942354SVladimir Oltean 	}
27954d942354SVladimir Oltean 
27964d942354SVladimir Oltean 	if (flags.val & BR_MCAST_FLOOD)
27974d942354SVladimir Oltean 		l2_lookup[match].destports |= BIT(to);
27984d942354SVladimir Oltean 	else
27994d942354SVladimir Oltean 		l2_lookup[match].destports &= ~BIT(to);
28004d942354SVladimir Oltean 
28014d942354SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
28024d942354SVladimir Oltean 					    l2_lookup[match].index,
28034d942354SVladimir Oltean 					    &l2_lookup[match],
28044d942354SVladimir Oltean 					    true);
28054d942354SVladimir Oltean }
28064d942354SVladimir Oltean 
28074d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port,
28084d942354SVladimir Oltean 					 struct switchdev_brport_flags flags,
28094d942354SVladimir Oltean 					 struct netlink_ext_ack *extack)
28104d942354SVladimir Oltean {
28114d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
28124d942354SVladimir Oltean 
28134d942354SVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
28144d942354SVladimir Oltean 			   BR_BCAST_FLOOD))
28154d942354SVladimir Oltean 		return -EINVAL;
28164d942354SVladimir Oltean 
28174d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) &&
28184d942354SVladimir Oltean 	    !priv->info->can_limit_mcast_flood) {
28194d942354SVladimir Oltean 		bool multicast = !!(flags.val & BR_MCAST_FLOOD);
28204d942354SVladimir Oltean 		bool unicast = !!(flags.val & BR_FLOOD);
28214d942354SVladimir Oltean 
28224d942354SVladimir Oltean 		if (unicast != multicast) {
28234d942354SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
28244d942354SVladimir Oltean 					   "This chip cannot configure multicast flooding independently of unicast");
28254d942354SVladimir Oltean 			return -EINVAL;
28264d942354SVladimir Oltean 		}
28274d942354SVladimir Oltean 	}
28284d942354SVladimir Oltean 
28294d942354SVladimir Oltean 	return 0;
28304d942354SVladimir Oltean }
28314d942354SVladimir Oltean 
28324d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port,
28334d942354SVladimir Oltean 				     struct switchdev_brport_flags flags,
28344d942354SVladimir Oltean 				     struct netlink_ext_ack *extack)
28354d942354SVladimir Oltean {
28364d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
28374d942354SVladimir Oltean 	int rc;
28384d942354SVladimir Oltean 
28394d942354SVladimir Oltean 	if (flags.mask & BR_LEARNING) {
28404d942354SVladimir Oltean 		bool learn_ena = !!(flags.val & BR_LEARNING);
28414d942354SVladimir Oltean 
28424d942354SVladimir Oltean 		rc = sja1105_port_set_learning(priv, port, learn_ena);
28434d942354SVladimir Oltean 		if (rc)
28444d942354SVladimir Oltean 			return rc;
28454d942354SVladimir Oltean 	}
28464d942354SVladimir Oltean 
28474d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) {
28484d942354SVladimir Oltean 		rc = sja1105_port_ucast_bcast_flood(priv, port, flags);
28494d942354SVladimir Oltean 		if (rc)
28504d942354SVladimir Oltean 			return rc;
28514d942354SVladimir Oltean 	}
28524d942354SVladimir Oltean 
28534d942354SVladimir Oltean 	/* For chips that can't offload BR_MCAST_FLOOD independently, there
28544d942354SVladimir Oltean 	 * is nothing to do here, we ensured the configuration is in sync by
28554d942354SVladimir Oltean 	 * offloading BR_FLOOD.
28564d942354SVladimir Oltean 	 */
28574d942354SVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) {
28584d942354SVladimir Oltean 		rc = sja1105_port_mcast_flood(priv, port, flags,
28594d942354SVladimir Oltean 					      extack);
28604d942354SVladimir Oltean 		if (rc)
28614d942354SVladimir Oltean 			return rc;
28624d942354SVladimir Oltean 	}
28634d942354SVladimir Oltean 
28644d942354SVladimir Oltean 	return 0;
28654d942354SVladimir Oltean }
28664d942354SVladimir Oltean 
28678aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
28688aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
28698aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
2870f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
28718456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
2872c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
2873c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
2874ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
2875af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
28768400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
28778400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
287852c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
287952c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
288052c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
2881bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
2882a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
2883291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
2884291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
2885291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
28868aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
28878aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
28884d942354SVladimir Oltean 	.port_pre_bridge_flags	= sja1105_port_pre_bridge_flags,
28894d942354SVladimir Oltean 	.port_bridge_flags	= sja1105_port_bridge_flags,
2890640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
28916666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
28926dfd23d3SVladimir Oltean 	.port_vlan_add		= sja1105_bridge_vlan_add,
28936dfd23d3SVladimir Oltean 	.port_vlan_del		= sja1105_bridge_vlan_del,
2894291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
2895291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
2896a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
2897a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
2898f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
289947ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
2900317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
2901511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
2902511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
2903a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
2904a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
2905a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
2906a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
2907834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
2908ff4cf8eaSVladimir Oltean 	.devlink_info_get	= sja1105_devlink_info_get,
29095da11eb4SVladimir Oltean 	.tag_8021q_vlan_add	= sja1105_dsa_8021q_vlan_add,
29105da11eb4SVladimir Oltean 	.tag_8021q_vlan_del	= sja1105_dsa_8021q_vlan_del,
29114fbc08bdSVladimir Oltean 	.port_prechangeupper	= sja1105_prechangeupper,
2912b6ad86e6SVladimir Oltean 	.port_bridge_tx_fwd_offload = dsa_tag_8021q_bridge_tx_fwd_offload,
2913b6ad86e6SVladimir Oltean 	.port_bridge_tx_fwd_unoffload = dsa_tag_8021q_bridge_tx_fwd_unoffload,
29148aa9ebccSVladimir Oltean };
29158aa9ebccSVladimir Oltean 
29160b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[];
29170b0e2997SVladimir Oltean 
29188aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
29198aa9ebccSVladimir Oltean {
29208aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
29218aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
29228aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
29230b0e2997SVladimir Oltean 	const struct of_device_id *match;
2924dff79620SVladimir Oltean 	u32 device_id;
29258aa9ebccSVladimir Oltean 	u64 part_no;
29268aa9ebccSVladimir Oltean 	int rc;
29278aa9ebccSVladimir Oltean 
292834d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
292934d76e9fSVladimir Oltean 			      NULL);
29308aa9ebccSVladimir Oltean 	if (rc < 0)
29318aa9ebccSVladimir Oltean 		return rc;
29328aa9ebccSVladimir Oltean 
29331bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
29341bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
29358aa9ebccSVladimir Oltean 	if (rc < 0)
29368aa9ebccSVladimir Oltean 		return rc;
29378aa9ebccSVladimir Oltean 
29388aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
29398aa9ebccSVladimir Oltean 
29405978fac0SNathan Chancellor 	for (match = sja1105_dt_ids; match->compatible[0]; match++) {
29410b0e2997SVladimir Oltean 		const struct sja1105_info *info = match->data;
29420b0e2997SVladimir Oltean 
29430b0e2997SVladimir Oltean 		/* Is what's been probed in our match table at all? */
29440b0e2997SVladimir Oltean 		if (info->device_id != device_id || info->part_no != part_no)
29450b0e2997SVladimir Oltean 			continue;
29460b0e2997SVladimir Oltean 
29470b0e2997SVladimir Oltean 		/* But is it what's in the device tree? */
29480b0e2997SVladimir Oltean 		if (priv->info->device_id != device_id ||
29490b0e2997SVladimir Oltean 		    priv->info->part_no != part_no) {
29500b0e2997SVladimir Oltean 			dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
29510b0e2997SVladimir Oltean 				 priv->info->name, info->name);
29520b0e2997SVladimir Oltean 			/* It isn't. No problem, pick that up. */
29530b0e2997SVladimir Oltean 			priv->info = info;
29548aa9ebccSVladimir Oltean 		}
29558aa9ebccSVladimir Oltean 
29568aa9ebccSVladimir Oltean 		return 0;
29578aa9ebccSVladimir Oltean 	}
29588aa9ebccSVladimir Oltean 
29590b0e2997SVladimir Oltean 	dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
29600b0e2997SVladimir Oltean 		device_id, part_no);
29610b0e2997SVladimir Oltean 
29620b0e2997SVladimir Oltean 	return -ENODEV;
29630b0e2997SVladimir Oltean }
29640b0e2997SVladimir Oltean 
29658aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
29668aa9ebccSVladimir Oltean {
2967844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
29688aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
29698aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
2970718bad0eSVladimir Oltean 	size_t max_xfer, max_msg;
29718aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
2972a68578c2SVladimir Oltean 	int rc, port;
29738aa9ebccSVladimir Oltean 
29748aa9ebccSVladimir Oltean 	if (!dev->of_node) {
29758aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
29768aa9ebccSVladimir Oltean 		return -EINVAL;
29778aa9ebccSVladimir Oltean 	}
29788aa9ebccSVladimir Oltean 
29798aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
29808aa9ebccSVladimir Oltean 	if (!priv)
29818aa9ebccSVladimir Oltean 		return -ENOMEM;
29828aa9ebccSVladimir Oltean 
29838aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
29848aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
29858aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
29868aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
29878aa9ebccSVladimir Oltean 	else
29888aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
29898aa9ebccSVladimir Oltean 
29908aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
29918aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
29928aa9ebccSVladimir Oltean 	 */
29938aa9ebccSVladimir Oltean 	priv->spidev = spi;
29948aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
29958aa9ebccSVladimir Oltean 
29968aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
29978aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
29988aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
29998aa9ebccSVladimir Oltean 	if (rc < 0) {
30008aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
30018aa9ebccSVladimir Oltean 		return rc;
30028aa9ebccSVladimir Oltean 	}
30038aa9ebccSVladimir Oltean 
3004718bad0eSVladimir Oltean 	/* In sja1105_xfer, we send spi_messages composed of two spi_transfers:
3005718bad0eSVladimir Oltean 	 * a small one for the message header and another one for the current
3006718bad0eSVladimir Oltean 	 * chunk of the packed buffer.
3007718bad0eSVladimir Oltean 	 * Check that the restrictions imposed by the SPI controller are
3008718bad0eSVladimir Oltean 	 * respected: the chunk buffer is smaller than the max transfer size,
3009718bad0eSVladimir Oltean 	 * and the total length of the chunk plus its message header is smaller
3010718bad0eSVladimir Oltean 	 * than the max message size.
3011718bad0eSVladimir Oltean 	 * We do that during probe time since the maximum transfer size is a
3012718bad0eSVladimir Oltean 	 * runtime invariant.
3013718bad0eSVladimir Oltean 	 */
3014718bad0eSVladimir Oltean 	max_xfer = spi_max_transfer_size(spi);
3015718bad0eSVladimir Oltean 	max_msg = spi_max_message_size(spi);
3016718bad0eSVladimir Oltean 
3017718bad0eSVladimir Oltean 	/* We need to send at least one 64-bit word of SPI payload per message
3018718bad0eSVladimir Oltean 	 * in order to be able to make useful progress.
3019718bad0eSVladimir Oltean 	 */
3020718bad0eSVladimir Oltean 	if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) {
3021718bad0eSVladimir Oltean 		dev_err(dev, "SPI master cannot send large enough buffers, aborting\n");
3022718bad0eSVladimir Oltean 		return -EINVAL;
3023718bad0eSVladimir Oltean 	}
3024718bad0eSVladimir Oltean 
3025718bad0eSVladimir Oltean 	priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN;
3026718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_xfer)
3027718bad0eSVladimir Oltean 		priv->max_xfer_len = max_xfer;
3028718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER)
3029718bad0eSVladimir Oltean 		priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER;
3030718bad0eSVladimir Oltean 
30318aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
30328aa9ebccSVladimir Oltean 
30338aa9ebccSVladimir Oltean 	/* Detect hardware device */
30348aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
30358aa9ebccSVladimir Oltean 	if (rc < 0) {
30368aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
30378aa9ebccSVladimir Oltean 		return rc;
30388aa9ebccSVladimir Oltean 	}
30398aa9ebccSVladimir Oltean 
30408aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
30418aa9ebccSVladimir Oltean 
30427e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
30438aa9ebccSVladimir Oltean 	if (!ds)
30448aa9ebccSVladimir Oltean 		return -ENOMEM;
30458aa9ebccSVladimir Oltean 
30467e99e347SVivien Didelot 	ds->dev = dev;
30473e77e59bSVladimir Oltean 	ds->num_ports = priv->info->num_ports;
30488aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
30498aa9ebccSVladimir Oltean 	ds->priv = priv;
30508aa9ebccSVladimir Oltean 	priv->ds = ds;
30518aa9ebccSVladimir Oltean 
3052844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
3053844d7edcSVladimir Oltean 
3054d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
3055d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
3056d5a619bfSVivien Didelot 
3057d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
3058a6af7763SVladimir Oltean 	sja1105_flower_setup(ds);
3059d5a619bfSVivien Didelot 
3060d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
3061d5a619bfSVivien Didelot 	if (rc)
3062328621f6SVladimir Oltean 		return rc;
3063d5a619bfSVivien Didelot 
30644d752508SVladimir Oltean 	if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
30654d752508SVladimir Oltean 		priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
30664d752508SVladimir Oltean 					 sizeof(struct sja1105_cbs_entry),
30674d752508SVladimir Oltean 					 GFP_KERNEL);
3068dc596e3fSVladimir Oltean 		if (!priv->cbs) {
3069dc596e3fSVladimir Oltean 			rc = -ENOMEM;
3070dc596e3fSVladimir Oltean 			goto out_unregister_switch;
3071dc596e3fSVladimir Oltean 		}
30724d752508SVladimir Oltean 	}
30734d752508SVladimir Oltean 
3074227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
3075542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
3076a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3077a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
3078a68578c2SVladimir Oltean 		struct net_device *slave;
3079227d07a0SVladimir Oltean 
3080a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3081a68578c2SVladimir Oltean 			continue;
3082a68578c2SVladimir Oltean 
3083a68578c2SVladimir Oltean 		dp->priv = sp;
3084a68578c2SVladimir Oltean 		sp->dp = dp;
3085844d7edcSVladimir Oltean 		sp->data = tagger_data;
3086a68578c2SVladimir Oltean 		slave = dp->slave;
3087a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
3088a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
3089a68578c2SVladimir Oltean 							slave->name);
3090a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
3091a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
3092a68578c2SVladimir Oltean 			dev_err(ds->dev,
3093a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
3094a68578c2SVladimir Oltean 				rc);
3095dc596e3fSVladimir Oltean 			goto out_destroy_workers;
3096a68578c2SVladimir Oltean 		}
3097a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
309838b5beeaSVladimir Oltean 		sp->xmit_tpid = ETH_P_SJA1105;
3099227d07a0SVladimir Oltean 	}
3100227d07a0SVladimir Oltean 
3101d5a619bfSVivien Didelot 	return 0;
3102dc596e3fSVladimir Oltean 
3103dc596e3fSVladimir Oltean out_destroy_workers:
3104a68578c2SVladimir Oltean 	while (port-- > 0) {
3105a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3106a68578c2SVladimir Oltean 
3107a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3108a68578c2SVladimir Oltean 			continue;
3109a68578c2SVladimir Oltean 
3110a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
3111a68578c2SVladimir Oltean 	}
3112dc596e3fSVladimir Oltean 
3113dc596e3fSVladimir Oltean out_unregister_switch:
3114dc596e3fSVladimir Oltean 	dsa_unregister_switch(ds);
3115dc596e3fSVladimir Oltean 
3116a68578c2SVladimir Oltean 	return rc;
31178aa9ebccSVladimir Oltean }
31188aa9ebccSVladimir Oltean 
31198aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
31208aa9ebccSVladimir Oltean {
31218aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
3122cedf4670SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
31238aa9ebccSVladimir Oltean 
3124cedf4670SVladimir Oltean 	dsa_unregister_switch(ds);
3125cedf4670SVladimir Oltean 
31268aa9ebccSVladimir Oltean 	return 0;
31278aa9ebccSVladimir Oltean }
31288aa9ebccSVladimir Oltean 
31298aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
31308aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
31318aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
31328aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
31338aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
31348aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
31358aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
31363e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110a", .data = &sja1110a_info },
31373e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110b", .data = &sja1110b_info },
31383e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110c", .data = &sja1110c_info },
31393e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110d", .data = &sja1110d_info },
31408aa9ebccSVladimir Oltean 	{ /* sentinel */ },
31418aa9ebccSVladimir Oltean };
31428aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
31438aa9ebccSVladimir Oltean 
31448aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
31458aa9ebccSVladimir Oltean 	.driver = {
31468aa9ebccSVladimir Oltean 		.name  = "sja1105",
31478aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
31488aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
31498aa9ebccSVladimir Oltean 	},
31508aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
31518aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
31528aa9ebccSVladimir Oltean };
31538aa9ebccSVladimir Oltean 
31548aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
31558aa9ebccSVladimir Oltean 
31568aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
31578aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
31588aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
31598aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
3160