1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2021, Intel Corporation. */
3 
4 #ifndef _ICE_PTP_H_
5 #define _ICE_PTP_H_
6 
7 #include <linux/ptp_clock_kernel.h>
8 #include <linux/kthread.h>
9 
10 #include "ice_ptp_hw.h"
11 
12 enum ice_ptp_pin_e810 {
13 	GPIO_20 = 0,
14 	GPIO_21,
15 	GPIO_22,
16 	GPIO_23,
17 	NUM_PTP_PIN_E810
18 };
19 
20 enum ice_ptp_pin_e810t {
21 	GNSS = 0,
22 	SMA1,
23 	UFL1,
24 	SMA2,
25 	UFL2,
26 	NUM_PTP_PINS_E810T
27 };
28 
29 struct ice_perout_channel {
30 	bool ena;
31 	u32 gpio_pin;
32 	u64 period;
33 	u64 start_time;
34 };
35 
36 /* The ice hardware captures Tx hardware timestamps in the PHY. The timestamp
37  * is stored in a buffer of registers. Depending on the specific hardware,
38  * this buffer might be shared across multiple PHY ports.
39  *
40  * On transmit of a packet to be timestamped, software is responsible for
41  * selecting an open index. Hardware makes no attempt to lock or prevent
42  * re-use of an index for multiple packets.
43  *
44  * To handle this, timestamp indexes must be tracked by software to ensure
45  * that an index is not re-used for multiple transmitted packets. The
46  * structures and functions declared in this file track the available Tx
47  * register indexes, as well as provide storage for the SKB pointers.
48  *
49  * To allow multiple ports to access the shared register block independently,
50  * the blocks are split up so that indexes are assigned to each port based on
51  * hardware logical port number.
52  */
53 
54 /**
55  * struct ice_tx_tstamp - Tracking for a single Tx timestamp
56  * @skb: pointer to the SKB for this timestamp request
57  * @start: jiffies when the timestamp was first requested
58  *
59  * This structure tracks a single timestamp request. The SKB pointer is
60  * provided when initiating a request. The start time is used to ensure that
61  * we discard old requests that were not fulfilled within a 2 second time
62  * window.
63  */
64 struct ice_tx_tstamp {
65 	struct sk_buff *skb;
66 	unsigned long start;
67 };
68 
69 /**
70  * struct ice_ptp_tx - Tracking structure for all Tx timestamp requests on a port
71  * @work: work function to handle processing of Tx timestamps
72  * @lock: lock to prevent concurrent write to in_use bitmap
73  * @tstamps: array of len to store outstanding requests
74  * @in_use: bitmap of len to indicate which slots are in use
75  * @quad: which quad the timestamps are captured in
76  * @quad_offset: offset into timestamp block of the quad to get the real index
77  * @len: length of the tstamps and in_use fields.
78  * @init: if true, the tracker is initialized;
79  */
80 struct ice_ptp_tx {
81 	struct kthread_work work;
82 	spinlock_t lock; /* lock protecting in_use bitmap */
83 	struct ice_tx_tstamp *tstamps;
84 	unsigned long *in_use;
85 	u8 quad;
86 	u8 quad_offset;
87 	u8 len;
88 	u8 init;
89 };
90 
91 /* Quad and port information for initializing timestamp blocks */
92 #define INDEX_PER_QUAD			64
93 #define INDEX_PER_PORT			(INDEX_PER_QUAD / ICE_PORTS_PER_QUAD)
94 
95 /**
96  * struct ice_ptp_port - data used to initialize an external port for PTP
97  *
98  * This structure contains PTP data related to the external ports. Currently
99  * it is used for tracking the Tx timestamps of a port. In the future this
100  * structure will also hold information for the E822 port initialization
101  * logic.
102  *
103  * @tx: Tx timestamp tracking for this port
104  */
105 struct ice_ptp_port {
106 	struct ice_ptp_tx tx;
107 };
108 
109 #define GLTSYN_TGT_H_IDX_MAX		4
110 
111 /**
112  * struct ice_ptp - data used for integrating with CONFIG_PTP_1588_CLOCK
113  * @port: data for the PHY port initialization procedure
114  * @work: delayed work function for periodic tasks
115  * @extts_work: work function for handling external Tx timestamps
116  * @cached_phc_time: a cached copy of the PHC time for timestamp extension
117  * @ext_ts_chan: the external timestamp channel in use
118  * @ext_ts_irq: the external timestamp IRQ in use
119  * @kworker: kwork thread for handling periodic work
120  * @perout_channels: periodic output data
121  * @info: structure defining PTP hardware capabilities
122  * @clock: pointer to registered PTP clock device
123  * @tstamp_config: hardware timestamping configuration
124  */
125 struct ice_ptp {
126 	struct ice_ptp_port port;
127 	struct kthread_delayed_work work;
128 	struct kthread_work extts_work;
129 	u64 cached_phc_time;
130 	u8 ext_ts_chan;
131 	u8 ext_ts_irq;
132 	struct kthread_worker *kworker;
133 	struct ice_perout_channel perout_channels[GLTSYN_TGT_H_IDX_MAX];
134 	struct ptp_clock_info info;
135 	struct ptp_clock *clock;
136 	struct hwtstamp_config tstamp_config;
137 };
138 
139 #define __ptp_port_to_ptp(p) \
140 	container_of((p), struct ice_ptp, port)
141 #define ptp_port_to_pf(p) \
142 	container_of(__ptp_port_to_ptp((p)), struct ice_pf, ptp)
143 
144 #define __ptp_info_to_ptp(i) \
145 	container_of((i), struct ice_ptp, info)
146 #define ptp_info_to_pf(i) \
147 	container_of(__ptp_info_to_ptp((i)), struct ice_pf, ptp)
148 
149 #define PTP_SHARED_CLK_IDX_VALID	BIT(31)
150 #define ICE_PTP_TS_VALID		BIT(0)
151 
152 /* Per-channel register definitions */
153 #define GLTSYN_AUX_OUT(_chan, _idx)	(GLTSYN_AUX_OUT_0(_idx) + ((_chan) * 8))
154 #define GLTSYN_AUX_IN(_chan, _idx)	(GLTSYN_AUX_IN_0(_idx) + ((_chan) * 8))
155 #define GLTSYN_CLKO(_chan, _idx)	(GLTSYN_CLKO_0(_idx) + ((_chan) * 8))
156 #define GLTSYN_TGT_L(_chan, _idx)	(GLTSYN_TGT_L_0(_idx) + ((_chan) * 16))
157 #define GLTSYN_TGT_H(_chan, _idx)	(GLTSYN_TGT_H_0(_idx) + ((_chan) * 16))
158 #define GLTSYN_EVNT_L(_chan, _idx)	(GLTSYN_EVNT_L_0(_idx) + ((_chan) * 16))
159 #define GLTSYN_EVNT_H(_chan, _idx)	(GLTSYN_EVNT_H_0(_idx) + ((_chan) * 16))
160 #define GLTSYN_EVNT_H_IDX_MAX		3
161 
162 /* Pin definitions for PTP PPS out */
163 #define PPS_CLK_GEN_CHAN		3
164 #define PPS_CLK_SRC_CHAN		2
165 #define PPS_PIN_INDEX			5
166 #define TIME_SYNC_PIN_INDEX		4
167 #define N_EXT_TS_E810			3
168 #define N_PER_OUT_E810			4
169 #define N_PER_OUT_E810T			3
170 #define N_PER_OUT_E810T_NO_SMA		2
171 #define N_EXT_TS_E810_NO_SMA		2
172 
173 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
174 struct ice_pf;
175 int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr);
176 int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr);
177 int ice_get_ptp_clock_index(struct ice_pf *pf);
178 
179 s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb);
180 void ice_ptp_process_ts(struct ice_pf *pf);
181 
182 void
183 ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring,
184 		    union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb);
185 void ice_ptp_init(struct ice_pf *pf);
186 void ice_ptp_release(struct ice_pf *pf);
187 #else /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
188 static inline int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
189 {
190 	return -EOPNOTSUPP;
191 }
192 
193 static inline int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
194 {
195 	return -EOPNOTSUPP;
196 }
197 
198 static inline int ice_get_ptp_clock_index(struct ice_pf *pf)
199 {
200 	return -1;
201 }
202 
203 static inline s8
204 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
205 {
206 	return -1;
207 }
208 
209 static inline void ice_ptp_process_ts(struct ice_pf *pf) { }
210 static inline void
211 ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring,
212 		    union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) { }
213 static inline void ice_ptp_init(struct ice_pf *pf) { }
214 static inline void ice_ptp_release(struct ice_pf *pf) { }
215 #endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
216 #endif /* _ICE_PTP_H_ */
217