en_clock.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include <linux/mlx4/device.h>
  34. #include <linux/clocksource.h>
  35. #include "mlx4_en.h"
  36. /* mlx4_en_read_clock - read raw cycle counter (to be used by time counter)
  37. */
  38. static cycle_t mlx4_en_read_clock(const struct cyclecounter *tc)
  39. {
  40. struct mlx4_en_dev *mdev =
  41. container_of(tc, struct mlx4_en_dev, cycles);
  42. struct mlx4_dev *dev = mdev->dev;
  43. return mlx4_read_clock(dev) & tc->mask;
  44. }
  45. u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe)
  46. {
  47. u64 hi, lo;
  48. struct mlx4_ts_cqe *ts_cqe = (struct mlx4_ts_cqe *)cqe;
  49. lo = (u64)be16_to_cpu(ts_cqe->timestamp_lo);
  50. hi = ((u64)be32_to_cpu(ts_cqe->timestamp_hi) + !lo) << 16;
  51. return hi | lo;
  52. }
  53. void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
  54. struct skb_shared_hwtstamps *hwts,
  55. u64 timestamp)
  56. {
  57. unsigned long flags;
  58. u64 nsec;
  59. read_lock_irqsave(&mdev->clock_lock, flags);
  60. nsec = timecounter_cyc2time(&mdev->clock, timestamp);
  61. read_unlock_irqrestore(&mdev->clock_lock, flags);
  62. memset(hwts, 0, sizeof(struct skb_shared_hwtstamps));
  63. hwts->hwtstamp = ns_to_ktime(nsec);
  64. }
  65. /**
  66. * mlx4_en_remove_timestamp - disable PTP device
  67. * @mdev: board private structure
  68. *
  69. * Stop the PTP support.
  70. **/
  71. void mlx4_en_remove_timestamp(struct mlx4_en_dev *mdev)
  72. {
  73. if (mdev->ptp_clock) {
  74. ptp_clock_unregister(mdev->ptp_clock);
  75. mdev->ptp_clock = NULL;
  76. mlx4_info(mdev, "removed PHC\n");
  77. }
  78. }
  79. #define MLX4_EN_WRAP_AROUND_SEC 10UL
  80. /* By scheduling the overflow check every 5 seconds, we have a reasonably
  81. * good chance we wont miss a wrap around.
  82. * TOTO: Use a timer instead of a work queue to increase the guarantee.
  83. */
  84. #define MLX4_EN_OVERFLOW_PERIOD (MLX4_EN_WRAP_AROUND_SEC * HZ / 2)
  85. void mlx4_en_ptp_overflow_check(struct mlx4_en_dev *mdev)
  86. {
  87. bool timeout = time_is_before_jiffies(mdev->last_overflow_check +
  88. MLX4_EN_OVERFLOW_PERIOD);
  89. unsigned long flags;
  90. if (timeout) {
  91. write_lock_irqsave(&mdev->clock_lock, flags);
  92. timecounter_read(&mdev->clock);
  93. write_unlock_irqrestore(&mdev->clock_lock, flags);
  94. mdev->last_overflow_check = jiffies;
  95. }
  96. }
  97. /**
  98. * mlx4_en_phc_adjfreq - adjust the frequency of the hardware clock
  99. * @ptp: ptp clock structure
  100. * @delta: Desired frequency change in parts per billion
  101. *
  102. * Adjust the frequency of the PHC cycle counter by the indicated delta from
  103. * the base frequency.
  104. **/
  105. static int mlx4_en_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
  106. {
  107. u64 adj;
  108. u32 diff, mult;
  109. int neg_adj = 0;
  110. unsigned long flags;
  111. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  112. ptp_clock_info);
  113. if (delta < 0) {
  114. neg_adj = 1;
  115. delta = -delta;
  116. }
  117. mult = mdev->nominal_c_mult;
  118. adj = mult;
  119. adj *= delta;
  120. diff = div_u64(adj, 1000000000ULL);
  121. write_lock_irqsave(&mdev->clock_lock, flags);
  122. timecounter_read(&mdev->clock);
  123. mdev->cycles.mult = neg_adj ? mult - diff : mult + diff;
  124. write_unlock_irqrestore(&mdev->clock_lock, flags);
  125. return 0;
  126. }
  127. /**
  128. * mlx4_en_phc_adjtime - Shift the time of the hardware clock
  129. * @ptp: ptp clock structure
  130. * @delta: Desired change in nanoseconds
  131. *
  132. * Adjust the timer by resetting the timecounter structure.
  133. **/
  134. static int mlx4_en_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  135. {
  136. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  137. ptp_clock_info);
  138. unsigned long flags;
  139. write_lock_irqsave(&mdev->clock_lock, flags);
  140. timecounter_adjtime(&mdev->clock, delta);
  141. write_unlock_irqrestore(&mdev->clock_lock, flags);
  142. return 0;
  143. }
  144. /**
  145. * mlx4_en_phc_gettime - Reads the current time from the hardware clock
  146. * @ptp: ptp clock structure
  147. * @ts: timespec structure to hold the current time value
  148. *
  149. * Read the timecounter and return the correct value in ns after converting
  150. * it into a struct timespec.
  151. **/
  152. static int mlx4_en_phc_gettime(struct ptp_clock_info *ptp,
  153. struct timespec64 *ts)
  154. {
  155. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  156. ptp_clock_info);
  157. unsigned long flags;
  158. u64 ns;
  159. write_lock_irqsave(&mdev->clock_lock, flags);
  160. ns = timecounter_read(&mdev->clock);
  161. write_unlock_irqrestore(&mdev->clock_lock, flags);
  162. *ts = ns_to_timespec64(ns);
  163. return 0;
  164. }
  165. /**
  166. * mlx4_en_phc_settime - Set the current time on the hardware clock
  167. * @ptp: ptp clock structure
  168. * @ts: timespec containing the new time for the cycle counter
  169. *
  170. * Reset the timecounter to use a new base value instead of the kernel
  171. * wall timer value.
  172. **/
  173. static int mlx4_en_phc_settime(struct ptp_clock_info *ptp,
  174. const struct timespec64 *ts)
  175. {
  176. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  177. ptp_clock_info);
  178. u64 ns = timespec64_to_ns(ts);
  179. unsigned long flags;
  180. /* reset the timecounter */
  181. write_lock_irqsave(&mdev->clock_lock, flags);
  182. timecounter_init(&mdev->clock, &mdev->cycles, ns);
  183. write_unlock_irqrestore(&mdev->clock_lock, flags);
  184. return 0;
  185. }
  186. /**
  187. * mlx4_en_phc_enable - enable or disable an ancillary feature
  188. * @ptp: ptp clock structure
  189. * @request: Desired resource to enable or disable
  190. * @on: Caller passes one to enable or zero to disable
  191. *
  192. * Enable (or disable) ancillary features of the PHC subsystem.
  193. * Currently, no ancillary features are supported.
  194. **/
  195. static int mlx4_en_phc_enable(struct ptp_clock_info __always_unused *ptp,
  196. struct ptp_clock_request __always_unused *request,
  197. int __always_unused on)
  198. {
  199. return -EOPNOTSUPP;
  200. }
  201. static const struct ptp_clock_info mlx4_en_ptp_clock_info = {
  202. .owner = THIS_MODULE,
  203. .max_adj = 100000000,
  204. .n_alarm = 0,
  205. .n_ext_ts = 0,
  206. .n_per_out = 0,
  207. .n_pins = 0,
  208. .pps = 0,
  209. .adjfreq = mlx4_en_phc_adjfreq,
  210. .adjtime = mlx4_en_phc_adjtime,
  211. .gettime64 = mlx4_en_phc_gettime,
  212. .settime64 = mlx4_en_phc_settime,
  213. .enable = mlx4_en_phc_enable,
  214. };
  215. /* This function calculates the max shift that enables the user range
  216. * of MLX4_EN_WRAP_AROUND_SEC values in the cycles register.
  217. */
  218. static u32 freq_to_shift(u16 freq)
  219. {
  220. u32 freq_khz = freq * 1000;
  221. u64 max_val_cycles = freq_khz * 1000 * MLX4_EN_WRAP_AROUND_SEC;
  222. u64 tmp_rounded =
  223. roundup_pow_of_two(max_val_cycles) > max_val_cycles ?
  224. roundup_pow_of_two(max_val_cycles) - 1 : UINT_MAX;
  225. u64 max_val_cycles_rounded = is_power_of_2(max_val_cycles + 1) ?
  226. max_val_cycles : tmp_rounded;
  227. /* calculate max possible multiplier in order to fit in 64bit */
  228. u64 max_mul = div_u64(0xffffffffffffffffULL, max_val_cycles_rounded);
  229. /* This comes from the reverse of clocksource_khz2mult */
  230. return ilog2(div_u64(max_mul * freq_khz, 1000000));
  231. }
  232. void mlx4_en_init_timestamp(struct mlx4_en_dev *mdev)
  233. {
  234. struct mlx4_dev *dev = mdev->dev;
  235. unsigned long flags;
  236. /* mlx4_en_init_timestamp is called for each netdev.
  237. * mdev->ptp_clock is common for all ports, skip initialization if
  238. * was done for other port.
  239. */
  240. if (mdev->ptp_clock)
  241. return;
  242. rwlock_init(&mdev->clock_lock);
  243. memset(&mdev->cycles, 0, sizeof(mdev->cycles));
  244. mdev->cycles.read = mlx4_en_read_clock;
  245. mdev->cycles.mask = CLOCKSOURCE_MASK(48);
  246. mdev->cycles.shift = freq_to_shift(dev->caps.hca_core_clock);
  247. mdev->cycles.mult =
  248. clocksource_khz2mult(1000 * dev->caps.hca_core_clock, mdev->cycles.shift);
  249. mdev->nominal_c_mult = mdev->cycles.mult;
  250. write_lock_irqsave(&mdev->clock_lock, flags);
  251. timecounter_init(&mdev->clock, &mdev->cycles,
  252. ktime_to_ns(ktime_get_real()));
  253. write_unlock_irqrestore(&mdev->clock_lock, flags);
  254. /* Configure the PHC */
  255. mdev->ptp_clock_info = mlx4_en_ptp_clock_info;
  256. snprintf(mdev->ptp_clock_info.name, 16, "mlx4 ptp");
  257. mdev->ptp_clock = ptp_clock_register(&mdev->ptp_clock_info,
  258. &mdev->pdev->dev);
  259. if (IS_ERR(mdev->ptp_clock)) {
  260. mdev->ptp_clock = NULL;
  261. mlx4_err(mdev, "ptp_clock_register failed\n");
  262. } else {
  263. mlx4_info(mdev, "registered PHC clock\n");
  264. }
  265. }