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 "tag.h"
22
23#define RTL4_A_NAME		"rtl4a"
24
25#define RTL4_A_HDR_LEN		4
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{
38	struct dsa_port *dp = dsa_user_to_port(dev);
39	__be16 *p;
40	u8 *tag;
41	u16 out;
42
43	/* Pad out to at least 60 bytes */
44	if (unlikely(__skb_put_padto(skb, ETH_ZLEN, false)))
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
51	dsa_alloc_etype_header(skb, RTL4_A_HDR_LEN);
52	tag = dsa_etype_header_pos_tx(skb);
53
54	/* Set Ethertype */
55	p = (__be16 *)tag;
56	*p = htons(ETH_P_REALTEK);
57
58	out = (RTL4_A_PROTOCOL_RTL8366RB << RTL4_A_PROTOCOL_SHIFT);
59	/* The lower bits indicate the port number */
60	out |= BIT(dp->index);
61
62	p = (__be16 *)(tag + 2);
63	*p = htons(out);
64
65	return skb;
66}
67
68static struct sk_buff *rtl4a_tag_rcv(struct sk_buff *skb,
69				     struct net_device *dev)
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
81	tag = dsa_etype_header_pos_rx(skb);
82	p = (__be16 *)tag;
83	etype = ntohs(*p);
84	if (etype != ETH_P_REALTEK) {
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
99	skb->dev = dsa_conduit_find_user(dev, 0, port);
100	if (!skb->dev) {
101		netdev_dbg(dev, "could not find user for port %d\n", port);
102		return NULL;
103	}
104
105	/* Remove RTL4 tag and recalculate checksum */
106	skb_pull_rcsum(skb, RTL4_A_HDR_LEN);
107
108	dsa_strip_etype_header(skb, RTL4_A_HDR_LEN);
109
110	dsa_default_offload_fwd_mark(skb);
111
112	return skb;
113}
114
115static const struct dsa_device_ops rtl4a_netdev_ops = {
116	.name	= RTL4_A_NAME,
117	.proto	= DSA_TAG_PROTO_RTL4_A,
118	.xmit	= rtl4a_tag_xmit,
119	.rcv	= rtl4a_tag_rcv,
120	.needed_headroom = RTL4_A_HDR_LEN,
121};
122module_dsa_tag_driver(rtl4a_netdev_ops);
123
124MODULE_DESCRIPTION("DSA tag driver for Realtek 4 byte protocol A tags");
125MODULE_LICENSE("GPL");
126MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_RTL4_A, RTL4_A_NAME);
127