xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 60b33aeb7e0e664865ace822c0a7aeeb5ebe521c)
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>
198aa9ebccSVladimir Oltean #include <linux/netdev_features.h>
208aa9ebccSVladimir Oltean #include <linux/netdevice.h>
218aa9ebccSVladimir Oltean #include <linux/if_bridge.h>
228aa9ebccSVladimir Oltean #include <linux/if_ether.h>
23227d07a0SVladimir Oltean #include <linux/dsa/8021q.h>
248aa9ebccSVladimir Oltean #include "sja1105.h"
25ffe10e67SVladimir Oltean #include "sja1105_sgmii.h"
26317ab5b8SVladimir Oltean #include "sja1105_tas.h"
278aa9ebccSVladimir Oltean 
28ac02a451SVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops;
29ac02a451SVladimir Oltean 
308aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
318aa9ebccSVladimir Oltean 			     unsigned int startup_delay)
328aa9ebccSVladimir Oltean {
338aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
348aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
358aa9ebccSVladimir Oltean 	msleep(pulse_len);
368aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
378aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
388aa9ebccSVladimir Oltean 	msleep(startup_delay);
398aa9ebccSVladimir Oltean }
408aa9ebccSVladimir Oltean 
418aa9ebccSVladimir Oltean static void
428aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
438aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
448aa9ebccSVladimir Oltean {
458aa9ebccSVladimir Oltean 	if (allow) {
468aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  |= BIT(to);
478aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
488aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  |= BIT(to);
498aa9ebccSVladimir Oltean 	} else {
508aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  &= ~BIT(to);
518aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
528aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  &= ~BIT(to);
538aa9ebccSVladimir Oltean 	}
548aa9ebccSVladimir Oltean }
558aa9ebccSVladimir Oltean 
568aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree
578aa9ebccSVladimir Oltean  * settings into sja1105_setup
588aa9ebccSVladimir Oltean  */
598aa9ebccSVladimir Oltean struct sja1105_dt_port {
608aa9ebccSVladimir Oltean 	phy_interface_t phy_mode;
618aa9ebccSVladimir Oltean 	sja1105_mii_role_t role;
628aa9ebccSVladimir Oltean };
638aa9ebccSVladimir Oltean 
648aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
658aa9ebccSVladimir Oltean {
668aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
678aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
688aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
698aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
708aa9ebccSVladimir Oltean 		 */
718aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
728aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
738aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
748aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
758aa9ebccSVladimir Oltean 		.ifg = 0,
768aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
771fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
788aa9ebccSVladimir Oltean 		 */
798aa9ebccSVladimir Oltean 		.speed = SJA1105_SPEED_AUTO,
808aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
818aa9ebccSVladimir Oltean 		.tp_delin = 0,
828aa9ebccSVladimir Oltean 		.tp_delout = 0,
838aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
848aa9ebccSVladimir Oltean 		.maxage = 0xFF,
858aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
868aa9ebccSVladimir Oltean 		.vlanprio = 0,
87e3502b82SVladimir Oltean 		.vlanid = 1,
888aa9ebccSVladimir Oltean 		.ing_mirr = false,
898aa9ebccSVladimir Oltean 		.egr_mirr = false,
908aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
918aa9ebccSVladimir Oltean 		.drpnona664 = false,
928aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
938aa9ebccSVladimir Oltean 		.drpdtag = false,
948aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
958aa9ebccSVladimir Oltean 		.drpuntag = false,
968aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
978aa9ebccSVladimir Oltean 		.retag = false,
98640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
99640f763fSVladimir Oltean 		 * STP will enable it.
100640f763fSVladimir Oltean 		 */
101640f763fSVladimir Oltean 		.dyn_learn = false,
1028aa9ebccSVladimir Oltean 		.egress = false,
1038aa9ebccSVladimir Oltean 		.ingress = false,
1048aa9ebccSVladimir Oltean 	};
1058aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1068aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1078aa9ebccSVladimir Oltean 	int i;
1088aa9ebccSVladimir Oltean 
1098aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1108aa9ebccSVladimir Oltean 
1118aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1128aa9ebccSVladimir Oltean 	if (table->entry_count) {
1138aa9ebccSVladimir Oltean 		kfree(table->entries);
1148aa9ebccSVladimir Oltean 		table->entry_count = 0;
1158aa9ebccSVladimir Oltean 	}
1168aa9ebccSVladimir Oltean 
1178aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_NUM_PORTS,
1188aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1198aa9ebccSVladimir Oltean 	if (!table->entries)
1208aa9ebccSVladimir Oltean 		return -ENOMEM;
1218aa9ebccSVladimir Oltean 
1228aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_NUM_PORTS;
1238aa9ebccSVladimir Oltean 
1248aa9ebccSVladimir Oltean 	mac = table->entries;
1258aa9ebccSVladimir Oltean 
126640f763fSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1278aa9ebccSVladimir Oltean 		mac[i] = default_mac;
128640f763fSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, i)) {
129640f763fSVladimir Oltean 			/* STP doesn't get called for CPU port, so we need to
130640f763fSVladimir Oltean 			 * set the I/O parameters statically.
131640f763fSVladimir Oltean 			 */
132640f763fSVladimir Oltean 			mac[i].dyn_learn = true;
133640f763fSVladimir Oltean 			mac[i].ingress = true;
134640f763fSVladimir Oltean 			mac[i].egress = true;
135640f763fSVladimir Oltean 		}
136640f763fSVladimir Oltean 	}
1378aa9ebccSVladimir Oltean 
1388aa9ebccSVladimir Oltean 	return 0;
1398aa9ebccSVladimir Oltean }
1408aa9ebccSVladimir Oltean 
141ffe10e67SVladimir Oltean static bool sja1105_supports_sgmii(struct sja1105_private *priv, int port)
142ffe10e67SVladimir Oltean {
143ffe10e67SVladimir Oltean 	if (priv->info->part_no != SJA1105R_PART_NO &&
144ffe10e67SVladimir Oltean 	    priv->info->part_no != SJA1105S_PART_NO)
145ffe10e67SVladimir Oltean 		return false;
146ffe10e67SVladimir Oltean 
147ffe10e67SVladimir Oltean 	if (port != SJA1105_SGMII_PORT)
148ffe10e67SVladimir Oltean 		return false;
149ffe10e67SVladimir Oltean 
150ffe10e67SVladimir Oltean 	if (dsa_is_unused_port(priv->ds, port))
151ffe10e67SVladimir Oltean 		return false;
152ffe10e67SVladimir Oltean 
153ffe10e67SVladimir Oltean 	return true;
154ffe10e67SVladimir Oltean }
155ffe10e67SVladimir Oltean 
1568aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv,
1578aa9ebccSVladimir Oltean 				     struct sja1105_dt_port *ports)
1588aa9ebccSVladimir Oltean {
1598aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1608aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1618aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1628aa9ebccSVladimir Oltean 	int i;
1638aa9ebccSVladimir Oltean 
1648aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1658aa9ebccSVladimir Oltean 
1668aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1678aa9ebccSVladimir Oltean 	if (table->entry_count) {
1688aa9ebccSVladimir Oltean 		kfree(table->entries);
1698aa9ebccSVladimir Oltean 		table->entry_count = 0;
1708aa9ebccSVladimir Oltean 	}
1718aa9ebccSVladimir Oltean 
1728aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
1738aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1748aa9ebccSVladimir Oltean 	if (!table->entries)
1758aa9ebccSVladimir Oltean 		return -ENOMEM;
1768aa9ebccSVladimir Oltean 
1771fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
1788aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
1798aa9ebccSVladimir Oltean 
1808aa9ebccSVladimir Oltean 	mii = table->entries;
1818aa9ebccSVladimir Oltean 
1828aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
183ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
184ee9d0cb6SVladimir Oltean 			continue;
185ee9d0cb6SVladimir Oltean 
1868aa9ebccSVladimir Oltean 		switch (ports[i].phy_mode) {
1878aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
1888aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1898aa9ebccSVladimir Oltean 			break;
1908aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
1918aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
1928aa9ebccSVladimir Oltean 			break;
1938aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
1948aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
1958aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
1968aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
1978aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
1988aa9ebccSVladimir Oltean 			break;
199ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
200ffe10e67SVladimir Oltean 			if (!sja1105_supports_sgmii(priv, i))
201ffe10e67SVladimir Oltean 				return -EINVAL;
202ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
203ffe10e67SVladimir Oltean 			break;
2048aa9ebccSVladimir Oltean 		default:
2058aa9ebccSVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s!\n",
2068aa9ebccSVladimir Oltean 				phy_modes(ports[i].phy_mode));
2078aa9ebccSVladimir Oltean 		}
2088aa9ebccSVladimir Oltean 
209ffe10e67SVladimir Oltean 		/* Even though the SerDes port is able to drive SGMII autoneg
210ffe10e67SVladimir Oltean 		 * like a PHY would, from the perspective of the XMII tables,
211ffe10e67SVladimir Oltean 		 * the SGMII port should always be put in MAC mode.
212ffe10e67SVladimir Oltean 		 */
213ffe10e67SVladimir Oltean 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII)
214ffe10e67SVladimir Oltean 			mii->phy_mac[i] = XMII_MAC;
215ffe10e67SVladimir Oltean 		else
2168aa9ebccSVladimir Oltean 			mii->phy_mac[i] = ports[i].role;
2178aa9ebccSVladimir Oltean 	}
2188aa9ebccSVladimir Oltean 	return 0;
2198aa9ebccSVladimir Oltean }
2208aa9ebccSVladimir Oltean 
2218aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
2228aa9ebccSVladimir Oltean {
2238aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2248aa9ebccSVladimir Oltean 
2258aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
2268aa9ebccSVladimir Oltean 
227291d1e72SVladimir Oltean 	/* We only populate the FDB table through dynamic
228291d1e72SVladimir Oltean 	 * L2 Address Lookup entries
229291d1e72SVladimir Oltean 	 */
2308aa9ebccSVladimir Oltean 	if (table->entry_count) {
2318aa9ebccSVladimir Oltean 		kfree(table->entries);
2328aa9ebccSVladimir Oltean 		table->entry_count = 0;
2338aa9ebccSVladimir Oltean 	}
2348aa9ebccSVladimir Oltean 	return 0;
2358aa9ebccSVladimir Oltean }
2368aa9ebccSVladimir Oltean 
2378aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
2388aa9ebccSVladimir Oltean {
2398aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2406c56e167SVladimir Oltean 	u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
2418aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
2428456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
2438456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
2448aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
2458aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
2461da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
2471da73821SVladimir Oltean 		.start_dynspc = 0,
2486c56e167SVladimir Oltean 		.maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
2496c56e167SVladimir Oltean 			     max_fdb_entries, max_fdb_entries, },
2508aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
2518aa9ebccSVladimir Oltean 		.poly = 0x97,
2528aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
2538aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
2548aa9ebccSVladimir Oltean 		 */
2556d7c7d94SVladimir Oltean 		.shared_learn = true,
2568aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
2578aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
2588aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
2598aa9ebccSVladimir Oltean 		 */
2608aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
2618aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
2628aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
2638aa9ebccSVladimir Oltean 		 */
2648aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
2651da73821SVladimir Oltean 		/* P/Q/R/S only */
2661da73821SVladimir Oltean 		.use_static = true,
2671da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
2681da73821SVladimir Oltean 		 * dynamic FDB entries
2691da73821SVladimir Oltean 		 */
2701da73821SVladimir Oltean 		.owr_dyn = true,
2711da73821SVladimir Oltean 		.drpnolearn = true,
2728aa9ebccSVladimir Oltean 	};
2738aa9ebccSVladimir Oltean 
2748aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2758aa9ebccSVladimir Oltean 
2768aa9ebccSVladimir Oltean 	if (table->entry_count) {
2778aa9ebccSVladimir Oltean 		kfree(table->entries);
2788aa9ebccSVladimir Oltean 		table->entry_count = 0;
2798aa9ebccSVladimir Oltean 	}
2808aa9ebccSVladimir Oltean 
2818aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
2828aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2838aa9ebccSVladimir Oltean 	if (!table->entries)
2848aa9ebccSVladimir Oltean 		return -ENOMEM;
2858aa9ebccSVladimir Oltean 
2868aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
2878aa9ebccSVladimir Oltean 
2888aa9ebccSVladimir Oltean 	/* This table only has a single entry */
2898aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
2908aa9ebccSVladimir Oltean 				default_l2_lookup_params;
2918aa9ebccSVladimir Oltean 
2928aa9ebccSVladimir Oltean 	return 0;
2938aa9ebccSVladimir Oltean }
2948aa9ebccSVladimir Oltean 
2958aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
2968aa9ebccSVladimir Oltean {
2978aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2988aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
2998aa9ebccSVladimir Oltean 		.ving_mirr = 0,
3008aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
3018aa9ebccSVladimir Oltean 		.vmemb_port = 0,
3028aa9ebccSVladimir Oltean 		.vlan_bc = 0,
3038aa9ebccSVladimir Oltean 		.tag_port = 0,
304e3502b82SVladimir Oltean 		.vlanid = 1,
3058aa9ebccSVladimir Oltean 	};
3068aa9ebccSVladimir Oltean 	int i;
3078aa9ebccSVladimir Oltean 
3088aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
3098aa9ebccSVladimir Oltean 
310e3502b82SVladimir Oltean 	/* The static VLAN table will only contain the initial pvid of 1.
3116666cebcSVladimir Oltean 	 * All other VLANs are to be configured through dynamic entries,
3126666cebcSVladimir Oltean 	 * and kept in the static configuration table as backing memory.
3138aa9ebccSVladimir Oltean 	 */
3148aa9ebccSVladimir Oltean 	if (table->entry_count) {
3158aa9ebccSVladimir Oltean 		kfree(table->entries);
3168aa9ebccSVladimir Oltean 		table->entry_count = 0;
3178aa9ebccSVladimir Oltean 	}
3188aa9ebccSVladimir Oltean 
3198aa9ebccSVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
3208aa9ebccSVladimir Oltean 				 GFP_KERNEL);
3218aa9ebccSVladimir Oltean 	if (!table->entries)
3228aa9ebccSVladimir Oltean 		return -ENOMEM;
3238aa9ebccSVladimir Oltean 
3248aa9ebccSVladimir Oltean 	table->entry_count = 1;
3258aa9ebccSVladimir Oltean 
326e3502b82SVladimir Oltean 	/* VLAN 1: all DT-defined ports are members; no restrictions on
3278aa9ebccSVladimir Oltean 	 * forwarding; always transmit priority-tagged frames as untagged.
3288aa9ebccSVladimir Oltean 	 */
3298aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3308aa9ebccSVladimir Oltean 		pvid.vmemb_port |= BIT(i);
3318aa9ebccSVladimir Oltean 		pvid.vlan_bc |= BIT(i);
3328aa9ebccSVladimir Oltean 		pvid.tag_port &= ~BIT(i);
3338aa9ebccSVladimir Oltean 	}
3348aa9ebccSVladimir Oltean 
3358aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
3368aa9ebccSVladimir Oltean 	return 0;
3378aa9ebccSVladimir Oltean }
3388aa9ebccSVladimir Oltean 
3398aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
3408aa9ebccSVladimir Oltean {
3418aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
3428aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3438aa9ebccSVladimir Oltean 	int i, j;
3448aa9ebccSVladimir Oltean 
3458aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
3468aa9ebccSVladimir Oltean 
3478aa9ebccSVladimir Oltean 	if (table->entry_count) {
3488aa9ebccSVladimir Oltean 		kfree(table->entries);
3498aa9ebccSVladimir Oltean 		table->entry_count = 0;
3508aa9ebccSVladimir Oltean 	}
3518aa9ebccSVladimir Oltean 
3528aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
3538aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3548aa9ebccSVladimir Oltean 	if (!table->entries)
3558aa9ebccSVladimir Oltean 		return -ENOMEM;
3568aa9ebccSVladimir Oltean 
3578aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
3588aa9ebccSVladimir Oltean 
3598aa9ebccSVladimir Oltean 	l2fwd = table->entries;
3608aa9ebccSVladimir Oltean 
3618aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
3628aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3638aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
3648aa9ebccSVladimir Oltean 
3658aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
3668aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
3678aa9ebccSVladimir Oltean 
3688aa9ebccSVladimir Oltean 		if (i == upstream)
3698aa9ebccSVladimir Oltean 			continue;
3708aa9ebccSVladimir Oltean 
3718aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
3728aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
3738aa9ebccSVladimir Oltean 	}
3748aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
3758aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
3768aa9ebccSVladimir Oltean 	 */
3778aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++)
3788aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_PORTS; j++)
3798aa9ebccSVladimir Oltean 			l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
3808aa9ebccSVladimir Oltean 
3818aa9ebccSVladimir Oltean 	return 0;
3828aa9ebccSVladimir Oltean }
3838aa9ebccSVladimir Oltean 
3848aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
3858aa9ebccSVladimir Oltean {
3868aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
3878aa9ebccSVladimir Oltean 		/* Disallow dynamic reconfiguration of vlan_pmap */
3888aa9ebccSVladimir Oltean 		.max_dynp = 0,
3898aa9ebccSVladimir Oltean 		/* Use a single memory partition for all ingress queues */
3908aa9ebccSVladimir Oltean 		.part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
3918aa9ebccSVladimir Oltean 	};
3928aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3938aa9ebccSVladimir Oltean 
3948aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
3958aa9ebccSVladimir Oltean 
3968aa9ebccSVladimir Oltean 	if (table->entry_count) {
3978aa9ebccSVladimir Oltean 		kfree(table->entries);
3988aa9ebccSVladimir Oltean 		table->entry_count = 0;
3998aa9ebccSVladimir Oltean 	}
4008aa9ebccSVladimir Oltean 
4018aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
4028aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4038aa9ebccSVladimir Oltean 	if (!table->entries)
4048aa9ebccSVladimir Oltean 		return -ENOMEM;
4058aa9ebccSVladimir Oltean 
4068aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
4078aa9ebccSVladimir Oltean 
4088aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4098aa9ebccSVladimir Oltean 	((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
4108aa9ebccSVladimir Oltean 				default_l2fwd_params;
4118aa9ebccSVladimir Oltean 
4128aa9ebccSVladimir Oltean 	return 0;
4138aa9ebccSVladimir Oltean }
4148aa9ebccSVladimir Oltean 
4158aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
4168aa9ebccSVladimir Oltean {
4178aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
418511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
419511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
4208aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
4215f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
4225f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
4235f06c63bSVladimir Oltean 		 */
42408fde09aSVladimir Oltean 		.hostprio = 7,
4258aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
4268aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
42742824463SVladimir Oltean 		.incl_srcpt1 = false,
4288aa9ebccSVladimir Oltean 		.send_meta1  = false,
4298aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
4308aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
43142824463SVladimir Oltean 		.incl_srcpt0 = false,
4328aa9ebccSVladimir Oltean 		.send_meta0  = false,
4338aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
4348aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
4358aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
4368aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
4378aa9ebccSVladimir Oltean 		 */
4388aa9ebccSVladimir Oltean 		.host_port = dsa_upstream_port(priv->ds, 0),
439511e6ca0SVladimir Oltean 		/* Default to an invalid value */
440511e6ca0SVladimir Oltean 		.mirr_port = SJA1105_NUM_PORTS,
4418aa9ebccSVladimir Oltean 		/* Link-local traffic received on casc_port will be forwarded
4428aa9ebccSVladimir Oltean 		 * to host_port without embedding the source port and device ID
4438aa9ebccSVladimir Oltean 		 * info in the destination MAC address (presumably because it
4448aa9ebccSVladimir Oltean 		 * is a cascaded port and a downstream SJA switch already did
4458aa9ebccSVladimir Oltean 		 * that). Default to an invalid port (to disable the feature)
4468aa9ebccSVladimir Oltean 		 * and overwrite this if we find any DSA (cascaded) ports.
4478aa9ebccSVladimir Oltean 		 */
4488aa9ebccSVladimir Oltean 		.casc_port = SJA1105_NUM_PORTS,
4498aa9ebccSVladimir Oltean 		/* No TTEthernet */
450dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
4518aa9ebccSVladimir Oltean 		.vlmarker = 0,
4528aa9ebccSVladimir Oltean 		.vlmask = 0,
4538aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
4548aa9ebccSVladimir Oltean 		.ignore2stf = 0,
4556666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
4566666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
4576666cebcSVladimir Oltean 		 */
4586666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
4596666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
4608aa9ebccSVladimir Oltean 	};
4618aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4628aa9ebccSVladimir Oltean 
4638aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
4648aa9ebccSVladimir Oltean 
4658aa9ebccSVladimir Oltean 	if (table->entry_count) {
4668aa9ebccSVladimir Oltean 		kfree(table->entries);
4678aa9ebccSVladimir Oltean 		table->entry_count = 0;
4688aa9ebccSVladimir Oltean 	}
4698aa9ebccSVladimir Oltean 
4708aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
4718aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4728aa9ebccSVladimir Oltean 	if (!table->entries)
4738aa9ebccSVladimir Oltean 		return -ENOMEM;
4748aa9ebccSVladimir Oltean 
4758aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
4768aa9ebccSVladimir Oltean 
4778aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4788aa9ebccSVladimir Oltean 	((struct sja1105_general_params_entry *)table->entries)[0] =
4798aa9ebccSVladimir Oltean 				default_general_params;
4808aa9ebccSVladimir Oltean 
4818aa9ebccSVladimir Oltean 	return 0;
4828aa9ebccSVladimir Oltean }
4838aa9ebccSVladimir Oltean 
48479d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
48579d5511cSVladimir Oltean {
48679d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
48779d5511cSVladimir Oltean 	struct sja1105_table *table;
48879d5511cSVladimir Oltean 
48979d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
49079d5511cSVladimir Oltean 
49179d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
49279d5511cSVladimir Oltean 	if (table->entry_count) {
49379d5511cSVladimir Oltean 		kfree(table->entries);
49479d5511cSVladimir Oltean 		table->entry_count = 0;
49579d5511cSVladimir Oltean 	}
49679d5511cSVladimir Oltean 
49779d5511cSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
49879d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
49979d5511cSVladimir Oltean 	if (!table->entries)
50079d5511cSVladimir Oltean 		return -ENOMEM;
50179d5511cSVladimir Oltean 
50279d5511cSVladimir Oltean 	table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
50379d5511cSVladimir Oltean 
50479d5511cSVladimir Oltean 	avb = table->entries;
50579d5511cSVladimir Oltean 
50679d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
50779d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
50879d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
509747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
510747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
511747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
512747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
513747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
514747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
515747e5eb3SVladimir Oltean 	 */
516747e5eb3SVladimir Oltean 	avb->cas_master = false;
51779d5511cSVladimir Oltean 
51879d5511cSVladimir Oltean 	return 0;
51979d5511cSVladimir Oltean }
52079d5511cSVladimir Oltean 
521a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
522a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
523a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
524a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
525a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
526a7cc081cSVladimir Oltean  * will be used for this frame.
527a7cc081cSVladimir Oltean  *
528a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
529a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
530a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
531a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
532a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
533a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
534a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
535a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
536a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
537a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
538a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
539a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
540a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
541a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
542a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
543a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
544a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
545a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
546a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
547a7cc081cSVladimir Oltean  * +------------+--------+
548a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
549a7cc081cSVladimir Oltean  * +------------+--------+
550a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
551a7cc081cSVladimir Oltean  * +------------+--------+
552a7cc081cSVladimir Oltean  *    ...                                  ...
553a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
554a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
555a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
556a7cc081cSVladimir Oltean  *
557a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
558a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
559a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
560a7cc081cSVladimir Oltean  * lookup) equal.
561a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
562a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
563a7cc081cSVladimir Oltean  */
5648aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
5658aa9ebccSVladimir Oltean 
5668aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
5678aa9ebccSVladimir Oltean {
5688aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
5698aa9ebccSVladimir Oltean 	struct sja1105_table *table;
570a7cc081cSVladimir Oltean 	int port, tc;
5718aa9ebccSVladimir Oltean 
5728aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
5738aa9ebccSVladimir Oltean 
5748aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
5758aa9ebccSVladimir Oltean 	if (table->entry_count) {
5768aa9ebccSVladimir Oltean 		kfree(table->entries);
5778aa9ebccSVladimir Oltean 		table->entry_count = 0;
5788aa9ebccSVladimir Oltean 	}
5798aa9ebccSVladimir Oltean 
5808aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
5818aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5828aa9ebccSVladimir Oltean 	if (!table->entries)
5838aa9ebccSVladimir Oltean 		return -ENOMEM;
5848aa9ebccSVladimir Oltean 
5858aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
5868aa9ebccSVladimir Oltean 
5878aa9ebccSVladimir Oltean 	policing = table->entries;
5888aa9ebccSVladimir Oltean 
589a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
590a7cc081cSVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
591a7cc081cSVladimir Oltean 		int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + port;
592a7cc081cSVladimir Oltean 
593a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
594a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
595a7cc081cSVladimir Oltean 
596a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
597a7cc081cSVladimir Oltean 	}
598a7cc081cSVladimir Oltean 
599a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
600a7cc081cSVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
601c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
602c279c726SVladimir Oltean 
603a7cc081cSVladimir Oltean 		if (dsa_is_cpu_port(priv->ds, port))
604c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
6058aa9ebccSVladimir Oltean 
606a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
607a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
608a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
609a7cc081cSVladimir Oltean 		policing[port].partition = 0;
6108aa9ebccSVladimir Oltean 	}
611a7cc081cSVladimir Oltean 
6128aa9ebccSVladimir Oltean 	return 0;
6138aa9ebccSVladimir Oltean }
6148aa9ebccSVladimir Oltean 
6158aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv,
6168aa9ebccSVladimir Oltean 				      struct sja1105_dt_port *ports)
6178aa9ebccSVladimir Oltean {
6188aa9ebccSVladimir Oltean 	int rc;
6198aa9ebccSVladimir Oltean 
6208aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
6218aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
6228aa9ebccSVladimir Oltean 					priv->info->static_ops,
6238aa9ebccSVladimir Oltean 					priv->info->device_id);
6248aa9ebccSVladimir Oltean 	if (rc)
6258aa9ebccSVladimir Oltean 		return rc;
6268aa9ebccSVladimir Oltean 
6278aa9ebccSVladimir Oltean 	/* Build static configuration */
6288aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
6298aa9ebccSVladimir Oltean 	if (rc < 0)
6308aa9ebccSVladimir Oltean 		return rc;
6318aa9ebccSVladimir Oltean 	rc = sja1105_init_mii_settings(priv, ports);
6328aa9ebccSVladimir Oltean 	if (rc < 0)
6338aa9ebccSVladimir Oltean 		return rc;
6348aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
6358aa9ebccSVladimir Oltean 	if (rc < 0)
6368aa9ebccSVladimir Oltean 		return rc;
6378aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
6388aa9ebccSVladimir Oltean 	if (rc < 0)
6398aa9ebccSVladimir Oltean 		return rc;
6408aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
6418aa9ebccSVladimir Oltean 	if (rc < 0)
6428aa9ebccSVladimir Oltean 		return rc;
6438aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
6448aa9ebccSVladimir Oltean 	if (rc < 0)
6458aa9ebccSVladimir Oltean 		return rc;
6468aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
6478aa9ebccSVladimir Oltean 	if (rc < 0)
6488aa9ebccSVladimir Oltean 		return rc;
6498aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
6508aa9ebccSVladimir Oltean 	if (rc < 0)
6518aa9ebccSVladimir Oltean 		return rc;
6528aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
6538aa9ebccSVladimir Oltean 	if (rc < 0)
6548aa9ebccSVladimir Oltean 		return rc;
65579d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
65679d5511cSVladimir Oltean 	if (rc < 0)
65779d5511cSVladimir Oltean 		return rc;
6588aa9ebccSVladimir Oltean 
6598aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
6608aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
6618aa9ebccSVladimir Oltean }
6628aa9ebccSVladimir Oltean 
663f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
664f5b8631cSVladimir Oltean 				      const struct sja1105_dt_port *ports)
665f5b8631cSVladimir Oltean {
666f5b8631cSVladimir Oltean 	int i;
667f5b8631cSVladimir Oltean 
668f5b8631cSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
6699bca3a0aSOleksij Rempel 		if (ports[i].role == XMII_MAC)
670f5b8631cSVladimir Oltean 			continue;
671f5b8631cSVladimir Oltean 
6729bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
6739bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
674f5b8631cSVladimir Oltean 			priv->rgmii_rx_delay[i] = true;
675f5b8631cSVladimir Oltean 
6769bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
6779bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
678f5b8631cSVladimir Oltean 			priv->rgmii_tx_delay[i] = true;
679f5b8631cSVladimir Oltean 
680f5b8631cSVladimir Oltean 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
681f5b8631cSVladimir Oltean 		     !priv->info->setup_rgmii_delay)
682f5b8631cSVladimir Oltean 			return -EINVAL;
683f5b8631cSVladimir Oltean 	}
684f5b8631cSVladimir Oltean 	return 0;
685f5b8631cSVladimir Oltean }
686f5b8631cSVladimir Oltean 
6878aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
6888aa9ebccSVladimir Oltean 				    struct sja1105_dt_port *ports,
6898aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
6908aa9ebccSVladimir Oltean {
6918aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6928aa9ebccSVladimir Oltean 	struct device_node *child;
6938aa9ebccSVladimir Oltean 
69427afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
6958aa9ebccSVladimir Oltean 		struct device_node *phy_node;
6960c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
6978aa9ebccSVladimir Oltean 		u32 index;
6980c65b2b9SAndrew Lunn 		int err;
6998aa9ebccSVladimir Oltean 
7008aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
7018aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
7028aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
7038aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
7047ba771e3SNishka Dasgupta 			of_node_put(child);
7058aa9ebccSVladimir Oltean 			return -ENODEV;
7068aa9ebccSVladimir Oltean 		}
7078aa9ebccSVladimir Oltean 
7088aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
7090c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
7100c65b2b9SAndrew Lunn 		if (err) {
7118aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
7128aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
7138aa9ebccSVladimir Oltean 				index);
7147ba771e3SNishka Dasgupta 			of_node_put(child);
7158aa9ebccSVladimir Oltean 			return -ENODEV;
7168aa9ebccSVladimir Oltean 		}
7178aa9ebccSVladimir Oltean 		ports[index].phy_mode = phy_mode;
7188aa9ebccSVladimir Oltean 
7198aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
7208aa9ebccSVladimir Oltean 		if (!phy_node) {
7218aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
7228aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
7238aa9ebccSVladimir Oltean 					"properties missing!\n");
7247ba771e3SNishka Dasgupta 				of_node_put(child);
7258aa9ebccSVladimir Oltean 				return -ENODEV;
7268aa9ebccSVladimir Oltean 			}
7278aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
7288aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
7298aa9ebccSVladimir Oltean 			 */
7308aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
7318aa9ebccSVladimir Oltean 		} else {
7328aa9ebccSVladimir Oltean 			/* phy-handle present => put port in MAC role */
7338aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
7348aa9ebccSVladimir Oltean 			of_node_put(phy_node);
7358aa9ebccSVladimir Oltean 		}
7368aa9ebccSVladimir Oltean 
7378aa9ebccSVladimir Oltean 		/* The MAC/PHY role can be overridden with explicit bindings */
7388aa9ebccSVladimir Oltean 		if (of_property_read_bool(child, "sja1105,role-mac"))
7398aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
7408aa9ebccSVladimir Oltean 		else if (of_property_read_bool(child, "sja1105,role-phy"))
7418aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
7428aa9ebccSVladimir Oltean 	}
7438aa9ebccSVladimir Oltean 
7448aa9ebccSVladimir Oltean 	return 0;
7458aa9ebccSVladimir Oltean }
7468aa9ebccSVladimir Oltean 
7478aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv,
7488aa9ebccSVladimir Oltean 			    struct sja1105_dt_port *ports)
7498aa9ebccSVladimir Oltean {
7508aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
7518aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
7528aa9ebccSVladimir Oltean 	struct device_node *ports_node;
7538aa9ebccSVladimir Oltean 	int rc;
7548aa9ebccSVladimir Oltean 
7558aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
7568aa9ebccSVladimir Oltean 	if (!ports_node) {
7578aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
7588aa9ebccSVladimir Oltean 		return -ENODEV;
7598aa9ebccSVladimir Oltean 	}
7608aa9ebccSVladimir Oltean 
7618aa9ebccSVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
7628aa9ebccSVladimir Oltean 	of_node_put(ports_node);
7638aa9ebccSVladimir Oltean 
7648aa9ebccSVladimir Oltean 	return rc;
7658aa9ebccSVladimir Oltean }
7668aa9ebccSVladimir Oltean 
767ffe10e67SVladimir Oltean static int sja1105_sgmii_read(struct sja1105_private *priv, int pcs_reg)
768ffe10e67SVladimir Oltean {
769ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
770ffe10e67SVladimir Oltean 	u32 val;
771ffe10e67SVladimir Oltean 	int rc;
772ffe10e67SVladimir Oltean 
773ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->sgmii + pcs_reg, &val,
774ffe10e67SVladimir Oltean 			      NULL);
775ffe10e67SVladimir Oltean 	if (rc < 0)
776ffe10e67SVladimir Oltean 		return rc;
777ffe10e67SVladimir Oltean 
778ffe10e67SVladimir Oltean 	return val;
779ffe10e67SVladimir Oltean }
780ffe10e67SVladimir Oltean 
781ffe10e67SVladimir Oltean static int sja1105_sgmii_write(struct sja1105_private *priv, int pcs_reg,
782ffe10e67SVladimir Oltean 			       u16 pcs_val)
783ffe10e67SVladimir Oltean {
784ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
785ffe10e67SVladimir Oltean 	u32 val = pcs_val;
786ffe10e67SVladimir Oltean 	int rc;
787ffe10e67SVladimir Oltean 
788ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->sgmii + pcs_reg, &val,
789ffe10e67SVladimir Oltean 			      NULL);
790ffe10e67SVladimir Oltean 	if (rc < 0)
791ffe10e67SVladimir Oltean 		return rc;
792ffe10e67SVladimir Oltean 
793ffe10e67SVladimir Oltean 	return val;
794ffe10e67SVladimir Oltean }
795ffe10e67SVladimir Oltean 
796ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_config(struct sja1105_private *priv,
797ffe10e67SVladimir Oltean 				     bool an_enabled, bool an_master)
798ffe10e67SVladimir Oltean {
799ffe10e67SVladimir Oltean 	u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII;
800ffe10e67SVladimir Oltean 
801ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
802ffe10e67SVladimir Oltean 	 * stop the clock during LPI mode, make the MAC reconfigure
803ffe10e67SVladimir Oltean 	 * autonomously after PCS autoneg is done, flush the internal FIFOs.
804ffe10e67SVladimir Oltean 	 */
805ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC1, SJA1105_DC1_EN_VSMMD1 |
806ffe10e67SVladimir Oltean 					       SJA1105_DC1_CLOCK_STOP_EN |
807ffe10e67SVladimir Oltean 					       SJA1105_DC1_MAC_AUTO_SW |
808ffe10e67SVladimir Oltean 					       SJA1105_DC1_INIT);
809ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
810ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC2, SJA1105_DC2_TX_POL_INV_DISABLE);
811ffe10e67SVladimir Oltean 	/* AUTONEG_CONTROL: Use SGMII autoneg */
812ffe10e67SVladimir Oltean 	if (an_master)
813ffe10e67SVladimir Oltean 		ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK;
814ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_AC, ac);
815ffe10e67SVladimir Oltean 	/* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
816ffe10e67SVladimir Oltean 	 * sja1105_sgmii_pcs_force_speed must be called later for the link
817ffe10e67SVladimir Oltean 	 * to become operational.
818ffe10e67SVladimir Oltean 	 */
819ffe10e67SVladimir Oltean 	if (an_enabled)
820ffe10e67SVladimir Oltean 		sja1105_sgmii_write(priv, MII_BMCR,
821ffe10e67SVladimir Oltean 				    BMCR_ANENABLE | BMCR_ANRESTART);
822ffe10e67SVladimir Oltean }
823ffe10e67SVladimir Oltean 
824ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv,
825ffe10e67SVladimir Oltean 					  int speed)
826ffe10e67SVladimir Oltean {
827ffe10e67SVladimir Oltean 	int pcs_speed;
828ffe10e67SVladimir Oltean 
829ffe10e67SVladimir Oltean 	switch (speed) {
830ffe10e67SVladimir Oltean 	case SPEED_1000:
831ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED1000;
832ffe10e67SVladimir Oltean 		break;
833ffe10e67SVladimir Oltean 	case SPEED_100:
834ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED100;
835ffe10e67SVladimir Oltean 		break;
836ffe10e67SVladimir Oltean 	case SPEED_10:
837ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED10;
838ffe10e67SVladimir Oltean 		break;
839ffe10e67SVladimir Oltean 	default:
840ffe10e67SVladimir Oltean 		dev_err(priv->ds->dev, "Invalid speed %d\n", speed);
841ffe10e67SVladimir Oltean 		return;
842ffe10e67SVladimir Oltean 	}
843ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, MII_BMCR, pcs_speed | BMCR_FULLDPLX);
844ffe10e67SVladimir Oltean }
845ffe10e67SVladimir Oltean 
846c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
8478aa9ebccSVladimir Oltean static int sja1105_speed[] = {
848c44d0535SVladimir Oltean 	[SJA1105_SPEED_AUTO]		= SPEED_UNKNOWN,
849c44d0535SVladimir Oltean 	[SJA1105_SPEED_10MBPS]		= SPEED_10,
850c44d0535SVladimir Oltean 	[SJA1105_SPEED_100MBPS]		= SPEED_100,
851c44d0535SVladimir Oltean 	[SJA1105_SPEED_1000MBPS]	= SPEED_1000,
8528aa9ebccSVladimir Oltean };
8538aa9ebccSVladimir Oltean 
8548400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
8558aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
8568400cff6SVladimir Oltean 				      int speed_mbps)
8578aa9ebccSVladimir Oltean {
8588aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
8598aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
8608aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
8618aa9ebccSVladimir Oltean 	sja1105_phy_interface_t phy_mode;
8628aa9ebccSVladimir Oltean 	sja1105_speed_t speed;
8638aa9ebccSVladimir Oltean 	int rc;
8648aa9ebccSVladimir Oltean 
8658400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
8668400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
8678400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
8688400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
8698400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
8708400cff6SVladimir Oltean 	 */
8718aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
8728400cff6SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
8738aa9ebccSVladimir Oltean 
874f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
875c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
876a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
877a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
878a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
879a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
880a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
881a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
882a979a0abSVladimir Oltean 		 */
883f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_AUTO;
884f4cfcfbdSVladimir Oltean 		break;
885c44d0535SVladimir Oltean 	case SPEED_10:
886f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_10MBPS;
887f4cfcfbdSVladimir Oltean 		break;
888c44d0535SVladimir Oltean 	case SPEED_100:
889f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_100MBPS;
890f4cfcfbdSVladimir Oltean 		break;
891c44d0535SVladimir Oltean 	case SPEED_1000:
892f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_1000MBPS;
893f4cfcfbdSVladimir Oltean 		break;
894f4cfcfbdSVladimir Oltean 	default:
8958aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
8968aa9ebccSVladimir Oltean 		return -EINVAL;
8978aa9ebccSVladimir Oltean 	}
8988aa9ebccSVladimir Oltean 
8998400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
9008400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
9018400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
9028400cff6SVladimir Oltean 	 * we want auto during upload phase).
903ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
904ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
9058aa9ebccSVladimir Oltean 	 */
906ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port))
907ffe10e67SVladimir Oltean 		mac[port].speed = SJA1105_SPEED_1000MBPS;
908ffe10e67SVladimir Oltean 	else
9098aa9ebccSVladimir Oltean 		mac[port].speed = speed;
9108aa9ebccSVladimir Oltean 
9118aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
9128400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
9138400cff6SVladimir Oltean 					  &mac[port], true);
9148aa9ebccSVladimir Oltean 	if (rc < 0) {
9158aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
9168aa9ebccSVladimir Oltean 		return rc;
9178aa9ebccSVladimir Oltean 	}
9188aa9ebccSVladimir Oltean 
9198aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
9208aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
9218aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
9228aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
9238aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
9248aa9ebccSVladimir Oltean 	 */
9258aa9ebccSVladimir Oltean 	phy_mode = mii->xmii_mode[port];
9268aa9ebccSVladimir Oltean 	if (phy_mode != XMII_MODE_RGMII)
9278aa9ebccSVladimir Oltean 		return 0;
9288aa9ebccSVladimir Oltean 
9298aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
9308aa9ebccSVladimir Oltean }
9318aa9ebccSVladimir Oltean 
93239710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
93339710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
93439710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
93539710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
93639710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
93739710229SVladimir Oltean  * now.
93839710229SVladimir Oltean  */
93939710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
94039710229SVladimir Oltean 				      phy_interface_t interface)
94139710229SVladimir Oltean {
94239710229SVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
94339710229SVladimir Oltean 	sja1105_phy_interface_t phy_mode;
94439710229SVladimir Oltean 
94539710229SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
94639710229SVladimir Oltean 	phy_mode = mii->xmii_mode[port];
94739710229SVladimir Oltean 
94839710229SVladimir Oltean 	switch (interface) {
94939710229SVladimir Oltean 	case PHY_INTERFACE_MODE_MII:
95039710229SVladimir Oltean 		return (phy_mode != XMII_MODE_MII);
95139710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RMII:
95239710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RMII);
95339710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII:
95439710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_ID:
95539710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_RXID:
95639710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_TXID:
95739710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RGMII);
958ffe10e67SVladimir Oltean 	case PHY_INTERFACE_MODE_SGMII:
959ffe10e67SVladimir Oltean 		return (phy_mode != XMII_MODE_SGMII);
96039710229SVladimir Oltean 	default:
96139710229SVladimir Oltean 		return true;
96239710229SVladimir Oltean 	}
96339710229SVladimir Oltean }
96439710229SVladimir Oltean 
965af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
966ffe10e67SVladimir Oltean 			       unsigned int mode,
967af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
9688aa9ebccSVladimir Oltean {
9698aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
970ffe10e67SVladimir Oltean 	bool is_sgmii = sja1105_supports_sgmii(priv, port);
9718aa9ebccSVladimir Oltean 
972ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
973ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
974ec8582d1SVladimir Oltean 			phy_modes(state->interface));
97539710229SVladimir Oltean 		return;
976ec8582d1SVladimir Oltean 	}
97739710229SVladimir Oltean 
978ffe10e67SVladimir Oltean 	if (phylink_autoneg_inband(mode) && !is_sgmii) {
9799f971573SVladimir Oltean 		dev_err(ds->dev, "In-band AN not supported!\n");
9809f971573SVladimir Oltean 		return;
9819f971573SVladimir Oltean 	}
982ffe10e67SVladimir Oltean 
983ffe10e67SVladimir Oltean 	if (is_sgmii)
984ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, phylink_autoneg_inband(mode),
985ffe10e67SVladimir Oltean 					 false);
9868400cff6SVladimir Oltean }
9878400cff6SVladimir Oltean 
9888400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
9898400cff6SVladimir Oltean 				  unsigned int mode,
9908400cff6SVladimir Oltean 				  phy_interface_t interface)
9918400cff6SVladimir Oltean {
9928400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
9938400cff6SVladimir Oltean }
9948400cff6SVladimir Oltean 
9958400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
9968400cff6SVladimir Oltean 				unsigned int mode,
9978400cff6SVladimir Oltean 				phy_interface_t interface,
9985b502a7bSRussell King 				struct phy_device *phydev,
9995b502a7bSRussell King 				int speed, int duplex,
10005b502a7bSRussell King 				bool tx_pause, bool rx_pause)
10018400cff6SVladimir Oltean {
1002ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1003ec8582d1SVladimir Oltean 
1004ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1005ec8582d1SVladimir Oltean 
1006ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode))
1007ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_force_speed(priv, speed);
1008ffe10e67SVladimir Oltean 
1009ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
10108aa9ebccSVladimir Oltean }
10118aa9ebccSVladimir Oltean 
1012ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1013ad9f299aSVladimir Oltean 				     unsigned long *supported,
1014ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
1015ad9f299aSVladimir Oltean {
1016ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
1017ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
1018ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
1019ad9f299aSVladimir Oltean 	 */
1020ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1021ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1022ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1023ad9f299aSVladimir Oltean 
1024ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1025ad9f299aSVladimir Oltean 
102639710229SVladimir Oltean 	/* include/linux/phylink.h says:
102739710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
102839710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
102939710229SVladimir Oltean 	 */
103039710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
103139710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
103239710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
103339710229SVladimir Oltean 		return;
103439710229SVladimir Oltean 	}
103539710229SVladimir Oltean 
1036ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1037ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1038ad9f299aSVladimir Oltean 	 */
1039ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
1040ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
1041ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
1042ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
1043ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
1044ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1045ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1046ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
1047ad9f299aSVladimir Oltean 
1048ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1049ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1050ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1051ad9f299aSVladimir Oltean }
1052ad9f299aSVladimir Oltean 
1053ffe10e67SVladimir Oltean static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port,
1054ffe10e67SVladimir Oltean 				     struct phylink_link_state *state)
1055ffe10e67SVladimir Oltean {
1056ffe10e67SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1057ffe10e67SVladimir Oltean 	int ais;
1058ffe10e67SVladimir Oltean 
1059ffe10e67SVladimir Oltean 	/* Read the vendor-specific AUTONEG_INTR_STATUS register */
1060ffe10e67SVladimir Oltean 	ais = sja1105_sgmii_read(priv, SJA1105_AIS);
1061ffe10e67SVladimir Oltean 	if (ais < 0)
1062ffe10e67SVladimir Oltean 		return ais;
1063ffe10e67SVladimir Oltean 
1064ffe10e67SVladimir Oltean 	switch (SJA1105_AIS_SPEED(ais)) {
1065ffe10e67SVladimir Oltean 	case 0:
1066ffe10e67SVladimir Oltean 		state->speed = SPEED_10;
1067ffe10e67SVladimir Oltean 		break;
1068ffe10e67SVladimir Oltean 	case 1:
1069ffe10e67SVladimir Oltean 		state->speed = SPEED_100;
1070ffe10e67SVladimir Oltean 		break;
1071ffe10e67SVladimir Oltean 	case 2:
1072ffe10e67SVladimir Oltean 		state->speed = SPEED_1000;
1073ffe10e67SVladimir Oltean 		break;
1074ffe10e67SVladimir Oltean 	default:
1075ffe10e67SVladimir Oltean 		dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n",
1076ffe10e67SVladimir Oltean 			SJA1105_AIS_SPEED(ais));
1077ffe10e67SVladimir Oltean 	}
1078ffe10e67SVladimir Oltean 	state->duplex = SJA1105_AIS_DUPLEX_MODE(ais);
1079ffe10e67SVladimir Oltean 	state->an_complete = SJA1105_AIS_COMPLETE(ais);
1080ffe10e67SVladimir Oltean 	state->link = SJA1105_AIS_LINK_STATUS(ais);
1081ffe10e67SVladimir Oltean 
1082ffe10e67SVladimir Oltean 	return 0;
1083ffe10e67SVladimir Oltean }
1084ffe10e67SVladimir Oltean 
108560f6053fSVladimir Oltean static int
108660f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
108760f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
108860f6053fSVladimir Oltean {
108960f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
109060f6053fSVladimir Oltean 	struct sja1105_table *table;
109160f6053fSVladimir Oltean 	int i;
109260f6053fSVladimir Oltean 
109360f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
109460f6053fSVladimir Oltean 	l2_lookup = table->entries;
109560f6053fSVladimir Oltean 
109660f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
109760f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
109860f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
109960f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
110060f6053fSVladimir Oltean 			return i;
110160f6053fSVladimir Oltean 
110260f6053fSVladimir Oltean 	return -1;
110360f6053fSVladimir Oltean }
110460f6053fSVladimir Oltean 
110560f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
110660f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
110760f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
110860f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
110960f6053fSVladimir Oltean  */
111060f6053fSVladimir Oltean static int
111160f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
111260f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
111360f6053fSVladimir Oltean 			  bool keep)
111460f6053fSVladimir Oltean {
111560f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
111660f6053fSVladimir Oltean 	struct sja1105_table *table;
111760f6053fSVladimir Oltean 	int rc, match;
111860f6053fSVladimir Oltean 
111960f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
112060f6053fSVladimir Oltean 
112160f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
112260f6053fSVladimir Oltean 	if (match < 0) {
112360f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
112460f6053fSVladimir Oltean 		if (!keep)
112560f6053fSVladimir Oltean 			return 0;
112660f6053fSVladimir Oltean 
112760f6053fSVladimir Oltean 		/* No match => new entry */
112860f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
112960f6053fSVladimir Oltean 		if (rc)
113060f6053fSVladimir Oltean 			return rc;
113160f6053fSVladimir Oltean 
113260f6053fSVladimir Oltean 		match = table->entry_count - 1;
113360f6053fSVladimir Oltean 	}
113460f6053fSVladimir Oltean 
113560f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
113660f6053fSVladimir Oltean 	l2_lookup = table->entries;
113760f6053fSVladimir Oltean 
113860f6053fSVladimir Oltean 	/* We have a match.
113960f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
114060f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
114160f6053fSVladimir Oltean 	 * which we update it).
114260f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
114360f6053fSVladimir Oltean 	 */
114460f6053fSVladimir Oltean 	if (keep) {
114560f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
114660f6053fSVladimir Oltean 		return 0;
114760f6053fSVladimir Oltean 	}
114860f6053fSVladimir Oltean 
114960f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
115060f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
115160f6053fSVladimir Oltean 	 */
115260f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
115360f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
115460f6053fSVladimir Oltean }
115560f6053fSVladimir Oltean 
1156291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1157291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1158291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1159291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1160291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1161291d1e72SVladimir Oltean  */
116209c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1163291d1e72SVladimir Oltean {
1164291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1165291d1e72SVladimir Oltean }
1166291d1e72SVladimir Oltean 
11679dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1168291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1169291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1170291d1e72SVladimir Oltean 					 int *last_unused)
1171291d1e72SVladimir Oltean {
1172291d1e72SVladimir Oltean 	int way;
1173291d1e72SVladimir Oltean 
1174291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1175291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1176291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1177291d1e72SVladimir Oltean 
1178291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1179291d1e72SVladimir Oltean 		 * into the return value
1180291d1e72SVladimir Oltean 		 */
1181291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1182291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1183291d1e72SVladimir Oltean 			if (last_unused)
1184291d1e72SVladimir Oltean 				*last_unused = way;
1185291d1e72SVladimir Oltean 			continue;
1186291d1e72SVladimir Oltean 		}
1187291d1e72SVladimir Oltean 
1188291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1189291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1190291d1e72SVladimir Oltean 			if (match)
1191291d1e72SVladimir Oltean 				*match = l2_lookup;
1192291d1e72SVladimir Oltean 			return way;
1193291d1e72SVladimir Oltean 		}
1194291d1e72SVladimir Oltean 	}
1195291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1196291d1e72SVladimir Oltean 	return -1;
1197291d1e72SVladimir Oltean }
1198291d1e72SVladimir Oltean 
11999dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1200291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1201291d1e72SVladimir Oltean {
1202291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1203291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1204291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1205291d1e72SVladimir Oltean 	int last_unused = -1;
120660f6053fSVladimir Oltean 	int bin, way, rc;
1207291d1e72SVladimir Oltean 
12089dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1209291d1e72SVladimir Oltean 
12109dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1211291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1212291d1e72SVladimir Oltean 	if (way >= 0) {
1213291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1214291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1215291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1216291d1e72SVladimir Oltean 		 */
1217291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1218291d1e72SVladimir Oltean 			return 0;
1219291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1220291d1e72SVladimir Oltean 	} else {
1221291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1222291d1e72SVladimir Oltean 
1223291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1224291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1225291d1e72SVladimir Oltean 		 */
1226291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1227291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1228291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1229291d1e72SVladimir Oltean 
1230291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1231291d1e72SVladimir Oltean 			way = last_unused;
1232291d1e72SVladimir Oltean 		} else {
1233291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1234291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1235291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1236291d1e72SVladimir Oltean 			 * distribution function:
1237291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1238291d1e72SVladimir Oltean 			 */
1239291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1240291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1241291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1242291d1e72SVladimir Oltean 				 bin, addr, way);
1243291d1e72SVladimir Oltean 			/* Evict entry */
1244291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1245291d1e72SVladimir Oltean 						     index, NULL, false);
1246291d1e72SVladimir Oltean 		}
1247291d1e72SVladimir Oltean 	}
1248291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1249291d1e72SVladimir Oltean 
125060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1251291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1252291d1e72SVladimir Oltean 					  true);
125360f6053fSVladimir Oltean 	if (rc < 0)
125460f6053fSVladimir Oltean 		return rc;
125560f6053fSVladimir Oltean 
125660f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1257291d1e72SVladimir Oltean }
1258291d1e72SVladimir Oltean 
12599dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1260291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1261291d1e72SVladimir Oltean {
1262291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1263291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
126460f6053fSVladimir Oltean 	int index, bin, way, rc;
1265291d1e72SVladimir Oltean 	bool keep;
1266291d1e72SVladimir Oltean 
12679dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
12689dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1269291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1270291d1e72SVladimir Oltean 	if (way < 0)
1271291d1e72SVladimir Oltean 		return 0;
1272291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1273291d1e72SVladimir Oltean 
1274291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1275291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1276291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1277291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1278291d1e72SVladimir Oltean 	 */
1279291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
12807752e937SVladimir Oltean 
1281291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1282291d1e72SVladimir Oltean 		keep = true;
1283291d1e72SVladimir Oltean 	else
1284291d1e72SVladimir Oltean 		keep = false;
1285291d1e72SVladimir Oltean 
128660f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1287291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
128860f6053fSVladimir Oltean 	if (rc < 0)
128960f6053fSVladimir Oltean 		return rc;
129060f6053fSVladimir Oltean 
129160f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1292291d1e72SVladimir Oltean }
1293291d1e72SVladimir Oltean 
12949dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
12959dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
12969dfa6911SVladimir Oltean {
12971da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
12981da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12991da73821SVladimir Oltean 	int rc, i;
13001da73821SVladimir Oltean 
13011da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
13021da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
13031da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
13041da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
13051da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
13067f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
13071da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
13081da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
13096d7c7d94SVladimir Oltean 	} else {
13106d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
13116d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
13126d7c7d94SVladimir Oltean 	}
13131da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
13141da73821SVladimir Oltean 
13151da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13161da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
13171da73821SVladimir Oltean 	if (rc == 0) {
13181da73821SVladimir Oltean 		/* Found and this port is already in the entry's
13191da73821SVladimir Oltean 		 * port mask => job done
13201da73821SVladimir Oltean 		 */
13211da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
13221da73821SVladimir Oltean 			return 0;
13231da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
13241da73821SVladimir Oltean 		 * found something.
13251da73821SVladimir Oltean 		 */
13261da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
13271da73821SVladimir Oltean 		goto skip_finding_an_index;
13281da73821SVladimir Oltean 	}
13291da73821SVladimir Oltean 
13301da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
13311da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
13321da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
13331da73821SVladimir Oltean 	 */
13341da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
13351da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13361da73821SVladimir Oltean 						 i, NULL);
13371da73821SVladimir Oltean 		if (rc < 0)
13381da73821SVladimir Oltean 			break;
13391da73821SVladimir Oltean 	}
13401da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
13411da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
13421da73821SVladimir Oltean 		return -EINVAL;
13431da73821SVladimir Oltean 	}
134417ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
13451da73821SVladimir Oltean 	l2_lookup.index = i;
13461da73821SVladimir Oltean 
13471da73821SVladimir Oltean skip_finding_an_index:
134860f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
13491da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
13501da73821SVladimir Oltean 					  true);
135160f6053fSVladimir Oltean 	if (rc < 0)
135260f6053fSVladimir Oltean 		return rc;
135360f6053fSVladimir Oltean 
135460f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
13559dfa6911SVladimir Oltean }
13569dfa6911SVladimir Oltean 
13579dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
13589dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
13599dfa6911SVladimir Oltean {
13601da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
13611da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
13621da73821SVladimir Oltean 	bool keep;
13631da73821SVladimir Oltean 	int rc;
13641da73821SVladimir Oltean 
13651da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
13661da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
13671da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
13681da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
13697f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
13701da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
13711da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
13726d7c7d94SVladimir Oltean 	} else {
13736d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
13746d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
13756d7c7d94SVladimir Oltean 	}
13761da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
13771da73821SVladimir Oltean 
13781da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13791da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
13801da73821SVladimir Oltean 	if (rc < 0)
13811da73821SVladimir Oltean 		return 0;
13821da73821SVladimir Oltean 
13831da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
13841da73821SVladimir Oltean 
13851da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
13861da73821SVladimir Oltean 	 * or if we remove it completely.
13871da73821SVladimir Oltean 	 */
13881da73821SVladimir Oltean 	if (l2_lookup.destports)
13891da73821SVladimir Oltean 		keep = true;
13901da73821SVladimir Oltean 	else
13911da73821SVladimir Oltean 		keep = false;
13921da73821SVladimir Oltean 
139360f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
13941da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
139560f6053fSVladimir Oltean 	if (rc < 0)
139660f6053fSVladimir Oltean 		return rc;
139760f6053fSVladimir Oltean 
139860f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
13999dfa6911SVladimir Oltean }
14009dfa6911SVladimir Oltean 
14019dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
14029dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
14039dfa6911SVladimir Oltean {
14049dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1405b3ee526aSVladimir Oltean 
14066d7c7d94SVladimir Oltean 	/* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
14076d7c7d94SVladimir Oltean 	 * so the switch still does some VLAN processing internally.
14086d7c7d94SVladimir Oltean 	 * But Shared VLAN Learning (SVL) is also active, and it will take
14096d7c7d94SVladimir Oltean 	 * care of autonomous forwarding between the unique pvid's of each
14106d7c7d94SVladimir Oltean 	 * port.  Here we just make sure that users can't add duplicate FDB
14116d7c7d94SVladimir Oltean 	 * entries when in this mode - the actual VID doesn't matter except
14126d7c7d94SVladimir Oltean 	 * for what gets printed in 'bridge fdb show'.  In the case of zero,
14136d7c7d94SVladimir Oltean 	 * no VID gets printed at all.
141493647594SVladimir Oltean 	 */
14157f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
14166d7c7d94SVladimir Oltean 		vid = 0;
141793647594SVladimir Oltean 
14186d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
14199dfa6911SVladimir Oltean }
14209dfa6911SVladimir Oltean 
14219dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
14229dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
14239dfa6911SVladimir Oltean {
14249dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14259dfa6911SVladimir Oltean 
14267f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
14276d7c7d94SVladimir Oltean 		vid = 0;
14286d7c7d94SVladimir Oltean 
1429b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
14309dfa6911SVladimir Oltean }
14319dfa6911SVladimir Oltean 
1432291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1433291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1434291d1e72SVladimir Oltean {
1435291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1436291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1437291d1e72SVladimir Oltean 	int i;
1438291d1e72SVladimir Oltean 
1439291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1440291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1441291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1442291d1e72SVladimir Oltean 		int rc;
1443291d1e72SVladimir Oltean 
1444291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1445291d1e72SVladimir Oltean 						 i, &l2_lookup);
1446291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1447def84604SVladimir Oltean 		if (rc == -ENOENT)
1448291d1e72SVladimir Oltean 			continue;
1449291d1e72SVladimir Oltean 		if (rc) {
1450291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1451291d1e72SVladimir Oltean 			return rc;
1452291d1e72SVladimir Oltean 		}
1453291d1e72SVladimir Oltean 
1454291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1455291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1456291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1457291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1458291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1459291d1e72SVladimir Oltean 		 */
1460291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1461291d1e72SVladimir Oltean 			continue;
1462291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
146393647594SVladimir Oltean 
14646d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
14657f14937fSVladimir Oltean 		if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
14666d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
146717ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1468291d1e72SVladimir Oltean 	}
1469291d1e72SVladimir Oltean 	return 0;
1470291d1e72SVladimir Oltean }
1471291d1e72SVladimir Oltean 
1472291d1e72SVladimir Oltean /* This callback needs to be present */
1473291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1474291d1e72SVladimir Oltean 			       const struct switchdev_obj_port_mdb *mdb)
1475291d1e72SVladimir Oltean {
1476291d1e72SVladimir Oltean 	return 0;
1477291d1e72SVladimir Oltean }
1478291d1e72SVladimir Oltean 
1479291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1480291d1e72SVladimir Oltean 			    const struct switchdev_obj_port_mdb *mdb)
1481291d1e72SVladimir Oltean {
1482291d1e72SVladimir Oltean 	sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1483291d1e72SVladimir Oltean }
1484291d1e72SVladimir Oltean 
1485291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1486291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1487291d1e72SVladimir Oltean {
1488291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1489291d1e72SVladimir Oltean }
1490291d1e72SVladimir Oltean 
14918aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
14928aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
14938aa9ebccSVladimir Oltean {
14948aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
14958aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14968aa9ebccSVladimir Oltean 	int i, rc;
14978aa9ebccSVladimir Oltean 
14988aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
14998aa9ebccSVladimir Oltean 
15008aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
15018aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
15028aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
15038aa9ebccSVladimir Oltean 		 */
15048aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
15058aa9ebccSVladimir Oltean 			continue;
15068aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
15078aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
15088aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
15098aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
15108aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
15118aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
15128aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
15138aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
15148aa9ebccSVladimir Oltean 		 */
15158aa9ebccSVladimir Oltean 		if (i == port)
15168aa9ebccSVladimir Oltean 			continue;
15178aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
15188aa9ebccSVladimir Oltean 			continue;
15198aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
15208aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
15218aa9ebccSVladimir Oltean 
15228aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
15238aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
15248aa9ebccSVladimir Oltean 		if (rc < 0)
15258aa9ebccSVladimir Oltean 			return rc;
15268aa9ebccSVladimir Oltean 	}
15278aa9ebccSVladimir Oltean 
15288aa9ebccSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
15298aa9ebccSVladimir Oltean 					    port, &l2_fwd[port], true);
15308aa9ebccSVladimir Oltean }
15318aa9ebccSVladimir Oltean 
1532640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1533640f763fSVladimir Oltean 					 u8 state)
1534640f763fSVladimir Oltean {
1535640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1536640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1537640f763fSVladimir Oltean 
1538640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1539640f763fSVladimir Oltean 
1540640f763fSVladimir Oltean 	switch (state) {
1541640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1542640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1543640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1544640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1545640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1546640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1547640f763fSVladimir Oltean 		 */
1548640f763fSVladimir Oltean 		mac[port].ingress   = false;
1549640f763fSVladimir Oltean 		mac[port].egress    = false;
1550640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1551640f763fSVladimir Oltean 		break;
1552640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1553640f763fSVladimir Oltean 		mac[port].ingress   = true;
1554640f763fSVladimir Oltean 		mac[port].egress    = false;
1555640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1556640f763fSVladimir Oltean 		break;
1557640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1558640f763fSVladimir Oltean 		mac[port].ingress   = true;
1559640f763fSVladimir Oltean 		mac[port].egress    = false;
1560640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1561640f763fSVladimir Oltean 		break;
1562640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1563640f763fSVladimir Oltean 		mac[port].ingress   = true;
1564640f763fSVladimir Oltean 		mac[port].egress    = true;
1565640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1566640f763fSVladimir Oltean 		break;
1567640f763fSVladimir Oltean 	default:
1568640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1569640f763fSVladimir Oltean 		return;
1570640f763fSVladimir Oltean 	}
1571640f763fSVladimir Oltean 
1572640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1573640f763fSVladimir Oltean 				     &mac[port], true);
1574640f763fSVladimir Oltean }
1575640f763fSVladimir Oltean 
15768aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
15778aa9ebccSVladimir Oltean 			       struct net_device *br)
15788aa9ebccSVladimir Oltean {
15798aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
15808aa9ebccSVladimir Oltean }
15818aa9ebccSVladimir Oltean 
15828aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
15838aa9ebccSVladimir Oltean 				 struct net_device *br)
15848aa9ebccSVladimir Oltean {
15858aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
15868aa9ebccSVladimir Oltean }
15878aa9ebccSVladimir Oltean 
15882eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
15892eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
15902eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
15912eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
15922eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
1593c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1594dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
15952eea1fa8SVladimir Oltean };
15962eea1fa8SVladimir Oltean 
15976666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
15986666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
15996666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
16006666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
16016666cebcSVladimir Oltean  * such that this operation is relatively seamless.
16026666cebcSVladimir Oltean  */
16032eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
16042eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
16056666cebcSVladimir Oltean {
16066cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
16076cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
16086666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
16096666cebcSVladimir Oltean 	int speed_mbps[SJA1105_NUM_PORTS];
16106cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
16116cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
16126cf99c13SVladimir Oltean 	s64 t12, t34;
1613ffe10e67SVladimir Oltean 	u16 bmcr = 0;
16146666cebcSVladimir Oltean 	int rc, i;
16156cf99c13SVladimir Oltean 	s64 now;
16166666cebcSVladimir Oltean 
1617af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1618af580ae2SVladimir Oltean 
16196666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
16206666cebcSVladimir Oltean 
16218400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
16228400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
16238400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
16248400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
16256666cebcSVladimir Oltean 	 */
16266666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
16276666cebcSVladimir Oltean 		speed_mbps[i] = sja1105_speed[mac[i].speed];
16286666cebcSVladimir Oltean 		mac[i].speed = SJA1105_SPEED_AUTO;
16296666cebcSVladimir Oltean 	}
16306666cebcSVladimir Oltean 
1631ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT))
1632ffe10e67SVladimir Oltean 		bmcr = sja1105_sgmii_read(priv, MII_BMCR);
1633ffe10e67SVladimir Oltean 
16346cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
16356cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
16366cf99c13SVladimir Oltean 
16376cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
16386cf99c13SVladimir Oltean 	if (rc < 0)
16396cf99c13SVladimir Oltean 		goto out_unlock_ptp;
16406cf99c13SVladimir Oltean 
16416666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
16426666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
16436666cebcSVladimir Oltean 	if (rc < 0)
16446cf99c13SVladimir Oltean 		goto out_unlock_ptp;
16456cf99c13SVladimir Oltean 
16466cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
16476cf99c13SVladimir Oltean 	if (rc < 0)
16486cf99c13SVladimir Oltean 		goto out_unlock_ptp;
16496cf99c13SVladimir Oltean 
16506cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
16516cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
16526cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
16536cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
16546cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
16556cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
16566cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
16576cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
16586cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
16596cf99c13SVladimir Oltean 	now += (t34 - t12);
16606cf99c13SVladimir Oltean 
16616cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
16626cf99c13SVladimir Oltean 
16636cf99c13SVladimir Oltean out_unlock_ptp:
16646cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
16656666cebcSVladimir Oltean 
16662eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
16672eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
16682eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
16692eea1fa8SVladimir Oltean 
16706666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
16716666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
16726666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
16736666cebcSVladimir Oltean 	 */
16746666cebcSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
16756666cebcSVladimir Oltean 	if (rc < 0)
16766666cebcSVladimir Oltean 		goto out;
16776666cebcSVladimir Oltean 
16786666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
16798400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
16806666cebcSVladimir Oltean 		if (rc < 0)
16816666cebcSVladimir Oltean 			goto out;
16826666cebcSVladimir Oltean 	}
1683ffe10e67SVladimir Oltean 
1684ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) {
1685ffe10e67SVladimir Oltean 		bool an_enabled = !!(bmcr & BMCR_ANENABLE);
1686ffe10e67SVladimir Oltean 
1687ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, an_enabled, false);
1688ffe10e67SVladimir Oltean 
1689ffe10e67SVladimir Oltean 		if (!an_enabled) {
1690ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
1691ffe10e67SVladimir Oltean 
1692ffe10e67SVladimir Oltean 			if (bmcr & BMCR_SPEED1000)
1693ffe10e67SVladimir Oltean 				speed = SPEED_1000;
1694ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED100)
1695ffe10e67SVladimir Oltean 				speed = SPEED_100;
1696ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED10)
1697ffe10e67SVladimir Oltean 				speed = SPEED_10;
1698ffe10e67SVladimir Oltean 
1699ffe10e67SVladimir Oltean 			sja1105_sgmii_pcs_force_speed(priv, speed);
1700ffe10e67SVladimir Oltean 		}
1701ffe10e67SVladimir Oltean 	}
17026666cebcSVladimir Oltean out:
1703af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1704af580ae2SVladimir Oltean 
17056666cebcSVladimir Oltean 	return rc;
17066666cebcSVladimir Oltean }
17076666cebcSVladimir Oltean 
17086666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
17096666cebcSVladimir Oltean {
17106666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
17116666cebcSVladimir Oltean 
17126666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
17136666cebcSVladimir Oltean 
17146666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
17156666cebcSVladimir Oltean 
17166666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
17176666cebcSVladimir Oltean 					   &mac[port], true);
17186666cebcSVladimir Oltean }
17196666cebcSVladimir Oltean 
17206666cebcSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
17216666cebcSVladimir Oltean {
17226666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
17236666cebcSVladimir Oltean 	int count, i;
17246666cebcSVladimir Oltean 
17256666cebcSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
17266666cebcSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
17276666cebcSVladimir Oltean 
17286666cebcSVladimir Oltean 	for (i = 0; i < count; i++)
17296666cebcSVladimir Oltean 		if (vlan[i].vlanid == vid)
17306666cebcSVladimir Oltean 			return i;
17316666cebcSVladimir Oltean 
17326666cebcSVladimir Oltean 	/* Return an invalid entry index if not found */
17336666cebcSVladimir Oltean 	return -1;
17346666cebcSVladimir Oltean }
17356666cebcSVladimir Oltean 
17366666cebcSVladimir Oltean static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid,
17376666cebcSVladimir Oltean 			      bool enabled, bool untagged)
17386666cebcSVladimir Oltean {
17396666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
17406666cebcSVladimir Oltean 	struct sja1105_table *table;
17416666cebcSVladimir Oltean 	bool keep = true;
17426666cebcSVladimir Oltean 	int match, rc;
17436666cebcSVladimir Oltean 
17446666cebcSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
17456666cebcSVladimir Oltean 
17466666cebcSVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
17476666cebcSVladimir Oltean 	if (match < 0) {
17486666cebcSVladimir Oltean 		/* Can't delete a missing entry. */
17496666cebcSVladimir Oltean 		if (!enabled)
17506666cebcSVladimir Oltean 			return 0;
17516666cebcSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
17526666cebcSVladimir Oltean 		if (rc)
17536666cebcSVladimir Oltean 			return rc;
17546666cebcSVladimir Oltean 		match = table->entry_count - 1;
17556666cebcSVladimir Oltean 	}
17566666cebcSVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
17576666cebcSVladimir Oltean 	vlan = table->entries;
17586666cebcSVladimir Oltean 	vlan[match].vlanid = vid;
17596666cebcSVladimir Oltean 	if (enabled) {
17606666cebcSVladimir Oltean 		vlan[match].vlan_bc |= BIT(port);
17616666cebcSVladimir Oltean 		vlan[match].vmemb_port |= BIT(port);
17626666cebcSVladimir Oltean 	} else {
17636666cebcSVladimir Oltean 		vlan[match].vlan_bc &= ~BIT(port);
17646666cebcSVladimir Oltean 		vlan[match].vmemb_port &= ~BIT(port);
17656666cebcSVladimir Oltean 	}
17666666cebcSVladimir Oltean 	/* Also unset tag_port if removing this VLAN was requested,
17676666cebcSVladimir Oltean 	 * just so we don't have a confusing bitmap (no practical purpose).
17686666cebcSVladimir Oltean 	 */
17696666cebcSVladimir Oltean 	if (untagged || !enabled)
17706666cebcSVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
17716666cebcSVladimir Oltean 	else
17726666cebcSVladimir Oltean 		vlan[match].tag_port |= BIT(port);
17736666cebcSVladimir Oltean 	/* If there's no port left as member of this VLAN,
17746666cebcSVladimir Oltean 	 * it's time for it to go.
17756666cebcSVladimir Oltean 	 */
17766666cebcSVladimir Oltean 	if (!vlan[match].vmemb_port)
17776666cebcSVladimir Oltean 		keep = false;
17786666cebcSVladimir Oltean 
17796666cebcSVladimir Oltean 	dev_dbg(priv->ds->dev,
17806666cebcSVladimir Oltean 		"%s: port %d, vid %llu, broadcast domain 0x%llx, "
17816666cebcSVladimir Oltean 		"port members 0x%llx, tagged ports 0x%llx, keep %d\n",
17826666cebcSVladimir Oltean 		__func__, port, vlan[match].vlanid, vlan[match].vlan_bc,
17836666cebcSVladimir Oltean 		vlan[match].vmemb_port, vlan[match].tag_port, keep);
17846666cebcSVladimir Oltean 
17856666cebcSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
17866666cebcSVladimir Oltean 					  &vlan[match], keep);
17876666cebcSVladimir Oltean 	if (rc < 0)
17886666cebcSVladimir Oltean 		return rc;
17896666cebcSVladimir Oltean 
17906666cebcSVladimir Oltean 	if (!keep)
17916666cebcSVladimir Oltean 		return sja1105_table_delete_entry(table, match);
17926666cebcSVladimir Oltean 
17936666cebcSVladimir Oltean 	return 0;
17946666cebcSVladimir Oltean }
17956666cebcSVladimir Oltean 
1796ac02a451SVladimir Oltean static int sja1105_crosschip_bridge_join(struct dsa_switch *ds,
1797ac02a451SVladimir Oltean 					 int tree_index, int sw_index,
1798ac02a451SVladimir Oltean 					 int other_port, struct net_device *br)
1799ac02a451SVladimir Oltean {
1800ac02a451SVladimir Oltean 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1801ac02a451SVladimir Oltean 	struct sja1105_private *other_priv = other_ds->priv;
1802ac02a451SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1803ac02a451SVladimir Oltean 	int port, rc;
1804ac02a451SVladimir Oltean 
1805ac02a451SVladimir Oltean 	if (other_ds->ops != &sja1105_switch_ops)
1806ac02a451SVladimir Oltean 		return 0;
1807ac02a451SVladimir Oltean 
1808ac02a451SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1809ac02a451SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
1810ac02a451SVladimir Oltean 			continue;
1811ac02a451SVladimir Oltean 		if (dsa_to_port(ds, port)->bridge_dev != br)
1812ac02a451SVladimir Oltean 			continue;
1813ac02a451SVladimir Oltean 
1814*60b33aebSVladimir Oltean 		other_priv->expect_dsa_8021q = true;
1815ac02a451SVladimir Oltean 		rc = dsa_8021q_crosschip_bridge_join(ds, port, other_ds,
1816ac02a451SVladimir Oltean 						     other_port, br,
1817ac02a451SVladimir Oltean 						     &priv->crosschip_links);
1818*60b33aebSVladimir Oltean 		other_priv->expect_dsa_8021q = false;
1819ac02a451SVladimir Oltean 		if (rc)
1820ac02a451SVladimir Oltean 			return rc;
1821ac02a451SVladimir Oltean 
1822*60b33aebSVladimir Oltean 		priv->expect_dsa_8021q = true;
1823ac02a451SVladimir Oltean 		rc = dsa_8021q_crosschip_bridge_join(other_ds, other_port, ds,
1824ac02a451SVladimir Oltean 						     port, br,
1825ac02a451SVladimir Oltean 						     &other_priv->crosschip_links);
1826*60b33aebSVladimir Oltean 		priv->expect_dsa_8021q = false;
1827ac02a451SVladimir Oltean 		if (rc)
1828ac02a451SVladimir Oltean 			return rc;
1829ac02a451SVladimir Oltean 	}
1830ac02a451SVladimir Oltean 
1831ac02a451SVladimir Oltean 	return 0;
1832ac02a451SVladimir Oltean }
1833ac02a451SVladimir Oltean 
1834ac02a451SVladimir Oltean static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds,
1835ac02a451SVladimir Oltean 					   int tree_index, int sw_index,
1836ac02a451SVladimir Oltean 					   int other_port,
1837ac02a451SVladimir Oltean 					   struct net_device *br)
1838ac02a451SVladimir Oltean {
1839ac02a451SVladimir Oltean 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1840ac02a451SVladimir Oltean 	struct sja1105_private *other_priv = other_ds->priv;
1841ac02a451SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1842ac02a451SVladimir Oltean 	int port;
1843ac02a451SVladimir Oltean 
1844ac02a451SVladimir Oltean 	if (other_ds->ops != &sja1105_switch_ops)
1845ac02a451SVladimir Oltean 		return;
1846ac02a451SVladimir Oltean 
1847ac02a451SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1848ac02a451SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
1849ac02a451SVladimir Oltean 			continue;
1850ac02a451SVladimir Oltean 		if (dsa_to_port(ds, port)->bridge_dev != br)
1851ac02a451SVladimir Oltean 			continue;
1852ac02a451SVladimir Oltean 
1853*60b33aebSVladimir Oltean 		other_priv->expect_dsa_8021q = true;
1854ac02a451SVladimir Oltean 		dsa_8021q_crosschip_bridge_leave(ds, port, other_ds, other_port,
1855ac02a451SVladimir Oltean 						 br, &priv->crosschip_links);
1856*60b33aebSVladimir Oltean 		other_priv->expect_dsa_8021q = false;
1857ac02a451SVladimir Oltean 
1858*60b33aebSVladimir Oltean 		priv->expect_dsa_8021q = true;
1859ac02a451SVladimir Oltean 		dsa_8021q_crosschip_bridge_leave(other_ds, other_port, ds,
1860ac02a451SVladimir Oltean 						 port, br,
1861ac02a451SVladimir Oltean 						 &other_priv->crosschip_links);
1862*60b33aebSVladimir Oltean 		priv->expect_dsa_8021q = false;
1863ac02a451SVladimir Oltean 	}
1864ac02a451SVladimir Oltean }
1865ac02a451SVladimir Oltean 
1866ac02a451SVladimir Oltean static int sja1105_replay_crosschip_vlans(struct dsa_switch *ds, bool enabled)
1867ac02a451SVladimir Oltean {
1868ac02a451SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1869ac02a451SVladimir Oltean 	struct dsa_8021q_crosschip_link *c;
1870ac02a451SVladimir Oltean 	int rc;
1871ac02a451SVladimir Oltean 
1872ac02a451SVladimir Oltean 	list_for_each_entry(c, &priv->crosschip_links, list) {
1873*60b33aebSVladimir Oltean 		priv->expect_dsa_8021q = true;
1874ac02a451SVladimir Oltean 		rc = dsa_8021q_crosschip_link_apply(ds, c->port, c->other_ds,
1875ac02a451SVladimir Oltean 						    c->other_port, enabled);
1876*60b33aebSVladimir Oltean 		priv->expect_dsa_8021q = false;
1877ac02a451SVladimir Oltean 		if (rc)
1878ac02a451SVladimir Oltean 			break;
1879ac02a451SVladimir Oltean 	}
1880ac02a451SVladimir Oltean 
1881ac02a451SVladimir Oltean 	return rc;
1882ac02a451SVladimir Oltean }
1883ac02a451SVladimir Oltean 
1884227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1885227d07a0SVladimir Oltean {
1886*60b33aebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1887227d07a0SVladimir Oltean 	int rc, i;
1888227d07a0SVladimir Oltean 
1889227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1890*60b33aebSVladimir Oltean 		priv->expect_dsa_8021q = true;
1891227d07a0SVladimir Oltean 		rc = dsa_port_setup_8021q_tagging(ds, i, enabled);
1892*60b33aebSVladimir Oltean 		priv->expect_dsa_8021q = false;
1893227d07a0SVladimir Oltean 		if (rc < 0) {
1894227d07a0SVladimir Oltean 			dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n",
1895227d07a0SVladimir Oltean 				i, rc);
1896227d07a0SVladimir Oltean 			return rc;
1897227d07a0SVladimir Oltean 		}
1898227d07a0SVladimir Oltean 	}
1899ac02a451SVladimir Oltean 	rc = sja1105_replay_crosschip_vlans(ds, enabled);
1900ac02a451SVladimir Oltean 	if (rc) {
1901ac02a451SVladimir Oltean 		dev_err(ds->dev, "Failed to replay crosschip VLANs: %d\n", rc);
1902ac02a451SVladimir Oltean 		return rc;
1903ac02a451SVladimir Oltean 	}
1904ac02a451SVladimir Oltean 
1905227d07a0SVladimir Oltean 	dev_info(ds->dev, "%s switch tagging\n",
1906227d07a0SVladimir Oltean 		 enabled ? "Enabled" : "Disabled");
1907227d07a0SVladimir Oltean 	return 0;
1908227d07a0SVladimir Oltean }
1909227d07a0SVladimir Oltean 
19108aa9ebccSVladimir Oltean static enum dsa_tag_protocol
19114d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
19124d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
19138aa9ebccSVladimir Oltean {
1914227d07a0SVladimir Oltean 	return DSA_TAG_PROTO_SJA1105;
19158aa9ebccSVladimir Oltean }
19168aa9ebccSVladimir Oltean 
19176666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
19186666cebcSVladimir Oltean 				const struct switchdev_obj_port_vlan *vlan)
19196666cebcSVladimir Oltean {
1920*60b33aebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1921*60b33aebSVladimir Oltean 	u16 vid;
1922*60b33aebSVladimir Oltean 
1923*60b33aebSVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
1924*60b33aebSVladimir Oltean 		return 0;
1925*60b33aebSVladimir Oltean 
1926*60b33aebSVladimir Oltean 	/* If the user wants best-effort VLAN filtering (aka vlan_filtering
1927*60b33aebSVladimir Oltean 	 * bridge plus tagging), be sure to at least deny alterations to the
1928*60b33aebSVladimir Oltean 	 * configuration done by dsa_8021q.
1929*60b33aebSVladimir Oltean 	 */
1930*60b33aebSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1931*60b33aebSVladimir Oltean 		if (!priv->expect_dsa_8021q && vid_is_dsa_8021q(vid)) {
1932*60b33aebSVladimir Oltean 			dev_err(ds->dev, "Range 1024-3071 reserved for dsa_8021q operation\n");
1933*60b33aebSVladimir Oltean 			return -EBUSY;
1934*60b33aebSVladimir Oltean 		}
1935*60b33aebSVladimir Oltean 	}
1936*60b33aebSVladimir Oltean 
19376666cebcSVladimir Oltean 	return 0;
19386666cebcSVladimir Oltean }
19396666cebcSVladimir Oltean 
1940070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
1941070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
1942070ca3bbSVladimir Oltean  * So a switch reset is required.
1943070ca3bbSVladimir Oltean  */
19446666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
19456666cebcSVladimir Oltean {
19466d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
1947070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
19486666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
19497f14937fSVladimir Oltean 	enum sja1105_vlan_state state;
1950070ca3bbSVladimir Oltean 	struct sja1105_table *table;
1951dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
1952070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
19536666cebcSVladimir Oltean 	int rc;
19546666cebcSVladimir Oltean 
1955dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
1956dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
1957dfacc5a2SVladimir Oltean 			dev_err(ds->dev,
1958dfacc5a2SVladimir Oltean 				"Cannot change VLAN filtering state while VL rules are active\n");
1959dfacc5a2SVladimir Oltean 			return -EBUSY;
1960dfacc5a2SVladimir Oltean 		}
1961dfacc5a2SVladimir Oltean 	}
1962dfacc5a2SVladimir Oltean 
1963070ca3bbSVladimir Oltean 	if (enabled) {
19646666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
196554fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
196654fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
1967070ca3bbSVladimir Oltean 	} else {
19686666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
1969070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
1970070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
1971070ca3bbSVladimir Oltean 	}
1972070ca3bbSVladimir Oltean 
19737f14937fSVladimir Oltean 	if (!enabled)
19747f14937fSVladimir Oltean 		state = SJA1105_VLAN_UNAWARE;
19757f14937fSVladimir Oltean 	else
19767f14937fSVladimir Oltean 		state = SJA1105_VLAN_FILTERING_FULL;
19777f14937fSVladimir Oltean 
19787f14937fSVladimir Oltean 	priv->vlan_state = state;
19797f14937fSVladimir Oltean 
1980070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1981070ca3bbSVladimir Oltean 	general_params = table->entries;
1982f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
198354fa49eeSVladimir Oltean 	general_params->tpid = tpid;
198454fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
1985070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
198642824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
198742824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
198842824463SVladimir Oltean 	 */
198942824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
199042824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
1991070ca3bbSVladimir Oltean 
19926d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
19936d7c7d94SVladimir Oltean 	 * No VLAN filtering => shared VLAN learning.
19946d7c7d94SVladimir Oltean 	 *
19956d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
19966d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
19976d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
19986d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
19996d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
20006d7c7d94SVladimir Oltean 	 * forwarding decision.
20016d7c7d94SVladimir Oltean 	 *
20026d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
20036d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
20046d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
20056d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
20066d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
20076d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
20086d7c7d94SVladimir Oltean 	 * (all frames get flooded).
20096d7c7d94SVladimir Oltean 	 */
20106d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
20116d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
20126d7c7d94SVladimir Oltean 	l2_lookup_params->shared_learn = !enabled;
20136d7c7d94SVladimir Oltean 
20142eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
20156666cebcSVladimir Oltean 	if (rc)
20166666cebcSVladimir Oltean 		dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
20176666cebcSVladimir Oltean 
2018227d07a0SVladimir Oltean 	/* Switch port identification based on 802.1Q is only passable
2019227d07a0SVladimir Oltean 	 * if we are not under a vlan_filtering bridge. So make sure
2020227d07a0SVladimir Oltean 	 * the two configurations are mutually exclusive.
2021227d07a0SVladimir Oltean 	 */
2022227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, !enabled);
20236666cebcSVladimir Oltean }
20246666cebcSVladimir Oltean 
20256666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port,
20266666cebcSVladimir Oltean 			     const struct switchdev_obj_port_vlan *vlan)
20276666cebcSVladimir Oltean {
20286666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
20296666cebcSVladimir Oltean 	u16 vid;
20306666cebcSVladimir Oltean 	int rc;
20316666cebcSVladimir Oltean 
20326666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
20336666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags &
20346666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
20356666cebcSVladimir Oltean 		if (rc < 0) {
20366666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
20376666cebcSVladimir Oltean 				vid, port, rc);
20386666cebcSVladimir Oltean 			return;
20396666cebcSVladimir Oltean 		}
20406666cebcSVladimir Oltean 		if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
20416666cebcSVladimir Oltean 			rc = sja1105_pvid_apply(ds->priv, port, vid);
20426666cebcSVladimir Oltean 			if (rc < 0) {
20436666cebcSVladimir Oltean 				dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n",
20446666cebcSVladimir Oltean 					vid, port, rc);
20456666cebcSVladimir Oltean 				return;
20466666cebcSVladimir Oltean 			}
20476666cebcSVladimir Oltean 		}
20486666cebcSVladimir Oltean 	}
20496666cebcSVladimir Oltean }
20506666cebcSVladimir Oltean 
20516666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
20526666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
20536666cebcSVladimir Oltean {
20546666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
20556666cebcSVladimir Oltean 	u16 vid;
20566666cebcSVladimir Oltean 	int rc;
20576666cebcSVladimir Oltean 
20586666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
20596666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags &
20606666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
20616666cebcSVladimir Oltean 		if (rc < 0) {
20626666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
20636666cebcSVladimir Oltean 				vid, port, rc);
20646666cebcSVladimir Oltean 			return rc;
20656666cebcSVladimir Oltean 		}
20666666cebcSVladimir Oltean 	}
20676666cebcSVladimir Oltean 	return 0;
20686666cebcSVladimir Oltean }
20696666cebcSVladimir Oltean 
20708aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
20718aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
20728aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
20738aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
20748aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
20758aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
20768aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
20778aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
20788aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
20798aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
20808aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
20818aa9ebccSVladimir Oltean  */
20828aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
20838aa9ebccSVladimir Oltean {
20848aa9ebccSVladimir Oltean 	struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
20858aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
20868aa9ebccSVladimir Oltean 	int rc;
20878aa9ebccSVladimir Oltean 
20888aa9ebccSVladimir Oltean 	rc = sja1105_parse_dt(priv, ports);
20898aa9ebccSVladimir Oltean 	if (rc < 0) {
20908aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
20918aa9ebccSVladimir Oltean 		return rc;
20928aa9ebccSVladimir Oltean 	}
2093f5b8631cSVladimir Oltean 
2094f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
2095f5b8631cSVladimir Oltean 	 * and we can't apply them.
2096f5b8631cSVladimir Oltean 	 */
2097f5b8631cSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv, ports);
2098f5b8631cSVladimir Oltean 	if (rc < 0) {
2099f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
2100f5b8631cSVladimir Oltean 		return rc;
2101f5b8631cSVladimir Oltean 	}
2102f5b8631cSVladimir Oltean 
210361c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
2104bb77f36aSVladimir Oltean 	if (rc < 0) {
2105bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
2106bb77f36aSVladimir Oltean 		return rc;
2107bb77f36aSVladimir Oltean 	}
21088aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
21098aa9ebccSVladimir Oltean 	rc = sja1105_static_config_load(priv, ports);
21108aa9ebccSVladimir Oltean 	if (rc < 0) {
21118aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
21128aa9ebccSVladimir Oltean 		return rc;
21138aa9ebccSVladimir Oltean 	}
21148aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
21158aa9ebccSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
21168aa9ebccSVladimir Oltean 	if (rc < 0) {
21178aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
21188aa9ebccSVladimir Oltean 		return rc;
21198aa9ebccSVladimir Oltean 	}
21206666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
21216666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
21226666cebcSVladimir Oltean 	 * EtherType is.
21236666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
21246666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
21256666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
21266666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
21276666cebcSVladimir Oltean 	 */
21286666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
21298aa9ebccSVladimir Oltean 
21305f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
21315f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
21325f06c63bSVladimir Oltean 
2133c279c726SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
2134c279c726SVladimir Oltean 
2135227d07a0SVladimir Oltean 	/* The DSA/switchdev model brings up switch ports in standalone mode by
2136227d07a0SVladimir Oltean 	 * default, and that means vlan_filtering is 0 since they're not under
2137227d07a0SVladimir Oltean 	 * a bridge, so it's safe to set up switch tagging at this time.
2138227d07a0SVladimir Oltean 	 */
2139227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, true);
2140227d07a0SVladimir Oltean }
2141227d07a0SVladimir Oltean 
2142f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
2143f3097be2SVladimir Oltean {
2144f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2145a68578c2SVladimir Oltean 	int port;
2146a68578c2SVladimir Oltean 
2147a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2148a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2149a68578c2SVladimir Oltean 
2150a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2151a68578c2SVladimir Oltean 			continue;
2152a68578c2SVladimir Oltean 
215352c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
2154a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
2155a68578c2SVladimir Oltean 	}
2156f3097be2SVladimir Oltean 
2157a6af7763SVladimir Oltean 	sja1105_flower_teardown(ds);
2158317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
215961c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
21606cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2161f3097be2SVladimir Oltean }
2162f3097be2SVladimir Oltean 
2163e9bf9694SVladimir Oltean static int sja1105_port_enable(struct dsa_switch *ds, int port,
2164e9bf9694SVladimir Oltean 			       struct phy_device *phy)
2165e9bf9694SVladimir Oltean {
2166e9bf9694SVladimir Oltean 	struct net_device *slave;
2167e9bf9694SVladimir Oltean 
2168e9bf9694SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2169e9bf9694SVladimir Oltean 		return 0;
2170e9bf9694SVladimir Oltean 
217168bb8ea8SVivien Didelot 	slave = dsa_to_port(ds, port)->slave;
2172e9bf9694SVladimir Oltean 
2173e9bf9694SVladimir Oltean 	slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
2174e9bf9694SVladimir Oltean 
2175e9bf9694SVladimir Oltean 	return 0;
2176e9bf9694SVladimir Oltean }
2177e9bf9694SVladimir Oltean 
2178a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
2179a68578c2SVladimir Oltean {
2180a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2181a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
2182a68578c2SVladimir Oltean 
2183a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2184a68578c2SVladimir Oltean 		return;
2185a68578c2SVladimir Oltean 
2186a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
2187a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
2188a68578c2SVladimir Oltean }
2189a68578c2SVladimir Oltean 
2190227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
219147ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2192227d07a0SVladimir Oltean {
2193227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2194227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2195227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2196227d07a0SVladimir Oltean 	int timeout = 10;
2197227d07a0SVladimir Oltean 	int rc;
2198227d07a0SVladimir Oltean 
2199227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2200227d07a0SVladimir Oltean 
2201227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2202227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2203227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
220447ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
220547ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2206227d07a0SVladimir Oltean 
2207227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2208227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2209227d07a0SVladimir Oltean 	if (rc < 0) {
2210227d07a0SVladimir Oltean 		kfree_skb(skb);
2211227d07a0SVladimir Oltean 		return rc;
2212227d07a0SVladimir Oltean 	}
2213227d07a0SVladimir Oltean 
2214227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
221568bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2216227d07a0SVladimir Oltean 
2217227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2218227d07a0SVladimir Oltean 	do {
2219227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2220227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2221227d07a0SVladimir Oltean 		if (rc < 0) {
2222227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2223227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2224227d07a0SVladimir Oltean 			continue;
2225227d07a0SVladimir Oltean 		}
2226227d07a0SVladimir Oltean 
2227227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2228227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2229227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2230227d07a0SVladimir Oltean 		 */
2231227d07a0SVladimir Oltean 		cpu_relax();
2232227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2233227d07a0SVladimir Oltean 
2234227d07a0SVladimir Oltean 	if (!timeout) {
2235227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2236227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
22372a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
22382a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2239227d07a0SVladimir Oltean 		 */
2240227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2241227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2242227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2243227d07a0SVladimir Oltean 	}
2244227d07a0SVladimir Oltean 
2245227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2246227d07a0SVladimir Oltean }
2247227d07a0SVladimir Oltean 
2248a68578c2SVladimir Oltean #define work_to_port(work) \
2249a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
2250a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
2251a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
2252a68578c2SVladimir Oltean 
2253227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2254227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2255227d07a0SVladimir Oltean  * lock on the bus)
2256227d07a0SVladimir Oltean  */
2257a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2258227d07a0SVladimir Oltean {
2259a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
2260a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
2261a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
2262a68578c2SVladimir Oltean 	int port = sp - priv->ports;
2263a68578c2SVladimir Oltean 	struct sk_buff *skb;
2264a68578c2SVladimir Oltean 
2265a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
2266a68578c2SVladimir Oltean 		struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
2267227d07a0SVladimir Oltean 
2268227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
2269227d07a0SVladimir Oltean 
2270a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
2271a68578c2SVladimir Oltean 
227247ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
2273a68578c2SVladimir Oltean 		if (clone)
2274a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
2275227d07a0SVladimir Oltean 
2276227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
2277a68578c2SVladimir Oltean 	}
22788aa9ebccSVladimir Oltean }
22798aa9ebccSVladimir Oltean 
22808456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
22818456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
22828456721dSVladimir Oltean  */
22838456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
22848456721dSVladimir Oltean 				   unsigned int ageing_time)
22858456721dSVladimir Oltean {
22868456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
22878456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
22888456721dSVladimir Oltean 	struct sja1105_table *table;
22898456721dSVladimir Oltean 	unsigned int maxage;
22908456721dSVladimir Oltean 
22918456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
22928456721dSVladimir Oltean 	l2_lookup_params = table->entries;
22938456721dSVladimir Oltean 
22948456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
22958456721dSVladimir Oltean 
22968456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
22978456721dSVladimir Oltean 		return 0;
22988456721dSVladimir Oltean 
22998456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
23008456721dSVladimir Oltean 
23012eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
23028456721dSVladimir Oltean }
23038456721dSVladimir Oltean 
2304c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
2305c279c726SVladimir Oltean {
2306c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2307c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2308c279c726SVladimir Oltean 
2309c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
2310c279c726SVladimir Oltean 
2311c279c726SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
2312c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
2313c279c726SVladimir Oltean 
2314c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2315c279c726SVladimir Oltean 
2316a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
2317c279c726SVladimir Oltean 		return 0;
2318c279c726SVladimir Oltean 
2319a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
2320c279c726SVladimir Oltean 
2321c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2322c279c726SVladimir Oltean }
2323c279c726SVladimir Oltean 
2324c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
2325c279c726SVladimir Oltean {
2326c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
2327c279c726SVladimir Oltean }
2328c279c726SVladimir Oltean 
2329317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2330317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2331317ab5b8SVladimir Oltean 				 void *type_data)
2332317ab5b8SVladimir Oltean {
2333317ab5b8SVladimir Oltean 	switch (type) {
2334317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2335317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
2336317ab5b8SVladimir Oltean 	default:
2337317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2338317ab5b8SVladimir Oltean 	}
2339317ab5b8SVladimir Oltean }
2340317ab5b8SVladimir Oltean 
2341511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2342511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2343511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2344511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2345511e6ca0SVladimir Oltean  * mirroring rule that references it.
2346511e6ca0SVladimir Oltean  */
2347511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2348511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2349511e6ca0SVladimir Oltean {
2350511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2351511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2352511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2353511e6ca0SVladimir Oltean 	bool already_enabled;
2354511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2355511e6ca0SVladimir Oltean 	int rc;
2356511e6ca0SVladimir Oltean 
2357511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2358511e6ca0SVladimir Oltean 	general_params = table->entries;
2359511e6ca0SVladimir Oltean 
2360511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2361511e6ca0SVladimir Oltean 
2362511e6ca0SVladimir Oltean 	already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS);
2363511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2364511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2365511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2366511e6ca0SVladimir Oltean 			general_params->mirr_port);
2367511e6ca0SVladimir Oltean 		return -EBUSY;
2368511e6ca0SVladimir Oltean 	}
2369511e6ca0SVladimir Oltean 
2370511e6ca0SVladimir Oltean 	new_mirr_port = to;
2371511e6ca0SVladimir Oltean 	if (!enabled) {
2372511e6ca0SVladimir Oltean 		bool keep = false;
2373511e6ca0SVladimir Oltean 		int port;
2374511e6ca0SVladimir Oltean 
2375511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2376511e6ca0SVladimir Oltean 		for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2377511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2378511e6ca0SVladimir Oltean 				keep = true;
2379511e6ca0SVladimir Oltean 				break;
2380511e6ca0SVladimir Oltean 			}
2381511e6ca0SVladimir Oltean 		}
2382511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2383511e6ca0SVladimir Oltean 		if (!keep)
2384511e6ca0SVladimir Oltean 			new_mirr_port = SJA1105_NUM_PORTS;
2385511e6ca0SVladimir Oltean 	}
2386511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2387511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2388511e6ca0SVladimir Oltean 
2389511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2390511e6ca0SVladimir Oltean 						  0, general_params, true);
2391511e6ca0SVladimir Oltean 		if (rc < 0)
2392511e6ca0SVladimir Oltean 			return rc;
2393511e6ca0SVladimir Oltean 	}
2394511e6ca0SVladimir Oltean 
2395511e6ca0SVladimir Oltean 	if (ingress)
2396511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2397511e6ca0SVladimir Oltean 	else
2398511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2399511e6ca0SVladimir Oltean 
2400511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2401511e6ca0SVladimir Oltean 					    &mac[from], true);
2402511e6ca0SVladimir Oltean }
2403511e6ca0SVladimir Oltean 
2404511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2405511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2406511e6ca0SVladimir Oltean 			      bool ingress)
2407511e6ca0SVladimir Oltean {
2408511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2409511e6ca0SVladimir Oltean 				    ingress, true);
2410511e6ca0SVladimir Oltean }
2411511e6ca0SVladimir Oltean 
2412511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2413511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2414511e6ca0SVladimir Oltean {
2415511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2416511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2417511e6ca0SVladimir Oltean }
2418511e6ca0SVladimir Oltean 
2419a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
2420a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
2421a7cc081cSVladimir Oltean {
2422a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2423a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2424a7cc081cSVladimir Oltean 
2425a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2426a7cc081cSVladimir Oltean 
2427a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
2428a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
2429a7cc081cSVladimir Oltean 	 * bytes.
2430a7cc081cSVladimir Oltean 	 */
2431a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
2432a7cc081cSVladimir Oltean 				      1000000);
2433a7cc081cSVladimir Oltean 	policing[port].smax = div_u64(policer->rate_bytes_per_sec *
2434a7cc081cSVladimir Oltean 				      PSCHED_NS2TICKS(policer->burst),
2435a7cc081cSVladimir Oltean 				      PSCHED_TICKS_PER_SEC);
2436a7cc081cSVladimir Oltean 
2437a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2438a7cc081cSVladimir Oltean }
2439a7cc081cSVladimir Oltean 
2440a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
2441a7cc081cSVladimir Oltean {
2442a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2443a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2444a7cc081cSVladimir Oltean 
2445a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2446a7cc081cSVladimir Oltean 
2447a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
2448a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
2449a7cc081cSVladimir Oltean 
2450a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2451a7cc081cSVladimir Oltean }
2452a7cc081cSVladimir Oltean 
24538aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
24548aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
24558aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
2456f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
24578456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
2458c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
2459c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
2460ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
2461ffe10e67SVladimir Oltean 	.phylink_mac_link_state	= sja1105_mac_pcs_get_state,
2462af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
24638400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
24648400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
246552c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
246652c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
246752c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
2468bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
2469e9bf9694SVladimir Oltean 	.port_enable		= sja1105_port_enable,
2470a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
2471291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
2472291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
2473291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
24748aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
24758aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
2476640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
24776666cebcSVladimir Oltean 	.port_vlan_prepare	= sja1105_vlan_prepare,
24786666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
24796666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
24806666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
2481291d1e72SVladimir Oltean 	.port_mdb_prepare	= sja1105_mdb_prepare,
2482291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
2483291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
2484a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
2485a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
2486f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
248747ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
2488317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
2489511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
2490511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
2491a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
2492a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
2493a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
2494a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
2495834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
2496ac02a451SVladimir Oltean 	.crosschip_bridge_join	= sja1105_crosschip_bridge_join,
2497ac02a451SVladimir Oltean 	.crosschip_bridge_leave	= sja1105_crosschip_bridge_leave,
24988aa9ebccSVladimir Oltean };
24998aa9ebccSVladimir Oltean 
25008aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
25018aa9ebccSVladimir Oltean {
25028aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
25038aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
25048aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
2505dff79620SVladimir Oltean 	u32 device_id;
25068aa9ebccSVladimir Oltean 	u64 part_no;
25078aa9ebccSVladimir Oltean 	int rc;
25088aa9ebccSVladimir Oltean 
250934d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
251034d76e9fSVladimir Oltean 			      NULL);
25118aa9ebccSVladimir Oltean 	if (rc < 0)
25128aa9ebccSVladimir Oltean 		return rc;
25138aa9ebccSVladimir Oltean 
25148aa9ebccSVladimir Oltean 	if (device_id != priv->info->device_id) {
2515dff79620SVladimir Oltean 		dev_err(dev, "Expected device ID 0x%llx but read 0x%x\n",
25168aa9ebccSVladimir Oltean 			priv->info->device_id, device_id);
25178aa9ebccSVladimir Oltean 		return -ENODEV;
25188aa9ebccSVladimir Oltean 	}
25198aa9ebccSVladimir Oltean 
25201bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
25211bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
25228aa9ebccSVladimir Oltean 	if (rc < 0)
25238aa9ebccSVladimir Oltean 		return rc;
25248aa9ebccSVladimir Oltean 
25258aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
25268aa9ebccSVladimir Oltean 
25278aa9ebccSVladimir Oltean 	if (part_no != priv->info->part_no) {
25288aa9ebccSVladimir Oltean 		dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n",
25298aa9ebccSVladimir Oltean 			priv->info->part_no, part_no);
25308aa9ebccSVladimir Oltean 		return -ENODEV;
25318aa9ebccSVladimir Oltean 	}
25328aa9ebccSVladimir Oltean 
25338aa9ebccSVladimir Oltean 	return 0;
25348aa9ebccSVladimir Oltean }
25358aa9ebccSVladimir Oltean 
25368aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
25378aa9ebccSVladimir Oltean {
2538844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
25398aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
25408aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
25418aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
2542a68578c2SVladimir Oltean 	int rc, port;
25438aa9ebccSVladimir Oltean 
25448aa9ebccSVladimir Oltean 	if (!dev->of_node) {
25458aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
25468aa9ebccSVladimir Oltean 		return -EINVAL;
25478aa9ebccSVladimir Oltean 	}
25488aa9ebccSVladimir Oltean 
25498aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
25508aa9ebccSVladimir Oltean 	if (!priv)
25518aa9ebccSVladimir Oltean 		return -ENOMEM;
25528aa9ebccSVladimir Oltean 
25538aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
25548aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
25558aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
25568aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
25578aa9ebccSVladimir Oltean 	else
25588aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
25598aa9ebccSVladimir Oltean 
25608aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
25618aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
25628aa9ebccSVladimir Oltean 	 */
25638aa9ebccSVladimir Oltean 	priv->spidev = spi;
25648aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
25658aa9ebccSVladimir Oltean 
25668aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
25678aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
25688aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
25698aa9ebccSVladimir Oltean 	if (rc < 0) {
25708aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
25718aa9ebccSVladimir Oltean 		return rc;
25728aa9ebccSVladimir Oltean 	}
25738aa9ebccSVladimir Oltean 
25748aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
25758aa9ebccSVladimir Oltean 
25768aa9ebccSVladimir Oltean 	/* Detect hardware device */
25778aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
25788aa9ebccSVladimir Oltean 	if (rc < 0) {
25798aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
25808aa9ebccSVladimir Oltean 		return rc;
25818aa9ebccSVladimir Oltean 	}
25828aa9ebccSVladimir Oltean 
25838aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
25848aa9ebccSVladimir Oltean 
25857e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
25868aa9ebccSVladimir Oltean 	if (!ds)
25878aa9ebccSVladimir Oltean 		return -ENOMEM;
25888aa9ebccSVladimir Oltean 
25897e99e347SVivien Didelot 	ds->dev = dev;
25907e99e347SVivien Didelot 	ds->num_ports = SJA1105_NUM_PORTS;
25918aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
25928aa9ebccSVladimir Oltean 	ds->priv = priv;
25938aa9ebccSVladimir Oltean 	priv->ds = ds;
25948aa9ebccSVladimir Oltean 
2595844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
2596844d7edcSVladimir Oltean 
2597d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
2598d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
2599d5a619bfSVivien Didelot 
2600ac02a451SVladimir Oltean 	INIT_LIST_HEAD(&priv->crosschip_links);
2601ac02a451SVladimir Oltean 
2602d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
2603a6af7763SVladimir Oltean 	sja1105_flower_setup(ds);
2604d5a619bfSVivien Didelot 
2605d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
2606d5a619bfSVivien Didelot 	if (rc)
2607d5a619bfSVivien Didelot 		return rc;
2608d5a619bfSVivien Didelot 
2609227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
2610a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2611a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2612a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
2613a68578c2SVladimir Oltean 		struct net_device *slave;
2614227d07a0SVladimir Oltean 
2615a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2616a68578c2SVladimir Oltean 			continue;
2617a68578c2SVladimir Oltean 
2618a68578c2SVladimir Oltean 		dp->priv = sp;
2619a68578c2SVladimir Oltean 		sp->dp = dp;
2620844d7edcSVladimir Oltean 		sp->data = tagger_data;
2621a68578c2SVladimir Oltean 		slave = dp->slave;
2622a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
2623a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
2624a68578c2SVladimir Oltean 							slave->name);
2625a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
2626a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
2627a68578c2SVladimir Oltean 			dev_err(ds->dev,
2628a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
2629a68578c2SVladimir Oltean 				rc);
2630a68578c2SVladimir Oltean 			goto out;
2631a68578c2SVladimir Oltean 		}
2632a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
2633227d07a0SVladimir Oltean 	}
2634227d07a0SVladimir Oltean 
2635d5a619bfSVivien Didelot 	return 0;
2636a68578c2SVladimir Oltean out:
2637a68578c2SVladimir Oltean 	while (port-- > 0) {
2638a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2639a68578c2SVladimir Oltean 
2640a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2641a68578c2SVladimir Oltean 			continue;
2642a68578c2SVladimir Oltean 
2643a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
2644a68578c2SVladimir Oltean 	}
2645a68578c2SVladimir Oltean 	return rc;
26468aa9ebccSVladimir Oltean }
26478aa9ebccSVladimir Oltean 
26488aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
26498aa9ebccSVladimir Oltean {
26508aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
26518aa9ebccSVladimir Oltean 
26528aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
26538aa9ebccSVladimir Oltean 	return 0;
26548aa9ebccSVladimir Oltean }
26558aa9ebccSVladimir Oltean 
26568aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
26578aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
26588aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
26598aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
26608aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
26618aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
26628aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
26638aa9ebccSVladimir Oltean 	{ /* sentinel */ },
26648aa9ebccSVladimir Oltean };
26658aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
26668aa9ebccSVladimir Oltean 
26678aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
26688aa9ebccSVladimir Oltean 	.driver = {
26698aa9ebccSVladimir Oltean 		.name  = "sja1105",
26708aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
26718aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
26728aa9ebccSVladimir Oltean 	},
26738aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
26748aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
26758aa9ebccSVladimir Oltean };
26768aa9ebccSVladimir Oltean 
26778aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
26788aa9ebccSVladimir Oltean 
26798aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
26808aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
26818aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
26828aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
2683