wlan_osif_request_manager.c 4.7 KB

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