osif_driver_sync.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) 2019, 2021 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 "__osif_driver_sync.h"
  19. #include "osif_driver_sync.h"
  20. #include "qdf_lock.h"
  21. #include "qdf_status.h"
  22. #include "qdf_types.h"
  23. #include "wlan_dsc_driver.h"
  24. #include "wlan_dsc_psoc.h"
  25. /**
  26. * struct osif_driver_sync - a driver synchronization context
  27. * @dsc_driver: the dsc_driver used for synchronization
  28. * @in_use: indicates if the context is being used
  29. * @is_registered: indicates if the context is registered for
  30. * transitions/operations
  31. */
  32. struct osif_driver_sync {
  33. struct dsc_driver *dsc_driver;
  34. bool in_use;
  35. bool is_registered;
  36. };
  37. static struct osif_driver_sync __osif_driver_sync;
  38. static qdf_spinlock_t __osif_driver_sync_lock;
  39. #define osif_driver_sync_lock_create() \
  40. qdf_spinlock_create(&__osif_driver_sync_lock)
  41. #define osif_driver_sync_lock_destroy() \
  42. qdf_spinlock_destroy(&__osif_driver_sync_lock)
  43. #define osif_driver_sync_lock() qdf_spin_lock_bh(&__osif_driver_sync_lock)
  44. #define osif_driver_sync_unlock() qdf_spin_unlock_bh(&__osif_driver_sync_lock)
  45. static struct osif_driver_sync *osif_driver_sync_lookup(void)
  46. {
  47. if (!__osif_driver_sync.is_registered)
  48. return NULL;
  49. return &__osif_driver_sync;
  50. }
  51. static struct osif_driver_sync *osif_driver_sync_get(void)
  52. {
  53. if (__osif_driver_sync.in_use)
  54. return NULL;
  55. __osif_driver_sync.in_use = true;
  56. return &__osif_driver_sync;
  57. }
  58. static void osif_driver_sync_put(struct osif_driver_sync *driver_sync)
  59. {
  60. qdf_mem_zero(driver_sync, sizeof(*driver_sync));
  61. }
  62. int osif_driver_sync_create(struct osif_driver_sync **out_driver_sync)
  63. {
  64. QDF_STATUS status;
  65. struct osif_driver_sync *driver_sync;
  66. QDF_BUG(out_driver_sync);
  67. if (!out_driver_sync)
  68. return -EINVAL;
  69. osif_driver_sync_lock();
  70. driver_sync = osif_driver_sync_get();
  71. osif_driver_sync_unlock();
  72. if (!driver_sync)
  73. return -ENOMEM;
  74. status = dsc_driver_create(&driver_sync->dsc_driver);
  75. if (QDF_IS_STATUS_ERROR(status))
  76. goto sync_put;
  77. *out_driver_sync = driver_sync;
  78. return 0;
  79. sync_put:
  80. osif_driver_sync_lock();
  81. osif_driver_sync_put(driver_sync);
  82. osif_driver_sync_unlock();
  83. return qdf_status_to_os_return(status);
  84. }
  85. int
  86. __osif_driver_sync_create_and_trans(struct osif_driver_sync **out_driver_sync,
  87. const char *desc)
  88. {
  89. struct osif_driver_sync *driver_sync;
  90. QDF_STATUS status;
  91. int errno;
  92. errno = osif_driver_sync_create(&driver_sync);
  93. if (errno)
  94. return errno;
  95. status = dsc_driver_trans_start(driver_sync->dsc_driver, desc);
  96. if (QDF_IS_STATUS_ERROR(status))
  97. goto sync_destroy;
  98. *out_driver_sync = driver_sync;
  99. return 0;
  100. sync_destroy:
  101. osif_driver_sync_destroy(driver_sync);
  102. return qdf_status_to_os_return(status);
  103. }
  104. void osif_driver_sync_destroy(struct osif_driver_sync *driver_sync)
  105. {
  106. QDF_BUG(driver_sync);
  107. if (!driver_sync)
  108. return;
  109. dsc_driver_destroy(&driver_sync->dsc_driver);
  110. osif_driver_sync_lock();
  111. osif_driver_sync_put(driver_sync);
  112. osif_driver_sync_unlock();
  113. }
  114. void osif_driver_sync_register(struct osif_driver_sync *driver_sync)
  115. {
  116. QDF_BUG(driver_sync);
  117. if (!driver_sync)
  118. return;
  119. osif_driver_sync_lock();
  120. driver_sync->is_registered = true;
  121. osif_driver_sync_unlock();
  122. }
  123. struct osif_driver_sync *osif_driver_sync_unregister(void)
  124. {
  125. struct osif_driver_sync *driver_sync;
  126. osif_driver_sync_lock();
  127. driver_sync = osif_driver_sync_lookup();
  128. if (driver_sync)
  129. driver_sync->is_registered = false;
  130. osif_driver_sync_unlock();
  131. return driver_sync;
  132. }
  133. typedef QDF_STATUS (*driver_start_func)(struct dsc_driver *, const char *);
  134. static int
  135. __osif_driver_sync_start_callback(struct osif_driver_sync **out_driver_sync,
  136. const char *desc,
  137. driver_start_func driver_start_cb)
  138. {
  139. QDF_STATUS status;
  140. struct osif_driver_sync *driver_sync;
  141. *out_driver_sync = NULL;
  142. driver_sync = osif_driver_sync_lookup();
  143. if (!driver_sync)
  144. return -EAGAIN;
  145. status = driver_start_cb(driver_sync->dsc_driver, desc);
  146. if (QDF_IS_STATUS_ERROR(status))
  147. return qdf_status_to_os_return(status);
  148. *out_driver_sync = driver_sync;
  149. return 0;
  150. }
  151. static int
  152. __osif_driver_sync_start_wait_callback(
  153. struct osif_driver_sync **out_driver_sync,
  154. const char *desc,
  155. driver_start_func driver_start_cb)
  156. {
  157. QDF_STATUS status;
  158. struct osif_driver_sync *driver_sync;
  159. *out_driver_sync = NULL;
  160. osif_driver_sync_lock();
  161. driver_sync = osif_driver_sync_lookup();
  162. osif_driver_sync_unlock();
  163. if (!driver_sync)
  164. return -EAGAIN;
  165. status = driver_start_cb(driver_sync->dsc_driver, desc);
  166. if (QDF_IS_STATUS_ERROR(status))
  167. return qdf_status_to_os_return(status);
  168. *out_driver_sync = driver_sync;
  169. return 0;
  170. }
  171. int __osif_driver_sync_trans_start(struct osif_driver_sync **out_driver_sync,
  172. const char *desc)
  173. {
  174. int errno;
  175. osif_driver_sync_lock();
  176. errno = __osif_driver_sync_start_callback(out_driver_sync, desc,
  177. dsc_driver_trans_start);
  178. osif_driver_sync_unlock();
  179. return errno;
  180. }
  181. int
  182. __osif_driver_sync_trans_start_wait(struct osif_driver_sync **out_driver_sync,
  183. const char *desc)
  184. {
  185. int errno;
  186. /* since dsc_driver_trans_start_wait may sleep do not take lock here */
  187. errno = __osif_driver_sync_start_wait_callback(out_driver_sync, desc,
  188. dsc_driver_trans_start_wait);
  189. return errno;
  190. }
  191. void osif_driver_sync_trans_stop(struct osif_driver_sync *driver_sync)
  192. {
  193. dsc_driver_trans_stop(driver_sync->dsc_driver);
  194. }
  195. void osif_driver_sync_assert_trans_protected(void)
  196. {
  197. struct osif_driver_sync *driver_sync;
  198. osif_driver_sync_lock();
  199. driver_sync = osif_driver_sync_lookup();
  200. QDF_BUG(driver_sync);
  201. if (driver_sync)
  202. dsc_driver_assert_trans_protected(driver_sync->dsc_driver);
  203. osif_driver_sync_unlock();
  204. }
  205. int __osif_driver_sync_op_start(struct osif_driver_sync **out_driver_sync,
  206. const char *func)
  207. {
  208. int errno;
  209. osif_driver_sync_lock();
  210. errno = __osif_driver_sync_start_callback(out_driver_sync, func,
  211. _dsc_driver_op_start);
  212. osif_driver_sync_unlock();
  213. return errno;
  214. }
  215. void __osif_driver_sync_op_stop(struct osif_driver_sync *driver_sync,
  216. const char *func)
  217. {
  218. _dsc_driver_op_stop(driver_sync->dsc_driver, func);
  219. }
  220. void osif_driver_sync_wait_for_ops(struct osif_driver_sync *driver_sync)
  221. {
  222. dsc_driver_wait_for_ops(driver_sync->dsc_driver);
  223. }
  224. void osif_driver_sync_init(void)
  225. {
  226. osif_driver_sync_lock_create();
  227. }
  228. void osif_driver_sync_deinit(void)
  229. {
  230. osif_driver_sync_lock_destroy();
  231. }
  232. static QDF_STATUS
  233. __osif_driver_sync_dsc_psoc_create(struct dsc_psoc **out_dsc_psoc)
  234. {
  235. struct osif_driver_sync *driver_sync;
  236. driver_sync = osif_driver_sync_lookup();
  237. if (!driver_sync)
  238. return QDF_STATUS_E_INVAL;
  239. return dsc_psoc_create(driver_sync->dsc_driver, out_dsc_psoc);
  240. }
  241. QDF_STATUS osif_driver_sync_dsc_psoc_create(struct dsc_psoc **out_dsc_psoc)
  242. {
  243. QDF_STATUS status;
  244. osif_driver_sync_lock();
  245. status = __osif_driver_sync_dsc_psoc_create(out_dsc_psoc);
  246. osif_driver_sync_unlock();
  247. return status;
  248. }