xref: /linux/drivers/net/ethernet/intel/ice/ice_dpll.c (revision e075d7768386235f4a08d7e04d02c90ce51fd64e)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2022, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_lib.h"
6 #include "ice_trace.h"
7 #include "ice_txclk.h"
8 #include <linux/dpll.h>
9 #include <linux/property.h>
10 
11 #define ICE_CGU_STATE_ACQ_ERR_THRESHOLD		50
12 #define ICE_DPLL_PIN_IDX_INVALID		0xff
13 #define ICE_DPLL_RCLK_NUM_PER_PF		1
14 #define ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT	25
15 #define ICE_DPLL_PIN_GEN_RCLK_FREQ		1953125
16 #define ICE_DPLL_PIN_PRIO_OUTPUT		0xff
17 #define ICE_DPLL_INPUT_REF_NUM			10
18 #define ICE_DPLL_PHASE_OFFSET_PERIOD		2
19 #define ICE_DPLL_SW_PIN_INPUT_BASE_SFP		4
20 #define ICE_DPLL_SW_PIN_INPUT_BASE_QSFP		6
21 #define ICE_DPLL_SW_PIN_OUTPUT_BASE		0
22 
23 #define E825_RCLK_PARENT_0_PIN_IDX		0
24 #define E825_RCLK_PARENT_1_PIN_IDX		1
25 
26 #define ICE_DPLL_PIN_SW_INPUT_ABS(in_idx) \
27 	(ICE_DPLL_SW_PIN_INPUT_BASE_SFP + (in_idx))
28 
29 #define ICE_DPLL_PIN_SW_1_INPUT_ABS_IDX \
30 	(ICE_DPLL_PIN_SW_INPUT_ABS(ICE_DPLL_PIN_SW_1_IDX))
31 
32 #define ICE_DPLL_PIN_SW_2_INPUT_ABS_IDX \
33 	(ICE_DPLL_PIN_SW_INPUT_ABS(ICE_DPLL_PIN_SW_2_IDX))
34 
35 #define ICE_DPLL_PIN_SW_OUTPUT_ABS(out_idx) \
36 	(ICE_DPLL_SW_PIN_OUTPUT_BASE + (out_idx))
37 
38 #define ICE_DPLL_PIN_SW_1_OUTPUT_ABS_IDX \
39 	(ICE_DPLL_PIN_SW_OUTPUT_ABS(ICE_DPLL_PIN_SW_1_IDX))
40 
41 #define ICE_DPLL_PIN_SW_2_OUTPUT_ABS_IDX \
42 	(ICE_DPLL_PIN_SW_OUTPUT_ABS(ICE_DPLL_PIN_SW_2_IDX))
43 
44 #define ICE_SR_PFA_DPLL_DEFAULTS		0x152
45 #define ICE_DPLL_PFA_REF_SYNC_TYPE		0x2420
46 #define ICE_DPLL_PFA_REF_SYNC_TYPE2		0x2424
47 #define ICE_DPLL_PFA_END			0xFFFF
48 #define ICE_DPLL_PFA_HEADER_LEN			4
49 #define ICE_DPLL_PFA_ENTRY_LEN			3
50 #define ICE_DPLL_PFA_MAILBOX_REF_SYNC_PIN_S	4
51 #define ICE_DPLL_PFA_MASK_OFFSET		1
52 #define ICE_DPLL_PFA_VALUE_OFFSET		2
53 
54 #define ICE_DPLL_E810C_SFP_NC_PINS		2
55 #define ICE_DPLL_E810C_SFP_NC_START		4
56 
57 /**
58  * enum ice_dpll_pin_type - enumerate ice pin types:
59  * @ICE_DPLL_PIN_INVALID: invalid pin type
60  * @ICE_DPLL_PIN_TYPE_INPUT: input pin
61  * @ICE_DPLL_PIN_TYPE_OUTPUT: output pin
62  * @ICE_DPLL_PIN_TYPE_RCLK_INPUT: recovery clock input pin
63  * @ICE_DPLL_PIN_TYPE_SOFTWARE: software controlled SMA/U.FL pins
64  * @ICE_DPLL_PIN_TYPE_TXCLK: transmit clock reference input pin
65  */
66 enum ice_dpll_pin_type {
67 	ICE_DPLL_PIN_INVALID,
68 	ICE_DPLL_PIN_TYPE_INPUT,
69 	ICE_DPLL_PIN_TYPE_OUTPUT,
70 	ICE_DPLL_PIN_TYPE_RCLK_INPUT,
71 	ICE_DPLL_PIN_TYPE_SOFTWARE,
72 	ICE_DPLL_PIN_TYPE_TXCLK,
73 };
74 
75 static const char * const pin_type_name[] = {
76 	[ICE_DPLL_PIN_TYPE_INPUT] = "input",
77 	[ICE_DPLL_PIN_TYPE_OUTPUT] = "output",
78 	[ICE_DPLL_PIN_TYPE_RCLK_INPUT] = "rclk-input",
79 	[ICE_DPLL_PIN_TYPE_SOFTWARE] = "software",
80 	[ICE_DPLL_PIN_TYPE_TXCLK] = "txclk-input",
81 };
82 
83 static const char * const ice_dpll_sw_pin_sma[] = { "SMA1", "SMA2" };
84 static const char * const ice_dpll_sw_pin_ufl[] = { "U.FL1", "U.FL2" };
85 static const char * const ice_dpll_ext_eref_pin = "EXT_EREF0";
86 static const char * const ice_dpll_fwnode_ext_synce = "clk_ref_synce";
87 
88 static const struct dpll_pin_frequency ice_esync_range[] = {
89 	DPLL_PIN_FREQUENCY_RANGE(0, DPLL_PIN_FREQUENCY_1_HZ),
90 };
91 
92 /**
93  * ice_dpll_is_sw_pin - check if given pin shall be controlled by SW
94  * @pf: private board structure
95  * @index: index of a pin as understood by FW
96  * @input: true for input, false for output
97  *
98  * Check if the pin shall be controlled by SW - instead of providing raw access
99  * for pin control. For E810 NIC with dpll there is additional MUX-related logic
100  * between SMA/U.FL pins/connectors and dpll device, best to give user access
101  * with series of wrapper functions as from user perspective they convey single
102  * functionality rather then separated pins.
103  *
104  * Return:
105  * * true - pin controlled by SW
106  * * false - pin not controlled by SW
107  */
108 static bool ice_dpll_is_sw_pin(struct ice_pf *pf, u8 index, bool input)
109 {
110 	if (input && pf->hw.device_id == ICE_DEV_ID_E810C_QSFP)
111 		index -= ICE_DPLL_SW_PIN_INPUT_BASE_QSFP -
112 			 ICE_DPLL_SW_PIN_INPUT_BASE_SFP;
113 
114 	if ((input && (index == ICE_DPLL_PIN_SW_1_INPUT_ABS_IDX ||
115 		       index == ICE_DPLL_PIN_SW_2_INPUT_ABS_IDX)) ||
116 	    (!input && (index == ICE_DPLL_PIN_SW_1_OUTPUT_ABS_IDX ||
117 			index == ICE_DPLL_PIN_SW_2_OUTPUT_ABS_IDX)))
118 		return true;
119 	return false;
120 }
121 
122 /**
123  * ice_dpll_is_reset - check if reset is in progress
124  * @pf: private board structure
125  * @extack: error reporting
126  *
127  * If reset is in progress, fill extack with error.
128  *
129  * Return:
130  * * false - no reset in progress
131  * * true - reset in progress
132  */
133 static bool ice_dpll_is_reset(struct ice_pf *pf, struct netlink_ext_ack *extack)
134 {
135 	if (ice_is_reset_in_progress(pf->state)) {
136 		NL_SET_ERR_MSG(extack, "PF reset in progress");
137 		return true;
138 	}
139 	return false;
140 }
141 
142 /**
143  * ice_dpll_pin_freq_set - set pin's frequency
144  * @pf: private board structure
145  * @pin: pointer to a pin
146  * @pin_type: type of pin being configured
147  * @freq: frequency to be set
148  * @extack: error reporting
149  *
150  * Set requested frequency on a pin.
151  *
152  * Context: Called under pf->dplls.lock
153  * Return:
154  * * 0 - success
155  * * negative - error on AQ or wrong pin type given
156  */
157 static int
158 ice_dpll_pin_freq_set(struct ice_pf *pf, struct ice_dpll_pin *pin,
159 		      enum ice_dpll_pin_type pin_type, const u32 freq,
160 		      struct netlink_ext_ack *extack)
161 {
162 	u8 flags;
163 	int ret;
164 
165 	switch (pin_type) {
166 	case ICE_DPLL_PIN_TYPE_INPUT:
167 		flags = ICE_AQC_SET_CGU_IN_CFG_FLG1_UPDATE_FREQ;
168 		ret = ice_aq_set_input_pin_cfg(&pf->hw, pin->idx, flags,
169 					       pin->flags[0], freq, 0);
170 		break;
171 	case ICE_DPLL_PIN_TYPE_OUTPUT:
172 		flags = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_FREQ;
173 		ret = ice_aq_set_output_pin_cfg(&pf->hw, pin->idx, flags,
174 						0, freq, 0);
175 		break;
176 	default:
177 		return -EINVAL;
178 	}
179 	if (ret) {
180 		NL_SET_ERR_MSG_FMT(extack,
181 				   "err:%d %s failed to set pin freq:%u on pin:%u",
182 				   ret,
183 				   libie_aq_str(pf->hw.adminq.sq_last_status),
184 				   freq, pin->idx);
185 		return ret;
186 	}
187 	pin->freq = freq;
188 
189 	return 0;
190 }
191 
192 /**
193  * ice_dpll_frequency_set - wrapper for pin callback for set frequency
194  * @pin: pointer to a pin
195  * @pin_priv: private data pointer passed on pin registration
196  * @dpll: pointer to dpll
197  * @dpll_priv: private data pointer passed on dpll registration
198  * @frequency: frequency to be set
199  * @extack: error reporting
200  * @pin_type: type of pin being configured
201  *
202  * Wraps internal set frequency command on a pin.
203  *
204  * Context: Acquires pf->dplls.lock
205  * Return:
206  * * 0 - success
207  * * negative - error pin not found or couldn't set in hw
208  */
209 static int
210 ice_dpll_frequency_set(const struct dpll_pin *pin, void *pin_priv,
211 		       const struct dpll_device *dpll, void *dpll_priv,
212 		       const u32 frequency,
213 		       struct netlink_ext_ack *extack,
214 		       enum ice_dpll_pin_type pin_type)
215 {
216 	struct ice_dpll_pin *p = pin_priv;
217 	struct ice_dpll *d = dpll_priv;
218 	struct ice_pf *pf = d->pf;
219 	int ret;
220 
221 	if (ice_dpll_is_reset(pf, extack))
222 		return -EBUSY;
223 
224 	mutex_lock(&pf->dplls.lock);
225 	ret = ice_dpll_pin_freq_set(pf, p, pin_type, frequency, extack);
226 	mutex_unlock(&pf->dplls.lock);
227 
228 	return ret;
229 }
230 
231 /**
232  * ice_dpll_input_frequency_set - input pin callback for set frequency
233  * @pin: pointer to a pin
234  * @pin_priv: private data pointer passed on pin registration
235  * @dpll: pointer to dpll
236  * @dpll_priv: private data pointer passed on dpll registration
237  * @frequency: frequency to be set
238  * @extack: error reporting
239  *
240  * Wraps internal set frequency command on a pin.
241  *
242  * Context: Calls a function which acquires pf->dplls.lock
243  * Return:
244  * * 0 - success
245  * * negative - error pin not found or couldn't set in hw
246  */
247 static int
248 ice_dpll_input_frequency_set(const struct dpll_pin *pin, void *pin_priv,
249 			     const struct dpll_device *dpll, void *dpll_priv,
250 			     u64 frequency, struct netlink_ext_ack *extack)
251 {
252 	return ice_dpll_frequency_set(pin, pin_priv, dpll, dpll_priv, frequency,
253 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
254 }
255 
256 /**
257  * ice_dpll_output_frequency_set - output pin callback for set frequency
258  * @pin: pointer to a pin
259  * @pin_priv: private data pointer passed on pin registration
260  * @dpll: pointer to dpll
261  * @dpll_priv: private data pointer passed on dpll registration
262  * @frequency: frequency to be set
263  * @extack: error reporting
264  *
265  * Wraps internal set frequency command on a pin.
266  *
267  * Context: Calls a function which acquires pf->dplls.lock
268  * Return:
269  * * 0 - success
270  * * negative - error pin not found or couldn't set in hw
271  */
272 static int
273 ice_dpll_output_frequency_set(const struct dpll_pin *pin, void *pin_priv,
274 			      const struct dpll_device *dpll, void *dpll_priv,
275 			      u64 frequency, struct netlink_ext_ack *extack)
276 {
277 	return ice_dpll_frequency_set(pin, pin_priv, dpll, dpll_priv, frequency,
278 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
279 }
280 
281 /**
282  * ice_dpll_frequency_get - wrapper for pin callback for get frequency
283  * @pin: pointer to a pin
284  * @pin_priv: private data pointer passed on pin registration
285  * @dpll: pointer to dpll
286  * @dpll_priv: private data pointer passed on dpll registration
287  * @frequency: on success holds pin's frequency
288  * @extack: error reporting
289  * @pin_type: type of pin being configured
290  *
291  * Wraps internal get frequency command of a pin.
292  *
293  * Context: Acquires pf->dplls.lock
294  * Return:
295  * * 0 - success
296  * * negative - error pin not found or couldn't get from hw
297  */
298 static int
299 ice_dpll_frequency_get(const struct dpll_pin *pin, void *pin_priv,
300 		       const struct dpll_device *dpll, void *dpll_priv,
301 		       u64 *frequency, struct netlink_ext_ack *extack,
302 		       enum ice_dpll_pin_type pin_type)
303 {
304 	struct ice_dpll_pin *p = pin_priv;
305 	struct ice_dpll *d = dpll_priv;
306 	struct ice_pf *pf = d->pf;
307 
308 	mutex_lock(&pf->dplls.lock);
309 	*frequency = p->freq;
310 	mutex_unlock(&pf->dplls.lock);
311 
312 	return 0;
313 }
314 
315 /**
316  * ice_dpll_input_frequency_get - input pin callback for get frequency
317  * @pin: pointer to a pin
318  * @pin_priv: private data pointer passed on pin registration
319  * @dpll: pointer to dpll
320  * @dpll_priv: private data pointer passed on dpll registration
321  * @frequency: on success holds pin's frequency
322  * @extack: error reporting
323  *
324  * Wraps internal get frequency command of a input pin.
325  *
326  * Context: Calls a function which acquires pf->dplls.lock
327  * Return:
328  * * 0 - success
329  * * negative - error pin not found or couldn't get from hw
330  */
331 static int
332 ice_dpll_input_frequency_get(const struct dpll_pin *pin, void *pin_priv,
333 			     const struct dpll_device *dpll, void *dpll_priv,
334 			     u64 *frequency, struct netlink_ext_ack *extack)
335 {
336 	return ice_dpll_frequency_get(pin, pin_priv, dpll, dpll_priv, frequency,
337 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
338 }
339 
340 /**
341  * ice_dpll_output_frequency_get - output pin callback for get frequency
342  * @pin: pointer to a pin
343  * @pin_priv: private data pointer passed on pin registration
344  * @dpll: pointer to dpll
345  * @dpll_priv: private data pointer passed on dpll registration
346  * @frequency: on success holds pin's frequency
347  * @extack: error reporting
348  *
349  * Wraps internal get frequency command of a pin.
350  *
351  * Context: Calls a function which acquires pf->dplls.lock
352  * Return:
353  * * 0 - success
354  * * negative - error pin not found or couldn't get from hw
355  */
356 static int
357 ice_dpll_output_frequency_get(const struct dpll_pin *pin, void *pin_priv,
358 			      const struct dpll_device *dpll, void *dpll_priv,
359 			      u64 *frequency, struct netlink_ext_ack *extack)
360 {
361 	return ice_dpll_frequency_get(pin, pin_priv, dpll, dpll_priv, frequency,
362 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
363 }
364 
365 /**
366  * ice_dpll_sw_pin_frequency_set - callback to set frequency of SW pin
367  * @pin: pointer to a pin
368  * @pin_priv: private data pointer passed on pin registration
369  * @dpll: pointer to dpll
370  * @dpll_priv: private data pointer passed on dpll registration
371  * @frequency: on success holds pin's frequency
372  * @extack: error reporting
373  *
374  * Calls set frequency command for corresponding and active input/output pin.
375  *
376  * Context: Calls a function which acquires and releases pf->dplls.lock
377  * Return:
378  * * 0 - success
379  * * negative - error pin not active or couldn't get from hw
380  */
381 static int
382 ice_dpll_sw_pin_frequency_set(const struct dpll_pin *pin, void *pin_priv,
383 			      const struct dpll_device *dpll, void *dpll_priv,
384 			      u64 frequency, struct netlink_ext_ack *extack)
385 {
386 	struct ice_dpll_pin *sma = pin_priv;
387 	int ret;
388 
389 	if (!sma->active) {
390 		NL_SET_ERR_MSG(extack, "pin is not active");
391 		return -EINVAL;
392 	}
393 	if (sma->direction == DPLL_PIN_DIRECTION_INPUT)
394 		ret = ice_dpll_input_frequency_set(NULL, sma->input, dpll,
395 						   dpll_priv, frequency,
396 						   extack);
397 	else
398 		ret = ice_dpll_output_frequency_set(NULL, sma->output, dpll,
399 						    dpll_priv, frequency,
400 						    extack);
401 
402 	return ret;
403 }
404 
405 /**
406  * ice_dpll_sw_pin_frequency_get - callback for get frequency of SW pin
407  * @pin: pointer to a pin
408  * @pin_priv: private data pointer passed on pin registration
409  * @dpll: pointer to dpll
410  * @dpll_priv: private data pointer passed on dpll registration
411  * @frequency: on success holds pin's frequency
412  * @extack: error reporting
413  *
414  * Calls get frequency command for corresponding active input/output.
415  *
416  * Context: Calls a function which acquires and releases pf->dplls.lock
417  * Return:
418  * * 0 - success
419  * * negative - error pin not active or couldn't get from hw
420  */
421 static int
422 ice_dpll_sw_pin_frequency_get(const struct dpll_pin *pin, void *pin_priv,
423 			      const struct dpll_device *dpll, void *dpll_priv,
424 			      u64 *frequency, struct netlink_ext_ack *extack)
425 {
426 	struct ice_dpll_pin *sma = pin_priv;
427 	int ret;
428 
429 	if (!sma->active) {
430 		*frequency = 0;
431 		return 0;
432 	}
433 	if (sma->direction == DPLL_PIN_DIRECTION_INPUT) {
434 		ret = ice_dpll_input_frequency_get(NULL, sma->input, dpll,
435 						   dpll_priv, frequency,
436 						   extack);
437 	} else {
438 		ret = ice_dpll_output_frequency_get(NULL, sma->output, dpll,
439 						    dpll_priv, frequency,
440 						    extack);
441 	}
442 
443 	return ret;
444 }
445 
446 /**
447  * ice_dpll_pin_enable - enable a pin on dplls
448  * @hw: board private hw structure
449  * @pin: pointer to a pin
450  * @dpll_idx: dpll index to connect to output pin
451  * @pin_type: type of pin being enabled
452  * @extack: error reporting
453  *
454  * Enable a pin on both dplls. Store current state in pin->flags.
455  *
456  * Context: Called under pf->dplls.lock
457  * Return:
458  * * 0 - OK
459  * * negative - error
460  */
461 static int
462 ice_dpll_pin_enable(struct ice_hw *hw, struct ice_dpll_pin *pin,
463 		    u8 dpll_idx, enum ice_dpll_pin_type pin_type,
464 		    struct netlink_ext_ack *extack)
465 {
466 	u8 flags = 0;
467 	int ret;
468 
469 	switch (pin_type) {
470 	case ICE_DPLL_PIN_TYPE_INPUT:
471 		if (pin->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
472 			flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
473 		flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
474 		ret = ice_aq_set_input_pin_cfg(hw, pin->idx, 0, flags, 0, 0);
475 		break;
476 	case ICE_DPLL_PIN_TYPE_OUTPUT:
477 		flags = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_SRC_SEL;
478 		if (pin->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
479 			flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
480 		flags |= ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
481 		ret = ice_aq_set_output_pin_cfg(hw, pin->idx, flags, dpll_idx,
482 						0, 0);
483 		break;
484 	default:
485 		return -EINVAL;
486 	}
487 	if (ret)
488 		NL_SET_ERR_MSG_FMT(extack,
489 				   "err:%d %s failed to enable %s pin:%u",
490 				   ret, libie_aq_str(hw->adminq.sq_last_status),
491 				   pin_type_name[pin_type], pin->idx);
492 
493 	return ret;
494 }
495 
496 /**
497  * ice_dpll_pin_disable - disable a pin on dplls
498  * @hw: board private hw structure
499  * @pin: pointer to a pin
500  * @pin_type: type of pin being disabled
501  * @extack: error reporting
502  *
503  * Disable a pin on both dplls. Store current state in pin->flags.
504  *
505  * Context: Called under pf->dplls.lock
506  * Return:
507  * * 0 - OK
508  * * negative - error
509  */
510 static int
511 ice_dpll_pin_disable(struct ice_hw *hw, struct ice_dpll_pin *pin,
512 		     enum ice_dpll_pin_type pin_type,
513 		     struct netlink_ext_ack *extack)
514 {
515 	u8 flags = 0;
516 	int ret;
517 
518 	switch (pin_type) {
519 	case ICE_DPLL_PIN_TYPE_INPUT:
520 		if (pin->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
521 			flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
522 		ret = ice_aq_set_input_pin_cfg(hw, pin->idx, 0, flags, 0, 0);
523 		break;
524 	case ICE_DPLL_PIN_TYPE_OUTPUT:
525 		if (pin->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
526 			flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
527 		ret = ice_aq_set_output_pin_cfg(hw, pin->idx, flags, 0, 0, 0);
528 		break;
529 	default:
530 		return -EINVAL;
531 	}
532 	if (ret)
533 		NL_SET_ERR_MSG_FMT(extack,
534 				   "err:%d %s failed to disable %s pin:%u",
535 				   ret, libie_aq_str(hw->adminq.sq_last_status),
536 				   pin_type_name[pin_type], pin->idx);
537 
538 	return ret;
539 }
540 
541 /**
542  * ice_dpll_pin_store_state - updates the state of pin in SW bookkeeping
543  * @pin: pointer to a pin
544  * @parent: parent pin index
545  * @state: pin state (connected or disconnected)
546  */
547 static void
548 ice_dpll_pin_store_state(struct ice_dpll_pin *pin, int parent, bool state)
549 {
550 	pin->state[parent] = state ? DPLL_PIN_STATE_CONNECTED :
551 					DPLL_PIN_STATE_DISCONNECTED;
552 }
553 
554 /**
555  * ice_dpll_rclk_update_e825c - updates the state of rclk pin on e825c device
556  * @pf: private board struct
557  * @pin: pointer to a pin
558  *
559  * Update struct holding pin states info, states are separate for each parent
560  *
561  * Context: Called under pf->dplls.lock
562  * Return:
563  * * 0 - OK
564  * * negative - error
565  */
566 static int ice_dpll_rclk_update_e825c(struct ice_pf *pf,
567 				      struct ice_dpll_pin *pin)
568 {
569 	u8 rclk_bits;
570 	int err;
571 	u32 reg;
572 
573 	if (pf->dplls.rclk.num_parents > ICE_SYNCE_CLK_NUM)
574 		return -EINVAL;
575 
576 	err = ice_read_cgu_reg(&pf->hw, ICE_CGU_R10, &reg);
577 	if (err)
578 		return err;
579 
580 	rclk_bits = FIELD_GET(ICE_CGU_R10_SYNCE_S_REF_CLK, reg);
581 	ice_dpll_pin_store_state(pin, ICE_SYNCE_CLK0, rclk_bits ==
582 		(pf->ptp.port.port_num + ICE_CGU_BYPASS_MUX_OFFSET_E825C));
583 
584 	err = ice_read_cgu_reg(&pf->hw, ICE_CGU_R11, &reg);
585 	if (err)
586 		return err;
587 
588 	rclk_bits = FIELD_GET(ICE_CGU_R11_SYNCE_S_BYP_CLK, reg);
589 	ice_dpll_pin_store_state(pin, ICE_SYNCE_CLK1, rclk_bits ==
590 		(pf->ptp.port.port_num + ICE_CGU_BYPASS_MUX_OFFSET_E825C));
591 
592 	return 0;
593 }
594 
595 /**
596  * ice_dpll_rclk_update - updates the state of rclk pin on a device
597  * @pf: private board struct
598  * @pin: pointer to a pin
599  * @port_num: port number
600  *
601  * Update struct holding pin states info, states are separate for each parent
602  *
603  * Context: Called under pf->dplls.lock
604  * Return:
605  * * 0 - OK
606  * * negative - error
607  */
608 static int ice_dpll_rclk_update(struct ice_pf *pf, struct ice_dpll_pin *pin,
609 				u8 port_num)
610 {
611 	int ret;
612 
613 	for (u8 parent = 0; parent < pf->dplls.rclk.num_parents; parent++) {
614 		u8 p = parent;
615 
616 		ret = ice_aq_get_phy_rec_clk_out(&pf->hw, &p, &port_num,
617 						 &pin->flags[parent], NULL);
618 		if (ret)
619 			return ret;
620 
621 		ice_dpll_pin_store_state(pin, parent,
622 					 ICE_AQC_GET_PHY_REC_CLK_OUT_OUT_EN &
623 					 pin->flags[parent]);
624 	}
625 
626 	return 0;
627 }
628 
629 /**
630  * ice_dpll_sw_pins_update - update status of all SW pins
631  * @pf: private board struct
632  *
633  * Determine and update pin struct fields (direction/active) of their current
634  * values for all the SW controlled pins.
635  *
636  * Context: Call with pf->dplls.lock held
637  * Return:
638  * * 0 - OK
639  * * negative - error
640  */
641 static int
642 ice_dpll_sw_pins_update(struct ice_pf *pf)
643 {
644 	struct ice_dplls *d = &pf->dplls;
645 	struct ice_dpll_pin *p;
646 	u8 data = 0;
647 	int ret;
648 
649 	ret = ice_read_sma_ctrl(&pf->hw, &data);
650 	if (ret)
651 		return ret;
652 	/* no change since last check */
653 	if (d->sma_data == data)
654 		return 0;
655 
656 	/*
657 	 * SMA1/U.FL1 vs SMA2/U.FL2 are using different bit scheme to decide
658 	 * on their direction and if are active
659 	 */
660 	p = &d->sma[ICE_DPLL_PIN_SW_1_IDX];
661 	p->active = true;
662 	p->direction = DPLL_PIN_DIRECTION_INPUT;
663 	if (data & ICE_SMA1_DIR_EN) {
664 		p->direction = DPLL_PIN_DIRECTION_OUTPUT;
665 		if (data & ICE_SMA1_TX_EN)
666 			p->active = false;
667 	}
668 
669 	p = &d->sma[ICE_DPLL_PIN_SW_2_IDX];
670 	p->active = true;
671 	p->direction = DPLL_PIN_DIRECTION_INPUT;
672 	if ((data & ICE_SMA2_INACTIVE_MASK) == ICE_SMA2_INACTIVE_MASK)
673 		p->active = false;
674 	else if (data & ICE_SMA2_DIR_EN)
675 		p->direction = DPLL_PIN_DIRECTION_OUTPUT;
676 
677 	p = &d->ufl[ICE_DPLL_PIN_SW_1_IDX];
678 	if (!(data & (ICE_SMA1_DIR_EN | ICE_SMA1_TX_EN)))
679 		p->active = true;
680 	else
681 		p->active = false;
682 
683 	p = &d->ufl[ICE_DPLL_PIN_SW_2_IDX];
684 	p->active = (data & ICE_SMA2_DIR_EN) && !(data & ICE_SMA2_UFL2_RX_DIS);
685 	d->sma_data = data;
686 
687 	return 0;
688 }
689 
690 /**
691  * ice_dpll_pin_state_update - update pin's state
692  * @pf: private board struct
693  * @pin: structure with pin attributes to be updated
694  * @pin_type: type of pin being updated
695  * @extack: error reporting
696  *
697  * Determine pin current state and frequency, then update struct
698  * holding the pin info. For input pin states are separated for each
699  * dpll, for rclk pins states are separated for each parent.
700  *
701  * Context: Called under pf->dplls.lock
702  * Return:
703  * * 0 - OK
704  * * negative - error
705  */
706 static int
707 ice_dpll_pin_state_update(struct ice_pf *pf, struct ice_dpll_pin *pin,
708 			  enum ice_dpll_pin_type pin_type,
709 			  struct netlink_ext_ack *extack)
710 {
711 	u8 parent, port_num = ICE_AQC_SET_PHY_REC_CLK_OUT_CURR_PORT;
712 	int ret;
713 
714 	switch (pin_type) {
715 	case ICE_DPLL_PIN_TYPE_INPUT:
716 		ret = ice_aq_get_input_pin_cfg(&pf->hw, pin->idx, &pin->status,
717 					       NULL, NULL, &pin->flags[0],
718 					       &pin->freq, &pin->phase_adjust);
719 		if (ret)
720 			goto err;
721 		if (ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN & pin->flags[0]) {
722 			if (pin->pin) {
723 				pin->state[pf->dplls.eec.dpll_idx] =
724 					pin->pin == pf->dplls.eec.active_input ?
725 					DPLL_PIN_STATE_CONNECTED :
726 					DPLL_PIN_STATE_SELECTABLE;
727 				pin->state[pf->dplls.pps.dpll_idx] =
728 					pin->pin == pf->dplls.pps.active_input ?
729 					DPLL_PIN_STATE_CONNECTED :
730 					DPLL_PIN_STATE_SELECTABLE;
731 			} else {
732 				pin->state[pf->dplls.eec.dpll_idx] =
733 					DPLL_PIN_STATE_SELECTABLE;
734 				pin->state[pf->dplls.pps.dpll_idx] =
735 					DPLL_PIN_STATE_SELECTABLE;
736 			}
737 		} else {
738 			pin->state[pf->dplls.eec.dpll_idx] =
739 				DPLL_PIN_STATE_DISCONNECTED;
740 			pin->state[pf->dplls.pps.dpll_idx] =
741 				DPLL_PIN_STATE_DISCONNECTED;
742 		}
743 		break;
744 	case ICE_DPLL_PIN_TYPE_OUTPUT:
745 		ret = ice_aq_get_output_pin_cfg(&pf->hw, pin->idx,
746 						&pin->flags[0], &parent,
747 						&pin->freq, NULL);
748 		if (ret)
749 			goto err;
750 
751 		parent &= ICE_AQC_GET_CGU_OUT_CFG_DPLL_SRC_SEL;
752 		if (ICE_AQC_GET_CGU_OUT_CFG_OUT_EN & pin->flags[0]) {
753 			pin->state[pf->dplls.eec.dpll_idx] =
754 				parent == pf->dplls.eec.dpll_idx ?
755 				DPLL_PIN_STATE_CONNECTED :
756 				DPLL_PIN_STATE_DISCONNECTED;
757 			pin->state[pf->dplls.pps.dpll_idx] =
758 				parent == pf->dplls.pps.dpll_idx ?
759 				DPLL_PIN_STATE_CONNECTED :
760 				DPLL_PIN_STATE_DISCONNECTED;
761 		} else {
762 			pin->state[pf->dplls.eec.dpll_idx] =
763 				DPLL_PIN_STATE_DISCONNECTED;
764 			pin->state[pf->dplls.pps.dpll_idx] =
765 				DPLL_PIN_STATE_DISCONNECTED;
766 		}
767 		break;
768 	case ICE_DPLL_PIN_TYPE_RCLK_INPUT:
769 		if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825) {
770 			ret = ice_dpll_rclk_update_e825c(pf, pin);
771 			if (ret)
772 				goto err;
773 		} else {
774 			ret = ice_dpll_rclk_update(pf, pin, port_num);
775 			if (ret)
776 				goto err;
777 		}
778 		break;
779 	case ICE_DPLL_PIN_TYPE_SOFTWARE:
780 		ret = ice_dpll_sw_pins_update(pf);
781 		if (ret)
782 			goto err;
783 		break;
784 	default:
785 		return -EINVAL;
786 	}
787 
788 	return 0;
789 err:
790 	if (extack)
791 		NL_SET_ERR_MSG_FMT(extack,
792 				   "err:%d %s failed to update %s pin:%u",
793 				   ret,
794 				   libie_aq_str(pf->hw.adminq.sq_last_status),
795 				   pin_type_name[pin_type], pin->idx);
796 	else
797 		dev_err_ratelimited(ice_pf_to_dev(pf),
798 				    "err:%d %s failed to update %s pin:%u\n",
799 				    ret,
800 				    libie_aq_str(pf->hw.adminq.sq_last_status),
801 				    pin_type_name[pin_type], pin->idx);
802 	return ret;
803 }
804 
805 /**
806  * ice_dpll_hw_input_prio_set - set input priority value in hardware
807  * @pf: board private structure
808  * @dpll: ice dpll pointer
809  * @pin: ice pin pointer
810  * @prio: priority value being set on a dpll
811  * @extack: error reporting
812  *
813  * Internal wrapper for setting the priority in the hardware.
814  *
815  * Context: Called under pf->dplls.lock
816  * Return:
817  * * 0 - success
818  * * negative - failure
819  */
820 static int
821 ice_dpll_hw_input_prio_set(struct ice_pf *pf, struct ice_dpll *dpll,
822 			   struct ice_dpll_pin *pin, const u32 prio,
823 			   struct netlink_ext_ack *extack)
824 {
825 	int ret;
826 
827 	ret = ice_aq_set_cgu_ref_prio(&pf->hw, dpll->dpll_idx, pin->idx,
828 				      (u8)prio);
829 	if (ret)
830 		NL_SET_ERR_MSG_FMT(extack,
831 				   "err:%d %s failed to set pin prio:%u on pin:%u",
832 				   ret,
833 				   libie_aq_str(pf->hw.adminq.sq_last_status),
834 				   prio, pin->idx);
835 	else
836 		dpll->input_prio[pin->idx] = prio;
837 
838 	return ret;
839 }
840 
841 /**
842  * ice_dpll_lock_status_get - get dpll lock status callback
843  * @dpll: registered dpll pointer
844  * @dpll_priv: private data pointer passed on dpll registration
845  * @status: on success holds dpll's lock status
846  * @status_error: status error value
847  * @extack: error reporting
848  *
849  * Dpll subsystem callback, provides dpll's lock status.
850  *
851  * Context: Acquires pf->dplls.lock
852  * Return:
853  * * 0 - success
854  * * negative - failure
855  */
856 static int
857 ice_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv,
858 			 enum dpll_lock_status *status,
859 			 enum dpll_lock_status_error *status_error,
860 			 struct netlink_ext_ack *extack)
861 {
862 	struct ice_dpll *d = dpll_priv;
863 	struct ice_pf *pf = d->pf;
864 
865 	mutex_lock(&pf->dplls.lock);
866 	*status = d->dpll_state;
867 	mutex_unlock(&pf->dplls.lock);
868 
869 	return 0;
870 }
871 
872 /**
873  * ice_dpll_mode_get - get dpll's working mode
874  * @dpll: registered dpll pointer
875  * @dpll_priv: private data pointer passed on dpll registration
876  * @mode: on success holds current working mode of dpll
877  * @extack: error reporting
878  *
879  * Dpll subsystem callback. Provides working mode of dpll.
880  *
881  * Context: Acquires pf->dplls.lock
882  * Return:
883  * * 0 - success
884  * * negative - failure
885  */
886 static int ice_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv,
887 			     enum dpll_mode *mode,
888 			     struct netlink_ext_ack *extack)
889 {
890 	struct ice_dpll *d = dpll_priv;
891 	struct ice_pf *pf = d->pf;
892 
893 	mutex_lock(&pf->dplls.lock);
894 	*mode = d->mode;
895 	mutex_unlock(&pf->dplls.lock);
896 
897 	return 0;
898 }
899 
900 /**
901  * ice_dpll_phase_offset_monitor_set - set phase offset monitor state
902  * @dpll: registered dpll pointer
903  * @dpll_priv: private data pointer passed on dpll registration
904  * @state: feature state to be set
905  * @extack: error reporting
906  *
907  * Dpll subsystem callback. Enable/disable phase offset monitor feature of dpll.
908  *
909  * Context: Acquires and releases pf->dplls.lock
910  * Return: 0 - success
911  */
912 static int ice_dpll_phase_offset_monitor_set(const struct dpll_device *dpll,
913 					     void *dpll_priv,
914 					     enum dpll_feature_state state,
915 					     struct netlink_ext_ack *extack)
916 {
917 	struct ice_dpll *d = dpll_priv;
918 	struct ice_pf *pf = d->pf;
919 
920 	mutex_lock(&pf->dplls.lock);
921 	if (state == DPLL_FEATURE_STATE_ENABLE)
922 		d->phase_offset_monitor_period = ICE_DPLL_PHASE_OFFSET_PERIOD;
923 	else
924 		d->phase_offset_monitor_period = 0;
925 	mutex_unlock(&pf->dplls.lock);
926 
927 	return 0;
928 }
929 
930 /**
931  * ice_dpll_phase_offset_monitor_get - get phase offset monitor state
932  * @dpll: registered dpll pointer
933  * @dpll_priv: private data pointer passed on dpll registration
934  * @state: on success holds current state of phase offset monitor
935  * @extack: error reporting
936  *
937  * Dpll subsystem callback. Provides current state of phase offset monitor
938  * features on dpll device.
939  *
940  * Context: Acquires and releases pf->dplls.lock
941  * Return: 0 - success
942  */
943 static int ice_dpll_phase_offset_monitor_get(const struct dpll_device *dpll,
944 					     void *dpll_priv,
945 					     enum dpll_feature_state *state,
946 					     struct netlink_ext_ack *extack)
947 {
948 	struct ice_dpll *d = dpll_priv;
949 	struct ice_pf *pf = d->pf;
950 
951 	mutex_lock(&pf->dplls.lock);
952 	if (d->phase_offset_monitor_period)
953 		*state = DPLL_FEATURE_STATE_ENABLE;
954 	else
955 		*state = DPLL_FEATURE_STATE_DISABLE;
956 	mutex_unlock(&pf->dplls.lock);
957 
958 	return 0;
959 }
960 
961 /**
962  * ice_dpll_pin_state_set - set pin's state on dpll
963  * @pin: pointer to a pin
964  * @pin_priv: private data pointer passed on pin registration
965  * @dpll: registered dpll pointer
966  * @dpll_priv: private data pointer passed on dpll registration
967  * @enable: if pin shalll be enabled
968  * @extack: error reporting
969  * @pin_type: type of a pin
970  *
971  * Set pin state on a pin.
972  *
973  * Context: Acquires pf->dplls.lock
974  * Return:
975  * * 0 - OK or no change required
976  * * negative - error
977  */
978 static int
979 ice_dpll_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
980 		       const struct dpll_device *dpll, void *dpll_priv,
981 		       bool enable, struct netlink_ext_ack *extack,
982 		       enum ice_dpll_pin_type pin_type)
983 {
984 	struct ice_dpll_pin *p = pin_priv;
985 	struct ice_dpll *d = dpll_priv;
986 	struct ice_pf *pf = d->pf;
987 	int ret;
988 
989 	if (ice_dpll_is_reset(pf, extack))
990 		return -EBUSY;
991 
992 	mutex_lock(&pf->dplls.lock);
993 	if (enable)
994 		ret = ice_dpll_pin_enable(&pf->hw, p, d->dpll_idx, pin_type,
995 					  extack);
996 	else
997 		ret = ice_dpll_pin_disable(&pf->hw, p, pin_type, extack);
998 	if (!ret)
999 		ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
1000 	mutex_unlock(&pf->dplls.lock);
1001 
1002 	return ret;
1003 }
1004 
1005 /**
1006  * ice_dpll_output_state_set - enable/disable output pin on dpll device
1007  * @pin: pointer to a pin
1008  * @pin_priv: private data pointer passed on pin registration
1009  * @dpll: dpll being configured
1010  * @dpll_priv: private data pointer passed on dpll registration
1011  * @state: state of pin to be set
1012  * @extack: error reporting
1013  *
1014  * Dpll subsystem callback. Set given state on output type pin.
1015  *
1016  * Context: Calls a function which acquires pf->dplls.lock
1017  * Return:
1018  * * 0 - successfully enabled mode
1019  * * negative - failed to enable mode
1020  */
1021 static int
1022 ice_dpll_output_state_set(const struct dpll_pin *pin, void *pin_priv,
1023 			  const struct dpll_device *dpll, void *dpll_priv,
1024 			  enum dpll_pin_state state,
1025 			  struct netlink_ext_ack *extack)
1026 {
1027 	bool enable = state == DPLL_PIN_STATE_CONNECTED;
1028 	struct ice_dpll_pin *p = pin_priv;
1029 	struct ice_dpll *d = dpll_priv;
1030 
1031 	if (state == DPLL_PIN_STATE_SELECTABLE)
1032 		return -EINVAL;
1033 	if (!enable && p->state[d->dpll_idx] == DPLL_PIN_STATE_DISCONNECTED)
1034 		return 0;
1035 
1036 	return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
1037 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
1038 }
1039 
1040 /**
1041  * ice_dpll_input_state_set - enable/disable input pin on dpll levice
1042  * @pin: pointer to a pin
1043  * @pin_priv: private data pointer passed on pin registration
1044  * @dpll: dpll being configured
1045  * @dpll_priv: private data pointer passed on dpll registration
1046  * @state: state of pin to be set
1047  * @extack: error reporting
1048  *
1049  * Dpll subsystem callback. Enables given mode on input type pin.
1050  *
1051  * Context: Calls a function which acquires pf->dplls.lock
1052  * Return:
1053  * * 0 - successfully enabled mode
1054  * * negative - failed to enable mode
1055  */
1056 static int
1057 ice_dpll_input_state_set(const struct dpll_pin *pin, void *pin_priv,
1058 			 const struct dpll_device *dpll, void *dpll_priv,
1059 			 enum dpll_pin_state state,
1060 			 struct netlink_ext_ack *extack)
1061 {
1062 	bool enable = state == DPLL_PIN_STATE_SELECTABLE;
1063 
1064 	return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
1065 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
1066 }
1067 
1068 /**
1069  * ice_dpll_pin_state_get - set pin's state on dpll
1070  * @pin: pointer to a pin
1071  * @pin_priv: private data pointer passed on pin registration
1072  * @dpll: registered dpll pointer
1073  * @dpll_priv: private data pointer passed on dpll registration
1074  * @state: on success holds state of the pin
1075  * @extack: error reporting
1076  * @pin_type: type of questioned pin
1077  *
1078  * Determine pin state set it on a pin.
1079  *
1080  * Context: Acquires pf->dplls.lock
1081  * Return:
1082  * * 0 - success
1083  * * negative - failed to get state
1084  */
1085 static int
1086 ice_dpll_pin_state_get(const struct dpll_pin *pin, void *pin_priv,
1087 		       const struct dpll_device *dpll, void *dpll_priv,
1088 		       enum dpll_pin_state *state,
1089 		       struct netlink_ext_ack *extack,
1090 		       enum ice_dpll_pin_type pin_type)
1091 {
1092 	struct ice_dpll_pin *p = pin_priv;
1093 	struct ice_dpll *d = dpll_priv;
1094 	struct ice_pf *pf = d->pf;
1095 	int ret;
1096 
1097 	if (ice_dpll_is_reset(pf, extack))
1098 		return -EBUSY;
1099 
1100 	mutex_lock(&pf->dplls.lock);
1101 	ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
1102 	if (ret)
1103 		goto unlock;
1104 	if (pin_type == ICE_DPLL_PIN_TYPE_INPUT ||
1105 	    pin_type == ICE_DPLL_PIN_TYPE_OUTPUT)
1106 		*state = p->state[d->dpll_idx];
1107 	ret = 0;
1108 unlock:
1109 	mutex_unlock(&pf->dplls.lock);
1110 
1111 	return ret;
1112 }
1113 
1114 /**
1115  * ice_dpll_output_state_get - get output pin state on dpll device
1116  * @pin: pointer to a pin
1117  * @pin_priv: private data pointer passed on pin registration
1118  * @dpll: registered dpll pointer
1119  * @dpll_priv: private data pointer passed on dpll registration
1120  * @state: on success holds state of the pin
1121  * @extack: error reporting
1122  *
1123  * Dpll subsystem callback. Check state of a pin.
1124  *
1125  * Context: Calls a function which acquires pf->dplls.lock
1126  * Return:
1127  * * 0 - success
1128  * * negative - failed to get state
1129  */
1130 static int
1131 ice_dpll_output_state_get(const struct dpll_pin *pin, void *pin_priv,
1132 			  const struct dpll_device *dpll, void *dpll_priv,
1133 			  enum dpll_pin_state *state,
1134 			  struct netlink_ext_ack *extack)
1135 {
1136 	return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
1137 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
1138 }
1139 
1140 /**
1141  * ice_dpll_input_state_get - get input pin state on dpll device
1142  * @pin: pointer to a pin
1143  * @pin_priv: private data pointer passed on pin registration
1144  * @dpll: registered dpll pointer
1145  * @dpll_priv: private data pointer passed on dpll registration
1146  * @state: on success holds state of the pin
1147  * @extack: error reporting
1148  *
1149  * Dpll subsystem callback. Check state of a input pin.
1150  *
1151  * Context: Calls a function which acquires pf->dplls.lock
1152  * Return:
1153  * * 0 - success
1154  * * negative - failed to get state
1155  */
1156 static int
1157 ice_dpll_input_state_get(const struct dpll_pin *pin, void *pin_priv,
1158 			 const struct dpll_device *dpll, void *dpll_priv,
1159 			 enum dpll_pin_state *state,
1160 			 struct netlink_ext_ack *extack)
1161 {
1162 	return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
1163 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
1164 }
1165 
1166 /**
1167  * ice_dpll_sw_pin_notify_peer - notify the paired SW pin after a state change
1168  * @d: pointer to dplls struct
1169  * @changed: the SW pin that was explicitly changed (already notified by dpll core)
1170  *
1171  * SMA and U.FL pins share physical signal paths in pairs (SMA1/U.FL1 and
1172  * SMA2/U.FL2).  When one pin's routing changes via the PCA9575 GPIO
1173  * expander, the paired pin's state may also change.  Send a change
1174  * notification for the peer pin so userspace consumers monitoring the
1175  * peer via dpll netlink learn about the update.
1176  *
1177  * Context: Called from dpll_pin_ops callbacks after pf->dplls.lock is
1178  *          released.  Uses __dpll_pin_change_ntf() because dpll_lock is
1179  *          still held by the dpll netlink layer.
1180  */
1181 static void ice_dpll_sw_pin_notify_peer(struct ice_dplls *d,
1182 					struct ice_dpll_pin *changed)
1183 {
1184 	struct ice_dpll_pin *peer;
1185 
1186 	peer = (changed >= d->sma && changed < d->sma + ICE_DPLL_PIN_SW_NUM) ?
1187 		&d->ufl[changed->idx] : &d->sma[changed->idx];
1188 	if (peer->pin)
1189 		__dpll_pin_change_ntf(peer->pin);
1190 }
1191 
1192 /**
1193  * ice_dpll_sma_direction_set - set direction of SMA pin
1194  * @p: pointer to a pin
1195  * @direction: requested direction of the pin
1196  * @extack: error reporting
1197  *
1198  * Wrapper for dpll subsystem callback. Set direction of a SMA pin.
1199  *
1200  * Context: Call with pf->dplls.lock held
1201  * Return:
1202  * * 0 - success
1203  * * negative - failed to get state
1204  */
1205 static int ice_dpll_sma_direction_set(struct ice_dpll_pin *p,
1206 				      enum dpll_pin_direction direction,
1207 				      struct netlink_ext_ack *extack)
1208 {
1209 	struct ice_dplls *d = &p->pf->dplls;
1210 	struct ice_dpll_pin *peer;
1211 	u8 data;
1212 	int ret;
1213 
1214 	if (p->direction == direction && p->active)
1215 		return 0;
1216 	ret = ice_read_sma_ctrl(&p->pf->hw, &data);
1217 	if (ret)
1218 		return ret;
1219 
1220 	switch (p->idx) {
1221 	case ICE_DPLL_PIN_SW_1_IDX:
1222 		data &= ~ICE_SMA1_MASK;
1223 		if (direction == DPLL_PIN_DIRECTION_OUTPUT)
1224 			data |= ICE_SMA1_DIR_EN;
1225 		break;
1226 	case ICE_DPLL_PIN_SW_2_IDX:
1227 		if (direction == DPLL_PIN_DIRECTION_INPUT) {
1228 			data &= ~ICE_SMA2_DIR_EN;
1229 			data |= ICE_SMA2_UFL2_RX_DIS;
1230 		} else {
1231 			data &= ~(ICE_SMA2_TX_EN | ICE_SMA2_UFL2_RX_DIS);
1232 			data |= ICE_SMA2_DIR_EN;
1233 		}
1234 		break;
1235 	default:
1236 		return -EINVAL;
1237 	}
1238 	ret = ice_write_sma_ctrl(&p->pf->hw, data);
1239 	if (!ret)
1240 		ret = ice_dpll_pin_state_update(p->pf, p,
1241 						ICE_DPLL_PIN_TYPE_SOFTWARE,
1242 						extack);
1243 	if (ret)
1244 		return ret;
1245 
1246 	/* When a direction change activates the paired U.FL pin, enable
1247 	 * its backing CGU pin so the pin reports as connected. Without
1248 	 * this the U.FL routing is correct but the CGU pin stays disabled
1249 	 * and userspace sees the pin as disconnected.  Do not disable the
1250 	 * backing pin when U.FL becomes inactive because the SMA pin may
1251 	 * still be using it.
1252 	 */
1253 	peer = &d->ufl[p->idx];
1254 	if (peer->active) {
1255 		struct ice_dpll_pin *target;
1256 		enum ice_dpll_pin_type type;
1257 
1258 		if (peer->output) {
1259 			target = peer->output;
1260 			type = ICE_DPLL_PIN_TYPE_OUTPUT;
1261 		} else {
1262 			target = peer->input;
1263 			type = ICE_DPLL_PIN_TYPE_INPUT;
1264 		}
1265 		ret = ice_dpll_pin_enable(&p->pf->hw, target,
1266 					  d->eec.dpll_idx, type, extack);
1267 		if (!ret)
1268 			ret = ice_dpll_pin_state_update(p->pf, target,
1269 							type, extack);
1270 	}
1271 
1272 	return ret;
1273 }
1274 
1275 /**
1276  * ice_dpll_ufl_pin_state_set - set U.FL pin state on dpll device
1277  * @pin: pointer to a pin
1278  * @pin_priv: private data pointer passed on pin registration
1279  * @dpll: registered dpll pointer
1280  * @dpll_priv: private data pointer passed on dpll registration
1281  * @state: requested state of the pin
1282  * @extack: error reporting
1283  *
1284  * Dpll subsystem callback. Set the state of a pin.
1285  *
1286  * Context: Acquires and releases pf->dplls.lock
1287  * Return:
1288  * * 0 - success
1289  * * negative - error
1290  */
1291 static int
1292 ice_dpll_ufl_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
1293 			   const struct dpll_device *dpll, void *dpll_priv,
1294 			   enum dpll_pin_state state,
1295 			   struct netlink_ext_ack *extack)
1296 {
1297 	struct ice_dpll_pin *p = pin_priv, *target;
1298 	struct ice_dpll *d = dpll_priv;
1299 	enum ice_dpll_pin_type type;
1300 	struct ice_pf *pf = p->pf;
1301 	struct ice_hw *hw;
1302 	bool enable;
1303 	u8 data;
1304 	int ret;
1305 
1306 	if (ice_dpll_is_reset(pf, extack))
1307 		return -EBUSY;
1308 
1309 	mutex_lock(&pf->dplls.lock);
1310 	hw = &pf->hw;
1311 	ret = ice_read_sma_ctrl(hw, &data);
1312 	if (ret)
1313 		goto unlock;
1314 
1315 	ret = -EINVAL;
1316 	switch (p->idx) {
1317 	case ICE_DPLL_PIN_SW_1_IDX:
1318 		if (state == DPLL_PIN_STATE_CONNECTED) {
1319 			data &= ~ICE_SMA1_MASK;
1320 			enable = true;
1321 		} else if (state == DPLL_PIN_STATE_DISCONNECTED) {
1322 			/* Skip if U.FL1 is not active, setting TX_EN
1323 			 * while DIR_EN is set would also deactivate
1324 			 * the paired SMA1 output.
1325 			 */
1326 			if (data & (ICE_SMA1_DIR_EN | ICE_SMA1_TX_EN)) {
1327 				ret = 0;
1328 				goto unlock;
1329 			}
1330 			data |= ICE_SMA1_TX_EN;
1331 			enable = false;
1332 		} else {
1333 			goto unlock;
1334 		}
1335 		target = p->output;
1336 		type = ICE_DPLL_PIN_TYPE_OUTPUT;
1337 		break;
1338 	case ICE_DPLL_PIN_SW_2_IDX:
1339 		if (state == DPLL_PIN_STATE_SELECTABLE) {
1340 			data |= ICE_SMA2_DIR_EN;
1341 			data &= ~ICE_SMA2_UFL2_RX_DIS;
1342 			enable = true;
1343 		} else if (state == DPLL_PIN_STATE_DISCONNECTED) {
1344 			/* Skip if U.FL2 is not active, setting
1345 			 * UFL2_RX_DIS could also disable the paired
1346 			 * SMA2 input.
1347 			 */
1348 			if (!(data & ICE_SMA2_DIR_EN) ||
1349 			    (data & ICE_SMA2_UFL2_RX_DIS)) {
1350 				ret = 0;
1351 				goto unlock;
1352 			}
1353 			data |= ICE_SMA2_UFL2_RX_DIS;
1354 			enable = false;
1355 		} else {
1356 			goto unlock;
1357 		}
1358 		target = p->input;
1359 		type = ICE_DPLL_PIN_TYPE_INPUT;
1360 		break;
1361 	default:
1362 		goto unlock;
1363 	}
1364 
1365 	ret = ice_write_sma_ctrl(hw, data);
1366 	if (ret)
1367 		goto unlock;
1368 	ret = ice_dpll_pin_state_update(pf, p, ICE_DPLL_PIN_TYPE_SOFTWARE,
1369 					extack);
1370 	if (ret)
1371 		goto unlock;
1372 
1373 	if (enable)
1374 		ret = ice_dpll_pin_enable(hw, target, d->dpll_idx, type, extack);
1375 	else
1376 		ret = ice_dpll_pin_disable(hw, target, type, extack);
1377 	if (!ret)
1378 		ret = ice_dpll_pin_state_update(pf, target, type, extack);
1379 
1380 unlock:
1381 	mutex_unlock(&pf->dplls.lock);
1382 	if (!ret)
1383 		ice_dpll_sw_pin_notify_peer(&pf->dplls, p);
1384 
1385 	return ret;
1386 }
1387 
1388 /**
1389  * ice_dpll_sw_pin_state_get - get SW pin state
1390  * @pin: pointer to a pin
1391  * @pin_priv: private data pointer passed on pin registration
1392  * @dpll: registered dpll pointer
1393  * @dpll_priv: private data pointer passed on dpll registration
1394  * @state: on success holds state of the pin
1395  * @extack: error reporting
1396  *
1397  * Dpll subsystem callback. Check state of a SW pin.
1398  *
1399  * Context: Acquires and releases pf->dplls.lock
1400  * Return:
1401  * * 0 - success
1402  * * negative - error
1403  */
1404 static int
1405 ice_dpll_sw_pin_state_get(const struct dpll_pin *pin, void *pin_priv,
1406 			  const struct dpll_device *dpll, void *dpll_priv,
1407 			  enum dpll_pin_state *state,
1408 			  struct netlink_ext_ack *extack)
1409 {
1410 	struct ice_dpll_pin *p = pin_priv;
1411 	struct ice_dpll *d = dpll_priv;
1412 	struct ice_pf *pf = p->pf;
1413 	int ret = 0;
1414 
1415 	if (ice_dpll_is_reset(pf, extack))
1416 		return -EBUSY;
1417 	mutex_lock(&pf->dplls.lock);
1418 	if (!p->active) {
1419 		*state = DPLL_PIN_STATE_DISCONNECTED;
1420 		goto unlock;
1421 	}
1422 
1423 	if (p->direction == DPLL_PIN_DIRECTION_INPUT) {
1424 		ret = ice_dpll_pin_state_update(pf, p->input,
1425 						ICE_DPLL_PIN_TYPE_INPUT,
1426 						extack);
1427 		if (ret)
1428 			goto unlock;
1429 		*state = p->input->state[d->dpll_idx];
1430 	} else {
1431 		ret = ice_dpll_pin_state_update(pf, p->output,
1432 						ICE_DPLL_PIN_TYPE_OUTPUT,
1433 						extack);
1434 		if (ret)
1435 			goto unlock;
1436 		*state = p->output->state[d->dpll_idx];
1437 	}
1438 unlock:
1439 	mutex_unlock(&pf->dplls.lock);
1440 
1441 	return ret;
1442 }
1443 
1444 /**
1445  * ice_dpll_sma_pin_state_set - set SMA pin state on dpll device
1446  * @pin: pointer to a pin
1447  * @pin_priv: private data pointer passed on pin registration
1448  * @dpll: registered dpll pointer
1449  * @dpll_priv: private data pointer passed on dpll registration
1450  * @state: requested state of the pin
1451  * @extack: error reporting
1452  *
1453  * Dpll subsystem callback. Set state of a pin.
1454  *
1455  * Context: Acquires and releases pf->dplls.lock
1456  * Return:
1457  * * 0 - success
1458  * * negative - failed to get state
1459  */
1460 static int
1461 ice_dpll_sma_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
1462 			   const struct dpll_device *dpll, void *dpll_priv,
1463 			   enum dpll_pin_state state,
1464 			   struct netlink_ext_ack *extack)
1465 {
1466 	struct ice_dpll_pin *sma = pin_priv, *target;
1467 	struct ice_dpll *d = dpll_priv;
1468 	struct ice_pf *pf = sma->pf;
1469 	enum ice_dpll_pin_type type;
1470 	bool enable;
1471 	int ret;
1472 
1473 	if (ice_dpll_is_reset(pf, extack))
1474 		return -EBUSY;
1475 
1476 	mutex_lock(&pf->dplls.lock);
1477 	if (!sma->active) {
1478 		ret = ice_dpll_sma_direction_set(sma, sma->direction, extack);
1479 		if (ret)
1480 			goto unlock;
1481 	}
1482 	if (sma->direction == DPLL_PIN_DIRECTION_INPUT) {
1483 		enable = state == DPLL_PIN_STATE_SELECTABLE;
1484 		target = sma->input;
1485 		type = ICE_DPLL_PIN_TYPE_INPUT;
1486 	} else {
1487 		enable = state == DPLL_PIN_STATE_CONNECTED;
1488 		target = sma->output;
1489 		type = ICE_DPLL_PIN_TYPE_OUTPUT;
1490 	}
1491 
1492 	if (enable)
1493 		ret = ice_dpll_pin_enable(&pf->hw, target, d->dpll_idx, type,
1494 					  extack);
1495 	else
1496 		ret = ice_dpll_pin_disable(&pf->hw, target, type, extack);
1497 	if (!ret)
1498 		ret = ice_dpll_pin_state_update(pf, target, type, extack);
1499 
1500 unlock:
1501 	mutex_unlock(&pf->dplls.lock);
1502 	if (!ret)
1503 		ice_dpll_sw_pin_notify_peer(&pf->dplls, sma);
1504 
1505 	return ret;
1506 }
1507 
1508 /**
1509  * ice_dpll_input_prio_get - get dpll's input prio
1510  * @pin: pointer to a pin
1511  * @pin_priv: private data pointer passed on pin registration
1512  * @dpll: registered dpll pointer
1513  * @dpll_priv: private data pointer passed on dpll registration
1514  * @prio: on success - returns input priority on dpll
1515  * @extack: error reporting
1516  *
1517  * Dpll subsystem callback. Handler for getting priority of a input pin.
1518  *
1519  * Context: Acquires pf->dplls.lock
1520  * Return:
1521  * * 0 - success
1522  * * negative - failure
1523  */
1524 static int
1525 ice_dpll_input_prio_get(const struct dpll_pin *pin, void *pin_priv,
1526 			const struct dpll_device *dpll, void *dpll_priv,
1527 			u32 *prio, struct netlink_ext_ack *extack)
1528 {
1529 	struct ice_dpll_pin *p = pin_priv;
1530 	struct ice_dpll *d = dpll_priv;
1531 	struct ice_pf *pf = d->pf;
1532 
1533 	mutex_lock(&pf->dplls.lock);
1534 	*prio = d->input_prio[p->idx];
1535 	mutex_unlock(&pf->dplls.lock);
1536 
1537 	return 0;
1538 }
1539 
1540 /**
1541  * ice_dpll_input_prio_set - set dpll input prio
1542  * @pin: pointer to a pin
1543  * @pin_priv: private data pointer passed on pin registration
1544  * @dpll: registered dpll pointer
1545  * @dpll_priv: private data pointer passed on dpll registration
1546  * @prio: input priority to be set on dpll
1547  * @extack: error reporting
1548  *
1549  * Dpll subsystem callback. Handler for setting priority of a input pin.
1550  *
1551  * Context: Acquires pf->dplls.lock
1552  * Return:
1553  * * 0 - success
1554  * * negative - failure
1555  */
1556 static int
1557 ice_dpll_input_prio_set(const struct dpll_pin *pin, void *pin_priv,
1558 			const struct dpll_device *dpll, void *dpll_priv,
1559 			u32 prio, struct netlink_ext_ack *extack)
1560 {
1561 	struct ice_dpll_pin *p = pin_priv;
1562 	struct ice_dpll *d = dpll_priv;
1563 	struct ice_pf *pf = d->pf;
1564 	int ret;
1565 
1566 	if (ice_dpll_is_reset(pf, extack))
1567 		return -EBUSY;
1568 
1569 	mutex_lock(&pf->dplls.lock);
1570 	ret = ice_dpll_hw_input_prio_set(pf, d, p, prio, extack);
1571 	mutex_unlock(&pf->dplls.lock);
1572 
1573 	return ret;
1574 }
1575 
1576 static int
1577 ice_dpll_sw_input_prio_get(const struct dpll_pin *pin, void *pin_priv,
1578 			   const struct dpll_device *dpll, void *dpll_priv,
1579 			   u32 *prio, struct netlink_ext_ack *extack)
1580 {
1581 	struct ice_dpll_pin *p = pin_priv;
1582 	struct ice_dpll *d = dpll_priv;
1583 	struct ice_pf *pf = d->pf;
1584 
1585 	mutex_lock(&pf->dplls.lock);
1586 	if (p->input && p->direction == DPLL_PIN_DIRECTION_INPUT)
1587 		*prio = d->input_prio[p->input->idx];
1588 	else
1589 		*prio = ICE_DPLL_PIN_PRIO_OUTPUT;
1590 	mutex_unlock(&pf->dplls.lock);
1591 
1592 	return 0;
1593 }
1594 
1595 static int
1596 ice_dpll_sw_input_prio_set(const struct dpll_pin *pin, void *pin_priv,
1597 			   const struct dpll_device *dpll, void *dpll_priv,
1598 			   u32 prio, struct netlink_ext_ack *extack)
1599 {
1600 	struct ice_dpll_pin *p = pin_priv;
1601 	struct ice_dpll *d = dpll_priv;
1602 	struct ice_pf *pf = d->pf;
1603 	int ret;
1604 
1605 	if (!p->input || p->direction != DPLL_PIN_DIRECTION_INPUT)
1606 		return -EINVAL;
1607 	if (ice_dpll_is_reset(pf, extack))
1608 		return -EBUSY;
1609 
1610 	mutex_lock(&pf->dplls.lock);
1611 	ret = ice_dpll_hw_input_prio_set(pf, d, p->input, prio, extack);
1612 	mutex_unlock(&pf->dplls.lock);
1613 
1614 	return ret;
1615 }
1616 
1617 /**
1618  * ice_dpll_input_direction - callback for get input pin direction
1619  * @pin: pointer to a pin
1620  * @pin_priv: private data pointer passed on pin registration
1621  * @dpll: registered dpll pointer
1622  * @dpll_priv: private data pointer passed on dpll registration
1623  * @direction: holds input pin direction
1624  * @extack: error reporting
1625  *
1626  * Dpll subsystem callback. Handler for getting direction of a input pin.
1627  *
1628  * Return:
1629  * * 0 - success
1630  */
1631 static int
1632 ice_dpll_input_direction(const struct dpll_pin *pin, void *pin_priv,
1633 			 const struct dpll_device *dpll, void *dpll_priv,
1634 			 enum dpll_pin_direction *direction,
1635 			 struct netlink_ext_ack *extack)
1636 {
1637 	*direction = DPLL_PIN_DIRECTION_INPUT;
1638 
1639 	return 0;
1640 }
1641 
1642 /**
1643  * ice_dpll_output_direction - callback for get output pin direction
1644  * @pin: pointer to a pin
1645  * @pin_priv: private data pointer passed on pin registration
1646  * @dpll: registered dpll pointer
1647  * @dpll_priv: private data pointer passed on dpll registration
1648  * @direction: holds output pin direction
1649  * @extack: error reporting
1650  *
1651  * Dpll subsystem callback. Handler for getting direction of an output pin.
1652  *
1653  * Return:
1654  * * 0 - success
1655  */
1656 static int
1657 ice_dpll_output_direction(const struct dpll_pin *pin, void *pin_priv,
1658 			  const struct dpll_device *dpll, void *dpll_priv,
1659 			  enum dpll_pin_direction *direction,
1660 			  struct netlink_ext_ack *extack)
1661 {
1662 	*direction = DPLL_PIN_DIRECTION_OUTPUT;
1663 
1664 	return 0;
1665 }
1666 
1667 /**
1668  * ice_dpll_pin_sma_direction_set - callback for set SMA pin direction
1669  * @pin: pointer to a pin
1670  * @pin_priv: private data pointer passed on pin registration
1671  * @dpll: registered dpll pointer
1672  * @dpll_priv: private data pointer passed on dpll registration
1673  * @direction: requested pin direction
1674  * @extack: error reporting
1675  *
1676  * Dpll subsystem callback. Handler for setting direction of a SMA pin.
1677  *
1678  * Context: Acquires and releases pf->dplls.lock
1679  * Return:
1680  * * 0 - success
1681  * * negative - error
1682  */
1683 static int
1684 ice_dpll_pin_sma_direction_set(const struct dpll_pin *pin, void *pin_priv,
1685 			       const struct dpll_device *dpll, void *dpll_priv,
1686 			       enum dpll_pin_direction direction,
1687 			       struct netlink_ext_ack *extack)
1688 {
1689 	struct ice_dpll_pin *p = pin_priv;
1690 	struct ice_pf *pf = p->pf;
1691 	int ret;
1692 
1693 	if (ice_dpll_is_reset(pf, extack))
1694 		return -EBUSY;
1695 
1696 	mutex_lock(&pf->dplls.lock);
1697 	ret = ice_dpll_sma_direction_set(p, direction, extack);
1698 	mutex_unlock(&pf->dplls.lock);
1699 	if (!ret)
1700 		ice_dpll_sw_pin_notify_peer(&pf->dplls, p);
1701 
1702 	return ret;
1703 }
1704 
1705 /**
1706  * ice_dpll_pin_sw_direction_get - callback for get SW pin direction
1707  * @pin: pointer to a pin
1708  * @pin_priv: private data pointer passed on pin registration
1709  * @dpll: registered dpll pointer
1710  * @dpll_priv: private data pointer passed on dpll registration
1711  * @direction: on success holds pin direction
1712  * @extack: error reporting
1713  *
1714  * Dpll subsystem callback. Handler for getting direction of a SMA pin.
1715  *
1716  * Context: Acquires and releases pf->dplls.lock
1717  * Return:
1718  * * 0 - success
1719  * * negative - error
1720  */
1721 static int
1722 ice_dpll_pin_sw_direction_get(const struct dpll_pin *pin, void *pin_priv,
1723 			      const struct dpll_device *dpll, void *dpll_priv,
1724 			      enum dpll_pin_direction *direction,
1725 			      struct netlink_ext_ack *extack)
1726 {
1727 	struct ice_dpll_pin *p = pin_priv;
1728 	struct ice_pf *pf = p->pf;
1729 
1730 	if (ice_dpll_is_reset(pf, extack))
1731 		return -EBUSY;
1732 	mutex_lock(&pf->dplls.lock);
1733 	*direction = p->direction;
1734 	mutex_unlock(&pf->dplls.lock);
1735 
1736 	return 0;
1737 }
1738 
1739 /**
1740  * ice_dpll_pin_phase_adjust_get - callback for get pin phase adjust value
1741  * @pin: pointer to a pin
1742  * @pin_priv: private data pointer passed on pin registration
1743  * @dpll: registered dpll pointer
1744  * @dpll_priv: private data pointer passed on dpll registration
1745  * @phase_adjust: on success holds pin phase_adjust value
1746  * @extack: error reporting
1747  *
1748  * Dpll subsystem callback. Handler for getting phase adjust value of a pin.
1749  *
1750  * Context: Acquires pf->dplls.lock
1751  * Return:
1752  * * 0 - success
1753  * * negative - error
1754  */
1755 static int
1756 ice_dpll_pin_phase_adjust_get(const struct dpll_pin *pin, void *pin_priv,
1757 			      const struct dpll_device *dpll, void *dpll_priv,
1758 			      s32 *phase_adjust,
1759 			      struct netlink_ext_ack *extack)
1760 {
1761 	struct ice_dpll_pin *p = pin_priv;
1762 	struct ice_pf *pf = p->pf;
1763 
1764 	mutex_lock(&pf->dplls.lock);
1765 	*phase_adjust = p->phase_adjust;
1766 	mutex_unlock(&pf->dplls.lock);
1767 
1768 	return 0;
1769 }
1770 
1771 /**
1772  * ice_dpll_pin_phase_adjust_set - helper for setting a pin phase adjust value
1773  * @pin: pointer to a pin
1774  * @pin_priv: private data pointer passed on pin registration
1775  * @dpll: registered dpll pointer
1776  * @dpll_priv: private data pointer passed on dpll registration
1777  * @phase_adjust: phase_adjust to be set
1778  * @extack: error reporting
1779  * @type: type of a pin
1780  *
1781  * Helper for dpll subsystem callback. Handler for setting phase adjust value
1782  * of a pin.
1783  *
1784  * Context: Acquires pf->dplls.lock
1785  * Return:
1786  * * 0 - success
1787  * * negative - error
1788  */
1789 static int
1790 ice_dpll_pin_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
1791 			      const struct dpll_device *dpll, void *dpll_priv,
1792 			      s32 phase_adjust,
1793 			      struct netlink_ext_ack *extack,
1794 			      enum ice_dpll_pin_type type)
1795 {
1796 	struct ice_dpll_pin *p = pin_priv;
1797 	struct ice_dpll *d = dpll_priv;
1798 	struct ice_pf *pf = d->pf;
1799 	u8 flag, flags_en = 0;
1800 	int ret;
1801 
1802 	if (ice_dpll_is_reset(pf, extack))
1803 		return -EBUSY;
1804 
1805 	mutex_lock(&pf->dplls.lock);
1806 	switch (type) {
1807 	case ICE_DPLL_PIN_TYPE_INPUT:
1808 		flag = ICE_AQC_SET_CGU_IN_CFG_FLG1_UPDATE_DELAY;
1809 		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
1810 			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
1811 		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
1812 			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
1813 		ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, flag, flags_en,
1814 					       0, phase_adjust);
1815 		break;
1816 	case ICE_DPLL_PIN_TYPE_OUTPUT:
1817 		flag = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_PHASE;
1818 		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_OUT_EN)
1819 			flag |= ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
1820 		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
1821 			flag |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
1822 		ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flag, 0, 0,
1823 						phase_adjust);
1824 		break;
1825 	default:
1826 		ret = -EINVAL;
1827 	}
1828 	if (!ret)
1829 		p->phase_adjust = phase_adjust;
1830 	mutex_unlock(&pf->dplls.lock);
1831 	if (ret)
1832 		NL_SET_ERR_MSG_FMT(extack,
1833 				   "err:%d %s failed to set pin phase_adjust:%d for pin:%u on dpll:%u",
1834 				   ret,
1835 				   libie_aq_str(pf->hw.adminq.sq_last_status),
1836 				   phase_adjust, p->idx, d->dpll_idx);
1837 
1838 	return ret;
1839 }
1840 
1841 /**
1842  * ice_dpll_input_phase_adjust_set - callback for set input pin phase adjust
1843  * @pin: pointer to a pin
1844  * @pin_priv: private data pointer passed on pin registration
1845  * @dpll: registered dpll pointer
1846  * @dpll_priv: private data pointer passed on dpll registration
1847  * @phase_adjust: phase_adjust to be set
1848  * @extack: error reporting
1849  *
1850  * Dpll subsystem callback. Wraps a handler for setting phase adjust on input
1851  * pin.
1852  *
1853  * Context: Calls a function which acquires and releases pf->dplls.lock
1854  * Return:
1855  * * 0 - success
1856  * * negative - error
1857  */
1858 static int
1859 ice_dpll_input_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
1860 				const struct dpll_device *dpll, void *dpll_priv,
1861 				s32 phase_adjust,
1862 				struct netlink_ext_ack *extack)
1863 {
1864 	return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
1865 					     phase_adjust, extack,
1866 					     ICE_DPLL_PIN_TYPE_INPUT);
1867 }
1868 
1869 /**
1870  * ice_dpll_output_phase_adjust_set - callback for set output pin phase adjust
1871  * @pin: pointer to a pin
1872  * @pin_priv: private data pointer passed on pin registration
1873  * @dpll: registered dpll pointer
1874  * @dpll_priv: private data pointer passed on dpll registration
1875  * @phase_adjust: phase_adjust to be set
1876  * @extack: error reporting
1877  *
1878  * Dpll subsystem callback. Wraps a handler for setting phase adjust on output
1879  * pin.
1880  *
1881  * Context: Calls a function which acquires pf->dplls.lock
1882  * Return:
1883  * * 0 - success
1884  * * negative - error
1885  */
1886 static int
1887 ice_dpll_output_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
1888 				 const struct dpll_device *dpll, void *dpll_priv,
1889 				 s32 phase_adjust,
1890 				 struct netlink_ext_ack *extack)
1891 {
1892 	return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
1893 					     phase_adjust, extack,
1894 					     ICE_DPLL_PIN_TYPE_OUTPUT);
1895 }
1896 
1897 /**
1898  * ice_dpll_sw_phase_adjust_get - callback for get SW pin phase adjust
1899  * @pin: pointer to a pin
1900  * @pin_priv: private data pointer passed on pin registration
1901  * @dpll: registered dpll pointer
1902  * @dpll_priv: private data pointer passed on dpll registration
1903  * @phase_adjust: on success holds phase adjust value
1904  * @extack: error reporting
1905  *
1906  * Dpll subsystem callback. Wraps a handler for getting phase adjust on sw
1907  * pin.
1908  *
1909  * Context: Calls a function which acquires and releases pf->dplls.lock
1910  * Return:
1911  * * 0 - success
1912  * * negative - error
1913  */
1914 static int
1915 ice_dpll_sw_phase_adjust_get(const struct dpll_pin *pin, void *pin_priv,
1916 			     const struct dpll_device *dpll, void *dpll_priv,
1917 			     s32 *phase_adjust,
1918 			     struct netlink_ext_ack *extack)
1919 {
1920 	struct ice_dpll_pin *p = pin_priv;
1921 
1922 	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
1923 		return ice_dpll_pin_phase_adjust_get(p->input->pin, p->input,
1924 						     dpll, dpll_priv,
1925 						     phase_adjust, extack);
1926 	else
1927 		return ice_dpll_pin_phase_adjust_get(p->output->pin, p->output,
1928 						     dpll, dpll_priv,
1929 						     phase_adjust, extack);
1930 }
1931 
1932 /**
1933  * ice_dpll_sw_phase_adjust_set - callback for set SW pin phase adjust value
1934  * @pin: pointer to a pin
1935  * @pin_priv: private data pointer passed on pin registration
1936  * @dpll: registered dpll pointer
1937  * @dpll_priv: private data pointer passed on dpll registration
1938  * @phase_adjust: phase_adjust to be set
1939  * @extack: error reporting
1940  *
1941  * Dpll subsystem callback. Wraps a handler for setting phase adjust on output
1942  * pin.
1943  *
1944  * Context: Calls a function which acquires and releases pf->dplls.lock
1945  * Return:
1946  * * 0 - success
1947  * * negative - error
1948  */
1949 static int
1950 ice_dpll_sw_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
1951 			     const struct dpll_device *dpll, void *dpll_priv,
1952 			     s32 phase_adjust,
1953 			     struct netlink_ext_ack *extack)
1954 {
1955 	struct ice_dpll_pin *p = pin_priv;
1956 
1957 	if (!p->active) {
1958 		NL_SET_ERR_MSG(extack, "pin is not active");
1959 		return -EINVAL;
1960 	}
1961 	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
1962 		return ice_dpll_pin_phase_adjust_set(p->input->pin, p->input,
1963 						     dpll, dpll_priv,
1964 						     phase_adjust, extack,
1965 						     ICE_DPLL_PIN_TYPE_INPUT);
1966 	else
1967 		return ice_dpll_pin_phase_adjust_set(p->output->pin, p->output,
1968 						     dpll, dpll_priv,
1969 						     phase_adjust, extack,
1970 						     ICE_DPLL_PIN_TYPE_OUTPUT);
1971 }
1972 
1973 #define ICE_DPLL_PHASE_OFFSET_DIVIDER	100
1974 #define ICE_DPLL_PHASE_OFFSET_FACTOR		\
1975 	(DPLL_PHASE_OFFSET_DIVIDER / ICE_DPLL_PHASE_OFFSET_DIVIDER)
1976 /**
1977  * ice_dpll_phase_offset_get - callback for get dpll phase shift value
1978  * @pin: pointer to a pin
1979  * @pin_priv: private data pointer passed on pin registration
1980  * @dpll: registered dpll pointer
1981  * @dpll_priv: private data pointer passed on dpll registration
1982  * @phase_offset: on success holds pin phase_offset value
1983  * @extack: error reporting
1984  *
1985  * Dpll subsystem callback. Handler for getting phase shift value between
1986  * dpll's input and output.
1987  *
1988  * Context: Acquires pf->dplls.lock
1989  * Return:
1990  * * 0 - success
1991  * * negative - error
1992  */
1993 static int
1994 ice_dpll_phase_offset_get(const struct dpll_pin *pin, void *pin_priv,
1995 			  const struct dpll_device *dpll, void *dpll_priv,
1996 			  s64 *phase_offset, struct netlink_ext_ack *extack)
1997 {
1998 	struct ice_dpll_pin *p = pin_priv;
1999 	struct ice_dpll *d = dpll_priv;
2000 	struct ice_pf *pf = d->pf;
2001 
2002 	mutex_lock(&pf->dplls.lock);
2003 	if (d->active_input == pin || (p->input &&
2004 				       d->active_input == p->input->pin))
2005 		*phase_offset = d->phase_offset * ICE_DPLL_PHASE_OFFSET_FACTOR;
2006 	else if (d->phase_offset_monitor_period)
2007 		*phase_offset = (p->input &&
2008 				 p->direction == DPLL_PIN_DIRECTION_INPUT ?
2009 				 p->input->phase_offset :
2010 				 p->phase_offset) * ICE_DPLL_PHASE_OFFSET_FACTOR;
2011 	else
2012 		*phase_offset = 0;
2013 	mutex_unlock(&pf->dplls.lock);
2014 
2015 	return 0;
2016 }
2017 
2018 /**
2019  * ice_dpll_synce_update_e825c - setting PHY recovered clock pins on e825c
2020  * @hw: Pointer to the HW struct
2021  * @ena: true if enable, false in disable
2022  * @port_num: port number
2023  * @output: output pin, we have two in E825C
2024  *
2025  * DPLL subsystem callback. Set proper signals to recover clock from port.
2026  *
2027  * Context: Called under pf->dplls.lock
2028  * Return:
2029  * * 0 - success
2030  * * negative - error
2031  */
2032 static int ice_dpll_synce_update_e825c(struct ice_hw *hw, bool ena,
2033 				       u32 port_num, enum ice_synce_clk output)
2034 {
2035 	int err;
2036 
2037 	/* configure the mux to deliver proper signal to DPLL from the MUX */
2038 	err = ice_tspll_cfg_bypass_mux_e825c(hw, ena, port_num, output);
2039 	if (err)
2040 		return err;
2041 
2042 	err = ice_tspll_cfg_synce_ethdiv_e825c(hw, output);
2043 	if (err)
2044 		return err;
2045 
2046 	dev_dbg(ice_hw_to_dev(hw), "CLK_SYNCE%u recovered clock: pin %s\n",
2047 		output, str_enabled_disabled(ena));
2048 
2049 	return 0;
2050 }
2051 
2052 /**
2053  * ice_dpll_output_esync_set - callback for setting embedded sync
2054  * @pin: pointer to a pin
2055  * @pin_priv: private data pointer passed on pin registration
2056  * @dpll: registered dpll pointer
2057  * @dpll_priv: private data pointer passed on dpll registration
2058  * @freq: requested embedded sync frequency
2059  * @extack: error reporting
2060  *
2061  * Dpll subsystem callback. Handler for setting embedded sync frequency value
2062  * on output pin.
2063  *
2064  * Context: Acquires pf->dplls.lock
2065  * Return:
2066  * * 0 - success
2067  * * negative - error
2068  */
2069 static int
2070 ice_dpll_output_esync_set(const struct dpll_pin *pin, void *pin_priv,
2071 			  const struct dpll_device *dpll, void *dpll_priv,
2072 			  u64 freq, struct netlink_ext_ack *extack)
2073 {
2074 	struct ice_dpll_pin *p = pin_priv;
2075 	struct ice_dpll *d = dpll_priv;
2076 	struct ice_pf *pf = d->pf;
2077 	u8 flags = 0;
2078 	int ret;
2079 
2080 	if (ice_dpll_is_reset(pf, extack))
2081 		return -EBUSY;
2082 	mutex_lock(&pf->dplls.lock);
2083 	if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_OUT_EN)
2084 		flags = ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
2085 	if (freq == DPLL_PIN_FREQUENCY_1_HZ) {
2086 		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN) {
2087 			ret = 0;
2088 		} else {
2089 			flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
2090 			ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flags,
2091 							0, 0, 0);
2092 		}
2093 	} else {
2094 		if (!(p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)) {
2095 			ret = 0;
2096 		} else {
2097 			flags &= ~ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
2098 			ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flags,
2099 							0, 0, 0);
2100 		}
2101 	}
2102 	mutex_unlock(&pf->dplls.lock);
2103 
2104 	return ret;
2105 }
2106 
2107 /**
2108  * ice_dpll_output_esync_get - callback for getting embedded sync config
2109  * @pin: pointer to a pin
2110  * @pin_priv: private data pointer passed on pin registration
2111  * @dpll: registered dpll pointer
2112  * @dpll_priv: private data pointer passed on dpll registration
2113  * @esync: on success holds embedded sync pin properties
2114  * @extack: error reporting
2115  *
2116  * Dpll subsystem callback. Handler for getting embedded sync frequency value
2117  * and capabilities on output pin.
2118  *
2119  * Context: Acquires pf->dplls.lock
2120  * Return:
2121  * * 0 - success
2122  * * negative - error
2123  */
2124 static int
2125 ice_dpll_output_esync_get(const struct dpll_pin *pin, void *pin_priv,
2126 			  const struct dpll_device *dpll, void *dpll_priv,
2127 			  struct dpll_pin_esync *esync,
2128 			  struct netlink_ext_ack *extack)
2129 {
2130 	struct ice_dpll_pin *p = pin_priv;
2131 	struct ice_dpll *d = dpll_priv;
2132 	struct ice_pf *pf = d->pf;
2133 
2134 	if (ice_dpll_is_reset(pf, extack))
2135 		return -EBUSY;
2136 	mutex_lock(&pf->dplls.lock);
2137 	if (!(p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_ABILITY) ||
2138 	    p->freq != DPLL_PIN_FREQUENCY_10_MHZ) {
2139 		mutex_unlock(&pf->dplls.lock);
2140 		return -EOPNOTSUPP;
2141 	}
2142 	esync->range = ice_esync_range;
2143 	esync->range_num = ARRAY_SIZE(ice_esync_range);
2144 	if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN) {
2145 		esync->freq = DPLL_PIN_FREQUENCY_1_HZ;
2146 		esync->pulse = ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT;
2147 	} else {
2148 		esync->freq = 0;
2149 		esync->pulse = 0;
2150 	}
2151 	mutex_unlock(&pf->dplls.lock);
2152 
2153 	return 0;
2154 }
2155 
2156 /**
2157  * ice_dpll_input_esync_set - callback for setting embedded sync
2158  * @pin: pointer to a pin
2159  * @pin_priv: private data pointer passed on pin registration
2160  * @dpll: registered dpll pointer
2161  * @dpll_priv: private data pointer passed on dpll registration
2162  * @freq: requested embedded sync frequency
2163  * @extack: error reporting
2164  *
2165  * Dpll subsystem callback. Handler for setting embedded sync frequency value
2166  * on input pin.
2167  *
2168  * Context: Acquires pf->dplls.lock
2169  * Return:
2170  * * 0 - success
2171  * * negative - error
2172  */
2173 static int
2174 ice_dpll_input_esync_set(const struct dpll_pin *pin, void *pin_priv,
2175 			 const struct dpll_device *dpll, void *dpll_priv,
2176 			 u64 freq, struct netlink_ext_ack *extack)
2177 {
2178 	struct ice_dpll_pin *p = pin_priv;
2179 	struct ice_dpll *d = dpll_priv;
2180 	struct ice_pf *pf = d->pf;
2181 	u8 flags_en = 0;
2182 	int ret;
2183 
2184 	if (ice_dpll_is_reset(pf, extack))
2185 		return -EBUSY;
2186 	mutex_lock(&pf->dplls.lock);
2187 	if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
2188 		flags_en = ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
2189 	if (freq == DPLL_PIN_FREQUENCY_1_HZ) {
2190 		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN) {
2191 			ret = 0;
2192 		} else {
2193 			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
2194 			ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, 0,
2195 						       flags_en, 0, 0);
2196 		}
2197 	} else {
2198 		if (!(p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)) {
2199 			ret = 0;
2200 		} else {
2201 			flags_en &= ~ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
2202 			ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, 0,
2203 						       flags_en, 0, 0);
2204 		}
2205 	}
2206 	mutex_unlock(&pf->dplls.lock);
2207 
2208 	return ret;
2209 }
2210 
2211 /**
2212  * ice_dpll_input_esync_get - callback for getting embedded sync config
2213  * @pin: pointer to a pin
2214  * @pin_priv: private data pointer passed on pin registration
2215  * @dpll: registered dpll pointer
2216  * @dpll_priv: private data pointer passed on dpll registration
2217  * @esync: on success holds embedded sync pin properties
2218  * @extack: error reporting
2219  *
2220  * Dpll subsystem callback. Handler for getting embedded sync frequency value
2221  * and capabilities on input pin.
2222  *
2223  * Context: Acquires pf->dplls.lock
2224  * Return:
2225  * * 0 - success
2226  * * negative - error
2227  */
2228 static int
2229 ice_dpll_input_esync_get(const struct dpll_pin *pin, void *pin_priv,
2230 			 const struct dpll_device *dpll, void *dpll_priv,
2231 			 struct dpll_pin_esync *esync,
2232 			 struct netlink_ext_ack *extack)
2233 {
2234 	struct ice_dpll_pin *p = pin_priv;
2235 	struct ice_dpll *d = dpll_priv;
2236 	struct ice_pf *pf = d->pf;
2237 
2238 	if (ice_dpll_is_reset(pf, extack))
2239 		return -EBUSY;
2240 	mutex_lock(&pf->dplls.lock);
2241 	if (!(p->status & ICE_AQC_GET_CGU_IN_CFG_STATUS_ESYNC_CAP) ||
2242 	    p->freq != DPLL_PIN_FREQUENCY_10_MHZ) {
2243 		mutex_unlock(&pf->dplls.lock);
2244 		return -EOPNOTSUPP;
2245 	}
2246 	esync->range = ice_esync_range;
2247 	esync->range_num = ARRAY_SIZE(ice_esync_range);
2248 	if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN) {
2249 		esync->freq = DPLL_PIN_FREQUENCY_1_HZ;
2250 		esync->pulse = ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT;
2251 	} else {
2252 		esync->freq = 0;
2253 		esync->pulse = 0;
2254 	}
2255 	mutex_unlock(&pf->dplls.lock);
2256 
2257 	return 0;
2258 }
2259 
2260 /**
2261  * ice_dpll_sw_esync_set - callback for setting embedded sync on SW pin
2262  * @pin: pointer to a pin
2263  * @pin_priv: private data pointer passed on pin registration
2264  * @dpll: registered dpll pointer
2265  * @dpll_priv: private data pointer passed on dpll registration
2266  * @freq: requested embedded sync frequency
2267  * @extack: error reporting
2268  *
2269  * Dpll subsystem callback. Handler for setting embedded sync frequency value
2270  * on SW pin.
2271  *
2272  * Context: Calls a function which acquires and releases pf->dplls.lock
2273  * Return:
2274  * * 0 - success
2275  * * negative - error
2276  */
2277 static int
2278 ice_dpll_sw_esync_set(const struct dpll_pin *pin, void *pin_priv,
2279 		      const struct dpll_device *dpll, void *dpll_priv,
2280 		      u64 freq, struct netlink_ext_ack *extack)
2281 {
2282 	struct ice_dpll_pin *p = pin_priv;
2283 
2284 	if (!p->active) {
2285 		NL_SET_ERR_MSG(extack, "pin is not active");
2286 		return -EINVAL;
2287 	}
2288 	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
2289 		return ice_dpll_input_esync_set(p->input->pin, p->input, dpll,
2290 						dpll_priv, freq, extack);
2291 	else
2292 		return ice_dpll_output_esync_set(p->output->pin, p->output,
2293 						 dpll, dpll_priv, freq, extack);
2294 }
2295 
2296 /**
2297  * ice_dpll_sw_esync_get - callback for getting embedded sync on SW pin
2298  * @pin: pointer to a pin
2299  * @pin_priv: private data pointer passed on pin registration
2300  * @dpll: registered dpll pointer
2301  * @dpll_priv: private data pointer passed on dpll registration
2302  * @esync: on success holds embedded sync frequency and properties
2303  * @extack: error reporting
2304  *
2305  * Dpll subsystem callback. Handler for getting embedded sync frequency value
2306  * of SW pin.
2307  *
2308  * Context: Calls a function which acquires and releases pf->dplls.lock
2309  * Return:
2310  * * 0 - success
2311  * * negative - error
2312  */
2313 static int
2314 ice_dpll_sw_esync_get(const struct dpll_pin *pin, void *pin_priv,
2315 		      const struct dpll_device *dpll, void *dpll_priv,
2316 		      struct dpll_pin_esync *esync,
2317 		      struct netlink_ext_ack *extack)
2318 {
2319 	struct ice_dpll_pin *p = pin_priv;
2320 
2321 	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
2322 		return ice_dpll_input_esync_get(p->input->pin, p->input, dpll,
2323 						dpll_priv, esync, extack);
2324 	else
2325 		return ice_dpll_output_esync_get(p->output->pin, p->output,
2326 						 dpll, dpll_priv, esync,
2327 						 extack);
2328 }
2329 
2330 /*
2331  * ice_dpll_input_ref_sync_set - callback for setting reference sync feature
2332  * @pin: pointer to a pin
2333  * @pin_priv: private data pointer passed on pin registration
2334  * @ref_pin: pin pointer for reference sync pair
2335  * @ref_pin_priv: private data pointer of ref_pin
2336  * @state: requested state for reference sync for pin pair
2337  * @extack: error reporting
2338  *
2339  * Dpll subsystem callback. Handler for setting reference sync frequency
2340  * feature for input pin.
2341  *
2342  * Context: Acquires and releases pf->dplls.lock
2343  * Return:
2344  * * 0 - success
2345  * * negative - error
2346  */
2347 static int
2348 ice_dpll_input_ref_sync_set(const struct dpll_pin *pin, void *pin_priv,
2349 			    const struct dpll_pin *ref_pin, void *ref_pin_priv,
2350 			    const enum dpll_pin_state state,
2351 			    struct netlink_ext_ack *extack)
2352 {
2353 	struct ice_dpll_pin *p = pin_priv;
2354 	struct ice_pf *pf = p->pf;
2355 	u8 flags_en = 0;
2356 	int ret;
2357 
2358 	if (ice_dpll_is_reset(pf, extack))
2359 		return -EBUSY;
2360 	mutex_lock(&pf->dplls.lock);
2361 
2362 	if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
2363 		flags_en = ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
2364 	if (state == DPLL_PIN_STATE_CONNECTED)
2365 		flags_en |= ICE_AQC_CGU_IN_CFG_FLG2_REFSYNC_EN;
2366 	ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, 0, flags_en, 0, 0);
2367 	if (!ret)
2368 		ret = ice_dpll_pin_state_update(pf, p, ICE_DPLL_PIN_TYPE_INPUT,
2369 						extack);
2370 	mutex_unlock(&pf->dplls.lock);
2371 
2372 	return ret;
2373 }
2374 
2375 /**
2376  * ice_dpll_input_ref_sync_get - callback for getting reference sync config
2377  * @pin: pointer to a pin
2378  * @pin_priv: private data pointer passed on pin registration
2379  * @ref_pin: pin pointer for reference sync pair
2380  * @ref_pin_priv: private data pointer of ref_pin
2381  * @state: on success holds reference sync state for pin pair
2382  * @extack: error reporting
2383  *
2384  * Dpll subsystem callback. Handler for setting reference sync frequency
2385  * feature for input pin.
2386  *
2387  * Context: Acquires and releases pf->dplls.lock
2388  * Return:
2389  * * 0 - success
2390  * * negative - error
2391  */
2392 static int
2393 ice_dpll_input_ref_sync_get(const struct dpll_pin *pin, void *pin_priv,
2394 			    const struct dpll_pin *ref_pin, void *ref_pin_priv,
2395 			    enum dpll_pin_state *state,
2396 			    struct netlink_ext_ack *extack)
2397 {
2398 	struct ice_dpll_pin *p = pin_priv;
2399 	struct ice_pf *pf = p->pf;
2400 
2401 	if (ice_dpll_is_reset(pf, extack))
2402 		return -EBUSY;
2403 	mutex_lock(&pf->dplls.lock);
2404 	if (p->flags[0] & ICE_AQC_CGU_IN_CFG_FLG2_REFSYNC_EN)
2405 		*state = DPLL_PIN_STATE_CONNECTED;
2406 	else
2407 		*state = DPLL_PIN_STATE_DISCONNECTED;
2408 	mutex_unlock(&pf->dplls.lock);
2409 
2410 	return 0;
2411 }
2412 
2413 /*
2414  * ice_dpll_sw_input_ref_sync_set - callback for setting reference sync feature
2415  * @pin: pointer to a pin
2416  * @pin_priv: private data pointer passed on pin registration
2417  * @ref_pin: pin pointer for reference sync pair
2418  * @ref_pin_priv: private data pointer of ref_pin
2419  * @state: requested state for reference sync for pin pair
2420  * @extack: error reporting
2421  *
2422  * Dpll subsystem callback. Handler for setting reference sync
2423  * feature for input pins.
2424  *
2425  * Context: Calls a function which acquires and releases pf->dplls.lock
2426  * Return:
2427  * * 0 - success
2428  * * negative - error
2429  */
2430 static int
2431 ice_dpll_sw_input_ref_sync_set(const struct dpll_pin *pin, void *pin_priv,
2432 			       const struct dpll_pin *ref_pin,
2433 			       void *ref_pin_priv,
2434 			       const enum dpll_pin_state state,
2435 			       struct netlink_ext_ack *extack)
2436 {
2437 	struct ice_dpll_pin *p = pin_priv;
2438 
2439 	return ice_dpll_input_ref_sync_set(pin, p->input, ref_pin, ref_pin_priv,
2440 					   state, extack);
2441 }
2442 
2443 /**
2444  * ice_dpll_sw_input_ref_sync_get - callback for getting reference sync config
2445  * @pin: pointer to a pin
2446  * @pin_priv: private data pointer passed on pin registration
2447  * @ref_pin: pin pointer for reference sync pair
2448  * @ref_pin_priv: private data pointer of ref_pin
2449  * @state: on success holds reference sync state for pin pair
2450  * @extack: error reporting
2451  *
2452  * Dpll subsystem callback. Handler for setting reference sync feature for
2453  * input pins.
2454  *
2455  * Context: Calls a function which acquires and releases pf->dplls.lock
2456  * Return:
2457  * * 0 - success
2458  * * negative - error
2459  */
2460 static int
2461 ice_dpll_sw_input_ref_sync_get(const struct dpll_pin *pin, void *pin_priv,
2462 			       const struct dpll_pin *ref_pin,
2463 			       void *ref_pin_priv,
2464 			       enum dpll_pin_state *state,
2465 			       struct netlink_ext_ack *extack)
2466 {
2467 	struct ice_dpll_pin *p = pin_priv;
2468 
2469 	return ice_dpll_input_ref_sync_get(pin, p->input, ref_pin, ref_pin_priv,
2470 					   state, extack);
2471 }
2472 
2473 static int
2474 ice_dpll_pin_get_parent_num(struct ice_dpll_pin *pin,
2475 			    const struct dpll_pin *parent)
2476 {
2477 	int i;
2478 
2479 	for (i = 0; i < pin->num_parents; i++)
2480 		if (pin->pf->dplls.inputs[pin->parent_idx[i]].pin == parent)
2481 			return i;
2482 
2483 	return -ENOENT;
2484 }
2485 
2486 static int
2487 ice_dpll_pin_get_parent_idx(struct ice_dpll_pin *pin,
2488 			    const struct dpll_pin *parent)
2489 {
2490 	int num = ice_dpll_pin_get_parent_num(pin, parent);
2491 
2492 	return num < 0 ? num : pin->parent_idx[num];
2493 }
2494 
2495 /**
2496  * ice_dpll_rclk_state_on_pin_set - set a state on rclk pin
2497  * @pin: pointer to a pin
2498  * @pin_priv: private data pointer passed on pin registration
2499  * @parent_pin: pin parent pointer
2500  * @parent_pin_priv: parent private data pointer passed on pin registration
2501  * @state: state to be set on pin
2502  * @extack: error reporting
2503  *
2504  * Dpll subsystem callback, set a state of a rclk pin on a parent pin
2505  *
2506  * Context: Acquires pf->dplls.lock
2507  * Return:
2508  * * 0 - success
2509  * * negative - failure
2510  */
2511 static int
2512 ice_dpll_rclk_state_on_pin_set(const struct dpll_pin *pin, void *pin_priv,
2513 			       const struct dpll_pin *parent_pin,
2514 			       void *parent_pin_priv,
2515 			       enum dpll_pin_state state,
2516 			       struct netlink_ext_ack *extack)
2517 {
2518 	bool enable = state == DPLL_PIN_STATE_CONNECTED;
2519 	struct ice_dpll_pin *p = pin_priv;
2520 	struct ice_pf *pf = p->pf;
2521 	struct ice_hw *hw;
2522 	int ret = -EINVAL;
2523 	int hw_idx;
2524 
2525 	hw = &pf->hw;
2526 
2527 	if (ice_dpll_is_reset(pf, extack))
2528 		return -EBUSY;
2529 
2530 	mutex_lock(&pf->dplls.lock);
2531 	hw_idx = ice_dpll_pin_get_parent_idx(p, parent_pin);
2532 	if (hw_idx < 0)
2533 		goto unlock;
2534 	hw_idx -= pf->dplls.base_rclk_idx;
2535 	if (hw_idx >= ICE_DPLL_RCLK_NUM_MAX)
2536 		goto unlock;
2537 
2538 	if ((enable && p->state[hw_idx] == DPLL_PIN_STATE_CONNECTED) ||
2539 	    (!enable && p->state[hw_idx] == DPLL_PIN_STATE_DISCONNECTED)) {
2540 		NL_SET_ERR_MSG_FMT(extack,
2541 				   "pin:%u state:%u on parent:%u already set",
2542 				   p->idx, state,
2543 				   ice_dpll_pin_get_parent_num(p, parent_pin));
2544 		goto unlock;
2545 	}
2546 
2547 	ret = hw->mac_type == ICE_MAC_GENERIC_3K_E825 ?
2548 		ice_dpll_synce_update_e825c(hw, enable,
2549 					    pf->ptp.port.port_num,
2550 					    (enum ice_synce_clk)hw_idx) :
2551 		ice_aq_set_phy_rec_clk_out(hw, hw_idx, enable, &p->freq);
2552 	if (ret)
2553 		NL_SET_ERR_MSG_FMT(extack,
2554 				   "err:%d %s failed to set pin state:%u for pin:%u on parent:%u",
2555 				   ret,
2556 				   libie_aq_str(hw->adminq.sq_last_status),
2557 				   state, p->idx,
2558 				   ice_dpll_pin_get_parent_num(p, parent_pin));
2559 unlock:
2560 	mutex_unlock(&pf->dplls.lock);
2561 
2562 	return ret;
2563 }
2564 
2565 /**
2566  * ice_dpll_rclk_state_on_pin_get - get a state of rclk pin
2567  * @pin: pointer to a pin
2568  * @pin_priv: private data pointer passed on pin registration
2569  * @parent_pin: pin parent pointer
2570  * @parent_pin_priv: pin parent priv data pointer passed on pin registration
2571  * @state: on success holds pin state on parent pin
2572  * @extack: error reporting
2573  *
2574  * dpll subsystem callback, get a state of a recovered clock pin.
2575  *
2576  * Context: Acquires pf->dplls.lock
2577  * Return:
2578  * * 0 - success
2579  * * negative - failure
2580  */
2581 static int
2582 ice_dpll_rclk_state_on_pin_get(const struct dpll_pin *pin, void *pin_priv,
2583 			       const struct dpll_pin *parent_pin,
2584 			       void *parent_pin_priv,
2585 			       enum dpll_pin_state *state,
2586 			       struct netlink_ext_ack *extack)
2587 {
2588 	struct ice_dpll_pin *p = pin_priv;
2589 	struct ice_pf *pf = p->pf;
2590 	int ret = -EINVAL;
2591 	int hw_idx;
2592 
2593 	if (ice_dpll_is_reset(pf, extack))
2594 		return -EBUSY;
2595 
2596 	mutex_lock(&pf->dplls.lock);
2597 	hw_idx = ice_dpll_pin_get_parent_idx(p, parent_pin);
2598 	if (hw_idx < 0)
2599 		goto unlock;
2600 	hw_idx -= pf->dplls.base_rclk_idx;
2601 	if (hw_idx >= ICE_DPLL_RCLK_NUM_MAX)
2602 		goto unlock;
2603 
2604 	ret = ice_dpll_pin_state_update(pf, p, ICE_DPLL_PIN_TYPE_RCLK_INPUT,
2605 					extack);
2606 	if (ret)
2607 		goto unlock;
2608 
2609 	*state = p->state[hw_idx];
2610 	ret = 0;
2611 unlock:
2612 	mutex_unlock(&pf->dplls.lock);
2613 
2614 	return ret;
2615 }
2616 
2617 /**
2618  * ice_dpll_txclk_work - apply a pending TX reference clock change
2619  * @work: work_struct embedded in struct ice_dplls
2620  *
2621  * This worker executes an outstanding TX reference clock switch request
2622  * that was previously queued via the DPLL TXCLK pin set callback.
2623  *
2624  * The worker performs only the operational part of the switch, issuing
2625  * the necessary firmware commands to request a new TX reference clock
2626  * selection (e.g. triggering an AN restart). It does not verify whether
2627  * the requested clock was ultimately accepted by the hardware.
2628  *
2629  * Hardware verification, software state reconciliation, pin state
2630  * notification, and TXC DPLL lock-status updates are performed later,
2631  * after link-up, by ice_txclk_update_and_notify().
2632  *
2633  * Context:
2634  *   - Runs in process context on pf->dplls.wq and may sleep.
2635  *   - Serializes access to shared TXCLK state using pf->dplls.lock.
2636  */
2637 static void ice_dpll_txclk_work(struct work_struct *work)
2638 {
2639 	struct ice_dplls *dplls =
2640 		container_of(work, struct ice_dplls, txclk_work);
2641 	struct ice_pf *pf = container_of(dplls, struct ice_pf, dplls);
2642 	struct dpll_pin *old_pin = NULL;
2643 	struct dpll_pin *new_pin = NULL;
2644 	enum ice_e825c_ref_clk clk;
2645 	bool do_switch;
2646 	int err;
2647 
2648 	mutex_lock(&pf->dplls.lock);
2649 	do_switch = pf->dplls.txclk_switch_requested;
2650 	clk = pf->ptp.port.tx_clk_req;
2651 	mutex_unlock(&pf->dplls.lock);
2652 
2653 	if (!do_switch)
2654 		return;
2655 
2656 	err = ice_txclk_set_clk(pf, clk);
2657 
2658 	mutex_lock(&pf->dplls.lock);
2659 	/* Only clear the request flag if no newer request arrived while
2660 	 * the lock was dropped. Otherwise leave it set so the re-queued
2661 	 * worker run picks up the updated tx_clk_req value.
2662 	 */
2663 	if (pf->ptp.port.tx_clk_req == clk)
2664 		pf->dplls.txclk_switch_requested = false;
2665 	if (err) {
2666 		/* Roll back the requested clock to match the current hardware
2667 		 * state so that ice_txclk_update_and_notify() does not
2668 		 * misinterpret a future link-up as a failed switch. Only roll
2669 		 * back if no newer request arrived in the meantime; otherwise
2670 		 * the re-queued worker run will apply the updated value.
2671 		 */
2672 		dev_err(ice_pf_to_dev(pf),
2673 			"TX clock switch to %u failed, err=%d; reverting\n",
2674 			clk, err);
2675 		if (pf->ptp.port.tx_clk_req == clk) {
2676 			/* Capture pins for post-unlock notification so that
2677 			 * userspace observes the requested pin flipping back
2678 			 * to DISCONNECTED and the effective pin to CONNECTED.
2679 			 */
2680 			new_pin = ice_txclk_get_pin(pf, clk);
2681 			old_pin = ice_txclk_get_pin(pf, pf->ptp.port.tx_clk);
2682 			pf->ptp.port.tx_clk_req = pf->ptp.port.tx_clk;
2683 		}
2684 	}
2685 	mutex_unlock(&pf->dplls.lock);
2686 
2687 	if (old_pin)
2688 		dpll_pin_change_ntf(old_pin);
2689 	if (new_pin)
2690 		dpll_pin_change_ntf(new_pin);
2691 }
2692 
2693 /**
2694  * ice_dpll_txclk_state_on_dpll_set - set a state on TX clk pin
2695  * @pin: pointer to a pin
2696  * @pin_priv: private data pointer passed on pin registration
2697  * @dpll: registered dpll pointer
2698  * @dpll_priv: private data pointer passed on dpll registration
2699  * @state: state to be set on pin
2700  * @extack: error reporting
2701  *
2702  * Dpll subsystem callback, set a state of a Tx reference clock pin
2703  *
2704  * Context: Acquires and releases pf->dplls.lock.
2705  * Return:
2706  * * 0 - success
2707  * * negative - failure
2708  */
2709 static int
2710 ice_dpll_txclk_state_on_dpll_set(const struct dpll_pin *pin, void *pin_priv,
2711 				 const struct dpll_device *dpll,
2712 				 void *dpll_priv, enum dpll_pin_state state,
2713 				 struct netlink_ext_ack *extack)
2714 {
2715 	struct ice_dpll_pin *p = pin_priv;
2716 	struct ice_pf *pf = p->pf;
2717 	enum ice_e825c_ref_clk new_clk;
2718 	int ret = 0;
2719 
2720 	if (ice_dpll_is_reset(pf, extack))
2721 		return -EBUSY;
2722 
2723 	if (state != DPLL_PIN_STATE_CONNECTED &&
2724 	    state != DPLL_PIN_STATE_DISCONNECTED) {
2725 		NL_SET_ERR_MSG(extack,
2726 			       "unsupported pin state for TX reference clock");
2727 		return -EINVAL;
2728 	}
2729 
2730 	/* Check ICE_FLAG_DPLL and queue_work() under pf->dplls.lock.
2731 	 * ice_dpll_deinit() clears the flag under the same lock before
2732 	 * cancel_work_sync() and wq destruction, so a callback arriving
2733 	 * after teardown observes the cleared flag and bails out.
2734 	 */
2735 	mutex_lock(&pf->dplls.lock);
2736 	if (!test_bit(ICE_FLAG_DPLL, pf->flags)) {
2737 		ret = -ENODEV;
2738 		goto unlock;
2739 	}
2740 	if (state == DPLL_PIN_STATE_DISCONNECTED &&
2741 	    p->tx_ref_src != pf->ptp.port.tx_clk_req)
2742 		goto unlock;
2743 
2744 	new_clk = (state == DPLL_PIN_STATE_DISCONNECTED) ? ICE_REF_CLK_ENET :
2745 			p->tx_ref_src;
2746 	if (new_clk == pf->ptp.port.tx_clk_req)
2747 		goto unlock;
2748 
2749 	pf->ptp.port.tx_clk_req = new_clk;
2750 	pf->dplls.txclk_switch_requested = true;
2751 	queue_work(pf->dplls.wq, &pf->dplls.txclk_work);
2752 unlock:
2753 	mutex_unlock(&pf->dplls.lock);
2754 	return ret;
2755 }
2756 
2757 /**
2758  * ice_dpll_txclk_state_on_dpll_get - get a state of Tx clk reference pin
2759  * @pin: pointer to a pin
2760  * @pin_priv: private data pointer passed on pin registration
2761  * @dpll: registered dpll pointer
2762  * @dpll_priv: private data pointer passed on dpll registration
2763  * @state: on success holds pin state on parent pin
2764  * @extack: error reporting
2765  *
2766  * TXCLK DPLL pin state is derived and not stored explicitly.
2767  *
2768  * Only external TX reference clocks (SYNCE, EREF0) are modeled
2769  * as DPLL pins. The internal ENET (TXCO) clock has no pin and,
2770  * when selected, all TXCLK pins are reported DISCONNECTED.
2771  *
2772  * During a pending TXCLK switch, the requested pin may be
2773  * reported as CONNECTED before hardware verification.
2774  * Hardware acceptance and synchronization are reported
2775  * exclusively via TXC DPLL lock-status.
2776  *
2777  * Context: Acquires and releases pf->dplls.lock
2778  * Return:
2779  * * 0 - success
2780  * * negative - failure
2781  */
2782 static int
2783 ice_dpll_txclk_state_on_dpll_get(const struct dpll_pin *pin, void *pin_priv,
2784 				 const struct dpll_device *dpll,
2785 				 void *dpll_priv,
2786 				 enum dpll_pin_state *state,
2787 				 struct netlink_ext_ack *extack)
2788 {
2789 	struct ice_dpll_pin *p = pin_priv;
2790 	struct ice_pf *pf = p->pf;
2791 
2792 	if (ice_dpll_is_reset(pf, extack))
2793 		return -EBUSY;
2794 
2795 	mutex_lock(&pf->dplls.lock);
2796 	if (pf->ptp.port.tx_clk_req == p->tx_ref_src)
2797 		*state = DPLL_PIN_STATE_CONNECTED;
2798 	else
2799 		*state = DPLL_PIN_STATE_DISCONNECTED;
2800 	mutex_unlock(&pf->dplls.lock);
2801 
2802 	return 0;
2803 }
2804 
2805 static const struct dpll_pin_ops ice_dpll_rclk_ops = {
2806 	.state_on_pin_set = ice_dpll_rclk_state_on_pin_set,
2807 	.state_on_pin_get = ice_dpll_rclk_state_on_pin_get,
2808 	.direction_get = ice_dpll_input_direction,
2809 };
2810 
2811 static const struct dpll_pin_ops ice_dpll_txclk_ops = {
2812 	.state_on_dpll_set = ice_dpll_txclk_state_on_dpll_set,
2813 	.state_on_dpll_get = ice_dpll_txclk_state_on_dpll_get,
2814 	.direction_get = ice_dpll_input_direction,
2815 };
2816 
2817 static const struct dpll_pin_ops ice_dpll_pin_sma_ops = {
2818 	.state_on_dpll_set = ice_dpll_sma_pin_state_set,
2819 	.state_on_dpll_get = ice_dpll_sw_pin_state_get,
2820 	.direction_get = ice_dpll_pin_sw_direction_get,
2821 	.direction_set = ice_dpll_pin_sma_direction_set,
2822 	.prio_get = ice_dpll_sw_input_prio_get,
2823 	.prio_set = ice_dpll_sw_input_prio_set,
2824 	.frequency_get = ice_dpll_sw_pin_frequency_get,
2825 	.frequency_set = ice_dpll_sw_pin_frequency_set,
2826 	.phase_adjust_get = ice_dpll_sw_phase_adjust_get,
2827 	.phase_adjust_set = ice_dpll_sw_phase_adjust_set,
2828 	.phase_offset_get = ice_dpll_phase_offset_get,
2829 	.esync_set = ice_dpll_sw_esync_set,
2830 	.esync_get = ice_dpll_sw_esync_get,
2831 	.ref_sync_set = ice_dpll_sw_input_ref_sync_set,
2832 	.ref_sync_get = ice_dpll_sw_input_ref_sync_get,
2833 };
2834 
2835 static const struct dpll_pin_ops ice_dpll_pin_ufl_ops = {
2836 	.state_on_dpll_set = ice_dpll_ufl_pin_state_set,
2837 	.state_on_dpll_get = ice_dpll_sw_pin_state_get,
2838 	.direction_get = ice_dpll_pin_sw_direction_get,
2839 	.prio_get = ice_dpll_sw_input_prio_get,
2840 	.prio_set = ice_dpll_sw_input_prio_set,
2841 	.frequency_get = ice_dpll_sw_pin_frequency_get,
2842 	.frequency_set = ice_dpll_sw_pin_frequency_set,
2843 	.esync_set = ice_dpll_sw_esync_set,
2844 	.esync_get = ice_dpll_sw_esync_get,
2845 	.phase_adjust_get = ice_dpll_sw_phase_adjust_get,
2846 	.phase_adjust_set = ice_dpll_sw_phase_adjust_set,
2847 	.phase_offset_get = ice_dpll_phase_offset_get,
2848 };
2849 
2850 static const struct dpll_pin_ops ice_dpll_input_ops = {
2851 	.frequency_get = ice_dpll_input_frequency_get,
2852 	.frequency_set = ice_dpll_input_frequency_set,
2853 	.state_on_dpll_get = ice_dpll_input_state_get,
2854 	.state_on_dpll_set = ice_dpll_input_state_set,
2855 	.prio_get = ice_dpll_input_prio_get,
2856 	.prio_set = ice_dpll_input_prio_set,
2857 	.direction_get = ice_dpll_input_direction,
2858 	.phase_adjust_get = ice_dpll_pin_phase_adjust_get,
2859 	.phase_adjust_set = ice_dpll_input_phase_adjust_set,
2860 	.phase_offset_get = ice_dpll_phase_offset_get,
2861 	.esync_set = ice_dpll_input_esync_set,
2862 	.esync_get = ice_dpll_input_esync_get,
2863 	.ref_sync_set = ice_dpll_input_ref_sync_set,
2864 	.ref_sync_get = ice_dpll_input_ref_sync_get,
2865 };
2866 
2867 static const struct dpll_pin_ops ice_dpll_output_ops = {
2868 	.frequency_get = ice_dpll_output_frequency_get,
2869 	.frequency_set = ice_dpll_output_frequency_set,
2870 	.state_on_dpll_get = ice_dpll_output_state_get,
2871 	.state_on_dpll_set = ice_dpll_output_state_set,
2872 	.direction_get = ice_dpll_output_direction,
2873 	.phase_adjust_get = ice_dpll_pin_phase_adjust_get,
2874 	.phase_adjust_set = ice_dpll_output_phase_adjust_set,
2875 	.esync_set = ice_dpll_output_esync_set,
2876 	.esync_get = ice_dpll_output_esync_get,
2877 };
2878 
2879 static const struct dpll_device_ops ice_dpll_ops = {
2880 	.lock_status_get = ice_dpll_lock_status_get,
2881 	.mode_get = ice_dpll_mode_get,
2882 };
2883 
2884 static const struct dpll_device_ops ice_dpll_pom_ops = {
2885 	.lock_status_get = ice_dpll_lock_status_get,
2886 	.mode_get = ice_dpll_mode_get,
2887 	.phase_offset_monitor_set = ice_dpll_phase_offset_monitor_set,
2888 	.phase_offset_monitor_get = ice_dpll_phase_offset_monitor_get,
2889 };
2890 
2891 /**
2892  * ice_generate_clock_id - generates unique clock_id for registering dpll.
2893  * @pf: board private structure
2894  *
2895  * Generates unique (per board) clock_id for allocation and search of dpll
2896  * devices in Linux dpll subsystem.
2897  *
2898  * Return: generated clock id for the board
2899  */
2900 static u64 ice_generate_clock_id(struct ice_pf *pf)
2901 {
2902 	return pci_get_dsn(pf->pdev);
2903 }
2904 
2905 /**
2906  * ice_dpll_pin_ntf - notify pin change including any SW pin wrappers
2907  * @dplls: pointer to dplls struct
2908  * @pin: the dpll_pin that changed
2909  *
2910  * Send a change notification for @pin and for any registered SMA/U.FL pin
2911  * whose backing CGU input matches @pin.
2912  */
2913 static void ice_dpll_pin_ntf(struct ice_dplls *dplls, struct dpll_pin *pin)
2914 {
2915 	dpll_pin_change_ntf(pin);
2916 	for (int i = 0; i < ICE_DPLL_PIN_SW_NUM; i++) {
2917 		if (dplls->sma[i].pin && dplls->sma[i].input &&
2918 		    dplls->sma[i].input->pin == pin)
2919 			dpll_pin_change_ntf(dplls->sma[i].pin);
2920 		if (dplls->ufl[i].pin && dplls->ufl[i].input &&
2921 		    dplls->ufl[i].input->pin == pin)
2922 			dpll_pin_change_ntf(dplls->ufl[i].pin);
2923 	}
2924 }
2925 
2926 /**
2927  * ice_dpll_notify_changes - notify dpll subsystem about changes
2928  * @d: pointer do dpll
2929  *
2930  * Once change detected appropriate event is submitted to the dpll subsystem.
2931  */
2932 static void ice_dpll_notify_changes(struct ice_dpll *d)
2933 {
2934 	struct ice_dplls *dplls = &d->pf->dplls;
2935 	bool pin_notified = false;
2936 
2937 	if (d->prev_dpll_state != d->dpll_state) {
2938 		d->prev_dpll_state = d->dpll_state;
2939 		dpll_device_change_ntf(d->dpll);
2940 	}
2941 	if (d->prev_input != d->active_input) {
2942 		if (d->prev_input)
2943 			ice_dpll_pin_ntf(dplls, d->prev_input);
2944 		d->prev_input = d->active_input;
2945 		if (d->active_input) {
2946 			ice_dpll_pin_ntf(dplls, d->active_input);
2947 			pin_notified = true;
2948 		}
2949 	}
2950 	if (d->prev_phase_offset != d->phase_offset) {
2951 		d->prev_phase_offset = d->phase_offset;
2952 		if (!pin_notified && d->active_input)
2953 			ice_dpll_pin_ntf(dplls, d->active_input);
2954 	}
2955 }
2956 
2957 /**
2958  * ice_dpll_is_pps_phase_monitor - check if dpll capable of phase offset monitor
2959  * @pf: pf private structure
2960  *
2961  * Check if firmware is capable of supporting admin command to provide
2962  * phase offset monitoring on all the input pins on PPS dpll.
2963  *
2964  * Returns:
2965  * * true - PPS dpll phase offset monitoring is supported
2966  * * false - PPS dpll phase offset monitoring is not supported
2967  */
2968 static bool ice_dpll_is_pps_phase_monitor(struct ice_pf *pf)
2969 {
2970 	struct ice_cgu_input_measure meas[ICE_DPLL_INPUT_REF_NUM];
2971 	int ret = ice_aq_get_cgu_input_pin_measure(&pf->hw, DPLL_TYPE_PPS, meas,
2972 						   ARRAY_SIZE(meas));
2973 
2974 	if (ret && pf->hw.adminq.sq_last_status == LIBIE_AQ_RC_ESRCH)
2975 		return false;
2976 
2977 	return true;
2978 }
2979 
2980 /**
2981  * ice_dpll_pins_notify_mask - notify dpll subsystem about bulk pin changes
2982  * @dplls: pointer to dplls struct
2983  * @pins: array of ice_dpll_pin pointers registered within dpll subsystem
2984  * @pin_num: number of pins
2985  * @phase_offset_ntf_mask: bitmask of pin indexes to notify
2986  *
2987  * Iterate over array of pins and call dpll subsystem pin notify if
2988  * corresponding pin index within bitmask is set.
2989  *
2990  * Context: Must be called while pf->dplls.lock is released.
2991  */
2992 static void ice_dpll_pins_notify_mask(struct ice_dplls *dplls,
2993 				      struct ice_dpll_pin *pins,
2994 				      u8 pin_num,
2995 				      u32 phase_offset_ntf_mask)
2996 {
2997 	for (int i = 0; i < pin_num; i++)
2998 		if (phase_offset_ntf_mask & BIT(i))
2999 			ice_dpll_pin_ntf(dplls, pins[i].pin);
3000 }
3001 
3002 /**
3003  * ice_dpll_pps_update_phase_offsets - update phase offset measurements
3004  * @pf: pf private structure
3005  * @phase_offset_pins_updated: returns mask of updated input pin indexes
3006  *
3007  * Read phase offset measurements for PPS dpll device and store values in
3008  * input pins array. On success phase_offset_pins_updated - fills bitmask of
3009  * updated input pin indexes, pins shall be notified.
3010  *
3011  * Context: Shall be called with pf->dplls.lock being locked.
3012  * Returns:
3013  * * 0 - success or no data available
3014  * * negative - AQ failure
3015  */
3016 static int ice_dpll_pps_update_phase_offsets(struct ice_pf *pf,
3017 					     u32 *phase_offset_pins_updated)
3018 {
3019 	struct ice_cgu_input_measure meas[ICE_DPLL_INPUT_REF_NUM];
3020 	struct ice_dpll_pin *p;
3021 	s64 phase_offset, tmp;
3022 	int i, j, ret;
3023 
3024 	*phase_offset_pins_updated = 0;
3025 	ret = ice_aq_get_cgu_input_pin_measure(&pf->hw, DPLL_TYPE_PPS, meas,
3026 					       ARRAY_SIZE(meas));
3027 	if (ret && pf->hw.adminq.sq_last_status == LIBIE_AQ_RC_EAGAIN) {
3028 		return 0;
3029 	} else if (ret) {
3030 		dev_err(ice_pf_to_dev(pf),
3031 			"failed to get input pin measurements dpll=%d, ret=%d %s\n",
3032 			DPLL_TYPE_PPS, ret,
3033 			libie_aq_str(pf->hw.adminq.sq_last_status));
3034 		return ret;
3035 	}
3036 	for (i = 0; i < pf->dplls.num_inputs; i++) {
3037 		p = &pf->dplls.inputs[i];
3038 		phase_offset = 0;
3039 		for (j = 0; j < ICE_CGU_INPUT_PHASE_OFFSET_BYTES; j++) {
3040 			tmp = meas[i].phase_offset[j];
3041 #ifdef __LITTLE_ENDIAN
3042 			phase_offset += tmp << 8 * j;
3043 #else
3044 			phase_offset += tmp << 8 *
3045 				(ICE_CGU_INPUT_PHASE_OFFSET_BYTES - 1 - j);
3046 #endif
3047 		}
3048 		phase_offset = sign_extend64(phase_offset, 47);
3049 		if (p->phase_offset != phase_offset) {
3050 			dev_dbg(ice_pf_to_dev(pf),
3051 				"phase offset changed for pin:%d old:%llx, new:%llx\n",
3052 				p->idx, p->phase_offset, phase_offset);
3053 			p->phase_offset = phase_offset;
3054 			*phase_offset_pins_updated |= (1 << i);
3055 		}
3056 	}
3057 
3058 	return 0;
3059 }
3060 
3061 /**
3062  * ice_dpll_update_state - update dpll state
3063  * @pf: pf private structure
3064  * @d: pointer to queried dpll device
3065  * @init: if function called on initialization of ice dpll
3066  *
3067  * Poll current state of dpll from hw and update ice_dpll struct.
3068  *
3069  * Context: Called by kworker under pf->dplls.lock
3070  * Return:
3071  * * 0 - success
3072  * * negative - AQ failure
3073  */
3074 static int
3075 ice_dpll_update_state(struct ice_pf *pf, struct ice_dpll *d, bool init)
3076 {
3077 	struct ice_dpll_pin *p = NULL;
3078 	int ret;
3079 
3080 	ret = ice_get_cgu_state(&pf->hw, d->dpll_idx, d->prev_dpll_state,
3081 				&d->input_idx, &d->ref_state, &d->eec_mode,
3082 				&d->phase_offset, &d->dpll_state);
3083 
3084 	dev_dbg(ice_pf_to_dev(pf),
3085 		"update dpll=%d, prev_src_idx:%u, src_idx:%u, state:%d, prev:%d mode:%d\n",
3086 		d->dpll_idx, d->prev_input_idx, d->input_idx,
3087 		d->dpll_state, d->prev_dpll_state, d->mode);
3088 	if (ret) {
3089 		dev_err(ice_pf_to_dev(pf),
3090 			"update dpll=%d state failed, ret=%d %s\n",
3091 			d->dpll_idx, ret,
3092 			libie_aq_str(pf->hw.adminq.sq_last_status));
3093 		return ret;
3094 	}
3095 	if (init) {
3096 		if (d->dpll_state == DPLL_LOCK_STATUS_LOCKED ||
3097 		    d->dpll_state == DPLL_LOCK_STATUS_LOCKED_HO_ACQ)
3098 			d->active_input = pf->dplls.inputs[d->input_idx].pin;
3099 		p = &pf->dplls.inputs[d->input_idx];
3100 		return ice_dpll_pin_state_update(pf, p,
3101 						 ICE_DPLL_PIN_TYPE_INPUT, NULL);
3102 	}
3103 	if (d->dpll_state == DPLL_LOCK_STATUS_HOLDOVER ||
3104 	    d->dpll_state == DPLL_LOCK_STATUS_UNLOCKED) {
3105 		d->active_input = NULL;
3106 		if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID)
3107 			p = &pf->dplls.inputs[d->input_idx];
3108 		d->prev_input_idx = ICE_DPLL_PIN_IDX_INVALID;
3109 		d->input_idx = ICE_DPLL_PIN_IDX_INVALID;
3110 		if (!p)
3111 			return 0;
3112 		ret = ice_dpll_pin_state_update(pf, p,
3113 						ICE_DPLL_PIN_TYPE_INPUT, NULL);
3114 	} else if (d->input_idx != d->prev_input_idx) {
3115 		if (d->prev_input_idx != ICE_DPLL_PIN_IDX_INVALID) {
3116 			p = &pf->dplls.inputs[d->prev_input_idx];
3117 			ice_dpll_pin_state_update(pf, p,
3118 						  ICE_DPLL_PIN_TYPE_INPUT,
3119 						  NULL);
3120 		}
3121 		if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID) {
3122 			p = &pf->dplls.inputs[d->input_idx];
3123 			d->active_input = p->pin;
3124 			ice_dpll_pin_state_update(pf, p,
3125 						  ICE_DPLL_PIN_TYPE_INPUT,
3126 						  NULL);
3127 		}
3128 		d->prev_input_idx = d->input_idx;
3129 	}
3130 
3131 	return ret;
3132 }
3133 
3134 /**
3135  * ice_dpll_periodic_work - DPLLs periodic worker
3136  * @work: pointer to kthread_work structure
3137  *
3138  * DPLLs periodic worker is responsible for polling state of dpll.
3139  * Context: Holds pf->dplls.lock
3140  */
3141 static void ice_dpll_periodic_work(struct kthread_work *work)
3142 {
3143 	struct ice_dplls *d = container_of(work, struct ice_dplls, work.work);
3144 	struct ice_pf *pf = container_of(d, struct ice_pf, dplls);
3145 	struct ice_dpll *de = &pf->dplls.eec;
3146 	struct ice_dpll *dp = &pf->dplls.pps;
3147 	u32 phase_offset_ntf = 0;
3148 	int ret = 0;
3149 
3150 	if (ice_is_reset_in_progress(pf->state))
3151 		goto resched;
3152 	mutex_lock(&pf->dplls.lock);
3153 	d->periodic_counter++;
3154 	ret = ice_dpll_update_state(pf, de, false);
3155 	if (!ret)
3156 		ret = ice_dpll_update_state(pf, dp, false);
3157 	if (!ret && dp->phase_offset_monitor_period &&
3158 	    d->periodic_counter % dp->phase_offset_monitor_period == 0)
3159 		ret = ice_dpll_pps_update_phase_offsets(pf, &phase_offset_ntf);
3160 	if (ret) {
3161 		d->cgu_state_acq_err_num++;
3162 		/* stop rescheduling this worker */
3163 		if (d->cgu_state_acq_err_num >
3164 		    ICE_CGU_STATE_ACQ_ERR_THRESHOLD) {
3165 			dev_err(ice_pf_to_dev(pf),
3166 				"EEC/PPS DPLLs periodic work disabled\n");
3167 			mutex_unlock(&pf->dplls.lock);
3168 			return;
3169 		}
3170 	}
3171 	mutex_unlock(&pf->dplls.lock);
3172 	ice_dpll_notify_changes(de);
3173 	ice_dpll_notify_changes(dp);
3174 	if (phase_offset_ntf)
3175 		ice_dpll_pins_notify_mask(d, d->inputs, d->num_inputs,
3176 					  phase_offset_ntf);
3177 
3178 resched:
3179 	/* Run twice a second or reschedule if update failed */
3180 	kthread_queue_delayed_work(d->kworker, &d->work,
3181 				   ret ? msecs_to_jiffies(10) :
3182 				   msecs_to_jiffies(500));
3183 }
3184 
3185 /**
3186  * ice_dpll_init_ref_sync_inputs - initialize reference sync pin pairs
3187  * @pf: pf private structure
3188  *
3189  * Read DPLL TLV capabilities and initialize reference sync pin pairs in
3190  * dpll subsystem.
3191  *
3192  * Return:
3193  * * 0 - success or nothing to do (no ref-sync tlv are present)
3194  * * negative - AQ failure
3195  */
3196 static int ice_dpll_init_ref_sync_inputs(struct ice_pf *pf)
3197 {
3198 	struct ice_dpll_pin *inputs = pf->dplls.inputs;
3199 	struct ice_hw *hw = &pf->hw;
3200 	u16 addr, len, end, hdr;
3201 	int ret;
3202 
3203 	ret = ice_get_pfa_module_tlv(hw, &hdr, &len, ICE_SR_PFA_DPLL_DEFAULTS);
3204 	if (ret) {
3205 		dev_err(ice_pf_to_dev(pf),
3206 			"Failed to read PFA dpll defaults TLV ret=%d\n", ret);
3207 		return ret;
3208 	}
3209 	end = hdr + len;
3210 
3211 	for (addr = hdr + ICE_DPLL_PFA_HEADER_LEN; addr < end;
3212 	     addr += ICE_DPLL_PFA_ENTRY_LEN) {
3213 		unsigned long bit, ul_mask, offset;
3214 		u16 pin, mask, buf;
3215 		bool valid = false;
3216 
3217 		ret = ice_read_sr_word(hw, addr, &buf);
3218 		if (ret)
3219 			return ret;
3220 
3221 		switch (buf) {
3222 		case ICE_DPLL_PFA_REF_SYNC_TYPE:
3223 		case ICE_DPLL_PFA_REF_SYNC_TYPE2:
3224 		{
3225 			u16 mask_addr = addr + ICE_DPLL_PFA_MASK_OFFSET;
3226 			u16 val_addr = addr + ICE_DPLL_PFA_VALUE_OFFSET;
3227 
3228 			ret = ice_read_sr_word(hw, mask_addr, &mask);
3229 			if (ret)
3230 				return ret;
3231 			ret = ice_read_sr_word(hw, val_addr, &pin);
3232 			if (ret)
3233 				return ret;
3234 			if (buf == ICE_DPLL_PFA_REF_SYNC_TYPE)
3235 				pin >>= ICE_DPLL_PFA_MAILBOX_REF_SYNC_PIN_S;
3236 			valid = true;
3237 			break;
3238 		}
3239 		case ICE_DPLL_PFA_END:
3240 			addr = end;
3241 			break;
3242 		default:
3243 			continue;
3244 		}
3245 		if (!valid)
3246 			continue;
3247 
3248 		ul_mask = mask;
3249 		offset = 0;
3250 		for_each_set_bit(bit, &ul_mask, BITS_PER_TYPE(u16)) {
3251 			int i, j;
3252 
3253 			if (hw->device_id == ICE_DEV_ID_E810C_SFP &&
3254 			    pin > ICE_DPLL_E810C_SFP_NC_START)
3255 				offset = -ICE_DPLL_E810C_SFP_NC_PINS;
3256 			i = pin + offset;
3257 			j = bit + offset;
3258 			if (i < 0 || j < 0)
3259 				return -ERANGE;
3260 			inputs[i].ref_sync = j;
3261 		}
3262 	}
3263 
3264 	return 0;
3265 }
3266 
3267 /**
3268  * ice_dpll_release_pins - release pins resources from dpll subsystem
3269  * @pins: pointer to pins array
3270  * @count: number of pins
3271  *
3272  * Release resources of given pins array in the dpll subsystem.
3273  */
3274 static void ice_dpll_release_pins(struct ice_dpll_pin *pins, int count)
3275 {
3276 	int i;
3277 
3278 	for (i = 0; i < count; i++)
3279 		if (!IS_ERR_OR_NULL(pins[i].pin))
3280 			dpll_pin_put(pins[i].pin, &pins[i].tracker);
3281 }
3282 
3283 /**
3284  * ice_dpll_get_pins - get pins from dpll subsystem
3285  * @pf: board private structure
3286  * @pins: pointer to pins array
3287  * @start_idx: get starts from this pin idx value
3288  * @count: number of pins
3289  * @clock_id: clock_id of dpll device
3290  *
3291  * Get pins - allocate - in dpll subsystem, store them in pin field of given
3292  * pins array.
3293  *
3294  * Return:
3295  * * 0 - success
3296  * * negative - allocation failure reason
3297  */
3298 static int
3299 ice_dpll_get_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,
3300 		  int start_idx, int count, u64 clock_id)
3301 {
3302 	u32 pin_index;
3303 	int i, ret;
3304 
3305 	for (i = 0; i < count; i++) {
3306 		pin_index = start_idx;
3307 		if (start_idx != DPLL_PIN_IDX_UNSPEC)
3308 			pin_index += i;
3309 		pins[i].pin = dpll_pin_get(clock_id, pin_index, THIS_MODULE,
3310 					   &pins[i].prop, &pins[i].tracker);
3311 		if (IS_ERR(pins[i].pin)) {
3312 			ret = PTR_ERR(pins[i].pin);
3313 			goto release_pins;
3314 		}
3315 	}
3316 
3317 	return 0;
3318 
3319 release_pins:
3320 	while (--i >= 0)
3321 		dpll_pin_put(pins[i].pin, &pins[i].tracker);
3322 	return ret;
3323 }
3324 
3325 /**
3326  * ice_dpll_unregister_pins - unregister pins from a dpll
3327  * @dpll: dpll device pointer
3328  * @pins: pointer to pins array
3329  * @ops: callback ops registered with the pins
3330  * @count: number of pins
3331  *
3332  * Unregister pins of a given array of pins from given dpll device registered in
3333  * dpll subsystem.
3334  */
3335 static void
3336 ice_dpll_unregister_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
3337 			 const struct dpll_pin_ops *ops, int count)
3338 {
3339 	int i;
3340 
3341 	for (i = 0; i < count; i++) {
3342 		if (pins[i].hidden)
3343 			continue;
3344 		if (IS_ERR_OR_NULL(pins[i].pin))
3345 			continue;
3346 		dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
3347 	}
3348 }
3349 
3350 /**
3351  * ice_dpll_pin_ref_sync_register - register reference sync pins
3352  * @pins: pointer to pins array
3353  * @count: number of pins
3354  *
3355  * Register reference sync pins in dpll subsystem.
3356  *
3357  * Return:
3358  * * 0 - success
3359  * * negative - registration failure reason
3360  */
3361 static int
3362 ice_dpll_pin_ref_sync_register(struct ice_dpll_pin *pins, int count)
3363 {
3364 	int ret, i;
3365 
3366 	for (i = 0; i < count; i++) {
3367 		if (!pins[i].hidden && pins[i].ref_sync) {
3368 			int j = pins[i].ref_sync;
3369 
3370 			ret = dpll_pin_ref_sync_pair_add(pins[i].pin,
3371 							 pins[j].pin);
3372 			if (ret)
3373 				return ret;
3374 		}
3375 	}
3376 
3377 	return 0;
3378 }
3379 
3380 /**
3381  * ice_dpll_register_pins - register pins with a dpll
3382  * @dpll: dpll pointer to register pins with
3383  * @pins: pointer to pins array
3384  * @ops: callback ops registered with the pins
3385  * @count: number of pins
3386  *
3387  * Register pins of a given array with given dpll in dpll subsystem.
3388  *
3389  * Return:
3390  * * 0 - success
3391  * * negative - registration failure reason
3392  */
3393 static int
3394 ice_dpll_register_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
3395 		       const struct dpll_pin_ops *ops, int count)
3396 {
3397 	int ret, i;
3398 
3399 	for (i = 0; i < count; i++) {
3400 		if (!pins[i].hidden) {
3401 			ret = dpll_pin_register(dpll, pins[i].pin, ops, &pins[i]);
3402 			if (ret)
3403 				goto unregister_pins;
3404 		}
3405 	}
3406 
3407 	return 0;
3408 
3409 unregister_pins:
3410 	while (--i >= 0)
3411 		if (!pins[i].hidden)
3412 			dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
3413 	return ret;
3414 }
3415 
3416 /**
3417  * ice_dpll_deinit_direct_pins - deinitialize direct pins
3418  * @pf: board private structure
3419  * @cgu: if cgu is present and controlled by this NIC
3420  * @pins: pointer to pins array
3421  * @count: number of pins
3422  * @ops: callback ops registered with the pins
3423  * @first: dpll device pointer
3424  * @second: dpll device pointer
3425  *
3426  * If cgu is owned unregister pins from given dplls.
3427  * Release pins resources to the dpll subsystem.
3428  */
3429 static void
3430 ice_dpll_deinit_direct_pins(struct ice_pf *pf, bool cgu,
3431 			    struct ice_dpll_pin *pins, int count,
3432 			    const struct dpll_pin_ops *ops,
3433 			    struct dpll_device *first,
3434 			    struct dpll_device *second)
3435 {
3436 	if (cgu) {
3437 		ice_dpll_unregister_pins(first, pins, ops, count);
3438 		ice_dpll_unregister_pins(second, pins, ops, count);
3439 	}
3440 	ice_dpll_release_pins(pins, count);
3441 }
3442 
3443 /**
3444  * ice_dpll_init_direct_pins - initialize direct pins
3445  * @pf: board private structure
3446  * @cgu: if cgu is present and controlled by this NIC
3447  * @pins: pointer to pins array
3448  * @start_idx: on which index shall allocation start in dpll subsystem
3449  * @count: number of pins
3450  * @ops: callback ops registered with the pins
3451  * @first: dpll device pointer
3452  * @second: dpll device pointer
3453  *
3454  * Allocate directly connected pins of a given array in dpll subsystem.
3455  * If cgu is owned register allocated pins with given dplls.
3456  *
3457  * Return:
3458  * * 0 - success
3459  * * negative - registration failure reason
3460  */
3461 static int
3462 ice_dpll_init_direct_pins(struct ice_pf *pf, bool cgu,
3463 			  struct ice_dpll_pin *pins, int start_idx, int count,
3464 			  const struct dpll_pin_ops *ops,
3465 			  struct dpll_device *first, struct dpll_device *second)
3466 {
3467 	int ret;
3468 
3469 	ret = ice_dpll_get_pins(pf, pins, start_idx, count, pf->dplls.clock_id);
3470 	if (ret)
3471 		return ret;
3472 	if (cgu) {
3473 		ret = ice_dpll_register_pins(first, pins, ops, count);
3474 		if (ret)
3475 			goto release_pins;
3476 		ret = ice_dpll_register_pins(second, pins, ops, count);
3477 		if (ret)
3478 			goto unregister_first;
3479 	}
3480 
3481 	return 0;
3482 
3483 unregister_first:
3484 	ice_dpll_unregister_pins(first, pins, ops, count);
3485 release_pins:
3486 	ice_dpll_release_pins(pins, count);
3487 	return ret;
3488 }
3489 
3490 /**
3491  * ice_dpll_deinit_rclk_pin - release rclk pin resources
3492  * @pf: board private structure
3493  *
3494  * Deregister rclk pin from parent pins and release resources in dpll subsystem.
3495  */
3496 static void ice_dpll_deinit_rclk_pin(struct ice_pf *pf)
3497 {
3498 	struct ice_dpll_pin *rclk = &pf->dplls.rclk;
3499 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
3500 	struct ice_dpll_pin *parent;
3501 	int i;
3502 
3503 	for (i = 0; i < rclk->num_parents; i++) {
3504 		parent = &pf->dplls.inputs[rclk->parent_idx[i]];
3505 		if (IS_ERR_OR_NULL(parent->pin))
3506 			continue;
3507 		dpll_pin_on_pin_unregister(parent->pin, rclk->pin,
3508 					   &ice_dpll_rclk_ops, rclk);
3509 	}
3510 	if (WARN_ON_ONCE(!vsi || !vsi->netdev))
3511 		return;
3512 	dpll_netdev_pin_clear(vsi->netdev);
3513 	dpll_pin_put(rclk->pin, &rclk->tracker);
3514 }
3515 
3516 static bool ice_dpll_is_fwnode_pin(struct ice_dpll_pin *pin)
3517 {
3518 	return !IS_ERR_OR_NULL(pin->fwnode);
3519 }
3520 
3521 static bool ice_dpll_fwnode_eq(const struct fwnode_handle *a,
3522 			       const struct fwnode_handle *b)
3523 {
3524 	return a && a == b;
3525 }
3526 
3527 static void ice_dpll_pin_notify_work(struct work_struct *work)
3528 {
3529 	struct ice_dpll_pin_work *w = container_of(work,
3530 						   struct ice_dpll_pin_work,
3531 						   work);
3532 	struct ice_dpll_pin *pin, *parent = w->pin;
3533 	bool is_tx_synce_parent = false;
3534 	struct ice_pf *pf = parent->pf;
3535 	bool is_rclk_parent = false;
3536 	int ret;
3537 
3538 	wait_for_completion(&pf->dplls.dpll_init);
3539 	if (!test_bit(ICE_FLAG_DPLL, pf->flags))
3540 		goto out; /* DPLL initialization failed */
3541 
3542 	/* Decide which parent we are handling, defensively checking FWNs */
3543 	for (int i = 0; i < pf->dplls.rclk.num_parents; i++) {
3544 		if (ice_dpll_fwnode_eq(parent->fwnode,
3545 				       pf->dplls.inputs[i].fwnode)) {
3546 			is_rclk_parent = true;
3547 			break;
3548 		}
3549 	}
3550 
3551 	is_tx_synce_parent =
3552 		ice_dpll_fwnode_eq(parent->fwnode,
3553 				   pf->dplls.txclks[E825_EXT_SYNCE_PIN_IDX].fwnode);
3554 	if (!is_rclk_parent && !is_tx_synce_parent)
3555 		goto out;
3556 
3557 	switch (w->action) {
3558 	case DPLL_PIN_CREATED:
3559 		if (!IS_ERR_OR_NULL(parent->pin)) {
3560 			/* We have already our pin registered */
3561 			goto out;
3562 		}
3563 
3564 		/* Grab reference on fwnode pin */
3565 		parent->pin = fwnode_dpll_pin_find(parent->fwnode,
3566 						   &parent->tracker);
3567 		if (IS_ERR_OR_NULL(parent->pin)) {
3568 			dev_err(ice_pf_to_dev(pf),
3569 				"Cannot get fwnode pin reference\n");
3570 			goto out;
3571 		}
3572 
3573 		if (is_rclk_parent) {
3574 			/* Register rclk pin via on-pin relationship */
3575 			pin = &pf->dplls.rclk;
3576 			ret = dpll_pin_on_pin_register(parent->pin, pin->pin,
3577 						       &ice_dpll_rclk_ops, pin);
3578 			if (ret) {
3579 				dev_err(ice_pf_to_dev(pf),
3580 					"RCLK pin register failed: %pe\n",
3581 					ERR_PTR(ret));
3582 				goto drop_parent_ref;
3583 			}
3584 		} else if (is_tx_synce_parent) {
3585 			/* Register TX-CLK SYNCE pin directly to TXC DPLL */
3586 			pin = &pf->dplls.txclks[E825_EXT_SYNCE_PIN_IDX];
3587 			ret = dpll_pin_register(pf->dplls.txc.dpll, pin->pin,
3588 						&ice_dpll_txclk_ops, pin);
3589 			if (ret) {
3590 				dev_err(ice_pf_to_dev(pf),
3591 					"TX SYNCE pin register failed: %pe\n",
3592 					ERR_PTR(ret));
3593 				goto drop_parent_ref;
3594 			}
3595 		}
3596 		break;
3597 	case DPLL_PIN_DELETED:
3598 		if (IS_ERR_OR_NULL(parent->pin)) {
3599 			/* We have already our pin unregistered */
3600 			goto out;
3601 		}
3602 
3603 		if (is_rclk_parent) {
3604 			/* Unregister rclk pin */
3605 			pin = &pf->dplls.rclk;
3606 			dpll_pin_on_pin_unregister(parent->pin, pin->pin,
3607 						   &ice_dpll_rclk_ops, pin);
3608 		} else if (is_tx_synce_parent) {
3609 			/* Unregister TX-CLK SYNCE pin from TXC DPLL */
3610 			pin = &pf->dplls.txclks[E825_EXT_SYNCE_PIN_IDX];
3611 			dpll_pin_unregister(pf->dplls.txc.dpll, pin->pin,
3612 					    &ice_dpll_txclk_ops, pin);
3613 		}
3614 drop_parent_ref:
3615 		/* Drop fwnode pin reference */
3616 		dpll_pin_put(parent->pin, &parent->tracker);
3617 		parent->pin = NULL;
3618 		break;
3619 	default:
3620 		break;
3621 	}
3622 out:
3623 	kfree(w);
3624 }
3625 
3626 static int ice_dpll_pin_notify(struct notifier_block *nb, unsigned long action,
3627 			       void *data)
3628 {
3629 	struct ice_dpll_pin *pin = container_of(nb, struct ice_dpll_pin, nb);
3630 	struct dpll_pin_notifier_info *info = data;
3631 	struct ice_dpll_pin_work *work;
3632 
3633 	if (action != DPLL_PIN_CREATED && action != DPLL_PIN_DELETED)
3634 		return NOTIFY_DONE;
3635 
3636 	/* Check if the reported pin is this one */
3637 	if (pin->fwnode != info->fwnode)
3638 		return NOTIFY_DONE; /* Not this pin */
3639 
3640 	/* Ignore notification which are the outcome of internal pin
3641 	 * registration/unregistration calls - synce pin case.
3642 	 */
3643 	if (info->src_clock_id == pin->pf->dplls.clock_id)
3644 		return NOTIFY_DONE;
3645 
3646 	work = kzalloc_obj(*work);
3647 	if (!work)
3648 		return NOTIFY_DONE;
3649 
3650 	INIT_WORK(&work->work, ice_dpll_pin_notify_work);
3651 	work->action = action;
3652 	work->pin = pin;
3653 
3654 	queue_work(pin->pf->dplls.wq, &work->work);
3655 
3656 	return NOTIFY_OK;
3657 }
3658 
3659 /**
3660  * ice_dpll_init_pin_common - initialize pin
3661  * @pf: board private structure
3662  * @pin: pin to register
3663  * @start_idx: on which index shall allocation start in dpll subsystem
3664  * @ops: callback ops registered with the pins
3665  *
3666  * Allocate resource for given pin in dpll subsystem. Register the pin with
3667  * the parents it has in the info.
3668  *
3669  * Return:
3670  * * 0 - success
3671  * * negative - registration failure reason
3672  */
3673 static int
3674 ice_dpll_init_pin_common(struct ice_pf *pf, struct ice_dpll_pin *pin,
3675 			 int start_idx, const struct dpll_pin_ops *ops)
3676 {
3677 	struct ice_dpll_pin *parent;
3678 	int ret, i;
3679 
3680 	ret = ice_dpll_get_pins(pf, pin, start_idx, 1, pf->dplls.clock_id);
3681 	if (ret)
3682 		return ret;
3683 
3684 	for (i = 0; i < pin->num_parents; i++) {
3685 		parent = &pf->dplls.inputs[pin->parent_idx[i]];
3686 		if (IS_ERR_OR_NULL(parent->pin)) {
3687 			if (!ice_dpll_is_fwnode_pin(parent)) {
3688 				ret = -ENODEV;
3689 				goto unregister_pins;
3690 			}
3691 			parent->pin = fwnode_dpll_pin_find(parent->fwnode,
3692 							   &parent->tracker);
3693 			if (IS_ERR_OR_NULL(parent->pin)) {
3694 				dev_info(ice_pf_to_dev(pf),
3695 					 "Mux pin not registered yet\n");
3696 				continue;
3697 			}
3698 		}
3699 		ret = dpll_pin_on_pin_register(parent->pin, pin->pin, ops, pin);
3700 		if (ret)
3701 			goto unregister_pins;
3702 	}
3703 
3704 	return 0;
3705 
3706 unregister_pins:
3707 	while (i) {
3708 		parent = &pf->dplls.inputs[pin->parent_idx[--i]];
3709 		if (IS_ERR_OR_NULL(parent->pin))
3710 			continue;
3711 		dpll_pin_on_pin_unregister(parent->pin, pin->pin, ops, pin);
3712 	}
3713 	ice_dpll_release_pins(pin, 1);
3714 
3715 	return ret;
3716 }
3717 
3718 /**
3719  * ice_dpll_init_rclk_pin - initialize recovered clock pin
3720  * @pf: board private structure
3721  * @start_idx: on which index shall allocation start in dpll subsystem
3722  * @ops: callback ops registered with the pins
3723  *
3724  * Allocate resource for recovered clock pin in dpll subsystem. Register the
3725  * pin with the parents it has in the info.
3726  *
3727  * Return:
3728  * * 0 - success
3729  * * negative - registration failure reason
3730  */
3731 static int
3732 ice_dpll_init_rclk_pin(struct ice_pf *pf, int start_idx,
3733 		       const struct dpll_pin_ops *ops)
3734 {
3735 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
3736 	int ret;
3737 
3738 	ret = ice_dpll_init_pin_common(pf, &pf->dplls.rclk, start_idx, ops);
3739 	if (ret)
3740 		return ret;
3741 
3742 	dpll_netdev_pin_set(vsi->netdev, pf->dplls.rclk.pin);
3743 
3744 	return 0;
3745 }
3746 
3747 static void
3748 ice_dpll_stop_fwnode_pin_activity(struct ice_dpll_pin *pin, bool flush)
3749 {
3750 	unregister_dpll_notifier(&pin->nb);
3751 	if (flush)
3752 		flush_workqueue(pin->pf->dplls.wq);
3753 }
3754 
3755 static void
3756 ice_dpll_release_fwnode_pin(struct ice_dpll_pin *pin)
3757 {
3758 	if (!IS_ERR_OR_NULL(pin->pin)) {
3759 		dpll_pin_put(pin->pin, &pin->tracker);
3760 		pin->pin = NULL;
3761 	}
3762 	fwnode_handle_put(pin->fwnode);
3763 	pin->fwnode = NULL;
3764 }
3765 
3766 static void
3767 ice_dpll_deinit_fwnode_pin(struct ice_dpll_pin *pin)
3768 {
3769 	ice_dpll_stop_fwnode_pin_activity(pin, true);
3770 	ice_dpll_release_fwnode_pin(pin);
3771 }
3772 
3773 static void
3774 ice_dpll_deinit_fwnode_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,
3775 			    int start_idx)
3776 {
3777 	int i;
3778 
3779 	for (i = 0; i < pf->dplls.rclk.num_parents; i++)
3780 		ice_dpll_deinit_fwnode_pin(&pins[start_idx + i]);
3781 	destroy_workqueue(pf->dplls.wq);
3782 }
3783 
3784 static int ice_dpll_deinit_txclk_pins(struct ice_pf *pf)
3785 {
3786 	struct ice_dpll_pin *synce_pin = &pf->dplls.txclks[E825_EXT_SYNCE_PIN_IDX];
3787 	struct ice_dpll *dt = &pf->dplls.txc;
3788 
3789 	ice_dpll_stop_fwnode_pin_activity(synce_pin, true);
3790 	ice_dpll_unregister_pins(dt->dpll, pf->dplls.txclks,
3791 				 &ice_dpll_txclk_ops,
3792 				 ARRAY_SIZE(pf->dplls.txclks));
3793 	ice_dpll_release_pins(&pf->dplls.txclks[E825_EXT_EREF_PIN_IDX], 1);
3794 	/* ice_dpll_release_pins() puts the pin but does not clear the slot,
3795 	 * unlike ice_dpll_release_fwnode_pin() used for SYNCE below. NULL it
3796 	 * so a late ice_txclk_get_pin() returns NULL rather than a dangling
3797 	 * pointer.
3798 	 */
3799 	pf->dplls.txclks[E825_EXT_EREF_PIN_IDX].pin = NULL;
3800 	ice_dpll_release_fwnode_pin(synce_pin);
3801 	return 0;
3802 }
3803 
3804 /**
3805  * ice_dpll_deinit_pins - deinitialize direct pins
3806  * @pf: board private structure
3807  * @cgu: if cgu is controlled by this pf
3808  *
3809  * If cgu is owned unregister directly connected pins from the dplls.
3810  * Release resources of directly connected pins from the dpll subsystem.
3811  */
3812 static void ice_dpll_deinit_pins(struct ice_pf *pf, bool cgu)
3813 {
3814 	struct ice_dpll_pin *outputs = pf->dplls.outputs;
3815 	struct ice_dpll_pin *inputs = pf->dplls.inputs;
3816 	int num_outputs = pf->dplls.num_outputs;
3817 	int num_inputs = pf->dplls.num_inputs;
3818 	struct ice_dplls *d = &pf->dplls;
3819 	struct ice_dpll *de = &d->eec;
3820 	struct ice_dpll *dp = &d->pps;
3821 
3822 	ice_dpll_deinit_rclk_pin(pf);
3823 	if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825) {
3824 		ice_dpll_deinit_txclk_pins(pf);
3825 		ice_dpll_deinit_fwnode_pins(pf, pf->dplls.inputs, 0);
3826 	}
3827 	if (cgu) {
3828 		ice_dpll_unregister_pins(dp->dpll, inputs, &ice_dpll_input_ops,
3829 					 num_inputs);
3830 		ice_dpll_unregister_pins(de->dpll, inputs, &ice_dpll_input_ops,
3831 					 num_inputs);
3832 	}
3833 	ice_dpll_release_pins(inputs, num_inputs);
3834 	if (cgu) {
3835 		ice_dpll_unregister_pins(dp->dpll, outputs,
3836 					 &ice_dpll_output_ops, num_outputs);
3837 		ice_dpll_unregister_pins(de->dpll, outputs,
3838 					 &ice_dpll_output_ops, num_outputs);
3839 		ice_dpll_release_pins(outputs, num_outputs);
3840 		if (!pf->dplls.generic) {
3841 			ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.ufl,
3842 						    ICE_DPLL_PIN_SW_NUM,
3843 						    &ice_dpll_pin_ufl_ops,
3844 						    pf->dplls.pps.dpll,
3845 						    pf->dplls.eec.dpll);
3846 			ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.sma,
3847 						    ICE_DPLL_PIN_SW_NUM,
3848 						    &ice_dpll_pin_sma_ops,
3849 						    pf->dplls.pps.dpll,
3850 						    pf->dplls.eec.dpll);
3851 		}
3852 	}
3853 }
3854 
3855 static struct fwnode_handle *
3856 ice_dpll_pin_node_get(struct ice_pf *pf, const char *name)
3857 {
3858 	struct fwnode_handle *fwnode = dev_fwnode(ice_pf_to_dev(pf));
3859 	int index;
3860 
3861 	index = fwnode_property_match_string(fwnode, "dpll-pin-names", name);
3862 	if (index < 0)
3863 		return ERR_PTR(-ENOENT);
3864 
3865 	return fwnode_find_reference(fwnode, "dpll-pins", index);
3866 }
3867 
3868 static int
3869 ice_dpll_init_fwnode_pin(struct ice_dpll_pin *pin, const char *name)
3870 {
3871 	struct ice_pf *pf = pin->pf;
3872 	int ret;
3873 
3874 	pin->fwnode = ice_dpll_pin_node_get(pf, name);
3875 	if (IS_ERR(pin->fwnode)) {
3876 		dev_err(ice_pf_to_dev(pf),
3877 			"Failed to find %s firmware node: %pe\n", name,
3878 			pin->fwnode);
3879 		pin->fwnode = NULL;
3880 		return -ENODEV;
3881 	}
3882 
3883 	dev_dbg(ice_pf_to_dev(pf), "Found fwnode node for %s\n", name);
3884 
3885 	pin->pin = fwnode_dpll_pin_find(pin->fwnode, &pin->tracker);
3886 	if (IS_ERR_OR_NULL(pin->pin)) {
3887 		dev_info(ice_pf_to_dev(pf),
3888 			 "DPLL pin for %pfwp not registered yet\n",
3889 			 pin->fwnode);
3890 		pin->pin = NULL;
3891 	}
3892 
3893 	pin->nb.notifier_call = ice_dpll_pin_notify;
3894 	ret = register_dpll_notifier(&pin->nb);
3895 	if (ret) {
3896 		dev_err(ice_pf_to_dev(pf),
3897 			"Failed to subscribe for DPLL notifications\n");
3898 
3899 		if (!IS_ERR_OR_NULL(pin->pin)) {
3900 			dpll_pin_put(pin->pin, &pin->tracker);
3901 			pin->pin = NULL;
3902 		}
3903 		fwnode_handle_put(pin->fwnode);
3904 		pin->fwnode = NULL;
3905 
3906 		return ret;
3907 	}
3908 
3909 	return ret;
3910 }
3911 
3912 /**
3913  * ice_dpll_init_fwnode_pins - initialize pins from device tree
3914  * @pf: board private structure
3915  * @pins: pointer to pins array
3916  * @start_idx: starting index for pins
3917  * @count: number of pins to initialize
3918  *
3919  * Initialize input pins for E825 RCLK support. The parent pins (rclk0, rclk1)
3920  * are expected to be defined by the system firmware (ACPI). This function
3921  * allocates them in the dpll subsystem and stores their indices for later
3922  * registration with the rclk pin.
3923  *
3924  * Return:
3925  * * 0 - success
3926  * * negative - initialization failure reason
3927  */
3928 static int
3929 ice_dpll_init_fwnode_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,
3930 			  int start_idx)
3931 {
3932 	char pin_name[16];
3933 	int i, ret;
3934 
3935 	pf->dplls.wq = create_singlethread_workqueue("ice_dpll_wq");
3936 	if (!pf->dplls.wq)
3937 		return -ENOMEM;
3938 
3939 	for (i = 0; i < pf->dplls.rclk.num_parents; i++) {
3940 		pins[start_idx + i].pf = pf;
3941 		snprintf(pin_name, sizeof(pin_name), "rclk%u", i);
3942 		ret = ice_dpll_init_fwnode_pin(&pins[start_idx + i], pin_name);
3943 		if (ret)
3944 			goto error;
3945 	}
3946 
3947 	return 0;
3948 error:
3949 	/*
3950 	 * A notifier worker may already be queued and blocked on dpll_init;
3951 	 * release it so the per-pin flush below does not deadlock.
3952 	 */
3953 	complete_all(&pf->dplls.dpll_init);
3954 	while (i--)
3955 		ice_dpll_deinit_fwnode_pin(&pins[start_idx + i]);
3956 
3957 	destroy_workqueue(pf->dplls.wq);
3958 
3959 	return ret;
3960 }
3961 
3962 static int ice_dpll_init_txclk_pins(struct ice_pf *pf, int start_idx)
3963 {
3964 	struct ice_dpll_pin *ref_pin = pf->dplls.txclks;
3965 	struct ice_dpll *txc = &pf->dplls.txc;
3966 	int ret;
3967 
3968 	/* Configure EXT_EREF0 pin */
3969 	ret = ice_dpll_get_pins(pf, ref_pin, start_idx, 1, pf->dplls.clock_id);
3970 	if (ret)
3971 		return ret;
3972 	ret = dpll_pin_register(txc->dpll, ref_pin->pin, &ice_dpll_txclk_ops,
3973 				ref_pin);
3974 	if (ret)
3975 		goto err_release_ext_eref;
3976 
3977 	/*
3978 	 * Configure EXT_SYNCE pin (fwnode-backed).
3979 	 * The pin may not yet be available; in that case registration
3980 	 * will be deferred via the notifier path.
3981 	 */
3982 	ref_pin++;
3983 	ret = ice_dpll_init_fwnode_pin(ref_pin, ice_dpll_fwnode_ext_synce);
3984 	if (ret)
3985 		goto err_unregister_ext_eref;
3986 
3987 	if (IS_ERR_OR_NULL(ref_pin->pin)) {
3988 		dev_dbg(ice_pf_to_dev(pf),
3989 			"Tx-clk SYNCE pin not registered yet\n");
3990 		return 0;
3991 	}
3992 
3993 	ret = dpll_pin_register(txc->dpll, ref_pin->pin, &ice_dpll_txclk_ops,
3994 				ref_pin);
3995 	if (ret)
3996 		goto err_deinit_synce;
3997 
3998 	return 0;
3999 
4000 err_deinit_synce:
4001 	/*
4002 	 * Avoid deadlock against notifier workers blocked on dpll_init.
4003 	 * The outer init error path will complete dpll_init and flush the
4004 	 * shared workqueue before destroying it.
4005 	 */
4006 	ice_dpll_stop_fwnode_pin_activity(ref_pin, false);
4007 	ice_dpll_release_fwnode_pin(ref_pin);
4008 err_unregister_ext_eref:
4009 	dpll_pin_unregister(txc->dpll,
4010 			    pf->dplls.txclks[E825_EXT_EREF_PIN_IDX].pin,
4011 			    &ice_dpll_txclk_ops,
4012 			    &pf->dplls.txclks[E825_EXT_EREF_PIN_IDX]);
4013 
4014 err_release_ext_eref:
4015 	ice_dpll_release_pins(&pf->dplls.txclks[E825_EXT_EREF_PIN_IDX], 1);
4016 
4017 	return ret;
4018 }
4019 
4020 /**
4021  * ice_dpll_init_pins_e825 - init pins and register pins with a dplls
4022  * @pf: board private structure
4023  * @cgu: if cgu is present and controlled by this NIC
4024  *
4025  * Initialize directly connected pf's pins within pf's dplls in a Linux dpll
4026  * subsystem.
4027  *
4028  * Return:
4029  * * 0 - success
4030  * * negative - initialization failure reason
4031  */
4032 static int ice_dpll_init_pins_e825(struct ice_pf *pf)
4033 {
4034 	int ret;
4035 
4036 	ret = ice_dpll_init_fwnode_pins(pf, pf->dplls.inputs, 0);
4037 	if (ret)
4038 		return ret;
4039 
4040 	ret = ice_dpll_init_rclk_pin(pf, DPLL_PIN_IDX_UNSPEC,
4041 				     &ice_dpll_rclk_ops);
4042 
4043 	if (ret)
4044 		goto unregister_pins;
4045 
4046 	ret = ice_dpll_init_txclk_pins(pf, 0);
4047 	if (ret)
4048 		ice_dpll_deinit_rclk_pin(pf);
4049 
4050 unregister_pins:
4051 	if (ret) {
4052 		/* Inform DPLL notifier works that DPLL init was finished
4053 		 * unsuccessfully (ICE_DPLL_FLAG not set).
4054 		 */
4055 		complete_all(&pf->dplls.dpll_init);
4056 		ice_dpll_deinit_fwnode_pins(pf, pf->dplls.inputs, 0);
4057 	}
4058 
4059 	return ret;
4060 }
4061 
4062 /**
4063  * ice_dpll_init_pins - init pins and register pins with a dplls
4064  * @pf: board private structure
4065  * @cgu: if cgu is present and controlled by this NIC
4066  *
4067  * Initialize directly connected pf's pins within pf's dplls in a Linux dpll
4068  * subsystem.
4069  *
4070  * Return:
4071  * * 0 - success
4072  * * negative - initialization failure reason
4073  */
4074 static int ice_dpll_init_pins(struct ice_pf *pf, bool cgu)
4075 {
4076 	const struct dpll_pin_ops *output_ops;
4077 	const struct dpll_pin_ops *input_ops;
4078 	int ret, count;
4079 
4080 	input_ops = &ice_dpll_input_ops;
4081 	output_ops = &ice_dpll_output_ops;
4082 
4083 	ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.inputs, 0,
4084 					pf->dplls.num_inputs, input_ops,
4085 					pf->dplls.eec.dpll,
4086 					pf->dplls.pps.dpll);
4087 	if (ret)
4088 		return ret;
4089 	count = pf->dplls.num_inputs;
4090 	if (cgu) {
4091 		ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.outputs,
4092 						count, pf->dplls.num_outputs,
4093 						output_ops, pf->dplls.eec.dpll,
4094 						pf->dplls.pps.dpll);
4095 		if (ret)
4096 			goto deinit_inputs;
4097 		count += pf->dplls.num_outputs;
4098 		if (!pf->dplls.generic) {
4099 			ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.sma,
4100 							count,
4101 							ICE_DPLL_PIN_SW_NUM,
4102 							&ice_dpll_pin_sma_ops,
4103 							pf->dplls.eec.dpll,
4104 							pf->dplls.pps.dpll);
4105 			if (ret)
4106 				goto deinit_outputs;
4107 			count += ICE_DPLL_PIN_SW_NUM;
4108 			ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.ufl,
4109 							count,
4110 							ICE_DPLL_PIN_SW_NUM,
4111 							&ice_dpll_pin_ufl_ops,
4112 							pf->dplls.eec.dpll,
4113 							pf->dplls.pps.dpll);
4114 			if (ret)
4115 				goto deinit_sma;
4116 			count += ICE_DPLL_PIN_SW_NUM;
4117 		}
4118 		ret = ice_dpll_pin_ref_sync_register(pf->dplls.inputs,
4119 						     pf->dplls.num_inputs);
4120 		if (ret)
4121 			goto deinit_ufl;
4122 		ret = ice_dpll_pin_ref_sync_register(pf->dplls.sma,
4123 						     ICE_DPLL_PIN_SW_NUM);
4124 		if (ret)
4125 			goto deinit_ufl;
4126 	} else {
4127 		count += pf->dplls.num_outputs + 2 * ICE_DPLL_PIN_SW_NUM;
4128 	}
4129 
4130 	ret = ice_dpll_init_rclk_pin(pf, count + pf->ptp.port.port_num,
4131 				     &ice_dpll_rclk_ops);
4132 	if (ret)
4133 		goto deinit_ufl;
4134 
4135 	return 0;
4136 deinit_ufl:
4137 	ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.ufl, ICE_DPLL_PIN_SW_NUM,
4138 				    &ice_dpll_pin_ufl_ops, pf->dplls.pps.dpll,
4139 				    pf->dplls.eec.dpll);
4140 deinit_sma:
4141 	ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.sma, ICE_DPLL_PIN_SW_NUM,
4142 				    &ice_dpll_pin_sma_ops, pf->dplls.pps.dpll,
4143 				    pf->dplls.eec.dpll);
4144 deinit_outputs:
4145 	ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.outputs,
4146 				    pf->dplls.num_outputs,
4147 				    output_ops, pf->dplls.pps.dpll,
4148 				    pf->dplls.eec.dpll);
4149 deinit_inputs:
4150 	ice_dpll_deinit_direct_pins(pf, cgu, pf->dplls.inputs,
4151 				    pf->dplls.num_inputs,
4152 				    input_ops, pf->dplls.pps.dpll,
4153 				    pf->dplls.eec.dpll);
4154 	return ret;
4155 }
4156 
4157 /**
4158  * ice_dpll_deinit_dpll - deinitialize dpll device
4159  * @pf: board private structure
4160  * @d: pointer to ice_dpll
4161  * @cgu: if cgu is present and controlled by this NIC
4162  *
4163  * If cgu is owned, unregister the DPLL from DPLL subsystem.
4164  * Release resources of DPLL device from DPLL subsystem.
4165  */
4166 static void
4167 ice_dpll_deinit_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu)
4168 {
4169 	if (cgu || pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825)
4170 		dpll_device_unregister(d->dpll, d->ops, d);
4171 	dpll_device_put(d->dpll, &d->tracker);
4172 }
4173 
4174 /**
4175  * ice_dpll_init_dpll - initialize dpll device in dpll subsystem
4176  * @pf: board private structure
4177  * @d: dpll to be initialized
4178  * @cgu: if cgu is present and controlled by this NIC
4179  * @type: type of dpll being initialized
4180  *
4181  * Allocate DPLL instance for this board in dpll subsystem, if cgu is controlled
4182  * by this NIC, register DPLL with the callback ops.
4183  *
4184  * Return:
4185  * * 0 - success
4186  * * negative - initialization failure reason
4187  */
4188 static int
4189 ice_dpll_init_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu,
4190 		   enum dpll_type type)
4191 {
4192 	u64 clock_id = pf->dplls.clock_id;
4193 	int ret;
4194 
4195 	d->dpll = dpll_device_get(clock_id, d->dpll_idx, THIS_MODULE,
4196 				  &d->tracker);
4197 	if (IS_ERR(d->dpll)) {
4198 		ret = PTR_ERR(d->dpll);
4199 		dev_err(ice_pf_to_dev(pf),
4200 			"dpll_device_get failed (%p) err=%d\n", d, ret);
4201 		return ret;
4202 	}
4203 	d->pf = pf;
4204 	if (cgu || pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825) {
4205 		const struct dpll_device_ops *ops = &ice_dpll_ops;
4206 
4207 		if (type == DPLL_TYPE_PPS && ice_dpll_is_pps_phase_monitor(pf))
4208 			ops =  &ice_dpll_pom_ops;
4209 		if (cgu)
4210 			ice_dpll_update_state(pf, d, true);
4211 		ret = dpll_device_register(d->dpll, type, ops, d);
4212 		if (ret) {
4213 			dpll_device_put(d->dpll, &d->tracker);
4214 			d->dpll = NULL;
4215 			return ret;
4216 		}
4217 		d->ops = ops;
4218 	}
4219 
4220 	return 0;
4221 }
4222 
4223 /**
4224  * ice_dpll_deinit_worker - deinitialize dpll kworker
4225  * @pf: board private structure
4226  *
4227  * Stop dpll's kworker, release it's resources.
4228  */
4229 static void ice_dpll_deinit_worker(struct ice_pf *pf)
4230 {
4231 	struct ice_dplls *d = &pf->dplls;
4232 
4233 	kthread_cancel_delayed_work_sync(&d->work);
4234 	kthread_destroy_worker(d->kworker);
4235 }
4236 
4237 /**
4238  * ice_dpll_init_worker - Initialize DPLLs periodic worker
4239  * @pf: board private structure
4240  *
4241  * Create and start DPLLs periodic worker.
4242  *
4243  * Context: Shall be called after pf->dplls.lock is initialized.
4244  * Return:
4245  * * 0 - success
4246  * * negative - create worker failure
4247  */
4248 static int ice_dpll_init_worker(struct ice_pf *pf)
4249 {
4250 	struct ice_dplls *d = &pf->dplls;
4251 	struct kthread_worker *kworker;
4252 
4253 	kthread_init_delayed_work(&d->work, ice_dpll_periodic_work);
4254 	kworker = kthread_run_worker(0, "ice-dplls-%s",
4255 					dev_name(ice_pf_to_dev(pf)));
4256 	if (IS_ERR(kworker))
4257 		return PTR_ERR(kworker);
4258 	d->kworker = kworker;
4259 	d->cgu_state_acq_err_num = 0;
4260 	kthread_queue_delayed_work(d->kworker, &d->work, 0);
4261 
4262 	return 0;
4263 }
4264 
4265 /**
4266  * ice_dpll_phase_range_set - initialize phase adjust range helper
4267  * @range: pointer to phase adjust range struct to be initialized
4268  * @phase_adj: a value to be used as min(-)/max(+) boundary
4269  */
4270 static void ice_dpll_phase_range_set(struct dpll_pin_phase_adjust_range *range,
4271 				     u32 phase_adj)
4272 {
4273 	range->min = -phase_adj;
4274 	range->max = phase_adj;
4275 }
4276 
4277 /**
4278  * ice_dpll_init_info_pins_generic - initializes generic pins info
4279  * @pf: board private structure
4280  * @input: if input pins initialized
4281  *
4282  * Init information for generic pins, cache them in PF's pins structures.
4283  *
4284  * Return:
4285  * * 0 - success
4286  * * negative - init failure reason
4287  */
4288 static int ice_dpll_init_info_pins_generic(struct ice_pf *pf, bool input)
4289 {
4290 	struct ice_dpll *de = &pf->dplls.eec, *dp = &pf->dplls.pps;
4291 	static const char labels[][sizeof("99")] = {
4292 		"0", "1", "2", "3", "4", "5", "6", "7", "8",
4293 		"9", "10", "11", "12", "13", "14", "15" };
4294 	u32 cap = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
4295 	enum ice_dpll_pin_type pin_type;
4296 	int i, pin_num, ret = -EINVAL;
4297 	struct ice_dpll_pin *pins;
4298 	u32 phase_adj_max;
4299 
4300 	if (input) {
4301 		pin_num = pf->dplls.num_inputs;
4302 		pins = pf->dplls.inputs;
4303 		phase_adj_max = pf->dplls.input_phase_adj_max;
4304 		pin_type = ICE_DPLL_PIN_TYPE_INPUT;
4305 		cap |= DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE;
4306 	} else {
4307 		pin_num = pf->dplls.num_outputs;
4308 		pins = pf->dplls.outputs;
4309 		phase_adj_max = pf->dplls.output_phase_adj_max;
4310 		pin_type = ICE_DPLL_PIN_TYPE_OUTPUT;
4311 	}
4312 	if (pin_num > ARRAY_SIZE(labels))
4313 		return ret;
4314 
4315 	for (i = 0; i < pin_num; i++) {
4316 		pins[i].idx = i;
4317 		pins[i].prop.board_label = labels[i];
4318 		ice_dpll_phase_range_set(&pins[i].prop.phase_range,
4319 					 phase_adj_max);
4320 		pins[i].prop.capabilities = cap;
4321 		pins[i].pf = pf;
4322 		ret = ice_dpll_pin_state_update(pf, &pins[i], pin_type, NULL);
4323 		if (ret)
4324 			break;
4325 		if (input && pins[i].freq == ICE_DPLL_PIN_GEN_RCLK_FREQ)
4326 			pins[i].prop.type = DPLL_PIN_TYPE_MUX;
4327 		else
4328 			pins[i].prop.type = DPLL_PIN_TYPE_EXT;
4329 		if (!input)
4330 			continue;
4331 		ret = ice_aq_get_cgu_ref_prio(&pf->hw, de->dpll_idx, i,
4332 					      &de->input_prio[i]);
4333 		if (ret)
4334 			break;
4335 		ret = ice_aq_get_cgu_ref_prio(&pf->hw, dp->dpll_idx, i,
4336 					      &dp->input_prio[i]);
4337 		if (ret)
4338 			break;
4339 	}
4340 
4341 	return ret;
4342 }
4343 
4344 /**
4345  * ice_dpll_init_info_direct_pins - initializes direct pins info
4346  * @pf: board private structure
4347  * @pin_type: type of pins being initialized
4348  *
4349  * Init information for directly connected pins, cache them in pf's pins
4350  * structures.
4351  *
4352  * Return:
4353  * * 0 - success
4354  * * negative - init failure reason
4355  */
4356 static int
4357 ice_dpll_init_info_direct_pins(struct ice_pf *pf,
4358 			       enum ice_dpll_pin_type pin_type)
4359 {
4360 	struct ice_dpll *de = &pf->dplls.eec, *dp = &pf->dplls.pps;
4361 	int num_pins, i, ret = -EINVAL;
4362 	struct ice_hw *hw = &pf->hw;
4363 	struct ice_dpll_pin *pins;
4364 	unsigned long caps;
4365 	u32 phase_adj_max;
4366 	u8 freq_supp_num;
4367 	bool input;
4368 
4369 	switch (pin_type) {
4370 	case ICE_DPLL_PIN_TYPE_INPUT:
4371 		pins = pf->dplls.inputs;
4372 		num_pins = pf->dplls.num_inputs;
4373 		phase_adj_max = pf->dplls.input_phase_adj_max;
4374 		input = true;
4375 		break;
4376 	case ICE_DPLL_PIN_TYPE_OUTPUT:
4377 		pins = pf->dplls.outputs;
4378 		num_pins = pf->dplls.num_outputs;
4379 		phase_adj_max = pf->dplls.output_phase_adj_max;
4380 		input = false;
4381 		break;
4382 	default:
4383 		return -EINVAL;
4384 	}
4385 	if (num_pins != ice_cgu_get_num_pins(hw, input)) {
4386 		pf->dplls.generic = true;
4387 		return ice_dpll_init_info_pins_generic(pf, input);
4388 	}
4389 
4390 	for (i = 0; i < num_pins; i++) {
4391 		caps = 0;
4392 		pins[i].idx = i;
4393 		pins[i].prop.board_label = ice_cgu_get_pin_name(hw, i, input);
4394 		pins[i].prop.type = ice_cgu_get_pin_type(hw, i, input);
4395 		if (input) {
4396 			ret = ice_aq_get_cgu_ref_prio(hw, de->dpll_idx, i,
4397 						      &de->input_prio[i]);
4398 			if (ret)
4399 				return ret;
4400 			ret = ice_aq_get_cgu_ref_prio(hw, dp->dpll_idx, i,
4401 						      &dp->input_prio[i]);
4402 			if (ret)
4403 				return ret;
4404 			caps |= (DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
4405 				 DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE);
4406 			if (ice_dpll_is_sw_pin(pf, i, true))
4407 				pins[i].hidden = true;
4408 		} else {
4409 			ret = ice_cgu_get_output_pin_state_caps(hw, i, &caps);
4410 			if (ret)
4411 				return ret;
4412 			if (ice_dpll_is_sw_pin(pf, i, false))
4413 				pins[i].hidden = true;
4414 		}
4415 		ice_dpll_phase_range_set(&pins[i].prop.phase_range,
4416 					 phase_adj_max);
4417 		pins[i].prop.capabilities = caps;
4418 		ret = ice_dpll_pin_state_update(pf, &pins[i], pin_type, NULL);
4419 		if (ret)
4420 			return ret;
4421 		pins[i].prop.freq_supported =
4422 			ice_cgu_get_pin_freq_supp(hw, i, input, &freq_supp_num);
4423 		pins[i].prop.freq_supported_num = freq_supp_num;
4424 		pins[i].pf = pf;
4425 	}
4426 	if (input)
4427 		ret = ice_dpll_init_ref_sync_inputs(pf);
4428 
4429 	return ret;
4430 }
4431 
4432 /**
4433  * ice_dpll_init_info_pin_on_pin_e825c - initializes rclk pin information
4434  * @pf: board private structure
4435  *
4436  * Init information for rclk pin, cache them in pf->dplls.rclk.
4437  *
4438  * Return:
4439  * * 0 - success
4440  */
4441 static int ice_dpll_init_info_pin_on_pin_e825c(struct ice_pf *pf)
4442 {
4443 	struct ice_dpll_pin *rclk_pin = &pf->dplls.rclk;
4444 
4445 	rclk_pin->prop.type = DPLL_PIN_TYPE_SYNCE_ETH_PORT;
4446 	rclk_pin->prop.capabilities |= DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
4447 	rclk_pin->pf = pf;
4448 
4449 	return 0;
4450 }
4451 
4452 /**
4453  * ice_dpll_init_info_rclk_pin - initializes rclk pin information
4454  * @pf: board private structure
4455  *
4456  * Init information for rclk pin, cache them in pf->dplls.rclk.
4457  *
4458  * Return:
4459  * * 0 - success
4460  * * negative - init failure reason
4461  */
4462 static int ice_dpll_init_info_rclk_pin(struct ice_pf *pf)
4463 {
4464 	struct ice_dpll_pin *pin = &pf->dplls.rclk;
4465 
4466 	pin->prop.type = DPLL_PIN_TYPE_SYNCE_ETH_PORT;
4467 	pin->prop.capabilities |= DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
4468 	pin->pf = pf;
4469 
4470 	return ice_dpll_pin_state_update(pf, pin,
4471 					 ICE_DPLL_PIN_TYPE_RCLK_INPUT, NULL);
4472 }
4473 
4474 /**
4475  * ice_dpll_init_info_sw_pins - initializes software controlled pin information
4476  * @pf: board private structure
4477  *
4478  * Init information for software controlled pins, cache them in
4479  * pf->dplls.sma and pf->dplls.ufl.
4480  *
4481  * Return:
4482  * * 0 - success
4483  * * negative - init failure reason
4484  */
4485 static int ice_dpll_init_info_sw_pins(struct ice_pf *pf)
4486 {
4487 	u8 freq_supp_num, pin_abs_idx, input_idx_offset = 0;
4488 	struct ice_dplls *d = &pf->dplls;
4489 	struct ice_dpll_pin *pin;
4490 	u32 phase_adj_max, caps;
4491 	int i, ret;
4492 	u8 data;
4493 
4494 	if (pf->hw.device_id == ICE_DEV_ID_E810C_QSFP)
4495 		input_idx_offset = ICE_E810_RCLK_PINS_NUM;
4496 	phase_adj_max = max(d->input_phase_adj_max, d->output_phase_adj_max);
4497 	caps = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
4498 	for (i = 0; i < ICE_DPLL_PIN_SW_NUM; i++) {
4499 		pin = &d->sma[i];
4500 		pin->idx = i;
4501 		pin->prop.type = DPLL_PIN_TYPE_EXT;
4502 		pin_abs_idx = ICE_DPLL_PIN_SW_INPUT_ABS(i) + input_idx_offset;
4503 		pin->prop.freq_supported =
4504 			ice_cgu_get_pin_freq_supp(&pf->hw, pin_abs_idx,
4505 						  true, &freq_supp_num);
4506 		pin->prop.freq_supported_num = freq_supp_num;
4507 		pin->prop.capabilities =
4508 			(DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE |
4509 			 DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
4510 			 caps);
4511 		pin->pf = pf;
4512 		pin->prop.board_label = ice_dpll_sw_pin_sma[i];
4513 		pin->input = &d->inputs[pin_abs_idx];
4514 		if (pin->input->ref_sync)
4515 			pin->ref_sync = pin->input->ref_sync - pin_abs_idx;
4516 		pin->output = &d->outputs[ICE_DPLL_PIN_SW_OUTPUT_ABS(i)];
4517 		ice_dpll_phase_range_set(&pin->prop.phase_range, phase_adj_max);
4518 	}
4519 	for (i = 0; i < ICE_DPLL_PIN_SW_NUM; i++) {
4520 		pin = &d->ufl[i];
4521 		pin->idx = i;
4522 		pin->prop.type = DPLL_PIN_TYPE_EXT;
4523 		pin->prop.capabilities = caps;
4524 		pin->pf = pf;
4525 		pin->prop.board_label = ice_dpll_sw_pin_ufl[i];
4526 		if (i == ICE_DPLL_PIN_SW_1_IDX) {
4527 			pin->direction = DPLL_PIN_DIRECTION_OUTPUT;
4528 			pin_abs_idx = ICE_DPLL_PIN_SW_OUTPUT_ABS(i);
4529 			pin->prop.freq_supported =
4530 				ice_cgu_get_pin_freq_supp(&pf->hw, pin_abs_idx,
4531 							  false,
4532 							  &freq_supp_num);
4533 			pin->prop.freq_supported_num = freq_supp_num;
4534 			pin->input = NULL;
4535 			pin->output = &d->outputs[pin_abs_idx];
4536 		} else if (i == ICE_DPLL_PIN_SW_2_IDX) {
4537 			pin->direction = DPLL_PIN_DIRECTION_INPUT;
4538 			pin_abs_idx = ICE_DPLL_PIN_SW_INPUT_ABS(i) +
4539 				      input_idx_offset;
4540 			pin->output = NULL;
4541 			pin->input = &d->inputs[pin_abs_idx];
4542 			pin->prop.freq_supported =
4543 				ice_cgu_get_pin_freq_supp(&pf->hw, pin_abs_idx,
4544 							  true, &freq_supp_num);
4545 			pin->prop.freq_supported_num = freq_supp_num;
4546 			pin->prop.capabilities =
4547 				(DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
4548 				 caps);
4549 		}
4550 		ice_dpll_phase_range_set(&pin->prop.phase_range, phase_adj_max);
4551 	}
4552 
4553 	/* Initialize the SMA control register to a known-good default state.
4554 	 * Without this write the PCA9575 GPIO expander retains its power-on
4555 	 * default (all outputs high) which makes all SW pins appear inactive.
4556 	 * Set SMA1 and SMA2 as active inputs, disable U.FL1 output and
4557 	 * U.FL2 input.
4558 	 */
4559 	ret = ice_read_sma_ctrl(&pf->hw, &data);
4560 	if (ret)
4561 		return ret;
4562 	data &= ~ICE_ALL_SMA_MASK;
4563 	data |= ICE_SMA1_TX_EN | ICE_SMA2_TX_EN | ICE_SMA2_UFL2_RX_DIS;
4564 	ret = ice_write_sma_ctrl(&pf->hw, data);
4565 	if (ret)
4566 		return ret;
4567 
4568 	ret = ice_dpll_pin_state_update(pf, pin, ICE_DPLL_PIN_TYPE_SOFTWARE,
4569 					NULL);
4570 	if (ret)
4571 		return ret;
4572 
4573 	return 0;
4574 }
4575 
4576 /**
4577  * ice_dpll_init_info_txclk_pins_e825c - initializes tx-clk pins information
4578  * @pf: board private structure
4579  *
4580  * Init information for tx-clks pin, cache them in pf->dplls.txclks
4581  *
4582  * Return:
4583  * * 0 - success
4584  */
4585 static int ice_dpll_init_info_txclk_pins_e825c(struct ice_pf *pf)
4586 {
4587 	struct ice_dpll_pin *tx_pin;
4588 
4589 	for (int i = 0; i < ICE_DPLL_TXCLK_NUM_MAX; i++) {
4590 		tx_pin = &pf->dplls.txclks[i];
4591 		tx_pin->prop.type = DPLL_PIN_TYPE_EXT;
4592 		tx_pin->prop.capabilities |=
4593 				 DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
4594 		tx_pin->pf = pf;
4595 		if (i == E825_EXT_EREF_PIN_IDX) {
4596 			tx_pin->prop.board_label = ice_dpll_ext_eref_pin;
4597 			tx_pin->tx_ref_src = ICE_REF_CLK_EREF0;
4598 		} else if (i == E825_EXT_SYNCE_PIN_IDX) {
4599 			tx_pin->tx_ref_src = ICE_REF_CLK_SYNCE;
4600 		}
4601 	}
4602 
4603 	return 0;
4604 }
4605 
4606 /**
4607  * ice_dpll_init_pins_info - init pins info wrapper
4608  * @pf: board private structure
4609  * @pin_type: type of pins being initialized
4610  *
4611  * Wraps functions for pin initialization.
4612  *
4613  * Return:
4614  * * 0 - success
4615  * * negative - init failure reason
4616  */
4617 static int
4618 ice_dpll_init_pins_info(struct ice_pf *pf, enum ice_dpll_pin_type pin_type)
4619 {
4620 	switch (pin_type) {
4621 	case ICE_DPLL_PIN_TYPE_INPUT:
4622 	case ICE_DPLL_PIN_TYPE_OUTPUT:
4623 		return ice_dpll_init_info_direct_pins(pf, pin_type);
4624 	case ICE_DPLL_PIN_TYPE_RCLK_INPUT:
4625 		if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825)
4626 			return ice_dpll_init_info_pin_on_pin_e825c(pf);
4627 		else
4628 			return ice_dpll_init_info_rclk_pin(pf);
4629 	case ICE_DPLL_PIN_TYPE_SOFTWARE:
4630 		return ice_dpll_init_info_sw_pins(pf);
4631 
4632 	case ICE_DPLL_PIN_TYPE_TXCLK:
4633 		return ice_dpll_init_info_txclk_pins_e825c(pf);
4634 	default:
4635 		return -EINVAL;
4636 	}
4637 }
4638 
4639 /**
4640  * ice_dpll_deinit_info - release memory allocated for pins info
4641  * @pf: board private structure
4642  *
4643  * Release memory allocated for pins by ice_dpll_init_info function.
4644  */
4645 static void ice_dpll_deinit_info(struct ice_pf *pf)
4646 {
4647 	kfree(pf->dplls.inputs);
4648 	kfree(pf->dplls.outputs);
4649 	kfree(pf->dplls.eec.input_prio);
4650 	kfree(pf->dplls.pps.input_prio);
4651 }
4652 
4653 /**
4654  * ice_dpll_init_info_e825c - prepare pf's dpll information structure for e825c
4655  * device
4656  * @pf: board private structure
4657  *
4658  * Acquire (from HW) and set basic DPLL information (on pf->dplls struct).
4659  *
4660  * Return:
4661  * * 0 - success
4662  * * negative - init failure reason
4663  */
4664 static int ice_dpll_init_info_e825c(struct ice_pf *pf)
4665 {
4666 	struct ice_dplls *d = &pf->dplls;
4667 	struct ice_dpll *dt = &d->txc;
4668 	int ret = 0;
4669 	int i;
4670 
4671 	d->clock_id = ice_generate_clock_id(pf);
4672 	d->num_inputs = ICE_SYNCE_CLK_NUM;
4673 	dt->dpll_state = ice_txclk_lock_status(pf->ptp.port.tx_clk);
4674 	dt->mode = DPLL_MODE_MANUAL;
4675 	dt->dpll_idx = pf->ptp.port.port_num;
4676 
4677 	d->inputs = kzalloc_objs(*d->inputs, d->num_inputs);
4678 	if (!d->inputs)
4679 		return -ENOMEM;
4680 
4681 	ret = ice_get_cgu_rclk_pin_info(&pf->hw, &d->base_rclk_idx,
4682 					&pf->dplls.rclk.num_parents);
4683 	if (ret)
4684 		goto deinit_info;
4685 
4686 	for (i = 0; i < pf->dplls.rclk.num_parents; i++)
4687 		pf->dplls.rclk.parent_idx[i] = d->base_rclk_idx + i;
4688 
4689 	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_RCLK_INPUT);
4690 	if (ret)
4691 		goto deinit_info;
4692 
4693 	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_TXCLK);
4694 	if (ret)
4695 		goto deinit_info;
4696 
4697 	dev_dbg(ice_pf_to_dev(pf),
4698 		"%s - success, inputs: %u, outputs: %u, rclk-parents: %u\n",
4699 		 __func__, d->num_inputs, d->num_outputs, d->rclk.num_parents);
4700 	return 0;
4701 deinit_info:
4702 	ice_dpll_deinit_info(pf);
4703 	return ret;
4704 }
4705 
4706 /**
4707  * ice_dpll_init_info - prepare pf's dpll information structure
4708  * @pf: board private structure
4709  * @cgu: if cgu is present and controlled by this NIC
4710  *
4711  * Acquire (from HW) and set basic dpll information (on pf->dplls struct).
4712  *
4713  * Return:
4714  * * 0 - success
4715  * * negative - init failure reason
4716  */
4717 static int ice_dpll_init_info(struct ice_pf *pf, bool cgu)
4718 {
4719 	struct ice_aqc_get_cgu_abilities abilities;
4720 	struct ice_dpll *de = &pf->dplls.eec;
4721 	struct ice_dpll *dp = &pf->dplls.pps;
4722 	struct ice_dplls *d = &pf->dplls;
4723 	struct ice_hw *hw = &pf->hw;
4724 	int ret, alloc_size, i;
4725 
4726 	d->clock_id = ice_generate_clock_id(pf);
4727 	ret = ice_aq_get_cgu_abilities(hw, &abilities);
4728 	if (ret) {
4729 		dev_err(ice_pf_to_dev(pf),
4730 			"err:%d %s failed to read cgu abilities\n",
4731 			ret, libie_aq_str(hw->adminq.sq_last_status));
4732 		return ret;
4733 	}
4734 
4735 	de->dpll_idx = abilities.eec_dpll_idx;
4736 	dp->dpll_idx = abilities.pps_dpll_idx;
4737 	d->num_inputs = abilities.num_inputs;
4738 	d->num_outputs = abilities.num_outputs;
4739 	d->input_phase_adj_max = le32_to_cpu(abilities.max_in_phase_adj) &
4740 		ICE_AQC_GET_CGU_MAX_PHASE_ADJ;
4741 	d->output_phase_adj_max = le32_to_cpu(abilities.max_out_phase_adj) &
4742 		ICE_AQC_GET_CGU_MAX_PHASE_ADJ;
4743 
4744 	alloc_size = sizeof(*d->inputs) * d->num_inputs;
4745 	d->inputs = kzalloc(alloc_size, GFP_KERNEL);
4746 	if (!d->inputs)
4747 		return -ENOMEM;
4748 
4749 	alloc_size = sizeof(*de->input_prio) * d->num_inputs;
4750 	de->input_prio = kzalloc(alloc_size, GFP_KERNEL);
4751 	if (!de->input_prio)
4752 		return -ENOMEM;
4753 
4754 	dp->input_prio = kzalloc(alloc_size, GFP_KERNEL);
4755 	if (!dp->input_prio)
4756 		return -ENOMEM;
4757 
4758 	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_INPUT);
4759 	if (ret)
4760 		goto deinit_info;
4761 
4762 	if (cgu) {
4763 		alloc_size = sizeof(*d->outputs) * d->num_outputs;
4764 		d->outputs = kzalloc(alloc_size, GFP_KERNEL);
4765 		if (!d->outputs) {
4766 			ret = -ENOMEM;
4767 			goto deinit_info;
4768 		}
4769 
4770 		ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_OUTPUT);
4771 		if (ret)
4772 			goto deinit_info;
4773 		ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_SOFTWARE);
4774 		if (ret)
4775 			goto deinit_info;
4776 	}
4777 
4778 	ret = ice_get_cgu_rclk_pin_info(&pf->hw, &d->base_rclk_idx,
4779 					&pf->dplls.rclk.num_parents);
4780 	if (ret)
4781 		return ret;
4782 	for (i = 0; i < pf->dplls.rclk.num_parents; i++)
4783 		pf->dplls.rclk.parent_idx[i] = d->base_rclk_idx + i;
4784 	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_RCLK_INPUT);
4785 	if (ret)
4786 		return ret;
4787 	de->mode = DPLL_MODE_AUTOMATIC;
4788 	dp->mode = DPLL_MODE_AUTOMATIC;
4789 
4790 	dev_dbg(ice_pf_to_dev(pf),
4791 		"%s - success, inputs:%u, outputs:%u rclk-parents:%u\n",
4792 		__func__, d->num_inputs, d->num_outputs, d->rclk.num_parents);
4793 
4794 	return 0;
4795 
4796 deinit_info:
4797 	dev_err(ice_pf_to_dev(pf),
4798 		"%s - fail: d->inputs:%p, de->input_prio:%p, dp->input_prio:%p, d->outputs:%p\n",
4799 		__func__, d->inputs, de->input_prio,
4800 		dp->input_prio, d->outputs);
4801 	ice_dpll_deinit_info(pf);
4802 	return ret;
4803 }
4804 
4805 /**
4806  * ice_dpll_deinit - Disable the driver/HW support for dpll subsystem
4807  * the dpll device.
4808  * @pf: board private structure
4809  *
4810  * Handles the cleanup work required after dpll initialization, freeing
4811  * resources and unregistering the dpll, pin and all resources used for
4812  * handling them.
4813  *
4814  * Context: Destroys pf->dplls.lock mutex. Call only if ICE_FLAG_DPLL was set.
4815  */
4816 void ice_dpll_deinit(struct ice_pf *pf)
4817 {
4818 	bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);
4819 
4820 	/* Clear ICE_FLAG_DPLL under the lock so that any new caller of
4821 	 * ice_txclk_update_and_notify() observes the cleared flag and
4822 	 * returns early. In-flight callers that already passed the flag
4823 	 * check hold txclk_notify_rwsem for read across the out-of-lock
4824 	 * dpll_*_change_ntf() calls; the down_write/up_write barrier
4825 	 * below waits for them to finish before pins and the TXC DPLL
4826 	 * device may be freed.
4827 	 */
4828 	mutex_lock(&pf->dplls.lock);
4829 	clear_bit(ICE_FLAG_DPLL, pf->flags);
4830 	mutex_unlock(&pf->dplls.lock);
4831 
4832 	/* Wait for in-flight ice_txclk_update_and_notify() readers */
4833 	if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825) {
4834 		down_write(&pf->dplls.txclk_notify_rwsem);
4835 		up_write(&pf->dplls.txclk_notify_rwsem);
4836 	}
4837 
4838 	if (cgu)
4839 		ice_dpll_deinit_worker(pf);
4840 
4841 	if (pf->hw.mac_type == ICE_MAC_GENERIC_3K_E825)
4842 		cancel_work_sync(&pf->dplls.txclk_work);
4843 
4844 	ice_dpll_deinit_pins(pf, cgu);
4845 	if (!IS_ERR_OR_NULL(pf->dplls.pps.dpll))
4846 		ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
4847 	if (!IS_ERR_OR_NULL(pf->dplls.eec.dpll))
4848 		ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
4849 	if (!IS_ERR_OR_NULL(pf->dplls.txc.dpll))
4850 		ice_dpll_deinit_dpll(pf, &pf->dplls.txc, false);
4851 
4852 	ice_dpll_deinit_info(pf);
4853 	mutex_destroy(&pf->dplls.lock);
4854 }
4855 
4856 /**
4857  * ice_dpll_init_e825 - initialize support for dpll subsystem
4858  * @pf: board private structure
4859  *
4860  * Set up the device dplls, register them and pins connected within Linux dpll
4861  * subsystem. Allow userspace to obtain state of DPLL and handling of DPLL
4862  * configuration requests.
4863  *
4864  * Context: Initializes pf->dplls.lock mutex.
4865  */
4866 static void ice_dpll_init_e825(struct ice_pf *pf)
4867 {
4868 	struct ice_dplls *d = &pf->dplls;
4869 	int err;
4870 
4871 	/* E825 sets ICE_F_PHY_RCLK unconditionally, so DPLL may run without
4872 	 * PTP. Populate the HW-topology fields the TX-clk path divides by,
4873 	 * otherwise userspace can trigger div-by-zero in ice_txclk_set_clk().
4874 	 * When PTP is supported, ice_ptp_init() handles this.
4875 	 */
4876 	if (!test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags)) {
4877 		ice_ptp_init_hw(&pf->hw);
4878 		if (pf->hw.lane_num >= 0)
4879 			pf->ptp.port.port_num = pf->hw.lane_num;
4880 	}
4881 
4882 	mutex_init(&d->lock);
4883 	/* Initialize the txclk worker and its notification rwsem before any
4884 	 * code path can fail: ice_dpll_deinit() runs unconditionally on
4885 	 * failure and calls cancel_work_sync() / down_write() on these.
4886 	 */
4887 	INIT_WORK(&d->txclk_work, ice_dpll_txclk_work);
4888 	init_rwsem(&d->txclk_notify_rwsem);
4889 	init_completion(&d->dpll_init);
4890 
4891 	err = ice_dpll_init_info_e825c(pf);
4892 	if (err)
4893 		goto err_exit;
4894 	err = ice_dpll_init_dpll(pf, &pf->dplls.txc, false, DPLL_TYPE_GENERIC);
4895 	if (err)
4896 		goto deinit_info;
4897 	err = ice_dpll_init_pins_e825(pf);
4898 	if (err)
4899 		goto deinit_txclk;
4900 	set_bit(ICE_FLAG_DPLL, pf->flags);
4901 	complete_all(&d->dpll_init);
4902 
4903 	return;
4904 
4905 deinit_txclk:
4906 	ice_dpll_deinit_dpll(pf, &pf->dplls.txc, false);
4907 deinit_info:
4908 	ice_dpll_deinit_info(pf);
4909 err_exit:
4910 	mutex_destroy(&d->lock);
4911 	dev_warn(ice_pf_to_dev(pf), "DPLLs init failure err:%d\n", err);
4912 }
4913 
4914 /**
4915  * ice_dpll_init_e810 - initialize support for dpll subsystem
4916  * @pf: board private structure
4917  *
4918  * Set up the device dplls, register them and pins connected within Linux dpll
4919  * subsystem. Allow userspace to obtain state of DPLL and handling of DPLL
4920  * configuration requests.
4921  *
4922  * Context: Initializes pf->dplls.lock mutex.
4923  */
4924 static void ice_dpll_init_e810(struct ice_pf *pf)
4925 {
4926 	bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);
4927 	struct ice_dplls *d = &pf->dplls;
4928 	int err = 0;
4929 
4930 	mutex_init(&d->lock);
4931 	err = ice_dpll_init_info(pf, cgu);
4932 	if (err)
4933 		goto err_exit;
4934 	err = ice_dpll_init_dpll(pf, &pf->dplls.eec, cgu, DPLL_TYPE_EEC);
4935 	if (err)
4936 		goto deinit_info;
4937 	err = ice_dpll_init_dpll(pf, &pf->dplls.pps, cgu, DPLL_TYPE_PPS);
4938 	if (err)
4939 		goto deinit_eec;
4940 	err = ice_dpll_init_pins(pf, cgu);
4941 	if (err)
4942 		goto deinit_pps;
4943 	if (cgu) {
4944 		err = ice_dpll_init_worker(pf);
4945 		if (err)
4946 			goto deinit_pins;
4947 	}
4948 	set_bit(ICE_FLAG_DPLL, pf->flags);
4949 
4950 	return;
4951 
4952 deinit_pins:
4953 	ice_dpll_deinit_pins(pf, cgu);
4954 deinit_pps:
4955 	ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
4956 deinit_eec:
4957 	ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
4958 deinit_info:
4959 	ice_dpll_deinit_info(pf);
4960 err_exit:
4961 	mutex_destroy(&d->lock);
4962 	dev_warn(ice_pf_to_dev(pf), "DPLLs init failure err:%d\n", err);
4963 }
4964 
4965 void ice_dpll_init(struct ice_pf *pf)
4966 {
4967 	switch (pf->hw.mac_type) {
4968 	case ICE_MAC_GENERIC_3K_E825:
4969 		ice_dpll_init_e825(pf);
4970 		break;
4971 	default:
4972 		ice_dpll_init_e810(pf);
4973 		break;
4974 	}
4975 }
4976