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"
606c16d89SJacob Keller 
7172db5f9SMaciej Machnikowski #define E810_OUT_PROP_DELAY_NS 1
8172db5f9SMaciej Machnikowski 
9325b2064SMaciej Machnikowski static const struct ptp_pin_desc ice_pin_desc_e810t[] = {
10325b2064SMaciej Machnikowski 	/* name    idx   func         chan */
11325b2064SMaciej Machnikowski 	{ "GNSS",  GNSS, PTP_PF_EXTTS, 0, { 0, } },
12325b2064SMaciej Machnikowski 	{ "SMA1",  SMA1, PTP_PF_NONE, 1, { 0, } },
13325b2064SMaciej Machnikowski 	{ "U.FL1", UFL1, PTP_PF_NONE, 1, { 0, } },
14325b2064SMaciej Machnikowski 	{ "SMA2",  SMA2, PTP_PF_NONE, 2, { 0, } },
15325b2064SMaciej Machnikowski 	{ "U.FL2", UFL2, PTP_PF_NONE, 2, { 0, } },
16325b2064SMaciej Machnikowski };
17325b2064SMaciej Machnikowski 
18325b2064SMaciej Machnikowski /**
19325b2064SMaciej Machnikowski  * ice_get_sma_config_e810t
20325b2064SMaciej Machnikowski  * @hw: pointer to the hw struct
21325b2064SMaciej Machnikowski  * @ptp_pins: pointer to the ptp_pin_desc struture
22325b2064SMaciej Machnikowski  *
23325b2064SMaciej Machnikowski  * Read the configuration of the SMA control logic and put it into the
24325b2064SMaciej Machnikowski  * ptp_pin_desc structure
25325b2064SMaciej Machnikowski  */
26325b2064SMaciej Machnikowski static int
27325b2064SMaciej Machnikowski ice_get_sma_config_e810t(struct ice_hw *hw, struct ptp_pin_desc *ptp_pins)
28325b2064SMaciej Machnikowski {
29325b2064SMaciej Machnikowski 	u8 data, i;
30325b2064SMaciej Machnikowski 	int status;
31325b2064SMaciej Machnikowski 
32325b2064SMaciej Machnikowski 	/* Read initial pin state */
33325b2064SMaciej Machnikowski 	status = ice_read_sma_ctrl_e810t(hw, &data);
34325b2064SMaciej Machnikowski 	if (status)
35325b2064SMaciej Machnikowski 		return status;
36325b2064SMaciej Machnikowski 
37325b2064SMaciej Machnikowski 	/* initialize with defaults */
38325b2064SMaciej Machnikowski 	for (i = 0; i < NUM_PTP_PINS_E810T; i++) {
39325b2064SMaciej Machnikowski 		snprintf(ptp_pins[i].name, sizeof(ptp_pins[i].name),
40325b2064SMaciej Machnikowski 			 "%s", ice_pin_desc_e810t[i].name);
41325b2064SMaciej Machnikowski 		ptp_pins[i].index = ice_pin_desc_e810t[i].index;
42325b2064SMaciej Machnikowski 		ptp_pins[i].func = ice_pin_desc_e810t[i].func;
43325b2064SMaciej Machnikowski 		ptp_pins[i].chan = ice_pin_desc_e810t[i].chan;
44325b2064SMaciej Machnikowski 	}
45325b2064SMaciej Machnikowski 
46325b2064SMaciej Machnikowski 	/* Parse SMA1/UFL1 */
47325b2064SMaciej Machnikowski 	switch (data & ICE_SMA1_MASK_E810T) {
48325b2064SMaciej Machnikowski 	case ICE_SMA1_MASK_E810T:
49325b2064SMaciej Machnikowski 	default:
50325b2064SMaciej Machnikowski 		ptp_pins[SMA1].func = PTP_PF_NONE;
51325b2064SMaciej Machnikowski 		ptp_pins[UFL1].func = PTP_PF_NONE;
52325b2064SMaciej Machnikowski 		break;
53325b2064SMaciej Machnikowski 	case ICE_SMA1_DIR_EN_E810T:
54325b2064SMaciej Machnikowski 		ptp_pins[SMA1].func = PTP_PF_PEROUT;
55325b2064SMaciej Machnikowski 		ptp_pins[UFL1].func = PTP_PF_NONE;
56325b2064SMaciej Machnikowski 		break;
57325b2064SMaciej Machnikowski 	case ICE_SMA1_TX_EN_E810T:
58325b2064SMaciej Machnikowski 		ptp_pins[SMA1].func = PTP_PF_EXTTS;
59325b2064SMaciej Machnikowski 		ptp_pins[UFL1].func = PTP_PF_NONE;
60325b2064SMaciej Machnikowski 		break;
61325b2064SMaciej Machnikowski 	case 0:
62325b2064SMaciej Machnikowski 		ptp_pins[SMA1].func = PTP_PF_EXTTS;
63325b2064SMaciej Machnikowski 		ptp_pins[UFL1].func = PTP_PF_PEROUT;
64325b2064SMaciej Machnikowski 		break;
65325b2064SMaciej Machnikowski 	}
66325b2064SMaciej Machnikowski 
67325b2064SMaciej Machnikowski 	/* Parse SMA2/UFL2 */
68325b2064SMaciej Machnikowski 	switch (data & ICE_SMA2_MASK_E810T) {
69325b2064SMaciej Machnikowski 	case ICE_SMA2_MASK_E810T:
70325b2064SMaciej Machnikowski 	default:
71325b2064SMaciej Machnikowski 		ptp_pins[SMA2].func = PTP_PF_NONE;
72325b2064SMaciej Machnikowski 		ptp_pins[UFL2].func = PTP_PF_NONE;
73325b2064SMaciej Machnikowski 		break;
74325b2064SMaciej Machnikowski 	case (ICE_SMA2_TX_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T):
75325b2064SMaciej Machnikowski 		ptp_pins[SMA2].func = PTP_PF_EXTTS;
76325b2064SMaciej Machnikowski 		ptp_pins[UFL2].func = PTP_PF_NONE;
77325b2064SMaciej Machnikowski 		break;
78325b2064SMaciej Machnikowski 	case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T):
79325b2064SMaciej Machnikowski 		ptp_pins[SMA2].func = PTP_PF_PEROUT;
80325b2064SMaciej Machnikowski 		ptp_pins[UFL2].func = PTP_PF_NONE;
81325b2064SMaciej Machnikowski 		break;
82325b2064SMaciej Machnikowski 	case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T):
83325b2064SMaciej Machnikowski 		ptp_pins[SMA2].func = PTP_PF_NONE;
84325b2064SMaciej Machnikowski 		ptp_pins[UFL2].func = PTP_PF_EXTTS;
85325b2064SMaciej Machnikowski 		break;
86325b2064SMaciej Machnikowski 	case ICE_SMA2_DIR_EN_E810T:
87325b2064SMaciej Machnikowski 		ptp_pins[SMA2].func = PTP_PF_PEROUT;
88325b2064SMaciej Machnikowski 		ptp_pins[UFL2].func = PTP_PF_EXTTS;
89325b2064SMaciej Machnikowski 		break;
90325b2064SMaciej Machnikowski 	}
91325b2064SMaciej Machnikowski 
92325b2064SMaciej Machnikowski 	return 0;
93325b2064SMaciej Machnikowski }
94325b2064SMaciej Machnikowski 
95325b2064SMaciej Machnikowski /**
96325b2064SMaciej Machnikowski  * ice_ptp_set_sma_config_e810t
97325b2064SMaciej Machnikowski  * @hw: pointer to the hw struct
98325b2064SMaciej Machnikowski  * @ptp_pins: pointer to the ptp_pin_desc struture
99325b2064SMaciej Machnikowski  *
100325b2064SMaciej Machnikowski  * Set the configuration of the SMA control logic based on the configuration in
101325b2064SMaciej Machnikowski  * num_pins parameter
102325b2064SMaciej Machnikowski  */
103325b2064SMaciej Machnikowski static int
104325b2064SMaciej Machnikowski ice_ptp_set_sma_config_e810t(struct ice_hw *hw,
105325b2064SMaciej Machnikowski 			     const struct ptp_pin_desc *ptp_pins)
106325b2064SMaciej Machnikowski {
107325b2064SMaciej Machnikowski 	int status;
108325b2064SMaciej Machnikowski 	u8 data;
109325b2064SMaciej Machnikowski 
110325b2064SMaciej Machnikowski 	/* SMA1 and UFL1 cannot be set to TX at the same time */
111325b2064SMaciej Machnikowski 	if (ptp_pins[SMA1].func == PTP_PF_PEROUT &&
112325b2064SMaciej Machnikowski 	    ptp_pins[UFL1].func == PTP_PF_PEROUT)
113325b2064SMaciej Machnikowski 		return -EINVAL;
114325b2064SMaciej Machnikowski 
115325b2064SMaciej Machnikowski 	/* SMA2 and UFL2 cannot be set to RX at the same time */
116325b2064SMaciej Machnikowski 	if (ptp_pins[SMA2].func == PTP_PF_EXTTS &&
117325b2064SMaciej Machnikowski 	    ptp_pins[UFL2].func == PTP_PF_EXTTS)
118325b2064SMaciej Machnikowski 		return -EINVAL;
119325b2064SMaciej Machnikowski 
120325b2064SMaciej Machnikowski 	/* Read initial pin state value */
121325b2064SMaciej Machnikowski 	status = ice_read_sma_ctrl_e810t(hw, &data);
122325b2064SMaciej Machnikowski 	if (status)
123325b2064SMaciej Machnikowski 		return status;
124325b2064SMaciej Machnikowski 
125325b2064SMaciej Machnikowski 	/* Set the right sate based on the desired configuration */
126325b2064SMaciej Machnikowski 	data &= ~ICE_SMA1_MASK_E810T;
127325b2064SMaciej Machnikowski 	if (ptp_pins[SMA1].func == PTP_PF_NONE &&
128325b2064SMaciej Machnikowski 	    ptp_pins[UFL1].func == PTP_PF_NONE) {
129325b2064SMaciej Machnikowski 		dev_info(ice_hw_to_dev(hw), "SMA1 + U.FL1 disabled");
130325b2064SMaciej Machnikowski 		data |= ICE_SMA1_MASK_E810T;
131325b2064SMaciej Machnikowski 	} else if (ptp_pins[SMA1].func == PTP_PF_EXTTS &&
132325b2064SMaciej Machnikowski 		   ptp_pins[UFL1].func == PTP_PF_NONE) {
133325b2064SMaciej Machnikowski 		dev_info(ice_hw_to_dev(hw), "SMA1 RX");
134325b2064SMaciej Machnikowski 		data |= ICE_SMA1_TX_EN_E810T;
135325b2064SMaciej Machnikowski 	} else if (ptp_pins[SMA1].func == PTP_PF_NONE &&
136325b2064SMaciej Machnikowski 		   ptp_pins[UFL1].func == PTP_PF_PEROUT) {
137325b2064SMaciej Machnikowski 		/* U.FL 1 TX will always enable SMA 1 RX */
138325b2064SMaciej Machnikowski 		dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX");
139325b2064SMaciej Machnikowski 	} else if (ptp_pins[SMA1].func == PTP_PF_EXTTS &&
140325b2064SMaciej Machnikowski 		   ptp_pins[UFL1].func == PTP_PF_PEROUT) {
141325b2064SMaciej Machnikowski 		dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX");
142325b2064SMaciej Machnikowski 	} else if (ptp_pins[SMA1].func == PTP_PF_PEROUT &&
143325b2064SMaciej Machnikowski 		   ptp_pins[UFL1].func == PTP_PF_NONE) {
144325b2064SMaciej Machnikowski 		dev_info(ice_hw_to_dev(hw), "SMA1 TX");
145325b2064SMaciej Machnikowski 		data |= ICE_SMA1_DIR_EN_E810T;
146325b2064SMaciej Machnikowski 	}
147325b2064SMaciej Machnikowski 
148325b2064SMaciej Machnikowski 	data &= ~ICE_SMA2_MASK_E810T;
149325b2064SMaciej Machnikowski 	if (ptp_pins[SMA2].func == PTP_PF_NONE &&
150325b2064SMaciej Machnikowski 	    ptp_pins[UFL2].func == PTP_PF_NONE) {
151325b2064SMaciej Machnikowski 		dev_info(ice_hw_to_dev(hw), "SMA2 + U.FL2 disabled");
152325b2064SMaciej Machnikowski 		data |= ICE_SMA2_MASK_E810T;
153325b2064SMaciej Machnikowski 	} else if (ptp_pins[SMA2].func == PTP_PF_EXTTS &&
154325b2064SMaciej Machnikowski 			ptp_pins[UFL2].func == PTP_PF_NONE) {
155325b2064SMaciej Machnikowski 		dev_info(ice_hw_to_dev(hw), "SMA2 RX");
156325b2064SMaciej Machnikowski 		data |= (ICE_SMA2_TX_EN_E810T |
157325b2064SMaciej Machnikowski 			 ICE_SMA2_UFL2_RX_DIS_E810T);
158325b2064SMaciej Machnikowski 	} else if (ptp_pins[SMA2].func == PTP_PF_NONE &&
159325b2064SMaciej Machnikowski 		   ptp_pins[UFL2].func == PTP_PF_EXTTS) {
160325b2064SMaciej Machnikowski 		dev_info(ice_hw_to_dev(hw), "UFL2 RX");
161325b2064SMaciej Machnikowski 		data |= (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T);
162325b2064SMaciej Machnikowski 	} else if (ptp_pins[SMA2].func == PTP_PF_PEROUT &&
163325b2064SMaciej Machnikowski 		   ptp_pins[UFL2].func == PTP_PF_NONE) {
164325b2064SMaciej Machnikowski 		dev_info(ice_hw_to_dev(hw), "SMA2 TX");
165325b2064SMaciej Machnikowski 		data |= (ICE_SMA2_DIR_EN_E810T |
166325b2064SMaciej Machnikowski 			 ICE_SMA2_UFL2_RX_DIS_E810T);
167325b2064SMaciej Machnikowski 	} else if (ptp_pins[SMA2].func == PTP_PF_PEROUT &&
168325b2064SMaciej Machnikowski 		   ptp_pins[UFL2].func == PTP_PF_EXTTS) {
169325b2064SMaciej Machnikowski 		dev_info(ice_hw_to_dev(hw), "SMA2 TX + U.FL2 RX");
170325b2064SMaciej Machnikowski 		data |= ICE_SMA2_DIR_EN_E810T;
171325b2064SMaciej Machnikowski 	}
172325b2064SMaciej Machnikowski 
173325b2064SMaciej Machnikowski 	return ice_write_sma_ctrl_e810t(hw, data);
174325b2064SMaciej Machnikowski }
175325b2064SMaciej Machnikowski 
176325b2064SMaciej Machnikowski /**
177325b2064SMaciej Machnikowski  * ice_ptp_set_sma_e810t
178325b2064SMaciej Machnikowski  * @info: the driver's PTP info structure
179325b2064SMaciej Machnikowski  * @pin: pin index in kernel structure
180325b2064SMaciej Machnikowski  * @func: Pin function to be set (PTP_PF_NONE, PTP_PF_EXTTS or PTP_PF_PEROUT)
181325b2064SMaciej Machnikowski  *
182325b2064SMaciej Machnikowski  * Set the configuration of a single SMA pin
183325b2064SMaciej Machnikowski  */
184325b2064SMaciej Machnikowski static int
185325b2064SMaciej Machnikowski ice_ptp_set_sma_e810t(struct ptp_clock_info *info, unsigned int pin,
186325b2064SMaciej Machnikowski 		      enum ptp_pin_function func)
187325b2064SMaciej Machnikowski {
188325b2064SMaciej Machnikowski 	struct ptp_pin_desc ptp_pins[NUM_PTP_PINS_E810T];
189325b2064SMaciej Machnikowski 	struct ice_pf *pf = ptp_info_to_pf(info);
190325b2064SMaciej Machnikowski 	struct ice_hw *hw = &pf->hw;
191325b2064SMaciej Machnikowski 	int err;
192325b2064SMaciej Machnikowski 
193325b2064SMaciej Machnikowski 	if (pin < SMA1 || func > PTP_PF_PEROUT)
194325b2064SMaciej Machnikowski 		return -EOPNOTSUPP;
195325b2064SMaciej Machnikowski 
196325b2064SMaciej Machnikowski 	err = ice_get_sma_config_e810t(hw, ptp_pins);
197325b2064SMaciej Machnikowski 	if (err)
198325b2064SMaciej Machnikowski 		return err;
199325b2064SMaciej Machnikowski 
200325b2064SMaciej Machnikowski 	/* Disable the same function on the other pin sharing the channel */
201325b2064SMaciej Machnikowski 	if (pin == SMA1 && ptp_pins[UFL1].func == func)
202325b2064SMaciej Machnikowski 		ptp_pins[UFL1].func = PTP_PF_NONE;
203325b2064SMaciej Machnikowski 	if (pin == UFL1 && ptp_pins[SMA1].func == func)
204325b2064SMaciej Machnikowski 		ptp_pins[SMA1].func = PTP_PF_NONE;
205325b2064SMaciej Machnikowski 
206325b2064SMaciej Machnikowski 	if (pin == SMA2 && ptp_pins[UFL2].func == func)
207325b2064SMaciej Machnikowski 		ptp_pins[UFL2].func = PTP_PF_NONE;
208325b2064SMaciej Machnikowski 	if (pin == UFL2 && ptp_pins[SMA2].func == func)
209325b2064SMaciej Machnikowski 		ptp_pins[SMA2].func = PTP_PF_NONE;
210325b2064SMaciej Machnikowski 
211325b2064SMaciej Machnikowski 	/* Set up new pin function in the temp table */
212325b2064SMaciej Machnikowski 	ptp_pins[pin].func = func;
213325b2064SMaciej Machnikowski 
214325b2064SMaciej Machnikowski 	return ice_ptp_set_sma_config_e810t(hw, ptp_pins);
215325b2064SMaciej Machnikowski }
216325b2064SMaciej Machnikowski 
217325b2064SMaciej Machnikowski /**
218325b2064SMaciej Machnikowski  * ice_verify_pin_e810t
219325b2064SMaciej Machnikowski  * @info: the driver's PTP info structure
220325b2064SMaciej Machnikowski  * @pin: Pin index
221325b2064SMaciej Machnikowski  * @func: Assigned function
222325b2064SMaciej Machnikowski  * @chan: Assigned channel
223325b2064SMaciej Machnikowski  *
224325b2064SMaciej Machnikowski  * Verify if pin supports requested pin function. If the Check pins consistency.
225325b2064SMaciej Machnikowski  * Reconfigure the SMA logic attached to the given pin to enable its
226325b2064SMaciej Machnikowski  * desired functionality
227325b2064SMaciej Machnikowski  */
228325b2064SMaciej Machnikowski static int
229325b2064SMaciej Machnikowski ice_verify_pin_e810t(struct ptp_clock_info *info, unsigned int pin,
230325b2064SMaciej Machnikowski 		     enum ptp_pin_function func, unsigned int chan)
231325b2064SMaciej Machnikowski {
232325b2064SMaciej Machnikowski 	/* Don't allow channel reassignment */
233325b2064SMaciej Machnikowski 	if (chan != ice_pin_desc_e810t[pin].chan)
234325b2064SMaciej Machnikowski 		return -EOPNOTSUPP;
235325b2064SMaciej Machnikowski 
236325b2064SMaciej Machnikowski 	/* Check if functions are properly assigned */
237325b2064SMaciej Machnikowski 	switch (func) {
238325b2064SMaciej Machnikowski 	case PTP_PF_NONE:
239325b2064SMaciej Machnikowski 		break;
240325b2064SMaciej Machnikowski 	case PTP_PF_EXTTS:
241325b2064SMaciej Machnikowski 		if (pin == UFL1)
242325b2064SMaciej Machnikowski 			return -EOPNOTSUPP;
243325b2064SMaciej Machnikowski 		break;
244325b2064SMaciej Machnikowski 	case PTP_PF_PEROUT:
245325b2064SMaciej Machnikowski 		if (pin == UFL2 || pin == GNSS)
246325b2064SMaciej Machnikowski 			return -EOPNOTSUPP;
247325b2064SMaciej Machnikowski 		break;
248325b2064SMaciej Machnikowski 	case PTP_PF_PHYSYNC:
249325b2064SMaciej Machnikowski 		return -EOPNOTSUPP;
250325b2064SMaciej Machnikowski 	}
251325b2064SMaciej Machnikowski 
252325b2064SMaciej Machnikowski 	return ice_ptp_set_sma_e810t(info, pin, func);
253325b2064SMaciej Machnikowski }
254325b2064SMaciej Machnikowski 
25506c16d89SJacob Keller /**
256ea9b847cSJacob Keller  * ice_set_tx_tstamp - Enable or disable Tx timestamping
257ea9b847cSJacob Keller  * @pf: The PF pointer to search in
258ea9b847cSJacob Keller  * @on: bool value for whether timestamps are enabled or disabled
259ea9b847cSJacob Keller  */
260ea9b847cSJacob Keller static void ice_set_tx_tstamp(struct ice_pf *pf, bool on)
261ea9b847cSJacob Keller {
262ea9b847cSJacob Keller 	struct ice_vsi *vsi;
263ea9b847cSJacob Keller 	u32 val;
264ea9b847cSJacob Keller 	u16 i;
265ea9b847cSJacob Keller 
266ea9b847cSJacob Keller 	vsi = ice_get_main_vsi(pf);
267ea9b847cSJacob Keller 	if (!vsi)
268ea9b847cSJacob Keller 		return;
269ea9b847cSJacob Keller 
270ea9b847cSJacob Keller 	/* Set the timestamp enable flag for all the Tx rings */
27184c5fb8cSJacob Keller 	ice_for_each_txq(vsi, i) {
272ea9b847cSJacob Keller 		if (!vsi->tx_rings[i])
273ea9b847cSJacob Keller 			continue;
274ea9b847cSJacob Keller 		vsi->tx_rings[i]->ptp_tx = on;
275ea9b847cSJacob Keller 	}
276ea9b847cSJacob Keller 
277ea9b847cSJacob Keller 	/* Configure the Tx timestamp interrupt */
278ea9b847cSJacob Keller 	val = rd32(&pf->hw, PFINT_OICR_ENA);
279ea9b847cSJacob Keller 	if (on)
280ea9b847cSJacob Keller 		val |= PFINT_OICR_TSYN_TX_M;
281ea9b847cSJacob Keller 	else
282ea9b847cSJacob Keller 		val &= ~PFINT_OICR_TSYN_TX_M;
283ea9b847cSJacob Keller 	wr32(&pf->hw, PFINT_OICR_ENA, val);
284ea9b847cSJacob Keller }
285ea9b847cSJacob Keller 
286ea9b847cSJacob Keller /**
28777a78115SJacob Keller  * ice_set_rx_tstamp - Enable or disable Rx timestamping
28877a78115SJacob Keller  * @pf: The PF pointer to search in
28977a78115SJacob Keller  * @on: bool value for whether timestamps are enabled or disabled
29077a78115SJacob Keller  */
29177a78115SJacob Keller static void ice_set_rx_tstamp(struct ice_pf *pf, bool on)
29277a78115SJacob Keller {
29377a78115SJacob Keller 	struct ice_vsi *vsi;
29477a78115SJacob Keller 	u16 i;
29577a78115SJacob Keller 
29677a78115SJacob Keller 	vsi = ice_get_main_vsi(pf);
29777a78115SJacob Keller 	if (!vsi)
29877a78115SJacob Keller 		return;
29977a78115SJacob Keller 
30077a78115SJacob Keller 	/* Set the timestamp flag for all the Rx rings */
30177a78115SJacob Keller 	ice_for_each_rxq(vsi, i) {
30277a78115SJacob Keller 		if (!vsi->rx_rings[i])
30377a78115SJacob Keller 			continue;
30477a78115SJacob Keller 		vsi->rx_rings[i]->ptp_rx = on;
30577a78115SJacob Keller 	}
30677a78115SJacob Keller }
30777a78115SJacob Keller 
30877a78115SJacob Keller /**
30977a78115SJacob Keller  * ice_ptp_cfg_timestamp - Configure timestamp for init/deinit
31077a78115SJacob Keller  * @pf: Board private structure
31177a78115SJacob Keller  * @ena: bool value to enable or disable time stamp
31277a78115SJacob Keller  *
31377a78115SJacob Keller  * This function will configure timestamping during PTP initialization
31477a78115SJacob Keller  * and deinitialization
31577a78115SJacob Keller  */
31677a78115SJacob Keller static void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena)
31777a78115SJacob Keller {
318ea9b847cSJacob Keller 	ice_set_tx_tstamp(pf, ena);
31977a78115SJacob Keller 	ice_set_rx_tstamp(pf, ena);
32077a78115SJacob Keller 
321ea9b847cSJacob Keller 	if (ena) {
32277a78115SJacob Keller 		pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_ALL;
323ea9b847cSJacob Keller 		pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_ON;
324ea9b847cSJacob Keller 	} else {
32577a78115SJacob Keller 		pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
326ea9b847cSJacob Keller 		pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_OFF;
327ea9b847cSJacob Keller 	}
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_update_cached_phctime - Update the cached PHC time values
49577a78115SJacob Keller  * @pf: Board specific private structure
49677a78115SJacob Keller  *
49777a78115SJacob Keller  * This function updates the system time values which are cached in the PF
49877a78115SJacob Keller  * structure and the Rx rings.
49977a78115SJacob Keller  *
50077a78115SJacob Keller  * This function must be called periodically to ensure that the cached value
50177a78115SJacob Keller  * is never more than 2 seconds old. It must also be called whenever the PHC
50277a78115SJacob Keller  * time has been changed.
50377a78115SJacob Keller  */
50477a78115SJacob Keller static void ice_ptp_update_cached_phctime(struct ice_pf *pf)
50577a78115SJacob Keller {
50677a78115SJacob Keller 	u64 systime;
50777a78115SJacob Keller 	int i;
50877a78115SJacob Keller 
50977a78115SJacob Keller 	/* Read the current PHC time */
51077a78115SJacob Keller 	systime = ice_ptp_read_src_clk_reg(pf, NULL);
51177a78115SJacob Keller 
51277a78115SJacob Keller 	/* Update the cached PHC time stored in the PF structure */
51377a78115SJacob Keller 	WRITE_ONCE(pf->ptp.cached_phc_time, systime);
51477a78115SJacob Keller 
51577a78115SJacob Keller 	ice_for_each_vsi(pf, i) {
51677a78115SJacob Keller 		struct ice_vsi *vsi = pf->vsi[i];
51777a78115SJacob Keller 		int j;
51877a78115SJacob Keller 
51977a78115SJacob Keller 		if (!vsi)
52077a78115SJacob Keller 			continue;
52177a78115SJacob Keller 
52277a78115SJacob Keller 		if (vsi->type != ICE_VSI_PF)
52377a78115SJacob Keller 			continue;
52477a78115SJacob Keller 
52577a78115SJacob Keller 		ice_for_each_rxq(vsi, j) {
52677a78115SJacob Keller 			if (!vsi->rx_rings[j])
52777a78115SJacob Keller 				continue;
52877a78115SJacob Keller 			WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime);
52977a78115SJacob Keller 		}
53077a78115SJacob Keller 	}
53177a78115SJacob Keller }
53277a78115SJacob Keller 
53377a78115SJacob Keller /**
53477a78115SJacob Keller  * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b
53577a78115SJacob Keller  * @cached_phc_time: recently cached copy of PHC time
53677a78115SJacob Keller  * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value
53777a78115SJacob Keller  *
53877a78115SJacob Keller  * Hardware captures timestamps which contain only 32 bits of nominal
53977a78115SJacob Keller  * nanoseconds, as opposed to the 64bit timestamps that the stack expects.
54077a78115SJacob Keller  * Note that the captured timestamp values may be 40 bits, but the lower
54177a78115SJacob Keller  * 8 bits are sub-nanoseconds and generally discarded.
54277a78115SJacob Keller  *
54377a78115SJacob Keller  * Extend the 32bit nanosecond timestamp using the following algorithm and
54477a78115SJacob Keller  * assumptions:
54577a78115SJacob Keller  *
54677a78115SJacob Keller  * 1) have a recently cached copy of the PHC time
54777a78115SJacob Keller  * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1
54877a78115SJacob Keller  *    seconds) before or after the PHC time was captured.
54977a78115SJacob Keller  * 3) calculate the delta between the cached time and the timestamp
55077a78115SJacob Keller  * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was
55177a78115SJacob Keller  *    captured after the PHC time. In this case, the full timestamp is just
55277a78115SJacob Keller  *    the cached PHC time plus the delta.
55377a78115SJacob Keller  * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the
55477a78115SJacob Keller  *    timestamp was captured *before* the PHC time, i.e. because the PHC
55577a78115SJacob Keller  *    cache was updated after the timestamp was captured by hardware. In this
55677a78115SJacob Keller  *    case, the full timestamp is the cached time minus the inverse delta.
55777a78115SJacob Keller  *
55877a78115SJacob Keller  * This algorithm works even if the PHC time was updated after a Tx timestamp
55977a78115SJacob Keller  * was requested, but before the Tx timestamp event was reported from
56077a78115SJacob Keller  * hardware.
56177a78115SJacob Keller  *
56277a78115SJacob Keller  * This calculation primarily relies on keeping the cached PHC time up to
56377a78115SJacob Keller  * date. If the timestamp was captured more than 2^31 nanoseconds after the
56477a78115SJacob Keller  * PHC time, it is possible that the lower 32bits of PHC time have
56577a78115SJacob Keller  * overflowed more than once, and we might generate an incorrect timestamp.
56677a78115SJacob Keller  *
56777a78115SJacob Keller  * This is prevented by (a) periodically updating the cached PHC time once
56877a78115SJacob Keller  * a second, and (b) discarding any Tx timestamp packet if it has waited for
56977a78115SJacob Keller  * a timestamp for more than one second.
57077a78115SJacob Keller  */
57177a78115SJacob Keller static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp)
57277a78115SJacob Keller {
57377a78115SJacob Keller 	u32 delta, phc_time_lo;
57477a78115SJacob Keller 	u64 ns;
57577a78115SJacob Keller 
57677a78115SJacob Keller 	/* Extract the lower 32 bits of the PHC time */
57777a78115SJacob Keller 	phc_time_lo = (u32)cached_phc_time;
57877a78115SJacob Keller 
57977a78115SJacob Keller 	/* Calculate the delta between the lower 32bits of the cached PHC
58077a78115SJacob Keller 	 * time and the in_tstamp value
58177a78115SJacob Keller 	 */
58277a78115SJacob Keller 	delta = (in_tstamp - phc_time_lo);
58377a78115SJacob Keller 
58477a78115SJacob Keller 	/* Do not assume that the in_tstamp is always more recent than the
58577a78115SJacob Keller 	 * cached PHC time. If the delta is large, it indicates that the
58677a78115SJacob Keller 	 * in_tstamp was taken in the past, and should be converted
58777a78115SJacob Keller 	 * forward.
58877a78115SJacob Keller 	 */
58977a78115SJacob Keller 	if (delta > (U32_MAX / 2)) {
59077a78115SJacob Keller 		/* reverse the delta calculation here */
59177a78115SJacob Keller 		delta = (phc_time_lo - in_tstamp);
59277a78115SJacob Keller 		ns = cached_phc_time - delta;
59377a78115SJacob Keller 	} else {
59477a78115SJacob Keller 		ns = cached_phc_time + delta;
59577a78115SJacob Keller 	}
59677a78115SJacob Keller 
59777a78115SJacob Keller 	return ns;
59877a78115SJacob Keller }
59977a78115SJacob Keller 
60077a78115SJacob Keller /**
601ea9b847cSJacob Keller  * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds
602ea9b847cSJacob Keller  * @pf: Board private structure
603ea9b847cSJacob Keller  * @in_tstamp: Ingress/egress 40b timestamp value
604ea9b847cSJacob Keller  *
605ea9b847cSJacob Keller  * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal
606ea9b847cSJacob Keller  * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit.
607ea9b847cSJacob Keller  *
608ea9b847cSJacob Keller  *  *--------------------------------------------------------------*
609ea9b847cSJacob Keller  *  | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v |
610ea9b847cSJacob Keller  *  *--------------------------------------------------------------*
611ea9b847cSJacob Keller  *
612ea9b847cSJacob Keller  * The low bit is an indicator of whether the timestamp is valid. The next
613ea9b847cSJacob Keller  * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow,
614ea9b847cSJacob Keller  * and the remaining 32 bits are the lower 32 bits of the PHC timer.
615ea9b847cSJacob Keller  *
616ea9b847cSJacob Keller  * It is assumed that the caller verifies the timestamp is valid prior to
617ea9b847cSJacob Keller  * calling this function.
618ea9b847cSJacob Keller  *
619ea9b847cSJacob Keller  * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC
620ea9b847cSJacob Keller  * time stored in the device private PTP structure as the basis for timestamp
621ea9b847cSJacob Keller  * extension.
622ea9b847cSJacob Keller  *
623ea9b847cSJacob Keller  * See ice_ptp_extend_32b_ts for a detailed explanation of the extension
624ea9b847cSJacob Keller  * algorithm.
625ea9b847cSJacob Keller  */
626ea9b847cSJacob Keller static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp)
627ea9b847cSJacob Keller {
628ea9b847cSJacob Keller 	const u64 mask = GENMASK_ULL(31, 0);
629ea9b847cSJacob Keller 
630ea9b847cSJacob Keller 	return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time,
631ea9b847cSJacob Keller 				     (in_tstamp >> 8) & mask);
632ea9b847cSJacob Keller }
633ea9b847cSJacob Keller 
634ea9b847cSJacob Keller /**
63506c16d89SJacob Keller  * ice_ptp_read_time - Read the time from the device
63606c16d89SJacob Keller  * @pf: Board private structure
63706c16d89SJacob Keller  * @ts: timespec structure to hold the current time value
63806c16d89SJacob Keller  * @sts: Optional parameter for holding a pair of system timestamps from
63906c16d89SJacob Keller  *       the system clock. Will be ignored if NULL is given.
64006c16d89SJacob Keller  *
64106c16d89SJacob Keller  * This function reads the source clock registers and stores them in a timespec.
64206c16d89SJacob Keller  * However, since the registers are 64 bits of nanoseconds, we must convert the
64306c16d89SJacob Keller  * result to a timespec before we can return.
64406c16d89SJacob Keller  */
64506c16d89SJacob Keller static void
64606c16d89SJacob Keller ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts,
64706c16d89SJacob Keller 		  struct ptp_system_timestamp *sts)
64806c16d89SJacob Keller {
64906c16d89SJacob Keller 	u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts);
65006c16d89SJacob Keller 
65106c16d89SJacob Keller 	*ts = ns_to_timespec64(time_ns);
65206c16d89SJacob Keller }
65306c16d89SJacob Keller 
65406c16d89SJacob Keller /**
65506c16d89SJacob Keller  * ice_ptp_write_init - Set PHC time to provided value
65606c16d89SJacob Keller  * @pf: Board private structure
65706c16d89SJacob Keller  * @ts: timespec structure that holds the new time value
65806c16d89SJacob Keller  *
65906c16d89SJacob Keller  * Set the PHC time to the specified time provided in the timespec.
66006c16d89SJacob Keller  */
66106c16d89SJacob Keller static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts)
66206c16d89SJacob Keller {
66306c16d89SJacob Keller 	u64 ns = timespec64_to_ns(ts);
66406c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
66506c16d89SJacob Keller 
66606c16d89SJacob Keller 	return ice_ptp_init_time(hw, ns);
66706c16d89SJacob Keller }
66806c16d89SJacob Keller 
66906c16d89SJacob Keller /**
67006c16d89SJacob Keller  * ice_ptp_write_adj - Adjust PHC clock time atomically
67106c16d89SJacob Keller  * @pf: Board private structure
67206c16d89SJacob Keller  * @adj: Adjustment in nanoseconds
67306c16d89SJacob Keller  *
67406c16d89SJacob Keller  * Perform an atomic adjustment of the PHC time by the specified number of
67506c16d89SJacob Keller  * nanoseconds.
67606c16d89SJacob Keller  */
67706c16d89SJacob Keller static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj)
67806c16d89SJacob Keller {
67906c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
68006c16d89SJacob Keller 
68106c16d89SJacob Keller 	return ice_ptp_adj_clock(hw, adj);
68206c16d89SJacob Keller }
68306c16d89SJacob Keller 
68406c16d89SJacob Keller /**
68506c16d89SJacob Keller  * ice_ptp_adjfine - Adjust clock increment rate
68606c16d89SJacob Keller  * @info: the driver's PTP info structure
68706c16d89SJacob Keller  * @scaled_ppm: Parts per million with 16-bit fractional field
68806c16d89SJacob Keller  *
68906c16d89SJacob Keller  * Adjust the frequency of the clock by the indicated scaled ppm from the
69006c16d89SJacob Keller  * base frequency.
69106c16d89SJacob Keller  */
69206c16d89SJacob Keller static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
69306c16d89SJacob Keller {
69406c16d89SJacob Keller 	struct ice_pf *pf = ptp_info_to_pf(info);
69506c16d89SJacob Keller 	u64 freq, divisor = 1000000ULL;
69606c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
69706c16d89SJacob Keller 	s64 incval, diff;
69806c16d89SJacob Keller 	int neg_adj = 0;
69906c16d89SJacob Keller 	int err;
70006c16d89SJacob Keller 
70106c16d89SJacob Keller 	incval = ICE_PTP_NOMINAL_INCVAL_E810;
70206c16d89SJacob Keller 
70306c16d89SJacob Keller 	if (scaled_ppm < 0) {
70406c16d89SJacob Keller 		neg_adj = 1;
70506c16d89SJacob Keller 		scaled_ppm = -scaled_ppm;
70606c16d89SJacob Keller 	}
70706c16d89SJacob Keller 
708*0013881cSKarol Kolacinski 	while ((u64)scaled_ppm > div64_u64(U64_MAX, incval)) {
70906c16d89SJacob Keller 		/* handle overflow by scaling down the scaled_ppm and
71006c16d89SJacob Keller 		 * the divisor, losing some precision
71106c16d89SJacob Keller 		 */
71206c16d89SJacob Keller 		scaled_ppm >>= 2;
71306c16d89SJacob Keller 		divisor >>= 2;
71406c16d89SJacob Keller 	}
71506c16d89SJacob Keller 
71606c16d89SJacob Keller 	freq = (incval * (u64)scaled_ppm) >> 16;
71706c16d89SJacob Keller 	diff = div_u64(freq, divisor);
71806c16d89SJacob Keller 
71906c16d89SJacob Keller 	if (neg_adj)
72006c16d89SJacob Keller 		incval -= diff;
72106c16d89SJacob Keller 	else
72206c16d89SJacob Keller 		incval += diff;
72306c16d89SJacob Keller 
72406c16d89SJacob Keller 	err = ice_ptp_write_incval_locked(hw, incval);
72506c16d89SJacob Keller 	if (err) {
72606c16d89SJacob Keller 		dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n",
72706c16d89SJacob Keller 			err);
72806c16d89SJacob Keller 		return -EIO;
72906c16d89SJacob Keller 	}
73006c16d89SJacob Keller 
73106c16d89SJacob Keller 	return 0;
73206c16d89SJacob Keller }
73306c16d89SJacob Keller 
73406c16d89SJacob Keller /**
735172db5f9SMaciej Machnikowski  * ice_ptp_extts_work - Workqueue task function
736172db5f9SMaciej Machnikowski  * @work: external timestamp work structure
737172db5f9SMaciej Machnikowski  *
738172db5f9SMaciej Machnikowski  * Service for PTP external clock event
739172db5f9SMaciej Machnikowski  */
740172db5f9SMaciej Machnikowski static void ice_ptp_extts_work(struct kthread_work *work)
741172db5f9SMaciej Machnikowski {
742172db5f9SMaciej Machnikowski 	struct ice_ptp *ptp = container_of(work, struct ice_ptp, extts_work);
743172db5f9SMaciej Machnikowski 	struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp);
744172db5f9SMaciej Machnikowski 	struct ptp_clock_event event;
745172db5f9SMaciej Machnikowski 	struct ice_hw *hw = &pf->hw;
746172db5f9SMaciej Machnikowski 	u8 chan, tmr_idx;
747172db5f9SMaciej Machnikowski 	u32 hi, lo;
748172db5f9SMaciej Machnikowski 
749172db5f9SMaciej Machnikowski 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
750172db5f9SMaciej Machnikowski 	/* Event time is captured by one of the two matched registers
751172db5f9SMaciej Machnikowski 	 *      GLTSYN_EVNT_L: 32 LSB of sampled time event
752172db5f9SMaciej Machnikowski 	 *      GLTSYN_EVNT_H: 32 MSB of sampled time event
753172db5f9SMaciej Machnikowski 	 * Event is defined in GLTSYN_EVNT_0 register
754172db5f9SMaciej Machnikowski 	 */
755172db5f9SMaciej Machnikowski 	for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) {
756172db5f9SMaciej Machnikowski 		/* Check if channel is enabled */
757172db5f9SMaciej Machnikowski 		if (pf->ptp.ext_ts_irq & (1 << chan)) {
758172db5f9SMaciej Machnikowski 			lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx));
759172db5f9SMaciej Machnikowski 			hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx));
760172db5f9SMaciej Machnikowski 			event.timestamp = (((u64)hi) << 32) | lo;
761172db5f9SMaciej Machnikowski 			event.type = PTP_CLOCK_EXTTS;
762172db5f9SMaciej Machnikowski 			event.index = chan;
763172db5f9SMaciej Machnikowski 
764172db5f9SMaciej Machnikowski 			/* Fire event */
765172db5f9SMaciej Machnikowski 			ptp_clock_event(pf->ptp.clock, &event);
766172db5f9SMaciej Machnikowski 			pf->ptp.ext_ts_irq &= ~(1 << chan);
767172db5f9SMaciej Machnikowski 		}
768172db5f9SMaciej Machnikowski 	}
769172db5f9SMaciej Machnikowski }
770172db5f9SMaciej Machnikowski 
771172db5f9SMaciej Machnikowski /**
772172db5f9SMaciej Machnikowski  * ice_ptp_cfg_extts - Configure EXTTS pin and channel
773172db5f9SMaciej Machnikowski  * @pf: Board private structure
774172db5f9SMaciej Machnikowski  * @ena: true to enable; false to disable
775172db5f9SMaciej Machnikowski  * @chan: GPIO channel (0-3)
776172db5f9SMaciej Machnikowski  * @gpio_pin: GPIO pin
777172db5f9SMaciej Machnikowski  * @extts_flags: request flags from the ptp_extts_request.flags
778172db5f9SMaciej Machnikowski  */
779172db5f9SMaciej Machnikowski static int
780172db5f9SMaciej Machnikowski ice_ptp_cfg_extts(struct ice_pf *pf, bool ena, unsigned int chan, u32 gpio_pin,
781172db5f9SMaciej Machnikowski 		  unsigned int extts_flags)
782172db5f9SMaciej Machnikowski {
783172db5f9SMaciej Machnikowski 	u32 func, aux_reg, gpio_reg, irq_reg;
784172db5f9SMaciej Machnikowski 	struct ice_hw *hw = &pf->hw;
785172db5f9SMaciej Machnikowski 	u8 tmr_idx;
786172db5f9SMaciej Machnikowski 
787172db5f9SMaciej Machnikowski 	if (chan > (unsigned int)pf->ptp.info.n_ext_ts)
788172db5f9SMaciej Machnikowski 		return -EINVAL;
789172db5f9SMaciej Machnikowski 
790172db5f9SMaciej Machnikowski 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
791172db5f9SMaciej Machnikowski 
792172db5f9SMaciej Machnikowski 	irq_reg = rd32(hw, PFINT_OICR_ENA);
793172db5f9SMaciej Machnikowski 
794172db5f9SMaciej Machnikowski 	if (ena) {
795172db5f9SMaciej Machnikowski 		/* Enable the interrupt */
796172db5f9SMaciej Machnikowski 		irq_reg |= PFINT_OICR_TSYN_EVNT_M;
797172db5f9SMaciej Machnikowski 		aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M;
798172db5f9SMaciej Machnikowski 
799172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE	BIT(0)
800172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE	BIT(1)
801172db5f9SMaciej Machnikowski 
802172db5f9SMaciej Machnikowski 		/* set event level to requested edge */
803172db5f9SMaciej Machnikowski 		if (extts_flags & PTP_FALLING_EDGE)
804172db5f9SMaciej Machnikowski 			aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE;
805172db5f9SMaciej Machnikowski 		if (extts_flags & PTP_RISING_EDGE)
806172db5f9SMaciej Machnikowski 			aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE;
807172db5f9SMaciej Machnikowski 
808172db5f9SMaciej Machnikowski 		/* Write GPIO CTL reg.
809172db5f9SMaciej Machnikowski 		 * 0x1 is input sampled by EVENT register(channel)
810172db5f9SMaciej Machnikowski 		 * + num_in_channels * tmr_idx
811172db5f9SMaciej Machnikowski 		 */
812172db5f9SMaciej Machnikowski 		func = 1 + chan + (tmr_idx * 3);
813172db5f9SMaciej Machnikowski 		gpio_reg = ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) &
814172db5f9SMaciej Machnikowski 			    GLGEN_GPIO_CTL_PIN_FUNC_M);
815172db5f9SMaciej Machnikowski 		pf->ptp.ext_ts_chan |= (1 << chan);
816172db5f9SMaciej Machnikowski 	} else {
817172db5f9SMaciej Machnikowski 		/* clear the values we set to reset defaults */
818172db5f9SMaciej Machnikowski 		aux_reg = 0;
819172db5f9SMaciej Machnikowski 		gpio_reg = 0;
820172db5f9SMaciej Machnikowski 		pf->ptp.ext_ts_chan &= ~(1 << chan);
821172db5f9SMaciej Machnikowski 		if (!pf->ptp.ext_ts_chan)
822172db5f9SMaciej Machnikowski 			irq_reg &= ~PFINT_OICR_TSYN_EVNT_M;
823172db5f9SMaciej Machnikowski 	}
824172db5f9SMaciej Machnikowski 
825172db5f9SMaciej Machnikowski 	wr32(hw, PFINT_OICR_ENA, irq_reg);
826172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg);
827172db5f9SMaciej Machnikowski 	wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg);
828172db5f9SMaciej Machnikowski 
829172db5f9SMaciej Machnikowski 	return 0;
830172db5f9SMaciej Machnikowski }
831172db5f9SMaciej Machnikowski 
832172db5f9SMaciej Machnikowski /**
833172db5f9SMaciej Machnikowski  * ice_ptp_cfg_clkout - Configure clock to generate periodic wave
834172db5f9SMaciej Machnikowski  * @pf: Board private structure
835172db5f9SMaciej Machnikowski  * @chan: GPIO channel (0-3)
836172db5f9SMaciej Machnikowski  * @config: desired periodic clk configuration. NULL will disable channel
837172db5f9SMaciej Machnikowski  * @store: If set to true the values will be stored
838172db5f9SMaciej Machnikowski  *
839172db5f9SMaciej Machnikowski  * Configure the internal clock generator modules to generate the clock wave of
840172db5f9SMaciej Machnikowski  * specified period.
841172db5f9SMaciej Machnikowski  */
842172db5f9SMaciej Machnikowski static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan,
843172db5f9SMaciej Machnikowski 			      struct ice_perout_channel *config, bool store)
844172db5f9SMaciej Machnikowski {
845172db5f9SMaciej Machnikowski 	u64 current_time, period, start_time, phase;
846172db5f9SMaciej Machnikowski 	struct ice_hw *hw = &pf->hw;
847172db5f9SMaciej Machnikowski 	u32 func, val, gpio_pin;
848172db5f9SMaciej Machnikowski 	u8 tmr_idx;
849172db5f9SMaciej Machnikowski 
850172db5f9SMaciej Machnikowski 	tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
851172db5f9SMaciej Machnikowski 
852172db5f9SMaciej Machnikowski 	/* 0. Reset mode & out_en in AUX_OUT */
853172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0);
854172db5f9SMaciej Machnikowski 
855172db5f9SMaciej Machnikowski 	/* If we're disabling the output, clear out CLKO and TGT and keep
856172db5f9SMaciej Machnikowski 	 * output level low
857172db5f9SMaciej Machnikowski 	 */
858172db5f9SMaciej Machnikowski 	if (!config || !config->ena) {
859172db5f9SMaciej Machnikowski 		wr32(hw, GLTSYN_CLKO(chan, tmr_idx), 0);
860172db5f9SMaciej Machnikowski 		wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), 0);
861172db5f9SMaciej Machnikowski 		wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), 0);
862172db5f9SMaciej Machnikowski 
863172db5f9SMaciej Machnikowski 		val = GLGEN_GPIO_CTL_PIN_DIR_M;
864172db5f9SMaciej Machnikowski 		gpio_pin = pf->ptp.perout_channels[chan].gpio_pin;
865172db5f9SMaciej Machnikowski 		wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
866172db5f9SMaciej Machnikowski 
867172db5f9SMaciej Machnikowski 		/* Store the value if requested */
868172db5f9SMaciej Machnikowski 		if (store)
869172db5f9SMaciej Machnikowski 			memset(&pf->ptp.perout_channels[chan], 0,
870172db5f9SMaciej Machnikowski 			       sizeof(struct ice_perout_channel));
871172db5f9SMaciej Machnikowski 
872172db5f9SMaciej Machnikowski 		return 0;
873172db5f9SMaciej Machnikowski 	}
874172db5f9SMaciej Machnikowski 	period = config->period;
875172db5f9SMaciej Machnikowski 	start_time = config->start_time;
876172db5f9SMaciej Machnikowski 	div64_u64_rem(start_time, period, &phase);
877172db5f9SMaciej Machnikowski 	gpio_pin = config->gpio_pin;
878172db5f9SMaciej Machnikowski 
879172db5f9SMaciej Machnikowski 	/* 1. Write clkout with half of required period value */
880172db5f9SMaciej Machnikowski 	if (period & 0x1) {
881172db5f9SMaciej Machnikowski 		dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n");
882172db5f9SMaciej Machnikowski 		goto err;
883172db5f9SMaciej Machnikowski 	}
884172db5f9SMaciej Machnikowski 
885172db5f9SMaciej Machnikowski 	period >>= 1;
886172db5f9SMaciej Machnikowski 
887172db5f9SMaciej Machnikowski 	/* For proper operation, the GLTSYN_CLKO must be larger than clock tick
888172db5f9SMaciej Machnikowski 	 */
889172db5f9SMaciej Machnikowski #define MIN_PULSE 3
890172db5f9SMaciej Machnikowski 	if (period <= MIN_PULSE || period > U32_MAX) {
891172db5f9SMaciej Machnikowski 		dev_err(ice_pf_to_dev(pf), "CLK Period must be > %d && < 2^33",
892172db5f9SMaciej Machnikowski 			MIN_PULSE * 2);
893172db5f9SMaciej Machnikowski 		goto err;
894172db5f9SMaciej Machnikowski 	}
895172db5f9SMaciej Machnikowski 
896172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period));
897172db5f9SMaciej Machnikowski 
898172db5f9SMaciej Machnikowski 	/* Allow time for programming before start_time is hit */
899172db5f9SMaciej Machnikowski 	current_time = ice_ptp_read_src_clk_reg(pf, NULL);
900172db5f9SMaciej Machnikowski 
901172db5f9SMaciej Machnikowski 	/* if start time is in the past start the timer at the nearest second
902172db5f9SMaciej Machnikowski 	 * maintaining phase
903172db5f9SMaciej Machnikowski 	 */
904172db5f9SMaciej Machnikowski 	if (start_time < current_time)
9055f773519SMaciej Machnikowski 		start_time = div64_u64(current_time + NSEC_PER_SEC - 1,
906172db5f9SMaciej Machnikowski 				       NSEC_PER_SEC) * NSEC_PER_SEC + phase;
907172db5f9SMaciej Machnikowski 
908172db5f9SMaciej Machnikowski 	start_time -= E810_OUT_PROP_DELAY_NS;
909172db5f9SMaciej Machnikowski 
910172db5f9SMaciej Machnikowski 	/* 2. Write TARGET time */
911172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start_time));
912172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start_time));
913172db5f9SMaciej Machnikowski 
914172db5f9SMaciej Machnikowski 	/* 3. Write AUX_OUT register */
915172db5f9SMaciej Machnikowski 	val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M;
916172db5f9SMaciej Machnikowski 	wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val);
917172db5f9SMaciej Machnikowski 
918172db5f9SMaciej Machnikowski 	/* 4. write GPIO CTL reg */
919172db5f9SMaciej Machnikowski 	func = 8 + chan + (tmr_idx * 4);
920172db5f9SMaciej Machnikowski 	val = GLGEN_GPIO_CTL_PIN_DIR_M |
921172db5f9SMaciej Machnikowski 	      ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & GLGEN_GPIO_CTL_PIN_FUNC_M);
922172db5f9SMaciej Machnikowski 	wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
923172db5f9SMaciej Machnikowski 
924172db5f9SMaciej Machnikowski 	/* Store the value if requested */
925172db5f9SMaciej Machnikowski 	if (store) {
926172db5f9SMaciej Machnikowski 		memcpy(&pf->ptp.perout_channels[chan], config,
927172db5f9SMaciej Machnikowski 		       sizeof(struct ice_perout_channel));
928172db5f9SMaciej Machnikowski 		pf->ptp.perout_channels[chan].start_time = phase;
929172db5f9SMaciej Machnikowski 	}
930172db5f9SMaciej Machnikowski 
931172db5f9SMaciej Machnikowski 	return 0;
932172db5f9SMaciej Machnikowski err:
933172db5f9SMaciej Machnikowski 	dev_err(ice_pf_to_dev(pf), "PTP failed to cfg per_clk\n");
934172db5f9SMaciej Machnikowski 	return -EFAULT;
935172db5f9SMaciej Machnikowski }
936172db5f9SMaciej Machnikowski 
937172db5f9SMaciej Machnikowski /**
9389ee31343SJacob Keller  * ice_ptp_disable_all_clkout - Disable all currently configured outputs
9399ee31343SJacob Keller  * @pf: pointer to the PF structure
9409ee31343SJacob Keller  *
9419ee31343SJacob Keller  * Disable all currently configured clock outputs. This is necessary before
9429ee31343SJacob Keller  * certain changes to the PTP hardware clock. Use ice_ptp_enable_all_clkout to
9439ee31343SJacob Keller  * re-enable the clocks again.
9449ee31343SJacob Keller  */
9459ee31343SJacob Keller static void ice_ptp_disable_all_clkout(struct ice_pf *pf)
9469ee31343SJacob Keller {
9479ee31343SJacob Keller 	uint i;
9489ee31343SJacob Keller 
9499ee31343SJacob Keller 	for (i = 0; i < pf->ptp.info.n_per_out; i++)
9509ee31343SJacob Keller 		if (pf->ptp.perout_channels[i].ena)
9519ee31343SJacob Keller 			ice_ptp_cfg_clkout(pf, i, NULL, false);
9529ee31343SJacob Keller }
9539ee31343SJacob Keller 
9549ee31343SJacob Keller /**
9559ee31343SJacob Keller  * ice_ptp_enable_all_clkout - Enable all configured periodic clock outputs
9569ee31343SJacob Keller  * @pf: pointer to the PF structure
9579ee31343SJacob Keller  *
9589ee31343SJacob Keller  * Enable all currently configured clock outputs. Use this after
9599ee31343SJacob Keller  * ice_ptp_disable_all_clkout to reconfigure the output signals according to
9609ee31343SJacob Keller  * their configuration.
9619ee31343SJacob Keller  */
9629ee31343SJacob Keller static void ice_ptp_enable_all_clkout(struct ice_pf *pf)
9639ee31343SJacob Keller {
9649ee31343SJacob Keller 	uint i;
9659ee31343SJacob Keller 
9669ee31343SJacob Keller 	for (i = 0; i < pf->ptp.info.n_per_out; i++)
9679ee31343SJacob Keller 		if (pf->ptp.perout_channels[i].ena)
9689ee31343SJacob Keller 			ice_ptp_cfg_clkout(pf, i, &pf->ptp.perout_channels[i],
9699ee31343SJacob Keller 					   false);
9709ee31343SJacob Keller }
9719ee31343SJacob Keller 
9729ee31343SJacob Keller /**
973172db5f9SMaciej Machnikowski  * ice_ptp_gpio_enable_e810 - Enable/disable ancillary features of PHC
974172db5f9SMaciej Machnikowski  * @info: the driver's PTP info structure
975172db5f9SMaciej Machnikowski  * @rq: The requested feature to change
976172db5f9SMaciej Machnikowski  * @on: Enable/disable flag
977172db5f9SMaciej Machnikowski  */
978172db5f9SMaciej Machnikowski static int
979172db5f9SMaciej Machnikowski ice_ptp_gpio_enable_e810(struct ptp_clock_info *info,
980172db5f9SMaciej Machnikowski 			 struct ptp_clock_request *rq, int on)
981172db5f9SMaciej Machnikowski {
982172db5f9SMaciej Machnikowski 	struct ice_pf *pf = ptp_info_to_pf(info);
983172db5f9SMaciej Machnikowski 	struct ice_perout_channel clk_cfg = {0};
984325b2064SMaciej Machnikowski 	bool sma_pres = false;
985172db5f9SMaciej Machnikowski 	unsigned int chan;
986172db5f9SMaciej Machnikowski 	u32 gpio_pin;
987172db5f9SMaciej Machnikowski 	int err;
988172db5f9SMaciej Machnikowski 
989325b2064SMaciej Machnikowski 	if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL))
990325b2064SMaciej Machnikowski 		sma_pres = true;
991325b2064SMaciej Machnikowski 
992172db5f9SMaciej Machnikowski 	switch (rq->type) {
993172db5f9SMaciej Machnikowski 	case PTP_CLK_REQ_PEROUT:
994172db5f9SMaciej Machnikowski 		chan = rq->perout.index;
995325b2064SMaciej Machnikowski 		if (sma_pres) {
996325b2064SMaciej Machnikowski 			if (chan == ice_pin_desc_e810t[SMA1].chan)
997325b2064SMaciej Machnikowski 				clk_cfg.gpio_pin = GPIO_20;
998325b2064SMaciej Machnikowski 			else if (chan == ice_pin_desc_e810t[SMA2].chan)
999325b2064SMaciej Machnikowski 				clk_cfg.gpio_pin = GPIO_22;
1000172db5f9SMaciej Machnikowski 			else
1001325b2064SMaciej Machnikowski 				return -1;
1002325b2064SMaciej Machnikowski 		} else if (ice_is_e810t(&pf->hw)) {
1003325b2064SMaciej Machnikowski 			if (chan == 0)
1004325b2064SMaciej Machnikowski 				clk_cfg.gpio_pin = GPIO_20;
1005325b2064SMaciej Machnikowski 			else
1006325b2064SMaciej Machnikowski 				clk_cfg.gpio_pin = GPIO_22;
1007325b2064SMaciej Machnikowski 		} else if (chan == PPS_CLK_GEN_CHAN) {
1008325b2064SMaciej Machnikowski 			clk_cfg.gpio_pin = PPS_PIN_INDEX;
1009325b2064SMaciej Machnikowski 		} else {
1010172db5f9SMaciej Machnikowski 			clk_cfg.gpio_pin = chan;
1011325b2064SMaciej Machnikowski 		}
1012172db5f9SMaciej Machnikowski 
1013172db5f9SMaciej Machnikowski 		clk_cfg.period = ((rq->perout.period.sec * NSEC_PER_SEC) +
1014172db5f9SMaciej Machnikowski 				   rq->perout.period.nsec);
1015172db5f9SMaciej Machnikowski 		clk_cfg.start_time = ((rq->perout.start.sec * NSEC_PER_SEC) +
1016172db5f9SMaciej Machnikowski 				       rq->perout.start.nsec);
1017172db5f9SMaciej Machnikowski 		clk_cfg.ena = !!on;
1018172db5f9SMaciej Machnikowski 
1019172db5f9SMaciej Machnikowski 		err = ice_ptp_cfg_clkout(pf, chan, &clk_cfg, true);
1020172db5f9SMaciej Machnikowski 		break;
1021172db5f9SMaciej Machnikowski 	case PTP_CLK_REQ_EXTTS:
1022172db5f9SMaciej Machnikowski 		chan = rq->extts.index;
1023325b2064SMaciej Machnikowski 		if (sma_pres) {
1024325b2064SMaciej Machnikowski 			if (chan < ice_pin_desc_e810t[SMA2].chan)
1025325b2064SMaciej Machnikowski 				gpio_pin = GPIO_21;
1026325b2064SMaciej Machnikowski 			else
1027325b2064SMaciej Machnikowski 				gpio_pin = GPIO_23;
1028325b2064SMaciej Machnikowski 		} else if (ice_is_e810t(&pf->hw)) {
1029325b2064SMaciej Machnikowski 			if (chan == 0)
1030325b2064SMaciej Machnikowski 				gpio_pin = GPIO_21;
1031325b2064SMaciej Machnikowski 			else
1032325b2064SMaciej Machnikowski 				gpio_pin = GPIO_23;
1033325b2064SMaciej Machnikowski 		} else {
1034172db5f9SMaciej Machnikowski 			gpio_pin = chan;
1035325b2064SMaciej Machnikowski 		}
1036172db5f9SMaciej Machnikowski 
1037172db5f9SMaciej Machnikowski 		err = ice_ptp_cfg_extts(pf, !!on, chan, gpio_pin,
1038172db5f9SMaciej Machnikowski 					rq->extts.flags);
1039172db5f9SMaciej Machnikowski 		break;
1040172db5f9SMaciej Machnikowski 	default:
1041172db5f9SMaciej Machnikowski 		return -EOPNOTSUPP;
1042172db5f9SMaciej Machnikowski 	}
1043172db5f9SMaciej Machnikowski 
1044172db5f9SMaciej Machnikowski 	return err;
1045172db5f9SMaciej Machnikowski }
1046172db5f9SMaciej Machnikowski 
1047172db5f9SMaciej Machnikowski /**
104806c16d89SJacob Keller  * ice_ptp_gettimex64 - Get the time of the clock
104906c16d89SJacob Keller  * @info: the driver's PTP info structure
105006c16d89SJacob Keller  * @ts: timespec64 structure to hold the current time value
105106c16d89SJacob Keller  * @sts: Optional parameter for holding a pair of system timestamps from
105206c16d89SJacob Keller  *       the system clock. Will be ignored if NULL is given.
105306c16d89SJacob Keller  *
105406c16d89SJacob Keller  * Read the device clock and return the correct value on ns, after converting it
105506c16d89SJacob Keller  * into a timespec struct.
105606c16d89SJacob Keller  */
105706c16d89SJacob Keller static int
105806c16d89SJacob Keller ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts,
105906c16d89SJacob Keller 		   struct ptp_system_timestamp *sts)
106006c16d89SJacob Keller {
106106c16d89SJacob Keller 	struct ice_pf *pf = ptp_info_to_pf(info);
106206c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
106306c16d89SJacob Keller 
106406c16d89SJacob Keller 	if (!ice_ptp_lock(hw)) {
106506c16d89SJacob Keller 		dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n");
106606c16d89SJacob Keller 		return -EBUSY;
106706c16d89SJacob Keller 	}
106806c16d89SJacob Keller 
106906c16d89SJacob Keller 	ice_ptp_read_time(pf, ts, sts);
107006c16d89SJacob Keller 	ice_ptp_unlock(hw);
107106c16d89SJacob Keller 
107206c16d89SJacob Keller 	return 0;
107306c16d89SJacob Keller }
107406c16d89SJacob Keller 
107506c16d89SJacob Keller /**
107606c16d89SJacob Keller  * ice_ptp_settime64 - Set the time of the clock
107706c16d89SJacob Keller  * @info: the driver's PTP info structure
107806c16d89SJacob Keller  * @ts: timespec64 structure that holds the new time value
107906c16d89SJacob Keller  *
108006c16d89SJacob Keller  * Set the device clock to the user input value. The conversion from timespec
108106c16d89SJacob Keller  * to ns happens in the write function.
108206c16d89SJacob Keller  */
108306c16d89SJacob Keller static int
108406c16d89SJacob Keller ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts)
108506c16d89SJacob Keller {
108606c16d89SJacob Keller 	struct ice_pf *pf = ptp_info_to_pf(info);
108706c16d89SJacob Keller 	struct timespec64 ts64 = *ts;
108806c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
108906c16d89SJacob Keller 	int err;
109006c16d89SJacob Keller 
109106c16d89SJacob Keller 	if (!ice_ptp_lock(hw)) {
109206c16d89SJacob Keller 		err = -EBUSY;
109306c16d89SJacob Keller 		goto exit;
109406c16d89SJacob Keller 	}
109506c16d89SJacob Keller 
10969ee31343SJacob Keller 	/* Disable periodic outputs */
10979ee31343SJacob Keller 	ice_ptp_disable_all_clkout(pf);
10989ee31343SJacob Keller 
109906c16d89SJacob Keller 	err = ice_ptp_write_init(pf, &ts64);
110006c16d89SJacob Keller 	ice_ptp_unlock(hw);
110106c16d89SJacob Keller 
110277a78115SJacob Keller 	if (!err)
110377a78115SJacob Keller 		ice_ptp_update_cached_phctime(pf);
110477a78115SJacob Keller 
11059ee31343SJacob Keller 	/* Reenable periodic outputs */
11069ee31343SJacob Keller 	ice_ptp_enable_all_clkout(pf);
110706c16d89SJacob Keller exit:
110806c16d89SJacob Keller 	if (err) {
110906c16d89SJacob Keller 		dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err);
111006c16d89SJacob Keller 		return err;
111106c16d89SJacob Keller 	}
111206c16d89SJacob Keller 
111306c16d89SJacob Keller 	return 0;
111406c16d89SJacob Keller }
111506c16d89SJacob Keller 
111606c16d89SJacob Keller /**
111706c16d89SJacob Keller  * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment
111806c16d89SJacob Keller  * @info: the driver's PTP info structure
111906c16d89SJacob Keller  * @delta: Offset in nanoseconds to adjust the time by
112006c16d89SJacob Keller  */
112106c16d89SJacob Keller static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta)
112206c16d89SJacob Keller {
112306c16d89SJacob Keller 	struct timespec64 now, then;
112406c16d89SJacob Keller 
112506c16d89SJacob Keller 	then = ns_to_timespec64(delta);
112606c16d89SJacob Keller 	ice_ptp_gettimex64(info, &now, NULL);
112706c16d89SJacob Keller 	now = timespec64_add(now, then);
112806c16d89SJacob Keller 
112906c16d89SJacob Keller 	return ice_ptp_settime64(info, (const struct timespec64 *)&now);
113006c16d89SJacob Keller }
113106c16d89SJacob Keller 
113206c16d89SJacob Keller /**
113306c16d89SJacob Keller  * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta
113406c16d89SJacob Keller  * @info: the driver's PTP info structure
113506c16d89SJacob Keller  * @delta: Offset in nanoseconds to adjust the time by
113606c16d89SJacob Keller  */
113706c16d89SJacob Keller static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
113806c16d89SJacob Keller {
113906c16d89SJacob Keller 	struct ice_pf *pf = ptp_info_to_pf(info);
114006c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
114106c16d89SJacob Keller 	struct device *dev;
114206c16d89SJacob Keller 	int err;
114306c16d89SJacob Keller 
114406c16d89SJacob Keller 	dev = ice_pf_to_dev(pf);
114506c16d89SJacob Keller 
114606c16d89SJacob Keller 	/* Hardware only supports atomic adjustments using signed 32-bit
114706c16d89SJacob Keller 	 * integers. For any adjustment outside this range, perform
114806c16d89SJacob Keller 	 * a non-atomic get->adjust->set flow.
114906c16d89SJacob Keller 	 */
115006c16d89SJacob Keller 	if (delta > S32_MAX || delta < S32_MIN) {
115106c16d89SJacob Keller 		dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta);
115206c16d89SJacob Keller 		return ice_ptp_adjtime_nonatomic(info, delta);
115306c16d89SJacob Keller 	}
115406c16d89SJacob Keller 
115506c16d89SJacob Keller 	if (!ice_ptp_lock(hw)) {
115606c16d89SJacob Keller 		dev_err(dev, "PTP failed to acquire semaphore in adjtime\n");
115706c16d89SJacob Keller 		return -EBUSY;
115806c16d89SJacob Keller 	}
115906c16d89SJacob Keller 
11609ee31343SJacob Keller 	/* Disable periodic outputs */
11619ee31343SJacob Keller 	ice_ptp_disable_all_clkout(pf);
11629ee31343SJacob Keller 
116306c16d89SJacob Keller 	err = ice_ptp_write_adj(pf, delta);
116406c16d89SJacob Keller 
11659ee31343SJacob Keller 	/* Reenable periodic outputs */
11669ee31343SJacob Keller 	ice_ptp_enable_all_clkout(pf);
11679ee31343SJacob Keller 
116806c16d89SJacob Keller 	ice_ptp_unlock(hw);
116906c16d89SJacob Keller 
117006c16d89SJacob Keller 	if (err) {
117106c16d89SJacob Keller 		dev_err(dev, "PTP failed to adjust time, err %d\n", err);
117206c16d89SJacob Keller 		return err;
117306c16d89SJacob Keller 	}
117406c16d89SJacob Keller 
117577a78115SJacob Keller 	ice_ptp_update_cached_phctime(pf);
117677a78115SJacob Keller 
117706c16d89SJacob Keller 	return 0;
117806c16d89SJacob Keller }
117906c16d89SJacob Keller 
118006c16d89SJacob Keller /**
118177a78115SJacob Keller  * ice_ptp_get_ts_config - ioctl interface to read the timestamping config
118277a78115SJacob Keller  * @pf: Board private structure
118377a78115SJacob Keller  * @ifr: ioctl data
118477a78115SJacob Keller  *
118577a78115SJacob Keller  * Copy the timestamping config to user buffer
118677a78115SJacob Keller  */
118777a78115SJacob Keller int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
118877a78115SJacob Keller {
118977a78115SJacob Keller 	struct hwtstamp_config *config;
119077a78115SJacob Keller 
119177a78115SJacob Keller 	if (!test_bit(ICE_FLAG_PTP, pf->flags))
119277a78115SJacob Keller 		return -EIO;
119377a78115SJacob Keller 
119477a78115SJacob Keller 	config = &pf->ptp.tstamp_config;
119577a78115SJacob Keller 
119677a78115SJacob Keller 	return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
119777a78115SJacob Keller 		-EFAULT : 0;
119877a78115SJacob Keller }
119977a78115SJacob Keller 
120077a78115SJacob Keller /**
120177a78115SJacob Keller  * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode
120277a78115SJacob Keller  * @pf: Board private structure
120377a78115SJacob Keller  * @config: hwtstamp settings requested or saved
120477a78115SJacob Keller  */
120577a78115SJacob Keller static int
120677a78115SJacob Keller ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config)
120777a78115SJacob Keller {
120877a78115SJacob Keller 	/* Reserved for future extensions. */
120977a78115SJacob Keller 	if (config->flags)
121077a78115SJacob Keller 		return -EINVAL;
121177a78115SJacob Keller 
121277a78115SJacob Keller 	switch (config->tx_type) {
121377a78115SJacob Keller 	case HWTSTAMP_TX_OFF:
1214ea9b847cSJacob Keller 		ice_set_tx_tstamp(pf, false);
1215ea9b847cSJacob Keller 		break;
1216ea9b847cSJacob Keller 	case HWTSTAMP_TX_ON:
1217ea9b847cSJacob Keller 		ice_set_tx_tstamp(pf, true);
121877a78115SJacob Keller 		break;
121977a78115SJacob Keller 	default:
122077a78115SJacob Keller 		return -ERANGE;
122177a78115SJacob Keller 	}
122277a78115SJacob Keller 
122377a78115SJacob Keller 	switch (config->rx_filter) {
122477a78115SJacob Keller 	case HWTSTAMP_FILTER_NONE:
122577a78115SJacob Keller 		ice_set_rx_tstamp(pf, false);
122677a78115SJacob Keller 		break;
122777a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
122877a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
122977a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
123077a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
123177a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
123277a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
123377a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
123477a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
123577a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
123677a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
123777a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
123877a78115SJacob Keller 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
123977a78115SJacob Keller 	case HWTSTAMP_FILTER_NTP_ALL:
124077a78115SJacob Keller 	case HWTSTAMP_FILTER_ALL:
124177a78115SJacob Keller 		config->rx_filter = HWTSTAMP_FILTER_ALL;
124277a78115SJacob Keller 		ice_set_rx_tstamp(pf, true);
124377a78115SJacob Keller 		break;
124477a78115SJacob Keller 	default:
124577a78115SJacob Keller 		return -ERANGE;
124677a78115SJacob Keller 	}
124777a78115SJacob Keller 
124877a78115SJacob Keller 	return 0;
124977a78115SJacob Keller }
125077a78115SJacob Keller 
125177a78115SJacob Keller /**
125277a78115SJacob Keller  * ice_ptp_set_ts_config - ioctl interface to control the timestamping
125377a78115SJacob Keller  * @pf: Board private structure
125477a78115SJacob Keller  * @ifr: ioctl data
125577a78115SJacob Keller  *
125677a78115SJacob Keller  * Get the user config and store it
125777a78115SJacob Keller  */
125877a78115SJacob Keller int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
125977a78115SJacob Keller {
126077a78115SJacob Keller 	struct hwtstamp_config config;
126177a78115SJacob Keller 	int err;
126277a78115SJacob Keller 
126377a78115SJacob Keller 	if (!test_bit(ICE_FLAG_PTP, pf->flags))
126477a78115SJacob Keller 		return -EAGAIN;
126577a78115SJacob Keller 
126677a78115SJacob Keller 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
126777a78115SJacob Keller 		return -EFAULT;
126877a78115SJacob Keller 
126977a78115SJacob Keller 	err = ice_ptp_set_timestamp_mode(pf, &config);
127077a78115SJacob Keller 	if (err)
127177a78115SJacob Keller 		return err;
127277a78115SJacob Keller 
127377a78115SJacob Keller 	/* Save these settings for future reference */
127477a78115SJacob Keller 	pf->ptp.tstamp_config = config;
127577a78115SJacob Keller 
127677a78115SJacob Keller 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
127777a78115SJacob Keller 		-EFAULT : 0;
127877a78115SJacob Keller }
127977a78115SJacob Keller 
128077a78115SJacob Keller /**
128177a78115SJacob Keller  * ice_ptp_rx_hwtstamp - Check for an Rx timestamp
128277a78115SJacob Keller  * @rx_ring: Ring to get the VSI info
128377a78115SJacob Keller  * @rx_desc: Receive descriptor
128477a78115SJacob Keller  * @skb: Particular skb to send timestamp with
128577a78115SJacob Keller  *
128677a78115SJacob Keller  * The driver receives a notification in the receive descriptor with timestamp.
128777a78115SJacob Keller  * The timestamp is in ns, so we must convert the result first.
128877a78115SJacob Keller  */
128977a78115SJacob Keller void
1290e72bba21SMaciej Fijalkowski ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring,
129177a78115SJacob Keller 		    union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb)
129277a78115SJacob Keller {
129377a78115SJacob Keller 	u32 ts_high;
129477a78115SJacob Keller 	u64 ts_ns;
129577a78115SJacob Keller 
129677a78115SJacob Keller 	/* Populate timesync data into skb */
129777a78115SJacob Keller 	if (rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID) {
129877a78115SJacob Keller 		struct skb_shared_hwtstamps *hwtstamps;
129977a78115SJacob Keller 
130077a78115SJacob Keller 		/* Use ice_ptp_extend_32b_ts directly, using the ring-specific
130177a78115SJacob Keller 		 * cached PHC value, rather than accessing the PF. This also
130277a78115SJacob Keller 		 * allows us to simply pass the upper 32bits of nanoseconds
130377a78115SJacob Keller 		 * directly. Calling ice_ptp_extend_40b_ts is unnecessary as
130477a78115SJacob Keller 		 * it would just discard these bits itself.
130577a78115SJacob Keller 		 */
130677a78115SJacob Keller 		ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high);
130777a78115SJacob Keller 		ts_ns = ice_ptp_extend_32b_ts(rx_ring->cached_phctime, ts_high);
130877a78115SJacob Keller 
130977a78115SJacob Keller 		hwtstamps = skb_hwtstamps(skb);
131077a78115SJacob Keller 		memset(hwtstamps, 0, sizeof(*hwtstamps));
131177a78115SJacob Keller 		hwtstamps->hwtstamp = ns_to_ktime(ts_ns);
131277a78115SJacob Keller 	}
131377a78115SJacob Keller }
131477a78115SJacob Keller 
131577a78115SJacob Keller /**
1316325b2064SMaciej Machnikowski  * ice_ptp_disable_sma_pins_e810t - Disable E810-T SMA pins
1317325b2064SMaciej Machnikowski  * @pf: pointer to the PF structure
1318325b2064SMaciej Machnikowski  * @info: PTP clock info structure
1319325b2064SMaciej Machnikowski  *
1320325b2064SMaciej Machnikowski  * Disable the OS access to the SMA pins. Called to clear out the OS
1321325b2064SMaciej Machnikowski  * indications of pin support when we fail to setup the E810-T SMA control
1322325b2064SMaciej Machnikowski  * register.
1323325b2064SMaciej Machnikowski  */
1324325b2064SMaciej Machnikowski static void
1325325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info)
1326325b2064SMaciej Machnikowski {
1327325b2064SMaciej Machnikowski 	struct device *dev = ice_pf_to_dev(pf);
1328325b2064SMaciej Machnikowski 
1329325b2064SMaciej Machnikowski 	dev_warn(dev, "Failed to configure E810-T SMA pin control\n");
1330325b2064SMaciej Machnikowski 
1331325b2064SMaciej Machnikowski 	info->enable = NULL;
1332325b2064SMaciej Machnikowski 	info->verify = NULL;
1333325b2064SMaciej Machnikowski 	info->n_pins = 0;
1334325b2064SMaciej Machnikowski 	info->n_ext_ts = 0;
1335325b2064SMaciej Machnikowski 	info->n_per_out = 0;
1336325b2064SMaciej Machnikowski }
1337325b2064SMaciej Machnikowski 
1338325b2064SMaciej Machnikowski /**
1339325b2064SMaciej Machnikowski  * ice_ptp_setup_sma_pins_e810t - Setup the SMA pins
1340325b2064SMaciej Machnikowski  * @pf: pointer to the PF structure
1341325b2064SMaciej Machnikowski  * @info: PTP clock info structure
1342325b2064SMaciej Machnikowski  *
1343325b2064SMaciej Machnikowski  * Finish setting up the SMA pins by allocating pin_config, and setting it up
1344325b2064SMaciej Machnikowski  * according to the current status of the SMA. On failure, disable all of the
1345325b2064SMaciej Machnikowski  * extended SMA pin support.
1346325b2064SMaciej Machnikowski  */
1347325b2064SMaciej Machnikowski static void
1348325b2064SMaciej Machnikowski ice_ptp_setup_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info)
1349325b2064SMaciej Machnikowski {
1350325b2064SMaciej Machnikowski 	struct device *dev = ice_pf_to_dev(pf);
1351325b2064SMaciej Machnikowski 	int err;
1352325b2064SMaciej Machnikowski 
1353325b2064SMaciej Machnikowski 	/* Allocate memory for kernel pins interface */
1354325b2064SMaciej Machnikowski 	info->pin_config = devm_kcalloc(dev, info->n_pins,
1355325b2064SMaciej Machnikowski 					sizeof(*info->pin_config), GFP_KERNEL);
1356325b2064SMaciej Machnikowski 	if (!info->pin_config) {
1357325b2064SMaciej Machnikowski 		ice_ptp_disable_sma_pins_e810t(pf, info);
1358325b2064SMaciej Machnikowski 		return;
1359325b2064SMaciej Machnikowski 	}
1360325b2064SMaciej Machnikowski 
1361325b2064SMaciej Machnikowski 	/* Read current SMA status */
1362325b2064SMaciej Machnikowski 	err = ice_get_sma_config_e810t(&pf->hw, info->pin_config);
1363325b2064SMaciej Machnikowski 	if (err)
1364325b2064SMaciej Machnikowski 		ice_ptp_disable_sma_pins_e810t(pf, info);
1365325b2064SMaciej Machnikowski }
1366325b2064SMaciej Machnikowski 
1367325b2064SMaciej Machnikowski /**
1368325b2064SMaciej Machnikowski  * ice_ptp_setup_pins_e810t - Setup PTP pins in sysfs
1369325b2064SMaciej Machnikowski  * @pf: pointer to the PF instance
1370325b2064SMaciej Machnikowski  * @info: PTP clock capabilities
1371325b2064SMaciej Machnikowski  */
1372325b2064SMaciej Machnikowski static void
1373325b2064SMaciej Machnikowski ice_ptp_setup_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info)
1374325b2064SMaciej Machnikowski {
1375325b2064SMaciej Machnikowski 	/* Check if SMA controller is in the netlist */
1376325b2064SMaciej Machnikowski 	if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL) &&
1377325b2064SMaciej Machnikowski 	    !ice_is_pca9575_present(&pf->hw))
1378325b2064SMaciej Machnikowski 		ice_clear_feature_support(pf, ICE_F_SMA_CTRL);
1379325b2064SMaciej Machnikowski 
1380325b2064SMaciej Machnikowski 	if (!ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) {
1381325b2064SMaciej Machnikowski 		info->n_ext_ts = N_EXT_TS_E810_NO_SMA;
1382325b2064SMaciej Machnikowski 		info->n_per_out = N_PER_OUT_E810T_NO_SMA;
1383325b2064SMaciej Machnikowski 		return;
1384325b2064SMaciej Machnikowski 	}
1385325b2064SMaciej Machnikowski 
1386325b2064SMaciej Machnikowski 	info->n_per_out = N_PER_OUT_E810T;
1387325b2064SMaciej Machnikowski 	info->n_ext_ts = N_EXT_TS_E810;
1388325b2064SMaciej Machnikowski 	info->n_pins = NUM_PTP_PINS_E810T;
1389325b2064SMaciej Machnikowski 	info->verify = ice_verify_pin_e810t;
1390325b2064SMaciej Machnikowski 
1391325b2064SMaciej Machnikowski 	/* Complete setup of the SMA pins */
1392325b2064SMaciej Machnikowski 	ice_ptp_setup_sma_pins_e810t(pf, info);
1393325b2064SMaciej Machnikowski }
1394325b2064SMaciej Machnikowski 
1395325b2064SMaciej Machnikowski /**
1396172db5f9SMaciej Machnikowski  * ice_ptp_setup_pins_e810 - Setup PTP pins in sysfs
1397172db5f9SMaciej Machnikowski  * @info: PTP clock capabilities
1398172db5f9SMaciej Machnikowski  */
1399172db5f9SMaciej Machnikowski static void ice_ptp_setup_pins_e810(struct ptp_clock_info *info)
1400172db5f9SMaciej Machnikowski {
1401325b2064SMaciej Machnikowski 	info->n_per_out = N_PER_OUT_E810;
1402325b2064SMaciej Machnikowski 	info->n_ext_ts = N_EXT_TS_E810;
1403172db5f9SMaciej Machnikowski }
1404172db5f9SMaciej Machnikowski 
1405172db5f9SMaciej Machnikowski /**
1406172db5f9SMaciej Machnikowski  * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support
1407172db5f9SMaciej Machnikowski  * @pf: Board private structure
1408172db5f9SMaciej Machnikowski  * @info: PTP info to fill
1409172db5f9SMaciej Machnikowski  *
1410172db5f9SMaciej Machnikowski  * Assign functions to the PTP capabiltiies structure for E810 devices.
1411172db5f9SMaciej Machnikowski  * Functions which operate across all device families should be set directly
1412172db5f9SMaciej Machnikowski  * in ice_ptp_set_caps. Only add functions here which are distinct for e810
1413172db5f9SMaciej Machnikowski  * devices.
1414172db5f9SMaciej Machnikowski  */
1415172db5f9SMaciej Machnikowski static void
1416172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(struct ice_pf *pf, struct ptp_clock_info *info)
1417172db5f9SMaciej Machnikowski {
1418172db5f9SMaciej Machnikowski 	info->enable = ice_ptp_gpio_enable_e810;
1419172db5f9SMaciej Machnikowski 
1420325b2064SMaciej Machnikowski 	if (ice_is_e810t(&pf->hw))
1421325b2064SMaciej Machnikowski 		ice_ptp_setup_pins_e810t(pf, info);
1422325b2064SMaciej Machnikowski 	else
1423172db5f9SMaciej Machnikowski 		ice_ptp_setup_pins_e810(info);
1424172db5f9SMaciej Machnikowski }
1425172db5f9SMaciej Machnikowski 
1426172db5f9SMaciej Machnikowski /**
142706c16d89SJacob Keller  * ice_ptp_set_caps - Set PTP capabilities
142806c16d89SJacob Keller  * @pf: Board private structure
142906c16d89SJacob Keller  */
143006c16d89SJacob Keller static void ice_ptp_set_caps(struct ice_pf *pf)
143106c16d89SJacob Keller {
143206c16d89SJacob Keller 	struct ptp_clock_info *info = &pf->ptp.info;
143306c16d89SJacob Keller 	struct device *dev = ice_pf_to_dev(pf);
143406c16d89SJacob Keller 
143506c16d89SJacob Keller 	snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk",
143606c16d89SJacob Keller 		 dev_driver_string(dev), dev_name(dev));
143706c16d89SJacob Keller 	info->owner = THIS_MODULE;
143806c16d89SJacob Keller 	info->max_adj = 999999999;
143906c16d89SJacob Keller 	info->adjtime = ice_ptp_adjtime;
144006c16d89SJacob Keller 	info->adjfine = ice_ptp_adjfine;
144106c16d89SJacob Keller 	info->gettimex64 = ice_ptp_gettimex64;
144206c16d89SJacob Keller 	info->settime64 = ice_ptp_settime64;
1443172db5f9SMaciej Machnikowski 
1444172db5f9SMaciej Machnikowski 	ice_ptp_set_funcs_e810(pf, info);
144506c16d89SJacob Keller }
144606c16d89SJacob Keller 
144706c16d89SJacob Keller /**
144806c16d89SJacob Keller  * ice_ptp_create_clock - Create PTP clock device for userspace
144906c16d89SJacob Keller  * @pf: Board private structure
145006c16d89SJacob Keller  *
145106c16d89SJacob Keller  * This function creates a new PTP clock device. It only creates one if we
145206c16d89SJacob Keller  * don't already have one. Will return error if it can't create one, but success
145306c16d89SJacob Keller  * if we already have a device. Should be used by ice_ptp_init to create clock
145406c16d89SJacob Keller  * initially, and prevent global resets from creating new clock devices.
145506c16d89SJacob Keller  */
145606c16d89SJacob Keller static long ice_ptp_create_clock(struct ice_pf *pf)
145706c16d89SJacob Keller {
145806c16d89SJacob Keller 	struct ptp_clock_info *info;
145906c16d89SJacob Keller 	struct ptp_clock *clock;
146006c16d89SJacob Keller 	struct device *dev;
146106c16d89SJacob Keller 
146206c16d89SJacob Keller 	/* No need to create a clock device if we already have one */
146306c16d89SJacob Keller 	if (pf->ptp.clock)
146406c16d89SJacob Keller 		return 0;
146506c16d89SJacob Keller 
146606c16d89SJacob Keller 	ice_ptp_set_caps(pf);
146706c16d89SJacob Keller 
146806c16d89SJacob Keller 	info = &pf->ptp.info;
146906c16d89SJacob Keller 	dev = ice_pf_to_dev(pf);
147006c16d89SJacob Keller 
147106c16d89SJacob Keller 	/* Attempt to register the clock before enabling the hardware. */
147206c16d89SJacob Keller 	clock = ptp_clock_register(info, dev);
147306c16d89SJacob Keller 	if (IS_ERR(clock))
147406c16d89SJacob Keller 		return PTR_ERR(clock);
147506c16d89SJacob Keller 
147606c16d89SJacob Keller 	pf->ptp.clock = clock;
147706c16d89SJacob Keller 
147806c16d89SJacob Keller 	return 0;
147906c16d89SJacob Keller }
148006c16d89SJacob Keller 
1481ea9b847cSJacob Keller /**
1482ea9b847cSJacob Keller  * ice_ptp_tx_tstamp_work - Process Tx timestamps for a port
1483ea9b847cSJacob Keller  * @work: pointer to the kthread_work struct
1484ea9b847cSJacob Keller  *
1485ea9b847cSJacob Keller  * Process timestamps captured by the PHY associated with this port. To do
1486ea9b847cSJacob Keller  * this, loop over each index with a waiting skb.
1487ea9b847cSJacob Keller  *
1488ea9b847cSJacob Keller  * If a given index has a valid timestamp, perform the following steps:
1489ea9b847cSJacob Keller  *
1490ea9b847cSJacob Keller  * 1) copy the timestamp out of the PHY register
1491ea9b847cSJacob Keller  * 4) clear the timestamp valid bit in the PHY register
1492ea9b847cSJacob Keller  * 5) unlock the index by clearing the associated in_use bit.
1493ea9b847cSJacob Keller  * 2) extend the 40b timestamp value to get a 64bit timestamp
1494ea9b847cSJacob Keller  * 3) send that timestamp to the stack
1495ea9b847cSJacob Keller  *
1496ea9b847cSJacob Keller  * After looping, if we still have waiting SKBs, then re-queue the work. This
1497ea9b847cSJacob Keller  * may cause us effectively poll even when not strictly necessary. We do this
1498ea9b847cSJacob Keller  * because it's possible a new timestamp was requested around the same time as
1499ea9b847cSJacob Keller  * the interrupt. In some cases hardware might not interrupt us again when the
1500ea9b847cSJacob Keller  * timestamp is captured.
1501ea9b847cSJacob Keller  *
1502ea9b847cSJacob Keller  * Note that we only take the tracking lock when clearing the bit and when
1503ea9b847cSJacob Keller  * checking if we need to re-queue this task. The only place where bits can be
1504ea9b847cSJacob Keller  * set is the hard xmit routine where an SKB has a request flag set. The only
1505ea9b847cSJacob Keller  * places where we clear bits are this work function, or the periodic cleanup
1506ea9b847cSJacob Keller  * thread. If the cleanup thread clears a bit we're processing we catch it
1507ea9b847cSJacob Keller  * when we lock to clear the bit and then grab the SKB pointer. If a Tx thread
1508ea9b847cSJacob Keller  * starts a new timestamp, we might not begin processing it right away but we
1509ea9b847cSJacob Keller  * will notice it at the end when we re-queue the work item. If a Tx thread
1510ea9b847cSJacob Keller  * starts a new timestamp just after this function exits without re-queuing,
1511ea9b847cSJacob Keller  * the interrupt when the timestamp finishes should trigger. Avoiding holding
1512ea9b847cSJacob Keller  * the lock for the entire function is important in order to ensure that Tx
1513ea9b847cSJacob Keller  * threads do not get blocked while waiting for the lock.
1514ea9b847cSJacob Keller  */
1515ea9b847cSJacob Keller static void ice_ptp_tx_tstamp_work(struct kthread_work *work)
1516ea9b847cSJacob Keller {
1517ea9b847cSJacob Keller 	struct ice_ptp_port *ptp_port;
1518ea9b847cSJacob Keller 	struct ice_ptp_tx *tx;
1519ea9b847cSJacob Keller 	struct ice_pf *pf;
1520ea9b847cSJacob Keller 	struct ice_hw *hw;
1521ea9b847cSJacob Keller 	u8 idx;
1522ea9b847cSJacob Keller 
1523ea9b847cSJacob Keller 	tx = container_of(work, struct ice_ptp_tx, work);
1524ea9b847cSJacob Keller 	if (!tx->init)
1525ea9b847cSJacob Keller 		return;
1526ea9b847cSJacob Keller 
1527ea9b847cSJacob Keller 	ptp_port = container_of(tx, struct ice_ptp_port, tx);
1528ea9b847cSJacob Keller 	pf = ptp_port_to_pf(ptp_port);
1529ea9b847cSJacob Keller 	hw = &pf->hw;
1530ea9b847cSJacob Keller 
1531ea9b847cSJacob Keller 	for_each_set_bit(idx, tx->in_use, tx->len) {
1532ea9b847cSJacob Keller 		struct skb_shared_hwtstamps shhwtstamps = {};
1533ea9b847cSJacob Keller 		u8 phy_idx = idx + tx->quad_offset;
1534ea9b847cSJacob Keller 		u64 raw_tstamp, tstamp;
1535ea9b847cSJacob Keller 		struct sk_buff *skb;
1536ea9b847cSJacob Keller 		int err;
1537ea9b847cSJacob Keller 
1538ea9b847cSJacob Keller 		err = ice_read_phy_tstamp(hw, tx->quad, phy_idx,
1539ea9b847cSJacob Keller 					  &raw_tstamp);
1540ea9b847cSJacob Keller 		if (err)
1541ea9b847cSJacob Keller 			continue;
1542ea9b847cSJacob Keller 
1543ea9b847cSJacob Keller 		/* Check if the timestamp is valid */
1544ea9b847cSJacob Keller 		if (!(raw_tstamp & ICE_PTP_TS_VALID))
1545ea9b847cSJacob Keller 			continue;
1546ea9b847cSJacob Keller 
1547ea9b847cSJacob Keller 		/* clear the timestamp register, so that it won't show valid
1548ea9b847cSJacob Keller 		 * again when re-used.
1549ea9b847cSJacob Keller 		 */
1550ea9b847cSJacob Keller 		ice_clear_phy_tstamp(hw, tx->quad, phy_idx);
1551ea9b847cSJacob Keller 
1552ea9b847cSJacob Keller 		/* The timestamp is valid, so we'll go ahead and clear this
1553ea9b847cSJacob Keller 		 * index and then send the timestamp up to the stack.
1554ea9b847cSJacob Keller 		 */
1555ea9b847cSJacob Keller 		spin_lock(&tx->lock);
1556ea9b847cSJacob Keller 		clear_bit(idx, tx->in_use);
1557ea9b847cSJacob Keller 		skb = tx->tstamps[idx].skb;
1558ea9b847cSJacob Keller 		tx->tstamps[idx].skb = NULL;
1559ea9b847cSJacob Keller 		spin_unlock(&tx->lock);
1560ea9b847cSJacob Keller 
1561ea9b847cSJacob Keller 		/* it's (unlikely but) possible we raced with the cleanup
1562ea9b847cSJacob Keller 		 * thread for discarding old timestamp requests.
1563ea9b847cSJacob Keller 		 */
1564ea9b847cSJacob Keller 		if (!skb)
1565ea9b847cSJacob Keller 			continue;
1566ea9b847cSJacob Keller 
1567ea9b847cSJacob Keller 		/* Extend the timestamp using cached PHC time */
1568ea9b847cSJacob Keller 		tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp);
1569ea9b847cSJacob Keller 		shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
1570ea9b847cSJacob Keller 
1571ea9b847cSJacob Keller 		skb_tstamp_tx(skb, &shhwtstamps);
1572ea9b847cSJacob Keller 		dev_kfree_skb_any(skb);
1573ea9b847cSJacob Keller 	}
1574ea9b847cSJacob Keller 
1575ea9b847cSJacob Keller 	/* Check if we still have work to do. If so, re-queue this task to
1576ea9b847cSJacob Keller 	 * poll for remaining timestamps.
1577ea9b847cSJacob Keller 	 */
1578ea9b847cSJacob Keller 	spin_lock(&tx->lock);
1579ea9b847cSJacob Keller 	if (!bitmap_empty(tx->in_use, tx->len))
1580ea9b847cSJacob Keller 		kthread_queue_work(pf->ptp.kworker, &tx->work);
1581ea9b847cSJacob Keller 	spin_unlock(&tx->lock);
1582ea9b847cSJacob Keller }
1583ea9b847cSJacob Keller 
1584ea9b847cSJacob Keller /**
1585ea9b847cSJacob Keller  * ice_ptp_request_ts - Request an available Tx timestamp index
1586ea9b847cSJacob Keller  * @tx: the PTP Tx timestamp tracker to request from
1587ea9b847cSJacob Keller  * @skb: the SKB to associate with this timestamp request
1588ea9b847cSJacob Keller  */
1589ea9b847cSJacob Keller s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
1590ea9b847cSJacob Keller {
1591ea9b847cSJacob Keller 	u8 idx;
1592ea9b847cSJacob Keller 
1593ea9b847cSJacob Keller 	/* Check if this tracker is initialized */
1594ea9b847cSJacob Keller 	if (!tx->init)
1595ea9b847cSJacob Keller 		return -1;
1596ea9b847cSJacob Keller 
1597ea9b847cSJacob Keller 	spin_lock(&tx->lock);
1598ea9b847cSJacob Keller 	/* Find and set the first available index */
1599ea9b847cSJacob Keller 	idx = find_first_zero_bit(tx->in_use, tx->len);
1600ea9b847cSJacob Keller 	if (idx < tx->len) {
1601ea9b847cSJacob Keller 		/* We got a valid index that no other thread could have set. Store
1602ea9b847cSJacob Keller 		 * a reference to the skb and the start time to allow discarding old
1603ea9b847cSJacob Keller 		 * requests.
1604ea9b847cSJacob Keller 		 */
1605ea9b847cSJacob Keller 		set_bit(idx, tx->in_use);
1606ea9b847cSJacob Keller 		tx->tstamps[idx].start = jiffies;
1607ea9b847cSJacob Keller 		tx->tstamps[idx].skb = skb_get(skb);
1608ea9b847cSJacob Keller 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1609ea9b847cSJacob Keller 	}
1610ea9b847cSJacob Keller 
1611ea9b847cSJacob Keller 	spin_unlock(&tx->lock);
1612ea9b847cSJacob Keller 
1613ea9b847cSJacob Keller 	/* return the appropriate PHY timestamp register index, -1 if no
1614ea9b847cSJacob Keller 	 * indexes were available.
1615ea9b847cSJacob Keller 	 */
1616ea9b847cSJacob Keller 	if (idx >= tx->len)
1617ea9b847cSJacob Keller 		return -1;
1618ea9b847cSJacob Keller 	else
1619ea9b847cSJacob Keller 		return idx + tx->quad_offset;
1620ea9b847cSJacob Keller }
1621ea9b847cSJacob Keller 
1622ea9b847cSJacob Keller /**
1623ea9b847cSJacob Keller  * ice_ptp_process_ts - Spawn kthread work to handle timestamps
1624ea9b847cSJacob Keller  * @pf: Board private structure
1625ea9b847cSJacob Keller  *
1626ea9b847cSJacob Keller  * Queue work required to process the PTP Tx timestamps outside of interrupt
1627ea9b847cSJacob Keller  * context.
1628ea9b847cSJacob Keller  */
1629ea9b847cSJacob Keller void ice_ptp_process_ts(struct ice_pf *pf)
1630ea9b847cSJacob Keller {
1631ea9b847cSJacob Keller 	if (pf->ptp.port.tx.init)
1632ea9b847cSJacob Keller 		kthread_queue_work(pf->ptp.kworker, &pf->ptp.port.tx.work);
1633ea9b847cSJacob Keller }
1634ea9b847cSJacob Keller 
1635ea9b847cSJacob Keller /**
1636ea9b847cSJacob Keller  * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps
1637ea9b847cSJacob Keller  * @tx: Tx tracking structure to initialize
1638ea9b847cSJacob Keller  *
1639ea9b847cSJacob Keller  * Assumes that the length has already been initialized. Do not call directly,
1640ea9b847cSJacob Keller  * use the ice_ptp_init_tx_e822 or ice_ptp_init_tx_e810 instead.
1641ea9b847cSJacob Keller  */
1642ea9b847cSJacob Keller static int
1643ea9b847cSJacob Keller ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx)
1644ea9b847cSJacob Keller {
1645ea9b847cSJacob Keller 	tx->tstamps = kcalloc(tx->len, sizeof(*tx->tstamps), GFP_KERNEL);
1646ea9b847cSJacob Keller 	if (!tx->tstamps)
1647ea9b847cSJacob Keller 		return -ENOMEM;
1648ea9b847cSJacob Keller 
1649ea9b847cSJacob Keller 	tx->in_use = bitmap_zalloc(tx->len, GFP_KERNEL);
1650ea9b847cSJacob Keller 	if (!tx->in_use) {
1651ea9b847cSJacob Keller 		kfree(tx->tstamps);
1652ea9b847cSJacob Keller 		tx->tstamps = NULL;
1653ea9b847cSJacob Keller 		return -ENOMEM;
1654ea9b847cSJacob Keller 	}
1655ea9b847cSJacob Keller 
1656ea9b847cSJacob Keller 	spin_lock_init(&tx->lock);
1657ea9b847cSJacob Keller 	kthread_init_work(&tx->work, ice_ptp_tx_tstamp_work);
1658ea9b847cSJacob Keller 
1659ea9b847cSJacob Keller 	tx->init = 1;
1660ea9b847cSJacob Keller 
1661ea9b847cSJacob Keller 	return 0;
1662ea9b847cSJacob Keller }
1663ea9b847cSJacob Keller 
1664ea9b847cSJacob Keller /**
1665ea9b847cSJacob Keller  * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker
1666ea9b847cSJacob Keller  * @pf: Board private structure
1667ea9b847cSJacob Keller  * @tx: the tracker to flush
1668ea9b847cSJacob Keller  */
1669ea9b847cSJacob Keller static void
1670ea9b847cSJacob Keller ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
1671ea9b847cSJacob Keller {
1672ea9b847cSJacob Keller 	u8 idx;
1673ea9b847cSJacob Keller 
1674ea9b847cSJacob Keller 	for (idx = 0; idx < tx->len; idx++) {
1675ea9b847cSJacob Keller 		u8 phy_idx = idx + tx->quad_offset;
1676ea9b847cSJacob Keller 
16774d4a223aSJacob Keller 		spin_lock(&tx->lock);
1678ea9b847cSJacob Keller 		if (tx->tstamps[idx].skb) {
1679ea9b847cSJacob Keller 			dev_kfree_skb_any(tx->tstamps[idx].skb);
1680ea9b847cSJacob Keller 			tx->tstamps[idx].skb = NULL;
1681ea9b847cSJacob Keller 		}
16824d4a223aSJacob Keller 		clear_bit(idx, tx->in_use);
16834dd0d5c3SJacob Keller 		spin_unlock(&tx->lock);
16844d4a223aSJacob Keller 
16854d4a223aSJacob Keller 		/* Clear any potential residual timestamp in the PHY block */
16864d4a223aSJacob Keller 		if (!pf->hw.reset_ongoing)
16874d4a223aSJacob Keller 			ice_clear_phy_tstamp(&pf->hw, tx->quad, phy_idx);
16884d4a223aSJacob Keller 	}
1689ea9b847cSJacob Keller }
1690ea9b847cSJacob Keller 
1691ea9b847cSJacob Keller /**
1692ea9b847cSJacob Keller  * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker
1693ea9b847cSJacob Keller  * @pf: Board private structure
1694ea9b847cSJacob Keller  * @tx: Tx tracking structure to release
1695ea9b847cSJacob Keller  *
1696ea9b847cSJacob Keller  * Free memory associated with the Tx timestamp tracker.
1697ea9b847cSJacob Keller  */
1698ea9b847cSJacob Keller static void
1699ea9b847cSJacob Keller ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
1700ea9b847cSJacob Keller {
1701ea9b847cSJacob Keller 	tx->init = 0;
1702ea9b847cSJacob Keller 
1703ea9b847cSJacob Keller 	kthread_cancel_work_sync(&tx->work);
1704ea9b847cSJacob Keller 
1705ea9b847cSJacob Keller 	ice_ptp_flush_tx_tracker(pf, tx);
1706ea9b847cSJacob Keller 
1707ea9b847cSJacob Keller 	kfree(tx->tstamps);
1708ea9b847cSJacob Keller 	tx->tstamps = NULL;
1709ea9b847cSJacob Keller 
1710ea9b847cSJacob Keller 	kfree(tx->in_use);
1711ea9b847cSJacob Keller 	tx->in_use = NULL;
1712ea9b847cSJacob Keller 
1713ea9b847cSJacob Keller 	tx->len = 0;
1714ea9b847cSJacob Keller }
1715ea9b847cSJacob Keller 
1716ea9b847cSJacob Keller /**
1717ea9b847cSJacob Keller  * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps
1718ea9b847cSJacob Keller  * @pf: Board private structure
1719ea9b847cSJacob Keller  * @tx: the Tx tracking structure to initialize
1720ea9b847cSJacob Keller  *
1721ea9b847cSJacob Keller  * Initialize the Tx timestamp tracker for this PF. For E810 devices, each
1722ea9b847cSJacob Keller  * port has its own block of timestamps, independent of the other ports.
1723ea9b847cSJacob Keller  */
1724ea9b847cSJacob Keller static int
1725ea9b847cSJacob Keller ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx)
1726ea9b847cSJacob Keller {
1727ea9b847cSJacob Keller 	tx->quad = pf->hw.port_info->lport;
1728ea9b847cSJacob Keller 	tx->quad_offset = 0;
1729ea9b847cSJacob Keller 	tx->len = INDEX_PER_QUAD;
1730ea9b847cSJacob Keller 
1731ea9b847cSJacob Keller 	return ice_ptp_alloc_tx_tracker(tx);
1732ea9b847cSJacob Keller }
1733ea9b847cSJacob Keller 
1734ea9b847cSJacob Keller /**
1735ea9b847cSJacob Keller  * ice_ptp_tx_tstamp_cleanup - Cleanup old timestamp requests that got dropped
1736ea9b847cSJacob Keller  * @tx: PTP Tx tracker to clean up
1737ea9b847cSJacob Keller  *
1738ea9b847cSJacob Keller  * Loop through the Tx timestamp requests and see if any of them have been
1739ea9b847cSJacob Keller  * waiting for a long time. Discard any SKBs that have been waiting for more
1740ea9b847cSJacob Keller  * than 2 seconds. This is long enough to be reasonably sure that the
1741ea9b847cSJacob Keller  * timestamp will never be captured. This might happen if the packet gets
1742ea9b847cSJacob Keller  * discarded before it reaches the PHY timestamping block.
1743ea9b847cSJacob Keller  */
1744ea9b847cSJacob Keller static void ice_ptp_tx_tstamp_cleanup(struct ice_ptp_tx *tx)
1745ea9b847cSJacob Keller {
1746ea9b847cSJacob Keller 	u8 idx;
1747ea9b847cSJacob Keller 
1748ea9b847cSJacob Keller 	if (!tx->init)
1749ea9b847cSJacob Keller 		return;
1750ea9b847cSJacob Keller 
1751ea9b847cSJacob Keller 	for_each_set_bit(idx, tx->in_use, tx->len) {
1752ea9b847cSJacob Keller 		struct sk_buff *skb;
1753ea9b847cSJacob Keller 
1754ea9b847cSJacob Keller 		/* Check if this SKB has been waiting for too long */
1755ea9b847cSJacob Keller 		if (time_is_after_jiffies(tx->tstamps[idx].start + 2 * HZ))
1756ea9b847cSJacob Keller 			continue;
1757ea9b847cSJacob Keller 
1758ea9b847cSJacob Keller 		spin_lock(&tx->lock);
1759ea9b847cSJacob Keller 		skb = tx->tstamps[idx].skb;
1760ea9b847cSJacob Keller 		tx->tstamps[idx].skb = NULL;
1761ea9b847cSJacob Keller 		clear_bit(idx, tx->in_use);
1762ea9b847cSJacob Keller 		spin_unlock(&tx->lock);
1763ea9b847cSJacob Keller 
1764ea9b847cSJacob Keller 		/* Free the SKB after we've cleared the bit */
1765ea9b847cSJacob Keller 		dev_kfree_skb_any(skb);
1766ea9b847cSJacob Keller 	}
1767ea9b847cSJacob Keller }
1768ea9b847cSJacob Keller 
176977a78115SJacob Keller static void ice_ptp_periodic_work(struct kthread_work *work)
177077a78115SJacob Keller {
177177a78115SJacob Keller 	struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work);
177277a78115SJacob Keller 	struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp);
177377a78115SJacob Keller 
177477a78115SJacob Keller 	if (!test_bit(ICE_FLAG_PTP, pf->flags))
177577a78115SJacob Keller 		return;
177677a78115SJacob Keller 
177777a78115SJacob Keller 	ice_ptp_update_cached_phctime(pf);
177877a78115SJacob Keller 
1779ea9b847cSJacob Keller 	ice_ptp_tx_tstamp_cleanup(&pf->ptp.port.tx);
1780ea9b847cSJacob Keller 
178177a78115SJacob Keller 	/* Run twice a second */
178277a78115SJacob Keller 	kthread_queue_delayed_work(ptp->kworker, &ptp->work,
178377a78115SJacob Keller 				   msecs_to_jiffies(500));
178477a78115SJacob Keller }
178577a78115SJacob Keller 
178606c16d89SJacob Keller /**
178706c16d89SJacob Keller  * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device
178806c16d89SJacob Keller  * @pf: Board private structure
178906c16d89SJacob Keller  *
179006c16d89SJacob Keller  * Setup and initialize a PTP clock device that represents the device hardware
179106c16d89SJacob Keller  * clock. Save the clock index for other functions connected to the same
179206c16d89SJacob Keller  * hardware resource.
179306c16d89SJacob Keller  */
179406c16d89SJacob Keller static int ice_ptp_init_owner(struct ice_pf *pf)
179506c16d89SJacob Keller {
179606c16d89SJacob Keller 	struct device *dev = ice_pf_to_dev(pf);
179706c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
179806c16d89SJacob Keller 	struct timespec64 ts;
179906c16d89SJacob Keller 	u8 src_idx;
180006c16d89SJacob Keller 	int err;
180106c16d89SJacob Keller 
180206c16d89SJacob Keller 	wr32(hw, GLTSYN_SYNC_DLAY, 0);
180306c16d89SJacob Keller 
180406c16d89SJacob Keller 	/* Clear some HW residue and enable source clock */
180506c16d89SJacob Keller 	src_idx = hw->func_caps.ts_func_info.tmr_index_owned;
180606c16d89SJacob Keller 
180706c16d89SJacob Keller 	/* Enable source clocks */
180806c16d89SJacob Keller 	wr32(hw, GLTSYN_ENA(src_idx), GLTSYN_ENA_TSYN_ENA_M);
180906c16d89SJacob Keller 
181006c16d89SJacob Keller 	/* Enable PHY time sync */
181106c16d89SJacob Keller 	err = ice_ptp_init_phy_e810(hw);
181206c16d89SJacob Keller 	if (err)
181306c16d89SJacob Keller 		goto err_exit;
181406c16d89SJacob Keller 
181506c16d89SJacob Keller 	/* Clear event status indications for auxiliary pins */
181606c16d89SJacob Keller 	(void)rd32(hw, GLTSYN_STAT(src_idx));
181706c16d89SJacob Keller 
181806c16d89SJacob Keller 	/* Acquire the global hardware lock */
181906c16d89SJacob Keller 	if (!ice_ptp_lock(hw)) {
182006c16d89SJacob Keller 		err = -EBUSY;
182106c16d89SJacob Keller 		goto err_exit;
182206c16d89SJacob Keller 	}
182306c16d89SJacob Keller 
182406c16d89SJacob Keller 	/* Write the increment time value to PHY and LAN */
182506c16d89SJacob Keller 	err = ice_ptp_write_incval(hw, ICE_PTP_NOMINAL_INCVAL_E810);
182606c16d89SJacob Keller 	if (err) {
182706c16d89SJacob Keller 		ice_ptp_unlock(hw);
182806c16d89SJacob Keller 		goto err_exit;
182906c16d89SJacob Keller 	}
183006c16d89SJacob Keller 
183106c16d89SJacob Keller 	ts = ktime_to_timespec64(ktime_get_real());
183206c16d89SJacob Keller 	/* Write the initial Time value to PHY and LAN */
183306c16d89SJacob Keller 	err = ice_ptp_write_init(pf, &ts);
183406c16d89SJacob Keller 	if (err) {
183506c16d89SJacob Keller 		ice_ptp_unlock(hw);
183606c16d89SJacob Keller 		goto err_exit;
183706c16d89SJacob Keller 	}
183806c16d89SJacob Keller 
183906c16d89SJacob Keller 	/* Release the global hardware lock */
184006c16d89SJacob Keller 	ice_ptp_unlock(hw);
184106c16d89SJacob Keller 
184206c16d89SJacob Keller 	/* Ensure we have a clock device */
184306c16d89SJacob Keller 	err = ice_ptp_create_clock(pf);
184406c16d89SJacob Keller 	if (err)
184506c16d89SJacob Keller 		goto err_clk;
184606c16d89SJacob Keller 
184767569a7fSJacob Keller 	/* Store the PTP clock index for other PFs */
184867569a7fSJacob Keller 	ice_set_ptp_clock_index(pf);
184967569a7fSJacob Keller 
185006c16d89SJacob Keller 	return 0;
185106c16d89SJacob Keller 
185206c16d89SJacob Keller err_clk:
185306c16d89SJacob Keller 	pf->ptp.clock = NULL;
185406c16d89SJacob Keller err_exit:
185506c16d89SJacob Keller 	dev_err(dev, "PTP failed to register clock, err %d\n", err);
185606c16d89SJacob Keller 
185706c16d89SJacob Keller 	return err;
185806c16d89SJacob Keller }
185906c16d89SJacob Keller 
186006c16d89SJacob Keller /**
186106c16d89SJacob Keller  * ice_ptp_init - Initialize the PTP support after device probe or reset
186206c16d89SJacob Keller  * @pf: Board private structure
186306c16d89SJacob Keller  *
186406c16d89SJacob Keller  * This function sets device up for PTP support. The first time it is run, it
186506c16d89SJacob Keller  * will create a clock device. It does not create a clock device if one
186606c16d89SJacob Keller  * already exists. It also reconfigures the device after a reset.
186706c16d89SJacob Keller  */
186806c16d89SJacob Keller void ice_ptp_init(struct ice_pf *pf)
186906c16d89SJacob Keller {
187006c16d89SJacob Keller 	struct device *dev = ice_pf_to_dev(pf);
187177a78115SJacob Keller 	struct kthread_worker *kworker;
187206c16d89SJacob Keller 	struct ice_hw *hw = &pf->hw;
187306c16d89SJacob Keller 	int err;
187406c16d89SJacob Keller 
187506c16d89SJacob Keller 	/* PTP is currently only supported on E810 devices */
187606c16d89SJacob Keller 	if (!ice_is_e810(hw))
187706c16d89SJacob Keller 		return;
187806c16d89SJacob Keller 
187906c16d89SJacob Keller 	/* Check if this PF owns the source timer */
188006c16d89SJacob Keller 	if (hw->func_caps.ts_func_info.src_tmr_owned) {
188106c16d89SJacob Keller 		err = ice_ptp_init_owner(pf);
188206c16d89SJacob Keller 		if (err)
188306c16d89SJacob Keller 			return;
188406c16d89SJacob Keller 	}
188506c16d89SJacob Keller 
188677a78115SJacob Keller 	/* Disable timestamping for both Tx and Rx */
188777a78115SJacob Keller 	ice_ptp_cfg_timestamp(pf, false);
188877a78115SJacob Keller 
1889ea9b847cSJacob Keller 	/* Initialize the PTP port Tx timestamp tracker */
1890ea9b847cSJacob Keller 	ice_ptp_init_tx_e810(pf, &pf->ptp.port.tx);
1891ea9b847cSJacob Keller 
189277a78115SJacob Keller 	/* Initialize work functions */
189377a78115SJacob Keller 	kthread_init_delayed_work(&pf->ptp.work, ice_ptp_periodic_work);
1894172db5f9SMaciej Machnikowski 	kthread_init_work(&pf->ptp.extts_work, ice_ptp_extts_work);
189577a78115SJacob Keller 
189677a78115SJacob Keller 	/* Allocate a kworker for handling work required for the ports
189777a78115SJacob Keller 	 * connected to the PTP hardware clock.
189877a78115SJacob Keller 	 */
189977a78115SJacob Keller 	kworker = kthread_create_worker(0, "ice-ptp-%s", dev_name(dev));
190077a78115SJacob Keller 	if (IS_ERR(kworker)) {
190177a78115SJacob Keller 		err = PTR_ERR(kworker);
190277a78115SJacob Keller 		goto err_kworker;
190377a78115SJacob Keller 	}
190477a78115SJacob Keller 	pf->ptp.kworker = kworker;
190577a78115SJacob Keller 
190606c16d89SJacob Keller 	set_bit(ICE_FLAG_PTP, pf->flags);
190706c16d89SJacob Keller 
190877a78115SJacob Keller 	/* Start periodic work going */
190977a78115SJacob Keller 	kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work, 0);
191077a78115SJacob Keller 
191106c16d89SJacob Keller 	dev_info(dev, "PTP init successful\n");
191277a78115SJacob Keller 	return;
191377a78115SJacob Keller 
191477a78115SJacob Keller err_kworker:
191577a78115SJacob Keller 	/* If we registered a PTP clock, release it */
191677a78115SJacob Keller 	if (pf->ptp.clock) {
191777a78115SJacob Keller 		ptp_clock_unregister(pf->ptp.clock);
191877a78115SJacob Keller 		pf->ptp.clock = NULL;
191977a78115SJacob Keller 	}
192077a78115SJacob Keller 	dev_err(dev, "PTP failed %d\n", err);
192106c16d89SJacob Keller }
192206c16d89SJacob Keller 
192306c16d89SJacob Keller /**
192406c16d89SJacob Keller  * ice_ptp_release - Disable the driver/HW support and unregister the clock
192506c16d89SJacob Keller  * @pf: Board private structure
192606c16d89SJacob Keller  *
192706c16d89SJacob Keller  * This function handles the cleanup work required from the initialization by
192806c16d89SJacob Keller  * clearing out the important information and unregistering the clock
192906c16d89SJacob Keller  */
193006c16d89SJacob Keller void ice_ptp_release(struct ice_pf *pf)
193106c16d89SJacob Keller {
1932fd1b5bebSYongxin Liu 	if (!test_bit(ICE_FLAG_PTP, pf->flags))
1933fd1b5bebSYongxin Liu 		return;
1934fd1b5bebSYongxin Liu 
193577a78115SJacob Keller 	/* Disable timestamping for both Tx and Rx */
193677a78115SJacob Keller 	ice_ptp_cfg_timestamp(pf, false);
193777a78115SJacob Keller 
1938ea9b847cSJacob Keller 	ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
1939ea9b847cSJacob Keller 
194006c16d89SJacob Keller 	clear_bit(ICE_FLAG_PTP, pf->flags);
194106c16d89SJacob Keller 
194277a78115SJacob Keller 	kthread_cancel_delayed_work_sync(&pf->ptp.work);
194377a78115SJacob Keller 
194477a78115SJacob Keller 	if (pf->ptp.kworker) {
194577a78115SJacob Keller 		kthread_destroy_worker(pf->ptp.kworker);
194677a78115SJacob Keller 		pf->ptp.kworker = NULL;
194777a78115SJacob Keller 	}
194877a78115SJacob Keller 
194906c16d89SJacob Keller 	if (!pf->ptp.clock)
195006c16d89SJacob Keller 		return;
195106c16d89SJacob Keller 
19529ee31343SJacob Keller 	/* Disable periodic outputs */
19539ee31343SJacob Keller 	ice_ptp_disable_all_clkout(pf);
19549ee31343SJacob Keller 
195567569a7fSJacob Keller 	ice_clear_ptp_clock_index(pf);
195606c16d89SJacob Keller 	ptp_clock_unregister(pf->ptp.clock);
195706c16d89SJacob Keller 	pf->ptp.clock = NULL;
195806c16d89SJacob Keller 
195906c16d89SJacob Keller 	dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n");
196006c16d89SJacob Keller }
1961