htc_credit_history.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2018 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "htc_debug.h"
  19. #include "htc_internal.h"
  20. #include "htc_credit_history.h"
  21. #include <qdf_lock.h>
  22. struct HTC_CREDIT_HISTORY {
  23. enum htc_credit_exchange_type type;
  24. uint64_t time;
  25. uint32_t tx_credit;
  26. uint32_t htc_tx_queue_depth;
  27. };
  28. static qdf_spinlock_t g_htc_credit_lock;
  29. static uint32_t g_htc_credit_history_idx;
  30. static uint32_t g_htc_credit_history_length;
  31. static
  32. struct HTC_CREDIT_HISTORY htc_credit_history_buffer[HTC_CREDIT_HISTORY_MAX];
  33. #ifdef QCA_WIFI_NAPIER_EMULATION
  34. #define HTC_EMULATION_DELAY_IN_MS 20
  35. /**
  36. * htc_add_delay(): Adds a delay in before proceeding, only for emulation
  37. *
  38. * Return: None
  39. */
  40. static inline void htc_add_emulation_delay(void)
  41. {
  42. qdf_mdelay(HTC_EMULATION_DELAY_IN_MS);
  43. }
  44. #else
  45. static inline void htc_add_emulation_delay(void)
  46. {
  47. }
  48. #endif
  49. void htc_credit_history_init(void)
  50. {
  51. qdf_spinlock_create(&g_htc_credit_lock);
  52. g_htc_credit_history_idx = 0;
  53. g_htc_credit_history_length = 0;
  54. }
  55. /**
  56. * htc_credit_record() - records tx que state & credit transactions
  57. * @type: type of echange can be HTC_REQUEST_CREDIT
  58. * or HTC_PROCESS_CREDIT_REPORT
  59. * @tx_credits: current number of tx_credits
  60. * @htc_tx_queue_depth: current hct tx queue depth
  61. *
  62. * This function records the credits and pending commands whenever a command is
  63. * sent or credits are returned. Call this after the credits have been updated
  64. * according to the transaction. Call this before dequeing commands.
  65. *
  66. * Consider making this function accept an HTC_ENDPOINT and find the current
  67. * credits and queue depth itself.
  68. *
  69. */
  70. void htc_credit_record(enum htc_credit_exchange_type type, uint32_t tx_credit,
  71. uint32_t htc_tx_queue_depth)
  72. {
  73. qdf_spin_lock_bh(&g_htc_credit_lock);
  74. if (g_htc_credit_history_idx >= HTC_CREDIT_HISTORY_MAX)
  75. g_htc_credit_history_idx = 0;
  76. htc_credit_history_buffer[g_htc_credit_history_idx].type = type;
  77. htc_credit_history_buffer[g_htc_credit_history_idx].time =
  78. qdf_get_log_timestamp();
  79. htc_credit_history_buffer[g_htc_credit_history_idx].tx_credit =
  80. tx_credit;
  81. htc_credit_history_buffer[g_htc_credit_history_idx].htc_tx_queue_depth =
  82. htc_tx_queue_depth;
  83. g_htc_credit_history_idx++;
  84. g_htc_credit_history_length++;
  85. htc_add_emulation_delay();
  86. qdf_spin_unlock_bh(&g_htc_credit_lock);
  87. }
  88. void htc_print_credit_history(HTC_HANDLE htc, uint32_t count,
  89. qdf_abstract_print *print, void *print_priv)
  90. {
  91. uint32_t idx;
  92. print(print_priv, "HTC Credit History (count %u)", count);
  93. qdf_spin_lock_bh(&g_htc_credit_lock);
  94. if (count > HTC_CREDIT_HISTORY_MAX)
  95. count = HTC_CREDIT_HISTORY_MAX;
  96. if (count > g_htc_credit_history_length)
  97. count = g_htc_credit_history_length;
  98. /* subtract count from index, and wrap if necessary */
  99. idx = HTC_CREDIT_HISTORY_MAX + g_htc_credit_history_idx - count;
  100. idx %= HTC_CREDIT_HISTORY_MAX;
  101. print(print_priv,
  102. "Time (seconds) Type Credits Queue Depth");
  103. while (count) {
  104. struct HTC_CREDIT_HISTORY *hist =
  105. &htc_credit_history_buffer[idx];
  106. uint64_t secs, usecs;
  107. qdf_log_timestamp_to_secs(hist->time, &secs, &usecs);
  108. print(print_priv, "% 8lld.%06lld %-25s %-7.d %d",
  109. secs,
  110. usecs,
  111. htc_credit_exchange_type_str(hist->type),
  112. hist->tx_credit,
  113. hist->htc_tx_queue_depth);
  114. --count;
  115. ++idx;
  116. if (idx >= HTC_CREDIT_HISTORY_MAX)
  117. idx = 0;
  118. }
  119. qdf_spin_unlock_bh(&g_htc_credit_lock);
  120. }