1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021, Intel Corporation. */
8 #define E810_OUT_PROP_DELAY_NS 1
10 static const struct ptp_pin_desc ice_pin_desc_e810t[] = {
11 /* name idx func chan */
12 { "GNSS", GNSS, PTP_PF_EXTTS, 0, { 0, } },
13 { "SMA1", SMA1, PTP_PF_NONE, 1, { 0, } },
14 { "U.FL1", UFL1, PTP_PF_NONE, 1, { 0, } },
15 { "SMA2", SMA2, PTP_PF_NONE, 2, { 0, } },
16 { "U.FL2", UFL2, PTP_PF_NONE, 2, { 0, } },
20 * ice_get_sma_config_e810t
21 * @hw: pointer to the hw struct
22 * @ptp_pins: pointer to the ptp_pin_desc struture
24 * Read the configuration of the SMA control logic and put it into the
25 * ptp_pin_desc structure
28 ice_get_sma_config_e810t(struct ice_hw *hw, struct ptp_pin_desc *ptp_pins)
33 /* Read initial pin state */
34 status = ice_read_sma_ctrl_e810t(hw, &data);
38 /* initialize with defaults */
39 for (i = 0; i < NUM_PTP_PINS_E810T; i++) {
40 strscpy(ptp_pins[i].name, ice_pin_desc_e810t[i].name,
41 sizeof(ptp_pins[i].name));
42 ptp_pins[i].index = ice_pin_desc_e810t[i].index;
43 ptp_pins[i].func = ice_pin_desc_e810t[i].func;
44 ptp_pins[i].chan = ice_pin_desc_e810t[i].chan;
48 switch (data & ICE_SMA1_MASK_E810T) {
49 case ICE_SMA1_MASK_E810T:
51 ptp_pins[SMA1].func = PTP_PF_NONE;
52 ptp_pins[UFL1].func = PTP_PF_NONE;
54 case ICE_SMA1_DIR_EN_E810T:
55 ptp_pins[SMA1].func = PTP_PF_PEROUT;
56 ptp_pins[UFL1].func = PTP_PF_NONE;
58 case ICE_SMA1_TX_EN_E810T:
59 ptp_pins[SMA1].func = PTP_PF_EXTTS;
60 ptp_pins[UFL1].func = PTP_PF_NONE;
63 ptp_pins[SMA1].func = PTP_PF_EXTTS;
64 ptp_pins[UFL1].func = PTP_PF_PEROUT;
69 switch (data & ICE_SMA2_MASK_E810T) {
70 case ICE_SMA2_MASK_E810T:
72 ptp_pins[SMA2].func = PTP_PF_NONE;
73 ptp_pins[UFL2].func = PTP_PF_NONE;
75 case (ICE_SMA2_TX_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T):
76 ptp_pins[SMA2].func = PTP_PF_EXTTS;
77 ptp_pins[UFL2].func = PTP_PF_NONE;
79 case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T):
80 ptp_pins[SMA2].func = PTP_PF_PEROUT;
81 ptp_pins[UFL2].func = PTP_PF_NONE;
83 case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T):
84 ptp_pins[SMA2].func = PTP_PF_NONE;
85 ptp_pins[UFL2].func = PTP_PF_EXTTS;
87 case ICE_SMA2_DIR_EN_E810T:
88 ptp_pins[SMA2].func = PTP_PF_PEROUT;
89 ptp_pins[UFL2].func = PTP_PF_EXTTS;
97 * ice_ptp_set_sma_config_e810t
98 * @hw: pointer to the hw struct
99 * @ptp_pins: pointer to the ptp_pin_desc struture
101 * Set the configuration of the SMA control logic based on the configuration in
105 ice_ptp_set_sma_config_e810t(struct ice_hw *hw,
106 const struct ptp_pin_desc *ptp_pins)
111 /* SMA1 and UFL1 cannot be set to TX at the same time */
112 if (ptp_pins[SMA1].func == PTP_PF_PEROUT &&
113 ptp_pins[UFL1].func == PTP_PF_PEROUT)
116 /* SMA2 and UFL2 cannot be set to RX at the same time */
117 if (ptp_pins[SMA2].func == PTP_PF_EXTTS &&
118 ptp_pins[UFL2].func == PTP_PF_EXTTS)
121 /* Read initial pin state value */
122 status = ice_read_sma_ctrl_e810t(hw, &data);
126 /* Set the right sate based on the desired configuration */
127 data &= ~ICE_SMA1_MASK_E810T;
128 if (ptp_pins[SMA1].func == PTP_PF_NONE &&
129 ptp_pins[UFL1].func == PTP_PF_NONE) {
130 dev_info(ice_hw_to_dev(hw), "SMA1 + U.FL1 disabled");
131 data |= ICE_SMA1_MASK_E810T;
132 } else if (ptp_pins[SMA1].func == PTP_PF_EXTTS &&
133 ptp_pins[UFL1].func == PTP_PF_NONE) {
134 dev_info(ice_hw_to_dev(hw), "SMA1 RX");
135 data |= ICE_SMA1_TX_EN_E810T;
136 } else if (ptp_pins[SMA1].func == PTP_PF_NONE &&
137 ptp_pins[UFL1].func == PTP_PF_PEROUT) {
138 /* U.FL 1 TX will always enable SMA 1 RX */
139 dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX");
140 } else if (ptp_pins[SMA1].func == PTP_PF_EXTTS &&
141 ptp_pins[UFL1].func == PTP_PF_PEROUT) {
142 dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX");
143 } else if (ptp_pins[SMA1].func == PTP_PF_PEROUT &&
144 ptp_pins[UFL1].func == PTP_PF_NONE) {
145 dev_info(ice_hw_to_dev(hw), "SMA1 TX");
146 data |= ICE_SMA1_DIR_EN_E810T;
149 data &= ~ICE_SMA2_MASK_E810T;
150 if (ptp_pins[SMA2].func == PTP_PF_NONE &&
151 ptp_pins[UFL2].func == PTP_PF_NONE) {
152 dev_info(ice_hw_to_dev(hw), "SMA2 + U.FL2 disabled");
153 data |= ICE_SMA2_MASK_E810T;
154 } else if (ptp_pins[SMA2].func == PTP_PF_EXTTS &&
155 ptp_pins[UFL2].func == PTP_PF_NONE) {
156 dev_info(ice_hw_to_dev(hw), "SMA2 RX");
157 data |= (ICE_SMA2_TX_EN_E810T |
158 ICE_SMA2_UFL2_RX_DIS_E810T);
159 } else if (ptp_pins[SMA2].func == PTP_PF_NONE &&
160 ptp_pins[UFL2].func == PTP_PF_EXTTS) {
161 dev_info(ice_hw_to_dev(hw), "UFL2 RX");
162 data |= (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T);
163 } else if (ptp_pins[SMA2].func == PTP_PF_PEROUT &&
164 ptp_pins[UFL2].func == PTP_PF_NONE) {
165 dev_info(ice_hw_to_dev(hw), "SMA2 TX");
166 data |= (ICE_SMA2_DIR_EN_E810T |
167 ICE_SMA2_UFL2_RX_DIS_E810T);
168 } else if (ptp_pins[SMA2].func == PTP_PF_PEROUT &&
169 ptp_pins[UFL2].func == PTP_PF_EXTTS) {
170 dev_info(ice_hw_to_dev(hw), "SMA2 TX + U.FL2 RX");
171 data |= ICE_SMA2_DIR_EN_E810T;
174 return ice_write_sma_ctrl_e810t(hw, data);
178 * ice_ptp_set_sma_e810t
179 * @info: the driver's PTP info structure
180 * @pin: pin index in kernel structure
181 * @func: Pin function to be set (PTP_PF_NONE, PTP_PF_EXTTS or PTP_PF_PEROUT)
183 * Set the configuration of a single SMA pin
186 ice_ptp_set_sma_e810t(struct ptp_clock_info *info, unsigned int pin,
187 enum ptp_pin_function func)
189 struct ptp_pin_desc ptp_pins[NUM_PTP_PINS_E810T];
190 struct ice_pf *pf = ptp_info_to_pf(info);
191 struct ice_hw *hw = &pf->hw;
194 if (pin < SMA1 || func > PTP_PF_PEROUT)
197 err = ice_get_sma_config_e810t(hw, ptp_pins);
201 /* Disable the same function on the other pin sharing the channel */
202 if (pin == SMA1 && ptp_pins[UFL1].func == func)
203 ptp_pins[UFL1].func = PTP_PF_NONE;
204 if (pin == UFL1 && ptp_pins[SMA1].func == func)
205 ptp_pins[SMA1].func = PTP_PF_NONE;
207 if (pin == SMA2 && ptp_pins[UFL2].func == func)
208 ptp_pins[UFL2].func = PTP_PF_NONE;
209 if (pin == UFL2 && ptp_pins[SMA2].func == func)
210 ptp_pins[SMA2].func = PTP_PF_NONE;
212 /* Set up new pin function in the temp table */
213 ptp_pins[pin].func = func;
215 return ice_ptp_set_sma_config_e810t(hw, ptp_pins);
219 * ice_verify_pin_e810t
220 * @info: the driver's PTP info structure
222 * @func: Assigned function
223 * @chan: Assigned channel
225 * Verify if pin supports requested pin function. If the Check pins consistency.
226 * Reconfigure the SMA logic attached to the given pin to enable its
227 * desired functionality
230 ice_verify_pin_e810t(struct ptp_clock_info *info, unsigned int pin,
231 enum ptp_pin_function func, unsigned int chan)
233 /* Don't allow channel reassignment */
234 if (chan != ice_pin_desc_e810t[pin].chan)
237 /* Check if functions are properly assigned */
246 if (pin == UFL2 || pin == GNSS)
253 return ice_ptp_set_sma_e810t(info, pin, func);
257 * ice_ptp_cfg_tx_interrupt - Configure Tx timestamp interrupt for the device
258 * @pf: Board private structure
260 * Program the device to respond appropriately to the Tx timestamp interrupt
263 static void ice_ptp_cfg_tx_interrupt(struct ice_pf *pf)
265 struct ice_hw *hw = &pf->hw;
269 switch (pf->ptp.tx_interrupt_mode) {
270 case ICE_PTP_TX_INTERRUPT_ALL:
271 /* React to interrupts across all quads. */
272 wr32(hw, PFINT_TSYN_MSK + (0x4 * hw->pf_id), (u32)0x1f);
275 case ICE_PTP_TX_INTERRUPT_NONE:
276 /* Do not react to interrupts on any quad. */
277 wr32(hw, PFINT_TSYN_MSK + (0x4 * hw->pf_id), (u32)0x0);
280 case ICE_PTP_TX_INTERRUPT_SELF:
282 enable = pf->ptp.tstamp_config.tx_type == HWTSTAMP_TX_ON;
286 /* Configure the Tx timestamp interrupt */
287 val = rd32(hw, PFINT_OICR_ENA);
289 val |= PFINT_OICR_TSYN_TX_M;
291 val &= ~PFINT_OICR_TSYN_TX_M;
292 wr32(hw, PFINT_OICR_ENA, val);
296 * ice_set_rx_tstamp - Enable or disable Rx timestamping
297 * @pf: The PF pointer to search in
298 * @on: bool value for whether timestamps are enabled or disabled
300 static void ice_set_rx_tstamp(struct ice_pf *pf, bool on)
305 vsi = ice_get_main_vsi(pf);
306 if (!vsi || !vsi->rx_rings)
309 /* Set the timestamp flag for all the Rx rings */
310 ice_for_each_rxq(vsi, i) {
311 if (!vsi->rx_rings[i])
313 vsi->rx_rings[i]->ptp_rx = on;
318 * ice_ptp_disable_timestamp_mode - Disable current timestamp mode
319 * @pf: Board private structure
321 * Called during preparation for reset to temporarily disable timestamping on
322 * the device. Called during remove to disable timestamping while cleaning up
325 static void ice_ptp_disable_timestamp_mode(struct ice_pf *pf)
327 struct ice_hw *hw = &pf->hw;
330 val = rd32(hw, PFINT_OICR_ENA);
331 val &= ~PFINT_OICR_TSYN_TX_M;
332 wr32(hw, PFINT_OICR_ENA, val);
334 ice_set_rx_tstamp(pf, false);
338 * ice_ptp_restore_timestamp_mode - Restore timestamp configuration
339 * @pf: Board private structure
341 * Called at the end of rebuild to restore timestamp configuration after
344 void ice_ptp_restore_timestamp_mode(struct ice_pf *pf)
346 struct ice_hw *hw = &pf->hw;
349 ice_ptp_cfg_tx_interrupt(pf);
351 enable_rx = pf->ptp.tstamp_config.rx_filter == HWTSTAMP_FILTER_ALL;
352 ice_set_rx_tstamp(pf, enable_rx);
354 /* Trigger an immediate software interrupt to ensure that timestamps
355 * which occurred during reset are handled now.
357 wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
362 * ice_ptp_read_src_clk_reg - Read the source clock register
363 * @pf: Board private structure
364 * @sts: Optional parameter for holding a pair of system timestamps from
365 * the system clock. Will be ignored if NULL is given.
368 ice_ptp_read_src_clk_reg(struct ice_pf *pf, struct ptp_system_timestamp *sts)
370 struct ice_hw *hw = &pf->hw;
374 tmr_idx = ice_get_ptp_src_clock_index(hw);
375 guard(spinlock)(&pf->adapter->ptp_gltsyn_time_lock);
376 /* Read the system timestamp pre PHC read */
377 ptp_read_system_prets(sts);
379 lo = rd32(hw, GLTSYN_TIME_L(tmr_idx));
381 /* Read the system timestamp post PHC read */
382 ptp_read_system_postts(sts);
384 hi = rd32(hw, GLTSYN_TIME_H(tmr_idx));
385 lo2 = rd32(hw, GLTSYN_TIME_L(tmr_idx));
388 /* if TIME_L rolled over read TIME_L again and update
391 ptp_read_system_prets(sts);
392 lo = rd32(hw, GLTSYN_TIME_L(tmr_idx));
393 ptp_read_system_postts(sts);
394 hi = rd32(hw, GLTSYN_TIME_H(tmr_idx));
397 return ((u64)hi << 32) | lo;
401 * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b
402 * @cached_phc_time: recently cached copy of PHC time
403 * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value
405 * Hardware captures timestamps which contain only 32 bits of nominal
406 * nanoseconds, as opposed to the 64bit timestamps that the stack expects.
407 * Note that the captured timestamp values may be 40 bits, but the lower
408 * 8 bits are sub-nanoseconds and generally discarded.
410 * Extend the 32bit nanosecond timestamp using the following algorithm and
413 * 1) have a recently cached copy of the PHC time
414 * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1
415 * seconds) before or after the PHC time was captured.
416 * 3) calculate the delta between the cached time and the timestamp
417 * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was
418 * captured after the PHC time. In this case, the full timestamp is just
419 * the cached PHC time plus the delta.
420 * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the
421 * timestamp was captured *before* the PHC time, i.e. because the PHC
422 * cache was updated after the timestamp was captured by hardware. In this
423 * case, the full timestamp is the cached time minus the inverse delta.
425 * This algorithm works even if the PHC time was updated after a Tx timestamp
426 * was requested, but before the Tx timestamp event was reported from
429 * This calculation primarily relies on keeping the cached PHC time up to
430 * date. If the timestamp was captured more than 2^31 nanoseconds after the
431 * PHC time, it is possible that the lower 32bits of PHC time have
432 * overflowed more than once, and we might generate an incorrect timestamp.
434 * This is prevented by (a) periodically updating the cached PHC time once
435 * a second, and (b) discarding any Tx timestamp packet if it has waited for
436 * a timestamp for more than one second.
438 static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp)
440 u32 delta, phc_time_lo;
443 /* Extract the lower 32 bits of the PHC time */
444 phc_time_lo = (u32)cached_phc_time;
446 /* Calculate the delta between the lower 32bits of the cached PHC
447 * time and the in_tstamp value
449 delta = (in_tstamp - phc_time_lo);
451 /* Do not assume that the in_tstamp is always more recent than the
452 * cached PHC time. If the delta is large, it indicates that the
453 * in_tstamp was taken in the past, and should be converted
456 if (delta > (U32_MAX / 2)) {
457 /* reverse the delta calculation here */
458 delta = (phc_time_lo - in_tstamp);
459 ns = cached_phc_time - delta;
461 ns = cached_phc_time + delta;
468 * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds
469 * @pf: Board private structure
470 * @in_tstamp: Ingress/egress 40b timestamp value
472 * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal
473 * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit.
475 * *--------------------------------------------------------------*
476 * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v |
477 * *--------------------------------------------------------------*
479 * The low bit is an indicator of whether the timestamp is valid. The next
480 * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow,
481 * and the remaining 32 bits are the lower 32 bits of the PHC timer.
483 * It is assumed that the caller verifies the timestamp is valid prior to
484 * calling this function.
486 * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC
487 * time stored in the device private PTP structure as the basis for timestamp
490 * See ice_ptp_extend_32b_ts for a detailed explanation of the extension
493 static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp)
495 const u64 mask = GENMASK_ULL(31, 0);
496 unsigned long discard_time;
498 /* Discard the hardware timestamp if the cached PHC time is too old */
499 discard_time = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000);
500 if (time_is_before_jiffies(discard_time)) {
501 pf->ptp.tx_hwtstamp_discarded++;
505 return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time,
506 (in_tstamp >> 8) & mask);
510 * ice_ptp_is_tx_tracker_up - Check if Tx tracker is ready for new timestamps
511 * @tx: the PTP Tx timestamp tracker to check
513 * Check that a given PTP Tx timestamp tracker is up, i.e. that it is ready
514 * to accept new timestamp requests.
516 * Assumes the tx->lock spinlock is already held.
519 ice_ptp_is_tx_tracker_up(struct ice_ptp_tx *tx)
521 lockdep_assert_held(&tx->lock);
523 return tx->init && !tx->calibrating;
527 * ice_ptp_req_tx_single_tstamp - Request Tx timestamp for a port from FW
528 * @tx: the PTP Tx timestamp tracker
529 * @idx: index of the timestamp to request
531 void ice_ptp_req_tx_single_tstamp(struct ice_ptp_tx *tx, u8 idx)
533 struct ice_ptp_port *ptp_port;
540 ptp_port = container_of(tx, struct ice_ptp_port, tx);
541 pf = ptp_port_to_pf(ptp_port);
543 /* Drop packets which have waited for more than 2 seconds */
544 if (time_is_before_jiffies(tx->tstamps[idx].start + 2 * HZ)) {
545 /* Count the number of Tx timestamps that timed out */
546 pf->ptp.tx_hwtstamp_timeouts++;
548 skb = tx->tstamps[idx].skb;
549 tx->tstamps[idx].skb = NULL;
550 clear_bit(idx, tx->in_use);
552 dev_kfree_skb_any(skb);
556 ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx);
558 /* Write TS index to read to the PF register so the FW can read it */
559 wr32(&pf->hw, PF_SB_ATQBAL,
560 TS_LL_READ_TS_INTR | FIELD_PREP(TS_LL_READ_TS_IDX, idx) |
562 tx->last_ll_ts_idx_read = idx;
566 * ice_ptp_complete_tx_single_tstamp - Complete Tx timestamp for a port
567 * @tx: the PTP Tx timestamp tracker
569 void ice_ptp_complete_tx_single_tstamp(struct ice_ptp_tx *tx)
571 struct skb_shared_hwtstamps shhwtstamps = {};
572 u8 idx = tx->last_ll_ts_idx_read;
573 struct ice_ptp_port *ptp_port;
574 u64 raw_tstamp, tstamp;
575 bool drop_ts = false;
580 if (!tx->init || tx->last_ll_ts_idx_read < 0)
583 ptp_port = container_of(tx, struct ice_ptp_port, tx);
584 pf = ptp_port_to_pf(ptp_port);
586 ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx);
588 val = rd32(&pf->hw, PF_SB_ATQBAL);
590 /* When the bit is cleared, the TS is ready in the register */
591 if (val & TS_LL_READ_TS) {
592 dev_err(ice_pf_to_dev(pf), "Failed to get the Tx tstamp - FW not ready");
596 /* High 8 bit value of the TS is on the bits 16:23 */
597 raw_tstamp = FIELD_GET(TS_LL_READ_TS_HIGH, val);
600 /* Read the low 32 bit value */
601 raw_tstamp |= (u64)rd32(&pf->hw, PF_SB_ATQBAH);
603 /* Devices using this interface always verify the timestamp differs
604 * relative to the last cached timestamp value.
606 if (raw_tstamp == tx->tstamps[idx].cached_tstamp)
609 tx->tstamps[idx].cached_tstamp = raw_tstamp;
610 clear_bit(idx, tx->in_use);
611 skb = tx->tstamps[idx].skb;
612 tx->tstamps[idx].skb = NULL;
613 if (test_and_clear_bit(idx, tx->stale))
620 dev_kfree_skb_any(skb);
624 /* Extend the timestamp using cached PHC time */
625 tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp);
627 shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
628 ice_trace(tx_tstamp_complete, skb, idx);
631 skb_tstamp_tx(skb, &shhwtstamps);
632 dev_kfree_skb_any(skb);
636 * ice_ptp_process_tx_tstamp - Process Tx timestamps for a port
637 * @tx: the PTP Tx timestamp tracker
639 * Process timestamps captured by the PHY associated with this port. To do
640 * this, loop over each index with a waiting skb.
642 * If a given index has a valid timestamp, perform the following steps:
644 * 1) check that the timestamp request is not stale
645 * 2) check that a timestamp is ready and available in the PHY memory bank
646 * 3) read and copy the timestamp out of the PHY register
647 * 4) unlock the index by clearing the associated in_use bit
648 * 5) check if the timestamp is stale, and discard if so
649 * 6) extend the 40 bit timestamp value to get a 64 bit timestamp value
650 * 7) send this 64 bit timestamp to the stack
652 * Note that we do not hold the tracking lock while reading the Tx timestamp.
653 * This is because reading the timestamp requires taking a mutex that might
656 * The only place where we set in_use is when a new timestamp is initiated
657 * with a slot index. This is only called in the hard xmit routine where an
658 * SKB has a request flag set. The only places where we clear this bit is this
659 * function, or during teardown when the Tx timestamp tracker is being
660 * removed. A timestamp index will never be re-used until the in_use bit for
661 * that index is cleared.
663 * If a Tx thread starts a new timestamp, we might not begin processing it
664 * right away but we will notice it at the end when we re-queue the task.
666 * If a Tx thread starts a new timestamp just after this function exits, the
667 * interrupt for that timestamp should re-trigger this function once
668 * a timestamp is ready.
670 * In cases where the PTP hardware clock was directly adjusted, some
671 * timestamps may not be able to safely use the timestamp extension math. In
672 * this case, software will set the stale bit for any outstanding Tx
673 * timestamps when the clock is adjusted. Then this function will discard
674 * those captured timestamps instead of sending them to the stack.
676 * If a Tx packet has been waiting for more than 2 seconds, it is not possible
677 * to correctly extend the timestamp using the cached PHC time. It is
678 * extremely unlikely that a packet will ever take this long to timestamp. If
679 * we detect a Tx timestamp request that has waited for this long we assume
680 * the packet will never be sent by hardware and discard it without reading
681 * the timestamp register.
683 static void ice_ptp_process_tx_tstamp(struct ice_ptp_tx *tx)
685 struct ice_ptp_port *ptp_port;
694 ptp_port = container_of(tx, struct ice_ptp_port, tx);
695 pf = ptp_port_to_pf(ptp_port);
698 /* Read the Tx ready status first */
699 if (tx->has_ready_bitmap) {
700 err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready);
705 /* Drop packets if the link went down */
706 link_up = ptp_port->link_up;
708 for_each_set_bit(idx, tx->in_use, tx->len) {
709 struct skb_shared_hwtstamps shhwtstamps = {};
710 u8 phy_idx = idx + tx->offset;
711 u64 raw_tstamp = 0, tstamp;
712 bool drop_ts = !link_up;
715 /* Drop packets which have waited for more than 2 seconds */
716 if (time_is_before_jiffies(tx->tstamps[idx].start + 2 * HZ)) {
719 /* Count the number of Tx timestamps that timed out */
720 pf->ptp.tx_hwtstamp_timeouts++;
723 /* Only read a timestamp from the PHY if its marked as ready
724 * by the tstamp_ready register. This avoids unnecessary
725 * reading of timestamps which are not yet valid. This is
726 * important as we must read all timestamps which are valid
727 * and only timestamps which are valid during each interrupt.
728 * If we do not, the hardware logic for generating a new
729 * interrupt can get stuck on some devices.
731 if (tx->has_ready_bitmap &&
732 !(tstamp_ready & BIT_ULL(phy_idx))) {
739 ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx);
741 err = ice_read_phy_tstamp(hw, tx->block, phy_idx, &raw_tstamp);
745 ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx);
747 /* For PHYs which don't implement a proper timestamp ready
748 * bitmap, verify that the timestamp value is different
749 * from the last cached timestamp. If it is not, skip this for
750 * now assuming it hasn't yet been captured by hardware.
752 if (!drop_ts && !tx->has_ready_bitmap &&
753 raw_tstamp == tx->tstamps[idx].cached_tstamp)
756 /* Discard any timestamp value without the valid bit set */
757 if (!(raw_tstamp & ICE_PTP_TS_VALID))
761 spin_lock_irqsave(&tx->lock, flags);
762 if (!tx->has_ready_bitmap && raw_tstamp)
763 tx->tstamps[idx].cached_tstamp = raw_tstamp;
764 clear_bit(idx, tx->in_use);
765 skb = tx->tstamps[idx].skb;
766 tx->tstamps[idx].skb = NULL;
767 if (test_and_clear_bit(idx, tx->stale))
769 spin_unlock_irqrestore(&tx->lock, flags);
771 /* It is unlikely but possible that the SKB will have been
772 * flushed at this point due to link change or teardown.
778 dev_kfree_skb_any(skb);
782 /* Extend the timestamp using cached PHC time */
783 tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp);
785 shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
786 ice_trace(tx_tstamp_complete, skb, idx);
789 skb_tstamp_tx(skb, &shhwtstamps);
790 dev_kfree_skb_any(skb);
795 * ice_ptp_tx_tstamp_owner - Process Tx timestamps for all ports on the device
796 * @pf: Board private structure
798 static enum ice_tx_tstamp_work ice_ptp_tx_tstamp_owner(struct ice_pf *pf)
800 struct ice_ptp_port *port;
803 mutex_lock(&pf->ptp.ports_owner.lock);
804 list_for_each_entry(port, &pf->ptp.ports_owner.ports, list_member) {
805 struct ice_ptp_tx *tx = &port->tx;
807 if (!tx || !tx->init)
810 ice_ptp_process_tx_tstamp(tx);
812 mutex_unlock(&pf->ptp.ports_owner.lock);
814 for (i = 0; i < ICE_GET_QUAD_NUM(pf->hw.ptp.num_lports); i++) {
818 /* Read the Tx ready status first */
819 err = ice_get_phy_tx_tstamp_ready(&pf->hw, i, &tstamp_ready);
822 else if (tstamp_ready)
823 return ICE_TX_TSTAMP_WORK_PENDING;
826 return ICE_TX_TSTAMP_WORK_DONE;
830 * ice_ptp_tx_tstamp - Process Tx timestamps for this function.
831 * @tx: Tx tracking structure to initialize
833 * Returns: ICE_TX_TSTAMP_WORK_PENDING if there are any outstanding incomplete
834 * Tx timestamps, or ICE_TX_TSTAMP_WORK_DONE otherwise.
836 static enum ice_tx_tstamp_work ice_ptp_tx_tstamp(struct ice_ptp_tx *tx)
838 bool more_timestamps;
842 return ICE_TX_TSTAMP_WORK_DONE;
844 /* Process the Tx timestamp tracker */
845 ice_ptp_process_tx_tstamp(tx);
847 /* Check if there are outstanding Tx timestamps */
848 spin_lock_irqsave(&tx->lock, flags);
849 more_timestamps = tx->init && !bitmap_empty(tx->in_use, tx->len);
850 spin_unlock_irqrestore(&tx->lock, flags);
853 return ICE_TX_TSTAMP_WORK_PENDING;
855 return ICE_TX_TSTAMP_WORK_DONE;
859 * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps
860 * @tx: Tx tracking structure to initialize
862 * Assumes that the length has already been initialized. Do not call directly,
863 * use the ice_ptp_init_tx_* instead.
866 ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx)
868 unsigned long *in_use, *stale;
869 struct ice_tx_tstamp *tstamps;
871 tstamps = kcalloc(tx->len, sizeof(*tstamps), GFP_KERNEL);
872 in_use = bitmap_zalloc(tx->len, GFP_KERNEL);
873 stale = bitmap_zalloc(tx->len, GFP_KERNEL);
875 if (!tstamps || !in_use || !stale) {
883 tx->tstamps = tstamps;
887 tx->last_ll_ts_idx_read = -1;
889 spin_lock_init(&tx->lock);
895 * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker
896 * @pf: Board private structure
897 * @tx: the tracker to flush
899 * Called during teardown when a Tx tracker is being removed.
902 ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
904 struct ice_hw *hw = &pf->hw;
910 err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready);
912 dev_dbg(ice_pf_to_dev(pf), "Failed to get the Tx tstamp ready bitmap for block %u, err %d\n",
915 /* If we fail to read the Tx timestamp ready bitmap just
916 * skip clearing the PHY timestamps.
921 for_each_set_bit(idx, tx->in_use, tx->len) {
922 u8 phy_idx = idx + tx->offset;
925 /* In case this timestamp is ready, we need to clear it. */
926 if (!hw->reset_ongoing && (tstamp_ready & BIT_ULL(phy_idx)))
927 ice_clear_phy_tstamp(hw, tx->block, phy_idx);
929 spin_lock_irqsave(&tx->lock, flags);
930 skb = tx->tstamps[idx].skb;
931 tx->tstamps[idx].skb = NULL;
932 clear_bit(idx, tx->in_use);
933 clear_bit(idx, tx->stale);
934 spin_unlock_irqrestore(&tx->lock, flags);
936 /* Count the number of Tx timestamps flushed */
937 pf->ptp.tx_hwtstamp_flushed++;
939 /* Free the SKB after we've cleared the bit */
940 dev_kfree_skb_any(skb);
945 * ice_ptp_mark_tx_tracker_stale - Mark unfinished timestamps as stale
946 * @tx: the tracker to mark
948 * Mark currently outstanding Tx timestamps as stale. This prevents sending
949 * their timestamp value to the stack. This is required to prevent extending
950 * the 40bit hardware timestamp incorrectly.
952 * This should be called when the PTP clock is modified such as after a set
956 ice_ptp_mark_tx_tracker_stale(struct ice_ptp_tx *tx)
960 spin_lock_irqsave(&tx->lock, flags);
961 bitmap_or(tx->stale, tx->stale, tx->in_use, tx->len);
962 spin_unlock_irqrestore(&tx->lock, flags);
966 * ice_ptp_flush_all_tx_tracker - Flush all timestamp trackers on this clock
967 * @pf: Board private structure
969 * Called by the clock owner to flush all the Tx timestamp trackers associated
973 ice_ptp_flush_all_tx_tracker(struct ice_pf *pf)
975 struct ice_ptp_port *port;
977 list_for_each_entry(port, &pf->ptp.ports_owner.ports, list_member)
978 ice_ptp_flush_tx_tracker(ptp_port_to_pf(port), &port->tx);
982 * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker
983 * @pf: Board private structure
984 * @tx: Tx tracking structure to release
986 * Free memory associated with the Tx timestamp tracker.
989 ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
993 spin_lock_irqsave(&tx->lock, flags);
995 spin_unlock_irqrestore(&tx->lock, flags);
997 /* wait for potentially outstanding interrupt to complete */
998 synchronize_irq(pf->oicr_irq.virq);
1000 ice_ptp_flush_tx_tracker(pf, tx);
1005 bitmap_free(tx->in_use);
1008 bitmap_free(tx->stale);
1015 * ice_ptp_init_tx_eth56g - Initialize tracking for Tx timestamps
1016 * @pf: Board private structure
1017 * @tx: the Tx tracking structure to initialize
1018 * @port: the port this structure tracks
1020 * Initialize the Tx timestamp tracker for this port. ETH56G PHYs
1021 * have independent memory blocks for all ports.
1023 * Return: 0 for success, -ENOMEM when failed to allocate Tx tracker
1025 static int ice_ptp_init_tx_eth56g(struct ice_pf *pf, struct ice_ptp_tx *tx,
1030 tx->len = INDEX_PER_PORT_ETH56G;
1031 tx->has_ready_bitmap = 1;
1033 return ice_ptp_alloc_tx_tracker(tx);
1037 * ice_ptp_init_tx_e82x - Initialize tracking for Tx timestamps
1038 * @pf: Board private structure
1039 * @tx: the Tx tracking structure to initialize
1040 * @port: the port this structure tracks
1042 * Initialize the Tx timestamp tracker for this port. For generic MAC devices,
1043 * the timestamp block is shared for all ports in the same quad. To avoid
1044 * ports using the same timestamp index, logically break the block of
1045 * registers into chunks based on the port number.
1048 ice_ptp_init_tx_e82x(struct ice_pf *pf, struct ice_ptp_tx *tx, u8 port)
1050 tx->block = ICE_GET_QUAD_NUM(port);
1051 tx->offset = (port % ICE_PORTS_PER_QUAD) * INDEX_PER_PORT_E82X;
1052 tx->len = INDEX_PER_PORT_E82X;
1053 tx->has_ready_bitmap = 1;
1055 return ice_ptp_alloc_tx_tracker(tx);
1059 * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps
1060 * @pf: Board private structure
1061 * @tx: the Tx tracking structure to initialize
1063 * Initialize the Tx timestamp tracker for this PF. For E810 devices, each
1064 * port has its own block of timestamps, independent of the other ports.
1067 ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx)
1069 tx->block = pf->hw.port_info->lport;
1071 tx->len = INDEX_PER_PORT_E810;
1072 /* The E810 PHY does not provide a timestamp ready bitmap. Instead,
1073 * verify new timestamps against cached copy of the last read
1076 tx->has_ready_bitmap = 0;
1078 return ice_ptp_alloc_tx_tracker(tx);
1082 * ice_ptp_update_cached_phctime - Update the cached PHC time values
1083 * @pf: Board specific private structure
1085 * This function updates the system time values which are cached in the PF
1086 * structure and the Rx rings.
1088 * This function must be called periodically to ensure that the cached value
1089 * is never more than 2 seconds old.
1091 * Note that the cached copy in the PF PTP structure is always updated, even
1092 * if we can't update the copy in the Rx rings.
1095 * * 0 - OK, successfully updated
1096 * * -EAGAIN - PF was busy, need to reschedule the update
1098 static int ice_ptp_update_cached_phctime(struct ice_pf *pf)
1100 struct device *dev = ice_pf_to_dev(pf);
1101 unsigned long update_before;
1105 update_before = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000);
1106 if (pf->ptp.cached_phc_time &&
1107 time_is_before_jiffies(update_before)) {
1108 unsigned long time_taken = jiffies - pf->ptp.cached_phc_jiffies;
1110 dev_warn(dev, "%u msecs passed between update to cached PHC time\n",
1111 jiffies_to_msecs(time_taken));
1112 pf->ptp.late_cached_phc_updates++;
1115 /* Read the current PHC time */
1116 systime = ice_ptp_read_src_clk_reg(pf, NULL);
1118 /* Update the cached PHC time stored in the PF structure */
1119 WRITE_ONCE(pf->ptp.cached_phc_time, systime);
1120 WRITE_ONCE(pf->ptp.cached_phc_jiffies, jiffies);
1122 if (test_and_set_bit(ICE_CFG_BUSY, pf->state))
1125 ice_for_each_vsi(pf, i) {
1126 struct ice_vsi *vsi = pf->vsi[i];
1132 if (vsi->type != ICE_VSI_PF)
1135 ice_for_each_rxq(vsi, j) {
1136 if (!vsi->rx_rings[j])
1138 WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime);
1141 clear_bit(ICE_CFG_BUSY, pf->state);
1147 * ice_ptp_reset_cached_phctime - Reset cached PHC time after an update
1148 * @pf: Board specific private structure
1150 * This function must be called when the cached PHC time is no longer valid,
1151 * such as after a time adjustment. It marks any currently outstanding Tx
1152 * timestamps as stale and updates the cached PHC time for both the PF and Rx
1155 * If updating the PHC time cannot be done immediately, a warning message is
1156 * logged and the work item is scheduled immediately to minimize the window
1157 * with a wrong cached timestamp.
1159 static void ice_ptp_reset_cached_phctime(struct ice_pf *pf)
1161 struct device *dev = ice_pf_to_dev(pf);
1164 /* Update the cached PHC time immediately if possible, otherwise
1165 * schedule the work item to execute soon.
1167 err = ice_ptp_update_cached_phctime(pf);
1169 /* If another thread is updating the Rx rings, we won't
1170 * properly reset them here. This could lead to reporting of
1171 * invalid timestamps, but there isn't much we can do.
1173 dev_warn(dev, "%s: ICE_CFG_BUSY, unable to immediately update cached PHC time\n",
1176 /* Queue the work item to update the Rx rings when possible */
1177 kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work,
1178 msecs_to_jiffies(10));
1181 /* Mark any outstanding timestamps as stale, since they might have
1182 * been captured in hardware before the time update. This could lead
1183 * to us extending them with the wrong cached value resulting in
1184 * incorrect timestamp values.
1186 ice_ptp_mark_tx_tracker_stale(&pf->ptp.port.tx);
1190 * ice_ptp_write_init - Set PHC time to provided value
1191 * @pf: Board private structure
1192 * @ts: timespec structure that holds the new time value
1194 * Set the PHC time to the specified time provided in the timespec.
1196 static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts)
1198 u64 ns = timespec64_to_ns(ts);
1199 struct ice_hw *hw = &pf->hw;
1201 return ice_ptp_init_time(hw, ns);
1205 * ice_ptp_write_adj - Adjust PHC clock time atomically
1206 * @pf: Board private structure
1207 * @adj: Adjustment in nanoseconds
1209 * Perform an atomic adjustment of the PHC time by the specified number of
1212 static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj)
1214 struct ice_hw *hw = &pf->hw;
1216 return ice_ptp_adj_clock(hw, adj);
1220 * ice_base_incval - Get base timer increment value
1221 * @pf: Board private structure
1223 * Look up the base timer increment value for this device. The base increment
1224 * value is used to define the nominal clock tick rate. This increment value
1225 * is programmed during device initialization. It is also used as the basis
1226 * for calculating adjustments using scaled_ppm.
1228 static u64 ice_base_incval(struct ice_pf *pf)
1230 struct ice_hw *hw = &pf->hw;
1233 incval = ice_get_base_incval(hw);
1235 dev_dbg(ice_pf_to_dev(pf), "PTP: using base increment value of 0x%016llx\n",
1242 * ice_ptp_check_tx_fifo - Check whether Tx FIFO is in an OK state
1243 * @port: PTP port for which Tx FIFO is checked
1245 static int ice_ptp_check_tx_fifo(struct ice_ptp_port *port)
1247 int offs = port->port_num % ICE_PORTS_PER_QUAD;
1248 int quad = ICE_GET_QUAD_NUM(port->port_num);
1254 pf = ptp_port_to_pf(port);
1257 if (port->tx_fifo_busy_cnt == FIFO_OK)
1260 /* need to read FIFO state */
1261 if (offs == 0 || offs == 1)
1262 err = ice_read_quad_reg_e82x(hw, quad, Q_REG_FIFO01_STATUS,
1265 err = ice_read_quad_reg_e82x(hw, quad, Q_REG_FIFO23_STATUS,
1269 dev_err(ice_pf_to_dev(pf), "PTP failed to check port %d Tx FIFO, err %d\n",
1270 port->port_num, err);
1275 phy_sts = FIELD_GET(Q_REG_FIFO13_M, val);
1277 phy_sts = FIELD_GET(Q_REG_FIFO02_M, val);
1279 if (phy_sts & FIFO_EMPTY) {
1280 port->tx_fifo_busy_cnt = FIFO_OK;
1284 port->tx_fifo_busy_cnt++;
1286 dev_dbg(ice_pf_to_dev(pf), "Try %d, port %d FIFO not empty\n",
1287 port->tx_fifo_busy_cnt, port->port_num);
1289 if (port->tx_fifo_busy_cnt == ICE_PTP_FIFO_NUM_CHECKS) {
1290 dev_dbg(ice_pf_to_dev(pf),
1291 "Port %d Tx FIFO still not empty; resetting quad %d\n",
1292 port->port_num, quad);
1293 ice_ptp_reset_ts_memory_quad_e82x(hw, quad);
1294 port->tx_fifo_busy_cnt = FIFO_OK;
1302 * ice_ptp_wait_for_offsets - Check for valid Tx and Rx offsets
1303 * @work: Pointer to the kthread_work structure for this task
1305 * Check whether hardware has completed measuring the Tx and Rx offset values
1306 * used to configure and enable vernier timestamp calibration.
1308 * Once the offset in either direction is measured, configure the associated
1309 * registers with the calibrated offset values and enable timestamping. The Tx
1310 * and Rx directions are configured independently as soon as their associated
1311 * offsets are known.
1313 * This function reschedules itself until both Tx and Rx calibration have
1316 static void ice_ptp_wait_for_offsets(struct kthread_work *work)
1318 struct ice_ptp_port *port;
1324 port = container_of(work, struct ice_ptp_port, ov_work.work);
1325 pf = ptp_port_to_pf(port);
1328 if (ice_is_reset_in_progress(pf->state)) {
1329 /* wait for device driver to complete reset */
1330 kthread_queue_delayed_work(pf->ptp.kworker,
1332 msecs_to_jiffies(100));
1336 tx_err = ice_ptp_check_tx_fifo(port);
1338 tx_err = ice_phy_cfg_tx_offset_e82x(hw, port->port_num);
1339 rx_err = ice_phy_cfg_rx_offset_e82x(hw, port->port_num);
1340 if (tx_err || rx_err) {
1341 /* Tx and/or Rx offset not yet configured, try again later */
1342 kthread_queue_delayed_work(pf->ptp.kworker,
1344 msecs_to_jiffies(100));
1350 * ice_ptp_port_phy_stop - Stop timestamping for a PHY port
1351 * @ptp_port: PTP port to stop
1354 ice_ptp_port_phy_stop(struct ice_ptp_port *ptp_port)
1356 struct ice_pf *pf = ptp_port_to_pf(ptp_port);
1357 u8 port = ptp_port->port_num;
1358 struct ice_hw *hw = &pf->hw;
1361 if (ice_is_e810(hw))
1364 mutex_lock(&ptp_port->ps_lock);
1366 switch (hw->ptp.phy_model) {
1367 case ICE_PHY_ETH56G:
1368 err = ice_stop_phy_timer_eth56g(hw, port, true);
1371 kthread_cancel_delayed_work_sync(&ptp_port->ov_work);
1373 err = ice_stop_phy_timer_e82x(hw, port, true);
1378 if (err && err != -EBUSY)
1379 dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d down, err %d\n",
1382 mutex_unlock(&ptp_port->ps_lock);
1388 * ice_ptp_port_phy_restart - (Re)start and calibrate PHY timestamping
1389 * @ptp_port: PTP port for which the PHY start is set
1391 * Start the PHY timestamping block, and initiate Vernier timestamping
1392 * calibration. If timestamping cannot be calibrated (such as if link is down)
1393 * then disable the timestamping block instead.
1396 ice_ptp_port_phy_restart(struct ice_ptp_port *ptp_port)
1398 struct ice_pf *pf = ptp_port_to_pf(ptp_port);
1399 u8 port = ptp_port->port_num;
1400 struct ice_hw *hw = &pf->hw;
1401 unsigned long flags;
1404 if (ice_is_e810(hw))
1407 if (!ptp_port->link_up)
1408 return ice_ptp_port_phy_stop(ptp_port);
1410 mutex_lock(&ptp_port->ps_lock);
1412 switch (hw->ptp.phy_model) {
1413 case ICE_PHY_ETH56G:
1414 err = ice_start_phy_timer_eth56g(hw, port);
1417 /* Start the PHY timer in Vernier mode */
1418 kthread_cancel_delayed_work_sync(&ptp_port->ov_work);
1420 /* temporarily disable Tx timestamps while calibrating
1423 spin_lock_irqsave(&ptp_port->tx.lock, flags);
1424 ptp_port->tx.calibrating = true;
1425 spin_unlock_irqrestore(&ptp_port->tx.lock, flags);
1426 ptp_port->tx_fifo_busy_cnt = 0;
1428 /* Start the PHY timer in Vernier mode */
1429 err = ice_start_phy_timer_e82x(hw, port);
1433 /* Enable Tx timestamps right away */
1434 spin_lock_irqsave(&ptp_port->tx.lock, flags);
1435 ptp_port->tx.calibrating = false;
1436 spin_unlock_irqrestore(&ptp_port->tx.lock, flags);
1438 kthread_queue_delayed_work(pf->ptp.kworker, &ptp_port->ov_work,
1446 dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d up, err %d\n",
1449 mutex_unlock(&ptp_port->ps_lock);
1455 * ice_ptp_link_change - Reconfigure PTP after link status change
1456 * @pf: Board private structure
1457 * @port: Port for which the PHY start is set
1458 * @linkup: Link is up or down
1460 void ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup)
1462 struct ice_ptp_port *ptp_port;
1463 struct ice_hw *hw = &pf->hw;
1465 if (pf->ptp.state != ICE_PTP_READY)
1468 if (WARN_ON_ONCE(port >= hw->ptp.num_lports))
1471 ptp_port = &pf->ptp.port;
1472 if (ice_is_e825c(hw) && hw->ptp.is_2x50g_muxed_topo)
1474 if (WARN_ON_ONCE(ptp_port->port_num != port))
1477 /* Update cached link status for this port immediately */
1478 ptp_port->link_up = linkup;
1480 switch (hw->ptp.phy_model) {
1482 /* Do not reconfigure E810 PHY */
1484 case ICE_PHY_ETH56G:
1486 ice_ptp_port_phy_restart(ptp_port);
1489 dev_warn(ice_pf_to_dev(pf), "%s: Unknown PHY type\n", __func__);
1494 * ice_ptp_cfg_phy_interrupt - Configure PHY interrupt settings
1495 * @pf: PF private structure
1496 * @ena: bool value to enable or disable interrupt
1497 * @threshold: Minimum number of packets at which intr is triggered
1499 * Utility function to configure all the PHY interrupt settings, including
1500 * whether the PHY interrupt is enabled, and what threshold to use. Also
1501 * configures The E82X timestamp owner to react to interrupts from all PHYs.
1503 * Return: 0 on success, -EOPNOTSUPP when PHY model incorrect, other error codes
1504 * when failed to configure PHY interrupt for E82X
1506 static int ice_ptp_cfg_phy_interrupt(struct ice_pf *pf, bool ena, u32 threshold)
1508 struct device *dev = ice_pf_to_dev(pf);
1509 struct ice_hw *hw = &pf->hw;
1511 ice_ptp_reset_ts_memory(hw);
1513 switch (hw->ptp.phy_model) {
1514 case ICE_PHY_ETH56G: {
1517 for (port = 0; port < hw->ptp.num_lports; port++) {
1520 err = ice_phy_cfg_intr_eth56g(hw, port, ena, threshold);
1522 dev_err(dev, "Failed to configure PHY interrupt for port %d, err %d\n",
1530 case ICE_PHY_E82X: {
1533 for (quad = 0; quad < ICE_GET_QUAD_NUM(hw->ptp.num_lports);
1537 err = ice_phy_cfg_intr_e82x(hw, quad, ena, threshold);
1539 dev_err(dev, "Failed to configure PHY interrupt for quad %d, err %d\n",
1551 dev_warn(dev, "%s: Unexpected PHY model %d\n", __func__,
1558 * ice_ptp_reset_phy_timestamping - Reset PHY timestamping block
1559 * @pf: Board private structure
1561 static void ice_ptp_reset_phy_timestamping(struct ice_pf *pf)
1563 ice_ptp_port_phy_restart(&pf->ptp.port);
1567 * ice_ptp_restart_all_phy - Restart all PHYs to recalibrate timestamping
1568 * @pf: Board private structure
1570 static void ice_ptp_restart_all_phy(struct ice_pf *pf)
1572 struct list_head *entry;
1574 list_for_each(entry, &pf->ptp.ports_owner.ports) {
1575 struct ice_ptp_port *port = list_entry(entry,
1576 struct ice_ptp_port,
1580 ice_ptp_port_phy_restart(port);
1585 * ice_ptp_adjfine - Adjust clock increment rate
1586 * @info: the driver's PTP info structure
1587 * @scaled_ppm: Parts per million with 16-bit fractional field
1589 * Adjust the frequency of the clock by the indicated scaled ppm from the
1592 static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
1594 struct ice_pf *pf = ptp_info_to_pf(info);
1595 struct ice_hw *hw = &pf->hw;
1599 incval = adjust_by_scaled_ppm(ice_base_incval(pf), scaled_ppm);
1600 err = ice_ptp_write_incval_locked(hw, incval);
1602 dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n",
1611 * ice_ptp_extts_event - Process PTP external clock event
1612 * @pf: Board private structure
1614 void ice_ptp_extts_event(struct ice_pf *pf)
1616 struct ptp_clock_event event;
1617 struct ice_hw *hw = &pf->hw;
1621 /* Don't process timestamp events if PTP is not ready */
1622 if (pf->ptp.state != ICE_PTP_READY)
1625 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1626 /* Event time is captured by one of the two matched registers
1627 * GLTSYN_EVNT_L: 32 LSB of sampled time event
1628 * GLTSYN_EVNT_H: 32 MSB of sampled time event
1629 * Event is defined in GLTSYN_EVNT_0 register
1631 for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) {
1632 /* Check if channel is enabled */
1633 if (pf->ptp.ext_ts_irq & (1 << chan)) {
1634 lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx));
1635 hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx));
1636 event.timestamp = (((u64)hi) << 32) | lo;
1637 event.type = PTP_CLOCK_EXTTS;
1641 ptp_clock_event(pf->ptp.clock, &event);
1642 pf->ptp.ext_ts_irq &= ~(1 << chan);
1648 * ice_ptp_cfg_extts - Configure EXTTS pin and channel
1649 * @pf: Board private structure
1650 * @chan: GPIO channel (0-3)
1651 * @config: desired EXTTS configuration.
1652 * @store: If set to true, the values will be stored
1654 * Configure an external timestamp event on the requested channel.
1656 * Return: 0 on success, -EOPNOTUSPP on unsupported flags
1658 static int ice_ptp_cfg_extts(struct ice_pf *pf, unsigned int chan,
1659 struct ice_extts_channel *config, bool store)
1661 u32 func, aux_reg, gpio_reg, irq_reg;
1662 struct ice_hw *hw = &pf->hw;
1665 /* Reject requests with unsupported flags */
1666 if (config->flags & ~(PTP_ENABLE_FEATURE |
1672 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1674 irq_reg = rd32(hw, PFINT_OICR_ENA);
1677 /* Enable the interrupt */
1678 irq_reg |= PFINT_OICR_TSYN_EVNT_M;
1679 aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M;
1681 #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE BIT(0)
1682 #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE BIT(1)
1684 /* set event level to requested edge */
1685 if (config->flags & PTP_FALLING_EDGE)
1686 aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE;
1687 if (config->flags & PTP_RISING_EDGE)
1688 aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE;
1690 /* Write GPIO CTL reg.
1691 * 0x1 is input sampled by EVENT register(channel)
1692 * + num_in_channels * tmr_idx
1694 func = 1 + chan + (tmr_idx * 3);
1695 gpio_reg = FIELD_PREP(GLGEN_GPIO_CTL_PIN_FUNC_M, func);
1696 pf->ptp.ext_ts_chan |= (1 << chan);
1698 /* clear the values we set to reset defaults */
1701 pf->ptp.ext_ts_chan &= ~(1 << chan);
1702 if (!pf->ptp.ext_ts_chan)
1703 irq_reg &= ~PFINT_OICR_TSYN_EVNT_M;
1706 wr32(hw, PFINT_OICR_ENA, irq_reg);
1707 wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg);
1708 wr32(hw, GLGEN_GPIO_CTL(config->gpio_pin), gpio_reg);
1711 memcpy(&pf->ptp.extts_channels[chan], config, sizeof(*config));
1717 * ice_ptp_disable_all_extts - Disable all EXTTS channels
1718 * @pf: Board private structure
1720 static void ice_ptp_disable_all_extts(struct ice_pf *pf)
1722 struct ice_extts_channel extts_cfg = {};
1725 for (i = 0; i < pf->ptp.info.n_ext_ts; i++) {
1726 if (pf->ptp.extts_channels[i].ena) {
1727 extts_cfg.gpio_pin = pf->ptp.extts_channels[i].gpio_pin;
1728 extts_cfg.ena = false;
1729 ice_ptp_cfg_extts(pf, i, &extts_cfg, false);
1733 synchronize_irq(pf->oicr_irq.virq);
1737 * ice_ptp_enable_all_extts - Enable all EXTTS channels
1738 * @pf: Board private structure
1740 * Called during reset to restore user configuration.
1742 static void ice_ptp_enable_all_extts(struct ice_pf *pf)
1746 for (i = 0; i < pf->ptp.info.n_ext_ts; i++) {
1747 if (pf->ptp.extts_channels[i].ena)
1748 ice_ptp_cfg_extts(pf, i, &pf->ptp.extts_channels[i],
1754 * ice_ptp_cfg_clkout - Configure clock to generate periodic wave
1755 * @pf: Board private structure
1756 * @chan: GPIO channel (0-3)
1757 * @config: desired periodic clk configuration. NULL will disable channel
1758 * @store: If set to true the values will be stored
1760 * Configure the internal clock generator modules to generate the clock wave of
1763 static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan,
1764 struct ice_perout_channel *config, bool store)
1766 u64 current_time, period, start_time, phase;
1767 struct ice_hw *hw = &pf->hw;
1768 u32 func, val, gpio_pin;
1771 if (config && config->flags & ~PTP_PEROUT_PHASE)
1774 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1776 /* 0. Reset mode & out_en in AUX_OUT */
1777 wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0);
1779 /* If we're disabling the output, clear out CLKO and TGT and keep
1782 if (!config || !config->ena) {
1783 wr32(hw, GLTSYN_CLKO(chan, tmr_idx), 0);
1784 wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), 0);
1785 wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), 0);
1787 val = GLGEN_GPIO_CTL_PIN_DIR_M;
1788 gpio_pin = pf->ptp.perout_channels[chan].gpio_pin;
1789 wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
1791 /* Store the value if requested */
1793 memset(&pf->ptp.perout_channels[chan], 0,
1794 sizeof(struct ice_perout_channel));
1798 period = config->period;
1799 start_time = config->start_time;
1800 div64_u64_rem(start_time, period, &phase);
1801 gpio_pin = config->gpio_pin;
1803 /* 1. Write clkout with half of required period value */
1805 dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n");
1811 /* For proper operation, the GLTSYN_CLKO must be larger than clock tick
1814 if (period <= MIN_PULSE || period > U32_MAX) {
1815 dev_err(ice_pf_to_dev(pf), "CLK Period must be > %d && < 2^33",
1820 wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period));
1822 /* Allow time for programming before start_time is hit */
1823 current_time = ice_ptp_read_src_clk_reg(pf, NULL);
1825 /* if start time is in the past start the timer at the nearest second
1828 if (start_time < current_time)
1829 start_time = roundup_u64(current_time, NSEC_PER_SEC) + phase;
1831 if (ice_is_e810(hw))
1832 start_time -= E810_OUT_PROP_DELAY_NS;
1834 start_time -= ice_e82x_pps_delay(ice_e82x_time_ref(hw));
1836 /* 2. Write TARGET time */
1837 wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start_time));
1838 wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start_time));
1840 /* 3. Write AUX_OUT register */
1841 val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M;
1842 wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val);
1844 /* 4. write GPIO CTL reg */
1845 func = 8 + chan + (tmr_idx * 4);
1846 val = GLGEN_GPIO_CTL_PIN_DIR_M |
1847 FIELD_PREP(GLGEN_GPIO_CTL_PIN_FUNC_M, func);
1848 wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
1850 /* Store the value if requested */
1852 memcpy(&pf->ptp.perout_channels[chan], config,
1853 sizeof(struct ice_perout_channel));
1854 pf->ptp.perout_channels[chan].start_time = phase;
1859 dev_err(ice_pf_to_dev(pf), "PTP failed to cfg per_clk\n");
1864 * ice_ptp_disable_all_clkout - Disable all currently configured outputs
1865 * @pf: pointer to the PF structure
1867 * Disable all currently configured clock outputs. This is necessary before
1868 * certain changes to the PTP hardware clock. Use ice_ptp_enable_all_clkout to
1869 * re-enable the clocks again.
1871 static void ice_ptp_disable_all_clkout(struct ice_pf *pf)
1875 for (i = 0; i < pf->ptp.info.n_per_out; i++)
1876 if (pf->ptp.perout_channels[i].ena)
1877 ice_ptp_cfg_clkout(pf, i, NULL, false);
1881 * ice_ptp_enable_all_clkout - Enable all configured periodic clock outputs
1882 * @pf: pointer to the PF structure
1884 * Enable all currently configured clock outputs. Use this after
1885 * ice_ptp_disable_all_clkout to reconfigure the output signals according to
1886 * their configuration.
1888 static void ice_ptp_enable_all_clkout(struct ice_pf *pf)
1892 for (i = 0; i < pf->ptp.info.n_per_out; i++)
1893 if (pf->ptp.perout_channels[i].ena)
1894 ice_ptp_cfg_clkout(pf, i, &pf->ptp.perout_channels[i],
1899 * ice_ptp_gpio_enable_e810 - Enable/disable ancillary features of PHC
1900 * @info: the driver's PTP info structure
1901 * @rq: The requested feature to change
1902 * @on: Enable/disable flag
1905 ice_ptp_gpio_enable_e810(struct ptp_clock_info *info,
1906 struct ptp_clock_request *rq, int on)
1908 struct ice_pf *pf = ptp_info_to_pf(info);
1909 bool sma_pres = false;
1913 if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL))
1917 case PTP_CLK_REQ_PEROUT:
1919 struct ice_perout_channel clk_cfg = {};
1921 chan = rq->perout.index;
1923 if (chan == ice_pin_desc_e810t[SMA1].chan)
1924 clk_cfg.gpio_pin = GPIO_20;
1925 else if (chan == ice_pin_desc_e810t[SMA2].chan)
1926 clk_cfg.gpio_pin = GPIO_22;
1929 } else if (ice_is_e810t(&pf->hw)) {
1931 clk_cfg.gpio_pin = GPIO_20;
1933 clk_cfg.gpio_pin = GPIO_22;
1934 } else if (chan == PPS_CLK_GEN_CHAN) {
1935 clk_cfg.gpio_pin = PPS_PIN_INDEX;
1937 clk_cfg.gpio_pin = chan;
1940 clk_cfg.flags = rq->perout.flags;
1941 clk_cfg.period = ((rq->perout.period.sec * NSEC_PER_SEC) +
1942 rq->perout.period.nsec);
1943 clk_cfg.start_time = ((rq->perout.start.sec * NSEC_PER_SEC) +
1944 rq->perout.start.nsec);
1947 return ice_ptp_cfg_clkout(pf, chan, &clk_cfg, true);
1949 case PTP_CLK_REQ_EXTTS:
1951 struct ice_extts_channel extts_cfg = {};
1953 chan = rq->extts.index;
1955 if (chan < ice_pin_desc_e810t[SMA2].chan)
1959 } else if (ice_is_e810t(&pf->hw)) {
1968 extts_cfg.flags = rq->extts.flags;
1969 extts_cfg.gpio_pin = gpio_pin;
1970 extts_cfg.ena = !!on;
1972 return ice_ptp_cfg_extts(pf, chan, &extts_cfg, true);
1980 * ice_ptp_gpio_enable_e823 - Enable/disable ancillary features of PHC
1981 * @info: the driver's PTP info structure
1982 * @rq: The requested feature to change
1983 * @on: Enable/disable flag
1985 static int ice_ptp_gpio_enable_e823(struct ptp_clock_info *info,
1986 struct ptp_clock_request *rq, int on)
1988 struct ice_pf *pf = ptp_info_to_pf(info);
1991 case PTP_CLK_REQ_PPS:
1993 struct ice_perout_channel clk_cfg = {};
1995 clk_cfg.flags = rq->perout.flags;
1996 clk_cfg.gpio_pin = PPS_PIN_INDEX;
1997 clk_cfg.period = NSEC_PER_SEC;
2000 return ice_ptp_cfg_clkout(pf, PPS_CLK_GEN_CHAN, &clk_cfg, true);
2002 case PTP_CLK_REQ_EXTTS:
2004 struct ice_extts_channel extts_cfg = {};
2006 extts_cfg.flags = rq->extts.flags;
2007 extts_cfg.gpio_pin = TIME_SYNC_PIN_INDEX;
2008 extts_cfg.ena = !!on;
2010 return ice_ptp_cfg_extts(pf, rq->extts.index, &extts_cfg, true);
2018 * ice_ptp_gettimex64 - Get the time of the clock
2019 * @info: the driver's PTP info structure
2020 * @ts: timespec64 structure to hold the current time value
2021 * @sts: Optional parameter for holding a pair of system timestamps from
2022 * the system clock. Will be ignored if NULL is given.
2024 * Read the device clock and return the correct value on ns, after converting it
2025 * into a timespec struct.
2028 ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts,
2029 struct ptp_system_timestamp *sts)
2031 struct ice_pf *pf = ptp_info_to_pf(info);
2034 time_ns = ice_ptp_read_src_clk_reg(pf, sts);
2035 *ts = ns_to_timespec64(time_ns);
2040 * ice_ptp_settime64 - Set the time of the clock
2041 * @info: the driver's PTP info structure
2042 * @ts: timespec64 structure that holds the new time value
2044 * Set the device clock to the user input value. The conversion from timespec
2045 * to ns happens in the write function.
2048 ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts)
2050 struct ice_pf *pf = ptp_info_to_pf(info);
2051 struct timespec64 ts64 = *ts;
2052 struct ice_hw *hw = &pf->hw;
2055 /* For Vernier mode on E82X, we need to recalibrate after new settime.
2056 * Start with marking timestamps as invalid.
2058 if (hw->ptp.phy_model == ICE_PHY_E82X) {
2059 err = ice_ptp_clear_phy_offset_ready_e82x(hw);
2061 dev_warn(ice_pf_to_dev(pf), "Failed to mark timestamps as invalid before settime\n");
2064 if (!ice_ptp_lock(hw)) {
2069 /* Disable periodic outputs */
2070 ice_ptp_disable_all_clkout(pf);
2072 err = ice_ptp_write_init(pf, &ts64);
2076 ice_ptp_reset_cached_phctime(pf);
2078 /* Reenable periodic outputs */
2079 ice_ptp_enable_all_clkout(pf);
2081 /* Recalibrate and re-enable timestamp blocks for E822/E823 */
2082 if (hw->ptp.phy_model == ICE_PHY_E82X)
2083 ice_ptp_restart_all_phy(pf);
2086 dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err);
2094 * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment
2095 * @info: the driver's PTP info structure
2096 * @delta: Offset in nanoseconds to adjust the time by
2098 static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta)
2100 struct timespec64 now, then;
2103 then = ns_to_timespec64(delta);
2104 ret = ice_ptp_gettimex64(info, &now, NULL);
2107 now = timespec64_add(now, then);
2109 return ice_ptp_settime64(info, (const struct timespec64 *)&now);
2113 * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta
2114 * @info: the driver's PTP info structure
2115 * @delta: Offset in nanoseconds to adjust the time by
2117 static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
2119 struct ice_pf *pf = ptp_info_to_pf(info);
2120 struct ice_hw *hw = &pf->hw;
2124 dev = ice_pf_to_dev(pf);
2126 /* Hardware only supports atomic adjustments using signed 32-bit
2127 * integers. For any adjustment outside this range, perform
2128 * a non-atomic get->adjust->set flow.
2130 if (delta > S32_MAX || delta < S32_MIN) {
2131 dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta);
2132 return ice_ptp_adjtime_nonatomic(info, delta);
2135 if (!ice_ptp_lock(hw)) {
2136 dev_err(dev, "PTP failed to acquire semaphore in adjtime\n");
2140 /* Disable periodic outputs */
2141 ice_ptp_disable_all_clkout(pf);
2143 err = ice_ptp_write_adj(pf, delta);
2145 /* Reenable periodic outputs */
2146 ice_ptp_enable_all_clkout(pf);
2151 dev_err(dev, "PTP failed to adjust time, err %d\n", err);
2155 ice_ptp_reset_cached_phctime(pf);
2160 #ifdef CONFIG_ICE_HWTS
2162 * ice_ptp_get_syncdevicetime - Get the cross time stamp info
2163 * @device: Current device time
2164 * @system: System counter value read synchronously with device time
2165 * @ctx: Context provided by timekeeping code
2167 * Read device and system (ART) clock simultaneously and return the corrected
2168 * clock values in ns.
2171 ice_ptp_get_syncdevicetime(ktime_t *device,
2172 struct system_counterval_t *system,
2175 struct ice_pf *pf = (struct ice_pf *)ctx;
2176 struct ice_hw *hw = &pf->hw;
2177 u32 hh_lock, hh_art_ctl;
2180 #define MAX_HH_HW_LOCK_TRIES 5
2181 #define MAX_HH_CTL_LOCK_TRIES 100
2183 for (i = 0; i < MAX_HH_HW_LOCK_TRIES; i++) {
2184 /* Get the HW lock */
2185 hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
2186 if (hh_lock & PFHH_SEM_BUSY_M) {
2187 usleep_range(10000, 15000);
2192 if (hh_lock & PFHH_SEM_BUSY_M) {
2193 dev_err(ice_pf_to_dev(pf), "PTP failed to get hh lock\n");
2197 /* Program cmd to master timer */
2198 ice_ptp_src_cmd(hw, ICE_PTP_READ_TIME);
2200 /* Start the ART and device clock sync sequence */
2201 hh_art_ctl = rd32(hw, GLHH_ART_CTL);
2202 hh_art_ctl = hh_art_ctl | GLHH_ART_CTL_ACTIVE_M;
2203 wr32(hw, GLHH_ART_CTL, hh_art_ctl);
2205 for (i = 0; i < MAX_HH_CTL_LOCK_TRIES; i++) {
2206 /* Wait for sync to complete */
2207 hh_art_ctl = rd32(hw, GLHH_ART_CTL);
2208 if (hh_art_ctl & GLHH_ART_CTL_ACTIVE_M) {
2212 u32 hh_ts_lo, hh_ts_hi, tmr_idx;
2215 tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc;
2217 hh_ts_lo = rd32(hw, GLHH_ART_TIME_L);
2218 hh_ts_hi = rd32(hw, GLHH_ART_TIME_H);
2219 hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo;
2220 system->cycles = hh_ts;
2221 system->cs_id = CSID_X86_ART;
2222 /* Read Device source clock time */
2223 hh_ts_lo = rd32(hw, GLTSYN_HHTIME_L(tmr_idx));
2224 hh_ts_hi = rd32(hw, GLTSYN_HHTIME_H(tmr_idx));
2225 hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo;
2226 *device = ns_to_ktime(hh_ts);
2231 /* Clear the master timer */
2232 ice_ptp_src_cmd(hw, ICE_PTP_NOP);
2234 /* Release HW lock */
2235 hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
2236 hh_lock = hh_lock & ~PFHH_SEM_BUSY_M;
2237 wr32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), hh_lock);
2239 if (i == MAX_HH_CTL_LOCK_TRIES)
2246 * ice_ptp_getcrosststamp_e82x - Capture a device cross timestamp
2247 * @info: the driver's PTP info structure
2248 * @cts: The memory to fill the cross timestamp info
2250 * Capture a cross timestamp between the ART and the device PTP hardware
2251 * clock. Fill the cross timestamp information and report it back to the
2254 * This is only valid for E822 and E823 devices which have support for
2255 * generating the cross timestamp via PCIe PTM.
2257 * In order to correctly correlate the ART timestamp back to the TSC time, the
2258 * CPU must have X86_FEATURE_TSC_KNOWN_FREQ.
2261 ice_ptp_getcrosststamp_e82x(struct ptp_clock_info *info,
2262 struct system_device_crosststamp *cts)
2264 struct ice_pf *pf = ptp_info_to_pf(info);
2266 return get_device_system_crosststamp(ice_ptp_get_syncdevicetime,
2269 #endif /* CONFIG_ICE_HWTS */
2272 * ice_ptp_get_ts_config - ioctl interface to read the timestamping config
2273 * @pf: Board private structure
2276 * Copy the timestamping config to user buffer
2278 int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
2280 struct hwtstamp_config *config;
2282 if (pf->ptp.state != ICE_PTP_READY)
2285 config = &pf->ptp.tstamp_config;
2287 return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
2292 * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode
2293 * @pf: Board private structure
2294 * @config: hwtstamp settings requested or saved
2297 ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config)
2299 switch (config->tx_type) {
2300 case HWTSTAMP_TX_OFF:
2301 pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_OFF;
2303 case HWTSTAMP_TX_ON:
2304 pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_ON;
2310 switch (config->rx_filter) {
2311 case HWTSTAMP_FILTER_NONE:
2312 pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2314 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2315 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2316 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2317 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2318 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2319 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2320 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2321 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2322 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2323 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2324 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2325 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2326 case HWTSTAMP_FILTER_NTP_ALL:
2327 case HWTSTAMP_FILTER_ALL:
2328 pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_ALL;
2334 /* Immediately update the device timestamping mode */
2335 ice_ptp_restore_timestamp_mode(pf);
2341 * ice_ptp_set_ts_config - ioctl interface to control the timestamping
2342 * @pf: Board private structure
2345 * Get the user config and store it
2347 int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
2349 struct hwtstamp_config config;
2352 if (pf->ptp.state != ICE_PTP_READY)
2355 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2358 err = ice_ptp_set_timestamp_mode(pf, &config);
2362 /* Return the actual configuration set */
2363 config = pf->ptp.tstamp_config;
2365 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
2370 * ice_ptp_get_rx_hwts - Get packet Rx timestamp in ns
2371 * @rx_desc: Receive descriptor
2372 * @pkt_ctx: Packet context to get the cached time
2374 * The driver receives a notification in the receive descriptor with timestamp.
2376 u64 ice_ptp_get_rx_hwts(const union ice_32b_rx_flex_desc *rx_desc,
2377 const struct ice_pkt_ctx *pkt_ctx)
2379 u64 ts_ns, cached_time;
2382 if (!(rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID))
2385 cached_time = READ_ONCE(pkt_ctx->cached_phctime);
2387 /* Do not report a timestamp if we don't have a cached PHC time */
2391 /* Use ice_ptp_extend_32b_ts directly, using the ring-specific cached
2392 * PHC value, rather than accessing the PF. This also allows us to
2393 * simply pass the upper 32bits of nanoseconds directly. Calling
2394 * ice_ptp_extend_40b_ts is unnecessary as it would just discard these
2397 ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high);
2398 ts_ns = ice_ptp_extend_32b_ts(cached_time, ts_high);
2404 * ice_ptp_disable_sma_pins_e810t - Disable E810-T SMA pins
2405 * @pf: pointer to the PF structure
2406 * @info: PTP clock info structure
2408 * Disable the OS access to the SMA pins. Called to clear out the OS
2409 * indications of pin support when we fail to setup the E810-T SMA control
2413 ice_ptp_disable_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info)
2415 struct device *dev = ice_pf_to_dev(pf);
2417 dev_warn(dev, "Failed to configure E810-T SMA pin control\n");
2419 info->enable = NULL;
2420 info->verify = NULL;
2423 info->n_per_out = 0;
2427 * ice_ptp_setup_sma_pins_e810t - Setup the SMA pins
2428 * @pf: pointer to the PF structure
2429 * @info: PTP clock info structure
2431 * Finish setting up the SMA pins by allocating pin_config, and setting it up
2432 * according to the current status of the SMA. On failure, disable all of the
2433 * extended SMA pin support.
2436 ice_ptp_setup_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info)
2438 struct device *dev = ice_pf_to_dev(pf);
2441 /* Allocate memory for kernel pins interface */
2442 info->pin_config = devm_kcalloc(dev, info->n_pins,
2443 sizeof(*info->pin_config), GFP_KERNEL);
2444 if (!info->pin_config) {
2445 ice_ptp_disable_sma_pins_e810t(pf, info);
2449 /* Read current SMA status */
2450 err = ice_get_sma_config_e810t(&pf->hw, info->pin_config);
2452 ice_ptp_disable_sma_pins_e810t(pf, info);
2456 * ice_ptp_setup_pins_e810 - Setup PTP pins in sysfs
2457 * @pf: pointer to the PF instance
2458 * @info: PTP clock capabilities
2461 ice_ptp_setup_pins_e810(struct ice_pf *pf, struct ptp_clock_info *info)
2463 if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) {
2464 info->n_ext_ts = N_EXT_TS_E810;
2465 info->n_per_out = N_PER_OUT_E810T;
2466 info->n_pins = NUM_PTP_PINS_E810T;
2467 info->verify = ice_verify_pin_e810t;
2469 /* Complete setup of the SMA pins */
2470 ice_ptp_setup_sma_pins_e810t(pf, info);
2471 } else if (ice_is_e810t(&pf->hw)) {
2472 info->n_ext_ts = N_EXT_TS_NO_SMA_E810T;
2473 info->n_per_out = N_PER_OUT_NO_SMA_E810T;
2475 info->n_per_out = N_PER_OUT_E810;
2476 info->n_ext_ts = N_EXT_TS_E810;
2481 * ice_ptp_setup_pins_e823 - Setup PTP pins in sysfs
2482 * @pf: pointer to the PF instance
2483 * @info: PTP clock capabilities
2486 ice_ptp_setup_pins_e823(struct ice_pf *pf, struct ptp_clock_info *info)
2489 info->n_per_out = 0;
2494 * ice_ptp_set_funcs_e82x - Set specialized functions for E82x support
2495 * @pf: Board private structure
2496 * @info: PTP info to fill
2498 * Assign functions to the PTP capabiltiies structure for E82x devices.
2499 * Functions which operate across all device families should be set directly
2500 * in ice_ptp_set_caps. Only add functions here which are distinct for E82x
2504 ice_ptp_set_funcs_e82x(struct ice_pf *pf, struct ptp_clock_info *info)
2506 #ifdef CONFIG_ICE_HWTS
2507 if (boot_cpu_has(X86_FEATURE_ART) &&
2508 boot_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ))
2509 info->getcrosststamp = ice_ptp_getcrosststamp_e82x;
2510 #endif /* CONFIG_ICE_HWTS */
2514 * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support
2515 * @pf: Board private structure
2516 * @info: PTP info to fill
2518 * Assign functions to the PTP capabiltiies structure for E810 devices.
2519 * Functions which operate across all device families should be set directly
2520 * in ice_ptp_set_caps. Only add functions here which are distinct for e810
2524 ice_ptp_set_funcs_e810(struct ice_pf *pf, struct ptp_clock_info *info)
2526 info->enable = ice_ptp_gpio_enable_e810;
2527 ice_ptp_setup_pins_e810(pf, info);
2531 * ice_ptp_set_funcs_e823 - Set specialized functions for E823 support
2532 * @pf: Board private structure
2533 * @info: PTP info to fill
2535 * Assign functions to the PTP capabiltiies structure for E823 devices.
2536 * Functions which operate across all device families should be set directly
2537 * in ice_ptp_set_caps. Only add functions here which are distinct for e823
2541 ice_ptp_set_funcs_e823(struct ice_pf *pf, struct ptp_clock_info *info)
2543 ice_ptp_set_funcs_e82x(pf, info);
2545 info->enable = ice_ptp_gpio_enable_e823;
2546 ice_ptp_setup_pins_e823(pf, info);
2550 * ice_ptp_set_caps - Set PTP capabilities
2551 * @pf: Board private structure
2553 static void ice_ptp_set_caps(struct ice_pf *pf)
2555 struct ptp_clock_info *info = &pf->ptp.info;
2556 struct device *dev = ice_pf_to_dev(pf);
2558 snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk",
2559 dev_driver_string(dev), dev_name(dev));
2560 info->owner = THIS_MODULE;
2561 info->max_adj = 100000000;
2562 info->adjtime = ice_ptp_adjtime;
2563 info->adjfine = ice_ptp_adjfine;
2564 info->gettimex64 = ice_ptp_gettimex64;
2565 info->settime64 = ice_ptp_settime64;
2567 if (ice_is_e810(&pf->hw))
2568 ice_ptp_set_funcs_e810(pf, info);
2569 else if (ice_is_e823(&pf->hw))
2570 ice_ptp_set_funcs_e823(pf, info);
2572 ice_ptp_set_funcs_e82x(pf, info);
2576 * ice_ptp_create_clock - Create PTP clock device for userspace
2577 * @pf: Board private structure
2579 * This function creates a new PTP clock device. It only creates one if we
2580 * don't already have one. Will return error if it can't create one, but success
2581 * if we already have a device. Should be used by ice_ptp_init to create clock
2582 * initially, and prevent global resets from creating new clock devices.
2584 static long ice_ptp_create_clock(struct ice_pf *pf)
2586 struct ptp_clock_info *info;
2589 /* No need to create a clock device if we already have one */
2593 ice_ptp_set_caps(pf);
2595 info = &pf->ptp.info;
2596 dev = ice_pf_to_dev(pf);
2598 /* Attempt to register the clock before enabling the hardware. */
2599 pf->ptp.clock = ptp_clock_register(info, dev);
2600 if (IS_ERR(pf->ptp.clock)) {
2601 dev_err(ice_pf_to_dev(pf), "Failed to register PTP clock device");
2602 return PTR_ERR(pf->ptp.clock);
2609 * ice_ptp_request_ts - Request an available Tx timestamp index
2610 * @tx: the PTP Tx timestamp tracker to request from
2611 * @skb: the SKB to associate with this timestamp request
2613 s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
2615 unsigned long flags;
2618 spin_lock_irqsave(&tx->lock, flags);
2620 /* Check that this tracker is accepting new timestamp requests */
2621 if (!ice_ptp_is_tx_tracker_up(tx)) {
2622 spin_unlock_irqrestore(&tx->lock, flags);
2626 /* Find and set the first available index */
2627 idx = find_next_zero_bit(tx->in_use, tx->len,
2628 tx->last_ll_ts_idx_read + 1);
2630 idx = find_first_zero_bit(tx->in_use, tx->len);
2632 if (idx < tx->len) {
2633 /* We got a valid index that no other thread could have set. Store
2634 * a reference to the skb and the start time to allow discarding old
2637 set_bit(idx, tx->in_use);
2638 clear_bit(idx, tx->stale);
2639 tx->tstamps[idx].start = jiffies;
2640 tx->tstamps[idx].skb = skb_get(skb);
2641 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2642 ice_trace(tx_tstamp_request, skb, idx);
2645 spin_unlock_irqrestore(&tx->lock, flags);
2647 /* return the appropriate PHY timestamp register index, -1 if no
2648 * indexes were available.
2653 return idx + tx->offset;
2657 * ice_ptp_process_ts - Process the PTP Tx timestamps
2658 * @pf: Board private structure
2660 * Returns: ICE_TX_TSTAMP_WORK_PENDING if there are any outstanding Tx
2661 * timestamps that need processing, and ICE_TX_TSTAMP_WORK_DONE otherwise.
2663 enum ice_tx_tstamp_work ice_ptp_process_ts(struct ice_pf *pf)
2665 switch (pf->ptp.tx_interrupt_mode) {
2666 case ICE_PTP_TX_INTERRUPT_NONE:
2667 /* This device has the clock owner handle timestamps for it */
2668 return ICE_TX_TSTAMP_WORK_DONE;
2669 case ICE_PTP_TX_INTERRUPT_SELF:
2670 /* This device handles its own timestamps */
2671 return ice_ptp_tx_tstamp(&pf->ptp.port.tx);
2672 case ICE_PTP_TX_INTERRUPT_ALL:
2673 /* This device handles timestamps for all ports */
2674 return ice_ptp_tx_tstamp_owner(pf);
2676 WARN_ONCE(1, "Unexpected Tx timestamp interrupt mode %u\n",
2677 pf->ptp.tx_interrupt_mode);
2678 return ICE_TX_TSTAMP_WORK_DONE;
2683 * ice_ptp_maybe_trigger_tx_interrupt - Trigger Tx timstamp interrupt
2684 * @pf: Board private structure
2686 * The device PHY issues Tx timestamp interrupts to the driver for processing
2687 * timestamp data from the PHY. It will not interrupt again until all
2688 * current timestamp data is read. In rare circumstances, it is possible that
2689 * the driver fails to read all outstanding data.
2691 * To avoid getting permanently stuck, periodically check if the PHY has
2692 * outstanding timestamp data. If so, trigger an interrupt from software to
2693 * process this data.
2695 static void ice_ptp_maybe_trigger_tx_interrupt(struct ice_pf *pf)
2697 struct device *dev = ice_pf_to_dev(pf);
2698 struct ice_hw *hw = &pf->hw;
2699 bool trigger_oicr = false;
2702 if (ice_is_e810(hw))
2705 if (!ice_pf_src_tmr_owned(pf))
2708 for (i = 0; i < ICE_GET_QUAD_NUM(hw->ptp.num_lports); i++) {
2712 err = ice_get_phy_tx_tstamp_ready(&pf->hw, i, &tstamp_ready);
2713 if (!err && tstamp_ready) {
2714 trigger_oicr = true;
2720 /* Trigger a software interrupt, to ensure this data
2723 dev_dbg(dev, "PTP periodic task detected waiting timestamps. Triggering Tx timestamp interrupt now.\n");
2725 wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
2730 static void ice_ptp_periodic_work(struct kthread_work *work)
2732 struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work);
2733 struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp);
2736 if (pf->ptp.state != ICE_PTP_READY)
2739 err = ice_ptp_update_cached_phctime(pf);
2741 ice_ptp_maybe_trigger_tx_interrupt(pf);
2743 /* Run twice a second or reschedule if phc update failed */
2744 kthread_queue_delayed_work(ptp->kworker, &ptp->work,
2745 msecs_to_jiffies(err ? 10 : 500));
2749 * ice_ptp_prepare_for_reset - Prepare PTP for reset
2750 * @pf: Board private structure
2751 * @reset_type: the reset type being performed
2753 void ice_ptp_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
2755 struct ice_ptp *ptp = &pf->ptp;
2758 if (ptp->state != ICE_PTP_READY)
2761 ptp->state = ICE_PTP_RESETTING;
2763 /* Disable timestamping for both Tx and Rx */
2764 ice_ptp_disable_timestamp_mode(pf);
2766 kthread_cancel_delayed_work_sync(&ptp->work);
2768 if (reset_type == ICE_RESET_PFR)
2771 ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
2773 /* Disable periodic outputs */
2774 ice_ptp_disable_all_clkout(pf);
2776 src_tmr = ice_get_ptp_src_clock_index(&pf->hw);
2778 /* Disable source clock */
2779 wr32(&pf->hw, GLTSYN_ENA(src_tmr), (u32)~GLTSYN_ENA_TSYN_ENA_M);
2781 /* Acquire PHC and system timer to restore after reset */
2782 ptp->reset_time = ktime_get_real_ns();
2786 * ice_ptp_rebuild_owner - Initialize PTP clock owner after reset
2787 * @pf: Board private structure
2789 * Companion function for ice_ptp_rebuild() which handles tasks that only the
2790 * PTP clock owner instance should perform.
2792 static int ice_ptp_rebuild_owner(struct ice_pf *pf)
2794 struct ice_ptp *ptp = &pf->ptp;
2795 struct ice_hw *hw = &pf->hw;
2796 struct timespec64 ts;
2800 err = ice_ptp_init_phc(hw);
2804 /* Acquire the global hardware lock */
2805 if (!ice_ptp_lock(hw)) {
2810 /* Write the increment time value to PHY and LAN */
2811 err = ice_ptp_write_incval(hw, ice_base_incval(pf));
2817 /* Write the initial Time value to PHY and LAN using the cached PHC
2818 * time before the reset and time difference between stopping and
2819 * starting the clock.
2821 if (ptp->cached_phc_time) {
2822 time_diff = ktime_get_real_ns() - ptp->reset_time;
2823 ts = ns_to_timespec64(ptp->cached_phc_time + time_diff);
2825 ts = ktime_to_timespec64(ktime_get_real());
2827 err = ice_ptp_write_init(pf, &ts);
2833 /* Release the global hardware lock */
2836 /* Flush software tracking of any outstanding timestamps since we're
2837 * about to flush the PHY timestamp block.
2839 ice_ptp_flush_all_tx_tracker(pf);
2841 if (!ice_is_e810(hw)) {
2842 /* Enable quad interrupts */
2843 err = ice_ptp_cfg_phy_interrupt(pf, true, 1);
2847 ice_ptp_restart_all_phy(pf);
2850 /* Re-enable all periodic outputs and external timestamp events */
2851 ice_ptp_enable_all_clkout(pf);
2852 ice_ptp_enable_all_extts(pf);
2858 * ice_ptp_rebuild - Initialize PTP hardware clock support after reset
2859 * @pf: Board private structure
2860 * @reset_type: the reset type being performed
2862 void ice_ptp_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
2864 struct ice_ptp *ptp = &pf->ptp;
2867 if (ptp->state == ICE_PTP_READY) {
2868 ice_ptp_prepare_for_reset(pf, reset_type);
2869 } else if (ptp->state != ICE_PTP_RESETTING) {
2871 dev_err(ice_pf_to_dev(pf), "PTP was not initialized\n");
2875 if (ice_pf_src_tmr_owned(pf) && reset_type != ICE_RESET_PFR) {
2876 err = ice_ptp_rebuild_owner(pf);
2881 ptp->state = ICE_PTP_READY;
2883 /* Start periodic work going */
2884 kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0);
2886 dev_info(ice_pf_to_dev(pf), "PTP reset successful\n");
2890 ptp->state = ICE_PTP_ERROR;
2891 dev_err(ice_pf_to_dev(pf), "PTP reset failed %d\n", err);
2895 * ice_ptp_aux_dev_to_aux_pf - Get auxiliary PF handle for the auxiliary device
2896 * @aux_dev: auxiliary device to get the auxiliary PF for
2898 static struct ice_pf *
2899 ice_ptp_aux_dev_to_aux_pf(struct auxiliary_device *aux_dev)
2901 struct ice_ptp_port *aux_port;
2902 struct ice_ptp *aux_ptp;
2904 aux_port = container_of(aux_dev, struct ice_ptp_port, aux_dev);
2905 aux_ptp = container_of(aux_port, struct ice_ptp, port);
2907 return container_of(aux_ptp, struct ice_pf, ptp);
2911 * ice_ptp_aux_dev_to_owner_pf - Get PF handle for the auxiliary device
2912 * @aux_dev: auxiliary device to get the PF for
2914 static struct ice_pf *
2915 ice_ptp_aux_dev_to_owner_pf(struct auxiliary_device *aux_dev)
2917 struct ice_ptp_port_owner *ports_owner;
2918 struct auxiliary_driver *aux_drv;
2919 struct ice_ptp *owner_ptp;
2921 if (!aux_dev->dev.driver)
2924 aux_drv = to_auxiliary_drv(aux_dev->dev.driver);
2925 ports_owner = container_of(aux_drv, struct ice_ptp_port_owner,
2927 owner_ptp = container_of(ports_owner, struct ice_ptp, ports_owner);
2928 return container_of(owner_ptp, struct ice_pf, ptp);
2932 * ice_ptp_auxbus_probe - Probe auxiliary devices
2933 * @aux_dev: PF's auxiliary device
2934 * @id: Auxiliary device ID
2936 static int ice_ptp_auxbus_probe(struct auxiliary_device *aux_dev,
2937 const struct auxiliary_device_id *id)
2939 struct ice_pf *owner_pf = ice_ptp_aux_dev_to_owner_pf(aux_dev);
2940 struct ice_pf *aux_pf = ice_ptp_aux_dev_to_aux_pf(aux_dev);
2942 if (WARN_ON(!owner_pf))
2945 INIT_LIST_HEAD(&aux_pf->ptp.port.list_member);
2946 mutex_lock(&owner_pf->ptp.ports_owner.lock);
2947 list_add(&aux_pf->ptp.port.list_member,
2948 &owner_pf->ptp.ports_owner.ports);
2949 mutex_unlock(&owner_pf->ptp.ports_owner.lock);
2955 * ice_ptp_auxbus_remove - Remove auxiliary devices from the bus
2956 * @aux_dev: PF's auxiliary device
2958 static void ice_ptp_auxbus_remove(struct auxiliary_device *aux_dev)
2960 struct ice_pf *owner_pf = ice_ptp_aux_dev_to_owner_pf(aux_dev);
2961 struct ice_pf *aux_pf = ice_ptp_aux_dev_to_aux_pf(aux_dev);
2963 mutex_lock(&owner_pf->ptp.ports_owner.lock);
2964 list_del(&aux_pf->ptp.port.list_member);
2965 mutex_unlock(&owner_pf->ptp.ports_owner.lock);
2969 * ice_ptp_auxbus_shutdown
2970 * @aux_dev: PF's auxiliary device
2972 static void ice_ptp_auxbus_shutdown(struct auxiliary_device *aux_dev)
2974 /* Doing nothing here, but handle to auxbus driver must be satisfied */
2978 * ice_ptp_auxbus_suspend
2979 * @aux_dev: PF's auxiliary device
2980 * @state: power management state indicator
2983 ice_ptp_auxbus_suspend(struct auxiliary_device *aux_dev, pm_message_t state)
2985 /* Doing nothing here, but handle to auxbus driver must be satisfied */
2990 * ice_ptp_auxbus_resume
2991 * @aux_dev: PF's auxiliary device
2993 static int ice_ptp_auxbus_resume(struct auxiliary_device *aux_dev)
2995 /* Doing nothing here, but handle to auxbus driver must be satisfied */
3000 * ice_ptp_auxbus_create_id_table - Create auxiliary device ID table
3001 * @pf: Board private structure
3002 * @name: auxiliary bus driver name
3004 static struct auxiliary_device_id *
3005 ice_ptp_auxbus_create_id_table(struct ice_pf *pf, const char *name)
3007 struct auxiliary_device_id *ids;
3009 /* Second id left empty to terminate the array */
3010 ids = devm_kcalloc(ice_pf_to_dev(pf), 2,
3011 sizeof(struct auxiliary_device_id), GFP_KERNEL);
3015 snprintf(ids[0].name, sizeof(ids[0].name), "ice.%s", name);
3021 * ice_ptp_register_auxbus_driver - Register PTP auxiliary bus driver
3022 * @pf: Board private structure
3024 static int ice_ptp_register_auxbus_driver(struct ice_pf *pf)
3026 struct auxiliary_driver *aux_driver;
3027 struct ice_ptp *ptp;
3033 dev = ice_pf_to_dev(pf);
3034 aux_driver = &ptp->ports_owner.aux_driver;
3035 INIT_LIST_HEAD(&ptp->ports_owner.ports);
3036 mutex_init(&ptp->ports_owner.lock);
3037 name = devm_kasprintf(dev, GFP_KERNEL, "ptp_aux_dev_%u_%u_clk%u",
3038 pf->pdev->bus->number, PCI_SLOT(pf->pdev->devfn),
3039 ice_get_ptp_src_clock_index(&pf->hw));
3043 aux_driver->name = name;
3044 aux_driver->shutdown = ice_ptp_auxbus_shutdown;
3045 aux_driver->suspend = ice_ptp_auxbus_suspend;
3046 aux_driver->remove = ice_ptp_auxbus_remove;
3047 aux_driver->resume = ice_ptp_auxbus_resume;
3048 aux_driver->probe = ice_ptp_auxbus_probe;
3049 aux_driver->id_table = ice_ptp_auxbus_create_id_table(pf, name);
3050 if (!aux_driver->id_table)
3053 err = auxiliary_driver_register(aux_driver);
3055 devm_kfree(dev, aux_driver->id_table);
3056 dev_err(dev, "Failed registering aux_driver, name <%s>\n",
3064 * ice_ptp_unregister_auxbus_driver - Unregister PTP auxiliary bus driver
3065 * @pf: Board private structure
3067 static void ice_ptp_unregister_auxbus_driver(struct ice_pf *pf)
3069 struct auxiliary_driver *aux_driver = &pf->ptp.ports_owner.aux_driver;
3071 auxiliary_driver_unregister(aux_driver);
3072 devm_kfree(ice_pf_to_dev(pf), aux_driver->id_table);
3074 mutex_destroy(&pf->ptp.ports_owner.lock);
3078 * ice_ptp_clock_index - Get the PTP clock index for this device
3079 * @pf: Board private structure
3081 * Returns: the PTP clock index associated with this PF, or -1 if no PTP clock
3084 int ice_ptp_clock_index(struct ice_pf *pf)
3086 struct auxiliary_device *aux_dev;
3087 struct ice_pf *owner_pf;
3088 struct ptp_clock *clock;
3090 aux_dev = &pf->ptp.port.aux_dev;
3091 owner_pf = ice_ptp_aux_dev_to_owner_pf(aux_dev);
3094 clock = owner_pf->ptp.clock;
3096 return clock ? ptp_clock_index(clock) : -1;
3100 * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device
3101 * @pf: Board private structure
3103 * Setup and initialize a PTP clock device that represents the device hardware
3104 * clock. Save the clock index for other functions connected to the same
3105 * hardware resource.
3107 static int ice_ptp_init_owner(struct ice_pf *pf)
3109 struct ice_hw *hw = &pf->hw;
3110 struct timespec64 ts;
3113 err = ice_ptp_init_phc(hw);
3115 dev_err(ice_pf_to_dev(pf), "Failed to initialize PHC, err %d\n",
3120 /* Acquire the global hardware lock */
3121 if (!ice_ptp_lock(hw)) {
3126 /* Write the increment time value to PHY and LAN */
3127 err = ice_ptp_write_incval(hw, ice_base_incval(pf));
3133 ts = ktime_to_timespec64(ktime_get_real());
3134 /* Write the initial Time value to PHY and LAN */
3135 err = ice_ptp_write_init(pf, &ts);
3141 /* Release the global hardware lock */
3144 /* Configure PHY interrupt settings */
3145 err = ice_ptp_cfg_phy_interrupt(pf, true, 1);
3149 /* Ensure we have a clock device */
3150 err = ice_ptp_create_clock(pf);
3154 err = ice_ptp_register_auxbus_driver(pf);
3156 dev_err(ice_pf_to_dev(pf), "Failed to register PTP auxbus driver");
3162 ptp_clock_unregister(pf->ptp.clock);
3164 pf->ptp.clock = NULL;
3170 * ice_ptp_init_work - Initialize PTP work threads
3171 * @pf: Board private structure
3172 * @ptp: PF PTP structure
3174 static int ice_ptp_init_work(struct ice_pf *pf, struct ice_ptp *ptp)
3176 struct kthread_worker *kworker;
3178 /* Initialize work functions */
3179 kthread_init_delayed_work(&ptp->work, ice_ptp_periodic_work);
3181 /* Allocate a kworker for handling work required for the ports
3182 * connected to the PTP hardware clock.
3184 kworker = kthread_create_worker(0, "ice-ptp-%s",
3185 dev_name(ice_pf_to_dev(pf)));
3186 if (IS_ERR(kworker))
3187 return PTR_ERR(kworker);
3189 ptp->kworker = kworker;
3191 /* Start periodic work going */
3192 kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0);
3198 * ice_ptp_init_port - Initialize PTP port structure
3199 * @pf: Board private structure
3200 * @ptp_port: PTP port structure
3202 static int ice_ptp_init_port(struct ice_pf *pf, struct ice_ptp_port *ptp_port)
3204 struct ice_hw *hw = &pf->hw;
3206 mutex_init(&ptp_port->ps_lock);
3208 switch (hw->ptp.phy_model) {
3209 case ICE_PHY_ETH56G:
3210 return ice_ptp_init_tx_eth56g(pf, &ptp_port->tx,
3211 ptp_port->port_num);
3213 return ice_ptp_init_tx_e810(pf, &ptp_port->tx);
3215 kthread_init_delayed_work(&ptp_port->ov_work,
3216 ice_ptp_wait_for_offsets);
3218 return ice_ptp_init_tx_e82x(pf, &ptp_port->tx,
3219 ptp_port->port_num);
3226 * ice_ptp_release_auxbus_device
3227 * @dev: device that utilizes the auxbus
3229 static void ice_ptp_release_auxbus_device(struct device *dev)
3231 /* Doing nothing here, but handle to auxbux device must be satisfied */
3235 * ice_ptp_create_auxbus_device - Create PTP auxiliary bus device
3236 * @pf: Board private structure
3238 static int ice_ptp_create_auxbus_device(struct ice_pf *pf)
3240 struct auxiliary_device *aux_dev;
3241 struct ice_ptp *ptp;
3248 id = ptp->port.port_num;
3249 dev = ice_pf_to_dev(pf);
3251 aux_dev = &ptp->port.aux_dev;
3253 name = devm_kasprintf(dev, GFP_KERNEL, "ptp_aux_dev_%u_%u_clk%u",
3254 pf->pdev->bus->number, PCI_SLOT(pf->pdev->devfn),
3255 ice_get_ptp_src_clock_index(&pf->hw));
3259 aux_dev->name = name;
3261 aux_dev->dev.release = ice_ptp_release_auxbus_device;
3262 aux_dev->dev.parent = dev;
3264 err = auxiliary_device_init(aux_dev);
3268 err = auxiliary_device_add(aux_dev);
3270 auxiliary_device_uninit(aux_dev);
3276 dev_err(dev, "Failed to create PTP auxiliary bus device <%s>\n", name);
3277 devm_kfree(dev, name);
3282 * ice_ptp_remove_auxbus_device - Remove PTP auxiliary bus device
3283 * @pf: Board private structure
3285 static void ice_ptp_remove_auxbus_device(struct ice_pf *pf)
3287 struct auxiliary_device *aux_dev = &pf->ptp.port.aux_dev;
3289 auxiliary_device_delete(aux_dev);
3290 auxiliary_device_uninit(aux_dev);
3292 memset(aux_dev, 0, sizeof(*aux_dev));
3296 * ice_ptp_init_tx_interrupt_mode - Initialize device Tx interrupt mode
3297 * @pf: Board private structure
3299 * Initialize the Tx timestamp interrupt mode for this device. For most device
3300 * types, each PF processes the interrupt and manages its own timestamps. For
3301 * E822-based devices, only the clock owner processes the timestamps. Other
3302 * PFs disable the interrupt and do not process their own timestamps.
3304 static void ice_ptp_init_tx_interrupt_mode(struct ice_pf *pf)
3306 switch (pf->hw.ptp.phy_model) {
3308 /* E822 based PHY has the clock owner process the interrupt
3311 if (ice_pf_src_tmr_owned(pf))
3312 pf->ptp.tx_interrupt_mode = ICE_PTP_TX_INTERRUPT_ALL;
3314 pf->ptp.tx_interrupt_mode = ICE_PTP_TX_INTERRUPT_NONE;
3317 /* other PHY types handle their own Tx interrupt */
3318 pf->ptp.tx_interrupt_mode = ICE_PTP_TX_INTERRUPT_SELF;
3323 * ice_ptp_init - Initialize PTP hardware clock support
3324 * @pf: Board private structure
3326 * Set up the device for interacting with the PTP hardware clock for all
3327 * functions, both the function that owns the clock hardware, and the
3328 * functions connected to the clock hardware.
3330 * The clock owner will allocate and register a ptp_clock with the
3331 * PTP_1588_CLOCK infrastructure. All functions allocate a kthread and work
3332 * items used for asynchronous work such as Tx timestamps and periodic work.
3334 void ice_ptp_init(struct ice_pf *pf)
3336 struct ice_ptp *ptp = &pf->ptp;
3337 struct ice_hw *hw = &pf->hw;
3340 ptp->state = ICE_PTP_INITIALIZING;
3342 ice_ptp_init_hw(hw);
3344 ice_ptp_init_tx_interrupt_mode(pf);
3346 /* If this function owns the clock hardware, it must allocate and
3347 * configure the PTP clock device to represent it.
3349 if (ice_pf_src_tmr_owned(pf)) {
3350 err = ice_ptp_init_owner(pf);
3355 ptp->port.port_num = hw->pf_id;
3356 if (ice_is_e825c(hw) && hw->ptp.is_2x50g_muxed_topo)
3357 ptp->port.port_num = hw->pf_id * 2;
3359 err = ice_ptp_init_port(pf, &ptp->port);
3363 /* Start the PHY timestamping block */
3364 ice_ptp_reset_phy_timestamping(pf);
3366 /* Configure initial Tx interrupt settings */
3367 ice_ptp_cfg_tx_interrupt(pf);
3369 err = ice_ptp_create_auxbus_device(pf);
3373 ptp->state = ICE_PTP_READY;
3375 err = ice_ptp_init_work(pf, ptp);
3379 dev_info(ice_pf_to_dev(pf), "PTP init successful\n");
3383 /* If we registered a PTP clock, release it */
3384 if (pf->ptp.clock) {
3385 ptp_clock_unregister(ptp->clock);
3386 pf->ptp.clock = NULL;
3388 ptp->state = ICE_PTP_ERROR;
3389 dev_err(ice_pf_to_dev(pf), "PTP failed %d\n", err);
3393 * ice_ptp_release - Disable the driver/HW support and unregister the clock
3394 * @pf: Board private structure
3396 * This function handles the cleanup work required from the initialization by
3397 * clearing out the important information and unregistering the clock
3399 void ice_ptp_release(struct ice_pf *pf)
3401 if (pf->ptp.state != ICE_PTP_READY)
3404 pf->ptp.state = ICE_PTP_UNINIT;
3406 /* Disable timestamping for both Tx and Rx */
3407 ice_ptp_disable_timestamp_mode(pf);
3409 ice_ptp_remove_auxbus_device(pf);
3411 ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
3413 ice_ptp_disable_all_extts(pf);
3415 kthread_cancel_delayed_work_sync(&pf->ptp.work);
3417 ice_ptp_port_phy_stop(&pf->ptp.port);
3418 mutex_destroy(&pf->ptp.port.ps_lock);
3419 if (pf->ptp.kworker) {
3420 kthread_destroy_worker(pf->ptp.kworker);
3421 pf->ptp.kworker = NULL;
3424 if (ice_pf_src_tmr_owned(pf))
3425 ice_ptp_unregister_auxbus_driver(pf);
3430 /* Disable periodic outputs */
3431 ice_ptp_disable_all_clkout(pf);
3433 ptp_clock_unregister(pf->ptp.clock);
3434 pf->ptp.clock = NULL;
3436 dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n");