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" 64c120218SJacob Keller #include "ice_trace.h" 706c16d89SJacob Keller 8172db5f9SMaciej Machnikowski #define E810_OUT_PROP_DELAY_NS 1 9172db5f9SMaciej Machnikowski 103a749623SJacob Keller #define UNKNOWN_INCVAL_E822 0x100000000ULL 113a749623SJacob Keller 12325b2064SMaciej Machnikowski static const struct ptp_pin_desc ice_pin_desc_e810t[] = { 13325b2064SMaciej Machnikowski /* name idx func chan */ 14325b2064SMaciej Machnikowski { "GNSS", GNSS, PTP_PF_EXTTS, 0, { 0, } }, 15325b2064SMaciej Machnikowski { "SMA1", SMA1, PTP_PF_NONE, 1, { 0, } }, 16325b2064SMaciej Machnikowski { "U.FL1", UFL1, PTP_PF_NONE, 1, { 0, } }, 17325b2064SMaciej Machnikowski { "SMA2", SMA2, PTP_PF_NONE, 2, { 0, } }, 18325b2064SMaciej Machnikowski { "U.FL2", UFL2, PTP_PF_NONE, 2, { 0, } }, 19325b2064SMaciej Machnikowski }; 20325b2064SMaciej Machnikowski 21325b2064SMaciej Machnikowski /** 22325b2064SMaciej Machnikowski * ice_get_sma_config_e810t 23325b2064SMaciej Machnikowski * @hw: pointer to the hw struct 24325b2064SMaciej Machnikowski * @ptp_pins: pointer to the ptp_pin_desc struture 25325b2064SMaciej Machnikowski * 26325b2064SMaciej Machnikowski * Read the configuration of the SMA control logic and put it into the 27325b2064SMaciej Machnikowski * ptp_pin_desc structure 28325b2064SMaciej Machnikowski */ 29325b2064SMaciej Machnikowski static int 30325b2064SMaciej Machnikowski ice_get_sma_config_e810t(struct ice_hw *hw, struct ptp_pin_desc *ptp_pins) 31325b2064SMaciej Machnikowski { 32325b2064SMaciej Machnikowski u8 data, i; 33325b2064SMaciej Machnikowski int status; 34325b2064SMaciej Machnikowski 35325b2064SMaciej Machnikowski /* Read initial pin state */ 36325b2064SMaciej Machnikowski status = ice_read_sma_ctrl_e810t(hw, &data); 37325b2064SMaciej Machnikowski if (status) 38325b2064SMaciej Machnikowski return status; 39325b2064SMaciej Machnikowski 40325b2064SMaciej Machnikowski /* initialize with defaults */ 41325b2064SMaciej Machnikowski for (i = 0; i < NUM_PTP_PINS_E810T; i++) { 42325b2064SMaciej Machnikowski snprintf(ptp_pins[i].name, sizeof(ptp_pins[i].name), 43325b2064SMaciej Machnikowski "%s", ice_pin_desc_e810t[i].name); 44325b2064SMaciej Machnikowski ptp_pins[i].index = ice_pin_desc_e810t[i].index; 45325b2064SMaciej Machnikowski ptp_pins[i].func = ice_pin_desc_e810t[i].func; 46325b2064SMaciej Machnikowski ptp_pins[i].chan = ice_pin_desc_e810t[i].chan; 47325b2064SMaciej Machnikowski } 48325b2064SMaciej Machnikowski 49325b2064SMaciej Machnikowski /* Parse SMA1/UFL1 */ 50325b2064SMaciej Machnikowski switch (data & ICE_SMA1_MASK_E810T) { 51325b2064SMaciej Machnikowski case ICE_SMA1_MASK_E810T: 52325b2064SMaciej Machnikowski default: 53325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_NONE; 54325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 55325b2064SMaciej Machnikowski break; 56325b2064SMaciej Machnikowski case ICE_SMA1_DIR_EN_E810T: 57325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_PEROUT; 58325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 59325b2064SMaciej Machnikowski break; 60325b2064SMaciej Machnikowski case ICE_SMA1_TX_EN_E810T: 61325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_EXTTS; 62325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 63325b2064SMaciej Machnikowski break; 64325b2064SMaciej Machnikowski case 0: 65325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_EXTTS; 66325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_PEROUT; 67325b2064SMaciej Machnikowski break; 68325b2064SMaciej Machnikowski } 69325b2064SMaciej Machnikowski 70325b2064SMaciej Machnikowski /* Parse SMA2/UFL2 */ 71325b2064SMaciej Machnikowski switch (data & ICE_SMA2_MASK_E810T) { 72325b2064SMaciej Machnikowski case ICE_SMA2_MASK_E810T: 73325b2064SMaciej Machnikowski default: 74325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE; 75325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 76325b2064SMaciej Machnikowski break; 77325b2064SMaciej Machnikowski case (ICE_SMA2_TX_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T): 78325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_EXTTS; 79325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 80325b2064SMaciej Machnikowski break; 81325b2064SMaciej Machnikowski case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T): 82325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_PEROUT; 83325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 84325b2064SMaciej Machnikowski break; 85325b2064SMaciej Machnikowski case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T): 86325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE; 87325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_EXTTS; 88325b2064SMaciej Machnikowski break; 89325b2064SMaciej Machnikowski case ICE_SMA2_DIR_EN_E810T: 90325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_PEROUT; 91325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_EXTTS; 92325b2064SMaciej Machnikowski break; 93325b2064SMaciej Machnikowski } 94325b2064SMaciej Machnikowski 95325b2064SMaciej Machnikowski return 0; 96325b2064SMaciej Machnikowski } 97325b2064SMaciej Machnikowski 98325b2064SMaciej Machnikowski /** 99325b2064SMaciej Machnikowski * ice_ptp_set_sma_config_e810t 100325b2064SMaciej Machnikowski * @hw: pointer to the hw struct 101325b2064SMaciej Machnikowski * @ptp_pins: pointer to the ptp_pin_desc struture 102325b2064SMaciej Machnikowski * 103325b2064SMaciej Machnikowski * Set the configuration of the SMA control logic based on the configuration in 104325b2064SMaciej Machnikowski * num_pins parameter 105325b2064SMaciej Machnikowski */ 106325b2064SMaciej Machnikowski static int 107325b2064SMaciej Machnikowski ice_ptp_set_sma_config_e810t(struct ice_hw *hw, 108325b2064SMaciej Machnikowski const struct ptp_pin_desc *ptp_pins) 109325b2064SMaciej Machnikowski { 110325b2064SMaciej Machnikowski int status; 111325b2064SMaciej Machnikowski u8 data; 112325b2064SMaciej Machnikowski 113325b2064SMaciej Machnikowski /* SMA1 and UFL1 cannot be set to TX at the same time */ 114325b2064SMaciej Machnikowski if (ptp_pins[SMA1].func == PTP_PF_PEROUT && 115325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT) 116325b2064SMaciej Machnikowski return -EINVAL; 117325b2064SMaciej Machnikowski 118325b2064SMaciej Machnikowski /* SMA2 and UFL2 cannot be set to RX at the same time */ 119325b2064SMaciej Machnikowski if (ptp_pins[SMA2].func == PTP_PF_EXTTS && 120325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS) 121325b2064SMaciej Machnikowski return -EINVAL; 122325b2064SMaciej Machnikowski 123325b2064SMaciej Machnikowski /* Read initial pin state value */ 124325b2064SMaciej Machnikowski status = ice_read_sma_ctrl_e810t(hw, &data); 125325b2064SMaciej Machnikowski if (status) 126325b2064SMaciej Machnikowski return status; 127325b2064SMaciej Machnikowski 128325b2064SMaciej Machnikowski /* Set the right sate based on the desired configuration */ 129325b2064SMaciej Machnikowski data &= ~ICE_SMA1_MASK_E810T; 130325b2064SMaciej Machnikowski if (ptp_pins[SMA1].func == PTP_PF_NONE && 131325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) { 132325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 + U.FL1 disabled"); 133325b2064SMaciej Machnikowski data |= ICE_SMA1_MASK_E810T; 134325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_EXTTS && 135325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) { 136325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX"); 137325b2064SMaciej Machnikowski data |= ICE_SMA1_TX_EN_E810T; 138325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_NONE && 139325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT) { 140325b2064SMaciej Machnikowski /* U.FL 1 TX will always enable SMA 1 RX */ 141325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX"); 142325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_EXTTS && 143325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT) { 144325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX"); 145325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_PEROUT && 146325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) { 147325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 TX"); 148325b2064SMaciej Machnikowski data |= ICE_SMA1_DIR_EN_E810T; 149325b2064SMaciej Machnikowski } 150325b2064SMaciej Machnikowski 151325b2064SMaciej Machnikowski data &= ~ICE_SMA2_MASK_E810T; 152325b2064SMaciej Machnikowski if (ptp_pins[SMA2].func == PTP_PF_NONE && 153325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) { 154325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 + U.FL2 disabled"); 155325b2064SMaciej Machnikowski data |= ICE_SMA2_MASK_E810T; 156325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_EXTTS && 157325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) { 158325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 RX"); 159325b2064SMaciej Machnikowski data |= (ICE_SMA2_TX_EN_E810T | 160325b2064SMaciej Machnikowski ICE_SMA2_UFL2_RX_DIS_E810T); 161325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_NONE && 162325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS) { 163325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "UFL2 RX"); 164325b2064SMaciej Machnikowski data |= (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T); 165325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_PEROUT && 166325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) { 167325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 TX"); 168325b2064SMaciej Machnikowski data |= (ICE_SMA2_DIR_EN_E810T | 169325b2064SMaciej Machnikowski ICE_SMA2_UFL2_RX_DIS_E810T); 170325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_PEROUT && 171325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS) { 172325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 TX + U.FL2 RX"); 173325b2064SMaciej Machnikowski data |= ICE_SMA2_DIR_EN_E810T; 174325b2064SMaciej Machnikowski } 175325b2064SMaciej Machnikowski 176325b2064SMaciej Machnikowski return ice_write_sma_ctrl_e810t(hw, data); 177325b2064SMaciej Machnikowski } 178325b2064SMaciej Machnikowski 179325b2064SMaciej Machnikowski /** 180325b2064SMaciej Machnikowski * ice_ptp_set_sma_e810t 181325b2064SMaciej Machnikowski * @info: the driver's PTP info structure 182325b2064SMaciej Machnikowski * @pin: pin index in kernel structure 183325b2064SMaciej Machnikowski * @func: Pin function to be set (PTP_PF_NONE, PTP_PF_EXTTS or PTP_PF_PEROUT) 184325b2064SMaciej Machnikowski * 185325b2064SMaciej Machnikowski * Set the configuration of a single SMA pin 186325b2064SMaciej Machnikowski */ 187325b2064SMaciej Machnikowski static int 188325b2064SMaciej Machnikowski ice_ptp_set_sma_e810t(struct ptp_clock_info *info, unsigned int pin, 189325b2064SMaciej Machnikowski enum ptp_pin_function func) 190325b2064SMaciej Machnikowski { 191325b2064SMaciej Machnikowski struct ptp_pin_desc ptp_pins[NUM_PTP_PINS_E810T]; 192325b2064SMaciej Machnikowski struct ice_pf *pf = ptp_info_to_pf(info); 193325b2064SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 194325b2064SMaciej Machnikowski int err; 195325b2064SMaciej Machnikowski 196325b2064SMaciej Machnikowski if (pin < SMA1 || func > PTP_PF_PEROUT) 197325b2064SMaciej Machnikowski return -EOPNOTSUPP; 198325b2064SMaciej Machnikowski 199325b2064SMaciej Machnikowski err = ice_get_sma_config_e810t(hw, ptp_pins); 200325b2064SMaciej Machnikowski if (err) 201325b2064SMaciej Machnikowski return err; 202325b2064SMaciej Machnikowski 203325b2064SMaciej Machnikowski /* Disable the same function on the other pin sharing the channel */ 204325b2064SMaciej Machnikowski if (pin == SMA1 && ptp_pins[UFL1].func == func) 205325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 206325b2064SMaciej Machnikowski if (pin == UFL1 && ptp_pins[SMA1].func == func) 207325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_NONE; 208325b2064SMaciej Machnikowski 209325b2064SMaciej Machnikowski if (pin == SMA2 && ptp_pins[UFL2].func == func) 210325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 211325b2064SMaciej Machnikowski if (pin == UFL2 && ptp_pins[SMA2].func == func) 212325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE; 213325b2064SMaciej Machnikowski 214325b2064SMaciej Machnikowski /* Set up new pin function in the temp table */ 215325b2064SMaciej Machnikowski ptp_pins[pin].func = func; 216325b2064SMaciej Machnikowski 217325b2064SMaciej Machnikowski return ice_ptp_set_sma_config_e810t(hw, ptp_pins); 218325b2064SMaciej Machnikowski } 219325b2064SMaciej Machnikowski 220325b2064SMaciej Machnikowski /** 221325b2064SMaciej Machnikowski * ice_verify_pin_e810t 222325b2064SMaciej Machnikowski * @info: the driver's PTP info structure 223325b2064SMaciej Machnikowski * @pin: Pin index 224325b2064SMaciej Machnikowski * @func: Assigned function 225325b2064SMaciej Machnikowski * @chan: Assigned channel 226325b2064SMaciej Machnikowski * 227325b2064SMaciej Machnikowski * Verify if pin supports requested pin function. If the Check pins consistency. 228325b2064SMaciej Machnikowski * Reconfigure the SMA logic attached to the given pin to enable its 229325b2064SMaciej Machnikowski * desired functionality 230325b2064SMaciej Machnikowski */ 231325b2064SMaciej Machnikowski static int 232325b2064SMaciej Machnikowski ice_verify_pin_e810t(struct ptp_clock_info *info, unsigned int pin, 233325b2064SMaciej Machnikowski enum ptp_pin_function func, unsigned int chan) 234325b2064SMaciej Machnikowski { 235325b2064SMaciej Machnikowski /* Don't allow channel reassignment */ 236325b2064SMaciej Machnikowski if (chan != ice_pin_desc_e810t[pin].chan) 237325b2064SMaciej Machnikowski return -EOPNOTSUPP; 238325b2064SMaciej Machnikowski 239325b2064SMaciej Machnikowski /* Check if functions are properly assigned */ 240325b2064SMaciej Machnikowski switch (func) { 241325b2064SMaciej Machnikowski case PTP_PF_NONE: 242325b2064SMaciej Machnikowski break; 243325b2064SMaciej Machnikowski case PTP_PF_EXTTS: 244325b2064SMaciej Machnikowski if (pin == UFL1) 245325b2064SMaciej Machnikowski return -EOPNOTSUPP; 246325b2064SMaciej Machnikowski break; 247325b2064SMaciej Machnikowski case PTP_PF_PEROUT: 248325b2064SMaciej Machnikowski if (pin == UFL2 || pin == GNSS) 249325b2064SMaciej Machnikowski return -EOPNOTSUPP; 250325b2064SMaciej Machnikowski break; 251325b2064SMaciej Machnikowski case PTP_PF_PHYSYNC: 252325b2064SMaciej Machnikowski return -EOPNOTSUPP; 253325b2064SMaciej Machnikowski } 254325b2064SMaciej Machnikowski 255325b2064SMaciej Machnikowski return ice_ptp_set_sma_e810t(info, pin, func); 256325b2064SMaciej Machnikowski } 257325b2064SMaciej Machnikowski 25806c16d89SJacob Keller /** 259ea9b847cSJacob Keller * ice_set_tx_tstamp - Enable or disable Tx timestamping 260ea9b847cSJacob Keller * @pf: The PF pointer to search in 261ea9b847cSJacob Keller * @on: bool value for whether timestamps are enabled or disabled 262ea9b847cSJacob Keller */ 263ea9b847cSJacob Keller static void ice_set_tx_tstamp(struct ice_pf *pf, bool on) 264ea9b847cSJacob Keller { 265ea9b847cSJacob Keller struct ice_vsi *vsi; 266ea9b847cSJacob Keller u32 val; 267ea9b847cSJacob Keller u16 i; 268ea9b847cSJacob Keller 269ea9b847cSJacob Keller vsi = ice_get_main_vsi(pf); 270ea9b847cSJacob Keller if (!vsi) 271ea9b847cSJacob Keller return; 272ea9b847cSJacob Keller 273ea9b847cSJacob Keller /* Set the timestamp enable flag for all the Tx rings */ 27484c5fb8cSJacob Keller ice_for_each_txq(vsi, i) { 275ea9b847cSJacob Keller if (!vsi->tx_rings[i]) 276ea9b847cSJacob Keller continue; 277ea9b847cSJacob Keller vsi->tx_rings[i]->ptp_tx = on; 278ea9b847cSJacob Keller } 279ea9b847cSJacob Keller 280ea9b847cSJacob Keller /* Configure the Tx timestamp interrupt */ 281ea9b847cSJacob Keller val = rd32(&pf->hw, PFINT_OICR_ENA); 282ea9b847cSJacob Keller if (on) 283ea9b847cSJacob Keller val |= PFINT_OICR_TSYN_TX_M; 284ea9b847cSJacob Keller else 285ea9b847cSJacob Keller val &= ~PFINT_OICR_TSYN_TX_M; 286ea9b847cSJacob Keller wr32(&pf->hw, PFINT_OICR_ENA, val); 287e59d75ddSJacob Keller 288e59d75ddSJacob Keller pf->ptp.tstamp_config.tx_type = on ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 289ea9b847cSJacob Keller } 290ea9b847cSJacob Keller 291ea9b847cSJacob Keller /** 29277a78115SJacob Keller * ice_set_rx_tstamp - Enable or disable Rx timestamping 29377a78115SJacob Keller * @pf: The PF pointer to search in 29477a78115SJacob Keller * @on: bool value for whether timestamps are enabled or disabled 29577a78115SJacob Keller */ 29677a78115SJacob Keller static void ice_set_rx_tstamp(struct ice_pf *pf, bool on) 29777a78115SJacob Keller { 29877a78115SJacob Keller struct ice_vsi *vsi; 29977a78115SJacob Keller u16 i; 30077a78115SJacob Keller 30177a78115SJacob Keller vsi = ice_get_main_vsi(pf); 30277a78115SJacob Keller if (!vsi) 30377a78115SJacob Keller return; 30477a78115SJacob Keller 30577a78115SJacob Keller /* Set the timestamp flag for all the Rx rings */ 30677a78115SJacob Keller ice_for_each_rxq(vsi, i) { 30777a78115SJacob Keller if (!vsi->rx_rings[i]) 30877a78115SJacob Keller continue; 30977a78115SJacob Keller vsi->rx_rings[i]->ptp_rx = on; 31077a78115SJacob Keller } 311e59d75ddSJacob Keller 312e59d75ddSJacob Keller pf->ptp.tstamp_config.rx_filter = on ? HWTSTAMP_FILTER_ALL : 313e59d75ddSJacob Keller HWTSTAMP_FILTER_NONE; 31477a78115SJacob Keller } 31577a78115SJacob Keller 31677a78115SJacob Keller /** 31777a78115SJacob Keller * ice_ptp_cfg_timestamp - Configure timestamp for init/deinit 31877a78115SJacob Keller * @pf: Board private structure 31977a78115SJacob Keller * @ena: bool value to enable or disable time stamp 32077a78115SJacob Keller * 32177a78115SJacob Keller * This function will configure timestamping during PTP initialization 32277a78115SJacob Keller * and deinitialization 32377a78115SJacob Keller */ 32448096710SKarol Kolacinski void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena) 32577a78115SJacob Keller { 326ea9b847cSJacob Keller ice_set_tx_tstamp(pf, ena); 32777a78115SJacob Keller ice_set_rx_tstamp(pf, ena); 32877a78115SJacob Keller } 32977a78115SJacob Keller 33077a78115SJacob Keller /** 33167569a7fSJacob Keller * ice_get_ptp_clock_index - Get the PTP clock index 33267569a7fSJacob Keller * @pf: the PF pointer 33367569a7fSJacob Keller * 33467569a7fSJacob Keller * Determine the clock index of the PTP clock associated with this device. If 33567569a7fSJacob Keller * this is the PF controlling the clock, just use the local access to the 33667569a7fSJacob Keller * clock device pointer. 33767569a7fSJacob Keller * 33867569a7fSJacob Keller * Otherwise, read from the driver shared parameters to determine the clock 33967569a7fSJacob Keller * index value. 34067569a7fSJacob Keller * 34167569a7fSJacob Keller * Returns: the index of the PTP clock associated with this device, or -1 if 34267569a7fSJacob Keller * there is no associated clock. 34367569a7fSJacob Keller */ 34467569a7fSJacob Keller int ice_get_ptp_clock_index(struct ice_pf *pf) 34567569a7fSJacob Keller { 34667569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 34767569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 34867569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 34967569a7fSJacob Keller u8 tmr_idx; 35067569a7fSJacob Keller u32 value; 35167569a7fSJacob Keller int err; 35267569a7fSJacob Keller 35367569a7fSJacob Keller /* Use the ptp_clock structure if we're the main PF */ 35467569a7fSJacob Keller if (pf->ptp.clock) 35567569a7fSJacob Keller return ptp_clock_index(pf->ptp.clock); 35667569a7fSJacob Keller 35767569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 35867569a7fSJacob Keller if (!tmr_idx) 35967569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 36067569a7fSJacob Keller else 36167569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 36267569a7fSJacob Keller 36367569a7fSJacob Keller err = ice_aq_get_driver_param(hw, param_idx, &value, NULL); 36467569a7fSJacob Keller if (err) { 36567569a7fSJacob Keller dev_err(dev, "Failed to read PTP clock index parameter, err %d aq_err %s\n", 36667569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 36767569a7fSJacob Keller return -1; 36867569a7fSJacob Keller } 36967569a7fSJacob Keller 37067569a7fSJacob Keller /* The PTP clock index is an integer, and will be between 0 and 37167569a7fSJacob Keller * INT_MAX. The highest bit of the driver shared parameter is used to 37267569a7fSJacob Keller * indicate whether or not the currently stored clock index is valid. 37367569a7fSJacob Keller */ 37467569a7fSJacob Keller if (!(value & PTP_SHARED_CLK_IDX_VALID)) 37567569a7fSJacob Keller return -1; 37667569a7fSJacob Keller 37767569a7fSJacob Keller return value & ~PTP_SHARED_CLK_IDX_VALID; 37867569a7fSJacob Keller } 37967569a7fSJacob Keller 38067569a7fSJacob Keller /** 38167569a7fSJacob Keller * ice_set_ptp_clock_index - Set the PTP clock index 38267569a7fSJacob Keller * @pf: the PF pointer 38367569a7fSJacob Keller * 38467569a7fSJacob Keller * Set the PTP clock index for this device into the shared driver parameters, 38567569a7fSJacob Keller * so that other PFs associated with this device can read it. 38667569a7fSJacob Keller * 38767569a7fSJacob Keller * If the PF is unable to store the clock index, it will log an error, but 38867569a7fSJacob Keller * will continue operating PTP. 38967569a7fSJacob Keller */ 39067569a7fSJacob Keller static void ice_set_ptp_clock_index(struct ice_pf *pf) 39167569a7fSJacob Keller { 39267569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 39367569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 39467569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 39567569a7fSJacob Keller u8 tmr_idx; 39667569a7fSJacob Keller u32 value; 39767569a7fSJacob Keller int err; 39867569a7fSJacob Keller 39967569a7fSJacob Keller if (!pf->ptp.clock) 40067569a7fSJacob Keller return; 40167569a7fSJacob Keller 40267569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 40367569a7fSJacob Keller if (!tmr_idx) 40467569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 40567569a7fSJacob Keller else 40667569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 40767569a7fSJacob Keller 40867569a7fSJacob Keller value = (u32)ptp_clock_index(pf->ptp.clock); 40967569a7fSJacob Keller if (value > INT_MAX) { 41067569a7fSJacob Keller dev_err(dev, "PTP Clock index is too large to store\n"); 41167569a7fSJacob Keller return; 41267569a7fSJacob Keller } 41367569a7fSJacob Keller value |= PTP_SHARED_CLK_IDX_VALID; 41467569a7fSJacob Keller 41567569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, value, NULL); 41667569a7fSJacob Keller if (err) { 41767569a7fSJacob Keller dev_err(dev, "Failed to set PTP clock index parameter, err %d aq_err %s\n", 41867569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 41967569a7fSJacob Keller } 42067569a7fSJacob Keller } 42167569a7fSJacob Keller 42267569a7fSJacob Keller /** 42367569a7fSJacob Keller * ice_clear_ptp_clock_index - Clear the PTP clock index 42467569a7fSJacob Keller * @pf: the PF pointer 42567569a7fSJacob Keller * 42667569a7fSJacob Keller * Clear the PTP clock index for this device. Must be called when 42767569a7fSJacob Keller * unregistering the PTP clock, in order to ensure other PFs stop reporting 42867569a7fSJacob Keller * a clock object that no longer exists. 42967569a7fSJacob Keller */ 43067569a7fSJacob Keller static void ice_clear_ptp_clock_index(struct ice_pf *pf) 43167569a7fSJacob Keller { 43267569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 43367569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 43467569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 43567569a7fSJacob Keller u8 tmr_idx; 43667569a7fSJacob Keller int err; 43767569a7fSJacob Keller 43867569a7fSJacob Keller /* Do not clear the index if we don't own the timer */ 43967569a7fSJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned) 44067569a7fSJacob Keller return; 44167569a7fSJacob Keller 44267569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 44367569a7fSJacob Keller if (!tmr_idx) 44467569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 44567569a7fSJacob Keller else 44667569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 44767569a7fSJacob Keller 44867569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, 0, NULL); 44967569a7fSJacob Keller if (err) { 45067569a7fSJacob Keller dev_dbg(dev, "Failed to clear PTP clock index parameter, err %d aq_err %s\n", 45167569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 45267569a7fSJacob Keller } 45367569a7fSJacob Keller } 45467569a7fSJacob Keller 45567569a7fSJacob Keller /** 45606c16d89SJacob Keller * ice_ptp_read_src_clk_reg - Read the source clock register 45706c16d89SJacob Keller * @pf: Board private structure 45806c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 45906c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 46006c16d89SJacob Keller */ 46106c16d89SJacob Keller static u64 46206c16d89SJacob Keller ice_ptp_read_src_clk_reg(struct ice_pf *pf, struct ptp_system_timestamp *sts) 46306c16d89SJacob Keller { 46406c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 46506c16d89SJacob Keller u32 hi, lo, lo2; 46606c16d89SJacob Keller u8 tmr_idx; 46706c16d89SJacob Keller 46806c16d89SJacob Keller tmr_idx = ice_get_ptp_src_clock_index(hw); 46906c16d89SJacob Keller /* Read the system timestamp pre PHC read */ 47006c16d89SJacob Keller ptp_read_system_prets(sts); 47106c16d89SJacob Keller 47206c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 47306c16d89SJacob Keller 47406c16d89SJacob Keller /* Read the system timestamp post PHC read */ 47506c16d89SJacob Keller ptp_read_system_postts(sts); 47606c16d89SJacob Keller 47706c16d89SJacob Keller hi = rd32(hw, GLTSYN_TIME_H(tmr_idx)); 47806c16d89SJacob Keller lo2 = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 47906c16d89SJacob Keller 48006c16d89SJacob Keller if (lo2 < lo) { 48106c16d89SJacob Keller /* if TIME_L rolled over read TIME_L again and update 48206c16d89SJacob Keller * system timestamps 48306c16d89SJacob Keller */ 48406c16d89SJacob Keller ptp_read_system_prets(sts); 48506c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 48606c16d89SJacob Keller ptp_read_system_postts(sts); 48706c16d89SJacob Keller hi = rd32(hw, GLTSYN_TIME_H(tmr_idx)); 48806c16d89SJacob Keller } 48906c16d89SJacob Keller 49006c16d89SJacob Keller return ((u64)hi << 32) | lo; 49106c16d89SJacob Keller } 49206c16d89SJacob Keller 49306c16d89SJacob Keller /** 49477a78115SJacob Keller * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b 49577a78115SJacob Keller * @cached_phc_time: recently cached copy of PHC time 49677a78115SJacob Keller * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value 49777a78115SJacob Keller * 49877a78115SJacob Keller * Hardware captures timestamps which contain only 32 bits of nominal 49977a78115SJacob Keller * nanoseconds, as opposed to the 64bit timestamps that the stack expects. 50077a78115SJacob Keller * Note that the captured timestamp values may be 40 bits, but the lower 50177a78115SJacob Keller * 8 bits are sub-nanoseconds and generally discarded. 50277a78115SJacob Keller * 50377a78115SJacob Keller * Extend the 32bit nanosecond timestamp using the following algorithm and 50477a78115SJacob Keller * assumptions: 50577a78115SJacob Keller * 50677a78115SJacob Keller * 1) have a recently cached copy of the PHC time 50777a78115SJacob Keller * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1 50877a78115SJacob Keller * seconds) before or after the PHC time was captured. 50977a78115SJacob Keller * 3) calculate the delta between the cached time and the timestamp 51077a78115SJacob Keller * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was 51177a78115SJacob Keller * captured after the PHC time. In this case, the full timestamp is just 51277a78115SJacob Keller * the cached PHC time plus the delta. 51377a78115SJacob Keller * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the 51477a78115SJacob Keller * timestamp was captured *before* the PHC time, i.e. because the PHC 51577a78115SJacob Keller * cache was updated after the timestamp was captured by hardware. In this 51677a78115SJacob Keller * case, the full timestamp is the cached time minus the inverse delta. 51777a78115SJacob Keller * 51877a78115SJacob Keller * This algorithm works even if the PHC time was updated after a Tx timestamp 51977a78115SJacob Keller * was requested, but before the Tx timestamp event was reported from 52077a78115SJacob Keller * hardware. 52177a78115SJacob Keller * 52277a78115SJacob Keller * This calculation primarily relies on keeping the cached PHC time up to 52377a78115SJacob Keller * date. If the timestamp was captured more than 2^31 nanoseconds after the 52477a78115SJacob Keller * PHC time, it is possible that the lower 32bits of PHC time have 52577a78115SJacob Keller * overflowed more than once, and we might generate an incorrect timestamp. 52677a78115SJacob Keller * 52777a78115SJacob Keller * This is prevented by (a) periodically updating the cached PHC time once 52877a78115SJacob Keller * a second, and (b) discarding any Tx timestamp packet if it has waited for 52977a78115SJacob Keller * a timestamp for more than one second. 53077a78115SJacob Keller */ 53177a78115SJacob Keller static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp) 53277a78115SJacob Keller { 53377a78115SJacob Keller u32 delta, phc_time_lo; 53477a78115SJacob Keller u64 ns; 53577a78115SJacob Keller 53677a78115SJacob Keller /* Extract the lower 32 bits of the PHC time */ 53777a78115SJacob Keller phc_time_lo = (u32)cached_phc_time; 53877a78115SJacob Keller 53977a78115SJacob Keller /* Calculate the delta between the lower 32bits of the cached PHC 54077a78115SJacob Keller * time and the in_tstamp value 54177a78115SJacob Keller */ 54277a78115SJacob Keller delta = (in_tstamp - phc_time_lo); 54377a78115SJacob Keller 54477a78115SJacob Keller /* Do not assume that the in_tstamp is always more recent than the 54577a78115SJacob Keller * cached PHC time. If the delta is large, it indicates that the 54677a78115SJacob Keller * in_tstamp was taken in the past, and should be converted 54777a78115SJacob Keller * forward. 54877a78115SJacob Keller */ 54977a78115SJacob Keller if (delta > (U32_MAX / 2)) { 55077a78115SJacob Keller /* reverse the delta calculation here */ 55177a78115SJacob Keller delta = (phc_time_lo - in_tstamp); 55277a78115SJacob Keller ns = cached_phc_time - delta; 55377a78115SJacob Keller } else { 55477a78115SJacob Keller ns = cached_phc_time + delta; 55577a78115SJacob Keller } 55677a78115SJacob Keller 55777a78115SJacob Keller return ns; 55877a78115SJacob Keller } 55977a78115SJacob Keller 56077a78115SJacob Keller /** 561ea9b847cSJacob Keller * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds 562ea9b847cSJacob Keller * @pf: Board private structure 563ea9b847cSJacob Keller * @in_tstamp: Ingress/egress 40b timestamp value 564ea9b847cSJacob Keller * 565ea9b847cSJacob Keller * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal 566ea9b847cSJacob Keller * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit. 567ea9b847cSJacob Keller * 568ea9b847cSJacob Keller * *--------------------------------------------------------------* 569ea9b847cSJacob Keller * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v | 570ea9b847cSJacob Keller * *--------------------------------------------------------------* 571ea9b847cSJacob Keller * 572ea9b847cSJacob Keller * The low bit is an indicator of whether the timestamp is valid. The next 573ea9b847cSJacob Keller * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow, 574ea9b847cSJacob Keller * and the remaining 32 bits are the lower 32 bits of the PHC timer. 575ea9b847cSJacob Keller * 576ea9b847cSJacob Keller * It is assumed that the caller verifies the timestamp is valid prior to 577ea9b847cSJacob Keller * calling this function. 578ea9b847cSJacob Keller * 579ea9b847cSJacob Keller * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC 580ea9b847cSJacob Keller * time stored in the device private PTP structure as the basis for timestamp 581ea9b847cSJacob Keller * extension. 582ea9b847cSJacob Keller * 583ea9b847cSJacob Keller * See ice_ptp_extend_32b_ts for a detailed explanation of the extension 584ea9b847cSJacob Keller * algorithm. 585ea9b847cSJacob Keller */ 586ea9b847cSJacob Keller static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp) 587ea9b847cSJacob Keller { 588ea9b847cSJacob Keller const u64 mask = GENMASK_ULL(31, 0); 589cd25507aSJacob Keller unsigned long discard_time; 590cd25507aSJacob Keller 591cd25507aSJacob Keller /* Discard the hardware timestamp if the cached PHC time is too old */ 592cd25507aSJacob Keller discard_time = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000); 593cd25507aSJacob Keller if (time_is_before_jiffies(discard_time)) { 594cd25507aSJacob Keller pf->ptp.tx_hwtstamp_discarded++; 595cd25507aSJacob Keller return 0; 596cd25507aSJacob Keller } 597ea9b847cSJacob Keller 598ea9b847cSJacob Keller return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time, 599ea9b847cSJacob Keller (in_tstamp >> 8) & mask); 600ea9b847cSJacob Keller } 601ea9b847cSJacob Keller 602ea9b847cSJacob Keller /** 6033ad5c10bSJacob Keller * ice_ptp_is_tx_tracker_up - Check if Tx tracker is ready for new timestamps 6043ad5c10bSJacob Keller * @tx: the PTP Tx timestamp tracker to check 6053ad5c10bSJacob Keller * 6063ad5c10bSJacob Keller * Check that a given PTP Tx timestamp tracker is up, i.e. that it is ready 6073ad5c10bSJacob Keller * to accept new timestamp requests. 6083ad5c10bSJacob Keller * 6093ad5c10bSJacob Keller * Assumes the tx->lock spinlock is already held. 6103ad5c10bSJacob Keller */ 6113ad5c10bSJacob Keller static bool 6123ad5c10bSJacob Keller ice_ptp_is_tx_tracker_up(struct ice_ptp_tx *tx) 6133ad5c10bSJacob Keller { 6143ad5c10bSJacob Keller lockdep_assert_held(&tx->lock); 6153ad5c10bSJacob Keller 6163ad5c10bSJacob Keller return tx->init && !tx->calibrating; 6173ad5c10bSJacob Keller } 6183ad5c10bSJacob Keller 6193ad5c10bSJacob Keller /** 6201229b339SKarol Kolacinski * ice_ptp_tx_tstamp - Process Tx timestamps for a port 6211229b339SKarol Kolacinski * @tx: the PTP Tx timestamp tracker 6224b1251bdSJacob Keller * 6234b1251bdSJacob Keller * Process timestamps captured by the PHY associated with this port. To do 6244b1251bdSJacob Keller * this, loop over each index with a waiting skb. 6254b1251bdSJacob Keller * 6264b1251bdSJacob Keller * If a given index has a valid timestamp, perform the following steps: 6274b1251bdSJacob Keller * 628d40fd600SJacob Keller * 1) check that the timestamp request is not stale 629d40fd600SJacob Keller * 2) check that a timestamp is ready and available in the PHY memory bank 630d40fd600SJacob Keller * 3) read and copy the timestamp out of the PHY register 631d40fd600SJacob Keller * 4) unlock the index by clearing the associated in_use bit 632d40fd600SJacob Keller * 5) check if the timestamp is stale, and discard if so 633d40fd600SJacob Keller * 6) extend the 40 bit timestamp value to get a 64 bit timestamp value 634d40fd600SJacob Keller * 7) send this 64 bit timestamp to the stack 6354b1251bdSJacob Keller * 63630f15874SJacob Keller * Returns true if all timestamps were handled, and false if any slots remain 63730f15874SJacob Keller * without a timestamp. 63830f15874SJacob Keller * 63930f15874SJacob Keller * After looping, if we still have waiting SKBs, return false. This may cause 64030f15874SJacob Keller * us effectively poll even when not strictly necessary. We do this because 64130f15874SJacob Keller * it's possible a new timestamp was requested around the same time as the 64230f15874SJacob Keller * interrupt. In some cases hardware might not interrupt us again when the 64330f15874SJacob Keller * timestamp is captured. 6444b1251bdSJacob Keller * 645d40fd600SJacob Keller * Note that we do not hold the tracking lock while reading the Tx timestamp. 646d40fd600SJacob Keller * This is because reading the timestamp requires taking a mutex that might 647d40fd600SJacob Keller * sleep. 6480dd92862SJacob Keller * 649d40fd600SJacob Keller * The only place where we set in_use is when a new timestamp is initiated 650d40fd600SJacob Keller * with a slot index. This is only called in the hard xmit routine where an 651d40fd600SJacob Keller * SKB has a request flag set. The only places where we clear this bit is this 652d40fd600SJacob Keller * function, or during teardown when the Tx timestamp tracker is being 653d40fd600SJacob Keller * removed. A timestamp index will never be re-used until the in_use bit for 654d40fd600SJacob Keller * that index is cleared. 6550dd92862SJacob Keller * 6560dd92862SJacob Keller * If a Tx thread starts a new timestamp, we might not begin processing it 6570dd92862SJacob Keller * right away but we will notice it at the end when we re-queue the task. 6580dd92862SJacob Keller * 6590dd92862SJacob Keller * If a Tx thread starts a new timestamp just after this function exits, the 6600dd92862SJacob Keller * interrupt for that timestamp should re-trigger this function once 6610dd92862SJacob Keller * a timestamp is ready. 6620dd92862SJacob Keller * 663d40fd600SJacob Keller * In cases where the PTP hardware clock was directly adjusted, some 664d40fd600SJacob Keller * timestamps may not be able to safely use the timestamp extension math. In 665d40fd600SJacob Keller * this case, software will set the stale bit for any outstanding Tx 666d40fd600SJacob Keller * timestamps when the clock is adjusted. Then this function will discard 667d40fd600SJacob Keller * those captured timestamps instead of sending them to the stack. 6680dd92862SJacob Keller * 6690dd92862SJacob Keller * If a Tx packet has been waiting for more than 2 seconds, it is not possible 6700dd92862SJacob Keller * to correctly extend the timestamp using the cached PHC time. It is 6710dd92862SJacob Keller * extremely unlikely that a packet will ever take this long to timestamp. If 6720dd92862SJacob Keller * we detect a Tx timestamp request that has waited for this long we assume 6730dd92862SJacob Keller * the packet will never be sent by hardware and discard it without reading 6740dd92862SJacob Keller * the timestamp register. 6754b1251bdSJacob Keller */ 6761229b339SKarol Kolacinski static bool ice_ptp_tx_tstamp(struct ice_ptp_tx *tx) 6774b1251bdSJacob Keller { 6784b1251bdSJacob Keller struct ice_ptp_port *ptp_port; 679f0ae1240SJacob Keller bool more_timestamps; 6804b1251bdSJacob Keller struct ice_pf *pf; 68110e4b4a3SJacob Keller struct ice_hw *hw; 68210e4b4a3SJacob Keller u64 tstamp_ready; 68310e4b4a3SJacob Keller int err; 6844b1251bdSJacob Keller u8 idx; 6854b1251bdSJacob Keller 6864b1251bdSJacob Keller if (!tx->init) 68730f15874SJacob Keller return true; 6884b1251bdSJacob Keller 6894b1251bdSJacob Keller ptp_port = container_of(tx, struct ice_ptp_port, tx); 6904b1251bdSJacob Keller pf = ptp_port_to_pf(ptp_port); 69110e4b4a3SJacob Keller hw = &pf->hw; 69210e4b4a3SJacob Keller 69310e4b4a3SJacob Keller /* Read the Tx ready status first */ 69410e4b4a3SJacob Keller err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready); 69510e4b4a3SJacob Keller if (err) 69610e4b4a3SJacob Keller return false; 6974b1251bdSJacob Keller 6984b1251bdSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 6994b1251bdSJacob Keller struct skb_shared_hwtstamps shhwtstamps = {}; 7006b5cbc8cSSergey Temerkhanov u8 phy_idx = idx + tx->offset; 7010dd92862SJacob Keller u64 raw_tstamp = 0, tstamp; 7020dd92862SJacob Keller bool drop_ts = false; 7034b1251bdSJacob Keller struct sk_buff *skb; 7044b1251bdSJacob Keller 7050dd92862SJacob Keller /* Drop packets which have waited for more than 2 seconds */ 7060dd92862SJacob Keller if (time_is_before_jiffies(tx->tstamps[idx].start + 2 * HZ)) { 7070dd92862SJacob Keller drop_ts = true; 7080dd92862SJacob Keller 7090dd92862SJacob Keller /* Count the number of Tx timestamps that timed out */ 7100dd92862SJacob Keller pf->ptp.tx_hwtstamp_timeouts++; 71110e4b4a3SJacob Keller } 7120dd92862SJacob Keller 71310e4b4a3SJacob Keller /* Only read a timestamp from the PHY if its marked as ready 71410e4b4a3SJacob Keller * by the tstamp_ready register. This avoids unnecessary 71510e4b4a3SJacob Keller * reading of timestamps which are not yet valid. This is 71610e4b4a3SJacob Keller * important as we must read all timestamps which are valid 71710e4b4a3SJacob Keller * and only timestamps which are valid during each interrupt. 71810e4b4a3SJacob Keller * If we do not, the hardware logic for generating a new 71910e4b4a3SJacob Keller * interrupt can get stuck on some devices. 72010e4b4a3SJacob Keller */ 72110e4b4a3SJacob Keller if (!(tstamp_ready & BIT_ULL(phy_idx))) { 72210e4b4a3SJacob Keller if (drop_ts) 7230dd92862SJacob Keller goto skip_ts_read; 72410e4b4a3SJacob Keller 72510e4b4a3SJacob Keller continue; 7260dd92862SJacob Keller } 7270dd92862SJacob Keller 7284b1251bdSJacob Keller ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx); 7294b1251bdSJacob Keller 73010e4b4a3SJacob Keller err = ice_read_phy_tstamp(hw, tx->block, phy_idx, &raw_tstamp); 7314b1251bdSJacob Keller if (err) 7324b1251bdSJacob Keller continue; 7334b1251bdSJacob Keller 7344b1251bdSJacob Keller ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx); 7354b1251bdSJacob Keller 73610e4b4a3SJacob Keller /* For PHYs which don't implement a proper timestamp ready 73710e4b4a3SJacob Keller * bitmap, verify that the timestamp value is different 73810e4b4a3SJacob Keller * from the last cached timestamp. If it is not, skip this for 73910e4b4a3SJacob Keller * now assuming it hasn't yet been captured by hardware. 74010e4b4a3SJacob Keller */ 74110e4b4a3SJacob Keller if (!drop_ts && tx->verify_cached && 7424b1251bdSJacob Keller raw_tstamp == tx->tstamps[idx].cached_tstamp) 7434b1251bdSJacob Keller continue; 7444b1251bdSJacob Keller 74510e4b4a3SJacob Keller /* Discard any timestamp value without the valid bit set */ 74610e4b4a3SJacob Keller if (!(raw_tstamp & ICE_PTP_TS_VALID)) 74710e4b4a3SJacob Keller drop_ts = true; 74810e4b4a3SJacob Keller 7490dd92862SJacob Keller skip_ts_read: 7504b1251bdSJacob Keller spin_lock(&tx->lock); 75110e4b4a3SJacob Keller if (tx->verify_cached && raw_tstamp) 7524b1251bdSJacob Keller tx->tstamps[idx].cached_tstamp = raw_tstamp; 7534b1251bdSJacob Keller clear_bit(idx, tx->in_use); 7544b1251bdSJacob Keller skb = tx->tstamps[idx].skb; 7554b1251bdSJacob Keller tx->tstamps[idx].skb = NULL; 756d40fd600SJacob Keller if (test_and_clear_bit(idx, tx->stale)) 757d40fd600SJacob Keller drop_ts = true; 7584b1251bdSJacob Keller spin_unlock(&tx->lock); 7594b1251bdSJacob Keller 7600dd92862SJacob Keller /* It is unlikely but possible that the SKB will have been 7610dd92862SJacob Keller * flushed at this point due to link change or teardown. 7624b1251bdSJacob Keller */ 7634b1251bdSJacob Keller if (!skb) 7644b1251bdSJacob Keller continue; 7654b1251bdSJacob Keller 7660dd92862SJacob Keller if (drop_ts) { 7670dd92862SJacob Keller dev_kfree_skb_any(skb); 7680dd92862SJacob Keller continue; 7690dd92862SJacob Keller } 7700dd92862SJacob Keller 7714b1251bdSJacob Keller /* Extend the timestamp using cached PHC time */ 7724b1251bdSJacob Keller tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp); 7734b1251bdSJacob Keller if (tstamp) { 7744b1251bdSJacob Keller shhwtstamps.hwtstamp = ns_to_ktime(tstamp); 7754b1251bdSJacob Keller ice_trace(tx_tstamp_complete, skb, idx); 7764b1251bdSJacob Keller } 7774b1251bdSJacob Keller 7784b1251bdSJacob Keller skb_tstamp_tx(skb, &shhwtstamps); 7794b1251bdSJacob Keller dev_kfree_skb_any(skb); 7804b1251bdSJacob Keller } 7814b1251bdSJacob Keller 7824b1251bdSJacob Keller /* Check if we still have work to do. If so, re-queue this task to 7834b1251bdSJacob Keller * poll for remaining timestamps. 7844b1251bdSJacob Keller */ 7854b1251bdSJacob Keller spin_lock(&tx->lock); 786f0ae1240SJacob Keller more_timestamps = tx->init && !bitmap_empty(tx->in_use, tx->len); 7874b1251bdSJacob Keller spin_unlock(&tx->lock); 7881229b339SKarol Kolacinski 789f0ae1240SJacob Keller return !more_timestamps; 7904b1251bdSJacob Keller } 7914b1251bdSJacob Keller 7924b1251bdSJacob Keller /** 7934b1251bdSJacob Keller * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps 7944b1251bdSJacob Keller * @tx: Tx tracking structure to initialize 7954b1251bdSJacob Keller * 7964b1251bdSJacob Keller * Assumes that the length has already been initialized. Do not call directly, 7976b5cbc8cSSergey Temerkhanov * use the ice_ptp_init_tx_* instead. 7984b1251bdSJacob Keller */ 7994b1251bdSJacob Keller static int 8004b1251bdSJacob Keller ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx) 8014b1251bdSJacob Keller { 802d40fd600SJacob Keller unsigned long *in_use, *stale; 803c1f3414dSJacob Keller struct ice_tx_tstamp *tstamps; 8044b1251bdSJacob Keller 805c1f3414dSJacob Keller tstamps = kcalloc(tx->len, sizeof(*tstamps), GFP_KERNEL); 806c1f3414dSJacob Keller in_use = bitmap_zalloc(tx->len, GFP_KERNEL); 807d40fd600SJacob Keller stale = bitmap_zalloc(tx->len, GFP_KERNEL); 808c1f3414dSJacob Keller 809d40fd600SJacob Keller if (!tstamps || !in_use || !stale) { 810c1f3414dSJacob Keller kfree(tstamps); 811c1f3414dSJacob Keller bitmap_free(in_use); 812d40fd600SJacob Keller bitmap_free(stale); 813c1f3414dSJacob Keller 8144b1251bdSJacob Keller return -ENOMEM; 8154b1251bdSJacob Keller } 8164b1251bdSJacob Keller 817c1f3414dSJacob Keller tx->tstamps = tstamps; 818c1f3414dSJacob Keller tx->in_use = in_use; 819d40fd600SJacob Keller tx->stale = stale; 8204b1251bdSJacob Keller tx->init = 1; 8214b1251bdSJacob Keller 8223ad5c10bSJacob Keller spin_lock_init(&tx->lock); 8233ad5c10bSJacob Keller 8244b1251bdSJacob Keller return 0; 8254b1251bdSJacob Keller } 8264b1251bdSJacob Keller 8274b1251bdSJacob Keller /** 8284b1251bdSJacob Keller * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker 8294b1251bdSJacob Keller * @pf: Board private structure 8304b1251bdSJacob Keller * @tx: the tracker to flush 831e3ba5248SJacob Keller * 832e3ba5248SJacob Keller * Called during teardown when a Tx tracker is being removed. 8334b1251bdSJacob Keller */ 8344b1251bdSJacob Keller static void 8354b1251bdSJacob Keller ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 8364b1251bdSJacob Keller { 837e3ba5248SJacob Keller struct ice_hw *hw = &pf->hw; 838e3ba5248SJacob Keller u64 tstamp_ready; 839e3ba5248SJacob Keller int err; 8404b1251bdSJacob Keller u8 idx; 8414b1251bdSJacob Keller 842e3ba5248SJacob Keller err = ice_get_phy_tx_tstamp_ready(hw, tx->block, &tstamp_ready); 843e3ba5248SJacob Keller if (err) { 844e3ba5248SJacob Keller dev_dbg(ice_pf_to_dev(pf), "Failed to get the Tx tstamp ready bitmap for block %u, err %d\n", 845e3ba5248SJacob Keller tx->block, err); 846e3ba5248SJacob Keller 847e3ba5248SJacob Keller /* If we fail to read the Tx timestamp ready bitmap just 848e3ba5248SJacob Keller * skip clearing the PHY timestamps. 849e3ba5248SJacob Keller */ 850e3ba5248SJacob Keller tstamp_ready = 0; 851e3ba5248SJacob Keller } 852e3ba5248SJacob Keller 853e3ba5248SJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 8546b5cbc8cSSergey Temerkhanov u8 phy_idx = idx + tx->offset; 855e3ba5248SJacob Keller struct sk_buff *skb; 856e3ba5248SJacob Keller 857e3ba5248SJacob Keller /* In case this timestamp is ready, we need to clear it. */ 858e3ba5248SJacob Keller if (!hw->reset_ongoing && (tstamp_ready & BIT_ULL(phy_idx))) 859e3ba5248SJacob Keller ice_clear_phy_tstamp(hw, tx->block, phy_idx); 8604b1251bdSJacob Keller 8614b1251bdSJacob Keller spin_lock(&tx->lock); 862e3ba5248SJacob Keller skb = tx->tstamps[idx].skb; 8634b1251bdSJacob Keller tx->tstamps[idx].skb = NULL; 8644b1251bdSJacob Keller clear_bit(idx, tx->in_use); 865d40fd600SJacob Keller clear_bit(idx, tx->stale); 8664b1251bdSJacob Keller spin_unlock(&tx->lock); 8674b1251bdSJacob Keller 868e3ba5248SJacob Keller /* Count the number of Tx timestamps flushed */ 869e3ba5248SJacob Keller pf->ptp.tx_hwtstamp_flushed++; 870e3ba5248SJacob Keller 871e3ba5248SJacob Keller /* Free the SKB after we've cleared the bit */ 872e3ba5248SJacob Keller dev_kfree_skb_any(skb); 8734b1251bdSJacob Keller } 8744b1251bdSJacob Keller } 8754b1251bdSJacob Keller 8764b1251bdSJacob Keller /** 877d40fd600SJacob Keller * ice_ptp_mark_tx_tracker_stale - Mark unfinished timestamps as stale 878d40fd600SJacob Keller * @tx: the tracker to mark 879d40fd600SJacob Keller * 880d40fd600SJacob Keller * Mark currently outstanding Tx timestamps as stale. This prevents sending 881d40fd600SJacob Keller * their timestamp value to the stack. This is required to prevent extending 882d40fd600SJacob Keller * the 40bit hardware timestamp incorrectly. 883d40fd600SJacob Keller * 884d40fd600SJacob Keller * This should be called when the PTP clock is modified such as after a set 885d40fd600SJacob Keller * time request. 886d40fd600SJacob Keller */ 887d40fd600SJacob Keller static void 888d40fd600SJacob Keller ice_ptp_mark_tx_tracker_stale(struct ice_ptp_tx *tx) 889d40fd600SJacob Keller { 890d40fd600SJacob Keller spin_lock(&tx->lock); 891d40fd600SJacob Keller bitmap_or(tx->stale, tx->stale, tx->in_use, tx->len); 892d40fd600SJacob Keller spin_unlock(&tx->lock); 893d40fd600SJacob Keller } 894d40fd600SJacob Keller 895d40fd600SJacob Keller /** 8964b1251bdSJacob Keller * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker 8974b1251bdSJacob Keller * @pf: Board private structure 8984b1251bdSJacob Keller * @tx: Tx tracking structure to release 8994b1251bdSJacob Keller * 9004b1251bdSJacob Keller * Free memory associated with the Tx timestamp tracker. 9014b1251bdSJacob Keller */ 9024b1251bdSJacob Keller static void 9034b1251bdSJacob Keller ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 9044b1251bdSJacob Keller { 9053ad5c10bSJacob Keller spin_lock(&tx->lock); 9064b1251bdSJacob Keller tx->init = 0; 9073ad5c10bSJacob Keller spin_unlock(&tx->lock); 9084b1251bdSJacob Keller 909f0ae1240SJacob Keller /* wait for potentially outstanding interrupt to complete */ 910f0ae1240SJacob Keller synchronize_irq(pf->msix_entries[pf->oicr_idx].vector); 911f0ae1240SJacob Keller 9124b1251bdSJacob Keller ice_ptp_flush_tx_tracker(pf, tx); 9134b1251bdSJacob Keller 9144b1251bdSJacob Keller kfree(tx->tstamps); 9154b1251bdSJacob Keller tx->tstamps = NULL; 9164b1251bdSJacob Keller 9174b1251bdSJacob Keller bitmap_free(tx->in_use); 9184b1251bdSJacob Keller tx->in_use = NULL; 9194b1251bdSJacob Keller 920d40fd600SJacob Keller bitmap_free(tx->stale); 921d40fd600SJacob Keller tx->stale = NULL; 922d40fd600SJacob Keller 9234b1251bdSJacob Keller tx->len = 0; 9244b1251bdSJacob Keller } 9254b1251bdSJacob Keller 9264b1251bdSJacob Keller /** 9274b1251bdSJacob Keller * ice_ptp_init_tx_e822 - Initialize tracking for Tx timestamps 9284b1251bdSJacob Keller * @pf: Board private structure 9294b1251bdSJacob Keller * @tx: the Tx tracking structure to initialize 9304b1251bdSJacob Keller * @port: the port this structure tracks 9314b1251bdSJacob Keller * 9324b1251bdSJacob Keller * Initialize the Tx timestamp tracker for this port. For generic MAC devices, 9334b1251bdSJacob Keller * the timestamp block is shared for all ports in the same quad. To avoid 9344b1251bdSJacob Keller * ports using the same timestamp index, logically break the block of 9354b1251bdSJacob Keller * registers into chunks based on the port number. 9364b1251bdSJacob Keller */ 9374b1251bdSJacob Keller static int 9384b1251bdSJacob Keller ice_ptp_init_tx_e822(struct ice_pf *pf, struct ice_ptp_tx *tx, u8 port) 9394b1251bdSJacob Keller { 9406b5cbc8cSSergey Temerkhanov tx->block = port / ICE_PORTS_PER_QUAD; 9416b5cbc8cSSergey Temerkhanov tx->offset = (port % ICE_PORTS_PER_QUAD) * INDEX_PER_PORT_E822; 9426b5cbc8cSSergey Temerkhanov tx->len = INDEX_PER_PORT_E822; 94310e4b4a3SJacob Keller tx->verify_cached = 0; 9444b1251bdSJacob Keller 9454b1251bdSJacob Keller return ice_ptp_alloc_tx_tracker(tx); 9464b1251bdSJacob Keller } 9474b1251bdSJacob Keller 9484b1251bdSJacob Keller /** 9494b1251bdSJacob Keller * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps 9504b1251bdSJacob Keller * @pf: Board private structure 9514b1251bdSJacob Keller * @tx: the Tx tracking structure to initialize 9524b1251bdSJacob Keller * 9534b1251bdSJacob Keller * Initialize the Tx timestamp tracker for this PF. For E810 devices, each 9544b1251bdSJacob Keller * port has its own block of timestamps, independent of the other ports. 9554b1251bdSJacob Keller */ 9564b1251bdSJacob Keller static int 9574b1251bdSJacob Keller ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx) 9584b1251bdSJacob Keller { 9596b5cbc8cSSergey Temerkhanov tx->block = pf->hw.port_info->lport; 9606b5cbc8cSSergey Temerkhanov tx->offset = 0; 9616b5cbc8cSSergey Temerkhanov tx->len = INDEX_PER_PORT_E810; 96210e4b4a3SJacob Keller /* The E810 PHY does not provide a timestamp ready bitmap. Instead, 96310e4b4a3SJacob Keller * verify new timestamps against cached copy of the last read 96410e4b4a3SJacob Keller * timestamp. 96510e4b4a3SJacob Keller */ 96610e4b4a3SJacob Keller tx->verify_cached = 1; 9674b1251bdSJacob Keller 9684b1251bdSJacob Keller return ice_ptp_alloc_tx_tracker(tx); 9694b1251bdSJacob Keller } 9704b1251bdSJacob Keller 9714b1251bdSJacob Keller /** 9724b1251bdSJacob Keller * ice_ptp_update_cached_phctime - Update the cached PHC time values 9734b1251bdSJacob Keller * @pf: Board specific private structure 9744b1251bdSJacob Keller * 9754b1251bdSJacob Keller * This function updates the system time values which are cached in the PF 9764b1251bdSJacob Keller * structure and the Rx rings. 9774b1251bdSJacob Keller * 9784b1251bdSJacob Keller * This function must be called periodically to ensure that the cached value 979b1a582e6SJacob Keller * is never more than 2 seconds old. 980b1a582e6SJacob Keller * 981b1a582e6SJacob Keller * Note that the cached copy in the PF PTP structure is always updated, even 982b1a582e6SJacob Keller * if we can't update the copy in the Rx rings. 9834b1251bdSJacob Keller * 9844b1251bdSJacob Keller * Return: 9854b1251bdSJacob Keller * * 0 - OK, successfully updated 9864b1251bdSJacob Keller * * -EAGAIN - PF was busy, need to reschedule the update 9874b1251bdSJacob Keller */ 9884b1251bdSJacob Keller static int ice_ptp_update_cached_phctime(struct ice_pf *pf) 9894b1251bdSJacob Keller { 9904b1251bdSJacob Keller struct device *dev = ice_pf_to_dev(pf); 9914b1251bdSJacob Keller unsigned long update_before; 9924b1251bdSJacob Keller u64 systime; 9934b1251bdSJacob Keller int i; 9944b1251bdSJacob Keller 9954b1251bdSJacob Keller update_before = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000); 9964b1251bdSJacob Keller if (pf->ptp.cached_phc_time && 9974b1251bdSJacob Keller time_is_before_jiffies(update_before)) { 9984b1251bdSJacob Keller unsigned long time_taken = jiffies - pf->ptp.cached_phc_jiffies; 9994b1251bdSJacob Keller 10004b1251bdSJacob Keller dev_warn(dev, "%u msecs passed between update to cached PHC time\n", 10014b1251bdSJacob Keller jiffies_to_msecs(time_taken)); 10024b1251bdSJacob Keller pf->ptp.late_cached_phc_updates++; 10034b1251bdSJacob Keller } 10044b1251bdSJacob Keller 10054b1251bdSJacob Keller /* Read the current PHC time */ 10064b1251bdSJacob Keller systime = ice_ptp_read_src_clk_reg(pf, NULL); 10074b1251bdSJacob Keller 10084b1251bdSJacob Keller /* Update the cached PHC time stored in the PF structure */ 10094b1251bdSJacob Keller WRITE_ONCE(pf->ptp.cached_phc_time, systime); 10104b1251bdSJacob Keller WRITE_ONCE(pf->ptp.cached_phc_jiffies, jiffies); 10114b1251bdSJacob Keller 1012b1a582e6SJacob Keller if (test_and_set_bit(ICE_CFG_BUSY, pf->state)) 1013b1a582e6SJacob Keller return -EAGAIN; 1014b1a582e6SJacob Keller 10154b1251bdSJacob Keller ice_for_each_vsi(pf, i) { 10164b1251bdSJacob Keller struct ice_vsi *vsi = pf->vsi[i]; 10174b1251bdSJacob Keller int j; 10184b1251bdSJacob Keller 10194b1251bdSJacob Keller if (!vsi) 10204b1251bdSJacob Keller continue; 10214b1251bdSJacob Keller 10224b1251bdSJacob Keller if (vsi->type != ICE_VSI_PF) 10234b1251bdSJacob Keller continue; 10244b1251bdSJacob Keller 10254b1251bdSJacob Keller ice_for_each_rxq(vsi, j) { 10264b1251bdSJacob Keller if (!vsi->rx_rings[j]) 10274b1251bdSJacob Keller continue; 10284b1251bdSJacob Keller WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime); 10294b1251bdSJacob Keller } 10304b1251bdSJacob Keller } 10314b1251bdSJacob Keller clear_bit(ICE_CFG_BUSY, pf->state); 10324b1251bdSJacob Keller 10334b1251bdSJacob Keller return 0; 10344b1251bdSJacob Keller } 10354b1251bdSJacob Keller 10364b1251bdSJacob Keller /** 1037b1a582e6SJacob Keller * ice_ptp_reset_cached_phctime - Reset cached PHC time after an update 1038b1a582e6SJacob Keller * @pf: Board specific private structure 1039b1a582e6SJacob Keller * 1040b1a582e6SJacob Keller * This function must be called when the cached PHC time is no longer valid, 1041d40fd600SJacob Keller * such as after a time adjustment. It marks any currently outstanding Tx 1042d40fd600SJacob Keller * timestamps as stale and updates the cached PHC time for both the PF and Rx 1043d40fd600SJacob Keller * rings. 1044b1a582e6SJacob Keller * 1045d40fd600SJacob Keller * If updating the PHC time cannot be done immediately, a warning message is 1046d40fd600SJacob Keller * logged and the work item is scheduled immediately to minimize the window 1047d40fd600SJacob Keller * with a wrong cached timestamp. 1048b1a582e6SJacob Keller */ 1049b1a582e6SJacob Keller static void ice_ptp_reset_cached_phctime(struct ice_pf *pf) 1050b1a582e6SJacob Keller { 1051b1a582e6SJacob Keller struct device *dev = ice_pf_to_dev(pf); 1052b1a582e6SJacob Keller int err; 1053b1a582e6SJacob Keller 1054b1a582e6SJacob Keller /* Update the cached PHC time immediately if possible, otherwise 1055b1a582e6SJacob Keller * schedule the work item to execute soon. 1056b1a582e6SJacob Keller */ 1057b1a582e6SJacob Keller err = ice_ptp_update_cached_phctime(pf); 1058b1a582e6SJacob Keller if (err) { 1059b1a582e6SJacob Keller /* If another thread is updating the Rx rings, we won't 1060b1a582e6SJacob Keller * properly reset them here. This could lead to reporting of 1061b1a582e6SJacob Keller * invalid timestamps, but there isn't much we can do. 1062b1a582e6SJacob Keller */ 1063b1a582e6SJacob Keller dev_warn(dev, "%s: ICE_CFG_BUSY, unable to immediately update cached PHC time\n", 1064b1a582e6SJacob Keller __func__); 1065b1a582e6SJacob Keller 1066b1a582e6SJacob Keller /* Queue the work item to update the Rx rings when possible */ 1067b1a582e6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work, 1068b1a582e6SJacob Keller msecs_to_jiffies(10)); 1069b1a582e6SJacob Keller } 1070b1a582e6SJacob Keller 1071d40fd600SJacob Keller /* Mark any outstanding timestamps as stale, since they might have 1072d40fd600SJacob Keller * been captured in hardware before the time update. This could lead 1073d40fd600SJacob Keller * to us extending them with the wrong cached value resulting in 1074d40fd600SJacob Keller * incorrect timestamp values. 1075d40fd600SJacob Keller */ 1076d40fd600SJacob Keller ice_ptp_mark_tx_tracker_stale(&pf->ptp.port.tx); 1077b1a582e6SJacob Keller } 1078b1a582e6SJacob Keller 1079b1a582e6SJacob Keller /** 108006c16d89SJacob Keller * ice_ptp_read_time - Read the time from the device 108106c16d89SJacob Keller * @pf: Board private structure 108206c16d89SJacob Keller * @ts: timespec structure to hold the current time value 108306c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 108406c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 108506c16d89SJacob Keller * 108606c16d89SJacob Keller * This function reads the source clock registers and stores them in a timespec. 108706c16d89SJacob Keller * However, since the registers are 64 bits of nanoseconds, we must convert the 108806c16d89SJacob Keller * result to a timespec before we can return. 108906c16d89SJacob Keller */ 109006c16d89SJacob Keller static void 109106c16d89SJacob Keller ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts, 109206c16d89SJacob Keller struct ptp_system_timestamp *sts) 109306c16d89SJacob Keller { 109406c16d89SJacob Keller u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts); 109506c16d89SJacob Keller 109606c16d89SJacob Keller *ts = ns_to_timespec64(time_ns); 109706c16d89SJacob Keller } 109806c16d89SJacob Keller 109906c16d89SJacob Keller /** 110006c16d89SJacob Keller * ice_ptp_write_init - Set PHC time to provided value 110106c16d89SJacob Keller * @pf: Board private structure 110206c16d89SJacob Keller * @ts: timespec structure that holds the new time value 110306c16d89SJacob Keller * 110406c16d89SJacob Keller * Set the PHC time to the specified time provided in the timespec. 110506c16d89SJacob Keller */ 110606c16d89SJacob Keller static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts) 110706c16d89SJacob Keller { 110806c16d89SJacob Keller u64 ns = timespec64_to_ns(ts); 110906c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 111006c16d89SJacob Keller 111106c16d89SJacob Keller return ice_ptp_init_time(hw, ns); 111206c16d89SJacob Keller } 111306c16d89SJacob Keller 111406c16d89SJacob Keller /** 111506c16d89SJacob Keller * ice_ptp_write_adj - Adjust PHC clock time atomically 111606c16d89SJacob Keller * @pf: Board private structure 111706c16d89SJacob Keller * @adj: Adjustment in nanoseconds 111806c16d89SJacob Keller * 111906c16d89SJacob Keller * Perform an atomic adjustment of the PHC time by the specified number of 112006c16d89SJacob Keller * nanoseconds. 112106c16d89SJacob Keller */ 112206c16d89SJacob Keller static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj) 112306c16d89SJacob Keller { 112406c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 112506c16d89SJacob Keller 112606c16d89SJacob Keller return ice_ptp_adj_clock(hw, adj); 112706c16d89SJacob Keller } 112806c16d89SJacob Keller 112906c16d89SJacob Keller /** 113078267d0cSJacob Keller * ice_base_incval - Get base timer increment value 113178267d0cSJacob Keller * @pf: Board private structure 113278267d0cSJacob Keller * 113378267d0cSJacob Keller * Look up the base timer increment value for this device. The base increment 113478267d0cSJacob Keller * value is used to define the nominal clock tick rate. This increment value 113578267d0cSJacob Keller * is programmed during device initialization. It is also used as the basis 113678267d0cSJacob Keller * for calculating adjustments using scaled_ppm. 113778267d0cSJacob Keller */ 113878267d0cSJacob Keller static u64 ice_base_incval(struct ice_pf *pf) 113978267d0cSJacob Keller { 11403a749623SJacob Keller struct ice_hw *hw = &pf->hw; 11413a749623SJacob Keller u64 incval; 11423a749623SJacob Keller 11433a749623SJacob Keller if (ice_is_e810(hw)) 11443a749623SJacob Keller incval = ICE_PTP_NOMINAL_INCVAL_E810; 11453a749623SJacob Keller else if (ice_e822_time_ref(hw) < NUM_ICE_TIME_REF_FREQ) 11463a749623SJacob Keller incval = ice_e822_nominal_incval(ice_e822_time_ref(hw)); 11473a749623SJacob Keller else 11483a749623SJacob Keller incval = UNKNOWN_INCVAL_E822; 11493a749623SJacob Keller 11503a749623SJacob Keller dev_dbg(ice_pf_to_dev(pf), "PTP: using base increment value of 0x%016llx\n", 11513a749623SJacob Keller incval); 11523a749623SJacob Keller 11533a749623SJacob Keller return incval; 11543a749623SJacob Keller } 11553a749623SJacob Keller 11563a749623SJacob Keller /** 1157a69f1cb6SJacob Keller * ice_ptp_check_tx_fifo - Check whether Tx FIFO is in an OK state 1158a69f1cb6SJacob Keller * @port: PTP port for which Tx FIFO is checked 1159a69f1cb6SJacob Keller */ 1160a69f1cb6SJacob Keller static int ice_ptp_check_tx_fifo(struct ice_ptp_port *port) 1161a69f1cb6SJacob Keller { 1162a69f1cb6SJacob Keller int quad = port->port_num / ICE_PORTS_PER_QUAD; 1163a69f1cb6SJacob Keller int offs = port->port_num % ICE_PORTS_PER_QUAD; 1164a69f1cb6SJacob Keller struct ice_pf *pf; 1165a69f1cb6SJacob Keller struct ice_hw *hw; 1166a69f1cb6SJacob Keller u32 val, phy_sts; 1167a69f1cb6SJacob Keller int err; 1168a69f1cb6SJacob Keller 1169a69f1cb6SJacob Keller pf = ptp_port_to_pf(port); 1170a69f1cb6SJacob Keller hw = &pf->hw; 1171a69f1cb6SJacob Keller 1172a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == FIFO_OK) 1173a69f1cb6SJacob Keller return 0; 1174a69f1cb6SJacob Keller 1175a69f1cb6SJacob Keller /* need to read FIFO state */ 1176a69f1cb6SJacob Keller if (offs == 0 || offs == 1) 1177a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO01_STATUS, 1178a69f1cb6SJacob Keller &val); 1179a69f1cb6SJacob Keller else 1180a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO23_STATUS, 1181a69f1cb6SJacob Keller &val); 1182a69f1cb6SJacob Keller 1183a69f1cb6SJacob Keller if (err) { 1184a69f1cb6SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to check port %d Tx FIFO, err %d\n", 1185a69f1cb6SJacob Keller port->port_num, err); 1186a69f1cb6SJacob Keller return err; 1187a69f1cb6SJacob Keller } 1188a69f1cb6SJacob Keller 1189a69f1cb6SJacob Keller if (offs & 0x1) 1190a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO13_M) >> Q_REG_FIFO13_S; 1191a69f1cb6SJacob Keller else 1192a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO02_M) >> Q_REG_FIFO02_S; 1193a69f1cb6SJacob Keller 1194a69f1cb6SJacob Keller if (phy_sts & FIFO_EMPTY) { 1195a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK; 1196a69f1cb6SJacob Keller return 0; 1197a69f1cb6SJacob Keller } 1198a69f1cb6SJacob Keller 1199a69f1cb6SJacob Keller port->tx_fifo_busy_cnt++; 1200a69f1cb6SJacob Keller 1201a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf), "Try %d, port %d FIFO not empty\n", 1202a69f1cb6SJacob Keller port->tx_fifo_busy_cnt, port->port_num); 1203a69f1cb6SJacob Keller 1204a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == ICE_PTP_FIFO_NUM_CHECKS) { 1205a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf), 1206a69f1cb6SJacob Keller "Port %d Tx FIFO still not empty; resetting quad %d\n", 1207a69f1cb6SJacob Keller port->port_num, quad); 1208407b66c0SKarol Kolacinski ice_ptp_reset_ts_memory_quad_e822(hw, quad); 1209a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK; 1210a69f1cb6SJacob Keller return 0; 1211a69f1cb6SJacob Keller } 1212a69f1cb6SJacob Keller 1213a69f1cb6SJacob Keller return -EAGAIN; 1214a69f1cb6SJacob Keller } 1215a69f1cb6SJacob Keller 1216a69f1cb6SJacob Keller /** 1217*f029a343SSiddaraju DH * ice_ptp_wait_for_offsets - Check for valid Tx and Rx offsets 1218a69f1cb6SJacob Keller * @work: Pointer to the kthread_work structure for this task 1219a69f1cb6SJacob Keller * 1220*f029a343SSiddaraju DH * Check whether hardware has completed measuring the Tx and Rx offset values 1221*f029a343SSiddaraju DH * used to configure and enable vernier timestamp calibration. 1222a69f1cb6SJacob Keller * 1223*f029a343SSiddaraju DH * Once the offset in either direction is measured, configure the associated 1224*f029a343SSiddaraju DH * registers with the calibrated offset values and enable timestamping. The Tx 1225*f029a343SSiddaraju DH * and Rx directions are configured independently as soon as their associated 1226*f029a343SSiddaraju DH * offsets are known. 1227*f029a343SSiddaraju DH * 1228*f029a343SSiddaraju DH * This function reschedules itself until both Tx and Rx calibration have 1229*f029a343SSiddaraju DH * completed. 1230a69f1cb6SJacob Keller */ 1231*f029a343SSiddaraju DH static void ice_ptp_wait_for_offsets(struct kthread_work *work) 1232a69f1cb6SJacob Keller { 1233a69f1cb6SJacob Keller struct ice_ptp_port *port; 1234a69f1cb6SJacob Keller struct ice_pf *pf; 1235a69f1cb6SJacob Keller struct ice_hw *hw; 1236*f029a343SSiddaraju DH int tx_err; 1237*f029a343SSiddaraju DH int rx_err; 1238a69f1cb6SJacob Keller 1239a69f1cb6SJacob Keller port = container_of(work, struct ice_ptp_port, ov_work.work); 1240a69f1cb6SJacob Keller pf = ptp_port_to_pf(port); 1241a69f1cb6SJacob Keller hw = &pf->hw; 1242a69f1cb6SJacob Keller 12430b57e0d4SMichal Michalik if (ice_is_reset_in_progress(pf->state)) 12440b57e0d4SMichal Michalik return; 12450b57e0d4SMichal Michalik 1246*f029a343SSiddaraju DH tx_err = ice_ptp_check_tx_fifo(port); 1247*f029a343SSiddaraju DH if (!tx_err) 1248*f029a343SSiddaraju DH tx_err = ice_phy_cfg_tx_offset_e822(hw, port->port_num); 1249*f029a343SSiddaraju DH rx_err = ice_phy_cfg_rx_offset_e822(hw, port->port_num); 1250*f029a343SSiddaraju DH if (tx_err || rx_err) { 1251*f029a343SSiddaraju DH /* Tx and/or Rx offset not yet configured, try again later */ 1252a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, 1253a69f1cb6SJacob Keller &port->ov_work, 1254a69f1cb6SJacob Keller msecs_to_jiffies(100)); 1255a69f1cb6SJacob Keller return; 1256a69f1cb6SJacob Keller } 1257a69f1cb6SJacob Keller } 1258a69f1cb6SJacob Keller 1259a69f1cb6SJacob Keller /** 12603a749623SJacob Keller * ice_ptp_port_phy_stop - Stop timestamping for a PHY port 12613a749623SJacob Keller * @ptp_port: PTP port to stop 12623a749623SJacob Keller */ 12633a749623SJacob Keller static int 12643a749623SJacob Keller ice_ptp_port_phy_stop(struct ice_ptp_port *ptp_port) 12653a749623SJacob Keller { 12663a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port); 12673a749623SJacob Keller u8 port = ptp_port->port_num; 12683a749623SJacob Keller struct ice_hw *hw = &pf->hw; 12693a749623SJacob Keller int err; 12703a749623SJacob Keller 12713a749623SJacob Keller if (ice_is_e810(hw)) 12723a749623SJacob Keller return 0; 12733a749623SJacob Keller 12743a749623SJacob Keller mutex_lock(&ptp_port->ps_lock); 12753a749623SJacob Keller 1276a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work); 1277a69f1cb6SJacob Keller 12783a749623SJacob Keller err = ice_stop_phy_timer_e822(hw, port, true); 12793a749623SJacob Keller if (err) 12803a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d down, err %d\n", 12813a749623SJacob Keller port, err); 12823a749623SJacob Keller 12833a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock); 12843a749623SJacob Keller 12853a749623SJacob Keller return err; 12863a749623SJacob Keller } 12873a749623SJacob Keller 12883a749623SJacob Keller /** 12893a749623SJacob Keller * ice_ptp_port_phy_restart - (Re)start and calibrate PHY timestamping 12903a749623SJacob Keller * @ptp_port: PTP port for which the PHY start is set 12913a749623SJacob Keller * 12923a749623SJacob Keller * Start the PHY timestamping block, and initiate Vernier timestamping 12933a749623SJacob Keller * calibration. If timestamping cannot be calibrated (such as if link is down) 12943a749623SJacob Keller * then disable the timestamping block instead. 12953a749623SJacob Keller */ 12963a749623SJacob Keller static int 12973a749623SJacob Keller ice_ptp_port_phy_restart(struct ice_ptp_port *ptp_port) 12983a749623SJacob Keller { 12993a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port); 13003a749623SJacob Keller u8 port = ptp_port->port_num; 13013a749623SJacob Keller struct ice_hw *hw = &pf->hw; 13023a749623SJacob Keller int err; 13033a749623SJacob Keller 13043a749623SJacob Keller if (ice_is_e810(hw)) 13053a749623SJacob Keller return 0; 13063a749623SJacob Keller 13073a749623SJacob Keller if (!ptp_port->link_up) 13083a749623SJacob Keller return ice_ptp_port_phy_stop(ptp_port); 13093a749623SJacob Keller 13103a749623SJacob Keller mutex_lock(&ptp_port->ps_lock); 13113a749623SJacob Keller 1312a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work); 1313a69f1cb6SJacob Keller 13143a749623SJacob Keller /* temporarily disable Tx timestamps while calibrating PHY offset */ 13153ad5c10bSJacob Keller spin_lock(&ptp_port->tx.lock); 13163a749623SJacob Keller ptp_port->tx.calibrating = true; 13173ad5c10bSJacob Keller spin_unlock(&ptp_port->tx.lock); 1318a69f1cb6SJacob Keller ptp_port->tx_fifo_busy_cnt = 0; 13193a749623SJacob Keller 13200357d5caSMilena Olech /* Start the PHY timer in Vernier mode */ 13210357d5caSMilena Olech err = ice_start_phy_timer_e822(hw, port); 13223a749623SJacob Keller if (err) 13233a749623SJacob Keller goto out_unlock; 13243a749623SJacob Keller 13253a749623SJacob Keller /* Enable Tx timestamps right away */ 13263ad5c10bSJacob Keller spin_lock(&ptp_port->tx.lock); 13273a749623SJacob Keller ptp_port->tx.calibrating = false; 13283ad5c10bSJacob Keller spin_unlock(&ptp_port->tx.lock); 13293a749623SJacob Keller 1330a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &ptp_port->ov_work, 0); 1331a69f1cb6SJacob Keller 13323a749623SJacob Keller out_unlock: 13333a749623SJacob Keller if (err) 13343a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d up, err %d\n", 13353a749623SJacob Keller port, err); 13363a749623SJacob Keller 13373a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock); 13383a749623SJacob Keller 13393a749623SJacob Keller return err; 13403a749623SJacob Keller } 13413a749623SJacob Keller 13423a749623SJacob Keller /** 13436b1ff5d3SJacob Keller * ice_ptp_link_change - Reconfigure PTP after link status change 13443a749623SJacob Keller * @pf: Board private structure 13453a749623SJacob Keller * @port: Port for which the PHY start is set 13463a749623SJacob Keller * @linkup: Link is up or down 13473a749623SJacob Keller */ 13486b1ff5d3SJacob Keller void ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup) 13493a749623SJacob Keller { 13503a749623SJacob Keller struct ice_ptp_port *ptp_port; 13513a749623SJacob Keller 13526b1ff5d3SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 13536b1ff5d3SJacob Keller return; 13543a749623SJacob Keller 13556b1ff5d3SJacob Keller if (WARN_ON_ONCE(port >= ICE_NUM_EXTERNAL_PORTS)) 13566b1ff5d3SJacob Keller return; 13573a749623SJacob Keller 13583a749623SJacob Keller ptp_port = &pf->ptp.port; 13596b1ff5d3SJacob Keller if (WARN_ON_ONCE(ptp_port->port_num != port)) 13606b1ff5d3SJacob Keller return; 13613a749623SJacob Keller 136211722c39SJacob Keller /* Update cached link status for this port immediately */ 13633a749623SJacob Keller ptp_port->link_up = linkup; 13643a749623SJacob Keller 13656b1ff5d3SJacob Keller /* E810 devices do not need to reconfigure the PHY */ 13666b1ff5d3SJacob Keller if (ice_is_e810(&pf->hw)) 13676b1ff5d3SJacob Keller return; 13683a749623SJacob Keller 13696b1ff5d3SJacob Keller ice_ptp_port_phy_restart(ptp_port); 13703a749623SJacob Keller } 13713a749623SJacob Keller 13723a749623SJacob Keller /** 13733a749623SJacob Keller * ice_ptp_tx_ena_intr - Enable or disable the Tx timestamp interrupt 13743a749623SJacob Keller * @pf: PF private structure 13753a749623SJacob Keller * @ena: bool value to enable or disable interrupt 13763a749623SJacob Keller * @threshold: Minimum number of packets at which intr is triggered 13773a749623SJacob Keller * 13783a749623SJacob Keller * Utility function to enable or disable Tx timestamp interrupt and threshold 13793a749623SJacob Keller */ 13803a749623SJacob Keller static int ice_ptp_tx_ena_intr(struct ice_pf *pf, bool ena, u32 threshold) 13813a749623SJacob Keller { 13823a749623SJacob Keller struct ice_hw *hw = &pf->hw; 13833a749623SJacob Keller int err = 0; 13843a749623SJacob Keller int quad; 13853a749623SJacob Keller u32 val; 13863a749623SJacob Keller 1387407b66c0SKarol Kolacinski ice_ptp_reset_ts_memory(hw); 13883a749623SJacob Keller 13893a749623SJacob Keller for (quad = 0; quad < ICE_MAX_QUAD; quad++) { 13903a749623SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, 13913a749623SJacob Keller &val); 13923a749623SJacob Keller if (err) 13933a749623SJacob Keller break; 13943a749623SJacob Keller 13953a749623SJacob Keller if (ena) { 13963a749623SJacob Keller val |= Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M; 13973a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_THR_M; 13983a749623SJacob Keller val |= ((threshold << Q_REG_TX_MEM_GBL_CFG_INTR_THR_S) & 13993a749623SJacob Keller Q_REG_TX_MEM_GBL_CFG_INTR_THR_M); 14003a749623SJacob Keller } else { 14013a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M; 14023a749623SJacob Keller } 14033a749623SJacob Keller 14043a749623SJacob Keller err = ice_write_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, 14053a749623SJacob Keller val); 14063a749623SJacob Keller if (err) 14073a749623SJacob Keller break; 14083a749623SJacob Keller } 14093a749623SJacob Keller 14103a749623SJacob Keller if (err) 14113a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed in intr ena, err %d\n", 14123a749623SJacob Keller err); 14133a749623SJacob Keller return err; 14143a749623SJacob Keller } 14153a749623SJacob Keller 14163a749623SJacob Keller /** 14173a749623SJacob Keller * ice_ptp_reset_phy_timestamping - Reset PHY timestamping block 14183a749623SJacob Keller * @pf: Board private structure 14193a749623SJacob Keller */ 14203a749623SJacob Keller static void ice_ptp_reset_phy_timestamping(struct ice_pf *pf) 14213a749623SJacob Keller { 14223a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port); 142378267d0cSJacob Keller } 142478267d0cSJacob Keller 142578267d0cSJacob Keller /** 142606c16d89SJacob Keller * ice_ptp_adjfine - Adjust clock increment rate 142706c16d89SJacob Keller * @info: the driver's PTP info structure 142806c16d89SJacob Keller * @scaled_ppm: Parts per million with 16-bit fractional field 142906c16d89SJacob Keller * 143006c16d89SJacob Keller * Adjust the frequency of the clock by the indicated scaled ppm from the 143106c16d89SJacob Keller * base frequency. 143206c16d89SJacob Keller */ 143306c16d89SJacob Keller static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm) 143406c16d89SJacob Keller { 143506c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 143606c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 14371060707eSJacob Keller u64 incval; 143806c16d89SJacob Keller int err; 143906c16d89SJacob Keller 14401060707eSJacob Keller incval = adjust_by_scaled_ppm(ice_base_incval(pf), scaled_ppm); 144106c16d89SJacob Keller err = ice_ptp_write_incval_locked(hw, incval); 144206c16d89SJacob Keller if (err) { 144306c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n", 144406c16d89SJacob Keller err); 144506c16d89SJacob Keller return -EIO; 144606c16d89SJacob Keller } 144706c16d89SJacob Keller 144806c16d89SJacob Keller return 0; 144906c16d89SJacob Keller } 145006c16d89SJacob Keller 145106c16d89SJacob Keller /** 1452172db5f9SMaciej Machnikowski * ice_ptp_extts_work - Workqueue task function 1453172db5f9SMaciej Machnikowski * @work: external timestamp work structure 1454172db5f9SMaciej Machnikowski * 1455172db5f9SMaciej Machnikowski * Service for PTP external clock event 1456172db5f9SMaciej Machnikowski */ 1457172db5f9SMaciej Machnikowski static void ice_ptp_extts_work(struct kthread_work *work) 1458172db5f9SMaciej Machnikowski { 1459172db5f9SMaciej Machnikowski struct ice_ptp *ptp = container_of(work, struct ice_ptp, extts_work); 1460172db5f9SMaciej Machnikowski struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 1461172db5f9SMaciej Machnikowski struct ptp_clock_event event; 1462172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1463172db5f9SMaciej Machnikowski u8 chan, tmr_idx; 1464172db5f9SMaciej Machnikowski u32 hi, lo; 1465172db5f9SMaciej Machnikowski 1466172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1467172db5f9SMaciej Machnikowski /* Event time is captured by one of the two matched registers 1468172db5f9SMaciej Machnikowski * GLTSYN_EVNT_L: 32 LSB of sampled time event 1469172db5f9SMaciej Machnikowski * GLTSYN_EVNT_H: 32 MSB of sampled time event 1470172db5f9SMaciej Machnikowski * Event is defined in GLTSYN_EVNT_0 register 1471172db5f9SMaciej Machnikowski */ 1472172db5f9SMaciej Machnikowski for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) { 1473172db5f9SMaciej Machnikowski /* Check if channel is enabled */ 1474172db5f9SMaciej Machnikowski if (pf->ptp.ext_ts_irq & (1 << chan)) { 1475172db5f9SMaciej Machnikowski lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx)); 1476172db5f9SMaciej Machnikowski hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx)); 1477172db5f9SMaciej Machnikowski event.timestamp = (((u64)hi) << 32) | lo; 1478172db5f9SMaciej Machnikowski event.type = PTP_CLOCK_EXTTS; 1479172db5f9SMaciej Machnikowski event.index = chan; 1480172db5f9SMaciej Machnikowski 1481172db5f9SMaciej Machnikowski /* Fire event */ 1482172db5f9SMaciej Machnikowski ptp_clock_event(pf->ptp.clock, &event); 1483172db5f9SMaciej Machnikowski pf->ptp.ext_ts_irq &= ~(1 << chan); 1484172db5f9SMaciej Machnikowski } 1485172db5f9SMaciej Machnikowski } 1486172db5f9SMaciej Machnikowski } 1487172db5f9SMaciej Machnikowski 1488172db5f9SMaciej Machnikowski /** 1489172db5f9SMaciej Machnikowski * ice_ptp_cfg_extts - Configure EXTTS pin and channel 1490172db5f9SMaciej Machnikowski * @pf: Board private structure 1491172db5f9SMaciej Machnikowski * @ena: true to enable; false to disable 1492172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 1493172db5f9SMaciej Machnikowski * @gpio_pin: GPIO pin 1494172db5f9SMaciej Machnikowski * @extts_flags: request flags from the ptp_extts_request.flags 1495172db5f9SMaciej Machnikowski */ 1496172db5f9SMaciej Machnikowski static int 1497172db5f9SMaciej Machnikowski ice_ptp_cfg_extts(struct ice_pf *pf, bool ena, unsigned int chan, u32 gpio_pin, 1498172db5f9SMaciej Machnikowski unsigned int extts_flags) 1499172db5f9SMaciej Machnikowski { 1500172db5f9SMaciej Machnikowski u32 func, aux_reg, gpio_reg, irq_reg; 1501172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1502172db5f9SMaciej Machnikowski u8 tmr_idx; 1503172db5f9SMaciej Machnikowski 1504172db5f9SMaciej Machnikowski if (chan > (unsigned int)pf->ptp.info.n_ext_ts) 1505172db5f9SMaciej Machnikowski return -EINVAL; 1506172db5f9SMaciej Machnikowski 1507172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1508172db5f9SMaciej Machnikowski 1509172db5f9SMaciej Machnikowski irq_reg = rd32(hw, PFINT_OICR_ENA); 1510172db5f9SMaciej Machnikowski 1511172db5f9SMaciej Machnikowski if (ena) { 1512172db5f9SMaciej Machnikowski /* Enable the interrupt */ 1513172db5f9SMaciej Machnikowski irq_reg |= PFINT_OICR_TSYN_EVNT_M; 1514172db5f9SMaciej Machnikowski aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M; 1515172db5f9SMaciej Machnikowski 1516172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE BIT(0) 1517172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE BIT(1) 1518172db5f9SMaciej Machnikowski 1519172db5f9SMaciej Machnikowski /* set event level to requested edge */ 1520172db5f9SMaciej Machnikowski if (extts_flags & PTP_FALLING_EDGE) 1521172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE; 1522172db5f9SMaciej Machnikowski if (extts_flags & PTP_RISING_EDGE) 1523172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE; 1524172db5f9SMaciej Machnikowski 1525172db5f9SMaciej Machnikowski /* Write GPIO CTL reg. 1526172db5f9SMaciej Machnikowski * 0x1 is input sampled by EVENT register(channel) 1527172db5f9SMaciej Machnikowski * + num_in_channels * tmr_idx 1528172db5f9SMaciej Machnikowski */ 1529172db5f9SMaciej Machnikowski func = 1 + chan + (tmr_idx * 3); 1530172db5f9SMaciej Machnikowski gpio_reg = ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & 1531172db5f9SMaciej Machnikowski GLGEN_GPIO_CTL_PIN_FUNC_M); 1532172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan |= (1 << chan); 1533172db5f9SMaciej Machnikowski } else { 1534172db5f9SMaciej Machnikowski /* clear the values we set to reset defaults */ 1535172db5f9SMaciej Machnikowski aux_reg = 0; 1536172db5f9SMaciej Machnikowski gpio_reg = 0; 1537172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan &= ~(1 << chan); 1538172db5f9SMaciej Machnikowski if (!pf->ptp.ext_ts_chan) 1539172db5f9SMaciej Machnikowski irq_reg &= ~PFINT_OICR_TSYN_EVNT_M; 1540172db5f9SMaciej Machnikowski } 1541172db5f9SMaciej Machnikowski 1542172db5f9SMaciej Machnikowski wr32(hw, PFINT_OICR_ENA, irq_reg); 1543172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg); 1544172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg); 1545172db5f9SMaciej Machnikowski 1546172db5f9SMaciej Machnikowski return 0; 1547172db5f9SMaciej Machnikowski } 1548172db5f9SMaciej Machnikowski 1549172db5f9SMaciej Machnikowski /** 1550172db5f9SMaciej Machnikowski * ice_ptp_cfg_clkout - Configure clock to generate periodic wave 1551172db5f9SMaciej Machnikowski * @pf: Board private structure 1552172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 1553172db5f9SMaciej Machnikowski * @config: desired periodic clk configuration. NULL will disable channel 1554172db5f9SMaciej Machnikowski * @store: If set to true the values will be stored 1555172db5f9SMaciej Machnikowski * 1556172db5f9SMaciej Machnikowski * Configure the internal clock generator modules to generate the clock wave of 1557172db5f9SMaciej Machnikowski * specified period. 1558172db5f9SMaciej Machnikowski */ 1559172db5f9SMaciej Machnikowski static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan, 1560172db5f9SMaciej Machnikowski struct ice_perout_channel *config, bool store) 1561172db5f9SMaciej Machnikowski { 1562172db5f9SMaciej Machnikowski u64 current_time, period, start_time, phase; 1563172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1564172db5f9SMaciej Machnikowski u32 func, val, gpio_pin; 1565172db5f9SMaciej Machnikowski u8 tmr_idx; 1566172db5f9SMaciej Machnikowski 1567172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1568172db5f9SMaciej Machnikowski 1569172db5f9SMaciej Machnikowski /* 0. Reset mode & out_en in AUX_OUT */ 1570172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0); 1571172db5f9SMaciej Machnikowski 1572172db5f9SMaciej Machnikowski /* If we're disabling the output, clear out CLKO and TGT and keep 1573172db5f9SMaciej Machnikowski * output level low 1574172db5f9SMaciej Machnikowski */ 1575172db5f9SMaciej Machnikowski if (!config || !config->ena) { 1576172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), 0); 1577172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), 0); 1578172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), 0); 1579172db5f9SMaciej Machnikowski 1580172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M; 1581172db5f9SMaciej Machnikowski gpio_pin = pf->ptp.perout_channels[chan].gpio_pin; 1582172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 1583172db5f9SMaciej Machnikowski 1584172db5f9SMaciej Machnikowski /* Store the value if requested */ 1585172db5f9SMaciej Machnikowski if (store) 1586172db5f9SMaciej Machnikowski memset(&pf->ptp.perout_channels[chan], 0, 1587172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 1588172db5f9SMaciej Machnikowski 1589172db5f9SMaciej Machnikowski return 0; 1590172db5f9SMaciej Machnikowski } 1591172db5f9SMaciej Machnikowski period = config->period; 1592172db5f9SMaciej Machnikowski start_time = config->start_time; 1593172db5f9SMaciej Machnikowski div64_u64_rem(start_time, period, &phase); 1594172db5f9SMaciej Machnikowski gpio_pin = config->gpio_pin; 1595172db5f9SMaciej Machnikowski 1596172db5f9SMaciej Machnikowski /* 1. Write clkout with half of required period value */ 1597172db5f9SMaciej Machnikowski if (period & 0x1) { 1598172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n"); 1599172db5f9SMaciej Machnikowski goto err; 1600172db5f9SMaciej Machnikowski } 1601172db5f9SMaciej Machnikowski 1602172db5f9SMaciej Machnikowski period >>= 1; 1603172db5f9SMaciej Machnikowski 1604172db5f9SMaciej Machnikowski /* For proper operation, the GLTSYN_CLKO must be larger than clock tick 1605172db5f9SMaciej Machnikowski */ 1606172db5f9SMaciej Machnikowski #define MIN_PULSE 3 1607172db5f9SMaciej Machnikowski if (period <= MIN_PULSE || period > U32_MAX) { 1608172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be > %d && < 2^33", 1609172db5f9SMaciej Machnikowski MIN_PULSE * 2); 1610172db5f9SMaciej Machnikowski goto err; 1611172db5f9SMaciej Machnikowski } 1612172db5f9SMaciej Machnikowski 1613172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period)); 1614172db5f9SMaciej Machnikowski 1615172db5f9SMaciej Machnikowski /* Allow time for programming before start_time is hit */ 1616172db5f9SMaciej Machnikowski current_time = ice_ptp_read_src_clk_reg(pf, NULL); 1617172db5f9SMaciej Machnikowski 1618172db5f9SMaciej Machnikowski /* if start time is in the past start the timer at the nearest second 1619172db5f9SMaciej Machnikowski * maintaining phase 1620172db5f9SMaciej Machnikowski */ 1621172db5f9SMaciej Machnikowski if (start_time < current_time) 16225f773519SMaciej Machnikowski start_time = div64_u64(current_time + NSEC_PER_SEC - 1, 1623172db5f9SMaciej Machnikowski NSEC_PER_SEC) * NSEC_PER_SEC + phase; 1624172db5f9SMaciej Machnikowski 16253a749623SJacob Keller if (ice_is_e810(hw)) 1626172db5f9SMaciej Machnikowski start_time -= E810_OUT_PROP_DELAY_NS; 16273a749623SJacob Keller else 16283a749623SJacob Keller start_time -= ice_e822_pps_delay(ice_e822_time_ref(hw)); 1629172db5f9SMaciej Machnikowski 1630172db5f9SMaciej Machnikowski /* 2. Write TARGET time */ 1631172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start_time)); 1632172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start_time)); 1633172db5f9SMaciej Machnikowski 1634172db5f9SMaciej Machnikowski /* 3. Write AUX_OUT register */ 1635172db5f9SMaciej Machnikowski val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M; 1636172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val); 1637172db5f9SMaciej Machnikowski 1638172db5f9SMaciej Machnikowski /* 4. write GPIO CTL reg */ 1639172db5f9SMaciej Machnikowski func = 8 + chan + (tmr_idx * 4); 1640172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M | 1641172db5f9SMaciej Machnikowski ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & GLGEN_GPIO_CTL_PIN_FUNC_M); 1642172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 1643172db5f9SMaciej Machnikowski 1644172db5f9SMaciej Machnikowski /* Store the value if requested */ 1645172db5f9SMaciej Machnikowski if (store) { 1646172db5f9SMaciej Machnikowski memcpy(&pf->ptp.perout_channels[chan], config, 1647172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 1648172db5f9SMaciej Machnikowski pf->ptp.perout_channels[chan].start_time = phase; 1649172db5f9SMaciej Machnikowski } 1650172db5f9SMaciej Machnikowski 1651172db5f9SMaciej Machnikowski return 0; 1652172db5f9SMaciej Machnikowski err: 1653172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "PTP failed to cfg per_clk\n"); 1654172db5f9SMaciej Machnikowski return -EFAULT; 1655172db5f9SMaciej Machnikowski } 1656172db5f9SMaciej Machnikowski 1657172db5f9SMaciej Machnikowski /** 16589ee31343SJacob Keller * ice_ptp_disable_all_clkout - Disable all currently configured outputs 16599ee31343SJacob Keller * @pf: pointer to the PF structure 16609ee31343SJacob Keller * 16619ee31343SJacob Keller * Disable all currently configured clock outputs. This is necessary before 16629ee31343SJacob Keller * certain changes to the PTP hardware clock. Use ice_ptp_enable_all_clkout to 16639ee31343SJacob Keller * re-enable the clocks again. 16649ee31343SJacob Keller */ 16659ee31343SJacob Keller static void ice_ptp_disable_all_clkout(struct ice_pf *pf) 16669ee31343SJacob Keller { 16679ee31343SJacob Keller uint i; 16689ee31343SJacob Keller 16699ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++) 16709ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena) 16719ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, NULL, false); 16729ee31343SJacob Keller } 16739ee31343SJacob Keller 16749ee31343SJacob Keller /** 16759ee31343SJacob Keller * ice_ptp_enable_all_clkout - Enable all configured periodic clock outputs 16769ee31343SJacob Keller * @pf: pointer to the PF structure 16779ee31343SJacob Keller * 16789ee31343SJacob Keller * Enable all currently configured clock outputs. Use this after 16799ee31343SJacob Keller * ice_ptp_disable_all_clkout to reconfigure the output signals according to 16809ee31343SJacob Keller * their configuration. 16819ee31343SJacob Keller */ 16829ee31343SJacob Keller static void ice_ptp_enable_all_clkout(struct ice_pf *pf) 16839ee31343SJacob Keller { 16849ee31343SJacob Keller uint i; 16859ee31343SJacob Keller 16869ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++) 16879ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena) 16889ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, &pf->ptp.perout_channels[i], 16899ee31343SJacob Keller false); 16909ee31343SJacob Keller } 16919ee31343SJacob Keller 16929ee31343SJacob Keller /** 1693172db5f9SMaciej Machnikowski * ice_ptp_gpio_enable_e810 - Enable/disable ancillary features of PHC 1694172db5f9SMaciej Machnikowski * @info: the driver's PTP info structure 1695172db5f9SMaciej Machnikowski * @rq: The requested feature to change 1696172db5f9SMaciej Machnikowski * @on: Enable/disable flag 1697172db5f9SMaciej Machnikowski */ 1698172db5f9SMaciej Machnikowski static int 1699172db5f9SMaciej Machnikowski ice_ptp_gpio_enable_e810(struct ptp_clock_info *info, 1700172db5f9SMaciej Machnikowski struct ptp_clock_request *rq, int on) 1701172db5f9SMaciej Machnikowski { 1702172db5f9SMaciej Machnikowski struct ice_pf *pf = ptp_info_to_pf(info); 1703172db5f9SMaciej Machnikowski struct ice_perout_channel clk_cfg = {0}; 1704325b2064SMaciej Machnikowski bool sma_pres = false; 1705172db5f9SMaciej Machnikowski unsigned int chan; 1706172db5f9SMaciej Machnikowski u32 gpio_pin; 1707172db5f9SMaciej Machnikowski int err; 1708172db5f9SMaciej Machnikowski 1709325b2064SMaciej Machnikowski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) 1710325b2064SMaciej Machnikowski sma_pres = true; 1711325b2064SMaciej Machnikowski 1712172db5f9SMaciej Machnikowski switch (rq->type) { 1713172db5f9SMaciej Machnikowski case PTP_CLK_REQ_PEROUT: 1714172db5f9SMaciej Machnikowski chan = rq->perout.index; 1715325b2064SMaciej Machnikowski if (sma_pres) { 1716325b2064SMaciej Machnikowski if (chan == ice_pin_desc_e810t[SMA1].chan) 1717325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20; 1718325b2064SMaciej Machnikowski else if (chan == ice_pin_desc_e810t[SMA2].chan) 1719325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22; 1720172db5f9SMaciej Machnikowski else 1721325b2064SMaciej Machnikowski return -1; 1722325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) { 1723325b2064SMaciej Machnikowski if (chan == 0) 1724325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20; 1725325b2064SMaciej Machnikowski else 1726325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22; 1727325b2064SMaciej Machnikowski } else if (chan == PPS_CLK_GEN_CHAN) { 1728325b2064SMaciej Machnikowski clk_cfg.gpio_pin = PPS_PIN_INDEX; 1729325b2064SMaciej Machnikowski } else { 1730172db5f9SMaciej Machnikowski clk_cfg.gpio_pin = chan; 1731325b2064SMaciej Machnikowski } 1732172db5f9SMaciej Machnikowski 1733172db5f9SMaciej Machnikowski clk_cfg.period = ((rq->perout.period.sec * NSEC_PER_SEC) + 1734172db5f9SMaciej Machnikowski rq->perout.period.nsec); 1735172db5f9SMaciej Machnikowski clk_cfg.start_time = ((rq->perout.start.sec * NSEC_PER_SEC) + 1736172db5f9SMaciej Machnikowski rq->perout.start.nsec); 1737172db5f9SMaciej Machnikowski clk_cfg.ena = !!on; 1738172db5f9SMaciej Machnikowski 1739172db5f9SMaciej Machnikowski err = ice_ptp_cfg_clkout(pf, chan, &clk_cfg, true); 1740172db5f9SMaciej Machnikowski break; 1741172db5f9SMaciej Machnikowski case PTP_CLK_REQ_EXTTS: 1742172db5f9SMaciej Machnikowski chan = rq->extts.index; 1743325b2064SMaciej Machnikowski if (sma_pres) { 1744325b2064SMaciej Machnikowski if (chan < ice_pin_desc_e810t[SMA2].chan) 1745325b2064SMaciej Machnikowski gpio_pin = GPIO_21; 1746325b2064SMaciej Machnikowski else 1747325b2064SMaciej Machnikowski gpio_pin = GPIO_23; 1748325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) { 1749325b2064SMaciej Machnikowski if (chan == 0) 1750325b2064SMaciej Machnikowski gpio_pin = GPIO_21; 1751325b2064SMaciej Machnikowski else 1752325b2064SMaciej Machnikowski gpio_pin = GPIO_23; 1753325b2064SMaciej Machnikowski } else { 1754172db5f9SMaciej Machnikowski gpio_pin = chan; 1755325b2064SMaciej Machnikowski } 1756172db5f9SMaciej Machnikowski 1757172db5f9SMaciej Machnikowski err = ice_ptp_cfg_extts(pf, !!on, chan, gpio_pin, 1758172db5f9SMaciej Machnikowski rq->extts.flags); 1759172db5f9SMaciej Machnikowski break; 1760172db5f9SMaciej Machnikowski default: 1761172db5f9SMaciej Machnikowski return -EOPNOTSUPP; 1762172db5f9SMaciej Machnikowski } 1763172db5f9SMaciej Machnikowski 1764172db5f9SMaciej Machnikowski return err; 1765172db5f9SMaciej Machnikowski } 1766172db5f9SMaciej Machnikowski 1767172db5f9SMaciej Machnikowski /** 176806c16d89SJacob Keller * ice_ptp_gettimex64 - Get the time of the clock 176906c16d89SJacob Keller * @info: the driver's PTP info structure 177006c16d89SJacob Keller * @ts: timespec64 structure to hold the current time value 177106c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 177206c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 177306c16d89SJacob Keller * 177406c16d89SJacob Keller * Read the device clock and return the correct value on ns, after converting it 177506c16d89SJacob Keller * into a timespec struct. 177606c16d89SJacob Keller */ 177706c16d89SJacob Keller static int 177806c16d89SJacob Keller ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts, 177906c16d89SJacob Keller struct ptp_system_timestamp *sts) 178006c16d89SJacob Keller { 178106c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 178206c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 178306c16d89SJacob Keller 178406c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 178506c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n"); 178606c16d89SJacob Keller return -EBUSY; 178706c16d89SJacob Keller } 178806c16d89SJacob Keller 178906c16d89SJacob Keller ice_ptp_read_time(pf, ts, sts); 179006c16d89SJacob Keller ice_ptp_unlock(hw); 179106c16d89SJacob Keller 179206c16d89SJacob Keller return 0; 179306c16d89SJacob Keller } 179406c16d89SJacob Keller 179506c16d89SJacob Keller /** 179606c16d89SJacob Keller * ice_ptp_settime64 - Set the time of the clock 179706c16d89SJacob Keller * @info: the driver's PTP info structure 179806c16d89SJacob Keller * @ts: timespec64 structure that holds the new time value 179906c16d89SJacob Keller * 180006c16d89SJacob Keller * Set the device clock to the user input value. The conversion from timespec 180106c16d89SJacob Keller * to ns happens in the write function. 180206c16d89SJacob Keller */ 180306c16d89SJacob Keller static int 180406c16d89SJacob Keller ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts) 180506c16d89SJacob Keller { 180606c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 180706c16d89SJacob Keller struct timespec64 ts64 = *ts; 180806c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 180906c16d89SJacob Keller int err; 181006c16d89SJacob Keller 18113a749623SJacob Keller /* For Vernier mode, we need to recalibrate after new settime 18123a749623SJacob Keller * Start with disabling timestamp block 18133a749623SJacob Keller */ 18143a749623SJacob Keller if (pf->ptp.port.link_up) 18153a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port); 18163a749623SJacob Keller 181706c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 181806c16d89SJacob Keller err = -EBUSY; 181906c16d89SJacob Keller goto exit; 182006c16d89SJacob Keller } 182106c16d89SJacob Keller 18229ee31343SJacob Keller /* Disable periodic outputs */ 18239ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 18249ee31343SJacob Keller 182506c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts64); 182606c16d89SJacob Keller ice_ptp_unlock(hw); 182706c16d89SJacob Keller 182877a78115SJacob Keller if (!err) 1829b1a582e6SJacob Keller ice_ptp_reset_cached_phctime(pf); 183077a78115SJacob Keller 18319ee31343SJacob Keller /* Reenable periodic outputs */ 18329ee31343SJacob Keller ice_ptp_enable_all_clkout(pf); 18333a749623SJacob Keller 18343a749623SJacob Keller /* Recalibrate and re-enable timestamp block */ 18353a749623SJacob Keller if (pf->ptp.port.link_up) 18363a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port); 183706c16d89SJacob Keller exit: 183806c16d89SJacob Keller if (err) { 183906c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err); 184006c16d89SJacob Keller return err; 184106c16d89SJacob Keller } 184206c16d89SJacob Keller 184306c16d89SJacob Keller return 0; 184406c16d89SJacob Keller } 184506c16d89SJacob Keller 184606c16d89SJacob Keller /** 184706c16d89SJacob Keller * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment 184806c16d89SJacob Keller * @info: the driver's PTP info structure 184906c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 185006c16d89SJacob Keller */ 185106c16d89SJacob Keller static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta) 185206c16d89SJacob Keller { 185306c16d89SJacob Keller struct timespec64 now, then; 1854ed22d9c8STom Rix int ret; 185506c16d89SJacob Keller 185606c16d89SJacob Keller then = ns_to_timespec64(delta); 1857ed22d9c8STom Rix ret = ice_ptp_gettimex64(info, &now, NULL); 1858ed22d9c8STom Rix if (ret) 1859ed22d9c8STom Rix return ret; 186006c16d89SJacob Keller now = timespec64_add(now, then); 186106c16d89SJacob Keller 186206c16d89SJacob Keller return ice_ptp_settime64(info, (const struct timespec64 *)&now); 186306c16d89SJacob Keller } 186406c16d89SJacob Keller 186506c16d89SJacob Keller /** 186606c16d89SJacob Keller * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta 186706c16d89SJacob Keller * @info: the driver's PTP info structure 186806c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 186906c16d89SJacob Keller */ 187006c16d89SJacob Keller static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta) 187106c16d89SJacob Keller { 187206c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 187306c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 187406c16d89SJacob Keller struct device *dev; 187506c16d89SJacob Keller int err; 187606c16d89SJacob Keller 187706c16d89SJacob Keller dev = ice_pf_to_dev(pf); 187806c16d89SJacob Keller 187906c16d89SJacob Keller /* Hardware only supports atomic adjustments using signed 32-bit 188006c16d89SJacob Keller * integers. For any adjustment outside this range, perform 188106c16d89SJacob Keller * a non-atomic get->adjust->set flow. 188206c16d89SJacob Keller */ 188306c16d89SJacob Keller if (delta > S32_MAX || delta < S32_MIN) { 188406c16d89SJacob Keller dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta); 188506c16d89SJacob Keller return ice_ptp_adjtime_nonatomic(info, delta); 188606c16d89SJacob Keller } 188706c16d89SJacob Keller 188806c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 188906c16d89SJacob Keller dev_err(dev, "PTP failed to acquire semaphore in adjtime\n"); 189006c16d89SJacob Keller return -EBUSY; 189106c16d89SJacob Keller } 189206c16d89SJacob Keller 18939ee31343SJacob Keller /* Disable periodic outputs */ 18949ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 18959ee31343SJacob Keller 189606c16d89SJacob Keller err = ice_ptp_write_adj(pf, delta); 189706c16d89SJacob Keller 18989ee31343SJacob Keller /* Reenable periodic outputs */ 18999ee31343SJacob Keller ice_ptp_enable_all_clkout(pf); 19009ee31343SJacob Keller 190106c16d89SJacob Keller ice_ptp_unlock(hw); 190206c16d89SJacob Keller 190306c16d89SJacob Keller if (err) { 190406c16d89SJacob Keller dev_err(dev, "PTP failed to adjust time, err %d\n", err); 190506c16d89SJacob Keller return err; 190606c16d89SJacob Keller } 190706c16d89SJacob Keller 1908b1a582e6SJacob Keller ice_ptp_reset_cached_phctime(pf); 190977a78115SJacob Keller 191006c16d89SJacob Keller return 0; 191106c16d89SJacob Keller } 191206c16d89SJacob Keller 191313a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS 191413a64f0bSJacob Keller /** 191513a64f0bSJacob Keller * ice_ptp_get_syncdevicetime - Get the cross time stamp info 191613a64f0bSJacob Keller * @device: Current device time 191713a64f0bSJacob Keller * @system: System counter value read synchronously with device time 191813a64f0bSJacob Keller * @ctx: Context provided by timekeeping code 191913a64f0bSJacob Keller * 192013a64f0bSJacob Keller * Read device and system (ART) clock simultaneously and return the corrected 192113a64f0bSJacob Keller * clock values in ns. 192213a64f0bSJacob Keller */ 192313a64f0bSJacob Keller static int 192413a64f0bSJacob Keller ice_ptp_get_syncdevicetime(ktime_t *device, 192513a64f0bSJacob Keller struct system_counterval_t *system, 192613a64f0bSJacob Keller void *ctx) 192713a64f0bSJacob Keller { 192813a64f0bSJacob Keller struct ice_pf *pf = (struct ice_pf *)ctx; 192913a64f0bSJacob Keller struct ice_hw *hw = &pf->hw; 193013a64f0bSJacob Keller u32 hh_lock, hh_art_ctl; 193113a64f0bSJacob Keller int i; 193213a64f0bSJacob Keller 193313a64f0bSJacob Keller /* Get the HW lock */ 193413a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id)); 193513a64f0bSJacob Keller if (hh_lock & PFHH_SEM_BUSY_M) { 193613a64f0bSJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get hh lock\n"); 193713a64f0bSJacob Keller return -EFAULT; 193813a64f0bSJacob Keller } 193913a64f0bSJacob Keller 194013a64f0bSJacob Keller /* Start the ART and device clock sync sequence */ 194113a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL); 194213a64f0bSJacob Keller hh_art_ctl = hh_art_ctl | GLHH_ART_CTL_ACTIVE_M; 194313a64f0bSJacob Keller wr32(hw, GLHH_ART_CTL, hh_art_ctl); 194413a64f0bSJacob Keller 194513a64f0bSJacob Keller #define MAX_HH_LOCK_TRIES 100 194613a64f0bSJacob Keller 194713a64f0bSJacob Keller for (i = 0; i < MAX_HH_LOCK_TRIES; i++) { 194813a64f0bSJacob Keller /* Wait for sync to complete */ 194913a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL); 195013a64f0bSJacob Keller if (hh_art_ctl & GLHH_ART_CTL_ACTIVE_M) { 195113a64f0bSJacob Keller udelay(1); 195213a64f0bSJacob Keller continue; 195313a64f0bSJacob Keller } else { 195413a64f0bSJacob Keller u32 hh_ts_lo, hh_ts_hi, tmr_idx; 195513a64f0bSJacob Keller u64 hh_ts; 195613a64f0bSJacob Keller 195713a64f0bSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 195813a64f0bSJacob Keller /* Read ART time */ 195913a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLHH_ART_TIME_L); 196013a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLHH_ART_TIME_H); 196113a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo; 196213a64f0bSJacob Keller *system = convert_art_ns_to_tsc(hh_ts); 196313a64f0bSJacob Keller /* Read Device source clock time */ 196413a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLTSYN_HHTIME_L(tmr_idx)); 196513a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLTSYN_HHTIME_H(tmr_idx)); 196613a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo; 196713a64f0bSJacob Keller *device = ns_to_ktime(hh_ts); 196813a64f0bSJacob Keller break; 196913a64f0bSJacob Keller } 197013a64f0bSJacob Keller } 197113a64f0bSJacob Keller /* Release HW lock */ 197213a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id)); 197313a64f0bSJacob Keller hh_lock = hh_lock & ~PFHH_SEM_BUSY_M; 197413a64f0bSJacob Keller wr32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), hh_lock); 197513a64f0bSJacob Keller 197613a64f0bSJacob Keller if (i == MAX_HH_LOCK_TRIES) 197713a64f0bSJacob Keller return -ETIMEDOUT; 197813a64f0bSJacob Keller 197913a64f0bSJacob Keller return 0; 198013a64f0bSJacob Keller } 198113a64f0bSJacob Keller 198213a64f0bSJacob Keller /** 198313a64f0bSJacob Keller * ice_ptp_getcrosststamp_e822 - Capture a device cross timestamp 198413a64f0bSJacob Keller * @info: the driver's PTP info structure 198513a64f0bSJacob Keller * @cts: The memory to fill the cross timestamp info 198613a64f0bSJacob Keller * 198713a64f0bSJacob Keller * Capture a cross timestamp between the ART and the device PTP hardware 198813a64f0bSJacob Keller * clock. Fill the cross timestamp information and report it back to the 198913a64f0bSJacob Keller * caller. 199013a64f0bSJacob Keller * 199113a64f0bSJacob Keller * This is only valid for E822 devices which have support for generating the 199213a64f0bSJacob Keller * cross timestamp via PCIe PTM. 199313a64f0bSJacob Keller * 199413a64f0bSJacob Keller * In order to correctly correlate the ART timestamp back to the TSC time, the 199513a64f0bSJacob Keller * CPU must have X86_FEATURE_TSC_KNOWN_FREQ. 199613a64f0bSJacob Keller */ 199713a64f0bSJacob Keller static int 199813a64f0bSJacob Keller ice_ptp_getcrosststamp_e822(struct ptp_clock_info *info, 199913a64f0bSJacob Keller struct system_device_crosststamp *cts) 200013a64f0bSJacob Keller { 200113a64f0bSJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 200213a64f0bSJacob Keller 200313a64f0bSJacob Keller return get_device_system_crosststamp(ice_ptp_get_syncdevicetime, 200413a64f0bSJacob Keller pf, NULL, cts); 200513a64f0bSJacob Keller } 200613a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */ 200713a64f0bSJacob Keller 200806c16d89SJacob Keller /** 200977a78115SJacob Keller * ice_ptp_get_ts_config - ioctl interface to read the timestamping config 201077a78115SJacob Keller * @pf: Board private structure 201177a78115SJacob Keller * @ifr: ioctl data 201277a78115SJacob Keller * 201377a78115SJacob Keller * Copy the timestamping config to user buffer 201477a78115SJacob Keller */ 201577a78115SJacob Keller int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr) 201677a78115SJacob Keller { 201777a78115SJacob Keller struct hwtstamp_config *config; 201877a78115SJacob Keller 201977a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 202077a78115SJacob Keller return -EIO; 202177a78115SJacob Keller 202277a78115SJacob Keller config = &pf->ptp.tstamp_config; 202377a78115SJacob Keller 202477a78115SJacob Keller return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ? 202577a78115SJacob Keller -EFAULT : 0; 202677a78115SJacob Keller } 202777a78115SJacob Keller 202877a78115SJacob Keller /** 202977a78115SJacob Keller * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode 203077a78115SJacob Keller * @pf: Board private structure 203177a78115SJacob Keller * @config: hwtstamp settings requested or saved 203277a78115SJacob Keller */ 203377a78115SJacob Keller static int 203477a78115SJacob Keller ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config) 203577a78115SJacob Keller { 203677a78115SJacob Keller switch (config->tx_type) { 203777a78115SJacob Keller case HWTSTAMP_TX_OFF: 2038ea9b847cSJacob Keller ice_set_tx_tstamp(pf, false); 2039ea9b847cSJacob Keller break; 2040ea9b847cSJacob Keller case HWTSTAMP_TX_ON: 2041ea9b847cSJacob Keller ice_set_tx_tstamp(pf, true); 204277a78115SJacob Keller break; 204377a78115SJacob Keller default: 204477a78115SJacob Keller return -ERANGE; 204577a78115SJacob Keller } 204677a78115SJacob Keller 204777a78115SJacob Keller switch (config->rx_filter) { 204877a78115SJacob Keller case HWTSTAMP_FILTER_NONE: 204977a78115SJacob Keller ice_set_rx_tstamp(pf, false); 205077a78115SJacob Keller break; 205177a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 205277a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 205377a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 205477a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_EVENT: 205577a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 205677a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 205777a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_SYNC: 205877a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 205977a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 206077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 206177a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 206277a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 206377a78115SJacob Keller case HWTSTAMP_FILTER_NTP_ALL: 206477a78115SJacob Keller case HWTSTAMP_FILTER_ALL: 206577a78115SJacob Keller ice_set_rx_tstamp(pf, true); 206677a78115SJacob Keller break; 206777a78115SJacob Keller default: 206877a78115SJacob Keller return -ERANGE; 206977a78115SJacob Keller } 207077a78115SJacob Keller 207177a78115SJacob Keller return 0; 207277a78115SJacob Keller } 207377a78115SJacob Keller 207477a78115SJacob Keller /** 207577a78115SJacob Keller * ice_ptp_set_ts_config - ioctl interface to control the timestamping 207677a78115SJacob Keller * @pf: Board private structure 207777a78115SJacob Keller * @ifr: ioctl data 207877a78115SJacob Keller * 207977a78115SJacob Keller * Get the user config and store it 208077a78115SJacob Keller */ 208177a78115SJacob Keller int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr) 208277a78115SJacob Keller { 208377a78115SJacob Keller struct hwtstamp_config config; 208477a78115SJacob Keller int err; 208577a78115SJacob Keller 208677a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 208777a78115SJacob Keller return -EAGAIN; 208877a78115SJacob Keller 208977a78115SJacob Keller if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 209077a78115SJacob Keller return -EFAULT; 209177a78115SJacob Keller 209277a78115SJacob Keller err = ice_ptp_set_timestamp_mode(pf, &config); 209377a78115SJacob Keller if (err) 209477a78115SJacob Keller return err; 209577a78115SJacob Keller 2096e59d75ddSJacob Keller /* Return the actual configuration set */ 2097e59d75ddSJacob Keller config = pf->ptp.tstamp_config; 209877a78115SJacob Keller 209977a78115SJacob Keller return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 210077a78115SJacob Keller -EFAULT : 0; 210177a78115SJacob Keller } 210277a78115SJacob Keller 210377a78115SJacob Keller /** 210477a78115SJacob Keller * ice_ptp_rx_hwtstamp - Check for an Rx timestamp 210577a78115SJacob Keller * @rx_ring: Ring to get the VSI info 210677a78115SJacob Keller * @rx_desc: Receive descriptor 210777a78115SJacob Keller * @skb: Particular skb to send timestamp with 210877a78115SJacob Keller * 210977a78115SJacob Keller * The driver receives a notification in the receive descriptor with timestamp. 211077a78115SJacob Keller * The timestamp is in ns, so we must convert the result first. 211177a78115SJacob Keller */ 211277a78115SJacob Keller void 2113e72bba21SMaciej Fijalkowski ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring, 211477a78115SJacob Keller union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) 211577a78115SJacob Keller { 211677a78115SJacob Keller struct skb_shared_hwtstamps *hwtstamps; 2117b1a582e6SJacob Keller u64 ts_ns, cached_time; 2118b1a582e6SJacob Keller u32 ts_high; 211977a78115SJacob Keller 2120b1a582e6SJacob Keller if (!(rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID)) 2121b1a582e6SJacob Keller return; 2122b1a582e6SJacob Keller 2123b1a582e6SJacob Keller cached_time = READ_ONCE(rx_ring->cached_phctime); 2124b1a582e6SJacob Keller 2125b1a582e6SJacob Keller /* Do not report a timestamp if we don't have a cached PHC time */ 2126b1a582e6SJacob Keller if (!cached_time) 2127b1a582e6SJacob Keller return; 2128b1a582e6SJacob Keller 2129b1a582e6SJacob Keller /* Use ice_ptp_extend_32b_ts directly, using the ring-specific cached 2130b1a582e6SJacob Keller * PHC value, rather than accessing the PF. This also allows us to 2131b1a582e6SJacob Keller * simply pass the upper 32bits of nanoseconds directly. Calling 2132b1a582e6SJacob Keller * ice_ptp_extend_40b_ts is unnecessary as it would just discard these 2133b1a582e6SJacob Keller * bits itself. 213477a78115SJacob Keller */ 213577a78115SJacob Keller ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high); 2136b1a582e6SJacob Keller ts_ns = ice_ptp_extend_32b_ts(cached_time, ts_high); 213777a78115SJacob Keller 213877a78115SJacob Keller hwtstamps = skb_hwtstamps(skb); 213977a78115SJacob Keller memset(hwtstamps, 0, sizeof(*hwtstamps)); 214077a78115SJacob Keller hwtstamps->hwtstamp = ns_to_ktime(ts_ns); 214177a78115SJacob Keller } 214277a78115SJacob Keller 214377a78115SJacob Keller /** 2144325b2064SMaciej Machnikowski * ice_ptp_disable_sma_pins_e810t - Disable E810-T SMA pins 2145325b2064SMaciej Machnikowski * @pf: pointer to the PF structure 2146325b2064SMaciej Machnikowski * @info: PTP clock info structure 2147325b2064SMaciej Machnikowski * 2148325b2064SMaciej Machnikowski * Disable the OS access to the SMA pins. Called to clear out the OS 2149325b2064SMaciej Machnikowski * indications of pin support when we fail to setup the E810-T SMA control 2150325b2064SMaciej Machnikowski * register. 2151325b2064SMaciej Machnikowski */ 2152325b2064SMaciej Machnikowski static void 2153325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 2154325b2064SMaciej Machnikowski { 2155325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf); 2156325b2064SMaciej Machnikowski 2157325b2064SMaciej Machnikowski dev_warn(dev, "Failed to configure E810-T SMA pin control\n"); 2158325b2064SMaciej Machnikowski 2159325b2064SMaciej Machnikowski info->enable = NULL; 2160325b2064SMaciej Machnikowski info->verify = NULL; 2161325b2064SMaciej Machnikowski info->n_pins = 0; 2162325b2064SMaciej Machnikowski info->n_ext_ts = 0; 2163325b2064SMaciej Machnikowski info->n_per_out = 0; 2164325b2064SMaciej Machnikowski } 2165325b2064SMaciej Machnikowski 2166325b2064SMaciej Machnikowski /** 2167325b2064SMaciej Machnikowski * ice_ptp_setup_sma_pins_e810t - Setup the SMA pins 2168325b2064SMaciej Machnikowski * @pf: pointer to the PF structure 2169325b2064SMaciej Machnikowski * @info: PTP clock info structure 2170325b2064SMaciej Machnikowski * 2171325b2064SMaciej Machnikowski * Finish setting up the SMA pins by allocating pin_config, and setting it up 2172325b2064SMaciej Machnikowski * according to the current status of the SMA. On failure, disable all of the 2173325b2064SMaciej Machnikowski * extended SMA pin support. 2174325b2064SMaciej Machnikowski */ 2175325b2064SMaciej Machnikowski static void 2176325b2064SMaciej Machnikowski ice_ptp_setup_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 2177325b2064SMaciej Machnikowski { 2178325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf); 2179325b2064SMaciej Machnikowski int err; 2180325b2064SMaciej Machnikowski 2181325b2064SMaciej Machnikowski /* Allocate memory for kernel pins interface */ 2182325b2064SMaciej Machnikowski info->pin_config = devm_kcalloc(dev, info->n_pins, 2183325b2064SMaciej Machnikowski sizeof(*info->pin_config), GFP_KERNEL); 2184325b2064SMaciej Machnikowski if (!info->pin_config) { 2185325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info); 2186325b2064SMaciej Machnikowski return; 2187325b2064SMaciej Machnikowski } 2188325b2064SMaciej Machnikowski 2189325b2064SMaciej Machnikowski /* Read current SMA status */ 2190325b2064SMaciej Machnikowski err = ice_get_sma_config_e810t(&pf->hw, info->pin_config); 2191325b2064SMaciej Machnikowski if (err) 2192325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info); 2193325b2064SMaciej Machnikowski } 2194325b2064SMaciej Machnikowski 2195325b2064SMaciej Machnikowski /** 2196172db5f9SMaciej Machnikowski * ice_ptp_setup_pins_e810 - Setup PTP pins in sysfs 2197896a55aaSAnirudh Venkataramanan * @pf: pointer to the PF instance 2198172db5f9SMaciej Machnikowski * @info: PTP clock capabilities 2199172db5f9SMaciej Machnikowski */ 220043c4958aSArkadiusz Kubalewski static void 220143c4958aSArkadiusz Kubalewski ice_ptp_setup_pins_e810(struct ice_pf *pf, struct ptp_clock_info *info) 2202172db5f9SMaciej Machnikowski { 2203325b2064SMaciej Machnikowski info->n_per_out = N_PER_OUT_E810; 2204896a55aaSAnirudh Venkataramanan 220543c4958aSArkadiusz Kubalewski if (ice_is_feature_supported(pf, ICE_F_PTP_EXTTS)) 2206325b2064SMaciej Machnikowski info->n_ext_ts = N_EXT_TS_E810; 220743c4958aSArkadiusz Kubalewski 220843c4958aSArkadiusz Kubalewski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) { 220943c4958aSArkadiusz Kubalewski info->n_ext_ts = N_EXT_TS_E810; 221043c4958aSArkadiusz Kubalewski info->n_pins = NUM_PTP_PINS_E810T; 221143c4958aSArkadiusz Kubalewski info->verify = ice_verify_pin_e810t; 221243c4958aSArkadiusz Kubalewski 221343c4958aSArkadiusz Kubalewski /* Complete setup of the SMA pins */ 221443c4958aSArkadiusz Kubalewski ice_ptp_setup_sma_pins_e810t(pf, info); 221543c4958aSArkadiusz Kubalewski } 2216172db5f9SMaciej Machnikowski } 2217172db5f9SMaciej Machnikowski 2218172db5f9SMaciej Machnikowski /** 221913a64f0bSJacob Keller * ice_ptp_set_funcs_e822 - Set specialized functions for E822 support 222013a64f0bSJacob Keller * @pf: Board private structure 222113a64f0bSJacob Keller * @info: PTP info to fill 222213a64f0bSJacob Keller * 222313a64f0bSJacob Keller * Assign functions to the PTP capabiltiies structure for E822 devices. 222413a64f0bSJacob Keller * Functions which operate across all device families should be set directly 222513a64f0bSJacob Keller * in ice_ptp_set_caps. Only add functions here which are distinct for E822 222613a64f0bSJacob Keller * devices. 222713a64f0bSJacob Keller */ 222813a64f0bSJacob Keller static void 222913a64f0bSJacob Keller ice_ptp_set_funcs_e822(struct ice_pf *pf, struct ptp_clock_info *info) 223013a64f0bSJacob Keller { 223113a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS 223213a64f0bSJacob Keller if (boot_cpu_has(X86_FEATURE_ART) && 223313a64f0bSJacob Keller boot_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ)) 223413a64f0bSJacob Keller info->getcrosststamp = ice_ptp_getcrosststamp_e822; 223513a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */ 223613a64f0bSJacob Keller } 223713a64f0bSJacob Keller 223813a64f0bSJacob Keller /** 2239172db5f9SMaciej Machnikowski * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support 2240172db5f9SMaciej Machnikowski * @pf: Board private structure 2241172db5f9SMaciej Machnikowski * @info: PTP info to fill 2242172db5f9SMaciej Machnikowski * 2243172db5f9SMaciej Machnikowski * Assign functions to the PTP capabiltiies structure for E810 devices. 2244172db5f9SMaciej Machnikowski * Functions which operate across all device families should be set directly 2245172db5f9SMaciej Machnikowski * in ice_ptp_set_caps. Only add functions here which are distinct for e810 2246172db5f9SMaciej Machnikowski * devices. 2247172db5f9SMaciej Machnikowski */ 2248172db5f9SMaciej Machnikowski static void 2249172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(struct ice_pf *pf, struct ptp_clock_info *info) 2250172db5f9SMaciej Machnikowski { 2251172db5f9SMaciej Machnikowski info->enable = ice_ptp_gpio_enable_e810; 2252896a55aaSAnirudh Venkataramanan ice_ptp_setup_pins_e810(pf, info); 2253172db5f9SMaciej Machnikowski } 2254172db5f9SMaciej Machnikowski 2255172db5f9SMaciej Machnikowski /** 225606c16d89SJacob Keller * ice_ptp_set_caps - Set PTP capabilities 225706c16d89SJacob Keller * @pf: Board private structure 225806c16d89SJacob Keller */ 225906c16d89SJacob Keller static void ice_ptp_set_caps(struct ice_pf *pf) 226006c16d89SJacob Keller { 226106c16d89SJacob Keller struct ptp_clock_info *info = &pf->ptp.info; 226206c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf); 226306c16d89SJacob Keller 226406c16d89SJacob Keller snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk", 226506c16d89SJacob Keller dev_driver_string(dev), dev_name(dev)); 226606c16d89SJacob Keller info->owner = THIS_MODULE; 226706c16d89SJacob Keller info->max_adj = 999999999; 226806c16d89SJacob Keller info->adjtime = ice_ptp_adjtime; 226906c16d89SJacob Keller info->adjfine = ice_ptp_adjfine; 227006c16d89SJacob Keller info->gettimex64 = ice_ptp_gettimex64; 227106c16d89SJacob Keller info->settime64 = ice_ptp_settime64; 2272172db5f9SMaciej Machnikowski 22733a749623SJacob Keller if (ice_is_e810(&pf->hw)) 2274172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(pf, info); 227513a64f0bSJacob Keller else 227613a64f0bSJacob Keller ice_ptp_set_funcs_e822(pf, info); 227706c16d89SJacob Keller } 227806c16d89SJacob Keller 227906c16d89SJacob Keller /** 228006c16d89SJacob Keller * ice_ptp_create_clock - Create PTP clock device for userspace 228106c16d89SJacob Keller * @pf: Board private structure 228206c16d89SJacob Keller * 228306c16d89SJacob Keller * This function creates a new PTP clock device. It only creates one if we 228406c16d89SJacob Keller * don't already have one. Will return error if it can't create one, but success 228506c16d89SJacob Keller * if we already have a device. Should be used by ice_ptp_init to create clock 228606c16d89SJacob Keller * initially, and prevent global resets from creating new clock devices. 228706c16d89SJacob Keller */ 228806c16d89SJacob Keller static long ice_ptp_create_clock(struct ice_pf *pf) 228906c16d89SJacob Keller { 229006c16d89SJacob Keller struct ptp_clock_info *info; 229106c16d89SJacob Keller struct ptp_clock *clock; 229206c16d89SJacob Keller struct device *dev; 229306c16d89SJacob Keller 229406c16d89SJacob Keller /* No need to create a clock device if we already have one */ 229506c16d89SJacob Keller if (pf->ptp.clock) 229606c16d89SJacob Keller return 0; 229706c16d89SJacob Keller 229806c16d89SJacob Keller ice_ptp_set_caps(pf); 229906c16d89SJacob Keller 230006c16d89SJacob Keller info = &pf->ptp.info; 230106c16d89SJacob Keller dev = ice_pf_to_dev(pf); 230206c16d89SJacob Keller 230306c16d89SJacob Keller /* Attempt to register the clock before enabling the hardware. */ 230406c16d89SJacob Keller clock = ptp_clock_register(info, dev); 230506c16d89SJacob Keller if (IS_ERR(clock)) 230606c16d89SJacob Keller return PTR_ERR(clock); 230706c16d89SJacob Keller 230806c16d89SJacob Keller pf->ptp.clock = clock; 230906c16d89SJacob Keller 231006c16d89SJacob Keller return 0; 231106c16d89SJacob Keller } 231206c16d89SJacob Keller 2313ea9b847cSJacob Keller /** 2314ea9b847cSJacob Keller * ice_ptp_request_ts - Request an available Tx timestamp index 2315ea9b847cSJacob Keller * @tx: the PTP Tx timestamp tracker to request from 2316ea9b847cSJacob Keller * @skb: the SKB to associate with this timestamp request 2317ea9b847cSJacob Keller */ 2318ea9b847cSJacob Keller s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb) 2319ea9b847cSJacob Keller { 2320ea9b847cSJacob Keller u8 idx; 2321ea9b847cSJacob Keller 2322ea9b847cSJacob Keller spin_lock(&tx->lock); 23233ad5c10bSJacob Keller 23243ad5c10bSJacob Keller /* Check that this tracker is accepting new timestamp requests */ 23253ad5c10bSJacob Keller if (!ice_ptp_is_tx_tracker_up(tx)) { 23263ad5c10bSJacob Keller spin_unlock(&tx->lock); 23273ad5c10bSJacob Keller return -1; 23283ad5c10bSJacob Keller } 23293ad5c10bSJacob Keller 2330ea9b847cSJacob Keller /* Find and set the first available index */ 2331ea9b847cSJacob Keller idx = find_first_zero_bit(tx->in_use, tx->len); 2332ea9b847cSJacob Keller if (idx < tx->len) { 2333ea9b847cSJacob Keller /* We got a valid index that no other thread could have set. Store 2334ea9b847cSJacob Keller * a reference to the skb and the start time to allow discarding old 2335ea9b847cSJacob Keller * requests. 2336ea9b847cSJacob Keller */ 2337ea9b847cSJacob Keller set_bit(idx, tx->in_use); 2338d40fd600SJacob Keller clear_bit(idx, tx->stale); 2339ea9b847cSJacob Keller tx->tstamps[idx].start = jiffies; 2340ea9b847cSJacob Keller tx->tstamps[idx].skb = skb_get(skb); 2341ea9b847cSJacob Keller skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 23424c120218SJacob Keller ice_trace(tx_tstamp_request, skb, idx); 2343ea9b847cSJacob Keller } 2344ea9b847cSJacob Keller 2345ea9b847cSJacob Keller spin_unlock(&tx->lock); 2346ea9b847cSJacob Keller 2347ea9b847cSJacob Keller /* return the appropriate PHY timestamp register index, -1 if no 2348ea9b847cSJacob Keller * indexes were available. 2349ea9b847cSJacob Keller */ 2350ea9b847cSJacob Keller if (idx >= tx->len) 2351ea9b847cSJacob Keller return -1; 2352ea9b847cSJacob Keller else 23536b5cbc8cSSergey Temerkhanov return idx + tx->offset; 2354ea9b847cSJacob Keller } 2355ea9b847cSJacob Keller 2356ea9b847cSJacob Keller /** 23571229b339SKarol Kolacinski * ice_ptp_process_ts - Process the PTP Tx timestamps 2358ea9b847cSJacob Keller * @pf: Board private structure 2359ea9b847cSJacob Keller * 23601229b339SKarol Kolacinski * Returns true if timestamps are processed. 2361ea9b847cSJacob Keller */ 23621229b339SKarol Kolacinski bool ice_ptp_process_ts(struct ice_pf *pf) 2363ea9b847cSJacob Keller { 23641229b339SKarol Kolacinski return ice_ptp_tx_tstamp(&pf->ptp.port.tx); 2365ea9b847cSJacob Keller } 2366ea9b847cSJacob Keller 236777a78115SJacob Keller static void ice_ptp_periodic_work(struct kthread_work *work) 236877a78115SJacob Keller { 236977a78115SJacob Keller struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work); 237077a78115SJacob Keller struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 23714503cc7fSArkadiusz Kubalewski int err; 237277a78115SJacob Keller 237377a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 237477a78115SJacob Keller return; 237577a78115SJacob Keller 23764503cc7fSArkadiusz Kubalewski err = ice_ptp_update_cached_phctime(pf); 237777a78115SJacob Keller 23784503cc7fSArkadiusz Kubalewski /* Run twice a second or reschedule if phc update failed */ 237977a78115SJacob Keller kthread_queue_delayed_work(ptp->kworker, &ptp->work, 23804503cc7fSArkadiusz Kubalewski msecs_to_jiffies(err ? 10 : 500)); 238177a78115SJacob Keller } 238277a78115SJacob Keller 238306c16d89SJacob Keller /** 238448096710SKarol Kolacinski * ice_ptp_reset - Initialize PTP hardware clock support after reset 238548096710SKarol Kolacinski * @pf: Board private structure 238648096710SKarol Kolacinski */ 238748096710SKarol Kolacinski void ice_ptp_reset(struct ice_pf *pf) 238848096710SKarol Kolacinski { 238948096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 239048096710SKarol Kolacinski struct ice_hw *hw = &pf->hw; 239148096710SKarol Kolacinski struct timespec64 ts; 23923a749623SJacob Keller int err, itr = 1; 239348096710SKarol Kolacinski u64 time_diff; 239448096710SKarol Kolacinski 239548096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state)) 239648096710SKarol Kolacinski goto pfr; 239748096710SKarol Kolacinski 2398b2ee7256SJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned) 23993a749623SJacob Keller goto reset_ts; 240048096710SKarol Kolacinski 2401b2ee7256SJacob Keller err = ice_ptp_init_phc(hw); 240248096710SKarol Kolacinski if (err) 240348096710SKarol Kolacinski goto err; 240448096710SKarol Kolacinski 240548096710SKarol Kolacinski /* Acquire the global hardware lock */ 240648096710SKarol Kolacinski if (!ice_ptp_lock(hw)) { 240748096710SKarol Kolacinski err = -EBUSY; 240848096710SKarol Kolacinski goto err; 240948096710SKarol Kolacinski } 241048096710SKarol Kolacinski 241148096710SKarol Kolacinski /* Write the increment time value to PHY and LAN */ 241278267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf)); 241348096710SKarol Kolacinski if (err) { 241448096710SKarol Kolacinski ice_ptp_unlock(hw); 241548096710SKarol Kolacinski goto err; 241648096710SKarol Kolacinski } 241748096710SKarol Kolacinski 241848096710SKarol Kolacinski /* Write the initial Time value to PHY and LAN using the cached PHC 241948096710SKarol Kolacinski * time before the reset and time difference between stopping and 242048096710SKarol Kolacinski * starting the clock. 242148096710SKarol Kolacinski */ 242248096710SKarol Kolacinski if (ptp->cached_phc_time) { 242348096710SKarol Kolacinski time_diff = ktime_get_real_ns() - ptp->reset_time; 242448096710SKarol Kolacinski ts = ns_to_timespec64(ptp->cached_phc_time + time_diff); 242548096710SKarol Kolacinski } else { 242648096710SKarol Kolacinski ts = ktime_to_timespec64(ktime_get_real()); 242748096710SKarol Kolacinski } 242848096710SKarol Kolacinski err = ice_ptp_write_init(pf, &ts); 242948096710SKarol Kolacinski if (err) { 243048096710SKarol Kolacinski ice_ptp_unlock(hw); 243148096710SKarol Kolacinski goto err; 243248096710SKarol Kolacinski } 243348096710SKarol Kolacinski 243448096710SKarol Kolacinski /* Release the global hardware lock */ 243548096710SKarol Kolacinski ice_ptp_unlock(hw); 243648096710SKarol Kolacinski 24373a749623SJacob Keller if (!ice_is_e810(hw)) { 24383a749623SJacob Keller /* Enable quad interrupts */ 24393a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr); 24403a749623SJacob Keller if (err) 24413a749623SJacob Keller goto err; 24423a749623SJacob Keller } 24433a749623SJacob Keller 24443a749623SJacob Keller reset_ts: 24453a749623SJacob Keller /* Restart the PHY timestamping block */ 24463a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf); 24473a749623SJacob Keller 244848096710SKarol Kolacinski pfr: 244948096710SKarol Kolacinski /* Init Tx structures */ 2450a69f1cb6SJacob Keller if (ice_is_e810(&pf->hw)) { 245148096710SKarol Kolacinski err = ice_ptp_init_tx_e810(pf, &ptp->port.tx); 2452a69f1cb6SJacob Keller } else { 2453a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp->port.ov_work, 2454*f029a343SSiddaraju DH ice_ptp_wait_for_offsets); 24553a749623SJacob Keller err = ice_ptp_init_tx_e822(pf, &ptp->port.tx, 24563a749623SJacob Keller ptp->port.port_num); 2457a69f1cb6SJacob Keller } 245848096710SKarol Kolacinski if (err) 245948096710SKarol Kolacinski goto err; 246048096710SKarol Kolacinski 246148096710SKarol Kolacinski set_bit(ICE_FLAG_PTP, pf->flags); 246248096710SKarol Kolacinski 246348096710SKarol Kolacinski /* Start periodic work going */ 246448096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0); 246548096710SKarol Kolacinski 246648096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP reset successful\n"); 246748096710SKarol Kolacinski return; 246848096710SKarol Kolacinski 246948096710SKarol Kolacinski err: 247048096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP reset failed %d\n", err); 247148096710SKarol Kolacinski } 247248096710SKarol Kolacinski 247348096710SKarol Kolacinski /** 247448096710SKarol Kolacinski * ice_ptp_prepare_for_reset - Prepare PTP for reset 247548096710SKarol Kolacinski * @pf: Board private structure 247648096710SKarol Kolacinski */ 247748096710SKarol Kolacinski void ice_ptp_prepare_for_reset(struct ice_pf *pf) 247848096710SKarol Kolacinski { 247948096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 248048096710SKarol Kolacinski u8 src_tmr; 248148096710SKarol Kolacinski 248248096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags); 248348096710SKarol Kolacinski 248448096710SKarol Kolacinski /* Disable timestamping for both Tx and Rx */ 248548096710SKarol Kolacinski ice_ptp_cfg_timestamp(pf, false); 248648096710SKarol Kolacinski 248748096710SKarol Kolacinski kthread_cancel_delayed_work_sync(&ptp->work); 248848096710SKarol Kolacinski kthread_cancel_work_sync(&ptp->extts_work); 248948096710SKarol Kolacinski 249048096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state)) 249148096710SKarol Kolacinski return; 249248096710SKarol Kolacinski 249348096710SKarol Kolacinski ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 249448096710SKarol Kolacinski 249548096710SKarol Kolacinski /* Disable periodic outputs */ 249648096710SKarol Kolacinski ice_ptp_disable_all_clkout(pf); 249748096710SKarol Kolacinski 249848096710SKarol Kolacinski src_tmr = ice_get_ptp_src_clock_index(&pf->hw); 249948096710SKarol Kolacinski 250048096710SKarol Kolacinski /* Disable source clock */ 250148096710SKarol Kolacinski wr32(&pf->hw, GLTSYN_ENA(src_tmr), (u32)~GLTSYN_ENA_TSYN_ENA_M); 250248096710SKarol Kolacinski 250348096710SKarol Kolacinski /* Acquire PHC and system timer to restore after reset */ 250448096710SKarol Kolacinski ptp->reset_time = ktime_get_real_ns(); 250548096710SKarol Kolacinski } 250648096710SKarol Kolacinski 250748096710SKarol Kolacinski /** 250806c16d89SJacob Keller * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device 250906c16d89SJacob Keller * @pf: Board private structure 251006c16d89SJacob Keller * 251106c16d89SJacob Keller * Setup and initialize a PTP clock device that represents the device hardware 251206c16d89SJacob Keller * clock. Save the clock index for other functions connected to the same 251306c16d89SJacob Keller * hardware resource. 251406c16d89SJacob Keller */ 251506c16d89SJacob Keller static int ice_ptp_init_owner(struct ice_pf *pf) 251606c16d89SJacob Keller { 251706c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 251806c16d89SJacob Keller struct timespec64 ts; 25193a749623SJacob Keller int err, itr = 1; 252006c16d89SJacob Keller 2521b2ee7256SJacob Keller err = ice_ptp_init_phc(hw); 2522b2ee7256SJacob Keller if (err) { 2523b2ee7256SJacob Keller dev_err(ice_pf_to_dev(pf), "Failed to initialize PHC, err %d\n", 2524b2ee7256SJacob Keller err); 2525b2ee7256SJacob Keller return err; 2526b2ee7256SJacob Keller } 252706c16d89SJacob Keller 252806c16d89SJacob Keller /* Acquire the global hardware lock */ 252906c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 253006c16d89SJacob Keller err = -EBUSY; 253106c16d89SJacob Keller goto err_exit; 253206c16d89SJacob Keller } 253306c16d89SJacob Keller 253406c16d89SJacob Keller /* Write the increment time value to PHY and LAN */ 253578267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf)); 253606c16d89SJacob Keller if (err) { 253706c16d89SJacob Keller ice_ptp_unlock(hw); 253806c16d89SJacob Keller goto err_exit; 253906c16d89SJacob Keller } 254006c16d89SJacob Keller 254106c16d89SJacob Keller ts = ktime_to_timespec64(ktime_get_real()); 254206c16d89SJacob Keller /* Write the initial Time value to PHY and LAN */ 254306c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts); 254406c16d89SJacob Keller if (err) { 254506c16d89SJacob Keller ice_ptp_unlock(hw); 254606c16d89SJacob Keller goto err_exit; 254706c16d89SJacob Keller } 254806c16d89SJacob Keller 254906c16d89SJacob Keller /* Release the global hardware lock */ 255006c16d89SJacob Keller ice_ptp_unlock(hw); 255106c16d89SJacob Keller 25523a749623SJacob Keller if (!ice_is_e810(hw)) { 25533a749623SJacob Keller /* Enable quad interrupts */ 25543a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr); 25553a749623SJacob Keller if (err) 25563a749623SJacob Keller goto err_exit; 25573a749623SJacob Keller } 25583a749623SJacob Keller 255906c16d89SJacob Keller /* Ensure we have a clock device */ 256006c16d89SJacob Keller err = ice_ptp_create_clock(pf); 256106c16d89SJacob Keller if (err) 256206c16d89SJacob Keller goto err_clk; 256306c16d89SJacob Keller 256467569a7fSJacob Keller /* Store the PTP clock index for other PFs */ 256567569a7fSJacob Keller ice_set_ptp_clock_index(pf); 256667569a7fSJacob Keller 256706c16d89SJacob Keller return 0; 256806c16d89SJacob Keller 256906c16d89SJacob Keller err_clk: 257006c16d89SJacob Keller pf->ptp.clock = NULL; 257106c16d89SJacob Keller err_exit: 257206c16d89SJacob Keller return err; 257306c16d89SJacob Keller } 257406c16d89SJacob Keller 257506c16d89SJacob Keller /** 257648096710SKarol Kolacinski * ice_ptp_init_work - Initialize PTP work threads 257748096710SKarol Kolacinski * @pf: Board private structure 257848096710SKarol Kolacinski * @ptp: PF PTP structure 257948096710SKarol Kolacinski */ 258048096710SKarol Kolacinski static int ice_ptp_init_work(struct ice_pf *pf, struct ice_ptp *ptp) 258148096710SKarol Kolacinski { 258248096710SKarol Kolacinski struct kthread_worker *kworker; 258348096710SKarol Kolacinski 258448096710SKarol Kolacinski /* Initialize work functions */ 258548096710SKarol Kolacinski kthread_init_delayed_work(&ptp->work, ice_ptp_periodic_work); 258648096710SKarol Kolacinski kthread_init_work(&ptp->extts_work, ice_ptp_extts_work); 258748096710SKarol Kolacinski 258848096710SKarol Kolacinski /* Allocate a kworker for handling work required for the ports 258948096710SKarol Kolacinski * connected to the PTP hardware clock. 259048096710SKarol Kolacinski */ 259148096710SKarol Kolacinski kworker = kthread_create_worker(0, "ice-ptp-%s", 259248096710SKarol Kolacinski dev_name(ice_pf_to_dev(pf))); 259348096710SKarol Kolacinski if (IS_ERR(kworker)) 259448096710SKarol Kolacinski return PTR_ERR(kworker); 259548096710SKarol Kolacinski 259648096710SKarol Kolacinski ptp->kworker = kworker; 259748096710SKarol Kolacinski 259848096710SKarol Kolacinski /* Start periodic work going */ 259948096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0); 260048096710SKarol Kolacinski 260148096710SKarol Kolacinski return 0; 260248096710SKarol Kolacinski } 260348096710SKarol Kolacinski 260448096710SKarol Kolacinski /** 26053a749623SJacob Keller * ice_ptp_init_port - Initialize PTP port structure 26063a749623SJacob Keller * @pf: Board private structure 26073a749623SJacob Keller * @ptp_port: PTP port structure 26083a749623SJacob Keller */ 26093a749623SJacob Keller static int ice_ptp_init_port(struct ice_pf *pf, struct ice_ptp_port *ptp_port) 26103a749623SJacob Keller { 26113a749623SJacob Keller mutex_init(&ptp_port->ps_lock); 26123a749623SJacob Keller 26133a749623SJacob Keller if (ice_is_e810(&pf->hw)) 26143a749623SJacob Keller return ice_ptp_init_tx_e810(pf, &ptp_port->tx); 26153a749623SJacob Keller 2616a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp_port->ov_work, 2617*f029a343SSiddaraju DH ice_ptp_wait_for_offsets); 26183a749623SJacob Keller return ice_ptp_init_tx_e822(pf, &ptp_port->tx, ptp_port->port_num); 26193a749623SJacob Keller } 26203a749623SJacob Keller 26213a749623SJacob Keller /** 2622b2ee7256SJacob Keller * ice_ptp_init - Initialize PTP hardware clock support 262306c16d89SJacob Keller * @pf: Board private structure 262406c16d89SJacob Keller * 2625b2ee7256SJacob Keller * Set up the device for interacting with the PTP hardware clock for all 2626b2ee7256SJacob Keller * functions, both the function that owns the clock hardware, and the 2627b2ee7256SJacob Keller * functions connected to the clock hardware. 2628b2ee7256SJacob Keller * 2629b2ee7256SJacob Keller * The clock owner will allocate and register a ptp_clock with the 2630b2ee7256SJacob Keller * PTP_1588_CLOCK infrastructure. All functions allocate a kthread and work 2631b2ee7256SJacob Keller * items used for asynchronous work such as Tx timestamps and periodic work. 263206c16d89SJacob Keller */ 263306c16d89SJacob Keller void ice_ptp_init(struct ice_pf *pf) 263406c16d89SJacob Keller { 263548096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 263606c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 263706c16d89SJacob Keller int err; 263806c16d89SJacob Keller 2639b2ee7256SJacob Keller /* If this function owns the clock hardware, it must allocate and 2640b2ee7256SJacob Keller * configure the PTP clock device to represent it. 2641b2ee7256SJacob Keller */ 264206c16d89SJacob Keller if (hw->func_caps.ts_func_info.src_tmr_owned) { 264306c16d89SJacob Keller err = ice_ptp_init_owner(pf); 264406c16d89SJacob Keller if (err) 264548096710SKarol Kolacinski goto err; 264606c16d89SJacob Keller } 264706c16d89SJacob Keller 26483a749623SJacob Keller ptp->port.port_num = hw->pf_id; 26493a749623SJacob Keller err = ice_ptp_init_port(pf, &ptp->port); 265048096710SKarol Kolacinski if (err) 265148096710SKarol Kolacinski goto err; 265277a78115SJacob Keller 26533a749623SJacob Keller /* Start the PHY timestamping block */ 26543a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf); 26553a749623SJacob Keller 265606c16d89SJacob Keller set_bit(ICE_FLAG_PTP, pf->flags); 265748096710SKarol Kolacinski err = ice_ptp_init_work(pf, ptp); 265848096710SKarol Kolacinski if (err) 265948096710SKarol Kolacinski goto err; 266006c16d89SJacob Keller 266148096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP init successful\n"); 266277a78115SJacob Keller return; 266377a78115SJacob Keller 266448096710SKarol Kolacinski err: 266577a78115SJacob Keller /* If we registered a PTP clock, release it */ 266677a78115SJacob Keller if (pf->ptp.clock) { 266748096710SKarol Kolacinski ptp_clock_unregister(ptp->clock); 266877a78115SJacob Keller pf->ptp.clock = NULL; 266977a78115SJacob Keller } 267048096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags); 267148096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP failed %d\n", err); 267206c16d89SJacob Keller } 267306c16d89SJacob Keller 267406c16d89SJacob Keller /** 267506c16d89SJacob Keller * ice_ptp_release - Disable the driver/HW support and unregister the clock 267606c16d89SJacob Keller * @pf: Board private structure 267706c16d89SJacob Keller * 267806c16d89SJacob Keller * This function handles the cleanup work required from the initialization by 267906c16d89SJacob Keller * clearing out the important information and unregistering the clock 268006c16d89SJacob Keller */ 268106c16d89SJacob Keller void ice_ptp_release(struct ice_pf *pf) 268206c16d89SJacob Keller { 2683fd1b5bebSYongxin Liu if (!test_bit(ICE_FLAG_PTP, pf->flags)) 2684fd1b5bebSYongxin Liu return; 2685fd1b5bebSYongxin Liu 268677a78115SJacob Keller /* Disable timestamping for both Tx and Rx */ 268777a78115SJacob Keller ice_ptp_cfg_timestamp(pf, false); 268877a78115SJacob Keller 2689ea9b847cSJacob Keller ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 2690ea9b847cSJacob Keller 269106c16d89SJacob Keller clear_bit(ICE_FLAG_PTP, pf->flags); 269206c16d89SJacob Keller 269377a78115SJacob Keller kthread_cancel_delayed_work_sync(&pf->ptp.work); 269477a78115SJacob Keller 26953a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port); 26963a749623SJacob Keller mutex_destroy(&pf->ptp.port.ps_lock); 269777a78115SJacob Keller if (pf->ptp.kworker) { 269877a78115SJacob Keller kthread_destroy_worker(pf->ptp.kworker); 269977a78115SJacob Keller pf->ptp.kworker = NULL; 270077a78115SJacob Keller } 270177a78115SJacob Keller 270206c16d89SJacob Keller if (!pf->ptp.clock) 270306c16d89SJacob Keller return; 270406c16d89SJacob Keller 27059ee31343SJacob Keller /* Disable periodic outputs */ 27069ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 27079ee31343SJacob Keller 270867569a7fSJacob Keller ice_clear_ptp_clock_index(pf); 270906c16d89SJacob Keller ptp_clock_unregister(pf->ptp.clock); 271006c16d89SJacob Keller pf->ptp.clock = NULL; 271106c16d89SJacob Keller 271206c16d89SJacob Keller dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n"); 271306c16d89SJacob Keller } 2714