Skip to contentSkip to conversation

ptp: convert drivers to .adjfine

PATCH In review Jacob Keller 4 participants 14 messages Oct 28, 2022 – Oct 31, 2022

Subsystems (12)
lore
Share & tools
lore
#0 lore raw

v3 cover ptp: convert drivers to .adjfine

Many drivers implementing PTP have not yet migrated to the new .adjfine frequency adjustment implementation.

A handful of these drivers use hardware with a simple increment value which is adjusted by multiplying by the adjustment factor and then dividing by 1 billion. This calculation is very easy to convert to .adjfine, by simply updating the divisor.

Introduce new helper functions, diff_by_scaled_ppm and adjust_by_scaled_ppm which perform the most common calculations used by drivers for this purpose.

The adjust_by_scaled_ppm takes the base increment and scaled PPM value, and calculates the new increment to use.

A few drivers need the difference and direction rather than a raw increment value. The diff_by_scaled_ppm calculates the difference and returns true if it should be a subtraction, false otherwise. This most closely aligns with existing driver implementations.

I previously submitted v1 of this series at [1], and got some feedback only on a handful of drivers. In the interest of merging the changes which have received feedback, I've dropped the following drivers out of this send:

* ptp_phc
* ptp_ipx46x
* tg3
* hclge
* stmac
* cpts

I plan to submit those drivers changes again at a later date. As before, there are some drivers which are not trivial to convert to the new helper functions. While they may be able to work, their implementation is different and I lack the hardware or datasheets to determine what the correct implementation would be.

* drivers/net/ethernet/broadcom/bnx2x
* drivers/net/ethernet/broadcom/bnxt
* drivers/net/ethernet/cavium/liquidio
* drivers/net/ethernet/chelsio/cxgb4
* drivers/net/ethernet/freescale
* drivers/net/ethernet/qlogic/qed
* drivers/net/ethernet/qlogic/qede
* drivers/net/ethernet/sfc
* drivers/net/ethernet/sfc/siena
* drivers/net/ethernet/ti/am65-cpts.c
* drivers/ptp/ptp_dte.c

My end goal is to drop the .adjfreq implementation entirely, and to that end I plan on modifying these drivers in the future to directly use scaled_ppm_to_ppb as the simplest method to convert them.

Changes since v2:
* Rebased to allow landing in 6.2
* Added Richard's Acked-by

Cc "K. Y. Srinivasan" <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Stephen Hemminger <[email protected]>
Cc Wei Liu <[email protected]>
Cc Dexuan Cui <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Shyam Sundar S K <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Siva Reddy Kallam <[email protected]>
Cc Prashant Sreedharan <[email protected]>
Cc Michael Chan <[email protected]>
Cc Yisen Zhuang <[email protected]>
Cc Salil Mehta <[email protected]>
Cc Jesse Brandeburg <[email protected]>
Cc Tony Nguyen <[email protected]>
Cc Tariq Toukan <[email protected]>
Cc Saeed Mahameed <[email protected]>
Cc Leon Romanovsky <[email protected]>
Cc Bryan Whitehead <[email protected]>
Cc Sergey Shtylyov <[email protected]>
Cc Giuseppe Cavallaro <[email protected]>
Cc Alexandre Torgue <[email protected]>
Cc Jose Abreu <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Richard Cochran <[email protected]>
Cc Vivek Thampi <[email protected]>
Cc VMware PV-Drivers Reviewers <[email protected]>
Cc Jie Wang <[email protected]>
Cc Jacob Keller <[email protected]>
Cc Guangbin Huang <[email protected]>
Cc Eran Ben Elisha <[email protected]>
Cc Aya Levin <[email protected]>
Cc Cai Huoqing <[email protected]>
Cc Biju Das <[email protected]>
Cc Lad Prabhakar <[email protected]>
Cc Phil Edworthy <[email protected]>
Cc Jiasheng Jiang <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Wan Jiabing <[email protected]>
Cc Lv Ruyi <[email protected]>
Cc Arnd Bergmann <[email protected]>

Jacob Keller (9):
ptp: add missing documentation for parameters
ptp: introduce helpers to adjust by scaled parts per million
drivers: convert unsupported .adjfreq to .adjfine
ptp: mlx4: convert to .adjfine and adjust_by_scaled_ppm
ptp: mlx5: convert to .adjfine and adjust_by_scaled_ppm
ptp: lan743x: remove .adjfreq implementation
ptp: lan743x: use diff_by_scaled_ppm in .adjfine implementation
ptp: ravb: convert to .adjfine and adjust_by_scaled_ppm
ptp: xgbe: convert to .adjfine and adjust_by_scaled_ppm

 drivers/hv/hv_util.c                          |  4 +-
 drivers/net/ethernet/amd/xgbe/xgbe-ptp.c      | 20 ++-----
 drivers/net/ethernet/intel/e1000e/ptp.c       | 16 ++----
 drivers/net/ethernet/intel/i40e/i40e_ptp.c    | 17 ++----
 drivers/net/ethernet/intel/ice/ice_ptp.c      | 18 +------
 drivers/net/ethernet/intel/igb/igb_ptp.c      | 18 +------
 drivers/net/ethernet/intel/ixgbe/ixgbe_ptp.c  | 24 ++-------
 drivers/net/ethernet/mellanox/mlx4/en_clock.c | 29 ++++------
 .../ethernet/mellanox/mlx5/core/lib/clock.c   | 22 +++-----
 drivers/net/ethernet/microchip/lan743x_ptp.c  | 54 +++----------------
 drivers/net/ethernet/renesas/ravb_ptp.c       | 17 ++----
 drivers/ptp/ptp_kvm_common.c                  |  4 +-
 drivers/ptp/ptp_vmw.c                         |  4 +-
 include/linux/ptp_clock_kernel.h              | 53 ++++++++++++++++++
 14 files changed, 105 insertions(+), 195 deletions(-)

base-commit: 915b96c52763e2988e6368b538b487a7138b8fa4

Signature · 1 line
2.37.1.394.gc50926e1f488
#1 lore raw

PATCH v3 1/9 ptp: add missing documentation for parameters

The ptp_find_pin_unlocked function and the ptp_system_timestamp structure didn't document their parameters and fields. Fix this.

---
 include/linux/ptp_clock_kernel.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/linux/ptp_clock_kernel.h +7 -0
index 92b44161408e..ad4aaadc2f7a 100644
@@ -45,6 +45,8 @@ struct system_device_crosststamp;
 
 /**
  * struct ptp_system_timestamp - system time corresponding to a PHC timestamp
+ * @pre_ts: system timestamp before capturing PHC
+ * @post_ts: system timestamp after capturing PHC
  */
 struct ptp_system_timestamp {
 	struct timespec64 pre_ts;
@@ -316,6 +318,11 @@ int ptp_find_pin(struct ptp_clock *ptp,
  * should most likely call ptp_find_pin() directly from their
  * ptp_clock_info::enable() method.
  *
+* @ptp:    The clock obtained from ptp_clock_register().
+* @func:   One of the ptp_pin_function enumerated values.
+* @chan:   The particular functional channel to find.
+* Return:  Pin index in the range of zero to ptp_clock_caps.n_pins - 1,
+*          or -1 if the auxiliary function cannot be found.
  */
 
 int ptp_find_pin_unlocked(struct ptp_clock *ptp,

base-commit: 12dee519d466025fdedced911d0fe81cb7ba29e7

Signature · 1 line
2.38.0.83.gd420dda05763
#2 lore raw

PATCH v3 2/9 ptp: introduce helpers to adjust by scaled parts per million

In reply to Jacob Keller

Many drivers implement the .adjfreq or .adjfine PTP op function with the same basic logic:

1. Determine a base frequency value
2. Multiply this by the abs() of the requested adjustment, then divide by the appropriate divisor (1 billion, or 65,536 billion).
3. Add or subtract this difference from the base frequency to calculate a new adjustment.

A few drivers need the difference and direction rather than the combined new increment value.

I recently converted the Intel drivers to .adjfine and the scaled parts per million (65.536 parts per billion) logic. To avoid overflow with minimal loss of precision, mul_u64_u64_div_u64 was used.

The basic logic used by all of these drivers is very similar, and leads to a lot of duplicate code to perform the same task.

Rather than keep this duplicate code, introduce diff_by_scaled_ppm and adjust_by_scaled_ppm. These helper functions calculate the difference or adjustment necessary based on the scaled parts per million input.

The diff_by_scaled_ppm function returns true if the difference should be subtracted, and false otherwise.

Update the Intel drivers to use the new helper functions. Other vendor drivers will be converted to .adjfine and this helper function in the following changes.

---
 drivers/net/ethernet/intel/e1000e/ptp.c      | 16 ++-----
 drivers/net/ethernet/intel/i40e/i40e_ptp.c   | 17 ++------
 drivers/net/ethernet/intel/ice/ice_ptp.c     | 18 +-------
 drivers/net/ethernet/intel/igb/igb_ptp.c     | 18 +-------
 drivers/net/ethernet/intel/ixgbe/ixgbe_ptp.c | 24 ++--------
 include/linux/ptp_clock_kernel.h             | 46 ++++++++++++++++++++
 6 files changed, 60 insertions(+), 79 deletions(-)
drivers/net/ethernet/intel/e1000e/ptp.c +3 -13
index 0e488e4fa5c1..6e5a1720e6cd 100644
@@ -29,17 +29,11 @@ static int e1000e_phc_adjfine(struct ptp_clock_info *ptp, long delta)
 	struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
 						     ptp_clock_info);
 	struct e1000_hw *hw = &adapter->hw;
-	bool neg_adj = false;
 	unsigned long flags;
-	u64 adjustment;
-	u32 timinca, incvalue;
+	u64 incvalue;
+	u32 timinca;
 	s32 ret_val;
 
-	if (delta < 0) {
-		neg_adj = true;
-		delta = -delta;
-	}
-
 	/* Get the System Time Register SYSTIM base frequency */
 	ret_val = e1000e_get_base_timinca(adapter, &timinca);
 	if (ret_val)
@@ -48,11 +42,7 @@ static int e1000e_phc_adjfine(struct ptp_clock_info *ptp, long delta)
 	spin_lock_irqsave(&adapter->systim_lock, flags);
 
 	incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
-
-	adjustment = mul_u64_u64_div_u64(incvalue, (u64)delta,
-					 1000000ULL << 16);
-
-	incvalue = neg_adj ? (incvalue - adjustment) : (incvalue + adjustment);
+	incvalue = adjust_by_scaled_ppm(incvalue, delta);
 
 	timinca &= ~E1000_TIMINCA_INCVALUE_MASK;
 	timinca |= incvalue;
drivers/net/ethernet/intel/i40e/i40e_ptp.c +3 -14
index ffea0c9c82f1..c37abbb3cd06 100644
@@ -347,23 +347,12 @@ static int i40e_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
 {
 	struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
 	struct i40e_hw *hw = &pf->hw;
-	u64 adj, freq, diff;
-	int neg_adj = 0;
-
-	if (scaled_ppm < 0) {
-		neg_adj = 1;
-		scaled_ppm = -scaled_ppm;
-	}
+	u64 adj, base_adj;
 
 	smp_mb(); /* Force any pending update before accessing. */
-	freq = I40E_PTP_40GB_INCVAL * READ_ONCE(pf->ptp_adj_mult);
-	diff = mul_u64_u64_div_u64(freq, (u64)scaled_ppm,
-				   1000000ULL << 16);
+	base_adj = I40E_PTP_40GB_INCVAL * READ_ONCE(pf->ptp_adj_mult);
 
-	if (neg_adj)
-		adj = I40E_PTP_40GB_INCVAL - diff;
-	else
-		adj = I40E_PTP_40GB_INCVAL + diff;
+	adj = adjust_by_scaled_ppm(base_adj, scaled_ppm);
 
 	wr32(hw, I40E_PRTTSYN_INC_L, adj & 0xFFFFFFFF);
 	wr32(hw, I40E_PRTTSYN_INC_H, adj >> 32);
drivers/net/ethernet/intel/ice/ice_ptp.c +2 -16
index 011b727ab190..5cf198a33e26 100644
@@ -1444,24 +1444,10 @@ static int ice_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
 {
 	struct ice_pf *pf = ptp_info_to_pf(info);
 	struct ice_hw *hw = &pf->hw;
-	u64 incval, diff;
-	int neg_adj = 0;
+	u64 incval;
 	int err;
 
-	incval = ice_base_incval(pf);
-
-	if (scaled_ppm < 0) {
-		neg_adj = 1;
-		scaled_ppm = -scaled_ppm;
-	}
-
-	diff = mul_u64_u64_div_u64(incval, (u64)scaled_ppm,
-				   1000000ULL << 16);
-	if (neg_adj)
-		incval -= diff;
-	else
-		incval += diff;
-
+	incval = adjust_by_scaled_ppm(ice_base_incval(pf), scaled_ppm);
 	err = ice_ptp_write_incval_locked(hw, incval);
 	if (err) {
 		dev_err(ice_pf_to_dev(pf), "PTP failed to set incval, err %d\n",
drivers/net/ethernet/intel/igb/igb_ptp.c +2 -16
index 15e57460e19e..6f471b91f562 100644
@@ -195,23 +195,9 @@ static int igb_ptp_adjfine_82576(struct ptp_clock_info *ptp, long scaled_ppm)
 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
 					       ptp_caps);
 	struct e1000_hw *hw = &igb->hw;
-	int neg_adj = 0;
-	u64 rate;
-	u32 incvalue;
+	u64 incvalue;
 
-	if (scaled_ppm < 0) {
-		neg_adj = 1;
-		scaled_ppm = -scaled_ppm;
-	}
-
-	incvalue = INCVALUE_82576;
-	rate = mul_u64_u64_div_u64(incvalue, (u64)scaled_ppm,
-				   1000000ULL << 16);
-
-	if (neg_adj)
-		incvalue -= rate;
-	else
-		incvalue += rate;
+	incvalue = adjust_by_scaled_ppm(INCVALUE_82576, scaled_ppm);
 
 	wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
 
drivers/net/ethernet/intel/ixgbe/ixgbe_ptp.c +4 -20
index f8605f57bd06..fd3f77a9e28d 100644
@@ -451,21 +451,11 @@ static int ixgbe_ptp_adjfine_82599(struct ptp_clock_info *ptp, long scaled_ppm)
 	struct ixgbe_adapter *adapter =
 		container_of(ptp, struct ixgbe_adapter, ptp_caps);
 	struct ixgbe_hw *hw = &adapter->hw;
-	u64 incval, diff;
-	int neg_adj = 0;
-
-	if (scaled_ppm < 0) {
-		neg_adj = 1;
-		scaled_ppm = -scaled_ppm;
-	}
+	u64 incval;
 
 	smp_mb();
 	incval = READ_ONCE(adapter->base_incval);
-
-	diff = mul_u64_u64_div_u64(incval, scaled_ppm,
-				   1000000ULL << 16);
-
-	incval = neg_adj ? (incval - diff) : (incval + diff);
+	incval = adjust_by_scaled_ppm(incval, scaled_ppm);
 
 	switch (hw->mac.type) {
 	case ixgbe_mac_X540:
@@ -502,17 +492,11 @@ static int ixgbe_ptp_adjfine_X550(struct ptp_clock_info *ptp, long scaled_ppm)
 	struct ixgbe_adapter *adapter =
 			container_of(ptp, struct ixgbe_adapter, ptp_caps);
 	struct ixgbe_hw *hw = &adapter->hw;
-	int neg_adj = 0;
+	bool neg_adj;
 	u64 rate;
 	u32 inca;
 
-	if (scaled_ppm < 0) {
-		neg_adj = 1;
-		scaled_ppm = -scaled_ppm;
-	}
-
-	rate = mul_u64_u64_div_u64(IXGBE_X550_BASE_PERIOD, scaled_ppm,
-				   1000000ULL << 16);
+	neg_adj = diff_by_scaled_ppm(IXGBE_X550_BASE_PERIOD, scaled_ppm, &rate);
 
 	/* warn if rate is too large */
 	if (rate >= INCVALUE_MASK)
include/linux/ptp_clock_kernel.h +46 -0

52 lines not on this page · raw message

Signature · 1 line
2.38.0.83.gd420dda05763
#3 lore raw

PATCH v3 3/9 drivers: convert unsupported .adjfreq to .adjfine

In reply to Jacob Keller

A few PTP drivers implement a .adjfreq handler which indicates the operation is not supported. Convert all of these to .adjfine.

Cc "K. Y. Srinivasan" <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Stephen Hemminger <[email protected]>
Cc Wei Liu <[email protected]>
Cc Dexuan Cui <[email protected]>
Cc Vivek Thampi <[email protected]>
---
 drivers/hv/hv_util.c         | 4 ++--
 drivers/ptp/ptp_kvm_common.c | 4 ++--
 drivers/ptp/ptp_vmw.c        | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
drivers/hv/hv_util.c +2 -2
index 835e6039c186..d776074b49cb 100644
@@ -706,7 +706,7 @@ static int hv_ptp_settime(struct ptp_clock_info *p, const struct timespec64 *ts)
 	return -EOPNOTSUPP;
 }
 
-static int hv_ptp_adjfreq(struct ptp_clock_info *ptp, s32 delta)
+static int hv_ptp_adjfine(struct ptp_clock_info *ptp, long delta)
 {
 	return -EOPNOTSUPP;
 }
@@ -724,7 +724,7 @@ static struct ptp_clock_info ptp_hyperv_info = {
 	.name		= "hyperv",
 	.enable         = hv_ptp_enable,
 	.adjtime        = hv_ptp_adjtime,
-	.adjfreq        = hv_ptp_adjfreq,
+	.adjfine        = hv_ptp_adjfine,
 	.gettime64      = hv_ptp_gettime,
 	.settime64      = hv_ptp_settime,
 	.owner		= THIS_MODULE,
drivers/ptp/ptp_kvm_common.c +2 -2
index fcae32f56f25..9141162c4237 100644
@@ -66,7 +66,7 @@ static int ptp_kvm_getcrosststamp(struct ptp_clock_info *ptp,
  * PTP clock operations
  */
 
-static int ptp_kvm_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
+static int ptp_kvm_adjfine(struct ptp_clock_info *ptp, long delta)
 {
 	return -EOPNOTSUPP;
 }
@@ -115,7 +115,7 @@ static const struct ptp_clock_info ptp_kvm_caps = {
 	.n_ext_ts	= 0,
 	.n_pins		= 0,
 	.pps		= 0,
-	.adjfreq	= ptp_kvm_adjfreq,
+	.adjfine	= ptp_kvm_adjfine,
 	.adjtime	= ptp_kvm_adjtime,
 	.gettime64	= ptp_kvm_gettime,
 	.settime64	= ptp_kvm_settime,
drivers/ptp/ptp_vmw.c +2 -2
index 5dca26e14bdc..d64eec5b1788 100644
@@ -47,7 +47,7 @@ static int ptp_vmw_adjtime(struct ptp_clock_info *info, s64 delta)
 	return -EOPNOTSUPP;
 }
 
-static int ptp_vmw_adjfreq(struct ptp_clock_info *info, s32 delta)
+static int ptp_vmw_adjfine(struct ptp_clock_info *info, long delta)
 {
 	return -EOPNOTSUPP;
 }
@@ -79,7 +79,7 @@ static struct ptp_clock_info ptp_vmw_clock_info = {
 	.name		= "ptp_vmw",
 	.max_adj	= 0,
 	.adjtime	= ptp_vmw_adjtime,
-	.adjfreq	= ptp_vmw_adjfreq,
+	.adjfine	= ptp_vmw_adjfine,
 	.gettime64	= ptp_vmw_gettime,
 	.settime64	= ptp_vmw_settime,
 	.enable		= ptp_vmw_enable,
Signature · 1 line
2.38.0.83.gd420dda05763
#4 lore raw

PATCH v3 4/9 ptp: mlx4: convert to .adjfine and adjust_by_scaled_ppm

In reply to Jacob Keller

The mlx4 implementation of .adjfreq is implemented in terms of a straight forward "base * ppb / 1 billion" calculation.

Convert this driver to .adjfine and use adjust_by_scaled_ppm to perform the calculation.

---
 drivers/net/ethernet/mellanox/mlx4/en_clock.c | 29 +++++++------------
 1 file changed, 11 insertions(+), 18 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_clock.c +11 -18
index 024788549c25..98b5ffb4d729 100644
@@ -111,34 +111,27 @@ void mlx4_en_ptp_overflow_check(struct mlx4_en_dev *mdev)
 }
 
 /**
- * mlx4_en_phc_adjfreq - adjust the frequency of the hardware clock
+ * mlx4_en_phc_adjfine - adjust the frequency of the hardware clock
  * @ptp: ptp clock structure
- * @delta: Desired frequency change in parts per billion
+ * @scaled_ppm: Desired frequency change in scaled parts per million
  *
- * Adjust the frequency of the PHC cycle counter by the indicated delta from
- * the base frequency.
+ * Adjust the frequency of the PHC cycle counter by the indicated scaled_ppm
+ * from the base frequency.
+ *
+ * Scaled parts per million is ppm with a 16-bit binary fractional field.
  **/
-static int mlx4_en_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
+static int mlx4_en_phc_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
 {
-	u64 adj;
-	u32 diff, mult;
-	int neg_adj = 0;
+	u32 mult;
 	unsigned long flags;
 	struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
 						ptp_clock_info);
 
-	if (delta < 0) {
-		neg_adj = 1;
-		delta = -delta;
-	}
-	mult = mdev->nominal_c_mult;
-	adj = mult;
-	adj *= delta;
-	diff = div_u64(adj, 1000000000ULL);
+	mult = (u32)adjust_by_scaled_ppm(mdev->nominal_c_mult, scaled_ppm);
 
 	write_seqlock_irqsave(&mdev->clock_lock, flags);
 	timecounter_read(&mdev->clock);
-	mdev->cycles.mult = neg_adj ? mult - diff : mult + diff;
+	mdev->cycles.mult = mult;
 	write_sequnlock_irqrestore(&mdev->clock_lock, flags);
 
 	return 0;
@@ -237,7 +230,7 @@ static const struct ptp_clock_info mlx4_en_ptp_clock_info = {
 	.n_per_out	= 0,
 	.n_pins		= 0,
 	.pps		= 0,
-	.adjfreq	= mlx4_en_phc_adjfreq,
+	.adjfine	= mlx4_en_phc_adjfine,
 	.adjtime	= mlx4_en_phc_adjtime,
 	.gettime64	= mlx4_en_phc_gettime,
 	.settime64	= mlx4_en_phc_settime,
Signature · 1 line
2.38.0.83.gd420dda05763
Reviewed-by on v3 4/9
#5 lore raw

Re: [PATCH net-next v3 4/9] ptp: mlx4: convert to .adjfine and adjust_by_scaled_ppm

Quoted 6 lines · Jacob Keller

The mlx4 implementation of .adjfreq is implemented in terms of a straight forward "base * ppb / 1 billion" calculation.

Convert this driver to .adjfine and use adjust_by_scaled_ppm to perform the calculation.

Quoted 7 lines
---
  drivers/net/ethernet/mellanox/mlx4/en_clock.c | 29 +++++++------------
  1 file changed, 11 insertions(+), 18 deletions(-)

Thanks for your patch.

#6 lore raw

PATCH v3 5/9 ptp: mlx5: convert to .adjfine and adjust_by_scaled_ppm

In reply to Jacob Keller

The mlx5 implementation of .adjfreq is implemented in terms of a straight forward "base * ppb / 1 billion" calculation.

Convert this to the .adjfine interface and use adjust_by_scaled_ppm for the calculation of the new mult value.

Note that the mlx5_ptp_adjfreq_real_time function expects input in terms of ppb, so use the scaled_ppm_to_ppb to convert before passing to this function.

---
 .../ethernet/mellanox/mlx5/core/lib/clock.c   | 22 +++++--------------
 1 file changed, 6 insertions(+), 16 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c +6 -16
index d3a9ae80fd30..69cfe60c558a 100644
@@ -339,35 +339,25 @@ static int mlx5_ptp_adjfreq_real_time(struct mlx5_core_dev *mdev, s32 freq)
 	return mlx5_set_mtutc(mdev, in, sizeof(in));
 }
 
-static int mlx5_ptp_adjfreq(struct ptp_clock_info *ptp, s32 delta)
+static int mlx5_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
 {
 	struct mlx5_clock *clock = container_of(ptp, struct mlx5_clock, ptp_info);
 	struct mlx5_timer *timer = &clock->timer;
 	struct mlx5_core_dev *mdev;
 	unsigned long flags;
-	int neg_adj = 0;
-	u32 diff;
-	u64 adj;
+	u32 mult;
 	int err;
 
 	mdev = container_of(clock, struct mlx5_core_dev, clock);
-	err = mlx5_ptp_adjfreq_real_time(mdev, delta);
+	err = mlx5_ptp_adjfreq_real_time(mdev, scaled_ppm_to_ppb(scaled_ppm));
 	if (err)
 		return err;
 
-	if (delta < 0) {
-		neg_adj = 1;
-		delta = -delta;
-	}
-
-	adj = timer->nominal_c_mult;
-	adj *= delta;
-	diff = div_u64(adj, 1000000000ULL);
+	mult = (u32)adjust_by_scaled_ppm(timer->nominal_c_mult, scaled_ppm);
 
 	write_seqlock_irqsave(&clock->lock, flags);
 	timecounter_read(&timer->tc);
-	timer->cycles.mult = neg_adj ? timer->nominal_c_mult - diff :
-				       timer->nominal_c_mult + diff;
+	timer->cycles.mult = mult;
 	mlx5_update_clock_info_page(mdev);
 	write_sequnlock_irqrestore(&clock->lock, flags);
 
@@ -697,7 +687,7 @@ static const struct ptp_clock_info mlx5_ptp_clock_info = {
 	.n_per_out	= 0,
 	.n_pins		= 0,
 	.pps		= 0,
-	.adjfreq	= mlx5_ptp_adjfreq,
+	.adjfine	= mlx5_ptp_adjfine,
 	.adjtime	= mlx5_ptp_adjtime,
 	.gettimex64	= mlx5_ptp_gettimex,
 	.settime64	= mlx5_ptp_settime,
Signature · 1 line
2.38.0.83.gd420dda05763
#7 lore raw

PATCH v3 6/9 ptp: lan743x: remove .adjfreq implementation

In reply to Jacob Keller

The lan743x driver implements both .adjfreq and .adjfine, but the core PTP subsystem prefers .adjfine if implemented. There is no reason to carry a .adjfreq implementation, so we can remove it.

---
 drivers/net/ethernet/microchip/lan743x_ptp.c | 35 --------------------
 1 file changed, 35 deletions(-)
drivers/net/ethernet/microchip/lan743x_ptp.c +0 -35
index da3ea905adbb..28930f3c52c2 100644
@@ -365,40 +365,6 @@ static int lan743x_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
 	return 0;
 }
 
-static int lan743x_ptpci_adjfreq(struct ptp_clock_info *ptpci, s32 delta_ppb)
-{
-	struct lan743x_ptp *ptp =
-		container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
-	struct lan743x_adapter *adapter =
-		container_of(ptp, struct lan743x_adapter, ptp);
-	u32 lan743x_rate_adj = 0;
-	bool positive = true;
-	u32 u32_delta = 0;
-	u64 u64_delta = 0;
-
-	if ((delta_ppb < (-LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB)) ||
-	    delta_ppb > LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB) {
-		return -EINVAL;
-	}
-	if (delta_ppb > 0) {
-		u32_delta = (u32)delta_ppb;
-		positive = true;
-	} else {
-		u32_delta = (u32)(-delta_ppb);
-		positive = false;
-	}
-	u64_delta = (((u64)u32_delta) << 35);
-	lan743x_rate_adj = div_u64(u64_delta, 1000000000);
-
-	if (positive)
-		lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
-
-	lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
-			  lan743x_rate_adj);
-
-	return 0;
-}
-
 static int lan743x_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
 {
 	struct lan743x_ptp *ptp =
@@ -1583,7 +1549,6 @@ int lan743x_ptp_open(struct lan743x_adapter *adapter)
 	ptp->ptp_clock_info.pps = LAN743X_PTP_N_PPS;
 	ptp->ptp_clock_info.pin_config = ptp->pin_config;
 	ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine;
-	ptp->ptp_clock_info.adjfreq = lan743x_ptpci_adjfreq;
 	ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime;
 	ptp->ptp_clock_info.gettime64 = lan743x_ptpci_gettime64;
 	ptp->ptp_clock_info.getcrosststamp = NULL;
Signature · 1 line
2.38.0.83.gd420dda05763
#8 lore raw

PATCH v3 7/9 ptp: lan743x: use diff_by_scaled_ppm in .adjfine implementation

In reply to Jacob Keller

Update the lan743x driver to use the recently added diff_by_scaled_ppm helper function. This reduces the amount of code required in lan743x_ptp.c driver file.

---
 drivers/net/ethernet/microchip/lan743x_ptp.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
drivers/net/ethernet/microchip/lan743x_ptp.c +6 -13
index 28930f3c52c2..39e1066ecd5f 100644
@@ -339,25 +339,18 @@ static int lan743x_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
 	struct lan743x_adapter *adapter =
 		container_of(ptp, struct lan743x_adapter, ptp);
 	u32 lan743x_rate_adj = 0;
-	bool positive = true;
-	u64 u64_delta = 0;
+	u64 u64_delta;
 
 	if ((scaled_ppm < (-LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM)) ||
 	    scaled_ppm > LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM) {
 		return -EINVAL;
 	}
-	if (scaled_ppm > 0) {
-		u64_delta = (u64)scaled_ppm;
-		positive = true;
-	} else {
-		u64_delta = (u64)(-scaled_ppm);
-		positive = false;
-	}
-	u64_delta = (u64_delta << 19);
-	lan743x_rate_adj = div_u64(u64_delta, 1000000);
 
-	if (positive)
-		lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
+	/* diff_by_scaled_ppm returns true if the difference is negative */
+	if (diff_by_scaled_ppm(1ULL << 35, scaled_ppm, &u64_delta))
+		lan743x_rate_adj = (u32)u64_delta;
+	else
+		lan743x_rate_adj = (u32)u64_delta | PTP_CLOCK_RATE_ADJ_DIR_;
 
 	lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
 			  lan743x_rate_adj);
Signature · 1 line
2.38.0.83.gd420dda05763
#9 lore raw

PATCH v3 8/9 ptp: ravb: convert to .adjfine and adjust_by_scaled_ppm

In reply to Jacob Keller

The ravb implementation of .adjfreq is implemented in terms of a straight forward "base * ppb / 1 billion" calculation.

Convert this driver to .adjfine and use the adjust_by_scaled_ppm helper function to calculate the new addend.

---
 drivers/net/ethernet/renesas/ravb_ptp.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
drivers/net/ethernet/renesas/ravb_ptp.c +5 -12
index 87c4306d66ec..6e4ef7af27bf 100644
@@ -88,24 +88,17 @@ static int ravb_ptp_update_compare(struct ravb_private *priv, u32 ns)
 }
 
 /* PTP clock operations */
-static int ravb_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
+static int ravb_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
 {
 	struct ravb_private *priv = container_of(ptp, struct ravb_private,
 						 ptp.info);
 	struct net_device *ndev = priv->ndev;
 	unsigned long flags;
-	u32 diff, addend;
-	bool neg_adj = false;
+	u32 addend;
 	u32 gccr;
 
-	if (ppb < 0) {
-		neg_adj = true;
-		ppb = -ppb;
-	}
-	addend = priv->ptp.default_addend;
-	diff = div_u64((u64)addend * ppb, NSEC_PER_SEC);
-
-	addend = neg_adj ? addend - diff : addend + diff;
+	addend = (u32)adjust_by_scaled_ppm(priv->ptp.default_addend,
+					   scaled_ppm);
 
 	spin_lock_irqsave(&priv->lock, flags);
 
@@ -295,7 +288,7 @@ static const struct ptp_clock_info ravb_ptp_info = {
 	.max_adj	= 50000000,
 	.n_ext_ts	= N_EXT_TS,
 	.n_per_out	= N_PER_OUT,
-	.adjfreq	= ravb_ptp_adjfreq,
+	.adjfine	= ravb_ptp_adjfine,
 	.adjtime	= ravb_ptp_adjtime,
 	.gettime64	= ravb_ptp_gettime64,
 	.settime64	= ravb_ptp_settime64,
Signature · 1 line
2.38.0.83.gd420dda05763
#10 lore raw

Re: [PATCH net-next v3 8/9] ptp: ravb: convert to .adjfine and adjust_by_scaled_ppm

Hello!

Quoted 9 lines · Jacob Keller

The ravb implementation of .adjfreq is implemented in terms of a straight forward "base * ppb / 1 billion" calculation.

Convert this driver to .adjfine and use the adjust_by_scaled_ppm helper function to calculate the new addend.

You forgot to collect my R-b tag... :-/

Quoted 4 lines
Cc Biju Das <[email protected]>
Cc Phil Edworthy <[email protected]>
Cc Lad Prabhakar <[email protected]>

[...]

MBR, Sergey

#11 lore raw

RE: [PATCH net-next v3 8/9] ptp: ravb: convert to .adjfine and adjust_by_scaled_ppm

Quoted 21 lines

-----Original Message-----
From: Sergey Shtylyov <[email protected]>
Sent: Friday, October 28, 2022 4:10 AM
To: Keller, Jacob E <[email protected]>; Jakub Kicinski <[email protected]>; David Miller <[email protected]>
Cc: [email protected]; Richard Cochran <[email protected]>; Biju Das <[email protected]>; Phil Edworthy <[email protected]>; Lad Prabhakar <prabhakar.mahadev- [email protected]>; [email protected]
Subject: Re: [PATCH net-next v3 8/9] ptp: ravb: convert to .adjfine and adjust_by_scaled_ppm

Hello!

Quoted 3 lines · Jacob Keller

The ravb implementation of .adjfreq is implemented in terms of a straight forward "base * ppb / 1 billion" calculation.

Quoted 8 lines
Quoted 6 lines

Convert this driver to .adjfine and use the adjust_by_scaled_ppm helper function to calculate the new addend.

You forgot to collect my R-b tag... :-/

Oops, I apologize for missing that.

Quoted 8 lines
Quoted 5 lines
Cc Biju Das <[email protected]>
Cc Phil Edworthy <[email protected]>
Cc Lad Prabhakar <[email protected]>

[...]

MBR, Sergey

#12 lore raw

PATCH v3 9/9 ptp: xgbe: convert to .adjfine and adjust_by_scaled_ppm

In reply to Jacob Keller

The xgbe implementation of .adjfreq is implemented in terms of a straight forward "base * ppb / 1 billion" calculation.

Convert this driver to .adjfine and use adjust_by_scaled_ppm to calculate the new addend value.

---
 drivers/net/ethernet/amd/xgbe/xgbe-ptp.c | 20 ++++----------------
 1 file changed, 4 insertions(+), 16 deletions(-)
drivers/net/ethernet/amd/xgbe/xgbe-ptp.c +4 -16
index d06d260cf1e2..7051bd7cf6dc 100644
@@ -134,27 +134,15 @@ static u64 xgbe_cc_read(const struct cyclecounter *cc)
 	return nsec;
 }
 
-static int xgbe_adjfreq(struct ptp_clock_info *info, s32 delta)
+static int xgbe_adjfine(struct ptp_clock_info *info, long scaled_ppm)
 {
 	struct xgbe_prv_data *pdata = container_of(info,
 						   struct xgbe_prv_data,
 						   ptp_clock_info);
 	unsigned long flags;
-	u64 adjust;
-	u32 addend, diff;
-	unsigned int neg_adjust = 0;
+	u64 addend;
 
-	if (delta < 0) {
-		neg_adjust = 1;
-		delta = -delta;
-	}
-
-	adjust = pdata->tstamp_addend;
-	adjust *= delta;
-	diff = div_u64(adjust, 1000000000UL);
-
-	addend = (neg_adjust) ? pdata->tstamp_addend - diff :
-				pdata->tstamp_addend + diff;
+	addend = adjust_by_scaled_ppm(pdata->tstamp_addend, scaled_ppm);
 
 	spin_lock_irqsave(&pdata->tstamp_lock, flags);
 
@@ -235,7 +223,7 @@ void xgbe_ptp_register(struct xgbe_prv_data *pdata)
 		 netdev_name(pdata->netdev));
 	info->owner = THIS_MODULE;
 	info->max_adj = pdata->ptpclk_rate;
-	info->adjfreq = xgbe_adjfreq;
+	info->adjfine = xgbe_adjfine;
 	info->adjtime = xgbe_adjtime;
 	info->gettime64 = xgbe_gettime;
 	info->settime64 = xgbe_settime;
Signature · 1 line
2.38.0.83.gd420dda05763
#13 lore raw
In reply to Jacob Keller
This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Quoted 9 lines · On Fri, 28 Oct 2022 04:04:11 -0700 you wrote:

Many drivers implementing PTP have not yet migrated to the new .adjfine frequency adjustment implementation.

A handful of these drivers use hardware with a simple increment value which is adjusted by multiplying by the adjustment factor and then dividing by 1 billion. This calculation is very easy to convert to .adjfine, by simply updating the divisor.

[...]

Here is the summary with links:
- [net-next,v3,1/9] ptp: add missing documentation for parameters

    https://git.kernel.org/netdev/net-next/c/b9a61b97798c
  - [net-next,v3,2/9] ptp: introduce helpers to adjust by scaled parts per million
    (no matching commit)
  - [net-next,v3,3/9] drivers: convert unsupported .adjfreq to .adjfine
    https://git.kernel.org/netdev/net-next/c/73aa29a2b119
  - [net-next,v3,4/9] ptp: mlx4: convert to .adjfine and adjust_by_scaled_ppm
    https://git.kernel.org/netdev/net-next/c/6ed795965ede
  - [net-next,v3,5/9] ptp: mlx5: convert to .adjfine and adjust_by_scaled_ppm
    https://git.kernel.org/netdev/net-next/c/d8aad3f3694f
  - [net-next,v3,6/9] ptp: lan743x: remove .adjfreq implementation
    https://git.kernel.org/netdev/net-next/c/c56dff6a9a31
  - [net-next,v3,7/9] ptp: lan743x: use diff_by_scaled_ppm in .adjfine implementation
    https://git.kernel.org/netdev/net-next/c/8bc900cbffa7
  - [net-next,v3,8/9] ptp: ravb: convert to .adjfine and adjust_by_scaled_ppm
    https://git.kernel.org/netdev/net-next/c/673dd2c78817
  - [net-next,v3,9/9] ptp: xgbe: convert to .adjfine and adjust_by_scaled_ppm
    https://git.kernel.org/netdev/net-next/c/337ffae0e4d6

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html