Skip to content

Commit 23797b9

Browse files
amaftei-xilinxdavem330
authored andcommitted
sfc: fix timestamp reconstruction at 16-bit rollover points
We can't just use the top bits of the last sync event as they could be off-by-one every 65,536 seconds, giving an error in reconstruction of 65,536 seconds. This patch uses the difference in the bottom 16 bits (mod 2^16) to calculate an offset that needs to be applied to the last sync event to get to the current time. Signed-off-by: Alexandru-Mihai Maftei <[email protected]> Acked-by: Martin Habets <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 3f74957 commit 23797b9

File tree

1 file changed

+35
-3
lines changed
  • drivers/net/ethernet/sfc

1 file changed

+35
-3
lines changed

drivers/net/ethernet/sfc/ptp.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,45 @@ efx_ptp_mac_nic_to_ktime_correction(struct efx_nic *efx,
560560
u32 nic_major, u32 nic_minor,
561561
s32 correction)
562562
{
563+
u32 sync_timestamp;
563564
ktime_t kt = { 0 };
565+
s16 delta;
564566

565567
if (!(nic_major & 0x80000000)) {
566568
WARN_ON_ONCE(nic_major >> 16);
567-
/* Use the top bits from the latest sync event. */
568-
nic_major &= 0xffff;
569-
nic_major |= (last_sync_timestamp_major(efx) & 0xffff0000);
569+
570+
/* Medford provides 48 bits of timestamp, so we must get the top
571+
* 16 bits from the timesync event state.
572+
*
573+
* We only have the lower 16 bits of the time now, but we do
574+
* have a full resolution timestamp at some point in past. As
575+
* long as the difference between the (real) now and the sync
576+
* is less than 2^15, then we can reconstruct the difference
577+
* between those two numbers using only the lower 16 bits of
578+
* each.
579+
*
580+
* Put another way
581+
*
582+
* a - b = ((a mod k) - b) mod k
583+
*
584+
* when -k/2 < (a-b) < k/2. In our case k is 2^16. We know
585+
* (a mod k) and b, so can calculate the delta, a - b.
586+
*
587+
*/
588+
sync_timestamp = last_sync_timestamp_major(efx);
589+
590+
/* Because delta is s16 this does an implicit mask down to
591+
* 16 bits which is what we need, assuming
592+
* MEDFORD_TX_SECS_EVENT_BITS is 16. delta is signed so that
593+
* we can deal with the (unlikely) case of sync timestamps
594+
* arriving from the future.
595+
*/
596+
delta = nic_major - sync_timestamp;
597+
598+
/* Recover the fully specified time now, by applying the offset
599+
* to the (fully specified) sync time.
600+
*/
601+
nic_major = sync_timestamp + delta;
570602

571603
kt = ptp->nic_to_kernel_time(nic_major, nic_minor,
572604
correction);

0 commit comments

Comments
 (0)