wlan_osif_request_manager.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2017-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 <linux/kernel.h>
  19. #include "qdf_mem.h"
  20. #include "qdf_list.h"
  21. #include "qdf_event.h"
  22. #include "wlan_cfg80211.h"
  23. #include "wlan_osif_request_manager.h"
  24. /* arbitrary value */
  25. #define MAX_NUM_REQUESTS 20
  26. static bool is_initialized;
  27. static qdf_list_t requests;
  28. static qdf_spinlock_t spinlock;
  29. static void *cookie;
  30. struct osif_request {
  31. qdf_list_node_t node;
  32. void *cookie;
  33. uint32_t reference_count;
  34. struct osif_request_params params;
  35. qdf_event_t completed;
  36. };
  37. /* must be called with spinlock held */
  38. static void osif_request_unlink(struct osif_request *request)
  39. {
  40. qdf_list_remove_node(&requests, &request->node);
  41. }
  42. static void osif_request_destroy(struct osif_request *request)
  43. {
  44. struct osif_request_params *params;
  45. params = &request->params;
  46. if (params->dealloc) {
  47. void *priv = osif_request_priv(request);
  48. params->dealloc(priv);
  49. }
  50. qdf_event_destroy(&request->completed);
  51. qdf_mem_free(request);
  52. }
  53. /* must be called with spinlock held */
  54. static struct osif_request *osif_request_find(void *cookie)
  55. {
  56. QDF_STATUS status;
  57. struct osif_request *request;
  58. qdf_list_node_t *node;
  59. status = qdf_list_peek_front(&requests, &node);
  60. while (QDF_IS_STATUS_SUCCESS(status)) {
  61. request = qdf_container_of(node, struct osif_request, node);
  62. if (request->cookie == cookie)
  63. return request;
  64. status = qdf_list_peek_next(&requests, node, &node);
  65. }
  66. return NULL;
  67. }
  68. struct osif_request *osif_request_alloc(const struct osif_request_params *params)
  69. {
  70. size_t length;
  71. struct osif_request *request;
  72. if (!is_initialized) {
  73. cfg80211_err("invoked when not initialized from %pS",
  74. (void *)_RET_IP_);
  75. return NULL;
  76. }
  77. length = sizeof(*request) + params->priv_size;
  78. request = qdf_mem_malloc(length);
  79. if (!request) {
  80. cfg80211_err("allocation failed for %pS", (void *)_RET_IP_);
  81. return NULL;
  82. }
  83. request->reference_count = 1;
  84. request->params = *params;
  85. qdf_event_create(&request->completed);
  86. qdf_spin_lock_bh(&spinlock);
  87. request->cookie = cookie++;
  88. qdf_list_insert_back(&requests, &request->node);
  89. qdf_spin_unlock_bh(&spinlock);
  90. cfg80211_debug("request %pK, cookie %pK, caller %pS",
  91. request, request->cookie, (void *)_RET_IP_);
  92. return request;
  93. }
  94. void *osif_request_priv(struct osif_request *request)
  95. {
  96. /* private data area immediately follows the struct osif_request */
  97. return request + 1;
  98. }
  99. void *osif_request_cookie(struct osif_request *request)
  100. {
  101. return request->cookie;
  102. }
  103. struct osif_request *osif_request_get(void *cookie)
  104. {
  105. struct osif_request *request;
  106. if (!is_initialized) {
  107. cfg80211_err("invoked when not initialized from %pS",
  108. (void *)_RET_IP_);
  109. return NULL;
  110. }
  111. qdf_spin_lock_bh(&spinlock);
  112. request = osif_request_find(cookie);
  113. if (request)
  114. request->reference_count++;
  115. qdf_spin_unlock_bh(&spinlock);
  116. cfg80211_debug("cookie %pK, request %pK, caller %pS",
  117. cookie, request, (void *)_RET_IP_);
  118. return request;
  119. }
  120. void osif_request_put(struct osif_request *request)
  121. {
  122. bool unlinked = false;
  123. cfg80211_debug("request %pK, cookie %pK, caller %pS",
  124. request, request->cookie, (void *)_RET_IP_);
  125. qdf_spin_lock_bh(&spinlock);
  126. request->reference_count--;
  127. if (0 == request->reference_count) {
  128. osif_request_unlink(request);
  129. unlinked = true;
  130. }
  131. qdf_spin_unlock_bh(&spinlock);
  132. if (unlinked)
  133. osif_request_destroy(request);
  134. }
  135. int osif_request_wait_for_response(struct osif_request *request)
  136. {
  137. QDF_STATUS status;
  138. status = qdf_wait_for_event_completion(&request->completed,
  139. request->params.timeout_ms);
  140. return qdf_status_to_os_return(status);
  141. }
  142. void osif_request_complete(struct osif_request *request)
  143. {
  144. (void) qdf_event_set(&request->completed);
  145. }
  146. void osif_request_manager_init(void)
  147. {
  148. cfg80211_debug("%pS", (void *)_RET_IP_);
  149. if (is_initialized)
  150. return;
  151. qdf_list_create(&requests, MAX_NUM_REQUESTS);
  152. qdf_spinlock_create(&spinlock);
  153. is_initialized = true;
  154. }
  155. /*
  156. * osif_request_manager_deinit implementation note:
  157. * It is intentional that we do not destroy the list or the spinlock.
  158. * This allows threads to still access the infrastructure even when it
  159. * has been deinitialized. Since neither lists nor spinlocks consume
  160. * resources this does not result in a resource leak.
  161. */
  162. void osif_request_manager_deinit(void)
  163. {
  164. cfg80211_debug("%pS", (void *)_RET_IP_);
  165. is_initialized = false;
  166. }