1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com>
4 #include <linux/if_vlan.h>
5 #include <linux/dsa/sja1105.h>
6 #include <linux/dsa/8021q.h>
7 #include <linux/packing.h>
10 /* Is this a TX or an RX header? */
11 #define SJA1110_HEADER_HOST_TO_SWITCH BIT(15)
14 #define SJA1110_RX_HEADER_IS_METADATA BIT(14)
15 #define SJA1110_RX_HEADER_HOST_ONLY BIT(13)
16 #define SJA1110_RX_HEADER_HAS_TRAILER BIT(12)
18 /* Trap-to-host format (no trailer present) */
19 #define SJA1110_RX_HEADER_SRC_PORT(x) (((x) & GENMASK(7, 4)) >> 4)
20 #define SJA1110_RX_HEADER_SWITCH_ID(x) ((x) & GENMASK(3, 0))
22 /* Timestamp format (trailer present) */
23 #define SJA1110_RX_HEADER_TRAILER_POS(x) ((x) & GENMASK(11, 0))
25 #define SJA1110_RX_TRAILER_SWITCH_ID(x) (((x) & GENMASK(7, 4)) >> 4)
26 #define SJA1110_RX_TRAILER_SRC_PORT(x) ((x) & GENMASK(3, 0))
28 /* Meta frame format (for 2-step TX timestamps) */
29 #define SJA1110_RX_HEADER_N_TS(x) (((x) & GENMASK(8, 4)) >> 4)
32 #define SJA1110_TX_HEADER_UPDATE_TC BIT(14)
33 #define SJA1110_TX_HEADER_TAKE_TS BIT(13)
34 #define SJA1110_TX_HEADER_TAKE_TS_CASC BIT(12)
35 #define SJA1110_TX_HEADER_HAS_TRAILER BIT(11)
37 /* Only valid if SJA1110_TX_HEADER_HAS_TRAILER is false */
38 #define SJA1110_TX_HEADER_PRIO(x) (((x) << 7) & GENMASK(10, 7))
39 #define SJA1110_TX_HEADER_TSTAMP_ID(x) ((x) & GENMASK(7, 0))
41 /* Only valid if SJA1110_TX_HEADER_HAS_TRAILER is true */
42 #define SJA1110_TX_HEADER_TRAILER_POS(x) ((x) & GENMASK(10, 0))
44 #define SJA1110_TX_TRAILER_TSTAMP_ID(x) (((x) << 24) & GENMASK(31, 24))
45 #define SJA1110_TX_TRAILER_PRIO(x) (((x) << 21) & GENMASK(23, 21))
46 #define SJA1110_TX_TRAILER_SWITCHID(x) (((x) << 12) & GENMASK(15, 12))
47 #define SJA1110_TX_TRAILER_DESTPORTS(x) (((x) << 1) & GENMASK(11, 1))
49 #define SJA1110_META_TSTAMP_SIZE 10
51 #define SJA1110_HEADER_LEN 4
52 #define SJA1110_RX_TRAILER_LEN 13
53 #define SJA1110_TX_TRAILER_LEN 4
54 #define SJA1110_MAX_PADDING_LEN 15
56 /* Similar to is_link_local_ether_addr(hdr->h_dest) but also covers PTP */
57 static inline bool sja1105_is_link_local(const struct sk_buff *skb)
59 const struct ethhdr *hdr = eth_hdr(skb);
60 u64 dmac = ether_addr_to_u64(hdr->h_dest);
62 if (ntohs(hdr->h_proto) == ETH_P_SJA1105_META)
64 if ((dmac & SJA1105_LINKLOCAL_FILTER_A_MASK) ==
65 SJA1105_LINKLOCAL_FILTER_A)
67 if ((dmac & SJA1105_LINKLOCAL_FILTER_B_MASK) ==
68 SJA1105_LINKLOCAL_FILTER_B)
81 static void sja1105_meta_unpack(const struct sk_buff *skb,
82 struct sja1105_meta *meta)
84 u8 *buf = skb_mac_header(skb) + ETH_HLEN;
86 /* UM10944.pdf section 4.2.17 AVB Parameters:
87 * Structure of the meta-data follow-up frame.
88 * It is in network byte order, so there are no quirks
89 * while unpacking the meta frame.
91 * Also SJA1105 E/T only populates bits 23:0 of the timestamp
92 * whereas P/Q/R/S does 32 bits. Since the structure is the
93 * same and the E/T puts zeroes in the high-order byte, use
94 * a unified unpacking command for both device series.
96 packing(buf, &meta->tstamp, 31, 0, 4, UNPACK, 0);
97 packing(buf + 4, &meta->dmac_byte_4, 7, 0, 1, UNPACK, 0);
98 packing(buf + 5, &meta->dmac_byte_3, 7, 0, 1, UNPACK, 0);
99 packing(buf + 6, &meta->source_port, 7, 0, 1, UNPACK, 0);
100 packing(buf + 7, &meta->switch_id, 7, 0, 1, UNPACK, 0);
103 static inline bool sja1105_is_meta_frame(const struct sk_buff *skb)
105 const struct ethhdr *hdr = eth_hdr(skb);
106 u64 smac = ether_addr_to_u64(hdr->h_source);
107 u64 dmac = ether_addr_to_u64(hdr->h_dest);
109 if (smac != SJA1105_META_SMAC)
111 if (dmac != SJA1105_META_DMAC)
113 if (ntohs(hdr->h_proto) != ETH_P_SJA1105_META)
118 static bool sja1105_can_use_vlan_as_tags(const struct sk_buff *skb)
120 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
123 if (hdr->h_vlan_proto == htons(ETH_P_SJA1105))
126 if (hdr->h_vlan_proto != htons(ETH_P_8021Q) &&
127 !skb_vlan_tag_present(skb))
130 if (skb_vlan_tag_present(skb))
131 vlan_tci = skb_vlan_tag_get(skb);
133 vlan_tci = ntohs(hdr->h_vlan_TCI);
135 return vid_is_dsa_8021q(vlan_tci & VLAN_VID_MASK);
138 /* This is the first time the tagger sees the frame on RX.
139 * Figure out if we can decode it.
141 static bool sja1105_filter(const struct sk_buff *skb, struct net_device *dev)
143 if (sja1105_can_use_vlan_as_tags(skb))
145 if (sja1105_is_link_local(skb))
147 if (sja1105_is_meta_frame(skb))
152 /* Calls sja1105_port_deferred_xmit in sja1105_main.c */
153 static struct sk_buff *sja1105_defer_xmit(struct sja1105_port *sp,
156 /* Increase refcount so the kfree_skb in dsa_slave_xmit
157 * won't really free the packet.
159 skb_queue_tail(&sp->xmit_queue, skb_get(skb));
160 kthread_queue_work(sp->xmit_worker, &sp->xmit_work);
165 static u16 sja1105_xmit_tpid(struct sja1105_port *sp)
167 return sp->xmit_tpid;
170 static struct sk_buff *sja1105_xmit(struct sk_buff *skb,
171 struct net_device *netdev)
173 struct dsa_port *dp = dsa_slave_to_port(netdev);
174 u16 tx_vid = dsa_8021q_tx_vid(dp->ds, dp->index);
175 u16 queue_mapping = skb_get_queue_mapping(skb);
176 u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
178 /* Transmitting management traffic does not rely upon switch tagging,
179 * but instead SPI-installed management routes. Part 2 of this
180 * is the .port_deferred_xmit driver callback.
182 if (unlikely(sja1105_is_link_local(skb)))
183 return sja1105_defer_xmit(dp->priv, skb);
185 return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp->priv),
186 ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
189 static struct sk_buff *sja1110_xmit(struct sk_buff *skb,
190 struct net_device *netdev)
192 struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
193 struct dsa_port *dp = dsa_slave_to_port(netdev);
194 u16 tx_vid = dsa_8021q_tx_vid(dp->ds, dp->index);
195 u16 queue_mapping = skb_get_queue_mapping(skb);
196 u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
197 struct ethhdr *eth_hdr;
202 /* Transmitting control packets is done using in-band control
203 * extensions, while data packets are transmitted using
204 * tag_8021q TX VLANs.
206 if (likely(!sja1105_is_link_local(skb)))
207 return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp->priv),
208 ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
210 skb_push(skb, SJA1110_HEADER_LEN);
212 /* Move Ethernet header to the left, making space for DSA tag */
213 memmove(skb->data, skb->data + SJA1110_HEADER_LEN, 2 * ETH_ALEN);
215 trailer_pos = skb->len;
217 /* On TX, skb->data points to skb_mac_header(skb) */
218 eth_hdr = (struct ethhdr *)skb->data;
219 tx_header = (__be16 *)(eth_hdr + 1);
220 tx_trailer = skb_put(skb, SJA1110_TX_TRAILER_LEN);
222 eth_hdr->h_proto = htons(ETH_P_SJA1110);
224 *tx_header = htons(SJA1110_HEADER_HOST_TO_SWITCH |
225 SJA1110_TX_HEADER_HAS_TRAILER |
226 SJA1110_TX_HEADER_TRAILER_POS(trailer_pos));
227 *tx_trailer = cpu_to_be32(SJA1110_TX_TRAILER_PRIO(pcp) |
228 SJA1110_TX_TRAILER_SWITCHID(dp->ds->index) |
229 SJA1110_TX_TRAILER_DESTPORTS(BIT(dp->index)));
231 u8 ts_id = SJA1105_SKB_CB(clone)->ts_id;
233 *tx_header |= htons(SJA1110_TX_HEADER_TAKE_TS);
234 *tx_trailer |= cpu_to_be32(SJA1110_TX_TRAILER_TSTAMP_ID(ts_id));
240 static void sja1105_transfer_meta(struct sk_buff *skb,
241 const struct sja1105_meta *meta)
243 struct ethhdr *hdr = eth_hdr(skb);
245 hdr->h_dest[3] = meta->dmac_byte_3;
246 hdr->h_dest[4] = meta->dmac_byte_4;
247 SJA1105_SKB_CB(skb)->tstamp = meta->tstamp;
250 /* This is a simple state machine which follows the hardware mechanism of
251 * generating RX timestamps:
253 * After each timestampable skb (all traffic for which send_meta1 and
254 * send_meta0 is true, aka all MAC-filtered link-local traffic) a meta frame
255 * containing a partial timestamp is immediately generated by the switch and
256 * sent as a follow-up to the link-local frame on the CPU port.
258 * The meta frames have no unique identifier (such as sequence number) by which
259 * one may pair them to the correct timestampable frame.
260 * Instead, the switch has internal logic that ensures no frames are sent on
261 * the CPU port between a link-local timestampable frame and its corresponding
262 * meta follow-up. It also ensures strict ordering between ports (lower ports
263 * have higher priority towards the CPU port). For this reason, a per-port
264 * data structure is not needed/desirable.
266 * This function pairs the link-local frame with its partial timestamp from the
267 * meta follow-up frame. The full timestamp will be reconstructed later in a
270 static struct sk_buff
271 *sja1105_rcv_meta_state_machine(struct sk_buff *skb,
272 struct sja1105_meta *meta,
276 struct sja1105_port *sp;
279 dp = dsa_slave_to_port(skb->dev);
282 /* Step 1: A timestampable frame was received.
283 * Buffer it until we get its meta frame.
286 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state))
287 /* Do normal processing. */
290 spin_lock(&sp->data->meta_lock);
291 /* Was this a link-local frame instead of the meta
292 * that we were expecting?
294 if (sp->data->stampable_skb) {
295 dev_err_ratelimited(dp->ds->dev,
296 "Expected meta frame, is %12llx "
297 "in the DSA master multicast filter?\n",
299 kfree_skb(sp->data->stampable_skb);
302 /* Hold a reference to avoid dsa_switch_rcv
303 * from freeing the skb.
305 sp->data->stampable_skb = skb_get(skb);
306 spin_unlock(&sp->data->meta_lock);
308 /* Tell DSA we got nothing */
311 /* Step 2: The meta frame arrived.
312 * Time to take the stampable skb out of the closet, annotate it
313 * with the partial timestamp, and pretend that we received it
314 * just now (basically masquerade the buffered frame as the meta
315 * frame, which serves no further purpose).
317 } else if (is_meta) {
318 struct sk_buff *stampable_skb;
320 /* Drop the meta frame if we're not in the right state
323 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state))
326 spin_lock(&sp->data->meta_lock);
328 stampable_skb = sp->data->stampable_skb;
329 sp->data->stampable_skb = NULL;
331 /* Was this a meta frame instead of the link-local
332 * that we were expecting?
334 if (!stampable_skb) {
335 dev_err_ratelimited(dp->ds->dev,
336 "Unexpected meta frame\n");
337 spin_unlock(&sp->data->meta_lock);
341 if (stampable_skb->dev != skb->dev) {
342 dev_err_ratelimited(dp->ds->dev,
343 "Meta frame on wrong port\n");
344 spin_unlock(&sp->data->meta_lock);
348 /* Free the meta frame and give DSA the buffered stampable_skb
349 * for further processing up the network stack.
353 sja1105_transfer_meta(skb, meta);
355 spin_unlock(&sp->data->meta_lock);
361 static bool sja1105_skb_has_tag_8021q(const struct sk_buff *skb)
363 u16 tpid = ntohs(eth_hdr(skb)->h_proto);
365 return tpid == ETH_P_SJA1105 || tpid == ETH_P_8021Q ||
366 skb_vlan_tag_present(skb);
369 static bool sja1110_skb_has_inband_control_extension(const struct sk_buff *skb)
371 return ntohs(eth_hdr(skb)->h_proto) == ETH_P_SJA1110;
374 static struct sk_buff *sja1105_rcv(struct sk_buff *skb,
375 struct net_device *netdev,
376 struct packet_type *pt)
378 struct sja1105_meta meta = {0};
379 int source_port, switch_id;
385 is_link_local = sja1105_is_link_local(skb);
386 is_meta = sja1105_is_meta_frame(skb);
388 skb->offload_fwd_mark = 1;
390 if (sja1105_skb_has_tag_8021q(skb)) {
391 /* Normal traffic path. */
392 dsa_8021q_rcv(skb, &source_port, &switch_id);
393 } else if (is_link_local) {
394 /* Management traffic path. Switch embeds the switch ID and
395 * port ID into bytes of the destination MAC, courtesy of
396 * the incl_srcpt options.
398 source_port = hdr->h_dest[3];
399 switch_id = hdr->h_dest[4];
400 /* Clear the DMAC bytes that were mangled by the switch */
403 } else if (is_meta) {
404 sja1105_meta_unpack(skb, &meta);
405 source_port = meta.source_port;
406 switch_id = meta.switch_id;
411 skb->dev = dsa_master_find_slave(netdev, switch_id, source_port);
413 netdev_warn(netdev, "Couldn't decode source port\n");
417 return sja1105_rcv_meta_state_machine(skb, &meta, is_link_local,
421 static struct sk_buff *sja1110_rcv_meta(struct sk_buff *skb, u16 rx_header)
423 int switch_id = SJA1110_RX_HEADER_SWITCH_ID(rx_header);
424 int n_ts = SJA1110_RX_HEADER_N_TS(rx_header);
425 struct net_device *master = skb->dev;
426 struct dsa_port *cpu_dp;
427 u8 *buf = skb->data + 2;
428 struct dsa_switch *ds;
431 cpu_dp = master->dsa_ptr;
432 ds = dsa_switch_find(cpu_dp->dst->index, switch_id);
434 net_err_ratelimited("%s: cannot find switch id %d\n",
435 master->name, switch_id);
439 for (i = 0; i <= n_ts; i++) {
440 u8 ts_id, source_port, dir;
444 source_port = (buf[1] & GENMASK(7, 4)) >> 4;
445 dir = (buf[1] & BIT(3)) >> 3;
446 tstamp = be64_to_cpu(*(__be64 *)(buf + 2));
448 sja1110_process_meta_tstamp(ds, source_port, ts_id, dir,
451 buf += SJA1110_META_TSTAMP_SIZE;
454 /* Discard the meta frame, we've consumed the timestamps it contained */
458 static struct sk_buff *sja1110_rcv_inband_control_extension(struct sk_buff *skb,
464 if (unlikely(!pskb_may_pull(skb, SJA1110_HEADER_LEN)))
467 /* skb->data points to skb_mac_header(skb) + ETH_HLEN, which is exactly
468 * what we need because the caller has checked the EtherType (which is
469 * located 2 bytes back) and we just need a pointer to the header that
472 rx_header = ntohs(*(__be16 *)skb->data);
474 if (rx_header & SJA1110_RX_HEADER_IS_METADATA)
475 return sja1110_rcv_meta(skb, rx_header);
477 /* Timestamp frame, we have a trailer */
478 if (rx_header & SJA1110_RX_HEADER_HAS_TRAILER) {
479 int start_of_padding = SJA1110_RX_HEADER_TRAILER_POS(rx_header);
480 u8 *rx_trailer = skb_tail_pointer(skb) - SJA1110_RX_TRAILER_LEN;
481 u64 *tstamp = &SJA1105_SKB_CB(skb)->tstamp;
482 u8 last_byte = rx_trailer[12];
484 /* The timestamp is unaligned, so we need to use packing()
487 packing(rx_trailer, tstamp, 63, 0, 8, UNPACK, 0);
489 *source_port = SJA1110_RX_TRAILER_SRC_PORT(last_byte);
490 *switch_id = SJA1110_RX_TRAILER_SWITCH_ID(last_byte);
492 /* skb->len counts from skb->data, while start_of_padding
493 * counts from the destination MAC address. Right now skb->data
494 * is still as set by the DSA master, so to trim away the
495 * padding and trailer we need to account for the fact that
496 * skb->data points to skb_mac_header(skb) + ETH_HLEN.
498 pskb_trim_rcsum(skb, start_of_padding - ETH_HLEN);
499 /* Trap-to-host frame, no timestamp trailer */
501 *source_port = SJA1110_RX_HEADER_SRC_PORT(rx_header);
502 *switch_id = SJA1110_RX_HEADER_SWITCH_ID(rx_header);
505 /* Advance skb->data past the DSA header */
506 skb_pull_rcsum(skb, SJA1110_HEADER_LEN);
508 /* Remove the DSA header */
509 memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - SJA1110_HEADER_LEN,
512 /* With skb->data in its final place, update the MAC header
513 * so that eth_hdr() continues to works properly.
515 skb_set_mac_header(skb, -ETH_HLEN);
520 static struct sk_buff *sja1110_rcv(struct sk_buff *skb,
521 struct net_device *netdev,
522 struct packet_type *pt)
524 int source_port = -1, switch_id = -1;
526 skb->offload_fwd_mark = 1;
528 if (sja1110_skb_has_inband_control_extension(skb)) {
529 skb = sja1110_rcv_inband_control_extension(skb, &source_port,
535 /* Packets with in-band control extensions might still have RX VLANs */
536 if (likely(sja1105_skb_has_tag_8021q(skb)))
537 dsa_8021q_rcv(skb, &source_port, &switch_id);
539 skb->dev = dsa_master_find_slave(netdev, switch_id, source_port);
542 "Couldn't decode source port %d and switch id %d\n",
543 source_port, switch_id);
550 static void sja1105_flow_dissect(const struct sk_buff *skb, __be16 *proto,
553 /* No tag added for management frames, all ok */
554 if (unlikely(sja1105_is_link_local(skb)))
557 dsa_tag_generic_flow_dissect(skb, proto, offset);
560 static void sja1110_flow_dissect(const struct sk_buff *skb, __be16 *proto,
563 /* Management frames have 2 DSA tags on RX, so the needed_headroom we
564 * declared is fine for the generic dissector adjustment procedure.
566 if (unlikely(sja1105_is_link_local(skb)))
567 return dsa_tag_generic_flow_dissect(skb, proto, offset);
569 /* For the rest, there is a single DSA tag, the tag_8021q one */
571 *proto = ((__be16 *)skb->data)[(VLAN_HLEN / 2) - 1];
574 static const struct dsa_device_ops sja1105_netdev_ops = {
576 .proto = DSA_TAG_PROTO_SJA1105,
577 .xmit = sja1105_xmit,
579 .filter = sja1105_filter,
580 .needed_headroom = VLAN_HLEN,
581 .flow_dissect = sja1105_flow_dissect,
582 .promisc_on_master = true,
585 DSA_TAG_DRIVER(sja1105_netdev_ops);
586 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SJA1105);
588 static const struct dsa_device_ops sja1110_netdev_ops = {
590 .proto = DSA_TAG_PROTO_SJA1110,
591 .xmit = sja1110_xmit,
593 .filter = sja1105_filter,
594 .flow_dissect = sja1110_flow_dissect,
595 .needed_headroom = SJA1110_HEADER_LEN + VLAN_HLEN,
596 .needed_tailroom = SJA1110_RX_TRAILER_LEN + SJA1110_MAX_PADDING_LEN,
599 DSA_TAG_DRIVER(sja1110_netdev_ops);
600 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SJA1110);
602 static struct dsa_tag_driver *sja1105_tag_driver_array[] = {
603 &DSA_TAG_DRIVER_NAME(sja1105_netdev_ops),
604 &DSA_TAG_DRIVER_NAME(sja1110_netdev_ops),
607 module_dsa_tag_drivers(sja1105_tag_driver_array);
609 MODULE_LICENSE("GPL v2");