1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021, Intel Corporation. */
7 #include "ice_cgu_regs.h"
9 static const char ice_pin_names[][64] = {
18 static const struct ice_ptp_pin_desc ice_pin_desc_e82x[] = {
19 /* name, gpio, delay */
20 { TIME_SYNC, { 4, -1 }, { 0, 0 }},
21 { ONE_PPS, { -1, 5 }, { 0, 11 }},
24 static const struct ice_ptp_pin_desc ice_pin_desc_e825c[] = {
25 /* name, gpio, delay */
26 { SDP0, { 0, 0 }, { 15, 14 }},
27 { SDP1, { 1, 1 }, { 15, 14 }},
28 { SDP2, { 2, 2 }, { 15, 14 }},
29 { SDP3, { 3, 3 }, { 15, 14 }},
30 { TIME_SYNC, { 4, -1 }, { 11, 0 }},
31 { ONE_PPS, { -1, 5 }, { 0, 9 }},
34 static const struct ice_ptp_pin_desc ice_pin_desc_e810[] = {
35 /* name, gpio, delay */
36 { SDP0, { 0, 0 }, { 0, 1 }},
37 { SDP1, { 1, 1 }, { 0, 1 }},
38 { SDP2, { 2, 2 }, { 0, 1 }},
39 { SDP3, { 3, 3 }, { 0, 1 }},
40 { ONE_PPS, { -1, 5 }, { 0, 1 }},
43 static const char ice_pin_names_nvm[][64] = {
51 static const struct ice_ptp_pin_desc ice_pin_desc_e810_sma[] = {
52 /* name, gpio, delay */
53 { GNSS, { 1, -1 }, { 0, 0 }},
54 { SMA1, { 1, 0 }, { 0, 1 }},
55 { UFL1, { -1, 0 }, { 0, 1 }},
56 { SMA2, { 3, 2 }, { 0, 1 }},
57 { UFL2, { 3, -1 }, { 0, 0 }},
60 static struct ice_pf *ice_get_ctrl_pf(struct ice_pf *pf)
62 return !pf->adapter ? NULL : pf->adapter->ctrl_pf;
65 static struct ice_ptp *ice_get_ctrl_ptp(struct ice_pf *pf)
67 struct ice_pf *ctrl_pf = ice_get_ctrl_pf(pf);
69 return !ctrl_pf ? NULL : &ctrl_pf->ptp;
73 * ice_ptp_find_pin_idx - Find pin index in ptp_pin_desc
74 * @pf: Board private structure
78 * Return: positive pin number when pin is present, -1 otherwise
80 static int ice_ptp_find_pin_idx(struct ice_pf *pf, enum ptp_pin_function func,
83 const struct ptp_clock_info *info = &pf->ptp.info;
86 for (i = 0; i < info->n_pins; i++) {
87 if (info->pin_config[i].func == func &&
88 info->pin_config[i].chan == chan)
96 * ice_ptp_update_sma_data - update SMA pins data according to pins setup
97 * @pf: Board private structure
98 * @sma_pins: parsed SMA pins status
99 * @data: SMA data to update
101 static void ice_ptp_update_sma_data(struct ice_pf *pf, unsigned int sma_pins[],
104 const char *state1, *state2;
106 /* Set the right state based on the desired configuration.
107 * When bit is set, functionality is disabled.
109 *data &= ~ICE_ALL_SMA_MASK;
110 if (!sma_pins[UFL1 - 1]) {
111 if (sma_pins[SMA1 - 1] == PTP_PF_EXTTS) {
112 state1 = "SMA1 Rx, U.FL1 disabled";
113 *data |= ICE_SMA1_TX_EN;
114 } else if (sma_pins[SMA1 - 1] == PTP_PF_PEROUT) {
115 state1 = "SMA1 Tx U.FL1 disabled";
116 *data |= ICE_SMA1_DIR_EN;
118 state1 = "SMA1 disabled, U.FL1 disabled";
119 *data |= ICE_SMA1_MASK;
122 /* U.FL1 Tx will always enable SMA1 Rx */
123 state1 = "SMA1 Rx, U.FL1 Tx";
126 if (!sma_pins[UFL2 - 1]) {
127 if (sma_pins[SMA2 - 1] == PTP_PF_EXTTS) {
128 state2 = "SMA2 Rx, U.FL2 disabled";
129 *data |= ICE_SMA2_TX_EN | ICE_SMA2_UFL2_RX_DIS;
130 } else if (sma_pins[SMA2 - 1] == PTP_PF_PEROUT) {
131 state2 = "SMA2 Tx, U.FL2 disabled";
132 *data |= ICE_SMA2_DIR_EN | ICE_SMA2_UFL2_RX_DIS;
134 state2 = "SMA2 disabled, U.FL2 disabled";
135 *data |= ICE_SMA2_MASK;
138 if (!sma_pins[SMA2 - 1]) {
139 state2 = "SMA2 disabled, U.FL2 Rx";
140 *data |= ICE_SMA2_DIR_EN | ICE_SMA2_TX_EN;
142 state2 = "SMA2 Tx, U.FL2 Rx";
143 *data |= ICE_SMA2_DIR_EN;
147 dev_dbg(ice_pf_to_dev(pf), "%s, %s\n", state1, state2);
151 * ice_ptp_set_sma_cfg - set the configuration of the SMA control logic
152 * @pf: Board private structure
154 * Return: 0 on success, negative error code otherwise
156 static int ice_ptp_set_sma_cfg(struct ice_pf *pf)
158 const struct ice_ptp_pin_desc *ice_pins = pf->ptp.ice_pin_desc;
159 struct ptp_pin_desc *pins = pf->ptp.pin_desc;
160 unsigned int sma_pins[ICE_SMA_PINS_NUM] = {};
164 /* Read initial pin state value */
165 err = ice_read_sma_ctrl(&pf->hw, &data);
169 /* Get SMA/U.FL pins states */
170 for (int i = 0; i < pf->ptp.info.n_pins; i++)
172 int name_idx = ice_pins[i].name_idx;
179 sma_pins[name_idx - 1] = pins[i].func;
186 ice_ptp_update_sma_data(pf, sma_pins, &data);
187 return ice_write_sma_ctrl(&pf->hw, data);
191 * ice_ptp_cfg_tx_interrupt - Configure Tx timestamp interrupt for the device
192 * @pf: Board private structure
194 * Program the device to respond appropriately to the Tx timestamp interrupt
197 static void ice_ptp_cfg_tx_interrupt(struct ice_pf *pf)
199 struct ice_hw *hw = &pf->hw;
203 switch (pf->ptp.tx_interrupt_mode) {
204 case ICE_PTP_TX_INTERRUPT_ALL:
205 /* React to interrupts across all quads. */
206 wr32(hw, PFINT_TSYN_MSK + (0x4 * hw->pf_id), (u32)0x1f);
209 case ICE_PTP_TX_INTERRUPT_NONE:
210 /* Do not react to interrupts on any quad. */
211 wr32(hw, PFINT_TSYN_MSK + (0x4 * hw->pf_id), (u32)0x0);
214 case ICE_PTP_TX_INTERRUPT_SELF:
216 enable = pf->ptp.tstamp_config.tx_type == HWTSTAMP_TX_ON;
220 /* Configure the Tx timestamp interrupt */
221 val = rd32(hw, PFINT_OICR_ENA);
223 val |= PFINT_OICR_TSYN_TX_M;
225 val &= ~PFINT_OICR_TSYN_TX_M;
226 wr32(hw, PFINT_OICR_ENA, val);
230 * ice_set_rx_tstamp - Enable or disable Rx timestamping
231 * @pf: The PF pointer to search in
232 * @on: bool value for whether timestamps are enabled or disabled
234 static void ice_set_rx_tstamp(struct ice_pf *pf, bool on)
239 vsi = ice_get_main_vsi(pf);
240 if (!vsi || !vsi->rx_rings)
243 /* Set the timestamp flag for all the Rx rings */
244 ice_for_each_rxq(vsi, i) {
245 if (!vsi->rx_rings[i])
247 vsi->rx_rings[i]->ptp_rx = on;
252 * ice_ptp_disable_timestamp_mode - Disable current timestamp mode
253 * @pf: Board private structure
255 * Called during preparation for reset to temporarily disable timestamping on
256 * the device. Called during remove to disable timestamping while cleaning up
259 static void ice_ptp_disable_timestamp_mode(struct ice_pf *pf)
261 struct ice_hw *hw = &pf->hw;
264 val = rd32(hw, PFINT_OICR_ENA);
265 val &= ~PFINT_OICR_TSYN_TX_M;
266 wr32(hw, PFINT_OICR_ENA, val);
268 ice_set_rx_tstamp(pf, false);
272 * ice_ptp_restore_timestamp_mode - Restore timestamp configuration
273 * @pf: Board private structure
275 * Called at the end of rebuild to restore timestamp configuration after
278 void ice_ptp_restore_timestamp_mode(struct ice_pf *pf)
280 struct ice_hw *hw = &pf->hw;
283 ice_ptp_cfg_tx_interrupt(pf);
285 enable_rx = pf->ptp.tstamp_config.rx_filter == HWTSTAMP_FILTER_ALL;
286 ice_set_rx_tstamp(pf, enable_rx);
288 /* Trigger an immediate software interrupt to ensure that timestamps
289 * which occurred during reset are handled now.
291 wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
296 * ice_ptp_read_src_clk_reg - Read the source clock register
297 * @pf: Board private structure
298 * @sts: Optional parameter for holding a pair of system timestamps from
299 * the system clock. Will be ignored if NULL is given.
302 ice_ptp_read_src_clk_reg(struct ice_pf *pf, struct ptp_system_timestamp *sts)
304 struct ice_hw *hw = &pf->hw;
308 tmr_idx = ice_get_ptp_src_clock_index(hw);
309 guard(spinlock)(&pf->adapter->ptp_gltsyn_time_lock);
310 /* Read the system timestamp pre PHC read */
311 ptp_read_system_prets(sts);
313 lo = rd32(hw, GLTSYN_TIME_L(tmr_idx));
315 /* Read the system timestamp post PHC read */
316 ptp_read_system_postts(sts);
318 hi = rd32(hw, GLTSYN_TIME_H(tmr_idx));
319 lo2 = rd32(hw, GLTSYN_TIME_L(tmr_idx));
322 /* if TIME_L rolled over read TIME_L again and update
325 ptp_read_system_prets(sts);
326 lo = rd32(hw, GLTSYN_TIME_L(tmr_idx));
327 ptp_read_system_postts(sts);
328 hi = rd32(hw, GLTSYN_TIME_H(tmr_idx));
331 return ((u64)hi << 32) | lo;
335 * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b
336 * @cached_phc_time: recently cached copy of PHC time
337 * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value
339 * Hardware captures timestamps which contain only 32 bits of nominal
340 * nanoseconds, as opposed to the 64bit timestamps that the stack expects.
341 * Note that the captured timestamp values may be 40 bits, but the lower
342 * 8 bits are sub-nanoseconds and generally discarded.
344 * Extend the 32bit nanosecond timestamp using the following algorithm and
347 * 1) have a recently cached copy of the PHC time
348 * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1
349 * seconds) before or after the PHC time was captured.
350 * 3) calculate the delta between the cached time and the timestamp
351 * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was
352 * captured after the PHC time. In this case, the full timestamp is just
353 * the cached PHC time plus the delta.
354 * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the
355 * timestamp was captured *before* the PHC time, i.e. because the PHC
356 * cache was updated after the timestamp was captured by hardware. In this
357 * case, the full timestamp is the cached time minus the inverse delta.
359 * This algorithm works even if the PHC time was updated after a Tx timestamp
360 * was requested, but before the Tx timestamp event was reported from
363 * This calculation primarily relies on keeping the cached PHC time up to
364 * date. If the timestamp was captured more than 2^31 nanoseconds after the
365 * PHC time, it is possible that the lower 32bits of PHC time have
366 * overflowed more than once, and we might generate an incorrect timestamp.
368 * This is prevented by (a) periodically updating the cached PHC time once
369 * a second, and (b) discarding any Tx timestamp packet if it has waited for
370 * a timestamp for more than one second.
372 static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp)
374 u32 delta, phc_time_lo;
377 /* Extract the lower 32 bits of the PHC time */
378 phc_time_lo = (u32)cached_phc_time;
380 /* Calculate the delta between the lower 32bits of the cached PHC
381 * time and the in_tstamp value
383 delta = (in_tstamp - phc_time_lo);
385 /* Do not assume that the in_tstamp is always more recent than the
386 * cached PHC time. If the delta is large, it indicates that the
387 * in_tstamp was taken in the past, and should be converted
390 if (delta > (U32_MAX / 2)) {
391 /* reverse the delta calculation here */
392 delta = (phc_time_lo - in_tstamp);
393 ns = cached_phc_time - delta;
395 ns = cached_phc_time + delta;
402 * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds
403 * @pf: Board private structure
404 * @in_tstamp: Ingress/egress 40b timestamp value
406 * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal
407 * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit.
409 * *--------------------------------------------------------------*
410 * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v |
411 * *--------------------------------------------------------------*
413 * The low bit is an indicator of whether the timestamp is valid. The next
414 * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow,
415 * and the remaining 32 bits are the lower 32 bits of the PHC timer.
417 * It is assumed that the caller verifies the timestamp is valid prior to
418 * calling this function.
420 * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC
421 * time stored in the device private PTP structure as the basis for timestamp
424 * See ice_ptp_extend_32b_ts for a detailed explanation of the extension
427 static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp)
429 const u64 mask = GENMASK_ULL(31, 0);
430 unsigned long discard_time;
432 /* Discard the hardware timestamp if the cached PHC time is too old */
433 discard_time = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000);
434 if (time_is_before_jiffies(discard_time)) {
435 pf->ptp.tx_hwtstamp_discarded++;
439 return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time,
440 (in_tstamp >> 8) & mask);
444 * ice_ptp_is_tx_tracker_up - Check if Tx tracker is ready for new timestamps
445 * @tx: the PTP Tx timestamp tracker to check
447 * Check that a given PTP Tx timestamp tracker is up, i.e. that it is ready
448 * to accept new timestamp requests.
450 * Assumes the tx->lock spinlock is already held.
453 ice_ptp_is_tx_tracker_up(struct ice_ptp_tx *tx)
455 lockdep_assert_held(&tx->lock);
457 return tx->init && !tx->calibrating;
461 * ice_ptp_req_tx_single_tstamp - Request Tx timestamp for a port from FW
462 * @tx: the PTP Tx timestamp tracker
463 * @idx: index of the timestamp to request
465 void ice_ptp_req_tx_single_tstamp(struct ice_ptp_tx *tx, u8 idx)
467 struct ice_e810_params *params;
468 struct ice_ptp_port *ptp_port;
476 ptp_port = container_of(tx, struct ice_ptp_port, tx);
477 pf = ptp_port_to_pf(ptp_port);
478 params = &pf->hw.ptp.phy.e810;
480 /* Drop packets which have waited for more than 2 seconds */
481 if (time_is_before_jiffies(tx->tstamps[idx].start + 2 * HZ)) {
482 /* Count the number of Tx timestamps that timed out */
483 pf->ptp.tx_hwtstamp_timeouts++;
485 skb = tx->tstamps[idx].skb;
486 tx->tstamps[idx].skb = NULL;
487 clear_bit(idx, tx->in_use);
489 dev_kfree_skb_any(skb);
493 ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx);
495 spin_lock_irqsave(¶ms->atqbal_wq.lock, flags);
497 params->atqbal_flags |= ATQBAL_FLAGS_INTR_IN_PROGRESS;
499 /* Write TS index to read to the PF register so the FW can read it */
500 wr32(&pf->hw, REG_LL_PROXY_H,
501 REG_LL_PROXY_H_TS_INTR_ENA | FIELD_PREP(REG_LL_PROXY_H_TS_IDX, idx) |
502 REG_LL_PROXY_H_EXEC);
503 tx->last_ll_ts_idx_read = idx;
505 spin_unlock_irqrestore(¶ms->atqbal_wq.lock, flags);
509 * ice_ptp_complete_tx_single_tstamp - Complete Tx timestamp for a port
510 * @tx: the PTP Tx timestamp tracker
512 void ice_ptp_complete_tx_single_tstamp(struct ice_ptp_tx *tx)
514 struct skb_shared_hwtstamps shhwtstamps = {};
515 u8 idx = tx->last_ll_ts_idx_read;
516 struct ice_e810_params *params;
517 struct ice_ptp_port *ptp_port;
518 u64 raw_tstamp, tstamp;
519 bool drop_ts = false;
526 if (!tx->init || tx->last_ll_ts_idx_read < 0)
529 ptp_port = container_of(tx, struct ice_ptp_port, tx);
530 pf = ptp_port_to_pf(ptp_port);
531 dev = ice_pf_to_dev(pf);
532 params = &pf->hw.ptp.phy.e810;
534 ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx);
536 spin_lock_irqsave(¶ms->atqbal_wq.lock, flags);
538 if (!(params->atqbal_flags & ATQBAL_FLAGS_INTR_IN_PROGRESS))
539 dev_dbg(dev, "%s: low latency interrupt request not in progress?\n",
542 /* Read the low 32 bit value */
543 raw_tstamp = rd32(&pf->hw, REG_LL_PROXY_L);
544 /* Read the status together with high TS part */
545 reg_ll_high = rd32(&pf->hw, REG_LL_PROXY_H);
547 /* Wake up threads waiting on low latency interface */
548 params->atqbal_flags &= ~ATQBAL_FLAGS_INTR_IN_PROGRESS;
550 wake_up_locked(¶ms->atqbal_wq);
552 spin_unlock_irqrestore(¶ms->atqbal_wq.lock, flags);
554 /* When the bit is cleared, the TS is ready in the register */
555 if (reg_ll_high & REG_LL_PROXY_H_EXEC) {
556 dev_err(ice_pf_to_dev(pf), "Failed to get the Tx tstamp - FW not ready");
560 /* High 8 bit value of the TS is on the bits 16:23 */
561 raw_tstamp |= ((u64)FIELD_GET(REG_LL_PROXY_H_TS_HIGH, reg_ll_high)) << 32;
563 /* Devices using this interface always verify the timestamp differs
564 * relative to the last cached timestamp value.
566 if (raw_tstamp == tx->tstamps[idx].cached_tstamp)
569 tx->tstamps[idx].cached_tstamp = raw_tstamp;
570 clear_bit(idx, tx->in_use);
571 skb = tx->tstamps[idx].skb;
572 tx->tstamps[idx].skb = NULL;
573 if (test_and_clear_bit(idx, tx->stale))
580 dev_kfree_skb_any(skb);
584 /* Extend the timestamp using cached PHC time */
585 tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp);
587 shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
588 ice_trace(tx_tstamp_complete, skb, idx);
591 skb_tstamp_tx(skb, &shhwtstamps);
592 dev_kfree_skb_any(skb);
596 * ice_ptp_process_tx_tstamp - Process Tx timestamps for a port
597 * @tx: the PTP Tx timestamp tracker
599 * Process timestamps captured by the PHY associated with this port. To do
600 * this, loop over each index with a waiting skb.
602 * If a given index has a valid timestamp, perform the following steps:
604 * 1) check that the timestamp request is not stale
605 * 2) check that a timestamp is ready and available in the PHY memory bank
606 * 3) read and copy the timestamp out of the PHY register
607 * 4) unlock the index by clearing the associated in_use bit
608 * 5) check if the timestamp is stale, and discard if so
609 * 6) extend the 40 bit timestamp value to get a 64 bit timestamp value
610 * 7) send this 64 bit timestamp to the stack
612 * Note that we do not hold the tracking lock while reading the Tx timestamp.
613 * This is because reading the timestamp requires taking a mutex that might
616 * The only place where we set in_use is when a new timestamp is initiated
617 * with a slot index. This is only called in the hard xmit routine where an
618 * SKB has a request flag set. The only places where we clear this bit is this
619 * function, or during teardown when the Tx timestamp tracker is being
620 * removed. A timestamp index will never be re-used until the in_use bit for
621 * that index is cleared.
623 * If a Tx thread starts a new timestamp, we might not begin processing it
624 * right away but we will notice it at the end when we re-queue the task.
626 * If a Tx thread starts a new timestamp just after this function exits, the
627 * interrupt for that timestamp should re-trigger this function once
628 * a timestamp is ready.
630 * In cases where the PTP hardware clock was directly adjusted, some
631 * timestamps may not be able to safely use the timestamp extension math. In
632 * this case, software will set the stale bit for any outstanding Tx
633 * timestamps when the clock is adjusted. Then this function will discard
634 * those captured timestamps instead of sending them to the stack.
636 * If a Tx packet has been waiting for more than 2 seconds, it is not possible
637 * to correctly extend the timestamp using the cached PHC time. It is
638 * extremely unlikely that a packet will ever take this long to timestamp. If
639 * we detect a Tx timestamp request that has waited for this long we assume
640 * the packet will never be sent by hardware and discard it without reading
641 * the timestamp register.
643 static void ice_ptp_process_tx_tstamp(struct ice_ptp_tx *tx)
645 struct ice_ptp_port *ptp_port;
654 ptp_port = container_of(tx, struct ice_ptp_port, tx);
655 pf = ptp_port_to_pf(ptp_port);
658 /* Read the Tx ready status first */
659 if (tx->has_ready_bitmap) {
660 err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready);
665 /* Drop packets if the link went down */
666 link_up = ptp_port->link_up;
668 for_each_set_bit(idx, tx->in_use, tx->len) {
669 struct skb_shared_hwtstamps shhwtstamps = {};
670 u8 phy_idx = idx + tx->offset;
671 u64 raw_tstamp = 0, tstamp;
672 bool drop_ts = !link_up;
675 /* Drop packets which have waited for more than 2 seconds */
676 if (time_is_before_jiffies(tx->tstamps[idx].start + 2 * HZ)) {
679 /* Count the number of Tx timestamps that timed out */
680 pf->ptp.tx_hwtstamp_timeouts++;
683 /* Only read a timestamp from the PHY if its marked as ready
684 * by the tstamp_ready register. This avoids unnecessary
685 * reading of timestamps which are not yet valid. This is
686 * important as we must read all timestamps which are valid
687 * and only timestamps which are valid during each interrupt.
688 * If we do not, the hardware logic for generating a new
689 * interrupt can get stuck on some devices.
691 if (tx->has_ready_bitmap &&
692 !(tstamp_ready & BIT_ULL(phy_idx))) {
699 ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx);
701 err = ice_read_phy_tstamp(hw, tx->block, phy_idx, &raw_tstamp);
705 ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx);
707 /* For PHYs which don't implement a proper timestamp ready
708 * bitmap, verify that the timestamp value is different
709 * from the last cached timestamp. If it is not, skip this for
710 * now assuming it hasn't yet been captured by hardware.
712 if (!drop_ts && !tx->has_ready_bitmap &&
713 raw_tstamp == tx->tstamps[idx].cached_tstamp)
716 /* Discard any timestamp value without the valid bit set */
717 if (!(raw_tstamp & ICE_PTP_TS_VALID))
721 spin_lock_irqsave(&tx->lock, flags);
722 if (!tx->has_ready_bitmap && raw_tstamp)
723 tx->tstamps[idx].cached_tstamp = raw_tstamp;
724 clear_bit(idx, tx->in_use);
725 skb = tx->tstamps[idx].skb;
726 tx->tstamps[idx].skb = NULL;
727 if (test_and_clear_bit(idx, tx->stale))
729 spin_unlock_irqrestore(&tx->lock, flags);
731 /* It is unlikely but possible that the SKB will have been
732 * flushed at this point due to link change or teardown.
738 dev_kfree_skb_any(skb);
742 /* Extend the timestamp using cached PHC time */
743 tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp);
745 shhwtstamps.hwtstamp = ns_to_ktime(tstamp);
746 ice_trace(tx_tstamp_complete, skb, idx);
749 skb_tstamp_tx(skb, &shhwtstamps);
750 dev_kfree_skb_any(skb);
755 * ice_ptp_tx_tstamp_owner - Process Tx timestamps for all ports on the device
756 * @pf: Board private structure
758 static enum ice_tx_tstamp_work ice_ptp_tx_tstamp_owner(struct ice_pf *pf)
760 struct ice_ptp_port *port;
763 mutex_lock(&pf->adapter->ports.lock);
764 list_for_each_entry(port, &pf->adapter->ports.ports, list_node) {
765 struct ice_ptp_tx *tx = &port->tx;
767 if (!tx || !tx->init)
770 ice_ptp_process_tx_tstamp(tx);
772 mutex_unlock(&pf->adapter->ports.lock);
774 for (i = 0; i < ICE_GET_QUAD_NUM(pf->hw.ptp.num_lports); i++) {
778 /* Read the Tx ready status first */
779 err = ice_get_phy_tx_tstamp_ready(&pf->hw, i, &tstamp_ready);
782 else if (tstamp_ready)
783 return ICE_TX_TSTAMP_WORK_PENDING;
786 return ICE_TX_TSTAMP_WORK_DONE;
790 * ice_ptp_tx_tstamp - Process Tx timestamps for this function.
791 * @tx: Tx tracking structure to initialize
793 * Returns: ICE_TX_TSTAMP_WORK_PENDING if there are any outstanding incomplete
794 * Tx timestamps, or ICE_TX_TSTAMP_WORK_DONE otherwise.
796 static enum ice_tx_tstamp_work ice_ptp_tx_tstamp(struct ice_ptp_tx *tx)
798 bool more_timestamps;
802 return ICE_TX_TSTAMP_WORK_DONE;
804 /* Process the Tx timestamp tracker */
805 ice_ptp_process_tx_tstamp(tx);
807 /* Check if there are outstanding Tx timestamps */
808 spin_lock_irqsave(&tx->lock, flags);
809 more_timestamps = tx->init && !bitmap_empty(tx->in_use, tx->len);
810 spin_unlock_irqrestore(&tx->lock, flags);
813 return ICE_TX_TSTAMP_WORK_PENDING;
815 return ICE_TX_TSTAMP_WORK_DONE;
819 * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps
820 * @tx: Tx tracking structure to initialize
822 * Assumes that the length has already been initialized. Do not call directly,
823 * use the ice_ptp_init_tx_* instead.
826 ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx)
828 unsigned long *in_use, *stale;
829 struct ice_tx_tstamp *tstamps;
831 tstamps = kcalloc(tx->len, sizeof(*tstamps), GFP_KERNEL);
832 in_use = bitmap_zalloc(tx->len, GFP_KERNEL);
833 stale = bitmap_zalloc(tx->len, GFP_KERNEL);
835 if (!tstamps || !in_use || !stale) {
843 tx->tstamps = tstamps;
847 tx->last_ll_ts_idx_read = -1;
849 spin_lock_init(&tx->lock);
855 * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker
856 * @pf: Board private structure
857 * @tx: the tracker to flush
859 * Called during teardown when a Tx tracker is being removed.
862 ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
864 struct ice_hw *hw = &pf->hw;
870 err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready);
872 dev_dbg(ice_pf_to_dev(pf), "Failed to get the Tx tstamp ready bitmap for block %u, err %d\n",
875 /* If we fail to read the Tx timestamp ready bitmap just
876 * skip clearing the PHY timestamps.
881 for_each_set_bit(idx, tx->in_use, tx->len) {
882 u8 phy_idx = idx + tx->offset;
885 /* In case this timestamp is ready, we need to clear it. */
886 if (!hw->reset_ongoing && (tstamp_ready & BIT_ULL(phy_idx)))
887 ice_clear_phy_tstamp(hw, tx->block, phy_idx);
889 spin_lock_irqsave(&tx->lock, flags);
890 skb = tx->tstamps[idx].skb;
891 tx->tstamps[idx].skb = NULL;
892 clear_bit(idx, tx->in_use);
893 clear_bit(idx, tx->stale);
894 spin_unlock_irqrestore(&tx->lock, flags);
896 /* Count the number of Tx timestamps flushed */
897 pf->ptp.tx_hwtstamp_flushed++;
899 /* Free the SKB after we've cleared the bit */
900 dev_kfree_skb_any(skb);
905 * ice_ptp_mark_tx_tracker_stale - Mark unfinished timestamps as stale
906 * @tx: the tracker to mark
908 * Mark currently outstanding Tx timestamps as stale. This prevents sending
909 * their timestamp value to the stack. This is required to prevent extending
910 * the 40bit hardware timestamp incorrectly.
912 * This should be called when the PTP clock is modified such as after a set
916 ice_ptp_mark_tx_tracker_stale(struct ice_ptp_tx *tx)
920 spin_lock_irqsave(&tx->lock, flags);
921 bitmap_or(tx->stale, tx->stale, tx->in_use, tx->len);
922 spin_unlock_irqrestore(&tx->lock, flags);
926 * ice_ptp_flush_all_tx_tracker - Flush all timestamp trackers on this clock
927 * @pf: Board private structure
929 * Called by the clock owner to flush all the Tx timestamp trackers associated
933 ice_ptp_flush_all_tx_tracker(struct ice_pf *pf)
935 struct ice_ptp_port *port;
937 list_for_each_entry(port, &pf->adapter->ports.ports, list_node)
938 ice_ptp_flush_tx_tracker(ptp_port_to_pf(port), &port->tx);
942 * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker
943 * @pf: Board private structure
944 * @tx: Tx tracking structure to release
946 * Free memory associated with the Tx timestamp tracker.
949 ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx)
953 spin_lock_irqsave(&tx->lock, flags);
955 spin_unlock_irqrestore(&tx->lock, flags);
957 /* wait for potentially outstanding interrupt to complete */
958 synchronize_irq(pf->oicr_irq.virq);
960 ice_ptp_flush_tx_tracker(pf, tx);
965 bitmap_free(tx->in_use);
968 bitmap_free(tx->stale);
975 * ice_ptp_init_tx_eth56g - Initialize tracking for Tx timestamps
976 * @pf: Board private structure
977 * @tx: the Tx tracking structure to initialize
978 * @port: the port this structure tracks
980 * Initialize the Tx timestamp tracker for this port. ETH56G PHYs
981 * have independent memory blocks for all ports.
983 * Return: 0 for success, -ENOMEM when failed to allocate Tx tracker
985 static int ice_ptp_init_tx_eth56g(struct ice_pf *pf, struct ice_ptp_tx *tx,
990 tx->len = INDEX_PER_PORT_ETH56G;
991 tx->has_ready_bitmap = 1;
993 return ice_ptp_alloc_tx_tracker(tx);
997 * ice_ptp_init_tx_e82x - Initialize tracking for Tx timestamps
998 * @pf: Board private structure
999 * @tx: the Tx tracking structure to initialize
1000 * @port: the port this structure tracks
1002 * Initialize the Tx timestamp tracker for this port. For generic MAC devices,
1003 * the timestamp block is shared for all ports in the same quad. To avoid
1004 * ports using the same timestamp index, logically break the block of
1005 * registers into chunks based on the port number.
1008 ice_ptp_init_tx_e82x(struct ice_pf *pf, struct ice_ptp_tx *tx, u8 port)
1010 tx->block = ICE_GET_QUAD_NUM(port);
1011 tx->offset = (port % ICE_PORTS_PER_QUAD) * INDEX_PER_PORT_E82X;
1012 tx->len = INDEX_PER_PORT_E82X;
1013 tx->has_ready_bitmap = 1;
1015 return ice_ptp_alloc_tx_tracker(tx);
1019 * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps
1020 * @pf: Board private structure
1021 * @tx: the Tx tracking structure to initialize
1023 * Initialize the Tx timestamp tracker for this PF. For E810 devices, each
1024 * port has its own block of timestamps, independent of the other ports.
1027 ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx)
1029 tx->block = pf->hw.port_info->lport;
1031 tx->len = INDEX_PER_PORT_E810;
1032 /* The E810 PHY does not provide a timestamp ready bitmap. Instead,
1033 * verify new timestamps against cached copy of the last read
1036 tx->has_ready_bitmap = 0;
1038 return ice_ptp_alloc_tx_tracker(tx);
1042 * ice_ptp_update_cached_phctime - Update the cached PHC time values
1043 * @pf: Board specific private structure
1045 * This function updates the system time values which are cached in the PF
1046 * structure and the Rx rings.
1048 * This function must be called periodically to ensure that the cached value
1049 * is never more than 2 seconds old.
1051 * Note that the cached copy in the PF PTP structure is always updated, even
1052 * if we can't update the copy in the Rx rings.
1055 * * 0 - OK, successfully updated
1056 * * -EAGAIN - PF was busy, need to reschedule the update
1058 static int ice_ptp_update_cached_phctime(struct ice_pf *pf)
1060 struct device *dev = ice_pf_to_dev(pf);
1061 unsigned long update_before;
1065 update_before = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000);
1066 if (pf->ptp.cached_phc_time &&
1067 time_is_before_jiffies(update_before)) {
1068 unsigned long time_taken = jiffies - pf->ptp.cached_phc_jiffies;
1070 dev_warn(dev, "%u msecs passed between update to cached PHC time\n",
1071 jiffies_to_msecs(time_taken));
1072 pf->ptp.late_cached_phc_updates++;
1075 /* Read the current PHC time */
1076 systime = ice_ptp_read_src_clk_reg(pf, NULL);
1078 /* Update the cached PHC time stored in the PF structure */
1079 WRITE_ONCE(pf->ptp.cached_phc_time, systime);
1080 WRITE_ONCE(pf->ptp.cached_phc_jiffies, jiffies);
1082 if (test_and_set_bit(ICE_CFG_BUSY, pf->state))
1085 ice_for_each_vsi(pf, i) {
1086 struct ice_vsi *vsi = pf->vsi[i];
1092 if (vsi->type != ICE_VSI_PF)
1095 ice_for_each_rxq(vsi, j) {
1096 if (!vsi->rx_rings[j])
1098 WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime);
1101 clear_bit(ICE_CFG_BUSY, pf->state);
1107 * ice_ptp_reset_cached_phctime - Reset cached PHC time after an update
1108 * @pf: Board specific private structure
1110 * This function must be called when the cached PHC time is no longer valid,
1111 * such as after a time adjustment. It marks any currently outstanding Tx
1112 * timestamps as stale and updates the cached PHC time for both the PF and Rx
1115 * If updating the PHC time cannot be done immediately, a warning message is
1116 * logged and the work item is scheduled immediately to minimize the window
1117 * with a wrong cached timestamp.
1119 static void ice_ptp_reset_cached_phctime(struct ice_pf *pf)
1121 struct device *dev = ice_pf_to_dev(pf);
1124 /* Update the cached PHC time immediately if possible, otherwise
1125 * schedule the work item to execute soon.
1127 err = ice_ptp_update_cached_phctime(pf);
1129 /* If another thread is updating the Rx rings, we won't
1130 * properly reset them here. This could lead to reporting of
1131 * invalid timestamps, but there isn't much we can do.
1133 dev_warn(dev, "%s: ICE_CFG_BUSY, unable to immediately update cached PHC time\n",
1136 /* Queue the work item to update the Rx rings when possible */
1137 kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work,
1138 msecs_to_jiffies(10));
1141 /* Mark any outstanding timestamps as stale, since they might have
1142 * been captured in hardware before the time update. This could lead
1143 * to us extending them with the wrong cached value resulting in
1144 * incorrect timestamp values.
1146 ice_ptp_mark_tx_tracker_stale(&pf->ptp.port.tx);
1150 * ice_ptp_write_init - Set PHC time to provided value
1151 * @pf: Board private structure
1152 * @ts: timespec structure that holds the new time value
1154 * Set the PHC time to the specified time provided in the timespec.
1156 static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts)
1158 u64 ns = timespec64_to_ns(ts);
1159 struct ice_hw *hw = &pf->hw;
1161 return ice_ptp_init_time(hw, ns);
1165 * ice_ptp_write_adj - Adjust PHC clock time atomically
1166 * @pf: Board private structure
1167 * @adj: Adjustment in nanoseconds
1169 * Perform an atomic adjustment of the PHC time by the specified number of
1172 static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj)
1174 struct ice_hw *hw = &pf->hw;
1176 return ice_ptp_adj_clock(hw, adj);
1180 * ice_base_incval - Get base timer increment value
1181 * @pf: Board private structure
1183 * Look up the base timer increment value for this device. The base increment
1184 * value is used to define the nominal clock tick rate. This increment value
1185 * is programmed during device initialization. It is also used as the basis
1186 * for calculating adjustments using scaled_ppm.
1188 static u64 ice_base_incval(struct ice_pf *pf)
1190 struct ice_hw *hw = &pf->hw;
1193 incval = ice_get_base_incval(hw);
1195 dev_dbg(ice_pf_to_dev(pf), "PTP: using base increment value of 0x%016llx\n",
1202 * ice_ptp_check_tx_fifo - Check whether Tx FIFO is in an OK state
1203 * @port: PTP port for which Tx FIFO is checked
1205 static int ice_ptp_check_tx_fifo(struct ice_ptp_port *port)
1207 int offs = port->port_num % ICE_PORTS_PER_QUAD;
1208 int quad = ICE_GET_QUAD_NUM(port->port_num);
1214 pf = ptp_port_to_pf(port);
1217 if (port->tx_fifo_busy_cnt == FIFO_OK)
1220 /* need to read FIFO state */
1221 if (offs == 0 || offs == 1)
1222 err = ice_read_quad_reg_e82x(hw, quad, Q_REG_FIFO01_STATUS,
1225 err = ice_read_quad_reg_e82x(hw, quad, Q_REG_FIFO23_STATUS,
1229 dev_err(ice_pf_to_dev(pf), "PTP failed to check port %d Tx FIFO, err %d\n",
1230 port->port_num, err);
1235 phy_sts = FIELD_GET(Q_REG_FIFO13_M, val);
1237 phy_sts = FIELD_GET(Q_REG_FIFO02_M, val);
1239 if (phy_sts & FIFO_EMPTY) {
1240 port->tx_fifo_busy_cnt = FIFO_OK;
1244 port->tx_fifo_busy_cnt++;
1246 dev_dbg(ice_pf_to_dev(pf), "Try %d, port %d FIFO not empty\n",
1247 port->tx_fifo_busy_cnt, port->port_num);
1249 if (port->tx_fifo_busy_cnt == ICE_PTP_FIFO_NUM_CHECKS) {
1250 dev_dbg(ice_pf_to_dev(pf),
1251 "Port %d Tx FIFO still not empty; resetting quad %d\n",
1252 port->port_num, quad);
1253 ice_ptp_reset_ts_memory_quad_e82x(hw, quad);
1254 port->tx_fifo_busy_cnt = FIFO_OK;
1262 * ice_ptp_wait_for_offsets - Check for valid Tx and Rx offsets
1263 * @work: Pointer to the kthread_work structure for this task
1265 * Check whether hardware has completed measuring the Tx and Rx offset values
1266 * used to configure and enable vernier timestamp calibration.
1268 * Once the offset in either direction is measured, configure the associated
1269 * registers with the calibrated offset values and enable timestamping. The Tx
1270 * and Rx directions are configured independently as soon as their associated
1271 * offsets are known.
1273 * This function reschedules itself until both Tx and Rx calibration have
1276 static void ice_ptp_wait_for_offsets(struct kthread_work *work)
1278 struct ice_ptp_port *port;
1284 port = container_of(work, struct ice_ptp_port, ov_work.work);
1285 pf = ptp_port_to_pf(port);
1288 if (ice_is_reset_in_progress(pf->state)) {
1289 /* wait for device driver to complete reset */
1290 kthread_queue_delayed_work(pf->ptp.kworker,
1292 msecs_to_jiffies(100));
1296 tx_err = ice_ptp_check_tx_fifo(port);
1298 tx_err = ice_phy_cfg_tx_offset_e82x(hw, port->port_num);
1299 rx_err = ice_phy_cfg_rx_offset_e82x(hw, port->port_num);
1300 if (tx_err || rx_err) {
1301 /* Tx and/or Rx offset not yet configured, try again later */
1302 kthread_queue_delayed_work(pf->ptp.kworker,
1304 msecs_to_jiffies(100));
1310 * ice_ptp_port_phy_stop - Stop timestamping for a PHY port
1311 * @ptp_port: PTP port to stop
1314 ice_ptp_port_phy_stop(struct ice_ptp_port *ptp_port)
1316 struct ice_pf *pf = ptp_port_to_pf(ptp_port);
1317 u8 port = ptp_port->port_num;
1318 struct ice_hw *hw = &pf->hw;
1321 if (ice_is_e810(hw))
1324 mutex_lock(&ptp_port->ps_lock);
1326 switch (ice_get_phy_model(hw)) {
1327 case ICE_PHY_ETH56G:
1328 err = ice_stop_phy_timer_eth56g(hw, port, true);
1331 kthread_cancel_delayed_work_sync(&ptp_port->ov_work);
1333 err = ice_stop_phy_timer_e82x(hw, port, true);
1338 if (err && err != -EBUSY)
1339 dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d down, err %d\n",
1342 mutex_unlock(&ptp_port->ps_lock);
1348 * ice_ptp_port_phy_restart - (Re)start and calibrate PHY timestamping
1349 * @ptp_port: PTP port for which the PHY start is set
1351 * Start the PHY timestamping block, and initiate Vernier timestamping
1352 * calibration. If timestamping cannot be calibrated (such as if link is down)
1353 * then disable the timestamping block instead.
1356 ice_ptp_port_phy_restart(struct ice_ptp_port *ptp_port)
1358 struct ice_pf *pf = ptp_port_to_pf(ptp_port);
1359 u8 port = ptp_port->port_num;
1360 struct ice_hw *hw = &pf->hw;
1361 unsigned long flags;
1364 if (ice_is_e810(hw))
1367 if (!ptp_port->link_up)
1368 return ice_ptp_port_phy_stop(ptp_port);
1370 mutex_lock(&ptp_port->ps_lock);
1372 switch (ice_get_phy_model(hw)) {
1373 case ICE_PHY_ETH56G:
1374 err = ice_start_phy_timer_eth56g(hw, port);
1377 /* Start the PHY timer in Vernier mode */
1378 kthread_cancel_delayed_work_sync(&ptp_port->ov_work);
1380 /* temporarily disable Tx timestamps while calibrating
1383 spin_lock_irqsave(&ptp_port->tx.lock, flags);
1384 ptp_port->tx.calibrating = true;
1385 spin_unlock_irqrestore(&ptp_port->tx.lock, flags);
1386 ptp_port->tx_fifo_busy_cnt = 0;
1388 /* Start the PHY timer in Vernier mode */
1389 err = ice_start_phy_timer_e82x(hw, port);
1393 /* Enable Tx timestamps right away */
1394 spin_lock_irqsave(&ptp_port->tx.lock, flags);
1395 ptp_port->tx.calibrating = false;
1396 spin_unlock_irqrestore(&ptp_port->tx.lock, flags);
1398 kthread_queue_delayed_work(pf->ptp.kworker, &ptp_port->ov_work,
1406 dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d up, err %d\n",
1409 mutex_unlock(&ptp_port->ps_lock);
1415 * ice_ptp_link_change - Reconfigure PTP after link status change
1416 * @pf: Board private structure
1417 * @linkup: Link is up or down
1419 void ice_ptp_link_change(struct ice_pf *pf, bool linkup)
1421 struct ice_ptp_port *ptp_port;
1422 struct ice_hw *hw = &pf->hw;
1424 if (pf->ptp.state != ICE_PTP_READY)
1427 ptp_port = &pf->ptp.port;
1429 /* Update cached link status for this port immediately */
1430 ptp_port->link_up = linkup;
1432 /* Skip HW writes if reset is in progress */
1433 if (pf->hw.reset_ongoing)
1435 switch (ice_get_phy_model(hw)) {
1437 /* Do not reconfigure E810 PHY */
1439 case ICE_PHY_ETH56G:
1441 ice_ptp_port_phy_restart(ptp_port);
1444 dev_warn(ice_pf_to_dev(pf), "%s: Unknown PHY type\n", __func__);
1449 * ice_ptp_cfg_phy_interrupt - Configure PHY interrupt settings
1450 * @pf: PF private structure
1451 * @ena: bool value to enable or disable interrupt
1452 * @threshold: Minimum number of packets at which intr is triggered
1454 * Utility function to configure all the PHY interrupt settings, including
1455 * whether the PHY interrupt is enabled, and what threshold to use. Also
1456 * configures The E82X timestamp owner to react to interrupts from all PHYs.
1458 * Return: 0 on success, -EOPNOTSUPP when PHY model incorrect, other error codes
1459 * when failed to configure PHY interrupt for E82X
1461 static int ice_ptp_cfg_phy_interrupt(struct ice_pf *pf, bool ena, u32 threshold)
1463 struct device *dev = ice_pf_to_dev(pf);
1464 struct ice_hw *hw = &pf->hw;
1466 ice_ptp_reset_ts_memory(hw);
1468 switch (ice_get_phy_model(hw)) {
1469 case ICE_PHY_ETH56G: {
1472 for (port = 0; port < hw->ptp.num_lports; port++) {
1475 err = ice_phy_cfg_intr_eth56g(hw, port, ena, threshold);
1477 dev_err(dev, "Failed to configure PHY interrupt for port %d, err %d\n",
1485 case ICE_PHY_E82X: {
1488 for (quad = 0; quad < ICE_GET_QUAD_NUM(hw->ptp.num_lports);
1492 err = ice_phy_cfg_intr_e82x(hw, quad, ena, threshold);
1494 dev_err(dev, "Failed to configure PHY interrupt for quad %d, err %d\n",
1506 dev_warn(dev, "%s: Unexpected PHY model %d\n", __func__,
1507 ice_get_phy_model(hw));
1513 * ice_ptp_reset_phy_timestamping - Reset PHY timestamping block
1514 * @pf: Board private structure
1516 static void ice_ptp_reset_phy_timestamping(struct ice_pf *pf)
1518 ice_ptp_port_phy_restart(&pf->ptp.port);
1522 * ice_ptp_restart_all_phy - Restart all PHYs to recalibrate timestamping
1523 * @pf: Board private structure
1525 static void ice_ptp_restart_all_phy(struct ice_pf *pf)
1527 struct list_head *entry;
1529 list_for_each(entry, &pf->adapter->ports.ports) {
1530 struct ice_ptp_port *port = list_entry(entry,
1531 struct ice_ptp_port,
1535 ice_ptp_port_phy_restart(port);
1540 * ice_ptp_adjfine - Adjust clock increment rate
1541 * @info: the driver's PTP info structure
1542 * @scaled_ppm: Parts per million with 16-bit fractional field
1544 * Adjust the frequency of the clock by the indicated scaled ppm from the
1547 static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
1549 struct ice_pf *pf = ptp_info_to_pf(info);
1550 struct ice_hw *hw = &pf->hw;
1554 incval = adjust_by_scaled_ppm(ice_base_incval(pf), scaled_ppm);
1555 err = ice_ptp_write_incval_locked(hw, incval);
1557 dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n",
1566 * ice_ptp_extts_event - Process PTP external clock event
1567 * @pf: Board private structure
1569 void ice_ptp_extts_event(struct ice_pf *pf)
1571 struct ptp_clock_event event;
1572 struct ice_hw *hw = &pf->hw;
1576 /* Don't process timestamp events if PTP is not ready */
1577 if (pf->ptp.state != ICE_PTP_READY)
1580 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1581 /* Event time is captured by one of the two matched registers
1582 * GLTSYN_EVNT_L: 32 LSB of sampled time event
1583 * GLTSYN_EVNT_H: 32 MSB of sampled time event
1584 * Event is defined in GLTSYN_EVNT_0 register
1586 for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) {
1589 /* Check if channel is enabled */
1590 if (!(pf->ptp.ext_ts_irq & (1 << chan)))
1593 lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx));
1594 hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx));
1595 event.timestamp = (u64)hi << 32 | lo;
1597 /* Add delay compensation */
1598 pin_desc_idx = ice_ptp_find_pin_idx(pf, PTP_PF_EXTTS, chan);
1599 if (pin_desc_idx >= 0) {
1600 const struct ice_ptp_pin_desc *desc;
1602 desc = &pf->ptp.ice_pin_desc[pin_desc_idx];
1603 event.timestamp -= desc->delay[0];
1606 event.type = PTP_CLOCK_EXTTS;
1608 pf->ptp.ext_ts_irq &= ~(1 << chan);
1609 ptp_clock_event(pf->ptp.clock, &event);
1614 * ice_ptp_cfg_extts - Configure EXTTS pin and channel
1615 * @pf: Board private structure
1616 * @rq: External timestamp request
1617 * @on: Enable/disable flag
1619 * Configure an external timestamp event on the requested channel.
1621 * Return: 0 on success, negative error code otherwise
1623 static int ice_ptp_cfg_extts(struct ice_pf *pf, struct ptp_extts_request *rq,
1626 u32 aux_reg, gpio_reg, irq_reg;
1627 struct ice_hw *hw = &pf->hw;
1628 unsigned int chan, gpio_pin;
1632 /* Reject requests with unsupported flags */
1634 if (rq->flags & ~(PTP_ENABLE_FEATURE |
1640 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1643 pin_desc_idx = ice_ptp_find_pin_idx(pf, PTP_PF_EXTTS, chan);
1644 if (pin_desc_idx < 0)
1647 gpio_pin = pf->ptp.ice_pin_desc[pin_desc_idx].gpio[0];
1648 irq_reg = rd32(hw, PFINT_OICR_ENA);
1651 /* Enable the interrupt */
1652 irq_reg |= PFINT_OICR_TSYN_EVNT_M;
1653 aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M;
1655 #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE BIT(0)
1656 #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE BIT(1)
1658 /* set event level to requested edge */
1659 if (rq->flags & PTP_FALLING_EDGE)
1660 aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE;
1661 if (rq->flags & PTP_RISING_EDGE)
1662 aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE;
1664 /* Write GPIO CTL reg.
1665 * 0x1 is input sampled by EVENT register(channel)
1666 * + num_in_channels * tmr_idx
1668 gpio_reg = FIELD_PREP(GLGEN_GPIO_CTL_PIN_FUNC_M,
1669 1 + chan + (tmr_idx * 3));
1671 bool last_enabled = true;
1673 /* clear the values we set to reset defaults */
1677 for (unsigned int i = 0; i < pf->ptp.info.n_ext_ts; i++)
1678 if ((pf->ptp.extts_rqs[i].flags &
1679 PTP_ENABLE_FEATURE) &&
1681 last_enabled = false;
1685 irq_reg &= ~PFINT_OICR_TSYN_EVNT_M;
1688 wr32(hw, PFINT_OICR_ENA, irq_reg);
1689 wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg);
1690 wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg);
1696 * ice_ptp_disable_all_extts - Disable all EXTTS channels
1697 * @pf: Board private structure
1699 static void ice_ptp_disable_all_extts(struct ice_pf *pf)
1701 for (unsigned int i = 0; i < pf->ptp.info.n_ext_ts ; i++)
1702 if (pf->ptp.extts_rqs[i].flags & PTP_ENABLE_FEATURE)
1703 ice_ptp_cfg_extts(pf, &pf->ptp.extts_rqs[i],
1706 synchronize_irq(pf->oicr_irq.virq);
1710 * ice_ptp_enable_all_extts - Enable all EXTTS channels
1711 * @pf: Board private structure
1713 * Called during reset to restore user configuration.
1715 static void ice_ptp_enable_all_extts(struct ice_pf *pf)
1717 for (unsigned int i = 0; i < pf->ptp.info.n_ext_ts ; i++)
1718 if (pf->ptp.extts_rqs[i].flags & PTP_ENABLE_FEATURE)
1719 ice_ptp_cfg_extts(pf, &pf->ptp.extts_rqs[i],
1724 * ice_ptp_write_perout - Write periodic wave parameters to HW
1725 * @hw: pointer to the HW struct
1726 * @chan: target channel
1727 * @gpio_pin: target GPIO pin
1728 * @start: target time to start periodic output
1729 * @period: target period
1731 * Return: 0 on success, negative error code otherwise
1733 static int ice_ptp_write_perout(struct ice_hw *hw, unsigned int chan,
1734 unsigned int gpio_pin, u64 start, u64 period)
1737 u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
1740 /* 0. Reset mode & out_en in AUX_OUT */
1741 wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0);
1743 if (ice_is_e825c(hw)) {
1746 /* Enable/disable CGU 1PPS output for E825C */
1747 err = ice_cgu_cfg_pps_out(hw, !!period);
1752 /* 1. Write perout with half of required period value.
1753 * HW toggles output when source clock hits the TGT and then adds
1754 * GLTSYN_CLKO value to the target, so it ends up with 50% duty cycle.
1758 /* For proper operation, GLTSYN_CLKO must be larger than clock tick and
1759 * period has to fit in 32 bit register.
1762 if (!!period && (period <= MIN_PULSE || period > U32_MAX)) {
1763 dev_err(ice_hw_to_dev(hw), "CLK period ticks must be >= %d && <= 2^32",
1768 wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period));
1770 /* 2. Write TARGET time */
1771 wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start));
1772 wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start));
1774 /* 3. Write AUX_OUT register */
1776 val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M;
1777 wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val);
1779 /* 4. write GPIO CTL reg */
1780 val = GLGEN_GPIO_CTL_PIN_DIR_M;
1782 val |= FIELD_PREP(GLGEN_GPIO_CTL_PIN_FUNC_M,
1783 8 + chan + (tmr_idx * 4));
1785 wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val);
1791 * ice_ptp_cfg_perout - Configure clock to generate periodic wave
1792 * @pf: Board private structure
1793 * @rq: Periodic output request
1794 * @on: Enable/disable flag
1796 * Configure the internal clock generator modules to generate the clock wave of
1799 * Return: 0 on success, negative error code otherwise
1801 static int ice_ptp_cfg_perout(struct ice_pf *pf, struct ptp_perout_request *rq,
1804 unsigned int gpio_pin, prop_delay_ns;
1805 u64 clk, period, start, phase;
1806 struct ice_hw *hw = &pf->hw;
1809 if (rq->flags & ~PTP_PEROUT_PHASE)
1812 pin_desc_idx = ice_ptp_find_pin_idx(pf, PTP_PF_PEROUT, rq->index);
1813 if (pin_desc_idx < 0)
1816 gpio_pin = pf->ptp.ice_pin_desc[pin_desc_idx].gpio[1];
1817 prop_delay_ns = pf->ptp.ice_pin_desc[pin_desc_idx].delay[1];
1818 period = rq->period.sec * NSEC_PER_SEC + rq->period.nsec;
1820 /* If we're disabling the output or period is 0, clear out CLKO and TGT
1821 * and keep output level low.
1824 return ice_ptp_write_perout(hw, rq->index, gpio_pin, 0, 0);
1826 if (strncmp(pf->ptp.pin_desc[pin_desc_idx].name, "1PPS", 64) == 0 &&
1827 period != NSEC_PER_SEC && hw->ptp.phy_model == ICE_PHY_E82X) {
1828 dev_err(ice_pf_to_dev(pf), "1PPS pin supports only 1 s period\n");
1833 dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n");
1837 start = rq->start.sec * NSEC_PER_SEC + rq->start.nsec;
1839 /* If PTP_PEROUT_PHASE is set, rq has phase instead of start time */
1840 if (rq->flags & PTP_PEROUT_PHASE)
1843 div64_u64_rem(start, period, &phase);
1845 /* If we have only phase or start time is in the past, start the timer
1846 * at the next multiple of period, maintaining phase.
1848 clk = ice_ptp_read_src_clk_reg(pf, NULL);
1849 if (rq->flags & PTP_PEROUT_PHASE || start <= clk - prop_delay_ns)
1850 start = div64_u64(clk + period - 1, period) * period + phase;
1852 /* Compensate for propagation delay from the generator to the pin. */
1853 start -= prop_delay_ns;
1855 return ice_ptp_write_perout(hw, rq->index, gpio_pin, start, period);
1859 * ice_ptp_disable_all_perout - Disable all currently configured outputs
1860 * @pf: Board private structure
1862 * Disable all currently configured clock outputs. This is necessary before
1863 * certain changes to the PTP hardware clock. Use ice_ptp_enable_all_perout to
1864 * re-enable the clocks again.
1866 static void ice_ptp_disable_all_perout(struct ice_pf *pf)
1868 for (unsigned int i = 0; i < pf->ptp.info.n_per_out; i++)
1869 if (pf->ptp.perout_rqs[i].period.sec ||
1870 pf->ptp.perout_rqs[i].period.nsec)
1871 ice_ptp_cfg_perout(pf, &pf->ptp.perout_rqs[i],
1876 * ice_ptp_enable_all_perout - Enable all configured periodic clock outputs
1877 * @pf: Board private structure
1879 * Enable all currently configured clock outputs. Use this after
1880 * ice_ptp_disable_all_perout to reconfigure the output signals according to
1881 * their configuration.
1883 static void ice_ptp_enable_all_perout(struct ice_pf *pf)
1885 for (unsigned int i = 0; i < pf->ptp.info.n_per_out; i++)
1886 if (pf->ptp.perout_rqs[i].period.sec ||
1887 pf->ptp.perout_rqs[i].period.nsec)
1888 ice_ptp_cfg_perout(pf, &pf->ptp.perout_rqs[i],
1893 * ice_ptp_disable_shared_pin - Disable enabled pin that shares GPIO
1894 * @pf: Board private structure
1896 * @func: Assigned function
1898 * Return: 0 on success, negative error code otherwise
1900 static int ice_ptp_disable_shared_pin(struct ice_pf *pf, unsigned int pin,
1901 enum ptp_pin_function func)
1903 unsigned int gpio_pin;
1907 gpio_pin = pf->ptp.ice_pin_desc[pin].gpio[1];
1910 gpio_pin = pf->ptp.ice_pin_desc[pin].gpio[0];
1916 for (unsigned int i = 0; i < pf->ptp.info.n_pins; i++) {
1917 struct ptp_pin_desc *pin_desc = &pf->ptp.pin_desc[i];
1918 unsigned int chan = pin_desc->chan;
1920 /* Skip pin idx from the request */
1924 if (pin_desc->func == PTP_PF_PEROUT &&
1925 pf->ptp.ice_pin_desc[i].gpio[1] == gpio_pin) {
1926 pf->ptp.perout_rqs[chan].period.sec = 0;
1927 pf->ptp.perout_rqs[chan].period.nsec = 0;
1928 pin_desc->func = PTP_PF_NONE;
1930 dev_dbg(ice_pf_to_dev(pf), "Disabling pin %u with shared output GPIO pin %u\n",
1932 return ice_ptp_cfg_perout(pf, &pf->ptp.perout_rqs[chan],
1934 } else if (pf->ptp.pin_desc->func == PTP_PF_EXTTS &&
1935 pf->ptp.ice_pin_desc[i].gpio[0] == gpio_pin) {
1936 pf->ptp.extts_rqs[chan].flags &= ~PTP_ENABLE_FEATURE;
1937 pin_desc->func = PTP_PF_NONE;
1939 dev_dbg(ice_pf_to_dev(pf), "Disabling pin %u with shared input GPIO pin %u\n",
1941 return ice_ptp_cfg_extts(pf, &pf->ptp.extts_rqs[chan],
1950 * ice_verify_pin - verify if pin supports requested pin function
1951 * @info: the driver's PTP info structure
1953 * @func: Assigned function
1954 * @chan: Assigned channel
1956 * Return: 0 on success, -EOPNOTSUPP when function is not supported.
1958 static int ice_verify_pin(struct ptp_clock_info *info, unsigned int pin,
1959 enum ptp_pin_function func, unsigned int chan)
1961 struct ice_pf *pf = ptp_info_to_pf(info);
1962 const struct ice_ptp_pin_desc *pin_desc;
1964 pin_desc = &pf->ptp.ice_pin_desc[pin];
1966 /* Is assigned function allowed? */
1969 if (pin_desc->gpio[0] < 0)
1973 if (pin_desc->gpio[1] < 0)
1978 case PTP_PF_PHYSYNC:
1983 /* On adapters with SMA_CTRL disable other pins that share same GPIO */
1984 if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) {
1985 ice_ptp_disable_shared_pin(pf, pin, func);
1986 pf->ptp.pin_desc[pin].func = func;
1987 pf->ptp.pin_desc[pin].chan = chan;
1988 return ice_ptp_set_sma_cfg(pf);
1995 * ice_ptp_gpio_enable - Enable/disable ancillary features of PHC
1996 * @info: The driver's PTP info structure
1997 * @rq: The requested feature to change
1998 * @on: Enable/disable flag
2000 * Return: 0 on success, negative error code otherwise
2002 static int ice_ptp_gpio_enable(struct ptp_clock_info *info,
2003 struct ptp_clock_request *rq, int on)
2005 struct ice_pf *pf = ptp_info_to_pf(info);
2009 case PTP_CLK_REQ_PEROUT:
2011 struct ptp_perout_request *cached =
2012 &pf->ptp.perout_rqs[rq->perout.index];
2014 err = ice_ptp_cfg_perout(pf, &rq->perout, on);
2016 *cached = rq->perout;
2018 cached->period.sec = 0;
2019 cached->period.nsec = 0;
2023 case PTP_CLK_REQ_EXTTS:
2025 struct ptp_extts_request *cached =
2026 &pf->ptp.extts_rqs[rq->extts.index];
2028 err = ice_ptp_cfg_extts(pf, &rq->extts, on);
2030 *cached = rq->extts;
2032 cached->flags &= ~PTP_ENABLE_FEATURE;
2041 * ice_ptp_gettimex64 - Get the time of the clock
2042 * @info: the driver's PTP info structure
2043 * @ts: timespec64 structure to hold the current time value
2044 * @sts: Optional parameter for holding a pair of system timestamps from
2045 * the system clock. Will be ignored if NULL is given.
2047 * Read the device clock and return the correct value on ns, after converting it
2048 * into a timespec struct.
2051 ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts,
2052 struct ptp_system_timestamp *sts)
2054 struct ice_pf *pf = ptp_info_to_pf(info);
2057 time_ns = ice_ptp_read_src_clk_reg(pf, sts);
2058 *ts = ns_to_timespec64(time_ns);
2063 * ice_ptp_settime64 - Set the time of the clock
2064 * @info: the driver's PTP info structure
2065 * @ts: timespec64 structure that holds the new time value
2067 * Set the device clock to the user input value. The conversion from timespec
2068 * to ns happens in the write function.
2071 ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts)
2073 struct ice_pf *pf = ptp_info_to_pf(info);
2074 struct timespec64 ts64 = *ts;
2075 struct ice_hw *hw = &pf->hw;
2078 /* For Vernier mode on E82X, we need to recalibrate after new settime.
2079 * Start with marking timestamps as invalid.
2081 if (ice_get_phy_model(hw) == ICE_PHY_E82X) {
2082 err = ice_ptp_clear_phy_offset_ready_e82x(hw);
2084 dev_warn(ice_pf_to_dev(pf), "Failed to mark timestamps as invalid before settime\n");
2087 if (!ice_ptp_lock(hw)) {
2092 /* Disable periodic outputs */
2093 ice_ptp_disable_all_perout(pf);
2095 err = ice_ptp_write_init(pf, &ts64);
2099 ice_ptp_reset_cached_phctime(pf);
2101 /* Reenable periodic outputs */
2102 ice_ptp_enable_all_perout(pf);
2104 /* Recalibrate and re-enable timestamp blocks for E822/E823 */
2105 if (ice_get_phy_model(hw) == ICE_PHY_E82X)
2106 ice_ptp_restart_all_phy(pf);
2109 dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err);
2117 * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment
2118 * @info: the driver's PTP info structure
2119 * @delta: Offset in nanoseconds to adjust the time by
2121 static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta)
2123 struct timespec64 now, then;
2126 then = ns_to_timespec64(delta);
2127 ret = ice_ptp_gettimex64(info, &now, NULL);
2130 now = timespec64_add(now, then);
2132 return ice_ptp_settime64(info, (const struct timespec64 *)&now);
2136 * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta
2137 * @info: the driver's PTP info structure
2138 * @delta: Offset in nanoseconds to adjust the time by
2140 static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
2142 struct ice_pf *pf = ptp_info_to_pf(info);
2143 struct ice_hw *hw = &pf->hw;
2147 dev = ice_pf_to_dev(pf);
2149 /* Hardware only supports atomic adjustments using signed 32-bit
2150 * integers. For any adjustment outside this range, perform
2151 * a non-atomic get->adjust->set flow.
2153 if (delta > S32_MAX || delta < S32_MIN) {
2154 dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta);
2155 return ice_ptp_adjtime_nonatomic(info, delta);
2158 if (!ice_ptp_lock(hw)) {
2159 dev_err(dev, "PTP failed to acquire semaphore in adjtime\n");
2163 /* Disable periodic outputs */
2164 ice_ptp_disable_all_perout(pf);
2166 err = ice_ptp_write_adj(pf, delta);
2168 /* Reenable periodic outputs */
2169 ice_ptp_enable_all_perout(pf);
2174 dev_err(dev, "PTP failed to adjust time, err %d\n", err);
2178 ice_ptp_reset_cached_phctime(pf);
2183 #ifdef CONFIG_ICE_HWTS
2185 * ice_ptp_get_syncdevicetime - Get the cross time stamp info
2186 * @device: Current device time
2187 * @system: System counter value read synchronously with device time
2188 * @ctx: Context provided by timekeeping code
2190 * Read device and system (ART) clock simultaneously and return the corrected
2191 * clock values in ns.
2194 ice_ptp_get_syncdevicetime(ktime_t *device,
2195 struct system_counterval_t *system,
2198 struct ice_pf *pf = (struct ice_pf *)ctx;
2199 struct ice_hw *hw = &pf->hw;
2200 u32 hh_lock, hh_art_ctl;
2203 #define MAX_HH_HW_LOCK_TRIES 5
2204 #define MAX_HH_CTL_LOCK_TRIES 100
2206 for (i = 0; i < MAX_HH_HW_LOCK_TRIES; i++) {
2207 /* Get the HW lock */
2208 hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
2209 if (hh_lock & PFHH_SEM_BUSY_M) {
2210 usleep_range(10000, 15000);
2215 if (hh_lock & PFHH_SEM_BUSY_M) {
2216 dev_err(ice_pf_to_dev(pf), "PTP failed to get hh lock\n");
2220 /* Program cmd to master timer */
2221 ice_ptp_src_cmd(hw, ICE_PTP_READ_TIME);
2223 /* Start the ART and device clock sync sequence */
2224 hh_art_ctl = rd32(hw, GLHH_ART_CTL);
2225 hh_art_ctl = hh_art_ctl | GLHH_ART_CTL_ACTIVE_M;
2226 wr32(hw, GLHH_ART_CTL, hh_art_ctl);
2228 for (i = 0; i < MAX_HH_CTL_LOCK_TRIES; i++) {
2229 /* Wait for sync to complete */
2230 hh_art_ctl = rd32(hw, GLHH_ART_CTL);
2231 if (hh_art_ctl & GLHH_ART_CTL_ACTIVE_M) {
2235 u32 hh_ts_lo, hh_ts_hi, tmr_idx;
2238 tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc;
2240 hh_ts_lo = rd32(hw, GLHH_ART_TIME_L);
2241 hh_ts_hi = rd32(hw, GLHH_ART_TIME_H);
2242 hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo;
2243 system->cycles = hh_ts;
2244 system->cs_id = CSID_X86_ART;
2245 /* Read Device source clock time */
2246 hh_ts_lo = rd32(hw, GLTSYN_HHTIME_L(tmr_idx));
2247 hh_ts_hi = rd32(hw, GLTSYN_HHTIME_H(tmr_idx));
2248 hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo;
2249 *device = ns_to_ktime(hh_ts);
2254 /* Clear the master timer */
2255 ice_ptp_src_cmd(hw, ICE_PTP_NOP);
2257 /* Release HW lock */
2258 hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
2259 hh_lock = hh_lock & ~PFHH_SEM_BUSY_M;
2260 wr32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), hh_lock);
2262 if (i == MAX_HH_CTL_LOCK_TRIES)
2269 * ice_ptp_getcrosststamp_e82x - Capture a device cross timestamp
2270 * @info: the driver's PTP info structure
2271 * @cts: The memory to fill the cross timestamp info
2273 * Capture a cross timestamp between the ART and the device PTP hardware
2274 * clock. Fill the cross timestamp information and report it back to the
2277 * This is only valid for E822 and E823 devices which have support for
2278 * generating the cross timestamp via PCIe PTM.
2280 * In order to correctly correlate the ART timestamp back to the TSC time, the
2281 * CPU must have X86_FEATURE_TSC_KNOWN_FREQ.
2284 ice_ptp_getcrosststamp_e82x(struct ptp_clock_info *info,
2285 struct system_device_crosststamp *cts)
2287 struct ice_pf *pf = ptp_info_to_pf(info);
2289 return get_device_system_crosststamp(ice_ptp_get_syncdevicetime,
2292 #endif /* CONFIG_ICE_HWTS */
2295 * ice_ptp_get_ts_config - ioctl interface to read the timestamping config
2296 * @pf: Board private structure
2299 * Copy the timestamping config to user buffer
2301 int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr)
2303 struct hwtstamp_config *config;
2305 if (pf->ptp.state != ICE_PTP_READY)
2308 config = &pf->ptp.tstamp_config;
2310 return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
2315 * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode
2316 * @pf: Board private structure
2317 * @config: hwtstamp settings requested or saved
2320 ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config)
2322 switch (config->tx_type) {
2323 case HWTSTAMP_TX_OFF:
2324 pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_OFF;
2326 case HWTSTAMP_TX_ON:
2327 pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_ON;
2333 switch (config->rx_filter) {
2334 case HWTSTAMP_FILTER_NONE:
2335 pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2337 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2338 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2339 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2340 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2341 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2342 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2343 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2344 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2345 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2346 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2347 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2348 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2349 case HWTSTAMP_FILTER_NTP_ALL:
2350 case HWTSTAMP_FILTER_ALL:
2351 pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_ALL;
2357 /* Immediately update the device timestamping mode */
2358 ice_ptp_restore_timestamp_mode(pf);
2364 * ice_ptp_set_ts_config - ioctl interface to control the timestamping
2365 * @pf: Board private structure
2368 * Get the user config and store it
2370 int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr)
2372 struct hwtstamp_config config;
2375 if (pf->ptp.state != ICE_PTP_READY)
2378 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
2381 err = ice_ptp_set_timestamp_mode(pf, &config);
2385 /* Return the actual configuration set */
2386 config = pf->ptp.tstamp_config;
2388 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
2393 * ice_ptp_get_rx_hwts - Get packet Rx timestamp in ns
2394 * @rx_desc: Receive descriptor
2395 * @pkt_ctx: Packet context to get the cached time
2397 * The driver receives a notification in the receive descriptor with timestamp.
2399 u64 ice_ptp_get_rx_hwts(const union ice_32b_rx_flex_desc *rx_desc,
2400 const struct ice_pkt_ctx *pkt_ctx)
2402 u64 ts_ns, cached_time;
2405 if (!(rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID))
2408 cached_time = READ_ONCE(pkt_ctx->cached_phctime);
2410 /* Do not report a timestamp if we don't have a cached PHC time */
2414 /* Use ice_ptp_extend_32b_ts directly, using the ring-specific cached
2415 * PHC value, rather than accessing the PF. This also allows us to
2416 * simply pass the upper 32bits of nanoseconds directly. Calling
2417 * ice_ptp_extend_40b_ts is unnecessary as it would just discard these
2420 ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high);
2421 ts_ns = ice_ptp_extend_32b_ts(cached_time, ts_high);
2427 * ice_ptp_setup_pin_cfg - setup PTP pin_config structure
2428 * @pf: Board private structure
2430 static void ice_ptp_setup_pin_cfg(struct ice_pf *pf)
2432 for (unsigned int i = 0; i < pf->ptp.info.n_pins; i++) {
2433 const struct ice_ptp_pin_desc *desc = &pf->ptp.ice_pin_desc[i];
2434 struct ptp_pin_desc *pin = &pf->ptp.pin_desc[i];
2435 const char *name = NULL;
2437 if (!ice_is_feature_supported(pf, ICE_F_SMA_CTRL))
2438 name = ice_pin_names[desc->name_idx];
2439 else if (desc->name_idx != GPIO_NA)
2440 name = ice_pin_names_nvm[desc->name_idx];
2442 strscpy(pin->name, name, sizeof(pin->name));
2447 pf->ptp.info.pin_config = pf->ptp.pin_desc;
2451 * ice_ptp_disable_pins - Disable PTP pins
2452 * @pf: pointer to the PF structure
2454 * Disable the OS access to the SMA pins. Called to clear out the OS
2455 * indications of pin support when we fail to setup the SMA control register.
2457 static void ice_ptp_disable_pins(struct ice_pf *pf)
2459 struct ptp_clock_info *info = &pf->ptp.info;
2461 dev_warn(ice_pf_to_dev(pf), "Failed to configure PTP pin control\n");
2463 info->enable = NULL;
2464 info->verify = NULL;
2467 info->n_per_out = 0;
2471 * ice_ptp_parse_sdp_entries - update ice_ptp_pin_desc structure from NVM
2472 * @pf: pointer to the PF structure
2473 * @entries: SDP connection section from NVM
2474 * @num_entries: number of valid entries in sdp_entries
2475 * @pins: PTP pins array to update
2477 * Return: 0 on success, negative error code otherwise.
2479 static int ice_ptp_parse_sdp_entries(struct ice_pf *pf, __le16 *entries,
2480 unsigned int num_entries,
2481 struct ice_ptp_pin_desc *pins)
2483 unsigned int n_pins = 0;
2486 /* Setup ice_pin_desc array */
2487 for (i = 0; i < ICE_N_PINS_MAX; i++) {
2488 pins[i].name_idx = -1;
2489 pins[i].gpio[0] = -1;
2490 pins[i].gpio[1] = -1;
2493 for (i = 0; i < num_entries; i++) {
2494 u16 entry = le16_to_cpu(entries[i]);
2495 DECLARE_BITMAP(bitmap, GPIO_NA);
2496 unsigned int bitmap_idx;
2500 *bitmap = FIELD_GET(ICE_AQC_NVM_SDP_AC_PIN_M, entry);
2501 dir = !!FIELD_GET(ICE_AQC_NVM_SDP_AC_DIR_M, entry);
2502 gpio = FIELD_GET(ICE_AQC_NVM_SDP_AC_SDP_NUM_M, entry);
2503 for_each_set_bit(bitmap_idx, bitmap, GPIO_NA + 1) {
2506 /* Check if entry's pin bit is valid */
2507 if (bitmap_idx >= NUM_PTP_PINS_NVM &&
2508 bitmap_idx != GPIO_NA)
2511 /* Check if pin already exists */
2512 for (idx = 0; idx < ICE_N_PINS_MAX; idx++)
2513 if (pins[idx].name_idx == bitmap_idx)
2516 if (idx == ICE_N_PINS_MAX) {
2517 /* Pin not found, setup its entry and name */
2519 pins[idx].name_idx = bitmap_idx;
2520 if (bitmap_idx == GPIO_NA)
2521 strscpy(pf->ptp.pin_desc[idx].name,
2522 ice_pin_names[gpio],
2523 sizeof(pf->ptp.pin_desc[idx]
2527 /* Setup in/out GPIO number */
2528 pins[idx].gpio[dir] = gpio;
2532 for (i = 0; i < n_pins; i++) {
2533 dev_dbg(ice_pf_to_dev(pf),
2534 "NVM pin entry[%d] : name_idx %d gpio_out %d gpio_in %d\n",
2535 i, pins[i].name_idx, pins[i].gpio[1], pins[i].gpio[0]);
2538 pf->ptp.info.n_pins = n_pins;
2543 * ice_ptp_set_funcs_e82x - Set specialized functions for E82X support
2544 * @pf: Board private structure
2546 * Assign functions to the PTP capabilities structure for E82X devices.
2547 * Functions which operate across all device families should be set directly
2548 * in ice_ptp_set_caps. Only add functions here which are distinct for E82X
2551 static void ice_ptp_set_funcs_e82x(struct ice_pf *pf)
2553 #ifdef CONFIG_ICE_HWTS
2554 if (boot_cpu_has(X86_FEATURE_ART) &&
2555 boot_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ))
2556 pf->ptp.info.getcrosststamp = ice_ptp_getcrosststamp_e82x;
2558 #endif /* CONFIG_ICE_HWTS */
2559 if (ice_is_e825c(&pf->hw)) {
2560 pf->ptp.ice_pin_desc = ice_pin_desc_e825c;
2561 pf->ptp.info.n_pins = ICE_PIN_DESC_ARR_LEN(ice_pin_desc_e825c);
2563 pf->ptp.ice_pin_desc = ice_pin_desc_e82x;
2564 pf->ptp.info.n_pins = ICE_PIN_DESC_ARR_LEN(ice_pin_desc_e82x);
2566 ice_ptp_setup_pin_cfg(pf);
2570 * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support
2571 * @pf: Board private structure
2573 * Assign functions to the PTP capabiltiies structure for E810 devices.
2574 * Functions which operate across all device families should be set directly
2575 * in ice_ptp_set_caps. Only add functions here which are distinct for E810
2578 static void ice_ptp_set_funcs_e810(struct ice_pf *pf)
2580 __le16 entries[ICE_AQC_NVM_SDP_AC_MAX_SIZE];
2581 struct ice_ptp_pin_desc *desc = NULL;
2582 struct ice_ptp *ptp = &pf->ptp;
2583 unsigned int num_entries;
2586 err = ice_ptp_read_sdp_ac(&pf->hw, entries, &num_entries);
2588 /* SDP section does not exist in NVM or is corrupted */
2589 if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) {
2590 ptp->ice_pin_desc = ice_pin_desc_e810_sma;
2592 ICE_PIN_DESC_ARR_LEN(ice_pin_desc_e810_sma);
2594 pf->ptp.ice_pin_desc = ice_pin_desc_e810;
2595 pf->ptp.info.n_pins =
2596 ICE_PIN_DESC_ARR_LEN(ice_pin_desc_e810);
2600 desc = devm_kcalloc(ice_pf_to_dev(pf), ICE_N_PINS_MAX,
2601 sizeof(struct ice_ptp_pin_desc),
2606 err = ice_ptp_parse_sdp_entries(pf, entries, num_entries, desc);
2610 ptp->ice_pin_desc = (const struct ice_ptp_pin_desc *)desc;
2613 ptp->info.pin_config = ptp->pin_desc;
2614 ice_ptp_setup_pin_cfg(pf);
2616 if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL))
2617 err = ice_ptp_set_sma_cfg(pf);
2620 devm_kfree(ice_pf_to_dev(pf), desc);
2621 ice_ptp_disable_pins(pf);
2626 * ice_ptp_set_caps - Set PTP capabilities
2627 * @pf: Board private structure
2629 static void ice_ptp_set_caps(struct ice_pf *pf)
2631 struct ptp_clock_info *info = &pf->ptp.info;
2632 struct device *dev = ice_pf_to_dev(pf);
2634 snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk",
2635 dev_driver_string(dev), dev_name(dev));
2636 info->owner = THIS_MODULE;
2637 info->max_adj = 100000000;
2638 info->adjtime = ice_ptp_adjtime;
2639 info->adjfine = ice_ptp_adjfine;
2640 info->gettimex64 = ice_ptp_gettimex64;
2641 info->settime64 = ice_ptp_settime64;
2642 info->n_per_out = GLTSYN_TGT_H_IDX_MAX;
2643 info->n_ext_ts = GLTSYN_EVNT_H_IDX_MAX;
2644 info->enable = ice_ptp_gpio_enable;
2645 info->verify = ice_verify_pin;
2647 if (ice_is_e810(&pf->hw))
2648 ice_ptp_set_funcs_e810(pf);
2650 ice_ptp_set_funcs_e82x(pf);
2654 * ice_ptp_create_clock - Create PTP clock device for userspace
2655 * @pf: Board private structure
2657 * This function creates a new PTP clock device. It only creates one if we
2658 * don't already have one. Will return error if it can't create one, but success
2659 * if we already have a device. Should be used by ice_ptp_init to create clock
2660 * initially, and prevent global resets from creating new clock devices.
2662 static long ice_ptp_create_clock(struct ice_pf *pf)
2664 struct ptp_clock_info *info;
2667 /* No need to create a clock device if we already have one */
2671 ice_ptp_set_caps(pf);
2673 info = &pf->ptp.info;
2674 dev = ice_pf_to_dev(pf);
2676 /* Attempt to register the clock before enabling the hardware. */
2677 pf->ptp.clock = ptp_clock_register(info, dev);
2678 if (IS_ERR(pf->ptp.clock)) {
2679 dev_err(ice_pf_to_dev(pf), "Failed to register PTP clock device");
2680 return PTR_ERR(pf->ptp.clock);
2687 * ice_ptp_request_ts - Request an available Tx timestamp index
2688 * @tx: the PTP Tx timestamp tracker to request from
2689 * @skb: the SKB to associate with this timestamp request
2691 s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb)
2693 unsigned long flags;
2696 spin_lock_irqsave(&tx->lock, flags);
2698 /* Check that this tracker is accepting new timestamp requests */
2699 if (!ice_ptp_is_tx_tracker_up(tx)) {
2700 spin_unlock_irqrestore(&tx->lock, flags);
2704 /* Find and set the first available index */
2705 idx = find_next_zero_bit(tx->in_use, tx->len,
2706 tx->last_ll_ts_idx_read + 1);
2708 idx = find_first_zero_bit(tx->in_use, tx->len);
2710 if (idx < tx->len) {
2711 /* We got a valid index that no other thread could have set. Store
2712 * a reference to the skb and the start time to allow discarding old
2715 set_bit(idx, tx->in_use);
2716 clear_bit(idx, tx->stale);
2717 tx->tstamps[idx].start = jiffies;
2718 tx->tstamps[idx].skb = skb_get(skb);
2719 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2720 ice_trace(tx_tstamp_request, skb, idx);
2723 spin_unlock_irqrestore(&tx->lock, flags);
2725 /* return the appropriate PHY timestamp register index, -1 if no
2726 * indexes were available.
2731 return idx + tx->offset;
2735 * ice_ptp_process_ts - Process the PTP Tx timestamps
2736 * @pf: Board private structure
2738 * Returns: ICE_TX_TSTAMP_WORK_PENDING if there are any outstanding Tx
2739 * timestamps that need processing, and ICE_TX_TSTAMP_WORK_DONE otherwise.
2741 enum ice_tx_tstamp_work ice_ptp_process_ts(struct ice_pf *pf)
2743 switch (pf->ptp.tx_interrupt_mode) {
2744 case ICE_PTP_TX_INTERRUPT_NONE:
2745 /* This device has the clock owner handle timestamps for it */
2746 return ICE_TX_TSTAMP_WORK_DONE;
2747 case ICE_PTP_TX_INTERRUPT_SELF:
2748 /* This device handles its own timestamps */
2749 return ice_ptp_tx_tstamp(&pf->ptp.port.tx);
2750 case ICE_PTP_TX_INTERRUPT_ALL:
2751 /* This device handles timestamps for all ports */
2752 return ice_ptp_tx_tstamp_owner(pf);
2754 WARN_ONCE(1, "Unexpected Tx timestamp interrupt mode %u\n",
2755 pf->ptp.tx_interrupt_mode);
2756 return ICE_TX_TSTAMP_WORK_DONE;
2761 * ice_ptp_maybe_trigger_tx_interrupt - Trigger Tx timstamp interrupt
2762 * @pf: Board private structure
2764 * The device PHY issues Tx timestamp interrupts to the driver for processing
2765 * timestamp data from the PHY. It will not interrupt again until all
2766 * current timestamp data is read. In rare circumstances, it is possible that
2767 * the driver fails to read all outstanding data.
2769 * To avoid getting permanently stuck, periodically check if the PHY has
2770 * outstanding timestamp data. If so, trigger an interrupt from software to
2771 * process this data.
2773 static void ice_ptp_maybe_trigger_tx_interrupt(struct ice_pf *pf)
2775 struct device *dev = ice_pf_to_dev(pf);
2776 struct ice_hw *hw = &pf->hw;
2777 bool trigger_oicr = false;
2780 if (ice_is_e810(hw))
2783 if (!ice_pf_src_tmr_owned(pf))
2786 for (i = 0; i < ICE_GET_QUAD_NUM(hw->ptp.num_lports); i++) {
2790 err = ice_get_phy_tx_tstamp_ready(&pf->hw, i, &tstamp_ready);
2791 if (!err && tstamp_ready) {
2792 trigger_oicr = true;
2798 /* Trigger a software interrupt, to ensure this data
2801 dev_dbg(dev, "PTP periodic task detected waiting timestamps. Triggering Tx timestamp interrupt now.\n");
2803 wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
2808 static void ice_ptp_periodic_work(struct kthread_work *work)
2810 struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work);
2811 struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp);
2814 if (pf->ptp.state != ICE_PTP_READY)
2817 err = ice_ptp_update_cached_phctime(pf);
2819 ice_ptp_maybe_trigger_tx_interrupt(pf);
2821 /* Run twice a second or reschedule if phc update failed */
2822 kthread_queue_delayed_work(ptp->kworker, &ptp->work,
2823 msecs_to_jiffies(err ? 10 : 500));
2827 * ice_ptp_prepare_for_reset - Prepare PTP for reset
2828 * @pf: Board private structure
2829 * @reset_type: the reset type being performed
2831 void ice_ptp_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
2833 struct ice_ptp *ptp = &pf->ptp;
2836 if (ptp->state != ICE_PTP_READY)
2839 ptp->state = ICE_PTP_RESETTING;
2841 /* Disable timestamping for both Tx and Rx */
2842 ice_ptp_disable_timestamp_mode(pf);
2844 kthread_cancel_delayed_work_sync(&ptp->work);
2846 if (reset_type == ICE_RESET_PFR)
2849 ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
2851 /* Disable periodic outputs */
2852 ice_ptp_disable_all_perout(pf);
2854 src_tmr = ice_get_ptp_src_clock_index(&pf->hw);
2856 /* Disable source clock */
2857 wr32(&pf->hw, GLTSYN_ENA(src_tmr), (u32)~GLTSYN_ENA_TSYN_ENA_M);
2859 /* Acquire PHC and system timer to restore after reset */
2860 ptp->reset_time = ktime_get_real_ns();
2864 * ice_ptp_rebuild_owner - Initialize PTP clock owner after reset
2865 * @pf: Board private structure
2867 * Companion function for ice_ptp_rebuild() which handles tasks that only the
2868 * PTP clock owner instance should perform.
2870 static int ice_ptp_rebuild_owner(struct ice_pf *pf)
2872 struct ice_ptp *ptp = &pf->ptp;
2873 struct ice_hw *hw = &pf->hw;
2874 struct timespec64 ts;
2878 err = ice_ptp_init_phc(hw);
2882 /* Acquire the global hardware lock */
2883 if (!ice_ptp_lock(hw)) {
2888 /* Write the increment time value to PHY and LAN */
2889 err = ice_ptp_write_incval(hw, ice_base_incval(pf));
2893 /* Write the initial Time value to PHY and LAN using the cached PHC
2894 * time before the reset and time difference between stopping and
2895 * starting the clock.
2897 if (ptp->cached_phc_time) {
2898 time_diff = ktime_get_real_ns() - ptp->reset_time;
2899 ts = ns_to_timespec64(ptp->cached_phc_time + time_diff);
2901 ts = ktime_to_timespec64(ktime_get_real());
2903 err = ice_ptp_write_init(pf, &ts);
2907 /* Release the global hardware lock */
2910 /* Flush software tracking of any outstanding timestamps since we're
2911 * about to flush the PHY timestamp block.
2913 ice_ptp_flush_all_tx_tracker(pf);
2915 if (!ice_is_e810(hw)) {
2916 /* Enable quad interrupts */
2917 err = ice_ptp_cfg_phy_interrupt(pf, true, 1);
2921 ice_ptp_restart_all_phy(pf);
2924 /* Re-enable all periodic outputs and external timestamp events */
2925 ice_ptp_enable_all_perout(pf);
2926 ice_ptp_enable_all_extts(pf);
2936 * ice_ptp_rebuild - Initialize PTP hardware clock support after reset
2937 * @pf: Board private structure
2938 * @reset_type: the reset type being performed
2940 void ice_ptp_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
2942 struct ice_ptp *ptp = &pf->ptp;
2945 if (ptp->state == ICE_PTP_READY) {
2946 ice_ptp_prepare_for_reset(pf, reset_type);
2947 } else if (ptp->state != ICE_PTP_RESETTING) {
2949 dev_err(ice_pf_to_dev(pf), "PTP was not initialized\n");
2953 if (ice_pf_src_tmr_owned(pf) && reset_type != ICE_RESET_PFR) {
2954 err = ice_ptp_rebuild_owner(pf);
2959 ptp->state = ICE_PTP_READY;
2961 /* Start periodic work going */
2962 kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0);
2964 dev_info(ice_pf_to_dev(pf), "PTP reset successful\n");
2968 ptp->state = ICE_PTP_ERROR;
2969 dev_err(ice_pf_to_dev(pf), "PTP reset failed %d\n", err);
2972 static bool ice_is_primary(struct ice_hw *hw)
2974 return ice_is_e825c(hw) && ice_is_dual(hw) ?
2975 !!(hw->dev_caps.nac_topo.mode & ICE_NAC_TOPO_PRIMARY_M) : true;
2978 static int ice_ptp_setup_adapter(struct ice_pf *pf)
2980 if (!ice_pf_src_tmr_owned(pf) || !ice_is_primary(&pf->hw))
2983 pf->adapter->ctrl_pf = pf;
2988 static int ice_ptp_setup_pf(struct ice_pf *pf)
2990 struct ice_ptp *ctrl_ptp = ice_get_ctrl_ptp(pf);
2991 struct ice_ptp *ptp = &pf->ptp;
2993 if (WARN_ON(!ctrl_ptp) || ice_get_phy_model(&pf->hw) == ICE_PHY_UNSUP)
2996 INIT_LIST_HEAD(&ptp->port.list_node);
2997 mutex_lock(&pf->adapter->ports.lock);
2999 list_add(&ptp->port.list_node,
3000 &pf->adapter->ports.ports);
3001 mutex_unlock(&pf->adapter->ports.lock);
3006 static void ice_ptp_cleanup_pf(struct ice_pf *pf)
3008 struct ice_ptp *ptp = &pf->ptp;
3010 if (ice_get_phy_model(&pf->hw) != ICE_PHY_UNSUP) {
3011 mutex_lock(&pf->adapter->ports.lock);
3012 list_del(&ptp->port.list_node);
3013 mutex_unlock(&pf->adapter->ports.lock);
3018 * ice_ptp_clock_index - Get the PTP clock index for this device
3019 * @pf: Board private structure
3021 * Returns: the PTP clock index associated with this PF, or -1 if no PTP clock
3024 int ice_ptp_clock_index(struct ice_pf *pf)
3026 struct ice_ptp *ctrl_ptp = ice_get_ctrl_ptp(pf);
3027 struct ptp_clock *clock;
3031 clock = ctrl_ptp->clock;
3033 return clock ? ptp_clock_index(clock) : -1;
3037 * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device
3038 * @pf: Board private structure
3040 * Setup and initialize a PTP clock device that represents the device hardware
3041 * clock. Save the clock index for other functions connected to the same
3042 * hardware resource.
3044 static int ice_ptp_init_owner(struct ice_pf *pf)
3046 struct ice_hw *hw = &pf->hw;
3047 struct timespec64 ts;
3050 err = ice_ptp_init_phc(hw);
3052 dev_err(ice_pf_to_dev(pf), "Failed to initialize PHC, err %d\n",
3057 /* Acquire the global hardware lock */
3058 if (!ice_ptp_lock(hw)) {
3063 /* Write the increment time value to PHY and LAN */
3064 err = ice_ptp_write_incval(hw, ice_base_incval(pf));
3068 ts = ktime_to_timespec64(ktime_get_real());
3069 /* Write the initial Time value to PHY and LAN */
3070 err = ice_ptp_write_init(pf, &ts);
3074 /* Release the global hardware lock */
3077 /* Configure PHY interrupt settings */
3078 err = ice_ptp_cfg_phy_interrupt(pf, true, 1);
3082 /* Ensure we have a clock device */
3083 err = ice_ptp_create_clock(pf);
3089 pf->ptp.clock = NULL;
3099 * ice_ptp_init_work - Initialize PTP work threads
3100 * @pf: Board private structure
3101 * @ptp: PF PTP structure
3103 static int ice_ptp_init_work(struct ice_pf *pf, struct ice_ptp *ptp)
3105 struct kthread_worker *kworker;
3107 /* Initialize work functions */
3108 kthread_init_delayed_work(&ptp->work, ice_ptp_periodic_work);
3110 /* Allocate a kworker for handling work required for the ports
3111 * connected to the PTP hardware clock.
3113 kworker = kthread_run_worker(0, "ice-ptp-%s",
3114 dev_name(ice_pf_to_dev(pf)));
3115 if (IS_ERR(kworker))
3116 return PTR_ERR(kworker);
3118 ptp->kworker = kworker;
3120 /* Start periodic work going */
3121 kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0);
3127 * ice_ptp_init_port - Initialize PTP port structure
3128 * @pf: Board private structure
3129 * @ptp_port: PTP port structure
3131 static int ice_ptp_init_port(struct ice_pf *pf, struct ice_ptp_port *ptp_port)
3133 struct ice_hw *hw = &pf->hw;
3135 mutex_init(&ptp_port->ps_lock);
3137 switch (ice_get_phy_model(hw)) {
3138 case ICE_PHY_ETH56G:
3139 return ice_ptp_init_tx_eth56g(pf, &ptp_port->tx,
3140 ptp_port->port_num);
3142 return ice_ptp_init_tx_e810(pf, &ptp_port->tx);
3144 kthread_init_delayed_work(&ptp_port->ov_work,
3145 ice_ptp_wait_for_offsets);
3147 return ice_ptp_init_tx_e82x(pf, &ptp_port->tx,
3148 ptp_port->port_num);
3155 * ice_ptp_init_tx_interrupt_mode - Initialize device Tx interrupt mode
3156 * @pf: Board private structure
3158 * Initialize the Tx timestamp interrupt mode for this device. For most device
3159 * types, each PF processes the interrupt and manages its own timestamps. For
3160 * E822-based devices, only the clock owner processes the timestamps. Other
3161 * PFs disable the interrupt and do not process their own timestamps.
3163 static void ice_ptp_init_tx_interrupt_mode(struct ice_pf *pf)
3165 switch (ice_get_phy_model(&pf->hw)) {
3167 /* E822 based PHY has the clock owner process the interrupt
3170 if (ice_pf_src_tmr_owned(pf))
3171 pf->ptp.tx_interrupt_mode = ICE_PTP_TX_INTERRUPT_ALL;
3173 pf->ptp.tx_interrupt_mode = ICE_PTP_TX_INTERRUPT_NONE;
3176 /* other PHY types handle their own Tx interrupt */
3177 pf->ptp.tx_interrupt_mode = ICE_PTP_TX_INTERRUPT_SELF;
3182 * ice_ptp_init - Initialize PTP hardware clock support
3183 * @pf: Board private structure
3185 * Set up the device for interacting with the PTP hardware clock for all
3186 * functions, both the function that owns the clock hardware, and the
3187 * functions connected to the clock hardware.
3189 * The clock owner will allocate and register a ptp_clock with the
3190 * PTP_1588_CLOCK infrastructure. All functions allocate a kthread and work
3191 * items used for asynchronous work such as Tx timestamps and periodic work.
3193 void ice_ptp_init(struct ice_pf *pf)
3195 struct ice_ptp *ptp = &pf->ptp;
3196 struct ice_hw *hw = &pf->hw;
3199 ptp->state = ICE_PTP_INITIALIZING;
3201 lane_num = ice_get_phy_lane_number(hw);
3207 ptp->port.port_num = (u8)lane_num;
3208 ice_ptp_init_hw(hw);
3210 ice_ptp_init_tx_interrupt_mode(pf);
3212 /* If this function owns the clock hardware, it must allocate and
3213 * configure the PTP clock device to represent it.
3215 if (ice_pf_src_tmr_owned(pf) && ice_is_primary(hw)) {
3216 err = ice_ptp_setup_adapter(pf);
3219 err = ice_ptp_init_owner(pf);
3224 err = ice_ptp_setup_pf(pf);
3228 err = ice_ptp_init_port(pf, &ptp->port);
3232 /* Start the PHY timestamping block */
3233 ice_ptp_reset_phy_timestamping(pf);
3235 /* Configure initial Tx interrupt settings */
3236 ice_ptp_cfg_tx_interrupt(pf);
3238 ptp->state = ICE_PTP_READY;
3240 err = ice_ptp_init_work(pf, ptp);
3244 dev_info(ice_pf_to_dev(pf), "PTP init successful\n");
3248 /* If we registered a PTP clock, release it */
3249 if (pf->ptp.clock) {
3250 ptp_clock_unregister(ptp->clock);
3251 pf->ptp.clock = NULL;
3253 ptp->state = ICE_PTP_ERROR;
3254 dev_err(ice_pf_to_dev(pf), "PTP failed %d\n", err);
3258 * ice_ptp_release - Disable the driver/HW support and unregister the clock
3259 * @pf: Board private structure
3261 * This function handles the cleanup work required from the initialization by
3262 * clearing out the important information and unregistering the clock
3264 void ice_ptp_release(struct ice_pf *pf)
3266 if (pf->ptp.state != ICE_PTP_READY)
3269 pf->ptp.state = ICE_PTP_UNINIT;
3271 /* Disable timestamping for both Tx and Rx */
3272 ice_ptp_disable_timestamp_mode(pf);
3274 ice_ptp_cleanup_pf(pf);
3276 ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
3278 ice_ptp_disable_all_extts(pf);
3280 kthread_cancel_delayed_work_sync(&pf->ptp.work);
3282 ice_ptp_port_phy_stop(&pf->ptp.port);
3283 mutex_destroy(&pf->ptp.port.ps_lock);
3284 if (pf->ptp.kworker) {
3285 kthread_destroy_worker(pf->ptp.kworker);
3286 pf->ptp.kworker = NULL;
3292 /* Disable periodic outputs */
3293 ice_ptp_disable_all_perout(pf);
3295 ptp_clock_unregister(pf->ptp.clock);
3296 pf->ptp.clock = NULL;
3298 dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n");