GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / net / ethernet / mellanox / mlx4 / en_clock.c
1 /*
2  * Copyright (c) 2012 Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33
34 #include <linux/mlx4/device.h>
35 #include <linux/clocksource.h>
36
37 #include "mlx4_en.h"
38
39 /* mlx4_en_read_clock - read raw cycle counter (to be used by time counter)
40  */
41 static 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
50 u64 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
61 void 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  * mlx4_en_remove_timestamp - disable PTP device
78  * @mdev: board private structure
79  *
80  * Stop the PTP support.
81  **/
82 void 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
91 #define MLX4_EN_WRAP_AROUND_SEC 10UL
92 /* By scheduling the overflow check every 5 seconds, we have a reasonably
93  * good chance we wont miss a wrap around.
94  * TOTO: Use a timer instead of a work queue to increase the guarantee.
95  */
96 #define MLX4_EN_OVERFLOW_PERIOD (MLX4_EN_WRAP_AROUND_SEC * HZ / 2)
97
98 void mlx4_en_ptp_overflow_check(struct mlx4_en_dev *mdev)
99 {
100         bool timeout = time_is_before_jiffies(mdev->last_overflow_check +
101                                               MLX4_EN_OVERFLOW_PERIOD);
102         unsigned long flags;
103
104         if (timeout) {
105                 write_lock_irqsave(&mdev->clock_lock, flags);
106                 timecounter_read(&mdev->clock);
107                 write_unlock_irqrestore(&mdev->clock_lock, flags);
108                 mdev->last_overflow_check = jiffies;
109         }
110 }
111
112 /**
113  * mlx4_en_phc_adjfreq - adjust the frequency of the hardware clock
114  * @ptp: ptp clock structure
115  * @delta: Desired frequency change in parts per billion
116  *
117  * Adjust the frequency of the PHC cycle counter by the indicated delta from
118  * the base frequency.
119  **/
120 static int mlx4_en_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
121 {
122         u64 adj;
123         u32 diff, mult;
124         int neg_adj = 0;
125         unsigned long flags;
126         struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
127                                                 ptp_clock_info);
128
129         if (delta < 0) {
130                 neg_adj = 1;
131                 delta = -delta;
132         }
133         mult = mdev->nominal_c_mult;
134         adj = mult;
135         adj *= delta;
136         diff = div_u64(adj, 1000000000ULL);
137
138         write_lock_irqsave(&mdev->clock_lock, flags);
139         timecounter_read(&mdev->clock);
140         mdev->cycles.mult = neg_adj ? mult - diff : mult + diff;
141         write_unlock_irqrestore(&mdev->clock_lock, flags);
142
143         return 0;
144 }
145
146 /**
147  * mlx4_en_phc_adjtime - Shift the time of the hardware clock
148  * @ptp: ptp clock structure
149  * @delta: Desired change in nanoseconds
150  *
151  * Adjust the timer by resetting the timecounter structure.
152  **/
153 static int mlx4_en_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
154 {
155         struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
156                                                 ptp_clock_info);
157         unsigned long flags;
158
159         write_lock_irqsave(&mdev->clock_lock, flags);
160         timecounter_adjtime(&mdev->clock, delta);
161         write_unlock_irqrestore(&mdev->clock_lock, flags);
162
163         return 0;
164 }
165
166 /**
167  * mlx4_en_phc_gettime - Reads the current time from the hardware clock
168  * @ptp: ptp clock structure
169  * @ts: timespec structure to hold the current time value
170  *
171  * Read the timecounter and return the correct value in ns after converting
172  * it into a struct timespec.
173  **/
174 static int mlx4_en_phc_gettime(struct ptp_clock_info *ptp,
175                                struct timespec64 *ts)
176 {
177         struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
178                                                 ptp_clock_info);
179         unsigned long flags;
180         u64 ns;
181
182         write_lock_irqsave(&mdev->clock_lock, flags);
183         ns = timecounter_read(&mdev->clock);
184         write_unlock_irqrestore(&mdev->clock_lock, flags);
185
186         *ts = ns_to_timespec64(ns);
187
188         return 0;
189 }
190
191 /**
192  * mlx4_en_phc_settime - Set the current time on the hardware clock
193  * @ptp: ptp clock structure
194  * @ts: timespec containing the new time for the cycle counter
195  *
196  * Reset the timecounter to use a new base value instead of the kernel
197  * wall timer value.
198  **/
199 static int mlx4_en_phc_settime(struct ptp_clock_info *ptp,
200                                const struct timespec64 *ts)
201 {
202         struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
203                                                 ptp_clock_info);
204         u64 ns = timespec64_to_ns(ts);
205         unsigned long flags;
206
207         /* reset the timecounter */
208         write_lock_irqsave(&mdev->clock_lock, flags);
209         timecounter_init(&mdev->clock, &mdev->cycles, ns);
210         write_unlock_irqrestore(&mdev->clock_lock, flags);
211
212         return 0;
213 }
214
215 /**
216  * mlx4_en_phc_enable - enable or disable an ancillary feature
217  * @ptp: ptp clock structure
218  * @request: Desired resource to enable or disable
219  * @on: Caller passes one to enable or zero to disable
220  *
221  * Enable (or disable) ancillary features of the PHC subsystem.
222  * Currently, no ancillary features are supported.
223  **/
224 static int mlx4_en_phc_enable(struct ptp_clock_info __always_unused *ptp,
225                               struct ptp_clock_request __always_unused *request,
226                               int __always_unused on)
227 {
228         return -EOPNOTSUPP;
229 }
230
231 static const struct ptp_clock_info mlx4_en_ptp_clock_info = {
232         .owner          = THIS_MODULE,
233         .max_adj        = 100000000,
234         .n_alarm        = 0,
235         .n_ext_ts       = 0,
236         .n_per_out      = 0,
237         .n_pins         = 0,
238         .pps            = 0,
239         .adjfreq        = mlx4_en_phc_adjfreq,
240         .adjtime        = mlx4_en_phc_adjtime,
241         .gettime64      = mlx4_en_phc_gettime,
242         .settime64      = mlx4_en_phc_settime,
243         .enable         = mlx4_en_phc_enable,
244 };
245
246
247 /* This function calculates the max shift that enables the user range
248  * of MLX4_EN_WRAP_AROUND_SEC values in the cycles register.
249  */
250 static u32 freq_to_shift(u16 freq)
251 {
252         u32 freq_khz = freq * 1000;
253         u64 max_val_cycles = freq_khz * 1000 * MLX4_EN_WRAP_AROUND_SEC;
254         u64 max_val_cycles_rounded = 1ULL << fls64(max_val_cycles - 1);
255         /* calculate max possible multiplier in order to fit in 64bit */
256         u64 max_mul = div64_u64(ULLONG_MAX, max_val_cycles_rounded);
257
258         /* This comes from the reverse of clocksource_khz2mult */
259         return ilog2(div_u64(max_mul * freq_khz, 1000000));
260 }
261
262 void mlx4_en_init_timestamp(struct mlx4_en_dev *mdev)
263 {
264         struct mlx4_dev *dev = mdev->dev;
265         unsigned long flags;
266
267         /* mlx4_en_init_timestamp is called for each netdev.
268          * mdev->ptp_clock is common for all ports, skip initialization if
269          * was done for other port.
270          */
271         if (mdev->ptp_clock)
272                 return;
273
274         rwlock_init(&mdev->clock_lock);
275
276         memset(&mdev->cycles, 0, sizeof(mdev->cycles));
277         mdev->cycles.read = mlx4_en_read_clock;
278         mdev->cycles.mask = CLOCKSOURCE_MASK(48);
279         mdev->cycles.shift = freq_to_shift(dev->caps.hca_core_clock);
280         mdev->cycles.mult =
281                 clocksource_khz2mult(1000 * dev->caps.hca_core_clock, mdev->cycles.shift);
282         mdev->nominal_c_mult = mdev->cycles.mult;
283
284         write_lock_irqsave(&mdev->clock_lock, flags);
285         timecounter_init(&mdev->clock, &mdev->cycles,
286                          ktime_to_ns(ktime_get_real()));
287         write_unlock_irqrestore(&mdev->clock_lock, flags);
288
289         /* Configure the PHC */
290         mdev->ptp_clock_info = mlx4_en_ptp_clock_info;
291         snprintf(mdev->ptp_clock_info.name, 16, "mlx4 ptp");
292
293         mdev->ptp_clock = ptp_clock_register(&mdev->ptp_clock_info,
294                                              &mdev->pdev->dev);
295         if (IS_ERR(mdev->ptp_clock)) {
296                 mdev->ptp_clock = NULL;
297                 mlx4_err(mdev, "ptp_clock_register failed\n");
298         } else if (mdev->ptp_clock) {
299                 mlx4_info(mdev, "registered PHC clock\n");
300         }
301
302 }