1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Microchip switch tag common header
3  *
4  * Copyright (C) 2022 Microchip Technology Inc.
5  */
6 
7 #ifndef _NET_DSA_KSZ_COMMON_H_
8 #define _NET_DSA_KSZ_COMMON_H_
9 
10 #include <net/dsa.h>
11 
12 /* All time stamps from the KSZ consist of 2 bits for seconds and 30 bits for
13  * nanoseconds. This is NOT the same as 32 bits for nanoseconds.
14  */
15 #define KSZ_TSTAMP_SEC_MASK  GENMASK(31, 30)
16 #define KSZ_TSTAMP_NSEC_MASK GENMASK(29, 0)
17 
ksz_decode_tstamp(u32 tstamp)18 static inline ktime_t ksz_decode_tstamp(u32 tstamp)
19 {
20 	u64 ns = FIELD_GET(KSZ_TSTAMP_SEC_MASK, tstamp) * NSEC_PER_SEC +
21 		 FIELD_GET(KSZ_TSTAMP_NSEC_MASK, tstamp);
22 
23 	return ns_to_ktime(ns);
24 }
25 
26 struct ksz_deferred_xmit_work {
27 	struct dsa_port *dp;
28 	struct sk_buff *skb;
29 	struct kthread_work work;
30 };
31 
32 struct ksz_tagger_data {
33 	void (*xmit_work_fn)(struct kthread_work *work);
34 	void (*hwtstamp_set_state)(struct dsa_switch *ds, bool on);
35 };
36 
37 struct ksz_skb_cb {
38 	struct sk_buff *clone;
39 	unsigned int ptp_type;
40 	bool update_correction;
41 	u32 tstamp;
42 };
43 
44 #define KSZ_SKB_CB(skb) \
45 	((struct ksz_skb_cb *)((skb)->cb))
46 
47 static inline struct ksz_tagger_data *
ksz_tagger_data(struct dsa_switch * ds)48 ksz_tagger_data(struct dsa_switch *ds)
49 {
50 	return ds->tagger_data;
51 }
52 
53 #endif /* _NET_DSA_KSZ_COMMON_H_ */
54