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  * @cached_tstamp: last read timestamp
59  *
60  * This structure tracks a single timestamp request. The SKB pointer is
61  * provided when initiating a request. The start time is used to ensure that
62  * we discard old requests that were not fulfilled within a 2 second time
63  * window.
64  * Timestamp values in the PHY are read only and do not get cleared except at
65  * hardware reset or when a new timestamp value is captured. The cached_tstamp
66  * field is used to detect the case where a new timestamp has not yet been
67  * captured, ensuring that we avoid sending stale timestamp data to the stack.
68  */
69 struct ice_tx_tstamp {
70 	struct sk_buff *skb;
71 	unsigned long start;
72 	u64 cached_tstamp;
73 };
74 
75 /**
76  * struct ice_ptp_tx - Tracking structure for all Tx timestamp requests on a port
77  * @work: work function to handle processing of Tx timestamps
78  * @lock: lock to prevent concurrent write to in_use bitmap
79  * @tstamps: array of len to store outstanding requests
80  * @in_use: bitmap of len to indicate which slots are in use
81  * @quad: which quad the timestamps are captured in
82  * @quad_offset: offset into timestamp block of the quad to get the real index
83  * @len: length of the tstamps and in_use fields.
84  * @init: if true, the tracker is initialized;
85  */
86 struct ice_ptp_tx {
87 	struct kthread_work work;
88 	spinlock_t lock; /* lock protecting in_use bitmap */
89 	struct ice_tx_tstamp *tstamps;
90 	unsigned long *in_use;
91 	u8 quad;
92 	u8 quad_offset;
93 	u8 len;
94 	u8 init;
95 };
96 
97 /* Quad and port information for initializing timestamp blocks */
98 #define INDEX_PER_QUAD			64
99 #define INDEX_PER_PORT			(INDEX_PER_QUAD / ICE_PORTS_PER_QUAD)
100 
101 /**
102  * struct ice_ptp_port - data used to initialize an external port for PTP
103  *
104  * This structure contains PTP data related to the external ports. Currently
105  * it is used for tracking the Tx timestamps of a port. In the future this
106  * structure will also hold information for the E822 port initialization
107  * logic.
108  *
109  * @tx: Tx timestamp tracking for this port
110  */
111 struct ice_ptp_port {
112 	struct ice_ptp_tx tx;
113 };
114 
115 #define GLTSYN_TGT_H_IDX_MAX		4
116 
117 /**
118  * struct ice_ptp - data used for integrating with CONFIG_PTP_1588_CLOCK
119  * @port: data for the PHY port initialization procedure
120  * @work: delayed work function for periodic tasks
121  * @extts_work: work function for handling external Tx timestamps
122  * @cached_phc_time: a cached copy of the PHC time for timestamp extension
123  * @ext_ts_chan: the external timestamp channel in use
124  * @ext_ts_irq: the external timestamp IRQ in use
125  * @kworker: kwork thread for handling periodic work
126  * @perout_channels: periodic output data
127  * @info: structure defining PTP hardware capabilities
128  * @clock: pointer to registered PTP clock device
129  * @tstamp_config: hardware timestamping configuration
130  */
131 struct ice_ptp {
132 	struct ice_ptp_port port;
133 	struct kthread_delayed_work work;
134 	struct kthread_work extts_work;
135 	u64 cached_phc_time;
136 	u8 ext_ts_chan;
137 	u8 ext_ts_irq;
138 	struct kthread_worker *kworker;
139 	struct ice_perout_channel perout_channels[GLTSYN_TGT_H_IDX_MAX];
140 	struct ptp_clock_info info;
141 	struct ptp_clock *clock;
142 	struct hwtstamp_config tstamp_config;
143 };
144 
145 #define __ptp_port_to_ptp(p) \
146 	container_of((p), struct ice_ptp, port)
147 #define ptp_port_to_pf(p) \
148 	container_of(__ptp_port_to_ptp((p)), struct ice_pf, ptp)
149 
150 #define __ptp_info_to_ptp(i) \
151 	container_of((i), struct ice_ptp, info)
152 #define ptp_info_to_pf(i) \
153 	container_of(__ptp_info_to_ptp((i)), struct ice_pf, ptp)
154 
155 #define PTP_SHARED_CLK_IDX_VALID	BIT(31)
156 #define ICE_PTP_TS_VALID		BIT(0)
157 
158 /* Per-channel register definitions */
159 #define GLTSYN_AUX_OUT(_chan, _idx)	(GLTSYN_AUX_OUT_0(_idx) + ((_chan) * 8))
160 #define GLTSYN_AUX_IN(_chan, _idx)	(GLTSYN_AUX_IN_0(_idx) + ((_chan) * 8))
161 #define GLTSYN_CLKO(_chan, _idx)	(GLTSYN_CLKO_0(_idx) + ((_chan) * 8))
162 #define GLTSYN_TGT_L(_chan, _idx)	(GLTSYN_TGT_L_0(_idx) + ((_chan) * 16))
163 #define GLTSYN_TGT_H(_chan, _idx)	(GLTSYN_TGT_H_0(_idx) + ((_chan) * 16))
164 #define GLTSYN_EVNT_L(_chan, _idx)	(GLTSYN_EVNT_L_0(_idx) + ((_chan) * 16))
165 #define GLTSYN_EVNT_H(_chan, _idx)	(GLTSYN_EVNT_H_0(_idx) + ((_chan) * 16))
166 #define GLTSYN_EVNT_H_IDX_MAX		3
167 
168 /* Pin definitions for PTP PPS out */
169 #define PPS_CLK_GEN_CHAN		3
170 #define PPS_CLK_SRC_CHAN		2
171 #define PPS_PIN_INDEX			5
172 #define TIME_SYNC_PIN_INDEX		4
173 #define N_EXT_TS_E810			3
174 #define N_PER_OUT_E810			4
175 #define N_PER_OUT_E810T			3
176 #define N_PER_OUT_E810T_NO_SMA		2
177 #define N_EXT_TS_E810_NO_SMA		2
178 
179 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
180 struct ice_pf;
181 int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr);
182 int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr);
183 int ice_get_ptp_clock_index(struct ice_pf *pf);
184 
185 s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb);
186 void ice_ptp_process_ts(struct ice_pf *pf);
187 
188 void
189 ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring,
190 		    union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb);
191 void ice_ptp_init(struct ice_pf *pf);
192 void ice_ptp_release(struct ice_pf *pf);
193 #else /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
ice_ptp_set_ts_config(struct ice_pf * pf,struct ifreq * ifr)194 static inline int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
195 {
196 	return -EOPNOTSUPP;
197 }
198 
ice_ptp_get_ts_config(struct ice_pf * pf,struct ifreq * ifr)199 static inline int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
200 {
201 	return -EOPNOTSUPP;
202 }
203 
ice_get_ptp_clock_index(struct ice_pf * pf)204 static inline int ice_get_ptp_clock_index(struct ice_pf *pf)
205 {
206 	return -1;
207 }
208 
209 static inline s8
ice_ptp_request_ts(struct ice_ptp_tx * tx,struct sk_buff * skb)210 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
211 {
212 	return -1;
213 }
214 
ice_ptp_process_ts(struct ice_pf * pf)215 static inline void ice_ptp_process_ts(struct ice_pf *pf) { }
216 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)217 ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring,
218 		    union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) { }
ice_ptp_init(struct ice_pf * pf)219 static inline void ice_ptp_init(struct ice_pf *pf) { }
ice_ptp_release(struct ice_pf * pf)220 static inline void ice_ptp_release(struct ice_pf *pf) { }
221 #endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
222 #endif /* _ICE_PTP_H_ */
223