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 * The timestamp blocks are handled differently for E810- and E822-based
54 * devices. In E810 devices, each port has its own block of timestamps, while in
55 * E822 there is a need to logically break the block of registers into smaller
56 * chunks based on the port number to avoid collisions.
57 *
58 * Example for port 5 in E810:
59 * +--------+--------+--------+--------+--------+--------+--------+--------+
60 * |register|register|register|register|register|register|register|register|
61 * | block | block | block | block | block | block | block | block |
62 * | for | for | for | for | for | for | for | for |
63 * | port 0 | port 1 | port 2 | port 3 | port 4 | port 5 | port 6 | port 7 |
64 * +--------+--------+--------+--------+--------+--------+--------+--------+
65 * ^^
66 * ||
67 * |--- quad offset is always 0
68 * ---- quad number
69 *
70 * Example for port 5 in E822:
71 * +-----------------------------+-----------------------------+
72 * | register block for quad 0 | register block for quad 1 |
73 * |+------+------+------+------+|+------+------+------+------+|
74 * ||port 0|port 1|port 2|port 3|||port 0|port 1|port 2|port 3||
75 * |+------+------+------+------+|+------+------+------+------+|
76 * +-----------------------------+-------^---------------------+
77 * ^ |
78 * | --- quad offset*
79 * ---- quad number
80 *
81 * * PHY port 5 is port 1 in quad 1
82 *
83 */
84
85 /**
86 * struct ice_tx_tstamp - Tracking for a single Tx timestamp
87 * @skb: pointer to the SKB for this timestamp request
88 * @start: jiffies when the timestamp was first requested
89 * @cached_tstamp: last read timestamp
90 *
91 * This structure tracks a single timestamp request. The SKB pointer is
92 * provided when initiating a request. The start time is used to ensure that
93 * we discard old requests that were not fulfilled within a 2 second time
94 * window.
95 * Timestamp values in the PHY are read only and do not get cleared except at
96 * hardware reset or when a new timestamp value is captured. The cached_tstamp
97 * field is used to detect the case where a new timestamp has not yet been
98 * captured, ensuring that we avoid sending stale timestamp data to the stack.
99 */
100 struct ice_tx_tstamp {
101 struct sk_buff *skb;
102 unsigned long start;
103 u64 cached_tstamp;
104 };
105
106 /**
107 * struct ice_ptp_tx - Tracking structure for all Tx timestamp requests on a port
108 * @lock: lock to prevent concurrent write to in_use bitmap
109 * @tstamps: array of len to store outstanding requests
110 * @in_use: bitmap of len to indicate which slots are in use
111 * @quad: which quad the timestamps are captured in
112 * @quad_offset: offset into timestamp block of the quad to get the real index
113 * @len: length of the tstamps and in_use fields.
114 * @init: if true, the tracker is initialized;
115 * @calibrating: if true, the PHY is calibrating the Tx offset. During this
116 * window, timestamps are temporarily disabled.
117 */
118 struct ice_ptp_tx {
119 spinlock_t lock; /* lock protecting in_use bitmap */
120 struct ice_tx_tstamp *tstamps;
121 unsigned long *in_use;
122 u8 quad;
123 u8 quad_offset;
124 u8 len;
125 u8 init;
126 u8 calibrating;
127 };
128
129 /* Quad and port information for initializing timestamp blocks */
130 #define INDEX_PER_QUAD 64
131 #define INDEX_PER_PORT (INDEX_PER_QUAD / ICE_PORTS_PER_QUAD)
132
133 /**
134 * struct ice_ptp_port - data used to initialize an external port for PTP
135 *
136 * This structure contains data indicating whether a single external port is
137 * ready for PTP functionality. It is used to track the port initialization
138 * and determine when the port's PHY offset is valid.
139 *
140 * @tx: Tx timestamp tracking for this port
141 * @ov_work: delayed work task for tracking when PHY offset is valid
142 * @ps_lock: mutex used to protect the overall PTP PHY start procedure
143 * @link_up: indicates whether the link is up
144 * @tx_fifo_busy_cnt: number of times the Tx FIFO was busy
145 * @port_num: the port number this structure represents
146 */
147 struct ice_ptp_port {
148 struct ice_ptp_tx tx;
149 struct kthread_delayed_work ov_work;
150 struct mutex ps_lock; /* protects overall PTP PHY start procedure */
151 bool link_up;
152 u8 tx_fifo_busy_cnt;
153 u8 port_num;
154 };
155
156 #define GLTSYN_TGT_H_IDX_MAX 4
157
158 /**
159 * struct ice_ptp - data used for integrating with CONFIG_PTP_1588_CLOCK
160 * @port: data for the PHY port initialization procedure
161 * @work: delayed work function for periodic tasks
162 * @cached_phc_time: a cached copy of the PHC time for timestamp extension
163 * @cached_phc_jiffies: jiffies when cached_phc_time was last updated
164 * @ext_ts_chan: the external timestamp channel in use
165 * @ext_ts_irq: the external timestamp IRQ in use
166 * @kworker: kwork thread for handling periodic work
167 * @perout_channels: periodic output data
168 * @info: structure defining PTP hardware capabilities
169 * @clock: pointer to registered PTP clock device
170 * @tstamp_config: hardware timestamping configuration
171 * @reset_time: kernel time after clock stop on reset
172 * @tx_hwtstamp_skipped: number of Tx time stamp requests skipped
173 * @tx_hwtstamp_timeouts: number of Tx skbs discarded with no time stamp
174 * @tx_hwtstamp_flushed: number of Tx skbs flushed due to interface closed
175 * @tx_hwtstamp_discarded: number of Tx skbs discarded due to cached PHC time
176 * being too old to correctly extend timestamp
177 * @late_cached_phc_updates: number of times cached PHC update is late
178 */
179 struct ice_ptp {
180 struct ice_ptp_port port;
181 struct kthread_delayed_work work;
182 u64 cached_phc_time;
183 unsigned long cached_phc_jiffies;
184 u8 ext_ts_chan;
185 u8 ext_ts_irq;
186 struct kthread_worker *kworker;
187 struct ice_perout_channel perout_channels[GLTSYN_TGT_H_IDX_MAX];
188 struct ptp_clock_info info;
189 struct ptp_clock *clock;
190 struct hwtstamp_config tstamp_config;
191 u64 reset_time;
192 u32 tx_hwtstamp_skipped;
193 u32 tx_hwtstamp_timeouts;
194 u32 tx_hwtstamp_flushed;
195 u32 tx_hwtstamp_discarded;
196 u32 late_cached_phc_updates;
197 };
198
199 #define __ptp_port_to_ptp(p) \
200 container_of((p), struct ice_ptp, port)
201 #define ptp_port_to_pf(p) \
202 container_of(__ptp_port_to_ptp((p)), struct ice_pf, ptp)
203
204 #define __ptp_info_to_ptp(i) \
205 container_of((i), struct ice_ptp, info)
206 #define ptp_info_to_pf(i) \
207 container_of(__ptp_info_to_ptp((i)), struct ice_pf, ptp)
208
209 #define PFTSYN_SEM_BYTES 4
210 #define PTP_SHARED_CLK_IDX_VALID BIT(31)
211 #define TS_CMD_MASK 0xF
212 #define SYNC_EXEC_CMD 0x3
213 #define ICE_PTP_TS_VALID BIT(0)
214
215 #define FIFO_EMPTY BIT(2)
216 #define FIFO_OK 0xFF
217 #define ICE_PTP_FIFO_NUM_CHECKS 5
218 /* Per-channel register definitions */
219 #define GLTSYN_AUX_OUT(_chan, _idx) (GLTSYN_AUX_OUT_0(_idx) + ((_chan) * 8))
220 #define GLTSYN_AUX_IN(_chan, _idx) (GLTSYN_AUX_IN_0(_idx) + ((_chan) * 8))
221 #define GLTSYN_CLKO(_chan, _idx) (GLTSYN_CLKO_0(_idx) + ((_chan) * 8))
222 #define GLTSYN_TGT_L(_chan, _idx) (GLTSYN_TGT_L_0(_idx) + ((_chan) * 16))
223 #define GLTSYN_TGT_H(_chan, _idx) (GLTSYN_TGT_H_0(_idx) + ((_chan) * 16))
224 #define GLTSYN_EVNT_L(_chan, _idx) (GLTSYN_EVNT_L_0(_idx) + ((_chan) * 16))
225 #define GLTSYN_EVNT_H(_chan, _idx) (GLTSYN_EVNT_H_0(_idx) + ((_chan) * 16))
226 #define GLTSYN_EVNT_H_IDX_MAX 3
227
228 /* Pin definitions for PTP PPS out */
229 #define PPS_CLK_GEN_CHAN 3
230 #define PPS_CLK_SRC_CHAN 2
231 #define PPS_PIN_INDEX 5
232 #define TIME_SYNC_PIN_INDEX 4
233 #define N_EXT_TS_E810 3
234 #define N_PER_OUT_E810 4
235 #define N_PER_OUT_E810T 3
236 #define N_PER_OUT_NO_SMA_E810T 2
237 #define N_EXT_TS_NO_SMA_E810T 2
238 #define ETH_GLTSYN_ENA(_i) (0x03000348 + ((_i) * 4))
239
240 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
241 struct ice_pf;
242 int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr);
243 int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr);
244 void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena);
245 int ice_get_ptp_clock_index(struct ice_pf *pf);
246
247 void ice_ptp_extts_event(struct ice_pf *pf);
248 s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb);
249 bool ice_ptp_process_ts(struct ice_pf *pf);
250
251 void
252 ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring,
253 union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb);
254 void ice_ptp_reset(struct ice_pf *pf);
255 void ice_ptp_prepare_for_reset(struct ice_pf *pf);
256 void ice_ptp_init(struct ice_pf *pf);
257 void ice_ptp_release(struct ice_pf *pf);
258 int ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup);
259 #else /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
ice_ptp_set_ts_config(struct ice_pf * pf,struct ifreq * ifr)260 static inline int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
261 {
262 return -EOPNOTSUPP;
263 }
264
ice_ptp_get_ts_config(struct ice_pf * pf,struct ifreq * ifr)265 static inline int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
266 {
267 return -EOPNOTSUPP;
268 }
269
ice_ptp_cfg_timestamp(struct ice_pf * pf,bool ena)270 static inline void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena) { }
ice_get_ptp_clock_index(struct ice_pf * pf)271 static inline int ice_get_ptp_clock_index(struct ice_pf *pf)
272 {
273 return -1;
274 }
275
ice_ptp_extts_event(struct ice_pf * pf)276 static inline void ice_ptp_extts_event(struct ice_pf *pf) { }
277 static inline s8
ice_ptp_request_ts(struct ice_ptp_tx * tx,struct sk_buff * skb)278 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
279 {
280 return -1;
281 }
282
ice_ptp_process_ts(struct ice_pf * pf)283 static inline bool ice_ptp_process_ts(struct ice_pf *pf)
284 {
285 return true;
286 }
287 static inline void
ice_ptp_rx_hwtstamp(struct ice_rx_ring * rx_ring,union ice_32b_rx_flex_desc * rx_desc,struct sk_buff * skb)288 ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring,
289 union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) { }
ice_ptp_reset(struct ice_pf * pf)290 static inline void ice_ptp_reset(struct ice_pf *pf) { }
ice_ptp_prepare_for_reset(struct ice_pf * pf)291 static inline void ice_ptp_prepare_for_reset(struct ice_pf *pf) { }
ice_ptp_init(struct ice_pf * pf)292 static inline void ice_ptp_init(struct ice_pf *pf) { }
ice_ptp_release(struct ice_pf * pf)293 static inline void ice_ptp_release(struct ice_pf *pf) { }
ice_ptp_link_change(struct ice_pf * pf,u8 port,bool linkup)294 static inline int ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup)
295 { return 0; }
296 #endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
297 #endif /* _ICE_PTP_H_ */
298