xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 38b5beeae7a4cde87edabb0196fac1f55ae668ee)
18aa9ebccSVladimir Oltean // SPDX-License-Identifier: GPL-2.0
28aa9ebccSVladimir Oltean /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
38aa9ebccSVladimir Oltean  * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
48aa9ebccSVladimir Oltean  */
58aa9ebccSVladimir Oltean 
68aa9ebccSVladimir Oltean #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
78aa9ebccSVladimir Oltean 
88aa9ebccSVladimir Oltean #include <linux/delay.h>
98aa9ebccSVladimir Oltean #include <linux/module.h>
108aa9ebccSVladimir Oltean #include <linux/printk.h>
118aa9ebccSVladimir Oltean #include <linux/spi/spi.h>
128aa9ebccSVladimir Oltean #include <linux/errno.h>
138aa9ebccSVladimir Oltean #include <linux/gpio/consumer.h>
14ad9f299aSVladimir Oltean #include <linux/phylink.h>
158aa9ebccSVladimir Oltean #include <linux/of.h>
168aa9ebccSVladimir Oltean #include <linux/of_net.h>
178aa9ebccSVladimir Oltean #include <linux/of_mdio.h>
188aa9ebccSVladimir Oltean #include <linux/of_device.h>
198aa9ebccSVladimir Oltean #include <linux/netdev_features.h>
208aa9ebccSVladimir Oltean #include <linux/netdevice.h>
218aa9ebccSVladimir Oltean #include <linux/if_bridge.h>
228aa9ebccSVladimir Oltean #include <linux/if_ether.h>
23227d07a0SVladimir Oltean #include <linux/dsa/8021q.h>
248aa9ebccSVladimir Oltean #include "sja1105.h"
25ffe10e67SVladimir Oltean #include "sja1105_sgmii.h"
26317ab5b8SVladimir Oltean #include "sja1105_tas.h"
278aa9ebccSVladimir Oltean 
28ac02a451SVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops;
29ac02a451SVladimir Oltean 
308aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
318aa9ebccSVladimir Oltean 			     unsigned int startup_delay)
328aa9ebccSVladimir Oltean {
338aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
348aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
358aa9ebccSVladimir Oltean 	msleep(pulse_len);
368aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
378aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
388aa9ebccSVladimir Oltean 	msleep(startup_delay);
398aa9ebccSVladimir Oltean }
408aa9ebccSVladimir Oltean 
418aa9ebccSVladimir Oltean static void
428aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
438aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
448aa9ebccSVladimir Oltean {
458aa9ebccSVladimir Oltean 	if (allow) {
468aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  |= BIT(to);
478aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
488aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  |= BIT(to);
498aa9ebccSVladimir Oltean 	} else {
508aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  &= ~BIT(to);
518aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
528aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  &= ~BIT(to);
538aa9ebccSVladimir Oltean 	}
548aa9ebccSVladimir Oltean }
558aa9ebccSVladimir Oltean 
568aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree
578aa9ebccSVladimir Oltean  * settings into sja1105_setup
588aa9ebccSVladimir Oltean  */
598aa9ebccSVladimir Oltean struct sja1105_dt_port {
608aa9ebccSVladimir Oltean 	phy_interface_t phy_mode;
618aa9ebccSVladimir Oltean 	sja1105_mii_role_t role;
628aa9ebccSVladimir Oltean };
638aa9ebccSVladimir Oltean 
648aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
658aa9ebccSVladimir Oltean {
668aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
678aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
688aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
698aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
708aa9ebccSVladimir Oltean 		 */
718aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
728aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
738aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
748aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
758aa9ebccSVladimir Oltean 		.ifg = 0,
768aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
771fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
788aa9ebccSVladimir Oltean 		 */
798aa9ebccSVladimir Oltean 		.speed = SJA1105_SPEED_AUTO,
808aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
818aa9ebccSVladimir Oltean 		.tp_delin = 0,
828aa9ebccSVladimir Oltean 		.tp_delout = 0,
838aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
848aa9ebccSVladimir Oltean 		.maxage = 0xFF,
858aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
868aa9ebccSVladimir Oltean 		.vlanprio = 0,
87e3502b82SVladimir Oltean 		.vlanid = 1,
888aa9ebccSVladimir Oltean 		.ing_mirr = false,
898aa9ebccSVladimir Oltean 		.egr_mirr = false,
908aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
918aa9ebccSVladimir Oltean 		.drpnona664 = false,
928aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
938aa9ebccSVladimir Oltean 		.drpdtag = false,
948aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
958aa9ebccSVladimir Oltean 		.drpuntag = false,
968aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
978aa9ebccSVladimir Oltean 		.retag = false,
98640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
99640f763fSVladimir Oltean 		 * STP will enable it.
100640f763fSVladimir Oltean 		 */
101640f763fSVladimir Oltean 		.dyn_learn = false,
1028aa9ebccSVladimir Oltean 		.egress = false,
1038aa9ebccSVladimir Oltean 		.ingress = false,
1048aa9ebccSVladimir Oltean 	};
1058aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1068aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1078aa9ebccSVladimir Oltean 	int i;
1088aa9ebccSVladimir Oltean 
1098aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1108aa9ebccSVladimir Oltean 
1118aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1128aa9ebccSVladimir Oltean 	if (table->entry_count) {
1138aa9ebccSVladimir Oltean 		kfree(table->entries);
1148aa9ebccSVladimir Oltean 		table->entry_count = 0;
1158aa9ebccSVladimir Oltean 	}
1168aa9ebccSVladimir Oltean 
1178aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_NUM_PORTS,
1188aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1198aa9ebccSVladimir Oltean 	if (!table->entries)
1208aa9ebccSVladimir Oltean 		return -ENOMEM;
1218aa9ebccSVladimir Oltean 
1228aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_NUM_PORTS;
1238aa9ebccSVladimir Oltean 
1248aa9ebccSVladimir Oltean 	mac = table->entries;
1258aa9ebccSVladimir Oltean 
126640f763fSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1278aa9ebccSVladimir Oltean 		mac[i] = default_mac;
128640f763fSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, i)) {
129640f763fSVladimir Oltean 			/* STP doesn't get called for CPU port, so we need to
130640f763fSVladimir Oltean 			 * set the I/O parameters statically.
131640f763fSVladimir Oltean 			 */
132640f763fSVladimir Oltean 			mac[i].dyn_learn = true;
133640f763fSVladimir Oltean 			mac[i].ingress = true;
134640f763fSVladimir Oltean 			mac[i].egress = true;
135640f763fSVladimir Oltean 		}
136640f763fSVladimir Oltean 	}
1378aa9ebccSVladimir Oltean 
1388aa9ebccSVladimir Oltean 	return 0;
1398aa9ebccSVladimir Oltean }
1408aa9ebccSVladimir Oltean 
141ffe10e67SVladimir Oltean static bool sja1105_supports_sgmii(struct sja1105_private *priv, int port)
142ffe10e67SVladimir Oltean {
143ffe10e67SVladimir Oltean 	if (priv->info->part_no != SJA1105R_PART_NO &&
144ffe10e67SVladimir Oltean 	    priv->info->part_no != SJA1105S_PART_NO)
145ffe10e67SVladimir Oltean 		return false;
146ffe10e67SVladimir Oltean 
147ffe10e67SVladimir Oltean 	if (port != SJA1105_SGMII_PORT)
148ffe10e67SVladimir Oltean 		return false;
149ffe10e67SVladimir Oltean 
150ffe10e67SVladimir Oltean 	if (dsa_is_unused_port(priv->ds, port))
151ffe10e67SVladimir Oltean 		return false;
152ffe10e67SVladimir Oltean 
153ffe10e67SVladimir Oltean 	return true;
154ffe10e67SVladimir Oltean }
155ffe10e67SVladimir Oltean 
1568aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv,
1578aa9ebccSVladimir Oltean 				     struct sja1105_dt_port *ports)
1588aa9ebccSVladimir Oltean {
1598aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1608aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1618aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1628aa9ebccSVladimir Oltean 	int i;
1638aa9ebccSVladimir Oltean 
1648aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1658aa9ebccSVladimir Oltean 
1668aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1678aa9ebccSVladimir Oltean 	if (table->entry_count) {
1688aa9ebccSVladimir Oltean 		kfree(table->entries);
1698aa9ebccSVladimir Oltean 		table->entry_count = 0;
1708aa9ebccSVladimir Oltean 	}
1718aa9ebccSVladimir Oltean 
1728aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
1738aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1748aa9ebccSVladimir Oltean 	if (!table->entries)
1758aa9ebccSVladimir Oltean 		return -ENOMEM;
1768aa9ebccSVladimir Oltean 
1771fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
1788aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
1798aa9ebccSVladimir Oltean 
1808aa9ebccSVladimir Oltean 	mii = table->entries;
1818aa9ebccSVladimir Oltean 
1828aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
183ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
184ee9d0cb6SVladimir Oltean 			continue;
185ee9d0cb6SVladimir Oltean 
1868aa9ebccSVladimir Oltean 		switch (ports[i].phy_mode) {
1878aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
1888aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1898aa9ebccSVladimir Oltean 			break;
1908aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
1918aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
1928aa9ebccSVladimir Oltean 			break;
1938aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
1948aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
1958aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
1968aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
1978aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
1988aa9ebccSVladimir Oltean 			break;
199ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
200ffe10e67SVladimir Oltean 			if (!sja1105_supports_sgmii(priv, i))
201ffe10e67SVladimir Oltean 				return -EINVAL;
202ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
203ffe10e67SVladimir Oltean 			break;
2048aa9ebccSVladimir Oltean 		default:
2058aa9ebccSVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s!\n",
2068aa9ebccSVladimir Oltean 				phy_modes(ports[i].phy_mode));
2078aa9ebccSVladimir Oltean 		}
2088aa9ebccSVladimir Oltean 
209ffe10e67SVladimir Oltean 		/* Even though the SerDes port is able to drive SGMII autoneg
210ffe10e67SVladimir Oltean 		 * like a PHY would, from the perspective of the XMII tables,
211ffe10e67SVladimir Oltean 		 * the SGMII port should always be put in MAC mode.
212ffe10e67SVladimir Oltean 		 */
213ffe10e67SVladimir Oltean 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII)
214ffe10e67SVladimir Oltean 			mii->phy_mac[i] = XMII_MAC;
215ffe10e67SVladimir Oltean 		else
2168aa9ebccSVladimir Oltean 			mii->phy_mac[i] = ports[i].role;
2178aa9ebccSVladimir Oltean 	}
2188aa9ebccSVladimir Oltean 	return 0;
2198aa9ebccSVladimir Oltean }
2208aa9ebccSVladimir Oltean 
2218aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
2228aa9ebccSVladimir Oltean {
2238aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2248aa9ebccSVladimir Oltean 
2258aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
2268aa9ebccSVladimir Oltean 
227291d1e72SVladimir Oltean 	/* We only populate the FDB table through dynamic
228291d1e72SVladimir Oltean 	 * L2 Address Lookup entries
229291d1e72SVladimir Oltean 	 */
2308aa9ebccSVladimir Oltean 	if (table->entry_count) {
2318aa9ebccSVladimir Oltean 		kfree(table->entries);
2328aa9ebccSVladimir Oltean 		table->entry_count = 0;
2338aa9ebccSVladimir Oltean 	}
2348aa9ebccSVladimir Oltean 	return 0;
2358aa9ebccSVladimir Oltean }
2368aa9ebccSVladimir Oltean 
2378aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
2388aa9ebccSVladimir Oltean {
2398aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2406c56e167SVladimir Oltean 	u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
2418aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
2428456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
2438456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
2448aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
2458aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
2461da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
2471da73821SVladimir Oltean 		.start_dynspc = 0,
2486c56e167SVladimir Oltean 		.maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
2496c56e167SVladimir Oltean 			     max_fdb_entries, max_fdb_entries, },
2508aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
2518aa9ebccSVladimir Oltean 		.poly = 0x97,
2528aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
2538aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
2548aa9ebccSVladimir Oltean 		 */
2556d7c7d94SVladimir Oltean 		.shared_learn = true,
2568aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
2578aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
2588aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
2598aa9ebccSVladimir Oltean 		 */
2608aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
2618aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
2628aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
2638aa9ebccSVladimir Oltean 		 */
2648aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
2651da73821SVladimir Oltean 		/* P/Q/R/S only */
2661da73821SVladimir Oltean 		.use_static = true,
2671da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
2681da73821SVladimir Oltean 		 * dynamic FDB entries
2691da73821SVladimir Oltean 		 */
2701da73821SVladimir Oltean 		.owr_dyn = true,
2711da73821SVladimir Oltean 		.drpnolearn = true,
2728aa9ebccSVladimir Oltean 	};
2738aa9ebccSVladimir Oltean 
2748aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2758aa9ebccSVladimir Oltean 
2768aa9ebccSVladimir Oltean 	if (table->entry_count) {
2778aa9ebccSVladimir Oltean 		kfree(table->entries);
2788aa9ebccSVladimir Oltean 		table->entry_count = 0;
2798aa9ebccSVladimir Oltean 	}
2808aa9ebccSVladimir Oltean 
2818aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
2828aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2838aa9ebccSVladimir Oltean 	if (!table->entries)
2848aa9ebccSVladimir Oltean 		return -ENOMEM;
2858aa9ebccSVladimir Oltean 
2868aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
2878aa9ebccSVladimir Oltean 
2888aa9ebccSVladimir Oltean 	/* This table only has a single entry */
2898aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
2908aa9ebccSVladimir Oltean 				default_l2_lookup_params;
2918aa9ebccSVladimir Oltean 
2928aa9ebccSVladimir Oltean 	return 0;
2938aa9ebccSVladimir Oltean }
2948aa9ebccSVladimir Oltean 
2958aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
2968aa9ebccSVladimir Oltean {
2978aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2988aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
2998aa9ebccSVladimir Oltean 		.ving_mirr = 0,
3008aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
3018aa9ebccSVladimir Oltean 		.vmemb_port = 0,
3028aa9ebccSVladimir Oltean 		.vlan_bc = 0,
3038aa9ebccSVladimir Oltean 		.tag_port = 0,
304e3502b82SVladimir Oltean 		.vlanid = 1,
3058aa9ebccSVladimir Oltean 	};
306ec5ae610SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
307ec5ae610SVladimir Oltean 	int port;
3088aa9ebccSVladimir Oltean 
3098aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
3108aa9ebccSVladimir Oltean 
311e3502b82SVladimir Oltean 	/* The static VLAN table will only contain the initial pvid of 1.
3126666cebcSVladimir Oltean 	 * All other VLANs are to be configured through dynamic entries,
3136666cebcSVladimir Oltean 	 * and kept in the static configuration table as backing memory.
3148aa9ebccSVladimir Oltean 	 */
3158aa9ebccSVladimir Oltean 	if (table->entry_count) {
3168aa9ebccSVladimir Oltean 		kfree(table->entries);
3178aa9ebccSVladimir Oltean 		table->entry_count = 0;
3188aa9ebccSVladimir Oltean 	}
3198aa9ebccSVladimir Oltean 
3208aa9ebccSVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
3218aa9ebccSVladimir Oltean 				 GFP_KERNEL);
3228aa9ebccSVladimir Oltean 	if (!table->entries)
3238aa9ebccSVladimir Oltean 		return -ENOMEM;
3248aa9ebccSVladimir Oltean 
3258aa9ebccSVladimir Oltean 	table->entry_count = 1;
3268aa9ebccSVladimir Oltean 
327e3502b82SVladimir Oltean 	/* VLAN 1: all DT-defined ports are members; no restrictions on
328ec5ae610SVladimir Oltean 	 * forwarding; always transmit as untagged.
3298aa9ebccSVladimir Oltean 	 */
330ec5ae610SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
331ec5ae610SVladimir Oltean 		struct sja1105_bridge_vlan *v;
332ec5ae610SVladimir Oltean 
333ec5ae610SVladimir Oltean 		if (dsa_is_unused_port(ds, port))
334ec5ae610SVladimir Oltean 			continue;
335ec5ae610SVladimir Oltean 
336ec5ae610SVladimir Oltean 		pvid.vmemb_port |= BIT(port);
337ec5ae610SVladimir Oltean 		pvid.vlan_bc |= BIT(port);
338ec5ae610SVladimir Oltean 		pvid.tag_port &= ~BIT(port);
339ec5ae610SVladimir Oltean 
340ec5ae610SVladimir Oltean 		/* Let traffic that don't need dsa_8021q (e.g. STP, PTP) be
341ec5ae610SVladimir Oltean 		 * transmitted as untagged.
342ec5ae610SVladimir Oltean 		 */
343ec5ae610SVladimir Oltean 		v = kzalloc(sizeof(*v), GFP_KERNEL);
344ec5ae610SVladimir Oltean 		if (!v)
345ec5ae610SVladimir Oltean 			return -ENOMEM;
346ec5ae610SVladimir Oltean 
347ec5ae610SVladimir Oltean 		v->port = port;
348ec5ae610SVladimir Oltean 		v->vid = 1;
349ec5ae610SVladimir Oltean 		v->untagged = true;
350ec5ae610SVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
351ec5ae610SVladimir Oltean 			v->pvid = true;
352ec5ae610SVladimir Oltean 		list_add(&v->list, &priv->dsa_8021q_vlans);
3538aa9ebccSVladimir Oltean 	}
3548aa9ebccSVladimir Oltean 
3558aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
3568aa9ebccSVladimir Oltean 	return 0;
3578aa9ebccSVladimir Oltean }
3588aa9ebccSVladimir Oltean 
3598aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
3608aa9ebccSVladimir Oltean {
3618aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
3628aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3638aa9ebccSVladimir Oltean 	int i, j;
3648aa9ebccSVladimir Oltean 
3658aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
3668aa9ebccSVladimir Oltean 
3678aa9ebccSVladimir Oltean 	if (table->entry_count) {
3688aa9ebccSVladimir Oltean 		kfree(table->entries);
3698aa9ebccSVladimir Oltean 		table->entry_count = 0;
3708aa9ebccSVladimir Oltean 	}
3718aa9ebccSVladimir Oltean 
3728aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
3738aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3748aa9ebccSVladimir Oltean 	if (!table->entries)
3758aa9ebccSVladimir Oltean 		return -ENOMEM;
3768aa9ebccSVladimir Oltean 
3778aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
3788aa9ebccSVladimir Oltean 
3798aa9ebccSVladimir Oltean 	l2fwd = table->entries;
3808aa9ebccSVladimir Oltean 
3818aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
3828aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3838aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
3848aa9ebccSVladimir Oltean 
3858aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
3868aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
3878aa9ebccSVladimir Oltean 
3888aa9ebccSVladimir Oltean 		if (i == upstream)
3898aa9ebccSVladimir Oltean 			continue;
3908aa9ebccSVladimir Oltean 
3918aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
3928aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
3938aa9ebccSVladimir Oltean 	}
3948aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
3958aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
3968aa9ebccSVladimir Oltean 	 */
3978aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++)
3988aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_PORTS; j++)
3998aa9ebccSVladimir Oltean 			l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
4008aa9ebccSVladimir Oltean 
4018aa9ebccSVladimir Oltean 	return 0;
4028aa9ebccSVladimir Oltean }
4038aa9ebccSVladimir Oltean 
4048aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
4058aa9ebccSVladimir Oltean {
4068aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
4078aa9ebccSVladimir Oltean 		/* Disallow dynamic reconfiguration of vlan_pmap */
4088aa9ebccSVladimir Oltean 		.max_dynp = 0,
4098aa9ebccSVladimir Oltean 		/* Use a single memory partition for all ingress queues */
4108aa9ebccSVladimir Oltean 		.part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
4118aa9ebccSVladimir Oltean 	};
4128aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4138aa9ebccSVladimir Oltean 
4148aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
4158aa9ebccSVladimir Oltean 
4168aa9ebccSVladimir Oltean 	if (table->entry_count) {
4178aa9ebccSVladimir Oltean 		kfree(table->entries);
4188aa9ebccSVladimir Oltean 		table->entry_count = 0;
4198aa9ebccSVladimir Oltean 	}
4208aa9ebccSVladimir Oltean 
4218aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
4228aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4238aa9ebccSVladimir Oltean 	if (!table->entries)
4248aa9ebccSVladimir Oltean 		return -ENOMEM;
4258aa9ebccSVladimir Oltean 
4268aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
4278aa9ebccSVladimir Oltean 
4288aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4298aa9ebccSVladimir Oltean 	((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
4308aa9ebccSVladimir Oltean 				default_l2fwd_params;
4318aa9ebccSVladimir Oltean 
4328aa9ebccSVladimir Oltean 	return 0;
4338aa9ebccSVladimir Oltean }
4348aa9ebccSVladimir Oltean 
4358aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
4368aa9ebccSVladimir Oltean {
4378aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
438511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
439511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
4408aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
4415f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
4425f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
4435f06c63bSVladimir Oltean 		 */
44408fde09aSVladimir Oltean 		.hostprio = 7,
4458aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
4468aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
44742824463SVladimir Oltean 		.incl_srcpt1 = false,
4488aa9ebccSVladimir Oltean 		.send_meta1  = false,
4498aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
4508aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
45142824463SVladimir Oltean 		.incl_srcpt0 = false,
4528aa9ebccSVladimir Oltean 		.send_meta0  = false,
4538aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
4548aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
4558aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
4568aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
4578aa9ebccSVladimir Oltean 		 */
4588aa9ebccSVladimir Oltean 		.host_port = dsa_upstream_port(priv->ds, 0),
459511e6ca0SVladimir Oltean 		/* Default to an invalid value */
460511e6ca0SVladimir Oltean 		.mirr_port = SJA1105_NUM_PORTS,
4618aa9ebccSVladimir Oltean 		/* Link-local traffic received on casc_port will be forwarded
4628aa9ebccSVladimir Oltean 		 * to host_port without embedding the source port and device ID
4638aa9ebccSVladimir Oltean 		 * info in the destination MAC address (presumably because it
4648aa9ebccSVladimir Oltean 		 * is a cascaded port and a downstream SJA switch already did
4658aa9ebccSVladimir Oltean 		 * that). Default to an invalid port (to disable the feature)
4668aa9ebccSVladimir Oltean 		 * and overwrite this if we find any DSA (cascaded) ports.
4678aa9ebccSVladimir Oltean 		 */
4688aa9ebccSVladimir Oltean 		.casc_port = SJA1105_NUM_PORTS,
4698aa9ebccSVladimir Oltean 		/* No TTEthernet */
470dfacc5a2SVladimir Oltean 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
4718aa9ebccSVladimir Oltean 		.vlmarker = 0,
4728aa9ebccSVladimir Oltean 		.vlmask = 0,
4738aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
4748aa9ebccSVladimir Oltean 		.ignore2stf = 0,
4756666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
4766666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
4776666cebcSVladimir Oltean 		 */
4786666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
4796666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
4808aa9ebccSVladimir Oltean 	};
4818aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4828aa9ebccSVladimir Oltean 
4838aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
4848aa9ebccSVladimir Oltean 
4858aa9ebccSVladimir Oltean 	if (table->entry_count) {
4868aa9ebccSVladimir Oltean 		kfree(table->entries);
4878aa9ebccSVladimir Oltean 		table->entry_count = 0;
4888aa9ebccSVladimir Oltean 	}
4898aa9ebccSVladimir Oltean 
4908aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
4918aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4928aa9ebccSVladimir Oltean 	if (!table->entries)
4938aa9ebccSVladimir Oltean 		return -ENOMEM;
4948aa9ebccSVladimir Oltean 
4958aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
4968aa9ebccSVladimir Oltean 
4978aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4988aa9ebccSVladimir Oltean 	((struct sja1105_general_params_entry *)table->entries)[0] =
4998aa9ebccSVladimir Oltean 				default_general_params;
5008aa9ebccSVladimir Oltean 
5018aa9ebccSVladimir Oltean 	return 0;
5028aa9ebccSVladimir Oltean }
5038aa9ebccSVladimir Oltean 
50479d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
50579d5511cSVladimir Oltean {
50679d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
50779d5511cSVladimir Oltean 	struct sja1105_table *table;
50879d5511cSVladimir Oltean 
50979d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
51079d5511cSVladimir Oltean 
51179d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
51279d5511cSVladimir Oltean 	if (table->entry_count) {
51379d5511cSVladimir Oltean 		kfree(table->entries);
51479d5511cSVladimir Oltean 		table->entry_count = 0;
51579d5511cSVladimir Oltean 	}
51679d5511cSVladimir Oltean 
51779d5511cSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
51879d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
51979d5511cSVladimir Oltean 	if (!table->entries)
52079d5511cSVladimir Oltean 		return -ENOMEM;
52179d5511cSVladimir Oltean 
52279d5511cSVladimir Oltean 	table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
52379d5511cSVladimir Oltean 
52479d5511cSVladimir Oltean 	avb = table->entries;
52579d5511cSVladimir Oltean 
52679d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
52779d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
52879d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
529747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
530747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
531747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
532747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
533747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
534747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
535747e5eb3SVladimir Oltean 	 */
536747e5eb3SVladimir Oltean 	avb->cas_master = false;
53779d5511cSVladimir Oltean 
53879d5511cSVladimir Oltean 	return 0;
53979d5511cSVladimir Oltean }
54079d5511cSVladimir Oltean 
541a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame
542a7cc081cSVladimir Oltean  * according to the ingress port, whether it was broadcast or not, and the
543a7cc081cSVladimir Oltean  * classified traffic class (given by VLAN PCP). This portion of the lookup is
544a7cc081cSVladimir Oltean  * fixed, and gives access to the SHARINDX, an indirection register pointing
545a7cc081cSVladimir Oltean  * within the policing table itself, which is used to resolve the policer that
546a7cc081cSVladimir Oltean  * will be used for this frame.
547a7cc081cSVladimir Oltean  *
548a7cc081cSVladimir Oltean  *  Stage 1                              Stage 2
549a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
550a7cc081cSVladimir Oltean  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
551a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
552a7cc081cSVladimir Oltean  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
553a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
554a7cc081cSVladimir Oltean  *    ...                               | Policer 2: Rate, Burst, MTU     |
555a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
556a7cc081cSVladimir Oltean  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
557a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
558a7cc081cSVladimir Oltean  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
559a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
560a7cc081cSVladimir Oltean  *    ...                               | Policer 5: Rate, Burst, MTU     |
561a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
562a7cc081cSVladimir Oltean  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
563a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
564a7cc081cSVladimir Oltean  *    ...                               | Policer 7: Rate, Burst, MTU     |
565a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
566a7cc081cSVladimir Oltean  * |Port 4 TC 7 |SHARINDX|                 ...
567a7cc081cSVladimir Oltean  * +------------+--------+
568a7cc081cSVladimir Oltean  * |Port 0 BCAST|SHARINDX|                 ...
569a7cc081cSVladimir Oltean  * +------------+--------+
570a7cc081cSVladimir Oltean  * |Port 1 BCAST|SHARINDX|                 ...
571a7cc081cSVladimir Oltean  * +------------+--------+
572a7cc081cSVladimir Oltean  *    ...                                  ...
573a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
574a7cc081cSVladimir Oltean  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
575a7cc081cSVladimir Oltean  * +------------+--------+              +---------------------------------+
576a7cc081cSVladimir Oltean  *
577a7cc081cSVladimir Oltean  * In this driver, we shall use policers 0-4 as statically alocated port
578a7cc081cSVladimir Oltean  * (matchall) policers. So we need to make the SHARINDX for all lookups
579a7cc081cSVladimir Oltean  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
580a7cc081cSVladimir Oltean  * lookup) equal.
581a7cc081cSVladimir Oltean  * The remaining policers (40) shall be dynamically allocated for flower
582a7cc081cSVladimir Oltean  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
583a7cc081cSVladimir Oltean  */
5848aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
5858aa9ebccSVladimir Oltean 
5868aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
5878aa9ebccSVladimir Oltean {
5888aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
5898aa9ebccSVladimir Oltean 	struct sja1105_table *table;
590a7cc081cSVladimir Oltean 	int port, tc;
5918aa9ebccSVladimir Oltean 
5928aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
5938aa9ebccSVladimir Oltean 
5948aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
5958aa9ebccSVladimir Oltean 	if (table->entry_count) {
5968aa9ebccSVladimir Oltean 		kfree(table->entries);
5978aa9ebccSVladimir Oltean 		table->entry_count = 0;
5988aa9ebccSVladimir Oltean 	}
5998aa9ebccSVladimir Oltean 
6008aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
6018aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
6028aa9ebccSVladimir Oltean 	if (!table->entries)
6038aa9ebccSVladimir Oltean 		return -ENOMEM;
6048aa9ebccSVladimir Oltean 
6058aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
6068aa9ebccSVladimir Oltean 
6078aa9ebccSVladimir Oltean 	policing = table->entries;
6088aa9ebccSVladimir Oltean 
609a7cc081cSVladimir Oltean 	/* Setup shared indices for the matchall policers */
610a7cc081cSVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
611a7cc081cSVladimir Oltean 		int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + port;
612a7cc081cSVladimir Oltean 
613a7cc081cSVladimir Oltean 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
614a7cc081cSVladimir Oltean 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
615a7cc081cSVladimir Oltean 
616a7cc081cSVladimir Oltean 		policing[bcast].sharindx = port;
617a7cc081cSVladimir Oltean 	}
618a7cc081cSVladimir Oltean 
619a7cc081cSVladimir Oltean 	/* Setup the matchall policer parameters */
620a7cc081cSVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
621c279c726SVladimir Oltean 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
622c279c726SVladimir Oltean 
623a7cc081cSVladimir Oltean 		if (dsa_is_cpu_port(priv->ds, port))
624c279c726SVladimir Oltean 			mtu += VLAN_HLEN;
6258aa9ebccSVladimir Oltean 
626a7cc081cSVladimir Oltean 		policing[port].smax = 65535; /* Burst size in bytes */
627a7cc081cSVladimir Oltean 		policing[port].rate = SJA1105_RATE_MBPS(1000);
628a7cc081cSVladimir Oltean 		policing[port].maxlen = mtu;
629a7cc081cSVladimir Oltean 		policing[port].partition = 0;
6308aa9ebccSVladimir Oltean 	}
631a7cc081cSVladimir Oltean 
6328aa9ebccSVladimir Oltean 	return 0;
6338aa9ebccSVladimir Oltean }
6348aa9ebccSVladimir Oltean 
6358aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv,
6368aa9ebccSVladimir Oltean 				      struct sja1105_dt_port *ports)
6378aa9ebccSVladimir Oltean {
6388aa9ebccSVladimir Oltean 	int rc;
6398aa9ebccSVladimir Oltean 
6408aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
6418aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
6428aa9ebccSVladimir Oltean 					priv->info->static_ops,
6438aa9ebccSVladimir Oltean 					priv->info->device_id);
6448aa9ebccSVladimir Oltean 	if (rc)
6458aa9ebccSVladimir Oltean 		return rc;
6468aa9ebccSVladimir Oltean 
6478aa9ebccSVladimir Oltean 	/* Build static configuration */
6488aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
6498aa9ebccSVladimir Oltean 	if (rc < 0)
6508aa9ebccSVladimir Oltean 		return rc;
6518aa9ebccSVladimir Oltean 	rc = sja1105_init_mii_settings(priv, ports);
6528aa9ebccSVladimir Oltean 	if (rc < 0)
6538aa9ebccSVladimir Oltean 		return rc;
6548aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
6558aa9ebccSVladimir Oltean 	if (rc < 0)
6568aa9ebccSVladimir Oltean 		return rc;
6578aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
6588aa9ebccSVladimir Oltean 	if (rc < 0)
6598aa9ebccSVladimir Oltean 		return rc;
6608aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
6618aa9ebccSVladimir Oltean 	if (rc < 0)
6628aa9ebccSVladimir Oltean 		return rc;
6638aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
6648aa9ebccSVladimir Oltean 	if (rc < 0)
6658aa9ebccSVladimir Oltean 		return rc;
6668aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
6678aa9ebccSVladimir Oltean 	if (rc < 0)
6688aa9ebccSVladimir Oltean 		return rc;
6698aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
6708aa9ebccSVladimir Oltean 	if (rc < 0)
6718aa9ebccSVladimir Oltean 		return rc;
6728aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
6738aa9ebccSVladimir Oltean 	if (rc < 0)
6748aa9ebccSVladimir Oltean 		return rc;
67579d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
67679d5511cSVladimir Oltean 	if (rc < 0)
67779d5511cSVladimir Oltean 		return rc;
6788aa9ebccSVladimir Oltean 
6798aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
6808aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
6818aa9ebccSVladimir Oltean }
6828aa9ebccSVladimir Oltean 
683f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
684f5b8631cSVladimir Oltean 				      const struct sja1105_dt_port *ports)
685f5b8631cSVladimir Oltean {
686f5b8631cSVladimir Oltean 	int i;
687f5b8631cSVladimir Oltean 
688f5b8631cSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
6899bca3a0aSOleksij Rempel 		if (ports[i].role == XMII_MAC)
690f5b8631cSVladimir Oltean 			continue;
691f5b8631cSVladimir Oltean 
6929bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
6939bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
694f5b8631cSVladimir Oltean 			priv->rgmii_rx_delay[i] = true;
695f5b8631cSVladimir Oltean 
6969bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
6979bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
698f5b8631cSVladimir Oltean 			priv->rgmii_tx_delay[i] = true;
699f5b8631cSVladimir Oltean 
700f5b8631cSVladimir Oltean 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
701f5b8631cSVladimir Oltean 		     !priv->info->setup_rgmii_delay)
702f5b8631cSVladimir Oltean 			return -EINVAL;
703f5b8631cSVladimir Oltean 	}
704f5b8631cSVladimir Oltean 	return 0;
705f5b8631cSVladimir Oltean }
706f5b8631cSVladimir Oltean 
7078aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
7088aa9ebccSVladimir Oltean 				    struct sja1105_dt_port *ports,
7098aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
7108aa9ebccSVladimir Oltean {
7118aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
7128aa9ebccSVladimir Oltean 	struct device_node *child;
7138aa9ebccSVladimir Oltean 
71427afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
7158aa9ebccSVladimir Oltean 		struct device_node *phy_node;
7160c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
7178aa9ebccSVladimir Oltean 		u32 index;
7180c65b2b9SAndrew Lunn 		int err;
7198aa9ebccSVladimir Oltean 
7208aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
7218aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
7228aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
7238aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
7247ba771e3SNishka Dasgupta 			of_node_put(child);
7258aa9ebccSVladimir Oltean 			return -ENODEV;
7268aa9ebccSVladimir Oltean 		}
7278aa9ebccSVladimir Oltean 
7288aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
7290c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
7300c65b2b9SAndrew Lunn 		if (err) {
7318aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
7328aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
7338aa9ebccSVladimir Oltean 				index);
7347ba771e3SNishka Dasgupta 			of_node_put(child);
7358aa9ebccSVladimir Oltean 			return -ENODEV;
7368aa9ebccSVladimir Oltean 		}
7378aa9ebccSVladimir Oltean 		ports[index].phy_mode = phy_mode;
7388aa9ebccSVladimir Oltean 
7398aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
7408aa9ebccSVladimir Oltean 		if (!phy_node) {
7418aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
7428aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
7438aa9ebccSVladimir Oltean 					"properties missing!\n");
7447ba771e3SNishka Dasgupta 				of_node_put(child);
7458aa9ebccSVladimir Oltean 				return -ENODEV;
7468aa9ebccSVladimir Oltean 			}
7478aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
7488aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
7498aa9ebccSVladimir Oltean 			 */
7508aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
7518aa9ebccSVladimir Oltean 		} else {
7528aa9ebccSVladimir Oltean 			/* phy-handle present => put port in MAC role */
7538aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
7548aa9ebccSVladimir Oltean 			of_node_put(phy_node);
7558aa9ebccSVladimir Oltean 		}
7568aa9ebccSVladimir Oltean 
7578aa9ebccSVladimir Oltean 		/* The MAC/PHY role can be overridden with explicit bindings */
7588aa9ebccSVladimir Oltean 		if (of_property_read_bool(child, "sja1105,role-mac"))
7598aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
7608aa9ebccSVladimir Oltean 		else if (of_property_read_bool(child, "sja1105,role-phy"))
7618aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
7628aa9ebccSVladimir Oltean 	}
7638aa9ebccSVladimir Oltean 
7648aa9ebccSVladimir Oltean 	return 0;
7658aa9ebccSVladimir Oltean }
7668aa9ebccSVladimir Oltean 
7678aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv,
7688aa9ebccSVladimir Oltean 			    struct sja1105_dt_port *ports)
7698aa9ebccSVladimir Oltean {
7708aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
7718aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
7728aa9ebccSVladimir Oltean 	struct device_node *ports_node;
7738aa9ebccSVladimir Oltean 	int rc;
7748aa9ebccSVladimir Oltean 
7758aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
7768aa9ebccSVladimir Oltean 	if (!ports_node) {
7778aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
7788aa9ebccSVladimir Oltean 		return -ENODEV;
7798aa9ebccSVladimir Oltean 	}
7808aa9ebccSVladimir Oltean 
7818aa9ebccSVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
7828aa9ebccSVladimir Oltean 	of_node_put(ports_node);
7838aa9ebccSVladimir Oltean 
7848aa9ebccSVladimir Oltean 	return rc;
7858aa9ebccSVladimir Oltean }
7868aa9ebccSVladimir Oltean 
787ffe10e67SVladimir Oltean static int sja1105_sgmii_read(struct sja1105_private *priv, int pcs_reg)
788ffe10e67SVladimir Oltean {
789ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
790ffe10e67SVladimir Oltean 	u32 val;
791ffe10e67SVladimir Oltean 	int rc;
792ffe10e67SVladimir Oltean 
793ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->sgmii + pcs_reg, &val,
794ffe10e67SVladimir Oltean 			      NULL);
795ffe10e67SVladimir Oltean 	if (rc < 0)
796ffe10e67SVladimir Oltean 		return rc;
797ffe10e67SVladimir Oltean 
798ffe10e67SVladimir Oltean 	return val;
799ffe10e67SVladimir Oltean }
800ffe10e67SVladimir Oltean 
801ffe10e67SVladimir Oltean static int sja1105_sgmii_write(struct sja1105_private *priv, int pcs_reg,
802ffe10e67SVladimir Oltean 			       u16 pcs_val)
803ffe10e67SVladimir Oltean {
804ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
805ffe10e67SVladimir Oltean 	u32 val = pcs_val;
806ffe10e67SVladimir Oltean 	int rc;
807ffe10e67SVladimir Oltean 
808ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->sgmii + pcs_reg, &val,
809ffe10e67SVladimir Oltean 			      NULL);
810ffe10e67SVladimir Oltean 	if (rc < 0)
811ffe10e67SVladimir Oltean 		return rc;
812ffe10e67SVladimir Oltean 
813ffe10e67SVladimir Oltean 	return val;
814ffe10e67SVladimir Oltean }
815ffe10e67SVladimir Oltean 
816ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_config(struct sja1105_private *priv,
817ffe10e67SVladimir Oltean 				     bool an_enabled, bool an_master)
818ffe10e67SVladimir Oltean {
819ffe10e67SVladimir Oltean 	u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII;
820ffe10e67SVladimir Oltean 
821ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
822ffe10e67SVladimir Oltean 	 * stop the clock during LPI mode, make the MAC reconfigure
823ffe10e67SVladimir Oltean 	 * autonomously after PCS autoneg is done, flush the internal FIFOs.
824ffe10e67SVladimir Oltean 	 */
825ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC1, SJA1105_DC1_EN_VSMMD1 |
826ffe10e67SVladimir Oltean 					       SJA1105_DC1_CLOCK_STOP_EN |
827ffe10e67SVladimir Oltean 					       SJA1105_DC1_MAC_AUTO_SW |
828ffe10e67SVladimir Oltean 					       SJA1105_DC1_INIT);
829ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
830ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC2, SJA1105_DC2_TX_POL_INV_DISABLE);
831ffe10e67SVladimir Oltean 	/* AUTONEG_CONTROL: Use SGMII autoneg */
832ffe10e67SVladimir Oltean 	if (an_master)
833ffe10e67SVladimir Oltean 		ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK;
834ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_AC, ac);
835ffe10e67SVladimir Oltean 	/* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
836ffe10e67SVladimir Oltean 	 * sja1105_sgmii_pcs_force_speed must be called later for the link
837ffe10e67SVladimir Oltean 	 * to become operational.
838ffe10e67SVladimir Oltean 	 */
839ffe10e67SVladimir Oltean 	if (an_enabled)
840ffe10e67SVladimir Oltean 		sja1105_sgmii_write(priv, MII_BMCR,
841ffe10e67SVladimir Oltean 				    BMCR_ANENABLE | BMCR_ANRESTART);
842ffe10e67SVladimir Oltean }
843ffe10e67SVladimir Oltean 
844ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv,
845ffe10e67SVladimir Oltean 					  int speed)
846ffe10e67SVladimir Oltean {
847ffe10e67SVladimir Oltean 	int pcs_speed;
848ffe10e67SVladimir Oltean 
849ffe10e67SVladimir Oltean 	switch (speed) {
850ffe10e67SVladimir Oltean 	case SPEED_1000:
851ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED1000;
852ffe10e67SVladimir Oltean 		break;
853ffe10e67SVladimir Oltean 	case SPEED_100:
854ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED100;
855ffe10e67SVladimir Oltean 		break;
856ffe10e67SVladimir Oltean 	case SPEED_10:
857ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED10;
858ffe10e67SVladimir Oltean 		break;
859ffe10e67SVladimir Oltean 	default:
860ffe10e67SVladimir Oltean 		dev_err(priv->ds->dev, "Invalid speed %d\n", speed);
861ffe10e67SVladimir Oltean 		return;
862ffe10e67SVladimir Oltean 	}
863ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, MII_BMCR, pcs_speed | BMCR_FULLDPLX);
864ffe10e67SVladimir Oltean }
865ffe10e67SVladimir Oltean 
866c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
8678aa9ebccSVladimir Oltean static int sja1105_speed[] = {
868c44d0535SVladimir Oltean 	[SJA1105_SPEED_AUTO]		= SPEED_UNKNOWN,
869c44d0535SVladimir Oltean 	[SJA1105_SPEED_10MBPS]		= SPEED_10,
870c44d0535SVladimir Oltean 	[SJA1105_SPEED_100MBPS]		= SPEED_100,
871c44d0535SVladimir Oltean 	[SJA1105_SPEED_1000MBPS]	= SPEED_1000,
8728aa9ebccSVladimir Oltean };
8738aa9ebccSVladimir Oltean 
8748400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
8758aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
8768400cff6SVladimir Oltean 				      int speed_mbps)
8778aa9ebccSVladimir Oltean {
8788aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
8798aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
8808aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
8818aa9ebccSVladimir Oltean 	sja1105_phy_interface_t phy_mode;
8828aa9ebccSVladimir Oltean 	sja1105_speed_t speed;
8838aa9ebccSVladimir Oltean 	int rc;
8848aa9ebccSVladimir Oltean 
8858400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
8868400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
8878400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
8888400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
8898400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
8908400cff6SVladimir Oltean 	 */
8918aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
8928400cff6SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
8938aa9ebccSVladimir Oltean 
894f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
895c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
896a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
897a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
898a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
899a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
900a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
901a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
902a979a0abSVladimir Oltean 		 */
903f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_AUTO;
904f4cfcfbdSVladimir Oltean 		break;
905c44d0535SVladimir Oltean 	case SPEED_10:
906f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_10MBPS;
907f4cfcfbdSVladimir Oltean 		break;
908c44d0535SVladimir Oltean 	case SPEED_100:
909f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_100MBPS;
910f4cfcfbdSVladimir Oltean 		break;
911c44d0535SVladimir Oltean 	case SPEED_1000:
912f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_1000MBPS;
913f4cfcfbdSVladimir Oltean 		break;
914f4cfcfbdSVladimir Oltean 	default:
9158aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
9168aa9ebccSVladimir Oltean 		return -EINVAL;
9178aa9ebccSVladimir Oltean 	}
9188aa9ebccSVladimir Oltean 
9198400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
9208400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
9218400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
9228400cff6SVladimir Oltean 	 * we want auto during upload phase).
923ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
924ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
9258aa9ebccSVladimir Oltean 	 */
926ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port))
927ffe10e67SVladimir Oltean 		mac[port].speed = SJA1105_SPEED_1000MBPS;
928ffe10e67SVladimir Oltean 	else
9298aa9ebccSVladimir Oltean 		mac[port].speed = speed;
9308aa9ebccSVladimir Oltean 
9318aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
9328400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
9338400cff6SVladimir Oltean 					  &mac[port], true);
9348aa9ebccSVladimir Oltean 	if (rc < 0) {
9358aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
9368aa9ebccSVladimir Oltean 		return rc;
9378aa9ebccSVladimir Oltean 	}
9388aa9ebccSVladimir Oltean 
9398aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
9408aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
9418aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
9428aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
9438aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
9448aa9ebccSVladimir Oltean 	 */
9458aa9ebccSVladimir Oltean 	phy_mode = mii->xmii_mode[port];
9468aa9ebccSVladimir Oltean 	if (phy_mode != XMII_MODE_RGMII)
9478aa9ebccSVladimir Oltean 		return 0;
9488aa9ebccSVladimir Oltean 
9498aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
9508aa9ebccSVladimir Oltean }
9518aa9ebccSVladimir Oltean 
95239710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
95339710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
95439710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
95539710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
95639710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
95739710229SVladimir Oltean  * now.
95839710229SVladimir Oltean  */
95939710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
96039710229SVladimir Oltean 				      phy_interface_t interface)
96139710229SVladimir Oltean {
96239710229SVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
96339710229SVladimir Oltean 	sja1105_phy_interface_t phy_mode;
96439710229SVladimir Oltean 
96539710229SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
96639710229SVladimir Oltean 	phy_mode = mii->xmii_mode[port];
96739710229SVladimir Oltean 
96839710229SVladimir Oltean 	switch (interface) {
96939710229SVladimir Oltean 	case PHY_INTERFACE_MODE_MII:
97039710229SVladimir Oltean 		return (phy_mode != XMII_MODE_MII);
97139710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RMII:
97239710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RMII);
97339710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII:
97439710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_ID:
97539710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_RXID:
97639710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_TXID:
97739710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RGMII);
978ffe10e67SVladimir Oltean 	case PHY_INTERFACE_MODE_SGMII:
979ffe10e67SVladimir Oltean 		return (phy_mode != XMII_MODE_SGMII);
98039710229SVladimir Oltean 	default:
98139710229SVladimir Oltean 		return true;
98239710229SVladimir Oltean 	}
98339710229SVladimir Oltean }
98439710229SVladimir Oltean 
985af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
986ffe10e67SVladimir Oltean 			       unsigned int mode,
987af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
9888aa9ebccSVladimir Oltean {
9898aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
990ffe10e67SVladimir Oltean 	bool is_sgmii = sja1105_supports_sgmii(priv, port);
9918aa9ebccSVladimir Oltean 
992ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
993ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
994ec8582d1SVladimir Oltean 			phy_modes(state->interface));
99539710229SVladimir Oltean 		return;
996ec8582d1SVladimir Oltean 	}
99739710229SVladimir Oltean 
998ffe10e67SVladimir Oltean 	if (phylink_autoneg_inband(mode) && !is_sgmii) {
9999f971573SVladimir Oltean 		dev_err(ds->dev, "In-band AN not supported!\n");
10009f971573SVladimir Oltean 		return;
10019f971573SVladimir Oltean 	}
1002ffe10e67SVladimir Oltean 
1003ffe10e67SVladimir Oltean 	if (is_sgmii)
1004ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, phylink_autoneg_inband(mode),
1005ffe10e67SVladimir Oltean 					 false);
10068400cff6SVladimir Oltean }
10078400cff6SVladimir Oltean 
10088400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
10098400cff6SVladimir Oltean 				  unsigned int mode,
10108400cff6SVladimir Oltean 				  phy_interface_t interface)
10118400cff6SVladimir Oltean {
10128400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
10138400cff6SVladimir Oltean }
10148400cff6SVladimir Oltean 
10158400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
10168400cff6SVladimir Oltean 				unsigned int mode,
10178400cff6SVladimir Oltean 				phy_interface_t interface,
10185b502a7bSRussell King 				struct phy_device *phydev,
10195b502a7bSRussell King 				int speed, int duplex,
10205b502a7bSRussell King 				bool tx_pause, bool rx_pause)
10218400cff6SVladimir Oltean {
1022ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1023ec8582d1SVladimir Oltean 
1024ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
1025ec8582d1SVladimir Oltean 
1026ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode))
1027ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_force_speed(priv, speed);
1028ffe10e67SVladimir Oltean 
1029ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
10308aa9ebccSVladimir Oltean }
10318aa9ebccSVladimir Oltean 
1032ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1033ad9f299aSVladimir Oltean 				     unsigned long *supported,
1034ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
1035ad9f299aSVladimir Oltean {
1036ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
1037ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
1038ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
1039ad9f299aSVladimir Oltean 	 */
1040ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1041ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1042ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1043ad9f299aSVladimir Oltean 
1044ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1045ad9f299aSVladimir Oltean 
104639710229SVladimir Oltean 	/* include/linux/phylink.h says:
104739710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
104839710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
104939710229SVladimir Oltean 	 */
105039710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
105139710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
105239710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
105339710229SVladimir Oltean 		return;
105439710229SVladimir Oltean 	}
105539710229SVladimir Oltean 
1056ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
1057ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
1058ad9f299aSVladimir Oltean 	 */
1059ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
1060ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
1061ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
1062ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
1063ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
1064ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1065ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1066ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
1067ad9f299aSVladimir Oltean 
1068ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1069ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1070ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1071ad9f299aSVladimir Oltean }
1072ad9f299aSVladimir Oltean 
1073ffe10e67SVladimir Oltean static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port,
1074ffe10e67SVladimir Oltean 				     struct phylink_link_state *state)
1075ffe10e67SVladimir Oltean {
1076ffe10e67SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1077ffe10e67SVladimir Oltean 	int ais;
1078ffe10e67SVladimir Oltean 
1079ffe10e67SVladimir Oltean 	/* Read the vendor-specific AUTONEG_INTR_STATUS register */
1080ffe10e67SVladimir Oltean 	ais = sja1105_sgmii_read(priv, SJA1105_AIS);
1081ffe10e67SVladimir Oltean 	if (ais < 0)
1082ffe10e67SVladimir Oltean 		return ais;
1083ffe10e67SVladimir Oltean 
1084ffe10e67SVladimir Oltean 	switch (SJA1105_AIS_SPEED(ais)) {
1085ffe10e67SVladimir Oltean 	case 0:
1086ffe10e67SVladimir Oltean 		state->speed = SPEED_10;
1087ffe10e67SVladimir Oltean 		break;
1088ffe10e67SVladimir Oltean 	case 1:
1089ffe10e67SVladimir Oltean 		state->speed = SPEED_100;
1090ffe10e67SVladimir Oltean 		break;
1091ffe10e67SVladimir Oltean 	case 2:
1092ffe10e67SVladimir Oltean 		state->speed = SPEED_1000;
1093ffe10e67SVladimir Oltean 		break;
1094ffe10e67SVladimir Oltean 	default:
1095ffe10e67SVladimir Oltean 		dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n",
1096ffe10e67SVladimir Oltean 			SJA1105_AIS_SPEED(ais));
1097ffe10e67SVladimir Oltean 	}
1098ffe10e67SVladimir Oltean 	state->duplex = SJA1105_AIS_DUPLEX_MODE(ais);
1099ffe10e67SVladimir Oltean 	state->an_complete = SJA1105_AIS_COMPLETE(ais);
1100ffe10e67SVladimir Oltean 	state->link = SJA1105_AIS_LINK_STATUS(ais);
1101ffe10e67SVladimir Oltean 
1102ffe10e67SVladimir Oltean 	return 0;
1103ffe10e67SVladimir Oltean }
1104ffe10e67SVladimir Oltean 
110560f6053fSVladimir Oltean static int
110660f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
110760f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
110860f6053fSVladimir Oltean {
110960f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
111060f6053fSVladimir Oltean 	struct sja1105_table *table;
111160f6053fSVladimir Oltean 	int i;
111260f6053fSVladimir Oltean 
111360f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
111460f6053fSVladimir Oltean 	l2_lookup = table->entries;
111560f6053fSVladimir Oltean 
111660f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
111760f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
111860f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
111960f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
112060f6053fSVladimir Oltean 			return i;
112160f6053fSVladimir Oltean 
112260f6053fSVladimir Oltean 	return -1;
112360f6053fSVladimir Oltean }
112460f6053fSVladimir Oltean 
112560f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
112660f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
112760f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
112860f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
112960f6053fSVladimir Oltean  */
113060f6053fSVladimir Oltean static int
113160f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
113260f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
113360f6053fSVladimir Oltean 			  bool keep)
113460f6053fSVladimir Oltean {
113560f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
113660f6053fSVladimir Oltean 	struct sja1105_table *table;
113760f6053fSVladimir Oltean 	int rc, match;
113860f6053fSVladimir Oltean 
113960f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
114060f6053fSVladimir Oltean 
114160f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
114260f6053fSVladimir Oltean 	if (match < 0) {
114360f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
114460f6053fSVladimir Oltean 		if (!keep)
114560f6053fSVladimir Oltean 			return 0;
114660f6053fSVladimir Oltean 
114760f6053fSVladimir Oltean 		/* No match => new entry */
114860f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
114960f6053fSVladimir Oltean 		if (rc)
115060f6053fSVladimir Oltean 			return rc;
115160f6053fSVladimir Oltean 
115260f6053fSVladimir Oltean 		match = table->entry_count - 1;
115360f6053fSVladimir Oltean 	}
115460f6053fSVladimir Oltean 
115560f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
115660f6053fSVladimir Oltean 	l2_lookup = table->entries;
115760f6053fSVladimir Oltean 
115860f6053fSVladimir Oltean 	/* We have a match.
115960f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
116060f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
116160f6053fSVladimir Oltean 	 * which we update it).
116260f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
116360f6053fSVladimir Oltean 	 */
116460f6053fSVladimir Oltean 	if (keep) {
116560f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
116660f6053fSVladimir Oltean 		return 0;
116760f6053fSVladimir Oltean 	}
116860f6053fSVladimir Oltean 
116960f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
117060f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
117160f6053fSVladimir Oltean 	 */
117260f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
117360f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
117460f6053fSVladimir Oltean }
117560f6053fSVladimir Oltean 
1176291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1177291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1178291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1179291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1180291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1181291d1e72SVladimir Oltean  */
118209c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1183291d1e72SVladimir Oltean {
1184291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1185291d1e72SVladimir Oltean }
1186291d1e72SVladimir Oltean 
11879dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1188291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1189291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1190291d1e72SVladimir Oltean 					 int *last_unused)
1191291d1e72SVladimir Oltean {
1192291d1e72SVladimir Oltean 	int way;
1193291d1e72SVladimir Oltean 
1194291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1195291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1196291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1197291d1e72SVladimir Oltean 
1198291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1199291d1e72SVladimir Oltean 		 * into the return value
1200291d1e72SVladimir Oltean 		 */
1201291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1202291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1203291d1e72SVladimir Oltean 			if (last_unused)
1204291d1e72SVladimir Oltean 				*last_unused = way;
1205291d1e72SVladimir Oltean 			continue;
1206291d1e72SVladimir Oltean 		}
1207291d1e72SVladimir Oltean 
1208291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1209291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1210291d1e72SVladimir Oltean 			if (match)
1211291d1e72SVladimir Oltean 				*match = l2_lookup;
1212291d1e72SVladimir Oltean 			return way;
1213291d1e72SVladimir Oltean 		}
1214291d1e72SVladimir Oltean 	}
1215291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1216291d1e72SVladimir Oltean 	return -1;
1217291d1e72SVladimir Oltean }
1218291d1e72SVladimir Oltean 
12199dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1220291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1221291d1e72SVladimir Oltean {
1222291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1223291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1224291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1225291d1e72SVladimir Oltean 	int last_unused = -1;
122660f6053fSVladimir Oltean 	int bin, way, rc;
1227291d1e72SVladimir Oltean 
12289dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1229291d1e72SVladimir Oltean 
12309dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1231291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1232291d1e72SVladimir Oltean 	if (way >= 0) {
1233291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1234291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1235291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1236291d1e72SVladimir Oltean 		 */
1237291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1238291d1e72SVladimir Oltean 			return 0;
1239291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1240291d1e72SVladimir Oltean 	} else {
1241291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1242291d1e72SVladimir Oltean 
1243291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1244291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1245291d1e72SVladimir Oltean 		 */
1246291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1247291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1248291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1249291d1e72SVladimir Oltean 
1250291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1251291d1e72SVladimir Oltean 			way = last_unused;
1252291d1e72SVladimir Oltean 		} else {
1253291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1254291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1255291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1256291d1e72SVladimir Oltean 			 * distribution function:
1257291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1258291d1e72SVladimir Oltean 			 */
1259291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1260291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1261291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1262291d1e72SVladimir Oltean 				 bin, addr, way);
1263291d1e72SVladimir Oltean 			/* Evict entry */
1264291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1265291d1e72SVladimir Oltean 						     index, NULL, false);
1266291d1e72SVladimir Oltean 		}
1267291d1e72SVladimir Oltean 	}
1268291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1269291d1e72SVladimir Oltean 
127060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1271291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1272291d1e72SVladimir Oltean 					  true);
127360f6053fSVladimir Oltean 	if (rc < 0)
127460f6053fSVladimir Oltean 		return rc;
127560f6053fSVladimir Oltean 
127660f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1277291d1e72SVladimir Oltean }
1278291d1e72SVladimir Oltean 
12799dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1280291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1281291d1e72SVladimir Oltean {
1282291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1283291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
128460f6053fSVladimir Oltean 	int index, bin, way, rc;
1285291d1e72SVladimir Oltean 	bool keep;
1286291d1e72SVladimir Oltean 
12879dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
12889dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1289291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1290291d1e72SVladimir Oltean 	if (way < 0)
1291291d1e72SVladimir Oltean 		return 0;
1292291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1293291d1e72SVladimir Oltean 
1294291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1295291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1296291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1297291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1298291d1e72SVladimir Oltean 	 */
1299291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
13007752e937SVladimir Oltean 
1301291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1302291d1e72SVladimir Oltean 		keep = true;
1303291d1e72SVladimir Oltean 	else
1304291d1e72SVladimir Oltean 		keep = false;
1305291d1e72SVladimir Oltean 
130660f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1307291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
130860f6053fSVladimir Oltean 	if (rc < 0)
130960f6053fSVladimir Oltean 		return rc;
131060f6053fSVladimir Oltean 
131160f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1312291d1e72SVladimir Oltean }
1313291d1e72SVladimir Oltean 
13149dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
13159dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
13169dfa6911SVladimir Oltean {
13171da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
13181da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
13191da73821SVladimir Oltean 	int rc, i;
13201da73821SVladimir Oltean 
13211da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
13221da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
13231da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
13241da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
13251da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
13267f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
13271da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
13281da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
13296d7c7d94SVladimir Oltean 	} else {
13306d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
13316d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
13326d7c7d94SVladimir Oltean 	}
13331da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
13341da73821SVladimir Oltean 
13351da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13361da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
13371da73821SVladimir Oltean 	if (rc == 0) {
13381da73821SVladimir Oltean 		/* Found and this port is already in the entry's
13391da73821SVladimir Oltean 		 * port mask => job done
13401da73821SVladimir Oltean 		 */
13411da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
13421da73821SVladimir Oltean 			return 0;
13431da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
13441da73821SVladimir Oltean 		 * found something.
13451da73821SVladimir Oltean 		 */
13461da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
13471da73821SVladimir Oltean 		goto skip_finding_an_index;
13481da73821SVladimir Oltean 	}
13491da73821SVladimir Oltean 
13501da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
13511da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
13521da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
13531da73821SVladimir Oltean 	 */
13541da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
13551da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13561da73821SVladimir Oltean 						 i, NULL);
13571da73821SVladimir Oltean 		if (rc < 0)
13581da73821SVladimir Oltean 			break;
13591da73821SVladimir Oltean 	}
13601da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
13611da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
13621da73821SVladimir Oltean 		return -EINVAL;
13631da73821SVladimir Oltean 	}
136417ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
13651da73821SVladimir Oltean 	l2_lookup.index = i;
13661da73821SVladimir Oltean 
13671da73821SVladimir Oltean skip_finding_an_index:
136860f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
13691da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
13701da73821SVladimir Oltean 					  true);
137160f6053fSVladimir Oltean 	if (rc < 0)
137260f6053fSVladimir Oltean 		return rc;
137360f6053fSVladimir Oltean 
137460f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
13759dfa6911SVladimir Oltean }
13769dfa6911SVladimir Oltean 
13779dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
13789dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
13799dfa6911SVladimir Oltean {
13801da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
13811da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
13821da73821SVladimir Oltean 	bool keep;
13831da73821SVladimir Oltean 	int rc;
13841da73821SVladimir Oltean 
13851da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
13861da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
13871da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
13881da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
13897f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
13901da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
13911da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
13926d7c7d94SVladimir Oltean 	} else {
13936d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
13946d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
13956d7c7d94SVladimir Oltean 	}
13961da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
13971da73821SVladimir Oltean 
13981da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13991da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
14001da73821SVladimir Oltean 	if (rc < 0)
14011da73821SVladimir Oltean 		return 0;
14021da73821SVladimir Oltean 
14031da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
14041da73821SVladimir Oltean 
14051da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
14061da73821SVladimir Oltean 	 * or if we remove it completely.
14071da73821SVladimir Oltean 	 */
14081da73821SVladimir Oltean 	if (l2_lookup.destports)
14091da73821SVladimir Oltean 		keep = true;
14101da73821SVladimir Oltean 	else
14111da73821SVladimir Oltean 		keep = false;
14121da73821SVladimir Oltean 
141360f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
14141da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
141560f6053fSVladimir Oltean 	if (rc < 0)
141660f6053fSVladimir Oltean 		return rc;
141760f6053fSVladimir Oltean 
141860f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
14199dfa6911SVladimir Oltean }
14209dfa6911SVladimir Oltean 
14219dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
14229dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
14239dfa6911SVladimir Oltean {
14249dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1425b3ee526aSVladimir Oltean 
14266d7c7d94SVladimir Oltean 	/* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
14276d7c7d94SVladimir Oltean 	 * so the switch still does some VLAN processing internally.
14286d7c7d94SVladimir Oltean 	 * But Shared VLAN Learning (SVL) is also active, and it will take
14296d7c7d94SVladimir Oltean 	 * care of autonomous forwarding between the unique pvid's of each
14306d7c7d94SVladimir Oltean 	 * port.  Here we just make sure that users can't add duplicate FDB
14316d7c7d94SVladimir Oltean 	 * entries when in this mode - the actual VID doesn't matter except
14326d7c7d94SVladimir Oltean 	 * for what gets printed in 'bridge fdb show'.  In the case of zero,
14336d7c7d94SVladimir Oltean 	 * no VID gets printed at all.
143493647594SVladimir Oltean 	 */
14357f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
14366d7c7d94SVladimir Oltean 		vid = 0;
143793647594SVladimir Oltean 
14386d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
14399dfa6911SVladimir Oltean }
14409dfa6911SVladimir Oltean 
14419dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
14429dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
14439dfa6911SVladimir Oltean {
14449dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14459dfa6911SVladimir Oltean 
14467f14937fSVladimir Oltean 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
14476d7c7d94SVladimir Oltean 		vid = 0;
14486d7c7d94SVladimir Oltean 
1449b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
14509dfa6911SVladimir Oltean }
14519dfa6911SVladimir Oltean 
1452291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1453291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1454291d1e72SVladimir Oltean {
1455291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1456291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1457291d1e72SVladimir Oltean 	int i;
1458291d1e72SVladimir Oltean 
1459291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1460291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1461291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1462291d1e72SVladimir Oltean 		int rc;
1463291d1e72SVladimir Oltean 
1464291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1465291d1e72SVladimir Oltean 						 i, &l2_lookup);
1466291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1467def84604SVladimir Oltean 		if (rc == -ENOENT)
1468291d1e72SVladimir Oltean 			continue;
1469291d1e72SVladimir Oltean 		if (rc) {
1470291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1471291d1e72SVladimir Oltean 			return rc;
1472291d1e72SVladimir Oltean 		}
1473291d1e72SVladimir Oltean 
1474291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1475291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1476291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1477291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1478291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1479291d1e72SVladimir Oltean 		 */
1480291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1481291d1e72SVladimir Oltean 			continue;
1482291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
148393647594SVladimir Oltean 
14846d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
14857f14937fSVladimir Oltean 		if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
14866d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
148717ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1488291d1e72SVladimir Oltean 	}
1489291d1e72SVladimir Oltean 	return 0;
1490291d1e72SVladimir Oltean }
1491291d1e72SVladimir Oltean 
1492291d1e72SVladimir Oltean /* This callback needs to be present */
1493291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1494291d1e72SVladimir Oltean 			       const struct switchdev_obj_port_mdb *mdb)
1495291d1e72SVladimir Oltean {
1496291d1e72SVladimir Oltean 	return 0;
1497291d1e72SVladimir Oltean }
1498291d1e72SVladimir Oltean 
1499291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1500291d1e72SVladimir Oltean 			    const struct switchdev_obj_port_mdb *mdb)
1501291d1e72SVladimir Oltean {
1502291d1e72SVladimir Oltean 	sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1503291d1e72SVladimir Oltean }
1504291d1e72SVladimir Oltean 
1505291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1506291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1507291d1e72SVladimir Oltean {
1508291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1509291d1e72SVladimir Oltean }
1510291d1e72SVladimir Oltean 
15118aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
15128aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
15138aa9ebccSVladimir Oltean {
15148aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
15158aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15168aa9ebccSVladimir Oltean 	int i, rc;
15178aa9ebccSVladimir Oltean 
15188aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
15198aa9ebccSVladimir Oltean 
15208aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
15218aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
15228aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
15238aa9ebccSVladimir Oltean 		 */
15248aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
15258aa9ebccSVladimir Oltean 			continue;
15268aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
15278aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
15288aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
15298aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
15308aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
15318aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
15328aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
15338aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
15348aa9ebccSVladimir Oltean 		 */
15358aa9ebccSVladimir Oltean 		if (i == port)
15368aa9ebccSVladimir Oltean 			continue;
15378aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
15388aa9ebccSVladimir Oltean 			continue;
15398aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
15408aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
15418aa9ebccSVladimir Oltean 
15428aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
15438aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
15448aa9ebccSVladimir Oltean 		if (rc < 0)
15458aa9ebccSVladimir Oltean 			return rc;
15468aa9ebccSVladimir Oltean 	}
15478aa9ebccSVladimir Oltean 
15488aa9ebccSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
15498aa9ebccSVladimir Oltean 					    port, &l2_fwd[port], true);
15508aa9ebccSVladimir Oltean }
15518aa9ebccSVladimir Oltean 
1552640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1553640f763fSVladimir Oltean 					 u8 state)
1554640f763fSVladimir Oltean {
1555640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1556640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1557640f763fSVladimir Oltean 
1558640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1559640f763fSVladimir Oltean 
1560640f763fSVladimir Oltean 	switch (state) {
1561640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1562640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1563640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1564640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1565640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1566640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1567640f763fSVladimir Oltean 		 */
1568640f763fSVladimir Oltean 		mac[port].ingress   = false;
1569640f763fSVladimir Oltean 		mac[port].egress    = false;
1570640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1571640f763fSVladimir Oltean 		break;
1572640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1573640f763fSVladimir Oltean 		mac[port].ingress   = true;
1574640f763fSVladimir Oltean 		mac[port].egress    = false;
1575640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1576640f763fSVladimir Oltean 		break;
1577640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1578640f763fSVladimir Oltean 		mac[port].ingress   = true;
1579640f763fSVladimir Oltean 		mac[port].egress    = false;
1580640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1581640f763fSVladimir Oltean 		break;
1582640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1583640f763fSVladimir Oltean 		mac[port].ingress   = true;
1584640f763fSVladimir Oltean 		mac[port].egress    = true;
1585640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1586640f763fSVladimir Oltean 		break;
1587640f763fSVladimir Oltean 	default:
1588640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1589640f763fSVladimir Oltean 		return;
1590640f763fSVladimir Oltean 	}
1591640f763fSVladimir Oltean 
1592640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1593640f763fSVladimir Oltean 				     &mac[port], true);
1594640f763fSVladimir Oltean }
1595640f763fSVladimir Oltean 
15968aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
15978aa9ebccSVladimir Oltean 			       struct net_device *br)
15988aa9ebccSVladimir Oltean {
15998aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
16008aa9ebccSVladimir Oltean }
16018aa9ebccSVladimir Oltean 
16028aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
16038aa9ebccSVladimir Oltean 				 struct net_device *br)
16048aa9ebccSVladimir Oltean {
16058aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
16068aa9ebccSVladimir Oltean }
16078aa9ebccSVladimir Oltean 
16082eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
16092eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
16102eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
16112eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
16122eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
1613c279c726SVladimir Oltean 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1614dfacc5a2SVladimir Oltean 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
16152eea1fa8SVladimir Oltean };
16162eea1fa8SVladimir Oltean 
16176666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
16186666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
16196666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
16206666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
16216666cebcSVladimir Oltean  * such that this operation is relatively seamless.
16226666cebcSVladimir Oltean  */
16232eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
16242eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
16256666cebcSVladimir Oltean {
16266cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
16276cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
16286666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
16296666cebcSVladimir Oltean 	int speed_mbps[SJA1105_NUM_PORTS];
16306cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
16316cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
16326cf99c13SVladimir Oltean 	s64 t12, t34;
1633ffe10e67SVladimir Oltean 	u16 bmcr = 0;
16346666cebcSVladimir Oltean 	int rc, i;
16356cf99c13SVladimir Oltean 	s64 now;
16366666cebcSVladimir Oltean 
1637af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1638af580ae2SVladimir Oltean 
16396666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
16406666cebcSVladimir Oltean 
16418400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
16428400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
16438400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
16448400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
16456666cebcSVladimir Oltean 	 */
16466666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
16476666cebcSVladimir Oltean 		speed_mbps[i] = sja1105_speed[mac[i].speed];
16486666cebcSVladimir Oltean 		mac[i].speed = SJA1105_SPEED_AUTO;
16496666cebcSVladimir Oltean 	}
16506666cebcSVladimir Oltean 
1651ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT))
1652ffe10e67SVladimir Oltean 		bmcr = sja1105_sgmii_read(priv, MII_BMCR);
1653ffe10e67SVladimir Oltean 
16546cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
16556cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
16566cf99c13SVladimir Oltean 
16576cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
16586cf99c13SVladimir Oltean 	if (rc < 0)
16596cf99c13SVladimir Oltean 		goto out_unlock_ptp;
16606cf99c13SVladimir Oltean 
16616666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
16626666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
16636666cebcSVladimir Oltean 	if (rc < 0)
16646cf99c13SVladimir Oltean 		goto out_unlock_ptp;
16656cf99c13SVladimir Oltean 
16666cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
16676cf99c13SVladimir Oltean 	if (rc < 0)
16686cf99c13SVladimir Oltean 		goto out_unlock_ptp;
16696cf99c13SVladimir Oltean 
16706cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
16716cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
16726cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
16736cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
16746cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
16756cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
16766cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
16776cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
16786cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
16796cf99c13SVladimir Oltean 	now += (t34 - t12);
16806cf99c13SVladimir Oltean 
16816cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
16826cf99c13SVladimir Oltean 
16836cf99c13SVladimir Oltean out_unlock_ptp:
16846cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
16856666cebcSVladimir Oltean 
16862eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
16872eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
16882eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
16892eea1fa8SVladimir Oltean 
16906666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
16916666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
16926666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
16936666cebcSVladimir Oltean 	 */
16946666cebcSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
16956666cebcSVladimir Oltean 	if (rc < 0)
16966666cebcSVladimir Oltean 		goto out;
16976666cebcSVladimir Oltean 
16986666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
16998400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
17006666cebcSVladimir Oltean 		if (rc < 0)
17016666cebcSVladimir Oltean 			goto out;
17026666cebcSVladimir Oltean 	}
1703ffe10e67SVladimir Oltean 
1704ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) {
1705ffe10e67SVladimir Oltean 		bool an_enabled = !!(bmcr & BMCR_ANENABLE);
1706ffe10e67SVladimir Oltean 
1707ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, an_enabled, false);
1708ffe10e67SVladimir Oltean 
1709ffe10e67SVladimir Oltean 		if (!an_enabled) {
1710ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
1711ffe10e67SVladimir Oltean 
1712ffe10e67SVladimir Oltean 			if (bmcr & BMCR_SPEED1000)
1713ffe10e67SVladimir Oltean 				speed = SPEED_1000;
1714ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED100)
1715ffe10e67SVladimir Oltean 				speed = SPEED_100;
1716ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED10)
1717ffe10e67SVladimir Oltean 				speed = SPEED_10;
1718ffe10e67SVladimir Oltean 
1719ffe10e67SVladimir Oltean 			sja1105_sgmii_pcs_force_speed(priv, speed);
1720ffe10e67SVladimir Oltean 		}
1721ffe10e67SVladimir Oltean 	}
17226666cebcSVladimir Oltean out:
1723af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1724af580ae2SVladimir Oltean 
17256666cebcSVladimir Oltean 	return rc;
17266666cebcSVladimir Oltean }
17276666cebcSVladimir Oltean 
17286666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
17296666cebcSVladimir Oltean {
17306666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
17316666cebcSVladimir Oltean 
17326666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
17336666cebcSVladimir Oltean 
17346666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
17356666cebcSVladimir Oltean 
17366666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
17376666cebcSVladimir Oltean 					   &mac[port], true);
17386666cebcSVladimir Oltean }
17396666cebcSVladimir Oltean 
1740ac02a451SVladimir Oltean static int sja1105_crosschip_bridge_join(struct dsa_switch *ds,
1741ac02a451SVladimir Oltean 					 int tree_index, int sw_index,
1742ac02a451SVladimir Oltean 					 int other_port, struct net_device *br)
1743ac02a451SVladimir Oltean {
1744ac02a451SVladimir Oltean 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1745ac02a451SVladimir Oltean 	struct sja1105_private *other_priv = other_ds->priv;
1746ac02a451SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1747ac02a451SVladimir Oltean 	int port, rc;
1748ac02a451SVladimir Oltean 
1749ac02a451SVladimir Oltean 	if (other_ds->ops != &sja1105_switch_ops)
1750ac02a451SVladimir Oltean 		return 0;
1751ac02a451SVladimir Oltean 
1752ac02a451SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1753ac02a451SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
1754ac02a451SVladimir Oltean 			continue;
1755ac02a451SVladimir Oltean 		if (dsa_to_port(ds, port)->bridge_dev != br)
1756ac02a451SVladimir Oltean 			continue;
1757ac02a451SVladimir Oltean 
175860b33aebSVladimir Oltean 		other_priv->expect_dsa_8021q = true;
1759ac02a451SVladimir Oltean 		rc = dsa_8021q_crosschip_bridge_join(ds, port, other_ds,
1760ec5ae610SVladimir Oltean 						     other_port,
1761ac02a451SVladimir Oltean 						     &priv->crosschip_links);
176260b33aebSVladimir Oltean 		other_priv->expect_dsa_8021q = false;
1763ac02a451SVladimir Oltean 		if (rc)
1764ac02a451SVladimir Oltean 			return rc;
1765ac02a451SVladimir Oltean 
176660b33aebSVladimir Oltean 		priv->expect_dsa_8021q = true;
1767ac02a451SVladimir Oltean 		rc = dsa_8021q_crosschip_bridge_join(other_ds, other_port, ds,
1768ec5ae610SVladimir Oltean 						     port,
1769ac02a451SVladimir Oltean 						     &other_priv->crosschip_links);
177060b33aebSVladimir Oltean 		priv->expect_dsa_8021q = false;
1771ac02a451SVladimir Oltean 		if (rc)
1772ac02a451SVladimir Oltean 			return rc;
1773ac02a451SVladimir Oltean 	}
1774ac02a451SVladimir Oltean 
1775ac02a451SVladimir Oltean 	return 0;
1776ac02a451SVladimir Oltean }
1777ac02a451SVladimir Oltean 
1778ac02a451SVladimir Oltean static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds,
1779ac02a451SVladimir Oltean 					   int tree_index, int sw_index,
1780ac02a451SVladimir Oltean 					   int other_port,
1781ac02a451SVladimir Oltean 					   struct net_device *br)
1782ac02a451SVladimir Oltean {
1783ac02a451SVladimir Oltean 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1784ac02a451SVladimir Oltean 	struct sja1105_private *other_priv = other_ds->priv;
1785ac02a451SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1786ac02a451SVladimir Oltean 	int port;
1787ac02a451SVladimir Oltean 
1788ac02a451SVladimir Oltean 	if (other_ds->ops != &sja1105_switch_ops)
1789ac02a451SVladimir Oltean 		return;
1790ac02a451SVladimir Oltean 
1791ac02a451SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
1792ac02a451SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
1793ac02a451SVladimir Oltean 			continue;
1794ac02a451SVladimir Oltean 		if (dsa_to_port(ds, port)->bridge_dev != br)
1795ac02a451SVladimir Oltean 			continue;
1796ac02a451SVladimir Oltean 
179760b33aebSVladimir Oltean 		other_priv->expect_dsa_8021q = true;
1798ac02a451SVladimir Oltean 		dsa_8021q_crosschip_bridge_leave(ds, port, other_ds, other_port,
1799ec5ae610SVladimir Oltean 						 &priv->crosschip_links);
180060b33aebSVladimir Oltean 		other_priv->expect_dsa_8021q = false;
1801ac02a451SVladimir Oltean 
180260b33aebSVladimir Oltean 		priv->expect_dsa_8021q = true;
1803ec5ae610SVladimir Oltean 		dsa_8021q_crosschip_bridge_leave(other_ds, other_port, ds, port,
1804ac02a451SVladimir Oltean 						 &other_priv->crosschip_links);
180560b33aebSVladimir Oltean 		priv->expect_dsa_8021q = false;
1806ac02a451SVladimir Oltean 	}
1807ac02a451SVladimir Oltean }
1808ac02a451SVladimir Oltean 
1809227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1810227d07a0SVladimir Oltean {
181160b33aebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1812227d07a0SVladimir Oltean 	int rc, i;
1813227d07a0SVladimir Oltean 
1814227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
181560b33aebSVladimir Oltean 		priv->expect_dsa_8021q = true;
1816227d07a0SVladimir Oltean 		rc = dsa_port_setup_8021q_tagging(ds, i, enabled);
181760b33aebSVladimir Oltean 		priv->expect_dsa_8021q = false;
1818227d07a0SVladimir Oltean 		if (rc < 0) {
1819227d07a0SVladimir Oltean 			dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n",
1820227d07a0SVladimir Oltean 				i, rc);
1821227d07a0SVladimir Oltean 			return rc;
1822227d07a0SVladimir Oltean 		}
1823227d07a0SVladimir Oltean 	}
1824ac02a451SVladimir Oltean 
1825227d07a0SVladimir Oltean 	dev_info(ds->dev, "%s switch tagging\n",
1826227d07a0SVladimir Oltean 		 enabled ? "Enabled" : "Disabled");
1827227d07a0SVladimir Oltean 	return 0;
1828227d07a0SVladimir Oltean }
1829227d07a0SVladimir Oltean 
18308aa9ebccSVladimir Oltean static enum dsa_tag_protocol
18314d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
18324d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
18338aa9ebccSVladimir Oltean {
1834227d07a0SVladimir Oltean 	return DSA_TAG_PROTO_SJA1105;
18358aa9ebccSVladimir Oltean }
18368aa9ebccSVladimir Oltean 
1837ec5ae610SVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
1838ec5ae610SVladimir Oltean {
1839ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
1840ec5ae610SVladimir Oltean 	int count, i;
1841ec5ae610SVladimir Oltean 
1842ec5ae610SVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
1843ec5ae610SVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
1844ec5ae610SVladimir Oltean 
1845ec5ae610SVladimir Oltean 	for (i = 0; i < count; i++)
1846ec5ae610SVladimir Oltean 		if (vlan[i].vlanid == vid)
1847ec5ae610SVladimir Oltean 			return i;
1848ec5ae610SVladimir Oltean 
1849ec5ae610SVladimir Oltean 	/* Return an invalid entry index if not found */
1850ec5ae610SVladimir Oltean 	return -1;
1851ec5ae610SVladimir Oltean }
1852ec5ae610SVladimir Oltean 
1853ec5ae610SVladimir Oltean static int sja1105_commit_vlans(struct sja1105_private *priv,
1854ec5ae610SVladimir Oltean 				struct sja1105_vlan_lookup_entry *new_vlan)
1855ec5ae610SVladimir Oltean {
1856ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
1857ec5ae610SVladimir Oltean 	struct sja1105_table *table;
1858ec5ae610SVladimir Oltean 	int num_vlans = 0;
1859ec5ae610SVladimir Oltean 	int rc, i, k = 0;
1860ec5ae610SVladimir Oltean 
1861ec5ae610SVladimir Oltean 	/* VLAN table */
1862ec5ae610SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
1863ec5ae610SVladimir Oltean 	vlan = table->entries;
1864ec5ae610SVladimir Oltean 
1865ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++) {
1866ec5ae610SVladimir Oltean 		int match = sja1105_is_vlan_configured(priv, i);
1867ec5ae610SVladimir Oltean 
1868ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid != VLAN_N_VID)
1869ec5ae610SVladimir Oltean 			num_vlans++;
1870ec5ae610SVladimir Oltean 
1871ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) {
1872ec5ae610SVladimir Oltean 			/* Was there before, no longer is. Delete */
1873ec5ae610SVladimir Oltean 			dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i);
1874ec5ae610SVladimir Oltean 			rc = sja1105_dynamic_config_write(priv,
1875ec5ae610SVladimir Oltean 							  BLK_IDX_VLAN_LOOKUP,
1876ec5ae610SVladimir Oltean 							  i, &vlan[match], false);
1877ec5ae610SVladimir Oltean 			if (rc < 0)
1878ec5ae610SVladimir Oltean 				return rc;
1879ec5ae610SVladimir Oltean 		} else if (new_vlan[i].vlanid != VLAN_N_VID) {
1880ec5ae610SVladimir Oltean 			/* Nothing changed, don't do anything */
1881ec5ae610SVladimir Oltean 			if (match >= 0 &&
1882ec5ae610SVladimir Oltean 			    vlan[match].vlanid == new_vlan[i].vlanid &&
1883ec5ae610SVladimir Oltean 			    vlan[match].tag_port == new_vlan[i].tag_port &&
1884ec5ae610SVladimir Oltean 			    vlan[match].vlan_bc == new_vlan[i].vlan_bc &&
1885ec5ae610SVladimir Oltean 			    vlan[match].vmemb_port == new_vlan[i].vmemb_port)
1886ec5ae610SVladimir Oltean 				continue;
1887ec5ae610SVladimir Oltean 			/* Update entry */
1888ec5ae610SVladimir Oltean 			dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i);
1889ec5ae610SVladimir Oltean 			rc = sja1105_dynamic_config_write(priv,
1890ec5ae610SVladimir Oltean 							  BLK_IDX_VLAN_LOOKUP,
1891ec5ae610SVladimir Oltean 							  i, &new_vlan[i],
1892ec5ae610SVladimir Oltean 							  true);
1893ec5ae610SVladimir Oltean 			if (rc < 0)
1894ec5ae610SVladimir Oltean 				return rc;
1895ec5ae610SVladimir Oltean 		}
1896ec5ae610SVladimir Oltean 	}
1897ec5ae610SVladimir Oltean 
1898ec5ae610SVladimir Oltean 	if (table->entry_count)
1899ec5ae610SVladimir Oltean 		kfree(table->entries);
1900ec5ae610SVladimir Oltean 
1901ec5ae610SVladimir Oltean 	table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size,
1902ec5ae610SVladimir Oltean 				 GFP_KERNEL);
1903ec5ae610SVladimir Oltean 	if (!table->entries)
1904ec5ae610SVladimir Oltean 		return -ENOMEM;
1905ec5ae610SVladimir Oltean 
1906ec5ae610SVladimir Oltean 	table->entry_count = num_vlans;
1907ec5ae610SVladimir Oltean 	vlan = table->entries;
1908ec5ae610SVladimir Oltean 
1909ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++) {
1910ec5ae610SVladimir Oltean 		if (new_vlan[i].vlanid == VLAN_N_VID)
1911ec5ae610SVladimir Oltean 			continue;
1912ec5ae610SVladimir Oltean 		vlan[k++] = new_vlan[i];
1913ec5ae610SVladimir Oltean 	}
1914ec5ae610SVladimir Oltean 
1915ec5ae610SVladimir Oltean 	return 0;
1916ec5ae610SVladimir Oltean }
1917ec5ae610SVladimir Oltean 
1918ec5ae610SVladimir Oltean struct sja1105_crosschip_switch {
1919ec5ae610SVladimir Oltean 	struct list_head list;
1920ec5ae610SVladimir Oltean 	struct dsa_switch *other_ds;
1921ec5ae610SVladimir Oltean };
1922ec5ae610SVladimir Oltean 
1923ec5ae610SVladimir Oltean static int sja1105_commit_pvid(struct sja1105_private *priv)
1924ec5ae610SVladimir Oltean {
1925ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
1926ec5ae610SVladimir Oltean 	struct list_head *vlan_list;
1927ec5ae610SVladimir Oltean 	int rc = 0;
1928ec5ae610SVladimir Oltean 
1929ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
1930ec5ae610SVladimir Oltean 		vlan_list = &priv->bridge_vlans;
1931ec5ae610SVladimir Oltean 	else
1932ec5ae610SVladimir Oltean 		vlan_list = &priv->dsa_8021q_vlans;
1933ec5ae610SVladimir Oltean 
1934ec5ae610SVladimir Oltean 	list_for_each_entry(v, vlan_list, list) {
1935ec5ae610SVladimir Oltean 		if (v->pvid) {
1936ec5ae610SVladimir Oltean 			rc = sja1105_pvid_apply(priv, v->port, v->vid);
1937ec5ae610SVladimir Oltean 			if (rc)
1938ec5ae610SVladimir Oltean 				break;
1939ec5ae610SVladimir Oltean 		}
1940ec5ae610SVladimir Oltean 	}
1941ec5ae610SVladimir Oltean 
1942ec5ae610SVladimir Oltean 	return rc;
1943ec5ae610SVladimir Oltean }
1944ec5ae610SVladimir Oltean 
1945ec5ae610SVladimir Oltean static int
1946ec5ae610SVladimir Oltean sja1105_build_bridge_vlans(struct sja1105_private *priv,
1947ec5ae610SVladimir Oltean 			   struct sja1105_vlan_lookup_entry *new_vlan)
1948ec5ae610SVladimir Oltean {
1949ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
1950ec5ae610SVladimir Oltean 
1951ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
1952ec5ae610SVladimir Oltean 		return 0;
1953ec5ae610SVladimir Oltean 
1954ec5ae610SVladimir Oltean 	list_for_each_entry(v, &priv->bridge_vlans, list) {
1955ec5ae610SVladimir Oltean 		int match = v->vid;
1956ec5ae610SVladimir Oltean 
1957ec5ae610SVladimir Oltean 		new_vlan[match].vlanid = v->vid;
1958ec5ae610SVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(v->port);
1959ec5ae610SVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(v->port);
1960ec5ae610SVladimir Oltean 		if (!v->untagged)
1961ec5ae610SVladimir Oltean 			new_vlan[match].tag_port |= BIT(v->port);
1962ec5ae610SVladimir Oltean 	}
1963ec5ae610SVladimir Oltean 
1964ec5ae610SVladimir Oltean 	return 0;
1965ec5ae610SVladimir Oltean }
1966ec5ae610SVladimir Oltean 
1967ec5ae610SVladimir Oltean static int
1968ec5ae610SVladimir Oltean sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv,
1969ec5ae610SVladimir Oltean 			      struct sja1105_vlan_lookup_entry *new_vlan)
1970ec5ae610SVladimir Oltean {
1971ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v;
1972ec5ae610SVladimir Oltean 
1973ec5ae610SVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
1974ec5ae610SVladimir Oltean 		return 0;
1975ec5ae610SVladimir Oltean 
1976ec5ae610SVladimir Oltean 	list_for_each_entry(v, &priv->dsa_8021q_vlans, list) {
1977ec5ae610SVladimir Oltean 		int match = v->vid;
1978ec5ae610SVladimir Oltean 
1979ec5ae610SVladimir Oltean 		new_vlan[match].vlanid = v->vid;
1980ec5ae610SVladimir Oltean 		new_vlan[match].vmemb_port |= BIT(v->port);
1981ec5ae610SVladimir Oltean 		new_vlan[match].vlan_bc |= BIT(v->port);
1982ec5ae610SVladimir Oltean 		if (!v->untagged)
1983ec5ae610SVladimir Oltean 			new_vlan[match].tag_port |= BIT(v->port);
1984ec5ae610SVladimir Oltean 	}
1985ec5ae610SVladimir Oltean 
1986ec5ae610SVladimir Oltean 	return 0;
1987ec5ae610SVladimir Oltean }
1988ec5ae610SVladimir Oltean 
1989ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify);
1990ec5ae610SVladimir Oltean 
1991ec5ae610SVladimir Oltean static int sja1105_notify_crosschip_switches(struct sja1105_private *priv)
1992ec5ae610SVladimir Oltean {
1993ec5ae610SVladimir Oltean 	struct sja1105_crosschip_switch *s, *pos;
1994ec5ae610SVladimir Oltean 	struct list_head crosschip_switches;
1995ec5ae610SVladimir Oltean 	struct dsa_8021q_crosschip_link *c;
1996ec5ae610SVladimir Oltean 	int rc = 0;
1997ec5ae610SVladimir Oltean 
1998ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&crosschip_switches);
1999ec5ae610SVladimir Oltean 
2000ec5ae610SVladimir Oltean 	list_for_each_entry(c, &priv->crosschip_links, list) {
2001ec5ae610SVladimir Oltean 		bool already_added = false;
2002ec5ae610SVladimir Oltean 
2003ec5ae610SVladimir Oltean 		list_for_each_entry(s, &crosschip_switches, list) {
2004ec5ae610SVladimir Oltean 			if (s->other_ds == c->other_ds) {
2005ec5ae610SVladimir Oltean 				already_added = true;
2006ec5ae610SVladimir Oltean 				break;
2007ec5ae610SVladimir Oltean 			}
2008ec5ae610SVladimir Oltean 		}
2009ec5ae610SVladimir Oltean 
2010ec5ae610SVladimir Oltean 		if (already_added)
2011ec5ae610SVladimir Oltean 			continue;
2012ec5ae610SVladimir Oltean 
2013ec5ae610SVladimir Oltean 		s = kzalloc(sizeof(*s), GFP_KERNEL);
2014ec5ae610SVladimir Oltean 		if (!s) {
2015ec5ae610SVladimir Oltean 			dev_err(priv->ds->dev, "Failed to allocate memory\n");
2016ec5ae610SVladimir Oltean 			rc = -ENOMEM;
2017ec5ae610SVladimir Oltean 			goto out;
2018ec5ae610SVladimir Oltean 		}
2019ec5ae610SVladimir Oltean 		s->other_ds = c->other_ds;
2020ec5ae610SVladimir Oltean 		list_add(&s->list, &crosschip_switches);
2021ec5ae610SVladimir Oltean 	}
2022ec5ae610SVladimir Oltean 
2023ec5ae610SVladimir Oltean 	list_for_each_entry(s, &crosschip_switches, list) {
2024ec5ae610SVladimir Oltean 		struct sja1105_private *other_priv = s->other_ds->priv;
2025ec5ae610SVladimir Oltean 
2026ec5ae610SVladimir Oltean 		rc = sja1105_build_vlan_table(other_priv, false);
2027ec5ae610SVladimir Oltean 		if (rc)
2028ec5ae610SVladimir Oltean 			goto out;
2029ec5ae610SVladimir Oltean 	}
2030ec5ae610SVladimir Oltean 
2031ec5ae610SVladimir Oltean out:
2032ec5ae610SVladimir Oltean 	list_for_each_entry_safe(s, pos, &crosschip_switches, list) {
2033ec5ae610SVladimir Oltean 		list_del(&s->list);
2034ec5ae610SVladimir Oltean 		kfree(s);
2035ec5ae610SVladimir Oltean 	}
2036ec5ae610SVladimir Oltean 
2037ec5ae610SVladimir Oltean 	return rc;
2038ec5ae610SVladimir Oltean }
2039ec5ae610SVladimir Oltean 
2040ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify)
2041ec5ae610SVladimir Oltean {
2042ec5ae610SVladimir Oltean 	struct sja1105_vlan_lookup_entry *new_vlan;
2043ec5ae610SVladimir Oltean 	struct sja1105_table *table;
2044ec5ae610SVladimir Oltean 	int rc;
2045ec5ae610SVladimir Oltean 	int i;
2046ec5ae610SVladimir Oltean 
2047ec5ae610SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2048ec5ae610SVladimir Oltean 	new_vlan = kcalloc(VLAN_N_VID,
2049ec5ae610SVladimir Oltean 			   table->ops->unpacked_entry_size, GFP_KERNEL);
2050ec5ae610SVladimir Oltean 	if (!new_vlan)
2051ec5ae610SVladimir Oltean 		return -ENOMEM;
2052ec5ae610SVladimir Oltean 
2053ec5ae610SVladimir Oltean 	for (i = 0; i < VLAN_N_VID; i++)
2054ec5ae610SVladimir Oltean 		new_vlan[i].vlanid = VLAN_N_VID;
2055ec5ae610SVladimir Oltean 
2056ec5ae610SVladimir Oltean 	/* Bridge VLANs */
2057ec5ae610SVladimir Oltean 	rc = sja1105_build_bridge_vlans(priv, new_vlan);
2058ec5ae610SVladimir Oltean 	if (rc)
2059ec5ae610SVladimir Oltean 		goto out;
2060ec5ae610SVladimir Oltean 
2061ec5ae610SVladimir Oltean 	/* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c:
2062ec5ae610SVladimir Oltean 	 * - RX VLANs
2063ec5ae610SVladimir Oltean 	 * - TX VLANs
2064ec5ae610SVladimir Oltean 	 * - Crosschip links
2065ec5ae610SVladimir Oltean 	 */
2066ec5ae610SVladimir Oltean 	rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan);
2067ec5ae610SVladimir Oltean 	if (rc)
2068ec5ae610SVladimir Oltean 		goto out;
2069ec5ae610SVladimir Oltean 
2070ec5ae610SVladimir Oltean 	rc = sja1105_commit_vlans(priv, new_vlan);
2071ec5ae610SVladimir Oltean 	if (rc)
2072ec5ae610SVladimir Oltean 		goto out;
2073ec5ae610SVladimir Oltean 
2074ec5ae610SVladimir Oltean 	rc = sja1105_commit_pvid(priv);
2075ec5ae610SVladimir Oltean 	if (rc)
2076ec5ae610SVladimir Oltean 		goto out;
2077ec5ae610SVladimir Oltean 
2078ec5ae610SVladimir Oltean 	if (notify) {
2079ec5ae610SVladimir Oltean 		rc = sja1105_notify_crosschip_switches(priv);
2080ec5ae610SVladimir Oltean 		if (rc)
2081ec5ae610SVladimir Oltean 			goto out;
2082ec5ae610SVladimir Oltean 	}
2083ec5ae610SVladimir Oltean 
2084ec5ae610SVladimir Oltean out:
2085ec5ae610SVladimir Oltean 	kfree(new_vlan);
2086ec5ae610SVladimir Oltean 
2087ec5ae610SVladimir Oltean 	return rc;
2088ec5ae610SVladimir Oltean }
2089ec5ae610SVladimir Oltean 
2090ec5ae610SVladimir Oltean /* Select the list to which we should add this VLAN. */
2091ec5ae610SVladimir Oltean static struct list_head *sja1105_classify_vlan(struct sja1105_private *priv,
2092ec5ae610SVladimir Oltean 					       u16 vid)
2093ec5ae610SVladimir Oltean {
2094ec5ae610SVladimir Oltean 	if (priv->expect_dsa_8021q)
2095ec5ae610SVladimir Oltean 		return &priv->dsa_8021q_vlans;
2096ec5ae610SVladimir Oltean 
2097ec5ae610SVladimir Oltean 	return &priv->bridge_vlans;
2098ec5ae610SVladimir Oltean }
2099ec5ae610SVladimir Oltean 
21006666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
21016666cebcSVladimir Oltean 				const struct switchdev_obj_port_vlan *vlan)
21026666cebcSVladimir Oltean {
210360b33aebSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
210460b33aebSVladimir Oltean 	u16 vid;
210560b33aebSVladimir Oltean 
210660b33aebSVladimir Oltean 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
210760b33aebSVladimir Oltean 		return 0;
210860b33aebSVladimir Oltean 
210960b33aebSVladimir Oltean 	/* If the user wants best-effort VLAN filtering (aka vlan_filtering
211060b33aebSVladimir Oltean 	 * bridge plus tagging), be sure to at least deny alterations to the
211160b33aebSVladimir Oltean 	 * configuration done by dsa_8021q.
211260b33aebSVladimir Oltean 	 */
211360b33aebSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
211460b33aebSVladimir Oltean 		if (!priv->expect_dsa_8021q && vid_is_dsa_8021q(vid)) {
211560b33aebSVladimir Oltean 			dev_err(ds->dev, "Range 1024-3071 reserved for dsa_8021q operation\n");
211660b33aebSVladimir Oltean 			return -EBUSY;
211760b33aebSVladimir Oltean 		}
211860b33aebSVladimir Oltean 	}
211960b33aebSVladimir Oltean 
21206666cebcSVladimir Oltean 	return 0;
21216666cebcSVladimir Oltean }
21226666cebcSVladimir Oltean 
2123070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
2124070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
2125070ca3bbSVladimir Oltean  * So a switch reset is required.
2126070ca3bbSVladimir Oltean  */
21276666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
21286666cebcSVladimir Oltean {
21296d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2130070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
21316666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
21327f14937fSVladimir Oltean 	enum sja1105_vlan_state state;
2133070ca3bbSVladimir Oltean 	struct sja1105_table *table;
2134dfacc5a2SVladimir Oltean 	struct sja1105_rule *rule;
2135070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
21366666cebcSVladimir Oltean 	int rc;
21376666cebcSVladimir Oltean 
2138dfacc5a2SVladimir Oltean 	list_for_each_entry(rule, &priv->flow_block.rules, list) {
2139dfacc5a2SVladimir Oltean 		if (rule->type == SJA1105_RULE_VL) {
2140dfacc5a2SVladimir Oltean 			dev_err(ds->dev,
2141dfacc5a2SVladimir Oltean 				"Cannot change VLAN filtering state while VL rules are active\n");
2142dfacc5a2SVladimir Oltean 			return -EBUSY;
2143dfacc5a2SVladimir Oltean 		}
2144dfacc5a2SVladimir Oltean 	}
2145dfacc5a2SVladimir Oltean 
2146070ca3bbSVladimir Oltean 	if (enabled) {
21476666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
214854fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
214954fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
2150070ca3bbSVladimir Oltean 	} else {
21516666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
2152070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
2153070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
2154070ca3bbSVladimir Oltean 	}
2155070ca3bbSVladimir Oltean 
2156*38b5beeaSVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
2157*38b5beeaSVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2158*38b5beeaSVladimir Oltean 
2159*38b5beeaSVladimir Oltean 		if (enabled)
2160*38b5beeaSVladimir Oltean 			sp->xmit_tpid = priv->info->qinq_tpid;
2161*38b5beeaSVladimir Oltean 		else
2162*38b5beeaSVladimir Oltean 			sp->xmit_tpid = ETH_P_SJA1105;
2163*38b5beeaSVladimir Oltean 	}
2164*38b5beeaSVladimir Oltean 
21657f14937fSVladimir Oltean 	if (!enabled)
21667f14937fSVladimir Oltean 		state = SJA1105_VLAN_UNAWARE;
21677f14937fSVladimir Oltean 	else
21687f14937fSVladimir Oltean 		state = SJA1105_VLAN_FILTERING_FULL;
21697f14937fSVladimir Oltean 
2170cfa36b1fSVladimir Oltean 	if (priv->vlan_state == state)
2171cfa36b1fSVladimir Oltean 		return 0;
2172cfa36b1fSVladimir Oltean 
21737f14937fSVladimir Oltean 	priv->vlan_state = state;
21747f14937fSVladimir Oltean 
2175070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2176070ca3bbSVladimir Oltean 	general_params = table->entries;
2177f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
217854fa49eeSVladimir Oltean 	general_params->tpid = tpid;
217954fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2180070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
218142824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
218242824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
218342824463SVladimir Oltean 	 */
218442824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
218542824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
2186070ca3bbSVladimir Oltean 
21876d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
21886d7c7d94SVladimir Oltean 	 * No VLAN filtering => shared VLAN learning.
21896d7c7d94SVladimir Oltean 	 *
21906d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
21916d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
21926d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
21936d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
21946d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
21956d7c7d94SVladimir Oltean 	 * forwarding decision.
21966d7c7d94SVladimir Oltean 	 *
21976d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
21986d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
21996d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
22006d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
22016d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
22026d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
22036d7c7d94SVladimir Oltean 	 * (all frames get flooded).
22046d7c7d94SVladimir Oltean 	 */
22056d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
22066d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
22076d7c7d94SVladimir Oltean 	l2_lookup_params->shared_learn = !enabled;
22086d7c7d94SVladimir Oltean 
22092eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
22106666cebcSVladimir Oltean 	if (rc)
22116666cebcSVladimir Oltean 		dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
22126666cebcSVladimir Oltean 
2213227d07a0SVladimir Oltean 	/* Switch port identification based on 802.1Q is only passable
2214227d07a0SVladimir Oltean 	 * if we are not under a vlan_filtering bridge. So make sure
2215227d07a0SVladimir Oltean 	 * the two configurations are mutually exclusive.
2216227d07a0SVladimir Oltean 	 */
2217227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, !enabled);
22186666cebcSVladimir Oltean }
22196666cebcSVladimir Oltean 
22206666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port,
22216666cebcSVladimir Oltean 			     const struct switchdev_obj_port_vlan *vlan)
22226666cebcSVladimir Oltean {
22236666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2224ec5ae610SVladimir Oltean 	bool vlan_table_changed = false;
22256666cebcSVladimir Oltean 	u16 vid;
22266666cebcSVladimir Oltean 	int rc;
22276666cebcSVladimir Oltean 
22286666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2229ec5ae610SVladimir Oltean 		bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2230ec5ae610SVladimir Oltean 		bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2231ec5ae610SVladimir Oltean 		struct sja1105_bridge_vlan *v;
2232ec5ae610SVladimir Oltean 		struct list_head *vlan_list;
2233ec5ae610SVladimir Oltean 		bool already_added = false;
2234ec5ae610SVladimir Oltean 
2235ec5ae610SVladimir Oltean 		vlan_list = sja1105_classify_vlan(priv, vid);
2236ec5ae610SVladimir Oltean 
2237ec5ae610SVladimir Oltean 		list_for_each_entry(v, vlan_list, list) {
2238ec5ae610SVladimir Oltean 			if (v->port == port && v->vid == vid &&
2239ec5ae610SVladimir Oltean 			    v->untagged == untagged && v->pvid == pvid) {
2240ec5ae610SVladimir Oltean 				already_added = true;
2241ec5ae610SVladimir Oltean 				break;
2242ec5ae610SVladimir Oltean 			}
2243ec5ae610SVladimir Oltean 		}
2244ec5ae610SVladimir Oltean 
2245ec5ae610SVladimir Oltean 		if (already_added)
2246ec5ae610SVladimir Oltean 			continue;
2247ec5ae610SVladimir Oltean 
2248ec5ae610SVladimir Oltean 		v = kzalloc(sizeof(*v), GFP_KERNEL);
2249ec5ae610SVladimir Oltean 		if (!v) {
2250ec5ae610SVladimir Oltean 			dev_err(ds->dev, "Out of memory while storing VLAN\n");
22516666cebcSVladimir Oltean 			return;
22526666cebcSVladimir Oltean 		}
2253ec5ae610SVladimir Oltean 
2254ec5ae610SVladimir Oltean 		v->port = port;
2255ec5ae610SVladimir Oltean 		v->vid = vid;
2256ec5ae610SVladimir Oltean 		v->untagged = untagged;
2257ec5ae610SVladimir Oltean 		v->pvid = pvid;
2258ec5ae610SVladimir Oltean 		list_add(&v->list, vlan_list);
2259ec5ae610SVladimir Oltean 
2260ec5ae610SVladimir Oltean 		vlan_table_changed = true;
2261ec5ae610SVladimir Oltean 	}
2262ec5ae610SVladimir Oltean 
2263ec5ae610SVladimir Oltean 	if (!vlan_table_changed)
22646666cebcSVladimir Oltean 		return;
2265ec5ae610SVladimir Oltean 
2266ec5ae610SVladimir Oltean 	rc = sja1105_build_vlan_table(priv, true);
2267ec5ae610SVladimir Oltean 	if (rc)
2268ec5ae610SVladimir Oltean 		dev_err(ds->dev, "Failed to build VLAN table: %d\n", rc);
22696666cebcSVladimir Oltean }
22706666cebcSVladimir Oltean 
22716666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
22726666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
22736666cebcSVladimir Oltean {
22746666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2275ec5ae610SVladimir Oltean 	bool vlan_table_changed = false;
22766666cebcSVladimir Oltean 	u16 vid;
22776666cebcSVladimir Oltean 
22786666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2279ec5ae610SVladimir Oltean 		struct sja1105_bridge_vlan *v, *n;
2280ec5ae610SVladimir Oltean 		struct list_head *vlan_list;
2281ec5ae610SVladimir Oltean 
2282ec5ae610SVladimir Oltean 		vlan_list = sja1105_classify_vlan(priv, vid);
2283ec5ae610SVladimir Oltean 
2284ec5ae610SVladimir Oltean 		list_for_each_entry_safe(v, n, vlan_list, list) {
2285ec5ae610SVladimir Oltean 			if (v->port == port && v->vid == vid) {
2286ec5ae610SVladimir Oltean 				list_del(&v->list);
2287ec5ae610SVladimir Oltean 				kfree(v);
2288ec5ae610SVladimir Oltean 				vlan_table_changed = true;
2289ec5ae610SVladimir Oltean 				break;
22906666cebcSVladimir Oltean 			}
22916666cebcSVladimir Oltean 		}
2292ec5ae610SVladimir Oltean 	}
2293ec5ae610SVladimir Oltean 
2294ec5ae610SVladimir Oltean 	if (!vlan_table_changed)
22956666cebcSVladimir Oltean 		return 0;
2296ec5ae610SVladimir Oltean 
2297ec5ae610SVladimir Oltean 	return sja1105_build_vlan_table(priv, true);
22986666cebcSVladimir Oltean }
22996666cebcSVladimir Oltean 
23008aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
23018aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
23028aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
23038aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
23048aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
23058aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
23068aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
23078aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
23088aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
23098aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
23108aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
23118aa9ebccSVladimir Oltean  */
23128aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
23138aa9ebccSVladimir Oltean {
23148aa9ebccSVladimir Oltean 	struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
23158aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
23168aa9ebccSVladimir Oltean 	int rc;
23178aa9ebccSVladimir Oltean 
23188aa9ebccSVladimir Oltean 	rc = sja1105_parse_dt(priv, ports);
23198aa9ebccSVladimir Oltean 	if (rc < 0) {
23208aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
23218aa9ebccSVladimir Oltean 		return rc;
23228aa9ebccSVladimir Oltean 	}
2323f5b8631cSVladimir Oltean 
2324f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
2325f5b8631cSVladimir Oltean 	 * and we can't apply them.
2326f5b8631cSVladimir Oltean 	 */
2327f5b8631cSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv, ports);
2328f5b8631cSVladimir Oltean 	if (rc < 0) {
2329f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
2330f5b8631cSVladimir Oltean 		return rc;
2331f5b8631cSVladimir Oltean 	}
2332f5b8631cSVladimir Oltean 
233361c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
2334bb77f36aSVladimir Oltean 	if (rc < 0) {
2335bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
2336bb77f36aSVladimir Oltean 		return rc;
2337bb77f36aSVladimir Oltean 	}
23388aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
23398aa9ebccSVladimir Oltean 	rc = sja1105_static_config_load(priv, ports);
23408aa9ebccSVladimir Oltean 	if (rc < 0) {
23418aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
23428aa9ebccSVladimir Oltean 		return rc;
23438aa9ebccSVladimir Oltean 	}
23448aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
23458aa9ebccSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
23468aa9ebccSVladimir Oltean 	if (rc < 0) {
23478aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
23488aa9ebccSVladimir Oltean 		return rc;
23498aa9ebccSVladimir Oltean 	}
23506666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
23516666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
23526666cebcSVladimir Oltean 	 * EtherType is.
23536666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
23546666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
23556666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
23566666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
23576666cebcSVladimir Oltean 	 */
23586666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
23598aa9ebccSVladimir Oltean 
23605f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
23615f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
23625f06c63bSVladimir Oltean 
2363c279c726SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
2364c279c726SVladimir Oltean 
2365fa83e5d9SVladimir Oltean 	ds->configure_vlan_while_not_filtering = true;
2366fa83e5d9SVladimir Oltean 
2367227d07a0SVladimir Oltean 	/* The DSA/switchdev model brings up switch ports in standalone mode by
2368227d07a0SVladimir Oltean 	 * default, and that means vlan_filtering is 0 since they're not under
2369227d07a0SVladimir Oltean 	 * a bridge, so it's safe to set up switch tagging at this time.
2370227d07a0SVladimir Oltean 	 */
2371227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, true);
2372227d07a0SVladimir Oltean }
2373227d07a0SVladimir Oltean 
2374f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
2375f3097be2SVladimir Oltean {
2376f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2377ec5ae610SVladimir Oltean 	struct sja1105_bridge_vlan *v, *n;
2378a68578c2SVladimir Oltean 	int port;
2379a68578c2SVladimir Oltean 
2380a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2381a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2382a68578c2SVladimir Oltean 
2383a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2384a68578c2SVladimir Oltean 			continue;
2385a68578c2SVladimir Oltean 
238652c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
2387a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
2388a68578c2SVladimir Oltean 	}
2389f3097be2SVladimir Oltean 
2390a6af7763SVladimir Oltean 	sja1105_flower_teardown(ds);
2391317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
239261c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
23936cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
2394ec5ae610SVladimir Oltean 
2395ec5ae610SVladimir Oltean 	list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) {
2396ec5ae610SVladimir Oltean 		list_del(&v->list);
2397ec5ae610SVladimir Oltean 		kfree(v);
2398ec5ae610SVladimir Oltean 	}
2399ec5ae610SVladimir Oltean 
2400ec5ae610SVladimir Oltean 	list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) {
2401ec5ae610SVladimir Oltean 		list_del(&v->list);
2402ec5ae610SVladimir Oltean 		kfree(v);
2403ec5ae610SVladimir Oltean 	}
2404f3097be2SVladimir Oltean }
2405f3097be2SVladimir Oltean 
2406e9bf9694SVladimir Oltean static int sja1105_port_enable(struct dsa_switch *ds, int port,
2407e9bf9694SVladimir Oltean 			       struct phy_device *phy)
2408e9bf9694SVladimir Oltean {
2409e9bf9694SVladimir Oltean 	struct net_device *slave;
2410e9bf9694SVladimir Oltean 
2411e9bf9694SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2412e9bf9694SVladimir Oltean 		return 0;
2413e9bf9694SVladimir Oltean 
241468bb8ea8SVivien Didelot 	slave = dsa_to_port(ds, port)->slave;
2415e9bf9694SVladimir Oltean 
2416e9bf9694SVladimir Oltean 	slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
2417e9bf9694SVladimir Oltean 
2418e9bf9694SVladimir Oltean 	return 0;
2419e9bf9694SVladimir Oltean }
2420e9bf9694SVladimir Oltean 
2421a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
2422a68578c2SVladimir Oltean {
2423a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2424a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
2425a68578c2SVladimir Oltean 
2426a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2427a68578c2SVladimir Oltean 		return;
2428a68578c2SVladimir Oltean 
2429a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
2430a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
2431a68578c2SVladimir Oltean }
2432a68578c2SVladimir Oltean 
2433227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
243447ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2435227d07a0SVladimir Oltean {
2436227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2437227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2438227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2439227d07a0SVladimir Oltean 	int timeout = 10;
2440227d07a0SVladimir Oltean 	int rc;
2441227d07a0SVladimir Oltean 
2442227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2443227d07a0SVladimir Oltean 
2444227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2445227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2446227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
244747ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
244847ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2449227d07a0SVladimir Oltean 
2450227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2451227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2452227d07a0SVladimir Oltean 	if (rc < 0) {
2453227d07a0SVladimir Oltean 		kfree_skb(skb);
2454227d07a0SVladimir Oltean 		return rc;
2455227d07a0SVladimir Oltean 	}
2456227d07a0SVladimir Oltean 
2457227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
245868bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2459227d07a0SVladimir Oltean 
2460227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2461227d07a0SVladimir Oltean 	do {
2462227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2463227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2464227d07a0SVladimir Oltean 		if (rc < 0) {
2465227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2466227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2467227d07a0SVladimir Oltean 			continue;
2468227d07a0SVladimir Oltean 		}
2469227d07a0SVladimir Oltean 
2470227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2471227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2472227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2473227d07a0SVladimir Oltean 		 */
2474227d07a0SVladimir Oltean 		cpu_relax();
2475227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2476227d07a0SVladimir Oltean 
2477227d07a0SVladimir Oltean 	if (!timeout) {
2478227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2479227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
24802a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
24812a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2482227d07a0SVladimir Oltean 		 */
2483227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2484227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2485227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2486227d07a0SVladimir Oltean 	}
2487227d07a0SVladimir Oltean 
2488227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2489227d07a0SVladimir Oltean }
2490227d07a0SVladimir Oltean 
2491a68578c2SVladimir Oltean #define work_to_port(work) \
2492a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
2493a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
2494a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
2495a68578c2SVladimir Oltean 
2496227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2497227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2498227d07a0SVladimir Oltean  * lock on the bus)
2499227d07a0SVladimir Oltean  */
2500a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2501227d07a0SVladimir Oltean {
2502a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
2503a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
2504a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
2505a68578c2SVladimir Oltean 	int port = sp - priv->ports;
2506a68578c2SVladimir Oltean 	struct sk_buff *skb;
2507a68578c2SVladimir Oltean 
2508a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
2509a68578c2SVladimir Oltean 		struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
2510227d07a0SVladimir Oltean 
2511227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
2512227d07a0SVladimir Oltean 
2513a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
2514a68578c2SVladimir Oltean 
251547ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
2516a68578c2SVladimir Oltean 		if (clone)
2517a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
2518227d07a0SVladimir Oltean 
2519227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
2520a68578c2SVladimir Oltean 	}
25218aa9ebccSVladimir Oltean }
25228aa9ebccSVladimir Oltean 
25238456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
25248456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
25258456721dSVladimir Oltean  */
25268456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
25278456721dSVladimir Oltean 				   unsigned int ageing_time)
25288456721dSVladimir Oltean {
25298456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
25308456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
25318456721dSVladimir Oltean 	struct sja1105_table *table;
25328456721dSVladimir Oltean 	unsigned int maxage;
25338456721dSVladimir Oltean 
25348456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
25358456721dSVladimir Oltean 	l2_lookup_params = table->entries;
25368456721dSVladimir Oltean 
25378456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
25388456721dSVladimir Oltean 
25398456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
25408456721dSVladimir Oltean 		return 0;
25418456721dSVladimir Oltean 
25428456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
25438456721dSVladimir Oltean 
25442eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
25458456721dSVladimir Oltean }
25468456721dSVladimir Oltean 
2547c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
2548c279c726SVladimir Oltean {
2549c279c726SVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2550c279c726SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2551c279c726SVladimir Oltean 
2552c279c726SVladimir Oltean 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
2553c279c726SVladimir Oltean 
2554c279c726SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
2555c279c726SVladimir Oltean 		new_mtu += VLAN_HLEN;
2556c279c726SVladimir Oltean 
2557c279c726SVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2558c279c726SVladimir Oltean 
2559a7cc081cSVladimir Oltean 	if (policing[port].maxlen == new_mtu)
2560c279c726SVladimir Oltean 		return 0;
2561c279c726SVladimir Oltean 
2562a7cc081cSVladimir Oltean 	policing[port].maxlen = new_mtu;
2563c279c726SVladimir Oltean 
2564c279c726SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2565c279c726SVladimir Oltean }
2566c279c726SVladimir Oltean 
2567c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
2568c279c726SVladimir Oltean {
2569c279c726SVladimir Oltean 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
2570c279c726SVladimir Oltean }
2571c279c726SVladimir Oltean 
2572317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2573317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2574317ab5b8SVladimir Oltean 				 void *type_data)
2575317ab5b8SVladimir Oltean {
2576317ab5b8SVladimir Oltean 	switch (type) {
2577317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2578317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
2579317ab5b8SVladimir Oltean 	default:
2580317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2581317ab5b8SVladimir Oltean 	}
2582317ab5b8SVladimir Oltean }
2583317ab5b8SVladimir Oltean 
2584511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2585511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2586511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2587511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2588511e6ca0SVladimir Oltean  * mirroring rule that references it.
2589511e6ca0SVladimir Oltean  */
2590511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2591511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2592511e6ca0SVladimir Oltean {
2593511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2594511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2595511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2596511e6ca0SVladimir Oltean 	bool already_enabled;
2597511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2598511e6ca0SVladimir Oltean 	int rc;
2599511e6ca0SVladimir Oltean 
2600511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2601511e6ca0SVladimir Oltean 	general_params = table->entries;
2602511e6ca0SVladimir Oltean 
2603511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2604511e6ca0SVladimir Oltean 
2605511e6ca0SVladimir Oltean 	already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS);
2606511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2607511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2608511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2609511e6ca0SVladimir Oltean 			general_params->mirr_port);
2610511e6ca0SVladimir Oltean 		return -EBUSY;
2611511e6ca0SVladimir Oltean 	}
2612511e6ca0SVladimir Oltean 
2613511e6ca0SVladimir Oltean 	new_mirr_port = to;
2614511e6ca0SVladimir Oltean 	if (!enabled) {
2615511e6ca0SVladimir Oltean 		bool keep = false;
2616511e6ca0SVladimir Oltean 		int port;
2617511e6ca0SVladimir Oltean 
2618511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2619511e6ca0SVladimir Oltean 		for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2620511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2621511e6ca0SVladimir Oltean 				keep = true;
2622511e6ca0SVladimir Oltean 				break;
2623511e6ca0SVladimir Oltean 			}
2624511e6ca0SVladimir Oltean 		}
2625511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2626511e6ca0SVladimir Oltean 		if (!keep)
2627511e6ca0SVladimir Oltean 			new_mirr_port = SJA1105_NUM_PORTS;
2628511e6ca0SVladimir Oltean 	}
2629511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2630511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2631511e6ca0SVladimir Oltean 
2632511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2633511e6ca0SVladimir Oltean 						  0, general_params, true);
2634511e6ca0SVladimir Oltean 		if (rc < 0)
2635511e6ca0SVladimir Oltean 			return rc;
2636511e6ca0SVladimir Oltean 	}
2637511e6ca0SVladimir Oltean 
2638511e6ca0SVladimir Oltean 	if (ingress)
2639511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2640511e6ca0SVladimir Oltean 	else
2641511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2642511e6ca0SVladimir Oltean 
2643511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2644511e6ca0SVladimir Oltean 					    &mac[from], true);
2645511e6ca0SVladimir Oltean }
2646511e6ca0SVladimir Oltean 
2647511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2648511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2649511e6ca0SVladimir Oltean 			      bool ingress)
2650511e6ca0SVladimir Oltean {
2651511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2652511e6ca0SVladimir Oltean 				    ingress, true);
2653511e6ca0SVladimir Oltean }
2654511e6ca0SVladimir Oltean 
2655511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2656511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2657511e6ca0SVladimir Oltean {
2658511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2659511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2660511e6ca0SVladimir Oltean }
2661511e6ca0SVladimir Oltean 
2662a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
2663a7cc081cSVladimir Oltean 				    struct dsa_mall_policer_tc_entry *policer)
2664a7cc081cSVladimir Oltean {
2665a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2666a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2667a7cc081cSVladimir Oltean 
2668a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2669a7cc081cSVladimir Oltean 
2670a7cc081cSVladimir Oltean 	/* In hardware, every 8 microseconds the credit level is incremented by
2671a7cc081cSVladimir Oltean 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
2672a7cc081cSVladimir Oltean 	 * bytes.
2673a7cc081cSVladimir Oltean 	 */
2674a7cc081cSVladimir Oltean 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
2675a7cc081cSVladimir Oltean 				      1000000);
2676a7cc081cSVladimir Oltean 	policing[port].smax = div_u64(policer->rate_bytes_per_sec *
2677a7cc081cSVladimir Oltean 				      PSCHED_NS2TICKS(policer->burst),
2678a7cc081cSVladimir Oltean 				      PSCHED_TICKS_PER_SEC);
2679a7cc081cSVladimir Oltean 
2680a7cc081cSVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2681a7cc081cSVladimir Oltean }
2682a7cc081cSVladimir Oltean 
2683a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
2684a7cc081cSVladimir Oltean {
2685a7cc081cSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
2686a7cc081cSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2687a7cc081cSVladimir Oltean 
2688a7cc081cSVladimir Oltean 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
2689a7cc081cSVladimir Oltean 
2690a7cc081cSVladimir Oltean 	policing[port].rate = SJA1105_RATE_MBPS(1000);
2691a7cc081cSVladimir Oltean 	policing[port].smax = 65535;
2692a7cc081cSVladimir Oltean 
2693a7cc081cSVladimir Oltean 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
2694a7cc081cSVladimir Oltean }
2695a7cc081cSVladimir Oltean 
26968aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
26978aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
26988aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
2699f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
27008456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
2701c279c726SVladimir Oltean 	.port_change_mtu	= sja1105_change_mtu,
2702c279c726SVladimir Oltean 	.port_max_mtu		= sja1105_get_max_mtu,
2703ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
2704ffe10e67SVladimir Oltean 	.phylink_mac_link_state	= sja1105_mac_pcs_get_state,
2705af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
27068400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
27078400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
270852c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
270952c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
271052c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
2711bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
2712e9bf9694SVladimir Oltean 	.port_enable		= sja1105_port_enable,
2713a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
2714291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
2715291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
2716291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
27178aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
27188aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
2719640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
27206666cebcSVladimir Oltean 	.port_vlan_prepare	= sja1105_vlan_prepare,
27216666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
27226666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
27236666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
2724291d1e72SVladimir Oltean 	.port_mdb_prepare	= sja1105_mdb_prepare,
2725291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
2726291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
2727a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
2728a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
2729f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
273047ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
2731317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
2732511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
2733511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
2734a7cc081cSVladimir Oltean 	.port_policer_add	= sja1105_port_policer_add,
2735a7cc081cSVladimir Oltean 	.port_policer_del	= sja1105_port_policer_del,
2736a6af7763SVladimir Oltean 	.cls_flower_add		= sja1105_cls_flower_add,
2737a6af7763SVladimir Oltean 	.cls_flower_del		= sja1105_cls_flower_del,
2738834f8933SVladimir Oltean 	.cls_flower_stats	= sja1105_cls_flower_stats,
2739ac02a451SVladimir Oltean 	.crosschip_bridge_join	= sja1105_crosschip_bridge_join,
2740ac02a451SVladimir Oltean 	.crosschip_bridge_leave	= sja1105_crosschip_bridge_leave,
27418aa9ebccSVladimir Oltean };
27428aa9ebccSVladimir Oltean 
27438aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
27448aa9ebccSVladimir Oltean {
27458aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
27468aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
27478aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
2748dff79620SVladimir Oltean 	u32 device_id;
27498aa9ebccSVladimir Oltean 	u64 part_no;
27508aa9ebccSVladimir Oltean 	int rc;
27518aa9ebccSVladimir Oltean 
275234d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
275334d76e9fSVladimir Oltean 			      NULL);
27548aa9ebccSVladimir Oltean 	if (rc < 0)
27558aa9ebccSVladimir Oltean 		return rc;
27568aa9ebccSVladimir Oltean 
27578aa9ebccSVladimir Oltean 	if (device_id != priv->info->device_id) {
2758dff79620SVladimir Oltean 		dev_err(dev, "Expected device ID 0x%llx but read 0x%x\n",
27598aa9ebccSVladimir Oltean 			priv->info->device_id, device_id);
27608aa9ebccSVladimir Oltean 		return -ENODEV;
27618aa9ebccSVladimir Oltean 	}
27628aa9ebccSVladimir Oltean 
27631bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
27641bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
27658aa9ebccSVladimir Oltean 	if (rc < 0)
27668aa9ebccSVladimir Oltean 		return rc;
27678aa9ebccSVladimir Oltean 
27688aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
27698aa9ebccSVladimir Oltean 
27708aa9ebccSVladimir Oltean 	if (part_no != priv->info->part_no) {
27718aa9ebccSVladimir Oltean 		dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n",
27728aa9ebccSVladimir Oltean 			priv->info->part_no, part_no);
27738aa9ebccSVladimir Oltean 		return -ENODEV;
27748aa9ebccSVladimir Oltean 	}
27758aa9ebccSVladimir Oltean 
27768aa9ebccSVladimir Oltean 	return 0;
27778aa9ebccSVladimir Oltean }
27788aa9ebccSVladimir Oltean 
27798aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
27808aa9ebccSVladimir Oltean {
2781844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
27828aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
27838aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
27848aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
2785a68578c2SVladimir Oltean 	int rc, port;
27868aa9ebccSVladimir Oltean 
27878aa9ebccSVladimir Oltean 	if (!dev->of_node) {
27888aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
27898aa9ebccSVladimir Oltean 		return -EINVAL;
27908aa9ebccSVladimir Oltean 	}
27918aa9ebccSVladimir Oltean 
27928aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
27938aa9ebccSVladimir Oltean 	if (!priv)
27948aa9ebccSVladimir Oltean 		return -ENOMEM;
27958aa9ebccSVladimir Oltean 
27968aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
27978aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
27988aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
27998aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
28008aa9ebccSVladimir Oltean 	else
28018aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
28028aa9ebccSVladimir Oltean 
28038aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
28048aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
28058aa9ebccSVladimir Oltean 	 */
28068aa9ebccSVladimir Oltean 	priv->spidev = spi;
28078aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
28088aa9ebccSVladimir Oltean 
28098aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
28108aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
28118aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
28128aa9ebccSVladimir Oltean 	if (rc < 0) {
28138aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
28148aa9ebccSVladimir Oltean 		return rc;
28158aa9ebccSVladimir Oltean 	}
28168aa9ebccSVladimir Oltean 
28178aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
28188aa9ebccSVladimir Oltean 
28198aa9ebccSVladimir Oltean 	/* Detect hardware device */
28208aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
28218aa9ebccSVladimir Oltean 	if (rc < 0) {
28228aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
28238aa9ebccSVladimir Oltean 		return rc;
28248aa9ebccSVladimir Oltean 	}
28258aa9ebccSVladimir Oltean 
28268aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
28278aa9ebccSVladimir Oltean 
28287e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
28298aa9ebccSVladimir Oltean 	if (!ds)
28308aa9ebccSVladimir Oltean 		return -ENOMEM;
28318aa9ebccSVladimir Oltean 
28327e99e347SVivien Didelot 	ds->dev = dev;
28337e99e347SVivien Didelot 	ds->num_ports = SJA1105_NUM_PORTS;
28348aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
28358aa9ebccSVladimir Oltean 	ds->priv = priv;
28368aa9ebccSVladimir Oltean 	priv->ds = ds;
28378aa9ebccSVladimir Oltean 
2838844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
2839844d7edcSVladimir Oltean 
2840d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
2841d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
2842d5a619bfSVivien Didelot 
2843ac02a451SVladimir Oltean 	INIT_LIST_HEAD(&priv->crosschip_links);
2844ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&priv->bridge_vlans);
2845ec5ae610SVladimir Oltean 	INIT_LIST_HEAD(&priv->dsa_8021q_vlans);
2846ac02a451SVladimir Oltean 
2847d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
2848a6af7763SVladimir Oltean 	sja1105_flower_setup(ds);
2849d5a619bfSVivien Didelot 
2850d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
2851d5a619bfSVivien Didelot 	if (rc)
2852d5a619bfSVivien Didelot 		return rc;
2853d5a619bfSVivien Didelot 
2854227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
2855a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2856a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2857a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
2858a68578c2SVladimir Oltean 		struct net_device *slave;
2859227d07a0SVladimir Oltean 
2860a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2861a68578c2SVladimir Oltean 			continue;
2862a68578c2SVladimir Oltean 
2863a68578c2SVladimir Oltean 		dp->priv = sp;
2864a68578c2SVladimir Oltean 		sp->dp = dp;
2865844d7edcSVladimir Oltean 		sp->data = tagger_data;
2866a68578c2SVladimir Oltean 		slave = dp->slave;
2867a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
2868a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
2869a68578c2SVladimir Oltean 							slave->name);
2870a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
2871a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
2872a68578c2SVladimir Oltean 			dev_err(ds->dev,
2873a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
2874a68578c2SVladimir Oltean 				rc);
2875a68578c2SVladimir Oltean 			goto out;
2876a68578c2SVladimir Oltean 		}
2877a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
2878*38b5beeaSVladimir Oltean 		sp->xmit_tpid = ETH_P_SJA1105;
2879227d07a0SVladimir Oltean 	}
2880227d07a0SVladimir Oltean 
2881d5a619bfSVivien Didelot 	return 0;
2882a68578c2SVladimir Oltean out:
2883a68578c2SVladimir Oltean 	while (port-- > 0) {
2884a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2885a68578c2SVladimir Oltean 
2886a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2887a68578c2SVladimir Oltean 			continue;
2888a68578c2SVladimir Oltean 
2889a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
2890a68578c2SVladimir Oltean 	}
2891a68578c2SVladimir Oltean 	return rc;
28928aa9ebccSVladimir Oltean }
28938aa9ebccSVladimir Oltean 
28948aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
28958aa9ebccSVladimir Oltean {
28968aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
28978aa9ebccSVladimir Oltean 
28988aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
28998aa9ebccSVladimir Oltean 	return 0;
29008aa9ebccSVladimir Oltean }
29018aa9ebccSVladimir Oltean 
29028aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
29038aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
29048aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
29058aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
29068aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
29078aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
29088aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
29098aa9ebccSVladimir Oltean 	{ /* sentinel */ },
29108aa9ebccSVladimir Oltean };
29118aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
29128aa9ebccSVladimir Oltean 
29138aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
29148aa9ebccSVladimir Oltean 	.driver = {
29158aa9ebccSVladimir Oltean 		.name  = "sja1105",
29168aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
29178aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
29188aa9ebccSVladimir Oltean 	},
29198aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
29208aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
29218aa9ebccSVladimir Oltean };
29228aa9ebccSVladimir Oltean 
29238aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
29248aa9ebccSVladimir Oltean 
29258aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
29268aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
29278aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
29288aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
2929