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_DEFAULT_VLAN (VLAN_N_VID - 1)
30 static const struct dsa_switch_ops sja1105_switch_ops;
32 static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
33 unsigned int startup_delay)
35 gpiod_set_value_cansleep(gpio, 1);
36 /* Wait for minimum reset pulse length */
38 gpiod_set_value_cansleep(gpio, 0);
39 /* Wait until chip is ready after reset */
40 msleep(startup_delay);
44 sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
45 int from, int to, bool allow)
48 l2_fwd[from].bc_domain |= BIT(to);
49 l2_fwd[from].reach_port |= BIT(to);
50 l2_fwd[from].fl_domain |= BIT(to);
52 l2_fwd[from].bc_domain &= ~BIT(to);
53 l2_fwd[from].reach_port &= ~BIT(to);
54 l2_fwd[from].fl_domain &= ~BIT(to);
58 /* Structure used to temporarily transport device tree
59 * settings into sja1105_setup
61 struct sja1105_dt_port {
62 phy_interface_t phy_mode;
63 sja1105_mii_role_t role;
66 static int sja1105_init_mac_settings(struct sja1105_private *priv)
68 struct sja1105_mac_config_entry default_mac = {
69 /* Enable all 8 priority queues on egress.
70 * Every queue i holds top[i] - base[i] frames.
71 * Sum of top[i] - base[i] is 511 (max hardware limit).
73 .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
74 .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
75 .enabled = {true, true, true, true, true, true, true, true},
76 /* Keep standard IFG of 12 bytes on egress. */
78 /* Always put the MAC speed in automatic mode, where it can be
79 * adjusted at runtime by PHYLINK.
81 .speed = SJA1105_SPEED_AUTO,
82 /* No static correction for 1-step 1588 events */
85 /* Disable aging for critical TTEthernet traffic */
87 /* Internal VLAN (pvid) to apply to untagged ingress */
92 /* Don't drop traffic with other EtherType than ETH_P_IP */
94 /* Don't drop double-tagged traffic */
96 /* Don't drop untagged traffic */
98 /* Don't retag 802.1p (VID 0) traffic with the pvid */
100 /* Disable learning and I/O on user ports by default -
101 * STP will enable it.
107 struct sja1105_mac_config_entry *mac;
108 struct sja1105_table *table;
111 table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
113 /* Discard previous MAC Configuration Table */
114 if (table->entry_count) {
115 kfree(table->entries);
116 table->entry_count = 0;
119 table->entries = kcalloc(SJA1105_NUM_PORTS,
120 table->ops->unpacked_entry_size, GFP_KERNEL);
124 table->entry_count = SJA1105_NUM_PORTS;
126 mac = table->entries;
128 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
129 mac[i] = default_mac;
130 if (i == dsa_upstream_port(priv->ds, i)) {
131 /* STP doesn't get called for CPU port, so we need to
132 * set the I/O parameters statically.
134 mac[i].dyn_learn = true;
135 mac[i].ingress = true;
136 mac[i].egress = true;
143 static bool sja1105_supports_sgmii(struct sja1105_private *priv, int port)
145 if (priv->info->part_no != SJA1105R_PART_NO &&
146 priv->info->part_no != SJA1105S_PART_NO)
149 if (port != SJA1105_SGMII_PORT)
152 if (dsa_is_unused_port(priv->ds, port))
158 static int sja1105_init_mii_settings(struct sja1105_private *priv,
159 struct sja1105_dt_port *ports)
161 struct device *dev = &priv->spidev->dev;
162 struct sja1105_xmii_params_entry *mii;
163 struct sja1105_table *table;
166 table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
168 /* Discard previous xMII Mode Parameters Table */
169 if (table->entry_count) {
170 kfree(table->entries);
171 table->entry_count = 0;
174 table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
175 table->ops->unpacked_entry_size, GFP_KERNEL);
179 /* Override table based on PHYLINK DT bindings */
180 table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
182 mii = table->entries;
184 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
185 if (dsa_is_unused_port(priv->ds, i))
188 switch (ports[i].phy_mode) {
189 case PHY_INTERFACE_MODE_MII:
190 mii->xmii_mode[i] = XMII_MODE_MII;
192 case PHY_INTERFACE_MODE_RMII:
193 mii->xmii_mode[i] = XMII_MODE_RMII;
195 case PHY_INTERFACE_MODE_RGMII:
196 case PHY_INTERFACE_MODE_RGMII_ID:
197 case PHY_INTERFACE_MODE_RGMII_RXID:
198 case PHY_INTERFACE_MODE_RGMII_TXID:
199 mii->xmii_mode[i] = XMII_MODE_RGMII;
201 case PHY_INTERFACE_MODE_SGMII:
202 if (!sja1105_supports_sgmii(priv, i))
204 mii->xmii_mode[i] = XMII_MODE_SGMII;
207 dev_err(dev, "Unsupported PHY mode %s!\n",
208 phy_modes(ports[i].phy_mode));
212 /* Even though the SerDes port is able to drive SGMII autoneg
213 * like a PHY would, from the perspective of the XMII tables,
214 * the SGMII port should always be put in MAC mode.
216 if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII)
217 mii->phy_mac[i] = XMII_MAC;
219 mii->phy_mac[i] = ports[i].role;
224 static int sja1105_init_static_fdb(struct sja1105_private *priv)
226 struct sja1105_table *table;
228 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
230 /* We only populate the FDB table through dynamic
231 * L2 Address Lookup entries
233 if (table->entry_count) {
234 kfree(table->entries);
235 table->entry_count = 0;
240 static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
242 struct sja1105_table *table;
243 u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
244 struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
245 /* Learned FDB entries are forgotten after 300 seconds */
246 .maxage = SJA1105_AGEING_TIME_MS(300000),
247 /* All entries within a FDB bin are available for learning */
248 .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
249 /* And the P/Q/R/S equivalent setting: */
251 .maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
252 max_fdb_entries, max_fdb_entries, },
253 /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
255 /* This selects between Independent VLAN Learning (IVL) and
256 * Shared VLAN Learning (SVL)
258 .shared_learn = true,
259 /* Don't discard management traffic based on ENFPORT -
260 * we don't perform SMAC port enforcement anyway, so
261 * what we are setting here doesn't matter.
263 .no_enf_hostprt = false,
264 /* Don't learn SMAC for mac_fltres1 and mac_fltres0.
265 * Maybe correlate with no_linklocal_learn from bridge driver?
267 .no_mgmt_learn = true,
270 /* Dynamically learned FDB entries can overwrite other (older)
271 * dynamic FDB entries
277 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
279 if (table->entry_count) {
280 kfree(table->entries);
281 table->entry_count = 0;
284 table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
285 table->ops->unpacked_entry_size, GFP_KERNEL);
289 table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
291 /* This table only has a single entry */
292 ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
293 default_l2_lookup_params;
298 /* Set up a default VLAN for untagged traffic injected from the CPU
299 * using management routes (e.g. STP, PTP) as opposed to tag_8021q.
300 * All DT-defined ports are members of this VLAN, and there are no
301 * restrictions on forwarding (since the CPU selects the destination).
302 * Frames from this VLAN will always be transmitted as untagged, and
303 * neither the bridge nor the 8021q module cannot create this VLAN ID.
305 static int sja1105_init_static_vlan(struct sja1105_private *priv)
307 struct sja1105_table *table;
308 struct sja1105_vlan_lookup_entry pvid = {
314 .vlanid = SJA1105_DEFAULT_VLAN,
316 struct dsa_switch *ds = priv->ds;
319 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
321 if (table->entry_count) {
322 kfree(table->entries);
323 table->entry_count = 0;
326 table->entries = kcalloc(1, table->ops->unpacked_entry_size,
331 table->entry_count = 1;
333 for (port = 0; port < ds->num_ports; port++) {
334 struct sja1105_bridge_vlan *v;
336 if (dsa_is_unused_port(ds, port))
339 pvid.vmemb_port |= BIT(port);
340 pvid.vlan_bc |= BIT(port);
341 pvid.tag_port &= ~BIT(port);
343 v = kzalloc(sizeof(*v), GFP_KERNEL);
348 v->vid = SJA1105_DEFAULT_VLAN;
350 if (dsa_is_cpu_port(ds, port))
352 list_add(&v->list, &priv->dsa_8021q_vlans);
354 v = kmemdup(v, sizeof(*v), GFP_KERNEL);
358 list_add(&v->list, &priv->bridge_vlans);
361 ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
365 static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
367 struct sja1105_l2_forwarding_entry *l2fwd;
368 struct sja1105_table *table;
371 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
373 if (table->entry_count) {
374 kfree(table->entries);
375 table->entry_count = 0;
378 table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
379 table->ops->unpacked_entry_size, GFP_KERNEL);
383 table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
385 l2fwd = table->entries;
387 /* First 5 entries define the forwarding rules */
388 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
389 unsigned int upstream = dsa_upstream_port(priv->ds, i);
391 for (j = 0; j < SJA1105_NUM_TC; j++)
392 l2fwd[i].vlan_pmap[j] = j;
397 sja1105_port_allow_traffic(l2fwd, i, upstream, true);
398 sja1105_port_allow_traffic(l2fwd, upstream, i, true);
400 /* Next 8 entries define VLAN PCP mapping from ingress to egress.
401 * Create a one-to-one mapping.
403 for (i = 0; i < SJA1105_NUM_TC; i++)
404 for (j = 0; j < SJA1105_NUM_PORTS; j++)
405 l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
410 static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
412 struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
413 /* Disallow dynamic reconfiguration of vlan_pmap */
415 /* Use a single memory partition for all ingress queues */
416 .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
418 struct sja1105_table *table;
420 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
422 if (table->entry_count) {
423 kfree(table->entries);
424 table->entry_count = 0;
427 table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
428 table->ops->unpacked_entry_size, GFP_KERNEL);
432 table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
434 /* This table only has a single entry */
435 ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
436 default_l2fwd_params;
441 void sja1105_frame_memory_partitioning(struct sja1105_private *priv)
443 struct sja1105_l2_forwarding_params_entry *l2_fwd_params;
444 struct sja1105_vl_forwarding_params_entry *vl_fwd_params;
445 struct sja1105_table *table;
448 /* VLAN retagging is implemented using a loopback port that consumes
449 * frame buffers. That leaves less for us.
451 if (priv->vlan_state == SJA1105_VLAN_BEST_EFFORT)
452 max_mem = SJA1105_MAX_FRAME_MEMORY_RETAGGING;
454 max_mem = SJA1105_MAX_FRAME_MEMORY;
456 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
457 l2_fwd_params = table->entries;
458 l2_fwd_params->part_spc[0] = max_mem;
460 /* If we have any critical-traffic virtual links, we need to reserve
461 * some frame buffer memory for them. At the moment, hardcode the value
462 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks
463 * remaining for best-effort traffic. TODO: figure out a more flexible
464 * way to perform the frame buffer partitioning.
466 if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count)
469 table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS];
470 vl_fwd_params = table->entries;
472 l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY;
473 vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY;
476 static int sja1105_init_general_params(struct sja1105_private *priv)
478 struct sja1105_general_params_entry default_general_params = {
479 /* Allow dynamic changing of the mirror port */
481 .switchid = priv->ds->index,
482 /* Priority queue for link-local management frames
483 * (both ingress to and egress from CPU - PTP, STP etc)
486 .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
487 .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK,
488 .incl_srcpt1 = false,
490 .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
491 .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK,
492 .incl_srcpt0 = false,
494 /* The destination for traffic matching mac_fltres1 and
495 * mac_fltres0 on all ports except host_port. Such traffic
496 * receieved on host_port itself would be dropped, except
497 * by installing a temporary 'management route'
499 .host_port = dsa_upstream_port(priv->ds, 0),
500 /* Default to an invalid value */
501 .mirr_port = SJA1105_NUM_PORTS,
502 /* Link-local traffic received on casc_port will be forwarded
503 * to host_port without embedding the source port and device ID
504 * info in the destination MAC address (presumably because it
505 * is a cascaded port and a downstream SJA switch already did
506 * that). Default to an invalid port (to disable the feature)
507 * and overwrite this if we find any DSA (cascaded) ports.
509 .casc_port = SJA1105_NUM_PORTS,
511 .vllupformat = SJA1105_VL_FORMAT_PSFP,
514 /* Only update correctionField for 1-step PTP (L2 transport) */
516 /* Forcefully disable VLAN filtering by telling
517 * the switch that VLAN has a different EtherType.
519 .tpid = ETH_P_SJA1105,
520 .tpid2 = ETH_P_SJA1105,
522 struct sja1105_table *table;
524 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
526 if (table->entry_count) {
527 kfree(table->entries);
528 table->entry_count = 0;
531 table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
532 table->ops->unpacked_entry_size, GFP_KERNEL);
536 table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
538 /* This table only has a single entry */
539 ((struct sja1105_general_params_entry *)table->entries)[0] =
540 default_general_params;
545 static int sja1105_init_avb_params(struct sja1105_private *priv)
547 struct sja1105_avb_params_entry *avb;
548 struct sja1105_table *table;
550 table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
552 /* Discard previous AVB Parameters Table */
553 if (table->entry_count) {
554 kfree(table->entries);
555 table->entry_count = 0;
558 table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
559 table->ops->unpacked_entry_size, GFP_KERNEL);
563 table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
565 avb = table->entries;
567 /* Configure the MAC addresses for meta frames */
568 avb->destmeta = SJA1105_META_DMAC;
569 avb->srcmeta = SJA1105_META_SMAC;
570 /* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
571 * default. This is because there might be boards with a hardware
572 * layout where enabling the pin as output might cause an electrical
573 * clash. On E/T the pin is always an output, which the board designers
574 * probably already knew, so even if there are going to be electrical
575 * issues, there's nothing we can do.
577 avb->cas_master = false;
582 /* The L2 policing table is 2-stage. The table is looked up for each frame
583 * according to the ingress port, whether it was broadcast or not, and the
584 * classified traffic class (given by VLAN PCP). This portion of the lookup is
585 * fixed, and gives access to the SHARINDX, an indirection register pointing
586 * within the policing table itself, which is used to resolve the policer that
587 * will be used for this frame.
590 * +------------+--------+ +---------------------------------+
591 * |Port 0 TC 0 |SHARINDX| | Policer 0: Rate, Burst, MTU |
592 * +------------+--------+ +---------------------------------+
593 * |Port 0 TC 1 |SHARINDX| | Policer 1: Rate, Burst, MTU |
594 * +------------+--------+ +---------------------------------+
595 * ... | Policer 2: Rate, Burst, MTU |
596 * +------------+--------+ +---------------------------------+
597 * |Port 0 TC 7 |SHARINDX| | Policer 3: Rate, Burst, MTU |
598 * +------------+--------+ +---------------------------------+
599 * |Port 1 TC 0 |SHARINDX| | Policer 4: Rate, Burst, MTU |
600 * +------------+--------+ +---------------------------------+
601 * ... | Policer 5: Rate, Burst, MTU |
602 * +------------+--------+ +---------------------------------+
603 * |Port 1 TC 7 |SHARINDX| | Policer 6: Rate, Burst, MTU |
604 * +------------+--------+ +---------------------------------+
605 * ... | Policer 7: Rate, Burst, MTU |
606 * +------------+--------+ +---------------------------------+
607 * |Port 4 TC 7 |SHARINDX| ...
608 * +------------+--------+
609 * |Port 0 BCAST|SHARINDX| ...
610 * +------------+--------+
611 * |Port 1 BCAST|SHARINDX| ...
612 * +------------+--------+
614 * +------------+--------+ +---------------------------------+
615 * |Port 4 BCAST|SHARINDX| | Policer 44: Rate, Burst, MTU |
616 * +------------+--------+ +---------------------------------+
618 * In this driver, we shall use policers 0-4 as statically alocated port
619 * (matchall) policers. So we need to make the SHARINDX for all lookups
620 * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast
622 * The remaining policers (40) shall be dynamically allocated for flower
623 * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff.
625 #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
627 static int sja1105_init_l2_policing(struct sja1105_private *priv)
629 struct sja1105_l2_policing_entry *policing;
630 struct sja1105_table *table;
633 table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
635 /* Discard previous L2 Policing Table */
636 if (table->entry_count) {
637 kfree(table->entries);
638 table->entry_count = 0;
641 table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
642 table->ops->unpacked_entry_size, GFP_KERNEL);
646 table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
648 policing = table->entries;
650 /* Setup shared indices for the matchall policers */
651 for (port = 0; port < SJA1105_NUM_PORTS; port++) {
652 int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + port;
654 for (tc = 0; tc < SJA1105_NUM_TC; tc++)
655 policing[port * SJA1105_NUM_TC + tc].sharindx = port;
657 policing[bcast].sharindx = port;
660 /* Setup the matchall policer parameters */
661 for (port = 0; port < SJA1105_NUM_PORTS; port++) {
662 int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
664 if (dsa_is_cpu_port(priv->ds, port))
667 policing[port].smax = 65535; /* Burst size in bytes */
668 policing[port].rate = SJA1105_RATE_MBPS(1000);
669 policing[port].maxlen = mtu;
670 policing[port].partition = 0;
676 static int sja1105_static_config_load(struct sja1105_private *priv,
677 struct sja1105_dt_port *ports)
681 sja1105_static_config_free(&priv->static_config);
682 rc = sja1105_static_config_init(&priv->static_config,
683 priv->info->static_ops,
684 priv->info->device_id);
688 /* Build static configuration */
689 rc = sja1105_init_mac_settings(priv);
692 rc = sja1105_init_mii_settings(priv, ports);
695 rc = sja1105_init_static_fdb(priv);
698 rc = sja1105_init_static_vlan(priv);
701 rc = sja1105_init_l2_lookup_params(priv);
704 rc = sja1105_init_l2_forwarding(priv);
707 rc = sja1105_init_l2_forwarding_params(priv);
710 rc = sja1105_init_l2_policing(priv);
713 rc = sja1105_init_general_params(priv);
716 rc = sja1105_init_avb_params(priv);
720 /* Send initial configuration to hardware via SPI */
721 return sja1105_static_config_upload(priv);
724 static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
725 const struct sja1105_dt_port *ports)
729 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
730 if (ports[i].role == XMII_MAC)
733 if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
734 ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
735 priv->rgmii_rx_delay[i] = true;
737 if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
738 ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
739 priv->rgmii_tx_delay[i] = true;
741 if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
742 !priv->info->setup_rgmii_delay)
748 static int sja1105_parse_ports_node(struct sja1105_private *priv,
749 struct sja1105_dt_port *ports,
750 struct device_node *ports_node)
752 struct device *dev = &priv->spidev->dev;
753 struct device_node *child;
755 for_each_available_child_of_node(ports_node, child) {
756 struct device_node *phy_node;
757 phy_interface_t phy_mode;
761 /* Get switch port number from DT */
762 if (of_property_read_u32(child, "reg", &index) < 0) {
763 dev_err(dev, "Port number not defined in device tree "
764 "(property \"reg\")\n");
769 /* Get PHY mode from DT */
770 err = of_get_phy_mode(child, &phy_mode);
772 dev_err(dev, "Failed to read phy-mode or "
773 "phy-interface-type property for port %d\n",
778 ports[index].phy_mode = phy_mode;
780 phy_node = of_parse_phandle(child, "phy-handle", 0);
782 if (!of_phy_is_fixed_link(child)) {
783 dev_err(dev, "phy-handle or fixed-link "
784 "properties missing!\n");
788 /* phy-handle is missing, but fixed-link isn't.
789 * So it's a fixed link. Default to PHY role.
791 ports[index].role = XMII_PHY;
793 /* phy-handle present => put port in MAC role */
794 ports[index].role = XMII_MAC;
795 of_node_put(phy_node);
798 /* The MAC/PHY role can be overridden with explicit bindings */
799 if (of_property_read_bool(child, "sja1105,role-mac"))
800 ports[index].role = XMII_MAC;
801 else if (of_property_read_bool(child, "sja1105,role-phy"))
802 ports[index].role = XMII_PHY;
808 static int sja1105_parse_dt(struct sja1105_private *priv,
809 struct sja1105_dt_port *ports)
811 struct device *dev = &priv->spidev->dev;
812 struct device_node *switch_node = dev->of_node;
813 struct device_node *ports_node;
816 ports_node = of_get_child_by_name(switch_node, "ports");
818 dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
822 rc = sja1105_parse_ports_node(priv, ports, ports_node);
823 of_node_put(ports_node);
828 static int sja1105_sgmii_read(struct sja1105_private *priv, int pcs_reg)
830 const struct sja1105_regs *regs = priv->info->regs;
834 rc = sja1105_xfer_u32(priv, SPI_READ, regs->sgmii + pcs_reg, &val,
842 static int sja1105_sgmii_write(struct sja1105_private *priv, int pcs_reg,
845 const struct sja1105_regs *regs = priv->info->regs;
849 rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->sgmii + pcs_reg, &val,
857 static void sja1105_sgmii_pcs_config(struct sja1105_private *priv,
858 bool an_enabled, bool an_master)
860 u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII;
862 /* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
863 * stop the clock during LPI mode, make the MAC reconfigure
864 * autonomously after PCS autoneg is done, flush the internal FIFOs.
866 sja1105_sgmii_write(priv, SJA1105_DC1, SJA1105_DC1_EN_VSMMD1 |
867 SJA1105_DC1_CLOCK_STOP_EN |
868 SJA1105_DC1_MAC_AUTO_SW |
870 /* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
871 sja1105_sgmii_write(priv, SJA1105_DC2, SJA1105_DC2_TX_POL_INV_DISABLE);
872 /* AUTONEG_CONTROL: Use SGMII autoneg */
874 ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK;
875 sja1105_sgmii_write(priv, SJA1105_AC, ac);
876 /* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
877 * sja1105_sgmii_pcs_force_speed must be called later for the link
878 * to become operational.
881 sja1105_sgmii_write(priv, MII_BMCR,
882 BMCR_ANENABLE | BMCR_ANRESTART);
885 static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv,
892 pcs_speed = BMCR_SPEED1000;
895 pcs_speed = BMCR_SPEED100;
898 pcs_speed = BMCR_SPEED10;
901 dev_err(priv->ds->dev, "Invalid speed %d\n", speed);
904 sja1105_sgmii_write(priv, MII_BMCR, pcs_speed | BMCR_FULLDPLX);
907 /* Convert link speed from SJA1105 to ethtool encoding */
908 static int sja1105_speed[] = {
909 [SJA1105_SPEED_AUTO] = SPEED_UNKNOWN,
910 [SJA1105_SPEED_10MBPS] = SPEED_10,
911 [SJA1105_SPEED_100MBPS] = SPEED_100,
912 [SJA1105_SPEED_1000MBPS] = SPEED_1000,
915 /* Set link speed in the MAC configuration for a specific port. */
916 static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
919 struct sja1105_xmii_params_entry *mii;
920 struct sja1105_mac_config_entry *mac;
921 struct device *dev = priv->ds->dev;
922 sja1105_phy_interface_t phy_mode;
923 sja1105_speed_t speed;
926 /* On P/Q/R/S, one can read from the device via the MAC reconfiguration
927 * tables. On E/T, MAC reconfig tables are not readable, only writable.
928 * We have to *know* what the MAC looks like. For the sake of keeping
929 * the code common, we'll use the static configuration tables as a
930 * reasonable approximation for both E/T and P/Q/R/S.
932 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
933 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
935 switch (speed_mbps) {
937 /* PHYLINK called sja1105_mac_config() to inform us about
938 * the state->interface, but AN has not completed and the
939 * speed is not yet valid. UM10944.pdf says that setting
940 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
941 * ok for power consumption in case AN will never complete -
942 * otherwise PHYLINK should come back with a new update.
944 speed = SJA1105_SPEED_AUTO;
947 speed = SJA1105_SPEED_10MBPS;
950 speed = SJA1105_SPEED_100MBPS;
953 speed = SJA1105_SPEED_1000MBPS;
956 dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
960 /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
961 * table, since this will be used for the clocking setup, and we no
962 * longer need to store it in the static config (already told hardware
963 * we want auto during upload phase).
964 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
965 * we need to configure the PCS only (if even that).
967 if (sja1105_supports_sgmii(priv, port))
968 mac[port].speed = SJA1105_SPEED_1000MBPS;
970 mac[port].speed = speed;
972 /* Write to the dynamic reconfiguration tables */
973 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
976 dev_err(dev, "Failed to write MAC config: %d\n", rc);
980 /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
981 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
982 * RMII no change of the clock setup is required. Actually, changing
983 * the clock setup does interrupt the clock signal for a certain time
984 * which causes trouble for all PHYs relying on this signal.
986 phy_mode = mii->xmii_mode[port];
987 if (phy_mode != XMII_MODE_RGMII)
990 return sja1105_clocking_setup_port(priv, port);
993 /* The SJA1105 MAC programming model is through the static config (the xMII
994 * Mode table cannot be dynamically reconfigured), and we have to program
995 * that early (earlier than PHYLINK calls us, anyway).
996 * So just error out in case the connected PHY attempts to change the initial
997 * system interface MII protocol from what is defined in the DT, at least for
1000 static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
1001 phy_interface_t interface)
1003 struct sja1105_xmii_params_entry *mii;
1004 sja1105_phy_interface_t phy_mode;
1006 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1007 phy_mode = mii->xmii_mode[port];
1009 switch (interface) {
1010 case PHY_INTERFACE_MODE_MII:
1011 return (phy_mode != XMII_MODE_MII);
1012 case PHY_INTERFACE_MODE_RMII:
1013 return (phy_mode != XMII_MODE_RMII);
1014 case PHY_INTERFACE_MODE_RGMII:
1015 case PHY_INTERFACE_MODE_RGMII_ID:
1016 case PHY_INTERFACE_MODE_RGMII_RXID:
1017 case PHY_INTERFACE_MODE_RGMII_TXID:
1018 return (phy_mode != XMII_MODE_RGMII);
1019 case PHY_INTERFACE_MODE_SGMII:
1020 return (phy_mode != XMII_MODE_SGMII);
1026 static void sja1105_mac_config(struct dsa_switch *ds, int port,
1028 const struct phylink_link_state *state)
1030 struct sja1105_private *priv = ds->priv;
1031 bool is_sgmii = sja1105_supports_sgmii(priv, port);
1033 if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1034 dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
1035 phy_modes(state->interface));
1039 if (phylink_autoneg_inband(mode) && !is_sgmii) {
1040 dev_err(ds->dev, "In-band AN not supported!\n");
1045 sja1105_sgmii_pcs_config(priv, phylink_autoneg_inband(mode),
1049 static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
1051 phy_interface_t interface)
1053 sja1105_inhibit_tx(ds->priv, BIT(port), true);
1056 static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
1058 phy_interface_t interface,
1059 struct phy_device *phydev,
1060 int speed, int duplex,
1061 bool tx_pause, bool rx_pause)
1063 struct sja1105_private *priv = ds->priv;
1065 sja1105_adjust_port_config(priv, port, speed);
1067 if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode))
1068 sja1105_sgmii_pcs_force_speed(priv, speed);
1070 sja1105_inhibit_tx(priv, BIT(port), false);
1073 static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
1074 unsigned long *supported,
1075 struct phylink_link_state *state)
1077 /* Construct a new mask which exhaustively contains all link features
1078 * supported by the MAC, and then apply that (logical AND) to what will
1079 * be sent to the PHY for "marketing".
1081 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1082 struct sja1105_private *priv = ds->priv;
1083 struct sja1105_xmii_params_entry *mii;
1085 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
1087 /* include/linux/phylink.h says:
1088 * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
1089 * expects the MAC driver to return all supported link modes.
1091 if (state->interface != PHY_INTERFACE_MODE_NA &&
1092 sja1105_phy_mode_mismatch(priv, port, state->interface)) {
1093 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
1097 /* The MAC does not support pause frames, and also doesn't
1098 * support half-duplex traffic modes.
1100 phylink_set(mask, Autoneg);
1101 phylink_set(mask, MII);
1102 phylink_set(mask, 10baseT_Full);
1103 phylink_set(mask, 100baseT_Full);
1104 phylink_set(mask, 100baseT1_Full);
1105 if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
1106 mii->xmii_mode[port] == XMII_MODE_SGMII)
1107 phylink_set(mask, 1000baseT_Full);
1109 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1110 bitmap_and(state->advertising, state->advertising, mask,
1111 __ETHTOOL_LINK_MODE_MASK_NBITS);
1114 static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port,
1115 struct phylink_link_state *state)
1117 struct sja1105_private *priv = ds->priv;
1120 /* Read the vendor-specific AUTONEG_INTR_STATUS register */
1121 ais = sja1105_sgmii_read(priv, SJA1105_AIS);
1125 switch (SJA1105_AIS_SPEED(ais)) {
1127 state->speed = SPEED_10;
1130 state->speed = SPEED_100;
1133 state->speed = SPEED_1000;
1136 dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n",
1137 SJA1105_AIS_SPEED(ais));
1139 state->duplex = SJA1105_AIS_DUPLEX_MODE(ais);
1140 state->an_complete = SJA1105_AIS_COMPLETE(ais);
1141 state->link = SJA1105_AIS_LINK_STATUS(ais);
1147 sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
1148 const struct sja1105_l2_lookup_entry *requested)
1150 struct sja1105_l2_lookup_entry *l2_lookup;
1151 struct sja1105_table *table;
1154 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1155 l2_lookup = table->entries;
1157 for (i = 0; i < table->entry_count; i++)
1158 if (l2_lookup[i].macaddr == requested->macaddr &&
1159 l2_lookup[i].vlanid == requested->vlanid &&
1160 l2_lookup[i].destports & BIT(port))
1166 /* We want FDB entries added statically through the bridge command to persist
1167 * across switch resets, which are a common thing during normal SJA1105
1168 * operation. So we have to back them up in the static configuration tables
1169 * and hence apply them on next static config upload... yay!
1172 sja1105_static_fdb_change(struct sja1105_private *priv, int port,
1173 const struct sja1105_l2_lookup_entry *requested,
1176 struct sja1105_l2_lookup_entry *l2_lookup;
1177 struct sja1105_table *table;
1180 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1182 match = sja1105_find_static_fdb_entry(priv, port, requested);
1184 /* Can't delete a missing entry. */
1188 /* No match => new entry */
1189 rc = sja1105_table_resize(table, table->entry_count + 1);
1193 match = table->entry_count - 1;
1196 /* Assign pointer after the resize (it may be new memory) */
1197 l2_lookup = table->entries;
1200 * If the job was to add this FDB entry, it's already done (mostly
1201 * anyway, since the port forwarding mask may have changed, case in
1202 * which we update it).
1203 * Otherwise we have to delete it.
1206 l2_lookup[match] = *requested;
1210 /* To remove, the strategy is to overwrite the element with
1211 * the last one, and then reduce the array size by 1
1213 l2_lookup[match] = l2_lookup[table->entry_count - 1];
1214 return sja1105_table_resize(table, table->entry_count - 1);
1217 /* First-generation switches have a 4-way set associative TCAM that
1218 * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1219 * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1220 * For the placement of a newly learnt FDB entry, the switch selects the bin
1221 * based on a hash function, and the way within that bin incrementally.
1223 static int sja1105et_fdb_index(int bin, int way)
1225 return bin * SJA1105ET_FDB_BIN_SIZE + way;
1228 static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1229 const u8 *addr, u16 vid,
1230 struct sja1105_l2_lookup_entry *match,
1235 for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1236 struct sja1105_l2_lookup_entry l2_lookup = {0};
1237 int index = sja1105et_fdb_index(bin, way);
1239 /* Skip unused entries, optionally marking them
1240 * into the return value
1242 if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1243 index, &l2_lookup)) {
1249 if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1250 l2_lookup.vlanid == vid) {
1256 /* Return an invalid entry index if not found */
1260 int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1261 const unsigned char *addr, u16 vid)
1263 struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp;
1264 struct sja1105_private *priv = ds->priv;
1265 struct device *dev = ds->dev;
1266 int last_unused = -1;
1270 bin = sja1105et_fdb_hash(priv, addr, vid);
1272 way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1273 &l2_lookup, &last_unused);
1275 /* We have an FDB entry. Is our port in the destination
1276 * mask? If yes, we need to do nothing. If not, we need
1277 * to rewrite the entry by adding this port to it.
1279 if ((l2_lookup.destports & BIT(port)) && l2_lookup.lockeds)
1281 l2_lookup.destports |= BIT(port);
1283 int index = sja1105et_fdb_index(bin, way);
1285 /* We don't have an FDB entry. We construct a new one and
1286 * try to find a place for it within the FDB table.
1288 l2_lookup.macaddr = ether_addr_to_u64(addr);
1289 l2_lookup.destports = BIT(port);
1290 l2_lookup.vlanid = vid;
1292 if (last_unused >= 0) {
1295 /* Bin is full, need to evict somebody.
1296 * Choose victim at random. If you get these messages
1297 * often, you may need to consider changing the
1298 * distribution function:
1299 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1301 get_random_bytes(&way, sizeof(u8));
1302 way %= SJA1105ET_FDB_BIN_SIZE;
1303 dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1306 sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1307 index, NULL, false);
1310 l2_lookup.lockeds = true;
1311 l2_lookup.index = sja1105et_fdb_index(bin, way);
1313 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1314 l2_lookup.index, &l2_lookup,
1319 /* Invalidate a dynamically learned entry if that exists */
1320 start = sja1105et_fdb_index(bin, 0);
1321 end = sja1105et_fdb_index(bin, way);
1323 for (i = start; i < end; i++) {
1324 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1331 if (tmp.macaddr != ether_addr_to_u64(addr) || tmp.vlanid != vid)
1334 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1342 return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1345 int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1346 const unsigned char *addr, u16 vid)
1348 struct sja1105_l2_lookup_entry l2_lookup = {0};
1349 struct sja1105_private *priv = ds->priv;
1350 int index, bin, way, rc;
1353 bin = sja1105et_fdb_hash(priv, addr, vid);
1354 way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1358 index = sja1105et_fdb_index(bin, way);
1360 /* We have an FDB entry. Is our port in the destination mask? If yes,
1361 * we need to remove it. If the resulting port mask becomes empty, we
1362 * need to completely evict the FDB entry.
1363 * Otherwise we just write it back.
1365 l2_lookup.destports &= ~BIT(port);
1367 if (l2_lookup.destports)
1372 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1373 index, &l2_lookup, keep);
1377 return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1380 int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
1381 const unsigned char *addr, u16 vid)
1383 struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp;
1384 struct sja1105_private *priv = ds->priv;
1387 /* Search for an existing entry in the FDB table */
1388 l2_lookup.macaddr = ether_addr_to_u64(addr);
1389 l2_lookup.vlanid = vid;
1390 l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
1391 l2_lookup.mask_vlanid = VLAN_VID_MASK;
1392 l2_lookup.destports = BIT(port);
1394 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1395 SJA1105_SEARCH, &l2_lookup);
1397 /* Found a static entry and this port is already in the entry's
1398 * port mask => job done
1400 if ((l2_lookup.destports & BIT(port)) && l2_lookup.lockeds)
1402 /* l2_lookup.index is populated by the switch in case it
1405 l2_lookup.destports |= BIT(port);
1406 goto skip_finding_an_index;
1409 /* Not found, so try to find an unused spot in the FDB.
1410 * This is slightly inefficient because the strategy is knock-knock at
1411 * every possible position from 0 to 1023.
1413 for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1414 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1419 if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
1420 dev_err(ds->dev, "FDB is full, cannot add entry.\n");
1423 l2_lookup.index = i;
1425 skip_finding_an_index:
1426 l2_lookup.lockeds = true;
1428 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1429 l2_lookup.index, &l2_lookup,
1434 /* The switch learns dynamic entries and looks up the FDB left to
1435 * right. It is possible that our addition was concurrent with the
1436 * dynamic learning of the same address, so now that the static entry
1437 * has been installed, we are certain that address learning for this
1438 * particular address has been turned off, so the dynamic entry either
1439 * is in the FDB at an index smaller than the static one, or isn't (it
1440 * can also be at a larger index, but in that case it is inactive
1441 * because the static FDB entry will match first, and the dynamic one
1442 * will eventually age out). Search for a dynamically learned address
1443 * prior to our static one and invalidate it.
1447 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1448 SJA1105_SEARCH, &tmp);
1451 "port %d failed to read back entry for %pM vid %d: %pe\n",
1452 port, addr, vid, ERR_PTR(rc));
1456 if (tmp.index < l2_lookup.index) {
1457 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1458 tmp.index, NULL, false);
1463 return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1466 int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
1467 const unsigned char *addr, u16 vid)
1469 struct sja1105_l2_lookup_entry l2_lookup = {0};
1470 struct sja1105_private *priv = ds->priv;
1474 l2_lookup.macaddr = ether_addr_to_u64(addr);
1475 l2_lookup.vlanid = vid;
1476 l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
1477 l2_lookup.mask_vlanid = VLAN_VID_MASK;
1478 l2_lookup.destports = BIT(port);
1480 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1481 SJA1105_SEARCH, &l2_lookup);
1485 l2_lookup.destports &= ~BIT(port);
1487 /* Decide whether we remove just this port from the FDB entry,
1488 * or if we remove it completely.
1490 if (l2_lookup.destports)
1495 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1496 l2_lookup.index, &l2_lookup, keep);
1500 return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1503 static int sja1105_fdb_add(struct dsa_switch *ds, int port,
1504 const unsigned char *addr, u16 vid)
1506 struct sja1105_private *priv = ds->priv;
1508 /* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
1509 * so the switch still does some VLAN processing internally.
1510 * But Shared VLAN Learning (SVL) is also active, and it will take
1511 * care of autonomous forwarding between the unique pvid's of each
1512 * port. Here we just make sure that users can't add duplicate FDB
1513 * entries when in this mode - the actual VID doesn't matter except
1514 * for what gets printed in 'bridge fdb show'. In the case of zero,
1515 * no VID gets printed at all.
1517 if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
1520 return priv->info->fdb_add_cmd(ds, port, addr, vid);
1523 static int sja1105_fdb_del(struct dsa_switch *ds, int port,
1524 const unsigned char *addr, u16 vid)
1526 struct sja1105_private *priv = ds->priv;
1528 if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL)
1531 return priv->info->fdb_del_cmd(ds, port, addr, vid);
1534 static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1535 dsa_fdb_dump_cb_t *cb, void *data)
1537 struct sja1105_private *priv = ds->priv;
1538 struct device *dev = ds->dev;
1541 for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1542 struct sja1105_l2_lookup_entry l2_lookup = {0};
1543 u8 macaddr[ETH_ALEN];
1546 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1548 /* No fdb entry at i, not an issue */
1552 dev_err(dev, "Failed to dump FDB: %d\n", rc);
1556 /* FDB dump callback is per port. This means we have to
1557 * disregard a valid entry if it's not for this port, even if
1558 * only to revisit it later. This is inefficient because the
1559 * 1024-sized FDB table needs to be traversed 4 times through
1560 * SPI during a 'bridge fdb show' command.
1562 if (!(l2_lookup.destports & BIT(port)))
1564 u64_to_ether_addr(l2_lookup.macaddr, macaddr);
1566 /* We need to hide the dsa_8021q VLANs from the user. */
1567 if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
1568 l2_lookup.vlanid = 0;
1569 rc = cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1576 /* This callback needs to be present */
1577 static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1578 const struct switchdev_obj_port_mdb *mdb)
1583 static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1584 const struct switchdev_obj_port_mdb *mdb)
1586 sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1589 static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1590 const struct switchdev_obj_port_mdb *mdb)
1592 return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1595 static int sja1105_bridge_member(struct dsa_switch *ds, int port,
1596 struct net_device *br, bool member)
1598 struct sja1105_l2_forwarding_entry *l2_fwd;
1599 struct sja1105_private *priv = ds->priv;
1602 l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
1604 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1605 /* Add this port to the forwarding matrix of the
1606 * other ports in the same bridge, and viceversa.
1608 if (!dsa_is_user_port(ds, i))
1610 /* For the ports already under the bridge, only one thing needs
1611 * to be done, and that is to add this port to their
1612 * reachability domain. So we can perform the SPI write for
1613 * them immediately. However, for this port itself (the one
1614 * that is new to the bridge), we need to add all other ports
1615 * to its reachability domain. So we do that incrementally in
1616 * this loop, and perform the SPI write only at the end, once
1617 * the domain contains all other bridge ports.
1621 if (dsa_to_port(ds, i)->bridge_dev != br)
1623 sja1105_port_allow_traffic(l2_fwd, i, port, member);
1624 sja1105_port_allow_traffic(l2_fwd, port, i, member);
1626 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
1627 i, &l2_fwd[i], true);
1632 return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
1633 port, &l2_fwd[port], true);
1636 static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1639 struct sja1105_private *priv = ds->priv;
1640 struct sja1105_mac_config_entry *mac;
1642 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1645 case BR_STATE_DISABLED:
1646 case BR_STATE_BLOCKING:
1647 /* From UM10944 description of DRPDTAG (why put this there?):
1648 * "Management traffic flows to the port regardless of the state
1649 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1650 * At the moment no difference between DISABLED and BLOCKING.
1652 mac[port].ingress = false;
1653 mac[port].egress = false;
1654 mac[port].dyn_learn = false;
1656 case BR_STATE_LISTENING:
1657 mac[port].ingress = true;
1658 mac[port].egress = false;
1659 mac[port].dyn_learn = false;
1661 case BR_STATE_LEARNING:
1662 mac[port].ingress = true;
1663 mac[port].egress = false;
1664 mac[port].dyn_learn = true;
1666 case BR_STATE_FORWARDING:
1667 mac[port].ingress = true;
1668 mac[port].egress = true;
1669 mac[port].dyn_learn = true;
1672 dev_err(ds->dev, "invalid STP state: %d\n", state);
1676 sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1680 static int sja1105_bridge_join(struct dsa_switch *ds, int port,
1681 struct net_device *br)
1683 return sja1105_bridge_member(ds, port, br, true);
1686 static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
1687 struct net_device *br)
1689 sja1105_bridge_member(ds, port, br, false);
1692 #define BYTES_PER_KBIT (1000LL / 8)
1694 static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv)
1698 for (i = 0; i < priv->info->num_cbs_shapers; i++)
1699 if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope)
1705 static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port,
1710 for (i = 0; i < priv->info->num_cbs_shapers; i++) {
1711 struct sja1105_cbs_entry *cbs = &priv->cbs[i];
1713 if (cbs->port == port && cbs->prio == prio) {
1714 memset(cbs, 0, sizeof(*cbs));
1715 return sja1105_dynamic_config_write(priv, BLK_IDX_CBS,
1723 static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port,
1724 struct tc_cbs_qopt_offload *offload)
1726 struct sja1105_private *priv = ds->priv;
1727 struct sja1105_cbs_entry *cbs;
1730 if (!offload->enable)
1731 return sja1105_delete_cbs_shaper(priv, port, offload->queue);
1733 index = sja1105_find_unused_cbs_shaper(priv);
1737 cbs = &priv->cbs[index];
1739 cbs->prio = offload->queue;
1740 /* locredit and sendslope are negative by definition. In hardware,
1741 * positive values must be provided, and the negative sign is implicit.
1743 cbs->credit_hi = offload->hicredit;
1744 cbs->credit_lo = abs(offload->locredit);
1745 /* User space is in kbits/sec, hardware in bytes/sec */
1746 cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT;
1747 cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT);
1748 /* Convert the negative values from 64-bit 2's complement
1749 * to 32-bit 2's complement (for the case of 0x80000000 whose
1750 * negative is still negative).
1752 cbs->credit_lo &= GENMASK_ULL(31, 0);
1753 cbs->send_slope &= GENMASK_ULL(31, 0);
1755 return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs,
1759 static int sja1105_reload_cbs(struct sja1105_private *priv)
1763 /* The credit based shapers are only allocated if
1764 * CONFIG_NET_SCH_CBS is enabled.
1769 for (i = 0; i < priv->info->num_cbs_shapers; i++) {
1770 struct sja1105_cbs_entry *cbs = &priv->cbs[i];
1772 if (!cbs->idle_slope && !cbs->send_slope)
1775 rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs,
1784 static const char * const sja1105_reset_reasons[] = {
1785 [SJA1105_VLAN_FILTERING] = "VLAN filtering",
1786 [SJA1105_RX_HWTSTAMPING] = "RX timestamping",
1787 [SJA1105_AGEING_TIME] = "Ageing time",
1788 [SJA1105_SCHEDULING] = "Time-aware scheduling",
1789 [SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing",
1790 [SJA1105_VIRTUAL_LINKS] = "Virtual links",
1793 /* For situations where we need to change a setting at runtime that is only
1794 * available through the static configuration, resetting the switch in order
1795 * to upload the new static config is unavoidable. Back up the settings we
1796 * modify at runtime (currently only MAC) and restore them after uploading,
1797 * such that this operation is relatively seamless.
1799 int sja1105_static_config_reload(struct sja1105_private *priv,
1800 enum sja1105_reset_reason reason)
1802 struct ptp_system_timestamp ptp_sts_before;
1803 struct ptp_system_timestamp ptp_sts_after;
1804 struct sja1105_mac_config_entry *mac;
1805 int speed_mbps[SJA1105_NUM_PORTS];
1806 struct dsa_switch *ds = priv->ds;
1813 mutex_lock(&priv->mgmt_lock);
1815 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1817 /* Back up the dynamic link speed changed by sja1105_adjust_port_config
1818 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
1819 * switch wants to see in the static config in order to allow us to
1820 * change it through the dynamic interface later.
1822 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1823 speed_mbps[i] = sja1105_speed[mac[i].speed];
1824 mac[i].speed = SJA1105_SPEED_AUTO;
1827 if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT))
1828 bmcr = sja1105_sgmii_read(priv, MII_BMCR);
1830 /* No PTP operations can run right now */
1831 mutex_lock(&priv->ptp_data.lock);
1833 rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
1835 goto out_unlock_ptp;
1837 /* Reset switch and send updated static configuration */
1838 rc = sja1105_static_config_upload(priv);
1840 goto out_unlock_ptp;
1842 rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
1844 goto out_unlock_ptp;
1846 t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
1847 t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
1848 t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
1849 t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
1850 /* Mid point, corresponds to pre-reset PTPCLKVAL */
1851 t12 = t1 + (t2 - t1) / 2;
1852 /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
1853 t34 = t3 + (t4 - t3) / 2;
1854 /* Advance PTPCLKVAL by the time it took since its readout */
1857 __sja1105_ptp_adjtime(ds, now);
1860 mutex_unlock(&priv->ptp_data.lock);
1862 dev_info(priv->ds->dev,
1863 "Reset switch and programmed static config. Reason: %s\n",
1864 sja1105_reset_reasons[reason]);
1866 /* Configure the CGU (PLLs) for MII and RMII PHYs.
1867 * For these interfaces there is no dynamic configuration
1868 * needed, since PLLs have same settings at all speeds.
1870 rc = sja1105_clocking_setup(priv);
1874 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1875 rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
1880 if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) {
1881 bool an_enabled = !!(bmcr & BMCR_ANENABLE);
1883 sja1105_sgmii_pcs_config(priv, an_enabled, false);
1886 int speed = SPEED_UNKNOWN;
1888 if (bmcr & BMCR_SPEED1000)
1890 else if (bmcr & BMCR_SPEED100)
1895 sja1105_sgmii_pcs_force_speed(priv, speed);
1899 rc = sja1105_reload_cbs(priv);
1903 mutex_unlock(&priv->mgmt_lock);
1908 static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
1910 struct sja1105_mac_config_entry *mac;
1912 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1914 mac[port].vlanid = pvid;
1916 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1920 static int sja1105_crosschip_bridge_join(struct dsa_switch *ds,
1921 int tree_index, int sw_index,
1922 int other_port, struct net_device *br)
1924 struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1925 struct sja1105_private *other_priv = other_ds->priv;
1926 struct sja1105_private *priv = ds->priv;
1929 if (other_ds->ops != &sja1105_switch_ops)
1932 for (port = 0; port < ds->num_ports; port++) {
1933 if (!dsa_is_user_port(ds, port))
1935 if (dsa_to_port(ds, port)->bridge_dev != br)
1938 rc = dsa_8021q_crosschip_bridge_join(priv->dsa_8021q_ctx,
1940 other_priv->dsa_8021q_ctx,
1945 rc = dsa_8021q_crosschip_bridge_join(other_priv->dsa_8021q_ctx,
1947 priv->dsa_8021q_ctx,
1956 static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds,
1957 int tree_index, int sw_index,
1959 struct net_device *br)
1961 struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index);
1962 struct sja1105_private *other_priv = other_ds->priv;
1963 struct sja1105_private *priv = ds->priv;
1966 if (other_ds->ops != &sja1105_switch_ops)
1969 for (port = 0; port < ds->num_ports; port++) {
1970 if (!dsa_is_user_port(ds, port))
1972 if (dsa_to_port(ds, port)->bridge_dev != br)
1975 dsa_8021q_crosschip_bridge_leave(priv->dsa_8021q_ctx, port,
1976 other_priv->dsa_8021q_ctx,
1979 dsa_8021q_crosschip_bridge_leave(other_priv->dsa_8021q_ctx,
1981 priv->dsa_8021q_ctx, port);
1985 static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1987 struct sja1105_private *priv = ds->priv;
1990 rc = dsa_8021q_setup(priv->dsa_8021q_ctx, enabled);
1994 dev_info(ds->dev, "%s switch tagging\n",
1995 enabled ? "Enabled" : "Disabled");
1999 static enum dsa_tag_protocol
2000 sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
2001 enum dsa_tag_protocol mp)
2003 return DSA_TAG_PROTO_SJA1105;
2006 static int sja1105_find_free_subvlan(u16 *subvlan_map, bool pvid)
2013 for (subvlan = 1; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2014 if (subvlan_map[subvlan] == VLAN_N_VID)
2020 static int sja1105_find_subvlan(u16 *subvlan_map, u16 vid)
2024 for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2025 if (subvlan_map[subvlan] == vid)
2031 static int sja1105_find_committed_subvlan(struct sja1105_private *priv,
2034 struct sja1105_port *sp = &priv->ports[port];
2036 return sja1105_find_subvlan(sp->subvlan_map, vid);
2039 static void sja1105_init_subvlan_map(u16 *subvlan_map)
2043 for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2044 subvlan_map[subvlan] = VLAN_N_VID;
2047 static void sja1105_commit_subvlan_map(struct sja1105_private *priv, int port,
2050 struct sja1105_port *sp = &priv->ports[port];
2053 for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
2054 sp->subvlan_map[subvlan] = subvlan_map[subvlan];
2057 static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
2059 struct sja1105_vlan_lookup_entry *vlan;
2062 vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
2063 count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
2065 for (i = 0; i < count; i++)
2066 if (vlan[i].vlanid == vid)
2069 /* Return an invalid entry index if not found */
2074 sja1105_find_retagging_entry(struct sja1105_retagging_entry *retagging,
2075 int count, int from_port, u16 from_vid,
2080 for (i = 0; i < count; i++)
2081 if (retagging[i].ing_port == BIT(from_port) &&
2082 retagging[i].vlan_ing == from_vid &&
2083 retagging[i].vlan_egr == to_vid)
2086 /* Return an invalid entry index if not found */
2090 static int sja1105_commit_vlans(struct sja1105_private *priv,
2091 struct sja1105_vlan_lookup_entry *new_vlan,
2092 struct sja1105_retagging_entry *new_retagging,
2095 struct sja1105_retagging_entry *retagging;
2096 struct sja1105_vlan_lookup_entry *vlan;
2097 struct sja1105_table *table;
2102 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2103 vlan = table->entries;
2105 for (i = 0; i < VLAN_N_VID; i++) {
2106 int match = sja1105_is_vlan_configured(priv, i);
2108 if (new_vlan[i].vlanid != VLAN_N_VID)
2111 if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) {
2112 /* Was there before, no longer is. Delete */
2113 dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i);
2114 rc = sja1105_dynamic_config_write(priv,
2115 BLK_IDX_VLAN_LOOKUP,
2116 i, &vlan[match], false);
2119 } else if (new_vlan[i].vlanid != VLAN_N_VID) {
2120 /* Nothing changed, don't do anything */
2122 vlan[match].vlanid == new_vlan[i].vlanid &&
2123 vlan[match].tag_port == new_vlan[i].tag_port &&
2124 vlan[match].vlan_bc == new_vlan[i].vlan_bc &&
2125 vlan[match].vmemb_port == new_vlan[i].vmemb_port)
2128 dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i);
2129 rc = sja1105_dynamic_config_write(priv,
2130 BLK_IDX_VLAN_LOOKUP,
2138 if (table->entry_count)
2139 kfree(table->entries);
2141 table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size,
2143 if (!table->entries)
2146 table->entry_count = num_vlans;
2147 vlan = table->entries;
2149 for (i = 0; i < VLAN_N_VID; i++) {
2150 if (new_vlan[i].vlanid == VLAN_N_VID)
2152 vlan[k++] = new_vlan[i];
2155 /* VLAN Retagging Table */
2156 table = &priv->static_config.tables[BLK_IDX_RETAGGING];
2157 retagging = table->entries;
2159 for (i = 0; i < table->entry_count; i++) {
2160 rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
2161 i, &retagging[i], false);
2166 if (table->entry_count)
2167 kfree(table->entries);
2169 table->entries = kcalloc(num_retagging, table->ops->unpacked_entry_size,
2171 if (!table->entries)
2174 table->entry_count = num_retagging;
2175 retagging = table->entries;
2177 for (i = 0; i < num_retagging; i++) {
2178 retagging[i] = new_retagging[i];
2181 rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING,
2182 i, &retagging[i], true);
2190 struct sja1105_crosschip_vlan {
2191 struct list_head list;
2196 struct dsa_8021q_context *other_ctx;
2199 struct sja1105_crosschip_switch {
2200 struct list_head list;
2201 struct dsa_8021q_context *other_ctx;
2204 static int sja1105_commit_pvid(struct sja1105_private *priv)
2206 struct sja1105_bridge_vlan *v;
2207 struct list_head *vlan_list;
2210 if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2211 vlan_list = &priv->bridge_vlans;
2213 vlan_list = &priv->dsa_8021q_vlans;
2215 list_for_each_entry(v, vlan_list, list) {
2217 rc = sja1105_pvid_apply(priv, v->port, v->vid);
2227 sja1105_build_bridge_vlans(struct sja1105_private *priv,
2228 struct sja1105_vlan_lookup_entry *new_vlan)
2230 struct sja1105_bridge_vlan *v;
2232 if (priv->vlan_state == SJA1105_VLAN_UNAWARE)
2235 list_for_each_entry(v, &priv->bridge_vlans, list) {
2238 new_vlan[match].vlanid = v->vid;
2239 new_vlan[match].vmemb_port |= BIT(v->port);
2240 new_vlan[match].vlan_bc |= BIT(v->port);
2242 new_vlan[match].tag_port |= BIT(v->port);
2249 sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv,
2250 struct sja1105_vlan_lookup_entry *new_vlan)
2252 struct sja1105_bridge_vlan *v;
2254 if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2257 list_for_each_entry(v, &priv->dsa_8021q_vlans, list) {
2260 new_vlan[match].vlanid = v->vid;
2261 new_vlan[match].vmemb_port |= BIT(v->port);
2262 new_vlan[match].vlan_bc |= BIT(v->port);
2264 new_vlan[match].tag_port |= BIT(v->port);
2270 static int sja1105_build_subvlans(struct sja1105_private *priv,
2271 u16 subvlan_map[][DSA_8021Q_N_SUBVLAN],
2272 struct sja1105_vlan_lookup_entry *new_vlan,
2273 struct sja1105_retagging_entry *new_retagging,
2276 struct sja1105_bridge_vlan *v;
2277 int k = *num_retagging;
2279 if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
2282 list_for_each_entry(v, &priv->bridge_vlans, list) {
2283 int upstream = dsa_upstream_port(priv->ds, v->port);
2287 /* Only sub-VLANs on user ports need to be applied.
2288 * Bridge VLANs also include VLANs added automatically
2289 * by DSA on the CPU port.
2291 if (!dsa_is_user_port(priv->ds, v->port))
2294 subvlan = sja1105_find_subvlan(subvlan_map[v->port],
2297 subvlan = sja1105_find_free_subvlan(subvlan_map[v->port],
2300 dev_err(priv->ds->dev, "No more free subvlans\n");
2305 rx_vid = dsa_8021q_rx_vid_subvlan(priv->ds, v->port, subvlan);
2307 /* @v->vid on @v->port needs to be retagged to @rx_vid
2308 * on @upstream. Assume @v->vid on @v->port and on
2309 * @upstream was already configured by the previous
2310 * iteration over bridge_vlans.
2313 new_vlan[match].vlanid = rx_vid;
2314 new_vlan[match].vmemb_port |= BIT(v->port);
2315 new_vlan[match].vmemb_port |= BIT(upstream);
2316 new_vlan[match].vlan_bc |= BIT(v->port);
2317 new_vlan[match].vlan_bc |= BIT(upstream);
2318 /* The "untagged" flag is set the same as for the
2322 new_vlan[match].tag_port |= BIT(v->port);
2323 /* But it's always tagged towards the CPU */
2324 new_vlan[match].tag_port |= BIT(upstream);
2326 /* The Retagging Table generates packet *clones* with
2327 * the new VLAN. This is a very odd hardware quirk
2328 * which we need to suppress by dropping the original
2330 * Deny egress of the original VLAN towards the CPU
2331 * port. This will force the switch to drop it, and
2332 * we'll see only the retagged packets.
2335 new_vlan[match].vlan_bc &= ~BIT(upstream);
2337 /* And the retagging itself */
2338 new_retagging[k].vlan_ing = v->vid;
2339 new_retagging[k].vlan_egr = rx_vid;
2340 new_retagging[k].ing_port = BIT(v->port);
2341 new_retagging[k].egr_port = BIT(upstream);
2342 if (k++ == SJA1105_MAX_RETAGGING_COUNT) {
2343 dev_err(priv->ds->dev, "No more retagging rules\n");
2347 subvlan_map[v->port][subvlan] = v->vid;
2355 /* Sadly, in crosschip scenarios where the CPU port is also the link to another
2356 * switch, we should retag backwards (the dsa_8021q vid to the original vid) on
2357 * the CPU port of neighbour switches.
2360 sja1105_build_crosschip_subvlans(struct sja1105_private *priv,
2361 struct sja1105_vlan_lookup_entry *new_vlan,
2362 struct sja1105_retagging_entry *new_retagging,
2365 struct sja1105_crosschip_vlan *tmp, *pos;
2366 struct dsa_8021q_crosschip_link *c;
2367 struct sja1105_bridge_vlan *v, *w;
2368 struct list_head crosschip_vlans;
2369 int k = *num_retagging;
2372 if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT)
2375 INIT_LIST_HEAD(&crosschip_vlans);
2377 list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
2378 struct sja1105_private *other_priv = c->other_ctx->ds->priv;
2380 if (other_priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2383 /* Crosschip links are also added to the CPU ports.
2386 if (!dsa_is_user_port(priv->ds, c->port))
2388 if (!dsa_is_user_port(c->other_ctx->ds, c->other_port))
2391 /* Search for VLANs on the remote port */
2392 list_for_each_entry(v, &other_priv->bridge_vlans, list) {
2393 bool already_added = false;
2394 bool we_have_it = false;
2396 if (v->port != c->other_port)
2399 /* If @v is a pvid on @other_ds, it does not need
2400 * re-retagging, because its SVL field is 0 and we
2401 * already allow that, via the dsa_8021q crosschip
2407 /* Search for the VLAN on our local port */
2408 list_for_each_entry(w, &priv->bridge_vlans, list) {
2409 if (w->port == c->port && w->vid == v->vid) {
2418 list_for_each_entry(tmp, &crosschip_vlans, list) {
2419 if (tmp->vid == v->vid &&
2420 tmp->untagged == v->untagged &&
2421 tmp->port == c->port &&
2422 tmp->other_port == v->port &&
2423 tmp->other_ctx == c->other_ctx) {
2424 already_added = true;
2432 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
2434 dev_err(priv->ds->dev, "Failed to allocate memory\n");
2439 tmp->port = c->port;
2440 tmp->other_port = v->port;
2441 tmp->other_ctx = c->other_ctx;
2442 tmp->untagged = v->untagged;
2443 list_add(&tmp->list, &crosschip_vlans);
2447 list_for_each_entry(tmp, &crosschip_vlans, list) {
2448 struct sja1105_private *other_priv = tmp->other_ctx->ds->priv;
2449 int upstream = dsa_upstream_port(priv->ds, tmp->port);
2453 subvlan = sja1105_find_committed_subvlan(other_priv,
2456 /* If this happens, it's a bug. The neighbour switch does not
2457 * have a subvlan for tmp->vid on tmp->other_port, but it
2458 * should, since we already checked for its vlan_state.
2460 if (WARN_ON(subvlan < 0)) {
2465 rx_vid = dsa_8021q_rx_vid_subvlan(tmp->other_ctx->ds,
2469 /* The @rx_vid retagged from @tmp->vid on
2470 * {@tmp->other_ds, @tmp->other_port} needs to be
2471 * re-retagged to @tmp->vid on the way back to us.
2473 * Assume the original @tmp->vid is already configured
2474 * on this local switch, otherwise we wouldn't be
2475 * retagging its subvlan on the other switch in the
2476 * first place. We just need to add a reverse retagging
2477 * rule for @rx_vid and install @rx_vid on our ports.
2480 new_vlan[match].vlanid = rx_vid;
2481 new_vlan[match].vmemb_port |= BIT(tmp->port);
2482 new_vlan[match].vmemb_port |= BIT(upstream);
2483 /* The "untagged" flag is set the same as for the
2484 * original VLAN. And towards the CPU, it doesn't
2485 * really matter, because @rx_vid will only receive
2486 * traffic on that port. For consistency with other dsa_8021q
2487 * VLANs, we'll keep the CPU port tagged.
2490 new_vlan[match].tag_port |= BIT(tmp->port);
2491 new_vlan[match].tag_port |= BIT(upstream);
2492 /* Deny egress of @rx_vid towards our front-panel port.
2493 * This will force the switch to drop it, and we'll see
2494 * only the re-retagged packets (having the original,
2495 * pre-initial-retagging, VLAN @tmp->vid).
2497 new_vlan[match].vlan_bc &= ~BIT(tmp->port);
2499 /* On reverse retagging, the same ingress VLAN goes to multiple
2500 * ports. So we have an opportunity to create composite rules
2501 * to not waste the limited space in the retagging table.
2503 k = sja1105_find_retagging_entry(new_retagging, *num_retagging,
2504 upstream, rx_vid, tmp->vid);
2506 if (*num_retagging == SJA1105_MAX_RETAGGING_COUNT) {
2507 dev_err(priv->ds->dev, "No more retagging rules\n");
2511 k = (*num_retagging)++;
2513 /* And the retagging itself */
2514 new_retagging[k].vlan_ing = rx_vid;
2515 new_retagging[k].vlan_egr = tmp->vid;
2516 new_retagging[k].ing_port = BIT(upstream);
2517 new_retagging[k].egr_port |= BIT(tmp->port);
2521 list_for_each_entry_safe(tmp, pos, &crosschip_vlans, list) {
2522 list_del(&tmp->list);
2529 static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify);
2531 static int sja1105_notify_crosschip_switches(struct sja1105_private *priv)
2533 struct sja1105_crosschip_switch *s, *pos;
2534 struct list_head crosschip_switches;
2535 struct dsa_8021q_crosschip_link *c;
2538 INIT_LIST_HEAD(&crosschip_switches);
2540 list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) {
2541 bool already_added = false;
2543 list_for_each_entry(s, &crosschip_switches, list) {
2544 if (s->other_ctx == c->other_ctx) {
2545 already_added = true;
2553 s = kzalloc(sizeof(*s), GFP_KERNEL);
2555 dev_err(priv->ds->dev, "Failed to allocate memory\n");
2559 s->other_ctx = c->other_ctx;
2560 list_add(&s->list, &crosschip_switches);
2563 list_for_each_entry(s, &crosschip_switches, list) {
2564 struct sja1105_private *other_priv = s->other_ctx->ds->priv;
2566 rc = sja1105_build_vlan_table(other_priv, false);
2572 list_for_each_entry_safe(s, pos, &crosschip_switches, list) {
2580 static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify)
2582 u16 subvlan_map[SJA1105_NUM_PORTS][DSA_8021Q_N_SUBVLAN];
2583 struct sja1105_retagging_entry *new_retagging;
2584 struct sja1105_vlan_lookup_entry *new_vlan;
2585 struct sja1105_table *table;
2586 int i, num_retagging = 0;
2589 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2590 new_vlan = kcalloc(VLAN_N_VID,
2591 table->ops->unpacked_entry_size, GFP_KERNEL);
2595 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2596 new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT,
2597 table->ops->unpacked_entry_size, GFP_KERNEL);
2598 if (!new_retagging) {
2603 for (i = 0; i < VLAN_N_VID; i++)
2604 new_vlan[i].vlanid = VLAN_N_VID;
2606 for (i = 0; i < SJA1105_MAX_RETAGGING_COUNT; i++)
2607 new_retagging[i].vlan_ing = VLAN_N_VID;
2609 for (i = 0; i < priv->ds->num_ports; i++)
2610 sja1105_init_subvlan_map(subvlan_map[i]);
2613 rc = sja1105_build_bridge_vlans(priv, new_vlan);
2617 /* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c:
2622 rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan);
2626 /* Private VLANs necessary for dsa_8021q operation, which we need to
2627 * determine on our own:
2629 * - Sub-VLANs of crosschip switches
2631 rc = sja1105_build_subvlans(priv, subvlan_map, new_vlan, new_retagging,
2636 rc = sja1105_build_crosschip_subvlans(priv, new_vlan, new_retagging,
2641 rc = sja1105_commit_vlans(priv, new_vlan, new_retagging, num_retagging);
2645 rc = sja1105_commit_pvid(priv);
2649 for (i = 0; i < priv->ds->num_ports; i++)
2650 sja1105_commit_subvlan_map(priv, i, subvlan_map[i]);
2653 rc = sja1105_notify_crosschip_switches(priv);
2660 kfree(new_retagging);
2665 static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
2666 const struct switchdev_obj_port_vlan *vlan)
2668 struct sja1105_private *priv = ds->priv;
2671 if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL)
2674 /* If the user wants best-effort VLAN filtering (aka vlan_filtering
2675 * bridge plus tagging), be sure to at least deny alterations to the
2676 * configuration done by dsa_8021q.
2678 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2679 if (vid_is_dsa_8021q(vid)) {
2680 dev_err(ds->dev, "Range 1024-3071 reserved for dsa_8021q operation\n");
2688 /* The TPID setting belongs to the General Parameters table,
2689 * which can only be partially reconfigured at runtime (and not the TPID).
2690 * So a switch reset is required.
2692 int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
2693 struct switchdev_trans *trans)
2695 struct sja1105_l2_lookup_params_entry *l2_lookup_params;
2696 struct sja1105_general_params_entry *general_params;
2697 struct sja1105_private *priv = ds->priv;
2698 enum sja1105_vlan_state state;
2699 struct sja1105_table *table;
2700 struct sja1105_rule *rule;
2705 if (switchdev_trans_ph_prepare(trans)) {
2706 list_for_each_entry(rule, &priv->flow_block.rules, list) {
2707 if (rule->type == SJA1105_RULE_VL) {
2709 "Cannot change VLAN filtering with active VL rules\n");
2718 /* Enable VLAN filtering. */
2720 tpid2 = ETH_P_8021AD;
2722 /* Disable VLAN filtering. */
2723 tpid = ETH_P_SJA1105;
2724 tpid2 = ETH_P_SJA1105;
2727 for (port = 0; port < ds->num_ports; port++) {
2728 struct sja1105_port *sp = &priv->ports[port];
2731 sp->xmit_tpid = priv->info->qinq_tpid;
2733 sp->xmit_tpid = ETH_P_SJA1105;
2737 state = SJA1105_VLAN_UNAWARE;
2738 else if (priv->best_effort_vlan_filtering)
2739 state = SJA1105_VLAN_BEST_EFFORT;
2741 state = SJA1105_VLAN_FILTERING_FULL;
2743 if (priv->vlan_state == state)
2746 priv->vlan_state = state;
2747 want_tagging = (state == SJA1105_VLAN_UNAWARE ||
2748 state == SJA1105_VLAN_BEST_EFFORT);
2750 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2751 general_params = table->entries;
2752 /* EtherType used to identify inner tagged (C-tag) VLAN traffic */
2753 general_params->tpid = tpid;
2754 /* EtherType used to identify outer tagged (S-tag) VLAN traffic */
2755 general_params->tpid2 = tpid2;
2756 /* When VLAN filtering is on, we need to at least be able to
2757 * decode management traffic through the "backup plan".
2759 general_params->incl_srcpt1 = enabled;
2760 general_params->incl_srcpt0 = enabled;
2762 want_tagging = priv->best_effort_vlan_filtering || !enabled;
2764 /* VLAN filtering => independent VLAN learning.
2765 * No VLAN filtering (or best effort) => shared VLAN learning.
2767 * In shared VLAN learning mode, untagged traffic still gets
2768 * pvid-tagged, and the FDB table gets populated with entries
2769 * containing the "real" (pvid or from VLAN tag) VLAN ID.
2770 * However the switch performs a masked L2 lookup in the FDB,
2771 * effectively only looking up a frame's DMAC (and not VID) for the
2772 * forwarding decision.
2774 * This is extremely convenient for us, because in modes with
2775 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
2776 * each front panel port. This is good for identification but breaks
2777 * learning badly - the VID of the learnt FDB entry is unique, aka
2778 * no frames coming from any other port are going to have it. So
2779 * for forwarding purposes, this is as though learning was broken
2780 * (all frames get flooded).
2782 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2783 l2_lookup_params = table->entries;
2784 l2_lookup_params->shared_learn = want_tagging;
2786 sja1105_frame_memory_partitioning(priv);
2788 rc = sja1105_build_vlan_table(priv, false);
2792 rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
2794 dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
2796 /* Switch port identification based on 802.1Q is only passable
2797 * if we are not under a vlan_filtering bridge. So make sure
2798 * the two configurations are mutually exclusive (of course, the
2799 * user may know better, i.e. best_effort_vlan_filtering).
2801 return sja1105_setup_8021q_tagging(ds, want_tagging);
2804 /* Returns number of VLANs added (0 or 1) on success,
2805 * or a negative error code.
2807 static int sja1105_vlan_add_one(struct dsa_switch *ds, int port, u16 vid,
2808 u16 flags, struct list_head *vlan_list)
2810 bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED;
2811 bool pvid = flags & BRIDGE_VLAN_INFO_PVID;
2812 struct sja1105_bridge_vlan *v;
2814 list_for_each_entry(v, vlan_list, list) {
2815 if (v->port == port && v->vid == vid) {
2817 if (v->untagged == untagged && v->pvid == pvid)
2818 /* Nothing changed */
2821 /* It's the same VLAN, but some of the flags changed
2822 * and the user did not bother to delete it first.
2823 * Update it and trigger sja1105_build_vlan_table.
2825 v->untagged = untagged;
2831 v = kzalloc(sizeof(*v), GFP_KERNEL);
2833 dev_err(ds->dev, "Out of memory while storing VLAN\n");
2839 v->untagged = untagged;
2841 list_add(&v->list, vlan_list);
2846 /* Returns number of VLANs deleted (0 or 1) */
2847 static int sja1105_vlan_del_one(struct dsa_switch *ds, int port, u16 vid,
2848 struct list_head *vlan_list)
2850 struct sja1105_bridge_vlan *v, *n;
2852 list_for_each_entry_safe(v, n, vlan_list, list) {
2853 if (v->port == port && v->vid == vid) {
2863 static void sja1105_vlan_add(struct dsa_switch *ds, int port,
2864 const struct switchdev_obj_port_vlan *vlan)
2866 struct sja1105_private *priv = ds->priv;
2867 bool vlan_table_changed = false;
2871 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2872 rc = sja1105_vlan_add_one(ds, port, vid, vlan->flags,
2873 &priv->bridge_vlans);
2877 vlan_table_changed = true;
2880 if (!vlan_table_changed)
2883 rc = sja1105_build_vlan_table(priv, true);
2885 dev_err(ds->dev, "Failed to build VLAN table: %d\n", rc);
2888 static int sja1105_vlan_del(struct dsa_switch *ds, int port,
2889 const struct switchdev_obj_port_vlan *vlan)
2891 struct sja1105_private *priv = ds->priv;
2892 bool vlan_table_changed = false;
2896 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
2897 rc = sja1105_vlan_del_one(ds, port, vid, &priv->bridge_vlans);
2899 vlan_table_changed = true;
2902 if (!vlan_table_changed)
2905 return sja1105_build_vlan_table(priv, true);
2908 static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
2911 struct sja1105_private *priv = ds->priv;
2914 rc = sja1105_vlan_add_one(ds, port, vid, flags, &priv->dsa_8021q_vlans);
2918 return sja1105_build_vlan_table(priv, true);
2921 static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
2923 struct sja1105_private *priv = ds->priv;
2926 rc = sja1105_vlan_del_one(ds, port, vid, &priv->dsa_8021q_vlans);
2930 return sja1105_build_vlan_table(priv, true);
2933 static const struct dsa_8021q_ops sja1105_dsa_8021q_ops = {
2934 .vlan_add = sja1105_dsa_8021q_vlan_add,
2935 .vlan_del = sja1105_dsa_8021q_vlan_del,
2938 /* The programming model for the SJA1105 switch is "all-at-once" via static
2939 * configuration tables. Some of these can be dynamically modified at runtime,
2940 * but not the xMII mode parameters table.
2941 * Furthermode, some PHYs may not have crystals for generating their clocks
2942 * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
2943 * ref_clk pin. So port clocking needs to be initialized early, before
2944 * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
2945 * Setting correct PHY link speed does not matter now.
2946 * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
2947 * bindings are not yet parsed by DSA core. We need to parse early so that we
2948 * can populate the xMII mode parameters table.
2950 static int sja1105_setup(struct dsa_switch *ds)
2952 struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
2953 struct sja1105_private *priv = ds->priv;
2956 rc = sja1105_parse_dt(priv, ports);
2958 dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
2962 /* Error out early if internal delays are required through DT
2963 * and we can't apply them.
2965 rc = sja1105_parse_rgmii_delays(priv, ports);
2967 dev_err(ds->dev, "RGMII delay not supported\n");
2971 rc = sja1105_ptp_clock_register(ds);
2973 dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
2976 /* Create and send configuration down to device */
2977 rc = sja1105_static_config_load(priv, ports);
2979 dev_err(ds->dev, "Failed to load static config: %d\n", rc);
2980 goto out_ptp_clock_unregister;
2982 /* Configure the CGU (PHY link modes and speeds) */
2983 rc = sja1105_clocking_setup(priv);
2985 dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
2986 goto out_static_config_free;
2988 /* On SJA1105, VLAN filtering per se is always enabled in hardware.
2989 * The only thing we can do to disable it is lie about what the 802.1Q
2991 * So it will still try to apply VLAN filtering, but all ingress
2992 * traffic (except frames received with EtherType of ETH_P_SJA1105)
2993 * will be internally tagged with a distorted VLAN header where the
2994 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
2996 ds->vlan_filtering_is_global = true;
2998 /* Advertise the 8 egress queues */
2999 ds->num_tx_queues = SJA1105_NUM_TC;
3001 ds->mtu_enforcement_ingress = true;
3003 ds->configure_vlan_while_not_filtering = true;
3005 rc = sja1105_devlink_setup(ds);
3007 goto out_static_config_free;
3009 /* The DSA/switchdev model brings up switch ports in standalone mode by
3010 * default, and that means vlan_filtering is 0 since they're not under
3011 * a bridge, so it's safe to set up switch tagging at this time.
3014 rc = sja1105_setup_8021q_tagging(ds, true);
3017 goto out_devlink_teardown;
3021 out_devlink_teardown:
3022 sja1105_devlink_teardown(ds);
3023 out_ptp_clock_unregister:
3024 sja1105_ptp_clock_unregister(ds);
3025 out_static_config_free:
3026 sja1105_static_config_free(&priv->static_config);
3031 static void sja1105_teardown(struct dsa_switch *ds)
3033 struct sja1105_private *priv = ds->priv;
3034 struct sja1105_bridge_vlan *v, *n;
3037 for (port = 0; port < SJA1105_NUM_PORTS; port++) {
3038 struct sja1105_port *sp = &priv->ports[port];
3040 if (!dsa_is_user_port(ds, port))
3043 if (sp->xmit_worker)
3044 kthread_destroy_worker(sp->xmit_worker);
3047 sja1105_devlink_teardown(ds);
3048 sja1105_flower_teardown(ds);
3049 sja1105_tas_teardown(ds);
3050 sja1105_ptp_clock_unregister(ds);
3051 sja1105_static_config_free(&priv->static_config);
3053 list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) {
3058 list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) {
3064 static int sja1105_port_enable(struct dsa_switch *ds, int port,
3065 struct phy_device *phy)
3067 struct net_device *slave;
3069 if (!dsa_is_user_port(ds, port))
3072 slave = dsa_to_port(ds, port)->slave;
3074 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3079 static void sja1105_port_disable(struct dsa_switch *ds, int port)
3081 struct sja1105_private *priv = ds->priv;
3082 struct sja1105_port *sp = &priv->ports[port];
3084 if (!dsa_is_user_port(ds, port))
3087 kthread_cancel_work_sync(&sp->xmit_work);
3088 skb_queue_purge(&sp->xmit_queue);
3091 static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
3092 struct sk_buff *skb, bool takets)
3094 struct sja1105_mgmt_entry mgmt_route = {0};
3095 struct sja1105_private *priv = ds->priv;
3102 mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
3103 mgmt_route.destports = BIT(port);
3104 mgmt_route.enfport = 1;
3105 mgmt_route.tsreg = 0;
3106 mgmt_route.takets = takets;
3108 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3109 slot, &mgmt_route, true);
3115 /* Transfer skb to the host port. */
3116 dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
3118 /* Wait until the switch has processed the frame */
3120 rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
3123 dev_err_ratelimited(priv->ds->dev,
3124 "failed to poll for mgmt route\n");
3128 /* UM10944: The ENFPORT flag of the respective entry is
3129 * cleared when a match is found. The host can use this
3130 * flag as an acknowledgment.
3133 } while (mgmt_route.enfport && --timeout);
3136 /* Clean up the management route so that a follow-up
3137 * frame may not match on it by mistake.
3138 * This is only hardware supported on P/Q/R/S - on E/T it is
3139 * a no-op and we are silently discarding the -EOPNOTSUPP.
3141 sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
3142 slot, &mgmt_route, false);
3143 dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
3146 return NETDEV_TX_OK;
3149 #define work_to_port(work) \
3150 container_of((work), struct sja1105_port, xmit_work)
3151 #define tagger_to_sja1105(t) \
3152 container_of((t), struct sja1105_private, tagger_data)
3154 /* Deferred work is unfortunately necessary because setting up the management
3155 * route cannot be done from atomit context (SPI transfer takes a sleepable
3158 static void sja1105_port_deferred_xmit(struct kthread_work *work)
3160 struct sja1105_port *sp = work_to_port(work);
3161 struct sja1105_tagger_data *tagger_data = sp->data;
3162 struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
3163 int port = sp - priv->ports;
3164 struct sk_buff *skb;
3166 while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
3167 struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
3169 mutex_lock(&priv->mgmt_lock);
3171 sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
3173 /* The clone, if there, was made by dsa_skb_tx_timestamp */
3175 sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
3177 mutex_unlock(&priv->mgmt_lock);
3181 /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
3182 * which cannot be reconfigured at runtime. So a switch reset is required.
3184 static int sja1105_set_ageing_time(struct dsa_switch *ds,
3185 unsigned int ageing_time)
3187 struct sja1105_l2_lookup_params_entry *l2_lookup_params;
3188 struct sja1105_private *priv = ds->priv;
3189 struct sja1105_table *table;
3190 unsigned int maxage;
3192 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
3193 l2_lookup_params = table->entries;
3195 maxage = SJA1105_AGEING_TIME_MS(ageing_time);
3197 if (l2_lookup_params->maxage == maxage)
3200 l2_lookup_params->maxage = maxage;
3202 return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
3205 static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
3207 struct sja1105_l2_policing_entry *policing;
3208 struct sja1105_private *priv = ds->priv;
3210 new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
3212 if (dsa_is_cpu_port(ds, port))
3213 new_mtu += VLAN_HLEN;
3215 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3217 if (policing[port].maxlen == new_mtu)
3220 policing[port].maxlen = new_mtu;
3222 return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3225 static int sja1105_get_max_mtu(struct dsa_switch *ds, int port)
3227 return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN;
3230 static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
3231 enum tc_setup_type type,
3235 case TC_SETUP_QDISC_TAPRIO:
3236 return sja1105_setup_tc_taprio(ds, port, type_data);
3237 case TC_SETUP_QDISC_CBS:
3238 return sja1105_setup_tc_cbs(ds, port, type_data);
3244 /* We have a single mirror (@to) port, but can configure ingress and egress
3245 * mirroring on all other (@from) ports.
3246 * We need to allow mirroring rules only as long as the @to port is always the
3247 * same, and we need to unset the @to port from mirr_port only when there is no
3248 * mirroring rule that references it.
3250 static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
3251 bool ingress, bool enabled)
3253 struct sja1105_general_params_entry *general_params;
3254 struct sja1105_mac_config_entry *mac;
3255 struct sja1105_table *table;
3256 bool already_enabled;
3260 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
3261 general_params = table->entries;
3263 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
3265 already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS);
3266 if (already_enabled && enabled && general_params->mirr_port != to) {
3267 dev_err(priv->ds->dev,
3268 "Delete mirroring rules towards port %llu first\n",
3269 general_params->mirr_port);
3278 /* Anybody still referencing mirr_port? */
3279 for (port = 0; port < SJA1105_NUM_PORTS; port++) {
3280 if (mac[port].ing_mirr || mac[port].egr_mirr) {
3285 /* Unset already_enabled for next time */
3287 new_mirr_port = SJA1105_NUM_PORTS;
3289 if (new_mirr_port != general_params->mirr_port) {
3290 general_params->mirr_port = new_mirr_port;
3292 rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
3293 0, general_params, true);
3299 mac[from].ing_mirr = enabled;
3301 mac[from].egr_mirr = enabled;
3303 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
3307 static int sja1105_mirror_add(struct dsa_switch *ds, int port,
3308 struct dsa_mall_mirror_tc_entry *mirror,
3311 return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3315 static void sja1105_mirror_del(struct dsa_switch *ds, int port,
3316 struct dsa_mall_mirror_tc_entry *mirror)
3318 sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
3319 mirror->ingress, false);
3322 static int sja1105_port_policer_add(struct dsa_switch *ds, int port,
3323 struct dsa_mall_policer_tc_entry *policer)
3325 struct sja1105_l2_policing_entry *policing;
3326 struct sja1105_private *priv = ds->priv;
3328 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3330 /* In hardware, every 8 microseconds the credit level is incremented by
3331 * the value of RATE bytes divided by 64, up to a maximum of SMAX
3334 policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec,
3336 policing[port].smax = policer->burst;
3338 return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3341 static void sja1105_port_policer_del(struct dsa_switch *ds, int port)
3343 struct sja1105_l2_policing_entry *policing;
3344 struct sja1105_private *priv = ds->priv;
3346 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
3348 policing[port].rate = SJA1105_RATE_MBPS(1000);
3349 policing[port].smax = 65535;
3351 sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
3354 static const struct dsa_switch_ops sja1105_switch_ops = {
3355 .get_tag_protocol = sja1105_get_tag_protocol,
3356 .setup = sja1105_setup,
3357 .teardown = sja1105_teardown,
3358 .set_ageing_time = sja1105_set_ageing_time,
3359 .port_change_mtu = sja1105_change_mtu,
3360 .port_max_mtu = sja1105_get_max_mtu,
3361 .phylink_validate = sja1105_phylink_validate,
3362 .phylink_mac_link_state = sja1105_mac_pcs_get_state,
3363 .phylink_mac_config = sja1105_mac_config,
3364 .phylink_mac_link_up = sja1105_mac_link_up,
3365 .phylink_mac_link_down = sja1105_mac_link_down,
3366 .get_strings = sja1105_get_strings,
3367 .get_ethtool_stats = sja1105_get_ethtool_stats,
3368 .get_sset_count = sja1105_get_sset_count,
3369 .get_ts_info = sja1105_get_ts_info,
3370 .port_enable = sja1105_port_enable,
3371 .port_disable = sja1105_port_disable,
3372 .port_fdb_dump = sja1105_fdb_dump,
3373 .port_fdb_add = sja1105_fdb_add,
3374 .port_fdb_del = sja1105_fdb_del,
3375 .port_bridge_join = sja1105_bridge_join,
3376 .port_bridge_leave = sja1105_bridge_leave,
3377 .port_stp_state_set = sja1105_bridge_stp_state_set,
3378 .port_vlan_prepare = sja1105_vlan_prepare,
3379 .port_vlan_filtering = sja1105_vlan_filtering,
3380 .port_vlan_add = sja1105_vlan_add,
3381 .port_vlan_del = sja1105_vlan_del,
3382 .port_mdb_prepare = sja1105_mdb_prepare,
3383 .port_mdb_add = sja1105_mdb_add,
3384 .port_mdb_del = sja1105_mdb_del,
3385 .port_hwtstamp_get = sja1105_hwtstamp_get,
3386 .port_hwtstamp_set = sja1105_hwtstamp_set,
3387 .port_rxtstamp = sja1105_port_rxtstamp,
3388 .port_txtstamp = sja1105_port_txtstamp,
3389 .port_setup_tc = sja1105_port_setup_tc,
3390 .port_mirror_add = sja1105_mirror_add,
3391 .port_mirror_del = sja1105_mirror_del,
3392 .port_policer_add = sja1105_port_policer_add,
3393 .port_policer_del = sja1105_port_policer_del,
3394 .cls_flower_add = sja1105_cls_flower_add,
3395 .cls_flower_del = sja1105_cls_flower_del,
3396 .cls_flower_stats = sja1105_cls_flower_stats,
3397 .crosschip_bridge_join = sja1105_crosschip_bridge_join,
3398 .crosschip_bridge_leave = sja1105_crosschip_bridge_leave,
3399 .devlink_param_get = sja1105_devlink_param_get,
3400 .devlink_param_set = sja1105_devlink_param_set,
3401 .devlink_info_get = sja1105_devlink_info_get,
3404 static const struct of_device_id sja1105_dt_ids[];
3406 static int sja1105_check_device_id(struct sja1105_private *priv)
3408 const struct sja1105_regs *regs = priv->info->regs;
3409 u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
3410 struct device *dev = &priv->spidev->dev;
3411 const struct of_device_id *match;
3416 rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
3421 rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
3422 SJA1105_SIZE_DEVICE_ID);
3426 sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
3428 for (match = sja1105_dt_ids; match->compatible[0]; match++) {
3429 const struct sja1105_info *info = match->data;
3431 /* Is what's been probed in our match table at all? */
3432 if (info->device_id != device_id || info->part_no != part_no)
3435 /* But is it what's in the device tree? */
3436 if (priv->info->device_id != device_id ||
3437 priv->info->part_no != part_no) {
3438 dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n",
3439 priv->info->name, info->name);
3440 /* It isn't. No problem, pick that up. */
3447 dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n",
3448 device_id, part_no);
3453 static int sja1105_probe(struct spi_device *spi)
3455 struct sja1105_tagger_data *tagger_data;
3456 struct device *dev = &spi->dev;
3457 struct sja1105_private *priv;
3458 struct dsa_switch *ds;
3461 if (!dev->of_node) {
3462 dev_err(dev, "No DTS bindings for SJA1105 driver\n");
3466 priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
3470 /* Configure the optional reset pin and bring up switch */
3471 priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
3472 if (IS_ERR(priv->reset_gpio))
3473 dev_dbg(dev, "reset-gpios not defined, ignoring\n");
3475 sja1105_hw_reset(priv->reset_gpio, 1, 1);
3477 /* Populate our driver private structure (priv) based on
3478 * the device tree node that was probed (spi)
3481 spi_set_drvdata(spi, priv);
3483 /* Configure the SPI bus */
3484 spi->bits_per_word = 8;
3485 rc = spi_setup(spi);
3487 dev_err(dev, "Could not init SPI\n");
3491 priv->info = of_device_get_match_data(dev);
3493 /* Detect hardware device */
3494 rc = sja1105_check_device_id(priv);
3496 dev_err(dev, "Device ID check failed: %d\n", rc);
3500 dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
3502 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3507 ds->num_ports = SJA1105_NUM_PORTS;
3508 ds->ops = &sja1105_switch_ops;
3512 tagger_data = &priv->tagger_data;
3514 mutex_init(&priv->ptp_data.lock);
3515 mutex_init(&priv->mgmt_lock);
3517 priv->dsa_8021q_ctx = devm_kzalloc(dev, sizeof(*priv->dsa_8021q_ctx),
3519 if (!priv->dsa_8021q_ctx)
3522 priv->dsa_8021q_ctx->ops = &sja1105_dsa_8021q_ops;
3523 priv->dsa_8021q_ctx->proto = htons(ETH_P_8021Q);
3524 priv->dsa_8021q_ctx->ds = ds;
3526 INIT_LIST_HEAD(&priv->dsa_8021q_ctx->crosschip_links);
3527 INIT_LIST_HEAD(&priv->bridge_vlans);
3528 INIT_LIST_HEAD(&priv->dsa_8021q_vlans);
3530 sja1105_tas_setup(ds);
3531 sja1105_flower_setup(ds);
3533 rc = dsa_register_switch(priv->ds);
3537 if (IS_ENABLED(CONFIG_NET_SCH_CBS)) {
3538 priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers,
3539 sizeof(struct sja1105_cbs_entry),
3543 goto out_unregister_switch;
3547 /* Connections between dsa_port and sja1105_port */
3548 for (port = 0; port < SJA1105_NUM_PORTS; port++) {
3549 struct sja1105_port *sp = &priv->ports[port];
3550 struct dsa_port *dp = dsa_to_port(ds, port);
3551 struct net_device *slave;
3554 if (!dsa_is_user_port(ds, port))
3559 sp->data = tagger_data;
3561 kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
3562 sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
3564 if (IS_ERR(sp->xmit_worker)) {
3565 rc = PTR_ERR(sp->xmit_worker);
3567 "failed to create deferred xmit thread: %d\n",
3569 goto out_destroy_workers;
3571 skb_queue_head_init(&sp->xmit_queue);
3572 sp->xmit_tpid = ETH_P_SJA1105;
3574 for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++)
3575 sp->subvlan_map[subvlan] = VLAN_N_VID;
3580 out_destroy_workers:
3581 while (port-- > 0) {
3582 struct sja1105_port *sp = &priv->ports[port];
3584 if (!dsa_is_user_port(ds, port))
3587 kthread_destroy_worker(sp->xmit_worker);
3590 out_unregister_switch:
3591 dsa_unregister_switch(ds);
3596 static int sja1105_remove(struct spi_device *spi)
3598 struct sja1105_private *priv = spi_get_drvdata(spi);
3600 dsa_unregister_switch(priv->ds);
3604 static const struct of_device_id sja1105_dt_ids[] = {
3605 { .compatible = "nxp,sja1105e", .data = &sja1105e_info },
3606 { .compatible = "nxp,sja1105t", .data = &sja1105t_info },
3607 { .compatible = "nxp,sja1105p", .data = &sja1105p_info },
3608 { .compatible = "nxp,sja1105q", .data = &sja1105q_info },
3609 { .compatible = "nxp,sja1105r", .data = &sja1105r_info },
3610 { .compatible = "nxp,sja1105s", .data = &sja1105s_info },
3613 MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
3615 static struct spi_driver sja1105_driver = {
3618 .owner = THIS_MODULE,
3619 .of_match_table = of_match_ptr(sja1105_dt_ids),
3621 .probe = sja1105_probe,
3622 .remove = sja1105_remove,
3625 module_spi_driver(sja1105_driver);
3627 MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
3628 MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
3629 MODULE_DESCRIPTION("SJA1105 Driver");
3630 MODULE_LICENSE("GPL v2");