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 /* 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)
13 const struct ethhdr *hdr = eth_hdr(skb);
14 u64 dmac = ether_addr_to_u64(hdr->h_dest);
16 if (ntohs(hdr->h_proto) == ETH_P_SJA1105_META)
18 if ((dmac & SJA1105_LINKLOCAL_FILTER_A_MASK) ==
19 SJA1105_LINKLOCAL_FILTER_A)
21 if ((dmac & SJA1105_LINKLOCAL_FILTER_B_MASK) ==
22 SJA1105_LINKLOCAL_FILTER_B)
35 static void sja1105_meta_unpack(const struct sk_buff *skb,
36 struct sja1105_meta *meta)
38 u8 *buf = skb_mac_header(skb) + ETH_HLEN;
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.
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.
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);
57 static inline bool sja1105_is_meta_frame(const struct sk_buff *skb)
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);
63 if (smac != SJA1105_META_SMAC)
65 if (dmac != SJA1105_META_DMAC)
67 if (ntohs(hdr->h_proto) != ETH_P_SJA1105_META)
72 static bool sja1105_can_use_vlan_as_tags(const struct sk_buff *skb)
74 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
76 if (hdr->h_vlan_proto == htons(ETH_P_SJA1105))
79 if (hdr->h_vlan_proto != htons(ETH_P_8021Q))
82 return vid_is_dsa_8021q(ntohs(hdr->h_vlan_TCI) & VLAN_VID_MASK);
85 /* This is the first time the tagger sees the frame on RX.
86 * Figure out if we can decode it.
88 static bool sja1105_filter(const struct sk_buff *skb, struct net_device *dev)
90 if (sja1105_can_use_vlan_as_tags(skb))
92 if (sja1105_is_link_local(skb))
94 if (sja1105_is_meta_frame(skb))
99 /* Calls sja1105_port_deferred_xmit in sja1105_main.c */
100 static struct sk_buff *sja1105_defer_xmit(struct sja1105_port *sp,
103 /* Increase refcount so the kfree_skb in dsa_slave_xmit
104 * won't really free the packet.
106 skb_queue_tail(&sp->xmit_queue, skb_get(skb));
107 kthread_queue_work(sp->xmit_worker, &sp->xmit_work);
112 static u16 sja1105_xmit_tpid(struct sja1105_port *sp)
114 return sp->xmit_tpid;
117 static struct sk_buff *sja1105_xmit(struct sk_buff *skb,
118 struct net_device *netdev)
120 struct dsa_port *dp = dsa_slave_to_port(netdev);
121 u16 tx_vid = dsa_8021q_tx_vid(dp->ds, dp->index);
122 u16 queue_mapping = skb_get_queue_mapping(skb);
123 u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
125 /* Transmitting management traffic does not rely upon switch tagging,
126 * but instead SPI-installed management routes. Part 2 of this
127 * is the .port_deferred_xmit driver callback.
129 if (unlikely(sja1105_is_link_local(skb)))
130 return sja1105_defer_xmit(dp->priv, skb);
132 return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp->priv),
133 ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
136 static void sja1105_transfer_meta(struct sk_buff *skb,
137 const struct sja1105_meta *meta)
139 struct ethhdr *hdr = eth_hdr(skb);
141 hdr->h_dest[3] = meta->dmac_byte_3;
142 hdr->h_dest[4] = meta->dmac_byte_4;
143 SJA1105_SKB_CB(skb)->meta_tstamp = meta->tstamp;
146 /* This is a simple state machine which follows the hardware mechanism of
147 * generating RX timestamps:
149 * After each timestampable skb (all traffic for which send_meta1 and
150 * send_meta0 is true, aka all MAC-filtered link-local traffic) a meta frame
151 * containing a partial timestamp is immediately generated by the switch and
152 * sent as a follow-up to the link-local frame on the CPU port.
154 * The meta frames have no unique identifier (such as sequence number) by which
155 * one may pair them to the correct timestampable frame.
156 * Instead, the switch has internal logic that ensures no frames are sent on
157 * the CPU port between a link-local timestampable frame and its corresponding
158 * meta follow-up. It also ensures strict ordering between ports (lower ports
159 * have higher priority towards the CPU port). For this reason, a per-port
160 * data structure is not needed/desirable.
162 * This function pairs the link-local frame with its partial timestamp from the
163 * meta follow-up frame. The full timestamp will be reconstructed later in a
166 static struct sk_buff
167 *sja1105_rcv_meta_state_machine(struct sk_buff *skb,
168 struct sja1105_meta *meta,
172 struct sja1105_port *sp;
175 dp = dsa_slave_to_port(skb->dev);
178 /* Step 1: A timestampable frame was received.
179 * Buffer it until we get its meta frame.
182 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state))
183 /* Do normal processing. */
186 spin_lock(&sp->data->meta_lock);
187 /* Was this a link-local frame instead of the meta
188 * that we were expecting?
190 if (sp->data->stampable_skb) {
191 dev_err_ratelimited(dp->ds->dev,
192 "Expected meta frame, is %12llx "
193 "in the DSA master multicast filter?\n",
195 kfree_skb(sp->data->stampable_skb);
198 /* Hold a reference to avoid dsa_switch_rcv
199 * from freeing the skb.
201 sp->data->stampable_skb = skb_get(skb);
202 spin_unlock(&sp->data->meta_lock);
204 /* Tell DSA we got nothing */
207 /* Step 2: The meta frame arrived.
208 * Time to take the stampable skb out of the closet, annotate it
209 * with the partial timestamp, and pretend that we received it
210 * just now (basically masquerade the buffered frame as the meta
211 * frame, which serves no further purpose).
213 } else if (is_meta) {
214 struct sk_buff *stampable_skb;
216 /* Drop the meta frame if we're not in the right state
219 if (!test_bit(SJA1105_HWTS_RX_EN, &sp->data->state))
222 spin_lock(&sp->data->meta_lock);
224 stampable_skb = sp->data->stampable_skb;
225 sp->data->stampable_skb = NULL;
227 /* Was this a meta frame instead of the link-local
228 * that we were expecting?
230 if (!stampable_skb) {
231 dev_err_ratelimited(dp->ds->dev,
232 "Unexpected meta frame\n");
233 spin_unlock(&sp->data->meta_lock);
237 if (stampable_skb->dev != skb->dev) {
238 dev_err_ratelimited(dp->ds->dev,
239 "Meta frame on wrong port\n");
240 spin_unlock(&sp->data->meta_lock);
244 /* Free the meta frame and give DSA the buffered stampable_skb
245 * for further processing up the network stack.
249 sja1105_transfer_meta(skb, meta);
251 spin_unlock(&sp->data->meta_lock);
257 static void sja1105_decode_subvlan(struct sk_buff *skb, u16 subvlan)
259 struct dsa_port *dp = dsa_slave_to_port(skb->dev);
260 struct sja1105_port *sp = dp->priv;
261 u16 vid = sp->subvlan_map[subvlan];
264 if (vid == VLAN_N_VID)
267 vlan_tci = (skb->priority << VLAN_PRIO_SHIFT) | vid;
268 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
271 static struct sk_buff *sja1105_rcv(struct sk_buff *skb,
272 struct net_device *netdev,
273 struct packet_type *pt)
275 struct sja1105_meta meta = {0};
276 int source_port, switch_id;
285 tpid = ntohs(hdr->h_proto);
286 is_tagged = (tpid == ETH_P_SJA1105 || tpid == ETH_P_8021Q);
287 is_link_local = sja1105_is_link_local(skb);
288 is_meta = sja1105_is_meta_frame(skb);
290 skb->offload_fwd_mark = 1;
293 /* Normal traffic path. */
294 skb_push_rcsum(skb, ETH_HLEN);
295 __skb_vlan_pop(skb, &tci);
296 skb_pull_rcsum(skb, ETH_HLEN);
297 skb_reset_network_header(skb);
298 skb_reset_transport_header(skb);
300 vid = tci & VLAN_VID_MASK;
301 source_port = dsa_8021q_rx_source_port(vid);
302 switch_id = dsa_8021q_rx_switch_id(vid);
303 skb->priority = (tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
304 subvlan = dsa_8021q_rx_subvlan(vid);
305 } else if (is_link_local) {
306 /* Management traffic path. Switch embeds the switch ID and
307 * port ID into bytes of the destination MAC, courtesy of
308 * the incl_srcpt options.
310 source_port = hdr->h_dest[3];
311 switch_id = hdr->h_dest[4];
312 /* Clear the DMAC bytes that were mangled by the switch */
315 } else if (is_meta) {
316 sja1105_meta_unpack(skb, &meta);
317 source_port = meta.source_port;
318 switch_id = meta.switch_id;
323 skb->dev = dsa_master_find_slave(netdev, switch_id, source_port);
325 netdev_warn(netdev, "Couldn't decode source port\n");
330 sja1105_decode_subvlan(skb, subvlan);
332 return sja1105_rcv_meta_state_machine(skb, &meta, is_link_local,
336 static const struct dsa_device_ops sja1105_netdev_ops = {
338 .proto = DSA_TAG_PROTO_SJA1105,
339 .xmit = sja1105_xmit,
341 .filter = sja1105_filter,
342 .overhead = VLAN_HLEN,
345 MODULE_LICENSE("GPL v2");
346 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SJA1105);
348 module_dsa_tag_driver(sja1105_netdev_ops);