xref: /linux/net/dsa/tag_brcm.c (revision 87320be9f0d24fce67631b7eef919f0b79c3e45c)
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 	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 	 * Free the SKB on error.
106 	 */
107 	if (skb_put_padto(skb, ETH_ZLEN + BRCM_TAG_LEN))
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  */
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 		kfree_skb(skb);
156 		return NULL;
157 	}
158 
159 	brcm_tag = skb->data - offset;
160 
161 	/* The opcode should never be different than 0b000 */
162 	if (unlikely((brcm_tag[0] >> BRCM_OPCODE_SHIFT) & BRCM_OPCODE_MASK)) {
163 		kfree_skb(skb);
164 		return NULL;
165 	}
166 
167 	/* We should never see a reserved reason code without knowing how to
168 	 * handle it
169 	 */
170 	if (unlikely(brcm_tag[2] & BRCM_EG_RC_RSVD)) {
171 		kfree_skb(skb);
172 		return NULL;
173 	}
174 
175 	/* Locate which port this is coming from */
176 	source_port = brcm_tag[3] & BRCM_EG_PID_MASK;
177 
178 	skb->dev = dsa_conduit_find_user(dev, 0, source_port);
179 	if (!skb->dev) {
180 		kfree_skb(skb);
181 		return NULL;
182 	}
183 
184 	/* Remove Broadcom tag and update checksum */
185 	skb_pull_rcsum(skb, BRCM_TAG_LEN);
186 
187 	if (likely(!is_link_local_ether_addr(eth_hdr(skb)->h_dest)))
188 		dsa_default_offload_fwd_mark(skb);
189 
190 	return skb;
191 }
192 #endif
193 
194 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM)
brcm_tag_xmit(struct sk_buff * skb,struct net_device * dev)195 static struct sk_buff *brcm_tag_xmit(struct sk_buff *skb,
196 				     struct net_device *dev)
197 {
198 	/* Build the tag after the MAC Source Address */
199 	return brcm_tag_xmit_ll(skb, dev, 2 * ETH_ALEN);
200 }
201 
202 
brcm_tag_rcv(struct sk_buff * skb,struct net_device * dev)203 static struct sk_buff *brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev)
204 {
205 	struct sk_buff *nskb;
206 
207 	/* skb->data points to the EtherType, the tag is right before it */
208 	nskb = brcm_tag_rcv_ll(skb, dev, 2);
209 	if (!nskb)
210 		return nskb;
211 
212 	dsa_strip_etype_header(skb, BRCM_TAG_LEN);
213 
214 	return nskb;
215 }
216 
217 static const struct dsa_device_ops brcm_netdev_ops = {
218 	.name	= BRCM_NAME,
219 	.proto	= DSA_TAG_PROTO_BRCM,
220 	.xmit	= brcm_tag_xmit,
221 	.rcv	= brcm_tag_rcv,
222 	.needed_headroom = BRCM_TAG_LEN,
223 };
224 
225 DSA_TAG_DRIVER(brcm_netdev_ops);
226 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM, BRCM_NAME);
227 #endif
228 
229 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY) || \
230 	IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS)
brcm_leg_tag_rcv(struct sk_buff * skb,struct net_device * dev)231 static struct sk_buff *brcm_leg_tag_rcv(struct sk_buff *skb,
232 					struct net_device *dev)
233 {
234 	int len = BRCM_LEG_TAG_LEN;
235 	int source_port;
236 	__be16 *proto;
237 	u8 *brcm_tag;
238 
239 	if (unlikely(!pskb_may_pull(skb, BRCM_LEG_TAG_LEN + VLAN_HLEN))) {
240 		kfree_skb(skb);
241 		return NULL;
242 	}
243 
244 	brcm_tag = dsa_etype_header_pos_rx(skb);
245 	proto = (__be16 *)(brcm_tag + BRCM_LEG_TAG_LEN);
246 
247 	source_port = brcm_tag[5] & BRCM_LEG_PORT_ID;
248 
249 	skb->dev = dsa_conduit_find_user(dev, 0, source_port);
250 	if (!skb->dev) {
251 		kfree_skb(skb);
252 		return NULL;
253 	}
254 
255 	/* The internal switch in BCM63XX SoCs always tags on egress on the CPU
256 	 * port. We use VID 0 internally for untagged traffic, so strip the tag
257 	 * if the TCI field is all 0, and keep it otherwise to also retain
258 	 * e.g. 802.1p tagged packets.
259 	 */
260 	if (proto[0] == htons(ETH_P_8021Q) && proto[1] == 0)
261 		len += VLAN_HLEN;
262 
263 	/* Remove Broadcom tag and update checksum */
264 	skb_pull_rcsum(skb, len);
265 
266 	if (likely(!is_link_local_ether_addr(eth_hdr(skb)->h_dest)))
267 		dsa_default_offload_fwd_mark(skb);
268 
269 	dsa_strip_etype_header(skb, len);
270 
271 	return skb;
272 }
273 #endif /* CONFIG_NET_DSA_TAG_BRCM_LEGACY || CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS */
274 
275 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY)
brcm_leg_tag_xmit(struct sk_buff * skb,struct net_device * dev)276 static struct sk_buff *brcm_leg_tag_xmit(struct sk_buff *skb,
277 					 struct net_device *dev)
278 {
279 	struct dsa_port *dp = dsa_user_to_port(dev);
280 	u8 *brcm_tag;
281 
282 	/* The Ethernet switch we are interfaced with needs packets to be at
283 	 * least 64 bytes (including FCS) otherwise they will be discarded when
284 	 * they enter the switch port logic. When Broadcom tags are enabled, we
285 	 * need to make sure that packets are at least 70 bytes
286 	 * (including FCS and tag) because the length verification is done after
287 	 * the Broadcom tag is stripped off the ingress packet.
288 	 */
289 	if (skb_put_padto(skb, ETH_ZLEN + BRCM_LEG_TAG_LEN))
290 		return NULL;
291 
292 	skb_push(skb, BRCM_LEG_TAG_LEN);
293 
294 	dsa_alloc_etype_header(skb, BRCM_LEG_TAG_LEN);
295 
296 	brcm_tag = skb->data + 2 * ETH_ALEN;
297 
298 	/* Broadcom tag type */
299 	brcm_tag[0] = BRCM_LEG_TYPE_HI;
300 	brcm_tag[1] = BRCM_LEG_TYPE_LO;
301 
302 	/* Broadcom tag value */
303 	brcm_tag[2] = BRCM_LEG_EGRESS;
304 	brcm_tag[3] = 0;
305 	brcm_tag[4] = 0;
306 	brcm_tag[5] = dp->index & BRCM_LEG_PORT_ID;
307 
308 	return skb;
309 }
310 
311 static const struct dsa_device_ops brcm_legacy_netdev_ops = {
312 	.name = BRCM_LEGACY_NAME,
313 	.proto = DSA_TAG_PROTO_BRCM_LEGACY,
314 	.xmit = brcm_leg_tag_xmit,
315 	.rcv = brcm_leg_tag_rcv,
316 	.needed_headroom = BRCM_LEG_TAG_LEN,
317 };
318 
319 DSA_TAG_DRIVER(brcm_legacy_netdev_ops);
320 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_LEGACY, BRCM_LEGACY_NAME);
321 #endif /* CONFIG_NET_DSA_TAG_BRCM_LEGACY */
322 
323 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS)
brcm_leg_fcs_tag_xmit(struct sk_buff * skb,struct net_device * dev)324 static struct sk_buff *brcm_leg_fcs_tag_xmit(struct sk_buff *skb,
325 					     struct net_device *dev)
326 {
327 	struct dsa_port *dp = dsa_user_to_port(dev);
328 	unsigned int fcs_len;
329 	__le32 fcs_val;
330 	u8 *brcm_tag;
331 
332 	/* The Ethernet switch we are interfaced with needs packets to be at
333 	 * least 64 bytes (including FCS) otherwise they will be discarded when
334 	 * they enter the switch port logic. When Broadcom tags are enabled, we
335 	 * need to make sure that packets are at least 70 bytes (including FCS
336 	 * and tag) because the length verification is done after the Broadcom
337 	 * tag is stripped off the ingress packet.
338 	 */
339 	if (skb_put_padto(skb, ETH_ZLEN + BRCM_LEG_TAG_LEN))
340 		return NULL;
341 
342 	fcs_len = skb->len;
343 	fcs_val = cpu_to_le32(crc32_le(~0, skb->data, fcs_len) ^ ~0);
344 
345 	skb_push(skb, BRCM_LEG_TAG_LEN);
346 
347 	dsa_alloc_etype_header(skb, BRCM_LEG_TAG_LEN);
348 
349 	brcm_tag = skb->data + 2 * ETH_ALEN;
350 
351 	/* Broadcom tag type */
352 	brcm_tag[0] = BRCM_LEG_TYPE_HI;
353 	brcm_tag[1] = BRCM_LEG_TYPE_LO;
354 
355 	/* Broadcom tag value */
356 	brcm_tag[2] = BRCM_LEG_EGRESS | BRCM_LEG_LEN_HI(fcs_len);
357 	brcm_tag[3] = BRCM_LEG_LEN_LO(fcs_len);
358 	brcm_tag[4] = 0;
359 	brcm_tag[5] = dp->index & BRCM_LEG_PORT_ID;
360 
361 	/* Original FCS value */
362 	if (skb_pad(skb, ETH_FCS_LEN))
363 		return NULL;
364 
365 	skb_put_data(skb, &fcs_val, ETH_FCS_LEN);
366 
367 	return skb;
368 }
369 
370 static const struct dsa_device_ops brcm_legacy_fcs_netdev_ops = {
371 	.name = BRCM_LEGACY_FCS_NAME,
372 	.proto = DSA_TAG_PROTO_BRCM_LEGACY_FCS,
373 	.xmit = brcm_leg_fcs_tag_xmit,
374 	.rcv = brcm_leg_tag_rcv,
375 	.needed_headroom = BRCM_LEG_TAG_LEN,
376 };
377 
378 DSA_TAG_DRIVER(brcm_legacy_fcs_netdev_ops);
379 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_LEGACY_FCS, BRCM_LEGACY_FCS_NAME);
380 #endif /* CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS */
381 
382 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
brcm_tag_xmit_prepend(struct sk_buff * skb,struct net_device * dev)383 static struct sk_buff *brcm_tag_xmit_prepend(struct sk_buff *skb,
384 					     struct net_device *dev)
385 {
386 	/* tag is prepended to the packet */
387 	return brcm_tag_xmit_ll(skb, dev, 0);
388 }
389 
brcm_tag_rcv_prepend(struct sk_buff * skb,struct net_device * dev)390 static struct sk_buff *brcm_tag_rcv_prepend(struct sk_buff *skb,
391 					    struct net_device *dev)
392 {
393 	/* tag is prepended to the packet */
394 	return brcm_tag_rcv_ll(skb, dev, ETH_HLEN);
395 }
396 
397 static const struct dsa_device_ops brcm_prepend_netdev_ops = {
398 	.name	= BRCM_PREPEND_NAME,
399 	.proto	= DSA_TAG_PROTO_BRCM_PREPEND,
400 	.xmit	= brcm_tag_xmit_prepend,
401 	.rcv	= brcm_tag_rcv_prepend,
402 	.needed_headroom = BRCM_TAG_LEN,
403 };
404 
405 DSA_TAG_DRIVER(brcm_prepend_netdev_ops);
406 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_PREPEND, BRCM_PREPEND_NAME);
407 #endif
408 
409 static struct dsa_tag_driver *dsa_tag_driver_array[] =	{
410 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM)
411 	&DSA_TAG_DRIVER_NAME(brcm_netdev_ops),
412 #endif
413 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY)
414 	&DSA_TAG_DRIVER_NAME(brcm_legacy_netdev_ops),
415 #endif
416 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY_FCS)
417 	&DSA_TAG_DRIVER_NAME(brcm_legacy_fcs_netdev_ops),
418 #endif
419 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
420 	&DSA_TAG_DRIVER_NAME(brcm_prepend_netdev_ops),
421 #endif
422 };
423 
424 module_dsa_tag_drivers(dsa_tag_driver_array);
425 
426 MODULE_DESCRIPTION("DSA tag driver for Broadcom switches using in-frame headers");
427 MODULE_LICENSE("GPL");
428