106c16d89SJacob Keller // SPDX-License-Identifier: GPL-2.0 206c16d89SJacob Keller /* Copyright (C) 2021, Intel Corporation. */ 306c16d89SJacob Keller 406c16d89SJacob Keller #include "ice.h" 506c16d89SJacob Keller #include "ice_lib.h" 606c16d89SJacob Keller 7172db5f9SMaciej Machnikowski #define E810_OUT_PROP_DELAY_NS 1 8172db5f9SMaciej Machnikowski 93a749623SJacob Keller #define UNKNOWN_INCVAL_E822 0x100000000ULL 103a749623SJacob Keller 11325b2064SMaciej Machnikowski static const struct ptp_pin_desc ice_pin_desc_e810t[] = { 12325b2064SMaciej Machnikowski /* name idx func chan */ 13325b2064SMaciej Machnikowski { "GNSS", GNSS, PTP_PF_EXTTS, 0, { 0, } }, 14325b2064SMaciej Machnikowski { "SMA1", SMA1, PTP_PF_NONE, 1, { 0, } }, 15325b2064SMaciej Machnikowski { "U.FL1", UFL1, PTP_PF_NONE, 1, { 0, } }, 16325b2064SMaciej Machnikowski { "SMA2", SMA2, PTP_PF_NONE, 2, { 0, } }, 17325b2064SMaciej Machnikowski { "U.FL2", UFL2, PTP_PF_NONE, 2, { 0, } }, 18325b2064SMaciej Machnikowski }; 19325b2064SMaciej Machnikowski 20325b2064SMaciej Machnikowski /** 21325b2064SMaciej Machnikowski * ice_get_sma_config_e810t 22325b2064SMaciej Machnikowski * @hw: pointer to the hw struct 23325b2064SMaciej Machnikowski * @ptp_pins: pointer to the ptp_pin_desc struture 24325b2064SMaciej Machnikowski * 25325b2064SMaciej Machnikowski * Read the configuration of the SMA control logic and put it into the 26325b2064SMaciej Machnikowski * ptp_pin_desc structure 27325b2064SMaciej Machnikowski */ 28325b2064SMaciej Machnikowski static int 29325b2064SMaciej Machnikowski ice_get_sma_config_e810t(struct ice_hw *hw, struct ptp_pin_desc *ptp_pins) 30325b2064SMaciej Machnikowski { 31325b2064SMaciej Machnikowski u8 data, i; 32325b2064SMaciej Machnikowski int status; 33325b2064SMaciej Machnikowski 34325b2064SMaciej Machnikowski /* Read initial pin state */ 35325b2064SMaciej Machnikowski status = ice_read_sma_ctrl_e810t(hw, &data); 36325b2064SMaciej Machnikowski if (status) 37325b2064SMaciej Machnikowski return status; 38325b2064SMaciej Machnikowski 39325b2064SMaciej Machnikowski /* initialize with defaults */ 40325b2064SMaciej Machnikowski for (i = 0; i < NUM_PTP_PINS_E810T; i++) { 41325b2064SMaciej Machnikowski snprintf(ptp_pins[i].name, sizeof(ptp_pins[i].name), 42325b2064SMaciej Machnikowski "%s", ice_pin_desc_e810t[i].name); 43325b2064SMaciej Machnikowski ptp_pins[i].index = ice_pin_desc_e810t[i].index; 44325b2064SMaciej Machnikowski ptp_pins[i].func = ice_pin_desc_e810t[i].func; 45325b2064SMaciej Machnikowski ptp_pins[i].chan = ice_pin_desc_e810t[i].chan; 46325b2064SMaciej Machnikowski } 47325b2064SMaciej Machnikowski 48325b2064SMaciej Machnikowski /* Parse SMA1/UFL1 */ 49325b2064SMaciej Machnikowski switch (data & ICE_SMA1_MASK_E810T) { 50325b2064SMaciej Machnikowski case ICE_SMA1_MASK_E810T: 51325b2064SMaciej Machnikowski default: 52325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_NONE; 53325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 54325b2064SMaciej Machnikowski break; 55325b2064SMaciej Machnikowski case ICE_SMA1_DIR_EN_E810T: 56325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_PEROUT; 57325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 58325b2064SMaciej Machnikowski break; 59325b2064SMaciej Machnikowski case ICE_SMA1_TX_EN_E810T: 60325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_EXTTS; 61325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 62325b2064SMaciej Machnikowski break; 63325b2064SMaciej Machnikowski case 0: 64325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_EXTTS; 65325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_PEROUT; 66325b2064SMaciej Machnikowski break; 67325b2064SMaciej Machnikowski } 68325b2064SMaciej Machnikowski 69325b2064SMaciej Machnikowski /* Parse SMA2/UFL2 */ 70325b2064SMaciej Machnikowski switch (data & ICE_SMA2_MASK_E810T) { 71325b2064SMaciej Machnikowski case ICE_SMA2_MASK_E810T: 72325b2064SMaciej Machnikowski default: 73325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE; 74325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 75325b2064SMaciej Machnikowski break; 76325b2064SMaciej Machnikowski case (ICE_SMA2_TX_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T): 77325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_EXTTS; 78325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 79325b2064SMaciej Machnikowski break; 80325b2064SMaciej Machnikowski case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T): 81325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_PEROUT; 82325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 83325b2064SMaciej Machnikowski break; 84325b2064SMaciej Machnikowski case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T): 85325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE; 86325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_EXTTS; 87325b2064SMaciej Machnikowski break; 88325b2064SMaciej Machnikowski case ICE_SMA2_DIR_EN_E810T: 89325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_PEROUT; 90325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_EXTTS; 91325b2064SMaciej Machnikowski break; 92325b2064SMaciej Machnikowski } 93325b2064SMaciej Machnikowski 94325b2064SMaciej Machnikowski return 0; 95325b2064SMaciej Machnikowski } 96325b2064SMaciej Machnikowski 97325b2064SMaciej Machnikowski /** 98325b2064SMaciej Machnikowski * ice_ptp_set_sma_config_e810t 99325b2064SMaciej Machnikowski * @hw: pointer to the hw struct 100325b2064SMaciej Machnikowski * @ptp_pins: pointer to the ptp_pin_desc struture 101325b2064SMaciej Machnikowski * 102325b2064SMaciej Machnikowski * Set the configuration of the SMA control logic based on the configuration in 103325b2064SMaciej Machnikowski * num_pins parameter 104325b2064SMaciej Machnikowski */ 105325b2064SMaciej Machnikowski static int 106325b2064SMaciej Machnikowski ice_ptp_set_sma_config_e810t(struct ice_hw *hw, 107325b2064SMaciej Machnikowski const struct ptp_pin_desc *ptp_pins) 108325b2064SMaciej Machnikowski { 109325b2064SMaciej Machnikowski int status; 110325b2064SMaciej Machnikowski u8 data; 111325b2064SMaciej Machnikowski 112325b2064SMaciej Machnikowski /* SMA1 and UFL1 cannot be set to TX at the same time */ 113325b2064SMaciej Machnikowski if (ptp_pins[SMA1].func == PTP_PF_PEROUT && 114325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT) 115325b2064SMaciej Machnikowski return -EINVAL; 116325b2064SMaciej Machnikowski 117325b2064SMaciej Machnikowski /* SMA2 and UFL2 cannot be set to RX at the same time */ 118325b2064SMaciej Machnikowski if (ptp_pins[SMA2].func == PTP_PF_EXTTS && 119325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS) 120325b2064SMaciej Machnikowski return -EINVAL; 121325b2064SMaciej Machnikowski 122325b2064SMaciej Machnikowski /* Read initial pin state value */ 123325b2064SMaciej Machnikowski status = ice_read_sma_ctrl_e810t(hw, &data); 124325b2064SMaciej Machnikowski if (status) 125325b2064SMaciej Machnikowski return status; 126325b2064SMaciej Machnikowski 127325b2064SMaciej Machnikowski /* Set the right sate based on the desired configuration */ 128325b2064SMaciej Machnikowski data &= ~ICE_SMA1_MASK_E810T; 129325b2064SMaciej Machnikowski if (ptp_pins[SMA1].func == PTP_PF_NONE && 130325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) { 131325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 + U.FL1 disabled"); 132325b2064SMaciej Machnikowski data |= ICE_SMA1_MASK_E810T; 133325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_EXTTS && 134325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) { 135325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX"); 136325b2064SMaciej Machnikowski data |= ICE_SMA1_TX_EN_E810T; 137325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_NONE && 138325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT) { 139325b2064SMaciej Machnikowski /* U.FL 1 TX will always enable SMA 1 RX */ 140325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX"); 141325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_EXTTS && 142325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT) { 143325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX"); 144325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_PEROUT && 145325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) { 146325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 TX"); 147325b2064SMaciej Machnikowski data |= ICE_SMA1_DIR_EN_E810T; 148325b2064SMaciej Machnikowski } 149325b2064SMaciej Machnikowski 150325b2064SMaciej Machnikowski data &= ~ICE_SMA2_MASK_E810T; 151325b2064SMaciej Machnikowski if (ptp_pins[SMA2].func == PTP_PF_NONE && 152325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) { 153325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 + U.FL2 disabled"); 154325b2064SMaciej Machnikowski data |= ICE_SMA2_MASK_E810T; 155325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_EXTTS && 156325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) { 157325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 RX"); 158325b2064SMaciej Machnikowski data |= (ICE_SMA2_TX_EN_E810T | 159325b2064SMaciej Machnikowski ICE_SMA2_UFL2_RX_DIS_E810T); 160325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_NONE && 161325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS) { 162325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "UFL2 RX"); 163325b2064SMaciej Machnikowski data |= (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T); 164325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_PEROUT && 165325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) { 166325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 TX"); 167325b2064SMaciej Machnikowski data |= (ICE_SMA2_DIR_EN_E810T | 168325b2064SMaciej Machnikowski ICE_SMA2_UFL2_RX_DIS_E810T); 169325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_PEROUT && 170325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS) { 171325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 TX + U.FL2 RX"); 172325b2064SMaciej Machnikowski data |= ICE_SMA2_DIR_EN_E810T; 173325b2064SMaciej Machnikowski } 174325b2064SMaciej Machnikowski 175325b2064SMaciej Machnikowski return ice_write_sma_ctrl_e810t(hw, data); 176325b2064SMaciej Machnikowski } 177325b2064SMaciej Machnikowski 178325b2064SMaciej Machnikowski /** 179325b2064SMaciej Machnikowski * ice_ptp_set_sma_e810t 180325b2064SMaciej Machnikowski * @info: the driver's PTP info structure 181325b2064SMaciej Machnikowski * @pin: pin index in kernel structure 182325b2064SMaciej Machnikowski * @func: Pin function to be set (PTP_PF_NONE, PTP_PF_EXTTS or PTP_PF_PEROUT) 183325b2064SMaciej Machnikowski * 184325b2064SMaciej Machnikowski * Set the configuration of a single SMA pin 185325b2064SMaciej Machnikowski */ 186325b2064SMaciej Machnikowski static int 187325b2064SMaciej Machnikowski ice_ptp_set_sma_e810t(struct ptp_clock_info *info, unsigned int pin, 188325b2064SMaciej Machnikowski enum ptp_pin_function func) 189325b2064SMaciej Machnikowski { 190325b2064SMaciej Machnikowski struct ptp_pin_desc ptp_pins[NUM_PTP_PINS_E810T]; 191325b2064SMaciej Machnikowski struct ice_pf *pf = ptp_info_to_pf(info); 192325b2064SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 193325b2064SMaciej Machnikowski int err; 194325b2064SMaciej Machnikowski 195325b2064SMaciej Machnikowski if (pin < SMA1 || func > PTP_PF_PEROUT) 196325b2064SMaciej Machnikowski return -EOPNOTSUPP; 197325b2064SMaciej Machnikowski 198325b2064SMaciej Machnikowski err = ice_get_sma_config_e810t(hw, ptp_pins); 199325b2064SMaciej Machnikowski if (err) 200325b2064SMaciej Machnikowski return err; 201325b2064SMaciej Machnikowski 202325b2064SMaciej Machnikowski /* Disable the same function on the other pin sharing the channel */ 203325b2064SMaciej Machnikowski if (pin == SMA1 && ptp_pins[UFL1].func == func) 204325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 205325b2064SMaciej Machnikowski if (pin == UFL1 && ptp_pins[SMA1].func == func) 206325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_NONE; 207325b2064SMaciej Machnikowski 208325b2064SMaciej Machnikowski if (pin == SMA2 && ptp_pins[UFL2].func == func) 209325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 210325b2064SMaciej Machnikowski if (pin == UFL2 && ptp_pins[SMA2].func == func) 211325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE; 212325b2064SMaciej Machnikowski 213325b2064SMaciej Machnikowski /* Set up new pin function in the temp table */ 214325b2064SMaciej Machnikowski ptp_pins[pin].func = func; 215325b2064SMaciej Machnikowski 216325b2064SMaciej Machnikowski return ice_ptp_set_sma_config_e810t(hw, ptp_pins); 217325b2064SMaciej Machnikowski } 218325b2064SMaciej Machnikowski 219325b2064SMaciej Machnikowski /** 220325b2064SMaciej Machnikowski * ice_verify_pin_e810t 221325b2064SMaciej Machnikowski * @info: the driver's PTP info structure 222325b2064SMaciej Machnikowski * @pin: Pin index 223325b2064SMaciej Machnikowski * @func: Assigned function 224325b2064SMaciej Machnikowski * @chan: Assigned channel 225325b2064SMaciej Machnikowski * 226325b2064SMaciej Machnikowski * Verify if pin supports requested pin function. If the Check pins consistency. 227325b2064SMaciej Machnikowski * Reconfigure the SMA logic attached to the given pin to enable its 228325b2064SMaciej Machnikowski * desired functionality 229325b2064SMaciej Machnikowski */ 230325b2064SMaciej Machnikowski static int 231325b2064SMaciej Machnikowski ice_verify_pin_e810t(struct ptp_clock_info *info, unsigned int pin, 232325b2064SMaciej Machnikowski enum ptp_pin_function func, unsigned int chan) 233325b2064SMaciej Machnikowski { 234325b2064SMaciej Machnikowski /* Don't allow channel reassignment */ 235325b2064SMaciej Machnikowski if (chan != ice_pin_desc_e810t[pin].chan) 236325b2064SMaciej Machnikowski return -EOPNOTSUPP; 237325b2064SMaciej Machnikowski 238325b2064SMaciej Machnikowski /* Check if functions are properly assigned */ 239325b2064SMaciej Machnikowski switch (func) { 240325b2064SMaciej Machnikowski case PTP_PF_NONE: 241325b2064SMaciej Machnikowski break; 242325b2064SMaciej Machnikowski case PTP_PF_EXTTS: 243325b2064SMaciej Machnikowski if (pin == UFL1) 244325b2064SMaciej Machnikowski return -EOPNOTSUPP; 245325b2064SMaciej Machnikowski break; 246325b2064SMaciej Machnikowski case PTP_PF_PEROUT: 247325b2064SMaciej Machnikowski if (pin == UFL2 || pin == GNSS) 248325b2064SMaciej Machnikowski return -EOPNOTSUPP; 249325b2064SMaciej Machnikowski break; 250325b2064SMaciej Machnikowski case PTP_PF_PHYSYNC: 251325b2064SMaciej Machnikowski return -EOPNOTSUPP; 252325b2064SMaciej Machnikowski } 253325b2064SMaciej Machnikowski 254325b2064SMaciej Machnikowski return ice_ptp_set_sma_e810t(info, pin, func); 255325b2064SMaciej Machnikowski } 256325b2064SMaciej Machnikowski 25706c16d89SJacob Keller /** 258ea9b847cSJacob Keller * ice_set_tx_tstamp - Enable or disable Tx timestamping 259ea9b847cSJacob Keller * @pf: The PF pointer to search in 260ea9b847cSJacob Keller * @on: bool value for whether timestamps are enabled or disabled 261ea9b847cSJacob Keller */ 262ea9b847cSJacob Keller static void ice_set_tx_tstamp(struct ice_pf *pf, bool on) 263ea9b847cSJacob Keller { 264ea9b847cSJacob Keller struct ice_vsi *vsi; 265ea9b847cSJacob Keller u32 val; 266ea9b847cSJacob Keller u16 i; 267ea9b847cSJacob Keller 268ea9b847cSJacob Keller vsi = ice_get_main_vsi(pf); 269ea9b847cSJacob Keller if (!vsi) 270ea9b847cSJacob Keller return; 271ea9b847cSJacob Keller 272ea9b847cSJacob Keller /* Set the timestamp enable flag for all the Tx rings */ 27384c5fb8cSJacob Keller ice_for_each_txq(vsi, i) { 274ea9b847cSJacob Keller if (!vsi->tx_rings[i]) 275ea9b847cSJacob Keller continue; 276ea9b847cSJacob Keller vsi->tx_rings[i]->ptp_tx = on; 277ea9b847cSJacob Keller } 278ea9b847cSJacob Keller 279ea9b847cSJacob Keller /* Configure the Tx timestamp interrupt */ 280ea9b847cSJacob Keller val = rd32(&pf->hw, PFINT_OICR_ENA); 281ea9b847cSJacob Keller if (on) 282ea9b847cSJacob Keller val |= PFINT_OICR_TSYN_TX_M; 283ea9b847cSJacob Keller else 284ea9b847cSJacob Keller val &= ~PFINT_OICR_TSYN_TX_M; 285ea9b847cSJacob Keller wr32(&pf->hw, PFINT_OICR_ENA, val); 286e59d75ddSJacob Keller 287e59d75ddSJacob Keller pf->ptp.tstamp_config.tx_type = on ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 288ea9b847cSJacob Keller } 289ea9b847cSJacob Keller 290ea9b847cSJacob Keller /** 29177a78115SJacob Keller * ice_set_rx_tstamp - Enable or disable Rx timestamping 29277a78115SJacob Keller * @pf: The PF pointer to search in 29377a78115SJacob Keller * @on: bool value for whether timestamps are enabled or disabled 29477a78115SJacob Keller */ 29577a78115SJacob Keller static void ice_set_rx_tstamp(struct ice_pf *pf, bool on) 29677a78115SJacob Keller { 29777a78115SJacob Keller struct ice_vsi *vsi; 29877a78115SJacob Keller u16 i; 29977a78115SJacob Keller 30077a78115SJacob Keller vsi = ice_get_main_vsi(pf); 30177a78115SJacob Keller if (!vsi) 30277a78115SJacob Keller return; 30377a78115SJacob Keller 30477a78115SJacob Keller /* Set the timestamp flag for all the Rx rings */ 30577a78115SJacob Keller ice_for_each_rxq(vsi, i) { 30677a78115SJacob Keller if (!vsi->rx_rings[i]) 30777a78115SJacob Keller continue; 30877a78115SJacob Keller vsi->rx_rings[i]->ptp_rx = on; 30977a78115SJacob Keller } 310e59d75ddSJacob Keller 311e59d75ddSJacob Keller pf->ptp.tstamp_config.rx_filter = on ? HWTSTAMP_FILTER_ALL : 312e59d75ddSJacob Keller HWTSTAMP_FILTER_NONE; 31377a78115SJacob Keller } 31477a78115SJacob Keller 31577a78115SJacob Keller /** 31677a78115SJacob Keller * ice_ptp_cfg_timestamp - Configure timestamp for init/deinit 31777a78115SJacob Keller * @pf: Board private structure 31877a78115SJacob Keller * @ena: bool value to enable or disable time stamp 31977a78115SJacob Keller * 32077a78115SJacob Keller * This function will configure timestamping during PTP initialization 32177a78115SJacob Keller * and deinitialization 32277a78115SJacob Keller */ 32348096710SKarol Kolacinski void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena) 32477a78115SJacob Keller { 325ea9b847cSJacob Keller ice_set_tx_tstamp(pf, ena); 32677a78115SJacob Keller ice_set_rx_tstamp(pf, ena); 32777a78115SJacob Keller } 32877a78115SJacob Keller 32977a78115SJacob Keller /** 33067569a7fSJacob Keller * ice_get_ptp_clock_index - Get the PTP clock index 33167569a7fSJacob Keller * @pf: the PF pointer 33267569a7fSJacob Keller * 33367569a7fSJacob Keller * Determine the clock index of the PTP clock associated with this device. If 33467569a7fSJacob Keller * this is the PF controlling the clock, just use the local access to the 33567569a7fSJacob Keller * clock device pointer. 33667569a7fSJacob Keller * 33767569a7fSJacob Keller * Otherwise, read from the driver shared parameters to determine the clock 33867569a7fSJacob Keller * index value. 33967569a7fSJacob Keller * 34067569a7fSJacob Keller * Returns: the index of the PTP clock associated with this device, or -1 if 34167569a7fSJacob Keller * there is no associated clock. 34267569a7fSJacob Keller */ 34367569a7fSJacob Keller int ice_get_ptp_clock_index(struct ice_pf *pf) 34467569a7fSJacob Keller { 34567569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 34667569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 34767569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 34867569a7fSJacob Keller u8 tmr_idx; 34967569a7fSJacob Keller u32 value; 35067569a7fSJacob Keller int err; 35167569a7fSJacob Keller 35267569a7fSJacob Keller /* Use the ptp_clock structure if we're the main PF */ 35367569a7fSJacob Keller if (pf->ptp.clock) 35467569a7fSJacob Keller return ptp_clock_index(pf->ptp.clock); 35567569a7fSJacob Keller 35667569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 35767569a7fSJacob Keller if (!tmr_idx) 35867569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 35967569a7fSJacob Keller else 36067569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 36167569a7fSJacob Keller 36267569a7fSJacob Keller err = ice_aq_get_driver_param(hw, param_idx, &value, NULL); 36367569a7fSJacob Keller if (err) { 36467569a7fSJacob Keller dev_err(dev, "Failed to read PTP clock index parameter, err %d aq_err %s\n", 36567569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 36667569a7fSJacob Keller return -1; 36767569a7fSJacob Keller } 36867569a7fSJacob Keller 36967569a7fSJacob Keller /* The PTP clock index is an integer, and will be between 0 and 37067569a7fSJacob Keller * INT_MAX. The highest bit of the driver shared parameter is used to 37167569a7fSJacob Keller * indicate whether or not the currently stored clock index is valid. 37267569a7fSJacob Keller */ 37367569a7fSJacob Keller if (!(value & PTP_SHARED_CLK_IDX_VALID)) 37467569a7fSJacob Keller return -1; 37567569a7fSJacob Keller 37667569a7fSJacob Keller return value & ~PTP_SHARED_CLK_IDX_VALID; 37767569a7fSJacob Keller } 37867569a7fSJacob Keller 37967569a7fSJacob Keller /** 38067569a7fSJacob Keller * ice_set_ptp_clock_index - Set the PTP clock index 38167569a7fSJacob Keller * @pf: the PF pointer 38267569a7fSJacob Keller * 38367569a7fSJacob Keller * Set the PTP clock index for this device into the shared driver parameters, 38467569a7fSJacob Keller * so that other PFs associated with this device can read it. 38567569a7fSJacob Keller * 38667569a7fSJacob Keller * If the PF is unable to store the clock index, it will log an error, but 38767569a7fSJacob Keller * will continue operating PTP. 38867569a7fSJacob Keller */ 38967569a7fSJacob Keller static void ice_set_ptp_clock_index(struct ice_pf *pf) 39067569a7fSJacob Keller { 39167569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 39267569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 39367569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 39467569a7fSJacob Keller u8 tmr_idx; 39567569a7fSJacob Keller u32 value; 39667569a7fSJacob Keller int err; 39767569a7fSJacob Keller 39867569a7fSJacob Keller if (!pf->ptp.clock) 39967569a7fSJacob Keller return; 40067569a7fSJacob Keller 40167569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 40267569a7fSJacob Keller if (!tmr_idx) 40367569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 40467569a7fSJacob Keller else 40567569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 40667569a7fSJacob Keller 40767569a7fSJacob Keller value = (u32)ptp_clock_index(pf->ptp.clock); 40867569a7fSJacob Keller if (value > INT_MAX) { 40967569a7fSJacob Keller dev_err(dev, "PTP Clock index is too large to store\n"); 41067569a7fSJacob Keller return; 41167569a7fSJacob Keller } 41267569a7fSJacob Keller value |= PTP_SHARED_CLK_IDX_VALID; 41367569a7fSJacob Keller 41467569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, value, NULL); 41567569a7fSJacob Keller if (err) { 41667569a7fSJacob Keller dev_err(dev, "Failed to set PTP clock index parameter, err %d aq_err %s\n", 41767569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 41867569a7fSJacob Keller } 41967569a7fSJacob Keller } 42067569a7fSJacob Keller 42167569a7fSJacob Keller /** 42267569a7fSJacob Keller * ice_clear_ptp_clock_index - Clear the PTP clock index 42367569a7fSJacob Keller * @pf: the PF pointer 42467569a7fSJacob Keller * 42567569a7fSJacob Keller * Clear the PTP clock index for this device. Must be called when 42667569a7fSJacob Keller * unregistering the PTP clock, in order to ensure other PFs stop reporting 42767569a7fSJacob Keller * a clock object that no longer exists. 42867569a7fSJacob Keller */ 42967569a7fSJacob Keller static void ice_clear_ptp_clock_index(struct ice_pf *pf) 43067569a7fSJacob Keller { 43167569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 43267569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 43367569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 43467569a7fSJacob Keller u8 tmr_idx; 43567569a7fSJacob Keller int err; 43667569a7fSJacob Keller 43767569a7fSJacob Keller /* Do not clear the index if we don't own the timer */ 43867569a7fSJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned) 43967569a7fSJacob Keller return; 44067569a7fSJacob Keller 44167569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 44267569a7fSJacob Keller if (!tmr_idx) 44367569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 44467569a7fSJacob Keller else 44567569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 44667569a7fSJacob Keller 44767569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, 0, NULL); 44867569a7fSJacob Keller if (err) { 44967569a7fSJacob Keller dev_dbg(dev, "Failed to clear PTP clock index parameter, err %d aq_err %s\n", 45067569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 45167569a7fSJacob Keller } 45267569a7fSJacob Keller } 45367569a7fSJacob Keller 45467569a7fSJacob Keller /** 45506c16d89SJacob Keller * ice_ptp_read_src_clk_reg - Read the source clock register 45606c16d89SJacob Keller * @pf: Board private structure 45706c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 45806c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 45906c16d89SJacob Keller */ 46006c16d89SJacob Keller static u64 46106c16d89SJacob Keller ice_ptp_read_src_clk_reg(struct ice_pf *pf, struct ptp_system_timestamp *sts) 46206c16d89SJacob Keller { 46306c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 46406c16d89SJacob Keller u32 hi, lo, lo2; 46506c16d89SJacob Keller u8 tmr_idx; 46606c16d89SJacob Keller 46706c16d89SJacob Keller tmr_idx = ice_get_ptp_src_clock_index(hw); 46806c16d89SJacob Keller /* Read the system timestamp pre PHC read */ 46906c16d89SJacob Keller ptp_read_system_prets(sts); 47006c16d89SJacob Keller 47106c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 47206c16d89SJacob Keller 47306c16d89SJacob Keller /* Read the system timestamp post PHC read */ 47406c16d89SJacob Keller ptp_read_system_postts(sts); 47506c16d89SJacob Keller 47606c16d89SJacob Keller hi = rd32(hw, GLTSYN_TIME_H(tmr_idx)); 47706c16d89SJacob Keller lo2 = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 47806c16d89SJacob Keller 47906c16d89SJacob Keller if (lo2 < lo) { 48006c16d89SJacob Keller /* if TIME_L rolled over read TIME_L again and update 48106c16d89SJacob Keller * system timestamps 48206c16d89SJacob Keller */ 48306c16d89SJacob Keller ptp_read_system_prets(sts); 48406c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 48506c16d89SJacob Keller ptp_read_system_postts(sts); 48606c16d89SJacob Keller hi = rd32(hw, GLTSYN_TIME_H(tmr_idx)); 48706c16d89SJacob Keller } 48806c16d89SJacob Keller 48906c16d89SJacob Keller return ((u64)hi << 32) | lo; 49006c16d89SJacob Keller } 49106c16d89SJacob Keller 49206c16d89SJacob Keller /** 49377a78115SJacob Keller * ice_ptp_update_cached_phctime - Update the cached PHC time values 49477a78115SJacob Keller * @pf: Board specific private structure 49577a78115SJacob Keller * 49677a78115SJacob Keller * This function updates the system time values which are cached in the PF 49777a78115SJacob Keller * structure and the Rx rings. 49877a78115SJacob Keller * 49977a78115SJacob Keller * This function must be called periodically to ensure that the cached value 50077a78115SJacob Keller * is never more than 2 seconds old. It must also be called whenever the PHC 50177a78115SJacob Keller * time has been changed. 50277a78115SJacob Keller */ 50377a78115SJacob Keller static void ice_ptp_update_cached_phctime(struct ice_pf *pf) 50477a78115SJacob Keller { 50577a78115SJacob Keller u64 systime; 50677a78115SJacob Keller int i; 50777a78115SJacob Keller 50877a78115SJacob Keller /* Read the current PHC time */ 50977a78115SJacob Keller systime = ice_ptp_read_src_clk_reg(pf, NULL); 51077a78115SJacob Keller 51177a78115SJacob Keller /* Update the cached PHC time stored in the PF structure */ 51277a78115SJacob Keller WRITE_ONCE(pf->ptp.cached_phc_time, systime); 51377a78115SJacob Keller 51477a78115SJacob Keller ice_for_each_vsi(pf, i) { 51577a78115SJacob Keller struct ice_vsi *vsi = pf->vsi[i]; 51677a78115SJacob Keller int j; 51777a78115SJacob Keller 51877a78115SJacob Keller if (!vsi) 51977a78115SJacob Keller continue; 52077a78115SJacob Keller 52177a78115SJacob Keller if (vsi->type != ICE_VSI_PF) 52277a78115SJacob Keller continue; 52377a78115SJacob Keller 52477a78115SJacob Keller ice_for_each_rxq(vsi, j) { 52577a78115SJacob Keller if (!vsi->rx_rings[j]) 52677a78115SJacob Keller continue; 52777a78115SJacob Keller WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime); 52877a78115SJacob Keller } 52977a78115SJacob Keller } 53077a78115SJacob Keller } 53177a78115SJacob Keller 53277a78115SJacob Keller /** 53377a78115SJacob Keller * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b 53477a78115SJacob Keller * @cached_phc_time: recently cached copy of PHC time 53577a78115SJacob Keller * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value 53677a78115SJacob Keller * 53777a78115SJacob Keller * Hardware captures timestamps which contain only 32 bits of nominal 53877a78115SJacob Keller * nanoseconds, as opposed to the 64bit timestamps that the stack expects. 53977a78115SJacob Keller * Note that the captured timestamp values may be 40 bits, but the lower 54077a78115SJacob Keller * 8 bits are sub-nanoseconds and generally discarded. 54177a78115SJacob Keller * 54277a78115SJacob Keller * Extend the 32bit nanosecond timestamp using the following algorithm and 54377a78115SJacob Keller * assumptions: 54477a78115SJacob Keller * 54577a78115SJacob Keller * 1) have a recently cached copy of the PHC time 54677a78115SJacob Keller * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1 54777a78115SJacob Keller * seconds) before or after the PHC time was captured. 54877a78115SJacob Keller * 3) calculate the delta between the cached time and the timestamp 54977a78115SJacob Keller * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was 55077a78115SJacob Keller * captured after the PHC time. In this case, the full timestamp is just 55177a78115SJacob Keller * the cached PHC time plus the delta. 55277a78115SJacob Keller * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the 55377a78115SJacob Keller * timestamp was captured *before* the PHC time, i.e. because the PHC 55477a78115SJacob Keller * cache was updated after the timestamp was captured by hardware. In this 55577a78115SJacob Keller * case, the full timestamp is the cached time minus the inverse delta. 55677a78115SJacob Keller * 55777a78115SJacob Keller * This algorithm works even if the PHC time was updated after a Tx timestamp 55877a78115SJacob Keller * was requested, but before the Tx timestamp event was reported from 55977a78115SJacob Keller * hardware. 56077a78115SJacob Keller * 56177a78115SJacob Keller * This calculation primarily relies on keeping the cached PHC time up to 56277a78115SJacob Keller * date. If the timestamp was captured more than 2^31 nanoseconds after the 56377a78115SJacob Keller * PHC time, it is possible that the lower 32bits of PHC time have 56477a78115SJacob Keller * overflowed more than once, and we might generate an incorrect timestamp. 56577a78115SJacob Keller * 56677a78115SJacob Keller * This is prevented by (a) periodically updating the cached PHC time once 56777a78115SJacob Keller * a second, and (b) discarding any Tx timestamp packet if it has waited for 56877a78115SJacob Keller * a timestamp for more than one second. 56977a78115SJacob Keller */ 57077a78115SJacob Keller static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp) 57177a78115SJacob Keller { 57277a78115SJacob Keller u32 delta, phc_time_lo; 57377a78115SJacob Keller u64 ns; 57477a78115SJacob Keller 57577a78115SJacob Keller /* Extract the lower 32 bits of the PHC time */ 57677a78115SJacob Keller phc_time_lo = (u32)cached_phc_time; 57777a78115SJacob Keller 57877a78115SJacob Keller /* Calculate the delta between the lower 32bits of the cached PHC 57977a78115SJacob Keller * time and the in_tstamp value 58077a78115SJacob Keller */ 58177a78115SJacob Keller delta = (in_tstamp - phc_time_lo); 58277a78115SJacob Keller 58377a78115SJacob Keller /* Do not assume that the in_tstamp is always more recent than the 58477a78115SJacob Keller * cached PHC time. If the delta is large, it indicates that the 58577a78115SJacob Keller * in_tstamp was taken in the past, and should be converted 58677a78115SJacob Keller * forward. 58777a78115SJacob Keller */ 58877a78115SJacob Keller if (delta > (U32_MAX / 2)) { 58977a78115SJacob Keller /* reverse the delta calculation here */ 59077a78115SJacob Keller delta = (phc_time_lo - in_tstamp); 59177a78115SJacob Keller ns = cached_phc_time - delta; 59277a78115SJacob Keller } else { 59377a78115SJacob Keller ns = cached_phc_time + delta; 59477a78115SJacob Keller } 59577a78115SJacob Keller 59677a78115SJacob Keller return ns; 59777a78115SJacob Keller } 59877a78115SJacob Keller 59977a78115SJacob Keller /** 600ea9b847cSJacob Keller * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds 601ea9b847cSJacob Keller * @pf: Board private structure 602ea9b847cSJacob Keller * @in_tstamp: Ingress/egress 40b timestamp value 603ea9b847cSJacob Keller * 604ea9b847cSJacob Keller * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal 605ea9b847cSJacob Keller * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit. 606ea9b847cSJacob Keller * 607ea9b847cSJacob Keller * *--------------------------------------------------------------* 608ea9b847cSJacob Keller * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v | 609ea9b847cSJacob Keller * *--------------------------------------------------------------* 610ea9b847cSJacob Keller * 611ea9b847cSJacob Keller * The low bit is an indicator of whether the timestamp is valid. The next 612ea9b847cSJacob Keller * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow, 613ea9b847cSJacob Keller * and the remaining 32 bits are the lower 32 bits of the PHC timer. 614ea9b847cSJacob Keller * 615ea9b847cSJacob Keller * It is assumed that the caller verifies the timestamp is valid prior to 616ea9b847cSJacob Keller * calling this function. 617ea9b847cSJacob Keller * 618ea9b847cSJacob Keller * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC 619ea9b847cSJacob Keller * time stored in the device private PTP structure as the basis for timestamp 620ea9b847cSJacob Keller * extension. 621ea9b847cSJacob Keller * 622ea9b847cSJacob Keller * See ice_ptp_extend_32b_ts for a detailed explanation of the extension 623ea9b847cSJacob Keller * algorithm. 624ea9b847cSJacob Keller */ 625ea9b847cSJacob Keller static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp) 626ea9b847cSJacob Keller { 627ea9b847cSJacob Keller const u64 mask = GENMASK_ULL(31, 0); 628ea9b847cSJacob Keller 629ea9b847cSJacob Keller return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time, 630ea9b847cSJacob Keller (in_tstamp >> 8) & mask); 631ea9b847cSJacob Keller } 632ea9b847cSJacob Keller 633ea9b847cSJacob Keller /** 63406c16d89SJacob Keller * ice_ptp_read_time - Read the time from the device 63506c16d89SJacob Keller * @pf: Board private structure 63606c16d89SJacob Keller * @ts: timespec structure to hold the current time value 63706c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 63806c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 63906c16d89SJacob Keller * 64006c16d89SJacob Keller * This function reads the source clock registers and stores them in a timespec. 64106c16d89SJacob Keller * However, since the registers are 64 bits of nanoseconds, we must convert the 64206c16d89SJacob Keller * result to a timespec before we can return. 64306c16d89SJacob Keller */ 64406c16d89SJacob Keller static void 64506c16d89SJacob Keller ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts, 64606c16d89SJacob Keller struct ptp_system_timestamp *sts) 64706c16d89SJacob Keller { 64806c16d89SJacob Keller u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts); 64906c16d89SJacob Keller 65006c16d89SJacob Keller *ts = ns_to_timespec64(time_ns); 65106c16d89SJacob Keller } 65206c16d89SJacob Keller 65306c16d89SJacob Keller /** 65406c16d89SJacob Keller * ice_ptp_write_init - Set PHC time to provided value 65506c16d89SJacob Keller * @pf: Board private structure 65606c16d89SJacob Keller * @ts: timespec structure that holds the new time value 65706c16d89SJacob Keller * 65806c16d89SJacob Keller * Set the PHC time to the specified time provided in the timespec. 65906c16d89SJacob Keller */ 66006c16d89SJacob Keller static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts) 66106c16d89SJacob Keller { 66206c16d89SJacob Keller u64 ns = timespec64_to_ns(ts); 66306c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 66406c16d89SJacob Keller 66506c16d89SJacob Keller return ice_ptp_init_time(hw, ns); 66606c16d89SJacob Keller } 66706c16d89SJacob Keller 66806c16d89SJacob Keller /** 66906c16d89SJacob Keller * ice_ptp_write_adj - Adjust PHC clock time atomically 67006c16d89SJacob Keller * @pf: Board private structure 67106c16d89SJacob Keller * @adj: Adjustment in nanoseconds 67206c16d89SJacob Keller * 67306c16d89SJacob Keller * Perform an atomic adjustment of the PHC time by the specified number of 67406c16d89SJacob Keller * nanoseconds. 67506c16d89SJacob Keller */ 67606c16d89SJacob Keller static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj) 67706c16d89SJacob Keller { 67806c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 67906c16d89SJacob Keller 68006c16d89SJacob Keller return ice_ptp_adj_clock(hw, adj); 68106c16d89SJacob Keller } 68206c16d89SJacob Keller 68306c16d89SJacob Keller /** 68478267d0cSJacob Keller * ice_base_incval - Get base timer increment value 68578267d0cSJacob Keller * @pf: Board private structure 68678267d0cSJacob Keller * 68778267d0cSJacob Keller * Look up the base timer increment value for this device. The base increment 68878267d0cSJacob Keller * value is used to define the nominal clock tick rate. This increment value 68978267d0cSJacob Keller * is programmed during device initialization. It is also used as the basis 69078267d0cSJacob Keller * for calculating adjustments using scaled_ppm. 69178267d0cSJacob Keller */ 69278267d0cSJacob Keller static u64 ice_base_incval(struct ice_pf *pf) 69378267d0cSJacob Keller { 6943a749623SJacob Keller struct ice_hw *hw = &pf->hw; 6953a749623SJacob Keller u64 incval; 6963a749623SJacob Keller 6973a749623SJacob Keller if (ice_is_e810(hw)) 6983a749623SJacob Keller incval = ICE_PTP_NOMINAL_INCVAL_E810; 6993a749623SJacob Keller else if (ice_e822_time_ref(hw) < NUM_ICE_TIME_REF_FREQ) 7003a749623SJacob Keller incval = ice_e822_nominal_incval(ice_e822_time_ref(hw)); 7013a749623SJacob Keller else 7023a749623SJacob Keller incval = UNKNOWN_INCVAL_E822; 7033a749623SJacob Keller 7043a749623SJacob Keller dev_dbg(ice_pf_to_dev(pf), "PTP: using base increment value of 0x%016llx\n", 7053a749623SJacob Keller incval); 7063a749623SJacob Keller 7073a749623SJacob Keller return incval; 7083a749623SJacob Keller } 7093a749623SJacob Keller 7103a749623SJacob Keller /** 7113a749623SJacob Keller * ice_ptp_reset_ts_memory_quad - Reset timestamp memory for one quad 7123a749623SJacob Keller * @pf: The PF private data structure 7133a749623SJacob Keller * @quad: The quad (0-4) 7143a749623SJacob Keller */ 7153a749623SJacob Keller static void ice_ptp_reset_ts_memory_quad(struct ice_pf *pf, int quad) 7163a749623SJacob Keller { 7173a749623SJacob Keller struct ice_hw *hw = &pf->hw; 7183a749623SJacob Keller 7193a749623SJacob Keller ice_write_quad_reg_e822(hw, quad, Q_REG_TS_CTRL, Q_REG_TS_CTRL_M); 7203a749623SJacob Keller ice_write_quad_reg_e822(hw, quad, Q_REG_TS_CTRL, ~(u32)Q_REG_TS_CTRL_M); 7213a749623SJacob Keller } 7223a749623SJacob Keller 7233a749623SJacob Keller /** 724a69f1cb6SJacob Keller * ice_ptp_check_tx_fifo - Check whether Tx FIFO is in an OK state 725a69f1cb6SJacob Keller * @port: PTP port for which Tx FIFO is checked 726a69f1cb6SJacob Keller */ 727a69f1cb6SJacob Keller static int ice_ptp_check_tx_fifo(struct ice_ptp_port *port) 728a69f1cb6SJacob Keller { 729a69f1cb6SJacob Keller int quad = port->port_num / ICE_PORTS_PER_QUAD; 730a69f1cb6SJacob Keller int offs = port->port_num % ICE_PORTS_PER_QUAD; 731a69f1cb6SJacob Keller struct ice_pf *pf; 732a69f1cb6SJacob Keller struct ice_hw *hw; 733a69f1cb6SJacob Keller u32 val, phy_sts; 734a69f1cb6SJacob Keller int err; 735a69f1cb6SJacob Keller 736a69f1cb6SJacob Keller pf = ptp_port_to_pf(port); 737a69f1cb6SJacob Keller hw = &pf->hw; 738a69f1cb6SJacob Keller 739a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == FIFO_OK) 740a69f1cb6SJacob Keller return 0; 741a69f1cb6SJacob Keller 742a69f1cb6SJacob Keller /* need to read FIFO state */ 743a69f1cb6SJacob Keller if (offs == 0 || offs == 1) 744a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO01_STATUS, 745a69f1cb6SJacob Keller &val); 746a69f1cb6SJacob Keller else 747a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO23_STATUS, 748a69f1cb6SJacob Keller &val); 749a69f1cb6SJacob Keller 750a69f1cb6SJacob Keller if (err) { 751a69f1cb6SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to check port %d Tx FIFO, err %d\n", 752a69f1cb6SJacob Keller port->port_num, err); 753a69f1cb6SJacob Keller return err; 754a69f1cb6SJacob Keller } 755a69f1cb6SJacob Keller 756a69f1cb6SJacob Keller if (offs & 0x1) 757a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO13_M) >> Q_REG_FIFO13_S; 758a69f1cb6SJacob Keller else 759a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO02_M) >> Q_REG_FIFO02_S; 760a69f1cb6SJacob Keller 761a69f1cb6SJacob Keller if (phy_sts & FIFO_EMPTY) { 762a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK; 763a69f1cb6SJacob Keller return 0; 764a69f1cb6SJacob Keller } 765a69f1cb6SJacob Keller 766a69f1cb6SJacob Keller port->tx_fifo_busy_cnt++; 767a69f1cb6SJacob Keller 768a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf), "Try %d, port %d FIFO not empty\n", 769a69f1cb6SJacob Keller port->tx_fifo_busy_cnt, port->port_num); 770a69f1cb6SJacob Keller 771a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == ICE_PTP_FIFO_NUM_CHECKS) { 772a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf), 773a69f1cb6SJacob Keller "Port %d Tx FIFO still not empty; resetting quad %d\n", 774a69f1cb6SJacob Keller port->port_num, quad); 775a69f1cb6SJacob Keller ice_ptp_reset_ts_memory_quad(pf, quad); 776a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK; 777a69f1cb6SJacob Keller return 0; 778a69f1cb6SJacob Keller } 779a69f1cb6SJacob Keller 780a69f1cb6SJacob Keller return -EAGAIN; 781a69f1cb6SJacob Keller } 782a69f1cb6SJacob Keller 783a69f1cb6SJacob Keller /** 784a69f1cb6SJacob Keller * ice_ptp_check_tx_offset_valid - Check if the Tx PHY offset is valid 785a69f1cb6SJacob Keller * @port: the PTP port to check 786a69f1cb6SJacob Keller * 787a69f1cb6SJacob Keller * Checks whether the Tx offset for the PHY associated with this port is 788a69f1cb6SJacob Keller * valid. Returns 0 if the offset is valid, and a non-zero error code if it is 789a69f1cb6SJacob Keller * not. 790a69f1cb6SJacob Keller */ 791a69f1cb6SJacob Keller static int ice_ptp_check_tx_offset_valid(struct ice_ptp_port *port) 792a69f1cb6SJacob Keller { 793a69f1cb6SJacob Keller struct ice_pf *pf = ptp_port_to_pf(port); 794a69f1cb6SJacob Keller struct device *dev = ice_pf_to_dev(pf); 795a69f1cb6SJacob Keller struct ice_hw *hw = &pf->hw; 796a69f1cb6SJacob Keller u32 val; 797a69f1cb6SJacob Keller int err; 798a69f1cb6SJacob Keller 799a69f1cb6SJacob Keller err = ice_ptp_check_tx_fifo(port); 800a69f1cb6SJacob Keller if (err) 801a69f1cb6SJacob Keller return err; 802a69f1cb6SJacob Keller 803a69f1cb6SJacob Keller err = ice_read_phy_reg_e822(hw, port->port_num, P_REG_TX_OV_STATUS, 804a69f1cb6SJacob Keller &val); 805a69f1cb6SJacob Keller if (err) { 806a69f1cb6SJacob Keller dev_err(dev, "Failed to read TX_OV_STATUS for port %d, err %d\n", 807a69f1cb6SJacob Keller port->port_num, err); 808a69f1cb6SJacob Keller return -EAGAIN; 809a69f1cb6SJacob Keller } 810a69f1cb6SJacob Keller 811a69f1cb6SJacob Keller if (!(val & P_REG_TX_OV_STATUS_OV_M)) 812a69f1cb6SJacob Keller return -EAGAIN; 813a69f1cb6SJacob Keller 814a69f1cb6SJacob Keller return 0; 815a69f1cb6SJacob Keller } 816a69f1cb6SJacob Keller 817a69f1cb6SJacob Keller /** 818a69f1cb6SJacob Keller * ice_ptp_check_rx_offset_valid - Check if the Rx PHY offset is valid 819a69f1cb6SJacob Keller * @port: the PTP port to check 820a69f1cb6SJacob Keller * 821a69f1cb6SJacob Keller * Checks whether the Rx offset for the PHY associated with this port is 822a69f1cb6SJacob Keller * valid. Returns 0 if the offset is valid, and a non-zero error code if it is 823a69f1cb6SJacob Keller * not. 824a69f1cb6SJacob Keller */ 825a69f1cb6SJacob Keller static int ice_ptp_check_rx_offset_valid(struct ice_ptp_port *port) 826a69f1cb6SJacob Keller { 827a69f1cb6SJacob Keller struct ice_pf *pf = ptp_port_to_pf(port); 828a69f1cb6SJacob Keller struct device *dev = ice_pf_to_dev(pf); 829a69f1cb6SJacob Keller struct ice_hw *hw = &pf->hw; 830a69f1cb6SJacob Keller int err; 831a69f1cb6SJacob Keller u32 val; 832a69f1cb6SJacob Keller 833a69f1cb6SJacob Keller err = ice_read_phy_reg_e822(hw, port->port_num, P_REG_RX_OV_STATUS, 834a69f1cb6SJacob Keller &val); 835a69f1cb6SJacob Keller if (err) { 836a69f1cb6SJacob Keller dev_err(dev, "Failed to read RX_OV_STATUS for port %d, err %d\n", 837a69f1cb6SJacob Keller port->port_num, err); 838a69f1cb6SJacob Keller return err; 839a69f1cb6SJacob Keller } 840a69f1cb6SJacob Keller 841a69f1cb6SJacob Keller if (!(val & P_REG_RX_OV_STATUS_OV_M)) 842a69f1cb6SJacob Keller return -EAGAIN; 843a69f1cb6SJacob Keller 844a69f1cb6SJacob Keller return 0; 845a69f1cb6SJacob Keller } 846a69f1cb6SJacob Keller 847a69f1cb6SJacob Keller /** 848a69f1cb6SJacob Keller * ice_ptp_check_offset_valid - Check port offset valid bit 849a69f1cb6SJacob Keller * @port: Port for which offset valid bit is checked 850a69f1cb6SJacob Keller * 851a69f1cb6SJacob Keller * Returns 0 if both Tx and Rx offset are valid, and -EAGAIN if one of the 852a69f1cb6SJacob Keller * offset is not ready. 853a69f1cb6SJacob Keller */ 854a69f1cb6SJacob Keller static int ice_ptp_check_offset_valid(struct ice_ptp_port *port) 855a69f1cb6SJacob Keller { 856a69f1cb6SJacob Keller int tx_err, rx_err; 857a69f1cb6SJacob Keller 858a69f1cb6SJacob Keller /* always check both Tx and Rx offset validity */ 859a69f1cb6SJacob Keller tx_err = ice_ptp_check_tx_offset_valid(port); 860a69f1cb6SJacob Keller rx_err = ice_ptp_check_rx_offset_valid(port); 861a69f1cb6SJacob Keller 862a69f1cb6SJacob Keller if (tx_err || rx_err) 863a69f1cb6SJacob Keller return -EAGAIN; 864a69f1cb6SJacob Keller 865a69f1cb6SJacob Keller return 0; 866a69f1cb6SJacob Keller } 867a69f1cb6SJacob Keller 868a69f1cb6SJacob Keller /** 869a69f1cb6SJacob Keller * ice_ptp_wait_for_offset_valid - Check for valid Tx and Rx offsets 870a69f1cb6SJacob Keller * @work: Pointer to the kthread_work structure for this task 871a69f1cb6SJacob Keller * 872a69f1cb6SJacob Keller * Check whether both the Tx and Rx offsets are valid for enabling the vernier 873a69f1cb6SJacob Keller * calibration. 874a69f1cb6SJacob Keller * 875a69f1cb6SJacob Keller * Once we have valid offsets from hardware, update the total Tx and Rx 876a69f1cb6SJacob Keller * offsets, and exit bypass mode. This enables more precise timestamps using 877a69f1cb6SJacob Keller * the extra data measured during the vernier calibration process. 878a69f1cb6SJacob Keller */ 879a69f1cb6SJacob Keller static void ice_ptp_wait_for_offset_valid(struct kthread_work *work) 880a69f1cb6SJacob Keller { 881a69f1cb6SJacob Keller struct ice_ptp_port *port; 882a69f1cb6SJacob Keller int err; 883a69f1cb6SJacob Keller struct device *dev; 884a69f1cb6SJacob Keller struct ice_pf *pf; 885a69f1cb6SJacob Keller struct ice_hw *hw; 886a69f1cb6SJacob Keller 887a69f1cb6SJacob Keller port = container_of(work, struct ice_ptp_port, ov_work.work); 888a69f1cb6SJacob Keller pf = ptp_port_to_pf(port); 889a69f1cb6SJacob Keller hw = &pf->hw; 890a69f1cb6SJacob Keller dev = ice_pf_to_dev(pf); 891a69f1cb6SJacob Keller 892a69f1cb6SJacob Keller if (ice_ptp_check_offset_valid(port)) { 893a69f1cb6SJacob Keller /* Offsets not ready yet, try again later */ 894a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, 895a69f1cb6SJacob Keller &port->ov_work, 896a69f1cb6SJacob Keller msecs_to_jiffies(100)); 897a69f1cb6SJacob Keller return; 898a69f1cb6SJacob Keller } 899a69f1cb6SJacob Keller 900a69f1cb6SJacob Keller /* Offsets are valid, so it is safe to exit bypass mode */ 901a69f1cb6SJacob Keller err = ice_phy_exit_bypass_e822(hw, port->port_num); 902a69f1cb6SJacob Keller if (err) { 903a69f1cb6SJacob Keller dev_warn(dev, "Failed to exit bypass mode for PHY port %u, err %d\n", 904a69f1cb6SJacob Keller port->port_num, err); 905a69f1cb6SJacob Keller return; 906a69f1cb6SJacob Keller } 907a69f1cb6SJacob Keller } 908a69f1cb6SJacob Keller 909a69f1cb6SJacob Keller /** 9103a749623SJacob Keller * ice_ptp_port_phy_stop - Stop timestamping for a PHY port 9113a749623SJacob Keller * @ptp_port: PTP port to stop 9123a749623SJacob Keller */ 9133a749623SJacob Keller static int 9143a749623SJacob Keller ice_ptp_port_phy_stop(struct ice_ptp_port *ptp_port) 9153a749623SJacob Keller { 9163a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port); 9173a749623SJacob Keller u8 port = ptp_port->port_num; 9183a749623SJacob Keller struct ice_hw *hw = &pf->hw; 9193a749623SJacob Keller int err; 9203a749623SJacob Keller 9213a749623SJacob Keller if (ice_is_e810(hw)) 9223a749623SJacob Keller return 0; 9233a749623SJacob Keller 9243a749623SJacob Keller mutex_lock(&ptp_port->ps_lock); 9253a749623SJacob Keller 926a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work); 927a69f1cb6SJacob Keller 9283a749623SJacob Keller err = ice_stop_phy_timer_e822(hw, port, true); 9293a749623SJacob Keller if (err) 9303a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d down, err %d\n", 9313a749623SJacob Keller port, err); 9323a749623SJacob Keller 9333a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock); 9343a749623SJacob Keller 9353a749623SJacob Keller return err; 9363a749623SJacob Keller } 9373a749623SJacob Keller 9383a749623SJacob Keller /** 9393a749623SJacob Keller * ice_ptp_port_phy_restart - (Re)start and calibrate PHY timestamping 9403a749623SJacob Keller * @ptp_port: PTP port for which the PHY start is set 9413a749623SJacob Keller * 9423a749623SJacob Keller * Start the PHY timestamping block, and initiate Vernier timestamping 9433a749623SJacob Keller * calibration. If timestamping cannot be calibrated (such as if link is down) 9443a749623SJacob Keller * then disable the timestamping block instead. 9453a749623SJacob Keller */ 9463a749623SJacob Keller static int 9473a749623SJacob Keller ice_ptp_port_phy_restart(struct ice_ptp_port *ptp_port) 9483a749623SJacob Keller { 9493a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port); 9503a749623SJacob Keller u8 port = ptp_port->port_num; 9513a749623SJacob Keller struct ice_hw *hw = &pf->hw; 9523a749623SJacob Keller int err; 9533a749623SJacob Keller 9543a749623SJacob Keller if (ice_is_e810(hw)) 9553a749623SJacob Keller return 0; 9563a749623SJacob Keller 9573a749623SJacob Keller if (!ptp_port->link_up) 9583a749623SJacob Keller return ice_ptp_port_phy_stop(ptp_port); 9593a749623SJacob Keller 9603a749623SJacob Keller mutex_lock(&ptp_port->ps_lock); 9613a749623SJacob Keller 962a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work); 963a69f1cb6SJacob Keller 9643a749623SJacob Keller /* temporarily disable Tx timestamps while calibrating PHY offset */ 9653a749623SJacob Keller ptp_port->tx.calibrating = true; 966a69f1cb6SJacob Keller ptp_port->tx_fifo_busy_cnt = 0; 9673a749623SJacob Keller 9683a749623SJacob Keller /* Start the PHY timer in bypass mode */ 9693a749623SJacob Keller err = ice_start_phy_timer_e822(hw, port, true); 9703a749623SJacob Keller if (err) 9713a749623SJacob Keller goto out_unlock; 9723a749623SJacob Keller 9733a749623SJacob Keller /* Enable Tx timestamps right away */ 9743a749623SJacob Keller ptp_port->tx.calibrating = false; 9753a749623SJacob Keller 976a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &ptp_port->ov_work, 0); 977a69f1cb6SJacob Keller 9783a749623SJacob Keller out_unlock: 9793a749623SJacob Keller if (err) 9803a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d up, err %d\n", 9813a749623SJacob Keller port, err); 9823a749623SJacob Keller 9833a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock); 9843a749623SJacob Keller 9853a749623SJacob Keller return err; 9863a749623SJacob Keller } 9873a749623SJacob Keller 9883a749623SJacob Keller /** 9893a749623SJacob Keller * ice_ptp_link_change - Set or clear port registers for timestamping 9903a749623SJacob Keller * @pf: Board private structure 9913a749623SJacob Keller * @port: Port for which the PHY start is set 9923a749623SJacob Keller * @linkup: Link is up or down 9933a749623SJacob Keller */ 9943a749623SJacob Keller int ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup) 9953a749623SJacob Keller { 9963a749623SJacob Keller struct ice_ptp_port *ptp_port; 9973a749623SJacob Keller 9983a749623SJacob Keller if (!test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags)) 9993a749623SJacob Keller return 0; 10003a749623SJacob Keller 10013a749623SJacob Keller if (port >= ICE_NUM_EXTERNAL_PORTS) 10023a749623SJacob Keller return -EINVAL; 10033a749623SJacob Keller 10043a749623SJacob Keller ptp_port = &pf->ptp.port; 10053a749623SJacob Keller if (ptp_port->port_num != port) 10063a749623SJacob Keller return -EINVAL; 10073a749623SJacob Keller 10083a749623SJacob Keller /* Update cached link err for this port immediately */ 10093a749623SJacob Keller ptp_port->link_up = linkup; 10103a749623SJacob Keller 10113a749623SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 10123a749623SJacob Keller /* PTP is not setup */ 10133a749623SJacob Keller return -EAGAIN; 10143a749623SJacob Keller 10153a749623SJacob Keller return ice_ptp_port_phy_restart(ptp_port); 10163a749623SJacob Keller } 10173a749623SJacob Keller 10183a749623SJacob Keller /** 10193a749623SJacob Keller * ice_ptp_reset_ts_memory - Reset timestamp memory for all quads 10203a749623SJacob Keller * @pf: The PF private data structure 10213a749623SJacob Keller */ 10223a749623SJacob Keller static void ice_ptp_reset_ts_memory(struct ice_pf *pf) 10233a749623SJacob Keller { 10243a749623SJacob Keller int quad; 10253a749623SJacob Keller 10263a749623SJacob Keller quad = pf->hw.port_info->lport / ICE_PORTS_PER_QUAD; 10273a749623SJacob Keller ice_ptp_reset_ts_memory_quad(pf, quad); 10283a749623SJacob Keller } 10293a749623SJacob Keller 10303a749623SJacob Keller /** 10313a749623SJacob Keller * ice_ptp_tx_ena_intr - Enable or disable the Tx timestamp interrupt 10323a749623SJacob Keller * @pf: PF private structure 10333a749623SJacob Keller * @ena: bool value to enable or disable interrupt 10343a749623SJacob Keller * @threshold: Minimum number of packets at which intr is triggered 10353a749623SJacob Keller * 10363a749623SJacob Keller * Utility function to enable or disable Tx timestamp interrupt and threshold 10373a749623SJacob Keller */ 10383a749623SJacob Keller static int ice_ptp_tx_ena_intr(struct ice_pf *pf, bool ena, u32 threshold) 10393a749623SJacob Keller { 10403a749623SJacob Keller struct ice_hw *hw = &pf->hw; 10413a749623SJacob Keller int err = 0; 10423a749623SJacob Keller int quad; 10433a749623SJacob Keller u32 val; 10443a749623SJacob Keller 10453a749623SJacob Keller ice_ptp_reset_ts_memory(pf); 10463a749623SJacob Keller 10473a749623SJacob Keller for (quad = 0; quad < ICE_MAX_QUAD; quad++) { 10483a749623SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, 10493a749623SJacob Keller &val); 10503a749623SJacob Keller if (err) 10513a749623SJacob Keller break; 10523a749623SJacob Keller 10533a749623SJacob Keller if (ena) { 10543a749623SJacob Keller val |= Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M; 10553a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_THR_M; 10563a749623SJacob Keller val |= ((threshold << Q_REG_TX_MEM_GBL_CFG_INTR_THR_S) & 10573a749623SJacob Keller Q_REG_TX_MEM_GBL_CFG_INTR_THR_M); 10583a749623SJacob Keller } else { 10593a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M; 10603a749623SJacob Keller } 10613a749623SJacob Keller 10623a749623SJacob Keller err = ice_write_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, 10633a749623SJacob Keller val); 10643a749623SJacob Keller if (err) 10653a749623SJacob Keller break; 10663a749623SJacob Keller } 10673a749623SJacob Keller 10683a749623SJacob Keller if (err) 10693a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed in intr ena, err %d\n", 10703a749623SJacob Keller err); 10713a749623SJacob Keller return err; 10723a749623SJacob Keller } 10733a749623SJacob Keller 10743a749623SJacob Keller /** 10753a749623SJacob Keller * ice_ptp_reset_phy_timestamping - Reset PHY timestamping block 10763a749623SJacob Keller * @pf: Board private structure 10773a749623SJacob Keller */ 10783a749623SJacob Keller static void ice_ptp_reset_phy_timestamping(struct ice_pf *pf) 10793a749623SJacob Keller { 10803a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port); 108178267d0cSJacob Keller } 108278267d0cSJacob Keller 108378267d0cSJacob Keller /** 108406c16d89SJacob Keller * ice_ptp_adjfine - Adjust clock increment rate 108506c16d89SJacob Keller * @info: the driver's PTP info structure 108606c16d89SJacob Keller * @scaled_ppm: Parts per million with 16-bit fractional field 108706c16d89SJacob Keller * 108806c16d89SJacob Keller * Adjust the frequency of the clock by the indicated scaled ppm from the 108906c16d89SJacob Keller * base frequency. 109006c16d89SJacob Keller */ 109106c16d89SJacob Keller static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm) 109206c16d89SJacob Keller { 109306c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 109406c16d89SJacob Keller u64 freq, divisor = 1000000ULL; 109506c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 109606c16d89SJacob Keller s64 incval, diff; 109706c16d89SJacob Keller int neg_adj = 0; 109806c16d89SJacob Keller int err; 109906c16d89SJacob Keller 110078267d0cSJacob Keller incval = ice_base_incval(pf); 110106c16d89SJacob Keller 110206c16d89SJacob Keller if (scaled_ppm < 0) { 110306c16d89SJacob Keller neg_adj = 1; 110406c16d89SJacob Keller scaled_ppm = -scaled_ppm; 110506c16d89SJacob Keller } 110606c16d89SJacob Keller 11070013881cSKarol Kolacinski while ((u64)scaled_ppm > div64_u64(U64_MAX, incval)) { 110806c16d89SJacob Keller /* handle overflow by scaling down the scaled_ppm and 110906c16d89SJacob Keller * the divisor, losing some precision 111006c16d89SJacob Keller */ 111106c16d89SJacob Keller scaled_ppm >>= 2; 111206c16d89SJacob Keller divisor >>= 2; 111306c16d89SJacob Keller } 111406c16d89SJacob Keller 111506c16d89SJacob Keller freq = (incval * (u64)scaled_ppm) >> 16; 111606c16d89SJacob Keller diff = div_u64(freq, divisor); 111706c16d89SJacob Keller 111806c16d89SJacob Keller if (neg_adj) 111906c16d89SJacob Keller incval -= diff; 112006c16d89SJacob Keller else 112106c16d89SJacob Keller incval += diff; 112206c16d89SJacob Keller 112306c16d89SJacob Keller err = ice_ptp_write_incval_locked(hw, incval); 112406c16d89SJacob Keller if (err) { 112506c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n", 112606c16d89SJacob Keller err); 112706c16d89SJacob Keller return -EIO; 112806c16d89SJacob Keller } 112906c16d89SJacob Keller 113006c16d89SJacob Keller return 0; 113106c16d89SJacob Keller } 113206c16d89SJacob Keller 113306c16d89SJacob Keller /** 1134172db5f9SMaciej Machnikowski * ice_ptp_extts_work - Workqueue task function 1135172db5f9SMaciej Machnikowski * @work: external timestamp work structure 1136172db5f9SMaciej Machnikowski * 1137172db5f9SMaciej Machnikowski * Service for PTP external clock event 1138172db5f9SMaciej Machnikowski */ 1139172db5f9SMaciej Machnikowski static void ice_ptp_extts_work(struct kthread_work *work) 1140172db5f9SMaciej Machnikowski { 1141172db5f9SMaciej Machnikowski struct ice_ptp *ptp = container_of(work, struct ice_ptp, extts_work); 1142172db5f9SMaciej Machnikowski struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 1143172db5f9SMaciej Machnikowski struct ptp_clock_event event; 1144172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1145172db5f9SMaciej Machnikowski u8 chan, tmr_idx; 1146172db5f9SMaciej Machnikowski u32 hi, lo; 1147172db5f9SMaciej Machnikowski 1148172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1149172db5f9SMaciej Machnikowski /* Event time is captured by one of the two matched registers 1150172db5f9SMaciej Machnikowski * GLTSYN_EVNT_L: 32 LSB of sampled time event 1151172db5f9SMaciej Machnikowski * GLTSYN_EVNT_H: 32 MSB of sampled time event 1152172db5f9SMaciej Machnikowski * Event is defined in GLTSYN_EVNT_0 register 1153172db5f9SMaciej Machnikowski */ 1154172db5f9SMaciej Machnikowski for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) { 1155172db5f9SMaciej Machnikowski /* Check if channel is enabled */ 1156172db5f9SMaciej Machnikowski if (pf->ptp.ext_ts_irq & (1 << chan)) { 1157172db5f9SMaciej Machnikowski lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx)); 1158172db5f9SMaciej Machnikowski hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx)); 1159172db5f9SMaciej Machnikowski event.timestamp = (((u64)hi) << 32) | lo; 1160172db5f9SMaciej Machnikowski event.type = PTP_CLOCK_EXTTS; 1161172db5f9SMaciej Machnikowski event.index = chan; 1162172db5f9SMaciej Machnikowski 1163172db5f9SMaciej Machnikowski /* Fire event */ 1164172db5f9SMaciej Machnikowski ptp_clock_event(pf->ptp.clock, &event); 1165172db5f9SMaciej Machnikowski pf->ptp.ext_ts_irq &= ~(1 << chan); 1166172db5f9SMaciej Machnikowski } 1167172db5f9SMaciej Machnikowski } 1168172db5f9SMaciej Machnikowski } 1169172db5f9SMaciej Machnikowski 1170172db5f9SMaciej Machnikowski /** 1171172db5f9SMaciej Machnikowski * ice_ptp_cfg_extts - Configure EXTTS pin and channel 1172172db5f9SMaciej Machnikowski * @pf: Board private structure 1173172db5f9SMaciej Machnikowski * @ena: true to enable; false to disable 1174172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 1175172db5f9SMaciej Machnikowski * @gpio_pin: GPIO pin 1176172db5f9SMaciej Machnikowski * @extts_flags: request flags from the ptp_extts_request.flags 1177172db5f9SMaciej Machnikowski */ 1178172db5f9SMaciej Machnikowski static int 1179172db5f9SMaciej Machnikowski ice_ptp_cfg_extts(struct ice_pf *pf, bool ena, unsigned int chan, u32 gpio_pin, 1180172db5f9SMaciej Machnikowski unsigned int extts_flags) 1181172db5f9SMaciej Machnikowski { 1182172db5f9SMaciej Machnikowski u32 func, aux_reg, gpio_reg, irq_reg; 1183172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1184172db5f9SMaciej Machnikowski u8 tmr_idx; 1185172db5f9SMaciej Machnikowski 1186172db5f9SMaciej Machnikowski if (chan > (unsigned int)pf->ptp.info.n_ext_ts) 1187172db5f9SMaciej Machnikowski return -EINVAL; 1188172db5f9SMaciej Machnikowski 1189172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1190172db5f9SMaciej Machnikowski 1191172db5f9SMaciej Machnikowski irq_reg = rd32(hw, PFINT_OICR_ENA); 1192172db5f9SMaciej Machnikowski 1193172db5f9SMaciej Machnikowski if (ena) { 1194172db5f9SMaciej Machnikowski /* Enable the interrupt */ 1195172db5f9SMaciej Machnikowski irq_reg |= PFINT_OICR_TSYN_EVNT_M; 1196172db5f9SMaciej Machnikowski aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M; 1197172db5f9SMaciej Machnikowski 1198172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE BIT(0) 1199172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE BIT(1) 1200172db5f9SMaciej Machnikowski 1201172db5f9SMaciej Machnikowski /* set event level to requested edge */ 1202172db5f9SMaciej Machnikowski if (extts_flags & PTP_FALLING_EDGE) 1203172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE; 1204172db5f9SMaciej Machnikowski if (extts_flags & PTP_RISING_EDGE) 1205172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE; 1206172db5f9SMaciej Machnikowski 1207172db5f9SMaciej Machnikowski /* Write GPIO CTL reg. 1208172db5f9SMaciej Machnikowski * 0x1 is input sampled by EVENT register(channel) 1209172db5f9SMaciej Machnikowski * + num_in_channels * tmr_idx 1210172db5f9SMaciej Machnikowski */ 1211172db5f9SMaciej Machnikowski func = 1 + chan + (tmr_idx * 3); 1212172db5f9SMaciej Machnikowski gpio_reg = ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & 1213172db5f9SMaciej Machnikowski GLGEN_GPIO_CTL_PIN_FUNC_M); 1214172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan |= (1 << chan); 1215172db5f9SMaciej Machnikowski } else { 1216172db5f9SMaciej Machnikowski /* clear the values we set to reset defaults */ 1217172db5f9SMaciej Machnikowski aux_reg = 0; 1218172db5f9SMaciej Machnikowski gpio_reg = 0; 1219172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan &= ~(1 << chan); 1220172db5f9SMaciej Machnikowski if (!pf->ptp.ext_ts_chan) 1221172db5f9SMaciej Machnikowski irq_reg &= ~PFINT_OICR_TSYN_EVNT_M; 1222172db5f9SMaciej Machnikowski } 1223172db5f9SMaciej Machnikowski 1224172db5f9SMaciej Machnikowski wr32(hw, PFINT_OICR_ENA, irq_reg); 1225172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg); 1226172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg); 1227172db5f9SMaciej Machnikowski 1228172db5f9SMaciej Machnikowski return 0; 1229172db5f9SMaciej Machnikowski } 1230172db5f9SMaciej Machnikowski 1231172db5f9SMaciej Machnikowski /** 1232172db5f9SMaciej Machnikowski * ice_ptp_cfg_clkout - Configure clock to generate periodic wave 1233172db5f9SMaciej Machnikowski * @pf: Board private structure 1234172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 1235172db5f9SMaciej Machnikowski * @config: desired periodic clk configuration. NULL will disable channel 1236172db5f9SMaciej Machnikowski * @store: If set to true the values will be stored 1237172db5f9SMaciej Machnikowski * 1238172db5f9SMaciej Machnikowski * Configure the internal clock generator modules to generate the clock wave of 1239172db5f9SMaciej Machnikowski * specified period. 1240172db5f9SMaciej Machnikowski */ 1241172db5f9SMaciej Machnikowski static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan, 1242172db5f9SMaciej Machnikowski struct ice_perout_channel *config, bool store) 1243172db5f9SMaciej Machnikowski { 1244172db5f9SMaciej Machnikowski u64 current_time, period, start_time, phase; 1245172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1246172db5f9SMaciej Machnikowski u32 func, val, gpio_pin; 1247172db5f9SMaciej Machnikowski u8 tmr_idx; 1248172db5f9SMaciej Machnikowski 1249172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1250172db5f9SMaciej Machnikowski 1251172db5f9SMaciej Machnikowski /* 0. Reset mode & out_en in AUX_OUT */ 1252172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0); 1253172db5f9SMaciej Machnikowski 1254172db5f9SMaciej Machnikowski /* If we're disabling the output, clear out CLKO and TGT and keep 1255172db5f9SMaciej Machnikowski * output level low 1256172db5f9SMaciej Machnikowski */ 1257172db5f9SMaciej Machnikowski if (!config || !config->ena) { 1258172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), 0); 1259172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), 0); 1260172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), 0); 1261172db5f9SMaciej Machnikowski 1262172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M; 1263172db5f9SMaciej Machnikowski gpio_pin = pf->ptp.perout_channels[chan].gpio_pin; 1264172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 1265172db5f9SMaciej Machnikowski 1266172db5f9SMaciej Machnikowski /* Store the value if requested */ 1267172db5f9SMaciej Machnikowski if (store) 1268172db5f9SMaciej Machnikowski memset(&pf->ptp.perout_channels[chan], 0, 1269172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 1270172db5f9SMaciej Machnikowski 1271172db5f9SMaciej Machnikowski return 0; 1272172db5f9SMaciej Machnikowski } 1273172db5f9SMaciej Machnikowski period = config->period; 1274172db5f9SMaciej Machnikowski start_time = config->start_time; 1275172db5f9SMaciej Machnikowski div64_u64_rem(start_time, period, &phase); 1276172db5f9SMaciej Machnikowski gpio_pin = config->gpio_pin; 1277172db5f9SMaciej Machnikowski 1278172db5f9SMaciej Machnikowski /* 1. Write clkout with half of required period value */ 1279172db5f9SMaciej Machnikowski if (period & 0x1) { 1280172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n"); 1281172db5f9SMaciej Machnikowski goto err; 1282172db5f9SMaciej Machnikowski } 1283172db5f9SMaciej Machnikowski 1284172db5f9SMaciej Machnikowski period >>= 1; 1285172db5f9SMaciej Machnikowski 1286172db5f9SMaciej Machnikowski /* For proper operation, the GLTSYN_CLKO must be larger than clock tick 1287172db5f9SMaciej Machnikowski */ 1288172db5f9SMaciej Machnikowski #define MIN_PULSE 3 1289172db5f9SMaciej Machnikowski if (period <= MIN_PULSE || period > U32_MAX) { 1290172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be > %d && < 2^33", 1291172db5f9SMaciej Machnikowski MIN_PULSE * 2); 1292172db5f9SMaciej Machnikowski goto err; 1293172db5f9SMaciej Machnikowski } 1294172db5f9SMaciej Machnikowski 1295172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period)); 1296172db5f9SMaciej Machnikowski 1297172db5f9SMaciej Machnikowski /* Allow time for programming before start_time is hit */ 1298172db5f9SMaciej Machnikowski current_time = ice_ptp_read_src_clk_reg(pf, NULL); 1299172db5f9SMaciej Machnikowski 1300172db5f9SMaciej Machnikowski /* if start time is in the past start the timer at the nearest second 1301172db5f9SMaciej Machnikowski * maintaining phase 1302172db5f9SMaciej Machnikowski */ 1303172db5f9SMaciej Machnikowski if (start_time < current_time) 13045f773519SMaciej Machnikowski start_time = div64_u64(current_time + NSEC_PER_SEC - 1, 1305172db5f9SMaciej Machnikowski NSEC_PER_SEC) * NSEC_PER_SEC + phase; 1306172db5f9SMaciej Machnikowski 13073a749623SJacob Keller if (ice_is_e810(hw)) 1308172db5f9SMaciej Machnikowski start_time -= E810_OUT_PROP_DELAY_NS; 13093a749623SJacob Keller else 13103a749623SJacob Keller start_time -= ice_e822_pps_delay(ice_e822_time_ref(hw)); 1311172db5f9SMaciej Machnikowski 1312172db5f9SMaciej Machnikowski /* 2. Write TARGET time */ 1313172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start_time)); 1314172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start_time)); 1315172db5f9SMaciej Machnikowski 1316172db5f9SMaciej Machnikowski /* 3. Write AUX_OUT register */ 1317172db5f9SMaciej Machnikowski val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M; 1318172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val); 1319172db5f9SMaciej Machnikowski 1320172db5f9SMaciej Machnikowski /* 4. write GPIO CTL reg */ 1321172db5f9SMaciej Machnikowski func = 8 + chan + (tmr_idx * 4); 1322172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M | 1323172db5f9SMaciej Machnikowski ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & GLGEN_GPIO_CTL_PIN_FUNC_M); 1324172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 1325172db5f9SMaciej Machnikowski 1326172db5f9SMaciej Machnikowski /* Store the value if requested */ 1327172db5f9SMaciej Machnikowski if (store) { 1328172db5f9SMaciej Machnikowski memcpy(&pf->ptp.perout_channels[chan], config, 1329172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 1330172db5f9SMaciej Machnikowski pf->ptp.perout_channels[chan].start_time = phase; 1331172db5f9SMaciej Machnikowski } 1332172db5f9SMaciej Machnikowski 1333172db5f9SMaciej Machnikowski return 0; 1334172db5f9SMaciej Machnikowski err: 1335172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "PTP failed to cfg per_clk\n"); 1336172db5f9SMaciej Machnikowski return -EFAULT; 1337172db5f9SMaciej Machnikowski } 1338172db5f9SMaciej Machnikowski 1339172db5f9SMaciej Machnikowski /** 13409ee31343SJacob Keller * ice_ptp_disable_all_clkout - Disable all currently configured outputs 13419ee31343SJacob Keller * @pf: pointer to the PF structure 13429ee31343SJacob Keller * 13439ee31343SJacob Keller * Disable all currently configured clock outputs. This is necessary before 13449ee31343SJacob Keller * certain changes to the PTP hardware clock. Use ice_ptp_enable_all_clkout to 13459ee31343SJacob Keller * re-enable the clocks again. 13469ee31343SJacob Keller */ 13479ee31343SJacob Keller static void ice_ptp_disable_all_clkout(struct ice_pf *pf) 13489ee31343SJacob Keller { 13499ee31343SJacob Keller uint i; 13509ee31343SJacob Keller 13519ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++) 13529ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena) 13539ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, NULL, false); 13549ee31343SJacob Keller } 13559ee31343SJacob Keller 13569ee31343SJacob Keller /** 13579ee31343SJacob Keller * ice_ptp_enable_all_clkout - Enable all configured periodic clock outputs 13589ee31343SJacob Keller * @pf: pointer to the PF structure 13599ee31343SJacob Keller * 13609ee31343SJacob Keller * Enable all currently configured clock outputs. Use this after 13619ee31343SJacob Keller * ice_ptp_disable_all_clkout to reconfigure the output signals according to 13629ee31343SJacob Keller * their configuration. 13639ee31343SJacob Keller */ 13649ee31343SJacob Keller static void ice_ptp_enable_all_clkout(struct ice_pf *pf) 13659ee31343SJacob Keller { 13669ee31343SJacob Keller uint i; 13679ee31343SJacob Keller 13689ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++) 13699ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena) 13709ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, &pf->ptp.perout_channels[i], 13719ee31343SJacob Keller false); 13729ee31343SJacob Keller } 13739ee31343SJacob Keller 13749ee31343SJacob Keller /** 1375172db5f9SMaciej Machnikowski * ice_ptp_gpio_enable_e810 - Enable/disable ancillary features of PHC 1376172db5f9SMaciej Machnikowski * @info: the driver's PTP info structure 1377172db5f9SMaciej Machnikowski * @rq: The requested feature to change 1378172db5f9SMaciej Machnikowski * @on: Enable/disable flag 1379172db5f9SMaciej Machnikowski */ 1380172db5f9SMaciej Machnikowski static int 1381172db5f9SMaciej Machnikowski ice_ptp_gpio_enable_e810(struct ptp_clock_info *info, 1382172db5f9SMaciej Machnikowski struct ptp_clock_request *rq, int on) 1383172db5f9SMaciej Machnikowski { 1384172db5f9SMaciej Machnikowski struct ice_pf *pf = ptp_info_to_pf(info); 1385172db5f9SMaciej Machnikowski struct ice_perout_channel clk_cfg = {0}; 1386325b2064SMaciej Machnikowski bool sma_pres = false; 1387172db5f9SMaciej Machnikowski unsigned int chan; 1388172db5f9SMaciej Machnikowski u32 gpio_pin; 1389172db5f9SMaciej Machnikowski int err; 1390172db5f9SMaciej Machnikowski 1391325b2064SMaciej Machnikowski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) 1392325b2064SMaciej Machnikowski sma_pres = true; 1393325b2064SMaciej Machnikowski 1394172db5f9SMaciej Machnikowski switch (rq->type) { 1395172db5f9SMaciej Machnikowski case PTP_CLK_REQ_PEROUT: 1396172db5f9SMaciej Machnikowski chan = rq->perout.index; 1397325b2064SMaciej Machnikowski if (sma_pres) { 1398325b2064SMaciej Machnikowski if (chan == ice_pin_desc_e810t[SMA1].chan) 1399325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20; 1400325b2064SMaciej Machnikowski else if (chan == ice_pin_desc_e810t[SMA2].chan) 1401325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22; 1402172db5f9SMaciej Machnikowski else 1403325b2064SMaciej Machnikowski return -1; 1404325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) { 1405325b2064SMaciej Machnikowski if (chan == 0) 1406325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20; 1407325b2064SMaciej Machnikowski else 1408325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22; 1409325b2064SMaciej Machnikowski } else if (chan == PPS_CLK_GEN_CHAN) { 1410325b2064SMaciej Machnikowski clk_cfg.gpio_pin = PPS_PIN_INDEX; 1411325b2064SMaciej Machnikowski } else { 1412172db5f9SMaciej Machnikowski clk_cfg.gpio_pin = chan; 1413325b2064SMaciej Machnikowski } 1414172db5f9SMaciej Machnikowski 1415172db5f9SMaciej Machnikowski clk_cfg.period = ((rq->perout.period.sec * NSEC_PER_SEC) + 1416172db5f9SMaciej Machnikowski rq->perout.period.nsec); 1417172db5f9SMaciej Machnikowski clk_cfg.start_time = ((rq->perout.start.sec * NSEC_PER_SEC) + 1418172db5f9SMaciej Machnikowski rq->perout.start.nsec); 1419172db5f9SMaciej Machnikowski clk_cfg.ena = !!on; 1420172db5f9SMaciej Machnikowski 1421172db5f9SMaciej Machnikowski err = ice_ptp_cfg_clkout(pf, chan, &clk_cfg, true); 1422172db5f9SMaciej Machnikowski break; 1423172db5f9SMaciej Machnikowski case PTP_CLK_REQ_EXTTS: 1424172db5f9SMaciej Machnikowski chan = rq->extts.index; 1425325b2064SMaciej Machnikowski if (sma_pres) { 1426325b2064SMaciej Machnikowski if (chan < ice_pin_desc_e810t[SMA2].chan) 1427325b2064SMaciej Machnikowski gpio_pin = GPIO_21; 1428325b2064SMaciej Machnikowski else 1429325b2064SMaciej Machnikowski gpio_pin = GPIO_23; 1430325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) { 1431325b2064SMaciej Machnikowski if (chan == 0) 1432325b2064SMaciej Machnikowski gpio_pin = GPIO_21; 1433325b2064SMaciej Machnikowski else 1434325b2064SMaciej Machnikowski gpio_pin = GPIO_23; 1435325b2064SMaciej Machnikowski } else { 1436172db5f9SMaciej Machnikowski gpio_pin = chan; 1437325b2064SMaciej Machnikowski } 1438172db5f9SMaciej Machnikowski 1439172db5f9SMaciej Machnikowski err = ice_ptp_cfg_extts(pf, !!on, chan, gpio_pin, 1440172db5f9SMaciej Machnikowski rq->extts.flags); 1441172db5f9SMaciej Machnikowski break; 1442172db5f9SMaciej Machnikowski default: 1443172db5f9SMaciej Machnikowski return -EOPNOTSUPP; 1444172db5f9SMaciej Machnikowski } 1445172db5f9SMaciej Machnikowski 1446172db5f9SMaciej Machnikowski return err; 1447172db5f9SMaciej Machnikowski } 1448172db5f9SMaciej Machnikowski 1449172db5f9SMaciej Machnikowski /** 145006c16d89SJacob Keller * ice_ptp_gettimex64 - Get the time of the clock 145106c16d89SJacob Keller * @info: the driver's PTP info structure 145206c16d89SJacob Keller * @ts: timespec64 structure to hold the current time value 145306c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 145406c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 145506c16d89SJacob Keller * 145606c16d89SJacob Keller * Read the device clock and return the correct value on ns, after converting it 145706c16d89SJacob Keller * into a timespec struct. 145806c16d89SJacob Keller */ 145906c16d89SJacob Keller static int 146006c16d89SJacob Keller ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts, 146106c16d89SJacob Keller struct ptp_system_timestamp *sts) 146206c16d89SJacob Keller { 146306c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 146406c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 146506c16d89SJacob Keller 146606c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 146706c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n"); 146806c16d89SJacob Keller return -EBUSY; 146906c16d89SJacob Keller } 147006c16d89SJacob Keller 147106c16d89SJacob Keller ice_ptp_read_time(pf, ts, sts); 147206c16d89SJacob Keller ice_ptp_unlock(hw); 147306c16d89SJacob Keller 147406c16d89SJacob Keller return 0; 147506c16d89SJacob Keller } 147606c16d89SJacob Keller 147706c16d89SJacob Keller /** 147806c16d89SJacob Keller * ice_ptp_settime64 - Set the time of the clock 147906c16d89SJacob Keller * @info: the driver's PTP info structure 148006c16d89SJacob Keller * @ts: timespec64 structure that holds the new time value 148106c16d89SJacob Keller * 148206c16d89SJacob Keller * Set the device clock to the user input value. The conversion from timespec 148306c16d89SJacob Keller * to ns happens in the write function. 148406c16d89SJacob Keller */ 148506c16d89SJacob Keller static int 148606c16d89SJacob Keller ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts) 148706c16d89SJacob Keller { 148806c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 148906c16d89SJacob Keller struct timespec64 ts64 = *ts; 149006c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 149106c16d89SJacob Keller int err; 149206c16d89SJacob Keller 14933a749623SJacob Keller /* For Vernier mode, we need to recalibrate after new settime 14943a749623SJacob Keller * Start with disabling timestamp block 14953a749623SJacob Keller */ 14963a749623SJacob Keller if (pf->ptp.port.link_up) 14973a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port); 14983a749623SJacob Keller 149906c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 150006c16d89SJacob Keller err = -EBUSY; 150106c16d89SJacob Keller goto exit; 150206c16d89SJacob Keller } 150306c16d89SJacob Keller 15049ee31343SJacob Keller /* Disable periodic outputs */ 15059ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 15069ee31343SJacob Keller 150706c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts64); 150806c16d89SJacob Keller ice_ptp_unlock(hw); 150906c16d89SJacob Keller 151077a78115SJacob Keller if (!err) 151177a78115SJacob Keller ice_ptp_update_cached_phctime(pf); 151277a78115SJacob Keller 15139ee31343SJacob Keller /* Reenable periodic outputs */ 15149ee31343SJacob Keller ice_ptp_enable_all_clkout(pf); 15153a749623SJacob Keller 15163a749623SJacob Keller /* Recalibrate and re-enable timestamp block */ 15173a749623SJacob Keller if (pf->ptp.port.link_up) 15183a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port); 151906c16d89SJacob Keller exit: 152006c16d89SJacob Keller if (err) { 152106c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err); 152206c16d89SJacob Keller return err; 152306c16d89SJacob Keller } 152406c16d89SJacob Keller 152506c16d89SJacob Keller return 0; 152606c16d89SJacob Keller } 152706c16d89SJacob Keller 152806c16d89SJacob Keller /** 152906c16d89SJacob Keller * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment 153006c16d89SJacob Keller * @info: the driver's PTP info structure 153106c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 153206c16d89SJacob Keller */ 153306c16d89SJacob Keller static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta) 153406c16d89SJacob Keller { 153506c16d89SJacob Keller struct timespec64 now, then; 153606c16d89SJacob Keller 153706c16d89SJacob Keller then = ns_to_timespec64(delta); 153806c16d89SJacob Keller ice_ptp_gettimex64(info, &now, NULL); 153906c16d89SJacob Keller now = timespec64_add(now, then); 154006c16d89SJacob Keller 154106c16d89SJacob Keller return ice_ptp_settime64(info, (const struct timespec64 *)&now); 154206c16d89SJacob Keller } 154306c16d89SJacob Keller 154406c16d89SJacob Keller /** 154506c16d89SJacob Keller * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta 154606c16d89SJacob Keller * @info: the driver's PTP info structure 154706c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 154806c16d89SJacob Keller */ 154906c16d89SJacob Keller static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta) 155006c16d89SJacob Keller { 155106c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 155206c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 155306c16d89SJacob Keller struct device *dev; 155406c16d89SJacob Keller int err; 155506c16d89SJacob Keller 155606c16d89SJacob Keller dev = ice_pf_to_dev(pf); 155706c16d89SJacob Keller 155806c16d89SJacob Keller /* Hardware only supports atomic adjustments using signed 32-bit 155906c16d89SJacob Keller * integers. For any adjustment outside this range, perform 156006c16d89SJacob Keller * a non-atomic get->adjust->set flow. 156106c16d89SJacob Keller */ 156206c16d89SJacob Keller if (delta > S32_MAX || delta < S32_MIN) { 156306c16d89SJacob Keller dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta); 156406c16d89SJacob Keller return ice_ptp_adjtime_nonatomic(info, delta); 156506c16d89SJacob Keller } 156606c16d89SJacob Keller 156706c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 156806c16d89SJacob Keller dev_err(dev, "PTP failed to acquire semaphore in adjtime\n"); 156906c16d89SJacob Keller return -EBUSY; 157006c16d89SJacob Keller } 157106c16d89SJacob Keller 15729ee31343SJacob Keller /* Disable periodic outputs */ 15739ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 15749ee31343SJacob Keller 157506c16d89SJacob Keller err = ice_ptp_write_adj(pf, delta); 157606c16d89SJacob Keller 15779ee31343SJacob Keller /* Reenable periodic outputs */ 15789ee31343SJacob Keller ice_ptp_enable_all_clkout(pf); 15799ee31343SJacob Keller 158006c16d89SJacob Keller ice_ptp_unlock(hw); 158106c16d89SJacob Keller 158206c16d89SJacob Keller if (err) { 158306c16d89SJacob Keller dev_err(dev, "PTP failed to adjust time, err %d\n", err); 158406c16d89SJacob Keller return err; 158506c16d89SJacob Keller } 158606c16d89SJacob Keller 158777a78115SJacob Keller ice_ptp_update_cached_phctime(pf); 158877a78115SJacob Keller 158906c16d89SJacob Keller return 0; 159006c16d89SJacob Keller } 159106c16d89SJacob Keller 159213a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS 159313a64f0bSJacob Keller /** 159413a64f0bSJacob Keller * ice_ptp_get_syncdevicetime - Get the cross time stamp info 159513a64f0bSJacob Keller * @device: Current device time 159613a64f0bSJacob Keller * @system: System counter value read synchronously with device time 159713a64f0bSJacob Keller * @ctx: Context provided by timekeeping code 159813a64f0bSJacob Keller * 159913a64f0bSJacob Keller * Read device and system (ART) clock simultaneously and return the corrected 160013a64f0bSJacob Keller * clock values in ns. 160113a64f0bSJacob Keller */ 160213a64f0bSJacob Keller static int 160313a64f0bSJacob Keller ice_ptp_get_syncdevicetime(ktime_t *device, 160413a64f0bSJacob Keller struct system_counterval_t *system, 160513a64f0bSJacob Keller void *ctx) 160613a64f0bSJacob Keller { 160713a64f0bSJacob Keller struct ice_pf *pf = (struct ice_pf *)ctx; 160813a64f0bSJacob Keller struct ice_hw *hw = &pf->hw; 160913a64f0bSJacob Keller u32 hh_lock, hh_art_ctl; 161013a64f0bSJacob Keller int i; 161113a64f0bSJacob Keller 161213a64f0bSJacob Keller /* Get the HW lock */ 161313a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id)); 161413a64f0bSJacob Keller if (hh_lock & PFHH_SEM_BUSY_M) { 161513a64f0bSJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get hh lock\n"); 161613a64f0bSJacob Keller return -EFAULT; 161713a64f0bSJacob Keller } 161813a64f0bSJacob Keller 161913a64f0bSJacob Keller /* Start the ART and device clock sync sequence */ 162013a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL); 162113a64f0bSJacob Keller hh_art_ctl = hh_art_ctl | GLHH_ART_CTL_ACTIVE_M; 162213a64f0bSJacob Keller wr32(hw, GLHH_ART_CTL, hh_art_ctl); 162313a64f0bSJacob Keller 162413a64f0bSJacob Keller #define MAX_HH_LOCK_TRIES 100 162513a64f0bSJacob Keller 162613a64f0bSJacob Keller for (i = 0; i < MAX_HH_LOCK_TRIES; i++) { 162713a64f0bSJacob Keller /* Wait for sync to complete */ 162813a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL); 162913a64f0bSJacob Keller if (hh_art_ctl & GLHH_ART_CTL_ACTIVE_M) { 163013a64f0bSJacob Keller udelay(1); 163113a64f0bSJacob Keller continue; 163213a64f0bSJacob Keller } else { 163313a64f0bSJacob Keller u32 hh_ts_lo, hh_ts_hi, tmr_idx; 163413a64f0bSJacob Keller u64 hh_ts; 163513a64f0bSJacob Keller 163613a64f0bSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 163713a64f0bSJacob Keller /* Read ART time */ 163813a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLHH_ART_TIME_L); 163913a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLHH_ART_TIME_H); 164013a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo; 164113a64f0bSJacob Keller *system = convert_art_ns_to_tsc(hh_ts); 164213a64f0bSJacob Keller /* Read Device source clock time */ 164313a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLTSYN_HHTIME_L(tmr_idx)); 164413a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLTSYN_HHTIME_H(tmr_idx)); 164513a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo; 164613a64f0bSJacob Keller *device = ns_to_ktime(hh_ts); 164713a64f0bSJacob Keller break; 164813a64f0bSJacob Keller } 164913a64f0bSJacob Keller } 165013a64f0bSJacob Keller /* Release HW lock */ 165113a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id)); 165213a64f0bSJacob Keller hh_lock = hh_lock & ~PFHH_SEM_BUSY_M; 165313a64f0bSJacob Keller wr32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), hh_lock); 165413a64f0bSJacob Keller 165513a64f0bSJacob Keller if (i == MAX_HH_LOCK_TRIES) 165613a64f0bSJacob Keller return -ETIMEDOUT; 165713a64f0bSJacob Keller 165813a64f0bSJacob Keller return 0; 165913a64f0bSJacob Keller } 166013a64f0bSJacob Keller 166113a64f0bSJacob Keller /** 166213a64f0bSJacob Keller * ice_ptp_getcrosststamp_e822 - Capture a device cross timestamp 166313a64f0bSJacob Keller * @info: the driver's PTP info structure 166413a64f0bSJacob Keller * @cts: The memory to fill the cross timestamp info 166513a64f0bSJacob Keller * 166613a64f0bSJacob Keller * Capture a cross timestamp between the ART and the device PTP hardware 166713a64f0bSJacob Keller * clock. Fill the cross timestamp information and report it back to the 166813a64f0bSJacob Keller * caller. 166913a64f0bSJacob Keller * 167013a64f0bSJacob Keller * This is only valid for E822 devices which have support for generating the 167113a64f0bSJacob Keller * cross timestamp via PCIe PTM. 167213a64f0bSJacob Keller * 167313a64f0bSJacob Keller * In order to correctly correlate the ART timestamp back to the TSC time, the 167413a64f0bSJacob Keller * CPU must have X86_FEATURE_TSC_KNOWN_FREQ. 167513a64f0bSJacob Keller */ 167613a64f0bSJacob Keller static int 167713a64f0bSJacob Keller ice_ptp_getcrosststamp_e822(struct ptp_clock_info *info, 167813a64f0bSJacob Keller struct system_device_crosststamp *cts) 167913a64f0bSJacob Keller { 168013a64f0bSJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 168113a64f0bSJacob Keller 168213a64f0bSJacob Keller return get_device_system_crosststamp(ice_ptp_get_syncdevicetime, 168313a64f0bSJacob Keller pf, NULL, cts); 168413a64f0bSJacob Keller } 168513a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */ 168613a64f0bSJacob Keller 168706c16d89SJacob Keller /** 168877a78115SJacob Keller * ice_ptp_get_ts_config - ioctl interface to read the timestamping config 168977a78115SJacob Keller * @pf: Board private structure 169077a78115SJacob Keller * @ifr: ioctl data 169177a78115SJacob Keller * 169277a78115SJacob Keller * Copy the timestamping config to user buffer 169377a78115SJacob Keller */ 169477a78115SJacob Keller int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr) 169577a78115SJacob Keller { 169677a78115SJacob Keller struct hwtstamp_config *config; 169777a78115SJacob Keller 169877a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 169977a78115SJacob Keller return -EIO; 170077a78115SJacob Keller 170177a78115SJacob Keller config = &pf->ptp.tstamp_config; 170277a78115SJacob Keller 170377a78115SJacob Keller return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ? 170477a78115SJacob Keller -EFAULT : 0; 170577a78115SJacob Keller } 170677a78115SJacob Keller 170777a78115SJacob Keller /** 170877a78115SJacob Keller * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode 170977a78115SJacob Keller * @pf: Board private structure 171077a78115SJacob Keller * @config: hwtstamp settings requested or saved 171177a78115SJacob Keller */ 171277a78115SJacob Keller static int 171377a78115SJacob Keller ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config) 171477a78115SJacob Keller { 171577a78115SJacob Keller switch (config->tx_type) { 171677a78115SJacob Keller case HWTSTAMP_TX_OFF: 1717ea9b847cSJacob Keller ice_set_tx_tstamp(pf, false); 1718ea9b847cSJacob Keller break; 1719ea9b847cSJacob Keller case HWTSTAMP_TX_ON: 1720ea9b847cSJacob Keller ice_set_tx_tstamp(pf, true); 172177a78115SJacob Keller break; 172277a78115SJacob Keller default: 172377a78115SJacob Keller return -ERANGE; 172477a78115SJacob Keller } 172577a78115SJacob Keller 172677a78115SJacob Keller switch (config->rx_filter) { 172777a78115SJacob Keller case HWTSTAMP_FILTER_NONE: 172877a78115SJacob Keller ice_set_rx_tstamp(pf, false); 172977a78115SJacob Keller break; 173077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 173177a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 173277a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 173377a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_EVENT: 173477a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 173577a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 173677a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_SYNC: 173777a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 173877a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 173977a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 174077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 174177a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 174277a78115SJacob Keller case HWTSTAMP_FILTER_NTP_ALL: 174377a78115SJacob Keller case HWTSTAMP_FILTER_ALL: 174477a78115SJacob Keller ice_set_rx_tstamp(pf, true); 174577a78115SJacob Keller break; 174677a78115SJacob Keller default: 174777a78115SJacob Keller return -ERANGE; 174877a78115SJacob Keller } 174977a78115SJacob Keller 175077a78115SJacob Keller return 0; 175177a78115SJacob Keller } 175277a78115SJacob Keller 175377a78115SJacob Keller /** 175477a78115SJacob Keller * ice_ptp_set_ts_config - ioctl interface to control the timestamping 175577a78115SJacob Keller * @pf: Board private structure 175677a78115SJacob Keller * @ifr: ioctl data 175777a78115SJacob Keller * 175877a78115SJacob Keller * Get the user config and store it 175977a78115SJacob Keller */ 176077a78115SJacob Keller int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr) 176177a78115SJacob Keller { 176277a78115SJacob Keller struct hwtstamp_config config; 176377a78115SJacob Keller int err; 176477a78115SJacob Keller 176577a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 176677a78115SJacob Keller return -EAGAIN; 176777a78115SJacob Keller 176877a78115SJacob Keller if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 176977a78115SJacob Keller return -EFAULT; 177077a78115SJacob Keller 177177a78115SJacob Keller err = ice_ptp_set_timestamp_mode(pf, &config); 177277a78115SJacob Keller if (err) 177377a78115SJacob Keller return err; 177477a78115SJacob Keller 1775e59d75ddSJacob Keller /* Return the actual configuration set */ 1776e59d75ddSJacob Keller config = pf->ptp.tstamp_config; 177777a78115SJacob Keller 177877a78115SJacob Keller return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 177977a78115SJacob Keller -EFAULT : 0; 178077a78115SJacob Keller } 178177a78115SJacob Keller 178277a78115SJacob Keller /** 178377a78115SJacob Keller * ice_ptp_rx_hwtstamp - Check for an Rx timestamp 178477a78115SJacob Keller * @rx_ring: Ring to get the VSI info 178577a78115SJacob Keller * @rx_desc: Receive descriptor 178677a78115SJacob Keller * @skb: Particular skb to send timestamp with 178777a78115SJacob Keller * 178877a78115SJacob Keller * The driver receives a notification in the receive descriptor with timestamp. 178977a78115SJacob Keller * The timestamp is in ns, so we must convert the result first. 179077a78115SJacob Keller */ 179177a78115SJacob Keller void 1792e72bba21SMaciej Fijalkowski ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring, 179377a78115SJacob Keller union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) 179477a78115SJacob Keller { 179577a78115SJacob Keller u32 ts_high; 179677a78115SJacob Keller u64 ts_ns; 179777a78115SJacob Keller 179877a78115SJacob Keller /* Populate timesync data into skb */ 179977a78115SJacob Keller if (rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID) { 180077a78115SJacob Keller struct skb_shared_hwtstamps *hwtstamps; 180177a78115SJacob Keller 180277a78115SJacob Keller /* Use ice_ptp_extend_32b_ts directly, using the ring-specific 180377a78115SJacob Keller * cached PHC value, rather than accessing the PF. This also 180477a78115SJacob Keller * allows us to simply pass the upper 32bits of nanoseconds 180577a78115SJacob Keller * directly. Calling ice_ptp_extend_40b_ts is unnecessary as 180677a78115SJacob Keller * it would just discard these bits itself. 180777a78115SJacob Keller */ 180877a78115SJacob Keller ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high); 180977a78115SJacob Keller ts_ns = ice_ptp_extend_32b_ts(rx_ring->cached_phctime, ts_high); 181077a78115SJacob Keller 181177a78115SJacob Keller hwtstamps = skb_hwtstamps(skb); 181277a78115SJacob Keller memset(hwtstamps, 0, sizeof(*hwtstamps)); 181377a78115SJacob Keller hwtstamps->hwtstamp = ns_to_ktime(ts_ns); 181477a78115SJacob Keller } 181577a78115SJacob Keller } 181677a78115SJacob Keller 181777a78115SJacob Keller /** 1818325b2064SMaciej Machnikowski * ice_ptp_disable_sma_pins_e810t - Disable E810-T SMA pins 1819325b2064SMaciej Machnikowski * @pf: pointer to the PF structure 1820325b2064SMaciej Machnikowski * @info: PTP clock info structure 1821325b2064SMaciej Machnikowski * 1822325b2064SMaciej Machnikowski * Disable the OS access to the SMA pins. Called to clear out the OS 1823325b2064SMaciej Machnikowski * indications of pin support when we fail to setup the E810-T SMA control 1824325b2064SMaciej Machnikowski * register. 1825325b2064SMaciej Machnikowski */ 1826325b2064SMaciej Machnikowski static void 1827325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 1828325b2064SMaciej Machnikowski { 1829325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf); 1830325b2064SMaciej Machnikowski 1831325b2064SMaciej Machnikowski dev_warn(dev, "Failed to configure E810-T SMA pin control\n"); 1832325b2064SMaciej Machnikowski 1833325b2064SMaciej Machnikowski info->enable = NULL; 1834325b2064SMaciej Machnikowski info->verify = NULL; 1835325b2064SMaciej Machnikowski info->n_pins = 0; 1836325b2064SMaciej Machnikowski info->n_ext_ts = 0; 1837325b2064SMaciej Machnikowski info->n_per_out = 0; 1838325b2064SMaciej Machnikowski } 1839325b2064SMaciej Machnikowski 1840325b2064SMaciej Machnikowski /** 1841325b2064SMaciej Machnikowski * ice_ptp_setup_sma_pins_e810t - Setup the SMA pins 1842325b2064SMaciej Machnikowski * @pf: pointer to the PF structure 1843325b2064SMaciej Machnikowski * @info: PTP clock info structure 1844325b2064SMaciej Machnikowski * 1845325b2064SMaciej Machnikowski * Finish setting up the SMA pins by allocating pin_config, and setting it up 1846325b2064SMaciej Machnikowski * according to the current status of the SMA. On failure, disable all of the 1847325b2064SMaciej Machnikowski * extended SMA pin support. 1848325b2064SMaciej Machnikowski */ 1849325b2064SMaciej Machnikowski static void 1850325b2064SMaciej Machnikowski ice_ptp_setup_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 1851325b2064SMaciej Machnikowski { 1852325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf); 1853325b2064SMaciej Machnikowski int err; 1854325b2064SMaciej Machnikowski 1855325b2064SMaciej Machnikowski /* Allocate memory for kernel pins interface */ 1856325b2064SMaciej Machnikowski info->pin_config = devm_kcalloc(dev, info->n_pins, 1857325b2064SMaciej Machnikowski sizeof(*info->pin_config), GFP_KERNEL); 1858325b2064SMaciej Machnikowski if (!info->pin_config) { 1859325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info); 1860325b2064SMaciej Machnikowski return; 1861325b2064SMaciej Machnikowski } 1862325b2064SMaciej Machnikowski 1863325b2064SMaciej Machnikowski /* Read current SMA status */ 1864325b2064SMaciej Machnikowski err = ice_get_sma_config_e810t(&pf->hw, info->pin_config); 1865325b2064SMaciej Machnikowski if (err) 1866325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info); 1867325b2064SMaciej Machnikowski } 1868325b2064SMaciej Machnikowski 1869325b2064SMaciej Machnikowski /** 1870325b2064SMaciej Machnikowski * ice_ptp_setup_pins_e810t - Setup PTP pins in sysfs 1871325b2064SMaciej Machnikowski * @pf: pointer to the PF instance 1872325b2064SMaciej Machnikowski * @info: PTP clock capabilities 1873325b2064SMaciej Machnikowski */ 1874325b2064SMaciej Machnikowski static void 1875325b2064SMaciej Machnikowski ice_ptp_setup_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 1876325b2064SMaciej Machnikowski { 1877325b2064SMaciej Machnikowski /* Check if SMA controller is in the netlist */ 1878325b2064SMaciej Machnikowski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL) && 1879325b2064SMaciej Machnikowski !ice_is_pca9575_present(&pf->hw)) 1880325b2064SMaciej Machnikowski ice_clear_feature_support(pf, ICE_F_SMA_CTRL); 1881325b2064SMaciej Machnikowski 1882325b2064SMaciej Machnikowski if (!ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) { 1883325b2064SMaciej Machnikowski info->n_ext_ts = N_EXT_TS_E810_NO_SMA; 1884325b2064SMaciej Machnikowski info->n_per_out = N_PER_OUT_E810T_NO_SMA; 1885325b2064SMaciej Machnikowski return; 1886325b2064SMaciej Machnikowski } 1887325b2064SMaciej Machnikowski 1888325b2064SMaciej Machnikowski info->n_per_out = N_PER_OUT_E810T; 1889325b2064SMaciej Machnikowski info->n_ext_ts = N_EXT_TS_E810; 1890325b2064SMaciej Machnikowski info->n_pins = NUM_PTP_PINS_E810T; 1891325b2064SMaciej Machnikowski info->verify = ice_verify_pin_e810t; 1892325b2064SMaciej Machnikowski 1893325b2064SMaciej Machnikowski /* Complete setup of the SMA pins */ 1894325b2064SMaciej Machnikowski ice_ptp_setup_sma_pins_e810t(pf, info); 1895325b2064SMaciej Machnikowski } 1896325b2064SMaciej Machnikowski 1897325b2064SMaciej Machnikowski /** 1898172db5f9SMaciej Machnikowski * ice_ptp_setup_pins_e810 - Setup PTP pins in sysfs 1899172db5f9SMaciej Machnikowski * @info: PTP clock capabilities 1900172db5f9SMaciej Machnikowski */ 1901172db5f9SMaciej Machnikowski static void ice_ptp_setup_pins_e810(struct ptp_clock_info *info) 1902172db5f9SMaciej Machnikowski { 1903325b2064SMaciej Machnikowski info->n_per_out = N_PER_OUT_E810; 1904325b2064SMaciej Machnikowski info->n_ext_ts = N_EXT_TS_E810; 1905172db5f9SMaciej Machnikowski } 1906172db5f9SMaciej Machnikowski 1907172db5f9SMaciej Machnikowski /** 190813a64f0bSJacob Keller * ice_ptp_set_funcs_e822 - Set specialized functions for E822 support 190913a64f0bSJacob Keller * @pf: Board private structure 191013a64f0bSJacob Keller * @info: PTP info to fill 191113a64f0bSJacob Keller * 191213a64f0bSJacob Keller * Assign functions to the PTP capabiltiies structure for E822 devices. 191313a64f0bSJacob Keller * Functions which operate across all device families should be set directly 191413a64f0bSJacob Keller * in ice_ptp_set_caps. Only add functions here which are distinct for E822 191513a64f0bSJacob Keller * devices. 191613a64f0bSJacob Keller */ 191713a64f0bSJacob Keller static void 191813a64f0bSJacob Keller ice_ptp_set_funcs_e822(struct ice_pf *pf, struct ptp_clock_info *info) 191913a64f0bSJacob Keller { 192013a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS 192113a64f0bSJacob Keller if (boot_cpu_has(X86_FEATURE_ART) && 192213a64f0bSJacob Keller boot_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ)) 192313a64f0bSJacob Keller info->getcrosststamp = ice_ptp_getcrosststamp_e822; 192413a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */ 192513a64f0bSJacob Keller } 192613a64f0bSJacob Keller 192713a64f0bSJacob Keller /** 1928172db5f9SMaciej Machnikowski * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support 1929172db5f9SMaciej Machnikowski * @pf: Board private structure 1930172db5f9SMaciej Machnikowski * @info: PTP info to fill 1931172db5f9SMaciej Machnikowski * 1932172db5f9SMaciej Machnikowski * Assign functions to the PTP capabiltiies structure for E810 devices. 1933172db5f9SMaciej Machnikowski * Functions which operate across all device families should be set directly 1934172db5f9SMaciej Machnikowski * in ice_ptp_set_caps. Only add functions here which are distinct for e810 1935172db5f9SMaciej Machnikowski * devices. 1936172db5f9SMaciej Machnikowski */ 1937172db5f9SMaciej Machnikowski static void 1938172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(struct ice_pf *pf, struct ptp_clock_info *info) 1939172db5f9SMaciej Machnikowski { 1940172db5f9SMaciej Machnikowski info->enable = ice_ptp_gpio_enable_e810; 1941172db5f9SMaciej Machnikowski 1942325b2064SMaciej Machnikowski if (ice_is_e810t(&pf->hw)) 1943325b2064SMaciej Machnikowski ice_ptp_setup_pins_e810t(pf, info); 1944325b2064SMaciej Machnikowski else 1945172db5f9SMaciej Machnikowski ice_ptp_setup_pins_e810(info); 1946172db5f9SMaciej Machnikowski } 1947172db5f9SMaciej Machnikowski 1948172db5f9SMaciej Machnikowski /** 194906c16d89SJacob Keller * ice_ptp_set_caps - Set PTP capabilities 195006c16d89SJacob Keller * @pf: Board private structure 195106c16d89SJacob Keller */ 195206c16d89SJacob Keller static void ice_ptp_set_caps(struct ice_pf *pf) 195306c16d89SJacob Keller { 195406c16d89SJacob Keller struct ptp_clock_info *info = &pf->ptp.info; 195506c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf); 195606c16d89SJacob Keller 195706c16d89SJacob Keller snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk", 195806c16d89SJacob Keller dev_driver_string(dev), dev_name(dev)); 195906c16d89SJacob Keller info->owner = THIS_MODULE; 196006c16d89SJacob Keller info->max_adj = 999999999; 196106c16d89SJacob Keller info->adjtime = ice_ptp_adjtime; 196206c16d89SJacob Keller info->adjfine = ice_ptp_adjfine; 196306c16d89SJacob Keller info->gettimex64 = ice_ptp_gettimex64; 196406c16d89SJacob Keller info->settime64 = ice_ptp_settime64; 1965172db5f9SMaciej Machnikowski 19663a749623SJacob Keller if (ice_is_e810(&pf->hw)) 1967172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(pf, info); 196813a64f0bSJacob Keller else 196913a64f0bSJacob Keller ice_ptp_set_funcs_e822(pf, info); 197006c16d89SJacob Keller } 197106c16d89SJacob Keller 197206c16d89SJacob Keller /** 197306c16d89SJacob Keller * ice_ptp_create_clock - Create PTP clock device for userspace 197406c16d89SJacob Keller * @pf: Board private structure 197506c16d89SJacob Keller * 197606c16d89SJacob Keller * This function creates a new PTP clock device. It only creates one if we 197706c16d89SJacob Keller * don't already have one. Will return error if it can't create one, but success 197806c16d89SJacob Keller * if we already have a device. Should be used by ice_ptp_init to create clock 197906c16d89SJacob Keller * initially, and prevent global resets from creating new clock devices. 198006c16d89SJacob Keller */ 198106c16d89SJacob Keller static long ice_ptp_create_clock(struct ice_pf *pf) 198206c16d89SJacob Keller { 198306c16d89SJacob Keller struct ptp_clock_info *info; 198406c16d89SJacob Keller struct ptp_clock *clock; 198506c16d89SJacob Keller struct device *dev; 198606c16d89SJacob Keller 198706c16d89SJacob Keller /* No need to create a clock device if we already have one */ 198806c16d89SJacob Keller if (pf->ptp.clock) 198906c16d89SJacob Keller return 0; 199006c16d89SJacob Keller 199106c16d89SJacob Keller ice_ptp_set_caps(pf); 199206c16d89SJacob Keller 199306c16d89SJacob Keller info = &pf->ptp.info; 199406c16d89SJacob Keller dev = ice_pf_to_dev(pf); 199506c16d89SJacob Keller 199606c16d89SJacob Keller /* Attempt to register the clock before enabling the hardware. */ 199706c16d89SJacob Keller clock = ptp_clock_register(info, dev); 199806c16d89SJacob Keller if (IS_ERR(clock)) 199906c16d89SJacob Keller return PTR_ERR(clock); 200006c16d89SJacob Keller 200106c16d89SJacob Keller pf->ptp.clock = clock; 200206c16d89SJacob Keller 200306c16d89SJacob Keller return 0; 200406c16d89SJacob Keller } 200506c16d89SJacob Keller 2006ea9b847cSJacob Keller /** 2007ea9b847cSJacob Keller * ice_ptp_tx_tstamp_work - Process Tx timestamps for a port 2008ea9b847cSJacob Keller * @work: pointer to the kthread_work struct 2009ea9b847cSJacob Keller * 2010ea9b847cSJacob Keller * Process timestamps captured by the PHY associated with this port. To do 2011ea9b847cSJacob Keller * this, loop over each index with a waiting skb. 2012ea9b847cSJacob Keller * 2013ea9b847cSJacob Keller * If a given index has a valid timestamp, perform the following steps: 2014ea9b847cSJacob Keller * 2015ea9b847cSJacob Keller * 1) copy the timestamp out of the PHY register 2016ea9b847cSJacob Keller * 4) clear the timestamp valid bit in the PHY register 2017ea9b847cSJacob Keller * 5) unlock the index by clearing the associated in_use bit. 2018ea9b847cSJacob Keller * 2) extend the 40b timestamp value to get a 64bit timestamp 2019ea9b847cSJacob Keller * 3) send that timestamp to the stack 2020ea9b847cSJacob Keller * 2021ea9b847cSJacob Keller * After looping, if we still have waiting SKBs, then re-queue the work. This 2022ea9b847cSJacob Keller * may cause us effectively poll even when not strictly necessary. We do this 2023ea9b847cSJacob Keller * because it's possible a new timestamp was requested around the same time as 2024ea9b847cSJacob Keller * the interrupt. In some cases hardware might not interrupt us again when the 2025ea9b847cSJacob Keller * timestamp is captured. 2026ea9b847cSJacob Keller * 2027ea9b847cSJacob Keller * Note that we only take the tracking lock when clearing the bit and when 2028ea9b847cSJacob Keller * checking if we need to re-queue this task. The only place where bits can be 2029ea9b847cSJacob Keller * set is the hard xmit routine where an SKB has a request flag set. The only 2030ea9b847cSJacob Keller * places where we clear bits are this work function, or the periodic cleanup 2031ea9b847cSJacob Keller * thread. If the cleanup thread clears a bit we're processing we catch it 2032ea9b847cSJacob Keller * when we lock to clear the bit and then grab the SKB pointer. If a Tx thread 2033ea9b847cSJacob Keller * starts a new timestamp, we might not begin processing it right away but we 2034ea9b847cSJacob Keller * will notice it at the end when we re-queue the work item. If a Tx thread 2035ea9b847cSJacob Keller * starts a new timestamp just after this function exits without re-queuing, 2036ea9b847cSJacob Keller * the interrupt when the timestamp finishes should trigger. Avoiding holding 2037ea9b847cSJacob Keller * the lock for the entire function is important in order to ensure that Tx 2038ea9b847cSJacob Keller * threads do not get blocked while waiting for the lock. 2039ea9b847cSJacob Keller */ 2040ea9b847cSJacob Keller static void ice_ptp_tx_tstamp_work(struct kthread_work *work) 2041ea9b847cSJacob Keller { 2042ea9b847cSJacob Keller struct ice_ptp_port *ptp_port; 2043ea9b847cSJacob Keller struct ice_ptp_tx *tx; 2044ea9b847cSJacob Keller struct ice_pf *pf; 2045ea9b847cSJacob Keller struct ice_hw *hw; 2046ea9b847cSJacob Keller u8 idx; 2047ea9b847cSJacob Keller 2048ea9b847cSJacob Keller tx = container_of(work, struct ice_ptp_tx, work); 2049ea9b847cSJacob Keller if (!tx->init) 2050ea9b847cSJacob Keller return; 2051ea9b847cSJacob Keller 2052ea9b847cSJacob Keller ptp_port = container_of(tx, struct ice_ptp_port, tx); 2053ea9b847cSJacob Keller pf = ptp_port_to_pf(ptp_port); 2054ea9b847cSJacob Keller hw = &pf->hw; 2055ea9b847cSJacob Keller 2056ea9b847cSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 2057ea9b847cSJacob Keller struct skb_shared_hwtstamps shhwtstamps = {}; 2058ea9b847cSJacob Keller u8 phy_idx = idx + tx->quad_offset; 2059ea9b847cSJacob Keller u64 raw_tstamp, tstamp; 2060ea9b847cSJacob Keller struct sk_buff *skb; 2061ea9b847cSJacob Keller int err; 2062ea9b847cSJacob Keller 2063ea9b847cSJacob Keller err = ice_read_phy_tstamp(hw, tx->quad, phy_idx, 2064ea9b847cSJacob Keller &raw_tstamp); 2065ea9b847cSJacob Keller if (err) 2066ea9b847cSJacob Keller continue; 2067ea9b847cSJacob Keller 206837e738b6SKarol Kolacinski /* Check if the timestamp is invalid or stale */ 206937e738b6SKarol Kolacinski if (!(raw_tstamp & ICE_PTP_TS_VALID) || 207037e738b6SKarol Kolacinski raw_tstamp == tx->tstamps[idx].cached_tstamp) 2071ea9b847cSJacob Keller continue; 2072ea9b847cSJacob Keller 2073ea9b847cSJacob Keller /* The timestamp is valid, so we'll go ahead and clear this 2074ea9b847cSJacob Keller * index and then send the timestamp up to the stack. 2075ea9b847cSJacob Keller */ 2076ea9b847cSJacob Keller spin_lock(&tx->lock); 207737e738b6SKarol Kolacinski tx->tstamps[idx].cached_tstamp = raw_tstamp; 2078ea9b847cSJacob Keller clear_bit(idx, tx->in_use); 2079ea9b847cSJacob Keller skb = tx->tstamps[idx].skb; 2080ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 2081ea9b847cSJacob Keller spin_unlock(&tx->lock); 2082ea9b847cSJacob Keller 2083ea9b847cSJacob Keller /* it's (unlikely but) possible we raced with the cleanup 2084ea9b847cSJacob Keller * thread for discarding old timestamp requests. 2085ea9b847cSJacob Keller */ 2086ea9b847cSJacob Keller if (!skb) 2087ea9b847cSJacob Keller continue; 2088ea9b847cSJacob Keller 2089ea9b847cSJacob Keller /* Extend the timestamp using cached PHC time */ 2090ea9b847cSJacob Keller tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp); 2091ea9b847cSJacob Keller shhwtstamps.hwtstamp = ns_to_ktime(tstamp); 2092ea9b847cSJacob Keller 2093ea9b847cSJacob Keller skb_tstamp_tx(skb, &shhwtstamps); 2094ea9b847cSJacob Keller dev_kfree_skb_any(skb); 2095ea9b847cSJacob Keller } 2096ea9b847cSJacob Keller 2097ea9b847cSJacob Keller /* Check if we still have work to do. If so, re-queue this task to 2098ea9b847cSJacob Keller * poll for remaining timestamps. 2099ea9b847cSJacob Keller */ 2100ea9b847cSJacob Keller spin_lock(&tx->lock); 2101ea9b847cSJacob Keller if (!bitmap_empty(tx->in_use, tx->len)) 2102ea9b847cSJacob Keller kthread_queue_work(pf->ptp.kworker, &tx->work); 2103ea9b847cSJacob Keller spin_unlock(&tx->lock); 2104ea9b847cSJacob Keller } 2105ea9b847cSJacob Keller 2106ea9b847cSJacob Keller /** 2107ea9b847cSJacob Keller * ice_ptp_request_ts - Request an available Tx timestamp index 2108ea9b847cSJacob Keller * @tx: the PTP Tx timestamp tracker to request from 2109ea9b847cSJacob Keller * @skb: the SKB to associate with this timestamp request 2110ea9b847cSJacob Keller */ 2111ea9b847cSJacob Keller s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb) 2112ea9b847cSJacob Keller { 2113ea9b847cSJacob Keller u8 idx; 2114ea9b847cSJacob Keller 2115ea9b847cSJacob Keller /* Check if this tracker is initialized */ 21163a749623SJacob Keller if (!tx->init || tx->calibrating) 2117ea9b847cSJacob Keller return -1; 2118ea9b847cSJacob Keller 2119ea9b847cSJacob Keller spin_lock(&tx->lock); 2120ea9b847cSJacob Keller /* Find and set the first available index */ 2121ea9b847cSJacob Keller idx = find_first_zero_bit(tx->in_use, tx->len); 2122ea9b847cSJacob Keller if (idx < tx->len) { 2123ea9b847cSJacob Keller /* We got a valid index that no other thread could have set. Store 2124ea9b847cSJacob Keller * a reference to the skb and the start time to allow discarding old 2125ea9b847cSJacob Keller * requests. 2126ea9b847cSJacob Keller */ 2127ea9b847cSJacob Keller set_bit(idx, tx->in_use); 2128ea9b847cSJacob Keller tx->tstamps[idx].start = jiffies; 2129ea9b847cSJacob Keller tx->tstamps[idx].skb = skb_get(skb); 2130ea9b847cSJacob Keller skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 2131ea9b847cSJacob Keller } 2132ea9b847cSJacob Keller 2133ea9b847cSJacob Keller spin_unlock(&tx->lock); 2134ea9b847cSJacob Keller 2135ea9b847cSJacob Keller /* return the appropriate PHY timestamp register index, -1 if no 2136ea9b847cSJacob Keller * indexes were available. 2137ea9b847cSJacob Keller */ 2138ea9b847cSJacob Keller if (idx >= tx->len) 2139ea9b847cSJacob Keller return -1; 2140ea9b847cSJacob Keller else 2141ea9b847cSJacob Keller return idx + tx->quad_offset; 2142ea9b847cSJacob Keller } 2143ea9b847cSJacob Keller 2144ea9b847cSJacob Keller /** 2145ea9b847cSJacob Keller * ice_ptp_process_ts - Spawn kthread work to handle timestamps 2146ea9b847cSJacob Keller * @pf: Board private structure 2147ea9b847cSJacob Keller * 2148ea9b847cSJacob Keller * Queue work required to process the PTP Tx timestamps outside of interrupt 2149ea9b847cSJacob Keller * context. 2150ea9b847cSJacob Keller */ 2151ea9b847cSJacob Keller void ice_ptp_process_ts(struct ice_pf *pf) 2152ea9b847cSJacob Keller { 2153ea9b847cSJacob Keller if (pf->ptp.port.tx.init) 2154ea9b847cSJacob Keller kthread_queue_work(pf->ptp.kworker, &pf->ptp.port.tx.work); 2155ea9b847cSJacob Keller } 2156ea9b847cSJacob Keller 2157ea9b847cSJacob Keller /** 2158ea9b847cSJacob Keller * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps 2159ea9b847cSJacob Keller * @tx: Tx tracking structure to initialize 2160ea9b847cSJacob Keller * 2161ea9b847cSJacob Keller * Assumes that the length has already been initialized. Do not call directly, 2162ea9b847cSJacob Keller * use the ice_ptp_init_tx_e822 or ice_ptp_init_tx_e810 instead. 2163ea9b847cSJacob Keller */ 2164ea9b847cSJacob Keller static int 2165ea9b847cSJacob Keller ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx) 2166ea9b847cSJacob Keller { 2167ea9b847cSJacob Keller tx->tstamps = kcalloc(tx->len, sizeof(*tx->tstamps), GFP_KERNEL); 2168ea9b847cSJacob Keller if (!tx->tstamps) 2169ea9b847cSJacob Keller return -ENOMEM; 2170ea9b847cSJacob Keller 2171ea9b847cSJacob Keller tx->in_use = bitmap_zalloc(tx->len, GFP_KERNEL); 2172ea9b847cSJacob Keller if (!tx->in_use) { 2173ea9b847cSJacob Keller kfree(tx->tstamps); 2174ea9b847cSJacob Keller tx->tstamps = NULL; 2175ea9b847cSJacob Keller return -ENOMEM; 2176ea9b847cSJacob Keller } 2177ea9b847cSJacob Keller 2178ea9b847cSJacob Keller spin_lock_init(&tx->lock); 2179ea9b847cSJacob Keller kthread_init_work(&tx->work, ice_ptp_tx_tstamp_work); 2180ea9b847cSJacob Keller 2181ea9b847cSJacob Keller tx->init = 1; 2182ea9b847cSJacob Keller 2183ea9b847cSJacob Keller return 0; 2184ea9b847cSJacob Keller } 2185ea9b847cSJacob Keller 2186ea9b847cSJacob Keller /** 2187ea9b847cSJacob Keller * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker 2188ea9b847cSJacob Keller * @pf: Board private structure 2189ea9b847cSJacob Keller * @tx: the tracker to flush 2190ea9b847cSJacob Keller */ 2191ea9b847cSJacob Keller static void 2192ea9b847cSJacob Keller ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 2193ea9b847cSJacob Keller { 2194ea9b847cSJacob Keller u8 idx; 2195ea9b847cSJacob Keller 2196ea9b847cSJacob Keller for (idx = 0; idx < tx->len; idx++) { 2197ea9b847cSJacob Keller u8 phy_idx = idx + tx->quad_offset; 2198ea9b847cSJacob Keller 21994d4a223aSJacob Keller spin_lock(&tx->lock); 2200ea9b847cSJacob Keller if (tx->tstamps[idx].skb) { 2201ea9b847cSJacob Keller dev_kfree_skb_any(tx->tstamps[idx].skb); 2202ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 2203ea9b847cSJacob Keller } 22044d4a223aSJacob Keller clear_bit(idx, tx->in_use); 22054dd0d5c3SJacob Keller spin_unlock(&tx->lock); 22064d4a223aSJacob Keller 22074d4a223aSJacob Keller /* Clear any potential residual timestamp in the PHY block */ 22084d4a223aSJacob Keller if (!pf->hw.reset_ongoing) 22094d4a223aSJacob Keller ice_clear_phy_tstamp(&pf->hw, tx->quad, phy_idx); 22104d4a223aSJacob Keller } 2211ea9b847cSJacob Keller } 2212ea9b847cSJacob Keller 2213ea9b847cSJacob Keller /** 2214ea9b847cSJacob Keller * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker 2215ea9b847cSJacob Keller * @pf: Board private structure 2216ea9b847cSJacob Keller * @tx: Tx tracking structure to release 2217ea9b847cSJacob Keller * 2218ea9b847cSJacob Keller * Free memory associated with the Tx timestamp tracker. 2219ea9b847cSJacob Keller */ 2220ea9b847cSJacob Keller static void 2221ea9b847cSJacob Keller ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 2222ea9b847cSJacob Keller { 2223ea9b847cSJacob Keller tx->init = 0; 2224ea9b847cSJacob Keller 2225ea9b847cSJacob Keller kthread_cancel_work_sync(&tx->work); 2226ea9b847cSJacob Keller 2227ea9b847cSJacob Keller ice_ptp_flush_tx_tracker(pf, tx); 2228ea9b847cSJacob Keller 2229ea9b847cSJacob Keller kfree(tx->tstamps); 2230ea9b847cSJacob Keller tx->tstamps = NULL; 2231ea9b847cSJacob Keller 2232*0dbc4162SChristophe JAILLET bitmap_free(tx->in_use); 2233ea9b847cSJacob Keller tx->in_use = NULL; 2234ea9b847cSJacob Keller 2235ea9b847cSJacob Keller tx->len = 0; 2236ea9b847cSJacob Keller } 2237ea9b847cSJacob Keller 2238ea9b847cSJacob Keller /** 22393a749623SJacob Keller * ice_ptp_init_tx_e822 - Initialize tracking for Tx timestamps 22403a749623SJacob Keller * @pf: Board private structure 22413a749623SJacob Keller * @tx: the Tx tracking structure to initialize 22423a749623SJacob Keller * @port: the port this structure tracks 22433a749623SJacob Keller * 22443a749623SJacob Keller * Initialize the Tx timestamp tracker for this port. For generic MAC devices, 22453a749623SJacob Keller * the timestamp block is shared for all ports in the same quad. To avoid 22463a749623SJacob Keller * ports using the same timestamp index, logically break the block of 22473a749623SJacob Keller * registers into chunks based on the port number. 22483a749623SJacob Keller */ 22493a749623SJacob Keller static int 22503a749623SJacob Keller ice_ptp_init_tx_e822(struct ice_pf *pf, struct ice_ptp_tx *tx, u8 port) 22513a749623SJacob Keller { 22523a749623SJacob Keller tx->quad = port / ICE_PORTS_PER_QUAD; 22533a749623SJacob Keller tx->quad_offset = tx->quad * INDEX_PER_PORT; 22543a749623SJacob Keller tx->len = INDEX_PER_PORT; 22553a749623SJacob Keller 22563a749623SJacob Keller return ice_ptp_alloc_tx_tracker(tx); 22573a749623SJacob Keller } 22583a749623SJacob Keller 22593a749623SJacob Keller /** 2260ea9b847cSJacob Keller * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps 2261ea9b847cSJacob Keller * @pf: Board private structure 2262ea9b847cSJacob Keller * @tx: the Tx tracking structure to initialize 2263ea9b847cSJacob Keller * 2264ea9b847cSJacob Keller * Initialize the Tx timestamp tracker for this PF. For E810 devices, each 2265ea9b847cSJacob Keller * port has its own block of timestamps, independent of the other ports. 2266ea9b847cSJacob Keller */ 2267ea9b847cSJacob Keller static int 2268ea9b847cSJacob Keller ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx) 2269ea9b847cSJacob Keller { 2270ea9b847cSJacob Keller tx->quad = pf->hw.port_info->lport; 2271ea9b847cSJacob Keller tx->quad_offset = 0; 2272ea9b847cSJacob Keller tx->len = INDEX_PER_QUAD; 2273ea9b847cSJacob Keller 2274ea9b847cSJacob Keller return ice_ptp_alloc_tx_tracker(tx); 2275ea9b847cSJacob Keller } 2276ea9b847cSJacob Keller 2277ea9b847cSJacob Keller /** 2278ea9b847cSJacob Keller * ice_ptp_tx_tstamp_cleanup - Cleanup old timestamp requests that got dropped 2279ea9b847cSJacob Keller * @tx: PTP Tx tracker to clean up 2280ea9b847cSJacob Keller * 2281ea9b847cSJacob Keller * Loop through the Tx timestamp requests and see if any of them have been 2282ea9b847cSJacob Keller * waiting for a long time. Discard any SKBs that have been waiting for more 2283ea9b847cSJacob Keller * than 2 seconds. This is long enough to be reasonably sure that the 2284ea9b847cSJacob Keller * timestamp will never be captured. This might happen if the packet gets 2285ea9b847cSJacob Keller * discarded before it reaches the PHY timestamping block. 2286ea9b847cSJacob Keller */ 2287ea9b847cSJacob Keller static void ice_ptp_tx_tstamp_cleanup(struct ice_ptp_tx *tx) 2288ea9b847cSJacob Keller { 2289ea9b847cSJacob Keller u8 idx; 2290ea9b847cSJacob Keller 2291ea9b847cSJacob Keller if (!tx->init) 2292ea9b847cSJacob Keller return; 2293ea9b847cSJacob Keller 2294ea9b847cSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 2295ea9b847cSJacob Keller struct sk_buff *skb; 2296ea9b847cSJacob Keller 2297ea9b847cSJacob Keller /* Check if this SKB has been waiting for too long */ 2298ea9b847cSJacob Keller if (time_is_after_jiffies(tx->tstamps[idx].start + 2 * HZ)) 2299ea9b847cSJacob Keller continue; 2300ea9b847cSJacob Keller 2301ea9b847cSJacob Keller spin_lock(&tx->lock); 2302ea9b847cSJacob Keller skb = tx->tstamps[idx].skb; 2303ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 2304ea9b847cSJacob Keller clear_bit(idx, tx->in_use); 2305ea9b847cSJacob Keller spin_unlock(&tx->lock); 2306ea9b847cSJacob Keller 2307ea9b847cSJacob Keller /* Free the SKB after we've cleared the bit */ 2308ea9b847cSJacob Keller dev_kfree_skb_any(skb); 2309ea9b847cSJacob Keller } 2310ea9b847cSJacob Keller } 2311ea9b847cSJacob Keller 231277a78115SJacob Keller static void ice_ptp_periodic_work(struct kthread_work *work) 231377a78115SJacob Keller { 231477a78115SJacob Keller struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work); 231577a78115SJacob Keller struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 231677a78115SJacob Keller 231777a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 231877a78115SJacob Keller return; 231977a78115SJacob Keller 232077a78115SJacob Keller ice_ptp_update_cached_phctime(pf); 232177a78115SJacob Keller 2322ea9b847cSJacob Keller ice_ptp_tx_tstamp_cleanup(&pf->ptp.port.tx); 2323ea9b847cSJacob Keller 232477a78115SJacob Keller /* Run twice a second */ 232577a78115SJacob Keller kthread_queue_delayed_work(ptp->kworker, &ptp->work, 232677a78115SJacob Keller msecs_to_jiffies(500)); 232777a78115SJacob Keller } 232877a78115SJacob Keller 232906c16d89SJacob Keller /** 233048096710SKarol Kolacinski * ice_ptp_reset - Initialize PTP hardware clock support after reset 233148096710SKarol Kolacinski * @pf: Board private structure 233248096710SKarol Kolacinski */ 233348096710SKarol Kolacinski void ice_ptp_reset(struct ice_pf *pf) 233448096710SKarol Kolacinski { 233548096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 233648096710SKarol Kolacinski struct ice_hw *hw = &pf->hw; 233748096710SKarol Kolacinski struct timespec64 ts; 23383a749623SJacob Keller int err, itr = 1; 233948096710SKarol Kolacinski u64 time_diff; 234048096710SKarol Kolacinski 234148096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state)) 234248096710SKarol Kolacinski goto pfr; 234348096710SKarol Kolacinski 2344b2ee7256SJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned) 23453a749623SJacob Keller goto reset_ts; 234648096710SKarol Kolacinski 2347b2ee7256SJacob Keller err = ice_ptp_init_phc(hw); 234848096710SKarol Kolacinski if (err) 234948096710SKarol Kolacinski goto err; 235048096710SKarol Kolacinski 235148096710SKarol Kolacinski /* Acquire the global hardware lock */ 235248096710SKarol Kolacinski if (!ice_ptp_lock(hw)) { 235348096710SKarol Kolacinski err = -EBUSY; 235448096710SKarol Kolacinski goto err; 235548096710SKarol Kolacinski } 235648096710SKarol Kolacinski 235748096710SKarol Kolacinski /* Write the increment time value to PHY and LAN */ 235878267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf)); 235948096710SKarol Kolacinski if (err) { 236048096710SKarol Kolacinski ice_ptp_unlock(hw); 236148096710SKarol Kolacinski goto err; 236248096710SKarol Kolacinski } 236348096710SKarol Kolacinski 236448096710SKarol Kolacinski /* Write the initial Time value to PHY and LAN using the cached PHC 236548096710SKarol Kolacinski * time before the reset and time difference between stopping and 236648096710SKarol Kolacinski * starting the clock. 236748096710SKarol Kolacinski */ 236848096710SKarol Kolacinski if (ptp->cached_phc_time) { 236948096710SKarol Kolacinski time_diff = ktime_get_real_ns() - ptp->reset_time; 237048096710SKarol Kolacinski ts = ns_to_timespec64(ptp->cached_phc_time + time_diff); 237148096710SKarol Kolacinski } else { 237248096710SKarol Kolacinski ts = ktime_to_timespec64(ktime_get_real()); 237348096710SKarol Kolacinski } 237448096710SKarol Kolacinski err = ice_ptp_write_init(pf, &ts); 237548096710SKarol Kolacinski if (err) { 237648096710SKarol Kolacinski ice_ptp_unlock(hw); 237748096710SKarol Kolacinski goto err; 237848096710SKarol Kolacinski } 237948096710SKarol Kolacinski 238048096710SKarol Kolacinski /* Release the global hardware lock */ 238148096710SKarol Kolacinski ice_ptp_unlock(hw); 238248096710SKarol Kolacinski 23833a749623SJacob Keller if (!ice_is_e810(hw)) { 23843a749623SJacob Keller /* Enable quad interrupts */ 23853a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr); 23863a749623SJacob Keller if (err) 23873a749623SJacob Keller goto err; 23883a749623SJacob Keller } 23893a749623SJacob Keller 23903a749623SJacob Keller reset_ts: 23913a749623SJacob Keller /* Restart the PHY timestamping block */ 23923a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf); 23933a749623SJacob Keller 239448096710SKarol Kolacinski pfr: 239548096710SKarol Kolacinski /* Init Tx structures */ 2396a69f1cb6SJacob Keller if (ice_is_e810(&pf->hw)) { 239748096710SKarol Kolacinski err = ice_ptp_init_tx_e810(pf, &ptp->port.tx); 2398a69f1cb6SJacob Keller } else { 2399a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp->port.ov_work, 2400a69f1cb6SJacob Keller ice_ptp_wait_for_offset_valid); 24013a749623SJacob Keller err = ice_ptp_init_tx_e822(pf, &ptp->port.tx, 24023a749623SJacob Keller ptp->port.port_num); 2403a69f1cb6SJacob Keller } 240448096710SKarol Kolacinski if (err) 240548096710SKarol Kolacinski goto err; 240648096710SKarol Kolacinski 240748096710SKarol Kolacinski set_bit(ICE_FLAG_PTP, pf->flags); 240848096710SKarol Kolacinski 240948096710SKarol Kolacinski /* Start periodic work going */ 241048096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0); 241148096710SKarol Kolacinski 241248096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP reset successful\n"); 241348096710SKarol Kolacinski return; 241448096710SKarol Kolacinski 241548096710SKarol Kolacinski err: 241648096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP reset failed %d\n", err); 241748096710SKarol Kolacinski } 241848096710SKarol Kolacinski 241948096710SKarol Kolacinski /** 242048096710SKarol Kolacinski * ice_ptp_prepare_for_reset - Prepare PTP for reset 242148096710SKarol Kolacinski * @pf: Board private structure 242248096710SKarol Kolacinski */ 242348096710SKarol Kolacinski void ice_ptp_prepare_for_reset(struct ice_pf *pf) 242448096710SKarol Kolacinski { 242548096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 242648096710SKarol Kolacinski u8 src_tmr; 242748096710SKarol Kolacinski 242848096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags); 242948096710SKarol Kolacinski 243048096710SKarol Kolacinski /* Disable timestamping for both Tx and Rx */ 243148096710SKarol Kolacinski ice_ptp_cfg_timestamp(pf, false); 243248096710SKarol Kolacinski 243348096710SKarol Kolacinski kthread_cancel_delayed_work_sync(&ptp->work); 243448096710SKarol Kolacinski kthread_cancel_work_sync(&ptp->extts_work); 243548096710SKarol Kolacinski 243648096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state)) 243748096710SKarol Kolacinski return; 243848096710SKarol Kolacinski 243948096710SKarol Kolacinski ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 244048096710SKarol Kolacinski 244148096710SKarol Kolacinski /* Disable periodic outputs */ 244248096710SKarol Kolacinski ice_ptp_disable_all_clkout(pf); 244348096710SKarol Kolacinski 244448096710SKarol Kolacinski src_tmr = ice_get_ptp_src_clock_index(&pf->hw); 244548096710SKarol Kolacinski 244648096710SKarol Kolacinski /* Disable source clock */ 244748096710SKarol Kolacinski wr32(&pf->hw, GLTSYN_ENA(src_tmr), (u32)~GLTSYN_ENA_TSYN_ENA_M); 244848096710SKarol Kolacinski 244948096710SKarol Kolacinski /* Acquire PHC and system timer to restore after reset */ 245048096710SKarol Kolacinski ptp->reset_time = ktime_get_real_ns(); 245148096710SKarol Kolacinski } 245248096710SKarol Kolacinski 245348096710SKarol Kolacinski /** 245406c16d89SJacob Keller * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device 245506c16d89SJacob Keller * @pf: Board private structure 245606c16d89SJacob Keller * 245706c16d89SJacob Keller * Setup and initialize a PTP clock device that represents the device hardware 245806c16d89SJacob Keller * clock. Save the clock index for other functions connected to the same 245906c16d89SJacob Keller * hardware resource. 246006c16d89SJacob Keller */ 246106c16d89SJacob Keller static int ice_ptp_init_owner(struct ice_pf *pf) 246206c16d89SJacob Keller { 246306c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 246406c16d89SJacob Keller struct timespec64 ts; 24653a749623SJacob Keller int err, itr = 1; 246606c16d89SJacob Keller 2467b2ee7256SJacob Keller err = ice_ptp_init_phc(hw); 2468b2ee7256SJacob Keller if (err) { 2469b2ee7256SJacob Keller dev_err(ice_pf_to_dev(pf), "Failed to initialize PHC, err %d\n", 2470b2ee7256SJacob Keller err); 2471b2ee7256SJacob Keller return err; 2472b2ee7256SJacob Keller } 247306c16d89SJacob Keller 247406c16d89SJacob Keller /* Acquire the global hardware lock */ 247506c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 247606c16d89SJacob Keller err = -EBUSY; 247706c16d89SJacob Keller goto err_exit; 247806c16d89SJacob Keller } 247906c16d89SJacob Keller 248006c16d89SJacob Keller /* Write the increment time value to PHY and LAN */ 248178267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf)); 248206c16d89SJacob Keller if (err) { 248306c16d89SJacob Keller ice_ptp_unlock(hw); 248406c16d89SJacob Keller goto err_exit; 248506c16d89SJacob Keller } 248606c16d89SJacob Keller 248706c16d89SJacob Keller ts = ktime_to_timespec64(ktime_get_real()); 248806c16d89SJacob Keller /* Write the initial Time value to PHY and LAN */ 248906c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts); 249006c16d89SJacob Keller if (err) { 249106c16d89SJacob Keller ice_ptp_unlock(hw); 249206c16d89SJacob Keller goto err_exit; 249306c16d89SJacob Keller } 249406c16d89SJacob Keller 249506c16d89SJacob Keller /* Release the global hardware lock */ 249606c16d89SJacob Keller ice_ptp_unlock(hw); 249706c16d89SJacob Keller 24983a749623SJacob Keller if (!ice_is_e810(hw)) { 24993a749623SJacob Keller /* Enable quad interrupts */ 25003a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr); 25013a749623SJacob Keller if (err) 25023a749623SJacob Keller goto err_exit; 25033a749623SJacob Keller } 25043a749623SJacob Keller 250506c16d89SJacob Keller /* Ensure we have a clock device */ 250606c16d89SJacob Keller err = ice_ptp_create_clock(pf); 250706c16d89SJacob Keller if (err) 250806c16d89SJacob Keller goto err_clk; 250906c16d89SJacob Keller 251067569a7fSJacob Keller /* Store the PTP clock index for other PFs */ 251167569a7fSJacob Keller ice_set_ptp_clock_index(pf); 251267569a7fSJacob Keller 251306c16d89SJacob Keller return 0; 251406c16d89SJacob Keller 251506c16d89SJacob Keller err_clk: 251606c16d89SJacob Keller pf->ptp.clock = NULL; 251706c16d89SJacob Keller err_exit: 251806c16d89SJacob Keller return err; 251906c16d89SJacob Keller } 252006c16d89SJacob Keller 252106c16d89SJacob Keller /** 252248096710SKarol Kolacinski * ice_ptp_init_work - Initialize PTP work threads 252348096710SKarol Kolacinski * @pf: Board private structure 252448096710SKarol Kolacinski * @ptp: PF PTP structure 252548096710SKarol Kolacinski */ 252648096710SKarol Kolacinski static int ice_ptp_init_work(struct ice_pf *pf, struct ice_ptp *ptp) 252748096710SKarol Kolacinski { 252848096710SKarol Kolacinski struct kthread_worker *kworker; 252948096710SKarol Kolacinski 253048096710SKarol Kolacinski /* Initialize work functions */ 253148096710SKarol Kolacinski kthread_init_delayed_work(&ptp->work, ice_ptp_periodic_work); 253248096710SKarol Kolacinski kthread_init_work(&ptp->extts_work, ice_ptp_extts_work); 253348096710SKarol Kolacinski 253448096710SKarol Kolacinski /* Allocate a kworker for handling work required for the ports 253548096710SKarol Kolacinski * connected to the PTP hardware clock. 253648096710SKarol Kolacinski */ 253748096710SKarol Kolacinski kworker = kthread_create_worker(0, "ice-ptp-%s", 253848096710SKarol Kolacinski dev_name(ice_pf_to_dev(pf))); 253948096710SKarol Kolacinski if (IS_ERR(kworker)) 254048096710SKarol Kolacinski return PTR_ERR(kworker); 254148096710SKarol Kolacinski 254248096710SKarol Kolacinski ptp->kworker = kworker; 254348096710SKarol Kolacinski 254448096710SKarol Kolacinski /* Start periodic work going */ 254548096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0); 254648096710SKarol Kolacinski 254748096710SKarol Kolacinski return 0; 254848096710SKarol Kolacinski } 254948096710SKarol Kolacinski 255048096710SKarol Kolacinski /** 25513a749623SJacob Keller * ice_ptp_init_port - Initialize PTP port structure 25523a749623SJacob Keller * @pf: Board private structure 25533a749623SJacob Keller * @ptp_port: PTP port structure 25543a749623SJacob Keller */ 25553a749623SJacob Keller static int ice_ptp_init_port(struct ice_pf *pf, struct ice_ptp_port *ptp_port) 25563a749623SJacob Keller { 25573a749623SJacob Keller mutex_init(&ptp_port->ps_lock); 25583a749623SJacob Keller 25593a749623SJacob Keller if (ice_is_e810(&pf->hw)) 25603a749623SJacob Keller return ice_ptp_init_tx_e810(pf, &ptp_port->tx); 25613a749623SJacob Keller 2562a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp_port->ov_work, 2563a69f1cb6SJacob Keller ice_ptp_wait_for_offset_valid); 25643a749623SJacob Keller return ice_ptp_init_tx_e822(pf, &ptp_port->tx, ptp_port->port_num); 25653a749623SJacob Keller } 25663a749623SJacob Keller 25673a749623SJacob Keller /** 2568b2ee7256SJacob Keller * ice_ptp_init - Initialize PTP hardware clock support 256906c16d89SJacob Keller * @pf: Board private structure 257006c16d89SJacob Keller * 2571b2ee7256SJacob Keller * Set up the device for interacting with the PTP hardware clock for all 2572b2ee7256SJacob Keller * functions, both the function that owns the clock hardware, and the 2573b2ee7256SJacob Keller * functions connected to the clock hardware. 2574b2ee7256SJacob Keller * 2575b2ee7256SJacob Keller * The clock owner will allocate and register a ptp_clock with the 2576b2ee7256SJacob Keller * PTP_1588_CLOCK infrastructure. All functions allocate a kthread and work 2577b2ee7256SJacob Keller * items used for asynchronous work such as Tx timestamps and periodic work. 257806c16d89SJacob Keller */ 257906c16d89SJacob Keller void ice_ptp_init(struct ice_pf *pf) 258006c16d89SJacob Keller { 258148096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 258206c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 258306c16d89SJacob Keller int err; 258406c16d89SJacob Keller 2585b2ee7256SJacob Keller /* If this function owns the clock hardware, it must allocate and 2586b2ee7256SJacob Keller * configure the PTP clock device to represent it. 2587b2ee7256SJacob Keller */ 258806c16d89SJacob Keller if (hw->func_caps.ts_func_info.src_tmr_owned) { 258906c16d89SJacob Keller err = ice_ptp_init_owner(pf); 259006c16d89SJacob Keller if (err) 259148096710SKarol Kolacinski goto err; 259206c16d89SJacob Keller } 259306c16d89SJacob Keller 25943a749623SJacob Keller ptp->port.port_num = hw->pf_id; 25953a749623SJacob Keller err = ice_ptp_init_port(pf, &ptp->port); 259648096710SKarol Kolacinski if (err) 259748096710SKarol Kolacinski goto err; 259877a78115SJacob Keller 25993a749623SJacob Keller /* Start the PHY timestamping block */ 26003a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf); 26013a749623SJacob Keller 260206c16d89SJacob Keller set_bit(ICE_FLAG_PTP, pf->flags); 260348096710SKarol Kolacinski err = ice_ptp_init_work(pf, ptp); 260448096710SKarol Kolacinski if (err) 260548096710SKarol Kolacinski goto err; 260606c16d89SJacob Keller 260748096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP init successful\n"); 260877a78115SJacob Keller return; 260977a78115SJacob Keller 261048096710SKarol Kolacinski err: 261177a78115SJacob Keller /* If we registered a PTP clock, release it */ 261277a78115SJacob Keller if (pf->ptp.clock) { 261348096710SKarol Kolacinski ptp_clock_unregister(ptp->clock); 261477a78115SJacob Keller pf->ptp.clock = NULL; 261577a78115SJacob Keller } 261648096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags); 261748096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP failed %d\n", err); 261806c16d89SJacob Keller } 261906c16d89SJacob Keller 262006c16d89SJacob Keller /** 262106c16d89SJacob Keller * ice_ptp_release - Disable the driver/HW support and unregister the clock 262206c16d89SJacob Keller * @pf: Board private structure 262306c16d89SJacob Keller * 262406c16d89SJacob Keller * This function handles the cleanup work required from the initialization by 262506c16d89SJacob Keller * clearing out the important information and unregistering the clock 262606c16d89SJacob Keller */ 262706c16d89SJacob Keller void ice_ptp_release(struct ice_pf *pf) 262806c16d89SJacob Keller { 2629fd1b5bebSYongxin Liu if (!test_bit(ICE_FLAG_PTP, pf->flags)) 2630fd1b5bebSYongxin Liu return; 2631fd1b5bebSYongxin Liu 263277a78115SJacob Keller /* Disable timestamping for both Tx and Rx */ 263377a78115SJacob Keller ice_ptp_cfg_timestamp(pf, false); 263477a78115SJacob Keller 2635ea9b847cSJacob Keller ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 2636ea9b847cSJacob Keller 263706c16d89SJacob Keller clear_bit(ICE_FLAG_PTP, pf->flags); 263806c16d89SJacob Keller 263977a78115SJacob Keller kthread_cancel_delayed_work_sync(&pf->ptp.work); 264077a78115SJacob Keller 26413a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port); 26423a749623SJacob Keller mutex_destroy(&pf->ptp.port.ps_lock); 264377a78115SJacob Keller if (pf->ptp.kworker) { 264477a78115SJacob Keller kthread_destroy_worker(pf->ptp.kworker); 264577a78115SJacob Keller pf->ptp.kworker = NULL; 264677a78115SJacob Keller } 264777a78115SJacob Keller 264806c16d89SJacob Keller if (!pf->ptp.clock) 264906c16d89SJacob Keller return; 265006c16d89SJacob Keller 26519ee31343SJacob Keller /* Disable periodic outputs */ 26529ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 26539ee31343SJacob Keller 265467569a7fSJacob Keller ice_clear_ptp_clock_index(pf); 265506c16d89SJacob Keller ptp_clock_unregister(pf->ptp.clock); 265606c16d89SJacob Keller pf->ptp.clock = NULL; 265706c16d89SJacob Keller 265806c16d89SJacob Keller dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n"); 265906c16d89SJacob Keller } 2660