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