wlan_osif_request_manager.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2017-2018 The Linux Foundation. All rights reserved.
  3. *
  4. * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
  5. *
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for
  8. * any purpose with or without fee is hereby granted, provided that the
  9. * above copyright notice and this permission notice appear in all
  10. * copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  13. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  15. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  16. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  17. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  18. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  19. * PERFORMANCE OF THIS SOFTWARE.
  20. */
  21. /*
  22. * This file was originally distributed by Qualcomm Atheros, Inc.
  23. * under proprietary terms before Copyright ownership was assigned
  24. * to the Linux Foundation.
  25. */
  26. #include <linux/kernel.h>
  27. #include "qdf_mem.h"
  28. #include "qdf_list.h"
  29. #include "qdf_event.h"
  30. #include "wlan_cfg80211.h"
  31. #include "wlan_osif_request_manager.h"
  32. /* arbitrary value */
  33. #define MAX_NUM_REQUESTS 20
  34. static bool is_initialized;
  35. static qdf_list_t requests;
  36. static qdf_spinlock_t spinlock;
  37. static void *cookie;
  38. struct osif_request {
  39. qdf_list_node_t node;
  40. void *cookie;
  41. uint32_t reference_count;
  42. struct osif_request_params params;
  43. qdf_event_t completed;
  44. };
  45. /* must be called with spinlock held */
  46. static void osif_request_unlink(struct osif_request *request)
  47. {
  48. qdf_list_remove_node(&requests, &request->node);
  49. }
  50. static void osif_request_destroy(struct osif_request *request)
  51. {
  52. struct osif_request_params *params;
  53. params = &request->params;
  54. if (params->dealloc) {
  55. void *priv = osif_request_priv(request);
  56. params->dealloc(priv);
  57. }
  58. qdf_event_destroy(&request->completed);
  59. qdf_mem_free(request);
  60. }
  61. /* must be called with spinlock held */
  62. static struct osif_request *osif_request_find(void *cookie)
  63. {
  64. QDF_STATUS status;
  65. struct osif_request *request;
  66. qdf_list_node_t *node;
  67. status = qdf_list_peek_front(&requests, &node);
  68. while (QDF_IS_STATUS_SUCCESS(status)) {
  69. request = qdf_container_of(node, struct osif_request, node);
  70. if (request->cookie == cookie)
  71. return request;
  72. status = qdf_list_peek_next(&requests, node, &node);
  73. }
  74. return NULL;
  75. }
  76. struct osif_request *osif_request_alloc(const struct osif_request_params *params)
  77. {
  78. size_t length;
  79. struct osif_request *request;
  80. if (!is_initialized) {
  81. cfg80211_err("invoked when not initialized from %pS",
  82. (void *)_RET_IP_);
  83. return NULL;
  84. }
  85. length = sizeof(*request) + params->priv_size;
  86. request = qdf_mem_malloc(length);
  87. if (!request) {
  88. cfg80211_err("allocation failed for %pS", (void *)_RET_IP_);
  89. return NULL;
  90. }
  91. request->reference_count = 1;
  92. request->params = *params;
  93. qdf_event_create(&request->completed);
  94. qdf_spin_lock_bh(&spinlock);
  95. request->cookie = cookie++;
  96. qdf_list_insert_back(&requests, &request->node);
  97. qdf_spin_unlock_bh(&spinlock);
  98. cfg80211_debug("request %pK, cookie %pK, caller %pS",
  99. request, request->cookie, (void *)_RET_IP_);
  100. return request;
  101. }
  102. void *osif_request_priv(struct osif_request *request)
  103. {
  104. /* private data area immediately follows the struct osif_request */
  105. return request + 1;
  106. }
  107. void *osif_request_cookie(struct osif_request *request)
  108. {
  109. return request->cookie;
  110. }
  111. struct osif_request *osif_request_get(void *cookie)
  112. {
  113. struct osif_request *request;
  114. if (!is_initialized) {
  115. cfg80211_err("invoked when not initialized from %pS",
  116. (void *)_RET_IP_);
  117. return NULL;
  118. }
  119. qdf_spin_lock_bh(&spinlock);
  120. request = osif_request_find(cookie);
  121. if (request)
  122. request->reference_count++;
  123. qdf_spin_unlock_bh(&spinlock);
  124. cfg80211_debug("cookie %pK, request %pK, caller %pS",
  125. cookie, request, (void *)_RET_IP_);
  126. return request;
  127. }
  128. void osif_request_put(struct osif_request *request)
  129. {
  130. bool unlinked = false;
  131. cfg80211_debug("request %pK, cookie %pK, caller %pS",
  132. request, request->cookie, (void *)_RET_IP_);
  133. qdf_spin_lock_bh(&spinlock);
  134. request->reference_count--;
  135. if (0 == request->reference_count) {
  136. osif_request_unlink(request);
  137. unlinked = true;
  138. }
  139. qdf_spin_unlock_bh(&spinlock);
  140. if (unlinked)
  141. osif_request_destroy(request);
  142. }
  143. int osif_request_wait_for_response(struct osif_request *request)
  144. {
  145. QDF_STATUS status;
  146. status = qdf_wait_for_event_completion(&request->completed,
  147. request->params.timeout_ms);
  148. return qdf_status_to_os_return(status);
  149. }
  150. void osif_request_complete(struct osif_request *request)
  151. {
  152. (void) qdf_event_set(&request->completed);
  153. }
  154. void osif_request_manager_init(void)
  155. {
  156. cfg80211_debug("%pS", (void *)_RET_IP_);
  157. if (is_initialized)
  158. return;
  159. qdf_list_create(&requests, MAX_NUM_REQUESTS);
  160. qdf_spinlock_create(&spinlock);
  161. is_initialized = true;
  162. }
  163. /*
  164. * osif_request_manager_deinit implementation note:
  165. * It is intentional that we do not destroy the list or the spinlock.
  166. * This allows threads to still access the infrastructure even when it
  167. * has been deinitialized. Since neither lists nor spinlocks consume
  168. * resources this does not result in a resource leak.
  169. */
  170. void osif_request_manager_deinit(void)
  171. {
  172. cfg80211_debug("%pS", (void *)_RET_IP_);
  173. is_initialized = false;
  174. }