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
brcm_tag_xmit_ll(struct sk_buff * skb,struct net_device * dev,unsigned int offset)89 static 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 u8 *brcm_tag;
96
97 /* The Ethernet switch we are interfaced with needs packets to be at
98 * least 64 bytes (including FCS) otherwise they will be discarded when
99 * they enter the switch port logic. When Broadcom tags are enabled, we
100 * need to make sure that packets are at least 68 bytes
101 * (including FCS and tag) because the length verification is done after
102 * the Broadcom tag is stripped off the ingress packet.
103 *
104 * Let dsa_user_xmit() free the SKB
105 */
106 if (__skb_put_padto(skb, ETH_ZLEN + BRCM_TAG_LEN, false))
107 return NULL;
108
109 skb_push(skb, BRCM_TAG_LEN);
110
111 if (offset)
112 dsa_alloc_etype_header(skb, BRCM_TAG_LEN);
113
114 brcm_tag = skb->data + offset;
115
116 /* Set the ingress opcode, traffic class, tag enforcement is
117 * deprecated
118 */
119 brcm_tag[0] = (1 << BRCM_OPCODE_SHIFT) |
120 ((queue & BRCM_IG_TC_MASK) << BRCM_IG_TC_SHIFT);
121 brcm_tag[1] = 0;
122 brcm_tag[2] = 0;
123 if (dp->index == 8)
124 brcm_tag[2] = BRCM_IG_DSTMAP2_MASK;
125 brcm_tag[3] = (1 << dp->index) & 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 */
brcm_tag_rcv_ll(struct sk_buff * skb,struct net_device * dev,unsigned int offset)147 static 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 dsa_default_offload_fwd_mark(skb);
180
181 return skb;
182 }
183 #endif
184
185 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM)
brcm_tag_xmit(struct sk_buff * skb,struct net_device * dev)186 static struct sk_buff *brcm_tag_xmit(struct sk_buff *skb,
187 struct net_device *dev)
188 {
189 /* Build the tag after the MAC Source Address */
190 return brcm_tag_xmit_ll(skb, dev, 2 * ETH_ALEN);
191 }
192
193
brcm_tag_rcv(struct sk_buff * skb,struct net_device * dev)194 static struct sk_buff *brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev)
195 {
196 struct sk_buff *nskb;
197
198 /* skb->data points to the EtherType, the tag is right before it */
199 nskb = brcm_tag_rcv_ll(skb, dev, 2);
200 if (!nskb)
201 return nskb;
202
203 dsa_strip_etype_header(skb, BRCM_TAG_LEN);
204
205 return nskb;
206 }
207
208 static const struct dsa_device_ops brcm_netdev_ops = {
209 .name = BRCM_NAME,
210 .proto = DSA_TAG_PROTO_BRCM,
211 .xmit = brcm_tag_xmit,
212 .rcv = brcm_tag_rcv,
213 .needed_headroom = BRCM_TAG_LEN,
214 };
215
216 DSA_TAG_DRIVER(brcm_netdev_ops);
217 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM, BRCM_NAME);
218 #endif
219
220 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY) || \
221 IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS)
brcm_leg_tag_rcv(struct sk_buff * skb,struct net_device * dev)222 static struct sk_buff *brcm_leg_tag_rcv(struct sk_buff *skb,
223 struct net_device *dev)
224 {
225 int len = BRCM_LEG_TAG_LEN;
226 int source_port;
227 u8 *brcm_tag;
228
229 if (unlikely(!pskb_may_pull(skb, BRCM_LEG_TAG_LEN + VLAN_HLEN)))
230 return NULL;
231
232 brcm_tag = dsa_etype_header_pos_rx(skb);
233
234 source_port = brcm_tag[5] & BRCM_LEG_PORT_ID;
235
236 skb->dev = dsa_conduit_find_user(dev, 0, source_port);
237 if (!skb->dev)
238 return NULL;
239
240 /* VLAN tag is added by BCM63xx internal switch */
241 if (netdev_uses_dsa(skb->dev))
242 len += VLAN_HLEN;
243
244 /* Remove Broadcom tag and update checksum */
245 skb_pull_rcsum(skb, len);
246
247 dsa_default_offload_fwd_mark(skb);
248
249 dsa_strip_etype_header(skb, len);
250
251 return skb;
252 }
253 #endif /* CONFIG_NET_DSA_TAG_BRCM_LEGACY || CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS */
254
255 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY)
brcm_leg_tag_xmit(struct sk_buff * skb,struct net_device * dev)256 static struct sk_buff *brcm_leg_tag_xmit(struct sk_buff *skb,
257 struct net_device *dev)
258 {
259 struct dsa_port *dp = dsa_user_to_port(dev);
260 u8 *brcm_tag;
261
262 /* The Ethernet switch we are interfaced with needs packets to be at
263 * least 64 bytes (including FCS) otherwise they will be discarded when
264 * they enter the switch port logic. When Broadcom tags are enabled, we
265 * need to make sure that packets are at least 70 bytes
266 * (including FCS and tag) because the length verification is done after
267 * the Broadcom tag is stripped off the ingress packet.
268 *
269 * Let dsa_user_xmit() free the SKB
270 */
271 if (__skb_put_padto(skb, ETH_ZLEN + BRCM_LEG_TAG_LEN, false))
272 return NULL;
273
274 skb_push(skb, BRCM_LEG_TAG_LEN);
275
276 dsa_alloc_etype_header(skb, BRCM_LEG_TAG_LEN);
277
278 brcm_tag = skb->data + 2 * ETH_ALEN;
279
280 /* Broadcom tag type */
281 brcm_tag[0] = BRCM_LEG_TYPE_HI;
282 brcm_tag[1] = BRCM_LEG_TYPE_LO;
283
284 /* Broadcom tag value */
285 brcm_tag[2] = BRCM_LEG_EGRESS;
286 brcm_tag[3] = 0;
287 brcm_tag[4] = 0;
288 brcm_tag[5] = dp->index & BRCM_LEG_PORT_ID;
289
290 return skb;
291 }
292
293 static const struct dsa_device_ops brcm_legacy_netdev_ops = {
294 .name = BRCM_LEGACY_NAME,
295 .proto = DSA_TAG_PROTO_BRCM_LEGACY,
296 .xmit = brcm_leg_tag_xmit,
297 .rcv = brcm_leg_tag_rcv,
298 .needed_headroom = BRCM_LEG_TAG_LEN,
299 };
300
301 DSA_TAG_DRIVER(brcm_legacy_netdev_ops);
302 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_LEGACY, BRCM_LEGACY_NAME);
303 #endif /* CONFIG_NET_DSA_TAG_BRCM_LEGACY */
304
305 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS)
brcm_leg_fcs_tag_xmit(struct sk_buff * skb,struct net_device * dev)306 static struct sk_buff *brcm_leg_fcs_tag_xmit(struct sk_buff *skb,
307 struct net_device *dev)
308 {
309 struct dsa_port *dp = dsa_user_to_port(dev);
310 unsigned int fcs_len;
311 __le32 fcs_val;
312 u8 *brcm_tag;
313
314 /* The Ethernet switch we are interfaced with needs packets to be at
315 * least 64 bytes (including FCS) otherwise they will be discarded when
316 * they enter the switch port logic. When Broadcom tags are enabled, we
317 * need to make sure that packets are at least 70 bytes (including FCS
318 * and tag) because the length verification is done after the Broadcom
319 * tag is stripped off the ingress packet.
320 *
321 * Let dsa_user_xmit() free the SKB.
322 */
323 if (__skb_put_padto(skb, ETH_ZLEN + BRCM_LEG_TAG_LEN, false))
324 return NULL;
325
326 fcs_len = skb->len;
327 fcs_val = cpu_to_le32(crc32_le(~0, skb->data, fcs_len) ^ ~0);
328
329 skb_push(skb, BRCM_LEG_TAG_LEN);
330
331 dsa_alloc_etype_header(skb, BRCM_LEG_TAG_LEN);
332
333 brcm_tag = skb->data + 2 * ETH_ALEN;
334
335 /* Broadcom tag type */
336 brcm_tag[0] = BRCM_LEG_TYPE_HI;
337 brcm_tag[1] = BRCM_LEG_TYPE_LO;
338
339 /* Broadcom tag value */
340 brcm_tag[2] = BRCM_LEG_EGRESS | BRCM_LEG_LEN_HI(fcs_len);
341 brcm_tag[3] = BRCM_LEG_LEN_LO(fcs_len);
342 brcm_tag[4] = 0;
343 brcm_tag[5] = dp->index & BRCM_LEG_PORT_ID;
344
345 /* Original FCS value */
346 if (__skb_pad(skb, ETH_FCS_LEN, false))
347 return NULL;
348 skb_put_data(skb, &fcs_val, ETH_FCS_LEN);
349
350 return skb;
351 }
352
353 static const struct dsa_device_ops brcm_legacy_fcs_netdev_ops = {
354 .name = BRCM_LEGACY_FCS_NAME,
355 .proto = DSA_TAG_PROTO_BRCM_LEGACY_FCS,
356 .xmit = brcm_leg_fcs_tag_xmit,
357 .rcv = brcm_leg_tag_rcv,
358 .needed_headroom = BRCM_LEG_TAG_LEN,
359 };
360
361 DSA_TAG_DRIVER(brcm_legacy_fcs_netdev_ops);
362 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_LEGACY_FCS, BRCM_LEGACY_FCS_NAME);
363 #endif /* CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS */
364
365 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
brcm_tag_xmit_prepend(struct sk_buff * skb,struct net_device * dev)366 static struct sk_buff *brcm_tag_xmit_prepend(struct sk_buff *skb,
367 struct net_device *dev)
368 {
369 /* tag is prepended to the packet */
370 return brcm_tag_xmit_ll(skb, dev, 0);
371 }
372
brcm_tag_rcv_prepend(struct sk_buff * skb,struct net_device * dev)373 static struct sk_buff *brcm_tag_rcv_prepend(struct sk_buff *skb,
374 struct net_device *dev)
375 {
376 /* tag is prepended to the packet */
377 return brcm_tag_rcv_ll(skb, dev, ETH_HLEN);
378 }
379
380 static const struct dsa_device_ops brcm_prepend_netdev_ops = {
381 .name = BRCM_PREPEND_NAME,
382 .proto = DSA_TAG_PROTO_BRCM_PREPEND,
383 .xmit = brcm_tag_xmit_prepend,
384 .rcv = brcm_tag_rcv_prepend,
385 .needed_headroom = BRCM_TAG_LEN,
386 };
387
388 DSA_TAG_DRIVER(brcm_prepend_netdev_ops);
389 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_PREPEND, BRCM_PREPEND_NAME);
390 #endif
391
392 static struct dsa_tag_driver *dsa_tag_driver_array[] = {
393 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM)
394 &DSA_TAG_DRIVER_NAME(brcm_netdev_ops),
395 #endif
396 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY)
397 &DSA_TAG_DRIVER_NAME(brcm_legacy_netdev_ops),
398 #endif
399 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS)
400 &DSA_TAG_DRIVER_NAME(brcm_legacy_fcs_netdev_ops),
401 #endif
402 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
403 &DSA_TAG_DRIVER_NAME(brcm_prepend_netdev_ops),
404 #endif
405 };
406
407 module_dsa_tag_drivers(dsa_tag_driver_array);
408
409 MODULE_DESCRIPTION("DSA tag driver for Broadcom switches using in-frame headers");
410 MODULE_LICENSE("GPL");
411