Skip to contentSkip to conversation

net: dsa: realtek: add rtl8_4t tag

PATCH Superseded Luiz Angelo Daros de Luca 4 participants 9 messages Feb 9, 2022 – Feb 18, 2022

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Luiz Angelo Daros de Luca

author maintainer
#0 lore raw

cover net: dsa: realtek: add rtl8_4t tag

This DSA tag might not be useful in production as the conduit driver might generate a broken checksum. It is useful, though, for test purpose.

Luiz

author maintainer
#1 lore raw

PATCH 1/2 net: dsa: tag_rtl8_4: add rtl8_4t tailing variant

The switch supports the same tag both before ethertype or before CRC.

---
 include/net/dsa.h    |   2 +
 net/dsa/tag_rtl8_4.c | 119 ++++++++++++++++++++++++++++++++-----------
 2 files changed, 90 insertions(+), 31 deletions(-)
include/net/dsa.h +2 -0
index fd1f62a6e0a8..b688ced04b0e 100644
@@ -52,6 +52,7 @@ struct phylink_link_state;
 #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE		22
 #define DSA_TAG_PROTO_SJA1110_VALUE		23
 #define DSA_TAG_PROTO_RTL8_4_VALUE		24
+#define DSA_TAG_PROTO_RTL8_4T_VALUE		25
 
 enum dsa_tag_protocol {
 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
@@ -79,6 +80,7 @@ enum dsa_tag_protocol {
 	DSA_TAG_PROTO_SEVILLE		= DSA_TAG_PROTO_SEVILLE_VALUE,
 	DSA_TAG_PROTO_SJA1110		= DSA_TAG_PROTO_SJA1110_VALUE,
 	DSA_TAG_PROTO_RTL8_4		= DSA_TAG_PROTO_RTL8_4_VALUE,
+	DSA_TAG_PROTO_RTL8_4T		= DSA_TAG_PROTO_RTL8_4T_VALUE,
 };
 
 struct dsa_switch;
net/dsa/tag_rtl8_4.c +88 -31

182 lines not on this page · raw message

Signature · 1 line
2.35.1
author maintainer
#5 lore raw

PATCH 2/2 net: dsa: realtek: rtl8365mb: add support for rtl8_4t

In reply to Luiz Angelo Daros de Luca

The tailing tag is also supported by this family. The default is still rtl8_4 but now the switch supports changing the tag to rtl8_4t.

---
 drivers/net/dsa/realtek/rtl8365mb.c | 63 +++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
drivers/net/dsa/realtek/rtl8365mb.c +63 -0
index e0c7f64bc56f..02d1521887ae 100644
@@ -853,9 +853,70 @@ static enum dsa_tag_protocol
 rtl8365mb_get_tag_protocol(struct dsa_switch *ds, int port,
 			   enum dsa_tag_protocol mp)
 {
+	struct realtek_priv *priv = ds->priv;
+	u32 val;
+
+	/* No way to return error */
+	regmap_read(priv->map, RTL8365MB_CPU_CTRL_REG, &val);
+
+	switch (FIELD_GET(RTL8365MB_CPU_CTRL_TAG_FORMAT_MASK, val)) {
+	case RTL8365MB_CPU_FORMAT_4BYTES:
+		/* Similar to DSA_TAG_PROTO_RTL4_A but with 1-byte version
+		 * To CPU: [0x88 0x99 0x04 portnum]. Not supported yet.
+		 */
+		break;
+	case RTL8365MB_CPU_FORMAT_8BYTES:
+		switch (FIELD_GET(RTL8365MB_CPU_CTRL_TAG_POSITION_MASK, val)) {
+		case RTL8365MB_CPU_POS_BEFORE_CRC:
+			return DSA_TAG_PROTO_RTL8_4T;
+		case RTL8365MB_CPU_POS_AFTER_SA:
+		default:
+			return DSA_TAG_PROTO_RTL8_4;
+		}
+		break;
+	}
+
 	return DSA_TAG_PROTO_RTL8_4;
 }
 
+static int rtl8365mb_change_tag_protocol(struct dsa_switch *ds, int cpu,
+					 enum dsa_tag_protocol proto)
+{
+	struct realtek_priv *priv = ds->priv;
+	int tag_position;
+	int tag_format;
+	int ret;
+
+	switch (proto) {
+	case DSA_TAG_PROTO_RTL8_4:
+		tag_format = RTL8365MB_CPU_FORMAT_8BYTES;
+		tag_position = RTL8365MB_CPU_POS_AFTER_SA;
+		break;
+	case DSA_TAG_PROTO_RTL8_4T:
+		tag_format = RTL8365MB_CPU_FORMAT_8BYTES;
+		tag_position = RTL8365MB_CPU_POS_BEFORE_CRC;
+		break;
+	/* The switch also supports a 4-byte format, similar to rtl4a but with
+	 * the same 0x04 8-bit version and probably 8-bit port source/dest.
+	 * There is no public doc about it. Not supported yet.
+	 */
+	default:
+		return -EPROTONOSUPPORT;
+	}
+
+	ret = regmap_update_bits(priv->map, RTL8365MB_CPU_CTRL_REG,
+				 RTL8365MB_CPU_CTRL_TAG_POSITION_MASK |
+				 RTL8365MB_CPU_CTRL_TAG_FORMAT_MASK,
+				 FIELD_PREP(RTL8365MB_CPU_CTRL_TAG_POSITION_MASK,
+					    tag_position) |
+				 FIELD_PREP(RTL8365MB_CPU_CTRL_TAG_FORMAT_MASK,
+					    tag_format));
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
 static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port,
 				      phy_interface_t interface)
 {
@@ -2079,6 +2140,7 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 
 static const struct dsa_switch_ops rtl8365mb_switch_ops_smi = {
 	.get_tag_protocol = rtl8365mb_get_tag_protocol,
+	.change_tag_protocol = rtl8365mb_change_tag_protocol,
 	.setup = rtl8365mb_setup,
 	.teardown = rtl8365mb_teardown,
 	.phylink_get_caps = rtl8365mb_phylink_get_caps,
@@ -2097,6 +2159,7 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops_smi = {
 
 static const struct dsa_switch_ops rtl8365mb_switch_ops_mdio = {
 	.get_tag_protocol = rtl8365mb_get_tag_protocol,
+	.change_tag_protocol = rtl8365mb_change_tag_protocol,
 	.setup = rtl8365mb_setup,
 	.teardown = rtl8365mb_teardown,
 	.phylink_get_caps = rtl8365mb_phylink_get_caps,
Signature · 1 line
2.35.1
Acked-by on the v1 series
#8 lore raw
In reply to Luiz Angelo Daros de Luca

Hi Luiz,

Quoted 3 lines · Luiz Angelo Daros de Luca

This DSA tag might not be useful in production as the conduit driver might generate a broken checksum. It is useful, though, for test purpose.

You make it sound like the code is never useful in production, but is that really the case? And what kind of test case do you have in mind?

Also, you call this "tailing" which sounds quite off. Do you mean "trailing"? I think that is the more common terminology.

In any case the series looks fine :-)

Kind regards,
Alvin

maintainer
#2 lore raw

Re: [PATCH net-next 1/2] net: dsa: tag_rtl8_4: add rtl8_4t tailing variant

In reply to Luiz Angelo Daros de Luca

Re: title. Tail or trailing?

net/dsa/tag_rtl8_4.c line 91

Quoted 11 lines · Luiz Angelo Daros de Luca
net/dsa/tag_rtl8_4.c +3 -6
+static inline void rtl8_4_write_tag(struct sk_buff *skb, struct net_device *dev,
+				    char *tag)
 {
 	struct dsa_port *dp = dsa_slave_to_port(dev);
-	__be16 *tag;
-
-	skb_push(skb, RTL8_4_TAG_LEN);
-
-	dsa_alloc_etype_header(skb, RTL8_4_TAG_LEN);
-	tag = dsa_etype_header_pos_tx(skb);
+	__be16 *tag16 = (__be16 *)tag;

Can the tail tag be aligned to an odd offset? In that case, should you access byte by byte, maybe? I'm not sure how arches handle this.

Quoted 25 lines
net/dsa/tag_rtl8_4.c +13 -4
 	/* Set Realtek EtherType */
-	tag[0] = htons(ETH_P_REALTEK);
+	tag16[0] = htons(ETH_P_REALTEK);
 
 	/* Set Protocol; zero REASON */
-	tag[1] = htons(FIELD_PREP(RTL8_4_PROTOCOL, RTL8_4_PROTOCOL_RTL8365MB));
+	tag16[1] = htons(FIELD_PREP(RTL8_4_PROTOCOL, RTL8_4_PROTOCOL_RTL8365MB));
 
 	/* Zero FID_EN, FID, PRI_EN, PRI, KEEP; set LEARN_DIS */
-	tag[2] = htons(FIELD_PREP(RTL8_4_LEARN_DIS, 1));
+	tag16[2] = htons(FIELD_PREP(RTL8_4_LEARN_DIS, 1));
 
 	/* Zero ALLOW; set RX (CPU->switch) forwarding port mask */
-	tag[3] = htons(FIELD_PREP(RTL8_4_RX, BIT(dp->index)));
+	tag16[3] = htons(FIELD_PREP(RTL8_4_RX, BIT(dp->index)));
+}
+
+static struct sk_buff *rtl8_4_tag_xmit(struct sk_buff *skb,
+				       struct net_device *dev)
+{
+	skb_push(skb, RTL8_4_TAG_LEN);
+
+	dsa_alloc_etype_header(skb, RTL8_4_TAG_LEN);
+

net/dsa/tag_rtl8_4.c line 120

Quoted 10 lines
net/dsa/tag_rtl8_4.c +4 -2
+	rtl8_4_write_tag(skb, dev, dsa_etype_header_pos_tx(skb));
 
 	return skb;
 }
 
-static struct sk_buff *rtl8_4_tag_rcv(struct sk_buff *skb,
-				      struct net_device *dev)
+static struct sk_buff *rtl8_4t_tag_xmit(struct sk_buff *skb,
+					struct net_device *dev)
+{

Why don't you want to add:

	if (skb->ip_summed == CHECKSUM_PARTIAL && skb_checksum_help(skb))
		return NULL;

and then you'll make this tagging protocol useful in production too.

Quoted 4 lines
net/dsa/tag_rtl8_4.c +4 -0
+	rtl8_4_write_tag(skb, dev, skb_put(skb, RTL8_4_TAG_LEN));
+
+	return skb;
+}
maintainer
#6 lore raw

Re: [PATCH net-next 2/2] net: dsa: realtek: rtl8365mb: add support for rtl8_4t

In reply to Luiz Angelo Daros de Luca
Quoted 30 lines · Luiz Angelo Daros de Luca

The tailing tag is also supported by this family. The default is still rtl8_4 but now the switch supports changing the tag to rtl8_4t.

---
 drivers/net/dsa/realtek/rtl8365mb.c | 63 +++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
drivers/net/dsa/realtek/rtl8365mb.c +14 -0
index e0c7f64bc56f..02d1521887ae 100644
@@ -853,9 +853,70 @@ static enum dsa_tag_protocol
 rtl8365mb_get_tag_protocol(struct dsa_switch *ds, int port,
 			   enum dsa_tag_protocol mp)
 {
+	struct realtek_priv *priv = ds->priv;
+	u32 val;
+
+	/* No way to return error */
+	regmap_read(priv->map, RTL8365MB_CPU_CTRL_REG, &val);
+
+	switch (FIELD_GET(RTL8365MB_CPU_CTRL_TAG_FORMAT_MASK, val)) {
+	case RTL8365MB_CPU_FORMAT_4BYTES:
+		/* Similar to DSA_TAG_PROTO_RTL4_A but with 1-byte version
+		 * To CPU: [0x88 0x99 0x04 portnum]. Not supported yet.
+		 */
+		break;
+	case RTL8365MB_CPU_FORMAT_8BYTES:
+		switch (FIELD_GET(RTL8365MB_CPU_CTRL_TAG_POSITION_MASK, val)) {

drivers/net/dsa/realtek/rtl8365mb.c static enum dsa_tag_protocol · line 879

Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb.c +9 -0
+		case RTL8365MB_CPU_POS_BEFORE_CRC:
+			return DSA_TAG_PROTO_RTL8_4T;
+		case RTL8365MB_CPU_POS_AFTER_SA:
+		default:
+			return DSA_TAG_PROTO_RTL8_4;
+		}
+		break;
+	}
+
 	return DSA_TAG_PROTO_RTL8_4;

Not great. dsa_get_tag_protocol gets called from dsa_port_parse_cpu, which is earlier than rtl8365mb_cpu_config, so you may temporarily report a tagging protocol which you don't expect (depending on what is in hardware at the time). Can you not keep the current tagging protocol in a variable? You could then avoid the need to return an error on regmap_read too.

Quoted 62 lines
drivers/net/dsa/realtek/rtl8365mb.c +40 -0
 }
 
+static int rtl8365mb_change_tag_protocol(struct dsa_switch *ds, int cpu,
+					 enum dsa_tag_protocol proto)
+{
+	struct realtek_priv *priv = ds->priv;
+	int tag_position;
+	int tag_format;
+	int ret;
+
+	switch (proto) {
+	case DSA_TAG_PROTO_RTL8_4:
+		tag_format = RTL8365MB_CPU_FORMAT_8BYTES;
+		tag_position = RTL8365MB_CPU_POS_AFTER_SA;
+		break;
+	case DSA_TAG_PROTO_RTL8_4T:
+		tag_format = RTL8365MB_CPU_FORMAT_8BYTES;
+		tag_position = RTL8365MB_CPU_POS_BEFORE_CRC;
+		break;
+	/* The switch also supports a 4-byte format, similar to rtl4a but with
+	 * the same 0x04 8-bit version and probably 8-bit port source/dest.
+	 * There is no public doc about it. Not supported yet.
+	 */
+	default:
+		return -EPROTONOSUPPORT;
+	}
+
+	ret = regmap_update_bits(priv->map, RTL8365MB_CPU_CTRL_REG,
+				 RTL8365MB_CPU_CTRL_TAG_POSITION_MASK |
+				 RTL8365MB_CPU_CTRL_TAG_FORMAT_MASK,
+				 FIELD_PREP(RTL8365MB_CPU_CTRL_TAG_POSITION_MASK,
+					    tag_position) |
+				 FIELD_PREP(RTL8365MB_CPU_CTRL_TAG_FORMAT_MASK,
+					    tag_format));
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
 static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port,
 				      phy_interface_t interface)
 {
@@ -2079,6 +2140,7 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 
 static const struct dsa_switch_ops rtl8365mb_switch_ops_smi = {
 	.get_tag_protocol = rtl8365mb_get_tag_protocol,
+	.change_tag_protocol = rtl8365mb_change_tag_protocol,
 	.setup = rtl8365mb_setup,
 	.teardown = rtl8365mb_teardown,
 	.phylink_get_caps = rtl8365mb_phylink_get_caps,
@@ -2097,6 +2159,7 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops_smi = {
 
 static const struct dsa_switch_ops rtl8365mb_switch_ops_mdio = {
 	.get_tag_protocol = rtl8365mb_get_tag_protocol,
+	.change_tag_protocol = rtl8365mb_change_tag_protocol,
 	.setup = rtl8365mb_setup,
 	.teardown = rtl8365mb_teardown,
 	.phylink_get_caps = rtl8365mb_phylink_get_caps,
Signature · 1 line
2.35.1
maintainer
#3 lore raw

Re: [PATCH net-next 1/2] net: dsa: tag_rtl8_4: add rtl8_4t tailing variant

In reply to Vladimir Oltean
Quoted 17 lines · Vladimir Oltean

Re: title. Tail or trailing?

net/dsa/tag_rtl8_4.c line 91

Quoted 11 lines · Luiz Angelo Daros de Luca
net/dsa/tag_rtl8_4.c +3 -6
+static inline void rtl8_4_write_tag(struct sk_buff *skb, struct net_device *dev,
+				    char *tag)
 {
 	struct dsa_port *dp = dsa_slave_to_port(dev);
-	__be16 *tag;
-
-	skb_push(skb, RTL8_4_TAG_LEN);
-
-	dsa_alloc_etype_header(skb, RTL8_4_TAG_LEN);
-	tag = dsa_etype_header_pos_tx(skb);
+	__be16 *tag16 = (__be16 *)tag;

Can the tail tag be aligned to an odd offset? In that case, should you access byte by byte, maybe? I'm not sure how arches handle this.

You should use get_unaligned_be16().

Andrew

author maintainer
#4 lore raw

Re: [PATCH net-next 1/2] net: dsa: tag_rtl8_4: add rtl8_4t tailing variant

In reply to Vladimir Oltean
Quoted 1 line

Re: title. Tail or trailing?

I guess I'll stick with trailing. It looks like the most used term.

Quoted 8 lines · Luiz Angelo Daros de Luca
Quoted 6 lines
net/dsa/tag_rtl8_4.c +2 -2
+static inline void rtl8_4_write_tag(struct sk_buff *skb, struct net_device *dev,
+                                 char *tag)
 {
      struct dsa_port *dp = dsa_slave_to_port(dev);
-     __be16 *tag;
-
Quoted 8 lines

net/dsa/tag_rtl8_4.c line 91

Quoted 5 lines
net/dsa/tag_rtl8_4.c +1 -4
-     skb_push(skb, RTL8_4_TAG_LEN);
-
-     dsa_alloc_etype_header(skb, RTL8_4_TAG_LEN);
-     tag = dsa_etype_header_pos_tx(skb);
+     __be16 *tag16 = (__be16 *)tag;

Can the tail tag be aligned to an odd offset? In that case, should you access byte by byte, maybe? I'm not sure how arches handle this.

I see. I'm not sure my big endian MIPS device is able to test it properly. I tried a wide range of payload sizes without any issue. But I believe we need to play safe here.

Andrew Lunn, you suggested using get_unaligned_be16(). However, as using get_unaligned_be16() I will already save the tag (or at least part of it) in the stack, could it be a memcpy from stack to the buffer (and the opposite while reading the tag)? It will be less intervention than rewriting the code to deal with each byte at a time. I'm not sure if I need to or can help the compiler optimize it. In my MIPS, it seems it did a good job, replacing the memcpy with four swl/swr instructions (used to write unaligned 32-bit values).

Quoted 38 lines
Quoted 25 lines
      /* Set Realtek EtherType */
net/dsa/tag_rtl8_4.c +13 -4
-     tag[0] = htons(ETH_P_REALTEK);
+     tag16[0] = htons(ETH_P_REALTEK);
 
      /* Set Protocol; zero REASON */
-     tag[1] = htons(FIELD_PREP(RTL8_4_PROTOCOL, RTL8_4_PROTOCOL_RTL8365MB));
+     tag16[1] = htons(FIELD_PREP(RTL8_4_PROTOCOL, RTL8_4_PROTOCOL_RTL8365MB));
 
      /* Zero FID_EN, FID, PRI_EN, PRI, KEEP; set LEARN_DIS */
-     tag[2] = htons(FIELD_PREP(RTL8_4_LEARN_DIS, 1));
+     tag16[2] = htons(FIELD_PREP(RTL8_4_LEARN_DIS, 1));
 
      /* Zero ALLOW; set RX (CPU->switch) forwarding port mask */
-     tag[3] = htons(FIELD_PREP(RTL8_4_RX, BIT(dp->index)));
+     tag16[3] = htons(FIELD_PREP(RTL8_4_RX, BIT(dp->index)));
+}
+
+static struct sk_buff *rtl8_4_tag_xmit(struct sk_buff *skb,
+                                    struct net_device *dev)
+{
+     skb_push(skb, RTL8_4_TAG_LEN);
+
+     dsa_alloc_etype_header(skb, RTL8_4_TAG_LEN);
+

net/dsa/tag_rtl8_4.c line 120

Quoted 10 lines
net/dsa/tag_rtl8_4.c +4 -2
+     rtl8_4_write_tag(skb, dev, dsa_etype_header_pos_tx(skb));
 
      return skb;
 }
 
-static struct sk_buff *rtl8_4_tag_rcv(struct sk_buff *skb,
-                                   struct net_device *dev)
+static struct sk_buff *rtl8_4t_tag_xmit(struct sk_buff *skb,
+                                     struct net_device *dev)
+{

Why don't you want to add:

Quoted 4 lines
        if (skb->ip_summed == CHECKSUM_PARTIAL && skb_checksum_help(skb))
                return NULL;

and then you'll make this tagging protocol useful in production too.

It works like a charm! Thanks! And now I have a pretty good use for this new tag: force checksum in software. Whenever the cpu ethernet driver cannot correctly deal with the checksum offloading, switch to rtl8_4t and be happy. It will work either adding 'dsa-tag-protocol = "rtl8_4t";' to the CPU port in the device-tree file or even changing the tag at runtime.

Now checksum will only break if you stack two trailing tags and the first one added does not calculate the checksum and the second one is "rtl8_4t". When "rtl8_4t" does calculate the checksum, it will include the other tag in the sum. But it might even be considered a bug in the first tagger.

Regards,

Luiz

author maintainer
#7 lore raw

Re: [PATCH net-next 2/2] net: dsa: realtek: rtl8365mb: add support for rtl8_4t

In reply to Vladimir Oltean
Quoted 26 lines
Quoted 26 lines

rtl8365mb_get_tag_protocol(struct dsa_switch *ds, int port,
enum dsa_tag_protocol mp)
{

drivers/net/dsa/realtek/rtl8365mb.c +23 -0
+     struct realtek_priv *priv = ds->priv;
+     u32 val;
+
+     /* No way to return error */
+     regmap_read(priv->map, RTL8365MB_CPU_CTRL_REG, &val);
+
+     switch (FIELD_GET(RTL8365MB_CPU_CTRL_TAG_FORMAT_MASK, val)) {
+     case RTL8365MB_CPU_FORMAT_4BYTES:
+             /* Similar to DSA_TAG_PROTO_RTL4_A but with 1-byte version
+              * To CPU: [0x88 0x99 0x04 portnum]. Not supported yet.
+              */
+             break;
+     case RTL8365MB_CPU_FORMAT_8BYTES:
+             switch (FIELD_GET(RTL8365MB_CPU_CTRL_TAG_POSITION_MASK, val)) {
+             case RTL8365MB_CPU_POS_BEFORE_CRC:
+                     return DSA_TAG_PROTO_RTL8_4T;
+             case RTL8365MB_CPU_POS_AFTER_SA:
+             default:
+                     return DSA_TAG_PROTO_RTL8_4;
+             }
+             break;
+     }
+
Quoted 8 lines
Quoted 1 line
      return DSA_TAG_PROTO_RTL8_4;

Not great. dsa_get_tag_protocol gets called from dsa_port_parse_cpu, which is earlier than rtl8365mb_cpu_config, so you may temporarily report a tagging protocol which you don't expect (depending on what is in hardware at the time). Can you not keep the current tagging protocol in a variable? You could then avoid the need to return an error on regmap_read too.

Thanks Vladimir.

Sure. I added the variable to the chip_data struct. With that, I can also remove the tag-related code and variables from rtl8365mb_cpu_config() and rtl8365mb_cpu. Now I simply call the same rtl8365mb_change_tag_protocol() during setup. I believe it is better to have a single place that touches that config.

Regards,

Luiz