xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision a7cc081cabdae395b1d2ae581a6fee61aec72317)
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 
288aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
298aa9ebccSVladimir Oltean 			     unsigned int startup_delay)
308aa9ebccSVladimir Oltean {
318aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
328aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
338aa9ebccSVladimir Oltean 	msleep(pulse_len);
348aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
358aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
368aa9ebccSVladimir Oltean 	msleep(startup_delay);
378aa9ebccSVladimir Oltean }
388aa9ebccSVladimir Oltean 
398aa9ebccSVladimir Oltean static void
408aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
418aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
428aa9ebccSVladimir Oltean {
438aa9ebccSVladimir Oltean 	if (allow) {
448aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  |= BIT(to);
458aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
468aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  |= BIT(to);
478aa9ebccSVladimir Oltean 	} else {
488aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  &= ~BIT(to);
498aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
508aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  &= ~BIT(to);
518aa9ebccSVladimir Oltean 	}
528aa9ebccSVladimir Oltean }
538aa9ebccSVladimir Oltean 
548aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree
558aa9ebccSVladimir Oltean  * settings into sja1105_setup
568aa9ebccSVladimir Oltean  */
578aa9ebccSVladimir Oltean struct sja1105_dt_port {
588aa9ebccSVladimir Oltean 	phy_interface_t phy_mode;
598aa9ebccSVladimir Oltean 	sja1105_mii_role_t role;
608aa9ebccSVladimir Oltean };
618aa9ebccSVladimir Oltean 
628aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
638aa9ebccSVladimir Oltean {
648aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
658aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
668aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
678aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
688aa9ebccSVladimir Oltean 		 */
698aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
708aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
718aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
728aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
738aa9ebccSVladimir Oltean 		.ifg = 0,
748aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
751fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
768aa9ebccSVladimir Oltean 		 */
778aa9ebccSVladimir Oltean 		.speed = SJA1105_SPEED_AUTO,
788aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
798aa9ebccSVladimir Oltean 		.tp_delin = 0,
808aa9ebccSVladimir Oltean 		.tp_delout = 0,
818aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
828aa9ebccSVladimir Oltean 		.maxage = 0xFF,
838aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
848aa9ebccSVladimir Oltean 		.vlanprio = 0,
85e3502b82SVladimir Oltean 		.vlanid = 1,
868aa9ebccSVladimir Oltean 		.ing_mirr = false,
878aa9ebccSVladimir Oltean 		.egr_mirr = false,
888aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
898aa9ebccSVladimir Oltean 		.drpnona664 = false,
908aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
918aa9ebccSVladimir Oltean 		.drpdtag = false,
928aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
938aa9ebccSVladimir Oltean 		.drpuntag = false,
948aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
958aa9ebccSVladimir Oltean 		.retag = false,
96640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
97640f763fSVladimir Oltean 		 * STP will enable it.
98640f763fSVladimir Oltean 		 */
99640f763fSVladimir Oltean 		.dyn_learn = false,
1008aa9ebccSVladimir Oltean 		.egress = false,
1018aa9ebccSVladimir Oltean 		.ingress = false,
1028aa9ebccSVladimir Oltean 	};
1038aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1048aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1058aa9ebccSVladimir Oltean 	int i;
1068aa9ebccSVladimir Oltean 
1078aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1088aa9ebccSVladimir Oltean 
1098aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1108aa9ebccSVladimir Oltean 	if (table->entry_count) {
1118aa9ebccSVladimir Oltean 		kfree(table->entries);
1128aa9ebccSVladimir Oltean 		table->entry_count = 0;
1138aa9ebccSVladimir Oltean 	}
1148aa9ebccSVladimir Oltean 
1158aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_NUM_PORTS,
1168aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1178aa9ebccSVladimir Oltean 	if (!table->entries)
1188aa9ebccSVladimir Oltean 		return -ENOMEM;
1198aa9ebccSVladimir Oltean 
1208aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_NUM_PORTS;
1218aa9ebccSVladimir Oltean 
1228aa9ebccSVladimir Oltean 	mac = table->entries;
1238aa9ebccSVladimir Oltean 
124640f763fSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1258aa9ebccSVladimir Oltean 		mac[i] = default_mac;
126640f763fSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, i)) {
127640f763fSVladimir Oltean 			/* STP doesn't get called for CPU port, so we need to
128640f763fSVladimir Oltean 			 * set the I/O parameters statically.
129640f763fSVladimir Oltean 			 */
130640f763fSVladimir Oltean 			mac[i].dyn_learn = true;
131640f763fSVladimir Oltean 			mac[i].ingress = true;
132640f763fSVladimir Oltean 			mac[i].egress = true;
133640f763fSVladimir Oltean 		}
134640f763fSVladimir Oltean 	}
1358aa9ebccSVladimir Oltean 
1368aa9ebccSVladimir Oltean 	return 0;
1378aa9ebccSVladimir Oltean }
1388aa9ebccSVladimir Oltean 
139ffe10e67SVladimir Oltean static bool sja1105_supports_sgmii(struct sja1105_private *priv, int port)
140ffe10e67SVladimir Oltean {
141ffe10e67SVladimir Oltean 	if (priv->info->part_no != SJA1105R_PART_NO &&
142ffe10e67SVladimir Oltean 	    priv->info->part_no != SJA1105S_PART_NO)
143ffe10e67SVladimir Oltean 		return false;
144ffe10e67SVladimir Oltean 
145ffe10e67SVladimir Oltean 	if (port != SJA1105_SGMII_PORT)
146ffe10e67SVladimir Oltean 		return false;
147ffe10e67SVladimir Oltean 
148ffe10e67SVladimir Oltean 	if (dsa_is_unused_port(priv->ds, port))
149ffe10e67SVladimir Oltean 		return false;
150ffe10e67SVladimir Oltean 
151ffe10e67SVladimir Oltean 	return true;
152ffe10e67SVladimir Oltean }
153ffe10e67SVladimir Oltean 
1548aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv,
1558aa9ebccSVladimir Oltean 				     struct sja1105_dt_port *ports)
1568aa9ebccSVladimir Oltean {
1578aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1588aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1598aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1608aa9ebccSVladimir Oltean 	int i;
1618aa9ebccSVladimir Oltean 
1628aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1638aa9ebccSVladimir Oltean 
1648aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1658aa9ebccSVladimir Oltean 	if (table->entry_count) {
1668aa9ebccSVladimir Oltean 		kfree(table->entries);
1678aa9ebccSVladimir Oltean 		table->entry_count = 0;
1688aa9ebccSVladimir Oltean 	}
1698aa9ebccSVladimir Oltean 
1708aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
1718aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1728aa9ebccSVladimir Oltean 	if (!table->entries)
1738aa9ebccSVladimir Oltean 		return -ENOMEM;
1748aa9ebccSVladimir Oltean 
1751fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
1768aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
1778aa9ebccSVladimir Oltean 
1788aa9ebccSVladimir Oltean 	mii = table->entries;
1798aa9ebccSVladimir Oltean 
1808aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
181ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
182ee9d0cb6SVladimir Oltean 			continue;
183ee9d0cb6SVladimir Oltean 
1848aa9ebccSVladimir Oltean 		switch (ports[i].phy_mode) {
1858aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
1868aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1878aa9ebccSVladimir Oltean 			break;
1888aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
1898aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
1908aa9ebccSVladimir Oltean 			break;
1918aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
1928aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
1938aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
1948aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
1958aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
1968aa9ebccSVladimir Oltean 			break;
197ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
198ffe10e67SVladimir Oltean 			if (!sja1105_supports_sgmii(priv, i))
199ffe10e67SVladimir Oltean 				return -EINVAL;
200ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
201ffe10e67SVladimir Oltean 			break;
2028aa9ebccSVladimir Oltean 		default:
2038aa9ebccSVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s!\n",
2048aa9ebccSVladimir Oltean 				phy_modes(ports[i].phy_mode));
2058aa9ebccSVladimir Oltean 		}
2068aa9ebccSVladimir Oltean 
207ffe10e67SVladimir Oltean 		/* Even though the SerDes port is able to drive SGMII autoneg
208ffe10e67SVladimir Oltean 		 * like a PHY would, from the perspective of the XMII tables,
209ffe10e67SVladimir Oltean 		 * the SGMII port should always be put in MAC mode.
210ffe10e67SVladimir Oltean 		 */
211ffe10e67SVladimir Oltean 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII)
212ffe10e67SVladimir Oltean 			mii->phy_mac[i] = XMII_MAC;
213ffe10e67SVladimir Oltean 		else
2148aa9ebccSVladimir Oltean 			mii->phy_mac[i] = ports[i].role;
2158aa9ebccSVladimir Oltean 	}
2168aa9ebccSVladimir Oltean 	return 0;
2178aa9ebccSVladimir Oltean }
2188aa9ebccSVladimir Oltean 
2198aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
2208aa9ebccSVladimir Oltean {
2218aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2228aa9ebccSVladimir Oltean 
2238aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
2248aa9ebccSVladimir Oltean 
225291d1e72SVladimir Oltean 	/* We only populate the FDB table through dynamic
226291d1e72SVladimir Oltean 	 * L2 Address Lookup entries
227291d1e72SVladimir Oltean 	 */
2288aa9ebccSVladimir Oltean 	if (table->entry_count) {
2298aa9ebccSVladimir Oltean 		kfree(table->entries);
2308aa9ebccSVladimir Oltean 		table->entry_count = 0;
2318aa9ebccSVladimir Oltean 	}
2328aa9ebccSVladimir Oltean 	return 0;
2338aa9ebccSVladimir Oltean }
2348aa9ebccSVladimir Oltean 
2358aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
2368aa9ebccSVladimir Oltean {
2378aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2386c56e167SVladimir Oltean 	u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
2398aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
2408456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
2418456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
2428aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
2438aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
2441da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
2451da73821SVladimir Oltean 		.start_dynspc = 0,
2466c56e167SVladimir Oltean 		.maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
2476c56e167SVladimir Oltean 			     max_fdb_entries, max_fdb_entries, },
2488aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
2498aa9ebccSVladimir Oltean 		.poly = 0x97,
2508aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
2518aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
2528aa9ebccSVladimir Oltean 		 */
2536d7c7d94SVladimir Oltean 		.shared_learn = true,
2548aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
2558aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
2568aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
2578aa9ebccSVladimir Oltean 		 */
2588aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
2598aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
2608aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
2618aa9ebccSVladimir Oltean 		 */
2628aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
2631da73821SVladimir Oltean 		/* P/Q/R/S only */
2641da73821SVladimir Oltean 		.use_static = true,
2651da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
2661da73821SVladimir Oltean 		 * dynamic FDB entries
2671da73821SVladimir Oltean 		 */
2681da73821SVladimir Oltean 		.owr_dyn = true,
2691da73821SVladimir Oltean 		.drpnolearn = true,
2708aa9ebccSVladimir Oltean 	};
2718aa9ebccSVladimir Oltean 
2728aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2738aa9ebccSVladimir Oltean 
2748aa9ebccSVladimir Oltean 	if (table->entry_count) {
2758aa9ebccSVladimir Oltean 		kfree(table->entries);
2768aa9ebccSVladimir Oltean 		table->entry_count = 0;
2778aa9ebccSVladimir Oltean 	}
2788aa9ebccSVladimir Oltean 
2798aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
2808aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2818aa9ebccSVladimir Oltean 	if (!table->entries)
2828aa9ebccSVladimir Oltean 		return -ENOMEM;
2838aa9ebccSVladimir Oltean 
2848aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
2858aa9ebccSVladimir Oltean 
2868aa9ebccSVladimir Oltean 	/* This table only has a single entry */
2878aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
2888aa9ebccSVladimir Oltean 				default_l2_lookup_params;
2898aa9ebccSVladimir Oltean 
2908aa9ebccSVladimir Oltean 	return 0;
2918aa9ebccSVladimir Oltean }
2928aa9ebccSVladimir Oltean 
2938aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
2948aa9ebccSVladimir Oltean {
2958aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2968aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
2978aa9ebccSVladimir Oltean 		.ving_mirr = 0,
2988aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
2998aa9ebccSVladimir Oltean 		.vmemb_port = 0,
3008aa9ebccSVladimir Oltean 		.vlan_bc = 0,
3018aa9ebccSVladimir Oltean 		.tag_port = 0,
302e3502b82SVladimir Oltean 		.vlanid = 1,
3038aa9ebccSVladimir Oltean 	};
3048aa9ebccSVladimir Oltean 	int i;
3058aa9ebccSVladimir Oltean 
3068aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
3078aa9ebccSVladimir Oltean 
308e3502b82SVladimir Oltean 	/* The static VLAN table will only contain the initial pvid of 1.
3096666cebcSVladimir Oltean 	 * All other VLANs are to be configured through dynamic entries,
3106666cebcSVladimir Oltean 	 * and kept in the static configuration table as backing memory.
3118aa9ebccSVladimir Oltean 	 */
3128aa9ebccSVladimir Oltean 	if (table->entry_count) {
3138aa9ebccSVladimir Oltean 		kfree(table->entries);
3148aa9ebccSVladimir Oltean 		table->entry_count = 0;
3158aa9ebccSVladimir Oltean 	}
3168aa9ebccSVladimir Oltean 
3178aa9ebccSVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
3188aa9ebccSVladimir Oltean 				 GFP_KERNEL);
3198aa9ebccSVladimir Oltean 	if (!table->entries)
3208aa9ebccSVladimir Oltean 		return -ENOMEM;
3218aa9ebccSVladimir Oltean 
3228aa9ebccSVladimir Oltean 	table->entry_count = 1;
3238aa9ebccSVladimir Oltean 
324e3502b82SVladimir Oltean 	/* VLAN 1: all DT-defined ports are members; no restrictions on
3258aa9ebccSVladimir Oltean 	 * forwarding; always transmit priority-tagged frames as untagged.
3268aa9ebccSVladimir Oltean 	 */
3278aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3288aa9ebccSVladimir Oltean 		pvid.vmemb_port |= BIT(i);
3298aa9ebccSVladimir Oltean 		pvid.vlan_bc |= BIT(i);
3308aa9ebccSVladimir Oltean 		pvid.tag_port &= ~BIT(i);
3318aa9ebccSVladimir Oltean 	}
3328aa9ebccSVladimir Oltean 
3338aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
3348aa9ebccSVladimir Oltean 	return 0;
3358aa9ebccSVladimir Oltean }
3368aa9ebccSVladimir Oltean 
3378aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
3388aa9ebccSVladimir Oltean {
3398aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
3408aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3418aa9ebccSVladimir Oltean 	int i, j;
3428aa9ebccSVladimir Oltean 
3438aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
3448aa9ebccSVladimir Oltean 
3458aa9ebccSVladimir Oltean 	if (table->entry_count) {
3468aa9ebccSVladimir Oltean 		kfree(table->entries);
3478aa9ebccSVladimir Oltean 		table->entry_count = 0;
3488aa9ebccSVladimir Oltean 	}
3498aa9ebccSVladimir Oltean 
3508aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
3518aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3528aa9ebccSVladimir Oltean 	if (!table->entries)
3538aa9ebccSVladimir Oltean 		return -ENOMEM;
3548aa9ebccSVladimir Oltean 
3558aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
3568aa9ebccSVladimir Oltean 
3578aa9ebccSVladimir Oltean 	l2fwd = table->entries;
3588aa9ebccSVladimir Oltean 
3598aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
3608aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3618aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
3628aa9ebccSVladimir Oltean 
3638aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
3648aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
3658aa9ebccSVladimir Oltean 
3668aa9ebccSVladimir Oltean 		if (i == upstream)
3678aa9ebccSVladimir Oltean 			continue;
3688aa9ebccSVladimir Oltean 
3698aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
3708aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
3718aa9ebccSVladimir Oltean 	}
3728aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
3738aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
3748aa9ebccSVladimir Oltean 	 */
3758aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++)
3768aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_PORTS; j++)
3778aa9ebccSVladimir Oltean 			l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
3788aa9ebccSVladimir Oltean 
3798aa9ebccSVladimir Oltean 	return 0;
3808aa9ebccSVladimir Oltean }
3818aa9ebccSVladimir Oltean 
3828aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
3838aa9ebccSVladimir Oltean {
3848aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
3858aa9ebccSVladimir Oltean 		/* Disallow dynamic reconfiguration of vlan_pmap */
3868aa9ebccSVladimir Oltean 		.max_dynp = 0,
3878aa9ebccSVladimir Oltean 		/* Use a single memory partition for all ingress queues */
3888aa9ebccSVladimir Oltean 		.part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
3898aa9ebccSVladimir Oltean 	};
3908aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3918aa9ebccSVladimir Oltean 
3928aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
3938aa9ebccSVladimir Oltean 
3948aa9ebccSVladimir Oltean 	if (table->entry_count) {
3958aa9ebccSVladimir Oltean 		kfree(table->entries);
3968aa9ebccSVladimir Oltean 		table->entry_count = 0;
3978aa9ebccSVladimir Oltean 	}
3988aa9ebccSVladimir Oltean 
3998aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
4008aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4018aa9ebccSVladimir Oltean 	if (!table->entries)
4028aa9ebccSVladimir Oltean 		return -ENOMEM;
4038aa9ebccSVladimir Oltean 
4048aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
4058aa9ebccSVladimir Oltean 
4068aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4078aa9ebccSVladimir Oltean 	((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
4088aa9ebccSVladimir Oltean 				default_l2fwd_params;
4098aa9ebccSVladimir Oltean 
4108aa9ebccSVladimir Oltean 	return 0;
4118aa9ebccSVladimir Oltean }
4128aa9ebccSVladimir Oltean 
4138aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
4148aa9ebccSVladimir Oltean {
4158aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
416511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
417511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
4188aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
4195f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
4205f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
4215f06c63bSVladimir Oltean 		 */
42208fde09aSVladimir Oltean 		.hostprio = 7,
4238aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
4248aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
42542824463SVladimir Oltean 		.incl_srcpt1 = false,
4268aa9ebccSVladimir Oltean 		.send_meta1  = false,
4278aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
4288aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
42942824463SVladimir Oltean 		.incl_srcpt0 = false,
4308aa9ebccSVladimir Oltean 		.send_meta0  = false,
4318aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
4328aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
4338aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
4348aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
4358aa9ebccSVladimir Oltean 		 */
4368aa9ebccSVladimir Oltean 		.host_port = dsa_upstream_port(priv->ds, 0),
437511e6ca0SVladimir Oltean 		/* Default to an invalid value */
438511e6ca0SVladimir Oltean 		.mirr_port = SJA1105_NUM_PORTS,
4398aa9ebccSVladimir Oltean 		/* Link-local traffic received on casc_port will be forwarded
4408aa9ebccSVladimir Oltean 		 * to host_port without embedding the source port and device ID
4418aa9ebccSVladimir Oltean 		 * info in the destination MAC address (presumably because it
4428aa9ebccSVladimir Oltean 		 * is a cascaded port and a downstream SJA switch already did
4438aa9ebccSVladimir Oltean 		 * that). Default to an invalid port (to disable the feature)
4448aa9ebccSVladimir Oltean 		 * and overwrite this if we find any DSA (cascaded) ports.
4458aa9ebccSVladimir Oltean 		 */
4468aa9ebccSVladimir Oltean 		.casc_port = SJA1105_NUM_PORTS,
4478aa9ebccSVladimir Oltean 		/* No TTEthernet */
4488aa9ebccSVladimir Oltean 		.vllupformat = 0,
4498aa9ebccSVladimir Oltean 		.vlmarker = 0,
4508aa9ebccSVladimir Oltean 		.vlmask = 0,
4518aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
4528aa9ebccSVladimir Oltean 		.ignore2stf = 0,
4536666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
4546666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
4556666cebcSVladimir Oltean 		 */
4566666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
4576666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
4588aa9ebccSVladimir Oltean 	};
4598aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4608aa9ebccSVladimir Oltean 
4618aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
4628aa9ebccSVladimir Oltean 
4638aa9ebccSVladimir Oltean 	if (table->entry_count) {
4648aa9ebccSVladimir Oltean 		kfree(table->entries);
4658aa9ebccSVladimir Oltean 		table->entry_count = 0;
4668aa9ebccSVladimir Oltean 	}
4678aa9ebccSVladimir Oltean 
4688aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
4698aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4708aa9ebccSVladimir Oltean 	if (!table->entries)
4718aa9ebccSVladimir Oltean 		return -ENOMEM;
4728aa9ebccSVladimir Oltean 
4738aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
4748aa9ebccSVladimir Oltean 
4758aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4768aa9ebccSVladimir Oltean 	((struct sja1105_general_params_entry *)table->entries)[0] =
4778aa9ebccSVladimir Oltean 				default_general_params;
4788aa9ebccSVladimir Oltean 
4798aa9ebccSVladimir Oltean 	return 0;
4808aa9ebccSVladimir Oltean }
4818aa9ebccSVladimir Oltean 
48279d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
48379d5511cSVladimir Oltean {
48479d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
48579d5511cSVladimir Oltean 	struct sja1105_table *table;
48679d5511cSVladimir Oltean 
48779d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
48879d5511cSVladimir Oltean 
48979d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
49079d5511cSVladimir Oltean 	if (table->entry_count) {
49179d5511cSVladimir Oltean 		kfree(table->entries);
49279d5511cSVladimir Oltean 		table->entry_count = 0;
49379d5511cSVladimir Oltean 	}
49479d5511cSVladimir Oltean 
49579d5511cSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
49679d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
49779d5511cSVladimir Oltean 	if (!table->entries)
49879d5511cSVladimir Oltean 		return -ENOMEM;
49979d5511cSVladimir Oltean 
50079d5511cSVladimir Oltean 	table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
50179d5511cSVladimir Oltean 
50279d5511cSVladimir Oltean 	avb = table->entries;
50379d5511cSVladimir Oltean 
50479d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
50579d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
50679d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
507747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
508747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
509747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
510747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
511747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
512747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
513747e5eb3SVladimir Oltean 	 */
514747e5eb3SVladimir Oltean 	avb->cas_master = false;
51579d5511cSVladimir Oltean 
51679d5511cSVladimir Oltean 	return 0;
51779d5511cSVladimir Oltean }
51879d5511cSVladimir Oltean 
519*a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
520*a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
521*a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
522*a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
523*a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
524*a7cc081cSVladimir Oltean  * will be used for this frame.
525*a7cc081cSVladimir Oltean  *
526*a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
527*a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
528*a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
529*a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
530*a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
531*a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
532*a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
533*a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
534*a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
535*a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
536*a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
537*a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
538*a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
539*a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
540*a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
541*a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
542*a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
543*a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
544*a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
545*a7cc081cSVladimir Oltean  * +------------+--------+
546*a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
547*a7cc081cSVladimir Oltean  * +------------+--------+
548*a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
549*a7cc081cSVladimir Oltean  * +------------+--------+
550*a7cc081cSVladimir Oltean  *    ...                                  ...
551*a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
552*a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
553*a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
554*a7cc081cSVladimir Oltean  *
555*a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
556*a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
557*a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
558*a7cc081cSVladimir Oltean  * lookup) equal.
559*a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
560*a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
561*a7cc081cSVladimir Oltean  */
5628aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
5638aa9ebccSVladimir Oltean 
5648aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
5658aa9ebccSVladimir Oltean {
5668aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
5678aa9ebccSVladimir Oltean 	struct sja1105_table *table;
568*a7cc081cSVladimir Oltean 	int port, tc;
5698aa9ebccSVladimir Oltean 
5708aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
5718aa9ebccSVladimir Oltean 
5728aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
5738aa9ebccSVladimir Oltean 	if (table->entry_count) {
5748aa9ebccSVladimir Oltean 		kfree(table->entries);
5758aa9ebccSVladimir Oltean 		table->entry_count = 0;
5768aa9ebccSVladimir Oltean 	}
5778aa9ebccSVladimir Oltean 
5788aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
5798aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5808aa9ebccSVladimir Oltean 	if (!table->entries)
5818aa9ebccSVladimir Oltean 		return -ENOMEM;
5828aa9ebccSVladimir Oltean 
5838aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
5848aa9ebccSVladimir Oltean 
5858aa9ebccSVladimir Oltean 	policing = table->entries;
5868aa9ebccSVladimir Oltean 
587*a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
588*a7cc081cSVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
589*a7cc081cSVladimir Oltean 		int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + port;
590*a7cc081cSVladimir Oltean 
591*a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
592*a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
593*a7cc081cSVladimir Oltean 
594*a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
595*a7cc081cSVladimir Oltean 	}
596*a7cc081cSVladimir Oltean 
597*a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
598*a7cc081cSVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
599c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
600c279c726SVladimir Oltean 
601*a7cc081cSVladimir Oltean 		if (dsa_is_cpu_port(priv->ds, port))
602c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
6038aa9ebccSVladimir Oltean 
604*a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
605*a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
606*a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
607*a7cc081cSVladimir Oltean 		policing[port].partition = 0;
6088aa9ebccSVladimir Oltean 	}
609*a7cc081cSVladimir Oltean 
6108aa9ebccSVladimir Oltean 	return 0;
6118aa9ebccSVladimir Oltean }
6128aa9ebccSVladimir Oltean 
6138aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv,
6148aa9ebccSVladimir Oltean 				      struct sja1105_dt_port *ports)
6158aa9ebccSVladimir Oltean {
6168aa9ebccSVladimir Oltean 	int rc;
6178aa9ebccSVladimir Oltean 
6188aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
6198aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
6208aa9ebccSVladimir Oltean 					priv->info->static_ops,
6218aa9ebccSVladimir Oltean 					priv->info->device_id);
6228aa9ebccSVladimir Oltean 	if (rc)
6238aa9ebccSVladimir Oltean 		return rc;
6248aa9ebccSVladimir Oltean 
6258aa9ebccSVladimir Oltean 	/* Build static configuration */
6268aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
6278aa9ebccSVladimir Oltean 	if (rc < 0)
6288aa9ebccSVladimir Oltean 		return rc;
6298aa9ebccSVladimir Oltean 	rc = sja1105_init_mii_settings(priv, ports);
6308aa9ebccSVladimir Oltean 	if (rc < 0)
6318aa9ebccSVladimir Oltean 		return rc;
6328aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
6338aa9ebccSVladimir Oltean 	if (rc < 0)
6348aa9ebccSVladimir Oltean 		return rc;
6358aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
6368aa9ebccSVladimir Oltean 	if (rc < 0)
6378aa9ebccSVladimir Oltean 		return rc;
6388aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
6398aa9ebccSVladimir Oltean 	if (rc < 0)
6408aa9ebccSVladimir Oltean 		return rc;
6418aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
6428aa9ebccSVladimir Oltean 	if (rc < 0)
6438aa9ebccSVladimir Oltean 		return rc;
6448aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
6458aa9ebccSVladimir Oltean 	if (rc < 0)
6468aa9ebccSVladimir Oltean 		return rc;
6478aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
6488aa9ebccSVladimir Oltean 	if (rc < 0)
6498aa9ebccSVladimir Oltean 		return rc;
6508aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
6518aa9ebccSVladimir Oltean 	if (rc < 0)
6528aa9ebccSVladimir Oltean 		return rc;
65379d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
65479d5511cSVladimir Oltean 	if (rc < 0)
65579d5511cSVladimir Oltean 		return rc;
6568aa9ebccSVladimir Oltean 
6578aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
6588aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
6598aa9ebccSVladimir Oltean }
6608aa9ebccSVladimir Oltean 
661f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
662f5b8631cSVladimir Oltean 				      const struct sja1105_dt_port *ports)
663f5b8631cSVladimir Oltean {
664f5b8631cSVladimir Oltean 	int i;
665f5b8631cSVladimir Oltean 
666f5b8631cSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
6679bca3a0aSOleksij Rempel 		if (ports[i].role == XMII_MAC)
668f5b8631cSVladimir Oltean 			continue;
669f5b8631cSVladimir Oltean 
6709bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
6719bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
672f5b8631cSVladimir Oltean 			priv->rgmii_rx_delay[i] = true;
673f5b8631cSVladimir Oltean 
6749bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
6759bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
676f5b8631cSVladimir Oltean 			priv->rgmii_tx_delay[i] = true;
677f5b8631cSVladimir Oltean 
678f5b8631cSVladimir Oltean 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
679f5b8631cSVladimir Oltean 		     !priv->info->setup_rgmii_delay)
680f5b8631cSVladimir Oltean 			return -EINVAL;
681f5b8631cSVladimir Oltean 	}
682f5b8631cSVladimir Oltean 	return 0;
683f5b8631cSVladimir Oltean }
684f5b8631cSVladimir Oltean 
6858aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
6868aa9ebccSVladimir Oltean 				    struct sja1105_dt_port *ports,
6878aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
6888aa9ebccSVladimir Oltean {
6898aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6908aa9ebccSVladimir Oltean 	struct device_node *child;
6918aa9ebccSVladimir Oltean 
69227afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
6938aa9ebccSVladimir Oltean 		struct device_node *phy_node;
6940c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
6958aa9ebccSVladimir Oltean 		u32 index;
6960c65b2b9SAndrew Lunn 		int err;
6978aa9ebccSVladimir Oltean 
6988aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
6998aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
7008aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
7018aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
7027ba771e3SNishka Dasgupta 			of_node_put(child);
7038aa9ebccSVladimir Oltean 			return -ENODEV;
7048aa9ebccSVladimir Oltean 		}
7058aa9ebccSVladimir Oltean 
7068aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
7070c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
7080c65b2b9SAndrew Lunn 		if (err) {
7098aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
7108aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
7118aa9ebccSVladimir Oltean 				index);
7127ba771e3SNishka Dasgupta 			of_node_put(child);
7138aa9ebccSVladimir Oltean 			return -ENODEV;
7148aa9ebccSVladimir Oltean 		}
7158aa9ebccSVladimir Oltean 		ports[index].phy_mode = phy_mode;
7168aa9ebccSVladimir Oltean 
7178aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
7188aa9ebccSVladimir Oltean 		if (!phy_node) {
7198aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
7208aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
7218aa9ebccSVladimir Oltean 					"properties missing!\n");
7227ba771e3SNishka Dasgupta 				of_node_put(child);
7238aa9ebccSVladimir Oltean 				return -ENODEV;
7248aa9ebccSVladimir Oltean 			}
7258aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
7268aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
7278aa9ebccSVladimir Oltean 			 */
7288aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
7298aa9ebccSVladimir Oltean 		} else {
7308aa9ebccSVladimir Oltean 			/* phy-handle present => put port in MAC role */
7318aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
7328aa9ebccSVladimir Oltean 			of_node_put(phy_node);
7338aa9ebccSVladimir Oltean 		}
7348aa9ebccSVladimir Oltean 
7358aa9ebccSVladimir Oltean 		/* The MAC/PHY role can be overridden with explicit bindings */
7368aa9ebccSVladimir Oltean 		if (of_property_read_bool(child, "sja1105,role-mac"))
7378aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
7388aa9ebccSVladimir Oltean 		else if (of_property_read_bool(child, "sja1105,role-phy"))
7398aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
7408aa9ebccSVladimir Oltean 	}
7418aa9ebccSVladimir Oltean 
7428aa9ebccSVladimir Oltean 	return 0;
7438aa9ebccSVladimir Oltean }
7448aa9ebccSVladimir Oltean 
7458aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv,
7468aa9ebccSVladimir Oltean 			    struct sja1105_dt_port *ports)
7478aa9ebccSVladimir Oltean {
7488aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
7498aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
7508aa9ebccSVladimir Oltean 	struct device_node *ports_node;
7518aa9ebccSVladimir Oltean 	int rc;
7528aa9ebccSVladimir Oltean 
7538aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
7548aa9ebccSVladimir Oltean 	if (!ports_node) {
7558aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
7568aa9ebccSVladimir Oltean 		return -ENODEV;
7578aa9ebccSVladimir Oltean 	}
7588aa9ebccSVladimir Oltean 
7598aa9ebccSVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
7608aa9ebccSVladimir Oltean 	of_node_put(ports_node);
7618aa9ebccSVladimir Oltean 
7628aa9ebccSVladimir Oltean 	return rc;
7638aa9ebccSVladimir Oltean }
7648aa9ebccSVladimir Oltean 
765ffe10e67SVladimir Oltean static int sja1105_sgmii_read(struct sja1105_private *priv, int pcs_reg)
766ffe10e67SVladimir Oltean {
767ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
768ffe10e67SVladimir Oltean 	u32 val;
769ffe10e67SVladimir Oltean 	int rc;
770ffe10e67SVladimir Oltean 
771ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->sgmii + pcs_reg, &val,
772ffe10e67SVladimir Oltean 			      NULL);
773ffe10e67SVladimir Oltean 	if (rc < 0)
774ffe10e67SVladimir Oltean 		return rc;
775ffe10e67SVladimir Oltean 
776ffe10e67SVladimir Oltean 	return val;
777ffe10e67SVladimir Oltean }
778ffe10e67SVladimir Oltean 
779ffe10e67SVladimir Oltean static int sja1105_sgmii_write(struct sja1105_private *priv, int pcs_reg,
780ffe10e67SVladimir Oltean 			       u16 pcs_val)
781ffe10e67SVladimir Oltean {
782ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
783ffe10e67SVladimir Oltean 	u32 val = pcs_val;
784ffe10e67SVladimir Oltean 	int rc;
785ffe10e67SVladimir Oltean 
786ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->sgmii + pcs_reg, &val,
787ffe10e67SVladimir Oltean 			      NULL);
788ffe10e67SVladimir Oltean 	if (rc < 0)
789ffe10e67SVladimir Oltean 		return rc;
790ffe10e67SVladimir Oltean 
791ffe10e67SVladimir Oltean 	return val;
792ffe10e67SVladimir Oltean }
793ffe10e67SVladimir Oltean 
794ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_config(struct sja1105_private *priv,
795ffe10e67SVladimir Oltean 				     bool an_enabled, bool an_master)
796ffe10e67SVladimir Oltean {
797ffe10e67SVladimir Oltean 	u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII;
798ffe10e67SVladimir Oltean 
799ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
800ffe10e67SVladimir Oltean 	 * stop the clock during LPI mode, make the MAC reconfigure
801ffe10e67SVladimir Oltean 	 * autonomously after PCS autoneg is done, flush the internal FIFOs.
802ffe10e67SVladimir Oltean 	 */
803ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC1, SJA1105_DC1_EN_VSMMD1 |
804ffe10e67SVladimir Oltean 					       SJA1105_DC1_CLOCK_STOP_EN |
805ffe10e67SVladimir Oltean 					       SJA1105_DC1_MAC_AUTO_SW |
806ffe10e67SVladimir Oltean 					       SJA1105_DC1_INIT);
807ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
808ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC2, SJA1105_DC2_TX_POL_INV_DISABLE);
809ffe10e67SVladimir Oltean 	/* AUTONEG_CONTROL: Use SGMII autoneg */
810ffe10e67SVladimir Oltean 	if (an_master)
811ffe10e67SVladimir Oltean 		ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK;
812ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_AC, ac);
813ffe10e67SVladimir Oltean 	/* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
814ffe10e67SVladimir Oltean 	 * sja1105_sgmii_pcs_force_speed must be called later for the link
815ffe10e67SVladimir Oltean 	 * to become operational.
816ffe10e67SVladimir Oltean 	 */
817ffe10e67SVladimir Oltean 	if (an_enabled)
818ffe10e67SVladimir Oltean 		sja1105_sgmii_write(priv, MII_BMCR,
819ffe10e67SVladimir Oltean 				    BMCR_ANENABLE | BMCR_ANRESTART);
820ffe10e67SVladimir Oltean }
821ffe10e67SVladimir Oltean 
822ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv,
823ffe10e67SVladimir Oltean 					  int speed)
824ffe10e67SVladimir Oltean {
825ffe10e67SVladimir Oltean 	int pcs_speed;
826ffe10e67SVladimir Oltean 
827ffe10e67SVladimir Oltean 	switch (speed) {
828ffe10e67SVladimir Oltean 	case SPEED_1000:
829ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED1000;
830ffe10e67SVladimir Oltean 		break;
831ffe10e67SVladimir Oltean 	case SPEED_100:
832ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED100;
833ffe10e67SVladimir Oltean 		break;
834ffe10e67SVladimir Oltean 	case SPEED_10:
835ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED10;
836ffe10e67SVladimir Oltean 		break;
837ffe10e67SVladimir Oltean 	default:
838ffe10e67SVladimir Oltean 		dev_err(priv->ds->dev, "Invalid speed %d\n", speed);
839ffe10e67SVladimir Oltean 		return;
840ffe10e67SVladimir Oltean 	}
841ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, MII_BMCR, pcs_speed | BMCR_FULLDPLX);
842ffe10e67SVladimir Oltean }
843ffe10e67SVladimir Oltean 
844c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
8458aa9ebccSVladimir Oltean static int sja1105_speed[] = {
846c44d0535SVladimir Oltean 	[SJA1105_SPEED_AUTO]		= SPEED_UNKNOWN,
847c44d0535SVladimir Oltean 	[SJA1105_SPEED_10MBPS]		= SPEED_10,
848c44d0535SVladimir Oltean 	[SJA1105_SPEED_100MBPS]		= SPEED_100,
849c44d0535SVladimir Oltean 	[SJA1105_SPEED_1000MBPS]	= SPEED_1000,
8508aa9ebccSVladimir Oltean };
8518aa9ebccSVladimir Oltean 
8528400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
8538aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
8548400cff6SVladimir Oltean 				      int speed_mbps)
8558aa9ebccSVladimir Oltean {
8568aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
8578aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
8588aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
8598aa9ebccSVladimir Oltean 	sja1105_phy_interface_t phy_mode;
8608aa9ebccSVladimir Oltean 	sja1105_speed_t speed;
8618aa9ebccSVladimir Oltean 	int rc;
8628aa9ebccSVladimir Oltean 
8638400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
8648400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
8658400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
8668400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
8678400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
8688400cff6SVladimir Oltean 	 */
8698aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
8708400cff6SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
8718aa9ebccSVladimir Oltean 
872f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
873c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
874a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
875a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
876a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
877a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
878a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
879a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
880a979a0abSVladimir Oltean 		 */
881f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_AUTO;
882f4cfcfbdSVladimir Oltean 		break;
883c44d0535SVladimir Oltean 	case SPEED_10:
884f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_10MBPS;
885f4cfcfbdSVladimir Oltean 		break;
886c44d0535SVladimir Oltean 	case SPEED_100:
887f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_100MBPS;
888f4cfcfbdSVladimir Oltean 		break;
889c44d0535SVladimir Oltean 	case SPEED_1000:
890f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_1000MBPS;
891f4cfcfbdSVladimir Oltean 		break;
892f4cfcfbdSVladimir Oltean 	default:
8938aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
8948aa9ebccSVladimir Oltean 		return -EINVAL;
8958aa9ebccSVladimir Oltean 	}
8968aa9ebccSVladimir Oltean 
8978400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
8988400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
8998400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
9008400cff6SVladimir Oltean 	 * we want auto during upload phase).
901ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
902ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
9038aa9ebccSVladimir Oltean 	 */
904ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port))
905ffe10e67SVladimir Oltean 		mac[port].speed = SJA1105_SPEED_1000MBPS;
906ffe10e67SVladimir Oltean 	else
9078aa9ebccSVladimir Oltean 		mac[port].speed = speed;
9088aa9ebccSVladimir Oltean 
9098aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
9108400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
9118400cff6SVladimir Oltean 					  &mac[port], true);
9128aa9ebccSVladimir Oltean 	if (rc < 0) {
9138aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
9148aa9ebccSVladimir Oltean 		return rc;
9158aa9ebccSVladimir Oltean 	}
9168aa9ebccSVladimir Oltean 
9178aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
9188aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
9198aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
9208aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
9218aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
9228aa9ebccSVladimir Oltean 	 */
9238aa9ebccSVladimir Oltean 	phy_mode = mii->xmii_mode[port];
9248aa9ebccSVladimir Oltean 	if (phy_mode != XMII_MODE_RGMII)
9258aa9ebccSVladimir Oltean 		return 0;
9268aa9ebccSVladimir Oltean 
9278aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
9288aa9ebccSVladimir Oltean }
9298aa9ebccSVladimir Oltean 
93039710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
93139710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
93239710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
93339710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
93439710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
93539710229SVladimir Oltean  * now.
93639710229SVladimir Oltean  */
93739710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
93839710229SVladimir Oltean 				      phy_interface_t interface)
93939710229SVladimir Oltean {
94039710229SVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
94139710229SVladimir Oltean 	sja1105_phy_interface_t phy_mode;
94239710229SVladimir Oltean 
94339710229SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
94439710229SVladimir Oltean 	phy_mode = mii->xmii_mode[port];
94539710229SVladimir Oltean 
94639710229SVladimir Oltean 	switch (interface) {
94739710229SVladimir Oltean 	case PHY_INTERFACE_MODE_MII:
94839710229SVladimir Oltean 		return (phy_mode != XMII_MODE_MII);
94939710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RMII:
95039710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RMII);
95139710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII:
95239710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_ID:
95339710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_RXID:
95439710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_TXID:
95539710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RGMII);
956ffe10e67SVladimir Oltean 	case PHY_INTERFACE_MODE_SGMII:
957ffe10e67SVladimir Oltean 		return (phy_mode != XMII_MODE_SGMII);
95839710229SVladimir Oltean 	default:
95939710229SVladimir Oltean 		return true;
96039710229SVladimir Oltean 	}
96139710229SVladimir Oltean }
96239710229SVladimir Oltean 
963af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
964ffe10e67SVladimir Oltean 			       unsigned int mode,
965af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
9668aa9ebccSVladimir Oltean {
9678aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
968ffe10e67SVladimir Oltean 	bool is_sgmii = sja1105_supports_sgmii(priv, port);
9698aa9ebccSVladimir Oltean 
970ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
971ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
972ec8582d1SVladimir Oltean 			phy_modes(state->interface));
97339710229SVladimir Oltean 		return;
974ec8582d1SVladimir Oltean 	}
97539710229SVladimir Oltean 
976ffe10e67SVladimir Oltean 	if (phylink_autoneg_inband(mode) && !is_sgmii) {
9779f971573SVladimir Oltean 		dev_err(ds->dev, "In-band AN not supported!\n");
9789f971573SVladimir Oltean 		return;
9799f971573SVladimir Oltean 	}
980ffe10e67SVladimir Oltean 
981ffe10e67SVladimir Oltean 	if (is_sgmii)
982ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, phylink_autoneg_inband(mode),
983ffe10e67SVladimir Oltean 					 false);
9848400cff6SVladimir Oltean }
9858400cff6SVladimir Oltean 
9868400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
9878400cff6SVladimir Oltean 				  unsigned int mode,
9888400cff6SVladimir Oltean 				  phy_interface_t interface)
9898400cff6SVladimir Oltean {
9908400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
9918400cff6SVladimir Oltean }
9928400cff6SVladimir Oltean 
9938400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
9948400cff6SVladimir Oltean 				unsigned int mode,
9958400cff6SVladimir Oltean 				phy_interface_t interface,
9965b502a7bSRussell King 				struct phy_device *phydev,
9975b502a7bSRussell King 				int speed, int duplex,
9985b502a7bSRussell King 				bool tx_pause, bool rx_pause)
9998400cff6SVladimir Oltean {
1000ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1001ec8582d1SVladimir Oltean 
1002ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1003ec8582d1SVladimir Oltean 
1004ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode))
1005ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_force_speed(priv, speed);
1006ffe10e67SVladimir Oltean 
1007ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
10088aa9ebccSVladimir Oltean }
10098aa9ebccSVladimir Oltean 
1010ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1011ad9f299aSVladimir Oltean 				     unsigned long *supported,
1012ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
1013ad9f299aSVladimir Oltean {
1014ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
1015ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
1016ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
1017ad9f299aSVladimir Oltean 	 */
1018ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1019ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1020ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1021ad9f299aSVladimir Oltean 
1022ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1023ad9f299aSVladimir Oltean 
102439710229SVladimir Oltean 	/* include/linux/phylink.h says:
102539710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
102639710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
102739710229SVladimir Oltean 	 */
102839710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
102939710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
103039710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
103139710229SVladimir Oltean 		return;
103239710229SVladimir Oltean 	}
103339710229SVladimir Oltean 
1034ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1035ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1036ad9f299aSVladimir Oltean 	 */
1037ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
1038ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
1039ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
1040ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
1041ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
1042ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1043ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1044ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
1045ad9f299aSVladimir Oltean 
1046ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1047ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1048ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1049ad9f299aSVladimir Oltean }
1050ad9f299aSVladimir Oltean 
1051ffe10e67SVladimir Oltean static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port,
1052ffe10e67SVladimir Oltean 				     struct phylink_link_state *state)
1053ffe10e67SVladimir Oltean {
1054ffe10e67SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1055ffe10e67SVladimir Oltean 	int ais;
1056ffe10e67SVladimir Oltean 
1057ffe10e67SVladimir Oltean 	/* Read the vendor-specific AUTONEG_INTR_STATUS register */
1058ffe10e67SVladimir Oltean 	ais = sja1105_sgmii_read(priv, SJA1105_AIS);
1059ffe10e67SVladimir Oltean 	if (ais < 0)
1060ffe10e67SVladimir Oltean 		return ais;
1061ffe10e67SVladimir Oltean 
1062ffe10e67SVladimir Oltean 	switch (SJA1105_AIS_SPEED(ais)) {
1063ffe10e67SVladimir Oltean 	case 0:
1064ffe10e67SVladimir Oltean 		state->speed = SPEED_10;
1065ffe10e67SVladimir Oltean 		break;
1066ffe10e67SVladimir Oltean 	case 1:
1067ffe10e67SVladimir Oltean 		state->speed = SPEED_100;
1068ffe10e67SVladimir Oltean 		break;
1069ffe10e67SVladimir Oltean 	case 2:
1070ffe10e67SVladimir Oltean 		state->speed = SPEED_1000;
1071ffe10e67SVladimir Oltean 		break;
1072ffe10e67SVladimir Oltean 	default:
1073ffe10e67SVladimir Oltean 		dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n",
1074ffe10e67SVladimir Oltean 			SJA1105_AIS_SPEED(ais));
1075ffe10e67SVladimir Oltean 	}
1076ffe10e67SVladimir Oltean 	state->duplex = SJA1105_AIS_DUPLEX_MODE(ais);
1077ffe10e67SVladimir Oltean 	state->an_complete = SJA1105_AIS_COMPLETE(ais);
1078ffe10e67SVladimir Oltean 	state->link = SJA1105_AIS_LINK_STATUS(ais);
1079ffe10e67SVladimir Oltean 
1080ffe10e67SVladimir Oltean 	return 0;
1081ffe10e67SVladimir Oltean }
1082ffe10e67SVladimir Oltean 
108360f6053fSVladimir Oltean static int
108460f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
108560f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
108660f6053fSVladimir Oltean {
108760f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
108860f6053fSVladimir Oltean 	struct sja1105_table *table;
108960f6053fSVladimir Oltean 	int i;
109060f6053fSVladimir Oltean 
109160f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
109260f6053fSVladimir Oltean 	l2_lookup = table->entries;
109360f6053fSVladimir Oltean 
109460f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
109560f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
109660f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
109760f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
109860f6053fSVladimir Oltean 			return i;
109960f6053fSVladimir Oltean 
110060f6053fSVladimir Oltean 	return -1;
110160f6053fSVladimir Oltean }
110260f6053fSVladimir Oltean 
110360f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
110460f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
110560f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
110660f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
110760f6053fSVladimir Oltean  */
110860f6053fSVladimir Oltean static int
110960f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
111060f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
111160f6053fSVladimir Oltean 			  bool keep)
111260f6053fSVladimir Oltean {
111360f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
111460f6053fSVladimir Oltean 	struct sja1105_table *table;
111560f6053fSVladimir Oltean 	int rc, match;
111660f6053fSVladimir Oltean 
111760f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
111860f6053fSVladimir Oltean 
111960f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
112060f6053fSVladimir Oltean 	if (match < 0) {
112160f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
112260f6053fSVladimir Oltean 		if (!keep)
112360f6053fSVladimir Oltean 			return 0;
112460f6053fSVladimir Oltean 
112560f6053fSVladimir Oltean 		/* No match => new entry */
112660f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
112760f6053fSVladimir Oltean 		if (rc)
112860f6053fSVladimir Oltean 			return rc;
112960f6053fSVladimir Oltean 
113060f6053fSVladimir Oltean 		match = table->entry_count - 1;
113160f6053fSVladimir Oltean 	}
113260f6053fSVladimir Oltean 
113360f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
113460f6053fSVladimir Oltean 	l2_lookup = table->entries;
113560f6053fSVladimir Oltean 
113660f6053fSVladimir Oltean 	/* We have a match.
113760f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
113860f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
113960f6053fSVladimir Oltean 	 * which we update it).
114060f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
114160f6053fSVladimir Oltean 	 */
114260f6053fSVladimir Oltean 	if (keep) {
114360f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
114460f6053fSVladimir Oltean 		return 0;
114560f6053fSVladimir Oltean 	}
114660f6053fSVladimir Oltean 
114760f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
114860f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
114960f6053fSVladimir Oltean 	 */
115060f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
115160f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
115260f6053fSVladimir Oltean }
115360f6053fSVladimir Oltean 
1154291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1155291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1156291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1157291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1158291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1159291d1e72SVladimir Oltean  */
116009c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1161291d1e72SVladimir Oltean {
1162291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1163291d1e72SVladimir Oltean }
1164291d1e72SVladimir Oltean 
11659dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1166291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1167291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1168291d1e72SVladimir Oltean 					 int *last_unused)
1169291d1e72SVladimir Oltean {
1170291d1e72SVladimir Oltean 	int way;
1171291d1e72SVladimir Oltean 
1172291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1173291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1174291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1175291d1e72SVladimir Oltean 
1176291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1177291d1e72SVladimir Oltean 		 * into the return value
1178291d1e72SVladimir Oltean 		 */
1179291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1180291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1181291d1e72SVladimir Oltean 			if (last_unused)
1182291d1e72SVladimir Oltean 				*last_unused = way;
1183291d1e72SVladimir Oltean 			continue;
1184291d1e72SVladimir Oltean 		}
1185291d1e72SVladimir Oltean 
1186291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1187291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1188291d1e72SVladimir Oltean 			if (match)
1189291d1e72SVladimir Oltean 				*match = l2_lookup;
1190291d1e72SVladimir Oltean 			return way;
1191291d1e72SVladimir Oltean 		}
1192291d1e72SVladimir Oltean 	}
1193291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1194291d1e72SVladimir Oltean 	return -1;
1195291d1e72SVladimir Oltean }
1196291d1e72SVladimir Oltean 
11979dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1198291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1199291d1e72SVladimir Oltean {
1200291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1201291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1202291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1203291d1e72SVladimir Oltean 	int last_unused = -1;
120460f6053fSVladimir Oltean 	int bin, way, rc;
1205291d1e72SVladimir Oltean 
12069dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1207291d1e72SVladimir Oltean 
12089dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1209291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1210291d1e72SVladimir Oltean 	if (way >= 0) {
1211291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1212291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1213291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1214291d1e72SVladimir Oltean 		 */
1215291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1216291d1e72SVladimir Oltean 			return 0;
1217291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1218291d1e72SVladimir Oltean 	} else {
1219291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1220291d1e72SVladimir Oltean 
1221291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1222291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1223291d1e72SVladimir Oltean 		 */
1224291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1225291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1226291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1227291d1e72SVladimir Oltean 
1228291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1229291d1e72SVladimir Oltean 			way = last_unused;
1230291d1e72SVladimir Oltean 		} else {
1231291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1232291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1233291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1234291d1e72SVladimir Oltean 			 * distribution function:
1235291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1236291d1e72SVladimir Oltean 			 */
1237291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1238291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1239291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1240291d1e72SVladimir Oltean 				 bin, addr, way);
1241291d1e72SVladimir Oltean 			/* Evict entry */
1242291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1243291d1e72SVladimir Oltean 						     index, NULL, false);
1244291d1e72SVladimir Oltean 		}
1245291d1e72SVladimir Oltean 	}
1246291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1247291d1e72SVladimir Oltean 
124860f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1249291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1250291d1e72SVladimir Oltean 					  true);
125160f6053fSVladimir Oltean 	if (rc < 0)
125260f6053fSVladimir Oltean 		return rc;
125360f6053fSVladimir Oltean 
125460f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1255291d1e72SVladimir Oltean }
1256291d1e72SVladimir Oltean 
12579dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1258291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1259291d1e72SVladimir Oltean {
1260291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1261291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
126260f6053fSVladimir Oltean 	int index, bin, way, rc;
1263291d1e72SVladimir Oltean 	bool keep;
1264291d1e72SVladimir Oltean 
12659dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
12669dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1267291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1268291d1e72SVladimir Oltean 	if (way < 0)
1269291d1e72SVladimir Oltean 		return 0;
1270291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1271291d1e72SVladimir Oltean 
1272291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1273291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1274291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1275291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1276291d1e72SVladimir Oltean 	 */
1277291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
12787752e937SVladimir Oltean 
1279291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1280291d1e72SVladimir Oltean 		keep = true;
1281291d1e72SVladimir Oltean 	else
1282291d1e72SVladimir Oltean 		keep = false;
1283291d1e72SVladimir Oltean 
128460f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1285291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
128660f6053fSVladimir Oltean 	if (rc < 0)
128760f6053fSVladimir Oltean 		return rc;
128860f6053fSVladimir Oltean 
128960f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1290291d1e72SVladimir Oltean }
1291291d1e72SVladimir Oltean 
12929dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
12939dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
12949dfa6911SVladimir Oltean {
12951da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
12961da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12971da73821SVladimir Oltean 	int rc, i;
12981da73821SVladimir Oltean 
12991da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
13001da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
13011da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
13021da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
13031da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
130468bb8ea8SVivien Didelot 	if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) {
13051da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
13061da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
13076d7c7d94SVladimir Oltean 	} else {
13086d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
13096d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
13106d7c7d94SVladimir Oltean 	}
13111da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
13121da73821SVladimir Oltean 
13131da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13141da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
13151da73821SVladimir Oltean 	if (rc == 0) {
13161da73821SVladimir Oltean 		/* Found and this port is already in the entry's
13171da73821SVladimir Oltean 		 * port mask => job done
13181da73821SVladimir Oltean 		 */
13191da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
13201da73821SVladimir Oltean 			return 0;
13211da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
13221da73821SVladimir Oltean 		 * found something.
13231da73821SVladimir Oltean 		 */
13241da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
13251da73821SVladimir Oltean 		goto skip_finding_an_index;
13261da73821SVladimir Oltean 	}
13271da73821SVladimir Oltean 
13281da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
13291da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
13301da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
13311da73821SVladimir Oltean 	 */
13321da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
13331da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13341da73821SVladimir Oltean 						 i, NULL);
13351da73821SVladimir Oltean 		if (rc < 0)
13361da73821SVladimir Oltean 			break;
13371da73821SVladimir Oltean 	}
13381da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
13391da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
13401da73821SVladimir Oltean 		return -EINVAL;
13411da73821SVladimir Oltean 	}
134217ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
13431da73821SVladimir Oltean 	l2_lookup.index = i;
13441da73821SVladimir Oltean 
13451da73821SVladimir Oltean skip_finding_an_index:
134660f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
13471da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
13481da73821SVladimir Oltean 					  true);
134960f6053fSVladimir Oltean 	if (rc < 0)
135060f6053fSVladimir Oltean 		return rc;
135160f6053fSVladimir Oltean 
135260f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
13539dfa6911SVladimir Oltean }
13549dfa6911SVladimir Oltean 
13559dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
13569dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
13579dfa6911SVladimir Oltean {
13581da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
13591da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
13601da73821SVladimir Oltean 	bool keep;
13611da73821SVladimir Oltean 	int rc;
13621da73821SVladimir Oltean 
13631da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
13641da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
13651da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
13661da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
136768bb8ea8SVivien Didelot 	if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) {
13681da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
13691da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
13706d7c7d94SVladimir Oltean 	} else {
13716d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
13726d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
13736d7c7d94SVladimir Oltean 	}
13741da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
13751da73821SVladimir Oltean 
13761da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13771da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
13781da73821SVladimir Oltean 	if (rc < 0)
13791da73821SVladimir Oltean 		return 0;
13801da73821SVladimir Oltean 
13811da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
13821da73821SVladimir Oltean 
13831da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
13841da73821SVladimir Oltean 	 * or if we remove it completely.
13851da73821SVladimir Oltean 	 */
13861da73821SVladimir Oltean 	if (l2_lookup.destports)
13871da73821SVladimir Oltean 		keep = true;
13881da73821SVladimir Oltean 	else
13891da73821SVladimir Oltean 		keep = false;
13901da73821SVladimir Oltean 
139160f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
13921da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
139360f6053fSVladimir Oltean 	if (rc < 0)
139460f6053fSVladimir Oltean 		return rc;
139560f6053fSVladimir Oltean 
139660f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
13979dfa6911SVladimir Oltean }
13989dfa6911SVladimir Oltean 
13999dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
14009dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
14019dfa6911SVladimir Oltean {
14029dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1403b3ee526aSVladimir Oltean 
14046d7c7d94SVladimir Oltean 	/* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
14056d7c7d94SVladimir Oltean 	 * so the switch still does some VLAN processing internally.
14066d7c7d94SVladimir Oltean 	 * But Shared VLAN Learning (SVL) is also active, and it will take
14076d7c7d94SVladimir Oltean 	 * care of autonomous forwarding between the unique pvid's of each
14086d7c7d94SVladimir Oltean 	 * port.  Here we just make sure that users can't add duplicate FDB
14096d7c7d94SVladimir Oltean 	 * entries when in this mode - the actual VID doesn't matter except
14106d7c7d94SVladimir Oltean 	 * for what gets printed in 'bridge fdb show'.  In the case of zero,
14116d7c7d94SVladimir Oltean 	 * no VID gets printed at all.
141293647594SVladimir Oltean 	 */
141368bb8ea8SVivien Didelot 	if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
14146d7c7d94SVladimir Oltean 		vid = 0;
141593647594SVladimir Oltean 
14166d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
14179dfa6911SVladimir Oltean }
14189dfa6911SVladimir Oltean 
14199dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
14209dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
14219dfa6911SVladimir Oltean {
14229dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14239dfa6911SVladimir Oltean 
142468bb8ea8SVivien Didelot 	if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
14256d7c7d94SVladimir Oltean 		vid = 0;
14266d7c7d94SVladimir Oltean 
1427b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
14289dfa6911SVladimir Oltean }
14299dfa6911SVladimir Oltean 
1430291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1431291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1432291d1e72SVladimir Oltean {
1433291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1434291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1435291d1e72SVladimir Oltean 	int i;
1436291d1e72SVladimir Oltean 
1437291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1438291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1439291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1440291d1e72SVladimir Oltean 		int rc;
1441291d1e72SVladimir Oltean 
1442291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1443291d1e72SVladimir Oltean 						 i, &l2_lookup);
1444291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1445def84604SVladimir Oltean 		if (rc == -ENOENT)
1446291d1e72SVladimir Oltean 			continue;
1447291d1e72SVladimir Oltean 		if (rc) {
1448291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1449291d1e72SVladimir Oltean 			return rc;
1450291d1e72SVladimir Oltean 		}
1451291d1e72SVladimir Oltean 
1452291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1453291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1454291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1455291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1456291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1457291d1e72SVladimir Oltean 		 */
1458291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1459291d1e72SVladimir Oltean 			continue;
1460291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
146193647594SVladimir Oltean 
14626d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
146368bb8ea8SVivien Didelot 		if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
14646d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
146517ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1466291d1e72SVladimir Oltean 	}
1467291d1e72SVladimir Oltean 	return 0;
1468291d1e72SVladimir Oltean }
1469291d1e72SVladimir Oltean 
1470291d1e72SVladimir Oltean /* This callback needs to be present */
1471291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1472291d1e72SVladimir Oltean 			       const struct switchdev_obj_port_mdb *mdb)
1473291d1e72SVladimir Oltean {
1474291d1e72SVladimir Oltean 	return 0;
1475291d1e72SVladimir Oltean }
1476291d1e72SVladimir Oltean 
1477291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1478291d1e72SVladimir Oltean 			    const struct switchdev_obj_port_mdb *mdb)
1479291d1e72SVladimir Oltean {
1480291d1e72SVladimir Oltean 	sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1481291d1e72SVladimir Oltean }
1482291d1e72SVladimir Oltean 
1483291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1484291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1485291d1e72SVladimir Oltean {
1486291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1487291d1e72SVladimir Oltean }
1488291d1e72SVladimir Oltean 
14898aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
14908aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
14918aa9ebccSVladimir Oltean {
14928aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
14938aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14948aa9ebccSVladimir Oltean 	int i, rc;
14958aa9ebccSVladimir Oltean 
14968aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
14978aa9ebccSVladimir Oltean 
14988aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
14998aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
15008aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
15018aa9ebccSVladimir Oltean 		 */
15028aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
15038aa9ebccSVladimir Oltean 			continue;
15048aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
15058aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
15068aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
15078aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
15088aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
15098aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
15108aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
15118aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
15128aa9ebccSVladimir Oltean 		 */
15138aa9ebccSVladimir Oltean 		if (i == port)
15148aa9ebccSVladimir Oltean 			continue;
15158aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
15168aa9ebccSVladimir Oltean 			continue;
15178aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
15188aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
15198aa9ebccSVladimir Oltean 
15208aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
15218aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
15228aa9ebccSVladimir Oltean 		if (rc < 0)
15238aa9ebccSVladimir Oltean 			return rc;
15248aa9ebccSVladimir Oltean 	}
15258aa9ebccSVladimir Oltean 
15268aa9ebccSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
15278aa9ebccSVladimir Oltean 					    port, &l2_fwd[port], true);
15288aa9ebccSVladimir Oltean }
15298aa9ebccSVladimir Oltean 
1530640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1531640f763fSVladimir Oltean 					 u8 state)
1532640f763fSVladimir Oltean {
1533640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1534640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1535640f763fSVladimir Oltean 
1536640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1537640f763fSVladimir Oltean 
1538640f763fSVladimir Oltean 	switch (state) {
1539640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1540640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1541640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1542640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1543640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1544640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1545640f763fSVladimir Oltean 		 */
1546640f763fSVladimir Oltean 		mac[port].ingress   = false;
1547640f763fSVladimir Oltean 		mac[port].egress    = false;
1548640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1549640f763fSVladimir Oltean 		break;
1550640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1551640f763fSVladimir Oltean 		mac[port].ingress   = true;
1552640f763fSVladimir Oltean 		mac[port].egress    = false;
1553640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1554640f763fSVladimir Oltean 		break;
1555640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1556640f763fSVladimir Oltean 		mac[port].ingress   = true;
1557640f763fSVladimir Oltean 		mac[port].egress    = false;
1558640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1559640f763fSVladimir Oltean 		break;
1560640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1561640f763fSVladimir Oltean 		mac[port].ingress   = true;
1562640f763fSVladimir Oltean 		mac[port].egress    = true;
1563640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1564640f763fSVladimir Oltean 		break;
1565640f763fSVladimir Oltean 	default:
1566640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1567640f763fSVladimir Oltean 		return;
1568640f763fSVladimir Oltean 	}
1569640f763fSVladimir Oltean 
1570640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1571640f763fSVladimir Oltean 				     &mac[port], true);
1572640f763fSVladimir Oltean }
1573640f763fSVladimir Oltean 
15748aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
15758aa9ebccSVladimir Oltean 			       struct net_device *br)
15768aa9ebccSVladimir Oltean {
15778aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
15788aa9ebccSVladimir Oltean }
15798aa9ebccSVladimir Oltean 
15808aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
15818aa9ebccSVladimir Oltean 				 struct net_device *br)
15828aa9ebccSVladimir Oltean {
15838aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
15848aa9ebccSVladimir Oltean }
15858aa9ebccSVladimir Oltean 
15862eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
15872eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
15882eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
15892eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
15902eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
1591c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
15922eea1fa8SVladimir Oltean };
15932eea1fa8SVladimir Oltean 
15946666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
15956666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
15966666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
15976666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
15986666cebcSVladimir Oltean  * such that this operation is relatively seamless.
15996666cebcSVladimir Oltean  */
16002eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
16012eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
16026666cebcSVladimir Oltean {
16036cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
16046cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
16056666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
16066666cebcSVladimir Oltean 	int speed_mbps[SJA1105_NUM_PORTS];
16076cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
16086cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
16096cf99c13SVladimir Oltean 	s64 t12, t34;
1610ffe10e67SVladimir Oltean 	u16 bmcr = 0;
16116666cebcSVladimir Oltean 	int rc, i;
16126cf99c13SVladimir Oltean 	s64 now;
16136666cebcSVladimir Oltean 
1614af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1615af580ae2SVladimir Oltean 
16166666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
16176666cebcSVladimir Oltean 
16188400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
16198400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
16208400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
16218400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
16226666cebcSVladimir Oltean 	 */
16236666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
16246666cebcSVladimir Oltean 		speed_mbps[i] = sja1105_speed[mac[i].speed];
16256666cebcSVladimir Oltean 		mac[i].speed = SJA1105_SPEED_AUTO;
16266666cebcSVladimir Oltean 	}
16276666cebcSVladimir Oltean 
1628ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT))
1629ffe10e67SVladimir Oltean 		bmcr = sja1105_sgmii_read(priv, MII_BMCR);
1630ffe10e67SVladimir Oltean 
16316cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
16326cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
16336cf99c13SVladimir Oltean 
16346cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
16356cf99c13SVladimir Oltean 	if (rc < 0)
16366cf99c13SVladimir Oltean 		goto out_unlock_ptp;
16376cf99c13SVladimir Oltean 
16386666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
16396666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
16406666cebcSVladimir Oltean 	if (rc < 0)
16416cf99c13SVladimir Oltean 		goto out_unlock_ptp;
16426cf99c13SVladimir Oltean 
16436cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
16446cf99c13SVladimir Oltean 	if (rc < 0)
16456cf99c13SVladimir Oltean 		goto out_unlock_ptp;
16466cf99c13SVladimir Oltean 
16476cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
16486cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
16496cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
16506cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
16516cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
16526cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
16536cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
16546cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
16556cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
16566cf99c13SVladimir Oltean 	now += (t34 - t12);
16576cf99c13SVladimir Oltean 
16586cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
16596cf99c13SVladimir Oltean 
16606cf99c13SVladimir Oltean out_unlock_ptp:
16616cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
16626666cebcSVladimir Oltean 
16632eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
16642eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
16652eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
16662eea1fa8SVladimir Oltean 
16676666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
16686666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
16696666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
16706666cebcSVladimir Oltean 	 */
16716666cebcSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
16726666cebcSVladimir Oltean 	if (rc < 0)
16736666cebcSVladimir Oltean 		goto out;
16746666cebcSVladimir Oltean 
16756666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
16768400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
16776666cebcSVladimir Oltean 		if (rc < 0)
16786666cebcSVladimir Oltean 			goto out;
16796666cebcSVladimir Oltean 	}
1680ffe10e67SVladimir Oltean 
1681ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) {
1682ffe10e67SVladimir Oltean 		bool an_enabled = !!(bmcr & BMCR_ANENABLE);
1683ffe10e67SVladimir Oltean 
1684ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, an_enabled, false);
1685ffe10e67SVladimir Oltean 
1686ffe10e67SVladimir Oltean 		if (!an_enabled) {
1687ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
1688ffe10e67SVladimir Oltean 
1689ffe10e67SVladimir Oltean 			if (bmcr & BMCR_SPEED1000)
1690ffe10e67SVladimir Oltean 				speed = SPEED_1000;
1691ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED100)
1692ffe10e67SVladimir Oltean 				speed = SPEED_100;
1693ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED10)
1694ffe10e67SVladimir Oltean 				speed = SPEED_10;
1695ffe10e67SVladimir Oltean 
1696ffe10e67SVladimir Oltean 			sja1105_sgmii_pcs_force_speed(priv, speed);
1697ffe10e67SVladimir Oltean 		}
1698ffe10e67SVladimir Oltean 	}
16996666cebcSVladimir Oltean out:
1700af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1701af580ae2SVladimir Oltean 
17026666cebcSVladimir Oltean 	return rc;
17036666cebcSVladimir Oltean }
17046666cebcSVladimir Oltean 
17056666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
17066666cebcSVladimir Oltean {
17076666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
17086666cebcSVladimir Oltean 
17096666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
17106666cebcSVladimir Oltean 
17116666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
17126666cebcSVladimir Oltean 
17136666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
17146666cebcSVladimir Oltean 					   &mac[port], true);
17156666cebcSVladimir Oltean }
17166666cebcSVladimir Oltean 
17176666cebcSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
17186666cebcSVladimir Oltean {
17196666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
17206666cebcSVladimir Oltean 	int count, i;
17216666cebcSVladimir Oltean 
17226666cebcSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
17236666cebcSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
17246666cebcSVladimir Oltean 
17256666cebcSVladimir Oltean 	for (i = 0; i < count; i++)
17266666cebcSVladimir Oltean 		if (vlan[i].vlanid == vid)
17276666cebcSVladimir Oltean 			return i;
17286666cebcSVladimir Oltean 
17296666cebcSVladimir Oltean 	/* Return an invalid entry index if not found */
17306666cebcSVladimir Oltean 	return -1;
17316666cebcSVladimir Oltean }
17326666cebcSVladimir Oltean 
17336666cebcSVladimir Oltean static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid,
17346666cebcSVladimir Oltean 			      bool enabled, bool untagged)
17356666cebcSVladimir Oltean {
17366666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
17376666cebcSVladimir Oltean 	struct sja1105_table *table;
17386666cebcSVladimir Oltean 	bool keep = true;
17396666cebcSVladimir Oltean 	int match, rc;
17406666cebcSVladimir Oltean 
17416666cebcSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
17426666cebcSVladimir Oltean 
17436666cebcSVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
17446666cebcSVladimir Oltean 	if (match < 0) {
17456666cebcSVladimir Oltean 		/* Can't delete a missing entry. */
17466666cebcSVladimir Oltean 		if (!enabled)
17476666cebcSVladimir Oltean 			return 0;
17486666cebcSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
17496666cebcSVladimir Oltean 		if (rc)
17506666cebcSVladimir Oltean 			return rc;
17516666cebcSVladimir Oltean 		match = table->entry_count - 1;
17526666cebcSVladimir Oltean 	}
17536666cebcSVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
17546666cebcSVladimir Oltean 	vlan = table->entries;
17556666cebcSVladimir Oltean 	vlan[match].vlanid = vid;
17566666cebcSVladimir Oltean 	if (enabled) {
17576666cebcSVladimir Oltean 		vlan[match].vlan_bc |= BIT(port);
17586666cebcSVladimir Oltean 		vlan[match].vmemb_port |= BIT(port);
17596666cebcSVladimir Oltean 	} else {
17606666cebcSVladimir Oltean 		vlan[match].vlan_bc &= ~BIT(port);
17616666cebcSVladimir Oltean 		vlan[match].vmemb_port &= ~BIT(port);
17626666cebcSVladimir Oltean 	}
17636666cebcSVladimir Oltean 	/* Also unset tag_port if removing this VLAN was requested,
17646666cebcSVladimir Oltean 	 * just so we don't have a confusing bitmap (no practical purpose).
17656666cebcSVladimir Oltean 	 */
17666666cebcSVladimir Oltean 	if (untagged || !enabled)
17676666cebcSVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
17686666cebcSVladimir Oltean 	else
17696666cebcSVladimir Oltean 		vlan[match].tag_port |= BIT(port);
17706666cebcSVladimir Oltean 	/* If there's no port left as member of this VLAN,
17716666cebcSVladimir Oltean 	 * it's time for it to go.
17726666cebcSVladimir Oltean 	 */
17736666cebcSVladimir Oltean 	if (!vlan[match].vmemb_port)
17746666cebcSVladimir Oltean 		keep = false;
17756666cebcSVladimir Oltean 
17766666cebcSVladimir Oltean 	dev_dbg(priv->ds->dev,
17776666cebcSVladimir Oltean 		"%s: port %d, vid %llu, broadcast domain 0x%llx, "
17786666cebcSVladimir Oltean 		"port members 0x%llx, tagged ports 0x%llx, keep %d\n",
17796666cebcSVladimir Oltean 		__func__, port, vlan[match].vlanid, vlan[match].vlan_bc,
17806666cebcSVladimir Oltean 		vlan[match].vmemb_port, vlan[match].tag_port, keep);
17816666cebcSVladimir Oltean 
17826666cebcSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
17836666cebcSVladimir Oltean 					  &vlan[match], keep);
17846666cebcSVladimir Oltean 	if (rc < 0)
17856666cebcSVladimir Oltean 		return rc;
17866666cebcSVladimir Oltean 
17876666cebcSVladimir Oltean 	if (!keep)
17886666cebcSVladimir Oltean 		return sja1105_table_delete_entry(table, match);
17896666cebcSVladimir Oltean 
17906666cebcSVladimir Oltean 	return 0;
17916666cebcSVladimir Oltean }
17926666cebcSVladimir Oltean 
1793227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1794227d07a0SVladimir Oltean {
1795227d07a0SVladimir Oltean 	int rc, i;
1796227d07a0SVladimir Oltean 
1797227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1798227d07a0SVladimir Oltean 		rc = dsa_port_setup_8021q_tagging(ds, i, enabled);
1799227d07a0SVladimir Oltean 		if (rc < 0) {
1800227d07a0SVladimir Oltean 			dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n",
1801227d07a0SVladimir Oltean 				i, rc);
1802227d07a0SVladimir Oltean 			return rc;
1803227d07a0SVladimir Oltean 		}
1804227d07a0SVladimir Oltean 	}
1805227d07a0SVladimir Oltean 	dev_info(ds->dev, "%s switch tagging\n",
1806227d07a0SVladimir Oltean 		 enabled ? "Enabled" : "Disabled");
1807227d07a0SVladimir Oltean 	return 0;
1808227d07a0SVladimir Oltean }
1809227d07a0SVladimir Oltean 
18108aa9ebccSVladimir Oltean static enum dsa_tag_protocol
18114d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
18124d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
18138aa9ebccSVladimir Oltean {
1814227d07a0SVladimir Oltean 	return DSA_TAG_PROTO_SJA1105;
18158aa9ebccSVladimir Oltean }
18168aa9ebccSVladimir Oltean 
18176666cebcSVladimir Oltean /* This callback needs to be present */
18186666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
18196666cebcSVladimir Oltean 				const struct switchdev_obj_port_vlan *vlan)
18206666cebcSVladimir Oltean {
18216666cebcSVladimir Oltean 	return 0;
18226666cebcSVladimir Oltean }
18236666cebcSVladimir Oltean 
1824070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
1825070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
1826070ca3bbSVladimir Oltean  * So a switch reset is required.
1827070ca3bbSVladimir Oltean  */
18286666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
18296666cebcSVladimir Oltean {
18306d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
1831070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
18326666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1833070ca3bbSVladimir Oltean 	struct sja1105_table *table;
1834070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
18356666cebcSVladimir Oltean 	int rc;
18366666cebcSVladimir Oltean 
1837070ca3bbSVladimir Oltean 	if (enabled) {
18386666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
183954fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
184054fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
1841070ca3bbSVladimir Oltean 	} else {
18426666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
1843070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
1844070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
1845070ca3bbSVladimir Oltean 	}
1846070ca3bbSVladimir Oltean 
1847070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1848070ca3bbSVladimir Oltean 	general_params = table->entries;
1849f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
185054fa49eeSVladimir Oltean 	general_params->tpid = tpid;
185154fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
1852070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
185342824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
185442824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
185542824463SVladimir Oltean 	 */
185642824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
185742824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
1858070ca3bbSVladimir Oltean 
18596d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
18606d7c7d94SVladimir Oltean 	 * No VLAN filtering => shared VLAN learning.
18616d7c7d94SVladimir Oltean 	 *
18626d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
18636d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
18646d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
18656d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
18666d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
18676d7c7d94SVladimir Oltean 	 * forwarding decision.
18686d7c7d94SVladimir Oltean 	 *
18696d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
18706d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
18716d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
18726d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
18736d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
18746d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
18756d7c7d94SVladimir Oltean 	 * (all frames get flooded).
18766d7c7d94SVladimir Oltean 	 */
18776d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
18786d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
18796d7c7d94SVladimir Oltean 	l2_lookup_params->shared_learn = !enabled;
18806d7c7d94SVladimir Oltean 
18812eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
18826666cebcSVladimir Oltean 	if (rc)
18836666cebcSVladimir Oltean 		dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
18846666cebcSVladimir Oltean 
1885227d07a0SVladimir Oltean 	/* Switch port identification based on 802.1Q is only passable
1886227d07a0SVladimir Oltean 	 * if we are not under a vlan_filtering bridge. So make sure
1887227d07a0SVladimir Oltean 	 * the two configurations are mutually exclusive.
1888227d07a0SVladimir Oltean 	 */
1889227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, !enabled);
18906666cebcSVladimir Oltean }
18916666cebcSVladimir Oltean 
18926666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port,
18936666cebcSVladimir Oltean 			     const struct switchdev_obj_port_vlan *vlan)
18946666cebcSVladimir Oltean {
18956666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18966666cebcSVladimir Oltean 	u16 vid;
18976666cebcSVladimir Oltean 	int rc;
18986666cebcSVladimir Oltean 
18996666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
19006666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags &
19016666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
19026666cebcSVladimir Oltean 		if (rc < 0) {
19036666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
19046666cebcSVladimir Oltean 				vid, port, rc);
19056666cebcSVladimir Oltean 			return;
19066666cebcSVladimir Oltean 		}
19076666cebcSVladimir Oltean 		if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
19086666cebcSVladimir Oltean 			rc = sja1105_pvid_apply(ds->priv, port, vid);
19096666cebcSVladimir Oltean 			if (rc < 0) {
19106666cebcSVladimir Oltean 				dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n",
19116666cebcSVladimir Oltean 					vid, port, rc);
19126666cebcSVladimir Oltean 				return;
19136666cebcSVladimir Oltean 			}
19146666cebcSVladimir Oltean 		}
19156666cebcSVladimir Oltean 	}
19166666cebcSVladimir Oltean }
19176666cebcSVladimir Oltean 
19186666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
19196666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
19206666cebcSVladimir Oltean {
19216666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
19226666cebcSVladimir Oltean 	u16 vid;
19236666cebcSVladimir Oltean 	int rc;
19246666cebcSVladimir Oltean 
19256666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
19266666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags &
19276666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
19286666cebcSVladimir Oltean 		if (rc < 0) {
19296666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
19306666cebcSVladimir Oltean 				vid, port, rc);
19316666cebcSVladimir Oltean 			return rc;
19326666cebcSVladimir Oltean 		}
19336666cebcSVladimir Oltean 	}
19346666cebcSVladimir Oltean 	return 0;
19356666cebcSVladimir Oltean }
19366666cebcSVladimir Oltean 
19378aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
19388aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
19398aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
19408aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
19418aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
19428aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
19438aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
19448aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
19458aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
19468aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
19478aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
19488aa9ebccSVladimir Oltean  */
19498aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
19508aa9ebccSVladimir Oltean {
19518aa9ebccSVladimir Oltean 	struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
19528aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
19538aa9ebccSVladimir Oltean 	int rc;
19548aa9ebccSVladimir Oltean 
19558aa9ebccSVladimir Oltean 	rc = sja1105_parse_dt(priv, ports);
19568aa9ebccSVladimir Oltean 	if (rc < 0) {
19578aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
19588aa9ebccSVladimir Oltean 		return rc;
19598aa9ebccSVladimir Oltean 	}
1960f5b8631cSVladimir Oltean 
1961f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
1962f5b8631cSVladimir Oltean 	 * and we can't apply them.
1963f5b8631cSVladimir Oltean 	 */
1964f5b8631cSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv, ports);
1965f5b8631cSVladimir Oltean 	if (rc < 0) {
1966f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
1967f5b8631cSVladimir Oltean 		return rc;
1968f5b8631cSVladimir Oltean 	}
1969f5b8631cSVladimir Oltean 
197061c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
1971bb77f36aSVladimir Oltean 	if (rc < 0) {
1972bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
1973bb77f36aSVladimir Oltean 		return rc;
1974bb77f36aSVladimir Oltean 	}
19758aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
19768aa9ebccSVladimir Oltean 	rc = sja1105_static_config_load(priv, ports);
19778aa9ebccSVladimir Oltean 	if (rc < 0) {
19788aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
19798aa9ebccSVladimir Oltean 		return rc;
19808aa9ebccSVladimir Oltean 	}
19818aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
19828aa9ebccSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
19838aa9ebccSVladimir Oltean 	if (rc < 0) {
19848aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
19858aa9ebccSVladimir Oltean 		return rc;
19868aa9ebccSVladimir Oltean 	}
19876666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
19886666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
19896666cebcSVladimir Oltean 	 * EtherType is.
19906666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
19916666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
19926666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
19936666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
19946666cebcSVladimir Oltean 	 */
19956666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
19968aa9ebccSVladimir Oltean 
19975f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
19985f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
19995f06c63bSVladimir Oltean 
2000c279c726SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
2001c279c726SVladimir Oltean 
2002227d07a0SVladimir Oltean 	/* The DSA/switchdev model brings up switch ports in standalone mode by
2003227d07a0SVladimir Oltean 	 * default, and that means vlan_filtering is 0 since they're not under
2004227d07a0SVladimir Oltean 	 * a bridge, so it's safe to set up switch tagging at this time.
2005227d07a0SVladimir Oltean 	 */
2006227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, true);
2007227d07a0SVladimir Oltean }
2008227d07a0SVladimir Oltean 
2009f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
2010f3097be2SVladimir Oltean {
2011f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2012a68578c2SVladimir Oltean 	int port;
2013a68578c2SVladimir Oltean 
2014a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2015a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2016a68578c2SVladimir Oltean 
2017a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2018a68578c2SVladimir Oltean 			continue;
2019a68578c2SVladimir Oltean 
202052c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
2021a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
2022a68578c2SVladimir Oltean 	}
2023f3097be2SVladimir Oltean 
2024317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
202561c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
20266cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2027f3097be2SVladimir Oltean }
2028f3097be2SVladimir Oltean 
2029e9bf9694SVladimir Oltean static int sja1105_port_enable(struct dsa_switch *ds, int port,
2030e9bf9694SVladimir Oltean 			       struct phy_device *phy)
2031e9bf9694SVladimir Oltean {
2032e9bf9694SVladimir Oltean 	struct net_device *slave;
2033e9bf9694SVladimir Oltean 
2034e9bf9694SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2035e9bf9694SVladimir Oltean 		return 0;
2036e9bf9694SVladimir Oltean 
203768bb8ea8SVivien Didelot 	slave = dsa_to_port(ds, port)->slave;
2038e9bf9694SVladimir Oltean 
2039e9bf9694SVladimir Oltean 	slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
2040e9bf9694SVladimir Oltean 
2041e9bf9694SVladimir Oltean 	return 0;
2042e9bf9694SVladimir Oltean }
2043e9bf9694SVladimir Oltean 
2044a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
2045a68578c2SVladimir Oltean {
2046a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2047a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
2048a68578c2SVladimir Oltean 
2049a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2050a68578c2SVladimir Oltean 		return;
2051a68578c2SVladimir Oltean 
2052a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
2053a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
2054a68578c2SVladimir Oltean }
2055a68578c2SVladimir Oltean 
2056227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
205747ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2058227d07a0SVladimir Oltean {
2059227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2060227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2061227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2062227d07a0SVladimir Oltean 	int timeout = 10;
2063227d07a0SVladimir Oltean 	int rc;
2064227d07a0SVladimir Oltean 
2065227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2066227d07a0SVladimir Oltean 
2067227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2068227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2069227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
207047ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
207147ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2072227d07a0SVladimir Oltean 
2073227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2074227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2075227d07a0SVladimir Oltean 	if (rc < 0) {
2076227d07a0SVladimir Oltean 		kfree_skb(skb);
2077227d07a0SVladimir Oltean 		return rc;
2078227d07a0SVladimir Oltean 	}
2079227d07a0SVladimir Oltean 
2080227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
208168bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2082227d07a0SVladimir Oltean 
2083227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2084227d07a0SVladimir Oltean 	do {
2085227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2086227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2087227d07a0SVladimir Oltean 		if (rc < 0) {
2088227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2089227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2090227d07a0SVladimir Oltean 			continue;
2091227d07a0SVladimir Oltean 		}
2092227d07a0SVladimir Oltean 
2093227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2094227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2095227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2096227d07a0SVladimir Oltean 		 */
2097227d07a0SVladimir Oltean 		cpu_relax();
2098227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2099227d07a0SVladimir Oltean 
2100227d07a0SVladimir Oltean 	if (!timeout) {
2101227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2102227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
21032a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
21042a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2105227d07a0SVladimir Oltean 		 */
2106227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2107227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2108227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2109227d07a0SVladimir Oltean 	}
2110227d07a0SVladimir Oltean 
2111227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2112227d07a0SVladimir Oltean }
2113227d07a0SVladimir Oltean 
2114a68578c2SVladimir Oltean #define work_to_port(work) \
2115a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
2116a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
2117a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
2118a68578c2SVladimir Oltean 
2119227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2120227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2121227d07a0SVladimir Oltean  * lock on the bus)
2122227d07a0SVladimir Oltean  */
2123a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2124227d07a0SVladimir Oltean {
2125a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
2126a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
2127a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
2128a68578c2SVladimir Oltean 	int port = sp - priv->ports;
2129a68578c2SVladimir Oltean 	struct sk_buff *skb;
2130a68578c2SVladimir Oltean 
2131a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
2132a68578c2SVladimir Oltean 		struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
2133227d07a0SVladimir Oltean 
2134227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
2135227d07a0SVladimir Oltean 
2136a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
2137a68578c2SVladimir Oltean 
213847ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
2139a68578c2SVladimir Oltean 		if (clone)
2140a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
2141227d07a0SVladimir Oltean 
2142227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
2143a68578c2SVladimir Oltean 	}
21448aa9ebccSVladimir Oltean }
21458aa9ebccSVladimir Oltean 
21468456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
21478456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
21488456721dSVladimir Oltean  */
21498456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
21508456721dSVladimir Oltean 				   unsigned int ageing_time)
21518456721dSVladimir Oltean {
21528456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
21538456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
21548456721dSVladimir Oltean 	struct sja1105_table *table;
21558456721dSVladimir Oltean 	unsigned int maxage;
21568456721dSVladimir Oltean 
21578456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
21588456721dSVladimir Oltean 	l2_lookup_params = table->entries;
21598456721dSVladimir Oltean 
21608456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
21618456721dSVladimir Oltean 
21628456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
21638456721dSVladimir Oltean 		return 0;
21648456721dSVladimir Oltean 
21658456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
21668456721dSVladimir Oltean 
21672eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
21688456721dSVladimir Oltean }
21698456721dSVladimir Oltean 
2170c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
2171c279c726SVladimir Oltean {
2172c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2173c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2174c279c726SVladimir Oltean 
2175c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
2176c279c726SVladimir Oltean 
2177c279c726SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
2178c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
2179c279c726SVladimir Oltean 
2180c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2181c279c726SVladimir Oltean 
2182*a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
2183c279c726SVladimir Oltean 		return 0;
2184c279c726SVladimir Oltean 
2185*a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
2186c279c726SVladimir Oltean 
2187c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2188c279c726SVladimir Oltean }
2189c279c726SVladimir Oltean 
2190c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
2191c279c726SVladimir Oltean {
2192c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
2193c279c726SVladimir Oltean }
2194c279c726SVladimir Oltean 
2195317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2196317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2197317ab5b8SVladimir Oltean 				 void *type_data)
2198317ab5b8SVladimir Oltean {
2199317ab5b8SVladimir Oltean 	switch (type) {
2200317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2201317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
2202317ab5b8SVladimir Oltean 	default:
2203317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2204317ab5b8SVladimir Oltean 	}
2205317ab5b8SVladimir Oltean }
2206317ab5b8SVladimir Oltean 
2207511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2208511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2209511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2210511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2211511e6ca0SVladimir Oltean  * mirroring rule that references it.
2212511e6ca0SVladimir Oltean  */
2213511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2214511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2215511e6ca0SVladimir Oltean {
2216511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2217511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2218511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2219511e6ca0SVladimir Oltean 	bool already_enabled;
2220511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2221511e6ca0SVladimir Oltean 	int rc;
2222511e6ca0SVladimir Oltean 
2223511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2224511e6ca0SVladimir Oltean 	general_params = table->entries;
2225511e6ca0SVladimir Oltean 
2226511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2227511e6ca0SVladimir Oltean 
2228511e6ca0SVladimir Oltean 	already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS);
2229511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2230511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2231511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2232511e6ca0SVladimir Oltean 			general_params->mirr_port);
2233511e6ca0SVladimir Oltean 		return -EBUSY;
2234511e6ca0SVladimir Oltean 	}
2235511e6ca0SVladimir Oltean 
2236511e6ca0SVladimir Oltean 	new_mirr_port = to;
2237511e6ca0SVladimir Oltean 	if (!enabled) {
2238511e6ca0SVladimir Oltean 		bool keep = false;
2239511e6ca0SVladimir Oltean 		int port;
2240511e6ca0SVladimir Oltean 
2241511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2242511e6ca0SVladimir Oltean 		for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2243511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2244511e6ca0SVladimir Oltean 				keep = true;
2245511e6ca0SVladimir Oltean 				break;
2246511e6ca0SVladimir Oltean 			}
2247511e6ca0SVladimir Oltean 		}
2248511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2249511e6ca0SVladimir Oltean 		if (!keep)
2250511e6ca0SVladimir Oltean 			new_mirr_port = SJA1105_NUM_PORTS;
2251511e6ca0SVladimir Oltean 	}
2252511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2253511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2254511e6ca0SVladimir Oltean 
2255511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2256511e6ca0SVladimir Oltean 						  0, general_params, true);
2257511e6ca0SVladimir Oltean 		if (rc < 0)
2258511e6ca0SVladimir Oltean 			return rc;
2259511e6ca0SVladimir Oltean 	}
2260511e6ca0SVladimir Oltean 
2261511e6ca0SVladimir Oltean 	if (ingress)
2262511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2263511e6ca0SVladimir Oltean 	else
2264511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2265511e6ca0SVladimir Oltean 
2266511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2267511e6ca0SVladimir Oltean 					    &mac[from], true);
2268511e6ca0SVladimir Oltean }
2269511e6ca0SVladimir Oltean 
2270511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2271511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2272511e6ca0SVladimir Oltean 			      bool ingress)
2273511e6ca0SVladimir Oltean {
2274511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2275511e6ca0SVladimir Oltean 				    ingress, true);
2276511e6ca0SVladimir Oltean }
2277511e6ca0SVladimir Oltean 
2278511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2279511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2280511e6ca0SVladimir Oltean {
2281511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2282511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2283511e6ca0SVladimir Oltean }
2284511e6ca0SVladimir Oltean 
2285*a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
2286*a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
2287*a7cc081cSVladimir Oltean {
2288*a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2289*a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2290*a7cc081cSVladimir Oltean 
2291*a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2292*a7cc081cSVladimir Oltean 
2293*a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
2294*a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
2295*a7cc081cSVladimir Oltean 	 * bytes.
2296*a7cc081cSVladimir Oltean 	 */
2297*a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
2298*a7cc081cSVladimir Oltean 				      1000000);
2299*a7cc081cSVladimir Oltean 	policing[port].smax = div_u64(policer->rate_bytes_per_sec *
2300*a7cc081cSVladimir Oltean 				      PSCHED_NS2TICKS(policer->burst),
2301*a7cc081cSVladimir Oltean 				      PSCHED_TICKS_PER_SEC);
2302*a7cc081cSVladimir Oltean 
2303*a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2304*a7cc081cSVladimir Oltean }
2305*a7cc081cSVladimir Oltean 
2306*a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
2307*a7cc081cSVladimir Oltean {
2308*a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2309*a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2310*a7cc081cSVladimir Oltean 
2311*a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2312*a7cc081cSVladimir Oltean 
2313*a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
2314*a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
2315*a7cc081cSVladimir Oltean 
2316*a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2317*a7cc081cSVladimir Oltean }
2318*a7cc081cSVladimir Oltean 
23198aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
23208aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
23218aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
2322f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
23238456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
2324c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
2325c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
2326ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
2327ffe10e67SVladimir Oltean 	.phylink_mac_link_state	= sja1105_mac_pcs_get_state,
2328af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
23298400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
23308400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
233152c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
233252c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
233352c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
2334bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
2335e9bf9694SVladimir Oltean 	.port_enable		= sja1105_port_enable,
2336a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
2337291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
2338291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
2339291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
23408aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
23418aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
2342640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
23436666cebcSVladimir Oltean 	.port_vlan_prepare	= sja1105_vlan_prepare,
23446666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
23456666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
23466666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
2347291d1e72SVladimir Oltean 	.port_mdb_prepare	= sja1105_mdb_prepare,
2348291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
2349291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
2350a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
2351a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
2352f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
235347ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
2354317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
2355511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
2356511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
2357*a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
2358*a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
23598aa9ebccSVladimir Oltean };
23608aa9ebccSVladimir Oltean 
23618aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
23628aa9ebccSVladimir Oltean {
23638aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
23648aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
23658aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
2366dff79620SVladimir Oltean 	u32 device_id;
23678aa9ebccSVladimir Oltean 	u64 part_no;
23688aa9ebccSVladimir Oltean 	int rc;
23698aa9ebccSVladimir Oltean 
237034d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
237134d76e9fSVladimir Oltean 			      NULL);
23728aa9ebccSVladimir Oltean 	if (rc < 0)
23738aa9ebccSVladimir Oltean 		return rc;
23748aa9ebccSVladimir Oltean 
23758aa9ebccSVladimir Oltean 	if (device_id != priv->info->device_id) {
2376dff79620SVladimir Oltean 		dev_err(dev, "Expected device ID 0x%llx but read 0x%x\n",
23778aa9ebccSVladimir Oltean 			priv->info->device_id, device_id);
23788aa9ebccSVladimir Oltean 		return -ENODEV;
23798aa9ebccSVladimir Oltean 	}
23808aa9ebccSVladimir Oltean 
23811bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
23821bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
23838aa9ebccSVladimir Oltean 	if (rc < 0)
23848aa9ebccSVladimir Oltean 		return rc;
23858aa9ebccSVladimir Oltean 
23868aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
23878aa9ebccSVladimir Oltean 
23888aa9ebccSVladimir Oltean 	if (part_no != priv->info->part_no) {
23898aa9ebccSVladimir Oltean 		dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n",
23908aa9ebccSVladimir Oltean 			priv->info->part_no, part_no);
23918aa9ebccSVladimir Oltean 		return -ENODEV;
23928aa9ebccSVladimir Oltean 	}
23938aa9ebccSVladimir Oltean 
23948aa9ebccSVladimir Oltean 	return 0;
23958aa9ebccSVladimir Oltean }
23968aa9ebccSVladimir Oltean 
23978aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
23988aa9ebccSVladimir Oltean {
2399844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
24008aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
24018aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
24028aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
2403a68578c2SVladimir Oltean 	int rc, port;
24048aa9ebccSVladimir Oltean 
24058aa9ebccSVladimir Oltean 	if (!dev->of_node) {
24068aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
24078aa9ebccSVladimir Oltean 		return -EINVAL;
24088aa9ebccSVladimir Oltean 	}
24098aa9ebccSVladimir Oltean 
24108aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
24118aa9ebccSVladimir Oltean 	if (!priv)
24128aa9ebccSVladimir Oltean 		return -ENOMEM;
24138aa9ebccSVladimir Oltean 
24148aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
24158aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
24168aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
24178aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
24188aa9ebccSVladimir Oltean 	else
24198aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
24208aa9ebccSVladimir Oltean 
24218aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
24228aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
24238aa9ebccSVladimir Oltean 	 */
24248aa9ebccSVladimir Oltean 	priv->spidev = spi;
24258aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
24268aa9ebccSVladimir Oltean 
24278aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
24288aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
24298aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
24308aa9ebccSVladimir Oltean 	if (rc < 0) {
24318aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
24328aa9ebccSVladimir Oltean 		return rc;
24338aa9ebccSVladimir Oltean 	}
24348aa9ebccSVladimir Oltean 
24358aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
24368aa9ebccSVladimir Oltean 
24378aa9ebccSVladimir Oltean 	/* Detect hardware device */
24388aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
24398aa9ebccSVladimir Oltean 	if (rc < 0) {
24408aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
24418aa9ebccSVladimir Oltean 		return rc;
24428aa9ebccSVladimir Oltean 	}
24438aa9ebccSVladimir Oltean 
24448aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
24458aa9ebccSVladimir Oltean 
24467e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
24478aa9ebccSVladimir Oltean 	if (!ds)
24488aa9ebccSVladimir Oltean 		return -ENOMEM;
24498aa9ebccSVladimir Oltean 
24507e99e347SVivien Didelot 	ds->dev = dev;
24517e99e347SVivien Didelot 	ds->num_ports = SJA1105_NUM_PORTS;
24528aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
24538aa9ebccSVladimir Oltean 	ds->priv = priv;
24548aa9ebccSVladimir Oltean 	priv->ds = ds;
24558aa9ebccSVladimir Oltean 
2456844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
2457844d7edcSVladimir Oltean 
2458d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
2459d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
2460d5a619bfSVivien Didelot 
2461d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
2462d5a619bfSVivien Didelot 
2463d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
2464d5a619bfSVivien Didelot 	if (rc)
2465d5a619bfSVivien Didelot 		return rc;
2466d5a619bfSVivien Didelot 
2467227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
2468a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2469a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2470a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
2471a68578c2SVladimir Oltean 		struct net_device *slave;
2472227d07a0SVladimir Oltean 
2473a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2474a68578c2SVladimir Oltean 			continue;
2475a68578c2SVladimir Oltean 
2476a68578c2SVladimir Oltean 		dp->priv = sp;
2477a68578c2SVladimir Oltean 		sp->dp = dp;
2478844d7edcSVladimir Oltean 		sp->data = tagger_data;
2479a68578c2SVladimir Oltean 		slave = dp->slave;
2480a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
2481a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
2482a68578c2SVladimir Oltean 							slave->name);
2483a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
2484a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
2485a68578c2SVladimir Oltean 			dev_err(ds->dev,
2486a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
2487a68578c2SVladimir Oltean 				rc);
2488a68578c2SVladimir Oltean 			goto out;
2489a68578c2SVladimir Oltean 		}
2490a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
2491227d07a0SVladimir Oltean 	}
2492227d07a0SVladimir Oltean 
2493d5a619bfSVivien Didelot 	return 0;
2494a68578c2SVladimir Oltean out:
2495a68578c2SVladimir Oltean 	while (port-- > 0) {
2496a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2497a68578c2SVladimir Oltean 
2498a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2499a68578c2SVladimir Oltean 			continue;
2500a68578c2SVladimir Oltean 
2501a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
2502a68578c2SVladimir Oltean 	}
2503a68578c2SVladimir Oltean 	return rc;
25048aa9ebccSVladimir Oltean }
25058aa9ebccSVladimir Oltean 
25068aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
25078aa9ebccSVladimir Oltean {
25088aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
25098aa9ebccSVladimir Oltean 
25108aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
25118aa9ebccSVladimir Oltean 	return 0;
25128aa9ebccSVladimir Oltean }
25138aa9ebccSVladimir Oltean 
25148aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
25158aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
25168aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
25178aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
25188aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
25198aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
25208aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
25218aa9ebccSVladimir Oltean 	{ /* sentinel */ },
25228aa9ebccSVladimir Oltean };
25238aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
25248aa9ebccSVladimir Oltean 
25258aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
25268aa9ebccSVladimir Oltean 	.driver = {
25278aa9ebccSVladimir Oltean 		.name  = "sja1105",
25288aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
25298aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
25308aa9ebccSVladimir Oltean 	},
25318aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
25328aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
25338aa9ebccSVladimir Oltean };
25348aa9ebccSVladimir Oltean 
25358aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
25368aa9ebccSVladimir Oltean 
25378aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
25388aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
25398aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
25408aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
2541