en_clock.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "mlx4_en.h"
  35. int mlx4_en_timestamp_config(struct net_device *dev, int tx_type, int rx_filter)
  36. {
  37. struct mlx4_en_priv *priv = netdev_priv(dev);
  38. struct mlx4_en_dev *mdev = priv->mdev;
  39. int port_up = 0;
  40. int err = 0;
  41. if (priv->hwtstamp_config.tx_type == tx_type &&
  42. priv->hwtstamp_config.rx_filter == rx_filter)
  43. return 0;
  44. mutex_lock(&mdev->state_lock);
  45. if (priv->port_up) {
  46. port_up = 1;
  47. mlx4_en_stop_port(dev, 1);
  48. }
  49. mlx4_en_free_resources(priv);
  50. en_warn(priv, "Changing Time Stamp configuration\n");
  51. priv->hwtstamp_config.tx_type = tx_type;
  52. priv->hwtstamp_config.rx_filter = rx_filter;
  53. if (rx_filter != HWTSTAMP_FILTER_NONE)
  54. dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
  55. else
  56. dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  57. err = mlx4_en_alloc_resources(priv);
  58. if (err) {
  59. en_err(priv, "Failed reallocating port resources\n");
  60. goto out;
  61. }
  62. if (port_up) {
  63. err = mlx4_en_start_port(dev);
  64. if (err)
  65. en_err(priv, "Failed starting port\n");
  66. }
  67. out:
  68. mutex_unlock(&mdev->state_lock);
  69. netdev_features_change(dev);
  70. return err;
  71. }
  72. /* mlx4_en_read_clock - read raw cycle counter (to be used by time counter)
  73. */
  74. static cycle_t mlx4_en_read_clock(const struct cyclecounter *tc)
  75. {
  76. struct mlx4_en_dev *mdev =
  77. container_of(tc, struct mlx4_en_dev, cycles);
  78. struct mlx4_dev *dev = mdev->dev;
  79. return mlx4_read_clock(dev) & tc->mask;
  80. }
  81. u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe)
  82. {
  83. u64 hi, lo;
  84. struct mlx4_ts_cqe *ts_cqe = (struct mlx4_ts_cqe *)cqe;
  85. lo = (u64)be16_to_cpu(ts_cqe->timestamp_lo);
  86. hi = ((u64)be32_to_cpu(ts_cqe->timestamp_hi) + !lo) << 16;
  87. return hi | lo;
  88. }
  89. void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
  90. struct skb_shared_hwtstamps *hwts,
  91. u64 timestamp)
  92. {
  93. unsigned long flags;
  94. u64 nsec;
  95. read_lock_irqsave(&mdev->clock_lock, flags);
  96. nsec = timecounter_cyc2time(&mdev->clock, timestamp);
  97. read_unlock_irqrestore(&mdev->clock_lock, flags);
  98. memset(hwts, 0, sizeof(struct skb_shared_hwtstamps));
  99. hwts->hwtstamp = ns_to_ktime(nsec);
  100. }
  101. /**
  102. * mlx4_en_remove_timestamp - disable PTP device
  103. * @mdev: board private structure
  104. *
  105. * Stop the PTP support.
  106. **/
  107. void mlx4_en_remove_timestamp(struct mlx4_en_dev *mdev)
  108. {
  109. if (mdev->ptp_clock) {
  110. ptp_clock_unregister(mdev->ptp_clock);
  111. mdev->ptp_clock = NULL;
  112. mlx4_info(mdev, "removed PHC\n");
  113. }
  114. }
  115. void mlx4_en_ptp_overflow_check(struct mlx4_en_dev *mdev)
  116. {
  117. bool timeout = time_is_before_jiffies(mdev->last_overflow_check +
  118. mdev->overflow_period);
  119. unsigned long flags;
  120. if (timeout) {
  121. write_lock_irqsave(&mdev->clock_lock, flags);
  122. timecounter_read(&mdev->clock);
  123. write_unlock_irqrestore(&mdev->clock_lock, flags);
  124. mdev->last_overflow_check = jiffies;
  125. }
  126. }
  127. /**
  128. * mlx4_en_phc_adjfreq - adjust the frequency of the hardware clock
  129. * @ptp: ptp clock structure
  130. * @delta: Desired frequency change in parts per billion
  131. *
  132. * Adjust the frequency of the PHC cycle counter by the indicated delta from
  133. * the base frequency.
  134. **/
  135. static int mlx4_en_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
  136. {
  137. u64 adj;
  138. u32 diff, mult;
  139. int neg_adj = 0;
  140. unsigned long flags;
  141. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  142. ptp_clock_info);
  143. if (delta < 0) {
  144. neg_adj = 1;
  145. delta = -delta;
  146. }
  147. mult = mdev->nominal_c_mult;
  148. adj = mult;
  149. adj *= delta;
  150. diff = div_u64(adj, 1000000000ULL);
  151. write_lock_irqsave(&mdev->clock_lock, flags);
  152. timecounter_read(&mdev->clock);
  153. mdev->cycles.mult = neg_adj ? mult - diff : mult + diff;
  154. write_unlock_irqrestore(&mdev->clock_lock, flags);
  155. return 0;
  156. }
  157. /**
  158. * mlx4_en_phc_adjtime - Shift the time of the hardware clock
  159. * @ptp: ptp clock structure
  160. * @delta: Desired change in nanoseconds
  161. *
  162. * Adjust the timer by resetting the timecounter structure.
  163. **/
  164. static int mlx4_en_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  165. {
  166. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  167. ptp_clock_info);
  168. unsigned long flags;
  169. s64 now;
  170. write_lock_irqsave(&mdev->clock_lock, flags);
  171. now = timecounter_read(&mdev->clock);
  172. now += delta;
  173. timecounter_init(&mdev->clock, &mdev->cycles, now);
  174. write_unlock_irqrestore(&mdev->clock_lock, flags);
  175. return 0;
  176. }
  177. /**
  178. * mlx4_en_phc_gettime - Reads the current time from the hardware clock
  179. * @ptp: ptp clock structure
  180. * @ts: timespec structure to hold the current time value
  181. *
  182. * Read the timecounter and return the correct value in ns after converting
  183. * it into a struct timespec.
  184. **/
  185. static int mlx4_en_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  186. {
  187. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  188. ptp_clock_info);
  189. unsigned long flags;
  190. u32 remainder;
  191. u64 ns;
  192. write_lock_irqsave(&mdev->clock_lock, flags);
  193. ns = timecounter_read(&mdev->clock);
  194. write_unlock_irqrestore(&mdev->clock_lock, flags);
  195. ts->tv_sec = div_u64_rem(ns, NSEC_PER_SEC, &remainder);
  196. ts->tv_nsec = remainder;
  197. return 0;
  198. }
  199. /**
  200. * mlx4_en_phc_settime - Set the current time on the hardware clock
  201. * @ptp: ptp clock structure
  202. * @ts: timespec containing the new time for the cycle counter
  203. *
  204. * Reset the timecounter to use a new base value instead of the kernel
  205. * wall timer value.
  206. **/
  207. static int mlx4_en_phc_settime(struct ptp_clock_info *ptp,
  208. const struct timespec *ts)
  209. {
  210. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  211. ptp_clock_info);
  212. u64 ns = timespec_to_ns(ts);
  213. unsigned long flags;
  214. /* reset the timecounter */
  215. write_lock_irqsave(&mdev->clock_lock, flags);
  216. timecounter_init(&mdev->clock, &mdev->cycles, ns);
  217. write_unlock_irqrestore(&mdev->clock_lock, flags);
  218. return 0;
  219. }
  220. /**
  221. * mlx4_en_phc_enable - enable or disable an ancillary feature
  222. * @ptp: ptp clock structure
  223. * @request: Desired resource to enable or disable
  224. * @on: Caller passes one to enable or zero to disable
  225. *
  226. * Enable (or disable) ancillary features of the PHC subsystem.
  227. * Currently, no ancillary features are supported.
  228. **/
  229. static int mlx4_en_phc_enable(struct ptp_clock_info __always_unused *ptp,
  230. struct ptp_clock_request __always_unused *request,
  231. int __always_unused on)
  232. {
  233. return -EOPNOTSUPP;
  234. }
  235. static const struct ptp_clock_info mlx4_en_ptp_clock_info = {
  236. .owner = THIS_MODULE,
  237. .max_adj = 100000000,
  238. .n_alarm = 0,
  239. .n_ext_ts = 0,
  240. .n_per_out = 0,
  241. .pps = 0,
  242. .adjfreq = mlx4_en_phc_adjfreq,
  243. .adjtime = mlx4_en_phc_adjtime,
  244. .gettime = mlx4_en_phc_gettime,
  245. .settime = mlx4_en_phc_settime,
  246. .enable = mlx4_en_phc_enable,
  247. };
  248. void mlx4_en_init_timestamp(struct mlx4_en_dev *mdev)
  249. {
  250. struct mlx4_dev *dev = mdev->dev;
  251. unsigned long flags;
  252. u64 ns;
  253. rwlock_init(&mdev->clock_lock);
  254. memset(&mdev->cycles, 0, sizeof(mdev->cycles));
  255. mdev->cycles.read = mlx4_en_read_clock;
  256. mdev->cycles.mask = CLOCKSOURCE_MASK(48);
  257. /* Using shift to make calculation more accurate. Since current HW
  258. * clock frequency is 427 MHz, and cycles are given using a 48 bits
  259. * register, the biggest shift when calculating using u64, is 14
  260. * (max_cycles * multiplier < 2^64)
  261. */
  262. mdev->cycles.shift = 14;
  263. mdev->cycles.mult =
  264. clocksource_khz2mult(1000 * dev->caps.hca_core_clock, mdev->cycles.shift);
  265. mdev->nominal_c_mult = mdev->cycles.mult;
  266. write_lock_irqsave(&mdev->clock_lock, flags);
  267. timecounter_init(&mdev->clock, &mdev->cycles,
  268. ktime_to_ns(ktime_get_real()));
  269. write_unlock_irqrestore(&mdev->clock_lock, flags);
  270. /* Calculate period in seconds to call the overflow watchdog - to make
  271. * sure counter is checked at least once every wrap around.
  272. */
  273. ns = cyclecounter_cyc2ns(&mdev->cycles, mdev->cycles.mask);
  274. do_div(ns, NSEC_PER_SEC / 2 / HZ);
  275. mdev->overflow_period = ns;
  276. /* Configure the PHC */
  277. mdev->ptp_clock_info = mlx4_en_ptp_clock_info;
  278. snprintf(mdev->ptp_clock_info.name, 16, "mlx4 ptp");
  279. mdev->ptp_clock = ptp_clock_register(&mdev->ptp_clock_info,
  280. &mdev->pdev->dev);
  281. if (IS_ERR(mdev->ptp_clock)) {
  282. mdev->ptp_clock = NULL;
  283. mlx4_err(mdev, "ptp_clock_register failed\n");
  284. } else {
  285. mlx4_info(mdev, "registered PHC clock\n");
  286. }
  287. }