1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com> 3 */ 4 #include <linux/if_vlan.h> 5 #include <linux/dsa/sja1105.h> 6 #include <linux/dsa/8021q.h> 7 #include <linux/packing.h> 8 #include "dsa_priv.h" 9 10 /* Similar to is_link_local_ether_addr(hdr->h_dest) but also covers PTP */ 11 static inline bool sja1105_is_link_local(const struct sk_buff *skb) 12 { 13 const struct ethhdr *hdr = eth_hdr(skb); 14 u64 dmac = ether_addr_to_u64(hdr->h_dest); 15 16 if (ntohs(hdr->h_proto) == ETH_P_SJA1105_META) 17 return false; 18 if ((dmac & SJA1105_LINKLOCAL_FILTER_A_MASK) == 19 SJA1105_LINKLOCAL_FILTER_A) 20 return true; 21 if ((dmac & SJA1105_LINKLOCAL_FILTER_B_MASK) == 22 SJA1105_LINKLOCAL_FILTER_B) 23 return true; 24 return false; 25 } 26 27 struct sja1105_meta { 28 u64 tstamp; 29 u64 dmac_byte_4; 30 u64 dmac_byte_3; 31 u64 source_port; 32 u64 switch_id; 33 }; 34 35 static void sja1105_meta_unpack(const struct sk_buff *skb, 36 struct sja1105_meta *meta) 37 { 38 u8 *buf = skb_mac_header(skb) + ETH_HLEN; 39 40 /* UM10944.pdf section 4.2.17 AVB Parameters: 41 * Structure of the meta-data follow-up frame. 42 * It is in network byte order, so there are no quirks 43 * while unpacking the meta frame. 44 * 45 * Also SJA1105 E/T only populates bits 23:0 of the timestamp 46 * whereas P/Q/R/S does 32 bits. Since the structure is the 47 * same and the E/T puts zeroes in the high-order byte, use 48 * a unified unpacking command for both device series. 49 */ 50 packing(buf, &meta->tstamp, 31, 0, 4, UNPACK, 0); 51 packing(buf + 4, &meta->dmac_byte_4, 7, 0, 1, UNPACK, 0); 52 packing(buf + 5, &meta->dmac_byte_3, 7, 0, 1, UNPACK, 0); 53 packing(buf + 6, &meta->source_port, 7, 0, 1, UNPACK, 0); 54 packing(buf + 7, &meta->switch_id, 7, 0, 1, UNPACK, 0); 55 } 56 57 static inline bool sja1105_is_meta_frame(const struct sk_buff *skb) 58 { 59 const struct ethhdr *hdr = eth_hdr(skb); 60 u64 smac = ether_addr_to_u64(hdr->h_source); 61 u64 dmac = ether_addr_to_u64(hdr->h_dest); 62 63 if (smac != SJA1105_META_SMAC) 64 return false; 65 if (dmac != SJA1105_META_DMAC) 66 return false; 67 if (ntohs(hdr->h_proto) != ETH_P_SJA1105_META) 68 return false; 69 return true; 70 } 71 72 static bool sja1105_can_use_vlan_as_tags(const struct sk_buff *skb) 73 { 74 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb); 75 u16 vlan_tci; 76 77 if (hdr->h_vlan_proto == htons(ETH_P_SJA1105)) 78 return true; 79 80 if (hdr->h_vlan_proto != htons(ETH_P_8021Q) && 81 !skb_vlan_tag_present(skb)) 82 return false; 83 84 if (skb_vlan_tag_present(skb)) 85 vlan_tci = skb_vlan_tag_get(skb); 86 else 87 vlan_tci = ntohs(hdr->h_vlan_TCI); 88 89 return vid_is_dsa_8021q(vlan_tci & VLAN_VID_MASK); 90 } 91 92 /* This is the first time the tagger sees the frame on RX. 93 * Figure out if we can decode it. 94 */ 95 static bool sja1105_filter(const struct sk_buff *skb, struct net_device *dev) 96 { 97 if (sja1105_can_use_vlan_as_tags(skb)) 98 return true; 99 if (sja1105_is_link_local(skb)) 100 return true; 101 if (sja1105_is_meta_frame(skb)) 102 return true; 103 return false; 104 } 105 106 /* Calls sja1105_port_deferred_xmit in sja1105_main.c */ 107 static struct sk_buff *sja1105_defer_xmit(struct sja1105_port *sp, 108 struct sk_buff *skb) 109 { 110 /* Increase refcount so the kfree_skb in dsa_slave_xmit 111 * won't really free the packet. 112 */ 113 skb_queue_tail(&sp->xmit_queue, skb_get(skb)); 114 kthread_queue_work(sp->xmit_worker, &sp->xmit_work); 115 116 return NULL; 117 } 118 119 static u16 sja1105_xmit_tpid(struct sja1105_port *sp) 120 { 121 return sp->xmit_tpid; 122 } 123 124 static struct sk_buff *sja1105_xmit(struct sk_buff *skb, 125 struct net_device *netdev) 126 { 127 struct dsa_port *dp = dsa_slave_to_port(netdev); 128 u16 tx_vid = dsa_8021q_tx_vid(dp->ds, dp->index); 129 u16 queue_mapping = skb_get_queue_mapping(skb); 130 u8 pcp = netdev_txq_to_tc(netdev, queue_mapping); 131 132 /* Transmitting management traffic does not rely upon switch tagging, 133 * but instead SPI-installed management routes. Part 2 of this 134 * is the .port_deferred_xmit driver callback. 135 */ 136 if (unlikely(sja1105_is_link_local(skb))) 137 return sja1105_defer_xmit(dp->priv, skb); 138 139 return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp->priv), 140 ((pcp << VLAN_PRIO_SHIFT) | tx_vid)); 141 } 142 143 static void sja1105_transfer_meta(struct sk_buff *skb, 144 const struct sja1105_meta *meta) 145 { 146 struct ethhdr *hdr = eth_hdr(skb); 147 148 hdr->h_dest[3] = meta->dmac_byte_3; 149 hdr->h_dest[4] = meta->dmac_byte_4; 150 SJA1105_SKB_CB(skb)->meta_tstamp = meta->tstamp; 151 } 152 153 /* This is a simple state machine which follows the hardware mechanism of 154 * generating RX timestamps: 155 * 156 * After each timestampable skb (all traffic for which send_meta1 and 157 * send_meta0 is true, aka all MAC-filtered link-local traffic) a meta frame 158 * containing a partial timestamp is immediately generated by the switch and 159 * sent as a follow-up to the link-local frame on the CPU port. 160 * 161 * The meta frames have no unique identifier (such as sequence number) by which 162 * one may pair them to the correct timestampable frame. 163 * Instead, the switch has internal logic that ensures no frames are sent on 164 * the CPU port between a link-local timestampable frame and its corresponding 165 * meta follow-up. It also ensures strict ordering between ports (lower ports 166 * have higher priority towards the CPU port). For this reason, a per-port 167 * data structure is not needed/desirable. 168 * 169 * This function pairs the link-local frame with its partial timestamp from the 170 * meta follow-up frame. The full timestamp will be reconstructed later in a 171 * work queue. 172 */ 173 static struct sk_buff 174 *sja1105_rcv_meta_state_machine(struct sk_buff *skb, 175 struct sja1105_meta *meta, 176 bool is_link_local, 177 bool is_meta) 178 { 179 struct sja1105_port *sp; 180 struct dsa_port *dp; 181 182 dp = dsa_slave_to_port(skb->dev); 183 sp = dp->priv; 184 185 /* Step 1: A timestampable frame was received. 186 * Buffer it until we get its meta frame. 187 */ 188 if (is_link_local) { 189 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state)) 190 /* Do normal processing. */ 191 return skb; 192 193 spin_lock(&sp->data->meta_lock); 194 /* Was this a link-local frame instead of the meta 195 * that we were expecting? 196 */ 197 if (sp->data->stampable_skb) { 198 dev_err_ratelimited(dp->ds->dev, 199 "Expected meta frame, is %12llx " 200 "in the DSA master multicast filter?\n", 201 SJA1105_META_DMAC); 202 kfree_skb(sp->data->stampable_skb); 203 } 204 205 /* Hold a reference to avoid dsa_switch_rcv 206 * from freeing the skb. 207 */ 208 sp->data->stampable_skb = skb_get(skb); 209 spin_unlock(&sp->data->meta_lock); 210 211 /* Tell DSA we got nothing */ 212 return NULL; 213 214 /* Step 2: The meta frame arrived. 215 * Time to take the stampable skb out of the closet, annotate it 216 * with the partial timestamp, and pretend that we received it 217 * just now (basically masquerade the buffered frame as the meta 218 * frame, which serves no further purpose). 219 */ 220 } else if (is_meta) { 221 struct sk_buff *stampable_skb; 222 223 /* Drop the meta frame if we're not in the right state 224 * to process it. 225 */ 226 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state)) 227 return NULL; 228 229 spin_lock(&sp->data->meta_lock); 230 231 stampable_skb = sp->data->stampable_skb; 232 sp->data->stampable_skb = NULL; 233 234 /* Was this a meta frame instead of the link-local 235 * that we were expecting? 236 */ 237 if (!stampable_skb) { 238 dev_err_ratelimited(dp->ds->dev, 239 "Unexpected meta frame\n"); 240 spin_unlock(&sp->data->meta_lock); 241 return NULL; 242 } 243 244 if (stampable_skb->dev != skb->dev) { 245 dev_err_ratelimited(dp->ds->dev, 246 "Meta frame on wrong port\n"); 247 spin_unlock(&sp->data->meta_lock); 248 return NULL; 249 } 250 251 /* Free the meta frame and give DSA the buffered stampable_skb 252 * for further processing up the network stack. 253 */ 254 kfree_skb(skb); 255 skb = stampable_skb; 256 sja1105_transfer_meta(skb, meta); 257 258 spin_unlock(&sp->data->meta_lock); 259 } 260 261 return skb; 262 } 263 264 static void sja1105_decode_subvlan(struct sk_buff *skb, u16 subvlan) 265 { 266 struct dsa_port *dp = dsa_slave_to_port(skb->dev); 267 struct sja1105_port *sp = dp->priv; 268 u16 vid = sp->subvlan_map[subvlan]; 269 u16 vlan_tci; 270 271 if (vid == VLAN_N_VID) 272 return; 273 274 vlan_tci = (skb->priority << VLAN_PRIO_SHIFT) | vid; 275 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci); 276 } 277 278 static struct sk_buff *sja1105_rcv(struct sk_buff *skb, 279 struct net_device *netdev, 280 struct packet_type *pt) 281 { 282 struct sja1105_meta meta = {0}; 283 int source_port, switch_id; 284 struct ethhdr *hdr; 285 u16 tpid, vid, tci; 286 bool is_link_local; 287 u16 subvlan = 0; 288 bool is_tagged; 289 bool is_meta; 290 291 hdr = eth_hdr(skb); 292 tpid = ntohs(hdr->h_proto); 293 is_tagged = (tpid == ETH_P_SJA1105 || tpid == ETH_P_8021Q || 294 skb_vlan_tag_present(skb)); 295 is_link_local = sja1105_is_link_local(skb); 296 is_meta = sja1105_is_meta_frame(skb); 297 298 skb->offload_fwd_mark = 1; 299 300 if (is_tagged) { 301 /* Normal traffic path. */ 302 skb_push_rcsum(skb, ETH_HLEN); 303 if (skb_vlan_tag_present(skb)) { 304 tci = skb_vlan_tag_get(skb); 305 __vlan_hwaccel_clear_tag(skb); 306 } else { 307 __skb_vlan_pop(skb, &tci); 308 } 309 skb_pull_rcsum(skb, ETH_HLEN); 310 skb_reset_network_header(skb); 311 skb_reset_transport_header(skb); 312 313 vid = tci & VLAN_VID_MASK; 314 source_port = dsa_8021q_rx_source_port(vid); 315 switch_id = dsa_8021q_rx_switch_id(vid); 316 skb->priority = (tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT; 317 subvlan = dsa_8021q_rx_subvlan(vid); 318 } else if (is_link_local) { 319 /* Management traffic path. Switch embeds the switch ID and 320 * port ID into bytes of the destination MAC, courtesy of 321 * the incl_srcpt options. 322 */ 323 source_port = hdr->h_dest[3]; 324 switch_id = hdr->h_dest[4]; 325 /* Clear the DMAC bytes that were mangled by the switch */ 326 hdr->h_dest[3] = 0; 327 hdr->h_dest[4] = 0; 328 } else if (is_meta) { 329 sja1105_meta_unpack(skb, &meta); 330 source_port = meta.source_port; 331 switch_id = meta.switch_id; 332 } else { 333 return NULL; 334 } 335 336 skb->dev = dsa_master_find_slave(netdev, switch_id, source_port); 337 if (!skb->dev) { 338 netdev_warn(netdev, "Couldn't decode source port\n"); 339 return NULL; 340 } 341 342 if (subvlan) 343 sja1105_decode_subvlan(skb, subvlan); 344 345 return sja1105_rcv_meta_state_machine(skb, &meta, is_link_local, 346 is_meta); 347 } 348 349 static void sja1105_flow_dissect(const struct sk_buff *skb, __be16 *proto, 350 int *offset) 351 { 352 /* No tag added for management frames, all ok */ 353 if (unlikely(sja1105_is_link_local(skb))) 354 return; 355 356 dsa_tag_generic_flow_dissect(skb, proto, offset); 357 } 358 359 static const struct dsa_device_ops sja1105_netdev_ops = { 360 .name = "sja1105", 361 .proto = DSA_TAG_PROTO_SJA1105, 362 .xmit = sja1105_xmit, 363 .rcv = sja1105_rcv, 364 .filter = sja1105_filter, 365 .overhead = VLAN_HLEN, 366 .flow_dissect = sja1105_flow_dissect, 367 .promisc_on_master = true, 368 }; 369 370 MODULE_LICENSE("GPL v2"); 371 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SJA1105); 372 373 module_dsa_tag_driver(sja1105_netdev_ops); 374