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 /* Calls sja1105_port_deferred_xmit in sja1105_main.c */
119 static struct sk_buff *sja1105_defer_xmit(struct dsa_port *dp,
122 struct sja1105_port *sp = dp->priv;
124 if (!dsa_port_is_sja1105(dp))
127 /* Increase refcount so the kfree_skb in dsa_slave_xmit
128 * won't really free the packet.
130 skb_queue_tail(&sp->xmit_queue, skb_get(skb));
131 kthread_queue_work(sp->xmit_worker, &sp->xmit_work);
136 static u16 sja1105_xmit_tpid(struct dsa_port *dp)
138 struct sja1105_port *sp = dp->priv;
140 if (unlikely(!dsa_port_is_sja1105(dp)))
143 return sp->xmit_tpid;
146 static struct sk_buff *sja1105_imprecise_xmit(struct sk_buff *skb,
147 struct net_device *netdev)
149 struct dsa_port *dp = dsa_slave_to_port(netdev);
150 struct net_device *br = dp->bridge_dev;
153 /* If the port is under a VLAN-aware bridge, just slide the
154 * VLAN-tagged packet into the FDB and hope for the best.
155 * This works because we support a single VLAN-aware bridge
156 * across the entire dst, and its VLANs cannot be shared with
157 * any standalone port.
159 if (br_vlan_enabled(br))
162 /* If the port is under a VLAN-unaware bridge, use an imprecise
163 * TX VLAN that targets the bridge's entire broadcast domain,
164 * instead of just the specific port.
166 tx_vid = dsa_8021q_bridge_tx_fwd_offload_vid(dp->bridge_num);
168 return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp), tx_vid);
171 static struct sk_buff *sja1105_xmit(struct sk_buff *skb,
172 struct net_device *netdev)
174 struct dsa_port *dp = dsa_slave_to_port(netdev);
175 u16 tx_vid = dsa_8021q_tx_vid(dp->ds, dp->index);
176 u16 queue_mapping = skb_get_queue_mapping(skb);
177 u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
179 if (skb->offload_fwd_mark)
180 return sja1105_imprecise_xmit(skb, netdev);
182 /* Transmitting management traffic does not rely upon switch tagging,
183 * but instead SPI-installed management routes. Part 2 of this
184 * is the .port_deferred_xmit driver callback.
186 if (unlikely(sja1105_is_link_local(skb)))
187 return sja1105_defer_xmit(dp, skb);
189 return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp),
190 ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
193 static struct sk_buff *sja1110_xmit(struct sk_buff *skb,
194 struct net_device *netdev)
196 struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
197 struct dsa_port *dp = dsa_slave_to_port(netdev);
198 u16 tx_vid = dsa_8021q_tx_vid(dp->ds, dp->index);
199 u16 queue_mapping = skb_get_queue_mapping(skb);
200 u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
205 if (skb->offload_fwd_mark)
206 return sja1105_imprecise_xmit(skb, netdev);
208 /* Transmitting control packets is done using in-band control
209 * extensions, while data packets are transmitted using
210 * tag_8021q TX VLANs.
212 if (likely(!sja1105_is_link_local(skb)))
213 return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp),
214 ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
216 skb_push(skb, SJA1110_HEADER_LEN);
218 dsa_alloc_etype_header(skb, SJA1110_HEADER_LEN);
220 trailer_pos = skb->len;
222 tx_header = dsa_etype_header_pos_tx(skb);
223 tx_trailer = skb_put(skb, SJA1110_TX_TRAILER_LEN);
225 tx_header[0] = htons(ETH_P_SJA1110);
226 tx_header[1] = htons(SJA1110_HEADER_HOST_TO_SWITCH |
227 SJA1110_TX_HEADER_HAS_TRAILER |
228 SJA1110_TX_HEADER_TRAILER_POS(trailer_pos));
229 *tx_trailer = cpu_to_be32(SJA1110_TX_TRAILER_PRIO(pcp) |
230 SJA1110_TX_TRAILER_SWITCHID(dp->ds->index) |
231 SJA1110_TX_TRAILER_DESTPORTS(BIT(dp->index)));
233 u8 ts_id = SJA1105_SKB_CB(clone)->ts_id;
235 tx_header[1] |= htons(SJA1110_TX_HEADER_TAKE_TS);
236 *tx_trailer |= cpu_to_be32(SJA1110_TX_TRAILER_TSTAMP_ID(ts_id));
242 static void sja1105_transfer_meta(struct sk_buff *skb,
243 const struct sja1105_meta *meta)
245 struct ethhdr *hdr = eth_hdr(skb);
247 hdr->h_dest[3] = meta->dmac_byte_3;
248 hdr->h_dest[4] = meta->dmac_byte_4;
249 SJA1105_SKB_CB(skb)->tstamp = meta->tstamp;
252 /* This is a simple state machine which follows the hardware mechanism of
253 * generating RX timestamps:
255 * After each timestampable skb (all traffic for which send_meta1 and
256 * send_meta0 is true, aka all MAC-filtered link-local traffic) a meta frame
257 * containing a partial timestamp is immediately generated by the switch and
258 * sent as a follow-up to the link-local frame on the CPU port.
260 * The meta frames have no unique identifier (such as sequence number) by which
261 * one may pair them to the correct timestampable frame.
262 * Instead, the switch has internal logic that ensures no frames are sent on
263 * the CPU port between a link-local timestampable frame and its corresponding
264 * meta follow-up. It also ensures strict ordering between ports (lower ports
265 * have higher priority towards the CPU port). For this reason, a per-port
266 * data structure is not needed/desirable.
268 * This function pairs the link-local frame with its partial timestamp from the
269 * meta follow-up frame. The full timestamp will be reconstructed later in a
272 static struct sk_buff
273 *sja1105_rcv_meta_state_machine(struct sk_buff *skb,
274 struct sja1105_meta *meta,
278 /* Step 1: A timestampable frame was received.
279 * Buffer it until we get its meta frame.
282 struct dsa_port *dp = dsa_slave_to_port(skb->dev);
283 struct sja1105_port *sp = dp->priv;
285 if (unlikely(!dsa_port_is_sja1105(dp)))
288 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state))
289 /* Do normal processing. */
292 spin_lock(&sp->data->meta_lock);
293 /* Was this a link-local frame instead of the meta
294 * that we were expecting?
296 if (sp->data->stampable_skb) {
297 dev_err_ratelimited(dp->ds->dev,
298 "Expected meta frame, is %12llx "
299 "in the DSA master multicast filter?\n",
301 kfree_skb(sp->data->stampable_skb);
304 /* Hold a reference to avoid dsa_switch_rcv
305 * from freeing the skb.
307 sp->data->stampable_skb = skb_get(skb);
308 spin_unlock(&sp->data->meta_lock);
310 /* Tell DSA we got nothing */
313 /* Step 2: The meta frame arrived.
314 * Time to take the stampable skb out of the closet, annotate it
315 * with the partial timestamp, and pretend that we received it
316 * just now (basically masquerade the buffered frame as the meta
317 * frame, which serves no further purpose).
319 } else if (is_meta) {
320 struct dsa_port *dp = dsa_slave_to_port(skb->dev);
321 struct sja1105_port *sp = dp->priv;
322 struct sk_buff *stampable_skb;
324 if (unlikely(!dsa_port_is_sja1105(dp)))
327 /* Drop the meta frame if we're not in the right state
330 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state))
333 spin_lock(&sp->data->meta_lock);
335 stampable_skb = sp->data->stampable_skb;
336 sp->data->stampable_skb = NULL;
338 /* Was this a meta frame instead of the link-local
339 * that we were expecting?
341 if (!stampable_skb) {
342 dev_err_ratelimited(dp->ds->dev,
343 "Unexpected meta frame\n");
344 spin_unlock(&sp->data->meta_lock);
348 if (stampable_skb->dev != skb->dev) {
349 dev_err_ratelimited(dp->ds->dev,
350 "Meta frame on wrong port\n");
351 spin_unlock(&sp->data->meta_lock);
355 /* Free the meta frame and give DSA the buffered stampable_skb
356 * for further processing up the network stack.
360 sja1105_transfer_meta(skb, meta);
362 spin_unlock(&sp->data->meta_lock);
368 static bool sja1105_skb_has_tag_8021q(const struct sk_buff *skb)
370 u16 tpid = ntohs(eth_hdr(skb)->h_proto);
372 return tpid == ETH_P_SJA1105 || tpid == ETH_P_8021Q ||
373 skb_vlan_tag_present(skb);
376 static bool sja1110_skb_has_inband_control_extension(const struct sk_buff *skb)
378 return ntohs(eth_hdr(skb)->h_proto) == ETH_P_SJA1110;
381 /* If the VLAN in the packet is a tag_8021q one, set @source_port and
382 * @switch_id and strip the header. Otherwise set @vid and keep it in the
385 static void sja1105_vlan_rcv(struct sk_buff *skb, int *source_port,
386 int *switch_id, u16 *vid)
388 struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)skb_mac_header(skb);
391 if (skb_vlan_tag_present(skb))
392 vlan_tci = skb_vlan_tag_get(skb);
394 vlan_tci = ntohs(hdr->h_vlan_TCI);
396 if (vid_is_dsa_8021q_rxvlan(vlan_tci & VLAN_VID_MASK))
397 return dsa_8021q_rcv(skb, source_port, switch_id);
399 /* Try our best with imprecise RX */
400 *vid = vlan_tci & VLAN_VID_MASK;
403 static struct sk_buff *sja1105_rcv(struct sk_buff *skb,
404 struct net_device *netdev)
406 int source_port = -1, switch_id = -1;
407 struct sja1105_meta meta = {0};
414 is_link_local = sja1105_is_link_local(skb);
415 is_meta = sja1105_is_meta_frame(skb);
417 if (sja1105_skb_has_tag_8021q(skb)) {
418 /* Normal traffic path. */
419 sja1105_vlan_rcv(skb, &source_port, &switch_id, &vid);
420 } else if (is_link_local) {
421 /* Management traffic path. Switch embeds the switch ID and
422 * port ID into bytes of the destination MAC, courtesy of
423 * the incl_srcpt options.
425 source_port = hdr->h_dest[3];
426 switch_id = hdr->h_dest[4];
427 /* Clear the DMAC bytes that were mangled by the switch */
430 } else if (is_meta) {
431 sja1105_meta_unpack(skb, &meta);
432 source_port = meta.source_port;
433 switch_id = meta.switch_id;
438 if (source_port == -1 || switch_id == -1)
439 skb->dev = dsa_find_designated_bridge_port_by_vid(netdev, vid);
441 skb->dev = dsa_master_find_slave(netdev, switch_id, source_port);
443 netdev_warn(netdev, "Couldn't decode source port\n");
448 dsa_default_offload_fwd_mark(skb);
450 return sja1105_rcv_meta_state_machine(skb, &meta, is_link_local,
454 static struct sk_buff *sja1110_rcv_meta(struct sk_buff *skb, u16 rx_header)
456 u8 *buf = dsa_etype_header_pos_rx(skb) + SJA1110_HEADER_LEN;
457 int switch_id = SJA1110_RX_HEADER_SWITCH_ID(rx_header);
458 int n_ts = SJA1110_RX_HEADER_N_TS(rx_header);
459 struct net_device *master = skb->dev;
460 struct dsa_port *cpu_dp;
461 struct dsa_switch *ds;
464 cpu_dp = master->dsa_ptr;
465 ds = dsa_switch_find(cpu_dp->dst->index, switch_id);
467 net_err_ratelimited("%s: cannot find switch id %d\n",
468 master->name, switch_id);
472 for (i = 0; i <= n_ts; i++) {
473 u8 ts_id, source_port, dir;
477 source_port = (buf[1] & GENMASK(7, 4)) >> 4;
478 dir = (buf[1] & BIT(3)) >> 3;
479 tstamp = be64_to_cpu(*(__be64 *)(buf + 2));
481 sja1110_process_meta_tstamp(ds, source_port, ts_id, dir,
484 buf += SJA1110_META_TSTAMP_SIZE;
487 /* Discard the meta frame, we've consumed the timestamps it contained */
491 static struct sk_buff *sja1110_rcv_inband_control_extension(struct sk_buff *skb,
498 if (unlikely(!pskb_may_pull(skb, SJA1110_HEADER_LEN)))
501 /* skb->data points to skb_mac_header(skb) + ETH_HLEN, which is exactly
502 * what we need because the caller has checked the EtherType (which is
503 * located 2 bytes back) and we just need a pointer to the header that
506 rx_header = ntohs(*(__be16 *)skb->data);
508 if (rx_header & SJA1110_RX_HEADER_HOST_ONLY)
511 if (rx_header & SJA1110_RX_HEADER_IS_METADATA)
512 return sja1110_rcv_meta(skb, rx_header);
514 /* Timestamp frame, we have a trailer */
515 if (rx_header & SJA1110_RX_HEADER_HAS_TRAILER) {
516 int start_of_padding = SJA1110_RX_HEADER_TRAILER_POS(rx_header);
517 u8 *rx_trailer = skb_tail_pointer(skb) - SJA1110_RX_TRAILER_LEN;
518 u64 *tstamp = &SJA1105_SKB_CB(skb)->tstamp;
519 u8 last_byte = rx_trailer[12];
521 /* The timestamp is unaligned, so we need to use packing()
524 packing(rx_trailer, tstamp, 63, 0, 8, UNPACK, 0);
526 *source_port = SJA1110_RX_TRAILER_SRC_PORT(last_byte);
527 *switch_id = SJA1110_RX_TRAILER_SWITCH_ID(last_byte);
529 /* skb->len counts from skb->data, while start_of_padding
530 * counts from the destination MAC address. Right now skb->data
531 * is still as set by the DSA master, so to trim away the
532 * padding and trailer we need to account for the fact that
533 * skb->data points to skb_mac_header(skb) + ETH_HLEN.
535 pskb_trim_rcsum(skb, start_of_padding - ETH_HLEN);
536 /* Trap-to-host frame, no timestamp trailer */
538 *source_port = SJA1110_RX_HEADER_SRC_PORT(rx_header);
539 *switch_id = SJA1110_RX_HEADER_SWITCH_ID(rx_header);
542 /* Advance skb->data past the DSA header */
543 skb_pull_rcsum(skb, SJA1110_HEADER_LEN);
545 dsa_strip_etype_header(skb, SJA1110_HEADER_LEN);
547 /* With skb->data in its final place, update the MAC header
548 * so that eth_hdr() continues to works properly.
550 skb_set_mac_header(skb, -ETH_HLEN);
555 static struct sk_buff *sja1110_rcv(struct sk_buff *skb,
556 struct net_device *netdev)
558 int source_port = -1, switch_id = -1;
559 bool host_only = false;
562 if (sja1110_skb_has_inband_control_extension(skb)) {
563 skb = sja1110_rcv_inband_control_extension(skb, &source_port,
570 /* Packets with in-band control extensions might still have RX VLANs */
571 if (likely(sja1105_skb_has_tag_8021q(skb)))
572 sja1105_vlan_rcv(skb, &source_port, &switch_id, &vid);
574 if (source_port == -1 || switch_id == -1)
575 skb->dev = dsa_find_designated_bridge_port_by_vid(netdev, vid);
577 skb->dev = dsa_master_find_slave(netdev, switch_id, source_port);
579 netdev_warn(netdev, "Couldn't decode source port\n");
584 dsa_default_offload_fwd_mark(skb);
589 static void sja1105_flow_dissect(const struct sk_buff *skb, __be16 *proto,
592 /* No tag added for management frames, all ok */
593 if (unlikely(sja1105_is_link_local(skb)))
596 dsa_tag_generic_flow_dissect(skb, proto, offset);
599 static void sja1110_flow_dissect(const struct sk_buff *skb, __be16 *proto,
602 /* Management frames have 2 DSA tags on RX, so the needed_headroom we
603 * declared is fine for the generic dissector adjustment procedure.
605 if (unlikely(sja1105_is_link_local(skb)))
606 return dsa_tag_generic_flow_dissect(skb, proto, offset);
608 /* For the rest, there is a single DSA tag, the tag_8021q one */
610 *proto = ((__be16 *)skb->data)[(VLAN_HLEN / 2) - 1];
613 static const struct dsa_device_ops sja1105_netdev_ops = {
615 .proto = DSA_TAG_PROTO_SJA1105,
616 .xmit = sja1105_xmit,
618 .needed_headroom = VLAN_HLEN,
619 .flow_dissect = sja1105_flow_dissect,
620 .promisc_on_master = true,
623 DSA_TAG_DRIVER(sja1105_netdev_ops);
624 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SJA1105);
626 static const struct dsa_device_ops sja1110_netdev_ops = {
628 .proto = DSA_TAG_PROTO_SJA1110,
629 .xmit = sja1110_xmit,
631 .flow_dissect = sja1110_flow_dissect,
632 .needed_headroom = SJA1110_HEADER_LEN + VLAN_HLEN,
633 .needed_tailroom = SJA1110_RX_TRAILER_LEN + SJA1110_MAX_PADDING_LEN,
636 DSA_TAG_DRIVER(sja1110_netdev_ops);
637 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SJA1110);
639 static struct dsa_tag_driver *sja1105_tag_driver_array[] = {
640 &DSA_TAG_DRIVER_NAME(sja1105_netdev_ops),
641 &DSA_TAG_DRIVER_NAME(sja1110_netdev_ops),
644 module_dsa_tag_drivers(sja1105_tag_driver_array);
646 MODULE_LICENSE("GPL v2");