1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) 2021, Intel Corporation. */ 3 4 #include "ice.h" 5 #include "ice_lib.h" 6 7 /** 8 * ice_set_tx_tstamp - Enable or disable Tx timestamping 9 * @pf: The PF pointer to search in 10 * @on: bool value for whether timestamps are enabled or disabled 11 */ 12 static void ice_set_tx_tstamp(struct ice_pf *pf, bool on) 13 { 14 struct ice_vsi *vsi; 15 u32 val; 16 u16 i; 17 18 vsi = ice_get_main_vsi(pf); 19 if (!vsi) 20 return; 21 22 /* Set the timestamp enable flag for all the Tx rings */ 23 ice_for_each_rxq(vsi, i) { 24 if (!vsi->tx_rings[i]) 25 continue; 26 vsi->tx_rings[i]->ptp_tx = on; 27 } 28 29 /* Configure the Tx timestamp interrupt */ 30 val = rd32(&pf->hw, PFINT_OICR_ENA); 31 if (on) 32 val |= PFINT_OICR_TSYN_TX_M; 33 else 34 val &= ~PFINT_OICR_TSYN_TX_M; 35 wr32(&pf->hw, PFINT_OICR_ENA, val); 36 } 37 38 /** 39 * ice_set_rx_tstamp - Enable or disable Rx timestamping 40 * @pf: The PF pointer to search in 41 * @on: bool value for whether timestamps are enabled or disabled 42 */ 43 static void ice_set_rx_tstamp(struct ice_pf *pf, bool on) 44 { 45 struct ice_vsi *vsi; 46 u16 i; 47 48 vsi = ice_get_main_vsi(pf); 49 if (!vsi) 50 return; 51 52 /* Set the timestamp flag for all the Rx rings */ 53 ice_for_each_rxq(vsi, i) { 54 if (!vsi->rx_rings[i]) 55 continue; 56 vsi->rx_rings[i]->ptp_rx = on; 57 } 58 } 59 60 /** 61 * ice_ptp_cfg_timestamp - Configure timestamp for init/deinit 62 * @pf: Board private structure 63 * @ena: bool value to enable or disable time stamp 64 * 65 * This function will configure timestamping during PTP initialization 66 * and deinitialization 67 */ 68 static void ice_ptp_cfg_timestamp(struct ice_pf *pf, bool ena) 69 { 70 ice_set_tx_tstamp(pf, ena); 71 ice_set_rx_tstamp(pf, ena); 72 73 if (ena) { 74 pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_ALL; 75 pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_ON; 76 } else { 77 pf->ptp.tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE; 78 pf->ptp.tstamp_config.tx_type = HWTSTAMP_TX_OFF; 79 } 80 } 81 82 /** 83 * ice_get_ptp_clock_index - Get the PTP clock index 84 * @pf: the PF pointer 85 * 86 * Determine the clock index of the PTP clock associated with this device. If 87 * this is the PF controlling the clock, just use the local access to the 88 * clock device pointer. 89 * 90 * Otherwise, read from the driver shared parameters to determine the clock 91 * index value. 92 * 93 * Returns: the index of the PTP clock associated with this device, or -1 if 94 * there is no associated clock. 95 */ 96 int ice_get_ptp_clock_index(struct ice_pf *pf) 97 { 98 struct device *dev = ice_pf_to_dev(pf); 99 enum ice_aqc_driver_params param_idx; 100 struct ice_hw *hw = &pf->hw; 101 u8 tmr_idx; 102 u32 value; 103 int err; 104 105 /* Use the ptp_clock structure if we're the main PF */ 106 if (pf->ptp.clock) 107 return ptp_clock_index(pf->ptp.clock); 108 109 tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 110 if (!tmr_idx) 111 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 112 else 113 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 114 115 err = ice_aq_get_driver_param(hw, param_idx, &value, NULL); 116 if (err) { 117 dev_err(dev, "Failed to read PTP clock index parameter, err %d aq_err %s\n", 118 err, ice_aq_str(hw->adminq.sq_last_status)); 119 return -1; 120 } 121 122 /* The PTP clock index is an integer, and will be between 0 and 123 * INT_MAX. The highest bit of the driver shared parameter is used to 124 * indicate whether or not the currently stored clock index is valid. 125 */ 126 if (!(value & PTP_SHARED_CLK_IDX_VALID)) 127 return -1; 128 129 return value & ~PTP_SHARED_CLK_IDX_VALID; 130 } 131 132 /** 133 * ice_set_ptp_clock_index - Set the PTP clock index 134 * @pf: the PF pointer 135 * 136 * Set the PTP clock index for this device into the shared driver parameters, 137 * so that other PFs associated with this device can read it. 138 * 139 * If the PF is unable to store the clock index, it will log an error, but 140 * will continue operating PTP. 141 */ 142 static void ice_set_ptp_clock_index(struct ice_pf *pf) 143 { 144 struct device *dev = ice_pf_to_dev(pf); 145 enum ice_aqc_driver_params param_idx; 146 struct ice_hw *hw = &pf->hw; 147 u8 tmr_idx; 148 u32 value; 149 int err; 150 151 if (!pf->ptp.clock) 152 return; 153 154 tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 155 if (!tmr_idx) 156 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 157 else 158 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 159 160 value = (u32)ptp_clock_index(pf->ptp.clock); 161 if (value > INT_MAX) { 162 dev_err(dev, "PTP Clock index is too large to store\n"); 163 return; 164 } 165 value |= PTP_SHARED_CLK_IDX_VALID; 166 167 err = ice_aq_set_driver_param(hw, param_idx, value, NULL); 168 if (err) { 169 dev_err(dev, "Failed to set PTP clock index parameter, err %d aq_err %s\n", 170 err, ice_aq_str(hw->adminq.sq_last_status)); 171 } 172 } 173 174 /** 175 * ice_clear_ptp_clock_index - Clear the PTP clock index 176 * @pf: the PF pointer 177 * 178 * Clear the PTP clock index for this device. Must be called when 179 * unregistering the PTP clock, in order to ensure other PFs stop reporting 180 * a clock object that no longer exists. 181 */ 182 static void ice_clear_ptp_clock_index(struct ice_pf *pf) 183 { 184 struct device *dev = ice_pf_to_dev(pf); 185 enum ice_aqc_driver_params param_idx; 186 struct ice_hw *hw = &pf->hw; 187 u8 tmr_idx; 188 int err; 189 190 /* Do not clear the index if we don't own the timer */ 191 if (!hw->func_caps.ts_func_info.src_tmr_owned) 192 return; 193 194 tmr_idx = hw->func_caps.ts_func_info.tmr_index_assoc; 195 if (!tmr_idx) 196 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR0; 197 else 198 param_idx = ICE_AQC_DRIVER_PARAM_CLK_IDX_TMR1; 199 200 err = ice_aq_set_driver_param(hw, param_idx, 0, NULL); 201 if (err) { 202 dev_dbg(dev, "Failed to clear PTP clock index parameter, err %d aq_err %s\n", 203 err, ice_aq_str(hw->adminq.sq_last_status)); 204 } 205 } 206 207 /** 208 * ice_ptp_read_src_clk_reg - Read the source clock register 209 * @pf: Board private structure 210 * @sts: Optional parameter for holding a pair of system timestamps from 211 * the system clock. Will be ignored if NULL is given. 212 */ 213 static u64 214 ice_ptp_read_src_clk_reg(struct ice_pf *pf, struct ptp_system_timestamp *sts) 215 { 216 struct ice_hw *hw = &pf->hw; 217 u32 hi, lo, lo2; 218 u8 tmr_idx; 219 220 tmr_idx = ice_get_ptp_src_clock_index(hw); 221 /* Read the system timestamp pre PHC read */ 222 ptp_read_system_prets(sts); 223 224 lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 225 226 /* Read the system timestamp post PHC read */ 227 ptp_read_system_postts(sts); 228 229 hi = rd32(hw, GLTSYN_TIME_H(tmr_idx)); 230 lo2 = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 231 232 if (lo2 < lo) { 233 /* if TIME_L rolled over read TIME_L again and update 234 * system timestamps 235 */ 236 ptp_read_system_prets(sts); 237 lo = rd32(hw, GLTSYN_TIME_L(tmr_idx)); 238 ptp_read_system_postts(sts); 239 hi = rd32(hw, GLTSYN_TIME_H(tmr_idx)); 240 } 241 242 return ((u64)hi << 32) | lo; 243 } 244 245 /** 246 * ice_ptp_update_cached_phctime - Update the cached PHC time values 247 * @pf: Board specific private structure 248 * 249 * This function updates the system time values which are cached in the PF 250 * structure and the Rx rings. 251 * 252 * This function must be called periodically to ensure that the cached value 253 * is never more than 2 seconds old. It must also be called whenever the PHC 254 * time has been changed. 255 */ 256 static void ice_ptp_update_cached_phctime(struct ice_pf *pf) 257 { 258 u64 systime; 259 int i; 260 261 /* Read the current PHC time */ 262 systime = ice_ptp_read_src_clk_reg(pf, NULL); 263 264 /* Update the cached PHC time stored in the PF structure */ 265 WRITE_ONCE(pf->ptp.cached_phc_time, systime); 266 267 ice_for_each_vsi(pf, i) { 268 struct ice_vsi *vsi = pf->vsi[i]; 269 int j; 270 271 if (!vsi) 272 continue; 273 274 if (vsi->type != ICE_VSI_PF) 275 continue; 276 277 ice_for_each_rxq(vsi, j) { 278 if (!vsi->rx_rings[j]) 279 continue; 280 WRITE_ONCE(vsi->rx_rings[j]->cached_phctime, systime); 281 } 282 } 283 } 284 285 /** 286 * ice_ptp_extend_32b_ts - Convert a 32b nanoseconds timestamp to 64b 287 * @cached_phc_time: recently cached copy of PHC time 288 * @in_tstamp: Ingress/egress 32b nanoseconds timestamp value 289 * 290 * Hardware captures timestamps which contain only 32 bits of nominal 291 * nanoseconds, as opposed to the 64bit timestamps that the stack expects. 292 * Note that the captured timestamp values may be 40 bits, but the lower 293 * 8 bits are sub-nanoseconds and generally discarded. 294 * 295 * Extend the 32bit nanosecond timestamp using the following algorithm and 296 * assumptions: 297 * 298 * 1) have a recently cached copy of the PHC time 299 * 2) assume that the in_tstamp was captured 2^31 nanoseconds (~2.1 300 * seconds) before or after the PHC time was captured. 301 * 3) calculate the delta between the cached time and the timestamp 302 * 4) if the delta is smaller than 2^31 nanoseconds, then the timestamp was 303 * captured after the PHC time. In this case, the full timestamp is just 304 * the cached PHC time plus the delta. 305 * 5) otherwise, if the delta is larger than 2^31 nanoseconds, then the 306 * timestamp was captured *before* the PHC time, i.e. because the PHC 307 * cache was updated after the timestamp was captured by hardware. In this 308 * case, the full timestamp is the cached time minus the inverse delta. 309 * 310 * This algorithm works even if the PHC time was updated after a Tx timestamp 311 * was requested, but before the Tx timestamp event was reported from 312 * hardware. 313 * 314 * This calculation primarily relies on keeping the cached PHC time up to 315 * date. If the timestamp was captured more than 2^31 nanoseconds after the 316 * PHC time, it is possible that the lower 32bits of PHC time have 317 * overflowed more than once, and we might generate an incorrect timestamp. 318 * 319 * This is prevented by (a) periodically updating the cached PHC time once 320 * a second, and (b) discarding any Tx timestamp packet if it has waited for 321 * a timestamp for more than one second. 322 */ 323 static u64 ice_ptp_extend_32b_ts(u64 cached_phc_time, u32 in_tstamp) 324 { 325 u32 delta, phc_time_lo; 326 u64 ns; 327 328 /* Extract the lower 32 bits of the PHC time */ 329 phc_time_lo = (u32)cached_phc_time; 330 331 /* Calculate the delta between the lower 32bits of the cached PHC 332 * time and the in_tstamp value 333 */ 334 delta = (in_tstamp - phc_time_lo); 335 336 /* Do not assume that the in_tstamp is always more recent than the 337 * cached PHC time. If the delta is large, it indicates that the 338 * in_tstamp was taken in the past, and should be converted 339 * forward. 340 */ 341 if (delta > (U32_MAX / 2)) { 342 /* reverse the delta calculation here */ 343 delta = (phc_time_lo - in_tstamp); 344 ns = cached_phc_time - delta; 345 } else { 346 ns = cached_phc_time + delta; 347 } 348 349 return ns; 350 } 351 352 /** 353 * ice_ptp_extend_40b_ts - Convert a 40b timestamp to 64b nanoseconds 354 * @pf: Board private structure 355 * @in_tstamp: Ingress/egress 40b timestamp value 356 * 357 * The Tx and Rx timestamps are 40 bits wide, including 32 bits of nominal 358 * nanoseconds, 7 bits of sub-nanoseconds, and a valid bit. 359 * 360 * *--------------------------------------------------------------* 361 * | 32 bits of nanoseconds | 7 high bits of sub ns underflow | v | 362 * *--------------------------------------------------------------* 363 * 364 * The low bit is an indicator of whether the timestamp is valid. The next 365 * 7 bits are a capture of the upper 7 bits of the sub-nanosecond underflow, 366 * and the remaining 32 bits are the lower 32 bits of the PHC timer. 367 * 368 * It is assumed that the caller verifies the timestamp is valid prior to 369 * calling this function. 370 * 371 * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC 372 * time stored in the device private PTP structure as the basis for timestamp 373 * extension. 374 * 375 * See ice_ptp_extend_32b_ts for a detailed explanation of the extension 376 * algorithm. 377 */ 378 static u64 ice_ptp_extend_40b_ts(struct ice_pf *pf, u64 in_tstamp) 379 { 380 const u64 mask = GENMASK_ULL(31, 0); 381 382 return ice_ptp_extend_32b_ts(pf->ptp.cached_phc_time, 383 (in_tstamp >> 8) & mask); 384 } 385 386 /** 387 * ice_ptp_read_time - Read the time from the device 388 * @pf: Board private structure 389 * @ts: timespec structure to hold the current time value 390 * @sts: Optional parameter for holding a pair of system timestamps from 391 * the system clock. Will be ignored if NULL is given. 392 * 393 * This function reads the source clock registers and stores them in a timespec. 394 * However, since the registers are 64 bits of nanoseconds, we must convert the 395 * result to a timespec before we can return. 396 */ 397 static void 398 ice_ptp_read_time(struct ice_pf *pf, struct timespec64 *ts, 399 struct ptp_system_timestamp *sts) 400 { 401 u64 time_ns = ice_ptp_read_src_clk_reg(pf, sts); 402 403 *ts = ns_to_timespec64(time_ns); 404 } 405 406 /** 407 * ice_ptp_write_init - Set PHC time to provided value 408 * @pf: Board private structure 409 * @ts: timespec structure that holds the new time value 410 * 411 * Set the PHC time to the specified time provided in the timespec. 412 */ 413 static int ice_ptp_write_init(struct ice_pf *pf, struct timespec64 *ts) 414 { 415 u64 ns = timespec64_to_ns(ts); 416 struct ice_hw *hw = &pf->hw; 417 418 return ice_ptp_init_time(hw, ns); 419 } 420 421 /** 422 * ice_ptp_write_adj - Adjust PHC clock time atomically 423 * @pf: Board private structure 424 * @adj: Adjustment in nanoseconds 425 * 426 * Perform an atomic adjustment of the PHC time by the specified number of 427 * nanoseconds. 428 */ 429 static int ice_ptp_write_adj(struct ice_pf *pf, s32 adj) 430 { 431 struct ice_hw *hw = &pf->hw; 432 433 return ice_ptp_adj_clock(hw, adj); 434 } 435 436 /** 437 * ice_ptp_adjfine - Adjust clock increment rate 438 * @info: the driver's PTP info structure 439 * @scaled_ppm: Parts per million with 16-bit fractional field 440 * 441 * Adjust the frequency of the clock by the indicated scaled ppm from the 442 * base frequency. 443 */ 444 static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm) 445 { 446 struct ice_pf *pf = ptp_info_to_pf(info); 447 u64 freq, divisor = 1000000ULL; 448 struct ice_hw *hw = &pf->hw; 449 s64 incval, diff; 450 int neg_adj = 0; 451 int err; 452 453 incval = ICE_PTP_NOMINAL_INCVAL_E810; 454 455 if (scaled_ppm < 0) { 456 neg_adj = 1; 457 scaled_ppm = -scaled_ppm; 458 } 459 460 while ((u64)scaled_ppm > div_u64(U64_MAX, incval)) { 461 /* handle overflow by scaling down the scaled_ppm and 462 * the divisor, losing some precision 463 */ 464 scaled_ppm >>= 2; 465 divisor >>= 2; 466 } 467 468 freq = (incval * (u64)scaled_ppm) >> 16; 469 diff = div_u64(freq, divisor); 470 471 if (neg_adj) 472 incval -= diff; 473 else 474 incval += diff; 475 476 err = ice_ptp_write_incval_locked(hw, incval); 477 if (err) { 478 dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n", 479 err); 480 return -EIO; 481 } 482 483 return 0; 484 } 485 486 /** 487 * ice_ptp_gettimex64 - Get the time of the clock 488 * @info: the driver's PTP info structure 489 * @ts: timespec64 structure to hold the current time value 490 * @sts: Optional parameter for holding a pair of system timestamps from 491 * the system clock. Will be ignored if NULL is given. 492 * 493 * Read the device clock and return the correct value on ns, after converting it 494 * into a timespec struct. 495 */ 496 static int 497 ice_ptp_gettimex64(struct ptp_clock_info *info, struct timespec64 *ts, 498 struct ptp_system_timestamp *sts) 499 { 500 struct ice_pf *pf = ptp_info_to_pf(info); 501 struct ice_hw *hw = &pf->hw; 502 503 if (!ice_ptp_lock(hw)) { 504 dev_err(ice_pf_to_dev(pf), "PTP failed to get time\n"); 505 return -EBUSY; 506 } 507 508 ice_ptp_read_time(pf, ts, sts); 509 ice_ptp_unlock(hw); 510 511 return 0; 512 } 513 514 /** 515 * ice_ptp_settime64 - Set the time of the clock 516 * @info: the driver's PTP info structure 517 * @ts: timespec64 structure that holds the new time value 518 * 519 * Set the device clock to the user input value. The conversion from timespec 520 * to ns happens in the write function. 521 */ 522 static int 523 ice_ptp_settime64(struct ptp_clock_info *info, const struct timespec64 *ts) 524 { 525 struct ice_pf *pf = ptp_info_to_pf(info); 526 struct timespec64 ts64 = *ts; 527 struct ice_hw *hw = &pf->hw; 528 int err; 529 530 if (!ice_ptp_lock(hw)) { 531 err = -EBUSY; 532 goto exit; 533 } 534 535 err = ice_ptp_write_init(pf, &ts64); 536 ice_ptp_unlock(hw); 537 538 if (!err) 539 ice_ptp_update_cached_phctime(pf); 540 541 exit: 542 if (err) { 543 dev_err(ice_pf_to_dev(pf), "PTP failed to set time %d\n", err); 544 return err; 545 } 546 547 return 0; 548 } 549 550 /** 551 * ice_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment 552 * @info: the driver's PTP info structure 553 * @delta: Offset in nanoseconds to adjust the time by 554 */ 555 static int ice_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta) 556 { 557 struct timespec64 now, then; 558 559 then = ns_to_timespec64(delta); 560 ice_ptp_gettimex64(info, &now, NULL); 561 now = timespec64_add(now, then); 562 563 return ice_ptp_settime64(info, (const struct timespec64 *)&now); 564 } 565 566 /** 567 * ice_ptp_adjtime - Adjust the time of the clock by the indicated delta 568 * @info: the driver's PTP info structure 569 * @delta: Offset in nanoseconds to adjust the time by 570 */ 571 static int ice_ptp_adjtime(struct ptp_clock_info *info, s64 delta) 572 { 573 struct ice_pf *pf = ptp_info_to_pf(info); 574 struct ice_hw *hw = &pf->hw; 575 struct device *dev; 576 int err; 577 578 dev = ice_pf_to_dev(pf); 579 580 /* Hardware only supports atomic adjustments using signed 32-bit 581 * integers. For any adjustment outside this range, perform 582 * a non-atomic get->adjust->set flow. 583 */ 584 if (delta > S32_MAX || delta < S32_MIN) { 585 dev_dbg(dev, "delta = %lld, adjtime non-atomic\n", delta); 586 return ice_ptp_adjtime_nonatomic(info, delta); 587 } 588 589 if (!ice_ptp_lock(hw)) { 590 dev_err(dev, "PTP failed to acquire semaphore in adjtime\n"); 591 return -EBUSY; 592 } 593 594 err = ice_ptp_write_adj(pf, delta); 595 596 ice_ptp_unlock(hw); 597 598 if (err) { 599 dev_err(dev, "PTP failed to adjust time, err %d\n", err); 600 return err; 601 } 602 603 ice_ptp_update_cached_phctime(pf); 604 605 return 0; 606 } 607 608 /** 609 * ice_ptp_get_ts_config - ioctl interface to read the timestamping config 610 * @pf: Board private structure 611 * @ifr: ioctl data 612 * 613 * Copy the timestamping config to user buffer 614 */ 615 int ice_ptp_get_ts_config(struct ice_pf *pf, struct ifreq *ifr) 616 { 617 struct hwtstamp_config *config; 618 619 if (!test_bit(ICE_FLAG_PTP, pf->flags)) 620 return -EIO; 621 622 config = &pf->ptp.tstamp_config; 623 624 return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ? 625 -EFAULT : 0; 626 } 627 628 /** 629 * ice_ptp_set_timestamp_mode - Setup driver for requested timestamp mode 630 * @pf: Board private structure 631 * @config: hwtstamp settings requested or saved 632 */ 633 static int 634 ice_ptp_set_timestamp_mode(struct ice_pf *pf, struct hwtstamp_config *config) 635 { 636 /* Reserved for future extensions. */ 637 if (config->flags) 638 return -EINVAL; 639 640 switch (config->tx_type) { 641 case HWTSTAMP_TX_OFF: 642 ice_set_tx_tstamp(pf, false); 643 break; 644 case HWTSTAMP_TX_ON: 645 ice_set_tx_tstamp(pf, true); 646 break; 647 default: 648 return -ERANGE; 649 } 650 651 switch (config->rx_filter) { 652 case HWTSTAMP_FILTER_NONE: 653 ice_set_rx_tstamp(pf, false); 654 break; 655 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 656 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 657 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 658 case HWTSTAMP_FILTER_PTP_V2_EVENT: 659 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 660 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 661 case HWTSTAMP_FILTER_PTP_V2_SYNC: 662 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 663 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 664 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 665 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 666 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 667 case HWTSTAMP_FILTER_NTP_ALL: 668 case HWTSTAMP_FILTER_ALL: 669 config->rx_filter = HWTSTAMP_FILTER_ALL; 670 ice_set_rx_tstamp(pf, true); 671 break; 672 default: 673 return -ERANGE; 674 } 675 676 return 0; 677 } 678 679 /** 680 * ice_ptp_set_ts_config - ioctl interface to control the timestamping 681 * @pf: Board private structure 682 * @ifr: ioctl data 683 * 684 * Get the user config and store it 685 */ 686 int ice_ptp_set_ts_config(struct ice_pf *pf, struct ifreq *ifr) 687 { 688 struct hwtstamp_config config; 689 int err; 690 691 if (!test_bit(ICE_FLAG_PTP, pf->flags)) 692 return -EAGAIN; 693 694 if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 695 return -EFAULT; 696 697 err = ice_ptp_set_timestamp_mode(pf, &config); 698 if (err) 699 return err; 700 701 /* Save these settings for future reference */ 702 pf->ptp.tstamp_config = config; 703 704 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 705 -EFAULT : 0; 706 } 707 708 /** 709 * ice_ptp_rx_hwtstamp - Check for an Rx timestamp 710 * @rx_ring: Ring to get the VSI info 711 * @rx_desc: Receive descriptor 712 * @skb: Particular skb to send timestamp with 713 * 714 * The driver receives a notification in the receive descriptor with timestamp. 715 * The timestamp is in ns, so we must convert the result first. 716 */ 717 void 718 ice_ptp_rx_hwtstamp(struct ice_ring *rx_ring, 719 union ice_32b_rx_flex_desc *rx_desc, struct sk_buff *skb) 720 { 721 u32 ts_high; 722 u64 ts_ns; 723 724 /* Populate timesync data into skb */ 725 if (rx_desc->wb.time_stamp_low & ICE_PTP_TS_VALID) { 726 struct skb_shared_hwtstamps *hwtstamps; 727 728 /* Use ice_ptp_extend_32b_ts directly, using the ring-specific 729 * cached PHC value, rather than accessing the PF. This also 730 * allows us to simply pass the upper 32bits of nanoseconds 731 * directly. Calling ice_ptp_extend_40b_ts is unnecessary as 732 * it would just discard these bits itself. 733 */ 734 ts_high = le32_to_cpu(rx_desc->wb.flex_ts.ts_high); 735 ts_ns = ice_ptp_extend_32b_ts(rx_ring->cached_phctime, ts_high); 736 737 hwtstamps = skb_hwtstamps(skb); 738 memset(hwtstamps, 0, sizeof(*hwtstamps)); 739 hwtstamps->hwtstamp = ns_to_ktime(ts_ns); 740 } 741 } 742 743 /** 744 * ice_ptp_set_caps - Set PTP capabilities 745 * @pf: Board private structure 746 */ 747 static void ice_ptp_set_caps(struct ice_pf *pf) 748 { 749 struct ptp_clock_info *info = &pf->ptp.info; 750 struct device *dev = ice_pf_to_dev(pf); 751 752 snprintf(info->name, sizeof(info->name) - 1, "%s-%s-clk", 753 dev_driver_string(dev), dev_name(dev)); 754 info->owner = THIS_MODULE; 755 info->max_adj = 999999999; 756 info->adjtime = ice_ptp_adjtime; 757 info->adjfine = ice_ptp_adjfine; 758 info->gettimex64 = ice_ptp_gettimex64; 759 info->settime64 = ice_ptp_settime64; 760 } 761 762 /** 763 * ice_ptp_create_clock - Create PTP clock device for userspace 764 * @pf: Board private structure 765 * 766 * This function creates a new PTP clock device. It only creates one if we 767 * don't already have one. Will return error if it can't create one, but success 768 * if we already have a device. Should be used by ice_ptp_init to create clock 769 * initially, and prevent global resets from creating new clock devices. 770 */ 771 static long ice_ptp_create_clock(struct ice_pf *pf) 772 { 773 struct ptp_clock_info *info; 774 struct ptp_clock *clock; 775 struct device *dev; 776 777 /* No need to create a clock device if we already have one */ 778 if (pf->ptp.clock) 779 return 0; 780 781 ice_ptp_set_caps(pf); 782 783 info = &pf->ptp.info; 784 dev = ice_pf_to_dev(pf); 785 786 /* Attempt to register the clock before enabling the hardware. */ 787 clock = ptp_clock_register(info, dev); 788 if (IS_ERR(clock)) 789 return PTR_ERR(clock); 790 791 pf->ptp.clock = clock; 792 793 return 0; 794 } 795 796 /** 797 * ice_ptp_tx_tstamp_work - Process Tx timestamps for a port 798 * @work: pointer to the kthread_work struct 799 * 800 * Process timestamps captured by the PHY associated with this port. To do 801 * this, loop over each index with a waiting skb. 802 * 803 * If a given index has a valid timestamp, perform the following steps: 804 * 805 * 1) copy the timestamp out of the PHY register 806 * 4) clear the timestamp valid bit in the PHY register 807 * 5) unlock the index by clearing the associated in_use bit. 808 * 2) extend the 40b timestamp value to get a 64bit timestamp 809 * 3) send that timestamp to the stack 810 * 811 * After looping, if we still have waiting SKBs, then re-queue the work. This 812 * may cause us effectively poll even when not strictly necessary. We do this 813 * because it's possible a new timestamp was requested around the same time as 814 * the interrupt. In some cases hardware might not interrupt us again when the 815 * timestamp is captured. 816 * 817 * Note that we only take the tracking lock when clearing the bit and when 818 * checking if we need to re-queue this task. The only place where bits can be 819 * set is the hard xmit routine where an SKB has a request flag set. The only 820 * places where we clear bits are this work function, or the periodic cleanup 821 * thread. If the cleanup thread clears a bit we're processing we catch it 822 * when we lock to clear the bit and then grab the SKB pointer. If a Tx thread 823 * starts a new timestamp, we might not begin processing it right away but we 824 * will notice it at the end when we re-queue the work item. If a Tx thread 825 * starts a new timestamp just after this function exits without re-queuing, 826 * the interrupt when the timestamp finishes should trigger. Avoiding holding 827 * the lock for the entire function is important in order to ensure that Tx 828 * threads do not get blocked while waiting for the lock. 829 */ 830 static void ice_ptp_tx_tstamp_work(struct kthread_work *work) 831 { 832 struct ice_ptp_port *ptp_port; 833 struct ice_ptp_tx *tx; 834 struct ice_pf *pf; 835 struct ice_hw *hw; 836 u8 idx; 837 838 tx = container_of(work, struct ice_ptp_tx, work); 839 if (!tx->init) 840 return; 841 842 ptp_port = container_of(tx, struct ice_ptp_port, tx); 843 pf = ptp_port_to_pf(ptp_port); 844 hw = &pf->hw; 845 846 for_each_set_bit(idx, tx->in_use, tx->len) { 847 struct skb_shared_hwtstamps shhwtstamps = {}; 848 u8 phy_idx = idx + tx->quad_offset; 849 u64 raw_tstamp, tstamp; 850 struct sk_buff *skb; 851 int err; 852 853 err = ice_read_phy_tstamp(hw, tx->quad, phy_idx, 854 &raw_tstamp); 855 if (err) 856 continue; 857 858 /* Check if the timestamp is valid */ 859 if (!(raw_tstamp & ICE_PTP_TS_VALID)) 860 continue; 861 862 /* clear the timestamp register, so that it won't show valid 863 * again when re-used. 864 */ 865 ice_clear_phy_tstamp(hw, tx->quad, phy_idx); 866 867 /* The timestamp is valid, so we'll go ahead and clear this 868 * index and then send the timestamp up to the stack. 869 */ 870 spin_lock(&tx->lock); 871 clear_bit(idx, tx->in_use); 872 skb = tx->tstamps[idx].skb; 873 tx->tstamps[idx].skb = NULL; 874 spin_unlock(&tx->lock); 875 876 /* it's (unlikely but) possible we raced with the cleanup 877 * thread for discarding old timestamp requests. 878 */ 879 if (!skb) 880 continue; 881 882 /* Extend the timestamp using cached PHC time */ 883 tstamp = ice_ptp_extend_40b_ts(pf, raw_tstamp); 884 shhwtstamps.hwtstamp = ns_to_ktime(tstamp); 885 886 skb_tstamp_tx(skb, &shhwtstamps); 887 dev_kfree_skb_any(skb); 888 } 889 890 /* Check if we still have work to do. If so, re-queue this task to 891 * poll for remaining timestamps. 892 */ 893 spin_lock(&tx->lock); 894 if (!bitmap_empty(tx->in_use, tx->len)) 895 kthread_queue_work(pf->ptp.kworker, &tx->work); 896 spin_unlock(&tx->lock); 897 } 898 899 /** 900 * ice_ptp_request_ts - Request an available Tx timestamp index 901 * @tx: the PTP Tx timestamp tracker to request from 902 * @skb: the SKB to associate with this timestamp request 903 */ 904 s8 ice_ptp_request_ts(struct ice_ptp_tx *tx, struct sk_buff *skb) 905 { 906 u8 idx; 907 908 /* Check if this tracker is initialized */ 909 if (!tx->init) 910 return -1; 911 912 spin_lock(&tx->lock); 913 /* Find and set the first available index */ 914 idx = find_first_zero_bit(tx->in_use, tx->len); 915 if (idx < tx->len) { 916 /* We got a valid index that no other thread could have set. Store 917 * a reference to the skb and the start time to allow discarding old 918 * requests. 919 */ 920 set_bit(idx, tx->in_use); 921 tx->tstamps[idx].start = jiffies; 922 tx->tstamps[idx].skb = skb_get(skb); 923 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 924 } 925 926 spin_unlock(&tx->lock); 927 928 /* return the appropriate PHY timestamp register index, -1 if no 929 * indexes were available. 930 */ 931 if (idx >= tx->len) 932 return -1; 933 else 934 return idx + tx->quad_offset; 935 } 936 937 /** 938 * ice_ptp_process_ts - Spawn kthread work to handle timestamps 939 * @pf: Board private structure 940 * 941 * Queue work required to process the PTP Tx timestamps outside of interrupt 942 * context. 943 */ 944 void ice_ptp_process_ts(struct ice_pf *pf) 945 { 946 if (pf->ptp.port.tx.init) 947 kthread_queue_work(pf->ptp.kworker, &pf->ptp.port.tx.work); 948 } 949 950 /** 951 * ice_ptp_alloc_tx_tracker - Initialize tracking for Tx timestamps 952 * @tx: Tx tracking structure to initialize 953 * 954 * Assumes that the length has already been initialized. Do not call directly, 955 * use the ice_ptp_init_tx_e822 or ice_ptp_init_tx_e810 instead. 956 */ 957 static int 958 ice_ptp_alloc_tx_tracker(struct ice_ptp_tx *tx) 959 { 960 tx->tstamps = kcalloc(tx->len, sizeof(*tx->tstamps), GFP_KERNEL); 961 if (!tx->tstamps) 962 return -ENOMEM; 963 964 tx->in_use = bitmap_zalloc(tx->len, GFP_KERNEL); 965 if (!tx->in_use) { 966 kfree(tx->tstamps); 967 tx->tstamps = NULL; 968 return -ENOMEM; 969 } 970 971 spin_lock_init(&tx->lock); 972 kthread_init_work(&tx->work, ice_ptp_tx_tstamp_work); 973 974 tx->init = 1; 975 976 return 0; 977 } 978 979 /** 980 * ice_ptp_flush_tx_tracker - Flush any remaining timestamps from the tracker 981 * @pf: Board private structure 982 * @tx: the tracker to flush 983 */ 984 static void 985 ice_ptp_flush_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 986 { 987 u8 idx; 988 989 for (idx = 0; idx < tx->len; idx++) { 990 u8 phy_idx = idx + tx->quad_offset; 991 992 /* Clear any potential residual timestamp in the PHY block */ 993 if (!pf->hw.reset_ongoing) 994 ice_clear_phy_tstamp(&pf->hw, tx->quad, phy_idx); 995 996 if (tx->tstamps[idx].skb) { 997 dev_kfree_skb_any(tx->tstamps[idx].skb); 998 tx->tstamps[idx].skb = NULL; 999 } 1000 } 1001 } 1002 1003 /** 1004 * ice_ptp_release_tx_tracker - Release allocated memory for Tx tracker 1005 * @pf: Board private structure 1006 * @tx: Tx tracking structure to release 1007 * 1008 * Free memory associated with the Tx timestamp tracker. 1009 */ 1010 static void 1011 ice_ptp_release_tx_tracker(struct ice_pf *pf, struct ice_ptp_tx *tx) 1012 { 1013 tx->init = 0; 1014 1015 kthread_cancel_work_sync(&tx->work); 1016 1017 ice_ptp_flush_tx_tracker(pf, tx); 1018 1019 kfree(tx->tstamps); 1020 tx->tstamps = NULL; 1021 1022 kfree(tx->in_use); 1023 tx->in_use = NULL; 1024 1025 tx->len = 0; 1026 } 1027 1028 /** 1029 * ice_ptp_init_tx_e810 - Initialize tracking for Tx timestamps 1030 * @pf: Board private structure 1031 * @tx: the Tx tracking structure to initialize 1032 * 1033 * Initialize the Tx timestamp tracker for this PF. For E810 devices, each 1034 * port has its own block of timestamps, independent of the other ports. 1035 */ 1036 static int 1037 ice_ptp_init_tx_e810(struct ice_pf *pf, struct ice_ptp_tx *tx) 1038 { 1039 tx->quad = pf->hw.port_info->lport; 1040 tx->quad_offset = 0; 1041 tx->len = INDEX_PER_QUAD; 1042 1043 return ice_ptp_alloc_tx_tracker(tx); 1044 } 1045 1046 /** 1047 * ice_ptp_tx_tstamp_cleanup - Cleanup old timestamp requests that got dropped 1048 * @tx: PTP Tx tracker to clean up 1049 * 1050 * Loop through the Tx timestamp requests and see if any of them have been 1051 * waiting for a long time. Discard any SKBs that have been waiting for more 1052 * than 2 seconds. This is long enough to be reasonably sure that the 1053 * timestamp will never be captured. This might happen if the packet gets 1054 * discarded before it reaches the PHY timestamping block. 1055 */ 1056 static void ice_ptp_tx_tstamp_cleanup(struct ice_ptp_tx *tx) 1057 { 1058 u8 idx; 1059 1060 if (!tx->init) 1061 return; 1062 1063 for_each_set_bit(idx, tx->in_use, tx->len) { 1064 struct sk_buff *skb; 1065 1066 /* Check if this SKB has been waiting for too long */ 1067 if (time_is_after_jiffies(tx->tstamps[idx].start + 2 * HZ)) 1068 continue; 1069 1070 spin_lock(&tx->lock); 1071 skb = tx->tstamps[idx].skb; 1072 tx->tstamps[idx].skb = NULL; 1073 clear_bit(idx, tx->in_use); 1074 spin_unlock(&tx->lock); 1075 1076 /* Free the SKB after we've cleared the bit */ 1077 dev_kfree_skb_any(skb); 1078 } 1079 } 1080 1081 static void ice_ptp_periodic_work(struct kthread_work *work) 1082 { 1083 struct ice_ptp *ptp = container_of(work, struct ice_ptp, work.work); 1084 struct ice_pf *pf = container_of(ptp, struct ice_pf, ptp); 1085 1086 if (!test_bit(ICE_FLAG_PTP, pf->flags)) 1087 return; 1088 1089 ice_ptp_update_cached_phctime(pf); 1090 1091 ice_ptp_tx_tstamp_cleanup(&pf->ptp.port.tx); 1092 1093 /* Run twice a second */ 1094 kthread_queue_delayed_work(ptp->kworker, &ptp->work, 1095 msecs_to_jiffies(500)); 1096 } 1097 1098 /** 1099 * ice_ptp_init_owner - Initialize PTP_1588_CLOCK device 1100 * @pf: Board private structure 1101 * 1102 * Setup and initialize a PTP clock device that represents the device hardware 1103 * clock. Save the clock index for other functions connected to the same 1104 * hardware resource. 1105 */ 1106 static int ice_ptp_init_owner(struct ice_pf *pf) 1107 { 1108 struct device *dev = ice_pf_to_dev(pf); 1109 struct ice_hw *hw = &pf->hw; 1110 struct timespec64 ts; 1111 u8 src_idx; 1112 int err; 1113 1114 wr32(hw, GLTSYN_SYNC_DLAY, 0); 1115 1116 /* Clear some HW residue and enable source clock */ 1117 src_idx = hw->func_caps.ts_func_info.tmr_index_owned; 1118 1119 /* Enable source clocks */ 1120 wr32(hw, GLTSYN_ENA(src_idx), GLTSYN_ENA_TSYN_ENA_M); 1121 1122 /* Enable PHY time sync */ 1123 err = ice_ptp_init_phy_e810(hw); 1124 if (err) 1125 goto err_exit; 1126 1127 /* Clear event status indications for auxiliary pins */ 1128 (void)rd32(hw, GLTSYN_STAT(src_idx)); 1129 1130 /* Acquire the global hardware lock */ 1131 if (!ice_ptp_lock(hw)) { 1132 err = -EBUSY; 1133 goto err_exit; 1134 } 1135 1136 /* Write the increment time value to PHY and LAN */ 1137 err = ice_ptp_write_incval(hw, ICE_PTP_NOMINAL_INCVAL_E810); 1138 if (err) { 1139 ice_ptp_unlock(hw); 1140 goto err_exit; 1141 } 1142 1143 ts = ktime_to_timespec64(ktime_get_real()); 1144 /* Write the initial Time value to PHY and LAN */ 1145 err = ice_ptp_write_init(pf, &ts); 1146 if (err) { 1147 ice_ptp_unlock(hw); 1148 goto err_exit; 1149 } 1150 1151 /* Release the global hardware lock */ 1152 ice_ptp_unlock(hw); 1153 1154 /* Ensure we have a clock device */ 1155 err = ice_ptp_create_clock(pf); 1156 if (err) 1157 goto err_clk; 1158 1159 /* Store the PTP clock index for other PFs */ 1160 ice_set_ptp_clock_index(pf); 1161 1162 return 0; 1163 1164 err_clk: 1165 pf->ptp.clock = NULL; 1166 err_exit: 1167 dev_err(dev, "PTP failed to register clock, err %d\n", err); 1168 1169 return err; 1170 } 1171 1172 /** 1173 * ice_ptp_init - Initialize the PTP support after device probe or reset 1174 * @pf: Board private structure 1175 * 1176 * This function sets device up for PTP support. The first time it is run, it 1177 * will create a clock device. It does not create a clock device if one 1178 * already exists. It also reconfigures the device after a reset. 1179 */ 1180 void ice_ptp_init(struct ice_pf *pf) 1181 { 1182 struct device *dev = ice_pf_to_dev(pf); 1183 struct kthread_worker *kworker; 1184 struct ice_hw *hw = &pf->hw; 1185 int err; 1186 1187 /* PTP is currently only supported on E810 devices */ 1188 if (!ice_is_e810(hw)) 1189 return; 1190 1191 /* Check if this PF owns the source timer */ 1192 if (hw->func_caps.ts_func_info.src_tmr_owned) { 1193 err = ice_ptp_init_owner(pf); 1194 if (err) 1195 return; 1196 } 1197 1198 /* Disable timestamping for both Tx and Rx */ 1199 ice_ptp_cfg_timestamp(pf, false); 1200 1201 /* Initialize the PTP port Tx timestamp tracker */ 1202 ice_ptp_init_tx_e810(pf, &pf->ptp.port.tx); 1203 1204 /* Initialize work functions */ 1205 kthread_init_delayed_work(&pf->ptp.work, ice_ptp_periodic_work); 1206 1207 /* Allocate a kworker for handling work required for the ports 1208 * connected to the PTP hardware clock. 1209 */ 1210 kworker = kthread_create_worker(0, "ice-ptp-%s", dev_name(dev)); 1211 if (IS_ERR(kworker)) { 1212 err = PTR_ERR(kworker); 1213 goto err_kworker; 1214 } 1215 pf->ptp.kworker = kworker; 1216 1217 set_bit(ICE_FLAG_PTP, pf->flags); 1218 1219 /* Start periodic work going */ 1220 kthread_queue_delayed_work(pf->ptp.kworker, &pf->ptp.work, 0); 1221 1222 dev_info(dev, "PTP init successful\n"); 1223 return; 1224 1225 err_kworker: 1226 /* If we registered a PTP clock, release it */ 1227 if (pf->ptp.clock) { 1228 ptp_clock_unregister(pf->ptp.clock); 1229 pf->ptp.clock = NULL; 1230 } 1231 dev_err(dev, "PTP failed %d\n", err); 1232 } 1233 1234 /** 1235 * ice_ptp_release - Disable the driver/HW support and unregister the clock 1236 * @pf: Board private structure 1237 * 1238 * This function handles the cleanup work required from the initialization by 1239 * clearing out the important information and unregistering the clock 1240 */ 1241 void ice_ptp_release(struct ice_pf *pf) 1242 { 1243 /* Disable timestamping for both Tx and Rx */ 1244 ice_ptp_cfg_timestamp(pf, false); 1245 1246 ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx); 1247 1248 clear_bit(ICE_FLAG_PTP, pf->flags); 1249 1250 kthread_cancel_delayed_work_sync(&pf->ptp.work); 1251 1252 if (pf->ptp.kworker) { 1253 kthread_destroy_worker(pf->ptp.kworker); 1254 pf->ptp.kworker = NULL; 1255 } 1256 1257 if (!pf->ptp.clock) 1258 return; 1259 1260 ice_clear_ptp_clock_index(pf); 1261 ptp_clock_unregister(pf->ptp.clock); 1262 pf->ptp.clock = NULL; 1263 1264 dev_info(ice_pf_to_dev(pf), "Removed PTP clock\n"); 1265 } 1266