1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2021, Intel Corporation. */
7 #include <linux/ptp_clock_kernel.h>
8 #include <linux/kthread.h>
10 #include "ice_ptp_hw.h"
20 struct ice_perout_channel {
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.
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.
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.
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.
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
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
55 struct ice_tx_tstamp {
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;
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;
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)
87 * struct ice_ptp_port - data used to initialize an external port for PTP
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
94 * @tx: Tx timestamp tracking for this port
100 #define GLTSYN_TGT_H_IDX_MAX 4
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
117 struct ice_ptp_port port;
118 struct kthread_delayed_work work;
119 struct kthread_work extts_work;
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;
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)
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)
140 #define PTP_SHARED_CLK_IDX_VALID BIT(31)
141 #define ICE_PTP_TS_VALID BIT(0)
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
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
161 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
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);
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);
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)
181 static inline int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
186 static inline int ice_get_ptp_clock_index(struct ice_pf *pf)
192 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
197 static inline void ice_ptp_process_ts(struct ice_pf *pf) { }
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_ */