1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2014 Broadcom Corporation
8 #include <linux/etherdevice.h>
9 #include <linux/list.h>
10 #include <linux/slab.h>
14 /* This tag length is 4 bytes, older ones were 6 bytes, we do not
17 #define BRCM_TAG_LEN 4
19 /* Tag is constructed and desconstructed using byte by byte access
20 * because the tag is placed after the MAC Source Address, which does
21 * not make it 4-bytes aligned, so this might cause unaligned accesses
22 * on most systems where this is used.
25 /* Ingress and egress opcodes */
26 #define BRCM_OPCODE_SHIFT 5
27 #define BRCM_OPCODE_MASK 0x7
30 /* 1st byte in the tag */
31 #define BRCM_IG_TC_SHIFT 2
32 #define BRCM_IG_TC_MASK 0x7
33 /* 2nd byte in the tag */
34 #define BRCM_IG_TE_MASK 0x3
35 #define BRCM_IG_TS_SHIFT 7
36 /* 3rd byte in the tag */
37 #define BRCM_IG_DSTMAP2_MASK 1
38 #define BRCM_IG_DSTMAP1_MASK 0xff
42 /* 2nd byte in the tag */
43 #define BRCM_EG_CID_MASK 0xff
45 /* 3rd byte in the tag */
46 #define BRCM_EG_RC_MASK 0xff
47 #define BRCM_EG_RC_RSVD (3 << 6)
48 #define BRCM_EG_RC_EXCEPTION (1 << 5)
49 #define BRCM_EG_RC_PROT_SNOOP (1 << 4)
50 #define BRCM_EG_RC_PROT_TERM (1 << 3)
51 #define BRCM_EG_RC_SWITCH (1 << 2)
52 #define BRCM_EG_RC_MAC_LEARN (1 << 1)
53 #define BRCM_EG_RC_MIRROR (1 << 0)
54 #define BRCM_EG_TC_SHIFT 5
55 #define BRCM_EG_TC_MASK 0x7
56 #define BRCM_EG_PID_MASK 0x1f
58 static struct sk_buff
*brcm_tag_xmit_ll(struct sk_buff
*skb
,
59 struct net_device
*dev
,
62 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
63 u16 queue
= skb_get_queue_mapping(skb
);
66 if (skb_cow_head(skb
, BRCM_TAG_LEN
) < 0)
69 /* The Ethernet switch we are interfaced with needs packets to be at
70 * least 64 bytes (including FCS) otherwise they will be discarded when
71 * they enter the switch port logic. When Broadcom tags are enabled, we
72 * need to make sure that packets are at least 68 bytes
73 * (including FCS and tag) because the length verification is done after
74 * the Broadcom tag is stripped off the ingress packet.
76 * Let dsa_slave_xmit() free the SKB
78 if (__skb_put_padto(skb
, ETH_ZLEN
+ BRCM_TAG_LEN
, false))
81 skb_push(skb
, BRCM_TAG_LEN
);
84 memmove(skb
->data
, skb
->data
+ BRCM_TAG_LEN
, offset
);
86 brcm_tag
= skb
->data
+ offset
;
88 /* Set the ingress opcode, traffic class, tag enforcment is
91 brcm_tag
[0] = (1 << BRCM_OPCODE_SHIFT
) |
92 ((queue
& BRCM_IG_TC_MASK
) << BRCM_IG_TC_SHIFT
);
96 brcm_tag
[2] = BRCM_IG_DSTMAP2_MASK
;
97 brcm_tag
[3] = (1 << dp
->index
) & BRCM_IG_DSTMAP1_MASK
;
99 /* Now tell the master network device about the desired output queue
102 skb_set_queue_mapping(skb
, BRCM_TAG_SET_PORT_QUEUE(dp
->index
, queue
));
107 static struct sk_buff
*brcm_tag_rcv_ll(struct sk_buff
*skb
,
108 struct net_device
*dev
,
109 struct packet_type
*pt
,
115 if (unlikely(!pskb_may_pull(skb
, BRCM_TAG_LEN
)))
118 brcm_tag
= skb
->data
- offset
;
120 /* The opcode should never be different than 0b000 */
121 if (unlikely((brcm_tag
[0] >> BRCM_OPCODE_SHIFT
) & BRCM_OPCODE_MASK
))
124 /* We should never see a reserved reason code without knowing how to
127 if (unlikely(brcm_tag
[2] & BRCM_EG_RC_RSVD
))
130 /* Locate which port this is coming from */
131 source_port
= brcm_tag
[3] & BRCM_EG_PID_MASK
;
133 skb
->dev
= dsa_master_find_slave(dev
, 0, source_port
);
137 /* Remove Broadcom tag and update checksum */
138 skb_pull_rcsum(skb
, BRCM_TAG_LEN
);
143 #ifdef CONFIG_NET_DSA_TAG_BRCM
144 static struct sk_buff
*brcm_tag_xmit(struct sk_buff
*skb
,
145 struct net_device
*dev
)
147 /* Build the tag after the MAC Source Address */
148 return brcm_tag_xmit_ll(skb
, dev
, 2 * ETH_ALEN
);
152 static struct sk_buff
*brcm_tag_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
153 struct packet_type
*pt
)
155 struct sk_buff
*nskb
;
157 /* skb->data points to the EtherType, the tag is right before it */
158 nskb
= brcm_tag_rcv_ll(skb
, dev
, pt
, 2);
162 /* Move the Ethernet DA and SA */
163 memmove(nskb
->data
- ETH_HLEN
,
164 nskb
->data
- ETH_HLEN
- BRCM_TAG_LEN
,
170 const struct dsa_device_ops brcm_netdev_ops
= {
172 .xmit
= brcm_tag_xmit
,
174 .overhead
= BRCM_TAG_LEN
,
177 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM
);
180 #ifdef CONFIG_NET_DSA_TAG_BRCM_PREPEND
181 static struct sk_buff
*brcm_tag_xmit_prepend(struct sk_buff
*skb
,
182 struct net_device
*dev
)
184 /* tag is prepended to the packet */
185 return brcm_tag_xmit_ll(skb
, dev
, 0);
188 static struct sk_buff
*brcm_tag_rcv_prepend(struct sk_buff
*skb
,
189 struct net_device
*dev
,
190 struct packet_type
*pt
)
192 /* tag is prepended to the packet */
193 return brcm_tag_rcv_ll(skb
, dev
, pt
, ETH_HLEN
);
196 const struct dsa_device_ops brcm_prepend_netdev_ops
= {
197 .name
= "brcm-prepend",
198 .xmit
= brcm_tag_xmit_prepend
,
199 .rcv
= brcm_tag_rcv_prepend
,
200 .overhead
= BRCM_TAG_LEN
,
204 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_PREPEND
);