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 "dsa_priv.h" 14 15 /* This tag length is 4 bytes, older ones were 6 bytes, we do not 16 * handle them 17 */ 18 #define BRCM_TAG_LEN 4 19 20 /* Tag is constructed and desconstructed using byte by byte access 21 * because the tag is placed after the MAC Source Address, which does 22 * not make it 4-bytes aligned, so this might cause unaligned accesses 23 * on most systems where this is used. 24 */ 25 26 /* Ingress and egress opcodes */ 27 #define BRCM_OPCODE_SHIFT 5 28 #define BRCM_OPCODE_MASK 0x7 29 30 /* Ingress fields */ 31 /* 1st byte in the tag */ 32 #define BRCM_IG_TC_SHIFT 2 33 #define BRCM_IG_TC_MASK 0x7 34 /* 2nd byte in the tag */ 35 #define BRCM_IG_TE_MASK 0x3 36 #define BRCM_IG_TS_SHIFT 7 37 /* 3rd byte in the tag */ 38 #define BRCM_IG_DSTMAP2_MASK 1 39 #define BRCM_IG_DSTMAP1_MASK 0xff 40 41 /* Egress fields */ 42 43 /* 2nd byte in the tag */ 44 #define BRCM_EG_CID_MASK 0xff 45 46 /* 3rd byte in the tag */ 47 #define BRCM_EG_RC_MASK 0xff 48 #define BRCM_EG_RC_RSVD (3 << 6) 49 #define BRCM_EG_RC_EXCEPTION (1 << 5) 50 #define BRCM_EG_RC_PROT_SNOOP (1 << 4) 51 #define BRCM_EG_RC_PROT_TERM (1 << 3) 52 #define BRCM_EG_RC_SWITCH (1 << 2) 53 #define BRCM_EG_RC_MAC_LEARN (1 << 1) 54 #define BRCM_EG_RC_MIRROR (1 << 0) 55 #define BRCM_EG_TC_SHIFT 5 56 #define BRCM_EG_TC_MASK 0x7 57 #define BRCM_EG_PID_MASK 0x1f 58 59 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM) || \ 60 IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND) 61 62 static struct sk_buff *brcm_tag_xmit_ll(struct sk_buff *skb, 63 struct net_device *dev, 64 unsigned int offset) 65 { 66 struct dsa_port *dp = dsa_slave_to_port(dev); 67 u16 queue = skb_get_queue_mapping(skb); 68 u8 *brcm_tag; 69 70 /* The Ethernet switch we are interfaced with needs packets to be at 71 * least 64 bytes (including FCS) otherwise they will be discarded when 72 * they enter the switch port logic. When Broadcom tags are enabled, we 73 * need to make sure that packets are at least 68 bytes 74 * (including FCS and tag) because the length verification is done after 75 * the Broadcom tag is stripped off the ingress packet. 76 * 77 * Let dsa_slave_xmit() free the SKB 78 */ 79 if (__skb_put_padto(skb, ETH_ZLEN + BRCM_TAG_LEN, false)) 80 return NULL; 81 82 skb_push(skb, BRCM_TAG_LEN); 83 84 if (offset) 85 memmove(skb->data, skb->data + BRCM_TAG_LEN, offset); 86 87 brcm_tag = skb->data + offset; 88 89 /* Set the ingress opcode, traffic class, tag enforcment is 90 * deprecated 91 */ 92 brcm_tag[0] = (1 << BRCM_OPCODE_SHIFT) | 93 ((queue & BRCM_IG_TC_MASK) << BRCM_IG_TC_SHIFT); 94 brcm_tag[1] = 0; 95 brcm_tag[2] = 0; 96 if (dp->index == 8) 97 brcm_tag[2] = BRCM_IG_DSTMAP2_MASK; 98 brcm_tag[3] = (1 << dp->index) & BRCM_IG_DSTMAP1_MASK; 99 100 /* Now tell the master network device about the desired output queue 101 * as well 102 */ 103 skb_set_queue_mapping(skb, BRCM_TAG_SET_PORT_QUEUE(dp->index, queue)); 104 105 return skb; 106 } 107 108 /* Frames with this tag have one of these two layouts: 109 * ----------------------------------- 110 * | MAC DA | MAC SA | 4b tag | Type | DSA_TAG_PROTO_BRCM 111 * ----------------------------------- 112 * ----------------------------------- 113 * | 4b tag | MAC DA | MAC SA | Type | DSA_TAG_PROTO_BRCM_PREPEND 114 * ----------------------------------- 115 * In both cases, at receive time, skb->data points 2 bytes before the actual 116 * Ethernet type field and we have an offset of 4bytes between where skb->data 117 * and where the payload starts. So the same low-level receive function can be 118 * used. 119 */ 120 static struct sk_buff *brcm_tag_rcv_ll(struct sk_buff *skb, 121 struct net_device *dev, 122 struct packet_type *pt, 123 unsigned int offset) 124 { 125 int source_port; 126 u8 *brcm_tag; 127 128 if (unlikely(!pskb_may_pull(skb, BRCM_TAG_LEN))) 129 return NULL; 130 131 brcm_tag = skb->data - offset; 132 133 /* The opcode should never be different than 0b000 */ 134 if (unlikely((brcm_tag[0] >> BRCM_OPCODE_SHIFT) & BRCM_OPCODE_MASK)) 135 return NULL; 136 137 /* We should never see a reserved reason code without knowing how to 138 * handle it 139 */ 140 if (unlikely(brcm_tag[2] & BRCM_EG_RC_RSVD)) 141 return NULL; 142 143 /* Locate which port this is coming from */ 144 source_port = brcm_tag[3] & BRCM_EG_PID_MASK; 145 146 skb->dev = dsa_master_find_slave(dev, 0, source_port); 147 if (!skb->dev) 148 return NULL; 149 150 /* Remove Broadcom tag and update checksum */ 151 skb_pull_rcsum(skb, BRCM_TAG_LEN); 152 153 skb->offload_fwd_mark = 1; 154 155 return skb; 156 } 157 #endif 158 159 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM) 160 static struct sk_buff *brcm_tag_xmit(struct sk_buff *skb, 161 struct net_device *dev) 162 { 163 /* Build the tag after the MAC Source Address */ 164 return brcm_tag_xmit_ll(skb, dev, 2 * ETH_ALEN); 165 } 166 167 168 static struct sk_buff *brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev, 169 struct packet_type *pt) 170 { 171 struct sk_buff *nskb; 172 173 /* skb->data points to the EtherType, the tag is right before it */ 174 nskb = brcm_tag_rcv_ll(skb, dev, pt, 2); 175 if (!nskb) 176 return nskb; 177 178 /* Move the Ethernet DA and SA */ 179 memmove(nskb->data - ETH_HLEN, 180 nskb->data - ETH_HLEN - BRCM_TAG_LEN, 181 2 * ETH_ALEN); 182 183 return nskb; 184 } 185 186 static const struct dsa_device_ops brcm_netdev_ops = { 187 .name = "brcm", 188 .proto = DSA_TAG_PROTO_BRCM, 189 .xmit = brcm_tag_xmit, 190 .rcv = brcm_tag_rcv, 191 .overhead = BRCM_TAG_LEN, 192 }; 193 194 DSA_TAG_DRIVER(brcm_netdev_ops); 195 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM); 196 #endif 197 198 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND) 199 static struct sk_buff *brcm_tag_xmit_prepend(struct sk_buff *skb, 200 struct net_device *dev) 201 { 202 /* tag is prepended to the packet */ 203 return brcm_tag_xmit_ll(skb, dev, 0); 204 } 205 206 static struct sk_buff *brcm_tag_rcv_prepend(struct sk_buff *skb, 207 struct net_device *dev, 208 struct packet_type *pt) 209 { 210 /* tag is prepended to the packet */ 211 return brcm_tag_rcv_ll(skb, dev, pt, ETH_HLEN); 212 } 213 214 static const struct dsa_device_ops brcm_prepend_netdev_ops = { 215 .name = "brcm-prepend", 216 .proto = DSA_TAG_PROTO_BRCM_PREPEND, 217 .xmit = brcm_tag_xmit_prepend, 218 .rcv = brcm_tag_rcv_prepend, 219 .overhead = BRCM_TAG_LEN, 220 }; 221 222 DSA_TAG_DRIVER(brcm_prepend_netdev_ops); 223 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_PREPEND); 224 #endif 225 226 static struct dsa_tag_driver *dsa_tag_driver_array[] = { 227 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM) 228 &DSA_TAG_DRIVER_NAME(brcm_netdev_ops), 229 #endif 230 #if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND) 231 &DSA_TAG_DRIVER_NAME(brcm_prepend_netdev_ops), 232 #endif 233 }; 234 235 module_dsa_tag_drivers(dsa_tag_driver_array); 236 237 MODULE_LICENSE("GPL"); 238