Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Broadcom tag support
4 *
5 * Copyright (C) 2014 Broadcom Corporation
6 */
7
8#include <linux/dsa/brcm.h>
9#include <linux/etherdevice.h>
10#include <linux/if_vlan.h>
11#include <linux/list.h>
12#include <linux/slab.h>
13
14#include "tag.h"
15
16#define BRCM_NAME "brcm"
17#define BRCM_LEGACY_NAME "brcm-legacy"
18#define BRCM_LEGACY_FCS_NAME "brcm-legacy-fcs"
19#define BRCM_PREPEND_NAME "brcm-prepend"
20
21/* Legacy Broadcom tag (6 bytes) */
22#define BRCM_LEG_TAG_LEN 6
23
24/* Type fields */
25/* 1st byte in the tag */
26#define BRCM_LEG_TYPE_HI 0x88
27/* 2nd byte in the tag */
28#define BRCM_LEG_TYPE_LO 0x74
29
30/* Tag fields */
31/* 3rd byte in the tag */
32#define BRCM_LEG_UNICAST (0 << 5)
33#define BRCM_LEG_MULTICAST (1 << 5)
34#define BRCM_LEG_EGRESS (2 << 5)
35#define BRCM_LEG_INGRESS (3 << 5)
36#define BRCM_LEG_LEN_HI(x) (((x) >> 8) & 0x7)
37
38/* 4th byte in the tag */
39#define BRCM_LEG_LEN_LO(x) ((x) & 0xff)
40
41/* 6th byte in the tag */
42#define BRCM_LEG_PORT_ID (0xf)
43
44/* Newer Broadcom tag (4 bytes) */
45#define BRCM_TAG_LEN 4
46
47/* Tag is constructed and deconstructed using byte by byte access
48 * because the tag is placed after the MAC Source Address, which does
49 * not make it 4-bytes aligned, so this might cause unaligned accesses
50 * on most systems where this is used.
51 */
52
53/* Ingress and egress opcodes */
54#define BRCM_OPCODE_SHIFT 5
55#define BRCM_OPCODE_MASK 0x7
56
57/* Ingress fields */
58/* 1st byte in the tag */
59#define BRCM_IG_TC_SHIFT 2
60#define BRCM_IG_TC_MASK 0x7
61/* 2nd byte in the tag */
62#define BRCM_IG_TE_MASK 0x3
63#define BRCM_IG_TS_SHIFT 7
64/* 3rd byte in the tag */
65#define BRCM_IG_DSTMAP2_MASK 1
66#define BRCM_IG_DSTMAP1_MASK 0xff
67
68/* Egress fields */
69
70/* 2nd byte in the tag */
71#define BRCM_EG_CID_MASK 0xff
72
73/* 3rd byte in the tag */
74#define BRCM_EG_RC_MASK 0xff
75#define BRCM_EG_RC_RSVD (3 << 6)
76#define BRCM_EG_RC_EXCEPTION (1 << 5)
77#define BRCM_EG_RC_PROT_SNOOP (1 << 4)
78#define BRCM_EG_RC_PROT_TERM (1 << 3)
79#define BRCM_EG_RC_SWITCH (1 << 2)
80#define BRCM_EG_RC_MAC_LEARN (1 << 1)
81#define BRCM_EG_RC_MIRROR (1 << 0)
82#define BRCM_EG_TC_SHIFT 5
83#define BRCM_EG_TC_MASK 0x7
84#define BRCM_EG_PID_MASK 0x1f
85
86#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM) || \
87 IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
88
89static struct sk_buff *brcm_tag_xmit_ll(struct sk_buff *skb,
90 struct net_device *dev,
91 unsigned int offset)
92{
93 struct dsa_port *dp = dsa_user_to_port(dev);
94 u16 queue = skb_get_queue_mapping(skb);
95 u16 port_mask;
96 u8 *brcm_tag;
97
98 /* The Ethernet switch we are interfaced with needs packets to be at
99 * least 64 bytes (including FCS) otherwise they will be discarded when
100 * they enter the switch port logic. When Broadcom tags are enabled, we
101 * need to make sure that packets are at least 68 bytes
102 * (including FCS and tag) because the length verification is done after
103 * the Broadcom tag is stripped off the ingress packet.
104 *
105 * Let dsa_user_xmit() free the SKB
106 */
107 if (__skb_put_padto(skb, ETH_ZLEN + BRCM_TAG_LEN, false))
108 return NULL;
109
110 skb_push(skb, BRCM_TAG_LEN);
111
112 if (offset)
113 dsa_alloc_etype_header(skb, BRCM_TAG_LEN);
114
115 brcm_tag = skb->data + offset;
116
117 /* Set the ingress opcode, traffic class, tag enforcement is
118 * deprecated
119 */
120 brcm_tag[0] = (1 << BRCM_OPCODE_SHIFT) |
121 ((queue & BRCM_IG_TC_MASK) << BRCM_IG_TC_SHIFT);
122 brcm_tag[1] = 0;
123 port_mask = dsa_xmit_port_mask(skb, dev);
124 brcm_tag[2] = (port_mask >> 8) & BRCM_IG_DSTMAP2_MASK;
125 brcm_tag[3] = port_mask & BRCM_IG_DSTMAP1_MASK;
126
127 /* Now tell the conduit network device about the desired output queue
128 * as well
129 */
130 skb_set_queue_mapping(skb, BRCM_TAG_SET_PORT_QUEUE(dp->index, queue));
131
132 return skb;
133}
134
135/* Frames with this tag have one of these two layouts:
136 * -----------------------------------
137 * | MAC DA | MAC SA | 4b tag | Type | DSA_TAG_PROTO_BRCM
138 * -----------------------------------
139 * -----------------------------------
140 * | 4b tag | MAC DA | MAC SA | Type | DSA_TAG_PROTO_BRCM_PREPEND
141 * -----------------------------------
142 * In both cases, at receive time, skb->data points 2 bytes before the actual
143 * Ethernet type field and we have an offset of 4bytes between where skb->data
144 * and where the payload starts. So the same low-level receive function can be
145 * used.
146 */
147static struct sk_buff *brcm_tag_rcv_ll(struct sk_buff *skb,
148 struct net_device *dev,
149 unsigned int offset)
150{
151 int source_port;
152 u8 *brcm_tag;
153
154 if (unlikely(!pskb_may_pull(skb, BRCM_TAG_LEN)))
155 return NULL;
156
157 brcm_tag = skb->data - offset;
158
159 /* The opcode should never be different than 0b000 */
160 if (unlikely((brcm_tag[0] >> BRCM_OPCODE_SHIFT) & BRCM_OPCODE_MASK))
161 return NULL;
162
163 /* We should never see a reserved reason code without knowing how to
164 * handle it
165 */
166 if (unlikely(brcm_tag[2] & BRCM_EG_RC_RSVD))
167 return NULL;
168
169 /* Locate which port this is coming from */
170 source_port = brcm_tag[3] & BRCM_EG_PID_MASK;
171
172 skb->dev = dsa_conduit_find_user(dev, 0, source_port);
173 if (!skb->dev)
174 return NULL;
175
176 /* Remove Broadcom tag and update checksum */
177 skb_pull_rcsum(skb, BRCM_TAG_LEN);
178
179 if (likely(!is_link_local_ether_addr(eth_hdr(skb)->h_dest)))
180 dsa_default_offload_fwd_mark(skb);
181
182 return skb;
183}
184#endif
185
186#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM)
187static struct sk_buff *brcm_tag_xmit(struct sk_buff *skb,
188 struct net_device *dev)
189{
190 /* Build the tag after the MAC Source Address */
191 return brcm_tag_xmit_ll(skb, dev, 2 * ETH_ALEN);
192}
193
194
195static struct sk_buff *brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev)
196{
197 struct sk_buff *nskb;
198
199 /* skb->data points to the EtherType, the tag is right before it */
200 nskb = brcm_tag_rcv_ll(skb, dev, 2);
201 if (!nskb)
202 return nskb;
203
204 dsa_strip_etype_header(skb, BRCM_TAG_LEN);
205
206 return nskb;
207}
208
209static const struct dsa_device_ops brcm_netdev_ops = {
210 .name = BRCM_NAME,
211 .proto = DSA_TAG_PROTO_BRCM,
212 .xmit = brcm_tag_xmit,
213 .rcv = brcm_tag_rcv,
214 .needed_headroom = BRCM_TAG_LEN,
215};
216
217DSA_TAG_DRIVER(brcm_netdev_ops);
218MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM, BRCM_NAME);
219#endif
220
221#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY) || \
222 IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS)
223static struct sk_buff *brcm_leg_tag_rcv(struct sk_buff *skb,
224 struct net_device *dev)
225{
226 int len = BRCM_LEG_TAG_LEN;
227 int source_port;
228 __be16 *proto;
229 u8 *brcm_tag;
230
231 if (unlikely(!pskb_may_pull(skb, BRCM_LEG_TAG_LEN + VLAN_HLEN)))
232 return NULL;
233
234 brcm_tag = dsa_etype_header_pos_rx(skb);
235 proto = (__be16 *)(brcm_tag + BRCM_LEG_TAG_LEN);
236
237 source_port = brcm_tag[5] & BRCM_LEG_PORT_ID;
238
239 skb->dev = dsa_conduit_find_user(dev, 0, source_port);
240 if (!skb->dev)
241 return NULL;
242
243 /* The internal switch in BCM63XX SoCs always tags on egress on the CPU
244 * port. We use VID 0 internally for untagged traffic, so strip the tag
245 * if the TCI field is all 0, and keep it otherwise to also retain
246 * e.g. 802.1p tagged packets.
247 */
248 if (proto[0] == htons(ETH_P_8021Q) && proto[1] == 0)
249 len += VLAN_HLEN;
250
251 /* Remove Broadcom tag and update checksum */
252 skb_pull_rcsum(skb, len);
253
254 if (likely(!is_link_local_ether_addr(eth_hdr(skb)->h_dest)))
255 dsa_default_offload_fwd_mark(skb);
256
257 dsa_strip_etype_header(skb, len);
258
259 return skb;
260}
261#endif /* CONFIG_NET_DSA_TAG_BRCM_LEGACY || CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS */
262
263#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY)
264static struct sk_buff *brcm_leg_tag_xmit(struct sk_buff *skb,
265 struct net_device *dev)
266{
267 struct dsa_port *dp = dsa_user_to_port(dev);
268 u8 *brcm_tag;
269
270 /* The Ethernet switch we are interfaced with needs packets to be at
271 * least 64 bytes (including FCS) otherwise they will be discarded when
272 * they enter the switch port logic. When Broadcom tags are enabled, we
273 * need to make sure that packets are at least 70 bytes
274 * (including FCS and tag) because the length verification is done after
275 * the Broadcom tag is stripped off the ingress packet.
276 *
277 * Let dsa_user_xmit() free the SKB
278 */
279 if (__skb_put_padto(skb, ETH_ZLEN + BRCM_LEG_TAG_LEN, false))
280 return NULL;
281
282 skb_push(skb, BRCM_LEG_TAG_LEN);
283
284 dsa_alloc_etype_header(skb, BRCM_LEG_TAG_LEN);
285
286 brcm_tag = skb->data + 2 * ETH_ALEN;
287
288 /* Broadcom tag type */
289 brcm_tag[0] = BRCM_LEG_TYPE_HI;
290 brcm_tag[1] = BRCM_LEG_TYPE_LO;
291
292 /* Broadcom tag value */
293 brcm_tag[2] = BRCM_LEG_EGRESS;
294 brcm_tag[3] = 0;
295 brcm_tag[4] = 0;
296 brcm_tag[5] = dp->index & BRCM_LEG_PORT_ID;
297
298 return skb;
299}
300
301static const struct dsa_device_ops brcm_legacy_netdev_ops = {
302 .name = BRCM_LEGACY_NAME,
303 .proto = DSA_TAG_PROTO_BRCM_LEGACY,
304 .xmit = brcm_leg_tag_xmit,
305 .rcv = brcm_leg_tag_rcv,
306 .needed_headroom = BRCM_LEG_TAG_LEN,
307};
308
309DSA_TAG_DRIVER(brcm_legacy_netdev_ops);
310MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_LEGACY, BRCM_LEGACY_NAME);
311#endif /* CONFIG_NET_DSA_TAG_BRCM_LEGACY */
312
313#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS)
314static struct sk_buff *brcm_leg_fcs_tag_xmit(struct sk_buff *skb,
315 struct net_device *dev)
316{
317 struct dsa_port *dp = dsa_user_to_port(dev);
318 unsigned int fcs_len;
319 __le32 fcs_val;
320 u8 *brcm_tag;
321
322 /* The Ethernet switch we are interfaced with needs packets to be at
323 * least 64 bytes (including FCS) otherwise they will be discarded when
324 * they enter the switch port logic. When Broadcom tags are enabled, we
325 * need to make sure that packets are at least 70 bytes (including FCS
326 * and tag) because the length verification is done after the Broadcom
327 * tag is stripped off the ingress packet.
328 *
329 * Let dsa_user_xmit() free the SKB.
330 */
331 if (__skb_put_padto(skb, ETH_ZLEN + BRCM_LEG_TAG_LEN, false))
332 return NULL;
333
334 fcs_len = skb->len;
335 fcs_val = cpu_to_le32(crc32_le(~0, skb->data, fcs_len) ^ ~0);
336
337 skb_push(skb, BRCM_LEG_TAG_LEN);
338
339 dsa_alloc_etype_header(skb, BRCM_LEG_TAG_LEN);
340
341 brcm_tag = skb->data + 2 * ETH_ALEN;
342
343 /* Broadcom tag type */
344 brcm_tag[0] = BRCM_LEG_TYPE_HI;
345 brcm_tag[1] = BRCM_LEG_TYPE_LO;
346
347 /* Broadcom tag value */
348 brcm_tag[2] = BRCM_LEG_EGRESS | BRCM_LEG_LEN_HI(fcs_len);
349 brcm_tag[3] = BRCM_LEG_LEN_LO(fcs_len);
350 brcm_tag[4] = 0;
351 brcm_tag[5] = dp->index & BRCM_LEG_PORT_ID;
352
353 /* Original FCS value */
354 if (__skb_pad(skb, ETH_FCS_LEN, false))
355 return NULL;
356 skb_put_data(skb, &fcs_val, ETH_FCS_LEN);
357
358 return skb;
359}
360
361static const struct dsa_device_ops brcm_legacy_fcs_netdev_ops = {
362 .name = BRCM_LEGACY_FCS_NAME,
363 .proto = DSA_TAG_PROTO_BRCM_LEGACY_FCS,
364 .xmit = brcm_leg_fcs_tag_xmit,
365 .rcv = brcm_leg_tag_rcv,
366 .needed_headroom = BRCM_LEG_TAG_LEN,
367};
368
369DSA_TAG_DRIVER(brcm_legacy_fcs_netdev_ops);
370MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_LEGACY_FCS, BRCM_LEGACY_FCS_NAME);
371#endif /* CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS */
372
373#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
374static struct sk_buff *brcm_tag_xmit_prepend(struct sk_buff *skb,
375 struct net_device *dev)
376{
377 /* tag is prepended to the packet */
378 return brcm_tag_xmit_ll(skb, dev, 0);
379}
380
381static struct sk_buff *brcm_tag_rcv_prepend(struct sk_buff *skb,
382 struct net_device *dev)
383{
384 /* tag is prepended to the packet */
385 return brcm_tag_rcv_ll(skb, dev, ETH_HLEN);
386}
387
388static const struct dsa_device_ops brcm_prepend_netdev_ops = {
389 .name = BRCM_PREPEND_NAME,
390 .proto = DSA_TAG_PROTO_BRCM_PREPEND,
391 .xmit = brcm_tag_xmit_prepend,
392 .rcv = brcm_tag_rcv_prepend,
393 .needed_headroom = BRCM_TAG_LEN,
394};
395
396DSA_TAG_DRIVER(brcm_prepend_netdev_ops);
397MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_PREPEND, BRCM_PREPEND_NAME);
398#endif
399
400static struct dsa_tag_driver *dsa_tag_driver_array[] = {
401#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM)
402 &DSA_TAG_DRIVER_NAME(brcm_netdev_ops),
403#endif
404#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY)
405 &DSA_TAG_DRIVER_NAME(brcm_legacy_netdev_ops),
406#endif
407#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS)
408 &DSA_TAG_DRIVER_NAME(brcm_legacy_fcs_netdev_ops),
409#endif
410#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
411 &DSA_TAG_DRIVER_NAME(brcm_prepend_netdev_ops),
412#endif
413};
414
415module_dsa_tag_drivers(dsa_tag_driver_array);
416
417MODULE_DESCRIPTION("DSA tag driver for Broadcom switches using in-frame headers");
418MODULE_LICENSE("GPL");