106c16d89SJacob Keller // SPDX-License-Identifier: GPL-2.0
206c16d89SJacob Keller /* Copyright (C) 2021, Intel Corporation. */
306c16d89SJacob Keller
406c16d89SJacob Keller #include "ice.h"
506c16d89SJacob Keller #include "ice_lib.h"
64c120218SJacob Keller #include "ice_trace.h"
706c16d89SJacob Keller
8172db5f9SMaciej Machnikowski #define E810_OUT_PROP_DELAY_NS 1
9172db5f9SMaciej Machnikowski
103a749623SJacob Keller #define UNKNOWN_INCVAL_E822 0x100000000ULL
113a749623SJacob Keller
12325b2064SMaciej Machnikowski static const struct ptp_pin_desc ice_pin_desc_e810t[] = {
13325b2064SMaciej Machnikowski /* name idx func chan */
14325b2064SMaciej Machnikowski { "GNSS", GNSS, PTP_PF_EXTTS, 0, { 0, } },
15325b2064SMaciej Machnikowski { "SMA1", SMA1, PTP_PF_NONE, 1, { 0, } },
16325b2064SMaciej Machnikowski { "U.FL1", UFL1, PTP_PF_NONE, 1, { 0, } },
17325b2064SMaciej Machnikowski { "SMA2", SMA2, PTP_PF_NONE, 2, { 0, } },
18325b2064SMaciej Machnikowski { "U.FL2", UFL2, PTP_PF_NONE, 2, { 0, } },
19325b2064SMaciej Machnikowski };
20325b2064SMaciej Machnikowski
21325b2064SMaciej Machnikowski /**
22325b2064SMaciej Machnikowski * ice_get_sma_config_e810t
23325b2064SMaciej Machnikowski * @hw: pointer to the hw struct
24325b2064SMaciej Machnikowski * @ptp_pins: pointer to the ptp_pin_desc struture
25325b2064SMaciej Machnikowski *
26325b2064SMaciej Machnikowski * Read the configuration of the SMA control logic and put it into the
27325b2064SMaciej Machnikowski * ptp_pin_desc structure
28325b2064SMaciej Machnikowski */
29325b2064SMaciej Machnikowski static int
ice_get_sma_config_e810t(struct ice_hw * hw,struct ptp_pin_desc * ptp_pins)30325b2064SMaciej Machnikowski ice_get_sma_config_e810t(struct ice_hw *hw, struct ptp_pin_desc *ptp_pins)
31325b2064SMaciej Machnikowski {
32325b2064SMaciej Machnikowski u8 data, i;
33325b2064SMaciej Machnikowski int status;
34325b2064SMaciej Machnikowski
35325b2064SMaciej Machnikowski /* Read initial pin state */
36325b2064SMaciej Machnikowski status = ice_read_sma_ctrl_e810t(hw, &data);
37325b2064SMaciej Machnikowski if (status)
38325b2064SMaciej Machnikowski return status;
39325b2064SMaciej Machnikowski
40325b2064SMaciej Machnikowski /* initialize with defaults */
41325b2064SMaciej Machnikowski for (i = 0; i < NUM_PTP_PINS_E810T; i++) {
42325b2064SMaciej Machnikowski snprintf(ptp_pins[i].name, sizeof(ptp_pins[i].name),
43325b2064SMaciej Machnikowski "%s", ice_pin_desc_e810t[i].name);
44325b2064SMaciej Machnikowski ptp_pins[i].index = ice_pin_desc_e810t[i].index;
45325b2064SMaciej Machnikowski ptp_pins[i].func = ice_pin_desc_e810t[i].func;
46325b2064SMaciej Machnikowski ptp_pins[i].chan = ice_pin_desc_e810t[i].chan;
47325b2064SMaciej Machnikowski }
48325b2064SMaciej Machnikowski
49325b2064SMaciej Machnikowski /* Parse SMA1/UFL1 */
50325b2064SMaciej Machnikowski switch (data & ICE_SMA1_MASK_E810T) {
51325b2064SMaciej Machnikowski case ICE_SMA1_MASK_E810T:
52325b2064SMaciej Machnikowski default:
53325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_NONE;
54325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE;
55325b2064SMaciej Machnikowski break;
56325b2064SMaciej Machnikowski case ICE_SMA1_DIR_EN_E810T:
57325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_PEROUT;
58325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE;
59325b2064SMaciej Machnikowski break;
60325b2064SMaciej Machnikowski case ICE_SMA1_TX_EN_E810T:
61325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_EXTTS;
62325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE;
63325b2064SMaciej Machnikowski break;
64325b2064SMaciej Machnikowski case 0:
65325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_EXTTS;
66325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_PEROUT;
67325b2064SMaciej Machnikowski break;
68325b2064SMaciej Machnikowski }
69325b2064SMaciej Machnikowski
70325b2064SMaciej Machnikowski /* Parse SMA2/UFL2 */
71325b2064SMaciej Machnikowski switch (data & ICE_SMA2_MASK_E810T) {
72325b2064SMaciej Machnikowski case ICE_SMA2_MASK_E810T:
73325b2064SMaciej Machnikowski default:
74325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE;
75325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE;
76325b2064SMaciej Machnikowski break;
77325b2064SMaciej Machnikowski case (ICE_SMA2_TX_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T):
78325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_EXTTS;
79325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE;
80325b2064SMaciej Machnikowski break;
81325b2064SMaciej Machnikowski case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T):
82325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_PEROUT;
83325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE;
84325b2064SMaciej Machnikowski break;
85325b2064SMaciej Machnikowski case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T):
86325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE;
87325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_EXTTS;
88325b2064SMaciej Machnikowski break;
89325b2064SMaciej Machnikowski case ICE_SMA2_DIR_EN_E810T:
90325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_PEROUT;
91325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_EXTTS;
92325b2064SMaciej Machnikowski break;
93325b2064SMaciej Machnikowski }
94325b2064SMaciej Machnikowski
95325b2064SMaciej Machnikowski return 0;
96325b2064SMaciej Machnikowski }
97325b2064SMaciej Machnikowski
98325b2064SMaciej Machnikowski /**
99325b2064SMaciej Machnikowski * ice_ptp_set_sma_config_e810t
100325b2064SMaciej Machnikowski * @hw: pointer to the hw struct
101325b2064SMaciej Machnikowski * @ptp_pins: pointer to the ptp_pin_desc struture
102325b2064SMaciej Machnikowski *
103325b2064SMaciej Machnikowski * Set the configuration of the SMA control logic based on the configuration in
104325b2064SMaciej Machnikowski * num_pins parameter
105325b2064SMaciej Machnikowski */
106325b2064SMaciej Machnikowski static int
ice_ptp_set_sma_config_e810t(struct ice_hw * hw,const struct ptp_pin_desc * ptp_pins)107325b2064SMaciej Machnikowski ice_ptp_set_sma_config_e810t(struct ice_hw *hw,
108325b2064SMaciej Machnikowski const struct ptp_pin_desc *ptp_pins)
109325b2064SMaciej Machnikowski {
110325b2064SMaciej Machnikowski int status;
111325b2064SMaciej Machnikowski u8 data;
112325b2064SMaciej Machnikowski
113325b2064SMaciej Machnikowski /* SMA1 and UFL1 cannot be set to TX at the same time */
114325b2064SMaciej Machnikowski if (ptp_pins[SMA1].func == PTP_PF_PEROUT &&
115325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT)
116325b2064SMaciej Machnikowski return -EINVAL;
117325b2064SMaciej Machnikowski
118325b2064SMaciej Machnikowski /* SMA2 and UFL2 cannot be set to RX at the same time */
119325b2064SMaciej Machnikowski if (ptp_pins[SMA2].func == PTP_PF_EXTTS &&
120325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS)
121325b2064SMaciej Machnikowski return -EINVAL;
122325b2064SMaciej Machnikowski
123325b2064SMaciej Machnikowski /* Read initial pin state value */
124325b2064SMaciej Machnikowski status = ice_read_sma_ctrl_e810t(hw, &data);
125325b2064SMaciej Machnikowski if (status)
126325b2064SMaciej Machnikowski return status;
127325b2064SMaciej Machnikowski
128325b2064SMaciej Machnikowski /* Set the right sate based on the desired configuration */
129325b2064SMaciej Machnikowski data &= ~ICE_SMA1_MASK_E810T;
130325b2064SMaciej Machnikowski if (ptp_pins[SMA1].func == PTP_PF_NONE &&
131325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) {
132325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 + U.FL1 disabled");
133325b2064SMaciej Machnikowski data |= ICE_SMA1_MASK_E810T;
134325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_EXTTS &&
135325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) {
136325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX");
137325b2064SMaciej Machnikowski data |= ICE_SMA1_TX_EN_E810T;
138325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_NONE &&
139325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT) {
140325b2064SMaciej Machnikowski /* U.FL 1 TX will always enable SMA 1 RX */
141325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX");
142325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_EXTTS &&
143325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT) {
144325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX");
145325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_PEROUT &&
146325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) {
147325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 TX");
148325b2064SMaciej Machnikowski data |= ICE_SMA1_DIR_EN_E810T;
149325b2064SMaciej Machnikowski }
150325b2064SMaciej Machnikowski
151325b2064SMaciej Machnikowski data &= ~ICE_SMA2_MASK_E810T;
152325b2064SMaciej Machnikowski if (ptp_pins[SMA2].func == PTP_PF_NONE &&
153325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) {
154325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 + U.FL2 disabled");
155325b2064SMaciej Machnikowski data |= ICE_SMA2_MASK_E810T;
156325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_EXTTS &&
157325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) {
158325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 RX");
159325b2064SMaciej Machnikowski data |= (ICE_SMA2_TX_EN_E810T |
160325b2064SMaciej Machnikowski ICE_SMA2_UFL2_RX_DIS_E810T);
161325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_NONE &&
162325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS) {
163325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "UFL2 RX");
164325b2064SMaciej Machnikowski data |= (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T);
165325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_PEROUT &&
166325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) {
167325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 TX");
168325b2064SMaciej Machnikowski data |= (ICE_SMA2_DIR_EN_E810T |
169325b2064SMaciej Machnikowski ICE_SMA2_UFL2_RX_DIS_E810T);
170325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_PEROUT &&
171325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS) {
172325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 TX + U.FL2 RX");
173325b2064SMaciej Machnikowski data |= ICE_SMA2_DIR_EN_E810T;
174325b2064SMaciej Machnikowski }
175325b2064SMaciej Machnikowski
176325b2064SMaciej Machnikowski return ice_write_sma_ctrl_e810t(hw, data);
177325b2064SMaciej Machnikowski }
178325b2064SMaciej Machnikowski
179325b2064SMaciej Machnikowski /**
180325b2064SMaciej Machnikowski * ice_ptp_set_sma_e810t
181325b2064SMaciej Machnikowski * @info: the driver's PTP info structure
182325b2064SMaciej Machnikowski * @pin: pin index in kernel structure
183325b2064SMaciej Machnikowski * @func: Pin function to be set (PTP_PF_NONE, PTP_PF_EXTTS or PTP_PF_PEROUT)
184325b2064SMaciej Machnikowski *
185325b2064SMaciej Machnikowski * Set the configuration of a single SMA pin
186325b2064SMaciej Machnikowski */
187325b2064SMaciej Machnikowski static int
ice_ptp_set_sma_e810t(struct ptp_clock_info * info,unsigned int pin,enum ptp_pin_function func)188325b2064SMaciej Machnikowski ice_ptp_set_sma_e810t(struct ptp_clock_info *info, unsigned int pin,
189325b2064SMaciej Machnikowski enum ptp_pin_function func)
190325b2064SMaciej Machnikowski {
191325b2064SMaciej Machnikowski struct ptp_pin_desc ptp_pins[NUM_PTP_PINS_E810T];
192325b2064SMaciej Machnikowski struct ice_pf *pf = ptp_info_to_pf(info);
193325b2064SMaciej Machnikowski struct ice_hw *hw = &pf->hw;
194325b2064SMaciej Machnikowski int err;
195325b2064SMaciej Machnikowski
196325b2064SMaciej Machnikowski if (pin < SMA1 || func > PTP_PF_PEROUT)
197325b2064SMaciej Machnikowski return -EOPNOTSUPP;
198325b2064SMaciej Machnikowski
199325b2064SMaciej Machnikowski err = ice_get_sma_config_e810t(hw, ptp_pins);
200325b2064SMaciej Machnikowski if (err)
201325b2064SMaciej Machnikowski return err;
202325b2064SMaciej Machnikowski
203325b2064SMaciej Machnikowski /* Disable the same function on the other pin sharing the channel */
204325b2064SMaciej Machnikowski if (pin == SMA1 && ptp_pins[UFL1].func == func)
205325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE;
206325b2064SMaciej Machnikowski if (pin == UFL1 && ptp_pins[SMA1].func == func)
207325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_NONE;
208325b2064SMaciej Machnikowski
209325b2064SMaciej Machnikowski if (pin == SMA2 && ptp_pins[UFL2].func == func)
210325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE;
211325b2064SMaciej Machnikowski if (pin == UFL2 && ptp_pins[SMA2].func == func)
212325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE;
213325b2064SMaciej Machnikowski
214325b2064SMaciej Machnikowski /* Set up new pin function in the temp table */
215325b2064SMaciej Machnikowski ptp_pins[pin].func = func;
216325b2064SMaciej Machnikowski
217325b2064SMaciej Machnikowski return ice_ptp_set_sma_config_e810t(hw, ptp_pins);
218325b2064SMaciej Machnikowski }
219325b2064SMaciej Machnikowski
220325b2064SMaciej Machnikowski /**
221325b2064SMaciej Machnikowski * ice_verify_pin_e810t
222325b2064SMaciej Machnikowski * @info: the driver's PTP info structure
223325b2064SMaciej Machnikowski * @pin: Pin index
224325b2064SMaciej Machnikowski * @func: Assigned function
225325b2064SMaciej Machnikowski * @chan: Assigned channel
226325b2064SMaciej Machnikowski *
227325b2064SMaciej Machnikowski * Verify if pin supports requested pin function. If the Check pins consistency.
228325b2064SMaciej Machnikowski * Reconfigure the SMA logic attached to the given pin to enable its
229325b2064SMaciej Machnikowski * desired functionality
230325b2064SMaciej Machnikowski */
231325b2064SMaciej Machnikowski static int
ice_verify_pin_e810t(struct ptp_clock_info * info,unsigned int pin,enum ptp_pin_function func,unsigned int chan)232325b2064SMaciej Machnikowski ice_verify_pin_e810t(struct ptp_clock_info *info, unsigned int pin,
233325b2064SMaciej Machnikowski enum ptp_pin_function func, unsigned int chan)
234325b2064SMaciej Machnikowski {
235325b2064SMaciej Machnikowski /* Don't allow channel reassignment */
236325b2064SMaciej Machnikowski if (chan != ice_pin_desc_e810t[pin].chan)
237325b2064SMaciej Machnikowski return -EOPNOTSUPP;
238325b2064SMaciej Machnikowski
239325b2064SMaciej Machnikowski /* Check if functions are properly assigned */
240325b2064SMaciej Machnikowski switch (func) {
241325b2064SMaciej Machnikowski case PTP_PF_NONE:
242325b2064SMaciej Machnikowski break;
243325b2064SMaciej Machnikowski case PTP_PF_EXTTS:
244325b2064SMaciej Machnikowski if (pin == UFL1)
245325b2064SMaciej Machnikowski return -EOPNOTSUPP;
246325b2064SMaciej Machnikowski break;
247325b2064SMaciej Machnikowski case PTP_PF_PEROUT:
248325b2064SMaciej Machnikowski if (pin == UFL2 || pin == GNSS)
249325b2064SMaciej Machnikowski return -EOPNOTSUPP;
250325b2064SMaciej Machnikowski break;
251325b2064SMaciej Machnikowski case PTP_PF_PHYSYNC:
252325b2064SMaciej Machnikowski return -EOPNOTSUPP;
253325b2064SMaciej Machnikowski }
254325b2064SMaciej Machnikowski
255325b2064SMaciej Machnikowski return ice_ptp_set_sma_e810t(info, pin, func);
256325b2064SMaciej Machnikowski }
257325b2064SMaciej Machnikowski
25806c16d89SJacob Keller /**
259ea9b847cSJacob Keller * ice_set_tx_tstamp - Enable or disable Tx timestamping
260ea9b847cSJacob Keller * @pf: The PF pointer to search in
261ea9b847cSJacob Keller * @on: bool value for whether timestamps are enabled or disabled
262ea9b847cSJacob Keller */
ice_set_tx_tstamp(struct ice_pf * pf,bool on)263ea9b847cSJacob Keller static void ice_set_tx_tstamp(struct ice_pf *pf, bool on)
264ea9b847cSJacob Keller {
265ea9b847cSJacob Keller struct ice_vsi *vsi;
266ea9b847cSJacob Keller u32 val;
267ea9b847cSJacob Keller u16 i;
268ea9b847cSJacob Keller
269ea9b847cSJacob Keller vsi = ice_get_main_vsi(pf);
270ea9b847cSJacob Keller if (!vsi)
271ea9b847cSJacob Keller return;
272ea9b847cSJacob Keller
273ea9b847cSJacob Keller /* Set the timestamp enable flag for all the Tx rings */
27484c5fb8cSJacob Keller ice_for_each_txq(vsi, i) {
275ea9b847cSJacob Keller if (!vsi->tx_rings[i])
276ea9b847cSJacob Keller continue;
277ea9b847cSJacob Keller vsi->tx_rings[i]->ptp_tx = on;
278ea9b847cSJacob Keller }
279ea9b847cSJacob Keller
280ea9b847cSJacob Keller /* Configure the Tx timestamp interrupt */
281ea9b847cSJacob Keller val = rd32(&pf->hw, PFINT_OICR_ENA);
282ea9b847cSJacob Keller if (on)
283ea9b847cSJacob Keller val |= PFINT_OICR_TSYN_TX_M;
284ea9b847cSJacob Keller else
285ea9b847cSJacob Keller val &= ~PFINT_OICR_TSYN_TX_M;
286ea9b847cSJacob Keller wr32(&pf->hw, PFINT_OICR_ENA, val);
287e59d75ddSJacob Keller
288e59d75ddSJacob Keller pf->ptp.tstamp_config.tx_type = on ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
289ea9b847cSJacob Keller }
290ea9b847cSJacob Keller
291ea9b847cSJacob Keller /**
29277a78115SJacob Keller * ice_set_rx_tstamp - Enable or disable Rx timestamping
29377a78115SJacob Keller * @pf: The PF pointer to search in
29477a78115SJacob Keller * @on: bool value for whether timestamps are enabled or disabled
29577a78115SJacob Keller */
ice_set_rx_tstamp(struct ice_pf * pf,bool on)29677a78115SJacob Keller static void ice_set_rx_tstamp(struct ice_pf *pf, bool on)
29777a78115SJacob Keller {
29877a78115SJacob Keller struct ice_vsi *vsi;
29977a78115SJacob Keller u16 i;
30077a78115SJacob Keller
30177a78115SJacob Keller vsi = ice_get_main_vsi(pf);
30277a78115SJacob Keller if (!vsi)
30377a78115SJacob Keller return;
30477a78115SJacob Keller
30577a78115SJacob Keller /* Set the timestamp flag for all the Rx rings */
30677a78115SJacob Keller ice_for_each_rxq(vsi, i) {
30777a78115SJacob Keller if (!vsi->rx_rings[i])
30877a78115SJacob Keller continue;
30977a78115SJacob Keller vsi->rx_rings[i]->ptp_rx = on;
31077a78115SJacob Keller }
311e59d75ddSJacob Keller
312e59d75ddSJacob Keller pf->ptp.tstamp_config.rx_filter = on ? HWTSTAMP_FILTER_ALL :
313e59d75ddSJacob Keller HWTSTAMP_FILTER_NONE;
31477a78115SJacob Keller }
31577a78115SJacob Keller
31677a78115SJacob Keller /**
31777a78115SJacob Keller * ice_ptp_cfg_timestamp - Configure timestamp for init/deinit
31877a78115SJacob Keller * @pf: Board private structure
31977a78115SJacob Keller * @ena: bool value to enable or disable time stamp
32077a78115SJacob Keller *
32177a78115SJacob Keller * This function will configure timestamping during PTP initialization
32277a78115SJacob Keller * and deinitialization
32377a78115SJacob Keller */
ice_ptp_cfg_timestamp(struct ice_pf * pf,bool ena)32448096710SKarol Kolacinski void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena)
32577a78115SJacob Keller {
326ea9b847cSJacob Keller ice_set_tx_tstamp(pf, ena);
32777a78115SJacob Keller ice_set_rx_tstamp(pf, ena);
32877a78115SJacob Keller }
32977a78115SJacob Keller
33077a78115SJacob Keller /**
33167569a7fSJacob Keller * ice_get_ptp_clock_index - Get the PTP clock index
33267569a7fSJacob Keller * @pf: the PF pointer
33367569a7fSJacob Keller *
33467569a7fSJacob Keller * Determine the clock index of the PTP clock associated with this device. If
33567569a7fSJacob Keller * this is the PF controlling the clock, just use the local access to the
33667569a7fSJacob Keller * clock device pointer.
33767569a7fSJacob Keller *
33867569a7fSJacob Keller * Otherwise, read from the driver shared parameters to determine the clock
33967569a7fSJacob Keller * index value.
34067569a7fSJacob Keller *
34167569a7fSJacob Keller * Returns: the index of the PTP clock associated with this device, or -1 if
34267569a7fSJacob Keller * there is no associated clock.
34367569a7fSJacob Keller */
ice_get_ptp_clock_index(struct ice_pf * pf)34467569a7fSJacob Keller int ice_get_ptp_clock_index(struct ice_pf *pf)
34567569a7fSJacob Keller {
34667569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf);
34767569a7fSJacob Keller enum ice_aqc_driver_params param_idx;
34867569a7fSJacob Keller struct ice_hw *hw = &pf->hw;
34967569a7fSJacob Keller u8 tmr_idx;
35067569a7fSJacob Keller u32 value;
35167569a7fSJacob Keller int err;
35267569a7fSJacob Keller
35367569a7fSJacob Keller /* Use the ptp_clock structure if we're the main PF */
35467569a7fSJacob Keller if (pf->ptp.clock)
35567569a7fSJacob Keller return ptp_clock_index(pf->ptp.clock);
35667569a7fSJacob Keller
35767569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc;
35867569a7fSJacob Keller if (!tmr_idx)
35967569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0;
36067569a7fSJacob Keller else
36167569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1;
36267569a7fSJacob Keller
36367569a7fSJacob Keller err = ice_aq_get_driver_param(hw, param_idx, &value, NULL);
36467569a7fSJacob Keller if (err) {
36567569a7fSJacob Keller dev_err(dev, "Failed to read PTP clock index parameter, err %d aq_err %s\n",
36667569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status));
36767569a7fSJacob Keller return -1;
36867569a7fSJacob Keller }
36967569a7fSJacob Keller
37067569a7fSJacob Keller /* The PTP clock index is an integer, and will be between 0 and
37167569a7fSJacob Keller * INT_MAX. The highest bit of the driver shared parameter is used to
37267569a7fSJacob Keller * indicate whether or not the currently stored clock index is valid.
37367569a7fSJacob Keller */
37467569a7fSJacob Keller if (!(value & PTP_SHARED_CLK_IDX_VALID))
37567569a7fSJacob Keller return -1;
37667569a7fSJacob Keller
37767569a7fSJacob Keller return value & ~PTP_SHARED_CLK_IDX_VALID;
37867569a7fSJacob Keller }
37967569a7fSJacob Keller
38067569a7fSJacob Keller /**
38167569a7fSJacob Keller * ice_set_ptp_clock_index - Set the PTP clock index
38267569a7fSJacob Keller * @pf: the PF pointer
38367569a7fSJacob Keller *
38467569a7fSJacob Keller * Set the PTP clock index for this device into the shared driver parameters,
38567569a7fSJacob Keller * so that other PFs associated with this device can read it.
38667569a7fSJacob Keller *
38767569a7fSJacob Keller * If the PF is unable to store the clock index, it will log an error, but
38867569a7fSJacob Keller * will continue operating PTP.
38967569a7fSJacob Keller */
ice_set_ptp_clock_index(struct ice_pf * pf)39067569a7fSJacob Keller static void ice_set_ptp_clock_index(struct ice_pf *pf)
39167569a7fSJacob Keller {
39267569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf);
39367569a7fSJacob Keller enum ice_aqc_driver_params param_idx;
39467569a7fSJacob Keller struct ice_hw *hw = &pf->hw;
39567569a7fSJacob Keller u8 tmr_idx;
39667569a7fSJacob Keller u32 value;
39767569a7fSJacob Keller int err;
39867569a7fSJacob Keller
39967569a7fSJacob Keller if (!pf->ptp.clock)
40067569a7fSJacob Keller return;
40167569a7fSJacob Keller
40267569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc;
40367569a7fSJacob Keller if (!tmr_idx)
40467569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0;
40567569a7fSJacob Keller else
40667569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1;
40767569a7fSJacob Keller
40867569a7fSJacob Keller value = (u32)ptp_clock_index(pf->ptp.clock);
40967569a7fSJacob Keller if (value > INT_MAX) {
41067569a7fSJacob Keller dev_err(dev, "PTP Clock index is too large to store\n");
41167569a7fSJacob Keller return;
41267569a7fSJacob Keller }
41367569a7fSJacob Keller value |= PTP_SHARED_CLK_IDX_VALID;
41467569a7fSJacob Keller
41567569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, value, NULL);
41667569a7fSJacob Keller if (err) {
41767569a7fSJacob Keller dev_err(dev, "Failed to set PTP clock index parameter, err %d aq_err %s\n",
41867569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status));
41967569a7fSJacob Keller }
42067569a7fSJacob Keller }
42167569a7fSJacob Keller
42267569a7fSJacob Keller /**
42367569a7fSJacob Keller * ice_clear_ptp_clock_index - Clear the PTP clock index
42467569a7fSJacob Keller * @pf: the PF pointer
42567569a7fSJacob Keller *
42667569a7fSJacob Keller * Clear the PTP clock index for this device. Must be called when
42767569a7fSJacob Keller * unregistering the PTP clock, in order to ensure other PFs stop reporting
42867569a7fSJacob Keller * a clock object that no longer exists.
42967569a7fSJacob Keller */
ice_clear_ptp_clock_index(struct ice_pf * pf)43067569a7fSJacob Keller static void ice_clear_ptp_clock_index(struct ice_pf *pf)
43167569a7fSJacob Keller {
43267569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf);
43367569a7fSJacob Keller enum ice_aqc_driver_params param_idx;
43467569a7fSJacob Keller struct ice_hw *hw = &pf->hw;
43567569a7fSJacob Keller u8 tmr_idx;
43667569a7fSJacob Keller int err;
43767569a7fSJacob Keller
43867569a7fSJacob Keller /* Do not clear the index if we don't own the timer */
43967569a7fSJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned)
44067569a7fSJacob Keller return;
44167569a7fSJacob Keller
44267569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc;
44367569a7fSJacob Keller if (!tmr_idx)
44467569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0;
44567569a7fSJacob Keller else
44667569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1;
44767569a7fSJacob Keller
44867569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, 0, NULL);
44967569a7fSJacob Keller if (err) {
45067569a7fSJacob Keller dev_dbg(dev, "Failed to clear PTP clock index parameter, err %d aq_err %s\n",
45167569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status));
45267569a7fSJacob Keller }
45367569a7fSJacob Keller }
45467569a7fSJacob Keller
45567569a7fSJacob Keller /**
45606c16d89SJacob Keller * ice_ptp_read_src_clk_reg - Read the source clock register
45706c16d89SJacob Keller * @pf: Board private structure
45806c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from
45906c16d89SJacob Keller * the system clock. Will be ignored if NULL is given.
46006c16d89SJacob Keller */
46106c16d89SJacob Keller static u64
ice_ptp_read_src_clk_reg(struct ice_pf * pf,struct ptp_system_timestamp * sts)46206c16d89SJacob Keller ice_ptp_read_src_clk_reg(struct ice_pf *pf, struct ptp_system_timestamp *sts)
46306c16d89SJacob Keller {
46406c16d89SJacob Keller struct ice_hw *hw = &pf->hw;
46506c16d89SJacob Keller u32 hi, lo, lo2;
46606c16d89SJacob Keller u8 tmr_idx;
46706c16d89SJacob Keller
46806c16d89SJacob Keller tmr_idx = ice_get_ptp_src_clock_index(hw);
46906c16d89SJacob Keller /* Read the system timestamp pre PHC read */
47006c16d89SJacob Keller ptp_read_system_prets(sts);
47106c16d89SJacob Keller
47206c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx));
47306c16d89SJacob Keller
47406c16d89SJacob Keller /* Read the system timestamp post PHC read */
47506c16d89SJacob Keller ptp_read_system_postts(sts);
47606c16d89SJacob Keller
47706c16d89SJacob Keller hi = rd32(hw, GLTSYN_TIME_H(tmr_idx));
47806c16d89SJacob Keller lo2 = rd32(hw, GLTSYN_TIME_L(tmr_idx));
47906c16d89SJacob Keller
48006c16d89SJacob Keller if (lo2 < lo) {
48106c16d89SJacob Keller /* if TIME_L rolled over read TIME_L again and update
48206c16d89SJacob Keller * system timestamps
48306c16d89SJacob Keller */
48406c16d89SJacob Keller ptp_read_system_prets(sts);
48506c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx));
48606c16d89SJacob Keller ptp_read_system_postts(sts);
48706c16d89SJacob Keller hi = rd32(hw, GLTSYN_TIME_H(tmr_idx));
48806c16d89SJacob Keller }
48906c16d89SJacob Keller
49006c16d89SJacob Keller return ((u64)hi << 32) | lo;
49106c16d89SJacob Keller }
49206c16d89SJacob Keller
49306c16d89SJacob Keller /**
49477a78115SJacob Keller * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b
49577a78115SJacob Keller * @cached_phc_time: recently cached copy of PHC time
49677a78115SJacob Keller * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value
49777a78115SJacob Keller *
49877a78115SJacob Keller * Hardware captures timestamps which contain only 32 bits of nominal
49977a78115SJacob Keller * nanoseconds, as opposed to the 64bit timestamps that the stack expects.
50077a78115SJacob Keller * Note that the captured timestamp values may be 40 bits, but the lower
50177a78115SJacob Keller * 8 bits are sub-nanoseconds and generally discarded.
50277a78115SJacob Keller *
50377a78115SJacob Keller * Extend the 32bit nanosecond timestamp using the following algorithm and
50477a78115SJacob Keller * assumptions:
50577a78115SJacob Keller *
50677a78115SJacob Keller * 1) have a recently cached copy of the PHC time
50777a78115SJacob Keller * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1
50877a78115SJacob Keller * seconds) before or after the PHC time was captured.
50977a78115SJacob Keller * 3) calculate the delta between the cached time and the timestamp
51077a78115SJacob Keller * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was
51177a78115SJacob Keller * captured after the PHC time. In this case, the full timestamp is just
51277a78115SJacob Keller * the cached PHC time plus the delta.
51377a78115SJacob Keller * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the
51477a78115SJacob Keller * timestamp was captured *before* the PHC time, i.e. because the PHC
51577a78115SJacob Keller * cache was updated after the timestamp was captured by hardware. In this
51677a78115SJacob Keller * case, the full timestamp is the cached time minus the inverse delta.
51777a78115SJacob Keller *
51877a78115SJacob Keller * This algorithm works even if the PHC time was updated after a Tx timestamp
51977a78115SJacob Keller * was requested, but before the Tx timestamp event was reported from
52077a78115SJacob Keller * hardware.
52177a78115SJacob Keller *
52277a78115SJacob Keller * This calculation primarily relies on keeping the cached PHC time up to
52377a78115SJacob Keller * date. If the timestamp was captured more than 2^31 nanoseconds after the
52477a78115SJacob Keller * PHC time, it is possible that the lower 32bits of PHC time have
52577a78115SJacob Keller * overflowed more than once, and we might generate an incorrect timestamp.
52677a78115SJacob Keller *
52777a78115SJacob Keller * This is prevented by (a) periodically updating the cached PHC time once
52877a78115SJacob Keller * a second, and (b) discarding any Tx timestamp packet if it has waited for
52977a78115SJacob Keller * a timestamp for more than one second.
53077a78115SJacob Keller */
ice_ptp_extend_32b_ts(u64 cached_phc_time,u32 in_tstamp)53177a78115SJacob Keller static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp)
53277a78115SJacob Keller {
53377a78115SJacob Keller u32 delta, phc_time_lo;
53477a78115SJacob Keller u64 ns;
53577a78115SJacob Keller
53677a78115SJacob Keller /* Extract the lower 32 bits of the PHC time */
53777a78115SJacob Keller phc_time_lo = (u32)cached_phc_time;
53877a78115SJacob Keller
53977a78115SJacob Keller /* Calculate the delta between the lower 32bits of the cached PHC
54077a78115SJacob Keller * time and the in_tstamp value
54177a78115SJacob Keller */
54277a78115SJacob Keller delta = (in_tstamp - phc_time_lo);
54377a78115SJacob Keller
54477a78115SJacob Keller /* Do not assume that the in_tstamp is always more recent than the
54577a78115SJacob Keller * cached PHC time. If the delta is large, it indicates that the
54677a78115SJacob Keller * in_tstamp was taken in the past, and should be converted
54777a78115SJacob Keller * forward.
54877a78115SJacob Keller */
54977a78115SJacob Keller if (delta > (U32_MAX / 2)) {
55077a78115SJacob Keller /* reverse the delta calculation here */
55177a78115SJacob Keller delta = (phc_time_lo - in_tstamp);
55277a78115SJacob Keller ns = cached_phc_time - delta;
55377a78115SJacob Keller } else {
55477a78115SJacob Keller ns = cached_phc_time + delta;
55577a78115SJacob Keller }
55677a78115SJacob Keller
55777a78115SJacob Keller return ns;
55877a78115SJacob Keller }
55977a78115SJacob Keller
56077a78115SJacob Keller /**
561ea9b847cSJacob Keller * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds
562ea9b847cSJacob Keller * @pf: Board private structure
563ea9b847cSJacob Keller * @in_tstamp: Ingress/egress 40b timestamp value
564ea9b847cSJacob Keller *
565ea9b847cSJacob Keller * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal
566ea9b847cSJacob Keller * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit.
567ea9b847cSJacob Keller *
568ea9b847cSJacob Keller * *--------------------------------------------------------------*
569ea9b847cSJacob Keller * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v |
570ea9b847cSJacob Keller * *--------------------------------------------------------------*
571ea9b847cSJacob Keller *
572ea9b847cSJacob Keller * The low bit is an indicator of whether the timestamp is valid. The next
573ea9b847cSJacob Keller * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow,
574ea9b847cSJacob Keller * and the remaining 32 bits are the lower 32 bits of the PHC timer.
575ea9b847cSJacob Keller *
576ea9b847cSJacob Keller * It is assumed that the caller verifies the timestamp is valid prior to
577ea9b847cSJacob Keller * calling this function.
578ea9b847cSJacob Keller *
579ea9b847cSJacob Keller * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC
580ea9b847cSJacob Keller * time stored in the device private PTP structure as the basis for timestamp
581ea9b847cSJacob Keller * extension.
582ea9b847cSJacob Keller *
583ea9b847cSJacob Keller * See ice_ptp_extend_32b_ts for a detailed explanation of the extension
584ea9b847cSJacob Keller * algorithm.
585ea9b847cSJacob Keller */
ice_ptp_extend_40b_ts(struct ice_pf * pf,u64 in_tstamp)586ea9b847cSJacob Keller static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp)
587ea9b847cSJacob Keller {
588ea9b847cSJacob Keller const u64 mask = GENMASK_ULL(31, 0);
589cd25507aSJacob Keller unsigned long discard_time;
590cd25507aSJacob Keller
591cd25507aSJacob Keller /* Discard the hardware timestamp if the cached PHC time is too old */
592cd25507aSJacob Keller discard_time = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000);
593cd25507aSJacob Keller if (time_is_before_jiffies(discard_time)) {
594cd25507aSJacob Keller pf->ptp.tx_hwtstamp_discarded++;
595cd25507aSJacob Keller return 0;
596cd25507aSJacob Keller }
597ea9b847cSJacob Keller
598ea9b847cSJacob Keller return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time,
599ea9b847cSJacob Keller (in_tstamp >> 8) & mask);
600ea9b847cSJacob Keller }
601ea9b847cSJacob Keller
602ea9b847cSJacob Keller /**
6033ad5c10bSJacob Keller * ice_ptp_is_tx_tracker_up - Check if Tx tracker is ready for new timestamps
6043ad5c10bSJacob Keller * @tx: the PTP Tx timestamp tracker to check
6053ad5c10bSJacob Keller *
6063ad5c10bSJacob Keller * Check that a given PTP Tx timestamp tracker is up, i.e. that it is ready
6073ad5c10bSJacob Keller * to accept new timestamp requests.
6083ad5c10bSJacob Keller *
6093ad5c10bSJacob Keller * Assumes the tx->lock spinlock is already held.
6103ad5c10bSJacob Keller */
6113ad5c10bSJacob Keller static bool
ice_ptp_is_tx_tracker_up(struct ice_ptp_tx * tx)6123ad5c10bSJacob Keller ice_ptp_is_tx_tracker_up(struct ice_ptp_tx *tx)
6133ad5c10bSJacob Keller {
6143ad5c10bSJacob Keller lockdep_assert_held(&tx->lock);
6153ad5c10bSJacob Keller
6163ad5c10bSJacob Keller return tx->init && !tx->calibrating;
6173ad5c10bSJacob Keller }
6183ad5c10bSJacob Keller
6193ad5c10bSJacob Keller /**
620ae39eb42SJacob Keller * ice_ptp_process_tx_tstamp - Process Tx timestamps for a port
6211229b339SKarol Kolacinski * @tx: the PTP Tx timestamp tracker
6224b1251bdSJacob Keller *
6234b1251bdSJacob Keller * Process timestamps captured by the PHY associated with this port. To do
6244b1251bdSJacob Keller * this, loop over each index with a waiting skb.
6254b1251bdSJacob Keller *
6264b1251bdSJacob Keller * If a given index has a valid timestamp, perform the following steps:
6274b1251bdSJacob Keller *
628d40fd600SJacob Keller * 1) check that the timestamp request is not stale
629d40fd600SJacob Keller * 2) check that a timestamp is ready and available in the PHY memory bank
630d40fd600SJacob Keller * 3) read and copy the timestamp out of the PHY register
631d40fd600SJacob Keller * 4) unlock the index by clearing the associated in_use bit
632d40fd600SJacob Keller * 5) check if the timestamp is stale, and discard if so
633d40fd600SJacob Keller * 6) extend the 40 bit timestamp value to get a 64 bit timestamp value
634d40fd600SJacob Keller * 7) send this 64 bit timestamp to the stack
6354b1251bdSJacob Keller *
636d40fd600SJacob Keller * Note that we do not hold the tracking lock while reading the Tx timestamp.
637d40fd600SJacob Keller * This is because reading the timestamp requires taking a mutex that might
638d40fd600SJacob Keller * sleep.
6390dd92862SJacob Keller *
640d40fd600SJacob Keller * The only place where we set in_use is when a new timestamp is initiated
641d40fd600SJacob Keller * with a slot index. This is only called in the hard xmit routine where an
642d40fd600SJacob Keller * SKB has a request flag set. The only places where we clear this bit is this
643d40fd600SJacob Keller * function, or during teardown when the Tx timestamp tracker is being
644d40fd600SJacob Keller * removed. A timestamp index will never be re-used until the in_use bit for
645d40fd600SJacob Keller * that index is cleared.
6460dd92862SJacob Keller *
6470dd92862SJacob Keller * If a Tx thread starts a new timestamp, we might not begin processing it
6480dd92862SJacob Keller * right away but we will notice it at the end when we re-queue the task.
6490dd92862SJacob Keller *
6500dd92862SJacob Keller * If a Tx thread starts a new timestamp just after this function exits, the
6510dd92862SJacob Keller * interrupt for that timestamp should re-trigger this function once
6520dd92862SJacob Keller * a timestamp is ready.
6530dd92862SJacob Keller *
654d40fd600SJacob Keller * In cases where the PTP hardware clock was directly adjusted, some
655d40fd600SJacob Keller * timestamps may not be able to safely use the timestamp extension math. In
656d40fd600SJacob Keller * this case, software will set the stale bit for any outstanding Tx
657d40fd600SJacob Keller * timestamps when the clock is adjusted. Then this function will discard
658d40fd600SJacob Keller * those captured timestamps instead of sending them to the stack.
6590dd92862SJacob Keller *
6600dd92862SJacob Keller * If a Tx packet has been waiting for more than 2 seconds, it is not possible
6610dd92862SJacob Keller * to correctly extend the timestamp using the cached PHC time. It is
6620dd92862SJacob Keller * extremely unlikely that a packet will ever take this long to timestamp. If
6630dd92862SJacob Keller * we detect a Tx timestamp request that has waited for this long we assume
6640dd92862SJacob Keller * the packet will never be sent by hardware and discard it without reading
6650dd92862SJacob Keller * the timestamp register.
6664b1251bdSJacob Keller */
ice_ptp_process_tx_tstamp(struct ice_ptp_tx * tx)667ae39eb42SJacob Keller static void ice_ptp_process_tx_tstamp(struct ice_ptp_tx *tx)
6684b1251bdSJacob Keller {
6694b1251bdSJacob Keller struct ice_ptp_port *ptp_port;
6704b1251bdSJacob Keller struct ice_pf *pf;
67110e4b4a3SJacob Keller struct ice_hw *hw;
67210e4b4a3SJacob Keller u64 tstamp_ready;
673fcc2cef3SDaniel Vacek bool link_up;
67410e4b4a3SJacob Keller int err;
6754b1251bdSJacob Keller u8 idx;
6764b1251bdSJacob Keller
6774b1251bdSJacob Keller if (!tx->init)
678ae39eb42SJacob Keller return;
6794b1251bdSJacob Keller
6804b1251bdSJacob Keller ptp_port = container_of(tx, struct ice_ptp_port, tx);
6814b1251bdSJacob Keller pf = ptp_port_to_pf(ptp_port);
68210e4b4a3SJacob Keller hw = &pf->hw;
68310e4b4a3SJacob Keller
68410e4b4a3SJacob Keller /* Read the Tx ready status first */
68510e4b4a3SJacob Keller err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready);
68610e4b4a3SJacob Keller if (err)
687ae39eb42SJacob Keller return;
6884b1251bdSJacob Keller
689fcc2cef3SDaniel Vacek /* Drop packets if the link went down */
690fcc2cef3SDaniel Vacek link_up = ptp_port->link_up;
691fcc2cef3SDaniel Vacek
6924b1251bdSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) {
6934b1251bdSJacob Keller struct skb_shared_hwtstamps shhwtstamps = {};
6946b5cbc8cSSergey Temerkhanov u8 phy_idx = idx + tx->offset;
6950dd92862SJacob Keller u64 raw_tstamp = 0, tstamp;
696fcc2cef3SDaniel Vacek bool drop_ts = !link_up;
6974b1251bdSJacob Keller struct sk_buff *skb;
6984b1251bdSJacob Keller
6990dd92862SJacob Keller /* Drop packets which have waited for more than 2 seconds */
7000dd92862SJacob Keller if (time_is_before_jiffies(tx->tstamps[idx].start + 2 * HZ)) {
7010dd92862SJacob Keller drop_ts = true;
7020dd92862SJacob Keller
7030dd92862SJacob Keller /* Count the number of Tx timestamps that timed out */
7040dd92862SJacob Keller pf->ptp.tx_hwtstamp_timeouts++;
70510e4b4a3SJacob Keller }
7060dd92862SJacob Keller
70710e4b4a3SJacob Keller /* Only read a timestamp from the PHY if its marked as ready
70810e4b4a3SJacob Keller * by the tstamp_ready register. This avoids unnecessary
70910e4b4a3SJacob Keller * reading of timestamps which are not yet valid. This is
71010e4b4a3SJacob Keller * important as we must read all timestamps which are valid
71110e4b4a3SJacob Keller * and only timestamps which are valid during each interrupt.
71210e4b4a3SJacob Keller * If we do not, the hardware logic for generating a new
71310e4b4a3SJacob Keller * interrupt can get stuck on some devices.
71410e4b4a3SJacob Keller */
71510e4b4a3SJacob Keller if (!(tstamp_ready & BIT_ULL(phy_idx))) {
71610e4b4a3SJacob Keller if (drop_ts)
7170dd92862SJacob Keller goto skip_ts_read;
71810e4b4a3SJacob Keller
71910e4b4a3SJacob Keller continue;
7200dd92862SJacob Keller }
7210dd92862SJacob Keller
7224b1251bdSJacob Keller ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx);
7234b1251bdSJacob Keller
72410e4b4a3SJacob Keller err = ice_read_phy_tstamp(hw, tx->block, phy_idx, &raw_tstamp);
725fcc2cef3SDaniel Vacek if (err && !drop_ts)
7264b1251bdSJacob Keller continue;
7274b1251bdSJacob Keller
7284b1251bdSJacob Keller ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx);
7294b1251bdSJacob Keller
73010e4b4a3SJacob Keller /* For PHYs which don't implement a proper timestamp ready
73110e4b4a3SJacob Keller * bitmap, verify that the timestamp value is different
73210e4b4a3SJacob Keller * from the last cached timestamp. If it is not, skip this for
73310e4b4a3SJacob Keller * now assuming it hasn't yet been captured by hardware.
73410e4b4a3SJacob Keller */
73510e4b4a3SJacob Keller if (!drop_ts && tx->verify_cached &&
7364b1251bdSJacob Keller raw_tstamp == tx->tstamps[idx].cached_tstamp)
7374b1251bdSJacob Keller continue;
7384b1251bdSJacob Keller
73910e4b4a3SJacob Keller /* Discard any timestamp value without the valid bit set */
74010e4b4a3SJacob Keller if (!(raw_tstamp & ICE_PTP_TS_VALID))
74110e4b4a3SJacob Keller drop_ts = true;
74210e4b4a3SJacob Keller
7430dd92862SJacob Keller skip_ts_read:
7444b1251bdSJacob Keller spin_lock(&tx->lock);
74510e4b4a3SJacob Keller if (tx->verify_cached && raw_tstamp)
7464b1251bdSJacob Keller tx->tstamps[idx].cached_tstamp = raw_tstamp;
7474b1251bdSJacob Keller clear_bit(idx, tx->in_use);
7484b1251bdSJacob Keller skb = tx->tstamps[idx].skb;
7494b1251bdSJacob Keller tx->tstamps[idx].skb = NULL;
750d40fd600SJacob Keller if (test_and_clear_bit(idx, tx->stale))
751d40fd600SJacob Keller drop_ts = true;
7524b1251bdSJacob Keller spin_unlock(&tx->lock);
7534b1251bdSJacob Keller
7540dd92862SJacob Keller /* It is unlikely but possible that the SKB will have been
7550dd92862SJacob Keller * flushed at this point due to link change or teardown.
7564b1251bdSJacob Keller */
7574b1251bdSJacob Keller if (!skb)
7584b1251bdSJacob Keller continue;
7594b1251bdSJacob Keller
7600dd92862SJacob Keller if (drop_ts) {
7610dd92862SJacob Keller dev_kfree_skb_any(skb);
7620dd92862SJacob Keller continue;
7630dd92862SJacob Keller }
7640dd92862SJacob Keller
7654b1251bdSJacob Keller /* Extend the timestamp using cached PHC time */
7664b1251bdSJacob Keller tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp);
7674b1251bdSJacob Keller if (tstamp) {
7684b1251bdSJacob Keller shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
7694b1251bdSJacob Keller ice_trace(tx_tstamp_complete, skb, idx);
7704b1251bdSJacob Keller }
7714b1251bdSJacob Keller
7724b1251bdSJacob Keller skb_tstamp_tx(skb, &shhwtstamps);
7734b1251bdSJacob Keller dev_kfree_skb_any(skb);
7744b1251bdSJacob Keller }
775ae39eb42SJacob Keller }
7764b1251bdSJacob Keller
777ae39eb42SJacob Keller /**
778ae39eb42SJacob Keller * ice_ptp_tx_tstamp - Process Tx timestamps for this function.
779ae39eb42SJacob Keller * @tx: Tx tracking structure to initialize
780ae39eb42SJacob Keller *
781ae39eb42SJacob Keller * Returns: ICE_TX_TSTAMP_WORK_PENDING if there are any outstanding incomplete
782ae39eb42SJacob Keller * Tx timestamps, or ICE_TX_TSTAMP_WORK_DONE otherwise.
7834b1251bdSJacob Keller */
ice_ptp_tx_tstamp(struct ice_ptp_tx * tx)784ae39eb42SJacob Keller static enum ice_tx_tstamp_work ice_ptp_tx_tstamp(struct ice_ptp_tx *tx)
785ae39eb42SJacob Keller {
786ae39eb42SJacob Keller bool more_timestamps;
787ae39eb42SJacob Keller
788ae39eb42SJacob Keller if (!tx->init)
789ae39eb42SJacob Keller return ICE_TX_TSTAMP_WORK_DONE;
790ae39eb42SJacob Keller
791ae39eb42SJacob Keller /* Process the Tx timestamp tracker */
792ae39eb42SJacob Keller ice_ptp_process_tx_tstamp(tx);
793ae39eb42SJacob Keller
794ae39eb42SJacob Keller /* Check if there are outstanding Tx timestamps */
7954b1251bdSJacob Keller spin_lock(&tx->lock);
796f0ae1240SJacob Keller more_timestamps = tx->init && !bitmap_empty(tx->in_use, tx->len);
7974b1251bdSJacob Keller spin_unlock(&tx->lock);
7981229b339SKarol Kolacinski
799ae39eb42SJacob Keller if (more_timestamps)
800ae39eb42SJacob Keller return ICE_TX_TSTAMP_WORK_PENDING;
801ae39eb42SJacob Keller
802ae39eb42SJacob Keller return ICE_TX_TSTAMP_WORK_DONE;
8034b1251bdSJacob Keller }
8044b1251bdSJacob Keller
8054b1251bdSJacob Keller /**
8064b1251bdSJacob Keller * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps
8074b1251bdSJacob Keller * @tx: Tx tracking structure to initialize
8084b1251bdSJacob Keller *
8094b1251bdSJacob Keller * Assumes that the length has already been initialized. Do not call directly,
8106b5cbc8cSSergey Temerkhanov * use the ice_ptp_init_tx_* instead.
8114b1251bdSJacob Keller */
8124b1251bdSJacob Keller static int
ice_ptp_alloc_tx_tracker(struct ice_ptp_tx * tx)8134b1251bdSJacob Keller ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx)
8144b1251bdSJacob Keller {
815d40fd600SJacob Keller unsigned long *in_use, *stale;
816c1f3414dSJacob Keller struct ice_tx_tstamp *tstamps;
8174b1251bdSJacob Keller
818c1f3414dSJacob Keller tstamps = kcalloc(tx->len, sizeof(*tstamps), GFP_KERNEL);
819c1f3414dSJacob Keller in_use = bitmap_zalloc(tx->len, GFP_KERNEL);
820d40fd600SJacob Keller stale = bitmap_zalloc(tx->len, GFP_KERNEL);
821c1f3414dSJacob Keller
822d40fd600SJacob Keller if (!tstamps || !in_use || !stale) {
823c1f3414dSJacob Keller kfree(tstamps);
824c1f3414dSJacob Keller bitmap_free(in_use);
825d40fd600SJacob Keller bitmap_free(stale);
826c1f3414dSJacob Keller
8274b1251bdSJacob Keller return -ENOMEM;
8284b1251bdSJacob Keller }
8294b1251bdSJacob Keller
830c1f3414dSJacob Keller tx->tstamps = tstamps;
831c1f3414dSJacob Keller tx->in_use = in_use;
832d40fd600SJacob Keller tx->stale = stale;
8334b1251bdSJacob Keller tx->init = 1;
8344b1251bdSJacob Keller
8353ad5c10bSJacob Keller spin_lock_init(&tx->lock);
8363ad5c10bSJacob Keller
8374b1251bdSJacob Keller return 0;
8384b1251bdSJacob Keller }
8394b1251bdSJacob Keller
8404b1251bdSJacob Keller /**
8414b1251bdSJacob Keller * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker
8424b1251bdSJacob Keller * @pf: Board private structure
8434b1251bdSJacob Keller * @tx: the tracker to flush
844e3ba5248SJacob Keller *
845e3ba5248SJacob Keller * Called during teardown when a Tx tracker is being removed.
8464b1251bdSJacob Keller */
8474b1251bdSJacob Keller static void
ice_ptp_flush_tx_tracker(struct ice_pf * pf,struct ice_ptp_tx * tx)8484b1251bdSJacob Keller ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
8494b1251bdSJacob Keller {
850e3ba5248SJacob Keller struct ice_hw *hw = &pf->hw;
851e3ba5248SJacob Keller u64 tstamp_ready;
852e3ba5248SJacob Keller int err;
8534b1251bdSJacob Keller u8 idx;
8544b1251bdSJacob Keller
855e3ba5248SJacob Keller err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready);
856e3ba5248SJacob Keller if (err) {
857e3ba5248SJacob Keller dev_dbg(ice_pf_to_dev(pf), "Failed to get the Tx tstamp ready bitmap for block %u, err %d\n",
858e3ba5248SJacob Keller tx->block, err);
859e3ba5248SJacob Keller
860e3ba5248SJacob Keller /* If we fail to read the Tx timestamp ready bitmap just
861e3ba5248SJacob Keller * skip clearing the PHY timestamps.
862e3ba5248SJacob Keller */
863e3ba5248SJacob Keller tstamp_ready = 0;
864e3ba5248SJacob Keller }
865e3ba5248SJacob Keller
866e3ba5248SJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) {
8676b5cbc8cSSergey Temerkhanov u8 phy_idx = idx + tx->offset;
868e3ba5248SJacob Keller struct sk_buff *skb;
869e3ba5248SJacob Keller
870e3ba5248SJacob Keller /* In case this timestamp is ready, we need to clear it. */
871e3ba5248SJacob Keller if (!hw->reset_ongoing && (tstamp_ready & BIT_ULL(phy_idx)))
872e3ba5248SJacob Keller ice_clear_phy_tstamp(hw, tx->block, phy_idx);
8734b1251bdSJacob Keller
8744b1251bdSJacob Keller spin_lock(&tx->lock);
875e3ba5248SJacob Keller skb = tx->tstamps[idx].skb;
8764b1251bdSJacob Keller tx->tstamps[idx].skb = NULL;
8774b1251bdSJacob Keller clear_bit(idx, tx->in_use);
878d40fd600SJacob Keller clear_bit(idx, tx->stale);
8794b1251bdSJacob Keller spin_unlock(&tx->lock);
8804b1251bdSJacob Keller
881e3ba5248SJacob Keller /* Count the number of Tx timestamps flushed */
882e3ba5248SJacob Keller pf->ptp.tx_hwtstamp_flushed++;
883e3ba5248SJacob Keller
884e3ba5248SJacob Keller /* Free the SKB after we've cleared the bit */
885e3ba5248SJacob Keller dev_kfree_skb_any(skb);
8864b1251bdSJacob Keller }
8874b1251bdSJacob Keller }
8884b1251bdSJacob Keller
8894b1251bdSJacob Keller /**
890d40fd600SJacob Keller * ice_ptp_mark_tx_tracker_stale - Mark unfinished timestamps as stale
891d40fd600SJacob Keller * @tx: the tracker to mark
892d40fd600SJacob Keller *
893d40fd600SJacob Keller * Mark currently outstanding Tx timestamps as stale. This prevents sending
894d40fd600SJacob Keller * their timestamp value to the stack. This is required to prevent extending
895d40fd600SJacob Keller * the 40bit hardware timestamp incorrectly.
896d40fd600SJacob Keller *
897d40fd600SJacob Keller * This should be called when the PTP clock is modified such as after a set
898d40fd600SJacob Keller * time request.
899d40fd600SJacob Keller */
900d40fd600SJacob Keller static void
ice_ptp_mark_tx_tracker_stale(struct ice_ptp_tx * tx)901d40fd600SJacob Keller ice_ptp_mark_tx_tracker_stale(struct ice_ptp_tx *tx)
902d40fd600SJacob Keller {
903d40fd600SJacob Keller spin_lock(&tx->lock);
904d40fd600SJacob Keller bitmap_or(tx->stale, tx->stale, tx->in_use, tx->len);
905d40fd600SJacob Keller spin_unlock(&tx->lock);
906d40fd600SJacob Keller }
907d40fd600SJacob Keller
908d40fd600SJacob Keller /**
9094b1251bdSJacob Keller * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker
9104b1251bdSJacob Keller * @pf: Board private structure
9114b1251bdSJacob Keller * @tx: Tx tracking structure to release
9124b1251bdSJacob Keller *
9134b1251bdSJacob Keller * Free memory associated with the Tx timestamp tracker.
9144b1251bdSJacob Keller */
9154b1251bdSJacob Keller static void
ice_ptp_release_tx_tracker(struct ice_pf * pf,struct ice_ptp_tx * tx)9164b1251bdSJacob Keller ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
9174b1251bdSJacob Keller {
9183ad5c10bSJacob Keller spin_lock(&tx->lock);
9194b1251bdSJacob Keller tx->init = 0;
9203ad5c10bSJacob Keller spin_unlock(&tx->lock);
9214b1251bdSJacob Keller
922f0ae1240SJacob Keller /* wait for potentially outstanding interrupt to complete */
9234aad5335SPiotr Raczynski synchronize_irq(pf->oicr_irq.virq);
924f0ae1240SJacob Keller
9254b1251bdSJacob Keller ice_ptp_flush_tx_tracker(pf, tx);
9264b1251bdSJacob Keller
9274b1251bdSJacob Keller kfree(tx->tstamps);
9284b1251bdSJacob Keller tx->tstamps = NULL;
9294b1251bdSJacob Keller
9304b1251bdSJacob Keller bitmap_free(tx->in_use);
9314b1251bdSJacob Keller tx->in_use = NULL;
9324b1251bdSJacob Keller
933d40fd600SJacob Keller bitmap_free(tx->stale);
934d40fd600SJacob Keller tx->stale = NULL;
935d40fd600SJacob Keller
9364b1251bdSJacob Keller tx->len = 0;
9374b1251bdSJacob Keller }
9384b1251bdSJacob Keller
9394b1251bdSJacob Keller /**
9404b1251bdSJacob Keller * ice_ptp_init_tx_e822 - Initialize tracking for Tx timestamps
9414b1251bdSJacob Keller * @pf: Board private structure
9424b1251bdSJacob Keller * @tx: the Tx tracking structure to initialize
9434b1251bdSJacob Keller * @port: the port this structure tracks
9444b1251bdSJacob Keller *
9454b1251bdSJacob Keller * Initialize the Tx timestamp tracker for this port. For generic MAC devices,
9464b1251bdSJacob Keller * the timestamp block is shared for all ports in the same quad. To avoid
9474b1251bdSJacob Keller * ports using the same timestamp index, logically break the block of
9484b1251bdSJacob Keller * registers into chunks based on the port number.
9494b1251bdSJacob Keller */
9504b1251bdSJacob Keller static int
ice_ptp_init_tx_e822(struct ice_pf * pf,struct ice_ptp_tx * tx,u8 port)9514b1251bdSJacob Keller ice_ptp_init_tx_e822(struct ice_pf *pf, struct ice_ptp_tx *tx, u8 port)
9524b1251bdSJacob Keller {
9536b5cbc8cSSergey Temerkhanov tx->block = port / ICE_PORTS_PER_QUAD;
9546b5cbc8cSSergey Temerkhanov tx->offset = (port % ICE_PORTS_PER_QUAD) * INDEX_PER_PORT_E822;
9556b5cbc8cSSergey Temerkhanov tx->len = INDEX_PER_PORT_E822;
95610e4b4a3SJacob Keller tx->verify_cached = 0;
9574b1251bdSJacob Keller
9584b1251bdSJacob Keller return ice_ptp_alloc_tx_tracker(tx);
9594b1251bdSJacob Keller }
9604b1251bdSJacob Keller
9614b1251bdSJacob Keller /**
9624b1251bdSJacob Keller * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps
9634b1251bdSJacob Keller * @pf: Board private structure
9644b1251bdSJacob Keller * @tx: the Tx tracking structure to initialize
9654b1251bdSJacob Keller *
9664b1251bdSJacob Keller * Initialize the Tx timestamp tracker for this PF. For E810 devices, each
9674b1251bdSJacob Keller * port has its own block of timestamps, independent of the other ports.
9684b1251bdSJacob Keller */
9694b1251bdSJacob Keller static int
ice_ptp_init_tx_e810(struct ice_pf * pf,struct ice_ptp_tx * tx)9704b1251bdSJacob Keller ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx)
9714b1251bdSJacob Keller {
9726b5cbc8cSSergey Temerkhanov tx->block = pf->hw.port_info->lport;
9736b5cbc8cSSergey Temerkhanov tx->offset = 0;
9746b5cbc8cSSergey Temerkhanov tx->len = INDEX_PER_PORT_E810;
97510e4b4a3SJacob Keller /* The E810 PHY does not provide a timestamp ready bitmap. Instead,
97610e4b4a3SJacob Keller * verify new timestamps against cached copy of the last read
97710e4b4a3SJacob Keller * timestamp.
97810e4b4a3SJacob Keller */
97910e4b4a3SJacob Keller tx->verify_cached = 1;
9804b1251bdSJacob Keller
9814b1251bdSJacob Keller return ice_ptp_alloc_tx_tracker(tx);
9824b1251bdSJacob Keller }
9834b1251bdSJacob Keller
9844b1251bdSJacob Keller /**
9854b1251bdSJacob Keller * ice_ptp_update_cached_phctime - Update the cached PHC time values
9864b1251bdSJacob Keller * @pf: Board specific private structure
9874b1251bdSJacob Keller *
9884b1251bdSJacob Keller * This function updates the system time values which are cached in the PF
9894b1251bdSJacob Keller * structure and the Rx rings.
9904b1251bdSJacob Keller *
9914b1251bdSJacob Keller * This function must be called periodically to ensure that the cached value
992b1a582e6SJacob Keller * is never more than 2 seconds old.
993b1a582e6SJacob Keller *
994b1a582e6SJacob Keller * Note that the cached copy in the PF PTP structure is always updated, even
995b1a582e6SJacob Keller * if we can't update the copy in the Rx rings.
9964b1251bdSJacob Keller *
9974b1251bdSJacob Keller * Return:
9984b1251bdSJacob Keller * * 0 - OK, successfully updated
9994b1251bdSJacob Keller * * -EAGAIN - PF was busy, need to reschedule the update
10004b1251bdSJacob Keller */
ice_ptp_update_cached_phctime(struct ice_pf * pf)10014b1251bdSJacob Keller static int ice_ptp_update_cached_phctime(struct ice_pf *pf)
10024b1251bdSJacob Keller {
10034b1251bdSJacob Keller struct device *dev = ice_pf_to_dev(pf);
10044b1251bdSJacob Keller unsigned long update_before;
10054b1251bdSJacob Keller u64 systime;
10064b1251bdSJacob Keller int i;
10074b1251bdSJacob Keller
10084b1251bdSJacob Keller update_before = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000);
10094b1251bdSJacob Keller if (pf->ptp.cached_phc_time &&
10104b1251bdSJacob Keller time_is_before_jiffies(update_before)) {
10114b1251bdSJacob Keller unsigned long time_taken = jiffies - pf->ptp.cached_phc_jiffies;
10124b1251bdSJacob Keller
10134b1251bdSJacob Keller dev_warn(dev, "%u msecs passed between update to cached PHC time\n",
10144b1251bdSJacob Keller jiffies_to_msecs(time_taken));
10154b1251bdSJacob Keller pf->ptp.late_cached_phc_updates++;
10164b1251bdSJacob Keller }
10174b1251bdSJacob Keller
10184b1251bdSJacob Keller /* Read the current PHC time */
10194b1251bdSJacob Keller systime = ice_ptp_read_src_clk_reg(pf, NULL);
10204b1251bdSJacob Keller
10214b1251bdSJacob Keller /* Update the cached PHC time stored in the PF structure */
10224b1251bdSJacob Keller WRITE_ONCE(pf->ptp.cached_phc_time, systime);
10234b1251bdSJacob Keller WRITE_ONCE(pf->ptp.cached_phc_jiffies, jiffies);
10244b1251bdSJacob Keller
1025b1a582e6SJacob Keller if (test_and_set_bit(ICE_CFG_BUSY, pf->state))
1026b1a582e6SJacob Keller return -EAGAIN;
1027b1a582e6SJacob Keller
10284b1251bdSJacob Keller ice_for_each_vsi(pf, i) {
10294b1251bdSJacob Keller struct ice_vsi *vsi = pf->vsi[i];
10304b1251bdSJacob Keller int j;
10314b1251bdSJacob Keller
10324b1251bdSJacob Keller if (!vsi)
10334b1251bdSJacob Keller continue;
10344b1251bdSJacob Keller
10354b1251bdSJacob Keller if (vsi->type != ICE_VSI_PF)
10364b1251bdSJacob Keller continue;
10374b1251bdSJacob Keller
10384b1251bdSJacob Keller ice_for_each_rxq(vsi, j) {
10394b1251bdSJacob Keller if (!vsi->rx_rings[j])
10404b1251bdSJacob Keller continue;
10414b1251bdSJacob Keller WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime);
10424b1251bdSJacob Keller }
10434b1251bdSJacob Keller }
10444b1251bdSJacob Keller clear_bit(ICE_CFG_BUSY, pf->state);
10454b1251bdSJacob Keller
10464b1251bdSJacob Keller return 0;
10474b1251bdSJacob Keller }
10484b1251bdSJacob Keller
10494b1251bdSJacob Keller /**
1050b1a582e6SJacob Keller * ice_ptp_reset_cached_phctime - Reset cached PHC time after an update
1051b1a582e6SJacob Keller * @pf: Board specific private structure
1052b1a582e6SJacob Keller *
1053b1a582e6SJacob Keller * This function must be called when the cached PHC time is no longer valid,
1054d40fd600SJacob Keller * such as after a time adjustment. It marks any currently outstanding Tx
1055d40fd600SJacob Keller * timestamps as stale and updates the cached PHC time for both the PF and Rx
1056d40fd600SJacob Keller * rings.
1057b1a582e6SJacob Keller *
1058d40fd600SJacob Keller * If updating the PHC time cannot be done immediately, a warning message is
1059d40fd600SJacob Keller * logged and the work item is scheduled immediately to minimize the window
1060d40fd600SJacob Keller * with a wrong cached timestamp.
1061b1a582e6SJacob Keller */
ice_ptp_reset_cached_phctime(struct ice_pf * pf)1062b1a582e6SJacob Keller static void ice_ptp_reset_cached_phctime(struct ice_pf *pf)
1063b1a582e6SJacob Keller {
1064b1a582e6SJacob Keller struct device *dev = ice_pf_to_dev(pf);
1065b1a582e6SJacob Keller int err;
1066b1a582e6SJacob Keller
1067b1a582e6SJacob Keller /* Update the cached PHC time immediately if possible, otherwise
1068b1a582e6SJacob Keller * schedule the work item to execute soon.
1069b1a582e6SJacob Keller */
1070b1a582e6SJacob Keller err = ice_ptp_update_cached_phctime(pf);
1071b1a582e6SJacob Keller if (err) {
1072b1a582e6SJacob Keller /* If another thread is updating the Rx rings, we won't
1073b1a582e6SJacob Keller * properly reset them here. This could lead to reporting of
1074b1a582e6SJacob Keller * invalid timestamps, but there isn't much we can do.
1075b1a582e6SJacob Keller */
1076b1a582e6SJacob Keller dev_warn(dev, "%s: ICE_CFG_BUSY, unable to immediately update cached PHC time\n",
1077b1a582e6SJacob Keller __func__);
1078b1a582e6SJacob Keller
1079b1a582e6SJacob Keller /* Queue the work item to update the Rx rings when possible */
1080b1a582e6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work,
1081b1a582e6SJacob Keller msecs_to_jiffies(10));
1082b1a582e6SJacob Keller }
1083b1a582e6SJacob Keller
1084d40fd600SJacob Keller /* Mark any outstanding timestamps as stale, since they might have
1085d40fd600SJacob Keller * been captured in hardware before the time update. This could lead
1086d40fd600SJacob Keller * to us extending them with the wrong cached value resulting in
1087d40fd600SJacob Keller * incorrect timestamp values.
1088d40fd600SJacob Keller */
1089d40fd600SJacob Keller ice_ptp_mark_tx_tracker_stale(&pf->ptp.port.tx);
1090b1a582e6SJacob Keller }
1091b1a582e6SJacob Keller
1092b1a582e6SJacob Keller /**
109306c16d89SJacob Keller * ice_ptp_read_time - Read the time from the device
109406c16d89SJacob Keller * @pf: Board private structure
109506c16d89SJacob Keller * @ts: timespec structure to hold the current time value
109606c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from
109706c16d89SJacob Keller * the system clock. Will be ignored if NULL is given.
109806c16d89SJacob Keller *
109906c16d89SJacob Keller * This function reads the source clock registers and stores them in a timespec.
110006c16d89SJacob Keller * However, since the registers are 64 bits of nanoseconds, we must convert the
110106c16d89SJacob Keller * result to a timespec before we can return.
110206c16d89SJacob Keller */
110306c16d89SJacob Keller static void
ice_ptp_read_time(struct ice_pf * pf,struct timespec64 * ts,struct ptp_system_timestamp * sts)110406c16d89SJacob Keller ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts,
110506c16d89SJacob Keller struct ptp_system_timestamp *sts)
110606c16d89SJacob Keller {
110706c16d89SJacob Keller u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts);
110806c16d89SJacob Keller
110906c16d89SJacob Keller *ts = ns_to_timespec64(time_ns);
111006c16d89SJacob Keller }
111106c16d89SJacob Keller
111206c16d89SJacob Keller /**
111306c16d89SJacob Keller * ice_ptp_write_init - Set PHC time to provided value
111406c16d89SJacob Keller * @pf: Board private structure
111506c16d89SJacob Keller * @ts: timespec structure that holds the new time value
111606c16d89SJacob Keller *
111706c16d89SJacob Keller * Set the PHC time to the specified time provided in the timespec.
111806c16d89SJacob Keller */
ice_ptp_write_init(struct ice_pf * pf,struct timespec64 * ts)111906c16d89SJacob Keller static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts)
112006c16d89SJacob Keller {
112106c16d89SJacob Keller u64 ns = timespec64_to_ns(ts);
112206c16d89SJacob Keller struct ice_hw *hw = &pf->hw;
112306c16d89SJacob Keller
112406c16d89SJacob Keller return ice_ptp_init_time(hw, ns);
112506c16d89SJacob Keller }
112606c16d89SJacob Keller
112706c16d89SJacob Keller /**
112806c16d89SJacob Keller * ice_ptp_write_adj - Adjust PHC clock time atomically
112906c16d89SJacob Keller * @pf: Board private structure
113006c16d89SJacob Keller * @adj: Adjustment in nanoseconds
113106c16d89SJacob Keller *
113206c16d89SJacob Keller * Perform an atomic adjustment of the PHC time by the specified number of
113306c16d89SJacob Keller * nanoseconds.
113406c16d89SJacob Keller */
ice_ptp_write_adj(struct ice_pf * pf,s32 adj)113506c16d89SJacob Keller static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj)
113606c16d89SJacob Keller {
113706c16d89SJacob Keller struct ice_hw *hw = &pf->hw;
113806c16d89SJacob Keller
113906c16d89SJacob Keller return ice_ptp_adj_clock(hw, adj);
114006c16d89SJacob Keller }
114106c16d89SJacob Keller
114206c16d89SJacob Keller /**
114378267d0cSJacob Keller * ice_base_incval - Get base timer increment value
114478267d0cSJacob Keller * @pf: Board private structure
114578267d0cSJacob Keller *
114678267d0cSJacob Keller * Look up the base timer increment value for this device. The base increment
114778267d0cSJacob Keller * value is used to define the nominal clock tick rate. This increment value
114878267d0cSJacob Keller * is programmed during device initialization. It is also used as the basis
114978267d0cSJacob Keller * for calculating adjustments using scaled_ppm.
115078267d0cSJacob Keller */
ice_base_incval(struct ice_pf * pf)115178267d0cSJacob Keller static u64 ice_base_incval(struct ice_pf *pf)
115278267d0cSJacob Keller {
11533a749623SJacob Keller struct ice_hw *hw = &pf->hw;
11543a749623SJacob Keller u64 incval;
11553a749623SJacob Keller
11563a749623SJacob Keller if (ice_is_e810(hw))
11573a749623SJacob Keller incval = ICE_PTP_NOMINAL_INCVAL_E810;
11583a749623SJacob Keller else if (ice_e822_time_ref(hw) < NUM_ICE_TIME_REF_FREQ)
11593a749623SJacob Keller incval = ice_e822_nominal_incval(ice_e822_time_ref(hw));
11603a749623SJacob Keller else
11613a749623SJacob Keller incval = UNKNOWN_INCVAL_E822;
11623a749623SJacob Keller
11633a749623SJacob Keller dev_dbg(ice_pf_to_dev(pf), "PTP: using base increment value of 0x%016llx\n",
11643a749623SJacob Keller incval);
11653a749623SJacob Keller
11663a749623SJacob Keller return incval;
11673a749623SJacob Keller }
11683a749623SJacob Keller
11693a749623SJacob Keller /**
1170a69f1cb6SJacob Keller * ice_ptp_check_tx_fifo - Check whether Tx FIFO is in an OK state
1171a69f1cb6SJacob Keller * @port: PTP port for which Tx FIFO is checked
1172a69f1cb6SJacob Keller */
ice_ptp_check_tx_fifo(struct ice_ptp_port * port)1173a69f1cb6SJacob Keller static int ice_ptp_check_tx_fifo(struct ice_ptp_port *port)
1174a69f1cb6SJacob Keller {
1175a69f1cb6SJacob Keller int quad = port->port_num / ICE_PORTS_PER_QUAD;
1176a69f1cb6SJacob Keller int offs = port->port_num % ICE_PORTS_PER_QUAD;
1177a69f1cb6SJacob Keller struct ice_pf *pf;
1178a69f1cb6SJacob Keller struct ice_hw *hw;
1179a69f1cb6SJacob Keller u32 val, phy_sts;
1180a69f1cb6SJacob Keller int err;
1181a69f1cb6SJacob Keller
1182a69f1cb6SJacob Keller pf = ptp_port_to_pf(port);
1183a69f1cb6SJacob Keller hw = &pf->hw;
1184a69f1cb6SJacob Keller
1185a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == FIFO_OK)
1186a69f1cb6SJacob Keller return 0;
1187a69f1cb6SJacob Keller
1188a69f1cb6SJacob Keller /* need to read FIFO state */
1189a69f1cb6SJacob Keller if (offs == 0 || offs == 1)
1190a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO01_STATUS,
1191a69f1cb6SJacob Keller &val);
1192a69f1cb6SJacob Keller else
1193a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO23_STATUS,
1194a69f1cb6SJacob Keller &val);
1195a69f1cb6SJacob Keller
1196a69f1cb6SJacob Keller if (err) {
1197a69f1cb6SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to check port %d Tx FIFO, err %d\n",
1198a69f1cb6SJacob Keller port->port_num, err);
1199a69f1cb6SJacob Keller return err;
1200a69f1cb6SJacob Keller }
1201a69f1cb6SJacob Keller
1202a69f1cb6SJacob Keller if (offs & 0x1)
1203a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO13_M) >> Q_REG_FIFO13_S;
1204a69f1cb6SJacob Keller else
1205a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO02_M) >> Q_REG_FIFO02_S;
1206a69f1cb6SJacob Keller
1207a69f1cb6SJacob Keller if (phy_sts & FIFO_EMPTY) {
1208a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK;
1209a69f1cb6SJacob Keller return 0;
1210a69f1cb6SJacob Keller }
1211a69f1cb6SJacob Keller
1212a69f1cb6SJacob Keller port->tx_fifo_busy_cnt++;
1213a69f1cb6SJacob Keller
1214a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf), "Try %d, port %d FIFO not empty\n",
1215a69f1cb6SJacob Keller port->tx_fifo_busy_cnt, port->port_num);
1216a69f1cb6SJacob Keller
1217a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == ICE_PTP_FIFO_NUM_CHECKS) {
1218a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf),
1219a69f1cb6SJacob Keller "Port %d Tx FIFO still not empty; resetting quad %d\n",
1220a69f1cb6SJacob Keller port->port_num, quad);
1221407b66c0SKarol Kolacinski ice_ptp_reset_ts_memory_quad_e822(hw, quad);
1222a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK;
1223a69f1cb6SJacob Keller return 0;
1224a69f1cb6SJacob Keller }
1225a69f1cb6SJacob Keller
1226a69f1cb6SJacob Keller return -EAGAIN;
1227a69f1cb6SJacob Keller }
1228a69f1cb6SJacob Keller
1229a69f1cb6SJacob Keller /**
1230f029a343SSiddaraju DH * ice_ptp_wait_for_offsets - Check for valid Tx and Rx offsets
1231a69f1cb6SJacob Keller * @work: Pointer to the kthread_work structure for this task
1232a69f1cb6SJacob Keller *
1233f029a343SSiddaraju DH * Check whether hardware has completed measuring the Tx and Rx offset values
1234f029a343SSiddaraju DH * used to configure and enable vernier timestamp calibration.
1235a69f1cb6SJacob Keller *
1236f029a343SSiddaraju DH * Once the offset in either direction is measured, configure the associated
1237f029a343SSiddaraju DH * registers with the calibrated offset values and enable timestamping. The Tx
1238f029a343SSiddaraju DH * and Rx directions are configured independently as soon as their associated
1239f029a343SSiddaraju DH * offsets are known.
1240f029a343SSiddaraju DH *
1241f029a343SSiddaraju DH * This function reschedules itself until both Tx and Rx calibration have
1242f029a343SSiddaraju DH * completed.
1243a69f1cb6SJacob Keller */
ice_ptp_wait_for_offsets(struct kthread_work * work)1244f029a343SSiddaraju DH static void ice_ptp_wait_for_offsets(struct kthread_work *work)
1245a69f1cb6SJacob Keller {
1246a69f1cb6SJacob Keller struct ice_ptp_port *port;
1247a69f1cb6SJacob Keller struct ice_pf *pf;
1248a69f1cb6SJacob Keller struct ice_hw *hw;
1249f029a343SSiddaraju DH int tx_err;
1250f029a343SSiddaraju DH int rx_err;
1251a69f1cb6SJacob Keller
1252a69f1cb6SJacob Keller port = container_of(work, struct ice_ptp_port, ov_work.work);
1253a69f1cb6SJacob Keller pf = ptp_port_to_pf(port);
1254a69f1cb6SJacob Keller hw = &pf->hw;
1255a69f1cb6SJacob Keller
125695af1f1cSJacob Keller if (ice_is_reset_in_progress(pf->state)) {
125795af1f1cSJacob Keller /* wait for device driver to complete reset */
125895af1f1cSJacob Keller kthread_queue_delayed_work(pf->ptp.kworker,
125995af1f1cSJacob Keller &port->ov_work,
126095af1f1cSJacob Keller msecs_to_jiffies(100));
12610b57e0d4SMichal Michalik return;
126295af1f1cSJacob Keller }
12630b57e0d4SMichal Michalik
1264f029a343SSiddaraju DH tx_err = ice_ptp_check_tx_fifo(port);
1265f029a343SSiddaraju DH if (!tx_err)
1266f029a343SSiddaraju DH tx_err = ice_phy_cfg_tx_offset_e822(hw, port->port_num);
1267f029a343SSiddaraju DH rx_err = ice_phy_cfg_rx_offset_e822(hw, port->port_num);
1268f029a343SSiddaraju DH if (tx_err || rx_err) {
1269f029a343SSiddaraju DH /* Tx and/or Rx offset not yet configured, try again later */
1270a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker,
1271a69f1cb6SJacob Keller &port->ov_work,
1272a69f1cb6SJacob Keller msecs_to_jiffies(100));
1273a69f1cb6SJacob Keller return;
1274a69f1cb6SJacob Keller }
1275a69f1cb6SJacob Keller }
1276a69f1cb6SJacob Keller
1277a69f1cb6SJacob Keller /**
12783a749623SJacob Keller * ice_ptp_port_phy_stop - Stop timestamping for a PHY port
12793a749623SJacob Keller * @ptp_port: PTP port to stop
12803a749623SJacob Keller */
12813a749623SJacob Keller static int
ice_ptp_port_phy_stop(struct ice_ptp_port * ptp_port)12823a749623SJacob Keller ice_ptp_port_phy_stop(struct ice_ptp_port *ptp_port)
12833a749623SJacob Keller {
12843a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port);
12853a749623SJacob Keller u8 port = ptp_port->port_num;
12863a749623SJacob Keller struct ice_hw *hw = &pf->hw;
12873a749623SJacob Keller int err;
12883a749623SJacob Keller
12893a749623SJacob Keller if (ice_is_e810(hw))
12903a749623SJacob Keller return 0;
12913a749623SJacob Keller
12923a749623SJacob Keller mutex_lock(&ptp_port->ps_lock);
12933a749623SJacob Keller
1294a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work);
1295a69f1cb6SJacob Keller
12963a749623SJacob Keller err = ice_stop_phy_timer_e822(hw, port, true);
12973a749623SJacob Keller if (err)
12983a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d down, err %d\n",
12993a749623SJacob Keller port, err);
13003a749623SJacob Keller
13013a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock);
13023a749623SJacob Keller
13033a749623SJacob Keller return err;
13043a749623SJacob Keller }
13053a749623SJacob Keller
13063a749623SJacob Keller /**
13073a749623SJacob Keller * ice_ptp_port_phy_restart - (Re)start and calibrate PHY timestamping
13083a749623SJacob Keller * @ptp_port: PTP port for which the PHY start is set
13093a749623SJacob Keller *
13103a749623SJacob Keller * Start the PHY timestamping block, and initiate Vernier timestamping
13113a749623SJacob Keller * calibration. If timestamping cannot be calibrated (such as if link is down)
13123a749623SJacob Keller * then disable the timestamping block instead.
13133a749623SJacob Keller */
13143a749623SJacob Keller static int
ice_ptp_port_phy_restart(struct ice_ptp_port * ptp_port)13153a749623SJacob Keller ice_ptp_port_phy_restart(struct ice_ptp_port *ptp_port)
13163a749623SJacob Keller {
13173a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port);
13183a749623SJacob Keller u8 port = ptp_port->port_num;
13193a749623SJacob Keller struct ice_hw *hw = &pf->hw;
13203a749623SJacob Keller int err;
13213a749623SJacob Keller
13223a749623SJacob Keller if (ice_is_e810(hw))
13233a749623SJacob Keller return 0;
13243a749623SJacob Keller
13253a749623SJacob Keller if (!ptp_port->link_up)
13263a749623SJacob Keller return ice_ptp_port_phy_stop(ptp_port);
13273a749623SJacob Keller
13283a749623SJacob Keller mutex_lock(&ptp_port->ps_lock);
13293a749623SJacob Keller
1330a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work);
1331a69f1cb6SJacob Keller
13323a749623SJacob Keller /* temporarily disable Tx timestamps while calibrating PHY offset */
13333ad5c10bSJacob Keller spin_lock(&ptp_port->tx.lock);
13343a749623SJacob Keller ptp_port->tx.calibrating = true;
13353ad5c10bSJacob Keller spin_unlock(&ptp_port->tx.lock);
1336a69f1cb6SJacob Keller ptp_port->tx_fifo_busy_cnt = 0;
13373a749623SJacob Keller
13380357d5caSMilena Olech /* Start the PHY timer in Vernier mode */
13390357d5caSMilena Olech err = ice_start_phy_timer_e822(hw, port);
13403a749623SJacob Keller if (err)
13413a749623SJacob Keller goto out_unlock;
13423a749623SJacob Keller
13433a749623SJacob Keller /* Enable Tx timestamps right away */
13443ad5c10bSJacob Keller spin_lock(&ptp_port->tx.lock);
13453a749623SJacob Keller ptp_port->tx.calibrating = false;
13463ad5c10bSJacob Keller spin_unlock(&ptp_port->tx.lock);
13473a749623SJacob Keller
1348a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &ptp_port->ov_work, 0);
1349a69f1cb6SJacob Keller
13503a749623SJacob Keller out_unlock:
13513a749623SJacob Keller if (err)
13523a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d up, err %d\n",
13533a749623SJacob Keller port, err);
13543a749623SJacob Keller
13553a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock);
13563a749623SJacob Keller
13573a749623SJacob Keller return err;
13583a749623SJacob Keller }
13593a749623SJacob Keller
13603a749623SJacob Keller /**
13616b1ff5d3SJacob Keller * ice_ptp_link_change - Reconfigure PTP after link status change
13623a749623SJacob Keller * @pf: Board private structure
13633a749623SJacob Keller * @port: Port for which the PHY start is set
13643a749623SJacob Keller * @linkup: Link is up or down
13653a749623SJacob Keller */
ice_ptp_link_change(struct ice_pf * pf,u8 port,bool linkup)13666b1ff5d3SJacob Keller void ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup)
13673a749623SJacob Keller {
13683a749623SJacob Keller struct ice_ptp_port *ptp_port;
13693a749623SJacob Keller
13706b1ff5d3SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags))
13716b1ff5d3SJacob Keller return;
13723a749623SJacob Keller
13736b1ff5d3SJacob Keller if (WARN_ON_ONCE(port >= ICE_NUM_EXTERNAL_PORTS))
13746b1ff5d3SJacob Keller return;
13753a749623SJacob Keller
13763a749623SJacob Keller ptp_port = &pf->ptp.port;
13776b1ff5d3SJacob Keller if (WARN_ON_ONCE(ptp_port->port_num != port))
13786b1ff5d3SJacob Keller return;
13793a749623SJacob Keller
138011722c39SJacob Keller /* Update cached link status for this port immediately */
13813a749623SJacob Keller ptp_port->link_up = linkup;
13823a749623SJacob Keller
13836b1ff5d3SJacob Keller /* E810 devices do not need to reconfigure the PHY */
13846b1ff5d3SJacob Keller if (ice_is_e810(&pf->hw))
13856b1ff5d3SJacob Keller return;
13863a749623SJacob Keller
13876b1ff5d3SJacob Keller ice_ptp_port_phy_restart(ptp_port);
13883a749623SJacob Keller }
13893a749623SJacob Keller
13903a749623SJacob Keller /**
13913a749623SJacob Keller * ice_ptp_tx_ena_intr - Enable or disable the Tx timestamp interrupt
13923a749623SJacob Keller * @pf: PF private structure
13933a749623SJacob Keller * @ena: bool value to enable or disable interrupt
13943a749623SJacob Keller * @threshold: Minimum number of packets at which intr is triggered
13953a749623SJacob Keller *
13963a749623SJacob Keller * Utility function to enable or disable Tx timestamp interrupt and threshold
13973a749623SJacob Keller */
ice_ptp_tx_ena_intr(struct ice_pf * pf,bool ena,u32 threshold)13983a749623SJacob Keller static int ice_ptp_tx_ena_intr(struct ice_pf *pf, bool ena, u32 threshold)
13993a749623SJacob Keller {
14003a749623SJacob Keller struct ice_hw *hw = &pf->hw;
14013a749623SJacob Keller int err = 0;
14023a749623SJacob Keller int quad;
14033a749623SJacob Keller u32 val;
14043a749623SJacob Keller
1405407b66c0SKarol Kolacinski ice_ptp_reset_ts_memory(hw);
14063a749623SJacob Keller
14073a749623SJacob Keller for (quad = 0; quad < ICE_MAX_QUAD; quad++) {
14083a749623SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG,
14093a749623SJacob Keller &val);
14103a749623SJacob Keller if (err)
14113a749623SJacob Keller break;
14123a749623SJacob Keller
14133a749623SJacob Keller if (ena) {
14143a749623SJacob Keller val |= Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M;
14153a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_THR_M;
14163a749623SJacob Keller val |= ((threshold << Q_REG_TX_MEM_GBL_CFG_INTR_THR_S) &
14173a749623SJacob Keller Q_REG_TX_MEM_GBL_CFG_INTR_THR_M);
14183a749623SJacob Keller } else {
14193a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M;
14203a749623SJacob Keller }
14213a749623SJacob Keller
14223a749623SJacob Keller err = ice_write_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG,
14233a749623SJacob Keller val);
14243a749623SJacob Keller if (err)
14253a749623SJacob Keller break;
14263a749623SJacob Keller }
14273a749623SJacob Keller
14283a749623SJacob Keller if (err)
14293a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed in intr ena, err %d\n",
14303a749623SJacob Keller err);
14313a749623SJacob Keller return err;
14323a749623SJacob Keller }
14333a749623SJacob Keller
14343a749623SJacob Keller /**
14353a749623SJacob Keller * ice_ptp_reset_phy_timestamping - Reset PHY timestamping block
14363a749623SJacob Keller * @pf: Board private structure
14373a749623SJacob Keller */
ice_ptp_reset_phy_timestamping(struct ice_pf * pf)14383a749623SJacob Keller static void ice_ptp_reset_phy_timestamping(struct ice_pf *pf)
14393a749623SJacob Keller {
14403a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port);
144178267d0cSJacob Keller }
144278267d0cSJacob Keller
144378267d0cSJacob Keller /**
144406c16d89SJacob Keller * ice_ptp_adjfine - Adjust clock increment rate
144506c16d89SJacob Keller * @info: the driver's PTP info structure
144606c16d89SJacob Keller * @scaled_ppm: Parts per million with 16-bit fractional field
144706c16d89SJacob Keller *
144806c16d89SJacob Keller * Adjust the frequency of the clock by the indicated scaled ppm from the
144906c16d89SJacob Keller * base frequency.
145006c16d89SJacob Keller */
ice_ptp_adjfine(struct ptp_clock_info * info,long scaled_ppm)145106c16d89SJacob Keller static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
145206c16d89SJacob Keller {
145306c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info);
145406c16d89SJacob Keller struct ice_hw *hw = &pf->hw;
14551060707eSJacob Keller u64 incval;
145606c16d89SJacob Keller int err;
145706c16d89SJacob Keller
14581060707eSJacob Keller incval = adjust_by_scaled_ppm(ice_base_incval(pf), scaled_ppm);
145906c16d89SJacob Keller err = ice_ptp_write_incval_locked(hw, incval);
146006c16d89SJacob Keller if (err) {
146106c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n",
146206c16d89SJacob Keller err);
146306c16d89SJacob Keller return -EIO;
146406c16d89SJacob Keller }
146506c16d89SJacob Keller
146606c16d89SJacob Keller return 0;
146706c16d89SJacob Keller }
146806c16d89SJacob Keller
146906c16d89SJacob Keller /**
14706e8b2c88SKarol Kolacinski * ice_ptp_extts_event - Process PTP external clock event
14716e8b2c88SKarol Kolacinski * @pf: Board private structure
1472172db5f9SMaciej Machnikowski */
ice_ptp_extts_event(struct ice_pf * pf)14736e8b2c88SKarol Kolacinski void ice_ptp_extts_event(struct ice_pf *pf)
1474172db5f9SMaciej Machnikowski {
1475172db5f9SMaciej Machnikowski struct ptp_clock_event event;
1476172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw;
1477172db5f9SMaciej Machnikowski u8 chan, tmr_idx;
1478172db5f9SMaciej Machnikowski u32 hi, lo;
1479172db5f9SMaciej Machnikowski
1480172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1481172db5f9SMaciej Machnikowski /* Event time is captured by one of the two matched registers
1482172db5f9SMaciej Machnikowski * GLTSYN_EVNT_L: 32 LSB of sampled time event
1483172db5f9SMaciej Machnikowski * GLTSYN_EVNT_H: 32 MSB of sampled time event
1484172db5f9SMaciej Machnikowski * Event is defined in GLTSYN_EVNT_0 register
1485172db5f9SMaciej Machnikowski */
1486172db5f9SMaciej Machnikowski for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) {
1487172db5f9SMaciej Machnikowski /* Check if channel is enabled */
1488172db5f9SMaciej Machnikowski if (pf->ptp.ext_ts_irq & (1 << chan)) {
1489172db5f9SMaciej Machnikowski lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx));
1490172db5f9SMaciej Machnikowski hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx));
1491172db5f9SMaciej Machnikowski event.timestamp = (((u64)hi) << 32) | lo;
1492172db5f9SMaciej Machnikowski event.type = PTP_CLOCK_EXTTS;
1493172db5f9SMaciej Machnikowski event.index = chan;
1494172db5f9SMaciej Machnikowski
1495172db5f9SMaciej Machnikowski /* Fire event */
1496172db5f9SMaciej Machnikowski ptp_clock_event(pf->ptp.clock, &event);
1497172db5f9SMaciej Machnikowski pf->ptp.ext_ts_irq &= ~(1 << chan);
1498172db5f9SMaciej Machnikowski }
1499172db5f9SMaciej Machnikowski }
1500172db5f9SMaciej Machnikowski }
1501172db5f9SMaciej Machnikowski
1502172db5f9SMaciej Machnikowski /**
1503172db5f9SMaciej Machnikowski * ice_ptp_cfg_extts - Configure EXTTS pin and channel
1504172db5f9SMaciej Machnikowski * @pf: Board private structure
1505172db5f9SMaciej Machnikowski * @ena: true to enable; false to disable
1506172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3)
1507172db5f9SMaciej Machnikowski * @gpio_pin: GPIO pin
1508172db5f9SMaciej Machnikowski * @extts_flags: request flags from the ptp_extts_request.flags
1509172db5f9SMaciej Machnikowski */
1510172db5f9SMaciej Machnikowski static int
ice_ptp_cfg_extts(struct ice_pf * pf,bool ena,unsigned int chan,u32 gpio_pin,unsigned int extts_flags)1511172db5f9SMaciej Machnikowski ice_ptp_cfg_extts(struct ice_pf *pf, bool ena, unsigned int chan, u32 gpio_pin,
1512172db5f9SMaciej Machnikowski unsigned int extts_flags)
1513172db5f9SMaciej Machnikowski {
1514172db5f9SMaciej Machnikowski u32 func, aux_reg, gpio_reg, irq_reg;
1515172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw;
1516172db5f9SMaciej Machnikowski u8 tmr_idx;
1517172db5f9SMaciej Machnikowski
1518172db5f9SMaciej Machnikowski if (chan > (unsigned int)pf->ptp.info.n_ext_ts)
1519172db5f9SMaciej Machnikowski return -EINVAL;
1520172db5f9SMaciej Machnikowski
1521172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1522172db5f9SMaciej Machnikowski
1523172db5f9SMaciej Machnikowski irq_reg = rd32(hw, PFINT_OICR_ENA);
1524172db5f9SMaciej Machnikowski
1525172db5f9SMaciej Machnikowski if (ena) {
1526172db5f9SMaciej Machnikowski /* Enable the interrupt */
1527172db5f9SMaciej Machnikowski irq_reg |= PFINT_OICR_TSYN_EVNT_M;
1528172db5f9SMaciej Machnikowski aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M;
1529172db5f9SMaciej Machnikowski
1530172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE BIT(0)
1531172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE BIT(1)
1532172db5f9SMaciej Machnikowski
1533172db5f9SMaciej Machnikowski /* set event level to requested edge */
1534172db5f9SMaciej Machnikowski if (extts_flags & PTP_FALLING_EDGE)
1535172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE;
1536172db5f9SMaciej Machnikowski if (extts_flags & PTP_RISING_EDGE)
1537172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE;
1538172db5f9SMaciej Machnikowski
1539172db5f9SMaciej Machnikowski /* Write GPIO CTL reg.
1540172db5f9SMaciej Machnikowski * 0x1 is input sampled by EVENT register(channel)
1541172db5f9SMaciej Machnikowski * + num_in_channels * tmr_idx
1542172db5f9SMaciej Machnikowski */
1543172db5f9SMaciej Machnikowski func = 1 + chan + (tmr_idx * 3);
1544172db5f9SMaciej Machnikowski gpio_reg = ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) &
1545172db5f9SMaciej Machnikowski GLGEN_GPIO_CTL_PIN_FUNC_M);
1546172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan |= (1 << chan);
1547172db5f9SMaciej Machnikowski } else {
1548172db5f9SMaciej Machnikowski /* clear the values we set to reset defaults */
1549172db5f9SMaciej Machnikowski aux_reg = 0;
1550172db5f9SMaciej Machnikowski gpio_reg = 0;
1551172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan &= ~(1 << chan);
1552172db5f9SMaciej Machnikowski if (!pf->ptp.ext_ts_chan)
1553172db5f9SMaciej Machnikowski irq_reg &= ~PFINT_OICR_TSYN_EVNT_M;
1554172db5f9SMaciej Machnikowski }
1555172db5f9SMaciej Machnikowski
1556172db5f9SMaciej Machnikowski wr32(hw, PFINT_OICR_ENA, irq_reg);
1557172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg);
1558172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg);
1559172db5f9SMaciej Machnikowski
1560172db5f9SMaciej Machnikowski return 0;
1561172db5f9SMaciej Machnikowski }
1562172db5f9SMaciej Machnikowski
1563172db5f9SMaciej Machnikowski /**
1564172db5f9SMaciej Machnikowski * ice_ptp_cfg_clkout - Configure clock to generate periodic wave
1565172db5f9SMaciej Machnikowski * @pf: Board private structure
1566172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3)
1567172db5f9SMaciej Machnikowski * @config: desired periodic clk configuration. NULL will disable channel
1568172db5f9SMaciej Machnikowski * @store: If set to true the values will be stored
1569172db5f9SMaciej Machnikowski *
1570172db5f9SMaciej Machnikowski * Configure the internal clock generator modules to generate the clock wave of
1571172db5f9SMaciej Machnikowski * specified period.
1572172db5f9SMaciej Machnikowski */
ice_ptp_cfg_clkout(struct ice_pf * pf,unsigned int chan,struct ice_perout_channel * config,bool store)1573172db5f9SMaciej Machnikowski static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan,
1574172db5f9SMaciej Machnikowski struct ice_perout_channel *config, bool store)
1575172db5f9SMaciej Machnikowski {
1576172db5f9SMaciej Machnikowski u64 current_time, period, start_time, phase;
1577172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw;
1578172db5f9SMaciej Machnikowski u32 func, val, gpio_pin;
1579172db5f9SMaciej Machnikowski u8 tmr_idx;
1580172db5f9SMaciej Machnikowski
1581172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1582172db5f9SMaciej Machnikowski
1583172db5f9SMaciej Machnikowski /* 0. Reset mode & out_en in AUX_OUT */
1584172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0);
1585172db5f9SMaciej Machnikowski
1586172db5f9SMaciej Machnikowski /* If we're disabling the output, clear out CLKO and TGT and keep
1587172db5f9SMaciej Machnikowski * output level low
1588172db5f9SMaciej Machnikowski */
1589172db5f9SMaciej Machnikowski if (!config || !config->ena) {
1590172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), 0);
1591172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), 0);
1592172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), 0);
1593172db5f9SMaciej Machnikowski
1594172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M;
1595172db5f9SMaciej Machnikowski gpio_pin = pf->ptp.perout_channels[chan].gpio_pin;
1596172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
1597172db5f9SMaciej Machnikowski
1598172db5f9SMaciej Machnikowski /* Store the value if requested */
1599172db5f9SMaciej Machnikowski if (store)
1600172db5f9SMaciej Machnikowski memset(&pf->ptp.perout_channels[chan], 0,
1601172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel));
1602172db5f9SMaciej Machnikowski
1603172db5f9SMaciej Machnikowski return 0;
1604172db5f9SMaciej Machnikowski }
1605172db5f9SMaciej Machnikowski period = config->period;
1606172db5f9SMaciej Machnikowski start_time = config->start_time;
1607172db5f9SMaciej Machnikowski div64_u64_rem(start_time, period, &phase);
1608172db5f9SMaciej Machnikowski gpio_pin = config->gpio_pin;
1609172db5f9SMaciej Machnikowski
1610172db5f9SMaciej Machnikowski /* 1. Write clkout with half of required period value */
1611172db5f9SMaciej Machnikowski if (period & 0x1) {
1612172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n");
1613172db5f9SMaciej Machnikowski goto err;
1614172db5f9SMaciej Machnikowski }
1615172db5f9SMaciej Machnikowski
1616172db5f9SMaciej Machnikowski period >>= 1;
1617172db5f9SMaciej Machnikowski
1618172db5f9SMaciej Machnikowski /* For proper operation, the GLTSYN_CLKO must be larger than clock tick
1619172db5f9SMaciej Machnikowski */
1620172db5f9SMaciej Machnikowski #define MIN_PULSE 3
1621172db5f9SMaciej Machnikowski if (period <= MIN_PULSE || period > U32_MAX) {
1622172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be > %d && < 2^33",
1623172db5f9SMaciej Machnikowski MIN_PULSE * 2);
1624172db5f9SMaciej Machnikowski goto err;
1625172db5f9SMaciej Machnikowski }
1626172db5f9SMaciej Machnikowski
1627172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period));
1628172db5f9SMaciej Machnikowski
1629172db5f9SMaciej Machnikowski /* Allow time for programming before start_time is hit */
1630172db5f9SMaciej Machnikowski current_time = ice_ptp_read_src_clk_reg(pf, NULL);
1631172db5f9SMaciej Machnikowski
1632172db5f9SMaciej Machnikowski /* if start time is in the past start the timer at the nearest second
1633172db5f9SMaciej Machnikowski * maintaining phase
1634172db5f9SMaciej Machnikowski */
1635172db5f9SMaciej Machnikowski if (start_time < current_time)
16365f773519SMaciej Machnikowski start_time = div64_u64(current_time + NSEC_PER_SEC - 1,
1637172db5f9SMaciej Machnikowski NSEC_PER_SEC) * NSEC_PER_SEC + phase;
1638172db5f9SMaciej Machnikowski
16393a749623SJacob Keller if (ice_is_e810(hw))
1640172db5f9SMaciej Machnikowski start_time -= E810_OUT_PROP_DELAY_NS;
16413a749623SJacob Keller else
16423a749623SJacob Keller start_time -= ice_e822_pps_delay(ice_e822_time_ref(hw));
1643172db5f9SMaciej Machnikowski
1644172db5f9SMaciej Machnikowski /* 2. Write TARGET time */
1645172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start_time));
1646172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start_time));
1647172db5f9SMaciej Machnikowski
1648172db5f9SMaciej Machnikowski /* 3. Write AUX_OUT register */
1649172db5f9SMaciej Machnikowski val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M;
1650172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val);
1651172db5f9SMaciej Machnikowski
1652172db5f9SMaciej Machnikowski /* 4. write GPIO CTL reg */
1653172db5f9SMaciej Machnikowski func = 8 + chan + (tmr_idx * 4);
1654172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M |
1655172db5f9SMaciej Machnikowski ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & GLGEN_GPIO_CTL_PIN_FUNC_M);
1656172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
1657172db5f9SMaciej Machnikowski
1658172db5f9SMaciej Machnikowski /* Store the value if requested */
1659172db5f9SMaciej Machnikowski if (store) {
1660172db5f9SMaciej Machnikowski memcpy(&pf->ptp.perout_channels[chan], config,
1661172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel));
1662172db5f9SMaciej Machnikowski pf->ptp.perout_channels[chan].start_time = phase;
1663172db5f9SMaciej Machnikowski }
1664172db5f9SMaciej Machnikowski
1665172db5f9SMaciej Machnikowski return 0;
1666172db5f9SMaciej Machnikowski err:
1667172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "PTP failed to cfg per_clk\n");
1668172db5f9SMaciej Machnikowski return -EFAULT;
1669172db5f9SMaciej Machnikowski }
1670172db5f9SMaciej Machnikowski
1671172db5f9SMaciej Machnikowski /**
16729ee31343SJacob Keller * ice_ptp_disable_all_clkout - Disable all currently configured outputs
16739ee31343SJacob Keller * @pf: pointer to the PF structure
16749ee31343SJacob Keller *
16759ee31343SJacob Keller * Disable all currently configured clock outputs. This is necessary before
16769ee31343SJacob Keller * certain changes to the PTP hardware clock. Use ice_ptp_enable_all_clkout to
16779ee31343SJacob Keller * re-enable the clocks again.
16789ee31343SJacob Keller */
ice_ptp_disable_all_clkout(struct ice_pf * pf)16799ee31343SJacob Keller static void ice_ptp_disable_all_clkout(struct ice_pf *pf)
16809ee31343SJacob Keller {
16819ee31343SJacob Keller uint i;
16829ee31343SJacob Keller
16839ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++)
16849ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena)
16859ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, NULL, false);
16869ee31343SJacob Keller }
16879ee31343SJacob Keller
16889ee31343SJacob Keller /**
16899ee31343SJacob Keller * ice_ptp_enable_all_clkout - Enable all configured periodic clock outputs
16909ee31343SJacob Keller * @pf: pointer to the PF structure
16919ee31343SJacob Keller *
16929ee31343SJacob Keller * Enable all currently configured clock outputs. Use this after
16939ee31343SJacob Keller * ice_ptp_disable_all_clkout to reconfigure the output signals according to
16949ee31343SJacob Keller * their configuration.
16959ee31343SJacob Keller */
ice_ptp_enable_all_clkout(struct ice_pf * pf)16969ee31343SJacob Keller static void ice_ptp_enable_all_clkout(struct ice_pf *pf)
16979ee31343SJacob Keller {
16989ee31343SJacob Keller uint i;
16999ee31343SJacob Keller
17009ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++)
17019ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena)
17029ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, &pf->ptp.perout_channels[i],
17039ee31343SJacob Keller false);
17049ee31343SJacob Keller }
17059ee31343SJacob Keller
17069ee31343SJacob Keller /**
1707172db5f9SMaciej Machnikowski * ice_ptp_gpio_enable_e810 - Enable/disable ancillary features of PHC
1708172db5f9SMaciej Machnikowski * @info: the driver's PTP info structure
1709172db5f9SMaciej Machnikowski * @rq: The requested feature to change
1710172db5f9SMaciej Machnikowski * @on: Enable/disable flag
1711172db5f9SMaciej Machnikowski */
1712172db5f9SMaciej Machnikowski static int
ice_ptp_gpio_enable_e810(struct ptp_clock_info * info,struct ptp_clock_request * rq,int on)1713172db5f9SMaciej Machnikowski ice_ptp_gpio_enable_e810(struct ptp_clock_info *info,
1714172db5f9SMaciej Machnikowski struct ptp_clock_request *rq, int on)
1715172db5f9SMaciej Machnikowski {
1716172db5f9SMaciej Machnikowski struct ice_pf *pf = ptp_info_to_pf(info);
1717172db5f9SMaciej Machnikowski struct ice_perout_channel clk_cfg = {0};
1718325b2064SMaciej Machnikowski bool sma_pres = false;
1719172db5f9SMaciej Machnikowski unsigned int chan;
1720172db5f9SMaciej Machnikowski u32 gpio_pin;
1721172db5f9SMaciej Machnikowski int err;
1722172db5f9SMaciej Machnikowski
1723325b2064SMaciej Machnikowski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL))
1724325b2064SMaciej Machnikowski sma_pres = true;
1725325b2064SMaciej Machnikowski
1726172db5f9SMaciej Machnikowski switch (rq->type) {
1727172db5f9SMaciej Machnikowski case PTP_CLK_REQ_PEROUT:
1728172db5f9SMaciej Machnikowski chan = rq->perout.index;
1729325b2064SMaciej Machnikowski if (sma_pres) {
1730325b2064SMaciej Machnikowski if (chan == ice_pin_desc_e810t[SMA1].chan)
1731325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20;
1732325b2064SMaciej Machnikowski else if (chan == ice_pin_desc_e810t[SMA2].chan)
1733325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22;
1734172db5f9SMaciej Machnikowski else
1735325b2064SMaciej Machnikowski return -1;
1736325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) {
1737325b2064SMaciej Machnikowski if (chan == 0)
1738325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20;
1739325b2064SMaciej Machnikowski else
1740325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22;
1741325b2064SMaciej Machnikowski } else if (chan == PPS_CLK_GEN_CHAN) {
1742325b2064SMaciej Machnikowski clk_cfg.gpio_pin = PPS_PIN_INDEX;
1743325b2064SMaciej Machnikowski } else {
1744172db5f9SMaciej Machnikowski clk_cfg.gpio_pin = chan;
1745325b2064SMaciej Machnikowski }
1746172db5f9SMaciej Machnikowski
1747172db5f9SMaciej Machnikowski clk_cfg.period = ((rq->perout.period.sec * NSEC_PER_SEC) +
1748172db5f9SMaciej Machnikowski rq->perout.period.nsec);
1749172db5f9SMaciej Machnikowski clk_cfg.start_time = ((rq->perout.start.sec * NSEC_PER_SEC) +
1750172db5f9SMaciej Machnikowski rq->perout.start.nsec);
1751172db5f9SMaciej Machnikowski clk_cfg.ena = !!on;
1752172db5f9SMaciej Machnikowski
1753172db5f9SMaciej Machnikowski err = ice_ptp_cfg_clkout(pf, chan, &clk_cfg, true);
1754172db5f9SMaciej Machnikowski break;
1755172db5f9SMaciej Machnikowski case PTP_CLK_REQ_EXTTS:
1756172db5f9SMaciej Machnikowski chan = rq->extts.index;
1757325b2064SMaciej Machnikowski if (sma_pres) {
1758325b2064SMaciej Machnikowski if (chan < ice_pin_desc_e810t[SMA2].chan)
1759325b2064SMaciej Machnikowski gpio_pin = GPIO_21;
1760325b2064SMaciej Machnikowski else
1761325b2064SMaciej Machnikowski gpio_pin = GPIO_23;
1762325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) {
1763325b2064SMaciej Machnikowski if (chan == 0)
1764325b2064SMaciej Machnikowski gpio_pin = GPIO_21;
1765325b2064SMaciej Machnikowski else
1766325b2064SMaciej Machnikowski gpio_pin = GPIO_23;
1767325b2064SMaciej Machnikowski } else {
1768172db5f9SMaciej Machnikowski gpio_pin = chan;
1769325b2064SMaciej Machnikowski }
1770172db5f9SMaciej Machnikowski
1771172db5f9SMaciej Machnikowski err = ice_ptp_cfg_extts(pf, !!on, chan, gpio_pin,
1772172db5f9SMaciej Machnikowski rq->extts.flags);
1773172db5f9SMaciej Machnikowski break;
1774172db5f9SMaciej Machnikowski default:
1775172db5f9SMaciej Machnikowski return -EOPNOTSUPP;
1776172db5f9SMaciej Machnikowski }
1777172db5f9SMaciej Machnikowski
1778172db5f9SMaciej Machnikowski return err;
1779172db5f9SMaciej Machnikowski }
1780172db5f9SMaciej Machnikowski
1781172db5f9SMaciej Machnikowski /**
1782634d841dSKarol Kolacinski * ice_ptp_gpio_enable_e823 - Enable/disable ancillary features of PHC
1783634d841dSKarol Kolacinski * @info: the driver's PTP info structure
1784634d841dSKarol Kolacinski * @rq: The requested feature to change
1785634d841dSKarol Kolacinski * @on: Enable/disable flag
1786634d841dSKarol Kolacinski */
ice_ptp_gpio_enable_e823(struct ptp_clock_info * info,struct ptp_clock_request * rq,int on)1787634d841dSKarol Kolacinski static int ice_ptp_gpio_enable_e823(struct ptp_clock_info *info,
1788634d841dSKarol Kolacinski struct ptp_clock_request *rq, int on)
1789634d841dSKarol Kolacinski {
1790634d841dSKarol Kolacinski struct ice_pf *pf = ptp_info_to_pf(info);
1791634d841dSKarol Kolacinski struct ice_perout_channel clk_cfg = {0};
1792634d841dSKarol Kolacinski int err;
1793634d841dSKarol Kolacinski
1794634d841dSKarol Kolacinski switch (rq->type) {
1795634d841dSKarol Kolacinski case PTP_CLK_REQ_PPS:
1796634d841dSKarol Kolacinski clk_cfg.gpio_pin = PPS_PIN_INDEX;
1797634d841dSKarol Kolacinski clk_cfg.period = NSEC_PER_SEC;
1798634d841dSKarol Kolacinski clk_cfg.ena = !!on;
1799634d841dSKarol Kolacinski
1800634d841dSKarol Kolacinski err = ice_ptp_cfg_clkout(pf, PPS_CLK_GEN_CHAN, &clk_cfg, true);
1801634d841dSKarol Kolacinski break;
1802634d841dSKarol Kolacinski case PTP_CLK_REQ_EXTTS:
1803634d841dSKarol Kolacinski err = ice_ptp_cfg_extts(pf, !!on, rq->extts.index,
1804634d841dSKarol Kolacinski TIME_SYNC_PIN_INDEX, rq->extts.flags);
1805634d841dSKarol Kolacinski break;
1806634d841dSKarol Kolacinski default:
1807634d841dSKarol Kolacinski return -EOPNOTSUPP;
1808634d841dSKarol Kolacinski }
1809634d841dSKarol Kolacinski
1810634d841dSKarol Kolacinski return err;
1811634d841dSKarol Kolacinski }
1812634d841dSKarol Kolacinski
1813634d841dSKarol Kolacinski /**
181406c16d89SJacob Keller * ice_ptp_gettimex64 - Get the time of the clock
181506c16d89SJacob Keller * @info: the driver's PTP info structure
181606c16d89SJacob Keller * @ts: timespec64 structure to hold the current time value
181706c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from
181806c16d89SJacob Keller * the system clock. Will be ignored if NULL is given.
181906c16d89SJacob Keller *
182006c16d89SJacob Keller * Read the device clock and return the correct value on ns, after converting it
182106c16d89SJacob Keller * into a timespec struct.
182206c16d89SJacob Keller */
182306c16d89SJacob Keller static int
ice_ptp_gettimex64(struct ptp_clock_info * info,struct timespec64 * ts,struct ptp_system_timestamp * sts)182406c16d89SJacob Keller ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts,
182506c16d89SJacob Keller struct ptp_system_timestamp *sts)
182606c16d89SJacob Keller {
182706c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info);
182806c16d89SJacob Keller struct ice_hw *hw = &pf->hw;
182906c16d89SJacob Keller
183006c16d89SJacob Keller if (!ice_ptp_lock(hw)) {
183106c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n");
183206c16d89SJacob Keller return -EBUSY;
183306c16d89SJacob Keller }
183406c16d89SJacob Keller
183506c16d89SJacob Keller ice_ptp_read_time(pf, ts, sts);
183606c16d89SJacob Keller ice_ptp_unlock(hw);
183706c16d89SJacob Keller
183806c16d89SJacob Keller return 0;
183906c16d89SJacob Keller }
184006c16d89SJacob Keller
184106c16d89SJacob Keller /**
184206c16d89SJacob Keller * ice_ptp_settime64 - Set the time of the clock
184306c16d89SJacob Keller * @info: the driver's PTP info structure
184406c16d89SJacob Keller * @ts: timespec64 structure that holds the new time value
184506c16d89SJacob Keller *
184606c16d89SJacob Keller * Set the device clock to the user input value. The conversion from timespec
184706c16d89SJacob Keller * to ns happens in the write function.
184806c16d89SJacob Keller */
184906c16d89SJacob Keller static int
ice_ptp_settime64(struct ptp_clock_info * info,const struct timespec64 * ts)185006c16d89SJacob Keller ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts)
185106c16d89SJacob Keller {
185206c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info);
185306c16d89SJacob Keller struct timespec64 ts64 = *ts;
185406c16d89SJacob Keller struct ice_hw *hw = &pf->hw;
185506c16d89SJacob Keller int err;
185606c16d89SJacob Keller
18573a749623SJacob Keller /* For Vernier mode, we need to recalibrate after new settime
18583a749623SJacob Keller * Start with disabling timestamp block
18593a749623SJacob Keller */
18603a749623SJacob Keller if (pf->ptp.port.link_up)
18613a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port);
18623a749623SJacob Keller
186306c16d89SJacob Keller if (!ice_ptp_lock(hw)) {
186406c16d89SJacob Keller err = -EBUSY;
186506c16d89SJacob Keller goto exit;
186606c16d89SJacob Keller }
186706c16d89SJacob Keller
18689ee31343SJacob Keller /* Disable periodic outputs */
18699ee31343SJacob Keller ice_ptp_disable_all_clkout(pf);
18709ee31343SJacob Keller
187106c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts64);
187206c16d89SJacob Keller ice_ptp_unlock(hw);
187306c16d89SJacob Keller
187477a78115SJacob Keller if (!err)
1875b1a582e6SJacob Keller ice_ptp_reset_cached_phctime(pf);
187677a78115SJacob Keller
18779ee31343SJacob Keller /* Reenable periodic outputs */
18789ee31343SJacob Keller ice_ptp_enable_all_clkout(pf);
18793a749623SJacob Keller
18803a749623SJacob Keller /* Recalibrate and re-enable timestamp block */
18813a749623SJacob Keller if (pf->ptp.port.link_up)
18823a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port);
188306c16d89SJacob Keller exit:
188406c16d89SJacob Keller if (err) {
188506c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err);
188606c16d89SJacob Keller return err;
188706c16d89SJacob Keller }
188806c16d89SJacob Keller
188906c16d89SJacob Keller return 0;
189006c16d89SJacob Keller }
189106c16d89SJacob Keller
189206c16d89SJacob Keller /**
189306c16d89SJacob Keller * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment
189406c16d89SJacob Keller * @info: the driver's PTP info structure
189506c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by
189606c16d89SJacob Keller */
ice_ptp_adjtime_nonatomic(struct ptp_clock_info * info,s64 delta)189706c16d89SJacob Keller static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta)
189806c16d89SJacob Keller {
189906c16d89SJacob Keller struct timespec64 now, then;
1900ed22d9c8STom Rix int ret;
190106c16d89SJacob Keller
190206c16d89SJacob Keller then = ns_to_timespec64(delta);
1903ed22d9c8STom Rix ret = ice_ptp_gettimex64(info, &now, NULL);
1904ed22d9c8STom Rix if (ret)
1905ed22d9c8STom Rix return ret;
190606c16d89SJacob Keller now = timespec64_add(now, then);
190706c16d89SJacob Keller
190806c16d89SJacob Keller return ice_ptp_settime64(info, (const struct timespec64 *)&now);
190906c16d89SJacob Keller }
191006c16d89SJacob Keller
191106c16d89SJacob Keller /**
191206c16d89SJacob Keller * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta
191306c16d89SJacob Keller * @info: the driver's PTP info structure
191406c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by
191506c16d89SJacob Keller */
ice_ptp_adjtime(struct ptp_clock_info * info,s64 delta)191606c16d89SJacob Keller static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
191706c16d89SJacob Keller {
191806c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info);
191906c16d89SJacob Keller struct ice_hw *hw = &pf->hw;
192006c16d89SJacob Keller struct device *dev;
192106c16d89SJacob Keller int err;
192206c16d89SJacob Keller
192306c16d89SJacob Keller dev = ice_pf_to_dev(pf);
192406c16d89SJacob Keller
192506c16d89SJacob Keller /* Hardware only supports atomic adjustments using signed 32-bit
192606c16d89SJacob Keller * integers. For any adjustment outside this range, perform
192706c16d89SJacob Keller * a non-atomic get->adjust->set flow.
192806c16d89SJacob Keller */
192906c16d89SJacob Keller if (delta > S32_MAX || delta < S32_MIN) {
193006c16d89SJacob Keller dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta);
193106c16d89SJacob Keller return ice_ptp_adjtime_nonatomic(info, delta);
193206c16d89SJacob Keller }
193306c16d89SJacob Keller
193406c16d89SJacob Keller if (!ice_ptp_lock(hw)) {
193506c16d89SJacob Keller dev_err(dev, "PTP failed to acquire semaphore in adjtime\n");
193606c16d89SJacob Keller return -EBUSY;
193706c16d89SJacob Keller }
193806c16d89SJacob Keller
19399ee31343SJacob Keller /* Disable periodic outputs */
19409ee31343SJacob Keller ice_ptp_disable_all_clkout(pf);
19419ee31343SJacob Keller
194206c16d89SJacob Keller err = ice_ptp_write_adj(pf, delta);
194306c16d89SJacob Keller
19449ee31343SJacob Keller /* Reenable periodic outputs */
19459ee31343SJacob Keller ice_ptp_enable_all_clkout(pf);
19469ee31343SJacob Keller
194706c16d89SJacob Keller ice_ptp_unlock(hw);
194806c16d89SJacob Keller
194906c16d89SJacob Keller if (err) {
195006c16d89SJacob Keller dev_err(dev, "PTP failed to adjust time, err %d\n", err);
195106c16d89SJacob Keller return err;
195206c16d89SJacob Keller }
195306c16d89SJacob Keller
1954b1a582e6SJacob Keller ice_ptp_reset_cached_phctime(pf);
195577a78115SJacob Keller
195606c16d89SJacob Keller return 0;
195706c16d89SJacob Keller }
195806c16d89SJacob Keller
195913a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS
196013a64f0bSJacob Keller /**
196113a64f0bSJacob Keller * ice_ptp_get_syncdevicetime - Get the cross time stamp info
196213a64f0bSJacob Keller * @device: Current device time
196313a64f0bSJacob Keller * @system: System counter value read synchronously with device time
196413a64f0bSJacob Keller * @ctx: Context provided by timekeeping code
196513a64f0bSJacob Keller *
196613a64f0bSJacob Keller * Read device and system (ART) clock simultaneously and return the corrected
196713a64f0bSJacob Keller * clock values in ns.
196813a64f0bSJacob Keller */
196913a64f0bSJacob Keller static int
ice_ptp_get_syncdevicetime(ktime_t * device,struct system_counterval_t * system,void * ctx)197013a64f0bSJacob Keller ice_ptp_get_syncdevicetime(ktime_t *device,
197113a64f0bSJacob Keller struct system_counterval_t *system,
197213a64f0bSJacob Keller void *ctx)
197313a64f0bSJacob Keller {
197413a64f0bSJacob Keller struct ice_pf *pf = (struct ice_pf *)ctx;
197513a64f0bSJacob Keller struct ice_hw *hw = &pf->hw;
197613a64f0bSJacob Keller u32 hh_lock, hh_art_ctl;
197713a64f0bSJacob Keller int i;
197813a64f0bSJacob Keller
197913a64f0bSJacob Keller /* Get the HW lock */
198013a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
198113a64f0bSJacob Keller if (hh_lock & PFHH_SEM_BUSY_M) {
198213a64f0bSJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get hh lock\n");
198313a64f0bSJacob Keller return -EFAULT;
198413a64f0bSJacob Keller }
198513a64f0bSJacob Keller
198613a64f0bSJacob Keller /* Start the ART and device clock sync sequence */
198713a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL);
198813a64f0bSJacob Keller hh_art_ctl = hh_art_ctl | GLHH_ART_CTL_ACTIVE_M;
198913a64f0bSJacob Keller wr32(hw, GLHH_ART_CTL, hh_art_ctl);
199013a64f0bSJacob Keller
199113a64f0bSJacob Keller #define MAX_HH_LOCK_TRIES 100
199213a64f0bSJacob Keller
199313a64f0bSJacob Keller for (i = 0; i < MAX_HH_LOCK_TRIES; i++) {
199413a64f0bSJacob Keller /* Wait for sync to complete */
199513a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL);
199613a64f0bSJacob Keller if (hh_art_ctl & GLHH_ART_CTL_ACTIVE_M) {
199713a64f0bSJacob Keller udelay(1);
199813a64f0bSJacob Keller continue;
199913a64f0bSJacob Keller } else {
200013a64f0bSJacob Keller u32 hh_ts_lo, hh_ts_hi, tmr_idx;
200113a64f0bSJacob Keller u64 hh_ts;
200213a64f0bSJacob Keller
200313a64f0bSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc;
200413a64f0bSJacob Keller /* Read ART time */
200513a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLHH_ART_TIME_L);
200613a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLHH_ART_TIME_H);
200713a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo;
200813a64f0bSJacob Keller *system = convert_art_ns_to_tsc(hh_ts);
200913a64f0bSJacob Keller /* Read Device source clock time */
201013a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLTSYN_HHTIME_L(tmr_idx));
201113a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLTSYN_HHTIME_H(tmr_idx));
201213a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo;
201313a64f0bSJacob Keller *device = ns_to_ktime(hh_ts);
201413a64f0bSJacob Keller break;
201513a64f0bSJacob Keller }
201613a64f0bSJacob Keller }
201713a64f0bSJacob Keller /* Release HW lock */
201813a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
201913a64f0bSJacob Keller hh_lock = hh_lock & ~PFHH_SEM_BUSY_M;
202013a64f0bSJacob Keller wr32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), hh_lock);
202113a64f0bSJacob Keller
202213a64f0bSJacob Keller if (i == MAX_HH_LOCK_TRIES)
202313a64f0bSJacob Keller return -ETIMEDOUT;
202413a64f0bSJacob Keller
202513a64f0bSJacob Keller return 0;
202613a64f0bSJacob Keller }
202713a64f0bSJacob Keller
202813a64f0bSJacob Keller /**
202913a64f0bSJacob Keller * ice_ptp_getcrosststamp_e822 - Capture a device cross timestamp
203013a64f0bSJacob Keller * @info: the driver's PTP info structure
203113a64f0bSJacob Keller * @cts: The memory to fill the cross timestamp info
203213a64f0bSJacob Keller *
203313a64f0bSJacob Keller * Capture a cross timestamp between the ART and the device PTP hardware
203413a64f0bSJacob Keller * clock. Fill the cross timestamp information and report it back to the
203513a64f0bSJacob Keller * caller.
203613a64f0bSJacob Keller *
203713a64f0bSJacob Keller * This is only valid for E822 devices which have support for generating the
203813a64f0bSJacob Keller * cross timestamp via PCIe PTM.
203913a64f0bSJacob Keller *
204013a64f0bSJacob Keller * In order to correctly correlate the ART timestamp back to the TSC time, the
204113a64f0bSJacob Keller * CPU must have X86_FEATURE_TSC_KNOWN_FREQ.
204213a64f0bSJacob Keller */
204313a64f0bSJacob Keller static int
ice_ptp_getcrosststamp_e822(struct ptp_clock_info * info,struct system_device_crosststamp * cts)204413a64f0bSJacob Keller ice_ptp_getcrosststamp_e822(struct ptp_clock_info *info,
204513a64f0bSJacob Keller struct system_device_crosststamp *cts)
204613a64f0bSJacob Keller {
204713a64f0bSJacob Keller struct ice_pf *pf = ptp_info_to_pf(info);
204813a64f0bSJacob Keller
204913a64f0bSJacob Keller return get_device_system_crosststamp(ice_ptp_get_syncdevicetime,
205013a64f0bSJacob Keller pf, NULL, cts);
205113a64f0bSJacob Keller }
205213a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */
205313a64f0bSJacob Keller
205406c16d89SJacob Keller /**
205577a78115SJacob Keller * ice_ptp_get_ts_config - ioctl interface to read the timestamping config
205677a78115SJacob Keller * @pf: Board private structure
205777a78115SJacob Keller * @ifr: ioctl data
205877a78115SJacob Keller *
205977a78115SJacob Keller * Copy the timestamping config to user buffer
206077a78115SJacob Keller */
ice_ptp_get_ts_config(struct ice_pf * pf,struct ifreq * ifr)206177a78115SJacob Keller int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
206277a78115SJacob Keller {
206377a78115SJacob Keller struct hwtstamp_config *config;
206477a78115SJacob Keller
206577a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags))
206677a78115SJacob Keller return -EIO;
206777a78115SJacob Keller
206877a78115SJacob Keller config = &pf->ptp.tstamp_config;
206977a78115SJacob Keller
207077a78115SJacob Keller return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
207177a78115SJacob Keller -EFAULT : 0;
207277a78115SJacob Keller }
207377a78115SJacob Keller
207477a78115SJacob Keller /**
207577a78115SJacob Keller * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode
207677a78115SJacob Keller * @pf: Board private structure
207777a78115SJacob Keller * @config: hwtstamp settings requested or saved
207877a78115SJacob Keller */
207977a78115SJacob Keller static int
ice_ptp_set_timestamp_mode(struct ice_pf * pf,struct hwtstamp_config * config)208077a78115SJacob Keller ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config)
208177a78115SJacob Keller {
208277a78115SJacob Keller switch (config->tx_type) {
208377a78115SJacob Keller case HWTSTAMP_TX_OFF:
2084ea9b847cSJacob Keller ice_set_tx_tstamp(pf, false);
2085ea9b847cSJacob Keller break;
2086ea9b847cSJacob Keller case HWTSTAMP_TX_ON:
2087ea9b847cSJacob Keller ice_set_tx_tstamp(pf, true);
208877a78115SJacob Keller break;
208977a78115SJacob Keller default:
209077a78115SJacob Keller return -ERANGE;
209177a78115SJacob Keller }
209277a78115SJacob Keller
209377a78115SJacob Keller switch (config->rx_filter) {
209477a78115SJacob Keller case HWTSTAMP_FILTER_NONE:
209577a78115SJacob Keller ice_set_rx_tstamp(pf, false);
209677a78115SJacob Keller break;
209777a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
209877a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
209977a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
210077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_EVENT:
210177a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
210277a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
210377a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_SYNC:
210477a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
210577a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
210677a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
210777a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
210877a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
210977a78115SJacob Keller case HWTSTAMP_FILTER_NTP_ALL:
211077a78115SJacob Keller case HWTSTAMP_FILTER_ALL:
211177a78115SJacob Keller ice_set_rx_tstamp(pf, true);
211277a78115SJacob Keller break;
211377a78115SJacob Keller default:
211477a78115SJacob Keller return -ERANGE;
211577a78115SJacob Keller }
211677a78115SJacob Keller
211777a78115SJacob Keller return 0;
211877a78115SJacob Keller }
211977a78115SJacob Keller
212077a78115SJacob Keller /**
212177a78115SJacob Keller * ice_ptp_set_ts_config - ioctl interface to control the timestamping
212277a78115SJacob Keller * @pf: Board private structure
212377a78115SJacob Keller * @ifr: ioctl data
212477a78115SJacob Keller *
212577a78115SJacob Keller * Get the user config and store it
212677a78115SJacob Keller */
ice_ptp_set_ts_config(struct ice_pf * pf,struct ifreq * ifr)212777a78115SJacob Keller int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
212877a78115SJacob Keller {
212977a78115SJacob Keller struct hwtstamp_config config;
213077a78115SJacob Keller int err;
213177a78115SJacob Keller
213277a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags))
213377a78115SJacob Keller return -EAGAIN;
213477a78115SJacob Keller
213577a78115SJacob Keller if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
213677a78115SJacob Keller return -EFAULT;
213777a78115SJacob Keller
213877a78115SJacob Keller err = ice_ptp_set_timestamp_mode(pf, &config);
213977a78115SJacob Keller if (err)
214077a78115SJacob Keller return err;
214177a78115SJacob Keller
2142e59d75ddSJacob Keller /* Return the actual configuration set */
2143e59d75ddSJacob Keller config = pf->ptp.tstamp_config;
214477a78115SJacob Keller
214577a78115SJacob Keller return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
214677a78115SJacob Keller -EFAULT : 0;
214777a78115SJacob Keller }
214877a78115SJacob Keller
214977a78115SJacob Keller /**
215077a78115SJacob Keller * ice_ptp_rx_hwtstamp - Check for an Rx timestamp
215177a78115SJacob Keller * @rx_ring: Ring to get the VSI info
215277a78115SJacob Keller * @rx_desc: Receive descriptor
215377a78115SJacob Keller * @skb: Particular skb to send timestamp with
215477a78115SJacob Keller *
215577a78115SJacob Keller * The driver receives a notification in the receive descriptor with timestamp.
215677a78115SJacob Keller * The timestamp is in ns, so we must convert the result first.
215777a78115SJacob Keller */
215877a78115SJacob Keller void
ice_ptp_rx_hwtstamp(struct ice_rx_ring * rx_ring,union ice_32b_rx_flex_desc * rx_desc,struct sk_buff * skb)2159e72bba21SMaciej Fijalkowski ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring,
216077a78115SJacob Keller union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb)
216177a78115SJacob Keller {
216277a78115SJacob Keller struct skb_shared_hwtstamps *hwtstamps;
2163b1a582e6SJacob Keller u64 ts_ns, cached_time;
2164b1a582e6SJacob Keller u32 ts_high;
216577a78115SJacob Keller
2166b1a582e6SJacob Keller if (!(rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID))
2167b1a582e6SJacob Keller return;
2168b1a582e6SJacob Keller
2169b1a582e6SJacob Keller cached_time = READ_ONCE(rx_ring->cached_phctime);
2170b1a582e6SJacob Keller
2171b1a582e6SJacob Keller /* Do not report a timestamp if we don't have a cached PHC time */
2172b1a582e6SJacob Keller if (!cached_time)
2173b1a582e6SJacob Keller return;
2174b1a582e6SJacob Keller
2175b1a582e6SJacob Keller /* Use ice_ptp_extend_32b_ts directly, using the ring-specific cached
2176b1a582e6SJacob Keller * PHC value, rather than accessing the PF. This also allows us to
2177b1a582e6SJacob Keller * simply pass the upper 32bits of nanoseconds directly. Calling
2178b1a582e6SJacob Keller * ice_ptp_extend_40b_ts is unnecessary as it would just discard these
2179b1a582e6SJacob Keller * bits itself.
218077a78115SJacob Keller */
218177a78115SJacob Keller ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high);
2182b1a582e6SJacob Keller ts_ns = ice_ptp_extend_32b_ts(cached_time, ts_high);
218377a78115SJacob Keller
218477a78115SJacob Keller hwtstamps = skb_hwtstamps(skb);
218577a78115SJacob Keller memset(hwtstamps, 0, sizeof(*hwtstamps));
218677a78115SJacob Keller hwtstamps->hwtstamp = ns_to_ktime(ts_ns);
218777a78115SJacob Keller }
218877a78115SJacob Keller
218977a78115SJacob Keller /**
2190325b2064SMaciej Machnikowski * ice_ptp_disable_sma_pins_e810t - Disable E810-T SMA pins
2191325b2064SMaciej Machnikowski * @pf: pointer to the PF structure
2192325b2064SMaciej Machnikowski * @info: PTP clock info structure
2193325b2064SMaciej Machnikowski *
2194325b2064SMaciej Machnikowski * Disable the OS access to the SMA pins. Called to clear out the OS
2195325b2064SMaciej Machnikowski * indications of pin support when we fail to setup the E810-T SMA control
2196325b2064SMaciej Machnikowski * register.
2197325b2064SMaciej Machnikowski */
2198325b2064SMaciej Machnikowski static void
ice_ptp_disable_sma_pins_e810t(struct ice_pf * pf,struct ptp_clock_info * info)2199325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info)
2200325b2064SMaciej Machnikowski {
2201325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf);
2202325b2064SMaciej Machnikowski
2203325b2064SMaciej Machnikowski dev_warn(dev, "Failed to configure E810-T SMA pin control\n");
2204325b2064SMaciej Machnikowski
2205325b2064SMaciej Machnikowski info->enable = NULL;
2206325b2064SMaciej Machnikowski info->verify = NULL;
2207325b2064SMaciej Machnikowski info->n_pins = 0;
2208325b2064SMaciej Machnikowski info->n_ext_ts = 0;
2209325b2064SMaciej Machnikowski info->n_per_out = 0;
2210325b2064SMaciej Machnikowski }
2211325b2064SMaciej Machnikowski
2212325b2064SMaciej Machnikowski /**
2213325b2064SMaciej Machnikowski * ice_ptp_setup_sma_pins_e810t - Setup the SMA pins
2214325b2064SMaciej Machnikowski * @pf: pointer to the PF structure
2215325b2064SMaciej Machnikowski * @info: PTP clock info structure
2216325b2064SMaciej Machnikowski *
2217325b2064SMaciej Machnikowski * Finish setting up the SMA pins by allocating pin_config, and setting it up
2218325b2064SMaciej Machnikowski * according to the current status of the SMA. On failure, disable all of the
2219325b2064SMaciej Machnikowski * extended SMA pin support.
2220325b2064SMaciej Machnikowski */
2221325b2064SMaciej Machnikowski static void
ice_ptp_setup_sma_pins_e810t(struct ice_pf * pf,struct ptp_clock_info * info)2222325b2064SMaciej Machnikowski ice_ptp_setup_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info)
2223325b2064SMaciej Machnikowski {
2224325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf);
2225325b2064SMaciej Machnikowski int err;
2226325b2064SMaciej Machnikowski
2227325b2064SMaciej Machnikowski /* Allocate memory for kernel pins interface */
2228325b2064SMaciej Machnikowski info->pin_config = devm_kcalloc(dev, info->n_pins,
2229325b2064SMaciej Machnikowski sizeof(*info->pin_config), GFP_KERNEL);
2230325b2064SMaciej Machnikowski if (!info->pin_config) {
2231325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info);
2232325b2064SMaciej Machnikowski return;
2233325b2064SMaciej Machnikowski }
2234325b2064SMaciej Machnikowski
2235325b2064SMaciej Machnikowski /* Read current SMA status */
2236325b2064SMaciej Machnikowski err = ice_get_sma_config_e810t(&pf->hw, info->pin_config);
2237325b2064SMaciej Machnikowski if (err)
2238325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info);
2239325b2064SMaciej Machnikowski }
2240325b2064SMaciej Machnikowski
2241325b2064SMaciej Machnikowski /**
2242172db5f9SMaciej Machnikowski * ice_ptp_setup_pins_e810 - Setup PTP pins in sysfs
2243896a55aaSAnirudh Venkataramanan * @pf: pointer to the PF instance
2244172db5f9SMaciej Machnikowski * @info: PTP clock capabilities
2245172db5f9SMaciej Machnikowski */
224643c4958aSArkadiusz Kubalewski static void
ice_ptp_setup_pins_e810(struct ice_pf * pf,struct ptp_clock_info * info)224743c4958aSArkadiusz Kubalewski ice_ptp_setup_pins_e810(struct ice_pf *pf, struct ptp_clock_info *info)
2248172db5f9SMaciej Machnikowski {
224943c4958aSArkadiusz Kubalewski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) {
225043c4958aSArkadiusz Kubalewski info->n_ext_ts = N_EXT_TS_E810;
2251*0d1b2236SJacob Keller info->n_per_out = N_PER_OUT_E810T;
225243c4958aSArkadiusz Kubalewski info->n_pins = NUM_PTP_PINS_E810T;
225343c4958aSArkadiusz Kubalewski info->verify = ice_verify_pin_e810t;
225443c4958aSArkadiusz Kubalewski
225543c4958aSArkadiusz Kubalewski /* Complete setup of the SMA pins */
225643c4958aSArkadiusz Kubalewski ice_ptp_setup_sma_pins_e810t(pf, info);
2257*0d1b2236SJacob Keller } else if (ice_is_e810t(&pf->hw)) {
2258*0d1b2236SJacob Keller info->n_ext_ts = N_EXT_TS_NO_SMA_E810T;
2259*0d1b2236SJacob Keller info->n_per_out = N_PER_OUT_NO_SMA_E810T;
2260*0d1b2236SJacob Keller } else {
2261*0d1b2236SJacob Keller info->n_per_out = N_PER_OUT_E810;
2262*0d1b2236SJacob Keller info->n_ext_ts = N_EXT_TS_E810;
226343c4958aSArkadiusz Kubalewski }
2264172db5f9SMaciej Machnikowski }
2265172db5f9SMaciej Machnikowski
2266172db5f9SMaciej Machnikowski /**
2267634d841dSKarol Kolacinski * ice_ptp_setup_pins_e823 - Setup PTP pins in sysfs
2268634d841dSKarol Kolacinski * @pf: pointer to the PF instance
2269634d841dSKarol Kolacinski * @info: PTP clock capabilities
2270634d841dSKarol Kolacinski */
2271634d841dSKarol Kolacinski static void
ice_ptp_setup_pins_e823(struct ice_pf * pf,struct ptp_clock_info * info)2272634d841dSKarol Kolacinski ice_ptp_setup_pins_e823(struct ice_pf *pf, struct ptp_clock_info *info)
2273634d841dSKarol Kolacinski {
2274634d841dSKarol Kolacinski info->pps = 1;
2275634d841dSKarol Kolacinski info->n_per_out = 0;
2276634d841dSKarol Kolacinski info->n_ext_ts = 1;
2277634d841dSKarol Kolacinski }
2278634d841dSKarol Kolacinski
2279634d841dSKarol Kolacinski /**
228013a64f0bSJacob Keller * ice_ptp_set_funcs_e822 - Set specialized functions for E822 support
228113a64f0bSJacob Keller * @pf: Board private structure
228213a64f0bSJacob Keller * @info: PTP info to fill
228313a64f0bSJacob Keller *
228413a64f0bSJacob Keller * Assign functions to the PTP capabiltiies structure for E822 devices.
228513a64f0bSJacob Keller * Functions which operate across all device families should be set directly
228613a64f0bSJacob Keller * in ice_ptp_set_caps. Only add functions here which are distinct for E822
228713a64f0bSJacob Keller * devices.
228813a64f0bSJacob Keller */
228913a64f0bSJacob Keller static void
ice_ptp_set_funcs_e822(struct ice_pf * pf,struct ptp_clock_info * info)229013a64f0bSJacob Keller ice_ptp_set_funcs_e822(struct ice_pf *pf, struct ptp_clock_info *info)
229113a64f0bSJacob Keller {
229213a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS
229313a64f0bSJacob Keller if (boot_cpu_has(X86_FEATURE_ART) &&
229413a64f0bSJacob Keller boot_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ))
229513a64f0bSJacob Keller info->getcrosststamp = ice_ptp_getcrosststamp_e822;
229613a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */
229713a64f0bSJacob Keller }
229813a64f0bSJacob Keller
229913a64f0bSJacob Keller /**
2300172db5f9SMaciej Machnikowski * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support
2301172db5f9SMaciej Machnikowski * @pf: Board private structure
2302172db5f9SMaciej Machnikowski * @info: PTP info to fill
2303172db5f9SMaciej Machnikowski *
2304172db5f9SMaciej Machnikowski * Assign functions to the PTP capabiltiies structure for E810 devices.
2305172db5f9SMaciej Machnikowski * Functions which operate across all device families should be set directly
2306172db5f9SMaciej Machnikowski * in ice_ptp_set_caps. Only add functions here which are distinct for e810
2307172db5f9SMaciej Machnikowski * devices.
2308172db5f9SMaciej Machnikowski */
2309172db5f9SMaciej Machnikowski static void
ice_ptp_set_funcs_e810(struct ice_pf * pf,struct ptp_clock_info * info)2310172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(struct ice_pf *pf, struct ptp_clock_info *info)
2311172db5f9SMaciej Machnikowski {
2312172db5f9SMaciej Machnikowski info->enable = ice_ptp_gpio_enable_e810;
2313896a55aaSAnirudh Venkataramanan ice_ptp_setup_pins_e810(pf, info);
2314172db5f9SMaciej Machnikowski }
2315172db5f9SMaciej Machnikowski
2316172db5f9SMaciej Machnikowski /**
2317634d841dSKarol Kolacinski * ice_ptp_set_funcs_e823 - Set specialized functions for E823 support
2318634d841dSKarol Kolacinski * @pf: Board private structure
2319634d841dSKarol Kolacinski * @info: PTP info to fill
2320634d841dSKarol Kolacinski *
2321634d841dSKarol Kolacinski * Assign functions to the PTP capabiltiies structure for E823 devices.
2322634d841dSKarol Kolacinski * Functions which operate across all device families should be set directly
2323634d841dSKarol Kolacinski * in ice_ptp_set_caps. Only add functions here which are distinct for e823
2324634d841dSKarol Kolacinski * devices.
2325634d841dSKarol Kolacinski */
2326634d841dSKarol Kolacinski static void
ice_ptp_set_funcs_e823(struct ice_pf * pf,struct ptp_clock_info * info)2327634d841dSKarol Kolacinski ice_ptp_set_funcs_e823(struct ice_pf *pf, struct ptp_clock_info *info)
2328634d841dSKarol Kolacinski {
2329634d841dSKarol Kolacinski info->enable = ice_ptp_gpio_enable_e823;
2330634d841dSKarol Kolacinski ice_ptp_setup_pins_e823(pf, info);
2331634d841dSKarol Kolacinski }
2332634d841dSKarol Kolacinski
2333634d841dSKarol Kolacinski /**
233406c16d89SJacob Keller * ice_ptp_set_caps - Set PTP capabilities
233506c16d89SJacob Keller * @pf: Board private structure
233606c16d89SJacob Keller */
ice_ptp_set_caps(struct ice_pf * pf)233706c16d89SJacob Keller static void ice_ptp_set_caps(struct ice_pf *pf)
233806c16d89SJacob Keller {
233906c16d89SJacob Keller struct ptp_clock_info *info = &pf->ptp.info;
234006c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf);
234106c16d89SJacob Keller
234206c16d89SJacob Keller snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk",
234306c16d89SJacob Keller dev_driver_string(dev), dev_name(dev));
234406c16d89SJacob Keller info->owner = THIS_MODULE;
23458aa4318cSSiddaraju DH info->max_adj = 100000000;
234606c16d89SJacob Keller info->adjtime = ice_ptp_adjtime;
234706c16d89SJacob Keller info->adjfine = ice_ptp_adjfine;
234806c16d89SJacob Keller info->gettimex64 = ice_ptp_gettimex64;
234906c16d89SJacob Keller info->settime64 = ice_ptp_settime64;
2350172db5f9SMaciej Machnikowski
23513a749623SJacob Keller if (ice_is_e810(&pf->hw))
2352172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(pf, info);
2353634d841dSKarol Kolacinski else if (ice_is_e823(&pf->hw))
2354634d841dSKarol Kolacinski ice_ptp_set_funcs_e823(pf, info);
235513a64f0bSJacob Keller else
235613a64f0bSJacob Keller ice_ptp_set_funcs_e822(pf, info);
235706c16d89SJacob Keller }
235806c16d89SJacob Keller
235906c16d89SJacob Keller /**
236006c16d89SJacob Keller * ice_ptp_create_clock - Create PTP clock device for userspace
236106c16d89SJacob Keller * @pf: Board private structure
236206c16d89SJacob Keller *
236306c16d89SJacob Keller * This function creates a new PTP clock device. It only creates one if we
236406c16d89SJacob Keller * don't already have one. Will return error if it can't create one, but success
236506c16d89SJacob Keller * if we already have a device. Should be used by ice_ptp_init to create clock
236606c16d89SJacob Keller * initially, and prevent global resets from creating new clock devices.
236706c16d89SJacob Keller */
ice_ptp_create_clock(struct ice_pf * pf)236806c16d89SJacob Keller static long ice_ptp_create_clock(struct ice_pf *pf)
236906c16d89SJacob Keller {
237006c16d89SJacob Keller struct ptp_clock_info *info;
237106c16d89SJacob Keller struct ptp_clock *clock;
237206c16d89SJacob Keller struct device *dev;
237306c16d89SJacob Keller
237406c16d89SJacob Keller /* No need to create a clock device if we already have one */
237506c16d89SJacob Keller if (pf->ptp.clock)
237606c16d89SJacob Keller return 0;
237706c16d89SJacob Keller
237806c16d89SJacob Keller ice_ptp_set_caps(pf);
237906c16d89SJacob Keller
238006c16d89SJacob Keller info = &pf->ptp.info;
238106c16d89SJacob Keller dev = ice_pf_to_dev(pf);
238206c16d89SJacob Keller
238306c16d89SJacob Keller /* Attempt to register the clock before enabling the hardware. */
238406c16d89SJacob Keller clock = ptp_clock_register(info, dev);
238506c16d89SJacob Keller if (IS_ERR(clock))
238606c16d89SJacob Keller return PTR_ERR(clock);
238706c16d89SJacob Keller
238806c16d89SJacob Keller pf->ptp.clock = clock;
238906c16d89SJacob Keller
239006c16d89SJacob Keller return 0;
239106c16d89SJacob Keller }
239206c16d89SJacob Keller
2393ea9b847cSJacob Keller /**
2394ea9b847cSJacob Keller * ice_ptp_request_ts - Request an available Tx timestamp index
2395ea9b847cSJacob Keller * @tx: the PTP Tx timestamp tracker to request from
2396ea9b847cSJacob Keller * @skb: the SKB to associate with this timestamp request
2397ea9b847cSJacob Keller */
ice_ptp_request_ts(struct ice_ptp_tx * tx,struct sk_buff * skb)2398ea9b847cSJacob Keller s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
2399ea9b847cSJacob Keller {
2400ea9b847cSJacob Keller u8 idx;
2401ea9b847cSJacob Keller
2402ea9b847cSJacob Keller spin_lock(&tx->lock);
24033ad5c10bSJacob Keller
24043ad5c10bSJacob Keller /* Check that this tracker is accepting new timestamp requests */
24053ad5c10bSJacob Keller if (!ice_ptp_is_tx_tracker_up(tx)) {
24063ad5c10bSJacob Keller spin_unlock(&tx->lock);
24073ad5c10bSJacob Keller return -1;
24083ad5c10bSJacob Keller }
24093ad5c10bSJacob Keller
2410ea9b847cSJacob Keller /* Find and set the first available index */
2411ea9b847cSJacob Keller idx = find_first_zero_bit(tx->in_use, tx->len);
2412ea9b847cSJacob Keller if (idx < tx->len) {
2413ea9b847cSJacob Keller /* We got a valid index that no other thread could have set. Store
2414ea9b847cSJacob Keller * a reference to the skb and the start time to allow discarding old
2415ea9b847cSJacob Keller * requests.
2416ea9b847cSJacob Keller */
2417ea9b847cSJacob Keller set_bit(idx, tx->in_use);
2418d40fd600SJacob Keller clear_bit(idx, tx->stale);
2419ea9b847cSJacob Keller tx->tstamps[idx].start = jiffies;
2420ea9b847cSJacob Keller tx->tstamps[idx].skb = skb_get(skb);
2421ea9b847cSJacob Keller skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
24224c120218SJacob Keller ice_trace(tx_tstamp_request, skb, idx);
2423ea9b847cSJacob Keller }
2424ea9b847cSJacob Keller
2425ea9b847cSJacob Keller spin_unlock(&tx->lock);
2426ea9b847cSJacob Keller
2427ea9b847cSJacob Keller /* return the appropriate PHY timestamp register index, -1 if no
2428ea9b847cSJacob Keller * indexes were available.
2429ea9b847cSJacob Keller */
2430ea9b847cSJacob Keller if (idx >= tx->len)
2431ea9b847cSJacob Keller return -1;
2432ea9b847cSJacob Keller else
24336b5cbc8cSSergey Temerkhanov return idx + tx->offset;
2434ea9b847cSJacob Keller }
2435ea9b847cSJacob Keller
2436ea9b847cSJacob Keller /**
24371229b339SKarol Kolacinski * ice_ptp_process_ts - Process the PTP Tx timestamps
2438ea9b847cSJacob Keller * @pf: Board private structure
2439ea9b847cSJacob Keller *
2440ae39eb42SJacob Keller * Returns: ICE_TX_TSTAMP_WORK_PENDING if there are any outstanding Tx
2441ae39eb42SJacob Keller * timestamps that need processing, and ICE_TX_TSTAMP_WORK_DONE otherwise.
2442ea9b847cSJacob Keller */
ice_ptp_process_ts(struct ice_pf * pf)2443ae39eb42SJacob Keller enum ice_tx_tstamp_work ice_ptp_process_ts(struct ice_pf *pf)
2444ea9b847cSJacob Keller {
24451229b339SKarol Kolacinski return ice_ptp_tx_tstamp(&pf->ptp.port.tx);
2446ea9b847cSJacob Keller }
2447ea9b847cSJacob Keller
ice_ptp_periodic_work(struct kthread_work * work)244877a78115SJacob Keller static void ice_ptp_periodic_work(struct kthread_work *work)
244977a78115SJacob Keller {
245077a78115SJacob Keller struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work);
245177a78115SJacob Keller struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp);
24524503cc7fSArkadiusz Kubalewski int err;
245377a78115SJacob Keller
245477a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags))
245577a78115SJacob Keller return;
245677a78115SJacob Keller
24574503cc7fSArkadiusz Kubalewski err = ice_ptp_update_cached_phctime(pf);
245877a78115SJacob Keller
24594503cc7fSArkadiusz Kubalewski /* Run twice a second or reschedule if phc update failed */
246077a78115SJacob Keller kthread_queue_delayed_work(ptp->kworker, &ptp->work,
24614503cc7fSArkadiusz Kubalewski msecs_to_jiffies(err ? 10 : 500));
246277a78115SJacob Keller }
246377a78115SJacob Keller
246406c16d89SJacob Keller /**
246548096710SKarol Kolacinski * ice_ptp_reset - Initialize PTP hardware clock support after reset
246648096710SKarol Kolacinski * @pf: Board private structure
246748096710SKarol Kolacinski */
ice_ptp_reset(struct ice_pf * pf)246848096710SKarol Kolacinski void ice_ptp_reset(struct ice_pf *pf)
246948096710SKarol Kolacinski {
247048096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp;
247148096710SKarol Kolacinski struct ice_hw *hw = &pf->hw;
247248096710SKarol Kolacinski struct timespec64 ts;
24733a749623SJacob Keller int err, itr = 1;
247448096710SKarol Kolacinski u64 time_diff;
247548096710SKarol Kolacinski
247648096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state))
247748096710SKarol Kolacinski goto pfr;
247848096710SKarol Kolacinski
2479b2ee7256SJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned)
24803a749623SJacob Keller goto reset_ts;
248148096710SKarol Kolacinski
2482b2ee7256SJacob Keller err = ice_ptp_init_phc(hw);
248348096710SKarol Kolacinski if (err)
248448096710SKarol Kolacinski goto err;
248548096710SKarol Kolacinski
248648096710SKarol Kolacinski /* Acquire the global hardware lock */
248748096710SKarol Kolacinski if (!ice_ptp_lock(hw)) {
248848096710SKarol Kolacinski err = -EBUSY;
248948096710SKarol Kolacinski goto err;
249048096710SKarol Kolacinski }
249148096710SKarol Kolacinski
249248096710SKarol Kolacinski /* Write the increment time value to PHY and LAN */
249378267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf));
249448096710SKarol Kolacinski if (err) {
249548096710SKarol Kolacinski ice_ptp_unlock(hw);
249648096710SKarol Kolacinski goto err;
249748096710SKarol Kolacinski }
249848096710SKarol Kolacinski
249948096710SKarol Kolacinski /* Write the initial Time value to PHY and LAN using the cached PHC
250048096710SKarol Kolacinski * time before the reset and time difference between stopping and
250148096710SKarol Kolacinski * starting the clock.
250248096710SKarol Kolacinski */
250348096710SKarol Kolacinski if (ptp->cached_phc_time) {
250448096710SKarol Kolacinski time_diff = ktime_get_real_ns() - ptp->reset_time;
250548096710SKarol Kolacinski ts = ns_to_timespec64(ptp->cached_phc_time + time_diff);
250648096710SKarol Kolacinski } else {
250748096710SKarol Kolacinski ts = ktime_to_timespec64(ktime_get_real());
250848096710SKarol Kolacinski }
250948096710SKarol Kolacinski err = ice_ptp_write_init(pf, &ts);
251048096710SKarol Kolacinski if (err) {
251148096710SKarol Kolacinski ice_ptp_unlock(hw);
251248096710SKarol Kolacinski goto err;
251348096710SKarol Kolacinski }
251448096710SKarol Kolacinski
251548096710SKarol Kolacinski /* Release the global hardware lock */
251648096710SKarol Kolacinski ice_ptp_unlock(hw);
251748096710SKarol Kolacinski
25183a749623SJacob Keller if (!ice_is_e810(hw)) {
25193a749623SJacob Keller /* Enable quad interrupts */
25203a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr);
25213a749623SJacob Keller if (err)
25223a749623SJacob Keller goto err;
25233a749623SJacob Keller }
25243a749623SJacob Keller
25253a749623SJacob Keller reset_ts:
25263a749623SJacob Keller /* Restart the PHY timestamping block */
25273a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf);
25283a749623SJacob Keller
252948096710SKarol Kolacinski pfr:
253048096710SKarol Kolacinski /* Init Tx structures */
2531a69f1cb6SJacob Keller if (ice_is_e810(&pf->hw)) {
253248096710SKarol Kolacinski err = ice_ptp_init_tx_e810(pf, &ptp->port.tx);
2533a69f1cb6SJacob Keller } else {
2534a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp->port.ov_work,
2535f029a343SSiddaraju DH ice_ptp_wait_for_offsets);
25363a749623SJacob Keller err = ice_ptp_init_tx_e822(pf, &ptp->port.tx,
25373a749623SJacob Keller ptp->port.port_num);
2538a69f1cb6SJacob Keller }
253948096710SKarol Kolacinski if (err)
254048096710SKarol Kolacinski goto err;
254148096710SKarol Kolacinski
254248096710SKarol Kolacinski set_bit(ICE_FLAG_PTP, pf->flags);
254348096710SKarol Kolacinski
254448096710SKarol Kolacinski /* Start periodic work going */
254548096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0);
254648096710SKarol Kolacinski
254748096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP reset successful\n");
254848096710SKarol Kolacinski return;
254948096710SKarol Kolacinski
255048096710SKarol Kolacinski err:
255148096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP reset failed %d\n", err);
255248096710SKarol Kolacinski }
255348096710SKarol Kolacinski
255448096710SKarol Kolacinski /**
255548096710SKarol Kolacinski * ice_ptp_prepare_for_reset - Prepare PTP for reset
255648096710SKarol Kolacinski * @pf: Board private structure
255748096710SKarol Kolacinski */
ice_ptp_prepare_for_reset(struct ice_pf * pf)255848096710SKarol Kolacinski void ice_ptp_prepare_for_reset(struct ice_pf *pf)
255948096710SKarol Kolacinski {
256048096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp;
256148096710SKarol Kolacinski u8 src_tmr;
256248096710SKarol Kolacinski
256348096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags);
256448096710SKarol Kolacinski
256548096710SKarol Kolacinski /* Disable timestamping for both Tx and Rx */
256648096710SKarol Kolacinski ice_ptp_cfg_timestamp(pf, false);
256748096710SKarol Kolacinski
256848096710SKarol Kolacinski kthread_cancel_delayed_work_sync(&ptp->work);
256948096710SKarol Kolacinski
257048096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state))
257148096710SKarol Kolacinski return;
257248096710SKarol Kolacinski
257348096710SKarol Kolacinski ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
257448096710SKarol Kolacinski
257548096710SKarol Kolacinski /* Disable periodic outputs */
257648096710SKarol Kolacinski ice_ptp_disable_all_clkout(pf);
257748096710SKarol Kolacinski
257848096710SKarol Kolacinski src_tmr = ice_get_ptp_src_clock_index(&pf->hw);
257948096710SKarol Kolacinski
258048096710SKarol Kolacinski /* Disable source clock */
258148096710SKarol Kolacinski wr32(&pf->hw, GLTSYN_ENA(src_tmr), (u32)~GLTSYN_ENA_TSYN_ENA_M);
258248096710SKarol Kolacinski
258348096710SKarol Kolacinski /* Acquire PHC and system timer to restore after reset */
258448096710SKarol Kolacinski ptp->reset_time = ktime_get_real_ns();
258548096710SKarol Kolacinski }
258648096710SKarol Kolacinski
258748096710SKarol Kolacinski /**
258806c16d89SJacob Keller * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device
258906c16d89SJacob Keller * @pf: Board private structure
259006c16d89SJacob Keller *
259106c16d89SJacob Keller * Setup and initialize a PTP clock device that represents the device hardware
259206c16d89SJacob Keller * clock. Save the clock index for other functions connected to the same
259306c16d89SJacob Keller * hardware resource.
259406c16d89SJacob Keller */
ice_ptp_init_owner(struct ice_pf * pf)259506c16d89SJacob Keller static int ice_ptp_init_owner(struct ice_pf *pf)
259606c16d89SJacob Keller {
259706c16d89SJacob Keller struct ice_hw *hw = &pf->hw;
259806c16d89SJacob Keller struct timespec64 ts;
25993a749623SJacob Keller int err, itr = 1;
260006c16d89SJacob Keller
2601b2ee7256SJacob Keller err = ice_ptp_init_phc(hw);
2602b2ee7256SJacob Keller if (err) {
2603b2ee7256SJacob Keller dev_err(ice_pf_to_dev(pf), "Failed to initialize PHC, err %d\n",
2604b2ee7256SJacob Keller err);
2605b2ee7256SJacob Keller return err;
2606b2ee7256SJacob Keller }
260706c16d89SJacob Keller
260806c16d89SJacob Keller /* Acquire the global hardware lock */
260906c16d89SJacob Keller if (!ice_ptp_lock(hw)) {
261006c16d89SJacob Keller err = -EBUSY;
261106c16d89SJacob Keller goto err_exit;
261206c16d89SJacob Keller }
261306c16d89SJacob Keller
261406c16d89SJacob Keller /* Write the increment time value to PHY and LAN */
261578267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf));
261606c16d89SJacob Keller if (err) {
261706c16d89SJacob Keller ice_ptp_unlock(hw);
261806c16d89SJacob Keller goto err_exit;
261906c16d89SJacob Keller }
262006c16d89SJacob Keller
262106c16d89SJacob Keller ts = ktime_to_timespec64(ktime_get_real());
262206c16d89SJacob Keller /* Write the initial Time value to PHY and LAN */
262306c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts);
262406c16d89SJacob Keller if (err) {
262506c16d89SJacob Keller ice_ptp_unlock(hw);
262606c16d89SJacob Keller goto err_exit;
262706c16d89SJacob Keller }
262806c16d89SJacob Keller
262906c16d89SJacob Keller /* Release the global hardware lock */
263006c16d89SJacob Keller ice_ptp_unlock(hw);
263106c16d89SJacob Keller
26323a749623SJacob Keller if (!ice_is_e810(hw)) {
26333a749623SJacob Keller /* Enable quad interrupts */
26343a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr);
26353a749623SJacob Keller if (err)
26363a749623SJacob Keller goto err_exit;
26373a749623SJacob Keller }
26383a749623SJacob Keller
263906c16d89SJacob Keller /* Ensure we have a clock device */
264006c16d89SJacob Keller err = ice_ptp_create_clock(pf);
264106c16d89SJacob Keller if (err)
264206c16d89SJacob Keller goto err_clk;
264306c16d89SJacob Keller
264467569a7fSJacob Keller /* Store the PTP clock index for other PFs */
264567569a7fSJacob Keller ice_set_ptp_clock_index(pf);
264667569a7fSJacob Keller
264706c16d89SJacob Keller return 0;
264806c16d89SJacob Keller
264906c16d89SJacob Keller err_clk:
265006c16d89SJacob Keller pf->ptp.clock = NULL;
265106c16d89SJacob Keller err_exit:
265206c16d89SJacob Keller return err;
265306c16d89SJacob Keller }
265406c16d89SJacob Keller
265506c16d89SJacob Keller /**
265648096710SKarol Kolacinski * ice_ptp_init_work - Initialize PTP work threads
265748096710SKarol Kolacinski * @pf: Board private structure
265848096710SKarol Kolacinski * @ptp: PF PTP structure
265948096710SKarol Kolacinski */
ice_ptp_init_work(struct ice_pf * pf,struct ice_ptp * ptp)266048096710SKarol Kolacinski static int ice_ptp_init_work(struct ice_pf *pf, struct ice_ptp *ptp)
266148096710SKarol Kolacinski {
266248096710SKarol Kolacinski struct kthread_worker *kworker;
266348096710SKarol Kolacinski
266448096710SKarol Kolacinski /* Initialize work functions */
266548096710SKarol Kolacinski kthread_init_delayed_work(&ptp->work, ice_ptp_periodic_work);
266648096710SKarol Kolacinski
266748096710SKarol Kolacinski /* Allocate a kworker for handling work required for the ports
266848096710SKarol Kolacinski * connected to the PTP hardware clock.
266948096710SKarol Kolacinski */
267048096710SKarol Kolacinski kworker = kthread_create_worker(0, "ice-ptp-%s",
267148096710SKarol Kolacinski dev_name(ice_pf_to_dev(pf)));
267248096710SKarol Kolacinski if (IS_ERR(kworker))
267348096710SKarol Kolacinski return PTR_ERR(kworker);
267448096710SKarol Kolacinski
267548096710SKarol Kolacinski ptp->kworker = kworker;
267648096710SKarol Kolacinski
267748096710SKarol Kolacinski /* Start periodic work going */
267848096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0);
267948096710SKarol Kolacinski
268048096710SKarol Kolacinski return 0;
268148096710SKarol Kolacinski }
268248096710SKarol Kolacinski
268348096710SKarol Kolacinski /**
26843a749623SJacob Keller * ice_ptp_init_port - Initialize PTP port structure
26853a749623SJacob Keller * @pf: Board private structure
26863a749623SJacob Keller * @ptp_port: PTP port structure
26873a749623SJacob Keller */
ice_ptp_init_port(struct ice_pf * pf,struct ice_ptp_port * ptp_port)26883a749623SJacob Keller static int ice_ptp_init_port(struct ice_pf *pf, struct ice_ptp_port *ptp_port)
26893a749623SJacob Keller {
26903a749623SJacob Keller mutex_init(&ptp_port->ps_lock);
26913a749623SJacob Keller
26923a749623SJacob Keller if (ice_is_e810(&pf->hw))
26933a749623SJacob Keller return ice_ptp_init_tx_e810(pf, &ptp_port->tx);
26943a749623SJacob Keller
2695a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp_port->ov_work,
2696f029a343SSiddaraju DH ice_ptp_wait_for_offsets);
26973a749623SJacob Keller return ice_ptp_init_tx_e822(pf, &ptp_port->tx, ptp_port->port_num);
26983a749623SJacob Keller }
26993a749623SJacob Keller
27003a749623SJacob Keller /**
2701b2ee7256SJacob Keller * ice_ptp_init - Initialize PTP hardware clock support
270206c16d89SJacob Keller * @pf: Board private structure
270306c16d89SJacob Keller *
2704b2ee7256SJacob Keller * Set up the device for interacting with the PTP hardware clock for all
2705b2ee7256SJacob Keller * functions, both the function that owns the clock hardware, and the
2706b2ee7256SJacob Keller * functions connected to the clock hardware.
2707b2ee7256SJacob Keller *
2708b2ee7256SJacob Keller * The clock owner will allocate and register a ptp_clock with the
2709b2ee7256SJacob Keller * PTP_1588_CLOCK infrastructure. All functions allocate a kthread and work
2710b2ee7256SJacob Keller * items used for asynchronous work such as Tx timestamps and periodic work.
271106c16d89SJacob Keller */
ice_ptp_init(struct ice_pf * pf)271206c16d89SJacob Keller void ice_ptp_init(struct ice_pf *pf)
271306c16d89SJacob Keller {
271448096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp;
271506c16d89SJacob Keller struct ice_hw *hw = &pf->hw;
271606c16d89SJacob Keller int err;
271706c16d89SJacob Keller
2718b2ee7256SJacob Keller /* If this function owns the clock hardware, it must allocate and
2719b2ee7256SJacob Keller * configure the PTP clock device to represent it.
2720b2ee7256SJacob Keller */
272106c16d89SJacob Keller if (hw->func_caps.ts_func_info.src_tmr_owned) {
272206c16d89SJacob Keller err = ice_ptp_init_owner(pf);
272306c16d89SJacob Keller if (err)
272448096710SKarol Kolacinski goto err;
272506c16d89SJacob Keller }
272606c16d89SJacob Keller
27273a749623SJacob Keller ptp->port.port_num = hw->pf_id;
27283a749623SJacob Keller err = ice_ptp_init_port(pf, &ptp->port);
272948096710SKarol Kolacinski if (err)
273048096710SKarol Kolacinski goto err;
273177a78115SJacob Keller
27323a749623SJacob Keller /* Start the PHY timestamping block */
27333a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf);
27343a749623SJacob Keller
273506c16d89SJacob Keller set_bit(ICE_FLAG_PTP, pf->flags);
273648096710SKarol Kolacinski err = ice_ptp_init_work(pf, ptp);
273748096710SKarol Kolacinski if (err)
273848096710SKarol Kolacinski goto err;
273906c16d89SJacob Keller
274048096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP init successful\n");
274177a78115SJacob Keller return;
274277a78115SJacob Keller
274348096710SKarol Kolacinski err:
274477a78115SJacob Keller /* If we registered a PTP clock, release it */
274577a78115SJacob Keller if (pf->ptp.clock) {
274648096710SKarol Kolacinski ptp_clock_unregister(ptp->clock);
274777a78115SJacob Keller pf->ptp.clock = NULL;
274877a78115SJacob Keller }
274948096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags);
275048096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP failed %d\n", err);
275106c16d89SJacob Keller }
275206c16d89SJacob Keller
275306c16d89SJacob Keller /**
275406c16d89SJacob Keller * ice_ptp_release - Disable the driver/HW support and unregister the clock
275506c16d89SJacob Keller * @pf: Board private structure
275606c16d89SJacob Keller *
275706c16d89SJacob Keller * This function handles the cleanup work required from the initialization by
275806c16d89SJacob Keller * clearing out the important information and unregistering the clock
275906c16d89SJacob Keller */
ice_ptp_release(struct ice_pf * pf)276006c16d89SJacob Keller void ice_ptp_release(struct ice_pf *pf)
276106c16d89SJacob Keller {
2762fd1b5bebSYongxin Liu if (!test_bit(ICE_FLAG_PTP, pf->flags))
2763fd1b5bebSYongxin Liu return;
2764fd1b5bebSYongxin Liu
276577a78115SJacob Keller /* Disable timestamping for both Tx and Rx */
276677a78115SJacob Keller ice_ptp_cfg_timestamp(pf, false);
276777a78115SJacob Keller
2768ea9b847cSJacob Keller ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
2769ea9b847cSJacob Keller
277006c16d89SJacob Keller clear_bit(ICE_FLAG_PTP, pf->flags);
277106c16d89SJacob Keller
277277a78115SJacob Keller kthread_cancel_delayed_work_sync(&pf->ptp.work);
277377a78115SJacob Keller
27743a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port);
27753a749623SJacob Keller mutex_destroy(&pf->ptp.port.ps_lock);
277677a78115SJacob Keller if (pf->ptp.kworker) {
277777a78115SJacob Keller kthread_destroy_worker(pf->ptp.kworker);
277877a78115SJacob Keller pf->ptp.kworker = NULL;
277977a78115SJacob Keller }
278077a78115SJacob Keller
278106c16d89SJacob Keller if (!pf->ptp.clock)
278206c16d89SJacob Keller return;
278306c16d89SJacob Keller
27849ee31343SJacob Keller /* Disable periodic outputs */
27859ee31343SJacob Keller ice_ptp_disable_all_clkout(pf);
27869ee31343SJacob Keller
278767569a7fSJacob Keller ice_clear_ptp_clock_index(pf);
278806c16d89SJacob Keller ptp_clock_unregister(pf->ptp.clock);
278906c16d89SJacob Keller pf->ptp.clock = NULL;
279006c16d89SJacob Keller
279106c16d89SJacob Keller dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n");
279206c16d89SJacob Keller }
2793