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>
183ad1d171SVladimir Oltean #include <linux/pcs/pcs-xpcs.h>
198aa9ebccSVladimir Oltean #include <linux/netdev_features.h>
208aa9ebccSVladimir Oltean #include <linux/netdevice.h>
218aa9ebccSVladimir Oltean #include <linux/if_bridge.h>
228aa9ebccSVladimir Oltean #include <linux/if_ether.h>
23227d07a0SVladimir Oltean #include <linux/dsa/8021q.h>
248aa9ebccSVladimir Oltean #include "sja1105.h"
25317ab5b8SVladimir Oltean #include "sja1105_tas.h"
268aa9ebccSVladimir Oltean 
274d942354SVladimir Oltean #define SJA1105_UNKNOWN_MULTICAST	0x010000000000ull
284d942354SVladimir Oltean 
2933e1501fSVladimir Oltean /* Configure the optional reset pin and bring up switch */
sja1105_hw_reset(struct device * dev,unsigned int pulse_len,unsigned int startup_delay)3033e1501fSVladimir Oltean static int sja1105_hw_reset(struct device *dev, unsigned int pulse_len,
318aa9ebccSVladimir Oltean 			    unsigned int startup_delay)
328aa9ebccSVladimir Oltean {
3333e1501fSVladimir Oltean 	struct gpio_desc *gpio;
3433e1501fSVladimir Oltean 
3533e1501fSVladimir Oltean 	gpio = gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
3633e1501fSVladimir Oltean 	if (IS_ERR(gpio))
3733e1501fSVladimir Oltean 		return PTR_ERR(gpio);
3833e1501fSVladimir Oltean 
3933e1501fSVladimir Oltean 	if (!gpio)
4033e1501fSVladimir Oltean 		return 0;
4133e1501fSVladimir Oltean 
428aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
438aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
448aa9ebccSVladimir Oltean 	msleep(pulse_len);
458aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
468aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
478aa9ebccSVladimir Oltean 	msleep(startup_delay);
4833e1501fSVladimir Oltean 
4933e1501fSVladimir Oltean 	gpiod_put(gpio);
5033e1501fSVladimir Oltean 
5133e1501fSVladimir Oltean 	return 0;
528aa9ebccSVladimir Oltean }
538aa9ebccSVladimir Oltean 
548aa9ebccSVladimir Oltean static void
sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry * l2_fwd,int from,int to,bool allow)558aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
568aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
578aa9ebccSVladimir Oltean {
584d942354SVladimir Oltean 	if (allow)
598aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
604d942354SVladimir Oltean 	else
618aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
628aa9ebccSVladimir Oltean }
638aa9ebccSVladimir Oltean 
sja1105_can_forward(struct sja1105_l2_forwarding_entry * l2_fwd,int from,int to)647f7ccdeaSVladimir Oltean static bool sja1105_can_forward(struct sja1105_l2_forwarding_entry *l2_fwd,
657f7ccdeaSVladimir Oltean 				int from, int to)
667f7ccdeaSVladimir Oltean {
677f7ccdeaSVladimir Oltean 	return !!(l2_fwd[from].reach_port & BIT(to));
687f7ccdeaSVladimir Oltean }
697f7ccdeaSVladimir Oltean 
sja1105_is_vlan_configured(struct sja1105_private * priv,u16 vid)70bef0746cSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
71bef0746cSVladimir Oltean {
72bef0746cSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
73bef0746cSVladimir Oltean 	int count, i;
74bef0746cSVladimir Oltean 
75bef0746cSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
76bef0746cSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
77bef0746cSVladimir Oltean 
78bef0746cSVladimir Oltean 	for (i = 0; i < count; i++)
79bef0746cSVladimir Oltean 		if (vlan[i].vlanid == vid)
80bef0746cSVladimir Oltean 			return i;
81bef0746cSVladimir Oltean 
82bef0746cSVladimir Oltean 	/* Return an invalid entry index if not found */
83bef0746cSVladimir Oltean 	return -1;
84bef0746cSVladimir Oltean }
85bef0746cSVladimir Oltean 
sja1105_drop_untagged(struct dsa_switch * ds,int port,bool drop)86bef0746cSVladimir Oltean static int sja1105_drop_untagged(struct dsa_switch *ds, int port, bool drop)
87bef0746cSVladimir Oltean {
88bef0746cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
89bef0746cSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
90bef0746cSVladimir Oltean 
91bef0746cSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
92bef0746cSVladimir Oltean 
93bef0746cSVladimir Oltean 	if (mac[port].drpuntag == drop)
94bef0746cSVladimir Oltean 		return 0;
95bef0746cSVladimir Oltean 
96bef0746cSVladimir Oltean 	mac[port].drpuntag = drop;
97bef0746cSVladimir Oltean 
98bef0746cSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
99bef0746cSVladimir Oltean 					    &mac[port], true);
100bef0746cSVladimir Oltean }
101bef0746cSVladimir Oltean 
sja1105_pvid_apply(struct sja1105_private * priv,int port,u16 pvid)102cde8078eSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
103cde8078eSVladimir Oltean {
104cde8078eSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
105cde8078eSVladimir Oltean 
106cde8078eSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
107cde8078eSVladimir Oltean 
108cde8078eSVladimir Oltean 	if (mac[port].vlanid == pvid)
109cde8078eSVladimir Oltean 		return 0;
110cde8078eSVladimir Oltean 
111cde8078eSVladimir Oltean 	mac[port].vlanid = pvid;
112cde8078eSVladimir Oltean 
113cde8078eSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
114cde8078eSVladimir Oltean 					    &mac[port], true);
115cde8078eSVladimir Oltean }
116cde8078eSVladimir Oltean 
sja1105_commit_pvid(struct dsa_switch * ds,int port)117cde8078eSVladimir Oltean static int sja1105_commit_pvid(struct dsa_switch *ds, int port)
118cde8078eSVladimir Oltean {
119cde8078eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
12041fb0cf1SVladimir Oltean 	struct net_device *br = dsa_port_bridge_dev_get(dp);
121cde8078eSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
122bef0746cSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
123bef0746cSVladimir Oltean 	bool drop_untagged = false;
124bef0746cSVladimir Oltean 	int match, rc;
125cde8078eSVladimir Oltean 	u16 pvid;
126cde8078eSVladimir Oltean 
12741fb0cf1SVladimir Oltean 	if (br && br_vlan_enabled(br))
128cde8078eSVladimir Oltean 		pvid = priv->bridge_pvid[port];
129cde8078eSVladimir Oltean 	else
130cde8078eSVladimir Oltean 		pvid = priv->tag_8021q_pvid[port];
131cde8078eSVladimir Oltean 
132bef0746cSVladimir Oltean 	rc = sja1105_pvid_apply(priv, port, pvid);
133bef0746cSVladimir Oltean 	if (rc)
134bef0746cSVladimir Oltean 		return rc;
135bef0746cSVladimir Oltean 
13673ceab83SVladimir Oltean 	/* Only force dropping of untagged packets when the port is under a
13773ceab83SVladimir Oltean 	 * VLAN-aware bridge. When the tag_8021q pvid is used, we are
13873ceab83SVladimir Oltean 	 * deliberately removing the RX VLAN from the port's VMEMB_PORT list,
13973ceab83SVladimir Oltean 	 * to prevent DSA tag spoofing from the link partner. Untagged packets
14073ceab83SVladimir Oltean 	 * are the only ones that should be received with tag_8021q, so
14173ceab83SVladimir Oltean 	 * definitely don't drop them.
14273ceab83SVladimir Oltean 	 */
14373ceab83SVladimir Oltean 	if (pvid == priv->bridge_pvid[port]) {
144bef0746cSVladimir Oltean 		vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
145bef0746cSVladimir Oltean 
146bef0746cSVladimir Oltean 		match = sja1105_is_vlan_configured(priv, pvid);
147bef0746cSVladimir Oltean 
148bef0746cSVladimir Oltean 		if (match < 0 || !(vlan[match].vmemb_port & BIT(port)))
149bef0746cSVladimir Oltean 			drop_untagged = true;
15073ceab83SVladimir Oltean 	}
151bef0746cSVladimir Oltean 
152b0b8c67eSVladimir Oltean 	if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
153b0b8c67eSVladimir Oltean 		drop_untagged = true;
154b0b8c67eSVladimir Oltean 
155bef0746cSVladimir Oltean 	return sja1105_drop_untagged(ds, port, drop_untagged);
156cde8078eSVladimir Oltean }
157cde8078eSVladimir Oltean 
sja1105_init_mac_settings(struct sja1105_private * priv)1588aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
1598aa9ebccSVladimir Oltean {
1608aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
1618aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
1628aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
1638aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
1648aa9ebccSVladimir Oltean 		 */
1658aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
1668aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
1678aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
1688aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
1698aa9ebccSVladimir Oltean 		.ifg = 0,
1708aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
1711fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
1728aa9ebccSVladimir Oltean 		 */
17341fed17fSVladimir Oltean 		.speed = priv->info->port_speed[SJA1105_SPEED_AUTO],
1748aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
1758aa9ebccSVladimir Oltean 		.tp_delin = 0,
1768aa9ebccSVladimir Oltean 		.tp_delout = 0,
1778aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
1788aa9ebccSVladimir Oltean 		.maxage = 0xFF,
1798aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
1808aa9ebccSVladimir Oltean 		.vlanprio = 0,
181e3502b82SVladimir Oltean 		.vlanid = 1,
1828aa9ebccSVladimir Oltean 		.ing_mirr = false,
1838aa9ebccSVladimir Oltean 		.egr_mirr = false,
1848aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
1858aa9ebccSVladimir Oltean 		.drpnona664 = false,
1868aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
1878aa9ebccSVladimir Oltean 		.drpdtag = false,
1888aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
1898aa9ebccSVladimir Oltean 		.drpuntag = false,
1908aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
1918aa9ebccSVladimir Oltean 		.retag = false,
192640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
193640f763fSVladimir Oltean 		 * STP will enable it.
194640f763fSVladimir Oltean 		 */
195640f763fSVladimir Oltean 		.dyn_learn = false,
1968aa9ebccSVladimir Oltean 		.egress = false,
1978aa9ebccSVladimir Oltean 		.ingress = false,
1988aa9ebccSVladimir Oltean 	};
1998aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
200542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2018aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2025313a37bSVladimir Oltean 	struct dsa_port *dp;
2038aa9ebccSVladimir Oltean 
2048aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
2058aa9ebccSVladimir Oltean 
2068aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
2078aa9ebccSVladimir Oltean 	if (table->entry_count) {
2088aa9ebccSVladimir Oltean 		kfree(table->entries);
2098aa9ebccSVladimir Oltean 		table->entry_count = 0;
2108aa9ebccSVladimir Oltean 	}
2118aa9ebccSVladimir Oltean 
212fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
2138aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2148aa9ebccSVladimir Oltean 	if (!table->entries)
2158aa9ebccSVladimir Oltean 		return -ENOMEM;
2168aa9ebccSVladimir Oltean 
217fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
2188aa9ebccSVladimir Oltean 
2198aa9ebccSVladimir Oltean 	mac = table->entries;
2208aa9ebccSVladimir Oltean 
2215313a37bSVladimir Oltean 	list_for_each_entry(dp, &ds->dst->ports, list) {
2225313a37bSVladimir Oltean 		if (dp->ds != ds)
2235313a37bSVladimir Oltean 			continue;
2245313a37bSVladimir Oltean 
2255313a37bSVladimir Oltean 		mac[dp->index] = default_mac;
226b0b33b04SVladimir Oltean 
227b0b33b04SVladimir Oltean 		/* Let sja1105_bridge_stp_state_set() keep address learning
22881d45898SVladimir Oltean 		 * enabled for the DSA ports. CPU ports use software-assisted
22981d45898SVladimir Oltean 		 * learning to ensure that only FDB entries belonging to the
23081d45898SVladimir Oltean 		 * bridge are learned, and that they are learned towards all
23181d45898SVladimir Oltean 		 * CPU ports in a cross-chip topology if multiple CPU ports
23281d45898SVladimir Oltean 		 * exist.
233640f763fSVladimir Oltean 		 */
2345313a37bSVladimir Oltean 		if (dsa_port_is_dsa(dp))
2355313a37bSVladimir Oltean 			dp->learning = true;
236b0b8c67eSVladimir Oltean 
237b0b8c67eSVladimir Oltean 		/* Disallow untagged packets from being received on the
238b0b8c67eSVladimir Oltean 		 * CPU and DSA ports.
239b0b8c67eSVladimir Oltean 		 */
240b0b8c67eSVladimir Oltean 		if (dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp))
241b0b8c67eSVladimir Oltean 			mac[dp->index].drpuntag = true;
242640f763fSVladimir Oltean 	}
2438aa9ebccSVladimir Oltean 
2448aa9ebccSVladimir Oltean 	return 0;
2458aa9ebccSVladimir Oltean }
2468aa9ebccSVladimir Oltean 
sja1105_init_mii_settings(struct sja1105_private * priv)2475d645df9SVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv)
2488aa9ebccSVladimir Oltean {
2498aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
2508aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
251542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2528aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2538aa9ebccSVladimir Oltean 	int i;
2548aa9ebccSVladimir Oltean 
2558aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
2568aa9ebccSVladimir Oltean 
2578aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
2588aa9ebccSVladimir Oltean 	if (table->entry_count) {
2598aa9ebccSVladimir Oltean 		kfree(table->entries);
2608aa9ebccSVladimir Oltean 		table->entry_count = 0;
2618aa9ebccSVladimir Oltean 	}
2628aa9ebccSVladimir Oltean 
263fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
2648aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2658aa9ebccSVladimir Oltean 	if (!table->entries)
2668aa9ebccSVladimir Oltean 		return -ENOMEM;
2678aa9ebccSVladimir Oltean 
2681fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
269fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
2708aa9ebccSVladimir Oltean 
2718aa9ebccSVladimir Oltean 	mii = table->entries;
2728aa9ebccSVladimir Oltean 
273542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
2745d645df9SVladimir Oltean 		sja1105_mii_role_t role = XMII_MAC;
2755d645df9SVladimir Oltean 
276ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
277ee9d0cb6SVladimir Oltean 			continue;
278ee9d0cb6SVladimir Oltean 
2795d645df9SVladimir Oltean 		switch (priv->phy_mode[i]) {
2805a8f0974SVladimir Oltean 		case PHY_INTERFACE_MODE_INTERNAL:
2815a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_NO_PHY)
2825a8f0974SVladimir Oltean 				goto unsupported;
2835a8f0974SVladimir Oltean 
2845a8f0974SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
2855a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_PHY_BASE_TX)
2865a8f0974SVladimir Oltean 				mii->special[i] = true;
2875a8f0974SVladimir Oltean 
2885a8f0974SVladimir Oltean 			break;
2895d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVMII:
2905d645df9SVladimir Oltean 			role = XMII_PHY;
2915d645df9SVladimir Oltean 			fallthrough;
2928aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
29391a05078SVladimir Oltean 			if (!priv->info->supports_mii[i])
29491a05078SVladimir Oltean 				goto unsupported;
29591a05078SVladimir Oltean 
2968aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
2978aa9ebccSVladimir Oltean 			break;
2985d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVRMII:
2995d645df9SVladimir Oltean 			role = XMII_PHY;
3005d645df9SVladimir Oltean 			fallthrough;
3018aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
30291a05078SVladimir Oltean 			if (!priv->info->supports_rmii[i])
30391a05078SVladimir Oltean 				goto unsupported;
30491a05078SVladimir Oltean 
3058aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
3068aa9ebccSVladimir Oltean 			break;
3078aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
3088aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
3098aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
3108aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
31191a05078SVladimir Oltean 			if (!priv->info->supports_rgmii[i])
31291a05078SVladimir Oltean 				goto unsupported;
31391a05078SVladimir Oltean 
3148aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
3158aa9ebccSVladimir Oltean 			break;
316ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
31791a05078SVladimir Oltean 			if (!priv->info->supports_sgmii[i])
31891a05078SVladimir Oltean 				goto unsupported;
31991a05078SVladimir Oltean 
320ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
321ece578bcSVladimir Oltean 			mii->special[i] = true;
322ffe10e67SVladimir Oltean 			break;
32391a05078SVladimir Oltean 		case PHY_INTERFACE_MODE_2500BASEX:
32491a05078SVladimir Oltean 			if (!priv->info->supports_2500basex[i])
32591a05078SVladimir Oltean 				goto unsupported;
32691a05078SVladimir Oltean 
32791a05078SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
328ece578bcSVladimir Oltean 			mii->special[i] = true;
32991a05078SVladimir Oltean 			break;
33091a05078SVladimir Oltean unsupported:
3318aa9ebccSVladimir Oltean 		default:
33291a05078SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d!\n",
3335d645df9SVladimir Oltean 				phy_modes(priv->phy_mode[i]), i);
3346729188dSVladimir Oltean 			return -EINVAL;
3358aa9ebccSVladimir Oltean 		}
3368aa9ebccSVladimir Oltean 
3375d645df9SVladimir Oltean 		mii->phy_mac[i] = role;
3388aa9ebccSVladimir Oltean 	}
3398aa9ebccSVladimir Oltean 	return 0;
3408aa9ebccSVladimir Oltean }
3418aa9ebccSVladimir Oltean 
sja1105_init_static_fdb(struct sja1105_private * priv)3428aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
3438aa9ebccSVladimir Oltean {
3444d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
3458aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3464d942354SVladimir Oltean 	int port;
3478aa9ebccSVladimir Oltean 
3488aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
3498aa9ebccSVladimir Oltean 
3504d942354SVladimir Oltean 	/* We only populate the FDB table through dynamic L2 Address Lookup
3514d942354SVladimir Oltean 	 * entries, except for a special entry at the end which is a catch-all
3524d942354SVladimir Oltean 	 * for unknown multicast and will be used to control flooding domain.
353291d1e72SVladimir Oltean 	 */
3548aa9ebccSVladimir Oltean 	if (table->entry_count) {
3558aa9ebccSVladimir Oltean 		kfree(table->entries);
3568aa9ebccSVladimir Oltean 		table->entry_count = 0;
3578aa9ebccSVladimir Oltean 	}
3584d942354SVladimir Oltean 
3594d942354SVladimir Oltean 	if (!priv->info->can_limit_mcast_flood)
3604d942354SVladimir Oltean 		return 0;
3614d942354SVladimir Oltean 
3624d942354SVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
3634d942354SVladimir Oltean 				 GFP_KERNEL);
3644d942354SVladimir Oltean 	if (!table->entries)
3654d942354SVladimir Oltean 		return -ENOMEM;
3664d942354SVladimir Oltean 
3674d942354SVladimir Oltean 	table->entry_count = 1;
3684d942354SVladimir Oltean 	l2_lookup = table->entries;
3694d942354SVladimir Oltean 
3704d942354SVladimir Oltean 	/* All L2 multicast addresses have an odd first octet */
3714d942354SVladimir Oltean 	l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST;
3724d942354SVladimir Oltean 	l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST;
3734d942354SVladimir Oltean 	l2_lookup[0].lockeds = true;
3744d942354SVladimir Oltean 	l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1;
3754d942354SVladimir Oltean 
3764d942354SVladimir Oltean 	/* Flood multicast to every port by default */
3774d942354SVladimir Oltean 	for (port = 0; port < priv->ds->num_ports; port++)
3784d942354SVladimir Oltean 		if (!dsa_is_unused_port(priv->ds, port))
3794d942354SVladimir Oltean 			l2_lookup[0].destports |= BIT(port);
3804d942354SVladimir Oltean 
3818aa9ebccSVladimir Oltean 	return 0;
3828aa9ebccSVladimir Oltean }
3838aa9ebccSVladimir Oltean 
sja1105_init_l2_lookup_params(struct sja1105_private * priv)3848aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
3858aa9ebccSVladimir Oltean {
3868aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
3878456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
3888456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
3898aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
3908aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
3911da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
3921da73821SVladimir Oltean 		.start_dynspc = 0,
3938aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
3948aa9ebccSVladimir Oltean 		.poly = 0x97,
395219827efSVladimir Oltean 		/* Always use Independent VLAN Learning (IVL) */
396219827efSVladimir Oltean 		.shared_learn = false,
3978aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
3988aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
3998aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
4008aa9ebccSVladimir Oltean 		 */
4018aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
4028aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
4038aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
4048aa9ebccSVladimir Oltean 		 */
4058aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
4061da73821SVladimir Oltean 		/* P/Q/R/S only */
4071da73821SVladimir Oltean 		.use_static = true,
4081da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
4091da73821SVladimir Oltean 		 * dynamic FDB entries
4101da73821SVladimir Oltean 		 */
4111da73821SVladimir Oltean 		.owr_dyn = true,
4121da73821SVladimir Oltean 		.drpnolearn = true,
4138aa9ebccSVladimir Oltean 	};
414542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
415f238fef1SVladimir Oltean 	int port, num_used_ports = 0;
416542043e9SVladimir Oltean 	struct sja1105_table *table;
417542043e9SVladimir Oltean 	u64 max_fdb_entries;
418542043e9SVladimir Oltean 
419542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++)
420f238fef1SVladimir Oltean 		if (!dsa_is_unused_port(ds, port))
421f238fef1SVladimir Oltean 			num_used_ports++;
422f238fef1SVladimir Oltean 
423f238fef1SVladimir Oltean 	max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports;
424f238fef1SVladimir Oltean 
425f238fef1SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
426f238fef1SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
427f238fef1SVladimir Oltean 			continue;
428f238fef1SVladimir Oltean 
429542043e9SVladimir Oltean 		default_l2_lookup_params.maxaddrp[port] = max_fdb_entries;
430f238fef1SVladimir Oltean 	}
4318aa9ebccSVladimir Oltean 
4328aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
4338aa9ebccSVladimir Oltean 
4348aa9ebccSVladimir Oltean 	if (table->entry_count) {
4358aa9ebccSVladimir Oltean 		kfree(table->entries);
4368aa9ebccSVladimir Oltean 		table->entry_count = 0;
4378aa9ebccSVladimir Oltean 	}
4388aa9ebccSVladimir Oltean 
439fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4408aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4418aa9ebccSVladimir Oltean 	if (!table->entries)
4428aa9ebccSVladimir Oltean 		return -ENOMEM;
4438aa9ebccSVladimir Oltean 
444fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
4458aa9ebccSVladimir Oltean 
4468aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4478aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
4488aa9ebccSVladimir Oltean 				default_l2_lookup_params;
4498aa9ebccSVladimir Oltean 
4508aa9ebccSVladimir Oltean 	return 0;
4518aa9ebccSVladimir Oltean }
4528aa9ebccSVladimir Oltean 
453ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU
454ed040abcSVladimir Oltean  * using management routes (e.g. STP, PTP) as opposed to tag_8021q.
455ed040abcSVladimir Oltean  * All DT-defined ports are members of this VLAN, and there are no
456ed040abcSVladimir Oltean  * restrictions on forwarding (since the CPU selects the destination).
457ed040abcSVladimir Oltean  * Frames from this VLAN will always be transmitted as untagged, and
458ed040abcSVladimir Oltean  * neither the bridge nor the 8021q module cannot create this VLAN ID.
459ed040abcSVladimir Oltean  */
sja1105_init_static_vlan(struct sja1105_private * priv)4608aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
4618aa9ebccSVladimir Oltean {
4628aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4638aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
4643e77e59bSVladimir Oltean 		.type_entry = SJA1110_VLAN_D_TAG,
4658aa9ebccSVladimir Oltean 		.ving_mirr = 0,
4668aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
4678aa9ebccSVladimir Oltean 		.vmemb_port = 0,
4688aa9ebccSVladimir Oltean 		.vlan_bc = 0,
4698aa9ebccSVladimir Oltean 		.tag_port = 0,
470ed040abcSVladimir Oltean 		.vlanid = SJA1105_DEFAULT_VLAN,
4718aa9ebccSVladimir Oltean 	};
472ec5ae610SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
473ec5ae610SVladimir Oltean 	int port;
4748aa9ebccSVladimir Oltean 
4758aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
4768aa9ebccSVladimir Oltean 
4778aa9ebccSVladimir Oltean 	if (table->entry_count) {
4788aa9ebccSVladimir Oltean 		kfree(table->entries);
4798aa9ebccSVladimir Oltean 		table->entry_count = 0;
4808aa9ebccSVladimir Oltean 	}
4818aa9ebccSVladimir Oltean 
482c75857b0SZheng Yongjun 	table->entries = kzalloc(table->ops->unpacked_entry_size,
4838aa9ebccSVladimir Oltean 				 GFP_KERNEL);
4848aa9ebccSVladimir Oltean 	if (!table->entries)
4858aa9ebccSVladimir Oltean 		return -ENOMEM;
4868aa9ebccSVladimir Oltean 
4878aa9ebccSVladimir Oltean 	table->entry_count = 1;
4888aa9ebccSVladimir Oltean 
489ec5ae610SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
490ec5ae610SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
491ec5ae610SVladimir Oltean 			continue;
492ec5ae610SVladimir Oltean 
493ec5ae610SVladimir Oltean 		pvid.vmemb_port |= BIT(port);
494ec5ae610SVladimir Oltean 		pvid.vlan_bc |= BIT(port);
495ec5ae610SVladimir Oltean 		pvid.tag_port &= ~BIT(port);
496ec5ae610SVladimir Oltean 
497c5130029SVladimir Oltean 		if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
4986dfd23d3SVladimir Oltean 			priv->tag_8021q_pvid[port] = SJA1105_DEFAULT_VLAN;
4996dfd23d3SVladimir Oltean 			priv->bridge_pvid[port] = SJA1105_DEFAULT_VLAN;
5006dfd23d3SVladimir Oltean 		}
5018aa9ebccSVladimir Oltean 	}
5028aa9ebccSVladimir Oltean 
5038aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
5048aa9ebccSVladimir Oltean 	return 0;
5058aa9ebccSVladimir Oltean }
5068aa9ebccSVladimir Oltean 
sja1105_init_l2_forwarding(struct sja1105_private * priv)5078aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
5088aa9ebccSVladimir Oltean {
5098aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
510542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
5110f9b762cSVladimir Oltean 	struct dsa_switch_tree *dst;
5128aa9ebccSVladimir Oltean 	struct sja1105_table *table;
5130f9b762cSVladimir Oltean 	struct dsa_link *dl;
5143fa21270SVladimir Oltean 	int port, tc;
5153fa21270SVladimir Oltean 	int from, to;
5168aa9ebccSVladimir Oltean 
5178aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
5188aa9ebccSVladimir Oltean 
5198aa9ebccSVladimir Oltean 	if (table->entry_count) {
5208aa9ebccSVladimir Oltean 		kfree(table->entries);
5218aa9ebccSVladimir Oltean 		table->entry_count = 0;
5228aa9ebccSVladimir Oltean 	}
5238aa9ebccSVladimir Oltean 
524fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
5258aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5268aa9ebccSVladimir Oltean 	if (!table->entries)
5278aa9ebccSVladimir Oltean 		return -ENOMEM;
5288aa9ebccSVladimir Oltean 
529fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
5308aa9ebccSVladimir Oltean 
5318aa9ebccSVladimir Oltean 	l2fwd = table->entries;
5328aa9ebccSVladimir Oltean 
5333fa21270SVladimir Oltean 	/* First 5 entries in the L2 Forwarding Table define the forwarding
5343fa21270SVladimir Oltean 	 * rules and the VLAN PCP to ingress queue mapping.
5353fa21270SVladimir Oltean 	 * Set up the ingress queue mapping first.
5367f7ccdeaSVladimir Oltean 	 */
5373fa21270SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
5383fa21270SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
5398aa9ebccSVladimir Oltean 			continue;
5408aa9ebccSVladimir Oltean 
5413fa21270SVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
5423fa21270SVladimir Oltean 			l2fwd[port].vlan_pmap[tc] = tc;
5433fa21270SVladimir Oltean 	}
5444d942354SVladimir Oltean 
5453fa21270SVladimir Oltean 	/* Then manage the forwarding domain for user ports. These can forward
5463fa21270SVladimir Oltean 	 * only to the always-on domain (CPU port and DSA links)
5473fa21270SVladimir Oltean 	 */
5483fa21270SVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
5493fa21270SVladimir Oltean 		if (!dsa_is_user_port(ds, from))
5503fa21270SVladimir Oltean 			continue;
5514d942354SVladimir Oltean 
5523fa21270SVladimir Oltean 		for (to = 0; to < ds->num_ports; to++) {
5533fa21270SVladimir Oltean 			if (!dsa_is_cpu_port(ds, to) &&
5543fa21270SVladimir Oltean 			    !dsa_is_dsa_port(ds, to))
5553fa21270SVladimir Oltean 				continue;
5563fa21270SVladimir Oltean 
5573fa21270SVladimir Oltean 			l2fwd[from].bc_domain |= BIT(to);
5583fa21270SVladimir Oltean 			l2fwd[from].fl_domain |= BIT(to);
5593fa21270SVladimir Oltean 
5603fa21270SVladimir Oltean 			sja1105_port_allow_traffic(l2fwd, from, to, true);
5613fa21270SVladimir Oltean 		}
5623fa21270SVladimir Oltean 	}
5633fa21270SVladimir Oltean 
5643fa21270SVladimir Oltean 	/* Then manage the forwarding domain for DSA links and CPU ports (the
5653fa21270SVladimir Oltean 	 * always-on domain). These can send packets to any enabled port except
5663fa21270SVladimir Oltean 	 * themselves.
5673fa21270SVladimir Oltean 	 */
5683fa21270SVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
5693fa21270SVladimir Oltean 		if (!dsa_is_cpu_port(ds, from) && !dsa_is_dsa_port(ds, from))
5703fa21270SVladimir Oltean 			continue;
5713fa21270SVladimir Oltean 
5723fa21270SVladimir Oltean 		for (to = 0; to < ds->num_ports; to++) {
5733fa21270SVladimir Oltean 			if (dsa_is_unused_port(ds, to))
5743fa21270SVladimir Oltean 				continue;
5753fa21270SVladimir Oltean 
5763fa21270SVladimir Oltean 			if (from == to)
5773fa21270SVladimir Oltean 				continue;
5783fa21270SVladimir Oltean 
5793fa21270SVladimir Oltean 			l2fwd[from].bc_domain |= BIT(to);
5803fa21270SVladimir Oltean 			l2fwd[from].fl_domain |= BIT(to);
5813fa21270SVladimir Oltean 
5823fa21270SVladimir Oltean 			sja1105_port_allow_traffic(l2fwd, from, to, true);
5833fa21270SVladimir Oltean 		}
5843fa21270SVladimir Oltean 	}
5853fa21270SVladimir Oltean 
5860f9b762cSVladimir Oltean 	/* In odd topologies ("H" connections where there is a DSA link to
5870f9b762cSVladimir Oltean 	 * another switch which also has its own CPU port), TX packets can loop
5880f9b762cSVladimir Oltean 	 * back into the system (they are flooded from CPU port 1 to the DSA
5890f9b762cSVladimir Oltean 	 * link, and from there to CPU port 2). Prevent this from happening by
5900f9b762cSVladimir Oltean 	 * cutting RX from DSA links towards our CPU port, if the remote switch
5910f9b762cSVladimir Oltean 	 * has its own CPU port and therefore doesn't need ours for network
5920f9b762cSVladimir Oltean 	 * stack termination.
5930f9b762cSVladimir Oltean 	 */
5940f9b762cSVladimir Oltean 	dst = ds->dst;
5950f9b762cSVladimir Oltean 
5960f9b762cSVladimir Oltean 	list_for_each_entry(dl, &dst->rtable, list) {
5970f9b762cSVladimir Oltean 		if (dl->dp->ds != ds || dl->link_dp->cpu_dp == dl->dp->cpu_dp)
5980f9b762cSVladimir Oltean 			continue;
5990f9b762cSVladimir Oltean 
6000f9b762cSVladimir Oltean 		from = dl->dp->index;
6010f9b762cSVladimir Oltean 		to = dsa_upstream_port(ds, from);
6020f9b762cSVladimir Oltean 
6030f9b762cSVladimir Oltean 		dev_warn(ds->dev,
6040f9b762cSVladimir Oltean 			 "H topology detected, cutting RX from DSA link %d to CPU port %d to prevent TX packet loops\n",
6050f9b762cSVladimir Oltean 			 from, to);
6060f9b762cSVladimir Oltean 
6070f9b762cSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, from, to, false);
6080f9b762cSVladimir Oltean 
6090f9b762cSVladimir Oltean 		l2fwd[from].bc_domain &= ~BIT(to);
6100f9b762cSVladimir Oltean 		l2fwd[from].fl_domain &= ~BIT(to);
6110f9b762cSVladimir Oltean 	}
6120f9b762cSVladimir Oltean 
6133fa21270SVladimir Oltean 	/* Finally, manage the egress flooding domain. All ports start up with
6143fa21270SVladimir Oltean 	 * flooding enabled, including the CPU port and DSA links.
6153fa21270SVladimir Oltean 	 */
6163fa21270SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
6173fa21270SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
6183fa21270SVladimir Oltean 			continue;
6193fa21270SVladimir Oltean 
6203fa21270SVladimir Oltean 		priv->ucast_egress_floods |= BIT(port);
6213fa21270SVladimir Oltean 		priv->bcast_egress_floods |= BIT(port);
6228aa9ebccSVladimir Oltean 	}
623f238fef1SVladimir Oltean 
6248aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
6258aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
6268aa9ebccSVladimir Oltean 	 */
6273fa21270SVladimir Oltean 	for (tc = 0; tc < SJA1105_NUM_TC; tc++) {
6283fa21270SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
6293fa21270SVladimir Oltean 			if (dsa_is_unused_port(ds, port))
630f238fef1SVladimir Oltean 				continue;
631f238fef1SVladimir Oltean 
6323fa21270SVladimir Oltean 			l2fwd[ds->num_ports + tc].vlan_pmap[port] = tc;
633f238fef1SVladimir Oltean 		}
6343e77e59bSVladimir Oltean 
6353fa21270SVladimir Oltean 		l2fwd[ds->num_ports + tc].type_egrpcp2outputq = true;
6363e77e59bSVladimir Oltean 	}
6373e77e59bSVladimir Oltean 
6383e77e59bSVladimir Oltean 	return 0;
6393e77e59bSVladimir Oltean }
6403e77e59bSVladimir Oltean 
sja1110_init_pcp_remapping(struct sja1105_private * priv)6413e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv)
6423e77e59bSVladimir Oltean {
6433e77e59bSVladimir Oltean 	struct sja1110_pcp_remapping_entry *pcp_remap;
6443e77e59bSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
6453e77e59bSVladimir Oltean 	struct sja1105_table *table;
6463e77e59bSVladimir Oltean 	int port, tc;
6473e77e59bSVladimir Oltean 
6483e77e59bSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING];
6493e77e59bSVladimir Oltean 
6503e77e59bSVladimir Oltean 	/* Nothing to do for SJA1105 */
6513e77e59bSVladimir Oltean 	if (!table->ops->max_entry_count)
6523e77e59bSVladimir Oltean 		return 0;
6533e77e59bSVladimir Oltean 
6543e77e59bSVladimir Oltean 	if (table->entry_count) {
6553e77e59bSVladimir Oltean 		kfree(table->entries);
6563e77e59bSVladimir Oltean 		table->entry_count = 0;
6573e77e59bSVladimir Oltean 	}
6583e77e59bSVladimir Oltean 
6593e77e59bSVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
6603e77e59bSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
6613e77e59bSVladimir Oltean 	if (!table->entries)
6623e77e59bSVladimir Oltean 		return -ENOMEM;
6633e77e59bSVladimir Oltean 
6643e77e59bSVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
6653e77e59bSVladimir Oltean 
6663e77e59bSVladimir Oltean 	pcp_remap = table->entries;
6673e77e59bSVladimir Oltean 
6683e77e59bSVladimir Oltean 	/* Repeat the configuration done for vlan_pmap */
6693e77e59bSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
6703e77e59bSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
6713e77e59bSVladimir Oltean 			continue;
6723e77e59bSVladimir Oltean 
6733e77e59bSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
6743e77e59bSVladimir Oltean 			pcp_remap[port].egrpcp[tc] = tc;
675f238fef1SVladimir Oltean 	}
6768aa9ebccSVladimir Oltean 
6778aa9ebccSVladimir Oltean 	return 0;
6788aa9ebccSVladimir Oltean }
6798aa9ebccSVladimir Oltean 
sja1105_init_l2_forwarding_params(struct sja1105_private * priv)6808aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
6818aa9ebccSVladimir Oltean {
6821bf658eeSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2fwd_params;
6838aa9ebccSVladimir Oltean 	struct sja1105_table *table;
6848aa9ebccSVladimir Oltean 
6858aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
6868aa9ebccSVladimir Oltean 
6878aa9ebccSVladimir Oltean 	if (table->entry_count) {
6888aa9ebccSVladimir Oltean 		kfree(table->entries);
6898aa9ebccSVladimir Oltean 		table->entry_count = 0;
6908aa9ebccSVladimir Oltean 	}
6918aa9ebccSVladimir Oltean 
692fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
6938aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
6948aa9ebccSVladimir Oltean 	if (!table->entries)
6958aa9ebccSVladimir Oltean 		return -ENOMEM;
6968aa9ebccSVladimir Oltean 
697fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
6988aa9ebccSVladimir Oltean 
6998aa9ebccSVladimir Oltean 	/* This table only has a single entry */
7001bf658eeSVladimir Oltean 	l2fwd_params = table->entries;
7011bf658eeSVladimir Oltean 
7021bf658eeSVladimir Oltean 	/* Disallow dynamic reconfiguration of vlan_pmap */
7031bf658eeSVladimir Oltean 	l2fwd_params->max_dynp = 0;
7041bf658eeSVladimir Oltean 	/* Use a single memory partition for all ingress queues */
7051bf658eeSVladimir Oltean 	l2fwd_params->part_spc[0] = priv->info->max_frame_mem;
7068aa9ebccSVladimir Oltean 
7078aa9ebccSVladimir Oltean 	return 0;
7088aa9ebccSVladimir Oltean }
7098aa9ebccSVladimir Oltean 
sja1105_frame_memory_partitioning(struct sja1105_private * priv)710aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
711aaa270c6SVladimir Oltean {
712aaa270c6SVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
713aaa270c6SVladimir Oltean 	struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
714aaa270c6SVladimir Oltean 	struct sja1105_table *table;
715aaa270c6SVladimir Oltean 
716aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
717aaa270c6SVladimir Oltean 	l2_fwd_params = table->entries;
7180fac6aa0SVladimir Oltean 	l2_fwd_params->part_spc[0] = SJA1105_MAX_FRAME_MEMORY;
719aaa270c6SVladimir Oltean 
720aaa270c6SVladimir Oltean 	/* If we have any critical-traffic virtual links, we need to reserve
721aaa270c6SVladimir Oltean 	 * some frame buffer memory for them. At the moment, hardcode the value
722aaa270c6SVladimir Oltean 	 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
723aaa270c6SVladimir Oltean 	 * remaining for best-effort traffic. TODO: figure out a more flexible
724aaa270c6SVladimir Oltean 	 * way to perform the frame buffer partitioning.
725aaa270c6SVladimir Oltean 	 */
726aaa270c6SVladimir Oltean 	if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
727aaa270c6SVladimir Oltean 		return;
728aaa270c6SVladimir Oltean 
729aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
730aaa270c6SVladimir Oltean 	vl_fwd_params = table->entries;
731aaa270c6SVladimir Oltean 
732aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
733aaa270c6SVladimir Oltean 	vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
734aaa270c6SVladimir Oltean }
735aaa270c6SVladimir Oltean 
736ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values:
737ceec8bc0SVladimir Oltean  *
738ceec8bc0SVladimir Oltean  *      | 100 Mbps ports |  1Gbps ports  | 2.5Gbps ports | Disabled ports
739ceec8bc0SVladimir Oltean  * -----+----------------+---------------+---------------+---------------
740ceec8bc0SVladimir Oltean  *   0  |   0, [5:10]    |     [1:2]     |     [3:4]     |     retag
741ceec8bc0SVladimir Oltean  *   1  |0, [5:10], retag|     [1:2]     |     [3:4]     |       -
742ceec8bc0SVladimir Oltean  *   2  |   0, [5:10]    |  [1:3], retag |       4       |       -
743ceec8bc0SVladimir Oltean  *   3  |   0, [5:10]    |[1:2], 4, retag|       3       |       -
744ceec8bc0SVladimir Oltean  *   4  |  0, 2, [5:10]  |    1, retag   |     [3:4]     |       -
745ceec8bc0SVladimir Oltean  *   5  |  0, 1, [5:10]  |    2, retag   |     [3:4]     |       -
746ceec8bc0SVladimir Oltean  *  14  |   0, [5:10]    | [1:4], retag  |       -       |       -
747ceec8bc0SVladimir Oltean  *  15  |     [5:10]     | [0:4], retag  |       -       |       -
748ceec8bc0SVladimir Oltean  */
sja1110_select_tdmaconfigidx(struct sja1105_private * priv)749ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv)
750ceec8bc0SVladimir Oltean {
751ceec8bc0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
752ceec8bc0SVladimir Oltean 	struct sja1105_table *table;
753ceec8bc0SVladimir Oltean 	bool port_1_is_base_tx;
754ceec8bc0SVladimir Oltean 	bool port_3_is_2500;
755ceec8bc0SVladimir Oltean 	bool port_4_is_2500;
756ceec8bc0SVladimir Oltean 	u64 tdmaconfigidx;
757ceec8bc0SVladimir Oltean 
758ceec8bc0SVladimir Oltean 	if (priv->info->device_id != SJA1110_DEVICE_ID)
759ceec8bc0SVladimir Oltean 		return;
760ceec8bc0SVladimir Oltean 
761ceec8bc0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
762ceec8bc0SVladimir Oltean 	general_params = table->entries;
763ceec8bc0SVladimir Oltean 
764ceec8bc0SVladimir Oltean 	/* All the settings below are "as opposed to SGMII", which is the
765ceec8bc0SVladimir Oltean 	 * other pinmuxing option.
766ceec8bc0SVladimir Oltean 	 */
767ceec8bc0SVladimir Oltean 	port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL;
768ceec8bc0SVladimir Oltean 	port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX;
769ceec8bc0SVladimir Oltean 	port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX;
770ceec8bc0SVladimir Oltean 
771ceec8bc0SVladimir Oltean 	if (port_1_is_base_tx)
772ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
773ceec8bc0SVladimir Oltean 		tdmaconfigidx = 5;
774ceec8bc0SVladimir Oltean 	else if (port_3_is_2500 && port_4_is_2500)
775ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 100 Mbps */
776ceec8bc0SVladimir Oltean 		tdmaconfigidx = 1;
777ceec8bc0SVladimir Oltean 	else if (port_3_is_2500)
778ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
779ceec8bc0SVladimir Oltean 		tdmaconfigidx = 3;
780ceec8bc0SVladimir Oltean 	else if (port_4_is_2500)
781ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
782ceec8bc0SVladimir Oltean 		tdmaconfigidx = 2;
783ceec8bc0SVladimir Oltean 	else
784ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
785ceec8bc0SVladimir Oltean 		tdmaconfigidx = 14;
786ceec8bc0SVladimir Oltean 
787ceec8bc0SVladimir Oltean 	general_params->tdmaconfigidx = tdmaconfigidx;
788ceec8bc0SVladimir Oltean }
789ceec8bc0SVladimir Oltean 
sja1105_init_topology(struct sja1105_private * priv,struct sja1105_general_params_entry * general_params)79030a100e6SVladimir Oltean static int sja1105_init_topology(struct sja1105_private *priv,
79130a100e6SVladimir Oltean 				 struct sja1105_general_params_entry *general_params)
79230a100e6SVladimir Oltean {
79330a100e6SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
79430a100e6SVladimir Oltean 	int port;
79530a100e6SVladimir Oltean 
79630a100e6SVladimir Oltean 	/* The host port is the destination for traffic matching mac_fltres1
79730a100e6SVladimir Oltean 	 * and mac_fltres0 on all ports except itself. Default to an invalid
79830a100e6SVladimir Oltean 	 * value.
79930a100e6SVladimir Oltean 	 */
80030a100e6SVladimir Oltean 	general_params->host_port = ds->num_ports;
80130a100e6SVladimir Oltean 
80230a100e6SVladimir Oltean 	/* Link-local traffic received on casc_port will be forwarded
80330a100e6SVladimir Oltean 	 * to host_port without embedding the source port and device ID
80430a100e6SVladimir Oltean 	 * info in the destination MAC address, and no RX timestamps will be
80530a100e6SVladimir Oltean 	 * taken either (presumably because it is a cascaded port and a
80630a100e6SVladimir Oltean 	 * downstream SJA switch already did that).
80730a100e6SVladimir Oltean 	 * To disable the feature, we need to do different things depending on
80830a100e6SVladimir Oltean 	 * switch generation. On SJA1105 we need to set an invalid port, while
80930a100e6SVladimir Oltean 	 * on SJA1110 which support multiple cascaded ports, this field is a
81030a100e6SVladimir Oltean 	 * bitmask so it must be left zero.
81130a100e6SVladimir Oltean 	 */
81230a100e6SVladimir Oltean 	if (!priv->info->multiple_cascade_ports)
81330a100e6SVladimir Oltean 		general_params->casc_port = ds->num_ports;
81430a100e6SVladimir Oltean 
81530a100e6SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
81630a100e6SVladimir Oltean 		bool is_upstream = dsa_is_upstream_port(ds, port);
81730a100e6SVladimir Oltean 		bool is_dsa_link = dsa_is_dsa_port(ds, port);
81830a100e6SVladimir Oltean 
81930a100e6SVladimir Oltean 		/* Upstream ports can be dedicated CPU ports or
82030a100e6SVladimir Oltean 		 * upstream-facing DSA links
82130a100e6SVladimir Oltean 		 */
82230a100e6SVladimir Oltean 		if (is_upstream) {
82330a100e6SVladimir Oltean 			if (general_params->host_port == ds->num_ports) {
82430a100e6SVladimir Oltean 				general_params->host_port = port;
82530a100e6SVladimir Oltean 			} else {
82630a100e6SVladimir Oltean 				dev_err(ds->dev,
82730a100e6SVladimir Oltean 					"Port %llu is already a host port, configuring %d as one too is not supported\n",
82830a100e6SVladimir Oltean 					general_params->host_port, port);
82930a100e6SVladimir Oltean 				return -EINVAL;
83030a100e6SVladimir Oltean 			}
83130a100e6SVladimir Oltean 		}
83230a100e6SVladimir Oltean 
83330a100e6SVladimir Oltean 		/* Cascade ports are downstream-facing DSA links */
83430a100e6SVladimir Oltean 		if (is_dsa_link && !is_upstream) {
83530a100e6SVladimir Oltean 			if (priv->info->multiple_cascade_ports) {
83630a100e6SVladimir Oltean 				general_params->casc_port |= BIT(port);
83730a100e6SVladimir Oltean 			} else if (general_params->casc_port == ds->num_ports) {
83830a100e6SVladimir Oltean 				general_params->casc_port = port;
83930a100e6SVladimir Oltean 			} else {
84030a100e6SVladimir Oltean 				dev_err(ds->dev,
84130a100e6SVladimir Oltean 					"Port %llu is already a cascade port, configuring %d as one too is not supported\n",
84230a100e6SVladimir Oltean 					general_params->casc_port, port);
84330a100e6SVladimir Oltean 				return -EINVAL;
84430a100e6SVladimir Oltean 			}
84530a100e6SVladimir Oltean 		}
84630a100e6SVladimir Oltean 	}
84730a100e6SVladimir Oltean 
84830a100e6SVladimir Oltean 	if (general_params->host_port == ds->num_ports) {
84930a100e6SVladimir Oltean 		dev_err(ds->dev, "No host port configured\n");
85030a100e6SVladimir Oltean 		return -EINVAL;
85130a100e6SVladimir Oltean 	}
85230a100e6SVladimir Oltean 
85330a100e6SVladimir Oltean 	return 0;
85430a100e6SVladimir Oltean }
85530a100e6SVladimir Oltean 
sja1105_init_general_params(struct sja1105_private * priv)8568aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
8578aa9ebccSVladimir Oltean {
8588aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
859511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
860511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
8618aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
8625f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
8635f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
8645f06c63bSVladimir Oltean 		 */
86508fde09aSVladimir Oltean 		.hostprio = 7,
8668aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
8678aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
868b4638af8SVladimir Oltean 		.incl_srcpt1 = true,
869a372d66aSVladimir Oltean 		.send_meta1  = true,
8708aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
8718aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
872b4638af8SVladimir Oltean 		.incl_srcpt0 = true,
873a372d66aSVladimir Oltean 		.send_meta0  = true,
874511e6ca0SVladimir Oltean 		/* Default to an invalid value */
875542043e9SVladimir Oltean 		.mirr_port = priv->ds->num_ports,
8768aa9ebccSVladimir Oltean 		/* No TTEthernet */
877dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
8788aa9ebccSVladimir Oltean 		.vlmarker = 0,
8798aa9ebccSVladimir Oltean 		.vlmask = 0,
8808aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
8818aa9ebccSVladimir Oltean 		.ignore2stf = 0,
8826666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
8836666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
8846666cebcSVladimir Oltean 		 */
8856666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
8866666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
88729305260SVladimir Oltean 		/* Enable the TTEthernet engine on SJA1110 */
88829305260SVladimir Oltean 		.tte_en = true,
8894913b8ebSVladimir Oltean 		/* Set up the EtherType for control packets on SJA1110 */
8904913b8ebSVladimir Oltean 		.header_type = ETH_P_SJA1110,
8918aa9ebccSVladimir Oltean 	};
8926c0de59bSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
8938aa9ebccSVladimir Oltean 	struct sja1105_table *table;
89430a100e6SVladimir Oltean 	int rc;
895df2a81a3SVladimir Oltean 
89630a100e6SVladimir Oltean 	rc = sja1105_init_topology(priv, &default_general_params);
89730a100e6SVladimir Oltean 	if (rc)
89830a100e6SVladimir Oltean 		return rc;
8998aa9ebccSVladimir Oltean 
9008aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
9018aa9ebccSVladimir Oltean 
9028aa9ebccSVladimir Oltean 	if (table->entry_count) {
9038aa9ebccSVladimir Oltean 		kfree(table->entries);
9048aa9ebccSVladimir Oltean 		table->entry_count = 0;
9058aa9ebccSVladimir Oltean 	}
9068aa9ebccSVladimir Oltean 
907fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
9088aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
9098aa9ebccSVladimir Oltean 	if (!table->entries)
9108aa9ebccSVladimir Oltean 		return -ENOMEM;
9118aa9ebccSVladimir Oltean 
912fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
9138aa9ebccSVladimir Oltean 
9146c0de59bSVladimir Oltean 	general_params = table->entries;
9156c0de59bSVladimir Oltean 
9168aa9ebccSVladimir Oltean 	/* This table only has a single entry */
9176c0de59bSVladimir Oltean 	general_params[0] = default_general_params;
9188aa9ebccSVladimir Oltean 
919ceec8bc0SVladimir Oltean 	sja1110_select_tdmaconfigidx(priv);
920ceec8bc0SVladimir Oltean 
9218aa9ebccSVladimir Oltean 	return 0;
9228aa9ebccSVladimir Oltean }
9238aa9ebccSVladimir Oltean 
sja1105_init_avb_params(struct sja1105_private * priv)92479d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
92579d5511cSVladimir Oltean {
92679d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
92779d5511cSVladimir Oltean 	struct sja1105_table *table;
92879d5511cSVladimir Oltean 
92979d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
93079d5511cSVladimir Oltean 
93179d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
93279d5511cSVladimir Oltean 	if (table->entry_count) {
93379d5511cSVladimir Oltean 		kfree(table->entries);
93479d5511cSVladimir Oltean 		table->entry_count = 0;
93579d5511cSVladimir Oltean 	}
93679d5511cSVladimir Oltean 
937fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
93879d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
93979d5511cSVladimir Oltean 	if (!table->entries)
94079d5511cSVladimir Oltean 		return -ENOMEM;
94179d5511cSVladimir Oltean 
942fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
94379d5511cSVladimir Oltean 
94479d5511cSVladimir Oltean 	avb = table->entries;
94579d5511cSVladimir Oltean 
94679d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
94779d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
94879d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
949747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
950747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
951747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
952747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
953747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
954747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
955747e5eb3SVladimir Oltean 	 */
956747e5eb3SVladimir Oltean 	avb->cas_master = false;
95779d5511cSVladimir Oltean 
95879d5511cSVladimir Oltean 	return 0;
95979d5511cSVladimir Oltean }
96079d5511cSVladimir Oltean 
961a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
962a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
963a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
964a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
965a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
966a7cc081cSVladimir Oltean  * will be used for this frame.
967a7cc081cSVladimir Oltean  *
968a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
969a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
970a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
971a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
972a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
973a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
974a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
975a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
976a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
977a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
978a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
979a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
980a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
981a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
982a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
983a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
984a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
985a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
986a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
987a7cc081cSVladimir Oltean  * +------------+--------+
988a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
989a7cc081cSVladimir Oltean  * +------------+--------+
990a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
991a7cc081cSVladimir Oltean  * +------------+--------+
992a7cc081cSVladimir Oltean  *    ...                                  ...
993a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
994a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
995a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
996a7cc081cSVladimir Oltean  *
997a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
998a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
999a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
1000a7cc081cSVladimir Oltean  * lookup) equal.
1001a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
1002a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
1003a7cc081cSVladimir Oltean  */
10048aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
10058aa9ebccSVladimir Oltean 
sja1105_init_l2_policing(struct sja1105_private * priv)10068aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
10078aa9ebccSVladimir Oltean {
10088aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
1009542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
10108aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1011a7cc081cSVladimir Oltean 	int port, tc;
10128aa9ebccSVladimir Oltean 
10138aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
10148aa9ebccSVladimir Oltean 
10158aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
10168aa9ebccSVladimir Oltean 	if (table->entry_count) {
10178aa9ebccSVladimir Oltean 		kfree(table->entries);
10188aa9ebccSVladimir Oltean 		table->entry_count = 0;
10198aa9ebccSVladimir Oltean 	}
10208aa9ebccSVladimir Oltean 
1021fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
10228aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
10238aa9ebccSVladimir Oltean 	if (!table->entries)
10248aa9ebccSVladimir Oltean 		return -ENOMEM;
10258aa9ebccSVladimir Oltean 
1026fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
10278aa9ebccSVladimir Oltean 
10288aa9ebccSVladimir Oltean 	policing = table->entries;
10298aa9ebccSVladimir Oltean 
1030a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
1031542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
103238fbe91fSVladimir Oltean 		int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port;
1033542043e9SVladimir Oltean 		int bcast = (ds->num_ports * SJA1105_NUM_TC) + port;
1034a7cc081cSVladimir Oltean 
1035a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
1036a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
1037a7cc081cSVladimir Oltean 
1038a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
103938fbe91fSVladimir Oltean 		/* Only SJA1110 has multicast policers */
1040f8bac7f9SRadu Nicolae Pirea (OSS) 		if (mcast < table->ops->max_entry_count)
104138fbe91fSVladimir Oltean 			policing[mcast].sharindx = port;
1042a7cc081cSVladimir Oltean 	}
1043a7cc081cSVladimir Oltean 
1044a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
1045542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1046c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
1047c279c726SVladimir Oltean 
1048777e55e3SVladimir Oltean 		if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
1049c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
10508aa9ebccSVladimir Oltean 
1051a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
1052a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
1053a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
1054a7cc081cSVladimir Oltean 		policing[port].partition = 0;
10558aa9ebccSVladimir Oltean 	}
1056a7cc081cSVladimir Oltean 
10578aa9ebccSVladimir Oltean 	return 0;
10588aa9ebccSVladimir Oltean }
10598aa9ebccSVladimir Oltean 
sja1105_static_config_load(struct sja1105_private * priv)10605d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv)
10618aa9ebccSVladimir Oltean {
10628aa9ebccSVladimir Oltean 	int rc;
10638aa9ebccSVladimir Oltean 
10648aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
10658aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
10668aa9ebccSVladimir Oltean 					priv->info->static_ops,
10678aa9ebccSVladimir Oltean 					priv->info->device_id);
10688aa9ebccSVladimir Oltean 	if (rc)
10698aa9ebccSVladimir Oltean 		return rc;
10708aa9ebccSVladimir Oltean 
10718aa9ebccSVladimir Oltean 	/* Build static configuration */
10728aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
10738aa9ebccSVladimir Oltean 	if (rc < 0)
10748aa9ebccSVladimir Oltean 		return rc;
10755d645df9SVladimir Oltean 	rc = sja1105_init_mii_settings(priv);
10768aa9ebccSVladimir Oltean 	if (rc < 0)
10778aa9ebccSVladimir Oltean 		return rc;
10788aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
10798aa9ebccSVladimir Oltean 	if (rc < 0)
10808aa9ebccSVladimir Oltean 		return rc;
10818aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
10828aa9ebccSVladimir Oltean 	if (rc < 0)
10838aa9ebccSVladimir Oltean 		return rc;
10848aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
10858aa9ebccSVladimir Oltean 	if (rc < 0)
10868aa9ebccSVladimir Oltean 		return rc;
10878aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
10888aa9ebccSVladimir Oltean 	if (rc < 0)
10898aa9ebccSVladimir Oltean 		return rc;
10908aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
10918aa9ebccSVladimir Oltean 	if (rc < 0)
10928aa9ebccSVladimir Oltean 		return rc;
10938aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
10948aa9ebccSVladimir Oltean 	if (rc < 0)
10958aa9ebccSVladimir Oltean 		return rc;
10968aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
10978aa9ebccSVladimir Oltean 	if (rc < 0)
10988aa9ebccSVladimir Oltean 		return rc;
109979d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
110079d5511cSVladimir Oltean 	if (rc < 0)
110179d5511cSVladimir Oltean 		return rc;
11023e77e59bSVladimir Oltean 	rc = sja1110_init_pcp_remapping(priv);
11033e77e59bSVladimir Oltean 	if (rc < 0)
11043e77e59bSVladimir Oltean 		return rc;
11058aa9ebccSVladimir Oltean 
11068aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
11078aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
11088aa9ebccSVladimir Oltean }
11098aa9ebccSVladimir Oltean 
11109ca482a2SVladimir Oltean /* This is the "new way" for a MAC driver to configure its RGMII delay lines,
11119ca482a2SVladimir Oltean  * based on the explicit "rx-internal-delay-ps" and "tx-internal-delay-ps"
11129ca482a2SVladimir Oltean  * properties. It has the advantage of working with fixed links and with PHYs
11139ca482a2SVladimir Oltean  * that apply RGMII delays too, and the MAC driver needs not perform any
11149ca482a2SVladimir Oltean  * special checks.
11159ca482a2SVladimir Oltean  *
11169ca482a2SVladimir Oltean  * Previously we were acting upon the "phy-mode" property when we were
11179ca482a2SVladimir Oltean  * operating in fixed-link, basically acting as a PHY, but with a reversed
11189ca482a2SVladimir Oltean  * interpretation: PHY_INTERFACE_MODE_RGMII_TXID means that the MAC should
11199ca482a2SVladimir Oltean  * behave as if it is connected to a PHY which has applied RGMII delays in the
11209ca482a2SVladimir Oltean  * TX direction. So if anything, RX delays should have been added by the MAC,
11219ca482a2SVladimir Oltean  * but we were adding TX delays.
11229ca482a2SVladimir Oltean  *
11239ca482a2SVladimir Oltean  * If the "{rx,tx}-internal-delay-ps" properties are not specified, we fall
11249ca482a2SVladimir Oltean  * back to the legacy behavior and apply delays on fixed-link ports based on
11259ca482a2SVladimir Oltean  * the reverse interpretation of the phy-mode. This is a deviation from the
11269ca482a2SVladimir Oltean  * expected default behavior which is to simply apply no delays. To achieve
11279ca482a2SVladimir Oltean  * that behavior with the new bindings, it is mandatory to specify
11289ca482a2SVladimir Oltean  * "{rx,tx}-internal-delay-ps" with a value of 0.
11299ca482a2SVladimir Oltean  */
sja1105_parse_rgmii_delays(struct sja1105_private * priv,int port,struct device_node * port_dn)11309ca482a2SVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv, int port,
11319ca482a2SVladimir Oltean 				      struct device_node *port_dn)
1132f5b8631cSVladimir Oltean {
11339ca482a2SVladimir Oltean 	phy_interface_t phy_mode = priv->phy_mode[port];
11349ca482a2SVladimir Oltean 	struct device *dev = &priv->spidev->dev;
11359ca482a2SVladimir Oltean 	int rx_delay = -1, tx_delay = -1;
1136f5b8631cSVladimir Oltean 
11379ca482a2SVladimir Oltean 	if (!phy_interface_mode_is_rgmii(phy_mode))
11389ca482a2SVladimir Oltean 		return 0;
1139f5b8631cSVladimir Oltean 
11409ca482a2SVladimir Oltean 	of_property_read_u32(port_dn, "rx-internal-delay-ps", &rx_delay);
11419ca482a2SVladimir Oltean 	of_property_read_u32(port_dn, "tx-internal-delay-ps", &tx_delay);
1142f5b8631cSVladimir Oltean 
11439ca482a2SVladimir Oltean 	if (rx_delay == -1 && tx_delay == -1 && priv->fixed_link[port]) {
11449ca482a2SVladimir Oltean 		dev_warn(dev,
11459ca482a2SVladimir Oltean 			 "Port %d interpreting RGMII delay settings based on \"phy-mode\" property, "
11469ca482a2SVladimir Oltean 			 "please update device tree to specify \"rx-internal-delay-ps\" and "
11479ca482a2SVladimir Oltean 			 "\"tx-internal-delay-ps\"",
11489ca482a2SVladimir Oltean 			 port);
1149f5b8631cSVladimir Oltean 
11509ca482a2SVladimir Oltean 		if (phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
11519ca482a2SVladimir Oltean 		    phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
11529ca482a2SVladimir Oltean 			rx_delay = 2000;
11539ca482a2SVladimir Oltean 
11549ca482a2SVladimir Oltean 		if (phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
11559ca482a2SVladimir Oltean 		    phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
11569ca482a2SVladimir Oltean 			tx_delay = 2000;
11579ca482a2SVladimir Oltean 	}
11589ca482a2SVladimir Oltean 
11599ca482a2SVladimir Oltean 	if (rx_delay < 0)
11609ca482a2SVladimir Oltean 		rx_delay = 0;
11619ca482a2SVladimir Oltean 	if (tx_delay < 0)
11629ca482a2SVladimir Oltean 		tx_delay = 0;
11639ca482a2SVladimir Oltean 
11649ca482a2SVladimir Oltean 	if ((rx_delay || tx_delay) && !priv->info->setup_rgmii_delay) {
11659ca482a2SVladimir Oltean 		dev_err(dev, "Chip cannot apply RGMII delays\n");
1166f5b8631cSVladimir Oltean 		return -EINVAL;
1167f5b8631cSVladimir Oltean 	}
11689ca482a2SVladimir Oltean 
11699ca482a2SVladimir Oltean 	if ((rx_delay && rx_delay < SJA1105_RGMII_DELAY_MIN_PS) ||
11709ca482a2SVladimir Oltean 	    (tx_delay && tx_delay < SJA1105_RGMII_DELAY_MIN_PS) ||
11719ca482a2SVladimir Oltean 	    (rx_delay > SJA1105_RGMII_DELAY_MAX_PS) ||
11729ca482a2SVladimir Oltean 	    (tx_delay > SJA1105_RGMII_DELAY_MAX_PS)) {
11739ca482a2SVladimir Oltean 		dev_err(dev,
11749ca482a2SVladimir Oltean 			"port %d RGMII delay values out of range, must be between %d and %d ps\n",
11759ca482a2SVladimir Oltean 			port, SJA1105_RGMII_DELAY_MIN_PS, SJA1105_RGMII_DELAY_MAX_PS);
11769ca482a2SVladimir Oltean 		return -ERANGE;
11779ca482a2SVladimir Oltean 	}
11789ca482a2SVladimir Oltean 
11799ca482a2SVladimir Oltean 	priv->rgmii_rx_delay_ps[port] = rx_delay;
11809ca482a2SVladimir Oltean 	priv->rgmii_tx_delay_ps[port] = tx_delay;
11819ca482a2SVladimir Oltean 
1182f5b8631cSVladimir Oltean 	return 0;
1183f5b8631cSVladimir Oltean }
1184f5b8631cSVladimir Oltean 
sja1105_parse_ports_node(struct sja1105_private * priv,struct device_node * ports_node)11858aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
11868aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
11878aa9ebccSVladimir Oltean {
11888aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
11898aa9ebccSVladimir Oltean 	struct device_node *child;
11908aa9ebccSVladimir Oltean 
119127afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
11928aa9ebccSVladimir Oltean 		struct device_node *phy_node;
11930c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
11948aa9ebccSVladimir Oltean 		u32 index;
11950c65b2b9SAndrew Lunn 		int err;
11968aa9ebccSVladimir Oltean 
11978aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
11988aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
11998aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
12008aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
12017ba771e3SNishka Dasgupta 			of_node_put(child);
12028aa9ebccSVladimir Oltean 			return -ENODEV;
12038aa9ebccSVladimir Oltean 		}
12048aa9ebccSVladimir Oltean 
12058aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
12060c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
12070c65b2b9SAndrew Lunn 		if (err) {
12088aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
12098aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
12108aa9ebccSVladimir Oltean 				index);
12117ba771e3SNishka Dasgupta 			of_node_put(child);
12128aa9ebccSVladimir Oltean 			return -ENODEV;
12138aa9ebccSVladimir Oltean 		}
12148aa9ebccSVladimir Oltean 
12158aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
12168aa9ebccSVladimir Oltean 		if (!phy_node) {
12178aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
12188aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
12198aa9ebccSVladimir Oltean 					"properties missing!\n");
12207ba771e3SNishka Dasgupta 				of_node_put(child);
12218aa9ebccSVladimir Oltean 				return -ENODEV;
12228aa9ebccSVladimir Oltean 			}
12238aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
12248aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
12258aa9ebccSVladimir Oltean 			 */
122629afb83aSVladimir Oltean 			priv->fixed_link[index] = true;
12278aa9ebccSVladimir Oltean 		} else {
12288aa9ebccSVladimir Oltean 			of_node_put(phy_node);
12298aa9ebccSVladimir Oltean 		}
12308aa9ebccSVladimir Oltean 
1231bf4edf4aSVladimir Oltean 		priv->phy_mode[index] = phy_mode;
12329ca482a2SVladimir Oltean 
12339ca482a2SVladimir Oltean 		err = sja1105_parse_rgmii_delays(priv, index, child);
1234f3956e30SWan Jiabing 		if (err) {
1235f3956e30SWan Jiabing 			of_node_put(child);
12369ca482a2SVladimir Oltean 			return err;
12378aa9ebccSVladimir Oltean 		}
1238f3956e30SWan Jiabing 	}
12398aa9ebccSVladimir Oltean 
12408aa9ebccSVladimir Oltean 	return 0;
12418aa9ebccSVladimir Oltean }
12428aa9ebccSVladimir Oltean 
sja1105_parse_dt(struct sja1105_private * priv)12435d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv)
12448aa9ebccSVladimir Oltean {
12458aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
12468aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
12478aa9ebccSVladimir Oltean 	struct device_node *ports_node;
12488aa9ebccSVladimir Oltean 	int rc;
12498aa9ebccSVladimir Oltean 
12508aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
125115074a36SVladimir Oltean 	if (!ports_node)
125215074a36SVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
12538aa9ebccSVladimir Oltean 	if (!ports_node) {
12548aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
12558aa9ebccSVladimir Oltean 		return -ENODEV;
12568aa9ebccSVladimir Oltean 	}
12578aa9ebccSVladimir Oltean 
12585d645df9SVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports_node);
12598aa9ebccSVladimir Oltean 	of_node_put(ports_node);
12608aa9ebccSVladimir Oltean 
12618aa9ebccSVladimir Oltean 	return rc;
12628aa9ebccSVladimir Oltean }
12638aa9ebccSVladimir Oltean 
1264c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
sja1105_port_speed_to_ethtool(struct sja1105_private * priv,u64 speed)126541fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv,
126641fed17fSVladimir Oltean 					 u64 speed)
126741fed17fSVladimir Oltean {
126841fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS])
126941fed17fSVladimir Oltean 		return SPEED_10;
127041fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS])
127141fed17fSVladimir Oltean 		return SPEED_100;
127241fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS])
127341fed17fSVladimir Oltean 		return SPEED_1000;
127441fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS])
127541fed17fSVladimir Oltean 		return SPEED_2500;
127641fed17fSVladimir Oltean 	return SPEED_UNKNOWN;
127741fed17fSVladimir Oltean }
12788aa9ebccSVladimir Oltean 
12798400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
sja1105_adjust_port_config(struct sja1105_private * priv,int port,int speed_mbps)12808aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
12818400cff6SVladimir Oltean 				      int speed_mbps)
12828aa9ebccSVladimir Oltean {
12838aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
12848aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
128541fed17fSVladimir Oltean 	u64 speed;
12868aa9ebccSVladimir Oltean 	int rc;
12878aa9ebccSVladimir Oltean 
12888400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
12898400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
12908400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
12918400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
12928400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
12938400cff6SVladimir Oltean 	 */
12948aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
12958aa9ebccSVladimir Oltean 
1296f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
1297c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
1298a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
1299a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
1300a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
1301a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
1302a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
1303a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
1304a979a0abSVladimir Oltean 		 */
130541fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
1306f4cfcfbdSVladimir Oltean 		break;
1307c44d0535SVladimir Oltean 	case SPEED_10:
130841fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_10MBPS];
1309f4cfcfbdSVladimir Oltean 		break;
1310c44d0535SVladimir Oltean 	case SPEED_100:
131141fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_100MBPS];
1312f4cfcfbdSVladimir Oltean 		break;
1313c44d0535SVladimir Oltean 	case SPEED_1000:
131441fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
1315f4cfcfbdSVladimir Oltean 		break;
131656b63466SVladimir Oltean 	case SPEED_2500:
131756b63466SVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
131856b63466SVladimir Oltean 		break;
1319f4cfcfbdSVladimir Oltean 	default:
13208aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
13218aa9ebccSVladimir Oltean 		return -EINVAL;
13228aa9ebccSVladimir Oltean 	}
13238aa9ebccSVladimir Oltean 
13248400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
13258400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
13268400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
13278400cff6SVladimir Oltean 	 * we want auto during upload phase).
1328ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
1329ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
13308aa9ebccSVladimir Oltean 	 */
133191a05078SVladimir Oltean 	if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII)
133241fed17fSVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
133356b63466SVladimir Oltean 	else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX)
133456b63466SVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
1335ffe10e67SVladimir Oltean 	else
13368aa9ebccSVladimir Oltean 		mac[port].speed = speed;
13378aa9ebccSVladimir Oltean 
13388aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
13398400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
13408400cff6SVladimir Oltean 					  &mac[port], true);
13418aa9ebccSVladimir Oltean 	if (rc < 0) {
13428aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
13438aa9ebccSVladimir Oltean 		return rc;
13448aa9ebccSVladimir Oltean 	}
13458aa9ebccSVladimir Oltean 
13468aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
13478aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
13488aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
13498aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
13508aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
13518aa9ebccSVladimir Oltean 	 */
135291a05078SVladimir Oltean 	if (!phy_interface_mode_is_rgmii(priv->phy_mode[port]))
13538aa9ebccSVladimir Oltean 		return 0;
13548aa9ebccSVladimir Oltean 
13558aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
13568aa9ebccSVladimir Oltean }
13578aa9ebccSVladimir Oltean 
1358827b4ef2SRussell King (Oracle) static struct phylink_pcs *
sja1105_mac_select_pcs(struct dsa_switch * ds,int port,phy_interface_t iface)1359827b4ef2SRussell King (Oracle) sja1105_mac_select_pcs(struct dsa_switch *ds, int port, phy_interface_t iface)
13608aa9ebccSVladimir Oltean {
13618aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1362827b4ef2SRussell King (Oracle) 	struct dw_xpcs *xpcs = priv->xpcs[port];
1363ffe10e67SVladimir Oltean 
13643ad1d171SVladimir Oltean 	if (xpcs)
1365827b4ef2SRussell King (Oracle) 		return &xpcs->pcs;
1366827b4ef2SRussell King (Oracle) 
1367827b4ef2SRussell King (Oracle) 	return NULL;
13688400cff6SVladimir Oltean }
13698400cff6SVladimir Oltean 
sja1105_mac_link_down(struct dsa_switch * ds,int port,unsigned int mode,phy_interface_t interface)13708400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
13718400cff6SVladimir Oltean 				  unsigned int mode,
13728400cff6SVladimir Oltean 				  phy_interface_t interface)
13738400cff6SVladimir Oltean {
13748400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
13758400cff6SVladimir Oltean }
13768400cff6SVladimir Oltean 
sja1105_mac_link_up(struct dsa_switch * ds,int port,unsigned int mode,phy_interface_t interface,struct phy_device * phydev,int speed,int duplex,bool tx_pause,bool rx_pause)13778400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
13788400cff6SVladimir Oltean 				unsigned int mode,
13798400cff6SVladimir Oltean 				phy_interface_t interface,
13805b502a7bSRussell King 				struct phy_device *phydev,
13815b502a7bSRussell King 				int speed, int duplex,
13825b502a7bSRussell King 				bool tx_pause, bool rx_pause)
13838400cff6SVladimir Oltean {
1384ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1385ec8582d1SVladimir Oltean 
1386ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1387ec8582d1SVladimir Oltean 
1388ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
13898aa9ebccSVladimir Oltean }
13908aa9ebccSVladimir Oltean 
sja1105_phylink_get_caps(struct dsa_switch * ds,int port,struct phylink_config * config)1391a420b757SRussell King (Oracle) static void sja1105_phylink_get_caps(struct dsa_switch *ds, int port,
1392a420b757SRussell King (Oracle) 				     struct phylink_config *config)
1393a420b757SRussell King (Oracle) {
1394a420b757SRussell King (Oracle) 	struct sja1105_private *priv = ds->priv;
13959c318be1SRussell King (Oracle) 	struct sja1105_xmii_params_entry *mii;
139683dc4c2aSRussell King (Oracle) 	phy_interface_t phy_mode;
1397a420b757SRussell King (Oracle) 
139883dc4c2aSRussell King (Oracle) 	phy_mode = priv->phy_mode[port];
139983dc4c2aSRussell King (Oracle) 	if (phy_mode == PHY_INTERFACE_MODE_SGMII ||
140083dc4c2aSRussell King (Oracle) 	    phy_mode == PHY_INTERFACE_MODE_2500BASEX) {
140183dc4c2aSRussell King (Oracle) 		/* Changing the PHY mode on SERDES ports is possible and makes
140283dc4c2aSRussell King (Oracle) 		 * sense, because that is done through the XPCS. We allow
140383dc4c2aSRussell King (Oracle) 		 * changes between SGMII and 2500base-X.
1404a420b757SRussell King (Oracle) 		 */
140583dc4c2aSRussell King (Oracle) 		if (priv->info->supports_sgmii[port])
140683dc4c2aSRussell King (Oracle) 			__set_bit(PHY_INTERFACE_MODE_SGMII,
140783dc4c2aSRussell King (Oracle) 				  config->supported_interfaces);
140883dc4c2aSRussell King (Oracle) 
140983dc4c2aSRussell King (Oracle) 		if (priv->info->supports_2500basex[port])
141083dc4c2aSRussell King (Oracle) 			__set_bit(PHY_INTERFACE_MODE_2500BASEX,
141183dc4c2aSRussell King (Oracle) 				  config->supported_interfaces);
141283dc4c2aSRussell King (Oracle) 	} else {
141383dc4c2aSRussell King (Oracle) 		/* The SJA1105 MAC programming model is through the static
141483dc4c2aSRussell King (Oracle) 		 * config (the xMII Mode table cannot be dynamically
141583dc4c2aSRussell King (Oracle) 		 * reconfigured), and we have to program that early.
141683dc4c2aSRussell King (Oracle) 		 */
141783dc4c2aSRussell King (Oracle) 		__set_bit(phy_mode, config->supported_interfaces);
141883dc4c2aSRussell King (Oracle) 	}
1419ad9f299aSVladimir Oltean 
1420ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1421ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1422ad9f299aSVladimir Oltean 	 */
14239c318be1SRussell King (Oracle) 	config->mac_capabilities = MAC_10FD | MAC_100FD;
14249c318be1SRussell King (Oracle) 
14259c318be1SRussell King (Oracle) 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1426ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1427ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
14289c318be1SRussell King (Oracle) 		config->mac_capabilities |= MAC_1000FD;
1429ad9f299aSVladimir Oltean 
14309c318be1SRussell King (Oracle) 	if (priv->info->supports_2500basex[port])
14319c318be1SRussell King (Oracle) 		config->mac_capabilities |= MAC_2500FD;
1432ad9f299aSVladimir Oltean }
1433ad9f299aSVladimir Oltean 
143460f6053fSVladimir Oltean static int
sja1105_find_static_fdb_entry(struct sja1105_private * priv,int port,const struct sja1105_l2_lookup_entry * requested)143560f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
143660f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
143760f6053fSVladimir Oltean {
143860f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
143960f6053fSVladimir Oltean 	struct sja1105_table *table;
144060f6053fSVladimir Oltean 	int i;
144160f6053fSVladimir Oltean 
144260f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
144360f6053fSVladimir Oltean 	l2_lookup = table->entries;
144460f6053fSVladimir Oltean 
144560f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
144660f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
144760f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
144860f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
144960f6053fSVladimir Oltean 			return i;
145060f6053fSVladimir Oltean 
145160f6053fSVladimir Oltean 	return -1;
145260f6053fSVladimir Oltean }
145360f6053fSVladimir Oltean 
145460f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
145560f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
145660f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
145760f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
145860f6053fSVladimir Oltean  */
145960f6053fSVladimir Oltean static int
sja1105_static_fdb_change(struct sja1105_private * priv,int port,const struct sja1105_l2_lookup_entry * requested,bool keep)146060f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
146160f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
146260f6053fSVladimir Oltean 			  bool keep)
146360f6053fSVladimir Oltean {
146460f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
146560f6053fSVladimir Oltean 	struct sja1105_table *table;
146660f6053fSVladimir Oltean 	int rc, match;
146760f6053fSVladimir Oltean 
146860f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
146960f6053fSVladimir Oltean 
147060f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
147160f6053fSVladimir Oltean 	if (match < 0) {
147260f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
147360f6053fSVladimir Oltean 		if (!keep)
147460f6053fSVladimir Oltean 			return 0;
147560f6053fSVladimir Oltean 
147660f6053fSVladimir Oltean 		/* No match => new entry */
147760f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
147860f6053fSVladimir Oltean 		if (rc)
147960f6053fSVladimir Oltean 			return rc;
148060f6053fSVladimir Oltean 
148160f6053fSVladimir Oltean 		match = table->entry_count - 1;
148260f6053fSVladimir Oltean 	}
148360f6053fSVladimir Oltean 
148460f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
148560f6053fSVladimir Oltean 	l2_lookup = table->entries;
148660f6053fSVladimir Oltean 
148760f6053fSVladimir Oltean 	/* We have a match.
148860f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
148960f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
149060f6053fSVladimir Oltean 	 * which we update it).
149160f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
149260f6053fSVladimir Oltean 	 */
149360f6053fSVladimir Oltean 	if (keep) {
149460f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
149560f6053fSVladimir Oltean 		return 0;
149660f6053fSVladimir Oltean 	}
149760f6053fSVladimir Oltean 
149860f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
149960f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
150060f6053fSVladimir Oltean 	 */
150160f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
150260f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
150360f6053fSVladimir Oltean }
150460f6053fSVladimir Oltean 
1505291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1506291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1507291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1508291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1509291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1510291d1e72SVladimir Oltean  */
sja1105et_fdb_index(int bin,int way)151109c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1512291d1e72SVladimir Oltean {
1513291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1514291d1e72SVladimir Oltean }
1515291d1e72SVladimir Oltean 
sja1105et_is_fdb_entry_in_bin(struct sja1105_private * priv,int bin,const u8 * addr,u16 vid,struct sja1105_l2_lookup_entry * match,int * last_unused)15169dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1517291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1518291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1519291d1e72SVladimir Oltean 					 int *last_unused)
1520291d1e72SVladimir Oltean {
1521291d1e72SVladimir Oltean 	int way;
1522291d1e72SVladimir Oltean 
1523291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1524291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1525291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1526291d1e72SVladimir Oltean 
1527291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1528291d1e72SVladimir Oltean 		 * into the return value
1529291d1e72SVladimir Oltean 		 */
1530291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1531291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1532291d1e72SVladimir Oltean 			if (last_unused)
1533291d1e72SVladimir Oltean 				*last_unused = way;
1534291d1e72SVladimir Oltean 			continue;
1535291d1e72SVladimir Oltean 		}
1536291d1e72SVladimir Oltean 
1537291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1538291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1539291d1e72SVladimir Oltean 			if (match)
1540291d1e72SVladimir Oltean 				*match = l2_lookup;
1541291d1e72SVladimir Oltean 			return way;
1542291d1e72SVladimir Oltean 		}
1543291d1e72SVladimir Oltean 	}
1544291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1545291d1e72SVladimir Oltean 	return -1;
1546291d1e72SVladimir Oltean }
1547291d1e72SVladimir Oltean 
sja1105et_fdb_add(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)15489dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1549291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1550291d1e72SVladimir Oltean {
15516c5fc159SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp;
1552291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1553291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1554291d1e72SVladimir Oltean 	int last_unused = -1;
15556c5fc159SVladimir Oltean 	int start, end, i;
155660f6053fSVladimir Oltean 	int bin, way, rc;
1557291d1e72SVladimir Oltean 
15589dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1559291d1e72SVladimir Oltean 
15609dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1561291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1562291d1e72SVladimir Oltean 	if (way >= 0) {
1563291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1564291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1565291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1566291d1e72SVladimir Oltean 		 */
1567e11e865bSVladimir Oltean 		if ((l2_lookup.destports & BIT(port)) && l2_lookup.lockeds)
1568291d1e72SVladimir Oltean 			return 0;
1569291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1570291d1e72SVladimir Oltean 	} else {
1571291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1572291d1e72SVladimir Oltean 
1573291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1574291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1575291d1e72SVladimir Oltean 		 */
1576291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1577291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1578291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1579291d1e72SVladimir Oltean 
1580291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1581291d1e72SVladimir Oltean 			way = last_unused;
1582291d1e72SVladimir Oltean 		} else {
1583291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1584291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1585291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1586291d1e72SVladimir Oltean 			 * distribution function:
1587291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1588291d1e72SVladimir Oltean 			 */
1589291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1590291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1591291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1592291d1e72SVladimir Oltean 				 bin, addr, way);
1593291d1e72SVladimir Oltean 			/* Evict entry */
1594291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1595291d1e72SVladimir Oltean 						     index, NULL, false);
1596291d1e72SVladimir Oltean 		}
1597291d1e72SVladimir Oltean 	}
1598e11e865bSVladimir Oltean 	l2_lookup.lockeds = true;
1599291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1600291d1e72SVladimir Oltean 
160160f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1602291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1603291d1e72SVladimir Oltean 					  true);
160460f6053fSVladimir Oltean 	if (rc < 0)
160560f6053fSVladimir Oltean 		return rc;
160660f6053fSVladimir Oltean 
16076c5fc159SVladimir Oltean 	/* Invalidate a dynamically learned entry if that exists */
16086c5fc159SVladimir Oltean 	start = sja1105et_fdb_index(bin, 0);
16096c5fc159SVladimir Oltean 	end = sja1105et_fdb_index(bin, way);
16106c5fc159SVladimir Oltean 
16116c5fc159SVladimir Oltean 	for (i = start; i < end; i++) {
16126c5fc159SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
16136c5fc159SVladimir Oltean 						 i, &tmp);
16146c5fc159SVladimir Oltean 		if (rc == -ENOENT)
16156c5fc159SVladimir Oltean 			continue;
16166c5fc159SVladimir Oltean 		if (rc)
16176c5fc159SVladimir Oltean 			return rc;
16186c5fc159SVladimir Oltean 
16196c5fc159SVladimir Oltean 		if (tmp.macaddr != ether_addr_to_u64(addr) || tmp.vlanid != vid)
16206c5fc159SVladimir Oltean 			continue;
16216c5fc159SVladimir Oltean 
16226c5fc159SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
16236c5fc159SVladimir Oltean 						  i, NULL, false);
16246c5fc159SVladimir Oltean 		if (rc)
16256c5fc159SVladimir Oltean 			return rc;
16266c5fc159SVladimir Oltean 
16276c5fc159SVladimir Oltean 		break;
16286c5fc159SVladimir Oltean 	}
16296c5fc159SVladimir Oltean 
163060f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1631291d1e72SVladimir Oltean }
1632291d1e72SVladimir Oltean 
sja1105et_fdb_del(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)16339dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1634291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1635291d1e72SVladimir Oltean {
1636291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1637291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
163860f6053fSVladimir Oltean 	int index, bin, way, rc;
1639291d1e72SVladimir Oltean 	bool keep;
1640291d1e72SVladimir Oltean 
16419dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
16429dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1643291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1644291d1e72SVladimir Oltean 	if (way < 0)
1645291d1e72SVladimir Oltean 		return 0;
1646291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1647291d1e72SVladimir Oltean 
1648291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1649291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1650291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1651291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1652291d1e72SVladimir Oltean 	 */
1653291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
16547752e937SVladimir Oltean 
1655291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1656291d1e72SVladimir Oltean 		keep = true;
1657291d1e72SVladimir Oltean 	else
1658291d1e72SVladimir Oltean 		keep = false;
1659291d1e72SVladimir Oltean 
166060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1661291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
166260f6053fSVladimir Oltean 	if (rc < 0)
166360f6053fSVladimir Oltean 		return rc;
166460f6053fSVladimir Oltean 
166560f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1666291d1e72SVladimir Oltean }
1667291d1e72SVladimir Oltean 
sja1105pqrs_fdb_add(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)16689dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
16699dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
16709dfa6911SVladimir Oltean {
16716c5fc159SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp;
16721da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16731da73821SVladimir Oltean 	int rc, i;
16741da73821SVladimir Oltean 
16751da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
16761da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
16771da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
16781da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
16791da73821SVladimir Oltean 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
16801da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
16811da73821SVladimir Oltean 
1682728db843SVladimir Oltean 	tmp = l2_lookup;
1683728db843SVladimir Oltean 
16841da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1685728db843SVladimir Oltean 					 SJA1105_SEARCH, &tmp);
1686728db843SVladimir Oltean 	if (rc == 0 && tmp.index != SJA1105_MAX_L2_LOOKUP_COUNT - 1) {
1687e11e865bSVladimir Oltean 		/* Found a static entry and this port is already in the entry's
16881da73821SVladimir Oltean 		 * port mask => job done
16891da73821SVladimir Oltean 		 */
1690728db843SVladimir Oltean 		if ((tmp.destports & BIT(port)) && tmp.lockeds)
16911da73821SVladimir Oltean 			return 0;
1692728db843SVladimir Oltean 
1693728db843SVladimir Oltean 		l2_lookup = tmp;
1694728db843SVladimir Oltean 
16951da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
16961da73821SVladimir Oltean 		 * found something.
16971da73821SVladimir Oltean 		 */
16981da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
16991da73821SVladimir Oltean 		goto skip_finding_an_index;
17001da73821SVladimir Oltean 	}
17011da73821SVladimir Oltean 
17021da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
17031da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
17041da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
17051da73821SVladimir Oltean 	 */
17061da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
17071da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
17081da73821SVladimir Oltean 						 i, NULL);
17091da73821SVladimir Oltean 		if (rc < 0)
17101da73821SVladimir Oltean 			break;
17111da73821SVladimir Oltean 	}
17121da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
17131da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
17141da73821SVladimir Oltean 		return -EINVAL;
17151da73821SVladimir Oltean 	}
17161da73821SVladimir Oltean 	l2_lookup.index = i;
17171da73821SVladimir Oltean 
17181da73821SVladimir Oltean skip_finding_an_index:
1719e11e865bSVladimir Oltean 	l2_lookup.lockeds = true;
1720e11e865bSVladimir Oltean 
172160f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
17221da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
17231da73821SVladimir Oltean 					  true);
172460f6053fSVladimir Oltean 	if (rc < 0)
172560f6053fSVladimir Oltean 		return rc;
172660f6053fSVladimir Oltean 
17276c5fc159SVladimir Oltean 	/* The switch learns dynamic entries and looks up the FDB left to
17286c5fc159SVladimir Oltean 	 * right. It is possible that our addition was concurrent with the
17296c5fc159SVladimir Oltean 	 * dynamic learning of the same address, so now that the static entry
17306c5fc159SVladimir Oltean 	 * has been installed, we are certain that address learning for this
17316c5fc159SVladimir Oltean 	 * particular address has been turned off, so the dynamic entry either
17326c5fc159SVladimir Oltean 	 * is in the FDB at an index smaller than the static one, or isn't (it
17336c5fc159SVladimir Oltean 	 * can also be at a larger index, but in that case it is inactive
17346c5fc159SVladimir Oltean 	 * because the static FDB entry will match first, and the dynamic one
17356c5fc159SVladimir Oltean 	 * will eventually age out). Search for a dynamically learned address
17366c5fc159SVladimir Oltean 	 * prior to our static one and invalidate it.
17376c5fc159SVladimir Oltean 	 */
17386c5fc159SVladimir Oltean 	tmp = l2_lookup;
17396c5fc159SVladimir Oltean 
17406c5fc159SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
17416c5fc159SVladimir Oltean 					 SJA1105_SEARCH, &tmp);
17426c5fc159SVladimir Oltean 	if (rc < 0) {
17436c5fc159SVladimir Oltean 		dev_err(ds->dev,
17446c5fc159SVladimir Oltean 			"port %d failed to read back entry for %pM vid %d: %pe\n",
17456c5fc159SVladimir Oltean 			port, addr, vid, ERR_PTR(rc));
17466c5fc159SVladimir Oltean 		return rc;
17476c5fc159SVladimir Oltean 	}
17486c5fc159SVladimir Oltean 
17496c5fc159SVladimir Oltean 	if (tmp.index < l2_lookup.index) {
17506c5fc159SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
17516c5fc159SVladimir Oltean 						  tmp.index, NULL, false);
17526c5fc159SVladimir Oltean 		if (rc < 0)
17536c5fc159SVladimir Oltean 			return rc;
17546c5fc159SVladimir Oltean 	}
17556c5fc159SVladimir Oltean 
175660f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
17579dfa6911SVladimir Oltean }
17589dfa6911SVladimir Oltean 
sja1105pqrs_fdb_del(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)17599dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
17609dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
17619dfa6911SVladimir Oltean {
17621da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
17631da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
17641da73821SVladimir Oltean 	bool keep;
17651da73821SVladimir Oltean 	int rc;
17661da73821SVladimir Oltean 
17671da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
17681da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
17691da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
17701da73821SVladimir Oltean 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
17711da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
17721da73821SVladimir Oltean 
17731da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
17741da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
17751da73821SVladimir Oltean 	if (rc < 0)
17761da73821SVladimir Oltean 		return 0;
17771da73821SVladimir Oltean 
17781da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
17791da73821SVladimir Oltean 
17801da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
17811da73821SVladimir Oltean 	 * or if we remove it completely.
17821da73821SVladimir Oltean 	 */
17831da73821SVladimir Oltean 	if (l2_lookup.destports)
17841da73821SVladimir Oltean 		keep = true;
17851da73821SVladimir Oltean 	else
17861da73821SVladimir Oltean 		keep = false;
17871da73821SVladimir Oltean 
178860f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
17891da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
179060f6053fSVladimir Oltean 	if (rc < 0)
179160f6053fSVladimir Oltean 		return rc;
179260f6053fSVladimir Oltean 
179360f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
17949dfa6911SVladimir Oltean }
17959dfa6911SVladimir Oltean 
sja1105_fdb_add(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid,struct dsa_db db)17969dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
1797c2693363SVladimir Oltean 			   const unsigned char *addr, u16 vid,
1798c2693363SVladimir Oltean 			   struct dsa_db db)
17999dfa6911SVladimir Oltean {
18009dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1801ea32690dSVladimir Oltean 	int rc;
1802b3ee526aSVladimir Oltean 
1803219827efSVladimir Oltean 	if (!vid) {
1804219827efSVladimir Oltean 		switch (db.type) {
1805219827efSVladimir Oltean 		case DSA_DB_PORT:
1806219827efSVladimir Oltean 			vid = dsa_tag_8021q_standalone_vid(db.dp);
1807219827efSVladimir Oltean 			break;
1808219827efSVladimir Oltean 		case DSA_DB_BRIDGE:
1809219827efSVladimir Oltean 			vid = dsa_tag_8021q_bridge_vid(db.bridge.num);
1810219827efSVladimir Oltean 			break;
1811219827efSVladimir Oltean 		default:
1812219827efSVladimir Oltean 			return -EOPNOTSUPP;
1813219827efSVladimir Oltean 		}
1814219827efSVladimir Oltean 	}
1815219827efSVladimir Oltean 
1816ea32690dSVladimir Oltean 	mutex_lock(&priv->fdb_lock);
1817ea32690dSVladimir Oltean 	rc = priv->info->fdb_add_cmd(ds, port, addr, vid);
1818ea32690dSVladimir Oltean 	mutex_unlock(&priv->fdb_lock);
1819ea32690dSVladimir Oltean 
1820ea32690dSVladimir Oltean 	return rc;
18219dfa6911SVladimir Oltean }
18229dfa6911SVladimir Oltean 
__sja1105_fdb_del(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid,struct dsa_db db)1823ea32690dSVladimir Oltean static int __sja1105_fdb_del(struct dsa_switch *ds, int port,
1824c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
1825c2693363SVladimir Oltean 			     struct dsa_db db)
18269dfa6911SVladimir Oltean {
18279dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18289dfa6911SVladimir Oltean 
1829219827efSVladimir Oltean 	if (!vid) {
1830219827efSVladimir Oltean 		switch (db.type) {
1831219827efSVladimir Oltean 		case DSA_DB_PORT:
1832219827efSVladimir Oltean 			vid = dsa_tag_8021q_standalone_vid(db.dp);
1833219827efSVladimir Oltean 			break;
1834219827efSVladimir Oltean 		case DSA_DB_BRIDGE:
1835219827efSVladimir Oltean 			vid = dsa_tag_8021q_bridge_vid(db.bridge.num);
1836219827efSVladimir Oltean 			break;
1837219827efSVladimir Oltean 		default:
1838219827efSVladimir Oltean 			return -EOPNOTSUPP;
1839219827efSVladimir Oltean 		}
1840219827efSVladimir Oltean 	}
1841219827efSVladimir Oltean 
1842b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
18439dfa6911SVladimir Oltean }
18449dfa6911SVladimir Oltean 
sja1105_fdb_del(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid,struct dsa_db db)1845ea32690dSVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
1846ea32690dSVladimir Oltean 			   const unsigned char *addr, u16 vid,
1847ea32690dSVladimir Oltean 			   struct dsa_db db)
1848ea32690dSVladimir Oltean {
1849ea32690dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1850ea32690dSVladimir Oltean 	int rc;
1851ea32690dSVladimir Oltean 
1852ea32690dSVladimir Oltean 	mutex_lock(&priv->fdb_lock);
1853ea32690dSVladimir Oltean 	rc = __sja1105_fdb_del(ds, port, addr, vid, db);
1854ea32690dSVladimir Oltean 	mutex_unlock(&priv->fdb_lock);
1855ea32690dSVladimir Oltean 
1856ea32690dSVladimir Oltean 	return rc;
1857ea32690dSVladimir Oltean }
1858ea32690dSVladimir Oltean 
sja1105_fdb_dump(struct dsa_switch * ds,int port,dsa_fdb_dump_cb_t * cb,void * data)1859291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1860291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1861291d1e72SVladimir Oltean {
1862291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1863291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1864291d1e72SVladimir Oltean 	int i;
1865291d1e72SVladimir Oltean 
1866291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1867291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1868291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1869291d1e72SVladimir Oltean 		int rc;
1870291d1e72SVladimir Oltean 
1871291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1872291d1e72SVladimir Oltean 						 i, &l2_lookup);
1873291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1874def84604SVladimir Oltean 		if (rc == -ENOENT)
1875291d1e72SVladimir Oltean 			continue;
1876291d1e72SVladimir Oltean 		if (rc) {
1877291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1878291d1e72SVladimir Oltean 			return rc;
1879291d1e72SVladimir Oltean 		}
1880291d1e72SVladimir Oltean 
1881291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1882291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1883291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1884291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1885291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1886291d1e72SVladimir Oltean 		 */
1887291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1888291d1e72SVladimir Oltean 			continue;
18894d942354SVladimir Oltean 
1890291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
189193647594SVladimir Oltean 
189202c652f5SVladimir Oltean 		/* Hardware FDB is shared for fdb and mdb, "bridge fdb show"
189302c652f5SVladimir Oltean 		 * only wants to see unicast
189402c652f5SVladimir Oltean 		 */
189502c652f5SVladimir Oltean 		if (is_multicast_ether_addr(macaddr))
189602c652f5SVladimir Oltean 			continue;
189702c652f5SVladimir Oltean 
18986d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
1899219827efSVladimir Oltean 		if (vid_is_dsa_8021q(l2_lookup.vlanid))
19006d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
190121b52fedSVladimir Oltean 		rc = cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
190221b52fedSVladimir Oltean 		if (rc)
190321b52fedSVladimir Oltean 			return rc;
1904291d1e72SVladimir Oltean 	}
1905291d1e72SVladimir Oltean 	return 0;
1906291d1e72SVladimir Oltean }
1907291d1e72SVladimir Oltean 
sja1105_fast_age(struct dsa_switch * ds,int port)19085126ec72SVladimir Oltean static void sja1105_fast_age(struct dsa_switch *ds, int port)
19095126ec72SVladimir Oltean {
1910c2693363SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
19115126ec72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1912c2693363SVladimir Oltean 	struct dsa_db db = {
1913c2693363SVladimir Oltean 		.type = DSA_DB_BRIDGE,
1914c2693363SVladimir Oltean 		.bridge = {
1915c2693363SVladimir Oltean 			.dev = dsa_port_bridge_dev_get(dp),
1916c2693363SVladimir Oltean 			.num = dsa_port_bridge_num_get(dp),
1917c2693363SVladimir Oltean 		},
1918c2693363SVladimir Oltean 	};
19195126ec72SVladimir Oltean 	int i;
19205126ec72SVladimir Oltean 
1921ea32690dSVladimir Oltean 	mutex_lock(&priv->fdb_lock);
1922ea32690dSVladimir Oltean 
19235126ec72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
19245126ec72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
19255126ec72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
19265126ec72SVladimir Oltean 		int rc;
19275126ec72SVladimir Oltean 
19285126ec72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
19295126ec72SVladimir Oltean 						 i, &l2_lookup);
19305126ec72SVladimir Oltean 		/* No fdb entry at i, not an issue */
19315126ec72SVladimir Oltean 		if (rc == -ENOENT)
19325126ec72SVladimir Oltean 			continue;
19335126ec72SVladimir Oltean 		if (rc) {
19345126ec72SVladimir Oltean 			dev_err(ds->dev, "Failed to read FDB: %pe\n",
19355126ec72SVladimir Oltean 				ERR_PTR(rc));
1936ea32690dSVladimir Oltean 			break;
19375126ec72SVladimir Oltean 		}
19385126ec72SVladimir Oltean 
19395126ec72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
19405126ec72SVladimir Oltean 			continue;
19415126ec72SVladimir Oltean 
19425126ec72SVladimir Oltean 		/* Don't delete static FDB entries */
19435126ec72SVladimir Oltean 		if (l2_lookup.lockeds)
19445126ec72SVladimir Oltean 			continue;
19455126ec72SVladimir Oltean 
19465126ec72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
19475126ec72SVladimir Oltean 
1948ea32690dSVladimir Oltean 		rc = __sja1105_fdb_del(ds, port, macaddr, l2_lookup.vlanid, db);
19495126ec72SVladimir Oltean 		if (rc) {
19505126ec72SVladimir Oltean 			dev_err(ds->dev,
19515126ec72SVladimir Oltean 				"Failed to delete FDB entry %pM vid %lld: %pe\n",
19525126ec72SVladimir Oltean 				macaddr, l2_lookup.vlanid, ERR_PTR(rc));
1953ea32690dSVladimir Oltean 			break;
19545126ec72SVladimir Oltean 		}
19555126ec72SVladimir Oltean 	}
1956ea32690dSVladimir Oltean 
1957ea32690dSVladimir Oltean 	mutex_unlock(&priv->fdb_lock);
19585126ec72SVladimir Oltean }
19595126ec72SVladimir Oltean 
sja1105_mdb_add(struct dsa_switch * ds,int port,const struct switchdev_obj_port_mdb * mdb,struct dsa_db db)1960a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port,
1961c2693363SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb,
1962c2693363SVladimir Oltean 			   struct dsa_db db)
1963291d1e72SVladimir Oltean {
1964c2693363SVladimir Oltean 	return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid, db);
1965291d1e72SVladimir Oltean }
1966291d1e72SVladimir Oltean 
sja1105_mdb_del(struct dsa_switch * ds,int port,const struct switchdev_obj_port_mdb * mdb,struct dsa_db db)1967291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1968c2693363SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb,
1969c2693363SVladimir Oltean 			   struct dsa_db db)
1970291d1e72SVladimir Oltean {
1971c2693363SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid, db);
1972291d1e72SVladimir Oltean }
1973291d1e72SVladimir Oltean 
19747f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration.
19757f7ccdeaSVladimir Oltean  * Flooding is configured between each {ingress, egress} port pair, and since
19767f7ccdeaSVladimir Oltean  * the bridge's semantics are those of "egress flooding", it means we must
19777f7ccdeaSVladimir Oltean  * enable flooding towards this port from all ingress ports that are in the
19787f7ccdeaSVladimir Oltean  * same forwarding domain.
19797f7ccdeaSVladimir Oltean  */
sja1105_manage_flood_domains(struct sja1105_private * priv)19807f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv)
19817f7ccdeaSVladimir Oltean {
19827f7ccdeaSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
19837f7ccdeaSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
19847f7ccdeaSVladimir Oltean 	int from, to, rc;
19857f7ccdeaSVladimir Oltean 
19867f7ccdeaSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
19877f7ccdeaSVladimir Oltean 
19887f7ccdeaSVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
19897f7ccdeaSVladimir Oltean 		u64 fl_domain = 0, bc_domain = 0;
19907f7ccdeaSVladimir Oltean 
19917f7ccdeaSVladimir Oltean 		for (to = 0; to < priv->ds->num_ports; to++) {
19927f7ccdeaSVladimir Oltean 			if (!sja1105_can_forward(l2_fwd, from, to))
19937f7ccdeaSVladimir Oltean 				continue;
19947f7ccdeaSVladimir Oltean 
19957f7ccdeaSVladimir Oltean 			if (priv->ucast_egress_floods & BIT(to))
19967f7ccdeaSVladimir Oltean 				fl_domain |= BIT(to);
19977f7ccdeaSVladimir Oltean 			if (priv->bcast_egress_floods & BIT(to))
19987f7ccdeaSVladimir Oltean 				bc_domain |= BIT(to);
19997f7ccdeaSVladimir Oltean 		}
20007f7ccdeaSVladimir Oltean 
20017f7ccdeaSVladimir Oltean 		/* Nothing changed, nothing to do */
20027f7ccdeaSVladimir Oltean 		if (l2_fwd[from].fl_domain == fl_domain &&
20037f7ccdeaSVladimir Oltean 		    l2_fwd[from].bc_domain == bc_domain)
20047f7ccdeaSVladimir Oltean 			continue;
20057f7ccdeaSVladimir Oltean 
20067f7ccdeaSVladimir Oltean 		l2_fwd[from].fl_domain = fl_domain;
20077f7ccdeaSVladimir Oltean 		l2_fwd[from].bc_domain = bc_domain;
20087f7ccdeaSVladimir Oltean 
20097f7ccdeaSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
20107f7ccdeaSVladimir Oltean 						  from, &l2_fwd[from], true);
20117f7ccdeaSVladimir Oltean 		if (rc < 0)
20127f7ccdeaSVladimir Oltean 			return rc;
20137f7ccdeaSVladimir Oltean 	}
20147f7ccdeaSVladimir Oltean 
20157f7ccdeaSVladimir Oltean 	return 0;
20167f7ccdeaSVladimir Oltean }
20177f7ccdeaSVladimir Oltean 
sja1105_bridge_member(struct dsa_switch * ds,int port,struct dsa_bridge bridge,bool member)20188aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
2019d3eed0e5SVladimir Oltean 				 struct dsa_bridge bridge, bool member)
20208aa9ebccSVladimir Oltean {
20218aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
20228aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
20238aa9ebccSVladimir Oltean 	int i, rc;
20248aa9ebccSVladimir Oltean 
20258aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
20268aa9ebccSVladimir Oltean 
2027542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
20288aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
20298aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
20308aa9ebccSVladimir Oltean 		 */
20318aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
20328aa9ebccSVladimir Oltean 			continue;
20338aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
20348aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
20358aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
20368aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
20378aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
20388aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
20398aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
20408aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
20418aa9ebccSVladimir Oltean 		 */
20428aa9ebccSVladimir Oltean 		if (i == port)
20438aa9ebccSVladimir Oltean 			continue;
2044d3eed0e5SVladimir Oltean 		if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
20458aa9ebccSVladimir Oltean 			continue;
20468aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
20478aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
20488aa9ebccSVladimir Oltean 
20498aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
20508aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
20518aa9ebccSVladimir Oltean 		if (rc < 0)
20528aa9ebccSVladimir Oltean 			return rc;
20538aa9ebccSVladimir Oltean 	}
20548aa9ebccSVladimir Oltean 
20557f7ccdeaSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
20568aa9ebccSVladimir Oltean 					  port, &l2_fwd[port], true);
20577f7ccdeaSVladimir Oltean 	if (rc)
20587f7ccdeaSVladimir Oltean 		return rc;
20597f7ccdeaSVladimir Oltean 
2060cde8078eSVladimir Oltean 	rc = sja1105_commit_pvid(ds, port);
2061cde8078eSVladimir Oltean 	if (rc)
2062cde8078eSVladimir Oltean 		return rc;
2063cde8078eSVladimir Oltean 
20647f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
20658aa9ebccSVladimir Oltean }
20668aa9ebccSVladimir Oltean 
sja1105_bridge_stp_state_set(struct dsa_switch * ds,int port,u8 state)2067640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
2068640f763fSVladimir Oltean 					 u8 state)
2069640f763fSVladimir Oltean {
20705313a37bSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
2071640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2072640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2073640f763fSVladimir Oltean 
2074640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2075640f763fSVladimir Oltean 
2076640f763fSVladimir Oltean 	switch (state) {
2077640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
2078640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
2079640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
2080640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
2081640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
2082640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
2083640f763fSVladimir Oltean 		 */
2084640f763fSVladimir Oltean 		mac[port].ingress   = false;
2085640f763fSVladimir Oltean 		mac[port].egress    = false;
2086640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
2087640f763fSVladimir Oltean 		break;
2088640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
2089640f763fSVladimir Oltean 		mac[port].ingress   = true;
2090640f763fSVladimir Oltean 		mac[port].egress    = false;
2091640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
2092640f763fSVladimir Oltean 		break;
2093640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
2094640f763fSVladimir Oltean 		mac[port].ingress   = true;
2095640f763fSVladimir Oltean 		mac[port].egress    = false;
20965313a37bSVladimir Oltean 		mac[port].dyn_learn = dp->learning;
2097640f763fSVladimir Oltean 		break;
2098640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
2099640f763fSVladimir Oltean 		mac[port].ingress   = true;
2100640f763fSVladimir Oltean 		mac[port].egress    = true;
21015313a37bSVladimir Oltean 		mac[port].dyn_learn = dp->learning;
2102640f763fSVladimir Oltean 		break;
2103640f763fSVladimir Oltean 	default:
2104640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
2105640f763fSVladimir Oltean 		return;
2106640f763fSVladimir Oltean 	}
2107640f763fSVladimir Oltean 
2108640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
2109640f763fSVladimir Oltean 				     &mac[port], true);
2110640f763fSVladimir Oltean }
2111640f763fSVladimir Oltean 
sja1105_bridge_join(struct dsa_switch * ds,int port,struct dsa_bridge bridge,bool * tx_fwd_offload,struct netlink_ext_ack * extack)21128aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
2113b079922bSVladimir Oltean 			       struct dsa_bridge bridge,
211406b9cce4SVladimir Oltean 			       bool *tx_fwd_offload,
211506b9cce4SVladimir Oltean 			       struct netlink_ext_ack *extack)
21168aa9ebccSVladimir Oltean {
2117857fdd74SVladimir Oltean 	int rc;
2118857fdd74SVladimir Oltean 
2119857fdd74SVladimir Oltean 	rc = sja1105_bridge_member(ds, port, bridge, true);
2120857fdd74SVladimir Oltean 	if (rc)
2121857fdd74SVladimir Oltean 		return rc;
2122857fdd74SVladimir Oltean 
212391495f21SVladimir Oltean 	rc = dsa_tag_8021q_bridge_join(ds, port, bridge);
2124857fdd74SVladimir Oltean 	if (rc) {
2125857fdd74SVladimir Oltean 		sja1105_bridge_member(ds, port, bridge, false);
2126857fdd74SVladimir Oltean 		return rc;
2127857fdd74SVladimir Oltean 	}
2128857fdd74SVladimir Oltean 
2129857fdd74SVladimir Oltean 	*tx_fwd_offload = true;
2130857fdd74SVladimir Oltean 
2131857fdd74SVladimir Oltean 	return 0;
21328aa9ebccSVladimir Oltean }
21338aa9ebccSVladimir Oltean 
sja1105_bridge_leave(struct dsa_switch * ds,int port,struct dsa_bridge bridge)21348aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
2135d3eed0e5SVladimir Oltean 				 struct dsa_bridge bridge)
21368aa9ebccSVladimir Oltean {
213791495f21SVladimir Oltean 	dsa_tag_8021q_bridge_leave(ds, port, bridge);
2138d3eed0e5SVladimir Oltean 	sja1105_bridge_member(ds, port, bridge, false);
21398aa9ebccSVladimir Oltean }
21408aa9ebccSVladimir Oltean 
21414d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8)
2142180a7419SVladimir Oltean /* Port 0 (the uC port) does not have CBS shapers */
2143180a7419SVladimir Oltean #define SJA1110_FIXED_CBS(port, prio) ((((port) - 1) * SJA1105_NUM_TC) + (prio))
21444d752508SVladimir Oltean 
sja1105_find_cbs_shaper(struct sja1105_private * priv,int port,int prio)2145894cafc5SVladimir Oltean static int sja1105_find_cbs_shaper(struct sja1105_private *priv,
2146894cafc5SVladimir Oltean 				   int port, int prio)
2147894cafc5SVladimir Oltean {
2148894cafc5SVladimir Oltean 	int i;
2149894cafc5SVladimir Oltean 
2150180a7419SVladimir Oltean 	if (priv->info->fixed_cbs_mapping) {
2151180a7419SVladimir Oltean 		i = SJA1110_FIXED_CBS(port, prio);
2152180a7419SVladimir Oltean 		if (i >= 0 && i < priv->info->num_cbs_shapers)
2153180a7419SVladimir Oltean 			return i;
2154180a7419SVladimir Oltean 
2155180a7419SVladimir Oltean 		return -1;
2156180a7419SVladimir Oltean 	}
2157180a7419SVladimir Oltean 
2158894cafc5SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
2159894cafc5SVladimir Oltean 		if (priv->cbs[i].port == port && priv->cbs[i].prio == prio)
2160894cafc5SVladimir Oltean 			return i;
2161894cafc5SVladimir Oltean 
2162894cafc5SVladimir Oltean 	return -1;
2163894cafc5SVladimir Oltean }
2164894cafc5SVladimir Oltean 
sja1105_find_unused_cbs_shaper(struct sja1105_private * priv)21654d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
21664d752508SVladimir Oltean {
21674d752508SVladimir Oltean 	int i;
21684d752508SVladimir Oltean 
2169180a7419SVladimir Oltean 	if (priv->info->fixed_cbs_mapping)
2170180a7419SVladimir Oltean 		return -1;
2171180a7419SVladimir Oltean 
21724d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
21734d752508SVladimir Oltean 		if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
21744d752508SVladimir Oltean 			return i;
21754d752508SVladimir Oltean 
21764d752508SVladimir Oltean 	return -1;
21774d752508SVladimir Oltean }
21784d752508SVladimir Oltean 
sja1105_delete_cbs_shaper(struct sja1105_private * priv,int port,int prio)21794d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
21804d752508SVladimir Oltean 				     int prio)
21814d752508SVladimir Oltean {
21824d752508SVladimir Oltean 	int i;
21834d752508SVladimir Oltean 
21844d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
21854d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
21864d752508SVladimir Oltean 
21874d752508SVladimir Oltean 		if (cbs->port == port && cbs->prio == prio) {
21884d752508SVladimir Oltean 			memset(cbs, 0, sizeof(*cbs));
21894d752508SVladimir Oltean 			return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
21904d752508SVladimir Oltean 							    i, cbs, true);
21914d752508SVladimir Oltean 		}
21924d752508SVladimir Oltean 	}
21934d752508SVladimir Oltean 
21944d752508SVladimir Oltean 	return 0;
21954d752508SVladimir Oltean }
21964d752508SVladimir Oltean 
sja1105_setup_tc_cbs(struct dsa_switch * ds,int port,struct tc_cbs_qopt_offload * offload)21974d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
21984d752508SVladimir Oltean 				struct tc_cbs_qopt_offload *offload)
21994d752508SVladimir Oltean {
22004d752508SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
22014d752508SVladimir Oltean 	struct sja1105_cbs_entry *cbs;
2202954ad9bfSVladimir Oltean 	s64 port_transmit_rate_kbps;
22034d752508SVladimir Oltean 	int index;
22044d752508SVladimir Oltean 
22054d752508SVladimir Oltean 	if (!offload->enable)
22064d752508SVladimir Oltean 		return sja1105_delete_cbs_shaper(priv, port, offload->queue);
22074d752508SVladimir Oltean 
2208894cafc5SVladimir Oltean 	/* The user may be replacing an existing shaper */
2209894cafc5SVladimir Oltean 	index = sja1105_find_cbs_shaper(priv, port, offload->queue);
2210894cafc5SVladimir Oltean 	if (index < 0) {
2211894cafc5SVladimir Oltean 		/* That isn't the case - see if we can allocate a new one */
22124d752508SVladimir Oltean 		index = sja1105_find_unused_cbs_shaper(priv);
22134d752508SVladimir Oltean 		if (index < 0)
22144d752508SVladimir Oltean 			return -ENOSPC;
2215894cafc5SVladimir Oltean 	}
22164d752508SVladimir Oltean 
22174d752508SVladimir Oltean 	cbs = &priv->cbs[index];
22184d752508SVladimir Oltean 	cbs->port = port;
22194d752508SVladimir Oltean 	cbs->prio = offload->queue;
22204d752508SVladimir Oltean 	/* locredit and sendslope are negative by definition. In hardware,
22214d752508SVladimir Oltean 	 * positive values must be provided, and the negative sign is implicit.
22224d752508SVladimir Oltean 	 */
22234d752508SVladimir Oltean 	cbs->credit_hi = offload->hicredit;
22244d752508SVladimir Oltean 	cbs->credit_lo = abs(offload->locredit);
2225954ad9bfSVladimir Oltean 	/* User space is in kbits/sec, while the hardware in bytes/sec times
2226954ad9bfSVladimir Oltean 	 * link speed. Since the given offload->sendslope is good only for the
2227954ad9bfSVladimir Oltean 	 * current link speed anyway, and user space is likely to reprogram it
2228954ad9bfSVladimir Oltean 	 * when that changes, don't even bother to track the port's link speed,
2229954ad9bfSVladimir Oltean 	 * but deduce the port transmit rate from idleslope - sendslope.
2230954ad9bfSVladimir Oltean 	 */
2231954ad9bfSVladimir Oltean 	port_transmit_rate_kbps = offload->idleslope - offload->sendslope;
2232954ad9bfSVladimir Oltean 	cbs->idle_slope = div_s64(offload->idleslope * BYTES_PER_KBIT,
2233954ad9bfSVladimir Oltean 				  port_transmit_rate_kbps);
2234954ad9bfSVladimir Oltean 	cbs->send_slope = div_s64(abs(offload->sendslope * BYTES_PER_KBIT),
2235954ad9bfSVladimir Oltean 				  port_transmit_rate_kbps);
22364d752508SVladimir Oltean 	/* Convert the negative values from 64-bit 2's complement
22374d752508SVladimir Oltean 	 * to 32-bit 2's complement (for the case of 0x80000000 whose
22384d752508SVladimir Oltean 	 * negative is still negative).
22394d752508SVladimir Oltean 	 */
22404d752508SVladimir Oltean 	cbs->credit_lo &= GENMASK_ULL(31, 0);
22414d752508SVladimir Oltean 	cbs->send_slope &= GENMASK_ULL(31, 0);
22424d752508SVladimir Oltean 
22434d752508SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
22444d752508SVladimir Oltean 					    true);
22454d752508SVladimir Oltean }
22464d752508SVladimir Oltean 
sja1105_reload_cbs(struct sja1105_private * priv)22474d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv)
22484d752508SVladimir Oltean {
22494d752508SVladimir Oltean 	int rc = 0, i;
22504d752508SVladimir Oltean 
2251be7f62eeSVladimir Oltean 	/* The credit based shapers are only allocated if
2252be7f62eeSVladimir Oltean 	 * CONFIG_NET_SCH_CBS is enabled.
2253be7f62eeSVladimir Oltean 	 */
2254be7f62eeSVladimir Oltean 	if (!priv->cbs)
2255be7f62eeSVladimir Oltean 		return 0;
2256be7f62eeSVladimir Oltean 
22574d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
22584d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
22594d752508SVladimir Oltean 
22604d752508SVladimir Oltean 		if (!cbs->idle_slope && !cbs->send_slope)
22614d752508SVladimir Oltean 			continue;
22624d752508SVladimir Oltean 
22634d752508SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
22644d752508SVladimir Oltean 						  true);
22654d752508SVladimir Oltean 		if (rc)
22664d752508SVladimir Oltean 			break;
22674d752508SVladimir Oltean 	}
22684d752508SVladimir Oltean 
22694d752508SVladimir Oltean 	return rc;
22704d752508SVladimir Oltean }
22714d752508SVladimir Oltean 
22722eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
22732eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
22742eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
22752eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
2276c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
2277dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
22782eea1fa8SVladimir Oltean };
22792eea1fa8SVladimir Oltean 
22806666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
22816666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
22826666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
22836666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
22846666cebcSVladimir Oltean  * such that this operation is relatively seamless.
22856666cebcSVladimir Oltean  */
sja1105_static_config_reload(struct sja1105_private * priv,enum sja1105_reset_reason reason)22862eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
22872eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
22886666cebcSVladimir Oltean {
22896cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
22906cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
229182760d7fSVladimir Oltean 	int speed_mbps[SJA1105_MAX_NUM_PORTS];
229284db00f2SVladimir Oltean 	u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0};
22936666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
22946cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
22956cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
22966cf99c13SVladimir Oltean 	s64 t12, t34;
22976666cebcSVladimir Oltean 	int rc, i;
22986cf99c13SVladimir Oltean 	s64 now;
22996666cebcSVladimir Oltean 
2300*86899e9eSVladimir Oltean 	mutex_lock(&priv->fdb_lock);
2301af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
2302af580ae2SVladimir Oltean 
23036666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
23046666cebcSVladimir Oltean 
23058400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
23068400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
23078400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
23088400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
23096666cebcSVladimir Oltean 	 */
2310542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
231141fed17fSVladimir Oltean 		speed_mbps[i] = sja1105_port_speed_to_ethtool(priv,
231241fed17fSVladimir Oltean 							      mac[i].speed);
231341fed17fSVladimir Oltean 		mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
23146666cebcSVladimir Oltean 
23153ad1d171SVladimir Oltean 		if (priv->xpcs[i])
2316639e4b93SAndrew Lunn 			bmcr[i] = mdiobus_c45_read(priv->mdio_pcs, i,
2317639e4b93SAndrew Lunn 						   MDIO_MMD_VEND2, MDIO_CTRL1);
231884db00f2SVladimir Oltean 	}
2319ffe10e67SVladimir Oltean 
23206cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
23216cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
23226cf99c13SVladimir Oltean 
23236cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
232461c77533SVladimir Oltean 	if (rc < 0) {
232561c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
232661c77533SVladimir Oltean 		goto out;
232761c77533SVladimir Oltean 	}
23286cf99c13SVladimir Oltean 
23296666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
23306666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
233161c77533SVladimir Oltean 	if (rc < 0) {
233261c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
233361c77533SVladimir Oltean 		goto out;
233461c77533SVladimir Oltean 	}
23356cf99c13SVladimir Oltean 
23366cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
233761c77533SVladimir Oltean 	if (rc < 0) {
233861c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
233961c77533SVladimir Oltean 		goto out;
234061c77533SVladimir Oltean 	}
23416cf99c13SVladimir Oltean 
23426cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
23436cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
23446cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
23456cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
23466cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
23476cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
23486cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
23496cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
23506cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
23516cf99c13SVladimir Oltean 	now += (t34 - t12);
23526cf99c13SVladimir Oltean 
23536cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
23546cf99c13SVladimir Oltean 
23556cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
23566666cebcSVladimir Oltean 
23572eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
23582eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
23592eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
23602eea1fa8SVladimir Oltean 
23616666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
23626666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
23636666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
23646666cebcSVladimir Oltean 	 */
2365cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
2366c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
23676666cebcSVladimir Oltean 		if (rc < 0)
23686666cebcSVladimir Oltean 			goto out;
2369cb5a82d2SVladimir Oltean 	}
23706666cebcSVladimir Oltean 
2371542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
23723ad1d171SVladimir Oltean 		struct dw_xpcs *xpcs = priv->xpcs[i];
2373a3a47cfbSRussell King (Oracle) 		unsigned int neg_mode;
237484db00f2SVladimir Oltean 
23758400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
23766666cebcSVladimir Oltean 		if (rc < 0)
23776666cebcSVladimir Oltean 			goto out;
2378ffe10e67SVladimir Oltean 
23793ad1d171SVladimir Oltean 		if (!xpcs)
238084db00f2SVladimir Oltean 			continue;
2381ffe10e67SVladimir Oltean 
23823ad1d171SVladimir Oltean 		if (bmcr[i] & BMCR_ANENABLE)
2383a3a47cfbSRussell King (Oracle) 			neg_mode = PHYLINK_PCS_NEG_INBAND_ENABLED;
23843ad1d171SVladimir Oltean 		else
2385a3a47cfbSRussell King (Oracle) 			neg_mode = PHYLINK_PCS_NEG_OUTBAND;
238684db00f2SVladimir Oltean 
2387a3a47cfbSRussell King (Oracle) 		rc = xpcs_do_config(xpcs, priv->phy_mode[i], NULL, neg_mode);
23883ad1d171SVladimir Oltean 		if (rc < 0)
23893ad1d171SVladimir Oltean 			goto out;
2390ffe10e67SVladimir Oltean 
2391a3a47cfbSRussell King (Oracle) 		if (neg_mode == PHYLINK_PCS_NEG_OUTBAND) {
2392ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
2393ffe10e67SVladimir Oltean 
239456b63466SVladimir Oltean 			if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX)
239556b63466SVladimir Oltean 				speed = SPEED_2500;
239656b63466SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED1000)
2397ffe10e67SVladimir Oltean 				speed = SPEED_1000;
239884db00f2SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED100)
2399ffe10e67SVladimir Oltean 				speed = SPEED_100;
2400053d8ad1SVladimir Oltean 			else
2401ffe10e67SVladimir Oltean 				speed = SPEED_10;
2402ffe10e67SVladimir Oltean 
2403a3a47cfbSRussell King (Oracle) 			xpcs_link_up(&xpcs->pcs, neg_mode, priv->phy_mode[i],
24043ad1d171SVladimir Oltean 				     speed, DUPLEX_FULL);
2405ffe10e67SVladimir Oltean 		}
2406ffe10e67SVladimir Oltean 	}
24074d752508SVladimir Oltean 
24084d752508SVladimir Oltean 	rc = sja1105_reload_cbs(priv);
24094d752508SVladimir Oltean 	if (rc < 0)
24104d752508SVladimir Oltean 		goto out;
24116666cebcSVladimir Oltean out:
2412af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
2413*86899e9eSVladimir Oltean 	mutex_unlock(&priv->fdb_lock);
2414af580ae2SVladimir Oltean 
24156666cebcSVladimir Oltean 	return rc;
24166666cebcSVladimir Oltean }
24176666cebcSVladimir Oltean 
24188aa9ebccSVladimir Oltean static enum dsa_tag_protocol
sja1105_get_tag_protocol(struct dsa_switch * ds,int port,enum dsa_tag_protocol mp)24194d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
24204d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
24218aa9ebccSVladimir Oltean {
24224913b8ebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
24234913b8ebSVladimir Oltean 
24244913b8ebSVladimir Oltean 	return priv->info->tag_proto;
24258aa9ebccSVladimir Oltean }
24268aa9ebccSVladimir Oltean 
2427070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
2428070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
2429070ca3bbSVladimir Oltean  * So a switch reset is required.
2430070ca3bbSVladimir Oltean  */
sja1105_vlan_filtering(struct dsa_switch * ds,int port,bool enabled,struct netlink_ext_ack * extack)243189153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
243289153ed6SVladimir Oltean 			   struct netlink_ext_ack *extack)
24336666cebcSVladimir Oltean {
2434070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
24356666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2436070ca3bbSVladimir Oltean 	struct sja1105_table *table;
2437dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
2438070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
24396666cebcSVladimir Oltean 	int rc;
24406666cebcSVladimir Oltean 
2441dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
2442dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
244389153ed6SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
244489153ed6SVladimir Oltean 					   "Cannot change VLAN filtering with active VL rules");
2445dfacc5a2SVladimir Oltean 			return -EBUSY;
2446dfacc5a2SVladimir Oltean 		}
2447dfacc5a2SVladimir Oltean 	}
2448dfacc5a2SVladimir Oltean 
2449070ca3bbSVladimir Oltean 	if (enabled) {
24506666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
245154fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
245254fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
2453070ca3bbSVladimir Oltean 	} else {
24546666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
2455070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
2456070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
2457070ca3bbSVladimir Oltean 	}
2458070ca3bbSVladimir Oltean 
2459070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2460070ca3bbSVladimir Oltean 	general_params = table->entries;
2461f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
246254fa49eeSVladimir Oltean 	general_params->tpid = tpid;
246354fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2464070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
2465070ca3bbSVladimir Oltean 
24666dfd23d3SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
24676dfd23d3SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
24686dfd23d3SVladimir Oltean 			continue;
24696dfd23d3SVladimir Oltean 
24706dfd23d3SVladimir Oltean 		rc = sja1105_commit_pvid(ds, port);
2471aef31718SVladimir Oltean 		if (rc)
2472aef31718SVladimir Oltean 			return rc;
24736dfd23d3SVladimir Oltean 	}
2474aef31718SVladimir Oltean 
24752eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
24766666cebcSVladimir Oltean 	if (rc)
247789153ed6SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype");
24786666cebcSVladimir Oltean 
24790fac6aa0SVladimir Oltean 	return rc;
24806666cebcSVladimir Oltean }
24816666cebcSVladimir Oltean 
sja1105_vlan_add(struct sja1105_private * priv,int port,u16 vid,u16 flags,bool allowed_ingress)24826dfd23d3SVladimir Oltean static int sja1105_vlan_add(struct sja1105_private *priv, int port, u16 vid,
248373ceab83SVladimir Oltean 			    u16 flags, bool allowed_ingress)
24845899ee36SVladimir Oltean {
24856dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
24866dfd23d3SVladimir Oltean 	struct sja1105_table *table;
24876dfd23d3SVladimir Oltean 	int match, rc;
24885899ee36SVladimir Oltean 
24896dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
24906dfd23d3SVladimir Oltean 
24916dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
24926dfd23d3SVladimir Oltean 	if (match < 0) {
24936dfd23d3SVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
24946dfd23d3SVladimir Oltean 		if (rc)
24956dfd23d3SVladimir Oltean 			return rc;
24966dfd23d3SVladimir Oltean 		match = table->entry_count - 1;
24976dfd23d3SVladimir Oltean 	}
24986dfd23d3SVladimir Oltean 
24996dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
25006dfd23d3SVladimir Oltean 	vlan = table->entries;
25016dfd23d3SVladimir Oltean 
25026dfd23d3SVladimir Oltean 	vlan[match].type_entry = SJA1110_VLAN_D_TAG;
25036dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
25046dfd23d3SVladimir Oltean 	vlan[match].vlan_bc |= BIT(port);
250573ceab83SVladimir Oltean 
250673ceab83SVladimir Oltean 	if (allowed_ingress)
25076dfd23d3SVladimir Oltean 		vlan[match].vmemb_port |= BIT(port);
250873ceab83SVladimir Oltean 	else
250973ceab83SVladimir Oltean 		vlan[match].vmemb_port &= ~BIT(port);
251073ceab83SVladimir Oltean 
25116dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
25126dfd23d3SVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
25136dfd23d3SVladimir Oltean 	else
25146dfd23d3SVladimir Oltean 		vlan[match].tag_port |= BIT(port);
25156dfd23d3SVladimir Oltean 
25166dfd23d3SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
25176dfd23d3SVladimir Oltean 					    &vlan[match], true);
25186dfd23d3SVladimir Oltean }
25196dfd23d3SVladimir Oltean 
sja1105_vlan_del(struct sja1105_private * priv,int port,u16 vid)25206dfd23d3SVladimir Oltean static int sja1105_vlan_del(struct sja1105_private *priv, int port, u16 vid)
25216dfd23d3SVladimir Oltean {
25226dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
25236dfd23d3SVladimir Oltean 	struct sja1105_table *table;
25246dfd23d3SVladimir Oltean 	bool keep = true;
25256dfd23d3SVladimir Oltean 	int match, rc;
25266dfd23d3SVladimir Oltean 
25276dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
25286dfd23d3SVladimir Oltean 
25296dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
25306dfd23d3SVladimir Oltean 	/* Can't delete a missing entry. */
25316dfd23d3SVladimir Oltean 	if (match < 0)
25325899ee36SVladimir Oltean 		return 0;
25335899ee36SVladimir Oltean 
25346dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
25356dfd23d3SVladimir Oltean 	vlan = table->entries;
25366dfd23d3SVladimir Oltean 
25376dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
25386dfd23d3SVladimir Oltean 	vlan[match].vlan_bc &= ~BIT(port);
25396dfd23d3SVladimir Oltean 	vlan[match].vmemb_port &= ~BIT(port);
25406dfd23d3SVladimir Oltean 	/* Also unset tag_port, just so we don't have a confusing bitmap
25416dfd23d3SVladimir Oltean 	 * (no practical purpose).
2542b38e659dSVladimir Oltean 	 */
25436dfd23d3SVladimir Oltean 	vlan[match].tag_port &= ~BIT(port);
2544b38e659dSVladimir Oltean 
25456dfd23d3SVladimir Oltean 	/* If there's no port left as member of this VLAN,
25466dfd23d3SVladimir Oltean 	 * it's time for it to go.
25476dfd23d3SVladimir Oltean 	 */
25486dfd23d3SVladimir Oltean 	if (!vlan[match].vmemb_port)
25496dfd23d3SVladimir Oltean 		keep = false;
25505899ee36SVladimir Oltean 
25516dfd23d3SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
25526dfd23d3SVladimir Oltean 					  &vlan[match], keep);
25536dfd23d3SVladimir Oltean 	if (rc < 0)
25546dfd23d3SVladimir Oltean 		return rc;
25555899ee36SVladimir Oltean 
25566dfd23d3SVladimir Oltean 	if (!keep)
25576dfd23d3SVladimir Oltean 		return sja1105_table_delete_entry(table, match);
25585899ee36SVladimir Oltean 
25595899ee36SVladimir Oltean 	return 0;
25605899ee36SVladimir Oltean }
25615899ee36SVladimir Oltean 
sja1105_bridge_vlan_add(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan,struct netlink_ext_ack * extack)25626dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_add(struct dsa_switch *ds, int port,
256331046a5fSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan,
256431046a5fSVladimir Oltean 				   struct netlink_ext_ack *extack)
25656666cebcSVladimir Oltean {
25666666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2567884be12fSVladimir Oltean 	u16 flags = vlan->flags;
25686666cebcSVladimir Oltean 	int rc;
25696666cebcSVladimir Oltean 
25700fac6aa0SVladimir Oltean 	/* Be sure to deny alterations to the configuration done by tag_8021q.
25711958d581SVladimir Oltean 	 */
25720fac6aa0SVladimir Oltean 	if (vid_is_dsa_8021q(vlan->vid)) {
257331046a5fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
257404b67e18SVladimir Oltean 				   "Range 3072-4095 reserved for dsa_8021q operation");
25751958d581SVladimir Oltean 		return -EBUSY;
25761958d581SVladimir Oltean 	}
25771958d581SVladimir Oltean 
2578c5130029SVladimir Oltean 	/* Always install bridge VLANs as egress-tagged on CPU and DSA ports */
2579c5130029SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2580884be12fSVladimir Oltean 		flags = 0;
2581884be12fSVladimir Oltean 
258273ceab83SVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vlan->vid, flags, true);
25836dfd23d3SVladimir Oltean 	if (rc)
25841958d581SVladimir Oltean 		return rc;
2585ec5ae610SVladimir Oltean 
25866dfd23d3SVladimir Oltean 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
25876dfd23d3SVladimir Oltean 		priv->bridge_pvid[port] = vlan->vid;
2588ec5ae610SVladimir Oltean 
25896dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
25906666cebcSVladimir Oltean }
25916666cebcSVladimir Oltean 
sja1105_bridge_vlan_del(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan)25926dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port,
25936666cebcSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan)
25946666cebcSVladimir Oltean {
25956666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2596bef0746cSVladimir Oltean 	int rc;
25976666cebcSVladimir Oltean 
2598bef0746cSVladimir Oltean 	rc = sja1105_vlan_del(priv, port, vlan->vid);
2599bef0746cSVladimir Oltean 	if (rc)
2600bef0746cSVladimir Oltean 		return rc;
2601bef0746cSVladimir Oltean 
2602bef0746cSVladimir Oltean 	/* In case the pvid was deleted, make sure that untagged packets will
2603bef0746cSVladimir Oltean 	 * be dropped.
2604bef0746cSVladimir Oltean 	 */
2605bef0746cSVladimir Oltean 	return sja1105_commit_pvid(ds, port);
26066666cebcSVladimir Oltean }
26076666cebcSVladimir Oltean 
sja1105_dsa_8021q_vlan_add(struct dsa_switch * ds,int port,u16 vid,u16 flags)26085899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
26095899ee36SVladimir Oltean 				      u16 flags)
26105899ee36SVladimir Oltean {
26115899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
261273ceab83SVladimir Oltean 	bool allowed_ingress = true;
26135899ee36SVladimir Oltean 	int rc;
26145899ee36SVladimir Oltean 
261573ceab83SVladimir Oltean 	/* Prevent attackers from trying to inject a DSA tag from
261673ceab83SVladimir Oltean 	 * the outside world.
261773ceab83SVladimir Oltean 	 */
261873ceab83SVladimir Oltean 	if (dsa_is_user_port(ds, port))
261973ceab83SVladimir Oltean 		allowed_ingress = false;
262073ceab83SVladimir Oltean 
262173ceab83SVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vid, flags, allowed_ingress);
26226dfd23d3SVladimir Oltean 	if (rc)
26235899ee36SVladimir Oltean 		return rc;
26245899ee36SVladimir Oltean 
26256dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_PVID)
26266dfd23d3SVladimir Oltean 		priv->tag_8021q_pvid[port] = vid;
26276dfd23d3SVladimir Oltean 
26286dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
26295899ee36SVladimir Oltean }
26305899ee36SVladimir Oltean 
sja1105_dsa_8021q_vlan_del(struct dsa_switch * ds,int port,u16 vid)26315899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
26325899ee36SVladimir Oltean {
26335899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
26345899ee36SVladimir Oltean 
26356dfd23d3SVladimir Oltean 	return sja1105_vlan_del(priv, port, vid);
26365899ee36SVladimir Oltean }
26375899ee36SVladimir Oltean 
sja1105_prechangeupper(struct dsa_switch * ds,int port,struct netdev_notifier_changeupper_info * info)26384fbc08bdSVladimir Oltean static int sja1105_prechangeupper(struct dsa_switch *ds, int port,
26394fbc08bdSVladimir Oltean 				  struct netdev_notifier_changeupper_info *info)
26404fbc08bdSVladimir Oltean {
26414fbc08bdSVladimir Oltean 	struct netlink_ext_ack *extack = info->info.extack;
26424fbc08bdSVladimir Oltean 	struct net_device *upper = info->upper_dev;
264319fa937aSVladimir Oltean 	struct dsa_switch_tree *dst = ds->dst;
264419fa937aSVladimir Oltean 	struct dsa_port *dp;
26454fbc08bdSVladimir Oltean 
26464fbc08bdSVladimir Oltean 	if (is_vlan_dev(upper)) {
26474fbc08bdSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "8021q uppers are not supported");
26484fbc08bdSVladimir Oltean 		return -EBUSY;
26494fbc08bdSVladimir Oltean 	}
26504fbc08bdSVladimir Oltean 
265119fa937aSVladimir Oltean 	if (netif_is_bridge_master(upper)) {
265219fa937aSVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
265341fb0cf1SVladimir Oltean 			struct net_device *br = dsa_port_bridge_dev_get(dp);
265441fb0cf1SVladimir Oltean 
265541fb0cf1SVladimir Oltean 			if (br && br != upper && br_vlan_enabled(br)) {
265619fa937aSVladimir Oltean 				NL_SET_ERR_MSG_MOD(extack,
265719fa937aSVladimir Oltean 						   "Only one VLAN-aware bridge is supported");
265819fa937aSVladimir Oltean 				return -EBUSY;
265919fa937aSVladimir Oltean 			}
266019fa937aSVladimir Oltean 		}
266119fa937aSVladimir Oltean 	}
266219fa937aSVladimir Oltean 
26634fbc08bdSVladimir Oltean 	return 0;
26644fbc08bdSVladimir Oltean }
26654fbc08bdSVladimir Oltean 
sja1105_mgmt_xmit(struct dsa_switch * ds,int port,int slot,struct sk_buff * skb,bool takets)2666227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
266747ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2668227d07a0SVladimir Oltean {
2669227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2670227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2671227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2672227d07a0SVladimir Oltean 	int timeout = 10;
2673227d07a0SVladimir Oltean 	int rc;
2674227d07a0SVladimir Oltean 
2675227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2676227d07a0SVladimir Oltean 
2677227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2678227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2679227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
268047ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
268147ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2682227d07a0SVladimir Oltean 
2683227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2684227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2685227d07a0SVladimir Oltean 	if (rc < 0) {
2686227d07a0SVladimir Oltean 		kfree_skb(skb);
2687227d07a0SVladimir Oltean 		return rc;
2688227d07a0SVladimir Oltean 	}
2689227d07a0SVladimir Oltean 
2690227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
269168bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2692227d07a0SVladimir Oltean 
2693227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2694227d07a0SVladimir Oltean 	do {
2695227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2696227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2697227d07a0SVladimir Oltean 		if (rc < 0) {
2698227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2699227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2700227d07a0SVladimir Oltean 			continue;
2701227d07a0SVladimir Oltean 		}
2702227d07a0SVladimir Oltean 
2703227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2704227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2705227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2706227d07a0SVladimir Oltean 		 */
2707227d07a0SVladimir Oltean 		cpu_relax();
2708227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2709227d07a0SVladimir Oltean 
2710227d07a0SVladimir Oltean 	if (!timeout) {
2711227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2712227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
27132a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
27142a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2715227d07a0SVladimir Oltean 		 */
2716227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2717227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2718227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2719227d07a0SVladimir Oltean 	}
2720227d07a0SVladimir Oltean 
2721227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2722227d07a0SVladimir Oltean }
2723227d07a0SVladimir Oltean 
2724d38049bbSVladimir Oltean #define work_to_xmit_work(w) \
2725d38049bbSVladimir Oltean 		container_of((w), struct sja1105_deferred_xmit_work, work)
2726a68578c2SVladimir Oltean 
2727227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2728227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2729227d07a0SVladimir Oltean  * lock on the bus)
2730227d07a0SVladimir Oltean  */
sja1105_port_deferred_xmit(struct kthread_work * work)2731a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2732227d07a0SVladimir Oltean {
2733d38049bbSVladimir Oltean 	struct sja1105_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
2734d38049bbSVladimir Oltean 	struct sk_buff *clone, *skb = xmit_work->skb;
2735d38049bbSVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
2736d38049bbSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2737d38049bbSVladimir Oltean 	int port = xmit_work->dp->index;
2738a68578c2SVladimir Oltean 
2739d38049bbSVladimir Oltean 	clone = SJA1105_SKB_CB(skb)->clone;
2740227d07a0SVladimir Oltean 
2741227d07a0SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
2742227d07a0SVladimir Oltean 
2743d38049bbSVladimir Oltean 	sja1105_mgmt_xmit(ds, port, 0, skb, !!clone);
2744a68578c2SVladimir Oltean 
274547ed985eSVladimir Oltean 	/* The clone, if there, was made by dsa_skb_tx_timestamp */
2746a68578c2SVladimir Oltean 	if (clone)
2747d38049bbSVladimir Oltean 		sja1105_ptp_txtstamp_skb(ds, port, clone);
2748227d07a0SVladimir Oltean 
2749227d07a0SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
2750d38049bbSVladimir Oltean 
2751d38049bbSVladimir Oltean 	kfree(xmit_work);
27528aa9ebccSVladimir Oltean }
27538aa9ebccSVladimir Oltean 
sja1105_connect_tag_protocol(struct dsa_switch * ds,enum dsa_tag_protocol proto)2754c79e8486SVladimir Oltean static int sja1105_connect_tag_protocol(struct dsa_switch *ds,
2755c79e8486SVladimir Oltean 					enum dsa_tag_protocol proto)
2756c79e8486SVladimir Oltean {
2757c8a2a011SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2758c79e8486SVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
2759c79e8486SVladimir Oltean 
2760c8a2a011SVladimir Oltean 	if (proto != priv->info->tag_proto)
2761c8a2a011SVladimir Oltean 		return -EPROTONOSUPPORT;
2762c8a2a011SVladimir Oltean 
2763c79e8486SVladimir Oltean 	tagger_data = sja1105_tagger_data(ds);
2764c79e8486SVladimir Oltean 	tagger_data->xmit_work_fn = sja1105_port_deferred_xmit;
2765fcbf979aSVladimir Oltean 	tagger_data->meta_tstamp_handler = sja1110_process_meta_tstamp;
2766c8a2a011SVladimir Oltean 
2767c79e8486SVladimir Oltean 	return 0;
2768c79e8486SVladimir Oltean }
2769c79e8486SVladimir Oltean 
27708456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
27718456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
27728456721dSVladimir Oltean  */
sja1105_set_ageing_time(struct dsa_switch * ds,unsigned int ageing_time)27738456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
27748456721dSVladimir Oltean 				   unsigned int ageing_time)
27758456721dSVladimir Oltean {
27768456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
27778456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
27788456721dSVladimir Oltean 	struct sja1105_table *table;
27798456721dSVladimir Oltean 	unsigned int maxage;
27808456721dSVladimir Oltean 
27818456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
27828456721dSVladimir Oltean 	l2_lookup_params = table->entries;
27838456721dSVladimir Oltean 
27848456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
27858456721dSVladimir Oltean 
27868456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
27878456721dSVladimir Oltean 		return 0;
27888456721dSVladimir Oltean 
27898456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
27908456721dSVladimir Oltean 
27912eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
27928456721dSVladimir Oltean }
27938456721dSVladimir Oltean 
sja1105_change_mtu(struct dsa_switch * ds,int port,int new_mtu)2794c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
2795c279c726SVladimir Oltean {
2796c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2797c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2798c279c726SVladimir Oltean 
2799c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
2800c279c726SVladimir Oltean 
2801777e55e3SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2802c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
2803c279c726SVladimir Oltean 
2804c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2805c279c726SVladimir Oltean 
2806a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
2807c279c726SVladimir Oltean 		return 0;
2808c279c726SVladimir Oltean 
2809a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
2810c279c726SVladimir Oltean 
2811c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2812c279c726SVladimir Oltean }
2813c279c726SVladimir Oltean 
sja1105_get_max_mtu(struct dsa_switch * ds,int port)2814c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
2815c279c726SVladimir Oltean {
2816c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
2817c279c726SVladimir Oltean }
2818c279c726SVladimir Oltean 
sja1105_port_setup_tc(struct dsa_switch * ds,int port,enum tc_setup_type type,void * type_data)2819317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2820317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2821317ab5b8SVladimir Oltean 				 void *type_data)
2822317ab5b8SVladimir Oltean {
2823317ab5b8SVladimir Oltean 	switch (type) {
2824317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2825317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
28264d752508SVladimir Oltean 	case TC_SETUP_QDISC_CBS:
28274d752508SVladimir Oltean 		return sja1105_setup_tc_cbs(ds, port, type_data);
2828317ab5b8SVladimir Oltean 	default:
2829317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2830317ab5b8SVladimir Oltean 	}
2831317ab5b8SVladimir Oltean }
2832317ab5b8SVladimir Oltean 
2833511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2834511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2835511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2836511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2837511e6ca0SVladimir Oltean  * mirroring rule that references it.
2838511e6ca0SVladimir Oltean  */
sja1105_mirror_apply(struct sja1105_private * priv,int from,int to,bool ingress,bool enabled)2839511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2840511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2841511e6ca0SVladimir Oltean {
2842511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2843511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2844542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2845511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2846511e6ca0SVladimir Oltean 	bool already_enabled;
2847511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2848511e6ca0SVladimir Oltean 	int rc;
2849511e6ca0SVladimir Oltean 
2850511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2851511e6ca0SVladimir Oltean 	general_params = table->entries;
2852511e6ca0SVladimir Oltean 
2853511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2854511e6ca0SVladimir Oltean 
2855542043e9SVladimir Oltean 	already_enabled = (general_params->mirr_port != ds->num_ports);
2856511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2857511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2858511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2859511e6ca0SVladimir Oltean 			general_params->mirr_port);
2860511e6ca0SVladimir Oltean 		return -EBUSY;
2861511e6ca0SVladimir Oltean 	}
2862511e6ca0SVladimir Oltean 
2863511e6ca0SVladimir Oltean 	new_mirr_port = to;
2864511e6ca0SVladimir Oltean 	if (!enabled) {
2865511e6ca0SVladimir Oltean 		bool keep = false;
2866511e6ca0SVladimir Oltean 		int port;
2867511e6ca0SVladimir Oltean 
2868511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2869542043e9SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
2870511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2871511e6ca0SVladimir Oltean 				keep = true;
2872511e6ca0SVladimir Oltean 				break;
2873511e6ca0SVladimir Oltean 			}
2874511e6ca0SVladimir Oltean 		}
2875511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2876511e6ca0SVladimir Oltean 		if (!keep)
2877542043e9SVladimir Oltean 			new_mirr_port = ds->num_ports;
2878511e6ca0SVladimir Oltean 	}
2879511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2880511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2881511e6ca0SVladimir Oltean 
2882511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2883511e6ca0SVladimir Oltean 						  0, general_params, true);
2884511e6ca0SVladimir Oltean 		if (rc < 0)
2885511e6ca0SVladimir Oltean 			return rc;
2886511e6ca0SVladimir Oltean 	}
2887511e6ca0SVladimir Oltean 
2888511e6ca0SVladimir Oltean 	if (ingress)
2889511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2890511e6ca0SVladimir Oltean 	else
2891511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2892511e6ca0SVladimir Oltean 
2893511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2894511e6ca0SVladimir Oltean 					    &mac[from], true);
2895511e6ca0SVladimir Oltean }
2896511e6ca0SVladimir Oltean 
sja1105_mirror_add(struct dsa_switch * ds,int port,struct dsa_mall_mirror_tc_entry * mirror,bool ingress,struct netlink_ext_ack * extack)2897511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2898511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
28990148bb50SVladimir Oltean 			      bool ingress, struct netlink_ext_ack *extack)
2900511e6ca0SVladimir Oltean {
2901511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2902511e6ca0SVladimir Oltean 				    ingress, true);
2903511e6ca0SVladimir Oltean }
2904511e6ca0SVladimir Oltean 
sja1105_mirror_del(struct dsa_switch * ds,int port,struct dsa_mall_mirror_tc_entry * mirror)2905511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2906511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2907511e6ca0SVladimir Oltean {
2908511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2909511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2910511e6ca0SVladimir Oltean }
2911511e6ca0SVladimir Oltean 
sja1105_port_policer_add(struct dsa_switch * ds,int port,struct dsa_mall_policer_tc_entry * policer)2912a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
2913a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
2914a7cc081cSVladimir Oltean {
2915a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2916a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2917a7cc081cSVladimir Oltean 
2918a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2919a7cc081cSVladimir Oltean 
2920a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
2921a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
2922a7cc081cSVladimir Oltean 	 * bytes.
2923a7cc081cSVladimir Oltean 	 */
2924a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
2925a7cc081cSVladimir Oltean 				      1000000);
29265f035af7SPo Liu 	policing[port].smax = policer->burst;
2927a7cc081cSVladimir Oltean 
2928a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2929a7cc081cSVladimir Oltean }
2930a7cc081cSVladimir Oltean 
sja1105_port_policer_del(struct dsa_switch * ds,int port)2931a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
2932a7cc081cSVladimir Oltean {
2933a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2934a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2935a7cc081cSVladimir Oltean 
2936a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2937a7cc081cSVladimir Oltean 
2938a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
2939a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
2940a7cc081cSVladimir Oltean 
2941a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2942a7cc081cSVladimir Oltean }
2943a7cc081cSVladimir Oltean 
sja1105_port_set_learning(struct sja1105_private * priv,int port,bool enabled)29444d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port,
29454d942354SVladimir Oltean 				     bool enabled)
29464d942354SVladimir Oltean {
29474d942354SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
29484d942354SVladimir Oltean 
29494d942354SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
29504d942354SVladimir Oltean 
29514c44fc5eSVladimir Oltean 	mac[port].dyn_learn = enabled;
29524d942354SVladimir Oltean 
29535313a37bSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
29544d942354SVladimir Oltean 					    &mac[port], true);
29554d942354SVladimir Oltean }
29564d942354SVladimir Oltean 
sja1105_port_ucast_bcast_flood(struct sja1105_private * priv,int to,struct switchdev_brport_flags flags)29574d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to,
29584d942354SVladimir Oltean 					  struct switchdev_brport_flags flags)
29594d942354SVladimir Oltean {
29604d942354SVladimir Oltean 	if (flags.mask & BR_FLOOD) {
29614d942354SVladimir Oltean 		if (flags.val & BR_FLOOD)
29627f7ccdeaSVladimir Oltean 			priv->ucast_egress_floods |= BIT(to);
29634d942354SVladimir Oltean 		else
29646a5166e0SVladimir Oltean 			priv->ucast_egress_floods &= ~BIT(to);
29654d942354SVladimir Oltean 	}
29667f7ccdeaSVladimir Oltean 
29674d942354SVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD) {
29684d942354SVladimir Oltean 		if (flags.val & BR_BCAST_FLOOD)
29697f7ccdeaSVladimir Oltean 			priv->bcast_egress_floods |= BIT(to);
29704d942354SVladimir Oltean 		else
29716a5166e0SVladimir Oltean 			priv->bcast_egress_floods &= ~BIT(to);
29724d942354SVladimir Oltean 	}
29734d942354SVladimir Oltean 
29747f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
29754d942354SVladimir Oltean }
29764d942354SVladimir Oltean 
sja1105_port_mcast_flood(struct sja1105_private * priv,int to,struct switchdev_brport_flags flags,struct netlink_ext_ack * extack)29774d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to,
29784d942354SVladimir Oltean 				    struct switchdev_brport_flags flags,
29794d942354SVladimir Oltean 				    struct netlink_ext_ack *extack)
29804d942354SVladimir Oltean {
29814d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
29824d942354SVladimir Oltean 	struct sja1105_table *table;
2983ea32690dSVladimir Oltean 	int match, rc;
2984ea32690dSVladimir Oltean 
2985ea32690dSVladimir Oltean 	mutex_lock(&priv->fdb_lock);
29864d942354SVladimir Oltean 
29874d942354SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
29884d942354SVladimir Oltean 	l2_lookup = table->entries;
29894d942354SVladimir Oltean 
29904d942354SVladimir Oltean 	for (match = 0; match < table->entry_count; match++)
29914d942354SVladimir Oltean 		if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST &&
29924d942354SVladimir Oltean 		    l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
29934d942354SVladimir Oltean 			break;
29944d942354SVladimir Oltean 
29954d942354SVladimir Oltean 	if (match == table->entry_count) {
29964d942354SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
29974d942354SVladimir Oltean 				   "Could not find FDB entry for unknown multicast");
2998ea32690dSVladimir Oltean 		rc = -ENOSPC;
2999ea32690dSVladimir Oltean 		goto out;
30004d942354SVladimir Oltean 	}
30014d942354SVladimir Oltean 
30024d942354SVladimir Oltean 	if (flags.val & BR_MCAST_FLOOD)
30034d942354SVladimir Oltean 		l2_lookup[match].destports |= BIT(to);
30044d942354SVladimir Oltean 	else
30054d942354SVladimir Oltean 		l2_lookup[match].destports &= ~BIT(to);
30064d942354SVladimir Oltean 
3007ea32690dSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
30084d942354SVladimir Oltean 					  l2_lookup[match].index,
3009ea32690dSVladimir Oltean 					  &l2_lookup[match], true);
3010ea32690dSVladimir Oltean out:
3011ea32690dSVladimir Oltean 	mutex_unlock(&priv->fdb_lock);
3012ea32690dSVladimir Oltean 
3013ea32690dSVladimir Oltean 	return rc;
30144d942354SVladimir Oltean }
30154d942354SVladimir Oltean 
sja1105_port_pre_bridge_flags(struct dsa_switch * ds,int port,struct switchdev_brport_flags flags,struct netlink_ext_ack * extack)30164d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port,
30174d942354SVladimir Oltean 					 struct switchdev_brport_flags flags,
30184d942354SVladimir Oltean 					 struct netlink_ext_ack *extack)
30194d942354SVladimir Oltean {
30204d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
30214d942354SVladimir Oltean 
30224d942354SVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
30234d942354SVladimir Oltean 			   BR_BCAST_FLOOD))
30244d942354SVladimir Oltean 		return -EINVAL;
30254d942354SVladimir Oltean 
30264d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) &&
30274d942354SVladimir Oltean 	    !priv->info->can_limit_mcast_flood) {
30284d942354SVladimir Oltean 		bool multicast = !!(flags.val & BR_MCAST_FLOOD);
30294d942354SVladimir Oltean 		bool unicast = !!(flags.val & BR_FLOOD);
30304d942354SVladimir Oltean 
30314d942354SVladimir Oltean 		if (unicast != multicast) {
30324d942354SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
30334d942354SVladimir Oltean 					   "This chip cannot configure multicast flooding independently of unicast");
30344d942354SVladimir Oltean 			return -EINVAL;
30354d942354SVladimir Oltean 		}
30364d942354SVladimir Oltean 	}
30374d942354SVladimir Oltean 
30384d942354SVladimir Oltean 	return 0;
30394d942354SVladimir Oltean }
30404d942354SVladimir Oltean 
sja1105_port_bridge_flags(struct dsa_switch * ds,int port,struct switchdev_brport_flags flags,struct netlink_ext_ack * extack)30414d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port,
30424d942354SVladimir Oltean 				     struct switchdev_brport_flags flags,
30434d942354SVladimir Oltean 				     struct netlink_ext_ack *extack)
30444d942354SVladimir Oltean {
30454d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
30464d942354SVladimir Oltean 	int rc;
30474d942354SVladimir Oltean 
30484d942354SVladimir Oltean 	if (flags.mask & BR_LEARNING) {
30494d942354SVladimir Oltean 		bool learn_ena = !!(flags.val & BR_LEARNING);
30504d942354SVladimir Oltean 
30514d942354SVladimir Oltean 		rc = sja1105_port_set_learning(priv, port, learn_ena);
30524d942354SVladimir Oltean 		if (rc)
30534d942354SVladimir Oltean 			return rc;
30544d942354SVladimir Oltean 	}
30554d942354SVladimir Oltean 
30564d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) {
30574d942354SVladimir Oltean 		rc = sja1105_port_ucast_bcast_flood(priv, port, flags);
30584d942354SVladimir Oltean 		if (rc)
30594d942354SVladimir Oltean 			return rc;
30604d942354SVladimir Oltean 	}
30614d942354SVladimir Oltean 
30624d942354SVladimir Oltean 	/* For chips that can't offload BR_MCAST_FLOOD independently, there
30634d942354SVladimir Oltean 	 * is nothing to do here, we ensured the configuration is in sync by
30644d942354SVladimir Oltean 	 * offloading BR_FLOOD.
30654d942354SVladimir Oltean 	 */
30664d942354SVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) {
30674d942354SVladimir Oltean 		rc = sja1105_port_mcast_flood(priv, port, flags,
30684d942354SVladimir Oltean 					      extack);
30694d942354SVladimir Oltean 		if (rc)
30704d942354SVladimir Oltean 			return rc;
30714d942354SVladimir Oltean 	}
30724d942354SVladimir Oltean 
30734d942354SVladimir Oltean 	return 0;
30744d942354SVladimir Oltean }
30754d942354SVladimir Oltean 
3076022522acSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
3077022522acSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
3078022522acSVladimir Oltean  * but not the xMII mode parameters table.
3079022522acSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
3080022522acSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
3081022522acSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
3082022522acSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
3083022522acSVladimir Oltean  * Setting correct PHY link speed does not matter now.
3084022522acSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
3085022522acSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
3086022522acSVladimir Oltean  * can populate the xMII mode parameters table.
3087022522acSVladimir Oltean  */
sja1105_setup(struct dsa_switch * ds)3088022522acSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
3089022522acSVladimir Oltean {
3090022522acSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3091022522acSVladimir Oltean 	int rc;
3092022522acSVladimir Oltean 
3093022522acSVladimir Oltean 	if (priv->info->disable_microcontroller) {
3094022522acSVladimir Oltean 		rc = priv->info->disable_microcontroller(priv);
3095022522acSVladimir Oltean 		if (rc < 0) {
3096022522acSVladimir Oltean 			dev_err(ds->dev,
3097022522acSVladimir Oltean 				"Failed to disable microcontroller: %pe\n",
3098022522acSVladimir Oltean 				ERR_PTR(rc));
3099022522acSVladimir Oltean 			return rc;
3100022522acSVladimir Oltean 		}
3101022522acSVladimir Oltean 	}
3102022522acSVladimir Oltean 
3103022522acSVladimir Oltean 	/* Create and send configuration down to device */
3104022522acSVladimir Oltean 	rc = sja1105_static_config_load(priv);
3105022522acSVladimir Oltean 	if (rc < 0) {
3106022522acSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
3107022522acSVladimir Oltean 		return rc;
3108022522acSVladimir Oltean 	}
3109022522acSVladimir Oltean 
3110022522acSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
3111022522acSVladimir Oltean 	if (priv->info->clocking_setup) {
3112022522acSVladimir Oltean 		rc = priv->info->clocking_setup(priv);
3113022522acSVladimir Oltean 		if (rc < 0) {
3114022522acSVladimir Oltean 			dev_err(ds->dev,
3115022522acSVladimir Oltean 				"Failed to configure MII clocking: %pe\n",
3116022522acSVladimir Oltean 				ERR_PTR(rc));
3117022522acSVladimir Oltean 			goto out_static_config_free;
3118022522acSVladimir Oltean 		}
3119022522acSVladimir Oltean 	}
3120022522acSVladimir Oltean 
3121022522acSVladimir Oltean 	sja1105_tas_setup(ds);
3122022522acSVladimir Oltean 	sja1105_flower_setup(ds);
3123022522acSVladimir Oltean 
3124022522acSVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
3125022522acSVladimir Oltean 	if (rc < 0) {
3126022522acSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
3127022522acSVladimir Oltean 		goto out_flower_teardown;
3128022522acSVladimir Oltean 	}
3129022522acSVladimir Oltean 
3130022522acSVladimir Oltean 	rc = sja1105_mdiobus_register(ds);
3131022522acSVladimir Oltean 	if (rc < 0) {
3132022522acSVladimir Oltean 		dev_err(ds->dev, "Failed to register MDIO bus: %pe\n",
3133022522acSVladimir Oltean 			ERR_PTR(rc));
3134022522acSVladimir Oltean 		goto out_ptp_clock_unregister;
3135022522acSVladimir Oltean 	}
3136022522acSVladimir Oltean 
3137022522acSVladimir Oltean 	rc = sja1105_devlink_setup(ds);
3138022522acSVladimir Oltean 	if (rc < 0)
3139022522acSVladimir Oltean 		goto out_mdiobus_unregister;
3140022522acSVladimir Oltean 
3141022522acSVladimir Oltean 	rtnl_lock();
3142022522acSVladimir Oltean 	rc = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q));
3143022522acSVladimir Oltean 	rtnl_unlock();
3144022522acSVladimir Oltean 	if (rc)
3145022522acSVladimir Oltean 		goto out_devlink_teardown;
3146022522acSVladimir Oltean 
3147022522acSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
3148022522acSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
3149022522acSVladimir Oltean 	 * EtherType is.
3150022522acSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
3151022522acSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
3152022522acSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
3153022522acSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
3154022522acSVladimir Oltean 	 */
3155022522acSVladimir Oltean 	ds->vlan_filtering_is_global = true;
3156022522acSVladimir Oltean 	ds->untag_bridge_pvid = true;
3157219827efSVladimir Oltean 	ds->fdb_isolation = true;
3158022522acSVladimir Oltean 	/* tag_8021q has 3 bits for the VBID, and the value 0 is reserved */
3159947c8746SVladimir Oltean 	ds->max_num_bridges = 7;
3160022522acSVladimir Oltean 
3161022522acSVladimir Oltean 	/* Advertise the 8 egress queues */
3162022522acSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
3163022522acSVladimir Oltean 
3164022522acSVladimir Oltean 	ds->mtu_enforcement_ingress = true;
3165022522acSVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
3166022522acSVladimir Oltean 
3167022522acSVladimir Oltean 	return 0;
3168022522acSVladimir Oltean 
3169022522acSVladimir Oltean out_devlink_teardown:
3170022522acSVladimir Oltean 	sja1105_devlink_teardown(ds);
3171022522acSVladimir Oltean out_mdiobus_unregister:
3172022522acSVladimir Oltean 	sja1105_mdiobus_unregister(ds);
3173022522acSVladimir Oltean out_ptp_clock_unregister:
3174022522acSVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
3175022522acSVladimir Oltean out_flower_teardown:
3176022522acSVladimir Oltean 	sja1105_flower_teardown(ds);
3177022522acSVladimir Oltean 	sja1105_tas_teardown(ds);
3178022522acSVladimir Oltean out_static_config_free:
3179022522acSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
3180022522acSVladimir Oltean 
3181022522acSVladimir Oltean 	return rc;
3182022522acSVladimir Oltean }
3183022522acSVladimir Oltean 
sja1105_teardown(struct dsa_switch * ds)3184022522acSVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
3185022522acSVladimir Oltean {
3186022522acSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
3187022522acSVladimir Oltean 
3188022522acSVladimir Oltean 	rtnl_lock();
3189022522acSVladimir Oltean 	dsa_tag_8021q_unregister(ds);
3190022522acSVladimir Oltean 	rtnl_unlock();
3191022522acSVladimir Oltean 
3192022522acSVladimir Oltean 	sja1105_devlink_teardown(ds);
3193022522acSVladimir Oltean 	sja1105_mdiobus_unregister(ds);
3194022522acSVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
3195022522acSVladimir Oltean 	sja1105_flower_teardown(ds);
3196022522acSVladimir Oltean 	sja1105_tas_teardown(ds);
3197022522acSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
3198022522acSVladimir Oltean }
3199022522acSVladimir Oltean 
3200f5aef424SVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
32018aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
3202c79e8486SVladimir Oltean 	.connect_tag_protocol	= sja1105_connect_tag_protocol,
32038aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
3204f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
32058456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
3206c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
3207c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
3208a420b757SRussell King (Oracle) 	.phylink_get_caps	= sja1105_phylink_get_caps,
3209827b4ef2SRussell King (Oracle) 	.phylink_mac_select_pcs	= sja1105_mac_select_pcs,
32108400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
32118400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
321252c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
321352c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
321452c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
3215bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
3216291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
3217291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
3218291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
32195126ec72SVladimir Oltean 	.port_fast_age		= sja1105_fast_age,
32208aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
32218aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
32224d942354SVladimir Oltean 	.port_pre_bridge_flags	= sja1105_port_pre_bridge_flags,
32234d942354SVladimir Oltean 	.port_bridge_flags	= sja1105_port_bridge_flags,
3224640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
32256666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
32266dfd23d3SVladimir Oltean 	.port_vlan_add		= sja1105_bridge_vlan_add,
32276dfd23d3SVladimir Oltean 	.port_vlan_del		= sja1105_bridge_vlan_del,
3228291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
3229291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
3230a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
3231a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
3232f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
323347ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
3234317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
3235511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
3236511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
3237a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
3238a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
3239a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
3240a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
3241834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
3242ff4cf8eaSVladimir Oltean 	.devlink_info_get	= sja1105_devlink_info_get,
32435da11eb4SVladimir Oltean 	.tag_8021q_vlan_add	= sja1105_dsa_8021q_vlan_add,
32445da11eb4SVladimir Oltean 	.tag_8021q_vlan_del	= sja1105_dsa_8021q_vlan_del,
32454fbc08bdSVladimir Oltean 	.port_prechangeupper	= sja1105_prechangeupper,
32468aa9ebccSVladimir Oltean };
32478aa9ebccSVladimir Oltean 
32480b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[];
32490b0e2997SVladimir Oltean 
sja1105_check_device_id(struct sja1105_private * priv)32508aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
32518aa9ebccSVladimir Oltean {
32528aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
32538aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
32548aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
32550b0e2997SVladimir Oltean 	const struct of_device_id *match;
3256dff79620SVladimir Oltean 	u32 device_id;
32578aa9ebccSVladimir Oltean 	u64 part_no;
32588aa9ebccSVladimir Oltean 	int rc;
32598aa9ebccSVladimir Oltean 
326034d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
326134d76e9fSVladimir Oltean 			      NULL);
32628aa9ebccSVladimir Oltean 	if (rc < 0)
32638aa9ebccSVladimir Oltean 		return rc;
32648aa9ebccSVladimir Oltean 
32651bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
32661bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
32678aa9ebccSVladimir Oltean 	if (rc < 0)
32688aa9ebccSVladimir Oltean 		return rc;
32698aa9ebccSVladimir Oltean 
32708aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
32718aa9ebccSVladimir Oltean 
32725978fac0SNathan Chancellor 	for (match = sja1105_dt_ids; match->compatible[0]; match++) {
32730b0e2997SVladimir Oltean 		const struct sja1105_info *info = match->data;
32740b0e2997SVladimir Oltean 
32750b0e2997SVladimir Oltean 		/* Is what's been probed in our match table at all? */
32760b0e2997SVladimir Oltean 		if (info->device_id != device_id || info->part_no != part_no)
32770b0e2997SVladimir Oltean 			continue;
32780b0e2997SVladimir Oltean 
32790b0e2997SVladimir Oltean 		/* But is it what's in the device tree? */
32800b0e2997SVladimir Oltean 		if (priv->info->device_id != device_id ||
32810b0e2997SVladimir Oltean 		    priv->info->part_no != part_no) {
32820b0e2997SVladimir Oltean 			dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
32830b0e2997SVladimir Oltean 				 priv->info->name, info->name);
32840b0e2997SVladimir Oltean 			/* It isn't. No problem, pick that up. */
32850b0e2997SVladimir Oltean 			priv->info = info;
32868aa9ebccSVladimir Oltean 		}
32878aa9ebccSVladimir Oltean 
32888aa9ebccSVladimir Oltean 		return 0;
32898aa9ebccSVladimir Oltean 	}
32908aa9ebccSVladimir Oltean 
32910b0e2997SVladimir Oltean 	dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
32920b0e2997SVladimir Oltean 		device_id, part_no);
32930b0e2997SVladimir Oltean 
32940b0e2997SVladimir Oltean 	return -ENODEV;
32950b0e2997SVladimir Oltean }
32960b0e2997SVladimir Oltean 
sja1105_probe(struct spi_device * spi)32978aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
32988aa9ebccSVladimir Oltean {
32998aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
33008aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
3301718bad0eSVladimir Oltean 	size_t max_xfer, max_msg;
33028aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
3303022522acSVladimir Oltean 	int rc;
33048aa9ebccSVladimir Oltean 
33058aa9ebccSVladimir Oltean 	if (!dev->of_node) {
33068aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
33078aa9ebccSVladimir Oltean 		return -EINVAL;
33088aa9ebccSVladimir Oltean 	}
33098aa9ebccSVladimir Oltean 
331033e1501fSVladimir Oltean 	rc = sja1105_hw_reset(dev, 1, 1);
331133e1501fSVladimir Oltean 	if (rc)
331233e1501fSVladimir Oltean 		return rc;
331333e1501fSVladimir Oltean 
33148aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
33158aa9ebccSVladimir Oltean 	if (!priv)
33168aa9ebccSVladimir Oltean 		return -ENOMEM;
33178aa9ebccSVladimir Oltean 
33188aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
33198aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
33208aa9ebccSVladimir Oltean 	 */
33218aa9ebccSVladimir Oltean 	priv->spidev = spi;
33228aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
33238aa9ebccSVladimir Oltean 
33248aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
33258aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
33268aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
33278aa9ebccSVladimir Oltean 	if (rc < 0) {
33288aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
33298aa9ebccSVladimir Oltean 		return rc;
33308aa9ebccSVladimir Oltean 	}
33318aa9ebccSVladimir Oltean 
3332718bad0eSVladimir Oltean 	/* In sja1105_xfer, we send spi_messages composed of two spi_transfers:
3333718bad0eSVladimir Oltean 	 * a small one for the message header and another one for the current
3334718bad0eSVladimir Oltean 	 * chunk of the packed buffer.
3335718bad0eSVladimir Oltean 	 * Check that the restrictions imposed by the SPI controller are
3336718bad0eSVladimir Oltean 	 * respected: the chunk buffer is smaller than the max transfer size,
3337718bad0eSVladimir Oltean 	 * and the total length of the chunk plus its message header is smaller
3338718bad0eSVladimir Oltean 	 * than the max message size.
3339718bad0eSVladimir Oltean 	 * We do that during probe time since the maximum transfer size is a
3340718bad0eSVladimir Oltean 	 * runtime invariant.
3341718bad0eSVladimir Oltean 	 */
3342718bad0eSVladimir Oltean 	max_xfer = spi_max_transfer_size(spi);
3343718bad0eSVladimir Oltean 	max_msg = spi_max_message_size(spi);
3344718bad0eSVladimir Oltean 
3345718bad0eSVladimir Oltean 	/* We need to send at least one 64-bit word of SPI payload per message
3346718bad0eSVladimir Oltean 	 * in order to be able to make useful progress.
3347718bad0eSVladimir Oltean 	 */
3348718bad0eSVladimir Oltean 	if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) {
3349718bad0eSVladimir Oltean 		dev_err(dev, "SPI master cannot send large enough buffers, aborting\n");
3350718bad0eSVladimir Oltean 		return -EINVAL;
3351718bad0eSVladimir Oltean 	}
3352718bad0eSVladimir Oltean 
3353718bad0eSVladimir Oltean 	priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN;
3354718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_xfer)
3355718bad0eSVladimir Oltean 		priv->max_xfer_len = max_xfer;
3356718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER)
3357718bad0eSVladimir Oltean 		priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER;
3358718bad0eSVladimir Oltean 
33598aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
33608aa9ebccSVladimir Oltean 
33618aa9ebccSVladimir Oltean 	/* Detect hardware device */
33628aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
33638aa9ebccSVladimir Oltean 	if (rc < 0) {
33648aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
33658aa9ebccSVladimir Oltean 		return rc;
33668aa9ebccSVladimir Oltean 	}
33678aa9ebccSVladimir Oltean 
33688aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
33698aa9ebccSVladimir Oltean 
33707e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
33718aa9ebccSVladimir Oltean 	if (!ds)
33728aa9ebccSVladimir Oltean 		return -ENOMEM;
33738aa9ebccSVladimir Oltean 
33747e99e347SVivien Didelot 	ds->dev = dev;
33753e77e59bSVladimir Oltean 	ds->num_ports = priv->info->num_ports;
33768aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
33778aa9ebccSVladimir Oltean 	ds->priv = priv;
33788aa9ebccSVladimir Oltean 	priv->ds = ds;
33798aa9ebccSVladimir Oltean 
3380d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
3381eb016afdSVladimir Oltean 	mutex_init(&priv->dynamic_config_lock);
3382d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
3383ea32690dSVladimir Oltean 	mutex_init(&priv->fdb_lock);
338422ee9f8eSVladimir Oltean 	spin_lock_init(&priv->ts_id_lock);
3385d5a619bfSVivien Didelot 
3386022522acSVladimir Oltean 	rc = sja1105_parse_dt(priv);
3387022522acSVladimir Oltean 	if (rc < 0) {
3388022522acSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
3389328621f6SVladimir Oltean 		return rc;
3390022522acSVladimir Oltean 	}
3391022522acSVladimir Oltean 
33924d752508SVladimir Oltean 	if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
33934d752508SVladimir Oltean 		priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
33944d752508SVladimir Oltean 					 sizeof(struct sja1105_cbs_entry),
33954d752508SVladimir Oltean 					 GFP_KERNEL);
3396022522acSVladimir Oltean 		if (!priv->cbs)
3397022522acSVladimir Oltean 			return -ENOMEM;
33984d752508SVladimir Oltean 	}
33994d752508SVladimir Oltean 
3400022522acSVladimir Oltean 	return dsa_register_switch(priv->ds);
34018aa9ebccSVladimir Oltean }
34028aa9ebccSVladimir Oltean 
sja1105_remove(struct spi_device * spi)3403a0386bbaSUwe Kleine-König static void sja1105_remove(struct spi_device *spi)
34048aa9ebccSVladimir Oltean {
34058aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
34068aa9ebccSVladimir Oltean 
34070650bf52SVladimir Oltean 	if (!priv)
3408a0386bbaSUwe Kleine-König 		return;
34090650bf52SVladimir Oltean 
34100650bf52SVladimir Oltean 	dsa_unregister_switch(priv->ds);
34118aa9ebccSVladimir Oltean }
34128aa9ebccSVladimir Oltean 
sja1105_shutdown(struct spi_device * spi)34130650bf52SVladimir Oltean static void sja1105_shutdown(struct spi_device *spi)
34140650bf52SVladimir Oltean {
34150650bf52SVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
34160650bf52SVladimir Oltean 
34170650bf52SVladimir Oltean 	if (!priv)
34180650bf52SVladimir Oltean 		return;
34190650bf52SVladimir Oltean 
34200650bf52SVladimir Oltean 	dsa_switch_shutdown(priv->ds);
34210650bf52SVladimir Oltean 
34220650bf52SVladimir Oltean 	spi_set_drvdata(spi, NULL);
34230650bf52SVladimir Oltean }
34240650bf52SVladimir Oltean 
34258aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
34268aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
34278aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
34288aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
34298aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
34308aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
34318aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
34323e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110a", .data = &sja1110a_info },
34333e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110b", .data = &sja1110b_info },
34343e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110c", .data = &sja1110c_info },
34353e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110d", .data = &sja1110d_info },
34368aa9ebccSVladimir Oltean 	{ /* sentinel */ },
34378aa9ebccSVladimir Oltean };
34388aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
34398aa9ebccSVladimir Oltean 
3440855fe499SOleksij Rempel static const struct spi_device_id sja1105_spi_ids[] = {
3441855fe499SOleksij Rempel 	{ "sja1105e" },
3442855fe499SOleksij Rempel 	{ "sja1105t" },
3443855fe499SOleksij Rempel 	{ "sja1105p" },
3444855fe499SOleksij Rempel 	{ "sja1105q" },
3445855fe499SOleksij Rempel 	{ "sja1105r" },
3446855fe499SOleksij Rempel 	{ "sja1105s" },
3447855fe499SOleksij Rempel 	{ "sja1110a" },
3448855fe499SOleksij Rempel 	{ "sja1110b" },
3449855fe499SOleksij Rempel 	{ "sja1110c" },
3450855fe499SOleksij Rempel 	{ "sja1110d" },
3451855fe499SOleksij Rempel 	{ },
3452855fe499SOleksij Rempel };
3453855fe499SOleksij Rempel MODULE_DEVICE_TABLE(spi, sja1105_spi_ids);
3454855fe499SOleksij Rempel 
34558aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
34568aa9ebccSVladimir Oltean 	.driver = {
34578aa9ebccSVladimir Oltean 		.name  = "sja1105",
34588aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
34598aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
34608aa9ebccSVladimir Oltean 	},
3461855fe499SOleksij Rempel 	.id_table = sja1105_spi_ids,
34628aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
34638aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
34640650bf52SVladimir Oltean 	.shutdown = sja1105_shutdown,
34658aa9ebccSVladimir Oltean };
34668aa9ebccSVladimir Oltean 
34678aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
34688aa9ebccSVladimir Oltean 
34698aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
34708aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
34718aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
34728aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
3473