Loading...
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
12enum ice_ptp_pin_e810 {
13 GPIO_20 = 0,
14 GPIO_21,
15 GPIO_22,
16 GPIO_23,
17 NUM_PTP_PIN_E810
18};
19
20enum ice_ptp_pin_e810t {
21 GNSS = 0,
22 SMA1,
23 UFL1,
24 SMA2,
25 UFL2,
26 NUM_PTP_PINS_E810T
27};
28
29struct 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.
97 *
98 * Some PHY types do not provide a "ready" bitmap indicating which timestamp
99 * indexes are valid. In these cases, we use a cached_tstamp to keep track of
100 * the last timestamp we read for a given index. If the current timestamp
101 * value is the same as the cached value, we assume a new timestamp hasn't
102 * been captured. This avoids reporting stale timestamps to the stack. This is
103 * only done if the verify_cached flag is set in ice_ptp_tx structure.
104 */
105struct ice_tx_tstamp {
106 struct sk_buff *skb;
107 unsigned long start;
108 u64 cached_tstamp;
109};
110
111/**
112 * struct ice_ptp_tx - Tracking structure for all Tx timestamp requests on a port
113 * @lock: lock to prevent concurrent access to fields of this struct
114 * @tstamps: array of len to store outstanding requests
115 * @in_use: bitmap of len to indicate which slots are in use
116 * @stale: bitmap of len to indicate slots which have stale timestamps
117 * @block: which memory block (quad or port) the timestamps are captured in
118 * @offset: offset into timestamp block to get the real index
119 * @len: length of the tstamps and in_use fields.
120 * @init: if true, the tracker is initialized;
121 * @calibrating: if true, the PHY is calibrating the Tx offset. During this
122 * window, timestamps are temporarily disabled.
123 * @verify_cached: if true, verify new timestamp differs from last read value
124 */
125struct ice_ptp_tx {
126 spinlock_t lock; /* lock protecting in_use bitmap */
127 struct ice_tx_tstamp *tstamps;
128 unsigned long *in_use;
129 unsigned long *stale;
130 u8 block;
131 u8 offset;
132 u8 len;
133 u8 init : 1;
134 u8 calibrating : 1;
135 u8 verify_cached : 1;
136};
137
138/* Quad and port information for initializing timestamp blocks */
139#define INDEX_PER_QUAD 64
140#define INDEX_PER_PORT_E822 16
141#define INDEX_PER_PORT_E810 64
142
143/**
144 * struct ice_ptp_port - data used to initialize an external port for PTP
145 *
146 * This structure contains data indicating whether a single external port is
147 * ready for PTP functionality. It is used to track the port initialization
148 * and determine when the port's PHY offset is valid.
149 *
150 * @tx: Tx timestamp tracking for this port
151 * @ov_work: delayed work task for tracking when PHY offset is valid
152 * @ps_lock: mutex used to protect the overall PTP PHY start procedure
153 * @link_up: indicates whether the link is up
154 * @tx_fifo_busy_cnt: number of times the Tx FIFO was busy
155 * @port_num: the port number this structure represents
156 */
157struct ice_ptp_port {
158 struct ice_ptp_tx tx;
159 struct kthread_delayed_work ov_work;
160 struct mutex ps_lock; /* protects overall PTP PHY start procedure */
161 bool link_up;
162 u8 tx_fifo_busy_cnt;
163 u8 port_num;
164};
165
166#define GLTSYN_TGT_H_IDX_MAX 4
167
168/**
169 * struct ice_ptp - data used for integrating with CONFIG_PTP_1588_CLOCK
170 * @port: data for the PHY port initialization procedure
171 * @work: delayed work function for periodic tasks
172 * @extts_work: work function for handling external Tx timestamps
173 * @cached_phc_time: a cached copy of the PHC time for timestamp extension
174 * @cached_phc_jiffies: jiffies when cached_phc_time was last updated
175 * @ext_ts_chan: the external timestamp channel in use
176 * @ext_ts_irq: the external timestamp IRQ in use
177 * @kworker: kwork thread for handling periodic work
178 * @perout_channels: periodic output data
179 * @info: structure defining PTP hardware capabilities
180 * @clock: pointer to registered PTP clock device
181 * @tstamp_config: hardware timestamping configuration
182 * @reset_time: kernel time after clock stop on reset
183 * @tx_hwtstamp_skipped: number of Tx time stamp requests skipped
184 * @tx_hwtstamp_timeouts: number of Tx skbs discarded with no time stamp
185 * @tx_hwtstamp_flushed: number of Tx skbs flushed due to interface closed
186 * @tx_hwtstamp_discarded: number of Tx skbs discarded due to cached PHC time
187 * being too old to correctly extend timestamp
188 * @late_cached_phc_updates: number of times cached PHC update is late
189 */
190struct ice_ptp {
191 struct ice_ptp_port port;
192 struct kthread_delayed_work work;
193 struct kthread_work extts_work;
194 u64 cached_phc_time;
195 unsigned long cached_phc_jiffies;
196 u8 ext_ts_chan;
197 u8 ext_ts_irq;
198 struct kthread_worker *kworker;
199 struct ice_perout_channel perout_channels[GLTSYN_TGT_H_IDX_MAX];
200 struct ptp_clock_info info;
201 struct ptp_clock *clock;
202 struct hwtstamp_config tstamp_config;
203 u64 reset_time;
204 u32 tx_hwtstamp_skipped;
205 u32 tx_hwtstamp_timeouts;
206 u32 tx_hwtstamp_flushed;
207 u32 tx_hwtstamp_discarded;
208 u32 late_cached_phc_updates;
209};
210
211#define __ptp_port_to_ptp(p) \
212 container_of((p), struct ice_ptp, port)
213#define ptp_port_to_pf(p) \
214 container_of(__ptp_port_to_ptp((p)), struct ice_pf, ptp)
215
216#define __ptp_info_to_ptp(i) \
217 container_of((i), struct ice_ptp, info)
218#define ptp_info_to_pf(i) \
219 container_of(__ptp_info_to_ptp((i)), struct ice_pf, ptp)
220
221#define PFTSYN_SEM_BYTES 4
222#define PTP_SHARED_CLK_IDX_VALID BIT(31)
223#define TS_CMD_MASK 0xF
224#define SYNC_EXEC_CMD 0x3
225#define ICE_PTP_TS_VALID BIT(0)
226
227#define FIFO_EMPTY BIT(2)
228#define FIFO_OK 0xFF
229#define ICE_PTP_FIFO_NUM_CHECKS 5
230/* Per-channel register definitions */
231#define GLTSYN_AUX_OUT(_chan, _idx) (GLTSYN_AUX_OUT_0(_idx) + ((_chan) * 8))
232#define GLTSYN_AUX_IN(_chan, _idx) (GLTSYN_AUX_IN_0(_idx) + ((_chan) * 8))
233#define GLTSYN_CLKO(_chan, _idx) (GLTSYN_CLKO_0(_idx) + ((_chan) * 8))
234#define GLTSYN_TGT_L(_chan, _idx) (GLTSYN_TGT_L_0(_idx) + ((_chan) * 16))
235#define GLTSYN_TGT_H(_chan, _idx) (GLTSYN_TGT_H_0(_idx) + ((_chan) * 16))
236#define GLTSYN_EVNT_L(_chan, _idx) (GLTSYN_EVNT_L_0(_idx) + ((_chan) * 16))
237#define GLTSYN_EVNT_H(_chan, _idx) (GLTSYN_EVNT_H_0(_idx) + ((_chan) * 16))
238#define GLTSYN_EVNT_H_IDX_MAX 3
239
240/* Pin definitions for PTP PPS out */
241#define PPS_CLK_GEN_CHAN 3
242#define PPS_CLK_SRC_CHAN 2
243#define PPS_PIN_INDEX 5
244#define TIME_SYNC_PIN_INDEX 4
245#define N_EXT_TS_E810 3
246#define N_PER_OUT_E810 4
247#define N_PER_OUT_E810T 3
248#define N_PER_OUT_NO_SMA_E810T 2
249#define N_EXT_TS_NO_SMA_E810T 2
250#define ETH_GLTSYN_ENA(_i) (0x03000348 + ((_i) * 4))
251
252#if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
253struct ice_pf;
254int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr);
255int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr);
256void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena);
257int ice_get_ptp_clock_index(struct ice_pf *pf);
258
259s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb);
260bool ice_ptp_process_ts(struct ice_pf *pf);
261
262void
263ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring,
264 union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb);
265void ice_ptp_reset(struct ice_pf *pf);
266void ice_ptp_prepare_for_reset(struct ice_pf *pf);
267void ice_ptp_init(struct ice_pf *pf);
268void ice_ptp_release(struct ice_pf *pf);
269void ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup);
270#else /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
271static inline int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
272{
273 return -EOPNOTSUPP;
274}
275
276static inline int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
277{
278 return -EOPNOTSUPP;
279}
280
281static inline void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena) { }
282static inline int ice_get_ptp_clock_index(struct ice_pf *pf)
283{
284 return -1;
285}
286
287static inline s8
288ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
289{
290 return -1;
291}
292
293static inline bool ice_ptp_process_ts(struct ice_pf *pf)
294{
295 return true;
296}
297static inline void
298ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring,
299 union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) { }
300static inline void ice_ptp_reset(struct ice_pf *pf) { }
301static inline void ice_ptp_prepare_for_reset(struct ice_pf *pf) { }
302static inline void ice_ptp_init(struct ice_pf *pf) { }
303static inline void ice_ptp_release(struct ice_pf *pf) { }
304static inline void ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup)
305{
306}
307#endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
308#endif /* _ICE_PTP_H_ */
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
12enum ice_ptp_pin {
13 GPIO_20 = 0,
14 GPIO_21,
15 GPIO_22,
16 GPIO_23,
17 NUM_ICE_PTP_PIN
18};
19
20struct ice_perout_channel {
21 bool ena;
22 u32 gpio_pin;
23 u64 period;
24 u64 start_time;
25};
26
27/* The ice hardware captures Tx hardware timestamps in the PHY. The timestamp
28 * is stored in a buffer of registers. Depending on the specific hardware,
29 * this buffer might be shared across multiple PHY ports.
30 *
31 * On transmit of a packet to be timestamped, software is responsible for
32 * selecting an open index. Hardware makes no attempt to lock or prevent
33 * re-use of an index for multiple packets.
34 *
35 * To handle this, timestamp indexes must be tracked by software to ensure
36 * that an index is not re-used for multiple transmitted packets. The
37 * structures and functions declared in this file track the available Tx
38 * register indexes, as well as provide storage for the SKB pointers.
39 *
40 * To allow multiple ports to access the shared register block independently,
41 * the blocks are split up so that indexes are assigned to each port based on
42 * hardware logical port number.
43 */
44
45/**
46 * struct ice_tx_tstamp - Tracking for a single Tx timestamp
47 * @skb: pointer to the SKB for this timestamp request
48 * @start: jiffies when the timestamp was first requested
49 *
50 * This structure tracks a single timestamp request. The SKB pointer is
51 * provided when initiating a request. The start time is used to ensure that
52 * we discard old requests that were not fulfilled within a 2 second time
53 * window.
54 */
55struct ice_tx_tstamp {
56 struct sk_buff *skb;
57 unsigned long start;
58};
59
60/**
61 * struct ice_ptp_tx - Tracking structure for all Tx timestamp requests on a port
62 * @work: work function to handle processing of Tx timestamps
63 * @lock: lock to prevent concurrent write to in_use bitmap
64 * @tstamps: array of len to store outstanding requests
65 * @in_use: bitmap of len to indicate which slots are in use
66 * @quad: which quad the timestamps are captured in
67 * @quad_offset: offset into timestamp block of the quad to get the real index
68 * @len: length of the tstamps and in_use fields.
69 * @init: if true, the tracker is initialized;
70 */
71struct ice_ptp_tx {
72 struct kthread_work work;
73 spinlock_t lock; /* lock protecting in_use bitmap */
74 struct ice_tx_tstamp *tstamps;
75 unsigned long *in_use;
76 u8 quad;
77 u8 quad_offset;
78 u8 len;
79 u8 init;
80};
81
82/* Quad and port information for initializing timestamp blocks */
83#define INDEX_PER_QUAD 64
84#define INDEX_PER_PORT (INDEX_PER_QUAD / ICE_PORTS_PER_QUAD)
85
86/**
87 * struct ice_ptp_port - data used to initialize an external port for PTP
88 *
89 * This structure contains PTP data related to the external ports. Currently
90 * it is used for tracking the Tx timestamps of a port. In the future this
91 * structure will also hold information for the E822 port initialization
92 * logic.
93 *
94 * @tx: Tx timestamp tracking for this port
95 */
96struct ice_ptp_port {
97 struct ice_ptp_tx tx;
98};
99
100#define GLTSYN_TGT_H_IDX_MAX 4
101
102/**
103 * struct ice_ptp - data used for integrating with CONFIG_PTP_1588_CLOCK
104 * @port: data for the PHY port initialization procedure
105 * @work: delayed work function for periodic tasks
106 * @extts_work: work function for handling external Tx timestamps
107 * @cached_phc_time: a cached copy of the PHC time for timestamp extension
108 * @ext_ts_chan: the external timestamp channel in use
109 * @ext_ts_irq: the external timestamp IRQ in use
110 * @kworker: kwork thread for handling periodic work
111 * @perout_channels: periodic output data
112 * @info: structure defining PTP hardware capabilities
113 * @clock: pointer to registered PTP clock device
114 * @tstamp_config: hardware timestamping configuration
115 */
116struct ice_ptp {
117 struct ice_ptp_port port;
118 struct kthread_delayed_work work;
119 struct kthread_work extts_work;
120 u64 cached_phc_time;
121 u8 ext_ts_chan;
122 u8 ext_ts_irq;
123 struct kthread_worker *kworker;
124 struct ice_perout_channel perout_channels[GLTSYN_TGT_H_IDX_MAX];
125 struct ptp_clock_info info;
126 struct ptp_clock *clock;
127 struct hwtstamp_config tstamp_config;
128};
129
130#define __ptp_port_to_ptp(p) \
131 container_of((p), struct ice_ptp, port)
132#define ptp_port_to_pf(p) \
133 container_of(__ptp_port_to_ptp((p)), struct ice_pf, ptp)
134
135#define __ptp_info_to_ptp(i) \
136 container_of((i), struct ice_ptp, info)
137#define ptp_info_to_pf(i) \
138 container_of(__ptp_info_to_ptp((i)), struct ice_pf, ptp)
139
140#define PTP_SHARED_CLK_IDX_VALID BIT(31)
141#define ICE_PTP_TS_VALID BIT(0)
142
143/* Per-channel register definitions */
144#define GLTSYN_AUX_OUT(_chan, _idx) (GLTSYN_AUX_OUT_0(_idx) + ((_chan) * 8))
145#define GLTSYN_AUX_IN(_chan, _idx) (GLTSYN_AUX_IN_0(_idx) + ((_chan) * 8))
146#define GLTSYN_CLKO(_chan, _idx) (GLTSYN_CLKO_0(_idx) + ((_chan) * 8))
147#define GLTSYN_TGT_L(_chan, _idx) (GLTSYN_TGT_L_0(_idx) + ((_chan) * 16))
148#define GLTSYN_TGT_H(_chan, _idx) (GLTSYN_TGT_H_0(_idx) + ((_chan) * 16))
149#define GLTSYN_EVNT_L(_chan, _idx) (GLTSYN_EVNT_L_0(_idx) + ((_chan) * 16))
150#define GLTSYN_EVNT_H(_chan, _idx) (GLTSYN_EVNT_H_0(_idx) + ((_chan) * 16))
151#define GLTSYN_EVNT_H_IDX_MAX 3
152
153/* Pin definitions for PTP PPS out */
154#define PPS_CLK_GEN_CHAN 3
155#define PPS_CLK_SRC_CHAN 2
156#define PPS_PIN_INDEX 5
157#define TIME_SYNC_PIN_INDEX 4
158#define E810_N_EXT_TS 3
159#define E810_N_PER_OUT 4
160
161#if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
162struct ice_pf;
163int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr);
164int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr);
165int ice_get_ptp_clock_index(struct ice_pf *pf);
166
167s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb);
168void ice_ptp_process_ts(struct ice_pf *pf);
169
170void
171ice_ptp_rx_hwtstamp(struct ice_ring *rx_ring,
172 union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb);
173void ice_ptp_init(struct ice_pf *pf);
174void ice_ptp_release(struct ice_pf *pf);
175#else /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
176static inline int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
177{
178 return -EOPNOTSUPP;
179}
180
181static inline int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
182{
183 return -EOPNOTSUPP;
184}
185
186static inline int ice_get_ptp_clock_index(struct ice_pf *pf)
187{
188 return -1;
189}
190
191static inline s8
192ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
193{
194 return -1;
195}
196
197static inline void ice_ptp_process_ts(struct ice_pf *pf) { }
198static inline void
199ice_ptp_rx_hwtstamp(struct ice_ring *rx_ring,
200 union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) { }
201static inline void ice_ptp_init(struct ice_pf *pf) { }
202static inline void ice_ptp_release(struct ice_pf *pf) { }
203#endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
204#endif /* _ICE_PTP_H_ */