xref: /linux/drivers/net/ethernet/intel/ice/ice_dpll.c (revision c4688ff47fd719e2371b984d59759f9fa09dd6a2)
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 <linux/dpll.h>
8 
9 #define ICE_CGU_STATE_ACQ_ERR_THRESHOLD		50
10 #define ICE_DPLL_PIN_IDX_INVALID		0xff
11 #define ICE_DPLL_RCLK_NUM_PER_PF		1
12 #define ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT	25
13 #define ICE_DPLL_PIN_GEN_RCLK_FREQ		1953125
14 #define ICE_DPLL_PIN_PRIO_OUTPUT		0xff
15 #define ICE_DPLL_SW_PIN_INPUT_BASE_SFP		4
16 #define ICE_DPLL_SW_PIN_INPUT_BASE_QSFP		6
17 #define ICE_DPLL_SW_PIN_OUTPUT_BASE		0
18 
19 #define ICE_DPLL_PIN_SW_INPUT_ABS(in_idx) \
20 	(ICE_DPLL_SW_PIN_INPUT_BASE_SFP + (in_idx))
21 
22 #define ICE_DPLL_PIN_SW_1_INPUT_ABS_IDX \
23 	(ICE_DPLL_PIN_SW_INPUT_ABS(ICE_DPLL_PIN_SW_1_IDX))
24 
25 #define ICE_DPLL_PIN_SW_2_INPUT_ABS_IDX \
26 	(ICE_DPLL_PIN_SW_INPUT_ABS(ICE_DPLL_PIN_SW_2_IDX))
27 
28 #define ICE_DPLL_PIN_SW_OUTPUT_ABS(out_idx) \
29 	(ICE_DPLL_SW_PIN_OUTPUT_BASE + (out_idx))
30 
31 #define ICE_DPLL_PIN_SW_1_OUTPUT_ABS_IDX \
32 	(ICE_DPLL_PIN_SW_OUTPUT_ABS(ICE_DPLL_PIN_SW_1_IDX))
33 
34 #define ICE_DPLL_PIN_SW_2_OUTPUT_ABS_IDX \
35 	(ICE_DPLL_PIN_SW_OUTPUT_ABS(ICE_DPLL_PIN_SW_2_IDX))
36 
37 /**
38  * enum ice_dpll_pin_type - enumerate ice pin types:
39  * @ICE_DPLL_PIN_INVALID: invalid pin type
40  * @ICE_DPLL_PIN_TYPE_INPUT: input pin
41  * @ICE_DPLL_PIN_TYPE_OUTPUT: output pin
42  * @ICE_DPLL_PIN_TYPE_RCLK_INPUT: recovery clock input pin
43  * @ICE_DPLL_PIN_TYPE_SOFTWARE: software controlled SMA/U.FL pins
44  */
45 enum ice_dpll_pin_type {
46 	ICE_DPLL_PIN_INVALID,
47 	ICE_DPLL_PIN_TYPE_INPUT,
48 	ICE_DPLL_PIN_TYPE_OUTPUT,
49 	ICE_DPLL_PIN_TYPE_RCLK_INPUT,
50 	ICE_DPLL_PIN_TYPE_SOFTWARE,
51 };
52 
53 static const char * const pin_type_name[] = {
54 	[ICE_DPLL_PIN_TYPE_INPUT] = "input",
55 	[ICE_DPLL_PIN_TYPE_OUTPUT] = "output",
56 	[ICE_DPLL_PIN_TYPE_RCLK_INPUT] = "rclk-input",
57 	[ICE_DPLL_PIN_TYPE_SOFTWARE] = "software",
58 };
59 
60 static const char * const ice_dpll_sw_pin_sma[] = { "SMA1", "SMA2" };
61 static const char * const ice_dpll_sw_pin_ufl[] = { "U.FL1", "U.FL2" };
62 
63 static const struct dpll_pin_frequency ice_esync_range[] = {
64 	DPLL_PIN_FREQUENCY_RANGE(0, DPLL_PIN_FREQUENCY_1_HZ),
65 };
66 
67 /**
68  * ice_dpll_is_sw_pin - check if given pin shall be controlled by SW
69  * @pf: private board structure
70  * @index: index of a pin as understood by FW
71  * @input: true for input, false for output
72  *
73  * Check if the pin shall be controlled by SW - instead of providing raw access
74  * for pin control. For E810 NIC with dpll there is additional MUX-related logic
75  * between SMA/U.FL pins/connectors and dpll device, best to give user access
76  * with series of wrapper functions as from user perspective they convey single
77  * functionality rather then separated pins.
78  *
79  * Return:
80  * * true - pin controlled by SW
81  * * false - pin not controlled by SW
82  */
83 static bool ice_dpll_is_sw_pin(struct ice_pf *pf, u8 index, bool input)
84 {
85 	if (input && pf->hw.device_id == ICE_DEV_ID_E810C_QSFP)
86 		index -= ICE_DPLL_SW_PIN_INPUT_BASE_QSFP -
87 			 ICE_DPLL_SW_PIN_INPUT_BASE_SFP;
88 
89 	if ((input && (index == ICE_DPLL_PIN_SW_1_INPUT_ABS_IDX ||
90 		       index == ICE_DPLL_PIN_SW_2_INPUT_ABS_IDX)) ||
91 	    (!input && (index == ICE_DPLL_PIN_SW_1_OUTPUT_ABS_IDX ||
92 			index == ICE_DPLL_PIN_SW_2_OUTPUT_ABS_IDX)))
93 		return true;
94 	return false;
95 }
96 
97 /**
98  * ice_dpll_is_reset - check if reset is in progress
99  * @pf: private board structure
100  * @extack: error reporting
101  *
102  * If reset is in progress, fill extack with error.
103  *
104  * Return:
105  * * false - no reset in progress
106  * * true - reset in progress
107  */
108 static bool ice_dpll_is_reset(struct ice_pf *pf, struct netlink_ext_ack *extack)
109 {
110 	if (ice_is_reset_in_progress(pf->state)) {
111 		NL_SET_ERR_MSG(extack, "PF reset in progress");
112 		return true;
113 	}
114 	return false;
115 }
116 
117 /**
118  * ice_dpll_pin_freq_set - set pin's frequency
119  * @pf: private board structure
120  * @pin: pointer to a pin
121  * @pin_type: type of pin being configured
122  * @freq: frequency to be set
123  * @extack: error reporting
124  *
125  * Set requested frequency on a pin.
126  *
127  * Context: Called under pf->dplls.lock
128  * Return:
129  * * 0 - success
130  * * negative - error on AQ or wrong pin type given
131  */
132 static int
133 ice_dpll_pin_freq_set(struct ice_pf *pf, struct ice_dpll_pin *pin,
134 		      enum ice_dpll_pin_type pin_type, const u32 freq,
135 		      struct netlink_ext_ack *extack)
136 {
137 	u8 flags;
138 	int ret;
139 
140 	switch (pin_type) {
141 	case ICE_DPLL_PIN_TYPE_INPUT:
142 		flags = ICE_AQC_SET_CGU_IN_CFG_FLG1_UPDATE_FREQ;
143 		ret = ice_aq_set_input_pin_cfg(&pf->hw, pin->idx, flags,
144 					       pin->flags[0], freq, 0);
145 		break;
146 	case ICE_DPLL_PIN_TYPE_OUTPUT:
147 		flags = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_FREQ;
148 		ret = ice_aq_set_output_pin_cfg(&pf->hw, pin->idx, flags,
149 						0, freq, 0);
150 		break;
151 	default:
152 		return -EINVAL;
153 	}
154 	if (ret) {
155 		NL_SET_ERR_MSG_FMT(extack,
156 				   "err:%d %s failed to set pin freq:%u on pin:%u",
157 				   ret,
158 				   ice_aq_str(pf->hw.adminq.sq_last_status),
159 				   freq, pin->idx);
160 		return ret;
161 	}
162 	pin->freq = freq;
163 
164 	return 0;
165 }
166 
167 /**
168  * ice_dpll_frequency_set - wrapper for pin callback for set frequency
169  * @pin: pointer to a pin
170  * @pin_priv: private data pointer passed on pin registration
171  * @dpll: pointer to dpll
172  * @dpll_priv: private data pointer passed on dpll registration
173  * @frequency: frequency to be set
174  * @extack: error reporting
175  * @pin_type: type of pin being configured
176  *
177  * Wraps internal set frequency command on a pin.
178  *
179  * Context: Acquires pf->dplls.lock
180  * Return:
181  * * 0 - success
182  * * negative - error pin not found or couldn't set in hw
183  */
184 static int
185 ice_dpll_frequency_set(const struct dpll_pin *pin, void *pin_priv,
186 		       const struct dpll_device *dpll, void *dpll_priv,
187 		       const u32 frequency,
188 		       struct netlink_ext_ack *extack,
189 		       enum ice_dpll_pin_type pin_type)
190 {
191 	struct ice_dpll_pin *p = pin_priv;
192 	struct ice_dpll *d = dpll_priv;
193 	struct ice_pf *pf = d->pf;
194 	int ret;
195 
196 	if (ice_dpll_is_reset(pf, extack))
197 		return -EBUSY;
198 
199 	mutex_lock(&pf->dplls.lock);
200 	ret = ice_dpll_pin_freq_set(pf, p, pin_type, frequency, extack);
201 	mutex_unlock(&pf->dplls.lock);
202 
203 	return ret;
204 }
205 
206 /**
207  * ice_dpll_input_frequency_set - input pin callback for set frequency
208  * @pin: pointer to a pin
209  * @pin_priv: private data pointer passed on pin registration
210  * @dpll: pointer to dpll
211  * @dpll_priv: private data pointer passed on dpll registration
212  * @frequency: frequency to be set
213  * @extack: error reporting
214  *
215  * Wraps internal set frequency command on a pin.
216  *
217  * Context: Calls a function which acquires pf->dplls.lock
218  * Return:
219  * * 0 - success
220  * * negative - error pin not found or couldn't set in hw
221  */
222 static int
223 ice_dpll_input_frequency_set(const struct dpll_pin *pin, void *pin_priv,
224 			     const struct dpll_device *dpll, void *dpll_priv,
225 			     u64 frequency, struct netlink_ext_ack *extack)
226 {
227 	return ice_dpll_frequency_set(pin, pin_priv, dpll, dpll_priv, frequency,
228 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
229 }
230 
231 /**
232  * ice_dpll_output_frequency_set - output 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_output_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_OUTPUT);
254 }
255 
256 /**
257  * ice_dpll_frequency_get - wrapper for pin callback for get 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: on success holds pin's frequency
263  * @extack: error reporting
264  * @pin_type: type of pin being configured
265  *
266  * Wraps internal get frequency command of a pin.
267  *
268  * Context: Acquires pf->dplls.lock
269  * Return:
270  * * 0 - success
271  * * negative - error pin not found or couldn't get from hw
272  */
273 static int
274 ice_dpll_frequency_get(const struct dpll_pin *pin, void *pin_priv,
275 		       const struct dpll_device *dpll, void *dpll_priv,
276 		       u64 *frequency, struct netlink_ext_ack *extack,
277 		       enum ice_dpll_pin_type pin_type)
278 {
279 	struct ice_dpll_pin *p = pin_priv;
280 	struct ice_dpll *d = dpll_priv;
281 	struct ice_pf *pf = d->pf;
282 
283 	mutex_lock(&pf->dplls.lock);
284 	*frequency = p->freq;
285 	mutex_unlock(&pf->dplls.lock);
286 
287 	return 0;
288 }
289 
290 /**
291  * ice_dpll_input_frequency_get - input pin callback for get frequency
292  * @pin: pointer to a pin
293  * @pin_priv: private data pointer passed on pin registration
294  * @dpll: pointer to dpll
295  * @dpll_priv: private data pointer passed on dpll registration
296  * @frequency: on success holds pin's frequency
297  * @extack: error reporting
298  *
299  * Wraps internal get frequency command of a input pin.
300  *
301  * Context: Calls a function which acquires pf->dplls.lock
302  * Return:
303  * * 0 - success
304  * * negative - error pin not found or couldn't get from hw
305  */
306 static int
307 ice_dpll_input_frequency_get(const struct dpll_pin *pin, void *pin_priv,
308 			     const struct dpll_device *dpll, void *dpll_priv,
309 			     u64 *frequency, struct netlink_ext_ack *extack)
310 {
311 	return ice_dpll_frequency_get(pin, pin_priv, dpll, dpll_priv, frequency,
312 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
313 }
314 
315 /**
316  * ice_dpll_output_frequency_get - output 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 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_output_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_OUTPUT);
338 }
339 
340 /**
341  * ice_dpll_sw_pin_frequency_set - callback to set frequency of SW pin
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  * Calls set frequency command for corresponding and active input/output pin.
350  *
351  * Context: Calls a function which acquires and releases pf->dplls.lock
352  * Return:
353  * * 0 - success
354  * * negative - error pin not active or couldn't get from hw
355  */
356 static int
357 ice_dpll_sw_pin_frequency_set(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 	struct ice_dpll_pin *sma = pin_priv;
362 	int ret;
363 
364 	if (!sma->active) {
365 		NL_SET_ERR_MSG(extack, "pin is not active");
366 		return -EINVAL;
367 	}
368 	if (sma->direction == DPLL_PIN_DIRECTION_INPUT)
369 		ret = ice_dpll_input_frequency_set(NULL, sma->input, dpll,
370 						   dpll_priv, frequency,
371 						   extack);
372 	else
373 		ret = ice_dpll_output_frequency_set(NULL, sma->output, dpll,
374 						    dpll_priv, frequency,
375 						    extack);
376 
377 	return ret;
378 }
379 
380 /**
381  * ice_dpll_sw_pin_frequency_get - callback for get frequency of SW pin
382  * @pin: pointer to a pin
383  * @pin_priv: private data pointer passed on pin registration
384  * @dpll: pointer to dpll
385  * @dpll_priv: private data pointer passed on dpll registration
386  * @frequency: on success holds pin's frequency
387  * @extack: error reporting
388  *
389  * Calls get frequency command for corresponding active input/output.
390  *
391  * Context: Calls a function which acquires and releases pf->dplls.lock
392  * Return:
393  * * 0 - success
394  * * negative - error pin not active or couldn't get from hw
395  */
396 static int
397 ice_dpll_sw_pin_frequency_get(const struct dpll_pin *pin, void *pin_priv,
398 			      const struct dpll_device *dpll, void *dpll_priv,
399 			      u64 *frequency, struct netlink_ext_ack *extack)
400 {
401 	struct ice_dpll_pin *sma = pin_priv;
402 	int ret;
403 
404 	if (!sma->active) {
405 		*frequency = 0;
406 		return 0;
407 	}
408 	if (sma->direction == DPLL_PIN_DIRECTION_INPUT) {
409 		ret = ice_dpll_input_frequency_get(NULL, sma->input, dpll,
410 						   dpll_priv, frequency,
411 						   extack);
412 	} else {
413 		ret = ice_dpll_output_frequency_get(NULL, sma->output, dpll,
414 						    dpll_priv, frequency,
415 						    extack);
416 	}
417 
418 	return ret;
419 }
420 
421 /**
422  * ice_dpll_pin_enable - enable a pin on dplls
423  * @hw: board private hw structure
424  * @pin: pointer to a pin
425  * @dpll_idx: dpll index to connect to output pin
426  * @pin_type: type of pin being enabled
427  * @extack: error reporting
428  *
429  * Enable a pin on both dplls. Store current state in pin->flags.
430  *
431  * Context: Called under pf->dplls.lock
432  * Return:
433  * * 0 - OK
434  * * negative - error
435  */
436 static int
437 ice_dpll_pin_enable(struct ice_hw *hw, struct ice_dpll_pin *pin,
438 		    u8 dpll_idx, enum ice_dpll_pin_type pin_type,
439 		    struct netlink_ext_ack *extack)
440 {
441 	u8 flags = 0;
442 	int ret;
443 
444 	switch (pin_type) {
445 	case ICE_DPLL_PIN_TYPE_INPUT:
446 		if (pin->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
447 			flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
448 		flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
449 		ret = ice_aq_set_input_pin_cfg(hw, pin->idx, 0, flags, 0, 0);
450 		break;
451 	case ICE_DPLL_PIN_TYPE_OUTPUT:
452 		flags = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_SRC_SEL;
453 		if (pin->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
454 			flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
455 		flags |= ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
456 		ret = ice_aq_set_output_pin_cfg(hw, pin->idx, flags, dpll_idx,
457 						0, 0);
458 		break;
459 	default:
460 		return -EINVAL;
461 	}
462 	if (ret)
463 		NL_SET_ERR_MSG_FMT(extack,
464 				   "err:%d %s failed to enable %s pin:%u",
465 				   ret, ice_aq_str(hw->adminq.sq_last_status),
466 				   pin_type_name[pin_type], pin->idx);
467 
468 	return ret;
469 }
470 
471 /**
472  * ice_dpll_pin_disable - disable a pin on dplls
473  * @hw: board private hw structure
474  * @pin: pointer to a pin
475  * @pin_type: type of pin being disabled
476  * @extack: error reporting
477  *
478  * Disable a pin on both dplls. Store current state in pin->flags.
479  *
480  * Context: Called under pf->dplls.lock
481  * Return:
482  * * 0 - OK
483  * * negative - error
484  */
485 static int
486 ice_dpll_pin_disable(struct ice_hw *hw, struct ice_dpll_pin *pin,
487 		     enum ice_dpll_pin_type pin_type,
488 		     struct netlink_ext_ack *extack)
489 {
490 	u8 flags = 0;
491 	int ret;
492 
493 	switch (pin_type) {
494 	case ICE_DPLL_PIN_TYPE_INPUT:
495 		if (pin->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
496 			flags |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
497 		ret = ice_aq_set_input_pin_cfg(hw, pin->idx, 0, flags, 0, 0);
498 		break;
499 	case ICE_DPLL_PIN_TYPE_OUTPUT:
500 		if (pin->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
501 			flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
502 		ret = ice_aq_set_output_pin_cfg(hw, pin->idx, flags, 0, 0, 0);
503 		break;
504 	default:
505 		return -EINVAL;
506 	}
507 	if (ret)
508 		NL_SET_ERR_MSG_FMT(extack,
509 				   "err:%d %s failed to disable %s pin:%u",
510 				   ret, ice_aq_str(hw->adminq.sq_last_status),
511 				   pin_type_name[pin_type], pin->idx);
512 
513 	return ret;
514 }
515 
516 /**
517  * ice_dpll_sw_pins_update - update status of all SW pins
518  * @pf: private board struct
519  *
520  * Determine and update pin struct fields (direction/active) of their current
521  * values for all the SW controlled pins.
522  *
523  * Context: Call with pf->dplls.lock held
524  * Return:
525  * * 0 - OK
526  * * negative - error
527  */
528 static int
529 ice_dpll_sw_pins_update(struct ice_pf *pf)
530 {
531 	struct ice_dplls *d = &pf->dplls;
532 	struct ice_dpll_pin *p;
533 	u8 data = 0;
534 	int ret;
535 
536 	ret = ice_read_sma_ctrl(&pf->hw, &data);
537 	if (ret)
538 		return ret;
539 	/* no change since last check */
540 	if (d->sma_data == data)
541 		return 0;
542 
543 	/*
544 	 * SMA1/U.FL1 vs SMA2/U.FL2 are using different bit scheme to decide
545 	 * on their direction and if are active
546 	 */
547 	p = &d->sma[ICE_DPLL_PIN_SW_1_IDX];
548 	p->active = true;
549 	p->direction = DPLL_PIN_DIRECTION_INPUT;
550 	if (data & ICE_SMA1_DIR_EN) {
551 		p->direction = DPLL_PIN_DIRECTION_OUTPUT;
552 		if (data & ICE_SMA1_TX_EN)
553 			p->active = false;
554 	}
555 
556 	p = &d->sma[ICE_DPLL_PIN_SW_2_IDX];
557 	p->active = true;
558 	p->direction = DPLL_PIN_DIRECTION_INPUT;
559 	if ((data & ICE_SMA2_INACTIVE_MASK) == ICE_SMA2_INACTIVE_MASK)
560 		p->active = false;
561 	else if (data & ICE_SMA2_DIR_EN)
562 		p->direction = DPLL_PIN_DIRECTION_OUTPUT;
563 
564 	p = &d->ufl[ICE_DPLL_PIN_SW_1_IDX];
565 	if (!(data & (ICE_SMA1_DIR_EN | ICE_SMA1_TX_EN)))
566 		p->active = true;
567 	else
568 		p->active = false;
569 
570 	p = &d->ufl[ICE_DPLL_PIN_SW_2_IDX];
571 	p->active = (data & ICE_SMA2_DIR_EN) && !(data & ICE_SMA2_UFL2_RX_DIS);
572 	d->sma_data = data;
573 
574 	return 0;
575 }
576 
577 /**
578  * ice_dpll_pin_state_update - update pin's state
579  * @pf: private board struct
580  * @pin: structure with pin attributes to be updated
581  * @pin_type: type of pin being updated
582  * @extack: error reporting
583  *
584  * Determine pin current state and frequency, then update struct
585  * holding the pin info. For input pin states are separated for each
586  * dpll, for rclk pins states are separated for each parent.
587  *
588  * Context: Called under pf->dplls.lock
589  * Return:
590  * * 0 - OK
591  * * negative - error
592  */
593 static int
594 ice_dpll_pin_state_update(struct ice_pf *pf, struct ice_dpll_pin *pin,
595 			  enum ice_dpll_pin_type pin_type,
596 			  struct netlink_ext_ack *extack)
597 {
598 	u8 parent, port_num = ICE_AQC_SET_PHY_REC_CLK_OUT_CURR_PORT;
599 	int ret;
600 
601 	switch (pin_type) {
602 	case ICE_DPLL_PIN_TYPE_INPUT:
603 		ret = ice_aq_get_input_pin_cfg(&pf->hw, pin->idx, &pin->status,
604 					       NULL, NULL, &pin->flags[0],
605 					       &pin->freq, &pin->phase_adjust);
606 		if (ret)
607 			goto err;
608 		if (ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN & pin->flags[0]) {
609 			if (pin->pin) {
610 				pin->state[pf->dplls.eec.dpll_idx] =
611 					pin->pin == pf->dplls.eec.active_input ?
612 					DPLL_PIN_STATE_CONNECTED :
613 					DPLL_PIN_STATE_SELECTABLE;
614 				pin->state[pf->dplls.pps.dpll_idx] =
615 					pin->pin == pf->dplls.pps.active_input ?
616 					DPLL_PIN_STATE_CONNECTED :
617 					DPLL_PIN_STATE_SELECTABLE;
618 			} else {
619 				pin->state[pf->dplls.eec.dpll_idx] =
620 					DPLL_PIN_STATE_SELECTABLE;
621 				pin->state[pf->dplls.pps.dpll_idx] =
622 					DPLL_PIN_STATE_SELECTABLE;
623 			}
624 		} else {
625 			pin->state[pf->dplls.eec.dpll_idx] =
626 				DPLL_PIN_STATE_DISCONNECTED;
627 			pin->state[pf->dplls.pps.dpll_idx] =
628 				DPLL_PIN_STATE_DISCONNECTED;
629 		}
630 		break;
631 	case ICE_DPLL_PIN_TYPE_OUTPUT:
632 		ret = ice_aq_get_output_pin_cfg(&pf->hw, pin->idx,
633 						&pin->flags[0], &parent,
634 						&pin->freq, NULL);
635 		if (ret)
636 			goto err;
637 
638 		parent &= ICE_AQC_GET_CGU_OUT_CFG_DPLL_SRC_SEL;
639 		if (ICE_AQC_GET_CGU_OUT_CFG_OUT_EN & pin->flags[0]) {
640 			pin->state[pf->dplls.eec.dpll_idx] =
641 				parent == pf->dplls.eec.dpll_idx ?
642 				DPLL_PIN_STATE_CONNECTED :
643 				DPLL_PIN_STATE_DISCONNECTED;
644 			pin->state[pf->dplls.pps.dpll_idx] =
645 				parent == pf->dplls.pps.dpll_idx ?
646 				DPLL_PIN_STATE_CONNECTED :
647 				DPLL_PIN_STATE_DISCONNECTED;
648 		} else {
649 			pin->state[pf->dplls.eec.dpll_idx] =
650 				DPLL_PIN_STATE_DISCONNECTED;
651 			pin->state[pf->dplls.pps.dpll_idx] =
652 				DPLL_PIN_STATE_DISCONNECTED;
653 		}
654 		break;
655 	case ICE_DPLL_PIN_TYPE_RCLK_INPUT:
656 		for (parent = 0; parent < pf->dplls.rclk.num_parents;
657 		     parent++) {
658 			u8 p = parent;
659 
660 			ret = ice_aq_get_phy_rec_clk_out(&pf->hw, &p,
661 							 &port_num,
662 							 &pin->flags[parent],
663 							 NULL);
664 			if (ret)
665 				goto err;
666 			if (ICE_AQC_GET_PHY_REC_CLK_OUT_OUT_EN &
667 			    pin->flags[parent])
668 				pin->state[parent] = DPLL_PIN_STATE_CONNECTED;
669 			else
670 				pin->state[parent] =
671 					DPLL_PIN_STATE_DISCONNECTED;
672 		}
673 		break;
674 	case ICE_DPLL_PIN_TYPE_SOFTWARE:
675 		ret = ice_dpll_sw_pins_update(pf);
676 		if (ret)
677 			goto err;
678 		break;
679 	default:
680 		return -EINVAL;
681 	}
682 
683 	return 0;
684 err:
685 	if (extack)
686 		NL_SET_ERR_MSG_FMT(extack,
687 				   "err:%d %s failed to update %s pin:%u",
688 				   ret,
689 				   ice_aq_str(pf->hw.adminq.sq_last_status),
690 				   pin_type_name[pin_type], pin->idx);
691 	else
692 		dev_err_ratelimited(ice_pf_to_dev(pf),
693 				    "err:%d %s failed to update %s pin:%u\n",
694 				    ret,
695 				    ice_aq_str(pf->hw.adminq.sq_last_status),
696 				    pin_type_name[pin_type], pin->idx);
697 	return ret;
698 }
699 
700 /**
701  * ice_dpll_hw_input_prio_set - set input priority value in hardware
702  * @pf: board private structure
703  * @dpll: ice dpll pointer
704  * @pin: ice pin pointer
705  * @prio: priority value being set on a dpll
706  * @extack: error reporting
707  *
708  * Internal wrapper for setting the priority in the hardware.
709  *
710  * Context: Called under pf->dplls.lock
711  * Return:
712  * * 0 - success
713  * * negative - failure
714  */
715 static int
716 ice_dpll_hw_input_prio_set(struct ice_pf *pf, struct ice_dpll *dpll,
717 			   struct ice_dpll_pin *pin, const u32 prio,
718 			   struct netlink_ext_ack *extack)
719 {
720 	int ret;
721 
722 	ret = ice_aq_set_cgu_ref_prio(&pf->hw, dpll->dpll_idx, pin->idx,
723 				      (u8)prio);
724 	if (ret)
725 		NL_SET_ERR_MSG_FMT(extack,
726 				   "err:%d %s failed to set pin prio:%u on pin:%u",
727 				   ret,
728 				   ice_aq_str(pf->hw.adminq.sq_last_status),
729 				   prio, pin->idx);
730 	else
731 		dpll->input_prio[pin->idx] = prio;
732 
733 	return ret;
734 }
735 
736 /**
737  * ice_dpll_lock_status_get - get dpll lock status callback
738  * @dpll: registered dpll pointer
739  * @dpll_priv: private data pointer passed on dpll registration
740  * @status: on success holds dpll's lock status
741  * @status_error: status error value
742  * @extack: error reporting
743  *
744  * Dpll subsystem callback, provides dpll's lock status.
745  *
746  * Context: Acquires pf->dplls.lock
747  * Return:
748  * * 0 - success
749  * * negative - failure
750  */
751 static int
752 ice_dpll_lock_status_get(const struct dpll_device *dpll, void *dpll_priv,
753 			 enum dpll_lock_status *status,
754 			 enum dpll_lock_status_error *status_error,
755 			 struct netlink_ext_ack *extack)
756 {
757 	struct ice_dpll *d = dpll_priv;
758 	struct ice_pf *pf = d->pf;
759 
760 	mutex_lock(&pf->dplls.lock);
761 	*status = d->dpll_state;
762 	mutex_unlock(&pf->dplls.lock);
763 
764 	return 0;
765 }
766 
767 /**
768  * ice_dpll_mode_get - get dpll's working mode
769  * @dpll: registered dpll pointer
770  * @dpll_priv: private data pointer passed on dpll registration
771  * @mode: on success holds current working mode of dpll
772  * @extack: error reporting
773  *
774  * Dpll subsystem callback. Provides working mode of dpll.
775  *
776  * Context: Acquires pf->dplls.lock
777  * Return:
778  * * 0 - success
779  * * negative - failure
780  */
781 static int ice_dpll_mode_get(const struct dpll_device *dpll, void *dpll_priv,
782 			     enum dpll_mode *mode,
783 			     struct netlink_ext_ack *extack)
784 {
785 	struct ice_dpll *d = dpll_priv;
786 	struct ice_pf *pf = d->pf;
787 
788 	mutex_lock(&pf->dplls.lock);
789 	*mode = d->mode;
790 	mutex_unlock(&pf->dplls.lock);
791 
792 	return 0;
793 }
794 
795 /**
796  * ice_dpll_pin_state_set - set pin's state on dpll
797  * @pin: pointer to a pin
798  * @pin_priv: private data pointer passed on pin registration
799  * @dpll: registered dpll pointer
800  * @dpll_priv: private data pointer passed on dpll registration
801  * @enable: if pin shalll be enabled
802  * @extack: error reporting
803  * @pin_type: type of a pin
804  *
805  * Set pin state on a pin.
806  *
807  * Context: Acquires pf->dplls.lock
808  * Return:
809  * * 0 - OK or no change required
810  * * negative - error
811  */
812 static int
813 ice_dpll_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
814 		       const struct dpll_device *dpll, void *dpll_priv,
815 		       bool enable, struct netlink_ext_ack *extack,
816 		       enum ice_dpll_pin_type pin_type)
817 {
818 	struct ice_dpll_pin *p = pin_priv;
819 	struct ice_dpll *d = dpll_priv;
820 	struct ice_pf *pf = d->pf;
821 	int ret;
822 
823 	if (ice_dpll_is_reset(pf, extack))
824 		return -EBUSY;
825 
826 	mutex_lock(&pf->dplls.lock);
827 	if (enable)
828 		ret = ice_dpll_pin_enable(&pf->hw, p, d->dpll_idx, pin_type,
829 					  extack);
830 	else
831 		ret = ice_dpll_pin_disable(&pf->hw, p, pin_type, extack);
832 	if (!ret)
833 		ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
834 	mutex_unlock(&pf->dplls.lock);
835 
836 	return ret;
837 }
838 
839 /**
840  * ice_dpll_output_state_set - enable/disable output pin on dpll device
841  * @pin: pointer to a pin
842  * @pin_priv: private data pointer passed on pin registration
843  * @dpll: dpll being configured
844  * @dpll_priv: private data pointer passed on dpll registration
845  * @state: state of pin to be set
846  * @extack: error reporting
847  *
848  * Dpll subsystem callback. Set given state on output type pin.
849  *
850  * Context: Calls a function which acquires pf->dplls.lock
851  * Return:
852  * * 0 - successfully enabled mode
853  * * negative - failed to enable mode
854  */
855 static int
856 ice_dpll_output_state_set(const struct dpll_pin *pin, void *pin_priv,
857 			  const struct dpll_device *dpll, void *dpll_priv,
858 			  enum dpll_pin_state state,
859 			  struct netlink_ext_ack *extack)
860 {
861 	bool enable = state == DPLL_PIN_STATE_CONNECTED;
862 	struct ice_dpll_pin *p = pin_priv;
863 	struct ice_dpll *d = dpll_priv;
864 
865 	if (state == DPLL_PIN_STATE_SELECTABLE)
866 		return -EINVAL;
867 	if (!enable && p->state[d->dpll_idx] == DPLL_PIN_STATE_DISCONNECTED)
868 		return 0;
869 
870 	return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
871 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
872 }
873 
874 /**
875  * ice_dpll_input_state_set - enable/disable input pin on dpll levice
876  * @pin: pointer to a pin
877  * @pin_priv: private data pointer passed on pin registration
878  * @dpll: dpll being configured
879  * @dpll_priv: private data pointer passed on dpll registration
880  * @state: state of pin to be set
881  * @extack: error reporting
882  *
883  * Dpll subsystem callback. Enables given mode on input type pin.
884  *
885  * Context: Calls a function which acquires pf->dplls.lock
886  * Return:
887  * * 0 - successfully enabled mode
888  * * negative - failed to enable mode
889  */
890 static int
891 ice_dpll_input_state_set(const struct dpll_pin *pin, void *pin_priv,
892 			 const struct dpll_device *dpll, void *dpll_priv,
893 			 enum dpll_pin_state state,
894 			 struct netlink_ext_ack *extack)
895 {
896 	bool enable = state == DPLL_PIN_STATE_SELECTABLE;
897 
898 	return ice_dpll_pin_state_set(pin, pin_priv, dpll, dpll_priv, enable,
899 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
900 }
901 
902 /**
903  * ice_dpll_pin_state_get - set pin's state on dpll
904  * @pin: pointer to a pin
905  * @pin_priv: private data pointer passed on pin registration
906  * @dpll: registered dpll pointer
907  * @dpll_priv: private data pointer passed on dpll registration
908  * @state: on success holds state of the pin
909  * @extack: error reporting
910  * @pin_type: type of questioned pin
911  *
912  * Determine pin state set it on a pin.
913  *
914  * Context: Acquires pf->dplls.lock
915  * Return:
916  * * 0 - success
917  * * negative - failed to get state
918  */
919 static int
920 ice_dpll_pin_state_get(const struct dpll_pin *pin, void *pin_priv,
921 		       const struct dpll_device *dpll, void *dpll_priv,
922 		       enum dpll_pin_state *state,
923 		       struct netlink_ext_ack *extack,
924 		       enum ice_dpll_pin_type pin_type)
925 {
926 	struct ice_dpll_pin *p = pin_priv;
927 	struct ice_dpll *d = dpll_priv;
928 	struct ice_pf *pf = d->pf;
929 	int ret;
930 
931 	if (ice_dpll_is_reset(pf, extack))
932 		return -EBUSY;
933 
934 	mutex_lock(&pf->dplls.lock);
935 	ret = ice_dpll_pin_state_update(pf, p, pin_type, extack);
936 	if (ret)
937 		goto unlock;
938 	if (pin_type == ICE_DPLL_PIN_TYPE_INPUT ||
939 	    pin_type == ICE_DPLL_PIN_TYPE_OUTPUT)
940 		*state = p->state[d->dpll_idx];
941 	ret = 0;
942 unlock:
943 	mutex_unlock(&pf->dplls.lock);
944 
945 	return ret;
946 }
947 
948 /**
949  * ice_dpll_output_state_get - get output pin state on dpll device
950  * @pin: pointer to a pin
951  * @pin_priv: private data pointer passed on pin registration
952  * @dpll: registered dpll pointer
953  * @dpll_priv: private data pointer passed on dpll registration
954  * @state: on success holds state of the pin
955  * @extack: error reporting
956  *
957  * Dpll subsystem callback. Check state of a pin.
958  *
959  * Context: Calls a function which acquires pf->dplls.lock
960  * Return:
961  * * 0 - success
962  * * negative - failed to get state
963  */
964 static int
965 ice_dpll_output_state_get(const struct dpll_pin *pin, void *pin_priv,
966 			  const struct dpll_device *dpll, void *dpll_priv,
967 			  enum dpll_pin_state *state,
968 			  struct netlink_ext_ack *extack)
969 {
970 	return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
971 				      extack, ICE_DPLL_PIN_TYPE_OUTPUT);
972 }
973 
974 /**
975  * ice_dpll_input_state_get - get input pin state on dpll device
976  * @pin: pointer to a pin
977  * @pin_priv: private data pointer passed on pin registration
978  * @dpll: registered dpll pointer
979  * @dpll_priv: private data pointer passed on dpll registration
980  * @state: on success holds state of the pin
981  * @extack: error reporting
982  *
983  * Dpll subsystem callback. Check state of a input pin.
984  *
985  * Context: Calls a function which acquires pf->dplls.lock
986  * Return:
987  * * 0 - success
988  * * negative - failed to get state
989  */
990 static int
991 ice_dpll_input_state_get(const struct dpll_pin *pin, void *pin_priv,
992 			 const struct dpll_device *dpll, void *dpll_priv,
993 			 enum dpll_pin_state *state,
994 			 struct netlink_ext_ack *extack)
995 {
996 	return ice_dpll_pin_state_get(pin, pin_priv, dpll, dpll_priv, state,
997 				      extack, ICE_DPLL_PIN_TYPE_INPUT);
998 }
999 
1000 /**
1001  * ice_dpll_sma_direction_set - set direction of SMA pin
1002  * @p: pointer to a pin
1003  * @direction: requested direction of the pin
1004  * @extack: error reporting
1005  *
1006  * Wrapper for dpll subsystem callback. Set direction of a SMA pin.
1007  *
1008  * Context: Call with pf->dplls.lock held
1009  * Return:
1010  * * 0 - success
1011  * * negative - failed to get state
1012  */
1013 static int ice_dpll_sma_direction_set(struct ice_dpll_pin *p,
1014 				      enum dpll_pin_direction direction,
1015 				      struct netlink_ext_ack *extack)
1016 {
1017 	u8 data;
1018 	int ret;
1019 
1020 	if (p->direction == direction && p->active)
1021 		return 0;
1022 	ret = ice_read_sma_ctrl(&p->pf->hw, &data);
1023 	if (ret)
1024 		return ret;
1025 
1026 	switch (p->idx) {
1027 	case ICE_DPLL_PIN_SW_1_IDX:
1028 		data &= ~ICE_SMA1_MASK;
1029 		if (direction == DPLL_PIN_DIRECTION_OUTPUT)
1030 			data |= ICE_SMA1_DIR_EN;
1031 		break;
1032 	case ICE_DPLL_PIN_SW_2_IDX:
1033 		if (direction == DPLL_PIN_DIRECTION_INPUT) {
1034 			data &= ~ICE_SMA2_DIR_EN;
1035 		} else {
1036 			data &= ~ICE_SMA2_TX_EN;
1037 			data |= ICE_SMA2_DIR_EN;
1038 		}
1039 		break;
1040 	default:
1041 		return -EINVAL;
1042 	}
1043 	ret = ice_write_sma_ctrl(&p->pf->hw, data);
1044 	if (!ret)
1045 		ret = ice_dpll_pin_state_update(p->pf, p,
1046 						ICE_DPLL_PIN_TYPE_SOFTWARE,
1047 						extack);
1048 
1049 	return ret;
1050 }
1051 
1052 /**
1053  * ice_dpll_ufl_pin_state_set - set U.FL pin state on dpll device
1054  * @pin: pointer to a pin
1055  * @pin_priv: private data pointer passed on pin registration
1056  * @dpll: registered dpll pointer
1057  * @dpll_priv: private data pointer passed on dpll registration
1058  * @state: requested state of the pin
1059  * @extack: error reporting
1060  *
1061  * Dpll subsystem callback. Set the state of a pin.
1062  *
1063  * Context: Acquires and releases pf->dplls.lock
1064  * Return:
1065  * * 0 - success
1066  * * negative - error
1067  */
1068 static int
1069 ice_dpll_ufl_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
1070 			   const struct dpll_device *dpll, void *dpll_priv,
1071 			   enum dpll_pin_state state,
1072 			   struct netlink_ext_ack *extack)
1073 {
1074 	struct ice_dpll_pin *p = pin_priv, *target;
1075 	struct ice_dpll *d = dpll_priv;
1076 	enum ice_dpll_pin_type type;
1077 	struct ice_pf *pf = p->pf;
1078 	struct ice_hw *hw;
1079 	bool enable;
1080 	u8 data;
1081 	int ret;
1082 
1083 	if (ice_dpll_is_reset(pf, extack))
1084 		return -EBUSY;
1085 
1086 	mutex_lock(&pf->dplls.lock);
1087 	hw = &pf->hw;
1088 	ret = ice_read_sma_ctrl(hw, &data);
1089 	if (ret)
1090 		goto unlock;
1091 
1092 	ret = -EINVAL;
1093 	switch (p->idx) {
1094 	case ICE_DPLL_PIN_SW_1_IDX:
1095 		if (state == DPLL_PIN_STATE_CONNECTED) {
1096 			data &= ~ICE_SMA1_MASK;
1097 			enable = true;
1098 		} else if (state == DPLL_PIN_STATE_DISCONNECTED) {
1099 			data |= ICE_SMA1_TX_EN;
1100 			enable = false;
1101 		} else {
1102 			goto unlock;
1103 		}
1104 		target = p->output;
1105 		type = ICE_DPLL_PIN_TYPE_OUTPUT;
1106 		break;
1107 	case ICE_DPLL_PIN_SW_2_IDX:
1108 		if (state == DPLL_PIN_STATE_SELECTABLE) {
1109 			data |= ICE_SMA2_DIR_EN;
1110 			data &= ~ICE_SMA2_UFL2_RX_DIS;
1111 			enable = true;
1112 		} else if (state == DPLL_PIN_STATE_DISCONNECTED) {
1113 			data |= ICE_SMA2_UFL2_RX_DIS;
1114 			enable = false;
1115 		} else {
1116 			goto unlock;
1117 		}
1118 		target = p->input;
1119 		type = ICE_DPLL_PIN_TYPE_INPUT;
1120 		break;
1121 	default:
1122 		goto unlock;
1123 	}
1124 
1125 	ret = ice_write_sma_ctrl(hw, data);
1126 	if (ret)
1127 		goto unlock;
1128 	ret = ice_dpll_pin_state_update(pf, p, ICE_DPLL_PIN_TYPE_SOFTWARE,
1129 					extack);
1130 	if (ret)
1131 		goto unlock;
1132 
1133 	if (enable)
1134 		ret = ice_dpll_pin_enable(hw, target, d->dpll_idx, type, extack);
1135 	else
1136 		ret = ice_dpll_pin_disable(hw, target, type, extack);
1137 	if (!ret)
1138 		ret = ice_dpll_pin_state_update(pf, target, type, extack);
1139 
1140 unlock:
1141 	mutex_unlock(&pf->dplls.lock);
1142 
1143 	return ret;
1144 }
1145 
1146 /**
1147  * ice_dpll_sw_pin_state_get - get SW pin state
1148  * @pin: pointer to a pin
1149  * @pin_priv: private data pointer passed on pin registration
1150  * @dpll: registered dpll pointer
1151  * @dpll_priv: private data pointer passed on dpll registration
1152  * @state: on success holds state of the pin
1153  * @extack: error reporting
1154  *
1155  * Dpll subsystem callback. Check state of a SW pin.
1156  *
1157  * Context: Acquires and releases pf->dplls.lock
1158  * Return:
1159  * * 0 - success
1160  * * negative - error
1161  */
1162 static int
1163 ice_dpll_sw_pin_state_get(const struct dpll_pin *pin, void *pin_priv,
1164 			  const struct dpll_device *dpll, void *dpll_priv,
1165 			  enum dpll_pin_state *state,
1166 			  struct netlink_ext_ack *extack)
1167 {
1168 	struct ice_dpll_pin *p = pin_priv;
1169 	struct ice_dpll *d = dpll_priv;
1170 	struct ice_pf *pf = p->pf;
1171 	int ret = 0;
1172 
1173 	if (ice_dpll_is_reset(pf, extack))
1174 		return -EBUSY;
1175 	mutex_lock(&pf->dplls.lock);
1176 	if (!p->active) {
1177 		*state = DPLL_PIN_STATE_DISCONNECTED;
1178 		goto unlock;
1179 	}
1180 
1181 	if (p->direction == DPLL_PIN_DIRECTION_INPUT) {
1182 		ret = ice_dpll_pin_state_update(pf, p->input,
1183 						ICE_DPLL_PIN_TYPE_INPUT,
1184 						extack);
1185 		if (ret)
1186 			goto unlock;
1187 		*state = p->input->state[d->dpll_idx];
1188 	} else {
1189 		ret = ice_dpll_pin_state_update(pf, p->output,
1190 						ICE_DPLL_PIN_TYPE_OUTPUT,
1191 						extack);
1192 		if (ret)
1193 			goto unlock;
1194 		*state = p->output->state[d->dpll_idx];
1195 	}
1196 unlock:
1197 	mutex_unlock(&pf->dplls.lock);
1198 
1199 	return ret;
1200 }
1201 
1202 /**
1203  * ice_dpll_sma_pin_state_set - set SMA pin state on dpll device
1204  * @pin: pointer to a pin
1205  * @pin_priv: private data pointer passed on pin registration
1206  * @dpll: registered dpll pointer
1207  * @dpll_priv: private data pointer passed on dpll registration
1208  * @state: requested state of the pin
1209  * @extack: error reporting
1210  *
1211  * Dpll subsystem callback. Set state of a pin.
1212  *
1213  * Context: Acquires and releases pf->dplls.lock
1214  * Return:
1215  * * 0 - success
1216  * * negative - failed to get state
1217  */
1218 static int
1219 ice_dpll_sma_pin_state_set(const struct dpll_pin *pin, void *pin_priv,
1220 			   const struct dpll_device *dpll, void *dpll_priv,
1221 			   enum dpll_pin_state state,
1222 			   struct netlink_ext_ack *extack)
1223 {
1224 	struct ice_dpll_pin *sma = pin_priv, *target;
1225 	struct ice_dpll *d = dpll_priv;
1226 	struct ice_pf *pf = sma->pf;
1227 	enum ice_dpll_pin_type type;
1228 	bool enable;
1229 	int ret;
1230 
1231 	if (ice_dpll_is_reset(pf, extack))
1232 		return -EBUSY;
1233 
1234 	mutex_lock(&pf->dplls.lock);
1235 	if (!sma->active) {
1236 		ret = ice_dpll_sma_direction_set(sma, sma->direction, extack);
1237 		if (ret)
1238 			goto unlock;
1239 	}
1240 	if (sma->direction == DPLL_PIN_DIRECTION_INPUT) {
1241 		enable = state == DPLL_PIN_STATE_SELECTABLE;
1242 		target = sma->input;
1243 		type = ICE_DPLL_PIN_TYPE_INPUT;
1244 	} else {
1245 		enable = state == DPLL_PIN_STATE_CONNECTED;
1246 		target = sma->output;
1247 		type = ICE_DPLL_PIN_TYPE_OUTPUT;
1248 	}
1249 
1250 	if (enable)
1251 		ret = ice_dpll_pin_enable(&pf->hw, target, d->dpll_idx, type,
1252 					  extack);
1253 	else
1254 		ret = ice_dpll_pin_disable(&pf->hw, target, type, extack);
1255 	if (!ret)
1256 		ret = ice_dpll_pin_state_update(pf, target, type, extack);
1257 
1258 unlock:
1259 	mutex_unlock(&pf->dplls.lock);
1260 
1261 	return ret;
1262 }
1263 
1264 /**
1265  * ice_dpll_input_prio_get - get dpll's input prio
1266  * @pin: pointer to a pin
1267  * @pin_priv: private data pointer passed on pin registration
1268  * @dpll: registered dpll pointer
1269  * @dpll_priv: private data pointer passed on dpll registration
1270  * @prio: on success - returns input priority on dpll
1271  * @extack: error reporting
1272  *
1273  * Dpll subsystem callback. Handler for getting priority of a input pin.
1274  *
1275  * Context: Acquires pf->dplls.lock
1276  * Return:
1277  * * 0 - success
1278  * * negative - failure
1279  */
1280 static int
1281 ice_dpll_input_prio_get(const struct dpll_pin *pin, void *pin_priv,
1282 			const struct dpll_device *dpll, void *dpll_priv,
1283 			u32 *prio, struct netlink_ext_ack *extack)
1284 {
1285 	struct ice_dpll_pin *p = pin_priv;
1286 	struct ice_dpll *d = dpll_priv;
1287 	struct ice_pf *pf = d->pf;
1288 
1289 	mutex_lock(&pf->dplls.lock);
1290 	*prio = d->input_prio[p->idx];
1291 	mutex_unlock(&pf->dplls.lock);
1292 
1293 	return 0;
1294 }
1295 
1296 /**
1297  * ice_dpll_input_prio_set - set dpll input prio
1298  * @pin: pointer to a pin
1299  * @pin_priv: private data pointer passed on pin registration
1300  * @dpll: registered dpll pointer
1301  * @dpll_priv: private data pointer passed on dpll registration
1302  * @prio: input priority to be set on dpll
1303  * @extack: error reporting
1304  *
1305  * Dpll subsystem callback. Handler for setting priority of a input pin.
1306  *
1307  * Context: Acquires pf->dplls.lock
1308  * Return:
1309  * * 0 - success
1310  * * negative - failure
1311  */
1312 static int
1313 ice_dpll_input_prio_set(const struct dpll_pin *pin, void *pin_priv,
1314 			const struct dpll_device *dpll, void *dpll_priv,
1315 			u32 prio, struct netlink_ext_ack *extack)
1316 {
1317 	struct ice_dpll_pin *p = pin_priv;
1318 	struct ice_dpll *d = dpll_priv;
1319 	struct ice_pf *pf = d->pf;
1320 	int ret;
1321 
1322 	if (ice_dpll_is_reset(pf, extack))
1323 		return -EBUSY;
1324 
1325 	mutex_lock(&pf->dplls.lock);
1326 	ret = ice_dpll_hw_input_prio_set(pf, d, p, prio, extack);
1327 	mutex_unlock(&pf->dplls.lock);
1328 
1329 	return ret;
1330 }
1331 
1332 static int
1333 ice_dpll_sw_input_prio_get(const struct dpll_pin *pin, void *pin_priv,
1334 			   const struct dpll_device *dpll, void *dpll_priv,
1335 			   u32 *prio, struct netlink_ext_ack *extack)
1336 {
1337 	struct ice_dpll_pin *p = pin_priv;
1338 	struct ice_dpll *d = dpll_priv;
1339 	struct ice_pf *pf = d->pf;
1340 
1341 	mutex_lock(&pf->dplls.lock);
1342 	if (p->input && p->direction == DPLL_PIN_DIRECTION_INPUT)
1343 		*prio = d->input_prio[p->input->idx];
1344 	else
1345 		*prio = ICE_DPLL_PIN_PRIO_OUTPUT;
1346 	mutex_unlock(&pf->dplls.lock);
1347 
1348 	return 0;
1349 }
1350 
1351 static int
1352 ice_dpll_sw_input_prio_set(const struct dpll_pin *pin, void *pin_priv,
1353 			   const struct dpll_device *dpll, void *dpll_priv,
1354 			   u32 prio, struct netlink_ext_ack *extack)
1355 {
1356 	struct ice_dpll_pin *p = pin_priv;
1357 	struct ice_dpll *d = dpll_priv;
1358 	struct ice_pf *pf = d->pf;
1359 	int ret;
1360 
1361 	if (!p->input || p->direction != DPLL_PIN_DIRECTION_INPUT)
1362 		return -EINVAL;
1363 	if (ice_dpll_is_reset(pf, extack))
1364 		return -EBUSY;
1365 
1366 	mutex_lock(&pf->dplls.lock);
1367 	ret = ice_dpll_hw_input_prio_set(pf, d, p->input, prio, extack);
1368 	mutex_unlock(&pf->dplls.lock);
1369 
1370 	return ret;
1371 }
1372 
1373 /**
1374  * ice_dpll_input_direction - callback for get input pin direction
1375  * @pin: pointer to a pin
1376  * @pin_priv: private data pointer passed on pin registration
1377  * @dpll: registered dpll pointer
1378  * @dpll_priv: private data pointer passed on dpll registration
1379  * @direction: holds input pin direction
1380  * @extack: error reporting
1381  *
1382  * Dpll subsystem callback. Handler for getting direction of a input pin.
1383  *
1384  * Return:
1385  * * 0 - success
1386  */
1387 static int
1388 ice_dpll_input_direction(const struct dpll_pin *pin, void *pin_priv,
1389 			 const struct dpll_device *dpll, void *dpll_priv,
1390 			 enum dpll_pin_direction *direction,
1391 			 struct netlink_ext_ack *extack)
1392 {
1393 	*direction = DPLL_PIN_DIRECTION_INPUT;
1394 
1395 	return 0;
1396 }
1397 
1398 /**
1399  * ice_dpll_output_direction - callback for get output pin direction
1400  * @pin: pointer to a pin
1401  * @pin_priv: private data pointer passed on pin registration
1402  * @dpll: registered dpll pointer
1403  * @dpll_priv: private data pointer passed on dpll registration
1404  * @direction: holds output pin direction
1405  * @extack: error reporting
1406  *
1407  * Dpll subsystem callback. Handler for getting direction of an output pin.
1408  *
1409  * Return:
1410  * * 0 - success
1411  */
1412 static int
1413 ice_dpll_output_direction(const struct dpll_pin *pin, void *pin_priv,
1414 			  const struct dpll_device *dpll, void *dpll_priv,
1415 			  enum dpll_pin_direction *direction,
1416 			  struct netlink_ext_ack *extack)
1417 {
1418 	*direction = DPLL_PIN_DIRECTION_OUTPUT;
1419 
1420 	return 0;
1421 }
1422 
1423 /**
1424  * ice_dpll_pin_sma_direction_set - callback for set SMA pin direction
1425  * @pin: pointer to a pin
1426  * @pin_priv: private data pointer passed on pin registration
1427  * @dpll: registered dpll pointer
1428  * @dpll_priv: private data pointer passed on dpll registration
1429  * @direction: requested pin direction
1430  * @extack: error reporting
1431  *
1432  * Dpll subsystem callback. Handler for setting direction of a SMA pin.
1433  *
1434  * Context: Acquires and releases pf->dplls.lock
1435  * Return:
1436  * * 0 - success
1437  * * negative - error
1438  */
1439 static int
1440 ice_dpll_pin_sma_direction_set(const struct dpll_pin *pin, void *pin_priv,
1441 			       const struct dpll_device *dpll, void *dpll_priv,
1442 			       enum dpll_pin_direction direction,
1443 			       struct netlink_ext_ack *extack)
1444 {
1445 	struct ice_dpll_pin *p = pin_priv;
1446 	struct ice_pf *pf = p->pf;
1447 	int ret;
1448 
1449 	if (ice_dpll_is_reset(pf, extack))
1450 		return -EBUSY;
1451 
1452 	mutex_lock(&pf->dplls.lock);
1453 	ret = ice_dpll_sma_direction_set(p, direction, extack);
1454 	mutex_unlock(&pf->dplls.lock);
1455 
1456 	return ret;
1457 }
1458 
1459 /**
1460  * ice_dpll_pin_sw_direction_get - callback for get SW pin direction
1461  * @pin: pointer to a pin
1462  * @pin_priv: private data pointer passed on pin registration
1463  * @dpll: registered dpll pointer
1464  * @dpll_priv: private data pointer passed on dpll registration
1465  * @direction: on success holds pin direction
1466  * @extack: error reporting
1467  *
1468  * Dpll subsystem callback. Handler for getting direction of a SMA pin.
1469  *
1470  * Context: Acquires and releases pf->dplls.lock
1471  * Return:
1472  * * 0 - success
1473  * * negative - error
1474  */
1475 static int
1476 ice_dpll_pin_sw_direction_get(const struct dpll_pin *pin, void *pin_priv,
1477 			      const struct dpll_device *dpll, void *dpll_priv,
1478 			      enum dpll_pin_direction *direction,
1479 			      struct netlink_ext_ack *extack)
1480 {
1481 	struct ice_dpll_pin *p = pin_priv;
1482 	struct ice_pf *pf = p->pf;
1483 
1484 	if (ice_dpll_is_reset(pf, extack))
1485 		return -EBUSY;
1486 	mutex_lock(&pf->dplls.lock);
1487 	*direction = p->direction;
1488 	mutex_unlock(&pf->dplls.lock);
1489 
1490 	return 0;
1491 }
1492 
1493 /**
1494  * ice_dpll_pin_phase_adjust_get - callback for get pin phase adjust value
1495  * @pin: pointer to a pin
1496  * @pin_priv: private data pointer passed on pin registration
1497  * @dpll: registered dpll pointer
1498  * @dpll_priv: private data pointer passed on dpll registration
1499  * @phase_adjust: on success holds pin phase_adjust value
1500  * @extack: error reporting
1501  *
1502  * Dpll subsystem callback. Handler for getting phase adjust value of a pin.
1503  *
1504  * Context: Acquires pf->dplls.lock
1505  * Return:
1506  * * 0 - success
1507  * * negative - error
1508  */
1509 static int
1510 ice_dpll_pin_phase_adjust_get(const struct dpll_pin *pin, void *pin_priv,
1511 			      const struct dpll_device *dpll, void *dpll_priv,
1512 			      s32 *phase_adjust,
1513 			      struct netlink_ext_ack *extack)
1514 {
1515 	struct ice_dpll_pin *p = pin_priv;
1516 	struct ice_pf *pf = p->pf;
1517 
1518 	mutex_lock(&pf->dplls.lock);
1519 	*phase_adjust = p->phase_adjust;
1520 	mutex_unlock(&pf->dplls.lock);
1521 
1522 	return 0;
1523 }
1524 
1525 /**
1526  * ice_dpll_pin_phase_adjust_set - helper for setting a pin phase adjust value
1527  * @pin: pointer to a pin
1528  * @pin_priv: private data pointer passed on pin registration
1529  * @dpll: registered dpll pointer
1530  * @dpll_priv: private data pointer passed on dpll registration
1531  * @phase_adjust: phase_adjust to be set
1532  * @extack: error reporting
1533  * @type: type of a pin
1534  *
1535  * Helper for dpll subsystem callback. Handler for setting phase adjust value
1536  * of a pin.
1537  *
1538  * Context: Acquires pf->dplls.lock
1539  * Return:
1540  * * 0 - success
1541  * * negative - error
1542  */
1543 static int
1544 ice_dpll_pin_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
1545 			      const struct dpll_device *dpll, void *dpll_priv,
1546 			      s32 phase_adjust,
1547 			      struct netlink_ext_ack *extack,
1548 			      enum ice_dpll_pin_type type)
1549 {
1550 	struct ice_dpll_pin *p = pin_priv;
1551 	struct ice_dpll *d = dpll_priv;
1552 	struct ice_pf *pf = d->pf;
1553 	u8 flag, flags_en = 0;
1554 	int ret;
1555 
1556 	if (ice_dpll_is_reset(pf, extack))
1557 		return -EBUSY;
1558 
1559 	mutex_lock(&pf->dplls.lock);
1560 	switch (type) {
1561 	case ICE_DPLL_PIN_TYPE_INPUT:
1562 		flag = ICE_AQC_SET_CGU_IN_CFG_FLG1_UPDATE_DELAY;
1563 		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)
1564 			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
1565 		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
1566 			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
1567 		ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, flag, flags_en,
1568 					       0, phase_adjust);
1569 		break;
1570 	case ICE_DPLL_PIN_TYPE_OUTPUT:
1571 		flag = ICE_AQC_SET_CGU_OUT_CFG_UPDATE_PHASE;
1572 		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_OUT_EN)
1573 			flag |= ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
1574 		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)
1575 			flag |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
1576 		ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flag, 0, 0,
1577 						phase_adjust);
1578 		break;
1579 	default:
1580 		ret = -EINVAL;
1581 	}
1582 	if (!ret)
1583 		p->phase_adjust = phase_adjust;
1584 	mutex_unlock(&pf->dplls.lock);
1585 	if (ret)
1586 		NL_SET_ERR_MSG_FMT(extack,
1587 				   "err:%d %s failed to set pin phase_adjust:%d for pin:%u on dpll:%u",
1588 				   ret,
1589 				   ice_aq_str(pf->hw.adminq.sq_last_status),
1590 				   phase_adjust, p->idx, d->dpll_idx);
1591 
1592 	return ret;
1593 }
1594 
1595 /**
1596  * ice_dpll_input_phase_adjust_set - callback for set input pin phase adjust
1597  * @pin: pointer to a pin
1598  * @pin_priv: private data pointer passed on pin registration
1599  * @dpll: registered dpll pointer
1600  * @dpll_priv: private data pointer passed on dpll registration
1601  * @phase_adjust: phase_adjust to be set
1602  * @extack: error reporting
1603  *
1604  * Dpll subsystem callback. Wraps a handler for setting phase adjust on input
1605  * pin.
1606  *
1607  * Context: Calls a function which acquires and releases pf->dplls.lock
1608  * Return:
1609  * * 0 - success
1610  * * negative - error
1611  */
1612 static int
1613 ice_dpll_input_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
1614 				const struct dpll_device *dpll, void *dpll_priv,
1615 				s32 phase_adjust,
1616 				struct netlink_ext_ack *extack)
1617 {
1618 	return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
1619 					     phase_adjust, extack,
1620 					     ICE_DPLL_PIN_TYPE_INPUT);
1621 }
1622 
1623 /**
1624  * ice_dpll_output_phase_adjust_set - callback for set output pin phase adjust
1625  * @pin: pointer to a pin
1626  * @pin_priv: private data pointer passed on pin registration
1627  * @dpll: registered dpll pointer
1628  * @dpll_priv: private data pointer passed on dpll registration
1629  * @phase_adjust: phase_adjust to be set
1630  * @extack: error reporting
1631  *
1632  * Dpll subsystem callback. Wraps a handler for setting phase adjust on output
1633  * pin.
1634  *
1635  * Context: Calls a function which acquires pf->dplls.lock
1636  * Return:
1637  * * 0 - success
1638  * * negative - error
1639  */
1640 static int
1641 ice_dpll_output_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
1642 				 const struct dpll_device *dpll, void *dpll_priv,
1643 				 s32 phase_adjust,
1644 				 struct netlink_ext_ack *extack)
1645 {
1646 	return ice_dpll_pin_phase_adjust_set(pin, pin_priv, dpll, dpll_priv,
1647 					     phase_adjust, extack,
1648 					     ICE_DPLL_PIN_TYPE_OUTPUT);
1649 }
1650 
1651 /**
1652  * ice_dpll_sw_phase_adjust_get - callback for get SW pin phase adjust
1653  * @pin: pointer to a pin
1654  * @pin_priv: private data pointer passed on pin registration
1655  * @dpll: registered dpll pointer
1656  * @dpll_priv: private data pointer passed on dpll registration
1657  * @phase_adjust: on success holds phase adjust value
1658  * @extack: error reporting
1659  *
1660  * Dpll subsystem callback. Wraps a handler for getting phase adjust on sw
1661  * pin.
1662  *
1663  * Context: Calls a function which acquires and releases pf->dplls.lock
1664  * Return:
1665  * * 0 - success
1666  * * negative - error
1667  */
1668 static int
1669 ice_dpll_sw_phase_adjust_get(const struct dpll_pin *pin, void *pin_priv,
1670 			     const struct dpll_device *dpll, void *dpll_priv,
1671 			     s32 *phase_adjust,
1672 			     struct netlink_ext_ack *extack)
1673 {
1674 	struct ice_dpll_pin *p = pin_priv;
1675 
1676 	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
1677 		return ice_dpll_pin_phase_adjust_get(p->input->pin, p->input,
1678 						     dpll, dpll_priv,
1679 						     phase_adjust, extack);
1680 	else
1681 		return ice_dpll_pin_phase_adjust_get(p->output->pin, p->output,
1682 						     dpll, dpll_priv,
1683 						     phase_adjust, extack);
1684 }
1685 
1686 /**
1687  * ice_dpll_sw_phase_adjust_set - callback for set SW pin phase adjust value
1688  * @pin: pointer to a pin
1689  * @pin_priv: private data pointer passed on pin registration
1690  * @dpll: registered dpll pointer
1691  * @dpll_priv: private data pointer passed on dpll registration
1692  * @phase_adjust: phase_adjust to be set
1693  * @extack: error reporting
1694  *
1695  * Dpll subsystem callback. Wraps a handler for setting phase adjust on output
1696  * pin.
1697  *
1698  * Context: Calls a function which acquires and releases pf->dplls.lock
1699  * Return:
1700  * * 0 - success
1701  * * negative - error
1702  */
1703 static int
1704 ice_dpll_sw_phase_adjust_set(const struct dpll_pin *pin, void *pin_priv,
1705 			     const struct dpll_device *dpll, void *dpll_priv,
1706 			     s32 phase_adjust,
1707 			     struct netlink_ext_ack *extack)
1708 {
1709 	struct ice_dpll_pin *p = pin_priv;
1710 
1711 	if (!p->active) {
1712 		NL_SET_ERR_MSG(extack, "pin is not active");
1713 		return -EINVAL;
1714 	}
1715 	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
1716 		return ice_dpll_pin_phase_adjust_set(p->input->pin, p->input,
1717 						     dpll, dpll_priv,
1718 						     phase_adjust, extack,
1719 						     ICE_DPLL_PIN_TYPE_INPUT);
1720 	else
1721 		return ice_dpll_pin_phase_adjust_set(p->output->pin, p->output,
1722 						     dpll, dpll_priv,
1723 						     phase_adjust, extack,
1724 						     ICE_DPLL_PIN_TYPE_OUTPUT);
1725 }
1726 
1727 #define ICE_DPLL_PHASE_OFFSET_DIVIDER	100
1728 #define ICE_DPLL_PHASE_OFFSET_FACTOR		\
1729 	(DPLL_PHASE_OFFSET_DIVIDER / ICE_DPLL_PHASE_OFFSET_DIVIDER)
1730 /**
1731  * ice_dpll_phase_offset_get - callback for get dpll phase shift value
1732  * @pin: pointer to a pin
1733  * @pin_priv: private data pointer passed on pin registration
1734  * @dpll: registered dpll pointer
1735  * @dpll_priv: private data pointer passed on dpll registration
1736  * @phase_offset: on success holds pin phase_offset value
1737  * @extack: error reporting
1738  *
1739  * Dpll subsystem callback. Handler for getting phase shift value between
1740  * dpll's input and output.
1741  *
1742  * Context: Acquires pf->dplls.lock
1743  * Return:
1744  * * 0 - success
1745  * * negative - error
1746  */
1747 static int
1748 ice_dpll_phase_offset_get(const struct dpll_pin *pin, void *pin_priv,
1749 			  const struct dpll_device *dpll, void *dpll_priv,
1750 			  s64 *phase_offset, struct netlink_ext_ack *extack)
1751 {
1752 	struct ice_dpll_pin *p = pin_priv;
1753 	struct ice_dpll *d = dpll_priv;
1754 	struct ice_pf *pf = d->pf;
1755 
1756 	mutex_lock(&pf->dplls.lock);
1757 	if (d->active_input == pin || (p->input &&
1758 				       d->active_input == p->input->pin))
1759 		*phase_offset = d->phase_offset * ICE_DPLL_PHASE_OFFSET_FACTOR;
1760 	else
1761 		*phase_offset = 0;
1762 	mutex_unlock(&pf->dplls.lock);
1763 
1764 	return 0;
1765 }
1766 
1767 /**
1768  * ice_dpll_output_esync_set - callback for setting embedded sync
1769  * @pin: pointer to a pin
1770  * @pin_priv: private data pointer passed on pin registration
1771  * @dpll: registered dpll pointer
1772  * @dpll_priv: private data pointer passed on dpll registration
1773  * @freq: requested embedded sync frequency
1774  * @extack: error reporting
1775  *
1776  * Dpll subsystem callback. Handler for setting embedded sync frequency value
1777  * on output pin.
1778  *
1779  * Context: Acquires pf->dplls.lock
1780  * Return:
1781  * * 0 - success
1782  * * negative - error
1783  */
1784 static int
1785 ice_dpll_output_esync_set(const struct dpll_pin *pin, void *pin_priv,
1786 			  const struct dpll_device *dpll, void *dpll_priv,
1787 			  u64 freq, struct netlink_ext_ack *extack)
1788 {
1789 	struct ice_dpll_pin *p = pin_priv;
1790 	struct ice_dpll *d = dpll_priv;
1791 	struct ice_pf *pf = d->pf;
1792 	u8 flags = 0;
1793 	int ret;
1794 
1795 	if (ice_dpll_is_reset(pf, extack))
1796 		return -EBUSY;
1797 	mutex_lock(&pf->dplls.lock);
1798 	if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_OUT_EN)
1799 		flags = ICE_AQC_SET_CGU_OUT_CFG_OUT_EN;
1800 	if (freq == DPLL_PIN_FREQUENCY_1_HZ) {
1801 		if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN) {
1802 			ret = 0;
1803 		} else {
1804 			flags |= ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
1805 			ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flags,
1806 							0, 0, 0);
1807 		}
1808 	} else {
1809 		if (!(p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN)) {
1810 			ret = 0;
1811 		} else {
1812 			flags &= ~ICE_AQC_SET_CGU_OUT_CFG_ESYNC_EN;
1813 			ret = ice_aq_set_output_pin_cfg(&pf->hw, p->idx, flags,
1814 							0, 0, 0);
1815 		}
1816 	}
1817 	mutex_unlock(&pf->dplls.lock);
1818 
1819 	return ret;
1820 }
1821 
1822 /**
1823  * ice_dpll_output_esync_get - callback for getting embedded sync config
1824  * @pin: pointer to a pin
1825  * @pin_priv: private data pointer passed on pin registration
1826  * @dpll: registered dpll pointer
1827  * @dpll_priv: private data pointer passed on dpll registration
1828  * @esync: on success holds embedded sync pin properties
1829  * @extack: error reporting
1830  *
1831  * Dpll subsystem callback. Handler for getting embedded sync frequency value
1832  * and capabilities on output pin.
1833  *
1834  * Context: Acquires pf->dplls.lock
1835  * Return:
1836  * * 0 - success
1837  * * negative - error
1838  */
1839 static int
1840 ice_dpll_output_esync_get(const struct dpll_pin *pin, void *pin_priv,
1841 			  const struct dpll_device *dpll, void *dpll_priv,
1842 			  struct dpll_pin_esync *esync,
1843 			  struct netlink_ext_ack *extack)
1844 {
1845 	struct ice_dpll_pin *p = pin_priv;
1846 	struct ice_dpll *d = dpll_priv;
1847 	struct ice_pf *pf = d->pf;
1848 
1849 	if (ice_dpll_is_reset(pf, extack))
1850 		return -EBUSY;
1851 	mutex_lock(&pf->dplls.lock);
1852 	if (!(p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_ABILITY) ||
1853 	    p->freq != DPLL_PIN_FREQUENCY_10_MHZ) {
1854 		mutex_unlock(&pf->dplls.lock);
1855 		return -EOPNOTSUPP;
1856 	}
1857 	esync->range = ice_esync_range;
1858 	esync->range_num = ARRAY_SIZE(ice_esync_range);
1859 	if (p->flags[0] & ICE_AQC_GET_CGU_OUT_CFG_ESYNC_EN) {
1860 		esync->freq = DPLL_PIN_FREQUENCY_1_HZ;
1861 		esync->pulse = ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT;
1862 	} else {
1863 		esync->freq = 0;
1864 		esync->pulse = 0;
1865 	}
1866 	mutex_unlock(&pf->dplls.lock);
1867 
1868 	return 0;
1869 }
1870 
1871 /**
1872  * ice_dpll_input_esync_set - callback for setting embedded sync
1873  * @pin: pointer to a pin
1874  * @pin_priv: private data pointer passed on pin registration
1875  * @dpll: registered dpll pointer
1876  * @dpll_priv: private data pointer passed on dpll registration
1877  * @freq: requested embedded sync frequency
1878  * @extack: error reporting
1879  *
1880  * Dpll subsystem callback. Handler for setting embedded sync frequency value
1881  * on input pin.
1882  *
1883  * Context: Acquires pf->dplls.lock
1884  * Return:
1885  * * 0 - success
1886  * * negative - error
1887  */
1888 static int
1889 ice_dpll_input_esync_set(const struct dpll_pin *pin, void *pin_priv,
1890 			 const struct dpll_device *dpll, void *dpll_priv,
1891 			 u64 freq, struct netlink_ext_ack *extack)
1892 {
1893 	struct ice_dpll_pin *p = pin_priv;
1894 	struct ice_dpll *d = dpll_priv;
1895 	struct ice_pf *pf = d->pf;
1896 	u8 flags_en = 0;
1897 	int ret;
1898 
1899 	if (ice_dpll_is_reset(pf, extack))
1900 		return -EBUSY;
1901 	mutex_lock(&pf->dplls.lock);
1902 	if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_INPUT_EN)
1903 		flags_en = ICE_AQC_SET_CGU_IN_CFG_FLG2_INPUT_EN;
1904 	if (freq == DPLL_PIN_FREQUENCY_1_HZ) {
1905 		if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN) {
1906 			ret = 0;
1907 		} else {
1908 			flags_en |= ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
1909 			ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, 0,
1910 						       flags_en, 0, 0);
1911 		}
1912 	} else {
1913 		if (!(p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN)) {
1914 			ret = 0;
1915 		} else {
1916 			flags_en &= ~ICE_AQC_SET_CGU_IN_CFG_FLG2_ESYNC_EN;
1917 			ret = ice_aq_set_input_pin_cfg(&pf->hw, p->idx, 0,
1918 						       flags_en, 0, 0);
1919 		}
1920 	}
1921 	mutex_unlock(&pf->dplls.lock);
1922 
1923 	return ret;
1924 }
1925 
1926 /**
1927  * ice_dpll_input_esync_get - callback for getting embedded sync config
1928  * @pin: pointer to a pin
1929  * @pin_priv: private data pointer passed on pin registration
1930  * @dpll: registered dpll pointer
1931  * @dpll_priv: private data pointer passed on dpll registration
1932  * @esync: on success holds embedded sync pin properties
1933  * @extack: error reporting
1934  *
1935  * Dpll subsystem callback. Handler for getting embedded sync frequency value
1936  * and capabilities on input pin.
1937  *
1938  * Context: Acquires pf->dplls.lock
1939  * Return:
1940  * * 0 - success
1941  * * negative - error
1942  */
1943 static int
1944 ice_dpll_input_esync_get(const struct dpll_pin *pin, void *pin_priv,
1945 			 const struct dpll_device *dpll, void *dpll_priv,
1946 			 struct dpll_pin_esync *esync,
1947 			 struct netlink_ext_ack *extack)
1948 {
1949 	struct ice_dpll_pin *p = pin_priv;
1950 	struct ice_dpll *d = dpll_priv;
1951 	struct ice_pf *pf = d->pf;
1952 
1953 	if (ice_dpll_is_reset(pf, extack))
1954 		return -EBUSY;
1955 	mutex_lock(&pf->dplls.lock);
1956 	if (!(p->status & ICE_AQC_GET_CGU_IN_CFG_STATUS_ESYNC_CAP) ||
1957 	    p->freq != DPLL_PIN_FREQUENCY_10_MHZ) {
1958 		mutex_unlock(&pf->dplls.lock);
1959 		return -EOPNOTSUPP;
1960 	}
1961 	esync->range = ice_esync_range;
1962 	esync->range_num = ARRAY_SIZE(ice_esync_range);
1963 	if (p->flags[0] & ICE_AQC_GET_CGU_IN_CFG_FLG2_ESYNC_EN) {
1964 		esync->freq = DPLL_PIN_FREQUENCY_1_HZ;
1965 		esync->pulse = ICE_DPLL_PIN_ESYNC_PULSE_HIGH_PERCENT;
1966 	} else {
1967 		esync->freq = 0;
1968 		esync->pulse = 0;
1969 	}
1970 	mutex_unlock(&pf->dplls.lock);
1971 
1972 	return 0;
1973 }
1974 
1975 /**
1976  * ice_dpll_sw_esync_set - callback for setting embedded sync on SW pin
1977  * @pin: pointer to a pin
1978  * @pin_priv: private data pointer passed on pin registration
1979  * @dpll: registered dpll pointer
1980  * @dpll_priv: private data pointer passed on dpll registration
1981  * @freq: requested embedded sync frequency
1982  * @extack: error reporting
1983  *
1984  * Dpll subsystem callback. Handler for setting embedded sync frequency value
1985  * on SW pin.
1986  *
1987  * Context: Calls a function which acquires and releases pf->dplls.lock
1988  * Return:
1989  * * 0 - success
1990  * * negative - error
1991  */
1992 static int
1993 ice_dpll_sw_esync_set(const struct dpll_pin *pin, void *pin_priv,
1994 		      const struct dpll_device *dpll, void *dpll_priv,
1995 		      u64 freq, struct netlink_ext_ack *extack)
1996 {
1997 	struct ice_dpll_pin *p = pin_priv;
1998 
1999 	if (!p->active) {
2000 		NL_SET_ERR_MSG(extack, "pin is not active");
2001 		return -EINVAL;
2002 	}
2003 	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
2004 		return ice_dpll_input_esync_set(p->input->pin, p->input, dpll,
2005 						dpll_priv, freq, extack);
2006 	else
2007 		return ice_dpll_output_esync_set(p->output->pin, p->output,
2008 						 dpll, dpll_priv, freq, extack);
2009 }
2010 
2011 /**
2012  * ice_dpll_sw_esync_get - callback for getting embedded sync on SW pin
2013  * @pin: pointer to a pin
2014  * @pin_priv: private data pointer passed on pin registration
2015  * @dpll: registered dpll pointer
2016  * @dpll_priv: private data pointer passed on dpll registration
2017  * @esync: on success holds embedded sync frequency and properties
2018  * @extack: error reporting
2019  *
2020  * Dpll subsystem callback. Handler for getting embedded sync frequency value
2021  * of SW pin.
2022  *
2023  * Context: Calls a function which acquires and releases pf->dplls.lock
2024  * Return:
2025  * * 0 - success
2026  * * negative - error
2027  */
2028 static int
2029 ice_dpll_sw_esync_get(const struct dpll_pin *pin, void *pin_priv,
2030 		      const struct dpll_device *dpll, void *dpll_priv,
2031 		      struct dpll_pin_esync *esync,
2032 		      struct netlink_ext_ack *extack)
2033 {
2034 	struct ice_dpll_pin *p = pin_priv;
2035 
2036 	if (p->direction == DPLL_PIN_DIRECTION_INPUT)
2037 		return ice_dpll_input_esync_get(p->input->pin, p->input, dpll,
2038 						dpll_priv, esync, extack);
2039 	else
2040 		return ice_dpll_output_esync_get(p->output->pin, p->output,
2041 						 dpll, dpll_priv, esync,
2042 						 extack);
2043 }
2044 
2045 /**
2046  * ice_dpll_rclk_state_on_pin_set - set a state on rclk pin
2047  * @pin: pointer to a pin
2048  * @pin_priv: private data pointer passed on pin registration
2049  * @parent_pin: pin parent pointer
2050  * @parent_pin_priv: parent private data pointer passed on pin registration
2051  * @state: state to be set on pin
2052  * @extack: error reporting
2053  *
2054  * Dpll subsystem callback, set a state of a rclk pin on a parent pin
2055  *
2056  * Context: Acquires pf->dplls.lock
2057  * Return:
2058  * * 0 - success
2059  * * negative - failure
2060  */
2061 static int
2062 ice_dpll_rclk_state_on_pin_set(const struct dpll_pin *pin, void *pin_priv,
2063 			       const struct dpll_pin *parent_pin,
2064 			       void *parent_pin_priv,
2065 			       enum dpll_pin_state state,
2066 			       struct netlink_ext_ack *extack)
2067 {
2068 	struct ice_dpll_pin *p = pin_priv, *parent = parent_pin_priv;
2069 	bool enable = state == DPLL_PIN_STATE_CONNECTED;
2070 	struct ice_pf *pf = p->pf;
2071 	int ret = -EINVAL;
2072 	u32 hw_idx;
2073 
2074 	if (ice_dpll_is_reset(pf, extack))
2075 		return -EBUSY;
2076 
2077 	mutex_lock(&pf->dplls.lock);
2078 	hw_idx = parent->idx - pf->dplls.base_rclk_idx;
2079 	if (hw_idx >= pf->dplls.num_inputs)
2080 		goto unlock;
2081 
2082 	if ((enable && p->state[hw_idx] == DPLL_PIN_STATE_CONNECTED) ||
2083 	    (!enable && p->state[hw_idx] == DPLL_PIN_STATE_DISCONNECTED)) {
2084 		NL_SET_ERR_MSG_FMT(extack,
2085 				   "pin:%u state:%u on parent:%u already set",
2086 				   p->idx, state, parent->idx);
2087 		goto unlock;
2088 	}
2089 	ret = ice_aq_set_phy_rec_clk_out(&pf->hw, hw_idx, enable,
2090 					 &p->freq);
2091 	if (ret)
2092 		NL_SET_ERR_MSG_FMT(extack,
2093 				   "err:%d %s failed to set pin state:%u for pin:%u on parent:%u",
2094 				   ret,
2095 				   ice_aq_str(pf->hw.adminq.sq_last_status),
2096 				   state, p->idx, parent->idx);
2097 unlock:
2098 	mutex_unlock(&pf->dplls.lock);
2099 
2100 	return ret;
2101 }
2102 
2103 /**
2104  * ice_dpll_rclk_state_on_pin_get - get a state of rclk pin
2105  * @pin: pointer to a pin
2106  * @pin_priv: private data pointer passed on pin registration
2107  * @parent_pin: pin parent pointer
2108  * @parent_pin_priv: pin parent priv data pointer passed on pin registration
2109  * @state: on success holds pin state on parent pin
2110  * @extack: error reporting
2111  *
2112  * dpll subsystem callback, get a state of a recovered clock pin.
2113  *
2114  * Context: Acquires pf->dplls.lock
2115  * Return:
2116  * * 0 - success
2117  * * negative - failure
2118  */
2119 static int
2120 ice_dpll_rclk_state_on_pin_get(const struct dpll_pin *pin, void *pin_priv,
2121 			       const struct dpll_pin *parent_pin,
2122 			       void *parent_pin_priv,
2123 			       enum dpll_pin_state *state,
2124 			       struct netlink_ext_ack *extack)
2125 {
2126 	struct ice_dpll_pin *p = pin_priv, *parent = parent_pin_priv;
2127 	struct ice_pf *pf = p->pf;
2128 	int ret = -EINVAL;
2129 	u32 hw_idx;
2130 
2131 	if (ice_dpll_is_reset(pf, extack))
2132 		return -EBUSY;
2133 
2134 	mutex_lock(&pf->dplls.lock);
2135 	hw_idx = parent->idx - pf->dplls.base_rclk_idx;
2136 	if (hw_idx >= pf->dplls.num_inputs)
2137 		goto unlock;
2138 
2139 	ret = ice_dpll_pin_state_update(pf, p, ICE_DPLL_PIN_TYPE_RCLK_INPUT,
2140 					extack);
2141 	if (ret)
2142 		goto unlock;
2143 
2144 	*state = p->state[hw_idx];
2145 	ret = 0;
2146 unlock:
2147 	mutex_unlock(&pf->dplls.lock);
2148 
2149 	return ret;
2150 }
2151 
2152 static const struct dpll_pin_ops ice_dpll_rclk_ops = {
2153 	.state_on_pin_set = ice_dpll_rclk_state_on_pin_set,
2154 	.state_on_pin_get = ice_dpll_rclk_state_on_pin_get,
2155 	.direction_get = ice_dpll_input_direction,
2156 };
2157 
2158 static const struct dpll_pin_ops ice_dpll_pin_sma_ops = {
2159 	.state_on_dpll_set = ice_dpll_sma_pin_state_set,
2160 	.state_on_dpll_get = ice_dpll_sw_pin_state_get,
2161 	.direction_get = ice_dpll_pin_sw_direction_get,
2162 	.direction_set = ice_dpll_pin_sma_direction_set,
2163 	.prio_get = ice_dpll_sw_input_prio_get,
2164 	.prio_set = ice_dpll_sw_input_prio_set,
2165 	.frequency_get = ice_dpll_sw_pin_frequency_get,
2166 	.frequency_set = ice_dpll_sw_pin_frequency_set,
2167 	.phase_adjust_get = ice_dpll_sw_phase_adjust_get,
2168 	.phase_adjust_set = ice_dpll_sw_phase_adjust_set,
2169 	.phase_offset_get = ice_dpll_phase_offset_get,
2170 	.esync_set = ice_dpll_sw_esync_set,
2171 	.esync_get = ice_dpll_sw_esync_get,
2172 };
2173 
2174 static const struct dpll_pin_ops ice_dpll_pin_ufl_ops = {
2175 	.state_on_dpll_set = ice_dpll_ufl_pin_state_set,
2176 	.state_on_dpll_get = ice_dpll_sw_pin_state_get,
2177 	.direction_get = ice_dpll_pin_sw_direction_get,
2178 	.frequency_get = ice_dpll_sw_pin_frequency_get,
2179 	.frequency_set = ice_dpll_sw_pin_frequency_set,
2180 	.esync_set = ice_dpll_sw_esync_set,
2181 	.esync_get = ice_dpll_sw_esync_get,
2182 	.phase_adjust_get = ice_dpll_sw_phase_adjust_get,
2183 	.phase_adjust_set = ice_dpll_sw_phase_adjust_set,
2184 	.phase_offset_get = ice_dpll_phase_offset_get,
2185 };
2186 
2187 static const struct dpll_pin_ops ice_dpll_input_ops = {
2188 	.frequency_get = ice_dpll_input_frequency_get,
2189 	.frequency_set = ice_dpll_input_frequency_set,
2190 	.state_on_dpll_get = ice_dpll_input_state_get,
2191 	.state_on_dpll_set = ice_dpll_input_state_set,
2192 	.prio_get = ice_dpll_input_prio_get,
2193 	.prio_set = ice_dpll_input_prio_set,
2194 	.direction_get = ice_dpll_input_direction,
2195 	.phase_adjust_get = ice_dpll_pin_phase_adjust_get,
2196 	.phase_adjust_set = ice_dpll_input_phase_adjust_set,
2197 	.phase_offset_get = ice_dpll_phase_offset_get,
2198 	.esync_set = ice_dpll_input_esync_set,
2199 	.esync_get = ice_dpll_input_esync_get,
2200 };
2201 
2202 static const struct dpll_pin_ops ice_dpll_output_ops = {
2203 	.frequency_get = ice_dpll_output_frequency_get,
2204 	.frequency_set = ice_dpll_output_frequency_set,
2205 	.state_on_dpll_get = ice_dpll_output_state_get,
2206 	.state_on_dpll_set = ice_dpll_output_state_set,
2207 	.direction_get = ice_dpll_output_direction,
2208 	.phase_adjust_get = ice_dpll_pin_phase_adjust_get,
2209 	.phase_adjust_set = ice_dpll_output_phase_adjust_set,
2210 	.esync_set = ice_dpll_output_esync_set,
2211 	.esync_get = ice_dpll_output_esync_get,
2212 };
2213 
2214 static const struct dpll_device_ops ice_dpll_ops = {
2215 	.lock_status_get = ice_dpll_lock_status_get,
2216 	.mode_get = ice_dpll_mode_get,
2217 };
2218 
2219 /**
2220  * ice_generate_clock_id - generates unique clock_id for registering dpll.
2221  * @pf: board private structure
2222  *
2223  * Generates unique (per board) clock_id for allocation and search of dpll
2224  * devices in Linux dpll subsystem.
2225  *
2226  * Return: generated clock id for the board
2227  */
2228 static u64 ice_generate_clock_id(struct ice_pf *pf)
2229 {
2230 	return pci_get_dsn(pf->pdev);
2231 }
2232 
2233 /**
2234  * ice_dpll_notify_changes - notify dpll subsystem about changes
2235  * @d: pointer do dpll
2236  *
2237  * Once change detected appropriate event is submitted to the dpll subsystem.
2238  */
2239 static void ice_dpll_notify_changes(struct ice_dpll *d)
2240 {
2241 	bool pin_notified = false;
2242 
2243 	if (d->prev_dpll_state != d->dpll_state) {
2244 		d->prev_dpll_state = d->dpll_state;
2245 		dpll_device_change_ntf(d->dpll);
2246 	}
2247 	if (d->prev_input != d->active_input) {
2248 		if (d->prev_input)
2249 			dpll_pin_change_ntf(d->prev_input);
2250 		d->prev_input = d->active_input;
2251 		if (d->active_input) {
2252 			dpll_pin_change_ntf(d->active_input);
2253 			pin_notified = true;
2254 		}
2255 	}
2256 	if (d->prev_phase_offset != d->phase_offset) {
2257 		d->prev_phase_offset = d->phase_offset;
2258 		if (!pin_notified && d->active_input)
2259 			dpll_pin_change_ntf(d->active_input);
2260 	}
2261 }
2262 
2263 /**
2264  * ice_dpll_update_state - update dpll state
2265  * @pf: pf private structure
2266  * @d: pointer to queried dpll device
2267  * @init: if function called on initialization of ice dpll
2268  *
2269  * Poll current state of dpll from hw and update ice_dpll struct.
2270  *
2271  * Context: Called by kworker under pf->dplls.lock
2272  * Return:
2273  * * 0 - success
2274  * * negative - AQ failure
2275  */
2276 static int
2277 ice_dpll_update_state(struct ice_pf *pf, struct ice_dpll *d, bool init)
2278 {
2279 	struct ice_dpll_pin *p = NULL;
2280 	int ret;
2281 
2282 	ret = ice_get_cgu_state(&pf->hw, d->dpll_idx, d->prev_dpll_state,
2283 				&d->input_idx, &d->ref_state, &d->eec_mode,
2284 				&d->phase_offset, &d->dpll_state);
2285 
2286 	dev_dbg(ice_pf_to_dev(pf),
2287 		"update dpll=%d, prev_src_idx:%u, src_idx:%u, state:%d, prev:%d mode:%d\n",
2288 		d->dpll_idx, d->prev_input_idx, d->input_idx,
2289 		d->dpll_state, d->prev_dpll_state, d->mode);
2290 	if (ret) {
2291 		dev_err(ice_pf_to_dev(pf),
2292 			"update dpll=%d state failed, ret=%d %s\n",
2293 			d->dpll_idx, ret,
2294 			ice_aq_str(pf->hw.adminq.sq_last_status));
2295 		return ret;
2296 	}
2297 	if (init) {
2298 		if (d->dpll_state == DPLL_LOCK_STATUS_LOCKED ||
2299 		    d->dpll_state == DPLL_LOCK_STATUS_LOCKED_HO_ACQ)
2300 			d->active_input = pf->dplls.inputs[d->input_idx].pin;
2301 		p = &pf->dplls.inputs[d->input_idx];
2302 		return ice_dpll_pin_state_update(pf, p,
2303 						 ICE_DPLL_PIN_TYPE_INPUT, NULL);
2304 	}
2305 	if (d->dpll_state == DPLL_LOCK_STATUS_HOLDOVER ||
2306 	    d->dpll_state == DPLL_LOCK_STATUS_UNLOCKED) {
2307 		d->active_input = NULL;
2308 		if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID)
2309 			p = &pf->dplls.inputs[d->input_idx];
2310 		d->prev_input_idx = ICE_DPLL_PIN_IDX_INVALID;
2311 		d->input_idx = ICE_DPLL_PIN_IDX_INVALID;
2312 		if (!p)
2313 			return 0;
2314 		ret = ice_dpll_pin_state_update(pf, p,
2315 						ICE_DPLL_PIN_TYPE_INPUT, NULL);
2316 	} else if (d->input_idx != d->prev_input_idx) {
2317 		if (d->prev_input_idx != ICE_DPLL_PIN_IDX_INVALID) {
2318 			p = &pf->dplls.inputs[d->prev_input_idx];
2319 			ice_dpll_pin_state_update(pf, p,
2320 						  ICE_DPLL_PIN_TYPE_INPUT,
2321 						  NULL);
2322 		}
2323 		if (d->input_idx != ICE_DPLL_PIN_IDX_INVALID) {
2324 			p = &pf->dplls.inputs[d->input_idx];
2325 			d->active_input = p->pin;
2326 			ice_dpll_pin_state_update(pf, p,
2327 						  ICE_DPLL_PIN_TYPE_INPUT,
2328 						  NULL);
2329 		}
2330 		d->prev_input_idx = d->input_idx;
2331 	}
2332 
2333 	return ret;
2334 }
2335 
2336 /**
2337  * ice_dpll_periodic_work - DPLLs periodic worker
2338  * @work: pointer to kthread_work structure
2339  *
2340  * DPLLs periodic worker is responsible for polling state of dpll.
2341  * Context: Holds pf->dplls.lock
2342  */
2343 static void ice_dpll_periodic_work(struct kthread_work *work)
2344 {
2345 	struct ice_dplls *d = container_of(work, struct ice_dplls, work.work);
2346 	struct ice_pf *pf = container_of(d, struct ice_pf, dplls);
2347 	struct ice_dpll *de = &pf->dplls.eec;
2348 	struct ice_dpll *dp = &pf->dplls.pps;
2349 	int ret = 0;
2350 
2351 	if (ice_is_reset_in_progress(pf->state))
2352 		goto resched;
2353 	mutex_lock(&pf->dplls.lock);
2354 	ret = ice_dpll_update_state(pf, de, false);
2355 	if (!ret)
2356 		ret = ice_dpll_update_state(pf, dp, false);
2357 	if (ret) {
2358 		d->cgu_state_acq_err_num++;
2359 		/* stop rescheduling this worker */
2360 		if (d->cgu_state_acq_err_num >
2361 		    ICE_CGU_STATE_ACQ_ERR_THRESHOLD) {
2362 			dev_err(ice_pf_to_dev(pf),
2363 				"EEC/PPS DPLLs periodic work disabled\n");
2364 			mutex_unlock(&pf->dplls.lock);
2365 			return;
2366 		}
2367 	}
2368 	mutex_unlock(&pf->dplls.lock);
2369 	ice_dpll_notify_changes(de);
2370 	ice_dpll_notify_changes(dp);
2371 
2372 resched:
2373 	/* Run twice a second or reschedule if update failed */
2374 	kthread_queue_delayed_work(d->kworker, &d->work,
2375 				   ret ? msecs_to_jiffies(10) :
2376 				   msecs_to_jiffies(500));
2377 }
2378 
2379 /**
2380  * ice_dpll_release_pins - release pins resources from dpll subsystem
2381  * @pins: pointer to pins array
2382  * @count: number of pins
2383  *
2384  * Release resources of given pins array in the dpll subsystem.
2385  */
2386 static void ice_dpll_release_pins(struct ice_dpll_pin *pins, int count)
2387 {
2388 	int i;
2389 
2390 	for (i = 0; i < count; i++)
2391 		dpll_pin_put(pins[i].pin);
2392 }
2393 
2394 /**
2395  * ice_dpll_get_pins - get pins from dpll subsystem
2396  * @pf: board private structure
2397  * @pins: pointer to pins array
2398  * @start_idx: get starts from this pin idx value
2399  * @count: number of pins
2400  * @clock_id: clock_id of dpll device
2401  *
2402  * Get pins - allocate - in dpll subsystem, store them in pin field of given
2403  * pins array.
2404  *
2405  * Return:
2406  * * 0 - success
2407  * * negative - allocation failure reason
2408  */
2409 static int
2410 ice_dpll_get_pins(struct ice_pf *pf, struct ice_dpll_pin *pins,
2411 		  int start_idx, int count, u64 clock_id)
2412 {
2413 	int i, ret;
2414 
2415 	for (i = 0; i < count; i++) {
2416 		pins[i].pin = dpll_pin_get(clock_id, i + start_idx, THIS_MODULE,
2417 					   &pins[i].prop);
2418 		if (IS_ERR(pins[i].pin)) {
2419 			ret = PTR_ERR(pins[i].pin);
2420 			goto release_pins;
2421 		}
2422 	}
2423 
2424 	return 0;
2425 
2426 release_pins:
2427 	while (--i >= 0)
2428 		dpll_pin_put(pins[i].pin);
2429 	return ret;
2430 }
2431 
2432 /**
2433  * ice_dpll_unregister_pins - unregister pins from a dpll
2434  * @dpll: dpll device pointer
2435  * @pins: pointer to pins array
2436  * @ops: callback ops registered with the pins
2437  * @count: number of pins
2438  *
2439  * Unregister pins of a given array of pins from given dpll device registered in
2440  * dpll subsystem.
2441  */
2442 static void
2443 ice_dpll_unregister_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
2444 			 const struct dpll_pin_ops *ops, int count)
2445 {
2446 	int i;
2447 
2448 	for (i = 0; i < count; i++)
2449 		if (!pins[i].hidden)
2450 			dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
2451 }
2452 
2453 /**
2454  * ice_dpll_register_pins - register pins with a dpll
2455  * @dpll: dpll pointer to register pins with
2456  * @pins: pointer to pins array
2457  * @ops: callback ops registered with the pins
2458  * @count: number of pins
2459  *
2460  * Register pins of a given array with given dpll in dpll subsystem.
2461  *
2462  * Return:
2463  * * 0 - success
2464  * * negative - registration failure reason
2465  */
2466 static int
2467 ice_dpll_register_pins(struct dpll_device *dpll, struct ice_dpll_pin *pins,
2468 		       const struct dpll_pin_ops *ops, int count)
2469 {
2470 	int ret, i;
2471 
2472 	for (i = 0; i < count; i++) {
2473 		if (!pins[i].hidden) {
2474 			ret = dpll_pin_register(dpll, pins[i].pin, ops, &pins[i]);
2475 			if (ret)
2476 				goto unregister_pins;
2477 		}
2478 	}
2479 
2480 	return 0;
2481 
2482 unregister_pins:
2483 	while (--i >= 0)
2484 		if (!pins[i].hidden)
2485 			dpll_pin_unregister(dpll, pins[i].pin, ops, &pins[i]);
2486 	return ret;
2487 }
2488 
2489 /**
2490  * ice_dpll_deinit_direct_pins - deinitialize direct pins
2491  * @cgu: if cgu is present and controlled by this NIC
2492  * @pins: pointer to pins array
2493  * @count: number of pins
2494  * @ops: callback ops registered with the pins
2495  * @first: dpll device pointer
2496  * @second: dpll device pointer
2497  *
2498  * If cgu is owned unregister pins from given dplls.
2499  * Release pins resources to the dpll subsystem.
2500  */
2501 static void
2502 ice_dpll_deinit_direct_pins(bool cgu, struct ice_dpll_pin *pins, int count,
2503 			    const struct dpll_pin_ops *ops,
2504 			    struct dpll_device *first,
2505 			    struct dpll_device *second)
2506 {
2507 	if (cgu) {
2508 		ice_dpll_unregister_pins(first, pins, ops, count);
2509 		ice_dpll_unregister_pins(second, pins, ops, count);
2510 	}
2511 	ice_dpll_release_pins(pins, count);
2512 }
2513 
2514 /**
2515  * ice_dpll_init_direct_pins - initialize direct pins
2516  * @pf: board private structure
2517  * @cgu: if cgu is present and controlled by this NIC
2518  * @pins: pointer to pins array
2519  * @start_idx: on which index shall allocation start in dpll subsystem
2520  * @count: number of pins
2521  * @ops: callback ops registered with the pins
2522  * @first: dpll device pointer
2523  * @second: dpll device pointer
2524  *
2525  * Allocate directly connected pins of a given array in dpll subsystem.
2526  * If cgu is owned register allocated pins with given dplls.
2527  *
2528  * Return:
2529  * * 0 - success
2530  * * negative - registration failure reason
2531  */
2532 static int
2533 ice_dpll_init_direct_pins(struct ice_pf *pf, bool cgu,
2534 			  struct ice_dpll_pin *pins, int start_idx, int count,
2535 			  const struct dpll_pin_ops *ops,
2536 			  struct dpll_device *first, struct dpll_device *second)
2537 {
2538 	int ret;
2539 
2540 	ret = ice_dpll_get_pins(pf, pins, start_idx, count, pf->dplls.clock_id);
2541 	if (ret)
2542 		return ret;
2543 	if (cgu) {
2544 		ret = ice_dpll_register_pins(first, pins, ops, count);
2545 		if (ret)
2546 			goto release_pins;
2547 		ret = ice_dpll_register_pins(second, pins, ops, count);
2548 		if (ret)
2549 			goto unregister_first;
2550 	}
2551 
2552 	return 0;
2553 
2554 unregister_first:
2555 	ice_dpll_unregister_pins(first, pins, ops, count);
2556 release_pins:
2557 	ice_dpll_release_pins(pins, count);
2558 	return ret;
2559 }
2560 
2561 /**
2562  * ice_dpll_deinit_rclk_pin - release rclk pin resources
2563  * @pf: board private structure
2564  *
2565  * Deregister rclk pin from parent pins and release resources in dpll subsystem.
2566  */
2567 static void ice_dpll_deinit_rclk_pin(struct ice_pf *pf)
2568 {
2569 	struct ice_dpll_pin *rclk = &pf->dplls.rclk;
2570 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
2571 	struct dpll_pin *parent;
2572 	int i;
2573 
2574 	for (i = 0; i < rclk->num_parents; i++) {
2575 		parent = pf->dplls.inputs[rclk->parent_idx[i]].pin;
2576 		if (!parent)
2577 			continue;
2578 		dpll_pin_on_pin_unregister(parent, rclk->pin,
2579 					   &ice_dpll_rclk_ops, rclk);
2580 	}
2581 	if (WARN_ON_ONCE(!vsi || !vsi->netdev))
2582 		return;
2583 	dpll_netdev_pin_clear(vsi->netdev);
2584 	dpll_pin_put(rclk->pin);
2585 }
2586 
2587 /**
2588  * ice_dpll_init_rclk_pins - initialize recovered clock pin
2589  * @pf: board private structure
2590  * @pin: pin to register
2591  * @start_idx: on which index shall allocation start in dpll subsystem
2592  * @ops: callback ops registered with the pins
2593  *
2594  * Allocate resource for recovered clock pin in dpll subsystem. Register the
2595  * pin with the parents it has in the info. Register pin with the pf's main vsi
2596  * netdev.
2597  *
2598  * Return:
2599  * * 0 - success
2600  * * negative - registration failure reason
2601  */
2602 static int
2603 ice_dpll_init_rclk_pins(struct ice_pf *pf, struct ice_dpll_pin *pin,
2604 			int start_idx, const struct dpll_pin_ops *ops)
2605 {
2606 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
2607 	struct dpll_pin *parent;
2608 	int ret, i;
2609 
2610 	if (WARN_ON((!vsi || !vsi->netdev)))
2611 		return -EINVAL;
2612 	ret = ice_dpll_get_pins(pf, pin, start_idx, ICE_DPLL_RCLK_NUM_PER_PF,
2613 				pf->dplls.clock_id);
2614 	if (ret)
2615 		return ret;
2616 	for (i = 0; i < pf->dplls.rclk.num_parents; i++) {
2617 		parent = pf->dplls.inputs[pf->dplls.rclk.parent_idx[i]].pin;
2618 		if (!parent) {
2619 			ret = -ENODEV;
2620 			goto unregister_pins;
2621 		}
2622 		ret = dpll_pin_on_pin_register(parent, pf->dplls.rclk.pin,
2623 					       ops, &pf->dplls.rclk);
2624 		if (ret)
2625 			goto unregister_pins;
2626 	}
2627 	dpll_netdev_pin_set(vsi->netdev, pf->dplls.rclk.pin);
2628 
2629 	return 0;
2630 
2631 unregister_pins:
2632 	while (i) {
2633 		parent = pf->dplls.inputs[pf->dplls.rclk.parent_idx[--i]].pin;
2634 		dpll_pin_on_pin_unregister(parent, pf->dplls.rclk.pin,
2635 					   &ice_dpll_rclk_ops, &pf->dplls.rclk);
2636 	}
2637 	ice_dpll_release_pins(pin, ICE_DPLL_RCLK_NUM_PER_PF);
2638 	return ret;
2639 }
2640 
2641 /**
2642  * ice_dpll_deinit_pins - deinitialize direct pins
2643  * @pf: board private structure
2644  * @cgu: if cgu is controlled by this pf
2645  *
2646  * If cgu is owned unregister directly connected pins from the dplls.
2647  * Release resources of directly connected pins from the dpll subsystem.
2648  */
2649 static void ice_dpll_deinit_pins(struct ice_pf *pf, bool cgu)
2650 {
2651 	struct ice_dpll_pin *outputs = pf->dplls.outputs;
2652 	struct ice_dpll_pin *inputs = pf->dplls.inputs;
2653 	int num_outputs = pf->dplls.num_outputs;
2654 	int num_inputs = pf->dplls.num_inputs;
2655 	struct ice_dplls *d = &pf->dplls;
2656 	struct ice_dpll *de = &d->eec;
2657 	struct ice_dpll *dp = &d->pps;
2658 
2659 	ice_dpll_deinit_rclk_pin(pf);
2660 	if (cgu) {
2661 		ice_dpll_unregister_pins(dp->dpll, inputs, &ice_dpll_input_ops,
2662 					 num_inputs);
2663 		ice_dpll_unregister_pins(de->dpll, inputs, &ice_dpll_input_ops,
2664 					 num_inputs);
2665 	}
2666 	ice_dpll_release_pins(inputs, num_inputs);
2667 	if (cgu) {
2668 		ice_dpll_unregister_pins(dp->dpll, outputs,
2669 					 &ice_dpll_output_ops, num_outputs);
2670 		ice_dpll_unregister_pins(de->dpll, outputs,
2671 					 &ice_dpll_output_ops, num_outputs);
2672 		ice_dpll_release_pins(outputs, num_outputs);
2673 		if (!pf->dplls.generic) {
2674 			ice_dpll_deinit_direct_pins(cgu, pf->dplls.ufl,
2675 						    ICE_DPLL_PIN_SW_NUM,
2676 						    &ice_dpll_pin_ufl_ops,
2677 						    pf->dplls.pps.dpll,
2678 						    pf->dplls.eec.dpll);
2679 			ice_dpll_deinit_direct_pins(cgu, pf->dplls.sma,
2680 						    ICE_DPLL_PIN_SW_NUM,
2681 						    &ice_dpll_pin_sma_ops,
2682 						    pf->dplls.pps.dpll,
2683 						    pf->dplls.eec.dpll);
2684 		}
2685 	}
2686 }
2687 
2688 /**
2689  * ice_dpll_init_pins - init pins and register pins with a dplls
2690  * @pf: board private structure
2691  * @cgu: if cgu is present and controlled by this NIC
2692  *
2693  * Initialize directly connected pf's pins within pf's dplls in a Linux dpll
2694  * subsystem.
2695  *
2696  * Return:
2697  * * 0 - success
2698  * * negative - initialization failure reason
2699  */
2700 static int ice_dpll_init_pins(struct ice_pf *pf, bool cgu)
2701 {
2702 	int ret, count;
2703 
2704 	ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.inputs, 0,
2705 					pf->dplls.num_inputs,
2706 					&ice_dpll_input_ops,
2707 					pf->dplls.eec.dpll, pf->dplls.pps.dpll);
2708 	if (ret)
2709 		return ret;
2710 	count = pf->dplls.num_inputs;
2711 	if (cgu) {
2712 		ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.outputs,
2713 						count,
2714 						pf->dplls.num_outputs,
2715 						&ice_dpll_output_ops,
2716 						pf->dplls.eec.dpll,
2717 						pf->dplls.pps.dpll);
2718 		if (ret)
2719 			goto deinit_inputs;
2720 		count += pf->dplls.num_outputs;
2721 		if (!pf->dplls.generic) {
2722 			ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.sma,
2723 							count,
2724 							ICE_DPLL_PIN_SW_NUM,
2725 							&ice_dpll_pin_sma_ops,
2726 							pf->dplls.eec.dpll,
2727 							pf->dplls.pps.dpll);
2728 			if (ret)
2729 				goto deinit_outputs;
2730 			count += ICE_DPLL_PIN_SW_NUM;
2731 			ret = ice_dpll_init_direct_pins(pf, cgu, pf->dplls.ufl,
2732 							count,
2733 							ICE_DPLL_PIN_SW_NUM,
2734 							&ice_dpll_pin_ufl_ops,
2735 							pf->dplls.eec.dpll,
2736 							pf->dplls.pps.dpll);
2737 			if (ret)
2738 				goto deinit_sma;
2739 			count += ICE_DPLL_PIN_SW_NUM;
2740 		}
2741 	} else {
2742 		count += pf->dplls.num_outputs + 2 * ICE_DPLL_PIN_SW_NUM;
2743 	}
2744 	ret = ice_dpll_init_rclk_pins(pf, &pf->dplls.rclk, count + pf->hw.pf_id,
2745 				      &ice_dpll_rclk_ops);
2746 	if (ret)
2747 		goto deinit_ufl;
2748 
2749 	return 0;
2750 deinit_ufl:
2751 	ice_dpll_deinit_direct_pins(cgu, pf->dplls.ufl,
2752 				    ICE_DPLL_PIN_SW_NUM,
2753 				    &ice_dpll_pin_ufl_ops,
2754 				    pf->dplls.pps.dpll, pf->dplls.eec.dpll);
2755 deinit_sma:
2756 	ice_dpll_deinit_direct_pins(cgu, pf->dplls.sma,
2757 				    ICE_DPLL_PIN_SW_NUM,
2758 				    &ice_dpll_pin_sma_ops,
2759 				    pf->dplls.pps.dpll, pf->dplls.eec.dpll);
2760 deinit_outputs:
2761 	ice_dpll_deinit_direct_pins(cgu, pf->dplls.outputs,
2762 				    pf->dplls.num_outputs,
2763 				    &ice_dpll_output_ops, pf->dplls.pps.dpll,
2764 				    pf->dplls.eec.dpll);
2765 deinit_inputs:
2766 	ice_dpll_deinit_direct_pins(cgu, pf->dplls.inputs, pf->dplls.num_inputs,
2767 				    &ice_dpll_input_ops, pf->dplls.pps.dpll,
2768 				    pf->dplls.eec.dpll);
2769 	return ret;
2770 }
2771 
2772 /**
2773  * ice_dpll_deinit_dpll - deinitialize dpll device
2774  * @pf: board private structure
2775  * @d: pointer to ice_dpll
2776  * @cgu: if cgu is present and controlled by this NIC
2777  *
2778  * If cgu is owned unregister the dpll from dpll subsystem.
2779  * Release resources of dpll device from dpll subsystem.
2780  */
2781 static void
2782 ice_dpll_deinit_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu)
2783 {
2784 	if (cgu)
2785 		dpll_device_unregister(d->dpll, &ice_dpll_ops, d);
2786 	dpll_device_put(d->dpll);
2787 }
2788 
2789 /**
2790  * ice_dpll_init_dpll - initialize dpll device in dpll subsystem
2791  * @pf: board private structure
2792  * @d: dpll to be initialized
2793  * @cgu: if cgu is present and controlled by this NIC
2794  * @type: type of dpll being initialized
2795  *
2796  * Allocate dpll instance for this board in dpll subsystem, if cgu is controlled
2797  * by this NIC, register dpll with the callback ops.
2798  *
2799  * Return:
2800  * * 0 - success
2801  * * negative - initialization failure reason
2802  */
2803 static int
2804 ice_dpll_init_dpll(struct ice_pf *pf, struct ice_dpll *d, bool cgu,
2805 		   enum dpll_type type)
2806 {
2807 	u64 clock_id = pf->dplls.clock_id;
2808 	int ret;
2809 
2810 	d->dpll = dpll_device_get(clock_id, d->dpll_idx, THIS_MODULE);
2811 	if (IS_ERR(d->dpll)) {
2812 		ret = PTR_ERR(d->dpll);
2813 		dev_err(ice_pf_to_dev(pf),
2814 			"dpll_device_get failed (%p) err=%d\n", d, ret);
2815 		return ret;
2816 	}
2817 	d->pf = pf;
2818 	if (cgu) {
2819 		ice_dpll_update_state(pf, d, true);
2820 		ret = dpll_device_register(d->dpll, type, &ice_dpll_ops, d);
2821 		if (ret) {
2822 			dpll_device_put(d->dpll);
2823 			return ret;
2824 		}
2825 	}
2826 
2827 	return 0;
2828 }
2829 
2830 /**
2831  * ice_dpll_deinit_worker - deinitialize dpll kworker
2832  * @pf: board private structure
2833  *
2834  * Stop dpll's kworker, release it's resources.
2835  */
2836 static void ice_dpll_deinit_worker(struct ice_pf *pf)
2837 {
2838 	struct ice_dplls *d = &pf->dplls;
2839 
2840 	kthread_cancel_delayed_work_sync(&d->work);
2841 	kthread_destroy_worker(d->kworker);
2842 }
2843 
2844 /**
2845  * ice_dpll_init_worker - Initialize DPLLs periodic worker
2846  * @pf: board private structure
2847  *
2848  * Create and start DPLLs periodic worker.
2849  *
2850  * Context: Shall be called after pf->dplls.lock is initialized.
2851  * Return:
2852  * * 0 - success
2853  * * negative - create worker failure
2854  */
2855 static int ice_dpll_init_worker(struct ice_pf *pf)
2856 {
2857 	struct ice_dplls *d = &pf->dplls;
2858 	struct kthread_worker *kworker;
2859 
2860 	kthread_init_delayed_work(&d->work, ice_dpll_periodic_work);
2861 	kworker = kthread_run_worker(0, "ice-dplls-%s",
2862 					dev_name(ice_pf_to_dev(pf)));
2863 	if (IS_ERR(kworker))
2864 		return PTR_ERR(kworker);
2865 	d->kworker = kworker;
2866 	d->cgu_state_acq_err_num = 0;
2867 	kthread_queue_delayed_work(d->kworker, &d->work, 0);
2868 
2869 	return 0;
2870 }
2871 
2872 /**
2873  * ice_dpll_phase_range_set - initialize phase adjust range helper
2874  * @range: pointer to phase adjust range struct to be initialized
2875  * @phase_adj: a value to be used as min(-)/max(+) boundary
2876  */
2877 static void ice_dpll_phase_range_set(struct dpll_pin_phase_adjust_range *range,
2878 				     u32 phase_adj)
2879 {
2880 	range->min = -phase_adj;
2881 	range->max = phase_adj;
2882 }
2883 
2884 /**
2885  * ice_dpll_init_info_pins_generic - initializes generic pins info
2886  * @pf: board private structure
2887  * @input: if input pins initialized
2888  *
2889  * Init information for generic pins, cache them in PF's pins structures.
2890  *
2891  * Return:
2892  * * 0 - success
2893  * * negative - init failure reason
2894  */
2895 static int ice_dpll_init_info_pins_generic(struct ice_pf *pf, bool input)
2896 {
2897 	struct ice_dpll *de = &pf->dplls.eec, *dp = &pf->dplls.pps;
2898 	static const char labels[][sizeof("99")] = {
2899 		"0", "1", "2", "3", "4", "5", "6", "7", "8",
2900 		"9", "10", "11", "12", "13", "14", "15" };
2901 	u32 cap = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
2902 	enum ice_dpll_pin_type pin_type;
2903 	int i, pin_num, ret = -EINVAL;
2904 	struct ice_dpll_pin *pins;
2905 	u32 phase_adj_max;
2906 
2907 	if (input) {
2908 		pin_num = pf->dplls.num_inputs;
2909 		pins = pf->dplls.inputs;
2910 		phase_adj_max = pf->dplls.input_phase_adj_max;
2911 		pin_type = ICE_DPLL_PIN_TYPE_INPUT;
2912 		cap |= DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE;
2913 	} else {
2914 		pin_num = pf->dplls.num_outputs;
2915 		pins = pf->dplls.outputs;
2916 		phase_adj_max = pf->dplls.output_phase_adj_max;
2917 		pin_type = ICE_DPLL_PIN_TYPE_OUTPUT;
2918 	}
2919 	if (pin_num > ARRAY_SIZE(labels))
2920 		return ret;
2921 
2922 	for (i = 0; i < pin_num; i++) {
2923 		pins[i].idx = i;
2924 		pins[i].prop.board_label = labels[i];
2925 		ice_dpll_phase_range_set(&pins[i].prop.phase_range,
2926 					 phase_adj_max);
2927 		pins[i].prop.capabilities = cap;
2928 		pins[i].pf = pf;
2929 		ret = ice_dpll_pin_state_update(pf, &pins[i], pin_type, NULL);
2930 		if (ret)
2931 			break;
2932 		if (input && pins[i].freq == ICE_DPLL_PIN_GEN_RCLK_FREQ)
2933 			pins[i].prop.type = DPLL_PIN_TYPE_MUX;
2934 		else
2935 			pins[i].prop.type = DPLL_PIN_TYPE_EXT;
2936 		if (!input)
2937 			continue;
2938 		ret = ice_aq_get_cgu_ref_prio(&pf->hw, de->dpll_idx, i,
2939 					      &de->input_prio[i]);
2940 		if (ret)
2941 			break;
2942 		ret = ice_aq_get_cgu_ref_prio(&pf->hw, dp->dpll_idx, i,
2943 					      &dp->input_prio[i]);
2944 		if (ret)
2945 			break;
2946 	}
2947 
2948 	return ret;
2949 }
2950 
2951 /**
2952  * ice_dpll_init_info_direct_pins - initializes direct pins info
2953  * @pf: board private structure
2954  * @pin_type: type of pins being initialized
2955  *
2956  * Init information for directly connected pins, cache them in pf's pins
2957  * structures.
2958  *
2959  * Return:
2960  * * 0 - success
2961  * * negative - init failure reason
2962  */
2963 static int
2964 ice_dpll_init_info_direct_pins(struct ice_pf *pf,
2965 			       enum ice_dpll_pin_type pin_type)
2966 {
2967 	struct ice_dpll *de = &pf->dplls.eec, *dp = &pf->dplls.pps;
2968 	int num_pins, i, ret = -EINVAL;
2969 	struct ice_hw *hw = &pf->hw;
2970 	struct ice_dpll_pin *pins;
2971 	unsigned long caps;
2972 	u32 phase_adj_max;
2973 	u8 freq_supp_num;
2974 	bool input;
2975 
2976 	switch (pin_type) {
2977 	case ICE_DPLL_PIN_TYPE_INPUT:
2978 		pins = pf->dplls.inputs;
2979 		num_pins = pf->dplls.num_inputs;
2980 		phase_adj_max = pf->dplls.input_phase_adj_max;
2981 		input = true;
2982 		break;
2983 	case ICE_DPLL_PIN_TYPE_OUTPUT:
2984 		pins = pf->dplls.outputs;
2985 		num_pins = pf->dplls.num_outputs;
2986 		phase_adj_max = pf->dplls.output_phase_adj_max;
2987 		input = false;
2988 		break;
2989 	default:
2990 		return -EINVAL;
2991 	}
2992 	if (num_pins != ice_cgu_get_num_pins(hw, input)) {
2993 		pf->dplls.generic = true;
2994 		return ice_dpll_init_info_pins_generic(pf, input);
2995 	}
2996 
2997 	for (i = 0; i < num_pins; i++) {
2998 		caps = 0;
2999 		pins[i].idx = i;
3000 		pins[i].prop.board_label = ice_cgu_get_pin_name(hw, i, input);
3001 		pins[i].prop.type = ice_cgu_get_pin_type(hw, i, input);
3002 		if (input) {
3003 			ret = ice_aq_get_cgu_ref_prio(hw, de->dpll_idx, i,
3004 						      &de->input_prio[i]);
3005 			if (ret)
3006 				return ret;
3007 			ret = ice_aq_get_cgu_ref_prio(hw, dp->dpll_idx, i,
3008 						      &dp->input_prio[i]);
3009 			if (ret)
3010 				return ret;
3011 			caps |= (DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
3012 				 DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE);
3013 			if (ice_dpll_is_sw_pin(pf, i, true))
3014 				pins[i].hidden = true;
3015 		} else {
3016 			ret = ice_cgu_get_output_pin_state_caps(hw, i, &caps);
3017 			if (ret)
3018 				return ret;
3019 			if (ice_dpll_is_sw_pin(pf, i, false))
3020 				pins[i].hidden = true;
3021 		}
3022 		ice_dpll_phase_range_set(&pins[i].prop.phase_range,
3023 					 phase_adj_max);
3024 		pins[i].prop.capabilities = caps;
3025 		ret = ice_dpll_pin_state_update(pf, &pins[i], pin_type, NULL);
3026 		if (ret)
3027 			return ret;
3028 		pins[i].prop.freq_supported =
3029 			ice_cgu_get_pin_freq_supp(hw, i, input, &freq_supp_num);
3030 		pins[i].prop.freq_supported_num = freq_supp_num;
3031 		pins[i].pf = pf;
3032 	}
3033 
3034 	return ret;
3035 }
3036 
3037 /**
3038  * ice_dpll_init_info_rclk_pin - initializes rclk pin information
3039  * @pf: board private structure
3040  *
3041  * Init information for rclk pin, cache them in pf->dplls.rclk.
3042  *
3043  * Return:
3044  * * 0 - success
3045  * * negative - init failure reason
3046  */
3047 static int ice_dpll_init_info_rclk_pin(struct ice_pf *pf)
3048 {
3049 	struct ice_dpll_pin *pin = &pf->dplls.rclk;
3050 
3051 	pin->prop.type = DPLL_PIN_TYPE_SYNCE_ETH_PORT;
3052 	pin->prop.capabilities |= DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
3053 	pin->pf = pf;
3054 
3055 	return ice_dpll_pin_state_update(pf, pin,
3056 					 ICE_DPLL_PIN_TYPE_RCLK_INPUT, NULL);
3057 }
3058 
3059 /**
3060  * ice_dpll_init_info_sw_pins - initializes software controlled pin information
3061  * @pf: board private structure
3062  *
3063  * Init information for software controlled pins, cache them in
3064  * pf->dplls.sma and pf->dplls.ufl.
3065  *
3066  * Return:
3067  * * 0 - success
3068  * * negative - init failure reason
3069  */
3070 static int ice_dpll_init_info_sw_pins(struct ice_pf *pf)
3071 {
3072 	u8 freq_supp_num, pin_abs_idx, input_idx_offset = 0;
3073 	struct ice_dplls *d = &pf->dplls;
3074 	struct ice_dpll_pin *pin;
3075 	u32 phase_adj_max, caps;
3076 	int i, ret;
3077 
3078 	if (pf->hw.device_id == ICE_DEV_ID_E810C_QSFP)
3079 		input_idx_offset = ICE_E810_RCLK_PINS_NUM;
3080 	phase_adj_max = max(d->input_phase_adj_max, d->output_phase_adj_max);
3081 	caps = DPLL_PIN_CAPABILITIES_STATE_CAN_CHANGE;
3082 	for (i = 0; i < ICE_DPLL_PIN_SW_NUM; i++) {
3083 		pin = &d->sma[i];
3084 		pin->idx = i;
3085 		pin->prop.type = DPLL_PIN_TYPE_EXT;
3086 		pin_abs_idx = ICE_DPLL_PIN_SW_INPUT_ABS(i) + input_idx_offset;
3087 		pin->prop.freq_supported =
3088 			ice_cgu_get_pin_freq_supp(&pf->hw, pin_abs_idx,
3089 						  true, &freq_supp_num);
3090 		pin->prop.freq_supported_num = freq_supp_num;
3091 		pin->prop.capabilities =
3092 			(DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE |
3093 			 DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
3094 			 caps);
3095 		pin->pf = pf;
3096 		pin->prop.board_label = ice_dpll_sw_pin_sma[i];
3097 		pin->input = &d->inputs[pin_abs_idx];
3098 		pin->output = &d->outputs[ICE_DPLL_PIN_SW_OUTPUT_ABS(i)];
3099 		ice_dpll_phase_range_set(&pin->prop.phase_range, phase_adj_max);
3100 	}
3101 	for (i = 0; i < ICE_DPLL_PIN_SW_NUM; i++) {
3102 		pin = &d->ufl[i];
3103 		pin->idx = i;
3104 		pin->prop.type = DPLL_PIN_TYPE_EXT;
3105 		pin->prop.capabilities = caps;
3106 		pin->pf = pf;
3107 		pin->prop.board_label = ice_dpll_sw_pin_ufl[i];
3108 		if (i == ICE_DPLL_PIN_SW_1_IDX) {
3109 			pin->direction = DPLL_PIN_DIRECTION_OUTPUT;
3110 			pin_abs_idx = ICE_DPLL_PIN_SW_OUTPUT_ABS(i);
3111 			pin->prop.freq_supported =
3112 				ice_cgu_get_pin_freq_supp(&pf->hw, pin_abs_idx,
3113 							  false,
3114 							  &freq_supp_num);
3115 			pin->prop.freq_supported_num = freq_supp_num;
3116 			pin->input = NULL;
3117 			pin->output = &d->outputs[pin_abs_idx];
3118 		} else if (i == ICE_DPLL_PIN_SW_2_IDX) {
3119 			pin->direction = DPLL_PIN_DIRECTION_INPUT;
3120 			pin_abs_idx = ICE_DPLL_PIN_SW_INPUT_ABS(i) +
3121 				      input_idx_offset;
3122 			pin->output = NULL;
3123 			pin->input = &d->inputs[pin_abs_idx];
3124 			pin->prop.freq_supported =
3125 				ice_cgu_get_pin_freq_supp(&pf->hw, pin_abs_idx,
3126 							  true, &freq_supp_num);
3127 			pin->prop.freq_supported_num = freq_supp_num;
3128 			pin->prop.capabilities =
3129 				(DPLL_PIN_CAPABILITIES_PRIORITY_CAN_CHANGE |
3130 				 caps);
3131 		}
3132 		ice_dpll_phase_range_set(&pin->prop.phase_range, phase_adj_max);
3133 	}
3134 	ret = ice_dpll_pin_state_update(pf, pin, ICE_DPLL_PIN_TYPE_SOFTWARE,
3135 					NULL);
3136 	if (ret)
3137 		return ret;
3138 
3139 	return 0;
3140 }
3141 
3142 /**
3143  * ice_dpll_init_pins_info - init pins info wrapper
3144  * @pf: board private structure
3145  * @pin_type: type of pins being initialized
3146  *
3147  * Wraps functions for pin initialization.
3148  *
3149  * Return:
3150  * * 0 - success
3151  * * negative - init failure reason
3152  */
3153 static int
3154 ice_dpll_init_pins_info(struct ice_pf *pf, enum ice_dpll_pin_type pin_type)
3155 {
3156 	switch (pin_type) {
3157 	case ICE_DPLL_PIN_TYPE_INPUT:
3158 	case ICE_DPLL_PIN_TYPE_OUTPUT:
3159 		return ice_dpll_init_info_direct_pins(pf, pin_type);
3160 	case ICE_DPLL_PIN_TYPE_RCLK_INPUT:
3161 		return ice_dpll_init_info_rclk_pin(pf);
3162 	case ICE_DPLL_PIN_TYPE_SOFTWARE:
3163 		return ice_dpll_init_info_sw_pins(pf);
3164 	default:
3165 		return -EINVAL;
3166 	}
3167 }
3168 
3169 /**
3170  * ice_dpll_deinit_info - release memory allocated for pins info
3171  * @pf: board private structure
3172  *
3173  * Release memory allocated for pins by ice_dpll_init_info function.
3174  */
3175 static void ice_dpll_deinit_info(struct ice_pf *pf)
3176 {
3177 	kfree(pf->dplls.inputs);
3178 	kfree(pf->dplls.outputs);
3179 	kfree(pf->dplls.eec.input_prio);
3180 	kfree(pf->dplls.pps.input_prio);
3181 }
3182 
3183 /**
3184  * ice_dpll_init_info - prepare pf's dpll information structure
3185  * @pf: board private structure
3186  * @cgu: if cgu is present and controlled by this NIC
3187  *
3188  * Acquire (from HW) and set basic dpll information (on pf->dplls struct).
3189  *
3190  * Return:
3191  * * 0 - success
3192  * * negative - init failure reason
3193  */
3194 static int ice_dpll_init_info(struct ice_pf *pf, bool cgu)
3195 {
3196 	struct ice_aqc_get_cgu_abilities abilities;
3197 	struct ice_dpll *de = &pf->dplls.eec;
3198 	struct ice_dpll *dp = &pf->dplls.pps;
3199 	struct ice_dplls *d = &pf->dplls;
3200 	struct ice_hw *hw = &pf->hw;
3201 	int ret, alloc_size, i;
3202 
3203 	d->clock_id = ice_generate_clock_id(pf);
3204 	ret = ice_aq_get_cgu_abilities(hw, &abilities);
3205 	if (ret) {
3206 		dev_err(ice_pf_to_dev(pf),
3207 			"err:%d %s failed to read cgu abilities\n",
3208 			ret, ice_aq_str(hw->adminq.sq_last_status));
3209 		return ret;
3210 	}
3211 
3212 	de->dpll_idx = abilities.eec_dpll_idx;
3213 	dp->dpll_idx = abilities.pps_dpll_idx;
3214 	d->num_inputs = abilities.num_inputs;
3215 	d->num_outputs = abilities.num_outputs;
3216 	d->input_phase_adj_max = le32_to_cpu(abilities.max_in_phase_adj) &
3217 		ICE_AQC_GET_CGU_MAX_PHASE_ADJ;
3218 	d->output_phase_adj_max = le32_to_cpu(abilities.max_out_phase_adj) &
3219 		ICE_AQC_GET_CGU_MAX_PHASE_ADJ;
3220 
3221 	alloc_size = sizeof(*d->inputs) * d->num_inputs;
3222 	d->inputs = kzalloc(alloc_size, GFP_KERNEL);
3223 	if (!d->inputs)
3224 		return -ENOMEM;
3225 
3226 	alloc_size = sizeof(*de->input_prio) * d->num_inputs;
3227 	de->input_prio = kzalloc(alloc_size, GFP_KERNEL);
3228 	if (!de->input_prio)
3229 		return -ENOMEM;
3230 
3231 	dp->input_prio = kzalloc(alloc_size, GFP_KERNEL);
3232 	if (!dp->input_prio)
3233 		return -ENOMEM;
3234 
3235 	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_INPUT);
3236 	if (ret)
3237 		goto deinit_info;
3238 
3239 	if (cgu) {
3240 		alloc_size = sizeof(*d->outputs) * d->num_outputs;
3241 		d->outputs = kzalloc(alloc_size, GFP_KERNEL);
3242 		if (!d->outputs) {
3243 			ret = -ENOMEM;
3244 			goto deinit_info;
3245 		}
3246 
3247 		ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_OUTPUT);
3248 		if (ret)
3249 			goto deinit_info;
3250 		ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_SOFTWARE);
3251 		if (ret)
3252 			goto deinit_info;
3253 	}
3254 
3255 	ret = ice_get_cgu_rclk_pin_info(&pf->hw, &d->base_rclk_idx,
3256 					&pf->dplls.rclk.num_parents);
3257 	if (ret)
3258 		return ret;
3259 	for (i = 0; i < pf->dplls.rclk.num_parents; i++)
3260 		pf->dplls.rclk.parent_idx[i] = d->base_rclk_idx + i;
3261 	ret = ice_dpll_init_pins_info(pf, ICE_DPLL_PIN_TYPE_RCLK_INPUT);
3262 	if (ret)
3263 		return ret;
3264 	de->mode = DPLL_MODE_AUTOMATIC;
3265 	dp->mode = DPLL_MODE_AUTOMATIC;
3266 
3267 	dev_dbg(ice_pf_to_dev(pf),
3268 		"%s - success, inputs:%u, outputs:%u rclk-parents:%u\n",
3269 		__func__, d->num_inputs, d->num_outputs, d->rclk.num_parents);
3270 
3271 	return 0;
3272 
3273 deinit_info:
3274 	dev_err(ice_pf_to_dev(pf),
3275 		"%s - fail: d->inputs:%p, de->input_prio:%p, dp->input_prio:%p, d->outputs:%p\n",
3276 		__func__, d->inputs, de->input_prio,
3277 		dp->input_prio, d->outputs);
3278 	ice_dpll_deinit_info(pf);
3279 	return ret;
3280 }
3281 
3282 /**
3283  * ice_dpll_deinit - Disable the driver/HW support for dpll subsystem
3284  * the dpll device.
3285  * @pf: board private structure
3286  *
3287  * Handles the cleanup work required after dpll initialization, freeing
3288  * resources and unregistering the dpll, pin and all resources used for
3289  * handling them.
3290  *
3291  * Context: Destroys pf->dplls.lock mutex. Call only if ICE_FLAG_DPLL was set.
3292  */
3293 void ice_dpll_deinit(struct ice_pf *pf)
3294 {
3295 	bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);
3296 
3297 	clear_bit(ICE_FLAG_DPLL, pf->flags);
3298 	if (cgu)
3299 		ice_dpll_deinit_worker(pf);
3300 
3301 	ice_dpll_deinit_pins(pf, cgu);
3302 	ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
3303 	ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
3304 	ice_dpll_deinit_info(pf);
3305 	mutex_destroy(&pf->dplls.lock);
3306 }
3307 
3308 /**
3309  * ice_dpll_init - initialize support for dpll subsystem
3310  * @pf: board private structure
3311  *
3312  * Set up the device dplls, register them and pins connected within Linux dpll
3313  * subsystem. Allow userspace to obtain state of DPLL and handling of DPLL
3314  * configuration requests.
3315  *
3316  * Context: Initializes pf->dplls.lock mutex.
3317  */
3318 void ice_dpll_init(struct ice_pf *pf)
3319 {
3320 	bool cgu = ice_is_feature_supported(pf, ICE_F_CGU);
3321 	struct ice_dplls *d = &pf->dplls;
3322 	int err = 0;
3323 
3324 	mutex_init(&d->lock);
3325 	err = ice_dpll_init_info(pf, cgu);
3326 	if (err)
3327 		goto err_exit;
3328 	err = ice_dpll_init_dpll(pf, &pf->dplls.eec, cgu, DPLL_TYPE_EEC);
3329 	if (err)
3330 		goto deinit_info;
3331 	err = ice_dpll_init_dpll(pf, &pf->dplls.pps, cgu, DPLL_TYPE_PPS);
3332 	if (err)
3333 		goto deinit_eec;
3334 	err = ice_dpll_init_pins(pf, cgu);
3335 	if (err)
3336 		goto deinit_pps;
3337 	if (cgu) {
3338 		err = ice_dpll_init_worker(pf);
3339 		if (err)
3340 			goto deinit_pins;
3341 	}
3342 	set_bit(ICE_FLAG_DPLL, pf->flags);
3343 
3344 	return;
3345 
3346 deinit_pins:
3347 	ice_dpll_deinit_pins(pf, cgu);
3348 deinit_pps:
3349 	ice_dpll_deinit_dpll(pf, &pf->dplls.pps, cgu);
3350 deinit_eec:
3351 	ice_dpll_deinit_dpll(pf, &pf->dplls.eec, cgu);
3352 deinit_info:
3353 	ice_dpll_deinit_info(pf);
3354 err_exit:
3355 	mutex_destroy(&d->lock);
3356 	dev_warn(ice_pf_to_dev(pf), "DPLLs init failure err:%d\n", err);
3357 }
3358