Netdev List
 help / color / mirror / Atom feed
From: Rustam Adilov <adilov@disroot.org>
To: "Linus Walleij" <linusw@kernel.org>,
	"Luiz Angelo Daros de Luca" <luizluca@gmail.com>,
	"Alvin Šipraga" <alvin.sipraga@analog.com>,
	"Andrew Lunn" <andrew@lunn.ch>,
	"Vladimir Oltean" <olteanv@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Heiner Kallweit" <hkallweit1@gmail.com>,
	"Russell King" <linux@armlinux.org.uk>,
	"Willem de Bruijn" <willemb@google.com>,
	"Simon Horman" <horms@kernel.org>,
	netdev@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: Rustam Adilov <adilov@disroot.org>, Taiga Ogawa <zectaiga@gmail.com>
Subject: [RFC PATCH net-next 1/6] net: dsa: tag_rtl8_4: add support for RTL9607C tag variant
Date: Fri,  4 Dec 2026 23:25:15 +0500	[thread overview]
Message-ID: <20261204182553.33952-2-adilov@disroot.org> (raw)
In-Reply-To: <20261204182553.33952-1-adilov@disroot.org>

The RTL9607C family of SoCs have an embdedded switch that utilizes
the same Realtek CPU Tag protocol 4 with 8 bytes in size but have
a bit different of a layout. And that layout is also different for
TX versus RX sides and introduces new fields not seen by RTL8_4.

The fields PORT, RX_PORTMASK, PRI, PRI_EN, KEEP, LEARN_DIS, PSEL
share the same semantics as in RTL8_4 tag. The REASON is the same
8 bit field at byte 3 but numbering seem to be very different.

Add basic necessities for this cpu tag variant to function.

Tested-by: Taiga Ogawa <zectaiga@gmail.com>
Signed-off-by: Rustam Adilov <adilov@disroot.org>
---
 include/net/dsa.h    |   2 +
 net/dsa/Kconfig      |   3 +-
 net/dsa/tag_rtl8_4.c | 171 ++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 172 insertions(+), 4 deletions(-)

diff --git a/include/net/dsa.h b/include/net/dsa.h
index 09f3043d57f0..6f86c7625304 100644
--- a/include/net/dsa.h
+++ b/include/net/dsa.h
@@ -63,6 +63,7 @@ struct tc_action;
 #define DSA_TAG_PROTO_MT7628_VALUE		35
 #define DSA_TAG_PROTO_KS8995_VALUE		36
 #define DSA_TAG_PROTO_LAN9645X_VALUE		37
+#define DSA_TAG_PROTO_RTL8_4AP_VALUE		38
 
 enum dsa_tag_protocol {
 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
@@ -103,6 +104,7 @@ enum dsa_tag_protocol {
 	DSA_TAG_PROTO_MT7628		= DSA_TAG_PROTO_MT7628_VALUE,
 	DSA_TAG_PROTO_KS8995		= DSA_TAG_PROTO_KS8995_VALUE,
 	DSA_TAG_PROTO_LAN9645X		= DSA_TAG_PROTO_LAN9645X_VALUE,
+	DSA_TAG_PROTO_RTL8_4AP		= DSA_TAG_PROTO_RTL8_4AP_VALUE,
 };
 
 struct dsa_switch;
diff --git a/net/dsa/Kconfig b/net/dsa/Kconfig
index bdeb60af764b..efa5f1661169 100644
--- a/net/dsa/Kconfig
+++ b/net/dsa/Kconfig
@@ -196,7 +196,8 @@ config NET_DSA_TAG_RTL8_4
 	tristate "Tag driver for Realtek 8 byte protocol 4 tags"
 	help
 	  Say Y or M if you want to enable support for tagging frames for Realtek
-	  switches with 8 byte protocol 4 tags, such as the Realtek RTL8365MB-VC.
+	  switches with 8 byte protocol 4 tags, such as the Realtek RTL8365MB-VC
+	  or the Realtek RTL9607C.
 
 config NET_DSA_TAG_RZN1_A5PSW
 	tristate "Tag driver for Renesas RZ/N1 A5PSW switch"
diff --git a/net/dsa/tag_rtl8_4.c b/net/dsa/tag_rtl8_4.c
index 4da3beebef75..902821524ed3 100644
--- a/net/dsa/tag_rtl8_4.c
+++ b/net/dsa/tag_rtl8_4.c
@@ -4,8 +4,8 @@
  *
  * Copyright (C) 2021 Alvin Šipraga <alsi@bang-olufsen.dk>
  *
- * NOTE: Currently only supports protocol "4" found in the RTL8365MB, hence
- * named tag_rtl8_4.
+ * NOTE: Currently only supports protocol "4" found in the RTL8365MB and RTL9607C,
+ * hence named tag_rtl8_4 and tag_rtl8_4ap.
  *
  * This tag has the following format:
  *
@@ -74,6 +74,54 @@
  * software or hardware. To avoid this issue, if the checksum is still pending,
  * this tagger checksums the packet in software before adding the tag.
  *
+ * This 8 byte switch tag can also be found on RTL9607C family of SoCs with
+ * identical protocol "4", but with a slightly different insides.
+ *
+ * On switch->CPU the bytes 4-7 are different and look like:
+ *
+ *  |-----------------------------------+-----------------------------------|
+ *  | (3) |     (3)    /    (5)         |     (2)     |  (1)  |  (1) |  (4) |
+ *  | PRI |   EXTSPA   / PON_STREAM_ID  |PON_STREAM_ID|L3ROUTE| ORIG | PORT |
+ *  |-----------------------------------+-----------------------------------|
+ *  |    (1)   |           (15-bit)     /      (7-bit)                      |
+ *  |    FBI   |      FLOW_BASED_HASH   /   DST_PORT_MASK                   |
+ *  |-----------------------------------+-----------------------------------|
+ *
+ * On CPU->Switch the bytes 3-7 are different and look like:
+ *
+ *  |-----------------------------------+-----------------------------------|
+ *  |              (8-bit)              |             (8-bit)               |
+ *  |          Protocol [0x04]          |         RX_PORTMASK_LOW           |
+ *  |-----------------------------------+-----------------------------------|
+ *  |     (3-bit)    |(1)|   (1)  | (3) |(1) |(1)|   (1)   |(1) |(2)| (1) |1|
+ *  |RX_PORTMASK_HIGH| X | PRI_EN | PRI |KEEP| X |LEARN_DIS|PSEL| X |DIRTX|X|
+ *  |-----------------------------------+-----------------------------------|
+ *  | (3-bit)|    (2-bit)   |     (4-bit)    |           (7-bit)            |
+ *  | EXTSPA |   PPPOE_ACT  |    PPPOE_IDX   |        PON_STREAM_ID         |
+ *  |-----------------------------------+-----------------------------------|
+ *
+ * With the following descriptions for new fields:
+ *
+ *    field           | description
+ *   -----------------+-------------
+ *    EXTSPA          | Source Extension Port. Relevant when PORT is CPU (9, 10, 7).
+ *    PON_STREAM_ID   | PON Stream ID (GPON) or LLID index (EPON). Relevant when
+ *                    | PORT is PON (5)
+ *    L3ROUTE         | "This frame is an intended routed frames" as per docs
+ *    ORIG            | The received frame by NIC is the original packet's format
+ *                    | before switch lookup and switch didn't do any modifications
+ *    FBI             | Flow Based I...? Only relevant when REASON is FB (aka < 64)
+ *    FLOW_BASED_HASH | Flow Based Hash Index.
+ *    DST_PORT_MASK   | Switch lookup destination port mask. bit 0: CPU, bit 1-6 are
+ *                    | extension ports 0~5.
+ *    PSEL            | PON stream id (GPON) / LLID index(EPON) selection.
+ *    DIRTX           | Direct TX, whatever that means? Used to be called L34KEEP.
+ *    PPPOE_ACT       | Egress action for PPPoE header whose Ether Type = 0x8864.
+ *                    | 0: intact, 1: Add PPPoE header, 2: Remove PPPoE header,
+ *                    | 3: remarking PPPoE switch TX mac action.
+ *    PPPOE_IDX       | PPPoE index pointed to an entry of the PPPoE table in the
+ *                    | switch. Switch TX-MAC look PPPoE table up by this index to
+ *                    | get PPPoE session ID.
  */
 
 #include <linux/bitfield.h>
@@ -84,11 +132,12 @@
 
 /* Protocols supported:
  *
- * 0x04 = RTL8365MB DSA protocol
+ * 0x04 = RTL8365MB / RTL9607C DSA protocol
  */
 
 #define RTL8_4_NAME			"rtl8_4"
 #define RTL8_4T_NAME			"rtl8_4t"
+#define RTL8_4AP_NAME			"rtl8_4ap"
 
 #define RTL8_4_TAG_LEN			8
 
@@ -97,13 +146,23 @@
 #define RTL8_4_REASON			GENMASK(7, 0)
 #define   RTL8_4_REASON_FORWARD		0
 #define   RTL8_4_REASON_TRAP		80
+#define   RTL8_4AP_REASON_FLOOD		202
+#define   RTL8_4AP_REASON_LUT		204
+
+#define RTL8_4AP_RX_PORTMASK_LOW	GENMASK(7, 0)
+#define RTL8_4AP_RX_PORTMASK_HIGH	GENMASK(15, 13)
+#define RTL9607C_RX_PORTMASK_HIGH	GENMASK(10, 8)
 
+#define RTL8_4AP_DIRTX			BIT(1)
 #define RTL8_4_LEARN_DIS		BIT(5)
 #define RTL8_4_KEEP			BIT(7)
 
 #define RTL8_4_TX			GENMASK(3, 0)
 #define RTL8_4_RX			GENMASK(10, 0)
 
+#define RTL8_4AP_DST_PMASK		GENMASK(6, 0)
+#define   RTL8_4AP_DST_CPU_MASK		BIT(0)
+
 static void rtl8_4_write_tag(struct sk_buff *skb, struct net_device *dev,
 			     void *tag)
 {
@@ -153,6 +212,44 @@ static struct sk_buff *rtl8_4t_tag_xmit(struct sk_buff *skb,
 	return skb;
 }
 
+static void rtl8_4ap_write_tag(struct sk_buff *skb, struct net_device *dev,
+			       void *tag)
+{
+	u16 pmask = dsa_xmit_port_mask(skb, dev);
+	__be16 tag16[RTL8_4_TAG_LEN / 2];
+	u16 pmask_low, pmask_high;
+
+	/* Set Realtek EtherType */
+	tag16[0] = htons(ETH_P_REALTEK);
+
+	pmask_low = FIELD_GET(RTL8_4AP_RX_PORTMASK_LOW, pmask);
+	/* Set Protocol; set  low forwarding port mask */
+	tag16[1] = htons(FIELD_PREP(RTL8_4_PROTOCOL, RTL8_4_PROTOCOL_RTL9607C) |
+			 FIELD_PREP(RTL8_4AP_RX_PORTMASK_LOW, pmask_low));
+
+	pmask_high = FIELD_GET(RTL9607C_RX_PORTMASK_HIGH, pmask);
+	/* set high forwarding port mask, KEEP, LEARN_DIS and DIRTX */
+	tag16[2] = htons(FIELD_PREP(RTL8_4AP_RX_PORTMASK_HIGH, pmask_high) |
+			 RTL8_4_LEARN_DIS | RTL8_4_KEEP | RTL8_4AP_DIRTX);
+
+	/* Zero EXTSPA, PPPOE_ACT/IDX and PON_STREAM_ID */
+	tag16[3] = 0;
+
+	memcpy(tag, tag16, RTL8_4_TAG_LEN);
+}
+
+static struct sk_buff *rtl8_4ap_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);
+
+	rtl8_4ap_write_tag(skb, dev, dsa_etype_header_pos_tx(skb));
+
+	return skb;
+}
+
 static int rtl8_4_read_tag(struct sk_buff *skb, struct net_device *dev,
 			   void *tag)
 {
@@ -242,6 +339,60 @@ static struct sk_buff *rtl8_4t_tag_rcv(struct sk_buff *skb,
 	return skb;
 }
 
+static int rtl8_4ap_read_tag(struct sk_buff *skb, struct net_device *dev,
+			     void *tag)
+{
+	__be16 tag16[RTL8_4_TAG_LEN / 2];
+	u16 etype;
+	u8 reason;
+	u8 proto;
+	u8 port;
+	u8 dst_pmask;
+	bool is_lut;
+
+	memcpy(tag16, tag, RTL8_4_TAG_LEN);
+
+	/* Parse Realtek EtherType */
+	etype = ntohs(tag16[0]);
+	if (unlikely(etype != ETH_P_REALTEK)) {
+		dev_warn_ratelimited(&dev->dev,
+				     "non-realtek ethertype 0x%04x\n", etype);
+		return -EPROTO;
+	}
+
+	/* Parse Protocol */
+	proto = FIELD_GET(RTL8_4_PROTOCOL, ntohs(tag16[1]));
+	if (unlikely(proto != RTL8_4_PROTOCOL_RTL9607C)) {
+		dev_warn_ratelimited(&dev->dev,
+				     "unknown realtek protocol 0x%02x\n", proto);
+		return -EPROTO;
+	}
+
+	/* Parse REASON */
+	reason = FIELD_GET(RTL8_4_REASON, ntohs(tag16[1]));
+
+	/* Parse TX (switch->CPU) */
+	port = FIELD_GET(RTL8_4_TX, ntohs(tag16[2]));
+	skb->dev = dsa_conduit_find_user(dev, 0, port);
+	if (!skb->dev) {
+		dev_warn_ratelimited(&dev->dev,
+				     "could not find user for port %d\n", port);
+		return -ENOENT;
+	}
+
+	/* Parse destination port mask */
+	dst_pmask = FIELD_GET(RTL8_4AP_DST_PMASK, ntohs(tag16[3]));
+
+	is_lut = reason == RTL8_4AP_REASON_LUT;
+	if (!is_lut && reason != RTL8_4AP_REASON_FLOOD) {
+		dev_warn_ratelimited(&dev->dev, "Unknown reason code: %d\n", reason);
+	} else if (!is_lut || (dst_pmask & RTL8_4AP_DST_CPU_MASK)) {
+		dsa_default_offload_fwd_mark(skb);
+	}
+
+	return 0;
+}
+
 /* Ethertype version */
 static const struct dsa_device_ops rtl8_4_netdev_ops = {
 	.name = "rtl8_4",
@@ -268,9 +419,23 @@ DSA_TAG_DRIVER(rtl8_4t_netdev_ops);
 
 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_RTL8_4T, RTL8_4T_NAME);
 
+/* Ethertype RTL9607C version */
+static const struct dsa_device_ops rtl8_4ap_netdev_ops = {
+	.name = RTL8_4AP_NAME,
+	.proto = DSA_TAG_PROTO_RTL8_4AP,
+	.xmit = rtl8_4ap_tag_xmit,
+	.rcv = rtl8_4ap_tag_rcv,
+	.needed_headroom = RTL8_4_TAG_LEN,
+};
+
+DSA_TAG_DRIVER(rtl8_4ap_netdev_ops);
+
+MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_RTL8_4AP, RTL8_4AP_NAME);
+
 static struct dsa_tag_driver *dsa_tag_drivers[] = {
 	&DSA_TAG_DRIVER_NAME(rtl8_4_netdev_ops),
 	&DSA_TAG_DRIVER_NAME(rtl8_4t_netdev_ops),
+	&DSA_TAG_DRIVER_NAME(rtl8_4ap_netdev_ops),
 };
 module_dsa_tag_drivers(dsa_tag_drivers);
 
-- 
2.56.1


  reply	other threads:[~2026-12-04 18:29 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:25 [RFC PATCH net-next 0/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
2026-12-04 18:25 ` Rustam Adilov [this message]
2026-12-04 18:25 ` [RFC PATCH net-next 2/6] net: dsa: realtek: add new mmio interface for drivers Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 3/6] dt-bindings: net: dsa: realtek: add new rtl9607c switch compatible Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 4/6] net: dsa: realtek: add support for RTL9607C switch Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 5/6] net: phy: realtek: use mdio.addr instead of 0 in rtlgen_read/write_vend2 Rustam Adilov
2026-12-04 18:25 ` [RFC PATCH net-next 6/6] net: phy: realtek: add support for RTL9607C internal PHYs Rustam Adilov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204182553.33952-2-adilov@disroot.org \
    --to=adilov@disroot.org \
    --cc=alvin.sipraga@analog.com \
    --cc=andrew@lunn.ch \
    --cc=conor+dt@kernel.org \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=edumazet@kernel.org \
    --cc=hkallweit1@gmail.com \
    --cc=horms@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=luizluca@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=olteanv@gmail.com \
    --cc=pabeni@redhat.com \
    --cc=robh@kernel.org \
    --cc=willemb@google.com \
    --cc=zectaiga@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox