106c16d89SJacob Keller // SPDX-License-Identifier: GPL-2.0 206c16d89SJacob Keller /* Copyright (C) 2021, Intel Corporation. */ 306c16d89SJacob Keller 406c16d89SJacob Keller #include "ice.h" 506c16d89SJacob Keller #include "ice_lib.h" 606c16d89SJacob Keller 706c16d89SJacob Keller /** 8*ea9b847cSJacob Keller * ice_set_tx_tstamp - Enable or disable Tx timestamping 9*ea9b847cSJacob Keller * @pf: The PF pointer to search in 10*ea9b847cSJacob Keller * @on: bool value for whether timestamps are enabled or disabled 11*ea9b847cSJacob Keller */ 12*ea9b847cSJacob Keller static void ice_set_tx_tstamp(struct ice_pf *pf, bool on) 13*ea9b847cSJacob Keller { 14*ea9b847cSJacob Keller struct ice_vsi *vsi; 15*ea9b847cSJacob Keller u32 val; 16*ea9b847cSJacob Keller u16 i; 17*ea9b847cSJacob Keller 18*ea9b847cSJacob Keller vsi = ice_get_main_vsi(pf); 19*ea9b847cSJacob Keller if (!vsi) 20*ea9b847cSJacob Keller return; 21*ea9b847cSJacob Keller 22*ea9b847cSJacob Keller /* Set the timestamp enable flag for all the Tx rings */ 23*ea9b847cSJacob Keller ice_for_each_rxq(vsi, i) { 24*ea9b847cSJacob Keller if (!vsi->tx_rings[i]) 25*ea9b847cSJacob Keller continue; 26*ea9b847cSJacob Keller vsi->tx_rings[i]->ptp_tx = on; 27*ea9b847cSJacob Keller } 28*ea9b847cSJacob Keller 29*ea9b847cSJacob Keller /* Configure the Tx timestamp interrupt */ 30*ea9b847cSJacob Keller val = rd32(&pf->hw, PFINT_OICR_ENA); 31*ea9b847cSJacob Keller if (on) 32*ea9b847cSJacob Keller val |= PFINT_OICR_TSYN_TX_M; 33*ea9b847cSJacob Keller else 34*ea9b847cSJacob Keller val &= ~PFINT_OICR_TSYN_TX_M; 35*ea9b847cSJacob Keller wr32(&pf->hw, PFINT_OICR_ENA, val); 36*ea9b847cSJacob Keller } 37*ea9b847cSJacob Keller 38*ea9b847cSJacob Keller /** 3977a78115SJacob Keller * ice_set_rx_tstamp - Enable or disable Rx timestamping 4077a78115SJacob Keller * @pf: The PF pointer to search in 4177a78115SJacob Keller * @on: bool value for whether timestamps are enabled or disabled 4277a78115SJacob Keller */ 4377a78115SJacob Keller static void ice_set_rx_tstamp(struct ice_pf *pf, bool on) 4477a78115SJacob Keller { 4577a78115SJacob Keller struct ice_vsi *vsi; 4677a78115SJacob Keller u16 i; 4777a78115SJacob Keller 4877a78115SJacob Keller vsi = ice_get_main_vsi(pf); 4977a78115SJacob Keller if (!vsi) 5077a78115SJacob Keller return; 5177a78115SJacob Keller 5277a78115SJacob Keller /* Set the timestamp flag for all the Rx rings */ 5377a78115SJacob Keller ice_for_each_rxq(vsi, i) { 5477a78115SJacob Keller if (!vsi->rx_rings[i]) 5577a78115SJacob Keller continue; 5677a78115SJacob Keller vsi->rx_rings[i]->ptp_rx = on; 5777a78115SJacob Keller } 5877a78115SJacob Keller } 5977a78115SJacob Keller 6077a78115SJacob Keller /** 6177a78115SJacob Keller * ice_ptp_cfg_timestamp - Configure timestamp for init/deinit 6277a78115SJacob Keller * @pf: Board private structure 6377a78115SJacob Keller * @ena: bool value to enable or disable time stamp 6477a78115SJacob Keller * 6577a78115SJacob Keller * This function will configure timestamping during PTP initialization 6677a78115SJacob Keller * and deinitialization 6777a78115SJacob Keller */ 6877a78115SJacob Keller static void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena) 6977a78115SJacob Keller { 70*ea9b847cSJacob Keller ice_set_tx_tstamp(pf, ena); 7177a78115SJacob Keller ice_set_rx_tstamp(pf, ena); 7277a78115SJacob Keller 73*ea9b847cSJacob Keller if (ena) { 7477a78115SJacob Keller pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_ALL; 75*ea9b847cSJacob Keller pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_ON; 76*ea9b847cSJacob Keller } else { 7777a78115SJacob Keller pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE; 78*ea9b847cSJacob Keller pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_OFF; 79*ea9b847cSJacob Keller } 8077a78115SJacob Keller } 8177a78115SJacob Keller 8277a78115SJacob Keller /** 8367569a7fSJacob Keller * ice_get_ptp_clock_index - Get the PTP clock index 8467569a7fSJacob Keller * @pf: the PF pointer 8567569a7fSJacob Keller * 8667569a7fSJacob Keller * Determine the clock index of the PTP clock associated with this device. If 8767569a7fSJacob Keller * this is the PF controlling the clock, just use the local access to the 8867569a7fSJacob Keller * clock device pointer. 8967569a7fSJacob Keller * 9067569a7fSJacob Keller * Otherwise, read from the driver shared parameters to determine the clock 9167569a7fSJacob Keller * index value. 9267569a7fSJacob Keller * 9367569a7fSJacob Keller * Returns: the index of the PTP clock associated with this device, or -1 if 9467569a7fSJacob Keller * there is no associated clock. 9567569a7fSJacob Keller */ 9667569a7fSJacob Keller int ice_get_ptp_clock_index(struct ice_pf *pf) 9767569a7fSJacob Keller { 9867569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 9967569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 10067569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 10167569a7fSJacob Keller u8 tmr_idx; 10267569a7fSJacob Keller u32 value; 10367569a7fSJacob Keller int err; 10467569a7fSJacob Keller 10567569a7fSJacob Keller /* Use the ptp_clock structure if we're the main PF */ 10667569a7fSJacob Keller if (pf->ptp.clock) 10767569a7fSJacob Keller return ptp_clock_index(pf->ptp.clock); 10867569a7fSJacob Keller 10967569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 11067569a7fSJacob Keller if (!tmr_idx) 11167569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 11267569a7fSJacob Keller else 11367569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 11467569a7fSJacob Keller 11567569a7fSJacob Keller err = ice_aq_get_driver_param(hw, param_idx, &value, NULL); 11667569a7fSJacob Keller if (err) { 11767569a7fSJacob Keller dev_err(dev, "Failed to read PTP clock index parameter, err %d aq_err %s\n", 11867569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 11967569a7fSJacob Keller return -1; 12067569a7fSJacob Keller } 12167569a7fSJacob Keller 12267569a7fSJacob Keller /* The PTP clock index is an integer, and will be between 0 and 12367569a7fSJacob Keller * INT_MAX. The highest bit of the driver shared parameter is used to 12467569a7fSJacob Keller * indicate whether or not the currently stored clock index is valid. 12567569a7fSJacob Keller */ 12667569a7fSJacob Keller if (!(value & PTP_SHARED_CLK_IDX_VALID)) 12767569a7fSJacob Keller return -1; 12867569a7fSJacob Keller 12967569a7fSJacob Keller return value & ~PTP_SHARED_CLK_IDX_VALID; 13067569a7fSJacob Keller } 13167569a7fSJacob Keller 13267569a7fSJacob Keller /** 13367569a7fSJacob Keller * ice_set_ptp_clock_index - Set the PTP clock index 13467569a7fSJacob Keller * @pf: the PF pointer 13567569a7fSJacob Keller * 13667569a7fSJacob Keller * Set the PTP clock index for this device into the shared driver parameters, 13767569a7fSJacob Keller * so that other PFs associated with this device can read it. 13867569a7fSJacob Keller * 13967569a7fSJacob Keller * If the PF is unable to store the clock index, it will log an error, but 14067569a7fSJacob Keller * will continue operating PTP. 14167569a7fSJacob Keller */ 14267569a7fSJacob Keller static void ice_set_ptp_clock_index(struct ice_pf *pf) 14367569a7fSJacob Keller { 14467569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 14567569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 14667569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 14767569a7fSJacob Keller u8 tmr_idx; 14867569a7fSJacob Keller u32 value; 14967569a7fSJacob Keller int err; 15067569a7fSJacob Keller 15167569a7fSJacob Keller if (!pf->ptp.clock) 15267569a7fSJacob Keller return; 15367569a7fSJacob Keller 15467569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 15567569a7fSJacob Keller if (!tmr_idx) 15667569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 15767569a7fSJacob Keller else 15867569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 15967569a7fSJacob Keller 16067569a7fSJacob Keller value = (u32)ptp_clock_index(pf->ptp.clock); 16167569a7fSJacob Keller if (value > INT_MAX) { 16267569a7fSJacob Keller dev_err(dev, "PTP Clock index is too large to store\n"); 16367569a7fSJacob Keller return; 16467569a7fSJacob Keller } 16567569a7fSJacob Keller value |= PTP_SHARED_CLK_IDX_VALID; 16667569a7fSJacob Keller 16767569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, value, NULL); 16867569a7fSJacob Keller if (err) { 16967569a7fSJacob Keller dev_err(dev, "Failed to set PTP clock index parameter, err %d aq_err %s\n", 17067569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 17167569a7fSJacob Keller } 17267569a7fSJacob Keller } 17367569a7fSJacob Keller 17467569a7fSJacob Keller /** 17567569a7fSJacob Keller * ice_clear_ptp_clock_index - Clear the PTP clock index 17667569a7fSJacob Keller * @pf: the PF pointer 17767569a7fSJacob Keller * 17867569a7fSJacob Keller * Clear the PTP clock index for this device. Must be called when 17967569a7fSJacob Keller * unregistering the PTP clock, in order to ensure other PFs stop reporting 18067569a7fSJacob Keller * a clock object that no longer exists. 18167569a7fSJacob Keller */ 18267569a7fSJacob Keller static void ice_clear_ptp_clock_index(struct ice_pf *pf) 18367569a7fSJacob Keller { 18467569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 18567569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 18667569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 18767569a7fSJacob Keller u8 tmr_idx; 18867569a7fSJacob Keller int err; 18967569a7fSJacob Keller 19067569a7fSJacob Keller /* Do not clear the index if we don't own the timer */ 19167569a7fSJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned) 19267569a7fSJacob Keller return; 19367569a7fSJacob Keller 19467569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 19567569a7fSJacob Keller if (!tmr_idx) 19667569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 19767569a7fSJacob Keller else 19867569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 19967569a7fSJacob Keller 20067569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, 0, NULL); 20167569a7fSJacob Keller if (err) { 20267569a7fSJacob Keller dev_dbg(dev, "Failed to clear PTP clock index parameter, err %d aq_err %s\n", 20367569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 20467569a7fSJacob Keller } 20567569a7fSJacob Keller } 20667569a7fSJacob Keller 20767569a7fSJacob Keller /** 20806c16d89SJacob Keller * ice_ptp_read_src_clk_reg - Read the source clock register 20906c16d89SJacob Keller * @pf: Board private structure 21006c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 21106c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 21206c16d89SJacob Keller */ 21306c16d89SJacob Keller static u64 21406c16d89SJacob Keller ice_ptp_read_src_clk_reg(struct ice_pf *pf, struct ptp_system_timestamp *sts) 21506c16d89SJacob Keller { 21606c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 21706c16d89SJacob Keller u32 hi, lo, lo2; 21806c16d89SJacob Keller u8 tmr_idx; 21906c16d89SJacob Keller 22006c16d89SJacob Keller tmr_idx = ice_get_ptp_src_clock_index(hw); 22106c16d89SJacob Keller /* Read the system timestamp pre PHC read */ 22206c16d89SJacob Keller if (sts) 22306c16d89SJacob Keller ptp_read_system_prets(sts); 22406c16d89SJacob Keller 22506c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 22606c16d89SJacob Keller 22706c16d89SJacob Keller /* Read the system timestamp post PHC read */ 22806c16d89SJacob Keller if (sts) 22906c16d89SJacob Keller ptp_read_system_postts(sts); 23006c16d89SJacob Keller 23106c16d89SJacob Keller hi = rd32(hw, GLTSYN_TIME_H(tmr_idx)); 23206c16d89SJacob Keller lo2 = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 23306c16d89SJacob Keller 23406c16d89SJacob Keller if (lo2 < lo) { 23506c16d89SJacob Keller /* if TIME_L rolled over read TIME_L again and update 23606c16d89SJacob Keller * system timestamps 23706c16d89SJacob Keller */ 23806c16d89SJacob Keller if (sts) 23906c16d89SJacob Keller ptp_read_system_prets(sts); 24006c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 24106c16d89SJacob Keller if (sts) 24206c16d89SJacob Keller ptp_read_system_postts(sts); 24306c16d89SJacob Keller hi = rd32(hw, GLTSYN_TIME_H(tmr_idx)); 24406c16d89SJacob Keller } 24506c16d89SJacob Keller 24606c16d89SJacob Keller return ((u64)hi << 32) | lo; 24706c16d89SJacob Keller } 24806c16d89SJacob Keller 24906c16d89SJacob Keller /** 25077a78115SJacob Keller * ice_ptp_update_cached_phctime - Update the cached PHC time values 25177a78115SJacob Keller * @pf: Board specific private structure 25277a78115SJacob Keller * 25377a78115SJacob Keller * This function updates the system time values which are cached in the PF 25477a78115SJacob Keller * structure and the Rx rings. 25577a78115SJacob Keller * 25677a78115SJacob Keller * This function must be called periodically to ensure that the cached value 25777a78115SJacob Keller * is never more than 2 seconds old. It must also be called whenever the PHC 25877a78115SJacob Keller * time has been changed. 25977a78115SJacob Keller */ 26077a78115SJacob Keller static void ice_ptp_update_cached_phctime(struct ice_pf *pf) 26177a78115SJacob Keller { 26277a78115SJacob Keller u64 systime; 26377a78115SJacob Keller int i; 26477a78115SJacob Keller 26577a78115SJacob Keller /* Read the current PHC time */ 26677a78115SJacob Keller systime = ice_ptp_read_src_clk_reg(pf, NULL); 26777a78115SJacob Keller 26877a78115SJacob Keller /* Update the cached PHC time stored in the PF structure */ 26977a78115SJacob Keller WRITE_ONCE(pf->ptp.cached_phc_time, systime); 27077a78115SJacob Keller 27177a78115SJacob Keller ice_for_each_vsi(pf, i) { 27277a78115SJacob Keller struct ice_vsi *vsi = pf->vsi[i]; 27377a78115SJacob Keller int j; 27477a78115SJacob Keller 27577a78115SJacob Keller if (!vsi) 27677a78115SJacob Keller continue; 27777a78115SJacob Keller 27877a78115SJacob Keller if (vsi->type != ICE_VSI_PF) 27977a78115SJacob Keller continue; 28077a78115SJacob Keller 28177a78115SJacob Keller ice_for_each_rxq(vsi, j) { 28277a78115SJacob Keller if (!vsi->rx_rings[j]) 28377a78115SJacob Keller continue; 28477a78115SJacob Keller WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime); 28577a78115SJacob Keller } 28677a78115SJacob Keller } 28777a78115SJacob Keller } 28877a78115SJacob Keller 28977a78115SJacob Keller /** 29077a78115SJacob Keller * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b 29177a78115SJacob Keller * @cached_phc_time: recently cached copy of PHC time 29277a78115SJacob Keller * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value 29377a78115SJacob Keller * 29477a78115SJacob Keller * Hardware captures timestamps which contain only 32 bits of nominal 29577a78115SJacob Keller * nanoseconds, as opposed to the 64bit timestamps that the stack expects. 29677a78115SJacob Keller * Note that the captured timestamp values may be 40 bits, but the lower 29777a78115SJacob Keller * 8 bits are sub-nanoseconds and generally discarded. 29877a78115SJacob Keller * 29977a78115SJacob Keller * Extend the 32bit nanosecond timestamp using the following algorithm and 30077a78115SJacob Keller * assumptions: 30177a78115SJacob Keller * 30277a78115SJacob Keller * 1) have a recently cached copy of the PHC time 30377a78115SJacob Keller * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1 30477a78115SJacob Keller * seconds) before or after the PHC time was captured. 30577a78115SJacob Keller * 3) calculate the delta between the cached time and the timestamp 30677a78115SJacob Keller * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was 30777a78115SJacob Keller * captured after the PHC time. In this case, the full timestamp is just 30877a78115SJacob Keller * the cached PHC time plus the delta. 30977a78115SJacob Keller * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the 31077a78115SJacob Keller * timestamp was captured *before* the PHC time, i.e. because the PHC 31177a78115SJacob Keller * cache was updated after the timestamp was captured by hardware. In this 31277a78115SJacob Keller * case, the full timestamp is the cached time minus the inverse delta. 31377a78115SJacob Keller * 31477a78115SJacob Keller * This algorithm works even if the PHC time was updated after a Tx timestamp 31577a78115SJacob Keller * was requested, but before the Tx timestamp event was reported from 31677a78115SJacob Keller * hardware. 31777a78115SJacob Keller * 31877a78115SJacob Keller * This calculation primarily relies on keeping the cached PHC time up to 31977a78115SJacob Keller * date. If the timestamp was captured more than 2^31 nanoseconds after the 32077a78115SJacob Keller * PHC time, it is possible that the lower 32bits of PHC time have 32177a78115SJacob Keller * overflowed more than once, and we might generate an incorrect timestamp. 32277a78115SJacob Keller * 32377a78115SJacob Keller * This is prevented by (a) periodically updating the cached PHC time once 32477a78115SJacob Keller * a second, and (b) discarding any Tx timestamp packet if it has waited for 32577a78115SJacob Keller * a timestamp for more than one second. 32677a78115SJacob Keller */ 32777a78115SJacob Keller static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp) 32877a78115SJacob Keller { 32977a78115SJacob Keller u32 delta, phc_time_lo; 33077a78115SJacob Keller u64 ns; 33177a78115SJacob Keller 33277a78115SJacob Keller /* Extract the lower 32 bits of the PHC time */ 33377a78115SJacob Keller phc_time_lo = (u32)cached_phc_time; 33477a78115SJacob Keller 33577a78115SJacob Keller /* Calculate the delta between the lower 32bits of the cached PHC 33677a78115SJacob Keller * time and the in_tstamp value 33777a78115SJacob Keller */ 33877a78115SJacob Keller delta = (in_tstamp - phc_time_lo); 33977a78115SJacob Keller 34077a78115SJacob Keller /* Do not assume that the in_tstamp is always more recent than the 34177a78115SJacob Keller * cached PHC time. If the delta is large, it indicates that the 34277a78115SJacob Keller * in_tstamp was taken in the past, and should be converted 34377a78115SJacob Keller * forward. 34477a78115SJacob Keller */ 34577a78115SJacob Keller if (delta > (U32_MAX / 2)) { 34677a78115SJacob Keller /* reverse the delta calculation here */ 34777a78115SJacob Keller delta = (phc_time_lo - in_tstamp); 34877a78115SJacob Keller ns = cached_phc_time - delta; 34977a78115SJacob Keller } else { 35077a78115SJacob Keller ns = cached_phc_time + delta; 35177a78115SJacob Keller } 35277a78115SJacob Keller 35377a78115SJacob Keller return ns; 35477a78115SJacob Keller } 35577a78115SJacob Keller 35677a78115SJacob Keller /** 357*ea9b847cSJacob Keller * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds 358*ea9b847cSJacob Keller * @pf: Board private structure 359*ea9b847cSJacob Keller * @in_tstamp: Ingress/egress 40b timestamp value 360*ea9b847cSJacob Keller * 361*ea9b847cSJacob Keller * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal 362*ea9b847cSJacob Keller * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit. 363*ea9b847cSJacob Keller * 364*ea9b847cSJacob Keller * *--------------------------------------------------------------* 365*ea9b847cSJacob Keller * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v | 366*ea9b847cSJacob Keller * *--------------------------------------------------------------* 367*ea9b847cSJacob Keller * 368*ea9b847cSJacob Keller * The low bit is an indicator of whether the timestamp is valid. The next 369*ea9b847cSJacob Keller * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow, 370*ea9b847cSJacob Keller * and the remaining 32 bits are the lower 32 bits of the PHC timer. 371*ea9b847cSJacob Keller * 372*ea9b847cSJacob Keller * It is assumed that the caller verifies the timestamp is valid prior to 373*ea9b847cSJacob Keller * calling this function. 374*ea9b847cSJacob Keller * 375*ea9b847cSJacob Keller * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC 376*ea9b847cSJacob Keller * time stored in the device private PTP structure as the basis for timestamp 377*ea9b847cSJacob Keller * extension. 378*ea9b847cSJacob Keller * 379*ea9b847cSJacob Keller * See ice_ptp_extend_32b_ts for a detailed explanation of the extension 380*ea9b847cSJacob Keller * algorithm. 381*ea9b847cSJacob Keller */ 382*ea9b847cSJacob Keller static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp) 383*ea9b847cSJacob Keller { 384*ea9b847cSJacob Keller const u64 mask = GENMASK_ULL(31, 0); 385*ea9b847cSJacob Keller 386*ea9b847cSJacob Keller return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time, 387*ea9b847cSJacob Keller (in_tstamp >> 8) & mask); 388*ea9b847cSJacob Keller } 389*ea9b847cSJacob Keller 390*ea9b847cSJacob Keller /** 39106c16d89SJacob Keller * ice_ptp_read_time - Read the time from the device 39206c16d89SJacob Keller * @pf: Board private structure 39306c16d89SJacob Keller * @ts: timespec structure to hold the current time value 39406c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 39506c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 39606c16d89SJacob Keller * 39706c16d89SJacob Keller * This function reads the source clock registers and stores them in a timespec. 39806c16d89SJacob Keller * However, since the registers are 64 bits of nanoseconds, we must convert the 39906c16d89SJacob Keller * result to a timespec before we can return. 40006c16d89SJacob Keller */ 40106c16d89SJacob Keller static void 40206c16d89SJacob Keller ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts, 40306c16d89SJacob Keller struct ptp_system_timestamp *sts) 40406c16d89SJacob Keller { 40506c16d89SJacob Keller u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts); 40606c16d89SJacob Keller 40706c16d89SJacob Keller *ts = ns_to_timespec64(time_ns); 40806c16d89SJacob Keller } 40906c16d89SJacob Keller 41006c16d89SJacob Keller /** 41106c16d89SJacob Keller * ice_ptp_write_init - Set PHC time to provided value 41206c16d89SJacob Keller * @pf: Board private structure 41306c16d89SJacob Keller * @ts: timespec structure that holds the new time value 41406c16d89SJacob Keller * 41506c16d89SJacob Keller * Set the PHC time to the specified time provided in the timespec. 41606c16d89SJacob Keller */ 41706c16d89SJacob Keller static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts) 41806c16d89SJacob Keller { 41906c16d89SJacob Keller u64 ns = timespec64_to_ns(ts); 42006c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 42106c16d89SJacob Keller 42206c16d89SJacob Keller return ice_ptp_init_time(hw, ns); 42306c16d89SJacob Keller } 42406c16d89SJacob Keller 42506c16d89SJacob Keller /** 42606c16d89SJacob Keller * ice_ptp_write_adj - Adjust PHC clock time atomically 42706c16d89SJacob Keller * @pf: Board private structure 42806c16d89SJacob Keller * @adj: Adjustment in nanoseconds 42906c16d89SJacob Keller * 43006c16d89SJacob Keller * Perform an atomic adjustment of the PHC time by the specified number of 43106c16d89SJacob Keller * nanoseconds. 43206c16d89SJacob Keller */ 43306c16d89SJacob Keller static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj) 43406c16d89SJacob Keller { 43506c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 43606c16d89SJacob Keller 43706c16d89SJacob Keller return ice_ptp_adj_clock(hw, adj); 43806c16d89SJacob Keller } 43906c16d89SJacob Keller 44006c16d89SJacob Keller /** 44106c16d89SJacob Keller * ice_ptp_adjfine - Adjust clock increment rate 44206c16d89SJacob Keller * @info: the driver's PTP info structure 44306c16d89SJacob Keller * @scaled_ppm: Parts per million with 16-bit fractional field 44406c16d89SJacob Keller * 44506c16d89SJacob Keller * Adjust the frequency of the clock by the indicated scaled ppm from the 44606c16d89SJacob Keller * base frequency. 44706c16d89SJacob Keller */ 44806c16d89SJacob Keller static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm) 44906c16d89SJacob Keller { 45006c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 45106c16d89SJacob Keller u64 freq, divisor = 1000000ULL; 45206c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 45306c16d89SJacob Keller s64 incval, diff; 45406c16d89SJacob Keller int neg_adj = 0; 45506c16d89SJacob Keller int err; 45606c16d89SJacob Keller 45706c16d89SJacob Keller incval = ICE_PTP_NOMINAL_INCVAL_E810; 45806c16d89SJacob Keller 45906c16d89SJacob Keller if (scaled_ppm < 0) { 46006c16d89SJacob Keller neg_adj = 1; 46106c16d89SJacob Keller scaled_ppm = -scaled_ppm; 46206c16d89SJacob Keller } 46306c16d89SJacob Keller 46406c16d89SJacob Keller while ((u64)scaled_ppm > div_u64(U64_MAX, incval)) { 46506c16d89SJacob Keller /* handle overflow by scaling down the scaled_ppm and 46606c16d89SJacob Keller * the divisor, losing some precision 46706c16d89SJacob Keller */ 46806c16d89SJacob Keller scaled_ppm >>= 2; 46906c16d89SJacob Keller divisor >>= 2; 47006c16d89SJacob Keller } 47106c16d89SJacob Keller 47206c16d89SJacob Keller freq = (incval * (u64)scaled_ppm) >> 16; 47306c16d89SJacob Keller diff = div_u64(freq, divisor); 47406c16d89SJacob Keller 47506c16d89SJacob Keller if (neg_adj) 47606c16d89SJacob Keller incval -= diff; 47706c16d89SJacob Keller else 47806c16d89SJacob Keller incval += diff; 47906c16d89SJacob Keller 48006c16d89SJacob Keller err = ice_ptp_write_incval_locked(hw, incval); 48106c16d89SJacob Keller if (err) { 48206c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n", 48306c16d89SJacob Keller err); 48406c16d89SJacob Keller return -EIO; 48506c16d89SJacob Keller } 48606c16d89SJacob Keller 48706c16d89SJacob Keller return 0; 48806c16d89SJacob Keller } 48906c16d89SJacob Keller 49006c16d89SJacob Keller /** 49106c16d89SJacob Keller * ice_ptp_gettimex64 - Get the time of the clock 49206c16d89SJacob Keller * @info: the driver's PTP info structure 49306c16d89SJacob Keller * @ts: timespec64 structure to hold the current time value 49406c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 49506c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 49606c16d89SJacob Keller * 49706c16d89SJacob Keller * Read the device clock and return the correct value on ns, after converting it 49806c16d89SJacob Keller * into a timespec struct. 49906c16d89SJacob Keller */ 50006c16d89SJacob Keller static int 50106c16d89SJacob Keller ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts, 50206c16d89SJacob Keller struct ptp_system_timestamp *sts) 50306c16d89SJacob Keller { 50406c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 50506c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 50606c16d89SJacob Keller 50706c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 50806c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n"); 50906c16d89SJacob Keller return -EBUSY; 51006c16d89SJacob Keller } 51106c16d89SJacob Keller 51206c16d89SJacob Keller ice_ptp_read_time(pf, ts, sts); 51306c16d89SJacob Keller ice_ptp_unlock(hw); 51406c16d89SJacob Keller 51506c16d89SJacob Keller return 0; 51606c16d89SJacob Keller } 51706c16d89SJacob Keller 51806c16d89SJacob Keller /** 51906c16d89SJacob Keller * ice_ptp_settime64 - Set the time of the clock 52006c16d89SJacob Keller * @info: the driver's PTP info structure 52106c16d89SJacob Keller * @ts: timespec64 structure that holds the new time value 52206c16d89SJacob Keller * 52306c16d89SJacob Keller * Set the device clock to the user input value. The conversion from timespec 52406c16d89SJacob Keller * to ns happens in the write function. 52506c16d89SJacob Keller */ 52606c16d89SJacob Keller static int 52706c16d89SJacob Keller ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts) 52806c16d89SJacob Keller { 52906c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 53006c16d89SJacob Keller struct timespec64 ts64 = *ts; 53106c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 53206c16d89SJacob Keller int err; 53306c16d89SJacob Keller 53406c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 53506c16d89SJacob Keller err = -EBUSY; 53606c16d89SJacob Keller goto exit; 53706c16d89SJacob Keller } 53806c16d89SJacob Keller 53906c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts64); 54006c16d89SJacob Keller ice_ptp_unlock(hw); 54106c16d89SJacob Keller 54277a78115SJacob Keller if (!err) 54377a78115SJacob Keller ice_ptp_update_cached_phctime(pf); 54477a78115SJacob Keller 54506c16d89SJacob Keller exit: 54606c16d89SJacob Keller if (err) { 54706c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err); 54806c16d89SJacob Keller return err; 54906c16d89SJacob Keller } 55006c16d89SJacob Keller 55106c16d89SJacob Keller return 0; 55206c16d89SJacob Keller } 55306c16d89SJacob Keller 55406c16d89SJacob Keller /** 55506c16d89SJacob Keller * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment 55606c16d89SJacob Keller * @info: the driver's PTP info structure 55706c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 55806c16d89SJacob Keller */ 55906c16d89SJacob Keller static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta) 56006c16d89SJacob Keller { 56106c16d89SJacob Keller struct timespec64 now, then; 56206c16d89SJacob Keller 56306c16d89SJacob Keller then = ns_to_timespec64(delta); 56406c16d89SJacob Keller ice_ptp_gettimex64(info, &now, NULL); 56506c16d89SJacob Keller now = timespec64_add(now, then); 56606c16d89SJacob Keller 56706c16d89SJacob Keller return ice_ptp_settime64(info, (const struct timespec64 *)&now); 56806c16d89SJacob Keller } 56906c16d89SJacob Keller 57006c16d89SJacob Keller /** 57106c16d89SJacob Keller * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta 57206c16d89SJacob Keller * @info: the driver's PTP info structure 57306c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 57406c16d89SJacob Keller */ 57506c16d89SJacob Keller static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta) 57606c16d89SJacob Keller { 57706c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 57806c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 57906c16d89SJacob Keller struct device *dev; 58006c16d89SJacob Keller int err; 58106c16d89SJacob Keller 58206c16d89SJacob Keller dev = ice_pf_to_dev(pf); 58306c16d89SJacob Keller 58406c16d89SJacob Keller /* Hardware only supports atomic adjustments using signed 32-bit 58506c16d89SJacob Keller * integers. For any adjustment outside this range, perform 58606c16d89SJacob Keller * a non-atomic get->adjust->set flow. 58706c16d89SJacob Keller */ 58806c16d89SJacob Keller if (delta > S32_MAX || delta < S32_MIN) { 58906c16d89SJacob Keller dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta); 59006c16d89SJacob Keller return ice_ptp_adjtime_nonatomic(info, delta); 59106c16d89SJacob Keller } 59206c16d89SJacob Keller 59306c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 59406c16d89SJacob Keller dev_err(dev, "PTP failed to acquire semaphore in adjtime\n"); 59506c16d89SJacob Keller return -EBUSY; 59606c16d89SJacob Keller } 59706c16d89SJacob Keller 59806c16d89SJacob Keller err = ice_ptp_write_adj(pf, delta); 59906c16d89SJacob Keller 60006c16d89SJacob Keller ice_ptp_unlock(hw); 60106c16d89SJacob Keller 60206c16d89SJacob Keller if (err) { 60306c16d89SJacob Keller dev_err(dev, "PTP failed to adjust time, err %d\n", err); 60406c16d89SJacob Keller return err; 60506c16d89SJacob Keller } 60606c16d89SJacob Keller 60777a78115SJacob Keller ice_ptp_update_cached_phctime(pf); 60877a78115SJacob Keller 60906c16d89SJacob Keller return 0; 61006c16d89SJacob Keller } 61106c16d89SJacob Keller 61206c16d89SJacob Keller /** 61377a78115SJacob Keller * ice_ptp_get_ts_config - ioctl interface to read the timestamping config 61477a78115SJacob Keller * @pf: Board private structure 61577a78115SJacob Keller * @ifr: ioctl data 61677a78115SJacob Keller * 61777a78115SJacob Keller * Copy the timestamping config to user buffer 61877a78115SJacob Keller */ 61977a78115SJacob Keller int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr) 62077a78115SJacob Keller { 62177a78115SJacob Keller struct hwtstamp_config *config; 62277a78115SJacob Keller 62377a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 62477a78115SJacob Keller return -EIO; 62577a78115SJacob Keller 62677a78115SJacob Keller config = &pf->ptp.tstamp_config; 62777a78115SJacob Keller 62877a78115SJacob Keller return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ? 62977a78115SJacob Keller -EFAULT : 0; 63077a78115SJacob Keller } 63177a78115SJacob Keller 63277a78115SJacob Keller /** 63377a78115SJacob Keller * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode 63477a78115SJacob Keller * @pf: Board private structure 63577a78115SJacob Keller * @config: hwtstamp settings requested or saved 63677a78115SJacob Keller */ 63777a78115SJacob Keller static int 63877a78115SJacob Keller ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config) 63977a78115SJacob Keller { 64077a78115SJacob Keller /* Reserved for future extensions. */ 64177a78115SJacob Keller if (config->flags) 64277a78115SJacob Keller return -EINVAL; 64377a78115SJacob Keller 64477a78115SJacob Keller switch (config->tx_type) { 64577a78115SJacob Keller case HWTSTAMP_TX_OFF: 646*ea9b847cSJacob Keller ice_set_tx_tstamp(pf, false); 647*ea9b847cSJacob Keller break; 648*ea9b847cSJacob Keller case HWTSTAMP_TX_ON: 649*ea9b847cSJacob Keller ice_set_tx_tstamp(pf, true); 65077a78115SJacob Keller break; 65177a78115SJacob Keller default: 65277a78115SJacob Keller return -ERANGE; 65377a78115SJacob Keller } 65477a78115SJacob Keller 65577a78115SJacob Keller switch (config->rx_filter) { 65677a78115SJacob Keller case HWTSTAMP_FILTER_NONE: 65777a78115SJacob Keller ice_set_rx_tstamp(pf, false); 65877a78115SJacob Keller break; 65977a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 66077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 66177a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 66277a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_EVENT: 66377a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 66477a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 66577a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_SYNC: 66677a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 66777a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 66877a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 66977a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 67077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 67177a78115SJacob Keller case HWTSTAMP_FILTER_NTP_ALL: 67277a78115SJacob Keller case HWTSTAMP_FILTER_ALL: 67377a78115SJacob Keller config->rx_filter = HWTSTAMP_FILTER_ALL; 67477a78115SJacob Keller ice_set_rx_tstamp(pf, true); 67577a78115SJacob Keller break; 67677a78115SJacob Keller default: 67777a78115SJacob Keller return -ERANGE; 67877a78115SJacob Keller } 67977a78115SJacob Keller 68077a78115SJacob Keller return 0; 68177a78115SJacob Keller } 68277a78115SJacob Keller 68377a78115SJacob Keller /** 68477a78115SJacob Keller * ice_ptp_set_ts_config - ioctl interface to control the timestamping 68577a78115SJacob Keller * @pf: Board private structure 68677a78115SJacob Keller * @ifr: ioctl data 68777a78115SJacob Keller * 68877a78115SJacob Keller * Get the user config and store it 68977a78115SJacob Keller */ 69077a78115SJacob Keller int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr) 69177a78115SJacob Keller { 69277a78115SJacob Keller struct hwtstamp_config config; 69377a78115SJacob Keller int err; 69477a78115SJacob Keller 69577a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 69677a78115SJacob Keller return -EAGAIN; 69777a78115SJacob Keller 69877a78115SJacob Keller if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 69977a78115SJacob Keller return -EFAULT; 70077a78115SJacob Keller 70177a78115SJacob Keller err = ice_ptp_set_timestamp_mode(pf, &config); 70277a78115SJacob Keller if (err) 70377a78115SJacob Keller return err; 70477a78115SJacob Keller 70577a78115SJacob Keller /* Save these settings for future reference */ 70677a78115SJacob Keller pf->ptp.tstamp_config = config; 70777a78115SJacob Keller 70877a78115SJacob Keller return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 70977a78115SJacob Keller -EFAULT : 0; 71077a78115SJacob Keller } 71177a78115SJacob Keller 71277a78115SJacob Keller /** 71377a78115SJacob Keller * ice_ptp_rx_hwtstamp - Check for an Rx timestamp 71477a78115SJacob Keller * @rx_ring: Ring to get the VSI info 71577a78115SJacob Keller * @rx_desc: Receive descriptor 71677a78115SJacob Keller * @skb: Particular skb to send timestamp with 71777a78115SJacob Keller * 71877a78115SJacob Keller * The driver receives a notification in the receive descriptor with timestamp. 71977a78115SJacob Keller * The timestamp is in ns, so we must convert the result first. 72077a78115SJacob Keller */ 72177a78115SJacob Keller void 72277a78115SJacob Keller ice_ptp_rx_hwtstamp(struct ice_ring *rx_ring, 72377a78115SJacob Keller union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) 72477a78115SJacob Keller { 72577a78115SJacob Keller u32 ts_high; 72677a78115SJacob Keller u64 ts_ns; 72777a78115SJacob Keller 72877a78115SJacob Keller /* Populate timesync data into skb */ 72977a78115SJacob Keller if (rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID) { 73077a78115SJacob Keller struct skb_shared_hwtstamps *hwtstamps; 73177a78115SJacob Keller 73277a78115SJacob Keller /* Use ice_ptp_extend_32b_ts directly, using the ring-specific 73377a78115SJacob Keller * cached PHC value, rather than accessing the PF. This also 73477a78115SJacob Keller * allows us to simply pass the upper 32bits of nanoseconds 73577a78115SJacob Keller * directly. Calling ice_ptp_extend_40b_ts is unnecessary as 73677a78115SJacob Keller * it would just discard these bits itself. 73777a78115SJacob Keller */ 73877a78115SJacob Keller ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high); 73977a78115SJacob Keller ts_ns = ice_ptp_extend_32b_ts(rx_ring->cached_phctime, ts_high); 74077a78115SJacob Keller 74177a78115SJacob Keller hwtstamps = skb_hwtstamps(skb); 74277a78115SJacob Keller memset(hwtstamps, 0, sizeof(*hwtstamps)); 74377a78115SJacob Keller hwtstamps->hwtstamp = ns_to_ktime(ts_ns); 74477a78115SJacob Keller } 74577a78115SJacob Keller } 74677a78115SJacob Keller 74777a78115SJacob Keller /** 74806c16d89SJacob Keller * ice_ptp_set_caps - Set PTP capabilities 74906c16d89SJacob Keller * @pf: Board private structure 75006c16d89SJacob Keller */ 75106c16d89SJacob Keller static void ice_ptp_set_caps(struct ice_pf *pf) 75206c16d89SJacob Keller { 75306c16d89SJacob Keller struct ptp_clock_info *info = &pf->ptp.info; 75406c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf); 75506c16d89SJacob Keller 75606c16d89SJacob Keller snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk", 75706c16d89SJacob Keller dev_driver_string(dev), dev_name(dev)); 75806c16d89SJacob Keller info->owner = THIS_MODULE; 75906c16d89SJacob Keller info->max_adj = 999999999; 76006c16d89SJacob Keller info->adjtime = ice_ptp_adjtime; 76106c16d89SJacob Keller info->adjfine = ice_ptp_adjfine; 76206c16d89SJacob Keller info->gettimex64 = ice_ptp_gettimex64; 76306c16d89SJacob Keller info->settime64 = ice_ptp_settime64; 76406c16d89SJacob Keller } 76506c16d89SJacob Keller 76606c16d89SJacob Keller /** 76706c16d89SJacob Keller * ice_ptp_create_clock - Create PTP clock device for userspace 76806c16d89SJacob Keller * @pf: Board private structure 76906c16d89SJacob Keller * 77006c16d89SJacob Keller * This function creates a new PTP clock device. It only creates one if we 77106c16d89SJacob Keller * don't already have one. Will return error if it can't create one, but success 77206c16d89SJacob Keller * if we already have a device. Should be used by ice_ptp_init to create clock 77306c16d89SJacob Keller * initially, and prevent global resets from creating new clock devices. 77406c16d89SJacob Keller */ 77506c16d89SJacob Keller static long ice_ptp_create_clock(struct ice_pf *pf) 77606c16d89SJacob Keller { 77706c16d89SJacob Keller struct ptp_clock_info *info; 77806c16d89SJacob Keller struct ptp_clock *clock; 77906c16d89SJacob Keller struct device *dev; 78006c16d89SJacob Keller 78106c16d89SJacob Keller /* No need to create a clock device if we already have one */ 78206c16d89SJacob Keller if (pf->ptp.clock) 78306c16d89SJacob Keller return 0; 78406c16d89SJacob Keller 78506c16d89SJacob Keller ice_ptp_set_caps(pf); 78606c16d89SJacob Keller 78706c16d89SJacob Keller info = &pf->ptp.info; 78806c16d89SJacob Keller dev = ice_pf_to_dev(pf); 78906c16d89SJacob Keller 79006c16d89SJacob Keller /* Attempt to register the clock before enabling the hardware. */ 79106c16d89SJacob Keller clock = ptp_clock_register(info, dev); 79206c16d89SJacob Keller if (IS_ERR(clock)) 79306c16d89SJacob Keller return PTR_ERR(clock); 79406c16d89SJacob Keller 79506c16d89SJacob Keller pf->ptp.clock = clock; 79606c16d89SJacob Keller 79706c16d89SJacob Keller return 0; 79806c16d89SJacob Keller } 79906c16d89SJacob Keller 800*ea9b847cSJacob Keller /** 801*ea9b847cSJacob Keller * ice_ptp_tx_tstamp_work - Process Tx timestamps for a port 802*ea9b847cSJacob Keller * @work: pointer to the kthread_work struct 803*ea9b847cSJacob Keller * 804*ea9b847cSJacob Keller * Process timestamps captured by the PHY associated with this port. To do 805*ea9b847cSJacob Keller * this, loop over each index with a waiting skb. 806*ea9b847cSJacob Keller * 807*ea9b847cSJacob Keller * If a given index has a valid timestamp, perform the following steps: 808*ea9b847cSJacob Keller * 809*ea9b847cSJacob Keller * 1) copy the timestamp out of the PHY register 810*ea9b847cSJacob Keller * 4) clear the timestamp valid bit in the PHY register 811*ea9b847cSJacob Keller * 5) unlock the index by clearing the associated in_use bit. 812*ea9b847cSJacob Keller * 2) extend the 40b timestamp value to get a 64bit timestamp 813*ea9b847cSJacob Keller * 3) send that timestamp to the stack 814*ea9b847cSJacob Keller * 815*ea9b847cSJacob Keller * After looping, if we still have waiting SKBs, then re-queue the work. This 816*ea9b847cSJacob Keller * may cause us effectively poll even when not strictly necessary. We do this 817*ea9b847cSJacob Keller * because it's possible a new timestamp was requested around the same time as 818*ea9b847cSJacob Keller * the interrupt. In some cases hardware might not interrupt us again when the 819*ea9b847cSJacob Keller * timestamp is captured. 820*ea9b847cSJacob Keller * 821*ea9b847cSJacob Keller * Note that we only take the tracking lock when clearing the bit and when 822*ea9b847cSJacob Keller * checking if we need to re-queue this task. The only place where bits can be 823*ea9b847cSJacob Keller * set is the hard xmit routine where an SKB has a request flag set. The only 824*ea9b847cSJacob Keller * places where we clear bits are this work function, or the periodic cleanup 825*ea9b847cSJacob Keller * thread. If the cleanup thread clears a bit we're processing we catch it 826*ea9b847cSJacob Keller * when we lock to clear the bit and then grab the SKB pointer. If a Tx thread 827*ea9b847cSJacob Keller * starts a new timestamp, we might not begin processing it right away but we 828*ea9b847cSJacob Keller * will notice it at the end when we re-queue the work item. If a Tx thread 829*ea9b847cSJacob Keller * starts a new timestamp just after this function exits without re-queuing, 830*ea9b847cSJacob Keller * the interrupt when the timestamp finishes should trigger. Avoiding holding 831*ea9b847cSJacob Keller * the lock for the entire function is important in order to ensure that Tx 832*ea9b847cSJacob Keller * threads do not get blocked while waiting for the lock. 833*ea9b847cSJacob Keller */ 834*ea9b847cSJacob Keller static void ice_ptp_tx_tstamp_work(struct kthread_work *work) 835*ea9b847cSJacob Keller { 836*ea9b847cSJacob Keller struct ice_ptp_port *ptp_port; 837*ea9b847cSJacob Keller struct ice_ptp_tx *tx; 838*ea9b847cSJacob Keller struct ice_pf *pf; 839*ea9b847cSJacob Keller struct ice_hw *hw; 840*ea9b847cSJacob Keller u8 idx; 841*ea9b847cSJacob Keller 842*ea9b847cSJacob Keller tx = container_of(work, struct ice_ptp_tx, work); 843*ea9b847cSJacob Keller if (!tx->init) 844*ea9b847cSJacob Keller return; 845*ea9b847cSJacob Keller 846*ea9b847cSJacob Keller ptp_port = container_of(tx, struct ice_ptp_port, tx); 847*ea9b847cSJacob Keller pf = ptp_port_to_pf(ptp_port); 848*ea9b847cSJacob Keller hw = &pf->hw; 849*ea9b847cSJacob Keller 850*ea9b847cSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 851*ea9b847cSJacob Keller struct skb_shared_hwtstamps shhwtstamps = {}; 852*ea9b847cSJacob Keller u8 phy_idx = idx + tx->quad_offset; 853*ea9b847cSJacob Keller u64 raw_tstamp, tstamp; 854*ea9b847cSJacob Keller struct sk_buff *skb; 855*ea9b847cSJacob Keller int err; 856*ea9b847cSJacob Keller 857*ea9b847cSJacob Keller err = ice_read_phy_tstamp(hw, tx->quad, phy_idx, 858*ea9b847cSJacob Keller &raw_tstamp); 859*ea9b847cSJacob Keller if (err) 860*ea9b847cSJacob Keller continue; 861*ea9b847cSJacob Keller 862*ea9b847cSJacob Keller /* Check if the timestamp is valid */ 863*ea9b847cSJacob Keller if (!(raw_tstamp & ICE_PTP_TS_VALID)) 864*ea9b847cSJacob Keller continue; 865*ea9b847cSJacob Keller 866*ea9b847cSJacob Keller /* clear the timestamp register, so that it won't show valid 867*ea9b847cSJacob Keller * again when re-used. 868*ea9b847cSJacob Keller */ 869*ea9b847cSJacob Keller ice_clear_phy_tstamp(hw, tx->quad, phy_idx); 870*ea9b847cSJacob Keller 871*ea9b847cSJacob Keller /* The timestamp is valid, so we'll go ahead and clear this 872*ea9b847cSJacob Keller * index and then send the timestamp up to the stack. 873*ea9b847cSJacob Keller */ 874*ea9b847cSJacob Keller spin_lock(&tx->lock); 875*ea9b847cSJacob Keller clear_bit(idx, tx->in_use); 876*ea9b847cSJacob Keller skb = tx->tstamps[idx].skb; 877*ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 878*ea9b847cSJacob Keller spin_unlock(&tx->lock); 879*ea9b847cSJacob Keller 880*ea9b847cSJacob Keller /* it's (unlikely but) possible we raced with the cleanup 881*ea9b847cSJacob Keller * thread for discarding old timestamp requests. 882*ea9b847cSJacob Keller */ 883*ea9b847cSJacob Keller if (!skb) 884*ea9b847cSJacob Keller continue; 885*ea9b847cSJacob Keller 886*ea9b847cSJacob Keller /* Extend the timestamp using cached PHC time */ 887*ea9b847cSJacob Keller tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp); 888*ea9b847cSJacob Keller shhwtstamps.hwtstamp = ns_to_ktime(tstamp); 889*ea9b847cSJacob Keller 890*ea9b847cSJacob Keller skb_tstamp_tx(skb, &shhwtstamps); 891*ea9b847cSJacob Keller dev_kfree_skb_any(skb); 892*ea9b847cSJacob Keller } 893*ea9b847cSJacob Keller 894*ea9b847cSJacob Keller /* Check if we still have work to do. If so, re-queue this task to 895*ea9b847cSJacob Keller * poll for remaining timestamps. 896*ea9b847cSJacob Keller */ 897*ea9b847cSJacob Keller spin_lock(&tx->lock); 898*ea9b847cSJacob Keller if (!bitmap_empty(tx->in_use, tx->len)) 899*ea9b847cSJacob Keller kthread_queue_work(pf->ptp.kworker, &tx->work); 900*ea9b847cSJacob Keller spin_unlock(&tx->lock); 901*ea9b847cSJacob Keller } 902*ea9b847cSJacob Keller 903*ea9b847cSJacob Keller /** 904*ea9b847cSJacob Keller * ice_ptp_request_ts - Request an available Tx timestamp index 905*ea9b847cSJacob Keller * @tx: the PTP Tx timestamp tracker to request from 906*ea9b847cSJacob Keller * @skb: the SKB to associate with this timestamp request 907*ea9b847cSJacob Keller */ 908*ea9b847cSJacob Keller s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb) 909*ea9b847cSJacob Keller { 910*ea9b847cSJacob Keller u8 idx; 911*ea9b847cSJacob Keller 912*ea9b847cSJacob Keller /* Check if this tracker is initialized */ 913*ea9b847cSJacob Keller if (!tx->init) 914*ea9b847cSJacob Keller return -1; 915*ea9b847cSJacob Keller 916*ea9b847cSJacob Keller spin_lock(&tx->lock); 917*ea9b847cSJacob Keller /* Find and set the first available index */ 918*ea9b847cSJacob Keller idx = find_first_zero_bit(tx->in_use, tx->len); 919*ea9b847cSJacob Keller if (idx < tx->len) { 920*ea9b847cSJacob Keller /* We got a valid index that no other thread could have set. Store 921*ea9b847cSJacob Keller * a reference to the skb and the start time to allow discarding old 922*ea9b847cSJacob Keller * requests. 923*ea9b847cSJacob Keller */ 924*ea9b847cSJacob Keller set_bit(idx, tx->in_use); 925*ea9b847cSJacob Keller tx->tstamps[idx].start = jiffies; 926*ea9b847cSJacob Keller tx->tstamps[idx].skb = skb_get(skb); 927*ea9b847cSJacob Keller skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 928*ea9b847cSJacob Keller } 929*ea9b847cSJacob Keller 930*ea9b847cSJacob Keller spin_unlock(&tx->lock); 931*ea9b847cSJacob Keller 932*ea9b847cSJacob Keller /* return the appropriate PHY timestamp register index, -1 if no 933*ea9b847cSJacob Keller * indexes were available. 934*ea9b847cSJacob Keller */ 935*ea9b847cSJacob Keller if (idx >= tx->len) 936*ea9b847cSJacob Keller return -1; 937*ea9b847cSJacob Keller else 938*ea9b847cSJacob Keller return idx + tx->quad_offset; 939*ea9b847cSJacob Keller } 940*ea9b847cSJacob Keller 941*ea9b847cSJacob Keller /** 942*ea9b847cSJacob Keller * ice_ptp_process_ts - Spawn kthread work to handle timestamps 943*ea9b847cSJacob Keller * @pf: Board private structure 944*ea9b847cSJacob Keller * 945*ea9b847cSJacob Keller * Queue work required to process the PTP Tx timestamps outside of interrupt 946*ea9b847cSJacob Keller * context. 947*ea9b847cSJacob Keller */ 948*ea9b847cSJacob Keller void ice_ptp_process_ts(struct ice_pf *pf) 949*ea9b847cSJacob Keller { 950*ea9b847cSJacob Keller if (pf->ptp.port.tx.init) 951*ea9b847cSJacob Keller kthread_queue_work(pf->ptp.kworker, &pf->ptp.port.tx.work); 952*ea9b847cSJacob Keller } 953*ea9b847cSJacob Keller 954*ea9b847cSJacob Keller /** 955*ea9b847cSJacob Keller * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps 956*ea9b847cSJacob Keller * @tx: Tx tracking structure to initialize 957*ea9b847cSJacob Keller * 958*ea9b847cSJacob Keller * Assumes that the length has already been initialized. Do not call directly, 959*ea9b847cSJacob Keller * use the ice_ptp_init_tx_e822 or ice_ptp_init_tx_e810 instead. 960*ea9b847cSJacob Keller */ 961*ea9b847cSJacob Keller static int 962*ea9b847cSJacob Keller ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx) 963*ea9b847cSJacob Keller { 964*ea9b847cSJacob Keller tx->tstamps = kcalloc(tx->len, sizeof(*tx->tstamps), GFP_KERNEL); 965*ea9b847cSJacob Keller if (!tx->tstamps) 966*ea9b847cSJacob Keller return -ENOMEM; 967*ea9b847cSJacob Keller 968*ea9b847cSJacob Keller tx->in_use = bitmap_zalloc(tx->len, GFP_KERNEL); 969*ea9b847cSJacob Keller if (!tx->in_use) { 970*ea9b847cSJacob Keller kfree(tx->tstamps); 971*ea9b847cSJacob Keller tx->tstamps = NULL; 972*ea9b847cSJacob Keller return -ENOMEM; 973*ea9b847cSJacob Keller } 974*ea9b847cSJacob Keller 975*ea9b847cSJacob Keller spin_lock_init(&tx->lock); 976*ea9b847cSJacob Keller kthread_init_work(&tx->work, ice_ptp_tx_tstamp_work); 977*ea9b847cSJacob Keller 978*ea9b847cSJacob Keller tx->init = 1; 979*ea9b847cSJacob Keller 980*ea9b847cSJacob Keller return 0; 981*ea9b847cSJacob Keller } 982*ea9b847cSJacob Keller 983*ea9b847cSJacob Keller /** 984*ea9b847cSJacob Keller * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker 985*ea9b847cSJacob Keller * @pf: Board private structure 986*ea9b847cSJacob Keller * @tx: the tracker to flush 987*ea9b847cSJacob Keller */ 988*ea9b847cSJacob Keller static void 989*ea9b847cSJacob Keller ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 990*ea9b847cSJacob Keller { 991*ea9b847cSJacob Keller u8 idx; 992*ea9b847cSJacob Keller 993*ea9b847cSJacob Keller for (idx = 0; idx < tx->len; idx++) { 994*ea9b847cSJacob Keller u8 phy_idx = idx + tx->quad_offset; 995*ea9b847cSJacob Keller 996*ea9b847cSJacob Keller /* Clear any potential residual timestamp in the PHY block */ 997*ea9b847cSJacob Keller if (!pf->hw.reset_ongoing) 998*ea9b847cSJacob Keller ice_clear_phy_tstamp(&pf->hw, tx->quad, phy_idx); 999*ea9b847cSJacob Keller 1000*ea9b847cSJacob Keller if (tx->tstamps[idx].skb) { 1001*ea9b847cSJacob Keller dev_kfree_skb_any(tx->tstamps[idx].skb); 1002*ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 1003*ea9b847cSJacob Keller } 1004*ea9b847cSJacob Keller } 1005*ea9b847cSJacob Keller } 1006*ea9b847cSJacob Keller 1007*ea9b847cSJacob Keller /** 1008*ea9b847cSJacob Keller * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker 1009*ea9b847cSJacob Keller * @pf: Board private structure 1010*ea9b847cSJacob Keller * @tx: Tx tracking structure to release 1011*ea9b847cSJacob Keller * 1012*ea9b847cSJacob Keller * Free memory associated with the Tx timestamp tracker. 1013*ea9b847cSJacob Keller */ 1014*ea9b847cSJacob Keller static void 1015*ea9b847cSJacob Keller ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 1016*ea9b847cSJacob Keller { 1017*ea9b847cSJacob Keller tx->init = 0; 1018*ea9b847cSJacob Keller 1019*ea9b847cSJacob Keller kthread_cancel_work_sync(&tx->work); 1020*ea9b847cSJacob Keller 1021*ea9b847cSJacob Keller ice_ptp_flush_tx_tracker(pf, tx); 1022*ea9b847cSJacob Keller 1023*ea9b847cSJacob Keller kfree(tx->tstamps); 1024*ea9b847cSJacob Keller tx->tstamps = NULL; 1025*ea9b847cSJacob Keller 1026*ea9b847cSJacob Keller kfree(tx->in_use); 1027*ea9b847cSJacob Keller tx->in_use = NULL; 1028*ea9b847cSJacob Keller 1029*ea9b847cSJacob Keller tx->len = 0; 1030*ea9b847cSJacob Keller } 1031*ea9b847cSJacob Keller 1032*ea9b847cSJacob Keller /** 1033*ea9b847cSJacob Keller * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps 1034*ea9b847cSJacob Keller * @pf: Board private structure 1035*ea9b847cSJacob Keller * @tx: the Tx tracking structure to initialize 1036*ea9b847cSJacob Keller * 1037*ea9b847cSJacob Keller * Initialize the Tx timestamp tracker for this PF. For E810 devices, each 1038*ea9b847cSJacob Keller * port has its own block of timestamps, independent of the other ports. 1039*ea9b847cSJacob Keller */ 1040*ea9b847cSJacob Keller static int 1041*ea9b847cSJacob Keller ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx) 1042*ea9b847cSJacob Keller { 1043*ea9b847cSJacob Keller tx->quad = pf->hw.port_info->lport; 1044*ea9b847cSJacob Keller tx->quad_offset = 0; 1045*ea9b847cSJacob Keller tx->len = INDEX_PER_QUAD; 1046*ea9b847cSJacob Keller 1047*ea9b847cSJacob Keller return ice_ptp_alloc_tx_tracker(tx); 1048*ea9b847cSJacob Keller } 1049*ea9b847cSJacob Keller 1050*ea9b847cSJacob Keller /** 1051*ea9b847cSJacob Keller * ice_ptp_tx_tstamp_cleanup - Cleanup old timestamp requests that got dropped 1052*ea9b847cSJacob Keller * @tx: PTP Tx tracker to clean up 1053*ea9b847cSJacob Keller * 1054*ea9b847cSJacob Keller * Loop through the Tx timestamp requests and see if any of them have been 1055*ea9b847cSJacob Keller * waiting for a long time. Discard any SKBs that have been waiting for more 1056*ea9b847cSJacob Keller * than 2 seconds. This is long enough to be reasonably sure that the 1057*ea9b847cSJacob Keller * timestamp will never be captured. This might happen if the packet gets 1058*ea9b847cSJacob Keller * discarded before it reaches the PHY timestamping block. 1059*ea9b847cSJacob Keller */ 1060*ea9b847cSJacob Keller static void ice_ptp_tx_tstamp_cleanup(struct ice_ptp_tx *tx) 1061*ea9b847cSJacob Keller { 1062*ea9b847cSJacob Keller u8 idx; 1063*ea9b847cSJacob Keller 1064*ea9b847cSJacob Keller if (!tx->init) 1065*ea9b847cSJacob Keller return; 1066*ea9b847cSJacob Keller 1067*ea9b847cSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 1068*ea9b847cSJacob Keller struct sk_buff *skb; 1069*ea9b847cSJacob Keller 1070*ea9b847cSJacob Keller /* Check if this SKB has been waiting for too long */ 1071*ea9b847cSJacob Keller if (time_is_after_jiffies(tx->tstamps[idx].start + 2 * HZ)) 1072*ea9b847cSJacob Keller continue; 1073*ea9b847cSJacob Keller 1074*ea9b847cSJacob Keller spin_lock(&tx->lock); 1075*ea9b847cSJacob Keller skb = tx->tstamps[idx].skb; 1076*ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 1077*ea9b847cSJacob Keller clear_bit(idx, tx->in_use); 1078*ea9b847cSJacob Keller spin_unlock(&tx->lock); 1079*ea9b847cSJacob Keller 1080*ea9b847cSJacob Keller /* Free the SKB after we've cleared the bit */ 1081*ea9b847cSJacob Keller dev_kfree_skb_any(skb); 1082*ea9b847cSJacob Keller } 1083*ea9b847cSJacob Keller } 1084*ea9b847cSJacob Keller 108577a78115SJacob Keller static void ice_ptp_periodic_work(struct kthread_work *work) 108677a78115SJacob Keller { 108777a78115SJacob Keller struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work); 108877a78115SJacob Keller struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 108977a78115SJacob Keller 109077a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 109177a78115SJacob Keller return; 109277a78115SJacob Keller 109377a78115SJacob Keller ice_ptp_update_cached_phctime(pf); 109477a78115SJacob Keller 1095*ea9b847cSJacob Keller ice_ptp_tx_tstamp_cleanup(&pf->ptp.port.tx); 1096*ea9b847cSJacob Keller 109777a78115SJacob Keller /* Run twice a second */ 109877a78115SJacob Keller kthread_queue_delayed_work(ptp->kworker, &ptp->work, 109977a78115SJacob Keller msecs_to_jiffies(500)); 110077a78115SJacob Keller } 110177a78115SJacob Keller 110206c16d89SJacob Keller /** 110306c16d89SJacob Keller * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device 110406c16d89SJacob Keller * @pf: Board private structure 110506c16d89SJacob Keller * 110606c16d89SJacob Keller * Setup and initialize a PTP clock device that represents the device hardware 110706c16d89SJacob Keller * clock. Save the clock index for other functions connected to the same 110806c16d89SJacob Keller * hardware resource. 110906c16d89SJacob Keller */ 111006c16d89SJacob Keller static int ice_ptp_init_owner(struct ice_pf *pf) 111106c16d89SJacob Keller { 111206c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf); 111306c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 111406c16d89SJacob Keller struct timespec64 ts; 111506c16d89SJacob Keller u8 src_idx; 111606c16d89SJacob Keller int err; 111706c16d89SJacob Keller 111806c16d89SJacob Keller wr32(hw, GLTSYN_SYNC_DLAY, 0); 111906c16d89SJacob Keller 112006c16d89SJacob Keller /* Clear some HW residue and enable source clock */ 112106c16d89SJacob Keller src_idx = hw->func_caps.ts_func_info.tmr_index_owned; 112206c16d89SJacob Keller 112306c16d89SJacob Keller /* Enable source clocks */ 112406c16d89SJacob Keller wr32(hw, GLTSYN_ENA(src_idx), GLTSYN_ENA_TSYN_ENA_M); 112506c16d89SJacob Keller 112606c16d89SJacob Keller /* Enable PHY time sync */ 112706c16d89SJacob Keller err = ice_ptp_init_phy_e810(hw); 112806c16d89SJacob Keller if (err) 112906c16d89SJacob Keller goto err_exit; 113006c16d89SJacob Keller 113106c16d89SJacob Keller /* Clear event status indications for auxiliary pins */ 113206c16d89SJacob Keller (void)rd32(hw, GLTSYN_STAT(src_idx)); 113306c16d89SJacob Keller 113406c16d89SJacob Keller /* Acquire the global hardware lock */ 113506c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 113606c16d89SJacob Keller err = -EBUSY; 113706c16d89SJacob Keller goto err_exit; 113806c16d89SJacob Keller } 113906c16d89SJacob Keller 114006c16d89SJacob Keller /* Write the increment time value to PHY and LAN */ 114106c16d89SJacob Keller err = ice_ptp_write_incval(hw, ICE_PTP_NOMINAL_INCVAL_E810); 114206c16d89SJacob Keller if (err) { 114306c16d89SJacob Keller ice_ptp_unlock(hw); 114406c16d89SJacob Keller goto err_exit; 114506c16d89SJacob Keller } 114606c16d89SJacob Keller 114706c16d89SJacob Keller ts = ktime_to_timespec64(ktime_get_real()); 114806c16d89SJacob Keller /* Write the initial Time value to PHY and LAN */ 114906c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts); 115006c16d89SJacob Keller if (err) { 115106c16d89SJacob Keller ice_ptp_unlock(hw); 115206c16d89SJacob Keller goto err_exit; 115306c16d89SJacob Keller } 115406c16d89SJacob Keller 115506c16d89SJacob Keller /* Release the global hardware lock */ 115606c16d89SJacob Keller ice_ptp_unlock(hw); 115706c16d89SJacob Keller 115806c16d89SJacob Keller /* Ensure we have a clock device */ 115906c16d89SJacob Keller err = ice_ptp_create_clock(pf); 116006c16d89SJacob Keller if (err) 116106c16d89SJacob Keller goto err_clk; 116206c16d89SJacob Keller 116367569a7fSJacob Keller /* Store the PTP clock index for other PFs */ 116467569a7fSJacob Keller ice_set_ptp_clock_index(pf); 116567569a7fSJacob Keller 116606c16d89SJacob Keller return 0; 116706c16d89SJacob Keller 116806c16d89SJacob Keller err_clk: 116906c16d89SJacob Keller pf->ptp.clock = NULL; 117006c16d89SJacob Keller err_exit: 117106c16d89SJacob Keller dev_err(dev, "PTP failed to register clock, err %d\n", err); 117206c16d89SJacob Keller 117306c16d89SJacob Keller return err; 117406c16d89SJacob Keller } 117506c16d89SJacob Keller 117606c16d89SJacob Keller /** 117706c16d89SJacob Keller * ice_ptp_init - Initialize the PTP support after device probe or reset 117806c16d89SJacob Keller * @pf: Board private structure 117906c16d89SJacob Keller * 118006c16d89SJacob Keller * This function sets device up for PTP support. The first time it is run, it 118106c16d89SJacob Keller * will create a clock device. It does not create a clock device if one 118206c16d89SJacob Keller * already exists. It also reconfigures the device after a reset. 118306c16d89SJacob Keller */ 118406c16d89SJacob Keller void ice_ptp_init(struct ice_pf *pf) 118506c16d89SJacob Keller { 118606c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf); 118777a78115SJacob Keller struct kthread_worker *kworker; 118806c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 118906c16d89SJacob Keller int err; 119006c16d89SJacob Keller 119106c16d89SJacob Keller /* PTP is currently only supported on E810 devices */ 119206c16d89SJacob Keller if (!ice_is_e810(hw)) 119306c16d89SJacob Keller return; 119406c16d89SJacob Keller 119506c16d89SJacob Keller /* Check if this PF owns the source timer */ 119606c16d89SJacob Keller if (hw->func_caps.ts_func_info.src_tmr_owned) { 119706c16d89SJacob Keller err = ice_ptp_init_owner(pf); 119806c16d89SJacob Keller if (err) 119906c16d89SJacob Keller return; 120006c16d89SJacob Keller } 120106c16d89SJacob Keller 120277a78115SJacob Keller /* Disable timestamping for both Tx and Rx */ 120377a78115SJacob Keller ice_ptp_cfg_timestamp(pf, false); 120477a78115SJacob Keller 1205*ea9b847cSJacob Keller /* Initialize the PTP port Tx timestamp tracker */ 1206*ea9b847cSJacob Keller ice_ptp_init_tx_e810(pf, &pf->ptp.port.tx); 1207*ea9b847cSJacob Keller 120877a78115SJacob Keller /* Initialize work functions */ 120977a78115SJacob Keller kthread_init_delayed_work(&pf->ptp.work, ice_ptp_periodic_work); 121077a78115SJacob Keller 121177a78115SJacob Keller /* Allocate a kworker for handling work required for the ports 121277a78115SJacob Keller * connected to the PTP hardware clock. 121377a78115SJacob Keller */ 121477a78115SJacob Keller kworker = kthread_create_worker(0, "ice-ptp-%s", dev_name(dev)); 121577a78115SJacob Keller if (IS_ERR(kworker)) { 121677a78115SJacob Keller err = PTR_ERR(kworker); 121777a78115SJacob Keller goto err_kworker; 121877a78115SJacob Keller } 121977a78115SJacob Keller pf->ptp.kworker = kworker; 122077a78115SJacob Keller 122106c16d89SJacob Keller set_bit(ICE_FLAG_PTP, pf->flags); 122206c16d89SJacob Keller 122377a78115SJacob Keller /* Start periodic work going */ 122477a78115SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work, 0); 122577a78115SJacob Keller 122606c16d89SJacob Keller dev_info(dev, "PTP init successful\n"); 122777a78115SJacob Keller return; 122877a78115SJacob Keller 122977a78115SJacob Keller err_kworker: 123077a78115SJacob Keller /* If we registered a PTP clock, release it */ 123177a78115SJacob Keller if (pf->ptp.clock) { 123277a78115SJacob Keller ptp_clock_unregister(pf->ptp.clock); 123377a78115SJacob Keller pf->ptp.clock = NULL; 123477a78115SJacob Keller } 123577a78115SJacob Keller dev_err(dev, "PTP failed %d\n", err); 123606c16d89SJacob Keller } 123706c16d89SJacob Keller 123806c16d89SJacob Keller /** 123906c16d89SJacob Keller * ice_ptp_release - Disable the driver/HW support and unregister the clock 124006c16d89SJacob Keller * @pf: Board private structure 124106c16d89SJacob Keller * 124206c16d89SJacob Keller * This function handles the cleanup work required from the initialization by 124306c16d89SJacob Keller * clearing out the important information and unregistering the clock 124406c16d89SJacob Keller */ 124506c16d89SJacob Keller void ice_ptp_release(struct ice_pf *pf) 124606c16d89SJacob Keller { 124777a78115SJacob Keller /* Disable timestamping for both Tx and Rx */ 124877a78115SJacob Keller ice_ptp_cfg_timestamp(pf, false); 124977a78115SJacob Keller 1250*ea9b847cSJacob Keller ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 1251*ea9b847cSJacob Keller 125206c16d89SJacob Keller clear_bit(ICE_FLAG_PTP, pf->flags); 125306c16d89SJacob Keller 125477a78115SJacob Keller kthread_cancel_delayed_work_sync(&pf->ptp.work); 125577a78115SJacob Keller 125677a78115SJacob Keller if (pf->ptp.kworker) { 125777a78115SJacob Keller kthread_destroy_worker(pf->ptp.kworker); 125877a78115SJacob Keller pf->ptp.kworker = NULL; 125977a78115SJacob Keller } 126077a78115SJacob Keller 126106c16d89SJacob Keller if (!pf->ptp.clock) 126206c16d89SJacob Keller return; 126306c16d89SJacob Keller 126467569a7fSJacob Keller ice_clear_ptp_clock_index(pf); 126506c16d89SJacob Keller ptp_clock_unregister(pf->ptp.clock); 126606c16d89SJacob Keller pf->ptp.clock = NULL; 126706c16d89SJacob Keller 126806c16d89SJacob Keller dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n"); 126906c16d89SJacob Keller } 1270