106c16d89SJacob Keller // SPDX-License-Identifier: GPL-2.0 206c16d89SJacob Keller /* Copyright (C) 2021, Intel Corporation. */ 306c16d89SJacob Keller 406c16d89SJacob Keller #include "ice.h" 506c16d89SJacob Keller #include "ice_lib.h" 64c120218SJacob Keller #include "ice_trace.h" 706c16d89SJacob Keller 8172db5f9SMaciej Machnikowski #define E810_OUT_PROP_DELAY_NS 1 9172db5f9SMaciej Machnikowski 103a749623SJacob Keller #define UNKNOWN_INCVAL_E822 0x100000000ULL 113a749623SJacob Keller 12325b2064SMaciej Machnikowski static const struct ptp_pin_desc ice_pin_desc_e810t[] = { 13325b2064SMaciej Machnikowski /* name idx func chan */ 14325b2064SMaciej Machnikowski { "GNSS", GNSS, PTP_PF_EXTTS, 0, { 0, } }, 15325b2064SMaciej Machnikowski { "SMA1", SMA1, PTP_PF_NONE, 1, { 0, } }, 16325b2064SMaciej Machnikowski { "U.FL1", UFL1, PTP_PF_NONE, 1, { 0, } }, 17325b2064SMaciej Machnikowski { "SMA2", SMA2, PTP_PF_NONE, 2, { 0, } }, 18325b2064SMaciej Machnikowski { "U.FL2", UFL2, PTP_PF_NONE, 2, { 0, } }, 19325b2064SMaciej Machnikowski }; 20325b2064SMaciej Machnikowski 21325b2064SMaciej Machnikowski /** 22325b2064SMaciej Machnikowski * ice_get_sma_config_e810t 23325b2064SMaciej Machnikowski * @hw: pointer to the hw struct 24325b2064SMaciej Machnikowski * @ptp_pins: pointer to the ptp_pin_desc struture 25325b2064SMaciej Machnikowski * 26325b2064SMaciej Machnikowski * Read the configuration of the SMA control logic and put it into the 27325b2064SMaciej Machnikowski * ptp_pin_desc structure 28325b2064SMaciej Machnikowski */ 29325b2064SMaciej Machnikowski static int 30325b2064SMaciej Machnikowski ice_get_sma_config_e810t(struct ice_hw *hw, struct ptp_pin_desc *ptp_pins) 31325b2064SMaciej Machnikowski { 32325b2064SMaciej Machnikowski u8 data, i; 33325b2064SMaciej Machnikowski int status; 34325b2064SMaciej Machnikowski 35325b2064SMaciej Machnikowski /* Read initial pin state */ 36325b2064SMaciej Machnikowski status = ice_read_sma_ctrl_e810t(hw, &data); 37325b2064SMaciej Machnikowski if (status) 38325b2064SMaciej Machnikowski return status; 39325b2064SMaciej Machnikowski 40325b2064SMaciej Machnikowski /* initialize with defaults */ 41325b2064SMaciej Machnikowski for (i = 0; i < NUM_PTP_PINS_E810T; i++) { 42325b2064SMaciej Machnikowski snprintf(ptp_pins[i].name, sizeof(ptp_pins[i].name), 43325b2064SMaciej Machnikowski "%s", ice_pin_desc_e810t[i].name); 44325b2064SMaciej Machnikowski ptp_pins[i].index = ice_pin_desc_e810t[i].index; 45325b2064SMaciej Machnikowski ptp_pins[i].func = ice_pin_desc_e810t[i].func; 46325b2064SMaciej Machnikowski ptp_pins[i].chan = ice_pin_desc_e810t[i].chan; 47325b2064SMaciej Machnikowski } 48325b2064SMaciej Machnikowski 49325b2064SMaciej Machnikowski /* Parse SMA1/UFL1 */ 50325b2064SMaciej Machnikowski switch (data & ICE_SMA1_MASK_E810T) { 51325b2064SMaciej Machnikowski case ICE_SMA1_MASK_E810T: 52325b2064SMaciej Machnikowski default: 53325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_NONE; 54325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 55325b2064SMaciej Machnikowski break; 56325b2064SMaciej Machnikowski case ICE_SMA1_DIR_EN_E810T: 57325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_PEROUT; 58325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 59325b2064SMaciej Machnikowski break; 60325b2064SMaciej Machnikowski case ICE_SMA1_TX_EN_E810T: 61325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_EXTTS; 62325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 63325b2064SMaciej Machnikowski break; 64325b2064SMaciej Machnikowski case 0: 65325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_EXTTS; 66325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_PEROUT; 67325b2064SMaciej Machnikowski break; 68325b2064SMaciej Machnikowski } 69325b2064SMaciej Machnikowski 70325b2064SMaciej Machnikowski /* Parse SMA2/UFL2 */ 71325b2064SMaciej Machnikowski switch (data & ICE_SMA2_MASK_E810T) { 72325b2064SMaciej Machnikowski case ICE_SMA2_MASK_E810T: 73325b2064SMaciej Machnikowski default: 74325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE; 75325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 76325b2064SMaciej Machnikowski break; 77325b2064SMaciej Machnikowski case (ICE_SMA2_TX_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T): 78325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_EXTTS; 79325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 80325b2064SMaciej Machnikowski break; 81325b2064SMaciej Machnikowski case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_UFL2_RX_DIS_E810T): 82325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_PEROUT; 83325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 84325b2064SMaciej Machnikowski break; 85325b2064SMaciej Machnikowski case (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T): 86325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE; 87325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_EXTTS; 88325b2064SMaciej Machnikowski break; 89325b2064SMaciej Machnikowski case ICE_SMA2_DIR_EN_E810T: 90325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_PEROUT; 91325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_EXTTS; 92325b2064SMaciej Machnikowski break; 93325b2064SMaciej Machnikowski } 94325b2064SMaciej Machnikowski 95325b2064SMaciej Machnikowski return 0; 96325b2064SMaciej Machnikowski } 97325b2064SMaciej Machnikowski 98325b2064SMaciej Machnikowski /** 99325b2064SMaciej Machnikowski * ice_ptp_set_sma_config_e810t 100325b2064SMaciej Machnikowski * @hw: pointer to the hw struct 101325b2064SMaciej Machnikowski * @ptp_pins: pointer to the ptp_pin_desc struture 102325b2064SMaciej Machnikowski * 103325b2064SMaciej Machnikowski * Set the configuration of the SMA control logic based on the configuration in 104325b2064SMaciej Machnikowski * num_pins parameter 105325b2064SMaciej Machnikowski */ 106325b2064SMaciej Machnikowski static int 107325b2064SMaciej Machnikowski ice_ptp_set_sma_config_e810t(struct ice_hw *hw, 108325b2064SMaciej Machnikowski const struct ptp_pin_desc *ptp_pins) 109325b2064SMaciej Machnikowski { 110325b2064SMaciej Machnikowski int status; 111325b2064SMaciej Machnikowski u8 data; 112325b2064SMaciej Machnikowski 113325b2064SMaciej Machnikowski /* SMA1 and UFL1 cannot be set to TX at the same time */ 114325b2064SMaciej Machnikowski if (ptp_pins[SMA1].func == PTP_PF_PEROUT && 115325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT) 116325b2064SMaciej Machnikowski return -EINVAL; 117325b2064SMaciej Machnikowski 118325b2064SMaciej Machnikowski /* SMA2 and UFL2 cannot be set to RX at the same time */ 119325b2064SMaciej Machnikowski if (ptp_pins[SMA2].func == PTP_PF_EXTTS && 120325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS) 121325b2064SMaciej Machnikowski return -EINVAL; 122325b2064SMaciej Machnikowski 123325b2064SMaciej Machnikowski /* Read initial pin state value */ 124325b2064SMaciej Machnikowski status = ice_read_sma_ctrl_e810t(hw, &data); 125325b2064SMaciej Machnikowski if (status) 126325b2064SMaciej Machnikowski return status; 127325b2064SMaciej Machnikowski 128325b2064SMaciej Machnikowski /* Set the right sate based on the desired configuration */ 129325b2064SMaciej Machnikowski data &= ~ICE_SMA1_MASK_E810T; 130325b2064SMaciej Machnikowski if (ptp_pins[SMA1].func == PTP_PF_NONE && 131325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) { 132325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 + U.FL1 disabled"); 133325b2064SMaciej Machnikowski data |= ICE_SMA1_MASK_E810T; 134325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_EXTTS && 135325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) { 136325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX"); 137325b2064SMaciej Machnikowski data |= ICE_SMA1_TX_EN_E810T; 138325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_NONE && 139325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT) { 140325b2064SMaciej Machnikowski /* U.FL 1 TX will always enable SMA 1 RX */ 141325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX"); 142325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_EXTTS && 143325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_PEROUT) { 144325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 RX + U.FL1 TX"); 145325b2064SMaciej Machnikowski } else if (ptp_pins[SMA1].func == PTP_PF_PEROUT && 146325b2064SMaciej Machnikowski ptp_pins[UFL1].func == PTP_PF_NONE) { 147325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA1 TX"); 148325b2064SMaciej Machnikowski data |= ICE_SMA1_DIR_EN_E810T; 149325b2064SMaciej Machnikowski } 150325b2064SMaciej Machnikowski 151325b2064SMaciej Machnikowski data &= ~ICE_SMA2_MASK_E810T; 152325b2064SMaciej Machnikowski if (ptp_pins[SMA2].func == PTP_PF_NONE && 153325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) { 154325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 + U.FL2 disabled"); 155325b2064SMaciej Machnikowski data |= ICE_SMA2_MASK_E810T; 156325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_EXTTS && 157325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) { 158325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 RX"); 159325b2064SMaciej Machnikowski data |= (ICE_SMA2_TX_EN_E810T | 160325b2064SMaciej Machnikowski ICE_SMA2_UFL2_RX_DIS_E810T); 161325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_NONE && 162325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS) { 163325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "UFL2 RX"); 164325b2064SMaciej Machnikowski data |= (ICE_SMA2_DIR_EN_E810T | ICE_SMA2_TX_EN_E810T); 165325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_PEROUT && 166325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_NONE) { 167325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 TX"); 168325b2064SMaciej Machnikowski data |= (ICE_SMA2_DIR_EN_E810T | 169325b2064SMaciej Machnikowski ICE_SMA2_UFL2_RX_DIS_E810T); 170325b2064SMaciej Machnikowski } else if (ptp_pins[SMA2].func == PTP_PF_PEROUT && 171325b2064SMaciej Machnikowski ptp_pins[UFL2].func == PTP_PF_EXTTS) { 172325b2064SMaciej Machnikowski dev_info(ice_hw_to_dev(hw), "SMA2 TX + U.FL2 RX"); 173325b2064SMaciej Machnikowski data |= ICE_SMA2_DIR_EN_E810T; 174325b2064SMaciej Machnikowski } 175325b2064SMaciej Machnikowski 176325b2064SMaciej Machnikowski return ice_write_sma_ctrl_e810t(hw, data); 177325b2064SMaciej Machnikowski } 178325b2064SMaciej Machnikowski 179325b2064SMaciej Machnikowski /** 180325b2064SMaciej Machnikowski * ice_ptp_set_sma_e810t 181325b2064SMaciej Machnikowski * @info: the driver's PTP info structure 182325b2064SMaciej Machnikowski * @pin: pin index in kernel structure 183325b2064SMaciej Machnikowski * @func: Pin function to be set (PTP_PF_NONE, PTP_PF_EXTTS or PTP_PF_PEROUT) 184325b2064SMaciej Machnikowski * 185325b2064SMaciej Machnikowski * Set the configuration of a single SMA pin 186325b2064SMaciej Machnikowski */ 187325b2064SMaciej Machnikowski static int 188325b2064SMaciej Machnikowski ice_ptp_set_sma_e810t(struct ptp_clock_info *info, unsigned int pin, 189325b2064SMaciej Machnikowski enum ptp_pin_function func) 190325b2064SMaciej Machnikowski { 191325b2064SMaciej Machnikowski struct ptp_pin_desc ptp_pins[NUM_PTP_PINS_E810T]; 192325b2064SMaciej Machnikowski struct ice_pf *pf = ptp_info_to_pf(info); 193325b2064SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 194325b2064SMaciej Machnikowski int err; 195325b2064SMaciej Machnikowski 196325b2064SMaciej Machnikowski if (pin < SMA1 || func > PTP_PF_PEROUT) 197325b2064SMaciej Machnikowski return -EOPNOTSUPP; 198325b2064SMaciej Machnikowski 199325b2064SMaciej Machnikowski err = ice_get_sma_config_e810t(hw, ptp_pins); 200325b2064SMaciej Machnikowski if (err) 201325b2064SMaciej Machnikowski return err; 202325b2064SMaciej Machnikowski 203325b2064SMaciej Machnikowski /* Disable the same function on the other pin sharing the channel */ 204325b2064SMaciej Machnikowski if (pin == SMA1 && ptp_pins[UFL1].func == func) 205325b2064SMaciej Machnikowski ptp_pins[UFL1].func = PTP_PF_NONE; 206325b2064SMaciej Machnikowski if (pin == UFL1 && ptp_pins[SMA1].func == func) 207325b2064SMaciej Machnikowski ptp_pins[SMA1].func = PTP_PF_NONE; 208325b2064SMaciej Machnikowski 209325b2064SMaciej Machnikowski if (pin == SMA2 && ptp_pins[UFL2].func == func) 210325b2064SMaciej Machnikowski ptp_pins[UFL2].func = PTP_PF_NONE; 211325b2064SMaciej Machnikowski if (pin == UFL2 && ptp_pins[SMA2].func == func) 212325b2064SMaciej Machnikowski ptp_pins[SMA2].func = PTP_PF_NONE; 213325b2064SMaciej Machnikowski 214325b2064SMaciej Machnikowski /* Set up new pin function in the temp table */ 215325b2064SMaciej Machnikowski ptp_pins[pin].func = func; 216325b2064SMaciej Machnikowski 217325b2064SMaciej Machnikowski return ice_ptp_set_sma_config_e810t(hw, ptp_pins); 218325b2064SMaciej Machnikowski } 219325b2064SMaciej Machnikowski 220325b2064SMaciej Machnikowski /** 221325b2064SMaciej Machnikowski * ice_verify_pin_e810t 222325b2064SMaciej Machnikowski * @info: the driver's PTP info structure 223325b2064SMaciej Machnikowski * @pin: Pin index 224325b2064SMaciej Machnikowski * @func: Assigned function 225325b2064SMaciej Machnikowski * @chan: Assigned channel 226325b2064SMaciej Machnikowski * 227325b2064SMaciej Machnikowski * Verify if pin supports requested pin function. If the Check pins consistency. 228325b2064SMaciej Machnikowski * Reconfigure the SMA logic attached to the given pin to enable its 229325b2064SMaciej Machnikowski * desired functionality 230325b2064SMaciej Machnikowski */ 231325b2064SMaciej Machnikowski static int 232325b2064SMaciej Machnikowski ice_verify_pin_e810t(struct ptp_clock_info *info, unsigned int pin, 233325b2064SMaciej Machnikowski enum ptp_pin_function func, unsigned int chan) 234325b2064SMaciej Machnikowski { 235325b2064SMaciej Machnikowski /* Don't allow channel reassignment */ 236325b2064SMaciej Machnikowski if (chan != ice_pin_desc_e810t[pin].chan) 237325b2064SMaciej Machnikowski return -EOPNOTSUPP; 238325b2064SMaciej Machnikowski 239325b2064SMaciej Machnikowski /* Check if functions are properly assigned */ 240325b2064SMaciej Machnikowski switch (func) { 241325b2064SMaciej Machnikowski case PTP_PF_NONE: 242325b2064SMaciej Machnikowski break; 243325b2064SMaciej Machnikowski case PTP_PF_EXTTS: 244325b2064SMaciej Machnikowski if (pin == UFL1) 245325b2064SMaciej Machnikowski return -EOPNOTSUPP; 246325b2064SMaciej Machnikowski break; 247325b2064SMaciej Machnikowski case PTP_PF_PEROUT: 248325b2064SMaciej Machnikowski if (pin == UFL2 || pin == GNSS) 249325b2064SMaciej Machnikowski return -EOPNOTSUPP; 250325b2064SMaciej Machnikowski break; 251325b2064SMaciej Machnikowski case PTP_PF_PHYSYNC: 252325b2064SMaciej Machnikowski return -EOPNOTSUPP; 253325b2064SMaciej Machnikowski } 254325b2064SMaciej Machnikowski 255325b2064SMaciej Machnikowski return ice_ptp_set_sma_e810t(info, pin, func); 256325b2064SMaciej Machnikowski } 257325b2064SMaciej Machnikowski 25806c16d89SJacob Keller /** 259ea9b847cSJacob Keller * ice_set_tx_tstamp - Enable or disable Tx timestamping 260ea9b847cSJacob Keller * @pf: The PF pointer to search in 261ea9b847cSJacob Keller * @on: bool value for whether timestamps are enabled or disabled 262ea9b847cSJacob Keller */ 263ea9b847cSJacob Keller static void ice_set_tx_tstamp(struct ice_pf *pf, bool on) 264ea9b847cSJacob Keller { 265ea9b847cSJacob Keller struct ice_vsi *vsi; 266ea9b847cSJacob Keller u32 val; 267ea9b847cSJacob Keller u16 i; 268ea9b847cSJacob Keller 269ea9b847cSJacob Keller vsi = ice_get_main_vsi(pf); 270ea9b847cSJacob Keller if (!vsi) 271ea9b847cSJacob Keller return; 272ea9b847cSJacob Keller 273ea9b847cSJacob Keller /* Set the timestamp enable flag for all the Tx rings */ 27484c5fb8cSJacob Keller ice_for_each_txq(vsi, i) { 275ea9b847cSJacob Keller if (!vsi->tx_rings[i]) 276ea9b847cSJacob Keller continue; 277ea9b847cSJacob Keller vsi->tx_rings[i]->ptp_tx = on; 278ea9b847cSJacob Keller } 279ea9b847cSJacob Keller 280ea9b847cSJacob Keller /* Configure the Tx timestamp interrupt */ 281ea9b847cSJacob Keller val = rd32(&pf->hw, PFINT_OICR_ENA); 282ea9b847cSJacob Keller if (on) 283ea9b847cSJacob Keller val |= PFINT_OICR_TSYN_TX_M; 284ea9b847cSJacob Keller else 285ea9b847cSJacob Keller val &= ~PFINT_OICR_TSYN_TX_M; 286ea9b847cSJacob Keller wr32(&pf->hw, PFINT_OICR_ENA, val); 287e59d75ddSJacob Keller 288e59d75ddSJacob Keller pf->ptp.tstamp_config.tx_type = on ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 289ea9b847cSJacob Keller } 290ea9b847cSJacob Keller 291ea9b847cSJacob Keller /** 29277a78115SJacob Keller * ice_set_rx_tstamp - Enable or disable Rx timestamping 29377a78115SJacob Keller * @pf: The PF pointer to search in 29477a78115SJacob Keller * @on: bool value for whether timestamps are enabled or disabled 29577a78115SJacob Keller */ 29677a78115SJacob Keller static void ice_set_rx_tstamp(struct ice_pf *pf, bool on) 29777a78115SJacob Keller { 29877a78115SJacob Keller struct ice_vsi *vsi; 29977a78115SJacob Keller u16 i; 30077a78115SJacob Keller 30177a78115SJacob Keller vsi = ice_get_main_vsi(pf); 30277a78115SJacob Keller if (!vsi) 30377a78115SJacob Keller return; 30477a78115SJacob Keller 30577a78115SJacob Keller /* Set the timestamp flag for all the Rx rings */ 30677a78115SJacob Keller ice_for_each_rxq(vsi, i) { 30777a78115SJacob Keller if (!vsi->rx_rings[i]) 30877a78115SJacob Keller continue; 30977a78115SJacob Keller vsi->rx_rings[i]->ptp_rx = on; 31077a78115SJacob Keller } 311e59d75ddSJacob Keller 312e59d75ddSJacob Keller pf->ptp.tstamp_config.rx_filter = on ? HWTSTAMP_FILTER_ALL : 313e59d75ddSJacob Keller HWTSTAMP_FILTER_NONE; 31477a78115SJacob Keller } 31577a78115SJacob Keller 31677a78115SJacob Keller /** 31777a78115SJacob Keller * ice_ptp_cfg_timestamp - Configure timestamp for init/deinit 31877a78115SJacob Keller * @pf: Board private structure 31977a78115SJacob Keller * @ena: bool value to enable or disable time stamp 32077a78115SJacob Keller * 32177a78115SJacob Keller * This function will configure timestamping during PTP initialization 32277a78115SJacob Keller * and deinitialization 32377a78115SJacob Keller */ 32448096710SKarol Kolacinski void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena) 32577a78115SJacob Keller { 326ea9b847cSJacob Keller ice_set_tx_tstamp(pf, ena); 32777a78115SJacob Keller ice_set_rx_tstamp(pf, ena); 32877a78115SJacob Keller } 32977a78115SJacob Keller 33077a78115SJacob Keller /** 33167569a7fSJacob Keller * ice_get_ptp_clock_index - Get the PTP clock index 33267569a7fSJacob Keller * @pf: the PF pointer 33367569a7fSJacob Keller * 33467569a7fSJacob Keller * Determine the clock index of the PTP clock associated with this device. If 33567569a7fSJacob Keller * this is the PF controlling the clock, just use the local access to the 33667569a7fSJacob Keller * clock device pointer. 33767569a7fSJacob Keller * 33867569a7fSJacob Keller * Otherwise, read from the driver shared parameters to determine the clock 33967569a7fSJacob Keller * index value. 34067569a7fSJacob Keller * 34167569a7fSJacob Keller * Returns: the index of the PTP clock associated with this device, or -1 if 34267569a7fSJacob Keller * there is no associated clock. 34367569a7fSJacob Keller */ 34467569a7fSJacob Keller int ice_get_ptp_clock_index(struct ice_pf *pf) 34567569a7fSJacob Keller { 34667569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 34767569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 34867569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 34967569a7fSJacob Keller u8 tmr_idx; 35067569a7fSJacob Keller u32 value; 35167569a7fSJacob Keller int err; 35267569a7fSJacob Keller 35367569a7fSJacob Keller /* Use the ptp_clock structure if we're the main PF */ 35467569a7fSJacob Keller if (pf->ptp.clock) 35567569a7fSJacob Keller return ptp_clock_index(pf->ptp.clock); 35667569a7fSJacob Keller 35767569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 35867569a7fSJacob Keller if (!tmr_idx) 35967569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 36067569a7fSJacob Keller else 36167569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 36267569a7fSJacob Keller 36367569a7fSJacob Keller err = ice_aq_get_driver_param(hw, param_idx, &value, NULL); 36467569a7fSJacob Keller if (err) { 36567569a7fSJacob Keller dev_err(dev, "Failed to read PTP clock index parameter, err %d aq_err %s\n", 36667569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 36767569a7fSJacob Keller return -1; 36867569a7fSJacob Keller } 36967569a7fSJacob Keller 37067569a7fSJacob Keller /* The PTP clock index is an integer, and will be between 0 and 37167569a7fSJacob Keller * INT_MAX. The highest bit of the driver shared parameter is used to 37267569a7fSJacob Keller * indicate whether or not the currently stored clock index is valid. 37367569a7fSJacob Keller */ 37467569a7fSJacob Keller if (!(value & PTP_SHARED_CLK_IDX_VALID)) 37567569a7fSJacob Keller return -1; 37667569a7fSJacob Keller 37767569a7fSJacob Keller return value & ~PTP_SHARED_CLK_IDX_VALID; 37867569a7fSJacob Keller } 37967569a7fSJacob Keller 38067569a7fSJacob Keller /** 38167569a7fSJacob Keller * ice_set_ptp_clock_index - Set the PTP clock index 38267569a7fSJacob Keller * @pf: the PF pointer 38367569a7fSJacob Keller * 38467569a7fSJacob Keller * Set the PTP clock index for this device into the shared driver parameters, 38567569a7fSJacob Keller * so that other PFs associated with this device can read it. 38667569a7fSJacob Keller * 38767569a7fSJacob Keller * If the PF is unable to store the clock index, it will log an error, but 38867569a7fSJacob Keller * will continue operating PTP. 38967569a7fSJacob Keller */ 39067569a7fSJacob Keller static void ice_set_ptp_clock_index(struct ice_pf *pf) 39167569a7fSJacob Keller { 39267569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 39367569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 39467569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 39567569a7fSJacob Keller u8 tmr_idx; 39667569a7fSJacob Keller u32 value; 39767569a7fSJacob Keller int err; 39867569a7fSJacob Keller 39967569a7fSJacob Keller if (!pf->ptp.clock) 40067569a7fSJacob Keller return; 40167569a7fSJacob Keller 40267569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 40367569a7fSJacob Keller if (!tmr_idx) 40467569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 40567569a7fSJacob Keller else 40667569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 40767569a7fSJacob Keller 40867569a7fSJacob Keller value = (u32)ptp_clock_index(pf->ptp.clock); 40967569a7fSJacob Keller if (value > INT_MAX) { 41067569a7fSJacob Keller dev_err(dev, "PTP Clock index is too large to store\n"); 41167569a7fSJacob Keller return; 41267569a7fSJacob Keller } 41367569a7fSJacob Keller value |= PTP_SHARED_CLK_IDX_VALID; 41467569a7fSJacob Keller 41567569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, value, NULL); 41667569a7fSJacob Keller if (err) { 41767569a7fSJacob Keller dev_err(dev, "Failed to set PTP clock index parameter, err %d aq_err %s\n", 41867569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 41967569a7fSJacob Keller } 42067569a7fSJacob Keller } 42167569a7fSJacob Keller 42267569a7fSJacob Keller /** 42367569a7fSJacob Keller * ice_clear_ptp_clock_index - Clear the PTP clock index 42467569a7fSJacob Keller * @pf: the PF pointer 42567569a7fSJacob Keller * 42667569a7fSJacob Keller * Clear the PTP clock index for this device. Must be called when 42767569a7fSJacob Keller * unregistering the PTP clock, in order to ensure other PFs stop reporting 42867569a7fSJacob Keller * a clock object that no longer exists. 42967569a7fSJacob Keller */ 43067569a7fSJacob Keller static void ice_clear_ptp_clock_index(struct ice_pf *pf) 43167569a7fSJacob Keller { 43267569a7fSJacob Keller struct device *dev = ice_pf_to_dev(pf); 43367569a7fSJacob Keller enum ice_aqc_driver_params param_idx; 43467569a7fSJacob Keller struct ice_hw *hw = &pf->hw; 43567569a7fSJacob Keller u8 tmr_idx; 43667569a7fSJacob Keller int err; 43767569a7fSJacob Keller 43867569a7fSJacob Keller /* Do not clear the index if we don't own the timer */ 43967569a7fSJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned) 44067569a7fSJacob Keller return; 44167569a7fSJacob Keller 44267569a7fSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 44367569a7fSJacob Keller if (!tmr_idx) 44467569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 44567569a7fSJacob Keller else 44667569a7fSJacob Keller param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 44767569a7fSJacob Keller 44867569a7fSJacob Keller err = ice_aq_set_driver_param(hw, param_idx, 0, NULL); 44967569a7fSJacob Keller if (err) { 45067569a7fSJacob Keller dev_dbg(dev, "Failed to clear PTP clock index parameter, err %d aq_err %s\n", 45167569a7fSJacob Keller err, ice_aq_str(hw->adminq.sq_last_status)); 45267569a7fSJacob Keller } 45367569a7fSJacob Keller } 45467569a7fSJacob Keller 45567569a7fSJacob Keller /** 45606c16d89SJacob Keller * ice_ptp_read_src_clk_reg - Read the source clock register 45706c16d89SJacob Keller * @pf: Board private structure 45806c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 45906c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 46006c16d89SJacob Keller */ 46106c16d89SJacob Keller static u64 46206c16d89SJacob Keller ice_ptp_read_src_clk_reg(struct ice_pf *pf, struct ptp_system_timestamp *sts) 46306c16d89SJacob Keller { 46406c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 46506c16d89SJacob Keller u32 hi, lo, lo2; 46606c16d89SJacob Keller u8 tmr_idx; 46706c16d89SJacob Keller 46806c16d89SJacob Keller tmr_idx = ice_get_ptp_src_clock_index(hw); 46906c16d89SJacob Keller /* Read the system timestamp pre PHC read */ 47006c16d89SJacob Keller ptp_read_system_prets(sts); 47106c16d89SJacob Keller 47206c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 47306c16d89SJacob Keller 47406c16d89SJacob Keller /* Read the system timestamp post PHC read */ 47506c16d89SJacob Keller ptp_read_system_postts(sts); 47606c16d89SJacob Keller 47706c16d89SJacob Keller hi = rd32(hw, GLTSYN_TIME_H(tmr_idx)); 47806c16d89SJacob Keller lo2 = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 47906c16d89SJacob Keller 48006c16d89SJacob Keller if (lo2 < lo) { 48106c16d89SJacob Keller /* if TIME_L rolled over read TIME_L again and update 48206c16d89SJacob Keller * system timestamps 48306c16d89SJacob Keller */ 48406c16d89SJacob Keller ptp_read_system_prets(sts); 48506c16d89SJacob Keller lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 48606c16d89SJacob Keller ptp_read_system_postts(sts); 48706c16d89SJacob Keller hi = rd32(hw, GLTSYN_TIME_H(tmr_idx)); 48806c16d89SJacob Keller } 48906c16d89SJacob Keller 49006c16d89SJacob Keller return ((u64)hi << 32) | lo; 49106c16d89SJacob Keller } 49206c16d89SJacob Keller 49306c16d89SJacob Keller /** 49477a78115SJacob Keller * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b 49577a78115SJacob Keller * @cached_phc_time: recently cached copy of PHC time 49677a78115SJacob Keller * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value 49777a78115SJacob Keller * 49877a78115SJacob Keller * Hardware captures timestamps which contain only 32 bits of nominal 49977a78115SJacob Keller * nanoseconds, as opposed to the 64bit timestamps that the stack expects. 50077a78115SJacob Keller * Note that the captured timestamp values may be 40 bits, but the lower 50177a78115SJacob Keller * 8 bits are sub-nanoseconds and generally discarded. 50277a78115SJacob Keller * 50377a78115SJacob Keller * Extend the 32bit nanosecond timestamp using the following algorithm and 50477a78115SJacob Keller * assumptions: 50577a78115SJacob Keller * 50677a78115SJacob Keller * 1) have a recently cached copy of the PHC time 50777a78115SJacob Keller * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1 50877a78115SJacob Keller * seconds) before or after the PHC time was captured. 50977a78115SJacob Keller * 3) calculate the delta between the cached time and the timestamp 51077a78115SJacob Keller * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was 51177a78115SJacob Keller * captured after the PHC time. In this case, the full timestamp is just 51277a78115SJacob Keller * the cached PHC time plus the delta. 51377a78115SJacob Keller * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the 51477a78115SJacob Keller * timestamp was captured *before* the PHC time, i.e. because the PHC 51577a78115SJacob Keller * cache was updated after the timestamp was captured by hardware. In this 51677a78115SJacob Keller * case, the full timestamp is the cached time minus the inverse delta. 51777a78115SJacob Keller * 51877a78115SJacob Keller * This algorithm works even if the PHC time was updated after a Tx timestamp 51977a78115SJacob Keller * was requested, but before the Tx timestamp event was reported from 52077a78115SJacob Keller * hardware. 52177a78115SJacob Keller * 52277a78115SJacob Keller * This calculation primarily relies on keeping the cached PHC time up to 52377a78115SJacob Keller * date. If the timestamp was captured more than 2^31 nanoseconds after the 52477a78115SJacob Keller * PHC time, it is possible that the lower 32bits of PHC time have 52577a78115SJacob Keller * overflowed more than once, and we might generate an incorrect timestamp. 52677a78115SJacob Keller * 52777a78115SJacob Keller * This is prevented by (a) periodically updating the cached PHC time once 52877a78115SJacob Keller * a second, and (b) discarding any Tx timestamp packet if it has waited for 52977a78115SJacob Keller * a timestamp for more than one second. 53077a78115SJacob Keller */ 53177a78115SJacob Keller static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp) 53277a78115SJacob Keller { 53377a78115SJacob Keller u32 delta, phc_time_lo; 53477a78115SJacob Keller u64 ns; 53577a78115SJacob Keller 53677a78115SJacob Keller /* Extract the lower 32 bits of the PHC time */ 53777a78115SJacob Keller phc_time_lo = (u32)cached_phc_time; 53877a78115SJacob Keller 53977a78115SJacob Keller /* Calculate the delta between the lower 32bits of the cached PHC 54077a78115SJacob Keller * time and the in_tstamp value 54177a78115SJacob Keller */ 54277a78115SJacob Keller delta = (in_tstamp - phc_time_lo); 54377a78115SJacob Keller 54477a78115SJacob Keller /* Do not assume that the in_tstamp is always more recent than the 54577a78115SJacob Keller * cached PHC time. If the delta is large, it indicates that the 54677a78115SJacob Keller * in_tstamp was taken in the past, and should be converted 54777a78115SJacob Keller * forward. 54877a78115SJacob Keller */ 54977a78115SJacob Keller if (delta > (U32_MAX / 2)) { 55077a78115SJacob Keller /* reverse the delta calculation here */ 55177a78115SJacob Keller delta = (phc_time_lo - in_tstamp); 55277a78115SJacob Keller ns = cached_phc_time - delta; 55377a78115SJacob Keller } else { 55477a78115SJacob Keller ns = cached_phc_time + delta; 55577a78115SJacob Keller } 55677a78115SJacob Keller 55777a78115SJacob Keller return ns; 55877a78115SJacob Keller } 55977a78115SJacob Keller 56077a78115SJacob Keller /** 561ea9b847cSJacob Keller * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds 562ea9b847cSJacob Keller * @pf: Board private structure 563ea9b847cSJacob Keller * @in_tstamp: Ingress/egress 40b timestamp value 564ea9b847cSJacob Keller * 565ea9b847cSJacob Keller * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal 566ea9b847cSJacob Keller * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit. 567ea9b847cSJacob Keller * 568ea9b847cSJacob Keller * *--------------------------------------------------------------* 569ea9b847cSJacob Keller * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v | 570ea9b847cSJacob Keller * *--------------------------------------------------------------* 571ea9b847cSJacob Keller * 572ea9b847cSJacob Keller * The low bit is an indicator of whether the timestamp is valid. The next 573ea9b847cSJacob Keller * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow, 574ea9b847cSJacob Keller * and the remaining 32 bits are the lower 32 bits of the PHC timer. 575ea9b847cSJacob Keller * 576ea9b847cSJacob Keller * It is assumed that the caller verifies the timestamp is valid prior to 577ea9b847cSJacob Keller * calling this function. 578ea9b847cSJacob Keller * 579ea9b847cSJacob Keller * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC 580ea9b847cSJacob Keller * time stored in the device private PTP structure as the basis for timestamp 581ea9b847cSJacob Keller * extension. 582ea9b847cSJacob Keller * 583ea9b847cSJacob Keller * See ice_ptp_extend_32b_ts for a detailed explanation of the extension 584ea9b847cSJacob Keller * algorithm. 585ea9b847cSJacob Keller */ 586ea9b847cSJacob Keller static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp) 587ea9b847cSJacob Keller { 588ea9b847cSJacob Keller const u64 mask = GENMASK_ULL(31, 0); 589cd25507aSJacob Keller unsigned long discard_time; 590cd25507aSJacob Keller 591cd25507aSJacob Keller /* Discard the hardware timestamp if the cached PHC time is too old */ 592cd25507aSJacob Keller discard_time = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000); 593cd25507aSJacob Keller if (time_is_before_jiffies(discard_time)) { 594cd25507aSJacob Keller pf->ptp.tx_hwtstamp_discarded++; 595cd25507aSJacob Keller return 0; 596cd25507aSJacob Keller } 597ea9b847cSJacob Keller 598ea9b847cSJacob Keller return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time, 599ea9b847cSJacob Keller (in_tstamp >> 8) & mask); 600ea9b847cSJacob Keller } 601ea9b847cSJacob Keller 602ea9b847cSJacob Keller /** 6034b1251bdSJacob Keller * ice_ptp_tx_tstamp_work - Process Tx timestamps for a port 6044b1251bdSJacob Keller * @work: pointer to the kthread_work struct 6054b1251bdSJacob Keller * 6064b1251bdSJacob Keller * Process timestamps captured by the PHY associated with this port. To do 6074b1251bdSJacob Keller * this, loop over each index with a waiting skb. 6084b1251bdSJacob Keller * 6094b1251bdSJacob Keller * If a given index has a valid timestamp, perform the following steps: 6104b1251bdSJacob Keller * 6114b1251bdSJacob Keller * 1) copy the timestamp out of the PHY register 6124b1251bdSJacob Keller * 4) clear the timestamp valid bit in the PHY register 6134b1251bdSJacob Keller * 5) unlock the index by clearing the associated in_use bit. 6144b1251bdSJacob Keller * 2) extend the 40b timestamp value to get a 64bit timestamp 6154b1251bdSJacob Keller * 3) send that timestamp to the stack 6164b1251bdSJacob Keller * 6174b1251bdSJacob Keller * After looping, if we still have waiting SKBs, then re-queue the work. This 6184b1251bdSJacob Keller * may cause us effectively poll even when not strictly necessary. We do this 6194b1251bdSJacob Keller * because it's possible a new timestamp was requested around the same time as 6204b1251bdSJacob Keller * the interrupt. In some cases hardware might not interrupt us again when the 6214b1251bdSJacob Keller * timestamp is captured. 6224b1251bdSJacob Keller * 6234b1251bdSJacob Keller * Note that we only take the tracking lock when clearing the bit and when 6244b1251bdSJacob Keller * checking if we need to re-queue this task. The only place where bits can be 6254b1251bdSJacob Keller * set is the hard xmit routine where an SKB has a request flag set. The only 6264b1251bdSJacob Keller * places where we clear bits are this work function, or the periodic cleanup 6274b1251bdSJacob Keller * thread. If the cleanup thread clears a bit we're processing we catch it 6284b1251bdSJacob Keller * when we lock to clear the bit and then grab the SKB pointer. If a Tx thread 6294b1251bdSJacob Keller * starts a new timestamp, we might not begin processing it right away but we 6304b1251bdSJacob Keller * will notice it at the end when we re-queue the work item. If a Tx thread 6314b1251bdSJacob Keller * starts a new timestamp just after this function exits without re-queuing, 6324b1251bdSJacob Keller * the interrupt when the timestamp finishes should trigger. Avoiding holding 6334b1251bdSJacob Keller * the lock for the entire function is important in order to ensure that Tx 6344b1251bdSJacob Keller * threads do not get blocked while waiting for the lock. 6354b1251bdSJacob Keller */ 6364b1251bdSJacob Keller static void ice_ptp_tx_tstamp_work(struct kthread_work *work) 6374b1251bdSJacob Keller { 6384b1251bdSJacob Keller struct ice_ptp_port *ptp_port; 6394b1251bdSJacob Keller struct ice_ptp_tx *tx; 6404b1251bdSJacob Keller struct ice_pf *pf; 6414b1251bdSJacob Keller struct ice_hw *hw; 6424b1251bdSJacob Keller u8 idx; 6434b1251bdSJacob Keller 6444b1251bdSJacob Keller tx = container_of(work, struct ice_ptp_tx, work); 6454b1251bdSJacob Keller if (!tx->init) 6464b1251bdSJacob Keller return; 6474b1251bdSJacob Keller 6484b1251bdSJacob Keller ptp_port = container_of(tx, struct ice_ptp_port, tx); 6494b1251bdSJacob Keller pf = ptp_port_to_pf(ptp_port); 6504b1251bdSJacob Keller hw = &pf->hw; 6514b1251bdSJacob Keller 6524b1251bdSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 6534b1251bdSJacob Keller struct skb_shared_hwtstamps shhwtstamps = {}; 6544b1251bdSJacob Keller u8 phy_idx = idx + tx->quad_offset; 6554b1251bdSJacob Keller u64 raw_tstamp, tstamp; 6564b1251bdSJacob Keller struct sk_buff *skb; 6574b1251bdSJacob Keller int err; 6584b1251bdSJacob Keller 6594b1251bdSJacob Keller ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx); 6604b1251bdSJacob Keller 6614b1251bdSJacob Keller err = ice_read_phy_tstamp(hw, tx->quad, phy_idx, 6624b1251bdSJacob Keller &raw_tstamp); 6634b1251bdSJacob Keller if (err) 6644b1251bdSJacob Keller continue; 6654b1251bdSJacob Keller 6664b1251bdSJacob Keller ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx); 6674b1251bdSJacob Keller 6684b1251bdSJacob Keller /* Check if the timestamp is invalid or stale */ 6694b1251bdSJacob Keller if (!(raw_tstamp & ICE_PTP_TS_VALID) || 6704b1251bdSJacob Keller raw_tstamp == tx->tstamps[idx].cached_tstamp) 6714b1251bdSJacob Keller continue; 6724b1251bdSJacob Keller 6734b1251bdSJacob Keller /* The timestamp is valid, so we'll go ahead and clear this 6744b1251bdSJacob Keller * index and then send the timestamp up to the stack. 6754b1251bdSJacob Keller */ 6764b1251bdSJacob Keller spin_lock(&tx->lock); 6774b1251bdSJacob Keller tx->tstamps[idx].cached_tstamp = raw_tstamp; 6784b1251bdSJacob Keller clear_bit(idx, tx->in_use); 6794b1251bdSJacob Keller skb = tx->tstamps[idx].skb; 6804b1251bdSJacob Keller tx->tstamps[idx].skb = NULL; 6814b1251bdSJacob Keller spin_unlock(&tx->lock); 6824b1251bdSJacob Keller 6834b1251bdSJacob Keller /* it's (unlikely but) possible we raced with the cleanup 6844b1251bdSJacob Keller * thread for discarding old timestamp requests. 6854b1251bdSJacob Keller */ 6864b1251bdSJacob Keller if (!skb) 6874b1251bdSJacob Keller continue; 6884b1251bdSJacob Keller 6894b1251bdSJacob Keller /* Extend the timestamp using cached PHC time */ 6904b1251bdSJacob Keller tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp); 6914b1251bdSJacob Keller if (tstamp) { 6924b1251bdSJacob Keller shhwtstamps.hwtstamp = ns_to_ktime(tstamp); 6934b1251bdSJacob Keller ice_trace(tx_tstamp_complete, skb, idx); 6944b1251bdSJacob Keller } 6954b1251bdSJacob Keller 6964b1251bdSJacob Keller skb_tstamp_tx(skb, &shhwtstamps); 6974b1251bdSJacob Keller dev_kfree_skb_any(skb); 6984b1251bdSJacob Keller } 6994b1251bdSJacob Keller 7004b1251bdSJacob Keller /* Check if we still have work to do. If so, re-queue this task to 7014b1251bdSJacob Keller * poll for remaining timestamps. 7024b1251bdSJacob Keller */ 7034b1251bdSJacob Keller spin_lock(&tx->lock); 7044b1251bdSJacob Keller if (!bitmap_empty(tx->in_use, tx->len)) 7054b1251bdSJacob Keller kthread_queue_work(pf->ptp.kworker, &tx->work); 7064b1251bdSJacob Keller spin_unlock(&tx->lock); 7074b1251bdSJacob Keller } 7084b1251bdSJacob Keller 7094b1251bdSJacob Keller /** 7104b1251bdSJacob Keller * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps 7114b1251bdSJacob Keller * @tx: Tx tracking structure to initialize 7124b1251bdSJacob Keller * 7134b1251bdSJacob Keller * Assumes that the length has already been initialized. Do not call directly, 7144b1251bdSJacob Keller * use the ice_ptp_init_tx_e822 or ice_ptp_init_tx_e810 instead. 7154b1251bdSJacob Keller */ 7164b1251bdSJacob Keller static int 7174b1251bdSJacob Keller ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx) 7184b1251bdSJacob Keller { 7194b1251bdSJacob Keller tx->tstamps = kcalloc(tx->len, sizeof(*tx->tstamps), GFP_KERNEL); 7204b1251bdSJacob Keller if (!tx->tstamps) 7214b1251bdSJacob Keller return -ENOMEM; 7224b1251bdSJacob Keller 7234b1251bdSJacob Keller tx->in_use = bitmap_zalloc(tx->len, GFP_KERNEL); 7244b1251bdSJacob Keller if (!tx->in_use) { 7254b1251bdSJacob Keller kfree(tx->tstamps); 7264b1251bdSJacob Keller tx->tstamps = NULL; 7274b1251bdSJacob Keller return -ENOMEM; 7284b1251bdSJacob Keller } 7294b1251bdSJacob Keller 7304b1251bdSJacob Keller spin_lock_init(&tx->lock); 7314b1251bdSJacob Keller kthread_init_work(&tx->work, ice_ptp_tx_tstamp_work); 7324b1251bdSJacob Keller 7334b1251bdSJacob Keller tx->init = 1; 7344b1251bdSJacob Keller 7354b1251bdSJacob Keller return 0; 7364b1251bdSJacob Keller } 7374b1251bdSJacob Keller 7384b1251bdSJacob Keller /** 7394b1251bdSJacob Keller * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker 7404b1251bdSJacob Keller * @pf: Board private structure 7414b1251bdSJacob Keller * @tx: the tracker to flush 7424b1251bdSJacob Keller */ 7434b1251bdSJacob Keller static void 7444b1251bdSJacob Keller ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 7454b1251bdSJacob Keller { 7464b1251bdSJacob Keller u8 idx; 7474b1251bdSJacob Keller 7484b1251bdSJacob Keller for (idx = 0; idx < tx->len; idx++) { 7494b1251bdSJacob Keller u8 phy_idx = idx + tx->quad_offset; 7504b1251bdSJacob Keller 7514b1251bdSJacob Keller spin_lock(&tx->lock); 7524b1251bdSJacob Keller if (tx->tstamps[idx].skb) { 7534b1251bdSJacob Keller dev_kfree_skb_any(tx->tstamps[idx].skb); 7544b1251bdSJacob Keller tx->tstamps[idx].skb = NULL; 7554b1251bdSJacob Keller pf->ptp.tx_hwtstamp_flushed++; 7564b1251bdSJacob Keller } 7574b1251bdSJacob Keller clear_bit(idx, tx->in_use); 7584b1251bdSJacob Keller spin_unlock(&tx->lock); 7594b1251bdSJacob Keller 7604b1251bdSJacob Keller /* Clear any potential residual timestamp in the PHY block */ 7614b1251bdSJacob Keller if (!pf->hw.reset_ongoing) 7624b1251bdSJacob Keller ice_clear_phy_tstamp(&pf->hw, tx->quad, phy_idx); 7634b1251bdSJacob Keller } 7644b1251bdSJacob Keller } 7654b1251bdSJacob Keller 7664b1251bdSJacob Keller /** 7674b1251bdSJacob Keller * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker 7684b1251bdSJacob Keller * @pf: Board private structure 7694b1251bdSJacob Keller * @tx: Tx tracking structure to release 7704b1251bdSJacob Keller * 7714b1251bdSJacob Keller * Free memory associated with the Tx timestamp tracker. 7724b1251bdSJacob Keller */ 7734b1251bdSJacob Keller static void 7744b1251bdSJacob Keller ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 7754b1251bdSJacob Keller { 7764b1251bdSJacob Keller tx->init = 0; 7774b1251bdSJacob Keller 7784b1251bdSJacob Keller kthread_cancel_work_sync(&tx->work); 7794b1251bdSJacob Keller 7804b1251bdSJacob Keller ice_ptp_flush_tx_tracker(pf, tx); 7814b1251bdSJacob Keller 7824b1251bdSJacob Keller kfree(tx->tstamps); 7834b1251bdSJacob Keller tx->tstamps = NULL; 7844b1251bdSJacob Keller 7854b1251bdSJacob Keller bitmap_free(tx->in_use); 7864b1251bdSJacob Keller tx->in_use = NULL; 7874b1251bdSJacob Keller 7884b1251bdSJacob Keller tx->len = 0; 7894b1251bdSJacob Keller } 7904b1251bdSJacob Keller 7914b1251bdSJacob Keller /** 7924b1251bdSJacob Keller * ice_ptp_init_tx_e822 - Initialize tracking for Tx timestamps 7934b1251bdSJacob Keller * @pf: Board private structure 7944b1251bdSJacob Keller * @tx: the Tx tracking structure to initialize 7954b1251bdSJacob Keller * @port: the port this structure tracks 7964b1251bdSJacob Keller * 7974b1251bdSJacob Keller * Initialize the Tx timestamp tracker for this port. For generic MAC devices, 7984b1251bdSJacob Keller * the timestamp block is shared for all ports in the same quad. To avoid 7994b1251bdSJacob Keller * ports using the same timestamp index, logically break the block of 8004b1251bdSJacob Keller * registers into chunks based on the port number. 8014b1251bdSJacob Keller */ 8024b1251bdSJacob Keller static int 8034b1251bdSJacob Keller ice_ptp_init_tx_e822(struct ice_pf *pf, struct ice_ptp_tx *tx, u8 port) 8044b1251bdSJacob Keller { 8054b1251bdSJacob Keller tx->quad = port / ICE_PORTS_PER_QUAD; 8064b1251bdSJacob Keller tx->quad_offset = (port % ICE_PORTS_PER_QUAD) * INDEX_PER_PORT; 8074b1251bdSJacob Keller tx->len = INDEX_PER_PORT; 8084b1251bdSJacob Keller 8094b1251bdSJacob Keller return ice_ptp_alloc_tx_tracker(tx); 8104b1251bdSJacob Keller } 8114b1251bdSJacob Keller 8124b1251bdSJacob Keller /** 8134b1251bdSJacob Keller * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps 8144b1251bdSJacob Keller * @pf: Board private structure 8154b1251bdSJacob Keller * @tx: the Tx tracking structure to initialize 8164b1251bdSJacob Keller * 8174b1251bdSJacob Keller * Initialize the Tx timestamp tracker for this PF. For E810 devices, each 8184b1251bdSJacob Keller * port has its own block of timestamps, independent of the other ports. 8194b1251bdSJacob Keller */ 8204b1251bdSJacob Keller static int 8214b1251bdSJacob Keller ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx) 8224b1251bdSJacob Keller { 8234b1251bdSJacob Keller tx->quad = pf->hw.port_info->lport; 8244b1251bdSJacob Keller tx->quad_offset = 0; 8254b1251bdSJacob Keller tx->len = INDEX_PER_QUAD; 8264b1251bdSJacob Keller 8274b1251bdSJacob Keller return ice_ptp_alloc_tx_tracker(tx); 8284b1251bdSJacob Keller } 8294b1251bdSJacob Keller 8304b1251bdSJacob Keller /** 8314b1251bdSJacob Keller * ice_ptp_tx_tstamp_cleanup - Cleanup old timestamp requests that got dropped 8324b1251bdSJacob Keller * @pf: pointer to the PF struct 8334b1251bdSJacob Keller * @tx: PTP Tx tracker to clean up 8344b1251bdSJacob Keller * 8354b1251bdSJacob Keller * Loop through the Tx timestamp requests and see if any of them have been 8364b1251bdSJacob Keller * waiting for a long time. Discard any SKBs that have been waiting for more 8374b1251bdSJacob Keller * than 2 seconds. This is long enough to be reasonably sure that the 8384b1251bdSJacob Keller * timestamp will never be captured. This might happen if the packet gets 8394b1251bdSJacob Keller * discarded before it reaches the PHY timestamping block. 8404b1251bdSJacob Keller */ 8414b1251bdSJacob Keller static void ice_ptp_tx_tstamp_cleanup(struct ice_pf *pf, struct ice_ptp_tx *tx) 8424b1251bdSJacob Keller { 8434b1251bdSJacob Keller struct ice_hw *hw = &pf->hw; 8444b1251bdSJacob Keller u8 idx; 8454b1251bdSJacob Keller 8464b1251bdSJacob Keller if (!tx->init) 8474b1251bdSJacob Keller return; 8484b1251bdSJacob Keller 8494b1251bdSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 8504b1251bdSJacob Keller struct sk_buff *skb; 8514b1251bdSJacob Keller u64 raw_tstamp; 8524b1251bdSJacob Keller 8534b1251bdSJacob Keller /* Check if this SKB has been waiting for too long */ 8544b1251bdSJacob Keller if (time_is_after_jiffies(tx->tstamps[idx].start + 2 * HZ)) 8554b1251bdSJacob Keller continue; 8564b1251bdSJacob Keller 8574b1251bdSJacob Keller /* Read tstamp to be able to use this register again */ 8584b1251bdSJacob Keller ice_read_phy_tstamp(hw, tx->quad, idx + tx->quad_offset, 8594b1251bdSJacob Keller &raw_tstamp); 8604b1251bdSJacob Keller 8614b1251bdSJacob Keller spin_lock(&tx->lock); 8624b1251bdSJacob Keller skb = tx->tstamps[idx].skb; 8634b1251bdSJacob Keller tx->tstamps[idx].skb = NULL; 8644b1251bdSJacob Keller clear_bit(idx, tx->in_use); 8654b1251bdSJacob Keller spin_unlock(&tx->lock); 8664b1251bdSJacob Keller 8674b1251bdSJacob Keller /* Count the number of Tx timestamps which have timed out */ 8684b1251bdSJacob Keller pf->ptp.tx_hwtstamp_timeouts++; 8694b1251bdSJacob Keller 8704b1251bdSJacob Keller /* Free the SKB after we've cleared the bit */ 8714b1251bdSJacob Keller dev_kfree_skb_any(skb); 8724b1251bdSJacob Keller } 8734b1251bdSJacob Keller } 8744b1251bdSJacob Keller 8754b1251bdSJacob Keller /** 8764b1251bdSJacob Keller * ice_ptp_update_cached_phctime - Update the cached PHC time values 8774b1251bdSJacob Keller * @pf: Board specific private structure 8784b1251bdSJacob Keller * 8794b1251bdSJacob Keller * This function updates the system time values which are cached in the PF 8804b1251bdSJacob Keller * structure and the Rx rings. 8814b1251bdSJacob Keller * 8824b1251bdSJacob Keller * This function must be called periodically to ensure that the cached value 883b1a582e6SJacob Keller * is never more than 2 seconds old. 884b1a582e6SJacob Keller * 885b1a582e6SJacob Keller * Note that the cached copy in the PF PTP structure is always updated, even 886b1a582e6SJacob Keller * if we can't update the copy in the Rx rings. 8874b1251bdSJacob Keller * 8884b1251bdSJacob Keller * Return: 8894b1251bdSJacob Keller * * 0 - OK, successfully updated 8904b1251bdSJacob Keller * * -EAGAIN - PF was busy, need to reschedule the update 8914b1251bdSJacob Keller */ 8924b1251bdSJacob Keller static int ice_ptp_update_cached_phctime(struct ice_pf *pf) 8934b1251bdSJacob Keller { 8944b1251bdSJacob Keller struct device *dev = ice_pf_to_dev(pf); 8954b1251bdSJacob Keller unsigned long update_before; 8964b1251bdSJacob Keller u64 systime; 8974b1251bdSJacob Keller int i; 8984b1251bdSJacob Keller 8994b1251bdSJacob Keller update_before = pf->ptp.cached_phc_jiffies + msecs_to_jiffies(2000); 9004b1251bdSJacob Keller if (pf->ptp.cached_phc_time && 9014b1251bdSJacob Keller time_is_before_jiffies(update_before)) { 9024b1251bdSJacob Keller unsigned long time_taken = jiffies - pf->ptp.cached_phc_jiffies; 9034b1251bdSJacob Keller 9044b1251bdSJacob Keller dev_warn(dev, "%u msecs passed between update to cached PHC time\n", 9054b1251bdSJacob Keller jiffies_to_msecs(time_taken)); 9064b1251bdSJacob Keller pf->ptp.late_cached_phc_updates++; 9074b1251bdSJacob Keller } 9084b1251bdSJacob Keller 9094b1251bdSJacob Keller /* Read the current PHC time */ 9104b1251bdSJacob Keller systime = ice_ptp_read_src_clk_reg(pf, NULL); 9114b1251bdSJacob Keller 9124b1251bdSJacob Keller /* Update the cached PHC time stored in the PF structure */ 9134b1251bdSJacob Keller WRITE_ONCE(pf->ptp.cached_phc_time, systime); 9144b1251bdSJacob Keller WRITE_ONCE(pf->ptp.cached_phc_jiffies, jiffies); 9154b1251bdSJacob Keller 916b1a582e6SJacob Keller if (test_and_set_bit(ICE_CFG_BUSY, pf->state)) 917b1a582e6SJacob Keller return -EAGAIN; 918b1a582e6SJacob Keller 9194b1251bdSJacob Keller ice_for_each_vsi(pf, i) { 9204b1251bdSJacob Keller struct ice_vsi *vsi = pf->vsi[i]; 9214b1251bdSJacob Keller int j; 9224b1251bdSJacob Keller 9234b1251bdSJacob Keller if (!vsi) 9244b1251bdSJacob Keller continue; 9254b1251bdSJacob Keller 9264b1251bdSJacob Keller if (vsi->type != ICE_VSI_PF) 9274b1251bdSJacob Keller continue; 9284b1251bdSJacob Keller 9294b1251bdSJacob Keller ice_for_each_rxq(vsi, j) { 9304b1251bdSJacob Keller if (!vsi->rx_rings[j]) 9314b1251bdSJacob Keller continue; 9324b1251bdSJacob Keller WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime); 9334b1251bdSJacob Keller } 9344b1251bdSJacob Keller } 9354b1251bdSJacob Keller clear_bit(ICE_CFG_BUSY, pf->state); 9364b1251bdSJacob Keller 9374b1251bdSJacob Keller return 0; 9384b1251bdSJacob Keller } 9394b1251bdSJacob Keller 9404b1251bdSJacob Keller /** 941b1a582e6SJacob Keller * ice_ptp_reset_cached_phctime - Reset cached PHC time after an update 942b1a582e6SJacob Keller * @pf: Board specific private structure 943b1a582e6SJacob Keller * 944b1a582e6SJacob Keller * This function must be called when the cached PHC time is no longer valid, 945b1a582e6SJacob Keller * such as after a time adjustment. It discards any outstanding Tx timestamps, 946b1a582e6SJacob Keller * and updates the cached PHC time for both the PF and Rx rings. If updating 947b1a582e6SJacob Keller * the PHC time cannot be done immediately, a warning message is logged and 948b1a582e6SJacob Keller * the work item is scheduled. 949b1a582e6SJacob Keller * 950b1a582e6SJacob Keller * These steps are required in order to ensure that we do not accidentally 951b1a582e6SJacob Keller * report a timestamp extended by the wrong PHC cached copy. Note that we 952b1a582e6SJacob Keller * do not directly update the cached timestamp here because it is possible 953b1a582e6SJacob Keller * this might produce an error when ICE_CFG_BUSY is set. If this occurred, we 954b1a582e6SJacob Keller * would have to try again. During that time window, timestamps might be 955b1a582e6SJacob Keller * requested and returned with an invalid extension. Thus, on failure to 956b1a582e6SJacob Keller * immediately update the cached PHC time we would need to zero the value 957b1a582e6SJacob Keller * anyways. For this reason, we just zero the value immediately and queue the 958b1a582e6SJacob Keller * update work item. 959b1a582e6SJacob Keller */ 960b1a582e6SJacob Keller static void ice_ptp_reset_cached_phctime(struct ice_pf *pf) 961b1a582e6SJacob Keller { 962b1a582e6SJacob Keller struct device *dev = ice_pf_to_dev(pf); 963b1a582e6SJacob Keller int err; 964b1a582e6SJacob Keller 965b1a582e6SJacob Keller /* Update the cached PHC time immediately if possible, otherwise 966b1a582e6SJacob Keller * schedule the work item to execute soon. 967b1a582e6SJacob Keller */ 968b1a582e6SJacob Keller err = ice_ptp_update_cached_phctime(pf); 969b1a582e6SJacob Keller if (err) { 970b1a582e6SJacob Keller /* If another thread is updating the Rx rings, we won't 971b1a582e6SJacob Keller * properly reset them here. This could lead to reporting of 972b1a582e6SJacob Keller * invalid timestamps, but there isn't much we can do. 973b1a582e6SJacob Keller */ 974b1a582e6SJacob Keller dev_warn(dev, "%s: ICE_CFG_BUSY, unable to immediately update cached PHC time\n", 975b1a582e6SJacob Keller __func__); 976b1a582e6SJacob Keller 977b1a582e6SJacob Keller /* Queue the work item to update the Rx rings when possible */ 978b1a582e6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work, 979b1a582e6SJacob Keller msecs_to_jiffies(10)); 980b1a582e6SJacob Keller } 981b1a582e6SJacob Keller 982b1a582e6SJacob Keller /* Flush any outstanding Tx timestamps */ 983b1a582e6SJacob Keller ice_ptp_flush_tx_tracker(pf, &pf->ptp.port.tx); 984b1a582e6SJacob Keller } 985b1a582e6SJacob Keller 986b1a582e6SJacob Keller /** 98706c16d89SJacob Keller * ice_ptp_read_time - Read the time from the device 98806c16d89SJacob Keller * @pf: Board private structure 98906c16d89SJacob Keller * @ts: timespec structure to hold the current time value 99006c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 99106c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 99206c16d89SJacob Keller * 99306c16d89SJacob Keller * This function reads the source clock registers and stores them in a timespec. 99406c16d89SJacob Keller * However, since the registers are 64 bits of nanoseconds, we must convert the 99506c16d89SJacob Keller * result to a timespec before we can return. 99606c16d89SJacob Keller */ 99706c16d89SJacob Keller static void 99806c16d89SJacob Keller ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts, 99906c16d89SJacob Keller struct ptp_system_timestamp *sts) 100006c16d89SJacob Keller { 100106c16d89SJacob Keller u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts); 100206c16d89SJacob Keller 100306c16d89SJacob Keller *ts = ns_to_timespec64(time_ns); 100406c16d89SJacob Keller } 100506c16d89SJacob Keller 100606c16d89SJacob Keller /** 100706c16d89SJacob Keller * ice_ptp_write_init - Set PHC time to provided value 100806c16d89SJacob Keller * @pf: Board private structure 100906c16d89SJacob Keller * @ts: timespec structure that holds the new time value 101006c16d89SJacob Keller * 101106c16d89SJacob Keller * Set the PHC time to the specified time provided in the timespec. 101206c16d89SJacob Keller */ 101306c16d89SJacob Keller static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts) 101406c16d89SJacob Keller { 101506c16d89SJacob Keller u64 ns = timespec64_to_ns(ts); 101606c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 101706c16d89SJacob Keller 101806c16d89SJacob Keller return ice_ptp_init_time(hw, ns); 101906c16d89SJacob Keller } 102006c16d89SJacob Keller 102106c16d89SJacob Keller /** 102206c16d89SJacob Keller * ice_ptp_write_adj - Adjust PHC clock time atomically 102306c16d89SJacob Keller * @pf: Board private structure 102406c16d89SJacob Keller * @adj: Adjustment in nanoseconds 102506c16d89SJacob Keller * 102606c16d89SJacob Keller * Perform an atomic adjustment of the PHC time by the specified number of 102706c16d89SJacob Keller * nanoseconds. 102806c16d89SJacob Keller */ 102906c16d89SJacob Keller static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj) 103006c16d89SJacob Keller { 103106c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 103206c16d89SJacob Keller 103306c16d89SJacob Keller return ice_ptp_adj_clock(hw, adj); 103406c16d89SJacob Keller } 103506c16d89SJacob Keller 103606c16d89SJacob Keller /** 103778267d0cSJacob Keller * ice_base_incval - Get base timer increment value 103878267d0cSJacob Keller * @pf: Board private structure 103978267d0cSJacob Keller * 104078267d0cSJacob Keller * Look up the base timer increment value for this device. The base increment 104178267d0cSJacob Keller * value is used to define the nominal clock tick rate. This increment value 104278267d0cSJacob Keller * is programmed during device initialization. It is also used as the basis 104378267d0cSJacob Keller * for calculating adjustments using scaled_ppm. 104478267d0cSJacob Keller */ 104578267d0cSJacob Keller static u64 ice_base_incval(struct ice_pf *pf) 104678267d0cSJacob Keller { 10473a749623SJacob Keller struct ice_hw *hw = &pf->hw; 10483a749623SJacob Keller u64 incval; 10493a749623SJacob Keller 10503a749623SJacob Keller if (ice_is_e810(hw)) 10513a749623SJacob Keller incval = ICE_PTP_NOMINAL_INCVAL_E810; 10523a749623SJacob Keller else if (ice_e822_time_ref(hw) < NUM_ICE_TIME_REF_FREQ) 10533a749623SJacob Keller incval = ice_e822_nominal_incval(ice_e822_time_ref(hw)); 10543a749623SJacob Keller else 10553a749623SJacob Keller incval = UNKNOWN_INCVAL_E822; 10563a749623SJacob Keller 10573a749623SJacob Keller dev_dbg(ice_pf_to_dev(pf), "PTP: using base increment value of 0x%016llx\n", 10583a749623SJacob Keller incval); 10593a749623SJacob Keller 10603a749623SJacob Keller return incval; 10613a749623SJacob Keller } 10623a749623SJacob Keller 10633a749623SJacob Keller /** 10643a749623SJacob Keller * ice_ptp_reset_ts_memory_quad - Reset timestamp memory for one quad 10653a749623SJacob Keller * @pf: The PF private data structure 10663a749623SJacob Keller * @quad: The quad (0-4) 10673a749623SJacob Keller */ 10683a749623SJacob Keller static void ice_ptp_reset_ts_memory_quad(struct ice_pf *pf, int quad) 10693a749623SJacob Keller { 10703a749623SJacob Keller struct ice_hw *hw = &pf->hw; 10713a749623SJacob Keller 10723a749623SJacob Keller ice_write_quad_reg_e822(hw, quad, Q_REG_TS_CTRL, Q_REG_TS_CTRL_M); 10733a749623SJacob Keller ice_write_quad_reg_e822(hw, quad, Q_REG_TS_CTRL, ~(u32)Q_REG_TS_CTRL_M); 10743a749623SJacob Keller } 10753a749623SJacob Keller 10763a749623SJacob Keller /** 1077a69f1cb6SJacob Keller * ice_ptp_check_tx_fifo - Check whether Tx FIFO is in an OK state 1078a69f1cb6SJacob Keller * @port: PTP port for which Tx FIFO is checked 1079a69f1cb6SJacob Keller */ 1080a69f1cb6SJacob Keller static int ice_ptp_check_tx_fifo(struct ice_ptp_port *port) 1081a69f1cb6SJacob Keller { 1082a69f1cb6SJacob Keller int quad = port->port_num / ICE_PORTS_PER_QUAD; 1083a69f1cb6SJacob Keller int offs = port->port_num % ICE_PORTS_PER_QUAD; 1084a69f1cb6SJacob Keller struct ice_pf *pf; 1085a69f1cb6SJacob Keller struct ice_hw *hw; 1086a69f1cb6SJacob Keller u32 val, phy_sts; 1087a69f1cb6SJacob Keller int err; 1088a69f1cb6SJacob Keller 1089a69f1cb6SJacob Keller pf = ptp_port_to_pf(port); 1090a69f1cb6SJacob Keller hw = &pf->hw; 1091a69f1cb6SJacob Keller 1092a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == FIFO_OK) 1093a69f1cb6SJacob Keller return 0; 1094a69f1cb6SJacob Keller 1095a69f1cb6SJacob Keller /* need to read FIFO state */ 1096a69f1cb6SJacob Keller if (offs == 0 || offs == 1) 1097a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO01_STATUS, 1098a69f1cb6SJacob Keller &val); 1099a69f1cb6SJacob Keller else 1100a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO23_STATUS, 1101a69f1cb6SJacob Keller &val); 1102a69f1cb6SJacob Keller 1103a69f1cb6SJacob Keller if (err) { 1104a69f1cb6SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to check port %d Tx FIFO, err %d\n", 1105a69f1cb6SJacob Keller port->port_num, err); 1106a69f1cb6SJacob Keller return err; 1107a69f1cb6SJacob Keller } 1108a69f1cb6SJacob Keller 1109a69f1cb6SJacob Keller if (offs & 0x1) 1110a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO13_M) >> Q_REG_FIFO13_S; 1111a69f1cb6SJacob Keller else 1112a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO02_M) >> Q_REG_FIFO02_S; 1113a69f1cb6SJacob Keller 1114a69f1cb6SJacob Keller if (phy_sts & FIFO_EMPTY) { 1115a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK; 1116a69f1cb6SJacob Keller return 0; 1117a69f1cb6SJacob Keller } 1118a69f1cb6SJacob Keller 1119a69f1cb6SJacob Keller port->tx_fifo_busy_cnt++; 1120a69f1cb6SJacob Keller 1121a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf), "Try %d, port %d FIFO not empty\n", 1122a69f1cb6SJacob Keller port->tx_fifo_busy_cnt, port->port_num); 1123a69f1cb6SJacob Keller 1124a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == ICE_PTP_FIFO_NUM_CHECKS) { 1125a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf), 1126a69f1cb6SJacob Keller "Port %d Tx FIFO still not empty; resetting quad %d\n", 1127a69f1cb6SJacob Keller port->port_num, quad); 1128a69f1cb6SJacob Keller ice_ptp_reset_ts_memory_quad(pf, quad); 1129a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK; 1130a69f1cb6SJacob Keller return 0; 1131a69f1cb6SJacob Keller } 1132a69f1cb6SJacob Keller 1133a69f1cb6SJacob Keller return -EAGAIN; 1134a69f1cb6SJacob Keller } 1135a69f1cb6SJacob Keller 1136a69f1cb6SJacob Keller /** 1137a69f1cb6SJacob Keller * ice_ptp_check_tx_offset_valid - Check if the Tx PHY offset is valid 1138a69f1cb6SJacob Keller * @port: the PTP port to check 1139a69f1cb6SJacob Keller * 1140a69f1cb6SJacob Keller * Checks whether the Tx offset for the PHY associated with this port is 1141a69f1cb6SJacob Keller * valid. Returns 0 if the offset is valid, and a non-zero error code if it is 1142a69f1cb6SJacob Keller * not. 1143a69f1cb6SJacob Keller */ 1144a69f1cb6SJacob Keller static int ice_ptp_check_tx_offset_valid(struct ice_ptp_port *port) 1145a69f1cb6SJacob Keller { 1146a69f1cb6SJacob Keller struct ice_pf *pf = ptp_port_to_pf(port); 1147a69f1cb6SJacob Keller struct device *dev = ice_pf_to_dev(pf); 1148a69f1cb6SJacob Keller struct ice_hw *hw = &pf->hw; 1149a69f1cb6SJacob Keller u32 val; 1150a69f1cb6SJacob Keller int err; 1151a69f1cb6SJacob Keller 1152a69f1cb6SJacob Keller err = ice_ptp_check_tx_fifo(port); 1153a69f1cb6SJacob Keller if (err) 1154a69f1cb6SJacob Keller return err; 1155a69f1cb6SJacob Keller 1156a69f1cb6SJacob Keller err = ice_read_phy_reg_e822(hw, port->port_num, P_REG_TX_OV_STATUS, 1157a69f1cb6SJacob Keller &val); 1158a69f1cb6SJacob Keller if (err) { 1159a69f1cb6SJacob Keller dev_err(dev, "Failed to read TX_OV_STATUS for port %d, err %d\n", 1160a69f1cb6SJacob Keller port->port_num, err); 1161a69f1cb6SJacob Keller return -EAGAIN; 1162a69f1cb6SJacob Keller } 1163a69f1cb6SJacob Keller 1164a69f1cb6SJacob Keller if (!(val & P_REG_TX_OV_STATUS_OV_M)) 1165a69f1cb6SJacob Keller return -EAGAIN; 1166a69f1cb6SJacob Keller 1167a69f1cb6SJacob Keller return 0; 1168a69f1cb6SJacob Keller } 1169a69f1cb6SJacob Keller 1170a69f1cb6SJacob Keller /** 1171a69f1cb6SJacob Keller * ice_ptp_check_rx_offset_valid - Check if the Rx PHY offset is valid 1172a69f1cb6SJacob Keller * @port: the PTP port to check 1173a69f1cb6SJacob Keller * 1174a69f1cb6SJacob Keller * Checks whether the Rx offset for the PHY associated with this port is 1175a69f1cb6SJacob Keller * valid. Returns 0 if the offset is valid, and a non-zero error code if it is 1176a69f1cb6SJacob Keller * not. 1177a69f1cb6SJacob Keller */ 1178a69f1cb6SJacob Keller static int ice_ptp_check_rx_offset_valid(struct ice_ptp_port *port) 1179a69f1cb6SJacob Keller { 1180a69f1cb6SJacob Keller struct ice_pf *pf = ptp_port_to_pf(port); 1181a69f1cb6SJacob Keller struct device *dev = ice_pf_to_dev(pf); 1182a69f1cb6SJacob Keller struct ice_hw *hw = &pf->hw; 1183a69f1cb6SJacob Keller int err; 1184a69f1cb6SJacob Keller u32 val; 1185a69f1cb6SJacob Keller 1186a69f1cb6SJacob Keller err = ice_read_phy_reg_e822(hw, port->port_num, P_REG_RX_OV_STATUS, 1187a69f1cb6SJacob Keller &val); 1188a69f1cb6SJacob Keller if (err) { 1189a69f1cb6SJacob Keller dev_err(dev, "Failed to read RX_OV_STATUS for port %d, err %d\n", 1190a69f1cb6SJacob Keller port->port_num, err); 1191a69f1cb6SJacob Keller return err; 1192a69f1cb6SJacob Keller } 1193a69f1cb6SJacob Keller 1194a69f1cb6SJacob Keller if (!(val & P_REG_RX_OV_STATUS_OV_M)) 1195a69f1cb6SJacob Keller return -EAGAIN; 1196a69f1cb6SJacob Keller 1197a69f1cb6SJacob Keller return 0; 1198a69f1cb6SJacob Keller } 1199a69f1cb6SJacob Keller 1200a69f1cb6SJacob Keller /** 1201a69f1cb6SJacob Keller * ice_ptp_check_offset_valid - Check port offset valid bit 1202a69f1cb6SJacob Keller * @port: Port for which offset valid bit is checked 1203a69f1cb6SJacob Keller * 1204a69f1cb6SJacob Keller * Returns 0 if both Tx and Rx offset are valid, and -EAGAIN if one of the 1205a69f1cb6SJacob Keller * offset is not ready. 1206a69f1cb6SJacob Keller */ 1207a69f1cb6SJacob Keller static int ice_ptp_check_offset_valid(struct ice_ptp_port *port) 1208a69f1cb6SJacob Keller { 1209a69f1cb6SJacob Keller int tx_err, rx_err; 1210a69f1cb6SJacob Keller 1211a69f1cb6SJacob Keller /* always check both Tx and Rx offset validity */ 1212a69f1cb6SJacob Keller tx_err = ice_ptp_check_tx_offset_valid(port); 1213a69f1cb6SJacob Keller rx_err = ice_ptp_check_rx_offset_valid(port); 1214a69f1cb6SJacob Keller 1215a69f1cb6SJacob Keller if (tx_err || rx_err) 1216a69f1cb6SJacob Keller return -EAGAIN; 1217a69f1cb6SJacob Keller 1218a69f1cb6SJacob Keller return 0; 1219a69f1cb6SJacob Keller } 1220a69f1cb6SJacob Keller 1221a69f1cb6SJacob Keller /** 1222a69f1cb6SJacob Keller * ice_ptp_wait_for_offset_valid - Check for valid Tx and Rx offsets 1223a69f1cb6SJacob Keller * @work: Pointer to the kthread_work structure for this task 1224a69f1cb6SJacob Keller * 1225a69f1cb6SJacob Keller * Check whether both the Tx and Rx offsets are valid for enabling the vernier 1226a69f1cb6SJacob Keller * calibration. 1227a69f1cb6SJacob Keller * 1228a69f1cb6SJacob Keller * Once we have valid offsets from hardware, update the total Tx and Rx 1229a69f1cb6SJacob Keller * offsets, and exit bypass mode. This enables more precise timestamps using 1230a69f1cb6SJacob Keller * the extra data measured during the vernier calibration process. 1231a69f1cb6SJacob Keller */ 1232a69f1cb6SJacob Keller static void ice_ptp_wait_for_offset_valid(struct kthread_work *work) 1233a69f1cb6SJacob Keller { 1234a69f1cb6SJacob Keller struct ice_ptp_port *port; 1235a69f1cb6SJacob Keller int err; 1236a69f1cb6SJacob Keller struct device *dev; 1237a69f1cb6SJacob Keller struct ice_pf *pf; 1238a69f1cb6SJacob Keller struct ice_hw *hw; 1239a69f1cb6SJacob Keller 1240a69f1cb6SJacob Keller port = container_of(work, struct ice_ptp_port, ov_work.work); 1241a69f1cb6SJacob Keller pf = ptp_port_to_pf(port); 1242a69f1cb6SJacob Keller hw = &pf->hw; 1243a69f1cb6SJacob Keller dev = ice_pf_to_dev(pf); 1244a69f1cb6SJacob Keller 1245*0b57e0d4SMichal Michalik if (ice_is_reset_in_progress(pf->state)) 1246*0b57e0d4SMichal Michalik return; 1247*0b57e0d4SMichal Michalik 1248a69f1cb6SJacob Keller if (ice_ptp_check_offset_valid(port)) { 1249a69f1cb6SJacob Keller /* Offsets not ready yet, try again later */ 1250a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, 1251a69f1cb6SJacob Keller &port->ov_work, 1252a69f1cb6SJacob Keller msecs_to_jiffies(100)); 1253a69f1cb6SJacob Keller return; 1254a69f1cb6SJacob Keller } 1255a69f1cb6SJacob Keller 1256a69f1cb6SJacob Keller /* Offsets are valid, so it is safe to exit bypass mode */ 1257a69f1cb6SJacob Keller err = ice_phy_exit_bypass_e822(hw, port->port_num); 1258a69f1cb6SJacob Keller if (err) { 1259a69f1cb6SJacob Keller dev_warn(dev, "Failed to exit bypass mode for PHY port %u, err %d\n", 1260a69f1cb6SJacob Keller port->port_num, err); 1261a69f1cb6SJacob Keller return; 1262a69f1cb6SJacob Keller } 1263a69f1cb6SJacob Keller } 1264a69f1cb6SJacob Keller 1265a69f1cb6SJacob Keller /** 12663a749623SJacob Keller * ice_ptp_port_phy_stop - Stop timestamping for a PHY port 12673a749623SJacob Keller * @ptp_port: PTP port to stop 12683a749623SJacob Keller */ 12693a749623SJacob Keller static int 12703a749623SJacob Keller ice_ptp_port_phy_stop(struct ice_ptp_port *ptp_port) 12713a749623SJacob Keller { 12723a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port); 12733a749623SJacob Keller u8 port = ptp_port->port_num; 12743a749623SJacob Keller struct ice_hw *hw = &pf->hw; 12753a749623SJacob Keller int err; 12763a749623SJacob Keller 12773a749623SJacob Keller if (ice_is_e810(hw)) 12783a749623SJacob Keller return 0; 12793a749623SJacob Keller 12803a749623SJacob Keller mutex_lock(&ptp_port->ps_lock); 12813a749623SJacob Keller 1282a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work); 1283a69f1cb6SJacob Keller 12843a749623SJacob Keller err = ice_stop_phy_timer_e822(hw, port, true); 12853a749623SJacob Keller if (err) 12863a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d down, err %d\n", 12873a749623SJacob Keller port, err); 12883a749623SJacob Keller 12893a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock); 12903a749623SJacob Keller 12913a749623SJacob Keller return err; 12923a749623SJacob Keller } 12933a749623SJacob Keller 12943a749623SJacob Keller /** 12953a749623SJacob Keller * ice_ptp_port_phy_restart - (Re)start and calibrate PHY timestamping 12963a749623SJacob Keller * @ptp_port: PTP port for which the PHY start is set 12973a749623SJacob Keller * 12983a749623SJacob Keller * Start the PHY timestamping block, and initiate Vernier timestamping 12993a749623SJacob Keller * calibration. If timestamping cannot be calibrated (such as if link is down) 13003a749623SJacob Keller * then disable the timestamping block instead. 13013a749623SJacob Keller */ 13023a749623SJacob Keller static int 13033a749623SJacob Keller ice_ptp_port_phy_restart(struct ice_ptp_port *ptp_port) 13043a749623SJacob Keller { 13053a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port); 13063a749623SJacob Keller u8 port = ptp_port->port_num; 13073a749623SJacob Keller struct ice_hw *hw = &pf->hw; 13083a749623SJacob Keller int err; 13093a749623SJacob Keller 13103a749623SJacob Keller if (ice_is_e810(hw)) 13113a749623SJacob Keller return 0; 13123a749623SJacob Keller 13133a749623SJacob Keller if (!ptp_port->link_up) 13143a749623SJacob Keller return ice_ptp_port_phy_stop(ptp_port); 13153a749623SJacob Keller 13163a749623SJacob Keller mutex_lock(&ptp_port->ps_lock); 13173a749623SJacob Keller 1318a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work); 1319a69f1cb6SJacob Keller 13203a749623SJacob Keller /* temporarily disable Tx timestamps while calibrating PHY offset */ 13213a749623SJacob Keller ptp_port->tx.calibrating = true; 1322a69f1cb6SJacob Keller ptp_port->tx_fifo_busy_cnt = 0; 13233a749623SJacob Keller 13243a749623SJacob Keller /* Start the PHY timer in bypass mode */ 13253a749623SJacob Keller err = ice_start_phy_timer_e822(hw, port, true); 13263a749623SJacob Keller if (err) 13273a749623SJacob Keller goto out_unlock; 13283a749623SJacob Keller 13293a749623SJacob Keller /* Enable Tx timestamps right away */ 13303a749623SJacob Keller ptp_port->tx.calibrating = false; 13313a749623SJacob Keller 1332a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &ptp_port->ov_work, 0); 1333a69f1cb6SJacob Keller 13343a749623SJacob Keller out_unlock: 13353a749623SJacob Keller if (err) 13363a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d up, err %d\n", 13373a749623SJacob Keller port, err); 13383a749623SJacob Keller 13393a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock); 13403a749623SJacob Keller 13413a749623SJacob Keller return err; 13423a749623SJacob Keller } 13433a749623SJacob Keller 13443a749623SJacob Keller /** 13453a749623SJacob Keller * ice_ptp_link_change - Set or clear port registers for timestamping 13463a749623SJacob Keller * @pf: Board private structure 13473a749623SJacob Keller * @port: Port for which the PHY start is set 13483a749623SJacob Keller * @linkup: Link is up or down 13493a749623SJacob Keller */ 13503a749623SJacob Keller int ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup) 13513a749623SJacob Keller { 13523a749623SJacob Keller struct ice_ptp_port *ptp_port; 13533a749623SJacob Keller 13543a749623SJacob Keller if (!test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags)) 13553a749623SJacob Keller return 0; 13563a749623SJacob Keller 13573a749623SJacob Keller if (port >= ICE_NUM_EXTERNAL_PORTS) 13583a749623SJacob Keller return -EINVAL; 13593a749623SJacob Keller 13603a749623SJacob Keller ptp_port = &pf->ptp.port; 13613a749623SJacob Keller if (ptp_port->port_num != port) 13623a749623SJacob Keller return -EINVAL; 13633a749623SJacob Keller 13643a749623SJacob Keller /* Update cached link err for this port immediately */ 13653a749623SJacob Keller ptp_port->link_up = linkup; 13663a749623SJacob Keller 13673a749623SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 13683a749623SJacob Keller /* PTP is not setup */ 13693a749623SJacob Keller return -EAGAIN; 13703a749623SJacob Keller 13713a749623SJacob Keller return ice_ptp_port_phy_restart(ptp_port); 13723a749623SJacob Keller } 13733a749623SJacob Keller 13743a749623SJacob Keller /** 13753a749623SJacob Keller * ice_ptp_reset_ts_memory - Reset timestamp memory for all quads 13763a749623SJacob Keller * @pf: The PF private data structure 13773a749623SJacob Keller */ 13783a749623SJacob Keller static void ice_ptp_reset_ts_memory(struct ice_pf *pf) 13793a749623SJacob Keller { 13803a749623SJacob Keller int quad; 13813a749623SJacob Keller 13823a749623SJacob Keller quad = pf->hw.port_info->lport / ICE_PORTS_PER_QUAD; 13833a749623SJacob Keller ice_ptp_reset_ts_memory_quad(pf, quad); 13843a749623SJacob Keller } 13853a749623SJacob Keller 13863a749623SJacob Keller /** 13873a749623SJacob Keller * ice_ptp_tx_ena_intr - Enable or disable the Tx timestamp interrupt 13883a749623SJacob Keller * @pf: PF private structure 13893a749623SJacob Keller * @ena: bool value to enable or disable interrupt 13903a749623SJacob Keller * @threshold: Minimum number of packets at which intr is triggered 13913a749623SJacob Keller * 13923a749623SJacob Keller * Utility function to enable or disable Tx timestamp interrupt and threshold 13933a749623SJacob Keller */ 13943a749623SJacob Keller static int ice_ptp_tx_ena_intr(struct ice_pf *pf, bool ena, u32 threshold) 13953a749623SJacob Keller { 13963a749623SJacob Keller struct ice_hw *hw = &pf->hw; 13973a749623SJacob Keller int err = 0; 13983a749623SJacob Keller int quad; 13993a749623SJacob Keller u32 val; 14003a749623SJacob Keller 14013a749623SJacob Keller ice_ptp_reset_ts_memory(pf); 14023a749623SJacob Keller 14033a749623SJacob Keller for (quad = 0; quad < ICE_MAX_QUAD; quad++) { 14043a749623SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, 14053a749623SJacob Keller &val); 14063a749623SJacob Keller if (err) 14073a749623SJacob Keller break; 14083a749623SJacob Keller 14093a749623SJacob Keller if (ena) { 14103a749623SJacob Keller val |= Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M; 14113a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_THR_M; 14123a749623SJacob Keller val |= ((threshold << Q_REG_TX_MEM_GBL_CFG_INTR_THR_S) & 14133a749623SJacob Keller Q_REG_TX_MEM_GBL_CFG_INTR_THR_M); 14143a749623SJacob Keller } else { 14153a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M; 14163a749623SJacob Keller } 14173a749623SJacob Keller 14183a749623SJacob Keller err = ice_write_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, 14193a749623SJacob Keller val); 14203a749623SJacob Keller if (err) 14213a749623SJacob Keller break; 14223a749623SJacob Keller } 14233a749623SJacob Keller 14243a749623SJacob Keller if (err) 14253a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed in intr ena, err %d\n", 14263a749623SJacob Keller err); 14273a749623SJacob Keller return err; 14283a749623SJacob Keller } 14293a749623SJacob Keller 14303a749623SJacob Keller /** 14313a749623SJacob Keller * ice_ptp_reset_phy_timestamping - Reset PHY timestamping block 14323a749623SJacob Keller * @pf: Board private structure 14333a749623SJacob Keller */ 14343a749623SJacob Keller static void ice_ptp_reset_phy_timestamping(struct ice_pf *pf) 14353a749623SJacob Keller { 14363a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port); 143778267d0cSJacob Keller } 143878267d0cSJacob Keller 143978267d0cSJacob Keller /** 144006c16d89SJacob Keller * ice_ptp_adjfine - Adjust clock increment rate 144106c16d89SJacob Keller * @info: the driver's PTP info structure 144206c16d89SJacob Keller * @scaled_ppm: Parts per million with 16-bit fractional field 144306c16d89SJacob Keller * 144406c16d89SJacob Keller * Adjust the frequency of the clock by the indicated scaled ppm from the 144506c16d89SJacob Keller * base frequency. 144606c16d89SJacob Keller */ 144706c16d89SJacob Keller static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm) 144806c16d89SJacob Keller { 144906c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 145006c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 14514488df14SJacob Keller u64 incval, diff; 145206c16d89SJacob Keller int neg_adj = 0; 145306c16d89SJacob Keller int err; 145406c16d89SJacob Keller 145578267d0cSJacob Keller incval = ice_base_incval(pf); 145606c16d89SJacob Keller 145706c16d89SJacob Keller if (scaled_ppm < 0) { 145806c16d89SJacob Keller neg_adj = 1; 145906c16d89SJacob Keller scaled_ppm = -scaled_ppm; 146006c16d89SJacob Keller } 146106c16d89SJacob Keller 14624488df14SJacob Keller diff = mul_u64_u64_div_u64(incval, (u64)scaled_ppm, 14634488df14SJacob Keller 1000000ULL << 16); 146406c16d89SJacob Keller if (neg_adj) 146506c16d89SJacob Keller incval -= diff; 146606c16d89SJacob Keller else 146706c16d89SJacob Keller incval += diff; 146806c16d89SJacob Keller 146906c16d89SJacob Keller err = ice_ptp_write_incval_locked(hw, incval); 147006c16d89SJacob Keller if (err) { 147106c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n", 147206c16d89SJacob Keller err); 147306c16d89SJacob Keller return -EIO; 147406c16d89SJacob Keller } 147506c16d89SJacob Keller 147606c16d89SJacob Keller return 0; 147706c16d89SJacob Keller } 147806c16d89SJacob Keller 147906c16d89SJacob Keller /** 1480172db5f9SMaciej Machnikowski * ice_ptp_extts_work - Workqueue task function 1481172db5f9SMaciej Machnikowski * @work: external timestamp work structure 1482172db5f9SMaciej Machnikowski * 1483172db5f9SMaciej Machnikowski * Service for PTP external clock event 1484172db5f9SMaciej Machnikowski */ 1485172db5f9SMaciej Machnikowski static void ice_ptp_extts_work(struct kthread_work *work) 1486172db5f9SMaciej Machnikowski { 1487172db5f9SMaciej Machnikowski struct ice_ptp *ptp = container_of(work, struct ice_ptp, extts_work); 1488172db5f9SMaciej Machnikowski struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 1489172db5f9SMaciej Machnikowski struct ptp_clock_event event; 1490172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1491172db5f9SMaciej Machnikowski u8 chan, tmr_idx; 1492172db5f9SMaciej Machnikowski u32 hi, lo; 1493172db5f9SMaciej Machnikowski 1494172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1495172db5f9SMaciej Machnikowski /* Event time is captured by one of the two matched registers 1496172db5f9SMaciej Machnikowski * GLTSYN_EVNT_L: 32 LSB of sampled time event 1497172db5f9SMaciej Machnikowski * GLTSYN_EVNT_H: 32 MSB of sampled time event 1498172db5f9SMaciej Machnikowski * Event is defined in GLTSYN_EVNT_0 register 1499172db5f9SMaciej Machnikowski */ 1500172db5f9SMaciej Machnikowski for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) { 1501172db5f9SMaciej Machnikowski /* Check if channel is enabled */ 1502172db5f9SMaciej Machnikowski if (pf->ptp.ext_ts_irq & (1 << chan)) { 1503172db5f9SMaciej Machnikowski lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx)); 1504172db5f9SMaciej Machnikowski hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx)); 1505172db5f9SMaciej Machnikowski event.timestamp = (((u64)hi) << 32) | lo; 1506172db5f9SMaciej Machnikowski event.type = PTP_CLOCK_EXTTS; 1507172db5f9SMaciej Machnikowski event.index = chan; 1508172db5f9SMaciej Machnikowski 1509172db5f9SMaciej Machnikowski /* Fire event */ 1510172db5f9SMaciej Machnikowski ptp_clock_event(pf->ptp.clock, &event); 1511172db5f9SMaciej Machnikowski pf->ptp.ext_ts_irq &= ~(1 << chan); 1512172db5f9SMaciej Machnikowski } 1513172db5f9SMaciej Machnikowski } 1514172db5f9SMaciej Machnikowski } 1515172db5f9SMaciej Machnikowski 1516172db5f9SMaciej Machnikowski /** 1517172db5f9SMaciej Machnikowski * ice_ptp_cfg_extts - Configure EXTTS pin and channel 1518172db5f9SMaciej Machnikowski * @pf: Board private structure 1519172db5f9SMaciej Machnikowski * @ena: true to enable; false to disable 1520172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 1521172db5f9SMaciej Machnikowski * @gpio_pin: GPIO pin 1522172db5f9SMaciej Machnikowski * @extts_flags: request flags from the ptp_extts_request.flags 1523172db5f9SMaciej Machnikowski */ 1524172db5f9SMaciej Machnikowski static int 1525172db5f9SMaciej Machnikowski ice_ptp_cfg_extts(struct ice_pf *pf, bool ena, unsigned int chan, u32 gpio_pin, 1526172db5f9SMaciej Machnikowski unsigned int extts_flags) 1527172db5f9SMaciej Machnikowski { 1528172db5f9SMaciej Machnikowski u32 func, aux_reg, gpio_reg, irq_reg; 1529172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1530172db5f9SMaciej Machnikowski u8 tmr_idx; 1531172db5f9SMaciej Machnikowski 1532172db5f9SMaciej Machnikowski if (chan > (unsigned int)pf->ptp.info.n_ext_ts) 1533172db5f9SMaciej Machnikowski return -EINVAL; 1534172db5f9SMaciej Machnikowski 1535172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1536172db5f9SMaciej Machnikowski 1537172db5f9SMaciej Machnikowski irq_reg = rd32(hw, PFINT_OICR_ENA); 1538172db5f9SMaciej Machnikowski 1539172db5f9SMaciej Machnikowski if (ena) { 1540172db5f9SMaciej Machnikowski /* Enable the interrupt */ 1541172db5f9SMaciej Machnikowski irq_reg |= PFINT_OICR_TSYN_EVNT_M; 1542172db5f9SMaciej Machnikowski aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M; 1543172db5f9SMaciej Machnikowski 1544172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE BIT(0) 1545172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE BIT(1) 1546172db5f9SMaciej Machnikowski 1547172db5f9SMaciej Machnikowski /* set event level to requested edge */ 1548172db5f9SMaciej Machnikowski if (extts_flags & PTP_FALLING_EDGE) 1549172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE; 1550172db5f9SMaciej Machnikowski if (extts_flags & PTP_RISING_EDGE) 1551172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE; 1552172db5f9SMaciej Machnikowski 1553172db5f9SMaciej Machnikowski /* Write GPIO CTL reg. 1554172db5f9SMaciej Machnikowski * 0x1 is input sampled by EVENT register(channel) 1555172db5f9SMaciej Machnikowski * + num_in_channels * tmr_idx 1556172db5f9SMaciej Machnikowski */ 1557172db5f9SMaciej Machnikowski func = 1 + chan + (tmr_idx * 3); 1558172db5f9SMaciej Machnikowski gpio_reg = ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & 1559172db5f9SMaciej Machnikowski GLGEN_GPIO_CTL_PIN_FUNC_M); 1560172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan |= (1 << chan); 1561172db5f9SMaciej Machnikowski } else { 1562172db5f9SMaciej Machnikowski /* clear the values we set to reset defaults */ 1563172db5f9SMaciej Machnikowski aux_reg = 0; 1564172db5f9SMaciej Machnikowski gpio_reg = 0; 1565172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan &= ~(1 << chan); 1566172db5f9SMaciej Machnikowski if (!pf->ptp.ext_ts_chan) 1567172db5f9SMaciej Machnikowski irq_reg &= ~PFINT_OICR_TSYN_EVNT_M; 1568172db5f9SMaciej Machnikowski } 1569172db5f9SMaciej Machnikowski 1570172db5f9SMaciej Machnikowski wr32(hw, PFINT_OICR_ENA, irq_reg); 1571172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg); 1572172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg); 1573172db5f9SMaciej Machnikowski 1574172db5f9SMaciej Machnikowski return 0; 1575172db5f9SMaciej Machnikowski } 1576172db5f9SMaciej Machnikowski 1577172db5f9SMaciej Machnikowski /** 1578172db5f9SMaciej Machnikowski * ice_ptp_cfg_clkout - Configure clock to generate periodic wave 1579172db5f9SMaciej Machnikowski * @pf: Board private structure 1580172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 1581172db5f9SMaciej Machnikowski * @config: desired periodic clk configuration. NULL will disable channel 1582172db5f9SMaciej Machnikowski * @store: If set to true the values will be stored 1583172db5f9SMaciej Machnikowski * 1584172db5f9SMaciej Machnikowski * Configure the internal clock generator modules to generate the clock wave of 1585172db5f9SMaciej Machnikowski * specified period. 1586172db5f9SMaciej Machnikowski */ 1587172db5f9SMaciej Machnikowski static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan, 1588172db5f9SMaciej Machnikowski struct ice_perout_channel *config, bool store) 1589172db5f9SMaciej Machnikowski { 1590172db5f9SMaciej Machnikowski u64 current_time, period, start_time, phase; 1591172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1592172db5f9SMaciej Machnikowski u32 func, val, gpio_pin; 1593172db5f9SMaciej Machnikowski u8 tmr_idx; 1594172db5f9SMaciej Machnikowski 1595172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1596172db5f9SMaciej Machnikowski 1597172db5f9SMaciej Machnikowski /* 0. Reset mode & out_en in AUX_OUT */ 1598172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0); 1599172db5f9SMaciej Machnikowski 1600172db5f9SMaciej Machnikowski /* If we're disabling the output, clear out CLKO and TGT and keep 1601172db5f9SMaciej Machnikowski * output level low 1602172db5f9SMaciej Machnikowski */ 1603172db5f9SMaciej Machnikowski if (!config || !config->ena) { 1604172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), 0); 1605172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), 0); 1606172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), 0); 1607172db5f9SMaciej Machnikowski 1608172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M; 1609172db5f9SMaciej Machnikowski gpio_pin = pf->ptp.perout_channels[chan].gpio_pin; 1610172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 1611172db5f9SMaciej Machnikowski 1612172db5f9SMaciej Machnikowski /* Store the value if requested */ 1613172db5f9SMaciej Machnikowski if (store) 1614172db5f9SMaciej Machnikowski memset(&pf->ptp.perout_channels[chan], 0, 1615172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 1616172db5f9SMaciej Machnikowski 1617172db5f9SMaciej Machnikowski return 0; 1618172db5f9SMaciej Machnikowski } 1619172db5f9SMaciej Machnikowski period = config->period; 1620172db5f9SMaciej Machnikowski start_time = config->start_time; 1621172db5f9SMaciej Machnikowski div64_u64_rem(start_time, period, &phase); 1622172db5f9SMaciej Machnikowski gpio_pin = config->gpio_pin; 1623172db5f9SMaciej Machnikowski 1624172db5f9SMaciej Machnikowski /* 1. Write clkout with half of required period value */ 1625172db5f9SMaciej Machnikowski if (period & 0x1) { 1626172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n"); 1627172db5f9SMaciej Machnikowski goto err; 1628172db5f9SMaciej Machnikowski } 1629172db5f9SMaciej Machnikowski 1630172db5f9SMaciej Machnikowski period >>= 1; 1631172db5f9SMaciej Machnikowski 1632172db5f9SMaciej Machnikowski /* For proper operation, the GLTSYN_CLKO must be larger than clock tick 1633172db5f9SMaciej Machnikowski */ 1634172db5f9SMaciej Machnikowski #define MIN_PULSE 3 1635172db5f9SMaciej Machnikowski if (period <= MIN_PULSE || period > U32_MAX) { 1636172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be > %d && < 2^33", 1637172db5f9SMaciej Machnikowski MIN_PULSE * 2); 1638172db5f9SMaciej Machnikowski goto err; 1639172db5f9SMaciej Machnikowski } 1640172db5f9SMaciej Machnikowski 1641172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period)); 1642172db5f9SMaciej Machnikowski 1643172db5f9SMaciej Machnikowski /* Allow time for programming before start_time is hit */ 1644172db5f9SMaciej Machnikowski current_time = ice_ptp_read_src_clk_reg(pf, NULL); 1645172db5f9SMaciej Machnikowski 1646172db5f9SMaciej Machnikowski /* if start time is in the past start the timer at the nearest second 1647172db5f9SMaciej Machnikowski * maintaining phase 1648172db5f9SMaciej Machnikowski */ 1649172db5f9SMaciej Machnikowski if (start_time < current_time) 16505f773519SMaciej Machnikowski start_time = div64_u64(current_time + NSEC_PER_SEC - 1, 1651172db5f9SMaciej Machnikowski NSEC_PER_SEC) * NSEC_PER_SEC + phase; 1652172db5f9SMaciej Machnikowski 16533a749623SJacob Keller if (ice_is_e810(hw)) 1654172db5f9SMaciej Machnikowski start_time -= E810_OUT_PROP_DELAY_NS; 16553a749623SJacob Keller else 16563a749623SJacob Keller start_time -= ice_e822_pps_delay(ice_e822_time_ref(hw)); 1657172db5f9SMaciej Machnikowski 1658172db5f9SMaciej Machnikowski /* 2. Write TARGET time */ 1659172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start_time)); 1660172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start_time)); 1661172db5f9SMaciej Machnikowski 1662172db5f9SMaciej Machnikowski /* 3. Write AUX_OUT register */ 1663172db5f9SMaciej Machnikowski val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M; 1664172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val); 1665172db5f9SMaciej Machnikowski 1666172db5f9SMaciej Machnikowski /* 4. write GPIO CTL reg */ 1667172db5f9SMaciej Machnikowski func = 8 + chan + (tmr_idx * 4); 1668172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M | 1669172db5f9SMaciej Machnikowski ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & GLGEN_GPIO_CTL_PIN_FUNC_M); 1670172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 1671172db5f9SMaciej Machnikowski 1672172db5f9SMaciej Machnikowski /* Store the value if requested */ 1673172db5f9SMaciej Machnikowski if (store) { 1674172db5f9SMaciej Machnikowski memcpy(&pf->ptp.perout_channels[chan], config, 1675172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 1676172db5f9SMaciej Machnikowski pf->ptp.perout_channels[chan].start_time = phase; 1677172db5f9SMaciej Machnikowski } 1678172db5f9SMaciej Machnikowski 1679172db5f9SMaciej Machnikowski return 0; 1680172db5f9SMaciej Machnikowski err: 1681172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "PTP failed to cfg per_clk\n"); 1682172db5f9SMaciej Machnikowski return -EFAULT; 1683172db5f9SMaciej Machnikowski } 1684172db5f9SMaciej Machnikowski 1685172db5f9SMaciej Machnikowski /** 16869ee31343SJacob Keller * ice_ptp_disable_all_clkout - Disable all currently configured outputs 16879ee31343SJacob Keller * @pf: pointer to the PF structure 16889ee31343SJacob Keller * 16899ee31343SJacob Keller * Disable all currently configured clock outputs. This is necessary before 16909ee31343SJacob Keller * certain changes to the PTP hardware clock. Use ice_ptp_enable_all_clkout to 16919ee31343SJacob Keller * re-enable the clocks again. 16929ee31343SJacob Keller */ 16939ee31343SJacob Keller static void ice_ptp_disable_all_clkout(struct ice_pf *pf) 16949ee31343SJacob Keller { 16959ee31343SJacob Keller uint i; 16969ee31343SJacob Keller 16979ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++) 16989ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena) 16999ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, NULL, false); 17009ee31343SJacob Keller } 17019ee31343SJacob Keller 17029ee31343SJacob Keller /** 17039ee31343SJacob Keller * ice_ptp_enable_all_clkout - Enable all configured periodic clock outputs 17049ee31343SJacob Keller * @pf: pointer to the PF structure 17059ee31343SJacob Keller * 17069ee31343SJacob Keller * Enable all currently configured clock outputs. Use this after 17079ee31343SJacob Keller * ice_ptp_disable_all_clkout to reconfigure the output signals according to 17089ee31343SJacob Keller * their configuration. 17099ee31343SJacob Keller */ 17109ee31343SJacob Keller static void ice_ptp_enable_all_clkout(struct ice_pf *pf) 17119ee31343SJacob Keller { 17129ee31343SJacob Keller uint i; 17139ee31343SJacob Keller 17149ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++) 17159ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena) 17169ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, &pf->ptp.perout_channels[i], 17179ee31343SJacob Keller false); 17189ee31343SJacob Keller } 17199ee31343SJacob Keller 17209ee31343SJacob Keller /** 1721172db5f9SMaciej Machnikowski * ice_ptp_gpio_enable_e810 - Enable/disable ancillary features of PHC 1722172db5f9SMaciej Machnikowski * @info: the driver's PTP info structure 1723172db5f9SMaciej Machnikowski * @rq: The requested feature to change 1724172db5f9SMaciej Machnikowski * @on: Enable/disable flag 1725172db5f9SMaciej Machnikowski */ 1726172db5f9SMaciej Machnikowski static int 1727172db5f9SMaciej Machnikowski ice_ptp_gpio_enable_e810(struct ptp_clock_info *info, 1728172db5f9SMaciej Machnikowski struct ptp_clock_request *rq, int on) 1729172db5f9SMaciej Machnikowski { 1730172db5f9SMaciej Machnikowski struct ice_pf *pf = ptp_info_to_pf(info); 1731172db5f9SMaciej Machnikowski struct ice_perout_channel clk_cfg = {0}; 1732325b2064SMaciej Machnikowski bool sma_pres = false; 1733172db5f9SMaciej Machnikowski unsigned int chan; 1734172db5f9SMaciej Machnikowski u32 gpio_pin; 1735172db5f9SMaciej Machnikowski int err; 1736172db5f9SMaciej Machnikowski 1737325b2064SMaciej Machnikowski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) 1738325b2064SMaciej Machnikowski sma_pres = true; 1739325b2064SMaciej Machnikowski 1740172db5f9SMaciej Machnikowski switch (rq->type) { 1741172db5f9SMaciej Machnikowski case PTP_CLK_REQ_PEROUT: 1742172db5f9SMaciej Machnikowski chan = rq->perout.index; 1743325b2064SMaciej Machnikowski if (sma_pres) { 1744325b2064SMaciej Machnikowski if (chan == ice_pin_desc_e810t[SMA1].chan) 1745325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20; 1746325b2064SMaciej Machnikowski else if (chan == ice_pin_desc_e810t[SMA2].chan) 1747325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22; 1748172db5f9SMaciej Machnikowski else 1749325b2064SMaciej Machnikowski return -1; 1750325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) { 1751325b2064SMaciej Machnikowski if (chan == 0) 1752325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20; 1753325b2064SMaciej Machnikowski else 1754325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22; 1755325b2064SMaciej Machnikowski } else if (chan == PPS_CLK_GEN_CHAN) { 1756325b2064SMaciej Machnikowski clk_cfg.gpio_pin = PPS_PIN_INDEX; 1757325b2064SMaciej Machnikowski } else { 1758172db5f9SMaciej Machnikowski clk_cfg.gpio_pin = chan; 1759325b2064SMaciej Machnikowski } 1760172db5f9SMaciej Machnikowski 1761172db5f9SMaciej Machnikowski clk_cfg.period = ((rq->perout.period.sec * NSEC_PER_SEC) + 1762172db5f9SMaciej Machnikowski rq->perout.period.nsec); 1763172db5f9SMaciej Machnikowski clk_cfg.start_time = ((rq->perout.start.sec * NSEC_PER_SEC) + 1764172db5f9SMaciej Machnikowski rq->perout.start.nsec); 1765172db5f9SMaciej Machnikowski clk_cfg.ena = !!on; 1766172db5f9SMaciej Machnikowski 1767172db5f9SMaciej Machnikowski err = ice_ptp_cfg_clkout(pf, chan, &clk_cfg, true); 1768172db5f9SMaciej Machnikowski break; 1769172db5f9SMaciej Machnikowski case PTP_CLK_REQ_EXTTS: 1770172db5f9SMaciej Machnikowski chan = rq->extts.index; 1771325b2064SMaciej Machnikowski if (sma_pres) { 1772325b2064SMaciej Machnikowski if (chan < ice_pin_desc_e810t[SMA2].chan) 1773325b2064SMaciej Machnikowski gpio_pin = GPIO_21; 1774325b2064SMaciej Machnikowski else 1775325b2064SMaciej Machnikowski gpio_pin = GPIO_23; 1776325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) { 1777325b2064SMaciej Machnikowski if (chan == 0) 1778325b2064SMaciej Machnikowski gpio_pin = GPIO_21; 1779325b2064SMaciej Machnikowski else 1780325b2064SMaciej Machnikowski gpio_pin = GPIO_23; 1781325b2064SMaciej Machnikowski } else { 1782172db5f9SMaciej Machnikowski gpio_pin = chan; 1783325b2064SMaciej Machnikowski } 1784172db5f9SMaciej Machnikowski 1785172db5f9SMaciej Machnikowski err = ice_ptp_cfg_extts(pf, !!on, chan, gpio_pin, 1786172db5f9SMaciej Machnikowski rq->extts.flags); 1787172db5f9SMaciej Machnikowski break; 1788172db5f9SMaciej Machnikowski default: 1789172db5f9SMaciej Machnikowski return -EOPNOTSUPP; 1790172db5f9SMaciej Machnikowski } 1791172db5f9SMaciej Machnikowski 1792172db5f9SMaciej Machnikowski return err; 1793172db5f9SMaciej Machnikowski } 1794172db5f9SMaciej Machnikowski 1795172db5f9SMaciej Machnikowski /** 179606c16d89SJacob Keller * ice_ptp_gettimex64 - Get the time of the clock 179706c16d89SJacob Keller * @info: the driver's PTP info structure 179806c16d89SJacob Keller * @ts: timespec64 structure to hold the current time value 179906c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 180006c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 180106c16d89SJacob Keller * 180206c16d89SJacob Keller * Read the device clock and return the correct value on ns, after converting it 180306c16d89SJacob Keller * into a timespec struct. 180406c16d89SJacob Keller */ 180506c16d89SJacob Keller static int 180606c16d89SJacob Keller ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts, 180706c16d89SJacob Keller struct ptp_system_timestamp *sts) 180806c16d89SJacob Keller { 180906c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 181006c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 181106c16d89SJacob Keller 181206c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 181306c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n"); 181406c16d89SJacob Keller return -EBUSY; 181506c16d89SJacob Keller } 181606c16d89SJacob Keller 181706c16d89SJacob Keller ice_ptp_read_time(pf, ts, sts); 181806c16d89SJacob Keller ice_ptp_unlock(hw); 181906c16d89SJacob Keller 182006c16d89SJacob Keller return 0; 182106c16d89SJacob Keller } 182206c16d89SJacob Keller 182306c16d89SJacob Keller /** 182406c16d89SJacob Keller * ice_ptp_settime64 - Set the time of the clock 182506c16d89SJacob Keller * @info: the driver's PTP info structure 182606c16d89SJacob Keller * @ts: timespec64 structure that holds the new time value 182706c16d89SJacob Keller * 182806c16d89SJacob Keller * Set the device clock to the user input value. The conversion from timespec 182906c16d89SJacob Keller * to ns happens in the write function. 183006c16d89SJacob Keller */ 183106c16d89SJacob Keller static int 183206c16d89SJacob Keller ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts) 183306c16d89SJacob Keller { 183406c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 183506c16d89SJacob Keller struct timespec64 ts64 = *ts; 183606c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 183706c16d89SJacob Keller int err; 183806c16d89SJacob Keller 18393a749623SJacob Keller /* For Vernier mode, we need to recalibrate after new settime 18403a749623SJacob Keller * Start with disabling timestamp block 18413a749623SJacob Keller */ 18423a749623SJacob Keller if (pf->ptp.port.link_up) 18433a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port); 18443a749623SJacob Keller 184506c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 184606c16d89SJacob Keller err = -EBUSY; 184706c16d89SJacob Keller goto exit; 184806c16d89SJacob Keller } 184906c16d89SJacob Keller 18509ee31343SJacob Keller /* Disable periodic outputs */ 18519ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 18529ee31343SJacob Keller 185306c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts64); 185406c16d89SJacob Keller ice_ptp_unlock(hw); 185506c16d89SJacob Keller 185677a78115SJacob Keller if (!err) 1857b1a582e6SJacob Keller ice_ptp_reset_cached_phctime(pf); 185877a78115SJacob Keller 18599ee31343SJacob Keller /* Reenable periodic outputs */ 18609ee31343SJacob Keller ice_ptp_enable_all_clkout(pf); 18613a749623SJacob Keller 18623a749623SJacob Keller /* Recalibrate and re-enable timestamp block */ 18633a749623SJacob Keller if (pf->ptp.port.link_up) 18643a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port); 186506c16d89SJacob Keller exit: 186606c16d89SJacob Keller if (err) { 186706c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err); 186806c16d89SJacob Keller return err; 186906c16d89SJacob Keller } 187006c16d89SJacob Keller 187106c16d89SJacob Keller return 0; 187206c16d89SJacob Keller } 187306c16d89SJacob Keller 187406c16d89SJacob Keller /** 187506c16d89SJacob Keller * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment 187606c16d89SJacob Keller * @info: the driver's PTP info structure 187706c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 187806c16d89SJacob Keller */ 187906c16d89SJacob Keller static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta) 188006c16d89SJacob Keller { 188106c16d89SJacob Keller struct timespec64 now, then; 1882ed22d9c8STom Rix int ret; 188306c16d89SJacob Keller 188406c16d89SJacob Keller then = ns_to_timespec64(delta); 1885ed22d9c8STom Rix ret = ice_ptp_gettimex64(info, &now, NULL); 1886ed22d9c8STom Rix if (ret) 1887ed22d9c8STom Rix return ret; 188806c16d89SJacob Keller now = timespec64_add(now, then); 188906c16d89SJacob Keller 189006c16d89SJacob Keller return ice_ptp_settime64(info, (const struct timespec64 *)&now); 189106c16d89SJacob Keller } 189206c16d89SJacob Keller 189306c16d89SJacob Keller /** 189406c16d89SJacob Keller * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta 189506c16d89SJacob Keller * @info: the driver's PTP info structure 189606c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 189706c16d89SJacob Keller */ 189806c16d89SJacob Keller static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta) 189906c16d89SJacob Keller { 190006c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 190106c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 190206c16d89SJacob Keller struct device *dev; 190306c16d89SJacob Keller int err; 190406c16d89SJacob Keller 190506c16d89SJacob Keller dev = ice_pf_to_dev(pf); 190606c16d89SJacob Keller 190706c16d89SJacob Keller /* Hardware only supports atomic adjustments using signed 32-bit 190806c16d89SJacob Keller * integers. For any adjustment outside this range, perform 190906c16d89SJacob Keller * a non-atomic get->adjust->set flow. 191006c16d89SJacob Keller */ 191106c16d89SJacob Keller if (delta > S32_MAX || delta < S32_MIN) { 191206c16d89SJacob Keller dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta); 191306c16d89SJacob Keller return ice_ptp_adjtime_nonatomic(info, delta); 191406c16d89SJacob Keller } 191506c16d89SJacob Keller 191606c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 191706c16d89SJacob Keller dev_err(dev, "PTP failed to acquire semaphore in adjtime\n"); 191806c16d89SJacob Keller return -EBUSY; 191906c16d89SJacob Keller } 192006c16d89SJacob Keller 19219ee31343SJacob Keller /* Disable periodic outputs */ 19229ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 19239ee31343SJacob Keller 192406c16d89SJacob Keller err = ice_ptp_write_adj(pf, delta); 192506c16d89SJacob Keller 19269ee31343SJacob Keller /* Reenable periodic outputs */ 19279ee31343SJacob Keller ice_ptp_enable_all_clkout(pf); 19289ee31343SJacob Keller 192906c16d89SJacob Keller ice_ptp_unlock(hw); 193006c16d89SJacob Keller 193106c16d89SJacob Keller if (err) { 193206c16d89SJacob Keller dev_err(dev, "PTP failed to adjust time, err %d\n", err); 193306c16d89SJacob Keller return err; 193406c16d89SJacob Keller } 193506c16d89SJacob Keller 1936b1a582e6SJacob Keller ice_ptp_reset_cached_phctime(pf); 193777a78115SJacob Keller 193806c16d89SJacob Keller return 0; 193906c16d89SJacob Keller } 194006c16d89SJacob Keller 194113a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS 194213a64f0bSJacob Keller /** 194313a64f0bSJacob Keller * ice_ptp_get_syncdevicetime - Get the cross time stamp info 194413a64f0bSJacob Keller * @device: Current device time 194513a64f0bSJacob Keller * @system: System counter value read synchronously with device time 194613a64f0bSJacob Keller * @ctx: Context provided by timekeeping code 194713a64f0bSJacob Keller * 194813a64f0bSJacob Keller * Read device and system (ART) clock simultaneously and return the corrected 194913a64f0bSJacob Keller * clock values in ns. 195013a64f0bSJacob Keller */ 195113a64f0bSJacob Keller static int 195213a64f0bSJacob Keller ice_ptp_get_syncdevicetime(ktime_t *device, 195313a64f0bSJacob Keller struct system_counterval_t *system, 195413a64f0bSJacob Keller void *ctx) 195513a64f0bSJacob Keller { 195613a64f0bSJacob Keller struct ice_pf *pf = (struct ice_pf *)ctx; 195713a64f0bSJacob Keller struct ice_hw *hw = &pf->hw; 195813a64f0bSJacob Keller u32 hh_lock, hh_art_ctl; 195913a64f0bSJacob Keller int i; 196013a64f0bSJacob Keller 196113a64f0bSJacob Keller /* Get the HW lock */ 196213a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id)); 196313a64f0bSJacob Keller if (hh_lock & PFHH_SEM_BUSY_M) { 196413a64f0bSJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get hh lock\n"); 196513a64f0bSJacob Keller return -EFAULT; 196613a64f0bSJacob Keller } 196713a64f0bSJacob Keller 196813a64f0bSJacob Keller /* Start the ART and device clock sync sequence */ 196913a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL); 197013a64f0bSJacob Keller hh_art_ctl = hh_art_ctl | GLHH_ART_CTL_ACTIVE_M; 197113a64f0bSJacob Keller wr32(hw, GLHH_ART_CTL, hh_art_ctl); 197213a64f0bSJacob Keller 197313a64f0bSJacob Keller #define MAX_HH_LOCK_TRIES 100 197413a64f0bSJacob Keller 197513a64f0bSJacob Keller for (i = 0; i < MAX_HH_LOCK_TRIES; i++) { 197613a64f0bSJacob Keller /* Wait for sync to complete */ 197713a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL); 197813a64f0bSJacob Keller if (hh_art_ctl & GLHH_ART_CTL_ACTIVE_M) { 197913a64f0bSJacob Keller udelay(1); 198013a64f0bSJacob Keller continue; 198113a64f0bSJacob Keller } else { 198213a64f0bSJacob Keller u32 hh_ts_lo, hh_ts_hi, tmr_idx; 198313a64f0bSJacob Keller u64 hh_ts; 198413a64f0bSJacob Keller 198513a64f0bSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 198613a64f0bSJacob Keller /* Read ART time */ 198713a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLHH_ART_TIME_L); 198813a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLHH_ART_TIME_H); 198913a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo; 199013a64f0bSJacob Keller *system = convert_art_ns_to_tsc(hh_ts); 199113a64f0bSJacob Keller /* Read Device source clock time */ 199213a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLTSYN_HHTIME_L(tmr_idx)); 199313a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLTSYN_HHTIME_H(tmr_idx)); 199413a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo; 199513a64f0bSJacob Keller *device = ns_to_ktime(hh_ts); 199613a64f0bSJacob Keller break; 199713a64f0bSJacob Keller } 199813a64f0bSJacob Keller } 199913a64f0bSJacob Keller /* Release HW lock */ 200013a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id)); 200113a64f0bSJacob Keller hh_lock = hh_lock & ~PFHH_SEM_BUSY_M; 200213a64f0bSJacob Keller wr32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), hh_lock); 200313a64f0bSJacob Keller 200413a64f0bSJacob Keller if (i == MAX_HH_LOCK_TRIES) 200513a64f0bSJacob Keller return -ETIMEDOUT; 200613a64f0bSJacob Keller 200713a64f0bSJacob Keller return 0; 200813a64f0bSJacob Keller } 200913a64f0bSJacob Keller 201013a64f0bSJacob Keller /** 201113a64f0bSJacob Keller * ice_ptp_getcrosststamp_e822 - Capture a device cross timestamp 201213a64f0bSJacob Keller * @info: the driver's PTP info structure 201313a64f0bSJacob Keller * @cts: The memory to fill the cross timestamp info 201413a64f0bSJacob Keller * 201513a64f0bSJacob Keller * Capture a cross timestamp between the ART and the device PTP hardware 201613a64f0bSJacob Keller * clock. Fill the cross timestamp information and report it back to the 201713a64f0bSJacob Keller * caller. 201813a64f0bSJacob Keller * 201913a64f0bSJacob Keller * This is only valid for E822 devices which have support for generating the 202013a64f0bSJacob Keller * cross timestamp via PCIe PTM. 202113a64f0bSJacob Keller * 202213a64f0bSJacob Keller * In order to correctly correlate the ART timestamp back to the TSC time, the 202313a64f0bSJacob Keller * CPU must have X86_FEATURE_TSC_KNOWN_FREQ. 202413a64f0bSJacob Keller */ 202513a64f0bSJacob Keller static int 202613a64f0bSJacob Keller ice_ptp_getcrosststamp_e822(struct ptp_clock_info *info, 202713a64f0bSJacob Keller struct system_device_crosststamp *cts) 202813a64f0bSJacob Keller { 202913a64f0bSJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 203013a64f0bSJacob Keller 203113a64f0bSJacob Keller return get_device_system_crosststamp(ice_ptp_get_syncdevicetime, 203213a64f0bSJacob Keller pf, NULL, cts); 203313a64f0bSJacob Keller } 203413a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */ 203513a64f0bSJacob Keller 203606c16d89SJacob Keller /** 203777a78115SJacob Keller * ice_ptp_get_ts_config - ioctl interface to read the timestamping config 203877a78115SJacob Keller * @pf: Board private structure 203977a78115SJacob Keller * @ifr: ioctl data 204077a78115SJacob Keller * 204177a78115SJacob Keller * Copy the timestamping config to user buffer 204277a78115SJacob Keller */ 204377a78115SJacob Keller int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr) 204477a78115SJacob Keller { 204577a78115SJacob Keller struct hwtstamp_config *config; 204677a78115SJacob Keller 204777a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 204877a78115SJacob Keller return -EIO; 204977a78115SJacob Keller 205077a78115SJacob Keller config = &pf->ptp.tstamp_config; 205177a78115SJacob Keller 205277a78115SJacob Keller return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ? 205377a78115SJacob Keller -EFAULT : 0; 205477a78115SJacob Keller } 205577a78115SJacob Keller 205677a78115SJacob Keller /** 205777a78115SJacob Keller * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode 205877a78115SJacob Keller * @pf: Board private structure 205977a78115SJacob Keller * @config: hwtstamp settings requested or saved 206077a78115SJacob Keller */ 206177a78115SJacob Keller static int 206277a78115SJacob Keller ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config) 206377a78115SJacob Keller { 206477a78115SJacob Keller switch (config->tx_type) { 206577a78115SJacob Keller case HWTSTAMP_TX_OFF: 2066ea9b847cSJacob Keller ice_set_tx_tstamp(pf, false); 2067ea9b847cSJacob Keller break; 2068ea9b847cSJacob Keller case HWTSTAMP_TX_ON: 2069ea9b847cSJacob Keller ice_set_tx_tstamp(pf, true); 207077a78115SJacob Keller break; 207177a78115SJacob Keller default: 207277a78115SJacob Keller return -ERANGE; 207377a78115SJacob Keller } 207477a78115SJacob Keller 207577a78115SJacob Keller switch (config->rx_filter) { 207677a78115SJacob Keller case HWTSTAMP_FILTER_NONE: 207777a78115SJacob Keller ice_set_rx_tstamp(pf, false); 207877a78115SJacob Keller break; 207977a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 208077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 208177a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 208277a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_EVENT: 208377a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 208477a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 208577a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_SYNC: 208677a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 208777a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 208877a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 208977a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 209077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 209177a78115SJacob Keller case HWTSTAMP_FILTER_NTP_ALL: 209277a78115SJacob Keller case HWTSTAMP_FILTER_ALL: 209377a78115SJacob Keller ice_set_rx_tstamp(pf, true); 209477a78115SJacob Keller break; 209577a78115SJacob Keller default: 209677a78115SJacob Keller return -ERANGE; 209777a78115SJacob Keller } 209877a78115SJacob Keller 209977a78115SJacob Keller return 0; 210077a78115SJacob Keller } 210177a78115SJacob Keller 210277a78115SJacob Keller /** 210377a78115SJacob Keller * ice_ptp_set_ts_config - ioctl interface to control the timestamping 210477a78115SJacob Keller * @pf: Board private structure 210577a78115SJacob Keller * @ifr: ioctl data 210677a78115SJacob Keller * 210777a78115SJacob Keller * Get the user config and store it 210877a78115SJacob Keller */ 210977a78115SJacob Keller int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr) 211077a78115SJacob Keller { 211177a78115SJacob Keller struct hwtstamp_config config; 211277a78115SJacob Keller int err; 211377a78115SJacob Keller 211477a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 211577a78115SJacob Keller return -EAGAIN; 211677a78115SJacob Keller 211777a78115SJacob Keller if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 211877a78115SJacob Keller return -EFAULT; 211977a78115SJacob Keller 212077a78115SJacob Keller err = ice_ptp_set_timestamp_mode(pf, &config); 212177a78115SJacob Keller if (err) 212277a78115SJacob Keller return err; 212377a78115SJacob Keller 2124e59d75ddSJacob Keller /* Return the actual configuration set */ 2125e59d75ddSJacob Keller config = pf->ptp.tstamp_config; 212677a78115SJacob Keller 212777a78115SJacob Keller return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 212877a78115SJacob Keller -EFAULT : 0; 212977a78115SJacob Keller } 213077a78115SJacob Keller 213177a78115SJacob Keller /** 213277a78115SJacob Keller * ice_ptp_rx_hwtstamp - Check for an Rx timestamp 213377a78115SJacob Keller * @rx_ring: Ring to get the VSI info 213477a78115SJacob Keller * @rx_desc: Receive descriptor 213577a78115SJacob Keller * @skb: Particular skb to send timestamp with 213677a78115SJacob Keller * 213777a78115SJacob Keller * The driver receives a notification in the receive descriptor with timestamp. 213877a78115SJacob Keller * The timestamp is in ns, so we must convert the result first. 213977a78115SJacob Keller */ 214077a78115SJacob Keller void 2141e72bba21SMaciej Fijalkowski ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring, 214277a78115SJacob Keller union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) 214377a78115SJacob Keller { 214477a78115SJacob Keller struct skb_shared_hwtstamps *hwtstamps; 2145b1a582e6SJacob Keller u64 ts_ns, cached_time; 2146b1a582e6SJacob Keller u32 ts_high; 214777a78115SJacob Keller 2148b1a582e6SJacob Keller if (!(rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID)) 2149b1a582e6SJacob Keller return; 2150b1a582e6SJacob Keller 2151b1a582e6SJacob Keller cached_time = READ_ONCE(rx_ring->cached_phctime); 2152b1a582e6SJacob Keller 2153b1a582e6SJacob Keller /* Do not report a timestamp if we don't have a cached PHC time */ 2154b1a582e6SJacob Keller if (!cached_time) 2155b1a582e6SJacob Keller return; 2156b1a582e6SJacob Keller 2157b1a582e6SJacob Keller /* Use ice_ptp_extend_32b_ts directly, using the ring-specific cached 2158b1a582e6SJacob Keller * PHC value, rather than accessing the PF. This also allows us to 2159b1a582e6SJacob Keller * simply pass the upper 32bits of nanoseconds directly. Calling 2160b1a582e6SJacob Keller * ice_ptp_extend_40b_ts is unnecessary as it would just discard these 2161b1a582e6SJacob Keller * bits itself. 216277a78115SJacob Keller */ 216377a78115SJacob Keller ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high); 2164b1a582e6SJacob Keller ts_ns = ice_ptp_extend_32b_ts(cached_time, ts_high); 216577a78115SJacob Keller 216677a78115SJacob Keller hwtstamps = skb_hwtstamps(skb); 216777a78115SJacob Keller memset(hwtstamps, 0, sizeof(*hwtstamps)); 216877a78115SJacob Keller hwtstamps->hwtstamp = ns_to_ktime(ts_ns); 216977a78115SJacob Keller } 217077a78115SJacob Keller 217177a78115SJacob Keller /** 2172325b2064SMaciej Machnikowski * ice_ptp_disable_sma_pins_e810t - Disable E810-T SMA pins 2173325b2064SMaciej Machnikowski * @pf: pointer to the PF structure 2174325b2064SMaciej Machnikowski * @info: PTP clock info structure 2175325b2064SMaciej Machnikowski * 2176325b2064SMaciej Machnikowski * Disable the OS access to the SMA pins. Called to clear out the OS 2177325b2064SMaciej Machnikowski * indications of pin support when we fail to setup the E810-T SMA control 2178325b2064SMaciej Machnikowski * register. 2179325b2064SMaciej Machnikowski */ 2180325b2064SMaciej Machnikowski static void 2181325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 2182325b2064SMaciej Machnikowski { 2183325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf); 2184325b2064SMaciej Machnikowski 2185325b2064SMaciej Machnikowski dev_warn(dev, "Failed to configure E810-T SMA pin control\n"); 2186325b2064SMaciej Machnikowski 2187325b2064SMaciej Machnikowski info->enable = NULL; 2188325b2064SMaciej Machnikowski info->verify = NULL; 2189325b2064SMaciej Machnikowski info->n_pins = 0; 2190325b2064SMaciej Machnikowski info->n_ext_ts = 0; 2191325b2064SMaciej Machnikowski info->n_per_out = 0; 2192325b2064SMaciej Machnikowski } 2193325b2064SMaciej Machnikowski 2194325b2064SMaciej Machnikowski /** 2195325b2064SMaciej Machnikowski * ice_ptp_setup_sma_pins_e810t - Setup the SMA pins 2196325b2064SMaciej Machnikowski * @pf: pointer to the PF structure 2197325b2064SMaciej Machnikowski * @info: PTP clock info structure 2198325b2064SMaciej Machnikowski * 2199325b2064SMaciej Machnikowski * Finish setting up the SMA pins by allocating pin_config, and setting it up 2200325b2064SMaciej Machnikowski * according to the current status of the SMA. On failure, disable all of the 2201325b2064SMaciej Machnikowski * extended SMA pin support. 2202325b2064SMaciej Machnikowski */ 2203325b2064SMaciej Machnikowski static void 2204325b2064SMaciej Machnikowski ice_ptp_setup_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 2205325b2064SMaciej Machnikowski { 2206325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf); 2207325b2064SMaciej Machnikowski int err; 2208325b2064SMaciej Machnikowski 2209325b2064SMaciej Machnikowski /* Allocate memory for kernel pins interface */ 2210325b2064SMaciej Machnikowski info->pin_config = devm_kcalloc(dev, info->n_pins, 2211325b2064SMaciej Machnikowski sizeof(*info->pin_config), GFP_KERNEL); 2212325b2064SMaciej Machnikowski if (!info->pin_config) { 2213325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info); 2214325b2064SMaciej Machnikowski return; 2215325b2064SMaciej Machnikowski } 2216325b2064SMaciej Machnikowski 2217325b2064SMaciej Machnikowski /* Read current SMA status */ 2218325b2064SMaciej Machnikowski err = ice_get_sma_config_e810t(&pf->hw, info->pin_config); 2219325b2064SMaciej Machnikowski if (err) 2220325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info); 2221325b2064SMaciej Machnikowski } 2222325b2064SMaciej Machnikowski 2223325b2064SMaciej Machnikowski /** 2224325b2064SMaciej Machnikowski * ice_ptp_setup_pins_e810t - Setup PTP pins in sysfs 2225325b2064SMaciej Machnikowski * @pf: pointer to the PF instance 2226325b2064SMaciej Machnikowski * @info: PTP clock capabilities 2227325b2064SMaciej Machnikowski */ 2228325b2064SMaciej Machnikowski static void 2229325b2064SMaciej Machnikowski ice_ptp_setup_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 2230325b2064SMaciej Machnikowski { 2231325b2064SMaciej Machnikowski /* Check if SMA controller is in the netlist */ 2232325b2064SMaciej Machnikowski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL) && 2233325b2064SMaciej Machnikowski !ice_is_pca9575_present(&pf->hw)) 2234325b2064SMaciej Machnikowski ice_clear_feature_support(pf, ICE_F_SMA_CTRL); 2235325b2064SMaciej Machnikowski 2236325b2064SMaciej Machnikowski if (!ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) { 2237325b2064SMaciej Machnikowski info->n_ext_ts = N_EXT_TS_E810_NO_SMA; 2238325b2064SMaciej Machnikowski info->n_per_out = N_PER_OUT_E810T_NO_SMA; 2239325b2064SMaciej Machnikowski return; 2240325b2064SMaciej Machnikowski } 2241325b2064SMaciej Machnikowski 2242325b2064SMaciej Machnikowski info->n_per_out = N_PER_OUT_E810T; 2243896a55aaSAnirudh Venkataramanan 2244896a55aaSAnirudh Venkataramanan if (ice_is_feature_supported(pf, ICE_F_PTP_EXTTS)) { 2245325b2064SMaciej Machnikowski info->n_ext_ts = N_EXT_TS_E810; 2246325b2064SMaciej Machnikowski info->n_pins = NUM_PTP_PINS_E810T; 2247325b2064SMaciej Machnikowski info->verify = ice_verify_pin_e810t; 2248896a55aaSAnirudh Venkataramanan } 2249325b2064SMaciej Machnikowski 2250325b2064SMaciej Machnikowski /* Complete setup of the SMA pins */ 2251325b2064SMaciej Machnikowski ice_ptp_setup_sma_pins_e810t(pf, info); 2252325b2064SMaciej Machnikowski } 2253325b2064SMaciej Machnikowski 2254325b2064SMaciej Machnikowski /** 2255172db5f9SMaciej Machnikowski * ice_ptp_setup_pins_e810 - Setup PTP pins in sysfs 2256896a55aaSAnirudh Venkataramanan * @pf: pointer to the PF instance 2257172db5f9SMaciej Machnikowski * @info: PTP clock capabilities 2258172db5f9SMaciej Machnikowski */ 2259896a55aaSAnirudh Venkataramanan static void ice_ptp_setup_pins_e810(struct ice_pf *pf, struct ptp_clock_info *info) 2260172db5f9SMaciej Machnikowski { 2261325b2064SMaciej Machnikowski info->n_per_out = N_PER_OUT_E810; 2262896a55aaSAnirudh Venkataramanan 2263896a55aaSAnirudh Venkataramanan if (!ice_is_feature_supported(pf, ICE_F_PTP_EXTTS)) 2264896a55aaSAnirudh Venkataramanan return; 2265896a55aaSAnirudh Venkataramanan 2266325b2064SMaciej Machnikowski info->n_ext_ts = N_EXT_TS_E810; 2267172db5f9SMaciej Machnikowski } 2268172db5f9SMaciej Machnikowski 2269172db5f9SMaciej Machnikowski /** 227013a64f0bSJacob Keller * ice_ptp_set_funcs_e822 - Set specialized functions for E822 support 227113a64f0bSJacob Keller * @pf: Board private structure 227213a64f0bSJacob Keller * @info: PTP info to fill 227313a64f0bSJacob Keller * 227413a64f0bSJacob Keller * Assign functions to the PTP capabiltiies structure for E822 devices. 227513a64f0bSJacob Keller * Functions which operate across all device families should be set directly 227613a64f0bSJacob Keller * in ice_ptp_set_caps. Only add functions here which are distinct for E822 227713a64f0bSJacob Keller * devices. 227813a64f0bSJacob Keller */ 227913a64f0bSJacob Keller static void 228013a64f0bSJacob Keller ice_ptp_set_funcs_e822(struct ice_pf *pf, struct ptp_clock_info *info) 228113a64f0bSJacob Keller { 228213a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS 228313a64f0bSJacob Keller if (boot_cpu_has(X86_FEATURE_ART) && 228413a64f0bSJacob Keller boot_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ)) 228513a64f0bSJacob Keller info->getcrosststamp = ice_ptp_getcrosststamp_e822; 228613a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */ 228713a64f0bSJacob Keller } 228813a64f0bSJacob Keller 228913a64f0bSJacob Keller /** 2290172db5f9SMaciej Machnikowski * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support 2291172db5f9SMaciej Machnikowski * @pf: Board private structure 2292172db5f9SMaciej Machnikowski * @info: PTP info to fill 2293172db5f9SMaciej Machnikowski * 2294172db5f9SMaciej Machnikowski * Assign functions to the PTP capabiltiies structure for E810 devices. 2295172db5f9SMaciej Machnikowski * Functions which operate across all device families should be set directly 2296172db5f9SMaciej Machnikowski * in ice_ptp_set_caps. Only add functions here which are distinct for e810 2297172db5f9SMaciej Machnikowski * devices. 2298172db5f9SMaciej Machnikowski */ 2299172db5f9SMaciej Machnikowski static void 2300172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(struct ice_pf *pf, struct ptp_clock_info *info) 2301172db5f9SMaciej Machnikowski { 2302172db5f9SMaciej Machnikowski info->enable = ice_ptp_gpio_enable_e810; 2303172db5f9SMaciej Machnikowski 2304325b2064SMaciej Machnikowski if (ice_is_e810t(&pf->hw)) 2305325b2064SMaciej Machnikowski ice_ptp_setup_pins_e810t(pf, info); 2306325b2064SMaciej Machnikowski else 2307896a55aaSAnirudh Venkataramanan ice_ptp_setup_pins_e810(pf, info); 2308172db5f9SMaciej Machnikowski } 2309172db5f9SMaciej Machnikowski 2310172db5f9SMaciej Machnikowski /** 231106c16d89SJacob Keller * ice_ptp_set_caps - Set PTP capabilities 231206c16d89SJacob Keller * @pf: Board private structure 231306c16d89SJacob Keller */ 231406c16d89SJacob Keller static void ice_ptp_set_caps(struct ice_pf *pf) 231506c16d89SJacob Keller { 231606c16d89SJacob Keller struct ptp_clock_info *info = &pf->ptp.info; 231706c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf); 231806c16d89SJacob Keller 231906c16d89SJacob Keller snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk", 232006c16d89SJacob Keller dev_driver_string(dev), dev_name(dev)); 232106c16d89SJacob Keller info->owner = THIS_MODULE; 232206c16d89SJacob Keller info->max_adj = 999999999; 232306c16d89SJacob Keller info->adjtime = ice_ptp_adjtime; 232406c16d89SJacob Keller info->adjfine = ice_ptp_adjfine; 232506c16d89SJacob Keller info->gettimex64 = ice_ptp_gettimex64; 232606c16d89SJacob Keller info->settime64 = ice_ptp_settime64; 2327172db5f9SMaciej Machnikowski 23283a749623SJacob Keller if (ice_is_e810(&pf->hw)) 2329172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(pf, info); 233013a64f0bSJacob Keller else 233113a64f0bSJacob Keller ice_ptp_set_funcs_e822(pf, info); 233206c16d89SJacob Keller } 233306c16d89SJacob Keller 233406c16d89SJacob Keller /** 233506c16d89SJacob Keller * ice_ptp_create_clock - Create PTP clock device for userspace 233606c16d89SJacob Keller * @pf: Board private structure 233706c16d89SJacob Keller * 233806c16d89SJacob Keller * This function creates a new PTP clock device. It only creates one if we 233906c16d89SJacob Keller * don't already have one. Will return error if it can't create one, but success 234006c16d89SJacob Keller * if we already have a device. Should be used by ice_ptp_init to create clock 234106c16d89SJacob Keller * initially, and prevent global resets from creating new clock devices. 234206c16d89SJacob Keller */ 234306c16d89SJacob Keller static long ice_ptp_create_clock(struct ice_pf *pf) 234406c16d89SJacob Keller { 234506c16d89SJacob Keller struct ptp_clock_info *info; 234606c16d89SJacob Keller struct ptp_clock *clock; 234706c16d89SJacob Keller struct device *dev; 234806c16d89SJacob Keller 234906c16d89SJacob Keller /* No need to create a clock device if we already have one */ 235006c16d89SJacob Keller if (pf->ptp.clock) 235106c16d89SJacob Keller return 0; 235206c16d89SJacob Keller 235306c16d89SJacob Keller ice_ptp_set_caps(pf); 235406c16d89SJacob Keller 235506c16d89SJacob Keller info = &pf->ptp.info; 235606c16d89SJacob Keller dev = ice_pf_to_dev(pf); 235706c16d89SJacob Keller 235806c16d89SJacob Keller /* Attempt to register the clock before enabling the hardware. */ 235906c16d89SJacob Keller clock = ptp_clock_register(info, dev); 236006c16d89SJacob Keller if (IS_ERR(clock)) 236106c16d89SJacob Keller return PTR_ERR(clock); 236206c16d89SJacob Keller 236306c16d89SJacob Keller pf->ptp.clock = clock; 236406c16d89SJacob Keller 236506c16d89SJacob Keller return 0; 236606c16d89SJacob Keller } 236706c16d89SJacob Keller 2368ea9b847cSJacob Keller /** 2369ea9b847cSJacob Keller * ice_ptp_request_ts - Request an available Tx timestamp index 2370ea9b847cSJacob Keller * @tx: the PTP Tx timestamp tracker to request from 2371ea9b847cSJacob Keller * @skb: the SKB to associate with this timestamp request 2372ea9b847cSJacob Keller */ 2373ea9b847cSJacob Keller s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb) 2374ea9b847cSJacob Keller { 2375ea9b847cSJacob Keller u8 idx; 2376ea9b847cSJacob Keller 2377ea9b847cSJacob Keller /* Check if this tracker is initialized */ 23783a749623SJacob Keller if (!tx->init || tx->calibrating) 2379ea9b847cSJacob Keller return -1; 2380ea9b847cSJacob Keller 2381ea9b847cSJacob Keller spin_lock(&tx->lock); 2382ea9b847cSJacob Keller /* Find and set the first available index */ 2383ea9b847cSJacob Keller idx = find_first_zero_bit(tx->in_use, tx->len); 2384ea9b847cSJacob Keller if (idx < tx->len) { 2385ea9b847cSJacob Keller /* We got a valid index that no other thread could have set. Store 2386ea9b847cSJacob Keller * a reference to the skb and the start time to allow discarding old 2387ea9b847cSJacob Keller * requests. 2388ea9b847cSJacob Keller */ 2389ea9b847cSJacob Keller set_bit(idx, tx->in_use); 2390ea9b847cSJacob Keller tx->tstamps[idx].start = jiffies; 2391ea9b847cSJacob Keller tx->tstamps[idx].skb = skb_get(skb); 2392ea9b847cSJacob Keller skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 23934c120218SJacob Keller ice_trace(tx_tstamp_request, skb, idx); 2394ea9b847cSJacob Keller } 2395ea9b847cSJacob Keller 2396ea9b847cSJacob Keller spin_unlock(&tx->lock); 2397ea9b847cSJacob Keller 2398ea9b847cSJacob Keller /* return the appropriate PHY timestamp register index, -1 if no 2399ea9b847cSJacob Keller * indexes were available. 2400ea9b847cSJacob Keller */ 2401ea9b847cSJacob Keller if (idx >= tx->len) 2402ea9b847cSJacob Keller return -1; 2403ea9b847cSJacob Keller else 2404ea9b847cSJacob Keller return idx + tx->quad_offset; 2405ea9b847cSJacob Keller } 2406ea9b847cSJacob Keller 2407ea9b847cSJacob Keller /** 2408ea9b847cSJacob Keller * ice_ptp_process_ts - Spawn kthread work to handle timestamps 2409ea9b847cSJacob Keller * @pf: Board private structure 2410ea9b847cSJacob Keller * 2411ea9b847cSJacob Keller * Queue work required to process the PTP Tx timestamps outside of interrupt 2412ea9b847cSJacob Keller * context. 2413ea9b847cSJacob Keller */ 2414ea9b847cSJacob Keller void ice_ptp_process_ts(struct ice_pf *pf) 2415ea9b847cSJacob Keller { 2416ea9b847cSJacob Keller if (pf->ptp.port.tx.init) 2417ea9b847cSJacob Keller kthread_queue_work(pf->ptp.kworker, &pf->ptp.port.tx.work); 2418ea9b847cSJacob Keller } 2419ea9b847cSJacob Keller 242077a78115SJacob Keller static void ice_ptp_periodic_work(struct kthread_work *work) 242177a78115SJacob Keller { 242277a78115SJacob Keller struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work); 242377a78115SJacob Keller struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 24244503cc7fSArkadiusz Kubalewski int err; 242577a78115SJacob Keller 242677a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 242777a78115SJacob Keller return; 242877a78115SJacob Keller 24294503cc7fSArkadiusz Kubalewski err = ice_ptp_update_cached_phctime(pf); 243077a78115SJacob Keller 2431f020481bSJacob Keller ice_ptp_tx_tstamp_cleanup(pf, &pf->ptp.port.tx); 2432ea9b847cSJacob Keller 24334503cc7fSArkadiusz Kubalewski /* Run twice a second or reschedule if phc update failed */ 243477a78115SJacob Keller kthread_queue_delayed_work(ptp->kworker, &ptp->work, 24354503cc7fSArkadiusz Kubalewski msecs_to_jiffies(err ? 10 : 500)); 243677a78115SJacob Keller } 243777a78115SJacob Keller 243806c16d89SJacob Keller /** 243948096710SKarol Kolacinski * ice_ptp_reset - Initialize PTP hardware clock support after reset 244048096710SKarol Kolacinski * @pf: Board private structure 244148096710SKarol Kolacinski */ 244248096710SKarol Kolacinski void ice_ptp_reset(struct ice_pf *pf) 244348096710SKarol Kolacinski { 244448096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 244548096710SKarol Kolacinski struct ice_hw *hw = &pf->hw; 244648096710SKarol Kolacinski struct timespec64 ts; 24473a749623SJacob Keller int err, itr = 1; 244848096710SKarol Kolacinski u64 time_diff; 244948096710SKarol Kolacinski 245048096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state)) 245148096710SKarol Kolacinski goto pfr; 245248096710SKarol Kolacinski 2453b2ee7256SJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned) 24543a749623SJacob Keller goto reset_ts; 245548096710SKarol Kolacinski 2456b2ee7256SJacob Keller err = ice_ptp_init_phc(hw); 245748096710SKarol Kolacinski if (err) 245848096710SKarol Kolacinski goto err; 245948096710SKarol Kolacinski 246048096710SKarol Kolacinski /* Acquire the global hardware lock */ 246148096710SKarol Kolacinski if (!ice_ptp_lock(hw)) { 246248096710SKarol Kolacinski err = -EBUSY; 246348096710SKarol Kolacinski goto err; 246448096710SKarol Kolacinski } 246548096710SKarol Kolacinski 246648096710SKarol Kolacinski /* Write the increment time value to PHY and LAN */ 246778267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf)); 246848096710SKarol Kolacinski if (err) { 246948096710SKarol Kolacinski ice_ptp_unlock(hw); 247048096710SKarol Kolacinski goto err; 247148096710SKarol Kolacinski } 247248096710SKarol Kolacinski 247348096710SKarol Kolacinski /* Write the initial Time value to PHY and LAN using the cached PHC 247448096710SKarol Kolacinski * time before the reset and time difference between stopping and 247548096710SKarol Kolacinski * starting the clock. 247648096710SKarol Kolacinski */ 247748096710SKarol Kolacinski if (ptp->cached_phc_time) { 247848096710SKarol Kolacinski time_diff = ktime_get_real_ns() - ptp->reset_time; 247948096710SKarol Kolacinski ts = ns_to_timespec64(ptp->cached_phc_time + time_diff); 248048096710SKarol Kolacinski } else { 248148096710SKarol Kolacinski ts = ktime_to_timespec64(ktime_get_real()); 248248096710SKarol Kolacinski } 248348096710SKarol Kolacinski err = ice_ptp_write_init(pf, &ts); 248448096710SKarol Kolacinski if (err) { 248548096710SKarol Kolacinski ice_ptp_unlock(hw); 248648096710SKarol Kolacinski goto err; 248748096710SKarol Kolacinski } 248848096710SKarol Kolacinski 248948096710SKarol Kolacinski /* Release the global hardware lock */ 249048096710SKarol Kolacinski ice_ptp_unlock(hw); 249148096710SKarol Kolacinski 24923a749623SJacob Keller if (!ice_is_e810(hw)) { 24933a749623SJacob Keller /* Enable quad interrupts */ 24943a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr); 24953a749623SJacob Keller if (err) 24963a749623SJacob Keller goto err; 24973a749623SJacob Keller } 24983a749623SJacob Keller 24993a749623SJacob Keller reset_ts: 25003a749623SJacob Keller /* Restart the PHY timestamping block */ 25013a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf); 25023a749623SJacob Keller 250348096710SKarol Kolacinski pfr: 250448096710SKarol Kolacinski /* Init Tx structures */ 2505a69f1cb6SJacob Keller if (ice_is_e810(&pf->hw)) { 250648096710SKarol Kolacinski err = ice_ptp_init_tx_e810(pf, &ptp->port.tx); 2507a69f1cb6SJacob Keller } else { 2508a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp->port.ov_work, 2509a69f1cb6SJacob Keller ice_ptp_wait_for_offset_valid); 25103a749623SJacob Keller err = ice_ptp_init_tx_e822(pf, &ptp->port.tx, 25113a749623SJacob Keller ptp->port.port_num); 2512a69f1cb6SJacob Keller } 251348096710SKarol Kolacinski if (err) 251448096710SKarol Kolacinski goto err; 251548096710SKarol Kolacinski 251648096710SKarol Kolacinski set_bit(ICE_FLAG_PTP, pf->flags); 251748096710SKarol Kolacinski 251848096710SKarol Kolacinski /* Start periodic work going */ 251948096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0); 252048096710SKarol Kolacinski 252148096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP reset successful\n"); 252248096710SKarol Kolacinski return; 252348096710SKarol Kolacinski 252448096710SKarol Kolacinski err: 252548096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP reset failed %d\n", err); 252648096710SKarol Kolacinski } 252748096710SKarol Kolacinski 252848096710SKarol Kolacinski /** 252948096710SKarol Kolacinski * ice_ptp_prepare_for_reset - Prepare PTP for reset 253048096710SKarol Kolacinski * @pf: Board private structure 253148096710SKarol Kolacinski */ 253248096710SKarol Kolacinski void ice_ptp_prepare_for_reset(struct ice_pf *pf) 253348096710SKarol Kolacinski { 253448096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 253548096710SKarol Kolacinski u8 src_tmr; 253648096710SKarol Kolacinski 253748096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags); 253848096710SKarol Kolacinski 253948096710SKarol Kolacinski /* Disable timestamping for both Tx and Rx */ 254048096710SKarol Kolacinski ice_ptp_cfg_timestamp(pf, false); 254148096710SKarol Kolacinski 254248096710SKarol Kolacinski kthread_cancel_delayed_work_sync(&ptp->work); 254348096710SKarol Kolacinski kthread_cancel_work_sync(&ptp->extts_work); 254448096710SKarol Kolacinski 254548096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state)) 254648096710SKarol Kolacinski return; 254748096710SKarol Kolacinski 254848096710SKarol Kolacinski ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 254948096710SKarol Kolacinski 255048096710SKarol Kolacinski /* Disable periodic outputs */ 255148096710SKarol Kolacinski ice_ptp_disable_all_clkout(pf); 255248096710SKarol Kolacinski 255348096710SKarol Kolacinski src_tmr = ice_get_ptp_src_clock_index(&pf->hw); 255448096710SKarol Kolacinski 255548096710SKarol Kolacinski /* Disable source clock */ 255648096710SKarol Kolacinski wr32(&pf->hw, GLTSYN_ENA(src_tmr), (u32)~GLTSYN_ENA_TSYN_ENA_M); 255748096710SKarol Kolacinski 255848096710SKarol Kolacinski /* Acquire PHC and system timer to restore after reset */ 255948096710SKarol Kolacinski ptp->reset_time = ktime_get_real_ns(); 256048096710SKarol Kolacinski } 256148096710SKarol Kolacinski 256248096710SKarol Kolacinski /** 256306c16d89SJacob Keller * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device 256406c16d89SJacob Keller * @pf: Board private structure 256506c16d89SJacob Keller * 256606c16d89SJacob Keller * Setup and initialize a PTP clock device that represents the device hardware 256706c16d89SJacob Keller * clock. Save the clock index for other functions connected to the same 256806c16d89SJacob Keller * hardware resource. 256906c16d89SJacob Keller */ 257006c16d89SJacob Keller static int ice_ptp_init_owner(struct ice_pf *pf) 257106c16d89SJacob Keller { 257206c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 257306c16d89SJacob Keller struct timespec64 ts; 25743a749623SJacob Keller int err, itr = 1; 257506c16d89SJacob Keller 2576b2ee7256SJacob Keller err = ice_ptp_init_phc(hw); 2577b2ee7256SJacob Keller if (err) { 2578b2ee7256SJacob Keller dev_err(ice_pf_to_dev(pf), "Failed to initialize PHC, err %d\n", 2579b2ee7256SJacob Keller err); 2580b2ee7256SJacob Keller return err; 2581b2ee7256SJacob Keller } 258206c16d89SJacob Keller 258306c16d89SJacob Keller /* Acquire the global hardware lock */ 258406c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 258506c16d89SJacob Keller err = -EBUSY; 258606c16d89SJacob Keller goto err_exit; 258706c16d89SJacob Keller } 258806c16d89SJacob Keller 258906c16d89SJacob Keller /* Write the increment time value to PHY and LAN */ 259078267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf)); 259106c16d89SJacob Keller if (err) { 259206c16d89SJacob Keller ice_ptp_unlock(hw); 259306c16d89SJacob Keller goto err_exit; 259406c16d89SJacob Keller } 259506c16d89SJacob Keller 259606c16d89SJacob Keller ts = ktime_to_timespec64(ktime_get_real()); 259706c16d89SJacob Keller /* Write the initial Time value to PHY and LAN */ 259806c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts); 259906c16d89SJacob Keller if (err) { 260006c16d89SJacob Keller ice_ptp_unlock(hw); 260106c16d89SJacob Keller goto err_exit; 260206c16d89SJacob Keller } 260306c16d89SJacob Keller 260406c16d89SJacob Keller /* Release the global hardware lock */ 260506c16d89SJacob Keller ice_ptp_unlock(hw); 260606c16d89SJacob Keller 26073a749623SJacob Keller if (!ice_is_e810(hw)) { 26083a749623SJacob Keller /* Enable quad interrupts */ 26093a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr); 26103a749623SJacob Keller if (err) 26113a749623SJacob Keller goto err_exit; 26123a749623SJacob Keller } 26133a749623SJacob Keller 261406c16d89SJacob Keller /* Ensure we have a clock device */ 261506c16d89SJacob Keller err = ice_ptp_create_clock(pf); 261606c16d89SJacob Keller if (err) 261706c16d89SJacob Keller goto err_clk; 261806c16d89SJacob Keller 261967569a7fSJacob Keller /* Store the PTP clock index for other PFs */ 262067569a7fSJacob Keller ice_set_ptp_clock_index(pf); 262167569a7fSJacob Keller 262206c16d89SJacob Keller return 0; 262306c16d89SJacob Keller 262406c16d89SJacob Keller err_clk: 262506c16d89SJacob Keller pf->ptp.clock = NULL; 262606c16d89SJacob Keller err_exit: 262706c16d89SJacob Keller return err; 262806c16d89SJacob Keller } 262906c16d89SJacob Keller 263006c16d89SJacob Keller /** 263148096710SKarol Kolacinski * ice_ptp_init_work - Initialize PTP work threads 263248096710SKarol Kolacinski * @pf: Board private structure 263348096710SKarol Kolacinski * @ptp: PF PTP structure 263448096710SKarol Kolacinski */ 263548096710SKarol Kolacinski static int ice_ptp_init_work(struct ice_pf *pf, struct ice_ptp *ptp) 263648096710SKarol Kolacinski { 263748096710SKarol Kolacinski struct kthread_worker *kworker; 263848096710SKarol Kolacinski 263948096710SKarol Kolacinski /* Initialize work functions */ 264048096710SKarol Kolacinski kthread_init_delayed_work(&ptp->work, ice_ptp_periodic_work); 264148096710SKarol Kolacinski kthread_init_work(&ptp->extts_work, ice_ptp_extts_work); 264248096710SKarol Kolacinski 264348096710SKarol Kolacinski /* Allocate a kworker for handling work required for the ports 264448096710SKarol Kolacinski * connected to the PTP hardware clock. 264548096710SKarol Kolacinski */ 264648096710SKarol Kolacinski kworker = kthread_create_worker(0, "ice-ptp-%s", 264748096710SKarol Kolacinski dev_name(ice_pf_to_dev(pf))); 264848096710SKarol Kolacinski if (IS_ERR(kworker)) 264948096710SKarol Kolacinski return PTR_ERR(kworker); 265048096710SKarol Kolacinski 265148096710SKarol Kolacinski ptp->kworker = kworker; 265248096710SKarol Kolacinski 265348096710SKarol Kolacinski /* Start periodic work going */ 265448096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0); 265548096710SKarol Kolacinski 265648096710SKarol Kolacinski return 0; 265748096710SKarol Kolacinski } 265848096710SKarol Kolacinski 265948096710SKarol Kolacinski /** 26603a749623SJacob Keller * ice_ptp_init_port - Initialize PTP port structure 26613a749623SJacob Keller * @pf: Board private structure 26623a749623SJacob Keller * @ptp_port: PTP port structure 26633a749623SJacob Keller */ 26643a749623SJacob Keller static int ice_ptp_init_port(struct ice_pf *pf, struct ice_ptp_port *ptp_port) 26653a749623SJacob Keller { 26663a749623SJacob Keller mutex_init(&ptp_port->ps_lock); 26673a749623SJacob Keller 26683a749623SJacob Keller if (ice_is_e810(&pf->hw)) 26693a749623SJacob Keller return ice_ptp_init_tx_e810(pf, &ptp_port->tx); 26703a749623SJacob Keller 2671a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp_port->ov_work, 2672a69f1cb6SJacob Keller ice_ptp_wait_for_offset_valid); 26733a749623SJacob Keller return ice_ptp_init_tx_e822(pf, &ptp_port->tx, ptp_port->port_num); 26743a749623SJacob Keller } 26753a749623SJacob Keller 26763a749623SJacob Keller /** 2677b2ee7256SJacob Keller * ice_ptp_init - Initialize PTP hardware clock support 267806c16d89SJacob Keller * @pf: Board private structure 267906c16d89SJacob Keller * 2680b2ee7256SJacob Keller * Set up the device for interacting with the PTP hardware clock for all 2681b2ee7256SJacob Keller * functions, both the function that owns the clock hardware, and the 2682b2ee7256SJacob Keller * functions connected to the clock hardware. 2683b2ee7256SJacob Keller * 2684b2ee7256SJacob Keller * The clock owner will allocate and register a ptp_clock with the 2685b2ee7256SJacob Keller * PTP_1588_CLOCK infrastructure. All functions allocate a kthread and work 2686b2ee7256SJacob Keller * items used for asynchronous work such as Tx timestamps and periodic work. 268706c16d89SJacob Keller */ 268806c16d89SJacob Keller void ice_ptp_init(struct ice_pf *pf) 268906c16d89SJacob Keller { 269048096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 269106c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 269206c16d89SJacob Keller int err; 269306c16d89SJacob Keller 2694b2ee7256SJacob Keller /* If this function owns the clock hardware, it must allocate and 2695b2ee7256SJacob Keller * configure the PTP clock device to represent it. 2696b2ee7256SJacob Keller */ 269706c16d89SJacob Keller if (hw->func_caps.ts_func_info.src_tmr_owned) { 269806c16d89SJacob Keller err = ice_ptp_init_owner(pf); 269906c16d89SJacob Keller if (err) 270048096710SKarol Kolacinski goto err; 270106c16d89SJacob Keller } 270206c16d89SJacob Keller 27033a749623SJacob Keller ptp->port.port_num = hw->pf_id; 27043a749623SJacob Keller err = ice_ptp_init_port(pf, &ptp->port); 270548096710SKarol Kolacinski if (err) 270648096710SKarol Kolacinski goto err; 270777a78115SJacob Keller 27083a749623SJacob Keller /* Start the PHY timestamping block */ 27093a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf); 27103a749623SJacob Keller 271106c16d89SJacob Keller set_bit(ICE_FLAG_PTP, pf->flags); 271248096710SKarol Kolacinski err = ice_ptp_init_work(pf, ptp); 271348096710SKarol Kolacinski if (err) 271448096710SKarol Kolacinski goto err; 271506c16d89SJacob Keller 271648096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP init successful\n"); 271777a78115SJacob Keller return; 271877a78115SJacob Keller 271948096710SKarol Kolacinski err: 272077a78115SJacob Keller /* If we registered a PTP clock, release it */ 272177a78115SJacob Keller if (pf->ptp.clock) { 272248096710SKarol Kolacinski ptp_clock_unregister(ptp->clock); 272377a78115SJacob Keller pf->ptp.clock = NULL; 272477a78115SJacob Keller } 272548096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags); 272648096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP failed %d\n", err); 272706c16d89SJacob Keller } 272806c16d89SJacob Keller 272906c16d89SJacob Keller /** 273006c16d89SJacob Keller * ice_ptp_release - Disable the driver/HW support and unregister the clock 273106c16d89SJacob Keller * @pf: Board private structure 273206c16d89SJacob Keller * 273306c16d89SJacob Keller * This function handles the cleanup work required from the initialization by 273406c16d89SJacob Keller * clearing out the important information and unregistering the clock 273506c16d89SJacob Keller */ 273606c16d89SJacob Keller void ice_ptp_release(struct ice_pf *pf) 273706c16d89SJacob Keller { 2738fd1b5bebSYongxin Liu if (!test_bit(ICE_FLAG_PTP, pf->flags)) 2739fd1b5bebSYongxin Liu return; 2740fd1b5bebSYongxin Liu 274177a78115SJacob Keller /* Disable timestamping for both Tx and Rx */ 274277a78115SJacob Keller ice_ptp_cfg_timestamp(pf, false); 274377a78115SJacob Keller 2744ea9b847cSJacob Keller ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 2745ea9b847cSJacob Keller 274606c16d89SJacob Keller clear_bit(ICE_FLAG_PTP, pf->flags); 274706c16d89SJacob Keller 274877a78115SJacob Keller kthread_cancel_delayed_work_sync(&pf->ptp.work); 274977a78115SJacob Keller 27503a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port); 27513a749623SJacob Keller mutex_destroy(&pf->ptp.port.ps_lock); 275277a78115SJacob Keller if (pf->ptp.kworker) { 275377a78115SJacob Keller kthread_destroy_worker(pf->ptp.kworker); 275477a78115SJacob Keller pf->ptp.kworker = NULL; 275577a78115SJacob Keller } 275677a78115SJacob Keller 275706c16d89SJacob Keller if (!pf->ptp.clock) 275806c16d89SJacob Keller return; 275906c16d89SJacob Keller 27609ee31343SJacob Keller /* Disable periodic outputs */ 27619ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 27629ee31343SJacob Keller 276367569a7fSJacob Keller ice_clear_ptp_clock_index(pf); 276406c16d89SJacob Keller ptp_clock_unregister(pf->ptp.clock); 276506c16d89SJacob Keller pf->ptp.clock = NULL; 276606c16d89SJacob Keller 276706c16d89SJacob Keller dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n"); 276806c16d89SJacob Keller } 2769