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
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
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
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
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  */
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  */
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  */
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  */
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  */
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  */
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
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  */
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  */
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
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 /**
6201229b339SKarol Kolacinski  * ice_ptp_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  *
63630f15874SJacob Keller  * Returns true if all timestamps were handled, and false if any slots remain
63730f15874SJacob Keller  * without a timestamp.
63830f15874SJacob Keller  *
63930f15874SJacob Keller  * After looping, if we still have waiting SKBs, return false. This may cause
64030f15874SJacob Keller  * us effectively poll even when not strictly necessary. We do this because
64130f15874SJacob Keller  * it's possible a new timestamp was requested around the same time as the
64230f15874SJacob Keller  * interrupt. In some cases hardware might not interrupt us again when the
64330f15874SJacob Keller  * timestamp is captured.
6444b1251bdSJacob Keller  *
645d40fd600SJacob Keller  * Note that we do not hold the tracking lock while reading the Tx timestamp.
646d40fd600SJacob Keller  * This is because reading the timestamp requires taking a mutex that might
647d40fd600SJacob Keller  * sleep.
6480dd92862SJacob Keller  *
649d40fd600SJacob Keller  * The only place where we set in_use is when a new timestamp is initiated
650d40fd600SJacob Keller  * with a slot index. This is only called in the hard xmit routine where an
651d40fd600SJacob Keller  * SKB has a request flag set. The only places where we clear this bit is this
652d40fd600SJacob Keller  * function, or during teardown when the Tx timestamp tracker is being
653d40fd600SJacob Keller  * removed. A timestamp index will never be re-used until the in_use bit for
654d40fd600SJacob Keller  * that index is cleared.
6550dd92862SJacob Keller  *
6560dd92862SJacob Keller  * If a Tx thread starts a new timestamp, we might not begin processing it
6570dd92862SJacob Keller  * right away but we will notice it at the end when we re-queue the task.
6580dd92862SJacob Keller  *
6590dd92862SJacob Keller  * If a Tx thread starts a new timestamp just after this function exits, the
6600dd92862SJacob Keller  * interrupt for that timestamp should re-trigger this function once
6610dd92862SJacob Keller  * a timestamp is ready.
6620dd92862SJacob Keller  *
663d40fd600SJacob Keller  * In cases where the PTP hardware clock was directly adjusted, some
664d40fd600SJacob Keller  * timestamps may not be able to safely use the timestamp extension math. In
665d40fd600SJacob Keller  * this case, software will set the stale bit for any outstanding Tx
666d40fd600SJacob Keller  * timestamps when the clock is adjusted. Then this function will discard
667d40fd600SJacob Keller  * those captured timestamps instead of sending them to the stack.
6680dd92862SJacob Keller  *
6690dd92862SJacob Keller  * If a Tx packet has been waiting for more than 2 seconds, it is not possible
6700dd92862SJacob Keller  * to correctly extend the timestamp using the cached PHC time. It is
6710dd92862SJacob Keller  * extremely unlikely that a packet will ever take this long to timestamp. If
6720dd92862SJacob Keller  * we detect a Tx timestamp request that has waited for this long we assume
6730dd92862SJacob Keller  * the packet will never be sent by hardware and discard it without reading
6740dd92862SJacob Keller  * the timestamp register.
6754b1251bdSJacob Keller  */
6761229b339SKarol Kolacinski static bool ice_ptp_tx_tstamp(struct ice_ptp_tx *tx)
6774b1251bdSJacob Keller {
6784b1251bdSJacob Keller 	struct ice_ptp_port *ptp_port;
679f0ae1240SJacob Keller 	bool more_timestamps;
6804b1251bdSJacob Keller 	struct ice_pf *pf;
68110e4b4a3SJacob Keller 	struct ice_hw *hw;
68210e4b4a3SJacob Keller 	u64 tstamp_ready;
68310e4b4a3SJacob Keller 	int err;
6844b1251bdSJacob Keller 	u8 idx;
6854b1251bdSJacob Keller 
6864b1251bdSJacob Keller 	if (!tx->init)
68730f15874SJacob Keller 		return true;
6884b1251bdSJacob Keller 
6894b1251bdSJacob Keller 	ptp_port = container_of(tx, struct ice_ptp_port, tx);
6904b1251bdSJacob Keller 	pf = ptp_port_to_pf(ptp_port);
69110e4b4a3SJacob Keller 	hw = &pf->hw;
69210e4b4a3SJacob Keller 
69310e4b4a3SJacob Keller 	/* Read the Tx ready status first */
69410e4b4a3SJacob Keller 	err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready);
69510e4b4a3SJacob Keller 	if (err)
69610e4b4a3SJacob Keller 		return false;
6974b1251bdSJacob Keller 
6984b1251bdSJacob Keller 	for_each_set_bit(idx, tx->in_use, tx->len) {
6994b1251bdSJacob Keller 		struct skb_shared_hwtstamps shhwtstamps = {};
7006b5cbc8cSSergey Temerkhanov 		u8 phy_idx = idx + tx->offset;
7010dd92862SJacob Keller 		u64 raw_tstamp = 0, tstamp;
7020dd92862SJacob Keller 		bool drop_ts = false;
7034b1251bdSJacob Keller 		struct sk_buff *skb;
7044b1251bdSJacob Keller 
7050dd92862SJacob Keller 		/* Drop packets which have waited for more than 2 seconds */
7060dd92862SJacob Keller 		if (time_is_before_jiffies(tx->tstamps[idx].start + 2 * HZ)) {
7070dd92862SJacob Keller 			drop_ts = true;
7080dd92862SJacob Keller 
7090dd92862SJacob Keller 			/* Count the number of Tx timestamps that timed out */
7100dd92862SJacob Keller 			pf->ptp.tx_hwtstamp_timeouts++;
71110e4b4a3SJacob Keller 		}
7120dd92862SJacob Keller 
71310e4b4a3SJacob Keller 		/* Only read a timestamp from the PHY if its marked as ready
71410e4b4a3SJacob Keller 		 * by the tstamp_ready register. This avoids unnecessary
71510e4b4a3SJacob Keller 		 * reading of timestamps which are not yet valid. This is
71610e4b4a3SJacob Keller 		 * important as we must read all timestamps which are valid
71710e4b4a3SJacob Keller 		 * and only timestamps which are valid during each interrupt.
71810e4b4a3SJacob Keller 		 * If we do not, the hardware logic for generating a new
71910e4b4a3SJacob Keller 		 * interrupt can get stuck on some devices.
72010e4b4a3SJacob Keller 		 */
72110e4b4a3SJacob Keller 		if (!(tstamp_ready & BIT_ULL(phy_idx))) {
72210e4b4a3SJacob Keller 			if (drop_ts)
7230dd92862SJacob Keller 				goto skip_ts_read;
72410e4b4a3SJacob Keller 
72510e4b4a3SJacob Keller 			continue;
7260dd92862SJacob Keller 		}
7270dd92862SJacob Keller 
7284b1251bdSJacob Keller 		ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx);
7294b1251bdSJacob Keller 
73010e4b4a3SJacob Keller 		err = ice_read_phy_tstamp(hw, tx->block, phy_idx, &raw_tstamp);
7314b1251bdSJacob Keller 		if (err)
7324b1251bdSJacob Keller 			continue;
7334b1251bdSJacob Keller 
7344b1251bdSJacob Keller 		ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx);
7354b1251bdSJacob Keller 
73610e4b4a3SJacob Keller 		/* For PHYs which don't implement a proper timestamp ready
73710e4b4a3SJacob Keller 		 * bitmap, verify that the timestamp value is different
73810e4b4a3SJacob Keller 		 * from the last cached timestamp. If it is not, skip this for
73910e4b4a3SJacob Keller 		 * now assuming it hasn't yet been captured by hardware.
74010e4b4a3SJacob Keller 		 */
74110e4b4a3SJacob Keller 		if (!drop_ts && tx->verify_cached &&
7424b1251bdSJacob Keller 		    raw_tstamp == tx->tstamps[idx].cached_tstamp)
7434b1251bdSJacob Keller 			continue;
7444b1251bdSJacob Keller 
74510e4b4a3SJacob Keller 		/* Discard any timestamp value without the valid bit set */
74610e4b4a3SJacob Keller 		if (!(raw_tstamp & ICE_PTP_TS_VALID))
74710e4b4a3SJacob Keller 			drop_ts = true;
74810e4b4a3SJacob Keller 
7490dd92862SJacob Keller skip_ts_read:
7504b1251bdSJacob Keller 		spin_lock(&tx->lock);
75110e4b4a3SJacob Keller 		if (tx->verify_cached && raw_tstamp)
7524b1251bdSJacob Keller 			tx->tstamps[idx].cached_tstamp = raw_tstamp;
7534b1251bdSJacob Keller 		clear_bit(idx, tx->in_use);
7544b1251bdSJacob Keller 		skb = tx->tstamps[idx].skb;
7554b1251bdSJacob Keller 		tx->tstamps[idx].skb = NULL;
756d40fd600SJacob Keller 		if (test_and_clear_bit(idx, tx->stale))
757d40fd600SJacob Keller 			drop_ts = true;
7584b1251bdSJacob Keller 		spin_unlock(&tx->lock);
7594b1251bdSJacob Keller 
7600dd92862SJacob Keller 		/* It is unlikely but possible that the SKB will have been
7610dd92862SJacob Keller 		 * flushed at this point due to link change or teardown.
7624b1251bdSJacob Keller 		 */
7634b1251bdSJacob Keller 		if (!skb)
7644b1251bdSJacob Keller 			continue;
7654b1251bdSJacob Keller 
7660dd92862SJacob Keller 		if (drop_ts) {
7670dd92862SJacob Keller 			dev_kfree_skb_any(skb);
7680dd92862SJacob Keller 			continue;
7690dd92862SJacob Keller 		}
7700dd92862SJacob Keller 
7714b1251bdSJacob Keller 		/* Extend the timestamp using cached PHC time */
7724b1251bdSJacob Keller 		tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp);
7734b1251bdSJacob Keller 		if (tstamp) {
7744b1251bdSJacob Keller 			shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
7754b1251bdSJacob Keller 			ice_trace(tx_tstamp_complete, skb, idx);
7764b1251bdSJacob Keller 		}
7774b1251bdSJacob Keller 
7784b1251bdSJacob Keller 		skb_tstamp_tx(skb, &shhwtstamps);
7794b1251bdSJacob Keller 		dev_kfree_skb_any(skb);
7804b1251bdSJacob Keller 	}
7814b1251bdSJacob Keller 
7824b1251bdSJacob Keller 	/* Check if we still have work to do. If so, re-queue this task to
7834b1251bdSJacob Keller 	 * poll for remaining timestamps.
7844b1251bdSJacob Keller 	 */
7854b1251bdSJacob Keller 	spin_lock(&tx->lock);
786f0ae1240SJacob Keller 	more_timestamps = tx->init && !bitmap_empty(tx->in_use, tx->len);
7874b1251bdSJacob Keller 	spin_unlock(&tx->lock);
7881229b339SKarol Kolacinski 
789f0ae1240SJacob Keller 	return !more_timestamps;
7904b1251bdSJacob Keller }
7914b1251bdSJacob Keller 
7924b1251bdSJacob Keller /**
7934b1251bdSJacob Keller  * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps
7944b1251bdSJacob Keller  * @tx: Tx tracking structure to initialize
7954b1251bdSJacob Keller  *
7964b1251bdSJacob Keller  * Assumes that the length has already been initialized. Do not call directly,
7976b5cbc8cSSergey Temerkhanov  * use the ice_ptp_init_tx_* instead.
7984b1251bdSJacob Keller  */
7994b1251bdSJacob Keller static int
8004b1251bdSJacob Keller ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx)
8014b1251bdSJacob Keller {
802d40fd600SJacob Keller 	unsigned long *in_use, *stale;
803c1f3414dSJacob Keller 	struct ice_tx_tstamp *tstamps;
8044b1251bdSJacob Keller 
805c1f3414dSJacob Keller 	tstamps = kcalloc(tx->len, sizeof(*tstamps), GFP_KERNEL);
806c1f3414dSJacob Keller 	in_use = bitmap_zalloc(tx->len, GFP_KERNEL);
807d40fd600SJacob Keller 	stale = bitmap_zalloc(tx->len, GFP_KERNEL);
808c1f3414dSJacob Keller 
809d40fd600SJacob Keller 	if (!tstamps || !in_use || !stale) {
810c1f3414dSJacob Keller 		kfree(tstamps);
811c1f3414dSJacob Keller 		bitmap_free(in_use);
812d40fd600SJacob Keller 		bitmap_free(stale);
813c1f3414dSJacob Keller 
8144b1251bdSJacob Keller 		return -ENOMEM;
8154b1251bdSJacob Keller 	}
8164b1251bdSJacob Keller 
817c1f3414dSJacob Keller 	tx->tstamps = tstamps;
818c1f3414dSJacob Keller 	tx->in_use = in_use;
819d40fd600SJacob Keller 	tx->stale = stale;
8204b1251bdSJacob Keller 	tx->init = 1;
8214b1251bdSJacob Keller 
8223ad5c10bSJacob Keller 	spin_lock_init(&tx->lock);
8233ad5c10bSJacob Keller 
8244b1251bdSJacob Keller 	return 0;
8254b1251bdSJacob Keller }
8264b1251bdSJacob Keller 
8274b1251bdSJacob Keller /**
8284b1251bdSJacob Keller  * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker
8294b1251bdSJacob Keller  * @pf: Board private structure
8304b1251bdSJacob Keller  * @tx: the tracker to flush
831e3ba5248SJacob Keller  *
832e3ba5248SJacob Keller  * Called during teardown when a Tx tracker is being removed.
8334b1251bdSJacob Keller  */
8344b1251bdSJacob Keller static void
8354b1251bdSJacob Keller ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
8364b1251bdSJacob Keller {
837e3ba5248SJacob Keller 	struct ice_hw *hw = &pf->hw;
838e3ba5248SJacob Keller 	u64 tstamp_ready;
839e3ba5248SJacob Keller 	int err;
8404b1251bdSJacob Keller 	u8 idx;
8414b1251bdSJacob Keller 
842e3ba5248SJacob Keller 	err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready);
843e3ba5248SJacob Keller 	if (err) {
844e3ba5248SJacob Keller 		dev_dbg(ice_pf_to_dev(pf), "Failed to get the Tx tstamp ready bitmap for block %u, err %d\n",
845e3ba5248SJacob Keller 			tx->block, err);
846e3ba5248SJacob Keller 
847e3ba5248SJacob Keller 		/* If we fail to read the Tx timestamp ready bitmap just
848e3ba5248SJacob Keller 		 * skip clearing the PHY timestamps.
849e3ba5248SJacob Keller 		 */
850e3ba5248SJacob Keller 		tstamp_ready = 0;
851e3ba5248SJacob Keller 	}
852e3ba5248SJacob Keller 
853e3ba5248SJacob Keller 	for_each_set_bit(idx, tx->in_use, tx->len) {
8546b5cbc8cSSergey Temerkhanov 		u8 phy_idx = idx + tx->offset;
855e3ba5248SJacob Keller 		struct sk_buff *skb;
856e3ba5248SJacob Keller 
857e3ba5248SJacob Keller 		/* In case this timestamp is ready, we need to clear it. */
858e3ba5248SJacob Keller 		if (!hw->reset_ongoing && (tstamp_ready & BIT_ULL(phy_idx)))
859e3ba5248SJacob Keller 			ice_clear_phy_tstamp(hw, tx->block, phy_idx);
8604b1251bdSJacob Keller 
8614b1251bdSJacob Keller 		spin_lock(&tx->lock);
862e3ba5248SJacob Keller 		skb = tx->tstamps[idx].skb;
8634b1251bdSJacob Keller 		tx->tstamps[idx].skb = NULL;
8644b1251bdSJacob Keller 		clear_bit(idx, tx->in_use);
865d40fd600SJacob Keller 		clear_bit(idx, tx->stale);
8664b1251bdSJacob Keller 		spin_unlock(&tx->lock);
8674b1251bdSJacob Keller 
868e3ba5248SJacob Keller 		/* Count the number of Tx timestamps flushed */
869e3ba5248SJacob Keller 		pf->ptp.tx_hwtstamp_flushed++;
870e3ba5248SJacob Keller 
871e3ba5248SJacob Keller 		/* Free the SKB after we've cleared the bit */
872e3ba5248SJacob Keller 		dev_kfree_skb_any(skb);
8734b1251bdSJacob Keller 	}
8744b1251bdSJacob Keller }
8754b1251bdSJacob Keller 
8764b1251bdSJacob Keller /**
877d40fd600SJacob Keller  * ice_ptp_mark_tx_tracker_stale - Mark unfinished timestamps as stale
878d40fd600SJacob Keller  * @tx: the tracker to mark
879d40fd600SJacob Keller  *
880d40fd600SJacob Keller  * Mark currently outstanding Tx timestamps as stale. This prevents sending
881d40fd600SJacob Keller  * their timestamp value to the stack. This is required to prevent extending
882d40fd600SJacob Keller  * the 40bit hardware timestamp incorrectly.
883d40fd600SJacob Keller  *
884d40fd600SJacob Keller  * This should be called when the PTP clock is modified such as after a set
885d40fd600SJacob Keller  * time request.
886d40fd600SJacob Keller  */
887d40fd600SJacob Keller static void
888d40fd600SJacob Keller ice_ptp_mark_tx_tracker_stale(struct ice_ptp_tx *tx)
889d40fd600SJacob Keller {
890d40fd600SJacob Keller 	spin_lock(&tx->lock);
891d40fd600SJacob Keller 	bitmap_or(tx->stale, tx->stale, tx->in_use, tx->len);
892d40fd600SJacob Keller 	spin_unlock(&tx->lock);
893d40fd600SJacob Keller }
894d40fd600SJacob Keller 
895d40fd600SJacob Keller /**
8964b1251bdSJacob Keller  * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker
8974b1251bdSJacob Keller  * @pf: Board private structure
8984b1251bdSJacob Keller  * @tx: Tx tracking structure to release
8994b1251bdSJacob Keller  *
9004b1251bdSJacob Keller  * Free memory associated with the Tx timestamp tracker.
9014b1251bdSJacob Keller  */
9024b1251bdSJacob Keller static void
9034b1251bdSJacob Keller ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
9044b1251bdSJacob Keller {
9053ad5c10bSJacob Keller 	spin_lock(&tx->lock);
9064b1251bdSJacob Keller 	tx->init = 0;
9073ad5c10bSJacob Keller 	spin_unlock(&tx->lock);
9084b1251bdSJacob Keller 
909f0ae1240SJacob Keller 	/* wait for potentially outstanding interrupt to complete */
910f0ae1240SJacob Keller 	synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
911f0ae1240SJacob Keller 
9124b1251bdSJacob Keller 	ice_ptp_flush_tx_tracker(pf, tx);
9134b1251bdSJacob Keller 
9144b1251bdSJacob Keller 	kfree(tx->tstamps);
9154b1251bdSJacob Keller 	tx->tstamps = NULL;
9164b1251bdSJacob Keller 
9174b1251bdSJacob Keller 	bitmap_free(tx->in_use);
9184b1251bdSJacob Keller 	tx->in_use = NULL;
9194b1251bdSJacob Keller 
920d40fd600SJacob Keller 	bitmap_free(tx->stale);
921d40fd600SJacob Keller 	tx->stale = NULL;
922d40fd600SJacob Keller 
9234b1251bdSJacob Keller 	tx->len = 0;
9244b1251bdSJacob Keller }
9254b1251bdSJacob Keller 
9264b1251bdSJacob Keller /**
9274b1251bdSJacob Keller  * ice_ptp_init_tx_e822 - Initialize tracking for Tx timestamps
9284b1251bdSJacob Keller  * @pf: Board private structure
9294b1251bdSJacob Keller  * @tx: the Tx tracking structure to initialize
9304b1251bdSJacob Keller  * @port: the port this structure tracks
9314b1251bdSJacob Keller  *
9324b1251bdSJacob Keller  * Initialize the Tx timestamp tracker for this port. For generic MAC devices,
9334b1251bdSJacob Keller  * the timestamp block is shared for all ports in the same quad. To avoid
9344b1251bdSJacob Keller  * ports using the same timestamp index, logically break the block of
9354b1251bdSJacob Keller  * registers into chunks based on the port number.
9364b1251bdSJacob Keller  */
9374b1251bdSJacob Keller static int
9384b1251bdSJacob Keller ice_ptp_init_tx_e822(struct ice_pf *pf, struct ice_ptp_tx *tx, u8 port)
9394b1251bdSJacob Keller {
9406b5cbc8cSSergey Temerkhanov 	tx->block = port / ICE_PORTS_PER_QUAD;
9416b5cbc8cSSergey Temerkhanov 	tx->offset = (port % ICE_PORTS_PER_QUAD) * INDEX_PER_PORT_E822;
9426b5cbc8cSSergey Temerkhanov 	tx->len = INDEX_PER_PORT_E822;
94310e4b4a3SJacob Keller 	tx->verify_cached = 0;
9444b1251bdSJacob Keller 
9454b1251bdSJacob Keller 	return ice_ptp_alloc_tx_tracker(tx);
9464b1251bdSJacob Keller }
9474b1251bdSJacob Keller 
9484b1251bdSJacob Keller /**
9494b1251bdSJacob Keller  * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps
9504b1251bdSJacob Keller  * @pf: Board private structure
9514b1251bdSJacob Keller  * @tx: the Tx tracking structure to initialize
9524b1251bdSJacob Keller  *
9534b1251bdSJacob Keller  * Initialize the Tx timestamp tracker for this PF. For E810 devices, each
9544b1251bdSJacob Keller  * port has its own block of timestamps, independent of the other ports.
9554b1251bdSJacob Keller  */
9564b1251bdSJacob Keller static int
9574b1251bdSJacob Keller ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx)
9584b1251bdSJacob Keller {
9596b5cbc8cSSergey Temerkhanov 	tx->block = pf->hw.port_info->lport;
9606b5cbc8cSSergey Temerkhanov 	tx->offset = 0;
9616b5cbc8cSSergey Temerkhanov 	tx->len = INDEX_PER_PORT_E810;
96210e4b4a3SJacob Keller 	/* The E810 PHY does not provide a timestamp ready bitmap. Instead,
96310e4b4a3SJacob Keller 	 * verify new timestamps against cached copy of the last read
96410e4b4a3SJacob Keller 	 * timestamp.
96510e4b4a3SJacob Keller 	 */
96610e4b4a3SJacob Keller 	tx->verify_cached = 1;
9674b1251bdSJacob Keller 
9684b1251bdSJacob Keller 	return ice_ptp_alloc_tx_tracker(tx);
9694b1251bdSJacob Keller }
9704b1251bdSJacob Keller 
9714b1251bdSJacob Keller /**
9724b1251bdSJacob Keller  * ice_ptp_update_cached_phctime - Update the cached PHC time values
9734b1251bdSJacob Keller  * @pf: Board specific private structure
9744b1251bdSJacob Keller  *
9754b1251bdSJacob Keller  * This function updates the system time values which are cached in the PF
9764b1251bdSJacob Keller  * structure and the Rx rings.
9774b1251bdSJacob Keller  *
9784b1251bdSJacob Keller  * This function must be called periodically to ensure that the cached value
979b1a582e6SJacob Keller  * is never more than 2 seconds old.
980b1a582e6SJacob Keller  *
981b1a582e6SJacob Keller  * Note that the cached copy in the PF PTP structure is always updated, even
982b1a582e6SJacob Keller  * if we can't update the copy in the Rx rings.
9834b1251bdSJacob Keller  *
9844b1251bdSJacob Keller  * Return:
9854b1251bdSJacob Keller  * * 0 - OK, successfully updated
9864b1251bdSJacob Keller  * * -EAGAIN - PF was busy, need to reschedule the update
9874b1251bdSJacob Keller  */
9884b1251bdSJacob Keller static int ice_ptp_update_cached_phctime(struct ice_pf *pf)
9894b1251bdSJacob Keller {
9904b1251bdSJacob Keller 	struct device *dev = ice_pf_to_dev(pf);
9914b1251bdSJacob Keller 	unsigned long update_before;
9924b1251bdSJacob Keller 	u64 systime;
9934b1251bdSJacob Keller 	int i;
9944b1251bdSJacob Keller 
9954b1251bdSJacob Keller 	update_before = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000);
9964b1251bdSJacob Keller 	if (pf->ptp.cached_phc_time &&
9974b1251bdSJacob Keller 	    time_is_before_jiffies(update_before)) {
9984b1251bdSJacob Keller 		unsigned long time_taken = jiffies - pf->ptp.cached_phc_jiffies;
9994b1251bdSJacob Keller 
10004b1251bdSJacob Keller 		dev_warn(dev, "%u msecs passed between update to cached PHC time\n",
10014b1251bdSJacob Keller 			 jiffies_to_msecs(time_taken));
10024b1251bdSJacob Keller 		pf->ptp.late_cached_phc_updates++;
10034b1251bdSJacob Keller 	}
10044b1251bdSJacob Keller 
10054b1251bdSJacob Keller 	/* Read the current PHC time */
10064b1251bdSJacob Keller 	systime = ice_ptp_read_src_clk_reg(pf, NULL);
10074b1251bdSJacob Keller 
10084b1251bdSJacob Keller 	/* Update the cached PHC time stored in the PF structure */
10094b1251bdSJacob Keller 	WRITE_ONCE(pf->ptp.cached_phc_time, systime);
10104b1251bdSJacob Keller 	WRITE_ONCE(pf->ptp.cached_phc_jiffies, jiffies);
10114b1251bdSJacob Keller 
1012b1a582e6SJacob Keller 	if (test_and_set_bit(ICE_CFG_BUSY, pf->state))
1013b1a582e6SJacob Keller 		return -EAGAIN;
1014b1a582e6SJacob Keller 
10154b1251bdSJacob Keller 	ice_for_each_vsi(pf, i) {
10164b1251bdSJacob Keller 		struct ice_vsi *vsi = pf->vsi[i];
10174b1251bdSJacob Keller 		int j;
10184b1251bdSJacob Keller 
10194b1251bdSJacob Keller 		if (!vsi)
10204b1251bdSJacob Keller 			continue;
10214b1251bdSJacob Keller 
10224b1251bdSJacob Keller 		if (vsi->type != ICE_VSI_PF)
10234b1251bdSJacob Keller 			continue;
10244b1251bdSJacob Keller 
10254b1251bdSJacob Keller 		ice_for_each_rxq(vsi, j) {
10264b1251bdSJacob Keller 			if (!vsi->rx_rings[j])
10274b1251bdSJacob Keller 				continue;
10284b1251bdSJacob Keller 			WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime);
10294b1251bdSJacob Keller 		}
10304b1251bdSJacob Keller 	}
10314b1251bdSJacob Keller 	clear_bit(ICE_CFG_BUSY, pf->state);
10324b1251bdSJacob Keller 
10334b1251bdSJacob Keller 	return 0;
10344b1251bdSJacob Keller }
10354b1251bdSJacob Keller 
10364b1251bdSJacob Keller /**
1037b1a582e6SJacob Keller  * ice_ptp_reset_cached_phctime - Reset cached PHC time after an update
1038b1a582e6SJacob Keller  * @pf: Board specific private structure
1039b1a582e6SJacob Keller  *
1040b1a582e6SJacob Keller  * This function must be called when the cached PHC time is no longer valid,
1041d40fd600SJacob Keller  * such as after a time adjustment. It marks any currently outstanding Tx
1042d40fd600SJacob Keller  * timestamps as stale and updates the cached PHC time for both the PF and Rx
1043d40fd600SJacob Keller  * rings.
1044b1a582e6SJacob Keller  *
1045d40fd600SJacob Keller  * If updating the PHC time cannot be done immediately, a warning message is
1046d40fd600SJacob Keller  * logged and the work item is scheduled immediately to minimize the window
1047d40fd600SJacob Keller  * with a wrong cached timestamp.
1048b1a582e6SJacob Keller  */
1049b1a582e6SJacob Keller static void ice_ptp_reset_cached_phctime(struct ice_pf *pf)
1050b1a582e6SJacob Keller {
1051b1a582e6SJacob Keller 	struct device *dev = ice_pf_to_dev(pf);
1052b1a582e6SJacob Keller 	int err;
1053b1a582e6SJacob Keller 
1054b1a582e6SJacob Keller 	/* Update the cached PHC time immediately if possible, otherwise
1055b1a582e6SJacob Keller 	 * schedule the work item to execute soon.
1056b1a582e6SJacob Keller 	 */
1057b1a582e6SJacob Keller 	err = ice_ptp_update_cached_phctime(pf);
1058b1a582e6SJacob Keller 	if (err) {
1059b1a582e6SJacob Keller 		/* If another thread is updating the Rx rings, we won't
1060b1a582e6SJacob Keller 		 * properly reset them here. This could lead to reporting of
1061b1a582e6SJacob Keller 		 * invalid timestamps, but there isn't much we can do.
1062b1a582e6SJacob Keller 		 */
1063b1a582e6SJacob Keller 		dev_warn(dev, "%s: ICE_CFG_BUSY, unable to immediately update cached PHC time\n",
1064b1a582e6SJacob Keller 			 __func__);
1065b1a582e6SJacob Keller 
1066b1a582e6SJacob Keller 		/* Queue the work item to update the Rx rings when possible */
1067b1a582e6SJacob Keller 		kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work,
1068b1a582e6SJacob Keller 					   msecs_to_jiffies(10));
1069b1a582e6SJacob Keller 	}
1070b1a582e6SJacob Keller 
1071d40fd600SJacob Keller 	/* Mark any outstanding timestamps as stale, since they might have
1072d40fd600SJacob Keller 	 * been captured in hardware before the time update. This could lead
1073d40fd600SJacob Keller 	 * to us extending them with the wrong cached value resulting in
1074d40fd600SJacob Keller 	 * incorrect timestamp values.
1075d40fd600SJacob Keller 	 */
1076d40fd600SJacob Keller 	ice_ptp_mark_tx_tracker_stale(&pf->ptp.port.tx);
1077b1a582e6SJacob Keller }
1078b1a582e6SJacob Keller 
1079b1a582e6SJacob Keller /**
108006c16d89SJacob Keller  * ice_ptp_read_time - Read the time from the device
108106c16d89SJacob Keller  * @pf: Board private structure
108206c16d89SJacob Keller  * @ts: timespec structure to hold the current time value
108306c16d89SJacob Keller  * @sts: Optional parameter for holding a pair of system timestamps from
108406c16d89SJacob Keller  *       the system clock. Will be ignored if NULL is given.
108506c16d89SJacob Keller  *
108606c16d89SJacob Keller  * This function reads the source clock registers and stores them in a timespec.
108706c16d89SJacob Keller  * However, since the registers are 64 bits of nanoseconds, we must convert the
108806c16d89SJacob Keller  * result to a timespec before we can return.
108906c16d89SJacob Keller  */
109006c16d89SJacob Keller static void
109106c16d89SJacob Keller ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts,
109206c16d89SJacob Keller 		  struct ptp_system_timestamp *sts)
109306c16d89SJacob Keller {
109406c16d89SJacob Keller 	u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts);
109506c16d89SJacob Keller 
109606c16d89SJacob Keller 	*ts = ns_to_timespec64(time_ns);
109706c16d89SJacob Keller }
109806c16d89SJacob Keller 
109906c16d89SJacob Keller /**
110006c16d89SJacob Keller  * ice_ptp_write_init - Set PHC time to provided value
110106c16d89SJacob Keller  * @pf: Board private structure
110206c16d89SJacob Keller  * @ts: timespec structure that holds the new time value
110306c16d89SJacob Keller  *
110406c16d89SJacob Keller  * Set the PHC time to the specified time provided in the timespec.
110506c16d89SJacob Keller  */
110606c16d89SJacob Keller static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts)
110706c16d89SJacob Keller {
110806c16d89SJacob Keller 	u64 ns = timespec64_to_ns(ts);
110906c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
111006c16d89SJacob Keller 
111106c16d89SJacob Keller 	return ice_ptp_init_time(hw, ns);
111206c16d89SJacob Keller }
111306c16d89SJacob Keller 
111406c16d89SJacob Keller /**
111506c16d89SJacob Keller  * ice_ptp_write_adj - Adjust PHC clock time atomically
111606c16d89SJacob Keller  * @pf: Board private structure
111706c16d89SJacob Keller  * @adj: Adjustment in nanoseconds
111806c16d89SJacob Keller  *
111906c16d89SJacob Keller  * Perform an atomic adjustment of the PHC time by the specified number of
112006c16d89SJacob Keller  * nanoseconds.
112106c16d89SJacob Keller  */
112206c16d89SJacob Keller static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj)
112306c16d89SJacob Keller {
112406c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
112506c16d89SJacob Keller 
112606c16d89SJacob Keller 	return ice_ptp_adj_clock(hw, adj);
112706c16d89SJacob Keller }
112806c16d89SJacob Keller 
112906c16d89SJacob Keller /**
113078267d0cSJacob Keller  * ice_base_incval - Get base timer increment value
113178267d0cSJacob Keller  * @pf: Board private structure
113278267d0cSJacob Keller  *
113378267d0cSJacob Keller  * Look up the base timer increment value for this device. The base increment
113478267d0cSJacob Keller  * value is used to define the nominal clock tick rate. This increment value
113578267d0cSJacob Keller  * is programmed during device initialization. It is also used as the basis
113678267d0cSJacob Keller  * for calculating adjustments using scaled_ppm.
113778267d0cSJacob Keller  */
113878267d0cSJacob Keller static u64 ice_base_incval(struct ice_pf *pf)
113978267d0cSJacob Keller {
11403a749623SJacob Keller 	struct ice_hw *hw = &pf->hw;
11413a749623SJacob Keller 	u64 incval;
11423a749623SJacob Keller 
11433a749623SJacob Keller 	if (ice_is_e810(hw))
11443a749623SJacob Keller 		incval = ICE_PTP_NOMINAL_INCVAL_E810;
11453a749623SJacob Keller 	else if (ice_e822_time_ref(hw) < NUM_ICE_TIME_REF_FREQ)
11463a749623SJacob Keller 		incval = ice_e822_nominal_incval(ice_e822_time_ref(hw));
11473a749623SJacob Keller 	else
11483a749623SJacob Keller 		incval = UNKNOWN_INCVAL_E822;
11493a749623SJacob Keller 
11503a749623SJacob Keller 	dev_dbg(ice_pf_to_dev(pf), "PTP: using base increment value of 0x%016llx\n",
11513a749623SJacob Keller 		incval);
11523a749623SJacob Keller 
11533a749623SJacob Keller 	return incval;
11543a749623SJacob Keller }
11553a749623SJacob Keller 
11563a749623SJacob Keller /**
1157a69f1cb6SJacob Keller  * ice_ptp_check_tx_fifo - Check whether Tx FIFO is in an OK state
1158a69f1cb6SJacob Keller  * @port: PTP port for which Tx FIFO is checked
1159a69f1cb6SJacob Keller  */
1160a69f1cb6SJacob Keller static int ice_ptp_check_tx_fifo(struct ice_ptp_port *port)
1161a69f1cb6SJacob Keller {
1162a69f1cb6SJacob Keller 	int quad = port->port_num / ICE_PORTS_PER_QUAD;
1163a69f1cb6SJacob Keller 	int offs = port->port_num % ICE_PORTS_PER_QUAD;
1164a69f1cb6SJacob Keller 	struct ice_pf *pf;
1165a69f1cb6SJacob Keller 	struct ice_hw *hw;
1166a69f1cb6SJacob Keller 	u32 val, phy_sts;
1167a69f1cb6SJacob Keller 	int err;
1168a69f1cb6SJacob Keller 
1169a69f1cb6SJacob Keller 	pf = ptp_port_to_pf(port);
1170a69f1cb6SJacob Keller 	hw = &pf->hw;
1171a69f1cb6SJacob Keller 
1172a69f1cb6SJacob Keller 	if (port->tx_fifo_busy_cnt == FIFO_OK)
1173a69f1cb6SJacob Keller 		return 0;
1174a69f1cb6SJacob Keller 
1175a69f1cb6SJacob Keller 	/* need to read FIFO state */
1176a69f1cb6SJacob Keller 	if (offs == 0 || offs == 1)
1177a69f1cb6SJacob Keller 		err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO01_STATUS,
1178a69f1cb6SJacob Keller 					     &val);
1179a69f1cb6SJacob Keller 	else
1180a69f1cb6SJacob Keller 		err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO23_STATUS,
1181a69f1cb6SJacob Keller 					     &val);
1182a69f1cb6SJacob Keller 
1183a69f1cb6SJacob Keller 	if (err) {
1184a69f1cb6SJacob Keller 		dev_err(ice_pf_to_dev(pf), "PTP failed to check port %d Tx FIFO, err %d\n",
1185a69f1cb6SJacob Keller 			port->port_num, err);
1186a69f1cb6SJacob Keller 		return err;
1187a69f1cb6SJacob Keller 	}
1188a69f1cb6SJacob Keller 
1189a69f1cb6SJacob Keller 	if (offs & 0x1)
1190a69f1cb6SJacob Keller 		phy_sts = (val & Q_REG_FIFO13_M) >> Q_REG_FIFO13_S;
1191a69f1cb6SJacob Keller 	else
1192a69f1cb6SJacob Keller 		phy_sts = (val & Q_REG_FIFO02_M) >> Q_REG_FIFO02_S;
1193a69f1cb6SJacob Keller 
1194a69f1cb6SJacob Keller 	if (phy_sts & FIFO_EMPTY) {
1195a69f1cb6SJacob Keller 		port->tx_fifo_busy_cnt = FIFO_OK;
1196a69f1cb6SJacob Keller 		return 0;
1197a69f1cb6SJacob Keller 	}
1198a69f1cb6SJacob Keller 
1199a69f1cb6SJacob Keller 	port->tx_fifo_busy_cnt++;
1200a69f1cb6SJacob Keller 
1201a69f1cb6SJacob Keller 	dev_dbg(ice_pf_to_dev(pf), "Try %d, port %d FIFO not empty\n",
1202a69f1cb6SJacob Keller 		port->tx_fifo_busy_cnt, port->port_num);
1203a69f1cb6SJacob Keller 
1204a69f1cb6SJacob Keller 	if (port->tx_fifo_busy_cnt == ICE_PTP_FIFO_NUM_CHECKS) {
1205a69f1cb6SJacob Keller 		dev_dbg(ice_pf_to_dev(pf),
1206a69f1cb6SJacob Keller 			"Port %d Tx FIFO still not empty; resetting quad %d\n",
1207a69f1cb6SJacob Keller 			port->port_num, quad);
1208407b66c0SKarol Kolacinski 		ice_ptp_reset_ts_memory_quad_e822(hw, quad);
1209a69f1cb6SJacob Keller 		port->tx_fifo_busy_cnt = FIFO_OK;
1210a69f1cb6SJacob Keller 		return 0;
1211a69f1cb6SJacob Keller 	}
1212a69f1cb6SJacob Keller 
1213a69f1cb6SJacob Keller 	return -EAGAIN;
1214a69f1cb6SJacob Keller }
1215a69f1cb6SJacob Keller 
1216a69f1cb6SJacob Keller /**
1217f029a343SSiddaraju DH  * ice_ptp_wait_for_offsets - Check for valid Tx and Rx offsets
1218a69f1cb6SJacob Keller  * @work: Pointer to the kthread_work structure for this task
1219a69f1cb6SJacob Keller  *
1220f029a343SSiddaraju DH  * Check whether hardware has completed measuring the Tx and Rx offset values
1221f029a343SSiddaraju DH  * used to configure and enable vernier timestamp calibration.
1222a69f1cb6SJacob Keller  *
1223f029a343SSiddaraju DH  * Once the offset in either direction is measured, configure the associated
1224f029a343SSiddaraju DH  * registers with the calibrated offset values and enable timestamping. The Tx
1225f029a343SSiddaraju DH  * and Rx directions are configured independently as soon as their associated
1226f029a343SSiddaraju DH  * offsets are known.
1227f029a343SSiddaraju DH  *
1228f029a343SSiddaraju DH  * This function reschedules itself until both Tx and Rx calibration have
1229f029a343SSiddaraju DH  * completed.
1230a69f1cb6SJacob Keller  */
1231f029a343SSiddaraju DH static void ice_ptp_wait_for_offsets(struct kthread_work *work)
1232a69f1cb6SJacob Keller {
1233a69f1cb6SJacob Keller 	struct ice_ptp_port *port;
1234a69f1cb6SJacob Keller 	struct ice_pf *pf;
1235a69f1cb6SJacob Keller 	struct ice_hw *hw;
1236f029a343SSiddaraju DH 	int tx_err;
1237f029a343SSiddaraju DH 	int rx_err;
1238a69f1cb6SJacob Keller 
1239a69f1cb6SJacob Keller 	port = container_of(work, struct ice_ptp_port, ov_work.work);
1240a69f1cb6SJacob Keller 	pf = ptp_port_to_pf(port);
1241a69f1cb6SJacob Keller 	hw = &pf->hw;
1242a69f1cb6SJacob Keller 
124395af1f1cSJacob Keller 	if (ice_is_reset_in_progress(pf->state)) {
124495af1f1cSJacob Keller 		/* wait for device driver to complete reset */
124595af1f1cSJacob Keller 		kthread_queue_delayed_work(pf->ptp.kworker,
124695af1f1cSJacob Keller 					   &port->ov_work,
124795af1f1cSJacob Keller 					   msecs_to_jiffies(100));
12480b57e0d4SMichal Michalik 		return;
124995af1f1cSJacob Keller 	}
12500b57e0d4SMichal Michalik 
1251f029a343SSiddaraju DH 	tx_err = ice_ptp_check_tx_fifo(port);
1252f029a343SSiddaraju DH 	if (!tx_err)
1253f029a343SSiddaraju DH 		tx_err = ice_phy_cfg_tx_offset_e822(hw, port->port_num);
1254f029a343SSiddaraju DH 	rx_err = ice_phy_cfg_rx_offset_e822(hw, port->port_num);
1255f029a343SSiddaraju DH 	if (tx_err || rx_err) {
1256f029a343SSiddaraju DH 		/* Tx and/or Rx offset not yet configured, try again later */
1257a69f1cb6SJacob Keller 		kthread_queue_delayed_work(pf->ptp.kworker,
1258a69f1cb6SJacob Keller 					   &port->ov_work,
1259a69f1cb6SJacob Keller 					   msecs_to_jiffies(100));
1260a69f1cb6SJacob Keller 		return;
1261a69f1cb6SJacob Keller 	}
1262a69f1cb6SJacob Keller }
1263a69f1cb6SJacob Keller 
1264a69f1cb6SJacob Keller /**
12653a749623SJacob Keller  * ice_ptp_port_phy_stop - Stop timestamping for a PHY port
12663a749623SJacob Keller  * @ptp_port: PTP port to stop
12673a749623SJacob Keller  */
12683a749623SJacob Keller static int
12693a749623SJacob Keller ice_ptp_port_phy_stop(struct ice_ptp_port *ptp_port)
12703a749623SJacob Keller {
12713a749623SJacob Keller 	struct ice_pf *pf = ptp_port_to_pf(ptp_port);
12723a749623SJacob Keller 	u8 port = ptp_port->port_num;
12733a749623SJacob Keller 	struct ice_hw *hw = &pf->hw;
12743a749623SJacob Keller 	int err;
12753a749623SJacob Keller 
12763a749623SJacob Keller 	if (ice_is_e810(hw))
12773a749623SJacob Keller 		return 0;
12783a749623SJacob Keller 
12793a749623SJacob Keller 	mutex_lock(&ptp_port->ps_lock);
12803a749623SJacob Keller 
1281a69f1cb6SJacob Keller 	kthread_cancel_delayed_work_sync(&ptp_port->ov_work);
1282a69f1cb6SJacob Keller 
12833a749623SJacob Keller 	err = ice_stop_phy_timer_e822(hw, port, true);
12843a749623SJacob Keller 	if (err)
12853a749623SJacob Keller 		dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d down, err %d\n",
12863a749623SJacob Keller 			port, err);
12873a749623SJacob Keller 
12883a749623SJacob Keller 	mutex_unlock(&ptp_port->ps_lock);
12893a749623SJacob Keller 
12903a749623SJacob Keller 	return err;
12913a749623SJacob Keller }
12923a749623SJacob Keller 
12933a749623SJacob Keller /**
12943a749623SJacob Keller  * ice_ptp_port_phy_restart - (Re)start and calibrate PHY timestamping
12953a749623SJacob Keller  * @ptp_port: PTP port for which the PHY start is set
12963a749623SJacob Keller  *
12973a749623SJacob Keller  * Start the PHY timestamping block, and initiate Vernier timestamping
12983a749623SJacob Keller  * calibration. If timestamping cannot be calibrated (such as if link is down)
12993a749623SJacob Keller  * then disable the timestamping block instead.
13003a749623SJacob Keller  */
13013a749623SJacob Keller static int
13023a749623SJacob Keller ice_ptp_port_phy_restart(struct ice_ptp_port *ptp_port)
13033a749623SJacob Keller {
13043a749623SJacob Keller 	struct ice_pf *pf = ptp_port_to_pf(ptp_port);
13053a749623SJacob Keller 	u8 port = ptp_port->port_num;
13063a749623SJacob Keller 	struct ice_hw *hw = &pf->hw;
13073a749623SJacob Keller 	int err;
13083a749623SJacob Keller 
13093a749623SJacob Keller 	if (ice_is_e810(hw))
13103a749623SJacob Keller 		return 0;
13113a749623SJacob Keller 
13123a749623SJacob Keller 	if (!ptp_port->link_up)
13133a749623SJacob Keller 		return ice_ptp_port_phy_stop(ptp_port);
13143a749623SJacob Keller 
13153a749623SJacob Keller 	mutex_lock(&ptp_port->ps_lock);
13163a749623SJacob Keller 
1317a69f1cb6SJacob Keller 	kthread_cancel_delayed_work_sync(&ptp_port->ov_work);
1318a69f1cb6SJacob Keller 
13193a749623SJacob Keller 	/* temporarily disable Tx timestamps while calibrating PHY offset */
13203ad5c10bSJacob Keller 	spin_lock(&ptp_port->tx.lock);
13213a749623SJacob Keller 	ptp_port->tx.calibrating = true;
13223ad5c10bSJacob Keller 	spin_unlock(&ptp_port->tx.lock);
1323a69f1cb6SJacob Keller 	ptp_port->tx_fifo_busy_cnt = 0;
13243a749623SJacob Keller 
13250357d5caSMilena Olech 	/* Start the PHY timer in Vernier mode */
13260357d5caSMilena Olech 	err = ice_start_phy_timer_e822(hw, port);
13273a749623SJacob Keller 	if (err)
13283a749623SJacob Keller 		goto out_unlock;
13293a749623SJacob Keller 
13303a749623SJacob Keller 	/* Enable Tx timestamps right away */
13313ad5c10bSJacob Keller 	spin_lock(&ptp_port->tx.lock);
13323a749623SJacob Keller 	ptp_port->tx.calibrating = false;
13333ad5c10bSJacob Keller 	spin_unlock(&ptp_port->tx.lock);
13343a749623SJacob Keller 
1335a69f1cb6SJacob Keller 	kthread_queue_delayed_work(pf->ptp.kworker, &ptp_port->ov_work, 0);
1336a69f1cb6SJacob Keller 
13373a749623SJacob Keller out_unlock:
13383a749623SJacob Keller 	if (err)
13393a749623SJacob Keller 		dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d up, err %d\n",
13403a749623SJacob Keller 			port, err);
13413a749623SJacob Keller 
13423a749623SJacob Keller 	mutex_unlock(&ptp_port->ps_lock);
13433a749623SJacob Keller 
13443a749623SJacob Keller 	return err;
13453a749623SJacob Keller }
13463a749623SJacob Keller 
13473a749623SJacob Keller /**
13486b1ff5d3SJacob Keller  * ice_ptp_link_change - Reconfigure PTP after link status change
13493a749623SJacob Keller  * @pf: Board private structure
13503a749623SJacob Keller  * @port: Port for which the PHY start is set
13513a749623SJacob Keller  * @linkup: Link is up or down
13523a749623SJacob Keller  */
13536b1ff5d3SJacob Keller void ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup)
13543a749623SJacob Keller {
13553a749623SJacob Keller 	struct ice_ptp_port *ptp_port;
13563a749623SJacob Keller 
13576b1ff5d3SJacob Keller 	if (!test_bit(ICE_FLAG_PTP, pf->flags))
13586b1ff5d3SJacob Keller 		return;
13593a749623SJacob Keller 
13606b1ff5d3SJacob Keller 	if (WARN_ON_ONCE(port >= ICE_NUM_EXTERNAL_PORTS))
13616b1ff5d3SJacob Keller 		return;
13623a749623SJacob Keller 
13633a749623SJacob Keller 	ptp_port = &pf->ptp.port;
13646b1ff5d3SJacob Keller 	if (WARN_ON_ONCE(ptp_port->port_num != port))
13656b1ff5d3SJacob Keller 		return;
13663a749623SJacob Keller 
136711722c39SJacob Keller 	/* Update cached link status for this port immediately */
13683a749623SJacob Keller 	ptp_port->link_up = linkup;
13693a749623SJacob Keller 
13706b1ff5d3SJacob Keller 	/* E810 devices do not need to reconfigure the PHY */
13716b1ff5d3SJacob Keller 	if (ice_is_e810(&pf->hw))
13726b1ff5d3SJacob Keller 		return;
13733a749623SJacob Keller 
13746b1ff5d3SJacob Keller 	ice_ptp_port_phy_restart(ptp_port);
13753a749623SJacob Keller }
13763a749623SJacob Keller 
13773a749623SJacob Keller /**
13783a749623SJacob Keller  * ice_ptp_tx_ena_intr - Enable or disable the Tx timestamp interrupt
13793a749623SJacob Keller  * @pf: PF private structure
13803a749623SJacob Keller  * @ena: bool value to enable or disable interrupt
13813a749623SJacob Keller  * @threshold: Minimum number of packets at which intr is triggered
13823a749623SJacob Keller  *
13833a749623SJacob Keller  * Utility function to enable or disable Tx timestamp interrupt and threshold
13843a749623SJacob Keller  */
13853a749623SJacob Keller static int ice_ptp_tx_ena_intr(struct ice_pf *pf, bool ena, u32 threshold)
13863a749623SJacob Keller {
13873a749623SJacob Keller 	struct ice_hw *hw = &pf->hw;
13883a749623SJacob Keller 	int err = 0;
13893a749623SJacob Keller 	int quad;
13903a749623SJacob Keller 	u32 val;
13913a749623SJacob Keller 
1392407b66c0SKarol Kolacinski 	ice_ptp_reset_ts_memory(hw);
13933a749623SJacob Keller 
13943a749623SJacob Keller 	for (quad = 0; quad < ICE_MAX_QUAD; quad++) {
13953a749623SJacob Keller 		err = ice_read_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG,
13963a749623SJacob Keller 					     &val);
13973a749623SJacob Keller 		if (err)
13983a749623SJacob Keller 			break;
13993a749623SJacob Keller 
14003a749623SJacob Keller 		if (ena) {
14013a749623SJacob Keller 			val |= Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M;
14023a749623SJacob Keller 			val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_THR_M;
14033a749623SJacob Keller 			val |= ((threshold << Q_REG_TX_MEM_GBL_CFG_INTR_THR_S) &
14043a749623SJacob Keller 				Q_REG_TX_MEM_GBL_CFG_INTR_THR_M);
14053a749623SJacob Keller 		} else {
14063a749623SJacob Keller 			val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M;
14073a749623SJacob Keller 		}
14083a749623SJacob Keller 
14093a749623SJacob Keller 		err = ice_write_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG,
14103a749623SJacob Keller 					      val);
14113a749623SJacob Keller 		if (err)
14123a749623SJacob Keller 			break;
14133a749623SJacob Keller 	}
14143a749623SJacob Keller 
14153a749623SJacob Keller 	if (err)
14163a749623SJacob Keller 		dev_err(ice_pf_to_dev(pf), "PTP failed in intr ena, err %d\n",
14173a749623SJacob Keller 			err);
14183a749623SJacob Keller 	return err;
14193a749623SJacob Keller }
14203a749623SJacob Keller 
14213a749623SJacob Keller /**
14223a749623SJacob Keller  * ice_ptp_reset_phy_timestamping - Reset PHY timestamping block
14233a749623SJacob Keller  * @pf: Board private structure
14243a749623SJacob Keller  */
14253a749623SJacob Keller static void ice_ptp_reset_phy_timestamping(struct ice_pf *pf)
14263a749623SJacob Keller {
14273a749623SJacob Keller 	ice_ptp_port_phy_restart(&pf->ptp.port);
142878267d0cSJacob Keller }
142978267d0cSJacob Keller 
143078267d0cSJacob Keller /**
143106c16d89SJacob Keller  * ice_ptp_adjfine - Adjust clock increment rate
143206c16d89SJacob Keller  * @info: the driver's PTP info structure
143306c16d89SJacob Keller  * @scaled_ppm: Parts per million with 16-bit fractional field
143406c16d89SJacob Keller  *
143506c16d89SJacob Keller  * Adjust the frequency of the clock by the indicated scaled ppm from the
143606c16d89SJacob Keller  * base frequency.
143706c16d89SJacob Keller  */
143806c16d89SJacob Keller static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
143906c16d89SJacob Keller {
144006c16d89SJacob Keller 	struct ice_pf *pf = ptp_info_to_pf(info);
144106c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
14421060707eSJacob Keller 	u64 incval;
144306c16d89SJacob Keller 	int err;
144406c16d89SJacob Keller 
14451060707eSJacob Keller 	incval = adjust_by_scaled_ppm(ice_base_incval(pf), scaled_ppm);
144606c16d89SJacob Keller 	err = ice_ptp_write_incval_locked(hw, incval);
144706c16d89SJacob Keller 	if (err) {
144806c16d89SJacob Keller 		dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n",
144906c16d89SJacob Keller 			err);
145006c16d89SJacob Keller 		return -EIO;
145106c16d89SJacob Keller 	}
145206c16d89SJacob Keller 
145306c16d89SJacob Keller 	return 0;
145406c16d89SJacob Keller }
145506c16d89SJacob Keller 
145606c16d89SJacob Keller /**
1457172db5f9SMaciej Machnikowski  * ice_ptp_extts_work - Workqueue task function
1458172db5f9SMaciej Machnikowski  * @work: external timestamp work structure
1459172db5f9SMaciej Machnikowski  *
1460172db5f9SMaciej Machnikowski  * Service for PTP external clock event
1461172db5f9SMaciej Machnikowski  */
1462172db5f9SMaciej Machnikowski static void ice_ptp_extts_work(struct kthread_work *work)
1463172db5f9SMaciej Machnikowski {
1464172db5f9SMaciej Machnikowski 	struct ice_ptp *ptp = container_of(work, struct ice_ptp, extts_work);
1465172db5f9SMaciej Machnikowski 	struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp);
1466172db5f9SMaciej Machnikowski 	struct ptp_clock_event event;
1467172db5f9SMaciej Machnikowski 	struct ice_hw *hw = &pf->hw;
1468172db5f9SMaciej Machnikowski 	u8 chan, tmr_idx;
1469172db5f9SMaciej Machnikowski 	u32 hi, lo;
1470172db5f9SMaciej Machnikowski 
1471172db5f9SMaciej Machnikowski 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1472172db5f9SMaciej Machnikowski 	/* Event time is captured by one of the two matched registers
1473172db5f9SMaciej Machnikowski 	 *      GLTSYN_EVNT_L: 32 LSB of sampled time event
1474172db5f9SMaciej Machnikowski 	 *      GLTSYN_EVNT_H: 32 MSB of sampled time event
1475172db5f9SMaciej Machnikowski 	 * Event is defined in GLTSYN_EVNT_0 register
1476172db5f9SMaciej Machnikowski 	 */
1477172db5f9SMaciej Machnikowski 	for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) {
1478172db5f9SMaciej Machnikowski 		/* Check if channel is enabled */
1479172db5f9SMaciej Machnikowski 		if (pf->ptp.ext_ts_irq & (1 << chan)) {
1480172db5f9SMaciej Machnikowski 			lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx));
1481172db5f9SMaciej Machnikowski 			hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx));
1482172db5f9SMaciej Machnikowski 			event.timestamp = (((u64)hi) << 32) | lo;
1483172db5f9SMaciej Machnikowski 			event.type = PTP_CLOCK_EXTTS;
1484172db5f9SMaciej Machnikowski 			event.index = chan;
1485172db5f9SMaciej Machnikowski 
1486172db5f9SMaciej Machnikowski 			/* Fire event */
1487172db5f9SMaciej Machnikowski 			ptp_clock_event(pf->ptp.clock, &event);
1488172db5f9SMaciej Machnikowski 			pf->ptp.ext_ts_irq &= ~(1 << chan);
1489172db5f9SMaciej Machnikowski 		}
1490172db5f9SMaciej Machnikowski 	}
1491172db5f9SMaciej Machnikowski }
1492172db5f9SMaciej Machnikowski 
1493172db5f9SMaciej Machnikowski /**
1494172db5f9SMaciej Machnikowski  * ice_ptp_cfg_extts - Configure EXTTS pin and channel
1495172db5f9SMaciej Machnikowski  * @pf: Board private structure
1496172db5f9SMaciej Machnikowski  * @ena: true to enable; false to disable
1497172db5f9SMaciej Machnikowski  * @chan: GPIO channel (0-3)
1498172db5f9SMaciej Machnikowski  * @gpio_pin: GPIO pin
1499172db5f9SMaciej Machnikowski  * @extts_flags: request flags from the ptp_extts_request.flags
1500172db5f9SMaciej Machnikowski  */
1501172db5f9SMaciej Machnikowski static int
1502172db5f9SMaciej Machnikowski ice_ptp_cfg_extts(struct ice_pf *pf, bool ena, unsigned int chan, u32 gpio_pin,
1503172db5f9SMaciej Machnikowski 		  unsigned int extts_flags)
1504172db5f9SMaciej Machnikowski {
1505172db5f9SMaciej Machnikowski 	u32 func, aux_reg, gpio_reg, irq_reg;
1506172db5f9SMaciej Machnikowski 	struct ice_hw *hw = &pf->hw;
1507172db5f9SMaciej Machnikowski 	u8 tmr_idx;
1508172db5f9SMaciej Machnikowski 
1509172db5f9SMaciej Machnikowski 	if (chan > (unsigned int)pf->ptp.info.n_ext_ts)
1510172db5f9SMaciej Machnikowski 		return -EINVAL;
1511172db5f9SMaciej Machnikowski 
1512172db5f9SMaciej Machnikowski 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1513172db5f9SMaciej Machnikowski 
1514172db5f9SMaciej Machnikowski 	irq_reg = rd32(hw, PFINT_OICR_ENA);
1515172db5f9SMaciej Machnikowski 
1516172db5f9SMaciej Machnikowski 	if (ena) {
1517172db5f9SMaciej Machnikowski 		/* Enable the interrupt */
1518172db5f9SMaciej Machnikowski 		irq_reg |= PFINT_OICR_TSYN_EVNT_M;
1519172db5f9SMaciej Machnikowski 		aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M;
1520172db5f9SMaciej Machnikowski 
1521172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE	BIT(0)
1522172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE	BIT(1)
1523172db5f9SMaciej Machnikowski 
1524172db5f9SMaciej Machnikowski 		/* set event level to requested edge */
1525172db5f9SMaciej Machnikowski 		if (extts_flags & PTP_FALLING_EDGE)
1526172db5f9SMaciej Machnikowski 			aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE;
1527172db5f9SMaciej Machnikowski 		if (extts_flags & PTP_RISING_EDGE)
1528172db5f9SMaciej Machnikowski 			aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE;
1529172db5f9SMaciej Machnikowski 
1530172db5f9SMaciej Machnikowski 		/* Write GPIO CTL reg.
1531172db5f9SMaciej Machnikowski 		 * 0x1 is input sampled by EVENT register(channel)
1532172db5f9SMaciej Machnikowski 		 * + num_in_channels * tmr_idx
1533172db5f9SMaciej Machnikowski 		 */
1534172db5f9SMaciej Machnikowski 		func = 1 + chan + (tmr_idx * 3);
1535172db5f9SMaciej Machnikowski 		gpio_reg = ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) &
1536172db5f9SMaciej Machnikowski 			    GLGEN_GPIO_CTL_PIN_FUNC_M);
1537172db5f9SMaciej Machnikowski 		pf->ptp.ext_ts_chan |= (1 << chan);
1538172db5f9SMaciej Machnikowski 	} else {
1539172db5f9SMaciej Machnikowski 		/* clear the values we set to reset defaults */
1540172db5f9SMaciej Machnikowski 		aux_reg = 0;
1541172db5f9SMaciej Machnikowski 		gpio_reg = 0;
1542172db5f9SMaciej Machnikowski 		pf->ptp.ext_ts_chan &= ~(1 << chan);
1543172db5f9SMaciej Machnikowski 		if (!pf->ptp.ext_ts_chan)
1544172db5f9SMaciej Machnikowski 			irq_reg &= ~PFINT_OICR_TSYN_EVNT_M;
1545172db5f9SMaciej Machnikowski 	}
1546172db5f9SMaciej Machnikowski 
1547172db5f9SMaciej Machnikowski 	wr32(hw, PFINT_OICR_ENA, irq_reg);
1548172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg);
1549172db5f9SMaciej Machnikowski 	wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg);
1550172db5f9SMaciej Machnikowski 
1551172db5f9SMaciej Machnikowski 	return 0;
1552172db5f9SMaciej Machnikowski }
1553172db5f9SMaciej Machnikowski 
1554172db5f9SMaciej Machnikowski /**
1555172db5f9SMaciej Machnikowski  * ice_ptp_cfg_clkout - Configure clock to generate periodic wave
1556172db5f9SMaciej Machnikowski  * @pf: Board private structure
1557172db5f9SMaciej Machnikowski  * @chan: GPIO channel (0-3)
1558172db5f9SMaciej Machnikowski  * @config: desired periodic clk configuration. NULL will disable channel
1559172db5f9SMaciej Machnikowski  * @store: If set to true the values will be stored
1560172db5f9SMaciej Machnikowski  *
1561172db5f9SMaciej Machnikowski  * Configure the internal clock generator modules to generate the clock wave of
1562172db5f9SMaciej Machnikowski  * specified period.
1563172db5f9SMaciej Machnikowski  */
1564172db5f9SMaciej Machnikowski static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan,
1565172db5f9SMaciej Machnikowski 			      struct ice_perout_channel *config, bool store)
1566172db5f9SMaciej Machnikowski {
1567172db5f9SMaciej Machnikowski 	u64 current_time, period, start_time, phase;
1568172db5f9SMaciej Machnikowski 	struct ice_hw *hw = &pf->hw;
1569172db5f9SMaciej Machnikowski 	u32 func, val, gpio_pin;
1570172db5f9SMaciej Machnikowski 	u8 tmr_idx;
1571172db5f9SMaciej Machnikowski 
1572172db5f9SMaciej Machnikowski 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1573172db5f9SMaciej Machnikowski 
1574172db5f9SMaciej Machnikowski 	/* 0. Reset mode & out_en in AUX_OUT */
1575172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0);
1576172db5f9SMaciej Machnikowski 
1577172db5f9SMaciej Machnikowski 	/* If we're disabling the output, clear out CLKO and TGT and keep
1578172db5f9SMaciej Machnikowski 	 * output level low
1579172db5f9SMaciej Machnikowski 	 */
1580172db5f9SMaciej Machnikowski 	if (!config || !config->ena) {
1581172db5f9SMaciej Machnikowski 		wr32(hw, GLTSYN_CLKO(chan, tmr_idx), 0);
1582172db5f9SMaciej Machnikowski 		wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), 0);
1583172db5f9SMaciej Machnikowski 		wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), 0);
1584172db5f9SMaciej Machnikowski 
1585172db5f9SMaciej Machnikowski 		val = GLGEN_GPIO_CTL_PIN_DIR_M;
1586172db5f9SMaciej Machnikowski 		gpio_pin = pf->ptp.perout_channels[chan].gpio_pin;
1587172db5f9SMaciej Machnikowski 		wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
1588172db5f9SMaciej Machnikowski 
1589172db5f9SMaciej Machnikowski 		/* Store the value if requested */
1590172db5f9SMaciej Machnikowski 		if (store)
1591172db5f9SMaciej Machnikowski 			memset(&pf->ptp.perout_channels[chan], 0,
1592172db5f9SMaciej Machnikowski 			       sizeof(struct ice_perout_channel));
1593172db5f9SMaciej Machnikowski 
1594172db5f9SMaciej Machnikowski 		return 0;
1595172db5f9SMaciej Machnikowski 	}
1596172db5f9SMaciej Machnikowski 	period = config->period;
1597172db5f9SMaciej Machnikowski 	start_time = config->start_time;
1598172db5f9SMaciej Machnikowski 	div64_u64_rem(start_time, period, &phase);
1599172db5f9SMaciej Machnikowski 	gpio_pin = config->gpio_pin;
1600172db5f9SMaciej Machnikowski 
1601172db5f9SMaciej Machnikowski 	/* 1. Write clkout with half of required period value */
1602172db5f9SMaciej Machnikowski 	if (period & 0x1) {
1603172db5f9SMaciej Machnikowski 		dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n");
1604172db5f9SMaciej Machnikowski 		goto err;
1605172db5f9SMaciej Machnikowski 	}
1606172db5f9SMaciej Machnikowski 
1607172db5f9SMaciej Machnikowski 	period >>= 1;
1608172db5f9SMaciej Machnikowski 
1609172db5f9SMaciej Machnikowski 	/* For proper operation, the GLTSYN_CLKO must be larger than clock tick
1610172db5f9SMaciej Machnikowski 	 */
1611172db5f9SMaciej Machnikowski #define MIN_PULSE 3
1612172db5f9SMaciej Machnikowski 	if (period <= MIN_PULSE || period > U32_MAX) {
1613172db5f9SMaciej Machnikowski 		dev_err(ice_pf_to_dev(pf), "CLK Period must be > %d && < 2^33",
1614172db5f9SMaciej Machnikowski 			MIN_PULSE * 2);
1615172db5f9SMaciej Machnikowski 		goto err;
1616172db5f9SMaciej Machnikowski 	}
1617172db5f9SMaciej Machnikowski 
1618172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period));
1619172db5f9SMaciej Machnikowski 
1620172db5f9SMaciej Machnikowski 	/* Allow time for programming before start_time is hit */
1621172db5f9SMaciej Machnikowski 	current_time = ice_ptp_read_src_clk_reg(pf, NULL);
1622172db5f9SMaciej Machnikowski 
1623172db5f9SMaciej Machnikowski 	/* if start time is in the past start the timer at the nearest second
1624172db5f9SMaciej Machnikowski 	 * maintaining phase
1625172db5f9SMaciej Machnikowski 	 */
1626172db5f9SMaciej Machnikowski 	if (start_time < current_time)
16275f773519SMaciej Machnikowski 		start_time = div64_u64(current_time + NSEC_PER_SEC - 1,
1628172db5f9SMaciej Machnikowski 				       NSEC_PER_SEC) * NSEC_PER_SEC + phase;
1629172db5f9SMaciej Machnikowski 
16303a749623SJacob Keller 	if (ice_is_e810(hw))
1631172db5f9SMaciej Machnikowski 		start_time -= E810_OUT_PROP_DELAY_NS;
16323a749623SJacob Keller 	else
16333a749623SJacob Keller 		start_time -= ice_e822_pps_delay(ice_e822_time_ref(hw));
1634172db5f9SMaciej Machnikowski 
1635172db5f9SMaciej Machnikowski 	/* 2. Write TARGET time */
1636172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start_time));
1637172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start_time));
1638172db5f9SMaciej Machnikowski 
1639172db5f9SMaciej Machnikowski 	/* 3. Write AUX_OUT register */
1640172db5f9SMaciej Machnikowski 	val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M;
1641172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val);
1642172db5f9SMaciej Machnikowski 
1643172db5f9SMaciej Machnikowski 	/* 4. write GPIO CTL reg */
1644172db5f9SMaciej Machnikowski 	func = 8 + chan + (tmr_idx * 4);
1645172db5f9SMaciej Machnikowski 	val = GLGEN_GPIO_CTL_PIN_DIR_M |
1646172db5f9SMaciej Machnikowski 	      ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & GLGEN_GPIO_CTL_PIN_FUNC_M);
1647172db5f9SMaciej Machnikowski 	wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
1648172db5f9SMaciej Machnikowski 
1649172db5f9SMaciej Machnikowski 	/* Store the value if requested */
1650172db5f9SMaciej Machnikowski 	if (store) {
1651172db5f9SMaciej Machnikowski 		memcpy(&pf->ptp.perout_channels[chan], config,
1652172db5f9SMaciej Machnikowski 		       sizeof(struct ice_perout_channel));
1653172db5f9SMaciej Machnikowski 		pf->ptp.perout_channels[chan].start_time = phase;
1654172db5f9SMaciej Machnikowski 	}
1655172db5f9SMaciej Machnikowski 
1656172db5f9SMaciej Machnikowski 	return 0;
1657172db5f9SMaciej Machnikowski err:
1658172db5f9SMaciej Machnikowski 	dev_err(ice_pf_to_dev(pf), "PTP failed to cfg per_clk\n");
1659172db5f9SMaciej Machnikowski 	return -EFAULT;
1660172db5f9SMaciej Machnikowski }
1661172db5f9SMaciej Machnikowski 
1662172db5f9SMaciej Machnikowski /**
16639ee31343SJacob Keller  * ice_ptp_disable_all_clkout - Disable all currently configured outputs
16649ee31343SJacob Keller  * @pf: pointer to the PF structure
16659ee31343SJacob Keller  *
16669ee31343SJacob Keller  * Disable all currently configured clock outputs. This is necessary before
16679ee31343SJacob Keller  * certain changes to the PTP hardware clock. Use ice_ptp_enable_all_clkout to
16689ee31343SJacob Keller  * re-enable the clocks again.
16699ee31343SJacob Keller  */
16709ee31343SJacob Keller static void ice_ptp_disable_all_clkout(struct ice_pf *pf)
16719ee31343SJacob Keller {
16729ee31343SJacob Keller 	uint i;
16739ee31343SJacob Keller 
16749ee31343SJacob Keller 	for (i = 0; i < pf->ptp.info.n_per_out; i++)
16759ee31343SJacob Keller 		if (pf->ptp.perout_channels[i].ena)
16769ee31343SJacob Keller 			ice_ptp_cfg_clkout(pf, i, NULL, false);
16779ee31343SJacob Keller }
16789ee31343SJacob Keller 
16799ee31343SJacob Keller /**
16809ee31343SJacob Keller  * ice_ptp_enable_all_clkout - Enable all configured periodic clock outputs
16819ee31343SJacob Keller  * @pf: pointer to the PF structure
16829ee31343SJacob Keller  *
16839ee31343SJacob Keller  * Enable all currently configured clock outputs. Use this after
16849ee31343SJacob Keller  * ice_ptp_disable_all_clkout to reconfigure the output signals according to
16859ee31343SJacob Keller  * their configuration.
16869ee31343SJacob Keller  */
16879ee31343SJacob Keller static void ice_ptp_enable_all_clkout(struct ice_pf *pf)
16889ee31343SJacob Keller {
16899ee31343SJacob Keller 	uint i;
16909ee31343SJacob Keller 
16919ee31343SJacob Keller 	for (i = 0; i < pf->ptp.info.n_per_out; i++)
16929ee31343SJacob Keller 		if (pf->ptp.perout_channels[i].ena)
16939ee31343SJacob Keller 			ice_ptp_cfg_clkout(pf, i, &pf->ptp.perout_channels[i],
16949ee31343SJacob Keller 					   false);
16959ee31343SJacob Keller }
16969ee31343SJacob Keller 
16979ee31343SJacob Keller /**
1698172db5f9SMaciej Machnikowski  * ice_ptp_gpio_enable_e810 - Enable/disable ancillary features of PHC
1699172db5f9SMaciej Machnikowski  * @info: the driver's PTP info structure
1700172db5f9SMaciej Machnikowski  * @rq: The requested feature to change
1701172db5f9SMaciej Machnikowski  * @on: Enable/disable flag
1702172db5f9SMaciej Machnikowski  */
1703172db5f9SMaciej Machnikowski static int
1704172db5f9SMaciej Machnikowski ice_ptp_gpio_enable_e810(struct ptp_clock_info *info,
1705172db5f9SMaciej Machnikowski 			 struct ptp_clock_request *rq, int on)
1706172db5f9SMaciej Machnikowski {
1707172db5f9SMaciej Machnikowski 	struct ice_pf *pf = ptp_info_to_pf(info);
1708172db5f9SMaciej Machnikowski 	struct ice_perout_channel clk_cfg = {0};
1709325b2064SMaciej Machnikowski 	bool sma_pres = false;
1710172db5f9SMaciej Machnikowski 	unsigned int chan;
1711172db5f9SMaciej Machnikowski 	u32 gpio_pin;
1712172db5f9SMaciej Machnikowski 	int err;
1713172db5f9SMaciej Machnikowski 
1714325b2064SMaciej Machnikowski 	if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL))
1715325b2064SMaciej Machnikowski 		sma_pres = true;
1716325b2064SMaciej Machnikowski 
1717172db5f9SMaciej Machnikowski 	switch (rq->type) {
1718172db5f9SMaciej Machnikowski 	case PTP_CLK_REQ_PEROUT:
1719172db5f9SMaciej Machnikowski 		chan = rq->perout.index;
1720325b2064SMaciej Machnikowski 		if (sma_pres) {
1721325b2064SMaciej Machnikowski 			if (chan == ice_pin_desc_e810t[SMA1].chan)
1722325b2064SMaciej Machnikowski 				clk_cfg.gpio_pin = GPIO_20;
1723325b2064SMaciej Machnikowski 			else if (chan == ice_pin_desc_e810t[SMA2].chan)
1724325b2064SMaciej Machnikowski 				clk_cfg.gpio_pin = GPIO_22;
1725172db5f9SMaciej Machnikowski 			else
1726325b2064SMaciej Machnikowski 				return -1;
1727325b2064SMaciej Machnikowski 		} else if (ice_is_e810t(&pf->hw)) {
1728325b2064SMaciej Machnikowski 			if (chan == 0)
1729325b2064SMaciej Machnikowski 				clk_cfg.gpio_pin = GPIO_20;
1730325b2064SMaciej Machnikowski 			else
1731325b2064SMaciej Machnikowski 				clk_cfg.gpio_pin = GPIO_22;
1732325b2064SMaciej Machnikowski 		} else if (chan == PPS_CLK_GEN_CHAN) {
1733325b2064SMaciej Machnikowski 			clk_cfg.gpio_pin = PPS_PIN_INDEX;
1734325b2064SMaciej Machnikowski 		} else {
1735172db5f9SMaciej Machnikowski 			clk_cfg.gpio_pin = chan;
1736325b2064SMaciej Machnikowski 		}
1737172db5f9SMaciej Machnikowski 
1738172db5f9SMaciej Machnikowski 		clk_cfg.period = ((rq->perout.period.sec * NSEC_PER_SEC) +
1739172db5f9SMaciej Machnikowski 				   rq->perout.period.nsec);
1740172db5f9SMaciej Machnikowski 		clk_cfg.start_time = ((rq->perout.start.sec * NSEC_PER_SEC) +
1741172db5f9SMaciej Machnikowski 				       rq->perout.start.nsec);
1742172db5f9SMaciej Machnikowski 		clk_cfg.ena = !!on;
1743172db5f9SMaciej Machnikowski 
1744172db5f9SMaciej Machnikowski 		err = ice_ptp_cfg_clkout(pf, chan, &clk_cfg, true);
1745172db5f9SMaciej Machnikowski 		break;
1746172db5f9SMaciej Machnikowski 	case PTP_CLK_REQ_EXTTS:
1747172db5f9SMaciej Machnikowski 		chan = rq->extts.index;
1748325b2064SMaciej Machnikowski 		if (sma_pres) {
1749325b2064SMaciej Machnikowski 			if (chan < ice_pin_desc_e810t[SMA2].chan)
1750325b2064SMaciej Machnikowski 				gpio_pin = GPIO_21;
1751325b2064SMaciej Machnikowski 			else
1752325b2064SMaciej Machnikowski 				gpio_pin = GPIO_23;
1753325b2064SMaciej Machnikowski 		} else if (ice_is_e810t(&pf->hw)) {
1754325b2064SMaciej Machnikowski 			if (chan == 0)
1755325b2064SMaciej Machnikowski 				gpio_pin = GPIO_21;
1756325b2064SMaciej Machnikowski 			else
1757325b2064SMaciej Machnikowski 				gpio_pin = GPIO_23;
1758325b2064SMaciej Machnikowski 		} else {
1759172db5f9SMaciej Machnikowski 			gpio_pin = chan;
1760325b2064SMaciej Machnikowski 		}
1761172db5f9SMaciej Machnikowski 
1762172db5f9SMaciej Machnikowski 		err = ice_ptp_cfg_extts(pf, !!on, chan, gpio_pin,
1763172db5f9SMaciej Machnikowski 					rq->extts.flags);
1764172db5f9SMaciej Machnikowski 		break;
1765172db5f9SMaciej Machnikowski 	default:
1766172db5f9SMaciej Machnikowski 		return -EOPNOTSUPP;
1767172db5f9SMaciej Machnikowski 	}
1768172db5f9SMaciej Machnikowski 
1769172db5f9SMaciej Machnikowski 	return err;
1770172db5f9SMaciej Machnikowski }
1771172db5f9SMaciej Machnikowski 
1772172db5f9SMaciej Machnikowski /**
1773*634d841dSKarol Kolacinski  * ice_ptp_gpio_enable_e823 - Enable/disable ancillary features of PHC
1774*634d841dSKarol Kolacinski  * @info: the driver's PTP info structure
1775*634d841dSKarol Kolacinski  * @rq: The requested feature to change
1776*634d841dSKarol Kolacinski  * @on: Enable/disable flag
1777*634d841dSKarol Kolacinski  */
1778*634d841dSKarol Kolacinski static int ice_ptp_gpio_enable_e823(struct ptp_clock_info *info,
1779*634d841dSKarol Kolacinski 				    struct ptp_clock_request *rq, int on)
1780*634d841dSKarol Kolacinski {
1781*634d841dSKarol Kolacinski 	struct ice_pf *pf = ptp_info_to_pf(info);
1782*634d841dSKarol Kolacinski 	struct ice_perout_channel clk_cfg = {0};
1783*634d841dSKarol Kolacinski 	int err;
1784*634d841dSKarol Kolacinski 
1785*634d841dSKarol Kolacinski 	switch (rq->type) {
1786*634d841dSKarol Kolacinski 	case PTP_CLK_REQ_PPS:
1787*634d841dSKarol Kolacinski 		clk_cfg.gpio_pin = PPS_PIN_INDEX;
1788*634d841dSKarol Kolacinski 		clk_cfg.period = NSEC_PER_SEC;
1789*634d841dSKarol Kolacinski 		clk_cfg.ena = !!on;
1790*634d841dSKarol Kolacinski 
1791*634d841dSKarol Kolacinski 		err = ice_ptp_cfg_clkout(pf, PPS_CLK_GEN_CHAN, &clk_cfg, true);
1792*634d841dSKarol Kolacinski 		break;
1793*634d841dSKarol Kolacinski 	case PTP_CLK_REQ_EXTTS:
1794*634d841dSKarol Kolacinski 		err = ice_ptp_cfg_extts(pf, !!on, rq->extts.index,
1795*634d841dSKarol Kolacinski 					TIME_SYNC_PIN_INDEX, rq->extts.flags);
1796*634d841dSKarol Kolacinski 		break;
1797*634d841dSKarol Kolacinski 	default:
1798*634d841dSKarol Kolacinski 		return -EOPNOTSUPP;
1799*634d841dSKarol Kolacinski 	}
1800*634d841dSKarol Kolacinski 
1801*634d841dSKarol Kolacinski 	return err;
1802*634d841dSKarol Kolacinski }
1803*634d841dSKarol Kolacinski 
1804*634d841dSKarol Kolacinski /**
180506c16d89SJacob Keller  * ice_ptp_gettimex64 - Get the time of the clock
180606c16d89SJacob Keller  * @info: the driver's PTP info structure
180706c16d89SJacob Keller  * @ts: timespec64 structure to hold the current time value
180806c16d89SJacob Keller  * @sts: Optional parameter for holding a pair of system timestamps from
180906c16d89SJacob Keller  *       the system clock. Will be ignored if NULL is given.
181006c16d89SJacob Keller  *
181106c16d89SJacob Keller  * Read the device clock and return the correct value on ns, after converting it
181206c16d89SJacob Keller  * into a timespec struct.
181306c16d89SJacob Keller  */
181406c16d89SJacob Keller static int
181506c16d89SJacob Keller ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts,
181606c16d89SJacob Keller 		   struct ptp_system_timestamp *sts)
181706c16d89SJacob Keller {
181806c16d89SJacob Keller 	struct ice_pf *pf = ptp_info_to_pf(info);
181906c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
182006c16d89SJacob Keller 
182106c16d89SJacob Keller 	if (!ice_ptp_lock(hw)) {
182206c16d89SJacob Keller 		dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n");
182306c16d89SJacob Keller 		return -EBUSY;
182406c16d89SJacob Keller 	}
182506c16d89SJacob Keller 
182606c16d89SJacob Keller 	ice_ptp_read_time(pf, ts, sts);
182706c16d89SJacob Keller 	ice_ptp_unlock(hw);
182806c16d89SJacob Keller 
182906c16d89SJacob Keller 	return 0;
183006c16d89SJacob Keller }
183106c16d89SJacob Keller 
183206c16d89SJacob Keller /**
183306c16d89SJacob Keller  * ice_ptp_settime64 - Set the time of the clock
183406c16d89SJacob Keller  * @info: the driver's PTP info structure
183506c16d89SJacob Keller  * @ts: timespec64 structure that holds the new time value
183606c16d89SJacob Keller  *
183706c16d89SJacob Keller  * Set the device clock to the user input value. The conversion from timespec
183806c16d89SJacob Keller  * to ns happens in the write function.
183906c16d89SJacob Keller  */
184006c16d89SJacob Keller static int
184106c16d89SJacob Keller ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts)
184206c16d89SJacob Keller {
184306c16d89SJacob Keller 	struct ice_pf *pf = ptp_info_to_pf(info);
184406c16d89SJacob Keller 	struct timespec64 ts64 = *ts;
184506c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
184606c16d89SJacob Keller 	int err;
184706c16d89SJacob Keller 
18483a749623SJacob Keller 	/* For Vernier mode, we need to recalibrate after new settime
18493a749623SJacob Keller 	 * Start with disabling timestamp block
18503a749623SJacob Keller 	 */
18513a749623SJacob Keller 	if (pf->ptp.port.link_up)
18523a749623SJacob Keller 		ice_ptp_port_phy_stop(&pf->ptp.port);
18533a749623SJacob Keller 
185406c16d89SJacob Keller 	if (!ice_ptp_lock(hw)) {
185506c16d89SJacob Keller 		err = -EBUSY;
185606c16d89SJacob Keller 		goto exit;
185706c16d89SJacob Keller 	}
185806c16d89SJacob Keller 
18599ee31343SJacob Keller 	/* Disable periodic outputs */
18609ee31343SJacob Keller 	ice_ptp_disable_all_clkout(pf);
18619ee31343SJacob Keller 
186206c16d89SJacob Keller 	err = ice_ptp_write_init(pf, &ts64);
186306c16d89SJacob Keller 	ice_ptp_unlock(hw);
186406c16d89SJacob Keller 
186577a78115SJacob Keller 	if (!err)
1866b1a582e6SJacob Keller 		ice_ptp_reset_cached_phctime(pf);
186777a78115SJacob Keller 
18689ee31343SJacob Keller 	/* Reenable periodic outputs */
18699ee31343SJacob Keller 	ice_ptp_enable_all_clkout(pf);
18703a749623SJacob Keller 
18713a749623SJacob Keller 	/* Recalibrate and re-enable timestamp block */
18723a749623SJacob Keller 	if (pf->ptp.port.link_up)
18733a749623SJacob Keller 		ice_ptp_port_phy_restart(&pf->ptp.port);
187406c16d89SJacob Keller exit:
187506c16d89SJacob Keller 	if (err) {
187606c16d89SJacob Keller 		dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err);
187706c16d89SJacob Keller 		return err;
187806c16d89SJacob Keller 	}
187906c16d89SJacob Keller 
188006c16d89SJacob Keller 	return 0;
188106c16d89SJacob Keller }
188206c16d89SJacob Keller 
188306c16d89SJacob Keller /**
188406c16d89SJacob Keller  * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment
188506c16d89SJacob Keller  * @info: the driver's PTP info structure
188606c16d89SJacob Keller  * @delta: Offset in nanoseconds to adjust the time by
188706c16d89SJacob Keller  */
188806c16d89SJacob Keller static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta)
188906c16d89SJacob Keller {
189006c16d89SJacob Keller 	struct timespec64 now, then;
1891ed22d9c8STom Rix 	int ret;
189206c16d89SJacob Keller 
189306c16d89SJacob Keller 	then = ns_to_timespec64(delta);
1894ed22d9c8STom Rix 	ret = ice_ptp_gettimex64(info, &now, NULL);
1895ed22d9c8STom Rix 	if (ret)
1896ed22d9c8STom Rix 		return ret;
189706c16d89SJacob Keller 	now = timespec64_add(now, then);
189806c16d89SJacob Keller 
189906c16d89SJacob Keller 	return ice_ptp_settime64(info, (const struct timespec64 *)&now);
190006c16d89SJacob Keller }
190106c16d89SJacob Keller 
190206c16d89SJacob Keller /**
190306c16d89SJacob Keller  * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta
190406c16d89SJacob Keller  * @info: the driver's PTP info structure
190506c16d89SJacob Keller  * @delta: Offset in nanoseconds to adjust the time by
190606c16d89SJacob Keller  */
190706c16d89SJacob Keller static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
190806c16d89SJacob Keller {
190906c16d89SJacob Keller 	struct ice_pf *pf = ptp_info_to_pf(info);
191006c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
191106c16d89SJacob Keller 	struct device *dev;
191206c16d89SJacob Keller 	int err;
191306c16d89SJacob Keller 
191406c16d89SJacob Keller 	dev = ice_pf_to_dev(pf);
191506c16d89SJacob Keller 
191606c16d89SJacob Keller 	/* Hardware only supports atomic adjustments using signed 32-bit
191706c16d89SJacob Keller 	 * integers. For any adjustment outside this range, perform
191806c16d89SJacob Keller 	 * a non-atomic get->adjust->set flow.
191906c16d89SJacob Keller 	 */
192006c16d89SJacob Keller 	if (delta > S32_MAX || delta < S32_MIN) {
192106c16d89SJacob Keller 		dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta);
192206c16d89SJacob Keller 		return ice_ptp_adjtime_nonatomic(info, delta);
192306c16d89SJacob Keller 	}
192406c16d89SJacob Keller 
192506c16d89SJacob Keller 	if (!ice_ptp_lock(hw)) {
192606c16d89SJacob Keller 		dev_err(dev, "PTP failed to acquire semaphore in adjtime\n");
192706c16d89SJacob Keller 		return -EBUSY;
192806c16d89SJacob Keller 	}
192906c16d89SJacob Keller 
19309ee31343SJacob Keller 	/* Disable periodic outputs */
19319ee31343SJacob Keller 	ice_ptp_disable_all_clkout(pf);
19329ee31343SJacob Keller 
193306c16d89SJacob Keller 	err = ice_ptp_write_adj(pf, delta);
193406c16d89SJacob Keller 
19359ee31343SJacob Keller 	/* Reenable periodic outputs */
19369ee31343SJacob Keller 	ice_ptp_enable_all_clkout(pf);
19379ee31343SJacob Keller 
193806c16d89SJacob Keller 	ice_ptp_unlock(hw);
193906c16d89SJacob Keller 
194006c16d89SJacob Keller 	if (err) {
194106c16d89SJacob Keller 		dev_err(dev, "PTP failed to adjust time, err %d\n", err);
194206c16d89SJacob Keller 		return err;
194306c16d89SJacob Keller 	}
194406c16d89SJacob Keller 
1945b1a582e6SJacob Keller 	ice_ptp_reset_cached_phctime(pf);
194677a78115SJacob Keller 
194706c16d89SJacob Keller 	return 0;
194806c16d89SJacob Keller }
194906c16d89SJacob Keller 
195013a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS
195113a64f0bSJacob Keller /**
195213a64f0bSJacob Keller  * ice_ptp_get_syncdevicetime - Get the cross time stamp info
195313a64f0bSJacob Keller  * @device: Current device time
195413a64f0bSJacob Keller  * @system: System counter value read synchronously with device time
195513a64f0bSJacob Keller  * @ctx: Context provided by timekeeping code
195613a64f0bSJacob Keller  *
195713a64f0bSJacob Keller  * Read device and system (ART) clock simultaneously and return the corrected
195813a64f0bSJacob Keller  * clock values in ns.
195913a64f0bSJacob Keller  */
196013a64f0bSJacob Keller static int
196113a64f0bSJacob Keller ice_ptp_get_syncdevicetime(ktime_t *device,
196213a64f0bSJacob Keller 			   struct system_counterval_t *system,
196313a64f0bSJacob Keller 			   void *ctx)
196413a64f0bSJacob Keller {
196513a64f0bSJacob Keller 	struct ice_pf *pf = (struct ice_pf *)ctx;
196613a64f0bSJacob Keller 	struct ice_hw *hw = &pf->hw;
196713a64f0bSJacob Keller 	u32 hh_lock, hh_art_ctl;
196813a64f0bSJacob Keller 	int i;
196913a64f0bSJacob Keller 
197013a64f0bSJacob Keller 	/* Get the HW lock */
197113a64f0bSJacob Keller 	hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
197213a64f0bSJacob Keller 	if (hh_lock & PFHH_SEM_BUSY_M) {
197313a64f0bSJacob Keller 		dev_err(ice_pf_to_dev(pf), "PTP failed to get hh lock\n");
197413a64f0bSJacob Keller 		return -EFAULT;
197513a64f0bSJacob Keller 	}
197613a64f0bSJacob Keller 
197713a64f0bSJacob Keller 	/* Start the ART and device clock sync sequence */
197813a64f0bSJacob Keller 	hh_art_ctl = rd32(hw, GLHH_ART_CTL);
197913a64f0bSJacob Keller 	hh_art_ctl = hh_art_ctl | GLHH_ART_CTL_ACTIVE_M;
198013a64f0bSJacob Keller 	wr32(hw, GLHH_ART_CTL, hh_art_ctl);
198113a64f0bSJacob Keller 
198213a64f0bSJacob Keller #define MAX_HH_LOCK_TRIES 100
198313a64f0bSJacob Keller 
198413a64f0bSJacob Keller 	for (i = 0; i < MAX_HH_LOCK_TRIES; i++) {
198513a64f0bSJacob Keller 		/* Wait for sync to complete */
198613a64f0bSJacob Keller 		hh_art_ctl = rd32(hw, GLHH_ART_CTL);
198713a64f0bSJacob Keller 		if (hh_art_ctl & GLHH_ART_CTL_ACTIVE_M) {
198813a64f0bSJacob Keller 			udelay(1);
198913a64f0bSJacob Keller 			continue;
199013a64f0bSJacob Keller 		} else {
199113a64f0bSJacob Keller 			u32 hh_ts_lo, hh_ts_hi, tmr_idx;
199213a64f0bSJacob Keller 			u64 hh_ts;
199313a64f0bSJacob Keller 
199413a64f0bSJacob Keller 			tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc;
199513a64f0bSJacob Keller 			/* Read ART time */
199613a64f0bSJacob Keller 			hh_ts_lo = rd32(hw, GLHH_ART_TIME_L);
199713a64f0bSJacob Keller 			hh_ts_hi = rd32(hw, GLHH_ART_TIME_H);
199813a64f0bSJacob Keller 			hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo;
199913a64f0bSJacob Keller 			*system = convert_art_ns_to_tsc(hh_ts);
200013a64f0bSJacob Keller 			/* Read Device source clock time */
200113a64f0bSJacob Keller 			hh_ts_lo = rd32(hw, GLTSYN_HHTIME_L(tmr_idx));
200213a64f0bSJacob Keller 			hh_ts_hi = rd32(hw, GLTSYN_HHTIME_H(tmr_idx));
200313a64f0bSJacob Keller 			hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo;
200413a64f0bSJacob Keller 			*device = ns_to_ktime(hh_ts);
200513a64f0bSJacob Keller 			break;
200613a64f0bSJacob Keller 		}
200713a64f0bSJacob Keller 	}
200813a64f0bSJacob Keller 	/* Release HW lock */
200913a64f0bSJacob Keller 	hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
201013a64f0bSJacob Keller 	hh_lock = hh_lock & ~PFHH_SEM_BUSY_M;
201113a64f0bSJacob Keller 	wr32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), hh_lock);
201213a64f0bSJacob Keller 
201313a64f0bSJacob Keller 	if (i == MAX_HH_LOCK_TRIES)
201413a64f0bSJacob Keller 		return -ETIMEDOUT;
201513a64f0bSJacob Keller 
201613a64f0bSJacob Keller 	return 0;
201713a64f0bSJacob Keller }
201813a64f0bSJacob Keller 
201913a64f0bSJacob Keller /**
202013a64f0bSJacob Keller  * ice_ptp_getcrosststamp_e822 - Capture a device cross timestamp
202113a64f0bSJacob Keller  * @info: the driver's PTP info structure
202213a64f0bSJacob Keller  * @cts: The memory to fill the cross timestamp info
202313a64f0bSJacob Keller  *
202413a64f0bSJacob Keller  * Capture a cross timestamp between the ART and the device PTP hardware
202513a64f0bSJacob Keller  * clock. Fill the cross timestamp information and report it back to the
202613a64f0bSJacob Keller  * caller.
202713a64f0bSJacob Keller  *
202813a64f0bSJacob Keller  * This is only valid for E822 devices which have support for generating the
202913a64f0bSJacob Keller  * cross timestamp via PCIe PTM.
203013a64f0bSJacob Keller  *
203113a64f0bSJacob Keller  * In order to correctly correlate the ART timestamp back to the TSC time, the
203213a64f0bSJacob Keller  * CPU must have X86_FEATURE_TSC_KNOWN_FREQ.
203313a64f0bSJacob Keller  */
203413a64f0bSJacob Keller static int
203513a64f0bSJacob Keller ice_ptp_getcrosststamp_e822(struct ptp_clock_info *info,
203613a64f0bSJacob Keller 			    struct system_device_crosststamp *cts)
203713a64f0bSJacob Keller {
203813a64f0bSJacob Keller 	struct ice_pf *pf = ptp_info_to_pf(info);
203913a64f0bSJacob Keller 
204013a64f0bSJacob Keller 	return get_device_system_crosststamp(ice_ptp_get_syncdevicetime,
204113a64f0bSJacob Keller 					     pf, NULL, cts);
204213a64f0bSJacob Keller }
204313a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */
204413a64f0bSJacob Keller 
204506c16d89SJacob Keller /**
204677a78115SJacob Keller  * ice_ptp_get_ts_config - ioctl interface to read the timestamping config
204777a78115SJacob Keller  * @pf: Board private structure
204877a78115SJacob Keller  * @ifr: ioctl data
204977a78115SJacob Keller  *
205077a78115SJacob Keller  * Copy the timestamping config to user buffer
205177a78115SJacob Keller  */
205277a78115SJacob Keller int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
205377a78115SJacob Keller {
205477a78115SJacob Keller 	struct hwtstamp_config *config;
205577a78115SJacob Keller 
205677a78115SJacob Keller 	if (!test_bit(ICE_FLAG_PTP, pf->flags))
205777a78115SJacob Keller 		return -EIO;
205877a78115SJacob Keller 
205977a78115SJacob Keller 	config = &pf->ptp.tstamp_config;
206077a78115SJacob Keller 
206177a78115SJacob Keller 	return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
206277a78115SJacob Keller 		-EFAULT : 0;
206377a78115SJacob Keller }
206477a78115SJacob Keller 
206577a78115SJacob Keller /**
206677a78115SJacob Keller  * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode
206777a78115SJacob Keller  * @pf: Board private structure
206877a78115SJacob Keller  * @config: hwtstamp settings requested or saved
206977a78115SJacob Keller  */
207077a78115SJacob Keller static int
207177a78115SJacob Keller ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config)
207277a78115SJacob Keller {
207377a78115SJacob Keller 	switch (config->tx_type) {
207477a78115SJacob Keller 	case HWTSTAMP_TX_OFF:
2075ea9b847cSJacob Keller 		ice_set_tx_tstamp(pf, false);
2076ea9b847cSJacob Keller 		break;
2077ea9b847cSJacob Keller 	case HWTSTAMP_TX_ON:
2078ea9b847cSJacob Keller 		ice_set_tx_tstamp(pf, true);
207977a78115SJacob Keller 		break;
208077a78115SJacob Keller 	default:
208177a78115SJacob Keller 		return -ERANGE;
208277a78115SJacob Keller 	}
208377a78115SJacob Keller 
208477a78115SJacob Keller 	switch (config->rx_filter) {
208577a78115SJacob Keller 	case HWTSTAMP_FILTER_NONE:
208677a78115SJacob Keller 		ice_set_rx_tstamp(pf, false);
208777a78115SJacob Keller 		break;
208877a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
208977a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
209077a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
209177a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
209277a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
209377a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
209477a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
209577a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
209677a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
209777a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
209877a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
209977a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
210077a78115SJacob Keller 	case HWTSTAMP_FILTER_NTP_ALL:
210177a78115SJacob Keller 	case HWTSTAMP_FILTER_ALL:
210277a78115SJacob Keller 		ice_set_rx_tstamp(pf, true);
210377a78115SJacob Keller 		break;
210477a78115SJacob Keller 	default:
210577a78115SJacob Keller 		return -ERANGE;
210677a78115SJacob Keller 	}
210777a78115SJacob Keller 
210877a78115SJacob Keller 	return 0;
210977a78115SJacob Keller }
211077a78115SJacob Keller 
211177a78115SJacob Keller /**
211277a78115SJacob Keller  * ice_ptp_set_ts_config - ioctl interface to control the timestamping
211377a78115SJacob Keller  * @pf: Board private structure
211477a78115SJacob Keller  * @ifr: ioctl data
211577a78115SJacob Keller  *
211677a78115SJacob Keller  * Get the user config and store it
211777a78115SJacob Keller  */
211877a78115SJacob Keller int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
211977a78115SJacob Keller {
212077a78115SJacob Keller 	struct hwtstamp_config config;
212177a78115SJacob Keller 	int err;
212277a78115SJacob Keller 
212377a78115SJacob Keller 	if (!test_bit(ICE_FLAG_PTP, pf->flags))
212477a78115SJacob Keller 		return -EAGAIN;
212577a78115SJacob Keller 
212677a78115SJacob Keller 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
212777a78115SJacob Keller 		return -EFAULT;
212877a78115SJacob Keller 
212977a78115SJacob Keller 	err = ice_ptp_set_timestamp_mode(pf, &config);
213077a78115SJacob Keller 	if (err)
213177a78115SJacob Keller 		return err;
213277a78115SJacob Keller 
2133e59d75ddSJacob Keller 	/* Return the actual configuration set */
2134e59d75ddSJacob Keller 	config = pf->ptp.tstamp_config;
213577a78115SJacob Keller 
213677a78115SJacob Keller 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
213777a78115SJacob Keller 		-EFAULT : 0;
213877a78115SJacob Keller }
213977a78115SJacob Keller 
214077a78115SJacob Keller /**
214177a78115SJacob Keller  * ice_ptp_rx_hwtstamp - Check for an Rx timestamp
214277a78115SJacob Keller  * @rx_ring: Ring to get the VSI info
214377a78115SJacob Keller  * @rx_desc: Receive descriptor
214477a78115SJacob Keller  * @skb: Particular skb to send timestamp with
214577a78115SJacob Keller  *
214677a78115SJacob Keller  * The driver receives a notification in the receive descriptor with timestamp.
214777a78115SJacob Keller  * The timestamp is in ns, so we must convert the result first.
214877a78115SJacob Keller  */
214977a78115SJacob Keller void
2150e72bba21SMaciej Fijalkowski ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring,
215177a78115SJacob Keller 		    union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb)
215277a78115SJacob Keller {
215377a78115SJacob Keller 	struct skb_shared_hwtstamps *hwtstamps;
2154b1a582e6SJacob Keller 	u64 ts_ns, cached_time;
2155b1a582e6SJacob Keller 	u32 ts_high;
215677a78115SJacob Keller 
2157b1a582e6SJacob Keller 	if (!(rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID))
2158b1a582e6SJacob Keller 		return;
2159b1a582e6SJacob Keller 
2160b1a582e6SJacob Keller 	cached_time = READ_ONCE(rx_ring->cached_phctime);
2161b1a582e6SJacob Keller 
2162b1a582e6SJacob Keller 	/* Do not report a timestamp if we don't have a cached PHC time */
2163b1a582e6SJacob Keller 	if (!cached_time)
2164b1a582e6SJacob Keller 		return;
2165b1a582e6SJacob Keller 
2166b1a582e6SJacob Keller 	/* Use ice_ptp_extend_32b_ts directly, using the ring-specific cached
2167b1a582e6SJacob Keller 	 * PHC value, rather than accessing the PF. This also allows us to
2168b1a582e6SJacob Keller 	 * simply pass the upper 32bits of nanoseconds directly. Calling
2169b1a582e6SJacob Keller 	 * ice_ptp_extend_40b_ts is unnecessary as it would just discard these
2170b1a582e6SJacob Keller 	 * bits itself.
217177a78115SJacob Keller 	 */
217277a78115SJacob Keller 	ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high);
2173b1a582e6SJacob Keller 	ts_ns = ice_ptp_extend_32b_ts(cached_time, ts_high);
217477a78115SJacob Keller 
217577a78115SJacob Keller 	hwtstamps = skb_hwtstamps(skb);
217677a78115SJacob Keller 	memset(hwtstamps, 0, sizeof(*hwtstamps));
217777a78115SJacob Keller 	hwtstamps->hwtstamp = ns_to_ktime(ts_ns);
217877a78115SJacob Keller }
217977a78115SJacob Keller 
218077a78115SJacob Keller /**
2181325b2064SMaciej Machnikowski  * ice_ptp_disable_sma_pins_e810t - Disable E810-T SMA pins
2182325b2064SMaciej Machnikowski  * @pf: pointer to the PF structure
2183325b2064SMaciej Machnikowski  * @info: PTP clock info structure
2184325b2064SMaciej Machnikowski  *
2185325b2064SMaciej Machnikowski  * Disable the OS access to the SMA pins. Called to clear out the OS
2186325b2064SMaciej Machnikowski  * indications of pin support when we fail to setup the E810-T SMA control
2187325b2064SMaciej Machnikowski  * register.
2188325b2064SMaciej Machnikowski  */
2189325b2064SMaciej Machnikowski static void
2190325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info)
2191325b2064SMaciej Machnikowski {
2192325b2064SMaciej Machnikowski 	struct device *dev = ice_pf_to_dev(pf);
2193325b2064SMaciej Machnikowski 
2194325b2064SMaciej Machnikowski 	dev_warn(dev, "Failed to configure E810-T SMA pin control\n");
2195325b2064SMaciej Machnikowski 
2196325b2064SMaciej Machnikowski 	info->enable = NULL;
2197325b2064SMaciej Machnikowski 	info->verify = NULL;
2198325b2064SMaciej Machnikowski 	info->n_pins = 0;
2199325b2064SMaciej Machnikowski 	info->n_ext_ts = 0;
2200325b2064SMaciej Machnikowski 	info->n_per_out = 0;
2201325b2064SMaciej Machnikowski }
2202325b2064SMaciej Machnikowski 
2203325b2064SMaciej Machnikowski /**
2204325b2064SMaciej Machnikowski  * ice_ptp_setup_sma_pins_e810t - Setup the SMA pins
2205325b2064SMaciej Machnikowski  * @pf: pointer to the PF structure
2206325b2064SMaciej Machnikowski  * @info: PTP clock info structure
2207325b2064SMaciej Machnikowski  *
2208325b2064SMaciej Machnikowski  * Finish setting up the SMA pins by allocating pin_config, and setting it up
2209325b2064SMaciej Machnikowski  * according to the current status of the SMA. On failure, disable all of the
2210325b2064SMaciej Machnikowski  * extended SMA pin support.
2211325b2064SMaciej Machnikowski  */
2212325b2064SMaciej Machnikowski static void
2213325b2064SMaciej Machnikowski ice_ptp_setup_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info)
2214325b2064SMaciej Machnikowski {
2215325b2064SMaciej Machnikowski 	struct device *dev = ice_pf_to_dev(pf);
2216325b2064SMaciej Machnikowski 	int err;
2217325b2064SMaciej Machnikowski 
2218325b2064SMaciej Machnikowski 	/* Allocate memory for kernel pins interface */
2219325b2064SMaciej Machnikowski 	info->pin_config = devm_kcalloc(dev, info->n_pins,
2220325b2064SMaciej Machnikowski 					sizeof(*info->pin_config), GFP_KERNEL);
2221325b2064SMaciej Machnikowski 	if (!info->pin_config) {
2222325b2064SMaciej Machnikowski 		ice_ptp_disable_sma_pins_e810t(pf, info);
2223325b2064SMaciej Machnikowski 		return;
2224325b2064SMaciej Machnikowski 	}
2225325b2064SMaciej Machnikowski 
2226325b2064SMaciej Machnikowski 	/* Read current SMA status */
2227325b2064SMaciej Machnikowski 	err = ice_get_sma_config_e810t(&pf->hw, info->pin_config);
2228325b2064SMaciej Machnikowski 	if (err)
2229325b2064SMaciej Machnikowski 		ice_ptp_disable_sma_pins_e810t(pf, info);
2230325b2064SMaciej Machnikowski }
2231325b2064SMaciej Machnikowski 
2232325b2064SMaciej Machnikowski /**
2233172db5f9SMaciej Machnikowski  * ice_ptp_setup_pins_e810 - Setup PTP pins in sysfs
2234896a55aaSAnirudh Venkataramanan  * @pf: pointer to the PF instance
2235172db5f9SMaciej Machnikowski  * @info: PTP clock capabilities
2236172db5f9SMaciej Machnikowski  */
223743c4958aSArkadiusz Kubalewski static void
223843c4958aSArkadiusz Kubalewski ice_ptp_setup_pins_e810(struct ice_pf *pf, struct ptp_clock_info *info)
2239172db5f9SMaciej Machnikowski {
2240325b2064SMaciej Machnikowski 	info->n_per_out = N_PER_OUT_E810;
2241896a55aaSAnirudh Venkataramanan 
224243c4958aSArkadiusz Kubalewski 	if (ice_is_feature_supported(pf, ICE_F_PTP_EXTTS))
2243325b2064SMaciej Machnikowski 		info->n_ext_ts = N_EXT_TS_E810;
224443c4958aSArkadiusz Kubalewski 
224543c4958aSArkadiusz Kubalewski 	if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) {
224643c4958aSArkadiusz Kubalewski 		info->n_ext_ts = N_EXT_TS_E810;
224743c4958aSArkadiusz Kubalewski 		info->n_pins = NUM_PTP_PINS_E810T;
224843c4958aSArkadiusz Kubalewski 		info->verify = ice_verify_pin_e810t;
224943c4958aSArkadiusz Kubalewski 
225043c4958aSArkadiusz Kubalewski 		/* Complete setup of the SMA pins */
225143c4958aSArkadiusz Kubalewski 		ice_ptp_setup_sma_pins_e810t(pf, info);
225243c4958aSArkadiusz Kubalewski 	}
2253172db5f9SMaciej Machnikowski }
2254172db5f9SMaciej Machnikowski 
2255172db5f9SMaciej Machnikowski /**
2256*634d841dSKarol Kolacinski  * ice_ptp_setup_pins_e823 - Setup PTP pins in sysfs
2257*634d841dSKarol Kolacinski  * @pf: pointer to the PF instance
2258*634d841dSKarol Kolacinski  * @info: PTP clock capabilities
2259*634d841dSKarol Kolacinski  */
2260*634d841dSKarol Kolacinski static void
2261*634d841dSKarol Kolacinski ice_ptp_setup_pins_e823(struct ice_pf *pf, struct ptp_clock_info *info)
2262*634d841dSKarol Kolacinski {
2263*634d841dSKarol Kolacinski 	info->pps = 1;
2264*634d841dSKarol Kolacinski 	info->n_per_out = 0;
2265*634d841dSKarol Kolacinski 	info->n_ext_ts = 1;
2266*634d841dSKarol Kolacinski }
2267*634d841dSKarol Kolacinski 
2268*634d841dSKarol Kolacinski /**
226913a64f0bSJacob Keller  * ice_ptp_set_funcs_e822 - Set specialized functions for E822 support
227013a64f0bSJacob Keller  * @pf: Board private structure
227113a64f0bSJacob Keller  * @info: PTP info to fill
227213a64f0bSJacob Keller  *
227313a64f0bSJacob Keller  * Assign functions to the PTP capabiltiies structure for E822 devices.
227413a64f0bSJacob Keller  * Functions which operate across all device families should be set directly
227513a64f0bSJacob Keller  * in ice_ptp_set_caps. Only add functions here which are distinct for E822
227613a64f0bSJacob Keller  * devices.
227713a64f0bSJacob Keller  */
227813a64f0bSJacob Keller static void
227913a64f0bSJacob Keller ice_ptp_set_funcs_e822(struct ice_pf *pf, struct ptp_clock_info *info)
228013a64f0bSJacob Keller {
228113a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS
228213a64f0bSJacob Keller 	if (boot_cpu_has(X86_FEATURE_ART) &&
228313a64f0bSJacob Keller 	    boot_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ))
228413a64f0bSJacob Keller 		info->getcrosststamp = ice_ptp_getcrosststamp_e822;
228513a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */
228613a64f0bSJacob Keller }
228713a64f0bSJacob Keller 
228813a64f0bSJacob Keller /**
2289172db5f9SMaciej Machnikowski  * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support
2290172db5f9SMaciej Machnikowski  * @pf: Board private structure
2291172db5f9SMaciej Machnikowski  * @info: PTP info to fill
2292172db5f9SMaciej Machnikowski  *
2293172db5f9SMaciej Machnikowski  * Assign functions to the PTP capabiltiies structure for E810 devices.
2294172db5f9SMaciej Machnikowski  * Functions which operate across all device families should be set directly
2295172db5f9SMaciej Machnikowski  * in ice_ptp_set_caps. Only add functions here which are distinct for e810
2296172db5f9SMaciej Machnikowski  * devices.
2297172db5f9SMaciej Machnikowski  */
2298172db5f9SMaciej Machnikowski static void
2299172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(struct ice_pf *pf, struct ptp_clock_info *info)
2300172db5f9SMaciej Machnikowski {
2301172db5f9SMaciej Machnikowski 	info->enable = ice_ptp_gpio_enable_e810;
2302896a55aaSAnirudh Venkataramanan 	ice_ptp_setup_pins_e810(pf, info);
2303172db5f9SMaciej Machnikowski }
2304172db5f9SMaciej Machnikowski 
2305172db5f9SMaciej Machnikowski /**
2306*634d841dSKarol Kolacinski  * ice_ptp_set_funcs_e823 - Set specialized functions for E823 support
2307*634d841dSKarol Kolacinski  * @pf: Board private structure
2308*634d841dSKarol Kolacinski  * @info: PTP info to fill
2309*634d841dSKarol Kolacinski  *
2310*634d841dSKarol Kolacinski  * Assign functions to the PTP capabiltiies structure for E823 devices.
2311*634d841dSKarol Kolacinski  * Functions which operate across all device families should be set directly
2312*634d841dSKarol Kolacinski  * in ice_ptp_set_caps. Only add functions here which are distinct for e823
2313*634d841dSKarol Kolacinski  * devices.
2314*634d841dSKarol Kolacinski  */
2315*634d841dSKarol Kolacinski static void
2316*634d841dSKarol Kolacinski ice_ptp_set_funcs_e823(struct ice_pf *pf, struct ptp_clock_info *info)
2317*634d841dSKarol Kolacinski {
2318*634d841dSKarol Kolacinski 	info->enable = ice_ptp_gpio_enable_e823;
2319*634d841dSKarol Kolacinski 	ice_ptp_setup_pins_e823(pf, info);
2320*634d841dSKarol Kolacinski }
2321*634d841dSKarol Kolacinski 
2322*634d841dSKarol Kolacinski /**
232306c16d89SJacob Keller  * ice_ptp_set_caps - Set PTP capabilities
232406c16d89SJacob Keller  * @pf: Board private structure
232506c16d89SJacob Keller  */
232606c16d89SJacob Keller static void ice_ptp_set_caps(struct ice_pf *pf)
232706c16d89SJacob Keller {
232806c16d89SJacob Keller 	struct ptp_clock_info *info = &pf->ptp.info;
232906c16d89SJacob Keller 	struct device *dev = ice_pf_to_dev(pf);
233006c16d89SJacob Keller 
233106c16d89SJacob Keller 	snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk",
233206c16d89SJacob Keller 		 dev_driver_string(dev), dev_name(dev));
233306c16d89SJacob Keller 	info->owner = THIS_MODULE;
23348aa4318cSSiddaraju DH 	info->max_adj = 100000000;
233506c16d89SJacob Keller 	info->adjtime = ice_ptp_adjtime;
233606c16d89SJacob Keller 	info->adjfine = ice_ptp_adjfine;
233706c16d89SJacob Keller 	info->gettimex64 = ice_ptp_gettimex64;
233806c16d89SJacob Keller 	info->settime64 = ice_ptp_settime64;
2339172db5f9SMaciej Machnikowski 
23403a749623SJacob Keller 	if (ice_is_e810(&pf->hw))
2341172db5f9SMaciej Machnikowski 		ice_ptp_set_funcs_e810(pf, info);
2342*634d841dSKarol Kolacinski 	else if (ice_is_e823(&pf->hw))
2343*634d841dSKarol Kolacinski 		ice_ptp_set_funcs_e823(pf, info);
234413a64f0bSJacob Keller 	else
234513a64f0bSJacob Keller 		ice_ptp_set_funcs_e822(pf, info);
234606c16d89SJacob Keller }
234706c16d89SJacob Keller 
234806c16d89SJacob Keller /**
234906c16d89SJacob Keller  * ice_ptp_create_clock - Create PTP clock device for userspace
235006c16d89SJacob Keller  * @pf: Board private structure
235106c16d89SJacob Keller  *
235206c16d89SJacob Keller  * This function creates a new PTP clock device. It only creates one if we
235306c16d89SJacob Keller  * don't already have one. Will return error if it can't create one, but success
235406c16d89SJacob Keller  * if we already have a device. Should be used by ice_ptp_init to create clock
235506c16d89SJacob Keller  * initially, and prevent global resets from creating new clock devices.
235606c16d89SJacob Keller  */
235706c16d89SJacob Keller static long ice_ptp_create_clock(struct ice_pf *pf)
235806c16d89SJacob Keller {
235906c16d89SJacob Keller 	struct ptp_clock_info *info;
236006c16d89SJacob Keller 	struct ptp_clock *clock;
236106c16d89SJacob Keller 	struct device *dev;
236206c16d89SJacob Keller 
236306c16d89SJacob Keller 	/* No need to create a clock device if we already have one */
236406c16d89SJacob Keller 	if (pf->ptp.clock)
236506c16d89SJacob Keller 		return 0;
236606c16d89SJacob Keller 
236706c16d89SJacob Keller 	ice_ptp_set_caps(pf);
236806c16d89SJacob Keller 
236906c16d89SJacob Keller 	info = &pf->ptp.info;
237006c16d89SJacob Keller 	dev = ice_pf_to_dev(pf);
237106c16d89SJacob Keller 
237206c16d89SJacob Keller 	/* Attempt to register the clock before enabling the hardware. */
237306c16d89SJacob Keller 	clock = ptp_clock_register(info, dev);
237406c16d89SJacob Keller 	if (IS_ERR(clock))
237506c16d89SJacob Keller 		return PTR_ERR(clock);
237606c16d89SJacob Keller 
237706c16d89SJacob Keller 	pf->ptp.clock = clock;
237806c16d89SJacob Keller 
237906c16d89SJacob Keller 	return 0;
238006c16d89SJacob Keller }
238106c16d89SJacob Keller 
2382ea9b847cSJacob Keller /**
2383ea9b847cSJacob Keller  * ice_ptp_request_ts - Request an available Tx timestamp index
2384ea9b847cSJacob Keller  * @tx: the PTP Tx timestamp tracker to request from
2385ea9b847cSJacob Keller  * @skb: the SKB to associate with this timestamp request
2386ea9b847cSJacob Keller  */
2387ea9b847cSJacob Keller s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
2388ea9b847cSJacob Keller {
2389ea9b847cSJacob Keller 	u8 idx;
2390ea9b847cSJacob Keller 
2391ea9b847cSJacob Keller 	spin_lock(&tx->lock);
23923ad5c10bSJacob Keller 
23933ad5c10bSJacob Keller 	/* Check that this tracker is accepting new timestamp requests */
23943ad5c10bSJacob Keller 	if (!ice_ptp_is_tx_tracker_up(tx)) {
23953ad5c10bSJacob Keller 		spin_unlock(&tx->lock);
23963ad5c10bSJacob Keller 		return -1;
23973ad5c10bSJacob Keller 	}
23983ad5c10bSJacob Keller 
2399ea9b847cSJacob Keller 	/* Find and set the first available index */
2400ea9b847cSJacob Keller 	idx = find_first_zero_bit(tx->in_use, tx->len);
2401ea9b847cSJacob Keller 	if (idx < tx->len) {
2402ea9b847cSJacob Keller 		/* We got a valid index that no other thread could have set. Store
2403ea9b847cSJacob Keller 		 * a reference to the skb and the start time to allow discarding old
2404ea9b847cSJacob Keller 		 * requests.
2405ea9b847cSJacob Keller 		 */
2406ea9b847cSJacob Keller 		set_bit(idx, tx->in_use);
2407d40fd600SJacob Keller 		clear_bit(idx, tx->stale);
2408ea9b847cSJacob Keller 		tx->tstamps[idx].start = jiffies;
2409ea9b847cSJacob Keller 		tx->tstamps[idx].skb = skb_get(skb);
2410ea9b847cSJacob Keller 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
24114c120218SJacob Keller 		ice_trace(tx_tstamp_request, skb, idx);
2412ea9b847cSJacob Keller 	}
2413ea9b847cSJacob Keller 
2414ea9b847cSJacob Keller 	spin_unlock(&tx->lock);
2415ea9b847cSJacob Keller 
2416ea9b847cSJacob Keller 	/* return the appropriate PHY timestamp register index, -1 if no
2417ea9b847cSJacob Keller 	 * indexes were available.
2418ea9b847cSJacob Keller 	 */
2419ea9b847cSJacob Keller 	if (idx >= tx->len)
2420ea9b847cSJacob Keller 		return -1;
2421ea9b847cSJacob Keller 	else
24226b5cbc8cSSergey Temerkhanov 		return idx + tx->offset;
2423ea9b847cSJacob Keller }
2424ea9b847cSJacob Keller 
2425ea9b847cSJacob Keller /**
24261229b339SKarol Kolacinski  * ice_ptp_process_ts - Process the PTP Tx timestamps
2427ea9b847cSJacob Keller  * @pf: Board private structure
2428ea9b847cSJacob Keller  *
24291229b339SKarol Kolacinski  * Returns true if timestamps are processed.
2430ea9b847cSJacob Keller  */
24311229b339SKarol Kolacinski bool ice_ptp_process_ts(struct ice_pf *pf)
2432ea9b847cSJacob Keller {
24331229b339SKarol Kolacinski 	return ice_ptp_tx_tstamp(&pf->ptp.port.tx);
2434ea9b847cSJacob Keller }
2435ea9b847cSJacob Keller 
243677a78115SJacob Keller static void ice_ptp_periodic_work(struct kthread_work *work)
243777a78115SJacob Keller {
243877a78115SJacob Keller 	struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work);
243977a78115SJacob Keller 	struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp);
24404503cc7fSArkadiusz Kubalewski 	int err;
244177a78115SJacob Keller 
244277a78115SJacob Keller 	if (!test_bit(ICE_FLAG_PTP, pf->flags))
244377a78115SJacob Keller 		return;
244477a78115SJacob Keller 
24454503cc7fSArkadiusz Kubalewski 	err = ice_ptp_update_cached_phctime(pf);
244677a78115SJacob Keller 
24474503cc7fSArkadiusz Kubalewski 	/* Run twice a second or reschedule if phc update failed */
244877a78115SJacob Keller 	kthread_queue_delayed_work(ptp->kworker, &ptp->work,
24494503cc7fSArkadiusz Kubalewski 				   msecs_to_jiffies(err ? 10 : 500));
245077a78115SJacob Keller }
245177a78115SJacob Keller 
245206c16d89SJacob Keller /**
245348096710SKarol Kolacinski  * ice_ptp_reset - Initialize PTP hardware clock support after reset
245448096710SKarol Kolacinski  * @pf: Board private structure
245548096710SKarol Kolacinski  */
245648096710SKarol Kolacinski void ice_ptp_reset(struct ice_pf *pf)
245748096710SKarol Kolacinski {
245848096710SKarol Kolacinski 	struct ice_ptp *ptp = &pf->ptp;
245948096710SKarol Kolacinski 	struct ice_hw *hw = &pf->hw;
246048096710SKarol Kolacinski 	struct timespec64 ts;
24613a749623SJacob Keller 	int err, itr = 1;
246248096710SKarol Kolacinski 	u64 time_diff;
246348096710SKarol Kolacinski 
246448096710SKarol Kolacinski 	if (test_bit(ICE_PFR_REQ, pf->state))
246548096710SKarol Kolacinski 		goto pfr;
246648096710SKarol Kolacinski 
2467b2ee7256SJacob Keller 	if (!hw->func_caps.ts_func_info.src_tmr_owned)
24683a749623SJacob Keller 		goto reset_ts;
246948096710SKarol Kolacinski 
2470b2ee7256SJacob Keller 	err = ice_ptp_init_phc(hw);
247148096710SKarol Kolacinski 	if (err)
247248096710SKarol Kolacinski 		goto err;
247348096710SKarol Kolacinski 
247448096710SKarol Kolacinski 	/* Acquire the global hardware lock */
247548096710SKarol Kolacinski 	if (!ice_ptp_lock(hw)) {
247648096710SKarol Kolacinski 		err = -EBUSY;
247748096710SKarol Kolacinski 		goto err;
247848096710SKarol Kolacinski 	}
247948096710SKarol Kolacinski 
248048096710SKarol Kolacinski 	/* Write the increment time value to PHY and LAN */
248178267d0cSJacob Keller 	err = ice_ptp_write_incval(hw, ice_base_incval(pf));
248248096710SKarol Kolacinski 	if (err) {
248348096710SKarol Kolacinski 		ice_ptp_unlock(hw);
248448096710SKarol Kolacinski 		goto err;
248548096710SKarol Kolacinski 	}
248648096710SKarol Kolacinski 
248748096710SKarol Kolacinski 	/* Write the initial Time value to PHY and LAN using the cached PHC
248848096710SKarol Kolacinski 	 * time before the reset and time difference between stopping and
248948096710SKarol Kolacinski 	 * starting the clock.
249048096710SKarol Kolacinski 	 */
249148096710SKarol Kolacinski 	if (ptp->cached_phc_time) {
249248096710SKarol Kolacinski 		time_diff = ktime_get_real_ns() - ptp->reset_time;
249348096710SKarol Kolacinski 		ts = ns_to_timespec64(ptp->cached_phc_time + time_diff);
249448096710SKarol Kolacinski 	} else {
249548096710SKarol Kolacinski 		ts = ktime_to_timespec64(ktime_get_real());
249648096710SKarol Kolacinski 	}
249748096710SKarol Kolacinski 	err = ice_ptp_write_init(pf, &ts);
249848096710SKarol Kolacinski 	if (err) {
249948096710SKarol Kolacinski 		ice_ptp_unlock(hw);
250048096710SKarol Kolacinski 		goto err;
250148096710SKarol Kolacinski 	}
250248096710SKarol Kolacinski 
250348096710SKarol Kolacinski 	/* Release the global hardware lock */
250448096710SKarol Kolacinski 	ice_ptp_unlock(hw);
250548096710SKarol Kolacinski 
25063a749623SJacob Keller 	if (!ice_is_e810(hw)) {
25073a749623SJacob Keller 		/* Enable quad interrupts */
25083a749623SJacob Keller 		err = ice_ptp_tx_ena_intr(pf, true, itr);
25093a749623SJacob Keller 		if (err)
25103a749623SJacob Keller 			goto err;
25113a749623SJacob Keller 	}
25123a749623SJacob Keller 
25133a749623SJacob Keller reset_ts:
25143a749623SJacob Keller 	/* Restart the PHY timestamping block */
25153a749623SJacob Keller 	ice_ptp_reset_phy_timestamping(pf);
25163a749623SJacob Keller 
251748096710SKarol Kolacinski pfr:
251848096710SKarol Kolacinski 	/* Init Tx structures */
2519a69f1cb6SJacob Keller 	if (ice_is_e810(&pf->hw)) {
252048096710SKarol Kolacinski 		err = ice_ptp_init_tx_e810(pf, &ptp->port.tx);
2521a69f1cb6SJacob Keller 	} else {
2522a69f1cb6SJacob Keller 		kthread_init_delayed_work(&ptp->port.ov_work,
2523f029a343SSiddaraju DH 					  ice_ptp_wait_for_offsets);
25243a749623SJacob Keller 		err = ice_ptp_init_tx_e822(pf, &ptp->port.tx,
25253a749623SJacob Keller 					   ptp->port.port_num);
2526a69f1cb6SJacob Keller 	}
252748096710SKarol Kolacinski 	if (err)
252848096710SKarol Kolacinski 		goto err;
252948096710SKarol Kolacinski 
253048096710SKarol Kolacinski 	set_bit(ICE_FLAG_PTP, pf->flags);
253148096710SKarol Kolacinski 
253248096710SKarol Kolacinski 	/* Start periodic work going */
253348096710SKarol Kolacinski 	kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0);
253448096710SKarol Kolacinski 
253548096710SKarol Kolacinski 	dev_info(ice_pf_to_dev(pf), "PTP reset successful\n");
253648096710SKarol Kolacinski 	return;
253748096710SKarol Kolacinski 
253848096710SKarol Kolacinski err:
253948096710SKarol Kolacinski 	dev_err(ice_pf_to_dev(pf), "PTP reset failed %d\n", err);
254048096710SKarol Kolacinski }
254148096710SKarol Kolacinski 
254248096710SKarol Kolacinski /**
254348096710SKarol Kolacinski  * ice_ptp_prepare_for_reset - Prepare PTP for reset
254448096710SKarol Kolacinski  * @pf: Board private structure
254548096710SKarol Kolacinski  */
254648096710SKarol Kolacinski void ice_ptp_prepare_for_reset(struct ice_pf *pf)
254748096710SKarol Kolacinski {
254848096710SKarol Kolacinski 	struct ice_ptp *ptp = &pf->ptp;
254948096710SKarol Kolacinski 	u8 src_tmr;
255048096710SKarol Kolacinski 
255148096710SKarol Kolacinski 	clear_bit(ICE_FLAG_PTP, pf->flags);
255248096710SKarol Kolacinski 
255348096710SKarol Kolacinski 	/* Disable timestamping for both Tx and Rx */
255448096710SKarol Kolacinski 	ice_ptp_cfg_timestamp(pf, false);
255548096710SKarol Kolacinski 
255648096710SKarol Kolacinski 	kthread_cancel_delayed_work_sync(&ptp->work);
255748096710SKarol Kolacinski 	kthread_cancel_work_sync(&ptp->extts_work);
255848096710SKarol Kolacinski 
255948096710SKarol Kolacinski 	if (test_bit(ICE_PFR_REQ, pf->state))
256048096710SKarol Kolacinski 		return;
256148096710SKarol Kolacinski 
256248096710SKarol Kolacinski 	ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
256348096710SKarol Kolacinski 
256448096710SKarol Kolacinski 	/* Disable periodic outputs */
256548096710SKarol Kolacinski 	ice_ptp_disable_all_clkout(pf);
256648096710SKarol Kolacinski 
256748096710SKarol Kolacinski 	src_tmr = ice_get_ptp_src_clock_index(&pf->hw);
256848096710SKarol Kolacinski 
256948096710SKarol Kolacinski 	/* Disable source clock */
257048096710SKarol Kolacinski 	wr32(&pf->hw, GLTSYN_ENA(src_tmr), (u32)~GLTSYN_ENA_TSYN_ENA_M);
257148096710SKarol Kolacinski 
257248096710SKarol Kolacinski 	/* Acquire PHC and system timer to restore after reset */
257348096710SKarol Kolacinski 	ptp->reset_time = ktime_get_real_ns();
257448096710SKarol Kolacinski }
257548096710SKarol Kolacinski 
257648096710SKarol Kolacinski /**
257706c16d89SJacob Keller  * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device
257806c16d89SJacob Keller  * @pf: Board private structure
257906c16d89SJacob Keller  *
258006c16d89SJacob Keller  * Setup and initialize a PTP clock device that represents the device hardware
258106c16d89SJacob Keller  * clock. Save the clock index for other functions connected to the same
258206c16d89SJacob Keller  * hardware resource.
258306c16d89SJacob Keller  */
258406c16d89SJacob Keller static int ice_ptp_init_owner(struct ice_pf *pf)
258506c16d89SJacob Keller {
258606c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
258706c16d89SJacob Keller 	struct timespec64 ts;
25883a749623SJacob Keller 	int err, itr = 1;
258906c16d89SJacob Keller 
2590b2ee7256SJacob Keller 	err = ice_ptp_init_phc(hw);
2591b2ee7256SJacob Keller 	if (err) {
2592b2ee7256SJacob Keller 		dev_err(ice_pf_to_dev(pf), "Failed to initialize PHC, err %d\n",
2593b2ee7256SJacob Keller 			err);
2594b2ee7256SJacob Keller 		return err;
2595b2ee7256SJacob Keller 	}
259606c16d89SJacob Keller 
259706c16d89SJacob Keller 	/* Acquire the global hardware lock */
259806c16d89SJacob Keller 	if (!ice_ptp_lock(hw)) {
259906c16d89SJacob Keller 		err = -EBUSY;
260006c16d89SJacob Keller 		goto err_exit;
260106c16d89SJacob Keller 	}
260206c16d89SJacob Keller 
260306c16d89SJacob Keller 	/* Write the increment time value to PHY and LAN */
260478267d0cSJacob Keller 	err = ice_ptp_write_incval(hw, ice_base_incval(pf));
260506c16d89SJacob Keller 	if (err) {
260606c16d89SJacob Keller 		ice_ptp_unlock(hw);
260706c16d89SJacob Keller 		goto err_exit;
260806c16d89SJacob Keller 	}
260906c16d89SJacob Keller 
261006c16d89SJacob Keller 	ts = ktime_to_timespec64(ktime_get_real());
261106c16d89SJacob Keller 	/* Write the initial Time value to PHY and LAN */
261206c16d89SJacob Keller 	err = ice_ptp_write_init(pf, &ts);
261306c16d89SJacob Keller 	if (err) {
261406c16d89SJacob Keller 		ice_ptp_unlock(hw);
261506c16d89SJacob Keller 		goto err_exit;
261606c16d89SJacob Keller 	}
261706c16d89SJacob Keller 
261806c16d89SJacob Keller 	/* Release the global hardware lock */
261906c16d89SJacob Keller 	ice_ptp_unlock(hw);
262006c16d89SJacob Keller 
26213a749623SJacob Keller 	if (!ice_is_e810(hw)) {
26223a749623SJacob Keller 		/* Enable quad interrupts */
26233a749623SJacob Keller 		err = ice_ptp_tx_ena_intr(pf, true, itr);
26243a749623SJacob Keller 		if (err)
26253a749623SJacob Keller 			goto err_exit;
26263a749623SJacob Keller 	}
26273a749623SJacob Keller 
262806c16d89SJacob Keller 	/* Ensure we have a clock device */
262906c16d89SJacob Keller 	err = ice_ptp_create_clock(pf);
263006c16d89SJacob Keller 	if (err)
263106c16d89SJacob Keller 		goto err_clk;
263206c16d89SJacob Keller 
263367569a7fSJacob Keller 	/* Store the PTP clock index for other PFs */
263467569a7fSJacob Keller 	ice_set_ptp_clock_index(pf);
263567569a7fSJacob Keller 
263606c16d89SJacob Keller 	return 0;
263706c16d89SJacob Keller 
263806c16d89SJacob Keller err_clk:
263906c16d89SJacob Keller 	pf->ptp.clock = NULL;
264006c16d89SJacob Keller err_exit:
264106c16d89SJacob Keller 	return err;
264206c16d89SJacob Keller }
264306c16d89SJacob Keller 
264406c16d89SJacob Keller /**
264548096710SKarol Kolacinski  * ice_ptp_init_work - Initialize PTP work threads
264648096710SKarol Kolacinski  * @pf: Board private structure
264748096710SKarol Kolacinski  * @ptp: PF PTP structure
264848096710SKarol Kolacinski  */
264948096710SKarol Kolacinski static int ice_ptp_init_work(struct ice_pf *pf, struct ice_ptp *ptp)
265048096710SKarol Kolacinski {
265148096710SKarol Kolacinski 	struct kthread_worker *kworker;
265248096710SKarol Kolacinski 
265348096710SKarol Kolacinski 	/* Initialize work functions */
265448096710SKarol Kolacinski 	kthread_init_delayed_work(&ptp->work, ice_ptp_periodic_work);
265548096710SKarol Kolacinski 	kthread_init_work(&ptp->extts_work, ice_ptp_extts_work);
265648096710SKarol Kolacinski 
265748096710SKarol Kolacinski 	/* Allocate a kworker for handling work required for the ports
265848096710SKarol Kolacinski 	 * connected to the PTP hardware clock.
265948096710SKarol Kolacinski 	 */
266048096710SKarol Kolacinski 	kworker = kthread_create_worker(0, "ice-ptp-%s",
266148096710SKarol Kolacinski 					dev_name(ice_pf_to_dev(pf)));
266248096710SKarol Kolacinski 	if (IS_ERR(kworker))
266348096710SKarol Kolacinski 		return PTR_ERR(kworker);
266448096710SKarol Kolacinski 
266548096710SKarol Kolacinski 	ptp->kworker = kworker;
266648096710SKarol Kolacinski 
266748096710SKarol Kolacinski 	/* Start periodic work going */
266848096710SKarol Kolacinski 	kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0);
266948096710SKarol Kolacinski 
267048096710SKarol Kolacinski 	return 0;
267148096710SKarol Kolacinski }
267248096710SKarol Kolacinski 
267348096710SKarol Kolacinski /**
26743a749623SJacob Keller  * ice_ptp_init_port - Initialize PTP port structure
26753a749623SJacob Keller  * @pf: Board private structure
26763a749623SJacob Keller  * @ptp_port: PTP port structure
26773a749623SJacob Keller  */
26783a749623SJacob Keller static int ice_ptp_init_port(struct ice_pf *pf, struct ice_ptp_port *ptp_port)
26793a749623SJacob Keller {
26803a749623SJacob Keller 	mutex_init(&ptp_port->ps_lock);
26813a749623SJacob Keller 
26823a749623SJacob Keller 	if (ice_is_e810(&pf->hw))
26833a749623SJacob Keller 		return ice_ptp_init_tx_e810(pf, &ptp_port->tx);
26843a749623SJacob Keller 
2685a69f1cb6SJacob Keller 	kthread_init_delayed_work(&ptp_port->ov_work,
2686f029a343SSiddaraju DH 				  ice_ptp_wait_for_offsets);
26873a749623SJacob Keller 	return ice_ptp_init_tx_e822(pf, &ptp_port->tx, ptp_port->port_num);
26883a749623SJacob Keller }
26893a749623SJacob Keller 
26903a749623SJacob Keller /**
2691b2ee7256SJacob Keller  * ice_ptp_init - Initialize PTP hardware clock support
269206c16d89SJacob Keller  * @pf: Board private structure
269306c16d89SJacob Keller  *
2694b2ee7256SJacob Keller  * Set up the device for interacting with the PTP hardware clock for all
2695b2ee7256SJacob Keller  * functions, both the function that owns the clock hardware, and the
2696b2ee7256SJacob Keller  * functions connected to the clock hardware.
2697b2ee7256SJacob Keller  *
2698b2ee7256SJacob Keller  * The clock owner will allocate and register a ptp_clock with the
2699b2ee7256SJacob Keller  * PTP_1588_CLOCK infrastructure. All functions allocate a kthread and work
2700b2ee7256SJacob Keller  * items used for asynchronous work such as Tx timestamps and periodic work.
270106c16d89SJacob Keller  */
270206c16d89SJacob Keller void ice_ptp_init(struct ice_pf *pf)
270306c16d89SJacob Keller {
270448096710SKarol Kolacinski 	struct ice_ptp *ptp = &pf->ptp;
270506c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
270606c16d89SJacob Keller 	int err;
270706c16d89SJacob Keller 
2708b2ee7256SJacob Keller 	/* If this function owns the clock hardware, it must allocate and
2709b2ee7256SJacob Keller 	 * configure the PTP clock device to represent it.
2710b2ee7256SJacob Keller 	 */
271106c16d89SJacob Keller 	if (hw->func_caps.ts_func_info.src_tmr_owned) {
271206c16d89SJacob Keller 		err = ice_ptp_init_owner(pf);
271306c16d89SJacob Keller 		if (err)
271448096710SKarol Kolacinski 			goto err;
271506c16d89SJacob Keller 	}
271606c16d89SJacob Keller 
27173a749623SJacob Keller 	ptp->port.port_num = hw->pf_id;
27183a749623SJacob Keller 	err = ice_ptp_init_port(pf, &ptp->port);
271948096710SKarol Kolacinski 	if (err)
272048096710SKarol Kolacinski 		goto err;
272177a78115SJacob Keller 
27223a749623SJacob Keller 	/* Start the PHY timestamping block */
27233a749623SJacob Keller 	ice_ptp_reset_phy_timestamping(pf);
27243a749623SJacob Keller 
272506c16d89SJacob Keller 	set_bit(ICE_FLAG_PTP, pf->flags);
272648096710SKarol Kolacinski 	err = ice_ptp_init_work(pf, ptp);
272748096710SKarol Kolacinski 	if (err)
272848096710SKarol Kolacinski 		goto err;
272906c16d89SJacob Keller 
273048096710SKarol Kolacinski 	dev_info(ice_pf_to_dev(pf), "PTP init successful\n");
273177a78115SJacob Keller 	return;
273277a78115SJacob Keller 
273348096710SKarol Kolacinski err:
273477a78115SJacob Keller 	/* If we registered a PTP clock, release it */
273577a78115SJacob Keller 	if (pf->ptp.clock) {
273648096710SKarol Kolacinski 		ptp_clock_unregister(ptp->clock);
273777a78115SJacob Keller 		pf->ptp.clock = NULL;
273877a78115SJacob Keller 	}
273948096710SKarol Kolacinski 	clear_bit(ICE_FLAG_PTP, pf->flags);
274048096710SKarol Kolacinski 	dev_err(ice_pf_to_dev(pf), "PTP failed %d\n", err);
274106c16d89SJacob Keller }
274206c16d89SJacob Keller 
274306c16d89SJacob Keller /**
274406c16d89SJacob Keller  * ice_ptp_release - Disable the driver/HW support and unregister the clock
274506c16d89SJacob Keller  * @pf: Board private structure
274606c16d89SJacob Keller  *
274706c16d89SJacob Keller  * This function handles the cleanup work required from the initialization by
274806c16d89SJacob Keller  * clearing out the important information and unregistering the clock
274906c16d89SJacob Keller  */
275006c16d89SJacob Keller void ice_ptp_release(struct ice_pf *pf)
275106c16d89SJacob Keller {
2752fd1b5bebSYongxin Liu 	if (!test_bit(ICE_FLAG_PTP, pf->flags))
2753fd1b5bebSYongxin Liu 		return;
2754fd1b5bebSYongxin Liu 
275577a78115SJacob Keller 	/* Disable timestamping for both Tx and Rx */
275677a78115SJacob Keller 	ice_ptp_cfg_timestamp(pf, false);
275777a78115SJacob Keller 
2758ea9b847cSJacob Keller 	ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
2759ea9b847cSJacob Keller 
276006c16d89SJacob Keller 	clear_bit(ICE_FLAG_PTP, pf->flags);
276106c16d89SJacob Keller 
276277a78115SJacob Keller 	kthread_cancel_delayed_work_sync(&pf->ptp.work);
276377a78115SJacob Keller 
27643a749623SJacob Keller 	ice_ptp_port_phy_stop(&pf->ptp.port);
27653a749623SJacob Keller 	mutex_destroy(&pf->ptp.port.ps_lock);
276677a78115SJacob Keller 	if (pf->ptp.kworker) {
276777a78115SJacob Keller 		kthread_destroy_worker(pf->ptp.kworker);
276877a78115SJacob Keller 		pf->ptp.kworker = NULL;
276977a78115SJacob Keller 	}
277077a78115SJacob Keller 
277106c16d89SJacob Keller 	if (!pf->ptp.clock)
277206c16d89SJacob Keller 		return;
277306c16d89SJacob Keller 
27749ee31343SJacob Keller 	/* Disable periodic outputs */
27759ee31343SJacob Keller 	ice_ptp_disable_all_clkout(pf);
27769ee31343SJacob Keller 
277767569a7fSJacob Keller 	ice_clear_ptp_clock_index(pf);
277806c16d89SJacob Keller 	ptp_clock_unregister(pf->ptp.clock);
277906c16d89SJacob Keller 	pf->ptp.clock = NULL;
278006c16d89SJacob Keller 
278106c16d89SJacob Keller 	dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n");
278206c16d89SJacob Keller }
2783