xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision cde8078e83e32022e30a4f670c2c8179f2f08991)
18aa9ebccSVladimir Oltean // SPDX-License-Identifier: GPL-2.0
28aa9ebccSVladimir Oltean /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
38aa9ebccSVladimir Oltean  * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
48aa9ebccSVladimir Oltean  */
58aa9ebccSVladimir Oltean 
68aa9ebccSVladimir Oltean #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
78aa9ebccSVladimir Oltean 
88aa9ebccSVladimir Oltean #include <linux/delay.h>
98aa9ebccSVladimir Oltean #include <linux/module.h>
108aa9ebccSVladimir Oltean #include <linux/printk.h>
118aa9ebccSVladimir Oltean #include <linux/spi/spi.h>
128aa9ebccSVladimir Oltean #include <linux/errno.h>
138aa9ebccSVladimir Oltean #include <linux/gpio/consumer.h>
14ad9f299aSVladimir Oltean #include <linux/phylink.h>
158aa9ebccSVladimir Oltean #include <linux/of.h>
168aa9ebccSVladimir Oltean #include <linux/of_net.h>
178aa9ebccSVladimir Oltean #include <linux/of_mdio.h>
188aa9ebccSVladimir Oltean #include <linux/of_device.h>
193ad1d171SVladimir Oltean #include <linux/pcs/pcs-xpcs.h>
208aa9ebccSVladimir Oltean #include <linux/netdev_features.h>
218aa9ebccSVladimir Oltean #include <linux/netdevice.h>
228aa9ebccSVladimir Oltean #include <linux/if_bridge.h>
238aa9ebccSVladimir Oltean #include <linux/if_ether.h>
24227d07a0SVladimir Oltean #include <linux/dsa/8021q.h>
258aa9ebccSVladimir Oltean #include "sja1105.h"
26317ab5b8SVladimir Oltean #include "sja1105_tas.h"
278aa9ebccSVladimir Oltean 
284d942354SVladimir Oltean #define SJA1105_UNKNOWN_MULTICAST	0x010000000000ull
29ed040abcSVladimir Oltean #define SJA1105_DEFAULT_VLAN		(VLAN_N_VID - 1)
304d942354SVladimir Oltean 
31ac02a451SVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops;
32ac02a451SVladimir Oltean 
338aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
348aa9ebccSVladimir Oltean 			     unsigned int startup_delay)
358aa9ebccSVladimir Oltean {
368aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
378aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
388aa9ebccSVladimir Oltean 	msleep(pulse_len);
398aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
408aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
418aa9ebccSVladimir Oltean 	msleep(startup_delay);
428aa9ebccSVladimir Oltean }
438aa9ebccSVladimir Oltean 
448aa9ebccSVladimir Oltean static void
458aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
468aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
478aa9ebccSVladimir Oltean {
484d942354SVladimir Oltean 	if (allow)
498aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
504d942354SVladimir Oltean 	else
518aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
528aa9ebccSVladimir Oltean }
538aa9ebccSVladimir Oltean 
547f7ccdeaSVladimir Oltean static bool sja1105_can_forward(struct sja1105_l2_forwarding_entry *l2_fwd,
557f7ccdeaSVladimir Oltean 				int from, int to)
567f7ccdeaSVladimir Oltean {
577f7ccdeaSVladimir Oltean 	return !!(l2_fwd[from].reach_port & BIT(to));
587f7ccdeaSVladimir Oltean }
597f7ccdeaSVladimir Oltean 
60*cde8078eSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
61*cde8078eSVladimir Oltean {
62*cde8078eSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
63*cde8078eSVladimir Oltean 
64*cde8078eSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
65*cde8078eSVladimir Oltean 
66*cde8078eSVladimir Oltean 	if (mac[port].vlanid == pvid)
67*cde8078eSVladimir Oltean 		return 0;
68*cde8078eSVladimir Oltean 
69*cde8078eSVladimir Oltean 	mac[port].vlanid = pvid;
70*cde8078eSVladimir Oltean 
71*cde8078eSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
72*cde8078eSVladimir Oltean 					    &mac[port], true);
73*cde8078eSVladimir Oltean }
74*cde8078eSVladimir Oltean 
75*cde8078eSVladimir Oltean static int sja1105_commit_pvid(struct dsa_switch *ds, int port)
76*cde8078eSVladimir Oltean {
77*cde8078eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
78*cde8078eSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
79*cde8078eSVladimir Oltean 	u16 pvid;
80*cde8078eSVladimir Oltean 
81*cde8078eSVladimir Oltean 	if (dp->bridge_dev && br_vlan_enabled(dp->bridge_dev))
82*cde8078eSVladimir Oltean 		pvid = priv->bridge_pvid[port];
83*cde8078eSVladimir Oltean 	else
84*cde8078eSVladimir Oltean 		pvid = priv->tag_8021q_pvid[port];
85*cde8078eSVladimir Oltean 
86*cde8078eSVladimir Oltean 	return sja1105_pvid_apply(priv, port, pvid);
87*cde8078eSVladimir Oltean }
88*cde8078eSVladimir Oltean 
898aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
908aa9ebccSVladimir Oltean {
918aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
928aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
938aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
948aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
958aa9ebccSVladimir Oltean 		 */
968aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
978aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
988aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
998aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
1008aa9ebccSVladimir Oltean 		.ifg = 0,
1018aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
1021fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
1038aa9ebccSVladimir Oltean 		 */
10441fed17fSVladimir Oltean 		.speed = priv->info->port_speed[SJA1105_SPEED_AUTO],
1058aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
1068aa9ebccSVladimir Oltean 		.tp_delin = 0,
1078aa9ebccSVladimir Oltean 		.tp_delout = 0,
1088aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
1098aa9ebccSVladimir Oltean 		.maxage = 0xFF,
1108aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
1118aa9ebccSVladimir Oltean 		.vlanprio = 0,
112e3502b82SVladimir Oltean 		.vlanid = 1,
1138aa9ebccSVladimir Oltean 		.ing_mirr = false,
1148aa9ebccSVladimir Oltean 		.egr_mirr = false,
1158aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
1168aa9ebccSVladimir Oltean 		.drpnona664 = false,
1178aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
1188aa9ebccSVladimir Oltean 		.drpdtag = false,
1198aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
1208aa9ebccSVladimir Oltean 		.drpuntag = false,
1218aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
1228aa9ebccSVladimir Oltean 		.retag = false,
123640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
124640f763fSVladimir Oltean 		 * STP will enable it.
125640f763fSVladimir Oltean 		 */
126640f763fSVladimir Oltean 		.dyn_learn = false,
1278aa9ebccSVladimir Oltean 		.egress = false,
1288aa9ebccSVladimir Oltean 		.ingress = false,
1298aa9ebccSVladimir Oltean 	};
1308aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
131542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
1328aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1338aa9ebccSVladimir Oltean 	int i;
1348aa9ebccSVladimir Oltean 
1358aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1368aa9ebccSVladimir Oltean 
1378aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1388aa9ebccSVladimir Oltean 	if (table->entry_count) {
1398aa9ebccSVladimir Oltean 		kfree(table->entries);
1408aa9ebccSVladimir Oltean 		table->entry_count = 0;
1418aa9ebccSVladimir Oltean 	}
1428aa9ebccSVladimir Oltean 
143fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
1448aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1458aa9ebccSVladimir Oltean 	if (!table->entries)
1468aa9ebccSVladimir Oltean 		return -ENOMEM;
1478aa9ebccSVladimir Oltean 
148fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
1498aa9ebccSVladimir Oltean 
1508aa9ebccSVladimir Oltean 	mac = table->entries;
1518aa9ebccSVladimir Oltean 
152542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1538aa9ebccSVladimir Oltean 		mac[i] = default_mac;
154b0b33b04SVladimir Oltean 
155b0b33b04SVladimir Oltean 		/* Let sja1105_bridge_stp_state_set() keep address learning
156b0b33b04SVladimir Oltean 		 * enabled for the CPU port.
157640f763fSVladimir Oltean 		 */
158b0b33b04SVladimir Oltean 		if (dsa_is_cpu_port(ds, i))
159b0b33b04SVladimir Oltean 			priv->learn_ena |= BIT(i);
160640f763fSVladimir Oltean 	}
1618aa9ebccSVladimir Oltean 
1628aa9ebccSVladimir Oltean 	return 0;
1638aa9ebccSVladimir Oltean }
1648aa9ebccSVladimir Oltean 
1655d645df9SVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv)
1668aa9ebccSVladimir Oltean {
1678aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1688aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
169542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
1708aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1718aa9ebccSVladimir Oltean 	int i;
1728aa9ebccSVladimir Oltean 
1738aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1748aa9ebccSVladimir Oltean 
1758aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1768aa9ebccSVladimir Oltean 	if (table->entry_count) {
1778aa9ebccSVladimir Oltean 		kfree(table->entries);
1788aa9ebccSVladimir Oltean 		table->entry_count = 0;
1798aa9ebccSVladimir Oltean 	}
1808aa9ebccSVladimir Oltean 
181fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
1828aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1838aa9ebccSVladimir Oltean 	if (!table->entries)
1848aa9ebccSVladimir Oltean 		return -ENOMEM;
1858aa9ebccSVladimir Oltean 
1861fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
187fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
1888aa9ebccSVladimir Oltean 
1898aa9ebccSVladimir Oltean 	mii = table->entries;
1908aa9ebccSVladimir Oltean 
191542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
1925d645df9SVladimir Oltean 		sja1105_mii_role_t role = XMII_MAC;
1935d645df9SVladimir Oltean 
194ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
195ee9d0cb6SVladimir Oltean 			continue;
196ee9d0cb6SVladimir Oltean 
1975d645df9SVladimir Oltean 		switch (priv->phy_mode[i]) {
1985a8f0974SVladimir Oltean 		case PHY_INTERFACE_MODE_INTERNAL:
1995a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_NO_PHY)
2005a8f0974SVladimir Oltean 				goto unsupported;
2015a8f0974SVladimir Oltean 
2025a8f0974SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
2035a8f0974SVladimir Oltean 			if (priv->info->internal_phy[i] == SJA1105_PHY_BASE_TX)
2045a8f0974SVladimir Oltean 				mii->special[i] = true;
2055a8f0974SVladimir Oltean 
2065a8f0974SVladimir Oltean 			break;
2075d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVMII:
2085d645df9SVladimir Oltean 			role = XMII_PHY;
2095d645df9SVladimir Oltean 			fallthrough;
2108aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
21191a05078SVladimir Oltean 			if (!priv->info->supports_mii[i])
21291a05078SVladimir Oltean 				goto unsupported;
21391a05078SVladimir Oltean 
2148aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
2158aa9ebccSVladimir Oltean 			break;
2165d645df9SVladimir Oltean 		case PHY_INTERFACE_MODE_REVRMII:
2175d645df9SVladimir Oltean 			role = XMII_PHY;
2185d645df9SVladimir Oltean 			fallthrough;
2198aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
22091a05078SVladimir Oltean 			if (!priv->info->supports_rmii[i])
22191a05078SVladimir Oltean 				goto unsupported;
22291a05078SVladimir Oltean 
2238aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
2248aa9ebccSVladimir Oltean 			break;
2258aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
2268aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
2278aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
2288aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
22991a05078SVladimir Oltean 			if (!priv->info->supports_rgmii[i])
23091a05078SVladimir Oltean 				goto unsupported;
23191a05078SVladimir Oltean 
2328aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
2338aa9ebccSVladimir Oltean 			break;
234ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
23591a05078SVladimir Oltean 			if (!priv->info->supports_sgmii[i])
23691a05078SVladimir Oltean 				goto unsupported;
23791a05078SVladimir Oltean 
238ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
239ece578bcSVladimir Oltean 			mii->special[i] = true;
240ffe10e67SVladimir Oltean 			break;
24191a05078SVladimir Oltean 		case PHY_INTERFACE_MODE_2500BASEX:
24291a05078SVladimir Oltean 			if (!priv->info->supports_2500basex[i])
24391a05078SVladimir Oltean 				goto unsupported;
24491a05078SVladimir Oltean 
24591a05078SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
246ece578bcSVladimir Oltean 			mii->special[i] = true;
24791a05078SVladimir Oltean 			break;
24891a05078SVladimir Oltean unsupported:
2498aa9ebccSVladimir Oltean 		default:
25091a05078SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d!\n",
2515d645df9SVladimir Oltean 				phy_modes(priv->phy_mode[i]), i);
2526729188dSVladimir Oltean 			return -EINVAL;
2538aa9ebccSVladimir Oltean 		}
2548aa9ebccSVladimir Oltean 
2555d645df9SVladimir Oltean 		mii->phy_mac[i] = role;
2568aa9ebccSVladimir Oltean 	}
2578aa9ebccSVladimir Oltean 	return 0;
2588aa9ebccSVladimir Oltean }
2598aa9ebccSVladimir Oltean 
2608aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
2618aa9ebccSVladimir Oltean {
2624d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
2638aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2644d942354SVladimir Oltean 	int port;
2658aa9ebccSVladimir Oltean 
2668aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
2678aa9ebccSVladimir Oltean 
2684d942354SVladimir Oltean 	/* We only populate the FDB table through dynamic L2 Address Lookup
2694d942354SVladimir Oltean 	 * entries, except for a special entry at the end which is a catch-all
2704d942354SVladimir Oltean 	 * for unknown multicast and will be used to control flooding domain.
271291d1e72SVladimir Oltean 	 */
2728aa9ebccSVladimir Oltean 	if (table->entry_count) {
2738aa9ebccSVladimir Oltean 		kfree(table->entries);
2748aa9ebccSVladimir Oltean 		table->entry_count = 0;
2758aa9ebccSVladimir Oltean 	}
2764d942354SVladimir Oltean 
2774d942354SVladimir Oltean 	if (!priv->info->can_limit_mcast_flood)
2784d942354SVladimir Oltean 		return 0;
2794d942354SVladimir Oltean 
2804d942354SVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
2814d942354SVladimir Oltean 				 GFP_KERNEL);
2824d942354SVladimir Oltean 	if (!table->entries)
2834d942354SVladimir Oltean 		return -ENOMEM;
2844d942354SVladimir Oltean 
2854d942354SVladimir Oltean 	table->entry_count = 1;
2864d942354SVladimir Oltean 	l2_lookup = table->entries;
2874d942354SVladimir Oltean 
2884d942354SVladimir Oltean 	/* All L2 multicast addresses have an odd first octet */
2894d942354SVladimir Oltean 	l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST;
2904d942354SVladimir Oltean 	l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST;
2914d942354SVladimir Oltean 	l2_lookup[0].lockeds = true;
2924d942354SVladimir Oltean 	l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1;
2934d942354SVladimir Oltean 
2944d942354SVladimir Oltean 	/* Flood multicast to every port by default */
2954d942354SVladimir Oltean 	for (port = 0; port < priv->ds->num_ports; port++)
2964d942354SVladimir Oltean 		if (!dsa_is_unused_port(priv->ds, port))
2974d942354SVladimir Oltean 			l2_lookup[0].destports |= BIT(port);
2984d942354SVladimir Oltean 
2998aa9ebccSVladimir Oltean 	return 0;
3008aa9ebccSVladimir Oltean }
3018aa9ebccSVladimir Oltean 
3028aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
3038aa9ebccSVladimir Oltean {
3048aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
3058456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
3068456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
3078aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
3088aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
3091da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
3101da73821SVladimir Oltean 		.start_dynspc = 0,
3118aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
3128aa9ebccSVladimir Oltean 		.poly = 0x97,
3138aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
3148aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
3158aa9ebccSVladimir Oltean 		 */
3166d7c7d94SVladimir Oltean 		.shared_learn = true,
3178aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
3188aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
3198aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
3208aa9ebccSVladimir Oltean 		 */
3218aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
3228aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
3238aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
3248aa9ebccSVladimir Oltean 		 */
3258aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
3261da73821SVladimir Oltean 		/* P/Q/R/S only */
3271da73821SVladimir Oltean 		.use_static = true,
3281da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
3291da73821SVladimir Oltean 		 * dynamic FDB entries
3301da73821SVladimir Oltean 		 */
3311da73821SVladimir Oltean 		.owr_dyn = true,
3321da73821SVladimir Oltean 		.drpnolearn = true,
3338aa9ebccSVladimir Oltean 	};
334542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
335f238fef1SVladimir Oltean 	int port, num_used_ports = 0;
336542043e9SVladimir Oltean 	struct sja1105_table *table;
337542043e9SVladimir Oltean 	u64 max_fdb_entries;
338542043e9SVladimir Oltean 
339542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++)
340f238fef1SVladimir Oltean 		if (!dsa_is_unused_port(ds, port))
341f238fef1SVladimir Oltean 			num_used_ports++;
342f238fef1SVladimir Oltean 
343f238fef1SVladimir Oltean 	max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports;
344f238fef1SVladimir Oltean 
345f238fef1SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
346f238fef1SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
347f238fef1SVladimir Oltean 			continue;
348f238fef1SVladimir Oltean 
349542043e9SVladimir Oltean 		default_l2_lookup_params.maxaddrp[port] = max_fdb_entries;
350f238fef1SVladimir Oltean 	}
3518aa9ebccSVladimir Oltean 
3528aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
3538aa9ebccSVladimir Oltean 
3548aa9ebccSVladimir Oltean 	if (table->entry_count) {
3558aa9ebccSVladimir Oltean 		kfree(table->entries);
3568aa9ebccSVladimir Oltean 		table->entry_count = 0;
3578aa9ebccSVladimir Oltean 	}
3588aa9ebccSVladimir Oltean 
359fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
3608aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3618aa9ebccSVladimir Oltean 	if (!table->entries)
3628aa9ebccSVladimir Oltean 		return -ENOMEM;
3638aa9ebccSVladimir Oltean 
364fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
3658aa9ebccSVladimir Oltean 
3668aa9ebccSVladimir Oltean 	/* This table only has a single entry */
3678aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
3688aa9ebccSVladimir Oltean 				default_l2_lookup_params;
3698aa9ebccSVladimir Oltean 
3708aa9ebccSVladimir Oltean 	return 0;
3718aa9ebccSVladimir Oltean }
3728aa9ebccSVladimir Oltean 
373ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU
374ed040abcSVladimir Oltean  * using management routes (e.g. STP, PTP) as opposed to tag_8021q.
375ed040abcSVladimir Oltean  * All DT-defined ports are members of this VLAN, and there are no
376ed040abcSVladimir Oltean  * restrictions on forwarding (since the CPU selects the destination).
377ed040abcSVladimir Oltean  * Frames from this VLAN will always be transmitted as untagged, and
378ed040abcSVladimir Oltean  * neither the bridge nor the 8021q module cannot create this VLAN ID.
379ed040abcSVladimir Oltean  */
3808aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
3818aa9ebccSVladimir Oltean {
3828aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3838aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
3843e77e59bSVladimir Oltean 		.type_entry = SJA1110_VLAN_D_TAG,
3858aa9ebccSVladimir Oltean 		.ving_mirr = 0,
3868aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
3878aa9ebccSVladimir Oltean 		.vmemb_port = 0,
3888aa9ebccSVladimir Oltean 		.vlan_bc = 0,
3898aa9ebccSVladimir Oltean 		.tag_port = 0,
390ed040abcSVladimir Oltean 		.vlanid = SJA1105_DEFAULT_VLAN,
3918aa9ebccSVladimir Oltean 	};
392ec5ae610SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
393ec5ae610SVladimir Oltean 	int port;
3948aa9ebccSVladimir Oltean 
3958aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
3968aa9ebccSVladimir Oltean 
3978aa9ebccSVladimir Oltean 	if (table->entry_count) {
3988aa9ebccSVladimir Oltean 		kfree(table->entries);
3998aa9ebccSVladimir Oltean 		table->entry_count = 0;
4008aa9ebccSVladimir Oltean 	}
4018aa9ebccSVladimir Oltean 
402c75857b0SZheng Yongjun 	table->entries = kzalloc(table->ops->unpacked_entry_size,
4038aa9ebccSVladimir Oltean 				 GFP_KERNEL);
4048aa9ebccSVladimir Oltean 	if (!table->entries)
4058aa9ebccSVladimir Oltean 		return -ENOMEM;
4068aa9ebccSVladimir Oltean 
4078aa9ebccSVladimir Oltean 	table->entry_count = 1;
4088aa9ebccSVladimir Oltean 
409ec5ae610SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
410ec5ae610SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
411ec5ae610SVladimir Oltean 			continue;
412ec5ae610SVladimir Oltean 
413ec5ae610SVladimir Oltean 		pvid.vmemb_port |= BIT(port);
414ec5ae610SVladimir Oltean 		pvid.vlan_bc |= BIT(port);
415ec5ae610SVladimir Oltean 		pvid.tag_port &= ~BIT(port);
416ec5ae610SVladimir Oltean 
4176dfd23d3SVladimir Oltean 		if (dsa_is_cpu_port(ds, port)) {
4186dfd23d3SVladimir Oltean 			priv->tag_8021q_pvid[port] = SJA1105_DEFAULT_VLAN;
4196dfd23d3SVladimir Oltean 			priv->bridge_pvid[port] = SJA1105_DEFAULT_VLAN;
4206dfd23d3SVladimir Oltean 		}
4218aa9ebccSVladimir Oltean 	}
4228aa9ebccSVladimir Oltean 
4238aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
4248aa9ebccSVladimir Oltean 	return 0;
4258aa9ebccSVladimir Oltean }
4268aa9ebccSVladimir Oltean 
4278aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
4288aa9ebccSVladimir Oltean {
4298aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
430542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
4318aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4328aa9ebccSVladimir Oltean 	int i, j;
4338aa9ebccSVladimir Oltean 
4348aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
4358aa9ebccSVladimir Oltean 
4368aa9ebccSVladimir Oltean 	if (table->entry_count) {
4378aa9ebccSVladimir Oltean 		kfree(table->entries);
4388aa9ebccSVladimir Oltean 		table->entry_count = 0;
4398aa9ebccSVladimir Oltean 	}
4408aa9ebccSVladimir Oltean 
441fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
4428aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4438aa9ebccSVladimir Oltean 	if (!table->entries)
4448aa9ebccSVladimir Oltean 		return -ENOMEM;
4458aa9ebccSVladimir Oltean 
446fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
4478aa9ebccSVladimir Oltean 
4488aa9ebccSVladimir Oltean 	l2fwd = table->entries;
4498aa9ebccSVladimir Oltean 
4508aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
451542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
4528aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
4538aa9ebccSVladimir Oltean 
454f238fef1SVladimir Oltean 		if (dsa_is_unused_port(ds, i))
455f238fef1SVladimir Oltean 			continue;
456f238fef1SVladimir Oltean 
4578aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
4588aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
4598aa9ebccSVladimir Oltean 
4607f7ccdeaSVladimir Oltean 		/* All ports start up with egress flooding enabled,
4617f7ccdeaSVladimir Oltean 		 * including the CPU port.
4627f7ccdeaSVladimir Oltean 		 */
4637f7ccdeaSVladimir Oltean 		priv->ucast_egress_floods |= BIT(i);
4647f7ccdeaSVladimir Oltean 		priv->bcast_egress_floods |= BIT(i);
4657f7ccdeaSVladimir Oltean 
4668aa9ebccSVladimir Oltean 		if (i == upstream)
4678aa9ebccSVladimir Oltean 			continue;
4688aa9ebccSVladimir Oltean 
4698aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
4708aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
4714d942354SVladimir Oltean 
4724d942354SVladimir Oltean 		l2fwd[i].bc_domain = BIT(upstream);
4734d942354SVladimir Oltean 		l2fwd[i].fl_domain = BIT(upstream);
4744d942354SVladimir Oltean 
4754d942354SVladimir Oltean 		l2fwd[upstream].bc_domain |= BIT(i);
4764d942354SVladimir Oltean 		l2fwd[upstream].fl_domain |= BIT(i);
4778aa9ebccSVladimir Oltean 	}
478f238fef1SVladimir Oltean 
4798aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
4808aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
4818aa9ebccSVladimir Oltean 	 */
482f238fef1SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++) {
483f238fef1SVladimir Oltean 		for (j = 0; j < ds->num_ports; j++) {
484f238fef1SVladimir Oltean 			if (dsa_is_unused_port(ds, j))
485f238fef1SVladimir Oltean 				continue;
486f238fef1SVladimir Oltean 
487542043e9SVladimir Oltean 			l2fwd[ds->num_ports + i].vlan_pmap[j] = i;
488f238fef1SVladimir Oltean 		}
4893e77e59bSVladimir Oltean 
4903e77e59bSVladimir Oltean 		l2fwd[ds->num_ports + i].type_egrpcp2outputq = true;
4913e77e59bSVladimir Oltean 	}
4923e77e59bSVladimir Oltean 
4933e77e59bSVladimir Oltean 	return 0;
4943e77e59bSVladimir Oltean }
4953e77e59bSVladimir Oltean 
4963e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv)
4973e77e59bSVladimir Oltean {
4983e77e59bSVladimir Oltean 	struct sja1110_pcp_remapping_entry *pcp_remap;
4993e77e59bSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
5003e77e59bSVladimir Oltean 	struct sja1105_table *table;
5013e77e59bSVladimir Oltean 	int port, tc;
5023e77e59bSVladimir Oltean 
5033e77e59bSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING];
5043e77e59bSVladimir Oltean 
5053e77e59bSVladimir Oltean 	/* Nothing to do for SJA1105 */
5063e77e59bSVladimir Oltean 	if (!table->ops->max_entry_count)
5073e77e59bSVladimir Oltean 		return 0;
5083e77e59bSVladimir Oltean 
5093e77e59bSVladimir Oltean 	if (table->entry_count) {
5103e77e59bSVladimir Oltean 		kfree(table->entries);
5113e77e59bSVladimir Oltean 		table->entry_count = 0;
5123e77e59bSVladimir Oltean 	}
5133e77e59bSVladimir Oltean 
5143e77e59bSVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
5153e77e59bSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5163e77e59bSVladimir Oltean 	if (!table->entries)
5173e77e59bSVladimir Oltean 		return -ENOMEM;
5183e77e59bSVladimir Oltean 
5193e77e59bSVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
5203e77e59bSVladimir Oltean 
5213e77e59bSVladimir Oltean 	pcp_remap = table->entries;
5223e77e59bSVladimir Oltean 
5233e77e59bSVladimir Oltean 	/* Repeat the configuration done for vlan_pmap */
5243e77e59bSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
5253e77e59bSVladimir Oltean 		if (dsa_is_unused_port(ds, port))
5263e77e59bSVladimir Oltean 			continue;
5273e77e59bSVladimir Oltean 
5283e77e59bSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
5293e77e59bSVladimir Oltean 			pcp_remap[port].egrpcp[tc] = tc;
530f238fef1SVladimir Oltean 	}
5318aa9ebccSVladimir Oltean 
5328aa9ebccSVladimir Oltean 	return 0;
5338aa9ebccSVladimir Oltean }
5348aa9ebccSVladimir Oltean 
5358aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
5368aa9ebccSVladimir Oltean {
5371bf658eeSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2fwd_params;
5388aa9ebccSVladimir Oltean 	struct sja1105_table *table;
5398aa9ebccSVladimir Oltean 
5408aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
5418aa9ebccSVladimir Oltean 
5428aa9ebccSVladimir Oltean 	if (table->entry_count) {
5438aa9ebccSVladimir Oltean 		kfree(table->entries);
5448aa9ebccSVladimir Oltean 		table->entry_count = 0;
5458aa9ebccSVladimir Oltean 	}
5468aa9ebccSVladimir Oltean 
547fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
5488aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5498aa9ebccSVladimir Oltean 	if (!table->entries)
5508aa9ebccSVladimir Oltean 		return -ENOMEM;
5518aa9ebccSVladimir Oltean 
552fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
5538aa9ebccSVladimir Oltean 
5548aa9ebccSVladimir Oltean 	/* This table only has a single entry */
5551bf658eeSVladimir Oltean 	l2fwd_params = table->entries;
5561bf658eeSVladimir Oltean 
5571bf658eeSVladimir Oltean 	/* Disallow dynamic reconfiguration of vlan_pmap */
5581bf658eeSVladimir Oltean 	l2fwd_params->max_dynp = 0;
5591bf658eeSVladimir Oltean 	/* Use a single memory partition for all ingress queues */
5601bf658eeSVladimir Oltean 	l2fwd_params->part_spc[0] = priv->info->max_frame_mem;
5618aa9ebccSVladimir Oltean 
5628aa9ebccSVladimir Oltean 	return 0;
5638aa9ebccSVladimir Oltean }
5648aa9ebccSVladimir Oltean 
565aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
566aaa270c6SVladimir Oltean {
567aaa270c6SVladimir Oltean 	struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
568aaa270c6SVladimir Oltean 	struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
569aaa270c6SVladimir Oltean 	struct sja1105_table *table;
570aaa270c6SVladimir Oltean 
571aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
572aaa270c6SVladimir Oltean 	l2_fwd_params = table->entries;
5730fac6aa0SVladimir Oltean 	l2_fwd_params->part_spc[0] = SJA1105_MAX_FRAME_MEMORY;
574aaa270c6SVladimir Oltean 
575aaa270c6SVladimir Oltean 	/* If we have any critical-traffic virtual links, we need to reserve
576aaa270c6SVladimir Oltean 	 * some frame buffer memory for them. At the moment, hardcode the value
577aaa270c6SVladimir Oltean 	 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
578aaa270c6SVladimir Oltean 	 * remaining for best-effort traffic. TODO: figure out a more flexible
579aaa270c6SVladimir Oltean 	 * way to perform the frame buffer partitioning.
580aaa270c6SVladimir Oltean 	 */
581aaa270c6SVladimir Oltean 	if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
582aaa270c6SVladimir Oltean 		return;
583aaa270c6SVladimir Oltean 
584aaa270c6SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
585aaa270c6SVladimir Oltean 	vl_fwd_params = table->entries;
586aaa270c6SVladimir Oltean 
587aaa270c6SVladimir Oltean 	l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
588aaa270c6SVladimir Oltean 	vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
589aaa270c6SVladimir Oltean }
590aaa270c6SVladimir Oltean 
591ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values:
592ceec8bc0SVladimir Oltean  *
593ceec8bc0SVladimir Oltean  *      | 100 Mbps ports |  1Gbps ports  | 2.5Gbps ports | Disabled ports
594ceec8bc0SVladimir Oltean  * -----+----------------+---------------+---------------+---------------
595ceec8bc0SVladimir Oltean  *   0  |   0, [5:10]    |     [1:2]     |     [3:4]     |     retag
596ceec8bc0SVladimir Oltean  *   1  |0, [5:10], retag|     [1:2]     |     [3:4]     |       -
597ceec8bc0SVladimir Oltean  *   2  |   0, [5:10]    |  [1:3], retag |       4       |       -
598ceec8bc0SVladimir Oltean  *   3  |   0, [5:10]    |[1:2], 4, retag|       3       |       -
599ceec8bc0SVladimir Oltean  *   4  |  0, 2, [5:10]  |    1, retag   |     [3:4]     |       -
600ceec8bc0SVladimir Oltean  *   5  |  0, 1, [5:10]  |    2, retag   |     [3:4]     |       -
601ceec8bc0SVladimir Oltean  *  14  |   0, [5:10]    | [1:4], retag  |       -       |       -
602ceec8bc0SVladimir Oltean  *  15  |     [5:10]     | [0:4], retag  |       -       |       -
603ceec8bc0SVladimir Oltean  */
604ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv)
605ceec8bc0SVladimir Oltean {
606ceec8bc0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
607ceec8bc0SVladimir Oltean 	struct sja1105_table *table;
608ceec8bc0SVladimir Oltean 	bool port_1_is_base_tx;
609ceec8bc0SVladimir Oltean 	bool port_3_is_2500;
610ceec8bc0SVladimir Oltean 	bool port_4_is_2500;
611ceec8bc0SVladimir Oltean 	u64 tdmaconfigidx;
612ceec8bc0SVladimir Oltean 
613ceec8bc0SVladimir Oltean 	if (priv->info->device_id != SJA1110_DEVICE_ID)
614ceec8bc0SVladimir Oltean 		return;
615ceec8bc0SVladimir Oltean 
616ceec8bc0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
617ceec8bc0SVladimir Oltean 	general_params = table->entries;
618ceec8bc0SVladimir Oltean 
619ceec8bc0SVladimir Oltean 	/* All the settings below are "as opposed to SGMII", which is the
620ceec8bc0SVladimir Oltean 	 * other pinmuxing option.
621ceec8bc0SVladimir Oltean 	 */
622ceec8bc0SVladimir Oltean 	port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL;
623ceec8bc0SVladimir Oltean 	port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX;
624ceec8bc0SVladimir Oltean 	port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX;
625ceec8bc0SVladimir Oltean 
626ceec8bc0SVladimir Oltean 	if (port_1_is_base_tx)
627ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
628ceec8bc0SVladimir Oltean 		tdmaconfigidx = 5;
629ceec8bc0SVladimir Oltean 	else if (port_3_is_2500 && port_4_is_2500)
630ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 100 Mbps */
631ceec8bc0SVladimir Oltean 		tdmaconfigidx = 1;
632ceec8bc0SVladimir Oltean 	else if (port_3_is_2500)
633ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
634ceec8bc0SVladimir Oltean 		tdmaconfigidx = 3;
635ceec8bc0SVladimir Oltean 	else if (port_4_is_2500)
636ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
637ceec8bc0SVladimir Oltean 		tdmaconfigidx = 2;
638ceec8bc0SVladimir Oltean 	else
639ceec8bc0SVladimir Oltean 		/* Retagging port will operate at 1 Gbps */
640ceec8bc0SVladimir Oltean 		tdmaconfigidx = 14;
641ceec8bc0SVladimir Oltean 
642ceec8bc0SVladimir Oltean 	general_params->tdmaconfigidx = tdmaconfigidx;
643ceec8bc0SVladimir Oltean }
644ceec8bc0SVladimir Oltean 
6458aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
6468aa9ebccSVladimir Oltean {
6478aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
648511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
649511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
6508aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
6515f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
6525f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
6535f06c63bSVladimir Oltean 		 */
65408fde09aSVladimir Oltean 		.hostprio = 7,
6558aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
6568aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
65742824463SVladimir Oltean 		.incl_srcpt1 = false,
6588aa9ebccSVladimir Oltean 		.send_meta1  = false,
6598aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
6608aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
66142824463SVladimir Oltean 		.incl_srcpt0 = false,
6628aa9ebccSVladimir Oltean 		.send_meta0  = false,
6638aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
6648aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
6658aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
6668aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
6678aa9ebccSVladimir Oltean 		 */
668df2a81a3SVladimir Oltean 		.host_port = priv->ds->num_ports,
669511e6ca0SVladimir Oltean 		/* Default to an invalid value */
670542043e9SVladimir Oltean 		.mirr_port = priv->ds->num_ports,
6718aa9ebccSVladimir Oltean 		/* No TTEthernet */
672dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
6738aa9ebccSVladimir Oltean 		.vlmarker = 0,
6748aa9ebccSVladimir Oltean 		.vlmask = 0,
6758aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
6768aa9ebccSVladimir Oltean 		.ignore2stf = 0,
6776666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
6786666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
6796666cebcSVladimir Oltean 		 */
6806666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
6816666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
68229305260SVladimir Oltean 		/* Enable the TTEthernet engine on SJA1110 */
68329305260SVladimir Oltean 		.tte_en = true,
6844913b8ebSVladimir Oltean 		/* Set up the EtherType for control packets on SJA1110 */
6854913b8ebSVladimir Oltean 		.header_type = ETH_P_SJA1110,
6868aa9ebccSVladimir Oltean 	};
6876c0de59bSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
688df2a81a3SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
6898aa9ebccSVladimir Oltean 	struct sja1105_table *table;
690df2a81a3SVladimir Oltean 	int port;
691df2a81a3SVladimir Oltean 
692df2a81a3SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
693df2a81a3SVladimir Oltean 		if (dsa_is_cpu_port(ds, port)) {
694df2a81a3SVladimir Oltean 			default_general_params.host_port = port;
695df2a81a3SVladimir Oltean 			break;
696df2a81a3SVladimir Oltean 		}
697df2a81a3SVladimir Oltean 	}
6988aa9ebccSVladimir Oltean 
6998aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
7008aa9ebccSVladimir Oltean 
7018aa9ebccSVladimir Oltean 	if (table->entry_count) {
7028aa9ebccSVladimir Oltean 		kfree(table->entries);
7038aa9ebccSVladimir Oltean 		table->entry_count = 0;
7048aa9ebccSVladimir Oltean 	}
7058aa9ebccSVladimir Oltean 
706fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
7078aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
7088aa9ebccSVladimir Oltean 	if (!table->entries)
7098aa9ebccSVladimir Oltean 		return -ENOMEM;
7108aa9ebccSVladimir Oltean 
711fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
7128aa9ebccSVladimir Oltean 
7136c0de59bSVladimir Oltean 	general_params = table->entries;
7146c0de59bSVladimir Oltean 
7158aa9ebccSVladimir Oltean 	/* This table only has a single entry */
7166c0de59bSVladimir Oltean 	general_params[0] = default_general_params;
7178aa9ebccSVladimir Oltean 
718ceec8bc0SVladimir Oltean 	sja1110_select_tdmaconfigidx(priv);
719ceec8bc0SVladimir Oltean 
7206c0de59bSVladimir Oltean 	/* Link-local traffic received on casc_port will be forwarded
7216c0de59bSVladimir Oltean 	 * to host_port without embedding the source port and device ID
7226c0de59bSVladimir Oltean 	 * info in the destination MAC address, and no RX timestamps will be
7236c0de59bSVladimir Oltean 	 * taken either (presumably because it is a cascaded port and a
7246c0de59bSVladimir Oltean 	 * downstream SJA switch already did that).
7256c0de59bSVladimir Oltean 	 * To disable the feature, we need to do different things depending on
7266c0de59bSVladimir Oltean 	 * switch generation. On SJA1105 we need to set an invalid port, while
7276c0de59bSVladimir Oltean 	 * on SJA1110 which support multiple cascaded ports, this field is a
7286c0de59bSVladimir Oltean 	 * bitmask so it must be left zero.
7296c0de59bSVladimir Oltean 	 */
7306c0de59bSVladimir Oltean 	if (!priv->info->multiple_cascade_ports)
7316c0de59bSVladimir Oltean 		general_params->casc_port = ds->num_ports;
7326c0de59bSVladimir Oltean 
7338aa9ebccSVladimir Oltean 	return 0;
7348aa9ebccSVladimir Oltean }
7358aa9ebccSVladimir Oltean 
73679d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
73779d5511cSVladimir Oltean {
73879d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
73979d5511cSVladimir Oltean 	struct sja1105_table *table;
74079d5511cSVladimir Oltean 
74179d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
74279d5511cSVladimir Oltean 
74379d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
74479d5511cSVladimir Oltean 	if (table->entry_count) {
74579d5511cSVladimir Oltean 		kfree(table->entries);
74679d5511cSVladimir Oltean 		table->entry_count = 0;
74779d5511cSVladimir Oltean 	}
74879d5511cSVladimir Oltean 
749fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
75079d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
75179d5511cSVladimir Oltean 	if (!table->entries)
75279d5511cSVladimir Oltean 		return -ENOMEM;
75379d5511cSVladimir Oltean 
754fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
75579d5511cSVladimir Oltean 
75679d5511cSVladimir Oltean 	avb = table->entries;
75779d5511cSVladimir Oltean 
75879d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
75979d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
76079d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
761747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
762747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
763747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
764747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
765747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
766747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
767747e5eb3SVladimir Oltean 	 */
768747e5eb3SVladimir Oltean 	avb->cas_master = false;
76979d5511cSVladimir Oltean 
77079d5511cSVladimir Oltean 	return 0;
77179d5511cSVladimir Oltean }
77279d5511cSVladimir Oltean 
773a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
774a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
775a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
776a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
777a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
778a7cc081cSVladimir Oltean  * will be used for this frame.
779a7cc081cSVladimir Oltean  *
780a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
781a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
782a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
783a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
784a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
785a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
786a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
787a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
788a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
789a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
790a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
791a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
792a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
793a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
794a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
795a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
796a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
797a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
798a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
799a7cc081cSVladimir Oltean  * +------------+--------+
800a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
801a7cc081cSVladimir Oltean  * +------------+--------+
802a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
803a7cc081cSVladimir Oltean  * +------------+--------+
804a7cc081cSVladimir Oltean  *    ...                                  ...
805a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
806a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
807a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
808a7cc081cSVladimir Oltean  *
809a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
810a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
811a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
812a7cc081cSVladimir Oltean  * lookup) equal.
813a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
814a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
815a7cc081cSVladimir Oltean  */
8168aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
8178aa9ebccSVladimir Oltean 
8188aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
8198aa9ebccSVladimir Oltean {
8208aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
821542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
8228aa9ebccSVladimir Oltean 	struct sja1105_table *table;
823a7cc081cSVladimir Oltean 	int port, tc;
8248aa9ebccSVladimir Oltean 
8258aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
8268aa9ebccSVladimir Oltean 
8278aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
8288aa9ebccSVladimir Oltean 	if (table->entry_count) {
8298aa9ebccSVladimir Oltean 		kfree(table->entries);
8308aa9ebccSVladimir Oltean 		table->entry_count = 0;
8318aa9ebccSVladimir Oltean 	}
8328aa9ebccSVladimir Oltean 
833fd6f2c25SVladimir Oltean 	table->entries = kcalloc(table->ops->max_entry_count,
8348aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
8358aa9ebccSVladimir Oltean 	if (!table->entries)
8368aa9ebccSVladimir Oltean 		return -ENOMEM;
8378aa9ebccSVladimir Oltean 
838fd6f2c25SVladimir Oltean 	table->entry_count = table->ops->max_entry_count;
8398aa9ebccSVladimir Oltean 
8408aa9ebccSVladimir Oltean 	policing = table->entries;
8418aa9ebccSVladimir Oltean 
842a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
843542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
84438fbe91fSVladimir Oltean 		int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port;
845542043e9SVladimir Oltean 		int bcast = (ds->num_ports * SJA1105_NUM_TC) + port;
846a7cc081cSVladimir Oltean 
847a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
848a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
849a7cc081cSVladimir Oltean 
850a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
85138fbe91fSVladimir Oltean 		/* Only SJA1110 has multicast policers */
85238fbe91fSVladimir Oltean 		if (mcast <= table->ops->max_entry_count)
85338fbe91fSVladimir Oltean 			policing[mcast].sharindx = port;
854a7cc081cSVladimir Oltean 	}
855a7cc081cSVladimir Oltean 
856a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
857542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
858c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
859c279c726SVladimir Oltean 
860a7cc081cSVladimir Oltean 		if (dsa_is_cpu_port(priv->ds, port))
861c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
8628aa9ebccSVladimir Oltean 
863a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
864a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
865a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
866a7cc081cSVladimir Oltean 		policing[port].partition = 0;
8678aa9ebccSVladimir Oltean 	}
868a7cc081cSVladimir Oltean 
8698aa9ebccSVladimir Oltean 	return 0;
8708aa9ebccSVladimir Oltean }
8718aa9ebccSVladimir Oltean 
8725d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv)
8738aa9ebccSVladimir Oltean {
8748aa9ebccSVladimir Oltean 	int rc;
8758aa9ebccSVladimir Oltean 
8768aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
8778aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
8788aa9ebccSVladimir Oltean 					priv->info->static_ops,
8798aa9ebccSVladimir Oltean 					priv->info->device_id);
8808aa9ebccSVladimir Oltean 	if (rc)
8818aa9ebccSVladimir Oltean 		return rc;
8828aa9ebccSVladimir Oltean 
8838aa9ebccSVladimir Oltean 	/* Build static configuration */
8848aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
8858aa9ebccSVladimir Oltean 	if (rc < 0)
8868aa9ebccSVladimir Oltean 		return rc;
8875d645df9SVladimir Oltean 	rc = sja1105_init_mii_settings(priv);
8888aa9ebccSVladimir Oltean 	if (rc < 0)
8898aa9ebccSVladimir Oltean 		return rc;
8908aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
8918aa9ebccSVladimir Oltean 	if (rc < 0)
8928aa9ebccSVladimir Oltean 		return rc;
8938aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
8948aa9ebccSVladimir Oltean 	if (rc < 0)
8958aa9ebccSVladimir Oltean 		return rc;
8968aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
8978aa9ebccSVladimir Oltean 	if (rc < 0)
8988aa9ebccSVladimir Oltean 		return rc;
8998aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
9008aa9ebccSVladimir Oltean 	if (rc < 0)
9018aa9ebccSVladimir Oltean 		return rc;
9028aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
9038aa9ebccSVladimir Oltean 	if (rc < 0)
9048aa9ebccSVladimir Oltean 		return rc;
9058aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
9068aa9ebccSVladimir Oltean 	if (rc < 0)
9078aa9ebccSVladimir Oltean 		return rc;
9088aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
9098aa9ebccSVladimir Oltean 	if (rc < 0)
9108aa9ebccSVladimir Oltean 		return rc;
91179d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
91279d5511cSVladimir Oltean 	if (rc < 0)
91379d5511cSVladimir Oltean 		return rc;
9143e77e59bSVladimir Oltean 	rc = sja1110_init_pcp_remapping(priv);
9153e77e59bSVladimir Oltean 	if (rc < 0)
9163e77e59bSVladimir Oltean 		return rc;
9178aa9ebccSVladimir Oltean 
9188aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
9198aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
9208aa9ebccSVladimir Oltean }
9218aa9ebccSVladimir Oltean 
92229afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv)
923f5b8631cSVladimir Oltean {
924542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
92529afb83aSVladimir Oltean 	int port;
926f5b8631cSVladimir Oltean 
92729afb83aSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
92829afb83aSVladimir Oltean 		if (!priv->fixed_link[port])
929f5b8631cSVladimir Oltean 			continue;
930f5b8631cSVladimir Oltean 
93129afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID ||
93229afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
93329afb83aSVladimir Oltean 			priv->rgmii_rx_delay[port] = true;
934f5b8631cSVladimir Oltean 
93529afb83aSVladimir Oltean 		if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID ||
93629afb83aSVladimir Oltean 		    priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID)
93729afb83aSVladimir Oltean 			priv->rgmii_tx_delay[port] = true;
938f5b8631cSVladimir Oltean 
93929afb83aSVladimir Oltean 		if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) &&
940f5b8631cSVladimir Oltean 		    !priv->info->setup_rgmii_delay)
941f5b8631cSVladimir Oltean 			return -EINVAL;
942f5b8631cSVladimir Oltean 	}
943f5b8631cSVladimir Oltean 	return 0;
944f5b8631cSVladimir Oltean }
945f5b8631cSVladimir Oltean 
9468aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
9478aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
9488aa9ebccSVladimir Oltean {
9498aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
9508aa9ebccSVladimir Oltean 	struct device_node *child;
9518aa9ebccSVladimir Oltean 
95227afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
9538aa9ebccSVladimir Oltean 		struct device_node *phy_node;
9540c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
9558aa9ebccSVladimir Oltean 		u32 index;
9560c65b2b9SAndrew Lunn 		int err;
9578aa9ebccSVladimir Oltean 
9588aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
9598aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
9608aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
9618aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
9627ba771e3SNishka Dasgupta 			of_node_put(child);
9638aa9ebccSVladimir Oltean 			return -ENODEV;
9648aa9ebccSVladimir Oltean 		}
9658aa9ebccSVladimir Oltean 
9668aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
9670c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
9680c65b2b9SAndrew Lunn 		if (err) {
9698aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
9708aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
9718aa9ebccSVladimir Oltean 				index);
9727ba771e3SNishka Dasgupta 			of_node_put(child);
9738aa9ebccSVladimir Oltean 			return -ENODEV;
9748aa9ebccSVladimir Oltean 		}
9758aa9ebccSVladimir Oltean 
9768aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
9778aa9ebccSVladimir Oltean 		if (!phy_node) {
9788aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
9798aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
9808aa9ebccSVladimir Oltean 					"properties missing!\n");
9817ba771e3SNishka Dasgupta 				of_node_put(child);
9828aa9ebccSVladimir Oltean 				return -ENODEV;
9838aa9ebccSVladimir Oltean 			}
9848aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
9858aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
9868aa9ebccSVladimir Oltean 			 */
98729afb83aSVladimir Oltean 			priv->fixed_link[index] = true;
9888aa9ebccSVladimir Oltean 		} else {
9898aa9ebccSVladimir Oltean 			of_node_put(phy_node);
9908aa9ebccSVladimir Oltean 		}
9918aa9ebccSVladimir Oltean 
992bf4edf4aSVladimir Oltean 		priv->phy_mode[index] = phy_mode;
9938aa9ebccSVladimir Oltean 	}
9948aa9ebccSVladimir Oltean 
9958aa9ebccSVladimir Oltean 	return 0;
9968aa9ebccSVladimir Oltean }
9978aa9ebccSVladimir Oltean 
9985d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv)
9998aa9ebccSVladimir Oltean {
10008aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
10018aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
10028aa9ebccSVladimir Oltean 	struct device_node *ports_node;
10038aa9ebccSVladimir Oltean 	int rc;
10048aa9ebccSVladimir Oltean 
10058aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
100615074a36SVladimir Oltean 	if (!ports_node)
100715074a36SVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
10088aa9ebccSVladimir Oltean 	if (!ports_node) {
10098aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
10108aa9ebccSVladimir Oltean 		return -ENODEV;
10118aa9ebccSVladimir Oltean 	}
10128aa9ebccSVladimir Oltean 
10135d645df9SVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports_node);
10148aa9ebccSVladimir Oltean 	of_node_put(ports_node);
10158aa9ebccSVladimir Oltean 
10168aa9ebccSVladimir Oltean 	return rc;
10178aa9ebccSVladimir Oltean }
10188aa9ebccSVladimir Oltean 
1019c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
102041fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv,
102141fed17fSVladimir Oltean 					 u64 speed)
102241fed17fSVladimir Oltean {
102341fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS])
102441fed17fSVladimir Oltean 		return SPEED_10;
102541fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS])
102641fed17fSVladimir Oltean 		return SPEED_100;
102741fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS])
102841fed17fSVladimir Oltean 		return SPEED_1000;
102941fed17fSVladimir Oltean 	if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS])
103041fed17fSVladimir Oltean 		return SPEED_2500;
103141fed17fSVladimir Oltean 	return SPEED_UNKNOWN;
103241fed17fSVladimir Oltean }
10338aa9ebccSVladimir Oltean 
10348400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
10358aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
10368400cff6SVladimir Oltean 				      int speed_mbps)
10378aa9ebccSVladimir Oltean {
10388aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
10398aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
104041fed17fSVladimir Oltean 	u64 speed;
10418aa9ebccSVladimir Oltean 	int rc;
10428aa9ebccSVladimir Oltean 
10438400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
10448400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
10458400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
10468400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
10478400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
10488400cff6SVladimir Oltean 	 */
10498aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
10508aa9ebccSVladimir Oltean 
1051f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
1052c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
1053a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
1054a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
1055a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
1056a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
1057a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
1058a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
1059a979a0abSVladimir Oltean 		 */
106041fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
1061f4cfcfbdSVladimir Oltean 		break;
1062c44d0535SVladimir Oltean 	case SPEED_10:
106341fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_10MBPS];
1064f4cfcfbdSVladimir Oltean 		break;
1065c44d0535SVladimir Oltean 	case SPEED_100:
106641fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_100MBPS];
1067f4cfcfbdSVladimir Oltean 		break;
1068c44d0535SVladimir Oltean 	case SPEED_1000:
106941fed17fSVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
1070f4cfcfbdSVladimir Oltean 		break;
107156b63466SVladimir Oltean 	case SPEED_2500:
107256b63466SVladimir Oltean 		speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
107356b63466SVladimir Oltean 		break;
1074f4cfcfbdSVladimir Oltean 	default:
10758aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
10768aa9ebccSVladimir Oltean 		return -EINVAL;
10778aa9ebccSVladimir Oltean 	}
10788aa9ebccSVladimir Oltean 
10798400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
10808400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
10818400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
10828400cff6SVladimir Oltean 	 * we want auto during upload phase).
1083ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
1084ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
10858aa9ebccSVladimir Oltean 	 */
108691a05078SVladimir Oltean 	if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII)
108741fed17fSVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
108856b63466SVladimir Oltean 	else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX)
108956b63466SVladimir Oltean 		mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS];
1090ffe10e67SVladimir Oltean 	else
10918aa9ebccSVladimir Oltean 		mac[port].speed = speed;
10928aa9ebccSVladimir Oltean 
10938aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
10948400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
10958400cff6SVladimir Oltean 					  &mac[port], true);
10968aa9ebccSVladimir Oltean 	if (rc < 0) {
10978aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
10988aa9ebccSVladimir Oltean 		return rc;
10998aa9ebccSVladimir Oltean 	}
11008aa9ebccSVladimir Oltean 
11018aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
11028aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
11038aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
11048aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
11058aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
11068aa9ebccSVladimir Oltean 	 */
110791a05078SVladimir Oltean 	if (!phy_interface_mode_is_rgmii(priv->phy_mode[port]))
11088aa9ebccSVladimir Oltean 		return 0;
11098aa9ebccSVladimir Oltean 
11108aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
11118aa9ebccSVladimir Oltean }
11128aa9ebccSVladimir Oltean 
111339710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
111439710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
111539710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
111639710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
111739710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
111839710229SVladimir Oltean  * now.
111939710229SVladimir Oltean  */
112039710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
112139710229SVladimir Oltean 				      phy_interface_t interface)
112239710229SVladimir Oltean {
1123bf4edf4aSVladimir Oltean 	return priv->phy_mode[port] != interface;
112439710229SVladimir Oltean }
112539710229SVladimir Oltean 
1126af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
1127ffe10e67SVladimir Oltean 			       unsigned int mode,
1128af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
11298aa9ebccSVladimir Oltean {
11303ad1d171SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
11318aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
11323ad1d171SVladimir Oltean 	struct dw_xpcs *xpcs;
11338aa9ebccSVladimir Oltean 
1134ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1135ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
1136ec8582d1SVladimir Oltean 			phy_modes(state->interface));
113739710229SVladimir Oltean 		return;
1138ec8582d1SVladimir Oltean 	}
113939710229SVladimir Oltean 
11403ad1d171SVladimir Oltean 	xpcs = priv->xpcs[port];
1141ffe10e67SVladimir Oltean 
11423ad1d171SVladimir Oltean 	if (xpcs)
11433ad1d171SVladimir Oltean 		phylink_set_pcs(dp->pl, &xpcs->pcs);
11448400cff6SVladimir Oltean }
11458400cff6SVladimir Oltean 
11468400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
11478400cff6SVladimir Oltean 				  unsigned int mode,
11488400cff6SVladimir Oltean 				  phy_interface_t interface)
11498400cff6SVladimir Oltean {
11508400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
11518400cff6SVladimir Oltean }
11528400cff6SVladimir Oltean 
11538400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
11548400cff6SVladimir Oltean 				unsigned int mode,
11558400cff6SVladimir Oltean 				phy_interface_t interface,
11565b502a7bSRussell King 				struct phy_device *phydev,
11575b502a7bSRussell King 				int speed, int duplex,
11585b502a7bSRussell King 				bool tx_pause, bool rx_pause)
11598400cff6SVladimir Oltean {
1160ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1161ec8582d1SVladimir Oltean 
1162ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1163ec8582d1SVladimir Oltean 
1164ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
11658aa9ebccSVladimir Oltean }
11668aa9ebccSVladimir Oltean 
1167ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1168ad9f299aSVladimir Oltean 				     unsigned long *supported,
1169ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
1170ad9f299aSVladimir Oltean {
1171ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
1172ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
1173ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
1174ad9f299aSVladimir Oltean 	 */
1175ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1176ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1177ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1178ad9f299aSVladimir Oltean 
1179ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1180ad9f299aSVladimir Oltean 
118139710229SVladimir Oltean 	/* include/linux/phylink.h says:
118239710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
118339710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
118439710229SVladimir Oltean 	 */
118539710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
118639710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
118739710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
118839710229SVladimir Oltean 		return;
118939710229SVladimir Oltean 	}
119039710229SVladimir Oltean 
1191ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1192ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1193ad9f299aSVladimir Oltean 	 */
1194ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
1195ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
1196ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
1197ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
1198ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
1199ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1200ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1201ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
120256b63466SVladimir Oltean 	if (priv->info->supports_2500basex[port]) {
120356b63466SVladimir Oltean 		phylink_set(mask, 2500baseT_Full);
120456b63466SVladimir Oltean 		phylink_set(mask, 2500baseX_Full);
120556b63466SVladimir Oltean 	}
1206ad9f299aSVladimir Oltean 
1207ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1208ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1209ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1210ad9f299aSVladimir Oltean }
1211ad9f299aSVladimir Oltean 
121260f6053fSVladimir Oltean static int
121360f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
121460f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
121560f6053fSVladimir Oltean {
121660f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
121760f6053fSVladimir Oltean 	struct sja1105_table *table;
121860f6053fSVladimir Oltean 	int i;
121960f6053fSVladimir Oltean 
122060f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
122160f6053fSVladimir Oltean 	l2_lookup = table->entries;
122260f6053fSVladimir Oltean 
122360f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
122460f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
122560f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
122660f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
122760f6053fSVladimir Oltean 			return i;
122860f6053fSVladimir Oltean 
122960f6053fSVladimir Oltean 	return -1;
123060f6053fSVladimir Oltean }
123160f6053fSVladimir Oltean 
123260f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
123360f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
123460f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
123560f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
123660f6053fSVladimir Oltean  */
123760f6053fSVladimir Oltean static int
123860f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
123960f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
124060f6053fSVladimir Oltean 			  bool keep)
124160f6053fSVladimir Oltean {
124260f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
124360f6053fSVladimir Oltean 	struct sja1105_table *table;
124460f6053fSVladimir Oltean 	int rc, match;
124560f6053fSVladimir Oltean 
124660f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
124760f6053fSVladimir Oltean 
124860f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
124960f6053fSVladimir Oltean 	if (match < 0) {
125060f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
125160f6053fSVladimir Oltean 		if (!keep)
125260f6053fSVladimir Oltean 			return 0;
125360f6053fSVladimir Oltean 
125460f6053fSVladimir Oltean 		/* No match => new entry */
125560f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
125660f6053fSVladimir Oltean 		if (rc)
125760f6053fSVladimir Oltean 			return rc;
125860f6053fSVladimir Oltean 
125960f6053fSVladimir Oltean 		match = table->entry_count - 1;
126060f6053fSVladimir Oltean 	}
126160f6053fSVladimir Oltean 
126260f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
126360f6053fSVladimir Oltean 	l2_lookup = table->entries;
126460f6053fSVladimir Oltean 
126560f6053fSVladimir Oltean 	/* We have a match.
126660f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
126760f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
126860f6053fSVladimir Oltean 	 * which we update it).
126960f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
127060f6053fSVladimir Oltean 	 */
127160f6053fSVladimir Oltean 	if (keep) {
127260f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
127360f6053fSVladimir Oltean 		return 0;
127460f6053fSVladimir Oltean 	}
127560f6053fSVladimir Oltean 
127660f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
127760f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
127860f6053fSVladimir Oltean 	 */
127960f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
128060f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
128160f6053fSVladimir Oltean }
128260f6053fSVladimir Oltean 
1283291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1284291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1285291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1286291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1287291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1288291d1e72SVladimir Oltean  */
128909c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1290291d1e72SVladimir Oltean {
1291291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1292291d1e72SVladimir Oltean }
1293291d1e72SVladimir Oltean 
12949dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1295291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1296291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1297291d1e72SVladimir Oltean 					 int *last_unused)
1298291d1e72SVladimir Oltean {
1299291d1e72SVladimir Oltean 	int way;
1300291d1e72SVladimir Oltean 
1301291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1302291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1303291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1304291d1e72SVladimir Oltean 
1305291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1306291d1e72SVladimir Oltean 		 * into the return value
1307291d1e72SVladimir Oltean 		 */
1308291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1309291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1310291d1e72SVladimir Oltean 			if (last_unused)
1311291d1e72SVladimir Oltean 				*last_unused = way;
1312291d1e72SVladimir Oltean 			continue;
1313291d1e72SVladimir Oltean 		}
1314291d1e72SVladimir Oltean 
1315291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1316291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1317291d1e72SVladimir Oltean 			if (match)
1318291d1e72SVladimir Oltean 				*match = l2_lookup;
1319291d1e72SVladimir Oltean 			return way;
1320291d1e72SVladimir Oltean 		}
1321291d1e72SVladimir Oltean 	}
1322291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1323291d1e72SVladimir Oltean 	return -1;
1324291d1e72SVladimir Oltean }
1325291d1e72SVladimir Oltean 
13269dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1327291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1328291d1e72SVladimir Oltean {
1329291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1330291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1331291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1332291d1e72SVladimir Oltean 	int last_unused = -1;
133360f6053fSVladimir Oltean 	int bin, way, rc;
1334291d1e72SVladimir Oltean 
13359dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1336291d1e72SVladimir Oltean 
13379dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1338291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1339291d1e72SVladimir Oltean 	if (way >= 0) {
1340291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1341291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1342291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1343291d1e72SVladimir Oltean 		 */
1344291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1345291d1e72SVladimir Oltean 			return 0;
1346291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1347291d1e72SVladimir Oltean 	} else {
1348291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1349291d1e72SVladimir Oltean 
1350291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1351291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1352291d1e72SVladimir Oltean 		 */
1353291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1354291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1355291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1356291d1e72SVladimir Oltean 
1357291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1358291d1e72SVladimir Oltean 			way = last_unused;
1359291d1e72SVladimir Oltean 		} else {
1360291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1361291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1362291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1363291d1e72SVladimir Oltean 			 * distribution function:
1364291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1365291d1e72SVladimir Oltean 			 */
1366291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1367291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1368291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1369291d1e72SVladimir Oltean 				 bin, addr, way);
1370291d1e72SVladimir Oltean 			/* Evict entry */
1371291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1372291d1e72SVladimir Oltean 						     index, NULL, false);
1373291d1e72SVladimir Oltean 		}
1374291d1e72SVladimir Oltean 	}
1375291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1376291d1e72SVladimir Oltean 
137760f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1378291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1379291d1e72SVladimir Oltean 					  true);
138060f6053fSVladimir Oltean 	if (rc < 0)
138160f6053fSVladimir Oltean 		return rc;
138260f6053fSVladimir Oltean 
138360f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1384291d1e72SVladimir Oltean }
1385291d1e72SVladimir Oltean 
13869dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1387291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1388291d1e72SVladimir Oltean {
1389291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1390291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
139160f6053fSVladimir Oltean 	int index, bin, way, rc;
1392291d1e72SVladimir Oltean 	bool keep;
1393291d1e72SVladimir Oltean 
13949dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
13959dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1396291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1397291d1e72SVladimir Oltean 	if (way < 0)
1398291d1e72SVladimir Oltean 		return 0;
1399291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1400291d1e72SVladimir Oltean 
1401291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1402291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1403291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1404291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1405291d1e72SVladimir Oltean 	 */
1406291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
14077752e937SVladimir Oltean 
1408291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1409291d1e72SVladimir Oltean 		keep = true;
1410291d1e72SVladimir Oltean 	else
1411291d1e72SVladimir Oltean 		keep = false;
1412291d1e72SVladimir Oltean 
141360f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1414291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
141560f6053fSVladimir Oltean 	if (rc < 0)
141660f6053fSVladimir Oltean 		return rc;
141760f6053fSVladimir Oltean 
141860f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1419291d1e72SVladimir Oltean }
1420291d1e72SVladimir Oltean 
14219dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
14229dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
14239dfa6911SVladimir Oltean {
14241da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
14251da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14261da73821SVladimir Oltean 	int rc, i;
14271da73821SVladimir Oltean 
14281da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
14291da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
14301da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
14311da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
14321da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
14330fac6aa0SVladimir Oltean 	if (priv->vlan_aware) {
14341da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
14351da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
14366d7c7d94SVladimir Oltean 	} else {
14376d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
14386d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
14396d7c7d94SVladimir Oltean 	}
14401da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
14411da73821SVladimir Oltean 
14421da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
14431da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
14441da73821SVladimir Oltean 	if (rc == 0) {
14451da73821SVladimir Oltean 		/* Found and this port is already in the entry's
14461da73821SVladimir Oltean 		 * port mask => job done
14471da73821SVladimir Oltean 		 */
14481da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
14491da73821SVladimir Oltean 			return 0;
14501da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
14511da73821SVladimir Oltean 		 * found something.
14521da73821SVladimir Oltean 		 */
14531da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
14541da73821SVladimir Oltean 		goto skip_finding_an_index;
14551da73821SVladimir Oltean 	}
14561da73821SVladimir Oltean 
14571da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
14581da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
14591da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
14601da73821SVladimir Oltean 	 */
14611da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
14621da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
14631da73821SVladimir Oltean 						 i, NULL);
14641da73821SVladimir Oltean 		if (rc < 0)
14651da73821SVladimir Oltean 			break;
14661da73821SVladimir Oltean 	}
14671da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
14681da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
14691da73821SVladimir Oltean 		return -EINVAL;
14701da73821SVladimir Oltean 	}
147117ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
14721da73821SVladimir Oltean 	l2_lookup.index = i;
14731da73821SVladimir Oltean 
14741da73821SVladimir Oltean skip_finding_an_index:
147560f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
14761da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
14771da73821SVladimir Oltean 					  true);
147860f6053fSVladimir Oltean 	if (rc < 0)
147960f6053fSVladimir Oltean 		return rc;
148060f6053fSVladimir Oltean 
148160f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
14829dfa6911SVladimir Oltean }
14839dfa6911SVladimir Oltean 
14849dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
14859dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
14869dfa6911SVladimir Oltean {
14871da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
14881da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14891da73821SVladimir Oltean 	bool keep;
14901da73821SVladimir Oltean 	int rc;
14911da73821SVladimir Oltean 
14921da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
14931da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
14941da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
14951da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
14960fac6aa0SVladimir Oltean 	if (priv->vlan_aware) {
14971da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
14981da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
14996d7c7d94SVladimir Oltean 	} else {
15006d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
15016d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
15026d7c7d94SVladimir Oltean 	}
15031da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
15041da73821SVladimir Oltean 
15051da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
15061da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
15071da73821SVladimir Oltean 	if (rc < 0)
15081da73821SVladimir Oltean 		return 0;
15091da73821SVladimir Oltean 
15101da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
15111da73821SVladimir Oltean 
15121da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
15131da73821SVladimir Oltean 	 * or if we remove it completely.
15141da73821SVladimir Oltean 	 */
15151da73821SVladimir Oltean 	if (l2_lookup.destports)
15161da73821SVladimir Oltean 		keep = true;
15171da73821SVladimir Oltean 	else
15181da73821SVladimir Oltean 		keep = false;
15191da73821SVladimir Oltean 
152060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
15211da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
152260f6053fSVladimir Oltean 	if (rc < 0)
152360f6053fSVladimir Oltean 		return rc;
152460f6053fSVladimir Oltean 
152560f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
15269dfa6911SVladimir Oltean }
15279dfa6911SVladimir Oltean 
15289dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
15299dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
15309dfa6911SVladimir Oltean {
15319dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1532b3ee526aSVladimir Oltean 
15336d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
15349dfa6911SVladimir Oltean }
15359dfa6911SVladimir Oltean 
15369dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
15379dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
15389dfa6911SVladimir Oltean {
15399dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15409dfa6911SVladimir Oltean 
1541b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
15429dfa6911SVladimir Oltean }
15439dfa6911SVladimir Oltean 
1544291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1545291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1546291d1e72SVladimir Oltean {
1547291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1548291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1549291d1e72SVladimir Oltean 	int i;
1550291d1e72SVladimir Oltean 
1551291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1552291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1553291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1554291d1e72SVladimir Oltean 		int rc;
1555291d1e72SVladimir Oltean 
1556291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1557291d1e72SVladimir Oltean 						 i, &l2_lookup);
1558291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1559def84604SVladimir Oltean 		if (rc == -ENOENT)
1560291d1e72SVladimir Oltean 			continue;
1561291d1e72SVladimir Oltean 		if (rc) {
1562291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1563291d1e72SVladimir Oltean 			return rc;
1564291d1e72SVladimir Oltean 		}
1565291d1e72SVladimir Oltean 
1566291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1567291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1568291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1569291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1570291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1571291d1e72SVladimir Oltean 		 */
1572291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1573291d1e72SVladimir Oltean 			continue;
15744d942354SVladimir Oltean 
15754d942354SVladimir Oltean 		/* We need to hide the FDB entry for unknown multicast */
15764d942354SVladimir Oltean 		if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST &&
15774d942354SVladimir Oltean 		    l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
15784d942354SVladimir Oltean 			continue;
15794d942354SVladimir Oltean 
1580291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
158193647594SVladimir Oltean 
15826d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
15830fac6aa0SVladimir Oltean 		if (!priv->vlan_aware)
15846d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
158517ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1586291d1e72SVladimir Oltean 	}
1587291d1e72SVladimir Oltean 	return 0;
1588291d1e72SVladimir Oltean }
1589291d1e72SVladimir Oltean 
1590a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port,
1591291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1592291d1e72SVladimir Oltean {
1593a52b2da7SVladimir Oltean 	return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1594291d1e72SVladimir Oltean }
1595291d1e72SVladimir Oltean 
1596291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1597291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1598291d1e72SVladimir Oltean {
1599291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1600291d1e72SVladimir Oltean }
1601291d1e72SVladimir Oltean 
16027f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration.
16037f7ccdeaSVladimir Oltean  * Flooding is configured between each {ingress, egress} port pair, and since
16047f7ccdeaSVladimir Oltean  * the bridge's semantics are those of "egress flooding", it means we must
16057f7ccdeaSVladimir Oltean  * enable flooding towards this port from all ingress ports that are in the
16067f7ccdeaSVladimir Oltean  * same forwarding domain.
16077f7ccdeaSVladimir Oltean  */
16087f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv)
16097f7ccdeaSVladimir Oltean {
16107f7ccdeaSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
16117f7ccdeaSVladimir Oltean 	struct dsa_switch *ds = priv->ds;
16127f7ccdeaSVladimir Oltean 	int from, to, rc;
16137f7ccdeaSVladimir Oltean 
16147f7ccdeaSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
16157f7ccdeaSVladimir Oltean 
16167f7ccdeaSVladimir Oltean 	for (from = 0; from < ds->num_ports; from++) {
16177f7ccdeaSVladimir Oltean 		u64 fl_domain = 0, bc_domain = 0;
16187f7ccdeaSVladimir Oltean 
16197f7ccdeaSVladimir Oltean 		for (to = 0; to < priv->ds->num_ports; to++) {
16207f7ccdeaSVladimir Oltean 			if (!sja1105_can_forward(l2_fwd, from, to))
16217f7ccdeaSVladimir Oltean 				continue;
16227f7ccdeaSVladimir Oltean 
16237f7ccdeaSVladimir Oltean 			if (priv->ucast_egress_floods & BIT(to))
16247f7ccdeaSVladimir Oltean 				fl_domain |= BIT(to);
16257f7ccdeaSVladimir Oltean 			if (priv->bcast_egress_floods & BIT(to))
16267f7ccdeaSVladimir Oltean 				bc_domain |= BIT(to);
16277f7ccdeaSVladimir Oltean 		}
16287f7ccdeaSVladimir Oltean 
16297f7ccdeaSVladimir Oltean 		/* Nothing changed, nothing to do */
16307f7ccdeaSVladimir Oltean 		if (l2_fwd[from].fl_domain == fl_domain &&
16317f7ccdeaSVladimir Oltean 		    l2_fwd[from].bc_domain == bc_domain)
16327f7ccdeaSVladimir Oltean 			continue;
16337f7ccdeaSVladimir Oltean 
16347f7ccdeaSVladimir Oltean 		l2_fwd[from].fl_domain = fl_domain;
16357f7ccdeaSVladimir Oltean 		l2_fwd[from].bc_domain = bc_domain;
16367f7ccdeaSVladimir Oltean 
16377f7ccdeaSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
16387f7ccdeaSVladimir Oltean 						  from, &l2_fwd[from], true);
16397f7ccdeaSVladimir Oltean 		if (rc < 0)
16407f7ccdeaSVladimir Oltean 			return rc;
16417f7ccdeaSVladimir Oltean 	}
16427f7ccdeaSVladimir Oltean 
16437f7ccdeaSVladimir Oltean 	return 0;
16447f7ccdeaSVladimir Oltean }
16457f7ccdeaSVladimir Oltean 
16468aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
16478aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
16488aa9ebccSVladimir Oltean {
16498aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
16508aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16518aa9ebccSVladimir Oltean 	int i, rc;
16528aa9ebccSVladimir Oltean 
16538aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
16548aa9ebccSVladimir Oltean 
1655542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
16568aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
16578aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
16588aa9ebccSVladimir Oltean 		 */
16598aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
16608aa9ebccSVladimir Oltean 			continue;
16618aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
16628aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
16638aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
16648aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
16658aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
16668aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
16678aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
16688aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
16698aa9ebccSVladimir Oltean 		 */
16708aa9ebccSVladimir Oltean 		if (i == port)
16718aa9ebccSVladimir Oltean 			continue;
16728aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
16738aa9ebccSVladimir Oltean 			continue;
16748aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
16758aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
16768aa9ebccSVladimir Oltean 
16778aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
16788aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
16798aa9ebccSVladimir Oltean 		if (rc < 0)
16808aa9ebccSVladimir Oltean 			return rc;
16818aa9ebccSVladimir Oltean 	}
16828aa9ebccSVladimir Oltean 
16837f7ccdeaSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
16848aa9ebccSVladimir Oltean 					  port, &l2_fwd[port], true);
16857f7ccdeaSVladimir Oltean 	if (rc)
16867f7ccdeaSVladimir Oltean 		return rc;
16877f7ccdeaSVladimir Oltean 
1688*cde8078eSVladimir Oltean 	rc = sja1105_commit_pvid(ds, port);
1689*cde8078eSVladimir Oltean 	if (rc)
1690*cde8078eSVladimir Oltean 		return rc;
1691*cde8078eSVladimir Oltean 
16927f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
16938aa9ebccSVladimir Oltean }
16948aa9ebccSVladimir Oltean 
1695640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1696640f763fSVladimir Oltean 					 u8 state)
1697640f763fSVladimir Oltean {
1698640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1699640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1700640f763fSVladimir Oltean 
1701640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1702640f763fSVladimir Oltean 
1703640f763fSVladimir Oltean 	switch (state) {
1704640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1705640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1706640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1707640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1708640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1709640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1710640f763fSVladimir Oltean 		 */
1711640f763fSVladimir Oltean 		mac[port].ingress   = false;
1712640f763fSVladimir Oltean 		mac[port].egress    = false;
1713640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1714640f763fSVladimir Oltean 		break;
1715640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1716640f763fSVladimir Oltean 		mac[port].ingress   = true;
1717640f763fSVladimir Oltean 		mac[port].egress    = false;
1718640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1719640f763fSVladimir Oltean 		break;
1720640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1721640f763fSVladimir Oltean 		mac[port].ingress   = true;
1722640f763fSVladimir Oltean 		mac[port].egress    = false;
17234d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1724640f763fSVladimir Oltean 		break;
1725640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1726640f763fSVladimir Oltean 		mac[port].ingress   = true;
1727640f763fSVladimir Oltean 		mac[port].egress    = true;
17284d942354SVladimir Oltean 		mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1729640f763fSVladimir Oltean 		break;
1730640f763fSVladimir Oltean 	default:
1731640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1732640f763fSVladimir Oltean 		return;
1733640f763fSVladimir Oltean 	}
1734640f763fSVladimir Oltean 
1735640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1736640f763fSVladimir Oltean 				     &mac[port], true);
1737640f763fSVladimir Oltean }
1738640f763fSVladimir Oltean 
17398aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
17408aa9ebccSVladimir Oltean 			       struct net_device *br)
17418aa9ebccSVladimir Oltean {
17428aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
17438aa9ebccSVladimir Oltean }
17448aa9ebccSVladimir Oltean 
17458aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
17468aa9ebccSVladimir Oltean 				 struct net_device *br)
17478aa9ebccSVladimir Oltean {
17488aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
17498aa9ebccSVladimir Oltean }
17508aa9ebccSVladimir Oltean 
17514d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8)
17524d752508SVladimir Oltean 
17534d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
17544d752508SVladimir Oltean {
17554d752508SVladimir Oltean 	int i;
17564d752508SVladimir Oltean 
17574d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
17584d752508SVladimir Oltean 		if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
17594d752508SVladimir Oltean 			return i;
17604d752508SVladimir Oltean 
17614d752508SVladimir Oltean 	return -1;
17624d752508SVladimir Oltean }
17634d752508SVladimir Oltean 
17644d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
17654d752508SVladimir Oltean 				     int prio)
17664d752508SVladimir Oltean {
17674d752508SVladimir Oltean 	int i;
17684d752508SVladimir Oltean 
17694d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
17704d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
17714d752508SVladimir Oltean 
17724d752508SVladimir Oltean 		if (cbs->port == port && cbs->prio == prio) {
17734d752508SVladimir Oltean 			memset(cbs, 0, sizeof(*cbs));
17744d752508SVladimir Oltean 			return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
17754d752508SVladimir Oltean 							    i, cbs, true);
17764d752508SVladimir Oltean 		}
17774d752508SVladimir Oltean 	}
17784d752508SVladimir Oltean 
17794d752508SVladimir Oltean 	return 0;
17804d752508SVladimir Oltean }
17814d752508SVladimir Oltean 
17824d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
17834d752508SVladimir Oltean 				struct tc_cbs_qopt_offload *offload)
17844d752508SVladimir Oltean {
17854d752508SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
17864d752508SVladimir Oltean 	struct sja1105_cbs_entry *cbs;
17874d752508SVladimir Oltean 	int index;
17884d752508SVladimir Oltean 
17894d752508SVladimir Oltean 	if (!offload->enable)
17904d752508SVladimir Oltean 		return sja1105_delete_cbs_shaper(priv, port, offload->queue);
17914d752508SVladimir Oltean 
17924d752508SVladimir Oltean 	index = sja1105_find_unused_cbs_shaper(priv);
17934d752508SVladimir Oltean 	if (index < 0)
17944d752508SVladimir Oltean 		return -ENOSPC;
17954d752508SVladimir Oltean 
17964d752508SVladimir Oltean 	cbs = &priv->cbs[index];
17974d752508SVladimir Oltean 	cbs->port = port;
17984d752508SVladimir Oltean 	cbs->prio = offload->queue;
17994d752508SVladimir Oltean 	/* locredit and sendslope are negative by definition. In hardware,
18004d752508SVladimir Oltean 	 * positive values must be provided, and the negative sign is implicit.
18014d752508SVladimir Oltean 	 */
18024d752508SVladimir Oltean 	cbs->credit_hi = offload->hicredit;
18034d752508SVladimir Oltean 	cbs->credit_lo = abs(offload->locredit);
18044d752508SVladimir Oltean 	/* User space is in kbits/sec, hardware in bytes/sec */
18054d752508SVladimir Oltean 	cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
18064d752508SVladimir Oltean 	cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
18074d752508SVladimir Oltean 	/* Convert the negative values from 64-bit 2's complement
18084d752508SVladimir Oltean 	 * to 32-bit 2's complement (for the case of 0x80000000 whose
18094d752508SVladimir Oltean 	 * negative is still negative).
18104d752508SVladimir Oltean 	 */
18114d752508SVladimir Oltean 	cbs->credit_lo &= GENMASK_ULL(31, 0);
18124d752508SVladimir Oltean 	cbs->send_slope &= GENMASK_ULL(31, 0);
18134d752508SVladimir Oltean 
18144d752508SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
18154d752508SVladimir Oltean 					    true);
18164d752508SVladimir Oltean }
18174d752508SVladimir Oltean 
18184d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv)
18194d752508SVladimir Oltean {
18204d752508SVladimir Oltean 	int rc = 0, i;
18214d752508SVladimir Oltean 
1822be7f62eeSVladimir Oltean 	/* The credit based shapers are only allocated if
1823be7f62eeSVladimir Oltean 	 * CONFIG_NET_SCH_CBS is enabled.
1824be7f62eeSVladimir Oltean 	 */
1825be7f62eeSVladimir Oltean 	if (!priv->cbs)
1826be7f62eeSVladimir Oltean 		return 0;
1827be7f62eeSVladimir Oltean 
18284d752508SVladimir Oltean 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
18294d752508SVladimir Oltean 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
18304d752508SVladimir Oltean 
18314d752508SVladimir Oltean 		if (!cbs->idle_slope && !cbs->send_slope)
18324d752508SVladimir Oltean 			continue;
18334d752508SVladimir Oltean 
18344d752508SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
18354d752508SVladimir Oltean 						  true);
18364d752508SVladimir Oltean 		if (rc)
18374d752508SVladimir Oltean 			break;
18384d752508SVladimir Oltean 	}
18394d752508SVladimir Oltean 
18404d752508SVladimir Oltean 	return rc;
18414d752508SVladimir Oltean }
18424d752508SVladimir Oltean 
18432eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
18442eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
18452eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
18462eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
18472eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
1848c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1849dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
18502eea1fa8SVladimir Oltean };
18512eea1fa8SVladimir Oltean 
18526666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
18536666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
18546666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
18556666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
18566666cebcSVladimir Oltean  * such that this operation is relatively seamless.
18576666cebcSVladimir Oltean  */
18582eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
18592eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
18606666cebcSVladimir Oltean {
18616cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
18626cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
186382760d7fSVladimir Oltean 	int speed_mbps[SJA1105_MAX_NUM_PORTS];
186484db00f2SVladimir Oltean 	u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0};
18656666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
18666cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
18676cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
18686cf99c13SVladimir Oltean 	s64 t12, t34;
18696666cebcSVladimir Oltean 	int rc, i;
18706cf99c13SVladimir Oltean 	s64 now;
18716666cebcSVladimir Oltean 
1872af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1873af580ae2SVladimir Oltean 
18746666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
18756666cebcSVladimir Oltean 
18768400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
18778400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
18788400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
18798400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
18806666cebcSVladimir Oltean 	 */
1881542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
18823ad1d171SVladimir Oltean 		u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1);
18833ad1d171SVladimir Oltean 
188441fed17fSVladimir Oltean 		speed_mbps[i] = sja1105_port_speed_to_ethtool(priv,
188541fed17fSVladimir Oltean 							      mac[i].speed);
188641fed17fSVladimir Oltean 		mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
18876666cebcSVladimir Oltean 
18883ad1d171SVladimir Oltean 		if (priv->xpcs[i])
18893ad1d171SVladimir Oltean 			bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr);
189084db00f2SVladimir Oltean 	}
1891ffe10e67SVladimir Oltean 
18926cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
18936cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
18946cf99c13SVladimir Oltean 
18956cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
189661c77533SVladimir Oltean 	if (rc < 0) {
189761c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
189861c77533SVladimir Oltean 		goto out;
189961c77533SVladimir Oltean 	}
19006cf99c13SVladimir Oltean 
19016666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
19026666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
190361c77533SVladimir Oltean 	if (rc < 0) {
190461c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
190561c77533SVladimir Oltean 		goto out;
190661c77533SVladimir Oltean 	}
19076cf99c13SVladimir Oltean 
19086cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
190961c77533SVladimir Oltean 	if (rc < 0) {
191061c77533SVladimir Oltean 		mutex_unlock(&priv->ptp_data.lock);
191161c77533SVladimir Oltean 		goto out;
191261c77533SVladimir Oltean 	}
19136cf99c13SVladimir Oltean 
19146cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
19156cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
19166cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
19176cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
19186cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
19196cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
19206cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
19216cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
19226cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
19236cf99c13SVladimir Oltean 	now += (t34 - t12);
19246cf99c13SVladimir Oltean 
19256cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
19266cf99c13SVladimir Oltean 
19276cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
19286666cebcSVladimir Oltean 
19292eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
19302eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
19312eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
19322eea1fa8SVladimir Oltean 
19336666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
19346666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
19356666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
19366666cebcSVladimir Oltean 	 */
1937cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
1938c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
19396666cebcSVladimir Oltean 		if (rc < 0)
19406666cebcSVladimir Oltean 			goto out;
1941cb5a82d2SVladimir Oltean 	}
19426666cebcSVladimir Oltean 
1943542043e9SVladimir Oltean 	for (i = 0; i < ds->num_ports; i++) {
19443ad1d171SVladimir Oltean 		struct dw_xpcs *xpcs = priv->xpcs[i];
19453ad1d171SVladimir Oltean 		unsigned int mode;
194684db00f2SVladimir Oltean 
19478400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
19486666cebcSVladimir Oltean 		if (rc < 0)
19496666cebcSVladimir Oltean 			goto out;
1950ffe10e67SVladimir Oltean 
19513ad1d171SVladimir Oltean 		if (!xpcs)
195284db00f2SVladimir Oltean 			continue;
1953ffe10e67SVladimir Oltean 
19543ad1d171SVladimir Oltean 		if (bmcr[i] & BMCR_ANENABLE)
19553ad1d171SVladimir Oltean 			mode = MLO_AN_INBAND;
19563ad1d171SVladimir Oltean 		else if (priv->fixed_link[i])
19573ad1d171SVladimir Oltean 			mode = MLO_AN_FIXED;
19583ad1d171SVladimir Oltean 		else
19593ad1d171SVladimir Oltean 			mode = MLO_AN_PHY;
196084db00f2SVladimir Oltean 
19613ad1d171SVladimir Oltean 		rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode);
19623ad1d171SVladimir Oltean 		if (rc < 0)
19633ad1d171SVladimir Oltean 			goto out;
1964ffe10e67SVladimir Oltean 
19653ad1d171SVladimir Oltean 		if (!phylink_autoneg_inband(mode)) {
1966ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
1967ffe10e67SVladimir Oltean 
196856b63466SVladimir Oltean 			if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX)
196956b63466SVladimir Oltean 				speed = SPEED_2500;
197056b63466SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED1000)
1971ffe10e67SVladimir Oltean 				speed = SPEED_1000;
197284db00f2SVladimir Oltean 			else if (bmcr[i] & BMCR_SPEED100)
1973ffe10e67SVladimir Oltean 				speed = SPEED_100;
1974053d8ad1SVladimir Oltean 			else
1975ffe10e67SVladimir Oltean 				speed = SPEED_10;
1976ffe10e67SVladimir Oltean 
19773ad1d171SVladimir Oltean 			xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i],
19783ad1d171SVladimir Oltean 				     speed, DUPLEX_FULL);
1979ffe10e67SVladimir Oltean 		}
1980ffe10e67SVladimir Oltean 	}
19814d752508SVladimir Oltean 
19824d752508SVladimir Oltean 	rc = sja1105_reload_cbs(priv);
19834d752508SVladimir Oltean 	if (rc < 0)
19844d752508SVladimir Oltean 		goto out;
19856666cebcSVladimir Oltean out:
1986af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1987af580ae2SVladimir Oltean 
19886666cebcSVladimir Oltean 	return rc;
19896666cebcSVladimir Oltean }
19906666cebcSVladimir Oltean 
19918aa9ebccSVladimir Oltean static enum dsa_tag_protocol
19924d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
19934d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
19948aa9ebccSVladimir Oltean {
19954913b8ebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
19964913b8ebSVladimir Oltean 
19974913b8ebSVladimir Oltean 	return priv->info->tag_proto;
19988aa9ebccSVladimir Oltean }
19998aa9ebccSVladimir Oltean 
2000ec5ae610SVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
2001ec5ae610SVladimir Oltean {
2002ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
2003ec5ae610SVladimir Oltean 	int count, i;
2004ec5ae610SVladimir Oltean 
2005ec5ae610SVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
2006ec5ae610SVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
2007ec5ae610SVladimir Oltean 
2008ec5ae610SVladimir Oltean 	for (i = 0; i < count; i++)
2009ec5ae610SVladimir Oltean 		if (vlan[i].vlanid == vid)
2010ec5ae610SVladimir Oltean 			return i;
2011ec5ae610SVladimir Oltean 
2012ec5ae610SVladimir Oltean 	/* Return an invalid entry index if not found */
2013ec5ae610SVladimir Oltean 	return -1;
2014ec5ae610SVladimir Oltean }
2015ec5ae610SVladimir Oltean 
2016070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
2017070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
2018070ca3bbSVladimir Oltean  * So a switch reset is required.
2019070ca3bbSVladimir Oltean  */
202089153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
202189153ed6SVladimir Oltean 			   struct netlink_ext_ack *extack)
20226666cebcSVladimir Oltean {
20236d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2024070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
20256666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2026070ca3bbSVladimir Oltean 	struct sja1105_table *table;
2027dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
2028070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
20296666cebcSVladimir Oltean 	int rc;
20306666cebcSVladimir Oltean 
2031dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
2032dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
203389153ed6SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
203489153ed6SVladimir Oltean 					   "Cannot change VLAN filtering with active VL rules");
2035dfacc5a2SVladimir Oltean 			return -EBUSY;
2036dfacc5a2SVladimir Oltean 		}
2037dfacc5a2SVladimir Oltean 	}
2038dfacc5a2SVladimir Oltean 
2039070ca3bbSVladimir Oltean 	if (enabled) {
20406666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
204154fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
204254fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
2043070ca3bbSVladimir Oltean 	} else {
20446666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
2045070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
2046070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
2047070ca3bbSVladimir Oltean 	}
2048070ca3bbSVladimir Oltean 
204938b5beeaSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
205038b5beeaSVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
205138b5beeaSVladimir Oltean 
205238b5beeaSVladimir Oltean 		if (enabled)
205338b5beeaSVladimir Oltean 			sp->xmit_tpid = priv->info->qinq_tpid;
205438b5beeaSVladimir Oltean 		else
205538b5beeaSVladimir Oltean 			sp->xmit_tpid = ETH_P_SJA1105;
205638b5beeaSVladimir Oltean 	}
205738b5beeaSVladimir Oltean 
20580fac6aa0SVladimir Oltean 	if (priv->vlan_aware == enabled)
2059cfa36b1fSVladimir Oltean 		return 0;
2060cfa36b1fSVladimir Oltean 
20610fac6aa0SVladimir Oltean 	priv->vlan_aware = enabled;
20627f14937fSVladimir Oltean 
2063070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2064070ca3bbSVladimir Oltean 	general_params = table->entries;
2065f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
206654fa49eeSVladimir Oltean 	general_params->tpid = tpid;
206754fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2068070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
206942824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
207042824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
207142824463SVladimir Oltean 	 */
207242824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
207342824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
2074070ca3bbSVladimir Oltean 
20756d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
20762cafa72eSVladimir Oltean 	 * No VLAN filtering (or best effort) => shared VLAN learning.
20776d7c7d94SVladimir Oltean 	 *
20786d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
20796d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
20806d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
20816d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
20826d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
20836d7c7d94SVladimir Oltean 	 * forwarding decision.
20846d7c7d94SVladimir Oltean 	 *
20856d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
20866d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
20876d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
20886d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
20896d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
20906d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
20916d7c7d94SVladimir Oltean 	 * (all frames get flooded).
20926d7c7d94SVladimir Oltean 	 */
20936d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
20946d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
20950fac6aa0SVladimir Oltean 	l2_lookup_params->shared_learn = !priv->vlan_aware;
2096aaa270c6SVladimir Oltean 
20976dfd23d3SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
20986dfd23d3SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
20996dfd23d3SVladimir Oltean 			continue;
21006dfd23d3SVladimir Oltean 
21016dfd23d3SVladimir Oltean 		rc = sja1105_commit_pvid(ds, port);
2102aef31718SVladimir Oltean 		if (rc)
2103aef31718SVladimir Oltean 			return rc;
21046dfd23d3SVladimir Oltean 	}
2105aef31718SVladimir Oltean 
21062eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
21076666cebcSVladimir Oltean 	if (rc)
210889153ed6SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype");
21096666cebcSVladimir Oltean 
21100fac6aa0SVladimir Oltean 	return rc;
21116666cebcSVladimir Oltean }
21126666cebcSVladimir Oltean 
21136dfd23d3SVladimir Oltean static int sja1105_vlan_add(struct sja1105_private *priv, int port, u16 vid,
21146dfd23d3SVladimir Oltean 			    u16 flags)
21155899ee36SVladimir Oltean {
21166dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
21176dfd23d3SVladimir Oltean 	struct sja1105_table *table;
21186dfd23d3SVladimir Oltean 	int match, rc;
21195899ee36SVladimir Oltean 
21206dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
21216dfd23d3SVladimir Oltean 
21226dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
21236dfd23d3SVladimir Oltean 	if (match < 0) {
21246dfd23d3SVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
21256dfd23d3SVladimir Oltean 		if (rc)
21266dfd23d3SVladimir Oltean 			return rc;
21276dfd23d3SVladimir Oltean 		match = table->entry_count - 1;
21286dfd23d3SVladimir Oltean 	}
21296dfd23d3SVladimir Oltean 
21306dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
21316dfd23d3SVladimir Oltean 	vlan = table->entries;
21326dfd23d3SVladimir Oltean 
21336dfd23d3SVladimir Oltean 	vlan[match].type_entry = SJA1110_VLAN_D_TAG;
21346dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
21356dfd23d3SVladimir Oltean 	vlan[match].vlan_bc |= BIT(port);
21366dfd23d3SVladimir Oltean 	vlan[match].vmemb_port |= BIT(port);
21376dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
21386dfd23d3SVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
21396dfd23d3SVladimir Oltean 	else
21406dfd23d3SVladimir Oltean 		vlan[match].tag_port |= BIT(port);
21416dfd23d3SVladimir Oltean 
21426dfd23d3SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
21436dfd23d3SVladimir Oltean 					    &vlan[match], true);
21446dfd23d3SVladimir Oltean }
21456dfd23d3SVladimir Oltean 
21466dfd23d3SVladimir Oltean static int sja1105_vlan_del(struct sja1105_private *priv, int port, u16 vid)
21476dfd23d3SVladimir Oltean {
21486dfd23d3SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
21496dfd23d3SVladimir Oltean 	struct sja1105_table *table;
21506dfd23d3SVladimir Oltean 	bool keep = true;
21516dfd23d3SVladimir Oltean 	int match, rc;
21526dfd23d3SVladimir Oltean 
21536dfd23d3SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
21546dfd23d3SVladimir Oltean 
21556dfd23d3SVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
21566dfd23d3SVladimir Oltean 	/* Can't delete a missing entry. */
21576dfd23d3SVladimir Oltean 	if (match < 0)
21585899ee36SVladimir Oltean 		return 0;
21595899ee36SVladimir Oltean 
21606dfd23d3SVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
21616dfd23d3SVladimir Oltean 	vlan = table->entries;
21626dfd23d3SVladimir Oltean 
21636dfd23d3SVladimir Oltean 	vlan[match].vlanid = vid;
21646dfd23d3SVladimir Oltean 	vlan[match].vlan_bc &= ~BIT(port);
21656dfd23d3SVladimir Oltean 	vlan[match].vmemb_port &= ~BIT(port);
21666dfd23d3SVladimir Oltean 	/* Also unset tag_port, just so we don't have a confusing bitmap
21676dfd23d3SVladimir Oltean 	 * (no practical purpose).
2168b38e659dSVladimir Oltean 	 */
21696dfd23d3SVladimir Oltean 	vlan[match].tag_port &= ~BIT(port);
2170b38e659dSVladimir Oltean 
21716dfd23d3SVladimir Oltean 	/* If there's no port left as member of this VLAN,
21726dfd23d3SVladimir Oltean 	 * it's time for it to go.
21736dfd23d3SVladimir Oltean 	 */
21746dfd23d3SVladimir Oltean 	if (!vlan[match].vmemb_port)
21756dfd23d3SVladimir Oltean 		keep = false;
21765899ee36SVladimir Oltean 
21776dfd23d3SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
21786dfd23d3SVladimir Oltean 					  &vlan[match], keep);
21796dfd23d3SVladimir Oltean 	if (rc < 0)
21806dfd23d3SVladimir Oltean 		return rc;
21815899ee36SVladimir Oltean 
21826dfd23d3SVladimir Oltean 	if (!keep)
21836dfd23d3SVladimir Oltean 		return sja1105_table_delete_entry(table, match);
21845899ee36SVladimir Oltean 
21855899ee36SVladimir Oltean 	return 0;
21865899ee36SVladimir Oltean }
21875899ee36SVladimir Oltean 
21886dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_add(struct dsa_switch *ds, int port,
218931046a5fSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan,
219031046a5fSVladimir Oltean 				   struct netlink_ext_ack *extack)
21916666cebcSVladimir Oltean {
21926666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2193884be12fSVladimir Oltean 	u16 flags = vlan->flags;
21946666cebcSVladimir Oltean 	int rc;
21956666cebcSVladimir Oltean 
21960fac6aa0SVladimir Oltean 	/* Be sure to deny alterations to the configuration done by tag_8021q.
21971958d581SVladimir Oltean 	 */
21980fac6aa0SVladimir Oltean 	if (vid_is_dsa_8021q(vlan->vid)) {
219931046a5fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
220031046a5fSVladimir Oltean 				   "Range 1024-3071 reserved for dsa_8021q operation");
22011958d581SVladimir Oltean 		return -EBUSY;
22021958d581SVladimir Oltean 	}
22031958d581SVladimir Oltean 
2204884be12fSVladimir Oltean 	/* Always install bridge VLANs as egress-tagged on the CPU port. */
2205884be12fSVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
2206884be12fSVladimir Oltean 		flags = 0;
2207884be12fSVladimir Oltean 
2208884be12fSVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vlan->vid, flags);
22096dfd23d3SVladimir Oltean 	if (rc)
22101958d581SVladimir Oltean 		return rc;
2211ec5ae610SVladimir Oltean 
22126dfd23d3SVladimir Oltean 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
22136dfd23d3SVladimir Oltean 		priv->bridge_pvid[port] = vlan->vid;
2214ec5ae610SVladimir Oltean 
22156dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
22166666cebcSVladimir Oltean }
22176666cebcSVladimir Oltean 
22186dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port,
22196666cebcSVladimir Oltean 				   const struct switchdev_obj_port_vlan *vlan)
22206666cebcSVladimir Oltean {
22216666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
22226666cebcSVladimir Oltean 
22236dfd23d3SVladimir Oltean 	return sja1105_vlan_del(priv, port, vlan->vid);
22246666cebcSVladimir Oltean }
22256666cebcSVladimir Oltean 
22265899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
22275899ee36SVladimir Oltean 				      u16 flags)
22285899ee36SVladimir Oltean {
22295899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
22305899ee36SVladimir Oltean 	int rc;
22315899ee36SVladimir Oltean 
22326dfd23d3SVladimir Oltean 	rc = sja1105_vlan_add(priv, port, vid, flags);
22336dfd23d3SVladimir Oltean 	if (rc)
22345899ee36SVladimir Oltean 		return rc;
22355899ee36SVladimir Oltean 
22366dfd23d3SVladimir Oltean 	if (flags & BRIDGE_VLAN_INFO_PVID)
22376dfd23d3SVladimir Oltean 		priv->tag_8021q_pvid[port] = vid;
22386dfd23d3SVladimir Oltean 
22396dfd23d3SVladimir Oltean 	return sja1105_commit_pvid(ds, port);
22405899ee36SVladimir Oltean }
22415899ee36SVladimir Oltean 
22425899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
22435899ee36SVladimir Oltean {
22445899ee36SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
22455899ee36SVladimir Oltean 
22466dfd23d3SVladimir Oltean 	return sja1105_vlan_del(priv, port, vid);
22475899ee36SVladimir Oltean }
22485899ee36SVladimir Oltean 
22494fbc08bdSVladimir Oltean static int sja1105_prechangeupper(struct dsa_switch *ds, int port,
22504fbc08bdSVladimir Oltean 				  struct netdev_notifier_changeupper_info *info)
22514fbc08bdSVladimir Oltean {
22524fbc08bdSVladimir Oltean 	struct netlink_ext_ack *extack = info->info.extack;
22534fbc08bdSVladimir Oltean 	struct net_device *upper = info->upper_dev;
225419fa937aSVladimir Oltean 	struct dsa_switch_tree *dst = ds->dst;
225519fa937aSVladimir Oltean 	struct dsa_port *dp;
22564fbc08bdSVladimir Oltean 
22574fbc08bdSVladimir Oltean 	if (is_vlan_dev(upper)) {
22584fbc08bdSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack, "8021q uppers are not supported");
22594fbc08bdSVladimir Oltean 		return -EBUSY;
22604fbc08bdSVladimir Oltean 	}
22614fbc08bdSVladimir Oltean 
226219fa937aSVladimir Oltean 	if (netif_is_bridge_master(upper)) {
226319fa937aSVladimir Oltean 		list_for_each_entry(dp, &dst->ports, list) {
226419fa937aSVladimir Oltean 			if (dp->bridge_dev && dp->bridge_dev != upper &&
226519fa937aSVladimir Oltean 			    br_vlan_enabled(dp->bridge_dev)) {
226619fa937aSVladimir Oltean 				NL_SET_ERR_MSG_MOD(extack,
226719fa937aSVladimir Oltean 						   "Only one VLAN-aware bridge is supported");
226819fa937aSVladimir Oltean 				return -EBUSY;
226919fa937aSVladimir Oltean 			}
227019fa937aSVladimir Oltean 		}
227119fa937aSVladimir Oltean 	}
227219fa937aSVladimir Oltean 
22734fbc08bdSVladimir Oltean 	return 0;
22744fbc08bdSVladimir Oltean }
22754fbc08bdSVladimir Oltean 
22768aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
22778aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
22788aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
22798aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
22808aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
22818aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
22828aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
22838aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
22848aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
22858aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
22868aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
22878aa9ebccSVladimir Oltean  */
22888aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
22898aa9ebccSVladimir Oltean {
22908aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
22918aa9ebccSVladimir Oltean 	int rc;
22928aa9ebccSVladimir Oltean 
22935d645df9SVladimir Oltean 	rc = sja1105_parse_dt(priv);
22948aa9ebccSVladimir Oltean 	if (rc < 0) {
22958aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
22968aa9ebccSVladimir Oltean 		return rc;
22978aa9ebccSVladimir Oltean 	}
2298f5b8631cSVladimir Oltean 
2299f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
2300f5b8631cSVladimir Oltean 	 * and we can't apply them.
2301f5b8631cSVladimir Oltean 	 */
230229afb83aSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv);
2303f5b8631cSVladimir Oltean 	if (rc < 0) {
2304f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
2305f5b8631cSVladimir Oltean 		return rc;
2306f5b8631cSVladimir Oltean 	}
2307f5b8631cSVladimir Oltean 
230861c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
2309bb77f36aSVladimir Oltean 	if (rc < 0) {
2310bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
2311bb77f36aSVladimir Oltean 		return rc;
2312bb77f36aSVladimir Oltean 	}
23135a8f0974SVladimir Oltean 
23145a8f0974SVladimir Oltean 	rc = sja1105_mdiobus_register(ds);
23155a8f0974SVladimir Oltean 	if (rc < 0) {
23165a8f0974SVladimir Oltean 		dev_err(ds->dev, "Failed to register MDIO bus: %pe\n",
23175a8f0974SVladimir Oltean 			ERR_PTR(rc));
23185a8f0974SVladimir Oltean 		goto out_ptp_clock_unregister;
23195a8f0974SVladimir Oltean 	}
23205a8f0974SVladimir Oltean 
2321cb5a82d2SVladimir Oltean 	if (priv->info->disable_microcontroller) {
2322cb5a82d2SVladimir Oltean 		rc = priv->info->disable_microcontroller(priv);
2323cb5a82d2SVladimir Oltean 		if (rc < 0) {
2324cb5a82d2SVladimir Oltean 			dev_err(ds->dev,
2325cb5a82d2SVladimir Oltean 				"Failed to disable microcontroller: %pe\n",
2326cb5a82d2SVladimir Oltean 				ERR_PTR(rc));
2327cb5a82d2SVladimir Oltean 			goto out_mdiobus_unregister;
2328cb5a82d2SVladimir Oltean 		}
2329cb5a82d2SVladimir Oltean 	}
2330cb5a82d2SVladimir Oltean 
23318aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
23325d645df9SVladimir Oltean 	rc = sja1105_static_config_load(priv);
23338aa9ebccSVladimir Oltean 	if (rc < 0) {
23348aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
23355a8f0974SVladimir Oltean 		goto out_mdiobus_unregister;
23368aa9ebccSVladimir Oltean 	}
2337cb5a82d2SVladimir Oltean 
23388aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
2339cb5a82d2SVladimir Oltean 	if (priv->info->clocking_setup) {
2340c5037678SVladimir Oltean 		rc = priv->info->clocking_setup(priv);
23418aa9ebccSVladimir Oltean 		if (rc < 0) {
2342cb5a82d2SVladimir Oltean 			dev_err(ds->dev,
2343cb5a82d2SVladimir Oltean 				"Failed to configure MII clocking: %pe\n",
2344cb5a82d2SVladimir Oltean 				ERR_PTR(rc));
2345cec279a8SVladimir Oltean 			goto out_static_config_free;
23468aa9ebccSVladimir Oltean 		}
2347cb5a82d2SVladimir Oltean 	}
2348cb5a82d2SVladimir Oltean 
23496666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
23506666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
23516666cebcSVladimir Oltean 	 * EtherType is.
23526666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
23536666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
23546666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
23556666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
23566666cebcSVladimir Oltean 	 */
23576666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
2358884be12fSVladimir Oltean 	ds->untag_bridge_pvid = true;
2359b6ad86e6SVladimir Oltean 	/* tag_8021q has 3 bits for the VBID, and the value 0 is reserved */
2360b6ad86e6SVladimir Oltean 	ds->num_fwd_offloading_bridges = 7;
23618aa9ebccSVladimir Oltean 
23625f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
23635f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
23645f06c63bSVladimir Oltean 
2365c279c726SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
2366c279c726SVladimir Oltean 
23670a7bdbc2SVladimir Oltean 	rc = sja1105_devlink_setup(ds);
23682cafa72eSVladimir Oltean 	if (rc < 0)
2369cec279a8SVladimir Oltean 		goto out_static_config_free;
23702cafa72eSVladimir Oltean 
2371bbed0bbdSVladimir Oltean 	rtnl_lock();
2372328621f6SVladimir Oltean 	rc = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q));
2373bbed0bbdSVladimir Oltean 	rtnl_unlock();
2374cec279a8SVladimir Oltean 	if (rc)
2375cec279a8SVladimir Oltean 		goto out_devlink_teardown;
2376cec279a8SVladimir Oltean 
2377cec279a8SVladimir Oltean 	return 0;
2378cec279a8SVladimir Oltean 
2379cec279a8SVladimir Oltean out_devlink_teardown:
2380cec279a8SVladimir Oltean 	sja1105_devlink_teardown(ds);
23815a8f0974SVladimir Oltean out_mdiobus_unregister:
23825a8f0974SVladimir Oltean 	sja1105_mdiobus_unregister(ds);
2383cec279a8SVladimir Oltean out_ptp_clock_unregister:
2384cec279a8SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
2385cec279a8SVladimir Oltean out_static_config_free:
2386cec279a8SVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2387bbed0bbdSVladimir Oltean 
2388bbed0bbdSVladimir Oltean 	return rc;
2389227d07a0SVladimir Oltean }
2390227d07a0SVladimir Oltean 
2391f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
2392f3097be2SVladimir Oltean {
2393f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2394a68578c2SVladimir Oltean 	int port;
2395a68578c2SVladimir Oltean 
2396328621f6SVladimir Oltean 	rtnl_lock();
2397328621f6SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
2398328621f6SVladimir Oltean 	rtnl_unlock();
2399328621f6SVladimir Oltean 
2400542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
2401a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2402a68578c2SVladimir Oltean 
2403a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2404a68578c2SVladimir Oltean 			continue;
2405a68578c2SVladimir Oltean 
240652c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
2407a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
2408a68578c2SVladimir Oltean 	}
2409f3097be2SVladimir Oltean 
24100a7bdbc2SVladimir Oltean 	sja1105_devlink_teardown(ds);
2411a6af7763SVladimir Oltean 	sja1105_flower_teardown(ds);
2412317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
241361c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
24146cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2415f3097be2SVladimir Oltean }
2416f3097be2SVladimir Oltean 
2417a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
2418a68578c2SVladimir Oltean {
2419a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2420a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
2421a68578c2SVladimir Oltean 
2422a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2423a68578c2SVladimir Oltean 		return;
2424a68578c2SVladimir Oltean 
2425a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
2426a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
2427a68578c2SVladimir Oltean }
2428a68578c2SVladimir Oltean 
2429227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
243047ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2431227d07a0SVladimir Oltean {
2432227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2433227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2434227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2435227d07a0SVladimir Oltean 	int timeout = 10;
2436227d07a0SVladimir Oltean 	int rc;
2437227d07a0SVladimir Oltean 
2438227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2439227d07a0SVladimir Oltean 
2440227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2441227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2442227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
244347ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
244447ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2445227d07a0SVladimir Oltean 
2446227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2447227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2448227d07a0SVladimir Oltean 	if (rc < 0) {
2449227d07a0SVladimir Oltean 		kfree_skb(skb);
2450227d07a0SVladimir Oltean 		return rc;
2451227d07a0SVladimir Oltean 	}
2452227d07a0SVladimir Oltean 
2453227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
245468bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2455227d07a0SVladimir Oltean 
2456227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2457227d07a0SVladimir Oltean 	do {
2458227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2459227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2460227d07a0SVladimir Oltean 		if (rc < 0) {
2461227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2462227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2463227d07a0SVladimir Oltean 			continue;
2464227d07a0SVladimir Oltean 		}
2465227d07a0SVladimir Oltean 
2466227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2467227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2468227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2469227d07a0SVladimir Oltean 		 */
2470227d07a0SVladimir Oltean 		cpu_relax();
2471227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2472227d07a0SVladimir Oltean 
2473227d07a0SVladimir Oltean 	if (!timeout) {
2474227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2475227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
24762a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
24772a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2478227d07a0SVladimir Oltean 		 */
2479227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2480227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2481227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2482227d07a0SVladimir Oltean 	}
2483227d07a0SVladimir Oltean 
2484227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2485227d07a0SVladimir Oltean }
2486227d07a0SVladimir Oltean 
2487a68578c2SVladimir Oltean #define work_to_port(work) \
2488a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
2489a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
2490a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
2491a68578c2SVladimir Oltean 
2492227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2493227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2494227d07a0SVladimir Oltean  * lock on the bus)
2495227d07a0SVladimir Oltean  */
2496a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2497227d07a0SVladimir Oltean {
2498a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
2499a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
2500a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
2501a68578c2SVladimir Oltean 	int port = sp - priv->ports;
2502a68578c2SVladimir Oltean 	struct sk_buff *skb;
2503a68578c2SVladimir Oltean 
2504a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
2505c4b364ceSYangbo Lu 		struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
2506227d07a0SVladimir Oltean 
2507227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
2508227d07a0SVladimir Oltean 
2509a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
2510a68578c2SVladimir Oltean 
251147ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
2512a68578c2SVladimir Oltean 		if (clone)
2513a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
2514227d07a0SVladimir Oltean 
2515227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
2516a68578c2SVladimir Oltean 	}
25178aa9ebccSVladimir Oltean }
25188aa9ebccSVladimir Oltean 
25198456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
25208456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
25218456721dSVladimir Oltean  */
25228456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
25238456721dSVladimir Oltean 				   unsigned int ageing_time)
25248456721dSVladimir Oltean {
25258456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
25268456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
25278456721dSVladimir Oltean 	struct sja1105_table *table;
25288456721dSVladimir Oltean 	unsigned int maxage;
25298456721dSVladimir Oltean 
25308456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
25318456721dSVladimir Oltean 	l2_lookup_params = table->entries;
25328456721dSVladimir Oltean 
25338456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
25348456721dSVladimir Oltean 
25358456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
25368456721dSVladimir Oltean 		return 0;
25378456721dSVladimir Oltean 
25388456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
25398456721dSVladimir Oltean 
25402eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
25418456721dSVladimir Oltean }
25428456721dSVladimir Oltean 
2543c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
2544c279c726SVladimir Oltean {
2545c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2546c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2547c279c726SVladimir Oltean 
2548c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
2549c279c726SVladimir Oltean 
2550c279c726SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
2551c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
2552c279c726SVladimir Oltean 
2553c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2554c279c726SVladimir Oltean 
2555a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
2556c279c726SVladimir Oltean 		return 0;
2557c279c726SVladimir Oltean 
2558a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
2559c279c726SVladimir Oltean 
2560c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2561c279c726SVladimir Oltean }
2562c279c726SVladimir Oltean 
2563c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
2564c279c726SVladimir Oltean {
2565c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
2566c279c726SVladimir Oltean }
2567c279c726SVladimir Oltean 
2568317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2569317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2570317ab5b8SVladimir Oltean 				 void *type_data)
2571317ab5b8SVladimir Oltean {
2572317ab5b8SVladimir Oltean 	switch (type) {
2573317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2574317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
25754d752508SVladimir Oltean 	case TC_SETUP_QDISC_CBS:
25764d752508SVladimir Oltean 		return sja1105_setup_tc_cbs(ds, port, type_data);
2577317ab5b8SVladimir Oltean 	default:
2578317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2579317ab5b8SVladimir Oltean 	}
2580317ab5b8SVladimir Oltean }
2581317ab5b8SVladimir Oltean 
2582511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2583511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2584511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2585511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2586511e6ca0SVladimir Oltean  * mirroring rule that references it.
2587511e6ca0SVladimir Oltean  */
2588511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2589511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2590511e6ca0SVladimir Oltean {
2591511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2592511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2593542043e9SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
2594511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2595511e6ca0SVladimir Oltean 	bool already_enabled;
2596511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2597511e6ca0SVladimir Oltean 	int rc;
2598511e6ca0SVladimir Oltean 
2599511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2600511e6ca0SVladimir Oltean 	general_params = table->entries;
2601511e6ca0SVladimir Oltean 
2602511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2603511e6ca0SVladimir Oltean 
2604542043e9SVladimir Oltean 	already_enabled = (general_params->mirr_port != ds->num_ports);
2605511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2606511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2607511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2608511e6ca0SVladimir Oltean 			general_params->mirr_port);
2609511e6ca0SVladimir Oltean 		return -EBUSY;
2610511e6ca0SVladimir Oltean 	}
2611511e6ca0SVladimir Oltean 
2612511e6ca0SVladimir Oltean 	new_mirr_port = to;
2613511e6ca0SVladimir Oltean 	if (!enabled) {
2614511e6ca0SVladimir Oltean 		bool keep = false;
2615511e6ca0SVladimir Oltean 		int port;
2616511e6ca0SVladimir Oltean 
2617511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2618542043e9SVladimir Oltean 		for (port = 0; port < ds->num_ports; port++) {
2619511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2620511e6ca0SVladimir Oltean 				keep = true;
2621511e6ca0SVladimir Oltean 				break;
2622511e6ca0SVladimir Oltean 			}
2623511e6ca0SVladimir Oltean 		}
2624511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2625511e6ca0SVladimir Oltean 		if (!keep)
2626542043e9SVladimir Oltean 			new_mirr_port = ds->num_ports;
2627511e6ca0SVladimir Oltean 	}
2628511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2629511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2630511e6ca0SVladimir Oltean 
2631511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2632511e6ca0SVladimir Oltean 						  0, general_params, true);
2633511e6ca0SVladimir Oltean 		if (rc < 0)
2634511e6ca0SVladimir Oltean 			return rc;
2635511e6ca0SVladimir Oltean 	}
2636511e6ca0SVladimir Oltean 
2637511e6ca0SVladimir Oltean 	if (ingress)
2638511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2639511e6ca0SVladimir Oltean 	else
2640511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2641511e6ca0SVladimir Oltean 
2642511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2643511e6ca0SVladimir Oltean 					    &mac[from], true);
2644511e6ca0SVladimir Oltean }
2645511e6ca0SVladimir Oltean 
2646511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2647511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2648511e6ca0SVladimir Oltean 			      bool ingress)
2649511e6ca0SVladimir Oltean {
2650511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2651511e6ca0SVladimir Oltean 				    ingress, true);
2652511e6ca0SVladimir Oltean }
2653511e6ca0SVladimir Oltean 
2654511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2655511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2656511e6ca0SVladimir Oltean {
2657511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2658511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2659511e6ca0SVladimir Oltean }
2660511e6ca0SVladimir Oltean 
2661a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
2662a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
2663a7cc081cSVladimir Oltean {
2664a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2665a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2666a7cc081cSVladimir Oltean 
2667a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2668a7cc081cSVladimir Oltean 
2669a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
2670a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
2671a7cc081cSVladimir Oltean 	 * bytes.
2672a7cc081cSVladimir Oltean 	 */
2673a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
2674a7cc081cSVladimir Oltean 				      1000000);
26755f035af7SPo Liu 	policing[port].smax = policer->burst;
2676a7cc081cSVladimir Oltean 
2677a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2678a7cc081cSVladimir Oltean }
2679a7cc081cSVladimir Oltean 
2680a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
2681a7cc081cSVladimir Oltean {
2682a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2683a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2684a7cc081cSVladimir Oltean 
2685a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2686a7cc081cSVladimir Oltean 
2687a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
2688a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
2689a7cc081cSVladimir Oltean 
2690a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2691a7cc081cSVladimir Oltean }
2692a7cc081cSVladimir Oltean 
26934d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port,
26944d942354SVladimir Oltean 				     bool enabled)
26954d942354SVladimir Oltean {
26964d942354SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
26974d942354SVladimir Oltean 	int rc;
26984d942354SVladimir Oltean 
26994d942354SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
27004d942354SVladimir Oltean 
27014c44fc5eSVladimir Oltean 	mac[port].dyn_learn = enabled;
27024d942354SVladimir Oltean 
27034d942354SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
27044d942354SVladimir Oltean 					  &mac[port], true);
27054d942354SVladimir Oltean 	if (rc)
27064d942354SVladimir Oltean 		return rc;
27074d942354SVladimir Oltean 
27084d942354SVladimir Oltean 	if (enabled)
27094d942354SVladimir Oltean 		priv->learn_ena |= BIT(port);
27104d942354SVladimir Oltean 	else
27114d942354SVladimir Oltean 		priv->learn_ena &= ~BIT(port);
27124d942354SVladimir Oltean 
27134d942354SVladimir Oltean 	return 0;
27144d942354SVladimir Oltean }
27154d942354SVladimir Oltean 
27164d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to,
27174d942354SVladimir Oltean 					  struct switchdev_brport_flags flags)
27184d942354SVladimir Oltean {
27194d942354SVladimir Oltean 	if (flags.mask & BR_FLOOD) {
27204d942354SVladimir Oltean 		if (flags.val & BR_FLOOD)
27217f7ccdeaSVladimir Oltean 			priv->ucast_egress_floods |= BIT(to);
27224d942354SVladimir Oltean 		else
27236a5166e0SVladimir Oltean 			priv->ucast_egress_floods &= ~BIT(to);
27244d942354SVladimir Oltean 	}
27257f7ccdeaSVladimir Oltean 
27264d942354SVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD) {
27274d942354SVladimir Oltean 		if (flags.val & BR_BCAST_FLOOD)
27287f7ccdeaSVladimir Oltean 			priv->bcast_egress_floods |= BIT(to);
27294d942354SVladimir Oltean 		else
27306a5166e0SVladimir Oltean 			priv->bcast_egress_floods &= ~BIT(to);
27314d942354SVladimir Oltean 	}
27324d942354SVladimir Oltean 
27337f7ccdeaSVladimir Oltean 	return sja1105_manage_flood_domains(priv);
27344d942354SVladimir Oltean }
27354d942354SVladimir Oltean 
27364d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to,
27374d942354SVladimir Oltean 				    struct switchdev_brport_flags flags,
27384d942354SVladimir Oltean 				    struct netlink_ext_ack *extack)
27394d942354SVladimir Oltean {
27404d942354SVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
27414d942354SVladimir Oltean 	struct sja1105_table *table;
27424d942354SVladimir Oltean 	int match;
27434d942354SVladimir Oltean 
27444d942354SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
27454d942354SVladimir Oltean 	l2_lookup = table->entries;
27464d942354SVladimir Oltean 
27474d942354SVladimir Oltean 	for (match = 0; match < table->entry_count; match++)
27484d942354SVladimir Oltean 		if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST &&
27494d942354SVladimir Oltean 		    l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
27504d942354SVladimir Oltean 			break;
27514d942354SVladimir Oltean 
27524d942354SVladimir Oltean 	if (match == table->entry_count) {
27534d942354SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
27544d942354SVladimir Oltean 				   "Could not find FDB entry for unknown multicast");
27554d942354SVladimir Oltean 		return -ENOSPC;
27564d942354SVladimir Oltean 	}
27574d942354SVladimir Oltean 
27584d942354SVladimir Oltean 	if (flags.val & BR_MCAST_FLOOD)
27594d942354SVladimir Oltean 		l2_lookup[match].destports |= BIT(to);
27604d942354SVladimir Oltean 	else
27614d942354SVladimir Oltean 		l2_lookup[match].destports &= ~BIT(to);
27624d942354SVladimir Oltean 
27634d942354SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
27644d942354SVladimir Oltean 					    l2_lookup[match].index,
27654d942354SVladimir Oltean 					    &l2_lookup[match],
27664d942354SVladimir Oltean 					    true);
27674d942354SVladimir Oltean }
27684d942354SVladimir Oltean 
27694d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port,
27704d942354SVladimir Oltean 					 struct switchdev_brport_flags flags,
27714d942354SVladimir Oltean 					 struct netlink_ext_ack *extack)
27724d942354SVladimir Oltean {
27734d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
27744d942354SVladimir Oltean 
27754d942354SVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
27764d942354SVladimir Oltean 			   BR_BCAST_FLOOD))
27774d942354SVladimir Oltean 		return -EINVAL;
27784d942354SVladimir Oltean 
27794d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) &&
27804d942354SVladimir Oltean 	    !priv->info->can_limit_mcast_flood) {
27814d942354SVladimir Oltean 		bool multicast = !!(flags.val & BR_MCAST_FLOOD);
27824d942354SVladimir Oltean 		bool unicast = !!(flags.val & BR_FLOOD);
27834d942354SVladimir Oltean 
27844d942354SVladimir Oltean 		if (unicast != multicast) {
27854d942354SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
27864d942354SVladimir Oltean 					   "This chip cannot configure multicast flooding independently of unicast");
27874d942354SVladimir Oltean 			return -EINVAL;
27884d942354SVladimir Oltean 		}
27894d942354SVladimir Oltean 	}
27904d942354SVladimir Oltean 
27914d942354SVladimir Oltean 	return 0;
27924d942354SVladimir Oltean }
27934d942354SVladimir Oltean 
27944d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port,
27954d942354SVladimir Oltean 				     struct switchdev_brport_flags flags,
27964d942354SVladimir Oltean 				     struct netlink_ext_ack *extack)
27974d942354SVladimir Oltean {
27984d942354SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
27994d942354SVladimir Oltean 	int rc;
28004d942354SVladimir Oltean 
28014d942354SVladimir Oltean 	if (flags.mask & BR_LEARNING) {
28024d942354SVladimir Oltean 		bool learn_ena = !!(flags.val & BR_LEARNING);
28034d942354SVladimir Oltean 
28044d942354SVladimir Oltean 		rc = sja1105_port_set_learning(priv, port, learn_ena);
28054d942354SVladimir Oltean 		if (rc)
28064d942354SVladimir Oltean 			return rc;
28074d942354SVladimir Oltean 	}
28084d942354SVladimir Oltean 
28094d942354SVladimir Oltean 	if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) {
28104d942354SVladimir Oltean 		rc = sja1105_port_ucast_bcast_flood(priv, port, flags);
28114d942354SVladimir Oltean 		if (rc)
28124d942354SVladimir Oltean 			return rc;
28134d942354SVladimir Oltean 	}
28144d942354SVladimir Oltean 
28154d942354SVladimir Oltean 	/* For chips that can't offload BR_MCAST_FLOOD independently, there
28164d942354SVladimir Oltean 	 * is nothing to do here, we ensured the configuration is in sync by
28174d942354SVladimir Oltean 	 * offloading BR_FLOOD.
28184d942354SVladimir Oltean 	 */
28194d942354SVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) {
28204d942354SVladimir Oltean 		rc = sja1105_port_mcast_flood(priv, port, flags,
28214d942354SVladimir Oltean 					      extack);
28224d942354SVladimir Oltean 		if (rc)
28234d942354SVladimir Oltean 			return rc;
28244d942354SVladimir Oltean 	}
28254d942354SVladimir Oltean 
28264d942354SVladimir Oltean 	return 0;
28274d942354SVladimir Oltean }
28284d942354SVladimir Oltean 
28298aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
28308aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
28318aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
2832f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
28338456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
2834c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
2835c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
2836ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
2837af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
28388400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
28398400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
284052c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
284152c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
284252c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
2843bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
2844a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
2845291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
2846291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
2847291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
28488aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
28498aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
28504d942354SVladimir Oltean 	.port_pre_bridge_flags	= sja1105_port_pre_bridge_flags,
28514d942354SVladimir Oltean 	.port_bridge_flags	= sja1105_port_bridge_flags,
2852640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
28536666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
28546dfd23d3SVladimir Oltean 	.port_vlan_add		= sja1105_bridge_vlan_add,
28556dfd23d3SVladimir Oltean 	.port_vlan_del		= sja1105_bridge_vlan_del,
2856291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
2857291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
2858a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
2859a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
2860f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
286147ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
2862317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
2863511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
2864511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
2865a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
2866a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
2867a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
2868a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
2869834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
2870ff4cf8eaSVladimir Oltean 	.devlink_info_get	= sja1105_devlink_info_get,
28715da11eb4SVladimir Oltean 	.tag_8021q_vlan_add	= sja1105_dsa_8021q_vlan_add,
28725da11eb4SVladimir Oltean 	.tag_8021q_vlan_del	= sja1105_dsa_8021q_vlan_del,
28734fbc08bdSVladimir Oltean 	.port_prechangeupper	= sja1105_prechangeupper,
2874b6ad86e6SVladimir Oltean 	.port_bridge_tx_fwd_offload = dsa_tag_8021q_bridge_tx_fwd_offload,
2875b6ad86e6SVladimir Oltean 	.port_bridge_tx_fwd_unoffload = dsa_tag_8021q_bridge_tx_fwd_unoffload,
28768aa9ebccSVladimir Oltean };
28778aa9ebccSVladimir Oltean 
28780b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[];
28790b0e2997SVladimir Oltean 
28808aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
28818aa9ebccSVladimir Oltean {
28828aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
28838aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
28848aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
28850b0e2997SVladimir Oltean 	const struct of_device_id *match;
2886dff79620SVladimir Oltean 	u32 device_id;
28878aa9ebccSVladimir Oltean 	u64 part_no;
28888aa9ebccSVladimir Oltean 	int rc;
28898aa9ebccSVladimir Oltean 
289034d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
289134d76e9fSVladimir Oltean 			      NULL);
28928aa9ebccSVladimir Oltean 	if (rc < 0)
28938aa9ebccSVladimir Oltean 		return rc;
28948aa9ebccSVladimir Oltean 
28951bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
28961bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
28978aa9ebccSVladimir Oltean 	if (rc < 0)
28988aa9ebccSVladimir Oltean 		return rc;
28998aa9ebccSVladimir Oltean 
29008aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
29018aa9ebccSVladimir Oltean 
29025978fac0SNathan Chancellor 	for (match = sja1105_dt_ids; match->compatible[0]; match++) {
29030b0e2997SVladimir Oltean 		const struct sja1105_info *info = match->data;
29040b0e2997SVladimir Oltean 
29050b0e2997SVladimir Oltean 		/* Is what's been probed in our match table at all? */
29060b0e2997SVladimir Oltean 		if (info->device_id != device_id || info->part_no != part_no)
29070b0e2997SVladimir Oltean 			continue;
29080b0e2997SVladimir Oltean 
29090b0e2997SVladimir Oltean 		/* But is it what's in the device tree? */
29100b0e2997SVladimir Oltean 		if (priv->info->device_id != device_id ||
29110b0e2997SVladimir Oltean 		    priv->info->part_no != part_no) {
29120b0e2997SVladimir Oltean 			dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
29130b0e2997SVladimir Oltean 				 priv->info->name, info->name);
29140b0e2997SVladimir Oltean 			/* It isn't. No problem, pick that up. */
29150b0e2997SVladimir Oltean 			priv->info = info;
29168aa9ebccSVladimir Oltean 		}
29178aa9ebccSVladimir Oltean 
29188aa9ebccSVladimir Oltean 		return 0;
29198aa9ebccSVladimir Oltean 	}
29208aa9ebccSVladimir Oltean 
29210b0e2997SVladimir Oltean 	dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
29220b0e2997SVladimir Oltean 		device_id, part_no);
29230b0e2997SVladimir Oltean 
29240b0e2997SVladimir Oltean 	return -ENODEV;
29250b0e2997SVladimir Oltean }
29260b0e2997SVladimir Oltean 
29278aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
29288aa9ebccSVladimir Oltean {
2929844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
29308aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
29318aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
2932718bad0eSVladimir Oltean 	size_t max_xfer, max_msg;
29338aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
2934a68578c2SVladimir Oltean 	int rc, port;
29358aa9ebccSVladimir Oltean 
29368aa9ebccSVladimir Oltean 	if (!dev->of_node) {
29378aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
29388aa9ebccSVladimir Oltean 		return -EINVAL;
29398aa9ebccSVladimir Oltean 	}
29408aa9ebccSVladimir Oltean 
29418aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
29428aa9ebccSVladimir Oltean 	if (!priv)
29438aa9ebccSVladimir Oltean 		return -ENOMEM;
29448aa9ebccSVladimir Oltean 
29458aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
29468aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
29478aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
29488aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
29498aa9ebccSVladimir Oltean 	else
29508aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
29518aa9ebccSVladimir Oltean 
29528aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
29538aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
29548aa9ebccSVladimir Oltean 	 */
29558aa9ebccSVladimir Oltean 	priv->spidev = spi;
29568aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
29578aa9ebccSVladimir Oltean 
29588aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
29598aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
29608aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
29618aa9ebccSVladimir Oltean 	if (rc < 0) {
29628aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
29638aa9ebccSVladimir Oltean 		return rc;
29648aa9ebccSVladimir Oltean 	}
29658aa9ebccSVladimir Oltean 
2966718bad0eSVladimir Oltean 	/* In sja1105_xfer, we send spi_messages composed of two spi_transfers:
2967718bad0eSVladimir Oltean 	 * a small one for the message header and another one for the current
2968718bad0eSVladimir Oltean 	 * chunk of the packed buffer.
2969718bad0eSVladimir Oltean 	 * Check that the restrictions imposed by the SPI controller are
2970718bad0eSVladimir Oltean 	 * respected: the chunk buffer is smaller than the max transfer size,
2971718bad0eSVladimir Oltean 	 * and the total length of the chunk plus its message header is smaller
2972718bad0eSVladimir Oltean 	 * than the max message size.
2973718bad0eSVladimir Oltean 	 * We do that during probe time since the maximum transfer size is a
2974718bad0eSVladimir Oltean 	 * runtime invariant.
2975718bad0eSVladimir Oltean 	 */
2976718bad0eSVladimir Oltean 	max_xfer = spi_max_transfer_size(spi);
2977718bad0eSVladimir Oltean 	max_msg = spi_max_message_size(spi);
2978718bad0eSVladimir Oltean 
2979718bad0eSVladimir Oltean 	/* We need to send at least one 64-bit word of SPI payload per message
2980718bad0eSVladimir Oltean 	 * in order to be able to make useful progress.
2981718bad0eSVladimir Oltean 	 */
2982718bad0eSVladimir Oltean 	if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) {
2983718bad0eSVladimir Oltean 		dev_err(dev, "SPI master cannot send large enough buffers, aborting\n");
2984718bad0eSVladimir Oltean 		return -EINVAL;
2985718bad0eSVladimir Oltean 	}
2986718bad0eSVladimir Oltean 
2987718bad0eSVladimir Oltean 	priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN;
2988718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_xfer)
2989718bad0eSVladimir Oltean 		priv->max_xfer_len = max_xfer;
2990718bad0eSVladimir Oltean 	if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER)
2991718bad0eSVladimir Oltean 		priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER;
2992718bad0eSVladimir Oltean 
29938aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
29948aa9ebccSVladimir Oltean 
29958aa9ebccSVladimir Oltean 	/* Detect hardware device */
29968aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
29978aa9ebccSVladimir Oltean 	if (rc < 0) {
29988aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
29998aa9ebccSVladimir Oltean 		return rc;
30008aa9ebccSVladimir Oltean 	}
30018aa9ebccSVladimir Oltean 
30028aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
30038aa9ebccSVladimir Oltean 
30047e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
30058aa9ebccSVladimir Oltean 	if (!ds)
30068aa9ebccSVladimir Oltean 		return -ENOMEM;
30078aa9ebccSVladimir Oltean 
30087e99e347SVivien Didelot 	ds->dev = dev;
30093e77e59bSVladimir Oltean 	ds->num_ports = priv->info->num_ports;
30108aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
30118aa9ebccSVladimir Oltean 	ds->priv = priv;
30128aa9ebccSVladimir Oltean 	priv->ds = ds;
30138aa9ebccSVladimir Oltean 
3014844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
3015844d7edcSVladimir Oltean 
3016d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
3017d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
3018d5a619bfSVivien Didelot 
3019d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
3020a6af7763SVladimir Oltean 	sja1105_flower_setup(ds);
3021d5a619bfSVivien Didelot 
3022d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
3023d5a619bfSVivien Didelot 	if (rc)
3024328621f6SVladimir Oltean 		return rc;
3025d5a619bfSVivien Didelot 
30264d752508SVladimir Oltean 	if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
30274d752508SVladimir Oltean 		priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
30284d752508SVladimir Oltean 					 sizeof(struct sja1105_cbs_entry),
30294d752508SVladimir Oltean 					 GFP_KERNEL);
3030dc596e3fSVladimir Oltean 		if (!priv->cbs) {
3031dc596e3fSVladimir Oltean 			rc = -ENOMEM;
3032dc596e3fSVladimir Oltean 			goto out_unregister_switch;
3033dc596e3fSVladimir Oltean 		}
30344d752508SVladimir Oltean 	}
30354d752508SVladimir Oltean 
3036227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
3037542043e9SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
3038a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3039a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
3040a68578c2SVladimir Oltean 		struct net_device *slave;
3041227d07a0SVladimir Oltean 
3042a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3043a68578c2SVladimir Oltean 			continue;
3044a68578c2SVladimir Oltean 
3045a68578c2SVladimir Oltean 		dp->priv = sp;
3046a68578c2SVladimir Oltean 		sp->dp = dp;
3047844d7edcSVladimir Oltean 		sp->data = tagger_data;
3048a68578c2SVladimir Oltean 		slave = dp->slave;
3049a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
3050a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
3051a68578c2SVladimir Oltean 							slave->name);
3052a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
3053a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
3054a68578c2SVladimir Oltean 			dev_err(ds->dev,
3055a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
3056a68578c2SVladimir Oltean 				rc);
3057dc596e3fSVladimir Oltean 			goto out_destroy_workers;
3058a68578c2SVladimir Oltean 		}
3059a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
306038b5beeaSVladimir Oltean 		sp->xmit_tpid = ETH_P_SJA1105;
3061227d07a0SVladimir Oltean 	}
3062227d07a0SVladimir Oltean 
3063d5a619bfSVivien Didelot 	return 0;
3064dc596e3fSVladimir Oltean 
3065dc596e3fSVladimir Oltean out_destroy_workers:
3066a68578c2SVladimir Oltean 	while (port-- > 0) {
3067a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
3068a68578c2SVladimir Oltean 
3069a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
3070a68578c2SVladimir Oltean 			continue;
3071a68578c2SVladimir Oltean 
3072a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
3073a68578c2SVladimir Oltean 	}
3074dc596e3fSVladimir Oltean 
3075dc596e3fSVladimir Oltean out_unregister_switch:
3076dc596e3fSVladimir Oltean 	dsa_unregister_switch(ds);
3077dc596e3fSVladimir Oltean 
3078a68578c2SVladimir Oltean 	return rc;
30798aa9ebccSVladimir Oltean }
30808aa9ebccSVladimir Oltean 
30818aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
30828aa9ebccSVladimir Oltean {
30838aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
3084cedf4670SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
30858aa9ebccSVladimir Oltean 
3086cedf4670SVladimir Oltean 	dsa_unregister_switch(ds);
3087cedf4670SVladimir Oltean 
30888aa9ebccSVladimir Oltean 	return 0;
30898aa9ebccSVladimir Oltean }
30908aa9ebccSVladimir Oltean 
30918aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
30928aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
30938aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
30948aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
30958aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
30968aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
30978aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
30983e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110a", .data = &sja1110a_info },
30993e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110b", .data = &sja1110b_info },
31003e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110c", .data = &sja1110c_info },
31013e77e59bSVladimir Oltean 	{ .compatible = "nxp,sja1110d", .data = &sja1110d_info },
31028aa9ebccSVladimir Oltean 	{ /* sentinel */ },
31038aa9ebccSVladimir Oltean };
31048aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
31058aa9ebccSVladimir Oltean 
31068aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
31078aa9ebccSVladimir Oltean 	.driver = {
31088aa9ebccSVladimir Oltean 		.name  = "sja1105",
31098aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
31108aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
31118aa9ebccSVladimir Oltean 	},
31128aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
31138aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
31148aa9ebccSVladimir Oltean };
31158aa9ebccSVladimir Oltean 
31168aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
31178aa9ebccSVladimir Oltean 
31188aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
31198aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
31208aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
31218aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
3122