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_tas.h"
27 static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
28 unsigned int startup_delay)
30 gpiod_set_value_cansleep(gpio, 1);
31 /* Wait for minimum reset pulse length */
33 gpiod_set_value_cansleep(gpio, 0);
34 /* Wait until chip is ready after reset */
35 msleep(startup_delay);
39 sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
40 int from, int to, bool allow)
43 l2_fwd[from].bc_domain |= BIT(to);
44 l2_fwd[from].reach_port |= BIT(to);
45 l2_fwd[from].fl_domain |= BIT(to);
47 l2_fwd[from].bc_domain &= ~BIT(to);
48 l2_fwd[from].reach_port &= ~BIT(to);
49 l2_fwd[from].fl_domain &= ~BIT(to);
53 /* Structure used to temporarily transport device tree
54 * settings into sja1105_setup
56 struct sja1105_dt_port {
57 phy_interface_t phy_mode;
58 sja1105_mii_role_t role;
61 static int sja1105_init_mac_settings(struct sja1105_private *priv)
63 struct sja1105_mac_config_entry default_mac = {
64 /* Enable all 8 priority queues on egress.
65 * Every queue i holds top[i] - base[i] frames.
66 * Sum of top[i] - base[i] is 511 (max hardware limit).
68 .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
69 .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
70 .enabled = {true, true, true, true, true, true, true, true},
71 /* Keep standard IFG of 12 bytes on egress. */
73 /* Always put the MAC speed in automatic mode, where it can be
74 * adjusted at runtime by PHYLINK.
76 .speed = SJA1105_SPEED_AUTO,
77 /* No static correction for 1-step 1588 events */
80 /* Disable aging for critical TTEthernet traffic */
82 /* Internal VLAN (pvid) to apply to untagged ingress */
87 /* Don't drop traffic with other EtherType than ETH_P_IP */
89 /* Don't drop double-tagged traffic */
91 /* Don't drop untagged traffic */
93 /* Don't retag 802.1p (VID 0) traffic with the pvid */
95 /* Disable learning and I/O on user ports by default -
102 struct sja1105_mac_config_entry *mac;
103 struct sja1105_table *table;
106 table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
108 /* Discard previous MAC Configuration Table */
109 if (table->entry_count) {
110 kfree(table->entries);
111 table->entry_count = 0;
114 table->entries = kcalloc(SJA1105_NUM_PORTS,
115 table->ops->unpacked_entry_size, GFP_KERNEL);
119 table->entry_count = SJA1105_NUM_PORTS;
121 mac = table->entries;
123 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
124 mac[i] = default_mac;
125 if (i == dsa_upstream_port(priv->ds, i)) {
126 /* STP doesn't get called for CPU port, so we need to
127 * set the I/O parameters statically.
129 mac[i].dyn_learn = true;
130 mac[i].ingress = true;
131 mac[i].egress = true;
138 static int sja1105_init_mii_settings(struct sja1105_private *priv,
139 struct sja1105_dt_port *ports)
141 struct device *dev = &priv->spidev->dev;
142 struct sja1105_xmii_params_entry *mii;
143 struct sja1105_table *table;
146 table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
148 /* Discard previous xMII Mode Parameters Table */
149 if (table->entry_count) {
150 kfree(table->entries);
151 table->entry_count = 0;
154 table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
155 table->ops->unpacked_entry_size, GFP_KERNEL);
159 /* Override table based on PHYLINK DT bindings */
160 table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
162 mii = table->entries;
164 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
165 switch (ports[i].phy_mode) {
166 case PHY_INTERFACE_MODE_MII:
167 mii->xmii_mode[i] = XMII_MODE_MII;
169 case PHY_INTERFACE_MODE_RMII:
170 mii->xmii_mode[i] = XMII_MODE_RMII;
172 case PHY_INTERFACE_MODE_RGMII:
173 case PHY_INTERFACE_MODE_RGMII_ID:
174 case PHY_INTERFACE_MODE_RGMII_RXID:
175 case PHY_INTERFACE_MODE_RGMII_TXID:
176 mii->xmii_mode[i] = XMII_MODE_RGMII;
179 dev_err(dev, "Unsupported PHY mode %s!\n",
180 phy_modes(ports[i].phy_mode));
183 mii->phy_mac[i] = ports[i].role;
188 static int sja1105_init_static_fdb(struct sja1105_private *priv)
190 struct sja1105_table *table;
192 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
194 /* We only populate the FDB table through dynamic
195 * L2 Address Lookup entries
197 if (table->entry_count) {
198 kfree(table->entries);
199 table->entry_count = 0;
204 static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
206 struct sja1105_table *table;
207 u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
208 struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
209 /* Learned FDB entries are forgotten after 300 seconds */
210 .maxage = SJA1105_AGEING_TIME_MS(300000),
211 /* All entries within a FDB bin are available for learning */
212 .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
213 /* And the P/Q/R/S equivalent setting: */
215 .maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
216 max_fdb_entries, max_fdb_entries, },
217 /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
219 /* This selects between Independent VLAN Learning (IVL) and
220 * Shared VLAN Learning (SVL)
222 .shared_learn = true,
223 /* Don't discard management traffic based on ENFPORT -
224 * we don't perform SMAC port enforcement anyway, so
225 * what we are setting here doesn't matter.
227 .no_enf_hostprt = false,
228 /* Don't learn SMAC for mac_fltres1 and mac_fltres0.
229 * Maybe correlate with no_linklocal_learn from bridge driver?
231 .no_mgmt_learn = true,
234 /* Dynamically learned FDB entries can overwrite other (older)
235 * dynamic FDB entries
241 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
243 if (table->entry_count) {
244 kfree(table->entries);
245 table->entry_count = 0;
248 table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
249 table->ops->unpacked_entry_size, GFP_KERNEL);
253 table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
255 /* This table only has a single entry */
256 ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
257 default_l2_lookup_params;
262 static int sja1105_init_static_vlan(struct sja1105_private *priv)
264 struct sja1105_table *table;
265 struct sja1105_vlan_lookup_entry pvid = {
275 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
277 /* The static VLAN table will only contain the initial pvid of 1.
278 * All other VLANs are to be configured through dynamic entries,
279 * and kept in the static configuration table as backing memory.
281 if (table->entry_count) {
282 kfree(table->entries);
283 table->entry_count = 0;
286 table->entries = kcalloc(1, table->ops->unpacked_entry_size,
291 table->entry_count = 1;
293 /* VLAN 1: all DT-defined ports are members; no restrictions on
294 * forwarding; always transmit priority-tagged frames as untagged.
296 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
297 pvid.vmemb_port |= BIT(i);
298 pvid.vlan_bc |= BIT(i);
299 pvid.tag_port &= ~BIT(i);
302 ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
306 static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
308 struct sja1105_l2_forwarding_entry *l2fwd;
309 struct sja1105_table *table;
312 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
314 if (table->entry_count) {
315 kfree(table->entries);
316 table->entry_count = 0;
319 table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
320 table->ops->unpacked_entry_size, GFP_KERNEL);
324 table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
326 l2fwd = table->entries;
328 /* First 5 entries define the forwarding rules */
329 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
330 unsigned int upstream = dsa_upstream_port(priv->ds, i);
332 for (j = 0; j < SJA1105_NUM_TC; j++)
333 l2fwd[i].vlan_pmap[j] = j;
338 sja1105_port_allow_traffic(l2fwd, i, upstream, true);
339 sja1105_port_allow_traffic(l2fwd, upstream, i, true);
341 /* Next 8 entries define VLAN PCP mapping from ingress to egress.
342 * Create a one-to-one mapping.
344 for (i = 0; i < SJA1105_NUM_TC; i++)
345 for (j = 0; j < SJA1105_NUM_PORTS; j++)
346 l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
351 static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
353 struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
354 /* Disallow dynamic reconfiguration of vlan_pmap */
356 /* Use a single memory partition for all ingress queues */
357 .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
359 struct sja1105_table *table;
361 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
363 if (table->entry_count) {
364 kfree(table->entries);
365 table->entry_count = 0;
368 table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
369 table->ops->unpacked_entry_size, GFP_KERNEL);
373 table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
375 /* This table only has a single entry */
376 ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
377 default_l2fwd_params;
382 static int sja1105_init_general_params(struct sja1105_private *priv)
384 struct sja1105_general_params_entry default_general_params = {
385 /* Allow dynamic changing of the mirror port */
387 .switchid = priv->ds->index,
388 /* Priority queue for link-local management frames
389 * (both ingress to and egress from CPU - PTP, STP etc)
392 .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
393 .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK,
394 .incl_srcpt1 = false,
396 .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
397 .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK,
398 .incl_srcpt0 = false,
400 /* The destination for traffic matching mac_fltres1 and
401 * mac_fltres0 on all ports except host_port. Such traffic
402 * receieved on host_port itself would be dropped, except
403 * by installing a temporary 'management route'
405 .host_port = dsa_upstream_port(priv->ds, 0),
406 /* Default to an invalid value */
407 .mirr_port = SJA1105_NUM_PORTS,
408 /* Link-local traffic received on casc_port will be forwarded
409 * to host_port without embedding the source port and device ID
410 * info in the destination MAC address (presumably because it
411 * is a cascaded port and a downstream SJA switch already did
412 * that). Default to an invalid port (to disable the feature)
413 * and overwrite this if we find any DSA (cascaded) ports.
415 .casc_port = SJA1105_NUM_PORTS,
420 /* Only update correctionField for 1-step PTP (L2 transport) */
422 /* Forcefully disable VLAN filtering by telling
423 * the switch that VLAN has a different EtherType.
425 .tpid = ETH_P_SJA1105,
426 .tpid2 = ETH_P_SJA1105,
428 struct sja1105_table *table;
430 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
432 if (table->entry_count) {
433 kfree(table->entries);
434 table->entry_count = 0;
437 table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
438 table->ops->unpacked_entry_size, GFP_KERNEL);
442 table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
444 /* This table only has a single entry */
445 ((struct sja1105_general_params_entry *)table->entries)[0] =
446 default_general_params;
451 #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
453 static void sja1105_setup_policer(struct sja1105_l2_policing_entry *policing,
456 policing[index].sharindx = index;
457 policing[index].smax = 65535; /* Burst size in bytes */
458 policing[index].rate = SJA1105_RATE_MBPS(1000);
459 policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
460 policing[index].partition = 0;
463 static int sja1105_init_l2_policing(struct sja1105_private *priv)
465 struct sja1105_l2_policing_entry *policing;
466 struct sja1105_table *table;
469 table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
471 /* Discard previous L2 Policing Table */
472 if (table->entry_count) {
473 kfree(table->entries);
474 table->entry_count = 0;
477 table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
478 table->ops->unpacked_entry_size, GFP_KERNEL);
482 table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
484 policing = table->entries;
486 /* k sweeps through all unicast policers (0-39).
487 * bcast sweeps through policers 40-44.
489 for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) {
490 int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i;
492 for (j = 0; j < SJA1105_NUM_TC; j++, k++)
493 sja1105_setup_policer(policing, k);
495 /* Set up this port's policer for broadcast traffic */
496 sja1105_setup_policer(policing, bcast);
501 static int sja1105_static_config_load(struct sja1105_private *priv,
502 struct sja1105_dt_port *ports)
506 sja1105_static_config_free(&priv->static_config);
507 rc = sja1105_static_config_init(&priv->static_config,
508 priv->info->static_ops,
509 priv->info->device_id);
513 /* Build static configuration */
514 rc = sja1105_init_mac_settings(priv);
517 rc = sja1105_init_mii_settings(priv, ports);
520 rc = sja1105_init_static_fdb(priv);
523 rc = sja1105_init_static_vlan(priv);
526 rc = sja1105_init_l2_lookup_params(priv);
529 rc = sja1105_init_l2_forwarding(priv);
532 rc = sja1105_init_l2_forwarding_params(priv);
535 rc = sja1105_init_l2_policing(priv);
538 rc = sja1105_init_general_params(priv);
542 /* Send initial configuration to hardware via SPI */
543 return sja1105_static_config_upload(priv);
546 static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
547 const struct sja1105_dt_port *ports)
551 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
552 if (ports[i].role == XMII_MAC)
555 if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
556 ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
557 priv->rgmii_rx_delay[i] = true;
559 if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
560 ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
561 priv->rgmii_tx_delay[i] = true;
563 if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
564 !priv->info->setup_rgmii_delay)
570 static int sja1105_parse_ports_node(struct sja1105_private *priv,
571 struct sja1105_dt_port *ports,
572 struct device_node *ports_node)
574 struct device *dev = &priv->spidev->dev;
575 struct device_node *child;
577 for_each_available_child_of_node(ports_node, child) {
578 struct device_node *phy_node;
579 phy_interface_t phy_mode;
583 /* Get switch port number from DT */
584 if (of_property_read_u32(child, "reg", &index) < 0) {
585 dev_err(dev, "Port number not defined in device tree "
586 "(property \"reg\")\n");
591 /* Get PHY mode from DT */
592 err = of_get_phy_mode(child, &phy_mode);
594 dev_err(dev, "Failed to read phy-mode or "
595 "phy-interface-type property for port %d\n",
600 ports[index].phy_mode = phy_mode;
602 phy_node = of_parse_phandle(child, "phy-handle", 0);
604 if (!of_phy_is_fixed_link(child)) {
605 dev_err(dev, "phy-handle or fixed-link "
606 "properties missing!\n");
610 /* phy-handle is missing, but fixed-link isn't.
611 * So it's a fixed link. Default to PHY role.
613 ports[index].role = XMII_PHY;
615 /* phy-handle present => put port in MAC role */
616 ports[index].role = XMII_MAC;
617 of_node_put(phy_node);
620 /* The MAC/PHY role can be overridden with explicit bindings */
621 if (of_property_read_bool(child, "sja1105,role-mac"))
622 ports[index].role = XMII_MAC;
623 else if (of_property_read_bool(child, "sja1105,role-phy"))
624 ports[index].role = XMII_PHY;
630 static int sja1105_parse_dt(struct sja1105_private *priv,
631 struct sja1105_dt_port *ports)
633 struct device *dev = &priv->spidev->dev;
634 struct device_node *switch_node = dev->of_node;
635 struct device_node *ports_node;
638 ports_node = of_get_child_by_name(switch_node, "ports");
640 dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
644 rc = sja1105_parse_ports_node(priv, ports, ports_node);
645 of_node_put(ports_node);
650 /* Convert link speed from SJA1105 to ethtool encoding */
651 static int sja1105_speed[] = {
652 [SJA1105_SPEED_AUTO] = SPEED_UNKNOWN,
653 [SJA1105_SPEED_10MBPS] = SPEED_10,
654 [SJA1105_SPEED_100MBPS] = SPEED_100,
655 [SJA1105_SPEED_1000MBPS] = SPEED_1000,
658 /* Set link speed in the MAC configuration for a specific port. */
659 static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
662 struct sja1105_xmii_params_entry *mii;
663 struct sja1105_mac_config_entry *mac;
664 struct device *dev = priv->ds->dev;
665 sja1105_phy_interface_t phy_mode;
666 sja1105_speed_t speed;
669 /* On P/Q/R/S, one can read from the device via the MAC reconfiguration
670 * tables. On E/T, MAC reconfig tables are not readable, only writable.
671 * We have to *know* what the MAC looks like. For the sake of keeping
672 * the code common, we'll use the static configuration tables as a
673 * reasonable approximation for both E/T and P/Q/R/S.
675 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
676 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
678 switch (speed_mbps) {
680 /* PHYLINK called sja1105_mac_config() to inform us about
681 * the state->interface, but AN has not completed and the
682 * speed is not yet valid. UM10944.pdf says that setting
683 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
684 * ok for power consumption in case AN will never complete -
685 * otherwise PHYLINK should come back with a new update.
687 speed = SJA1105_SPEED_AUTO;
690 speed = SJA1105_SPEED_10MBPS;
693 speed = SJA1105_SPEED_100MBPS;
696 speed = SJA1105_SPEED_1000MBPS;
699 dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
703 /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
704 * table, since this will be used for the clocking setup, and we no
705 * longer need to store it in the static config (already told hardware
706 * we want auto during upload phase).
708 mac[port].speed = speed;
710 /* Write to the dynamic reconfiguration tables */
711 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
714 dev_err(dev, "Failed to write MAC config: %d\n", rc);
718 /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
719 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
720 * RMII no change of the clock setup is required. Actually, changing
721 * the clock setup does interrupt the clock signal for a certain time
722 * which causes trouble for all PHYs relying on this signal.
724 phy_mode = mii->xmii_mode[port];
725 if (phy_mode != XMII_MODE_RGMII)
728 return sja1105_clocking_setup_port(priv, port);
731 /* The SJA1105 MAC programming model is through the static config (the xMII
732 * Mode table cannot be dynamically reconfigured), and we have to program
733 * that early (earlier than PHYLINK calls us, anyway).
734 * So just error out in case the connected PHY attempts to change the initial
735 * system interface MII protocol from what is defined in the DT, at least for
738 static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
739 phy_interface_t interface)
741 struct sja1105_xmii_params_entry *mii;
742 sja1105_phy_interface_t phy_mode;
744 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
745 phy_mode = mii->xmii_mode[port];
748 case PHY_INTERFACE_MODE_MII:
749 return (phy_mode != XMII_MODE_MII);
750 case PHY_INTERFACE_MODE_RMII:
751 return (phy_mode != XMII_MODE_RMII);
752 case PHY_INTERFACE_MODE_RGMII:
753 case PHY_INTERFACE_MODE_RGMII_ID:
754 case PHY_INTERFACE_MODE_RGMII_RXID:
755 case PHY_INTERFACE_MODE_RGMII_TXID:
756 return (phy_mode != XMII_MODE_RGMII);
762 static void sja1105_mac_config(struct dsa_switch *ds, int port,
763 unsigned int link_an_mode,
764 const struct phylink_link_state *state)
766 struct sja1105_private *priv = ds->priv;
768 if (sja1105_phy_mode_mismatch(priv, port, state->interface))
771 if (link_an_mode == MLO_AN_INBAND) {
772 dev_err(ds->dev, "In-band AN not supported!\n");
776 sja1105_adjust_port_config(priv, port, state->speed);
779 static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
781 phy_interface_t interface)
783 sja1105_inhibit_tx(ds->priv, BIT(port), true);
786 static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
788 phy_interface_t interface,
789 struct phy_device *phydev,
790 int speed, int duplex,
791 bool tx_pause, bool rx_pause)
793 sja1105_inhibit_tx(ds->priv, BIT(port), false);
796 static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
797 unsigned long *supported,
798 struct phylink_link_state *state)
800 /* Construct a new mask which exhaustively contains all link features
801 * supported by the MAC, and then apply that (logical AND) to what will
802 * be sent to the PHY for "marketing".
804 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
805 struct sja1105_private *priv = ds->priv;
806 struct sja1105_xmii_params_entry *mii;
808 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
810 /* include/linux/phylink.h says:
811 * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
812 * expects the MAC driver to return all supported link modes.
814 if (state->interface != PHY_INTERFACE_MODE_NA &&
815 sja1105_phy_mode_mismatch(priv, port, state->interface)) {
816 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
820 /* The MAC does not support pause frames, and also doesn't
821 * support half-duplex traffic modes.
823 phylink_set(mask, Autoneg);
824 phylink_set(mask, MII);
825 phylink_set(mask, 10baseT_Full);
826 phylink_set(mask, 100baseT_Full);
827 if (mii->xmii_mode[port] == XMII_MODE_RGMII)
828 phylink_set(mask, 1000baseT_Full);
830 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
831 bitmap_and(state->advertising, state->advertising, mask,
832 __ETHTOOL_LINK_MODE_MASK_NBITS);
836 sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
837 const struct sja1105_l2_lookup_entry *requested)
839 struct sja1105_l2_lookup_entry *l2_lookup;
840 struct sja1105_table *table;
843 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
844 l2_lookup = table->entries;
846 for (i = 0; i < table->entry_count; i++)
847 if (l2_lookup[i].macaddr == requested->macaddr &&
848 l2_lookup[i].vlanid == requested->vlanid &&
849 l2_lookup[i].destports & BIT(port))
855 /* We want FDB entries added statically through the bridge command to persist
856 * across switch resets, which are a common thing during normal SJA1105
857 * operation. So we have to back them up in the static configuration tables
858 * and hence apply them on next static config upload... yay!
861 sja1105_static_fdb_change(struct sja1105_private *priv, int port,
862 const struct sja1105_l2_lookup_entry *requested,
865 struct sja1105_l2_lookup_entry *l2_lookup;
866 struct sja1105_table *table;
869 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
871 match = sja1105_find_static_fdb_entry(priv, port, requested);
873 /* Can't delete a missing entry. */
877 /* No match => new entry */
878 rc = sja1105_table_resize(table, table->entry_count + 1);
882 match = table->entry_count - 1;
885 /* Assign pointer after the resize (it may be new memory) */
886 l2_lookup = table->entries;
889 * If the job was to add this FDB entry, it's already done (mostly
890 * anyway, since the port forwarding mask may have changed, case in
891 * which we update it).
892 * Otherwise we have to delete it.
895 l2_lookup[match] = *requested;
899 /* To remove, the strategy is to overwrite the element with
900 * the last one, and then reduce the array size by 1
902 l2_lookup[match] = l2_lookup[table->entry_count - 1];
903 return sja1105_table_resize(table, table->entry_count - 1);
906 /* First-generation switches have a 4-way set associative TCAM that
907 * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
908 * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
909 * For the placement of a newly learnt FDB entry, the switch selects the bin
910 * based on a hash function, and the way within that bin incrementally.
912 static int sja1105et_fdb_index(int bin, int way)
914 return bin * SJA1105ET_FDB_BIN_SIZE + way;
917 static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
918 const u8 *addr, u16 vid,
919 struct sja1105_l2_lookup_entry *match,
924 for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
925 struct sja1105_l2_lookup_entry l2_lookup = {0};
926 int index = sja1105et_fdb_index(bin, way);
928 /* Skip unused entries, optionally marking them
929 * into the return value
931 if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
932 index, &l2_lookup)) {
938 if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
939 l2_lookup.vlanid == vid) {
945 /* Return an invalid entry index if not found */
949 int sja1105et_fdb_add(struct dsa_switch *ds, int port,
950 const unsigned char *addr, u16 vid)
952 struct sja1105_l2_lookup_entry l2_lookup = {0};
953 struct sja1105_private *priv = ds->priv;
954 struct device *dev = ds->dev;
955 int last_unused = -1;
958 bin = sja1105et_fdb_hash(priv, addr, vid);
960 way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
961 &l2_lookup, &last_unused);
963 /* We have an FDB entry. Is our port in the destination
964 * mask? If yes, we need to do nothing. If not, we need
965 * to rewrite the entry by adding this port to it.
967 if (l2_lookup.destports & BIT(port))
969 l2_lookup.destports |= BIT(port);
971 int index = sja1105et_fdb_index(bin, way);
973 /* We don't have an FDB entry. We construct a new one and
974 * try to find a place for it within the FDB table.
976 l2_lookup.macaddr = ether_addr_to_u64(addr);
977 l2_lookup.destports = BIT(port);
978 l2_lookup.vlanid = vid;
980 if (last_unused >= 0) {
983 /* Bin is full, need to evict somebody.
984 * Choose victim at random. If you get these messages
985 * often, you may need to consider changing the
986 * distribution function:
987 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
989 get_random_bytes(&way, sizeof(u8));
990 way %= SJA1105ET_FDB_BIN_SIZE;
991 dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
994 sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
998 l2_lookup.index = sja1105et_fdb_index(bin, way);
1000 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1001 l2_lookup.index, &l2_lookup,
1006 return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1009 int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1010 const unsigned char *addr, u16 vid)
1012 struct sja1105_l2_lookup_entry l2_lookup = {0};
1013 struct sja1105_private *priv = ds->priv;
1014 int index, bin, way, rc;
1017 bin = sja1105et_fdb_hash(priv, addr, vid);
1018 way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1022 index = sja1105et_fdb_index(bin, way);
1024 /* We have an FDB entry. Is our port in the destination mask? If yes,
1025 * we need to remove it. If the resulting port mask becomes empty, we
1026 * need to completely evict the FDB entry.
1027 * Otherwise we just write it back.
1029 l2_lookup.destports &= ~BIT(port);
1031 if (l2_lookup.destports)
1036 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1037 index, &l2_lookup, keep);
1041 return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1044 int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
1045 const unsigned char *addr, u16 vid)
1047 struct sja1105_l2_lookup_entry l2_lookup = {0};
1048 struct sja1105_private *priv = ds->priv;
1051 /* Search for an existing entry in the FDB table */
1052 l2_lookup.macaddr = ether_addr_to_u64(addr);
1053 l2_lookup.vlanid = vid;
1054 l2_lookup.iotag = SJA1105_S_TAG;
1055 l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
1056 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) {
1057 l2_lookup.mask_vlanid = VLAN_VID_MASK;
1058 l2_lookup.mask_iotag = BIT(0);
1060 l2_lookup.mask_vlanid = 0;
1061 l2_lookup.mask_iotag = 0;
1063 l2_lookup.destports = BIT(port);
1065 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1066 SJA1105_SEARCH, &l2_lookup);
1068 /* Found and this port is already in the entry's
1069 * port mask => job done
1071 if (l2_lookup.destports & BIT(port))
1073 /* l2_lookup.index is populated by the switch in case it
1076 l2_lookup.destports |= BIT(port);
1077 goto skip_finding_an_index;
1080 /* Not found, so try to find an unused spot in the FDB.
1081 * This is slightly inefficient because the strategy is knock-knock at
1082 * every possible position from 0 to 1023.
1084 for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1085 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1090 if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
1091 dev_err(ds->dev, "FDB is full, cannot add entry.\n");
1094 l2_lookup.lockeds = true;
1095 l2_lookup.index = i;
1097 skip_finding_an_index:
1098 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1099 l2_lookup.index, &l2_lookup,
1104 return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1107 int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
1108 const unsigned char *addr, u16 vid)
1110 struct sja1105_l2_lookup_entry l2_lookup = {0};
1111 struct sja1105_private *priv = ds->priv;
1115 l2_lookup.macaddr = ether_addr_to_u64(addr);
1116 l2_lookup.vlanid = vid;
1117 l2_lookup.iotag = SJA1105_S_TAG;
1118 l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
1119 if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) {
1120 l2_lookup.mask_vlanid = VLAN_VID_MASK;
1121 l2_lookup.mask_iotag = BIT(0);
1123 l2_lookup.mask_vlanid = 0;
1124 l2_lookup.mask_iotag = 0;
1126 l2_lookup.destports = BIT(port);
1128 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1129 SJA1105_SEARCH, &l2_lookup);
1133 l2_lookup.destports &= ~BIT(port);
1135 /* Decide whether we remove just this port from the FDB entry,
1136 * or if we remove it completely.
1138 if (l2_lookup.destports)
1143 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1144 l2_lookup.index, &l2_lookup, keep);
1148 return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1151 static int sja1105_fdb_add(struct dsa_switch *ds, int port,
1152 const unsigned char *addr, u16 vid)
1154 struct sja1105_private *priv = ds->priv;
1156 /* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
1157 * so the switch still does some VLAN processing internally.
1158 * But Shared VLAN Learning (SVL) is also active, and it will take
1159 * care of autonomous forwarding between the unique pvid's of each
1160 * port. Here we just make sure that users can't add duplicate FDB
1161 * entries when in this mode - the actual VID doesn't matter except
1162 * for what gets printed in 'bridge fdb show'. In the case of zero,
1163 * no VID gets printed at all.
1165 if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1168 return priv->info->fdb_add_cmd(ds, port, addr, vid);
1171 static int sja1105_fdb_del(struct dsa_switch *ds, int port,
1172 const unsigned char *addr, u16 vid)
1174 struct sja1105_private *priv = ds->priv;
1176 if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1179 return priv->info->fdb_del_cmd(ds, port, addr, vid);
1182 static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1183 dsa_fdb_dump_cb_t *cb, void *data)
1185 struct sja1105_private *priv = ds->priv;
1186 struct device *dev = ds->dev;
1189 for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1190 struct sja1105_l2_lookup_entry l2_lookup = {0};
1191 u8 macaddr[ETH_ALEN];
1194 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1196 /* No fdb entry at i, not an issue */
1200 dev_err(dev, "Failed to dump FDB: %d\n", rc);
1204 /* FDB dump callback is per port. This means we have to
1205 * disregard a valid entry if it's not for this port, even if
1206 * only to revisit it later. This is inefficient because the
1207 * 1024-sized FDB table needs to be traversed 4 times through
1208 * SPI during a 'bridge fdb show' command.
1210 if (!(l2_lookup.destports & BIT(port)))
1212 u64_to_ether_addr(l2_lookup.macaddr, macaddr);
1214 /* We need to hide the dsa_8021q VLANs from the user. */
1215 if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
1216 l2_lookup.vlanid = 0;
1217 cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1222 /* This callback needs to be present */
1223 static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1224 const struct switchdev_obj_port_mdb *mdb)
1229 static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1230 const struct switchdev_obj_port_mdb *mdb)
1232 sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1235 static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1236 const struct switchdev_obj_port_mdb *mdb)
1238 return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1241 static int sja1105_bridge_member(struct dsa_switch *ds, int port,
1242 struct net_device *br, bool member)
1244 struct sja1105_l2_forwarding_entry *l2_fwd;
1245 struct sja1105_private *priv = ds->priv;
1248 l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
1250 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1251 /* Add this port to the forwarding matrix of the
1252 * other ports in the same bridge, and viceversa.
1254 if (!dsa_is_user_port(ds, i))
1256 /* For the ports already under the bridge, only one thing needs
1257 * to be done, and that is to add this port to their
1258 * reachability domain. So we can perform the SPI write for
1259 * them immediately. However, for this port itself (the one
1260 * that is new to the bridge), we need to add all other ports
1261 * to its reachability domain. So we do that incrementally in
1262 * this loop, and perform the SPI write only at the end, once
1263 * the domain contains all other bridge ports.
1267 if (dsa_to_port(ds, i)->bridge_dev != br)
1269 sja1105_port_allow_traffic(l2_fwd, i, port, member);
1270 sja1105_port_allow_traffic(l2_fwd, port, i, member);
1272 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
1273 i, &l2_fwd[i], true);
1278 return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
1279 port, &l2_fwd[port], true);
1282 static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1285 struct sja1105_private *priv = ds->priv;
1286 struct sja1105_mac_config_entry *mac;
1288 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1291 case BR_STATE_DISABLED:
1292 case BR_STATE_BLOCKING:
1293 /* From UM10944 description of DRPDTAG (why put this there?):
1294 * "Management traffic flows to the port regardless of the state
1295 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1296 * At the moment no difference between DISABLED and BLOCKING.
1298 mac[port].ingress = false;
1299 mac[port].egress = false;
1300 mac[port].dyn_learn = false;
1302 case BR_STATE_LISTENING:
1303 mac[port].ingress = true;
1304 mac[port].egress = false;
1305 mac[port].dyn_learn = false;
1307 case BR_STATE_LEARNING:
1308 mac[port].ingress = true;
1309 mac[port].egress = false;
1310 mac[port].dyn_learn = true;
1312 case BR_STATE_FORWARDING:
1313 mac[port].ingress = true;
1314 mac[port].egress = true;
1315 mac[port].dyn_learn = true;
1318 dev_err(ds->dev, "invalid STP state: %d\n", state);
1322 sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1326 static int sja1105_bridge_join(struct dsa_switch *ds, int port,
1327 struct net_device *br)
1329 return sja1105_bridge_member(ds, port, br, true);
1332 static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
1333 struct net_device *br)
1335 sja1105_bridge_member(ds, port, br, false);
1338 static const char * const sja1105_reset_reasons[] = {
1339 [SJA1105_VLAN_FILTERING] = "VLAN filtering",
1340 [SJA1105_RX_HWTSTAMPING] = "RX timestamping",
1341 [SJA1105_AGEING_TIME] = "Ageing time",
1342 [SJA1105_SCHEDULING] = "Time-aware scheduling",
1345 /* For situations where we need to change a setting at runtime that is only
1346 * available through the static configuration, resetting the switch in order
1347 * to upload the new static config is unavoidable. Back up the settings we
1348 * modify at runtime (currently only MAC) and restore them after uploading,
1349 * such that this operation is relatively seamless.
1351 int sja1105_static_config_reload(struct sja1105_private *priv,
1352 enum sja1105_reset_reason reason)
1354 struct ptp_system_timestamp ptp_sts_before;
1355 struct ptp_system_timestamp ptp_sts_after;
1356 struct sja1105_mac_config_entry *mac;
1357 int speed_mbps[SJA1105_NUM_PORTS];
1358 struct dsa_switch *ds = priv->ds;
1364 mutex_lock(&priv->mgmt_lock);
1366 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1368 /* Back up the dynamic link speed changed by sja1105_adjust_port_config
1369 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
1370 * switch wants to see in the static config in order to allow us to
1371 * change it through the dynamic interface later.
1373 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1374 speed_mbps[i] = sja1105_speed[mac[i].speed];
1375 mac[i].speed = SJA1105_SPEED_AUTO;
1378 /* No PTP operations can run right now */
1379 mutex_lock(&priv->ptp_data.lock);
1381 rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
1383 goto out_unlock_ptp;
1385 /* Reset switch and send updated static configuration */
1386 rc = sja1105_static_config_upload(priv);
1388 goto out_unlock_ptp;
1390 rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
1392 goto out_unlock_ptp;
1394 t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
1395 t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
1396 t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
1397 t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
1398 /* Mid point, corresponds to pre-reset PTPCLKVAL */
1399 t12 = t1 + (t2 - t1) / 2;
1400 /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
1401 t34 = t3 + (t4 - t3) / 2;
1402 /* Advance PTPCLKVAL by the time it took since its readout */
1405 __sja1105_ptp_adjtime(ds, now);
1408 mutex_unlock(&priv->ptp_data.lock);
1410 dev_info(priv->ds->dev,
1411 "Reset switch and programmed static config. Reason: %s\n",
1412 sja1105_reset_reasons[reason]);
1414 /* Configure the CGU (PLLs) for MII and RMII PHYs.
1415 * For these interfaces there is no dynamic configuration
1416 * needed, since PLLs have same settings at all speeds.
1418 rc = sja1105_clocking_setup(priv);
1422 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1423 rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
1428 mutex_unlock(&priv->mgmt_lock);
1433 static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
1435 struct sja1105_mac_config_entry *mac;
1437 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1439 mac[port].vlanid = pvid;
1441 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1445 static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
1447 struct sja1105_vlan_lookup_entry *vlan;
1450 vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
1451 count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
1453 for (i = 0; i < count; i++)
1454 if (vlan[i].vlanid == vid)
1457 /* Return an invalid entry index if not found */
1461 static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid,
1462 bool enabled, bool untagged)
1464 struct sja1105_vlan_lookup_entry *vlan;
1465 struct sja1105_table *table;
1469 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
1471 match = sja1105_is_vlan_configured(priv, vid);
1473 /* Can't delete a missing entry. */
1476 rc = sja1105_table_resize(table, table->entry_count + 1);
1479 match = table->entry_count - 1;
1481 /* Assign pointer after the resize (it's new memory) */
1482 vlan = table->entries;
1483 vlan[match].vlanid = vid;
1485 vlan[match].vlan_bc |= BIT(port);
1486 vlan[match].vmemb_port |= BIT(port);
1488 vlan[match].vlan_bc &= ~BIT(port);
1489 vlan[match].vmemb_port &= ~BIT(port);
1491 /* Also unset tag_port if removing this VLAN was requested,
1492 * just so we don't have a confusing bitmap (no practical purpose).
1494 if (untagged || !enabled)
1495 vlan[match].tag_port &= ~BIT(port);
1497 vlan[match].tag_port |= BIT(port);
1498 /* If there's no port left as member of this VLAN,
1499 * it's time for it to go.
1501 if (!vlan[match].vmemb_port)
1504 dev_dbg(priv->ds->dev,
1505 "%s: port %d, vid %llu, broadcast domain 0x%llx, "
1506 "port members 0x%llx, tagged ports 0x%llx, keep %d\n",
1507 __func__, port, vlan[match].vlanid, vlan[match].vlan_bc,
1508 vlan[match].vmemb_port, vlan[match].tag_port, keep);
1510 rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
1511 &vlan[match], keep);
1516 return sja1105_table_delete_entry(table, match);
1521 static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1525 for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1526 rc = dsa_port_setup_8021q_tagging(ds, i, enabled);
1528 dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n",
1533 dev_info(ds->dev, "%s switch tagging\n",
1534 enabled ? "Enabled" : "Disabled");
1538 static enum dsa_tag_protocol
1539 sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
1540 enum dsa_tag_protocol mp)
1542 return DSA_TAG_PROTO_SJA1105;
1545 /* This callback needs to be present */
1546 static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
1547 const struct switchdev_obj_port_vlan *vlan)
1552 /* The TPID setting belongs to the General Parameters table,
1553 * which can only be partially reconfigured at runtime (and not the TPID).
1554 * So a switch reset is required.
1556 static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
1558 struct sja1105_l2_lookup_params_entry *l2_lookup_params;
1559 struct sja1105_general_params_entry *general_params;
1560 struct sja1105_private *priv = ds->priv;
1561 struct sja1105_table *table;
1566 /* Enable VLAN filtering. */
1568 tpid2 = ETH_P_8021AD;
1570 /* Disable VLAN filtering. */
1571 tpid = ETH_P_SJA1105;
1572 tpid2 = ETH_P_SJA1105;
1575 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1576 general_params = table->entries;
1577 /* EtherType used to identify inner tagged (C-tag) VLAN traffic */
1578 general_params->tpid = tpid;
1579 /* EtherType used to identify outer tagged (S-tag) VLAN traffic */
1580 general_params->tpid2 = tpid2;
1581 /* When VLAN filtering is on, we need to at least be able to
1582 * decode management traffic through the "backup plan".
1584 general_params->incl_srcpt1 = enabled;
1585 general_params->incl_srcpt0 = enabled;
1587 /* VLAN filtering => independent VLAN learning.
1588 * No VLAN filtering => shared VLAN learning.
1590 * In shared VLAN learning mode, untagged traffic still gets
1591 * pvid-tagged, and the FDB table gets populated with entries
1592 * containing the "real" (pvid or from VLAN tag) VLAN ID.
1593 * However the switch performs a masked L2 lookup in the FDB,
1594 * effectively only looking up a frame's DMAC (and not VID) for the
1595 * forwarding decision.
1597 * This is extremely convenient for us, because in modes with
1598 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
1599 * each front panel port. This is good for identification but breaks
1600 * learning badly - the VID of the learnt FDB entry is unique, aka
1601 * no frames coming from any other port are going to have it. So
1602 * for forwarding purposes, this is as though learning was broken
1603 * (all frames get flooded).
1605 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
1606 l2_lookup_params = table->entries;
1607 l2_lookup_params->shared_learn = !enabled;
1609 rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
1611 dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
1613 /* Switch port identification based on 802.1Q is only passable
1614 * if we are not under a vlan_filtering bridge. So make sure
1615 * the two configurations are mutually exclusive.
1617 return sja1105_setup_8021q_tagging(ds, !enabled);
1620 static void sja1105_vlan_add(struct dsa_switch *ds, int port,
1621 const struct switchdev_obj_port_vlan *vlan)
1623 struct sja1105_private *priv = ds->priv;
1627 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1628 rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags &
1629 BRIDGE_VLAN_INFO_UNTAGGED);
1631 dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
1635 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1636 rc = sja1105_pvid_apply(ds->priv, port, vid);
1638 dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n",
1646 static int sja1105_vlan_del(struct dsa_switch *ds, int port,
1647 const struct switchdev_obj_port_vlan *vlan)
1649 struct sja1105_private *priv = ds->priv;
1653 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1654 rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags &
1655 BRIDGE_VLAN_INFO_UNTAGGED);
1657 dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
1665 /* The programming model for the SJA1105 switch is "all-at-once" via static
1666 * configuration tables. Some of these can be dynamically modified at runtime,
1667 * but not the xMII mode parameters table.
1668 * Furthermode, some PHYs may not have crystals for generating their clocks
1669 * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
1670 * ref_clk pin. So port clocking needs to be initialized early, before
1671 * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
1672 * Setting correct PHY link speed does not matter now.
1673 * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
1674 * bindings are not yet parsed by DSA core. We need to parse early so that we
1675 * can populate the xMII mode parameters table.
1677 static int sja1105_setup(struct dsa_switch *ds)
1679 struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
1680 struct sja1105_private *priv = ds->priv;
1683 rc = sja1105_parse_dt(priv, ports);
1685 dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
1689 /* Error out early if internal delays are required through DT
1690 * and we can't apply them.
1692 rc = sja1105_parse_rgmii_delays(priv, ports);
1694 dev_err(ds->dev, "RGMII delay not supported\n");
1698 rc = sja1105_ptp_clock_register(ds);
1700 dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
1703 /* Create and send configuration down to device */
1704 rc = sja1105_static_config_load(priv, ports);
1706 dev_err(ds->dev, "Failed to load static config: %d\n", rc);
1709 /* Configure the CGU (PHY link modes and speeds) */
1710 rc = sja1105_clocking_setup(priv);
1712 dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
1715 /* On SJA1105, VLAN filtering per se is always enabled in hardware.
1716 * The only thing we can do to disable it is lie about what the 802.1Q
1718 * So it will still try to apply VLAN filtering, but all ingress
1719 * traffic (except frames received with EtherType of ETH_P_SJA1105)
1720 * will be internally tagged with a distorted VLAN header where the
1721 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
1723 ds->vlan_filtering_is_global = true;
1725 /* Advertise the 8 egress queues */
1726 ds->num_tx_queues = SJA1105_NUM_TC;
1728 /* The DSA/switchdev model brings up switch ports in standalone mode by
1729 * default, and that means vlan_filtering is 0 since they're not under
1730 * a bridge, so it's safe to set up switch tagging at this time.
1732 return sja1105_setup_8021q_tagging(ds, true);
1735 static void sja1105_teardown(struct dsa_switch *ds)
1737 struct sja1105_private *priv = ds->priv;
1740 for (port = 0; port < SJA1105_NUM_PORTS; port++) {
1741 struct sja1105_port *sp = &priv->ports[port];
1743 if (!dsa_is_user_port(ds, port))
1746 kthread_destroy_worker(sp->xmit_worker);
1749 sja1105_tas_teardown(ds);
1750 sja1105_ptp_clock_unregister(ds);
1751 sja1105_static_config_free(&priv->static_config);
1754 static int sja1105_port_enable(struct dsa_switch *ds, int port,
1755 struct phy_device *phy)
1757 struct net_device *slave;
1759 if (!dsa_is_user_port(ds, port))
1762 slave = dsa_to_port(ds, port)->slave;
1764 slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1769 static void sja1105_port_disable(struct dsa_switch *ds, int port)
1771 struct sja1105_private *priv = ds->priv;
1772 struct sja1105_port *sp = &priv->ports[port];
1774 if (!dsa_is_user_port(ds, port))
1777 kthread_cancel_work_sync(&sp->xmit_work);
1778 skb_queue_purge(&sp->xmit_queue);
1781 static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
1782 struct sk_buff *skb, bool takets)
1784 struct sja1105_mgmt_entry mgmt_route = {0};
1785 struct sja1105_private *priv = ds->priv;
1792 mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
1793 mgmt_route.destports = BIT(port);
1794 mgmt_route.enfport = 1;
1795 mgmt_route.tsreg = 0;
1796 mgmt_route.takets = takets;
1798 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
1799 slot, &mgmt_route, true);
1805 /* Transfer skb to the host port. */
1806 dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
1808 /* Wait until the switch has processed the frame */
1810 rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
1813 dev_err_ratelimited(priv->ds->dev,
1814 "failed to poll for mgmt route\n");
1818 /* UM10944: The ENFPORT flag of the respective entry is
1819 * cleared when a match is found. The host can use this
1820 * flag as an acknowledgment.
1823 } while (mgmt_route.enfport && --timeout);
1826 /* Clean up the management route so that a follow-up
1827 * frame may not match on it by mistake.
1828 * This is only hardware supported on P/Q/R/S - on E/T it is
1829 * a no-op and we are silently discarding the -EOPNOTSUPP.
1831 sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
1832 slot, &mgmt_route, false);
1833 dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
1836 return NETDEV_TX_OK;
1839 #define work_to_port(work) \
1840 container_of((work), struct sja1105_port, xmit_work)
1841 #define tagger_to_sja1105(t) \
1842 container_of((t), struct sja1105_private, tagger_data)
1844 /* Deferred work is unfortunately necessary because setting up the management
1845 * route cannot be done from atomit context (SPI transfer takes a sleepable
1848 static void sja1105_port_deferred_xmit(struct kthread_work *work)
1850 struct sja1105_port *sp = work_to_port(work);
1851 struct sja1105_tagger_data *tagger_data = sp->data;
1852 struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
1853 int port = sp - priv->ports;
1854 struct sk_buff *skb;
1856 while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
1857 struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
1859 mutex_lock(&priv->mgmt_lock);
1861 sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
1863 /* The clone, if there, was made by dsa_skb_tx_timestamp */
1865 sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
1867 mutex_unlock(&priv->mgmt_lock);
1871 /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
1872 * which cannot be reconfigured at runtime. So a switch reset is required.
1874 static int sja1105_set_ageing_time(struct dsa_switch *ds,
1875 unsigned int ageing_time)
1877 struct sja1105_l2_lookup_params_entry *l2_lookup_params;
1878 struct sja1105_private *priv = ds->priv;
1879 struct sja1105_table *table;
1880 unsigned int maxage;
1882 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
1883 l2_lookup_params = table->entries;
1885 maxage = SJA1105_AGEING_TIME_MS(ageing_time);
1887 if (l2_lookup_params->maxage == maxage)
1890 l2_lookup_params->maxage = maxage;
1892 return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
1895 static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
1896 enum tc_setup_type type,
1900 case TC_SETUP_QDISC_TAPRIO:
1901 return sja1105_setup_tc_taprio(ds, port, type_data);
1907 /* We have a single mirror (@to) port, but can configure ingress and egress
1908 * mirroring on all other (@from) ports.
1909 * We need to allow mirroring rules only as long as the @to port is always the
1910 * same, and we need to unset the @to port from mirr_port only when there is no
1911 * mirroring rule that references it.
1913 static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
1914 bool ingress, bool enabled)
1916 struct sja1105_general_params_entry *general_params;
1917 struct sja1105_mac_config_entry *mac;
1918 struct sja1105_table *table;
1919 bool already_enabled;
1923 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1924 general_params = table->entries;
1926 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1928 already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS);
1929 if (already_enabled && enabled && general_params->mirr_port != to) {
1930 dev_err(priv->ds->dev,
1931 "Delete mirroring rules towards port %llu first\n",
1932 general_params->mirr_port);
1941 /* Anybody still referencing mirr_port? */
1942 for (port = 0; port < SJA1105_NUM_PORTS; port++) {
1943 if (mac[port].ing_mirr || mac[port].egr_mirr) {
1948 /* Unset already_enabled for next time */
1950 new_mirr_port = SJA1105_NUM_PORTS;
1952 if (new_mirr_port != general_params->mirr_port) {
1953 general_params->mirr_port = new_mirr_port;
1955 rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
1956 0, general_params, true);
1962 mac[from].ing_mirr = enabled;
1964 mac[from].egr_mirr = enabled;
1966 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
1970 static int sja1105_mirror_add(struct dsa_switch *ds, int port,
1971 struct dsa_mall_mirror_tc_entry *mirror,
1974 return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
1978 static void sja1105_mirror_del(struct dsa_switch *ds, int port,
1979 struct dsa_mall_mirror_tc_entry *mirror)
1981 sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
1982 mirror->ingress, false);
1985 static const struct dsa_switch_ops sja1105_switch_ops = {
1986 .get_tag_protocol = sja1105_get_tag_protocol,
1987 .setup = sja1105_setup,
1988 .teardown = sja1105_teardown,
1989 .set_ageing_time = sja1105_set_ageing_time,
1990 .phylink_validate = sja1105_phylink_validate,
1991 .phylink_mac_config = sja1105_mac_config,
1992 .phylink_mac_link_up = sja1105_mac_link_up,
1993 .phylink_mac_link_down = sja1105_mac_link_down,
1994 .get_strings = sja1105_get_strings,
1995 .get_ethtool_stats = sja1105_get_ethtool_stats,
1996 .get_sset_count = sja1105_get_sset_count,
1997 .get_ts_info = sja1105_get_ts_info,
1998 .port_enable = sja1105_port_enable,
1999 .port_disable = sja1105_port_disable,
2000 .port_fdb_dump = sja1105_fdb_dump,
2001 .port_fdb_add = sja1105_fdb_add,
2002 .port_fdb_del = sja1105_fdb_del,
2003 .port_bridge_join = sja1105_bridge_join,
2004 .port_bridge_leave = sja1105_bridge_leave,
2005 .port_stp_state_set = sja1105_bridge_stp_state_set,
2006 .port_vlan_prepare = sja1105_vlan_prepare,
2007 .port_vlan_filtering = sja1105_vlan_filtering,
2008 .port_vlan_add = sja1105_vlan_add,
2009 .port_vlan_del = sja1105_vlan_del,
2010 .port_mdb_prepare = sja1105_mdb_prepare,
2011 .port_mdb_add = sja1105_mdb_add,
2012 .port_mdb_del = sja1105_mdb_del,
2013 .port_hwtstamp_get = sja1105_hwtstamp_get,
2014 .port_hwtstamp_set = sja1105_hwtstamp_set,
2015 .port_rxtstamp = sja1105_port_rxtstamp,
2016 .port_txtstamp = sja1105_port_txtstamp,
2017 .port_setup_tc = sja1105_port_setup_tc,
2018 .port_mirror_add = sja1105_mirror_add,
2019 .port_mirror_del = sja1105_mirror_del,
2022 static int sja1105_check_device_id(struct sja1105_private *priv)
2024 const struct sja1105_regs *regs = priv->info->regs;
2025 u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
2026 struct device *dev = &priv->spidev->dev;
2031 rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
2036 if (device_id != priv->info->device_id) {
2037 dev_err(dev, "Expected device ID 0x%llx but read 0x%x\n",
2038 priv->info->device_id, device_id);
2042 rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
2043 SJA1105_SIZE_DEVICE_ID);
2047 sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
2049 if (part_no != priv->info->part_no) {
2050 dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n",
2051 priv->info->part_no, part_no);
2058 static int sja1105_probe(struct spi_device *spi)
2060 struct sja1105_tagger_data *tagger_data;
2061 struct device *dev = &spi->dev;
2062 struct sja1105_private *priv;
2063 struct dsa_switch *ds;
2066 if (!dev->of_node) {
2067 dev_err(dev, "No DTS bindings for SJA1105 driver\n");
2071 priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
2075 /* Configure the optional reset pin and bring up switch */
2076 priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
2077 if (IS_ERR(priv->reset_gpio))
2078 dev_dbg(dev, "reset-gpios not defined, ignoring\n");
2080 sja1105_hw_reset(priv->reset_gpio, 1, 1);
2082 /* Populate our driver private structure (priv) based on
2083 * the device tree node that was probed (spi)
2086 spi_set_drvdata(spi, priv);
2088 /* Configure the SPI bus */
2089 spi->bits_per_word = 8;
2090 rc = spi_setup(spi);
2092 dev_err(dev, "Could not init SPI\n");
2096 priv->info = of_device_get_match_data(dev);
2098 /* Detect hardware device */
2099 rc = sja1105_check_device_id(priv);
2101 dev_err(dev, "Device ID check failed: %d\n", rc);
2105 dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
2107 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
2112 ds->num_ports = SJA1105_NUM_PORTS;
2113 ds->ops = &sja1105_switch_ops;
2117 tagger_data = &priv->tagger_data;
2119 mutex_init(&priv->ptp_data.lock);
2120 mutex_init(&priv->mgmt_lock);
2122 sja1105_tas_setup(ds);
2124 rc = dsa_register_switch(priv->ds);
2128 /* Connections between dsa_port and sja1105_port */
2129 for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2130 struct sja1105_port *sp = &priv->ports[port];
2131 struct dsa_port *dp = dsa_to_port(ds, port);
2132 struct net_device *slave;
2134 if (!dsa_is_user_port(ds, port))
2139 sp->data = tagger_data;
2141 kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
2142 sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
2144 if (IS_ERR(sp->xmit_worker)) {
2145 rc = PTR_ERR(sp->xmit_worker);
2147 "failed to create deferred xmit thread: %d\n",
2151 skb_queue_head_init(&sp->xmit_queue);
2156 while (port-- > 0) {
2157 struct sja1105_port *sp = &priv->ports[port];
2159 if (!dsa_is_user_port(ds, port))
2162 kthread_destroy_worker(sp->xmit_worker);
2167 static int sja1105_remove(struct spi_device *spi)
2169 struct sja1105_private *priv = spi_get_drvdata(spi);
2171 dsa_unregister_switch(priv->ds);
2175 static const struct of_device_id sja1105_dt_ids[] = {
2176 { .compatible = "nxp,sja1105e", .data = &sja1105e_info },
2177 { .compatible = "nxp,sja1105t", .data = &sja1105t_info },
2178 { .compatible = "nxp,sja1105p", .data = &sja1105p_info },
2179 { .compatible = "nxp,sja1105q", .data = &sja1105q_info },
2180 { .compatible = "nxp,sja1105r", .data = &sja1105r_info },
2181 { .compatible = "nxp,sja1105s", .data = &sja1105s_info },
2184 MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
2186 static struct spi_driver sja1105_driver = {
2189 .owner = THIS_MODULE,
2190 .of_match_table = of_match_ptr(sja1105_dt_ids),
2192 .probe = sja1105_probe,
2193 .remove = sja1105_remove,
2196 module_spi_driver(sja1105_driver);
2198 MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
2199 MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
2200 MODULE_DESCRIPTION("SJA1105 Driver");
2201 MODULE_LICENSE("GPL v2");