commonring.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright (c) 2014 Broadcom Corporation
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include <linux/types.h>
  16. #include <linux/netdevice.h>
  17. #include <brcmu_utils.h>
  18. #include <brcmu_wifi.h>
  19. #include "core.h"
  20. #include "commonring.h"
  21. void brcmf_commonring_register_cb(struct brcmf_commonring *commonring,
  22. int (*cr_ring_bell)(void *ctx),
  23. int (*cr_update_rptr)(void *ctx),
  24. int (*cr_update_wptr)(void *ctx),
  25. int (*cr_write_rptr)(void *ctx),
  26. int (*cr_write_wptr)(void *ctx), void *ctx)
  27. {
  28. commonring->cr_ring_bell = cr_ring_bell;
  29. commonring->cr_update_rptr = cr_update_rptr;
  30. commonring->cr_update_wptr = cr_update_wptr;
  31. commonring->cr_write_rptr = cr_write_rptr;
  32. commonring->cr_write_wptr = cr_write_wptr;
  33. commonring->cr_ctx = ctx;
  34. }
  35. void brcmf_commonring_config(struct brcmf_commonring *commonring, u16 depth,
  36. u16 item_len, void *buf_addr)
  37. {
  38. commonring->depth = depth;
  39. commonring->item_len = item_len;
  40. commonring->buf_addr = buf_addr;
  41. if (!commonring->inited) {
  42. spin_lock_init(&commonring->lock);
  43. commonring->inited = true;
  44. }
  45. commonring->r_ptr = 0;
  46. if (commonring->cr_write_rptr)
  47. commonring->cr_write_rptr(commonring->cr_ctx);
  48. commonring->w_ptr = 0;
  49. if (commonring->cr_write_wptr)
  50. commonring->cr_write_wptr(commonring->cr_ctx);
  51. commonring->f_ptr = 0;
  52. }
  53. void brcmf_commonring_lock(struct brcmf_commonring *commonring)
  54. __acquires(&commonring->lock)
  55. {
  56. unsigned long flags;
  57. spin_lock_irqsave(&commonring->lock, flags);
  58. commonring->flags = flags;
  59. }
  60. void brcmf_commonring_unlock(struct brcmf_commonring *commonring)
  61. __releases(&commonring->lock)
  62. {
  63. spin_unlock_irqrestore(&commonring->lock, commonring->flags);
  64. }
  65. bool brcmf_commonring_write_available(struct brcmf_commonring *commonring)
  66. {
  67. u16 available;
  68. bool retry = true;
  69. again:
  70. if (commonring->r_ptr <= commonring->w_ptr)
  71. available = commonring->depth - commonring->w_ptr +
  72. commonring->r_ptr;
  73. else
  74. available = commonring->r_ptr - commonring->w_ptr;
  75. if (available > 1) {
  76. if (!commonring->was_full)
  77. return true;
  78. if (available > commonring->depth / 8) {
  79. commonring->was_full = false;
  80. return true;
  81. }
  82. if (retry) {
  83. if (commonring->cr_update_rptr)
  84. commonring->cr_update_rptr(commonring->cr_ctx);
  85. retry = false;
  86. goto again;
  87. }
  88. return false;
  89. }
  90. if (retry) {
  91. if (commonring->cr_update_rptr)
  92. commonring->cr_update_rptr(commonring->cr_ctx);
  93. retry = false;
  94. goto again;
  95. }
  96. commonring->was_full = true;
  97. return false;
  98. }
  99. void *brcmf_commonring_reserve_for_write(struct brcmf_commonring *commonring)
  100. {
  101. void *ret_ptr;
  102. u16 available;
  103. bool retry = true;
  104. again:
  105. if (commonring->r_ptr <= commonring->w_ptr)
  106. available = commonring->depth - commonring->w_ptr +
  107. commonring->r_ptr;
  108. else
  109. available = commonring->r_ptr - commonring->w_ptr;
  110. if (available > 1) {
  111. ret_ptr = commonring->buf_addr +
  112. (commonring->w_ptr * commonring->item_len);
  113. commonring->w_ptr++;
  114. if (commonring->w_ptr == commonring->depth)
  115. commonring->w_ptr = 0;
  116. return ret_ptr;
  117. }
  118. if (retry) {
  119. if (commonring->cr_update_rptr)
  120. commonring->cr_update_rptr(commonring->cr_ctx);
  121. retry = false;
  122. goto again;
  123. }
  124. commonring->was_full = true;
  125. return NULL;
  126. }
  127. void *
  128. brcmf_commonring_reserve_for_write_multiple(struct brcmf_commonring *commonring,
  129. u16 n_items, u16 *alloced)
  130. {
  131. void *ret_ptr;
  132. u16 available;
  133. bool retry = true;
  134. again:
  135. if (commonring->r_ptr <= commonring->w_ptr)
  136. available = commonring->depth - commonring->w_ptr +
  137. commonring->r_ptr;
  138. else
  139. available = commonring->r_ptr - commonring->w_ptr;
  140. if (available > 1) {
  141. ret_ptr = commonring->buf_addr +
  142. (commonring->w_ptr * commonring->item_len);
  143. *alloced = min_t(u16, n_items, available - 1);
  144. if (*alloced + commonring->w_ptr > commonring->depth)
  145. *alloced = commonring->depth - commonring->w_ptr;
  146. commonring->w_ptr += *alloced;
  147. if (commonring->w_ptr == commonring->depth)
  148. commonring->w_ptr = 0;
  149. return ret_ptr;
  150. }
  151. if (retry) {
  152. if (commonring->cr_update_rptr)
  153. commonring->cr_update_rptr(commonring->cr_ctx);
  154. retry = false;
  155. goto again;
  156. }
  157. commonring->was_full = true;
  158. return NULL;
  159. }
  160. int brcmf_commonring_write_complete(struct brcmf_commonring *commonring)
  161. {
  162. void *address;
  163. address = commonring->buf_addr;
  164. address += (commonring->f_ptr * commonring->item_len);
  165. if (commonring->f_ptr > commonring->w_ptr) {
  166. address = commonring->buf_addr;
  167. commonring->f_ptr = 0;
  168. }
  169. commonring->f_ptr = commonring->w_ptr;
  170. if (commonring->cr_write_wptr)
  171. commonring->cr_write_wptr(commonring->cr_ctx);
  172. if (commonring->cr_ring_bell)
  173. return commonring->cr_ring_bell(commonring->cr_ctx);
  174. return -EIO;
  175. }
  176. void brcmf_commonring_write_cancel(struct brcmf_commonring *commonring,
  177. u16 n_items)
  178. {
  179. if (commonring->w_ptr == 0)
  180. commonring->w_ptr = commonring->depth - n_items;
  181. else
  182. commonring->w_ptr -= n_items;
  183. }
  184. void *brcmf_commonring_get_read_ptr(struct brcmf_commonring *commonring,
  185. u16 *n_items)
  186. {
  187. if (commonring->cr_update_wptr)
  188. commonring->cr_update_wptr(commonring->cr_ctx);
  189. *n_items = (commonring->w_ptr >= commonring->r_ptr) ?
  190. (commonring->w_ptr - commonring->r_ptr) :
  191. (commonring->depth - commonring->r_ptr);
  192. if (*n_items == 0)
  193. return NULL;
  194. return commonring->buf_addr +
  195. (commonring->r_ptr * commonring->item_len);
  196. }
  197. int brcmf_commonring_read_complete(struct brcmf_commonring *commonring,
  198. u16 n_items)
  199. {
  200. commonring->r_ptr += n_items;
  201. if (commonring->r_ptr == commonring->depth)
  202. commonring->r_ptr = 0;
  203. if (commonring->cr_write_rptr)
  204. return commonring->cr_write_rptr(commonring->cr_ctx);
  205. return -EIO;
  206. }