dhd_linux_wq.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Broadcom Dongle Host Driver (DHD), Generic work queue framework
  3. * Generic interface to handle dhd deferred work events
  4. *
  5. * Copyright (C) 1999-2015, Broadcom Corporation
  6. *
  7. * Unless you and Broadcom execute a separate written software license
  8. * agreement governing use of this software, this software is licensed to you
  9. * under the terms of the GNU General Public License version 2 (the "GPL"),
  10. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  11. * following added to such license:
  12. *
  13. * As a special exception, the copyright holders of this software give you
  14. * permission to link this software with independent modules, and to copy and
  15. * distribute the resulting executable under terms of your choice, provided that
  16. * you also meet, for each linked independent module, the terms and conditions of
  17. * the license of that module. An independent module is a module which is not
  18. * derived from this software. The special exception does not apply to any
  19. * modifications of the software.
  20. *
  21. * Notwithstanding the above, under no circumstances may you combine this
  22. * software in any way with any other Broadcom software provided under a license
  23. * other than the GPL, without Broadcom's express prior written consent.
  24. *
  25. * $Id: dhd_linux_wq.c 411851 2013-07-10 20:48:00Z $
  26. */
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/fs.h>
  32. #include <linux/ip.h>
  33. #include <linux/kfifo.h>
  34. #include <linuxver.h>
  35. #include <osl.h>
  36. #include <bcmutils.h>
  37. #include <bcmendian.h>
  38. #include <bcmdevs.h>
  39. #include <dngl_stats.h>
  40. #include <dhd.h>
  41. #include <dhd_dbg.h>
  42. #include <dhd_linux_wq.h>
  43. struct dhd_deferred_event_t {
  44. u8 event; /* holds the event */
  45. void *event_data; /* Holds event specific data */
  46. event_handler_t event_handler;
  47. };
  48. #define DEFRD_EVT_SIZE sizeof(struct dhd_deferred_event_t)
  49. struct dhd_deferred_wq {
  50. struct work_struct deferred_work; /* should be the first member */
  51. /*
  52. * work events may occur simultaneously.
  53. * Can hold upto 64 low priority events and 4 high priority events
  54. */
  55. #define DHD_PRIO_WORK_FIFO_SIZE (4 * sizeof(struct dhd_deferred_event_t))
  56. #define DHD_WORK_FIFO_SIZE (64 * sizeof(struct dhd_deferred_event_t))
  57. struct kfifo *prio_fifo;
  58. struct kfifo *work_fifo;
  59. u8 *prio_fifo_buf;
  60. u8 *work_fifo_buf;
  61. spinlock_t work_lock;
  62. void *dhd_info; /* review: does it require */
  63. };
  64. struct dhd_deferred_wq *deferred_wq = NULL;
  65. static inline struct kfifo*
  66. dhd_kfifo_init(u8 *buf, int size, spinlock_t *lock)
  67. {
  68. struct kfifo *fifo;
  69. gfp_t flags = CAN_SLEEP()? GFP_KERNEL : GFP_ATOMIC;
  70. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33))
  71. fifo = kfifo_init(buf, size, flags, lock);
  72. #else
  73. fifo = (struct kfifo *)kzalloc(sizeof(struct kfifo), flags);
  74. if (!fifo) {
  75. return NULL;
  76. }
  77. kfifo_init(fifo, buf, size);
  78. #endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)) */
  79. return fifo;
  80. }
  81. static inline void
  82. dhd_kfifo_free(struct kfifo *fifo)
  83. {
  84. kfifo_free(fifo);
  85. }
  86. /* deferred work functions */
  87. static void dhd_deferred_work_handler(struct work_struct *data);
  88. void*
  89. dhd_deferred_work_init(void *dhd_info)
  90. {
  91. struct dhd_deferred_wq *work = NULL;
  92. u8* buf;
  93. unsigned long fifo_size = 0;
  94. gfp_t flags = CAN_SLEEP()? GFP_KERNEL : GFP_ATOMIC;
  95. if (!dhd_info) {
  96. DHD_ERROR(("%s: dhd info not initialized\n", __FUNCTION__));
  97. goto return_null;
  98. }
  99. work = (struct dhd_deferred_wq *)kzalloc(sizeof(struct dhd_deferred_wq),
  100. flags);
  101. if (!work) {
  102. DHD_ERROR(("%s: work queue creation failed \n", __FUNCTION__));
  103. goto return_null;
  104. }
  105. INIT_WORK((struct work_struct *)work, dhd_deferred_work_handler);
  106. /* initialize event fifo */
  107. spin_lock_init(&work->work_lock);
  108. /* allocate buffer to hold prio events */
  109. fifo_size = DHD_PRIO_WORK_FIFO_SIZE;
  110. fifo_size = is_power_of_2(fifo_size)? fifo_size : roundup_pow_of_two(fifo_size);
  111. buf = (u8*)kzalloc(fifo_size, flags);
  112. if (!buf) {
  113. DHD_ERROR(("%s: prio work fifo allocation failed \n", __FUNCTION__));
  114. goto return_null;
  115. }
  116. /* Initialize prio event fifo */
  117. work->prio_fifo = dhd_kfifo_init(buf, fifo_size, &work->work_lock);
  118. if (!work->prio_fifo) {
  119. kfree(buf);
  120. goto return_null;
  121. }
  122. /* allocate buffer to hold work events */
  123. fifo_size = DHD_WORK_FIFO_SIZE;
  124. fifo_size = is_power_of_2(fifo_size)? fifo_size : roundup_pow_of_two(fifo_size);
  125. buf = (u8*)kzalloc(fifo_size, flags);
  126. if (!buf) {
  127. DHD_ERROR(("%s: work fifo allocation failed \n", __FUNCTION__));
  128. goto return_null;
  129. }
  130. /* Initialize event fifo */
  131. work->work_fifo = dhd_kfifo_init(buf, fifo_size, &work->work_lock);
  132. if (!work->work_fifo) {
  133. kfree(buf);
  134. goto return_null;
  135. }
  136. work->dhd_info = dhd_info;
  137. deferred_wq = work;
  138. DHD_ERROR(("%s: work queue initialized \n", __FUNCTION__));
  139. return work;
  140. return_null:
  141. if (work)
  142. dhd_deferred_work_deinit(work);
  143. return NULL;
  144. }
  145. void
  146. dhd_deferred_work_deinit(void *work)
  147. {
  148. struct dhd_deferred_wq *deferred_work = work;
  149. if (!deferred_work) {
  150. DHD_ERROR(("%s: deferred work has been freed alread \n", __FUNCTION__));
  151. return;
  152. }
  153. /* cancel the deferred work handling */
  154. cancel_work_sync((struct work_struct *)deferred_work);
  155. /*
  156. * free work event fifo.
  157. * kfifo_free frees locally allocated fifo buffer
  158. */
  159. if (deferred_work->prio_fifo)
  160. dhd_kfifo_free(deferred_work->prio_fifo);
  161. if (deferred_work->work_fifo)
  162. dhd_kfifo_free(deferred_work->work_fifo);
  163. kfree(deferred_work);
  164. /* deinit internal reference pointer */
  165. deferred_wq = NULL;
  166. }
  167. /*
  168. * Prepares event to be queued
  169. * Schedules the event
  170. */
  171. int
  172. dhd_deferred_schedule_work(void *event_data, u8 event, event_handler_t event_handler, u8 priority)
  173. {
  174. struct dhd_deferred_event_t deferred_event;
  175. int status;
  176. if (!deferred_wq) {
  177. DHD_ERROR(("%s: work queue not initialized \n", __FUNCTION__));
  178. ASSERT(0);
  179. return DHD_WQ_STS_UNINITIALIZED;
  180. }
  181. if (!event || (event >= DHD_MAX_WQ_EVENTS)) {
  182. DHD_ERROR(("%s: Unknown event \n", __FUNCTION__));
  183. return DHD_WQ_STS_UNKNOWN_EVENT;
  184. }
  185. /*
  186. * default element size is 1, which can be changed
  187. * using kfifo_esize(). Older kernel(FC11) doesn't support
  188. * changing element size. For compatibility changing
  189. * element size is not prefered
  190. */
  191. ASSERT(kfifo_esize(deferred_wq->prio_fifo) == 1);
  192. ASSERT(kfifo_esize(deferred_wq->work_fifo) == 1);
  193. deferred_event.event = event;
  194. deferred_event.event_data = event_data;
  195. deferred_event.event_handler = event_handler;
  196. if (priority == DHD_WORK_PRIORITY_HIGH) {
  197. status = kfifo_in_spinlocked(deferred_wq->prio_fifo, &deferred_event,
  198. DEFRD_EVT_SIZE, &deferred_wq->work_lock);
  199. } else {
  200. status = kfifo_in_spinlocked(deferred_wq->work_fifo, &deferred_event,
  201. DEFRD_EVT_SIZE, &deferred_wq->work_lock);
  202. }
  203. if (!status) {
  204. return DHD_WQ_STS_SCHED_FAILED;
  205. }
  206. schedule_work((struct work_struct *)deferred_wq);
  207. return DHD_WQ_STS_OK;
  208. }
  209. static int
  210. dhd_get_scheduled_work(struct dhd_deferred_event_t *event)
  211. {
  212. int status = 0;
  213. if (!deferred_wq) {
  214. DHD_ERROR(("%s: work queue not initialized \n", __FUNCTION__));
  215. return DHD_WQ_STS_UNINITIALIZED;
  216. }
  217. /*
  218. * default element size is 1 byte, which can be changed
  219. * using kfifo_esize(). Older kernel(FC11) doesn't support
  220. * changing element size. For compatibility changing
  221. * element size is not prefered
  222. */
  223. ASSERT(kfifo_esize(deferred_wq->prio_fifo) == 1);
  224. ASSERT(kfifo_esize(deferred_wq->work_fifo) == 1);
  225. /* first read priorit event fifo */
  226. status = kfifo_out_spinlocked(deferred_wq->prio_fifo, event,
  227. DEFRD_EVT_SIZE, &deferred_wq->work_lock);
  228. if (!status) {
  229. /* priority fifo is empty. Now read low prio work fifo */
  230. status = kfifo_out_spinlocked(deferred_wq->work_fifo, event,
  231. DEFRD_EVT_SIZE, &deferred_wq->work_lock);
  232. }
  233. return status;
  234. }
  235. /*
  236. * Called when work is scheduled
  237. */
  238. static void
  239. dhd_deferred_work_handler(struct work_struct *work)
  240. {
  241. struct dhd_deferred_wq *deferred_work = (struct dhd_deferred_wq *)work;
  242. struct dhd_deferred_event_t work_event;
  243. int status;
  244. if (!deferred_work) {
  245. DHD_ERROR(("%s: work queue not initialized\n", __FUNCTION__));
  246. return;
  247. }
  248. do {
  249. status = dhd_get_scheduled_work(&work_event);
  250. DHD_TRACE(("%s: event to handle %d \n", __FUNCTION__, status));
  251. if (!status) {
  252. DHD_TRACE(("%s: No event to handle %d \n", __FUNCTION__, status));
  253. break;
  254. }
  255. if (work_event.event > DHD_MAX_WQ_EVENTS) {
  256. DHD_TRACE(("%s: Unknown event %d \n", __FUNCTION__, work_event.event));
  257. break;
  258. }
  259. if (work_event.event_handler) {
  260. work_event.event_handler(deferred_work->dhd_info,
  261. work_event.event_data, work_event.event);
  262. } else {
  263. DHD_ERROR(("%s: event not defined %d\n", __FUNCTION__, work_event.event));
  264. }
  265. } while (1);
  266. return;
  267. }