1 // SPDX-License-Identifier: GPL-2.0
3 * Handler for Realtek 4 byte DSA switch tags
4 * Currently only supports protocol "A" found in RTL8366RB
7 * This "proprietary tag" header looks like so:
9 * -------------------------------------------------
10 * | MAC DA | MAC SA | 0x8899 | 2 bytes tag | Type |
11 * -------------------------------------------------
13 * The 2 bytes tag form a 16 bit big endian word. The exact
14 * meaning has been guessed from packet dumps from ingress
18 #include <linux/etherdevice.h>
19 #include <linux/bits.h>
23 #define RTL4_A_NAME "rtl4a"
25 #define RTL4_A_HDR_LEN 4
26 #define RTL4_A_ETHERTYPE 0x8899
27 #define RTL4_A_PROTOCOL_SHIFT 12
29 * 0x1 = Realtek Remote Control protocol (RRCP)
30 * 0x2/0x3 seems to be used for loopback testing
31 * 0x9 = RTL8306 DSA protocol
32 * 0xa = RTL8366RB DSA protocol
34 #define RTL4_A_PROTOCOL_RTL8366RB 0xa
36 static struct sk_buff *rtl4a_tag_xmit(struct sk_buff *skb,
37 struct net_device *dev)
39 struct dsa_port *dp = dsa_slave_to_port(dev);
44 /* Pad out to at least 60 bytes */
45 if (unlikely(__skb_put_padto(skb, ETH_ZLEN, false)))
48 netdev_dbg(dev, "add realtek tag to package to port %d\n",
50 skb_push(skb, RTL4_A_HDR_LEN);
52 dsa_alloc_etype_header(skb, RTL4_A_HDR_LEN);
53 tag = dsa_etype_header_pos_tx(skb);
57 *p = htons(RTL4_A_ETHERTYPE);
59 out = (RTL4_A_PROTOCOL_RTL8366RB << RTL4_A_PROTOCOL_SHIFT);
60 /* The lower bits indicate the port number */
61 out |= BIT(dp->index);
63 p = (__be16 *)(tag + 2);
69 static struct sk_buff *rtl4a_tag_rcv(struct sk_buff *skb,
70 struct net_device *dev)
79 if (unlikely(!pskb_may_pull(skb, RTL4_A_HDR_LEN)))
82 tag = dsa_etype_header_pos_rx(skb);
85 if (etype != RTL4_A_ETHERTYPE) {
86 /* Not custom, just pass through */
87 netdev_dbg(dev, "non-realtek ethertype 0x%04x\n", etype);
90 p = (__be16 *)(tag + 2);
92 /* The 4 upper bits are the protocol */
93 prot = (protport >> RTL4_A_PROTOCOL_SHIFT) & 0x0f;
94 if (prot != RTL4_A_PROTOCOL_RTL8366RB) {
95 netdev_err(dev, "unknown realtek protocol 0x%01x\n", prot);
98 port = protport & 0xff;
100 skb->dev = dsa_master_find_slave(dev, 0, port);
102 netdev_dbg(dev, "could not find slave for port %d\n", port);
106 /* Remove RTL4 tag and recalculate checksum */
107 skb_pull_rcsum(skb, RTL4_A_HDR_LEN);
109 dsa_strip_etype_header(skb, RTL4_A_HDR_LEN);
111 dsa_default_offload_fwd_mark(skb);
116 static const struct dsa_device_ops rtl4a_netdev_ops = {
118 .proto = DSA_TAG_PROTO_RTL4_A,
119 .xmit = rtl4a_tag_xmit,
120 .rcv = rtl4a_tag_rcv,
121 .needed_headroom = RTL4_A_HDR_LEN,
123 module_dsa_tag_driver(rtl4a_netdev_ops);
125 MODULE_LICENSE("GPL");
126 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_RTL4_A, RTL4_A_NAME);