Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net/mlx5: Dynamic cyclecounter shift calculation for PTP free running clock

Use a dynamic calculation to determine the shift value for the internal
timer cyclecounter that will lead to the highest precision frequency
adjustments. Previously used a constant for the shift value assuming all
devices supported by the driver had a nominal frequency of 1GHz. However,
there are devices that operate at different frequencies. The previous shift
value constant would break the PHC functionality for those devices.

Reported-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
Closes: https://lore.kernel.org/netdev/20230815151507.3028503-1-vadfed@meta.com/
Fixes: 6a4010927562 ("net/mlx5: Update cyclecounter shift value to improve ptp free running mode precision")
Signed-off-by: Rahul Rameshbabu <rrameshbabu@nvidia.com>
Tested-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Acked-by: Saeed Mahameed <saeedm@nvidia.com>
Link: https://lore.kernel.org/r/20230821230554.236210-1-rrameshbabu@nvidia.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Rahul Rameshbabu and committed by
Jakub Kicinski
84a58e60 b5cc3833

+27 -5
+27 -5
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c
··· 32 32 33 33 #include <linux/clocksource.h> 34 34 #include <linux/highmem.h> 35 + #include <linux/log2.h> 35 36 #include <linux/ptp_clock_kernel.h> 36 37 #include <rdma/mlx5-abi.h> 37 38 #include "lib/eq.h" 38 39 #include "en.h" 39 40 #include "clock.h" 40 - 41 - enum { 42 - MLX5_CYCLES_SHIFT = 31 43 - }; 44 41 45 42 enum { 46 43 MLX5_PIN_MODE_IN = 0x0, ··· 88 91 static bool mlx5_modify_mtutc_allowed(struct mlx5_core_dev *mdev) 89 92 { 90 93 return MLX5_CAP_MCAM_FEATURE(mdev, ptpcyc2realtime_modify); 94 + } 95 + 96 + static u32 mlx5_ptp_shift_constant(u32 dev_freq_khz) 97 + { 98 + /* Optimal shift constant leads to corrections above just 1 scaled ppm. 99 + * 100 + * Two sets of equations are needed to derive the optimal shift 101 + * constant for the cyclecounter. 102 + * 103 + * dev_freq_khz * 1000 / 2^shift_constant = 1 scaled_ppm 104 + * ppb = scaled_ppm * 1000 / 2^16 105 + * 106 + * Using the two equations together 107 + * 108 + * dev_freq_khz * 1000 / 1 scaled_ppm = 2^shift_constant 109 + * dev_freq_khz * 2^16 / 1 ppb = 2^shift_constant 110 + * dev_freq_khz = 2^(shift_constant - 16) 111 + * 112 + * then yields 113 + * 114 + * shift_constant = ilog2(dev_freq_khz) + 16 115 + */ 116 + 117 + return min(ilog2(dev_freq_khz) + 16, 118 + ilog2((U32_MAX / NSEC_PER_MSEC) * dev_freq_khz)); 91 119 } 92 120 93 121 static s32 mlx5_ptp_getmaxphase(struct ptp_clock_info *ptp) ··· 931 909 932 910 dev_freq = MLX5_CAP_GEN(mdev, device_frequency_khz); 933 911 timer->cycles.read = read_internal_timer; 934 - timer->cycles.shift = MLX5_CYCLES_SHIFT; 912 + timer->cycles.shift = mlx5_ptp_shift_constant(dev_freq); 935 913 timer->cycles.mult = clocksource_khz2mult(dev_freq, 936 914 timer->cycles.shift); 937 915 timer->nominal_c_mult = timer->cycles.mult;