]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
net/mlx5: Add devcom component for the clock shared by functions
authorJianbo Liu <jianbol@nvidia.com>
Mon, 3 Feb 2025 21:35:07 +0000 (23:35 +0200)
committerPaolo Abeni <pabeni@redhat.com>
Thu, 6 Feb 2025 09:14:01 +0000 (10:14 +0100)
Add new devcom component for hardware clock. When it is running in
real time mode, the functions are grouped by the identify they query.

According to firmware document, the clock identify size is 64 bits, so
it's safe to memcpy to component key, as the key size is also 64 bits.

Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c
drivers/net/ethernet/mellanox/mlx5/core/lib/devcom.h
include/linux/mlx5/driver.h

index da2a21ce8060ab7a89e3dfb5c10f6255f4aa6e84..7e5882ea19e0509a0471290bef40741362fa4053 100644 (file)
@@ -43,6 +43,8 @@
 #include <linux/cpufeature.h>
 #endif /* CONFIG_X86 */
 
+#define MLX5_RT_CLOCK_IDENTITY_SIZE MLX5_FLD_SZ_BYTES(mrtcq_reg, rt_clock_identity)
+
 enum {
        MLX5_PIN_MODE_IN                = 0x0,
        MLX5_PIN_MODE_OUT               = 0x1,
@@ -77,6 +79,10 @@ enum {
        MLX5_MTUTC_OPERATION_ADJUST_TIME_EXTENDED_MAX = 200000,
 };
 
+struct mlx5_clock_dev_state {
+       struct mlx5_devcom_comp_dev *compdev;
+};
+
 struct mlx5_clock_priv {
        struct mlx5_clock clock;
        struct mlx5_core_dev *mdev;
@@ -109,6 +115,22 @@ static bool mlx5_modify_mtutc_allowed(struct mlx5_core_dev *mdev)
        return MLX5_CAP_MCAM_FEATURE(mdev, ptpcyc2realtime_modify);
 }
 
+static int mlx5_clock_identity_get(struct mlx5_core_dev *mdev,
+                                  u8 identify[MLX5_RT_CLOCK_IDENTITY_SIZE])
+{
+       u32 out[MLX5_ST_SZ_DW(mrtcq_reg)] = {};
+       u32 in[MLX5_ST_SZ_DW(mrtcq_reg)] = {};
+       int err;
+
+       err = mlx5_core_access_reg(mdev, in, sizeof(in),
+                                  out, sizeof(out), MLX5_REG_MRTCQ, 0, 0);
+       if (!err)
+               memcpy(identify, MLX5_ADDR_OF(mrtcq_reg, out, rt_clock_identity),
+                      MLX5_RT_CLOCK_IDENTITY_SIZE);
+
+       return err;
+}
+
 static u32 mlx5_ptp_shift_constant(u32 dev_freq_khz)
 {
        /* Optimal shift constant leads to corrections above just 1 scaled ppm.
@@ -1231,11 +1253,26 @@ static int mlx5_clock_alloc(struct mlx5_core_dev *mdev)
        return 0;
 }
 
+static void mlx5_shared_clock_register(struct mlx5_core_dev *mdev, u64 key)
+{
+       mdev->clock_state->compdev = mlx5_devcom_register_component(mdev->priv.devc,
+                                                                   MLX5_DEVCOM_SHARED_CLOCK,
+                                                                   key, NULL, mdev);
+}
+
+static void mlx5_shared_clock_unregister(struct mlx5_core_dev *mdev)
+{
+       mlx5_devcom_unregister_component(mdev->clock_state->compdev);
+}
+
 static struct mlx5_clock null_clock;
 
 int mlx5_init_clock(struct mlx5_core_dev *mdev)
 {
+       u8 identity[MLX5_RT_CLOCK_IDENTITY_SIZE];
+       struct mlx5_clock_dev_state *clock_state;
        struct mlx5_clock *clock;
+       u64 key;
        int err;
 
        if (!MLX5_CAP_GEN(mdev, device_frequency_khz)) {
@@ -1244,9 +1281,26 @@ int mlx5_init_clock(struct mlx5_core_dev *mdev)
                return 0;
        }
 
+       clock_state = kzalloc(sizeof(*clock_state), GFP_KERNEL);
+       if (!clock_state)
+               return -ENOMEM;
+       mdev->clock_state = clock_state;
+
+       if (MLX5_CAP_MCAM_REG3(mdev, mrtcq) && mlx5_real_time_mode(mdev)) {
+               if (mlx5_clock_identity_get(mdev, identity)) {
+                       mlx5_core_warn(mdev, "failed to get rt clock identity, create ptp dev per function\n");
+               } else {
+                       memcpy(&key, &identity, sizeof(key));
+                       mlx5_shared_clock_register(mdev, key);
+               }
+       }
+
        err = mlx5_clock_alloc(mdev);
-       if (err)
+       if (err) {
+               kfree(clock_state);
+               mdev->clock_state = NULL;
                return err;
+       }
        clock = mdev->clock;
 
        INIT_WORK(&clock->pps_info.out_work, mlx5_pps_out);
@@ -1267,4 +1321,7 @@ void mlx5_cleanup_clock(struct mlx5_core_dev *mdev)
        cancel_work_sync(&clock->pps_info.out_work);
 
        mlx5_clock_free(mdev);
+       mlx5_shared_clock_unregister(mdev);
+       kfree(mdev->clock_state);
+       mdev->clock_state = NULL;
 }
index d58032dd0df74c108b78d66bd21a89b31663c5f9..c79699b94a0254f9ab071aac4b0971f13f1a433b 100644 (file)
@@ -11,6 +11,7 @@ enum mlx5_devcom_component {
        MLX5_DEVCOM_MPV,
        MLX5_DEVCOM_HCA_PORTS,
        MLX5_DEVCOM_SD_GROUP,
+       MLX5_DEVCOM_SHARED_CLOCK,
        MLX5_DEVCOM_NUM_COMPONENTS,
 };
 
index 5dab3d8d05e4f957e6ee6709ab59f448ef3bc12a..46bd7550adf81b0f24590dc7ecd625ec5163df00 100644 (file)
@@ -679,6 +679,7 @@ struct mlx5_rsvd_gids {
 };
 
 struct mlx5_clock;
+struct mlx5_clock_dev_state;
 struct mlx5_dm;
 struct mlx5_fw_tracer;
 struct mlx5_vxlan;
@@ -763,6 +764,7 @@ struct mlx5_core_dev {
        struct mlx5_fpga_device *fpga;
 #endif
        struct mlx5_clock       *clock;
+       struct mlx5_clock_dev_state *clock_state;
        struct mlx5_ib_clock_info  *clock_info;
        struct mlx5_fw_tracer   *tracer;
        struct mlx5_rsc_dump    *rsc_dump;