osif_vdev_sync.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (c) 2018-2019, 2021 The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for
  6. * any purpose with or without fee is hereby granted, provided that the
  7. * above copyright notice and this permission notice appear in all
  8. * copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  11. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  12. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  13. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  14. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  15. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. * PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. #include "linux/device.h"
  20. #include "linux/netdevice.h"
  21. #include "__osif_psoc_sync.h"
  22. #include "__osif_vdev_sync.h"
  23. #include "osif_vdev_sync.h"
  24. #include "qdf_lock.h"
  25. #include "qdf_status.h"
  26. #include "qdf_types.h"
  27. #include <qdf_trace.h>
  28. #include <wlan_cfg80211.h>
  29. static struct osif_vdev_sync __osif_vdev_sync_arr[WLAN_MAX_VDEVS +
  30. WLAN_MAX_ML_VDEVS];
  31. static qdf_spinlock_t __osif_vdev_sync_lock;
  32. #define osif_vdev_sync_lock_create() qdf_spinlock_create(&__osif_vdev_sync_lock)
  33. #define osif_vdev_sync_lock_destroy() \
  34. qdf_spinlock_destroy(&__osif_vdev_sync_lock)
  35. #define osif_vdev_sync_lock() qdf_spin_lock_bh(&__osif_vdev_sync_lock)
  36. #define osif_vdev_sync_unlock() qdf_spin_unlock_bh(&__osif_vdev_sync_lock)
  37. static struct osif_vdev_sync *osif_vdev_sync_lookup(struct net_device *net_dev)
  38. {
  39. int i;
  40. for (i = 0; i < QDF_ARRAY_SIZE(__osif_vdev_sync_arr); i++) {
  41. struct osif_vdev_sync *vdev_sync = __osif_vdev_sync_arr + i;
  42. if (!vdev_sync->in_use)
  43. continue;
  44. if (vdev_sync->net_dev == net_dev)
  45. return vdev_sync;
  46. }
  47. return NULL;
  48. }
  49. struct osif_vdev_sync *osif_get_vdev_sync_arr(void)
  50. {
  51. return __osif_vdev_sync_arr;
  52. }
  53. static struct osif_vdev_sync *osif_vdev_sync_get(void)
  54. {
  55. int i;
  56. for (i = 0; i < QDF_ARRAY_SIZE(__osif_vdev_sync_arr); i++) {
  57. struct osif_vdev_sync *vdev_sync = __osif_vdev_sync_arr + i;
  58. if (!vdev_sync->in_use) {
  59. vdev_sync->in_use = true;
  60. return vdev_sync;
  61. }
  62. }
  63. return NULL;
  64. }
  65. static void osif_vdev_sync_put(struct osif_vdev_sync *vdev_sync)
  66. {
  67. qdf_mem_zero(vdev_sync, sizeof(*vdev_sync));
  68. }
  69. int osif_vdev_sync_create(struct device *dev,
  70. struct osif_vdev_sync **out_vdev_sync)
  71. {
  72. struct osif_vdev_sync *vdev_sync;
  73. QDF_STATUS status;
  74. QDF_BUG(dev);
  75. if (!dev)
  76. return -EINVAL;
  77. QDF_BUG(out_vdev_sync);
  78. if (!out_vdev_sync)
  79. return -EINVAL;
  80. osif_vdev_sync_lock();
  81. vdev_sync = osif_vdev_sync_get();
  82. osif_vdev_sync_unlock();
  83. if (!vdev_sync)
  84. return -ENOMEM;
  85. status = osif_psoc_sync_dsc_vdev_create(dev, &vdev_sync->dsc_vdev);
  86. if (QDF_IS_STATUS_ERROR(status))
  87. goto sync_put;
  88. *out_vdev_sync = vdev_sync;
  89. return 0;
  90. sync_put:
  91. osif_vdev_sync_lock();
  92. osif_vdev_sync_put(vdev_sync);
  93. osif_vdev_sync_unlock();
  94. return qdf_status_to_os_return(status);
  95. }
  96. int __osif_vdev_sync_create_and_trans(struct device *dev,
  97. struct osif_vdev_sync **out_vdev_sync,
  98. const char *desc)
  99. {
  100. struct osif_vdev_sync *vdev_sync;
  101. QDF_STATUS status;
  102. int errno;
  103. errno = osif_vdev_sync_create(dev, &vdev_sync);
  104. if (errno)
  105. return errno;
  106. status = dsc_vdev_trans_start(vdev_sync->dsc_vdev, desc);
  107. if (QDF_IS_STATUS_ERROR(status))
  108. goto sync_destroy;
  109. *out_vdev_sync = vdev_sync;
  110. return 0;
  111. sync_destroy:
  112. osif_vdev_sync_destroy(vdev_sync);
  113. return qdf_status_to_os_return(status);
  114. }
  115. void osif_vdev_sync_destroy(struct osif_vdev_sync *vdev_sync)
  116. {
  117. QDF_BUG(vdev_sync);
  118. if (!vdev_sync)
  119. return;
  120. dsc_vdev_destroy(&vdev_sync->dsc_vdev);
  121. osif_vdev_sync_lock();
  122. osif_vdev_sync_put(vdev_sync);
  123. osif_vdev_sync_unlock();
  124. }
  125. void osif_vdev_sync_register(struct net_device *net_dev,
  126. struct osif_vdev_sync *vdev_sync)
  127. {
  128. QDF_BUG(net_dev);
  129. QDF_BUG(vdev_sync);
  130. if (!vdev_sync)
  131. return;
  132. osif_vdev_sync_lock();
  133. vdev_sync->net_dev = net_dev;
  134. osif_vdev_sync_unlock();
  135. }
  136. struct osif_vdev_sync *osif_vdev_sync_unregister(struct net_device *net_dev)
  137. {
  138. struct osif_vdev_sync *vdev_sync;
  139. QDF_BUG(net_dev);
  140. if (!net_dev)
  141. return NULL;
  142. osif_vdev_sync_lock();
  143. vdev_sync = osif_vdev_sync_lookup(net_dev);
  144. if (vdev_sync)
  145. vdev_sync->net_dev = NULL;
  146. osif_vdev_sync_unlock();
  147. return vdev_sync;
  148. }
  149. typedef QDF_STATUS (*vdev_start_func)(struct dsc_vdev *, const char *);
  150. static int
  151. __osif_vdev_sync_start_callback(struct net_device *net_dev,
  152. struct osif_vdev_sync **out_vdev_sync,
  153. const char *desc,
  154. vdev_start_func vdev_start_cb)
  155. {
  156. QDF_STATUS status;
  157. struct osif_vdev_sync *vdev_sync;
  158. *out_vdev_sync = NULL;
  159. vdev_sync = osif_vdev_sync_lookup(net_dev);
  160. if (!vdev_sync)
  161. return -EAGAIN;
  162. *out_vdev_sync = vdev_sync;
  163. status = vdev_start_cb(vdev_sync->dsc_vdev, desc);
  164. if (QDF_IS_STATUS_ERROR(status))
  165. return qdf_status_to_os_return(status);
  166. return 0;
  167. }
  168. static int
  169. __osif_vdev_sync_start_wait_callback(struct net_device *net_dev,
  170. struct osif_vdev_sync **out_vdev_sync,
  171. const char *desc,
  172. vdev_start_func vdev_start_cb)
  173. {
  174. QDF_STATUS status;
  175. struct osif_vdev_sync *vdev_sync;
  176. *out_vdev_sync = NULL;
  177. osif_vdev_sync_lock();
  178. vdev_sync = osif_vdev_sync_lookup(net_dev);
  179. osif_vdev_sync_unlock();
  180. if (!vdev_sync)
  181. return -EAGAIN;
  182. status = vdev_start_cb(vdev_sync->dsc_vdev, desc);
  183. if (QDF_IS_STATUS_ERROR(status))
  184. return qdf_status_to_os_return(status);
  185. *out_vdev_sync = vdev_sync;
  186. return 0;
  187. }
  188. int __osif_vdev_sync_trans_start(struct net_device *net_dev,
  189. struct osif_vdev_sync **out_vdev_sync,
  190. const char *desc)
  191. {
  192. int errno;
  193. osif_vdev_sync_lock();
  194. errno = __osif_vdev_sync_start_callback(net_dev, out_vdev_sync, desc,
  195. dsc_vdev_trans_start);
  196. osif_vdev_sync_unlock();
  197. if (!errno)
  198. osif_vdev_sync_wait_for_ops(*out_vdev_sync);
  199. return errno;
  200. }
  201. int __osif_vdev_sync_trans_start_wait(struct net_device *net_dev,
  202. struct osif_vdev_sync **out_vdev_sync,
  203. const char *desc)
  204. {
  205. int errno;
  206. /* since dsc_vdev_trans_start_wait may sleep do not take lock here */
  207. errno = __osif_vdev_sync_start_wait_callback(net_dev,
  208. out_vdev_sync, desc,
  209. dsc_vdev_trans_start_wait);
  210. if (!errno)
  211. osif_vdev_sync_wait_for_ops(*out_vdev_sync);
  212. return errno;
  213. }
  214. void osif_vdev_sync_trans_stop(struct osif_vdev_sync *vdev_sync)
  215. {
  216. dsc_vdev_trans_stop(vdev_sync->dsc_vdev);
  217. }
  218. void osif_vdev_sync_assert_trans_protected(struct net_device *net_dev)
  219. {
  220. struct osif_vdev_sync *vdev_sync;
  221. osif_vdev_sync_lock();
  222. vdev_sync = osif_vdev_sync_lookup(net_dev);
  223. QDF_BUG(vdev_sync);
  224. if (vdev_sync)
  225. dsc_vdev_assert_trans_protected(vdev_sync->dsc_vdev);
  226. osif_vdev_sync_unlock();
  227. }
  228. int __osif_vdev_sync_op_start(struct net_device *net_dev,
  229. struct osif_vdev_sync **out_vdev_sync,
  230. const char *func)
  231. {
  232. int errno;
  233. osif_vdev_sync_lock();
  234. errno = __osif_vdev_sync_start_callback(net_dev, out_vdev_sync, func,
  235. _dsc_vdev_op_start);
  236. osif_vdev_sync_unlock();
  237. return errno;
  238. }
  239. void __osif_vdev_sync_op_stop(struct osif_vdev_sync *vdev_sync,
  240. const char *func)
  241. {
  242. _dsc_vdev_op_stop(vdev_sync->dsc_vdev, func);
  243. }
  244. void osif_vdev_sync_wait_for_ops(struct osif_vdev_sync *vdev_sync)
  245. {
  246. dsc_vdev_wait_for_ops(vdev_sync->dsc_vdev);
  247. }
  248. void osif_vdev_sync_init(void)
  249. {
  250. osif_vdev_sync_lock_create();
  251. }
  252. void osif_vdev_sync_deinit(void)
  253. {
  254. osif_vdev_sync_lock_destroy();
  255. }
  256. uint8_t osif_vdev_get_cached_cmd(struct osif_vdev_sync *vdev_sync)
  257. {
  258. return dsc_vdev_get_cached_cmd(vdev_sync->dsc_vdev);
  259. }
  260. void osif_vdev_cache_command(struct osif_vdev_sync *vdev_sync, uint8_t cmd_id)
  261. {
  262. dsc_vdev_cache_command(vdev_sync->dsc_vdev, cmd_id);
  263. osif_debug("Set cache cmd to %d", cmd_id);
  264. }