@@ -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+
8090static 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
8595static 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)
281291static 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
600610static 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
902912static 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
930940static 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
10261036static 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)
10441054static 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
10781088static 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
11011111static 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
11201130static 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
11571167static 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
11811191static 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
12141259void 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}
0 commit comments