en_clock.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. mutex_lock(&mdev->state_lock);
  42. if (priv->port_up) {
  43. port_up = 1;
  44. mlx4_en_stop_port(dev, 1);
  45. }
  46. mlx4_en_free_resources(priv);
  47. en_warn(priv, "Changing Time Stamp configuration\n");
  48. priv->hwtstamp_config.tx_type = tx_type;
  49. priv->hwtstamp_config.rx_filter = rx_filter;
  50. if (rx_filter != HWTSTAMP_FILTER_NONE)
  51. dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
  52. else
  53. dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  54. err = mlx4_en_alloc_resources(priv);
  55. if (err) {
  56. en_err(priv, "Failed reallocating port resources\n");
  57. goto out;
  58. }
  59. if (port_up) {
  60. err = mlx4_en_start_port(dev);
  61. if (err)
  62. en_err(priv, "Failed starting port\n");
  63. }
  64. out:
  65. mutex_unlock(&mdev->state_lock);
  66. netdev_features_change(dev);
  67. return err;
  68. }
  69. /* mlx4_en_read_clock - read raw cycle counter (to be used by time counter)
  70. */
  71. static cycle_t mlx4_en_read_clock(const struct cyclecounter *tc)
  72. {
  73. struct mlx4_en_dev *mdev =
  74. container_of(tc, struct mlx4_en_dev, cycles);
  75. struct mlx4_dev *dev = mdev->dev;
  76. return mlx4_read_clock(dev) & tc->mask;
  77. }
  78. u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe)
  79. {
  80. u64 hi, lo;
  81. struct mlx4_ts_cqe *ts_cqe = (struct mlx4_ts_cqe *)cqe;
  82. lo = (u64)be16_to_cpu(ts_cqe->timestamp_lo);
  83. hi = ((u64)be32_to_cpu(ts_cqe->timestamp_hi) + !lo) << 16;
  84. return hi | lo;
  85. }
  86. void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
  87. struct skb_shared_hwtstamps *hwts,
  88. u64 timestamp)
  89. {
  90. u64 nsec;
  91. nsec = timecounter_cyc2time(&mdev->clock, timestamp);
  92. memset(hwts, 0, sizeof(struct skb_shared_hwtstamps));
  93. hwts->hwtstamp = ns_to_ktime(nsec);
  94. }
  95. void mlx4_en_init_timestamp(struct mlx4_en_dev *mdev)
  96. {
  97. struct mlx4_dev *dev = mdev->dev;
  98. u64 ns;
  99. memset(&mdev->cycles, 0, sizeof(mdev->cycles));
  100. mdev->cycles.read = mlx4_en_read_clock;
  101. mdev->cycles.mask = CLOCKSOURCE_MASK(48);
  102. /* Using shift to make calculation more accurate. Since current HW
  103. * clock frequency is 427 MHz, and cycles are given using a 48 bits
  104. * register, the biggest shift when calculating using u64, is 14
  105. * (max_cycles * multiplier < 2^64)
  106. */
  107. mdev->cycles.shift = 14;
  108. mdev->cycles.mult =
  109. clocksource_khz2mult(1000 * dev->caps.hca_core_clock, mdev->cycles.shift);
  110. timecounter_init(&mdev->clock, &mdev->cycles,
  111. ktime_to_ns(ktime_get_real()));
  112. /* Calculate period in seconds to call the overflow watchdog - to make
  113. * sure counter is checked at least once every wrap around.
  114. */
  115. ns = cyclecounter_cyc2ns(&mdev->cycles, mdev->cycles.mask);
  116. do_div(ns, NSEC_PER_SEC / 2 / HZ);
  117. mdev->overflow_period = ns;
  118. }
  119. void mlx4_en_ptp_overflow_check(struct mlx4_en_dev *mdev)
  120. {
  121. bool timeout = time_is_before_jiffies(mdev->last_overflow_check +
  122. mdev->overflow_period);
  123. if (timeout) {
  124. timecounter_read(&mdev->clock);
  125. mdev->last_overflow_check = jiffies;
  126. }
  127. }