xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 91495f21fcec3a889d6a9c21e6df16c4c15f2184)
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
294d942354SVladimir Oltean 
3033e1501fSVladimir Oltean /* Configure the optional reset pin and bring up switch */
3133e1501fSVladimir Oltean static int sja1105_hw_reset(struct device *dev, unsigned int pulse_len,
328aa9ebccSVladimir Oltean 			    unsigned int startup_delay)
338aa9ebccSVladimir Oltean {
3433e1501fSVladimir Oltean 	struct gpio_desc *gpio;
3533e1501fSVladimir Oltean 
3633e1501fSVladimir Oltean 	gpio = gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
3733e1501fSVladimir Oltean 	if (IS_ERR(gpio))
3833e1501fSVladimir Oltean 		return PTR_ERR(gpio);
3933e1501fSVladimir Oltean 
4033e1501fSVladimir Oltean 	if (!gpio)
4133e1501fSVladimir Oltean 		return 0;
4233e1501fSVladimir Oltean 
438aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
448aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
458aa9ebccSVladimir Oltean 	msleep(pulse_len);
468aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
478aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
488aa9ebccSVladimir Oltean 	msleep(startup_delay);
4933e1501fSVladimir Oltean 
5033e1501fSVladimir Oltean 	gpiod_put(gpio);
5133e1501fSVladimir Oltean 
5233e1501fSVladimir Oltean 	return 0;
538aa9ebccSVladimir Oltean }
548aa9ebccSVladimir Oltean 
558aa9ebccSVladimir Oltean static void
568aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
578aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
588aa9ebccSVladimir Oltean {
594d942354SVladimir Oltean 	if (allow)
608aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
614d942354SVladimir Oltean 	else
628aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
638aa9ebccSVladimir Oltean }
648aa9ebccSVladimir Oltean 
657f7ccdeaSVladimir Oltean static bool sja1105_can_forward(struct sja1105_l2_forwarding_entry *l2_fwd,
667f7ccdeaSVladimir Oltean 				int from, int to)
677f7ccdeaSVladimir Oltean {
687f7ccdeaSVladimir Oltean 	return !!(l2_fwd[from].reach_port & BIT(to));
697f7ccdeaSVladimir Oltean }
707f7ccdeaSVladimir Oltean 
71bef0746cSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
72bef0746cSVladimir Oltean {
73bef0746cSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
74bef0746cSVladimir Oltean 	int count, i;
75bef0746cSVladimir Oltean 
76bef0746cSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
77bef0746cSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
78bef0746cSVladimir Oltean 
79bef0746cSVladimir Oltean 	for (i = 0; i < count; i++)
80bef0746cSVladimir Oltean 		if (vlan[i].vlanid == vid)
81bef0746cSVladimir Oltean 			return i;
82bef0746cSVladimir Oltean 
83bef0746cSVladimir Oltean 	/* Return an invalid entry index if not found */
84bef0746cSVladimir Oltean 	return -1;
85bef0746cSVladimir Oltean }
86bef0746cSVladimir Oltean 
87bef0746cSVladimir Oltean static int sja1105_drop_untagged(struct dsa_switch *ds, int port, bool drop)
88bef0746cSVladimir Oltean {
89bef0746cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
90bef0746cSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
91bef0746cSVladimir Oltean 
92bef0746cSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
93bef0746cSVladimir Oltean 
94bef0746cSVladimir Oltean 	if (mac[port].drpuntag == drop)
95bef0746cSVladimir Oltean 		return 0;
96bef0746cSVladimir Oltean 
97bef0746cSVladimir Oltean 	mac[port].drpuntag = drop;
98bef0746cSVladimir Oltean 
99bef0746cSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
100bef0746cSVladimir Oltean 					    &mac[port], true);
101bef0746cSVladimir Oltean }
102bef0746cSVladimir Oltean 
103cde8078eSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
104cde8078eSVladimir Oltean {
105cde8078eSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
106cde8078eSVladimir Oltean 
107cde8078eSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
108cde8078eSVladimir Oltean 
109cde8078eSVladimir Oltean 	if (mac[port].vlanid == pvid)
110cde8078eSVladimir Oltean 		return 0;
111cde8078eSVladimir Oltean 
112cde8078eSVladimir Oltean 	mac[port].vlanid = pvid;
113cde8078eSVladimir Oltean 
114cde8078eSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
115cde8078eSVladimir Oltean 					    &mac[port], true);
116cde8078eSVladimir Oltean }
117cde8078eSVladimir Oltean 
118cde8078eSVladimir Oltean static int sja1105_commit_pvid(struct dsa_switch *ds, int port)
119cde8078eSVladimir Oltean {
120cde8078eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
12141fb0cf1SVladimir Oltean 	struct net_device *br = dsa_port_bridge_dev_get(dp);
122cde8078eSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
123bef0746cSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
124bef0746cSVladimir Oltean 	bool drop_untagged = false;
125bef0746cSVladimir Oltean 	int match, rc;
126cde8078eSVladimir Oltean 	u16 pvid;
127cde8078eSVladimir Oltean 
12841fb0cf1SVladimir Oltean 	if (br && br_vlan_enabled(br))
129cde8078eSVladimir Oltean 		pvid = priv->bridge_pvid[port];
130cde8078eSVladimir Oltean 	else
131cde8078eSVladimir Oltean 		pvid = priv->tag_8021q_pvid[port];
132cde8078eSVladimir Oltean 
133bef0746cSVladimir Oltean 	rc = sja1105_pvid_apply(priv, port, pvid);
134bef0746cSVladimir Oltean 	if (rc)
135bef0746cSVladimir Oltean 		return rc;
136bef0746cSVladimir Oltean 
13773ceab83SVladimir Oltean 	/* Only force dropping of untagged packets when the port is under a
13873ceab83SVladimir Oltean 	 * VLAN-aware bridge. When the tag_8021q pvid is used, we are
13973ceab83SVladimir Oltean 	 * deliberately removing the RX VLAN from the port's VMEMB_PORT list,
14073ceab83SVladimir Oltean 	 * to prevent DSA tag spoofing from the link partner. Untagged packets
14173ceab83SVladimir Oltean 	 * are the only ones that should be received with tag_8021q, so
14273ceab83SVladimir Oltean 	 * definitely don't drop them.
14373ceab83SVladimir Oltean 	 */
14473ceab83SVladimir Oltean 	if (pvid == priv->bridge_pvid[port]) {
145bef0746cSVladimir Oltean 		vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
146bef0746cSVladimir Oltean 
147bef0746cSVladimir Oltean 		match = sja1105_is_vlan_configured(priv, pvid);
148bef0746cSVladimir Oltean 
149bef0746cSVladimir Oltean 		if (match < 0 || !(vlan[match].vmemb_port & BIT(port)))
150bef0746cSVladimir Oltean 			drop_untagged = true;
15173ceab83SVladimir Oltean 	}
152bef0746cSVladimir Oltean 
153b0b8c67eSVladimir Oltean 	if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
154b0b8c67eSVladimir Oltean 		drop_untagged = true;
155b0b8c67eSVladimir Oltean 
156bef0746cSVladimir Oltean 	return sja1105_drop_untagged(ds, port, drop_untagged);
157cde8078eSVladimir Oltean }
158cde8078eSVladimir Oltean 
1598aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
1608aa9ebccSVladimir Oltean {
1618aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
1628aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
1638aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
1648aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
1658aa9ebccSVladimir Oltean 		 */
1668aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
1678aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
1688aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
1698aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
1708aa9ebccSVladimir Oltean 		.ifg = 0,
1718aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
1721fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
1738aa9ebccSVladimir Oltean 		 */
17441fed17fSVladimir Oltean 		.speed = priv->info->port_speed[SJA1105_SPEED_AUTO],
1758aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
1768aa9ebccSVladimir Oltean 		.tp_delin = 0,
1778aa9ebccSVladimir Oltean 		.tp_delout = 0,
1788aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
1798aa9ebccSVladimir Oltean 		.maxage = 0xFF,
1808aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
1818aa9ebccSVladimir Oltean 		.vlanprio = 0,
182e3502b82SVladimir Oltean 		.vlanid = 1,
1838aa9ebccSVladimir Oltean 		.ing_mirr = false,
1848aa9ebccSVladimir Oltean 		.egr_mirr = false,
1858aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
1868aa9ebccSVladimir Oltean 		.drpnona664 = false,
1878aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
1888aa9ebccSVladimir Oltean 		.drpdtag = false,
1898aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
1908aa9ebccSVladimir Oltean 		.drpuntag = false,
1918aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
1928aa9ebccSVladimir Oltean 		.retag = false,
193640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
194640f763fSVladimir Oltean 		 * STP will enable it.
195640f763fSVladimir Oltean 		 */
196640f763fSVladimir Oltean 		.dyn_learn = false,
1978aa9ebccSVladimir Oltean 		.egress = false,
1988aa9ebccSVladimir Oltean 		.ingress = false,
1998aa9ebccSVladimir Oltean 	};
2008aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
201542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2028aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2035313a37bSVladimir Oltean 	struct dsa_port *dp;
2048aa9ebccSVladimir Oltean 
2058aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
2068aa9ebccSVladimir Oltean 
2078aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
2088aa9ebccSVladimir Oltean 	if (table->entry_count) {
2098aa9ebccSVladimir Oltean 		kfree(table->entries);
2108aa9ebccSVladimir Oltean 		table->entry_count = 0;
2118aa9ebccSVladimir Oltean 	}
2128aa9ebccSVladimir Oltean 
213fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
2148aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2158aa9ebccSVladimir Oltean 	if (!table->entries)
2168aa9ebccSVladimir Oltean 		return -ENOMEM;
2178aa9ebccSVladimir Oltean 
218fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
2198aa9ebccSVladimir Oltean 
2208aa9ebccSVladimir Oltean 	mac = table->entries;
2218aa9ebccSVladimir Oltean 
2225313a37bSVladimir Oltean 	list_for_each_entry(dp, &ds->dst->ports, list) {
2235313a37bSVladimir Oltean 		if (dp->ds != ds)
2245313a37bSVladimir Oltean 			continue;
2255313a37bSVladimir Oltean 
2265313a37bSVladimir Oltean 		mac[dp->index] = default_mac;
227b0b33b04SVladimir Oltean 
228b0b33b04SVladimir Oltean 		/* Let sja1105_bridge_stp_state_set() keep address learning
22981d45898SVladimir Oltean 		 * enabled for the DSA ports. CPU ports use software-assisted
23081d45898SVladimir Oltean 		 * learning to ensure that only FDB entries belonging to the
23181d45898SVladimir Oltean 		 * bridge are learned, and that they are learned towards all
23281d45898SVladimir Oltean 		 * CPU ports in a cross-chip topology if multiple CPU ports
23381d45898SVladimir Oltean 		 * exist.
234640f763fSVladimir Oltean 		 */
2355313a37bSVladimir Oltean 		if (dsa_port_is_dsa(dp))
2365313a37bSVladimir Oltean 			dp->learning = true;
237b0b8c67eSVladimir Oltean 
238b0b8c67eSVladimir Oltean 		/* Disallow untagged packets from being received on the
239b0b8c67eSVladimir Oltean 		 * CPU and DSA ports.
240b0b8c67eSVladimir Oltean 		 */
241b0b8c67eSVladimir Oltean 		if (dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp))
242b0b8c67eSVladimir Oltean 			mac[dp->index].drpuntag = true;
243640f763fSVladimir Oltean 	}
2448aa9ebccSVladimir Oltean 
2458aa9ebccSVladimir Oltean 	return 0;
2468aa9ebccSVladimir Oltean }
2478aa9ebccSVladimir Oltean 
2485d645df9SVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv)
2498aa9ebccSVladimir Oltean {
2508aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
2518aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
252542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2538aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2548aa9ebccSVladimir Oltean 	int i;
2558aa9ebccSVladimir Oltean 
2568aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
2578aa9ebccSVladimir Oltean 
2588aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
2598aa9ebccSVladimir Oltean 	if (table->entry_count) {
2608aa9ebccSVladimir Oltean 		kfree(table->entries);
2618aa9ebccSVladimir Oltean 		table->entry_count = 0;
2628aa9ebccSVladimir Oltean 	}
2638aa9ebccSVladimir Oltean 
264fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
2658aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2668aa9ebccSVladimir Oltean 	if (!table->entries)
2678aa9ebccSVladimir Oltean 		return -ENOMEM;
2688aa9ebccSVladimir Oltean 
2691fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
270fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
2718aa9ebccSVladimir Oltean 
2728aa9ebccSVladimir Oltean 	mii = table->entries;
2738aa9ebccSVladimir Oltean 
274542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
2755d645df9SVladimir Oltean 		sja1105_mii_role_t role = XMII_MAC;
2765d645df9SVladimir Oltean 
277ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
278ee9d0cb6SVladimir Oltean 			continue;
279ee9d0cb6SVladimir Oltean 
2805d645df9SVladimir Oltean 		switch (priv->phy_mode[i]) {
2815a8f0974SVladimir Oltean 		case PHY_INTERFACE_MODE_INTERNAL:
2825a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_NO_PHY)
2835a8f0974SVladimir Oltean 				goto unsupported;
2845a8f0974SVladimir Oltean 
2855a8f0974SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
2865a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_PHY_BASE_TX)
2875a8f0974SVladimir Oltean 				mii->special[i] = true;
2885a8f0974SVladimir Oltean 
2895a8f0974SVladimir Oltean 			break;
2905d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVMII:
2915d645df9SVladimir Oltean 			role = XMII_PHY;
2925d645df9SVladimir Oltean 			fallthrough;
2938aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
29491a05078SVladimir Oltean 			if (!priv->info->supports_mii[i])
29591a05078SVladimir Oltean 				goto unsupported;
29691a05078SVladimir Oltean 
2978aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
2988aa9ebccSVladimir Oltean 			break;
2995d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVRMII:
3005d645df9SVladimir Oltean 			role = XMII_PHY;
3015d645df9SVladimir Oltean 			fallthrough;
3028aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
30391a05078SVladimir Oltean 			if (!priv->info->supports_rmii[i])
30491a05078SVladimir Oltean 				goto unsupported;
30591a05078SVladimir Oltean 
3068aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
3078aa9ebccSVladimir Oltean 			break;
3088aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
3098aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
3108aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
3118aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
31291a05078SVladimir Oltean 			if (!priv->info->supports_rgmii[i])
31391a05078SVladimir Oltean 				goto unsupported;
31491a05078SVladimir Oltean 
3158aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
3168aa9ebccSVladimir Oltean 			break;
317ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
31891a05078SVladimir Oltean 			if (!priv->info->supports_sgmii[i])
31991a05078SVladimir Oltean 				goto unsupported;
32091a05078SVladimir Oltean 
321ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
322ece578bcSVladimir Oltean 			mii->special[i] = true;
323ffe10e67SVladimir Oltean 			break;
32491a05078SVladimir Oltean 		case PHY_INTERFACE_MODE_2500BASEX:
32591a05078SVladimir Oltean 			if (!priv->info->supports_2500basex[i])
32691a05078SVladimir Oltean 				goto unsupported;
32791a05078SVladimir Oltean 
32891a05078SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
329ece578bcSVladimir Oltean 			mii->special[i] = true;
33091a05078SVladimir Oltean 			break;
33191a05078SVladimir Oltean unsupported:
3328aa9ebccSVladimir Oltean 		default:
33391a05078SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d!\n",
3345d645df9SVladimir Oltean 				phy_modes(priv->phy_mode[i]), i);
3356729188dSVladimir Oltean 			return -EINVAL;
3368aa9ebccSVladimir Oltean 		}
3378aa9ebccSVladimir Oltean 
3385d645df9SVladimir Oltean 		mii->phy_mac[i] = role;
3398aa9ebccSVladimir Oltean 	}
3408aa9ebccSVladimir Oltean 	return 0;
3418aa9ebccSVladimir Oltean }
3428aa9ebccSVladimir Oltean 
3438aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
3448aa9ebccSVladimir Oltean {
3454d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
3468aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3474d942354SVladimir Oltean 	int port;
3488aa9ebccSVladimir Oltean 
3498aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
3508aa9ebccSVladimir Oltean 
3514d942354SVladimir Oltean 	/* We only populate the FDB table through dynamic L2 Address Lookup
3524d942354SVladimir Oltean 	 * entries, except for a special entry at the end which is a catch-all
3534d942354SVladimir Oltean 	 * for unknown multicast and will be used to control flooding domain.
354291d1e72SVladimir Oltean 	 */
3558aa9ebccSVladimir Oltean 	if (table->entry_count) {
3568aa9ebccSVladimir Oltean 		kfree(table->entries);
3578aa9ebccSVladimir Oltean 		table->entry_count = 0;
3588aa9ebccSVladimir Oltean 	}
3594d942354SVladimir Oltean 
3604d942354SVladimir Oltean 	if (!priv->info->can_limit_mcast_flood)
3614d942354SVladimir Oltean 		return 0;
3624d942354SVladimir Oltean 
3634d942354SVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
3644d942354SVladimir Oltean 				 GFP_KERNEL);
3654d942354SVladimir Oltean 	if (!table->entries)
3664d942354SVladimir Oltean 		return -ENOMEM;
3674d942354SVladimir Oltean 
3684d942354SVladimir Oltean 	table->entry_count = 1;
3694d942354SVladimir Oltean 	l2_lookup = table->entries;
3704d942354SVladimir Oltean 
3714d942354SVladimir Oltean 	/* All L2 multicast addresses have an odd first octet */
3724d942354SVladimir Oltean 	l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST;
3734d942354SVladimir Oltean 	l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST;
3744d942354SVladimir Oltean 	l2_lookup[0].lockeds = true;
3754d942354SVladimir Oltean 	l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1;
3764d942354SVladimir Oltean 
3774d942354SVladimir Oltean 	/* Flood multicast to every port by default */
3784d942354SVladimir Oltean 	for (port = 0; port < priv->ds->num_ports; port++)
3794d942354SVladimir Oltean 		if (!dsa_is_unused_port(priv->ds, port))
3804d942354SVladimir Oltean 			l2_lookup[0].destports |= BIT(port);
3814d942354SVladimir Oltean 
3828aa9ebccSVladimir Oltean 	return 0;
3838aa9ebccSVladimir Oltean }
3848aa9ebccSVladimir Oltean 
3858aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
3868aa9ebccSVladimir Oltean {
3878aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
3888456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
3898456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
3908aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
3918aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
3921da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
3931da73821SVladimir Oltean 		.start_dynspc = 0,
3948aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
3958aa9ebccSVladimir Oltean 		.poly = 0x97,
3968aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
3978aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
3988aa9ebccSVladimir Oltean 		 */
3996d7c7d94SVladimir Oltean 		.shared_learn = true,
4008aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
4018aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
4028aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
4038aa9ebccSVladimir Oltean 		 */
4048aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
4058aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
4068aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
4078aa9ebccSVladimir Oltean 		 */
4088aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
4091da73821SVladimir Oltean 		/* P/Q/R/S only */
4101da73821SVladimir Oltean 		.use_static = true,
4111da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
4121da73821SVladimir Oltean 		 * dynamic FDB entries
4131da73821SVladimir Oltean 		 */
4141da73821SVladimir Oltean 		.owr_dyn = true,
4151da73821SVladimir Oltean 		.drpnolearn = true,
4168aa9ebccSVladimir Oltean 	};
417542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
418f238fef1SVladimir Oltean 	int port, num_used_ports = 0;
419542043e9SVladimir Oltean 	struct sja1105_table *table;
420542043e9SVladimir Oltean 	u64 max_fdb_entries;
421542043e9SVladimir Oltean 
422542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++)
423f238fef1SVladimir Oltean 		if (!dsa_is_unused_port(ds, port))
424f238fef1SVladimir Oltean 			num_used_ports++;
425f238fef1SVladimir Oltean 
426f238fef1SVladimir Oltean 	max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports;
427f238fef1SVladimir Oltean 
428f238fef1SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
429f238fef1SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
430f238fef1SVladimir Oltean 			continue;
431f238fef1SVladimir Oltean 
432542043e9SVladimir Oltean 		default_l2_lookup_params.maxaddrp[port] = max_fdb_entries;
433f238fef1SVladimir Oltean 	}
4348aa9ebccSVladimir Oltean 
4358aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
4368aa9ebccSVladimir Oltean 
4378aa9ebccSVladimir Oltean 	if (table->entry_count) {
4388aa9ebccSVladimir Oltean 		kfree(table->entries);
4398aa9ebccSVladimir Oltean 		table->entry_count = 0;
4408aa9ebccSVladimir Oltean 	}
4418aa9ebccSVladimir Oltean 
442fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4438aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4448aa9ebccSVladimir Oltean 	if (!table->entries)
4458aa9ebccSVladimir Oltean 		return -ENOMEM;
4468aa9ebccSVladimir Oltean 
447fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
4488aa9ebccSVladimir Oltean 
4498aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4508aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
4518aa9ebccSVladimir Oltean 				default_l2_lookup_params;
4528aa9ebccSVladimir Oltean 
4538aa9ebccSVladimir Oltean 	return 0;
4548aa9ebccSVladimir Oltean }
4558aa9ebccSVladimir Oltean 
456ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU
457ed040abcSVladimir Oltean  * using management routes (e.g. STP, PTP) as opposed to tag_8021q.
458ed040abcSVladimir Oltean  * All DT-defined ports are members of this VLAN, and there are no
459ed040abcSVladimir Oltean  * restrictions on forwarding (since the CPU selects the destination).
460ed040abcSVladimir Oltean  * Frames from this VLAN will always be transmitted as untagged, and
461ed040abcSVladimir Oltean  * neither the bridge nor the 8021q module cannot create this VLAN ID.
462ed040abcSVladimir Oltean  */
4638aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
4648aa9ebccSVladimir Oltean {
4658aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4668aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
4673e77e59bSVladimir Oltean 		.type_entry = SJA1110_VLAN_D_TAG,
4688aa9ebccSVladimir Oltean 		.ving_mirr = 0,
4698aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
4708aa9ebccSVladimir Oltean 		.vmemb_port = 0,
4718aa9ebccSVladimir Oltean 		.vlan_bc = 0,
4728aa9ebccSVladimir Oltean 		.tag_port = 0,
473ed040abcSVladimir Oltean 		.vlanid = SJA1105_DEFAULT_VLAN,
4748aa9ebccSVladimir Oltean 	};
475ec5ae610SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
476ec5ae610SVladimir Oltean 	int port;
4778aa9ebccSVladimir Oltean 
4788aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
4798aa9ebccSVladimir Oltean 
4808aa9ebccSVladimir Oltean 	if (table->entry_count) {
4818aa9ebccSVladimir Oltean 		kfree(table->entries);
4828aa9ebccSVladimir Oltean 		table->entry_count = 0;
4838aa9ebccSVladimir Oltean 	}
4848aa9ebccSVladimir Oltean 
485c75857b0SZheng Yongjun 	table->entries = kzalloc(table->ops->unpacked_entry_size,
4868aa9ebccSVladimir Oltean 				 GFP_KERNEL);
4878aa9ebccSVladimir Oltean 	if (!table->entries)
4888aa9ebccSVladimir Oltean 		return -ENOMEM;
4898aa9ebccSVladimir Oltean 
4908aa9ebccSVladimir Oltean 	table->entry_count = 1;
4918aa9ebccSVladimir Oltean 
492ec5ae610SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
493ec5ae610SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
494ec5ae610SVladimir Oltean 			continue;
495ec5ae610SVladimir Oltean 
496ec5ae610SVladimir Oltean 		pvid.vmemb_port |= BIT(port);
497ec5ae610SVladimir Oltean 		pvid.vlan_bc |= BIT(port);
498ec5ae610SVladimir Oltean 		pvid.tag_port &= ~BIT(port);
499ec5ae610SVladimir Oltean 
500c5130029SVladimir Oltean 		if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
5016dfd23d3SVladimir Oltean 			priv->tag_8021q_pvid[port] = SJA1105_DEFAULT_VLAN;
5026dfd23d3SVladimir Oltean 			priv->bridge_pvid[port] = SJA1105_DEFAULT_VLAN;
5036dfd23d3SVladimir Oltean 		}
5048aa9ebccSVladimir Oltean 	}
5058aa9ebccSVladimir Oltean 
5068aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
5078aa9ebccSVladimir Oltean 	return 0;
5088aa9ebccSVladimir Oltean }
5098aa9ebccSVladimir Oltean 
5108aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
5118aa9ebccSVladimir Oltean {
5128aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
513542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
5140f9b762cSVladimir Oltean 	struct dsa_switch_tree *dst;
5158aa9ebccSVladimir Oltean 	struct sja1105_table *table;
5160f9b762cSVladimir Oltean 	struct dsa_link *dl;
5173fa21270SVladimir Oltean 	int port, tc;
5183fa21270SVladimir Oltean 	int from, to;
5198aa9ebccSVladimir Oltean 
5208aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
5218aa9ebccSVladimir Oltean 
5228aa9ebccSVladimir Oltean 	if (table->entry_count) {
5238aa9ebccSVladimir Oltean 		kfree(table->entries);
5248aa9ebccSVladimir Oltean 		table->entry_count = 0;
5258aa9ebccSVladimir Oltean 	}
5268aa9ebccSVladimir Oltean 
527fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
5288aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5298aa9ebccSVladimir Oltean 	if (!table->entries)
5308aa9ebccSVladimir Oltean 		return -ENOMEM;
5318aa9ebccSVladimir Oltean 
532fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
5338aa9ebccSVladimir Oltean 
5348aa9ebccSVladimir Oltean 	l2fwd = table->entries;
5358aa9ebccSVladimir Oltean 
5363fa21270SVladimir Oltean 	/* First 5 entries in the L2 Forwarding Table define the forwarding
5373fa21270SVladimir Oltean 	 * rules and the VLAN PCP to ingress queue mapping.
5383fa21270SVladimir Oltean 	 * Set up the ingress queue mapping first.
5397f7ccdeaSVladimir Oltean 	 */
5403fa21270SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
5413fa21270SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
5428aa9ebccSVladimir Oltean 			continue;
5438aa9ebccSVladimir Oltean 
5443fa21270SVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
5453fa21270SVladimir Oltean 			l2fwd[port].vlan_pmap[tc] = tc;
5463fa21270SVladimir Oltean 	}
5474d942354SVladimir Oltean 
5483fa21270SVladimir Oltean 	/* Then manage the forwarding domain for user ports. These can forward
5493fa21270SVladimir Oltean 	 * only to the always-on domain (CPU port and DSA links)
5503fa21270SVladimir Oltean 	 */
5513fa21270SVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
5523fa21270SVladimir Oltean 		if (!dsa_is_user_port(ds, from))
5533fa21270SVladimir Oltean 			continue;
5544d942354SVladimir Oltean 
5553fa21270SVladimir Oltean 		for (to = 0; to < ds->num_ports; to++) {
5563fa21270SVladimir Oltean 			if (!dsa_is_cpu_port(ds, to) &&
5573fa21270SVladimir Oltean 			    !dsa_is_dsa_port(ds, to))
5583fa21270SVladimir Oltean 				continue;
5593fa21270SVladimir Oltean 
5603fa21270SVladimir Oltean 			l2fwd[from].bc_domain |= BIT(to);
5613fa21270SVladimir Oltean 			l2fwd[from].fl_domain |= BIT(to);
5623fa21270SVladimir Oltean 
5633fa21270SVladimir Oltean 			sja1105_port_allow_traffic(l2fwd, from, to, true);
5643fa21270SVladimir Oltean 		}
5653fa21270SVladimir Oltean 	}
5663fa21270SVladimir Oltean 
5673fa21270SVladimir Oltean 	/* Then manage the forwarding domain for DSA links and CPU ports (the
5683fa21270SVladimir Oltean 	 * always-on domain). These can send packets to any enabled port except
5693fa21270SVladimir Oltean 	 * themselves.
5703fa21270SVladimir Oltean 	 */
5713fa21270SVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
5723fa21270SVladimir Oltean 		if (!dsa_is_cpu_port(ds, from) && !dsa_is_dsa_port(ds, from))
5733fa21270SVladimir Oltean 			continue;
5743fa21270SVladimir Oltean 
5753fa21270SVladimir Oltean 		for (to = 0; to < ds->num_ports; to++) {
5763fa21270SVladimir Oltean 			if (dsa_is_unused_port(ds, to))
5773fa21270SVladimir Oltean 				continue;
5783fa21270SVladimir Oltean 
5793fa21270SVladimir Oltean 			if (from == to)
5803fa21270SVladimir Oltean 				continue;
5813fa21270SVladimir Oltean 
5823fa21270SVladimir Oltean 			l2fwd[from].bc_domain |= BIT(to);
5833fa21270SVladimir Oltean 			l2fwd[from].fl_domain |= BIT(to);
5843fa21270SVladimir Oltean 
5853fa21270SVladimir Oltean 			sja1105_port_allow_traffic(l2fwd, from, to, true);
5863fa21270SVladimir Oltean 		}
5873fa21270SVladimir Oltean 	}
5883fa21270SVladimir Oltean 
5890f9b762cSVladimir Oltean 	/* In odd topologies ("H" connections where there is a DSA link to
5900f9b762cSVladimir Oltean 	 * another switch which also has its own CPU port), TX packets can loop
5910f9b762cSVladimir Oltean 	 * back into the system (they are flooded from CPU port 1 to the DSA
5920f9b762cSVladimir Oltean 	 * link, and from there to CPU port 2). Prevent this from happening by
5930f9b762cSVladimir Oltean 	 * cutting RX from DSA links towards our CPU port, if the remote switch
5940f9b762cSVladimir Oltean 	 * has its own CPU port and therefore doesn't need ours for network
5950f9b762cSVladimir Oltean 	 * stack termination.
5960f9b762cSVladimir Oltean 	 */
5970f9b762cSVladimir Oltean 	dst = ds->dst;
5980f9b762cSVladimir Oltean 
5990f9b762cSVladimir Oltean 	list_for_each_entry(dl, &dst->rtable, list) {
6000f9b762cSVladimir Oltean 		if (dl->dp->ds != ds || dl->link_dp->cpu_dp == dl->dp->cpu_dp)
6010f9b762cSVladimir Oltean 			continue;
6020f9b762cSVladimir Oltean 
6030f9b762cSVladimir Oltean 		from = dl->dp->index;
6040f9b762cSVladimir Oltean 		to = dsa_upstream_port(ds, from);
6050f9b762cSVladimir Oltean 
6060f9b762cSVladimir Oltean 		dev_warn(ds->dev,
6070f9b762cSVladimir Oltean 			 "H topology detected, cutting RX from DSA link %d to CPU port %d to prevent TX packet loops\n",
6080f9b762cSVladimir Oltean 			 from, to);
6090f9b762cSVladimir Oltean 
6100f9b762cSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, from, to, false);
6110f9b762cSVladimir Oltean 
6120f9b762cSVladimir Oltean 		l2fwd[from].bc_domain &= ~BIT(to);
6130f9b762cSVladimir Oltean 		l2fwd[from].fl_domain &= ~BIT(to);
6140f9b762cSVladimir Oltean 	}
6150f9b762cSVladimir Oltean 
6163fa21270SVladimir Oltean 	/* Finally, manage the egress flooding domain. All ports start up with
6173fa21270SVladimir Oltean 	 * flooding enabled, including the CPU port and DSA links.
6183fa21270SVladimir Oltean 	 */
6193fa21270SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
6203fa21270SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
6213fa21270SVladimir Oltean 			continue;
6223fa21270SVladimir Oltean 
6233fa21270SVladimir Oltean 		priv->ucast_egress_floods |= BIT(port);
6243fa21270SVladimir Oltean 		priv->bcast_egress_floods |= BIT(port);
6258aa9ebccSVladimir Oltean 	}
626f238fef1SVladimir Oltean 
6278aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
6288aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
6298aa9ebccSVladimir Oltean 	 */
6303fa21270SVladimir Oltean 	for (tc = 0; tc < SJA1105_NUM_TC; tc++) {
6313fa21270SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
6323fa21270SVladimir Oltean 			if (dsa_is_unused_port(ds, port))
633f238fef1SVladimir Oltean 				continue;
634f238fef1SVladimir Oltean 
6353fa21270SVladimir Oltean 			l2fwd[ds->num_ports + tc].vlan_pmap[port] = tc;
636f238fef1SVladimir Oltean 		}
6373e77e59bSVladimir Oltean 
6383fa21270SVladimir Oltean 		l2fwd[ds->num_ports + tc].type_egrpcp2outputq = true;
6393e77e59bSVladimir Oltean 	}
6403e77e59bSVladimir Oltean 
6413e77e59bSVladimir Oltean 	return 0;
6423e77e59bSVladimir Oltean }
6433e77e59bSVladimir Oltean 
6443e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv)
6453e77e59bSVladimir Oltean {
6463e77e59bSVladimir Oltean 	struct sja1110_pcp_remapping_entry *pcp_remap;
6473e77e59bSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
6483e77e59bSVladimir Oltean 	struct sja1105_table *table;
6493e77e59bSVladimir Oltean 	int port, tc;
6503e77e59bSVladimir Oltean 
6513e77e59bSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING];
6523e77e59bSVladimir Oltean 
6533e77e59bSVladimir Oltean 	/* Nothing to do for SJA1105 */
6543e77e59bSVladimir Oltean 	if (!table->ops->max_entry_count)
6553e77e59bSVladimir Oltean 		return 0;
6563e77e59bSVladimir Oltean 
6573e77e59bSVladimir Oltean 	if (table->entry_count) {
6583e77e59bSVladimir Oltean 		kfree(table->entries);
6593e77e59bSVladimir Oltean 		table->entry_count = 0;
6603e77e59bSVladimir Oltean 	}
6613e77e59bSVladimir Oltean 
6623e77e59bSVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
6633e77e59bSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
6643e77e59bSVladimir Oltean 	if (!table->entries)
6653e77e59bSVladimir Oltean 		return -ENOMEM;
6663e77e59bSVladimir Oltean 
6673e77e59bSVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
6683e77e59bSVladimir Oltean 
6693e77e59bSVladimir Oltean 	pcp_remap = table->entries;
6703e77e59bSVladimir Oltean 
6713e77e59bSVladimir Oltean 	/* Repeat the configuration done for vlan_pmap */
6723e77e59bSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
6733e77e59bSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
6743e77e59bSVladimir Oltean 			continue;
6753e77e59bSVladimir Oltean 
6763e77e59bSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
6773e77e59bSVladimir Oltean 			pcp_remap[port].egrpcp[tc] = tc;
678f238fef1SVladimir Oltean 	}
6798aa9ebccSVladimir Oltean 
6808aa9ebccSVladimir Oltean 	return 0;
6818aa9ebccSVladimir Oltean }
6828aa9ebccSVladimir Oltean 
6838aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
6848aa9ebccSVladimir Oltean {
6851bf658eeSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2fwd_params;
6868aa9ebccSVladimir Oltean 	struct sja1105_table *table;
6878aa9ebccSVladimir Oltean 
6888aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
6898aa9ebccSVladimir Oltean 
6908aa9ebccSVladimir Oltean 	if (table->entry_count) {
6918aa9ebccSVladimir Oltean 		kfree(table->entries);
6928aa9ebccSVladimir Oltean 		table->entry_count = 0;
6938aa9ebccSVladimir Oltean 	}
6948aa9ebccSVladimir Oltean 
695fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
6968aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
6978aa9ebccSVladimir Oltean 	if (!table->entries)
6988aa9ebccSVladimir Oltean 		return -ENOMEM;
6998aa9ebccSVladimir Oltean 
700fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
7018aa9ebccSVladimir Oltean 
7028aa9ebccSVladimir Oltean 	/* This table only has a single entry */
7031bf658eeSVladimir Oltean 	l2fwd_params = table->entries;
7041bf658eeSVladimir Oltean 
7051bf658eeSVladimir Oltean 	/* Disallow dynamic reconfiguration of vlan_pmap */
7061bf658eeSVladimir Oltean 	l2fwd_params->max_dynp = 0;
7071bf658eeSVladimir Oltean 	/* Use a single memory partition for all ingress queues */
7081bf658eeSVladimir Oltean 	l2fwd_params->part_spc[0] = priv->info->max_frame_mem;
7098aa9ebccSVladimir Oltean 
7108aa9ebccSVladimir Oltean 	return 0;
7118aa9ebccSVladimir Oltean }
7128aa9ebccSVladimir Oltean 
713aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
714aaa270c6SVladimir Oltean {
715aaa270c6SVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
716aaa270c6SVladimir Oltean 	struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
717aaa270c6SVladimir Oltean 	struct sja1105_table *table;
718aaa270c6SVladimir Oltean 
719aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
720aaa270c6SVladimir Oltean 	l2_fwd_params = table->entries;
7210fac6aa0SVladimir Oltean 	l2_fwd_params->part_spc[0] = SJA1105_MAX_FRAME_MEMORY;
722aaa270c6SVladimir Oltean 
723aaa270c6SVladimir Oltean 	/* If we have any critical-traffic virtual links, we need to reserve
724aaa270c6SVladimir Oltean 	 * some frame buffer memory for them. At the moment, hardcode the value
725aaa270c6SVladimir Oltean 	 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
726aaa270c6SVladimir Oltean 	 * remaining for best-effort traffic. TODO: figure out a more flexible
727aaa270c6SVladimir Oltean 	 * way to perform the frame buffer partitioning.
728aaa270c6SVladimir Oltean 	 */
729aaa270c6SVladimir Oltean 	if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
730aaa270c6SVladimir Oltean 		return;
731aaa270c6SVladimir Oltean 
732aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
733aaa270c6SVladimir Oltean 	vl_fwd_params = table->entries;
734aaa270c6SVladimir Oltean 
735aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
736aaa270c6SVladimir Oltean 	vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
737aaa270c6SVladimir Oltean }
738aaa270c6SVladimir Oltean 
739ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values:
740ceec8bc0SVladimir Oltean  *
741ceec8bc0SVladimir Oltean  *      | 100 Mbps ports |  1Gbps ports  | 2.5Gbps ports | Disabled ports
742ceec8bc0SVladimir Oltean  * -----+----------------+---------------+---------------+---------------
743ceec8bc0SVladimir Oltean  *   0  |   0, [5:10]    |     [1:2]     |     [3:4]     |     retag
744ceec8bc0SVladimir Oltean  *   1  |0, [5:10], retag|     [1:2]     |     [3:4]     |       -
745ceec8bc0SVladimir Oltean  *   2  |   0, [5:10]    |  [1:3], retag |       4       |       -
746ceec8bc0SVladimir Oltean  *   3  |   0, [5:10]    |[1:2], 4, retag|       3       |       -
747ceec8bc0SVladimir Oltean  *   4  |  0, 2, [5:10]  |    1, retag   |     [3:4]     |       -
748ceec8bc0SVladimir Oltean  *   5  |  0, 1, [5:10]  |    2, retag   |     [3:4]     |       -
749ceec8bc0SVladimir Oltean  *  14  |   0, [5:10]    | [1:4], retag  |       -       |       -
750ceec8bc0SVladimir Oltean  *  15  |     [5:10]     | [0:4], retag  |       -       |       -
751ceec8bc0SVladimir Oltean  */
752ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv)
753ceec8bc0SVladimir Oltean {
754ceec8bc0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
755ceec8bc0SVladimir Oltean 	struct sja1105_table *table;
756ceec8bc0SVladimir Oltean 	bool port_1_is_base_tx;
757ceec8bc0SVladimir Oltean 	bool port_3_is_2500;
758ceec8bc0SVladimir Oltean 	bool port_4_is_2500;
759ceec8bc0SVladimir Oltean 	u64 tdmaconfigidx;
760ceec8bc0SVladimir Oltean 
761ceec8bc0SVladimir Oltean 	if (priv->info->device_id != SJA1110_DEVICE_ID)
762ceec8bc0SVladimir Oltean 		return;
763ceec8bc0SVladimir Oltean 
764ceec8bc0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
765ceec8bc0SVladimir Oltean 	general_params = table->entries;
766ceec8bc0SVladimir Oltean 
767ceec8bc0SVladimir Oltean 	/* All the settings below are "as opposed to SGMII", which is the
768ceec8bc0SVladimir Oltean 	 * other pinmuxing option.
769ceec8bc0SVladimir Oltean 	 */
770ceec8bc0SVladimir Oltean 	port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL;
771ceec8bc0SVladimir Oltean 	port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX;
772ceec8bc0SVladimir Oltean 	port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX;
773ceec8bc0SVladimir Oltean 
774ceec8bc0SVladimir Oltean 	if (port_1_is_base_tx)
775ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
776ceec8bc0SVladimir Oltean 		tdmaconfigidx = 5;
777ceec8bc0SVladimir Oltean 	else if (port_3_is_2500 && port_4_is_2500)
778ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 100 Mbps */
779ceec8bc0SVladimir Oltean 		tdmaconfigidx = 1;
780ceec8bc0SVladimir Oltean 	else if (port_3_is_2500)
781ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
782ceec8bc0SVladimir Oltean 		tdmaconfigidx = 3;
783ceec8bc0SVladimir Oltean 	else if (port_4_is_2500)
784ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
785ceec8bc0SVladimir Oltean 		tdmaconfigidx = 2;
786ceec8bc0SVladimir Oltean 	else
787ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
788ceec8bc0SVladimir Oltean 		tdmaconfigidx = 14;
789ceec8bc0SVladimir Oltean 
790ceec8bc0SVladimir Oltean 	general_params->tdmaconfigidx = tdmaconfigidx;
791ceec8bc0SVladimir Oltean }
792ceec8bc0SVladimir Oltean 
79330a100e6SVladimir Oltean static int sja1105_init_topology(struct sja1105_private *priv,
79430a100e6SVladimir Oltean 				 struct sja1105_general_params_entry *general_params)
79530a100e6SVladimir Oltean {
79630a100e6SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
79730a100e6SVladimir Oltean 	int port;
79830a100e6SVladimir Oltean 
79930a100e6SVladimir Oltean 	/* The host port is the destination for traffic matching mac_fltres1
80030a100e6SVladimir Oltean 	 * and mac_fltres0 on all ports except itself. Default to an invalid
80130a100e6SVladimir Oltean 	 * value.
80230a100e6SVladimir Oltean 	 */
80330a100e6SVladimir Oltean 	general_params->host_port = ds->num_ports;
80430a100e6SVladimir Oltean 
80530a100e6SVladimir Oltean 	/* Link-local traffic received on casc_port will be forwarded
80630a100e6SVladimir Oltean 	 * to host_port without embedding the source port and device ID
80730a100e6SVladimir Oltean 	 * info in the destination MAC address, and no RX timestamps will be
80830a100e6SVladimir Oltean 	 * taken either (presumably because it is a cascaded port and a
80930a100e6SVladimir Oltean 	 * downstream SJA switch already did that).
81030a100e6SVladimir Oltean 	 * To disable the feature, we need to do different things depending on
81130a100e6SVladimir Oltean 	 * switch generation. On SJA1105 we need to set an invalid port, while
81230a100e6SVladimir Oltean 	 * on SJA1110 which support multiple cascaded ports, this field is a
81330a100e6SVladimir Oltean 	 * bitmask so it must be left zero.
81430a100e6SVladimir Oltean 	 */
81530a100e6SVladimir Oltean 	if (!priv->info->multiple_cascade_ports)
81630a100e6SVladimir Oltean 		general_params->casc_port = ds->num_ports;
81730a100e6SVladimir Oltean 
81830a100e6SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
81930a100e6SVladimir Oltean 		bool is_upstream = dsa_is_upstream_port(ds, port);
82030a100e6SVladimir Oltean 		bool is_dsa_link = dsa_is_dsa_port(ds, port);
82130a100e6SVladimir Oltean 
82230a100e6SVladimir Oltean 		/* Upstream ports can be dedicated CPU ports or
82330a100e6SVladimir Oltean 		 * upstream-facing DSA links
82430a100e6SVladimir Oltean 		 */
82530a100e6SVladimir Oltean 		if (is_upstream) {
82630a100e6SVladimir Oltean 			if (general_params->host_port == ds->num_ports) {
82730a100e6SVladimir Oltean 				general_params->host_port = port;
82830a100e6SVladimir Oltean 			} else {
82930a100e6SVladimir Oltean 				dev_err(ds->dev,
83030a100e6SVladimir Oltean 					"Port %llu is already a host port, configuring %d as one too is not supported\n",
83130a100e6SVladimir Oltean 					general_params->host_port, port);
83230a100e6SVladimir Oltean 				return -EINVAL;
83330a100e6SVladimir Oltean 			}
83430a100e6SVladimir Oltean 		}
83530a100e6SVladimir Oltean 
83630a100e6SVladimir Oltean 		/* Cascade ports are downstream-facing DSA links */
83730a100e6SVladimir Oltean 		if (is_dsa_link && !is_upstream) {
83830a100e6SVladimir Oltean 			if (priv->info->multiple_cascade_ports) {
83930a100e6SVladimir Oltean 				general_params->casc_port |= BIT(port);
84030a100e6SVladimir Oltean 			} else if (general_params->casc_port == ds->num_ports) {
84130a100e6SVladimir Oltean 				general_params->casc_port = port;
84230a100e6SVladimir Oltean 			} else {
84330a100e6SVladimir Oltean 				dev_err(ds->dev,
84430a100e6SVladimir Oltean 					"Port %llu is already a cascade port, configuring %d as one too is not supported\n",
84530a100e6SVladimir Oltean 					general_params->casc_port, port);
84630a100e6SVladimir Oltean 				return -EINVAL;
84730a100e6SVladimir Oltean 			}
84830a100e6SVladimir Oltean 		}
84930a100e6SVladimir Oltean 	}
85030a100e6SVladimir Oltean 
85130a100e6SVladimir Oltean 	if (general_params->host_port == ds->num_ports) {
85230a100e6SVladimir Oltean 		dev_err(ds->dev, "No host port configured\n");
85330a100e6SVladimir Oltean 		return -EINVAL;
85430a100e6SVladimir Oltean 	}
85530a100e6SVladimir Oltean 
85630a100e6SVladimir Oltean 	return 0;
85730a100e6SVladimir Oltean }
85830a100e6SVladimir Oltean 
8598aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
8608aa9ebccSVladimir Oltean {
8618aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
862511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
863511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
8648aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
8655f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
8665f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
8675f06c63bSVladimir Oltean 		 */
86808fde09aSVladimir Oltean 		.hostprio = 7,
8698aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
8708aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
87142824463SVladimir Oltean 		.incl_srcpt1 = false,
8728aa9ebccSVladimir Oltean 		.send_meta1  = false,
8738aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
8748aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
87542824463SVladimir Oltean 		.incl_srcpt0 = false,
8768aa9ebccSVladimir Oltean 		.send_meta0  = false,
877511e6ca0SVladimir Oltean 		/* Default to an invalid value */
878542043e9SVladimir Oltean 		.mirr_port = priv->ds->num_ports,
8798aa9ebccSVladimir Oltean 		/* No TTEthernet */
880dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
8818aa9ebccSVladimir Oltean 		.vlmarker = 0,
8828aa9ebccSVladimir Oltean 		.vlmask = 0,
8838aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
8848aa9ebccSVladimir Oltean 		.ignore2stf = 0,
8856666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
8866666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
8876666cebcSVladimir Oltean 		 */
8886666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
8896666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
89029305260SVladimir Oltean 		/* Enable the TTEthernet engine on SJA1110 */
89129305260SVladimir Oltean 		.tte_en = true,
8924913b8ebSVladimir Oltean 		/* Set up the EtherType for control packets on SJA1110 */
8934913b8ebSVladimir Oltean 		.header_type = ETH_P_SJA1110,
8948aa9ebccSVladimir Oltean 	};
8956c0de59bSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
8968aa9ebccSVladimir Oltean 	struct sja1105_table *table;
89730a100e6SVladimir Oltean 	int rc;
898df2a81a3SVladimir Oltean 
89930a100e6SVladimir Oltean 	rc = sja1105_init_topology(priv, &default_general_params);
90030a100e6SVladimir Oltean 	if (rc)
90130a100e6SVladimir Oltean 		return rc;
9028aa9ebccSVladimir Oltean 
9038aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
9048aa9ebccSVladimir Oltean 
9058aa9ebccSVladimir Oltean 	if (table->entry_count) {
9068aa9ebccSVladimir Oltean 		kfree(table->entries);
9078aa9ebccSVladimir Oltean 		table->entry_count = 0;
9088aa9ebccSVladimir Oltean 	}
9098aa9ebccSVladimir Oltean 
910fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
9118aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
9128aa9ebccSVladimir Oltean 	if (!table->entries)
9138aa9ebccSVladimir Oltean 		return -ENOMEM;
9148aa9ebccSVladimir Oltean 
915fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
9168aa9ebccSVladimir Oltean 
9176c0de59bSVladimir Oltean 	general_params = table->entries;
9186c0de59bSVladimir Oltean 
9198aa9ebccSVladimir Oltean 	/* This table only has a single entry */
9206c0de59bSVladimir Oltean 	general_params[0] = default_general_params;
9218aa9ebccSVladimir Oltean 
922ceec8bc0SVladimir Oltean 	sja1110_select_tdmaconfigidx(priv);
923ceec8bc0SVladimir Oltean 
9248aa9ebccSVladimir Oltean 	return 0;
9258aa9ebccSVladimir Oltean }
9268aa9ebccSVladimir Oltean 
92779d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
92879d5511cSVladimir Oltean {
92979d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
93079d5511cSVladimir Oltean 	struct sja1105_table *table;
93179d5511cSVladimir Oltean 
93279d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
93379d5511cSVladimir Oltean 
93479d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
93579d5511cSVladimir Oltean 	if (table->entry_count) {
93679d5511cSVladimir Oltean 		kfree(table->entries);
93779d5511cSVladimir Oltean 		table->entry_count = 0;
93879d5511cSVladimir Oltean 	}
93979d5511cSVladimir Oltean 
940fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
94179d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
94279d5511cSVladimir Oltean 	if (!table->entries)
94379d5511cSVladimir Oltean 		return -ENOMEM;
94479d5511cSVladimir Oltean 
945fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
94679d5511cSVladimir Oltean 
94779d5511cSVladimir Oltean 	avb = table->entries;
94879d5511cSVladimir Oltean 
94979d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
95079d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
95179d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
952747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
953747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
954747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
955747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
956747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
957747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
958747e5eb3SVladimir Oltean 	 */
959747e5eb3SVladimir Oltean 	avb->cas_master = false;
96079d5511cSVladimir Oltean 
96179d5511cSVladimir Oltean 	return 0;
96279d5511cSVladimir Oltean }
96379d5511cSVladimir Oltean 
964a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
965a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
966a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
967a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
968a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
969a7cc081cSVladimir Oltean  * will be used for this frame.
970a7cc081cSVladimir Oltean  *
971a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
972a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
973a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
974a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
975a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
976a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
977a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
978a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
979a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
980a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
981a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
982a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
983a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
984a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
985a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
986a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
987a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
988a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
989a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
990a7cc081cSVladimir Oltean  * +------------+--------+
991a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
992a7cc081cSVladimir Oltean  * +------------+--------+
993a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
994a7cc081cSVladimir Oltean  * +------------+--------+
995a7cc081cSVladimir Oltean  *    ...                                  ...
996a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
997a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
998a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
999a7cc081cSVladimir Oltean  *
1000a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
1001a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
1002a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
1003a7cc081cSVladimir Oltean  * lookup) equal.
1004a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
1005a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
1006a7cc081cSVladimir Oltean  */
10078aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
10088aa9ebccSVladimir Oltean 
10098aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
10108aa9ebccSVladimir Oltean {
10118aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
1012542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
10138aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1014a7cc081cSVladimir Oltean 	int port, tc;
10158aa9ebccSVladimir Oltean 
10168aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
10178aa9ebccSVladimir Oltean 
10188aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
10198aa9ebccSVladimir Oltean 	if (table->entry_count) {
10208aa9ebccSVladimir Oltean 		kfree(table->entries);
10218aa9ebccSVladimir Oltean 		table->entry_count = 0;
10228aa9ebccSVladimir Oltean 	}
10238aa9ebccSVladimir Oltean 
1024fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
10258aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
10268aa9ebccSVladimir Oltean 	if (!table->entries)
10278aa9ebccSVladimir Oltean 		return -ENOMEM;
10288aa9ebccSVladimir Oltean 
1029fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
10308aa9ebccSVladimir Oltean 
10318aa9ebccSVladimir Oltean 	policing = table->entries;
10328aa9ebccSVladimir Oltean 
1033a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
1034542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
103538fbe91fSVladimir Oltean 		int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port;
1036542043e9SVladimir Oltean 		int bcast = (ds->num_ports * SJA1105_NUM_TC) + port;
1037a7cc081cSVladimir Oltean 
1038a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
1039a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
1040a7cc081cSVladimir Oltean 
1041a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
104238fbe91fSVladimir Oltean 		/* Only SJA1110 has multicast policers */
104338fbe91fSVladimir Oltean 		if (mcast <= table->ops->max_entry_count)
104438fbe91fSVladimir Oltean 			policing[mcast].sharindx = port;
1045a7cc081cSVladimir Oltean 	}
1046a7cc081cSVladimir Oltean 
1047a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
1048542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1049c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
1050c279c726SVladimir Oltean 
1051777e55e3SVladimir Oltean 		if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
1052c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
10538aa9ebccSVladimir Oltean 
1054a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
1055a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
1056a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
1057a7cc081cSVladimir Oltean 		policing[port].partition = 0;
10588aa9ebccSVladimir Oltean 	}
1059a7cc081cSVladimir Oltean 
10608aa9ebccSVladimir Oltean 	return 0;
10618aa9ebccSVladimir Oltean }
10628aa9ebccSVladimir Oltean 
10635d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv)
10648aa9ebccSVladimir Oltean {
10658aa9ebccSVladimir Oltean 	int rc;
10668aa9ebccSVladimir Oltean 
10678aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
10688aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
10698aa9ebccSVladimir Oltean 					priv->info->static_ops,
10708aa9ebccSVladimir Oltean 					priv->info->device_id);
10718aa9ebccSVladimir Oltean 	if (rc)
10728aa9ebccSVladimir Oltean 		return rc;
10738aa9ebccSVladimir Oltean 
10748aa9ebccSVladimir Oltean 	/* Build static configuration */
10758aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
10768aa9ebccSVladimir Oltean 	if (rc < 0)
10778aa9ebccSVladimir Oltean 		return rc;
10785d645df9SVladimir Oltean 	rc = sja1105_init_mii_settings(priv);
10798aa9ebccSVladimir Oltean 	if (rc < 0)
10808aa9ebccSVladimir Oltean 		return rc;
10818aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
10828aa9ebccSVladimir Oltean 	if (rc < 0)
10838aa9ebccSVladimir Oltean 		return rc;
10848aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
10858aa9ebccSVladimir Oltean 	if (rc < 0)
10868aa9ebccSVladimir Oltean 		return rc;
10878aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
10888aa9ebccSVladimir Oltean 	if (rc < 0)
10898aa9ebccSVladimir Oltean 		return rc;
10908aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
10918aa9ebccSVladimir Oltean 	if (rc < 0)
10928aa9ebccSVladimir Oltean 		return rc;
10938aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
10948aa9ebccSVladimir Oltean 	if (rc < 0)
10958aa9ebccSVladimir Oltean 		return rc;
10968aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
10978aa9ebccSVladimir Oltean 	if (rc < 0)
10988aa9ebccSVladimir Oltean 		return rc;
10998aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
11008aa9ebccSVladimir Oltean 	if (rc < 0)
11018aa9ebccSVladimir Oltean 		return rc;
110279d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
110379d5511cSVladimir Oltean 	if (rc < 0)
110479d5511cSVladimir Oltean 		return rc;
11053e77e59bSVladimir Oltean 	rc = sja1110_init_pcp_remapping(priv);
11063e77e59bSVladimir Oltean 	if (rc < 0)
11073e77e59bSVladimir Oltean 		return rc;
11088aa9ebccSVladimir Oltean 
11098aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
11108aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
11118aa9ebccSVladimir Oltean }
11128aa9ebccSVladimir Oltean 
11139ca482a2SVladimir Oltean /* This is the "new way" for a MAC driver to configure its RGMII delay lines,
11149ca482a2SVladimir Oltean  * based on the explicit "rx-internal-delay-ps" and "tx-internal-delay-ps"
11159ca482a2SVladimir Oltean  * properties. It has the advantage of working with fixed links and with PHYs
11169ca482a2SVladimir Oltean  * that apply RGMII delays too, and the MAC driver needs not perform any
11179ca482a2SVladimir Oltean  * special checks.
11189ca482a2SVladimir Oltean  *
11199ca482a2SVladimir Oltean  * Previously we were acting upon the "phy-mode" property when we were
11209ca482a2SVladimir Oltean  * operating in fixed-link, basically acting as a PHY, but with a reversed
11219ca482a2SVladimir Oltean  * interpretation: PHY_INTERFACE_MODE_RGMII_TXID means that the MAC should
11229ca482a2SVladimir Oltean  * behave as if it is connected to a PHY which has applied RGMII delays in the
11239ca482a2SVladimir Oltean  * TX direction. So if anything, RX delays should have been added by the MAC,
11249ca482a2SVladimir Oltean  * but we were adding TX delays.
11259ca482a2SVladimir Oltean  *
11269ca482a2SVladimir Oltean  * If the "{rx,tx}-internal-delay-ps" properties are not specified, we fall
11279ca482a2SVladimir Oltean  * back to the legacy behavior and apply delays on fixed-link ports based on
11289ca482a2SVladimir Oltean  * the reverse interpretation of the phy-mode. This is a deviation from the
11299ca482a2SVladimir Oltean  * expected default behavior which is to simply apply no delays. To achieve
11309ca482a2SVladimir Oltean  * that behavior with the new bindings, it is mandatory to specify
11319ca482a2SVladimir Oltean  * "{rx,tx}-internal-delay-ps" with a value of 0.
11329ca482a2SVladimir Oltean  */
11339ca482a2SVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv, int port,
11349ca482a2SVladimir Oltean 				      struct device_node *port_dn)
1135f5b8631cSVladimir Oltean {
11369ca482a2SVladimir Oltean 	phy_interface_t phy_mode = priv->phy_mode[port];
11379ca482a2SVladimir Oltean 	struct device *dev = &priv->spidev->dev;
11389ca482a2SVladimir Oltean 	int rx_delay = -1, tx_delay = -1;
1139f5b8631cSVladimir Oltean 
11409ca482a2SVladimir Oltean 	if (!phy_interface_mode_is_rgmii(phy_mode))
11419ca482a2SVladimir Oltean 		return 0;
1142f5b8631cSVladimir Oltean 
11439ca482a2SVladimir Oltean 	of_property_read_u32(port_dn, "rx-internal-delay-ps", &rx_delay);
11449ca482a2SVladimir Oltean 	of_property_read_u32(port_dn, "tx-internal-delay-ps", &tx_delay);
1145f5b8631cSVladimir Oltean 
11469ca482a2SVladimir Oltean 	if (rx_delay == -1 && tx_delay == -1 && priv->fixed_link[port]) {
11479ca482a2SVladimir Oltean 		dev_warn(dev,
11489ca482a2SVladimir Oltean 			 "Port %d interpreting RGMII delay settings based on \"phy-mode\" property, "
11499ca482a2SVladimir Oltean 			 "please update device tree to specify \"rx-internal-delay-ps\" and "
11509ca482a2SVladimir Oltean 			 "\"tx-internal-delay-ps\"",
11519ca482a2SVladimir Oltean 			 port);
1152f5b8631cSVladimir Oltean 
11539ca482a2SVladimir Oltean 		if (phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
11549ca482a2SVladimir Oltean 		    phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
11559ca482a2SVladimir Oltean 			rx_delay = 2000;
11569ca482a2SVladimir Oltean 
11579ca482a2SVladimir Oltean 		if (phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
11589ca482a2SVladimir Oltean 		    phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
11599ca482a2SVladimir Oltean 			tx_delay = 2000;
11609ca482a2SVladimir Oltean 	}
11619ca482a2SVladimir Oltean 
11629ca482a2SVladimir Oltean 	if (rx_delay < 0)
11639ca482a2SVladimir Oltean 		rx_delay = 0;
11649ca482a2SVladimir Oltean 	if (tx_delay < 0)
11659ca482a2SVladimir Oltean 		tx_delay = 0;
11669ca482a2SVladimir Oltean 
11679ca482a2SVladimir Oltean 	if ((rx_delay || tx_delay) && !priv->info->setup_rgmii_delay) {
11689ca482a2SVladimir Oltean 		dev_err(dev, "Chip cannot apply RGMII delays\n");
1169f5b8631cSVladimir Oltean 		return -EINVAL;
1170f5b8631cSVladimir Oltean 	}
11719ca482a2SVladimir Oltean 
11729ca482a2SVladimir Oltean 	if ((rx_delay && rx_delay < SJA1105_RGMII_DELAY_MIN_PS) ||
11739ca482a2SVladimir Oltean 	    (tx_delay && tx_delay < SJA1105_RGMII_DELAY_MIN_PS) ||
11749ca482a2SVladimir Oltean 	    (rx_delay > SJA1105_RGMII_DELAY_MAX_PS) ||
11759ca482a2SVladimir Oltean 	    (tx_delay > SJA1105_RGMII_DELAY_MAX_PS)) {
11769ca482a2SVladimir Oltean 		dev_err(dev,
11779ca482a2SVladimir Oltean 			"port %d RGMII delay values out of range, must be between %d and %d ps\n",
11789ca482a2SVladimir Oltean 			port, SJA1105_RGMII_DELAY_MIN_PS, SJA1105_RGMII_DELAY_MAX_PS);
11799ca482a2SVladimir Oltean 		return -ERANGE;
11809ca482a2SVladimir Oltean 	}
11819ca482a2SVladimir Oltean 
11829ca482a2SVladimir Oltean 	priv->rgmii_rx_delay_ps[port] = rx_delay;
11839ca482a2SVladimir Oltean 	priv->rgmii_tx_delay_ps[port] = tx_delay;
11849ca482a2SVladimir Oltean 
1185f5b8631cSVladimir Oltean 	return 0;
1186f5b8631cSVladimir Oltean }
1187f5b8631cSVladimir Oltean 
11888aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
11898aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
11908aa9ebccSVladimir Oltean {
11918aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
11928aa9ebccSVladimir Oltean 	struct device_node *child;
11938aa9ebccSVladimir Oltean 
119427afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
11958aa9ebccSVladimir Oltean 		struct device_node *phy_node;
11960c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
11978aa9ebccSVladimir Oltean 		u32 index;
11980c65b2b9SAndrew Lunn 		int err;
11998aa9ebccSVladimir Oltean 
12008aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
12018aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
12028aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
12038aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
12047ba771e3SNishka Dasgupta 			of_node_put(child);
12058aa9ebccSVladimir Oltean 			return -ENODEV;
12068aa9ebccSVladimir Oltean 		}
12078aa9ebccSVladimir Oltean 
12088aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
12090c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
12100c65b2b9SAndrew Lunn 		if (err) {
12118aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
12128aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
12138aa9ebccSVladimir Oltean 				index);
12147ba771e3SNishka Dasgupta 			of_node_put(child);
12158aa9ebccSVladimir Oltean 			return -ENODEV;
12168aa9ebccSVladimir Oltean 		}
12178aa9ebccSVladimir Oltean 
12188aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
12198aa9ebccSVladimir Oltean 		if (!phy_node) {
12208aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
12218aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
12228aa9ebccSVladimir Oltean 					"properties missing!\n");
12237ba771e3SNishka Dasgupta 				of_node_put(child);
12248aa9ebccSVladimir Oltean 				return -ENODEV;
12258aa9ebccSVladimir Oltean 			}
12268aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
12278aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
12288aa9ebccSVladimir Oltean 			 */
122929afb83aSVladimir Oltean 			priv->fixed_link[index] = true;
12308aa9ebccSVladimir Oltean 		} else {
12318aa9ebccSVladimir Oltean 			of_node_put(phy_node);
12328aa9ebccSVladimir Oltean 		}
12338aa9ebccSVladimir Oltean 
1234bf4edf4aSVladimir Oltean 		priv->phy_mode[index] = phy_mode;
12359ca482a2SVladimir Oltean 
12369ca482a2SVladimir Oltean 		err = sja1105_parse_rgmii_delays(priv, index, child);
1237f3956e30SWan Jiabing 		if (err) {
1238f3956e30SWan Jiabing 			of_node_put(child);
12399ca482a2SVladimir Oltean 			return err;
12408aa9ebccSVladimir Oltean 		}
1241f3956e30SWan Jiabing 	}
12428aa9ebccSVladimir Oltean 
12438aa9ebccSVladimir Oltean 	return 0;
12448aa9ebccSVladimir Oltean }
12458aa9ebccSVladimir Oltean 
12465d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv)
12478aa9ebccSVladimir Oltean {
12488aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
12498aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
12508aa9ebccSVladimir Oltean 	struct device_node *ports_node;
12518aa9ebccSVladimir Oltean 	int rc;
12528aa9ebccSVladimir Oltean 
12538aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
125415074a36SVladimir Oltean 	if (!ports_node)
125515074a36SVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
12568aa9ebccSVladimir Oltean 	if (!ports_node) {
12578aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
12588aa9ebccSVladimir Oltean 		return -ENODEV;
12598aa9ebccSVladimir Oltean 	}
12608aa9ebccSVladimir Oltean 
12615d645df9SVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports_node);
12628aa9ebccSVladimir Oltean 	of_node_put(ports_node);
12638aa9ebccSVladimir Oltean 
12648aa9ebccSVladimir Oltean 	return rc;
12658aa9ebccSVladimir Oltean }
12668aa9ebccSVladimir Oltean 
1267c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
126841fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv,
126941fed17fSVladimir Oltean 					 u64 speed)
127041fed17fSVladimir Oltean {
127141fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS])
127241fed17fSVladimir Oltean 		return SPEED_10;
127341fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS])
127441fed17fSVladimir Oltean 		return SPEED_100;
127541fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS])
127641fed17fSVladimir Oltean 		return SPEED_1000;
127741fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS])
127841fed17fSVladimir Oltean 		return SPEED_2500;
127941fed17fSVladimir Oltean 	return SPEED_UNKNOWN;
128041fed17fSVladimir Oltean }
12818aa9ebccSVladimir Oltean 
12828400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
12838aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
12848400cff6SVladimir Oltean 				      int speed_mbps)
12858aa9ebccSVladimir Oltean {
12868aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
12878aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
128841fed17fSVladimir Oltean 	u64 speed;
12898aa9ebccSVladimir Oltean 	int rc;
12908aa9ebccSVladimir Oltean 
12918400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
12928400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
12938400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
12948400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
12958400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
12968400cff6SVladimir Oltean 	 */
12978aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
12988aa9ebccSVladimir Oltean 
1299f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
1300c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
1301a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
1302a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
1303a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
1304a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
1305a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
1306a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
1307a979a0abSVladimir Oltean 		 */
130841fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
1309f4cfcfbdSVladimir Oltean 		break;
1310c44d0535SVladimir Oltean 	case SPEED_10:
131141fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_10MBPS];
1312f4cfcfbdSVladimir Oltean 		break;
1313c44d0535SVladimir Oltean 	case SPEED_100:
131441fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_100MBPS];
1315f4cfcfbdSVladimir Oltean 		break;
1316c44d0535SVladimir Oltean 	case SPEED_1000:
131741fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
1318f4cfcfbdSVladimir Oltean 		break;
131956b63466SVladimir Oltean 	case SPEED_2500:
132056b63466SVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
132156b63466SVladimir Oltean 		break;
1322f4cfcfbdSVladimir Oltean 	default:
13238aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
13248aa9ebccSVladimir Oltean 		return -EINVAL;
13258aa9ebccSVladimir Oltean 	}
13268aa9ebccSVladimir Oltean 
13278400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
13288400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
13298400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
13308400cff6SVladimir Oltean 	 * we want auto during upload phase).
1331ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
1332ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
13338aa9ebccSVladimir Oltean 	 */
133491a05078SVladimir Oltean 	if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII)
133541fed17fSVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
133656b63466SVladimir Oltean 	else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX)
133756b63466SVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
1338ffe10e67SVladimir Oltean 	else
13398aa9ebccSVladimir Oltean 		mac[port].speed = speed;
13408aa9ebccSVladimir Oltean 
13418aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
13428400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
13438400cff6SVladimir Oltean 					  &mac[port], true);
13448aa9ebccSVladimir Oltean 	if (rc < 0) {
13458aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
13468aa9ebccSVladimir Oltean 		return rc;
13478aa9ebccSVladimir Oltean 	}
13488aa9ebccSVladimir Oltean 
13498aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
13508aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
13518aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
13528aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
13538aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
13548aa9ebccSVladimir Oltean 	 */
135591a05078SVladimir Oltean 	if (!phy_interface_mode_is_rgmii(priv->phy_mode[port]))
13568aa9ebccSVladimir Oltean 		return 0;
13578aa9ebccSVladimir Oltean 
13588aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
13598aa9ebccSVladimir Oltean }
13608aa9ebccSVladimir Oltean 
1361827b4ef2SRussell King (Oracle) static struct phylink_pcs *
1362827b4ef2SRussell King (Oracle) sja1105_mac_select_pcs(struct dsa_switch *ds, int port, phy_interface_t iface)
13638aa9ebccSVladimir Oltean {
13648aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1365827b4ef2SRussell King (Oracle) 	struct dw_xpcs *xpcs = priv->xpcs[port];
1366ffe10e67SVladimir Oltean 
13673ad1d171SVladimir Oltean 	if (xpcs)
1368827b4ef2SRussell King (Oracle) 		return &xpcs->pcs;
1369827b4ef2SRussell King (Oracle) 
1370827b4ef2SRussell King (Oracle) 	return NULL;
13718400cff6SVladimir Oltean }
13728400cff6SVladimir Oltean 
13738400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
13748400cff6SVladimir Oltean 				  unsigned int mode,
13758400cff6SVladimir Oltean 				  phy_interface_t interface)
13768400cff6SVladimir Oltean {
13778400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
13788400cff6SVladimir Oltean }
13798400cff6SVladimir Oltean 
13808400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
13818400cff6SVladimir Oltean 				unsigned int mode,
13828400cff6SVladimir Oltean 				phy_interface_t interface,
13835b502a7bSRussell King 				struct phy_device *phydev,
13845b502a7bSRussell King 				int speed, int duplex,
13855b502a7bSRussell King 				bool tx_pause, bool rx_pause)
13868400cff6SVladimir Oltean {
1387ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1388ec8582d1SVladimir Oltean 
1389ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1390ec8582d1SVladimir Oltean 
1391ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
13928aa9ebccSVladimir Oltean }
13938aa9ebccSVladimir Oltean 
1394a420b757SRussell King (Oracle) static void sja1105_phylink_get_caps(struct dsa_switch *ds, int port,
1395a420b757SRussell King (Oracle) 				     struct phylink_config *config)
1396a420b757SRussell King (Oracle) {
1397a420b757SRussell King (Oracle) 	struct sja1105_private *priv = ds->priv;
13989c318be1SRussell King (Oracle) 	struct sja1105_xmii_params_entry *mii;
139983dc4c2aSRussell King (Oracle) 	phy_interface_t phy_mode;
1400a420b757SRussell King (Oracle) 
14012d1d548eSRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
14022d1d548eSRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
14032d1d548eSRussell King (Oracle) 	 * as non-legacy.
14042d1d548eSRussell King (Oracle) 	 */
14052d1d548eSRussell King (Oracle) 	config->legacy_pre_march2020 = false;
14062d1d548eSRussell King (Oracle) 
140783dc4c2aSRussell King (Oracle) 	phy_mode = priv->phy_mode[port];
140883dc4c2aSRussell King (Oracle) 	if (phy_mode == PHY_INTERFACE_MODE_SGMII ||
140983dc4c2aSRussell King (Oracle) 	    phy_mode == PHY_INTERFACE_MODE_2500BASEX) {
141083dc4c2aSRussell King (Oracle) 		/* Changing the PHY mode on SERDES ports is possible and makes
141183dc4c2aSRussell King (Oracle) 		 * sense, because that is done through the XPCS. We allow
141283dc4c2aSRussell King (Oracle) 		 * changes between SGMII and 2500base-X.
1413a420b757SRussell King (Oracle) 		 */
141483dc4c2aSRussell King (Oracle) 		if (priv->info->supports_sgmii[port])
141583dc4c2aSRussell King (Oracle) 			__set_bit(PHY_INTERFACE_MODE_SGMII,
141683dc4c2aSRussell King (Oracle) 				  config->supported_interfaces);
141783dc4c2aSRussell King (Oracle) 
141883dc4c2aSRussell King (Oracle) 		if (priv->info->supports_2500basex[port])
141983dc4c2aSRussell King (Oracle) 			__set_bit(PHY_INTERFACE_MODE_2500BASEX,
142083dc4c2aSRussell King (Oracle) 				  config->supported_interfaces);
142183dc4c2aSRussell King (Oracle) 	} else {
142283dc4c2aSRussell King (Oracle) 		/* The SJA1105 MAC programming model is through the static
142383dc4c2aSRussell King (Oracle) 		 * config (the xMII Mode table cannot be dynamically
142483dc4c2aSRussell King (Oracle) 		 * reconfigured), and we have to program that early.
142583dc4c2aSRussell King (Oracle) 		 */
142683dc4c2aSRussell King (Oracle) 		__set_bit(phy_mode, config->supported_interfaces);
142783dc4c2aSRussell King (Oracle) 	}
1428ad9f299aSVladimir Oltean 
1429ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1430ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1431ad9f299aSVladimir Oltean 	 */
14329c318be1SRussell King (Oracle) 	config->mac_capabilities = MAC_10FD | MAC_100FD;
14339c318be1SRussell King (Oracle) 
14349c318be1SRussell King (Oracle) 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1435ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1436ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
14379c318be1SRussell King (Oracle) 		config->mac_capabilities |= MAC_1000FD;
1438ad9f299aSVladimir Oltean 
14399c318be1SRussell King (Oracle) 	if (priv->info->supports_2500basex[port])
14409c318be1SRussell King (Oracle) 		config->mac_capabilities |= MAC_2500FD;
1441ad9f299aSVladimir Oltean }
1442ad9f299aSVladimir Oltean 
144360f6053fSVladimir Oltean static int
144460f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
144560f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
144660f6053fSVladimir Oltean {
144760f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
144860f6053fSVladimir Oltean 	struct sja1105_table *table;
144960f6053fSVladimir Oltean 	int i;
145060f6053fSVladimir Oltean 
145160f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
145260f6053fSVladimir Oltean 	l2_lookup = table->entries;
145360f6053fSVladimir Oltean 
145460f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
145560f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
145660f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
145760f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
145860f6053fSVladimir Oltean 			return i;
145960f6053fSVladimir Oltean 
146060f6053fSVladimir Oltean 	return -1;
146160f6053fSVladimir Oltean }
146260f6053fSVladimir Oltean 
146360f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
146460f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
146560f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
146660f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
146760f6053fSVladimir Oltean  */
146860f6053fSVladimir Oltean static int
146960f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
147060f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
147160f6053fSVladimir Oltean 			  bool keep)
147260f6053fSVladimir Oltean {
147360f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
147460f6053fSVladimir Oltean 	struct sja1105_table *table;
147560f6053fSVladimir Oltean 	int rc, match;
147660f6053fSVladimir Oltean 
147760f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
147860f6053fSVladimir Oltean 
147960f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
148060f6053fSVladimir Oltean 	if (match < 0) {
148160f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
148260f6053fSVladimir Oltean 		if (!keep)
148360f6053fSVladimir Oltean 			return 0;
148460f6053fSVladimir Oltean 
148560f6053fSVladimir Oltean 		/* No match => new entry */
148660f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
148760f6053fSVladimir Oltean 		if (rc)
148860f6053fSVladimir Oltean 			return rc;
148960f6053fSVladimir Oltean 
149060f6053fSVladimir Oltean 		match = table->entry_count - 1;
149160f6053fSVladimir Oltean 	}
149260f6053fSVladimir Oltean 
149360f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
149460f6053fSVladimir Oltean 	l2_lookup = table->entries;
149560f6053fSVladimir Oltean 
149660f6053fSVladimir Oltean 	/* We have a match.
149760f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
149860f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
149960f6053fSVladimir Oltean 	 * which we update it).
150060f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
150160f6053fSVladimir Oltean 	 */
150260f6053fSVladimir Oltean 	if (keep) {
150360f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
150460f6053fSVladimir Oltean 		return 0;
150560f6053fSVladimir Oltean 	}
150660f6053fSVladimir Oltean 
150760f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
150860f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
150960f6053fSVladimir Oltean 	 */
151060f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
151160f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
151260f6053fSVladimir Oltean }
151360f6053fSVladimir Oltean 
1514291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1515291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1516291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1517291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1518291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1519291d1e72SVladimir Oltean  */
152009c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1521291d1e72SVladimir Oltean {
1522291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1523291d1e72SVladimir Oltean }
1524291d1e72SVladimir Oltean 
15259dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1526291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1527291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1528291d1e72SVladimir Oltean 					 int *last_unused)
1529291d1e72SVladimir Oltean {
1530291d1e72SVladimir Oltean 	int way;
1531291d1e72SVladimir Oltean 
1532291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1533291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1534291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1535291d1e72SVladimir Oltean 
1536291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1537291d1e72SVladimir Oltean 		 * into the return value
1538291d1e72SVladimir Oltean 		 */
1539291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1540291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1541291d1e72SVladimir Oltean 			if (last_unused)
1542291d1e72SVladimir Oltean 				*last_unused = way;
1543291d1e72SVladimir Oltean 			continue;
1544291d1e72SVladimir Oltean 		}
1545291d1e72SVladimir Oltean 
1546291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1547291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1548291d1e72SVladimir Oltean 			if (match)
1549291d1e72SVladimir Oltean 				*match = l2_lookup;
1550291d1e72SVladimir Oltean 			return way;
1551291d1e72SVladimir Oltean 		}
1552291d1e72SVladimir Oltean 	}
1553291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1554291d1e72SVladimir Oltean 	return -1;
1555291d1e72SVladimir Oltean }
1556291d1e72SVladimir Oltean 
15579dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1558291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1559291d1e72SVladimir Oltean {
15606c5fc159SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp;
1561291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1562291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1563291d1e72SVladimir Oltean 	int last_unused = -1;
15646c5fc159SVladimir Oltean 	int start, end, i;
156560f6053fSVladimir Oltean 	int bin, way, rc;
1566291d1e72SVladimir Oltean 
15679dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1568291d1e72SVladimir Oltean 
15699dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1570291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1571291d1e72SVladimir Oltean 	if (way >= 0) {
1572291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1573291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1574291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1575291d1e72SVladimir Oltean 		 */
1576e11e865bSVladimir Oltean 		if ((l2_lookup.destports & BIT(port)) && l2_lookup.lockeds)
1577291d1e72SVladimir Oltean 			return 0;
1578291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1579291d1e72SVladimir Oltean 	} else {
1580291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1581291d1e72SVladimir Oltean 
1582291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1583291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1584291d1e72SVladimir Oltean 		 */
1585291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1586291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1587291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1588291d1e72SVladimir Oltean 
1589291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1590291d1e72SVladimir Oltean 			way = last_unused;
1591291d1e72SVladimir Oltean 		} else {
1592291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1593291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1594291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1595291d1e72SVladimir Oltean 			 * distribution function:
1596291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1597291d1e72SVladimir Oltean 			 */
1598291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1599291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1600291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1601291d1e72SVladimir Oltean 				 bin, addr, way);
1602291d1e72SVladimir Oltean 			/* Evict entry */
1603291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1604291d1e72SVladimir Oltean 						     index, NULL, false);
1605291d1e72SVladimir Oltean 		}
1606291d1e72SVladimir Oltean 	}
1607e11e865bSVladimir Oltean 	l2_lookup.lockeds = true;
1608291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1609291d1e72SVladimir Oltean 
161060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1611291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1612291d1e72SVladimir Oltean 					  true);
161360f6053fSVladimir Oltean 	if (rc < 0)
161460f6053fSVladimir Oltean 		return rc;
161560f6053fSVladimir Oltean 
16166c5fc159SVladimir Oltean 	/* Invalidate a dynamically learned entry if that exists */
16176c5fc159SVladimir Oltean 	start = sja1105et_fdb_index(bin, 0);
16186c5fc159SVladimir Oltean 	end = sja1105et_fdb_index(bin, way);
16196c5fc159SVladimir Oltean 
16206c5fc159SVladimir Oltean 	for (i = start; i < end; i++) {
16216c5fc159SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
16226c5fc159SVladimir Oltean 						 i, &tmp);
16236c5fc159SVladimir Oltean 		if (rc == -ENOENT)
16246c5fc159SVladimir Oltean 			continue;
16256c5fc159SVladimir Oltean 		if (rc)
16266c5fc159SVladimir Oltean 			return rc;
16276c5fc159SVladimir Oltean 
16286c5fc159SVladimir Oltean 		if (tmp.macaddr != ether_addr_to_u64(addr) || tmp.vlanid != vid)
16296c5fc159SVladimir Oltean 			continue;
16306c5fc159SVladimir Oltean 
16316c5fc159SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
16326c5fc159SVladimir Oltean 						  i, NULL, false);
16336c5fc159SVladimir Oltean 		if (rc)
16346c5fc159SVladimir Oltean 			return rc;
16356c5fc159SVladimir Oltean 
16366c5fc159SVladimir Oltean 		break;
16376c5fc159SVladimir Oltean 	}
16386c5fc159SVladimir Oltean 
163960f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1640291d1e72SVladimir Oltean }
1641291d1e72SVladimir Oltean 
16429dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1643291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1644291d1e72SVladimir Oltean {
1645291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1646291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
164760f6053fSVladimir Oltean 	int index, bin, way, rc;
1648291d1e72SVladimir Oltean 	bool keep;
1649291d1e72SVladimir Oltean 
16509dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
16519dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1652291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1653291d1e72SVladimir Oltean 	if (way < 0)
1654291d1e72SVladimir Oltean 		return 0;
1655291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1656291d1e72SVladimir Oltean 
1657291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1658291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1659291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1660291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1661291d1e72SVladimir Oltean 	 */
1662291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
16637752e937SVladimir Oltean 
1664291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1665291d1e72SVladimir Oltean 		keep = true;
1666291d1e72SVladimir Oltean 	else
1667291d1e72SVladimir Oltean 		keep = false;
1668291d1e72SVladimir Oltean 
166960f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1670291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
167160f6053fSVladimir Oltean 	if (rc < 0)
167260f6053fSVladimir Oltean 		return rc;
167360f6053fSVladimir Oltean 
167460f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1675291d1e72SVladimir Oltean }
1676291d1e72SVladimir Oltean 
16779dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
16789dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
16799dfa6911SVladimir Oltean {
16806c5fc159SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp;
16811da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16821da73821SVladimir Oltean 	int rc, i;
16831da73821SVladimir Oltean 
16841da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
16851da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
16861da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
16871da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
16881da73821SVladimir Oltean 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
16891da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
16901da73821SVladimir Oltean 
1691728db843SVladimir Oltean 	tmp = l2_lookup;
1692728db843SVladimir Oltean 
16931da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1694728db843SVladimir Oltean 					 SJA1105_SEARCH, &tmp);
1695728db843SVladimir Oltean 	if (rc == 0 && tmp.index != SJA1105_MAX_L2_LOOKUP_COUNT - 1) {
1696e11e865bSVladimir Oltean 		/* Found a static entry and this port is already in the entry's
16971da73821SVladimir Oltean 		 * port mask => job done
16981da73821SVladimir Oltean 		 */
1699728db843SVladimir Oltean 		if ((tmp.destports & BIT(port)) && tmp.lockeds)
17001da73821SVladimir Oltean 			return 0;
1701728db843SVladimir Oltean 
1702728db843SVladimir Oltean 		l2_lookup = tmp;
1703728db843SVladimir Oltean 
17041da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
17051da73821SVladimir Oltean 		 * found something.
17061da73821SVladimir Oltean 		 */
17071da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
17081da73821SVladimir Oltean 		goto skip_finding_an_index;
17091da73821SVladimir Oltean 	}
17101da73821SVladimir Oltean 
17111da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
17121da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
17131da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
17141da73821SVladimir Oltean 	 */
17151da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
17161da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
17171da73821SVladimir Oltean 						 i, NULL);
17181da73821SVladimir Oltean 		if (rc < 0)
17191da73821SVladimir Oltean 			break;
17201da73821SVladimir Oltean 	}
17211da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
17221da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
17231da73821SVladimir Oltean 		return -EINVAL;
17241da73821SVladimir Oltean 	}
17251da73821SVladimir Oltean 	l2_lookup.index = i;
17261da73821SVladimir Oltean 
17271da73821SVladimir Oltean skip_finding_an_index:
1728e11e865bSVladimir Oltean 	l2_lookup.lockeds = true;
1729e11e865bSVladimir Oltean 
173060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
17311da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
17321da73821SVladimir Oltean 					  true);
173360f6053fSVladimir Oltean 	if (rc < 0)
173460f6053fSVladimir Oltean 		return rc;
173560f6053fSVladimir Oltean 
17366c5fc159SVladimir Oltean 	/* The switch learns dynamic entries and looks up the FDB left to
17376c5fc159SVladimir Oltean 	 * right. It is possible that our addition was concurrent with the
17386c5fc159SVladimir Oltean 	 * dynamic learning of the same address, so now that the static entry
17396c5fc159SVladimir Oltean 	 * has been installed, we are certain that address learning for this
17406c5fc159SVladimir Oltean 	 * particular address has been turned off, so the dynamic entry either
17416c5fc159SVladimir Oltean 	 * is in the FDB at an index smaller than the static one, or isn't (it
17426c5fc159SVladimir Oltean 	 * can also be at a larger index, but in that case it is inactive
17436c5fc159SVladimir Oltean 	 * because the static FDB entry will match first, and the dynamic one
17446c5fc159SVladimir Oltean 	 * will eventually age out). Search for a dynamically learned address
17456c5fc159SVladimir Oltean 	 * prior to our static one and invalidate it.
17466c5fc159SVladimir Oltean 	 */
17476c5fc159SVladimir Oltean 	tmp = l2_lookup;
17486c5fc159SVladimir Oltean 
17496c5fc159SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
17506c5fc159SVladimir Oltean 					 SJA1105_SEARCH, &tmp);
17516c5fc159SVladimir Oltean 	if (rc < 0) {
17526c5fc159SVladimir Oltean 		dev_err(ds->dev,
17536c5fc159SVladimir Oltean 			"port %d failed to read back entry for %pM vid %d: %pe\n",
17546c5fc159SVladimir Oltean 			port, addr, vid, ERR_PTR(rc));
17556c5fc159SVladimir Oltean 		return rc;
17566c5fc159SVladimir Oltean 	}
17576c5fc159SVladimir Oltean 
17586c5fc159SVladimir Oltean 	if (tmp.index < l2_lookup.index) {
17596c5fc159SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
17606c5fc159SVladimir Oltean 						  tmp.index, NULL, false);
17616c5fc159SVladimir Oltean 		if (rc < 0)
17626c5fc159SVladimir Oltean 			return rc;
17636c5fc159SVladimir Oltean 	}
17646c5fc159SVladimir Oltean 
176560f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
17669dfa6911SVladimir Oltean }
17679dfa6911SVladimir Oltean 
17689dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
17699dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
17709dfa6911SVladimir Oltean {
17711da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
17721da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
17731da73821SVladimir Oltean 	bool keep;
17741da73821SVladimir Oltean 	int rc;
17751da73821SVladimir Oltean 
17761da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
17771da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
17781da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
17791da73821SVladimir Oltean 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
17801da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
17811da73821SVladimir Oltean 
17821da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
17831da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
17841da73821SVladimir Oltean 	if (rc < 0)
17851da73821SVladimir Oltean 		return 0;
17861da73821SVladimir Oltean 
17871da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
17881da73821SVladimir Oltean 
17891da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
17901da73821SVladimir Oltean 	 * or if we remove it completely.
17911da73821SVladimir Oltean 	 */
17921da73821SVladimir Oltean 	if (l2_lookup.destports)
17931da73821SVladimir Oltean 		keep = true;
17941da73821SVladimir Oltean 	else
17951da73821SVladimir Oltean 		keep = false;
17961da73821SVladimir Oltean 
179760f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
17981da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
179960f6053fSVladimir Oltean 	if (rc < 0)
180060f6053fSVladimir Oltean 		return rc;
180160f6053fSVladimir Oltean 
180260f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
18039dfa6911SVladimir Oltean }
18049dfa6911SVladimir Oltean 
18059dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
18069dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
18079dfa6911SVladimir Oltean {
18089dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1809b3ee526aSVladimir Oltean 
18106d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
18119dfa6911SVladimir Oltean }
18129dfa6911SVladimir Oltean 
18139dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
18149dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
18159dfa6911SVladimir Oltean {
18169dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18179dfa6911SVladimir Oltean 
1818b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
18199dfa6911SVladimir Oltean }
18209dfa6911SVladimir Oltean 
1821291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1822291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1823291d1e72SVladimir Oltean {
18249aad3e4eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
1825291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1826291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1827291d1e72SVladimir Oltean 	int i;
1828291d1e72SVladimir Oltean 
1829291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1830291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1831291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1832291d1e72SVladimir Oltean 		int rc;
1833291d1e72SVladimir Oltean 
1834291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1835291d1e72SVladimir Oltean 						 i, &l2_lookup);
1836291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1837def84604SVladimir Oltean 		if (rc == -ENOENT)
1838291d1e72SVladimir Oltean 			continue;
1839291d1e72SVladimir Oltean 		if (rc) {
1840291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1841291d1e72SVladimir Oltean 			return rc;
1842291d1e72SVladimir Oltean 		}
1843291d1e72SVladimir Oltean 
1844291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1845291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1846291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1847291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1848291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1849291d1e72SVladimir Oltean 		 */
1850291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1851291d1e72SVladimir Oltean 			continue;
18524d942354SVladimir Oltean 
18534d942354SVladimir Oltean 		/* We need to hide the FDB entry for unknown multicast */
18544d942354SVladimir Oltean 		if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST &&
18554d942354SVladimir Oltean 		    l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
18564d942354SVladimir Oltean 			continue;
18574d942354SVladimir Oltean 
1858291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
185993647594SVladimir Oltean 
18606d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
18619aad3e4eSVladimir Oltean 		if (!dsa_port_is_vlan_filtering(dp))
18626d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
186321b52fedSVladimir Oltean 		rc = cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
186421b52fedSVladimir Oltean 		if (rc)
186521b52fedSVladimir Oltean 			return rc;
1866291d1e72SVladimir Oltean 	}
1867291d1e72SVladimir Oltean 	return 0;
1868291d1e72SVladimir Oltean }
1869291d1e72SVladimir Oltean 
18705126ec72SVladimir Oltean static void sja1105_fast_age(struct dsa_switch *ds, int port)
18715126ec72SVladimir Oltean {
18725126ec72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18735126ec72SVladimir Oltean 	int i;
18745126ec72SVladimir Oltean 
18755126ec72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
18765126ec72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
18775126ec72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
18785126ec72SVladimir Oltean 		int rc;
18795126ec72SVladimir Oltean 
18805126ec72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
18815126ec72SVladimir Oltean 						 i, &l2_lookup);
18825126ec72SVladimir Oltean 		/* No fdb entry at i, not an issue */
18835126ec72SVladimir Oltean 		if (rc == -ENOENT)
18845126ec72SVladimir Oltean 			continue;
18855126ec72SVladimir Oltean 		if (rc) {
18865126ec72SVladimir Oltean 			dev_err(ds->dev, "Failed to read FDB: %pe\n",
18875126ec72SVladimir Oltean 				ERR_PTR(rc));
18885126ec72SVladimir Oltean 			return;
18895126ec72SVladimir Oltean 		}
18905126ec72SVladimir Oltean 
18915126ec72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
18925126ec72SVladimir Oltean 			continue;
18935126ec72SVladimir Oltean 
18945126ec72SVladimir Oltean 		/* Don't delete static FDB entries */
18955126ec72SVladimir Oltean 		if (l2_lookup.lockeds)
18965126ec72SVladimir Oltean 			continue;
18975126ec72SVladimir Oltean 
18985126ec72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
18995126ec72SVladimir Oltean 
19005126ec72SVladimir Oltean 		rc = sja1105_fdb_del(ds, port, macaddr, l2_lookup.vlanid);
19015126ec72SVladimir Oltean 		if (rc) {
19025126ec72SVladimir Oltean 			dev_err(ds->dev,
19035126ec72SVladimir Oltean 				"Failed to delete FDB entry %pM vid %lld: %pe\n",
19045126ec72SVladimir Oltean 				macaddr, l2_lookup.vlanid, ERR_PTR(rc));
19055126ec72SVladimir Oltean 			return;
19065126ec72SVladimir Oltean 		}
19075126ec72SVladimir Oltean 	}
19085126ec72SVladimir Oltean }
19095126ec72SVladimir Oltean 
1910a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port,
1911291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1912291d1e72SVladimir Oltean {
1913a52b2da7SVladimir Oltean 	return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1914291d1e72SVladimir Oltean }
1915291d1e72SVladimir Oltean 
1916291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1917291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1918291d1e72SVladimir Oltean {
1919291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1920291d1e72SVladimir Oltean }
1921291d1e72SVladimir Oltean 
19227f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration.
19237f7ccdeaSVladimir Oltean  * Flooding is configured between each {ingress, egress} port pair, and since
19247f7ccdeaSVladimir Oltean  * the bridge's semantics are those of "egress flooding", it means we must
19257f7ccdeaSVladimir Oltean  * enable flooding towards this port from all ingress ports that are in the
19267f7ccdeaSVladimir Oltean  * same forwarding domain.
19277f7ccdeaSVladimir Oltean  */
19287f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv)
19297f7ccdeaSVladimir Oltean {
19307f7ccdeaSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
19317f7ccdeaSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
19327f7ccdeaSVladimir Oltean 	int from, to, rc;
19337f7ccdeaSVladimir Oltean 
19347f7ccdeaSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
19357f7ccdeaSVladimir Oltean 
19367f7ccdeaSVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
19377f7ccdeaSVladimir Oltean 		u64 fl_domain = 0, bc_domain = 0;
19387f7ccdeaSVladimir Oltean 
19397f7ccdeaSVladimir Oltean 		for (to = 0; to < priv->ds->num_ports; to++) {
19407f7ccdeaSVladimir Oltean 			if (!sja1105_can_forward(l2_fwd, from, to))
19417f7ccdeaSVladimir Oltean 				continue;
19427f7ccdeaSVladimir Oltean 
19437f7ccdeaSVladimir Oltean 			if (priv->ucast_egress_floods & BIT(to))
19447f7ccdeaSVladimir Oltean 				fl_domain |= BIT(to);
19457f7ccdeaSVladimir Oltean 			if (priv->bcast_egress_floods & BIT(to))
19467f7ccdeaSVladimir Oltean 				bc_domain |= BIT(to);
19477f7ccdeaSVladimir Oltean 		}
19487f7ccdeaSVladimir Oltean 
19497f7ccdeaSVladimir Oltean 		/* Nothing changed, nothing to do */
19507f7ccdeaSVladimir Oltean 		if (l2_fwd[from].fl_domain == fl_domain &&
19517f7ccdeaSVladimir Oltean 		    l2_fwd[from].bc_domain == bc_domain)
19527f7ccdeaSVladimir Oltean 			continue;
19537f7ccdeaSVladimir Oltean 
19547f7ccdeaSVladimir Oltean 		l2_fwd[from].fl_domain = fl_domain;
19557f7ccdeaSVladimir Oltean 		l2_fwd[from].bc_domain = bc_domain;
19567f7ccdeaSVladimir Oltean 
19577f7ccdeaSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
19587f7ccdeaSVladimir Oltean 						  from, &l2_fwd[from], true);
19597f7ccdeaSVladimir Oltean 		if (rc < 0)
19607f7ccdeaSVladimir Oltean 			return rc;
19617f7ccdeaSVladimir Oltean 	}
19627f7ccdeaSVladimir Oltean 
19637f7ccdeaSVladimir Oltean 	return 0;
19647f7ccdeaSVladimir Oltean }
19657f7ccdeaSVladimir Oltean 
19668aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
1967d3eed0e5SVladimir Oltean 				 struct dsa_bridge bridge, bool member)
19688aa9ebccSVladimir Oltean {
19698aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
19708aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
19718aa9ebccSVladimir Oltean 	int i, rc;
19728aa9ebccSVladimir Oltean 
19738aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
19748aa9ebccSVladimir Oltean 
1975542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
19768aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
19778aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
19788aa9ebccSVladimir Oltean 		 */
19798aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
19808aa9ebccSVladimir Oltean 			continue;
19818aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
19828aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
19838aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
19848aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
19858aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
19868aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
19878aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
19888aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
19898aa9ebccSVladimir Oltean 		 */
19908aa9ebccSVladimir Oltean 		if (i == port)
19918aa9ebccSVladimir Oltean 			continue;
1992d3eed0e5SVladimir Oltean 		if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
19938aa9ebccSVladimir Oltean 			continue;
19948aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
19958aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
19968aa9ebccSVladimir Oltean 
19978aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
19988aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
19998aa9ebccSVladimir Oltean 		if (rc < 0)
20008aa9ebccSVladimir Oltean 			return rc;
20018aa9ebccSVladimir Oltean 	}
20028aa9ebccSVladimir Oltean 
20037f7ccdeaSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
20048aa9ebccSVladimir Oltean 					  port, &l2_fwd[port], true);
20057f7ccdeaSVladimir Oltean 	if (rc)
20067f7ccdeaSVladimir Oltean 		return rc;
20077f7ccdeaSVladimir Oltean 
2008cde8078eSVladimir Oltean 	rc = sja1105_commit_pvid(ds, port);
2009cde8078eSVladimir Oltean 	if (rc)
2010cde8078eSVladimir Oltean 		return rc;
2011cde8078eSVladimir Oltean 
20127f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
20138aa9ebccSVladimir Oltean }
20148aa9ebccSVladimir Oltean 
2015640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
2016640f763fSVladimir Oltean 					 u8 state)
2017640f763fSVladimir Oltean {
20185313a37bSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
2019640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2020640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2021640f763fSVladimir Oltean 
2022640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2023640f763fSVladimir Oltean 
2024640f763fSVladimir Oltean 	switch (state) {
2025640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
2026640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
2027640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
2028640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
2029640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
2030640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
2031640f763fSVladimir Oltean 		 */
2032640f763fSVladimir Oltean 		mac[port].ingress   = false;
2033640f763fSVladimir Oltean 		mac[port].egress    = false;
2034640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
2035640f763fSVladimir Oltean 		break;
2036640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
2037640f763fSVladimir Oltean 		mac[port].ingress   = true;
2038640f763fSVladimir Oltean 		mac[port].egress    = false;
2039640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
2040640f763fSVladimir Oltean 		break;
2041640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
2042640f763fSVladimir Oltean 		mac[port].ingress   = true;
2043640f763fSVladimir Oltean 		mac[port].egress    = false;
20445313a37bSVladimir Oltean 		mac[port].dyn_learn = dp->learning;
2045640f763fSVladimir Oltean 		break;
2046640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
2047640f763fSVladimir Oltean 		mac[port].ingress   = true;
2048640f763fSVladimir Oltean 		mac[port].egress    = true;
20495313a37bSVladimir Oltean 		mac[port].dyn_learn = dp->learning;
2050640f763fSVladimir Oltean 		break;
2051640f763fSVladimir Oltean 	default:
2052640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
2053640f763fSVladimir Oltean 		return;
2054640f763fSVladimir Oltean 	}
2055640f763fSVladimir Oltean 
2056640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
2057640f763fSVladimir Oltean 				     &mac[port], true);
2058640f763fSVladimir Oltean }
2059640f763fSVladimir Oltean 
20608aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
2061b079922bSVladimir Oltean 			       struct dsa_bridge bridge,
2062b079922bSVladimir Oltean 			       bool *tx_fwd_offload)
20638aa9ebccSVladimir Oltean {
2064857fdd74SVladimir Oltean 	int rc;
2065857fdd74SVladimir Oltean 
2066857fdd74SVladimir Oltean 	rc = sja1105_bridge_member(ds, port, bridge, true);
2067857fdd74SVladimir Oltean 	if (rc)
2068857fdd74SVladimir Oltean 		return rc;
2069857fdd74SVladimir Oltean 
2070*91495f21SVladimir Oltean 	rc = dsa_tag_8021q_bridge_join(ds, port, bridge);
2071857fdd74SVladimir Oltean 	if (rc) {
2072857fdd74SVladimir Oltean 		sja1105_bridge_member(ds, port, bridge, false);
2073857fdd74SVladimir Oltean 		return rc;
2074857fdd74SVladimir Oltean 	}
2075857fdd74SVladimir Oltean 
2076857fdd74SVladimir Oltean 	*tx_fwd_offload = true;
2077857fdd74SVladimir Oltean 
2078857fdd74SVladimir Oltean 	return 0;
20798aa9ebccSVladimir Oltean }
20808aa9ebccSVladimir Oltean 
20818aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
2082d3eed0e5SVladimir Oltean 				 struct dsa_bridge bridge)
20838aa9ebccSVladimir Oltean {
2084*91495f21SVladimir Oltean 	dsa_tag_8021q_bridge_leave(ds, port, bridge);
2085d3eed0e5SVladimir Oltean 	sja1105_bridge_member(ds, port, bridge, false);
20868aa9ebccSVladimir Oltean }
20878aa9ebccSVladimir Oltean 
20884d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8)
20894d752508SVladimir Oltean 
20904d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
20914d752508SVladimir Oltean {
20924d752508SVladimir Oltean 	int i;
20934d752508SVladimir Oltean 
20944d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
20954d752508SVladimir Oltean 		if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
20964d752508SVladimir Oltean 			return i;
20974d752508SVladimir Oltean 
20984d752508SVladimir Oltean 	return -1;
20994d752508SVladimir Oltean }
21004d752508SVladimir Oltean 
21014d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
21024d752508SVladimir Oltean 				     int prio)
21034d752508SVladimir Oltean {
21044d752508SVladimir Oltean 	int i;
21054d752508SVladimir Oltean 
21064d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
21074d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
21084d752508SVladimir Oltean 
21094d752508SVladimir Oltean 		if (cbs->port == port && cbs->prio == prio) {
21104d752508SVladimir Oltean 			memset(cbs, 0, sizeof(*cbs));
21114d752508SVladimir Oltean 			return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
21124d752508SVladimir Oltean 							    i, cbs, true);
21134d752508SVladimir Oltean 		}
21144d752508SVladimir Oltean 	}
21154d752508SVladimir Oltean 
21164d752508SVladimir Oltean 	return 0;
21174d752508SVladimir Oltean }
21184d752508SVladimir Oltean 
21194d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
21204d752508SVladimir Oltean 				struct tc_cbs_qopt_offload *offload)
21214d752508SVladimir Oltean {
21224d752508SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
21234d752508SVladimir Oltean 	struct sja1105_cbs_entry *cbs;
21244d752508SVladimir Oltean 	int index;
21254d752508SVladimir Oltean 
21264d752508SVladimir Oltean 	if (!offload->enable)
21274d752508SVladimir Oltean 		return sja1105_delete_cbs_shaper(priv, port, offload->queue);
21284d752508SVladimir Oltean 
21294d752508SVladimir Oltean 	index = sja1105_find_unused_cbs_shaper(priv);
21304d752508SVladimir Oltean 	if (index < 0)
21314d752508SVladimir Oltean 		return -ENOSPC;
21324d752508SVladimir Oltean 
21334d752508SVladimir Oltean 	cbs = &priv->cbs[index];
21344d752508SVladimir Oltean 	cbs->port = port;
21354d752508SVladimir Oltean 	cbs->prio = offload->queue;
21364d752508SVladimir Oltean 	/* locredit and sendslope are negative by definition. In hardware,
21374d752508SVladimir Oltean 	 * positive values must be provided, and the negative sign is implicit.
21384d752508SVladimir Oltean 	 */
21394d752508SVladimir Oltean 	cbs->credit_hi = offload->hicredit;
21404d752508SVladimir Oltean 	cbs->credit_lo = abs(offload->locredit);
21414d752508SVladimir Oltean 	/* User space is in kbits/sec, hardware in bytes/sec */
21424d752508SVladimir Oltean 	cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
21434d752508SVladimir Oltean 	cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
21444d752508SVladimir Oltean 	/* Convert the negative values from 64-bit 2's complement
21454d752508SVladimir Oltean 	 * to 32-bit 2's complement (for the case of 0x80000000 whose
21464d752508SVladimir Oltean 	 * negative is still negative).
21474d752508SVladimir Oltean 	 */
21484d752508SVladimir Oltean 	cbs->credit_lo &= GENMASK_ULL(31, 0);
21494d752508SVladimir Oltean 	cbs->send_slope &= GENMASK_ULL(31, 0);
21504d752508SVladimir Oltean 
21514d752508SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
21524d752508SVladimir Oltean 					    true);
21534d752508SVladimir Oltean }
21544d752508SVladimir Oltean 
21554d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv)
21564d752508SVladimir Oltean {
21574d752508SVladimir Oltean 	int rc = 0, i;
21584d752508SVladimir Oltean 
2159be7f62eeSVladimir Oltean 	/* The credit based shapers are only allocated if
2160be7f62eeSVladimir Oltean 	 * CONFIG_NET_SCH_CBS is enabled.
2161be7f62eeSVladimir Oltean 	 */
2162be7f62eeSVladimir Oltean 	if (!priv->cbs)
2163be7f62eeSVladimir Oltean 		return 0;
2164be7f62eeSVladimir Oltean 
21654d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
21664d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
21674d752508SVladimir Oltean 
21684d752508SVladimir Oltean 		if (!cbs->idle_slope && !cbs->send_slope)
21694d752508SVladimir Oltean 			continue;
21704d752508SVladimir Oltean 
21714d752508SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
21724d752508SVladimir Oltean 						  true);
21734d752508SVladimir Oltean 		if (rc)
21744d752508SVladimir Oltean 			break;
21754d752508SVladimir Oltean 	}
21764d752508SVladimir Oltean 
21774d752508SVladimir Oltean 	return rc;
21784d752508SVladimir Oltean }
21794d752508SVladimir Oltean 
21802eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
21812eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
21822eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
21832eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
21842eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
2185c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
2186dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
21872eea1fa8SVladimir Oltean };
21882eea1fa8SVladimir Oltean 
21896666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
21906666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
21916666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
21926666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
21936666cebcSVladimir Oltean  * such that this operation is relatively seamless.
21946666cebcSVladimir Oltean  */
21952eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
21962eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
21976666cebcSVladimir Oltean {
21986cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
21996cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
220082760d7fSVladimir Oltean 	int speed_mbps[SJA1105_MAX_NUM_PORTS];
220184db00f2SVladimir Oltean 	u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0};
22026666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
22036cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
22046cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
22056cf99c13SVladimir Oltean 	s64 t12, t34;
22066666cebcSVladimir Oltean 	int rc, i;
22076cf99c13SVladimir Oltean 	s64 now;
22086666cebcSVladimir Oltean 
2209af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
2210af580ae2SVladimir Oltean 
22116666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
22126666cebcSVladimir Oltean 
22138400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
22148400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
22158400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
22168400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
22176666cebcSVladimir Oltean 	 */
2218542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
22193ad1d171SVladimir Oltean 		u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1);
22203ad1d171SVladimir Oltean 
222141fed17fSVladimir Oltean 		speed_mbps[i] = sja1105_port_speed_to_ethtool(priv,
222241fed17fSVladimir Oltean 							      mac[i].speed);
222341fed17fSVladimir Oltean 		mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
22246666cebcSVladimir Oltean 
22253ad1d171SVladimir Oltean 		if (priv->xpcs[i])
22263ad1d171SVladimir Oltean 			bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr);
222784db00f2SVladimir Oltean 	}
2228ffe10e67SVladimir Oltean 
22296cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
22306cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
22316cf99c13SVladimir Oltean 
22326cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
223361c77533SVladimir Oltean 	if (rc < 0) {
223461c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
223561c77533SVladimir Oltean 		goto out;
223661c77533SVladimir Oltean 	}
22376cf99c13SVladimir Oltean 
22386666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
22396666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
224061c77533SVladimir Oltean 	if (rc < 0) {
224161c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
224261c77533SVladimir Oltean 		goto out;
224361c77533SVladimir Oltean 	}
22446cf99c13SVladimir Oltean 
22456cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
224661c77533SVladimir Oltean 	if (rc < 0) {
224761c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
224861c77533SVladimir Oltean 		goto out;
224961c77533SVladimir Oltean 	}
22506cf99c13SVladimir Oltean 
22516cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
22526cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
22536cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
22546cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
22556cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
22566cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
22576cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
22586cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
22596cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
22606cf99c13SVladimir Oltean 	now += (t34 - t12);
22616cf99c13SVladimir Oltean 
22626cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
22636cf99c13SVladimir Oltean 
22646cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
22656666cebcSVladimir Oltean 
22662eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
22672eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
22682eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
22692eea1fa8SVladimir Oltean 
22706666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
22716666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
22726666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
22736666cebcSVladimir Oltean 	 */
2274cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
2275c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
22766666cebcSVladimir Oltean 		if (rc < 0)
22776666cebcSVladimir Oltean 			goto out;
2278cb5a82d2SVladimir Oltean 	}
22796666cebcSVladimir Oltean 
2280542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
22813ad1d171SVladimir Oltean 		struct dw_xpcs *xpcs = priv->xpcs[i];
22823ad1d171SVladimir Oltean 		unsigned int mode;
228384db00f2SVladimir Oltean 
22848400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
22856666cebcSVladimir Oltean 		if (rc < 0)
22866666cebcSVladimir Oltean 			goto out;
2287ffe10e67SVladimir Oltean 
22883ad1d171SVladimir Oltean 		if (!xpcs)
228984db00f2SVladimir Oltean 			continue;
2290ffe10e67SVladimir Oltean 
22913ad1d171SVladimir Oltean 		if (bmcr[i] & BMCR_ANENABLE)
22923ad1d171SVladimir Oltean 			mode = MLO_AN_INBAND;
22933ad1d171SVladimir Oltean 		else if (priv->fixed_link[i])
22943ad1d171SVladimir Oltean 			mode = MLO_AN_FIXED;
22953ad1d171SVladimir Oltean 		else
22963ad1d171SVladimir Oltean 			mode = MLO_AN_PHY;
229784db00f2SVladimir Oltean 
22983ad1d171SVladimir Oltean 		rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode);
22993ad1d171SVladimir Oltean 		if (rc < 0)
23003ad1d171SVladimir Oltean 			goto out;
2301ffe10e67SVladimir Oltean 
23023ad1d171SVladimir Oltean 		if (!phylink_autoneg_inband(mode)) {
2303ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
2304ffe10e67SVladimir Oltean 
230556b63466SVladimir Oltean 			if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX)
230656b63466SVladimir Oltean 				speed = SPEED_2500;
230756b63466SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED1000)
2308ffe10e67SVladimir Oltean 				speed = SPEED_1000;
230984db00f2SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED100)
2310ffe10e67SVladimir Oltean 				speed = SPEED_100;
2311053d8ad1SVladimir Oltean 			else
2312ffe10e67SVladimir Oltean 				speed = SPEED_10;
2313ffe10e67SVladimir Oltean 
23143ad1d171SVladimir Oltean 			xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i],
23153ad1d171SVladimir Oltean 				     speed, DUPLEX_FULL);
2316ffe10e67SVladimir Oltean 		}
2317ffe10e67SVladimir Oltean 	}
23184d752508SVladimir Oltean 
23194d752508SVladimir Oltean 	rc = sja1105_reload_cbs(priv);
23204d752508SVladimir Oltean 	if (rc < 0)
23214d752508SVladimir Oltean 		goto out;
23226666cebcSVladimir Oltean out:
2323af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
2324af580ae2SVladimir Oltean 
23256666cebcSVladimir Oltean 	return rc;
23266666cebcSVladimir Oltean }
23276666cebcSVladimir Oltean 
23288aa9ebccSVladimir Oltean static enum dsa_tag_protocol
23294d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
23304d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
23318aa9ebccSVladimir Oltean {
23324913b8ebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
23334913b8ebSVladimir Oltean 
23344913b8ebSVladimir Oltean 	return priv->info->tag_proto;
23358aa9ebccSVladimir Oltean }
23368aa9ebccSVladimir Oltean 
2337070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
2338070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
2339070ca3bbSVladimir Oltean  * So a switch reset is required.
2340070ca3bbSVladimir Oltean  */
234189153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
234289153ed6SVladimir Oltean 			   struct netlink_ext_ack *extack)
23436666cebcSVladimir Oltean {
23446d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2345070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
23466666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2347070ca3bbSVladimir Oltean 	struct sja1105_table *table;
2348dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
2349070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
23506666cebcSVladimir Oltean 	int rc;
23516666cebcSVladimir Oltean 
2352dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
2353dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
235489153ed6SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
235589153ed6SVladimir Oltean 					   "Cannot change VLAN filtering with active VL rules");
2356dfacc5a2SVladimir Oltean 			return -EBUSY;
2357dfacc5a2SVladimir Oltean 		}
2358dfacc5a2SVladimir Oltean 	}
2359dfacc5a2SVladimir Oltean 
2360070ca3bbSVladimir Oltean 	if (enabled) {
23616666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
236254fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
236354fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
2364070ca3bbSVladimir Oltean 	} else {
23656666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
2366070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
2367070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
2368070ca3bbSVladimir Oltean 	}
2369070ca3bbSVladimir Oltean 
2370070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2371070ca3bbSVladimir Oltean 	general_params = table->entries;
2372f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
237354fa49eeSVladimir Oltean 	general_params->tpid = tpid;
237454fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2375070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
237642824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
237742824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
237842824463SVladimir Oltean 	 */
237942824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
238042824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
2381070ca3bbSVladimir Oltean 
23826d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
23832cafa72eSVladimir Oltean 	 * No VLAN filtering (or best effort) => shared VLAN learning.
23846d7c7d94SVladimir Oltean 	 *
23856d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
23866d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
23876d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
23886d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
23896d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
23906d7c7d94SVladimir Oltean 	 * forwarding decision.
23916d7c7d94SVladimir Oltean 	 *
23926d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
23936d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
23946d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
23956d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
23966d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
23976d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
23986d7c7d94SVladimir Oltean 	 * (all frames get flooded).
23996d7c7d94SVladimir Oltean 	 */
24006d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
24016d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
24029aad3e4eSVladimir Oltean 	l2_lookup_params->shared_learn = !enabled;
2403aaa270c6SVladimir Oltean 
24046dfd23d3SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
24056dfd23d3SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
24066dfd23d3SVladimir Oltean 			continue;
24076dfd23d3SVladimir Oltean 
24086dfd23d3SVladimir Oltean 		rc = sja1105_commit_pvid(ds, port);
2409aef31718SVladimir Oltean 		if (rc)
2410aef31718SVladimir Oltean 			return rc;
24116dfd23d3SVladimir Oltean 	}
2412aef31718SVladimir Oltean 
24132eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
24146666cebcSVladimir Oltean 	if (rc)
241589153ed6SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype");
24166666cebcSVladimir Oltean 
24170fac6aa0SVladimir Oltean 	return rc;
24186666cebcSVladimir Oltean }
24196666cebcSVladimir Oltean 
24206dfd23d3SVladimir Oltean static int sja1105_vlan_add(struct sja1105_private *priv, int port, u16 vid,
242173ceab83SVladimir Oltean 			    u16 flags, bool allowed_ingress)
24225899ee36SVladimir Oltean {
24236dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
24246dfd23d3SVladimir Oltean 	struct sja1105_table *table;
24256dfd23d3SVladimir Oltean 	int match, rc;
24265899ee36SVladimir Oltean 
24276dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
24286dfd23d3SVladimir Oltean 
24296dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
24306dfd23d3SVladimir Oltean 	if (match < 0) {
24316dfd23d3SVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
24326dfd23d3SVladimir Oltean 		if (rc)
24336dfd23d3SVladimir Oltean 			return rc;
24346dfd23d3SVladimir Oltean 		match = table->entry_count - 1;
24356dfd23d3SVladimir Oltean 	}
24366dfd23d3SVladimir Oltean 
24376dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
24386dfd23d3SVladimir Oltean 	vlan = table->entries;
24396dfd23d3SVladimir Oltean 
24406dfd23d3SVladimir Oltean 	vlan[match].type_entry = SJA1110_VLAN_D_TAG;
24416dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
24426dfd23d3SVladimir Oltean 	vlan[match].vlan_bc |= BIT(port);
244373ceab83SVladimir Oltean 
244473ceab83SVladimir Oltean 	if (allowed_ingress)
24456dfd23d3SVladimir Oltean 		vlan[match].vmemb_port |= BIT(port);
244673ceab83SVladimir Oltean 	else
244773ceab83SVladimir Oltean 		vlan[match].vmemb_port &= ~BIT(port);
244873ceab83SVladimir Oltean 
24496dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
24506dfd23d3SVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
24516dfd23d3SVladimir Oltean 	else
24526dfd23d3SVladimir Oltean 		vlan[match].tag_port |= BIT(port);
24536dfd23d3SVladimir Oltean 
24546dfd23d3SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
24556dfd23d3SVladimir Oltean 					    &vlan[match], true);
24566dfd23d3SVladimir Oltean }
24576dfd23d3SVladimir Oltean 
24586dfd23d3SVladimir Oltean static int sja1105_vlan_del(struct sja1105_private *priv, int port, u16 vid)
24596dfd23d3SVladimir Oltean {
24606dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
24616dfd23d3SVladimir Oltean 	struct sja1105_table *table;
24626dfd23d3SVladimir Oltean 	bool keep = true;
24636dfd23d3SVladimir Oltean 	int match, rc;
24646dfd23d3SVladimir Oltean 
24656dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
24666dfd23d3SVladimir Oltean 
24676dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
24686dfd23d3SVladimir Oltean 	/* Can't delete a missing entry. */
24696dfd23d3SVladimir Oltean 	if (match < 0)
24705899ee36SVladimir Oltean 		return 0;
24715899ee36SVladimir Oltean 
24726dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
24736dfd23d3SVladimir Oltean 	vlan = table->entries;
24746dfd23d3SVladimir Oltean 
24756dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
24766dfd23d3SVladimir Oltean 	vlan[match].vlan_bc &= ~BIT(port);
24776dfd23d3SVladimir Oltean 	vlan[match].vmemb_port &= ~BIT(port);
24786dfd23d3SVladimir Oltean 	/* Also unset tag_port, just so we don't have a confusing bitmap
24796dfd23d3SVladimir Oltean 	 * (no practical purpose).
2480b38e659dSVladimir Oltean 	 */
24816dfd23d3SVladimir Oltean 	vlan[match].tag_port &= ~BIT(port);
2482b38e659dSVladimir Oltean 
24836dfd23d3SVladimir Oltean 	/* If there's no port left as member of this VLAN,
24846dfd23d3SVladimir Oltean 	 * it's time for it to go.
24856dfd23d3SVladimir Oltean 	 */
24866dfd23d3SVladimir Oltean 	if (!vlan[match].vmemb_port)
24876dfd23d3SVladimir Oltean 		keep = false;
24885899ee36SVladimir Oltean 
24896dfd23d3SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
24906dfd23d3SVladimir Oltean 					  &vlan[match], keep);
24916dfd23d3SVladimir Oltean 	if (rc < 0)
24926dfd23d3SVladimir Oltean 		return rc;
24935899ee36SVladimir Oltean 
24946dfd23d3SVladimir Oltean 	if (!keep)
24956dfd23d3SVladimir Oltean 		return sja1105_table_delete_entry(table, match);
24965899ee36SVladimir Oltean 
24975899ee36SVladimir Oltean 	return 0;
24985899ee36SVladimir Oltean }
24995899ee36SVladimir Oltean 
25006dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_add(struct dsa_switch *ds, int port,
250131046a5fSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan,
250231046a5fSVladimir Oltean 				   struct netlink_ext_ack *extack)
25036666cebcSVladimir Oltean {
25046666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2505884be12fSVladimir Oltean 	u16 flags = vlan->flags;
25066666cebcSVladimir Oltean 	int rc;
25076666cebcSVladimir Oltean 
25080fac6aa0SVladimir Oltean 	/* Be sure to deny alterations to the configuration done by tag_8021q.
25091958d581SVladimir Oltean 	 */
25100fac6aa0SVladimir Oltean 	if (vid_is_dsa_8021q(vlan->vid)) {
251131046a5fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
251231046a5fSVladimir Oltean 				   "Range 1024-3071 reserved for dsa_8021q operation");
25131958d581SVladimir Oltean 		return -EBUSY;
25141958d581SVladimir Oltean 	}
25151958d581SVladimir Oltean 
2516c5130029SVladimir Oltean 	/* Always install bridge VLANs as egress-tagged on CPU and DSA ports */
2517c5130029SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2518884be12fSVladimir Oltean 		flags = 0;
2519884be12fSVladimir Oltean 
252073ceab83SVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vlan->vid, flags, true);
25216dfd23d3SVladimir Oltean 	if (rc)
25221958d581SVladimir Oltean 		return rc;
2523ec5ae610SVladimir Oltean 
25246dfd23d3SVladimir Oltean 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
25256dfd23d3SVladimir Oltean 		priv->bridge_pvid[port] = vlan->vid;
2526ec5ae610SVladimir Oltean 
25276dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
25286666cebcSVladimir Oltean }
25296666cebcSVladimir Oltean 
25306dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port,
25316666cebcSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan)
25326666cebcSVladimir Oltean {
25336666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2534bef0746cSVladimir Oltean 	int rc;
25356666cebcSVladimir Oltean 
2536bef0746cSVladimir Oltean 	rc = sja1105_vlan_del(priv, port, vlan->vid);
2537bef0746cSVladimir Oltean 	if (rc)
2538bef0746cSVladimir Oltean 		return rc;
2539bef0746cSVladimir Oltean 
2540bef0746cSVladimir Oltean 	/* In case the pvid was deleted, make sure that untagged packets will
2541bef0746cSVladimir Oltean 	 * be dropped.
2542bef0746cSVladimir Oltean 	 */
2543bef0746cSVladimir Oltean 	return sja1105_commit_pvid(ds, port);
25446666cebcSVladimir Oltean }
25456666cebcSVladimir Oltean 
25465899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
25475899ee36SVladimir Oltean 				      u16 flags)
25485899ee36SVladimir Oltean {
25495899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
255073ceab83SVladimir Oltean 	bool allowed_ingress = true;
25515899ee36SVladimir Oltean 	int rc;
25525899ee36SVladimir Oltean 
255373ceab83SVladimir Oltean 	/* Prevent attackers from trying to inject a DSA tag from
255473ceab83SVladimir Oltean 	 * the outside world.
255573ceab83SVladimir Oltean 	 */
255673ceab83SVladimir Oltean 	if (dsa_is_user_port(ds, port))
255773ceab83SVladimir Oltean 		allowed_ingress = false;
255873ceab83SVladimir Oltean 
255973ceab83SVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vid, flags, allowed_ingress);
25606dfd23d3SVladimir Oltean 	if (rc)
25615899ee36SVladimir Oltean 		return rc;
25625899ee36SVladimir Oltean 
25636dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_PVID)
25646dfd23d3SVladimir Oltean 		priv->tag_8021q_pvid[port] = vid;
25656dfd23d3SVladimir Oltean 
25666dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
25675899ee36SVladimir Oltean }
25685899ee36SVladimir Oltean 
25695899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
25705899ee36SVladimir Oltean {
25715899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
25725899ee36SVladimir Oltean 
25736dfd23d3SVladimir Oltean 	return sja1105_vlan_del(priv, port, vid);
25745899ee36SVladimir Oltean }
25755899ee36SVladimir Oltean 
25764fbc08bdSVladimir Oltean static int sja1105_prechangeupper(struct dsa_switch *ds, int port,
25774fbc08bdSVladimir Oltean 				  struct netdev_notifier_changeupper_info *info)
25784fbc08bdSVladimir Oltean {
25794fbc08bdSVladimir Oltean 	struct netlink_ext_ack *extack = info->info.extack;
25804fbc08bdSVladimir Oltean 	struct net_device *upper = info->upper_dev;
258119fa937aSVladimir Oltean 	struct dsa_switch_tree *dst = ds->dst;
258219fa937aSVladimir Oltean 	struct dsa_port *dp;
25834fbc08bdSVladimir Oltean 
25844fbc08bdSVladimir Oltean 	if (is_vlan_dev(upper)) {
25854fbc08bdSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "8021q uppers are not supported");
25864fbc08bdSVladimir Oltean 		return -EBUSY;
25874fbc08bdSVladimir Oltean 	}
25884fbc08bdSVladimir Oltean 
258919fa937aSVladimir Oltean 	if (netif_is_bridge_master(upper)) {
259019fa937aSVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
259141fb0cf1SVladimir Oltean 			struct net_device *br = dsa_port_bridge_dev_get(dp);
259241fb0cf1SVladimir Oltean 
259341fb0cf1SVladimir Oltean 			if (br && br != upper && br_vlan_enabled(br)) {
259419fa937aSVladimir Oltean 				NL_SET_ERR_MSG_MOD(extack,
259519fa937aSVladimir Oltean 						   "Only one VLAN-aware bridge is supported");
259619fa937aSVladimir Oltean 				return -EBUSY;
259719fa937aSVladimir Oltean 			}
259819fa937aSVladimir Oltean 		}
259919fa937aSVladimir Oltean 	}
260019fa937aSVladimir Oltean 
26014fbc08bdSVladimir Oltean 	return 0;
26024fbc08bdSVladimir Oltean }
26034fbc08bdSVladimir Oltean 
2604227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
260547ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2606227d07a0SVladimir Oltean {
2607227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2608227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2609227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2610227d07a0SVladimir Oltean 	int timeout = 10;
2611227d07a0SVladimir Oltean 	int rc;
2612227d07a0SVladimir Oltean 
2613227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2614227d07a0SVladimir Oltean 
2615227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2616227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2617227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
261847ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
261947ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2620227d07a0SVladimir Oltean 
2621227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2622227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2623227d07a0SVladimir Oltean 	if (rc < 0) {
2624227d07a0SVladimir Oltean 		kfree_skb(skb);
2625227d07a0SVladimir Oltean 		return rc;
2626227d07a0SVladimir Oltean 	}
2627227d07a0SVladimir Oltean 
2628227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
262968bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2630227d07a0SVladimir Oltean 
2631227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2632227d07a0SVladimir Oltean 	do {
2633227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2634227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2635227d07a0SVladimir Oltean 		if (rc < 0) {
2636227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2637227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2638227d07a0SVladimir Oltean 			continue;
2639227d07a0SVladimir Oltean 		}
2640227d07a0SVladimir Oltean 
2641227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2642227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2643227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2644227d07a0SVladimir Oltean 		 */
2645227d07a0SVladimir Oltean 		cpu_relax();
2646227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2647227d07a0SVladimir Oltean 
2648227d07a0SVladimir Oltean 	if (!timeout) {
2649227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2650227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
26512a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
26522a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2653227d07a0SVladimir Oltean 		 */
2654227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2655227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2656227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2657227d07a0SVladimir Oltean 	}
2658227d07a0SVladimir Oltean 
2659227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2660227d07a0SVladimir Oltean }
2661227d07a0SVladimir Oltean 
2662d38049bbSVladimir Oltean #define work_to_xmit_work(w) \
2663d38049bbSVladimir Oltean 		container_of((w), struct sja1105_deferred_xmit_work, work)
2664a68578c2SVladimir Oltean 
2665227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2666227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2667227d07a0SVladimir Oltean  * lock on the bus)
2668227d07a0SVladimir Oltean  */
2669a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2670227d07a0SVladimir Oltean {
2671d38049bbSVladimir Oltean 	struct sja1105_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
2672d38049bbSVladimir Oltean 	struct sk_buff *clone, *skb = xmit_work->skb;
2673d38049bbSVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
2674d38049bbSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2675d38049bbSVladimir Oltean 	int port = xmit_work->dp->index;
2676a68578c2SVladimir Oltean 
2677d38049bbSVladimir Oltean 	clone = SJA1105_SKB_CB(skb)->clone;
2678227d07a0SVladimir Oltean 
2679227d07a0SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
2680227d07a0SVladimir Oltean 
2681d38049bbSVladimir Oltean 	sja1105_mgmt_xmit(ds, port, 0, skb, !!clone);
2682a68578c2SVladimir Oltean 
268347ed985eSVladimir Oltean 	/* The clone, if there, was made by dsa_skb_tx_timestamp */
2684a68578c2SVladimir Oltean 	if (clone)
2685d38049bbSVladimir Oltean 		sja1105_ptp_txtstamp_skb(ds, port, clone);
2686227d07a0SVladimir Oltean 
2687227d07a0SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
2688d38049bbSVladimir Oltean 
2689d38049bbSVladimir Oltean 	kfree(xmit_work);
26908aa9ebccSVladimir Oltean }
26918aa9ebccSVladimir Oltean 
2692c79e8486SVladimir Oltean static int sja1105_connect_tag_protocol(struct dsa_switch *ds,
2693c79e8486SVladimir Oltean 					enum dsa_tag_protocol proto)
2694c79e8486SVladimir Oltean {
2695c8a2a011SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2696c79e8486SVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
2697c79e8486SVladimir Oltean 
2698c8a2a011SVladimir Oltean 	if (proto != priv->info->tag_proto)
2699c8a2a011SVladimir Oltean 		return -EPROTONOSUPPORT;
2700c8a2a011SVladimir Oltean 
2701c79e8486SVladimir Oltean 	tagger_data = sja1105_tagger_data(ds);
2702c79e8486SVladimir Oltean 	tagger_data->xmit_work_fn = sja1105_port_deferred_xmit;
2703fcbf979aSVladimir Oltean 	tagger_data->meta_tstamp_handler = sja1110_process_meta_tstamp;
2704c8a2a011SVladimir Oltean 
2705c79e8486SVladimir Oltean 	return 0;
2706c79e8486SVladimir Oltean }
2707c79e8486SVladimir Oltean 
27088456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
27098456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
27108456721dSVladimir Oltean  */
27118456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
27128456721dSVladimir Oltean 				   unsigned int ageing_time)
27138456721dSVladimir Oltean {
27148456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
27158456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
27168456721dSVladimir Oltean 	struct sja1105_table *table;
27178456721dSVladimir Oltean 	unsigned int maxage;
27188456721dSVladimir Oltean 
27198456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
27208456721dSVladimir Oltean 	l2_lookup_params = table->entries;
27218456721dSVladimir Oltean 
27228456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
27238456721dSVladimir Oltean 
27248456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
27258456721dSVladimir Oltean 		return 0;
27268456721dSVladimir Oltean 
27278456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
27288456721dSVladimir Oltean 
27292eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
27308456721dSVladimir Oltean }
27318456721dSVladimir Oltean 
2732c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
2733c279c726SVladimir Oltean {
2734c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2735c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2736c279c726SVladimir Oltean 
2737c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
2738c279c726SVladimir Oltean 
2739777e55e3SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2740c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
2741c279c726SVladimir Oltean 
2742c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2743c279c726SVladimir Oltean 
2744a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
2745c279c726SVladimir Oltean 		return 0;
2746c279c726SVladimir Oltean 
2747a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
2748c279c726SVladimir Oltean 
2749c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2750c279c726SVladimir Oltean }
2751c279c726SVladimir Oltean 
2752c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
2753c279c726SVladimir Oltean {
2754c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
2755c279c726SVladimir Oltean }
2756c279c726SVladimir Oltean 
2757317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2758317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2759317ab5b8SVladimir Oltean 				 void *type_data)
2760317ab5b8SVladimir Oltean {
2761317ab5b8SVladimir Oltean 	switch (type) {
2762317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2763317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
27644d752508SVladimir Oltean 	case TC_SETUP_QDISC_CBS:
27654d752508SVladimir Oltean 		return sja1105_setup_tc_cbs(ds, port, type_data);
2766317ab5b8SVladimir Oltean 	default:
2767317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2768317ab5b8SVladimir Oltean 	}
2769317ab5b8SVladimir Oltean }
2770317ab5b8SVladimir Oltean 
2771511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2772511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2773511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2774511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2775511e6ca0SVladimir Oltean  * mirroring rule that references it.
2776511e6ca0SVladimir Oltean  */
2777511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2778511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2779511e6ca0SVladimir Oltean {
2780511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2781511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2782542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2783511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2784511e6ca0SVladimir Oltean 	bool already_enabled;
2785511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2786511e6ca0SVladimir Oltean 	int rc;
2787511e6ca0SVladimir Oltean 
2788511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2789511e6ca0SVladimir Oltean 	general_params = table->entries;
2790511e6ca0SVladimir Oltean 
2791511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2792511e6ca0SVladimir Oltean 
2793542043e9SVladimir Oltean 	already_enabled = (general_params->mirr_port != ds->num_ports);
2794511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2795511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2796511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2797511e6ca0SVladimir Oltean 			general_params->mirr_port);
2798511e6ca0SVladimir Oltean 		return -EBUSY;
2799511e6ca0SVladimir Oltean 	}
2800511e6ca0SVladimir Oltean 
2801511e6ca0SVladimir Oltean 	new_mirr_port = to;
2802511e6ca0SVladimir Oltean 	if (!enabled) {
2803511e6ca0SVladimir Oltean 		bool keep = false;
2804511e6ca0SVladimir Oltean 		int port;
2805511e6ca0SVladimir Oltean 
2806511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2807542043e9SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
2808511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2809511e6ca0SVladimir Oltean 				keep = true;
2810511e6ca0SVladimir Oltean 				break;
2811511e6ca0SVladimir Oltean 			}
2812511e6ca0SVladimir Oltean 		}
2813511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2814511e6ca0SVladimir Oltean 		if (!keep)
2815542043e9SVladimir Oltean 			new_mirr_port = ds->num_ports;
2816511e6ca0SVladimir Oltean 	}
2817511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2818511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2819511e6ca0SVladimir Oltean 
2820511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2821511e6ca0SVladimir Oltean 						  0, general_params, true);
2822511e6ca0SVladimir Oltean 		if (rc < 0)
2823511e6ca0SVladimir Oltean 			return rc;
2824511e6ca0SVladimir Oltean 	}
2825511e6ca0SVladimir Oltean 
2826511e6ca0SVladimir Oltean 	if (ingress)
2827511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2828511e6ca0SVladimir Oltean 	else
2829511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2830511e6ca0SVladimir Oltean 
2831511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2832511e6ca0SVladimir Oltean 					    &mac[from], true);
2833511e6ca0SVladimir Oltean }
2834511e6ca0SVladimir Oltean 
2835511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2836511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2837511e6ca0SVladimir Oltean 			      bool ingress)
2838511e6ca0SVladimir Oltean {
2839511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2840511e6ca0SVladimir Oltean 				    ingress, true);
2841511e6ca0SVladimir Oltean }
2842511e6ca0SVladimir Oltean 
2843511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2844511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2845511e6ca0SVladimir Oltean {
2846511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2847511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2848511e6ca0SVladimir Oltean }
2849511e6ca0SVladimir Oltean 
2850a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
2851a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
2852a7cc081cSVladimir Oltean {
2853a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2854a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2855a7cc081cSVladimir Oltean 
2856a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2857a7cc081cSVladimir Oltean 
2858a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
2859a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
2860a7cc081cSVladimir Oltean 	 * bytes.
2861a7cc081cSVladimir Oltean 	 */
2862a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
2863a7cc081cSVladimir Oltean 				      1000000);
28645f035af7SPo Liu 	policing[port].smax = policer->burst;
2865a7cc081cSVladimir Oltean 
2866a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2867a7cc081cSVladimir Oltean }
2868a7cc081cSVladimir Oltean 
2869a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
2870a7cc081cSVladimir Oltean {
2871a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2872a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2873a7cc081cSVladimir Oltean 
2874a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2875a7cc081cSVladimir Oltean 
2876a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
2877a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
2878a7cc081cSVladimir Oltean 
2879a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2880a7cc081cSVladimir Oltean }
2881a7cc081cSVladimir Oltean 
28824d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port,
28834d942354SVladimir Oltean 				     bool enabled)
28844d942354SVladimir Oltean {
28854d942354SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
28864d942354SVladimir Oltean 
28874d942354SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
28884d942354SVladimir Oltean 
28894c44fc5eSVladimir Oltean 	mac[port].dyn_learn = enabled;
28904d942354SVladimir Oltean 
28915313a37bSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
28924d942354SVladimir Oltean 					    &mac[port], true);
28934d942354SVladimir Oltean }
28944d942354SVladimir Oltean 
28954d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to,
28964d942354SVladimir Oltean 					  struct switchdev_brport_flags flags)
28974d942354SVladimir Oltean {
28984d942354SVladimir Oltean 	if (flags.mask & BR_FLOOD) {
28994d942354SVladimir Oltean 		if (flags.val & BR_FLOOD)
29007f7ccdeaSVladimir Oltean 			priv->ucast_egress_floods |= BIT(to);
29014d942354SVladimir Oltean 		else
29026a5166e0SVladimir Oltean 			priv->ucast_egress_floods &= ~BIT(to);
29034d942354SVladimir Oltean 	}
29047f7ccdeaSVladimir Oltean 
29054d942354SVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD) {
29064d942354SVladimir Oltean 		if (flags.val & BR_BCAST_FLOOD)
29077f7ccdeaSVladimir Oltean 			priv->bcast_egress_floods |= BIT(to);
29084d942354SVladimir Oltean 		else
29096a5166e0SVladimir Oltean 			priv->bcast_egress_floods &= ~BIT(to);
29104d942354SVladimir Oltean 	}
29114d942354SVladimir Oltean 
29127f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
29134d942354SVladimir Oltean }
29144d942354SVladimir Oltean 
29154d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to,
29164d942354SVladimir Oltean 				    struct switchdev_brport_flags flags,
29174d942354SVladimir Oltean 				    struct netlink_ext_ack *extack)
29184d942354SVladimir Oltean {
29194d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
29204d942354SVladimir Oltean 	struct sja1105_table *table;
29214d942354SVladimir Oltean 	int match;
29224d942354SVladimir Oltean 
29234d942354SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
29244d942354SVladimir Oltean 	l2_lookup = table->entries;
29254d942354SVladimir Oltean 
29264d942354SVladimir Oltean 	for (match = 0; match < table->entry_count; match++)
29274d942354SVladimir Oltean 		if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST &&
29284d942354SVladimir Oltean 		    l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
29294d942354SVladimir Oltean 			break;
29304d942354SVladimir Oltean 
29314d942354SVladimir Oltean 	if (match == table->entry_count) {
29324d942354SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
29334d942354SVladimir Oltean 				   "Could not find FDB entry for unknown multicast");
29344d942354SVladimir Oltean 		return -ENOSPC;
29354d942354SVladimir Oltean 	}
29364d942354SVladimir Oltean 
29374d942354SVladimir Oltean 	if (flags.val & BR_MCAST_FLOOD)
29384d942354SVladimir Oltean 		l2_lookup[match].destports |= BIT(to);
29394d942354SVladimir Oltean 	else
29404d942354SVladimir Oltean 		l2_lookup[match].destports &= ~BIT(to);
29414d942354SVladimir Oltean 
29424d942354SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
29434d942354SVladimir Oltean 					    l2_lookup[match].index,
29444d942354SVladimir Oltean 					    &l2_lookup[match],
29454d942354SVladimir Oltean 					    true);
29464d942354SVladimir Oltean }
29474d942354SVladimir Oltean 
29484d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port,
29494d942354SVladimir Oltean 					 struct switchdev_brport_flags flags,
29504d942354SVladimir Oltean 					 struct netlink_ext_ack *extack)
29514d942354SVladimir Oltean {
29524d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29534d942354SVladimir Oltean 
29544d942354SVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
29554d942354SVladimir Oltean 			   BR_BCAST_FLOOD))
29564d942354SVladimir Oltean 		return -EINVAL;
29574d942354SVladimir Oltean 
29584d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) &&
29594d942354SVladimir Oltean 	    !priv->info->can_limit_mcast_flood) {
29604d942354SVladimir Oltean 		bool multicast = !!(flags.val & BR_MCAST_FLOOD);
29614d942354SVladimir Oltean 		bool unicast = !!(flags.val & BR_FLOOD);
29624d942354SVladimir Oltean 
29634d942354SVladimir Oltean 		if (unicast != multicast) {
29644d942354SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
29654d942354SVladimir Oltean 					   "This chip cannot configure multicast flooding independently of unicast");
29664d942354SVladimir Oltean 			return -EINVAL;
29674d942354SVladimir Oltean 		}
29684d942354SVladimir Oltean 	}
29694d942354SVladimir Oltean 
29704d942354SVladimir Oltean 	return 0;
29714d942354SVladimir Oltean }
29724d942354SVladimir Oltean 
29734d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port,
29744d942354SVladimir Oltean 				     struct switchdev_brport_flags flags,
29754d942354SVladimir Oltean 				     struct netlink_ext_ack *extack)
29764d942354SVladimir Oltean {
29774d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
29784d942354SVladimir Oltean 	int rc;
29794d942354SVladimir Oltean 
29804d942354SVladimir Oltean 	if (flags.mask & BR_LEARNING) {
29814d942354SVladimir Oltean 		bool learn_ena = !!(flags.val & BR_LEARNING);
29824d942354SVladimir Oltean 
29834d942354SVladimir Oltean 		rc = sja1105_port_set_learning(priv, port, learn_ena);
29844d942354SVladimir Oltean 		if (rc)
29854d942354SVladimir Oltean 			return rc;
29864d942354SVladimir Oltean 	}
29874d942354SVladimir Oltean 
29884d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) {
29894d942354SVladimir Oltean 		rc = sja1105_port_ucast_bcast_flood(priv, port, flags);
29904d942354SVladimir Oltean 		if (rc)
29914d942354SVladimir Oltean 			return rc;
29924d942354SVladimir Oltean 	}
29934d942354SVladimir Oltean 
29944d942354SVladimir Oltean 	/* For chips that can't offload BR_MCAST_FLOOD independently, there
29954d942354SVladimir Oltean 	 * is nothing to do here, we ensured the configuration is in sync by
29964d942354SVladimir Oltean 	 * offloading BR_FLOOD.
29974d942354SVladimir Oltean 	 */
29984d942354SVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) {
29994d942354SVladimir Oltean 		rc = sja1105_port_mcast_flood(priv, port, flags,
30004d942354SVladimir Oltean 					      extack);
30014d942354SVladimir Oltean 		if (rc)
30024d942354SVladimir Oltean 			return rc;
30034d942354SVladimir Oltean 	}
30044d942354SVladimir Oltean 
30054d942354SVladimir Oltean 	return 0;
30064d942354SVladimir Oltean }
30074d942354SVladimir Oltean 
3008022522acSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
3009022522acSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
3010022522acSVladimir Oltean  * but not the xMII mode parameters table.
3011022522acSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
3012022522acSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
3013022522acSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
3014022522acSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
3015022522acSVladimir Oltean  * Setting correct PHY link speed does not matter now.
3016022522acSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
3017022522acSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
3018022522acSVladimir Oltean  * can populate the xMII mode parameters table.
3019022522acSVladimir Oltean  */
3020022522acSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
3021022522acSVladimir Oltean {
3022022522acSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3023022522acSVladimir Oltean 	int rc;
3024022522acSVladimir Oltean 
3025022522acSVladimir Oltean 	if (priv->info->disable_microcontroller) {
3026022522acSVladimir Oltean 		rc = priv->info->disable_microcontroller(priv);
3027022522acSVladimir Oltean 		if (rc < 0) {
3028022522acSVladimir Oltean 			dev_err(ds->dev,
3029022522acSVladimir Oltean 				"Failed to disable microcontroller: %pe\n",
3030022522acSVladimir Oltean 				ERR_PTR(rc));
3031022522acSVladimir Oltean 			return rc;
3032022522acSVladimir Oltean 		}
3033022522acSVladimir Oltean 	}
3034022522acSVladimir Oltean 
3035022522acSVladimir Oltean 	/* Create and send configuration down to device */
3036022522acSVladimir Oltean 	rc = sja1105_static_config_load(priv);
3037022522acSVladimir Oltean 	if (rc < 0) {
3038022522acSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
3039022522acSVladimir Oltean 		return rc;
3040022522acSVladimir Oltean 	}
3041022522acSVladimir Oltean 
3042022522acSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
3043022522acSVladimir Oltean 	if (priv->info->clocking_setup) {
3044022522acSVladimir Oltean 		rc = priv->info->clocking_setup(priv);
3045022522acSVladimir Oltean 		if (rc < 0) {
3046022522acSVladimir Oltean 			dev_err(ds->dev,
3047022522acSVladimir Oltean 				"Failed to configure MII clocking: %pe\n",
3048022522acSVladimir Oltean 				ERR_PTR(rc));
3049022522acSVladimir Oltean 			goto out_static_config_free;
3050022522acSVladimir Oltean 		}
3051022522acSVladimir Oltean 	}
3052022522acSVladimir Oltean 
3053022522acSVladimir Oltean 	sja1105_tas_setup(ds);
3054022522acSVladimir Oltean 	sja1105_flower_setup(ds);
3055022522acSVladimir Oltean 
3056022522acSVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
3057022522acSVladimir Oltean 	if (rc < 0) {
3058022522acSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
3059022522acSVladimir Oltean 		goto out_flower_teardown;
3060022522acSVladimir Oltean 	}
3061022522acSVladimir Oltean 
3062022522acSVladimir Oltean 	rc = sja1105_mdiobus_register(ds);
3063022522acSVladimir Oltean 	if (rc < 0) {
3064022522acSVladimir Oltean 		dev_err(ds->dev, "Failed to register MDIO bus: %pe\n",
3065022522acSVladimir Oltean 			ERR_PTR(rc));
3066022522acSVladimir Oltean 		goto out_ptp_clock_unregister;
3067022522acSVladimir Oltean 	}
3068022522acSVladimir Oltean 
3069022522acSVladimir Oltean 	rc = sja1105_devlink_setup(ds);
3070022522acSVladimir Oltean 	if (rc < 0)
3071022522acSVladimir Oltean 		goto out_mdiobus_unregister;
3072022522acSVladimir Oltean 
3073022522acSVladimir Oltean 	rtnl_lock();
3074022522acSVladimir Oltean 	rc = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q));
3075022522acSVladimir Oltean 	rtnl_unlock();
3076022522acSVladimir Oltean 	if (rc)
3077022522acSVladimir Oltean 		goto out_devlink_teardown;
3078022522acSVladimir Oltean 
3079022522acSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
3080022522acSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
3081022522acSVladimir Oltean 	 * EtherType is.
3082022522acSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
3083022522acSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
3084022522acSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
3085022522acSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
3086022522acSVladimir Oltean 	 */
3087022522acSVladimir Oltean 	ds->vlan_filtering_is_global = true;
3088022522acSVladimir Oltean 	ds->untag_bridge_pvid = true;
3089022522acSVladimir Oltean 	/* tag_8021q has 3 bits for the VBID, and the value 0 is reserved */
3090947c8746SVladimir Oltean 	ds->max_num_bridges = 7;
3091022522acSVladimir Oltean 
3092022522acSVladimir Oltean 	/* Advertise the 8 egress queues */
3093022522acSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
3094022522acSVladimir Oltean 
3095022522acSVladimir Oltean 	ds->mtu_enforcement_ingress = true;
3096022522acSVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
3097022522acSVladimir Oltean 
3098022522acSVladimir Oltean 	return 0;
3099022522acSVladimir Oltean 
3100022522acSVladimir Oltean out_devlink_teardown:
3101022522acSVladimir Oltean 	sja1105_devlink_teardown(ds);
3102022522acSVladimir Oltean out_mdiobus_unregister:
3103022522acSVladimir Oltean 	sja1105_mdiobus_unregister(ds);
3104022522acSVladimir Oltean out_ptp_clock_unregister:
3105022522acSVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
3106022522acSVladimir Oltean out_flower_teardown:
3107022522acSVladimir Oltean 	sja1105_flower_teardown(ds);
3108022522acSVladimir Oltean 	sja1105_tas_teardown(ds);
3109022522acSVladimir Oltean out_static_config_free:
3110022522acSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
3111022522acSVladimir Oltean 
3112022522acSVladimir Oltean 	return rc;
3113022522acSVladimir Oltean }
3114022522acSVladimir Oltean 
3115022522acSVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
3116022522acSVladimir Oltean {
3117022522acSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3118022522acSVladimir Oltean 
3119022522acSVladimir Oltean 	rtnl_lock();
3120022522acSVladimir Oltean 	dsa_tag_8021q_unregister(ds);
3121022522acSVladimir Oltean 	rtnl_unlock();
3122022522acSVladimir Oltean 
3123022522acSVladimir Oltean 	sja1105_devlink_teardown(ds);
3124022522acSVladimir Oltean 	sja1105_mdiobus_unregister(ds);
3125022522acSVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
3126022522acSVladimir Oltean 	sja1105_flower_teardown(ds);
3127022522acSVladimir Oltean 	sja1105_tas_teardown(ds);
3128022522acSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
3129022522acSVladimir Oltean }
3130022522acSVladimir Oltean 
3131f5aef424SVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
31328aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
3133c79e8486SVladimir Oltean 	.connect_tag_protocol	= sja1105_connect_tag_protocol,
31348aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
3135f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
31368456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
3137c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
3138c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
3139a420b757SRussell King (Oracle) 	.phylink_get_caps	= sja1105_phylink_get_caps,
3140827b4ef2SRussell King (Oracle) 	.phylink_mac_select_pcs	= sja1105_mac_select_pcs,
31418400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
31428400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
314352c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
314452c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
314552c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
3146bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
3147291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
3148291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
3149291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
31505126ec72SVladimir Oltean 	.port_fast_age		= sja1105_fast_age,
31518aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
31528aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
31534d942354SVladimir Oltean 	.port_pre_bridge_flags	= sja1105_port_pre_bridge_flags,
31544d942354SVladimir Oltean 	.port_bridge_flags	= sja1105_port_bridge_flags,
3155640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
31566666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
31576dfd23d3SVladimir Oltean 	.port_vlan_add		= sja1105_bridge_vlan_add,
31586dfd23d3SVladimir Oltean 	.port_vlan_del		= sja1105_bridge_vlan_del,
3159291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
3160291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
3161a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
3162a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
3163f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
316447ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
3165317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
3166511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
3167511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
3168a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
3169a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
3170a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
3171a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
3172834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
3173ff4cf8eaSVladimir Oltean 	.devlink_info_get	= sja1105_devlink_info_get,
31745da11eb4SVladimir Oltean 	.tag_8021q_vlan_add	= sja1105_dsa_8021q_vlan_add,
31755da11eb4SVladimir Oltean 	.tag_8021q_vlan_del	= sja1105_dsa_8021q_vlan_del,
31764fbc08bdSVladimir Oltean 	.port_prechangeupper	= sja1105_prechangeupper,
31778aa9ebccSVladimir Oltean };
31788aa9ebccSVladimir Oltean 
31790b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[];
31800b0e2997SVladimir Oltean 
31818aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
31828aa9ebccSVladimir Oltean {
31838aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
31848aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
31858aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
31860b0e2997SVladimir Oltean 	const struct of_device_id *match;
3187dff79620SVladimir Oltean 	u32 device_id;
31888aa9ebccSVladimir Oltean 	u64 part_no;
31898aa9ebccSVladimir Oltean 	int rc;
31908aa9ebccSVladimir Oltean 
319134d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
319234d76e9fSVladimir Oltean 			      NULL);
31938aa9ebccSVladimir Oltean 	if (rc < 0)
31948aa9ebccSVladimir Oltean 		return rc;
31958aa9ebccSVladimir Oltean 
31961bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
31971bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
31988aa9ebccSVladimir Oltean 	if (rc < 0)
31998aa9ebccSVladimir Oltean 		return rc;
32008aa9ebccSVladimir Oltean 
32018aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
32028aa9ebccSVladimir Oltean 
32035978fac0SNathan Chancellor 	for (match = sja1105_dt_ids; match->compatible[0]; match++) {
32040b0e2997SVladimir Oltean 		const struct sja1105_info *info = match->data;
32050b0e2997SVladimir Oltean 
32060b0e2997SVladimir Oltean 		/* Is what's been probed in our match table at all? */
32070b0e2997SVladimir Oltean 		if (info->device_id != device_id || info->part_no != part_no)
32080b0e2997SVladimir Oltean 			continue;
32090b0e2997SVladimir Oltean 
32100b0e2997SVladimir Oltean 		/* But is it what's in the device tree? */
32110b0e2997SVladimir Oltean 		if (priv->info->device_id != device_id ||
32120b0e2997SVladimir Oltean 		    priv->info->part_no != part_no) {
32130b0e2997SVladimir Oltean 			dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
32140b0e2997SVladimir Oltean 				 priv->info->name, info->name);
32150b0e2997SVladimir Oltean 			/* It isn't. No problem, pick that up. */
32160b0e2997SVladimir Oltean 			priv->info = info;
32178aa9ebccSVladimir Oltean 		}
32188aa9ebccSVladimir Oltean 
32198aa9ebccSVladimir Oltean 		return 0;
32208aa9ebccSVladimir Oltean 	}
32218aa9ebccSVladimir Oltean 
32220b0e2997SVladimir Oltean 	dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
32230b0e2997SVladimir Oltean 		device_id, part_no);
32240b0e2997SVladimir Oltean 
32250b0e2997SVladimir Oltean 	return -ENODEV;
32260b0e2997SVladimir Oltean }
32270b0e2997SVladimir Oltean 
32288aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
32298aa9ebccSVladimir Oltean {
32308aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
32318aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
3232718bad0eSVladimir Oltean 	size_t max_xfer, max_msg;
32338aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
3234022522acSVladimir Oltean 	int rc;
32358aa9ebccSVladimir Oltean 
32368aa9ebccSVladimir Oltean 	if (!dev->of_node) {
32378aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
32388aa9ebccSVladimir Oltean 		return -EINVAL;
32398aa9ebccSVladimir Oltean 	}
32408aa9ebccSVladimir Oltean 
324133e1501fSVladimir Oltean 	rc = sja1105_hw_reset(dev, 1, 1);
324233e1501fSVladimir Oltean 	if (rc)
324333e1501fSVladimir Oltean 		return rc;
324433e1501fSVladimir Oltean 
32458aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
32468aa9ebccSVladimir Oltean 	if (!priv)
32478aa9ebccSVladimir Oltean 		return -ENOMEM;
32488aa9ebccSVladimir Oltean 
32498aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
32508aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
32518aa9ebccSVladimir Oltean 	 */
32528aa9ebccSVladimir Oltean 	priv->spidev = spi;
32538aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
32548aa9ebccSVladimir Oltean 
32558aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
32568aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
32578aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
32588aa9ebccSVladimir Oltean 	if (rc < 0) {
32598aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
32608aa9ebccSVladimir Oltean 		return rc;
32618aa9ebccSVladimir Oltean 	}
32628aa9ebccSVladimir Oltean 
3263718bad0eSVladimir Oltean 	/* In sja1105_xfer, we send spi_messages composed of two spi_transfers:
3264718bad0eSVladimir Oltean 	 * a small one for the message header and another one for the current
3265718bad0eSVladimir Oltean 	 * chunk of the packed buffer.
3266718bad0eSVladimir Oltean 	 * Check that the restrictions imposed by the SPI controller are
3267718bad0eSVladimir Oltean 	 * respected: the chunk buffer is smaller than the max transfer size,
3268718bad0eSVladimir Oltean 	 * and the total length of the chunk plus its message header is smaller
3269718bad0eSVladimir Oltean 	 * than the max message size.
3270718bad0eSVladimir Oltean 	 * We do that during probe time since the maximum transfer size is a
3271718bad0eSVladimir Oltean 	 * runtime invariant.
3272718bad0eSVladimir Oltean 	 */
3273718bad0eSVladimir Oltean 	max_xfer = spi_max_transfer_size(spi);
3274718bad0eSVladimir Oltean 	max_msg = spi_max_message_size(spi);
3275718bad0eSVladimir Oltean 
3276718bad0eSVladimir Oltean 	/* We need to send at least one 64-bit word of SPI payload per message
3277718bad0eSVladimir Oltean 	 * in order to be able to make useful progress.
3278718bad0eSVladimir Oltean 	 */
3279718bad0eSVladimir Oltean 	if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) {
3280718bad0eSVladimir Oltean 		dev_err(dev, "SPI master cannot send large enough buffers, aborting\n");
3281718bad0eSVladimir Oltean 		return -EINVAL;
3282718bad0eSVladimir Oltean 	}
3283718bad0eSVladimir Oltean 
3284718bad0eSVladimir Oltean 	priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN;
3285718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_xfer)
3286718bad0eSVladimir Oltean 		priv->max_xfer_len = max_xfer;
3287718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER)
3288718bad0eSVladimir Oltean 		priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER;
3289718bad0eSVladimir Oltean 
32908aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
32918aa9ebccSVladimir Oltean 
32928aa9ebccSVladimir Oltean 	/* Detect hardware device */
32938aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
32948aa9ebccSVladimir Oltean 	if (rc < 0) {
32958aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
32968aa9ebccSVladimir Oltean 		return rc;
32978aa9ebccSVladimir Oltean 	}
32988aa9ebccSVladimir Oltean 
32998aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
33008aa9ebccSVladimir Oltean 
33017e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
33028aa9ebccSVladimir Oltean 	if (!ds)
33038aa9ebccSVladimir Oltean 		return -ENOMEM;
33048aa9ebccSVladimir Oltean 
33057e99e347SVivien Didelot 	ds->dev = dev;
33063e77e59bSVladimir Oltean 	ds->num_ports = priv->info->num_ports;
33078aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
33088aa9ebccSVladimir Oltean 	ds->priv = priv;
33098aa9ebccSVladimir Oltean 	priv->ds = ds;
33108aa9ebccSVladimir Oltean 
3311d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
3312eb016afdSVladimir Oltean 	mutex_init(&priv->dynamic_config_lock);
3313d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
331422ee9f8eSVladimir Oltean 	spin_lock_init(&priv->ts_id_lock);
3315d5a619bfSVivien Didelot 
3316022522acSVladimir Oltean 	rc = sja1105_parse_dt(priv);
3317022522acSVladimir Oltean 	if (rc < 0) {
3318022522acSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
3319328621f6SVladimir Oltean 		return rc;
3320022522acSVladimir Oltean 	}
3321022522acSVladimir Oltean 
33224d752508SVladimir Oltean 	if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
33234d752508SVladimir Oltean 		priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
33244d752508SVladimir Oltean 					 sizeof(struct sja1105_cbs_entry),
33254d752508SVladimir Oltean 					 GFP_KERNEL);
3326022522acSVladimir Oltean 		if (!priv->cbs)
3327022522acSVladimir Oltean 			return -ENOMEM;
33284d752508SVladimir Oltean 	}
33294d752508SVladimir Oltean 
3330022522acSVladimir Oltean 	return dsa_register_switch(priv->ds);
33318aa9ebccSVladimir Oltean }
33328aa9ebccSVladimir Oltean 
33338aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
33348aa9ebccSVladimir Oltean {
33358aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
33368aa9ebccSVladimir Oltean 
33370650bf52SVladimir Oltean 	if (!priv)
33380650bf52SVladimir Oltean 		return 0;
33390650bf52SVladimir Oltean 
33400650bf52SVladimir Oltean 	dsa_unregister_switch(priv->ds);
33410650bf52SVladimir Oltean 
33420650bf52SVladimir Oltean 	spi_set_drvdata(spi, NULL);
3343cedf4670SVladimir Oltean 
33448aa9ebccSVladimir Oltean 	return 0;
33458aa9ebccSVladimir Oltean }
33468aa9ebccSVladimir Oltean 
33470650bf52SVladimir Oltean static void sja1105_shutdown(struct spi_device *spi)
33480650bf52SVladimir Oltean {
33490650bf52SVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
33500650bf52SVladimir Oltean 
33510650bf52SVladimir Oltean 	if (!priv)
33520650bf52SVladimir Oltean 		return;
33530650bf52SVladimir Oltean 
33540650bf52SVladimir Oltean 	dsa_switch_shutdown(priv->ds);
33550650bf52SVladimir Oltean 
33560650bf52SVladimir Oltean 	spi_set_drvdata(spi, NULL);
33570650bf52SVladimir Oltean }
33580650bf52SVladimir Oltean 
33598aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
33608aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
33618aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
33628aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
33638aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
33648aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
33658aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
33663e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110a", .data = &sja1110a_info },
33673e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110b", .data = &sja1110b_info },
33683e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110c", .data = &sja1110c_info },
33693e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110d", .data = &sja1110d_info },
33708aa9ebccSVladimir Oltean 	{ /* sentinel */ },
33718aa9ebccSVladimir Oltean };
33728aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
33738aa9ebccSVladimir Oltean 
33748aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
33758aa9ebccSVladimir Oltean 	.driver = {
33768aa9ebccSVladimir Oltean 		.name  = "sja1105",
33778aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
33788aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
33798aa9ebccSVladimir Oltean 	},
33808aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
33818aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
33820650bf52SVladimir Oltean 	.shutdown = sja1105_shutdown,
33838aa9ebccSVladimir Oltean };
33848aa9ebccSVladimir Oltean 
33858aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
33868aa9ebccSVladimir Oltean 
33878aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
33888aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
33898aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
33908aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
3391