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