1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 1999 - 2018 Intel Corporation. */ 3 4 #include "ixgbe.h" 5 #include <linux/ptp_classify.h> 6 #include <linux/clocksource.h> 7 8 /* 9 * The 82599 and the X540 do not have true 64bit nanosecond scale 10 * counter registers. Instead, SYSTIME is defined by a fixed point 11 * system which allows the user to define the scale counter increment 12 * value at every level change of the oscillator driving the SYSTIME 13 * value. For both devices the TIMINCA:IV field defines this 14 * increment. On the X540 device, 31 bits are provided. However on the 15 * 82599 only provides 24 bits. The time unit is determined by the 16 * clock frequency of the oscillator in combination with the TIMINCA 17 * register. When these devices link at 10Gb the oscillator has a 18 * period of 6.4ns. In order to convert the scale counter into 19 * nanoseconds the cyclecounter and timecounter structures are 20 * used. The SYSTIME registers need to be converted to ns values by use 21 * of only a right shift (division by power of 2). The following math 22 * determines the largest incvalue that will fit into the available 23 * bits in the TIMINCA register. 24 * 25 * PeriodWidth: Number of bits to store the clock period 26 * MaxWidth: The maximum width value of the TIMINCA register 27 * Period: The clock period for the oscillator 28 * round(): discard the fractional portion of the calculation 29 * 30 * Period * [ 2 ^ ( MaxWidth - PeriodWidth ) ] 31 * 32 * For the X540, MaxWidth is 31 bits, and the base period is 6.4 ns 33 * For the 82599, MaxWidth is 24 bits, and the base period is 6.4 ns 34 * 35 * The period also changes based on the link speed: 36 * At 10Gb link or no link, the period remains the same. 37 * At 1Gb link, the period is multiplied by 10. (64ns) 38 * At 100Mb link, the period is multiplied by 100. (640ns) 39 * 40 * The calculated value allows us to right shift the SYSTIME register 41 * value in order to quickly convert it into a nanosecond clock, 42 * while allowing for the maximum possible adjustment value. 43 * 44 * These diagrams are only for the 10Gb link period 45 * 46 * SYSTIMEH SYSTIMEL 47 * +--------------+ +--------------+ 48 * X540 | 32 | | 1 | 3 | 28 | 49 * *--------------+ +--------------+ 50 * \________ 36 bits ______/ fract 51 * 52 * +--------------+ +--------------+ 53 * 82599 | 32 | | 8 | 3 | 21 | 54 * *--------------+ +--------------+ 55 * \________ 43 bits ______/ fract 56 * 57 * The 36 bit X540 SYSTIME overflows every 58 * 2^36 * 10^-9 / 60 = 1.14 minutes or 69 seconds 59 * 60 * The 43 bit 82599 SYSTIME overflows every 61 * 2^43 * 10^-9 / 3600 = 2.4 hours 62 */ 63 #define IXGBE_INCVAL_10GB 0x66666666 64 #define IXGBE_INCVAL_1GB 0x40000000 65 #define IXGBE_INCVAL_100 0x50000000 66 67 #define IXGBE_INCVAL_SHIFT_10GB 28 68 #define IXGBE_INCVAL_SHIFT_1GB 24 69 #define IXGBE_INCVAL_SHIFT_100 21 70 71 #define IXGBE_INCVAL_SHIFT_82599 7 72 #define IXGBE_INCPER_SHIFT_82599 24 73 74 #define IXGBE_OVERFLOW_PERIOD (HZ * 30) 75 #define IXGBE_PTP_TX_TIMEOUT (HZ) 76 77 /* We use our own definitions instead of NSEC_PER_SEC because we want to mark 78 * the value as a ULL to force precision when bit shifting. 79 */ 80 #define NS_PER_SEC 1000000000ULL 81 #define NS_PER_HALF_SEC 500000000ULL 82 83 /* In contrast, the X550 controller has two registers, SYSTIMEH and SYSTIMEL 84 * which contain measurements of seconds and nanoseconds respectively. This 85 * matches the standard linux representation of time in the kernel. In addition, 86 * the X550 also has a SYSTIMER register which represents residue, or 87 * subnanosecond overflow adjustments. To control clock adjustment, the TIMINCA 88 * register is used, but it is unlike the X540 and 82599 devices. TIMINCA 89 * represents units of 2^-32 nanoseconds, and uses 31 bits for this, with the 90 * high bit representing whether the adjustent is positive or negative. Every 91 * clock cycle, the X550 will add 12.5 ns + TIMINCA which can result in a range 92 * of 12 to 13 nanoseconds adjustment. Unlike the 82599 and X540 devices, the 93 * X550's clock for purposes of SYSTIME generation is constant and not dependent 94 * on the link speed. 95 * 96 * SYSTIMEH SYSTIMEL SYSTIMER 97 * +--------------+ +--------------+ +-------------+ 98 * X550 | 32 | | 32 | | 32 | 99 * *--------------+ +--------------+ +-------------+ 100 * \____seconds___/ \_nanoseconds_/ \__2^-32 ns__/ 101 * 102 * This results in a full 96 bits to represent the clock, with 32 bits for 103 * seconds, 32 bits for nanoseconds (largest value is 0d999999999 or just under 104 * 1 second) and an additional 32 bits to measure sub nanosecond adjustments for 105 * underflow of adjustments. 106 * 107 * The 32 bits of seconds for the X550 overflows every 108 * 2^32 / ( 365.25 * 24 * 60 * 60 ) = ~136 years. 109 * 110 * In order to adjust the clock frequency for the X550, the TIMINCA register is 111 * provided. This register represents a + or minus nearly 0.5 ns adjustment to 112 * the base frequency. It is measured in 2^-32 ns units, with the high bit being 113 * the sign bit. This register enables software to calculate frequency 114 * adjustments and apply them directly to the clock rate. 115 * 116 * The math for converting ppb into TIMINCA values is fairly straightforward. 117 * TIMINCA value = ( Base_Frequency * ppb ) / 1000000000ULL 118 * 119 * This assumes that ppb is never high enough to create a value bigger than 120 * TIMINCA's 31 bits can store. This is ensured by the stack. Calculating this 121 * value is also simple. 122 * Max ppb = ( Max Adjustment / Base Frequency ) / 1000000000ULL 123 * 124 * For the X550, the Max adjustment is +/- 0.5 ns, and the base frequency is 125 * 12.5 nanoseconds. This means that the Max ppb is 39999999 126 * Note: We subtract one in order to ensure no overflow, because the TIMINCA 127 * register can only hold slightly under 0.5 nanoseconds. 128 * 129 * Because TIMINCA is measured in 2^-32 ns units, we have to convert 12.5 ns 130 * into 2^-32 units, which is 131 * 132 * 12.5 * 2^32 = C80000000 133 * 134 * Some revisions of hardware have a faster base frequency than the registers 135 * were defined for. To fix this, we use a timecounter structure with the 136 * proper mult and shift to convert the cycles into nanoseconds of time. 137 */ 138 #define IXGBE_X550_BASE_PERIOD 0xC80000000ULL 139 #define INCVALUE_MASK 0x7FFFFFFF 140 #define ISGN 0x80000000 141 #define MAX_TIMADJ 0x7FFFFFFF 142 143 /** 144 * ixgbe_ptp_setup_sdp_X540 145 * @adapter: private adapter structure 146 * 147 * this function enables or disables the clock out feature on SDP0 for 148 * the X540 device. It will create a 1 second periodic output that can 149 * be used as the PPS (via an interrupt). 150 * 151 * It calculates when the system time will be on an exact second, and then 152 * aligns the start of the PPS signal to that value. 153 * 154 * This works by using the cycle counter shift and mult values in reverse, and 155 * assumes that the values we're shifting will not overflow. 156 */ 157 static void ixgbe_ptp_setup_sdp_X540(struct ixgbe_adapter *adapter) 158 { 159 struct cyclecounter *cc = &adapter->hw_cc; 160 struct ixgbe_hw *hw = &adapter->hw; 161 u32 esdp, tsauxc, clktiml, clktimh, trgttiml, trgttimh, rem; 162 u64 ns = 0, clock_edge = 0, clock_period; 163 unsigned long flags; 164 165 /* disable the pin first */ 166 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0); 167 IXGBE_WRITE_FLUSH(hw); 168 169 if (!(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED)) 170 return; 171 172 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP); 173 174 /* enable the SDP0 pin as output, and connected to the 175 * native function for Timesync (ClockOut) 176 */ 177 esdp |= IXGBE_ESDP_SDP0_DIR | 178 IXGBE_ESDP_SDP0_NATIVE; 179 180 /* enable the Clock Out feature on SDP0, and allow 181 * interrupts to occur when the pin changes 182 */ 183 tsauxc = (IXGBE_TSAUXC_EN_CLK | 184 IXGBE_TSAUXC_SYNCLK | 185 IXGBE_TSAUXC_SDP0_INT); 186 187 /* Determine the clock time period to use. This assumes that the 188 * cycle counter shift is small enough to avoid overflow. 189 */ 190 clock_period = div_u64((NS_PER_HALF_SEC << cc->shift), cc->mult); 191 clktiml = (u32)(clock_period); 192 clktimh = (u32)(clock_period >> 32); 193 194 /* Read the current clock time, and save the cycle counter value */ 195 spin_lock_irqsave(&adapter->tmreg_lock, flags); 196 ns = timecounter_read(&adapter->hw_tc); 197 clock_edge = adapter->hw_tc.cycle_last; 198 spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 199 200 /* Figure out how many seconds to add in order to round up */ 201 div_u64_rem(ns, NS_PER_SEC, &rem); 202 203 /* Figure out how many nanoseconds to add to round the clock edge up 204 * to the next full second 205 */ 206 rem = (NS_PER_SEC - rem); 207 208 /* Adjust the clock edge to align with the next full second. This 209 * assumes that the cycle counter shift is small enough to avoid 210 * overflowing when shifting the remainder. 211 */ 212 clock_edge += div_u64((rem << cc->shift), cc->mult); 213 trgttiml = (u32)clock_edge; 214 trgttimh = (u32)(clock_edge >> 32); 215 216 IXGBE_WRITE_REG(hw, IXGBE_CLKTIML, clktiml); 217 IXGBE_WRITE_REG(hw, IXGBE_CLKTIMH, clktimh); 218 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml); 219 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh); 220 221 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp); 222 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc); 223 224 IXGBE_WRITE_FLUSH(hw); 225 } 226 227 /** 228 * ixgbe_ptp_setup_sdp_X550 229 * @adapter: private adapter structure 230 * 231 * Enable or disable a clock output signal on SDP 0 for X550 hardware. 232 * 233 * Use the target time feature to align the output signal on the next full 234 * second. 235 * 236 * This works by using the cycle counter shift and mult values in reverse, and 237 * assumes that the values we're shifting will not overflow. 238 */ 239 static void ixgbe_ptp_setup_sdp_X550(struct ixgbe_adapter *adapter) 240 { 241 u32 esdp, tsauxc, freqout, trgttiml, trgttimh, rem, tssdp; 242 struct cyclecounter *cc = &adapter->hw_cc; 243 struct ixgbe_hw *hw = &adapter->hw; 244 u64 ns = 0, clock_edge = 0; 245 struct timespec64 ts; 246 unsigned long flags; 247 248 /* disable the pin first */ 249 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 0x0); 250 IXGBE_WRITE_FLUSH(hw); 251 252 if (!(adapter->flags2 & IXGBE_FLAG2_PTP_PPS_ENABLED)) 253 return; 254 255 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP); 256 257 /* enable the SDP0 pin as output, and connected to the 258 * native function for Timesync (ClockOut) 259 */ 260 esdp |= IXGBE_ESDP_SDP0_DIR | 261 IXGBE_ESDP_SDP0_NATIVE; 262 263 /* enable the Clock Out feature on SDP0, and use Target Time 0 to 264 * enable generation of interrupts on the clock change. 265 */ 266 #define IXGBE_TSAUXC_DIS_TS_CLEAR 0x40000000 267 tsauxc = (IXGBE_TSAUXC_EN_CLK | IXGBE_TSAUXC_ST0 | 268 IXGBE_TSAUXC_EN_TT0 | IXGBE_TSAUXC_SDP0_INT | 269 IXGBE_TSAUXC_DIS_TS_CLEAR); 270 271 tssdp = (IXGBE_TSSDP_TS_SDP0_EN | 272 IXGBE_TSSDP_TS_SDP0_CLK0); 273 274 /* Determine the clock time period to use. This assumes that the 275 * cycle counter shift is small enough to avoid overflowing a 32bit 276 * value. 277 */ 278 freqout = div_u64(NS_PER_HALF_SEC << cc->shift, cc->mult); 279 280 /* Read the current clock time, and save the cycle counter value */ 281 spin_lock_irqsave(&adapter->tmreg_lock, flags); 282 ns = timecounter_read(&adapter->hw_tc); 283 clock_edge = adapter->hw_tc.cycle_last; 284 spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 285 286 /* Figure out how far past the next second we are */ 287 div_u64_rem(ns, NS_PER_SEC, &rem); 288 289 /* Figure out how many nanoseconds to add to round the clock edge up 290 * to the next full second 291 */ 292 rem = (NS_PER_SEC - rem); 293 294 /* Adjust the clock edge to align with the next full second. This 295 * assumes that the cycle counter shift is small enough to avoid 296 * overflowing when shifting the remainder. 297 */ 298 clock_edge += div_u64((rem << cc->shift), cc->mult); 299 300 /* X550 hardware stores the time in 32bits of 'billions of cycles' and 301 * 32bits of 'cycles'. There's no guarantee that cycles represents 302 * nanoseconds. However, we can use the math from a timespec64 to 303 * convert into the hardware representation. 304 * 305 * See ixgbe_ptp_read_X550() for more details. 306 */ 307 ts = ns_to_timespec64(clock_edge); 308 trgttiml = (u32)ts.tv_nsec; 309 trgttimh = (u32)ts.tv_sec; 310 311 IXGBE_WRITE_REG(hw, IXGBE_FREQOUT0, freqout); 312 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIML0, trgttiml); 313 IXGBE_WRITE_REG(hw, IXGBE_TRGTTIMH0, trgttimh); 314 315 IXGBE_WRITE_REG(hw, IXGBE_ESDP, esdp); 316 IXGBE_WRITE_REG(hw, IXGBE_TSSDP, tssdp); 317 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc); 318 319 IXGBE_WRITE_FLUSH(hw); 320 } 321 322 /** 323 * ixgbe_ptp_read_X550 - read cycle counter value 324 * @cc: cyclecounter structure 325 * 326 * This function reads SYSTIME registers. It is called by the cyclecounter 327 * structure to convert from internal representation into nanoseconds. We need 328 * this for X550 since some skews do not have expected clock frequency and 329 * result of SYSTIME is 32bits of "billions of cycles" and 32 bits of 330 * "cycles", rather than seconds and nanoseconds. 331 */ 332 static u64 ixgbe_ptp_read_X550(const struct cyclecounter *cc) 333 { 334 struct ixgbe_adapter *adapter = 335 container_of(cc, struct ixgbe_adapter, hw_cc); 336 struct ixgbe_hw *hw = &adapter->hw; 337 struct timespec64 ts; 338 339 /* storage is 32 bits of 'billions of cycles' and 32 bits of 'cycles'. 340 * Some revisions of hardware run at a higher frequency and so the 341 * cycles are not guaranteed to be nanoseconds. The timespec64 created 342 * here is used for its math/conversions but does not necessarily 343 * represent nominal time. 344 * 345 * It should be noted that this cyclecounter will overflow at a 346 * non-bitmask field since we have to convert our billions of cycles 347 * into an actual cycles count. This results in some possible weird 348 * situations at high cycle counter stamps. However given that 32 bits 349 * of "seconds" is ~138 years this isn't a problem. Even at the 350 * increased frequency of some revisions, this is still ~103 years. 351 * Since the SYSTIME values start at 0 and we never write them, it is 352 * highly unlikely for the cyclecounter to overflow in practice. 353 */ 354 IXGBE_READ_REG(hw, IXGBE_SYSTIMR); 355 ts.tv_nsec = IXGBE_READ_REG(hw, IXGBE_SYSTIML); 356 ts.tv_sec = IXGBE_READ_REG(hw, IXGBE_SYSTIMH); 357 358 return (u64)timespec64_to_ns(&ts); 359 } 360 361 /** 362 * ixgbe_ptp_read_82599 - read raw cycle counter (to be used by time counter) 363 * @cc: the cyclecounter structure 364 * 365 * this function reads the cyclecounter registers and is called by the 366 * cyclecounter structure used to construct a ns counter from the 367 * arbitrary fixed point registers 368 */ 369 static u64 ixgbe_ptp_read_82599(const struct cyclecounter *cc) 370 { 371 struct ixgbe_adapter *adapter = 372 container_of(cc, struct ixgbe_adapter, hw_cc); 373 struct ixgbe_hw *hw = &adapter->hw; 374 u64 stamp = 0; 375 376 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIML); 377 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32; 378 379 return stamp; 380 } 381 382 /** 383 * ixgbe_ptp_convert_to_hwtstamp - convert register value to hw timestamp 384 * @adapter: private adapter structure 385 * @hwtstamp: stack timestamp structure 386 * @timestamp: unsigned 64bit system time value 387 * 388 * We need to convert the adapter's RX/TXSTMP registers into a hwtstamp value 389 * which can be used by the stack's ptp functions. 390 * 391 * The lock is used to protect consistency of the cyclecounter and the SYSTIME 392 * registers. However, it does not need to protect against the Rx or Tx 393 * timestamp registers, as there can't be a new timestamp until the old one is 394 * unlatched by reading. 395 * 396 * In addition to the timestamp in hardware, some controllers need a software 397 * overflow cyclecounter, and this function takes this into account as well. 398 **/ 399 static void ixgbe_ptp_convert_to_hwtstamp(struct ixgbe_adapter *adapter, 400 struct skb_shared_hwtstamps *hwtstamp, 401 u64 timestamp) 402 { 403 unsigned long flags; 404 struct timespec64 systime; 405 u64 ns; 406 407 memset(hwtstamp, 0, sizeof(*hwtstamp)); 408 409 switch (adapter->hw.mac.type) { 410 /* X550 and later hardware supposedly represent time using a seconds 411 * and nanoseconds counter, instead of raw 64bits nanoseconds. We need 412 * to convert the timestamp into cycles before it can be fed to the 413 * cyclecounter. We need an actual cyclecounter because some revisions 414 * of hardware run at a higher frequency and thus the counter does 415 * not represent seconds/nanoseconds. Instead it can be thought of as 416 * cycles and billions of cycles. 417 */ 418 case ixgbe_mac_X550: 419 case ixgbe_mac_X550EM_x: 420 case ixgbe_mac_x550em_a: 421 /* Upper 32 bits represent billions of cycles, lower 32 bits 422 * represent cycles. However, we use timespec64_to_ns for the 423 * correct math even though the units haven't been corrected 424 * yet. 425 */ 426 systime.tv_sec = timestamp >> 32; 427 systime.tv_nsec = timestamp & 0xFFFFFFFF; 428 429 timestamp = timespec64_to_ns(&systime); 430 break; 431 default: 432 break; 433 } 434 435 spin_lock_irqsave(&adapter->tmreg_lock, flags); 436 ns = timecounter_cyc2time(&adapter->hw_tc, timestamp); 437 spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 438 439 hwtstamp->hwtstamp = ns_to_ktime(ns); 440 } 441 442 /** 443 * ixgbe_ptp_adjfreq_82599 444 * @ptp: the ptp clock structure 445 * @ppb: parts per billion adjustment from base 446 * 447 * adjust the frequency of the ptp cycle counter by the 448 * indicated ppb from the base frequency. 449 */ 450 static int ixgbe_ptp_adjfreq_82599(struct ptp_clock_info *ptp, s32 ppb) 451 { 452 struct ixgbe_adapter *adapter = 453 container_of(ptp, struct ixgbe_adapter, ptp_caps); 454 struct ixgbe_hw *hw = &adapter->hw; 455 u64 freq, incval; 456 u32 diff; 457 int neg_adj = 0; 458 459 if (ppb < 0) { 460 neg_adj = 1; 461 ppb = -ppb; 462 } 463 464 smp_mb(); 465 incval = READ_ONCE(adapter->base_incval); 466 467 freq = incval; 468 freq *= ppb; 469 diff = div_u64(freq, 1000000000ULL); 470 471 incval = neg_adj ? (incval - diff) : (incval + diff); 472 473 switch (hw->mac.type) { 474 case ixgbe_mac_X540: 475 if (incval > 0xFFFFFFFFULL) 476 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n"); 477 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, (u32)incval); 478 break; 479 case ixgbe_mac_82599EB: 480 if (incval > 0x00FFFFFFULL) 481 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n"); 482 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, 483 BIT(IXGBE_INCPER_SHIFT_82599) | 484 ((u32)incval & 0x00FFFFFFUL)); 485 break; 486 default: 487 break; 488 } 489 490 return 0; 491 } 492 493 /** 494 * ixgbe_ptp_adjfreq_X550 495 * @ptp: the ptp clock structure 496 * @ppb: parts per billion adjustment from base 497 * 498 * adjust the frequency of the SYSTIME registers by the indicated ppb from base 499 * frequency 500 */ 501 static int ixgbe_ptp_adjfreq_X550(struct ptp_clock_info *ptp, s32 ppb) 502 { 503 struct ixgbe_adapter *adapter = 504 container_of(ptp, struct ixgbe_adapter, ptp_caps); 505 struct ixgbe_hw *hw = &adapter->hw; 506 int neg_adj = 0; 507 u64 rate = IXGBE_X550_BASE_PERIOD; 508 u32 inca; 509 510 if (ppb < 0) { 511 neg_adj = 1; 512 ppb = -ppb; 513 } 514 rate *= ppb; 515 rate = div_u64(rate, 1000000000ULL); 516 517 /* warn if rate is too large */ 518 if (rate >= INCVALUE_MASK) 519 e_dev_warn("PTP ppb adjusted SYSTIME rate overflowed!\n"); 520 521 inca = rate & INCVALUE_MASK; 522 if (neg_adj) 523 inca |= ISGN; 524 525 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, inca); 526 527 return 0; 528 } 529 530 /** 531 * ixgbe_ptp_adjtime 532 * @ptp: the ptp clock structure 533 * @delta: offset to adjust the cycle counter by 534 * 535 * adjust the timer by resetting the timecounter structure. 536 */ 537 static int ixgbe_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) 538 { 539 struct ixgbe_adapter *adapter = 540 container_of(ptp, struct ixgbe_adapter, ptp_caps); 541 unsigned long flags; 542 543 spin_lock_irqsave(&adapter->tmreg_lock, flags); 544 timecounter_adjtime(&adapter->hw_tc, delta); 545 spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 546 547 if (adapter->ptp_setup_sdp) 548 adapter->ptp_setup_sdp(adapter); 549 550 return 0; 551 } 552 553 /** 554 * ixgbe_ptp_gettimex 555 * @ptp: the ptp clock structure 556 * @ts: timespec to hold the PHC timestamp 557 * @sts: structure to hold the system time before and after reading the PHC 558 * 559 * read the timecounter and return the correct value on ns, 560 * after converting it into a struct timespec. 561 */ 562 static int ixgbe_ptp_gettimex(struct ptp_clock_info *ptp, 563 struct timespec64 *ts, 564 struct ptp_system_timestamp *sts) 565 { 566 struct ixgbe_adapter *adapter = 567 container_of(ptp, struct ixgbe_adapter, ptp_caps); 568 struct ixgbe_hw *hw = &adapter->hw; 569 unsigned long flags; 570 u64 ns, stamp; 571 572 spin_lock_irqsave(&adapter->tmreg_lock, flags); 573 574 switch (adapter->hw.mac.type) { 575 case ixgbe_mac_X550: 576 case ixgbe_mac_X550EM_x: 577 case ixgbe_mac_x550em_a: 578 /* Upper 32 bits represent billions of cycles, lower 32 bits 579 * represent cycles. However, we use timespec64_to_ns for the 580 * correct math even though the units haven't been corrected 581 * yet. 582 */ 583 ptp_read_system_prets(sts); 584 IXGBE_READ_REG(hw, IXGBE_SYSTIMR); 585 ptp_read_system_postts(sts); 586 ts->tv_nsec = IXGBE_READ_REG(hw, IXGBE_SYSTIML); 587 ts->tv_sec = IXGBE_READ_REG(hw, IXGBE_SYSTIMH); 588 stamp = timespec64_to_ns(ts); 589 break; 590 default: 591 ptp_read_system_prets(sts); 592 stamp = IXGBE_READ_REG(hw, IXGBE_SYSTIML); 593 ptp_read_system_postts(sts); 594 stamp |= (u64)IXGBE_READ_REG(hw, IXGBE_SYSTIMH) << 32; 595 break; 596 } 597 598 ns = timecounter_cyc2time(&adapter->hw_tc, stamp); 599 600 spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 601 602 *ts = ns_to_timespec64(ns); 603 604 return 0; 605 } 606 607 /** 608 * ixgbe_ptp_settime 609 * @ptp: the ptp clock structure 610 * @ts: the timespec containing the new time for the cycle counter 611 * 612 * reset the timecounter to use a new base value instead of the kernel 613 * wall timer value. 614 */ 615 static int ixgbe_ptp_settime(struct ptp_clock_info *ptp, 616 const struct timespec64 *ts) 617 { 618 struct ixgbe_adapter *adapter = 619 container_of(ptp, struct ixgbe_adapter, ptp_caps); 620 unsigned long flags; 621 u64 ns = timespec64_to_ns(ts); 622 623 /* reset the timecounter */ 624 spin_lock_irqsave(&adapter->tmreg_lock, flags); 625 timecounter_init(&adapter->hw_tc, &adapter->hw_cc, ns); 626 spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 627 628 if (adapter->ptp_setup_sdp) 629 adapter->ptp_setup_sdp(adapter); 630 return 0; 631 } 632 633 /** 634 * ixgbe_ptp_feature_enable 635 * @ptp: the ptp clock structure 636 * @rq: the requested feature to change 637 * @on: whether to enable or disable the feature 638 * 639 * enable (or disable) ancillary features of the phc subsystem. 640 * our driver only supports the PPS feature on the X540 641 */ 642 static int ixgbe_ptp_feature_enable(struct ptp_clock_info *ptp, 643 struct ptp_clock_request *rq, int on) 644 { 645 struct ixgbe_adapter *adapter = 646 container_of(ptp, struct ixgbe_adapter, ptp_caps); 647 648 /** 649 * When PPS is enabled, unmask the interrupt for the ClockOut 650 * feature, so that the interrupt handler can send the PPS 651 * event when the clock SDP triggers. Clear mask when PPS is 652 * disabled 653 */ 654 if (rq->type != PTP_CLK_REQ_PPS || !adapter->ptp_setup_sdp) 655 return -ENOTSUPP; 656 657 if (on) 658 adapter->flags2 |= IXGBE_FLAG2_PTP_PPS_ENABLED; 659 else 660 adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED; 661 662 adapter->ptp_setup_sdp(adapter); 663 return 0; 664 } 665 666 /** 667 * ixgbe_ptp_check_pps_event 668 * @adapter: the private adapter structure 669 * 670 * This function is called by the interrupt routine when checking for 671 * interrupts. It will check and handle a pps event. 672 */ 673 void ixgbe_ptp_check_pps_event(struct ixgbe_adapter *adapter) 674 { 675 struct ixgbe_hw *hw = &adapter->hw; 676 struct ptp_clock_event event; 677 678 event.type = PTP_CLOCK_PPS; 679 680 /* this check is necessary in case the interrupt was enabled via some 681 * alternative means (ex. debug_fs). Better to check here than 682 * everywhere that calls this function. 683 */ 684 if (!adapter->ptp_clock) 685 return; 686 687 switch (hw->mac.type) { 688 case ixgbe_mac_X540: 689 ptp_clock_event(adapter->ptp_clock, &event); 690 break; 691 default: 692 break; 693 } 694 } 695 696 /** 697 * ixgbe_ptp_overflow_check - watchdog task to detect SYSTIME overflow 698 * @adapter: private adapter struct 699 * 700 * this watchdog task periodically reads the timecounter 701 * in order to prevent missing when the system time registers wrap 702 * around. This needs to be run approximately twice a minute. 703 */ 704 void ixgbe_ptp_overflow_check(struct ixgbe_adapter *adapter) 705 { 706 bool timeout = time_is_before_jiffies(adapter->last_overflow_check + 707 IXGBE_OVERFLOW_PERIOD); 708 unsigned long flags; 709 710 if (timeout) { 711 /* Update the timecounter */ 712 spin_lock_irqsave(&adapter->tmreg_lock, flags); 713 timecounter_read(&adapter->hw_tc); 714 spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 715 716 adapter->last_overflow_check = jiffies; 717 } 718 } 719 720 /** 721 * ixgbe_ptp_rx_hang - detect error case when Rx timestamp registers latched 722 * @adapter: private network adapter structure 723 * 724 * this watchdog task is scheduled to detect error case where hardware has 725 * dropped an Rx packet that was timestamped when the ring is full. The 726 * particular error is rare but leaves the device in a state unable to timestamp 727 * any future packets. 728 */ 729 void ixgbe_ptp_rx_hang(struct ixgbe_adapter *adapter) 730 { 731 struct ixgbe_hw *hw = &adapter->hw; 732 u32 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL); 733 struct ixgbe_ring *rx_ring; 734 unsigned long rx_event; 735 int n; 736 737 /* if we don't have a valid timestamp in the registers, just update the 738 * timeout counter and exit 739 */ 740 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) { 741 adapter->last_rx_ptp_check = jiffies; 742 return; 743 } 744 745 /* determine the most recent watchdog or rx_timestamp event */ 746 rx_event = adapter->last_rx_ptp_check; 747 for (n = 0; n < adapter->num_rx_queues; n++) { 748 rx_ring = adapter->rx_ring[n]; 749 if (time_after(rx_ring->last_rx_timestamp, rx_event)) 750 rx_event = rx_ring->last_rx_timestamp; 751 } 752 753 /* only need to read the high RXSTMP register to clear the lock */ 754 if (time_is_before_jiffies(rx_event + 5 * HZ)) { 755 IXGBE_READ_REG(hw, IXGBE_RXSTMPH); 756 adapter->last_rx_ptp_check = jiffies; 757 758 adapter->rx_hwtstamp_cleared++; 759 e_warn(drv, "clearing RX Timestamp hang\n"); 760 } 761 } 762 763 /** 764 * ixgbe_ptp_clear_tx_timestamp - utility function to clear Tx timestamp state 765 * @adapter: the private adapter structure 766 * 767 * This function should be called whenever the state related to a Tx timestamp 768 * needs to be cleared. This helps ensure that all related bits are reset for 769 * the next Tx timestamp event. 770 */ 771 static void ixgbe_ptp_clear_tx_timestamp(struct ixgbe_adapter *adapter) 772 { 773 struct ixgbe_hw *hw = &adapter->hw; 774 775 IXGBE_READ_REG(hw, IXGBE_TXSTMPH); 776 if (adapter->ptp_tx_skb) { 777 dev_kfree_skb_any(adapter->ptp_tx_skb); 778 adapter->ptp_tx_skb = NULL; 779 } 780 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state); 781 } 782 783 /** 784 * ixgbe_ptp_tx_hang - detect error case where Tx timestamp never finishes 785 * @adapter: private network adapter structure 786 */ 787 void ixgbe_ptp_tx_hang(struct ixgbe_adapter *adapter) 788 { 789 bool timeout = time_is_before_jiffies(adapter->ptp_tx_start + 790 IXGBE_PTP_TX_TIMEOUT); 791 792 if (!adapter->ptp_tx_skb) 793 return; 794 795 if (!test_bit(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state)) 796 return; 797 798 /* If we haven't received a timestamp within the timeout, it is 799 * reasonable to assume that it will never occur, so we can unlock the 800 * timestamp bit when this occurs. 801 */ 802 if (timeout) { 803 cancel_work_sync(&adapter->ptp_tx_work); 804 ixgbe_ptp_clear_tx_timestamp(adapter); 805 adapter->tx_hwtstamp_timeouts++; 806 e_warn(drv, "clearing Tx timestamp hang\n"); 807 } 808 } 809 810 /** 811 * ixgbe_ptp_tx_hwtstamp - utility function which checks for TX time stamp 812 * @adapter: the private adapter struct 813 * 814 * if the timestamp is valid, we convert it into the timecounter ns 815 * value, then store that result into the shhwtstamps structure which 816 * is passed up the network stack 817 */ 818 static void ixgbe_ptp_tx_hwtstamp(struct ixgbe_adapter *adapter) 819 { 820 struct sk_buff *skb = adapter->ptp_tx_skb; 821 struct ixgbe_hw *hw = &adapter->hw; 822 struct skb_shared_hwtstamps shhwtstamps; 823 u64 regval = 0; 824 825 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPL); 826 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_TXSTMPH) << 32; 827 ixgbe_ptp_convert_to_hwtstamp(adapter, &shhwtstamps, regval); 828 829 /* Handle cleanup of the ptp_tx_skb ourselves, and unlock the state 830 * bit prior to notifying the stack via skb_tstamp_tx(). This prevents 831 * well behaved applications from attempting to timestamp again prior 832 * to the lock bit being clear. 833 */ 834 adapter->ptp_tx_skb = NULL; 835 clear_bit_unlock(__IXGBE_PTP_TX_IN_PROGRESS, &adapter->state); 836 837 /* Notify the stack and then free the skb after we've unlocked */ 838 skb_tstamp_tx(skb, &shhwtstamps); 839 dev_kfree_skb_any(skb); 840 } 841 842 /** 843 * ixgbe_ptp_tx_hwtstamp_work 844 * @work: pointer to the work struct 845 * 846 * This work item polls TSYNCTXCTL valid bit to determine when a Tx hardware 847 * timestamp has been taken for the current skb. It is necessary, because the 848 * descriptor's "done" bit does not correlate with the timestamp event. 849 */ 850 static void ixgbe_ptp_tx_hwtstamp_work(struct work_struct *work) 851 { 852 struct ixgbe_adapter *adapter = container_of(work, struct ixgbe_adapter, 853 ptp_tx_work); 854 struct ixgbe_hw *hw = &adapter->hw; 855 bool timeout = time_is_before_jiffies(adapter->ptp_tx_start + 856 IXGBE_PTP_TX_TIMEOUT); 857 u32 tsynctxctl; 858 859 /* we have to have a valid skb to poll for a timestamp */ 860 if (!adapter->ptp_tx_skb) { 861 ixgbe_ptp_clear_tx_timestamp(adapter); 862 return; 863 } 864 865 /* stop polling once we have a valid timestamp */ 866 tsynctxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL); 867 if (tsynctxctl & IXGBE_TSYNCTXCTL_VALID) { 868 ixgbe_ptp_tx_hwtstamp(adapter); 869 return; 870 } 871 872 if (timeout) { 873 ixgbe_ptp_clear_tx_timestamp(adapter); 874 adapter->tx_hwtstamp_timeouts++; 875 e_warn(drv, "clearing Tx Timestamp hang\n"); 876 } else { 877 /* reschedule to keep checking if it's not available yet */ 878 schedule_work(&adapter->ptp_tx_work); 879 } 880 } 881 882 /** 883 * ixgbe_ptp_rx_pktstamp - utility function to get RX time stamp from buffer 884 * @q_vector: structure containing interrupt and ring information 885 * @skb: the packet 886 * 887 * This function will be called by the Rx routine of the timestamp for this 888 * packet is stored in the buffer. The value is stored in little endian format 889 * starting at the end of the packet data. 890 */ 891 void ixgbe_ptp_rx_pktstamp(struct ixgbe_q_vector *q_vector, 892 struct sk_buff *skb) 893 { 894 __le64 regval; 895 896 /* copy the bits out of the skb, and then trim the skb length */ 897 skb_copy_bits(skb, skb->len - IXGBE_TS_HDR_LEN, ®val, 898 IXGBE_TS_HDR_LEN); 899 __pskb_trim(skb, skb->len - IXGBE_TS_HDR_LEN); 900 901 /* The timestamp is recorded in little endian format, and is stored at 902 * the end of the packet. 903 * 904 * DWORD: N N + 1 N + 2 905 * Field: End of Packet SYSTIMH SYSTIML 906 */ 907 ixgbe_ptp_convert_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb), 908 le64_to_cpu(regval)); 909 } 910 911 /** 912 * ixgbe_ptp_rx_rgtstamp - utility function which checks for RX time stamp 913 * @q_vector: structure containing interrupt and ring information 914 * @skb: particular skb to send timestamp with 915 * 916 * if the timestamp is valid, we convert it into the timecounter ns 917 * value, then store that result into the shhwtstamps structure which 918 * is passed up the network stack 919 */ 920 void ixgbe_ptp_rx_rgtstamp(struct ixgbe_q_vector *q_vector, 921 struct sk_buff *skb) 922 { 923 struct ixgbe_adapter *adapter; 924 struct ixgbe_hw *hw; 925 u64 regval = 0; 926 u32 tsyncrxctl; 927 928 /* we cannot process timestamps on a ring without a q_vector */ 929 if (!q_vector || !q_vector->adapter) 930 return; 931 932 adapter = q_vector->adapter; 933 hw = &adapter->hw; 934 935 /* Read the tsyncrxctl register afterwards in order to prevent taking an 936 * I/O hit on every packet. 937 */ 938 939 tsyncrxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL); 940 if (!(tsyncrxctl & IXGBE_TSYNCRXCTL_VALID)) 941 return; 942 943 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPL); 944 regval |= (u64)IXGBE_READ_REG(hw, IXGBE_RXSTMPH) << 32; 945 946 ixgbe_ptp_convert_to_hwtstamp(adapter, skb_hwtstamps(skb), regval); 947 } 948 949 /** 950 * ixgbe_ptp_get_ts_config - get current hardware timestamping configuration 951 * @adapter: pointer to adapter structure 952 * @ifr: ioctl data 953 * 954 * This function returns the current timestamping settings. Rather than 955 * attempt to deconstruct registers to fill in the values, simply keep a copy 956 * of the old settings around, and return a copy when requested. 957 */ 958 int ixgbe_ptp_get_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr) 959 { 960 struct hwtstamp_config *config = &adapter->tstamp_config; 961 962 return copy_to_user(ifr->ifr_data, config, 963 sizeof(*config)) ? -EFAULT : 0; 964 } 965 966 /** 967 * ixgbe_ptp_set_timestamp_mode - setup the hardware for the requested mode 968 * @adapter: the private ixgbe adapter structure 969 * @config: the hwtstamp configuration requested 970 * 971 * Outgoing time stamping can be enabled and disabled. Play nice and 972 * disable it when requested, although it shouldn't cause any overhead 973 * when no packet needs it. At most one packet in the queue may be 974 * marked for time stamping, otherwise it would be impossible to tell 975 * for sure to which packet the hardware time stamp belongs. 976 * 977 * Incoming time stamping has to be configured via the hardware 978 * filters. Not all combinations are supported, in particular event 979 * type has to be specified. Matching the kind of event packet is 980 * not supported, with the exception of "all V2 events regardless of 981 * level 2 or 4". 982 * 983 * Since hardware always timestamps Path delay packets when timestamping V2 984 * packets, regardless of the type specified in the register, only use V2 985 * Event mode. This more accurately tells the user what the hardware is going 986 * to do anyways. 987 * 988 * Note: this may modify the hwtstamp configuration towards a more general 989 * mode, if required to support the specifically requested mode. 990 */ 991 static int ixgbe_ptp_set_timestamp_mode(struct ixgbe_adapter *adapter, 992 struct hwtstamp_config *config) 993 { 994 struct ixgbe_hw *hw = &adapter->hw; 995 u32 tsync_tx_ctl = IXGBE_TSYNCTXCTL_ENABLED; 996 u32 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED; 997 u32 tsync_rx_mtrl = PTP_EV_PORT << 16; 998 bool is_l2 = false; 999 u32 regval; 1000 1001 /* reserved for future extensions */ 1002 if (config->flags) 1003 return -EINVAL; 1004 1005 switch (config->tx_type) { 1006 case HWTSTAMP_TX_OFF: 1007 tsync_tx_ctl = 0; 1008 case HWTSTAMP_TX_ON: 1009 break; 1010 default: 1011 return -ERANGE; 1012 } 1013 1014 switch (config->rx_filter) { 1015 case HWTSTAMP_FILTER_NONE: 1016 tsync_rx_ctl = 0; 1017 tsync_rx_mtrl = 0; 1018 adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED | 1019 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER); 1020 break; 1021 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 1022 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1; 1023 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_SYNC_MSG; 1024 adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED | 1025 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER); 1026 break; 1027 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 1028 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_L4_V1; 1029 tsync_rx_mtrl |= IXGBE_RXMTRL_V1_DELAY_REQ_MSG; 1030 adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED | 1031 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER); 1032 break; 1033 case HWTSTAMP_FILTER_PTP_V2_EVENT: 1034 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 1035 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 1036 case HWTSTAMP_FILTER_PTP_V2_SYNC: 1037 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 1038 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 1039 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 1040 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 1041 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 1042 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_EVENT_V2; 1043 is_l2 = true; 1044 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 1045 adapter->flags |= (IXGBE_FLAG_RX_HWTSTAMP_ENABLED | 1046 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER); 1047 break; 1048 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 1049 case HWTSTAMP_FILTER_NTP_ALL: 1050 case HWTSTAMP_FILTER_ALL: 1051 /* The X550 controller is capable of timestamping all packets, 1052 * which allows it to accept any filter. 1053 */ 1054 if (hw->mac.type >= ixgbe_mac_X550) { 1055 tsync_rx_ctl |= IXGBE_TSYNCRXCTL_TYPE_ALL; 1056 config->rx_filter = HWTSTAMP_FILTER_ALL; 1057 adapter->flags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED; 1058 break; 1059 } 1060 /* fall through */ 1061 default: 1062 /* 1063 * register RXMTRL must be set in order to do V1 packets, 1064 * therefore it is not possible to time stamp both V1 Sync and 1065 * Delay_Req messages and hardware does not support 1066 * timestamping all packets => return error 1067 */ 1068 adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED | 1069 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER); 1070 config->rx_filter = HWTSTAMP_FILTER_NONE; 1071 return -ERANGE; 1072 } 1073 1074 if (hw->mac.type == ixgbe_mac_82598EB) { 1075 adapter->flags &= ~(IXGBE_FLAG_RX_HWTSTAMP_ENABLED | 1076 IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER); 1077 if (tsync_rx_ctl | tsync_tx_ctl) 1078 return -ERANGE; 1079 return 0; 1080 } 1081 1082 /* Per-packet timestamping only works if the filter is set to all 1083 * packets. Since this is desired, always timestamp all packets as long 1084 * as any Rx filter was configured. 1085 */ 1086 switch (hw->mac.type) { 1087 case ixgbe_mac_X550: 1088 case ixgbe_mac_X550EM_x: 1089 case ixgbe_mac_x550em_a: 1090 /* enable timestamping all packets only if at least some 1091 * packets were requested. Otherwise, play nice and disable 1092 * timestamping 1093 */ 1094 if (config->rx_filter == HWTSTAMP_FILTER_NONE) 1095 break; 1096 1097 tsync_rx_ctl = IXGBE_TSYNCRXCTL_ENABLED | 1098 IXGBE_TSYNCRXCTL_TYPE_ALL | 1099 IXGBE_TSYNCRXCTL_TSIP_UT_EN; 1100 config->rx_filter = HWTSTAMP_FILTER_ALL; 1101 adapter->flags |= IXGBE_FLAG_RX_HWTSTAMP_ENABLED; 1102 adapter->flags &= ~IXGBE_FLAG_RX_HWTSTAMP_IN_REGISTER; 1103 is_l2 = true; 1104 break; 1105 default: 1106 break; 1107 } 1108 1109 /* define ethertype filter for timestamping L2 packets */ 1110 if (is_l2) 1111 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 1112 (IXGBE_ETQF_FILTER_EN | /* enable filter */ 1113 IXGBE_ETQF_1588 | /* enable timestamping */ 1114 ETH_P_1588)); /* 1588 eth protocol type */ 1115 else 1116 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0); 1117 1118 /* enable/disable TX */ 1119 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL); 1120 regval &= ~IXGBE_TSYNCTXCTL_ENABLED; 1121 regval |= tsync_tx_ctl; 1122 IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, regval); 1123 1124 /* enable/disable RX */ 1125 regval = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL); 1126 regval &= ~(IXGBE_TSYNCRXCTL_ENABLED | IXGBE_TSYNCRXCTL_TYPE_MASK); 1127 regval |= tsync_rx_ctl; 1128 IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, regval); 1129 1130 /* define which PTP packets are time stamped */ 1131 IXGBE_WRITE_REG(hw, IXGBE_RXMTRL, tsync_rx_mtrl); 1132 1133 IXGBE_WRITE_FLUSH(hw); 1134 1135 /* clear TX/RX time stamp registers, just to be sure */ 1136 ixgbe_ptp_clear_tx_timestamp(adapter); 1137 IXGBE_READ_REG(hw, IXGBE_RXSTMPH); 1138 1139 return 0; 1140 } 1141 1142 /** 1143 * ixgbe_ptp_set_ts_config - user entry point for timestamp mode 1144 * @adapter: pointer to adapter struct 1145 * @ifr: ioctl data 1146 * 1147 * Set hardware to requested mode. If unsupported, return an error with no 1148 * changes. Otherwise, store the mode for future reference. 1149 */ 1150 int ixgbe_ptp_set_ts_config(struct ixgbe_adapter *adapter, struct ifreq *ifr) 1151 { 1152 struct hwtstamp_config config; 1153 int err; 1154 1155 if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 1156 return -EFAULT; 1157 1158 err = ixgbe_ptp_set_timestamp_mode(adapter, &config); 1159 if (err) 1160 return err; 1161 1162 /* save these settings for future reference */ 1163 memcpy(&adapter->tstamp_config, &config, 1164 sizeof(adapter->tstamp_config)); 1165 1166 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 1167 -EFAULT : 0; 1168 } 1169 1170 static void ixgbe_ptp_link_speed_adjust(struct ixgbe_adapter *adapter, 1171 u32 *shift, u32 *incval) 1172 { 1173 /** 1174 * Scale the NIC cycle counter by a large factor so that 1175 * relatively small corrections to the frequency can be added 1176 * or subtracted. The drawbacks of a large factor include 1177 * (a) the clock register overflows more quickly, (b) the cycle 1178 * counter structure must be able to convert the systime value 1179 * to nanoseconds using only a multiplier and a right-shift, 1180 * and (c) the value must fit within the timinca register space 1181 * => math based on internal DMA clock rate and available bits 1182 * 1183 * Note that when there is no link, internal DMA clock is same as when 1184 * link speed is 10Gb. Set the registers correctly even when link is 1185 * down to preserve the clock setting 1186 */ 1187 switch (adapter->link_speed) { 1188 case IXGBE_LINK_SPEED_100_FULL: 1189 *shift = IXGBE_INCVAL_SHIFT_100; 1190 *incval = IXGBE_INCVAL_100; 1191 break; 1192 case IXGBE_LINK_SPEED_1GB_FULL: 1193 *shift = IXGBE_INCVAL_SHIFT_1GB; 1194 *incval = IXGBE_INCVAL_1GB; 1195 break; 1196 case IXGBE_LINK_SPEED_10GB_FULL: 1197 default: 1198 *shift = IXGBE_INCVAL_SHIFT_10GB; 1199 *incval = IXGBE_INCVAL_10GB; 1200 break; 1201 } 1202 } 1203 1204 /** 1205 * ixgbe_ptp_start_cyclecounter - create the cycle counter from hw 1206 * @adapter: pointer to the adapter structure 1207 * 1208 * This function should be called to set the proper values for the TIMINCA 1209 * register and tell the cyclecounter structure what the tick rate of SYSTIME 1210 * is. It does not directly modify SYSTIME registers or the timecounter 1211 * structure. It should be called whenever a new TIMINCA value is necessary, 1212 * such as during initialization or when the link speed changes. 1213 */ 1214 void ixgbe_ptp_start_cyclecounter(struct ixgbe_adapter *adapter) 1215 { 1216 struct ixgbe_hw *hw = &adapter->hw; 1217 struct cyclecounter cc; 1218 unsigned long flags; 1219 u32 incval = 0; 1220 u32 tsauxc = 0; 1221 u32 fuse0 = 0; 1222 1223 /* For some of the boards below this mask is technically incorrect. 1224 * The timestamp mask overflows at approximately 61bits. However the 1225 * particular hardware does not overflow on an even bitmask value. 1226 * Instead, it overflows due to conversion of upper 32bits billions of 1227 * cycles. Timecounters are not really intended for this purpose so 1228 * they do not properly function if the overflow point isn't 2^N-1. 1229 * However, the actual SYSTIME values in question take ~138 years to 1230 * overflow. In practice this means they won't actually overflow. A 1231 * proper fix to this problem would require modification of the 1232 * timecounter delta calculations. 1233 */ 1234 cc.mask = CLOCKSOURCE_MASK(64); 1235 cc.mult = 1; 1236 cc.shift = 0; 1237 1238 switch (hw->mac.type) { 1239 case ixgbe_mac_X550EM_x: 1240 /* SYSTIME assumes X550EM_x board frequency is 300Mhz, and is 1241 * designed to represent seconds and nanoseconds when this is 1242 * the case. However, some revisions of hardware have a 400Mhz 1243 * clock and we have to compensate for this frequency 1244 * variation using corrected mult and shift values. 1245 */ 1246 fuse0 = IXGBE_READ_REG(hw, IXGBE_FUSES0_GROUP(0)); 1247 if (!(fuse0 & IXGBE_FUSES0_300MHZ)) { 1248 cc.mult = 3; 1249 cc.shift = 2; 1250 } 1251 /* fallthrough */ 1252 case ixgbe_mac_x550em_a: 1253 case ixgbe_mac_X550: 1254 cc.read = ixgbe_ptp_read_X550; 1255 1256 /* enable SYSTIME counter */ 1257 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMR, 0); 1258 IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0); 1259 IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0); 1260 tsauxc = IXGBE_READ_REG(hw, IXGBE_TSAUXC); 1261 IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, 1262 tsauxc & ~IXGBE_TSAUXC_DISABLE_SYSTIME); 1263 IXGBE_WRITE_REG(hw, IXGBE_TSIM, IXGBE_TSIM_TXTS); 1264 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_TIMESYNC); 1265 1266 IXGBE_WRITE_FLUSH(hw); 1267 break; 1268 case ixgbe_mac_X540: 1269 cc.read = ixgbe_ptp_read_82599; 1270 1271 ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval); 1272 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval); 1273 break; 1274 case ixgbe_mac_82599EB: 1275 cc.read = ixgbe_ptp_read_82599; 1276 1277 ixgbe_ptp_link_speed_adjust(adapter, &cc.shift, &incval); 1278 incval >>= IXGBE_INCVAL_SHIFT_82599; 1279 cc.shift -= IXGBE_INCVAL_SHIFT_82599; 1280 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, 1281 BIT(IXGBE_INCPER_SHIFT_82599) | incval); 1282 break; 1283 default: 1284 /* other devices aren't supported */ 1285 return; 1286 } 1287 1288 /* update the base incval used to calculate frequency adjustment */ 1289 WRITE_ONCE(adapter->base_incval, incval); 1290 smp_mb(); 1291 1292 /* need lock to prevent incorrect read while modifying cyclecounter */ 1293 spin_lock_irqsave(&adapter->tmreg_lock, flags); 1294 memcpy(&adapter->hw_cc, &cc, sizeof(adapter->hw_cc)); 1295 spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 1296 } 1297 1298 /** 1299 * ixgbe_ptp_reset 1300 * @adapter: the ixgbe private board structure 1301 * 1302 * When the MAC resets, all the hardware bits for timesync are reset. This 1303 * function is used to re-enable the device for PTP based on current settings. 1304 * We do lose the current clock time, so just reset the cyclecounter to the 1305 * system real clock time. 1306 * 1307 * This function will maintain hwtstamp_config settings, and resets the SDP 1308 * output if it was enabled. 1309 */ 1310 void ixgbe_ptp_reset(struct ixgbe_adapter *adapter) 1311 { 1312 struct ixgbe_hw *hw = &adapter->hw; 1313 unsigned long flags; 1314 1315 /* reset the hardware timestamping mode */ 1316 ixgbe_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config); 1317 1318 /* 82598 does not support PTP */ 1319 if (hw->mac.type == ixgbe_mac_82598EB) 1320 return; 1321 1322 ixgbe_ptp_start_cyclecounter(adapter); 1323 1324 spin_lock_irqsave(&adapter->tmreg_lock, flags); 1325 timecounter_init(&adapter->hw_tc, &adapter->hw_cc, 1326 ktime_to_ns(ktime_get_real())); 1327 spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 1328 1329 adapter->last_overflow_check = jiffies; 1330 1331 /* Now that the shift has been calculated and the systime 1332 * registers reset, (re-)enable the Clock out feature 1333 */ 1334 if (adapter->ptp_setup_sdp) 1335 adapter->ptp_setup_sdp(adapter); 1336 } 1337 1338 /** 1339 * ixgbe_ptp_create_clock 1340 * @adapter: the ixgbe private adapter structure 1341 * 1342 * This function performs setup of the user entry point function table and 1343 * initializes the PTP clock device, which is used to access the clock-like 1344 * features of the PTP core. It will be called by ixgbe_ptp_init, and may 1345 * reuse a previously initialized clock (such as during a suspend/resume 1346 * cycle). 1347 */ 1348 static long ixgbe_ptp_create_clock(struct ixgbe_adapter *adapter) 1349 { 1350 struct net_device *netdev = adapter->netdev; 1351 long err; 1352 1353 /* do nothing if we already have a clock device */ 1354 if (!IS_ERR_OR_NULL(adapter->ptp_clock)) 1355 return 0; 1356 1357 switch (adapter->hw.mac.type) { 1358 case ixgbe_mac_X540: 1359 snprintf(adapter->ptp_caps.name, 1360 sizeof(adapter->ptp_caps.name), 1361 "%s", netdev->name); 1362 adapter->ptp_caps.owner = THIS_MODULE; 1363 adapter->ptp_caps.max_adj = 250000000; 1364 adapter->ptp_caps.n_alarm = 0; 1365 adapter->ptp_caps.n_ext_ts = 0; 1366 adapter->ptp_caps.n_per_out = 0; 1367 adapter->ptp_caps.pps = 1; 1368 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_82599; 1369 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime; 1370 adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex; 1371 adapter->ptp_caps.settime64 = ixgbe_ptp_settime; 1372 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable; 1373 adapter->ptp_setup_sdp = ixgbe_ptp_setup_sdp_X540; 1374 break; 1375 case ixgbe_mac_82599EB: 1376 snprintf(adapter->ptp_caps.name, 1377 sizeof(adapter->ptp_caps.name), 1378 "%s", netdev->name); 1379 adapter->ptp_caps.owner = THIS_MODULE; 1380 adapter->ptp_caps.max_adj = 250000000; 1381 adapter->ptp_caps.n_alarm = 0; 1382 adapter->ptp_caps.n_ext_ts = 0; 1383 adapter->ptp_caps.n_per_out = 0; 1384 adapter->ptp_caps.pps = 0; 1385 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_82599; 1386 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime; 1387 adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex; 1388 adapter->ptp_caps.settime64 = ixgbe_ptp_settime; 1389 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable; 1390 break; 1391 case ixgbe_mac_X550: 1392 case ixgbe_mac_X550EM_x: 1393 case ixgbe_mac_x550em_a: 1394 snprintf(adapter->ptp_caps.name, 16, "%s", netdev->name); 1395 adapter->ptp_caps.owner = THIS_MODULE; 1396 adapter->ptp_caps.max_adj = 30000000; 1397 adapter->ptp_caps.n_alarm = 0; 1398 adapter->ptp_caps.n_ext_ts = 0; 1399 adapter->ptp_caps.n_per_out = 0; 1400 adapter->ptp_caps.pps = 1; 1401 adapter->ptp_caps.adjfreq = ixgbe_ptp_adjfreq_X550; 1402 adapter->ptp_caps.adjtime = ixgbe_ptp_adjtime; 1403 adapter->ptp_caps.gettimex64 = ixgbe_ptp_gettimex; 1404 adapter->ptp_caps.settime64 = ixgbe_ptp_settime; 1405 adapter->ptp_caps.enable = ixgbe_ptp_feature_enable; 1406 adapter->ptp_setup_sdp = ixgbe_ptp_setup_sdp_X550; 1407 break; 1408 default: 1409 adapter->ptp_clock = NULL; 1410 adapter->ptp_setup_sdp = NULL; 1411 return -EOPNOTSUPP; 1412 } 1413 1414 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps, 1415 &adapter->pdev->dev); 1416 if (IS_ERR(adapter->ptp_clock)) { 1417 err = PTR_ERR(adapter->ptp_clock); 1418 adapter->ptp_clock = NULL; 1419 e_dev_err("ptp_clock_register failed\n"); 1420 return err; 1421 } else if (adapter->ptp_clock) 1422 e_dev_info("registered PHC device on %s\n", netdev->name); 1423 1424 /* set default timestamp mode to disabled here. We do this in 1425 * create_clock instead of init, because we don't want to override the 1426 * previous settings during a resume cycle. 1427 */ 1428 adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE; 1429 adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF; 1430 1431 return 0; 1432 } 1433 1434 /** 1435 * ixgbe_ptp_init 1436 * @adapter: the ixgbe private adapter structure 1437 * 1438 * This function performs the required steps for enabling PTP 1439 * support. If PTP support has already been loaded it simply calls the 1440 * cyclecounter init routine and exits. 1441 */ 1442 void ixgbe_ptp_init(struct ixgbe_adapter *adapter) 1443 { 1444 /* initialize the spin lock first since we can't control when a user 1445 * will call the entry functions once we have initialized the clock 1446 * device 1447 */ 1448 spin_lock_init(&adapter->tmreg_lock); 1449 1450 /* obtain a PTP device, or re-use an existing device */ 1451 if (ixgbe_ptp_create_clock(adapter)) 1452 return; 1453 1454 /* we have a clock so we can initialize work now */ 1455 INIT_WORK(&adapter->ptp_tx_work, ixgbe_ptp_tx_hwtstamp_work); 1456 1457 /* reset the PTP related hardware bits */ 1458 ixgbe_ptp_reset(adapter); 1459 1460 /* enter the IXGBE_PTP_RUNNING state */ 1461 set_bit(__IXGBE_PTP_RUNNING, &adapter->state); 1462 1463 return; 1464 } 1465 1466 /** 1467 * ixgbe_ptp_suspend - stop PTP work items 1468 * @adapter: pointer to adapter struct 1469 * 1470 * this function suspends PTP activity, and prevents more PTP work from being 1471 * generated, but does not destroy the PTP clock device. 1472 */ 1473 void ixgbe_ptp_suspend(struct ixgbe_adapter *adapter) 1474 { 1475 /* Leave the IXGBE_PTP_RUNNING state. */ 1476 if (!test_and_clear_bit(__IXGBE_PTP_RUNNING, &adapter->state)) 1477 return; 1478 1479 adapter->flags2 &= ~IXGBE_FLAG2_PTP_PPS_ENABLED; 1480 if (adapter->ptp_setup_sdp) 1481 adapter->ptp_setup_sdp(adapter); 1482 1483 /* ensure that we cancel any pending PTP Tx work item in progress */ 1484 cancel_work_sync(&adapter->ptp_tx_work); 1485 ixgbe_ptp_clear_tx_timestamp(adapter); 1486 } 1487 1488 /** 1489 * ixgbe_ptp_stop - close the PTP device 1490 * @adapter: pointer to adapter struct 1491 * 1492 * completely destroy the PTP device, should only be called when the device is 1493 * being fully closed. 1494 */ 1495 void ixgbe_ptp_stop(struct ixgbe_adapter *adapter) 1496 { 1497 /* first, suspend PTP activity */ 1498 ixgbe_ptp_suspend(adapter); 1499 1500 /* disable the PTP clock device */ 1501 if (adapter->ptp_clock) { 1502 ptp_clock_unregister(adapter->ptp_clock); 1503 adapter->ptp_clock = NULL; 1504 e_dev_info("removed PHC on %s\n", 1505 adapter->netdev->name); 1506 } 1507 } 1508