1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
3 * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
14 #include <linux/phylink.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>
23 #include <linux/dsa/8021q.h>
25 #include "sja1105_sgmii.h"
26 #include "sja1105_tas.h"
28 #define SJA1105_UNKNOWN_MULTICAST 0x010000000000ull
29 #define SJA1105_DEFAULT_VLAN (VLAN_N_VID - 1)
31 static const struct dsa_switch_ops sja1105_switch_ops;
33 static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
34 unsigned int startup_delay)
36 gpiod_set_value_cansleep(gpio, 1);
37 /* Wait for minimum reset pulse length */
39 gpiod_set_value_cansleep(gpio, 0);
40 /* Wait until chip is ready after reset */
41 msleep(startup_delay);
45 sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
46 int from, int to, bool allow)
49 l2_fwd[from].reach_port |= BIT(to);
51 l2_fwd[from].reach_port &= ~BIT(to);
54 static bool sja1105_can_forward(struct sja1105_l2_forwarding_entry *l2_fwd,
57 return !!(l2_fwd[from].reach_port & BIT(to));
60 /* Structure used to temporarily transport device tree
61 * settings into sja1105_setup
63 struct sja1105_dt_port {
64 phy_interface_t phy_mode;
65 sja1105_mii_role_t role;
68 static int sja1105_init_mac_settings(struct sja1105_private *priv)
70 struct sja1105_mac_config_entry default_mac = {
71 /* Enable all 8 priority queues on egress.
72 * Every queue i holds top[i] - base[i] frames.
73 * Sum of top[i] - base[i] is 511 (max hardware limit).
75 .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
76 .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
77 .enabled = {true, true, true, true, true, true, true, true},
78 /* Keep standard IFG of 12 bytes on egress. */
80 /* Always put the MAC speed in automatic mode, where it can be
81 * adjusted at runtime by PHYLINK.
83 .speed = priv->info->port_speed[SJA1105_SPEED_AUTO],
84 /* No static correction for 1-step 1588 events */
87 /* Disable aging for critical TTEthernet traffic */
89 /* Internal VLAN (pvid) to apply to untagged ingress */
94 /* Don't drop traffic with other EtherType than ETH_P_IP */
96 /* Don't drop double-tagged traffic */
98 /* Don't drop untagged traffic */
100 /* Don't retag 802.1p (VID 0) traffic with the pvid */
102 /* Disable learning and I/O on user ports by default -
103 * STP will enable it.
109 struct sja1105_mac_config_entry *mac;
110 struct dsa_switch *ds = priv->ds;
111 struct sja1105_table *table;
114 table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
116 /* Discard previous MAC Configuration Table */
117 if (table->entry_count) {
118 kfree(table->entries);
119 table->entry_count = 0;
122 table->entries = kcalloc(table->ops->max_entry_count,
123 table->ops->unpacked_entry_size, GFP_KERNEL);
127 table->entry_count = table->ops->max_entry_count;
129 mac = table->entries;
131 for (i = 0; i < ds->num_ports; i++) {
132 mac[i] = default_mac;
133 if (i == dsa_upstream_port(priv->ds, i)) {
134 /* STP doesn't get called for CPU port, so we need to
135 * set the I/O parameters statically.
137 mac[i].dyn_learn = true;
138 mac[i].ingress = true;
139 mac[i].egress = true;
146 static int sja1105_init_mii_settings(struct sja1105_private *priv,
147 struct sja1105_dt_port *ports)
149 struct device *dev = &priv->spidev->dev;
150 struct sja1105_xmii_params_entry *mii;
151 struct dsa_switch *ds = priv->ds;
152 struct sja1105_table *table;
155 table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
157 /* Discard previous xMII Mode Parameters Table */
158 if (table->entry_count) {
159 kfree(table->entries);
160 table->entry_count = 0;
163 table->entries = kcalloc(table->ops->max_entry_count,
164 table->ops->unpacked_entry_size, GFP_KERNEL);
168 /* Override table based on PHYLINK DT bindings */
169 table->entry_count = table->ops->max_entry_count;
171 mii = table->entries;
173 for (i = 0; i < ds->num_ports; i++) {
174 if (dsa_is_unused_port(priv->ds, i))
177 switch (ports[i].phy_mode) {
178 case PHY_INTERFACE_MODE_MII:
179 if (!priv->info->supports_mii[i])
182 mii->xmii_mode[i] = XMII_MODE_MII;
184 case PHY_INTERFACE_MODE_RMII:
185 if (!priv->info->supports_rmii[i])
188 mii->xmii_mode[i] = XMII_MODE_RMII;
190 case PHY_INTERFACE_MODE_RGMII:
191 case PHY_INTERFACE_MODE_RGMII_ID:
192 case PHY_INTERFACE_MODE_RGMII_RXID:
193 case PHY_INTERFACE_MODE_RGMII_TXID:
194 if (!priv->info->supports_rgmii[i])
197 mii->xmii_mode[i] = XMII_MODE_RGMII;
199 case PHY_INTERFACE_MODE_SGMII:
200 if (!priv->info->supports_sgmii[i])
203 mii->xmii_mode[i] = XMII_MODE_SGMII;
205 case PHY_INTERFACE_MODE_2500BASEX:
206 if (!priv->info->supports_2500basex[i])
209 mii->xmii_mode[i] = XMII_MODE_SGMII;
213 dev_err(dev, "Unsupported PHY mode %s on port %d!\n",
214 phy_modes(ports[i].phy_mode), i);
218 /* Even though the SerDes port is able to drive SGMII autoneg
219 * like a PHY would, from the perspective of the XMII tables,
220 * the SGMII port should always be put in MAC mode.
222 if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII)
223 mii->phy_mac[i] = XMII_MAC;
225 mii->phy_mac[i] = ports[i].role;
230 static int sja1105_init_static_fdb(struct sja1105_private *priv)
232 struct sja1105_l2_lookup_entry *l2_lookup;
233 struct sja1105_table *table;
236 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
238 /* We only populate the FDB table through dynamic L2 Address Lookup
239 * entries, except for a special entry at the end which is a catch-all
240 * for unknown multicast and will be used to control flooding domain.
242 if (table->entry_count) {
243 kfree(table->entries);
244 table->entry_count = 0;
247 if (!priv->info->can_limit_mcast_flood)
250 table->entries = kcalloc(1, table->ops->unpacked_entry_size,
255 table->entry_count = 1;
256 l2_lookup = table->entries;
258 /* All L2 multicast addresses have an odd first octet */
259 l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST;
260 l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST;
261 l2_lookup[0].lockeds = true;
262 l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1;
264 /* Flood multicast to every port by default */
265 for (port = 0; port < priv->ds->num_ports; port++)
266 if (!dsa_is_unused_port(priv->ds, port))
267 l2_lookup[0].destports |= BIT(port);
272 static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
274 struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
275 /* Learned FDB entries are forgotten after 300 seconds */
276 .maxage = SJA1105_AGEING_TIME_MS(300000),
277 /* All entries within a FDB bin are available for learning */
278 .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
279 /* And the P/Q/R/S equivalent setting: */
281 /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
283 /* This selects between Independent VLAN Learning (IVL) and
284 * Shared VLAN Learning (SVL)
286 .shared_learn = true,
287 /* Don't discard management traffic based on ENFPORT -
288 * we don't perform SMAC port enforcement anyway, so
289 * what we are setting here doesn't matter.
291 .no_enf_hostprt = false,
292 /* Don't learn SMAC for mac_fltres1 and mac_fltres0.
293 * Maybe correlate with no_linklocal_learn from bridge driver?
295 .no_mgmt_learn = true,
298 /* Dynamically learned FDB entries can overwrite other (older)
299 * dynamic FDB entries
304 struct dsa_switch *ds = priv->ds;
305 int port, num_used_ports = 0;
306 struct sja1105_table *table;
309 for (port = 0; port < ds->num_ports; port++)
310 if (!dsa_is_unused_port(ds, port))
313 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports;
315 for (port = 0; port < ds->num_ports; port++) {
316 if (dsa_is_unused_port(ds, port))
319 default_l2_lookup_params.maxaddrp[port] = max_fdb_entries;
322 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
324 if (table->entry_count) {
325 kfree(table->entries);
326 table->entry_count = 0;
329 table->entries = kcalloc(table->ops->max_entry_count,
330 table->ops->unpacked_entry_size, GFP_KERNEL);
334 table->entry_count = table->ops->max_entry_count;
336 /* This table only has a single entry */
337 ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
338 default_l2_lookup_params;
343 /* Set up a default VLAN for untagged traffic injected from the CPU
344 * using management routes (e.g. STP, PTP) as opposed to tag_8021q.
345 * All DT-defined ports are members of this VLAN, and there are no
346 * restrictions on forwarding (since the CPU selects the destination).
347 * Frames from this VLAN will always be transmitted as untagged, and
348 * neither the bridge nor the 8021q module cannot create this VLAN ID.
350 static int sja1105_init_static_vlan(struct sja1105_private *priv)
352 struct sja1105_table *table;
353 struct sja1105_vlan_lookup_entry pvid = {
359 .vlanid = SJA1105_DEFAULT_VLAN,
361 struct dsa_switch *ds = priv->ds;
364 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
366 if (table->entry_count) {
367 kfree(table->entries);
368 table->entry_count = 0;
371 table->entries = kzalloc(table->ops->unpacked_entry_size,
376 table->entry_count = 1;
378 for (port = 0; port < ds->num_ports; port++) {
379 struct sja1105_bridge_vlan *v;
381 if (dsa_is_unused_port(ds, port))
384 pvid.vmemb_port |= BIT(port);
385 pvid.vlan_bc |= BIT(port);
386 pvid.tag_port &= ~BIT(port);
388 v = kzalloc(sizeof(*v), GFP_KERNEL);
393 v->vid = SJA1105_DEFAULT_VLAN;
395 if (dsa_is_cpu_port(ds, port))
397 list_add(&v->list, &priv->dsa_8021q_vlans);
400 ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
404 static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
406 struct sja1105_l2_forwarding_entry *l2fwd;
407 struct dsa_switch *ds = priv->ds;
408 struct sja1105_table *table;
411 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
413 if (table->entry_count) {
414 kfree(table->entries);
415 table->entry_count = 0;
418 table->entries = kcalloc(table->ops->max_entry_count,
419 table->ops->unpacked_entry_size, GFP_KERNEL);
423 table->entry_count = table->ops->max_entry_count;
425 l2fwd = table->entries;
427 /* First 5 entries define the forwarding rules */
428 for (i = 0; i < ds->num_ports; i++) {
429 unsigned int upstream = dsa_upstream_port(priv->ds, i);
431 if (dsa_is_unused_port(ds, i))
434 for (j = 0; j < SJA1105_NUM_TC; j++)
435 l2fwd[i].vlan_pmap[j] = j;
437 /* All ports start up with egress flooding enabled,
438 * including the CPU port.
440 priv->ucast_egress_floods |= BIT(i);
441 priv->bcast_egress_floods |= BIT(i);
446 sja1105_port_allow_traffic(l2fwd, i, upstream, true);
447 sja1105_port_allow_traffic(l2fwd, upstream, i, true);
449 l2fwd[i].bc_domain = BIT(upstream);
450 l2fwd[i].fl_domain = BIT(upstream);
452 l2fwd[upstream].bc_domain |= BIT(i);
453 l2fwd[upstream].fl_domain |= BIT(i);
456 /* Next 8 entries define VLAN PCP mapping from ingress to egress.
457 * Create a one-to-one mapping.
459 for (i = 0; i < SJA1105_NUM_TC; i++) {
460 for (j = 0; j < ds->num_ports; j++) {
461 if (dsa_is_unused_port(ds, j))
464 l2fwd[ds->num_ports + i].vlan_pmap[j] = i;
471 static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
473 struct sja1105_l2_forwarding_params_entry *l2fwd_params;
474 struct sja1105_table *table;
476 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
478 if (table->entry_count) {
479 kfree(table->entries);
480 table->entry_count = 0;
483 table->entries = kcalloc(table->ops->max_entry_count,
484 table->ops->unpacked_entry_size, GFP_KERNEL);
488 table->entry_count = table->ops->max_entry_count;
490 /* This table only has a single entry */
491 l2fwd_params = table->entries;
493 /* Disallow dynamic reconfiguration of vlan_pmap */
494 l2fwd_params->max_dynp = 0;
495 /* Use a single memory partition for all ingress queues */
496 l2fwd_params->part_spc[0] = priv->info->max_frame_mem;
501 void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
503 struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
504 struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
505 int max_mem = priv->info->max_frame_mem;
506 struct sja1105_table *table;
508 /* VLAN retagging is implemented using a loopback port that consumes
509 * frame buffers. That leaves less for us.
511 if (priv->vlan_state == SJA1105_VLAN_BEST_EFFORT)
512 max_mem -= SJA1105_FRAME_MEMORY_RETAGGING_OVERHEAD;
514 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
515 l2_fwd_params = table->entries;
516 l2_fwd_params->part_spc[0] = max_mem;
518 /* If we have any critical-traffic virtual links, we need to reserve
519 * some frame buffer memory for them. At the moment, hardcode the value
520 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
521 * remaining for best-effort traffic. TODO: figure out a more flexible
522 * way to perform the frame buffer partitioning.
524 if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
527 table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
528 vl_fwd_params = table->entries;
530 l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
531 vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
534 static int sja1105_init_general_params(struct sja1105_private *priv)
536 struct sja1105_general_params_entry default_general_params = {
537 /* Allow dynamic changing of the mirror port */
539 .switchid = priv->ds->index,
540 /* Priority queue for link-local management frames
541 * (both ingress to and egress from CPU - PTP, STP etc)
544 .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
545 .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK,
546 .incl_srcpt1 = false,
548 .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
549 .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK,
550 .incl_srcpt0 = false,
552 /* The destination for traffic matching mac_fltres1 and
553 * mac_fltres0 on all ports except host_port. Such traffic
554 * receieved on host_port itself would be dropped, except
555 * by installing a temporary 'management route'
557 .host_port = priv->ds->num_ports,
558 /* Default to an invalid value */
559 .mirr_port = priv->ds->num_ports,
560 /* Link-local traffic received on casc_port will be forwarded
561 * to host_port without embedding the source port and device ID
562 * info in the destination MAC address (presumably because it
563 * is a cascaded port and a downstream SJA switch already did
564 * that). Default to an invalid port (to disable the feature)
565 * and overwrite this if we find any DSA (cascaded) ports.
567 .casc_port = priv->ds->num_ports,
569 .vllupformat = SJA1105_VL_FORMAT_PSFP,
572 /* Only update correctionField for 1-step PTP (L2 transport) */
574 /* Forcefully disable VLAN filtering by telling
575 * the switch that VLAN has a different EtherType.
577 .tpid = ETH_P_SJA1105,
578 .tpid2 = ETH_P_SJA1105,
580 struct dsa_switch *ds = priv->ds;
581 struct sja1105_table *table;
584 for (port = 0; port < ds->num_ports; port++) {
585 if (dsa_is_cpu_port(ds, port)) {
586 default_general_params.host_port = port;
591 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
593 if (table->entry_count) {
594 kfree(table->entries);
595 table->entry_count = 0;
598 table->entries = kcalloc(table->ops->max_entry_count,
599 table->ops->unpacked_entry_size, GFP_KERNEL);
603 table->entry_count = table->ops->max_entry_count;
605 /* This table only has a single entry */
606 ((struct sja1105_general_params_entry *)table->entries)[0] =
607 default_general_params;
612 static int sja1105_init_avb_params(struct sja1105_private *priv)
614 struct sja1105_avb_params_entry *avb;
615 struct sja1105_table *table;
617 table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
619 /* Discard previous AVB Parameters Table */
620 if (table->entry_count) {
621 kfree(table->entries);
622 table->entry_count = 0;
625 table->entries = kcalloc(table->ops->max_entry_count,
626 table->ops->unpacked_entry_size, GFP_KERNEL);
630 table->entry_count = table->ops->max_entry_count;
632 avb = table->entries;
634 /* Configure the MAC addresses for meta frames */
635 avb->destmeta = SJA1105_META_DMAC;
636 avb->srcmeta = SJA1105_META_SMAC;
637 /* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
638 * default. This is because there might be boards with a hardware
639 * layout where enabling the pin as output might cause an electrical
640 * clash. On E/T the pin is always an output, which the board designers
641 * probably already knew, so even if there are going to be electrical
642 * issues, there's nothing we can do.
644 avb->cas_master = false;
649 /* The L2 policing table is 2-stage. The table is looked up for each frame
650 * according to the ingress port, whether it was broadcast or not, and the
651 * classified traffic class (given by VLAN PCP). This portion of the lookup is
652 * fixed, and gives access to the SHARINDX, an indirection register pointing
653 * within the policing table itself, which is used to resolve the policer that
654 * will be used for this frame.
657 * +------------+--------+ +---------------------------------+
658 * |Port 0 TC 0 |SHARINDX| | Policer 0: Rate, Burst, MTU |
659 * +------------+--------+ +---------------------------------+
660 * |Port 0 TC 1 |SHARINDX| | Policer 1: Rate, Burst, MTU |
661 * +------------+--------+ +---------------------------------+
662 * ... | Policer 2: Rate, Burst, MTU |
663 * +------------+--------+ +---------------------------------+
664 * |Port 0 TC 7 |SHARINDX| | Policer 3: Rate, Burst, MTU |
665 * +------------+--------+ +---------------------------------+
666 * |Port 1 TC 0 |SHARINDX| | Policer 4: Rate, Burst, MTU |
667 * +------------+--------+ +---------------------------------+
668 * ... | Policer 5: Rate, Burst, MTU |
669 * +------------+--------+ +---------------------------------+
670 * |Port 1 TC 7 |SHARINDX| | Policer 6: Rate, Burst, MTU |
671 * +------------+--------+ +---------------------------------+
672 * ... | Policer 7: Rate, Burst, MTU |
673 * +------------+--------+ +---------------------------------+
674 * |Port 4 TC 7 |SHARINDX| ...
675 * +------------+--------+
676 * |Port 0 BCAST|SHARINDX| ...
677 * +------------+--------+
678 * |Port 1 BCAST|SHARINDX| ...
679 * +------------+--------+
681 * +------------+--------+ +---------------------------------+
682 * |Port 4 BCAST|SHARINDX| | Policer 44: Rate, Burst, MTU |
683 * +------------+--------+ +---------------------------------+
685 * In this driver, we shall use policers 0-4 as statically alocated port
686 * (matchall) policers. So we need to make the SHARINDX for all lookups
687 * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
689 * The remaining policers (40) shall be dynamically allocated for flower
690 * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
692 #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
694 static int sja1105_init_l2_policing(struct sja1105_private *priv)
696 struct sja1105_l2_policing_entry *policing;
697 struct dsa_switch *ds = priv->ds;
698 struct sja1105_table *table;
701 table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
703 /* Discard previous L2 Policing Table */
704 if (table->entry_count) {
705 kfree(table->entries);
706 table->entry_count = 0;
709 table->entries = kcalloc(table->ops->max_entry_count,
710 table->ops->unpacked_entry_size, GFP_KERNEL);
714 table->entry_count = table->ops->max_entry_count;
716 policing = table->entries;
718 /* Setup shared indices for the matchall policers */
719 for (port = 0; port < ds->num_ports; port++) {
720 int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port;
721 int bcast = (ds->num_ports * SJA1105_NUM_TC) + port;
723 for (tc = 0; tc < SJA1105_NUM_TC; tc++)
724 policing[port * SJA1105_NUM_TC + tc].sharindx = port;
726 policing[bcast].sharindx = port;
727 /* Only SJA1110 has multicast policers */
728 if (mcast <= table->ops->max_entry_count)
729 policing[mcast].sharindx = port;
732 /* Setup the matchall policer parameters */
733 for (port = 0; port < ds->num_ports; port++) {
734 int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
736 if (dsa_is_cpu_port(priv->ds, port))
739 policing[port].smax = 65535; /* Burst size in bytes */
740 policing[port].rate = SJA1105_RATE_MBPS(1000);
741 policing[port].maxlen = mtu;
742 policing[port].partition = 0;
748 static int sja1105_static_config_load(struct sja1105_private *priv,
749 struct sja1105_dt_port *ports)
753 sja1105_static_config_free(&priv->static_config);
754 rc = sja1105_static_config_init(&priv->static_config,
755 priv->info->static_ops,
756 priv->info->device_id);
760 /* Build static configuration */
761 rc = sja1105_init_mac_settings(priv);
764 rc = sja1105_init_mii_settings(priv, ports);
767 rc = sja1105_init_static_fdb(priv);
770 rc = sja1105_init_static_vlan(priv);
773 rc = sja1105_init_l2_lookup_params(priv);
776 rc = sja1105_init_l2_forwarding(priv);
779 rc = sja1105_init_l2_forwarding_params(priv);
782 rc = sja1105_init_l2_policing(priv);
785 rc = sja1105_init_general_params(priv);
788 rc = sja1105_init_avb_params(priv);
792 /* Send initial configuration to hardware via SPI */
793 return sja1105_static_config_upload(priv);
796 static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
797 const struct sja1105_dt_port *ports)
799 struct dsa_switch *ds = priv->ds;
802 for (i = 0; i < ds->num_ports; i++) {
803 if (ports[i].role == XMII_MAC)
806 if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
807 ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
808 priv->rgmii_rx_delay[i] = true;
810 if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
811 ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
812 priv->rgmii_tx_delay[i] = true;
814 if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
815 !priv->info->setup_rgmii_delay)
821 static int sja1105_parse_ports_node(struct sja1105_private *priv,
822 struct sja1105_dt_port *ports,
823 struct device_node *ports_node)
825 struct device *dev = &priv->spidev->dev;
826 struct device_node *child;
828 for_each_available_child_of_node(ports_node, child) {
829 struct device_node *phy_node;
830 phy_interface_t phy_mode;
834 /* Get switch port number from DT */
835 if (of_property_read_u32(child, "reg", &index) < 0) {
836 dev_err(dev, "Port number not defined in device tree "
837 "(property \"reg\")\n");
842 /* Get PHY mode from DT */
843 err = of_get_phy_mode(child, &phy_mode);
845 dev_err(dev, "Failed to read phy-mode or "
846 "phy-interface-type property for port %d\n",
851 ports[index].phy_mode = phy_mode;
853 phy_node = of_parse_phandle(child, "phy-handle", 0);
855 if (!of_phy_is_fixed_link(child)) {
856 dev_err(dev, "phy-handle or fixed-link "
857 "properties missing!\n");
861 /* phy-handle is missing, but fixed-link isn't.
862 * So it's a fixed link. Default to PHY role.
864 ports[index].role = XMII_PHY;
866 /* phy-handle present => put port in MAC role */
867 ports[index].role = XMII_MAC;
868 of_node_put(phy_node);
871 /* The MAC/PHY role can be overridden with explicit bindings */
872 if (of_property_read_bool(child, "sja1105,role-mac"))
873 ports[index].role = XMII_MAC;
874 else if (of_property_read_bool(child, "sja1105,role-phy"))
875 ports[index].role = XMII_PHY;
877 priv->phy_mode[index] = phy_mode;
883 static int sja1105_parse_dt(struct sja1105_private *priv,
884 struct sja1105_dt_port *ports)
886 struct device *dev = &priv->spidev->dev;
887 struct device_node *switch_node = dev->of_node;
888 struct device_node *ports_node;
891 ports_node = of_get_child_by_name(switch_node, "ports");
893 ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
895 dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
899 rc = sja1105_parse_ports_node(priv, ports, ports_node);
900 of_node_put(ports_node);
905 static int sja1105_sgmii_read(struct sja1105_private *priv, int port, int mmd,
908 u64 addr = (mmd << 16) | pcs_reg;
912 if (port != SJA1105_SGMII_PORT)
915 rc = sja1105_xfer_u32(priv, SPI_READ, addr, &val, NULL);
922 static int sja1105_sgmii_write(struct sja1105_private *priv, int port, int mmd,
923 int pcs_reg, u16 pcs_val)
925 u64 addr = (mmd << 16) | pcs_reg;
929 if (port != SJA1105_SGMII_PORT)
932 rc = sja1105_xfer_u32(priv, SPI_WRITE, addr, &val, NULL);
939 static void sja1105_sgmii_pcs_config(struct sja1105_private *priv, int port,
940 bool an_enabled, bool an_master)
942 u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII;
944 /* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
945 * stop the clock during LPI mode, make the MAC reconfigure
946 * autonomously after PCS autoneg is done, flush the internal FIFOs.
948 sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_DC1,
949 SJA1105_DC1_EN_VSMMD1 |
950 SJA1105_DC1_CLOCK_STOP_EN |
951 SJA1105_DC1_MAC_AUTO_SW |
953 /* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
954 sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_DC2,
955 SJA1105_DC2_TX_POL_INV_DISABLE);
956 /* AUTONEG_CONTROL: Use SGMII autoneg */
958 ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK;
959 sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_AC, ac);
960 /* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
961 * sja1105_sgmii_pcs_force_speed must be called later for the link
962 * to become operational.
965 sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1,
966 BMCR_ANENABLE | BMCR_ANRESTART);
969 static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv,
976 pcs_speed = BMCR_SPEED1000;
979 pcs_speed = BMCR_SPEED100;
982 pcs_speed = BMCR_SPEED10;
985 dev_err(priv->ds->dev, "Invalid speed %d\n", speed);
988 sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1,
989 pcs_speed | BMCR_FULLDPLX);
992 /* Convert link speed from SJA1105 to ethtool encoding */
993 static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv,
996 if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS])
998 if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS])
1000 if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS])
1002 if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS])
1004 return SPEED_UNKNOWN;
1007 /* Set link speed in the MAC configuration for a specific port. */
1008 static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
1011 struct sja1105_mac_config_entry *mac;
1012 struct device *dev = priv->ds->dev;
1016 /* On P/Q/R/S, one can read from the device via the MAC reconfiguration
1017 * tables. On E/T, MAC reconfig tables are not readable, only writable.
1018 * We have to *know* what the MAC looks like. For the sake of keeping
1019 * the code common, we'll use the static configuration tables as a
1020 * reasonable approximation for both E/T and P/Q/R/S.
1022 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1024 switch (speed_mbps) {
1026 /* PHYLINK called sja1105_mac_config() to inform us about
1027 * the state->interface, but AN has not completed and the
1028 * speed is not yet valid. UM10944.pdf says that setting
1029 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
1030 * ok for power consumption in case AN will never complete -
1031 * otherwise PHYLINK should come back with a new update.
1033 speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
1036 speed = priv->info->port_speed[SJA1105_SPEED_10MBPS];
1039 speed = priv->info->port_speed[SJA1105_SPEED_100MBPS];
1042 speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
1045 dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
1049 /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
1050 * table, since this will be used for the clocking setup, and we no
1051 * longer need to store it in the static config (already told hardware
1052 * we want auto during upload phase).
1053 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
1054 * we need to configure the PCS only (if even that).
1056 if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII)
1057 mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
1059 mac[port].speed = speed;
1061 /* Write to the dynamic reconfiguration tables */
1062 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1065 dev_err(dev, "Failed to write MAC config: %d\n", rc);
1069 /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
1070 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
1071 * RMII no change of the clock setup is required. Actually, changing
1072 * the clock setup does interrupt the clock signal for a certain time
1073 * which causes trouble for all PHYs relying on this signal.
1075 if (!phy_interface_mode_is_rgmii(priv->phy_mode[port]))
1078 return sja1105_clocking_setup_port(priv, port);
1081 /* The SJA1105 MAC programming model is through the static config (the xMII
1082 * Mode table cannot be dynamically reconfigured), and we have to program
1083 * that early (earlier than PHYLINK calls us, anyway).
1084 * So just error out in case the connected PHY attempts to change the initial
1085 * system interface MII protocol from what is defined in the DT, at least for
1088 static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
1089 phy_interface_t interface)
1091 return priv->phy_mode[port] != interface;
1094 static void sja1105_mac_config(struct dsa_switch *ds, int port,
1096 const struct phylink_link_state *state)
1098 struct sja1105_private *priv = ds->priv;
1101 is_sgmii = (state->interface == PHY_INTERFACE_MODE_SGMII);
1103 if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1104 dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
1105 phy_modes(state->interface));
1109 if (phylink_autoneg_inband(mode) && !is_sgmii) {
1110 dev_err(ds->dev, "In-band AN not supported!\n");
1115 sja1105_sgmii_pcs_config(priv, port,
1116 phylink_autoneg_inband(mode),
1120 static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
1122 phy_interface_t interface)
1124 sja1105_inhibit_tx(ds->priv, BIT(port), true);
1127 static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
1129 phy_interface_t interface,
1130 struct phy_device *phydev,
1131 int speed, int duplex,
1132 bool tx_pause, bool rx_pause)
1134 struct sja1105_private *priv = ds->priv;
1136 sja1105_adjust_port_config(priv, port, speed);
1138 if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII &&
1139 !phylink_autoneg_inband(mode))
1140 sja1105_sgmii_pcs_force_speed(priv, port, speed);
1142 sja1105_inhibit_tx(priv, BIT(port), false);
1145 static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1146 unsigned long *supported,
1147 struct phylink_link_state *state)
1149 /* Construct a new mask which exhaustively contains all link features
1150 * supported by the MAC, and then apply that (logical AND) to what will
1151 * be sent to the PHY for "marketing".
1153 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1154 struct sja1105_private *priv = ds->priv;
1155 struct sja1105_xmii_params_entry *mii;
1157 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1159 /* include/linux/phylink.h says:
1160 * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
1161 * expects the MAC driver to return all supported link modes.
1163 if (state->interface != PHY_INTERFACE_MODE_NA &&
1164 sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1165 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
1169 /* The MAC does not support pause frames, and also doesn't
1170 * support half-duplex traffic modes.
1172 phylink_set(mask, Autoneg);
1173 phylink_set(mask, MII);
1174 phylink_set(mask, 10baseT_Full);
1175 phylink_set(mask, 100baseT_Full);
1176 phylink_set(mask, 100baseT1_Full);
1177 if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1178 mii->xmii_mode[port] == XMII_MODE_SGMII)
1179 phylink_set(mask, 1000baseT_Full);
1181 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1182 bitmap_and(state->advertising, state->advertising, mask,
1183 __ETHTOOL_LINK_MODE_MASK_NBITS);
1186 static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port,
1187 struct phylink_link_state *state)
1189 struct sja1105_private *priv = ds->priv;
1192 /* Read the vendor-specific AUTONEG_INTR_STATUS register */
1193 ais = sja1105_sgmii_read(priv, port, MDIO_MMD_VEND2, SJA1105_AIS);
1197 switch (SJA1105_AIS_SPEED(ais)) {
1199 state->speed = SPEED_10;
1202 state->speed = SPEED_100;
1205 state->speed = SPEED_1000;
1208 dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n",
1209 SJA1105_AIS_SPEED(ais));
1211 state->duplex = SJA1105_AIS_DUPLEX_MODE(ais);
1212 state->an_complete = SJA1105_AIS_COMPLETE(ais);
1213 state->link = SJA1105_AIS_LINK_STATUS(ais);
1219 sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
1220 const struct sja1105_l2_lookup_entry *requested)
1222 struct sja1105_l2_lookup_entry *l2_lookup;
1223 struct sja1105_table *table;
1226 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1227 l2_lookup = table->entries;
1229 for (i = 0; i < table->entry_count; i++)
1230 if (l2_lookup[i].macaddr == requested->macaddr &&
1231 l2_lookup[i].vlanid == requested->vlanid &&
1232 l2_lookup[i].destports & BIT(port))
1238 /* We want FDB entries added statically through the bridge command to persist
1239 * across switch resets, which are a common thing during normal SJA1105
1240 * operation. So we have to back them up in the static configuration tables
1241 * and hence apply them on next static config upload... yay!
1244 sja1105_static_fdb_change(struct sja1105_private *priv, int port,
1245 const struct sja1105_l2_lookup_entry *requested,
1248 struct sja1105_l2_lookup_entry *l2_lookup;
1249 struct sja1105_table *table;
1252 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1254 match = sja1105_find_static_fdb_entry(priv, port, requested);
1256 /* Can't delete a missing entry. */
1260 /* No match => new entry */
1261 rc = sja1105_table_resize(table, table->entry_count + 1);
1265 match = table->entry_count - 1;
1268 /* Assign pointer after the resize (it may be new memory) */
1269 l2_lookup = table->entries;
1272 * If the job was to add this FDB entry, it's already done (mostly
1273 * anyway, since the port forwarding mask may have changed, case in
1274 * which we update it).
1275 * Otherwise we have to delete it.
1278 l2_lookup[match] = *requested;
1282 /* To remove, the strategy is to overwrite the element with
1283 * the last one, and then reduce the array size by 1
1285 l2_lookup[match] = l2_lookup[table->entry_count - 1];
1286 return sja1105_table_resize(table, table->entry_count - 1);
1289 /* First-generation switches have a 4-way set associative TCAM that
1290 * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1291 * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1292 * For the placement of a newly learnt FDB entry, the switch selects the bin
1293 * based on a hash function, and the way within that bin incrementally.
1295 static int sja1105et_fdb_index(int bin, int way)
1297 return bin * SJA1105ET_FDB_BIN_SIZE + way;
1300 static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1301 const u8 *addr, u16 vid,
1302 struct sja1105_l2_lookup_entry *match,
1307 for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1308 struct sja1105_l2_lookup_entry l2_lookup = {0};
1309 int index = sja1105et_fdb_index(bin, way);
1311 /* Skip unused entries, optionally marking them
1312 * into the return value
1314 if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1315 index, &l2_lookup)) {
1321 if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1322 l2_lookup.vlanid == vid) {
1328 /* Return an invalid entry index if not found */
1332 int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1333 const unsigned char *addr, u16 vid)
1335 struct sja1105_l2_lookup_entry l2_lookup = {0};
1336 struct sja1105_private *priv = ds->priv;
1337 struct device *dev = ds->dev;
1338 int last_unused = -1;
1341 bin = sja1105et_fdb_hash(priv, addr, vid);
1343 way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1344 &l2_lookup, &last_unused);
1346 /* We have an FDB entry. Is our port in the destination
1347 * mask? If yes, we need to do nothing. If not, we need
1348 * to rewrite the entry by adding this port to it.
1350 if (l2_lookup.destports & BIT(port))
1352 l2_lookup.destports |= BIT(port);
1354 int index = sja1105et_fdb_index(bin, way);
1356 /* We don't have an FDB entry. We construct a new one and
1357 * try to find a place for it within the FDB table.
1359 l2_lookup.macaddr = ether_addr_to_u64(addr);
1360 l2_lookup.destports = BIT(port);
1361 l2_lookup.vlanid = vid;
1363 if (last_unused >= 0) {
1366 /* Bin is full, need to evict somebody.
1367 * Choose victim at random. If you get these messages
1368 * often, you may need to consider changing the
1369 * distribution function:
1370 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1372 get_random_bytes(&way, sizeof(u8));
1373 way %= SJA1105ET_FDB_BIN_SIZE;
1374 dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1377 sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1378 index, NULL, false);
1381 l2_lookup.index = sja1105et_fdb_index(bin, way);
1383 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1384 l2_lookup.index, &l2_lookup,
1389 return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1392 int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1393 const unsigned char *addr, u16 vid)
1395 struct sja1105_l2_lookup_entry l2_lookup = {0};
1396 struct sja1105_private *priv = ds->priv;
1397 int index, bin, way, rc;
1400 bin = sja1105et_fdb_hash(priv, addr, vid);
1401 way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1405 index = sja1105et_fdb_index(bin, way);
1407 /* We have an FDB entry. Is our port in the destination mask? If yes,
1408 * we need to remove it. If the resulting port mask becomes empty, we
1409 * need to completely evict the FDB entry.
1410 * Otherwise we just write it back.
1412 l2_lookup.destports &= ~BIT(port);
1414 if (l2_lookup.destports)
1419 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1420 index, &l2_lookup, keep);
1424 return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1427 int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
1428 const unsigned char *addr, u16 vid)
1430 struct sja1105_l2_lookup_entry l2_lookup = {0};
1431 struct sja1105_private *priv = ds->priv;
1434 /* Search for an existing entry in the FDB table */
1435 l2_lookup.macaddr = ether_addr_to_u64(addr);
1436 l2_lookup.vlanid = vid;
1437 l2_lookup.iotag = SJA1105_S_TAG;
1438 l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
1439 if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
1440 l2_lookup.mask_vlanid = VLAN_VID_MASK;
1441 l2_lookup.mask_iotag = BIT(0);
1443 l2_lookup.mask_vlanid = 0;
1444 l2_lookup.mask_iotag = 0;
1446 l2_lookup.destports = BIT(port);
1448 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1449 SJA1105_SEARCH, &l2_lookup);
1451 /* Found and this port is already in the entry's
1452 * port mask => job done
1454 if (l2_lookup.destports & BIT(port))
1456 /* l2_lookup.index is populated by the switch in case it
1459 l2_lookup.destports |= BIT(port);
1460 goto skip_finding_an_index;
1463 /* Not found, so try to find an unused spot in the FDB.
1464 * This is slightly inefficient because the strategy is knock-knock at
1465 * every possible position from 0 to 1023.
1467 for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1468 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1473 if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
1474 dev_err(ds->dev, "FDB is full, cannot add entry.\n");
1477 l2_lookup.lockeds = true;
1478 l2_lookup.index = i;
1480 skip_finding_an_index:
1481 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1482 l2_lookup.index, &l2_lookup,
1487 return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1490 int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
1491 const unsigned char *addr, u16 vid)
1493 struct sja1105_l2_lookup_entry l2_lookup = {0};
1494 struct sja1105_private *priv = ds->priv;
1498 l2_lookup.macaddr = ether_addr_to_u64(addr);
1499 l2_lookup.vlanid = vid;
1500 l2_lookup.iotag = SJA1105_S_TAG;
1501 l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
1502 if (priv->vlan_state != SJA1105_VLAN_UNAWARE) {
1503 l2_lookup.mask_vlanid = VLAN_VID_MASK;
1504 l2_lookup.mask_iotag = BIT(0);
1506 l2_lookup.mask_vlanid = 0;
1507 l2_lookup.mask_iotag = 0;
1509 l2_lookup.destports = BIT(port);
1511 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1512 SJA1105_SEARCH, &l2_lookup);
1516 l2_lookup.destports &= ~BIT(port);
1518 /* Decide whether we remove just this port from the FDB entry,
1519 * or if we remove it completely.
1521 if (l2_lookup.destports)
1526 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1527 l2_lookup.index, &l2_lookup, keep);
1531 return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1534 static int sja1105_fdb_add(struct dsa_switch *ds, int port,
1535 const unsigned char *addr, u16 vid)
1537 struct sja1105_private *priv = ds->priv;
1539 /* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
1540 * so the switch still does some VLAN processing internally.
1541 * But Shared VLAN Learning (SVL) is also active, and it will take
1542 * care of autonomous forwarding between the unique pvid's of each
1543 * port. Here we just make sure that users can't add duplicate FDB
1544 * entries when in this mode - the actual VID doesn't matter except
1545 * for what gets printed in 'bridge fdb show'. In the case of zero,
1546 * no VID gets printed at all.
1548 if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
1551 return priv->info->fdb_add_cmd(ds, port, addr, vid);
1554 static int sja1105_fdb_del(struct dsa_switch *ds, int port,
1555 const unsigned char *addr, u16 vid)
1557 struct sja1105_private *priv = ds->priv;
1559 if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
1562 return priv->info->fdb_del_cmd(ds, port, addr, vid);
1565 static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1566 dsa_fdb_dump_cb_t *cb, void *data)
1568 struct sja1105_private *priv = ds->priv;
1569 struct device *dev = ds->dev;
1572 for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1573 struct sja1105_l2_lookup_entry l2_lookup = {0};
1574 u8 macaddr[ETH_ALEN];
1577 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1579 /* No fdb entry at i, not an issue */
1583 dev_err(dev, "Failed to dump FDB: %d\n", rc);
1587 /* FDB dump callback is per port. This means we have to
1588 * disregard a valid entry if it's not for this port, even if
1589 * only to revisit it later. This is inefficient because the
1590 * 1024-sized FDB table needs to be traversed 4 times through
1591 * SPI during a 'bridge fdb show' command.
1593 if (!(l2_lookup.destports & BIT(port)))
1596 /* We need to hide the FDB entry for unknown multicast */
1597 if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST &&
1598 l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
1601 u64_to_ether_addr(l2_lookup.macaddr, macaddr);
1603 /* We need to hide the dsa_8021q VLANs from the user. */
1604 if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
1605 l2_lookup.vlanid = 0;
1606 cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1611 static int sja1105_mdb_add(struct dsa_switch *ds, int port,
1612 const struct switchdev_obj_port_mdb *mdb)
1614 return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1617 static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1618 const struct switchdev_obj_port_mdb *mdb)
1620 return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1623 /* Common function for unicast and broadcast flood configuration.
1624 * Flooding is configured between each {ingress, egress} port pair, and since
1625 * the bridge's semantics are those of "egress flooding", it means we must
1626 * enable flooding towards this port from all ingress ports that are in the
1627 * same forwarding domain.
1629 static int sja1105_manage_flood_domains(struct sja1105_private *priv)
1631 struct sja1105_l2_forwarding_entry *l2_fwd;
1632 struct dsa_switch *ds = priv->ds;
1635 l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
1637 for (from = 0; from < ds->num_ports; from++) {
1638 u64 fl_domain = 0, bc_domain = 0;
1640 for (to = 0; to < priv->ds->num_ports; to++) {
1641 if (!sja1105_can_forward(l2_fwd, from, to))
1644 if (priv->ucast_egress_floods & BIT(to))
1645 fl_domain |= BIT(to);
1646 if (priv->bcast_egress_floods & BIT(to))
1647 bc_domain |= BIT(to);
1650 /* Nothing changed, nothing to do */
1651 if (l2_fwd[from].fl_domain == fl_domain &&
1652 l2_fwd[from].bc_domain == bc_domain)
1655 l2_fwd[from].fl_domain = fl_domain;
1656 l2_fwd[from].bc_domain = bc_domain;
1658 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
1659 from, &l2_fwd[from], true);
1667 static int sja1105_bridge_member(struct dsa_switch *ds, int port,
1668 struct net_device *br, bool member)
1670 struct sja1105_l2_forwarding_entry *l2_fwd;
1671 struct sja1105_private *priv = ds->priv;
1674 l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
1676 for (i = 0; i < ds->num_ports; i++) {
1677 /* Add this port to the forwarding matrix of the
1678 * other ports in the same bridge, and viceversa.
1680 if (!dsa_is_user_port(ds, i))
1682 /* For the ports already under the bridge, only one thing needs
1683 * to be done, and that is to add this port to their
1684 * reachability domain. So we can perform the SPI write for
1685 * them immediately. However, for this port itself (the one
1686 * that is new to the bridge), we need to add all other ports
1687 * to its reachability domain. So we do that incrementally in
1688 * this loop, and perform the SPI write only at the end, once
1689 * the domain contains all other bridge ports.
1693 if (dsa_to_port(ds, i)->bridge_dev != br)
1695 sja1105_port_allow_traffic(l2_fwd, i, port, member);
1696 sja1105_port_allow_traffic(l2_fwd, port, i, member);
1698 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
1699 i, &l2_fwd[i], true);
1704 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
1705 port, &l2_fwd[port], true);
1709 return sja1105_manage_flood_domains(priv);
1712 static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1715 struct sja1105_private *priv = ds->priv;
1716 struct sja1105_mac_config_entry *mac;
1718 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1721 case BR_STATE_DISABLED:
1722 case BR_STATE_BLOCKING:
1723 /* From UM10944 description of DRPDTAG (why put this there?):
1724 * "Management traffic flows to the port regardless of the state
1725 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1726 * At the moment no difference between DISABLED and BLOCKING.
1728 mac[port].ingress = false;
1729 mac[port].egress = false;
1730 mac[port].dyn_learn = false;
1732 case BR_STATE_LISTENING:
1733 mac[port].ingress = true;
1734 mac[port].egress = false;
1735 mac[port].dyn_learn = false;
1737 case BR_STATE_LEARNING:
1738 mac[port].ingress = true;
1739 mac[port].egress = false;
1740 mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1742 case BR_STATE_FORWARDING:
1743 mac[port].ingress = true;
1744 mac[port].egress = true;
1745 mac[port].dyn_learn = !!(priv->learn_ena & BIT(port));
1748 dev_err(ds->dev, "invalid STP state: %d\n", state);
1752 sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1756 static int sja1105_bridge_join(struct dsa_switch *ds, int port,
1757 struct net_device *br)
1759 return sja1105_bridge_member(ds, port, br, true);
1762 static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
1763 struct net_device *br)
1765 sja1105_bridge_member(ds, port, br, false);
1768 #define BYTES_PER_KBIT (1000LL / 8)
1770 static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
1774 for (i = 0; i < priv->info->num_cbs_shapers; i++)
1775 if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
1781 static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
1786 for (i = 0; i < priv->info->num_cbs_shapers; i++) {
1787 struct sja1105_cbs_entry *cbs = &priv->cbs[i];
1789 if (cbs->port == port && cbs->prio == prio) {
1790 memset(cbs, 0, sizeof(*cbs));
1791 return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
1799 static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
1800 struct tc_cbs_qopt_offload *offload)
1802 struct sja1105_private *priv = ds->priv;
1803 struct sja1105_cbs_entry *cbs;
1806 if (!offload->enable)
1807 return sja1105_delete_cbs_shaper(priv, port, offload->queue);
1809 index = sja1105_find_unused_cbs_shaper(priv);
1813 cbs = &priv->cbs[index];
1815 cbs->prio = offload->queue;
1816 /* locredit and sendslope are negative by definition. In hardware,
1817 * positive values must be provided, and the negative sign is implicit.
1819 cbs->credit_hi = offload->hicredit;
1820 cbs->credit_lo = abs(offload->locredit);
1821 /* User space is in kbits/sec, hardware in bytes/sec */
1822 cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
1823 cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
1824 /* Convert the negative values from 64-bit 2's complement
1825 * to 32-bit 2's complement (for the case of 0x80000000 whose
1826 * negative is still negative).
1828 cbs->credit_lo &= GENMASK_ULL(31, 0);
1829 cbs->send_slope &= GENMASK_ULL(31, 0);
1831 return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
1835 static int sja1105_reload_cbs(struct sja1105_private *priv)
1839 for (i = 0; i < priv->info->num_cbs_shapers; i++) {
1840 struct sja1105_cbs_entry *cbs = &priv->cbs[i];
1842 if (!cbs->idle_slope && !cbs->send_slope)
1845 rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
1854 static const char * const sja1105_reset_reasons[] = {
1855 [SJA1105_VLAN_FILTERING] = "VLAN filtering",
1856 [SJA1105_RX_HWTSTAMPING] = "RX timestamping",
1857 [SJA1105_AGEING_TIME] = "Ageing time",
1858 [SJA1105_SCHEDULING] = "Time-aware scheduling",
1859 [SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1860 [SJA1105_VIRTUAL_LINKS] = "Virtual links",
1863 /* For situations where we need to change a setting at runtime that is only
1864 * available through the static configuration, resetting the switch in order
1865 * to upload the new static config is unavoidable. Back up the settings we
1866 * modify at runtime (currently only MAC) and restore them after uploading,
1867 * such that this operation is relatively seamless.
1869 int sja1105_static_config_reload(struct sja1105_private *priv,
1870 enum sja1105_reset_reason reason)
1872 struct ptp_system_timestamp ptp_sts_before;
1873 struct ptp_system_timestamp ptp_sts_after;
1874 int speed_mbps[SJA1105_MAX_NUM_PORTS];
1875 u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0};
1876 struct sja1105_mac_config_entry *mac;
1877 struct dsa_switch *ds = priv->ds;
1883 mutex_lock(&priv->mgmt_lock);
1885 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1887 /* Back up the dynamic link speed changed by sja1105_adjust_port_config
1888 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
1889 * switch wants to see in the static config in order to allow us to
1890 * change it through the dynamic interface later.
1892 for (i = 0; i < ds->num_ports; i++) {
1893 speed_mbps[i] = sja1105_port_speed_to_ethtool(priv,
1895 mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO];
1897 if (priv->phy_mode[i] == PHY_INTERFACE_MODE_SGMII)
1898 bmcr[i] = sja1105_sgmii_read(priv, i,
1903 /* No PTP operations can run right now */
1904 mutex_lock(&priv->ptp_data.lock);
1906 rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
1908 goto out_unlock_ptp;
1910 /* Reset switch and send updated static configuration */
1911 rc = sja1105_static_config_upload(priv);
1913 goto out_unlock_ptp;
1915 rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
1917 goto out_unlock_ptp;
1919 t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
1920 t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
1921 t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
1922 t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
1923 /* Mid point, corresponds to pre-reset PTPCLKVAL */
1924 t12 = t1 + (t2 - t1) / 2;
1925 /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
1926 t34 = t3 + (t4 - t3) / 2;
1927 /* Advance PTPCLKVAL by the time it took since its readout */
1930 __sja1105_ptp_adjtime(ds, now);
1933 mutex_unlock(&priv->ptp_data.lock);
1935 dev_info(priv->ds->dev,
1936 "Reset switch and programmed static config. Reason: %s\n",
1937 sja1105_reset_reasons[reason]);
1939 /* Configure the CGU (PLLs) for MII and RMII PHYs.
1940 * For these interfaces there is no dynamic configuration
1941 * needed, since PLLs have same settings at all speeds.
1943 rc = priv->info->clocking_setup(priv);
1947 for (i = 0; i < ds->num_ports; i++) {
1950 rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
1954 if (priv->phy_mode[i] != PHY_INTERFACE_MODE_SGMII)
1957 an_enabled = !!(bmcr[i] & BMCR_ANENABLE);
1959 sja1105_sgmii_pcs_config(priv, i, an_enabled, false);
1962 int speed = SPEED_UNKNOWN;
1964 if (bmcr[i] & BMCR_SPEED1000)
1966 else if (bmcr[i] & BMCR_SPEED100)
1971 sja1105_sgmii_pcs_force_speed(priv, i, speed);
1975 rc = sja1105_reload_cbs(priv);
1979 mutex_unlock(&priv->mgmt_lock);
1984 static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
1986 struct sja1105_mac_config_entry *mac;
1988 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1990 mac[port].vlanid = pvid;
1992 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1996 static int sja1105_crosschip_bridge_join(struct dsa_switch *ds,
1997 int tree_index, int sw_index,
1998 int other_port, struct net_device *br)
2000 struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
2001 struct sja1105_private *other_priv = other_ds->priv;
2002 struct sja1105_private *priv = ds->priv;
2005 if (other_ds->ops != &sja1105_switch_ops)
2008 for (port = 0; port < ds->num_ports; port++) {
2009 if (!dsa_is_user_port(ds, port))
2011 if (dsa_to_port(ds, port)->bridge_dev != br)
2014 rc = dsa_8021q_crosschip_bridge_join(priv->dsa_8021q_ctx,
2016 other_priv->dsa_8021q_ctx,
2021 rc = dsa_8021q_crosschip_bridge_join(other_priv->dsa_8021q_ctx,
2023 priv->dsa_8021q_ctx,
2032 static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds,
2033 int tree_index, int sw_index,
2035 struct net_device *br)
2037 struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
2038 struct sja1105_private *other_priv = other_ds->priv;
2039 struct sja1105_private *priv = ds->priv;
2042 if (other_ds->ops != &sja1105_switch_ops)
2045 for (port = 0; port < ds->num_ports; port++) {
2046 if (!dsa_is_user_port(ds, port))
2048 if (dsa_to_port(ds, port)->bridge_dev != br)
2051 dsa_8021q_crosschip_bridge_leave(priv->dsa_8021q_ctx, port,
2052 other_priv->dsa_8021q_ctx,
2055 dsa_8021q_crosschip_bridge_leave(other_priv->dsa_8021q_ctx,
2057 priv->dsa_8021q_ctx, port);
2061 static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
2063 struct sja1105_private *priv = ds->priv;
2066 rc = dsa_8021q_setup(priv->dsa_8021q_ctx, enabled);
2070 dev_info(ds->dev, "%s switch tagging\n",
2071 enabled ? "Enabled" : "Disabled");
2075 static enum dsa_tag_protocol
2076 sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
2077 enum dsa_tag_protocol mp)
2079 return DSA_TAG_PROTO_SJA1105;
2082 static int sja1105_find_free_subvlan(u16 *subvlan_map, bool pvid)
2089 for (subvlan = 1; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2090 if (subvlan_map[subvlan] == VLAN_N_VID)
2096 static int sja1105_find_subvlan(u16 *subvlan_map, u16 vid)
2100 for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2101 if (subvlan_map[subvlan] == vid)
2107 static int sja1105_find_committed_subvlan(struct sja1105_private *priv,
2110 struct sja1105_port *sp = &priv->ports[port];
2112 return sja1105_find_subvlan(sp->subvlan_map, vid);
2115 static void sja1105_init_subvlan_map(u16 *subvlan_map)
2119 for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2120 subvlan_map[subvlan] = VLAN_N_VID;
2123 static void sja1105_commit_subvlan_map(struct sja1105_private *priv, int port,
2126 struct sja1105_port *sp = &priv->ports[port];
2129 for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2130 sp->subvlan_map[subvlan] = subvlan_map[subvlan];
2133 static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
2135 struct sja1105_vlan_lookup_entry *vlan;
2138 vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
2139 count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
2141 for (i = 0; i < count; i++)
2142 if (vlan[i].vlanid == vid)
2145 /* Return an invalid entry index if not found */
2150 sja1105_find_retagging_entry(struct sja1105_retagging_entry *retagging,
2151 int count, int from_port, u16 from_vid,
2156 for (i = 0; i < count; i++)
2157 if (retagging[i].ing_port == BIT(from_port) &&
2158 retagging[i].vlan_ing == from_vid &&
2159 retagging[i].vlan_egr == to_vid)
2162 /* Return an invalid entry index if not found */
2166 static int sja1105_commit_vlans(struct sja1105_private *priv,
2167 struct sja1105_vlan_lookup_entry *new_vlan,
2168 struct sja1105_retagging_entry *new_retagging,
2171 struct sja1105_retagging_entry *retagging;
2172 struct sja1105_vlan_lookup_entry *vlan;
2173 struct sja1105_table *table;
2178 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2179 vlan = table->entries;
2181 for (i = 0; i < VLAN_N_VID; i++) {
2182 int match = sja1105_is_vlan_configured(priv, i);
2184 if (new_vlan[i].vlanid != VLAN_N_VID)
2187 if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) {
2188 /* Was there before, no longer is. Delete */
2189 dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i);
2190 rc = sja1105_dynamic_config_write(priv,
2191 BLK_IDX_VLAN_LOOKUP,
2192 i, &vlan[match], false);
2195 } else if (new_vlan[i].vlanid != VLAN_N_VID) {
2196 /* Nothing changed, don't do anything */
2198 vlan[match].vlanid == new_vlan[i].vlanid &&
2199 vlan[match].tag_port == new_vlan[i].tag_port &&
2200 vlan[match].vlan_bc == new_vlan[i].vlan_bc &&
2201 vlan[match].vmemb_port == new_vlan[i].vmemb_port)
2204 dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i);
2205 rc = sja1105_dynamic_config_write(priv,
2206 BLK_IDX_VLAN_LOOKUP,
2214 if (table->entry_count)
2215 kfree(table->entries);
2217 table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size,
2219 if (!table->entries)
2222 table->entry_count = num_vlans;
2223 vlan = table->entries;
2225 for (i = 0; i < VLAN_N_VID; i++) {
2226 if (new_vlan[i].vlanid == VLAN_N_VID)
2228 vlan[k++] = new_vlan[i];
2231 /* VLAN Retagging Table */
2232 table = &priv->static_config.tables[BLK_IDX_RETAGGING];
2233 retagging = table->entries;
2235 for (i = 0; i < table->entry_count; i++) {
2236 rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
2237 i, &retagging[i], false);
2242 if (table->entry_count)
2243 kfree(table->entries);
2245 table->entries = kcalloc(num_retagging, table->ops->unpacked_entry_size,
2247 if (!table->entries)
2250 table->entry_count = num_retagging;
2251 retagging = table->entries;
2253 for (i = 0; i < num_retagging; i++) {
2254 retagging[i] = new_retagging[i];
2257 rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
2258 i, &retagging[i], true);
2266 struct sja1105_crosschip_vlan {
2267 struct list_head list;
2272 struct dsa_8021q_context *other_ctx;
2275 struct sja1105_crosschip_switch {
2276 struct list_head list;
2277 struct dsa_8021q_context *other_ctx;
2280 static int sja1105_commit_pvid(struct sja1105_private *priv)
2282 struct sja1105_bridge_vlan *v;
2283 struct list_head *vlan_list;
2286 if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2287 vlan_list = &priv->bridge_vlans;
2289 vlan_list = &priv->dsa_8021q_vlans;
2291 list_for_each_entry(v, vlan_list, list) {
2293 rc = sja1105_pvid_apply(priv, v->port, v->vid);
2303 sja1105_build_bridge_vlans(struct sja1105_private *priv,
2304 struct sja1105_vlan_lookup_entry *new_vlan)
2306 struct sja1105_bridge_vlan *v;
2308 if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
2311 list_for_each_entry(v, &priv->bridge_vlans, list) {
2314 new_vlan[match].vlanid = v->vid;
2315 new_vlan[match].vmemb_port |= BIT(v->port);
2316 new_vlan[match].vlan_bc |= BIT(v->port);
2318 new_vlan[match].tag_port |= BIT(v->port);
2325 sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv,
2326 struct sja1105_vlan_lookup_entry *new_vlan)
2328 struct sja1105_bridge_vlan *v;
2330 if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2333 list_for_each_entry(v, &priv->dsa_8021q_vlans, list) {
2336 new_vlan[match].vlanid = v->vid;
2337 new_vlan[match].vmemb_port |= BIT(v->port);
2338 new_vlan[match].vlan_bc |= BIT(v->port);
2340 new_vlan[match].tag_port |= BIT(v->port);
2346 static int sja1105_build_subvlans(struct sja1105_private *priv,
2347 u16 subvlan_map[][DSA_8021Q_N_SUBVLAN],
2348 struct sja1105_vlan_lookup_entry *new_vlan,
2349 struct sja1105_retagging_entry *new_retagging,
2352 struct sja1105_bridge_vlan *v;
2353 int k = *num_retagging;
2355 if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
2358 list_for_each_entry(v, &priv->bridge_vlans, list) {
2359 int upstream = dsa_upstream_port(priv->ds, v->port);
2363 /* Only sub-VLANs on user ports need to be applied.
2364 * Bridge VLANs also include VLANs added automatically
2365 * by DSA on the CPU port.
2367 if (!dsa_is_user_port(priv->ds, v->port))
2370 subvlan = sja1105_find_subvlan(subvlan_map[v->port],
2373 subvlan = sja1105_find_free_subvlan(subvlan_map[v->port],
2376 dev_err(priv->ds->dev, "No more free subvlans\n");
2381 rx_vid = dsa_8021q_rx_vid_subvlan(priv->ds, v->port, subvlan);
2383 /* @v->vid on @v->port needs to be retagged to @rx_vid
2384 * on @upstream. Assume @v->vid on @v->port and on
2385 * @upstream was already configured by the previous
2386 * iteration over bridge_vlans.
2389 new_vlan[match].vlanid = rx_vid;
2390 new_vlan[match].vmemb_port |= BIT(v->port);
2391 new_vlan[match].vmemb_port |= BIT(upstream);
2392 new_vlan[match].vlan_bc |= BIT(v->port);
2393 new_vlan[match].vlan_bc |= BIT(upstream);
2394 /* The "untagged" flag is set the same as for the
2398 new_vlan[match].tag_port |= BIT(v->port);
2399 /* But it's always tagged towards the CPU */
2400 new_vlan[match].tag_port |= BIT(upstream);
2402 /* The Retagging Table generates packet *clones* with
2403 * the new VLAN. This is a very odd hardware quirk
2404 * which we need to suppress by dropping the original
2406 * Deny egress of the original VLAN towards the CPU
2407 * port. This will force the switch to drop it, and
2408 * we'll see only the retagged packets.
2411 new_vlan[match].vlan_bc &= ~BIT(upstream);
2413 /* And the retagging itself */
2414 new_retagging[k].vlan_ing = v->vid;
2415 new_retagging[k].vlan_egr = rx_vid;
2416 new_retagging[k].ing_port = BIT(v->port);
2417 new_retagging[k].egr_port = BIT(upstream);
2418 if (k++ == SJA1105_MAX_RETAGGING_COUNT) {
2419 dev_err(priv->ds->dev, "No more retagging rules\n");
2423 subvlan_map[v->port][subvlan] = v->vid;
2431 /* Sadly, in crosschip scenarios where the CPU port is also the link to another
2432 * switch, we should retag backwards (the dsa_8021q vid to the original vid) on
2433 * the CPU port of neighbour switches.
2436 sja1105_build_crosschip_subvlans(struct sja1105_private *priv,
2437 struct sja1105_vlan_lookup_entry *new_vlan,
2438 struct sja1105_retagging_entry *new_retagging,
2441 struct sja1105_crosschip_vlan *tmp, *pos;
2442 struct dsa_8021q_crosschip_link *c;
2443 struct sja1105_bridge_vlan *v, *w;
2444 struct list_head crosschip_vlans;
2445 int k = *num_retagging;
2448 if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
2451 INIT_LIST_HEAD(&crosschip_vlans);
2453 list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
2454 struct sja1105_private *other_priv = c->other_ctx->ds->priv;
2456 if (other_priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2459 /* Crosschip links are also added to the CPU ports.
2462 if (!dsa_is_user_port(priv->ds, c->port))
2464 if (!dsa_is_user_port(c->other_ctx->ds, c->other_port))
2467 /* Search for VLANs on the remote port */
2468 list_for_each_entry(v, &other_priv->bridge_vlans, list) {
2469 bool already_added = false;
2470 bool we_have_it = false;
2472 if (v->port != c->other_port)
2475 /* If @v is a pvid on @other_ds, it does not need
2476 * re-retagging, because its SVL field is 0 and we
2477 * already allow that, via the dsa_8021q crosschip
2483 /* Search for the VLAN on our local port */
2484 list_for_each_entry(w, &priv->bridge_vlans, list) {
2485 if (w->port == c->port && w->vid == v->vid) {
2494 list_for_each_entry(tmp, &crosschip_vlans, list) {
2495 if (tmp->vid == v->vid &&
2496 tmp->untagged == v->untagged &&
2497 tmp->port == c->port &&
2498 tmp->other_port == v->port &&
2499 tmp->other_ctx == c->other_ctx) {
2500 already_added = true;
2508 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
2510 dev_err(priv->ds->dev, "Failed to allocate memory\n");
2515 tmp->port = c->port;
2516 tmp->other_port = v->port;
2517 tmp->other_ctx = c->other_ctx;
2518 tmp->untagged = v->untagged;
2519 list_add(&tmp->list, &crosschip_vlans);
2523 list_for_each_entry(tmp, &crosschip_vlans, list) {
2524 struct sja1105_private *other_priv = tmp->other_ctx->ds->priv;
2525 int upstream = dsa_upstream_port(priv->ds, tmp->port);
2529 subvlan = sja1105_find_committed_subvlan(other_priv,
2532 /* If this happens, it's a bug. The neighbour switch does not
2533 * have a subvlan for tmp->vid on tmp->other_port, but it
2534 * should, since we already checked for its vlan_state.
2536 if (WARN_ON(subvlan < 0)) {
2541 rx_vid = dsa_8021q_rx_vid_subvlan(tmp->other_ctx->ds,
2545 /* The @rx_vid retagged from @tmp->vid on
2546 * {@tmp->other_ds, @tmp->other_port} needs to be
2547 * re-retagged to @tmp->vid on the way back to us.
2549 * Assume the original @tmp->vid is already configured
2550 * on this local switch, otherwise we wouldn't be
2551 * retagging its subvlan on the other switch in the
2552 * first place. We just need to add a reverse retagging
2553 * rule for @rx_vid and install @rx_vid on our ports.
2556 new_vlan[match].vlanid = rx_vid;
2557 new_vlan[match].vmemb_port |= BIT(tmp->port);
2558 new_vlan[match].vmemb_port |= BIT(upstream);
2559 /* The "untagged" flag is set the same as for the
2560 * original VLAN. And towards the CPU, it doesn't
2561 * really matter, because @rx_vid will only receive
2562 * traffic on that port. For consistency with other dsa_8021q
2563 * VLANs, we'll keep the CPU port tagged.
2566 new_vlan[match].tag_port |= BIT(tmp->port);
2567 new_vlan[match].tag_port |= BIT(upstream);
2568 /* Deny egress of @rx_vid towards our front-panel port.
2569 * This will force the switch to drop it, and we'll see
2570 * only the re-retagged packets (having the original,
2571 * pre-initial-retagging, VLAN @tmp->vid).
2573 new_vlan[match].vlan_bc &= ~BIT(tmp->port);
2575 /* On reverse retagging, the same ingress VLAN goes to multiple
2576 * ports. So we have an opportunity to create composite rules
2577 * to not waste the limited space in the retagging table.
2579 k = sja1105_find_retagging_entry(new_retagging, *num_retagging,
2580 upstream, rx_vid, tmp->vid);
2582 if (*num_retagging == SJA1105_MAX_RETAGGING_COUNT) {
2583 dev_err(priv->ds->dev, "No more retagging rules\n");
2587 k = (*num_retagging)++;
2589 /* And the retagging itself */
2590 new_retagging[k].vlan_ing = rx_vid;
2591 new_retagging[k].vlan_egr = tmp->vid;
2592 new_retagging[k].ing_port = BIT(upstream);
2593 new_retagging[k].egr_port |= BIT(tmp->port);
2597 list_for_each_entry_safe(tmp, pos, &crosschip_vlans, list) {
2598 list_del(&tmp->list);
2605 static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify);
2607 static int sja1105_notify_crosschip_switches(struct sja1105_private *priv)
2609 struct sja1105_crosschip_switch *s, *pos;
2610 struct list_head crosschip_switches;
2611 struct dsa_8021q_crosschip_link *c;
2614 INIT_LIST_HEAD(&crosschip_switches);
2616 list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
2617 bool already_added = false;
2619 list_for_each_entry(s, &crosschip_switches, list) {
2620 if (s->other_ctx == c->other_ctx) {
2621 already_added = true;
2629 s = kzalloc(sizeof(*s), GFP_KERNEL);
2631 dev_err(priv->ds->dev, "Failed to allocate memory\n");
2635 s->other_ctx = c->other_ctx;
2636 list_add(&s->list, &crosschip_switches);
2639 list_for_each_entry(s, &crosschip_switches, list) {
2640 struct sja1105_private *other_priv = s->other_ctx->ds->priv;
2642 rc = sja1105_build_vlan_table(other_priv, false);
2648 list_for_each_entry_safe(s, pos, &crosschip_switches, list) {
2656 static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify)
2658 u16 subvlan_map[SJA1105_MAX_NUM_PORTS][DSA_8021Q_N_SUBVLAN];
2659 struct sja1105_retagging_entry *new_retagging;
2660 struct sja1105_vlan_lookup_entry *new_vlan;
2661 struct sja1105_table *table;
2662 int i, num_retagging = 0;
2665 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2666 new_vlan = kcalloc(VLAN_N_VID,
2667 table->ops->unpacked_entry_size, GFP_KERNEL);
2671 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2672 new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT,
2673 table->ops->unpacked_entry_size, GFP_KERNEL);
2674 if (!new_retagging) {
2679 for (i = 0; i < VLAN_N_VID; i++)
2680 new_vlan[i].vlanid = VLAN_N_VID;
2682 for (i = 0; i < SJA1105_MAX_RETAGGING_COUNT; i++)
2683 new_retagging[i].vlan_ing = VLAN_N_VID;
2685 for (i = 0; i < priv->ds->num_ports; i++)
2686 sja1105_init_subvlan_map(subvlan_map[i]);
2689 rc = sja1105_build_bridge_vlans(priv, new_vlan);
2693 /* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c:
2698 rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan);
2702 /* Private VLANs necessary for dsa_8021q operation, which we need to
2703 * determine on our own:
2705 * - Sub-VLANs of crosschip switches
2707 rc = sja1105_build_subvlans(priv, subvlan_map, new_vlan, new_retagging,
2712 rc = sja1105_build_crosschip_subvlans(priv, new_vlan, new_retagging,
2717 rc = sja1105_commit_vlans(priv, new_vlan, new_retagging, num_retagging);
2721 rc = sja1105_commit_pvid(priv);
2725 for (i = 0; i < priv->ds->num_ports; i++)
2726 sja1105_commit_subvlan_map(priv, i, subvlan_map[i]);
2729 rc = sja1105_notify_crosschip_switches(priv);
2736 kfree(new_retagging);
2741 /* The TPID setting belongs to the General Parameters table,
2742 * which can only be partially reconfigured at runtime (and not the TPID).
2743 * So a switch reset is required.
2745 int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
2746 struct netlink_ext_ack *extack)
2748 struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2749 struct sja1105_general_params_entry *general_params;
2750 struct sja1105_private *priv = ds->priv;
2751 enum sja1105_vlan_state state;
2752 struct sja1105_table *table;
2753 struct sja1105_rule *rule;
2758 list_for_each_entry(rule, &priv->flow_block.rules, list) {
2759 if (rule->type == SJA1105_RULE_VL) {
2760 NL_SET_ERR_MSG_MOD(extack,
2761 "Cannot change VLAN filtering with active VL rules");
2767 /* Enable VLAN filtering. */
2769 tpid2 = ETH_P_8021AD;
2771 /* Disable VLAN filtering. */
2772 tpid = ETH_P_SJA1105;
2773 tpid2 = ETH_P_SJA1105;
2776 for (port = 0; port < ds->num_ports; port++) {
2777 struct sja1105_port *sp = &priv->ports[port];
2780 sp->xmit_tpid = priv->info->qinq_tpid;
2782 sp->xmit_tpid = ETH_P_SJA1105;
2786 state = SJA1105_VLAN_UNAWARE;
2787 else if (priv->best_effort_vlan_filtering)
2788 state = SJA1105_VLAN_BEST_EFFORT;
2790 state = SJA1105_VLAN_FILTERING_FULL;
2792 if (priv->vlan_state == state)
2795 priv->vlan_state = state;
2796 want_tagging = (state == SJA1105_VLAN_UNAWARE ||
2797 state == SJA1105_VLAN_BEST_EFFORT);
2799 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2800 general_params = table->entries;
2801 /* EtherType used to identify inner tagged (C-tag) VLAN traffic */
2802 general_params->tpid = tpid;
2803 /* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2804 general_params->tpid2 = tpid2;
2805 /* When VLAN filtering is on, we need to at least be able to
2806 * decode management traffic through the "backup plan".
2808 general_params->incl_srcpt1 = enabled;
2809 general_params->incl_srcpt0 = enabled;
2811 want_tagging = priv->best_effort_vlan_filtering || !enabled;
2813 /* VLAN filtering => independent VLAN learning.
2814 * No VLAN filtering (or best effort) => shared VLAN learning.
2816 * In shared VLAN learning mode, untagged traffic still gets
2817 * pvid-tagged, and the FDB table gets populated with entries
2818 * containing the "real" (pvid or from VLAN tag) VLAN ID.
2819 * However the switch performs a masked L2 lookup in the FDB,
2820 * effectively only looking up a frame's DMAC (and not VID) for the
2821 * forwarding decision.
2823 * This is extremely convenient for us, because in modes with
2824 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
2825 * each front panel port. This is good for identification but breaks
2826 * learning badly - the VID of the learnt FDB entry is unique, aka
2827 * no frames coming from any other port are going to have it. So
2828 * for forwarding purposes, this is as though learning was broken
2829 * (all frames get flooded).
2831 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2832 l2_lookup_params = table->entries;
2833 l2_lookup_params->shared_learn = want_tagging;
2835 sja1105_frame_memory_partitioning(priv);
2837 rc = sja1105_build_vlan_table(priv, false);
2841 rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
2843 NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype");
2845 /* Switch port identification based on 802.1Q is only passable
2846 * if we are not under a vlan_filtering bridge. So make sure
2847 * the two configurations are mutually exclusive (of course, the
2848 * user may know better, i.e. best_effort_vlan_filtering).
2850 return sja1105_setup_8021q_tagging(ds, want_tagging);
2853 /* Returns number of VLANs added (0 or 1) on success,
2854 * or a negative error code.
2856 static int sja1105_vlan_add_one(struct dsa_switch *ds, int port, u16 vid,
2857 u16 flags, struct list_head *vlan_list)
2859 bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED;
2860 bool pvid = flags & BRIDGE_VLAN_INFO_PVID;
2861 struct sja1105_bridge_vlan *v;
2863 list_for_each_entry(v, vlan_list, list) {
2864 if (v->port == port && v->vid == vid) {
2866 if (v->untagged == untagged && v->pvid == pvid)
2867 /* Nothing changed */
2870 /* It's the same VLAN, but some of the flags changed
2871 * and the user did not bother to delete it first.
2872 * Update it and trigger sja1105_build_vlan_table.
2874 v->untagged = untagged;
2880 v = kzalloc(sizeof(*v), GFP_KERNEL);
2882 dev_err(ds->dev, "Out of memory while storing VLAN\n");
2888 v->untagged = untagged;
2890 list_add(&v->list, vlan_list);
2895 /* Returns number of VLANs deleted (0 or 1) */
2896 static int sja1105_vlan_del_one(struct dsa_switch *ds, int port, u16 vid,
2897 struct list_head *vlan_list)
2899 struct sja1105_bridge_vlan *v, *n;
2901 list_for_each_entry_safe(v, n, vlan_list, list) {
2902 if (v->port == port && v->vid == vid) {
2912 static int sja1105_vlan_add(struct dsa_switch *ds, int port,
2913 const struct switchdev_obj_port_vlan *vlan,
2914 struct netlink_ext_ack *extack)
2916 struct sja1105_private *priv = ds->priv;
2917 bool vlan_table_changed = false;
2920 /* If the user wants best-effort VLAN filtering (aka vlan_filtering
2921 * bridge plus tagging), be sure to at least deny alterations to the
2922 * configuration done by dsa_8021q.
2924 if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL &&
2925 vid_is_dsa_8021q(vlan->vid)) {
2926 NL_SET_ERR_MSG_MOD(extack,
2927 "Range 1024-3071 reserved for dsa_8021q operation");
2931 rc = sja1105_vlan_add_one(ds, port, vlan->vid, vlan->flags,
2932 &priv->bridge_vlans);
2936 vlan_table_changed = true;
2938 if (!vlan_table_changed)
2941 return sja1105_build_vlan_table(priv, true);
2944 static int sja1105_vlan_del(struct dsa_switch *ds, int port,
2945 const struct switchdev_obj_port_vlan *vlan)
2947 struct sja1105_private *priv = ds->priv;
2948 bool vlan_table_changed = false;
2951 rc = sja1105_vlan_del_one(ds, port, vlan->vid, &priv->bridge_vlans);
2953 vlan_table_changed = true;
2955 if (!vlan_table_changed)
2958 return sja1105_build_vlan_table(priv, true);
2961 static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
2964 struct sja1105_private *priv = ds->priv;
2967 rc = sja1105_vlan_add_one(ds, port, vid, flags, &priv->dsa_8021q_vlans);
2971 return sja1105_build_vlan_table(priv, true);
2974 static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
2976 struct sja1105_private *priv = ds->priv;
2979 rc = sja1105_vlan_del_one(ds, port, vid, &priv->dsa_8021q_vlans);
2983 return sja1105_build_vlan_table(priv, true);
2986 static const struct dsa_8021q_ops sja1105_dsa_8021q_ops = {
2987 .vlan_add = sja1105_dsa_8021q_vlan_add,
2988 .vlan_del = sja1105_dsa_8021q_vlan_del,
2991 /* The programming model for the SJA1105 switch is "all-at-once" via static
2992 * configuration tables. Some of these can be dynamically modified at runtime,
2993 * but not the xMII mode parameters table.
2994 * Furthermode, some PHYs may not have crystals for generating their clocks
2995 * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
2996 * ref_clk pin. So port clocking needs to be initialized early, before
2997 * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
2998 * Setting correct PHY link speed does not matter now.
2999 * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
3000 * bindings are not yet parsed by DSA core. We need to parse early so that we
3001 * can populate the xMII mode parameters table.
3003 static int sja1105_setup(struct dsa_switch *ds)
3005 struct sja1105_dt_port ports[SJA1105_MAX_NUM_PORTS];
3006 struct sja1105_private *priv = ds->priv;
3009 rc = sja1105_parse_dt(priv, ports);
3011 dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
3015 /* Error out early if internal delays are required through DT
3016 * and we can't apply them.
3018 rc = sja1105_parse_rgmii_delays(priv, ports);
3020 dev_err(ds->dev, "RGMII delay not supported\n");
3024 rc = sja1105_ptp_clock_register(ds);
3026 dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
3029 /* Create and send configuration down to device */
3030 rc = sja1105_static_config_load(priv, ports);
3032 dev_err(ds->dev, "Failed to load static config: %d\n", rc);
3033 goto out_ptp_clock_unregister;
3035 /* Configure the CGU (PHY link modes and speeds) */
3036 rc = priv->info->clocking_setup(priv);
3038 dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
3039 goto out_static_config_free;
3041 /* On SJA1105, VLAN filtering per se is always enabled in hardware.
3042 * The only thing we can do to disable it is lie about what the 802.1Q
3044 * So it will still try to apply VLAN filtering, but all ingress
3045 * traffic (except frames received with EtherType of ETH_P_SJA1105)
3046 * will be internally tagged with a distorted VLAN header where the
3047 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
3049 ds->vlan_filtering_is_global = true;
3051 /* Advertise the 8 egress queues */
3052 ds->num_tx_queues = SJA1105_NUM_TC;
3054 ds->mtu_enforcement_ingress = true;
3056 priv->best_effort_vlan_filtering = true;
3058 rc = sja1105_devlink_setup(ds);
3060 goto out_static_config_free;
3062 /* The DSA/switchdev model brings up switch ports in standalone mode by
3063 * default, and that means vlan_filtering is 0 since they're not under
3064 * a bridge, so it's safe to set up switch tagging at this time.
3067 rc = sja1105_setup_8021q_tagging(ds, true);
3070 goto out_devlink_teardown;
3074 out_devlink_teardown:
3075 sja1105_devlink_teardown(ds);
3076 out_ptp_clock_unregister:
3077 sja1105_ptp_clock_unregister(ds);
3078 out_static_config_free:
3079 sja1105_static_config_free(&priv->static_config);
3084 static void sja1105_teardown(struct dsa_switch *ds)
3086 struct sja1105_private *priv = ds->priv;
3087 struct sja1105_bridge_vlan *v, *n;
3090 for (port = 0; port < ds->num_ports; port++) {
3091 struct sja1105_port *sp = &priv->ports[port];
3093 if (!dsa_is_user_port(ds, port))
3096 if (sp->xmit_worker)
3097 kthread_destroy_worker(sp->xmit_worker);
3100 sja1105_devlink_teardown(ds);
3101 sja1105_flower_teardown(ds);
3102 sja1105_tas_teardown(ds);
3103 sja1105_ptp_clock_unregister(ds);
3104 sja1105_static_config_free(&priv->static_config);
3106 list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) {
3111 list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) {
3117 static void sja1105_port_disable(struct dsa_switch *ds, int port)
3119 struct sja1105_private *priv = ds->priv;
3120 struct sja1105_port *sp = &priv->ports[port];
3122 if (!dsa_is_user_port(ds, port))
3125 kthread_cancel_work_sync(&sp->xmit_work);
3126 skb_queue_purge(&sp->xmit_queue);
3129 static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
3130 struct sk_buff *skb, bool takets)
3132 struct sja1105_mgmt_entry mgmt_route = {0};
3133 struct sja1105_private *priv = ds->priv;
3140 mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
3141 mgmt_route.destports = BIT(port);
3142 mgmt_route.enfport = 1;
3143 mgmt_route.tsreg = 0;
3144 mgmt_route.takets = takets;
3146 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3147 slot, &mgmt_route, true);
3153 /* Transfer skb to the host port. */
3154 dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
3156 /* Wait until the switch has processed the frame */
3158 rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
3161 dev_err_ratelimited(priv->ds->dev,
3162 "failed to poll for mgmt route\n");
3166 /* UM10944: The ENFPORT flag of the respective entry is
3167 * cleared when a match is found. The host can use this
3168 * flag as an acknowledgment.
3171 } while (mgmt_route.enfport && --timeout);
3174 /* Clean up the management route so that a follow-up
3175 * frame may not match on it by mistake.
3176 * This is only hardware supported on P/Q/R/S - on E/T it is
3177 * a no-op and we are silently discarding the -EOPNOTSUPP.
3179 sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3180 slot, &mgmt_route, false);
3181 dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
3184 return NETDEV_TX_OK;
3187 #define work_to_port(work) \
3188 container_of((work), struct sja1105_port, xmit_work)
3189 #define tagger_to_sja1105(t) \
3190 container_of((t), struct sja1105_private, tagger_data)
3192 /* Deferred work is unfortunately necessary because setting up the management
3193 * route cannot be done from atomit context (SPI transfer takes a sleepable
3196 static void sja1105_port_deferred_xmit(struct kthread_work *work)
3198 struct sja1105_port *sp = work_to_port(work);
3199 struct sja1105_tagger_data *tagger_data = sp->data;
3200 struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
3201 int port = sp - priv->ports;
3202 struct sk_buff *skb;
3204 while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
3205 struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
3207 mutex_lock(&priv->mgmt_lock);
3209 sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
3211 /* The clone, if there, was made by dsa_skb_tx_timestamp */
3213 sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
3215 mutex_unlock(&priv->mgmt_lock);
3219 /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
3220 * which cannot be reconfigured at runtime. So a switch reset is required.
3222 static int sja1105_set_ageing_time(struct dsa_switch *ds,
3223 unsigned int ageing_time)
3225 struct sja1105_l2_lookup_params_entry *l2_lookup_params;
3226 struct sja1105_private *priv = ds->priv;
3227 struct sja1105_table *table;
3228 unsigned int maxage;
3230 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
3231 l2_lookup_params = table->entries;
3233 maxage = SJA1105_AGEING_TIME_MS(ageing_time);
3235 if (l2_lookup_params->maxage == maxage)
3238 l2_lookup_params->maxage = maxage;
3240 return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
3243 static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
3245 struct sja1105_l2_policing_entry *policing;
3246 struct sja1105_private *priv = ds->priv;
3248 new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
3250 if (dsa_is_cpu_port(ds, port))
3251 new_mtu += VLAN_HLEN;
3253 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3255 if (policing[port].maxlen == new_mtu)
3258 policing[port].maxlen = new_mtu;
3260 return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3263 static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
3265 return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
3268 static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
3269 enum tc_setup_type type,
3273 case TC_SETUP_QDISC_TAPRIO:
3274 return sja1105_setup_tc_taprio(ds, port, type_data);
3275 case TC_SETUP_QDISC_CBS:
3276 return sja1105_setup_tc_cbs(ds, port, type_data);
3282 /* We have a single mirror (@to) port, but can configure ingress and egress
3283 * mirroring on all other (@from) ports.
3284 * We need to allow mirroring rules only as long as the @to port is always the
3285 * same, and we need to unset the @to port from mirr_port only when there is no
3286 * mirroring rule that references it.
3288 static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
3289 bool ingress, bool enabled)
3291 struct sja1105_general_params_entry *general_params;
3292 struct sja1105_mac_config_entry *mac;
3293 struct dsa_switch *ds = priv->ds;
3294 struct sja1105_table *table;
3295 bool already_enabled;
3299 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
3300 general_params = table->entries;
3302 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
3304 already_enabled = (general_params->mirr_port != ds->num_ports);
3305 if (already_enabled && enabled && general_params->mirr_port != to) {
3306 dev_err(priv->ds->dev,
3307 "Delete mirroring rules towards port %llu first\n",
3308 general_params->mirr_port);
3317 /* Anybody still referencing mirr_port? */
3318 for (port = 0; port < ds->num_ports; port++) {
3319 if (mac[port].ing_mirr || mac[port].egr_mirr) {
3324 /* Unset already_enabled for next time */
3326 new_mirr_port = ds->num_ports;
3328 if (new_mirr_port != general_params->mirr_port) {
3329 general_params->mirr_port = new_mirr_port;
3331 rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
3332 0, general_params, true);
3338 mac[from].ing_mirr = enabled;
3340 mac[from].egr_mirr = enabled;
3342 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
3346 static int sja1105_mirror_add(struct dsa_switch *ds, int port,
3347 struct dsa_mall_mirror_tc_entry *mirror,
3350 return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3354 static void sja1105_mirror_del(struct dsa_switch *ds, int port,
3355 struct dsa_mall_mirror_tc_entry *mirror)
3357 sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3358 mirror->ingress, false);
3361 static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
3362 struct dsa_mall_policer_tc_entry *policer)
3364 struct sja1105_l2_policing_entry *policing;
3365 struct sja1105_private *priv = ds->priv;
3367 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3369 /* In hardware, every 8 microseconds the credit level is incremented by
3370 * the value of RATE bytes divided by 64, up to a maximum of SMAX
3373 policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
3375 policing[port].smax = policer->burst;
3377 return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3380 static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
3382 struct sja1105_l2_policing_entry *policing;
3383 struct sja1105_private *priv = ds->priv;
3385 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3387 policing[port].rate = SJA1105_RATE_MBPS(1000);
3388 policing[port].smax = 65535;
3390 sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3393 static int sja1105_port_set_learning(struct sja1105_private *priv, int port,
3396 struct sja1105_mac_config_entry *mac;
3399 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
3401 mac[port].dyn_learn = enabled;
3403 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
3409 priv->learn_ena |= BIT(port);
3411 priv->learn_ena &= ~BIT(port);
3416 static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to,
3417 struct switchdev_brport_flags flags)
3419 if (flags.mask & BR_FLOOD) {
3420 if (flags.val & BR_FLOOD)
3421 priv->ucast_egress_floods |= BIT(to);
3423 priv->ucast_egress_floods &= ~BIT(to);
3426 if (flags.mask & BR_BCAST_FLOOD) {
3427 if (flags.val & BR_BCAST_FLOOD)
3428 priv->bcast_egress_floods |= BIT(to);
3430 priv->bcast_egress_floods &= ~BIT(to);
3433 return sja1105_manage_flood_domains(priv);
3436 static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to,
3437 struct switchdev_brport_flags flags,
3438 struct netlink_ext_ack *extack)
3440 struct sja1105_l2_lookup_entry *l2_lookup;
3441 struct sja1105_table *table;
3444 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
3445 l2_lookup = table->entries;
3447 for (match = 0; match < table->entry_count; match++)
3448 if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST &&
3449 l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST)
3452 if (match == table->entry_count) {
3453 NL_SET_ERR_MSG_MOD(extack,
3454 "Could not find FDB entry for unknown multicast");
3458 if (flags.val & BR_MCAST_FLOOD)
3459 l2_lookup[match].destports |= BIT(to);
3461 l2_lookup[match].destports &= ~BIT(to);
3463 return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
3464 l2_lookup[match].index,
3469 static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port,
3470 struct switchdev_brport_flags flags,
3471 struct netlink_ext_ack *extack)
3473 struct sja1105_private *priv = ds->priv;
3475 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
3479 if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) &&
3480 !priv->info->can_limit_mcast_flood) {
3481 bool multicast = !!(flags.val & BR_MCAST_FLOOD);
3482 bool unicast = !!(flags.val & BR_FLOOD);
3484 if (unicast != multicast) {
3485 NL_SET_ERR_MSG_MOD(extack,
3486 "This chip cannot configure multicast flooding independently of unicast");
3494 static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port,
3495 struct switchdev_brport_flags flags,
3496 struct netlink_ext_ack *extack)
3498 struct sja1105_private *priv = ds->priv;
3501 if (flags.mask & BR_LEARNING) {
3502 bool learn_ena = !!(flags.val & BR_LEARNING);
3504 rc = sja1105_port_set_learning(priv, port, learn_ena);
3509 if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) {
3510 rc = sja1105_port_ucast_bcast_flood(priv, port, flags);
3515 /* For chips that can't offload BR_MCAST_FLOOD independently, there
3516 * is nothing to do here, we ensured the configuration is in sync by
3517 * offloading BR_FLOOD.
3519 if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) {
3520 rc = sja1105_port_mcast_flood(priv, port, flags,
3529 static const struct dsa_switch_ops sja1105_switch_ops = {
3530 .get_tag_protocol = sja1105_get_tag_protocol,
3531 .setup = sja1105_setup,
3532 .teardown = sja1105_teardown,
3533 .set_ageing_time = sja1105_set_ageing_time,
3534 .port_change_mtu = sja1105_change_mtu,
3535 .port_max_mtu = sja1105_get_max_mtu,
3536 .phylink_validate = sja1105_phylink_validate,
3537 .phylink_mac_link_state = sja1105_mac_pcs_get_state,
3538 .phylink_mac_config = sja1105_mac_config,
3539 .phylink_mac_link_up = sja1105_mac_link_up,
3540 .phylink_mac_link_down = sja1105_mac_link_down,
3541 .get_strings = sja1105_get_strings,
3542 .get_ethtool_stats = sja1105_get_ethtool_stats,
3543 .get_sset_count = sja1105_get_sset_count,
3544 .get_ts_info = sja1105_get_ts_info,
3545 .port_disable = sja1105_port_disable,
3546 .port_fdb_dump = sja1105_fdb_dump,
3547 .port_fdb_add = sja1105_fdb_add,
3548 .port_fdb_del = sja1105_fdb_del,
3549 .port_bridge_join = sja1105_bridge_join,
3550 .port_bridge_leave = sja1105_bridge_leave,
3551 .port_pre_bridge_flags = sja1105_port_pre_bridge_flags,
3552 .port_bridge_flags = sja1105_port_bridge_flags,
3553 .port_stp_state_set = sja1105_bridge_stp_state_set,
3554 .port_vlan_filtering = sja1105_vlan_filtering,
3555 .port_vlan_add = sja1105_vlan_add,
3556 .port_vlan_del = sja1105_vlan_del,
3557 .port_mdb_add = sja1105_mdb_add,
3558 .port_mdb_del = sja1105_mdb_del,
3559 .port_hwtstamp_get = sja1105_hwtstamp_get,
3560 .port_hwtstamp_set = sja1105_hwtstamp_set,
3561 .port_rxtstamp = sja1105_port_rxtstamp,
3562 .port_txtstamp = sja1105_port_txtstamp,
3563 .port_setup_tc = sja1105_port_setup_tc,
3564 .port_mirror_add = sja1105_mirror_add,
3565 .port_mirror_del = sja1105_mirror_del,
3566 .port_policer_add = sja1105_port_policer_add,
3567 .port_policer_del = sja1105_port_policer_del,
3568 .cls_flower_add = sja1105_cls_flower_add,
3569 .cls_flower_del = sja1105_cls_flower_del,
3570 .cls_flower_stats = sja1105_cls_flower_stats,
3571 .crosschip_bridge_join = sja1105_crosschip_bridge_join,
3572 .crosschip_bridge_leave = sja1105_crosschip_bridge_leave,
3573 .devlink_param_get = sja1105_devlink_param_get,
3574 .devlink_param_set = sja1105_devlink_param_set,
3575 .devlink_info_get = sja1105_devlink_info_get,
3578 static const struct of_device_id sja1105_dt_ids[];
3580 static int sja1105_check_device_id(struct sja1105_private *priv)
3582 const struct sja1105_regs *regs = priv->info->regs;
3583 u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
3584 struct device *dev = &priv->spidev->dev;
3585 const struct of_device_id *match;
3590 rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
3595 rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
3596 SJA1105_SIZE_DEVICE_ID);
3600 sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
3602 for (match = sja1105_dt_ids; match->compatible[0]; match++) {
3603 const struct sja1105_info *info = match->data;
3605 /* Is what's been probed in our match table at all? */
3606 if (info->device_id != device_id || info->part_no != part_no)
3609 /* But is it what's in the device tree? */
3610 if (priv->info->device_id != device_id ||
3611 priv->info->part_no != part_no) {
3612 dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
3613 priv->info->name, info->name);
3614 /* It isn't. No problem, pick that up. */
3621 dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
3622 device_id, part_no);
3627 static int sja1105_probe(struct spi_device *spi)
3629 struct sja1105_tagger_data *tagger_data;
3630 struct device *dev = &spi->dev;
3631 struct sja1105_private *priv;
3632 size_t max_xfer, max_msg;
3633 struct dsa_switch *ds;
3636 if (!dev->of_node) {
3637 dev_err(dev, "No DTS bindings for SJA1105 driver\n");
3641 priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
3645 /* Configure the optional reset pin and bring up switch */
3646 priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
3647 if (IS_ERR(priv->reset_gpio))
3648 dev_dbg(dev, "reset-gpios not defined, ignoring\n");
3650 sja1105_hw_reset(priv->reset_gpio, 1, 1);
3652 /* Populate our driver private structure (priv) based on
3653 * the device tree node that was probed (spi)
3656 spi_set_drvdata(spi, priv);
3658 /* Configure the SPI bus */
3659 spi->bits_per_word = 8;
3660 rc = spi_setup(spi);
3662 dev_err(dev, "Could not init SPI\n");
3666 /* In sja1105_xfer, we send spi_messages composed of two spi_transfers:
3667 * a small one for the message header and another one for the current
3668 * chunk of the packed buffer.
3669 * Check that the restrictions imposed by the SPI controller are
3670 * respected: the chunk buffer is smaller than the max transfer size,
3671 * and the total length of the chunk plus its message header is smaller
3672 * than the max message size.
3673 * We do that during probe time since the maximum transfer size is a
3674 * runtime invariant.
3676 max_xfer = spi_max_transfer_size(spi);
3677 max_msg = spi_max_message_size(spi);
3679 /* We need to send at least one 64-bit word of SPI payload per message
3680 * in order to be able to make useful progress.
3682 if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) {
3683 dev_err(dev, "SPI master cannot send large enough buffers, aborting\n");
3687 priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN;
3688 if (priv->max_xfer_len > max_xfer)
3689 priv->max_xfer_len = max_xfer;
3690 if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER)
3691 priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER;
3693 priv->info = of_device_get_match_data(dev);
3695 /* Detect hardware device */
3696 rc = sja1105_check_device_id(priv);
3698 dev_err(dev, "Device ID check failed: %d\n", rc);
3702 dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
3704 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3709 ds->num_ports = SJA1105_MAX_NUM_PORTS;
3710 ds->ops = &sja1105_switch_ops;
3714 tagger_data = &priv->tagger_data;
3716 mutex_init(&priv->ptp_data.lock);
3717 mutex_init(&priv->mgmt_lock);
3719 priv->dsa_8021q_ctx = devm_kzalloc(dev, sizeof(*priv->dsa_8021q_ctx),
3721 if (!priv->dsa_8021q_ctx)
3724 priv->dsa_8021q_ctx->ops = &sja1105_dsa_8021q_ops;
3725 priv->dsa_8021q_ctx->proto = htons(ETH_P_8021Q);
3726 priv->dsa_8021q_ctx->ds = ds;
3728 INIT_LIST_HEAD(&priv->dsa_8021q_ctx->crosschip_links);
3729 INIT_LIST_HEAD(&priv->bridge_vlans);
3730 INIT_LIST_HEAD(&priv->dsa_8021q_vlans);
3732 sja1105_tas_setup(ds);
3733 sja1105_flower_setup(ds);
3735 rc = dsa_register_switch(priv->ds);
3739 if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
3740 priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
3741 sizeof(struct sja1105_cbs_entry),
3745 goto out_unregister_switch;
3749 /* Connections between dsa_port and sja1105_port */
3750 for (port = 0; port < ds->num_ports; port++) {
3751 struct sja1105_port *sp = &priv->ports[port];
3752 struct dsa_port *dp = dsa_to_port(ds, port);
3753 struct net_device *slave;
3756 if (!dsa_is_user_port(ds, port))
3761 sp->data = tagger_data;
3763 kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
3764 sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
3766 if (IS_ERR(sp->xmit_worker)) {
3767 rc = PTR_ERR(sp->xmit_worker);
3769 "failed to create deferred xmit thread: %d\n",
3771 goto out_destroy_workers;
3773 skb_queue_head_init(&sp->xmit_queue);
3774 sp->xmit_tpid = ETH_P_SJA1105;
3776 for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
3777 sp->subvlan_map[subvlan] = VLAN_N_VID;
3782 out_destroy_workers:
3783 while (port-- > 0) {
3784 struct sja1105_port *sp = &priv->ports[port];
3786 if (!dsa_is_user_port(ds, port))
3789 kthread_destroy_worker(sp->xmit_worker);
3792 out_unregister_switch:
3793 dsa_unregister_switch(ds);
3798 static int sja1105_remove(struct spi_device *spi)
3800 struct sja1105_private *priv = spi_get_drvdata(spi);
3802 dsa_unregister_switch(priv->ds);
3806 static const struct of_device_id sja1105_dt_ids[] = {
3807 { .compatible = "nxp,sja1105e", .data = &sja1105e_info },
3808 { .compatible = "nxp,sja1105t", .data = &sja1105t_info },
3809 { .compatible = "nxp,sja1105p", .data = &sja1105p_info },
3810 { .compatible = "nxp,sja1105q", .data = &sja1105q_info },
3811 { .compatible = "nxp,sja1105r", .data = &sja1105r_info },
3812 { .compatible = "nxp,sja1105s", .data = &sja1105s_info },
3815 MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
3817 static struct spi_driver sja1105_driver = {
3820 .owner = THIS_MODULE,
3821 .of_match_table = of_match_ptr(sja1105_dt_ids),
3823 .probe = sja1105_probe,
3824 .remove = sja1105_remove,
3827 module_spi_driver(sja1105_driver);
3829 MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
3830 MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
3831 MODULE_DESCRIPTION("SJA1105 Driver");
3832 MODULE_LICENSE("GPL v2");