xref: /linux/net/dsa/tag_sja1105.c (revision 87320be9f0d24fce67631b7eef919f0b79c3e45c)
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 
9 #include "tag.h"
10 #include "tag_8021q.h"
11 
12 #define SJA1105_NAME				"sja1105"
13 #define SJA1110_NAME				"sja1110"
14 
15 /* Is this a TX or an RX header? */
16 #define SJA1110_HEADER_HOST_TO_SWITCH		BIT(15)
17 
18 /* RX header */
19 #define SJA1110_RX_HEADER_IS_METADATA		BIT(14)
20 #define SJA1110_RX_HEADER_HOST_ONLY		BIT(13)
21 #define SJA1110_RX_HEADER_HAS_TRAILER		BIT(12)
22 
23 /* Trap-to-host format (no trailer present) */
24 #define SJA1110_RX_HEADER_SRC_PORT(x)		(((x) & GENMASK(7, 4)) >> 4)
25 #define SJA1110_RX_HEADER_SWITCH_ID(x)		((x) & GENMASK(3, 0))
26 
27 /* Timestamp format (trailer present) */
28 #define SJA1110_RX_HEADER_TRAILER_POS(x)	((x) & GENMASK(11, 0))
29 
30 #define SJA1110_RX_TRAILER_SWITCH_ID(x)		(((x) & GENMASK(7, 4)) >> 4)
31 #define SJA1110_RX_TRAILER_SRC_PORT(x)		((x) & GENMASK(3, 0))
32 
33 /* Meta frame format (for 2-step TX timestamps) */
34 #define SJA1110_RX_HEADER_N_TS(x)		(((x) & GENMASK(8, 4)) >> 4)
35 
36 /* TX header */
37 #define SJA1110_TX_HEADER_UPDATE_TC		BIT(14)
38 #define SJA1110_TX_HEADER_TAKE_TS		BIT(13)
39 #define SJA1110_TX_HEADER_TAKE_TS_CASC		BIT(12)
40 #define SJA1110_TX_HEADER_HAS_TRAILER		BIT(11)
41 
42 /* Only valid if SJA1110_TX_HEADER_HAS_TRAILER is false */
43 #define SJA1110_TX_HEADER_PRIO(x)		(((x) << 7) & GENMASK(10, 7))
44 #define SJA1110_TX_HEADER_TSTAMP_ID(x)		((x) & GENMASK(7, 0))
45 
46 /* Only valid if SJA1110_TX_HEADER_HAS_TRAILER is true */
47 #define SJA1110_TX_HEADER_TRAILER_POS(x)	((x) & GENMASK(10, 0))
48 
49 #define SJA1110_TX_TRAILER_TSTAMP_ID(x)		(((x) << 24) & GENMASK(31, 24))
50 #define SJA1110_TX_TRAILER_PRIO(x)		(((x) << 21) & GENMASK(23, 21))
51 #define SJA1110_TX_TRAILER_SWITCHID(x)		(((x) << 12) & GENMASK(15, 12))
52 #define SJA1110_TX_TRAILER_DESTPORTS(x)		(((x) << 1) & GENMASK(11, 1))
53 
54 #define SJA1110_META_TSTAMP_SIZE		10
55 
56 #define SJA1110_HEADER_LEN			4
57 #define SJA1110_RX_TRAILER_LEN			13
58 #define SJA1110_TX_TRAILER_LEN			4
59 #define SJA1110_MAX_PADDING_LEN			15
60 
61 struct sja1105_tagger_private {
62 	struct sja1105_tagger_data data; /* Must be first */
63 	/* Protects concurrent access to the meta state machine
64 	 * from taggers running on multiple ports on SMP systems
65 	 */
66 	spinlock_t meta_lock;
67 	struct sk_buff *stampable_skb;
68 	struct kthread_worker *xmit_worker;
69 };
70 
71 static struct sja1105_tagger_private *
sja1105_tagger_private(struct dsa_switch * ds)72 sja1105_tagger_private(struct dsa_switch *ds)
73 {
74 	return ds->tagger_data;
75 }
76 
77 /* Similar to is_link_local_ether_addr(hdr->h_dest) but also covers PTP */
sja1105_is_link_local(const struct sk_buff * skb)78 static bool sja1105_is_link_local(const struct sk_buff *skb)
79 {
80 	const struct ethhdr *hdr = eth_hdr(skb);
81 	u64 dmac = ether_addr_to_u64(hdr->h_dest);
82 
83 	if (ntohs(hdr->h_proto) == ETH_P_SJA1105_META)
84 		return false;
85 	if ((dmac & SJA1105_LINKLOCAL_FILTER_A_MASK) ==
86 		    SJA1105_LINKLOCAL_FILTER_A)
87 		return true;
88 	if ((dmac & SJA1105_LINKLOCAL_FILTER_B_MASK) ==
89 		    SJA1105_LINKLOCAL_FILTER_B)
90 		return true;
91 	return false;
92 }
93 
94 struct sja1105_meta {
95 	u64 tstamp;
96 	u64 dmac_byte_4;
97 	u64 dmac_byte_3;
98 	u64 source_port;
99 	u64 switch_id;
100 };
101 
sja1105_meta_unpack(const struct sk_buff * skb,struct sja1105_meta * meta)102 static void sja1105_meta_unpack(const struct sk_buff *skb,
103 				struct sja1105_meta *meta)
104 {
105 	u8 *buf = skb_mac_header(skb) + ETH_HLEN;
106 
107 	/* UM10944.pdf section 4.2.17 AVB Parameters:
108 	 * Structure of the meta-data follow-up frame.
109 	 * It is in network byte order, so there are no quirks
110 	 * while unpacking the meta frame.
111 	 *
112 	 * Also SJA1105 E/T only populates bits 23:0 of the timestamp
113 	 * whereas P/Q/R/S does 32 bits. Since the structure is the
114 	 * same and the E/T puts zeroes in the high-order byte, use
115 	 * a unified unpacking command for both device series.
116 	 */
117 	packing(buf,     &meta->tstamp,     31, 0, 4, UNPACK, 0);
118 	packing(buf + 4, &meta->dmac_byte_3, 7, 0, 1, UNPACK, 0);
119 	packing(buf + 5, &meta->dmac_byte_4, 7, 0, 1, UNPACK, 0);
120 	packing(buf + 6, &meta->source_port, 7, 0, 1, UNPACK, 0);
121 	packing(buf + 7, &meta->switch_id,   7, 0, 1, UNPACK, 0);
122 }
123 
sja1105_is_meta_frame(const struct sk_buff * skb)124 static bool sja1105_is_meta_frame(const struct sk_buff *skb)
125 {
126 	const struct ethhdr *hdr = eth_hdr(skb);
127 	u64 smac = ether_addr_to_u64(hdr->h_source);
128 	u64 dmac = ether_addr_to_u64(hdr->h_dest);
129 
130 	if (smac != SJA1105_META_SMAC)
131 		return false;
132 	if (dmac != SJA1105_META_DMAC)
133 		return false;
134 	if (ntohs(hdr->h_proto) != ETH_P_SJA1105_META)
135 		return false;
136 	return true;
137 }
138 
139 /* Calls sja1105_port_deferred_xmit in sja1105_main.c */
sja1105_defer_xmit(struct dsa_port * dp,struct sk_buff * skb)140 static struct sk_buff *sja1105_defer_xmit(struct dsa_port *dp,
141 					  struct sk_buff *skb)
142 {
143 	struct sja1105_tagger_data *tagger_data = sja1105_tagger_data(dp->ds);
144 	struct sja1105_tagger_private *priv = sja1105_tagger_private(dp->ds);
145 	void (*xmit_work_fn)(struct kthread_work *work);
146 	struct sja1105_deferred_xmit_work *xmit_work;
147 	struct kthread_worker *xmit_worker;
148 
149 	xmit_work_fn = tagger_data->xmit_work_fn;
150 	xmit_worker = priv->xmit_worker;
151 
152 	if (!xmit_work_fn || !xmit_worker) {
153 		kfree_skb(skb);
154 		return NULL;
155 	}
156 
157 	xmit_work = kzalloc_obj(*xmit_work, GFP_ATOMIC);
158 	if (!xmit_work) {
159 		kfree_skb(skb);
160 		return NULL;
161 	}
162 
163 	kthread_init_work(&xmit_work->work, xmit_work_fn);
164 	xmit_work->dp = dp;
165 	xmit_work->skb = skb;
166 
167 	kthread_queue_work(xmit_worker, &xmit_work->work);
168 
169 	return NULL;
170 }
171 
172 /* Send VLAN tags with a TPID that blends in with whatever VLAN protocol a
173  * bridge spanning ports of this switch might have.
174  */
sja1105_xmit_tpid(struct dsa_port * dp)175 static u16 sja1105_xmit_tpid(struct dsa_port *dp)
176 {
177 	struct dsa_switch *ds = dp->ds;
178 	struct dsa_port *other_dp;
179 	u16 proto;
180 
181 	/* Since VLAN awareness is global, then if this port is VLAN-unaware,
182 	 * all ports are. Use the VLAN-unaware TPID used for tag_8021q.
183 	 */
184 	if (!dsa_port_is_vlan_filtering(dp))
185 		return ETH_P_SJA1105;
186 
187 	/* Port is VLAN-aware, so there is a bridge somewhere (a single one,
188 	 * we're sure about that). It may not be on this port though, so we
189 	 * need to find it.
190 	 */
191 	dsa_switch_for_each_port(other_dp, ds) {
192 		struct net_device *br = dsa_port_bridge_dev_get(other_dp);
193 
194 		if (!br)
195 			continue;
196 
197 		/* Error is returned only if CONFIG_BRIDGE_VLAN_FILTERING,
198 		 * which seems pointless to handle, as our port cannot become
199 		 * VLAN-aware in that case.
200 		 */
201 		br_vlan_get_proto(br, &proto);
202 
203 		return proto;
204 	}
205 
206 	WARN_ONCE(1, "Port is VLAN-aware but cannot find associated bridge!\n");
207 
208 	return ETH_P_SJA1105;
209 }
210 
sja1105_imprecise_xmit(struct sk_buff * skb,struct net_device * netdev)211 static struct sk_buff *sja1105_imprecise_xmit(struct sk_buff *skb,
212 					      struct net_device *netdev)
213 {
214 	struct dsa_port *dp = dsa_user_to_port(netdev);
215 	unsigned int bridge_num = dsa_port_bridge_num_get(dp);
216 	struct net_device *br = dsa_port_bridge_dev_get(dp);
217 	u16 tx_vid;
218 
219 	/* If the port is under a VLAN-aware bridge, just slide the
220 	 * VLAN-tagged packet into the FDB and hope for the best.
221 	 * This works because we support a single VLAN-aware bridge
222 	 * across the entire dst, and its VLANs cannot be shared with
223 	 * any standalone port.
224 	 */
225 	if (br_vlan_enabled(br))
226 		return skb;
227 
228 	/* If the port is under a VLAN-unaware bridge, use an imprecise
229 	 * TX VLAN that targets the bridge's entire broadcast domain,
230 	 * instead of just the specific port.
231 	 */
232 	tx_vid = dsa_tag_8021q_bridge_vid(bridge_num);
233 
234 	return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp), tx_vid);
235 }
236 
237 /* Transform untagged control packets into pvid-tagged control packets so that
238  * all packets sent by this tagger are VLAN-tagged and we can configure the
239  * switch to drop untagged packets coming from the DSA conduit.
240  */
sja1105_pvid_tag_control_pkt(struct dsa_port * dp,struct sk_buff * skb,u8 pcp)241 static struct sk_buff *sja1105_pvid_tag_control_pkt(struct dsa_port *dp,
242 						    struct sk_buff *skb, u8 pcp)
243 {
244 	__be16 xmit_tpid = htons(sja1105_xmit_tpid(dp));
245 	struct vlan_ethhdr *hdr;
246 
247 	/* If VLAN tag is in hwaccel area, move it to the payload
248 	 * to deal with both cases uniformly and to ensure that
249 	 * the VLANs are added in the right order.
250 	 */
251 	if (unlikely(skb_vlan_tag_present(skb))) {
252 		skb = __vlan_hwaccel_push_inside(skb);
253 		if (!skb)
254 			return NULL;
255 	}
256 
257 	hdr = skb_vlan_eth_hdr(skb);
258 
259 	/* If skb is already VLAN-tagged, leave that VLAN ID in place */
260 	if (hdr->h_vlan_proto == xmit_tpid)
261 		return skb;
262 
263 	return vlan_insert_tag(skb, xmit_tpid, (pcp << VLAN_PRIO_SHIFT) |
264 			       SJA1105_DEFAULT_VLAN);
265 }
266 
sja1105_xmit(struct sk_buff * skb,struct net_device * netdev)267 static struct sk_buff *sja1105_xmit(struct sk_buff *skb,
268 				    struct net_device *netdev)
269 {
270 	struct dsa_port *dp = dsa_user_to_port(netdev);
271 	u16 queue_mapping = skb_get_queue_mapping(skb);
272 	u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
273 	u16 tx_vid = dsa_tag_8021q_standalone_vid(dp);
274 
275 	if (skb->offload_fwd_mark)
276 		return sja1105_imprecise_xmit(skb, netdev);
277 
278 	/* Transmitting management traffic does not rely upon switch tagging,
279 	 * but instead SPI-installed management routes. Part 2 of this
280 	 * is the .port_deferred_xmit driver callback.
281 	 */
282 	if (unlikely(sja1105_is_link_local(skb))) {
283 		skb = sja1105_pvid_tag_control_pkt(dp, skb, pcp);
284 		if (!skb)
285 			return NULL;
286 
287 		return sja1105_defer_xmit(dp, skb);
288 	}
289 
290 	return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp),
291 			     ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
292 }
293 
sja1110_xmit(struct sk_buff * skb,struct net_device * netdev)294 static struct sk_buff *sja1110_xmit(struct sk_buff *skb,
295 				    struct net_device *netdev)
296 {
297 	struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
298 	struct dsa_port *dp = dsa_user_to_port(netdev);
299 	u16 queue_mapping = skb_get_queue_mapping(skb);
300 	u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
301 	u16 tx_vid = dsa_tag_8021q_standalone_vid(dp);
302 	__be32 *tx_trailer;
303 	__be16 *tx_header;
304 	int trailer_pos;
305 
306 	if (skb->offload_fwd_mark)
307 		return sja1105_imprecise_xmit(skb, netdev);
308 
309 	/* Transmitting control packets is done using in-band control
310 	 * extensions, while data packets are transmitted using
311 	 * tag_8021q TX VLANs.
312 	 */
313 	if (likely(!sja1105_is_link_local(skb)))
314 		return dsa_8021q_xmit(skb, netdev, sja1105_xmit_tpid(dp),
315 				     ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
316 
317 	skb = sja1105_pvid_tag_control_pkt(dp, skb, pcp);
318 	if (!skb)
319 		return NULL;
320 
321 	skb_push(skb, SJA1110_HEADER_LEN);
322 
323 	dsa_alloc_etype_header(skb, SJA1110_HEADER_LEN);
324 
325 	trailer_pos = skb->len;
326 
327 	tx_header = dsa_etype_header_pos_tx(skb);
328 	tx_trailer = skb_put(skb, SJA1110_TX_TRAILER_LEN);
329 
330 	tx_header[0] = htons(ETH_P_SJA1110);
331 	tx_header[1] = htons(SJA1110_HEADER_HOST_TO_SWITCH |
332 			     SJA1110_TX_HEADER_HAS_TRAILER |
333 			     SJA1110_TX_HEADER_TRAILER_POS(trailer_pos));
334 	*tx_trailer = cpu_to_be32(SJA1110_TX_TRAILER_PRIO(pcp) |
335 				  SJA1110_TX_TRAILER_SWITCHID(dp->ds->index) |
336 				  SJA1110_TX_TRAILER_DESTPORTS(BIT(dp->index)));
337 	if (clone) {
338 		u8 ts_id = SJA1105_SKB_CB(clone)->ts_id;
339 
340 		tx_header[1] |= htons(SJA1110_TX_HEADER_TAKE_TS);
341 		*tx_trailer |= cpu_to_be32(SJA1110_TX_TRAILER_TSTAMP_ID(ts_id));
342 	}
343 
344 	return skb;
345 }
346 
sja1105_transfer_meta(struct sk_buff * skb,const struct sja1105_meta * meta)347 static void sja1105_transfer_meta(struct sk_buff *skb,
348 				  const struct sja1105_meta *meta)
349 {
350 	struct ethhdr *hdr = eth_hdr(skb);
351 
352 	hdr->h_dest[3] = meta->dmac_byte_3;
353 	hdr->h_dest[4] = meta->dmac_byte_4;
354 	SJA1105_SKB_CB(skb)->tstamp = meta->tstamp;
355 }
356 
357 /* This is a simple state machine which follows the hardware mechanism of
358  * generating RX timestamps:
359  *
360  * After each timestampable skb (all traffic for which send_meta1 and
361  * send_meta0 is true, aka all MAC-filtered link-local traffic) a meta frame
362  * containing a partial timestamp is immediately generated by the switch and
363  * sent as a follow-up to the link-local frame on the CPU port.
364  *
365  * The meta frames have no unique identifier (such as sequence number) by which
366  * one may pair them to the correct timestampable frame.
367  * Instead, the switch has internal logic that ensures no frames are sent on
368  * the CPU port between a link-local timestampable frame and its corresponding
369  * meta follow-up. It also ensures strict ordering between ports (lower ports
370  * have higher priority towards the CPU port). For this reason, a per-port
371  * data structure is not needed/desirable.
372  *
373  * This function pairs the link-local frame with its partial timestamp from the
374  * meta follow-up frame. The full timestamp will be reconstructed later in a
375  * work queue.
376  */
377 static struct sk_buff
sja1105_rcv_meta_state_machine(struct sk_buff * skb,struct sja1105_meta * meta,bool is_link_local,bool is_meta)378 *sja1105_rcv_meta_state_machine(struct sk_buff *skb,
379 				struct sja1105_meta *meta,
380 				bool is_link_local,
381 				bool is_meta)
382 {
383 	/* Step 1: A timestampable frame was received.
384 	 * Buffer it until we get its meta frame.
385 	 */
386 	if (is_link_local) {
387 		struct dsa_port *dp = dsa_user_to_port(skb->dev);
388 		struct sja1105_tagger_private *priv;
389 		struct dsa_switch *ds = dp->ds;
390 
391 		priv = sja1105_tagger_private(ds);
392 
393 		spin_lock(&priv->meta_lock);
394 		/* Was this a link-local frame instead of the meta
395 		 * that we were expecting?
396 		 */
397 		if (priv->stampable_skb) {
398 			dev_err_ratelimited(ds->dev,
399 					    "Expected meta frame, is %12llx "
400 					    "in the DSA conduit multicast filter?\n",
401 					    SJA1105_META_DMAC);
402 			kfree_skb(priv->stampable_skb);
403 		}
404 
405 		priv->stampable_skb = skb;
406 		spin_unlock(&priv->meta_lock);
407 
408 		/* Tell DSA we got nothing */
409 		return NULL;
410 
411 	/* Step 2: The meta frame arrived.
412 	 * Time to take the stampable skb out of the closet, annotate it
413 	 * with the partial timestamp, and pretend that we received it
414 	 * just now (basically masquerade the buffered frame as the meta
415 	 * frame, which serves no further purpose).
416 	 */
417 	} else if (is_meta) {
418 		struct dsa_port *dp = dsa_user_to_port(skb->dev);
419 		struct sja1105_tagger_private *priv;
420 		struct dsa_switch *ds = dp->ds;
421 		struct sk_buff *stampable_skb;
422 
423 		priv = sja1105_tagger_private(ds);
424 
425 		spin_lock(&priv->meta_lock);
426 
427 		stampable_skb = priv->stampable_skb;
428 		priv->stampable_skb = NULL;
429 
430 		/* Was this a meta frame instead of the link-local
431 		 * that we were expecting?
432 		 */
433 		if (!stampable_skb) {
434 			dev_err_ratelimited(ds->dev,
435 					    "Unexpected meta frame\n");
436 			spin_unlock(&priv->meta_lock);
437 			kfree_skb(skb);
438 			return NULL;
439 		}
440 
441 		if (stampable_skb->dev != skb->dev) {
442 			dev_err_ratelimited(ds->dev,
443 					    "Meta frame on wrong port\n");
444 			spin_unlock(&priv->meta_lock);
445 			kfree_skb(skb);
446 			return NULL;
447 		}
448 
449 		/* Free the meta frame and give DSA the buffered stampable_skb
450 		 * for further processing up the network stack.
451 		 */
452 		kfree_skb(skb);
453 		skb = stampable_skb;
454 		sja1105_transfer_meta(skb, meta);
455 
456 		spin_unlock(&priv->meta_lock);
457 	}
458 
459 	return skb;
460 }
461 
sja1105_skb_has_tag_8021q(const struct sk_buff * skb)462 static bool sja1105_skb_has_tag_8021q(const struct sk_buff *skb)
463 {
464 	u16 tpid = ntohs(eth_hdr(skb)->h_proto);
465 
466 	return tpid == ETH_P_SJA1105 || tpid == ETH_P_8021Q ||
467 	       skb_vlan_tag_present(skb);
468 }
469 
sja1110_skb_has_inband_control_extension(const struct sk_buff * skb)470 static bool sja1110_skb_has_inband_control_extension(const struct sk_buff *skb)
471 {
472 	return ntohs(eth_hdr(skb)->h_proto) == ETH_P_SJA1110;
473 }
474 
sja1105_rcv(struct sk_buff * skb,struct net_device * netdev)475 static struct sk_buff *sja1105_rcv(struct sk_buff *skb,
476 				   struct net_device *netdev)
477 {
478 	int source_port = -1, switch_id = -1, vbid = -1, vid = -1;
479 	struct sja1105_meta meta = {0};
480 	struct ethhdr *hdr;
481 	bool is_link_local;
482 	bool is_meta;
483 
484 	hdr = eth_hdr(skb);
485 	is_link_local = sja1105_is_link_local(skb);
486 	is_meta = sja1105_is_meta_frame(skb);
487 
488 	if (is_link_local) {
489 		/* Management traffic path. Switch embeds the switch ID and
490 		 * port ID into bytes of the destination MAC, courtesy of
491 		 * the incl_srcpt options.
492 		 */
493 		source_port = hdr->h_dest[3];
494 		switch_id = hdr->h_dest[4];
495 	} else if (is_meta) {
496 		sja1105_meta_unpack(skb, &meta);
497 		source_port = meta.source_port;
498 		switch_id = meta.switch_id;
499 	}
500 
501 	/* Normal data plane traffic and link-local frames are tagged with
502 	 * a tag_8021q VLAN which we have to strip
503 	 */
504 	if (sja1105_skb_has_tag_8021q(skb)) {
505 		dsa_8021q_rcv(skb, &source_port, &switch_id, &vbid, &vid);
506 	} else if (source_port == -1 && switch_id == -1) {
507 		/* Packets with no source information have no chance of
508 		 * getting accepted, drop them straight away.
509 		 */
510 		kfree_skb(skb);
511 		return NULL;
512 	}
513 
514 	skb->dev = dsa_tag_8021q_find_user(netdev, source_port, switch_id,
515 					   vid, vbid);
516 	if (!skb->dev) {
517 		netdev_warn(netdev, "Couldn't decode source port\n");
518 		kfree_skb(skb);
519 		return NULL;
520 	}
521 
522 	if (!is_link_local)
523 		dsa_default_offload_fwd_mark(skb);
524 
525 	return sja1105_rcv_meta_state_machine(skb, &meta, is_link_local,
526 					      is_meta);
527 }
528 
sja1110_rcv_meta(struct sk_buff * skb,u16 rx_header)529 static struct sk_buff *sja1110_rcv_meta(struct sk_buff *skb, u16 rx_header)
530 {
531 	u8 *buf = dsa_etype_header_pos_rx(skb) + SJA1110_HEADER_LEN;
532 	int switch_id = SJA1110_RX_HEADER_SWITCH_ID(rx_header);
533 	int n_ts = SJA1110_RX_HEADER_N_TS(rx_header);
534 	struct sja1105_tagger_data *tagger_data;
535 	struct net_device *conduit = skb->dev;
536 	struct dsa_port *cpu_dp;
537 	struct dsa_switch *ds;
538 	int i;
539 
540 	cpu_dp = conduit->dsa_ptr;
541 	ds = dsa_switch_find(cpu_dp->dst->index, switch_id);
542 	if (!ds) {
543 		net_err_ratelimited("%s: cannot find switch id %d\n",
544 				    conduit->name, switch_id);
545 		kfree_skb(skb);
546 		return NULL;
547 	}
548 
549 	tagger_data = sja1105_tagger_data(ds);
550 	if (!tagger_data->meta_tstamp_handler) {
551 		kfree_skb(skb);
552 		return NULL;
553 	}
554 
555 	for (i = 0; i <= n_ts; i++) {
556 		u8 ts_id, source_port, dir;
557 		u64 tstamp;
558 
559 		ts_id = buf[0];
560 		source_port = (buf[1] & GENMASK(7, 4)) >> 4;
561 		dir = (buf[1] & BIT(3)) >> 3;
562 		tstamp = be64_to_cpu(*(__be64 *)(buf + 2));
563 
564 		tagger_data->meta_tstamp_handler(ds, source_port, ts_id, dir,
565 						 tstamp);
566 
567 		buf += SJA1110_META_TSTAMP_SIZE;
568 	}
569 
570 	/* Discard the meta frame, we've consumed the timestamps it contained */
571 	kfree_skb(skb);
572 	return NULL;
573 }
574 
sja1110_rcv_inband_control_extension(struct sk_buff * skb,int * source_port,int * switch_id,bool * host_only)575 static struct sk_buff *sja1110_rcv_inband_control_extension(struct sk_buff *skb,
576 							    int *source_port,
577 							    int *switch_id,
578 							    bool *host_only)
579 {
580 	u16 rx_header;
581 
582 	if (unlikely(!pskb_may_pull(skb, SJA1110_HEADER_LEN))) {
583 		kfree_skb(skb);
584 		return NULL;
585 	}
586 
587 	/* skb->data points to skb_mac_header(skb) + ETH_HLEN, which is exactly
588 	 * what we need because the caller has checked the EtherType (which is
589 	 * located 2 bytes back) and we just need a pointer to the header that
590 	 * comes afterwards.
591 	 */
592 	rx_header = ntohs(*(__be16 *)skb->data);
593 
594 	if (rx_header & SJA1110_RX_HEADER_HOST_ONLY)
595 		*host_only = true;
596 
597 	if (rx_header & SJA1110_RX_HEADER_IS_METADATA)
598 		return sja1110_rcv_meta(skb, rx_header);
599 
600 	/* Timestamp frame, we have a trailer */
601 	if (rx_header & SJA1110_RX_HEADER_HAS_TRAILER) {
602 		int start_of_padding = SJA1110_RX_HEADER_TRAILER_POS(rx_header);
603 		u8 *rx_trailer = skb_tail_pointer(skb) - SJA1110_RX_TRAILER_LEN;
604 		u64 *tstamp = &SJA1105_SKB_CB(skb)->tstamp;
605 		u8 last_byte = rx_trailer[12];
606 
607 		/* The timestamp is unaligned, so we need to use packing()
608 		 * to get it
609 		 */
610 		packing(rx_trailer, tstamp, 63, 0, 8, UNPACK, 0);
611 
612 		*source_port = SJA1110_RX_TRAILER_SRC_PORT(last_byte);
613 		*switch_id = SJA1110_RX_TRAILER_SWITCH_ID(last_byte);
614 
615 		/* skb->len counts from skb->data, while start_of_padding
616 		 * counts from the destination MAC address. Right now skb->data
617 		 * is still as set by the DSA conduit, so to trim away the
618 		 * padding and trailer we need to account for the fact that
619 		 * skb->data points to skb_mac_header(skb) + ETH_HLEN.
620 		 */
621 		if (pskb_trim_rcsum(skb, start_of_padding - ETH_HLEN)) {
622 			kfree_skb(skb);
623 			return NULL;
624 		}
625 	/* Trap-to-host frame, no timestamp trailer */
626 	} else {
627 		*source_port = SJA1110_RX_HEADER_SRC_PORT(rx_header);
628 		*switch_id = SJA1110_RX_HEADER_SWITCH_ID(rx_header);
629 	}
630 
631 	/* Advance skb->data past the DSA header */
632 	skb_pull_rcsum(skb, SJA1110_HEADER_LEN);
633 
634 	dsa_strip_etype_header(skb, SJA1110_HEADER_LEN);
635 
636 	/* With skb->data in its final place, update the MAC header
637 	 * so that eth_hdr() continues to works properly.
638 	 */
639 	skb_set_mac_header(skb, -ETH_HLEN);
640 
641 	return skb;
642 }
643 
sja1110_rcv(struct sk_buff * skb,struct net_device * netdev)644 static struct sk_buff *sja1110_rcv(struct sk_buff *skb,
645 				   struct net_device *netdev)
646 {
647 	int source_port = -1, switch_id = -1, vbid = -1, vid = -1;
648 	bool host_only = false;
649 
650 	if (sja1110_skb_has_inband_control_extension(skb)) {
651 		skb = sja1110_rcv_inband_control_extension(skb, &source_port,
652 							   &switch_id,
653 							   &host_only);
654 		if (!skb)
655 			return NULL;
656 	}
657 
658 	/* Packets with in-band control extensions might still have RX VLANs */
659 	if (likely(sja1105_skb_has_tag_8021q(skb)))
660 		dsa_8021q_rcv(skb, &source_port, &switch_id, &vbid, &vid);
661 
662 	skb->dev = dsa_tag_8021q_find_user(netdev, source_port, switch_id,
663 					   vid, vbid);
664 
665 	if (!skb->dev) {
666 		netdev_warn(netdev, "Couldn't decode source port\n");
667 		kfree_skb(skb);
668 		return NULL;
669 	}
670 
671 	if (!host_only)
672 		dsa_default_offload_fwd_mark(skb);
673 
674 	return skb;
675 }
676 
sja1105_flow_dissect(const struct sk_buff * skb,__be16 * proto,int * offset)677 static void sja1105_flow_dissect(const struct sk_buff *skb, __be16 *proto,
678 				 int *offset)
679 {
680 	/* No tag added for management frames, all ok */
681 	if (unlikely(sja1105_is_link_local(skb)))
682 		return;
683 
684 	dsa_tag_generic_flow_dissect(skb, proto, offset);
685 }
686 
sja1110_flow_dissect(const struct sk_buff * skb,__be16 * proto,int * offset)687 static void sja1110_flow_dissect(const struct sk_buff *skb, __be16 *proto,
688 				 int *offset)
689 {
690 	/* Management frames have 2 DSA tags on RX, so the needed_headroom we
691 	 * declared is fine for the generic dissector adjustment procedure.
692 	 */
693 	if (unlikely(sja1105_is_link_local(skb)))
694 		return dsa_tag_generic_flow_dissect(skb, proto, offset);
695 
696 	/* For the rest, there is a single DSA tag, the tag_8021q one */
697 	*offset = VLAN_HLEN;
698 	*proto = ((__be16 *)skb->data)[(VLAN_HLEN / 2) - 1];
699 }
700 
sja1105_disconnect(struct dsa_switch * ds)701 static void sja1105_disconnect(struct dsa_switch *ds)
702 {
703 	struct sja1105_tagger_private *priv = ds->tagger_data;
704 
705 	kthread_destroy_worker(priv->xmit_worker);
706 	kfree(priv);
707 	ds->tagger_data = NULL;
708 }
709 
sja1105_connect(struct dsa_switch * ds)710 static int sja1105_connect(struct dsa_switch *ds)
711 {
712 	struct sja1105_tagger_private *priv;
713 	struct kthread_worker *xmit_worker;
714 	int err;
715 
716 	priv = kzalloc_obj(*priv);
717 	if (!priv)
718 		return -ENOMEM;
719 
720 	spin_lock_init(&priv->meta_lock);
721 
722 	xmit_worker = kthread_run_worker(0, "dsa%d:%d_xmit",
723 					    ds->dst->index, ds->index);
724 	if (IS_ERR(xmit_worker)) {
725 		err = PTR_ERR(xmit_worker);
726 		kfree(priv);
727 		return err;
728 	}
729 
730 	priv->xmit_worker = xmit_worker;
731 	ds->tagger_data = priv;
732 
733 	return 0;
734 }
735 
736 static const struct dsa_device_ops sja1105_netdev_ops = {
737 	.name = SJA1105_NAME,
738 	.proto = DSA_TAG_PROTO_SJA1105,
739 	.xmit = sja1105_xmit,
740 	.rcv = sja1105_rcv,
741 	.connect = sja1105_connect,
742 	.disconnect = sja1105_disconnect,
743 	.needed_headroom = VLAN_HLEN,
744 	.flow_dissect = sja1105_flow_dissect,
745 	.promisc_on_conduit = true,
746 };
747 
748 DSA_TAG_DRIVER(sja1105_netdev_ops);
749 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SJA1105, SJA1105_NAME);
750 
751 static const struct dsa_device_ops sja1110_netdev_ops = {
752 	.name = SJA1110_NAME,
753 	.proto = DSA_TAG_PROTO_SJA1110,
754 	.xmit = sja1110_xmit,
755 	.rcv = sja1110_rcv,
756 	.connect = sja1105_connect,
757 	.disconnect = sja1105_disconnect,
758 	.flow_dissect = sja1110_flow_dissect,
759 	.needed_headroom = SJA1110_HEADER_LEN + VLAN_HLEN,
760 	.needed_tailroom = SJA1110_RX_TRAILER_LEN + SJA1110_MAX_PADDING_LEN,
761 };
762 
763 DSA_TAG_DRIVER(sja1110_netdev_ops);
764 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_SJA1110, SJA1110_NAME);
765 
766 static struct dsa_tag_driver *sja1105_tag_driver_array[] = {
767 	&DSA_TAG_DRIVER_NAME(sja1105_netdev_ops),
768 	&DSA_TAG_DRIVER_NAME(sja1110_netdev_ops),
769 };
770 
771 module_dsa_tag_drivers(sja1105_tag_driver_array);
772 
773 MODULE_DESCRIPTION("DSA tag driver for NXP SJA1105 switches");
774 MODULE_LICENSE("GPL v2");
775