Merge tag 'kbuild-fixes-v6.10' of git://git.kernel.org/pub/scm/linux/kernel/git/masah...
[linux-2.6-block.git] / net / dsa / tag_rtl4_a.c
CommitLineData
efd7fe68
LW
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
86dd9868 15 * frames.
efd7fe68
LW
16 */
17
18#include <linux/etherdevice.h>
19#include <linux/bits.h>
20
bd954b82 21#include "tag.h"
efd7fe68 22
94793a56
VO
23#define RTL4_A_NAME "rtl4a"
24
efd7fe68 25#define RTL4_A_HDR_LEN 4
efd7fe68
LW
26#define RTL4_A_PROTOCOL_SHIFT 12
27/*
28 * 0x1 = Realtek Remote Control protocol (RRCP)
29 * 0x2/0x3 seems to be used for loopback testing
30 * 0x9 = RTL8306 DSA protocol
31 * 0xa = RTL8366RB DSA protocol
32 */
33#define RTL4_A_PROTOCOL_RTL8366RB 0xa
34
35static struct sk_buff *rtl4a_tag_xmit(struct sk_buff *skb,
36 struct net_device *dev)
37{
6ca80638 38 struct dsa_port *dp = dsa_user_to_port(dev);
9eb8bc59 39 __be16 *p;
86dd9868 40 u8 *tag;
86dd9868
LW
41 u16 out;
42
43 /* Pad out to at least 60 bytes */
9eb8bc59 44 if (unlikely(__skb_put_padto(skb, ETH_ZLEN, false)))
86dd9868
LW
45 return NULL;
46
47 netdev_dbg(dev, "add realtek tag to package to port %d\n",
48 dp->index);
49 skb_push(skb, RTL4_A_HDR_LEN);
50
6bef794d 51 dsa_alloc_etype_header(skb, RTL4_A_HDR_LEN);
a72808b6 52 tag = dsa_etype_header_pos_tx(skb);
86dd9868
LW
53
54 /* Set Ethertype */
9eb8bc59 55 p = (__be16 *)tag;
8fedaaca 56 *p = htons(ETH_P_REALTEK);
86dd9868 57
339133f6 58 out = (RTL4_A_PROTOCOL_RTL8366RB << RTL4_A_PROTOCOL_SHIFT);
0e90dfa7
LW
59 /* The lower bits indicate the port number */
60 out |= BIT(dp->index);
61
9eb8bc59 62 p = (__be16 *)(tag + 2);
86dd9868
LW
63 *p = htons(out);
64
efd7fe68
LW
65 return skb;
66}
67
68static struct sk_buff *rtl4a_tag_rcv(struct sk_buff *skb,
29a097b7 69 struct net_device *dev)
efd7fe68
LW
70{
71 u16 protport;
72 __be16 *p;
73 u16 etype;
74 u8 *tag;
75 u8 prot;
76 u8 port;
77
78 if (unlikely(!pskb_may_pull(skb, RTL4_A_HDR_LEN)))
79 return NULL;
80
5d928ff4 81 tag = dsa_etype_header_pos_rx(skb);
efd7fe68
LW
82 p = (__be16 *)tag;
83 etype = ntohs(*p);
8fedaaca 84 if (etype != ETH_P_REALTEK) {
efd7fe68
LW
85 /* Not custom, just pass through */
86 netdev_dbg(dev, "non-realtek ethertype 0x%04x\n", etype);
87 return skb;
88 }
89 p = (__be16 *)(tag + 2);
90 protport = ntohs(*p);
91 /* The 4 upper bits are the protocol */
92 prot = (protport >> RTL4_A_PROTOCOL_SHIFT) & 0x0f;
93 if (prot != RTL4_A_PROTOCOL_RTL8366RB) {
94 netdev_err(dev, "unknown realtek protocol 0x%01x\n", prot);
95 return NULL;
96 }
97 port = protport & 0xff;
98
6ca80638 99 skb->dev = dsa_conduit_find_user(dev, 0, port);
efd7fe68 100 if (!skb->dev) {
6ca80638 101 netdev_dbg(dev, "could not find user for port %d\n", port);
efd7fe68
LW
102 return NULL;
103 }
104
105 /* Remove RTL4 tag and recalculate checksum */
106 skb_pull_rcsum(skb, RTL4_A_HDR_LEN);
107
f1dacd7a 108 dsa_strip_etype_header(skb, RTL4_A_HDR_LEN);
efd7fe68 109
bea79078 110 dsa_default_offload_fwd_mark(skb);
efd7fe68
LW
111
112 return skb;
113}
114
efd7fe68 115static const struct dsa_device_ops rtl4a_netdev_ops = {
94793a56 116 .name = RTL4_A_NAME,
efd7fe68
LW
117 .proto = DSA_TAG_PROTO_RTL4_A,
118 .xmit = rtl4a_tag_xmit,
119 .rcv = rtl4a_tag_rcv,
4e500251 120 .needed_headroom = RTL4_A_HDR_LEN,
efd7fe68
LW
121};
122module_dsa_tag_driver(rtl4a_netdev_ops);
123
0ed6e952 124MODULE_DESCRIPTION("DSA tag driver for Realtek 4 byte protocol A tags");
efd7fe68 125MODULE_LICENSE("GPL");
94793a56 126MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_RTL4_A, RTL4_A_NAME);