wlan_osif_request_manager.c 4.5 KB

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