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 {
13 	GPIO_20 = 0,
14 	GPIO_21,
15 	GPIO_22,
16 	GPIO_23,
17 	NUM_ICE_PTP_PIN
18 };
19 
20 struct 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  */
55 struct 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  */
71 struct 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  */
96 struct 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  */
116 struct 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)
162 struct ice_pf;
163 int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr);
164 int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr);
165 int ice_get_ptp_clock_index(struct ice_pf *pf);
166 
167 s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb);
168 void ice_ptp_process_ts(struct ice_pf *pf);
169 
170 void
171 ice_ptp_rx_hwtstamp(struct ice_ring *rx_ring,
172 		    union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb);
173 void ice_ptp_init(struct ice_pf *pf);
174 void ice_ptp_release(struct ice_pf *pf);
175 #else /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
176 static inline int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
177 {
178 	return -EOPNOTSUPP;
179 }
180 
181 static inline int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
182 {
183 	return -EOPNOTSUPP;
184 }
185 
186 static inline int ice_get_ptp_clock_index(struct ice_pf *pf)
187 {
188 	return -1;
189 }
190 
191 static inline s8
192 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
193 {
194 	return -1;
195 }
196 
197 static inline void ice_ptp_process_ts(struct ice_pf *pf) { }
198 static inline void
199 ice_ptp_rx_hwtstamp(struct ice_ring *rx_ring,
200 		    union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) { }
201 static inline void ice_ptp_init(struct ice_pf *pf) { }
202 static inline void ice_ptp_release(struct ice_pf *pf) { }
203 #endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
204 #endif /* _ICE_PTP_H_ */
205