blob: afafe2ecf248c05e17fff521e9e67574d357a599 [file] [log] [blame]
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03001// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
3 * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
4 */
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8#include <linux/delay.h>
9#include <linux/module.h>
10#include <linux/printk.h>
11#include <linux/spi/spi.h>
12#include <linux/errno.h>
13#include <linux/gpio/consumer.h>
Vladimir Olteanad9f2992019-05-02 23:23:38 +030014#include <linux/phylink.h>
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +030015#include <linux/of.h>
16#include <linux/of_net.h>
17#include <linux/of_mdio.h>
18#include <linux/of_device.h>
19#include <linux/netdev_features.h>
20#include <linux/netdevice.h>
21#include <linux/if_bridge.h>
22#include <linux/if_ether.h>
Vladimir Oltean227d07a2019-05-05 13:19:27 +030023#include <linux/dsa/8021q.h>
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +030024#include "sja1105.h"
Vladimir Olteanffe10e62020-03-20 13:29:37 +020025#include "sja1105_sgmii.h"
Vladimir Oltean317ab5b2019-09-15 05:00:02 +030026#include "sja1105_tas.h"
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +030027
28static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
29 unsigned int startup_delay)
30{
31 gpiod_set_value_cansleep(gpio, 1);
32 /* Wait for minimum reset pulse length */
33 msleep(pulse_len);
34 gpiod_set_value_cansleep(gpio, 0);
35 /* Wait until chip is ready after reset */
36 msleep(startup_delay);
37}
38
39static void
40sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
41 int from, int to, bool allow)
42{
43 if (allow) {
44 l2_fwd[from].bc_domain |= BIT(to);
45 l2_fwd[from].reach_port |= BIT(to);
46 l2_fwd[from].fl_domain |= BIT(to);
47 } else {
48 l2_fwd[from].bc_domain &= ~BIT(to);
49 l2_fwd[from].reach_port &= ~BIT(to);
50 l2_fwd[from].fl_domain &= ~BIT(to);
51 }
52}
53
54/* Structure used to temporarily transport device tree
55 * settings into sja1105_setup
56 */
57struct sja1105_dt_port {
58 phy_interface_t phy_mode;
59 sja1105_mii_role_t role;
60};
61
62static int sja1105_init_mac_settings(struct sja1105_private *priv)
63{
64 struct sja1105_mac_config_entry default_mac = {
65 /* Enable all 8 priority queues on egress.
66 * Every queue i holds top[i] - base[i] frames.
67 * Sum of top[i] - base[i] is 511 (max hardware limit).
68 */
69 .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
70 .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
71 .enabled = {true, true, true, true, true, true, true, true},
72 /* Keep standard IFG of 12 bytes on egress. */
73 .ifg = 0,
74 /* Always put the MAC speed in automatic mode, where it can be
Vladimir Oltean1fd4a172019-06-08 16:03:42 +030075 * adjusted at runtime by PHYLINK.
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +030076 */
77 .speed = SJA1105_SPEED_AUTO,
78 /* No static correction for 1-step 1588 events */
79 .tp_delin = 0,
80 .tp_delout = 0,
81 /* Disable aging for critical TTEthernet traffic */
82 .maxage = 0xFF,
83 /* Internal VLAN (pvid) to apply to untagged ingress */
84 .vlanprio = 0,
Vladimir Olteane3502b82019-06-26 02:39:35 +030085 .vlanid = 1,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +030086 .ing_mirr = false,
87 .egr_mirr = false,
88 /* Don't drop traffic with other EtherType than ETH_P_IP */
89 .drpnona664 = false,
90 /* Don't drop double-tagged traffic */
91 .drpdtag = false,
92 /* Don't drop untagged traffic */
93 .drpuntag = false,
94 /* Don't retag 802.1p (VID 0) traffic with the pvid */
95 .retag = false,
Vladimir Oltean640f7632019-05-05 13:19:28 +030096 /* Disable learning and I/O on user ports by default -
97 * STP will enable it.
98 */
99 .dyn_learn = false,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300100 .egress = false,
101 .ingress = false,
102 };
103 struct sja1105_mac_config_entry *mac;
104 struct sja1105_table *table;
105 int i;
106
107 table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
108
109 /* Discard previous MAC Configuration Table */
110 if (table->entry_count) {
111 kfree(table->entries);
112 table->entry_count = 0;
113 }
114
115 table->entries = kcalloc(SJA1105_NUM_PORTS,
116 table->ops->unpacked_entry_size, GFP_KERNEL);
117 if (!table->entries)
118 return -ENOMEM;
119
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300120 table->entry_count = SJA1105_NUM_PORTS;
121
122 mac = table->entries;
123
Vladimir Oltean640f7632019-05-05 13:19:28 +0300124 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300125 mac[i] = default_mac;
Vladimir Oltean640f7632019-05-05 13:19:28 +0300126 if (i == dsa_upstream_port(priv->ds, i)) {
127 /* STP doesn't get called for CPU port, so we need to
128 * set the I/O parameters statically.
129 */
130 mac[i].dyn_learn = true;
131 mac[i].ingress = true;
132 mac[i].egress = true;
133 }
134 }
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300135
136 return 0;
137}
138
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200139static bool sja1105_supports_sgmii(struct sja1105_private *priv, int port)
140{
141 if (priv->info->part_no != SJA1105R_PART_NO &&
142 priv->info->part_no != SJA1105S_PART_NO)
143 return false;
144
145 if (port != SJA1105_SGMII_PORT)
146 return false;
147
148 if (dsa_is_unused_port(priv->ds, port))
149 return false;
150
151 return true;
152}
153
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300154static int sja1105_init_mii_settings(struct sja1105_private *priv,
155 struct sja1105_dt_port *ports)
156{
157 struct device *dev = &priv->spidev->dev;
158 struct sja1105_xmii_params_entry *mii;
159 struct sja1105_table *table;
160 int i;
161
162 table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
163
164 /* Discard previous xMII Mode Parameters Table */
165 if (table->entry_count) {
166 kfree(table->entries);
167 table->entry_count = 0;
168 }
169
170 table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
171 table->ops->unpacked_entry_size, GFP_KERNEL);
172 if (!table->entries)
173 return -ENOMEM;
174
Vladimir Oltean1fd4a172019-06-08 16:03:42 +0300175 /* Override table based on PHYLINK DT bindings */
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300176 table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
177
178 mii = table->entries;
179
180 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
Vladimir Olteanee9d0cb2020-03-19 22:12:10 +0200181 if (dsa_is_unused_port(priv->ds, i))
182 continue;
183
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300184 switch (ports[i].phy_mode) {
185 case PHY_INTERFACE_MODE_MII:
186 mii->xmii_mode[i] = XMII_MODE_MII;
187 break;
188 case PHY_INTERFACE_MODE_RMII:
189 mii->xmii_mode[i] = XMII_MODE_RMII;
190 break;
191 case PHY_INTERFACE_MODE_RGMII:
192 case PHY_INTERFACE_MODE_RGMII_ID:
193 case PHY_INTERFACE_MODE_RGMII_RXID:
194 case PHY_INTERFACE_MODE_RGMII_TXID:
195 mii->xmii_mode[i] = XMII_MODE_RGMII;
196 break;
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200197 case PHY_INTERFACE_MODE_SGMII:
198 if (!sja1105_supports_sgmii(priv, i))
199 return -EINVAL;
200 mii->xmii_mode[i] = XMII_MODE_SGMII;
201 break;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300202 default:
203 dev_err(dev, "Unsupported PHY mode %s!\n",
204 phy_modes(ports[i].phy_mode));
205 }
206
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200207 /* Even though the SerDes port is able to drive SGMII autoneg
208 * like a PHY would, from the perspective of the XMII tables,
209 * the SGMII port should always be put in MAC mode.
210 */
211 if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII)
212 mii->phy_mac[i] = XMII_MAC;
213 else
214 mii->phy_mac[i] = ports[i].role;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300215 }
216 return 0;
217}
218
219static int sja1105_init_static_fdb(struct sja1105_private *priv)
220{
221 struct sja1105_table *table;
222
223 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
224
Vladimir Oltean291d1e72019-05-02 23:23:31 +0300225 /* We only populate the FDB table through dynamic
226 * L2 Address Lookup entries
227 */
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300228 if (table->entry_count) {
229 kfree(table->entries);
230 table->entry_count = 0;
231 }
232 return 0;
233}
234
235static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
236{
237 struct sja1105_table *table;
Vladimir Oltean6c56e162019-06-26 02:39:37 +0300238 u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300239 struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
Vladimir Oltean84567212019-05-02 23:23:36 +0300240 /* Learned FDB entries are forgotten after 300 seconds */
241 .maxage = SJA1105_AGEING_TIME_MS(300000),
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300242 /* All entries within a FDB bin are available for learning */
243 .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
Vladimir Oltean1da73822019-06-03 00:15:45 +0300244 /* And the P/Q/R/S equivalent setting: */
245 .start_dynspc = 0,
Vladimir Oltean6c56e162019-06-26 02:39:37 +0300246 .maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
247 max_fdb_entries, max_fdb_entries, },
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300248 /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
249 .poly = 0x97,
250 /* This selects between Independent VLAN Learning (IVL) and
251 * Shared VLAN Learning (SVL)
252 */
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +0300253 .shared_learn = true,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300254 /* Don't discard management traffic based on ENFPORT -
255 * we don't perform SMAC port enforcement anyway, so
256 * what we are setting here doesn't matter.
257 */
258 .no_enf_hostprt = false,
259 /* Don't learn SMAC for mac_fltres1 and mac_fltres0.
260 * Maybe correlate with no_linklocal_learn from bridge driver?
261 */
262 .no_mgmt_learn = true,
Vladimir Oltean1da73822019-06-03 00:15:45 +0300263 /* P/Q/R/S only */
264 .use_static = true,
265 /* Dynamically learned FDB entries can overwrite other (older)
266 * dynamic FDB entries
267 */
268 .owr_dyn = true,
269 .drpnolearn = true,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300270 };
271
272 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
273
274 if (table->entry_count) {
275 kfree(table->entries);
276 table->entry_count = 0;
277 }
278
279 table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
280 table->ops->unpacked_entry_size, GFP_KERNEL);
281 if (!table->entries)
282 return -ENOMEM;
283
284 table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
285
286 /* This table only has a single entry */
287 ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
288 default_l2_lookup_params;
289
290 return 0;
291}
292
293static int sja1105_init_static_vlan(struct sja1105_private *priv)
294{
295 struct sja1105_table *table;
296 struct sja1105_vlan_lookup_entry pvid = {
297 .ving_mirr = 0,
298 .vegr_mirr = 0,
299 .vmemb_port = 0,
300 .vlan_bc = 0,
301 .tag_port = 0,
Vladimir Olteane3502b82019-06-26 02:39:35 +0300302 .vlanid = 1,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300303 };
304 int i;
305
306 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
307
Vladimir Olteane3502b82019-06-26 02:39:35 +0300308 /* The static VLAN table will only contain the initial pvid of 1.
Vladimir Oltean6666ceb2019-05-02 23:23:34 +0300309 * All other VLANs are to be configured through dynamic entries,
310 * and kept in the static configuration table as backing memory.
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300311 */
312 if (table->entry_count) {
313 kfree(table->entries);
314 table->entry_count = 0;
315 }
316
317 table->entries = kcalloc(1, table->ops->unpacked_entry_size,
318 GFP_KERNEL);
319 if (!table->entries)
320 return -ENOMEM;
321
322 table->entry_count = 1;
323
Vladimir Olteane3502b82019-06-26 02:39:35 +0300324 /* VLAN 1: all DT-defined ports are members; no restrictions on
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300325 * forwarding; always transmit priority-tagged frames as untagged.
326 */
327 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
328 pvid.vmemb_port |= BIT(i);
329 pvid.vlan_bc |= BIT(i);
330 pvid.tag_port &= ~BIT(i);
331 }
332
333 ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
334 return 0;
335}
336
337static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
338{
339 struct sja1105_l2_forwarding_entry *l2fwd;
340 struct sja1105_table *table;
341 int i, j;
342
343 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
344
345 if (table->entry_count) {
346 kfree(table->entries);
347 table->entry_count = 0;
348 }
349
350 table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
351 table->ops->unpacked_entry_size, GFP_KERNEL);
352 if (!table->entries)
353 return -ENOMEM;
354
355 table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
356
357 l2fwd = table->entries;
358
359 /* First 5 entries define the forwarding rules */
360 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
361 unsigned int upstream = dsa_upstream_port(priv->ds, i);
362
363 for (j = 0; j < SJA1105_NUM_TC; j++)
364 l2fwd[i].vlan_pmap[j] = j;
365
366 if (i == upstream)
367 continue;
368
369 sja1105_port_allow_traffic(l2fwd, i, upstream, true);
370 sja1105_port_allow_traffic(l2fwd, upstream, i, true);
371 }
372 /* Next 8 entries define VLAN PCP mapping from ingress to egress.
373 * Create a one-to-one mapping.
374 */
375 for (i = 0; i < SJA1105_NUM_TC; i++)
376 for (j = 0; j < SJA1105_NUM_PORTS; j++)
377 l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
378
379 return 0;
380}
381
382static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
383{
384 struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
385 /* Disallow dynamic reconfiguration of vlan_pmap */
386 .max_dynp = 0,
387 /* Use a single memory partition for all ingress queues */
388 .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
389 };
390 struct sja1105_table *table;
391
392 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
393
394 if (table->entry_count) {
395 kfree(table->entries);
396 table->entry_count = 0;
397 }
398
399 table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
400 table->ops->unpacked_entry_size, GFP_KERNEL);
401 if (!table->entries)
402 return -ENOMEM;
403
404 table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
405
406 /* This table only has a single entry */
407 ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
408 default_l2fwd_params;
409
410 return 0;
411}
412
413static int sja1105_init_general_params(struct sja1105_private *priv)
414{
415 struct sja1105_general_params_entry default_general_params = {
Vladimir Oltean511e6ca2019-10-04 03:33:47 +0300416 /* Allow dynamic changing of the mirror port */
417 .mirr_ptacu = true,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300418 .switchid = priv->ds->index,
Vladimir Oltean5f06c632019-09-15 05:00:01 +0300419 /* Priority queue for link-local management frames
420 * (both ingress to and egress from CPU - PTP, STP etc)
421 */
Vladimir Oltean08fde092019-06-08 15:04:41 +0300422 .hostprio = 7,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300423 .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
424 .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK,
Vladimir Oltean42824462019-06-08 15:04:32 +0300425 .incl_srcpt1 = false,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300426 .send_meta1 = false,
427 .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
428 .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK,
Vladimir Oltean42824462019-06-08 15:04:32 +0300429 .incl_srcpt0 = false,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300430 .send_meta0 = false,
431 /* The destination for traffic matching mac_fltres1 and
432 * mac_fltres0 on all ports except host_port. Such traffic
433 * receieved on host_port itself would be dropped, except
434 * by installing a temporary 'management route'
435 */
436 .host_port = dsa_upstream_port(priv->ds, 0),
Vladimir Oltean511e6ca2019-10-04 03:33:47 +0300437 /* Default to an invalid value */
438 .mirr_port = SJA1105_NUM_PORTS,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300439 /* Link-local traffic received on casc_port will be forwarded
440 * to host_port without embedding the source port and device ID
441 * info in the destination MAC address (presumably because it
442 * is a cascaded port and a downstream SJA switch already did
443 * that). Default to an invalid port (to disable the feature)
444 * and overwrite this if we find any DSA (cascaded) ports.
445 */
446 .casc_port = SJA1105_NUM_PORTS,
447 /* No TTEthernet */
448 .vllupformat = 0,
449 .vlmarker = 0,
450 .vlmask = 0,
451 /* Only update correctionField for 1-step PTP (L2 transport) */
452 .ignore2stf = 0,
Vladimir Oltean6666ceb2019-05-02 23:23:34 +0300453 /* Forcefully disable VLAN filtering by telling
454 * the switch that VLAN has a different EtherType.
455 */
456 .tpid = ETH_P_SJA1105,
457 .tpid2 = ETH_P_SJA1105,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300458 };
459 struct sja1105_table *table;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300460
461 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
462
463 if (table->entry_count) {
464 kfree(table->entries);
465 table->entry_count = 0;
466 }
467
468 table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
469 table->ops->unpacked_entry_size, GFP_KERNEL);
470 if (!table->entries)
471 return -ENOMEM;
472
473 table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
474
475 /* This table only has a single entry */
476 ((struct sja1105_general_params_entry *)table->entries)[0] =
477 default_general_params;
478
479 return 0;
480}
481
482#define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
483
Vladimir Oltean09c1b412019-10-01 22:17:59 +0300484static void sja1105_setup_policer(struct sja1105_l2_policing_entry *policing,
485 int index)
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300486{
487 policing[index].sharindx = index;
488 policing[index].smax = 65535; /* Burst size in bytes */
489 policing[index].rate = SJA1105_RATE_MBPS(1000);
490 policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
491 policing[index].partition = 0;
492}
493
494static int sja1105_init_l2_policing(struct sja1105_private *priv)
495{
496 struct sja1105_l2_policing_entry *policing;
497 struct sja1105_table *table;
498 int i, j, k;
499
500 table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
501
502 /* Discard previous L2 Policing Table */
503 if (table->entry_count) {
504 kfree(table->entries);
505 table->entry_count = 0;
506 }
507
508 table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
509 table->ops->unpacked_entry_size, GFP_KERNEL);
510 if (!table->entries)
511 return -ENOMEM;
512
513 table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
514
515 policing = table->entries;
516
517 /* k sweeps through all unicast policers (0-39).
518 * bcast sweeps through policers 40-44.
519 */
520 for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) {
521 int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i;
522
523 for (j = 0; j < SJA1105_NUM_TC; j++, k++)
524 sja1105_setup_policer(policing, k);
525
526 /* Set up this port's policer for broadcast traffic */
527 sja1105_setup_policer(policing, bcast);
528 }
529 return 0;
530}
531
532static int sja1105_static_config_load(struct sja1105_private *priv,
533 struct sja1105_dt_port *ports)
534{
535 int rc;
536
537 sja1105_static_config_free(&priv->static_config);
538 rc = sja1105_static_config_init(&priv->static_config,
539 priv->info->static_ops,
540 priv->info->device_id);
541 if (rc)
542 return rc;
543
544 /* Build static configuration */
545 rc = sja1105_init_mac_settings(priv);
546 if (rc < 0)
547 return rc;
548 rc = sja1105_init_mii_settings(priv, ports);
549 if (rc < 0)
550 return rc;
551 rc = sja1105_init_static_fdb(priv);
552 if (rc < 0)
553 return rc;
554 rc = sja1105_init_static_vlan(priv);
555 if (rc < 0)
556 return rc;
557 rc = sja1105_init_l2_lookup_params(priv);
558 if (rc < 0)
559 return rc;
560 rc = sja1105_init_l2_forwarding(priv);
561 if (rc < 0)
562 return rc;
563 rc = sja1105_init_l2_forwarding_params(priv);
564 if (rc < 0)
565 return rc;
566 rc = sja1105_init_l2_policing(priv);
567 if (rc < 0)
568 return rc;
569 rc = sja1105_init_general_params(priv);
570 if (rc < 0)
571 return rc;
572
573 /* Send initial configuration to hardware via SPI */
574 return sja1105_static_config_upload(priv);
575}
576
Vladimir Olteanf5b86312019-05-02 23:23:32 +0300577static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
578 const struct sja1105_dt_port *ports)
579{
580 int i;
581
582 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
Oleksij Rempel9bca3a02019-11-25 12:43:51 +0100583 if (ports[i].role == XMII_MAC)
Vladimir Olteanf5b86312019-05-02 23:23:32 +0300584 continue;
585
Oleksij Rempel9bca3a02019-11-25 12:43:51 +0100586 if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
587 ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
Vladimir Olteanf5b86312019-05-02 23:23:32 +0300588 priv->rgmii_rx_delay[i] = true;
589
Oleksij Rempel9bca3a02019-11-25 12:43:51 +0100590 if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
591 ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
Vladimir Olteanf5b86312019-05-02 23:23:32 +0300592 priv->rgmii_tx_delay[i] = true;
593
594 if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
595 !priv->info->setup_rgmii_delay)
596 return -EINVAL;
597 }
598 return 0;
599}
600
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300601static int sja1105_parse_ports_node(struct sja1105_private *priv,
602 struct sja1105_dt_port *ports,
603 struct device_node *ports_node)
604{
605 struct device *dev = &priv->spidev->dev;
606 struct device_node *child;
607
Vladimir Oltean27afe0d2020-01-16 20:43:27 +0200608 for_each_available_child_of_node(ports_node, child) {
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300609 struct device_node *phy_node;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100610 phy_interface_t phy_mode;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300611 u32 index;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100612 int err;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300613
614 /* Get switch port number from DT */
615 if (of_property_read_u32(child, "reg", &index) < 0) {
616 dev_err(dev, "Port number not defined in device tree "
617 "(property \"reg\")\n");
Nishka Dasgupta7ba771e2019-07-23 16:14:48 +0530618 of_node_put(child);
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300619 return -ENODEV;
620 }
621
622 /* Get PHY mode from DT */
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100623 err = of_get_phy_mode(child, &phy_mode);
624 if (err) {
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300625 dev_err(dev, "Failed to read phy-mode or "
626 "phy-interface-type property for port %d\n",
627 index);
Nishka Dasgupta7ba771e2019-07-23 16:14:48 +0530628 of_node_put(child);
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300629 return -ENODEV;
630 }
631 ports[index].phy_mode = phy_mode;
632
633 phy_node = of_parse_phandle(child, "phy-handle", 0);
634 if (!phy_node) {
635 if (!of_phy_is_fixed_link(child)) {
636 dev_err(dev, "phy-handle or fixed-link "
637 "properties missing!\n");
Nishka Dasgupta7ba771e2019-07-23 16:14:48 +0530638 of_node_put(child);
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300639 return -ENODEV;
640 }
641 /* phy-handle is missing, but fixed-link isn't.
642 * So it's a fixed link. Default to PHY role.
643 */
644 ports[index].role = XMII_PHY;
645 } else {
646 /* phy-handle present => put port in MAC role */
647 ports[index].role = XMII_MAC;
648 of_node_put(phy_node);
649 }
650
651 /* The MAC/PHY role can be overridden with explicit bindings */
652 if (of_property_read_bool(child, "sja1105,role-mac"))
653 ports[index].role = XMII_MAC;
654 else if (of_property_read_bool(child, "sja1105,role-phy"))
655 ports[index].role = XMII_PHY;
656 }
657
658 return 0;
659}
660
661static int sja1105_parse_dt(struct sja1105_private *priv,
662 struct sja1105_dt_port *ports)
663{
664 struct device *dev = &priv->spidev->dev;
665 struct device_node *switch_node = dev->of_node;
666 struct device_node *ports_node;
667 int rc;
668
669 ports_node = of_get_child_by_name(switch_node, "ports");
670 if (!ports_node) {
671 dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
672 return -ENODEV;
673 }
674
675 rc = sja1105_parse_ports_node(priv, ports, ports_node);
676 of_node_put(ports_node);
677
678 return rc;
679}
680
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200681static int sja1105_sgmii_read(struct sja1105_private *priv, int pcs_reg)
682{
683 const struct sja1105_regs *regs = priv->info->regs;
684 u32 val;
685 int rc;
686
687 rc = sja1105_xfer_u32(priv, SPI_READ, regs->sgmii + pcs_reg, &val,
688 NULL);
689 if (rc < 0)
690 return rc;
691
692 return val;
693}
694
695static int sja1105_sgmii_write(struct sja1105_private *priv, int pcs_reg,
696 u16 pcs_val)
697{
698 const struct sja1105_regs *regs = priv->info->regs;
699 u32 val = pcs_val;
700 int rc;
701
702 rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->sgmii + pcs_reg, &val,
703 NULL);
704 if (rc < 0)
705 return rc;
706
707 return val;
708}
709
710static void sja1105_sgmii_pcs_config(struct sja1105_private *priv,
711 bool an_enabled, bool an_master)
712{
713 u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII;
714
715 /* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
716 * stop the clock during LPI mode, make the MAC reconfigure
717 * autonomously after PCS autoneg is done, flush the internal FIFOs.
718 */
719 sja1105_sgmii_write(priv, SJA1105_DC1, SJA1105_DC1_EN_VSMMD1 |
720 SJA1105_DC1_CLOCK_STOP_EN |
721 SJA1105_DC1_MAC_AUTO_SW |
722 SJA1105_DC1_INIT);
723 /* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
724 sja1105_sgmii_write(priv, SJA1105_DC2, SJA1105_DC2_TX_POL_INV_DISABLE);
725 /* AUTONEG_CONTROL: Use SGMII autoneg */
726 if (an_master)
727 ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK;
728 sja1105_sgmii_write(priv, SJA1105_AC, ac);
729 /* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
730 * sja1105_sgmii_pcs_force_speed must be called later for the link
731 * to become operational.
732 */
733 if (an_enabled)
734 sja1105_sgmii_write(priv, MII_BMCR,
735 BMCR_ANENABLE | BMCR_ANRESTART);
736}
737
738static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv,
739 int speed)
740{
741 int pcs_speed;
742
743 switch (speed) {
744 case SPEED_1000:
745 pcs_speed = BMCR_SPEED1000;
746 break;
747 case SPEED_100:
748 pcs_speed = BMCR_SPEED100;
749 break;
750 case SPEED_10:
751 pcs_speed = BMCR_SPEED10;
752 break;
753 default:
754 dev_err(priv->ds->dev, "Invalid speed %d\n", speed);
755 return;
756 }
757 sja1105_sgmii_write(priv, MII_BMCR, pcs_speed | BMCR_FULLDPLX);
758}
759
Vladimir Olteanc44d0532019-06-08 16:03:41 +0300760/* Convert link speed from SJA1105 to ethtool encoding */
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300761static int sja1105_speed[] = {
Vladimir Olteanc44d0532019-06-08 16:03:41 +0300762 [SJA1105_SPEED_AUTO] = SPEED_UNKNOWN,
763 [SJA1105_SPEED_10MBPS] = SPEED_10,
764 [SJA1105_SPEED_100MBPS] = SPEED_100,
765 [SJA1105_SPEED_1000MBPS] = SPEED_1000,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300766};
767
Vladimir Oltean8400cff2019-06-08 16:03:44 +0300768/* Set link speed in the MAC configuration for a specific port. */
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300769static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
Vladimir Oltean8400cff2019-06-08 16:03:44 +0300770 int speed_mbps)
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300771{
772 struct sja1105_xmii_params_entry *mii;
773 struct sja1105_mac_config_entry *mac;
774 struct device *dev = priv->ds->dev;
775 sja1105_phy_interface_t phy_mode;
776 sja1105_speed_t speed;
777 int rc;
778
Vladimir Oltean8400cff2019-06-08 16:03:44 +0300779 /* On P/Q/R/S, one can read from the device via the MAC reconfiguration
780 * tables. On E/T, MAC reconfig tables are not readable, only writable.
781 * We have to *know* what the MAC looks like. For the sake of keeping
782 * the code common, we'll use the static configuration tables as a
783 * reasonable approximation for both E/T and P/Q/R/S.
784 */
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300785 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
Vladimir Oltean8400cff2019-06-08 16:03:44 +0300786 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300787
Vladimir Olteanf4cfcfb2019-06-03 02:31:37 +0300788 switch (speed_mbps) {
Vladimir Olteanc44d0532019-06-08 16:03:41 +0300789 case SPEED_UNKNOWN:
Vladimir Olteana979a0a2019-06-28 00:46:35 +0300790 /* PHYLINK called sja1105_mac_config() to inform us about
791 * the state->interface, but AN has not completed and the
792 * speed is not yet valid. UM10944.pdf says that setting
793 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
794 * ok for power consumption in case AN will never complete -
795 * otherwise PHYLINK should come back with a new update.
796 */
Vladimir Olteanf4cfcfb2019-06-03 02:31:37 +0300797 speed = SJA1105_SPEED_AUTO;
798 break;
Vladimir Olteanc44d0532019-06-08 16:03:41 +0300799 case SPEED_10:
Vladimir Olteanf4cfcfb2019-06-03 02:31:37 +0300800 speed = SJA1105_SPEED_10MBPS;
801 break;
Vladimir Olteanc44d0532019-06-08 16:03:41 +0300802 case SPEED_100:
Vladimir Olteanf4cfcfb2019-06-03 02:31:37 +0300803 speed = SJA1105_SPEED_100MBPS;
804 break;
Vladimir Olteanc44d0532019-06-08 16:03:41 +0300805 case SPEED_1000:
Vladimir Olteanf4cfcfb2019-06-03 02:31:37 +0300806 speed = SJA1105_SPEED_1000MBPS;
807 break;
808 default:
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300809 dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
810 return -EINVAL;
811 }
812
Vladimir Oltean8400cff2019-06-08 16:03:44 +0300813 /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
814 * table, since this will be used for the clocking setup, and we no
815 * longer need to store it in the static config (already told hardware
816 * we want auto during upload phase).
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200817 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
818 * we need to configure the PCS only (if even that).
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300819 */
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200820 if (sja1105_supports_sgmii(priv, port))
821 mac[port].speed = SJA1105_SPEED_1000MBPS;
822 else
823 mac[port].speed = speed;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300824
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300825 /* Write to the dynamic reconfiguration tables */
Vladimir Oltean8400cff2019-06-08 16:03:44 +0300826 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
827 &mac[port], true);
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300828 if (rc < 0) {
829 dev_err(dev, "Failed to write MAC config: %d\n", rc);
830 return rc;
831 }
832
833 /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
834 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
835 * RMII no change of the clock setup is required. Actually, changing
836 * the clock setup does interrupt the clock signal for a certain time
837 * which causes trouble for all PHYs relying on this signal.
838 */
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300839 phy_mode = mii->xmii_mode[port];
840 if (phy_mode != XMII_MODE_RGMII)
841 return 0;
842
843 return sja1105_clocking_setup_port(priv, port);
844}
845
Vladimir Oltean39710222019-06-28 00:46:36 +0300846/* The SJA1105 MAC programming model is through the static config (the xMII
847 * Mode table cannot be dynamically reconfigured), and we have to program
848 * that early (earlier than PHYLINK calls us, anyway).
849 * So just error out in case the connected PHY attempts to change the initial
850 * system interface MII protocol from what is defined in the DT, at least for
851 * now.
852 */
853static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
854 phy_interface_t interface)
855{
856 struct sja1105_xmii_params_entry *mii;
857 sja1105_phy_interface_t phy_mode;
858
859 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
860 phy_mode = mii->xmii_mode[port];
861
862 switch (interface) {
863 case PHY_INTERFACE_MODE_MII:
864 return (phy_mode != XMII_MODE_MII);
865 case PHY_INTERFACE_MODE_RMII:
866 return (phy_mode != XMII_MODE_RMII);
867 case PHY_INTERFACE_MODE_RGMII:
868 case PHY_INTERFACE_MODE_RGMII_ID:
869 case PHY_INTERFACE_MODE_RGMII_RXID:
870 case PHY_INTERFACE_MODE_RGMII_TXID:
871 return (phy_mode != XMII_MODE_RGMII);
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200872 case PHY_INTERFACE_MODE_SGMII:
873 return (phy_mode != XMII_MODE_SGMII);
Vladimir Oltean39710222019-06-28 00:46:36 +0300874 default:
875 return true;
876 }
877}
878
Vladimir Olteanaf7cd032019-05-28 20:38:17 +0300879static void sja1105_mac_config(struct dsa_switch *ds, int port,
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200880 unsigned int mode,
Vladimir Olteanaf7cd032019-05-28 20:38:17 +0300881 const struct phylink_link_state *state)
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300882{
883 struct sja1105_private *priv = ds->priv;
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200884 bool is_sgmii = sja1105_supports_sgmii(priv, port);
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300885
Vladimir Olteanec8582d2020-03-12 12:19:51 +0000886 if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
887 dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
888 phy_modes(state->interface));
Vladimir Oltean39710222019-06-28 00:46:36 +0300889 return;
Vladimir Olteanec8582d2020-03-12 12:19:51 +0000890 }
Vladimir Oltean39710222019-06-28 00:46:36 +0300891
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200892 if (phylink_autoneg_inband(mode) && !is_sgmii) {
Vladimir Oltean9f971572019-06-28 00:46:37 +0300893 dev_err(ds->dev, "In-band AN not supported!\n");
894 return;
895 }
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200896
897 if (is_sgmii)
898 sja1105_sgmii_pcs_config(priv, phylink_autoneg_inband(mode),
899 false);
Vladimir Oltean8400cff2019-06-08 16:03:44 +0300900}
901
902static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
903 unsigned int mode,
904 phy_interface_t interface)
905{
906 sja1105_inhibit_tx(ds->priv, BIT(port), true);
907}
908
909static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
910 unsigned int mode,
911 phy_interface_t interface,
Russell King5b502a72020-02-26 10:23:46 +0000912 struct phy_device *phydev,
913 int speed, int duplex,
914 bool tx_pause, bool rx_pause)
Vladimir Oltean8400cff2019-06-08 16:03:44 +0300915{
Vladimir Olteanec8582d2020-03-12 12:19:51 +0000916 struct sja1105_private *priv = ds->priv;
917
918 sja1105_adjust_port_config(priv, port, speed);
919
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200920 if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode))
921 sja1105_sgmii_pcs_force_speed(priv, speed);
922
Vladimir Olteanec8582d2020-03-12 12:19:51 +0000923 sja1105_inhibit_tx(priv, BIT(port), false);
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +0300924}
925
Vladimir Olteanad9f2992019-05-02 23:23:38 +0300926static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
927 unsigned long *supported,
928 struct phylink_link_state *state)
929{
930 /* Construct a new mask which exhaustively contains all link features
931 * supported by the MAC, and then apply that (logical AND) to what will
932 * be sent to the PHY for "marketing".
933 */
934 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
935 struct sja1105_private *priv = ds->priv;
936 struct sja1105_xmii_params_entry *mii;
937
938 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
939
Vladimir Oltean39710222019-06-28 00:46:36 +0300940 /* include/linux/phylink.h says:
941 * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
942 * expects the MAC driver to return all supported link modes.
943 */
944 if (state->interface != PHY_INTERFACE_MODE_NA &&
945 sja1105_phy_mode_mismatch(priv, port, state->interface)) {
946 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
947 return;
948 }
949
Vladimir Olteanad9f2992019-05-02 23:23:38 +0300950 /* The MAC does not support pause frames, and also doesn't
951 * support half-duplex traffic modes.
952 */
953 phylink_set(mask, Autoneg);
954 phylink_set(mask, MII);
955 phylink_set(mask, 10baseT_Full);
956 phylink_set(mask, 100baseT_Full);
Oleksij Rempelca68e132020-03-03 08:44:14 +0100957 phylink_set(mask, 100baseT1_Full);
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200958 if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
959 mii->xmii_mode[port] == XMII_MODE_SGMII)
Vladimir Olteanad9f2992019-05-02 23:23:38 +0300960 phylink_set(mask, 1000baseT_Full);
961
962 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
963 bitmap_and(state->advertising, state->advertising, mask,
964 __ETHTOOL_LINK_MODE_MASK_NBITS);
965}
966
Vladimir Olteanffe10e62020-03-20 13:29:37 +0200967static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port,
968 struct phylink_link_state *state)
969{
970 struct sja1105_private *priv = ds->priv;
971 int ais;
972
973 /* Read the vendor-specific AUTONEG_INTR_STATUS register */
974 ais = sja1105_sgmii_read(priv, SJA1105_AIS);
975 if (ais < 0)
976 return ais;
977
978 switch (SJA1105_AIS_SPEED(ais)) {
979 case 0:
980 state->speed = SPEED_10;
981 break;
982 case 1:
983 state->speed = SPEED_100;
984 break;
985 case 2:
986 state->speed = SPEED_1000;
987 break;
988 default:
989 dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n",
990 SJA1105_AIS_SPEED(ais));
991 }
992 state->duplex = SJA1105_AIS_DUPLEX_MODE(ais);
993 state->an_complete = SJA1105_AIS_COMPLETE(ais);
994 state->link = SJA1105_AIS_LINK_STATUS(ais);
995
996 return 0;
997}
998
Vladimir Oltean60f60532019-06-26 02:39:38 +0300999static int
1000sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
1001 const struct sja1105_l2_lookup_entry *requested)
1002{
1003 struct sja1105_l2_lookup_entry *l2_lookup;
1004 struct sja1105_table *table;
1005 int i;
1006
1007 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1008 l2_lookup = table->entries;
1009
1010 for (i = 0; i < table->entry_count; i++)
1011 if (l2_lookup[i].macaddr == requested->macaddr &&
1012 l2_lookup[i].vlanid == requested->vlanid &&
1013 l2_lookup[i].destports & BIT(port))
1014 return i;
1015
1016 return -1;
1017}
1018
1019/* We want FDB entries added statically through the bridge command to persist
1020 * across switch resets, which are a common thing during normal SJA1105
1021 * operation. So we have to back them up in the static configuration tables
1022 * and hence apply them on next static config upload... yay!
1023 */
1024static int
1025sja1105_static_fdb_change(struct sja1105_private *priv, int port,
1026 const struct sja1105_l2_lookup_entry *requested,
1027 bool keep)
1028{
1029 struct sja1105_l2_lookup_entry *l2_lookup;
1030 struct sja1105_table *table;
1031 int rc, match;
1032
1033 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1034
1035 match = sja1105_find_static_fdb_entry(priv, port, requested);
1036 if (match < 0) {
1037 /* Can't delete a missing entry. */
1038 if (!keep)
1039 return 0;
1040
1041 /* No match => new entry */
1042 rc = sja1105_table_resize(table, table->entry_count + 1);
1043 if (rc)
1044 return rc;
1045
1046 match = table->entry_count - 1;
1047 }
1048
1049 /* Assign pointer after the resize (it may be new memory) */
1050 l2_lookup = table->entries;
1051
1052 /* We have a match.
1053 * If the job was to add this FDB entry, it's already done (mostly
1054 * anyway, since the port forwarding mask may have changed, case in
1055 * which we update it).
1056 * Otherwise we have to delete it.
1057 */
1058 if (keep) {
1059 l2_lookup[match] = *requested;
1060 return 0;
1061 }
1062
1063 /* To remove, the strategy is to overwrite the element with
1064 * the last one, and then reduce the array size by 1
1065 */
1066 l2_lookup[match] = l2_lookup[table->entry_count - 1];
1067 return sja1105_table_resize(table, table->entry_count - 1);
1068}
1069
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001070/* First-generation switches have a 4-way set associative TCAM that
1071 * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1072 * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1073 * For the placement of a newly learnt FDB entry, the switch selects the bin
1074 * based on a hash function, and the way within that bin incrementally.
1075 */
Vladimir Oltean09c1b412019-10-01 22:17:59 +03001076static int sja1105et_fdb_index(int bin, int way)
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001077{
1078 return bin * SJA1105ET_FDB_BIN_SIZE + way;
1079}
1080
Vladimir Oltean9dfa6912019-06-03 00:11:57 +03001081static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1082 const u8 *addr, u16 vid,
1083 struct sja1105_l2_lookup_entry *match,
1084 int *last_unused)
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001085{
1086 int way;
1087
1088 for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1089 struct sja1105_l2_lookup_entry l2_lookup = {0};
1090 int index = sja1105et_fdb_index(bin, way);
1091
1092 /* Skip unused entries, optionally marking them
1093 * into the return value
1094 */
1095 if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1096 index, &l2_lookup)) {
1097 if (last_unused)
1098 *last_unused = way;
1099 continue;
1100 }
1101
1102 if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1103 l2_lookup.vlanid == vid) {
1104 if (match)
1105 *match = l2_lookup;
1106 return way;
1107 }
1108 }
1109 /* Return an invalid entry index if not found */
1110 return -1;
1111}
1112
Vladimir Oltean9dfa6912019-06-03 00:11:57 +03001113int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1114 const unsigned char *addr, u16 vid)
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001115{
1116 struct sja1105_l2_lookup_entry l2_lookup = {0};
1117 struct sja1105_private *priv = ds->priv;
1118 struct device *dev = ds->dev;
1119 int last_unused = -1;
Vladimir Oltean60f60532019-06-26 02:39:38 +03001120 int bin, way, rc;
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001121
Vladimir Oltean9dfa6912019-06-03 00:11:57 +03001122 bin = sja1105et_fdb_hash(priv, addr, vid);
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001123
Vladimir Oltean9dfa6912019-06-03 00:11:57 +03001124 way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1125 &l2_lookup, &last_unused);
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001126 if (way >= 0) {
1127 /* We have an FDB entry. Is our port in the destination
1128 * mask? If yes, we need to do nothing. If not, we need
1129 * to rewrite the entry by adding this port to it.
1130 */
1131 if (l2_lookup.destports & BIT(port))
1132 return 0;
1133 l2_lookup.destports |= BIT(port);
1134 } else {
1135 int index = sja1105et_fdb_index(bin, way);
1136
1137 /* We don't have an FDB entry. We construct a new one and
1138 * try to find a place for it within the FDB table.
1139 */
1140 l2_lookup.macaddr = ether_addr_to_u64(addr);
1141 l2_lookup.destports = BIT(port);
1142 l2_lookup.vlanid = vid;
1143
1144 if (last_unused >= 0) {
1145 way = last_unused;
1146 } else {
1147 /* Bin is full, need to evict somebody.
1148 * Choose victim at random. If you get these messages
1149 * often, you may need to consider changing the
1150 * distribution function:
1151 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1152 */
1153 get_random_bytes(&way, sizeof(u8));
1154 way %= SJA1105ET_FDB_BIN_SIZE;
1155 dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1156 bin, addr, way);
1157 /* Evict entry */
1158 sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1159 index, NULL, false);
1160 }
1161 }
1162 l2_lookup.index = sja1105et_fdb_index(bin, way);
1163
Vladimir Oltean60f60532019-06-26 02:39:38 +03001164 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1165 l2_lookup.index, &l2_lookup,
1166 true);
1167 if (rc < 0)
1168 return rc;
1169
1170 return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001171}
1172
Vladimir Oltean9dfa6912019-06-03 00:11:57 +03001173int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1174 const unsigned char *addr, u16 vid)
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001175{
1176 struct sja1105_l2_lookup_entry l2_lookup = {0};
1177 struct sja1105_private *priv = ds->priv;
Vladimir Oltean60f60532019-06-26 02:39:38 +03001178 int index, bin, way, rc;
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001179 bool keep;
1180
Vladimir Oltean9dfa6912019-06-03 00:11:57 +03001181 bin = sja1105et_fdb_hash(priv, addr, vid);
1182 way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1183 &l2_lookup, NULL);
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001184 if (way < 0)
1185 return 0;
1186 index = sja1105et_fdb_index(bin, way);
1187
1188 /* We have an FDB entry. Is our port in the destination mask? If yes,
1189 * we need to remove it. If the resulting port mask becomes empty, we
1190 * need to completely evict the FDB entry.
1191 * Otherwise we just write it back.
1192 */
Vladimir Oltean7752e932019-06-03 00:15:54 +03001193 l2_lookup.destports &= ~BIT(port);
1194
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001195 if (l2_lookup.destports)
1196 keep = true;
1197 else
1198 keep = false;
1199
Vladimir Oltean60f60532019-06-26 02:39:38 +03001200 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1201 index, &l2_lookup, keep);
1202 if (rc < 0)
1203 return rc;
1204
1205 return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001206}
1207
Vladimir Oltean9dfa6912019-06-03 00:11:57 +03001208int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
1209 const unsigned char *addr, u16 vid)
1210{
Vladimir Oltean1da73822019-06-03 00:15:45 +03001211 struct sja1105_l2_lookup_entry l2_lookup = {0};
1212 struct sja1105_private *priv = ds->priv;
1213 int rc, i;
1214
1215 /* Search for an existing entry in the FDB table */
1216 l2_lookup.macaddr = ether_addr_to_u64(addr);
1217 l2_lookup.vlanid = vid;
1218 l2_lookup.iotag = SJA1105_S_TAG;
1219 l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001220 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) {
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +03001221 l2_lookup.mask_vlanid = VLAN_VID_MASK;
1222 l2_lookup.mask_iotag = BIT(0);
1223 } else {
1224 l2_lookup.mask_vlanid = 0;
1225 l2_lookup.mask_iotag = 0;
1226 }
Vladimir Oltean1da73822019-06-03 00:15:45 +03001227 l2_lookup.destports = BIT(port);
1228
1229 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1230 SJA1105_SEARCH, &l2_lookup);
1231 if (rc == 0) {
1232 /* Found and this port is already in the entry's
1233 * port mask => job done
1234 */
1235 if (l2_lookup.destports & BIT(port))
1236 return 0;
1237 /* l2_lookup.index is populated by the switch in case it
1238 * found something.
1239 */
1240 l2_lookup.destports |= BIT(port);
1241 goto skip_finding_an_index;
1242 }
1243
1244 /* Not found, so try to find an unused spot in the FDB.
1245 * This is slightly inefficient because the strategy is knock-knock at
1246 * every possible position from 0 to 1023.
1247 */
1248 for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1249 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1250 i, NULL);
1251 if (rc < 0)
1252 break;
1253 }
1254 if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
1255 dev_err(ds->dev, "FDB is full, cannot add entry.\n");
1256 return -EINVAL;
1257 }
Vladimir Oltean17ae6552019-06-26 02:39:40 +03001258 l2_lookup.lockeds = true;
Vladimir Oltean1da73822019-06-03 00:15:45 +03001259 l2_lookup.index = i;
1260
1261skip_finding_an_index:
Vladimir Oltean60f60532019-06-26 02:39:38 +03001262 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1263 l2_lookup.index, &l2_lookup,
1264 true);
1265 if (rc < 0)
1266 return rc;
1267
1268 return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
Vladimir Oltean9dfa6912019-06-03 00:11:57 +03001269}
1270
1271int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
1272 const unsigned char *addr, u16 vid)
1273{
Vladimir Oltean1da73822019-06-03 00:15:45 +03001274 struct sja1105_l2_lookup_entry l2_lookup = {0};
1275 struct sja1105_private *priv = ds->priv;
1276 bool keep;
1277 int rc;
1278
1279 l2_lookup.macaddr = ether_addr_to_u64(addr);
1280 l2_lookup.vlanid = vid;
1281 l2_lookup.iotag = SJA1105_S_TAG;
1282 l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001283 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) {
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +03001284 l2_lookup.mask_vlanid = VLAN_VID_MASK;
1285 l2_lookup.mask_iotag = BIT(0);
1286 } else {
1287 l2_lookup.mask_vlanid = 0;
1288 l2_lookup.mask_iotag = 0;
1289 }
Vladimir Oltean1da73822019-06-03 00:15:45 +03001290 l2_lookup.destports = BIT(port);
1291
1292 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1293 SJA1105_SEARCH, &l2_lookup);
1294 if (rc < 0)
1295 return 0;
1296
1297 l2_lookup.destports &= ~BIT(port);
1298
1299 /* Decide whether we remove just this port from the FDB entry,
1300 * or if we remove it completely.
1301 */
1302 if (l2_lookup.destports)
1303 keep = true;
1304 else
1305 keep = false;
1306
Vladimir Oltean60f60532019-06-26 02:39:38 +03001307 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1308 l2_lookup.index, &l2_lookup, keep);
1309 if (rc < 0)
1310 return rc;
1311
1312 return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
Vladimir Oltean9dfa6912019-06-03 00:11:57 +03001313}
1314
1315static int sja1105_fdb_add(struct dsa_switch *ds, int port,
1316 const unsigned char *addr, u16 vid)
1317{
1318 struct sja1105_private *priv = ds->priv;
Vladimir Olteanb3ee5262019-06-26 02:39:41 +03001319
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +03001320 /* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
1321 * so the switch still does some VLAN processing internally.
1322 * But Shared VLAN Learning (SVL) is also active, and it will take
1323 * care of autonomous forwarding between the unique pvid's of each
1324 * port. Here we just make sure that users can't add duplicate FDB
1325 * entries when in this mode - the actual VID doesn't matter except
1326 * for what gets printed in 'bridge fdb show'. In the case of zero,
1327 * no VID gets printed at all.
Vladimir Oltean93647592019-06-03 00:16:01 +03001328 */
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001329 if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +03001330 vid = 0;
Vladimir Oltean93647592019-06-03 00:16:01 +03001331
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +03001332 return priv->info->fdb_add_cmd(ds, port, addr, vid);
Vladimir Oltean9dfa6912019-06-03 00:11:57 +03001333}
1334
1335static int sja1105_fdb_del(struct dsa_switch *ds, int port,
1336 const unsigned char *addr, u16 vid)
1337{
1338 struct sja1105_private *priv = ds->priv;
1339
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001340 if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +03001341 vid = 0;
Vladimir Oltean93647592019-06-03 00:16:01 +03001342
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +03001343 return priv->info->fdb_del_cmd(ds, port, addr, vid);
Vladimir Oltean9dfa6912019-06-03 00:11:57 +03001344}
1345
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001346static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1347 dsa_fdb_dump_cb_t *cb, void *data)
1348{
1349 struct sja1105_private *priv = ds->priv;
1350 struct device *dev = ds->dev;
1351 int i;
1352
1353 for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1354 struct sja1105_l2_lookup_entry l2_lookup = {0};
1355 u8 macaddr[ETH_ALEN];
1356 int rc;
1357
1358 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1359 i, &l2_lookup);
1360 /* No fdb entry at i, not an issue */
Vladimir Olteandef84602019-06-03 00:11:59 +03001361 if (rc == -ENOENT)
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001362 continue;
1363 if (rc) {
1364 dev_err(dev, "Failed to dump FDB: %d\n", rc);
1365 return rc;
1366 }
1367
1368 /* FDB dump callback is per port. This means we have to
1369 * disregard a valid entry if it's not for this port, even if
1370 * only to revisit it later. This is inefficient because the
1371 * 1024-sized FDB table needs to be traversed 4 times through
1372 * SPI during a 'bridge fdb show' command.
1373 */
1374 if (!(l2_lookup.destports & BIT(port)))
1375 continue;
1376 u64_to_ether_addr(l2_lookup.macaddr, macaddr);
Vladimir Oltean93647592019-06-03 00:16:01 +03001377
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +03001378 /* We need to hide the dsa_8021q VLANs from the user. */
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001379 if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +03001380 l2_lookup.vlanid = 0;
Vladimir Oltean17ae6552019-06-26 02:39:40 +03001381 cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
Vladimir Oltean291d1e72019-05-02 23:23:31 +03001382 }
1383 return 0;
1384}
1385
1386/* This callback needs to be present */
1387static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1388 const struct switchdev_obj_port_mdb *mdb)
1389{
1390 return 0;
1391}
1392
1393static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1394 const struct switchdev_obj_port_mdb *mdb)
1395{
1396 sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1397}
1398
1399static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1400 const struct switchdev_obj_port_mdb *mdb)
1401{
1402 return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1403}
1404
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03001405static int sja1105_bridge_member(struct dsa_switch *ds, int port,
1406 struct net_device *br, bool member)
1407{
1408 struct sja1105_l2_forwarding_entry *l2_fwd;
1409 struct sja1105_private *priv = ds->priv;
1410 int i, rc;
1411
1412 l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
1413
1414 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1415 /* Add this port to the forwarding matrix of the
1416 * other ports in the same bridge, and viceversa.
1417 */
1418 if (!dsa_is_user_port(ds, i))
1419 continue;
1420 /* For the ports already under the bridge, only one thing needs
1421 * to be done, and that is to add this port to their
1422 * reachability domain. So we can perform the SPI write for
1423 * them immediately. However, for this port itself (the one
1424 * that is new to the bridge), we need to add all other ports
1425 * to its reachability domain. So we do that incrementally in
1426 * this loop, and perform the SPI write only at the end, once
1427 * the domain contains all other bridge ports.
1428 */
1429 if (i == port)
1430 continue;
1431 if (dsa_to_port(ds, i)->bridge_dev != br)
1432 continue;
1433 sja1105_port_allow_traffic(l2_fwd, i, port, member);
1434 sja1105_port_allow_traffic(l2_fwd, port, i, member);
1435
1436 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
1437 i, &l2_fwd[i], true);
1438 if (rc < 0)
1439 return rc;
1440 }
1441
1442 return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
1443 port, &l2_fwd[port], true);
1444}
1445
Vladimir Oltean640f7632019-05-05 13:19:28 +03001446static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1447 u8 state)
1448{
1449 struct sja1105_private *priv = ds->priv;
1450 struct sja1105_mac_config_entry *mac;
1451
1452 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1453
1454 switch (state) {
1455 case BR_STATE_DISABLED:
1456 case BR_STATE_BLOCKING:
1457 /* From UM10944 description of DRPDTAG (why put this there?):
1458 * "Management traffic flows to the port regardless of the state
1459 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1460 * At the moment no difference between DISABLED and BLOCKING.
1461 */
1462 mac[port].ingress = false;
1463 mac[port].egress = false;
1464 mac[port].dyn_learn = false;
1465 break;
1466 case BR_STATE_LISTENING:
1467 mac[port].ingress = true;
1468 mac[port].egress = false;
1469 mac[port].dyn_learn = false;
1470 break;
1471 case BR_STATE_LEARNING:
1472 mac[port].ingress = true;
1473 mac[port].egress = false;
1474 mac[port].dyn_learn = true;
1475 break;
1476 case BR_STATE_FORWARDING:
1477 mac[port].ingress = true;
1478 mac[port].egress = true;
1479 mac[port].dyn_learn = true;
1480 break;
1481 default:
1482 dev_err(ds->dev, "invalid STP state: %d\n", state);
1483 return;
1484 }
1485
1486 sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1487 &mac[port], true);
1488}
1489
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03001490static int sja1105_bridge_join(struct dsa_switch *ds, int port,
1491 struct net_device *br)
1492{
1493 return sja1105_bridge_member(ds, port, br, true);
1494}
1495
1496static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
1497 struct net_device *br)
1498{
1499 sja1105_bridge_member(ds, port, br, false);
1500}
1501
Vladimir Oltean2eea1fa2019-11-12 23:22:00 +02001502static const char * const sja1105_reset_reasons[] = {
1503 [SJA1105_VLAN_FILTERING] = "VLAN filtering",
1504 [SJA1105_RX_HWTSTAMPING] = "RX timestamping",
1505 [SJA1105_AGEING_TIME] = "Ageing time",
1506 [SJA1105_SCHEDULING] = "Time-aware scheduling",
1507};
1508
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001509/* For situations where we need to change a setting at runtime that is only
1510 * available through the static configuration, resetting the switch in order
1511 * to upload the new static config is unavoidable. Back up the settings we
1512 * modify at runtime (currently only MAC) and restore them after uploading,
1513 * such that this operation is relatively seamless.
1514 */
Vladimir Oltean2eea1fa2019-11-12 23:22:00 +02001515int sja1105_static_config_reload(struct sja1105_private *priv,
1516 enum sja1105_reset_reason reason)
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001517{
Vladimir Oltean6cf99c12019-11-09 13:32:23 +02001518 struct ptp_system_timestamp ptp_sts_before;
1519 struct ptp_system_timestamp ptp_sts_after;
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001520 struct sja1105_mac_config_entry *mac;
1521 int speed_mbps[SJA1105_NUM_PORTS];
Vladimir Oltean6cf99c12019-11-09 13:32:23 +02001522 struct dsa_switch *ds = priv->ds;
1523 s64 t1, t2, t3, t4;
1524 s64 t12, t34;
Vladimir Olteanffe10e62020-03-20 13:29:37 +02001525 u16 bmcr = 0;
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001526 int rc, i;
Vladimir Oltean6cf99c12019-11-09 13:32:23 +02001527 s64 now;
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001528
Vladimir Olteanaf580ae2019-11-09 13:32:24 +02001529 mutex_lock(&priv->mgmt_lock);
1530
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001531 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1532
Vladimir Oltean8400cff2019-06-08 16:03:44 +03001533 /* Back up the dynamic link speed changed by sja1105_adjust_port_config
1534 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
1535 * switch wants to see in the static config in order to allow us to
1536 * change it through the dynamic interface later.
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001537 */
1538 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1539 speed_mbps[i] = sja1105_speed[mac[i].speed];
1540 mac[i].speed = SJA1105_SPEED_AUTO;
1541 }
1542
Vladimir Olteanffe10e62020-03-20 13:29:37 +02001543 if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT))
1544 bmcr = sja1105_sgmii_read(priv, MII_BMCR);
1545
Vladimir Oltean6cf99c12019-11-09 13:32:23 +02001546 /* No PTP operations can run right now */
1547 mutex_lock(&priv->ptp_data.lock);
1548
1549 rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
1550 if (rc < 0)
1551 goto out_unlock_ptp;
1552
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001553 /* Reset switch and send updated static configuration */
1554 rc = sja1105_static_config_upload(priv);
1555 if (rc < 0)
Vladimir Oltean6cf99c12019-11-09 13:32:23 +02001556 goto out_unlock_ptp;
1557
1558 rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
1559 if (rc < 0)
1560 goto out_unlock_ptp;
1561
1562 t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
1563 t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
1564 t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
1565 t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
1566 /* Mid point, corresponds to pre-reset PTPCLKVAL */
1567 t12 = t1 + (t2 - t1) / 2;
1568 /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
1569 t34 = t3 + (t4 - t3) / 2;
1570 /* Advance PTPCLKVAL by the time it took since its readout */
1571 now += (t34 - t12);
1572
1573 __sja1105_ptp_adjtime(ds, now);
1574
1575out_unlock_ptp:
1576 mutex_unlock(&priv->ptp_data.lock);
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001577
Vladimir Oltean2eea1fa2019-11-12 23:22:00 +02001578 dev_info(priv->ds->dev,
1579 "Reset switch and programmed static config. Reason: %s\n",
1580 sja1105_reset_reasons[reason]);
1581
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001582 /* Configure the CGU (PLLs) for MII and RMII PHYs.
1583 * For these interfaces there is no dynamic configuration
1584 * needed, since PLLs have same settings at all speeds.
1585 */
1586 rc = sja1105_clocking_setup(priv);
1587 if (rc < 0)
1588 goto out;
1589
1590 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
Vladimir Oltean8400cff2019-06-08 16:03:44 +03001591 rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001592 if (rc < 0)
1593 goto out;
1594 }
Vladimir Olteanffe10e62020-03-20 13:29:37 +02001595
1596 if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) {
1597 bool an_enabled = !!(bmcr & BMCR_ANENABLE);
1598
1599 sja1105_sgmii_pcs_config(priv, an_enabled, false);
1600
1601 if (!an_enabled) {
1602 int speed = SPEED_UNKNOWN;
1603
1604 if (bmcr & BMCR_SPEED1000)
1605 speed = SPEED_1000;
1606 else if (bmcr & BMCR_SPEED100)
1607 speed = SPEED_100;
1608 else if (bmcr & BMCR_SPEED10)
1609 speed = SPEED_10;
1610
1611 sja1105_sgmii_pcs_force_speed(priv, speed);
1612 }
1613 }
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001614out:
Vladimir Olteanaf580ae2019-11-09 13:32:24 +02001615 mutex_unlock(&priv->mgmt_lock);
1616
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001617 return rc;
1618}
1619
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001620static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
1621{
1622 struct sja1105_mac_config_entry *mac;
1623
1624 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1625
1626 mac[port].vlanid = pvid;
1627
1628 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1629 &mac[port], true);
1630}
1631
1632static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
1633{
1634 struct sja1105_vlan_lookup_entry *vlan;
1635 int count, i;
1636
1637 vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
1638 count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
1639
1640 for (i = 0; i < count; i++)
1641 if (vlan[i].vlanid == vid)
1642 return i;
1643
1644 /* Return an invalid entry index if not found */
1645 return -1;
1646}
1647
1648static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid,
1649 bool enabled, bool untagged)
1650{
1651 struct sja1105_vlan_lookup_entry *vlan;
1652 struct sja1105_table *table;
1653 bool keep = true;
1654 int match, rc;
1655
1656 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
1657
1658 match = sja1105_is_vlan_configured(priv, vid);
1659 if (match < 0) {
1660 /* Can't delete a missing entry. */
1661 if (!enabled)
1662 return 0;
1663 rc = sja1105_table_resize(table, table->entry_count + 1);
1664 if (rc)
1665 return rc;
1666 match = table->entry_count - 1;
1667 }
1668 /* Assign pointer after the resize (it's new memory) */
1669 vlan = table->entries;
1670 vlan[match].vlanid = vid;
1671 if (enabled) {
1672 vlan[match].vlan_bc |= BIT(port);
1673 vlan[match].vmemb_port |= BIT(port);
1674 } else {
1675 vlan[match].vlan_bc &= ~BIT(port);
1676 vlan[match].vmemb_port &= ~BIT(port);
1677 }
1678 /* Also unset tag_port if removing this VLAN was requested,
1679 * just so we don't have a confusing bitmap (no practical purpose).
1680 */
1681 if (untagged || !enabled)
1682 vlan[match].tag_port &= ~BIT(port);
1683 else
1684 vlan[match].tag_port |= BIT(port);
1685 /* If there's no port left as member of this VLAN,
1686 * it's time for it to go.
1687 */
1688 if (!vlan[match].vmemb_port)
1689 keep = false;
1690
1691 dev_dbg(priv->ds->dev,
1692 "%s: port %d, vid %llu, broadcast domain 0x%llx, "
1693 "port members 0x%llx, tagged ports 0x%llx, keep %d\n",
1694 __func__, port, vlan[match].vlanid, vlan[match].vlan_bc,
1695 vlan[match].vmemb_port, vlan[match].tag_port, keep);
1696
1697 rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
1698 &vlan[match], keep);
1699 if (rc < 0)
1700 return rc;
1701
1702 if (!keep)
1703 return sja1105_table_delete_entry(table, match);
1704
1705 return 0;
1706}
1707
Vladimir Oltean227d07a2019-05-05 13:19:27 +03001708static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1709{
1710 int rc, i;
1711
1712 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1713 rc = dsa_port_setup_8021q_tagging(ds, i, enabled);
1714 if (rc < 0) {
1715 dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n",
1716 i, rc);
1717 return rc;
1718 }
1719 }
1720 dev_info(ds->dev, "%s switch tagging\n",
1721 enabled ? "Enabled" : "Disabled");
1722 return 0;
1723}
1724
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03001725static enum dsa_tag_protocol
Florian Fainelli4d776482020-01-07 21:06:05 -08001726sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
1727 enum dsa_tag_protocol mp)
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03001728{
Vladimir Oltean227d07a2019-05-05 13:19:27 +03001729 return DSA_TAG_PROTO_SJA1105;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03001730}
1731
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001732/* This callback needs to be present */
1733static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
1734 const struct switchdev_obj_port_vlan *vlan)
1735{
1736 return 0;
1737}
1738
Vladimir Oltean070ca3b2019-06-08 15:04:30 +03001739/* The TPID setting belongs to the General Parameters table,
1740 * which can only be partially reconfigured at runtime (and not the TPID).
1741 * So a switch reset is required.
1742 */
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001743static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
1744{
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +03001745 struct sja1105_l2_lookup_params_entry *l2_lookup_params;
Vladimir Oltean070ca3b2019-06-08 15:04:30 +03001746 struct sja1105_general_params_entry *general_params;
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001747 struct sja1105_private *priv = ds->priv;
Vladimir Oltean070ca3b2019-06-08 15:04:30 +03001748 struct sja1105_table *table;
1749 u16 tpid, tpid2;
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001750 int rc;
1751
Vladimir Oltean070ca3b2019-06-08 15:04:30 +03001752 if (enabled) {
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001753 /* Enable VLAN filtering. */
Vladimir Oltean54fa49e2019-12-27 03:11:13 +02001754 tpid = ETH_P_8021Q;
1755 tpid2 = ETH_P_8021AD;
Vladimir Oltean070ca3b2019-06-08 15:04:30 +03001756 } else {
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001757 /* Disable VLAN filtering. */
Vladimir Oltean070ca3b2019-06-08 15:04:30 +03001758 tpid = ETH_P_SJA1105;
1759 tpid2 = ETH_P_SJA1105;
1760 }
1761
1762 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1763 general_params = table->entries;
Vladimir Olteanf9a1a762019-06-08 15:04:31 +03001764 /* EtherType used to identify inner tagged (C-tag) VLAN traffic */
Vladimir Oltean54fa49e2019-12-27 03:11:13 +02001765 general_params->tpid = tpid;
1766 /* EtherType used to identify outer tagged (S-tag) VLAN traffic */
Vladimir Oltean070ca3b2019-06-08 15:04:30 +03001767 general_params->tpid2 = tpid2;
Vladimir Oltean42824462019-06-08 15:04:32 +03001768 /* When VLAN filtering is on, we need to at least be able to
1769 * decode management traffic through the "backup plan".
1770 */
1771 general_params->incl_srcpt1 = enabled;
1772 general_params->incl_srcpt0 = enabled;
Vladimir Oltean070ca3b2019-06-08 15:04:30 +03001773
Vladimir Oltean6d7c7d92019-08-05 01:38:44 +03001774 /* VLAN filtering => independent VLAN learning.
1775 * No VLAN filtering => shared VLAN learning.
1776 *
1777 * In shared VLAN learning mode, untagged traffic still gets
1778 * pvid-tagged, and the FDB table gets populated with entries
1779 * containing the "real" (pvid or from VLAN tag) VLAN ID.
1780 * However the switch performs a masked L2 lookup in the FDB,
1781 * effectively only looking up a frame's DMAC (and not VID) for the
1782 * forwarding decision.
1783 *
1784 * This is extremely convenient for us, because in modes with
1785 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
1786 * each front panel port. This is good for identification but breaks
1787 * learning badly - the VID of the learnt FDB entry is unique, aka
1788 * no frames coming from any other port are going to have it. So
1789 * for forwarding purposes, this is as though learning was broken
1790 * (all frames get flooded).
1791 */
1792 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
1793 l2_lookup_params = table->entries;
1794 l2_lookup_params->shared_learn = !enabled;
1795
Vladimir Oltean2eea1fa2019-11-12 23:22:00 +02001796 rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001797 if (rc)
1798 dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
1799
Vladimir Oltean227d07a2019-05-05 13:19:27 +03001800 /* Switch port identification based on 802.1Q is only passable
1801 * if we are not under a vlan_filtering bridge. So make sure
1802 * the two configurations are mutually exclusive.
1803 */
1804 return sja1105_setup_8021q_tagging(ds, !enabled);
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001805}
1806
1807static void sja1105_vlan_add(struct dsa_switch *ds, int port,
1808 const struct switchdev_obj_port_vlan *vlan)
1809{
1810 struct sja1105_private *priv = ds->priv;
1811 u16 vid;
1812 int rc;
1813
1814 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1815 rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags &
1816 BRIDGE_VLAN_INFO_UNTAGGED);
1817 if (rc < 0) {
1818 dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
1819 vid, port, rc);
1820 return;
1821 }
1822 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1823 rc = sja1105_pvid_apply(ds->priv, port, vid);
1824 if (rc < 0) {
1825 dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n",
1826 vid, port, rc);
1827 return;
1828 }
1829 }
1830 }
1831}
1832
1833static int sja1105_vlan_del(struct dsa_switch *ds, int port,
1834 const struct switchdev_obj_port_vlan *vlan)
1835{
1836 struct sja1105_private *priv = ds->priv;
1837 u16 vid;
1838 int rc;
1839
1840 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1841 rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags &
1842 BRIDGE_VLAN_INFO_UNTAGGED);
1843 if (rc < 0) {
1844 dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
1845 vid, port, rc);
1846 return rc;
1847 }
1848 }
1849 return 0;
1850}
1851
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03001852/* The programming model for the SJA1105 switch is "all-at-once" via static
1853 * configuration tables. Some of these can be dynamically modified at runtime,
1854 * but not the xMII mode parameters table.
1855 * Furthermode, some PHYs may not have crystals for generating their clocks
1856 * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
1857 * ref_clk pin. So port clocking needs to be initialized early, before
1858 * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
1859 * Setting correct PHY link speed does not matter now.
1860 * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
1861 * bindings are not yet parsed by DSA core. We need to parse early so that we
1862 * can populate the xMII mode parameters table.
1863 */
1864static int sja1105_setup(struct dsa_switch *ds)
1865{
1866 struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
1867 struct sja1105_private *priv = ds->priv;
1868 int rc;
1869
1870 rc = sja1105_parse_dt(priv, ports);
1871 if (rc < 0) {
1872 dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
1873 return rc;
1874 }
Vladimir Olteanf5b86312019-05-02 23:23:32 +03001875
1876 /* Error out early if internal delays are required through DT
1877 * and we can't apply them.
1878 */
1879 rc = sja1105_parse_rgmii_delays(priv, ports);
1880 if (rc < 0) {
1881 dev_err(ds->dev, "RGMII delay not supported\n");
1882 return rc;
1883 }
1884
Vladimir Oltean61c77122019-10-12 02:18:14 +03001885 rc = sja1105_ptp_clock_register(ds);
Vladimir Olteanbb77f362019-06-08 15:04:34 +03001886 if (rc < 0) {
1887 dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
1888 return rc;
1889 }
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03001890 /* Create and send configuration down to device */
1891 rc = sja1105_static_config_load(priv, ports);
1892 if (rc < 0) {
1893 dev_err(ds->dev, "Failed to load static config: %d\n", rc);
1894 return rc;
1895 }
1896 /* Configure the CGU (PHY link modes and speeds) */
1897 rc = sja1105_clocking_setup(priv);
1898 if (rc < 0) {
1899 dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
1900 return rc;
1901 }
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03001902 /* On SJA1105, VLAN filtering per se is always enabled in hardware.
1903 * The only thing we can do to disable it is lie about what the 802.1Q
1904 * EtherType is.
1905 * So it will still try to apply VLAN filtering, but all ingress
1906 * traffic (except frames received with EtherType of ETH_P_SJA1105)
1907 * will be internally tagged with a distorted VLAN header where the
1908 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
1909 */
1910 ds->vlan_filtering_is_global = true;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03001911
Vladimir Oltean5f06c632019-09-15 05:00:01 +03001912 /* Advertise the 8 egress queues */
1913 ds->num_tx_queues = SJA1105_NUM_TC;
1914
Vladimir Oltean227d07a2019-05-05 13:19:27 +03001915 /* The DSA/switchdev model brings up switch ports in standalone mode by
1916 * default, and that means vlan_filtering is 0 since they're not under
1917 * a bridge, so it's safe to set up switch tagging at this time.
1918 */
1919 return sja1105_setup_8021q_tagging(ds, true);
1920}
1921
Vladimir Olteanf3097be2019-06-08 15:04:42 +03001922static void sja1105_teardown(struct dsa_switch *ds)
1923{
1924 struct sja1105_private *priv = ds->priv;
Vladimir Olteana68578c22020-01-04 02:37:10 +02001925 int port;
1926
1927 for (port = 0; port < SJA1105_NUM_PORTS; port++) {
1928 struct sja1105_port *sp = &priv->ports[port];
1929
1930 if (!dsa_is_user_port(ds, port))
1931 continue;
1932
Vladimir Oltean52c0d4e2020-02-29 22:30:07 +02001933 if (sp->xmit_worker)
1934 kthread_destroy_worker(sp->xmit_worker);
Vladimir Olteana68578c22020-01-04 02:37:10 +02001935 }
Vladimir Olteanf3097be2019-06-08 15:04:42 +03001936
Vladimir Oltean317ab5b2019-09-15 05:00:02 +03001937 sja1105_tas_teardown(ds);
Vladimir Oltean61c77122019-10-12 02:18:14 +03001938 sja1105_ptp_clock_unregister(ds);
Vladimir Oltean6cb0abb2019-08-05 01:38:46 +03001939 sja1105_static_config_free(&priv->static_config);
Vladimir Olteanf3097be2019-06-08 15:04:42 +03001940}
1941
Vladimir Olteane9bf9692019-08-25 22:46:30 +03001942static int sja1105_port_enable(struct dsa_switch *ds, int port,
1943 struct phy_device *phy)
1944{
1945 struct net_device *slave;
1946
1947 if (!dsa_is_user_port(ds, port))
1948 return 0;
1949
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001950 slave = dsa_to_port(ds, port)->slave;
Vladimir Olteane9bf9692019-08-25 22:46:30 +03001951
1952 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1953
1954 return 0;
1955}
1956
Vladimir Olteana68578c22020-01-04 02:37:10 +02001957static void sja1105_port_disable(struct dsa_switch *ds, int port)
1958{
1959 struct sja1105_private *priv = ds->priv;
1960 struct sja1105_port *sp = &priv->ports[port];
1961
1962 if (!dsa_is_user_port(ds, port))
1963 return;
1964
1965 kthread_cancel_work_sync(&sp->xmit_work);
1966 skb_queue_purge(&sp->xmit_queue);
1967}
1968
Vladimir Oltean227d07a2019-05-05 13:19:27 +03001969static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
Vladimir Oltean47ed9852019-06-08 15:04:35 +03001970 struct sk_buff *skb, bool takets)
Vladimir Oltean227d07a2019-05-05 13:19:27 +03001971{
1972 struct sja1105_mgmt_entry mgmt_route = {0};
1973 struct sja1105_private *priv = ds->priv;
1974 struct ethhdr *hdr;
1975 int timeout = 10;
1976 int rc;
1977
1978 hdr = eth_hdr(skb);
1979
1980 mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
1981 mgmt_route.destports = BIT(port);
1982 mgmt_route.enfport = 1;
Vladimir Oltean47ed9852019-06-08 15:04:35 +03001983 mgmt_route.tsreg = 0;
1984 mgmt_route.takets = takets;
Vladimir Oltean227d07a2019-05-05 13:19:27 +03001985
1986 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
1987 slot, &mgmt_route, true);
1988 if (rc < 0) {
1989 kfree_skb(skb);
1990 return rc;
1991 }
1992
1993 /* Transfer skb to the host port. */
Vivien Didelot68bb8ea2019-10-21 16:51:15 -04001994 dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
Vladimir Oltean227d07a2019-05-05 13:19:27 +03001995
1996 /* Wait until the switch has processed the frame */
1997 do {
1998 rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
1999 slot, &mgmt_route);
2000 if (rc < 0) {
2001 dev_err_ratelimited(priv->ds->dev,
2002 "failed to poll for mgmt route\n");
2003 continue;
2004 }
2005
2006 /* UM10944: The ENFPORT flag of the respective entry is
2007 * cleared when a match is found. The host can use this
2008 * flag as an acknowledgment.
2009 */
2010 cpu_relax();
2011 } while (mgmt_route.enfport && --timeout);
2012
2013 if (!timeout) {
2014 /* Clean up the management route so that a follow-up
2015 * frame may not match on it by mistake.
Vladimir Oltean2a7e7402019-06-03 00:15:33 +03002016 * This is only hardware supported on P/Q/R/S - on E/T it is
2017 * a no-op and we are silently discarding the -EOPNOTSUPP.
Vladimir Oltean227d07a2019-05-05 13:19:27 +03002018 */
2019 sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2020 slot, &mgmt_route, false);
2021 dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2022 }
2023
2024 return NETDEV_TX_OK;
2025}
2026
Vladimir Olteana68578c22020-01-04 02:37:10 +02002027#define work_to_port(work) \
2028 container_of((work), struct sja1105_port, xmit_work)
2029#define tagger_to_sja1105(t) \
2030 container_of((t), struct sja1105_private, tagger_data)
2031
Vladimir Oltean227d07a2019-05-05 13:19:27 +03002032/* Deferred work is unfortunately necessary because setting up the management
2033 * route cannot be done from atomit context (SPI transfer takes a sleepable
2034 * lock on the bus)
2035 */
Vladimir Olteana68578c22020-01-04 02:37:10 +02002036static void sja1105_port_deferred_xmit(struct kthread_work *work)
Vladimir Oltean227d07a2019-05-05 13:19:27 +03002037{
Vladimir Olteana68578c22020-01-04 02:37:10 +02002038 struct sja1105_port *sp = work_to_port(work);
2039 struct sja1105_tagger_data *tagger_data = sp->data;
2040 struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
2041 int port = sp - priv->ports;
2042 struct sk_buff *skb;
Vladimir Oltean227d07a2019-05-05 13:19:27 +03002043
Vladimir Olteana68578c22020-01-04 02:37:10 +02002044 while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
2045 struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
Vladimir Oltean227d07a2019-05-05 13:19:27 +03002046
Vladimir Olteana68578c22020-01-04 02:37:10 +02002047 mutex_lock(&priv->mgmt_lock);
Vladimir Oltean227d07a2019-05-05 13:19:27 +03002048
Vladimir Olteana68578c22020-01-04 02:37:10 +02002049 sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
Vladimir Oltean47ed9852019-06-08 15:04:35 +03002050
Vladimir Olteana68578c22020-01-04 02:37:10 +02002051 /* The clone, if there, was made by dsa_skb_tx_timestamp */
2052 if (clone)
2053 sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
Vladimir Oltean47ed9852019-06-08 15:04:35 +03002054
Vladimir Olteana68578c22020-01-04 02:37:10 +02002055 mutex_unlock(&priv->mgmt_lock);
2056 }
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002057}
2058
Vladimir Oltean84567212019-05-02 23:23:36 +03002059/* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
2060 * which cannot be reconfigured at runtime. So a switch reset is required.
2061 */
2062static int sja1105_set_ageing_time(struct dsa_switch *ds,
2063 unsigned int ageing_time)
2064{
2065 struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2066 struct sja1105_private *priv = ds->priv;
2067 struct sja1105_table *table;
2068 unsigned int maxage;
2069
2070 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2071 l2_lookup_params = table->entries;
2072
2073 maxage = SJA1105_AGEING_TIME_MS(ageing_time);
2074
2075 if (l2_lookup_params->maxage == maxage)
2076 return 0;
2077
2078 l2_lookup_params->maxage = maxage;
2079
Vladimir Oltean2eea1fa2019-11-12 23:22:00 +02002080 return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
Vladimir Oltean84567212019-05-02 23:23:36 +03002081}
2082
Vladimir Oltean317ab5b2019-09-15 05:00:02 +03002083static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2084 enum tc_setup_type type,
2085 void *type_data)
2086{
2087 switch (type) {
2088 case TC_SETUP_QDISC_TAPRIO:
2089 return sja1105_setup_tc_taprio(ds, port, type_data);
2090 default:
2091 return -EOPNOTSUPP;
2092 }
2093}
2094
Vladimir Oltean511e6ca2019-10-04 03:33:47 +03002095/* We have a single mirror (@to) port, but can configure ingress and egress
2096 * mirroring on all other (@from) ports.
2097 * We need to allow mirroring rules only as long as the @to port is always the
2098 * same, and we need to unset the @to port from mirr_port only when there is no
2099 * mirroring rule that references it.
2100 */
2101static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2102 bool ingress, bool enabled)
2103{
2104 struct sja1105_general_params_entry *general_params;
2105 struct sja1105_mac_config_entry *mac;
2106 struct sja1105_table *table;
2107 bool already_enabled;
2108 u64 new_mirr_port;
2109 int rc;
2110
2111 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2112 general_params = table->entries;
2113
2114 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2115
2116 already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS);
2117 if (already_enabled && enabled && general_params->mirr_port != to) {
2118 dev_err(priv->ds->dev,
2119 "Delete mirroring rules towards port %llu first\n",
2120 general_params->mirr_port);
2121 return -EBUSY;
2122 }
2123
2124 new_mirr_port = to;
2125 if (!enabled) {
2126 bool keep = false;
2127 int port;
2128
2129 /* Anybody still referencing mirr_port? */
2130 for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2131 if (mac[port].ing_mirr || mac[port].egr_mirr) {
2132 keep = true;
2133 break;
2134 }
2135 }
2136 /* Unset already_enabled for next time */
2137 if (!keep)
2138 new_mirr_port = SJA1105_NUM_PORTS;
2139 }
2140 if (new_mirr_port != general_params->mirr_port) {
2141 general_params->mirr_port = new_mirr_port;
2142
2143 rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2144 0, general_params, true);
2145 if (rc < 0)
2146 return rc;
2147 }
2148
2149 if (ingress)
2150 mac[from].ing_mirr = enabled;
2151 else
2152 mac[from].egr_mirr = enabled;
2153
2154 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2155 &mac[from], true);
2156}
2157
2158static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2159 struct dsa_mall_mirror_tc_entry *mirror,
2160 bool ingress)
2161{
2162 return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2163 ingress, true);
2164}
2165
2166static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2167 struct dsa_mall_mirror_tc_entry *mirror)
2168{
2169 sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2170 mirror->ingress, false);
2171}
2172
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002173static const struct dsa_switch_ops sja1105_switch_ops = {
2174 .get_tag_protocol = sja1105_get_tag_protocol,
2175 .setup = sja1105_setup,
Vladimir Olteanf3097be2019-06-08 15:04:42 +03002176 .teardown = sja1105_teardown,
Vladimir Oltean84567212019-05-02 23:23:36 +03002177 .set_ageing_time = sja1105_set_ageing_time,
Vladimir Olteanad9f2992019-05-02 23:23:38 +03002178 .phylink_validate = sja1105_phylink_validate,
Vladimir Olteanffe10e62020-03-20 13:29:37 +02002179 .phylink_mac_link_state = sja1105_mac_pcs_get_state,
Vladimir Olteanaf7cd032019-05-28 20:38:17 +03002180 .phylink_mac_config = sja1105_mac_config,
Vladimir Oltean8400cff2019-06-08 16:03:44 +03002181 .phylink_mac_link_up = sja1105_mac_link_up,
2182 .phylink_mac_link_down = sja1105_mac_link_down,
Vladimir Oltean52c34e62019-05-02 23:23:35 +03002183 .get_strings = sja1105_get_strings,
2184 .get_ethtool_stats = sja1105_get_ethtool_stats,
2185 .get_sset_count = sja1105_get_sset_count,
Vladimir Olteanbb77f362019-06-08 15:04:34 +03002186 .get_ts_info = sja1105_get_ts_info,
Vladimir Olteane9bf9692019-08-25 22:46:30 +03002187 .port_enable = sja1105_port_enable,
Vladimir Olteana68578c22020-01-04 02:37:10 +02002188 .port_disable = sja1105_port_disable,
Vladimir Oltean291d1e72019-05-02 23:23:31 +03002189 .port_fdb_dump = sja1105_fdb_dump,
2190 .port_fdb_add = sja1105_fdb_add,
2191 .port_fdb_del = sja1105_fdb_del,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002192 .port_bridge_join = sja1105_bridge_join,
2193 .port_bridge_leave = sja1105_bridge_leave,
Vladimir Oltean640f7632019-05-05 13:19:28 +03002194 .port_stp_state_set = sja1105_bridge_stp_state_set,
Vladimir Oltean6666ceb2019-05-02 23:23:34 +03002195 .port_vlan_prepare = sja1105_vlan_prepare,
2196 .port_vlan_filtering = sja1105_vlan_filtering,
2197 .port_vlan_add = sja1105_vlan_add,
2198 .port_vlan_del = sja1105_vlan_del,
Vladimir Oltean291d1e72019-05-02 23:23:31 +03002199 .port_mdb_prepare = sja1105_mdb_prepare,
2200 .port_mdb_add = sja1105_mdb_add,
2201 .port_mdb_del = sja1105_mdb_del,
Vladimir Olteana602afd2019-06-08 15:04:43 +03002202 .port_hwtstamp_get = sja1105_hwtstamp_get,
2203 .port_hwtstamp_set = sja1105_hwtstamp_set,
Vladimir Olteanf3097be2019-06-08 15:04:42 +03002204 .port_rxtstamp = sja1105_port_rxtstamp,
Vladimir Oltean47ed9852019-06-08 15:04:35 +03002205 .port_txtstamp = sja1105_port_txtstamp,
Vladimir Oltean317ab5b2019-09-15 05:00:02 +03002206 .port_setup_tc = sja1105_port_setup_tc,
Vladimir Oltean511e6ca2019-10-04 03:33:47 +03002207 .port_mirror_add = sja1105_mirror_add,
2208 .port_mirror_del = sja1105_mirror_del,
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002209};
2210
2211static int sja1105_check_device_id(struct sja1105_private *priv)
2212{
2213 const struct sja1105_regs *regs = priv->info->regs;
2214 u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
2215 struct device *dev = &priv->spidev->dev;
Vladimir Olteandff79622019-10-01 22:18:00 +03002216 u32 device_id;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002217 u64 part_no;
2218 int rc;
2219
Vladimir Oltean34d76e92019-11-09 13:32:22 +02002220 rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
2221 NULL);
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002222 if (rc < 0)
2223 return rc;
2224
2225 if (device_id != priv->info->device_id) {
Vladimir Olteandff79622019-10-01 22:18:00 +03002226 dev_err(dev, "Expected device ID 0x%llx but read 0x%x\n",
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002227 priv->info->device_id, device_id);
2228 return -ENODEV;
2229 }
2230
Vladimir Oltean1bd44872019-10-01 22:18:01 +03002231 rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
2232 SJA1105_SIZE_DEVICE_ID);
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002233 if (rc < 0)
2234 return rc;
2235
2236 sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
2237
2238 if (part_no != priv->info->part_no) {
2239 dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n",
2240 priv->info->part_no, part_no);
2241 return -ENODEV;
2242 }
2243
2244 return 0;
2245}
2246
2247static int sja1105_probe(struct spi_device *spi)
2248{
Vladimir Oltean844d7ed2019-06-08 15:04:40 +03002249 struct sja1105_tagger_data *tagger_data;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002250 struct device *dev = &spi->dev;
2251 struct sja1105_private *priv;
2252 struct dsa_switch *ds;
Vladimir Olteana68578c22020-01-04 02:37:10 +02002253 int rc, port;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002254
2255 if (!dev->of_node) {
2256 dev_err(dev, "No DTS bindings for SJA1105 driver\n");
2257 return -EINVAL;
2258 }
2259
2260 priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
2261 if (!priv)
2262 return -ENOMEM;
2263
2264 /* Configure the optional reset pin and bring up switch */
2265 priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
2266 if (IS_ERR(priv->reset_gpio))
2267 dev_dbg(dev, "reset-gpios not defined, ignoring\n");
2268 else
2269 sja1105_hw_reset(priv->reset_gpio, 1, 1);
2270
2271 /* Populate our driver private structure (priv) based on
2272 * the device tree node that was probed (spi)
2273 */
2274 priv->spidev = spi;
2275 spi_set_drvdata(spi, priv);
2276
2277 /* Configure the SPI bus */
2278 spi->bits_per_word = 8;
2279 rc = spi_setup(spi);
2280 if (rc < 0) {
2281 dev_err(dev, "Could not init SPI\n");
2282 return rc;
2283 }
2284
2285 priv->info = of_device_get_match_data(dev);
2286
2287 /* Detect hardware device */
2288 rc = sja1105_check_device_id(priv);
2289 if (rc < 0) {
2290 dev_err(dev, "Device ID check failed: %d\n", rc);
2291 return rc;
2292 }
2293
2294 dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
2295
Vivien Didelot7e99e342019-10-21 16:51:30 -04002296 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002297 if (!ds)
2298 return -ENOMEM;
2299
Vivien Didelot7e99e342019-10-21 16:51:30 -04002300 ds->dev = dev;
2301 ds->num_ports = SJA1105_NUM_PORTS;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002302 ds->ops = &sja1105_switch_ops;
2303 ds->priv = priv;
2304 priv->ds = ds;
2305
Vladimir Oltean844d7ed2019-06-08 15:04:40 +03002306 tagger_data = &priv->tagger_data;
Vladimir Oltean844d7ed2019-06-08 15:04:40 +03002307
Vivien Didelotd5a619b2019-10-21 16:51:28 -04002308 mutex_init(&priv->ptp_data.lock);
2309 mutex_init(&priv->mgmt_lock);
2310
2311 sja1105_tas_setup(ds);
2312
2313 rc = dsa_register_switch(priv->ds);
2314 if (rc)
2315 return rc;
2316
Vladimir Oltean227d07a2019-05-05 13:19:27 +03002317 /* Connections between dsa_port and sja1105_port */
Vladimir Olteana68578c22020-01-04 02:37:10 +02002318 for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2319 struct sja1105_port *sp = &priv->ports[port];
2320 struct dsa_port *dp = dsa_to_port(ds, port);
2321 struct net_device *slave;
Vladimir Oltean227d07a2019-05-05 13:19:27 +03002322
Vladimir Olteana68578c22020-01-04 02:37:10 +02002323 if (!dsa_is_user_port(ds, port))
2324 continue;
2325
2326 dp->priv = sp;
2327 sp->dp = dp;
Vladimir Oltean844d7ed2019-06-08 15:04:40 +03002328 sp->data = tagger_data;
Vladimir Olteana68578c22020-01-04 02:37:10 +02002329 slave = dp->slave;
2330 kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
2331 sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
2332 slave->name);
2333 if (IS_ERR(sp->xmit_worker)) {
2334 rc = PTR_ERR(sp->xmit_worker);
2335 dev_err(ds->dev,
2336 "failed to create deferred xmit thread: %d\n",
2337 rc);
2338 goto out;
2339 }
2340 skb_queue_head_init(&sp->xmit_queue);
Vladimir Oltean227d07a2019-05-05 13:19:27 +03002341 }
Vladimir Oltean227d07a2019-05-05 13:19:27 +03002342
Vivien Didelotd5a619b2019-10-21 16:51:28 -04002343 return 0;
Vladimir Olteana68578c22020-01-04 02:37:10 +02002344out:
2345 while (port-- > 0) {
2346 struct sja1105_port *sp = &priv->ports[port];
2347
2348 if (!dsa_is_user_port(ds, port))
2349 continue;
2350
2351 kthread_destroy_worker(sp->xmit_worker);
2352 }
2353 return rc;
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002354}
2355
2356static int sja1105_remove(struct spi_device *spi)
2357{
2358 struct sja1105_private *priv = spi_get_drvdata(spi);
2359
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002360 dsa_unregister_switch(priv->ds);
Vladimir Oltean8aa9ebc2019-05-02 23:23:30 +03002361 return 0;
2362}
2363
2364static const struct of_device_id sja1105_dt_ids[] = {
2365 { .compatible = "nxp,sja1105e", .data = &sja1105e_info },
2366 { .compatible = "nxp,sja1105t", .data = &sja1105t_info },
2367 { .compatible = "nxp,sja1105p", .data = &sja1105p_info },
2368 { .compatible = "nxp,sja1105q", .data = &sja1105q_info },
2369 { .compatible = "nxp,sja1105r", .data = &sja1105r_info },
2370 { .compatible = "nxp,sja1105s", .data = &sja1105s_info },
2371 { /* sentinel */ },
2372};
2373MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
2374
2375static struct spi_driver sja1105_driver = {
2376 .driver = {
2377 .name = "sja1105",
2378 .owner = THIS_MODULE,
2379 .of_match_table = of_match_ptr(sja1105_dt_ids),
2380 },
2381 .probe = sja1105_probe,
2382 .remove = sja1105_remove,
2383};
2384
2385module_spi_driver(sja1105_driver);
2386
2387MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
2388MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
2389MODULE_DESCRIPTION("SJA1105 Driver");
2390MODULE_LICENSE("GPL v2");