2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/clocksource.h>
37 MLX5E_CYCLES_SHIFT = 23
41 MLX5E_PIN_MODE_IN = 0x0,
42 MLX5E_PIN_MODE_OUT = 0x1,
46 MLX5E_OUT_PATTERN_PULSE = 0x0,
47 MLX5E_OUT_PATTERN_PERIODIC = 0x1,
51 MLX5E_EVENT_MODE_DISABLE = 0x0,
52 MLX5E_EVENT_MODE_REPETETIVE = 0x1,
53 MLX5E_EVENT_MODE_ONCE_TILL_ARM = 0x2,
56 void mlx5e_fill_hwstamp(struct mlx5e_tstamp *tstamp, u64 timestamp,
57 struct skb_shared_hwtstamps *hwts)
61 read_lock(&tstamp->lock);
62 nsec = timecounter_cyc2time(&tstamp->clock, timestamp);
63 read_unlock(&tstamp->lock);
65 hwts->hwtstamp = ns_to_ktime(nsec);
68 static u64 mlx5e_read_internal_timer(const struct cyclecounter *cc)
70 struct mlx5e_tstamp *tstamp = container_of(cc, struct mlx5e_tstamp,
73 return mlx5_read_internal_timer(tstamp->mdev) & cc->mask;
76 static void mlx5e_timestamp_overflow(struct work_struct *work)
78 struct delayed_work *dwork = to_delayed_work(work);
79 struct mlx5e_tstamp *tstamp = container_of(dwork, struct mlx5e_tstamp,
83 write_lock_irqsave(&tstamp->lock, flags);
84 timecounter_read(&tstamp->clock);
85 write_unlock_irqrestore(&tstamp->lock, flags);
86 schedule_delayed_work(&tstamp->overflow_work, tstamp->overflow_period);
89 int mlx5e_hwstamp_set(struct net_device *dev, struct ifreq *ifr)
91 struct mlx5e_priv *priv = netdev_priv(dev);
92 struct hwtstamp_config config;
95 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
98 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
101 /* TX HW timestamp */
102 switch (config.tx_type) {
103 case HWTSTAMP_TX_OFF:
110 mutex_lock(&priv->state_lock);
111 /* RX HW timestamp */
112 switch (config.rx_filter) {
113 case HWTSTAMP_FILTER_NONE:
114 /* Reset CQE compression to Admin default */
115 mlx5e_modify_rx_cqe_compression_locked(priv, priv->channels.params.rx_cqe_compress_def);
117 case HWTSTAMP_FILTER_ALL:
118 case HWTSTAMP_FILTER_SOME:
119 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
120 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
121 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
122 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
123 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
124 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
125 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
126 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
127 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
128 case HWTSTAMP_FILTER_PTP_V2_EVENT:
129 case HWTSTAMP_FILTER_PTP_V2_SYNC:
130 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
131 /* Disable CQE compression */
132 netdev_warn(dev, "Disabling cqe compression");
133 err = mlx5e_modify_rx_cqe_compression_locked(priv, false);
135 netdev_err(dev, "Failed disabling cqe compression err=%d\n", err);
136 mutex_unlock(&priv->state_lock);
139 config.rx_filter = HWTSTAMP_FILTER_ALL;
142 mutex_unlock(&priv->state_lock);
146 memcpy(&priv->tstamp.hwtstamp_config, &config, sizeof(config));
147 mutex_unlock(&priv->state_lock);
149 return copy_to_user(ifr->ifr_data, &config,
150 sizeof(config)) ? -EFAULT : 0;
153 int mlx5e_hwstamp_get(struct net_device *dev, struct ifreq *ifr)
155 struct mlx5e_priv *priv = netdev_priv(dev);
156 struct hwtstamp_config *cfg = &priv->tstamp.hwtstamp_config;
158 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
161 return copy_to_user(ifr->ifr_data, cfg, sizeof(*cfg)) ? -EFAULT : 0;
164 static int mlx5e_ptp_settime(struct ptp_clock_info *ptp,
165 const struct timespec64 *ts)
167 struct mlx5e_tstamp *tstamp = container_of(ptp, struct mlx5e_tstamp,
169 u64 ns = timespec64_to_ns(ts);
172 write_lock_irqsave(&tstamp->lock, flags);
173 timecounter_init(&tstamp->clock, &tstamp->cycles, ns);
174 write_unlock_irqrestore(&tstamp->lock, flags);
179 static int mlx5e_ptp_gettime(struct ptp_clock_info *ptp,
180 struct timespec64 *ts)
182 struct mlx5e_tstamp *tstamp = container_of(ptp, struct mlx5e_tstamp,
187 write_lock_irqsave(&tstamp->lock, flags);
188 ns = timecounter_read(&tstamp->clock);
189 write_unlock_irqrestore(&tstamp->lock, flags);
191 *ts = ns_to_timespec64(ns);
196 static int mlx5e_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
198 struct mlx5e_tstamp *tstamp = container_of(ptp, struct mlx5e_tstamp,
202 write_lock_irqsave(&tstamp->lock, flags);
203 timecounter_adjtime(&tstamp->clock, delta);
204 write_unlock_irqrestore(&tstamp->lock, flags);
209 static int mlx5e_ptp_adjfreq(struct ptp_clock_info *ptp, s32 delta)
215 struct mlx5e_tstamp *tstamp = container_of(ptp, struct mlx5e_tstamp,
217 struct mlx5e_priv *priv =
218 container_of(tstamp, struct mlx5e_priv, tstamp);
220 if (MLX5_CAP_GEN(priv->mdev, pps_modify)) {
221 u32 in[MLX5_ST_SZ_DW(mtpps_reg)] = {0};
223 /* For future use need to add a loop for finding all 1PPS out pins */
224 MLX5_SET(mtpps_reg, in, pin_mode, MLX5E_PIN_MODE_OUT);
225 MLX5_SET(mtpps_reg, in, out_periodic_adjustment, delta & 0xFFFF);
227 mlx5_set_mtpps(priv->mdev, in, sizeof(in));
235 adj = tstamp->nominal_c_mult;
237 diff = div_u64(adj, 1000000000ULL);
239 write_lock_irqsave(&tstamp->lock, flags);
240 timecounter_read(&tstamp->clock);
241 tstamp->cycles.mult = neg_adj ? tstamp->nominal_c_mult - diff :
242 tstamp->nominal_c_mult + diff;
243 write_unlock_irqrestore(&tstamp->lock, flags);
248 static int mlx5e_extts_configure(struct ptp_clock_info *ptp,
249 struct ptp_clock_request *rq,
252 struct mlx5e_tstamp *tstamp =
253 container_of(ptp, struct mlx5e_tstamp, ptp_info);
254 struct mlx5e_priv *priv =
255 container_of(tstamp, struct mlx5e_priv, tstamp);
256 u32 in[MLX5_ST_SZ_DW(mtpps_reg)] = {0};
261 if (!MLX5_CAP_GEN(priv->mdev, pps) ||
262 !MLX5_CAP_GEN(priv->mdev, pps_modify))
265 if (rq->extts.index >= tstamp->ptp_info.n_pins)
269 pin = ptp_find_pin(tstamp->ptp, PTP_PF_EXTTS, rq->extts.index);
274 if (rq->extts.flags & PTP_FALLING_EDGE)
277 MLX5_SET(mtpps_reg, in, pin, pin);
278 MLX5_SET(mtpps_reg, in, pin_mode, MLX5E_PIN_MODE_IN);
279 MLX5_SET(mtpps_reg, in, pattern, pattern);
280 MLX5_SET(mtpps_reg, in, enable, on);
282 err = mlx5_set_mtpps(priv->mdev, in, sizeof(in));
286 return mlx5_set_mtppse(priv->mdev, pin, 0,
287 MLX5E_EVENT_MODE_REPETETIVE & on);
290 static int mlx5e_perout_configure(struct ptp_clock_info *ptp,
291 struct ptp_clock_request *rq,
294 struct mlx5e_tstamp *tstamp =
295 container_of(ptp, struct mlx5e_tstamp, ptp_info);
296 struct mlx5e_priv *priv =
297 container_of(tstamp, struct mlx5e_priv, tstamp);
298 u32 in[MLX5_ST_SZ_DW(mtpps_reg)] = {0};
299 u64 nsec_now, nsec_delta, time_stamp;
300 u64 cycles_now, cycles_delta;
301 struct timespec64 ts;
306 if (!MLX5_CAP_GEN(priv->mdev, pps_modify))
309 if (rq->perout.index >= tstamp->ptp_info.n_pins)
313 pin = ptp_find_pin(tstamp->ptp, PTP_PF_PEROUT,
319 ts.tv_sec = rq->perout.period.sec;
320 ts.tv_nsec = rq->perout.period.nsec;
321 ns = timespec64_to_ns(&ts);
323 if ((ns >> 1) != 500000000LL)
325 ts.tv_sec = rq->perout.start.sec;
326 ts.tv_nsec = rq->perout.start.nsec;
327 ns = timespec64_to_ns(&ts);
328 cycles_now = mlx5_read_internal_timer(tstamp->mdev);
329 write_lock_irqsave(&tstamp->lock, flags);
330 nsec_now = timecounter_cyc2time(&tstamp->clock, cycles_now);
331 nsec_delta = ns - nsec_now;
332 cycles_delta = div64_u64(nsec_delta << tstamp->cycles.shift,
333 tstamp->cycles.mult);
334 write_unlock_irqrestore(&tstamp->lock, flags);
335 time_stamp = cycles_now + cycles_delta;
336 MLX5_SET(mtpps_reg, in, pin, pin);
337 MLX5_SET(mtpps_reg, in, pin_mode, MLX5E_PIN_MODE_OUT);
338 MLX5_SET(mtpps_reg, in, pattern, MLX5E_OUT_PATTERN_PERIODIC);
339 MLX5_SET(mtpps_reg, in, enable, on);
340 MLX5_SET64(mtpps_reg, in, time_stamp, time_stamp);
342 return mlx5_set_mtpps(priv->mdev, in, sizeof(in));
345 static int mlx5e_ptp_enable(struct ptp_clock_info *ptp,
346 struct ptp_clock_request *rq,
350 case PTP_CLK_REQ_EXTTS:
351 return mlx5e_extts_configure(ptp, rq, on);
352 case PTP_CLK_REQ_PEROUT:
353 return mlx5e_perout_configure(ptp, rq, on);
360 static int mlx5e_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
361 enum ptp_pin_function func, unsigned int chan)
363 return (func == PTP_PF_PHYSYNC) ? -EOPNOTSUPP : 0;
366 static const struct ptp_clock_info mlx5e_ptp_clock_info = {
367 .owner = THIS_MODULE,
368 .max_adj = 100000000,
374 .adjfreq = mlx5e_ptp_adjfreq,
375 .adjtime = mlx5e_ptp_adjtime,
376 .gettime64 = mlx5e_ptp_gettime,
377 .settime64 = mlx5e_ptp_settime,
382 static void mlx5e_timestamp_init_config(struct mlx5e_tstamp *tstamp)
384 tstamp->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
385 tstamp->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
388 static int mlx5e_init_pin_config(struct mlx5e_tstamp *tstamp)
392 tstamp->ptp_info.pin_config =
393 kzalloc(sizeof(*tstamp->ptp_info.pin_config) *
394 tstamp->ptp_info.n_pins, GFP_KERNEL);
395 if (!tstamp->ptp_info.pin_config)
397 tstamp->ptp_info.enable = mlx5e_ptp_enable;
398 tstamp->ptp_info.verify = mlx5e_ptp_verify;
400 for (i = 0; i < tstamp->ptp_info.n_pins; i++) {
401 snprintf(tstamp->ptp_info.pin_config[i].name,
402 sizeof(tstamp->ptp_info.pin_config[i].name),
404 tstamp->ptp_info.pin_config[i].index = i;
405 tstamp->ptp_info.pin_config[i].func = PTP_PF_NONE;
406 tstamp->ptp_info.pin_config[i].chan = i;
412 static void mlx5e_get_pps_caps(struct mlx5e_priv *priv,
413 struct mlx5e_tstamp *tstamp)
415 u32 out[MLX5_ST_SZ_DW(mtpps_reg)] = {0};
417 mlx5_query_mtpps(priv->mdev, out, sizeof(out));
419 tstamp->ptp_info.n_pins = MLX5_GET(mtpps_reg, out,
420 cap_number_of_pps_pins);
421 tstamp->ptp_info.n_ext_ts = MLX5_GET(mtpps_reg, out,
422 cap_max_num_of_pps_in_pins);
423 tstamp->ptp_info.n_per_out = MLX5_GET(mtpps_reg, out,
424 cap_max_num_of_pps_out_pins);
426 tstamp->pps_pin_caps[0] = MLX5_GET(mtpps_reg, out, cap_pin_0_mode);
427 tstamp->pps_pin_caps[1] = MLX5_GET(mtpps_reg, out, cap_pin_1_mode);
428 tstamp->pps_pin_caps[2] = MLX5_GET(mtpps_reg, out, cap_pin_2_mode);
429 tstamp->pps_pin_caps[3] = MLX5_GET(mtpps_reg, out, cap_pin_3_mode);
430 tstamp->pps_pin_caps[4] = MLX5_GET(mtpps_reg, out, cap_pin_4_mode);
431 tstamp->pps_pin_caps[5] = MLX5_GET(mtpps_reg, out, cap_pin_5_mode);
432 tstamp->pps_pin_caps[6] = MLX5_GET(mtpps_reg, out, cap_pin_6_mode);
433 tstamp->pps_pin_caps[7] = MLX5_GET(mtpps_reg, out, cap_pin_7_mode);
436 void mlx5e_pps_event_handler(struct mlx5e_priv *priv,
437 struct ptp_clock_event *event)
439 struct mlx5e_tstamp *tstamp = &priv->tstamp;
441 ptp_clock_event(tstamp->ptp, event);
444 void mlx5e_timestamp_init(struct mlx5e_priv *priv)
446 struct mlx5e_tstamp *tstamp = &priv->tstamp;
451 mlx5e_timestamp_init_config(tstamp);
452 dev_freq = MLX5_CAP_GEN(priv->mdev, device_frequency_khz);
454 mlx5_core_warn(priv->mdev, "invalid device_frequency_khz, aborting HW clock init\n");
457 rwlock_init(&tstamp->lock);
458 tstamp->cycles.read = mlx5e_read_internal_timer;
459 tstamp->cycles.shift = MLX5E_CYCLES_SHIFT;
460 tstamp->cycles.mult = clocksource_khz2mult(dev_freq,
461 tstamp->cycles.shift);
462 tstamp->nominal_c_mult = tstamp->cycles.mult;
463 tstamp->cycles.mask = CLOCKSOURCE_MASK(41);
464 tstamp->mdev = priv->mdev;
466 timecounter_init(&tstamp->clock, &tstamp->cycles,
467 ktime_to_ns(ktime_get_real()));
469 /* Calculate period in seconds to call the overflow watchdog - to make
470 * sure counter is checked at least once every wrap around.
472 ns = cyclecounter_cyc2ns(&tstamp->cycles, tstamp->cycles.mask,
474 do_div(ns, NSEC_PER_SEC / 2 / HZ);
475 tstamp->overflow_period = ns;
477 INIT_DELAYED_WORK(&tstamp->overflow_work, mlx5e_timestamp_overflow);
478 if (tstamp->overflow_period)
479 schedule_delayed_work(&tstamp->overflow_work, 0);
481 mlx5_core_warn(priv->mdev, "invalid overflow period, overflow_work is not scheduled\n");
483 /* Configure the PHC */
484 tstamp->ptp_info = mlx5e_ptp_clock_info;
485 snprintf(tstamp->ptp_info.name, 16, "mlx5 ptp");
487 /* Initialize 1PPS data structures */
488 #define MAX_PIN_NUM 8
489 tstamp->pps_pin_caps = kzalloc(sizeof(u8) * MAX_PIN_NUM, GFP_KERNEL);
490 if (tstamp->pps_pin_caps) {
491 if (MLX5_CAP_GEN(priv->mdev, pps))
492 mlx5e_get_pps_caps(priv, tstamp);
493 if (tstamp->ptp_info.n_pins)
494 mlx5e_init_pin_config(tstamp);
496 mlx5_core_warn(priv->mdev, "1PPS initialization failed\n");
499 tstamp->ptp = ptp_clock_register(&tstamp->ptp_info,
500 &priv->mdev->pdev->dev);
501 if (IS_ERR(tstamp->ptp)) {
502 mlx5_core_warn(priv->mdev, "ptp_clock_register failed %ld\n",
503 PTR_ERR(tstamp->ptp));
508 void mlx5e_timestamp_cleanup(struct mlx5e_priv *priv)
510 struct mlx5e_tstamp *tstamp = &priv->tstamp;
512 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz))
515 if (priv->tstamp.ptp) {
516 ptp_clock_unregister(priv->tstamp.ptp);
517 priv->tstamp.ptp = NULL;
520 kfree(tstamp->pps_pin_caps);
521 kfree(tstamp->ptp_info.pin_config);
523 cancel_delayed_work_sync(&tstamp->overflow_work);