Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/klassert/
[linux-2.6-microblaze.git] / net / dsa / tag_rtl4_a.c
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         __be16 *p;
39         u8 *tag;
40         u16 out;
41
42         /* Pad out to at least 60 bytes */
43         if (unlikely(__skb_put_padto(skb, ETH_ZLEN, false)))
44                 return NULL;
45
46         netdev_dbg(dev, "add realtek tag to package to port %d\n",
47                    dp->index);
48         skb_push(skb, RTL4_A_HDR_LEN);
49
50         dsa_alloc_etype_header(skb, RTL4_A_HDR_LEN);
51         tag = dsa_etype_header_pos_tx(skb);
52
53         /* Set Ethertype */
54         p = (__be16 *)tag;
55         *p = htons(RTL4_A_ETHERTYPE);
56
57         out = (RTL4_A_PROTOCOL_RTL8366RB << 12) | (2 << 8);
58         /* The lower bits is the port number */
59         out |= (u8)dp->index;
60         p = (__be16 *)(tag + 2);
61         *p = htons(out);
62
63         return skb;
64 }
65
66 static struct sk_buff *rtl4a_tag_rcv(struct sk_buff *skb,
67                                      struct net_device *dev)
68 {
69         u16 protport;
70         __be16 *p;
71         u16 etype;
72         u8 *tag;
73         u8 prot;
74         u8 port;
75
76         if (unlikely(!pskb_may_pull(skb, RTL4_A_HDR_LEN)))
77                 return NULL;
78
79         tag = dsa_etype_header_pos_rx(skb);
80         p = (__be16 *)tag;
81         etype = ntohs(*p);
82         if (etype != RTL4_A_ETHERTYPE) {
83                 /* Not custom, just pass through */
84                 netdev_dbg(dev, "non-realtek ethertype 0x%04x\n", etype);
85                 return skb;
86         }
87         p = (__be16 *)(tag + 2);
88         protport = ntohs(*p);
89         /* The 4 upper bits are the protocol */
90         prot = (protport >> RTL4_A_PROTOCOL_SHIFT) & 0x0f;
91         if (prot != RTL4_A_PROTOCOL_RTL8366RB) {
92                 netdev_err(dev, "unknown realtek protocol 0x%01x\n", prot);
93                 return NULL;
94         }
95         port = protport & 0xff;
96
97         skb->dev = dsa_master_find_slave(dev, 0, port);
98         if (!skb->dev) {
99                 netdev_dbg(dev, "could not find slave for port %d\n", port);
100                 return NULL;
101         }
102
103         /* Remove RTL4 tag and recalculate checksum */
104         skb_pull_rcsum(skb, RTL4_A_HDR_LEN);
105
106         dsa_strip_etype_header(skb, RTL4_A_HDR_LEN);
107
108         dsa_default_offload_fwd_mark(skb);
109
110         return skb;
111 }
112
113 static const struct dsa_device_ops rtl4a_netdev_ops = {
114         .name   = "rtl4a",
115         .proto  = DSA_TAG_PROTO_RTL4_A,
116         .xmit   = rtl4a_tag_xmit,
117         .rcv    = rtl4a_tag_rcv,
118         .needed_headroom = RTL4_A_HDR_LEN,
119 };
120 module_dsa_tag_driver(rtl4a_netdev_ops);
121
122 MODULE_LICENSE("GPL");
123 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_RTL4_A);