Skip to content

Commit f9beaf4

Browse files
Jianbo LiuPaolo Abeni
authored andcommitted
net/mlx5: Change clock in mlx5_core_dev to mlx5_clock pointer
Change clock member in mlx5_core_dev to a pointer, so it can point to a clock shared by multiple functions in later patch. For now, each function has its own clock, so mdev in mlx5_clock_priv is the back pointer to the function. Later it points to one (normally the first one) of the multiple functions sharing the same clock. Change mlx5_init_clock() to return error if mlx5_clock is not allocated. Besides, a null clock is defined and used when hardware clock is not supported. So, the clock pointer is always pointing to something valid. 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>
1 parent 355f58f commit f9beaf4

File tree

9 files changed

+116
-64
lines changed

9 files changed

+116
-64
lines changed

drivers/net/ethernet/mellanox/mlx5/core/en/ptp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ static int mlx5e_ptp_alloc_txqsq(struct mlx5e_ptp *c, int txq_ix,
326326
int node;
327327

328328
sq->pdev = c->pdev;
329-
sq->clock = &mdev->clock;
329+
sq->clock = mdev->clock;
330330
sq->mkey_be = c->mkey_be;
331331
sq->netdev = c->netdev;
332332
sq->priv = c->priv;
@@ -696,7 +696,7 @@ static int mlx5e_init_ptp_rq(struct mlx5e_ptp *c, struct mlx5e_params *params,
696696
rq->pdev = c->pdev;
697697
rq->netdev = priv->netdev;
698698
rq->priv = priv;
699-
rq->clock = &mdev->clock;
699+
rq->clock = mdev->clock;
700700
rq->tstamp = &priv->tstamp;
701701
rq->mdev = mdev;
702702
rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);

drivers/net/ethernet/mellanox/mlx5/core/en/trap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void mlx5e_init_trap_rq(struct mlx5e_trap *t, struct mlx5e_params *params
4646
rq->pdev = t->pdev;
4747
rq->netdev = priv->netdev;
4848
rq->priv = priv;
49-
rq->clock = &mdev->clock;
49+
rq->clock = mdev->clock;
5050
rq->tstamp = &priv->tstamp;
5151
rq->mdev = mdev;
5252
rq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);

drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ static u64 mlx5e_xsk_fill_timestamp(void *_priv)
289289
ts = get_cqe_ts(priv->cqe);
290290

291291
if (mlx5_is_real_time_rq(priv->cq->mdev) || mlx5_is_real_time_sq(priv->cq->mdev))
292-
return mlx5_real_time_cyc2time(&priv->cq->mdev->clock, ts);
292+
return mlx5_real_time_cyc2time(priv->cq->mdev->clock, ts);
293293

294-
return mlx5_timecounter_cyc2time(&priv->cq->mdev->clock, ts);
294+
return mlx5_timecounter_cyc2time(priv->cq->mdev->clock, ts);
295295
}
296296

297297
static void mlx5e_xsk_request_checksum(u16 csum_start, u16 csum_offset, void *priv)

drivers/net/ethernet/mellanox/mlx5/core/en/xsk/setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static int mlx5e_init_xsk_rq(struct mlx5e_channel *c,
7272
rq->netdev = c->netdev;
7373
rq->priv = c->priv;
7474
rq->tstamp = c->tstamp;
75-
rq->clock = &mdev->clock;
75+
rq->clock = mdev->clock;
7676
rq->icosq = &c->icosq;
7777
rq->ix = c->ix;
7878
rq->channel = c;

drivers/net/ethernet/mellanox/mlx5/core/en_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ static int mlx5e_init_rxq_rq(struct mlx5e_channel *c, struct mlx5e_params *param
737737
rq->netdev = c->netdev;
738738
rq->priv = c->priv;
739739
rq->tstamp = c->tstamp;
740-
rq->clock = &mdev->clock;
740+
rq->clock = mdev->clock;
741741
rq->icosq = &c->icosq;
742742
rq->ix = c->ix;
743743
rq->channel = c;
@@ -1614,7 +1614,7 @@ static int mlx5e_alloc_txqsq(struct mlx5e_channel *c,
16141614
int err;
16151615

16161616
sq->pdev = c->pdev;
1617-
sq->clock = &mdev->clock;
1617+
sq->clock = mdev->clock;
16181618
sq->mkey_be = c->mkey_be;
16191619
sq->netdev = c->netdev;
16201620
sq->mdev = c->mdev;

drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,19 @@ enum {
7777
MLX5_MTUTC_OPERATION_ADJUST_TIME_EXTENDED_MAX = 200000,
7878
};
7979

80+
struct mlx5_clock_priv {
81+
struct mlx5_clock clock;
82+
struct mlx5_core_dev *mdev;
83+
};
84+
85+
static struct mlx5_clock_priv *clock_priv(struct mlx5_clock *clock)
86+
{
87+
return container_of(clock, struct mlx5_clock_priv, clock);
88+
}
89+
8090
static struct mlx5_core_dev *mlx5_clock_mdev_get(struct mlx5_clock *clock)
8191
{
82-
return container_of(clock, struct mlx5_core_dev, clock);
92+
return clock_priv(clock)->mdev;
8393
}
8494

8595
static bool mlx5_real_time_mode(struct mlx5_core_dev *mdev)
@@ -219,7 +229,7 @@ static int mlx5_mtctr_syncdevicetime(ktime_t *device_time,
219229
if (real_time_mode)
220230
*device_time = ns_to_ktime(REAL_TIME_TO_NS(device >> 32, device & U32_MAX));
221231
else
222-
*device_time = mlx5_timecounter_cyc2time(&mdev->clock, device);
232+
*device_time = mlx5_timecounter_cyc2time(mdev->clock, device);
223233

224234
return 0;
225235
}
@@ -281,7 +291,7 @@ static u64 read_internal_timer(const struct cyclecounter *cc)
281291
static void mlx5_update_clock_info_page(struct mlx5_core_dev *mdev)
282292
{
283293
struct mlx5_ib_clock_info *clock_info = mdev->clock_info;
284-
struct mlx5_clock *clock = &mdev->clock;
294+
struct mlx5_clock *clock = mdev->clock;
285295
struct mlx5_timer *timer;
286296
u32 sign;
287297

@@ -599,7 +609,7 @@ static int mlx5_extts_configure(struct ptp_clock_info *ptp,
599609

600610
static u64 find_target_cycles(struct mlx5_core_dev *mdev, s64 target_ns)
601611
{
602-
struct mlx5_clock *clock = &mdev->clock;
612+
struct mlx5_clock *clock = mdev->clock;
603613
u64 cycles_now, cycles_delta;
604614
u64 nsec_now, nsec_delta;
605615
struct mlx5_timer *timer;
@@ -658,7 +668,7 @@ static int mlx5_perout_conf_out_pulse_duration(struct mlx5_core_dev *mdev,
658668
struct ptp_clock_request *rq,
659669
u32 *out_pulse_duration_ns)
660670
{
661-
struct mlx5_pps *pps_info = &mdev->clock.pps_info;
671+
struct mlx5_pps *pps_info = &mdev->clock->pps_info;
662672
u32 out_pulse_duration;
663673
struct timespec64 ts;
664674

@@ -691,7 +701,7 @@ static int perout_conf_npps_real_time(struct mlx5_core_dev *mdev, struct ptp_clo
691701
u32 *field_select, u32 *out_pulse_duration_ns,
692702
u64 *period, u64 *time_stamp)
693703
{
694-
struct mlx5_pps *pps_info = &mdev->clock.pps_info;
704+
struct mlx5_pps *pps_info = &mdev->clock->pps_info;
695705
struct ptp_clock_time *time = &rq->perout.start;
696706
struct timespec64 ts;
697707

@@ -901,7 +911,7 @@ static int mlx5_get_pps_pin_mode(struct mlx5_core_dev *mdev, u8 pin)
901911

902912
static void mlx5_init_pin_config(struct mlx5_core_dev *mdev)
903913
{
904-
struct mlx5_clock *clock = &mdev->clock;
914+
struct mlx5_clock *clock = mdev->clock;
905915
int i;
906916

907917
if (!clock->ptp_info.n_pins)
@@ -929,8 +939,8 @@ static void mlx5_init_pin_config(struct mlx5_core_dev *mdev)
929939

930940
static void mlx5_get_pps_caps(struct mlx5_core_dev *mdev)
931941
{
932-
struct mlx5_clock *clock = &mdev->clock;
933942
u32 out[MLX5_ST_SZ_DW(mtpps_reg)] = {0};
943+
struct mlx5_clock *clock = mdev->clock;
934944

935945
mlx5_query_mtpps(mdev, out, sizeof(out));
936946

@@ -1025,7 +1035,7 @@ static int mlx5_pps_event(struct notifier_block *nb,
10251035

10261036
static void mlx5_timecounter_init(struct mlx5_core_dev *mdev)
10271037
{
1028-
struct mlx5_clock *clock = &mdev->clock;
1038+
struct mlx5_clock *clock = mdev->clock;
10291039
struct mlx5_timer *timer = &clock->timer;
10301040
u32 dev_freq;
10311041

@@ -1044,7 +1054,7 @@ static void mlx5_timecounter_init(struct mlx5_core_dev *mdev)
10441054
static void mlx5_init_overflow_period(struct mlx5_core_dev *mdev)
10451055
{
10461056
struct mlx5_ib_clock_info *clock_info = mdev->clock_info;
1047-
struct mlx5_clock *clock = &mdev->clock;
1057+
struct mlx5_clock *clock = mdev->clock;
10481058
struct mlx5_timer *timer = &clock->timer;
10491059
u64 overflow_cycles;
10501060
u64 frac = 0;
@@ -1077,7 +1087,7 @@ static void mlx5_init_overflow_period(struct mlx5_core_dev *mdev)
10771087

10781088
static void mlx5_init_clock_info(struct mlx5_core_dev *mdev)
10791089
{
1080-
struct mlx5_clock *clock = &mdev->clock;
1090+
struct mlx5_clock *clock = mdev->clock;
10811091
struct mlx5_ib_clock_info *info;
10821092
struct mlx5_timer *timer;
10831093

@@ -1100,7 +1110,7 @@ static void mlx5_init_clock_info(struct mlx5_core_dev *mdev)
11001110

11011111
static void mlx5_init_timer_max_freq_adjustment(struct mlx5_core_dev *mdev)
11021112
{
1103-
struct mlx5_clock *clock = &mdev->clock;
1113+
struct mlx5_clock *clock = mdev->clock;
11041114
u32 out[MLX5_ST_SZ_DW(mtutc_reg)] = {};
11051115
u32 in[MLX5_ST_SZ_DW(mtutc_reg)] = {};
11061116
u8 log_max_freq_adjustment = 0;
@@ -1119,7 +1129,7 @@ static void mlx5_init_timer_max_freq_adjustment(struct mlx5_core_dev *mdev)
11191129

11201130
static void mlx5_init_timer_clock(struct mlx5_core_dev *mdev)
11211131
{
1122-
struct mlx5_clock *clock = &mdev->clock;
1132+
struct mlx5_clock *clock = mdev->clock;
11231133

11241134
/* Configure the PHC */
11251135
clock->ptp_info = mlx5_ptp_clock_info;
@@ -1156,7 +1166,7 @@ static void mlx5_init_pps(struct mlx5_core_dev *mdev)
11561166

11571167
static void mlx5_init_clock_dev(struct mlx5_core_dev *mdev)
11581168
{
1159-
struct mlx5_clock *clock = &mdev->clock;
1169+
struct mlx5_clock *clock = mdev->clock;
11601170

11611171
seqlock_init(&clock->lock);
11621172

@@ -1180,7 +1190,7 @@ static void mlx5_init_clock_dev(struct mlx5_core_dev *mdev)
11801190

11811191
static void mlx5_destroy_clock_dev(struct mlx5_core_dev *mdev)
11821192
{
1183-
struct mlx5_clock *clock = &mdev->clock;
1193+
struct mlx5_clock *clock = mdev->clock;
11841194

11851195
if (clock->ptp) {
11861196
ptp_clock_unregister(clock->ptp);
@@ -1195,31 +1205,66 @@ static void mlx5_destroy_clock_dev(struct mlx5_core_dev *mdev)
11951205
kfree(clock->ptp_info.pin_config);
11961206
}
11971207

1198-
void mlx5_init_clock(struct mlx5_core_dev *mdev)
1208+
static void mlx5_clock_free(struct mlx5_core_dev *mdev)
1209+
{
1210+
struct mlx5_clock_priv *cpriv = clock_priv(mdev->clock);
1211+
1212+
mlx5_destroy_clock_dev(mdev);
1213+
kfree(cpriv);
1214+
mdev->clock = NULL;
1215+
}
1216+
1217+
static int mlx5_clock_alloc(struct mlx5_core_dev *mdev)
11991218
{
1200-
struct mlx5_clock *clock = &mdev->clock;
1219+
struct mlx5_clock_priv *cpriv;
1220+
struct mlx5_clock *clock;
1221+
1222+
cpriv = kzalloc(sizeof(*cpriv), GFP_KERNEL);
1223+
if (!cpriv)
1224+
return -ENOMEM;
1225+
1226+
cpriv->mdev = mdev;
1227+
clock = &cpriv->clock;
1228+
mdev->clock = clock;
1229+
mlx5_init_clock_dev(mdev);
1230+
1231+
return 0;
1232+
}
1233+
1234+
static struct mlx5_clock null_clock;
1235+
1236+
int mlx5_init_clock(struct mlx5_core_dev *mdev)
1237+
{
1238+
struct mlx5_clock *clock;
1239+
int err;
12011240

12021241
if (!MLX5_CAP_GEN(mdev, device_frequency_khz)) {
1242+
mdev->clock = &null_clock;
12031243
mlx5_core_warn(mdev, "invalid device_frequency_khz, aborting HW clock init\n");
1204-
return;
1244+
return 0;
12051245
}
12061246

1207-
mlx5_init_clock_dev(mdev);
1247+
err = mlx5_clock_alloc(mdev);
1248+
if (err)
1249+
return err;
1250+
clock = mdev->clock;
12081251

12091252
INIT_WORK(&clock->pps_info.out_work, mlx5_pps_out);
12101253
MLX5_NB_INIT(&clock->pps_nb, mlx5_pps_event, PPS_EVENT);
12111254
mlx5_eq_notifier_register(mdev, &clock->pps_nb);
1255+
1256+
return 0;
12121257
}
12131258

12141259
void mlx5_cleanup_clock(struct mlx5_core_dev *mdev)
12151260
{
1216-
struct mlx5_clock *clock = &mdev->clock;
1261+
struct mlx5_clock *clock = mdev->clock;
12171262

12181263
if (!MLX5_CAP_GEN(mdev, device_frequency_khz))
12191264
return;
12201265

12211266
mlx5_eq_notifier_unregister(mdev, &clock->pps_nb);
12221267
cancel_work_sync(&clock->pps_info.out_work);
12231268

1224-
mlx5_destroy_clock_dev(mdev);
1269+
mlx5_clock_free(mdev);
12251270
}

drivers/net/ethernet/mellanox/mlx5/core/lib/clock.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,35 @@
3333
#ifndef __LIB_CLOCK_H__
3434
#define __LIB_CLOCK_H__
3535

36+
#include <linux/ptp_clock_kernel.h>
37+
38+
#define MAX_PIN_NUM 8
39+
struct mlx5_pps {
40+
u8 pin_caps[MAX_PIN_NUM];
41+
struct work_struct out_work;
42+
u64 start[MAX_PIN_NUM];
43+
u8 enabled;
44+
u64 min_npps_period;
45+
u64 min_out_pulse_duration_ns;
46+
};
47+
48+
struct mlx5_timer {
49+
struct cyclecounter cycles;
50+
struct timecounter tc;
51+
u32 nominal_c_mult;
52+
unsigned long overflow_period;
53+
};
54+
55+
struct mlx5_clock {
56+
struct mlx5_nb pps_nb;
57+
seqlock_t lock;
58+
struct hwtstamp_config hwtstamp_config;
59+
struct ptp_clock *ptp;
60+
struct ptp_clock_info ptp_info;
61+
struct mlx5_pps pps_info;
62+
struct mlx5_timer timer;
63+
};
64+
3665
static inline bool mlx5_is_real_time_rq(struct mlx5_core_dev *mdev)
3766
{
3867
u8 rq_ts_format_cap = MLX5_CAP_GEN(mdev, rq_ts_format);
@@ -54,12 +83,12 @@ static inline bool mlx5_is_real_time_sq(struct mlx5_core_dev *mdev)
5483
typedef ktime_t (*cqe_ts_to_ns)(struct mlx5_clock *, u64);
5584

5685
#if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
57-
void mlx5_init_clock(struct mlx5_core_dev *mdev);
86+
int mlx5_init_clock(struct mlx5_core_dev *mdev);
5887
void mlx5_cleanup_clock(struct mlx5_core_dev *mdev);
5988

6089
static inline int mlx5_clock_get_ptp_index(struct mlx5_core_dev *mdev)
6190
{
62-
return mdev->clock.ptp ? ptp_clock_index(mdev->clock.ptp) : -1;
91+
return mdev->clock->ptp ? ptp_clock_index(mdev->clock->ptp) : -1;
6392
}
6493

6594
static inline ktime_t mlx5_timecounter_cyc2time(struct mlx5_clock *clock,
@@ -87,7 +116,7 @@ static inline ktime_t mlx5_real_time_cyc2time(struct mlx5_clock *clock,
87116
return ns_to_ktime(time);
88117
}
89118
#else
90-
static inline void mlx5_init_clock(struct mlx5_core_dev *mdev) {}
119+
static inline int mlx5_init_clock(struct mlx5_core_dev *mdev) { return 0; }
91120
static inline void mlx5_cleanup_clock(struct mlx5_core_dev *mdev) {}
92121
static inline int mlx5_clock_get_ptp_index(struct mlx5_core_dev *mdev)
93122
{

drivers/net/ethernet/mellanox/mlx5/core/main.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,15 +1038,19 @@ static int mlx5_init_once(struct mlx5_core_dev *dev)
10381038

10391039
mlx5_init_reserved_gids(dev);
10401040

1041-
mlx5_init_clock(dev);
1041+
err = mlx5_init_clock(dev);
1042+
if (err) {
1043+
mlx5_core_err(dev, "failed to initialize hardware clock\n");
1044+
goto err_tables_cleanup;
1045+
}
10421046

10431047
dev->vxlan = mlx5_vxlan_create(dev);
10441048
dev->geneve = mlx5_geneve_create(dev);
10451049

10461050
err = mlx5_init_rl_table(dev);
10471051
if (err) {
10481052
mlx5_core_err(dev, "Failed to init rate limiting\n");
1049-
goto err_tables_cleanup;
1053+
goto err_clock_cleanup;
10501054
}
10511055

10521056
err = mlx5_mpfs_init(dev);
@@ -1123,10 +1127,11 @@ static int mlx5_init_once(struct mlx5_core_dev *dev)
11231127
mlx5_mpfs_cleanup(dev);
11241128
err_rl_cleanup:
11251129
mlx5_cleanup_rl_table(dev);
1126-
err_tables_cleanup:
1130+
err_clock_cleanup:
11271131
mlx5_geneve_destroy(dev->geneve);
11281132
mlx5_vxlan_destroy(dev->vxlan);
11291133
mlx5_cleanup_clock(dev);
1134+
err_tables_cleanup:
11301135
mlx5_cleanup_reserved_gids(dev);
11311136
mlx5_cq_debugfs_cleanup(dev);
11321137
mlx5_fw_reset_cleanup(dev);

0 commit comments

Comments
 (0)