wlan_osif_request_manager.c 4.7 KB

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