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 Signed-off-by: Rustam Adilov --- 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 * - * 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 @@ -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