Skip to content

Commit 640f763

Browse files
vladimirolteandavem330
authored andcommitted
net: dsa: sja1105: Add support for Spanning Tree Protocol
While not explicitly documented as supported in UM10944, compliance with the STP states can be obtained by manipulating 3 settings at the (per-port) MAC config level: dynamic learning, inhibiting reception of regular traffic, and inhibiting transmission of regular traffic. In all these modes, transmission and reception of special BPDU frames from the stack is still enabled (not inhibited by the MAC-level settings). On ingress, BPDUs are classified by the MAC filter as link-local (01-80-C2-00-00-00) and forwarded to the CPU port. This mechanism works under all conditions (even without the custom 802.1Q tagging) because the switch hardware inserts the source port and switch ID into bytes 4 and 5 of the MAC-filtered frames. Then the DSA .rcv handler needs to put back zeroes into the MAC address after decoding the source port information. On egress, BPDUs are transmitted using management routes from the xmit worker thread. Again this does not require switch tagging, as the switch port is programmed through SPI to hold a temporary (single-fire) route for a frame with the programmed destination MAC (01-80-C2-00-00-00). STP is activated using the following commands and was tested by connecting two front-panel ports together and noticing that switching loops were prevented (one port remains in the blocking state): $ ip link add name br0 type bridge stp_state 1 && ip link set br0 up $ for eth in $(ls /sys/devices/platform/soc/2100000.spi/spi_master/spi0/spi0.1/net/); do ip link set ${eth} master br0 && ip link set ${eth} up; done Signed-off-by: Vladimir Oltean <[email protected]> Reviewed-by: Andrew Lunn <[email protected]> Reviewed-by: Florian Fainelli <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 227d07a commit 640f763

File tree

1 file changed

+99
-9
lines changed

1 file changed

+99
-9
lines changed

drivers/net/dsa/sja1105/sja1105_main.c

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ static int sja1105_init_mac_settings(struct sja1105_private *priv)
9292
.drpuntag = false,
9393
/* Don't retag 802.1p (VID 0) traffic with the pvid */
9494
.retag = false,
95-
/* Enable learning and I/O on user ports by default. */
96-
.dyn_learn = true,
95+
/* Disable learning and I/O on user ports by default -
96+
* STP will enable it.
97+
*/
98+
.dyn_learn = false,
9799
.egress = false,
98100
.ingress = false,
99101
};
@@ -119,8 +121,17 @@ static int sja1105_init_mac_settings(struct sja1105_private *priv)
119121

120122
mac = table->entries;
121123

122-
for (i = 0; i < SJA1105_NUM_PORTS; i++)
124+
for (i = 0; i < SJA1105_NUM_PORTS; i++) {
123125
mac[i] = default_mac;
126+
if (i == dsa_upstream_port(priv->ds, i)) {
127+
/* STP doesn't get called for CPU port, so we need to
128+
* set the I/O parameters statically.
129+
*/
130+
mac[i].dyn_learn = true;
131+
mac[i].ingress = true;
132+
mac[i].egress = true;
133+
}
134+
}
124135

125136
return 0;
126137
}
@@ -655,12 +666,14 @@ static sja1105_speed_t sja1105_get_speed_cfg(unsigned int speed_mbps)
655666
* for a specific port.
656667
*
657668
* @speed_mbps: If 0, leave the speed unchanged, else adapt MAC to PHY speed.
658-
* @enabled: Manage Rx and Tx settings for this port. Overrides the static
659-
* configuration settings.
669+
* @enabled: Manage Rx and Tx settings for this port. If false, overrides the
670+
* settings from the STP state, but not persistently (does not
671+
* overwrite the static MAC info for this port).
660672
*/
661673
static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
662674
int speed_mbps, bool enabled)
663675
{
676+
struct sja1105_mac_config_entry dyn_mac;
664677
struct sja1105_xmii_params_entry *mii;
665678
struct sja1105_mac_config_entry *mac;
666679
struct device *dev = priv->ds->dev;
@@ -693,12 +706,13 @@ static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
693706
* the code common, we'll use the static configuration tables as a
694707
* reasonable approximation for both E/T and P/Q/R/S.
695708
*/
696-
mac[port].ingress = enabled;
697-
mac[port].egress = enabled;
709+
dyn_mac = mac[port];
710+
dyn_mac.ingress = enabled && mac[port].ingress;
711+
dyn_mac.egress = enabled && mac[port].egress;
698712

699713
/* Write to the dynamic reconfiguration tables */
700714
rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG,
701-
port, &mac[port], true);
715+
port, &dyn_mac, true);
702716
if (rc < 0) {
703717
dev_err(dev, "Failed to write MAC config: %d\n", rc);
704718
return rc;
@@ -986,6 +1000,50 @@ static int sja1105_bridge_member(struct dsa_switch *ds, int port,
9861000
port, &l2_fwd[port], true);
9871001
}
9881002

1003+
static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1004+
u8 state)
1005+
{
1006+
struct sja1105_private *priv = ds->priv;
1007+
struct sja1105_mac_config_entry *mac;
1008+
1009+
mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1010+
1011+
switch (state) {
1012+
case BR_STATE_DISABLED:
1013+
case BR_STATE_BLOCKING:
1014+
/* From UM10944 description of DRPDTAG (why put this there?):
1015+
* "Management traffic flows to the port regardless of the state
1016+
* of the INGRESS flag". So BPDUs are still be allowed to pass.
1017+
* At the moment no difference between DISABLED and BLOCKING.
1018+
*/
1019+
mac[port].ingress = false;
1020+
mac[port].egress = false;
1021+
mac[port].dyn_learn = false;
1022+
break;
1023+
case BR_STATE_LISTENING:
1024+
mac[port].ingress = true;
1025+
mac[port].egress = false;
1026+
mac[port].dyn_learn = false;
1027+
break;
1028+
case BR_STATE_LEARNING:
1029+
mac[port].ingress = true;
1030+
mac[port].egress = false;
1031+
mac[port].dyn_learn = true;
1032+
break;
1033+
case BR_STATE_FORWARDING:
1034+
mac[port].ingress = true;
1035+
mac[port].egress = true;
1036+
mac[port].dyn_learn = true;
1037+
break;
1038+
default:
1039+
dev_err(ds->dev, "invalid STP state: %d\n", state);
1040+
return;
1041+
}
1042+
1043+
sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1044+
&mac[port], true);
1045+
}
1046+
9891047
static int sja1105_bridge_join(struct dsa_switch *ds, int port,
9901048
struct net_device *br)
9911049
{
@@ -998,6 +1056,23 @@ static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
9981056
sja1105_bridge_member(ds, port, br, false);
9991057
}
10001058

1059+
static u8 sja1105_stp_state_get(struct sja1105_private *priv, int port)
1060+
{
1061+
struct sja1105_mac_config_entry *mac;
1062+
1063+
mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1064+
1065+
if (!mac[port].ingress && !mac[port].egress && !mac[port].dyn_learn)
1066+
return BR_STATE_BLOCKING;
1067+
if (mac[port].ingress && !mac[port].egress && !mac[port].dyn_learn)
1068+
return BR_STATE_LISTENING;
1069+
if (mac[port].ingress && !mac[port].egress && mac[port].dyn_learn)
1070+
return BR_STATE_LEARNING;
1071+
if (mac[port].ingress && mac[port].egress && mac[port].dyn_learn)
1072+
return BR_STATE_FORWARDING;
1073+
return -EINVAL;
1074+
}
1075+
10011076
/* For situations where we need to change a setting at runtime that is only
10021077
* available through the static configuration, resetting the switch in order
10031078
* to upload the new static config is unavoidable. Back up the settings we
@@ -1008,16 +1083,27 @@ static int sja1105_static_config_reload(struct sja1105_private *priv)
10081083
{
10091084
struct sja1105_mac_config_entry *mac;
10101085
int speed_mbps[SJA1105_NUM_PORTS];
1086+
u8 stp_state[SJA1105_NUM_PORTS];
10111087
int rc, i;
10121088

10131089
mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
10141090

10151091
/* Back up settings changed by sja1105_adjust_port_config and
1016-
* and restore their defaults.
1092+
* sja1105_bridge_stp_state_set and restore their defaults.
10171093
*/
10181094
for (i = 0; i < SJA1105_NUM_PORTS; i++) {
10191095
speed_mbps[i] = sja1105_speed[mac[i].speed];
10201096
mac[i].speed = SJA1105_SPEED_AUTO;
1097+
if (i == dsa_upstream_port(priv->ds, i)) {
1098+
mac[i].ingress = true;
1099+
mac[i].egress = true;
1100+
mac[i].dyn_learn = true;
1101+
} else {
1102+
stp_state[i] = sja1105_stp_state_get(priv, i);
1103+
mac[i].ingress = false;
1104+
mac[i].egress = false;
1105+
mac[i].dyn_learn = false;
1106+
}
10211107
}
10221108

10231109
/* Reset switch and send updated static configuration */
@@ -1036,6 +1122,9 @@ static int sja1105_static_config_reload(struct sja1105_private *priv)
10361122
for (i = 0; i < SJA1105_NUM_PORTS; i++) {
10371123
bool enabled = (speed_mbps[i] != 0);
10381124

1125+
if (i != dsa_upstream_port(priv->ds, i))
1126+
sja1105_bridge_stp_state_set(priv->ds, i, stp_state[i]);
1127+
10391128
rc = sja1105_adjust_port_config(priv, i, speed_mbps[i],
10401129
enabled);
10411130
if (rc < 0)
@@ -1433,6 +1522,7 @@ static const struct dsa_switch_ops sja1105_switch_ops = {
14331522
.port_fdb_del = sja1105_fdb_del,
14341523
.port_bridge_join = sja1105_bridge_join,
14351524
.port_bridge_leave = sja1105_bridge_leave,
1525+
.port_stp_state_set = sja1105_bridge_stp_state_set,
14361526
.port_vlan_prepare = sja1105_vlan_prepare,
14371527
.port_vlan_filtering = sja1105_vlan_filtering,
14381528
.port_vlan_add = sja1105_vlan_add,

0 commit comments

Comments
 (0)