htc_credit_history.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2018,2020 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. #include <qdf_hang_event_notifier.h>
  23. #include <qdf_notifier.h>
  24. struct HTC_CREDIT_HISTORY {
  25. enum htc_credit_exchange_type type;
  26. uint64_t time;
  27. uint32_t tx_credit;
  28. uint32_t htc_tx_queue_depth;
  29. };
  30. struct htc_hang_data_fixed_param {
  31. uint16_t tlv_header;
  32. struct HTC_CREDIT_HISTORY credit_hist;
  33. } qdf_packed;
  34. static qdf_spinlock_t g_htc_credit_lock;
  35. static uint32_t g_htc_credit_history_idx;
  36. static uint32_t g_htc_credit_history_length;
  37. static
  38. struct HTC_CREDIT_HISTORY htc_credit_history_buffer[HTC_CREDIT_HISTORY_MAX];
  39. #define NUM_HANG_CREDIT_HISTORY 1
  40. #ifdef QCA_WIFI_NAPIER_EMULATION
  41. #define HTC_EMULATION_DELAY_IN_MS 20
  42. /**
  43. * htc_add_delay(): Adds a delay in before proceeding, only for emulation
  44. *
  45. * Return: None
  46. */
  47. static inline void htc_add_emulation_delay(void)
  48. {
  49. qdf_mdelay(HTC_EMULATION_DELAY_IN_MS);
  50. }
  51. #else
  52. static inline void htc_add_emulation_delay(void)
  53. {
  54. }
  55. #endif
  56. void htc_credit_history_init(void)
  57. {
  58. qdf_spinlock_create(&g_htc_credit_lock);
  59. g_htc_credit_history_idx = 0;
  60. g_htc_credit_history_length = 0;
  61. }
  62. /**
  63. * htc_credit_record() - records tx que state & credit transactions
  64. * @type: type of echange can be HTC_REQUEST_CREDIT
  65. * or HTC_PROCESS_CREDIT_REPORT
  66. * @tx_credits: current number of tx_credits
  67. * @htc_tx_queue_depth: current hct tx queue depth
  68. *
  69. * This function records the credits and pending commands whenever a command is
  70. * sent or credits are returned. Call this after the credits have been updated
  71. * according to the transaction. Call this before dequeing commands.
  72. *
  73. * Consider making this function accept an HTC_ENDPOINT and find the current
  74. * credits and queue depth itself.
  75. *
  76. */
  77. void htc_credit_record(enum htc_credit_exchange_type type, uint32_t tx_credit,
  78. uint32_t htc_tx_queue_depth)
  79. {
  80. qdf_spin_lock_bh(&g_htc_credit_lock);
  81. if (g_htc_credit_history_idx >= HTC_CREDIT_HISTORY_MAX)
  82. g_htc_credit_history_idx = 0;
  83. htc_credit_history_buffer[g_htc_credit_history_idx].type = type;
  84. htc_credit_history_buffer[g_htc_credit_history_idx].time =
  85. qdf_get_log_timestamp();
  86. htc_credit_history_buffer[g_htc_credit_history_idx].tx_credit =
  87. tx_credit;
  88. htc_credit_history_buffer[g_htc_credit_history_idx].htc_tx_queue_depth =
  89. htc_tx_queue_depth;
  90. g_htc_credit_history_idx++;
  91. g_htc_credit_history_length++;
  92. htc_add_emulation_delay();
  93. qdf_spin_unlock_bh(&g_htc_credit_lock);
  94. }
  95. void htc_print_credit_history(HTC_HANDLE htc, uint32_t count,
  96. qdf_abstract_print *print, void *print_priv)
  97. {
  98. uint32_t idx;
  99. print(print_priv, "HTC Credit History (count %u)", count);
  100. qdf_spin_lock_bh(&g_htc_credit_lock);
  101. if (count > HTC_CREDIT_HISTORY_MAX)
  102. count = HTC_CREDIT_HISTORY_MAX;
  103. if (count > g_htc_credit_history_length)
  104. count = g_htc_credit_history_length;
  105. /* subtract count from index, and wrap if necessary */
  106. idx = HTC_CREDIT_HISTORY_MAX + g_htc_credit_history_idx - count;
  107. idx %= HTC_CREDIT_HISTORY_MAX;
  108. print(print_priv,
  109. "Time (seconds) Type Credits Queue Depth");
  110. while (count) {
  111. struct HTC_CREDIT_HISTORY *hist =
  112. &htc_credit_history_buffer[idx];
  113. uint64_t secs, usecs;
  114. qdf_log_timestamp_to_secs(hist->time, &secs, &usecs);
  115. print(print_priv, "% 8lld.%06lld %-25s %-7.d %d",
  116. secs,
  117. usecs,
  118. htc_credit_exchange_type_str(hist->type),
  119. hist->tx_credit,
  120. hist->htc_tx_queue_depth);
  121. --count;
  122. ++idx;
  123. if (idx >= HTC_CREDIT_HISTORY_MAX)
  124. idx = 0;
  125. }
  126. qdf_spin_unlock_bh(&g_htc_credit_lock);
  127. }
  128. #ifdef WLAN_HANG_EVENT
  129. void htc_log_hang_credit_history(struct notifier_block *block, void *data)
  130. {
  131. qdf_notif_block *notif_block = qdf_container_of(block, qdf_notif_block,
  132. notif_block);
  133. struct qdf_notifer_data *htc_hang_data = data;
  134. uint32_t count = NUM_HANG_CREDIT_HISTORY, idx, total_len;
  135. HTC_HANDLE htc;
  136. struct htc_hang_data_fixed_param *cmd;
  137. uint8_t *htc_buf_ptr;
  138. htc = notif_block->priv_data;
  139. if (!htc)
  140. return;
  141. if (!htc_hang_data)
  142. return;
  143. if (htc_hang_data->offset >= QDF_WLAN_MAX_HOST_OFFSET)
  144. return;
  145. total_len = sizeof(struct htc_hang_data_fixed_param);
  146. qdf_spin_lock_bh(&g_htc_credit_lock);
  147. if (count > HTC_CREDIT_HISTORY_MAX)
  148. count = HTC_CREDIT_HISTORY_MAX;
  149. if (count > g_htc_credit_history_length)
  150. count = g_htc_credit_history_length;
  151. idx = HTC_CREDIT_HISTORY_MAX + g_htc_credit_history_idx - count;
  152. idx %= HTC_CREDIT_HISTORY_MAX;
  153. qdf_spin_unlock_bh(&g_htc_credit_lock);
  154. while (count) {
  155. struct HTC_CREDIT_HISTORY *hist =
  156. &htc_credit_history_buffer[idx];
  157. htc_buf_ptr = htc_hang_data->hang_data + htc_hang_data->offset;
  158. cmd = (struct htc_hang_data_fixed_param *)htc_buf_ptr;
  159. QDF_HANG_EVT_SET_HDR(&cmd->tlv_header,
  160. HANG_EVT_TAG_HTC_CREDIT_HIST,
  161. QDF_HANG_GET_STRUCT_TLVLEN(struct htc_hang_data_fixed_param));
  162. qdf_mem_copy(&cmd->credit_hist, hist, sizeof(*hist));
  163. --count;
  164. ++idx;
  165. if (idx >= HTC_CREDIT_HISTORY_MAX)
  166. idx = 0;
  167. htc_hang_data->offset += total_len;
  168. }
  169. }
  170. #endif