3b7d8f37cdfa59da11fd859657696d35e101ff82
[openwrt/openwrt.git] /
1 From 964dbf186eaa84d409c359ddf09c827a3fbe8228 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= <noltari@gmail.com>
3 Date: Wed, 17 Mar 2021 11:29:26 +0100
4 Subject: [PATCH] net: dsa: tag_brcm: add support for legacy tags
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Add support for legacy Broadcom tags, which are similar to DSA_TAG_PROTO_BRCM.
10 These tags are used on BCM5325, BCM5365 and BCM63xx switches.
11
12 Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
13 Signed-off-by: David S. Miller <davem@davemloft.net>
14 ---
15 include/net/dsa.h | 2 +
16 net/dsa/Kconfig | 7 +++
17 net/dsa/tag_brcm.c | 107 +++++++++++++++++++++++++++++++++++++++++++--
18 3 files changed, 113 insertions(+), 3 deletions(-)
19
20 --- a/include/net/dsa.h
21 +++ b/include/net/dsa.h
22 @@ -45,10 +45,12 @@ struct phylink_link_state;
23 #define DSA_TAG_PROTO_OCELOT_VALUE 15
24 #define DSA_TAG_PROTO_AR9331_VALUE 16
25 #define DSA_TAG_PROTO_RTL4_A_VALUE 17
26 +#define DSA_TAG_PROTO_BRCM_LEGACY_VALUE 22
27
28 enum dsa_tag_protocol {
29 DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
30 DSA_TAG_PROTO_BRCM = DSA_TAG_PROTO_BRCM_VALUE,
31 + DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
32 DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
33 DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE,
34 DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE,
35 --- a/net/dsa/Kconfig
36 +++ b/net/dsa/Kconfig
37 @@ -47,6 +47,13 @@ config NET_DSA_TAG_BRCM
38 Say Y if you want to enable support for tagging frames for the
39 Broadcom switches which place the tag after the MAC source address.
40
41 +config NET_DSA_TAG_BRCM_LEGACY
42 + tristate "Tag driver for Broadcom legacy switches using in-frame headers"
43 + select NET_DSA_TAG_BRCM_COMMON
44 + help
45 + Say Y if you want to enable support for tagging frames for the
46 + Broadcom legacy switches which place the tag after the MAC source
47 + address.
48
49 config NET_DSA_TAG_BRCM_PREPEND
50 tristate "Tag driver for Broadcom switches using prepended headers"
51 --- a/net/dsa/tag_brcm.c
52 +++ b/net/dsa/tag_brcm.c
53 @@ -11,9 +11,26 @@
54
55 #include "dsa_priv.h"
56
57 -/* This tag length is 4 bytes, older ones were 6 bytes, we do not
58 - * handle them
59 - */
60 +/* Legacy Broadcom tag (6 bytes) */
61 +#define BRCM_LEG_TAG_LEN 6
62 +
63 +/* Type fields */
64 +/* 1st byte in the tag */
65 +#define BRCM_LEG_TYPE_HI 0x88
66 +/* 2nd byte in the tag */
67 +#define BRCM_LEG_TYPE_LO 0x74
68 +
69 +/* Tag fields */
70 +/* 3rd byte in the tag */
71 +#define BRCM_LEG_UNICAST (0 << 5)
72 +#define BRCM_LEG_MULTICAST (1 << 5)
73 +#define BRCM_LEG_EGRESS (2 << 5)
74 +#define BRCM_LEG_INGRESS (3 << 5)
75 +
76 +/* 6th byte in the tag */
77 +#define BRCM_LEG_PORT_ID (0xf)
78 +
79 +/* Newer Broadcom tag (4 bytes) */
80 #define BRCM_TAG_LEN 4
81
82 /* Tag is constructed and desconstructed using byte by byte access
83 @@ -194,6 +211,87 @@ DSA_TAG_DRIVER(brcm_netdev_ops);
84 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM);
85 #endif
86
87 +#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY)
88 +static struct sk_buff *brcm_leg_tag_xmit(struct sk_buff *skb,
89 + struct net_device *dev)
90 +{
91 + struct dsa_port *dp = dsa_slave_to_port(dev);
92 + u8 *brcm_tag;
93 +
94 + /* The Ethernet switch we are interfaced with needs packets to be at
95 + * least 64 bytes (including FCS) otherwise they will be discarded when
96 + * they enter the switch port logic. When Broadcom tags are enabled, we
97 + * need to make sure that packets are at least 70 bytes
98 + * (including FCS and tag) because the length verification is done after
99 + * the Broadcom tag is stripped off the ingress packet.
100 + *
101 + * Let dsa_slave_xmit() free the SKB
102 + */
103 + if (__skb_put_padto(skb, ETH_ZLEN + BRCM_LEG_TAG_LEN, false))
104 + return NULL;
105 +
106 + skb_push(skb, BRCM_LEG_TAG_LEN);
107 +
108 + memmove(skb->data, skb->data + BRCM_LEG_TAG_LEN, 2 * ETH_ALEN);
109 +
110 + brcm_tag = skb->data + 2 * ETH_ALEN;
111 +
112 + /* Broadcom tag type */
113 + brcm_tag[0] = BRCM_LEG_TYPE_HI;
114 + brcm_tag[1] = BRCM_LEG_TYPE_LO;
115 +
116 + /* Broadcom tag value */
117 + brcm_tag[2] = BRCM_LEG_EGRESS;
118 + brcm_tag[3] = 0;
119 + brcm_tag[4] = 0;
120 + brcm_tag[5] = dp->index & BRCM_LEG_PORT_ID;
121 +
122 + return skb;
123 +}
124 +
125 +static struct sk_buff *brcm_leg_tag_rcv(struct sk_buff *skb,
126 + struct net_device *dev,
127 + struct packet_type *pt)
128 +{
129 + int source_port;
130 + u8 *brcm_tag;
131 +
132 + if (unlikely(!pskb_may_pull(skb, BRCM_LEG_PORT_ID)))
133 + return NULL;
134 +
135 + brcm_tag = skb->data - 2;
136 +
137 + source_port = brcm_tag[5] & BRCM_LEG_PORT_ID;
138 +
139 + skb->dev = dsa_master_find_slave(dev, 0, source_port);
140 + if (!skb->dev)
141 + return NULL;
142 +
143 + /* Remove Broadcom tag and update checksum */
144 + skb_pull_rcsum(skb, BRCM_LEG_TAG_LEN);
145 +
146 + skb->offload_fwd_mark = 1;
147 +
148 + /* Move the Ethernet DA and SA */
149 + memmove(skb->data - ETH_HLEN,
150 + skb->data - ETH_HLEN - BRCM_LEG_TAG_LEN,
151 + 2 * ETH_ALEN);
152 +
153 + return skb;
154 +}
155 +
156 +static const struct dsa_device_ops brcm_legacy_netdev_ops = {
157 + .name = "brcm-legacy",
158 + .proto = DSA_TAG_PROTO_BRCM_LEGACY,
159 + .xmit = brcm_leg_tag_xmit,
160 + .rcv = brcm_leg_tag_rcv,
161 + .overhead = BRCM_LEG_TAG_LEN,
162 +};
163 +
164 +DSA_TAG_DRIVER(brcm_legacy_netdev_ops);
165 +MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_LEGACY);
166 +#endif /* CONFIG_NET_DSA_TAG_BRCM_LEGACY */
167 +
168 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
169 static struct sk_buff *brcm_tag_xmit_prepend(struct sk_buff *skb,
170 struct net_device *dev)
171 @@ -226,6 +324,9 @@ static struct dsa_tag_driver *dsa_tag_dr
172 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM)
173 &DSA_TAG_DRIVER_NAME(brcm_netdev_ops),
174 #endif
175 +#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY)
176 + &DSA_TAG_DRIVER_NAME(brcm_legacy_netdev_ops),
177 +#endif
178 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
179 &DSA_TAG_DRIVER_NAME(brcm_prepend_netdev_ops),
180 #endif