xref: /OK3568_Linux_fs/kernel/drivers/net/dsa/sja1105/sja1105_main.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
3*4882a593Smuzhiyun  * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/delay.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/printk.h>
11*4882a593Smuzhiyun #include <linux/spi/spi.h>
12*4882a593Smuzhiyun #include <linux/errno.h>
13*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
14*4882a593Smuzhiyun #include <linux/phylink.h>
15*4882a593Smuzhiyun #include <linux/of.h>
16*4882a593Smuzhiyun #include <linux/of_net.h>
17*4882a593Smuzhiyun #include <linux/of_mdio.h>
18*4882a593Smuzhiyun #include <linux/of_device.h>
19*4882a593Smuzhiyun #include <linux/netdev_features.h>
20*4882a593Smuzhiyun #include <linux/netdevice.h>
21*4882a593Smuzhiyun #include <linux/if_bridge.h>
22*4882a593Smuzhiyun #include <linux/if_ether.h>
23*4882a593Smuzhiyun #include <linux/dsa/8021q.h>
24*4882a593Smuzhiyun #include "sja1105.h"
25*4882a593Smuzhiyun #include "sja1105_sgmii.h"
26*4882a593Smuzhiyun #include "sja1105_tas.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define SJA1105_DEFAULT_VLAN		(VLAN_N_VID - 1)
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun static const struct dsa_switch_ops sja1105_switch_ops;
31*4882a593Smuzhiyun 
sja1105_hw_reset(struct gpio_desc * gpio,unsigned int pulse_len,unsigned int startup_delay)32*4882a593Smuzhiyun static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
33*4882a593Smuzhiyun 			     unsigned int startup_delay)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	gpiod_set_value_cansleep(gpio, 1);
36*4882a593Smuzhiyun 	/* Wait for minimum reset pulse length */
37*4882a593Smuzhiyun 	msleep(pulse_len);
38*4882a593Smuzhiyun 	gpiod_set_value_cansleep(gpio, 0);
39*4882a593Smuzhiyun 	/* Wait until chip is ready after reset */
40*4882a593Smuzhiyun 	msleep(startup_delay);
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun static void
sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry * l2_fwd,int from,int to,bool allow)44*4882a593Smuzhiyun sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
45*4882a593Smuzhiyun 			   int from, int to, bool allow)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	if (allow) {
48*4882a593Smuzhiyun 		l2_fwd[from].bc_domain  |= BIT(to);
49*4882a593Smuzhiyun 		l2_fwd[from].reach_port |= BIT(to);
50*4882a593Smuzhiyun 		l2_fwd[from].fl_domain  |= BIT(to);
51*4882a593Smuzhiyun 	} else {
52*4882a593Smuzhiyun 		l2_fwd[from].bc_domain  &= ~BIT(to);
53*4882a593Smuzhiyun 		l2_fwd[from].reach_port &= ~BIT(to);
54*4882a593Smuzhiyun 		l2_fwd[from].fl_domain  &= ~BIT(to);
55*4882a593Smuzhiyun 	}
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /* Structure used to temporarily transport device tree
59*4882a593Smuzhiyun  * settings into sja1105_setup
60*4882a593Smuzhiyun  */
61*4882a593Smuzhiyun struct sja1105_dt_port {
62*4882a593Smuzhiyun 	phy_interface_t phy_mode;
63*4882a593Smuzhiyun 	sja1105_mii_role_t role;
64*4882a593Smuzhiyun };
65*4882a593Smuzhiyun 
sja1105_init_mac_settings(struct sja1105_private * priv)66*4882a593Smuzhiyun static int sja1105_init_mac_settings(struct sja1105_private *priv)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	struct sja1105_mac_config_entry default_mac = {
69*4882a593Smuzhiyun 		/* Enable all 8 priority queues on egress.
70*4882a593Smuzhiyun 		 * Every queue i holds top[i] - base[i] frames.
71*4882a593Smuzhiyun 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
72*4882a593Smuzhiyun 		 */
73*4882a593Smuzhiyun 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
74*4882a593Smuzhiyun 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
75*4882a593Smuzhiyun 		.enabled = {true, true, true, true, true, true, true, true},
76*4882a593Smuzhiyun 		/* Keep standard IFG of 12 bytes on egress. */
77*4882a593Smuzhiyun 		.ifg = 0,
78*4882a593Smuzhiyun 		/* Always put the MAC speed in automatic mode, where it can be
79*4882a593Smuzhiyun 		 * adjusted at runtime by PHYLINK.
80*4882a593Smuzhiyun 		 */
81*4882a593Smuzhiyun 		.speed = SJA1105_SPEED_AUTO,
82*4882a593Smuzhiyun 		/* No static correction for 1-step 1588 events */
83*4882a593Smuzhiyun 		.tp_delin = 0,
84*4882a593Smuzhiyun 		.tp_delout = 0,
85*4882a593Smuzhiyun 		/* Disable aging for critical TTEthernet traffic */
86*4882a593Smuzhiyun 		.maxage = 0xFF,
87*4882a593Smuzhiyun 		/* Internal VLAN (pvid) to apply to untagged ingress */
88*4882a593Smuzhiyun 		.vlanprio = 0,
89*4882a593Smuzhiyun 		.vlanid = 1,
90*4882a593Smuzhiyun 		.ing_mirr = false,
91*4882a593Smuzhiyun 		.egr_mirr = false,
92*4882a593Smuzhiyun 		/* Don't drop traffic with other EtherType than ETH_P_IP */
93*4882a593Smuzhiyun 		.drpnona664 = false,
94*4882a593Smuzhiyun 		/* Don't drop double-tagged traffic */
95*4882a593Smuzhiyun 		.drpdtag = false,
96*4882a593Smuzhiyun 		/* Don't drop untagged traffic */
97*4882a593Smuzhiyun 		.drpuntag = false,
98*4882a593Smuzhiyun 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
99*4882a593Smuzhiyun 		.retag = false,
100*4882a593Smuzhiyun 		/* Disable learning and I/O on user ports by default -
101*4882a593Smuzhiyun 		 * STP will enable it.
102*4882a593Smuzhiyun 		 */
103*4882a593Smuzhiyun 		.dyn_learn = false,
104*4882a593Smuzhiyun 		.egress = false,
105*4882a593Smuzhiyun 		.ingress = false,
106*4882a593Smuzhiyun 	};
107*4882a593Smuzhiyun 	struct sja1105_mac_config_entry *mac;
108*4882a593Smuzhiyun 	struct sja1105_table *table;
109*4882a593Smuzhiyun 	int i;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* Discard previous MAC Configuration Table */
114*4882a593Smuzhiyun 	if (table->entry_count) {
115*4882a593Smuzhiyun 		kfree(table->entries);
116*4882a593Smuzhiyun 		table->entry_count = 0;
117*4882a593Smuzhiyun 	}
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	table->entries = kcalloc(SJA1105_NUM_PORTS,
120*4882a593Smuzhiyun 				 table->ops->unpacked_entry_size, GFP_KERNEL);
121*4882a593Smuzhiyun 	if (!table->entries)
122*4882a593Smuzhiyun 		return -ENOMEM;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	table->entry_count = SJA1105_NUM_PORTS;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	mac = table->entries;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
129*4882a593Smuzhiyun 		mac[i] = default_mac;
130*4882a593Smuzhiyun 		if (i == dsa_upstream_port(priv->ds, i)) {
131*4882a593Smuzhiyun 			/* STP doesn't get called for CPU port, so we need to
132*4882a593Smuzhiyun 			 * set the I/O parameters statically.
133*4882a593Smuzhiyun 			 */
134*4882a593Smuzhiyun 			mac[i].dyn_learn = true;
135*4882a593Smuzhiyun 			mac[i].ingress = true;
136*4882a593Smuzhiyun 			mac[i].egress = true;
137*4882a593Smuzhiyun 		}
138*4882a593Smuzhiyun 	}
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	return 0;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
sja1105_supports_sgmii(struct sja1105_private * priv,int port)143*4882a593Smuzhiyun static bool sja1105_supports_sgmii(struct sja1105_private *priv, int port)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	if (priv->info->part_no != SJA1105R_PART_NO &&
146*4882a593Smuzhiyun 	    priv->info->part_no != SJA1105S_PART_NO)
147*4882a593Smuzhiyun 		return false;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	if (port != SJA1105_SGMII_PORT)
150*4882a593Smuzhiyun 		return false;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	if (dsa_is_unused_port(priv->ds, port))
153*4882a593Smuzhiyun 		return false;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	return true;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
sja1105_init_mii_settings(struct sja1105_private * priv,struct sja1105_dt_port * ports)158*4882a593Smuzhiyun static int sja1105_init_mii_settings(struct sja1105_private *priv,
159*4882a593Smuzhiyun 				     struct sja1105_dt_port *ports)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun 	struct device *dev = &priv->spidev->dev;
162*4882a593Smuzhiyun 	struct sja1105_xmii_params_entry *mii;
163*4882a593Smuzhiyun 	struct sja1105_table *table;
164*4882a593Smuzhiyun 	int i;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	/* Discard previous xMII Mode Parameters Table */
169*4882a593Smuzhiyun 	if (table->entry_count) {
170*4882a593Smuzhiyun 		kfree(table->entries);
171*4882a593Smuzhiyun 		table->entry_count = 0;
172*4882a593Smuzhiyun 	}
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
175*4882a593Smuzhiyun 				 table->ops->unpacked_entry_size, GFP_KERNEL);
176*4882a593Smuzhiyun 	if (!table->entries)
177*4882a593Smuzhiyun 		return -ENOMEM;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	/* Override table based on PHYLINK DT bindings */
180*4882a593Smuzhiyun 	table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	mii = table->entries;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
185*4882a593Smuzhiyun 		if (dsa_is_unused_port(priv->ds, i))
186*4882a593Smuzhiyun 			continue;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 		switch (ports[i].phy_mode) {
189*4882a593Smuzhiyun 		case PHY_INTERFACE_MODE_MII:
190*4882a593Smuzhiyun 			mii->xmii_mode[i] = XMII_MODE_MII;
191*4882a593Smuzhiyun 			break;
192*4882a593Smuzhiyun 		case PHY_INTERFACE_MODE_RMII:
193*4882a593Smuzhiyun 			mii->xmii_mode[i] = XMII_MODE_RMII;
194*4882a593Smuzhiyun 			break;
195*4882a593Smuzhiyun 		case PHY_INTERFACE_MODE_RGMII:
196*4882a593Smuzhiyun 		case PHY_INTERFACE_MODE_RGMII_ID:
197*4882a593Smuzhiyun 		case PHY_INTERFACE_MODE_RGMII_RXID:
198*4882a593Smuzhiyun 		case PHY_INTERFACE_MODE_RGMII_TXID:
199*4882a593Smuzhiyun 			mii->xmii_mode[i] = XMII_MODE_RGMII;
200*4882a593Smuzhiyun 			break;
201*4882a593Smuzhiyun 		case PHY_INTERFACE_MODE_SGMII:
202*4882a593Smuzhiyun 			if (!sja1105_supports_sgmii(priv, i))
203*4882a593Smuzhiyun 				return -EINVAL;
204*4882a593Smuzhiyun 			mii->xmii_mode[i] = XMII_MODE_SGMII;
205*4882a593Smuzhiyun 			break;
206*4882a593Smuzhiyun 		default:
207*4882a593Smuzhiyun 			dev_err(dev, "Unsupported PHY mode %s!\n",
208*4882a593Smuzhiyun 				phy_modes(ports[i].phy_mode));
209*4882a593Smuzhiyun 			return -EINVAL;
210*4882a593Smuzhiyun 		}
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 		/* Even though the SerDes port is able to drive SGMII autoneg
213*4882a593Smuzhiyun 		 * like a PHY would, from the perspective of the XMII tables,
214*4882a593Smuzhiyun 		 * the SGMII port should always be put in MAC mode.
215*4882a593Smuzhiyun 		 */
216*4882a593Smuzhiyun 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII)
217*4882a593Smuzhiyun 			mii->phy_mac[i] = XMII_MAC;
218*4882a593Smuzhiyun 		else
219*4882a593Smuzhiyun 			mii->phy_mac[i] = ports[i].role;
220*4882a593Smuzhiyun 	}
221*4882a593Smuzhiyun 	return 0;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun 
sja1105_init_static_fdb(struct sja1105_private * priv)224*4882a593Smuzhiyun static int sja1105_init_static_fdb(struct sja1105_private *priv)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	struct sja1105_table *table;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	/* We only populate the FDB table through dynamic
231*4882a593Smuzhiyun 	 * L2 Address Lookup entries
232*4882a593Smuzhiyun 	 */
233*4882a593Smuzhiyun 	if (table->entry_count) {
234*4882a593Smuzhiyun 		kfree(table->entries);
235*4882a593Smuzhiyun 		table->entry_count = 0;
236*4882a593Smuzhiyun 	}
237*4882a593Smuzhiyun 	return 0;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
sja1105_init_l2_lookup_params(struct sja1105_private * priv)240*4882a593Smuzhiyun static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	struct sja1105_table *table;
243*4882a593Smuzhiyun 	u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
244*4882a593Smuzhiyun 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
245*4882a593Smuzhiyun 		/* Learned FDB entries are forgotten after 300 seconds */
246*4882a593Smuzhiyun 		.maxage = SJA1105_AGEING_TIME_MS(300000),
247*4882a593Smuzhiyun 		/* All entries within a FDB bin are available for learning */
248*4882a593Smuzhiyun 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
249*4882a593Smuzhiyun 		/* And the P/Q/R/S equivalent setting: */
250*4882a593Smuzhiyun 		.start_dynspc = 0,
251*4882a593Smuzhiyun 		.maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
252*4882a593Smuzhiyun 			     max_fdb_entries, max_fdb_entries, },
253*4882a593Smuzhiyun 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
254*4882a593Smuzhiyun 		.poly = 0x97,
255*4882a593Smuzhiyun 		/* This selects between Independent VLAN Learning (IVL) and
256*4882a593Smuzhiyun 		 * Shared VLAN Learning (SVL)
257*4882a593Smuzhiyun 		 */
258*4882a593Smuzhiyun 		.shared_learn = true,
259*4882a593Smuzhiyun 		/* Don't discard management traffic based on ENFPORT -
260*4882a593Smuzhiyun 		 * we don't perform SMAC port enforcement anyway, so
261*4882a593Smuzhiyun 		 * what we are setting here doesn't matter.
262*4882a593Smuzhiyun 		 */
263*4882a593Smuzhiyun 		.no_enf_hostprt = false,
264*4882a593Smuzhiyun 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
265*4882a593Smuzhiyun 		 * Maybe correlate with no_linklocal_learn from bridge driver?
266*4882a593Smuzhiyun 		 */
267*4882a593Smuzhiyun 		.no_mgmt_learn = true,
268*4882a593Smuzhiyun 		/* P/Q/R/S only */
269*4882a593Smuzhiyun 		.use_static = true,
270*4882a593Smuzhiyun 		/* Dynamically learned FDB entries can overwrite other (older)
271*4882a593Smuzhiyun 		 * dynamic FDB entries
272*4882a593Smuzhiyun 		 */
273*4882a593Smuzhiyun 		.owr_dyn = true,
274*4882a593Smuzhiyun 		.drpnolearn = true,
275*4882a593Smuzhiyun 	};
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	if (table->entry_count) {
280*4882a593Smuzhiyun 		kfree(table->entries);
281*4882a593Smuzhiyun 		table->entry_count = 0;
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
285*4882a593Smuzhiyun 				 table->ops->unpacked_entry_size, GFP_KERNEL);
286*4882a593Smuzhiyun 	if (!table->entries)
287*4882a593Smuzhiyun 		return -ENOMEM;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	/* This table only has a single entry */
292*4882a593Smuzhiyun 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
293*4882a593Smuzhiyun 				default_l2_lookup_params;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	return 0;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun /* Set up a default VLAN for untagged traffic injected from the CPU
299*4882a593Smuzhiyun  * using management routes (e.g. STP, PTP) as opposed to tag_8021q.
300*4882a593Smuzhiyun  * All DT-defined ports are members of this VLAN, and there are no
301*4882a593Smuzhiyun  * restrictions on forwarding (since the CPU selects the destination).
302*4882a593Smuzhiyun  * Frames from this VLAN will always be transmitted as untagged, and
303*4882a593Smuzhiyun  * neither the bridge nor the 8021q module cannot create this VLAN ID.
304*4882a593Smuzhiyun  */
sja1105_init_static_vlan(struct sja1105_private * priv)305*4882a593Smuzhiyun static int sja1105_init_static_vlan(struct sja1105_private *priv)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun 	struct sja1105_table *table;
308*4882a593Smuzhiyun 	struct sja1105_vlan_lookup_entry pvid = {
309*4882a593Smuzhiyun 		.ving_mirr = 0,
310*4882a593Smuzhiyun 		.vegr_mirr = 0,
311*4882a593Smuzhiyun 		.vmemb_port = 0,
312*4882a593Smuzhiyun 		.vlan_bc = 0,
313*4882a593Smuzhiyun 		.tag_port = 0,
314*4882a593Smuzhiyun 		.vlanid = SJA1105_DEFAULT_VLAN,
315*4882a593Smuzhiyun 	};
316*4882a593Smuzhiyun 	struct dsa_switch *ds = priv->ds;
317*4882a593Smuzhiyun 	int port;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	if (table->entry_count) {
322*4882a593Smuzhiyun 		kfree(table->entries);
323*4882a593Smuzhiyun 		table->entry_count = 0;
324*4882a593Smuzhiyun 	}
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
327*4882a593Smuzhiyun 				 GFP_KERNEL);
328*4882a593Smuzhiyun 	if (!table->entries)
329*4882a593Smuzhiyun 		return -ENOMEM;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	table->entry_count = 1;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	for (port = 0; port < ds->num_ports; port++) {
334*4882a593Smuzhiyun 		struct sja1105_bridge_vlan *v;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 		if (dsa_is_unused_port(ds, port))
337*4882a593Smuzhiyun 			continue;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 		pvid.vmemb_port |= BIT(port);
340*4882a593Smuzhiyun 		pvid.vlan_bc |= BIT(port);
341*4882a593Smuzhiyun 		pvid.tag_port &= ~BIT(port);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 		v = kzalloc(sizeof(*v), GFP_KERNEL);
344*4882a593Smuzhiyun 		if (!v)
345*4882a593Smuzhiyun 			return -ENOMEM;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 		v->port = port;
348*4882a593Smuzhiyun 		v->vid = SJA1105_DEFAULT_VLAN;
349*4882a593Smuzhiyun 		v->untagged = true;
350*4882a593Smuzhiyun 		if (dsa_is_cpu_port(ds, port))
351*4882a593Smuzhiyun 			v->pvid = true;
352*4882a593Smuzhiyun 		list_add(&v->list, &priv->dsa_8021q_vlans);
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 		v = kmemdup(v, sizeof(*v), GFP_KERNEL);
355*4882a593Smuzhiyun 		if (!v)
356*4882a593Smuzhiyun 			return -ENOMEM;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 		list_add(&v->list, &priv->bridge_vlans);
359*4882a593Smuzhiyun 	}
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
362*4882a593Smuzhiyun 	return 0;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
sja1105_init_l2_forwarding(struct sja1105_private * priv)365*4882a593Smuzhiyun static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun 	struct sja1105_l2_forwarding_entry *l2fwd;
368*4882a593Smuzhiyun 	struct sja1105_table *table;
369*4882a593Smuzhiyun 	int i, j;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	if (table->entry_count) {
374*4882a593Smuzhiyun 		kfree(table->entries);
375*4882a593Smuzhiyun 		table->entry_count = 0;
376*4882a593Smuzhiyun 	}
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
379*4882a593Smuzhiyun 				 table->ops->unpacked_entry_size, GFP_KERNEL);
380*4882a593Smuzhiyun 	if (!table->entries)
381*4882a593Smuzhiyun 		return -ENOMEM;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	l2fwd = table->entries;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/* First 5 entries define the forwarding rules */
388*4882a593Smuzhiyun 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
389*4882a593Smuzhiyun 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 		for (j = 0; j < SJA1105_NUM_TC; j++)
392*4882a593Smuzhiyun 			l2fwd[i].vlan_pmap[j] = j;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 		if (i == upstream)
395*4882a593Smuzhiyun 			continue;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
398*4882a593Smuzhiyun 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
399*4882a593Smuzhiyun 	}
400*4882a593Smuzhiyun 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
401*4882a593Smuzhiyun 	 * Create a one-to-one mapping.
402*4882a593Smuzhiyun 	 */
403*4882a593Smuzhiyun 	for (i = 0; i < SJA1105_NUM_TC; i++)
404*4882a593Smuzhiyun 		for (j = 0; j < SJA1105_NUM_PORTS; j++)
405*4882a593Smuzhiyun 			l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	return 0;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun 
sja1105_init_l2_forwarding_params(struct sja1105_private * priv)410*4882a593Smuzhiyun static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun 	struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
413*4882a593Smuzhiyun 		/* Disallow dynamic reconfiguration of vlan_pmap */
414*4882a593Smuzhiyun 		.max_dynp = 0,
415*4882a593Smuzhiyun 		/* Use a single memory partition for all ingress queues */
416*4882a593Smuzhiyun 		.part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
417*4882a593Smuzhiyun 	};
418*4882a593Smuzhiyun 	struct sja1105_table *table;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	if (table->entry_count) {
423*4882a593Smuzhiyun 		kfree(table->entries);
424*4882a593Smuzhiyun 		table->entry_count = 0;
425*4882a593Smuzhiyun 	}
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
428*4882a593Smuzhiyun 				 table->ops->unpacked_entry_size, GFP_KERNEL);
429*4882a593Smuzhiyun 	if (!table->entries)
430*4882a593Smuzhiyun 		return -ENOMEM;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	/* This table only has a single entry */
435*4882a593Smuzhiyun 	((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
436*4882a593Smuzhiyun 				default_l2fwd_params;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	return 0;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun 
sja1105_frame_memory_partitioning(struct sja1105_private * priv)441*4882a593Smuzhiyun void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
444*4882a593Smuzhiyun 	struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
445*4882a593Smuzhiyun 	struct sja1105_table *table;
446*4882a593Smuzhiyun 	int max_mem;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	/* VLAN retagging is implemented using a loopback port that consumes
449*4882a593Smuzhiyun 	 * frame buffers. That leaves less for us.
450*4882a593Smuzhiyun 	 */
451*4882a593Smuzhiyun 	if (priv->vlan_state == SJA1105_VLAN_BEST_EFFORT)
452*4882a593Smuzhiyun 		max_mem = SJA1105_MAX_FRAME_MEMORY_RETAGGING;
453*4882a593Smuzhiyun 	else
454*4882a593Smuzhiyun 		max_mem = SJA1105_MAX_FRAME_MEMORY;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
457*4882a593Smuzhiyun 	l2_fwd_params = table->entries;
458*4882a593Smuzhiyun 	l2_fwd_params->part_spc[0] = max_mem;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	/* If we have any critical-traffic virtual links, we need to reserve
461*4882a593Smuzhiyun 	 * some frame buffer memory for them. At the moment, hardcode the value
462*4882a593Smuzhiyun 	 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
463*4882a593Smuzhiyun 	 * remaining for best-effort traffic. TODO: figure out a more flexible
464*4882a593Smuzhiyun 	 * way to perform the frame buffer partitioning.
465*4882a593Smuzhiyun 	 */
466*4882a593Smuzhiyun 	if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
467*4882a593Smuzhiyun 		return;
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
470*4882a593Smuzhiyun 	vl_fwd_params = table->entries;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
473*4882a593Smuzhiyun 	vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun 
sja1105_init_general_params(struct sja1105_private * priv)476*4882a593Smuzhiyun static int sja1105_init_general_params(struct sja1105_private *priv)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	struct sja1105_general_params_entry default_general_params = {
479*4882a593Smuzhiyun 		/* Allow dynamic changing of the mirror port */
480*4882a593Smuzhiyun 		.mirr_ptacu = true,
481*4882a593Smuzhiyun 		.switchid = priv->ds->index,
482*4882a593Smuzhiyun 		/* Priority queue for link-local management frames
483*4882a593Smuzhiyun 		 * (both ingress to and egress from CPU - PTP, STP etc)
484*4882a593Smuzhiyun 		 */
485*4882a593Smuzhiyun 		.hostprio = 7,
486*4882a593Smuzhiyun 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
487*4882a593Smuzhiyun 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
488*4882a593Smuzhiyun 		.incl_srcpt1 = false,
489*4882a593Smuzhiyun 		.send_meta1  = false,
490*4882a593Smuzhiyun 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
491*4882a593Smuzhiyun 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
492*4882a593Smuzhiyun 		.incl_srcpt0 = false,
493*4882a593Smuzhiyun 		.send_meta0  = false,
494*4882a593Smuzhiyun 		/* The destination for traffic matching mac_fltres1 and
495*4882a593Smuzhiyun 		 * mac_fltres0 on all ports except host_port. Such traffic
496*4882a593Smuzhiyun 		 * receieved on host_port itself would be dropped, except
497*4882a593Smuzhiyun 		 * by installing a temporary 'management route'
498*4882a593Smuzhiyun 		 */
499*4882a593Smuzhiyun 		.host_port = dsa_upstream_port(priv->ds, 0),
500*4882a593Smuzhiyun 		/* Default to an invalid value */
501*4882a593Smuzhiyun 		.mirr_port = SJA1105_NUM_PORTS,
502*4882a593Smuzhiyun 		/* Link-local traffic received on casc_port will be forwarded
503*4882a593Smuzhiyun 		 * to host_port without embedding the source port and device ID
504*4882a593Smuzhiyun 		 * info in the destination MAC address (presumably because it
505*4882a593Smuzhiyun 		 * is a cascaded port and a downstream SJA switch already did
506*4882a593Smuzhiyun 		 * that). Default to an invalid port (to disable the feature)
507*4882a593Smuzhiyun 		 * and overwrite this if we find any DSA (cascaded) ports.
508*4882a593Smuzhiyun 		 */
509*4882a593Smuzhiyun 		.casc_port = SJA1105_NUM_PORTS,
510*4882a593Smuzhiyun 		/* No TTEthernet */
511*4882a593Smuzhiyun 		.vllupformat = SJA1105_VL_FORMAT_PSFP,
512*4882a593Smuzhiyun 		.vlmarker = 0,
513*4882a593Smuzhiyun 		.vlmask = 0,
514*4882a593Smuzhiyun 		/* Only update correctionField for 1-step PTP (L2 transport) */
515*4882a593Smuzhiyun 		.ignore2stf = 0,
516*4882a593Smuzhiyun 		/* Forcefully disable VLAN filtering by telling
517*4882a593Smuzhiyun 		 * the switch that VLAN has a different EtherType.
518*4882a593Smuzhiyun 		 */
519*4882a593Smuzhiyun 		.tpid = ETH_P_SJA1105,
520*4882a593Smuzhiyun 		.tpid2 = ETH_P_SJA1105,
521*4882a593Smuzhiyun 	};
522*4882a593Smuzhiyun 	struct sja1105_table *table;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	if (table->entry_count) {
527*4882a593Smuzhiyun 		kfree(table->entries);
528*4882a593Smuzhiyun 		table->entry_count = 0;
529*4882a593Smuzhiyun 	}
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 	table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
532*4882a593Smuzhiyun 				 table->ops->unpacked_entry_size, GFP_KERNEL);
533*4882a593Smuzhiyun 	if (!table->entries)
534*4882a593Smuzhiyun 		return -ENOMEM;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	/* This table only has a single entry */
539*4882a593Smuzhiyun 	((struct sja1105_general_params_entry *)table->entries)[0] =
540*4882a593Smuzhiyun 				default_general_params;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	return 0;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun 
sja1105_init_avb_params(struct sja1105_private * priv)545*4882a593Smuzhiyun static int sja1105_init_avb_params(struct sja1105_private *priv)
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun 	struct sja1105_avb_params_entry *avb;
548*4882a593Smuzhiyun 	struct sja1105_table *table;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	/* Discard previous AVB Parameters Table */
553*4882a593Smuzhiyun 	if (table->entry_count) {
554*4882a593Smuzhiyun 		kfree(table->entries);
555*4882a593Smuzhiyun 		table->entry_count = 0;
556*4882a593Smuzhiyun 	}
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
559*4882a593Smuzhiyun 				 table->ops->unpacked_entry_size, GFP_KERNEL);
560*4882a593Smuzhiyun 	if (!table->entries)
561*4882a593Smuzhiyun 		return -ENOMEM;
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	avb = table->entries;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	/* Configure the MAC addresses for meta frames */
568*4882a593Smuzhiyun 	avb->destmeta = SJA1105_META_DMAC;
569*4882a593Smuzhiyun 	avb->srcmeta  = SJA1105_META_SMAC;
570*4882a593Smuzhiyun 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
571*4882a593Smuzhiyun 	 * default. This is because there might be boards with a hardware
572*4882a593Smuzhiyun 	 * layout where enabling the pin as output might cause an electrical
573*4882a593Smuzhiyun 	 * clash. On E/T the pin is always an output, which the board designers
574*4882a593Smuzhiyun 	 * probably already knew, so even if there are going to be electrical
575*4882a593Smuzhiyun 	 * issues, there's nothing we can do.
576*4882a593Smuzhiyun 	 */
577*4882a593Smuzhiyun 	avb->cas_master = false;
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 	return 0;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun /* The L2 policing table is 2-stage. The table is looked up for each frame
583*4882a593Smuzhiyun  * according to the ingress port, whether it was broadcast or not, and the
584*4882a593Smuzhiyun  * classified traffic class (given by VLAN PCP). This portion of the lookup is
585*4882a593Smuzhiyun  * fixed, and gives access to the SHARINDX, an indirection register pointing
586*4882a593Smuzhiyun  * within the policing table itself, which is used to resolve the policer that
587*4882a593Smuzhiyun  * will be used for this frame.
588*4882a593Smuzhiyun  *
589*4882a593Smuzhiyun  *  Stage 1                              Stage 2
590*4882a593Smuzhiyun  * +------------+--------+              +---------------------------------+
591*4882a593Smuzhiyun  * |Port 0 TC 0 |SHARINDX|              | Policer 0: Rate, Burst, MTU     |
592*4882a593Smuzhiyun  * +------------+--------+              +---------------------------------+
593*4882a593Smuzhiyun  * |Port 0 TC 1 |SHARINDX|              | Policer 1: Rate, Burst, MTU     |
594*4882a593Smuzhiyun  * +------------+--------+              +---------------------------------+
595*4882a593Smuzhiyun  *    ...                               | Policer 2: Rate, Burst, MTU     |
596*4882a593Smuzhiyun  * +------------+--------+              +---------------------------------+
597*4882a593Smuzhiyun  * |Port 0 TC 7 |SHARINDX|              | Policer 3: Rate, Burst, MTU     |
598*4882a593Smuzhiyun  * +------------+--------+              +---------------------------------+
599*4882a593Smuzhiyun  * |Port 1 TC 0 |SHARINDX|              | Policer 4: Rate, Burst, MTU     |
600*4882a593Smuzhiyun  * +------------+--------+              +---------------------------------+
601*4882a593Smuzhiyun  *    ...                               | Policer 5: Rate, Burst, MTU     |
602*4882a593Smuzhiyun  * +------------+--------+              +---------------------------------+
603*4882a593Smuzhiyun  * |Port 1 TC 7 |SHARINDX|              | Policer 6: Rate, Burst, MTU     |
604*4882a593Smuzhiyun  * +------------+--------+              +---------------------------------+
605*4882a593Smuzhiyun  *    ...                               | Policer 7: Rate, Burst, MTU     |
606*4882a593Smuzhiyun  * +------------+--------+              +---------------------------------+
607*4882a593Smuzhiyun  * |Port 4 TC 7 |SHARINDX|                 ...
608*4882a593Smuzhiyun  * +------------+--------+
609*4882a593Smuzhiyun  * |Port 0 BCAST|SHARINDX|                 ...
610*4882a593Smuzhiyun  * +------------+--------+
611*4882a593Smuzhiyun  * |Port 1 BCAST|SHARINDX|                 ...
612*4882a593Smuzhiyun  * +------------+--------+
613*4882a593Smuzhiyun  *    ...                                  ...
614*4882a593Smuzhiyun  * +------------+--------+              +---------------------------------+
615*4882a593Smuzhiyun  * |Port 4 BCAST|SHARINDX|              | Policer 44: Rate, Burst, MTU    |
616*4882a593Smuzhiyun  * +------------+--------+              +---------------------------------+
617*4882a593Smuzhiyun  *
618*4882a593Smuzhiyun  * In this driver, we shall use policers 0-4 as statically alocated port
619*4882a593Smuzhiyun  * (matchall) policers. So we need to make the SHARINDX for all lookups
620*4882a593Smuzhiyun  * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
621*4882a593Smuzhiyun  * lookup) equal.
622*4882a593Smuzhiyun  * The remaining policers (40) shall be dynamically allocated for flower
623*4882a593Smuzhiyun  * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
624*4882a593Smuzhiyun  */
625*4882a593Smuzhiyun #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
626*4882a593Smuzhiyun 
sja1105_init_l2_policing(struct sja1105_private * priv)627*4882a593Smuzhiyun static int sja1105_init_l2_policing(struct sja1105_private *priv)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun 	struct sja1105_l2_policing_entry *policing;
630*4882a593Smuzhiyun 	struct sja1105_table *table;
631*4882a593Smuzhiyun 	int port, tc;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	/* Discard previous L2 Policing Table */
636*4882a593Smuzhiyun 	if (table->entry_count) {
637*4882a593Smuzhiyun 		kfree(table->entries);
638*4882a593Smuzhiyun 		table->entry_count = 0;
639*4882a593Smuzhiyun 	}
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
642*4882a593Smuzhiyun 				 table->ops->unpacked_entry_size, GFP_KERNEL);
643*4882a593Smuzhiyun 	if (!table->entries)
644*4882a593Smuzhiyun 		return -ENOMEM;
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	policing = table->entries;
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 	/* Setup shared indices for the matchall policers */
651*4882a593Smuzhiyun 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
652*4882a593Smuzhiyun 		int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + port;
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 		for (tc = 0; tc < SJA1105_NUM_TC; tc++)
655*4882a593Smuzhiyun 			policing[port * SJA1105_NUM_TC + tc].sharindx = port;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 		policing[bcast].sharindx = port;
658*4882a593Smuzhiyun 	}
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	/* Setup the matchall policer parameters */
661*4882a593Smuzhiyun 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
662*4882a593Smuzhiyun 		int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 		if (dsa_is_cpu_port(priv->ds, port))
665*4882a593Smuzhiyun 			mtu += VLAN_HLEN;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 		policing[port].smax = 65535; /* Burst size in bytes */
668*4882a593Smuzhiyun 		policing[port].rate = SJA1105_RATE_MBPS(1000);
669*4882a593Smuzhiyun 		policing[port].maxlen = mtu;
670*4882a593Smuzhiyun 		policing[port].partition = 0;
671*4882a593Smuzhiyun 	}
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	return 0;
674*4882a593Smuzhiyun }
675*4882a593Smuzhiyun 
sja1105_static_config_load(struct sja1105_private * priv,struct sja1105_dt_port * ports)676*4882a593Smuzhiyun static int sja1105_static_config_load(struct sja1105_private *priv,
677*4882a593Smuzhiyun 				      struct sja1105_dt_port *ports)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun 	int rc;
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	sja1105_static_config_free(&priv->static_config);
682*4882a593Smuzhiyun 	rc = sja1105_static_config_init(&priv->static_config,
683*4882a593Smuzhiyun 					priv->info->static_ops,
684*4882a593Smuzhiyun 					priv->info->device_id);
685*4882a593Smuzhiyun 	if (rc)
686*4882a593Smuzhiyun 		return rc;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	/* Build static configuration */
689*4882a593Smuzhiyun 	rc = sja1105_init_mac_settings(priv);
690*4882a593Smuzhiyun 	if (rc < 0)
691*4882a593Smuzhiyun 		return rc;
692*4882a593Smuzhiyun 	rc = sja1105_init_mii_settings(priv, ports);
693*4882a593Smuzhiyun 	if (rc < 0)
694*4882a593Smuzhiyun 		return rc;
695*4882a593Smuzhiyun 	rc = sja1105_init_static_fdb(priv);
696*4882a593Smuzhiyun 	if (rc < 0)
697*4882a593Smuzhiyun 		return rc;
698*4882a593Smuzhiyun 	rc = sja1105_init_static_vlan(priv);
699*4882a593Smuzhiyun 	if (rc < 0)
700*4882a593Smuzhiyun 		return rc;
701*4882a593Smuzhiyun 	rc = sja1105_init_l2_lookup_params(priv);
702*4882a593Smuzhiyun 	if (rc < 0)
703*4882a593Smuzhiyun 		return rc;
704*4882a593Smuzhiyun 	rc = sja1105_init_l2_forwarding(priv);
705*4882a593Smuzhiyun 	if (rc < 0)
706*4882a593Smuzhiyun 		return rc;
707*4882a593Smuzhiyun 	rc = sja1105_init_l2_forwarding_params(priv);
708*4882a593Smuzhiyun 	if (rc < 0)
709*4882a593Smuzhiyun 		return rc;
710*4882a593Smuzhiyun 	rc = sja1105_init_l2_policing(priv);
711*4882a593Smuzhiyun 	if (rc < 0)
712*4882a593Smuzhiyun 		return rc;
713*4882a593Smuzhiyun 	rc = sja1105_init_general_params(priv);
714*4882a593Smuzhiyun 	if (rc < 0)
715*4882a593Smuzhiyun 		return rc;
716*4882a593Smuzhiyun 	rc = sja1105_init_avb_params(priv);
717*4882a593Smuzhiyun 	if (rc < 0)
718*4882a593Smuzhiyun 		return rc;
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	/* Send initial configuration to hardware via SPI */
721*4882a593Smuzhiyun 	return sja1105_static_config_upload(priv);
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun 
sja1105_parse_rgmii_delays(struct sja1105_private * priv,const struct sja1105_dt_port * ports)724*4882a593Smuzhiyun static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
725*4882a593Smuzhiyun 				      const struct sja1105_dt_port *ports)
726*4882a593Smuzhiyun {
727*4882a593Smuzhiyun 	int i;
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
730*4882a593Smuzhiyun 		if (ports[i].role == XMII_MAC)
731*4882a593Smuzhiyun 			continue;
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
734*4882a593Smuzhiyun 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
735*4882a593Smuzhiyun 			priv->rgmii_rx_delay[i] = true;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
738*4882a593Smuzhiyun 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
739*4882a593Smuzhiyun 			priv->rgmii_tx_delay[i] = true;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
742*4882a593Smuzhiyun 		     !priv->info->setup_rgmii_delay)
743*4882a593Smuzhiyun 			return -EINVAL;
744*4882a593Smuzhiyun 	}
745*4882a593Smuzhiyun 	return 0;
746*4882a593Smuzhiyun }
747*4882a593Smuzhiyun 
sja1105_parse_ports_node(struct sja1105_private * priv,struct sja1105_dt_port * ports,struct device_node * ports_node)748*4882a593Smuzhiyun static int sja1105_parse_ports_node(struct sja1105_private *priv,
749*4882a593Smuzhiyun 				    struct sja1105_dt_port *ports,
750*4882a593Smuzhiyun 				    struct device_node *ports_node)
751*4882a593Smuzhiyun {
752*4882a593Smuzhiyun 	struct device *dev = &priv->spidev->dev;
753*4882a593Smuzhiyun 	struct device_node *child;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	for_each_available_child_of_node(ports_node, child) {
756*4882a593Smuzhiyun 		struct device_node *phy_node;
757*4882a593Smuzhiyun 		phy_interface_t phy_mode;
758*4882a593Smuzhiyun 		u32 index;
759*4882a593Smuzhiyun 		int err;
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 		/* Get switch port number from DT */
762*4882a593Smuzhiyun 		if (of_property_read_u32(child, "reg", &index) < 0) {
763*4882a593Smuzhiyun 			dev_err(dev, "Port number not defined in device tree "
764*4882a593Smuzhiyun 				"(property \"reg\")\n");
765*4882a593Smuzhiyun 			of_node_put(child);
766*4882a593Smuzhiyun 			return -ENODEV;
767*4882a593Smuzhiyun 		}
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 		/* Get PHY mode from DT */
770*4882a593Smuzhiyun 		err = of_get_phy_mode(child, &phy_mode);
771*4882a593Smuzhiyun 		if (err) {
772*4882a593Smuzhiyun 			dev_err(dev, "Failed to read phy-mode or "
773*4882a593Smuzhiyun 				"phy-interface-type property for port %d\n",
774*4882a593Smuzhiyun 				index);
775*4882a593Smuzhiyun 			of_node_put(child);
776*4882a593Smuzhiyun 			return -ENODEV;
777*4882a593Smuzhiyun 		}
778*4882a593Smuzhiyun 		ports[index].phy_mode = phy_mode;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 		phy_node = of_parse_phandle(child, "phy-handle", 0);
781*4882a593Smuzhiyun 		if (!phy_node) {
782*4882a593Smuzhiyun 			if (!of_phy_is_fixed_link(child)) {
783*4882a593Smuzhiyun 				dev_err(dev, "phy-handle or fixed-link "
784*4882a593Smuzhiyun 					"properties missing!\n");
785*4882a593Smuzhiyun 				of_node_put(child);
786*4882a593Smuzhiyun 				return -ENODEV;
787*4882a593Smuzhiyun 			}
788*4882a593Smuzhiyun 			/* phy-handle is missing, but fixed-link isn't.
789*4882a593Smuzhiyun 			 * So it's a fixed link. Default to PHY role.
790*4882a593Smuzhiyun 			 */
791*4882a593Smuzhiyun 			ports[index].role = XMII_PHY;
792*4882a593Smuzhiyun 		} else {
793*4882a593Smuzhiyun 			/* phy-handle present => put port in MAC role */
794*4882a593Smuzhiyun 			ports[index].role = XMII_MAC;
795*4882a593Smuzhiyun 			of_node_put(phy_node);
796*4882a593Smuzhiyun 		}
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 		/* The MAC/PHY role can be overridden with explicit bindings */
799*4882a593Smuzhiyun 		if (of_property_read_bool(child, "sja1105,role-mac"))
800*4882a593Smuzhiyun 			ports[index].role = XMII_MAC;
801*4882a593Smuzhiyun 		else if (of_property_read_bool(child, "sja1105,role-phy"))
802*4882a593Smuzhiyun 			ports[index].role = XMII_PHY;
803*4882a593Smuzhiyun 	}
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	return 0;
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun 
sja1105_parse_dt(struct sja1105_private * priv,struct sja1105_dt_port * ports)808*4882a593Smuzhiyun static int sja1105_parse_dt(struct sja1105_private *priv,
809*4882a593Smuzhiyun 			    struct sja1105_dt_port *ports)
810*4882a593Smuzhiyun {
811*4882a593Smuzhiyun 	struct device *dev = &priv->spidev->dev;
812*4882a593Smuzhiyun 	struct device_node *switch_node = dev->of_node;
813*4882a593Smuzhiyun 	struct device_node *ports_node;
814*4882a593Smuzhiyun 	int rc;
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	ports_node = of_get_child_by_name(switch_node, "ports");
817*4882a593Smuzhiyun 	if (!ports_node) {
818*4882a593Smuzhiyun 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
819*4882a593Smuzhiyun 		return -ENODEV;
820*4882a593Smuzhiyun 	}
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
823*4882a593Smuzhiyun 	of_node_put(ports_node);
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	return rc;
826*4882a593Smuzhiyun }
827*4882a593Smuzhiyun 
sja1105_sgmii_read(struct sja1105_private * priv,int pcs_reg)828*4882a593Smuzhiyun static int sja1105_sgmii_read(struct sja1105_private *priv, int pcs_reg)
829*4882a593Smuzhiyun {
830*4882a593Smuzhiyun 	const struct sja1105_regs *regs = priv->info->regs;
831*4882a593Smuzhiyun 	u32 val;
832*4882a593Smuzhiyun 	int rc;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->sgmii + pcs_reg, &val,
835*4882a593Smuzhiyun 			      NULL);
836*4882a593Smuzhiyun 	if (rc < 0)
837*4882a593Smuzhiyun 		return rc;
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	return val;
840*4882a593Smuzhiyun }
841*4882a593Smuzhiyun 
sja1105_sgmii_write(struct sja1105_private * priv,int pcs_reg,u16 pcs_val)842*4882a593Smuzhiyun static int sja1105_sgmii_write(struct sja1105_private *priv, int pcs_reg,
843*4882a593Smuzhiyun 			       u16 pcs_val)
844*4882a593Smuzhiyun {
845*4882a593Smuzhiyun 	const struct sja1105_regs *regs = priv->info->regs;
846*4882a593Smuzhiyun 	u32 val = pcs_val;
847*4882a593Smuzhiyun 	int rc;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->sgmii + pcs_reg, &val,
850*4882a593Smuzhiyun 			      NULL);
851*4882a593Smuzhiyun 	if (rc < 0)
852*4882a593Smuzhiyun 		return rc;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	return val;
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
sja1105_sgmii_pcs_config(struct sja1105_private * priv,bool an_enabled,bool an_master)857*4882a593Smuzhiyun static void sja1105_sgmii_pcs_config(struct sja1105_private *priv,
858*4882a593Smuzhiyun 				     bool an_enabled, bool an_master)
859*4882a593Smuzhiyun {
860*4882a593Smuzhiyun 	u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII;
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	/* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
863*4882a593Smuzhiyun 	 * stop the clock during LPI mode, make the MAC reconfigure
864*4882a593Smuzhiyun 	 * autonomously after PCS autoneg is done, flush the internal FIFOs.
865*4882a593Smuzhiyun 	 */
866*4882a593Smuzhiyun 	sja1105_sgmii_write(priv, SJA1105_DC1, SJA1105_DC1_EN_VSMMD1 |
867*4882a593Smuzhiyun 					       SJA1105_DC1_CLOCK_STOP_EN |
868*4882a593Smuzhiyun 					       SJA1105_DC1_MAC_AUTO_SW |
869*4882a593Smuzhiyun 					       SJA1105_DC1_INIT);
870*4882a593Smuzhiyun 	/* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
871*4882a593Smuzhiyun 	sja1105_sgmii_write(priv, SJA1105_DC2, SJA1105_DC2_TX_POL_INV_DISABLE);
872*4882a593Smuzhiyun 	/* AUTONEG_CONTROL: Use SGMII autoneg */
873*4882a593Smuzhiyun 	if (an_master)
874*4882a593Smuzhiyun 		ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK;
875*4882a593Smuzhiyun 	sja1105_sgmii_write(priv, SJA1105_AC, ac);
876*4882a593Smuzhiyun 	/* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
877*4882a593Smuzhiyun 	 * sja1105_sgmii_pcs_force_speed must be called later for the link
878*4882a593Smuzhiyun 	 * to become operational.
879*4882a593Smuzhiyun 	 */
880*4882a593Smuzhiyun 	if (an_enabled)
881*4882a593Smuzhiyun 		sja1105_sgmii_write(priv, MII_BMCR,
882*4882a593Smuzhiyun 				    BMCR_ANENABLE | BMCR_ANRESTART);
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun 
sja1105_sgmii_pcs_force_speed(struct sja1105_private * priv,int speed)885*4882a593Smuzhiyun static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv,
886*4882a593Smuzhiyun 					  int speed)
887*4882a593Smuzhiyun {
888*4882a593Smuzhiyun 	int pcs_speed;
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	switch (speed) {
891*4882a593Smuzhiyun 	case SPEED_1000:
892*4882a593Smuzhiyun 		pcs_speed = BMCR_SPEED1000;
893*4882a593Smuzhiyun 		break;
894*4882a593Smuzhiyun 	case SPEED_100:
895*4882a593Smuzhiyun 		pcs_speed = BMCR_SPEED100;
896*4882a593Smuzhiyun 		break;
897*4882a593Smuzhiyun 	case SPEED_10:
898*4882a593Smuzhiyun 		pcs_speed = BMCR_SPEED10;
899*4882a593Smuzhiyun 		break;
900*4882a593Smuzhiyun 	default:
901*4882a593Smuzhiyun 		dev_err(priv->ds->dev, "Invalid speed %d\n", speed);
902*4882a593Smuzhiyun 		return;
903*4882a593Smuzhiyun 	}
904*4882a593Smuzhiyun 	sja1105_sgmii_write(priv, MII_BMCR, pcs_speed | BMCR_FULLDPLX);
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun /* Convert link speed from SJA1105 to ethtool encoding */
908*4882a593Smuzhiyun static int sja1105_speed[] = {
909*4882a593Smuzhiyun 	[SJA1105_SPEED_AUTO]		= SPEED_UNKNOWN,
910*4882a593Smuzhiyun 	[SJA1105_SPEED_10MBPS]		= SPEED_10,
911*4882a593Smuzhiyun 	[SJA1105_SPEED_100MBPS]		= SPEED_100,
912*4882a593Smuzhiyun 	[SJA1105_SPEED_1000MBPS]	= SPEED_1000,
913*4882a593Smuzhiyun };
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun /* Set link speed in the MAC configuration for a specific port. */
sja1105_adjust_port_config(struct sja1105_private * priv,int port,int speed_mbps)916*4882a593Smuzhiyun static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
917*4882a593Smuzhiyun 				      int speed_mbps)
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun 	struct sja1105_xmii_params_entry *mii;
920*4882a593Smuzhiyun 	struct sja1105_mac_config_entry *mac;
921*4882a593Smuzhiyun 	struct device *dev = priv->ds->dev;
922*4882a593Smuzhiyun 	sja1105_phy_interface_t phy_mode;
923*4882a593Smuzhiyun 	sja1105_speed_t speed;
924*4882a593Smuzhiyun 	int rc;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
927*4882a593Smuzhiyun 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
928*4882a593Smuzhiyun 	 * We have to *know* what the MAC looks like.  For the sake of keeping
929*4882a593Smuzhiyun 	 * the code common, we'll use the static configuration tables as a
930*4882a593Smuzhiyun 	 * reasonable approximation for both E/T and P/Q/R/S.
931*4882a593Smuzhiyun 	 */
932*4882a593Smuzhiyun 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
933*4882a593Smuzhiyun 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	switch (speed_mbps) {
936*4882a593Smuzhiyun 	case SPEED_UNKNOWN:
937*4882a593Smuzhiyun 		/* PHYLINK called sja1105_mac_config() to inform us about
938*4882a593Smuzhiyun 		 * the state->interface, but AN has not completed and the
939*4882a593Smuzhiyun 		 * speed is not yet valid. UM10944.pdf says that setting
940*4882a593Smuzhiyun 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
941*4882a593Smuzhiyun 		 * ok for power consumption in case AN will never complete -
942*4882a593Smuzhiyun 		 * otherwise PHYLINK should come back with a new update.
943*4882a593Smuzhiyun 		 */
944*4882a593Smuzhiyun 		speed = SJA1105_SPEED_AUTO;
945*4882a593Smuzhiyun 		break;
946*4882a593Smuzhiyun 	case SPEED_10:
947*4882a593Smuzhiyun 		speed = SJA1105_SPEED_10MBPS;
948*4882a593Smuzhiyun 		break;
949*4882a593Smuzhiyun 	case SPEED_100:
950*4882a593Smuzhiyun 		speed = SJA1105_SPEED_100MBPS;
951*4882a593Smuzhiyun 		break;
952*4882a593Smuzhiyun 	case SPEED_1000:
953*4882a593Smuzhiyun 		speed = SJA1105_SPEED_1000MBPS;
954*4882a593Smuzhiyun 		break;
955*4882a593Smuzhiyun 	default:
956*4882a593Smuzhiyun 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
957*4882a593Smuzhiyun 		return -EINVAL;
958*4882a593Smuzhiyun 	}
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
961*4882a593Smuzhiyun 	 * table, since this will be used for the clocking setup, and we no
962*4882a593Smuzhiyun 	 * longer need to store it in the static config (already told hardware
963*4882a593Smuzhiyun 	 * we want auto during upload phase).
964*4882a593Smuzhiyun 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
965*4882a593Smuzhiyun 	 * we need to configure the PCS only (if even that).
966*4882a593Smuzhiyun 	 */
967*4882a593Smuzhiyun 	if (sja1105_supports_sgmii(priv, port))
968*4882a593Smuzhiyun 		mac[port].speed = SJA1105_SPEED_1000MBPS;
969*4882a593Smuzhiyun 	else
970*4882a593Smuzhiyun 		mac[port].speed = speed;
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun 	/* Write to the dynamic reconfiguration tables */
973*4882a593Smuzhiyun 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
974*4882a593Smuzhiyun 					  &mac[port], true);
975*4882a593Smuzhiyun 	if (rc < 0) {
976*4882a593Smuzhiyun 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
977*4882a593Smuzhiyun 		return rc;
978*4882a593Smuzhiyun 	}
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
981*4882a593Smuzhiyun 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
982*4882a593Smuzhiyun 	 * RMII no change of the clock setup is required. Actually, changing
983*4882a593Smuzhiyun 	 * the clock setup does interrupt the clock signal for a certain time
984*4882a593Smuzhiyun 	 * which causes trouble for all PHYs relying on this signal.
985*4882a593Smuzhiyun 	 */
986*4882a593Smuzhiyun 	phy_mode = mii->xmii_mode[port];
987*4882a593Smuzhiyun 	if (phy_mode != XMII_MODE_RGMII)
988*4882a593Smuzhiyun 		return 0;
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun 	return sja1105_clocking_setup_port(priv, port);
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun /* The SJA1105 MAC programming model is through the static config (the xMII
994*4882a593Smuzhiyun  * Mode table cannot be dynamically reconfigured), and we have to program
995*4882a593Smuzhiyun  * that early (earlier than PHYLINK calls us, anyway).
996*4882a593Smuzhiyun  * So just error out in case the connected PHY attempts to change the initial
997*4882a593Smuzhiyun  * system interface MII protocol from what is defined in the DT, at least for
998*4882a593Smuzhiyun  * now.
999*4882a593Smuzhiyun  */
sja1105_phy_mode_mismatch(struct sja1105_private * priv,int port,phy_interface_t interface)1000*4882a593Smuzhiyun static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
1001*4882a593Smuzhiyun 				      phy_interface_t interface)
1002*4882a593Smuzhiyun {
1003*4882a593Smuzhiyun 	struct sja1105_xmii_params_entry *mii;
1004*4882a593Smuzhiyun 	sja1105_phy_interface_t phy_mode;
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1007*4882a593Smuzhiyun 	phy_mode = mii->xmii_mode[port];
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	switch (interface) {
1010*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_MII:
1011*4882a593Smuzhiyun 		return (phy_mode != XMII_MODE_MII);
1012*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RMII:
1013*4882a593Smuzhiyun 		return (phy_mode != XMII_MODE_RMII);
1014*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII:
1015*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_ID:
1016*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_RXID:
1017*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_RGMII_TXID:
1018*4882a593Smuzhiyun 		return (phy_mode != XMII_MODE_RGMII);
1019*4882a593Smuzhiyun 	case PHY_INTERFACE_MODE_SGMII:
1020*4882a593Smuzhiyun 		return (phy_mode != XMII_MODE_SGMII);
1021*4882a593Smuzhiyun 	default:
1022*4882a593Smuzhiyun 		return true;
1023*4882a593Smuzhiyun 	}
1024*4882a593Smuzhiyun }
1025*4882a593Smuzhiyun 
sja1105_mac_config(struct dsa_switch * ds,int port,unsigned int mode,const struct phylink_link_state * state)1026*4882a593Smuzhiyun static void sja1105_mac_config(struct dsa_switch *ds, int port,
1027*4882a593Smuzhiyun 			       unsigned int mode,
1028*4882a593Smuzhiyun 			       const struct phylink_link_state *state)
1029*4882a593Smuzhiyun {
1030*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1031*4882a593Smuzhiyun 	bool is_sgmii = sja1105_supports_sgmii(priv, port);
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1034*4882a593Smuzhiyun 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
1035*4882a593Smuzhiyun 			phy_modes(state->interface));
1036*4882a593Smuzhiyun 		return;
1037*4882a593Smuzhiyun 	}
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	if (phylink_autoneg_inband(mode) && !is_sgmii) {
1040*4882a593Smuzhiyun 		dev_err(ds->dev, "In-band AN not supported!\n");
1041*4882a593Smuzhiyun 		return;
1042*4882a593Smuzhiyun 	}
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 	if (is_sgmii)
1045*4882a593Smuzhiyun 		sja1105_sgmii_pcs_config(priv, phylink_autoneg_inband(mode),
1046*4882a593Smuzhiyun 					 false);
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun 
sja1105_mac_link_down(struct dsa_switch * ds,int port,unsigned int mode,phy_interface_t interface)1049*4882a593Smuzhiyun static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
1050*4882a593Smuzhiyun 				  unsigned int mode,
1051*4882a593Smuzhiyun 				  phy_interface_t interface)
1052*4882a593Smuzhiyun {
1053*4882a593Smuzhiyun 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
1054*4882a593Smuzhiyun }
1055*4882a593Smuzhiyun 
sja1105_mac_link_up(struct dsa_switch * ds,int port,unsigned int mode,phy_interface_t interface,struct phy_device * phydev,int speed,int duplex,bool tx_pause,bool rx_pause)1056*4882a593Smuzhiyun static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
1057*4882a593Smuzhiyun 				unsigned int mode,
1058*4882a593Smuzhiyun 				phy_interface_t interface,
1059*4882a593Smuzhiyun 				struct phy_device *phydev,
1060*4882a593Smuzhiyun 				int speed, int duplex,
1061*4882a593Smuzhiyun 				bool tx_pause, bool rx_pause)
1062*4882a593Smuzhiyun {
1063*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	sja1105_adjust_port_config(priv, port, speed);
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode))
1068*4882a593Smuzhiyun 		sja1105_sgmii_pcs_force_speed(priv, speed);
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	sja1105_inhibit_tx(priv, BIT(port), false);
1071*4882a593Smuzhiyun }
1072*4882a593Smuzhiyun 
sja1105_phylink_validate(struct dsa_switch * ds,int port,unsigned long * supported,struct phylink_link_state * state)1073*4882a593Smuzhiyun static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1074*4882a593Smuzhiyun 				     unsigned long *supported,
1075*4882a593Smuzhiyun 				     struct phylink_link_state *state)
1076*4882a593Smuzhiyun {
1077*4882a593Smuzhiyun 	/* Construct a new mask which exhaustively contains all link features
1078*4882a593Smuzhiyun 	 * supported by the MAC, and then apply that (logical AND) to what will
1079*4882a593Smuzhiyun 	 * be sent to the PHY for "marketing".
1080*4882a593Smuzhiyun 	 */
1081*4882a593Smuzhiyun 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1082*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1083*4882a593Smuzhiyun 	struct sja1105_xmii_params_entry *mii;
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	/* include/linux/phylink.h says:
1088*4882a593Smuzhiyun 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
1089*4882a593Smuzhiyun 	 *     expects the MAC driver to return all supported link modes.
1090*4882a593Smuzhiyun 	 */
1091*4882a593Smuzhiyun 	if (state->interface != PHY_INTERFACE_MODE_NA &&
1092*4882a593Smuzhiyun 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1093*4882a593Smuzhiyun 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
1094*4882a593Smuzhiyun 		return;
1095*4882a593Smuzhiyun 	}
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	/* The MAC does not support pause frames, and also doesn't
1098*4882a593Smuzhiyun 	 * support half-duplex traffic modes.
1099*4882a593Smuzhiyun 	 */
1100*4882a593Smuzhiyun 	phylink_set(mask, Autoneg);
1101*4882a593Smuzhiyun 	phylink_set(mask, MII);
1102*4882a593Smuzhiyun 	phylink_set(mask, 10baseT_Full);
1103*4882a593Smuzhiyun 	phylink_set(mask, 100baseT_Full);
1104*4882a593Smuzhiyun 	phylink_set(mask, 100baseT1_Full);
1105*4882a593Smuzhiyun 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1106*4882a593Smuzhiyun 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1107*4882a593Smuzhiyun 		phylink_set(mask, 1000baseT_Full);
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1110*4882a593Smuzhiyun 	bitmap_and(state->advertising, state->advertising, mask,
1111*4882a593Smuzhiyun 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1112*4882a593Smuzhiyun }
1113*4882a593Smuzhiyun 
sja1105_mac_pcs_get_state(struct dsa_switch * ds,int port,struct phylink_link_state * state)1114*4882a593Smuzhiyun static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port,
1115*4882a593Smuzhiyun 				     struct phylink_link_state *state)
1116*4882a593Smuzhiyun {
1117*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1118*4882a593Smuzhiyun 	int ais;
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	/* Read the vendor-specific AUTONEG_INTR_STATUS register */
1121*4882a593Smuzhiyun 	ais = sja1105_sgmii_read(priv, SJA1105_AIS);
1122*4882a593Smuzhiyun 	if (ais < 0)
1123*4882a593Smuzhiyun 		return ais;
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	switch (SJA1105_AIS_SPEED(ais)) {
1126*4882a593Smuzhiyun 	case 0:
1127*4882a593Smuzhiyun 		state->speed = SPEED_10;
1128*4882a593Smuzhiyun 		break;
1129*4882a593Smuzhiyun 	case 1:
1130*4882a593Smuzhiyun 		state->speed = SPEED_100;
1131*4882a593Smuzhiyun 		break;
1132*4882a593Smuzhiyun 	case 2:
1133*4882a593Smuzhiyun 		state->speed = SPEED_1000;
1134*4882a593Smuzhiyun 		break;
1135*4882a593Smuzhiyun 	default:
1136*4882a593Smuzhiyun 		dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n",
1137*4882a593Smuzhiyun 			SJA1105_AIS_SPEED(ais));
1138*4882a593Smuzhiyun 	}
1139*4882a593Smuzhiyun 	state->duplex = SJA1105_AIS_DUPLEX_MODE(ais);
1140*4882a593Smuzhiyun 	state->an_complete = SJA1105_AIS_COMPLETE(ais);
1141*4882a593Smuzhiyun 	state->link = SJA1105_AIS_LINK_STATUS(ais);
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	return 0;
1144*4882a593Smuzhiyun }
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun static int
sja1105_find_static_fdb_entry(struct sja1105_private * priv,int port,const struct sja1105_l2_lookup_entry * requested)1147*4882a593Smuzhiyun sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
1148*4882a593Smuzhiyun 			      const struct sja1105_l2_lookup_entry *requested)
1149*4882a593Smuzhiyun {
1150*4882a593Smuzhiyun 	struct sja1105_l2_lookup_entry *l2_lookup;
1151*4882a593Smuzhiyun 	struct sja1105_table *table;
1152*4882a593Smuzhiyun 	int i;
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1155*4882a593Smuzhiyun 	l2_lookup = table->entries;
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	for (i = 0; i < table->entry_count; i++)
1158*4882a593Smuzhiyun 		if (l2_lookup[i].macaddr == requested->macaddr &&
1159*4882a593Smuzhiyun 		    l2_lookup[i].vlanid == requested->vlanid &&
1160*4882a593Smuzhiyun 		    l2_lookup[i].destports & BIT(port))
1161*4882a593Smuzhiyun 			return i;
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	return -1;
1164*4882a593Smuzhiyun }
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun /* We want FDB entries added statically through the bridge command to persist
1167*4882a593Smuzhiyun  * across switch resets, which are a common thing during normal SJA1105
1168*4882a593Smuzhiyun  * operation. So we have to back them up in the static configuration tables
1169*4882a593Smuzhiyun  * and hence apply them on next static config upload... yay!
1170*4882a593Smuzhiyun  */
1171*4882a593Smuzhiyun static int
sja1105_static_fdb_change(struct sja1105_private * priv,int port,const struct sja1105_l2_lookup_entry * requested,bool keep)1172*4882a593Smuzhiyun sja1105_static_fdb_change(struct sja1105_private *priv, int port,
1173*4882a593Smuzhiyun 			  const struct sja1105_l2_lookup_entry *requested,
1174*4882a593Smuzhiyun 			  bool keep)
1175*4882a593Smuzhiyun {
1176*4882a593Smuzhiyun 	struct sja1105_l2_lookup_entry *l2_lookup;
1177*4882a593Smuzhiyun 	struct sja1105_table *table;
1178*4882a593Smuzhiyun 	int rc, match;
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun 	match = sja1105_find_static_fdb_entry(priv, port, requested);
1183*4882a593Smuzhiyun 	if (match < 0) {
1184*4882a593Smuzhiyun 		/* Can't delete a missing entry. */
1185*4882a593Smuzhiyun 		if (!keep)
1186*4882a593Smuzhiyun 			return 0;
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 		/* No match => new entry */
1189*4882a593Smuzhiyun 		rc = sja1105_table_resize(table, table->entry_count + 1);
1190*4882a593Smuzhiyun 		if (rc)
1191*4882a593Smuzhiyun 			return rc;
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun 		match = table->entry_count - 1;
1194*4882a593Smuzhiyun 	}
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 	/* Assign pointer after the resize (it may be new memory) */
1197*4882a593Smuzhiyun 	l2_lookup = table->entries;
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	/* We have a match.
1200*4882a593Smuzhiyun 	 * If the job was to add this FDB entry, it's already done (mostly
1201*4882a593Smuzhiyun 	 * anyway, since the port forwarding mask may have changed, case in
1202*4882a593Smuzhiyun 	 * which we update it).
1203*4882a593Smuzhiyun 	 * Otherwise we have to delete it.
1204*4882a593Smuzhiyun 	 */
1205*4882a593Smuzhiyun 	if (keep) {
1206*4882a593Smuzhiyun 		l2_lookup[match] = *requested;
1207*4882a593Smuzhiyun 		return 0;
1208*4882a593Smuzhiyun 	}
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	/* To remove, the strategy is to overwrite the element with
1211*4882a593Smuzhiyun 	 * the last one, and then reduce the array size by 1
1212*4882a593Smuzhiyun 	 */
1213*4882a593Smuzhiyun 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
1214*4882a593Smuzhiyun 	return sja1105_table_resize(table, table->entry_count - 1);
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun /* First-generation switches have a 4-way set associative TCAM that
1218*4882a593Smuzhiyun  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1219*4882a593Smuzhiyun  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1220*4882a593Smuzhiyun  * For the placement of a newly learnt FDB entry, the switch selects the bin
1221*4882a593Smuzhiyun  * based on a hash function, and the way within that bin incrementally.
1222*4882a593Smuzhiyun  */
sja1105et_fdb_index(int bin,int way)1223*4882a593Smuzhiyun static int sja1105et_fdb_index(int bin, int way)
1224*4882a593Smuzhiyun {
1225*4882a593Smuzhiyun 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1226*4882a593Smuzhiyun }
1227*4882a593Smuzhiyun 
sja1105et_is_fdb_entry_in_bin(struct sja1105_private * priv,int bin,const u8 * addr,u16 vid,struct sja1105_l2_lookup_entry * match,int * last_unused)1228*4882a593Smuzhiyun static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1229*4882a593Smuzhiyun 					 const u8 *addr, u16 vid,
1230*4882a593Smuzhiyun 					 struct sja1105_l2_lookup_entry *match,
1231*4882a593Smuzhiyun 					 int *last_unused)
1232*4882a593Smuzhiyun {
1233*4882a593Smuzhiyun 	int way;
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1236*4882a593Smuzhiyun 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1237*4882a593Smuzhiyun 		int index = sja1105et_fdb_index(bin, way);
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 		/* Skip unused entries, optionally marking them
1240*4882a593Smuzhiyun 		 * into the return value
1241*4882a593Smuzhiyun 		 */
1242*4882a593Smuzhiyun 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1243*4882a593Smuzhiyun 						index, &l2_lookup)) {
1244*4882a593Smuzhiyun 			if (last_unused)
1245*4882a593Smuzhiyun 				*last_unused = way;
1246*4882a593Smuzhiyun 			continue;
1247*4882a593Smuzhiyun 		}
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1250*4882a593Smuzhiyun 		    l2_lookup.vlanid == vid) {
1251*4882a593Smuzhiyun 			if (match)
1252*4882a593Smuzhiyun 				*match = l2_lookup;
1253*4882a593Smuzhiyun 			return way;
1254*4882a593Smuzhiyun 		}
1255*4882a593Smuzhiyun 	}
1256*4882a593Smuzhiyun 	/* Return an invalid entry index if not found */
1257*4882a593Smuzhiyun 	return -1;
1258*4882a593Smuzhiyun }
1259*4882a593Smuzhiyun 
sja1105et_fdb_add(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)1260*4882a593Smuzhiyun int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1261*4882a593Smuzhiyun 		      const unsigned char *addr, u16 vid)
1262*4882a593Smuzhiyun {
1263*4882a593Smuzhiyun 	struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp;
1264*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1265*4882a593Smuzhiyun 	struct device *dev = ds->dev;
1266*4882a593Smuzhiyun 	int last_unused = -1;
1267*4882a593Smuzhiyun 	int start, end, i;
1268*4882a593Smuzhiyun 	int bin, way, rc;
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	bin = sja1105et_fdb_hash(priv, addr, vid);
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1273*4882a593Smuzhiyun 					    &l2_lookup, &last_unused);
1274*4882a593Smuzhiyun 	if (way >= 0) {
1275*4882a593Smuzhiyun 		/* We have an FDB entry. Is our port in the destination
1276*4882a593Smuzhiyun 		 * mask? If yes, we need to do nothing. If not, we need
1277*4882a593Smuzhiyun 		 * to rewrite the entry by adding this port to it.
1278*4882a593Smuzhiyun 		 */
1279*4882a593Smuzhiyun 		if ((l2_lookup.destports & BIT(port)) && l2_lookup.lockeds)
1280*4882a593Smuzhiyun 			return 0;
1281*4882a593Smuzhiyun 		l2_lookup.destports |= BIT(port);
1282*4882a593Smuzhiyun 	} else {
1283*4882a593Smuzhiyun 		int index = sja1105et_fdb_index(bin, way);
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 		/* We don't have an FDB entry. We construct a new one and
1286*4882a593Smuzhiyun 		 * try to find a place for it within the FDB table.
1287*4882a593Smuzhiyun 		 */
1288*4882a593Smuzhiyun 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1289*4882a593Smuzhiyun 		l2_lookup.destports = BIT(port);
1290*4882a593Smuzhiyun 		l2_lookup.vlanid = vid;
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 		if (last_unused >= 0) {
1293*4882a593Smuzhiyun 			way = last_unused;
1294*4882a593Smuzhiyun 		} else {
1295*4882a593Smuzhiyun 			/* Bin is full, need to evict somebody.
1296*4882a593Smuzhiyun 			 * Choose victim at random. If you get these messages
1297*4882a593Smuzhiyun 			 * often, you may need to consider changing the
1298*4882a593Smuzhiyun 			 * distribution function:
1299*4882a593Smuzhiyun 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1300*4882a593Smuzhiyun 			 */
1301*4882a593Smuzhiyun 			get_random_bytes(&way, sizeof(u8));
1302*4882a593Smuzhiyun 			way %= SJA1105ET_FDB_BIN_SIZE;
1303*4882a593Smuzhiyun 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1304*4882a593Smuzhiyun 				 bin, addr, way);
1305*4882a593Smuzhiyun 			/* Evict entry */
1306*4882a593Smuzhiyun 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1307*4882a593Smuzhiyun 						     index, NULL, false);
1308*4882a593Smuzhiyun 		}
1309*4882a593Smuzhiyun 	}
1310*4882a593Smuzhiyun 	l2_lookup.lockeds = true;
1311*4882a593Smuzhiyun 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1314*4882a593Smuzhiyun 					  l2_lookup.index, &l2_lookup,
1315*4882a593Smuzhiyun 					  true);
1316*4882a593Smuzhiyun 	if (rc < 0)
1317*4882a593Smuzhiyun 		return rc;
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	/* Invalidate a dynamically learned entry if that exists */
1320*4882a593Smuzhiyun 	start = sja1105et_fdb_index(bin, 0);
1321*4882a593Smuzhiyun 	end = sja1105et_fdb_index(bin, way);
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	for (i = start; i < end; i++) {
1324*4882a593Smuzhiyun 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1325*4882a593Smuzhiyun 						 i, &tmp);
1326*4882a593Smuzhiyun 		if (rc == -ENOENT)
1327*4882a593Smuzhiyun 			continue;
1328*4882a593Smuzhiyun 		if (rc)
1329*4882a593Smuzhiyun 			return rc;
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 		if (tmp.macaddr != ether_addr_to_u64(addr) || tmp.vlanid != vid)
1332*4882a593Smuzhiyun 			continue;
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1335*4882a593Smuzhiyun 						  i, NULL, false);
1336*4882a593Smuzhiyun 		if (rc)
1337*4882a593Smuzhiyun 			return rc;
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 		break;
1340*4882a593Smuzhiyun 	}
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1343*4882a593Smuzhiyun }
1344*4882a593Smuzhiyun 
sja1105et_fdb_del(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)1345*4882a593Smuzhiyun int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1346*4882a593Smuzhiyun 		      const unsigned char *addr, u16 vid)
1347*4882a593Smuzhiyun {
1348*4882a593Smuzhiyun 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1349*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1350*4882a593Smuzhiyun 	int index, bin, way, rc;
1351*4882a593Smuzhiyun 	bool keep;
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 	bin = sja1105et_fdb_hash(priv, addr, vid);
1354*4882a593Smuzhiyun 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1355*4882a593Smuzhiyun 					    &l2_lookup, NULL);
1356*4882a593Smuzhiyun 	if (way < 0)
1357*4882a593Smuzhiyun 		return 0;
1358*4882a593Smuzhiyun 	index = sja1105et_fdb_index(bin, way);
1359*4882a593Smuzhiyun 
1360*4882a593Smuzhiyun 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1361*4882a593Smuzhiyun 	 * we need to remove it. If the resulting port mask becomes empty, we
1362*4882a593Smuzhiyun 	 * need to completely evict the FDB entry.
1363*4882a593Smuzhiyun 	 * Otherwise we just write it back.
1364*4882a593Smuzhiyun 	 */
1365*4882a593Smuzhiyun 	l2_lookup.destports &= ~BIT(port);
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	if (l2_lookup.destports)
1368*4882a593Smuzhiyun 		keep = true;
1369*4882a593Smuzhiyun 	else
1370*4882a593Smuzhiyun 		keep = false;
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1373*4882a593Smuzhiyun 					  index, &l2_lookup, keep);
1374*4882a593Smuzhiyun 	if (rc < 0)
1375*4882a593Smuzhiyun 		return rc;
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1378*4882a593Smuzhiyun }
1379*4882a593Smuzhiyun 
sja1105pqrs_fdb_add(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)1380*4882a593Smuzhiyun int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
1381*4882a593Smuzhiyun 			const unsigned char *addr, u16 vid)
1382*4882a593Smuzhiyun {
1383*4882a593Smuzhiyun 	struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp;
1384*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1385*4882a593Smuzhiyun 	int rc, i;
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 	/* Search for an existing entry in the FDB table */
1388*4882a593Smuzhiyun 	l2_lookup.macaddr = ether_addr_to_u64(addr);
1389*4882a593Smuzhiyun 	l2_lookup.vlanid = vid;
1390*4882a593Smuzhiyun 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
1391*4882a593Smuzhiyun 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
1392*4882a593Smuzhiyun 	l2_lookup.destports = BIT(port);
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1395*4882a593Smuzhiyun 					 SJA1105_SEARCH, &l2_lookup);
1396*4882a593Smuzhiyun 	if (rc == 0) {
1397*4882a593Smuzhiyun 		/* Found a static entry and this port is already in the entry's
1398*4882a593Smuzhiyun 		 * port mask => job done
1399*4882a593Smuzhiyun 		 */
1400*4882a593Smuzhiyun 		if ((l2_lookup.destports & BIT(port)) && l2_lookup.lockeds)
1401*4882a593Smuzhiyun 			return 0;
1402*4882a593Smuzhiyun 		/* l2_lookup.index is populated by the switch in case it
1403*4882a593Smuzhiyun 		 * found something.
1404*4882a593Smuzhiyun 		 */
1405*4882a593Smuzhiyun 		l2_lookup.destports |= BIT(port);
1406*4882a593Smuzhiyun 		goto skip_finding_an_index;
1407*4882a593Smuzhiyun 	}
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 	/* Not found, so try to find an unused spot in the FDB.
1410*4882a593Smuzhiyun 	 * This is slightly inefficient because the strategy is knock-knock at
1411*4882a593Smuzhiyun 	 * every possible position from 0 to 1023.
1412*4882a593Smuzhiyun 	 */
1413*4882a593Smuzhiyun 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1414*4882a593Smuzhiyun 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1415*4882a593Smuzhiyun 						 i, NULL);
1416*4882a593Smuzhiyun 		if (rc < 0)
1417*4882a593Smuzhiyun 			break;
1418*4882a593Smuzhiyun 	}
1419*4882a593Smuzhiyun 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
1420*4882a593Smuzhiyun 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
1421*4882a593Smuzhiyun 		return -EINVAL;
1422*4882a593Smuzhiyun 	}
1423*4882a593Smuzhiyun 	l2_lookup.index = i;
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun skip_finding_an_index:
1426*4882a593Smuzhiyun 	l2_lookup.lockeds = true;
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1429*4882a593Smuzhiyun 					  l2_lookup.index, &l2_lookup,
1430*4882a593Smuzhiyun 					  true);
1431*4882a593Smuzhiyun 	if (rc < 0)
1432*4882a593Smuzhiyun 		return rc;
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	/* The switch learns dynamic entries and looks up the FDB left to
1435*4882a593Smuzhiyun 	 * right. It is possible that our addition was concurrent with the
1436*4882a593Smuzhiyun 	 * dynamic learning of the same address, so now that the static entry
1437*4882a593Smuzhiyun 	 * has been installed, we are certain that address learning for this
1438*4882a593Smuzhiyun 	 * particular address has been turned off, so the dynamic entry either
1439*4882a593Smuzhiyun 	 * is in the FDB at an index smaller than the static one, or isn't (it
1440*4882a593Smuzhiyun 	 * can also be at a larger index, but in that case it is inactive
1441*4882a593Smuzhiyun 	 * because the static FDB entry will match first, and the dynamic one
1442*4882a593Smuzhiyun 	 * will eventually age out). Search for a dynamically learned address
1443*4882a593Smuzhiyun 	 * prior to our static one and invalidate it.
1444*4882a593Smuzhiyun 	 */
1445*4882a593Smuzhiyun 	tmp = l2_lookup;
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1448*4882a593Smuzhiyun 					 SJA1105_SEARCH, &tmp);
1449*4882a593Smuzhiyun 	if (rc < 0) {
1450*4882a593Smuzhiyun 		dev_err(ds->dev,
1451*4882a593Smuzhiyun 			"port %d failed to read back entry for %pM vid %d: %pe\n",
1452*4882a593Smuzhiyun 			port, addr, vid, ERR_PTR(rc));
1453*4882a593Smuzhiyun 		return rc;
1454*4882a593Smuzhiyun 	}
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun 	if (tmp.index < l2_lookup.index) {
1457*4882a593Smuzhiyun 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1458*4882a593Smuzhiyun 						  tmp.index, NULL, false);
1459*4882a593Smuzhiyun 		if (rc < 0)
1460*4882a593Smuzhiyun 			return rc;
1461*4882a593Smuzhiyun 	}
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1464*4882a593Smuzhiyun }
1465*4882a593Smuzhiyun 
sja1105pqrs_fdb_del(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)1466*4882a593Smuzhiyun int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
1467*4882a593Smuzhiyun 			const unsigned char *addr, u16 vid)
1468*4882a593Smuzhiyun {
1469*4882a593Smuzhiyun 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1470*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1471*4882a593Smuzhiyun 	bool keep;
1472*4882a593Smuzhiyun 	int rc;
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun 	l2_lookup.macaddr = ether_addr_to_u64(addr);
1475*4882a593Smuzhiyun 	l2_lookup.vlanid = vid;
1476*4882a593Smuzhiyun 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
1477*4882a593Smuzhiyun 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
1478*4882a593Smuzhiyun 	l2_lookup.destports = BIT(port);
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1481*4882a593Smuzhiyun 					 SJA1105_SEARCH, &l2_lookup);
1482*4882a593Smuzhiyun 	if (rc < 0)
1483*4882a593Smuzhiyun 		return 0;
1484*4882a593Smuzhiyun 
1485*4882a593Smuzhiyun 	l2_lookup.destports &= ~BIT(port);
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun 	/* Decide whether we remove just this port from the FDB entry,
1488*4882a593Smuzhiyun 	 * or if we remove it completely.
1489*4882a593Smuzhiyun 	 */
1490*4882a593Smuzhiyun 	if (l2_lookup.destports)
1491*4882a593Smuzhiyun 		keep = true;
1492*4882a593Smuzhiyun 	else
1493*4882a593Smuzhiyun 		keep = false;
1494*4882a593Smuzhiyun 
1495*4882a593Smuzhiyun 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1496*4882a593Smuzhiyun 					  l2_lookup.index, &l2_lookup, keep);
1497*4882a593Smuzhiyun 	if (rc < 0)
1498*4882a593Smuzhiyun 		return rc;
1499*4882a593Smuzhiyun 
1500*4882a593Smuzhiyun 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1501*4882a593Smuzhiyun }
1502*4882a593Smuzhiyun 
sja1105_fdb_add(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)1503*4882a593Smuzhiyun static int sja1105_fdb_add(struct dsa_switch *ds, int port,
1504*4882a593Smuzhiyun 			   const unsigned char *addr, u16 vid)
1505*4882a593Smuzhiyun {
1506*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun 	/* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
1509*4882a593Smuzhiyun 	 * so the switch still does some VLAN processing internally.
1510*4882a593Smuzhiyun 	 * But Shared VLAN Learning (SVL) is also active, and it will take
1511*4882a593Smuzhiyun 	 * care of autonomous forwarding between the unique pvid's of each
1512*4882a593Smuzhiyun 	 * port.  Here we just make sure that users can't add duplicate FDB
1513*4882a593Smuzhiyun 	 * entries when in this mode - the actual VID doesn't matter except
1514*4882a593Smuzhiyun 	 * for what gets printed in 'bridge fdb show'.  In the case of zero,
1515*4882a593Smuzhiyun 	 * no VID gets printed at all.
1516*4882a593Smuzhiyun 	 */
1517*4882a593Smuzhiyun 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
1518*4882a593Smuzhiyun 		vid = 0;
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
1521*4882a593Smuzhiyun }
1522*4882a593Smuzhiyun 
sja1105_fdb_del(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid)1523*4882a593Smuzhiyun static int sja1105_fdb_del(struct dsa_switch *ds, int port,
1524*4882a593Smuzhiyun 			   const unsigned char *addr, u16 vid)
1525*4882a593Smuzhiyun {
1526*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 	if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
1529*4882a593Smuzhiyun 		vid = 0;
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
1532*4882a593Smuzhiyun }
1533*4882a593Smuzhiyun 
sja1105_fdb_dump(struct dsa_switch * ds,int port,dsa_fdb_dump_cb_t * cb,void * data)1534*4882a593Smuzhiyun static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1535*4882a593Smuzhiyun 			    dsa_fdb_dump_cb_t *cb, void *data)
1536*4882a593Smuzhiyun {
1537*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1538*4882a593Smuzhiyun 	struct device *dev = ds->dev;
1539*4882a593Smuzhiyun 	int i;
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1542*4882a593Smuzhiyun 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1543*4882a593Smuzhiyun 		u8 macaddr[ETH_ALEN];
1544*4882a593Smuzhiyun 		int rc;
1545*4882a593Smuzhiyun 
1546*4882a593Smuzhiyun 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1547*4882a593Smuzhiyun 						 i, &l2_lookup);
1548*4882a593Smuzhiyun 		/* No fdb entry at i, not an issue */
1549*4882a593Smuzhiyun 		if (rc == -ENOENT)
1550*4882a593Smuzhiyun 			continue;
1551*4882a593Smuzhiyun 		if (rc) {
1552*4882a593Smuzhiyun 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1553*4882a593Smuzhiyun 			return rc;
1554*4882a593Smuzhiyun 		}
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun 		/* FDB dump callback is per port. This means we have to
1557*4882a593Smuzhiyun 		 * disregard a valid entry if it's not for this port, even if
1558*4882a593Smuzhiyun 		 * only to revisit it later. This is inefficient because the
1559*4882a593Smuzhiyun 		 * 1024-sized FDB table needs to be traversed 4 times through
1560*4882a593Smuzhiyun 		 * SPI during a 'bridge fdb show' command.
1561*4882a593Smuzhiyun 		 */
1562*4882a593Smuzhiyun 		if (!(l2_lookup.destports & BIT(port)))
1563*4882a593Smuzhiyun 			continue;
1564*4882a593Smuzhiyun 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
1565*4882a593Smuzhiyun 
1566*4882a593Smuzhiyun 		/* We need to hide the dsa_8021q VLANs from the user. */
1567*4882a593Smuzhiyun 		if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
1568*4882a593Smuzhiyun 			l2_lookup.vlanid = 0;
1569*4882a593Smuzhiyun 		rc = cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1570*4882a593Smuzhiyun 		if (rc)
1571*4882a593Smuzhiyun 			return rc;
1572*4882a593Smuzhiyun 	}
1573*4882a593Smuzhiyun 	return 0;
1574*4882a593Smuzhiyun }
1575*4882a593Smuzhiyun 
1576*4882a593Smuzhiyun /* This callback needs to be present */
sja1105_mdb_prepare(struct dsa_switch * ds,int port,const struct switchdev_obj_port_mdb * mdb)1577*4882a593Smuzhiyun static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1578*4882a593Smuzhiyun 			       const struct switchdev_obj_port_mdb *mdb)
1579*4882a593Smuzhiyun {
1580*4882a593Smuzhiyun 	return 0;
1581*4882a593Smuzhiyun }
1582*4882a593Smuzhiyun 
sja1105_mdb_add(struct dsa_switch * ds,int port,const struct switchdev_obj_port_mdb * mdb)1583*4882a593Smuzhiyun static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1584*4882a593Smuzhiyun 			    const struct switchdev_obj_port_mdb *mdb)
1585*4882a593Smuzhiyun {
1586*4882a593Smuzhiyun 	sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1587*4882a593Smuzhiyun }
1588*4882a593Smuzhiyun 
sja1105_mdb_del(struct dsa_switch * ds,int port,const struct switchdev_obj_port_mdb * mdb)1589*4882a593Smuzhiyun static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1590*4882a593Smuzhiyun 			   const struct switchdev_obj_port_mdb *mdb)
1591*4882a593Smuzhiyun {
1592*4882a593Smuzhiyun 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1593*4882a593Smuzhiyun }
1594*4882a593Smuzhiyun 
sja1105_bridge_member(struct dsa_switch * ds,int port,struct net_device * br,bool member)1595*4882a593Smuzhiyun static int sja1105_bridge_member(struct dsa_switch *ds, int port,
1596*4882a593Smuzhiyun 				 struct net_device *br, bool member)
1597*4882a593Smuzhiyun {
1598*4882a593Smuzhiyun 	struct sja1105_l2_forwarding_entry *l2_fwd;
1599*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1600*4882a593Smuzhiyun 	int i, rc;
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1605*4882a593Smuzhiyun 		/* Add this port to the forwarding matrix of the
1606*4882a593Smuzhiyun 		 * other ports in the same bridge, and viceversa.
1607*4882a593Smuzhiyun 		 */
1608*4882a593Smuzhiyun 		if (!dsa_is_user_port(ds, i))
1609*4882a593Smuzhiyun 			continue;
1610*4882a593Smuzhiyun 		/* For the ports already under the bridge, only one thing needs
1611*4882a593Smuzhiyun 		 * to be done, and that is to add this port to their
1612*4882a593Smuzhiyun 		 * reachability domain. So we can perform the SPI write for
1613*4882a593Smuzhiyun 		 * them immediately. However, for this port itself (the one
1614*4882a593Smuzhiyun 		 * that is new to the bridge), we need to add all other ports
1615*4882a593Smuzhiyun 		 * to its reachability domain. So we do that incrementally in
1616*4882a593Smuzhiyun 		 * this loop, and perform the SPI write only at the end, once
1617*4882a593Smuzhiyun 		 * the domain contains all other bridge ports.
1618*4882a593Smuzhiyun 		 */
1619*4882a593Smuzhiyun 		if (i == port)
1620*4882a593Smuzhiyun 			continue;
1621*4882a593Smuzhiyun 		if (dsa_to_port(ds, i)->bridge_dev != br)
1622*4882a593Smuzhiyun 			continue;
1623*4882a593Smuzhiyun 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
1624*4882a593Smuzhiyun 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
1627*4882a593Smuzhiyun 						  i, &l2_fwd[i], true);
1628*4882a593Smuzhiyun 		if (rc < 0)
1629*4882a593Smuzhiyun 			return rc;
1630*4882a593Smuzhiyun 	}
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
1633*4882a593Smuzhiyun 					    port, &l2_fwd[port], true);
1634*4882a593Smuzhiyun }
1635*4882a593Smuzhiyun 
sja1105_bridge_stp_state_set(struct dsa_switch * ds,int port,u8 state)1636*4882a593Smuzhiyun static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1637*4882a593Smuzhiyun 					 u8 state)
1638*4882a593Smuzhiyun {
1639*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1640*4882a593Smuzhiyun 	struct sja1105_mac_config_entry *mac;
1641*4882a593Smuzhiyun 
1642*4882a593Smuzhiyun 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1643*4882a593Smuzhiyun 
1644*4882a593Smuzhiyun 	switch (state) {
1645*4882a593Smuzhiyun 	case BR_STATE_DISABLED:
1646*4882a593Smuzhiyun 	case BR_STATE_BLOCKING:
1647*4882a593Smuzhiyun 		/* From UM10944 description of DRPDTAG (why put this there?):
1648*4882a593Smuzhiyun 		 * "Management traffic flows to the port regardless of the state
1649*4882a593Smuzhiyun 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1650*4882a593Smuzhiyun 		 * At the moment no difference between DISABLED and BLOCKING.
1651*4882a593Smuzhiyun 		 */
1652*4882a593Smuzhiyun 		mac[port].ingress   = false;
1653*4882a593Smuzhiyun 		mac[port].egress    = false;
1654*4882a593Smuzhiyun 		mac[port].dyn_learn = false;
1655*4882a593Smuzhiyun 		break;
1656*4882a593Smuzhiyun 	case BR_STATE_LISTENING:
1657*4882a593Smuzhiyun 		mac[port].ingress   = true;
1658*4882a593Smuzhiyun 		mac[port].egress    = false;
1659*4882a593Smuzhiyun 		mac[port].dyn_learn = false;
1660*4882a593Smuzhiyun 		break;
1661*4882a593Smuzhiyun 	case BR_STATE_LEARNING:
1662*4882a593Smuzhiyun 		mac[port].ingress   = true;
1663*4882a593Smuzhiyun 		mac[port].egress    = false;
1664*4882a593Smuzhiyun 		mac[port].dyn_learn = true;
1665*4882a593Smuzhiyun 		break;
1666*4882a593Smuzhiyun 	case BR_STATE_FORWARDING:
1667*4882a593Smuzhiyun 		mac[port].ingress   = true;
1668*4882a593Smuzhiyun 		mac[port].egress    = true;
1669*4882a593Smuzhiyun 		mac[port].dyn_learn = true;
1670*4882a593Smuzhiyun 		break;
1671*4882a593Smuzhiyun 	default:
1672*4882a593Smuzhiyun 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1673*4882a593Smuzhiyun 		return;
1674*4882a593Smuzhiyun 	}
1675*4882a593Smuzhiyun 
1676*4882a593Smuzhiyun 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1677*4882a593Smuzhiyun 				     &mac[port], true);
1678*4882a593Smuzhiyun }
1679*4882a593Smuzhiyun 
sja1105_bridge_join(struct dsa_switch * ds,int port,struct net_device * br)1680*4882a593Smuzhiyun static int sja1105_bridge_join(struct dsa_switch *ds, int port,
1681*4882a593Smuzhiyun 			       struct net_device *br)
1682*4882a593Smuzhiyun {
1683*4882a593Smuzhiyun 	return sja1105_bridge_member(ds, port, br, true);
1684*4882a593Smuzhiyun }
1685*4882a593Smuzhiyun 
sja1105_bridge_leave(struct dsa_switch * ds,int port,struct net_device * br)1686*4882a593Smuzhiyun static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
1687*4882a593Smuzhiyun 				 struct net_device *br)
1688*4882a593Smuzhiyun {
1689*4882a593Smuzhiyun 	sja1105_bridge_member(ds, port, br, false);
1690*4882a593Smuzhiyun }
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun #define BYTES_PER_KBIT (1000LL / 8)
1693*4882a593Smuzhiyun 
sja1105_find_unused_cbs_shaper(struct sja1105_private * priv)1694*4882a593Smuzhiyun static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
1695*4882a593Smuzhiyun {
1696*4882a593Smuzhiyun 	int i;
1697*4882a593Smuzhiyun 
1698*4882a593Smuzhiyun 	for (i = 0; i < priv->info->num_cbs_shapers; i++)
1699*4882a593Smuzhiyun 		if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
1700*4882a593Smuzhiyun 			return i;
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun 	return -1;
1703*4882a593Smuzhiyun }
1704*4882a593Smuzhiyun 
sja1105_delete_cbs_shaper(struct sja1105_private * priv,int port,int prio)1705*4882a593Smuzhiyun static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
1706*4882a593Smuzhiyun 				     int prio)
1707*4882a593Smuzhiyun {
1708*4882a593Smuzhiyun 	int i;
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
1711*4882a593Smuzhiyun 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
1712*4882a593Smuzhiyun 
1713*4882a593Smuzhiyun 		if (cbs->port == port && cbs->prio == prio) {
1714*4882a593Smuzhiyun 			memset(cbs, 0, sizeof(*cbs));
1715*4882a593Smuzhiyun 			return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
1716*4882a593Smuzhiyun 							    i, cbs, true);
1717*4882a593Smuzhiyun 		}
1718*4882a593Smuzhiyun 	}
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun 	return 0;
1721*4882a593Smuzhiyun }
1722*4882a593Smuzhiyun 
sja1105_setup_tc_cbs(struct dsa_switch * ds,int port,struct tc_cbs_qopt_offload * offload)1723*4882a593Smuzhiyun static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
1724*4882a593Smuzhiyun 				struct tc_cbs_qopt_offload *offload)
1725*4882a593Smuzhiyun {
1726*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1727*4882a593Smuzhiyun 	struct sja1105_cbs_entry *cbs;
1728*4882a593Smuzhiyun 	int index;
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun 	if (!offload->enable)
1731*4882a593Smuzhiyun 		return sja1105_delete_cbs_shaper(priv, port, offload->queue);
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	index = sja1105_find_unused_cbs_shaper(priv);
1734*4882a593Smuzhiyun 	if (index < 0)
1735*4882a593Smuzhiyun 		return -ENOSPC;
1736*4882a593Smuzhiyun 
1737*4882a593Smuzhiyun 	cbs = &priv->cbs[index];
1738*4882a593Smuzhiyun 	cbs->port = port;
1739*4882a593Smuzhiyun 	cbs->prio = offload->queue;
1740*4882a593Smuzhiyun 	/* locredit and sendslope are negative by definition. In hardware,
1741*4882a593Smuzhiyun 	 * positive values must be provided, and the negative sign is implicit.
1742*4882a593Smuzhiyun 	 */
1743*4882a593Smuzhiyun 	cbs->credit_hi = offload->hicredit;
1744*4882a593Smuzhiyun 	cbs->credit_lo = abs(offload->locredit);
1745*4882a593Smuzhiyun 	/* User space is in kbits/sec, hardware in bytes/sec */
1746*4882a593Smuzhiyun 	cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
1747*4882a593Smuzhiyun 	cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
1748*4882a593Smuzhiyun 	/* Convert the negative values from 64-bit 2's complement
1749*4882a593Smuzhiyun 	 * to 32-bit 2's complement (for the case of 0x80000000 whose
1750*4882a593Smuzhiyun 	 * negative is still negative).
1751*4882a593Smuzhiyun 	 */
1752*4882a593Smuzhiyun 	cbs->credit_lo &= GENMASK_ULL(31, 0);
1753*4882a593Smuzhiyun 	cbs->send_slope &= GENMASK_ULL(31, 0);
1754*4882a593Smuzhiyun 
1755*4882a593Smuzhiyun 	return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
1756*4882a593Smuzhiyun 					    true);
1757*4882a593Smuzhiyun }
1758*4882a593Smuzhiyun 
sja1105_reload_cbs(struct sja1105_private * priv)1759*4882a593Smuzhiyun static int sja1105_reload_cbs(struct sja1105_private *priv)
1760*4882a593Smuzhiyun {
1761*4882a593Smuzhiyun 	int rc = 0, i;
1762*4882a593Smuzhiyun 
1763*4882a593Smuzhiyun 	/* The credit based shapers are only allocated if
1764*4882a593Smuzhiyun 	 * CONFIG_NET_SCH_CBS is enabled.
1765*4882a593Smuzhiyun 	 */
1766*4882a593Smuzhiyun 	if (!priv->cbs)
1767*4882a593Smuzhiyun 		return 0;
1768*4882a593Smuzhiyun 
1769*4882a593Smuzhiyun 	for (i = 0; i < priv->info->num_cbs_shapers; i++) {
1770*4882a593Smuzhiyun 		struct sja1105_cbs_entry *cbs = &priv->cbs[i];
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 		if (!cbs->idle_slope && !cbs->send_slope)
1773*4882a593Smuzhiyun 			continue;
1774*4882a593Smuzhiyun 
1775*4882a593Smuzhiyun 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
1776*4882a593Smuzhiyun 						  true);
1777*4882a593Smuzhiyun 		if (rc)
1778*4882a593Smuzhiyun 			break;
1779*4882a593Smuzhiyun 	}
1780*4882a593Smuzhiyun 
1781*4882a593Smuzhiyun 	return rc;
1782*4882a593Smuzhiyun }
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun static const char * const sja1105_reset_reasons[] = {
1785*4882a593Smuzhiyun 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
1786*4882a593Smuzhiyun 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
1787*4882a593Smuzhiyun 	[SJA1105_AGEING_TIME] = "Ageing time",
1788*4882a593Smuzhiyun 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
1789*4882a593Smuzhiyun 	[SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1790*4882a593Smuzhiyun 	[SJA1105_VIRTUAL_LINKS] = "Virtual links",
1791*4882a593Smuzhiyun };
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun /* For situations where we need to change a setting at runtime that is only
1794*4882a593Smuzhiyun  * available through the static configuration, resetting the switch in order
1795*4882a593Smuzhiyun  * to upload the new static config is unavoidable. Back up the settings we
1796*4882a593Smuzhiyun  * modify at runtime (currently only MAC) and restore them after uploading,
1797*4882a593Smuzhiyun  * such that this operation is relatively seamless.
1798*4882a593Smuzhiyun  */
sja1105_static_config_reload(struct sja1105_private * priv,enum sja1105_reset_reason reason)1799*4882a593Smuzhiyun int sja1105_static_config_reload(struct sja1105_private *priv,
1800*4882a593Smuzhiyun 				 enum sja1105_reset_reason reason)
1801*4882a593Smuzhiyun {
1802*4882a593Smuzhiyun 	struct ptp_system_timestamp ptp_sts_before;
1803*4882a593Smuzhiyun 	struct ptp_system_timestamp ptp_sts_after;
1804*4882a593Smuzhiyun 	struct sja1105_mac_config_entry *mac;
1805*4882a593Smuzhiyun 	int speed_mbps[SJA1105_NUM_PORTS];
1806*4882a593Smuzhiyun 	struct dsa_switch *ds = priv->ds;
1807*4882a593Smuzhiyun 	s64 t1, t2, t3, t4;
1808*4882a593Smuzhiyun 	s64 t12, t34;
1809*4882a593Smuzhiyun 	u16 bmcr = 0;
1810*4882a593Smuzhiyun 	int rc, i;
1811*4882a593Smuzhiyun 	s64 now;
1812*4882a593Smuzhiyun 
1813*4882a593Smuzhiyun 	mutex_lock(&priv->mgmt_lock);
1814*4882a593Smuzhiyun 
1815*4882a593Smuzhiyun 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1816*4882a593Smuzhiyun 
1817*4882a593Smuzhiyun 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
1818*4882a593Smuzhiyun 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
1819*4882a593Smuzhiyun 	 * switch wants to see in the static config in order to allow us to
1820*4882a593Smuzhiyun 	 * change it through the dynamic interface later.
1821*4882a593Smuzhiyun 	 */
1822*4882a593Smuzhiyun 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1823*4882a593Smuzhiyun 		speed_mbps[i] = sja1105_speed[mac[i].speed];
1824*4882a593Smuzhiyun 		mac[i].speed = SJA1105_SPEED_AUTO;
1825*4882a593Smuzhiyun 	}
1826*4882a593Smuzhiyun 
1827*4882a593Smuzhiyun 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT))
1828*4882a593Smuzhiyun 		bmcr = sja1105_sgmii_read(priv, MII_BMCR);
1829*4882a593Smuzhiyun 
1830*4882a593Smuzhiyun 	/* No PTP operations can run right now */
1831*4882a593Smuzhiyun 	mutex_lock(&priv->ptp_data.lock);
1832*4882a593Smuzhiyun 
1833*4882a593Smuzhiyun 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
1834*4882a593Smuzhiyun 	if (rc < 0)
1835*4882a593Smuzhiyun 		goto out_unlock_ptp;
1836*4882a593Smuzhiyun 
1837*4882a593Smuzhiyun 	/* Reset switch and send updated static configuration */
1838*4882a593Smuzhiyun 	rc = sja1105_static_config_upload(priv);
1839*4882a593Smuzhiyun 	if (rc < 0)
1840*4882a593Smuzhiyun 		goto out_unlock_ptp;
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
1843*4882a593Smuzhiyun 	if (rc < 0)
1844*4882a593Smuzhiyun 		goto out_unlock_ptp;
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
1847*4882a593Smuzhiyun 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
1848*4882a593Smuzhiyun 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
1849*4882a593Smuzhiyun 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
1850*4882a593Smuzhiyun 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
1851*4882a593Smuzhiyun 	t12 = t1 + (t2 - t1) / 2;
1852*4882a593Smuzhiyun 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
1853*4882a593Smuzhiyun 	t34 = t3 + (t4 - t3) / 2;
1854*4882a593Smuzhiyun 	/* Advance PTPCLKVAL by the time it took since its readout */
1855*4882a593Smuzhiyun 	now += (t34 - t12);
1856*4882a593Smuzhiyun 
1857*4882a593Smuzhiyun 	__sja1105_ptp_adjtime(ds, now);
1858*4882a593Smuzhiyun 
1859*4882a593Smuzhiyun out_unlock_ptp:
1860*4882a593Smuzhiyun 	mutex_unlock(&priv->ptp_data.lock);
1861*4882a593Smuzhiyun 
1862*4882a593Smuzhiyun 	dev_info(priv->ds->dev,
1863*4882a593Smuzhiyun 		 "Reset switch and programmed static config. Reason: %s\n",
1864*4882a593Smuzhiyun 		 sja1105_reset_reasons[reason]);
1865*4882a593Smuzhiyun 
1866*4882a593Smuzhiyun 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
1867*4882a593Smuzhiyun 	 * For these interfaces there is no dynamic configuration
1868*4882a593Smuzhiyun 	 * needed, since PLLs have same settings at all speeds.
1869*4882a593Smuzhiyun 	 */
1870*4882a593Smuzhiyun 	rc = sja1105_clocking_setup(priv);
1871*4882a593Smuzhiyun 	if (rc < 0)
1872*4882a593Smuzhiyun 		goto out;
1873*4882a593Smuzhiyun 
1874*4882a593Smuzhiyun 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1875*4882a593Smuzhiyun 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
1876*4882a593Smuzhiyun 		if (rc < 0)
1877*4882a593Smuzhiyun 			goto out;
1878*4882a593Smuzhiyun 	}
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) {
1881*4882a593Smuzhiyun 		bool an_enabled = !!(bmcr & BMCR_ANENABLE);
1882*4882a593Smuzhiyun 
1883*4882a593Smuzhiyun 		sja1105_sgmii_pcs_config(priv, an_enabled, false);
1884*4882a593Smuzhiyun 
1885*4882a593Smuzhiyun 		if (!an_enabled) {
1886*4882a593Smuzhiyun 			int speed = SPEED_UNKNOWN;
1887*4882a593Smuzhiyun 
1888*4882a593Smuzhiyun 			if (bmcr & BMCR_SPEED1000)
1889*4882a593Smuzhiyun 				speed = SPEED_1000;
1890*4882a593Smuzhiyun 			else if (bmcr & BMCR_SPEED100)
1891*4882a593Smuzhiyun 				speed = SPEED_100;
1892*4882a593Smuzhiyun 			else
1893*4882a593Smuzhiyun 				speed = SPEED_10;
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun 			sja1105_sgmii_pcs_force_speed(priv, speed);
1896*4882a593Smuzhiyun 		}
1897*4882a593Smuzhiyun 	}
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun 	rc = sja1105_reload_cbs(priv);
1900*4882a593Smuzhiyun 	if (rc < 0)
1901*4882a593Smuzhiyun 		goto out;
1902*4882a593Smuzhiyun out:
1903*4882a593Smuzhiyun 	mutex_unlock(&priv->mgmt_lock);
1904*4882a593Smuzhiyun 
1905*4882a593Smuzhiyun 	return rc;
1906*4882a593Smuzhiyun }
1907*4882a593Smuzhiyun 
sja1105_pvid_apply(struct sja1105_private * priv,int port,u16 pvid)1908*4882a593Smuzhiyun static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
1909*4882a593Smuzhiyun {
1910*4882a593Smuzhiyun 	struct sja1105_mac_config_entry *mac;
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1913*4882a593Smuzhiyun 
1914*4882a593Smuzhiyun 	mac[port].vlanid = pvid;
1915*4882a593Smuzhiyun 
1916*4882a593Smuzhiyun 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1917*4882a593Smuzhiyun 					   &mac[port], true);
1918*4882a593Smuzhiyun }
1919*4882a593Smuzhiyun 
sja1105_crosschip_bridge_join(struct dsa_switch * ds,int tree_index,int sw_index,int other_port,struct net_device * br)1920*4882a593Smuzhiyun static int sja1105_crosschip_bridge_join(struct dsa_switch *ds,
1921*4882a593Smuzhiyun 					 int tree_index, int sw_index,
1922*4882a593Smuzhiyun 					 int other_port, struct net_device *br)
1923*4882a593Smuzhiyun {
1924*4882a593Smuzhiyun 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1925*4882a593Smuzhiyun 	struct sja1105_private *other_priv = other_ds->priv;
1926*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1927*4882a593Smuzhiyun 	int port, rc;
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun 	if (other_ds->ops != &sja1105_switch_ops)
1930*4882a593Smuzhiyun 		return 0;
1931*4882a593Smuzhiyun 
1932*4882a593Smuzhiyun 	for (port = 0; port < ds->num_ports; port++) {
1933*4882a593Smuzhiyun 		if (!dsa_is_user_port(ds, port))
1934*4882a593Smuzhiyun 			continue;
1935*4882a593Smuzhiyun 		if (dsa_to_port(ds, port)->bridge_dev != br)
1936*4882a593Smuzhiyun 			continue;
1937*4882a593Smuzhiyun 
1938*4882a593Smuzhiyun 		rc = dsa_8021q_crosschip_bridge_join(priv->dsa_8021q_ctx,
1939*4882a593Smuzhiyun 						     port,
1940*4882a593Smuzhiyun 						     other_priv->dsa_8021q_ctx,
1941*4882a593Smuzhiyun 						     other_port);
1942*4882a593Smuzhiyun 		if (rc)
1943*4882a593Smuzhiyun 			return rc;
1944*4882a593Smuzhiyun 
1945*4882a593Smuzhiyun 		rc = dsa_8021q_crosschip_bridge_join(other_priv->dsa_8021q_ctx,
1946*4882a593Smuzhiyun 						     other_port,
1947*4882a593Smuzhiyun 						     priv->dsa_8021q_ctx,
1948*4882a593Smuzhiyun 						     port);
1949*4882a593Smuzhiyun 		if (rc)
1950*4882a593Smuzhiyun 			return rc;
1951*4882a593Smuzhiyun 	}
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun 	return 0;
1954*4882a593Smuzhiyun }
1955*4882a593Smuzhiyun 
sja1105_crosschip_bridge_leave(struct dsa_switch * ds,int tree_index,int sw_index,int other_port,struct net_device * br)1956*4882a593Smuzhiyun static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds,
1957*4882a593Smuzhiyun 					   int tree_index, int sw_index,
1958*4882a593Smuzhiyun 					   int other_port,
1959*4882a593Smuzhiyun 					   struct net_device *br)
1960*4882a593Smuzhiyun {
1961*4882a593Smuzhiyun 	struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1962*4882a593Smuzhiyun 	struct sja1105_private *other_priv = other_ds->priv;
1963*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1964*4882a593Smuzhiyun 	int port;
1965*4882a593Smuzhiyun 
1966*4882a593Smuzhiyun 	if (other_ds->ops != &sja1105_switch_ops)
1967*4882a593Smuzhiyun 		return;
1968*4882a593Smuzhiyun 
1969*4882a593Smuzhiyun 	for (port = 0; port < ds->num_ports; port++) {
1970*4882a593Smuzhiyun 		if (!dsa_is_user_port(ds, port))
1971*4882a593Smuzhiyun 			continue;
1972*4882a593Smuzhiyun 		if (dsa_to_port(ds, port)->bridge_dev != br)
1973*4882a593Smuzhiyun 			continue;
1974*4882a593Smuzhiyun 
1975*4882a593Smuzhiyun 		dsa_8021q_crosschip_bridge_leave(priv->dsa_8021q_ctx, port,
1976*4882a593Smuzhiyun 						 other_priv->dsa_8021q_ctx,
1977*4882a593Smuzhiyun 						 other_port);
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun 		dsa_8021q_crosschip_bridge_leave(other_priv->dsa_8021q_ctx,
1980*4882a593Smuzhiyun 						 other_port,
1981*4882a593Smuzhiyun 						 priv->dsa_8021q_ctx, port);
1982*4882a593Smuzhiyun 	}
1983*4882a593Smuzhiyun }
1984*4882a593Smuzhiyun 
sja1105_setup_8021q_tagging(struct dsa_switch * ds,bool enabled)1985*4882a593Smuzhiyun static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1986*4882a593Smuzhiyun {
1987*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
1988*4882a593Smuzhiyun 	int rc;
1989*4882a593Smuzhiyun 
1990*4882a593Smuzhiyun 	rc = dsa_8021q_setup(priv->dsa_8021q_ctx, enabled);
1991*4882a593Smuzhiyun 	if (rc)
1992*4882a593Smuzhiyun 		return rc;
1993*4882a593Smuzhiyun 
1994*4882a593Smuzhiyun 	dev_info(ds->dev, "%s switch tagging\n",
1995*4882a593Smuzhiyun 		 enabled ? "Enabled" : "Disabled");
1996*4882a593Smuzhiyun 	return 0;
1997*4882a593Smuzhiyun }
1998*4882a593Smuzhiyun 
1999*4882a593Smuzhiyun static enum dsa_tag_protocol
sja1105_get_tag_protocol(struct dsa_switch * ds,int port,enum dsa_tag_protocol mp)2000*4882a593Smuzhiyun sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
2001*4882a593Smuzhiyun 			 enum dsa_tag_protocol mp)
2002*4882a593Smuzhiyun {
2003*4882a593Smuzhiyun 	return DSA_TAG_PROTO_SJA1105;
2004*4882a593Smuzhiyun }
2005*4882a593Smuzhiyun 
sja1105_find_free_subvlan(u16 * subvlan_map,bool pvid)2006*4882a593Smuzhiyun static int sja1105_find_free_subvlan(u16 *subvlan_map, bool pvid)
2007*4882a593Smuzhiyun {
2008*4882a593Smuzhiyun 	int subvlan;
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun 	if (pvid)
2011*4882a593Smuzhiyun 		return 0;
2012*4882a593Smuzhiyun 
2013*4882a593Smuzhiyun 	for (subvlan = 1; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2014*4882a593Smuzhiyun 		if (subvlan_map[subvlan] == VLAN_N_VID)
2015*4882a593Smuzhiyun 			return subvlan;
2016*4882a593Smuzhiyun 
2017*4882a593Smuzhiyun 	return -1;
2018*4882a593Smuzhiyun }
2019*4882a593Smuzhiyun 
sja1105_find_subvlan(u16 * subvlan_map,u16 vid)2020*4882a593Smuzhiyun static int sja1105_find_subvlan(u16 *subvlan_map, u16 vid)
2021*4882a593Smuzhiyun {
2022*4882a593Smuzhiyun 	int subvlan;
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2025*4882a593Smuzhiyun 		if (subvlan_map[subvlan] == vid)
2026*4882a593Smuzhiyun 			return subvlan;
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun 	return -1;
2029*4882a593Smuzhiyun }
2030*4882a593Smuzhiyun 
sja1105_find_committed_subvlan(struct sja1105_private * priv,int port,u16 vid)2031*4882a593Smuzhiyun static int sja1105_find_committed_subvlan(struct sja1105_private *priv,
2032*4882a593Smuzhiyun 					  int port, u16 vid)
2033*4882a593Smuzhiyun {
2034*4882a593Smuzhiyun 	struct sja1105_port *sp = &priv->ports[port];
2035*4882a593Smuzhiyun 
2036*4882a593Smuzhiyun 	return sja1105_find_subvlan(sp->subvlan_map, vid);
2037*4882a593Smuzhiyun }
2038*4882a593Smuzhiyun 
sja1105_init_subvlan_map(u16 * subvlan_map)2039*4882a593Smuzhiyun static void sja1105_init_subvlan_map(u16 *subvlan_map)
2040*4882a593Smuzhiyun {
2041*4882a593Smuzhiyun 	int subvlan;
2042*4882a593Smuzhiyun 
2043*4882a593Smuzhiyun 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2044*4882a593Smuzhiyun 		subvlan_map[subvlan] = VLAN_N_VID;
2045*4882a593Smuzhiyun }
2046*4882a593Smuzhiyun 
sja1105_commit_subvlan_map(struct sja1105_private * priv,int port,u16 * subvlan_map)2047*4882a593Smuzhiyun static void sja1105_commit_subvlan_map(struct sja1105_private *priv, int port,
2048*4882a593Smuzhiyun 				       u16 *subvlan_map)
2049*4882a593Smuzhiyun {
2050*4882a593Smuzhiyun 	struct sja1105_port *sp = &priv->ports[port];
2051*4882a593Smuzhiyun 	int subvlan;
2052*4882a593Smuzhiyun 
2053*4882a593Smuzhiyun 	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2054*4882a593Smuzhiyun 		sp->subvlan_map[subvlan] = subvlan_map[subvlan];
2055*4882a593Smuzhiyun }
2056*4882a593Smuzhiyun 
sja1105_is_vlan_configured(struct sja1105_private * priv,u16 vid)2057*4882a593Smuzhiyun static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
2058*4882a593Smuzhiyun {
2059*4882a593Smuzhiyun 	struct sja1105_vlan_lookup_entry *vlan;
2060*4882a593Smuzhiyun 	int count, i;
2061*4882a593Smuzhiyun 
2062*4882a593Smuzhiyun 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
2063*4882a593Smuzhiyun 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
2064*4882a593Smuzhiyun 
2065*4882a593Smuzhiyun 	for (i = 0; i < count; i++)
2066*4882a593Smuzhiyun 		if (vlan[i].vlanid == vid)
2067*4882a593Smuzhiyun 			return i;
2068*4882a593Smuzhiyun 
2069*4882a593Smuzhiyun 	/* Return an invalid entry index if not found */
2070*4882a593Smuzhiyun 	return -1;
2071*4882a593Smuzhiyun }
2072*4882a593Smuzhiyun 
2073*4882a593Smuzhiyun static int
sja1105_find_retagging_entry(struct sja1105_retagging_entry * retagging,int count,int from_port,u16 from_vid,u16 to_vid)2074*4882a593Smuzhiyun sja1105_find_retagging_entry(struct sja1105_retagging_entry *retagging,
2075*4882a593Smuzhiyun 			     int count, int from_port, u16 from_vid,
2076*4882a593Smuzhiyun 			     u16 to_vid)
2077*4882a593Smuzhiyun {
2078*4882a593Smuzhiyun 	int i;
2079*4882a593Smuzhiyun 
2080*4882a593Smuzhiyun 	for (i = 0; i < count; i++)
2081*4882a593Smuzhiyun 		if (retagging[i].ing_port == BIT(from_port) &&
2082*4882a593Smuzhiyun 		    retagging[i].vlan_ing == from_vid &&
2083*4882a593Smuzhiyun 		    retagging[i].vlan_egr == to_vid)
2084*4882a593Smuzhiyun 			return i;
2085*4882a593Smuzhiyun 
2086*4882a593Smuzhiyun 	/* Return an invalid entry index if not found */
2087*4882a593Smuzhiyun 	return -1;
2088*4882a593Smuzhiyun }
2089*4882a593Smuzhiyun 
sja1105_commit_vlans(struct sja1105_private * priv,struct sja1105_vlan_lookup_entry * new_vlan,struct sja1105_retagging_entry * new_retagging,int num_retagging)2090*4882a593Smuzhiyun static int sja1105_commit_vlans(struct sja1105_private *priv,
2091*4882a593Smuzhiyun 				struct sja1105_vlan_lookup_entry *new_vlan,
2092*4882a593Smuzhiyun 				struct sja1105_retagging_entry *new_retagging,
2093*4882a593Smuzhiyun 				int num_retagging)
2094*4882a593Smuzhiyun {
2095*4882a593Smuzhiyun 	struct sja1105_retagging_entry *retagging;
2096*4882a593Smuzhiyun 	struct sja1105_vlan_lookup_entry *vlan;
2097*4882a593Smuzhiyun 	struct sja1105_table *table;
2098*4882a593Smuzhiyun 	int num_vlans = 0;
2099*4882a593Smuzhiyun 	int rc, i, k = 0;
2100*4882a593Smuzhiyun 
2101*4882a593Smuzhiyun 	/* VLAN table */
2102*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2103*4882a593Smuzhiyun 	vlan = table->entries;
2104*4882a593Smuzhiyun 
2105*4882a593Smuzhiyun 	for (i = 0; i < VLAN_N_VID; i++) {
2106*4882a593Smuzhiyun 		int match = sja1105_is_vlan_configured(priv, i);
2107*4882a593Smuzhiyun 
2108*4882a593Smuzhiyun 		if (new_vlan[i].vlanid != VLAN_N_VID)
2109*4882a593Smuzhiyun 			num_vlans++;
2110*4882a593Smuzhiyun 
2111*4882a593Smuzhiyun 		if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) {
2112*4882a593Smuzhiyun 			/* Was there before, no longer is. Delete */
2113*4882a593Smuzhiyun 			dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i);
2114*4882a593Smuzhiyun 			rc = sja1105_dynamic_config_write(priv,
2115*4882a593Smuzhiyun 							  BLK_IDX_VLAN_LOOKUP,
2116*4882a593Smuzhiyun 							  i, &vlan[match], false);
2117*4882a593Smuzhiyun 			if (rc < 0)
2118*4882a593Smuzhiyun 				return rc;
2119*4882a593Smuzhiyun 		} else if (new_vlan[i].vlanid != VLAN_N_VID) {
2120*4882a593Smuzhiyun 			/* Nothing changed, don't do anything */
2121*4882a593Smuzhiyun 			if (match >= 0 &&
2122*4882a593Smuzhiyun 			    vlan[match].vlanid == new_vlan[i].vlanid &&
2123*4882a593Smuzhiyun 			    vlan[match].tag_port == new_vlan[i].tag_port &&
2124*4882a593Smuzhiyun 			    vlan[match].vlan_bc == new_vlan[i].vlan_bc &&
2125*4882a593Smuzhiyun 			    vlan[match].vmemb_port == new_vlan[i].vmemb_port)
2126*4882a593Smuzhiyun 				continue;
2127*4882a593Smuzhiyun 			/* Update entry */
2128*4882a593Smuzhiyun 			dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i);
2129*4882a593Smuzhiyun 			rc = sja1105_dynamic_config_write(priv,
2130*4882a593Smuzhiyun 							  BLK_IDX_VLAN_LOOKUP,
2131*4882a593Smuzhiyun 							  i, &new_vlan[i],
2132*4882a593Smuzhiyun 							  true);
2133*4882a593Smuzhiyun 			if (rc < 0)
2134*4882a593Smuzhiyun 				return rc;
2135*4882a593Smuzhiyun 		}
2136*4882a593Smuzhiyun 	}
2137*4882a593Smuzhiyun 
2138*4882a593Smuzhiyun 	if (table->entry_count)
2139*4882a593Smuzhiyun 		kfree(table->entries);
2140*4882a593Smuzhiyun 
2141*4882a593Smuzhiyun 	table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size,
2142*4882a593Smuzhiyun 				 GFP_KERNEL);
2143*4882a593Smuzhiyun 	if (!table->entries)
2144*4882a593Smuzhiyun 		return -ENOMEM;
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun 	table->entry_count = num_vlans;
2147*4882a593Smuzhiyun 	vlan = table->entries;
2148*4882a593Smuzhiyun 
2149*4882a593Smuzhiyun 	for (i = 0; i < VLAN_N_VID; i++) {
2150*4882a593Smuzhiyun 		if (new_vlan[i].vlanid == VLAN_N_VID)
2151*4882a593Smuzhiyun 			continue;
2152*4882a593Smuzhiyun 		vlan[k++] = new_vlan[i];
2153*4882a593Smuzhiyun 	}
2154*4882a593Smuzhiyun 
2155*4882a593Smuzhiyun 	/* VLAN Retagging Table */
2156*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_RETAGGING];
2157*4882a593Smuzhiyun 	retagging = table->entries;
2158*4882a593Smuzhiyun 
2159*4882a593Smuzhiyun 	for (i = 0; i < table->entry_count; i++) {
2160*4882a593Smuzhiyun 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
2161*4882a593Smuzhiyun 						  i, &retagging[i], false);
2162*4882a593Smuzhiyun 		if (rc)
2163*4882a593Smuzhiyun 			return rc;
2164*4882a593Smuzhiyun 	}
2165*4882a593Smuzhiyun 
2166*4882a593Smuzhiyun 	if (table->entry_count)
2167*4882a593Smuzhiyun 		kfree(table->entries);
2168*4882a593Smuzhiyun 
2169*4882a593Smuzhiyun 	table->entries = kcalloc(num_retagging, table->ops->unpacked_entry_size,
2170*4882a593Smuzhiyun 				 GFP_KERNEL);
2171*4882a593Smuzhiyun 	if (!table->entries)
2172*4882a593Smuzhiyun 		return -ENOMEM;
2173*4882a593Smuzhiyun 
2174*4882a593Smuzhiyun 	table->entry_count = num_retagging;
2175*4882a593Smuzhiyun 	retagging = table->entries;
2176*4882a593Smuzhiyun 
2177*4882a593Smuzhiyun 	for (i = 0; i < num_retagging; i++) {
2178*4882a593Smuzhiyun 		retagging[i] = new_retagging[i];
2179*4882a593Smuzhiyun 
2180*4882a593Smuzhiyun 		/* Update entry */
2181*4882a593Smuzhiyun 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
2182*4882a593Smuzhiyun 						  i, &retagging[i], true);
2183*4882a593Smuzhiyun 		if (rc < 0)
2184*4882a593Smuzhiyun 			return rc;
2185*4882a593Smuzhiyun 	}
2186*4882a593Smuzhiyun 
2187*4882a593Smuzhiyun 	return 0;
2188*4882a593Smuzhiyun }
2189*4882a593Smuzhiyun 
2190*4882a593Smuzhiyun struct sja1105_crosschip_vlan {
2191*4882a593Smuzhiyun 	struct list_head list;
2192*4882a593Smuzhiyun 	u16 vid;
2193*4882a593Smuzhiyun 	bool untagged;
2194*4882a593Smuzhiyun 	int port;
2195*4882a593Smuzhiyun 	int other_port;
2196*4882a593Smuzhiyun 	struct dsa_8021q_context *other_ctx;
2197*4882a593Smuzhiyun };
2198*4882a593Smuzhiyun 
2199*4882a593Smuzhiyun struct sja1105_crosschip_switch {
2200*4882a593Smuzhiyun 	struct list_head list;
2201*4882a593Smuzhiyun 	struct dsa_8021q_context *other_ctx;
2202*4882a593Smuzhiyun };
2203*4882a593Smuzhiyun 
sja1105_commit_pvid(struct sja1105_private * priv)2204*4882a593Smuzhiyun static int sja1105_commit_pvid(struct sja1105_private *priv)
2205*4882a593Smuzhiyun {
2206*4882a593Smuzhiyun 	struct sja1105_bridge_vlan *v;
2207*4882a593Smuzhiyun 	struct list_head *vlan_list;
2208*4882a593Smuzhiyun 	int rc = 0;
2209*4882a593Smuzhiyun 
2210*4882a593Smuzhiyun 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2211*4882a593Smuzhiyun 		vlan_list = &priv->bridge_vlans;
2212*4882a593Smuzhiyun 	else
2213*4882a593Smuzhiyun 		vlan_list = &priv->dsa_8021q_vlans;
2214*4882a593Smuzhiyun 
2215*4882a593Smuzhiyun 	list_for_each_entry(v, vlan_list, list) {
2216*4882a593Smuzhiyun 		if (v->pvid) {
2217*4882a593Smuzhiyun 			rc = sja1105_pvid_apply(priv, v->port, v->vid);
2218*4882a593Smuzhiyun 			if (rc)
2219*4882a593Smuzhiyun 				break;
2220*4882a593Smuzhiyun 		}
2221*4882a593Smuzhiyun 	}
2222*4882a593Smuzhiyun 
2223*4882a593Smuzhiyun 	return rc;
2224*4882a593Smuzhiyun }
2225*4882a593Smuzhiyun 
2226*4882a593Smuzhiyun static int
sja1105_build_bridge_vlans(struct sja1105_private * priv,struct sja1105_vlan_lookup_entry * new_vlan)2227*4882a593Smuzhiyun sja1105_build_bridge_vlans(struct sja1105_private *priv,
2228*4882a593Smuzhiyun 			   struct sja1105_vlan_lookup_entry *new_vlan)
2229*4882a593Smuzhiyun {
2230*4882a593Smuzhiyun 	struct sja1105_bridge_vlan *v;
2231*4882a593Smuzhiyun 
2232*4882a593Smuzhiyun 	if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
2233*4882a593Smuzhiyun 		return 0;
2234*4882a593Smuzhiyun 
2235*4882a593Smuzhiyun 	list_for_each_entry(v, &priv->bridge_vlans, list) {
2236*4882a593Smuzhiyun 		int match = v->vid;
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun 		new_vlan[match].vlanid = v->vid;
2239*4882a593Smuzhiyun 		new_vlan[match].vmemb_port |= BIT(v->port);
2240*4882a593Smuzhiyun 		new_vlan[match].vlan_bc |= BIT(v->port);
2241*4882a593Smuzhiyun 		if (!v->untagged)
2242*4882a593Smuzhiyun 			new_vlan[match].tag_port |= BIT(v->port);
2243*4882a593Smuzhiyun 	}
2244*4882a593Smuzhiyun 
2245*4882a593Smuzhiyun 	return 0;
2246*4882a593Smuzhiyun }
2247*4882a593Smuzhiyun 
2248*4882a593Smuzhiyun static int
sja1105_build_dsa_8021q_vlans(struct sja1105_private * priv,struct sja1105_vlan_lookup_entry * new_vlan)2249*4882a593Smuzhiyun sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv,
2250*4882a593Smuzhiyun 			      struct sja1105_vlan_lookup_entry *new_vlan)
2251*4882a593Smuzhiyun {
2252*4882a593Smuzhiyun 	struct sja1105_bridge_vlan *v;
2253*4882a593Smuzhiyun 
2254*4882a593Smuzhiyun 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2255*4882a593Smuzhiyun 		return 0;
2256*4882a593Smuzhiyun 
2257*4882a593Smuzhiyun 	list_for_each_entry(v, &priv->dsa_8021q_vlans, list) {
2258*4882a593Smuzhiyun 		int match = v->vid;
2259*4882a593Smuzhiyun 
2260*4882a593Smuzhiyun 		new_vlan[match].vlanid = v->vid;
2261*4882a593Smuzhiyun 		new_vlan[match].vmemb_port |= BIT(v->port);
2262*4882a593Smuzhiyun 		new_vlan[match].vlan_bc |= BIT(v->port);
2263*4882a593Smuzhiyun 		if (!v->untagged)
2264*4882a593Smuzhiyun 			new_vlan[match].tag_port |= BIT(v->port);
2265*4882a593Smuzhiyun 	}
2266*4882a593Smuzhiyun 
2267*4882a593Smuzhiyun 	return 0;
2268*4882a593Smuzhiyun }
2269*4882a593Smuzhiyun 
sja1105_build_subvlans(struct sja1105_private * priv,u16 subvlan_map[][DSA_8021Q_N_SUBVLAN],struct sja1105_vlan_lookup_entry * new_vlan,struct sja1105_retagging_entry * new_retagging,int * num_retagging)2270*4882a593Smuzhiyun static int sja1105_build_subvlans(struct sja1105_private *priv,
2271*4882a593Smuzhiyun 				  u16 subvlan_map[][DSA_8021Q_N_SUBVLAN],
2272*4882a593Smuzhiyun 				  struct sja1105_vlan_lookup_entry *new_vlan,
2273*4882a593Smuzhiyun 				  struct sja1105_retagging_entry *new_retagging,
2274*4882a593Smuzhiyun 				  int *num_retagging)
2275*4882a593Smuzhiyun {
2276*4882a593Smuzhiyun 	struct sja1105_bridge_vlan *v;
2277*4882a593Smuzhiyun 	int k = *num_retagging;
2278*4882a593Smuzhiyun 
2279*4882a593Smuzhiyun 	if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
2280*4882a593Smuzhiyun 		return 0;
2281*4882a593Smuzhiyun 
2282*4882a593Smuzhiyun 	list_for_each_entry(v, &priv->bridge_vlans, list) {
2283*4882a593Smuzhiyun 		int upstream = dsa_upstream_port(priv->ds, v->port);
2284*4882a593Smuzhiyun 		int match, subvlan;
2285*4882a593Smuzhiyun 		u16 rx_vid;
2286*4882a593Smuzhiyun 
2287*4882a593Smuzhiyun 		/* Only sub-VLANs on user ports need to be applied.
2288*4882a593Smuzhiyun 		 * Bridge VLANs also include VLANs added automatically
2289*4882a593Smuzhiyun 		 * by DSA on the CPU port.
2290*4882a593Smuzhiyun 		 */
2291*4882a593Smuzhiyun 		if (!dsa_is_user_port(priv->ds, v->port))
2292*4882a593Smuzhiyun 			continue;
2293*4882a593Smuzhiyun 
2294*4882a593Smuzhiyun 		subvlan = sja1105_find_subvlan(subvlan_map[v->port],
2295*4882a593Smuzhiyun 					       v->vid);
2296*4882a593Smuzhiyun 		if (subvlan < 0) {
2297*4882a593Smuzhiyun 			subvlan = sja1105_find_free_subvlan(subvlan_map[v->port],
2298*4882a593Smuzhiyun 							    v->pvid);
2299*4882a593Smuzhiyun 			if (subvlan < 0) {
2300*4882a593Smuzhiyun 				dev_err(priv->ds->dev, "No more free subvlans\n");
2301*4882a593Smuzhiyun 				return -ENOSPC;
2302*4882a593Smuzhiyun 			}
2303*4882a593Smuzhiyun 		}
2304*4882a593Smuzhiyun 
2305*4882a593Smuzhiyun 		rx_vid = dsa_8021q_rx_vid_subvlan(priv->ds, v->port, subvlan);
2306*4882a593Smuzhiyun 
2307*4882a593Smuzhiyun 		/* @v->vid on @v->port needs to be retagged to @rx_vid
2308*4882a593Smuzhiyun 		 * on @upstream. Assume @v->vid on @v->port and on
2309*4882a593Smuzhiyun 		 * @upstream was already configured by the previous
2310*4882a593Smuzhiyun 		 * iteration over bridge_vlans.
2311*4882a593Smuzhiyun 		 */
2312*4882a593Smuzhiyun 		match = rx_vid;
2313*4882a593Smuzhiyun 		new_vlan[match].vlanid = rx_vid;
2314*4882a593Smuzhiyun 		new_vlan[match].vmemb_port |= BIT(v->port);
2315*4882a593Smuzhiyun 		new_vlan[match].vmemb_port |= BIT(upstream);
2316*4882a593Smuzhiyun 		new_vlan[match].vlan_bc |= BIT(v->port);
2317*4882a593Smuzhiyun 		new_vlan[match].vlan_bc |= BIT(upstream);
2318*4882a593Smuzhiyun 		/* The "untagged" flag is set the same as for the
2319*4882a593Smuzhiyun 		 * original VLAN
2320*4882a593Smuzhiyun 		 */
2321*4882a593Smuzhiyun 		if (!v->untagged)
2322*4882a593Smuzhiyun 			new_vlan[match].tag_port |= BIT(v->port);
2323*4882a593Smuzhiyun 		/* But it's always tagged towards the CPU */
2324*4882a593Smuzhiyun 		new_vlan[match].tag_port |= BIT(upstream);
2325*4882a593Smuzhiyun 
2326*4882a593Smuzhiyun 		/* The Retagging Table generates packet *clones* with
2327*4882a593Smuzhiyun 		 * the new VLAN. This is a very odd hardware quirk
2328*4882a593Smuzhiyun 		 * which we need to suppress by dropping the original
2329*4882a593Smuzhiyun 		 * packet.
2330*4882a593Smuzhiyun 		 * Deny egress of the original VLAN towards the CPU
2331*4882a593Smuzhiyun 		 * port. This will force the switch to drop it, and
2332*4882a593Smuzhiyun 		 * we'll see only the retagged packets.
2333*4882a593Smuzhiyun 		 */
2334*4882a593Smuzhiyun 		match = v->vid;
2335*4882a593Smuzhiyun 		new_vlan[match].vlan_bc &= ~BIT(upstream);
2336*4882a593Smuzhiyun 
2337*4882a593Smuzhiyun 		/* And the retagging itself */
2338*4882a593Smuzhiyun 		new_retagging[k].vlan_ing = v->vid;
2339*4882a593Smuzhiyun 		new_retagging[k].vlan_egr = rx_vid;
2340*4882a593Smuzhiyun 		new_retagging[k].ing_port = BIT(v->port);
2341*4882a593Smuzhiyun 		new_retagging[k].egr_port = BIT(upstream);
2342*4882a593Smuzhiyun 		if (k++ == SJA1105_MAX_RETAGGING_COUNT) {
2343*4882a593Smuzhiyun 			dev_err(priv->ds->dev, "No more retagging rules\n");
2344*4882a593Smuzhiyun 			return -ENOSPC;
2345*4882a593Smuzhiyun 		}
2346*4882a593Smuzhiyun 
2347*4882a593Smuzhiyun 		subvlan_map[v->port][subvlan] = v->vid;
2348*4882a593Smuzhiyun 	}
2349*4882a593Smuzhiyun 
2350*4882a593Smuzhiyun 	*num_retagging = k;
2351*4882a593Smuzhiyun 
2352*4882a593Smuzhiyun 	return 0;
2353*4882a593Smuzhiyun }
2354*4882a593Smuzhiyun 
2355*4882a593Smuzhiyun /* Sadly, in crosschip scenarios where the CPU port is also the link to another
2356*4882a593Smuzhiyun  * switch, we should retag backwards (the dsa_8021q vid to the original vid) on
2357*4882a593Smuzhiyun  * the CPU port of neighbour switches.
2358*4882a593Smuzhiyun  */
2359*4882a593Smuzhiyun static int
sja1105_build_crosschip_subvlans(struct sja1105_private * priv,struct sja1105_vlan_lookup_entry * new_vlan,struct sja1105_retagging_entry * new_retagging,int * num_retagging)2360*4882a593Smuzhiyun sja1105_build_crosschip_subvlans(struct sja1105_private *priv,
2361*4882a593Smuzhiyun 				 struct sja1105_vlan_lookup_entry *new_vlan,
2362*4882a593Smuzhiyun 				 struct sja1105_retagging_entry *new_retagging,
2363*4882a593Smuzhiyun 				 int *num_retagging)
2364*4882a593Smuzhiyun {
2365*4882a593Smuzhiyun 	struct sja1105_crosschip_vlan *tmp, *pos;
2366*4882a593Smuzhiyun 	struct dsa_8021q_crosschip_link *c;
2367*4882a593Smuzhiyun 	struct sja1105_bridge_vlan *v, *w;
2368*4882a593Smuzhiyun 	struct list_head crosschip_vlans;
2369*4882a593Smuzhiyun 	int k = *num_retagging;
2370*4882a593Smuzhiyun 	int rc = 0;
2371*4882a593Smuzhiyun 
2372*4882a593Smuzhiyun 	if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
2373*4882a593Smuzhiyun 		return 0;
2374*4882a593Smuzhiyun 
2375*4882a593Smuzhiyun 	INIT_LIST_HEAD(&crosschip_vlans);
2376*4882a593Smuzhiyun 
2377*4882a593Smuzhiyun 	list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
2378*4882a593Smuzhiyun 		struct sja1105_private *other_priv = c->other_ctx->ds->priv;
2379*4882a593Smuzhiyun 
2380*4882a593Smuzhiyun 		if (other_priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2381*4882a593Smuzhiyun 			continue;
2382*4882a593Smuzhiyun 
2383*4882a593Smuzhiyun 		/* Crosschip links are also added to the CPU ports.
2384*4882a593Smuzhiyun 		 * Ignore those.
2385*4882a593Smuzhiyun 		 */
2386*4882a593Smuzhiyun 		if (!dsa_is_user_port(priv->ds, c->port))
2387*4882a593Smuzhiyun 			continue;
2388*4882a593Smuzhiyun 		if (!dsa_is_user_port(c->other_ctx->ds, c->other_port))
2389*4882a593Smuzhiyun 			continue;
2390*4882a593Smuzhiyun 
2391*4882a593Smuzhiyun 		/* Search for VLANs on the remote port */
2392*4882a593Smuzhiyun 		list_for_each_entry(v, &other_priv->bridge_vlans, list) {
2393*4882a593Smuzhiyun 			bool already_added = false;
2394*4882a593Smuzhiyun 			bool we_have_it = false;
2395*4882a593Smuzhiyun 
2396*4882a593Smuzhiyun 			if (v->port != c->other_port)
2397*4882a593Smuzhiyun 				continue;
2398*4882a593Smuzhiyun 
2399*4882a593Smuzhiyun 			/* If @v is a pvid on @other_ds, it does not need
2400*4882a593Smuzhiyun 			 * re-retagging, because its SVL field is 0 and we
2401*4882a593Smuzhiyun 			 * already allow that, via the dsa_8021q crosschip
2402*4882a593Smuzhiyun 			 * links.
2403*4882a593Smuzhiyun 			 */
2404*4882a593Smuzhiyun 			if (v->pvid)
2405*4882a593Smuzhiyun 				continue;
2406*4882a593Smuzhiyun 
2407*4882a593Smuzhiyun 			/* Search for the VLAN on our local port */
2408*4882a593Smuzhiyun 			list_for_each_entry(w, &priv->bridge_vlans, list) {
2409*4882a593Smuzhiyun 				if (w->port == c->port && w->vid == v->vid) {
2410*4882a593Smuzhiyun 					we_have_it = true;
2411*4882a593Smuzhiyun 					break;
2412*4882a593Smuzhiyun 				}
2413*4882a593Smuzhiyun 			}
2414*4882a593Smuzhiyun 
2415*4882a593Smuzhiyun 			if (!we_have_it)
2416*4882a593Smuzhiyun 				continue;
2417*4882a593Smuzhiyun 
2418*4882a593Smuzhiyun 			list_for_each_entry(tmp, &crosschip_vlans, list) {
2419*4882a593Smuzhiyun 				if (tmp->vid == v->vid &&
2420*4882a593Smuzhiyun 				    tmp->untagged == v->untagged &&
2421*4882a593Smuzhiyun 				    tmp->port == c->port &&
2422*4882a593Smuzhiyun 				    tmp->other_port == v->port &&
2423*4882a593Smuzhiyun 				    tmp->other_ctx == c->other_ctx) {
2424*4882a593Smuzhiyun 					already_added = true;
2425*4882a593Smuzhiyun 					break;
2426*4882a593Smuzhiyun 				}
2427*4882a593Smuzhiyun 			}
2428*4882a593Smuzhiyun 
2429*4882a593Smuzhiyun 			if (already_added)
2430*4882a593Smuzhiyun 				continue;
2431*4882a593Smuzhiyun 
2432*4882a593Smuzhiyun 			tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
2433*4882a593Smuzhiyun 			if (!tmp) {
2434*4882a593Smuzhiyun 				dev_err(priv->ds->dev, "Failed to allocate memory\n");
2435*4882a593Smuzhiyun 				rc = -ENOMEM;
2436*4882a593Smuzhiyun 				goto out;
2437*4882a593Smuzhiyun 			}
2438*4882a593Smuzhiyun 			tmp->vid = v->vid;
2439*4882a593Smuzhiyun 			tmp->port = c->port;
2440*4882a593Smuzhiyun 			tmp->other_port = v->port;
2441*4882a593Smuzhiyun 			tmp->other_ctx = c->other_ctx;
2442*4882a593Smuzhiyun 			tmp->untagged = v->untagged;
2443*4882a593Smuzhiyun 			list_add(&tmp->list, &crosschip_vlans);
2444*4882a593Smuzhiyun 		}
2445*4882a593Smuzhiyun 	}
2446*4882a593Smuzhiyun 
2447*4882a593Smuzhiyun 	list_for_each_entry(tmp, &crosschip_vlans, list) {
2448*4882a593Smuzhiyun 		struct sja1105_private *other_priv = tmp->other_ctx->ds->priv;
2449*4882a593Smuzhiyun 		int upstream = dsa_upstream_port(priv->ds, tmp->port);
2450*4882a593Smuzhiyun 		int match, subvlan;
2451*4882a593Smuzhiyun 		u16 rx_vid;
2452*4882a593Smuzhiyun 
2453*4882a593Smuzhiyun 		subvlan = sja1105_find_committed_subvlan(other_priv,
2454*4882a593Smuzhiyun 							 tmp->other_port,
2455*4882a593Smuzhiyun 							 tmp->vid);
2456*4882a593Smuzhiyun 		/* If this happens, it's a bug. The neighbour switch does not
2457*4882a593Smuzhiyun 		 * have a subvlan for tmp->vid on tmp->other_port, but it
2458*4882a593Smuzhiyun 		 * should, since we already checked for its vlan_state.
2459*4882a593Smuzhiyun 		 */
2460*4882a593Smuzhiyun 		if (WARN_ON(subvlan < 0)) {
2461*4882a593Smuzhiyun 			rc = -EINVAL;
2462*4882a593Smuzhiyun 			goto out;
2463*4882a593Smuzhiyun 		}
2464*4882a593Smuzhiyun 
2465*4882a593Smuzhiyun 		rx_vid = dsa_8021q_rx_vid_subvlan(tmp->other_ctx->ds,
2466*4882a593Smuzhiyun 						  tmp->other_port,
2467*4882a593Smuzhiyun 						  subvlan);
2468*4882a593Smuzhiyun 
2469*4882a593Smuzhiyun 		/* The @rx_vid retagged from @tmp->vid on
2470*4882a593Smuzhiyun 		 * {@tmp->other_ds, @tmp->other_port} needs to be
2471*4882a593Smuzhiyun 		 * re-retagged to @tmp->vid on the way back to us.
2472*4882a593Smuzhiyun 		 *
2473*4882a593Smuzhiyun 		 * Assume the original @tmp->vid is already configured
2474*4882a593Smuzhiyun 		 * on this local switch, otherwise we wouldn't be
2475*4882a593Smuzhiyun 		 * retagging its subvlan on the other switch in the
2476*4882a593Smuzhiyun 		 * first place. We just need to add a reverse retagging
2477*4882a593Smuzhiyun 		 * rule for @rx_vid and install @rx_vid on our ports.
2478*4882a593Smuzhiyun 		 */
2479*4882a593Smuzhiyun 		match = rx_vid;
2480*4882a593Smuzhiyun 		new_vlan[match].vlanid = rx_vid;
2481*4882a593Smuzhiyun 		new_vlan[match].vmemb_port |= BIT(tmp->port);
2482*4882a593Smuzhiyun 		new_vlan[match].vmemb_port |= BIT(upstream);
2483*4882a593Smuzhiyun 		/* The "untagged" flag is set the same as for the
2484*4882a593Smuzhiyun 		 * original VLAN. And towards the CPU, it doesn't
2485*4882a593Smuzhiyun 		 * really matter, because @rx_vid will only receive
2486*4882a593Smuzhiyun 		 * traffic on that port. For consistency with other dsa_8021q
2487*4882a593Smuzhiyun 		 * VLANs, we'll keep the CPU port tagged.
2488*4882a593Smuzhiyun 		 */
2489*4882a593Smuzhiyun 		if (!tmp->untagged)
2490*4882a593Smuzhiyun 			new_vlan[match].tag_port |= BIT(tmp->port);
2491*4882a593Smuzhiyun 		new_vlan[match].tag_port |= BIT(upstream);
2492*4882a593Smuzhiyun 		/* Deny egress of @rx_vid towards our front-panel port.
2493*4882a593Smuzhiyun 		 * This will force the switch to drop it, and we'll see
2494*4882a593Smuzhiyun 		 * only the re-retagged packets (having the original,
2495*4882a593Smuzhiyun 		 * pre-initial-retagging, VLAN @tmp->vid).
2496*4882a593Smuzhiyun 		 */
2497*4882a593Smuzhiyun 		new_vlan[match].vlan_bc &= ~BIT(tmp->port);
2498*4882a593Smuzhiyun 
2499*4882a593Smuzhiyun 		/* On reverse retagging, the same ingress VLAN goes to multiple
2500*4882a593Smuzhiyun 		 * ports. So we have an opportunity to create composite rules
2501*4882a593Smuzhiyun 		 * to not waste the limited space in the retagging table.
2502*4882a593Smuzhiyun 		 */
2503*4882a593Smuzhiyun 		k = sja1105_find_retagging_entry(new_retagging, *num_retagging,
2504*4882a593Smuzhiyun 						 upstream, rx_vid, tmp->vid);
2505*4882a593Smuzhiyun 		if (k < 0) {
2506*4882a593Smuzhiyun 			if (*num_retagging == SJA1105_MAX_RETAGGING_COUNT) {
2507*4882a593Smuzhiyun 				dev_err(priv->ds->dev, "No more retagging rules\n");
2508*4882a593Smuzhiyun 				rc = -ENOSPC;
2509*4882a593Smuzhiyun 				goto out;
2510*4882a593Smuzhiyun 			}
2511*4882a593Smuzhiyun 			k = (*num_retagging)++;
2512*4882a593Smuzhiyun 		}
2513*4882a593Smuzhiyun 		/* And the retagging itself */
2514*4882a593Smuzhiyun 		new_retagging[k].vlan_ing = rx_vid;
2515*4882a593Smuzhiyun 		new_retagging[k].vlan_egr = tmp->vid;
2516*4882a593Smuzhiyun 		new_retagging[k].ing_port = BIT(upstream);
2517*4882a593Smuzhiyun 		new_retagging[k].egr_port |= BIT(tmp->port);
2518*4882a593Smuzhiyun 	}
2519*4882a593Smuzhiyun 
2520*4882a593Smuzhiyun out:
2521*4882a593Smuzhiyun 	list_for_each_entry_safe(tmp, pos, &crosschip_vlans, list) {
2522*4882a593Smuzhiyun 		list_del(&tmp->list);
2523*4882a593Smuzhiyun 		kfree(tmp);
2524*4882a593Smuzhiyun 	}
2525*4882a593Smuzhiyun 
2526*4882a593Smuzhiyun 	return rc;
2527*4882a593Smuzhiyun }
2528*4882a593Smuzhiyun 
2529*4882a593Smuzhiyun static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify);
2530*4882a593Smuzhiyun 
sja1105_notify_crosschip_switches(struct sja1105_private * priv)2531*4882a593Smuzhiyun static int sja1105_notify_crosschip_switches(struct sja1105_private *priv)
2532*4882a593Smuzhiyun {
2533*4882a593Smuzhiyun 	struct sja1105_crosschip_switch *s, *pos;
2534*4882a593Smuzhiyun 	struct list_head crosschip_switches;
2535*4882a593Smuzhiyun 	struct dsa_8021q_crosschip_link *c;
2536*4882a593Smuzhiyun 	int rc = 0;
2537*4882a593Smuzhiyun 
2538*4882a593Smuzhiyun 	INIT_LIST_HEAD(&crosschip_switches);
2539*4882a593Smuzhiyun 
2540*4882a593Smuzhiyun 	list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
2541*4882a593Smuzhiyun 		bool already_added = false;
2542*4882a593Smuzhiyun 
2543*4882a593Smuzhiyun 		list_for_each_entry(s, &crosschip_switches, list) {
2544*4882a593Smuzhiyun 			if (s->other_ctx == c->other_ctx) {
2545*4882a593Smuzhiyun 				already_added = true;
2546*4882a593Smuzhiyun 				break;
2547*4882a593Smuzhiyun 			}
2548*4882a593Smuzhiyun 		}
2549*4882a593Smuzhiyun 
2550*4882a593Smuzhiyun 		if (already_added)
2551*4882a593Smuzhiyun 			continue;
2552*4882a593Smuzhiyun 
2553*4882a593Smuzhiyun 		s = kzalloc(sizeof(*s), GFP_KERNEL);
2554*4882a593Smuzhiyun 		if (!s) {
2555*4882a593Smuzhiyun 			dev_err(priv->ds->dev, "Failed to allocate memory\n");
2556*4882a593Smuzhiyun 			rc = -ENOMEM;
2557*4882a593Smuzhiyun 			goto out;
2558*4882a593Smuzhiyun 		}
2559*4882a593Smuzhiyun 		s->other_ctx = c->other_ctx;
2560*4882a593Smuzhiyun 		list_add(&s->list, &crosschip_switches);
2561*4882a593Smuzhiyun 	}
2562*4882a593Smuzhiyun 
2563*4882a593Smuzhiyun 	list_for_each_entry(s, &crosschip_switches, list) {
2564*4882a593Smuzhiyun 		struct sja1105_private *other_priv = s->other_ctx->ds->priv;
2565*4882a593Smuzhiyun 
2566*4882a593Smuzhiyun 		rc = sja1105_build_vlan_table(other_priv, false);
2567*4882a593Smuzhiyun 		if (rc)
2568*4882a593Smuzhiyun 			goto out;
2569*4882a593Smuzhiyun 	}
2570*4882a593Smuzhiyun 
2571*4882a593Smuzhiyun out:
2572*4882a593Smuzhiyun 	list_for_each_entry_safe(s, pos, &crosschip_switches, list) {
2573*4882a593Smuzhiyun 		list_del(&s->list);
2574*4882a593Smuzhiyun 		kfree(s);
2575*4882a593Smuzhiyun 	}
2576*4882a593Smuzhiyun 
2577*4882a593Smuzhiyun 	return rc;
2578*4882a593Smuzhiyun }
2579*4882a593Smuzhiyun 
sja1105_build_vlan_table(struct sja1105_private * priv,bool notify)2580*4882a593Smuzhiyun static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify)
2581*4882a593Smuzhiyun {
2582*4882a593Smuzhiyun 	u16 subvlan_map[SJA1105_NUM_PORTS][DSA_8021Q_N_SUBVLAN];
2583*4882a593Smuzhiyun 	struct sja1105_retagging_entry *new_retagging;
2584*4882a593Smuzhiyun 	struct sja1105_vlan_lookup_entry *new_vlan;
2585*4882a593Smuzhiyun 	struct sja1105_table *table;
2586*4882a593Smuzhiyun 	int i, num_retagging = 0;
2587*4882a593Smuzhiyun 	int rc;
2588*4882a593Smuzhiyun 
2589*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2590*4882a593Smuzhiyun 	new_vlan = kcalloc(VLAN_N_VID,
2591*4882a593Smuzhiyun 			   table->ops->unpacked_entry_size, GFP_KERNEL);
2592*4882a593Smuzhiyun 	if (!new_vlan)
2593*4882a593Smuzhiyun 		return -ENOMEM;
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2596*4882a593Smuzhiyun 	new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT,
2597*4882a593Smuzhiyun 				table->ops->unpacked_entry_size, GFP_KERNEL);
2598*4882a593Smuzhiyun 	if (!new_retagging) {
2599*4882a593Smuzhiyun 		kfree(new_vlan);
2600*4882a593Smuzhiyun 		return -ENOMEM;
2601*4882a593Smuzhiyun 	}
2602*4882a593Smuzhiyun 
2603*4882a593Smuzhiyun 	for (i = 0; i < VLAN_N_VID; i++)
2604*4882a593Smuzhiyun 		new_vlan[i].vlanid = VLAN_N_VID;
2605*4882a593Smuzhiyun 
2606*4882a593Smuzhiyun 	for (i = 0; i < SJA1105_MAX_RETAGGING_COUNT; i++)
2607*4882a593Smuzhiyun 		new_retagging[i].vlan_ing = VLAN_N_VID;
2608*4882a593Smuzhiyun 
2609*4882a593Smuzhiyun 	for (i = 0; i < priv->ds->num_ports; i++)
2610*4882a593Smuzhiyun 		sja1105_init_subvlan_map(subvlan_map[i]);
2611*4882a593Smuzhiyun 
2612*4882a593Smuzhiyun 	/* Bridge VLANs */
2613*4882a593Smuzhiyun 	rc = sja1105_build_bridge_vlans(priv, new_vlan);
2614*4882a593Smuzhiyun 	if (rc)
2615*4882a593Smuzhiyun 		goto out;
2616*4882a593Smuzhiyun 
2617*4882a593Smuzhiyun 	/* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c:
2618*4882a593Smuzhiyun 	 * - RX VLANs
2619*4882a593Smuzhiyun 	 * - TX VLANs
2620*4882a593Smuzhiyun 	 * - Crosschip links
2621*4882a593Smuzhiyun 	 */
2622*4882a593Smuzhiyun 	rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan);
2623*4882a593Smuzhiyun 	if (rc)
2624*4882a593Smuzhiyun 		goto out;
2625*4882a593Smuzhiyun 
2626*4882a593Smuzhiyun 	/* Private VLANs necessary for dsa_8021q operation, which we need to
2627*4882a593Smuzhiyun 	 * determine on our own:
2628*4882a593Smuzhiyun 	 * - Sub-VLANs
2629*4882a593Smuzhiyun 	 * - Sub-VLANs of crosschip switches
2630*4882a593Smuzhiyun 	 */
2631*4882a593Smuzhiyun 	rc = sja1105_build_subvlans(priv, subvlan_map, new_vlan, new_retagging,
2632*4882a593Smuzhiyun 				    &num_retagging);
2633*4882a593Smuzhiyun 	if (rc)
2634*4882a593Smuzhiyun 		goto out;
2635*4882a593Smuzhiyun 
2636*4882a593Smuzhiyun 	rc = sja1105_build_crosschip_subvlans(priv, new_vlan, new_retagging,
2637*4882a593Smuzhiyun 					      &num_retagging);
2638*4882a593Smuzhiyun 	if (rc)
2639*4882a593Smuzhiyun 		goto out;
2640*4882a593Smuzhiyun 
2641*4882a593Smuzhiyun 	rc = sja1105_commit_vlans(priv, new_vlan, new_retagging, num_retagging);
2642*4882a593Smuzhiyun 	if (rc)
2643*4882a593Smuzhiyun 		goto out;
2644*4882a593Smuzhiyun 
2645*4882a593Smuzhiyun 	rc = sja1105_commit_pvid(priv);
2646*4882a593Smuzhiyun 	if (rc)
2647*4882a593Smuzhiyun 		goto out;
2648*4882a593Smuzhiyun 
2649*4882a593Smuzhiyun 	for (i = 0; i < priv->ds->num_ports; i++)
2650*4882a593Smuzhiyun 		sja1105_commit_subvlan_map(priv, i, subvlan_map[i]);
2651*4882a593Smuzhiyun 
2652*4882a593Smuzhiyun 	if (notify) {
2653*4882a593Smuzhiyun 		rc = sja1105_notify_crosschip_switches(priv);
2654*4882a593Smuzhiyun 		if (rc)
2655*4882a593Smuzhiyun 			goto out;
2656*4882a593Smuzhiyun 	}
2657*4882a593Smuzhiyun 
2658*4882a593Smuzhiyun out:
2659*4882a593Smuzhiyun 	kfree(new_vlan);
2660*4882a593Smuzhiyun 	kfree(new_retagging);
2661*4882a593Smuzhiyun 
2662*4882a593Smuzhiyun 	return rc;
2663*4882a593Smuzhiyun }
2664*4882a593Smuzhiyun 
sja1105_vlan_prepare(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan)2665*4882a593Smuzhiyun static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
2666*4882a593Smuzhiyun 				const struct switchdev_obj_port_vlan *vlan)
2667*4882a593Smuzhiyun {
2668*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
2669*4882a593Smuzhiyun 	u16 vid;
2670*4882a593Smuzhiyun 
2671*4882a593Smuzhiyun 	if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2672*4882a593Smuzhiyun 		return 0;
2673*4882a593Smuzhiyun 
2674*4882a593Smuzhiyun 	/* If the user wants best-effort VLAN filtering (aka vlan_filtering
2675*4882a593Smuzhiyun 	 * bridge plus tagging), be sure to at least deny alterations to the
2676*4882a593Smuzhiyun 	 * configuration done by dsa_8021q.
2677*4882a593Smuzhiyun 	 */
2678*4882a593Smuzhiyun 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2679*4882a593Smuzhiyun 		if (vid_is_dsa_8021q(vid)) {
2680*4882a593Smuzhiyun 			dev_err(ds->dev, "Range 1024-3071 reserved for dsa_8021q operation\n");
2681*4882a593Smuzhiyun 			return -EBUSY;
2682*4882a593Smuzhiyun 		}
2683*4882a593Smuzhiyun 	}
2684*4882a593Smuzhiyun 
2685*4882a593Smuzhiyun 	return 0;
2686*4882a593Smuzhiyun }
2687*4882a593Smuzhiyun 
2688*4882a593Smuzhiyun /* The TPID setting belongs to the General Parameters table,
2689*4882a593Smuzhiyun  * which can only be partially reconfigured at runtime (and not the TPID).
2690*4882a593Smuzhiyun  * So a switch reset is required.
2691*4882a593Smuzhiyun  */
sja1105_vlan_filtering(struct dsa_switch * ds,int port,bool enabled,struct switchdev_trans * trans)2692*4882a593Smuzhiyun int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
2693*4882a593Smuzhiyun 			   struct switchdev_trans *trans)
2694*4882a593Smuzhiyun {
2695*4882a593Smuzhiyun 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2696*4882a593Smuzhiyun 	struct sja1105_general_params_entry *general_params;
2697*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
2698*4882a593Smuzhiyun 	enum sja1105_vlan_state state;
2699*4882a593Smuzhiyun 	struct sja1105_table *table;
2700*4882a593Smuzhiyun 	struct sja1105_rule *rule;
2701*4882a593Smuzhiyun 	bool want_tagging;
2702*4882a593Smuzhiyun 	u16 tpid, tpid2;
2703*4882a593Smuzhiyun 	int rc;
2704*4882a593Smuzhiyun 
2705*4882a593Smuzhiyun 	if (switchdev_trans_ph_prepare(trans)) {
2706*4882a593Smuzhiyun 		list_for_each_entry(rule, &priv->flow_block.rules, list) {
2707*4882a593Smuzhiyun 			if (rule->type == SJA1105_RULE_VL) {
2708*4882a593Smuzhiyun 				dev_err(ds->dev,
2709*4882a593Smuzhiyun 					"Cannot change VLAN filtering with active VL rules\n");
2710*4882a593Smuzhiyun 				return -EBUSY;
2711*4882a593Smuzhiyun 			}
2712*4882a593Smuzhiyun 		}
2713*4882a593Smuzhiyun 
2714*4882a593Smuzhiyun 		return 0;
2715*4882a593Smuzhiyun 	}
2716*4882a593Smuzhiyun 
2717*4882a593Smuzhiyun 	if (enabled) {
2718*4882a593Smuzhiyun 		/* Enable VLAN filtering. */
2719*4882a593Smuzhiyun 		tpid  = ETH_P_8021Q;
2720*4882a593Smuzhiyun 		tpid2 = ETH_P_8021AD;
2721*4882a593Smuzhiyun 	} else {
2722*4882a593Smuzhiyun 		/* Disable VLAN filtering. */
2723*4882a593Smuzhiyun 		tpid  = ETH_P_SJA1105;
2724*4882a593Smuzhiyun 		tpid2 = ETH_P_SJA1105;
2725*4882a593Smuzhiyun 	}
2726*4882a593Smuzhiyun 
2727*4882a593Smuzhiyun 	for (port = 0; port < ds->num_ports; port++) {
2728*4882a593Smuzhiyun 		struct sja1105_port *sp = &priv->ports[port];
2729*4882a593Smuzhiyun 
2730*4882a593Smuzhiyun 		if (enabled)
2731*4882a593Smuzhiyun 			sp->xmit_tpid = priv->info->qinq_tpid;
2732*4882a593Smuzhiyun 		else
2733*4882a593Smuzhiyun 			sp->xmit_tpid = ETH_P_SJA1105;
2734*4882a593Smuzhiyun 	}
2735*4882a593Smuzhiyun 
2736*4882a593Smuzhiyun 	if (!enabled)
2737*4882a593Smuzhiyun 		state = SJA1105_VLAN_UNAWARE;
2738*4882a593Smuzhiyun 	else if (priv->best_effort_vlan_filtering)
2739*4882a593Smuzhiyun 		state = SJA1105_VLAN_BEST_EFFORT;
2740*4882a593Smuzhiyun 	else
2741*4882a593Smuzhiyun 		state = SJA1105_VLAN_FILTERING_FULL;
2742*4882a593Smuzhiyun 
2743*4882a593Smuzhiyun 	if (priv->vlan_state == state)
2744*4882a593Smuzhiyun 		return 0;
2745*4882a593Smuzhiyun 
2746*4882a593Smuzhiyun 	priv->vlan_state = state;
2747*4882a593Smuzhiyun 	want_tagging = (state == SJA1105_VLAN_UNAWARE ||
2748*4882a593Smuzhiyun 			state == SJA1105_VLAN_BEST_EFFORT);
2749*4882a593Smuzhiyun 
2750*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2751*4882a593Smuzhiyun 	general_params = table->entries;
2752*4882a593Smuzhiyun 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
2753*4882a593Smuzhiyun 	general_params->tpid = tpid;
2754*4882a593Smuzhiyun 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2755*4882a593Smuzhiyun 	general_params->tpid2 = tpid2;
2756*4882a593Smuzhiyun 	/* When VLAN filtering is on, we need to at least be able to
2757*4882a593Smuzhiyun 	 * decode management traffic through the "backup plan".
2758*4882a593Smuzhiyun 	 */
2759*4882a593Smuzhiyun 	general_params->incl_srcpt1 = enabled;
2760*4882a593Smuzhiyun 	general_params->incl_srcpt0 = enabled;
2761*4882a593Smuzhiyun 
2762*4882a593Smuzhiyun 	want_tagging = priv->best_effort_vlan_filtering || !enabled;
2763*4882a593Smuzhiyun 
2764*4882a593Smuzhiyun 	/* VLAN filtering => independent VLAN learning.
2765*4882a593Smuzhiyun 	 * No VLAN filtering (or best effort) => shared VLAN learning.
2766*4882a593Smuzhiyun 	 *
2767*4882a593Smuzhiyun 	 * In shared VLAN learning mode, untagged traffic still gets
2768*4882a593Smuzhiyun 	 * pvid-tagged, and the FDB table gets populated with entries
2769*4882a593Smuzhiyun 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
2770*4882a593Smuzhiyun 	 * However the switch performs a masked L2 lookup in the FDB,
2771*4882a593Smuzhiyun 	 * effectively only looking up a frame's DMAC (and not VID) for the
2772*4882a593Smuzhiyun 	 * forwarding decision.
2773*4882a593Smuzhiyun 	 *
2774*4882a593Smuzhiyun 	 * This is extremely convenient for us, because in modes with
2775*4882a593Smuzhiyun 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
2776*4882a593Smuzhiyun 	 * each front panel port. This is good for identification but breaks
2777*4882a593Smuzhiyun 	 * learning badly - the VID of the learnt FDB entry is unique, aka
2778*4882a593Smuzhiyun 	 * no frames coming from any other port are going to have it. So
2779*4882a593Smuzhiyun 	 * for forwarding purposes, this is as though learning was broken
2780*4882a593Smuzhiyun 	 * (all frames get flooded).
2781*4882a593Smuzhiyun 	 */
2782*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2783*4882a593Smuzhiyun 	l2_lookup_params = table->entries;
2784*4882a593Smuzhiyun 	l2_lookup_params->shared_learn = want_tagging;
2785*4882a593Smuzhiyun 
2786*4882a593Smuzhiyun 	sja1105_frame_memory_partitioning(priv);
2787*4882a593Smuzhiyun 
2788*4882a593Smuzhiyun 	rc = sja1105_build_vlan_table(priv, false);
2789*4882a593Smuzhiyun 	if (rc)
2790*4882a593Smuzhiyun 		return rc;
2791*4882a593Smuzhiyun 
2792*4882a593Smuzhiyun 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
2793*4882a593Smuzhiyun 	if (rc)
2794*4882a593Smuzhiyun 		dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
2795*4882a593Smuzhiyun 
2796*4882a593Smuzhiyun 	/* Switch port identification based on 802.1Q is only passable
2797*4882a593Smuzhiyun 	 * if we are not under a vlan_filtering bridge. So make sure
2798*4882a593Smuzhiyun 	 * the two configurations are mutually exclusive (of course, the
2799*4882a593Smuzhiyun 	 * user may know better, i.e. best_effort_vlan_filtering).
2800*4882a593Smuzhiyun 	 */
2801*4882a593Smuzhiyun 	return sja1105_setup_8021q_tagging(ds, want_tagging);
2802*4882a593Smuzhiyun }
2803*4882a593Smuzhiyun 
2804*4882a593Smuzhiyun /* Returns number of VLANs added (0 or 1) on success,
2805*4882a593Smuzhiyun  * or a negative error code.
2806*4882a593Smuzhiyun  */
sja1105_vlan_add_one(struct dsa_switch * ds,int port,u16 vid,u16 flags,struct list_head * vlan_list)2807*4882a593Smuzhiyun static int sja1105_vlan_add_one(struct dsa_switch *ds, int port, u16 vid,
2808*4882a593Smuzhiyun 				u16 flags, struct list_head *vlan_list)
2809*4882a593Smuzhiyun {
2810*4882a593Smuzhiyun 	bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED;
2811*4882a593Smuzhiyun 	bool pvid = flags & BRIDGE_VLAN_INFO_PVID;
2812*4882a593Smuzhiyun 	struct sja1105_bridge_vlan *v;
2813*4882a593Smuzhiyun 
2814*4882a593Smuzhiyun 	list_for_each_entry(v, vlan_list, list) {
2815*4882a593Smuzhiyun 		if (v->port == port && v->vid == vid) {
2816*4882a593Smuzhiyun 			/* Already added */
2817*4882a593Smuzhiyun 			if (v->untagged == untagged && v->pvid == pvid)
2818*4882a593Smuzhiyun 				/* Nothing changed */
2819*4882a593Smuzhiyun 				return 0;
2820*4882a593Smuzhiyun 
2821*4882a593Smuzhiyun 			/* It's the same VLAN, but some of the flags changed
2822*4882a593Smuzhiyun 			 * and the user did not bother to delete it first.
2823*4882a593Smuzhiyun 			 * Update it and trigger sja1105_build_vlan_table.
2824*4882a593Smuzhiyun 			 */
2825*4882a593Smuzhiyun 			v->untagged = untagged;
2826*4882a593Smuzhiyun 			v->pvid = pvid;
2827*4882a593Smuzhiyun 			return 1;
2828*4882a593Smuzhiyun 		}
2829*4882a593Smuzhiyun 	}
2830*4882a593Smuzhiyun 
2831*4882a593Smuzhiyun 	v = kzalloc(sizeof(*v), GFP_KERNEL);
2832*4882a593Smuzhiyun 	if (!v) {
2833*4882a593Smuzhiyun 		dev_err(ds->dev, "Out of memory while storing VLAN\n");
2834*4882a593Smuzhiyun 		return -ENOMEM;
2835*4882a593Smuzhiyun 	}
2836*4882a593Smuzhiyun 
2837*4882a593Smuzhiyun 	v->port = port;
2838*4882a593Smuzhiyun 	v->vid = vid;
2839*4882a593Smuzhiyun 	v->untagged = untagged;
2840*4882a593Smuzhiyun 	v->pvid = pvid;
2841*4882a593Smuzhiyun 	list_add(&v->list, vlan_list);
2842*4882a593Smuzhiyun 
2843*4882a593Smuzhiyun 	return 1;
2844*4882a593Smuzhiyun }
2845*4882a593Smuzhiyun 
2846*4882a593Smuzhiyun /* Returns number of VLANs deleted (0 or 1) */
sja1105_vlan_del_one(struct dsa_switch * ds,int port,u16 vid,struct list_head * vlan_list)2847*4882a593Smuzhiyun static int sja1105_vlan_del_one(struct dsa_switch *ds, int port, u16 vid,
2848*4882a593Smuzhiyun 				struct list_head *vlan_list)
2849*4882a593Smuzhiyun {
2850*4882a593Smuzhiyun 	struct sja1105_bridge_vlan *v, *n;
2851*4882a593Smuzhiyun 
2852*4882a593Smuzhiyun 	list_for_each_entry_safe(v, n, vlan_list, list) {
2853*4882a593Smuzhiyun 		if (v->port == port && v->vid == vid) {
2854*4882a593Smuzhiyun 			list_del(&v->list);
2855*4882a593Smuzhiyun 			kfree(v);
2856*4882a593Smuzhiyun 			return 1;
2857*4882a593Smuzhiyun 		}
2858*4882a593Smuzhiyun 	}
2859*4882a593Smuzhiyun 
2860*4882a593Smuzhiyun 	return 0;
2861*4882a593Smuzhiyun }
2862*4882a593Smuzhiyun 
sja1105_vlan_add(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan)2863*4882a593Smuzhiyun static void sja1105_vlan_add(struct dsa_switch *ds, int port,
2864*4882a593Smuzhiyun 			     const struct switchdev_obj_port_vlan *vlan)
2865*4882a593Smuzhiyun {
2866*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
2867*4882a593Smuzhiyun 	bool vlan_table_changed = false;
2868*4882a593Smuzhiyun 	u16 vid;
2869*4882a593Smuzhiyun 	int rc;
2870*4882a593Smuzhiyun 
2871*4882a593Smuzhiyun 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2872*4882a593Smuzhiyun 		rc = sja1105_vlan_add_one(ds, port, vid, vlan->flags,
2873*4882a593Smuzhiyun 					  &priv->bridge_vlans);
2874*4882a593Smuzhiyun 		if (rc < 0)
2875*4882a593Smuzhiyun 			return;
2876*4882a593Smuzhiyun 		if (rc > 0)
2877*4882a593Smuzhiyun 			vlan_table_changed = true;
2878*4882a593Smuzhiyun 	}
2879*4882a593Smuzhiyun 
2880*4882a593Smuzhiyun 	if (!vlan_table_changed)
2881*4882a593Smuzhiyun 		return;
2882*4882a593Smuzhiyun 
2883*4882a593Smuzhiyun 	rc = sja1105_build_vlan_table(priv, true);
2884*4882a593Smuzhiyun 	if (rc)
2885*4882a593Smuzhiyun 		dev_err(ds->dev, "Failed to build VLAN table: %d\n", rc);
2886*4882a593Smuzhiyun }
2887*4882a593Smuzhiyun 
sja1105_vlan_del(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan)2888*4882a593Smuzhiyun static int sja1105_vlan_del(struct dsa_switch *ds, int port,
2889*4882a593Smuzhiyun 			    const struct switchdev_obj_port_vlan *vlan)
2890*4882a593Smuzhiyun {
2891*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
2892*4882a593Smuzhiyun 	bool vlan_table_changed = false;
2893*4882a593Smuzhiyun 	u16 vid;
2894*4882a593Smuzhiyun 	int rc;
2895*4882a593Smuzhiyun 
2896*4882a593Smuzhiyun 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2897*4882a593Smuzhiyun 		rc = sja1105_vlan_del_one(ds, port, vid, &priv->bridge_vlans);
2898*4882a593Smuzhiyun 		if (rc > 0)
2899*4882a593Smuzhiyun 			vlan_table_changed = true;
2900*4882a593Smuzhiyun 	}
2901*4882a593Smuzhiyun 
2902*4882a593Smuzhiyun 	if (!vlan_table_changed)
2903*4882a593Smuzhiyun 		return 0;
2904*4882a593Smuzhiyun 
2905*4882a593Smuzhiyun 	return sja1105_build_vlan_table(priv, true);
2906*4882a593Smuzhiyun }
2907*4882a593Smuzhiyun 
sja1105_dsa_8021q_vlan_add(struct dsa_switch * ds,int port,u16 vid,u16 flags)2908*4882a593Smuzhiyun static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
2909*4882a593Smuzhiyun 				      u16 flags)
2910*4882a593Smuzhiyun {
2911*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
2912*4882a593Smuzhiyun 	int rc;
2913*4882a593Smuzhiyun 
2914*4882a593Smuzhiyun 	rc = sja1105_vlan_add_one(ds, port, vid, flags, &priv->dsa_8021q_vlans);
2915*4882a593Smuzhiyun 	if (rc <= 0)
2916*4882a593Smuzhiyun 		return rc;
2917*4882a593Smuzhiyun 
2918*4882a593Smuzhiyun 	return sja1105_build_vlan_table(priv, true);
2919*4882a593Smuzhiyun }
2920*4882a593Smuzhiyun 
sja1105_dsa_8021q_vlan_del(struct dsa_switch * ds,int port,u16 vid)2921*4882a593Smuzhiyun static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
2922*4882a593Smuzhiyun {
2923*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
2924*4882a593Smuzhiyun 	int rc;
2925*4882a593Smuzhiyun 
2926*4882a593Smuzhiyun 	rc = sja1105_vlan_del_one(ds, port, vid, &priv->dsa_8021q_vlans);
2927*4882a593Smuzhiyun 	if (!rc)
2928*4882a593Smuzhiyun 		return 0;
2929*4882a593Smuzhiyun 
2930*4882a593Smuzhiyun 	return sja1105_build_vlan_table(priv, true);
2931*4882a593Smuzhiyun }
2932*4882a593Smuzhiyun 
2933*4882a593Smuzhiyun static const struct dsa_8021q_ops sja1105_dsa_8021q_ops = {
2934*4882a593Smuzhiyun 	.vlan_add	= sja1105_dsa_8021q_vlan_add,
2935*4882a593Smuzhiyun 	.vlan_del	= sja1105_dsa_8021q_vlan_del,
2936*4882a593Smuzhiyun };
2937*4882a593Smuzhiyun 
2938*4882a593Smuzhiyun /* The programming model for the SJA1105 switch is "all-at-once" via static
2939*4882a593Smuzhiyun  * configuration tables. Some of these can be dynamically modified at runtime,
2940*4882a593Smuzhiyun  * but not the xMII mode parameters table.
2941*4882a593Smuzhiyun  * Furthermode, some PHYs may not have crystals for generating their clocks
2942*4882a593Smuzhiyun  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
2943*4882a593Smuzhiyun  * ref_clk pin. So port clocking needs to be initialized early, before
2944*4882a593Smuzhiyun  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
2945*4882a593Smuzhiyun  * Setting correct PHY link speed does not matter now.
2946*4882a593Smuzhiyun  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
2947*4882a593Smuzhiyun  * bindings are not yet parsed by DSA core. We need to parse early so that we
2948*4882a593Smuzhiyun  * can populate the xMII mode parameters table.
2949*4882a593Smuzhiyun  */
sja1105_setup(struct dsa_switch * ds)2950*4882a593Smuzhiyun static int sja1105_setup(struct dsa_switch *ds)
2951*4882a593Smuzhiyun {
2952*4882a593Smuzhiyun 	struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
2953*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
2954*4882a593Smuzhiyun 	int rc;
2955*4882a593Smuzhiyun 
2956*4882a593Smuzhiyun 	rc = sja1105_parse_dt(priv, ports);
2957*4882a593Smuzhiyun 	if (rc < 0) {
2958*4882a593Smuzhiyun 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
2959*4882a593Smuzhiyun 		return rc;
2960*4882a593Smuzhiyun 	}
2961*4882a593Smuzhiyun 
2962*4882a593Smuzhiyun 	/* Error out early if internal delays are required through DT
2963*4882a593Smuzhiyun 	 * and we can't apply them.
2964*4882a593Smuzhiyun 	 */
2965*4882a593Smuzhiyun 	rc = sja1105_parse_rgmii_delays(priv, ports);
2966*4882a593Smuzhiyun 	if (rc < 0) {
2967*4882a593Smuzhiyun 		dev_err(ds->dev, "RGMII delay not supported\n");
2968*4882a593Smuzhiyun 		return rc;
2969*4882a593Smuzhiyun 	}
2970*4882a593Smuzhiyun 
2971*4882a593Smuzhiyun 	rc = sja1105_ptp_clock_register(ds);
2972*4882a593Smuzhiyun 	if (rc < 0) {
2973*4882a593Smuzhiyun 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
2974*4882a593Smuzhiyun 		return rc;
2975*4882a593Smuzhiyun 	}
2976*4882a593Smuzhiyun 	/* Create and send configuration down to device */
2977*4882a593Smuzhiyun 	rc = sja1105_static_config_load(priv, ports);
2978*4882a593Smuzhiyun 	if (rc < 0) {
2979*4882a593Smuzhiyun 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
2980*4882a593Smuzhiyun 		goto out_ptp_clock_unregister;
2981*4882a593Smuzhiyun 	}
2982*4882a593Smuzhiyun 	/* Configure the CGU (PHY link modes and speeds) */
2983*4882a593Smuzhiyun 	rc = sja1105_clocking_setup(priv);
2984*4882a593Smuzhiyun 	if (rc < 0) {
2985*4882a593Smuzhiyun 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
2986*4882a593Smuzhiyun 		goto out_static_config_free;
2987*4882a593Smuzhiyun 	}
2988*4882a593Smuzhiyun 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
2989*4882a593Smuzhiyun 	 * The only thing we can do to disable it is lie about what the 802.1Q
2990*4882a593Smuzhiyun 	 * EtherType is.
2991*4882a593Smuzhiyun 	 * So it will still try to apply VLAN filtering, but all ingress
2992*4882a593Smuzhiyun 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
2993*4882a593Smuzhiyun 	 * will be internally tagged with a distorted VLAN header where the
2994*4882a593Smuzhiyun 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
2995*4882a593Smuzhiyun 	 */
2996*4882a593Smuzhiyun 	ds->vlan_filtering_is_global = true;
2997*4882a593Smuzhiyun 
2998*4882a593Smuzhiyun 	/* Advertise the 8 egress queues */
2999*4882a593Smuzhiyun 	ds->num_tx_queues = SJA1105_NUM_TC;
3000*4882a593Smuzhiyun 
3001*4882a593Smuzhiyun 	ds->mtu_enforcement_ingress = true;
3002*4882a593Smuzhiyun 
3003*4882a593Smuzhiyun 	ds->configure_vlan_while_not_filtering = true;
3004*4882a593Smuzhiyun 
3005*4882a593Smuzhiyun 	rc = sja1105_devlink_setup(ds);
3006*4882a593Smuzhiyun 	if (rc < 0)
3007*4882a593Smuzhiyun 		goto out_static_config_free;
3008*4882a593Smuzhiyun 
3009*4882a593Smuzhiyun 	/* The DSA/switchdev model brings up switch ports in standalone mode by
3010*4882a593Smuzhiyun 	 * default, and that means vlan_filtering is 0 since they're not under
3011*4882a593Smuzhiyun 	 * a bridge, so it's safe to set up switch tagging at this time.
3012*4882a593Smuzhiyun 	 */
3013*4882a593Smuzhiyun 	rtnl_lock();
3014*4882a593Smuzhiyun 	rc = sja1105_setup_8021q_tagging(ds, true);
3015*4882a593Smuzhiyun 	rtnl_unlock();
3016*4882a593Smuzhiyun 	if (rc)
3017*4882a593Smuzhiyun 		goto out_devlink_teardown;
3018*4882a593Smuzhiyun 
3019*4882a593Smuzhiyun 	return 0;
3020*4882a593Smuzhiyun 
3021*4882a593Smuzhiyun out_devlink_teardown:
3022*4882a593Smuzhiyun 	sja1105_devlink_teardown(ds);
3023*4882a593Smuzhiyun out_ptp_clock_unregister:
3024*4882a593Smuzhiyun 	sja1105_ptp_clock_unregister(ds);
3025*4882a593Smuzhiyun out_static_config_free:
3026*4882a593Smuzhiyun 	sja1105_static_config_free(&priv->static_config);
3027*4882a593Smuzhiyun 
3028*4882a593Smuzhiyun 	return rc;
3029*4882a593Smuzhiyun }
3030*4882a593Smuzhiyun 
sja1105_teardown(struct dsa_switch * ds)3031*4882a593Smuzhiyun static void sja1105_teardown(struct dsa_switch *ds)
3032*4882a593Smuzhiyun {
3033*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
3034*4882a593Smuzhiyun 	struct sja1105_bridge_vlan *v, *n;
3035*4882a593Smuzhiyun 	int port;
3036*4882a593Smuzhiyun 
3037*4882a593Smuzhiyun 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
3038*4882a593Smuzhiyun 		struct sja1105_port *sp = &priv->ports[port];
3039*4882a593Smuzhiyun 
3040*4882a593Smuzhiyun 		if (!dsa_is_user_port(ds, port))
3041*4882a593Smuzhiyun 			continue;
3042*4882a593Smuzhiyun 
3043*4882a593Smuzhiyun 		if (sp->xmit_worker)
3044*4882a593Smuzhiyun 			kthread_destroy_worker(sp->xmit_worker);
3045*4882a593Smuzhiyun 	}
3046*4882a593Smuzhiyun 
3047*4882a593Smuzhiyun 	sja1105_devlink_teardown(ds);
3048*4882a593Smuzhiyun 	sja1105_flower_teardown(ds);
3049*4882a593Smuzhiyun 	sja1105_tas_teardown(ds);
3050*4882a593Smuzhiyun 	sja1105_ptp_clock_unregister(ds);
3051*4882a593Smuzhiyun 	sja1105_static_config_free(&priv->static_config);
3052*4882a593Smuzhiyun 
3053*4882a593Smuzhiyun 	list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) {
3054*4882a593Smuzhiyun 		list_del(&v->list);
3055*4882a593Smuzhiyun 		kfree(v);
3056*4882a593Smuzhiyun 	}
3057*4882a593Smuzhiyun 
3058*4882a593Smuzhiyun 	list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) {
3059*4882a593Smuzhiyun 		list_del(&v->list);
3060*4882a593Smuzhiyun 		kfree(v);
3061*4882a593Smuzhiyun 	}
3062*4882a593Smuzhiyun }
3063*4882a593Smuzhiyun 
sja1105_port_enable(struct dsa_switch * ds,int port,struct phy_device * phy)3064*4882a593Smuzhiyun static int sja1105_port_enable(struct dsa_switch *ds, int port,
3065*4882a593Smuzhiyun 			       struct phy_device *phy)
3066*4882a593Smuzhiyun {
3067*4882a593Smuzhiyun 	struct net_device *slave;
3068*4882a593Smuzhiyun 
3069*4882a593Smuzhiyun 	if (!dsa_is_user_port(ds, port))
3070*4882a593Smuzhiyun 		return 0;
3071*4882a593Smuzhiyun 
3072*4882a593Smuzhiyun 	slave = dsa_to_port(ds, port)->slave;
3073*4882a593Smuzhiyun 
3074*4882a593Smuzhiyun 	slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3075*4882a593Smuzhiyun 
3076*4882a593Smuzhiyun 	return 0;
3077*4882a593Smuzhiyun }
3078*4882a593Smuzhiyun 
sja1105_port_disable(struct dsa_switch * ds,int port)3079*4882a593Smuzhiyun static void sja1105_port_disable(struct dsa_switch *ds, int port)
3080*4882a593Smuzhiyun {
3081*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
3082*4882a593Smuzhiyun 	struct sja1105_port *sp = &priv->ports[port];
3083*4882a593Smuzhiyun 
3084*4882a593Smuzhiyun 	if (!dsa_is_user_port(ds, port))
3085*4882a593Smuzhiyun 		return;
3086*4882a593Smuzhiyun 
3087*4882a593Smuzhiyun 	kthread_cancel_work_sync(&sp->xmit_work);
3088*4882a593Smuzhiyun 	skb_queue_purge(&sp->xmit_queue);
3089*4882a593Smuzhiyun }
3090*4882a593Smuzhiyun 
sja1105_mgmt_xmit(struct dsa_switch * ds,int port,int slot,struct sk_buff * skb,bool takets)3091*4882a593Smuzhiyun static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
3092*4882a593Smuzhiyun 			     struct sk_buff *skb, bool takets)
3093*4882a593Smuzhiyun {
3094*4882a593Smuzhiyun 	struct sja1105_mgmt_entry mgmt_route = {0};
3095*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
3096*4882a593Smuzhiyun 	struct ethhdr *hdr;
3097*4882a593Smuzhiyun 	int timeout = 10;
3098*4882a593Smuzhiyun 	int rc;
3099*4882a593Smuzhiyun 
3100*4882a593Smuzhiyun 	hdr = eth_hdr(skb);
3101*4882a593Smuzhiyun 
3102*4882a593Smuzhiyun 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
3103*4882a593Smuzhiyun 	mgmt_route.destports = BIT(port);
3104*4882a593Smuzhiyun 	mgmt_route.enfport = 1;
3105*4882a593Smuzhiyun 	mgmt_route.tsreg = 0;
3106*4882a593Smuzhiyun 	mgmt_route.takets = takets;
3107*4882a593Smuzhiyun 
3108*4882a593Smuzhiyun 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3109*4882a593Smuzhiyun 					  slot, &mgmt_route, true);
3110*4882a593Smuzhiyun 	if (rc < 0) {
3111*4882a593Smuzhiyun 		kfree_skb(skb);
3112*4882a593Smuzhiyun 		return rc;
3113*4882a593Smuzhiyun 	}
3114*4882a593Smuzhiyun 
3115*4882a593Smuzhiyun 	/* Transfer skb to the host port. */
3116*4882a593Smuzhiyun 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
3117*4882a593Smuzhiyun 
3118*4882a593Smuzhiyun 	/* Wait until the switch has processed the frame */
3119*4882a593Smuzhiyun 	do {
3120*4882a593Smuzhiyun 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
3121*4882a593Smuzhiyun 						 slot, &mgmt_route);
3122*4882a593Smuzhiyun 		if (rc < 0) {
3123*4882a593Smuzhiyun 			dev_err_ratelimited(priv->ds->dev,
3124*4882a593Smuzhiyun 					    "failed to poll for mgmt route\n");
3125*4882a593Smuzhiyun 			continue;
3126*4882a593Smuzhiyun 		}
3127*4882a593Smuzhiyun 
3128*4882a593Smuzhiyun 		/* UM10944: The ENFPORT flag of the respective entry is
3129*4882a593Smuzhiyun 		 * cleared when a match is found. The host can use this
3130*4882a593Smuzhiyun 		 * flag as an acknowledgment.
3131*4882a593Smuzhiyun 		 */
3132*4882a593Smuzhiyun 		cpu_relax();
3133*4882a593Smuzhiyun 	} while (mgmt_route.enfport && --timeout);
3134*4882a593Smuzhiyun 
3135*4882a593Smuzhiyun 	if (!timeout) {
3136*4882a593Smuzhiyun 		/* Clean up the management route so that a follow-up
3137*4882a593Smuzhiyun 		 * frame may not match on it by mistake.
3138*4882a593Smuzhiyun 		 * This is only hardware supported on P/Q/R/S - on E/T it is
3139*4882a593Smuzhiyun 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
3140*4882a593Smuzhiyun 		 */
3141*4882a593Smuzhiyun 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3142*4882a593Smuzhiyun 					     slot, &mgmt_route, false);
3143*4882a593Smuzhiyun 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
3144*4882a593Smuzhiyun 	}
3145*4882a593Smuzhiyun 
3146*4882a593Smuzhiyun 	return NETDEV_TX_OK;
3147*4882a593Smuzhiyun }
3148*4882a593Smuzhiyun 
3149*4882a593Smuzhiyun #define work_to_port(work) \
3150*4882a593Smuzhiyun 		container_of((work), struct sja1105_port, xmit_work)
3151*4882a593Smuzhiyun #define tagger_to_sja1105(t) \
3152*4882a593Smuzhiyun 		container_of((t), struct sja1105_private, tagger_data)
3153*4882a593Smuzhiyun 
3154*4882a593Smuzhiyun /* Deferred work is unfortunately necessary because setting up the management
3155*4882a593Smuzhiyun  * route cannot be done from atomit context (SPI transfer takes a sleepable
3156*4882a593Smuzhiyun  * lock on the bus)
3157*4882a593Smuzhiyun  */
sja1105_port_deferred_xmit(struct kthread_work * work)3158*4882a593Smuzhiyun static void sja1105_port_deferred_xmit(struct kthread_work *work)
3159*4882a593Smuzhiyun {
3160*4882a593Smuzhiyun 	struct sja1105_port *sp = work_to_port(work);
3161*4882a593Smuzhiyun 	struct sja1105_tagger_data *tagger_data = sp->data;
3162*4882a593Smuzhiyun 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
3163*4882a593Smuzhiyun 	int port = sp - priv->ports;
3164*4882a593Smuzhiyun 	struct sk_buff *skb;
3165*4882a593Smuzhiyun 
3166*4882a593Smuzhiyun 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
3167*4882a593Smuzhiyun 		struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
3168*4882a593Smuzhiyun 
3169*4882a593Smuzhiyun 		mutex_lock(&priv->mgmt_lock);
3170*4882a593Smuzhiyun 
3171*4882a593Smuzhiyun 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
3172*4882a593Smuzhiyun 
3173*4882a593Smuzhiyun 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
3174*4882a593Smuzhiyun 		if (clone)
3175*4882a593Smuzhiyun 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
3176*4882a593Smuzhiyun 
3177*4882a593Smuzhiyun 		mutex_unlock(&priv->mgmt_lock);
3178*4882a593Smuzhiyun 	}
3179*4882a593Smuzhiyun }
3180*4882a593Smuzhiyun 
3181*4882a593Smuzhiyun /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
3182*4882a593Smuzhiyun  * which cannot be reconfigured at runtime. So a switch reset is required.
3183*4882a593Smuzhiyun  */
sja1105_set_ageing_time(struct dsa_switch * ds,unsigned int ageing_time)3184*4882a593Smuzhiyun static int sja1105_set_ageing_time(struct dsa_switch *ds,
3185*4882a593Smuzhiyun 				   unsigned int ageing_time)
3186*4882a593Smuzhiyun {
3187*4882a593Smuzhiyun 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
3188*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
3189*4882a593Smuzhiyun 	struct sja1105_table *table;
3190*4882a593Smuzhiyun 	unsigned int maxage;
3191*4882a593Smuzhiyun 
3192*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
3193*4882a593Smuzhiyun 	l2_lookup_params = table->entries;
3194*4882a593Smuzhiyun 
3195*4882a593Smuzhiyun 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
3196*4882a593Smuzhiyun 
3197*4882a593Smuzhiyun 	if (l2_lookup_params->maxage == maxage)
3198*4882a593Smuzhiyun 		return 0;
3199*4882a593Smuzhiyun 
3200*4882a593Smuzhiyun 	l2_lookup_params->maxage = maxage;
3201*4882a593Smuzhiyun 
3202*4882a593Smuzhiyun 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
3203*4882a593Smuzhiyun }
3204*4882a593Smuzhiyun 
sja1105_change_mtu(struct dsa_switch * ds,int port,int new_mtu)3205*4882a593Smuzhiyun static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
3206*4882a593Smuzhiyun {
3207*4882a593Smuzhiyun 	struct sja1105_l2_policing_entry *policing;
3208*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
3209*4882a593Smuzhiyun 
3210*4882a593Smuzhiyun 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
3211*4882a593Smuzhiyun 
3212*4882a593Smuzhiyun 	if (dsa_is_cpu_port(ds, port))
3213*4882a593Smuzhiyun 		new_mtu += VLAN_HLEN;
3214*4882a593Smuzhiyun 
3215*4882a593Smuzhiyun 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3216*4882a593Smuzhiyun 
3217*4882a593Smuzhiyun 	if (policing[port].maxlen == new_mtu)
3218*4882a593Smuzhiyun 		return 0;
3219*4882a593Smuzhiyun 
3220*4882a593Smuzhiyun 	policing[port].maxlen = new_mtu;
3221*4882a593Smuzhiyun 
3222*4882a593Smuzhiyun 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3223*4882a593Smuzhiyun }
3224*4882a593Smuzhiyun 
sja1105_get_max_mtu(struct dsa_switch * ds,int port)3225*4882a593Smuzhiyun static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
3226*4882a593Smuzhiyun {
3227*4882a593Smuzhiyun 	return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
3228*4882a593Smuzhiyun }
3229*4882a593Smuzhiyun 
sja1105_port_setup_tc(struct dsa_switch * ds,int port,enum tc_setup_type type,void * type_data)3230*4882a593Smuzhiyun static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
3231*4882a593Smuzhiyun 				 enum tc_setup_type type,
3232*4882a593Smuzhiyun 				 void *type_data)
3233*4882a593Smuzhiyun {
3234*4882a593Smuzhiyun 	switch (type) {
3235*4882a593Smuzhiyun 	case TC_SETUP_QDISC_TAPRIO:
3236*4882a593Smuzhiyun 		return sja1105_setup_tc_taprio(ds, port, type_data);
3237*4882a593Smuzhiyun 	case TC_SETUP_QDISC_CBS:
3238*4882a593Smuzhiyun 		return sja1105_setup_tc_cbs(ds, port, type_data);
3239*4882a593Smuzhiyun 	default:
3240*4882a593Smuzhiyun 		return -EOPNOTSUPP;
3241*4882a593Smuzhiyun 	}
3242*4882a593Smuzhiyun }
3243*4882a593Smuzhiyun 
3244*4882a593Smuzhiyun /* We have a single mirror (@to) port, but can configure ingress and egress
3245*4882a593Smuzhiyun  * mirroring on all other (@from) ports.
3246*4882a593Smuzhiyun  * We need to allow mirroring rules only as long as the @to port is always the
3247*4882a593Smuzhiyun  * same, and we need to unset the @to port from mirr_port only when there is no
3248*4882a593Smuzhiyun  * mirroring rule that references it.
3249*4882a593Smuzhiyun  */
sja1105_mirror_apply(struct sja1105_private * priv,int from,int to,bool ingress,bool enabled)3250*4882a593Smuzhiyun static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
3251*4882a593Smuzhiyun 				bool ingress, bool enabled)
3252*4882a593Smuzhiyun {
3253*4882a593Smuzhiyun 	struct sja1105_general_params_entry *general_params;
3254*4882a593Smuzhiyun 	struct sja1105_mac_config_entry *mac;
3255*4882a593Smuzhiyun 	struct sja1105_table *table;
3256*4882a593Smuzhiyun 	bool already_enabled;
3257*4882a593Smuzhiyun 	u64 new_mirr_port;
3258*4882a593Smuzhiyun 	int rc;
3259*4882a593Smuzhiyun 
3260*4882a593Smuzhiyun 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
3261*4882a593Smuzhiyun 	general_params = table->entries;
3262*4882a593Smuzhiyun 
3263*4882a593Smuzhiyun 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
3264*4882a593Smuzhiyun 
3265*4882a593Smuzhiyun 	already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS);
3266*4882a593Smuzhiyun 	if (already_enabled && enabled && general_params->mirr_port != to) {
3267*4882a593Smuzhiyun 		dev_err(priv->ds->dev,
3268*4882a593Smuzhiyun 			"Delete mirroring rules towards port %llu first\n",
3269*4882a593Smuzhiyun 			general_params->mirr_port);
3270*4882a593Smuzhiyun 		return -EBUSY;
3271*4882a593Smuzhiyun 	}
3272*4882a593Smuzhiyun 
3273*4882a593Smuzhiyun 	new_mirr_port = to;
3274*4882a593Smuzhiyun 	if (!enabled) {
3275*4882a593Smuzhiyun 		bool keep = false;
3276*4882a593Smuzhiyun 		int port;
3277*4882a593Smuzhiyun 
3278*4882a593Smuzhiyun 		/* Anybody still referencing mirr_port? */
3279*4882a593Smuzhiyun 		for (port = 0; port < SJA1105_NUM_PORTS; port++) {
3280*4882a593Smuzhiyun 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
3281*4882a593Smuzhiyun 				keep = true;
3282*4882a593Smuzhiyun 				break;
3283*4882a593Smuzhiyun 			}
3284*4882a593Smuzhiyun 		}
3285*4882a593Smuzhiyun 		/* Unset already_enabled for next time */
3286*4882a593Smuzhiyun 		if (!keep)
3287*4882a593Smuzhiyun 			new_mirr_port = SJA1105_NUM_PORTS;
3288*4882a593Smuzhiyun 	}
3289*4882a593Smuzhiyun 	if (new_mirr_port != general_params->mirr_port) {
3290*4882a593Smuzhiyun 		general_params->mirr_port = new_mirr_port;
3291*4882a593Smuzhiyun 
3292*4882a593Smuzhiyun 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
3293*4882a593Smuzhiyun 						  0, general_params, true);
3294*4882a593Smuzhiyun 		if (rc < 0)
3295*4882a593Smuzhiyun 			return rc;
3296*4882a593Smuzhiyun 	}
3297*4882a593Smuzhiyun 
3298*4882a593Smuzhiyun 	if (ingress)
3299*4882a593Smuzhiyun 		mac[from].ing_mirr = enabled;
3300*4882a593Smuzhiyun 	else
3301*4882a593Smuzhiyun 		mac[from].egr_mirr = enabled;
3302*4882a593Smuzhiyun 
3303*4882a593Smuzhiyun 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
3304*4882a593Smuzhiyun 					    &mac[from], true);
3305*4882a593Smuzhiyun }
3306*4882a593Smuzhiyun 
sja1105_mirror_add(struct dsa_switch * ds,int port,struct dsa_mall_mirror_tc_entry * mirror,bool ingress)3307*4882a593Smuzhiyun static int sja1105_mirror_add(struct dsa_switch *ds, int port,
3308*4882a593Smuzhiyun 			      struct dsa_mall_mirror_tc_entry *mirror,
3309*4882a593Smuzhiyun 			      bool ingress)
3310*4882a593Smuzhiyun {
3311*4882a593Smuzhiyun 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3312*4882a593Smuzhiyun 				    ingress, true);
3313*4882a593Smuzhiyun }
3314*4882a593Smuzhiyun 
sja1105_mirror_del(struct dsa_switch * ds,int port,struct dsa_mall_mirror_tc_entry * mirror)3315*4882a593Smuzhiyun static void sja1105_mirror_del(struct dsa_switch *ds, int port,
3316*4882a593Smuzhiyun 			       struct dsa_mall_mirror_tc_entry *mirror)
3317*4882a593Smuzhiyun {
3318*4882a593Smuzhiyun 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3319*4882a593Smuzhiyun 			     mirror->ingress, false);
3320*4882a593Smuzhiyun }
3321*4882a593Smuzhiyun 
sja1105_port_policer_add(struct dsa_switch * ds,int port,struct dsa_mall_policer_tc_entry * policer)3322*4882a593Smuzhiyun static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
3323*4882a593Smuzhiyun 				    struct dsa_mall_policer_tc_entry *policer)
3324*4882a593Smuzhiyun {
3325*4882a593Smuzhiyun 	struct sja1105_l2_policing_entry *policing;
3326*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
3327*4882a593Smuzhiyun 
3328*4882a593Smuzhiyun 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3329*4882a593Smuzhiyun 
3330*4882a593Smuzhiyun 	/* In hardware, every 8 microseconds the credit level is incremented by
3331*4882a593Smuzhiyun 	 * the value of RATE bytes divided by 64, up to a maximum of SMAX
3332*4882a593Smuzhiyun 	 * bytes.
3333*4882a593Smuzhiyun 	 */
3334*4882a593Smuzhiyun 	policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
3335*4882a593Smuzhiyun 				      1000000);
3336*4882a593Smuzhiyun 	policing[port].smax = policer->burst;
3337*4882a593Smuzhiyun 
3338*4882a593Smuzhiyun 	return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3339*4882a593Smuzhiyun }
3340*4882a593Smuzhiyun 
sja1105_port_policer_del(struct dsa_switch * ds,int port)3341*4882a593Smuzhiyun static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
3342*4882a593Smuzhiyun {
3343*4882a593Smuzhiyun 	struct sja1105_l2_policing_entry *policing;
3344*4882a593Smuzhiyun 	struct sja1105_private *priv = ds->priv;
3345*4882a593Smuzhiyun 
3346*4882a593Smuzhiyun 	policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3347*4882a593Smuzhiyun 
3348*4882a593Smuzhiyun 	policing[port].rate = SJA1105_RATE_MBPS(1000);
3349*4882a593Smuzhiyun 	policing[port].smax = 65535;
3350*4882a593Smuzhiyun 
3351*4882a593Smuzhiyun 	sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3352*4882a593Smuzhiyun }
3353*4882a593Smuzhiyun 
3354*4882a593Smuzhiyun static const struct dsa_switch_ops sja1105_switch_ops = {
3355*4882a593Smuzhiyun 	.get_tag_protocol	= sja1105_get_tag_protocol,
3356*4882a593Smuzhiyun 	.setup			= sja1105_setup,
3357*4882a593Smuzhiyun 	.teardown		= sja1105_teardown,
3358*4882a593Smuzhiyun 	.set_ageing_time	= sja1105_set_ageing_time,
3359*4882a593Smuzhiyun 	.port_change_mtu	= sja1105_change_mtu,
3360*4882a593Smuzhiyun 	.port_max_mtu		= sja1105_get_max_mtu,
3361*4882a593Smuzhiyun 	.phylink_validate	= sja1105_phylink_validate,
3362*4882a593Smuzhiyun 	.phylink_mac_link_state	= sja1105_mac_pcs_get_state,
3363*4882a593Smuzhiyun 	.phylink_mac_config	= sja1105_mac_config,
3364*4882a593Smuzhiyun 	.phylink_mac_link_up	= sja1105_mac_link_up,
3365*4882a593Smuzhiyun 	.phylink_mac_link_down	= sja1105_mac_link_down,
3366*4882a593Smuzhiyun 	.get_strings		= sja1105_get_strings,
3367*4882a593Smuzhiyun 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
3368*4882a593Smuzhiyun 	.get_sset_count		= sja1105_get_sset_count,
3369*4882a593Smuzhiyun 	.get_ts_info		= sja1105_get_ts_info,
3370*4882a593Smuzhiyun 	.port_enable		= sja1105_port_enable,
3371*4882a593Smuzhiyun 	.port_disable		= sja1105_port_disable,
3372*4882a593Smuzhiyun 	.port_fdb_dump		= sja1105_fdb_dump,
3373*4882a593Smuzhiyun 	.port_fdb_add		= sja1105_fdb_add,
3374*4882a593Smuzhiyun 	.port_fdb_del		= sja1105_fdb_del,
3375*4882a593Smuzhiyun 	.port_bridge_join	= sja1105_bridge_join,
3376*4882a593Smuzhiyun 	.port_bridge_leave	= sja1105_bridge_leave,
3377*4882a593Smuzhiyun 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
3378*4882a593Smuzhiyun 	.port_vlan_prepare	= sja1105_vlan_prepare,
3379*4882a593Smuzhiyun 	.port_vlan_filtering	= sja1105_vlan_filtering,
3380*4882a593Smuzhiyun 	.port_vlan_add		= sja1105_vlan_add,
3381*4882a593Smuzhiyun 	.port_vlan_del		= sja1105_vlan_del,
3382*4882a593Smuzhiyun 	.port_mdb_prepare	= sja1105_mdb_prepare,
3383*4882a593Smuzhiyun 	.port_mdb_add		= sja1105_mdb_add,
3384*4882a593Smuzhiyun 	.port_mdb_del		= sja1105_mdb_del,
3385*4882a593Smuzhiyun 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
3386*4882a593Smuzhiyun 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
3387*4882a593Smuzhiyun 	.port_rxtstamp		= sja1105_port_rxtstamp,
3388*4882a593Smuzhiyun 	.port_txtstamp		= sja1105_port_txtstamp,
3389*4882a593Smuzhiyun 	.port_setup_tc		= sja1105_port_setup_tc,
3390*4882a593Smuzhiyun 	.port_mirror_add	= sja1105_mirror_add,
3391*4882a593Smuzhiyun 	.port_mirror_del	= sja1105_mirror_del,
3392*4882a593Smuzhiyun 	.port_policer_add	= sja1105_port_policer_add,
3393*4882a593Smuzhiyun 	.port_policer_del	= sja1105_port_policer_del,
3394*4882a593Smuzhiyun 	.cls_flower_add		= sja1105_cls_flower_add,
3395*4882a593Smuzhiyun 	.cls_flower_del		= sja1105_cls_flower_del,
3396*4882a593Smuzhiyun 	.cls_flower_stats	= sja1105_cls_flower_stats,
3397*4882a593Smuzhiyun 	.crosschip_bridge_join	= sja1105_crosschip_bridge_join,
3398*4882a593Smuzhiyun 	.crosschip_bridge_leave	= sja1105_crosschip_bridge_leave,
3399*4882a593Smuzhiyun 	.devlink_param_get	= sja1105_devlink_param_get,
3400*4882a593Smuzhiyun 	.devlink_param_set	= sja1105_devlink_param_set,
3401*4882a593Smuzhiyun 	.devlink_info_get	= sja1105_devlink_info_get,
3402*4882a593Smuzhiyun };
3403*4882a593Smuzhiyun 
3404*4882a593Smuzhiyun static const struct of_device_id sja1105_dt_ids[];
3405*4882a593Smuzhiyun 
sja1105_check_device_id(struct sja1105_private * priv)3406*4882a593Smuzhiyun static int sja1105_check_device_id(struct sja1105_private *priv)
3407*4882a593Smuzhiyun {
3408*4882a593Smuzhiyun 	const struct sja1105_regs *regs = priv->info->regs;
3409*4882a593Smuzhiyun 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
3410*4882a593Smuzhiyun 	struct device *dev = &priv->spidev->dev;
3411*4882a593Smuzhiyun 	const struct of_device_id *match;
3412*4882a593Smuzhiyun 	u32 device_id;
3413*4882a593Smuzhiyun 	u64 part_no;
3414*4882a593Smuzhiyun 	int rc;
3415*4882a593Smuzhiyun 
3416*4882a593Smuzhiyun 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
3417*4882a593Smuzhiyun 			      NULL);
3418*4882a593Smuzhiyun 	if (rc < 0)
3419*4882a593Smuzhiyun 		return rc;
3420*4882a593Smuzhiyun 
3421*4882a593Smuzhiyun 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
3422*4882a593Smuzhiyun 			      SJA1105_SIZE_DEVICE_ID);
3423*4882a593Smuzhiyun 	if (rc < 0)
3424*4882a593Smuzhiyun 		return rc;
3425*4882a593Smuzhiyun 
3426*4882a593Smuzhiyun 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
3427*4882a593Smuzhiyun 
3428*4882a593Smuzhiyun 	for (match = sja1105_dt_ids; match->compatible[0]; match++) {
3429*4882a593Smuzhiyun 		const struct sja1105_info *info = match->data;
3430*4882a593Smuzhiyun 
3431*4882a593Smuzhiyun 		/* Is what's been probed in our match table at all? */
3432*4882a593Smuzhiyun 		if (info->device_id != device_id || info->part_no != part_no)
3433*4882a593Smuzhiyun 			continue;
3434*4882a593Smuzhiyun 
3435*4882a593Smuzhiyun 		/* But is it what's in the device tree? */
3436*4882a593Smuzhiyun 		if (priv->info->device_id != device_id ||
3437*4882a593Smuzhiyun 		    priv->info->part_no != part_no) {
3438*4882a593Smuzhiyun 			dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
3439*4882a593Smuzhiyun 				 priv->info->name, info->name);
3440*4882a593Smuzhiyun 			/* It isn't. No problem, pick that up. */
3441*4882a593Smuzhiyun 			priv->info = info;
3442*4882a593Smuzhiyun 		}
3443*4882a593Smuzhiyun 
3444*4882a593Smuzhiyun 		return 0;
3445*4882a593Smuzhiyun 	}
3446*4882a593Smuzhiyun 
3447*4882a593Smuzhiyun 	dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
3448*4882a593Smuzhiyun 		device_id, part_no);
3449*4882a593Smuzhiyun 
3450*4882a593Smuzhiyun 	return -ENODEV;
3451*4882a593Smuzhiyun }
3452*4882a593Smuzhiyun 
sja1105_probe(struct spi_device * spi)3453*4882a593Smuzhiyun static int sja1105_probe(struct spi_device *spi)
3454*4882a593Smuzhiyun {
3455*4882a593Smuzhiyun 	struct sja1105_tagger_data *tagger_data;
3456*4882a593Smuzhiyun 	struct device *dev = &spi->dev;
3457*4882a593Smuzhiyun 	struct sja1105_private *priv;
3458*4882a593Smuzhiyun 	struct dsa_switch *ds;
3459*4882a593Smuzhiyun 	int rc, port;
3460*4882a593Smuzhiyun 
3461*4882a593Smuzhiyun 	if (!dev->of_node) {
3462*4882a593Smuzhiyun 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
3463*4882a593Smuzhiyun 		return -EINVAL;
3464*4882a593Smuzhiyun 	}
3465*4882a593Smuzhiyun 
3466*4882a593Smuzhiyun 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
3467*4882a593Smuzhiyun 	if (!priv)
3468*4882a593Smuzhiyun 		return -ENOMEM;
3469*4882a593Smuzhiyun 
3470*4882a593Smuzhiyun 	/* Configure the optional reset pin and bring up switch */
3471*4882a593Smuzhiyun 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
3472*4882a593Smuzhiyun 	if (IS_ERR(priv->reset_gpio))
3473*4882a593Smuzhiyun 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
3474*4882a593Smuzhiyun 	else
3475*4882a593Smuzhiyun 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
3476*4882a593Smuzhiyun 
3477*4882a593Smuzhiyun 	/* Populate our driver private structure (priv) based on
3478*4882a593Smuzhiyun 	 * the device tree node that was probed (spi)
3479*4882a593Smuzhiyun 	 */
3480*4882a593Smuzhiyun 	priv->spidev = spi;
3481*4882a593Smuzhiyun 	spi_set_drvdata(spi, priv);
3482*4882a593Smuzhiyun 
3483*4882a593Smuzhiyun 	/* Configure the SPI bus */
3484*4882a593Smuzhiyun 	spi->bits_per_word = 8;
3485*4882a593Smuzhiyun 	rc = spi_setup(spi);
3486*4882a593Smuzhiyun 	if (rc < 0) {
3487*4882a593Smuzhiyun 		dev_err(dev, "Could not init SPI\n");
3488*4882a593Smuzhiyun 		return rc;
3489*4882a593Smuzhiyun 	}
3490*4882a593Smuzhiyun 
3491*4882a593Smuzhiyun 	priv->info = of_device_get_match_data(dev);
3492*4882a593Smuzhiyun 
3493*4882a593Smuzhiyun 	/* Detect hardware device */
3494*4882a593Smuzhiyun 	rc = sja1105_check_device_id(priv);
3495*4882a593Smuzhiyun 	if (rc < 0) {
3496*4882a593Smuzhiyun 		dev_err(dev, "Device ID check failed: %d\n", rc);
3497*4882a593Smuzhiyun 		return rc;
3498*4882a593Smuzhiyun 	}
3499*4882a593Smuzhiyun 
3500*4882a593Smuzhiyun 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
3501*4882a593Smuzhiyun 
3502*4882a593Smuzhiyun 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3503*4882a593Smuzhiyun 	if (!ds)
3504*4882a593Smuzhiyun 		return -ENOMEM;
3505*4882a593Smuzhiyun 
3506*4882a593Smuzhiyun 	ds->dev = dev;
3507*4882a593Smuzhiyun 	ds->num_ports = SJA1105_NUM_PORTS;
3508*4882a593Smuzhiyun 	ds->ops = &sja1105_switch_ops;
3509*4882a593Smuzhiyun 	ds->priv = priv;
3510*4882a593Smuzhiyun 	priv->ds = ds;
3511*4882a593Smuzhiyun 
3512*4882a593Smuzhiyun 	tagger_data = &priv->tagger_data;
3513*4882a593Smuzhiyun 
3514*4882a593Smuzhiyun 	mutex_init(&priv->ptp_data.lock);
3515*4882a593Smuzhiyun 	mutex_init(&priv->mgmt_lock);
3516*4882a593Smuzhiyun 
3517*4882a593Smuzhiyun 	priv->dsa_8021q_ctx = devm_kzalloc(dev, sizeof(*priv->dsa_8021q_ctx),
3518*4882a593Smuzhiyun 					   GFP_KERNEL);
3519*4882a593Smuzhiyun 	if (!priv->dsa_8021q_ctx)
3520*4882a593Smuzhiyun 		return -ENOMEM;
3521*4882a593Smuzhiyun 
3522*4882a593Smuzhiyun 	priv->dsa_8021q_ctx->ops = &sja1105_dsa_8021q_ops;
3523*4882a593Smuzhiyun 	priv->dsa_8021q_ctx->proto = htons(ETH_P_8021Q);
3524*4882a593Smuzhiyun 	priv->dsa_8021q_ctx->ds = ds;
3525*4882a593Smuzhiyun 
3526*4882a593Smuzhiyun 	INIT_LIST_HEAD(&priv->dsa_8021q_ctx->crosschip_links);
3527*4882a593Smuzhiyun 	INIT_LIST_HEAD(&priv->bridge_vlans);
3528*4882a593Smuzhiyun 	INIT_LIST_HEAD(&priv->dsa_8021q_vlans);
3529*4882a593Smuzhiyun 
3530*4882a593Smuzhiyun 	sja1105_tas_setup(ds);
3531*4882a593Smuzhiyun 	sja1105_flower_setup(ds);
3532*4882a593Smuzhiyun 
3533*4882a593Smuzhiyun 	rc = dsa_register_switch(priv->ds);
3534*4882a593Smuzhiyun 	if (rc)
3535*4882a593Smuzhiyun 		return rc;
3536*4882a593Smuzhiyun 
3537*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
3538*4882a593Smuzhiyun 		priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
3539*4882a593Smuzhiyun 					 sizeof(struct sja1105_cbs_entry),
3540*4882a593Smuzhiyun 					 GFP_KERNEL);
3541*4882a593Smuzhiyun 		if (!priv->cbs) {
3542*4882a593Smuzhiyun 			rc = -ENOMEM;
3543*4882a593Smuzhiyun 			goto out_unregister_switch;
3544*4882a593Smuzhiyun 		}
3545*4882a593Smuzhiyun 	}
3546*4882a593Smuzhiyun 
3547*4882a593Smuzhiyun 	/* Connections between dsa_port and sja1105_port */
3548*4882a593Smuzhiyun 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
3549*4882a593Smuzhiyun 		struct sja1105_port *sp = &priv->ports[port];
3550*4882a593Smuzhiyun 		struct dsa_port *dp = dsa_to_port(ds, port);
3551*4882a593Smuzhiyun 		struct net_device *slave;
3552*4882a593Smuzhiyun 		int subvlan;
3553*4882a593Smuzhiyun 
3554*4882a593Smuzhiyun 		if (!dsa_is_user_port(ds, port))
3555*4882a593Smuzhiyun 			continue;
3556*4882a593Smuzhiyun 
3557*4882a593Smuzhiyun 		dp->priv = sp;
3558*4882a593Smuzhiyun 		sp->dp = dp;
3559*4882a593Smuzhiyun 		sp->data = tagger_data;
3560*4882a593Smuzhiyun 		slave = dp->slave;
3561*4882a593Smuzhiyun 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
3562*4882a593Smuzhiyun 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
3563*4882a593Smuzhiyun 							slave->name);
3564*4882a593Smuzhiyun 		if (IS_ERR(sp->xmit_worker)) {
3565*4882a593Smuzhiyun 			rc = PTR_ERR(sp->xmit_worker);
3566*4882a593Smuzhiyun 			dev_err(ds->dev,
3567*4882a593Smuzhiyun 				"failed to create deferred xmit thread: %d\n",
3568*4882a593Smuzhiyun 				rc);
3569*4882a593Smuzhiyun 			goto out_destroy_workers;
3570*4882a593Smuzhiyun 		}
3571*4882a593Smuzhiyun 		skb_queue_head_init(&sp->xmit_queue);
3572*4882a593Smuzhiyun 		sp->xmit_tpid = ETH_P_SJA1105;
3573*4882a593Smuzhiyun 
3574*4882a593Smuzhiyun 		for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
3575*4882a593Smuzhiyun 			sp->subvlan_map[subvlan] = VLAN_N_VID;
3576*4882a593Smuzhiyun 	}
3577*4882a593Smuzhiyun 
3578*4882a593Smuzhiyun 	return 0;
3579*4882a593Smuzhiyun 
3580*4882a593Smuzhiyun out_destroy_workers:
3581*4882a593Smuzhiyun 	while (port-- > 0) {
3582*4882a593Smuzhiyun 		struct sja1105_port *sp = &priv->ports[port];
3583*4882a593Smuzhiyun 
3584*4882a593Smuzhiyun 		if (!dsa_is_user_port(ds, port))
3585*4882a593Smuzhiyun 			continue;
3586*4882a593Smuzhiyun 
3587*4882a593Smuzhiyun 		kthread_destroy_worker(sp->xmit_worker);
3588*4882a593Smuzhiyun 	}
3589*4882a593Smuzhiyun 
3590*4882a593Smuzhiyun out_unregister_switch:
3591*4882a593Smuzhiyun 	dsa_unregister_switch(ds);
3592*4882a593Smuzhiyun 
3593*4882a593Smuzhiyun 	return rc;
3594*4882a593Smuzhiyun }
3595*4882a593Smuzhiyun 
sja1105_remove(struct spi_device * spi)3596*4882a593Smuzhiyun static int sja1105_remove(struct spi_device *spi)
3597*4882a593Smuzhiyun {
3598*4882a593Smuzhiyun 	struct sja1105_private *priv = spi_get_drvdata(spi);
3599*4882a593Smuzhiyun 
3600*4882a593Smuzhiyun 	dsa_unregister_switch(priv->ds);
3601*4882a593Smuzhiyun 	return 0;
3602*4882a593Smuzhiyun }
3603*4882a593Smuzhiyun 
3604*4882a593Smuzhiyun static const struct of_device_id sja1105_dt_ids[] = {
3605*4882a593Smuzhiyun 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
3606*4882a593Smuzhiyun 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
3607*4882a593Smuzhiyun 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
3608*4882a593Smuzhiyun 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
3609*4882a593Smuzhiyun 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
3610*4882a593Smuzhiyun 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
3611*4882a593Smuzhiyun 	{ /* sentinel */ },
3612*4882a593Smuzhiyun };
3613*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
3614*4882a593Smuzhiyun 
3615*4882a593Smuzhiyun static struct spi_driver sja1105_driver = {
3616*4882a593Smuzhiyun 	.driver = {
3617*4882a593Smuzhiyun 		.name  = "sja1105",
3618*4882a593Smuzhiyun 		.owner = THIS_MODULE,
3619*4882a593Smuzhiyun 		.of_match_table = of_match_ptr(sja1105_dt_ids),
3620*4882a593Smuzhiyun 	},
3621*4882a593Smuzhiyun 	.probe  = sja1105_probe,
3622*4882a593Smuzhiyun 	.remove = sja1105_remove,
3623*4882a593Smuzhiyun };
3624*4882a593Smuzhiyun 
3625*4882a593Smuzhiyun module_spi_driver(sja1105_driver);
3626*4882a593Smuzhiyun 
3627*4882a593Smuzhiyun MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
3628*4882a593Smuzhiyun MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
3629*4882a593Smuzhiyun MODULE_DESCRIPTION("SJA1105 Driver");
3630*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
3631