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