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 7172db5f9SMaciej Machnikowski #define E810_OUT_PROP_DELAY_NS 1 8172db5f9SMaciej Machnikowski 906c16d89SJacob Keller /** 10ea9b847cSJacob Keller * ice_set_tx_tstamp - Enable or disable Tx timestamping 11ea9b847cSJacob Keller * @pf: The PF pointer to search in 12ea9b847cSJacob Keller * @on: bool value for whether timestamps are enabled or disabled 13ea9b847cSJacob Keller */ 14ea9b847cSJacob Keller static void ice_set_tx_tstamp(struct ice_pf *pf, bool on) 15ea9b847cSJacob Keller { 16ea9b847cSJacob Keller struct ice_vsi *vsi; 17ea9b847cSJacob Keller u32 val; 18ea9b847cSJacob Keller u16 i; 19ea9b847cSJacob Keller 20ea9b847cSJacob Keller vsi = ice_get_main_vsi(pf); 21ea9b847cSJacob Keller if (!vsi) 22ea9b847cSJacob Keller return; 23ea9b847cSJacob Keller 24ea9b847cSJacob Keller /* Set the timestamp enable flag for all the Tx rings */ 2584c5fb8cSJacob Keller ice_for_each_txq(vsi, i) { 26ea9b847cSJacob Keller if (!vsi->tx_rings[i]) 27ea9b847cSJacob Keller continue; 28ea9b847cSJacob Keller vsi->tx_rings[i]->ptp_tx = on; 29ea9b847cSJacob Keller } 30ea9b847cSJacob Keller 31ea9b847cSJacob Keller /* Configure the Tx timestamp interrupt */ 32ea9b847cSJacob Keller val = rd32(&pf->hw, PFINT_OICR_ENA); 33ea9b847cSJacob Keller if (on) 34ea9b847cSJacob Keller val |= PFINT_OICR_TSYN_TX_M; 35ea9b847cSJacob Keller else 36ea9b847cSJacob Keller val &= ~PFINT_OICR_TSYN_TX_M; 37ea9b847cSJacob Keller wr32(&pf->hw, PFINT_OICR_ENA, val); 38ea9b847cSJacob Keller } 39ea9b847cSJacob Keller 40ea9b847cSJacob Keller /** 4177a78115SJacob Keller * ice_set_rx_tstamp - Enable or disable Rx timestamping 4277a78115SJacob Keller * @pf: The PF pointer to search in 4377a78115SJacob Keller * @on: bool value for whether timestamps are enabled or disabled 4477a78115SJacob Keller */ 4577a78115SJacob Keller static void ice_set_rx_tstamp(struct ice_pf *pf, bool on) 4677a78115SJacob Keller { 4777a78115SJacob Keller struct ice_vsi *vsi; 4877a78115SJacob Keller u16 i; 4977a78115SJacob Keller 5077a78115SJacob Keller vsi = ice_get_main_vsi(pf); 5177a78115SJacob Keller if (!vsi) 5277a78115SJacob Keller return; 5377a78115SJacob Keller 5477a78115SJacob Keller /* Set the timestamp flag for all the Rx rings */ 5577a78115SJacob Keller ice_for_each_rxq(vsi, i) { 5677a78115SJacob Keller if (!vsi->rx_rings[i]) 5777a78115SJacob Keller continue; 5877a78115SJacob Keller vsi->rx_rings[i]->ptp_rx = on; 5977a78115SJacob Keller } 6077a78115SJacob Keller } 6177a78115SJacob Keller 6277a78115SJacob Keller /** 6377a78115SJacob Keller * ice_ptp_cfg_timestamp - Configure timestamp for init/deinit 6477a78115SJacob Keller * @pf: Board private structure 6577a78115SJacob Keller * @ena: bool value to enable or disable time stamp 6677a78115SJacob Keller * 6777a78115SJacob Keller * This function will configure timestamping during PTP initialization 6877a78115SJacob Keller * and deinitialization 6977a78115SJacob Keller */ 7077a78115SJacob Keller static void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena) 7177a78115SJacob Keller { 72ea9b847cSJacob Keller ice_set_tx_tstamp(pf, ena); 7377a78115SJacob Keller ice_set_rx_tstamp(pf, ena); 7477a78115SJacob Keller 75ea9b847cSJacob Keller if (ena) { 7677a78115SJacob Keller pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_ALL; 77ea9b847cSJacob Keller pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_ON; 78ea9b847cSJacob Keller } else { 7977a78115SJacob Keller pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE; 80ea9b847cSJacob Keller pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_OFF; 81ea9b847cSJacob Keller } 8277a78115SJacob Keller } 8377a78115SJacob Keller 8477a78115SJacob Keller /** 8567569a7fSJacob Keller * ice_get_ptp_clock_index - Get the PTP clock index 8667569a7fSJacob Keller * @pf: the PF pointer 8767569a7fSJacob Keller * 8867569a7fSJacob Keller * Determine the clock index of the PTP clock associated with this device. If 8967569a7fSJacob Keller * this is the PF controlling the clock, just use the local access to the 9067569a7fSJacob Keller * clock device pointer. 9167569a7fSJacob Keller * 9267569a7fSJacob Keller * Otherwise, read from the driver shared parameters to determine the clock 9367569a7fSJacob Keller * index value. 9467569a7fSJacob Keller * 9567569a7fSJacob Keller * Returns: the index of the PTP clock associated with this device, or -1 if 9667569a7fSJacob Keller * there is no associated clock. 9767569a7fSJacob Keller */ 9867569a7fSJacob Keller int ice_get_ptp_clock_index(struct ice_pf *pf) 9967569a7fSJacob Keller { 10067569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 10167569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 10267569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 10367569a7fSJacob Keller u8 tmr_idx; 10467569a7fSJacob Keller u32 value; 10567569a7fSJacob Keller int err; 10667569a7fSJacob Keller 10767569a7fSJacob Keller /* Use the ptp_clock structure if we're the main PF */ 10867569a7fSJacob Keller if (pf->ptp.clock) 10967569a7fSJacob Keller return ptp_clock_index(pf->ptp.clock); 11067569a7fSJacob Keller 11167569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 11267569a7fSJacob Keller if (!tmr_idx) 11367569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 11467569a7fSJacob Keller else 11567569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 11667569a7fSJacob Keller 11767569a7fSJacob Keller err = ice_aq_get_driver_param(hw, param_idx, &value, NULL); 11867569a7fSJacob Keller if (err) { 11967569a7fSJacob Keller dev_err(dev, "Failed to read PTP clock index parameter, err %d aq_err %s\n", 12067569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 12167569a7fSJacob Keller return -1; 12267569a7fSJacob Keller } 12367569a7fSJacob Keller 12467569a7fSJacob Keller /* The PTP clock index is an integer, and will be between 0 and 12567569a7fSJacob Keller * INT_MAX. The highest bit of the driver shared parameter is used to 12667569a7fSJacob Keller * indicate whether or not the currently stored clock index is valid. 12767569a7fSJacob Keller */ 12867569a7fSJacob Keller if (!(value & PTP_SHARED_CLK_IDX_VALID)) 12967569a7fSJacob Keller return -1; 13067569a7fSJacob Keller 13167569a7fSJacob Keller return value & ~PTP_SHARED_CLK_IDX_VALID; 13267569a7fSJacob Keller } 13367569a7fSJacob Keller 13467569a7fSJacob Keller /** 13567569a7fSJacob Keller * ice_set_ptp_clock_index - Set the PTP clock index 13667569a7fSJacob Keller * @pf: the PF pointer 13767569a7fSJacob Keller * 13867569a7fSJacob Keller * Set the PTP clock index for this device into the shared driver parameters, 13967569a7fSJacob Keller * so that other PFs associated with this device can read it. 14067569a7fSJacob Keller * 14167569a7fSJacob Keller * If the PF is unable to store the clock index, it will log an error, but 14267569a7fSJacob Keller * will continue operating PTP. 14367569a7fSJacob Keller */ 14467569a7fSJacob Keller static void ice_set_ptp_clock_index(struct ice_pf *pf) 14567569a7fSJacob Keller { 14667569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 14767569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 14867569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 14967569a7fSJacob Keller u8 tmr_idx; 15067569a7fSJacob Keller u32 value; 15167569a7fSJacob Keller int err; 15267569a7fSJacob Keller 15367569a7fSJacob Keller if (!pf->ptp.clock) 15467569a7fSJacob Keller return; 15567569a7fSJacob Keller 15667569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 15767569a7fSJacob Keller if (!tmr_idx) 15867569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 15967569a7fSJacob Keller else 16067569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 16167569a7fSJacob Keller 16267569a7fSJacob Keller value = (u32)ptp_clock_index(pf->ptp.clock); 16367569a7fSJacob Keller if (value > INT_MAX) { 16467569a7fSJacob Keller dev_err(dev, "PTP Clock index is too large to store\n"); 16567569a7fSJacob Keller return; 16667569a7fSJacob Keller } 16767569a7fSJacob Keller value |= PTP_SHARED_CLK_IDX_VALID; 16867569a7fSJacob Keller 16967569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, value, NULL); 17067569a7fSJacob Keller if (err) { 17167569a7fSJacob Keller dev_err(dev, "Failed to set PTP clock index parameter, err %d aq_err %s\n", 17267569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 17367569a7fSJacob Keller } 17467569a7fSJacob Keller } 17567569a7fSJacob Keller 17667569a7fSJacob Keller /** 17767569a7fSJacob Keller * ice_clear_ptp_clock_index - Clear the PTP clock index 17867569a7fSJacob Keller * @pf: the PF pointer 17967569a7fSJacob Keller * 18067569a7fSJacob Keller * Clear the PTP clock index for this device. Must be called when 18167569a7fSJacob Keller * unregistering the PTP clock, in order to ensure other PFs stop reporting 18267569a7fSJacob Keller * a clock object that no longer exists. 18367569a7fSJacob Keller */ 18467569a7fSJacob Keller static void ice_clear_ptp_clock_index(struct ice_pf *pf) 18567569a7fSJacob Keller { 18667569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 18767569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 18867569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 18967569a7fSJacob Keller u8 tmr_idx; 19067569a7fSJacob Keller int err; 19167569a7fSJacob Keller 19267569a7fSJacob Keller /* Do not clear the index if we don't own the timer */ 19367569a7fSJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned) 19467569a7fSJacob Keller return; 19567569a7fSJacob Keller 19667569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 19767569a7fSJacob Keller if (!tmr_idx) 19867569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 19967569a7fSJacob Keller else 20067569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 20167569a7fSJacob Keller 20267569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, 0, NULL); 20367569a7fSJacob Keller if (err) { 20467569a7fSJacob Keller dev_dbg(dev, "Failed to clear PTP clock index parameter, err %d aq_err %s\n", 20567569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 20667569a7fSJacob Keller } 20767569a7fSJacob Keller } 20867569a7fSJacob Keller 20967569a7fSJacob Keller /** 21006c16d89SJacob Keller * ice_ptp_read_src_clk_reg - Read the source clock register 21106c16d89SJacob Keller * @pf: Board private structure 21206c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 21306c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 21406c16d89SJacob Keller */ 21506c16d89SJacob Keller static u64 21606c16d89SJacob Keller ice_ptp_read_src_clk_reg(struct ice_pf *pf, struct ptp_system_timestamp *sts) 21706c16d89SJacob Keller { 21806c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 21906c16d89SJacob Keller u32 hi, lo, lo2; 22006c16d89SJacob Keller u8 tmr_idx; 22106c16d89SJacob Keller 22206c16d89SJacob Keller tmr_idx = ice_get_ptp_src_clock_index(hw); 22306c16d89SJacob Keller /* Read the system timestamp pre PHC read */ 22406c16d89SJacob Keller ptp_read_system_prets(sts); 22506c16d89SJacob Keller 22606c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 22706c16d89SJacob Keller 22806c16d89SJacob Keller /* Read the system timestamp post PHC read */ 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 ptp_read_system_prets(sts); 23906c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 24006c16d89SJacob Keller ptp_read_system_postts(sts); 24106c16d89SJacob Keller hi = rd32(hw, GLTSYN_TIME_H(tmr_idx)); 24206c16d89SJacob Keller } 24306c16d89SJacob Keller 24406c16d89SJacob Keller return ((u64)hi << 32) | lo; 24506c16d89SJacob Keller } 24606c16d89SJacob Keller 24706c16d89SJacob Keller /** 24877a78115SJacob Keller * ice_ptp_update_cached_phctime - Update the cached PHC time values 24977a78115SJacob Keller * @pf: Board specific private structure 25077a78115SJacob Keller * 25177a78115SJacob Keller * This function updates the system time values which are cached in the PF 25277a78115SJacob Keller * structure and the Rx rings. 25377a78115SJacob Keller * 25477a78115SJacob Keller * This function must be called periodically to ensure that the cached value 25577a78115SJacob Keller * is never more than 2 seconds old. It must also be called whenever the PHC 25677a78115SJacob Keller * time has been changed. 25777a78115SJacob Keller */ 25877a78115SJacob Keller static void ice_ptp_update_cached_phctime(struct ice_pf *pf) 25977a78115SJacob Keller { 26077a78115SJacob Keller u64 systime; 26177a78115SJacob Keller int i; 26277a78115SJacob Keller 26377a78115SJacob Keller /* Read the current PHC time */ 26477a78115SJacob Keller systime = ice_ptp_read_src_clk_reg(pf, NULL); 26577a78115SJacob Keller 26677a78115SJacob Keller /* Update the cached PHC time stored in the PF structure */ 26777a78115SJacob Keller WRITE_ONCE(pf->ptp.cached_phc_time, systime); 26877a78115SJacob Keller 26977a78115SJacob Keller ice_for_each_vsi(pf, i) { 27077a78115SJacob Keller struct ice_vsi *vsi = pf->vsi[i]; 27177a78115SJacob Keller int j; 27277a78115SJacob Keller 27377a78115SJacob Keller if (!vsi) 27477a78115SJacob Keller continue; 27577a78115SJacob Keller 27677a78115SJacob Keller if (vsi->type != ICE_VSI_PF) 27777a78115SJacob Keller continue; 27877a78115SJacob Keller 27977a78115SJacob Keller ice_for_each_rxq(vsi, j) { 28077a78115SJacob Keller if (!vsi->rx_rings[j]) 28177a78115SJacob Keller continue; 28277a78115SJacob Keller WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime); 28377a78115SJacob Keller } 28477a78115SJacob Keller } 28577a78115SJacob Keller } 28677a78115SJacob Keller 28777a78115SJacob Keller /** 28877a78115SJacob Keller * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b 28977a78115SJacob Keller * @cached_phc_time: recently cached copy of PHC time 29077a78115SJacob Keller * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value 29177a78115SJacob Keller * 29277a78115SJacob Keller * Hardware captures timestamps which contain only 32 bits of nominal 29377a78115SJacob Keller * nanoseconds, as opposed to the 64bit timestamps that the stack expects. 29477a78115SJacob Keller * Note that the captured timestamp values may be 40 bits, but the lower 29577a78115SJacob Keller * 8 bits are sub-nanoseconds and generally discarded. 29677a78115SJacob Keller * 29777a78115SJacob Keller * Extend the 32bit nanosecond timestamp using the following algorithm and 29877a78115SJacob Keller * assumptions: 29977a78115SJacob Keller * 30077a78115SJacob Keller * 1) have a recently cached copy of the PHC time 30177a78115SJacob Keller * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1 30277a78115SJacob Keller * seconds) before or after the PHC time was captured. 30377a78115SJacob Keller * 3) calculate the delta between the cached time and the timestamp 30477a78115SJacob Keller * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was 30577a78115SJacob Keller * captured after the PHC time. In this case, the full timestamp is just 30677a78115SJacob Keller * the cached PHC time plus the delta. 30777a78115SJacob Keller * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the 30877a78115SJacob Keller * timestamp was captured *before* the PHC time, i.e. because the PHC 30977a78115SJacob Keller * cache was updated after the timestamp was captured by hardware. In this 31077a78115SJacob Keller * case, the full timestamp is the cached time minus the inverse delta. 31177a78115SJacob Keller * 31277a78115SJacob Keller * This algorithm works even if the PHC time was updated after a Tx timestamp 31377a78115SJacob Keller * was requested, but before the Tx timestamp event was reported from 31477a78115SJacob Keller * hardware. 31577a78115SJacob Keller * 31677a78115SJacob Keller * This calculation primarily relies on keeping the cached PHC time up to 31777a78115SJacob Keller * date. If the timestamp was captured more than 2^31 nanoseconds after the 31877a78115SJacob Keller * PHC time, it is possible that the lower 32bits of PHC time have 31977a78115SJacob Keller * overflowed more than once, and we might generate an incorrect timestamp. 32077a78115SJacob Keller * 32177a78115SJacob Keller * This is prevented by (a) periodically updating the cached PHC time once 32277a78115SJacob Keller * a second, and (b) discarding any Tx timestamp packet if it has waited for 32377a78115SJacob Keller * a timestamp for more than one second. 32477a78115SJacob Keller */ 32577a78115SJacob Keller static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp) 32677a78115SJacob Keller { 32777a78115SJacob Keller u32 delta, phc_time_lo; 32877a78115SJacob Keller u64 ns; 32977a78115SJacob Keller 33077a78115SJacob Keller /* Extract the lower 32 bits of the PHC time */ 33177a78115SJacob Keller phc_time_lo = (u32)cached_phc_time; 33277a78115SJacob Keller 33377a78115SJacob Keller /* Calculate the delta between the lower 32bits of the cached PHC 33477a78115SJacob Keller * time and the in_tstamp value 33577a78115SJacob Keller */ 33677a78115SJacob Keller delta = (in_tstamp - phc_time_lo); 33777a78115SJacob Keller 33877a78115SJacob Keller /* Do not assume that the in_tstamp is always more recent than the 33977a78115SJacob Keller * cached PHC time. If the delta is large, it indicates that the 34077a78115SJacob Keller * in_tstamp was taken in the past, and should be converted 34177a78115SJacob Keller * forward. 34277a78115SJacob Keller */ 34377a78115SJacob Keller if (delta > (U32_MAX / 2)) { 34477a78115SJacob Keller /* reverse the delta calculation here */ 34577a78115SJacob Keller delta = (phc_time_lo - in_tstamp); 34677a78115SJacob Keller ns = cached_phc_time - delta; 34777a78115SJacob Keller } else { 34877a78115SJacob Keller ns = cached_phc_time + delta; 34977a78115SJacob Keller } 35077a78115SJacob Keller 35177a78115SJacob Keller return ns; 35277a78115SJacob Keller } 35377a78115SJacob Keller 35477a78115SJacob Keller /** 355ea9b847cSJacob Keller * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds 356ea9b847cSJacob Keller * @pf: Board private structure 357ea9b847cSJacob Keller * @in_tstamp: Ingress/egress 40b timestamp value 358ea9b847cSJacob Keller * 359ea9b847cSJacob Keller * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal 360ea9b847cSJacob Keller * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit. 361ea9b847cSJacob Keller * 362ea9b847cSJacob Keller * *--------------------------------------------------------------* 363ea9b847cSJacob Keller * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v | 364ea9b847cSJacob Keller * *--------------------------------------------------------------* 365ea9b847cSJacob Keller * 366ea9b847cSJacob Keller * The low bit is an indicator of whether the timestamp is valid. The next 367ea9b847cSJacob Keller * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow, 368ea9b847cSJacob Keller * and the remaining 32 bits are the lower 32 bits of the PHC timer. 369ea9b847cSJacob Keller * 370ea9b847cSJacob Keller * It is assumed that the caller verifies the timestamp is valid prior to 371ea9b847cSJacob Keller * calling this function. 372ea9b847cSJacob Keller * 373ea9b847cSJacob Keller * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC 374ea9b847cSJacob Keller * time stored in the device private PTP structure as the basis for timestamp 375ea9b847cSJacob Keller * extension. 376ea9b847cSJacob Keller * 377ea9b847cSJacob Keller * See ice_ptp_extend_32b_ts for a detailed explanation of the extension 378ea9b847cSJacob Keller * algorithm. 379ea9b847cSJacob Keller */ 380ea9b847cSJacob Keller static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp) 381ea9b847cSJacob Keller { 382ea9b847cSJacob Keller const u64 mask = GENMASK_ULL(31, 0); 383ea9b847cSJacob Keller 384ea9b847cSJacob Keller return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time, 385ea9b847cSJacob Keller (in_tstamp >> 8) & mask); 386ea9b847cSJacob Keller } 387ea9b847cSJacob Keller 388ea9b847cSJacob Keller /** 38906c16d89SJacob Keller * ice_ptp_read_time - Read the time from the device 39006c16d89SJacob Keller * @pf: Board private structure 39106c16d89SJacob Keller * @ts: timespec structure to hold the current time value 39206c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 39306c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 39406c16d89SJacob Keller * 39506c16d89SJacob Keller * This function reads the source clock registers and stores them in a timespec. 39606c16d89SJacob Keller * However, since the registers are 64 bits of nanoseconds, we must convert the 39706c16d89SJacob Keller * result to a timespec before we can return. 39806c16d89SJacob Keller */ 39906c16d89SJacob Keller static void 40006c16d89SJacob Keller ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts, 40106c16d89SJacob Keller struct ptp_system_timestamp *sts) 40206c16d89SJacob Keller { 40306c16d89SJacob Keller u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts); 40406c16d89SJacob Keller 40506c16d89SJacob Keller *ts = ns_to_timespec64(time_ns); 40606c16d89SJacob Keller } 40706c16d89SJacob Keller 40806c16d89SJacob Keller /** 40906c16d89SJacob Keller * ice_ptp_write_init - Set PHC time to provided value 41006c16d89SJacob Keller * @pf: Board private structure 41106c16d89SJacob Keller * @ts: timespec structure that holds the new time value 41206c16d89SJacob Keller * 41306c16d89SJacob Keller * Set the PHC time to the specified time provided in the timespec. 41406c16d89SJacob Keller */ 41506c16d89SJacob Keller static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts) 41606c16d89SJacob Keller { 41706c16d89SJacob Keller u64 ns = timespec64_to_ns(ts); 41806c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 41906c16d89SJacob Keller 42006c16d89SJacob Keller return ice_ptp_init_time(hw, ns); 42106c16d89SJacob Keller } 42206c16d89SJacob Keller 42306c16d89SJacob Keller /** 42406c16d89SJacob Keller * ice_ptp_write_adj - Adjust PHC clock time atomically 42506c16d89SJacob Keller * @pf: Board private structure 42606c16d89SJacob Keller * @adj: Adjustment in nanoseconds 42706c16d89SJacob Keller * 42806c16d89SJacob Keller * Perform an atomic adjustment of the PHC time by the specified number of 42906c16d89SJacob Keller * nanoseconds. 43006c16d89SJacob Keller */ 43106c16d89SJacob Keller static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj) 43206c16d89SJacob Keller { 43306c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 43406c16d89SJacob Keller 43506c16d89SJacob Keller return ice_ptp_adj_clock(hw, adj); 43606c16d89SJacob Keller } 43706c16d89SJacob Keller 43806c16d89SJacob Keller /** 43906c16d89SJacob Keller * ice_ptp_adjfine - Adjust clock increment rate 44006c16d89SJacob Keller * @info: the driver's PTP info structure 44106c16d89SJacob Keller * @scaled_ppm: Parts per million with 16-bit fractional field 44206c16d89SJacob Keller * 44306c16d89SJacob Keller * Adjust the frequency of the clock by the indicated scaled ppm from the 44406c16d89SJacob Keller * base frequency. 44506c16d89SJacob Keller */ 44606c16d89SJacob Keller static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm) 44706c16d89SJacob Keller { 44806c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 44906c16d89SJacob Keller u64 freq, divisor = 1000000ULL; 45006c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 45106c16d89SJacob Keller s64 incval, diff; 45206c16d89SJacob Keller int neg_adj = 0; 45306c16d89SJacob Keller int err; 45406c16d89SJacob Keller 45506c16d89SJacob Keller incval = ICE_PTP_NOMINAL_INCVAL_E810; 45606c16d89SJacob Keller 45706c16d89SJacob Keller if (scaled_ppm < 0) { 45806c16d89SJacob Keller neg_adj = 1; 45906c16d89SJacob Keller scaled_ppm = -scaled_ppm; 46006c16d89SJacob Keller } 46106c16d89SJacob Keller 46206c16d89SJacob Keller while ((u64)scaled_ppm > div_u64(U64_MAX, incval)) { 46306c16d89SJacob Keller /* handle overflow by scaling down the scaled_ppm and 46406c16d89SJacob Keller * the divisor, losing some precision 46506c16d89SJacob Keller */ 46606c16d89SJacob Keller scaled_ppm >>= 2; 46706c16d89SJacob Keller divisor >>= 2; 46806c16d89SJacob Keller } 46906c16d89SJacob Keller 47006c16d89SJacob Keller freq = (incval * (u64)scaled_ppm) >> 16; 47106c16d89SJacob Keller diff = div_u64(freq, divisor); 47206c16d89SJacob Keller 47306c16d89SJacob Keller if (neg_adj) 47406c16d89SJacob Keller incval -= diff; 47506c16d89SJacob Keller else 47606c16d89SJacob Keller incval += diff; 47706c16d89SJacob Keller 47806c16d89SJacob Keller err = ice_ptp_write_incval_locked(hw, incval); 47906c16d89SJacob Keller if (err) { 48006c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n", 48106c16d89SJacob Keller err); 48206c16d89SJacob Keller return -EIO; 48306c16d89SJacob Keller } 48406c16d89SJacob Keller 48506c16d89SJacob Keller return 0; 48606c16d89SJacob Keller } 48706c16d89SJacob Keller 48806c16d89SJacob Keller /** 489172db5f9SMaciej Machnikowski * ice_ptp_extts_work - Workqueue task function 490172db5f9SMaciej Machnikowski * @work: external timestamp work structure 491172db5f9SMaciej Machnikowski * 492172db5f9SMaciej Machnikowski * Service for PTP external clock event 493172db5f9SMaciej Machnikowski */ 494172db5f9SMaciej Machnikowski static void ice_ptp_extts_work(struct kthread_work *work) 495172db5f9SMaciej Machnikowski { 496172db5f9SMaciej Machnikowski struct ice_ptp *ptp = container_of(work, struct ice_ptp, extts_work); 497172db5f9SMaciej Machnikowski struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 498172db5f9SMaciej Machnikowski struct ptp_clock_event event; 499172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 500172db5f9SMaciej Machnikowski u8 chan, tmr_idx; 501172db5f9SMaciej Machnikowski u32 hi, lo; 502172db5f9SMaciej Machnikowski 503172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 504172db5f9SMaciej Machnikowski /* Event time is captured by one of the two matched registers 505172db5f9SMaciej Machnikowski * GLTSYN_EVNT_L: 32 LSB of sampled time event 506172db5f9SMaciej Machnikowski * GLTSYN_EVNT_H: 32 MSB of sampled time event 507172db5f9SMaciej Machnikowski * Event is defined in GLTSYN_EVNT_0 register 508172db5f9SMaciej Machnikowski */ 509172db5f9SMaciej Machnikowski for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) { 510172db5f9SMaciej Machnikowski /* Check if channel is enabled */ 511172db5f9SMaciej Machnikowski if (pf->ptp.ext_ts_irq & (1 << chan)) { 512172db5f9SMaciej Machnikowski lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx)); 513172db5f9SMaciej Machnikowski hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx)); 514172db5f9SMaciej Machnikowski event.timestamp = (((u64)hi) << 32) | lo; 515172db5f9SMaciej Machnikowski event.type = PTP_CLOCK_EXTTS; 516172db5f9SMaciej Machnikowski event.index = chan; 517172db5f9SMaciej Machnikowski 518172db5f9SMaciej Machnikowski /* Fire event */ 519172db5f9SMaciej Machnikowski ptp_clock_event(pf->ptp.clock, &event); 520172db5f9SMaciej Machnikowski pf->ptp.ext_ts_irq &= ~(1 << chan); 521172db5f9SMaciej Machnikowski } 522172db5f9SMaciej Machnikowski } 523172db5f9SMaciej Machnikowski } 524172db5f9SMaciej Machnikowski 525172db5f9SMaciej Machnikowski /** 526172db5f9SMaciej Machnikowski * ice_ptp_cfg_extts - Configure EXTTS pin and channel 527172db5f9SMaciej Machnikowski * @pf: Board private structure 528172db5f9SMaciej Machnikowski * @ena: true to enable; false to disable 529172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 530172db5f9SMaciej Machnikowski * @gpio_pin: GPIO pin 531172db5f9SMaciej Machnikowski * @extts_flags: request flags from the ptp_extts_request.flags 532172db5f9SMaciej Machnikowski */ 533172db5f9SMaciej Machnikowski static int 534172db5f9SMaciej Machnikowski ice_ptp_cfg_extts(struct ice_pf *pf, bool ena, unsigned int chan, u32 gpio_pin, 535172db5f9SMaciej Machnikowski unsigned int extts_flags) 536172db5f9SMaciej Machnikowski { 537172db5f9SMaciej Machnikowski u32 func, aux_reg, gpio_reg, irq_reg; 538172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 539172db5f9SMaciej Machnikowski u8 tmr_idx; 540172db5f9SMaciej Machnikowski 541172db5f9SMaciej Machnikowski if (chan > (unsigned int)pf->ptp.info.n_ext_ts) 542172db5f9SMaciej Machnikowski return -EINVAL; 543172db5f9SMaciej Machnikowski 544172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 545172db5f9SMaciej Machnikowski 546172db5f9SMaciej Machnikowski irq_reg = rd32(hw, PFINT_OICR_ENA); 547172db5f9SMaciej Machnikowski 548172db5f9SMaciej Machnikowski if (ena) { 549172db5f9SMaciej Machnikowski /* Enable the interrupt */ 550172db5f9SMaciej Machnikowski irq_reg |= PFINT_OICR_TSYN_EVNT_M; 551172db5f9SMaciej Machnikowski aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M; 552172db5f9SMaciej Machnikowski 553172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE BIT(0) 554172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE BIT(1) 555172db5f9SMaciej Machnikowski 556172db5f9SMaciej Machnikowski /* set event level to requested edge */ 557172db5f9SMaciej Machnikowski if (extts_flags & PTP_FALLING_EDGE) 558172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE; 559172db5f9SMaciej Machnikowski if (extts_flags & PTP_RISING_EDGE) 560172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE; 561172db5f9SMaciej Machnikowski 562172db5f9SMaciej Machnikowski /* Write GPIO CTL reg. 563172db5f9SMaciej Machnikowski * 0x1 is input sampled by EVENT register(channel) 564172db5f9SMaciej Machnikowski * + num_in_channels * tmr_idx 565172db5f9SMaciej Machnikowski */ 566172db5f9SMaciej Machnikowski func = 1 + chan + (tmr_idx * 3); 567172db5f9SMaciej Machnikowski gpio_reg = ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & 568172db5f9SMaciej Machnikowski GLGEN_GPIO_CTL_PIN_FUNC_M); 569172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan |= (1 << chan); 570172db5f9SMaciej Machnikowski } else { 571172db5f9SMaciej Machnikowski /* clear the values we set to reset defaults */ 572172db5f9SMaciej Machnikowski aux_reg = 0; 573172db5f9SMaciej Machnikowski gpio_reg = 0; 574172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan &= ~(1 << chan); 575172db5f9SMaciej Machnikowski if (!pf->ptp.ext_ts_chan) 576172db5f9SMaciej Machnikowski irq_reg &= ~PFINT_OICR_TSYN_EVNT_M; 577172db5f9SMaciej Machnikowski } 578172db5f9SMaciej Machnikowski 579172db5f9SMaciej Machnikowski wr32(hw, PFINT_OICR_ENA, irq_reg); 580172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg); 581172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg); 582172db5f9SMaciej Machnikowski 583172db5f9SMaciej Machnikowski return 0; 584172db5f9SMaciej Machnikowski } 585172db5f9SMaciej Machnikowski 586172db5f9SMaciej Machnikowski /** 587172db5f9SMaciej Machnikowski * ice_ptp_cfg_clkout - Configure clock to generate periodic wave 588172db5f9SMaciej Machnikowski * @pf: Board private structure 589172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 590172db5f9SMaciej Machnikowski * @config: desired periodic clk configuration. NULL will disable channel 591172db5f9SMaciej Machnikowski * @store: If set to true the values will be stored 592172db5f9SMaciej Machnikowski * 593172db5f9SMaciej Machnikowski * Configure the internal clock generator modules to generate the clock wave of 594172db5f9SMaciej Machnikowski * specified period. 595172db5f9SMaciej Machnikowski */ 596172db5f9SMaciej Machnikowski static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan, 597172db5f9SMaciej Machnikowski struct ice_perout_channel *config, bool store) 598172db5f9SMaciej Machnikowski { 599172db5f9SMaciej Machnikowski u64 current_time, period, start_time, phase; 600172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 601172db5f9SMaciej Machnikowski u32 func, val, gpio_pin; 602172db5f9SMaciej Machnikowski u8 tmr_idx; 603172db5f9SMaciej Machnikowski 604172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 605172db5f9SMaciej Machnikowski 606172db5f9SMaciej Machnikowski /* 0. Reset mode & out_en in AUX_OUT */ 607172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0); 608172db5f9SMaciej Machnikowski 609172db5f9SMaciej Machnikowski /* If we're disabling the output, clear out CLKO and TGT and keep 610172db5f9SMaciej Machnikowski * output level low 611172db5f9SMaciej Machnikowski */ 612172db5f9SMaciej Machnikowski if (!config || !config->ena) { 613172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), 0); 614172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), 0); 615172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), 0); 616172db5f9SMaciej Machnikowski 617172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M; 618172db5f9SMaciej Machnikowski gpio_pin = pf->ptp.perout_channels[chan].gpio_pin; 619172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 620172db5f9SMaciej Machnikowski 621172db5f9SMaciej Machnikowski /* Store the value if requested */ 622172db5f9SMaciej Machnikowski if (store) 623172db5f9SMaciej Machnikowski memset(&pf->ptp.perout_channels[chan], 0, 624172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 625172db5f9SMaciej Machnikowski 626172db5f9SMaciej Machnikowski return 0; 627172db5f9SMaciej Machnikowski } 628172db5f9SMaciej Machnikowski period = config->period; 629172db5f9SMaciej Machnikowski start_time = config->start_time; 630172db5f9SMaciej Machnikowski div64_u64_rem(start_time, period, &phase); 631172db5f9SMaciej Machnikowski gpio_pin = config->gpio_pin; 632172db5f9SMaciej Machnikowski 633172db5f9SMaciej Machnikowski /* 1. Write clkout with half of required period value */ 634172db5f9SMaciej Machnikowski if (period & 0x1) { 635172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n"); 636172db5f9SMaciej Machnikowski goto err; 637172db5f9SMaciej Machnikowski } 638172db5f9SMaciej Machnikowski 639172db5f9SMaciej Machnikowski period >>= 1; 640172db5f9SMaciej Machnikowski 641172db5f9SMaciej Machnikowski /* For proper operation, the GLTSYN_CLKO must be larger than clock tick 642172db5f9SMaciej Machnikowski */ 643172db5f9SMaciej Machnikowski #define MIN_PULSE 3 644172db5f9SMaciej Machnikowski if (period <= MIN_PULSE || period > U32_MAX) { 645172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be > %d && < 2^33", 646172db5f9SMaciej Machnikowski MIN_PULSE * 2); 647172db5f9SMaciej Machnikowski goto err; 648172db5f9SMaciej Machnikowski } 649172db5f9SMaciej Machnikowski 650172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period)); 651172db5f9SMaciej Machnikowski 652172db5f9SMaciej Machnikowski /* Allow time for programming before start_time is hit */ 653172db5f9SMaciej Machnikowski current_time = ice_ptp_read_src_clk_reg(pf, NULL); 654172db5f9SMaciej Machnikowski 655172db5f9SMaciej Machnikowski /* if start time is in the past start the timer at the nearest second 656172db5f9SMaciej Machnikowski * maintaining phase 657172db5f9SMaciej Machnikowski */ 658172db5f9SMaciej Machnikowski if (start_time < current_time) 6595f773519SMaciej Machnikowski start_time = div64_u64(current_time + NSEC_PER_SEC - 1, 660172db5f9SMaciej Machnikowski NSEC_PER_SEC) * NSEC_PER_SEC + phase; 661172db5f9SMaciej Machnikowski 662172db5f9SMaciej Machnikowski start_time -= E810_OUT_PROP_DELAY_NS; 663172db5f9SMaciej Machnikowski 664172db5f9SMaciej Machnikowski /* 2. Write TARGET time */ 665172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start_time)); 666172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start_time)); 667172db5f9SMaciej Machnikowski 668172db5f9SMaciej Machnikowski /* 3. Write AUX_OUT register */ 669172db5f9SMaciej Machnikowski val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M; 670172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val); 671172db5f9SMaciej Machnikowski 672172db5f9SMaciej Machnikowski /* 4. write GPIO CTL reg */ 673172db5f9SMaciej Machnikowski func = 8 + chan + (tmr_idx * 4); 674172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M | 675172db5f9SMaciej Machnikowski ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & GLGEN_GPIO_CTL_PIN_FUNC_M); 676172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 677172db5f9SMaciej Machnikowski 678172db5f9SMaciej Machnikowski /* Store the value if requested */ 679172db5f9SMaciej Machnikowski if (store) { 680172db5f9SMaciej Machnikowski memcpy(&pf->ptp.perout_channels[chan], config, 681172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 682172db5f9SMaciej Machnikowski pf->ptp.perout_channels[chan].start_time = phase; 683172db5f9SMaciej Machnikowski } 684172db5f9SMaciej Machnikowski 685172db5f9SMaciej Machnikowski return 0; 686172db5f9SMaciej Machnikowski err: 687172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "PTP failed to cfg per_clk\n"); 688172db5f9SMaciej Machnikowski return -EFAULT; 689172db5f9SMaciej Machnikowski } 690172db5f9SMaciej Machnikowski 691172db5f9SMaciej Machnikowski /** 692172db5f9SMaciej Machnikowski * ice_ptp_gpio_enable_e810 - Enable/disable ancillary features of PHC 693172db5f9SMaciej Machnikowski * @info: the driver's PTP info structure 694172db5f9SMaciej Machnikowski * @rq: The requested feature to change 695172db5f9SMaciej Machnikowski * @on: Enable/disable flag 696172db5f9SMaciej Machnikowski */ 697172db5f9SMaciej Machnikowski static int 698172db5f9SMaciej Machnikowski ice_ptp_gpio_enable_e810(struct ptp_clock_info *info, 699172db5f9SMaciej Machnikowski struct ptp_clock_request *rq, int on) 700172db5f9SMaciej Machnikowski { 701172db5f9SMaciej Machnikowski struct ice_pf *pf = ptp_info_to_pf(info); 702172db5f9SMaciej Machnikowski struct ice_perout_channel clk_cfg = {0}; 703172db5f9SMaciej Machnikowski unsigned int chan; 704172db5f9SMaciej Machnikowski u32 gpio_pin; 705172db5f9SMaciej Machnikowski int err; 706172db5f9SMaciej Machnikowski 707172db5f9SMaciej Machnikowski switch (rq->type) { 708172db5f9SMaciej Machnikowski case PTP_CLK_REQ_PEROUT: 709172db5f9SMaciej Machnikowski chan = rq->perout.index; 710172db5f9SMaciej Machnikowski if (chan == PPS_CLK_GEN_CHAN) 711172db5f9SMaciej Machnikowski clk_cfg.gpio_pin = PPS_PIN_INDEX; 712172db5f9SMaciej Machnikowski else 713172db5f9SMaciej Machnikowski clk_cfg.gpio_pin = chan; 714172db5f9SMaciej Machnikowski 715172db5f9SMaciej Machnikowski clk_cfg.period = ((rq->perout.period.sec * NSEC_PER_SEC) + 716172db5f9SMaciej Machnikowski rq->perout.period.nsec); 717172db5f9SMaciej Machnikowski clk_cfg.start_time = ((rq->perout.start.sec * NSEC_PER_SEC) + 718172db5f9SMaciej Machnikowski rq->perout.start.nsec); 719172db5f9SMaciej Machnikowski clk_cfg.ena = !!on; 720172db5f9SMaciej Machnikowski 721172db5f9SMaciej Machnikowski err = ice_ptp_cfg_clkout(pf, chan, &clk_cfg, true); 722172db5f9SMaciej Machnikowski break; 723172db5f9SMaciej Machnikowski case PTP_CLK_REQ_EXTTS: 724172db5f9SMaciej Machnikowski chan = rq->extts.index; 725172db5f9SMaciej Machnikowski gpio_pin = chan; 726172db5f9SMaciej Machnikowski 727172db5f9SMaciej Machnikowski err = ice_ptp_cfg_extts(pf, !!on, chan, gpio_pin, 728172db5f9SMaciej Machnikowski rq->extts.flags); 729172db5f9SMaciej Machnikowski break; 730172db5f9SMaciej Machnikowski default: 731172db5f9SMaciej Machnikowski return -EOPNOTSUPP; 732172db5f9SMaciej Machnikowski } 733172db5f9SMaciej Machnikowski 734172db5f9SMaciej Machnikowski return err; 735172db5f9SMaciej Machnikowski } 736172db5f9SMaciej Machnikowski 737172db5f9SMaciej Machnikowski /** 73806c16d89SJacob Keller * ice_ptp_gettimex64 - Get the time of the clock 73906c16d89SJacob Keller * @info: the driver's PTP info structure 74006c16d89SJacob Keller * @ts: timespec64 structure to hold the current time value 74106c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 74206c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 74306c16d89SJacob Keller * 74406c16d89SJacob Keller * Read the device clock and return the correct value on ns, after converting it 74506c16d89SJacob Keller * into a timespec struct. 74606c16d89SJacob Keller */ 74706c16d89SJacob Keller static int 74806c16d89SJacob Keller ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts, 74906c16d89SJacob Keller struct ptp_system_timestamp *sts) 75006c16d89SJacob Keller { 75106c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 75206c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 75306c16d89SJacob Keller 75406c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 75506c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n"); 75606c16d89SJacob Keller return -EBUSY; 75706c16d89SJacob Keller } 75806c16d89SJacob Keller 75906c16d89SJacob Keller ice_ptp_read_time(pf, ts, sts); 76006c16d89SJacob Keller ice_ptp_unlock(hw); 76106c16d89SJacob Keller 76206c16d89SJacob Keller return 0; 76306c16d89SJacob Keller } 76406c16d89SJacob Keller 76506c16d89SJacob Keller /** 76606c16d89SJacob Keller * ice_ptp_settime64 - Set the time of the clock 76706c16d89SJacob Keller * @info: the driver's PTP info structure 76806c16d89SJacob Keller * @ts: timespec64 structure that holds the new time value 76906c16d89SJacob Keller * 77006c16d89SJacob Keller * Set the device clock to the user input value. The conversion from timespec 77106c16d89SJacob Keller * to ns happens in the write function. 77206c16d89SJacob Keller */ 77306c16d89SJacob Keller static int 77406c16d89SJacob Keller ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts) 77506c16d89SJacob Keller { 77606c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 77706c16d89SJacob Keller struct timespec64 ts64 = *ts; 77806c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 77906c16d89SJacob Keller int err; 78006c16d89SJacob Keller 78106c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 78206c16d89SJacob Keller err = -EBUSY; 78306c16d89SJacob Keller goto exit; 78406c16d89SJacob Keller } 78506c16d89SJacob Keller 78606c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts64); 78706c16d89SJacob Keller ice_ptp_unlock(hw); 78806c16d89SJacob Keller 78977a78115SJacob Keller if (!err) 79077a78115SJacob Keller ice_ptp_update_cached_phctime(pf); 79177a78115SJacob Keller 79206c16d89SJacob Keller exit: 79306c16d89SJacob Keller if (err) { 79406c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err); 79506c16d89SJacob Keller return err; 79606c16d89SJacob Keller } 79706c16d89SJacob Keller 79806c16d89SJacob Keller return 0; 79906c16d89SJacob Keller } 80006c16d89SJacob Keller 80106c16d89SJacob Keller /** 80206c16d89SJacob Keller * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment 80306c16d89SJacob Keller * @info: the driver's PTP info structure 80406c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 80506c16d89SJacob Keller */ 80606c16d89SJacob Keller static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta) 80706c16d89SJacob Keller { 80806c16d89SJacob Keller struct timespec64 now, then; 80906c16d89SJacob Keller 81006c16d89SJacob Keller then = ns_to_timespec64(delta); 81106c16d89SJacob Keller ice_ptp_gettimex64(info, &now, NULL); 81206c16d89SJacob Keller now = timespec64_add(now, then); 81306c16d89SJacob Keller 81406c16d89SJacob Keller return ice_ptp_settime64(info, (const struct timespec64 *)&now); 81506c16d89SJacob Keller } 81606c16d89SJacob Keller 81706c16d89SJacob Keller /** 81806c16d89SJacob Keller * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta 81906c16d89SJacob Keller * @info: the driver's PTP info structure 82006c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 82106c16d89SJacob Keller */ 82206c16d89SJacob Keller static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta) 82306c16d89SJacob Keller { 82406c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 82506c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 82606c16d89SJacob Keller struct device *dev; 82706c16d89SJacob Keller int err; 82806c16d89SJacob Keller 82906c16d89SJacob Keller dev = ice_pf_to_dev(pf); 83006c16d89SJacob Keller 83106c16d89SJacob Keller /* Hardware only supports atomic adjustments using signed 32-bit 83206c16d89SJacob Keller * integers. For any adjustment outside this range, perform 83306c16d89SJacob Keller * a non-atomic get->adjust->set flow. 83406c16d89SJacob Keller */ 83506c16d89SJacob Keller if (delta > S32_MAX || delta < S32_MIN) { 83606c16d89SJacob Keller dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta); 83706c16d89SJacob Keller return ice_ptp_adjtime_nonatomic(info, delta); 83806c16d89SJacob Keller } 83906c16d89SJacob Keller 84006c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 84106c16d89SJacob Keller dev_err(dev, "PTP failed to acquire semaphore in adjtime\n"); 84206c16d89SJacob Keller return -EBUSY; 84306c16d89SJacob Keller } 84406c16d89SJacob Keller 84506c16d89SJacob Keller err = ice_ptp_write_adj(pf, delta); 84606c16d89SJacob Keller 84706c16d89SJacob Keller ice_ptp_unlock(hw); 84806c16d89SJacob Keller 84906c16d89SJacob Keller if (err) { 85006c16d89SJacob Keller dev_err(dev, "PTP failed to adjust time, err %d\n", err); 85106c16d89SJacob Keller return err; 85206c16d89SJacob Keller } 85306c16d89SJacob Keller 85477a78115SJacob Keller ice_ptp_update_cached_phctime(pf); 85577a78115SJacob Keller 85606c16d89SJacob Keller return 0; 85706c16d89SJacob Keller } 85806c16d89SJacob Keller 85906c16d89SJacob Keller /** 86077a78115SJacob Keller * ice_ptp_get_ts_config - ioctl interface to read the timestamping config 86177a78115SJacob Keller * @pf: Board private structure 86277a78115SJacob Keller * @ifr: ioctl data 86377a78115SJacob Keller * 86477a78115SJacob Keller * Copy the timestamping config to user buffer 86577a78115SJacob Keller */ 86677a78115SJacob Keller int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr) 86777a78115SJacob Keller { 86877a78115SJacob Keller struct hwtstamp_config *config; 86977a78115SJacob Keller 87077a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 87177a78115SJacob Keller return -EIO; 87277a78115SJacob Keller 87377a78115SJacob Keller config = &pf->ptp.tstamp_config; 87477a78115SJacob Keller 87577a78115SJacob Keller return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ? 87677a78115SJacob Keller -EFAULT : 0; 87777a78115SJacob Keller } 87877a78115SJacob Keller 87977a78115SJacob Keller /** 88077a78115SJacob Keller * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode 88177a78115SJacob Keller * @pf: Board private structure 88277a78115SJacob Keller * @config: hwtstamp settings requested or saved 88377a78115SJacob Keller */ 88477a78115SJacob Keller static int 88577a78115SJacob Keller ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config) 88677a78115SJacob Keller { 88777a78115SJacob Keller /* Reserved for future extensions. */ 88877a78115SJacob Keller if (config->flags) 88977a78115SJacob Keller return -EINVAL; 89077a78115SJacob Keller 89177a78115SJacob Keller switch (config->tx_type) { 89277a78115SJacob Keller case HWTSTAMP_TX_OFF: 893ea9b847cSJacob Keller ice_set_tx_tstamp(pf, false); 894ea9b847cSJacob Keller break; 895ea9b847cSJacob Keller case HWTSTAMP_TX_ON: 896ea9b847cSJacob Keller ice_set_tx_tstamp(pf, true); 89777a78115SJacob Keller break; 89877a78115SJacob Keller default: 89977a78115SJacob Keller return -ERANGE; 90077a78115SJacob Keller } 90177a78115SJacob Keller 90277a78115SJacob Keller switch (config->rx_filter) { 90377a78115SJacob Keller case HWTSTAMP_FILTER_NONE: 90477a78115SJacob Keller ice_set_rx_tstamp(pf, false); 90577a78115SJacob Keller break; 90677a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 90777a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 90877a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 90977a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_EVENT: 91077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 91177a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 91277a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_SYNC: 91377a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 91477a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 91577a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 91677a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 91777a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 91877a78115SJacob Keller case HWTSTAMP_FILTER_NTP_ALL: 91977a78115SJacob Keller case HWTSTAMP_FILTER_ALL: 92077a78115SJacob Keller config->rx_filter = HWTSTAMP_FILTER_ALL; 92177a78115SJacob Keller ice_set_rx_tstamp(pf, true); 92277a78115SJacob Keller break; 92377a78115SJacob Keller default: 92477a78115SJacob Keller return -ERANGE; 92577a78115SJacob Keller } 92677a78115SJacob Keller 92777a78115SJacob Keller return 0; 92877a78115SJacob Keller } 92977a78115SJacob Keller 93077a78115SJacob Keller /** 93177a78115SJacob Keller * ice_ptp_set_ts_config - ioctl interface to control the timestamping 93277a78115SJacob Keller * @pf: Board private structure 93377a78115SJacob Keller * @ifr: ioctl data 93477a78115SJacob Keller * 93577a78115SJacob Keller * Get the user config and store it 93677a78115SJacob Keller */ 93777a78115SJacob Keller int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr) 93877a78115SJacob Keller { 93977a78115SJacob Keller struct hwtstamp_config config; 94077a78115SJacob Keller int err; 94177a78115SJacob Keller 94277a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 94377a78115SJacob Keller return -EAGAIN; 94477a78115SJacob Keller 94577a78115SJacob Keller if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 94677a78115SJacob Keller return -EFAULT; 94777a78115SJacob Keller 94877a78115SJacob Keller err = ice_ptp_set_timestamp_mode(pf, &config); 94977a78115SJacob Keller if (err) 95077a78115SJacob Keller return err; 95177a78115SJacob Keller 95277a78115SJacob Keller /* Save these settings for future reference */ 95377a78115SJacob Keller pf->ptp.tstamp_config = config; 95477a78115SJacob Keller 95577a78115SJacob Keller return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 95677a78115SJacob Keller -EFAULT : 0; 95777a78115SJacob Keller } 95877a78115SJacob Keller 95977a78115SJacob Keller /** 96077a78115SJacob Keller * ice_ptp_rx_hwtstamp - Check for an Rx timestamp 96177a78115SJacob Keller * @rx_ring: Ring to get the VSI info 96277a78115SJacob Keller * @rx_desc: Receive descriptor 96377a78115SJacob Keller * @skb: Particular skb to send timestamp with 96477a78115SJacob Keller * 96577a78115SJacob Keller * The driver receives a notification in the receive descriptor with timestamp. 96677a78115SJacob Keller * The timestamp is in ns, so we must convert the result first. 96777a78115SJacob Keller */ 96877a78115SJacob Keller void 96977a78115SJacob Keller ice_ptp_rx_hwtstamp(struct ice_ring *rx_ring, 97077a78115SJacob Keller union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) 97177a78115SJacob Keller { 97277a78115SJacob Keller u32 ts_high; 97377a78115SJacob Keller u64 ts_ns; 97477a78115SJacob Keller 97577a78115SJacob Keller /* Populate timesync data into skb */ 97677a78115SJacob Keller if (rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID) { 97777a78115SJacob Keller struct skb_shared_hwtstamps *hwtstamps; 97877a78115SJacob Keller 97977a78115SJacob Keller /* Use ice_ptp_extend_32b_ts directly, using the ring-specific 98077a78115SJacob Keller * cached PHC value, rather than accessing the PF. This also 98177a78115SJacob Keller * allows us to simply pass the upper 32bits of nanoseconds 98277a78115SJacob Keller * directly. Calling ice_ptp_extend_40b_ts is unnecessary as 98377a78115SJacob Keller * it would just discard these bits itself. 98477a78115SJacob Keller */ 98577a78115SJacob Keller ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high); 98677a78115SJacob Keller ts_ns = ice_ptp_extend_32b_ts(rx_ring->cached_phctime, ts_high); 98777a78115SJacob Keller 98877a78115SJacob Keller hwtstamps = skb_hwtstamps(skb); 98977a78115SJacob Keller memset(hwtstamps, 0, sizeof(*hwtstamps)); 99077a78115SJacob Keller hwtstamps->hwtstamp = ns_to_ktime(ts_ns); 99177a78115SJacob Keller } 99277a78115SJacob Keller } 99377a78115SJacob Keller 99477a78115SJacob Keller /** 995172db5f9SMaciej Machnikowski * ice_ptp_setup_pins_e810 - Setup PTP pins in sysfs 996172db5f9SMaciej Machnikowski * @info: PTP clock capabilities 997172db5f9SMaciej Machnikowski */ 998172db5f9SMaciej Machnikowski static void ice_ptp_setup_pins_e810(struct ptp_clock_info *info) 999172db5f9SMaciej Machnikowski { 1000172db5f9SMaciej Machnikowski info->n_per_out = E810_N_PER_OUT; 1001172db5f9SMaciej Machnikowski info->n_ext_ts = E810_N_EXT_TS; 1002172db5f9SMaciej Machnikowski } 1003172db5f9SMaciej Machnikowski 1004172db5f9SMaciej Machnikowski /** 1005172db5f9SMaciej Machnikowski * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support 1006172db5f9SMaciej Machnikowski * @pf: Board private structure 1007172db5f9SMaciej Machnikowski * @info: PTP info to fill 1008172db5f9SMaciej Machnikowski * 1009172db5f9SMaciej Machnikowski * Assign functions to the PTP capabiltiies structure for E810 devices. 1010172db5f9SMaciej Machnikowski * Functions which operate across all device families should be set directly 1011172db5f9SMaciej Machnikowski * in ice_ptp_set_caps. Only add functions here which are distinct for e810 1012172db5f9SMaciej Machnikowski * devices. 1013172db5f9SMaciej Machnikowski */ 1014172db5f9SMaciej Machnikowski static void 1015172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(struct ice_pf *pf, struct ptp_clock_info *info) 1016172db5f9SMaciej Machnikowski { 1017172db5f9SMaciej Machnikowski info->enable = ice_ptp_gpio_enable_e810; 1018172db5f9SMaciej Machnikowski 1019172db5f9SMaciej Machnikowski ice_ptp_setup_pins_e810(info); 1020172db5f9SMaciej Machnikowski } 1021172db5f9SMaciej Machnikowski 1022172db5f9SMaciej Machnikowski /** 102306c16d89SJacob Keller * ice_ptp_set_caps - Set PTP capabilities 102406c16d89SJacob Keller * @pf: Board private structure 102506c16d89SJacob Keller */ 102606c16d89SJacob Keller static void ice_ptp_set_caps(struct ice_pf *pf) 102706c16d89SJacob Keller { 102806c16d89SJacob Keller struct ptp_clock_info *info = &pf->ptp.info; 102906c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf); 103006c16d89SJacob Keller 103106c16d89SJacob Keller snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk", 103206c16d89SJacob Keller dev_driver_string(dev), dev_name(dev)); 103306c16d89SJacob Keller info->owner = THIS_MODULE; 103406c16d89SJacob Keller info->max_adj = 999999999; 103506c16d89SJacob Keller info->adjtime = ice_ptp_adjtime; 103606c16d89SJacob Keller info->adjfine = ice_ptp_adjfine; 103706c16d89SJacob Keller info->gettimex64 = ice_ptp_gettimex64; 103806c16d89SJacob Keller info->settime64 = ice_ptp_settime64; 1039172db5f9SMaciej Machnikowski 1040172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(pf, info); 104106c16d89SJacob Keller } 104206c16d89SJacob Keller 104306c16d89SJacob Keller /** 104406c16d89SJacob Keller * ice_ptp_create_clock - Create PTP clock device for userspace 104506c16d89SJacob Keller * @pf: Board private structure 104606c16d89SJacob Keller * 104706c16d89SJacob Keller * This function creates a new PTP clock device. It only creates one if we 104806c16d89SJacob Keller * don't already have one. Will return error if it can't create one, but success 104906c16d89SJacob Keller * if we already have a device. Should be used by ice_ptp_init to create clock 105006c16d89SJacob Keller * initially, and prevent global resets from creating new clock devices. 105106c16d89SJacob Keller */ 105206c16d89SJacob Keller static long ice_ptp_create_clock(struct ice_pf *pf) 105306c16d89SJacob Keller { 105406c16d89SJacob Keller struct ptp_clock_info *info; 105506c16d89SJacob Keller struct ptp_clock *clock; 105606c16d89SJacob Keller struct device *dev; 105706c16d89SJacob Keller 105806c16d89SJacob Keller /* No need to create a clock device if we already have one */ 105906c16d89SJacob Keller if (pf->ptp.clock) 106006c16d89SJacob Keller return 0; 106106c16d89SJacob Keller 106206c16d89SJacob Keller ice_ptp_set_caps(pf); 106306c16d89SJacob Keller 106406c16d89SJacob Keller info = &pf->ptp.info; 106506c16d89SJacob Keller dev = ice_pf_to_dev(pf); 106606c16d89SJacob Keller 106706c16d89SJacob Keller /* Attempt to register the clock before enabling the hardware. */ 106806c16d89SJacob Keller clock = ptp_clock_register(info, dev); 106906c16d89SJacob Keller if (IS_ERR(clock)) 107006c16d89SJacob Keller return PTR_ERR(clock); 107106c16d89SJacob Keller 107206c16d89SJacob Keller pf->ptp.clock = clock; 107306c16d89SJacob Keller 107406c16d89SJacob Keller return 0; 107506c16d89SJacob Keller } 107606c16d89SJacob Keller 1077ea9b847cSJacob Keller /** 1078ea9b847cSJacob Keller * ice_ptp_tx_tstamp_work - Process Tx timestamps for a port 1079ea9b847cSJacob Keller * @work: pointer to the kthread_work struct 1080ea9b847cSJacob Keller * 1081ea9b847cSJacob Keller * Process timestamps captured by the PHY associated with this port. To do 1082ea9b847cSJacob Keller * this, loop over each index with a waiting skb. 1083ea9b847cSJacob Keller * 1084ea9b847cSJacob Keller * If a given index has a valid timestamp, perform the following steps: 1085ea9b847cSJacob Keller * 1086ea9b847cSJacob Keller * 1) copy the timestamp out of the PHY register 1087ea9b847cSJacob Keller * 4) clear the timestamp valid bit in the PHY register 1088ea9b847cSJacob Keller * 5) unlock the index by clearing the associated in_use bit. 1089ea9b847cSJacob Keller * 2) extend the 40b timestamp value to get a 64bit timestamp 1090ea9b847cSJacob Keller * 3) send that timestamp to the stack 1091ea9b847cSJacob Keller * 1092ea9b847cSJacob Keller * After looping, if we still have waiting SKBs, then re-queue the work. This 1093ea9b847cSJacob Keller * may cause us effectively poll even when not strictly necessary. We do this 1094ea9b847cSJacob Keller * because it's possible a new timestamp was requested around the same time as 1095ea9b847cSJacob Keller * the interrupt. In some cases hardware might not interrupt us again when the 1096ea9b847cSJacob Keller * timestamp is captured. 1097ea9b847cSJacob Keller * 1098ea9b847cSJacob Keller * Note that we only take the tracking lock when clearing the bit and when 1099ea9b847cSJacob Keller * checking if we need to re-queue this task. The only place where bits can be 1100ea9b847cSJacob Keller * set is the hard xmit routine where an SKB has a request flag set. The only 1101ea9b847cSJacob Keller * places where we clear bits are this work function, or the periodic cleanup 1102ea9b847cSJacob Keller * thread. If the cleanup thread clears a bit we're processing we catch it 1103ea9b847cSJacob Keller * when we lock to clear the bit and then grab the SKB pointer. If a Tx thread 1104ea9b847cSJacob Keller * starts a new timestamp, we might not begin processing it right away but we 1105ea9b847cSJacob Keller * will notice it at the end when we re-queue the work item. If a Tx thread 1106ea9b847cSJacob Keller * starts a new timestamp just after this function exits without re-queuing, 1107ea9b847cSJacob Keller * the interrupt when the timestamp finishes should trigger. Avoiding holding 1108ea9b847cSJacob Keller * the lock for the entire function is important in order to ensure that Tx 1109ea9b847cSJacob Keller * threads do not get blocked while waiting for the lock. 1110ea9b847cSJacob Keller */ 1111ea9b847cSJacob Keller static void ice_ptp_tx_tstamp_work(struct kthread_work *work) 1112ea9b847cSJacob Keller { 1113ea9b847cSJacob Keller struct ice_ptp_port *ptp_port; 1114ea9b847cSJacob Keller struct ice_ptp_tx *tx; 1115ea9b847cSJacob Keller struct ice_pf *pf; 1116ea9b847cSJacob Keller struct ice_hw *hw; 1117ea9b847cSJacob Keller u8 idx; 1118ea9b847cSJacob Keller 1119ea9b847cSJacob Keller tx = container_of(work, struct ice_ptp_tx, work); 1120ea9b847cSJacob Keller if (!tx->init) 1121ea9b847cSJacob Keller return; 1122ea9b847cSJacob Keller 1123ea9b847cSJacob Keller ptp_port = container_of(tx, struct ice_ptp_port, tx); 1124ea9b847cSJacob Keller pf = ptp_port_to_pf(ptp_port); 1125ea9b847cSJacob Keller hw = &pf->hw; 1126ea9b847cSJacob Keller 1127ea9b847cSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 1128ea9b847cSJacob Keller struct skb_shared_hwtstamps shhwtstamps = {}; 1129ea9b847cSJacob Keller u8 phy_idx = idx + tx->quad_offset; 1130ea9b847cSJacob Keller u64 raw_tstamp, tstamp; 1131ea9b847cSJacob Keller struct sk_buff *skb; 1132ea9b847cSJacob Keller int err; 1133ea9b847cSJacob Keller 1134ea9b847cSJacob Keller err = ice_read_phy_tstamp(hw, tx->quad, phy_idx, 1135ea9b847cSJacob Keller &raw_tstamp); 1136ea9b847cSJacob Keller if (err) 1137ea9b847cSJacob Keller continue; 1138ea9b847cSJacob Keller 1139ea9b847cSJacob Keller /* Check if the timestamp is valid */ 1140ea9b847cSJacob Keller if (!(raw_tstamp & ICE_PTP_TS_VALID)) 1141ea9b847cSJacob Keller continue; 1142ea9b847cSJacob Keller 1143ea9b847cSJacob Keller /* clear the timestamp register, so that it won't show valid 1144ea9b847cSJacob Keller * again when re-used. 1145ea9b847cSJacob Keller */ 1146ea9b847cSJacob Keller ice_clear_phy_tstamp(hw, tx->quad, phy_idx); 1147ea9b847cSJacob Keller 1148ea9b847cSJacob Keller /* The timestamp is valid, so we'll go ahead and clear this 1149ea9b847cSJacob Keller * index and then send the timestamp up to the stack. 1150ea9b847cSJacob Keller */ 1151ea9b847cSJacob Keller spin_lock(&tx->lock); 1152ea9b847cSJacob Keller clear_bit(idx, tx->in_use); 1153ea9b847cSJacob Keller skb = tx->tstamps[idx].skb; 1154ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 1155ea9b847cSJacob Keller spin_unlock(&tx->lock); 1156ea9b847cSJacob Keller 1157ea9b847cSJacob Keller /* it's (unlikely but) possible we raced with the cleanup 1158ea9b847cSJacob Keller * thread for discarding old timestamp requests. 1159ea9b847cSJacob Keller */ 1160ea9b847cSJacob Keller if (!skb) 1161ea9b847cSJacob Keller continue; 1162ea9b847cSJacob Keller 1163ea9b847cSJacob Keller /* Extend the timestamp using cached PHC time */ 1164ea9b847cSJacob Keller tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp); 1165ea9b847cSJacob Keller shhwtstamps.hwtstamp = ns_to_ktime(tstamp); 1166ea9b847cSJacob Keller 1167ea9b847cSJacob Keller skb_tstamp_tx(skb, &shhwtstamps); 1168ea9b847cSJacob Keller dev_kfree_skb_any(skb); 1169ea9b847cSJacob Keller } 1170ea9b847cSJacob Keller 1171ea9b847cSJacob Keller /* Check if we still have work to do. If so, re-queue this task to 1172ea9b847cSJacob Keller * poll for remaining timestamps. 1173ea9b847cSJacob Keller */ 1174ea9b847cSJacob Keller spin_lock(&tx->lock); 1175ea9b847cSJacob Keller if (!bitmap_empty(tx->in_use, tx->len)) 1176ea9b847cSJacob Keller kthread_queue_work(pf->ptp.kworker, &tx->work); 1177ea9b847cSJacob Keller spin_unlock(&tx->lock); 1178ea9b847cSJacob Keller } 1179ea9b847cSJacob Keller 1180ea9b847cSJacob Keller /** 1181ea9b847cSJacob Keller * ice_ptp_request_ts - Request an available Tx timestamp index 1182ea9b847cSJacob Keller * @tx: the PTP Tx timestamp tracker to request from 1183ea9b847cSJacob Keller * @skb: the SKB to associate with this timestamp request 1184ea9b847cSJacob Keller */ 1185ea9b847cSJacob Keller s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb) 1186ea9b847cSJacob Keller { 1187ea9b847cSJacob Keller u8 idx; 1188ea9b847cSJacob Keller 1189ea9b847cSJacob Keller /* Check if this tracker is initialized */ 1190ea9b847cSJacob Keller if (!tx->init) 1191ea9b847cSJacob Keller return -1; 1192ea9b847cSJacob Keller 1193ea9b847cSJacob Keller spin_lock(&tx->lock); 1194ea9b847cSJacob Keller /* Find and set the first available index */ 1195ea9b847cSJacob Keller idx = find_first_zero_bit(tx->in_use, tx->len); 1196ea9b847cSJacob Keller if (idx < tx->len) { 1197ea9b847cSJacob Keller /* We got a valid index that no other thread could have set. Store 1198ea9b847cSJacob Keller * a reference to the skb and the start time to allow discarding old 1199ea9b847cSJacob Keller * requests. 1200ea9b847cSJacob Keller */ 1201ea9b847cSJacob Keller set_bit(idx, tx->in_use); 1202ea9b847cSJacob Keller tx->tstamps[idx].start = jiffies; 1203ea9b847cSJacob Keller tx->tstamps[idx].skb = skb_get(skb); 1204ea9b847cSJacob Keller skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 1205ea9b847cSJacob Keller } 1206ea9b847cSJacob Keller 1207ea9b847cSJacob Keller spin_unlock(&tx->lock); 1208ea9b847cSJacob Keller 1209ea9b847cSJacob Keller /* return the appropriate PHY timestamp register index, -1 if no 1210ea9b847cSJacob Keller * indexes were available. 1211ea9b847cSJacob Keller */ 1212ea9b847cSJacob Keller if (idx >= tx->len) 1213ea9b847cSJacob Keller return -1; 1214ea9b847cSJacob Keller else 1215ea9b847cSJacob Keller return idx + tx->quad_offset; 1216ea9b847cSJacob Keller } 1217ea9b847cSJacob Keller 1218ea9b847cSJacob Keller /** 1219ea9b847cSJacob Keller * ice_ptp_process_ts - Spawn kthread work to handle timestamps 1220ea9b847cSJacob Keller * @pf: Board private structure 1221ea9b847cSJacob Keller * 1222ea9b847cSJacob Keller * Queue work required to process the PTP Tx timestamps outside of interrupt 1223ea9b847cSJacob Keller * context. 1224ea9b847cSJacob Keller */ 1225ea9b847cSJacob Keller void ice_ptp_process_ts(struct ice_pf *pf) 1226ea9b847cSJacob Keller { 1227ea9b847cSJacob Keller if (pf->ptp.port.tx.init) 1228ea9b847cSJacob Keller kthread_queue_work(pf->ptp.kworker, &pf->ptp.port.tx.work); 1229ea9b847cSJacob Keller } 1230ea9b847cSJacob Keller 1231ea9b847cSJacob Keller /** 1232ea9b847cSJacob Keller * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps 1233ea9b847cSJacob Keller * @tx: Tx tracking structure to initialize 1234ea9b847cSJacob Keller * 1235ea9b847cSJacob Keller * Assumes that the length has already been initialized. Do not call directly, 1236ea9b847cSJacob Keller * use the ice_ptp_init_tx_e822 or ice_ptp_init_tx_e810 instead. 1237ea9b847cSJacob Keller */ 1238ea9b847cSJacob Keller static int 1239ea9b847cSJacob Keller ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx) 1240ea9b847cSJacob Keller { 1241ea9b847cSJacob Keller tx->tstamps = kcalloc(tx->len, sizeof(*tx->tstamps), GFP_KERNEL); 1242ea9b847cSJacob Keller if (!tx->tstamps) 1243ea9b847cSJacob Keller return -ENOMEM; 1244ea9b847cSJacob Keller 1245ea9b847cSJacob Keller tx->in_use = bitmap_zalloc(tx->len, GFP_KERNEL); 1246ea9b847cSJacob Keller if (!tx->in_use) { 1247ea9b847cSJacob Keller kfree(tx->tstamps); 1248ea9b847cSJacob Keller tx->tstamps = NULL; 1249ea9b847cSJacob Keller return -ENOMEM; 1250ea9b847cSJacob Keller } 1251ea9b847cSJacob Keller 1252ea9b847cSJacob Keller spin_lock_init(&tx->lock); 1253ea9b847cSJacob Keller kthread_init_work(&tx->work, ice_ptp_tx_tstamp_work); 1254ea9b847cSJacob Keller 1255ea9b847cSJacob Keller tx->init = 1; 1256ea9b847cSJacob Keller 1257ea9b847cSJacob Keller return 0; 1258ea9b847cSJacob Keller } 1259ea9b847cSJacob Keller 1260ea9b847cSJacob Keller /** 1261ea9b847cSJacob Keller * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker 1262ea9b847cSJacob Keller * @pf: Board private structure 1263ea9b847cSJacob Keller * @tx: the tracker to flush 1264ea9b847cSJacob Keller */ 1265ea9b847cSJacob Keller static void 1266ea9b847cSJacob Keller ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 1267ea9b847cSJacob Keller { 1268ea9b847cSJacob Keller u8 idx; 1269ea9b847cSJacob Keller 1270*4dd0d5c3SJacob Keller spin_lock(&tx->lock); 1271*4dd0d5c3SJacob Keller 1272ea9b847cSJacob Keller for (idx = 0; idx < tx->len; idx++) { 1273ea9b847cSJacob Keller u8 phy_idx = idx + tx->quad_offset; 1274ea9b847cSJacob Keller 1275ea9b847cSJacob Keller /* Clear any potential residual timestamp in the PHY block */ 1276ea9b847cSJacob Keller if (!pf->hw.reset_ongoing) 1277ea9b847cSJacob Keller ice_clear_phy_tstamp(&pf->hw, tx->quad, phy_idx); 1278ea9b847cSJacob Keller 1279ea9b847cSJacob Keller if (tx->tstamps[idx].skb) { 1280ea9b847cSJacob Keller dev_kfree_skb_any(tx->tstamps[idx].skb); 1281ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 1282ea9b847cSJacob Keller } 1283ea9b847cSJacob Keller } 1284*4dd0d5c3SJacob Keller 1285*4dd0d5c3SJacob Keller spin_unlock(&tx->lock); 1286ea9b847cSJacob Keller } 1287ea9b847cSJacob Keller 1288ea9b847cSJacob Keller /** 1289ea9b847cSJacob Keller * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker 1290ea9b847cSJacob Keller * @pf: Board private structure 1291ea9b847cSJacob Keller * @tx: Tx tracking structure to release 1292ea9b847cSJacob Keller * 1293ea9b847cSJacob Keller * Free memory associated with the Tx timestamp tracker. 1294ea9b847cSJacob Keller */ 1295ea9b847cSJacob Keller static void 1296ea9b847cSJacob Keller ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 1297ea9b847cSJacob Keller { 1298ea9b847cSJacob Keller tx->init = 0; 1299ea9b847cSJacob Keller 1300ea9b847cSJacob Keller kthread_cancel_work_sync(&tx->work); 1301ea9b847cSJacob Keller 1302ea9b847cSJacob Keller ice_ptp_flush_tx_tracker(pf, tx); 1303ea9b847cSJacob Keller 1304ea9b847cSJacob Keller kfree(tx->tstamps); 1305ea9b847cSJacob Keller tx->tstamps = NULL; 1306ea9b847cSJacob Keller 1307ea9b847cSJacob Keller kfree(tx->in_use); 1308ea9b847cSJacob Keller tx->in_use = NULL; 1309ea9b847cSJacob Keller 1310ea9b847cSJacob Keller tx->len = 0; 1311ea9b847cSJacob Keller } 1312ea9b847cSJacob Keller 1313ea9b847cSJacob Keller /** 1314ea9b847cSJacob Keller * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps 1315ea9b847cSJacob Keller * @pf: Board private structure 1316ea9b847cSJacob Keller * @tx: the Tx tracking structure to initialize 1317ea9b847cSJacob Keller * 1318ea9b847cSJacob Keller * Initialize the Tx timestamp tracker for this PF. For E810 devices, each 1319ea9b847cSJacob Keller * port has its own block of timestamps, independent of the other ports. 1320ea9b847cSJacob Keller */ 1321ea9b847cSJacob Keller static int 1322ea9b847cSJacob Keller ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx) 1323ea9b847cSJacob Keller { 1324ea9b847cSJacob Keller tx->quad = pf->hw.port_info->lport; 1325ea9b847cSJacob Keller tx->quad_offset = 0; 1326ea9b847cSJacob Keller tx->len = INDEX_PER_QUAD; 1327ea9b847cSJacob Keller 1328ea9b847cSJacob Keller return ice_ptp_alloc_tx_tracker(tx); 1329ea9b847cSJacob Keller } 1330ea9b847cSJacob Keller 1331ea9b847cSJacob Keller /** 1332ea9b847cSJacob Keller * ice_ptp_tx_tstamp_cleanup - Cleanup old timestamp requests that got dropped 1333ea9b847cSJacob Keller * @tx: PTP Tx tracker to clean up 1334ea9b847cSJacob Keller * 1335ea9b847cSJacob Keller * Loop through the Tx timestamp requests and see if any of them have been 1336ea9b847cSJacob Keller * waiting for a long time. Discard any SKBs that have been waiting for more 1337ea9b847cSJacob Keller * than 2 seconds. This is long enough to be reasonably sure that the 1338ea9b847cSJacob Keller * timestamp will never be captured. This might happen if the packet gets 1339ea9b847cSJacob Keller * discarded before it reaches the PHY timestamping block. 1340ea9b847cSJacob Keller */ 1341ea9b847cSJacob Keller static void ice_ptp_tx_tstamp_cleanup(struct ice_ptp_tx *tx) 1342ea9b847cSJacob Keller { 1343ea9b847cSJacob Keller u8 idx; 1344ea9b847cSJacob Keller 1345ea9b847cSJacob Keller if (!tx->init) 1346ea9b847cSJacob Keller return; 1347ea9b847cSJacob Keller 1348ea9b847cSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 1349ea9b847cSJacob Keller struct sk_buff *skb; 1350ea9b847cSJacob Keller 1351ea9b847cSJacob Keller /* Check if this SKB has been waiting for too long */ 1352ea9b847cSJacob Keller if (time_is_after_jiffies(tx->tstamps[idx].start + 2 * HZ)) 1353ea9b847cSJacob Keller continue; 1354ea9b847cSJacob Keller 1355ea9b847cSJacob Keller spin_lock(&tx->lock); 1356ea9b847cSJacob Keller skb = tx->tstamps[idx].skb; 1357ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 1358ea9b847cSJacob Keller clear_bit(idx, tx->in_use); 1359ea9b847cSJacob Keller spin_unlock(&tx->lock); 1360ea9b847cSJacob Keller 1361ea9b847cSJacob Keller /* Free the SKB after we've cleared the bit */ 1362ea9b847cSJacob Keller dev_kfree_skb_any(skb); 1363ea9b847cSJacob Keller } 1364ea9b847cSJacob Keller } 1365ea9b847cSJacob Keller 136677a78115SJacob Keller static void ice_ptp_periodic_work(struct kthread_work *work) 136777a78115SJacob Keller { 136877a78115SJacob Keller struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work); 136977a78115SJacob Keller struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 137077a78115SJacob Keller 137177a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 137277a78115SJacob Keller return; 137377a78115SJacob Keller 137477a78115SJacob Keller ice_ptp_update_cached_phctime(pf); 137577a78115SJacob Keller 1376ea9b847cSJacob Keller ice_ptp_tx_tstamp_cleanup(&pf->ptp.port.tx); 1377ea9b847cSJacob Keller 137877a78115SJacob Keller /* Run twice a second */ 137977a78115SJacob Keller kthread_queue_delayed_work(ptp->kworker, &ptp->work, 138077a78115SJacob Keller msecs_to_jiffies(500)); 138177a78115SJacob Keller } 138277a78115SJacob Keller 138306c16d89SJacob Keller /** 138406c16d89SJacob Keller * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device 138506c16d89SJacob Keller * @pf: Board private structure 138606c16d89SJacob Keller * 138706c16d89SJacob Keller * Setup and initialize a PTP clock device that represents the device hardware 138806c16d89SJacob Keller * clock. Save the clock index for other functions connected to the same 138906c16d89SJacob Keller * hardware resource. 139006c16d89SJacob Keller */ 139106c16d89SJacob Keller static int ice_ptp_init_owner(struct ice_pf *pf) 139206c16d89SJacob Keller { 139306c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf); 139406c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 139506c16d89SJacob Keller struct timespec64 ts; 139606c16d89SJacob Keller u8 src_idx; 139706c16d89SJacob Keller int err; 139806c16d89SJacob Keller 139906c16d89SJacob Keller wr32(hw, GLTSYN_SYNC_DLAY, 0); 140006c16d89SJacob Keller 140106c16d89SJacob Keller /* Clear some HW residue and enable source clock */ 140206c16d89SJacob Keller src_idx = hw->func_caps.ts_func_info.tmr_index_owned; 140306c16d89SJacob Keller 140406c16d89SJacob Keller /* Enable source clocks */ 140506c16d89SJacob Keller wr32(hw, GLTSYN_ENA(src_idx), GLTSYN_ENA_TSYN_ENA_M); 140606c16d89SJacob Keller 140706c16d89SJacob Keller /* Enable PHY time sync */ 140806c16d89SJacob Keller err = ice_ptp_init_phy_e810(hw); 140906c16d89SJacob Keller if (err) 141006c16d89SJacob Keller goto err_exit; 141106c16d89SJacob Keller 141206c16d89SJacob Keller /* Clear event status indications for auxiliary pins */ 141306c16d89SJacob Keller (void)rd32(hw, GLTSYN_STAT(src_idx)); 141406c16d89SJacob Keller 141506c16d89SJacob Keller /* Acquire the global hardware lock */ 141606c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 141706c16d89SJacob Keller err = -EBUSY; 141806c16d89SJacob Keller goto err_exit; 141906c16d89SJacob Keller } 142006c16d89SJacob Keller 142106c16d89SJacob Keller /* Write the increment time value to PHY and LAN */ 142206c16d89SJacob Keller err = ice_ptp_write_incval(hw, ICE_PTP_NOMINAL_INCVAL_E810); 142306c16d89SJacob Keller if (err) { 142406c16d89SJacob Keller ice_ptp_unlock(hw); 142506c16d89SJacob Keller goto err_exit; 142606c16d89SJacob Keller } 142706c16d89SJacob Keller 142806c16d89SJacob Keller ts = ktime_to_timespec64(ktime_get_real()); 142906c16d89SJacob Keller /* Write the initial Time value to PHY and LAN */ 143006c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts); 143106c16d89SJacob Keller if (err) { 143206c16d89SJacob Keller ice_ptp_unlock(hw); 143306c16d89SJacob Keller goto err_exit; 143406c16d89SJacob Keller } 143506c16d89SJacob Keller 143606c16d89SJacob Keller /* Release the global hardware lock */ 143706c16d89SJacob Keller ice_ptp_unlock(hw); 143806c16d89SJacob Keller 143906c16d89SJacob Keller /* Ensure we have a clock device */ 144006c16d89SJacob Keller err = ice_ptp_create_clock(pf); 144106c16d89SJacob Keller if (err) 144206c16d89SJacob Keller goto err_clk; 144306c16d89SJacob Keller 144467569a7fSJacob Keller /* Store the PTP clock index for other PFs */ 144567569a7fSJacob Keller ice_set_ptp_clock_index(pf); 144667569a7fSJacob Keller 144706c16d89SJacob Keller return 0; 144806c16d89SJacob Keller 144906c16d89SJacob Keller err_clk: 145006c16d89SJacob Keller pf->ptp.clock = NULL; 145106c16d89SJacob Keller err_exit: 145206c16d89SJacob Keller dev_err(dev, "PTP failed to register clock, err %d\n", err); 145306c16d89SJacob Keller 145406c16d89SJacob Keller return err; 145506c16d89SJacob Keller } 145606c16d89SJacob Keller 145706c16d89SJacob Keller /** 145806c16d89SJacob Keller * ice_ptp_init - Initialize the PTP support after device probe or reset 145906c16d89SJacob Keller * @pf: Board private structure 146006c16d89SJacob Keller * 146106c16d89SJacob Keller * This function sets device up for PTP support. The first time it is run, it 146206c16d89SJacob Keller * will create a clock device. It does not create a clock device if one 146306c16d89SJacob Keller * already exists. It also reconfigures the device after a reset. 146406c16d89SJacob Keller */ 146506c16d89SJacob Keller void ice_ptp_init(struct ice_pf *pf) 146606c16d89SJacob Keller { 146706c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf); 146877a78115SJacob Keller struct kthread_worker *kworker; 146906c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 147006c16d89SJacob Keller int err; 147106c16d89SJacob Keller 147206c16d89SJacob Keller /* PTP is currently only supported on E810 devices */ 147306c16d89SJacob Keller if (!ice_is_e810(hw)) 147406c16d89SJacob Keller return; 147506c16d89SJacob Keller 147606c16d89SJacob Keller /* Check if this PF owns the source timer */ 147706c16d89SJacob Keller if (hw->func_caps.ts_func_info.src_tmr_owned) { 147806c16d89SJacob Keller err = ice_ptp_init_owner(pf); 147906c16d89SJacob Keller if (err) 148006c16d89SJacob Keller return; 148106c16d89SJacob Keller } 148206c16d89SJacob Keller 148377a78115SJacob Keller /* Disable timestamping for both Tx and Rx */ 148477a78115SJacob Keller ice_ptp_cfg_timestamp(pf, false); 148577a78115SJacob Keller 1486ea9b847cSJacob Keller /* Initialize the PTP port Tx timestamp tracker */ 1487ea9b847cSJacob Keller ice_ptp_init_tx_e810(pf, &pf->ptp.port.tx); 1488ea9b847cSJacob Keller 148977a78115SJacob Keller /* Initialize work functions */ 149077a78115SJacob Keller kthread_init_delayed_work(&pf->ptp.work, ice_ptp_periodic_work); 1491172db5f9SMaciej Machnikowski kthread_init_work(&pf->ptp.extts_work, ice_ptp_extts_work); 149277a78115SJacob Keller 149377a78115SJacob Keller /* Allocate a kworker for handling work required for the ports 149477a78115SJacob Keller * connected to the PTP hardware clock. 149577a78115SJacob Keller */ 149677a78115SJacob Keller kworker = kthread_create_worker(0, "ice-ptp-%s", dev_name(dev)); 149777a78115SJacob Keller if (IS_ERR(kworker)) { 149877a78115SJacob Keller err = PTR_ERR(kworker); 149977a78115SJacob Keller goto err_kworker; 150077a78115SJacob Keller } 150177a78115SJacob Keller pf->ptp.kworker = kworker; 150277a78115SJacob Keller 150306c16d89SJacob Keller set_bit(ICE_FLAG_PTP, pf->flags); 150406c16d89SJacob Keller 150577a78115SJacob Keller /* Start periodic work going */ 150677a78115SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work, 0); 150777a78115SJacob Keller 150806c16d89SJacob Keller dev_info(dev, "PTP init successful\n"); 150977a78115SJacob Keller return; 151077a78115SJacob Keller 151177a78115SJacob Keller err_kworker: 151277a78115SJacob Keller /* If we registered a PTP clock, release it */ 151377a78115SJacob Keller if (pf->ptp.clock) { 151477a78115SJacob Keller ptp_clock_unregister(pf->ptp.clock); 151577a78115SJacob Keller pf->ptp.clock = NULL; 151677a78115SJacob Keller } 151777a78115SJacob Keller dev_err(dev, "PTP failed %d\n", err); 151806c16d89SJacob Keller } 151906c16d89SJacob Keller 152006c16d89SJacob Keller /** 152106c16d89SJacob Keller * ice_ptp_release - Disable the driver/HW support and unregister the clock 152206c16d89SJacob Keller * @pf: Board private structure 152306c16d89SJacob Keller * 152406c16d89SJacob Keller * This function handles the cleanup work required from the initialization by 152506c16d89SJacob Keller * clearing out the important information and unregistering the clock 152606c16d89SJacob Keller */ 152706c16d89SJacob Keller void ice_ptp_release(struct ice_pf *pf) 152806c16d89SJacob Keller { 152977a78115SJacob Keller /* Disable timestamping for both Tx and Rx */ 153077a78115SJacob Keller ice_ptp_cfg_timestamp(pf, false); 153177a78115SJacob Keller 1532ea9b847cSJacob Keller ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 1533ea9b847cSJacob Keller 153406c16d89SJacob Keller clear_bit(ICE_FLAG_PTP, pf->flags); 153506c16d89SJacob Keller 153677a78115SJacob Keller kthread_cancel_delayed_work_sync(&pf->ptp.work); 153777a78115SJacob Keller 153877a78115SJacob Keller if (pf->ptp.kworker) { 153977a78115SJacob Keller kthread_destroy_worker(pf->ptp.kworker); 154077a78115SJacob Keller pf->ptp.kworker = NULL; 154177a78115SJacob Keller } 154277a78115SJacob Keller 154306c16d89SJacob Keller if (!pf->ptp.clock) 154406c16d89SJacob Keller return; 154506c16d89SJacob Keller 154667569a7fSJacob Keller ice_clear_ptp_clock_index(pf); 154706c16d89SJacob Keller ptp_clock_unregister(pf->ptp.clock); 154806c16d89SJacob Keller pf->ptp.clock = NULL; 154906c16d89SJacob Keller 155006c16d89SJacob Keller dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n"); 155106c16d89SJacob Keller } 1552