commonring.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. /* dma flushing needs implementation for mips and arm platforms. Should
  22. * be put in util. Note, this is not real flushing. It is virtual non
  23. * cached memory. Only write buffers should have to be drained. Though
  24. * this may be different depending on platform......
  25. * SEE ALSO msgbuf.c
  26. */
  27. #define brcmf_dma_flush(addr, len)
  28. #define brcmf_dma_invalidate_cache(addr, len)
  29. void brcmf_commonring_register_cb(struct brcmf_commonring *commonring,
  30. int (*cr_ring_bell)(void *ctx),
  31. int (*cr_update_rptr)(void *ctx),
  32. int (*cr_update_wptr)(void *ctx),
  33. int (*cr_write_rptr)(void *ctx),
  34. int (*cr_write_wptr)(void *ctx), void *ctx)
  35. {
  36. commonring->cr_ring_bell = cr_ring_bell;
  37. commonring->cr_update_rptr = cr_update_rptr;
  38. commonring->cr_update_wptr = cr_update_wptr;
  39. commonring->cr_write_rptr = cr_write_rptr;
  40. commonring->cr_write_wptr = cr_write_wptr;
  41. commonring->cr_ctx = ctx;
  42. }
  43. void brcmf_commonring_config(struct brcmf_commonring *commonring, u16 depth,
  44. u16 item_len, void *buf_addr)
  45. {
  46. commonring->depth = depth;
  47. commonring->item_len = item_len;
  48. commonring->buf_addr = buf_addr;
  49. if (!commonring->inited) {
  50. spin_lock_init(&commonring->lock);
  51. commonring->inited = true;
  52. }
  53. commonring->r_ptr = 0;
  54. if (commonring->cr_write_rptr)
  55. commonring->cr_write_rptr(commonring->cr_ctx);
  56. commonring->w_ptr = 0;
  57. if (commonring->cr_write_wptr)
  58. commonring->cr_write_wptr(commonring->cr_ctx);
  59. commonring->f_ptr = 0;
  60. }
  61. void brcmf_commonring_lock(struct brcmf_commonring *commonring)
  62. __acquires(&commonring->lock)
  63. {
  64. unsigned long flags;
  65. spin_lock_irqsave(&commonring->lock, flags);
  66. commonring->flags = flags;
  67. }
  68. void brcmf_commonring_unlock(struct brcmf_commonring *commonring)
  69. __releases(&commonring->lock)
  70. {
  71. spin_unlock_irqrestore(&commonring->lock, commonring->flags);
  72. }
  73. bool brcmf_commonring_write_available(struct brcmf_commonring *commonring)
  74. {
  75. u16 available;
  76. bool retry = true;
  77. again:
  78. if (commonring->r_ptr <= commonring->w_ptr)
  79. available = commonring->depth - commonring->w_ptr +
  80. commonring->r_ptr;
  81. else
  82. available = commonring->r_ptr - commonring->w_ptr;
  83. if (available > 1) {
  84. if (!commonring->was_full)
  85. return true;
  86. if (available > commonring->depth / 8) {
  87. commonring->was_full = false;
  88. return true;
  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. return false;
  97. }
  98. if (retry) {
  99. if (commonring->cr_update_rptr)
  100. commonring->cr_update_rptr(commonring->cr_ctx);
  101. retry = false;
  102. goto again;
  103. }
  104. commonring->was_full = true;
  105. return false;
  106. }
  107. void *brcmf_commonring_reserve_for_write(struct brcmf_commonring *commonring)
  108. {
  109. void *ret_ptr;
  110. u16 available;
  111. bool retry = true;
  112. again:
  113. if (commonring->r_ptr <= commonring->w_ptr)
  114. available = commonring->depth - commonring->w_ptr +
  115. commonring->r_ptr;
  116. else
  117. available = commonring->r_ptr - commonring->w_ptr;
  118. if (available > 1) {
  119. ret_ptr = commonring->buf_addr +
  120. (commonring->w_ptr * commonring->item_len);
  121. commonring->w_ptr++;
  122. if (commonring->w_ptr == commonring->depth)
  123. commonring->w_ptr = 0;
  124. return ret_ptr;
  125. }
  126. if (retry) {
  127. if (commonring->cr_update_rptr)
  128. commonring->cr_update_rptr(commonring->cr_ctx);
  129. retry = false;
  130. goto again;
  131. }
  132. commonring->was_full = true;
  133. return NULL;
  134. }
  135. void *
  136. brcmf_commonring_reserve_for_write_multiple(struct brcmf_commonring *commonring,
  137. u16 n_items, u16 *alloced)
  138. {
  139. void *ret_ptr;
  140. u16 available;
  141. bool retry = true;
  142. again:
  143. if (commonring->r_ptr <= commonring->w_ptr)
  144. available = commonring->depth - commonring->w_ptr +
  145. commonring->r_ptr;
  146. else
  147. available = commonring->r_ptr - commonring->w_ptr;
  148. if (available > 1) {
  149. ret_ptr = commonring->buf_addr +
  150. (commonring->w_ptr * commonring->item_len);
  151. *alloced = min_t(u16, n_items, available - 1);
  152. if (*alloced + commonring->w_ptr > commonring->depth)
  153. *alloced = commonring->depth - commonring->w_ptr;
  154. commonring->w_ptr += *alloced;
  155. if (commonring->w_ptr == commonring->depth)
  156. commonring->w_ptr = 0;
  157. return ret_ptr;
  158. }
  159. if (retry) {
  160. if (commonring->cr_update_rptr)
  161. commonring->cr_update_rptr(commonring->cr_ctx);
  162. retry = false;
  163. goto again;
  164. }
  165. commonring->was_full = true;
  166. return NULL;
  167. }
  168. int brcmf_commonring_write_complete(struct brcmf_commonring *commonring)
  169. {
  170. void *address;
  171. address = commonring->buf_addr;
  172. address += (commonring->f_ptr * commonring->item_len);
  173. if (commonring->f_ptr > commonring->w_ptr) {
  174. brcmf_dma_flush(address,
  175. (commonring->depth - commonring->f_ptr) *
  176. commonring->item_len);
  177. address = commonring->buf_addr;
  178. commonring->f_ptr = 0;
  179. }
  180. brcmf_dma_flush(address, (commonring->w_ptr - commonring->f_ptr) *
  181. commonring->item_len);
  182. commonring->f_ptr = commonring->w_ptr;
  183. if (commonring->cr_write_wptr)
  184. commonring->cr_write_wptr(commonring->cr_ctx);
  185. if (commonring->cr_ring_bell)
  186. return commonring->cr_ring_bell(commonring->cr_ctx);
  187. return -EIO;
  188. }
  189. void brcmf_commonring_write_cancel(struct brcmf_commonring *commonring,
  190. u16 n_items)
  191. {
  192. if (commonring->w_ptr == 0)
  193. commonring->w_ptr = commonring->depth - n_items;
  194. else
  195. commonring->w_ptr -= n_items;
  196. }
  197. void *brcmf_commonring_get_read_ptr(struct brcmf_commonring *commonring,
  198. u16 *n_items)
  199. {
  200. void *ret_addr;
  201. if (commonring->cr_update_wptr)
  202. commonring->cr_update_wptr(commonring->cr_ctx);
  203. *n_items = (commonring->w_ptr >= commonring->r_ptr) ?
  204. (commonring->w_ptr - commonring->r_ptr) :
  205. (commonring->depth - commonring->r_ptr);
  206. if (*n_items == 0)
  207. return NULL;
  208. ret_addr = commonring->buf_addr +
  209. (commonring->r_ptr * commonring->item_len);
  210. commonring->r_ptr += *n_items;
  211. if (commonring->r_ptr == commonring->depth)
  212. commonring->r_ptr = 0;
  213. brcmf_dma_invalidate_cache(ret_addr, *n_ items * commonring->item_len);
  214. return ret_addr;
  215. }
  216. int brcmf_commonring_read_complete(struct brcmf_commonring *commonring)
  217. {
  218. if (commonring->cr_write_rptr)
  219. return commonring->cr_write_rptr(commonring->cr_ctx);
  220. return -EIO;
  221. }