xref: /openbmc/linux/net/dsa/tag_rtl4_a.c (revision 31e67366)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Handler for Realtek 4 byte DSA switch tags
4  * Currently only supports protocol "A" found in RTL8366RB
5  * Copyright (c) 2020 Linus Walleij <linus.walleij@linaro.org>
6  *
7  * This "proprietary tag" header looks like so:
8  *
9  * -------------------------------------------------
10  * | MAC DA | MAC SA | 0x8899 | 2 bytes tag | Type |
11  * -------------------------------------------------
12  *
13  * The 2 bytes tag form a 16 bit big endian word. The exact
14  * meaning has been guessed from packet dumps from ingress
15  * frames.
16  */
17 
18 #include <linux/etherdevice.h>
19 #include <linux/bits.h>
20 
21 #include "dsa_priv.h"
22 
23 #define RTL4_A_HDR_LEN		4
24 #define RTL4_A_ETHERTYPE	0x8899
25 #define RTL4_A_PROTOCOL_SHIFT	12
26 /*
27  * 0x1 = Realtek Remote Control protocol (RRCP)
28  * 0x2/0x3 seems to be used for loopback testing
29  * 0x9 = RTL8306 DSA protocol
30  * 0xa = RTL8366RB DSA protocol
31  */
32 #define RTL4_A_PROTOCOL_RTL8366RB	0xa
33 
34 static struct sk_buff *rtl4a_tag_xmit(struct sk_buff *skb,
35 				      struct net_device *dev)
36 {
37 	struct dsa_port *dp = dsa_slave_to_port(dev);
38 	u8 *tag;
39 	u16 *p;
40 	u16 out;
41 
42 	/* Pad out to at least 60 bytes */
43 	if (unlikely(eth_skb_pad(skb)))
44 		return NULL;
45 	if (skb_cow_head(skb, RTL4_A_HDR_LEN) < 0)
46 		return NULL;
47 
48 	netdev_dbg(dev, "add realtek tag to package to port %d\n",
49 		   dp->index);
50 	skb_push(skb, RTL4_A_HDR_LEN);
51 
52 	memmove(skb->data, skb->data + RTL4_A_HDR_LEN, 2 * ETH_ALEN);
53 	tag = skb->data + 2 * ETH_ALEN;
54 
55 	/* Set Ethertype */
56 	p = (u16 *)tag;
57 	*p = htons(RTL4_A_ETHERTYPE);
58 
59 	out = (RTL4_A_PROTOCOL_RTL8366RB << 12) | (2 << 8);
60 	/* The lower bits is the port numer */
61 	out |= (u8)dp->index;
62 	p = (u16 *)(tag + 2);
63 	*p = htons(out);
64 
65 	return skb;
66 }
67 
68 static struct sk_buff *rtl4a_tag_rcv(struct sk_buff *skb,
69 				     struct net_device *dev,
70 				     struct packet_type *pt)
71 {
72 	u16 protport;
73 	__be16 *p;
74 	u16 etype;
75 	u8 *tag;
76 	u8 prot;
77 	u8 port;
78 
79 	if (unlikely(!pskb_may_pull(skb, RTL4_A_HDR_LEN)))
80 		return NULL;
81 
82 	/* The RTL4 header has its own custom Ethertype 0x8899 and that
83 	 * starts right at the beginning of the packet, after the src
84 	 * ethernet addr. Apparantly skb->data always points 2 bytes in,
85 	 * behind the Ethertype.
86 	 */
87 	tag = skb->data - 2;
88 	p = (__be16 *)tag;
89 	etype = ntohs(*p);
90 	if (etype != RTL4_A_ETHERTYPE) {
91 		/* Not custom, just pass through */
92 		netdev_dbg(dev, "non-realtek ethertype 0x%04x\n", etype);
93 		return skb;
94 	}
95 	p = (__be16 *)(tag + 2);
96 	protport = ntohs(*p);
97 	/* The 4 upper bits are the protocol */
98 	prot = (protport >> RTL4_A_PROTOCOL_SHIFT) & 0x0f;
99 	if (prot != RTL4_A_PROTOCOL_RTL8366RB) {
100 		netdev_err(dev, "unknown realtek protocol 0x%01x\n", prot);
101 		return NULL;
102 	}
103 	port = protport & 0xff;
104 
105 	skb->dev = dsa_master_find_slave(dev, 0, port);
106 	if (!skb->dev) {
107 		netdev_dbg(dev, "could not find slave for port %d\n", port);
108 		return NULL;
109 	}
110 
111 	/* Remove RTL4 tag and recalculate checksum */
112 	skb_pull_rcsum(skb, RTL4_A_HDR_LEN);
113 
114 	/* Move ethernet DA and SA in front of the data */
115 	memmove(skb->data - ETH_HLEN,
116 		skb->data - ETH_HLEN - RTL4_A_HDR_LEN,
117 		2 * ETH_ALEN);
118 
119 	skb->offload_fwd_mark = 1;
120 
121 	return skb;
122 }
123 
124 static const struct dsa_device_ops rtl4a_netdev_ops = {
125 	.name	= "rtl4a",
126 	.proto	= DSA_TAG_PROTO_RTL4_A,
127 	.xmit	= rtl4a_tag_xmit,
128 	.rcv	= rtl4a_tag_rcv,
129 	.overhead = RTL4_A_HDR_LEN,
130 };
131 module_dsa_tag_driver(rtl4a_netdev_ops);
132 
133 MODULE_LICENSE("GPL");
134 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_RTL4_A);
135