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_update_cached_phctime - Update the cached PHC time values 49577a78115SJacob Keller * @pf: Board specific private structure 49677a78115SJacob Keller * 49777a78115SJacob Keller * This function updates the system time values which are cached in the PF 49877a78115SJacob Keller * structure and the Rx rings. 49977a78115SJacob Keller * 50077a78115SJacob Keller * This function must be called periodically to ensure that the cached value 50177a78115SJacob Keller * is never more than 2 seconds old. It must also be called whenever the PHC 50277a78115SJacob Keller * time has been changed. 5034503cc7fSArkadiusz Kubalewski * 5044503cc7fSArkadiusz Kubalewski * Return: 5054503cc7fSArkadiusz Kubalewski * * 0 - OK, successfully updated 5064503cc7fSArkadiusz Kubalewski * * -EAGAIN - PF was busy, need to reschedule the update 50777a78115SJacob Keller */ 5084503cc7fSArkadiusz Kubalewski static int ice_ptp_update_cached_phctime(struct ice_pf *pf) 50977a78115SJacob Keller { 51077a78115SJacob Keller u64 systime; 51177a78115SJacob Keller int i; 51277a78115SJacob Keller 5134503cc7fSArkadiusz Kubalewski if (test_and_set_bit(ICE_CFG_BUSY, pf->state)) 5144503cc7fSArkadiusz Kubalewski return -EAGAIN; 5154503cc7fSArkadiusz Kubalewski 51677a78115SJacob Keller /* Read the current PHC time */ 51777a78115SJacob Keller systime = ice_ptp_read_src_clk_reg(pf, NULL); 51877a78115SJacob Keller 51977a78115SJacob Keller /* Update the cached PHC time stored in the PF structure */ 52077a78115SJacob Keller WRITE_ONCE(pf->ptp.cached_phc_time, systime); 52177a78115SJacob Keller 52277a78115SJacob Keller ice_for_each_vsi(pf, i) { 52377a78115SJacob Keller struct ice_vsi *vsi = pf->vsi[i]; 52477a78115SJacob Keller int j; 52577a78115SJacob Keller 52677a78115SJacob Keller if (!vsi) 52777a78115SJacob Keller continue; 52877a78115SJacob Keller 52977a78115SJacob Keller if (vsi->type != ICE_VSI_PF) 53077a78115SJacob Keller continue; 53177a78115SJacob Keller 53277a78115SJacob Keller ice_for_each_rxq(vsi, j) { 53377a78115SJacob Keller if (!vsi->rx_rings[j]) 53477a78115SJacob Keller continue; 53577a78115SJacob Keller WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime); 53677a78115SJacob Keller } 53777a78115SJacob Keller } 5384503cc7fSArkadiusz Kubalewski clear_bit(ICE_CFG_BUSY, pf->state); 5394503cc7fSArkadiusz Kubalewski 5404503cc7fSArkadiusz Kubalewski return 0; 54177a78115SJacob Keller } 54277a78115SJacob Keller 54377a78115SJacob Keller /** 54477a78115SJacob Keller * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b 54577a78115SJacob Keller * @cached_phc_time: recently cached copy of PHC time 54677a78115SJacob Keller * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value 54777a78115SJacob Keller * 54877a78115SJacob Keller * Hardware captures timestamps which contain only 32 bits of nominal 54977a78115SJacob Keller * nanoseconds, as opposed to the 64bit timestamps that the stack expects. 55077a78115SJacob Keller * Note that the captured timestamp values may be 40 bits, but the lower 55177a78115SJacob Keller * 8 bits are sub-nanoseconds and generally discarded. 55277a78115SJacob Keller * 55377a78115SJacob Keller * Extend the 32bit nanosecond timestamp using the following algorithm and 55477a78115SJacob Keller * assumptions: 55577a78115SJacob Keller * 55677a78115SJacob Keller * 1) have a recently cached copy of the PHC time 55777a78115SJacob Keller * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1 55877a78115SJacob Keller * seconds) before or after the PHC time was captured. 55977a78115SJacob Keller * 3) calculate the delta between the cached time and the timestamp 56077a78115SJacob Keller * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was 56177a78115SJacob Keller * captured after the PHC time. In this case, the full timestamp is just 56277a78115SJacob Keller * the cached PHC time plus the delta. 56377a78115SJacob Keller * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the 56477a78115SJacob Keller * timestamp was captured *before* the PHC time, i.e. because the PHC 56577a78115SJacob Keller * cache was updated after the timestamp was captured by hardware. In this 56677a78115SJacob Keller * case, the full timestamp is the cached time minus the inverse delta. 56777a78115SJacob Keller * 56877a78115SJacob Keller * This algorithm works even if the PHC time was updated after a Tx timestamp 56977a78115SJacob Keller * was requested, but before the Tx timestamp event was reported from 57077a78115SJacob Keller * hardware. 57177a78115SJacob Keller * 57277a78115SJacob Keller * This calculation primarily relies on keeping the cached PHC time up to 57377a78115SJacob Keller * date. If the timestamp was captured more than 2^31 nanoseconds after the 57477a78115SJacob Keller * PHC time, it is possible that the lower 32bits of PHC time have 57577a78115SJacob Keller * overflowed more than once, and we might generate an incorrect timestamp. 57677a78115SJacob Keller * 57777a78115SJacob Keller * This is prevented by (a) periodically updating the cached PHC time once 57877a78115SJacob Keller * a second, and (b) discarding any Tx timestamp packet if it has waited for 57977a78115SJacob Keller * a timestamp for more than one second. 58077a78115SJacob Keller */ 58177a78115SJacob Keller static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp) 58277a78115SJacob Keller { 58377a78115SJacob Keller u32 delta, phc_time_lo; 58477a78115SJacob Keller u64 ns; 58577a78115SJacob Keller 58677a78115SJacob Keller /* Extract the lower 32 bits of the PHC time */ 58777a78115SJacob Keller phc_time_lo = (u32)cached_phc_time; 58877a78115SJacob Keller 58977a78115SJacob Keller /* Calculate the delta between the lower 32bits of the cached PHC 59077a78115SJacob Keller * time and the in_tstamp value 59177a78115SJacob Keller */ 59277a78115SJacob Keller delta = (in_tstamp - phc_time_lo); 59377a78115SJacob Keller 59477a78115SJacob Keller /* Do not assume that the in_tstamp is always more recent than the 59577a78115SJacob Keller * cached PHC time. If the delta is large, it indicates that the 59677a78115SJacob Keller * in_tstamp was taken in the past, and should be converted 59777a78115SJacob Keller * forward. 59877a78115SJacob Keller */ 59977a78115SJacob Keller if (delta > (U32_MAX / 2)) { 60077a78115SJacob Keller /* reverse the delta calculation here */ 60177a78115SJacob Keller delta = (phc_time_lo - in_tstamp); 60277a78115SJacob Keller ns = cached_phc_time - delta; 60377a78115SJacob Keller } else { 60477a78115SJacob Keller ns = cached_phc_time + delta; 60577a78115SJacob Keller } 60677a78115SJacob Keller 60777a78115SJacob Keller return ns; 60877a78115SJacob Keller } 60977a78115SJacob Keller 61077a78115SJacob Keller /** 611ea9b847cSJacob Keller * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds 612ea9b847cSJacob Keller * @pf: Board private structure 613ea9b847cSJacob Keller * @in_tstamp: Ingress/egress 40b timestamp value 614ea9b847cSJacob Keller * 615ea9b847cSJacob Keller * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal 616ea9b847cSJacob Keller * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit. 617ea9b847cSJacob Keller * 618ea9b847cSJacob Keller * *--------------------------------------------------------------* 619ea9b847cSJacob Keller * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v | 620ea9b847cSJacob Keller * *--------------------------------------------------------------* 621ea9b847cSJacob Keller * 622ea9b847cSJacob Keller * The low bit is an indicator of whether the timestamp is valid. The next 623ea9b847cSJacob Keller * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow, 624ea9b847cSJacob Keller * and the remaining 32 bits are the lower 32 bits of the PHC timer. 625ea9b847cSJacob Keller * 626ea9b847cSJacob Keller * It is assumed that the caller verifies the timestamp is valid prior to 627ea9b847cSJacob Keller * calling this function. 628ea9b847cSJacob Keller * 629ea9b847cSJacob Keller * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC 630ea9b847cSJacob Keller * time stored in the device private PTP structure as the basis for timestamp 631ea9b847cSJacob Keller * extension. 632ea9b847cSJacob Keller * 633ea9b847cSJacob Keller * See ice_ptp_extend_32b_ts for a detailed explanation of the extension 634ea9b847cSJacob Keller * algorithm. 635ea9b847cSJacob Keller */ 636ea9b847cSJacob Keller static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp) 637ea9b847cSJacob Keller { 638ea9b847cSJacob Keller const u64 mask = GENMASK_ULL(31, 0); 639ea9b847cSJacob Keller 640ea9b847cSJacob Keller return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time, 641ea9b847cSJacob Keller (in_tstamp >> 8) & mask); 642ea9b847cSJacob Keller } 643ea9b847cSJacob Keller 644ea9b847cSJacob Keller /** 64506c16d89SJacob Keller * ice_ptp_read_time - Read the time from the device 64606c16d89SJacob Keller * @pf: Board private structure 64706c16d89SJacob Keller * @ts: timespec structure to hold the current time value 64806c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 64906c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 65006c16d89SJacob Keller * 65106c16d89SJacob Keller * This function reads the source clock registers and stores them in a timespec. 65206c16d89SJacob Keller * However, since the registers are 64 bits of nanoseconds, we must convert the 65306c16d89SJacob Keller * result to a timespec before we can return. 65406c16d89SJacob Keller */ 65506c16d89SJacob Keller static void 65606c16d89SJacob Keller ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts, 65706c16d89SJacob Keller struct ptp_system_timestamp *sts) 65806c16d89SJacob Keller { 65906c16d89SJacob Keller u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts); 66006c16d89SJacob Keller 66106c16d89SJacob Keller *ts = ns_to_timespec64(time_ns); 66206c16d89SJacob Keller } 66306c16d89SJacob Keller 66406c16d89SJacob Keller /** 66506c16d89SJacob Keller * ice_ptp_write_init - Set PHC time to provided value 66606c16d89SJacob Keller * @pf: Board private structure 66706c16d89SJacob Keller * @ts: timespec structure that holds the new time value 66806c16d89SJacob Keller * 66906c16d89SJacob Keller * Set the PHC time to the specified time provided in the timespec. 67006c16d89SJacob Keller */ 67106c16d89SJacob Keller static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts) 67206c16d89SJacob Keller { 67306c16d89SJacob Keller u64 ns = timespec64_to_ns(ts); 67406c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 67506c16d89SJacob Keller 67606c16d89SJacob Keller return ice_ptp_init_time(hw, ns); 67706c16d89SJacob Keller } 67806c16d89SJacob Keller 67906c16d89SJacob Keller /** 68006c16d89SJacob Keller * ice_ptp_write_adj - Adjust PHC clock time atomically 68106c16d89SJacob Keller * @pf: Board private structure 68206c16d89SJacob Keller * @adj: Adjustment in nanoseconds 68306c16d89SJacob Keller * 68406c16d89SJacob Keller * Perform an atomic adjustment of the PHC time by the specified number of 68506c16d89SJacob Keller * nanoseconds. 68606c16d89SJacob Keller */ 68706c16d89SJacob Keller static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj) 68806c16d89SJacob Keller { 68906c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 69006c16d89SJacob Keller 69106c16d89SJacob Keller return ice_ptp_adj_clock(hw, adj); 69206c16d89SJacob Keller } 69306c16d89SJacob Keller 69406c16d89SJacob Keller /** 69578267d0cSJacob Keller * ice_base_incval - Get base timer increment value 69678267d0cSJacob Keller * @pf: Board private structure 69778267d0cSJacob Keller * 69878267d0cSJacob Keller * Look up the base timer increment value for this device. The base increment 69978267d0cSJacob Keller * value is used to define the nominal clock tick rate. This increment value 70078267d0cSJacob Keller * is programmed during device initialization. It is also used as the basis 70178267d0cSJacob Keller * for calculating adjustments using scaled_ppm. 70278267d0cSJacob Keller */ 70378267d0cSJacob Keller static u64 ice_base_incval(struct ice_pf *pf) 70478267d0cSJacob Keller { 7053a749623SJacob Keller struct ice_hw *hw = &pf->hw; 7063a749623SJacob Keller u64 incval; 7073a749623SJacob Keller 7083a749623SJacob Keller if (ice_is_e810(hw)) 7093a749623SJacob Keller incval = ICE_PTP_NOMINAL_INCVAL_E810; 7103a749623SJacob Keller else if (ice_e822_time_ref(hw) < NUM_ICE_TIME_REF_FREQ) 7113a749623SJacob Keller incval = ice_e822_nominal_incval(ice_e822_time_ref(hw)); 7123a749623SJacob Keller else 7133a749623SJacob Keller incval = UNKNOWN_INCVAL_E822; 7143a749623SJacob Keller 7153a749623SJacob Keller dev_dbg(ice_pf_to_dev(pf), "PTP: using base increment value of 0x%016llx\n", 7163a749623SJacob Keller incval); 7173a749623SJacob Keller 7183a749623SJacob Keller return incval; 7193a749623SJacob Keller } 7203a749623SJacob Keller 7213a749623SJacob Keller /** 7223a749623SJacob Keller * ice_ptp_reset_ts_memory_quad - Reset timestamp memory for one quad 7233a749623SJacob Keller * @pf: The PF private data structure 7243a749623SJacob Keller * @quad: The quad (0-4) 7253a749623SJacob Keller */ 7263a749623SJacob Keller static void ice_ptp_reset_ts_memory_quad(struct ice_pf *pf, int quad) 7273a749623SJacob Keller { 7283a749623SJacob Keller struct ice_hw *hw = &pf->hw; 7293a749623SJacob Keller 7303a749623SJacob Keller ice_write_quad_reg_e822(hw, quad, Q_REG_TS_CTRL, Q_REG_TS_CTRL_M); 7313a749623SJacob Keller ice_write_quad_reg_e822(hw, quad, Q_REG_TS_CTRL, ~(u32)Q_REG_TS_CTRL_M); 7323a749623SJacob Keller } 7333a749623SJacob Keller 7343a749623SJacob Keller /** 735a69f1cb6SJacob Keller * ice_ptp_check_tx_fifo - Check whether Tx FIFO is in an OK state 736a69f1cb6SJacob Keller * @port: PTP port for which Tx FIFO is checked 737a69f1cb6SJacob Keller */ 738a69f1cb6SJacob Keller static int ice_ptp_check_tx_fifo(struct ice_ptp_port *port) 739a69f1cb6SJacob Keller { 740a69f1cb6SJacob Keller int quad = port->port_num / ICE_PORTS_PER_QUAD; 741a69f1cb6SJacob Keller int offs = port->port_num % ICE_PORTS_PER_QUAD; 742a69f1cb6SJacob Keller struct ice_pf *pf; 743a69f1cb6SJacob Keller struct ice_hw *hw; 744a69f1cb6SJacob Keller u32 val, phy_sts; 745a69f1cb6SJacob Keller int err; 746a69f1cb6SJacob Keller 747a69f1cb6SJacob Keller pf = ptp_port_to_pf(port); 748a69f1cb6SJacob Keller hw = &pf->hw; 749a69f1cb6SJacob Keller 750a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == FIFO_OK) 751a69f1cb6SJacob Keller return 0; 752a69f1cb6SJacob Keller 753a69f1cb6SJacob Keller /* need to read FIFO state */ 754a69f1cb6SJacob Keller if (offs == 0 || offs == 1) 755a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO01_STATUS, 756a69f1cb6SJacob Keller &val); 757a69f1cb6SJacob Keller else 758a69f1cb6SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_FIFO23_STATUS, 759a69f1cb6SJacob Keller &val); 760a69f1cb6SJacob Keller 761a69f1cb6SJacob Keller if (err) { 762a69f1cb6SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to check port %d Tx FIFO, err %d\n", 763a69f1cb6SJacob Keller port->port_num, err); 764a69f1cb6SJacob Keller return err; 765a69f1cb6SJacob Keller } 766a69f1cb6SJacob Keller 767a69f1cb6SJacob Keller if (offs & 0x1) 768a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO13_M) >> Q_REG_FIFO13_S; 769a69f1cb6SJacob Keller else 770a69f1cb6SJacob Keller phy_sts = (val & Q_REG_FIFO02_M) >> Q_REG_FIFO02_S; 771a69f1cb6SJacob Keller 772a69f1cb6SJacob Keller if (phy_sts & FIFO_EMPTY) { 773a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK; 774a69f1cb6SJacob Keller return 0; 775a69f1cb6SJacob Keller } 776a69f1cb6SJacob Keller 777a69f1cb6SJacob Keller port->tx_fifo_busy_cnt++; 778a69f1cb6SJacob Keller 779a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf), "Try %d, port %d FIFO not empty\n", 780a69f1cb6SJacob Keller port->tx_fifo_busy_cnt, port->port_num); 781a69f1cb6SJacob Keller 782a69f1cb6SJacob Keller if (port->tx_fifo_busy_cnt == ICE_PTP_FIFO_NUM_CHECKS) { 783a69f1cb6SJacob Keller dev_dbg(ice_pf_to_dev(pf), 784a69f1cb6SJacob Keller "Port %d Tx FIFO still not empty; resetting quad %d\n", 785a69f1cb6SJacob Keller port->port_num, quad); 786a69f1cb6SJacob Keller ice_ptp_reset_ts_memory_quad(pf, quad); 787a69f1cb6SJacob Keller port->tx_fifo_busy_cnt = FIFO_OK; 788a69f1cb6SJacob Keller return 0; 789a69f1cb6SJacob Keller } 790a69f1cb6SJacob Keller 791a69f1cb6SJacob Keller return -EAGAIN; 792a69f1cb6SJacob Keller } 793a69f1cb6SJacob Keller 794a69f1cb6SJacob Keller /** 795a69f1cb6SJacob Keller * ice_ptp_check_tx_offset_valid - Check if the Tx PHY offset is valid 796a69f1cb6SJacob Keller * @port: the PTP port to check 797a69f1cb6SJacob Keller * 798a69f1cb6SJacob Keller * Checks whether the Tx offset for the PHY associated with this port is 799a69f1cb6SJacob Keller * valid. Returns 0 if the offset is valid, and a non-zero error code if it is 800a69f1cb6SJacob Keller * not. 801a69f1cb6SJacob Keller */ 802a69f1cb6SJacob Keller static int ice_ptp_check_tx_offset_valid(struct ice_ptp_port *port) 803a69f1cb6SJacob Keller { 804a69f1cb6SJacob Keller struct ice_pf *pf = ptp_port_to_pf(port); 805a69f1cb6SJacob Keller struct device *dev = ice_pf_to_dev(pf); 806a69f1cb6SJacob Keller struct ice_hw *hw = &pf->hw; 807a69f1cb6SJacob Keller u32 val; 808a69f1cb6SJacob Keller int err; 809a69f1cb6SJacob Keller 810a69f1cb6SJacob Keller err = ice_ptp_check_tx_fifo(port); 811a69f1cb6SJacob Keller if (err) 812a69f1cb6SJacob Keller return err; 813a69f1cb6SJacob Keller 814a69f1cb6SJacob Keller err = ice_read_phy_reg_e822(hw, port->port_num, P_REG_TX_OV_STATUS, 815a69f1cb6SJacob Keller &val); 816a69f1cb6SJacob Keller if (err) { 817a69f1cb6SJacob Keller dev_err(dev, "Failed to read TX_OV_STATUS for port %d, err %d\n", 818a69f1cb6SJacob Keller port->port_num, err); 819a69f1cb6SJacob Keller return -EAGAIN; 820a69f1cb6SJacob Keller } 821a69f1cb6SJacob Keller 822a69f1cb6SJacob Keller if (!(val & P_REG_TX_OV_STATUS_OV_M)) 823a69f1cb6SJacob Keller return -EAGAIN; 824a69f1cb6SJacob Keller 825a69f1cb6SJacob Keller return 0; 826a69f1cb6SJacob Keller } 827a69f1cb6SJacob Keller 828a69f1cb6SJacob Keller /** 829a69f1cb6SJacob Keller * ice_ptp_check_rx_offset_valid - Check if the Rx PHY offset is valid 830a69f1cb6SJacob Keller * @port: the PTP port to check 831a69f1cb6SJacob Keller * 832a69f1cb6SJacob Keller * Checks whether the Rx offset for the PHY associated with this port is 833a69f1cb6SJacob Keller * valid. Returns 0 if the offset is valid, and a non-zero error code if it is 834a69f1cb6SJacob Keller * not. 835a69f1cb6SJacob Keller */ 836a69f1cb6SJacob Keller static int ice_ptp_check_rx_offset_valid(struct ice_ptp_port *port) 837a69f1cb6SJacob Keller { 838a69f1cb6SJacob Keller struct ice_pf *pf = ptp_port_to_pf(port); 839a69f1cb6SJacob Keller struct device *dev = ice_pf_to_dev(pf); 840a69f1cb6SJacob Keller struct ice_hw *hw = &pf->hw; 841a69f1cb6SJacob Keller int err; 842a69f1cb6SJacob Keller u32 val; 843a69f1cb6SJacob Keller 844a69f1cb6SJacob Keller err = ice_read_phy_reg_e822(hw, port->port_num, P_REG_RX_OV_STATUS, 845a69f1cb6SJacob Keller &val); 846a69f1cb6SJacob Keller if (err) { 847a69f1cb6SJacob Keller dev_err(dev, "Failed to read RX_OV_STATUS for port %d, err %d\n", 848a69f1cb6SJacob Keller port->port_num, err); 849a69f1cb6SJacob Keller return err; 850a69f1cb6SJacob Keller } 851a69f1cb6SJacob Keller 852a69f1cb6SJacob Keller if (!(val & P_REG_RX_OV_STATUS_OV_M)) 853a69f1cb6SJacob Keller return -EAGAIN; 854a69f1cb6SJacob Keller 855a69f1cb6SJacob Keller return 0; 856a69f1cb6SJacob Keller } 857a69f1cb6SJacob Keller 858a69f1cb6SJacob Keller /** 859a69f1cb6SJacob Keller * ice_ptp_check_offset_valid - Check port offset valid bit 860a69f1cb6SJacob Keller * @port: Port for which offset valid bit is checked 861a69f1cb6SJacob Keller * 862a69f1cb6SJacob Keller * Returns 0 if both Tx and Rx offset are valid, and -EAGAIN if one of the 863a69f1cb6SJacob Keller * offset is not ready. 864a69f1cb6SJacob Keller */ 865a69f1cb6SJacob Keller static int ice_ptp_check_offset_valid(struct ice_ptp_port *port) 866a69f1cb6SJacob Keller { 867a69f1cb6SJacob Keller int tx_err, rx_err; 868a69f1cb6SJacob Keller 869a69f1cb6SJacob Keller /* always check both Tx and Rx offset validity */ 870a69f1cb6SJacob Keller tx_err = ice_ptp_check_tx_offset_valid(port); 871a69f1cb6SJacob Keller rx_err = ice_ptp_check_rx_offset_valid(port); 872a69f1cb6SJacob Keller 873a69f1cb6SJacob Keller if (tx_err || rx_err) 874a69f1cb6SJacob Keller return -EAGAIN; 875a69f1cb6SJacob Keller 876a69f1cb6SJacob Keller return 0; 877a69f1cb6SJacob Keller } 878a69f1cb6SJacob Keller 879a69f1cb6SJacob Keller /** 880a69f1cb6SJacob Keller * ice_ptp_wait_for_offset_valid - Check for valid Tx and Rx offsets 881a69f1cb6SJacob Keller * @work: Pointer to the kthread_work structure for this task 882a69f1cb6SJacob Keller * 883a69f1cb6SJacob Keller * Check whether both the Tx and Rx offsets are valid for enabling the vernier 884a69f1cb6SJacob Keller * calibration. 885a69f1cb6SJacob Keller * 886a69f1cb6SJacob Keller * Once we have valid offsets from hardware, update the total Tx and Rx 887a69f1cb6SJacob Keller * offsets, and exit bypass mode. This enables more precise timestamps using 888a69f1cb6SJacob Keller * the extra data measured during the vernier calibration process. 889a69f1cb6SJacob Keller */ 890a69f1cb6SJacob Keller static void ice_ptp_wait_for_offset_valid(struct kthread_work *work) 891a69f1cb6SJacob Keller { 892a69f1cb6SJacob Keller struct ice_ptp_port *port; 893a69f1cb6SJacob Keller int err; 894a69f1cb6SJacob Keller struct device *dev; 895a69f1cb6SJacob Keller struct ice_pf *pf; 896a69f1cb6SJacob Keller struct ice_hw *hw; 897a69f1cb6SJacob Keller 898a69f1cb6SJacob Keller port = container_of(work, struct ice_ptp_port, ov_work.work); 899a69f1cb6SJacob Keller pf = ptp_port_to_pf(port); 900a69f1cb6SJacob Keller hw = &pf->hw; 901a69f1cb6SJacob Keller dev = ice_pf_to_dev(pf); 902a69f1cb6SJacob Keller 903a69f1cb6SJacob Keller if (ice_ptp_check_offset_valid(port)) { 904a69f1cb6SJacob Keller /* Offsets not ready yet, try again later */ 905a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, 906a69f1cb6SJacob Keller &port->ov_work, 907a69f1cb6SJacob Keller msecs_to_jiffies(100)); 908a69f1cb6SJacob Keller return; 909a69f1cb6SJacob Keller } 910a69f1cb6SJacob Keller 911a69f1cb6SJacob Keller /* Offsets are valid, so it is safe to exit bypass mode */ 912a69f1cb6SJacob Keller err = ice_phy_exit_bypass_e822(hw, port->port_num); 913a69f1cb6SJacob Keller if (err) { 914a69f1cb6SJacob Keller dev_warn(dev, "Failed to exit bypass mode for PHY port %u, err %d\n", 915a69f1cb6SJacob Keller port->port_num, err); 916a69f1cb6SJacob Keller return; 917a69f1cb6SJacob Keller } 918a69f1cb6SJacob Keller } 919a69f1cb6SJacob Keller 920a69f1cb6SJacob Keller /** 9213a749623SJacob Keller * ice_ptp_port_phy_stop - Stop timestamping for a PHY port 9223a749623SJacob Keller * @ptp_port: PTP port to stop 9233a749623SJacob Keller */ 9243a749623SJacob Keller static int 9253a749623SJacob Keller ice_ptp_port_phy_stop(struct ice_ptp_port *ptp_port) 9263a749623SJacob Keller { 9273a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port); 9283a749623SJacob Keller u8 port = ptp_port->port_num; 9293a749623SJacob Keller struct ice_hw *hw = &pf->hw; 9303a749623SJacob Keller int err; 9313a749623SJacob Keller 9323a749623SJacob Keller if (ice_is_e810(hw)) 9333a749623SJacob Keller return 0; 9343a749623SJacob Keller 9353a749623SJacob Keller mutex_lock(&ptp_port->ps_lock); 9363a749623SJacob Keller 937a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work); 938a69f1cb6SJacob Keller 9393a749623SJacob Keller err = ice_stop_phy_timer_e822(hw, port, true); 9403a749623SJacob Keller if (err) 9413a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d down, err %d\n", 9423a749623SJacob Keller port, err); 9433a749623SJacob Keller 9443a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock); 9453a749623SJacob Keller 9463a749623SJacob Keller return err; 9473a749623SJacob Keller } 9483a749623SJacob Keller 9493a749623SJacob Keller /** 9503a749623SJacob Keller * ice_ptp_port_phy_restart - (Re)start and calibrate PHY timestamping 9513a749623SJacob Keller * @ptp_port: PTP port for which the PHY start is set 9523a749623SJacob Keller * 9533a749623SJacob Keller * Start the PHY timestamping block, and initiate Vernier timestamping 9543a749623SJacob Keller * calibration. If timestamping cannot be calibrated (such as if link is down) 9553a749623SJacob Keller * then disable the timestamping block instead. 9563a749623SJacob Keller */ 9573a749623SJacob Keller static int 9583a749623SJacob Keller ice_ptp_port_phy_restart(struct ice_ptp_port *ptp_port) 9593a749623SJacob Keller { 9603a749623SJacob Keller struct ice_pf *pf = ptp_port_to_pf(ptp_port); 9613a749623SJacob Keller u8 port = ptp_port->port_num; 9623a749623SJacob Keller struct ice_hw *hw = &pf->hw; 9633a749623SJacob Keller int err; 9643a749623SJacob Keller 9653a749623SJacob Keller if (ice_is_e810(hw)) 9663a749623SJacob Keller return 0; 9673a749623SJacob Keller 9683a749623SJacob Keller if (!ptp_port->link_up) 9693a749623SJacob Keller return ice_ptp_port_phy_stop(ptp_port); 9703a749623SJacob Keller 9713a749623SJacob Keller mutex_lock(&ptp_port->ps_lock); 9723a749623SJacob Keller 973a69f1cb6SJacob Keller kthread_cancel_delayed_work_sync(&ptp_port->ov_work); 974a69f1cb6SJacob Keller 9753a749623SJacob Keller /* temporarily disable Tx timestamps while calibrating PHY offset */ 9763a749623SJacob Keller ptp_port->tx.calibrating = true; 977a69f1cb6SJacob Keller ptp_port->tx_fifo_busy_cnt = 0; 9783a749623SJacob Keller 9793a749623SJacob Keller /* Start the PHY timer in bypass mode */ 9803a749623SJacob Keller err = ice_start_phy_timer_e822(hw, port, true); 9813a749623SJacob Keller if (err) 9823a749623SJacob Keller goto out_unlock; 9833a749623SJacob Keller 9843a749623SJacob Keller /* Enable Tx timestamps right away */ 9853a749623SJacob Keller ptp_port->tx.calibrating = false; 9863a749623SJacob Keller 987a69f1cb6SJacob Keller kthread_queue_delayed_work(pf->ptp.kworker, &ptp_port->ov_work, 0); 988a69f1cb6SJacob Keller 9893a749623SJacob Keller out_unlock: 9903a749623SJacob Keller if (err) 9913a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set PHY port %d up, err %d\n", 9923a749623SJacob Keller port, err); 9933a749623SJacob Keller 9943a749623SJacob Keller mutex_unlock(&ptp_port->ps_lock); 9953a749623SJacob Keller 9963a749623SJacob Keller return err; 9973a749623SJacob Keller } 9983a749623SJacob Keller 9993a749623SJacob Keller /** 10003a749623SJacob Keller * ice_ptp_link_change - Set or clear port registers for timestamping 10013a749623SJacob Keller * @pf: Board private structure 10023a749623SJacob Keller * @port: Port for which the PHY start is set 10033a749623SJacob Keller * @linkup: Link is up or down 10043a749623SJacob Keller */ 10053a749623SJacob Keller int ice_ptp_link_change(struct ice_pf *pf, u8 port, bool linkup) 10063a749623SJacob Keller { 10073a749623SJacob Keller struct ice_ptp_port *ptp_port; 10083a749623SJacob Keller 10093a749623SJacob Keller if (!test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags)) 10103a749623SJacob Keller return 0; 10113a749623SJacob Keller 10123a749623SJacob Keller if (port >= ICE_NUM_EXTERNAL_PORTS) 10133a749623SJacob Keller return -EINVAL; 10143a749623SJacob Keller 10153a749623SJacob Keller ptp_port = &pf->ptp.port; 10163a749623SJacob Keller if (ptp_port->port_num != port) 10173a749623SJacob Keller return -EINVAL; 10183a749623SJacob Keller 10193a749623SJacob Keller /* Update cached link err for this port immediately */ 10203a749623SJacob Keller ptp_port->link_up = linkup; 10213a749623SJacob Keller 10223a749623SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 10233a749623SJacob Keller /* PTP is not setup */ 10243a749623SJacob Keller return -EAGAIN; 10253a749623SJacob Keller 10263a749623SJacob Keller return ice_ptp_port_phy_restart(ptp_port); 10273a749623SJacob Keller } 10283a749623SJacob Keller 10293a749623SJacob Keller /** 10303a749623SJacob Keller * ice_ptp_reset_ts_memory - Reset timestamp memory for all quads 10313a749623SJacob Keller * @pf: The PF private data structure 10323a749623SJacob Keller */ 10333a749623SJacob Keller static void ice_ptp_reset_ts_memory(struct ice_pf *pf) 10343a749623SJacob Keller { 10353a749623SJacob Keller int quad; 10363a749623SJacob Keller 10373a749623SJacob Keller quad = pf->hw.port_info->lport / ICE_PORTS_PER_QUAD; 10383a749623SJacob Keller ice_ptp_reset_ts_memory_quad(pf, quad); 10393a749623SJacob Keller } 10403a749623SJacob Keller 10413a749623SJacob Keller /** 10423a749623SJacob Keller * ice_ptp_tx_ena_intr - Enable or disable the Tx timestamp interrupt 10433a749623SJacob Keller * @pf: PF private structure 10443a749623SJacob Keller * @ena: bool value to enable or disable interrupt 10453a749623SJacob Keller * @threshold: Minimum number of packets at which intr is triggered 10463a749623SJacob Keller * 10473a749623SJacob Keller * Utility function to enable or disable Tx timestamp interrupt and threshold 10483a749623SJacob Keller */ 10493a749623SJacob Keller static int ice_ptp_tx_ena_intr(struct ice_pf *pf, bool ena, u32 threshold) 10503a749623SJacob Keller { 10513a749623SJacob Keller struct ice_hw *hw = &pf->hw; 10523a749623SJacob Keller int err = 0; 10533a749623SJacob Keller int quad; 10543a749623SJacob Keller u32 val; 10553a749623SJacob Keller 10563a749623SJacob Keller ice_ptp_reset_ts_memory(pf); 10573a749623SJacob Keller 10583a749623SJacob Keller for (quad = 0; quad < ICE_MAX_QUAD; quad++) { 10593a749623SJacob Keller err = ice_read_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, 10603a749623SJacob Keller &val); 10613a749623SJacob Keller if (err) 10623a749623SJacob Keller break; 10633a749623SJacob Keller 10643a749623SJacob Keller if (ena) { 10653a749623SJacob Keller val |= Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M; 10663a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_THR_M; 10673a749623SJacob Keller val |= ((threshold << Q_REG_TX_MEM_GBL_CFG_INTR_THR_S) & 10683a749623SJacob Keller Q_REG_TX_MEM_GBL_CFG_INTR_THR_M); 10693a749623SJacob Keller } else { 10703a749623SJacob Keller val &= ~Q_REG_TX_MEM_GBL_CFG_INTR_ENA_M; 10713a749623SJacob Keller } 10723a749623SJacob Keller 10733a749623SJacob Keller err = ice_write_quad_reg_e822(hw, quad, Q_REG_TX_MEM_GBL_CFG, 10743a749623SJacob Keller val); 10753a749623SJacob Keller if (err) 10763a749623SJacob Keller break; 10773a749623SJacob Keller } 10783a749623SJacob Keller 10793a749623SJacob Keller if (err) 10803a749623SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed in intr ena, err %d\n", 10813a749623SJacob Keller err); 10823a749623SJacob Keller return err; 10833a749623SJacob Keller } 10843a749623SJacob Keller 10853a749623SJacob Keller /** 10863a749623SJacob Keller * ice_ptp_reset_phy_timestamping - Reset PHY timestamping block 10873a749623SJacob Keller * @pf: Board private structure 10883a749623SJacob Keller */ 10893a749623SJacob Keller static void ice_ptp_reset_phy_timestamping(struct ice_pf *pf) 10903a749623SJacob Keller { 10913a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port); 109278267d0cSJacob Keller } 109378267d0cSJacob Keller 109478267d0cSJacob Keller /** 109506c16d89SJacob Keller * ice_ptp_adjfine - Adjust clock increment rate 109606c16d89SJacob Keller * @info: the driver's PTP info structure 109706c16d89SJacob Keller * @scaled_ppm: Parts per million with 16-bit fractional field 109806c16d89SJacob Keller * 109906c16d89SJacob Keller * Adjust the frequency of the clock by the indicated scaled ppm from the 110006c16d89SJacob Keller * base frequency. 110106c16d89SJacob Keller */ 110206c16d89SJacob Keller static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm) 110306c16d89SJacob Keller { 110406c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 110506c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 1106*4488df14SJacob Keller u64 incval, diff; 110706c16d89SJacob Keller int neg_adj = 0; 110806c16d89SJacob Keller int err; 110906c16d89SJacob Keller 111078267d0cSJacob Keller incval = ice_base_incval(pf); 111106c16d89SJacob Keller 111206c16d89SJacob Keller if (scaled_ppm < 0) { 111306c16d89SJacob Keller neg_adj = 1; 111406c16d89SJacob Keller scaled_ppm = -scaled_ppm; 111506c16d89SJacob Keller } 111606c16d89SJacob Keller 1117*4488df14SJacob Keller diff = mul_u64_u64_div_u64(incval, (u64)scaled_ppm, 1118*4488df14SJacob Keller 1000000ULL << 16); 111906c16d89SJacob Keller if (neg_adj) 112006c16d89SJacob Keller incval -= diff; 112106c16d89SJacob Keller else 112206c16d89SJacob Keller incval += diff; 112306c16d89SJacob Keller 112406c16d89SJacob Keller err = ice_ptp_write_incval_locked(hw, incval); 112506c16d89SJacob Keller if (err) { 112606c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n", 112706c16d89SJacob Keller err); 112806c16d89SJacob Keller return -EIO; 112906c16d89SJacob Keller } 113006c16d89SJacob Keller 113106c16d89SJacob Keller return 0; 113206c16d89SJacob Keller } 113306c16d89SJacob Keller 113406c16d89SJacob Keller /** 1135172db5f9SMaciej Machnikowski * ice_ptp_extts_work - Workqueue task function 1136172db5f9SMaciej Machnikowski * @work: external timestamp work structure 1137172db5f9SMaciej Machnikowski * 1138172db5f9SMaciej Machnikowski * Service for PTP external clock event 1139172db5f9SMaciej Machnikowski */ 1140172db5f9SMaciej Machnikowski static void ice_ptp_extts_work(struct kthread_work *work) 1141172db5f9SMaciej Machnikowski { 1142172db5f9SMaciej Machnikowski struct ice_ptp *ptp = container_of(work, struct ice_ptp, extts_work); 1143172db5f9SMaciej Machnikowski struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 1144172db5f9SMaciej Machnikowski struct ptp_clock_event event; 1145172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1146172db5f9SMaciej Machnikowski u8 chan, tmr_idx; 1147172db5f9SMaciej Machnikowski u32 hi, lo; 1148172db5f9SMaciej Machnikowski 1149172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1150172db5f9SMaciej Machnikowski /* Event time is captured by one of the two matched registers 1151172db5f9SMaciej Machnikowski * GLTSYN_EVNT_L: 32 LSB of sampled time event 1152172db5f9SMaciej Machnikowski * GLTSYN_EVNT_H: 32 MSB of sampled time event 1153172db5f9SMaciej Machnikowski * Event is defined in GLTSYN_EVNT_0 register 1154172db5f9SMaciej Machnikowski */ 1155172db5f9SMaciej Machnikowski for (chan = 0; chan < GLTSYN_EVNT_H_IDX_MAX; chan++) { 1156172db5f9SMaciej Machnikowski /* Check if channel is enabled */ 1157172db5f9SMaciej Machnikowski if (pf->ptp.ext_ts_irq & (1 << chan)) { 1158172db5f9SMaciej Machnikowski lo = rd32(hw, GLTSYN_EVNT_L(chan, tmr_idx)); 1159172db5f9SMaciej Machnikowski hi = rd32(hw, GLTSYN_EVNT_H(chan, tmr_idx)); 1160172db5f9SMaciej Machnikowski event.timestamp = (((u64)hi) << 32) | lo; 1161172db5f9SMaciej Machnikowski event.type = PTP_CLOCK_EXTTS; 1162172db5f9SMaciej Machnikowski event.index = chan; 1163172db5f9SMaciej Machnikowski 1164172db5f9SMaciej Machnikowski /* Fire event */ 1165172db5f9SMaciej Machnikowski ptp_clock_event(pf->ptp.clock, &event); 1166172db5f9SMaciej Machnikowski pf->ptp.ext_ts_irq &= ~(1 << chan); 1167172db5f9SMaciej Machnikowski } 1168172db5f9SMaciej Machnikowski } 1169172db5f9SMaciej Machnikowski } 1170172db5f9SMaciej Machnikowski 1171172db5f9SMaciej Machnikowski /** 1172172db5f9SMaciej Machnikowski * ice_ptp_cfg_extts - Configure EXTTS pin and channel 1173172db5f9SMaciej Machnikowski * @pf: Board private structure 1174172db5f9SMaciej Machnikowski * @ena: true to enable; false to disable 1175172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 1176172db5f9SMaciej Machnikowski * @gpio_pin: GPIO pin 1177172db5f9SMaciej Machnikowski * @extts_flags: request flags from the ptp_extts_request.flags 1178172db5f9SMaciej Machnikowski */ 1179172db5f9SMaciej Machnikowski static int 1180172db5f9SMaciej Machnikowski ice_ptp_cfg_extts(struct ice_pf *pf, bool ena, unsigned int chan, u32 gpio_pin, 1181172db5f9SMaciej Machnikowski unsigned int extts_flags) 1182172db5f9SMaciej Machnikowski { 1183172db5f9SMaciej Machnikowski u32 func, aux_reg, gpio_reg, irq_reg; 1184172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1185172db5f9SMaciej Machnikowski u8 tmr_idx; 1186172db5f9SMaciej Machnikowski 1187172db5f9SMaciej Machnikowski if (chan > (unsigned int)pf->ptp.info.n_ext_ts) 1188172db5f9SMaciej Machnikowski return -EINVAL; 1189172db5f9SMaciej Machnikowski 1190172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1191172db5f9SMaciej Machnikowski 1192172db5f9SMaciej Machnikowski irq_reg = rd32(hw, PFINT_OICR_ENA); 1193172db5f9SMaciej Machnikowski 1194172db5f9SMaciej Machnikowski if (ena) { 1195172db5f9SMaciej Machnikowski /* Enable the interrupt */ 1196172db5f9SMaciej Machnikowski irq_reg |= PFINT_OICR_TSYN_EVNT_M; 1197172db5f9SMaciej Machnikowski aux_reg = GLTSYN_AUX_IN_0_INT_ENA_M; 1198172db5f9SMaciej Machnikowski 1199172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE BIT(0) 1200172db5f9SMaciej Machnikowski #define GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE BIT(1) 1201172db5f9SMaciej Machnikowski 1202172db5f9SMaciej Machnikowski /* set event level to requested edge */ 1203172db5f9SMaciej Machnikowski if (extts_flags & PTP_FALLING_EDGE) 1204172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_FALLING_EDGE; 1205172db5f9SMaciej Machnikowski if (extts_flags & PTP_RISING_EDGE) 1206172db5f9SMaciej Machnikowski aux_reg |= GLTSYN_AUX_IN_0_EVNTLVL_RISING_EDGE; 1207172db5f9SMaciej Machnikowski 1208172db5f9SMaciej Machnikowski /* Write GPIO CTL reg. 1209172db5f9SMaciej Machnikowski * 0x1 is input sampled by EVENT register(channel) 1210172db5f9SMaciej Machnikowski * + num_in_channels * tmr_idx 1211172db5f9SMaciej Machnikowski */ 1212172db5f9SMaciej Machnikowski func = 1 + chan + (tmr_idx * 3); 1213172db5f9SMaciej Machnikowski gpio_reg = ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & 1214172db5f9SMaciej Machnikowski GLGEN_GPIO_CTL_PIN_FUNC_M); 1215172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan |= (1 << chan); 1216172db5f9SMaciej Machnikowski } else { 1217172db5f9SMaciej Machnikowski /* clear the values we set to reset defaults */ 1218172db5f9SMaciej Machnikowski aux_reg = 0; 1219172db5f9SMaciej Machnikowski gpio_reg = 0; 1220172db5f9SMaciej Machnikowski pf->ptp.ext_ts_chan &= ~(1 << chan); 1221172db5f9SMaciej Machnikowski if (!pf->ptp.ext_ts_chan) 1222172db5f9SMaciej Machnikowski irq_reg &= ~PFINT_OICR_TSYN_EVNT_M; 1223172db5f9SMaciej Machnikowski } 1224172db5f9SMaciej Machnikowski 1225172db5f9SMaciej Machnikowski wr32(hw, PFINT_OICR_ENA, irq_reg); 1226172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_IN(chan, tmr_idx), aux_reg); 1227172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), gpio_reg); 1228172db5f9SMaciej Machnikowski 1229172db5f9SMaciej Machnikowski return 0; 1230172db5f9SMaciej Machnikowski } 1231172db5f9SMaciej Machnikowski 1232172db5f9SMaciej Machnikowski /** 1233172db5f9SMaciej Machnikowski * ice_ptp_cfg_clkout - Configure clock to generate periodic wave 1234172db5f9SMaciej Machnikowski * @pf: Board private structure 1235172db5f9SMaciej Machnikowski * @chan: GPIO channel (0-3) 1236172db5f9SMaciej Machnikowski * @config: desired periodic clk configuration. NULL will disable channel 1237172db5f9SMaciej Machnikowski * @store: If set to true the values will be stored 1238172db5f9SMaciej Machnikowski * 1239172db5f9SMaciej Machnikowski * Configure the internal clock generator modules to generate the clock wave of 1240172db5f9SMaciej Machnikowski * specified period. 1241172db5f9SMaciej Machnikowski */ 1242172db5f9SMaciej Machnikowski static int ice_ptp_cfg_clkout(struct ice_pf *pf, unsigned int chan, 1243172db5f9SMaciej Machnikowski struct ice_perout_channel *config, bool store) 1244172db5f9SMaciej Machnikowski { 1245172db5f9SMaciej Machnikowski u64 current_time, period, start_time, phase; 1246172db5f9SMaciej Machnikowski struct ice_hw *hw = &pf->hw; 1247172db5f9SMaciej Machnikowski u32 func, val, gpio_pin; 1248172db5f9SMaciej Machnikowski u8 tmr_idx; 1249172db5f9SMaciej Machnikowski 1250172db5f9SMaciej Machnikowski tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1251172db5f9SMaciej Machnikowski 1252172db5f9SMaciej Machnikowski /* 0. Reset mode & out_en in AUX_OUT */ 1253172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), 0); 1254172db5f9SMaciej Machnikowski 1255172db5f9SMaciej Machnikowski /* If we're disabling the output, clear out CLKO and TGT and keep 1256172db5f9SMaciej Machnikowski * output level low 1257172db5f9SMaciej Machnikowski */ 1258172db5f9SMaciej Machnikowski if (!config || !config->ena) { 1259172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), 0); 1260172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), 0); 1261172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), 0); 1262172db5f9SMaciej Machnikowski 1263172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M; 1264172db5f9SMaciej Machnikowski gpio_pin = pf->ptp.perout_channels[chan].gpio_pin; 1265172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 1266172db5f9SMaciej Machnikowski 1267172db5f9SMaciej Machnikowski /* Store the value if requested */ 1268172db5f9SMaciej Machnikowski if (store) 1269172db5f9SMaciej Machnikowski memset(&pf->ptp.perout_channels[chan], 0, 1270172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 1271172db5f9SMaciej Machnikowski 1272172db5f9SMaciej Machnikowski return 0; 1273172db5f9SMaciej Machnikowski } 1274172db5f9SMaciej Machnikowski period = config->period; 1275172db5f9SMaciej Machnikowski start_time = config->start_time; 1276172db5f9SMaciej Machnikowski div64_u64_rem(start_time, period, &phase); 1277172db5f9SMaciej Machnikowski gpio_pin = config->gpio_pin; 1278172db5f9SMaciej Machnikowski 1279172db5f9SMaciej Machnikowski /* 1. Write clkout with half of required period value */ 1280172db5f9SMaciej Machnikowski if (period & 0x1) { 1281172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be an even value\n"); 1282172db5f9SMaciej Machnikowski goto err; 1283172db5f9SMaciej Machnikowski } 1284172db5f9SMaciej Machnikowski 1285172db5f9SMaciej Machnikowski period >>= 1; 1286172db5f9SMaciej Machnikowski 1287172db5f9SMaciej Machnikowski /* For proper operation, the GLTSYN_CLKO must be larger than clock tick 1288172db5f9SMaciej Machnikowski */ 1289172db5f9SMaciej Machnikowski #define MIN_PULSE 3 1290172db5f9SMaciej Machnikowski if (period <= MIN_PULSE || period > U32_MAX) { 1291172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "CLK Period must be > %d && < 2^33", 1292172db5f9SMaciej Machnikowski MIN_PULSE * 2); 1293172db5f9SMaciej Machnikowski goto err; 1294172db5f9SMaciej Machnikowski } 1295172db5f9SMaciej Machnikowski 1296172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_CLKO(chan, tmr_idx), lower_32_bits(period)); 1297172db5f9SMaciej Machnikowski 1298172db5f9SMaciej Machnikowski /* Allow time for programming before start_time is hit */ 1299172db5f9SMaciej Machnikowski current_time = ice_ptp_read_src_clk_reg(pf, NULL); 1300172db5f9SMaciej Machnikowski 1301172db5f9SMaciej Machnikowski /* if start time is in the past start the timer at the nearest second 1302172db5f9SMaciej Machnikowski * maintaining phase 1303172db5f9SMaciej Machnikowski */ 1304172db5f9SMaciej Machnikowski if (start_time < current_time) 13055f773519SMaciej Machnikowski start_time = div64_u64(current_time + NSEC_PER_SEC - 1, 1306172db5f9SMaciej Machnikowski NSEC_PER_SEC) * NSEC_PER_SEC + phase; 1307172db5f9SMaciej Machnikowski 13083a749623SJacob Keller if (ice_is_e810(hw)) 1309172db5f9SMaciej Machnikowski start_time -= E810_OUT_PROP_DELAY_NS; 13103a749623SJacob Keller else 13113a749623SJacob Keller start_time -= ice_e822_pps_delay(ice_e822_time_ref(hw)); 1312172db5f9SMaciej Machnikowski 1313172db5f9SMaciej Machnikowski /* 2. Write TARGET time */ 1314172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_L(chan, tmr_idx), lower_32_bits(start_time)); 1315172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_TGT_H(chan, tmr_idx), upper_32_bits(start_time)); 1316172db5f9SMaciej Machnikowski 1317172db5f9SMaciej Machnikowski /* 3. Write AUX_OUT register */ 1318172db5f9SMaciej Machnikowski val = GLTSYN_AUX_OUT_0_OUT_ENA_M | GLTSYN_AUX_OUT_0_OUTMOD_M; 1319172db5f9SMaciej Machnikowski wr32(hw, GLTSYN_AUX_OUT(chan, tmr_idx), val); 1320172db5f9SMaciej Machnikowski 1321172db5f9SMaciej Machnikowski /* 4. write GPIO CTL reg */ 1322172db5f9SMaciej Machnikowski func = 8 + chan + (tmr_idx * 4); 1323172db5f9SMaciej Machnikowski val = GLGEN_GPIO_CTL_PIN_DIR_M | 1324172db5f9SMaciej Machnikowski ((func << GLGEN_GPIO_CTL_PIN_FUNC_S) & GLGEN_GPIO_CTL_PIN_FUNC_M); 1325172db5f9SMaciej Machnikowski wr32(hw, GLGEN_GPIO_CTL(gpio_pin), val); 1326172db5f9SMaciej Machnikowski 1327172db5f9SMaciej Machnikowski /* Store the value if requested */ 1328172db5f9SMaciej Machnikowski if (store) { 1329172db5f9SMaciej Machnikowski memcpy(&pf->ptp.perout_channels[chan], config, 1330172db5f9SMaciej Machnikowski sizeof(struct ice_perout_channel)); 1331172db5f9SMaciej Machnikowski pf->ptp.perout_channels[chan].start_time = phase; 1332172db5f9SMaciej Machnikowski } 1333172db5f9SMaciej Machnikowski 1334172db5f9SMaciej Machnikowski return 0; 1335172db5f9SMaciej Machnikowski err: 1336172db5f9SMaciej Machnikowski dev_err(ice_pf_to_dev(pf), "PTP failed to cfg per_clk\n"); 1337172db5f9SMaciej Machnikowski return -EFAULT; 1338172db5f9SMaciej Machnikowski } 1339172db5f9SMaciej Machnikowski 1340172db5f9SMaciej Machnikowski /** 13419ee31343SJacob Keller * ice_ptp_disable_all_clkout - Disable all currently configured outputs 13429ee31343SJacob Keller * @pf: pointer to the PF structure 13439ee31343SJacob Keller * 13449ee31343SJacob Keller * Disable all currently configured clock outputs. This is necessary before 13459ee31343SJacob Keller * certain changes to the PTP hardware clock. Use ice_ptp_enable_all_clkout to 13469ee31343SJacob Keller * re-enable the clocks again. 13479ee31343SJacob Keller */ 13489ee31343SJacob Keller static void ice_ptp_disable_all_clkout(struct ice_pf *pf) 13499ee31343SJacob Keller { 13509ee31343SJacob Keller uint i; 13519ee31343SJacob Keller 13529ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++) 13539ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena) 13549ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, NULL, false); 13559ee31343SJacob Keller } 13569ee31343SJacob Keller 13579ee31343SJacob Keller /** 13589ee31343SJacob Keller * ice_ptp_enable_all_clkout - Enable all configured periodic clock outputs 13599ee31343SJacob Keller * @pf: pointer to the PF structure 13609ee31343SJacob Keller * 13619ee31343SJacob Keller * Enable all currently configured clock outputs. Use this after 13629ee31343SJacob Keller * ice_ptp_disable_all_clkout to reconfigure the output signals according to 13639ee31343SJacob Keller * their configuration. 13649ee31343SJacob Keller */ 13659ee31343SJacob Keller static void ice_ptp_enable_all_clkout(struct ice_pf *pf) 13669ee31343SJacob Keller { 13679ee31343SJacob Keller uint i; 13689ee31343SJacob Keller 13699ee31343SJacob Keller for (i = 0; i < pf->ptp.info.n_per_out; i++) 13709ee31343SJacob Keller if (pf->ptp.perout_channels[i].ena) 13719ee31343SJacob Keller ice_ptp_cfg_clkout(pf, i, &pf->ptp.perout_channels[i], 13729ee31343SJacob Keller false); 13739ee31343SJacob Keller } 13749ee31343SJacob Keller 13759ee31343SJacob Keller /** 1376172db5f9SMaciej Machnikowski * ice_ptp_gpio_enable_e810 - Enable/disable ancillary features of PHC 1377172db5f9SMaciej Machnikowski * @info: the driver's PTP info structure 1378172db5f9SMaciej Machnikowski * @rq: The requested feature to change 1379172db5f9SMaciej Machnikowski * @on: Enable/disable flag 1380172db5f9SMaciej Machnikowski */ 1381172db5f9SMaciej Machnikowski static int 1382172db5f9SMaciej Machnikowski ice_ptp_gpio_enable_e810(struct ptp_clock_info *info, 1383172db5f9SMaciej Machnikowski struct ptp_clock_request *rq, int on) 1384172db5f9SMaciej Machnikowski { 1385172db5f9SMaciej Machnikowski struct ice_pf *pf = ptp_info_to_pf(info); 1386172db5f9SMaciej Machnikowski struct ice_perout_channel clk_cfg = {0}; 1387325b2064SMaciej Machnikowski bool sma_pres = false; 1388172db5f9SMaciej Machnikowski unsigned int chan; 1389172db5f9SMaciej Machnikowski u32 gpio_pin; 1390172db5f9SMaciej Machnikowski int err; 1391172db5f9SMaciej Machnikowski 1392325b2064SMaciej Machnikowski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) 1393325b2064SMaciej Machnikowski sma_pres = true; 1394325b2064SMaciej Machnikowski 1395172db5f9SMaciej Machnikowski switch (rq->type) { 1396172db5f9SMaciej Machnikowski case PTP_CLK_REQ_PEROUT: 1397172db5f9SMaciej Machnikowski chan = rq->perout.index; 1398325b2064SMaciej Machnikowski if (sma_pres) { 1399325b2064SMaciej Machnikowski if (chan == ice_pin_desc_e810t[SMA1].chan) 1400325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20; 1401325b2064SMaciej Machnikowski else if (chan == ice_pin_desc_e810t[SMA2].chan) 1402325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22; 1403172db5f9SMaciej Machnikowski else 1404325b2064SMaciej Machnikowski return -1; 1405325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) { 1406325b2064SMaciej Machnikowski if (chan == 0) 1407325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_20; 1408325b2064SMaciej Machnikowski else 1409325b2064SMaciej Machnikowski clk_cfg.gpio_pin = GPIO_22; 1410325b2064SMaciej Machnikowski } else if (chan == PPS_CLK_GEN_CHAN) { 1411325b2064SMaciej Machnikowski clk_cfg.gpio_pin = PPS_PIN_INDEX; 1412325b2064SMaciej Machnikowski } else { 1413172db5f9SMaciej Machnikowski clk_cfg.gpio_pin = chan; 1414325b2064SMaciej Machnikowski } 1415172db5f9SMaciej Machnikowski 1416172db5f9SMaciej Machnikowski clk_cfg.period = ((rq->perout.period.sec * NSEC_PER_SEC) + 1417172db5f9SMaciej Machnikowski rq->perout.period.nsec); 1418172db5f9SMaciej Machnikowski clk_cfg.start_time = ((rq->perout.start.sec * NSEC_PER_SEC) + 1419172db5f9SMaciej Machnikowski rq->perout.start.nsec); 1420172db5f9SMaciej Machnikowski clk_cfg.ena = !!on; 1421172db5f9SMaciej Machnikowski 1422172db5f9SMaciej Machnikowski err = ice_ptp_cfg_clkout(pf, chan, &clk_cfg, true); 1423172db5f9SMaciej Machnikowski break; 1424172db5f9SMaciej Machnikowski case PTP_CLK_REQ_EXTTS: 1425172db5f9SMaciej Machnikowski chan = rq->extts.index; 1426325b2064SMaciej Machnikowski if (sma_pres) { 1427325b2064SMaciej Machnikowski if (chan < ice_pin_desc_e810t[SMA2].chan) 1428325b2064SMaciej Machnikowski gpio_pin = GPIO_21; 1429325b2064SMaciej Machnikowski else 1430325b2064SMaciej Machnikowski gpio_pin = GPIO_23; 1431325b2064SMaciej Machnikowski } else if (ice_is_e810t(&pf->hw)) { 1432325b2064SMaciej Machnikowski if (chan == 0) 1433325b2064SMaciej Machnikowski gpio_pin = GPIO_21; 1434325b2064SMaciej Machnikowski else 1435325b2064SMaciej Machnikowski gpio_pin = GPIO_23; 1436325b2064SMaciej Machnikowski } else { 1437172db5f9SMaciej Machnikowski gpio_pin = chan; 1438325b2064SMaciej Machnikowski } 1439172db5f9SMaciej Machnikowski 1440172db5f9SMaciej Machnikowski err = ice_ptp_cfg_extts(pf, !!on, chan, gpio_pin, 1441172db5f9SMaciej Machnikowski rq->extts.flags); 1442172db5f9SMaciej Machnikowski break; 1443172db5f9SMaciej Machnikowski default: 1444172db5f9SMaciej Machnikowski return -EOPNOTSUPP; 1445172db5f9SMaciej Machnikowski } 1446172db5f9SMaciej Machnikowski 1447172db5f9SMaciej Machnikowski return err; 1448172db5f9SMaciej Machnikowski } 1449172db5f9SMaciej Machnikowski 1450172db5f9SMaciej Machnikowski /** 145106c16d89SJacob Keller * ice_ptp_gettimex64 - Get the time of the clock 145206c16d89SJacob Keller * @info: the driver's PTP info structure 145306c16d89SJacob Keller * @ts: timespec64 structure to hold the current time value 145406c16d89SJacob Keller * @sts: Optional parameter for holding a pair of system timestamps from 145506c16d89SJacob Keller * the system clock. Will be ignored if NULL is given. 145606c16d89SJacob Keller * 145706c16d89SJacob Keller * Read the device clock and return the correct value on ns, after converting it 145806c16d89SJacob Keller * into a timespec struct. 145906c16d89SJacob Keller */ 146006c16d89SJacob Keller static int 146106c16d89SJacob Keller ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts, 146206c16d89SJacob Keller struct ptp_system_timestamp *sts) 146306c16d89SJacob Keller { 146406c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 146506c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 146606c16d89SJacob Keller 146706c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 146806c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n"); 146906c16d89SJacob Keller return -EBUSY; 147006c16d89SJacob Keller } 147106c16d89SJacob Keller 147206c16d89SJacob Keller ice_ptp_read_time(pf, ts, sts); 147306c16d89SJacob Keller ice_ptp_unlock(hw); 147406c16d89SJacob Keller 147506c16d89SJacob Keller return 0; 147606c16d89SJacob Keller } 147706c16d89SJacob Keller 147806c16d89SJacob Keller /** 147906c16d89SJacob Keller * ice_ptp_settime64 - Set the time of the clock 148006c16d89SJacob Keller * @info: the driver's PTP info structure 148106c16d89SJacob Keller * @ts: timespec64 structure that holds the new time value 148206c16d89SJacob Keller * 148306c16d89SJacob Keller * Set the device clock to the user input value. The conversion from timespec 148406c16d89SJacob Keller * to ns happens in the write function. 148506c16d89SJacob Keller */ 148606c16d89SJacob Keller static int 148706c16d89SJacob Keller ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts) 148806c16d89SJacob Keller { 148906c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 149006c16d89SJacob Keller struct timespec64 ts64 = *ts; 149106c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 149206c16d89SJacob Keller int err; 149306c16d89SJacob Keller 14943a749623SJacob Keller /* For Vernier mode, we need to recalibrate after new settime 14953a749623SJacob Keller * Start with disabling timestamp block 14963a749623SJacob Keller */ 14973a749623SJacob Keller if (pf->ptp.port.link_up) 14983a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port); 14993a749623SJacob Keller 150006c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 150106c16d89SJacob Keller err = -EBUSY; 150206c16d89SJacob Keller goto exit; 150306c16d89SJacob Keller } 150406c16d89SJacob Keller 15059ee31343SJacob Keller /* Disable periodic outputs */ 15069ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 15079ee31343SJacob Keller 150806c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts64); 150906c16d89SJacob Keller ice_ptp_unlock(hw); 151006c16d89SJacob Keller 151177a78115SJacob Keller if (!err) 151277a78115SJacob Keller ice_ptp_update_cached_phctime(pf); 151377a78115SJacob Keller 15149ee31343SJacob Keller /* Reenable periodic outputs */ 15159ee31343SJacob Keller ice_ptp_enable_all_clkout(pf); 15163a749623SJacob Keller 15173a749623SJacob Keller /* Recalibrate and re-enable timestamp block */ 15183a749623SJacob Keller if (pf->ptp.port.link_up) 15193a749623SJacob Keller ice_ptp_port_phy_restart(&pf->ptp.port); 152006c16d89SJacob Keller exit: 152106c16d89SJacob Keller if (err) { 152206c16d89SJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err); 152306c16d89SJacob Keller return err; 152406c16d89SJacob Keller } 152506c16d89SJacob Keller 152606c16d89SJacob Keller return 0; 152706c16d89SJacob Keller } 152806c16d89SJacob Keller 152906c16d89SJacob Keller /** 153006c16d89SJacob Keller * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment 153106c16d89SJacob Keller * @info: the driver's PTP info structure 153206c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 153306c16d89SJacob Keller */ 153406c16d89SJacob Keller static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta) 153506c16d89SJacob Keller { 153606c16d89SJacob Keller struct timespec64 now, then; 1537ed22d9c8STom Rix int ret; 153806c16d89SJacob Keller 153906c16d89SJacob Keller then = ns_to_timespec64(delta); 1540ed22d9c8STom Rix ret = ice_ptp_gettimex64(info, &now, NULL); 1541ed22d9c8STom Rix if (ret) 1542ed22d9c8STom Rix return ret; 154306c16d89SJacob Keller now = timespec64_add(now, then); 154406c16d89SJacob Keller 154506c16d89SJacob Keller return ice_ptp_settime64(info, (const struct timespec64 *)&now); 154606c16d89SJacob Keller } 154706c16d89SJacob Keller 154806c16d89SJacob Keller /** 154906c16d89SJacob Keller * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta 155006c16d89SJacob Keller * @info: the driver's PTP info structure 155106c16d89SJacob Keller * @delta: Offset in nanoseconds to adjust the time by 155206c16d89SJacob Keller */ 155306c16d89SJacob Keller static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta) 155406c16d89SJacob Keller { 155506c16d89SJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 155606c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 155706c16d89SJacob Keller struct device *dev; 155806c16d89SJacob Keller int err; 155906c16d89SJacob Keller 156006c16d89SJacob Keller dev = ice_pf_to_dev(pf); 156106c16d89SJacob Keller 156206c16d89SJacob Keller /* Hardware only supports atomic adjustments using signed 32-bit 156306c16d89SJacob Keller * integers. For any adjustment outside this range, perform 156406c16d89SJacob Keller * a non-atomic get->adjust->set flow. 156506c16d89SJacob Keller */ 156606c16d89SJacob Keller if (delta > S32_MAX || delta < S32_MIN) { 156706c16d89SJacob Keller dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta); 156806c16d89SJacob Keller return ice_ptp_adjtime_nonatomic(info, delta); 156906c16d89SJacob Keller } 157006c16d89SJacob Keller 157106c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 157206c16d89SJacob Keller dev_err(dev, "PTP failed to acquire semaphore in adjtime\n"); 157306c16d89SJacob Keller return -EBUSY; 157406c16d89SJacob Keller } 157506c16d89SJacob Keller 15769ee31343SJacob Keller /* Disable periodic outputs */ 15779ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 15789ee31343SJacob Keller 157906c16d89SJacob Keller err = ice_ptp_write_adj(pf, delta); 158006c16d89SJacob Keller 15819ee31343SJacob Keller /* Reenable periodic outputs */ 15829ee31343SJacob Keller ice_ptp_enable_all_clkout(pf); 15839ee31343SJacob Keller 158406c16d89SJacob Keller ice_ptp_unlock(hw); 158506c16d89SJacob Keller 158606c16d89SJacob Keller if (err) { 158706c16d89SJacob Keller dev_err(dev, "PTP failed to adjust time, err %d\n", err); 158806c16d89SJacob Keller return err; 158906c16d89SJacob Keller } 159006c16d89SJacob Keller 159177a78115SJacob Keller ice_ptp_update_cached_phctime(pf); 159277a78115SJacob Keller 159306c16d89SJacob Keller return 0; 159406c16d89SJacob Keller } 159506c16d89SJacob Keller 159613a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS 159713a64f0bSJacob Keller /** 159813a64f0bSJacob Keller * ice_ptp_get_syncdevicetime - Get the cross time stamp info 159913a64f0bSJacob Keller * @device: Current device time 160013a64f0bSJacob Keller * @system: System counter value read synchronously with device time 160113a64f0bSJacob Keller * @ctx: Context provided by timekeeping code 160213a64f0bSJacob Keller * 160313a64f0bSJacob Keller * Read device and system (ART) clock simultaneously and return the corrected 160413a64f0bSJacob Keller * clock values in ns. 160513a64f0bSJacob Keller */ 160613a64f0bSJacob Keller static int 160713a64f0bSJacob Keller ice_ptp_get_syncdevicetime(ktime_t *device, 160813a64f0bSJacob Keller struct system_counterval_t *system, 160913a64f0bSJacob Keller void *ctx) 161013a64f0bSJacob Keller { 161113a64f0bSJacob Keller struct ice_pf *pf = (struct ice_pf *)ctx; 161213a64f0bSJacob Keller struct ice_hw *hw = &pf->hw; 161313a64f0bSJacob Keller u32 hh_lock, hh_art_ctl; 161413a64f0bSJacob Keller int i; 161513a64f0bSJacob Keller 161613a64f0bSJacob Keller /* Get the HW lock */ 161713a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id)); 161813a64f0bSJacob Keller if (hh_lock & PFHH_SEM_BUSY_M) { 161913a64f0bSJacob Keller dev_err(ice_pf_to_dev(pf), "PTP failed to get hh lock\n"); 162013a64f0bSJacob Keller return -EFAULT; 162113a64f0bSJacob Keller } 162213a64f0bSJacob Keller 162313a64f0bSJacob Keller /* Start the ART and device clock sync sequence */ 162413a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL); 162513a64f0bSJacob Keller hh_art_ctl = hh_art_ctl | GLHH_ART_CTL_ACTIVE_M; 162613a64f0bSJacob Keller wr32(hw, GLHH_ART_CTL, hh_art_ctl); 162713a64f0bSJacob Keller 162813a64f0bSJacob Keller #define MAX_HH_LOCK_TRIES 100 162913a64f0bSJacob Keller 163013a64f0bSJacob Keller for (i = 0; i < MAX_HH_LOCK_TRIES; i++) { 163113a64f0bSJacob Keller /* Wait for sync to complete */ 163213a64f0bSJacob Keller hh_art_ctl = rd32(hw, GLHH_ART_CTL); 163313a64f0bSJacob Keller if (hh_art_ctl & GLHH_ART_CTL_ACTIVE_M) { 163413a64f0bSJacob Keller udelay(1); 163513a64f0bSJacob Keller continue; 163613a64f0bSJacob Keller } else { 163713a64f0bSJacob Keller u32 hh_ts_lo, hh_ts_hi, tmr_idx; 163813a64f0bSJacob Keller u64 hh_ts; 163913a64f0bSJacob Keller 164013a64f0bSJacob Keller tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 164113a64f0bSJacob Keller /* Read ART time */ 164213a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLHH_ART_TIME_L); 164313a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLHH_ART_TIME_H); 164413a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo; 164513a64f0bSJacob Keller *system = convert_art_ns_to_tsc(hh_ts); 164613a64f0bSJacob Keller /* Read Device source clock time */ 164713a64f0bSJacob Keller hh_ts_lo = rd32(hw, GLTSYN_HHTIME_L(tmr_idx)); 164813a64f0bSJacob Keller hh_ts_hi = rd32(hw, GLTSYN_HHTIME_H(tmr_idx)); 164913a64f0bSJacob Keller hh_ts = ((u64)hh_ts_hi << 32) | hh_ts_lo; 165013a64f0bSJacob Keller *device = ns_to_ktime(hh_ts); 165113a64f0bSJacob Keller break; 165213a64f0bSJacob Keller } 165313a64f0bSJacob Keller } 165413a64f0bSJacob Keller /* Release HW lock */ 165513a64f0bSJacob Keller hh_lock = rd32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id)); 165613a64f0bSJacob Keller hh_lock = hh_lock & ~PFHH_SEM_BUSY_M; 165713a64f0bSJacob Keller wr32(hw, PFHH_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), hh_lock); 165813a64f0bSJacob Keller 165913a64f0bSJacob Keller if (i == MAX_HH_LOCK_TRIES) 166013a64f0bSJacob Keller return -ETIMEDOUT; 166113a64f0bSJacob Keller 166213a64f0bSJacob Keller return 0; 166313a64f0bSJacob Keller } 166413a64f0bSJacob Keller 166513a64f0bSJacob Keller /** 166613a64f0bSJacob Keller * ice_ptp_getcrosststamp_e822 - Capture a device cross timestamp 166713a64f0bSJacob Keller * @info: the driver's PTP info structure 166813a64f0bSJacob Keller * @cts: The memory to fill the cross timestamp info 166913a64f0bSJacob Keller * 167013a64f0bSJacob Keller * Capture a cross timestamp between the ART and the device PTP hardware 167113a64f0bSJacob Keller * clock. Fill the cross timestamp information and report it back to the 167213a64f0bSJacob Keller * caller. 167313a64f0bSJacob Keller * 167413a64f0bSJacob Keller * This is only valid for E822 devices which have support for generating the 167513a64f0bSJacob Keller * cross timestamp via PCIe PTM. 167613a64f0bSJacob Keller * 167713a64f0bSJacob Keller * In order to correctly correlate the ART timestamp back to the TSC time, the 167813a64f0bSJacob Keller * CPU must have X86_FEATURE_TSC_KNOWN_FREQ. 167913a64f0bSJacob Keller */ 168013a64f0bSJacob Keller static int 168113a64f0bSJacob Keller ice_ptp_getcrosststamp_e822(struct ptp_clock_info *info, 168213a64f0bSJacob Keller struct system_device_crosststamp *cts) 168313a64f0bSJacob Keller { 168413a64f0bSJacob Keller struct ice_pf *pf = ptp_info_to_pf(info); 168513a64f0bSJacob Keller 168613a64f0bSJacob Keller return get_device_system_crosststamp(ice_ptp_get_syncdevicetime, 168713a64f0bSJacob Keller pf, NULL, cts); 168813a64f0bSJacob Keller } 168913a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */ 169013a64f0bSJacob Keller 169106c16d89SJacob Keller /** 169277a78115SJacob Keller * ice_ptp_get_ts_config - ioctl interface to read the timestamping config 169377a78115SJacob Keller * @pf: Board private structure 169477a78115SJacob Keller * @ifr: ioctl data 169577a78115SJacob Keller * 169677a78115SJacob Keller * Copy the timestamping config to user buffer 169777a78115SJacob Keller */ 169877a78115SJacob Keller int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr) 169977a78115SJacob Keller { 170077a78115SJacob Keller struct hwtstamp_config *config; 170177a78115SJacob Keller 170277a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 170377a78115SJacob Keller return -EIO; 170477a78115SJacob Keller 170577a78115SJacob Keller config = &pf->ptp.tstamp_config; 170677a78115SJacob Keller 170777a78115SJacob Keller return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ? 170877a78115SJacob Keller -EFAULT : 0; 170977a78115SJacob Keller } 171077a78115SJacob Keller 171177a78115SJacob Keller /** 171277a78115SJacob Keller * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode 171377a78115SJacob Keller * @pf: Board private structure 171477a78115SJacob Keller * @config: hwtstamp settings requested or saved 171577a78115SJacob Keller */ 171677a78115SJacob Keller static int 171777a78115SJacob Keller ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config) 171877a78115SJacob Keller { 171977a78115SJacob Keller switch (config->tx_type) { 172077a78115SJacob Keller case HWTSTAMP_TX_OFF: 1721ea9b847cSJacob Keller ice_set_tx_tstamp(pf, false); 1722ea9b847cSJacob Keller break; 1723ea9b847cSJacob Keller case HWTSTAMP_TX_ON: 1724ea9b847cSJacob Keller ice_set_tx_tstamp(pf, true); 172577a78115SJacob Keller break; 172677a78115SJacob Keller default: 172777a78115SJacob Keller return -ERANGE; 172877a78115SJacob Keller } 172977a78115SJacob Keller 173077a78115SJacob Keller switch (config->rx_filter) { 173177a78115SJacob Keller case HWTSTAMP_FILTER_NONE: 173277a78115SJacob Keller ice_set_rx_tstamp(pf, false); 173377a78115SJacob Keller break; 173477a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 173577a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 173677a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 173777a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_EVENT: 173877a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 173977a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 174077a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_SYNC: 174177a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 174277a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 174377a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 174477a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 174577a78115SJacob Keller case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 174677a78115SJacob Keller case HWTSTAMP_FILTER_NTP_ALL: 174777a78115SJacob Keller case HWTSTAMP_FILTER_ALL: 174877a78115SJacob Keller ice_set_rx_tstamp(pf, true); 174977a78115SJacob Keller break; 175077a78115SJacob Keller default: 175177a78115SJacob Keller return -ERANGE; 175277a78115SJacob Keller } 175377a78115SJacob Keller 175477a78115SJacob Keller return 0; 175577a78115SJacob Keller } 175677a78115SJacob Keller 175777a78115SJacob Keller /** 175877a78115SJacob Keller * ice_ptp_set_ts_config - ioctl interface to control the timestamping 175977a78115SJacob Keller * @pf: Board private structure 176077a78115SJacob Keller * @ifr: ioctl data 176177a78115SJacob Keller * 176277a78115SJacob Keller * Get the user config and store it 176377a78115SJacob Keller */ 176477a78115SJacob Keller int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr) 176577a78115SJacob Keller { 176677a78115SJacob Keller struct hwtstamp_config config; 176777a78115SJacob Keller int err; 176877a78115SJacob Keller 176977a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 177077a78115SJacob Keller return -EAGAIN; 177177a78115SJacob Keller 177277a78115SJacob Keller if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 177377a78115SJacob Keller return -EFAULT; 177477a78115SJacob Keller 177577a78115SJacob Keller err = ice_ptp_set_timestamp_mode(pf, &config); 177677a78115SJacob Keller if (err) 177777a78115SJacob Keller return err; 177877a78115SJacob Keller 1779e59d75ddSJacob Keller /* Return the actual configuration set */ 1780e59d75ddSJacob Keller config = pf->ptp.tstamp_config; 178177a78115SJacob Keller 178277a78115SJacob Keller return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 178377a78115SJacob Keller -EFAULT : 0; 178477a78115SJacob Keller } 178577a78115SJacob Keller 178677a78115SJacob Keller /** 178777a78115SJacob Keller * ice_ptp_rx_hwtstamp - Check for an Rx timestamp 178877a78115SJacob Keller * @rx_ring: Ring to get the VSI info 178977a78115SJacob Keller * @rx_desc: Receive descriptor 179077a78115SJacob Keller * @skb: Particular skb to send timestamp with 179177a78115SJacob Keller * 179277a78115SJacob Keller * The driver receives a notification in the receive descriptor with timestamp. 179377a78115SJacob Keller * The timestamp is in ns, so we must convert the result first. 179477a78115SJacob Keller */ 179577a78115SJacob Keller void 1796e72bba21SMaciej Fijalkowski ice_ptp_rx_hwtstamp(struct ice_rx_ring *rx_ring, 179777a78115SJacob Keller union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) 179877a78115SJacob Keller { 179977a78115SJacob Keller u32 ts_high; 180077a78115SJacob Keller u64 ts_ns; 180177a78115SJacob Keller 180277a78115SJacob Keller /* Populate timesync data into skb */ 180377a78115SJacob Keller if (rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID) { 180477a78115SJacob Keller struct skb_shared_hwtstamps *hwtstamps; 180577a78115SJacob Keller 180677a78115SJacob Keller /* Use ice_ptp_extend_32b_ts directly, using the ring-specific 180777a78115SJacob Keller * cached PHC value, rather than accessing the PF. This also 180877a78115SJacob Keller * allows us to simply pass the upper 32bits of nanoseconds 180977a78115SJacob Keller * directly. Calling ice_ptp_extend_40b_ts is unnecessary as 181077a78115SJacob Keller * it would just discard these bits itself. 181177a78115SJacob Keller */ 181277a78115SJacob Keller ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high); 181377a78115SJacob Keller ts_ns = ice_ptp_extend_32b_ts(rx_ring->cached_phctime, ts_high); 181477a78115SJacob Keller 181577a78115SJacob Keller hwtstamps = skb_hwtstamps(skb); 181677a78115SJacob Keller memset(hwtstamps, 0, sizeof(*hwtstamps)); 181777a78115SJacob Keller hwtstamps->hwtstamp = ns_to_ktime(ts_ns); 181877a78115SJacob Keller } 181977a78115SJacob Keller } 182077a78115SJacob Keller 182177a78115SJacob Keller /** 1822325b2064SMaciej Machnikowski * ice_ptp_disable_sma_pins_e810t - Disable E810-T SMA pins 1823325b2064SMaciej Machnikowski * @pf: pointer to the PF structure 1824325b2064SMaciej Machnikowski * @info: PTP clock info structure 1825325b2064SMaciej Machnikowski * 1826325b2064SMaciej Machnikowski * Disable the OS access to the SMA pins. Called to clear out the OS 1827325b2064SMaciej Machnikowski * indications of pin support when we fail to setup the E810-T SMA control 1828325b2064SMaciej Machnikowski * register. 1829325b2064SMaciej Machnikowski */ 1830325b2064SMaciej Machnikowski static void 1831325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 1832325b2064SMaciej Machnikowski { 1833325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf); 1834325b2064SMaciej Machnikowski 1835325b2064SMaciej Machnikowski dev_warn(dev, "Failed to configure E810-T SMA pin control\n"); 1836325b2064SMaciej Machnikowski 1837325b2064SMaciej Machnikowski info->enable = NULL; 1838325b2064SMaciej Machnikowski info->verify = NULL; 1839325b2064SMaciej Machnikowski info->n_pins = 0; 1840325b2064SMaciej Machnikowski info->n_ext_ts = 0; 1841325b2064SMaciej Machnikowski info->n_per_out = 0; 1842325b2064SMaciej Machnikowski } 1843325b2064SMaciej Machnikowski 1844325b2064SMaciej Machnikowski /** 1845325b2064SMaciej Machnikowski * ice_ptp_setup_sma_pins_e810t - Setup the SMA pins 1846325b2064SMaciej Machnikowski * @pf: pointer to the PF structure 1847325b2064SMaciej Machnikowski * @info: PTP clock info structure 1848325b2064SMaciej Machnikowski * 1849325b2064SMaciej Machnikowski * Finish setting up the SMA pins by allocating pin_config, and setting it up 1850325b2064SMaciej Machnikowski * according to the current status of the SMA. On failure, disable all of the 1851325b2064SMaciej Machnikowski * extended SMA pin support. 1852325b2064SMaciej Machnikowski */ 1853325b2064SMaciej Machnikowski static void 1854325b2064SMaciej Machnikowski ice_ptp_setup_sma_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 1855325b2064SMaciej Machnikowski { 1856325b2064SMaciej Machnikowski struct device *dev = ice_pf_to_dev(pf); 1857325b2064SMaciej Machnikowski int err; 1858325b2064SMaciej Machnikowski 1859325b2064SMaciej Machnikowski /* Allocate memory for kernel pins interface */ 1860325b2064SMaciej Machnikowski info->pin_config = devm_kcalloc(dev, info->n_pins, 1861325b2064SMaciej Machnikowski sizeof(*info->pin_config), GFP_KERNEL); 1862325b2064SMaciej Machnikowski if (!info->pin_config) { 1863325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info); 1864325b2064SMaciej Machnikowski return; 1865325b2064SMaciej Machnikowski } 1866325b2064SMaciej Machnikowski 1867325b2064SMaciej Machnikowski /* Read current SMA status */ 1868325b2064SMaciej Machnikowski err = ice_get_sma_config_e810t(&pf->hw, info->pin_config); 1869325b2064SMaciej Machnikowski if (err) 1870325b2064SMaciej Machnikowski ice_ptp_disable_sma_pins_e810t(pf, info); 1871325b2064SMaciej Machnikowski } 1872325b2064SMaciej Machnikowski 1873325b2064SMaciej Machnikowski /** 1874325b2064SMaciej Machnikowski * ice_ptp_setup_pins_e810t - Setup PTP pins in sysfs 1875325b2064SMaciej Machnikowski * @pf: pointer to the PF instance 1876325b2064SMaciej Machnikowski * @info: PTP clock capabilities 1877325b2064SMaciej Machnikowski */ 1878325b2064SMaciej Machnikowski static void 1879325b2064SMaciej Machnikowski ice_ptp_setup_pins_e810t(struct ice_pf *pf, struct ptp_clock_info *info) 1880325b2064SMaciej Machnikowski { 1881325b2064SMaciej Machnikowski /* Check if SMA controller is in the netlist */ 1882325b2064SMaciej Machnikowski if (ice_is_feature_supported(pf, ICE_F_SMA_CTRL) && 1883325b2064SMaciej Machnikowski !ice_is_pca9575_present(&pf->hw)) 1884325b2064SMaciej Machnikowski ice_clear_feature_support(pf, ICE_F_SMA_CTRL); 1885325b2064SMaciej Machnikowski 1886325b2064SMaciej Machnikowski if (!ice_is_feature_supported(pf, ICE_F_SMA_CTRL)) { 1887325b2064SMaciej Machnikowski info->n_ext_ts = N_EXT_TS_E810_NO_SMA; 1888325b2064SMaciej Machnikowski info->n_per_out = N_PER_OUT_E810T_NO_SMA; 1889325b2064SMaciej Machnikowski return; 1890325b2064SMaciej Machnikowski } 1891325b2064SMaciej Machnikowski 1892325b2064SMaciej Machnikowski info->n_per_out = N_PER_OUT_E810T; 1893896a55aaSAnirudh Venkataramanan 1894896a55aaSAnirudh Venkataramanan if (ice_is_feature_supported(pf, ICE_F_PTP_EXTTS)) { 1895325b2064SMaciej Machnikowski info->n_ext_ts = N_EXT_TS_E810; 1896325b2064SMaciej Machnikowski info->n_pins = NUM_PTP_PINS_E810T; 1897325b2064SMaciej Machnikowski info->verify = ice_verify_pin_e810t; 1898896a55aaSAnirudh Venkataramanan } 1899325b2064SMaciej Machnikowski 1900325b2064SMaciej Machnikowski /* Complete setup of the SMA pins */ 1901325b2064SMaciej Machnikowski ice_ptp_setup_sma_pins_e810t(pf, info); 1902325b2064SMaciej Machnikowski } 1903325b2064SMaciej Machnikowski 1904325b2064SMaciej Machnikowski /** 1905172db5f9SMaciej Machnikowski * ice_ptp_setup_pins_e810 - Setup PTP pins in sysfs 1906896a55aaSAnirudh Venkataramanan * @pf: pointer to the PF instance 1907172db5f9SMaciej Machnikowski * @info: PTP clock capabilities 1908172db5f9SMaciej Machnikowski */ 1909896a55aaSAnirudh Venkataramanan static void ice_ptp_setup_pins_e810(struct ice_pf *pf, struct ptp_clock_info *info) 1910172db5f9SMaciej Machnikowski { 1911325b2064SMaciej Machnikowski info->n_per_out = N_PER_OUT_E810; 1912896a55aaSAnirudh Venkataramanan 1913896a55aaSAnirudh Venkataramanan if (!ice_is_feature_supported(pf, ICE_F_PTP_EXTTS)) 1914896a55aaSAnirudh Venkataramanan return; 1915896a55aaSAnirudh Venkataramanan 1916325b2064SMaciej Machnikowski info->n_ext_ts = N_EXT_TS_E810; 1917172db5f9SMaciej Machnikowski } 1918172db5f9SMaciej Machnikowski 1919172db5f9SMaciej Machnikowski /** 192013a64f0bSJacob Keller * ice_ptp_set_funcs_e822 - Set specialized functions for E822 support 192113a64f0bSJacob Keller * @pf: Board private structure 192213a64f0bSJacob Keller * @info: PTP info to fill 192313a64f0bSJacob Keller * 192413a64f0bSJacob Keller * Assign functions to the PTP capabiltiies structure for E822 devices. 192513a64f0bSJacob Keller * Functions which operate across all device families should be set directly 192613a64f0bSJacob Keller * in ice_ptp_set_caps. Only add functions here which are distinct for E822 192713a64f0bSJacob Keller * devices. 192813a64f0bSJacob Keller */ 192913a64f0bSJacob Keller static void 193013a64f0bSJacob Keller ice_ptp_set_funcs_e822(struct ice_pf *pf, struct ptp_clock_info *info) 193113a64f0bSJacob Keller { 193213a64f0bSJacob Keller #ifdef CONFIG_ICE_HWTS 193313a64f0bSJacob Keller if (boot_cpu_has(X86_FEATURE_ART) && 193413a64f0bSJacob Keller boot_cpu_has(X86_FEATURE_TSC_KNOWN_FREQ)) 193513a64f0bSJacob Keller info->getcrosststamp = ice_ptp_getcrosststamp_e822; 193613a64f0bSJacob Keller #endif /* CONFIG_ICE_HWTS */ 193713a64f0bSJacob Keller } 193813a64f0bSJacob Keller 193913a64f0bSJacob Keller /** 1940172db5f9SMaciej Machnikowski * ice_ptp_set_funcs_e810 - Set specialized functions for E810 support 1941172db5f9SMaciej Machnikowski * @pf: Board private structure 1942172db5f9SMaciej Machnikowski * @info: PTP info to fill 1943172db5f9SMaciej Machnikowski * 1944172db5f9SMaciej Machnikowski * Assign functions to the PTP capabiltiies structure for E810 devices. 1945172db5f9SMaciej Machnikowski * Functions which operate across all device families should be set directly 1946172db5f9SMaciej Machnikowski * in ice_ptp_set_caps. Only add functions here which are distinct for e810 1947172db5f9SMaciej Machnikowski * devices. 1948172db5f9SMaciej Machnikowski */ 1949172db5f9SMaciej Machnikowski static void 1950172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(struct ice_pf *pf, struct ptp_clock_info *info) 1951172db5f9SMaciej Machnikowski { 1952172db5f9SMaciej Machnikowski info->enable = ice_ptp_gpio_enable_e810; 1953172db5f9SMaciej Machnikowski 1954325b2064SMaciej Machnikowski if (ice_is_e810t(&pf->hw)) 1955325b2064SMaciej Machnikowski ice_ptp_setup_pins_e810t(pf, info); 1956325b2064SMaciej Machnikowski else 1957896a55aaSAnirudh Venkataramanan ice_ptp_setup_pins_e810(pf, info); 1958172db5f9SMaciej Machnikowski } 1959172db5f9SMaciej Machnikowski 1960172db5f9SMaciej Machnikowski /** 196106c16d89SJacob Keller * ice_ptp_set_caps - Set PTP capabilities 196206c16d89SJacob Keller * @pf: Board private structure 196306c16d89SJacob Keller */ 196406c16d89SJacob Keller static void ice_ptp_set_caps(struct ice_pf *pf) 196506c16d89SJacob Keller { 196606c16d89SJacob Keller struct ptp_clock_info *info = &pf->ptp.info; 196706c16d89SJacob Keller struct device *dev = ice_pf_to_dev(pf); 196806c16d89SJacob Keller 196906c16d89SJacob Keller snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk", 197006c16d89SJacob Keller dev_driver_string(dev), dev_name(dev)); 197106c16d89SJacob Keller info->owner = THIS_MODULE; 197206c16d89SJacob Keller info->max_adj = 999999999; 197306c16d89SJacob Keller info->adjtime = ice_ptp_adjtime; 197406c16d89SJacob Keller info->adjfine = ice_ptp_adjfine; 197506c16d89SJacob Keller info->gettimex64 = ice_ptp_gettimex64; 197606c16d89SJacob Keller info->settime64 = ice_ptp_settime64; 1977172db5f9SMaciej Machnikowski 19783a749623SJacob Keller if (ice_is_e810(&pf->hw)) 1979172db5f9SMaciej Machnikowski ice_ptp_set_funcs_e810(pf, info); 198013a64f0bSJacob Keller else 198113a64f0bSJacob Keller ice_ptp_set_funcs_e822(pf, info); 198206c16d89SJacob Keller } 198306c16d89SJacob Keller 198406c16d89SJacob Keller /** 198506c16d89SJacob Keller * ice_ptp_create_clock - Create PTP clock device for userspace 198606c16d89SJacob Keller * @pf: Board private structure 198706c16d89SJacob Keller * 198806c16d89SJacob Keller * This function creates a new PTP clock device. It only creates one if we 198906c16d89SJacob Keller * don't already have one. Will return error if it can't create one, but success 199006c16d89SJacob Keller * if we already have a device. Should be used by ice_ptp_init to create clock 199106c16d89SJacob Keller * initially, and prevent global resets from creating new clock devices. 199206c16d89SJacob Keller */ 199306c16d89SJacob Keller static long ice_ptp_create_clock(struct ice_pf *pf) 199406c16d89SJacob Keller { 199506c16d89SJacob Keller struct ptp_clock_info *info; 199606c16d89SJacob Keller struct ptp_clock *clock; 199706c16d89SJacob Keller struct device *dev; 199806c16d89SJacob Keller 199906c16d89SJacob Keller /* No need to create a clock device if we already have one */ 200006c16d89SJacob Keller if (pf->ptp.clock) 200106c16d89SJacob Keller return 0; 200206c16d89SJacob Keller 200306c16d89SJacob Keller ice_ptp_set_caps(pf); 200406c16d89SJacob Keller 200506c16d89SJacob Keller info = &pf->ptp.info; 200606c16d89SJacob Keller dev = ice_pf_to_dev(pf); 200706c16d89SJacob Keller 200806c16d89SJacob Keller /* Attempt to register the clock before enabling the hardware. */ 200906c16d89SJacob Keller clock = ptp_clock_register(info, dev); 201006c16d89SJacob Keller if (IS_ERR(clock)) 201106c16d89SJacob Keller return PTR_ERR(clock); 201206c16d89SJacob Keller 201306c16d89SJacob Keller pf->ptp.clock = clock; 201406c16d89SJacob Keller 201506c16d89SJacob Keller return 0; 201606c16d89SJacob Keller } 201706c16d89SJacob Keller 2018ea9b847cSJacob Keller /** 2019ea9b847cSJacob Keller * ice_ptp_tx_tstamp_work - Process Tx timestamps for a port 2020ea9b847cSJacob Keller * @work: pointer to the kthread_work struct 2021ea9b847cSJacob Keller * 2022ea9b847cSJacob Keller * Process timestamps captured by the PHY associated with this port. To do 2023ea9b847cSJacob Keller * this, loop over each index with a waiting skb. 2024ea9b847cSJacob Keller * 2025ea9b847cSJacob Keller * If a given index has a valid timestamp, perform the following steps: 2026ea9b847cSJacob Keller * 2027ea9b847cSJacob Keller * 1) copy the timestamp out of the PHY register 2028ea9b847cSJacob Keller * 4) clear the timestamp valid bit in the PHY register 2029ea9b847cSJacob Keller * 5) unlock the index by clearing the associated in_use bit. 2030ea9b847cSJacob Keller * 2) extend the 40b timestamp value to get a 64bit timestamp 2031ea9b847cSJacob Keller * 3) send that timestamp to the stack 2032ea9b847cSJacob Keller * 2033ea9b847cSJacob Keller * After looping, if we still have waiting SKBs, then re-queue the work. This 2034ea9b847cSJacob Keller * may cause us effectively poll even when not strictly necessary. We do this 2035ea9b847cSJacob Keller * because it's possible a new timestamp was requested around the same time as 2036ea9b847cSJacob Keller * the interrupt. In some cases hardware might not interrupt us again when the 2037ea9b847cSJacob Keller * timestamp is captured. 2038ea9b847cSJacob Keller * 2039ea9b847cSJacob Keller * Note that we only take the tracking lock when clearing the bit and when 2040ea9b847cSJacob Keller * checking if we need to re-queue this task. The only place where bits can be 2041ea9b847cSJacob Keller * set is the hard xmit routine where an SKB has a request flag set. The only 2042ea9b847cSJacob Keller * places where we clear bits are this work function, or the periodic cleanup 2043ea9b847cSJacob Keller * thread. If the cleanup thread clears a bit we're processing we catch it 2044ea9b847cSJacob Keller * when we lock to clear the bit and then grab the SKB pointer. If a Tx thread 2045ea9b847cSJacob Keller * starts a new timestamp, we might not begin processing it right away but we 2046ea9b847cSJacob Keller * will notice it at the end when we re-queue the work item. If a Tx thread 2047ea9b847cSJacob Keller * starts a new timestamp just after this function exits without re-queuing, 2048ea9b847cSJacob Keller * the interrupt when the timestamp finishes should trigger. Avoiding holding 2049ea9b847cSJacob Keller * the lock for the entire function is important in order to ensure that Tx 2050ea9b847cSJacob Keller * threads do not get blocked while waiting for the lock. 2051ea9b847cSJacob Keller */ 2052ea9b847cSJacob Keller static void ice_ptp_tx_tstamp_work(struct kthread_work *work) 2053ea9b847cSJacob Keller { 2054ea9b847cSJacob Keller struct ice_ptp_port *ptp_port; 2055ea9b847cSJacob Keller struct ice_ptp_tx *tx; 2056ea9b847cSJacob Keller struct ice_pf *pf; 2057ea9b847cSJacob Keller struct ice_hw *hw; 2058ea9b847cSJacob Keller u8 idx; 2059ea9b847cSJacob Keller 2060ea9b847cSJacob Keller tx = container_of(work, struct ice_ptp_tx, work); 2061ea9b847cSJacob Keller if (!tx->init) 2062ea9b847cSJacob Keller return; 2063ea9b847cSJacob Keller 2064ea9b847cSJacob Keller ptp_port = container_of(tx, struct ice_ptp_port, tx); 2065ea9b847cSJacob Keller pf = ptp_port_to_pf(ptp_port); 2066ea9b847cSJacob Keller hw = &pf->hw; 2067ea9b847cSJacob Keller 2068ea9b847cSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 2069ea9b847cSJacob Keller struct skb_shared_hwtstamps shhwtstamps = {}; 2070ea9b847cSJacob Keller u8 phy_idx = idx + tx->quad_offset; 2071ea9b847cSJacob Keller u64 raw_tstamp, tstamp; 2072ea9b847cSJacob Keller struct sk_buff *skb; 2073ea9b847cSJacob Keller int err; 2074ea9b847cSJacob Keller 20754c120218SJacob Keller ice_trace(tx_tstamp_fw_req, tx->tstamps[idx].skb, idx); 20764c120218SJacob Keller 2077ea9b847cSJacob Keller err = ice_read_phy_tstamp(hw, tx->quad, phy_idx, 2078ea9b847cSJacob Keller &raw_tstamp); 2079ea9b847cSJacob Keller if (err) 2080ea9b847cSJacob Keller continue; 2081ea9b847cSJacob Keller 20824c120218SJacob Keller ice_trace(tx_tstamp_fw_done, tx->tstamps[idx].skb, idx); 20834c120218SJacob Keller 208437e738b6SKarol Kolacinski /* Check if the timestamp is invalid or stale */ 208537e738b6SKarol Kolacinski if (!(raw_tstamp & ICE_PTP_TS_VALID) || 208637e738b6SKarol Kolacinski raw_tstamp == tx->tstamps[idx].cached_tstamp) 2087ea9b847cSJacob Keller continue; 2088ea9b847cSJacob Keller 2089ea9b847cSJacob Keller /* The timestamp is valid, so we'll go ahead and clear this 2090ea9b847cSJacob Keller * index and then send the timestamp up to the stack. 2091ea9b847cSJacob Keller */ 2092ea9b847cSJacob Keller spin_lock(&tx->lock); 209337e738b6SKarol Kolacinski tx->tstamps[idx].cached_tstamp = raw_tstamp; 2094ea9b847cSJacob Keller clear_bit(idx, tx->in_use); 2095ea9b847cSJacob Keller skb = tx->tstamps[idx].skb; 2096ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 2097ea9b847cSJacob Keller spin_unlock(&tx->lock); 2098ea9b847cSJacob Keller 2099ea9b847cSJacob Keller /* it's (unlikely but) possible we raced with the cleanup 2100ea9b847cSJacob Keller * thread for discarding old timestamp requests. 2101ea9b847cSJacob Keller */ 2102ea9b847cSJacob Keller if (!skb) 2103ea9b847cSJacob Keller continue; 2104ea9b847cSJacob Keller 2105ea9b847cSJacob Keller /* Extend the timestamp using cached PHC time */ 2106ea9b847cSJacob Keller tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp); 2107ea9b847cSJacob Keller shhwtstamps.hwtstamp = ns_to_ktime(tstamp); 2108ea9b847cSJacob Keller 21094c120218SJacob Keller ice_trace(tx_tstamp_complete, skb, idx); 21104c120218SJacob Keller 2111ea9b847cSJacob Keller skb_tstamp_tx(skb, &shhwtstamps); 2112ea9b847cSJacob Keller dev_kfree_skb_any(skb); 2113ea9b847cSJacob Keller } 2114ea9b847cSJacob Keller 2115ea9b847cSJacob Keller /* Check if we still have work to do. If so, re-queue this task to 2116ea9b847cSJacob Keller * poll for remaining timestamps. 2117ea9b847cSJacob Keller */ 2118ea9b847cSJacob Keller spin_lock(&tx->lock); 2119ea9b847cSJacob Keller if (!bitmap_empty(tx->in_use, tx->len)) 2120ea9b847cSJacob Keller kthread_queue_work(pf->ptp.kworker, &tx->work); 2121ea9b847cSJacob Keller spin_unlock(&tx->lock); 2122ea9b847cSJacob Keller } 2123ea9b847cSJacob Keller 2124ea9b847cSJacob Keller /** 2125ea9b847cSJacob Keller * ice_ptp_request_ts - Request an available Tx timestamp index 2126ea9b847cSJacob Keller * @tx: the PTP Tx timestamp tracker to request from 2127ea9b847cSJacob Keller * @skb: the SKB to associate with this timestamp request 2128ea9b847cSJacob Keller */ 2129ea9b847cSJacob Keller s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb) 2130ea9b847cSJacob Keller { 2131ea9b847cSJacob Keller u8 idx; 2132ea9b847cSJacob Keller 2133ea9b847cSJacob Keller /* Check if this tracker is initialized */ 21343a749623SJacob Keller if (!tx->init || tx->calibrating) 2135ea9b847cSJacob Keller return -1; 2136ea9b847cSJacob Keller 2137ea9b847cSJacob Keller spin_lock(&tx->lock); 2138ea9b847cSJacob Keller /* Find and set the first available index */ 2139ea9b847cSJacob Keller idx = find_first_zero_bit(tx->in_use, tx->len); 2140ea9b847cSJacob Keller if (idx < tx->len) { 2141ea9b847cSJacob Keller /* We got a valid index that no other thread could have set. Store 2142ea9b847cSJacob Keller * a reference to the skb and the start time to allow discarding old 2143ea9b847cSJacob Keller * requests. 2144ea9b847cSJacob Keller */ 2145ea9b847cSJacob Keller set_bit(idx, tx->in_use); 2146ea9b847cSJacob Keller tx->tstamps[idx].start = jiffies; 2147ea9b847cSJacob Keller tx->tstamps[idx].skb = skb_get(skb); 2148ea9b847cSJacob Keller skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 21494c120218SJacob Keller ice_trace(tx_tstamp_request, skb, idx); 2150ea9b847cSJacob Keller } 2151ea9b847cSJacob Keller 2152ea9b847cSJacob Keller spin_unlock(&tx->lock); 2153ea9b847cSJacob Keller 2154ea9b847cSJacob Keller /* return the appropriate PHY timestamp register index, -1 if no 2155ea9b847cSJacob Keller * indexes were available. 2156ea9b847cSJacob Keller */ 2157ea9b847cSJacob Keller if (idx >= tx->len) 2158ea9b847cSJacob Keller return -1; 2159ea9b847cSJacob Keller else 2160ea9b847cSJacob Keller return idx + tx->quad_offset; 2161ea9b847cSJacob Keller } 2162ea9b847cSJacob Keller 2163ea9b847cSJacob Keller /** 2164ea9b847cSJacob Keller * ice_ptp_process_ts - Spawn kthread work to handle timestamps 2165ea9b847cSJacob Keller * @pf: Board private structure 2166ea9b847cSJacob Keller * 2167ea9b847cSJacob Keller * Queue work required to process the PTP Tx timestamps outside of interrupt 2168ea9b847cSJacob Keller * context. 2169ea9b847cSJacob Keller */ 2170ea9b847cSJacob Keller void ice_ptp_process_ts(struct ice_pf *pf) 2171ea9b847cSJacob Keller { 2172ea9b847cSJacob Keller if (pf->ptp.port.tx.init) 2173ea9b847cSJacob Keller kthread_queue_work(pf->ptp.kworker, &pf->ptp.port.tx.work); 2174ea9b847cSJacob Keller } 2175ea9b847cSJacob Keller 2176ea9b847cSJacob Keller /** 2177ea9b847cSJacob Keller * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps 2178ea9b847cSJacob Keller * @tx: Tx tracking structure to initialize 2179ea9b847cSJacob Keller * 2180ea9b847cSJacob Keller * Assumes that the length has already been initialized. Do not call directly, 2181ea9b847cSJacob Keller * use the ice_ptp_init_tx_e822 or ice_ptp_init_tx_e810 instead. 2182ea9b847cSJacob Keller */ 2183ea9b847cSJacob Keller static int 2184ea9b847cSJacob Keller ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx) 2185ea9b847cSJacob Keller { 2186ea9b847cSJacob Keller tx->tstamps = kcalloc(tx->len, sizeof(*tx->tstamps), GFP_KERNEL); 2187ea9b847cSJacob Keller if (!tx->tstamps) 2188ea9b847cSJacob Keller return -ENOMEM; 2189ea9b847cSJacob Keller 2190ea9b847cSJacob Keller tx->in_use = bitmap_zalloc(tx->len, GFP_KERNEL); 2191ea9b847cSJacob Keller if (!tx->in_use) { 2192ea9b847cSJacob Keller kfree(tx->tstamps); 2193ea9b847cSJacob Keller tx->tstamps = NULL; 2194ea9b847cSJacob Keller return -ENOMEM; 2195ea9b847cSJacob Keller } 2196ea9b847cSJacob Keller 2197ea9b847cSJacob Keller spin_lock_init(&tx->lock); 2198ea9b847cSJacob Keller kthread_init_work(&tx->work, ice_ptp_tx_tstamp_work); 2199ea9b847cSJacob Keller 2200ea9b847cSJacob Keller tx->init = 1; 2201ea9b847cSJacob Keller 2202ea9b847cSJacob Keller return 0; 2203ea9b847cSJacob Keller } 2204ea9b847cSJacob Keller 2205ea9b847cSJacob Keller /** 2206ea9b847cSJacob Keller * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker 2207ea9b847cSJacob Keller * @pf: Board private structure 2208ea9b847cSJacob Keller * @tx: the tracker to flush 2209ea9b847cSJacob Keller */ 2210ea9b847cSJacob Keller static void 2211ea9b847cSJacob Keller ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 2212ea9b847cSJacob Keller { 2213ea9b847cSJacob Keller u8 idx; 2214ea9b847cSJacob Keller 2215ea9b847cSJacob Keller for (idx = 0; idx < tx->len; idx++) { 2216ea9b847cSJacob Keller u8 phy_idx = idx + tx->quad_offset; 2217ea9b847cSJacob Keller 22184d4a223aSJacob Keller spin_lock(&tx->lock); 2219ea9b847cSJacob Keller if (tx->tstamps[idx].skb) { 2220ea9b847cSJacob Keller dev_kfree_skb_any(tx->tstamps[idx].skb); 2221ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 2222ea9b847cSJacob Keller } 22234d4a223aSJacob Keller clear_bit(idx, tx->in_use); 22244dd0d5c3SJacob Keller spin_unlock(&tx->lock); 22254d4a223aSJacob Keller 22264d4a223aSJacob Keller /* Clear any potential residual timestamp in the PHY block */ 22274d4a223aSJacob Keller if (!pf->hw.reset_ongoing) 22284d4a223aSJacob Keller ice_clear_phy_tstamp(&pf->hw, tx->quad, phy_idx); 22294d4a223aSJacob Keller } 2230ea9b847cSJacob Keller } 2231ea9b847cSJacob Keller 2232ea9b847cSJacob Keller /** 2233ea9b847cSJacob Keller * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker 2234ea9b847cSJacob Keller * @pf: Board private structure 2235ea9b847cSJacob Keller * @tx: Tx tracking structure to release 2236ea9b847cSJacob Keller * 2237ea9b847cSJacob Keller * Free memory associated with the Tx timestamp tracker. 2238ea9b847cSJacob Keller */ 2239ea9b847cSJacob Keller static void 2240ea9b847cSJacob Keller ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 2241ea9b847cSJacob Keller { 2242ea9b847cSJacob Keller tx->init = 0; 2243ea9b847cSJacob Keller 2244ea9b847cSJacob Keller kthread_cancel_work_sync(&tx->work); 2245ea9b847cSJacob Keller 2246ea9b847cSJacob Keller ice_ptp_flush_tx_tracker(pf, tx); 2247ea9b847cSJacob Keller 2248ea9b847cSJacob Keller kfree(tx->tstamps); 2249ea9b847cSJacob Keller tx->tstamps = NULL; 2250ea9b847cSJacob Keller 22510dbc4162SChristophe JAILLET bitmap_free(tx->in_use); 2252ea9b847cSJacob Keller tx->in_use = NULL; 2253ea9b847cSJacob Keller 2254ea9b847cSJacob Keller tx->len = 0; 2255ea9b847cSJacob Keller } 2256ea9b847cSJacob Keller 2257ea9b847cSJacob Keller /** 22583a749623SJacob Keller * ice_ptp_init_tx_e822 - Initialize tracking for Tx timestamps 22593a749623SJacob Keller * @pf: Board private structure 22603a749623SJacob Keller * @tx: the Tx tracking structure to initialize 22613a749623SJacob Keller * @port: the port this structure tracks 22623a749623SJacob Keller * 22633a749623SJacob Keller * Initialize the Tx timestamp tracker for this port. For generic MAC devices, 22643a749623SJacob Keller * the timestamp block is shared for all ports in the same quad. To avoid 22653a749623SJacob Keller * ports using the same timestamp index, logically break the block of 22663a749623SJacob Keller * registers into chunks based on the port number. 22673a749623SJacob Keller */ 22683a749623SJacob Keller static int 22693a749623SJacob Keller ice_ptp_init_tx_e822(struct ice_pf *pf, struct ice_ptp_tx *tx, u8 port) 22703a749623SJacob Keller { 22713a749623SJacob Keller tx->quad = port / ICE_PORTS_PER_QUAD; 227271a579f0SMichal Michalik tx->quad_offset = (port % ICE_PORTS_PER_QUAD) * INDEX_PER_PORT; 22733a749623SJacob Keller tx->len = INDEX_PER_PORT; 22743a749623SJacob Keller 22753a749623SJacob Keller return ice_ptp_alloc_tx_tracker(tx); 22763a749623SJacob Keller } 22773a749623SJacob Keller 22783a749623SJacob Keller /** 2279ea9b847cSJacob Keller * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps 2280ea9b847cSJacob Keller * @pf: Board private structure 2281ea9b847cSJacob Keller * @tx: the Tx tracking structure to initialize 2282ea9b847cSJacob Keller * 2283ea9b847cSJacob Keller * Initialize the Tx timestamp tracker for this PF. For E810 devices, each 2284ea9b847cSJacob Keller * port has its own block of timestamps, independent of the other ports. 2285ea9b847cSJacob Keller */ 2286ea9b847cSJacob Keller static int 2287ea9b847cSJacob Keller ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx) 2288ea9b847cSJacob Keller { 2289ea9b847cSJacob Keller tx->quad = pf->hw.port_info->lport; 2290ea9b847cSJacob Keller tx->quad_offset = 0; 2291ea9b847cSJacob Keller tx->len = INDEX_PER_QUAD; 2292ea9b847cSJacob Keller 2293ea9b847cSJacob Keller return ice_ptp_alloc_tx_tracker(tx); 2294ea9b847cSJacob Keller } 2295ea9b847cSJacob Keller 2296ea9b847cSJacob Keller /** 2297ea9b847cSJacob Keller * ice_ptp_tx_tstamp_cleanup - Cleanup old timestamp requests that got dropped 2298a11b6c1aSMichal Michalik * @hw: pointer to the hw struct 2299ea9b847cSJacob Keller * @tx: PTP Tx tracker to clean up 2300ea9b847cSJacob Keller * 2301ea9b847cSJacob Keller * Loop through the Tx timestamp requests and see if any of them have been 2302ea9b847cSJacob Keller * waiting for a long time. Discard any SKBs that have been waiting for more 2303ea9b847cSJacob Keller * than 2 seconds. This is long enough to be reasonably sure that the 2304ea9b847cSJacob Keller * timestamp will never be captured. This might happen if the packet gets 2305ea9b847cSJacob Keller * discarded before it reaches the PHY timestamping block. 2306ea9b847cSJacob Keller */ 2307a11b6c1aSMichal Michalik static void ice_ptp_tx_tstamp_cleanup(struct ice_hw *hw, struct ice_ptp_tx *tx) 2308ea9b847cSJacob Keller { 2309ea9b847cSJacob Keller u8 idx; 2310ea9b847cSJacob Keller 2311ea9b847cSJacob Keller if (!tx->init) 2312ea9b847cSJacob Keller return; 2313ea9b847cSJacob Keller 2314ea9b847cSJacob Keller for_each_set_bit(idx, tx->in_use, tx->len) { 2315ea9b847cSJacob Keller struct sk_buff *skb; 2316a11b6c1aSMichal Michalik u64 raw_tstamp; 2317ea9b847cSJacob Keller 2318ea9b847cSJacob Keller /* Check if this SKB has been waiting for too long */ 2319ea9b847cSJacob Keller if (time_is_after_jiffies(tx->tstamps[idx].start + 2 * HZ)) 2320ea9b847cSJacob Keller continue; 2321ea9b847cSJacob Keller 2322a11b6c1aSMichal Michalik /* Read tstamp to be able to use this register again */ 2323a11b6c1aSMichal Michalik ice_read_phy_tstamp(hw, tx->quad, idx + tx->quad_offset, 2324a11b6c1aSMichal Michalik &raw_tstamp); 2325a11b6c1aSMichal Michalik 2326ea9b847cSJacob Keller spin_lock(&tx->lock); 2327ea9b847cSJacob Keller skb = tx->tstamps[idx].skb; 2328ea9b847cSJacob Keller tx->tstamps[idx].skb = NULL; 2329ea9b847cSJacob Keller clear_bit(idx, tx->in_use); 2330ea9b847cSJacob Keller spin_unlock(&tx->lock); 2331ea9b847cSJacob Keller 2332ea9b847cSJacob Keller /* Free the SKB after we've cleared the bit */ 2333ea9b847cSJacob Keller dev_kfree_skb_any(skb); 2334ea9b847cSJacob Keller } 2335ea9b847cSJacob Keller } 2336ea9b847cSJacob Keller 233777a78115SJacob Keller static void ice_ptp_periodic_work(struct kthread_work *work) 233877a78115SJacob Keller { 233977a78115SJacob Keller struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work); 234077a78115SJacob Keller struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 23414503cc7fSArkadiusz Kubalewski int err; 234277a78115SJacob Keller 234377a78115SJacob Keller if (!test_bit(ICE_FLAG_PTP, pf->flags)) 234477a78115SJacob Keller return; 234577a78115SJacob Keller 23464503cc7fSArkadiusz Kubalewski err = ice_ptp_update_cached_phctime(pf); 234777a78115SJacob Keller 2348a11b6c1aSMichal Michalik ice_ptp_tx_tstamp_cleanup(&pf->hw, &pf->ptp.port.tx); 2349ea9b847cSJacob Keller 23504503cc7fSArkadiusz Kubalewski /* Run twice a second or reschedule if phc update failed */ 235177a78115SJacob Keller kthread_queue_delayed_work(ptp->kworker, &ptp->work, 23524503cc7fSArkadiusz Kubalewski msecs_to_jiffies(err ? 10 : 500)); 235377a78115SJacob Keller } 235477a78115SJacob Keller 235506c16d89SJacob Keller /** 235648096710SKarol Kolacinski * ice_ptp_reset - Initialize PTP hardware clock support after reset 235748096710SKarol Kolacinski * @pf: Board private structure 235848096710SKarol Kolacinski */ 235948096710SKarol Kolacinski void ice_ptp_reset(struct ice_pf *pf) 236048096710SKarol Kolacinski { 236148096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 236248096710SKarol Kolacinski struct ice_hw *hw = &pf->hw; 236348096710SKarol Kolacinski struct timespec64 ts; 23643a749623SJacob Keller int err, itr = 1; 236548096710SKarol Kolacinski u64 time_diff; 236648096710SKarol Kolacinski 236748096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state)) 236848096710SKarol Kolacinski goto pfr; 236948096710SKarol Kolacinski 2370b2ee7256SJacob Keller if (!hw->func_caps.ts_func_info.src_tmr_owned) 23713a749623SJacob Keller goto reset_ts; 237248096710SKarol Kolacinski 2373b2ee7256SJacob Keller err = ice_ptp_init_phc(hw); 237448096710SKarol Kolacinski if (err) 237548096710SKarol Kolacinski goto err; 237648096710SKarol Kolacinski 237748096710SKarol Kolacinski /* Acquire the global hardware lock */ 237848096710SKarol Kolacinski if (!ice_ptp_lock(hw)) { 237948096710SKarol Kolacinski err = -EBUSY; 238048096710SKarol Kolacinski goto err; 238148096710SKarol Kolacinski } 238248096710SKarol Kolacinski 238348096710SKarol Kolacinski /* Write the increment time value to PHY and LAN */ 238478267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf)); 238548096710SKarol Kolacinski if (err) { 238648096710SKarol Kolacinski ice_ptp_unlock(hw); 238748096710SKarol Kolacinski goto err; 238848096710SKarol Kolacinski } 238948096710SKarol Kolacinski 239048096710SKarol Kolacinski /* Write the initial Time value to PHY and LAN using the cached PHC 239148096710SKarol Kolacinski * time before the reset and time difference between stopping and 239248096710SKarol Kolacinski * starting the clock. 239348096710SKarol Kolacinski */ 239448096710SKarol Kolacinski if (ptp->cached_phc_time) { 239548096710SKarol Kolacinski time_diff = ktime_get_real_ns() - ptp->reset_time; 239648096710SKarol Kolacinski ts = ns_to_timespec64(ptp->cached_phc_time + time_diff); 239748096710SKarol Kolacinski } else { 239848096710SKarol Kolacinski ts = ktime_to_timespec64(ktime_get_real()); 239948096710SKarol Kolacinski } 240048096710SKarol Kolacinski err = ice_ptp_write_init(pf, &ts); 240148096710SKarol Kolacinski if (err) { 240248096710SKarol Kolacinski ice_ptp_unlock(hw); 240348096710SKarol Kolacinski goto err; 240448096710SKarol Kolacinski } 240548096710SKarol Kolacinski 240648096710SKarol Kolacinski /* Release the global hardware lock */ 240748096710SKarol Kolacinski ice_ptp_unlock(hw); 240848096710SKarol Kolacinski 24093a749623SJacob Keller if (!ice_is_e810(hw)) { 24103a749623SJacob Keller /* Enable quad interrupts */ 24113a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr); 24123a749623SJacob Keller if (err) 24133a749623SJacob Keller goto err; 24143a749623SJacob Keller } 24153a749623SJacob Keller 24163a749623SJacob Keller reset_ts: 24173a749623SJacob Keller /* Restart the PHY timestamping block */ 24183a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf); 24193a749623SJacob Keller 242048096710SKarol Kolacinski pfr: 242148096710SKarol Kolacinski /* Init Tx structures */ 2422a69f1cb6SJacob Keller if (ice_is_e810(&pf->hw)) { 242348096710SKarol Kolacinski err = ice_ptp_init_tx_e810(pf, &ptp->port.tx); 2424a69f1cb6SJacob Keller } else { 2425a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp->port.ov_work, 2426a69f1cb6SJacob Keller ice_ptp_wait_for_offset_valid); 24273a749623SJacob Keller err = ice_ptp_init_tx_e822(pf, &ptp->port.tx, 24283a749623SJacob Keller ptp->port.port_num); 2429a69f1cb6SJacob Keller } 243048096710SKarol Kolacinski if (err) 243148096710SKarol Kolacinski goto err; 243248096710SKarol Kolacinski 243348096710SKarol Kolacinski set_bit(ICE_FLAG_PTP, pf->flags); 243448096710SKarol Kolacinski 243548096710SKarol Kolacinski /* Start periodic work going */ 243648096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0); 243748096710SKarol Kolacinski 243848096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP reset successful\n"); 243948096710SKarol Kolacinski return; 244048096710SKarol Kolacinski 244148096710SKarol Kolacinski err: 244248096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP reset failed %d\n", err); 244348096710SKarol Kolacinski } 244448096710SKarol Kolacinski 244548096710SKarol Kolacinski /** 244648096710SKarol Kolacinski * ice_ptp_prepare_for_reset - Prepare PTP for reset 244748096710SKarol Kolacinski * @pf: Board private structure 244848096710SKarol Kolacinski */ 244948096710SKarol Kolacinski void ice_ptp_prepare_for_reset(struct ice_pf *pf) 245048096710SKarol Kolacinski { 245148096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 245248096710SKarol Kolacinski u8 src_tmr; 245348096710SKarol Kolacinski 245448096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags); 245548096710SKarol Kolacinski 245648096710SKarol Kolacinski /* Disable timestamping for both Tx and Rx */ 245748096710SKarol Kolacinski ice_ptp_cfg_timestamp(pf, false); 245848096710SKarol Kolacinski 245948096710SKarol Kolacinski kthread_cancel_delayed_work_sync(&ptp->work); 246048096710SKarol Kolacinski kthread_cancel_work_sync(&ptp->extts_work); 246148096710SKarol Kolacinski 246248096710SKarol Kolacinski if (test_bit(ICE_PFR_REQ, pf->state)) 246348096710SKarol Kolacinski return; 246448096710SKarol Kolacinski 246548096710SKarol Kolacinski ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 246648096710SKarol Kolacinski 246748096710SKarol Kolacinski /* Disable periodic outputs */ 246848096710SKarol Kolacinski ice_ptp_disable_all_clkout(pf); 246948096710SKarol Kolacinski 247048096710SKarol Kolacinski src_tmr = ice_get_ptp_src_clock_index(&pf->hw); 247148096710SKarol Kolacinski 247248096710SKarol Kolacinski /* Disable source clock */ 247348096710SKarol Kolacinski wr32(&pf->hw, GLTSYN_ENA(src_tmr), (u32)~GLTSYN_ENA_TSYN_ENA_M); 247448096710SKarol Kolacinski 247548096710SKarol Kolacinski /* Acquire PHC and system timer to restore after reset */ 247648096710SKarol Kolacinski ptp->reset_time = ktime_get_real_ns(); 247748096710SKarol Kolacinski } 247848096710SKarol Kolacinski 247948096710SKarol Kolacinski /** 248006c16d89SJacob Keller * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device 248106c16d89SJacob Keller * @pf: Board private structure 248206c16d89SJacob Keller * 248306c16d89SJacob Keller * Setup and initialize a PTP clock device that represents the device hardware 248406c16d89SJacob Keller * clock. Save the clock index for other functions connected to the same 248506c16d89SJacob Keller * hardware resource. 248606c16d89SJacob Keller */ 248706c16d89SJacob Keller static int ice_ptp_init_owner(struct ice_pf *pf) 248806c16d89SJacob Keller { 248906c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 249006c16d89SJacob Keller struct timespec64 ts; 24913a749623SJacob Keller int err, itr = 1; 249206c16d89SJacob Keller 2493b2ee7256SJacob Keller err = ice_ptp_init_phc(hw); 2494b2ee7256SJacob Keller if (err) { 2495b2ee7256SJacob Keller dev_err(ice_pf_to_dev(pf), "Failed to initialize PHC, err %d\n", 2496b2ee7256SJacob Keller err); 2497b2ee7256SJacob Keller return err; 2498b2ee7256SJacob Keller } 249906c16d89SJacob Keller 250006c16d89SJacob Keller /* Acquire the global hardware lock */ 250106c16d89SJacob Keller if (!ice_ptp_lock(hw)) { 250206c16d89SJacob Keller err = -EBUSY; 250306c16d89SJacob Keller goto err_exit; 250406c16d89SJacob Keller } 250506c16d89SJacob Keller 250606c16d89SJacob Keller /* Write the increment time value to PHY and LAN */ 250778267d0cSJacob Keller err = ice_ptp_write_incval(hw, ice_base_incval(pf)); 250806c16d89SJacob Keller if (err) { 250906c16d89SJacob Keller ice_ptp_unlock(hw); 251006c16d89SJacob Keller goto err_exit; 251106c16d89SJacob Keller } 251206c16d89SJacob Keller 251306c16d89SJacob Keller ts = ktime_to_timespec64(ktime_get_real()); 251406c16d89SJacob Keller /* Write the initial Time value to PHY and LAN */ 251506c16d89SJacob Keller err = ice_ptp_write_init(pf, &ts); 251606c16d89SJacob Keller if (err) { 251706c16d89SJacob Keller ice_ptp_unlock(hw); 251806c16d89SJacob Keller goto err_exit; 251906c16d89SJacob Keller } 252006c16d89SJacob Keller 252106c16d89SJacob Keller /* Release the global hardware lock */ 252206c16d89SJacob Keller ice_ptp_unlock(hw); 252306c16d89SJacob Keller 25243a749623SJacob Keller if (!ice_is_e810(hw)) { 25253a749623SJacob Keller /* Enable quad interrupts */ 25263a749623SJacob Keller err = ice_ptp_tx_ena_intr(pf, true, itr); 25273a749623SJacob Keller if (err) 25283a749623SJacob Keller goto err_exit; 25293a749623SJacob Keller } 25303a749623SJacob Keller 253106c16d89SJacob Keller /* Ensure we have a clock device */ 253206c16d89SJacob Keller err = ice_ptp_create_clock(pf); 253306c16d89SJacob Keller if (err) 253406c16d89SJacob Keller goto err_clk; 253506c16d89SJacob Keller 253667569a7fSJacob Keller /* Store the PTP clock index for other PFs */ 253767569a7fSJacob Keller ice_set_ptp_clock_index(pf); 253867569a7fSJacob Keller 253906c16d89SJacob Keller return 0; 254006c16d89SJacob Keller 254106c16d89SJacob Keller err_clk: 254206c16d89SJacob Keller pf->ptp.clock = NULL; 254306c16d89SJacob Keller err_exit: 254406c16d89SJacob Keller return err; 254506c16d89SJacob Keller } 254606c16d89SJacob Keller 254706c16d89SJacob Keller /** 254848096710SKarol Kolacinski * ice_ptp_init_work - Initialize PTP work threads 254948096710SKarol Kolacinski * @pf: Board private structure 255048096710SKarol Kolacinski * @ptp: PF PTP structure 255148096710SKarol Kolacinski */ 255248096710SKarol Kolacinski static int ice_ptp_init_work(struct ice_pf *pf, struct ice_ptp *ptp) 255348096710SKarol Kolacinski { 255448096710SKarol Kolacinski struct kthread_worker *kworker; 255548096710SKarol Kolacinski 255648096710SKarol Kolacinski /* Initialize work functions */ 255748096710SKarol Kolacinski kthread_init_delayed_work(&ptp->work, ice_ptp_periodic_work); 255848096710SKarol Kolacinski kthread_init_work(&ptp->extts_work, ice_ptp_extts_work); 255948096710SKarol Kolacinski 256048096710SKarol Kolacinski /* Allocate a kworker for handling work required for the ports 256148096710SKarol Kolacinski * connected to the PTP hardware clock. 256248096710SKarol Kolacinski */ 256348096710SKarol Kolacinski kworker = kthread_create_worker(0, "ice-ptp-%s", 256448096710SKarol Kolacinski dev_name(ice_pf_to_dev(pf))); 256548096710SKarol Kolacinski if (IS_ERR(kworker)) 256648096710SKarol Kolacinski return PTR_ERR(kworker); 256748096710SKarol Kolacinski 256848096710SKarol Kolacinski ptp->kworker = kworker; 256948096710SKarol Kolacinski 257048096710SKarol Kolacinski /* Start periodic work going */ 257148096710SKarol Kolacinski kthread_queue_delayed_work(ptp->kworker, &ptp->work, 0); 257248096710SKarol Kolacinski 257348096710SKarol Kolacinski return 0; 257448096710SKarol Kolacinski } 257548096710SKarol Kolacinski 257648096710SKarol Kolacinski /** 25773a749623SJacob Keller * ice_ptp_init_port - Initialize PTP port structure 25783a749623SJacob Keller * @pf: Board private structure 25793a749623SJacob Keller * @ptp_port: PTP port structure 25803a749623SJacob Keller */ 25813a749623SJacob Keller static int ice_ptp_init_port(struct ice_pf *pf, struct ice_ptp_port *ptp_port) 25823a749623SJacob Keller { 25833a749623SJacob Keller mutex_init(&ptp_port->ps_lock); 25843a749623SJacob Keller 25853a749623SJacob Keller if (ice_is_e810(&pf->hw)) 25863a749623SJacob Keller return ice_ptp_init_tx_e810(pf, &ptp_port->tx); 25873a749623SJacob Keller 2588a69f1cb6SJacob Keller kthread_init_delayed_work(&ptp_port->ov_work, 2589a69f1cb6SJacob Keller ice_ptp_wait_for_offset_valid); 25903a749623SJacob Keller return ice_ptp_init_tx_e822(pf, &ptp_port->tx, ptp_port->port_num); 25913a749623SJacob Keller } 25923a749623SJacob Keller 25933a749623SJacob Keller /** 2594b2ee7256SJacob Keller * ice_ptp_init - Initialize PTP hardware clock support 259506c16d89SJacob Keller * @pf: Board private structure 259606c16d89SJacob Keller * 2597b2ee7256SJacob Keller * Set up the device for interacting with the PTP hardware clock for all 2598b2ee7256SJacob Keller * functions, both the function that owns the clock hardware, and the 2599b2ee7256SJacob Keller * functions connected to the clock hardware. 2600b2ee7256SJacob Keller * 2601b2ee7256SJacob Keller * The clock owner will allocate and register a ptp_clock with the 2602b2ee7256SJacob Keller * PTP_1588_CLOCK infrastructure. All functions allocate a kthread and work 2603b2ee7256SJacob Keller * items used for asynchronous work such as Tx timestamps and periodic work. 260406c16d89SJacob Keller */ 260506c16d89SJacob Keller void ice_ptp_init(struct ice_pf *pf) 260606c16d89SJacob Keller { 260748096710SKarol Kolacinski struct ice_ptp *ptp = &pf->ptp; 260806c16d89SJacob Keller struct ice_hw *hw = &pf->hw; 260906c16d89SJacob Keller int err; 261006c16d89SJacob Keller 2611b2ee7256SJacob Keller /* If this function owns the clock hardware, it must allocate and 2612b2ee7256SJacob Keller * configure the PTP clock device to represent it. 2613b2ee7256SJacob Keller */ 261406c16d89SJacob Keller if (hw->func_caps.ts_func_info.src_tmr_owned) { 261506c16d89SJacob Keller err = ice_ptp_init_owner(pf); 261606c16d89SJacob Keller if (err) 261748096710SKarol Kolacinski goto err; 261806c16d89SJacob Keller } 261906c16d89SJacob Keller 26203a749623SJacob Keller ptp->port.port_num = hw->pf_id; 26213a749623SJacob Keller err = ice_ptp_init_port(pf, &ptp->port); 262248096710SKarol Kolacinski if (err) 262348096710SKarol Kolacinski goto err; 262477a78115SJacob Keller 26253a749623SJacob Keller /* Start the PHY timestamping block */ 26263a749623SJacob Keller ice_ptp_reset_phy_timestamping(pf); 26273a749623SJacob Keller 262806c16d89SJacob Keller set_bit(ICE_FLAG_PTP, pf->flags); 262948096710SKarol Kolacinski err = ice_ptp_init_work(pf, ptp); 263048096710SKarol Kolacinski if (err) 263148096710SKarol Kolacinski goto err; 263206c16d89SJacob Keller 263348096710SKarol Kolacinski dev_info(ice_pf_to_dev(pf), "PTP init successful\n"); 263477a78115SJacob Keller return; 263577a78115SJacob Keller 263648096710SKarol Kolacinski err: 263777a78115SJacob Keller /* If we registered a PTP clock, release it */ 263877a78115SJacob Keller if (pf->ptp.clock) { 263948096710SKarol Kolacinski ptp_clock_unregister(ptp->clock); 264077a78115SJacob Keller pf->ptp.clock = NULL; 264177a78115SJacob Keller } 264248096710SKarol Kolacinski clear_bit(ICE_FLAG_PTP, pf->flags); 264348096710SKarol Kolacinski dev_err(ice_pf_to_dev(pf), "PTP failed %d\n", err); 264406c16d89SJacob Keller } 264506c16d89SJacob Keller 264606c16d89SJacob Keller /** 264706c16d89SJacob Keller * ice_ptp_release - Disable the driver/HW support and unregister the clock 264806c16d89SJacob Keller * @pf: Board private structure 264906c16d89SJacob Keller * 265006c16d89SJacob Keller * This function handles the cleanup work required from the initialization by 265106c16d89SJacob Keller * clearing out the important information and unregistering the clock 265206c16d89SJacob Keller */ 265306c16d89SJacob Keller void ice_ptp_release(struct ice_pf *pf) 265406c16d89SJacob Keller { 2655fd1b5bebSYongxin Liu if (!test_bit(ICE_FLAG_PTP, pf->flags)) 2656fd1b5bebSYongxin Liu return; 2657fd1b5bebSYongxin Liu 265877a78115SJacob Keller /* Disable timestamping for both Tx and Rx */ 265977a78115SJacob Keller ice_ptp_cfg_timestamp(pf, false); 266077a78115SJacob Keller 2661ea9b847cSJacob Keller ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 2662ea9b847cSJacob Keller 266306c16d89SJacob Keller clear_bit(ICE_FLAG_PTP, pf->flags); 266406c16d89SJacob Keller 266577a78115SJacob Keller kthread_cancel_delayed_work_sync(&pf->ptp.work); 266677a78115SJacob Keller 26673a749623SJacob Keller ice_ptp_port_phy_stop(&pf->ptp.port); 26683a749623SJacob Keller mutex_destroy(&pf->ptp.port.ps_lock); 266977a78115SJacob Keller if (pf->ptp.kworker) { 267077a78115SJacob Keller kthread_destroy_worker(pf->ptp.kworker); 267177a78115SJacob Keller pf->ptp.kworker = NULL; 267277a78115SJacob Keller } 267377a78115SJacob Keller 267406c16d89SJacob Keller if (!pf->ptp.clock) 267506c16d89SJacob Keller return; 267606c16d89SJacob Keller 26779ee31343SJacob Keller /* Disable periodic outputs */ 26789ee31343SJacob Keller ice_ptp_disable_all_clkout(pf); 26799ee31343SJacob Keller 268067569a7fSJacob Keller ice_clear_ptp_clock_index(pf); 268106c16d89SJacob Keller ptp_clock_unregister(pf->ptp.clock); 268206c16d89SJacob Keller pf->ptp.clock = NULL; 268306c16d89SJacob Keller 268406c16d89SJacob Keller dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n"); 268506c16d89SJacob Keller } 2686