wlan_osif_request_manager.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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");
  74. return NULL;
  75. }
  76. length = sizeof(*request) + params->priv_size;
  77. request = qdf_mem_malloc(length);
  78. if (!request) {
  79. cfg80211_err("allocation failed");
  80. return NULL;
  81. }
  82. request->reference_count = 1;
  83. request->params = *params;
  84. qdf_event_create(&request->completed);
  85. qdf_spin_lock_bh(&spinlock);
  86. request->cookie = cookie++;
  87. qdf_list_insert_back(&requests, &request->node);
  88. qdf_spin_unlock_bh(&spinlock);
  89. cfg80211_debug("request %pK, cookie %pK",
  90. request, request->cookie);
  91. return request;
  92. }
  93. void *osif_request_priv(struct osif_request *request)
  94. {
  95. /* private data area immediately follows the struct osif_request */
  96. return request + 1;
  97. }
  98. void *osif_request_cookie(struct osif_request *request)
  99. {
  100. return request->cookie;
  101. }
  102. struct osif_request *osif_request_get(void *cookie)
  103. {
  104. struct osif_request *request;
  105. if (!is_initialized) {
  106. cfg80211_err("invoked when not initialized");
  107. return NULL;
  108. }
  109. qdf_spin_lock_bh(&spinlock);
  110. request = osif_request_find(cookie);
  111. if (request)
  112. request->reference_count++;
  113. qdf_spin_unlock_bh(&spinlock);
  114. cfg80211_debug("cookie %pK, request %pK",
  115. cookie, request);
  116. return request;
  117. }
  118. void osif_request_put(struct osif_request *request)
  119. {
  120. bool unlinked = false;
  121. cfg80211_debug("request %pK, cookie %pK",
  122. request, request->cookie);
  123. qdf_spin_lock_bh(&spinlock);
  124. request->reference_count--;
  125. if (0 == request->reference_count) {
  126. osif_request_unlink(request);
  127. unlinked = true;
  128. }
  129. qdf_spin_unlock_bh(&spinlock);
  130. if (unlinked)
  131. osif_request_destroy(request);
  132. }
  133. int osif_request_wait_for_response(struct osif_request *request)
  134. {
  135. QDF_STATUS status;
  136. status = qdf_wait_for_event_completion(&request->completed,
  137. request->params.timeout_ms);
  138. return qdf_status_to_os_return(status);
  139. }
  140. void osif_request_complete(struct osif_request *request)
  141. {
  142. (void) qdf_event_set(&request->completed);
  143. }
  144. void osif_request_manager_init(void)
  145. {
  146. if (is_initialized)
  147. return;
  148. qdf_list_create(&requests, MAX_NUM_REQUESTS);
  149. qdf_spinlock_create(&spinlock);
  150. is_initialized = true;
  151. }
  152. /*
  153. * osif_request_manager_deinit implementation note:
  154. * It is intentional that we do not destroy the list or the spinlock.
  155. * This allows threads to still access the infrastructure even when it
  156. * has been deinitialized. Since neither lists nor spinlocks consume
  157. * resources this does not result in a resource leak.
  158. */
  159. void osif_request_manager_deinit(void)
  160. {
  161. is_initialized = false;
  162. }