1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/mlx4/device.h>
35#include <linux/clocksource.h>
36
37#include "mlx4_en.h"
38
39
40
41static cycle_t mlx4_en_read_clock(const struct cyclecounter *tc)
42{
43 struct mlx4_en_dev *mdev =
44 container_of(tc, struct mlx4_en_dev, cycles);
45 struct mlx4_dev *dev = mdev->dev;
46
47 return mlx4_read_clock(dev) & tc->mask;
48}
49
50u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe)
51{
52 u64 hi, lo;
53 struct mlx4_ts_cqe *ts_cqe = (struct mlx4_ts_cqe *)cqe;
54
55 lo = (u64)be16_to_cpu(ts_cqe->timestamp_lo);
56 hi = ((u64)be32_to_cpu(ts_cqe->timestamp_hi) + !lo) << 16;
57
58 return hi | lo;
59}
60
61void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
62 struct skb_shared_hwtstamps *hwts,
63 u64 timestamp)
64{
65 unsigned long flags;
66 u64 nsec;
67
68 read_lock_irqsave(&mdev->clock_lock, flags);
69 nsec = timecounter_cyc2time(&mdev->clock, timestamp);
70 read_unlock_irqrestore(&mdev->clock_lock, flags);
71
72 memset(hwts, 0, sizeof(struct skb_shared_hwtstamps));
73 hwts->hwtstamp = ns_to_ktime(nsec);
74}
75
76
77
78
79
80
81
82void mlx4_en_remove_timestamp(struct mlx4_en_dev *mdev)
83{
84 if (mdev->ptp_clock) {
85 ptp_clock_unregister(mdev->ptp_clock);
86 mdev->ptp_clock = NULL;
87 mlx4_info(mdev, "removed PHC\n");
88 }
89}
90
91void mlx4_en_ptp_overflow_check(struct mlx4_en_dev *mdev)
92{
93 bool timeout = time_is_before_jiffies(mdev->last_overflow_check +
94 mdev->overflow_period);
95 unsigned long flags;
96
97 if (timeout) {
98 write_lock_irqsave(&mdev->clock_lock, flags);
99 timecounter_read(&mdev->clock);
100 write_unlock_irqrestore(&mdev->clock_lock, flags);
101 mdev->last_overflow_check = jiffies;
102 }
103}
104
105
106
107
108
109
110
111
112
113static int mlx4_en_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
114{
115 u64 adj;
116 u32 diff, mult;
117 int neg_adj = 0;
118 unsigned long flags;
119 struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
120 ptp_clock_info);
121
122 if (delta < 0) {
123 neg_adj = 1;
124 delta = -delta;
125 }
126 mult = mdev->nominal_c_mult;
127 adj = mult;
128 adj *= delta;
129 diff = div_u64(adj, 1000000000ULL);
130
131 write_lock_irqsave(&mdev->clock_lock, flags);
132 timecounter_read(&mdev->clock);
133 mdev->cycles.mult = neg_adj ? mult - diff : mult + diff;
134 write_unlock_irqrestore(&mdev->clock_lock, flags);
135
136 return 0;
137}
138
139
140
141
142
143
144
145
146static int mlx4_en_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
147{
148 struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
149 ptp_clock_info);
150 unsigned long flags;
151
152 write_lock_irqsave(&mdev->clock_lock, flags);
153 timecounter_adjtime(&mdev->clock, delta);
154 write_unlock_irqrestore(&mdev->clock_lock, flags);
155
156 return 0;
157}
158
159
160
161
162
163
164
165
166
167static int mlx4_en_phc_gettime(struct ptp_clock_info *ptp,
168 struct timespec64 *ts)
169{
170 struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
171 ptp_clock_info);
172 unsigned long flags;
173 u64 ns;
174
175 write_lock_irqsave(&mdev->clock_lock, flags);
176 ns = timecounter_read(&mdev->clock);
177 write_unlock_irqrestore(&mdev->clock_lock, flags);
178
179 *ts = ns_to_timespec64(ns);
180
181 return 0;
182}
183
184
185
186
187
188
189
190
191
192static int mlx4_en_phc_settime(struct ptp_clock_info *ptp,
193 const struct timespec64 *ts)
194{
195 struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
196 ptp_clock_info);
197 u64 ns = timespec64_to_ns(ts);
198 unsigned long flags;
199
200
201 write_lock_irqsave(&mdev->clock_lock, flags);
202 timecounter_init(&mdev->clock, &mdev->cycles, ns);
203 write_unlock_irqrestore(&mdev->clock_lock, flags);
204
205 return 0;
206}
207
208
209
210
211
212
213
214
215
216
217static int mlx4_en_phc_enable(struct ptp_clock_info __always_unused *ptp,
218 struct ptp_clock_request __always_unused *request,
219 int __always_unused on)
220{
221 return -EOPNOTSUPP;
222}
223
224static const struct ptp_clock_info mlx4_en_ptp_clock_info = {
225 .owner = THIS_MODULE,
226 .max_adj = 100000000,
227 .n_alarm = 0,
228 .n_ext_ts = 0,
229 .n_per_out = 0,
230 .n_pins = 0,
231 .pps = 0,
232 .adjfreq = mlx4_en_phc_adjfreq,
233 .adjtime = mlx4_en_phc_adjtime,
234 .gettime64 = mlx4_en_phc_gettime,
235 .settime64 = mlx4_en_phc_settime,
236 .enable = mlx4_en_phc_enable,
237};
238
239void mlx4_en_init_timestamp(struct mlx4_en_dev *mdev)
240{
241 struct mlx4_dev *dev = mdev->dev;
242 unsigned long flags;
243 u64 ns, zero = 0;
244
245 rwlock_init(&mdev->clock_lock);
246
247 memset(&mdev->cycles, 0, sizeof(mdev->cycles));
248 mdev->cycles.read = mlx4_en_read_clock;
249 mdev->cycles.mask = CLOCKSOURCE_MASK(48);
250
251
252
253
254
255 mdev->cycles.shift = 14;
256 mdev->cycles.mult =
257 clocksource_khz2mult(1000 * dev->caps.hca_core_clock, mdev->cycles.shift);
258 mdev->nominal_c_mult = mdev->cycles.mult;
259
260 write_lock_irqsave(&mdev->clock_lock, flags);
261 timecounter_init(&mdev->clock, &mdev->cycles,
262 ktime_to_ns(ktime_get_real()));
263 write_unlock_irqrestore(&mdev->clock_lock, flags);
264
265
266
267
268 ns = cyclecounter_cyc2ns(&mdev->cycles, mdev->cycles.mask, zero, &zero);
269 do_div(ns, NSEC_PER_SEC / 2 / HZ);
270 mdev->overflow_period = ns;
271
272
273 mdev->ptp_clock_info = mlx4_en_ptp_clock_info;
274 snprintf(mdev->ptp_clock_info.name, 16, "mlx4 ptp");
275
276 mdev->ptp_clock = ptp_clock_register(&mdev->ptp_clock_info,
277 &mdev->pdev->dev);
278 if (IS_ERR(mdev->ptp_clock)) {
279 mdev->ptp_clock = NULL;
280 mlx4_err(mdev, "ptp_clock_register failed\n");
281 } else {
282 mlx4_info(mdev, "registered PHC clock\n");
283 }
284
285}
286