xref: /openbmc/linux/net/dsa/tag_brcm.c (revision 5626af8f)
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/list.h>
11 #include <linux/slab.h>
12 
13 #include "tag.h"
14 
15 #define BRCM_NAME		"brcm"
16 #define BRCM_LEGACY_NAME	"brcm-legacy"
17 #define BRCM_PREPEND_NAME	"brcm-prepend"
18 
19 /* Legacy Broadcom tag (6 bytes) */
20 #define BRCM_LEG_TAG_LEN	6
21 
22 /* Type fields */
23 /* 1st byte in the tag */
24 #define BRCM_LEG_TYPE_HI	0x88
25 /* 2nd byte in the tag */
26 #define BRCM_LEG_TYPE_LO	0x74
27 
28 /* Tag fields */
29 /* 3rd byte in the tag */
30 #define BRCM_LEG_UNICAST	(0 << 5)
31 #define BRCM_LEG_MULTICAST	(1 << 5)
32 #define BRCM_LEG_EGRESS		(2 << 5)
33 #define BRCM_LEG_INGRESS	(3 << 5)
34 
35 /* 6th byte in the tag */
36 #define BRCM_LEG_PORT_ID	(0xf)
37 
38 /* Newer Broadcom tag (4 bytes) */
39 #define BRCM_TAG_LEN	4
40 
41 /* Tag is constructed and deconstructed using byte by byte access
42  * because the tag is placed after the MAC Source Address, which does
43  * not make it 4-bytes aligned, so this might cause unaligned accesses
44  * on most systems where this is used.
45  */
46 
47 /* Ingress and egress opcodes */
48 #define BRCM_OPCODE_SHIFT	5
49 #define BRCM_OPCODE_MASK	0x7
50 
51 /* Ingress fields */
52 /* 1st byte in the tag */
53 #define BRCM_IG_TC_SHIFT	2
54 #define BRCM_IG_TC_MASK		0x7
55 /* 2nd byte in the tag */
56 #define BRCM_IG_TE_MASK		0x3
57 #define BRCM_IG_TS_SHIFT	7
58 /* 3rd byte in the tag */
59 #define BRCM_IG_DSTMAP2_MASK	1
60 #define BRCM_IG_DSTMAP1_MASK	0xff
61 
62 /* Egress fields */
63 
64 /* 2nd byte in the tag */
65 #define BRCM_EG_CID_MASK	0xff
66 
67 /* 3rd byte in the tag */
68 #define BRCM_EG_RC_MASK		0xff
69 #define  BRCM_EG_RC_RSVD	(3 << 6)
70 #define  BRCM_EG_RC_EXCEPTION	(1 << 5)
71 #define  BRCM_EG_RC_PROT_SNOOP	(1 << 4)
72 #define  BRCM_EG_RC_PROT_TERM	(1 << 3)
73 #define  BRCM_EG_RC_SWITCH	(1 << 2)
74 #define  BRCM_EG_RC_MAC_LEARN	(1 << 1)
75 #define  BRCM_EG_RC_MIRROR	(1 << 0)
76 #define BRCM_EG_TC_SHIFT	5
77 #define BRCM_EG_TC_MASK		0x7
78 #define BRCM_EG_PID_MASK	0x1f
79 
80 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM) || \
81 	IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
82 
83 static struct sk_buff *brcm_tag_xmit_ll(struct sk_buff *skb,
84 					struct net_device *dev,
85 					unsigned int offset)
86 {
87 	struct dsa_port *dp = dsa_slave_to_port(dev);
88 	u16 queue = skb_get_queue_mapping(skb);
89 	u8 *brcm_tag;
90 
91 	/* The Ethernet switch we are interfaced with needs packets to be at
92 	 * least 64 bytes (including FCS) otherwise they will be discarded when
93 	 * they enter the switch port logic. When Broadcom tags are enabled, we
94 	 * need to make sure that packets are at least 68 bytes
95 	 * (including FCS and tag) because the length verification is done after
96 	 * the Broadcom tag is stripped off the ingress packet.
97 	 *
98 	 * Let dsa_slave_xmit() free the SKB
99 	 */
100 	if (__skb_put_padto(skb, ETH_ZLEN + BRCM_TAG_LEN, false))
101 		return NULL;
102 
103 	skb_push(skb, BRCM_TAG_LEN);
104 
105 	if (offset)
106 		dsa_alloc_etype_header(skb, BRCM_TAG_LEN);
107 
108 	brcm_tag = skb->data + offset;
109 
110 	/* Set the ingress opcode, traffic class, tag enforcement is
111 	 * deprecated
112 	 */
113 	brcm_tag[0] = (1 << BRCM_OPCODE_SHIFT) |
114 		       ((queue & BRCM_IG_TC_MASK) << BRCM_IG_TC_SHIFT);
115 	brcm_tag[1] = 0;
116 	brcm_tag[2] = 0;
117 	if (dp->index == 8)
118 		brcm_tag[2] = BRCM_IG_DSTMAP2_MASK;
119 	brcm_tag[3] = (1 << dp->index) & BRCM_IG_DSTMAP1_MASK;
120 
121 	/* Now tell the master network device about the desired output queue
122 	 * as well
123 	 */
124 	skb_set_queue_mapping(skb, BRCM_TAG_SET_PORT_QUEUE(dp->index, queue));
125 
126 	return skb;
127 }
128 
129 /* Frames with this tag have one of these two layouts:
130  * -----------------------------------
131  * | MAC DA | MAC SA | 4b tag | Type | DSA_TAG_PROTO_BRCM
132  * -----------------------------------
133  * -----------------------------------
134  * | 4b tag | MAC DA | MAC SA | Type | DSA_TAG_PROTO_BRCM_PREPEND
135  * -----------------------------------
136  * In both cases, at receive time, skb->data points 2 bytes before the actual
137  * Ethernet type field and we have an offset of 4bytes between where skb->data
138  * and where the payload starts. So the same low-level receive function can be
139  * used.
140  */
141 static struct sk_buff *brcm_tag_rcv_ll(struct sk_buff *skb,
142 				       struct net_device *dev,
143 				       unsigned int offset)
144 {
145 	int source_port;
146 	u8 *brcm_tag;
147 
148 	if (unlikely(!pskb_may_pull(skb, BRCM_TAG_LEN)))
149 		return NULL;
150 
151 	brcm_tag = skb->data - offset;
152 
153 	/* The opcode should never be different than 0b000 */
154 	if (unlikely((brcm_tag[0] >> BRCM_OPCODE_SHIFT) & BRCM_OPCODE_MASK))
155 		return NULL;
156 
157 	/* We should never see a reserved reason code without knowing how to
158 	 * handle it
159 	 */
160 	if (unlikely(brcm_tag[2] & BRCM_EG_RC_RSVD))
161 		return NULL;
162 
163 	/* Locate which port this is coming from */
164 	source_port = brcm_tag[3] & BRCM_EG_PID_MASK;
165 
166 	skb->dev = dsa_master_find_slave(dev, 0, source_port);
167 	if (!skb->dev)
168 		return NULL;
169 
170 	/* Remove Broadcom tag and update checksum */
171 	skb_pull_rcsum(skb, BRCM_TAG_LEN);
172 
173 	dsa_default_offload_fwd_mark(skb);
174 
175 	return skb;
176 }
177 #endif
178 
179 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM)
180 static struct sk_buff *brcm_tag_xmit(struct sk_buff *skb,
181 				     struct net_device *dev)
182 {
183 	/* Build the tag after the MAC Source Address */
184 	return brcm_tag_xmit_ll(skb, dev, 2 * ETH_ALEN);
185 }
186 
187 
188 static struct sk_buff *brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev)
189 {
190 	struct sk_buff *nskb;
191 
192 	/* skb->data points to the EtherType, the tag is right before it */
193 	nskb = brcm_tag_rcv_ll(skb, dev, 2);
194 	if (!nskb)
195 		return nskb;
196 
197 	dsa_strip_etype_header(skb, BRCM_TAG_LEN);
198 
199 	return nskb;
200 }
201 
202 static const struct dsa_device_ops brcm_netdev_ops = {
203 	.name	= BRCM_NAME,
204 	.proto	= DSA_TAG_PROTO_BRCM,
205 	.xmit	= brcm_tag_xmit,
206 	.rcv	= brcm_tag_rcv,
207 	.needed_headroom = BRCM_TAG_LEN,
208 };
209 
210 DSA_TAG_DRIVER(brcm_netdev_ops);
211 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM, BRCM_NAME);
212 #endif
213 
214 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY)
215 static struct sk_buff *brcm_leg_tag_xmit(struct sk_buff *skb,
216 					 struct net_device *dev)
217 {
218 	struct dsa_port *dp = dsa_slave_to_port(dev);
219 	u8 *brcm_tag;
220 
221 	/* The Ethernet switch we are interfaced with needs packets to be at
222 	 * least 64 bytes (including FCS) otherwise they will be discarded when
223 	 * they enter the switch port logic. When Broadcom tags are enabled, we
224 	 * need to make sure that packets are at least 70 bytes
225 	 * (including FCS and tag) because the length verification is done after
226 	 * the Broadcom tag is stripped off the ingress packet.
227 	 *
228 	 * Let dsa_slave_xmit() free the SKB
229 	 */
230 	if (__skb_put_padto(skb, ETH_ZLEN + BRCM_LEG_TAG_LEN, false))
231 		return NULL;
232 
233 	skb_push(skb, BRCM_LEG_TAG_LEN);
234 
235 	dsa_alloc_etype_header(skb, BRCM_LEG_TAG_LEN);
236 
237 	brcm_tag = skb->data + 2 * ETH_ALEN;
238 
239 	/* Broadcom tag type */
240 	brcm_tag[0] = BRCM_LEG_TYPE_HI;
241 	brcm_tag[1] = BRCM_LEG_TYPE_LO;
242 
243 	/* Broadcom tag value */
244 	brcm_tag[2] = BRCM_LEG_EGRESS;
245 	brcm_tag[3] = 0;
246 	brcm_tag[4] = 0;
247 	brcm_tag[5] = dp->index & BRCM_LEG_PORT_ID;
248 
249 	return skb;
250 }
251 
252 static struct sk_buff *brcm_leg_tag_rcv(struct sk_buff *skb,
253 					struct net_device *dev)
254 {
255 	int source_port;
256 	u8 *brcm_tag;
257 
258 	if (unlikely(!pskb_may_pull(skb, BRCM_LEG_PORT_ID)))
259 		return NULL;
260 
261 	brcm_tag = dsa_etype_header_pos_rx(skb);
262 
263 	source_port = brcm_tag[5] & BRCM_LEG_PORT_ID;
264 
265 	skb->dev = dsa_master_find_slave(dev, 0, source_port);
266 	if (!skb->dev)
267 		return NULL;
268 
269 	/* Remove Broadcom tag and update checksum */
270 	skb_pull_rcsum(skb, BRCM_LEG_TAG_LEN);
271 
272 	dsa_default_offload_fwd_mark(skb);
273 
274 	dsa_strip_etype_header(skb, BRCM_LEG_TAG_LEN);
275 
276 	return skb;
277 }
278 
279 static const struct dsa_device_ops brcm_legacy_netdev_ops = {
280 	.name = BRCM_LEGACY_NAME,
281 	.proto = DSA_TAG_PROTO_BRCM_LEGACY,
282 	.xmit = brcm_leg_tag_xmit,
283 	.rcv = brcm_leg_tag_rcv,
284 	.needed_headroom = BRCM_LEG_TAG_LEN,
285 };
286 
287 DSA_TAG_DRIVER(brcm_legacy_netdev_ops);
288 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_LEGACY, BRCM_LEGACY_NAME);
289 #endif /* CONFIG_NET_DSA_TAG_BRCM_LEGACY */
290 
291 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
292 static struct sk_buff *brcm_tag_xmit_prepend(struct sk_buff *skb,
293 					     struct net_device *dev)
294 {
295 	/* tag is prepended to the packet */
296 	return brcm_tag_xmit_ll(skb, dev, 0);
297 }
298 
299 static struct sk_buff *brcm_tag_rcv_prepend(struct sk_buff *skb,
300 					    struct net_device *dev)
301 {
302 	/* tag is prepended to the packet */
303 	return brcm_tag_rcv_ll(skb, dev, ETH_HLEN);
304 }
305 
306 static const struct dsa_device_ops brcm_prepend_netdev_ops = {
307 	.name	= BRCM_PREPEND_NAME,
308 	.proto	= DSA_TAG_PROTO_BRCM_PREPEND,
309 	.xmit	= brcm_tag_xmit_prepend,
310 	.rcv	= brcm_tag_rcv_prepend,
311 	.needed_headroom = BRCM_TAG_LEN,
312 };
313 
314 DSA_TAG_DRIVER(brcm_prepend_netdev_ops);
315 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_PREPEND, BRCM_PREPEND_NAME);
316 #endif
317 
318 static struct dsa_tag_driver *dsa_tag_driver_array[] =	{
319 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM)
320 	&DSA_TAG_DRIVER_NAME(brcm_netdev_ops),
321 #endif
322 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_LEGACY)
323 	&DSA_TAG_DRIVER_NAME(brcm_legacy_netdev_ops),
324 #endif
325 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
326 	&DSA_TAG_DRIVER_NAME(brcm_prepend_netdev_ops),
327 #endif
328 };
329 
330 module_dsa_tag_drivers(dsa_tag_driver_array);
331 
332 MODULE_LICENSE("GPL");
333