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 /** 6031229b339SKarol Kolacinski * ice_ptp_tx_tstamp - Process Tx timestamps for a port 6041229b339SKarol Kolacinski * @tx: the PTP Tx timestamp tracker 6054b1251bdSJacob Keller * 6064b1251bdSJacob Keller * Process timestamps captured by the PHY associated with this port. To do 6074b1251bdSJacob Keller * this, loop over each index with a waiting skb. 6084b1251bdSJacob Keller * 6094b1251bdSJacob Keller * If a given index has a valid timestamp, perform the following steps: 6104b1251bdSJacob Keller * 6114b1251bdSJacob Keller * 1) copy the timestamp out of the PHY register 6124b1251bdSJacob Keller * 4) clear the timestamp valid bit in the PHY register 6134b1251bdSJacob Keller * 5) unlock the index by clearing the associated in_use bit. 6144b1251bdSJacob Keller * 2) extend the 40b timestamp value to get a 64bit timestamp 6154b1251bdSJacob Keller * 3) send that timestamp to the stack 6164b1251bdSJacob Keller * 61730f15874SJacob Keller * Returns true if all timestamps were handled, and false if any slots remain 61830f15874SJacob Keller * without a timestamp. 61930f15874SJacob Keller * 62030f15874SJacob Keller * After looping, if we still have waiting SKBs, return false. This may cause 62130f15874SJacob Keller * us effectively poll even when not strictly necessary. We do this because 62230f15874SJacob Keller * it's possible a new timestamp was requested around the same time as the 62330f15874SJacob Keller * interrupt. In some cases hardware might not interrupt us again when the 62430f15874SJacob Keller * timestamp is captured. 6254b1251bdSJacob Keller * 6264b1251bdSJacob Keller * Note that we only take the tracking lock when clearing the bit and when 6274b1251bdSJacob Keller * checking if we need to re-queue this task. The only place where bits can be 6284b1251bdSJacob Keller * set is the hard xmit routine where an SKB has a request flag set. The only 6294b1251bdSJacob Keller * places where we clear bits are this work function, or the periodic cleanup 6304b1251bdSJacob Keller * thread. If the cleanup thread clears a bit we're processing we catch it 6314b1251bdSJacob Keller * when we lock to clear the bit and then grab the SKB pointer. If a Tx thread 6324b1251bdSJacob Keller * starts a new timestamp, we might not begin processing it right away but we 6331229b339SKarol Kolacinski * will notice it at the end when we re-queue the task. If a Tx thread starts 6341229b339SKarol Kolacinski * a new timestamp just after this function exits without re-queuing, 6354b1251bdSJacob Keller * the interrupt when the timestamp finishes should trigger. Avoiding holding 6364b1251bdSJacob Keller * the lock for the entire function is important in order to ensure that Tx 6374b1251bdSJacob Keller * threads do not get blocked while waiting for the lock. 6384b1251bdSJacob Keller */ 6391229b339SKarol Kolacinski static bool ice_ptp_tx_tstamp(struct ice_ptp_tx *tx) 6404b1251bdSJacob Keller { 6414b1251bdSJacob Keller struct ice_ptp_port *ptp_port; 6421229b339SKarol Kolacinski bool ts_handled = true; 6434b1251bdSJacob Keller struct ice_pf *pf; 6444b1251bdSJacob Keller u8 idx; 6454b1251bdSJacob Keller 6464b1251bdSJacob Keller if (!tx->init) 64730f15874SJacob Keller return true; 6484b1251bdSJacob Keller 6494b1251bdSJacob Keller ptp_port = container_of(tx, struct ice_ptp_port, tx); 6504b1251bdSJacob Keller pf = ptp_port_to_pf(ptp_port); 6514b1251bdSJacob Keller 6524b1251bdSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 6534b1251bdSJacob Keller struct skb_shared_hwtstamps shhwtstamps = {}; 6546b5cbc8cSSergey Temerkhanov u8 phy_idx = idx + tx->offset; 6554b1251bdSJacob Keller u64 raw_tstamp, tstamp; 6564b1251bdSJacob Keller struct sk_buff *skb; 6574b1251bdSJacob Keller int err; 6584b1251bdSJacob Keller 6594b1251bdSJacob Keller ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx); 6604b1251bdSJacob Keller 6616b5cbc8cSSergey Temerkhanov err = ice_read_phy_tstamp(&pf->hw, tx->block, phy_idx, 6624b1251bdSJacob Keller &raw_tstamp); 6634b1251bdSJacob Keller if (err) 6644b1251bdSJacob Keller continue; 6654b1251bdSJacob Keller 6664b1251bdSJacob Keller ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx); 6674b1251bdSJacob Keller 6684b1251bdSJacob Keller /* Check if the timestamp is invalid or stale */ 6694b1251bdSJacob Keller if (!(raw_tstamp & ICE_PTP_TS_VALID) || 6704b1251bdSJacob Keller raw_tstamp == tx->tstamps[idx].cached_tstamp) 6714b1251bdSJacob Keller continue; 6724b1251bdSJacob Keller 6734b1251bdSJacob Keller /* The timestamp is valid, so we'll go ahead and clear this 6744b1251bdSJacob Keller * index and then send the timestamp up to the stack. 6754b1251bdSJacob Keller */ 6764b1251bdSJacob Keller spin_lock(&tx->lock); 6774b1251bdSJacob Keller tx->tstamps[idx].cached_tstamp = raw_tstamp; 6784b1251bdSJacob Keller clear_bit(idx, tx->in_use); 6794b1251bdSJacob Keller skb = tx->tstamps[idx].skb; 6804b1251bdSJacob Keller tx->tstamps[idx].skb = NULL; 6814b1251bdSJacob Keller spin_unlock(&tx->lock); 6824b1251bdSJacob Keller 6834b1251bdSJacob Keller /* it's (unlikely but) possible we raced with the cleanup 6844b1251bdSJacob Keller * thread for discarding old timestamp requests. 6854b1251bdSJacob Keller */ 6864b1251bdSJacob Keller if (!skb) 6874b1251bdSJacob Keller continue; 6884b1251bdSJacob Keller 6894b1251bdSJacob Keller /* Extend the timestamp using cached PHC time */ 6904b1251bdSJacob Keller tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp); 6914b1251bdSJacob Keller if (tstamp) { 6924b1251bdSJacob Keller shhwtstamps.hwtstamp = ns_to_ktime(tstamp); 6934b1251bdSJacob Keller ice_trace(tx_tstamp_complete, skb, idx); 6944b1251bdSJacob Keller } 6954b1251bdSJacob Keller 6964b1251bdSJacob Keller skb_tstamp_tx(skb, &shhwtstamps); 6974b1251bdSJacob Keller dev_kfree_skb_any(skb); 6984b1251bdSJacob Keller } 6994b1251bdSJacob Keller 7004b1251bdSJacob Keller /* Check if we still have work to do. If so, re-queue this task to 7014b1251bdSJacob Keller * poll for remaining timestamps. 7024b1251bdSJacob Keller */ 7034b1251bdSJacob Keller spin_lock(&tx->lock); 7044b1251bdSJacob Keller if (!bitmap_empty(tx->in_use, tx->len)) 7051229b339SKarol Kolacinski ts_handled = false; 7064b1251bdSJacob Keller spin_unlock(&tx->lock); 7071229b339SKarol Kolacinski 7081229b339SKarol Kolacinski return ts_handled; 7094b1251bdSJacob Keller } 7104b1251bdSJacob Keller 7114b1251bdSJacob Keller /** 7124b1251bdSJacob Keller * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps 7134b1251bdSJacob Keller * @tx: Tx tracking structure to initialize 7144b1251bdSJacob Keller * 7154b1251bdSJacob Keller * Assumes that the length has already been initialized. Do not call directly, 7166b5cbc8cSSergey Temerkhanov * use the ice_ptp_init_tx_* instead. 7174b1251bdSJacob Keller */ 7184b1251bdSJacob Keller static int 7194b1251bdSJacob Keller ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx) 7204b1251bdSJacob Keller { 7214b1251bdSJacob Keller tx->tstamps = kcalloc(tx->len, sizeof(*tx->tstamps), GFP_KERNEL); 7224b1251bdSJacob Keller if (!tx->tstamps) 7234b1251bdSJacob Keller return -ENOMEM; 7244b1251bdSJacob Keller 7254b1251bdSJacob Keller tx->in_use = bitmap_zalloc(tx->len, GFP_KERNEL); 7264b1251bdSJacob Keller if (!tx->in_use) { 7274b1251bdSJacob Keller kfree(tx->tstamps); 7284b1251bdSJacob Keller tx->tstamps = NULL; 7294b1251bdSJacob Keller return -ENOMEM; 7304b1251bdSJacob Keller } 7314b1251bdSJacob Keller 7324b1251bdSJacob Keller spin_lock_init(&tx->lock); 7334b1251bdSJacob Keller 7344b1251bdSJacob Keller tx->init = 1; 7354b1251bdSJacob Keller 7364b1251bdSJacob Keller return 0; 7374b1251bdSJacob Keller } 7384b1251bdSJacob Keller 7394b1251bdSJacob Keller /** 7404b1251bdSJacob Keller * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker 7414b1251bdSJacob Keller * @pf: Board private structure 7424b1251bdSJacob Keller * @tx: the tracker to flush 7434b1251bdSJacob Keller */ 7444b1251bdSJacob Keller static void 7454b1251bdSJacob Keller ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 7464b1251bdSJacob Keller { 7474b1251bdSJacob Keller u8 idx; 7484b1251bdSJacob Keller 7494b1251bdSJacob Keller for (idx = 0; idx < tx->len; idx++) { 7506b5cbc8cSSergey Temerkhanov u8 phy_idx = idx + tx->offset; 7514b1251bdSJacob Keller 7524b1251bdSJacob Keller spin_lock(&tx->lock); 7534b1251bdSJacob Keller if (tx->tstamps[idx].skb) { 7544b1251bdSJacob Keller dev_kfree_skb_any(tx->tstamps[idx].skb); 7554b1251bdSJacob Keller tx->tstamps[idx].skb = NULL; 7564b1251bdSJacob Keller pf->ptp.tx_hwtstamp_flushed++; 7574b1251bdSJacob Keller } 7584b1251bdSJacob Keller clear_bit(idx, tx->in_use); 7594b1251bdSJacob Keller spin_unlock(&tx->lock); 7604b1251bdSJacob Keller 7614b1251bdSJacob Keller /* Clear any potential residual timestamp in the PHY block */ 7624b1251bdSJacob Keller if (!pf->hw.reset_ongoing) 7636b5cbc8cSSergey Temerkhanov ice_clear_phy_tstamp(&pf->hw, tx->block, phy_idx); 7644b1251bdSJacob Keller } 7654b1251bdSJacob Keller } 7664b1251bdSJacob Keller 7674b1251bdSJacob Keller /** 7684b1251bdSJacob Keller * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker 7694b1251bdSJacob Keller * @pf: Board private structure 7704b1251bdSJacob Keller * @tx: Tx tracking structure to release 7714b1251bdSJacob Keller * 7724b1251bdSJacob Keller * Free memory associated with the Tx timestamp tracker. 7734b1251bdSJacob Keller */ 7744b1251bdSJacob Keller static void 7754b1251bdSJacob Keller ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 7764b1251bdSJacob Keller { 7774b1251bdSJacob Keller tx->init = 0; 7784b1251bdSJacob Keller 7794b1251bdSJacob Keller ice_ptp_flush_tx_tracker(pf, tx); 7804b1251bdSJacob Keller 7814b1251bdSJacob Keller kfree(tx->tstamps); 7824b1251bdSJacob Keller tx->tstamps = NULL; 7834b1251bdSJacob Keller 7844b1251bdSJacob Keller bitmap_free(tx->in_use); 7854b1251bdSJacob Keller tx->in_use = NULL; 7864b1251bdSJacob Keller 7874b1251bdSJacob Keller tx->len = 0; 7884b1251bdSJacob Keller } 7894b1251bdSJacob Keller 7904b1251bdSJacob Keller /** 7914b1251bdSJacob Keller * ice_ptp_init_tx_e822 - Initialize tracking for Tx timestamps 7924b1251bdSJacob Keller * @pf: Board private structure 7934b1251bdSJacob Keller * @tx: the Tx tracking structure to initialize 7944b1251bdSJacob Keller * @port: the port this structure tracks 7954b1251bdSJacob Keller * 7964b1251bdSJacob Keller * Initialize the Tx timestamp tracker for this port. For generic MAC devices, 7974b1251bdSJacob Keller * the timestamp block is shared for all ports in the same quad. To avoid 7984b1251bdSJacob Keller * ports using the same timestamp index, logically break the block of 7994b1251bdSJacob Keller * registers into chunks based on the port number. 8004b1251bdSJacob Keller */ 8014b1251bdSJacob Keller static int 8024b1251bdSJacob Keller ice_ptp_init_tx_e822(struct ice_pf *pf, struct ice_ptp_tx *tx, u8 port) 8034b1251bdSJacob Keller { 8046b5cbc8cSSergey Temerkhanov tx->block = port / ICE_PORTS_PER_QUAD; 8056b5cbc8cSSergey Temerkhanov tx->offset = (port % ICE_PORTS_PER_QUAD) * INDEX_PER_PORT_E822; 8066b5cbc8cSSergey Temerkhanov tx->len = INDEX_PER_PORT_E822; 8074b1251bdSJacob Keller 8084b1251bdSJacob Keller return ice_ptp_alloc_tx_tracker(tx); 8094b1251bdSJacob Keller } 8104b1251bdSJacob Keller 8114b1251bdSJacob Keller /** 8124b1251bdSJacob Keller * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps 8134b1251bdSJacob Keller * @pf: Board private structure 8144b1251bdSJacob Keller * @tx: the Tx tracking structure to initialize 8154b1251bdSJacob Keller * 8164b1251bdSJacob Keller * Initialize the Tx timestamp tracker for this PF. For E810 devices, each 8174b1251bdSJacob Keller * port has its own block of timestamps, independent of the other ports. 8184b1251bdSJacob Keller */ 8194b1251bdSJacob Keller static int 8204b1251bdSJacob Keller ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx) 8214b1251bdSJacob Keller { 8226b5cbc8cSSergey Temerkhanov tx->block = pf->hw.port_info->lport; 8236b5cbc8cSSergey Temerkhanov tx->offset = 0; 8246b5cbc8cSSergey Temerkhanov tx->len = INDEX_PER_PORT_E810; 8254b1251bdSJacob Keller 8264b1251bdSJacob Keller return ice_ptp_alloc_tx_tracker(tx); 8274b1251bdSJacob Keller } 8284b1251bdSJacob Keller 8294b1251bdSJacob Keller /** 8304b1251bdSJacob Keller * ice_ptp_tx_tstamp_cleanup - Cleanup old timestamp requests that got dropped 8314b1251bdSJacob Keller * @pf: pointer to the PF struct 8324b1251bdSJacob Keller * @tx: PTP Tx tracker to clean up 8334b1251bdSJacob Keller * 8344b1251bdSJacob Keller * Loop through the Tx timestamp requests and see if any of them have been 8354b1251bdSJacob Keller * waiting for a long time. Discard any SKBs that have been waiting for more 8364b1251bdSJacob Keller * than 2 seconds. This is long enough to be reasonably sure that the 8374b1251bdSJacob Keller * timestamp will never be captured. This might happen if the packet gets 8384b1251bdSJacob Keller * discarded before it reaches the PHY timestamping block. 8394b1251bdSJacob Keller */ 8404b1251bdSJacob Keller static void ice_ptp_tx_tstamp_cleanup(struct ice_pf *pf, struct ice_ptp_tx *tx) 8414b1251bdSJacob Keller { 8424b1251bdSJacob Keller struct ice_hw *hw = &pf->hw; 8434b1251bdSJacob Keller u8 idx; 8444b1251bdSJacob Keller 8454b1251bdSJacob Keller if (!tx->init) 8464b1251bdSJacob Keller return; 8474b1251bdSJacob Keller 8484b1251bdSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 8494b1251bdSJacob Keller struct sk_buff *skb; 8504b1251bdSJacob Keller u64 raw_tstamp; 8514b1251bdSJacob Keller 8524b1251bdSJacob Keller /* Check if this SKB has been waiting for too long */ 8534b1251bdSJacob Keller if (time_is_after_jiffies(tx->tstamps[idx].start + 2 * HZ)) 8544b1251bdSJacob Keller continue; 8554b1251bdSJacob Keller 8564b1251bdSJacob Keller /* Read tstamp to be able to use this register again */ 8576b5cbc8cSSergey Temerkhanov ice_read_phy_tstamp(hw, tx->block, idx + tx->offset, 8584b1251bdSJacob Keller &raw_tstamp); 8594b1251bdSJacob Keller 8604b1251bdSJacob Keller spin_lock(&tx->lock); 8614b1251bdSJacob Keller skb = tx->tstamps[idx].skb; 8624b1251bdSJacob Keller tx->tstamps[idx].skb = NULL; 8634b1251bdSJacob Keller clear_bit(idx, tx->in_use); 8644b1251bdSJacob Keller spin_unlock(&tx->lock); 8654b1251bdSJacob Keller 8664b1251bdSJacob Keller /* Count the number of Tx timestamps which have timed out */ 8674b1251bdSJacob Keller pf->ptp.tx_hwtstamp_timeouts++; 8684b1251bdSJacob Keller 8694b1251bdSJacob Keller /* Free the SKB after we've cleared the bit */ 8704b1251bdSJacob Keller dev_kfree_skb_any(skb); 8714b1251bdSJacob Keller } 8724b1251bdSJacob Keller } 8734b1251bdSJacob Keller 8744b1251bdSJacob Keller /** 8754b1251bdSJacob Keller * ice_ptp_update_cached_phctime - Update the cached PHC time values 8764b1251bdSJacob Keller * @pf: Board specific private structure 8774b1251bdSJacob Keller * 8784b1251bdSJacob Keller * This function updates the system time values which are cached in the PF 8794b1251bdSJacob Keller * structure and the Rx rings. 8804b1251bdSJacob Keller * 8814b1251bdSJacob Keller * This function must be called periodically to ensure that the cached value 882b1a582e6SJacob Keller * is never more than 2 seconds old. 883b1a582e6SJacob Keller * 884b1a582e6SJacob Keller * Note that the cached copy in the PF PTP structure is always updated, even 885b1a582e6SJacob Keller * if we can't update the copy in the Rx rings. 8864b1251bdSJacob Keller * 8874b1251bdSJacob Keller * Return: 8884b1251bdSJacob Keller * * 0 - OK, successfully updated 8894b1251bdSJacob Keller * * -EAGAIN - PF was busy, need to reschedule the update 8904b1251bdSJacob Keller */ 8914b1251bdSJacob Keller static int ice_ptp_update_cached_phctime(struct ice_pf *pf) 8924b1251bdSJacob Keller { 8934b1251bdSJacob Keller struct device *dev = ice_pf_to_dev(pf); 8944b1251bdSJacob Keller unsigned long update_before; 8954b1251bdSJacob Keller u64 systime; 8964b1251bdSJacob Keller int i; 8974b1251bdSJacob Keller 8984b1251bdSJacob Keller update_before = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000); 8994b1251bdSJacob Keller if (pf->ptp.cached_phc_time && 9004b1251bdSJacob Keller time_is_before_jiffies(update_before)) { 9014b1251bdSJacob Keller unsigned long time_taken = jiffies - pf->ptp.cached_phc_jiffies; 9024b1251bdSJacob Keller 9034b1251bdSJacob Keller dev_warn(dev, "%u msecs passed between update to cached PHC time\n", 9044b1251bdSJacob Keller jiffies_to_msecs(time_taken)); 9054b1251bdSJacob Keller pf->ptp.late_cached_phc_updates++; 9064b1251bdSJacob Keller } 9074b1251bdSJacob Keller 9084b1251bdSJacob Keller /* Read the current PHC time */ 9094b1251bdSJacob Keller systime = ice_ptp_read_src_clk_reg(pf, NULL); 9104b1251bdSJacob Keller 9114b1251bdSJacob Keller /* Update the cached PHC time stored in the PF structure */ 9124b1251bdSJacob Keller WRITE_ONCE(pf->ptp.cached_phc_time, systime); 9134b1251bdSJacob Keller WRITE_ONCE(pf->ptp.cached_phc_jiffies, jiffies); 9144b1251bdSJacob Keller 915b1a582e6SJacob Keller if (test_and_set_bit(ICE_CFG_BUSY, pf->state)) 916b1a582e6SJacob Keller return -EAGAIN; 917b1a582e6SJacob Keller 9184b1251bdSJacob Keller ice_for_each_vsi(pf, i) { 9194b1251bdSJacob Keller struct ice_vsi *vsi = pf->vsi[i]; 9204b1251bdSJacob Keller int j; 9214b1251bdSJacob Keller 9224b1251bdSJacob Keller if (!vsi) 9234b1251bdSJacob Keller continue; 9244b1251bdSJacob Keller 9254b1251bdSJacob Keller if (vsi->type != ICE_VSI_PF) 9264b1251bdSJacob Keller continue; 9274b1251bdSJacob Keller 9284b1251bdSJacob Keller ice_for_each_rxq(vsi, j) { 9294b1251bdSJacob Keller if (!vsi->rx_rings[j]) 9304b1251bdSJacob Keller continue; 9314b1251bdSJacob Keller WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime); 9324b1251bdSJacob Keller } 9334b1251bdSJacob Keller } 9344b1251bdSJacob Keller clear_bit(ICE_CFG_BUSY, pf->state); 9354b1251bdSJacob Keller 9364b1251bdSJacob Keller return 0; 9374b1251bdSJacob Keller } 9384b1251bdSJacob Keller 9394b1251bdSJacob Keller /** 940b1a582e6SJacob Keller * ice_ptp_reset_cached_phctime - Reset cached PHC time after an update 941b1a582e6SJacob Keller * @pf: Board specific private structure 942b1a582e6SJacob Keller * 943b1a582e6SJacob Keller * This function must be called when the cached PHC time is no longer valid, 944b1a582e6SJacob Keller * such as after a time adjustment. It discards any outstanding Tx timestamps, 945b1a582e6SJacob Keller * and updates the cached PHC time for both the PF and Rx rings. If updating 946b1a582e6SJacob Keller * the PHC time cannot be done immediately, a warning message is logged and 947b1a582e6SJacob Keller * the work item is scheduled. 948b1a582e6SJacob Keller * 949b1a582e6SJacob Keller * These steps are required in order to ensure that we do not accidentally 950b1a582e6SJacob Keller * report a timestamp extended by the wrong PHC cached copy. Note that we 951b1a582e6SJacob Keller * do not directly update the cached timestamp here because it is possible 952b1a582e6SJacob Keller * this might produce an error when ICE_CFG_BUSY is set. If this occurred, we 953b1a582e6SJacob Keller * would have to try again. During that time window, timestamps might be 954b1a582e6SJacob Keller * requested and returned with an invalid extension. Thus, on failure to 955b1a582e6SJacob Keller * immediately update the cached PHC time we would need to zero the value 956b1a582e6SJacob Keller * anyways. For this reason, we just zero the value immediately and queue the 957b1a582e6SJacob Keller * update work item. 958b1a582e6SJacob Keller */ 959b1a582e6SJacob Keller static void ice_ptp_reset_cached_phctime(struct ice_pf *pf) 960b1a582e6SJacob Keller { 961b1a582e6SJacob Keller struct device *dev = ice_pf_to_dev(pf); 962b1a582e6SJacob Keller int err; 963b1a582e6SJacob Keller 964b1a582e6SJacob Keller /* Update the cached PHC time immediately if possible, otherwise 965b1a582e6SJacob Keller * schedule the work item to execute soon. 966b1a582e6SJacob Keller */ 967b1a582e6SJacob Keller err = ice_ptp_update_cached_phctime(pf); 968b1a582e6SJacob Keller if (err) { 969b1a582e6SJacob Keller /* If another thread is updating the Rx rings, we won't 970b1a582e6SJacob Keller * properly reset them here. This could lead to reporting of 971b1a582e6SJacob Keller * invalid timestamps, but there isn't much we can do. 972b1a582e6SJacob Keller */ 973b1a582e6SJacob Keller dev_warn(dev, "%s: ICE_CFG_BUSY, unable to immediately update cached PHC time\n", 974b1a582e6SJacob Keller __func__); 975b1a582e6SJacob Keller 976b1a582e6SJacob Keller /* Queue the work item to update the Rx rings when possible */ 977b1a582e6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work, 978b1a582e6SJacob Keller msecs_to_jiffies(10)); 979b1a582e6SJacob Keller } 980b1a582e6SJacob Keller 981b1a582e6SJacob Keller /* Flush any outstanding Tx timestamps */ 982b1a582e6SJacob Keller ice_ptp_flush_tx_tracker(pf, &pf->ptp.port.tx); 983b1a582e6SJacob Keller } 984b1a582e6SJacob Keller 985b1a582e6SJacob Keller /** 98606c16d89SJacob Keller * ice_ptp_read_time - Read the time from the device 98706c16d89SJacob Keller * @pf: Board private structure 98806c16d89SJacob Keller * @ts: timespec structure to hold the current time value 98906c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 99006c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 99106c16d89SJacob Keller * 99206c16d89SJacob Keller * This function reads the source clock registers and stores them in a timespec. 99306c16d89SJacob Keller * However, since the registers are 64 bits of nanoseconds, we must convert the 99406c16d89SJacob Keller * result to a timespec before we can return. 99506c16d89SJacob Keller */ 99606c16d89SJacob Keller static void 99706c16d89SJacob Keller ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts, 99806c16d89SJacob Keller struct ptp_system_timestamp *sts) 99906c16d89SJacob Keller { 100006c16d89SJacob Keller u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts); 100106c16d89SJacob Keller 100206c16d89SJacob Keller *ts = ns_to_timespec64(time_ns); 100306c16d89SJacob Keller } 100406c16d89SJacob Keller 100506c16d89SJacob Keller /** 100606c16d89SJacob Keller * ice_ptp_write_init - Set PHC time to provided value 100706c16d89SJacob Keller * @pf: Board private structure 100806c16d89SJacob Keller * @ts: timespec structure that holds the new time value 100906c16d89SJacob Keller * 101006c16d89SJacob Keller * Set the PHC time to the specified time provided in the timespec. 101106c16d89SJacob Keller */ 101206c16d89SJacob Keller static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts) 101306c16d89SJacob Keller { 101406c16d89SJacob Keller u64 ns = timespec64_to_ns(ts); 101506c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 101606c16d89SJacob Keller 101706c16d89SJacob Keller return ice_ptp_init_time(hw, ns); 101806c16d89SJacob Keller } 101906c16d89SJacob Keller 102006c16d89SJacob Keller /** 102106c16d89SJacob Keller * ice_ptp_write_adj - Adjust PHC clock time atomically 102206c16d89SJacob Keller * @pf: Board private structure 102306c16d89SJacob Keller * @adj: Adjustment in nanoseconds 102406c16d89SJacob Keller * 102506c16d89SJacob Keller * Perform an atomic adjustment of the PHC time by the specified number of 102606c16d89SJacob Keller * nanoseconds. 102706c16d89SJacob Keller */ 102806c16d89SJacob Keller static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj) 102906c16d89SJacob Keller { 103006c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 103106c16d89SJacob Keller 103206c16d89SJacob Keller return ice_ptp_adj_clock(hw, adj); 103306c16d89SJacob Keller } 103406c16d89SJacob Keller 103506c16d89SJacob Keller /** 103678267d0cSJacob Keller * ice_base_incval - Get base timer increment value 103778267d0cSJacob Keller * @pf: Board private structure 103878267d0cSJacob Keller * 103978267d0cSJacob Keller * Look up the base timer increment value for this device. The base increment 104078267d0cSJacob Keller * value is used to define the nominal clock tick rate. This increment value 104178267d0cSJacob Keller * is programmed during device initialization. It is also used as the basis 104278267d0cSJacob Keller * for calculating adjustments using scaled_ppm. 104378267d0cSJacob Keller */ 104478267d0cSJacob Keller static u64 ice_base_incval(struct ice_pf *pf) 104578267d0cSJacob Keller { 10463a749623SJacob Keller struct ice_hw *hw = &pf->hw; 10473a749623SJacob Keller u64 incval; 10483a749623SJacob Keller 10493a749623SJacob Keller if (ice_is_e810(hw)) 10503a749623SJacob Keller incval = ICE_PTP_NOMINAL_INCVAL_E810; 10513a749623SJacob Keller else if (ice_e822_time_ref(hw) < NUM_ICE_TIME_REF_FREQ) 10523a749623SJacob Keller incval = ice_e822_nominal_incval(ice_e822_time_ref(hw)); 10533a749623SJacob Keller else 10543a749623SJacob Keller incval = UNKNOWN_INCVAL_E822; 10553a749623SJacob Keller 10563a749623SJacob Keller dev_dbg(ice_pf_to_dev(pf), "PTP: using base increment value of 0x%016llx\n", 10573a749623SJacob Keller incval); 10583a749623SJacob Keller 10593a749623SJacob Keller return incval; 10603a749623SJacob Keller } 10613a749623SJacob Keller 10623a749623SJacob Keller /** 1063a69f1cb6SJacob Keller * ice_ptp_check_tx_fifo - Check whether Tx FIFO is in an OK state 1064a69f1cb6SJacob Keller * @port: PTP port for which Tx FIFO is checked 1065a69f1cb6SJacob Keller */ 1066a69f1cb6SJacob Keller static int ice_ptp_check_tx_fifo(struct ice_ptp_port *port) 1067a69f1cb6SJacob Keller { 1068a69f1cb6SJacob Keller int quad = port->port_num / ICE_PORTS_PER_QUAD; 1069a69f1cb6SJacob Keller int offs = port->port_num % ICE_PORTS_PER_QUAD; 1070a69f1cb6SJacob Keller struct ice_pf *pf; 1071a69f1cb6SJacob Keller struct ice_hw *hw; 1072a69f1cb6SJacob Keller u32 val, phy_sts; 1073a69f1cb6SJacob Keller int err; 1074a69f1cb6SJacob Keller 1075a69f1cb6SJacob Keller pf = ptp_port_to_pf(port); 1076a69f1cb6SJacob Keller hw = &pf->hw; 1077a69f1cb6SJacob Keller 1078a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == FIFO_OK) 1079a69f1cb6SJacob Keller return 0; 1080a69f1cb6SJacob Keller 1081a69f1cb6SJacob Keller /* need to read FIFO state */ 1082a69f1cb6SJacob Keller if (offs == 0 || offs == 1) 1083a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO01_STATUS, 1084a69f1cb6SJacob Keller &val); 1085a69f1cb6SJacob Keller else 1086a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO23_STATUS, 1087a69f1cb6SJacob Keller &val); 1088a69f1cb6SJacob Keller 1089a69f1cb6SJacob Keller if (err) { 1090a69f1cb6SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to check port %d Tx FIFO, err %d\n", 1091a69f1cb6SJacob Keller port->port_num, err); 1092a69f1cb6SJacob Keller return err; 1093a69f1cb6SJacob Keller } 1094a69f1cb6SJacob Keller 1095a69f1cb6SJacob Keller if (offs & 0x1) 1096a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO13_M) >> Q_REG_FIFO13_S; 1097a69f1cb6SJacob Keller else 1098a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO02_M) >> Q_REG_FIFO02_S; 1099a69f1cb6SJacob Keller 1100a69f1cb6SJacob Keller if (phy_sts & FIFO_EMPTY) { 1101a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK; 1102a69f1cb6SJacob Keller return 0; 1103a69f1cb6SJacob Keller } 1104a69f1cb6SJacob Keller 1105a69f1cb6SJacob Keller port->tx_fifo_busy_cnt++; 1106a69f1cb6SJacob Keller 1107a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf), "Try %d, port %d FIFO not empty\n", 1108a69f1cb6SJacob Keller port->tx_fifo_busy_cnt, port->port_num); 1109a69f1cb6SJacob Keller 1110a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == ICE_PTP_FIFO_NUM_CHECKS) { 1111a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf), 1112a69f1cb6SJacob Keller "Port %d Tx FIFO still not empty; resetting quad %d\n", 1113a69f1cb6SJacob Keller port->port_num, quad); 1114407b66c0SKarol Kolacinski ice_ptp_reset_ts_memory_quad_e822(hw, quad); 1115a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK; 1116a69f1cb6SJacob Keller return 0; 1117a69f1cb6SJacob Keller } 1118a69f1cb6SJacob Keller 1119a69f1cb6SJacob Keller return -EAGAIN; 1120a69f1cb6SJacob Keller } 1121a69f1cb6SJacob Keller 1122a69f1cb6SJacob Keller /** 1123a69f1cb6SJacob Keller * ice_ptp_check_tx_offset_valid - Check if the Tx PHY offset is valid 1124a69f1cb6SJacob Keller * @port: the PTP port to check 1125a69f1cb6SJacob Keller * 1126a69f1cb6SJacob Keller * Checks whether the Tx offset for the PHY associated with this port is 1127a69f1cb6SJacob Keller * valid. Returns 0 if the offset is valid, and a non-zero error code if it is 1128a69f1cb6SJacob Keller * not. 1129a69f1cb6SJacob Keller */ 1130a69f1cb6SJacob Keller static int ice_ptp_check_tx_offset_valid(struct ice_ptp_port *port) 1131a69f1cb6SJacob Keller { 1132a69f1cb6SJacob Keller struct ice_pf *pf = ptp_port_to_pf(port); 1133a69f1cb6SJacob Keller struct device *dev = ice_pf_to_dev(pf); 1134a69f1cb6SJacob Keller struct ice_hw *hw = &pf->hw; 1135a69f1cb6SJacob Keller u32 val; 1136a69f1cb6SJacob Keller int err; 1137a69f1cb6SJacob Keller 1138a69f1cb6SJacob Keller err = ice_ptp_check_tx_fifo(port); 1139a69f1cb6SJacob Keller if (err) 1140a69f1cb6SJacob Keller return err; 1141a69f1cb6SJacob Keller 1142a69f1cb6SJacob Keller err = ice_read_phy_reg_e822(hw, port->port_num, P_REG_TX_OV_STATUS, 1143a69f1cb6SJacob Keller &val); 1144a69f1cb6SJacob Keller if (err) { 1145a69f1cb6SJacob Keller dev_err(dev, "Failed to read TX_OV_STATUS for port %d, err %d\n", 1146a69f1cb6SJacob Keller port->port_num, err); 1147a69f1cb6SJacob Keller return -EAGAIN; 1148a69f1cb6SJacob Keller } 1149a69f1cb6SJacob Keller 1150a69f1cb6SJacob Keller if (!(val & P_REG_TX_OV_STATUS_OV_M)) 1151a69f1cb6SJacob Keller return -EAGAIN; 1152a69f1cb6SJacob Keller 1153a69f1cb6SJacob Keller return 0; 1154a69f1cb6SJacob Keller } 1155a69f1cb6SJacob Keller 1156a69f1cb6SJacob Keller /** 1157a69f1cb6SJacob Keller * ice_ptp_check_rx_offset_valid - Check if the Rx PHY offset is valid 1158a69f1cb6SJacob Keller * @port: the PTP port to check 1159a69f1cb6SJacob Keller * 1160a69f1cb6SJacob Keller * Checks whether the Rx offset for the PHY associated with this port is 1161a69f1cb6SJacob Keller * valid. Returns 0 if the offset is valid, and a non-zero error code if it is 1162a69f1cb6SJacob Keller * not. 1163a69f1cb6SJacob Keller */ 1164a69f1cb6SJacob Keller static int ice_ptp_check_rx_offset_valid(struct ice_ptp_port *port) 1165a69f1cb6SJacob Keller { 1166a69f1cb6SJacob Keller struct ice_pf *pf = ptp_port_to_pf(port); 1167a69f1cb6SJacob Keller struct device *dev = ice_pf_to_dev(pf); 1168a69f1cb6SJacob Keller struct ice_hw *hw = &pf->hw; 1169a69f1cb6SJacob Keller int err; 1170a69f1cb6SJacob Keller u32 val; 1171a69f1cb6SJacob Keller 1172a69f1cb6SJacob Keller err = ice_read_phy_reg_e822(hw, port->port_num, P_REG_RX_OV_STATUS, 1173a69f1cb6SJacob Keller &val); 1174a69f1cb6SJacob Keller if (err) { 1175a69f1cb6SJacob Keller dev_err(dev, "Failed to read RX_OV_STATUS for port %d, err %d\n", 1176a69f1cb6SJacob Keller port->port_num, err); 1177a69f1cb6SJacob Keller return err; 1178a69f1cb6SJacob Keller } 1179a69f1cb6SJacob Keller 1180a69f1cb6SJacob Keller if (!(val & P_REG_RX_OV_STATUS_OV_M)) 1181a69f1cb6SJacob Keller return -EAGAIN; 1182a69f1cb6SJacob Keller 1183a69f1cb6SJacob Keller return 0; 1184a69f1cb6SJacob Keller } 1185a69f1cb6SJacob Keller 1186a69f1cb6SJacob Keller /** 1187a69f1cb6SJacob Keller * ice_ptp_check_offset_valid - Check port offset valid bit 1188a69f1cb6SJacob Keller * @port: Port for which offset valid bit is checked 1189a69f1cb6SJacob Keller * 1190a69f1cb6SJacob Keller * Returns 0 if both Tx and Rx offset are valid, and -EAGAIN if one of the 1191a69f1cb6SJacob Keller * offset is not ready. 1192a69f1cb6SJacob Keller */ 1193a69f1cb6SJacob Keller static int ice_ptp_check_offset_valid(struct ice_ptp_port *port) 1194a69f1cb6SJacob Keller { 1195a69f1cb6SJacob Keller int tx_err, rx_err; 1196a69f1cb6SJacob Keller 1197a69f1cb6SJacob Keller /* always check both Tx and Rx offset validity */ 1198a69f1cb6SJacob Keller tx_err = ice_ptp_check_tx_offset_valid(port); 1199a69f1cb6SJacob Keller rx_err = ice_ptp_check_rx_offset_valid(port); 1200a69f1cb6SJacob Keller 1201a69f1cb6SJacob Keller if (tx_err || rx_err) 1202a69f1cb6SJacob Keller return -EAGAIN; 1203a69f1cb6SJacob Keller 1204a69f1cb6SJacob Keller return 0; 1205a69f1cb6SJacob Keller } 1206a69f1cb6SJacob Keller 1207a69f1cb6SJacob Keller /** 1208a69f1cb6SJacob Keller * ice_ptp_wait_for_offset_valid - Check for valid Tx and Rx offsets 1209a69f1cb6SJacob Keller * @work: Pointer to the kthread_work structure for this task 1210a69f1cb6SJacob Keller * 1211a69f1cb6SJacob Keller * Check whether both the Tx and Rx offsets are valid for enabling the vernier 1212a69f1cb6SJacob Keller * calibration. 1213a69f1cb6SJacob Keller * 1214a69f1cb6SJacob Keller * Once we have valid offsets from hardware, update the total Tx and Rx 1215a69f1cb6SJacob Keller * offsets, and exit bypass mode. This enables more precise timestamps using 1216a69f1cb6SJacob Keller * the extra data measured during the vernier calibration process. 1217a69f1cb6SJacob Keller */ 1218a69f1cb6SJacob Keller static void ice_ptp_wait_for_offset_valid(struct kthread_work *work) 1219a69f1cb6SJacob Keller { 1220a69f1cb6SJacob Keller struct ice_ptp_port *port; 1221a69f1cb6SJacob Keller int err; 1222a69f1cb6SJacob Keller struct device *dev; 1223a69f1cb6SJacob Keller struct ice_pf *pf; 1224a69f1cb6SJacob Keller struct ice_hw *hw; 1225a69f1cb6SJacob Keller 1226a69f1cb6SJacob Keller port = container_of(work, struct ice_ptp_port, ov_work.work); 1227a69f1cb6SJacob Keller pf = ptp_port_to_pf(port); 1228a69f1cb6SJacob Keller hw = &pf->hw; 1229a69f1cb6SJacob Keller dev = ice_pf_to_dev(pf); 1230a69f1cb6SJacob Keller 12310b57e0d4SMichal Michalik if (ice_is_reset_in_progress(pf->state)) 12320b57e0d4SMichal Michalik return; 12330b57e0d4SMichal Michalik 1234a69f1cb6SJacob Keller if (ice_ptp_check_offset_valid(port)) { 1235a69f1cb6SJacob Keller /* Offsets not ready yet, try again later */ 1236a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, 1237a69f1cb6SJacob Keller &port->ov_work, 1238a69f1cb6SJacob Keller msecs_to_jiffies(100)); 1239a69f1cb6SJacob Keller return; 1240a69f1cb6SJacob Keller } 1241a69f1cb6SJacob Keller 12420357d5caSMilena Olech /* Offsets are valid, so Vernier mode calculations are started */ 12430357d5caSMilena Olech err = ice_phy_calc_vernier_e822(hw, port->port_num); 1244a69f1cb6SJacob Keller if (err) { 12450357d5caSMilena Olech dev_warn(dev, "Failed to prepare Vernier mode for PHY port %u, err %d\n", 1246a69f1cb6SJacob Keller port->port_num, err); 1247a69f1cb6SJacob Keller return; 1248a69f1cb6SJacob Keller } 1249a69f1cb6SJacob Keller } 1250a69f1cb6SJacob Keller 1251a69f1cb6SJacob Keller /** 12523a749623SJacob Keller * ice_ptp_port_phy_stop - Stop timestamping for a PHY port 12533a749623SJacob Keller * @ptp_port: PTP port to stop 12543a749623SJacob Keller */ 12553a749623SJacob Keller static int 12563a749623SJacob Keller ice_ptp_port_phy_stop(struct ice_ptp_port *ptp_port) 12573a749623SJacob Keller { 12583a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port); 12593a749623SJacob Keller u8 port = ptp_port->port_num; 12603a749623SJacob Keller struct ice_hw *hw = &pf->hw; 12613a749623SJacob Keller int err; 12623a749623SJacob Keller 12633a749623SJacob Keller if (ice_is_e810(hw)) 12643a749623SJacob Keller return 0; 12653a749623SJacob Keller 12663a749623SJacob Keller mutex_lock(&ptp_port->ps_lock); 12673a749623SJacob Keller 1268a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work); 1269a69f1cb6SJacob Keller 12703a749623SJacob Keller err = ice_stop_phy_timer_e822(hw, port, true); 12713a749623SJacob Keller if (err) 12723a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d down, err %d\n", 12733a749623SJacob Keller port, err); 12743a749623SJacob Keller 12753a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock); 12763a749623SJacob Keller 12773a749623SJacob Keller return err; 12783a749623SJacob Keller } 12793a749623SJacob Keller 12803a749623SJacob Keller /** 12813a749623SJacob Keller * ice_ptp_port_phy_restart - (Re)start and calibrate PHY timestamping 12823a749623SJacob Keller * @ptp_port: PTP port for which the PHY start is set 12833a749623SJacob Keller * 12843a749623SJacob Keller * Start the PHY timestamping block, and initiate Vernier timestamping 12853a749623SJacob Keller * calibration. If timestamping cannot be calibrated (such as if link is down) 12863a749623SJacob Keller * then disable the timestamping block instead. 12873a749623SJacob Keller */ 12883a749623SJacob Keller static int 12893a749623SJacob Keller ice_ptp_port_phy_restart(struct ice_ptp_port *ptp_port) 12903a749623SJacob Keller { 12913a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port); 12923a749623SJacob Keller u8 port = ptp_port->port_num; 12933a749623SJacob Keller struct ice_hw *hw = &pf->hw; 12943a749623SJacob Keller int err; 12953a749623SJacob Keller 12963a749623SJacob Keller if (ice_is_e810(hw)) 12973a749623SJacob Keller return 0; 12983a749623SJacob Keller 12993a749623SJacob Keller if (!ptp_port->link_up) 13003a749623SJacob Keller return ice_ptp_port_phy_stop(ptp_port); 13013a749623SJacob Keller 13023a749623SJacob Keller mutex_lock(&ptp_port->ps_lock); 13033a749623SJacob Keller 1304a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work); 1305a69f1cb6SJacob Keller 13063a749623SJacob Keller /* temporarily disable Tx timestamps while calibrating PHY offset */ 13073a749623SJacob Keller ptp_port->tx.calibrating = true; 1308a69f1cb6SJacob Keller ptp_port->tx_fifo_busy_cnt = 0; 13093a749623SJacob Keller 13100357d5caSMilena Olech /* Start the PHY timer in Vernier mode */ 13110357d5caSMilena Olech err = ice_start_phy_timer_e822(hw, port); 13123a749623SJacob Keller if (err) 13133a749623SJacob Keller goto out_unlock; 13143a749623SJacob Keller 13153a749623SJacob Keller /* Enable Tx timestamps right away */ 13163a749623SJacob Keller ptp_port->tx.calibrating = false; 13173a749623SJacob Keller 1318a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &ptp_port->ov_work, 0); 1319a69f1cb6SJacob Keller 13203a749623SJacob Keller out_unlock: 13213a749623SJacob Keller if (err) 13223a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d up, err %d\n", 13233a749623SJacob Keller port, err); 13243a749623SJacob Keller 13253a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock); 13263a749623SJacob Keller 13273a749623SJacob Keller return err; 13283a749623SJacob Keller } 13293a749623SJacob Keller 13303a749623SJacob Keller /** 1331*6b1ff5d3SJacob Keller * ice_ptp_link_change - Reconfigure PTP after link status change 13323a749623SJacob Keller * @pf: Board private structure 13333a749623SJacob Keller * @port: Port for which the PHY start is set 13343a749623SJacob Keller * @linkup: Link is up or down 13353a749623SJacob Keller */ 1336*6b1ff5d3SJacob Keller void ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup) 13373a749623SJacob Keller { 13383a749623SJacob Keller struct ice_ptp_port *ptp_port; 13393a749623SJacob Keller 1340*6b1ff5d3SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 1341*6b1ff5d3SJacob Keller return; 13423a749623SJacob Keller 1343*6b1ff5d3SJacob Keller if (WARN_ON_ONCE(port >= ICE_NUM_EXTERNAL_PORTS)) 1344*6b1ff5d3SJacob Keller return; 13453a749623SJacob Keller 13463a749623SJacob Keller ptp_port = &pf->ptp.port; 1347*6b1ff5d3SJacob Keller if (WARN_ON_ONCE(ptp_port->port_num != port)) 1348*6b1ff5d3SJacob Keller return; 13493a749623SJacob Keller 135011722c39SJacob Keller /* Update cached link status for this port immediately */ 13513a749623SJacob Keller ptp_port->link_up = linkup; 13523a749623SJacob Keller 1353*6b1ff5d3SJacob Keller /* E810 devices do not need to reconfigure the PHY */ 1354*6b1ff5d3SJacob Keller if (ice_is_e810(&pf->hw)) 1355*6b1ff5d3SJacob Keller return; 13563a749623SJacob Keller 1357*6b1ff5d3SJacob Keller ice_ptp_port_phy_restart(ptp_port); 13583a749623SJacob Keller } 13593a749623SJacob Keller 13603a749623SJacob Keller /** 13613a749623SJacob Keller * ice_ptp_tx_ena_intr - Enable or disable the Tx timestamp interrupt 13623a749623SJacob Keller * @pf: PF private structure 13633a749623SJacob Keller * @ena: bool value to enable or disable interrupt 13643a749623SJacob Keller * @threshold: Minimum number of packets at which intr is triggered 13653a749623SJacob Keller * 13663a749623SJacob Keller * Utility function to enable or disable Tx timestamp interrupt and threshold 13673a749623SJacob Keller */ 13683a749623SJacob Keller static int ice_ptp_tx_ena_intr(struct ice_pf *pf, bool ena, u32 threshold) 13693a749623SJacob Keller { 13703a749623SJacob Keller struct ice_hw *hw = &pf->hw; 13713a749623SJacob Keller int err = 0; 13723a749623SJacob Keller int quad; 13733a749623SJacob Keller u32 val; 13743a749623SJacob Keller 1375407b66c0SKarol Kolacinski ice_ptp_reset_ts_memory(hw); 13763a749623SJacob Keller 13773a749623SJacob Keller for (quad = 0; quad < ICE_MAX_QUAD; quad++) { 13783a749623SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, 13793a749623SJacob Keller &val); 13803a749623SJacob Keller if (err) 13813a749623SJacob Keller break; 13823a749623SJacob Keller 13833a749623SJacob Keller if (ena) { 13843a749623SJacob Keller val |= Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M; 13853a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_THR_M; 13863a749623SJacob Keller val |= ((threshold << Q_REG_TX_MEM_GBL_CFG_INTR_THR_S) & 13873a749623SJacob Keller Q_REG_TX_MEM_GBL_CFG_INTR_THR_M); 13883a749623SJacob Keller } else { 13893a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M; 13903a749623SJacob Keller } 13913a749623SJacob Keller 13923a749623SJacob Keller err = ice_write_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, 13933a749623SJacob Keller val); 13943a749623SJacob Keller if (err) 13953a749623SJacob Keller break; 13963a749623SJacob Keller } 13973a749623SJacob Keller 13983a749623SJacob Keller if (err) 13993a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed in intr ena, err %d\n", 14003a749623SJacob Keller err); 14013a749623SJacob Keller return err; 14023a749623SJacob Keller } 14033a749623SJacob Keller 14043a749623SJacob Keller /** 14053a749623SJacob Keller * ice_ptp_reset_phy_timestamping - Reset PHY timestamping block 14063a749623SJacob Keller * @pf: Board private structure 14073a749623SJacob Keller */ 14083a749623SJacob Keller static void ice_ptp_reset_phy_timestamping(struct ice_pf *pf) 14093a749623SJacob Keller { 14103a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port); 141178267d0cSJacob Keller } 141278267d0cSJacob Keller 141378267d0cSJacob Keller /** 141406c16d89SJacob Keller * ice_ptp_adjfine - Adjust clock increment rate 141506c16d89SJacob Keller * @info: the driver's PTP info structure 141606c16d89SJacob Keller * @scaled_ppm: Parts per million with 16-bit fractional field 141706c16d89SJacob Keller * 141806c16d89SJacob Keller * Adjust the frequency of the clock by the indicated scaled ppm from the 141906c16d89SJacob Keller * base frequency. 142006c16d89SJacob Keller */ 142106c16d89SJacob Keller static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm) 142206c16d89SJacob Keller { 142306c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 142406c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 14251060707eSJacob Keller u64 incval; 142606c16d89SJacob Keller int err; 142706c16d89SJacob Keller 14281060707eSJacob Keller incval = adjust_by_scaled_ppm(ice_base_incval(pf), scaled_ppm); 142906c16d89SJacob Keller err = ice_ptp_write_incval_locked(hw, incval); 143006c16d89SJacob Keller if (err) { 143106c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n", 143206c16d89SJacob Keller err); 143306c16d89SJacob Keller return -EIO; 143406c16d89SJacob Keller } 143506c16d89SJacob Keller 143606c16d89SJacob Keller return 0; 143706c16d89SJacob Keller } 143806c16d89SJacob Keller 143906c16d89SJacob Keller /** 1440172db5f9SMaciej Machnikowski * ice_ptp_extts_work - Workqueue task function 1441172db5f9SMaciej Machnikowski * @work: external timestamp work structure 1442172db5f9SMaciej Machnikowski * 1443172db5f9SMaciej Machnikowski * Service for PTP external clock event 1444172db5f9SMaciej Machnikowski */ 1445172db5f9SMaciej Machnikowski static void ice_ptp_extts_work(struct kthread_work *work) 1446172db5f9SMaciej Machnikowski { 1447172db5f9SMaciej Machnikowski struct ice_ptp *ptp = container_of(work, struct ice_ptp, extts_work); 1448172db5f9SMaciej Machnikowski struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 1449172db5f9SMaciej Machnikowski struct ptp_clock_event event; 1450172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1451172db5f9SMaciej Machnikowski u8 chan, tmr_idx; 1452172db5f9SMaciej Machnikowski u32 hi, lo; 1453172db5f9SMaciej Machnikowski 1454172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1455172db5f9SMaciej Machnikowski /* Event time is captured by one of the two matched registers 1456172db5f9SMaciej Machnikowski * GLTSYN_EVNT_L: 32 LSB of sampled time event 1457172db5f9SMaciej Machnikowski * GLTSYN_EVNT_H: 32 MSB of sampled time event 1458172db5f9SMaciej Machnikowski * Event is defined in GLTSYN_EVNT_0 register 1459172db5f9SMaciej Machnikowski */ 1460172db5f9SMaciej Machnikowski for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) { 1461172db5f9SMaciej Machnikowski /* Check if channel is enabled */ 1462172db5f9SMaciej Machnikowski if (pf->ptp.ext_ts_irq & (1 << chan)) { 1463172db5f9SMaciej Machnikowski lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx)); 1464172db5f9SMaciej Machnikowski hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx)); 1465172db5f9SMaciej Machnikowski event.timestamp = (((u64)hi) << 32) | lo; 1466172db5f9SMaciej Machnikowski event.type = PTP_CLOCK_EXTTS; 1467172db5f9SMaciej Machnikowski event.index = chan; 1468172db5f9SMaciej Machnikowski 1469172db5f9SMaciej Machnikowski /* Fire event */ 1470172db5f9SMaciej Machnikowski ptp_clock_event(pf->ptp.clock, &event); 1471172db5f9SMaciej Machnikowski pf->ptp.ext_ts_irq &= ~(1 << chan); 1472172db5f9SMaciej Machnikowski } 1473172db5f9SMaciej Machnikowski } 1474172db5f9SMaciej Machnikowski } 1475172db5f9SMaciej Machnikowski 1476172db5f9SMaciej Machnikowski /** 1477172db5f9SMaciej Machnikowski * ice_ptp_cfg_extts - Configure EXTTS pin and channel 1478172db5f9SMaciej Machnikowski * @pf: Board private structure 1479172db5f9SMaciej Machnikowski * @ena: true to enable; false to disable 1480172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 1481172db5f9SMaciej Machnikowski * @gpio_pin: GPIO pin 1482172db5f9SMaciej Machnikowski * @extts_flags: request flags from the ptp_extts_request.flags 1483172db5f9SMaciej Machnikowski */ 1484172db5f9SMaciej Machnikowski static int 1485172db5f9SMaciej Machnikowski ice_ptp_cfg_extts(struct ice_pf *pf, bool ena, unsigned int chan, u32 gpio_pin, 1486172db5f9SMaciej Machnikowski unsigned int extts_flags) 1487172db5f9SMaciej Machnikowski { 1488172db5f9SMaciej Machnikowski u32 func, aux_reg, gpio_reg, irq_reg; 1489172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1490172db5f9SMaciej Machnikowski u8 tmr_idx; 1491172db5f9SMaciej Machnikowski 1492172db5f9SMaciej Machnikowski if (chan > (unsigned int)pf->ptp.info.n_ext_ts) 1493172db5f9SMaciej Machnikowski return -EINVAL; 1494172db5f9SMaciej Machnikowski 1495172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1496172db5f9SMaciej Machnikowski 1497172db5f9SMaciej Machnikowski irq_reg = rd32(hw, PFINT_OICR_ENA); 1498172db5f9SMaciej Machnikowski 1499172db5f9SMaciej Machnikowski if (ena) { 1500172db5f9SMaciej Machnikowski /* Enable the interrupt */ 1501172db5f9SMaciej Machnikowski irq_reg |= PFINT_OICR_TSYN_EVNT_M; 1502172db5f9SMaciej Machnikowski aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M; 1503172db5f9SMaciej Machnikowski 1504172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE BIT(0) 1505172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE BIT(1) 1506172db5f9SMaciej Machnikowski 1507172db5f9SMaciej Machnikowski /* set event level to requested edge */ 1508172db5f9SMaciej Machnikowski if (extts_flags & PTP_FALLING_EDGE) 1509172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE; 1510172db5f9SMaciej Machnikowski if (extts_flags & PTP_RISING_EDGE) 1511172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE; 1512172db5f9SMaciej Machnikowski 1513172db5f9SMaciej Machnikowski /* Write GPIO CTL reg. 1514172db5f9SMaciej Machnikowski * 0x1 is input sampled by EVENT register(channel) 1515172db5f9SMaciej Machnikowski * + num_in_channels * tmr_idx 1516172db5f9SMaciej Machnikowski */ 1517172db5f9SMaciej Machnikowski func = 1 + chan + (tmr_idx * 3); 1518172db5f9SMaciej Machnikowski gpio_reg = ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & 1519172db5f9SMaciej Machnikowski GLGEN_GPIO_CTL_PIN_FUNC_M); 1520172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan |= (1 << chan); 1521172db5f9SMaciej Machnikowski } else { 1522172db5f9SMaciej Machnikowski /* clear the values we set to reset defaults */ 1523172db5f9SMaciej Machnikowski aux_reg = 0; 1524172db5f9SMaciej Machnikowski gpio_reg = 0; 1525172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan &= ~(1 << chan); 1526172db5f9SMaciej Machnikowski if (!pf->ptp.ext_ts_chan) 1527172db5f9SMaciej Machnikowski irq_reg &= ~PFINT_OICR_TSYN_EVNT_M; 1528172db5f9SMaciej Machnikowski } 1529172db5f9SMaciej Machnikowski 1530172db5f9SMaciej Machnikowski wr32(hw, PFINT_OICR_ENA, irq_reg); 1531172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg); 1532172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg); 1533172db5f9SMaciej Machnikowski 1534172db5f9SMaciej Machnikowski return 0; 1535172db5f9SMaciej Machnikowski } 1536172db5f9SMaciej Machnikowski 1537172db5f9SMaciej Machnikowski /** 1538172db5f9SMaciej Machnikowski * ice_ptp_cfg_clkout - Configure clock to generate periodic wave 1539172db5f9SMaciej Machnikowski * @pf: Board private structure 1540172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 1541172db5f9SMaciej Machnikowski * @config: desired periodic clk configuration. NULL will disable channel 1542172db5f9SMaciej Machnikowski * @store: If set to true the values will be stored 1543172db5f9SMaciej Machnikowski * 1544172db5f9SMaciej Machnikowski * Configure the internal clock generator modules to generate the clock wave of 1545172db5f9SMaciej Machnikowski * specified period. 1546172db5f9SMaciej Machnikowski */ 1547172db5f9SMaciej Machnikowski static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan, 1548172db5f9SMaciej Machnikowski struct ice_perout_channel *config, bool store) 1549172db5f9SMaciej Machnikowski { 1550172db5f9SMaciej Machnikowski u64 current_time, period, start_time, phase; 1551172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1552172db5f9SMaciej Machnikowski u32 func, val, gpio_pin; 1553172db5f9SMaciej Machnikowski u8 tmr_idx; 1554172db5f9SMaciej Machnikowski 1555172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1556172db5f9SMaciej Machnikowski 1557172db5f9SMaciej Machnikowski /* 0. Reset mode & out_en in AUX_OUT */ 1558172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0); 1559172db5f9SMaciej Machnikowski 1560172db5f9SMaciej Machnikowski /* If we're disabling the output, clear out CLKO and TGT and keep 1561172db5f9SMaciej Machnikowski * output level low 1562172db5f9SMaciej Machnikowski */ 1563172db5f9SMaciej Machnikowski if (!config || !config->ena) { 1564172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), 0); 1565172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), 0); 1566172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), 0); 1567172db5f9SMaciej Machnikowski 1568172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M; 1569172db5f9SMaciej Machnikowski gpio_pin = pf->ptp.perout_channels[chan].gpio_pin; 1570172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 1571172db5f9SMaciej Machnikowski 1572172db5f9SMaciej Machnikowski /* Store the value if requested */ 1573172db5f9SMaciej Machnikowski if (store) 1574172db5f9SMaciej Machnikowski memset(&pf->ptp.perout_channels[chan], 0, 1575172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 1576172db5f9SMaciej Machnikowski 1577172db5f9SMaciej Machnikowski return 0; 1578172db5f9SMaciej Machnikowski } 1579172db5f9SMaciej Machnikowski period = config->period; 1580172db5f9SMaciej Machnikowski start_time = config->start_time; 1581172db5f9SMaciej Machnikowski div64_u64_rem(start_time, period, &phase); 1582172db5f9SMaciej Machnikowski gpio_pin = config->gpio_pin; 1583172db5f9SMaciej Machnikowski 1584172db5f9SMaciej Machnikowski /* 1. Write clkout with half of required period value */ 1585172db5f9SMaciej Machnikowski if (period & 0x1) { 1586172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n"); 1587172db5f9SMaciej Machnikowski goto err; 1588172db5f9SMaciej Machnikowski } 1589172db5f9SMaciej Machnikowski 1590172db5f9SMaciej Machnikowski period >>= 1; 1591172db5f9SMaciej Machnikowski 1592172db5f9SMaciej Machnikowski /* For proper operation, the GLTSYN_CLKO must be larger than clock tick 1593172db5f9SMaciej Machnikowski */ 1594172db5f9SMaciej Machnikowski #define MIN_PULSE 3 1595172db5f9SMaciej Machnikowski if (period <= MIN_PULSE || period > U32_MAX) { 1596172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be > %d && < 2^33", 1597172db5f9SMaciej Machnikowski MIN_PULSE * 2); 1598172db5f9SMaciej Machnikowski goto err; 1599172db5f9SMaciej Machnikowski } 1600172db5f9SMaciej Machnikowski 1601172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period)); 1602172db5f9SMaciej Machnikowski 1603172db5f9SMaciej Machnikowski /* Allow time for programming before start_time is hit */ 1604172db5f9SMaciej Machnikowski current_time = ice_ptp_read_src_clk_reg(pf, NULL); 1605172db5f9SMaciej Machnikowski 1606172db5f9SMaciej Machnikowski /* if start time is in the past start the timer at the nearest second 1607172db5f9SMaciej Machnikowski * maintaining phase 1608172db5f9SMaciej Machnikowski */ 1609172db5f9SMaciej Machnikowski if (start_time < current_time) 16105f773519SMaciej Machnikowski start_time = div64_u64(current_time + NSEC_PER_SEC - 1, 1611172db5f9SMaciej Machnikowski NSEC_PER_SEC) * NSEC_PER_SEC + phase; 1612172db5f9SMaciej Machnikowski 16133a749623SJacob Keller if (ice_is_e810(hw)) 1614172db5f9SMaciej Machnikowski start_time -= E810_OUT_PROP_DELAY_NS; 16153a749623SJacob Keller else 16163a749623SJacob Keller start_time -= ice_e822_pps_delay(ice_e822_time_ref(hw)); 1617172db5f9SMaciej Machnikowski 1618172db5f9SMaciej Machnikowski /* 2. Write TARGET time */ 1619172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start_time)); 1620172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start_time)); 1621172db5f9SMaciej Machnikowski 1622172db5f9SMaciej Machnikowski /* 3. Write AUX_OUT register */ 1623172db5f9SMaciej Machnikowski val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M; 1624172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val); 1625172db5f9SMaciej Machnikowski 1626172db5f9SMaciej Machnikowski /* 4. write GPIO CTL reg */ 1627172db5f9SMaciej Machnikowski func = 8 + chan + (tmr_idx * 4); 1628172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M | 1629172db5f9SMaciej Machnikowski ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & GLGEN_GPIO_CTL_PIN_FUNC_M); 1630172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 1631172db5f9SMaciej Machnikowski 1632172db5f9SMaciej Machnikowski /* Store the value if requested */ 1633172db5f9SMaciej Machnikowski if (store) { 1634172db5f9SMaciej Machnikowski memcpy(&pf->ptp.perout_channels[chan], config, 1635172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 1636172db5f9SMaciej Machnikowski pf->ptp.perout_channels[chan].start_time = phase; 1637172db5f9SMaciej Machnikowski } 1638172db5f9SMaciej Machnikowski 1639172db5f9SMaciej Machnikowski return 0; 1640172db5f9SMaciej Machnikowski err: 1641172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "PTP failed to cfg per_clk\n"); 1642172db5f9SMaciej Machnikowski return -EFAULT; 1643172db5f9SMaciej Machnikowski } 1644172db5f9SMaciej Machnikowski 1645172db5f9SMaciej Machnikowski /** 16469ee31343SJacob Keller * ice_ptp_disable_all_clkout - Disable all currently configured outputs 16479ee31343SJacob Keller * @pf: pointer to the PF structure 16489ee31343SJacob Keller * 16499ee31343SJacob Keller * Disable all currently configured clock outputs. This is necessary before 16509ee31343SJacob Keller * certain changes to the PTP hardware clock. Use ice_ptp_enable_all_clkout to 16519ee31343SJacob Keller * re-enable the clocks again. 16529ee31343SJacob Keller */ 16539ee31343SJacob Keller static void ice_ptp_disable_all_clkout(struct ice_pf *pf) 16549ee31343SJacob Keller { 16559ee31343SJacob Keller uint i; 16569ee31343SJacob Keller 16579ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++) 16589ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena) 16599ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, NULL, false); 16609ee31343SJacob Keller } 16619ee31343SJacob Keller 16629ee31343SJacob Keller /** 16639ee31343SJacob Keller * ice_ptp_enable_all_clkout - Enable all configured periodic clock outputs 16649ee31343SJacob Keller * @pf: pointer to the PF structure 16659ee31343SJacob Keller * 16669ee31343SJacob Keller * Enable all currently configured clock outputs. Use this after 16679ee31343SJacob Keller * ice_ptp_disable_all_clkout to reconfigure the output signals according to 16689ee31343SJacob Keller * their configuration. 16699ee31343SJacob Keller */ 16709ee31343SJacob Keller static void ice_ptp_enable_all_clkout(struct ice_pf *pf) 16719ee31343SJacob Keller { 16729ee31343SJacob Keller uint i; 16739ee31343SJacob Keller 16749ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++) 16759ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena) 16769ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, &pf->ptp.perout_channels[i], 16779ee31343SJacob Keller false); 16789ee31343SJacob Keller } 16799ee31343SJacob Keller 16809ee31343SJacob Keller /** 1681172db5f9SMaciej Machnikowski * ice_ptp_gpio_enable_e810 - Enable/disable ancillary features of PHC 1682172db5f9SMaciej Machnikowski * @info: the driver's PTP info structure 1683172db5f9SMaciej Machnikowski * @rq: The requested feature to change 1684172db5f9SMaciej Machnikowski * @on: Enable/disable flag 1685172db5f9SMaciej Machnikowski */ 1686172db5f9SMaciej Machnikowski static int 1687172db5f9SMaciej Machnikowski ice_ptp_gpio_enable_e810(struct ptp_clock_info *info, 1688172db5f9SMaciej Machnikowski struct ptp_clock_request *rq, int on) 1689172db5f9SMaciej Machnikowski { 1690172db5f9SMaciej Machnikowski struct ice_pf *pf = ptp_info_to_pf(info); 1691172db5f9SMaciej Machnikowski struct ice_perout_channel clk_cfg = {0}; 1692325b2064SMaciej Machnikowski bool sma_pres = false; 1693172db5f9SMaciej Machnikowski unsigned int chan; 1694172db5f9SMaciej Machnikowski u32 gpio_pin; 1695172db5f9SMaciej Machnikowski int err; 1696172db5f9SMaciej Machnikowski 1697325b2064SMaciej Machnikowski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) 1698325b2064SMaciej Machnikowski sma_pres = true; 1699325b2064SMaciej Machnikowski 1700172db5f9SMaciej Machnikowski switch (rq->type) { 1701172db5f9SMaciej Machnikowski case PTP_CLK_REQ_PEROUT: 1702172db5f9SMaciej Machnikowski chan = rq->perout.index; 1703325b2064SMaciej Machnikowski if (sma_pres) { 1704325b2064SMaciej Machnikowski if (chan == ice_pin_desc_e810t[SMA1].chan) 1705325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20; 1706325b2064SMaciej Machnikowski else if (chan == ice_pin_desc_e810t[SMA2].chan) 1707325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22; 1708172db5f9SMaciej Machnikowski else 1709325b2064SMaciej Machnikowski return -1; 1710325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) { 1711325b2064SMaciej Machnikowski if (chan == 0) 1712325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20; 1713325b2064SMaciej Machnikowski else 1714325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22; 1715325b2064SMaciej Machnikowski } else if (chan == PPS_CLK_GEN_CHAN) { 1716325b2064SMaciej Machnikowski clk_cfg.gpio_pin = PPS_PIN_INDEX; 1717325b2064SMaciej Machnikowski } else { 1718172db5f9SMaciej Machnikowski clk_cfg.gpio_pin = chan; 1719325b2064SMaciej Machnikowski } 1720172db5f9SMaciej Machnikowski 1721172db5f9SMaciej Machnikowski clk_cfg.period = ((rq->perout.period.sec * NSEC_PER_SEC) + 1722172db5f9SMaciej Machnikowski rq->perout.period.nsec); 1723172db5f9SMaciej Machnikowski clk_cfg.start_time = ((rq->perout.start.sec * NSEC_PER_SEC) + 1724172db5f9SMaciej Machnikowski rq->perout.start.nsec); 1725172db5f9SMaciej Machnikowski clk_cfg.ena = !!on; 1726172db5f9SMaciej Machnikowski 1727172db5f9SMaciej Machnikowski err = ice_ptp_cfg_clkout(pf, chan, &clk_cfg, true); 1728172db5f9SMaciej Machnikowski break; 1729172db5f9SMaciej Machnikowski case PTP_CLK_REQ_EXTTS: 1730172db5f9SMaciej Machnikowski chan = rq->extts.index; 1731325b2064SMaciej Machnikowski if (sma_pres) { 1732325b2064SMaciej Machnikowski if (chan < ice_pin_desc_e810t[SMA2].chan) 1733325b2064SMaciej Machnikowski gpio_pin = GPIO_21; 1734325b2064SMaciej Machnikowski else 1735325b2064SMaciej Machnikowski gpio_pin = GPIO_23; 1736325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) { 1737325b2064SMaciej Machnikowski if (chan == 0) 1738325b2064SMaciej Machnikowski gpio_pin = GPIO_21; 1739325b2064SMaciej Machnikowski else 1740325b2064SMaciej Machnikowski gpio_pin = GPIO_23; 1741325b2064SMaciej Machnikowski } else { 1742172db5f9SMaciej Machnikowski gpio_pin = chan; 1743325b2064SMaciej Machnikowski } 1744172db5f9SMaciej Machnikowski 1745172db5f9SMaciej Machnikowski err = ice_ptp_cfg_extts(pf, !!on, chan, gpio_pin, 1746172db5f9SMaciej Machnikowski rq->extts.flags); 1747172db5f9SMaciej Machnikowski break; 1748172db5f9SMaciej Machnikowski default: 1749172db5f9SMaciej Machnikowski return -EOPNOTSUPP; 1750172db5f9SMaciej Machnikowski } 1751172db5f9SMaciej Machnikowski 1752172db5f9SMaciej Machnikowski return err; 1753172db5f9SMaciej Machnikowski } 1754172db5f9SMaciej Machnikowski 1755172db5f9SMaciej Machnikowski /** 175606c16d89SJacob Keller * ice_ptp_gettimex64 - Get the time of the clock 175706c16d89SJacob Keller * @info: the driver's PTP info structure 175806c16d89SJacob Keller * @ts: timespec64 structure to hold the current time value 175906c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 176006c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 176106c16d89SJacob Keller * 176206c16d89SJacob Keller * Read the device clock and return the correct value on ns, after converting it 176306c16d89SJacob Keller * into a timespec struct. 176406c16d89SJacob Keller */ 176506c16d89SJacob Keller static int 176606c16d89SJacob Keller ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts, 176706c16d89SJacob Keller struct ptp_system_timestamp *sts) 176806c16d89SJacob Keller { 176906c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 177006c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 177106c16d89SJacob Keller 177206c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 177306c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n"); 177406c16d89SJacob Keller return -EBUSY; 177506c16d89SJacob Keller } 177606c16d89SJacob Keller 177706c16d89SJacob Keller ice_ptp_read_time(pf, ts, sts); 177806c16d89SJacob Keller ice_ptp_unlock(hw); 177906c16d89SJacob Keller 178006c16d89SJacob Keller return 0; 178106c16d89SJacob Keller } 178206c16d89SJacob Keller 178306c16d89SJacob Keller /** 178406c16d89SJacob Keller * ice_ptp_settime64 - Set the time of the clock 178506c16d89SJacob Keller * @info: the driver's PTP info structure 178606c16d89SJacob Keller * @ts: timespec64 structure that holds the new time value 178706c16d89SJacob Keller * 178806c16d89SJacob Keller * Set the device clock to the user input value. The conversion from timespec 178906c16d89SJacob Keller * to ns happens in the write function. 179006c16d89SJacob Keller */ 179106c16d89SJacob Keller static int 179206c16d89SJacob Keller ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts) 179306c16d89SJacob Keller { 179406c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 179506c16d89SJacob Keller struct timespec64 ts64 = *ts; 179606c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 179706c16d89SJacob Keller int err; 179806c16d89SJacob Keller 17993a749623SJacob Keller /* For Vernier mode, we need to recalibrate after new settime 18003a749623SJacob Keller * Start with disabling timestamp block 18013a749623SJacob Keller */ 18023a749623SJacob Keller if (pf->ptp.port.link_up) 18033a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port); 18043a749623SJacob Keller 180506c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 180606c16d89SJacob Keller err = -EBUSY; 180706c16d89SJacob Keller goto exit; 180806c16d89SJacob Keller } 180906c16d89SJacob Keller 18109ee31343SJacob Keller /* Disable periodic outputs */ 18119ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 18129ee31343SJacob Keller 181306c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts64); 181406c16d89SJacob Keller ice_ptp_unlock(hw); 181506c16d89SJacob Keller 181677a78115SJacob Keller if (!err) 1817b1a582e6SJacob Keller ice_ptp_reset_cached_phctime(pf); 181877a78115SJacob Keller 18199ee31343SJacob Keller /* Reenable periodic outputs */ 18209ee31343SJacob Keller ice_ptp_enable_all_clkout(pf); 18213a749623SJacob Keller 18223a749623SJacob Keller /* Recalibrate and re-enable timestamp block */ 18233a749623SJacob Keller if (pf->ptp.port.link_up) 18243a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port); 182506c16d89SJacob Keller exit: 182606c16d89SJacob Keller if (err) { 182706c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err); 182806c16d89SJacob Keller return err; 182906c16d89SJacob Keller } 183006c16d89SJacob Keller 183106c16d89SJacob Keller return 0; 183206c16d89SJacob Keller } 183306c16d89SJacob Keller 183406c16d89SJacob Keller /** 183506c16d89SJacob Keller * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment 183606c16d89SJacob Keller * @info: the driver's PTP info structure 183706c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 183806c16d89SJacob Keller */ 183906c16d89SJacob Keller static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta) 184006c16d89SJacob Keller { 184106c16d89SJacob Keller struct timespec64 now, then; 1842ed22d9c8STom Rix int ret; 184306c16d89SJacob Keller 184406c16d89SJacob Keller then = ns_to_timespec64(delta); 1845ed22d9c8STom Rix ret = ice_ptp_gettimex64(info, &now, NULL); 1846ed22d9c8STom Rix if (ret) 1847ed22d9c8STom Rix return ret; 184806c16d89SJacob Keller now = timespec64_add(now, then); 184906c16d89SJacob Keller 185006c16d89SJacob Keller return ice_ptp_settime64(info, (const struct timespec64 *)&now); 185106c16d89SJacob Keller } 185206c16d89SJacob Keller 185306c16d89SJacob Keller /** 185406c16d89SJacob Keller * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta 185506c16d89SJacob Keller * @info: the driver's PTP info structure 185606c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 185706c16d89SJacob Keller */ 185806c16d89SJacob Keller static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta) 185906c16d89SJacob Keller { 186006c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 186106c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 186206c16d89SJacob Keller struct device *dev; 186306c16d89SJacob Keller int err; 186406c16d89SJacob Keller 186506c16d89SJacob Keller dev = ice_pf_to_dev(pf); 186606c16d89SJacob Keller 186706c16d89SJacob Keller /* Hardware only supports atomic adjustments using signed 32-bit 186806c16d89SJacob Keller * integers. For any adjustment outside this range, perform 186906c16d89SJacob Keller * a non-atomic get->adjust->set flow. 187006c16d89SJacob Keller */ 187106c16d89SJacob Keller if (delta > S32_MAX || delta < S32_MIN) { 187206c16d89SJacob Keller dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta); 187306c16d89SJacob Keller return ice_ptp_adjtime_nonatomic(info, delta); 187406c16d89SJacob Keller } 187506c16d89SJacob Keller 187606c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 187706c16d89SJacob Keller dev_err(dev, "PTP failed to acquire semaphore in adjtime\n"); 187806c16d89SJacob Keller return -EBUSY; 187906c16d89SJacob Keller } 188006c16d89SJacob Keller 18819ee31343SJacob Keller /* Disable periodic outputs */ 18829ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 18839ee31343SJacob Keller 188406c16d89SJacob Keller err = ice_ptp_write_adj(pf, delta); 188506c16d89SJacob Keller 18869ee31343SJacob Keller /* Reenable periodic outputs */ 18879ee31343SJacob Keller ice_ptp_enable_all_clkout(pf); 18889ee31343SJacob Keller 188906c16d89SJacob Keller ice_ptp_unlock(hw); 189006c16d89SJacob Keller 189106c16d89SJacob Keller if (err) { 189206c16d89SJacob Keller dev_err(dev, "PTP failed to adjust time, err %d\n", err); 189306c16d89SJacob Keller return err; 189406c16d89SJacob Keller } 189506c16d89SJacob Keller 1896b1a582e6SJacob Keller ice_ptp_reset_cached_phctime(pf); 189777a78115SJacob Keller 189806c16d89SJacob Keller return 0; 189906c16d89SJacob Keller } 190006c16d89SJacob Keller 190113a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS 190213a64f0bSJacob Keller /** 190313a64f0bSJacob Keller * ice_ptp_get_syncdevicetime - Get the cross time stamp info 190413a64f0bSJacob Keller * @device: Current device time 190513a64f0bSJacob Keller * @system: System counter value read synchronously with device time 190613a64f0bSJacob Keller * @ctx: Context provided by timekeeping code 190713a64f0bSJacob Keller * 190813a64f0bSJacob Keller * Read device and system (ART) clock simultaneously and return the corrected 190913a64f0bSJacob Keller * clock values in ns. 191013a64f0bSJacob Keller */ 191113a64f0bSJacob Keller static int 191213a64f0bSJacob Keller ice_ptp_get_syncdevicetime(ktime_t *device, 191313a64f0bSJacob Keller struct system_counterval_t *system, 191413a64f0bSJacob Keller void *ctx) 191513a64f0bSJacob Keller { 191613a64f0bSJacob Keller struct ice_pf *pf = (struct ice_pf *)ctx; 191713a64f0bSJacob Keller struct ice_hw *hw = &pf->hw; 191813a64f0bSJacob Keller u32 hh_lock, hh_art_ctl; 191913a64f0bSJacob Keller int i; 192013a64f0bSJacob Keller 192113a64f0bSJacob Keller /* Get the HW lock */ 192213a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id)); 192313a64f0bSJacob Keller if (hh_lock & PFHH_SEM_BUSY_M) { 192413a64f0bSJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get hh lock\n"); 192513a64f0bSJacob Keller return -EFAULT; 192613a64f0bSJacob Keller } 192713a64f0bSJacob Keller 192813a64f0bSJacob Keller /* Start the ART and device clock sync sequence */ 192913a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL); 193013a64f0bSJacob Keller hh_art_ctl = hh_art_ctl | GLHH_ART_CTL_ACTIVE_M; 193113a64f0bSJacob Keller wr32(hw, GLHH_ART_CTL, hh_art_ctl); 193213a64f0bSJacob Keller 193313a64f0bSJacob Keller #define MAX_HH_LOCK_TRIES 100 193413a64f0bSJacob Keller 193513a64f0bSJacob Keller for (i = 0; i < MAX_HH_LOCK_TRIES; i++) { 193613a64f0bSJacob Keller /* Wait for sync to complete */ 193713a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL); 193813a64f0bSJacob Keller if (hh_art_ctl & GLHH_ART_CTL_ACTIVE_M) { 193913a64f0bSJacob Keller udelay(1); 194013a64f0bSJacob Keller continue; 194113a64f0bSJacob Keller } else { 194213a64f0bSJacob Keller u32 hh_ts_lo, hh_ts_hi, tmr_idx; 194313a64f0bSJacob Keller u64 hh_ts; 194413a64f0bSJacob Keller 194513a64f0bSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 194613a64f0bSJacob Keller /* Read ART time */ 194713a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLHH_ART_TIME_L); 194813a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLHH_ART_TIME_H); 194913a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo; 195013a64f0bSJacob Keller *system = convert_art_ns_to_tsc(hh_ts); 195113a64f0bSJacob Keller /* Read Device source clock time */ 195213a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLTSYN_HHTIME_L(tmr_idx)); 195313a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLTSYN_HHTIME_H(tmr_idx)); 195413a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo; 195513a64f0bSJacob Keller *device = ns_to_ktime(hh_ts); 195613a64f0bSJacob Keller break; 195713a64f0bSJacob Keller } 195813a64f0bSJacob Keller } 195913a64f0bSJacob Keller /* Release HW lock */ 196013a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id)); 196113a64f0bSJacob Keller hh_lock = hh_lock & ~PFHH_SEM_BUSY_M; 196213a64f0bSJacob Keller wr32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), hh_lock); 196313a64f0bSJacob Keller 196413a64f0bSJacob Keller if (i == MAX_HH_LOCK_TRIES) 196513a64f0bSJacob Keller return -ETIMEDOUT; 196613a64f0bSJacob Keller 196713a64f0bSJacob Keller return 0; 196813a64f0bSJacob Keller } 196913a64f0bSJacob Keller 197013a64f0bSJacob Keller /** 197113a64f0bSJacob Keller * ice_ptp_getcrosststamp_e822 - Capture a device cross timestamp 197213a64f0bSJacob Keller * @info: the driver's PTP info structure 197313a64f0bSJacob Keller * @cts: The memory to fill the cross timestamp info 197413a64f0bSJacob Keller * 197513a64f0bSJacob Keller * Capture a cross timestamp between the ART and the device PTP hardware 197613a64f0bSJacob Keller * clock. Fill the cross timestamp information and report it back to the 197713a64f0bSJacob Keller * caller. 197813a64f0bSJacob Keller * 197913a64f0bSJacob Keller * This is only valid for E822 devices which have support for generating the 198013a64f0bSJacob Keller * cross timestamp via PCIe PTM. 198113a64f0bSJacob Keller * 198213a64f0bSJacob Keller * In order to correctly correlate the ART timestamp back to the TSC time, the 198313a64f0bSJacob Keller * CPU must have X86_FEATURE_TSC_KNOWN_FREQ. 198413a64f0bSJacob Keller */ 198513a64f0bSJacob Keller static int 198613a64f0bSJacob Keller ice_ptp_getcrosststamp_e822(struct ptp_clock_info *info, 198713a64f0bSJacob Keller struct system_device_crosststamp *cts) 198813a64f0bSJacob Keller { 198913a64f0bSJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 199013a64f0bSJacob Keller 199113a64f0bSJacob Keller return get_device_system_crosststamp(ice_ptp_get_syncdevicetime, 199213a64f0bSJacob Keller pf, NULL, cts); 199313a64f0bSJacob Keller } 199413a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */ 199513a64f0bSJacob Keller 199606c16d89SJacob Keller /** 199777a78115SJacob Keller * ice_ptp_get_ts_config - ioctl interface to read the timestamping config 199877a78115SJacob Keller * @pf: Board private structure 199977a78115SJacob Keller * @ifr: ioctl data 200077a78115SJacob Keller * 200177a78115SJacob Keller * Copy the timestamping config to user buffer 200277a78115SJacob Keller */ 200377a78115SJacob Keller int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr) 200477a78115SJacob Keller { 200577a78115SJacob Keller struct hwtstamp_config *config; 200677a78115SJacob Keller 200777a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 200877a78115SJacob Keller return -EIO; 200977a78115SJacob Keller 201077a78115SJacob Keller config = &pf->ptp.tstamp_config; 201177a78115SJacob Keller 201277a78115SJacob Keller return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ? 201377a78115SJacob Keller -EFAULT : 0; 201477a78115SJacob Keller } 201577a78115SJacob Keller 201677a78115SJacob Keller /** 201777a78115SJacob Keller * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode 201877a78115SJacob Keller * @pf: Board private structure 201977a78115SJacob Keller * @config: hwtstamp settings requested or saved 202077a78115SJacob Keller */ 202177a78115SJacob Keller static int 202277a78115SJacob Keller ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config) 202377a78115SJacob Keller { 202477a78115SJacob Keller switch (config->tx_type) { 202577a78115SJacob Keller case HWTSTAMP_TX_OFF: 2026ea9b847cSJacob Keller ice_set_tx_tstamp(pf, false); 2027ea9b847cSJacob Keller break; 2028ea9b847cSJacob Keller case HWTSTAMP_TX_ON: 2029ea9b847cSJacob Keller ice_set_tx_tstamp(pf, true); 203077a78115SJacob Keller break; 203177a78115SJacob Keller default: 203277a78115SJacob Keller return -ERANGE; 203377a78115SJacob Keller } 203477a78115SJacob Keller 203577a78115SJacob Keller switch (config->rx_filter) { 203677a78115SJacob Keller case HWTSTAMP_FILTER_NONE: 203777a78115SJacob Keller ice_set_rx_tstamp(pf, false); 203877a78115SJacob Keller break; 203977a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 204077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 204177a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 204277a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_EVENT: 204377a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 204477a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 204577a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_SYNC: 204677a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 204777a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 204877a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 204977a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 205077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 205177a78115SJacob Keller case HWTSTAMP_FILTER_NTP_ALL: 205277a78115SJacob Keller case HWTSTAMP_FILTER_ALL: 205377a78115SJacob Keller ice_set_rx_tstamp(pf, true); 205477a78115SJacob Keller break; 205577a78115SJacob Keller default: 205677a78115SJacob Keller return -ERANGE; 205777a78115SJacob Keller } 205877a78115SJacob Keller 205977a78115SJacob Keller return 0; 206077a78115SJacob Keller } 206177a78115SJacob Keller 206277a78115SJacob Keller /** 206377a78115SJacob Keller * ice_ptp_set_ts_config - ioctl interface to control the timestamping 206477a78115SJacob Keller * @pf: Board private structure 206577a78115SJacob Keller * @ifr: ioctl data 206677a78115SJacob Keller * 206777a78115SJacob Keller * Get the user config and store it 206877a78115SJacob Keller */ 206977a78115SJacob Keller int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr) 207077a78115SJacob Keller { 207177a78115SJacob Keller struct hwtstamp_config config; 207277a78115SJacob Keller int err; 207377a78115SJacob Keller 207477a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 207577a78115SJacob Keller return -EAGAIN; 207677a78115SJacob Keller 207777a78115SJacob Keller if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 207877a78115SJacob Keller return -EFAULT; 207977a78115SJacob Keller 208077a78115SJacob Keller err = ice_ptp_set_timestamp_mode(pf, &config); 208177a78115SJacob Keller if (err) 208277a78115SJacob Keller return err; 208377a78115SJacob Keller 2084e59d75ddSJacob Keller /* Return the actual configuration set */ 2085e59d75ddSJacob Keller config = pf->ptp.tstamp_config; 208677a78115SJacob Keller 208777a78115SJacob Keller return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 208877a78115SJacob Keller -EFAULT : 0; 208977a78115SJacob Keller } 209077a78115SJacob Keller 209177a78115SJacob Keller /** 209277a78115SJacob Keller * ice_ptp_rx_hwtstamp - Check for an Rx timestamp 209377a78115SJacob Keller * @rx_ring: Ring to get the VSI info 209477a78115SJacob Keller * @rx_desc: Receive descriptor 209577a78115SJacob Keller * @skb: Particular skb to send timestamp with 209677a78115SJacob Keller * 209777a78115SJacob Keller * The driver receives a notification in the receive descriptor with timestamp. 209877a78115SJacob Keller * The timestamp is in ns, so we must convert the result first. 209977a78115SJacob Keller */ 210077a78115SJacob Keller void 2101e72bba21SMaciej Fijalkowski ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring, 210277a78115SJacob Keller union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) 210377a78115SJacob Keller { 210477a78115SJacob Keller struct skb_shared_hwtstamps *hwtstamps; 2105b1a582e6SJacob Keller u64 ts_ns, cached_time; 2106b1a582e6SJacob Keller u32 ts_high; 210777a78115SJacob Keller 2108b1a582e6SJacob Keller if (!(rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID)) 2109b1a582e6SJacob Keller return; 2110b1a582e6SJacob Keller 2111b1a582e6SJacob Keller cached_time = READ_ONCE(rx_ring->cached_phctime); 2112b1a582e6SJacob Keller 2113b1a582e6SJacob Keller /* Do not report a timestamp if we don't have a cached PHC time */ 2114b1a582e6SJacob Keller if (!cached_time) 2115b1a582e6SJacob Keller return; 2116b1a582e6SJacob Keller 2117b1a582e6SJacob Keller /* Use ice_ptp_extend_32b_ts directly, using the ring-specific cached 2118b1a582e6SJacob Keller * PHC value, rather than accessing the PF. This also allows us to 2119b1a582e6SJacob Keller * simply pass the upper 32bits of nanoseconds directly. Calling 2120b1a582e6SJacob Keller * ice_ptp_extend_40b_ts is unnecessary as it would just discard these 2121b1a582e6SJacob Keller * bits itself. 212277a78115SJacob Keller */ 212377a78115SJacob Keller ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high); 2124b1a582e6SJacob Keller ts_ns = ice_ptp_extend_32b_ts(cached_time, ts_high); 212577a78115SJacob Keller 212677a78115SJacob Keller hwtstamps = skb_hwtstamps(skb); 212777a78115SJacob Keller memset(hwtstamps, 0, sizeof(*hwtstamps)); 212877a78115SJacob Keller hwtstamps->hwtstamp = ns_to_ktime(ts_ns); 212977a78115SJacob Keller } 213077a78115SJacob Keller 213177a78115SJacob Keller /** 2132325b2064SMaciej Machnikowski * ice_ptp_disable_sma_pins_e810t - Disable E810-T SMA pins 2133325b2064SMaciej Machnikowski * @pf: pointer to the PF structure 2134325b2064SMaciej Machnikowski * @info: PTP clock info structure 2135325b2064SMaciej Machnikowski * 2136325b2064SMaciej Machnikowski * Disable the OS access to the SMA pins. Called to clear out the OS 2137325b2064SMaciej Machnikowski * indications of pin support when we fail to setup the E810-T SMA control 2138325b2064SMaciej Machnikowski * register. 2139325b2064SMaciej Machnikowski */ 2140325b2064SMaciej Machnikowski static void 2141325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 2142325b2064SMaciej Machnikowski { 2143325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf); 2144325b2064SMaciej Machnikowski 2145325b2064SMaciej Machnikowski dev_warn(dev, "Failed to configure E810-T SMA pin control\n"); 2146325b2064SMaciej Machnikowski 2147325b2064SMaciej Machnikowski info->enable = NULL; 2148325b2064SMaciej Machnikowski info->verify = NULL; 2149325b2064SMaciej Machnikowski info->n_pins = 0; 2150325b2064SMaciej Machnikowski info->n_ext_ts = 0; 2151325b2064SMaciej Machnikowski info->n_per_out = 0; 2152325b2064SMaciej Machnikowski } 2153325b2064SMaciej Machnikowski 2154325b2064SMaciej Machnikowski /** 2155325b2064SMaciej Machnikowski * ice_ptp_setup_sma_pins_e810t - Setup the SMA pins 2156325b2064SMaciej Machnikowski * @pf: pointer to the PF structure 2157325b2064SMaciej Machnikowski * @info: PTP clock info structure 2158325b2064SMaciej Machnikowski * 2159325b2064SMaciej Machnikowski * Finish setting up the SMA pins by allocating pin_config, and setting it up 2160325b2064SMaciej Machnikowski * according to the current status of the SMA. On failure, disable all of the 2161325b2064SMaciej Machnikowski * extended SMA pin support. 2162325b2064SMaciej Machnikowski */ 2163325b2064SMaciej Machnikowski static void 2164325b2064SMaciej Machnikowski ice_ptp_setup_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 2165325b2064SMaciej Machnikowski { 2166325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf); 2167325b2064SMaciej Machnikowski int err; 2168325b2064SMaciej Machnikowski 2169325b2064SMaciej Machnikowski /* Allocate memory for kernel pins interface */ 2170325b2064SMaciej Machnikowski info->pin_config = devm_kcalloc(dev, info->n_pins, 2171325b2064SMaciej Machnikowski sizeof(*info->pin_config), GFP_KERNEL); 2172325b2064SMaciej Machnikowski if (!info->pin_config) { 2173325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info); 2174325b2064SMaciej Machnikowski return; 2175325b2064SMaciej Machnikowski } 2176325b2064SMaciej Machnikowski 2177325b2064SMaciej Machnikowski /* Read current SMA status */ 2178325b2064SMaciej Machnikowski err = ice_get_sma_config_e810t(&pf->hw, info->pin_config); 2179325b2064SMaciej Machnikowski if (err) 2180325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info); 2181325b2064SMaciej Machnikowski } 2182325b2064SMaciej Machnikowski 2183325b2064SMaciej Machnikowski /** 2184172db5f9SMaciej Machnikowski * ice_ptp_setup_pins_e810 - Setup PTP pins in sysfs 2185896a55aaSAnirudh Venkataramanan * @pf: pointer to the PF instance 2186172db5f9SMaciej Machnikowski * @info: PTP clock capabilities 2187172db5f9SMaciej Machnikowski */ 218843c4958aSArkadiusz Kubalewski static void 218943c4958aSArkadiusz Kubalewski ice_ptp_setup_pins_e810(struct ice_pf *pf, struct ptp_clock_info *info) 2190172db5f9SMaciej Machnikowski { 2191325b2064SMaciej Machnikowski info->n_per_out = N_PER_OUT_E810; 2192896a55aaSAnirudh Venkataramanan 219343c4958aSArkadiusz Kubalewski if (ice_is_feature_supported(pf, ICE_F_PTP_EXTTS)) 2194325b2064SMaciej Machnikowski info->n_ext_ts = N_EXT_TS_E810; 219543c4958aSArkadiusz Kubalewski 219643c4958aSArkadiusz Kubalewski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) { 219743c4958aSArkadiusz Kubalewski info->n_ext_ts = N_EXT_TS_E810; 219843c4958aSArkadiusz Kubalewski info->n_pins = NUM_PTP_PINS_E810T; 219943c4958aSArkadiusz Kubalewski info->verify = ice_verify_pin_e810t; 220043c4958aSArkadiusz Kubalewski 220143c4958aSArkadiusz Kubalewski /* Complete setup of the SMA pins */ 220243c4958aSArkadiusz Kubalewski ice_ptp_setup_sma_pins_e810t(pf, info); 220343c4958aSArkadiusz Kubalewski } 2204172db5f9SMaciej Machnikowski } 2205172db5f9SMaciej Machnikowski 2206172db5f9SMaciej Machnikowski /** 220713a64f0bSJacob Keller * ice_ptp_set_funcs_e822 - Set specialized functions for E822 support 220813a64f0bSJacob Keller * @pf: Board private structure 220913a64f0bSJacob Keller * @info: PTP info to fill 221013a64f0bSJacob Keller * 221113a64f0bSJacob Keller * Assign functions to the PTP capabiltiies structure for E822 devices. 221213a64f0bSJacob Keller * Functions which operate across all device families should be set directly 221313a64f0bSJacob Keller * in ice_ptp_set_caps. Only add functions here which are distinct for E822 221413a64f0bSJacob Keller * devices. 221513a64f0bSJacob Keller */ 221613a64f0bSJacob Keller static void 221713a64f0bSJacob Keller ice_ptp_set_funcs_e822(struct ice_pf *pf, struct ptp_clock_info *info) 221813a64f0bSJacob Keller { 221913a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS 222013a64f0bSJacob Keller if (boot_cpu_has(X86_FEATURE_ART) && 222113a64f0bSJacob Keller boot_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ)) 222213a64f0bSJacob Keller info->getcrosststamp = ice_ptp_getcrosststamp_e822; 222313a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */ 222413a64f0bSJacob Keller } 222513a64f0bSJacob Keller 222613a64f0bSJacob Keller /** 2227172db5f9SMaciej Machnikowski * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support 2228172db5f9SMaciej Machnikowski * @pf: Board private structure 2229172db5f9SMaciej Machnikowski * @info: PTP info to fill 2230172db5f9SMaciej Machnikowski * 2231172db5f9SMaciej Machnikowski * Assign functions to the PTP capabiltiies structure for E810 devices. 2232172db5f9SMaciej Machnikowski * Functions which operate across all device families should be set directly 2233172db5f9SMaciej Machnikowski * in ice_ptp_set_caps. Only add functions here which are distinct for e810 2234172db5f9SMaciej Machnikowski * devices. 2235172db5f9SMaciej Machnikowski */ 2236172db5f9SMaciej Machnikowski static void 2237172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(struct ice_pf *pf, struct ptp_clock_info *info) 2238172db5f9SMaciej Machnikowski { 2239172db5f9SMaciej Machnikowski info->enable = ice_ptp_gpio_enable_e810; 2240896a55aaSAnirudh Venkataramanan ice_ptp_setup_pins_e810(pf, info); 2241172db5f9SMaciej Machnikowski } 2242172db5f9SMaciej Machnikowski 2243172db5f9SMaciej Machnikowski /** 224406c16d89SJacob Keller * ice_ptp_set_caps - Set PTP capabilities 224506c16d89SJacob Keller * @pf: Board private structure 224606c16d89SJacob Keller */ 224706c16d89SJacob Keller static void ice_ptp_set_caps(struct ice_pf *pf) 224806c16d89SJacob Keller { 224906c16d89SJacob Keller struct ptp_clock_info *info = &pf->ptp.info; 225006c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf); 225106c16d89SJacob Keller 225206c16d89SJacob Keller snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk", 225306c16d89SJacob Keller dev_driver_string(dev), dev_name(dev)); 225406c16d89SJacob Keller info->owner = THIS_MODULE; 225506c16d89SJacob Keller info->max_adj = 999999999; 225606c16d89SJacob Keller info->adjtime = ice_ptp_adjtime; 225706c16d89SJacob Keller info->adjfine = ice_ptp_adjfine; 225806c16d89SJacob Keller info->gettimex64 = ice_ptp_gettimex64; 225906c16d89SJacob Keller info->settime64 = ice_ptp_settime64; 2260172db5f9SMaciej Machnikowski 22613a749623SJacob Keller if (ice_is_e810(&pf->hw)) 2262172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(pf, info); 226313a64f0bSJacob Keller else 226413a64f0bSJacob Keller ice_ptp_set_funcs_e822(pf, info); 226506c16d89SJacob Keller } 226606c16d89SJacob Keller 226706c16d89SJacob Keller /** 226806c16d89SJacob Keller * ice_ptp_create_clock - Create PTP clock device for userspace 226906c16d89SJacob Keller * @pf: Board private structure 227006c16d89SJacob Keller * 227106c16d89SJacob Keller * This function creates a new PTP clock device. It only creates one if we 227206c16d89SJacob Keller * don't already have one. Will return error if it can't create one, but success 227306c16d89SJacob Keller * if we already have a device. Should be used by ice_ptp_init to create clock 227406c16d89SJacob Keller * initially, and prevent global resets from creating new clock devices. 227506c16d89SJacob Keller */ 227606c16d89SJacob Keller static long ice_ptp_create_clock(struct ice_pf *pf) 227706c16d89SJacob Keller { 227806c16d89SJacob Keller struct ptp_clock_info *info; 227906c16d89SJacob Keller struct ptp_clock *clock; 228006c16d89SJacob Keller struct device *dev; 228106c16d89SJacob Keller 228206c16d89SJacob Keller /* No need to create a clock device if we already have one */ 228306c16d89SJacob Keller if (pf->ptp.clock) 228406c16d89SJacob Keller return 0; 228506c16d89SJacob Keller 228606c16d89SJacob Keller ice_ptp_set_caps(pf); 228706c16d89SJacob Keller 228806c16d89SJacob Keller info = &pf->ptp.info; 228906c16d89SJacob Keller dev = ice_pf_to_dev(pf); 229006c16d89SJacob Keller 229106c16d89SJacob Keller /* Attempt to register the clock before enabling the hardware. */ 229206c16d89SJacob Keller clock = ptp_clock_register(info, dev); 229306c16d89SJacob Keller if (IS_ERR(clock)) 229406c16d89SJacob Keller return PTR_ERR(clock); 229506c16d89SJacob Keller 229606c16d89SJacob Keller pf->ptp.clock = clock; 229706c16d89SJacob Keller 229806c16d89SJacob Keller return 0; 229906c16d89SJacob Keller } 230006c16d89SJacob Keller 2301ea9b847cSJacob Keller /** 2302ea9b847cSJacob Keller * ice_ptp_request_ts - Request an available Tx timestamp index 2303ea9b847cSJacob Keller * @tx: the PTP Tx timestamp tracker to request from 2304ea9b847cSJacob Keller * @skb: the SKB to associate with this timestamp request 2305ea9b847cSJacob Keller */ 2306ea9b847cSJacob Keller s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb) 2307ea9b847cSJacob Keller { 2308ea9b847cSJacob Keller u8 idx; 2309ea9b847cSJacob Keller 2310ea9b847cSJacob Keller /* Check if this tracker is initialized */ 23113a749623SJacob Keller if (!tx->init || tx->calibrating) 2312ea9b847cSJacob Keller return -1; 2313ea9b847cSJacob Keller 2314ea9b847cSJacob Keller spin_lock(&tx->lock); 2315ea9b847cSJacob Keller /* Find and set the first available index */ 2316ea9b847cSJacob Keller idx = find_first_zero_bit(tx->in_use, tx->len); 2317ea9b847cSJacob Keller if (idx < tx->len) { 2318ea9b847cSJacob Keller /* We got a valid index that no other thread could have set. Store 2319ea9b847cSJacob Keller * a reference to the skb and the start time to allow discarding old 2320ea9b847cSJacob Keller * requests. 2321ea9b847cSJacob Keller */ 2322ea9b847cSJacob Keller set_bit(idx, tx->in_use); 2323ea9b847cSJacob Keller tx->tstamps[idx].start = jiffies; 2324ea9b847cSJacob Keller tx->tstamps[idx].skb = skb_get(skb); 2325ea9b847cSJacob Keller skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 23264c120218SJacob Keller ice_trace(tx_tstamp_request, skb, idx); 2327ea9b847cSJacob Keller } 2328ea9b847cSJacob Keller 2329ea9b847cSJacob Keller spin_unlock(&tx->lock); 2330ea9b847cSJacob Keller 2331ea9b847cSJacob Keller /* return the appropriate PHY timestamp register index, -1 if no 2332ea9b847cSJacob Keller * indexes were available. 2333ea9b847cSJacob Keller */ 2334ea9b847cSJacob Keller if (idx >= tx->len) 2335ea9b847cSJacob Keller return -1; 2336ea9b847cSJacob Keller else 23376b5cbc8cSSergey Temerkhanov return idx + tx->offset; 2338ea9b847cSJacob Keller } 2339ea9b847cSJacob Keller 2340ea9b847cSJacob Keller /** 23411229b339SKarol Kolacinski * ice_ptp_process_ts - Process the PTP Tx timestamps 2342ea9b847cSJacob Keller * @pf: Board private structure 2343ea9b847cSJacob Keller * 23441229b339SKarol Kolacinski * Returns true if timestamps are processed. 2345ea9b847cSJacob Keller */ 23461229b339SKarol Kolacinski bool ice_ptp_process_ts(struct ice_pf *pf) 2347ea9b847cSJacob Keller { 23481229b339SKarol Kolacinski return ice_ptp_tx_tstamp(&pf->ptp.port.tx); 2349ea9b847cSJacob Keller } 2350ea9b847cSJacob Keller 235177a78115SJacob Keller static void ice_ptp_periodic_work(struct kthread_work *work) 235277a78115SJacob Keller { 235377a78115SJacob Keller struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work); 235477a78115SJacob Keller struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 23554503cc7fSArkadiusz Kubalewski int err; 235677a78115SJacob Keller 235777a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 235877a78115SJacob Keller return; 235977a78115SJacob Keller 23604503cc7fSArkadiusz Kubalewski err = ice_ptp_update_cached_phctime(pf); 236177a78115SJacob Keller 2362f020481bSJacob Keller ice_ptp_tx_tstamp_cleanup(pf, &pf->ptp.port.tx); 2363ea9b847cSJacob Keller 23644503cc7fSArkadiusz Kubalewski /* Run twice a second or reschedule if phc update failed */ 236577a78115SJacob Keller kthread_queue_delayed_work(ptp->kworker, &ptp->work, 23664503cc7fSArkadiusz Kubalewski msecs_to_jiffies(err ? 10 : 500)); 236777a78115SJacob Keller } 236877a78115SJacob Keller 236906c16d89SJacob Keller /** 237048096710SKarol Kolacinski * ice_ptp_reset - Initialize PTP hardware clock support after reset 237148096710SKarol Kolacinski * @pf: Board private structure 237248096710SKarol Kolacinski */ 237348096710SKarol Kolacinski void ice_ptp_reset(struct ice_pf *pf) 237448096710SKarol Kolacinski { 237548096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 237648096710SKarol Kolacinski struct ice_hw *hw = &pf->hw; 237748096710SKarol Kolacinski struct timespec64 ts; 23783a749623SJacob Keller int err, itr = 1; 237948096710SKarol Kolacinski u64 time_diff; 238048096710SKarol Kolacinski 238148096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state)) 238248096710SKarol Kolacinski goto pfr; 238348096710SKarol Kolacinski 2384b2ee7256SJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned) 23853a749623SJacob Keller goto reset_ts; 238648096710SKarol Kolacinski 2387b2ee7256SJacob Keller err = ice_ptp_init_phc(hw); 238848096710SKarol Kolacinski if (err) 238948096710SKarol Kolacinski goto err; 239048096710SKarol Kolacinski 239148096710SKarol Kolacinski /* Acquire the global hardware lock */ 239248096710SKarol Kolacinski if (!ice_ptp_lock(hw)) { 239348096710SKarol Kolacinski err = -EBUSY; 239448096710SKarol Kolacinski goto err; 239548096710SKarol Kolacinski } 239648096710SKarol Kolacinski 239748096710SKarol Kolacinski /* Write the increment time value to PHY and LAN */ 239878267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf)); 239948096710SKarol Kolacinski if (err) { 240048096710SKarol Kolacinski ice_ptp_unlock(hw); 240148096710SKarol Kolacinski goto err; 240248096710SKarol Kolacinski } 240348096710SKarol Kolacinski 240448096710SKarol Kolacinski /* Write the initial Time value to PHY and LAN using the cached PHC 240548096710SKarol Kolacinski * time before the reset and time difference between stopping and 240648096710SKarol Kolacinski * starting the clock. 240748096710SKarol Kolacinski */ 240848096710SKarol Kolacinski if (ptp->cached_phc_time) { 240948096710SKarol Kolacinski time_diff = ktime_get_real_ns() - ptp->reset_time; 241048096710SKarol Kolacinski ts = ns_to_timespec64(ptp->cached_phc_time + time_diff); 241148096710SKarol Kolacinski } else { 241248096710SKarol Kolacinski ts = ktime_to_timespec64(ktime_get_real()); 241348096710SKarol Kolacinski } 241448096710SKarol Kolacinski err = ice_ptp_write_init(pf, &ts); 241548096710SKarol Kolacinski if (err) { 241648096710SKarol Kolacinski ice_ptp_unlock(hw); 241748096710SKarol Kolacinski goto err; 241848096710SKarol Kolacinski } 241948096710SKarol Kolacinski 242048096710SKarol Kolacinski /* Release the global hardware lock */ 242148096710SKarol Kolacinski ice_ptp_unlock(hw); 242248096710SKarol Kolacinski 24233a749623SJacob Keller if (!ice_is_e810(hw)) { 24243a749623SJacob Keller /* Enable quad interrupts */ 24253a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr); 24263a749623SJacob Keller if (err) 24273a749623SJacob Keller goto err; 24283a749623SJacob Keller } 24293a749623SJacob Keller 24303a749623SJacob Keller reset_ts: 24313a749623SJacob Keller /* Restart the PHY timestamping block */ 24323a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf); 24333a749623SJacob Keller 243448096710SKarol Kolacinski pfr: 243548096710SKarol Kolacinski /* Init Tx structures */ 2436a69f1cb6SJacob Keller if (ice_is_e810(&pf->hw)) { 243748096710SKarol Kolacinski err = ice_ptp_init_tx_e810(pf, &ptp->port.tx); 2438a69f1cb6SJacob Keller } else { 2439a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp->port.ov_work, 2440a69f1cb6SJacob Keller ice_ptp_wait_for_offset_valid); 24413a749623SJacob Keller err = ice_ptp_init_tx_e822(pf, &ptp->port.tx, 24423a749623SJacob Keller ptp->port.port_num); 2443a69f1cb6SJacob Keller } 244448096710SKarol Kolacinski if (err) 244548096710SKarol Kolacinski goto err; 244648096710SKarol Kolacinski 244748096710SKarol Kolacinski set_bit(ICE_FLAG_PTP, pf->flags); 244848096710SKarol Kolacinski 244948096710SKarol Kolacinski /* Start periodic work going */ 245048096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0); 245148096710SKarol Kolacinski 245248096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP reset successful\n"); 245348096710SKarol Kolacinski return; 245448096710SKarol Kolacinski 245548096710SKarol Kolacinski err: 245648096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP reset failed %d\n", err); 245748096710SKarol Kolacinski } 245848096710SKarol Kolacinski 245948096710SKarol Kolacinski /** 246048096710SKarol Kolacinski * ice_ptp_prepare_for_reset - Prepare PTP for reset 246148096710SKarol Kolacinski * @pf: Board private structure 246248096710SKarol Kolacinski */ 246348096710SKarol Kolacinski void ice_ptp_prepare_for_reset(struct ice_pf *pf) 246448096710SKarol Kolacinski { 246548096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 246648096710SKarol Kolacinski u8 src_tmr; 246748096710SKarol Kolacinski 246848096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags); 246948096710SKarol Kolacinski 247048096710SKarol Kolacinski /* Disable timestamping for both Tx and Rx */ 247148096710SKarol Kolacinski ice_ptp_cfg_timestamp(pf, false); 247248096710SKarol Kolacinski 247348096710SKarol Kolacinski kthread_cancel_delayed_work_sync(&ptp->work); 247448096710SKarol Kolacinski kthread_cancel_work_sync(&ptp->extts_work); 247548096710SKarol Kolacinski 247648096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state)) 247748096710SKarol Kolacinski return; 247848096710SKarol Kolacinski 247948096710SKarol Kolacinski ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 248048096710SKarol Kolacinski 248148096710SKarol Kolacinski /* Disable periodic outputs */ 248248096710SKarol Kolacinski ice_ptp_disable_all_clkout(pf); 248348096710SKarol Kolacinski 248448096710SKarol Kolacinski src_tmr = ice_get_ptp_src_clock_index(&pf->hw); 248548096710SKarol Kolacinski 248648096710SKarol Kolacinski /* Disable source clock */ 248748096710SKarol Kolacinski wr32(&pf->hw, GLTSYN_ENA(src_tmr), (u32)~GLTSYN_ENA_TSYN_ENA_M); 248848096710SKarol Kolacinski 248948096710SKarol Kolacinski /* Acquire PHC and system timer to restore after reset */ 249048096710SKarol Kolacinski ptp->reset_time = ktime_get_real_ns(); 249148096710SKarol Kolacinski } 249248096710SKarol Kolacinski 249348096710SKarol Kolacinski /** 249406c16d89SJacob Keller * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device 249506c16d89SJacob Keller * @pf: Board private structure 249606c16d89SJacob Keller * 249706c16d89SJacob Keller * Setup and initialize a PTP clock device that represents the device hardware 249806c16d89SJacob Keller * clock. Save the clock index for other functions connected to the same 249906c16d89SJacob Keller * hardware resource. 250006c16d89SJacob Keller */ 250106c16d89SJacob Keller static int ice_ptp_init_owner(struct ice_pf *pf) 250206c16d89SJacob Keller { 250306c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 250406c16d89SJacob Keller struct timespec64 ts; 25053a749623SJacob Keller int err, itr = 1; 250606c16d89SJacob Keller 2507b2ee7256SJacob Keller err = ice_ptp_init_phc(hw); 2508b2ee7256SJacob Keller if (err) { 2509b2ee7256SJacob Keller dev_err(ice_pf_to_dev(pf), "Failed to initialize PHC, err %d\n", 2510b2ee7256SJacob Keller err); 2511b2ee7256SJacob Keller return err; 2512b2ee7256SJacob Keller } 251306c16d89SJacob Keller 251406c16d89SJacob Keller /* Acquire the global hardware lock */ 251506c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 251606c16d89SJacob Keller err = -EBUSY; 251706c16d89SJacob Keller goto err_exit; 251806c16d89SJacob Keller } 251906c16d89SJacob Keller 252006c16d89SJacob Keller /* Write the increment time value to PHY and LAN */ 252178267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf)); 252206c16d89SJacob Keller if (err) { 252306c16d89SJacob Keller ice_ptp_unlock(hw); 252406c16d89SJacob Keller goto err_exit; 252506c16d89SJacob Keller } 252606c16d89SJacob Keller 252706c16d89SJacob Keller ts = ktime_to_timespec64(ktime_get_real()); 252806c16d89SJacob Keller /* Write the initial Time value to PHY and LAN */ 252906c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts); 253006c16d89SJacob Keller if (err) { 253106c16d89SJacob Keller ice_ptp_unlock(hw); 253206c16d89SJacob Keller goto err_exit; 253306c16d89SJacob Keller } 253406c16d89SJacob Keller 253506c16d89SJacob Keller /* Release the global hardware lock */ 253606c16d89SJacob Keller ice_ptp_unlock(hw); 253706c16d89SJacob Keller 25383a749623SJacob Keller if (!ice_is_e810(hw)) { 25393a749623SJacob Keller /* Enable quad interrupts */ 25403a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr); 25413a749623SJacob Keller if (err) 25423a749623SJacob Keller goto err_exit; 25433a749623SJacob Keller } 25443a749623SJacob Keller 254506c16d89SJacob Keller /* Ensure we have a clock device */ 254606c16d89SJacob Keller err = ice_ptp_create_clock(pf); 254706c16d89SJacob Keller if (err) 254806c16d89SJacob Keller goto err_clk; 254906c16d89SJacob Keller 255067569a7fSJacob Keller /* Store the PTP clock index for other PFs */ 255167569a7fSJacob Keller ice_set_ptp_clock_index(pf); 255267569a7fSJacob Keller 255306c16d89SJacob Keller return 0; 255406c16d89SJacob Keller 255506c16d89SJacob Keller err_clk: 255606c16d89SJacob Keller pf->ptp.clock = NULL; 255706c16d89SJacob Keller err_exit: 255806c16d89SJacob Keller return err; 255906c16d89SJacob Keller } 256006c16d89SJacob Keller 256106c16d89SJacob Keller /** 256248096710SKarol Kolacinski * ice_ptp_init_work - Initialize PTP work threads 256348096710SKarol Kolacinski * @pf: Board private structure 256448096710SKarol Kolacinski * @ptp: PF PTP structure 256548096710SKarol Kolacinski */ 256648096710SKarol Kolacinski static int ice_ptp_init_work(struct ice_pf *pf, struct ice_ptp *ptp) 256748096710SKarol Kolacinski { 256848096710SKarol Kolacinski struct kthread_worker *kworker; 256948096710SKarol Kolacinski 257048096710SKarol Kolacinski /* Initialize work functions */ 257148096710SKarol Kolacinski kthread_init_delayed_work(&ptp->work, ice_ptp_periodic_work); 257248096710SKarol Kolacinski kthread_init_work(&ptp->extts_work, ice_ptp_extts_work); 257348096710SKarol Kolacinski 257448096710SKarol Kolacinski /* Allocate a kworker for handling work required for the ports 257548096710SKarol Kolacinski * connected to the PTP hardware clock. 257648096710SKarol Kolacinski */ 257748096710SKarol Kolacinski kworker = kthread_create_worker(0, "ice-ptp-%s", 257848096710SKarol Kolacinski dev_name(ice_pf_to_dev(pf))); 257948096710SKarol Kolacinski if (IS_ERR(kworker)) 258048096710SKarol Kolacinski return PTR_ERR(kworker); 258148096710SKarol Kolacinski 258248096710SKarol Kolacinski ptp->kworker = kworker; 258348096710SKarol Kolacinski 258448096710SKarol Kolacinski /* Start periodic work going */ 258548096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0); 258648096710SKarol Kolacinski 258748096710SKarol Kolacinski return 0; 258848096710SKarol Kolacinski } 258948096710SKarol Kolacinski 259048096710SKarol Kolacinski /** 25913a749623SJacob Keller * ice_ptp_init_port - Initialize PTP port structure 25923a749623SJacob Keller * @pf: Board private structure 25933a749623SJacob Keller * @ptp_port: PTP port structure 25943a749623SJacob Keller */ 25953a749623SJacob Keller static int ice_ptp_init_port(struct ice_pf *pf, struct ice_ptp_port *ptp_port) 25963a749623SJacob Keller { 25973a749623SJacob Keller mutex_init(&ptp_port->ps_lock); 25983a749623SJacob Keller 25993a749623SJacob Keller if (ice_is_e810(&pf->hw)) 26003a749623SJacob Keller return ice_ptp_init_tx_e810(pf, &ptp_port->tx); 26013a749623SJacob Keller 2602a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp_port->ov_work, 2603a69f1cb6SJacob Keller ice_ptp_wait_for_offset_valid); 26043a749623SJacob Keller return ice_ptp_init_tx_e822(pf, &ptp_port->tx, ptp_port->port_num); 26053a749623SJacob Keller } 26063a749623SJacob Keller 26073a749623SJacob Keller /** 2608b2ee7256SJacob Keller * ice_ptp_init - Initialize PTP hardware clock support 260906c16d89SJacob Keller * @pf: Board private structure 261006c16d89SJacob Keller * 2611b2ee7256SJacob Keller * Set up the device for interacting with the PTP hardware clock for all 2612b2ee7256SJacob Keller * functions, both the function that owns the clock hardware, and the 2613b2ee7256SJacob Keller * functions connected to the clock hardware. 2614b2ee7256SJacob Keller * 2615b2ee7256SJacob Keller * The clock owner will allocate and register a ptp_clock with the 2616b2ee7256SJacob Keller * PTP_1588_CLOCK infrastructure. All functions allocate a kthread and work 2617b2ee7256SJacob Keller * items used for asynchronous work such as Tx timestamps and periodic work. 261806c16d89SJacob Keller */ 261906c16d89SJacob Keller void ice_ptp_init(struct ice_pf *pf) 262006c16d89SJacob Keller { 262148096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 262206c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 262306c16d89SJacob Keller int err; 262406c16d89SJacob Keller 2625b2ee7256SJacob Keller /* If this function owns the clock hardware, it must allocate and 2626b2ee7256SJacob Keller * configure the PTP clock device to represent it. 2627b2ee7256SJacob Keller */ 262806c16d89SJacob Keller if (hw->func_caps.ts_func_info.src_tmr_owned) { 262906c16d89SJacob Keller err = ice_ptp_init_owner(pf); 263006c16d89SJacob Keller if (err) 263148096710SKarol Kolacinski goto err; 263206c16d89SJacob Keller } 263306c16d89SJacob Keller 26343a749623SJacob Keller ptp->port.port_num = hw->pf_id; 26353a749623SJacob Keller err = ice_ptp_init_port(pf, &ptp->port); 263648096710SKarol Kolacinski if (err) 263748096710SKarol Kolacinski goto err; 263877a78115SJacob Keller 26393a749623SJacob Keller /* Start the PHY timestamping block */ 26403a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf); 26413a749623SJacob Keller 264206c16d89SJacob Keller set_bit(ICE_FLAG_PTP, pf->flags); 264348096710SKarol Kolacinski err = ice_ptp_init_work(pf, ptp); 264448096710SKarol Kolacinski if (err) 264548096710SKarol Kolacinski goto err; 264606c16d89SJacob Keller 264748096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP init successful\n"); 264877a78115SJacob Keller return; 264977a78115SJacob Keller 265048096710SKarol Kolacinski err: 265177a78115SJacob Keller /* If we registered a PTP clock, release it */ 265277a78115SJacob Keller if (pf->ptp.clock) { 265348096710SKarol Kolacinski ptp_clock_unregister(ptp->clock); 265477a78115SJacob Keller pf->ptp.clock = NULL; 265577a78115SJacob Keller } 265648096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags); 265748096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP failed %d\n", err); 265806c16d89SJacob Keller } 265906c16d89SJacob Keller 266006c16d89SJacob Keller /** 266106c16d89SJacob Keller * ice_ptp_release - Disable the driver/HW support and unregister the clock 266206c16d89SJacob Keller * @pf: Board private structure 266306c16d89SJacob Keller * 266406c16d89SJacob Keller * This function handles the cleanup work required from the initialization by 266506c16d89SJacob Keller * clearing out the important information and unregistering the clock 266606c16d89SJacob Keller */ 266706c16d89SJacob Keller void ice_ptp_release(struct ice_pf *pf) 266806c16d89SJacob Keller { 2669fd1b5bebSYongxin Liu if (!test_bit(ICE_FLAG_PTP, pf->flags)) 2670fd1b5bebSYongxin Liu return; 2671fd1b5bebSYongxin Liu 267277a78115SJacob Keller /* Disable timestamping for both Tx and Rx */ 267377a78115SJacob Keller ice_ptp_cfg_timestamp(pf, false); 267477a78115SJacob Keller 2675ea9b847cSJacob Keller ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 2676ea9b847cSJacob Keller 267706c16d89SJacob Keller clear_bit(ICE_FLAG_PTP, pf->flags); 267806c16d89SJacob Keller 267977a78115SJacob Keller kthread_cancel_delayed_work_sync(&pf->ptp.work); 268077a78115SJacob Keller 26813a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port); 26823a749623SJacob Keller mutex_destroy(&pf->ptp.port.ps_lock); 268377a78115SJacob Keller if (pf->ptp.kworker) { 268477a78115SJacob Keller kthread_destroy_worker(pf->ptp.kworker); 268577a78115SJacob Keller pf->ptp.kworker = NULL; 268677a78115SJacob Keller } 268777a78115SJacob Keller 268806c16d89SJacob Keller if (!pf->ptp.clock) 268906c16d89SJacob Keller return; 269006c16d89SJacob Keller 26919ee31343SJacob Keller /* Disable periodic outputs */ 26929ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 26939ee31343SJacob Keller 269467569a7fSJacob Keller ice_clear_ptp_clock_index(pf); 269506c16d89SJacob Keller ptp_clock_unregister(pf->ptp.clock); 269606c16d89SJacob Keller pf->ptp.clock = NULL; 269706c16d89SJacob Keller 269806c16d89SJacob Keller dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n"); 269906c16d89SJacob Keller } 2700