dp_tx_desc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2016-2017 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. #ifndef DP_TX_DESC_H
  19. #define DP_TX_DESC_H
  20. #include "dp_types.h"
  21. #include "dp_tx.h"
  22. #include "dp_internal.h"
  23. /**
  24. * 21 bits cookie
  25. * 3 bits ring id 0 ~ 7, mask 0x1C0000, offset 18
  26. * 8 bits page id 0 ~ 255, mask 0x03C800, offset 10
  27. * 10 bits offset id 0 ~ 1023 mask 0x0003FF, offset 0
  28. */
  29. /* ???Ring ID needed??? */
  30. #define DP_TX_DESC_ID_POOL_MASK 0x1C0000
  31. #define DP_TX_DESC_ID_POOL_OS 18
  32. #define DP_TX_DESC_ID_PAGE_MASK 0x03FC00
  33. #define DP_TX_DESC_ID_PAGE_OS 10
  34. #define DP_TX_DESC_ID_OFFSET_MASK 0x0003FF
  35. #define DP_TX_DESC_ID_OFFSET_OS 0
  36. #define TX_DESC_LOCK_CREATE(lock) qdf_spinlock_create(lock)
  37. #define TX_DESC_LOCK_DESTROY(lock) qdf_spinlock_destroy(lock)
  38. #define TX_DESC_LOCK_LOCK(lock) qdf_spin_lock(lock)
  39. #define TX_DESC_LOCK_UNLOCK(lock) qdf_spin_unlock(lock)
  40. #define MAX_POOL_BUFF_COUNT 10000
  41. QDF_STATUS dp_tx_desc_pool_alloc(struct dp_soc *soc, uint8_t pool_id,
  42. uint16_t num_elem);
  43. QDF_STATUS dp_tx_desc_pool_free(struct dp_soc *soc, uint8_t pool_id);
  44. QDF_STATUS dp_tx_ext_desc_pool_alloc(struct dp_soc *soc, uint8_t pool_id,
  45. uint16_t num_elem);
  46. QDF_STATUS dp_tx_ext_desc_pool_free(struct dp_soc *soc, uint8_t pool_id);
  47. QDF_STATUS dp_tx_tso_desc_pool_alloc(struct dp_soc *soc, uint8_t pool_id,
  48. uint16_t num_elem);
  49. void dp_tx_tso_desc_pool_free(struct dp_soc *soc, uint8_t pool_id);
  50. /**
  51. * dp_tx_desc_alloc() - Allocate a Software Tx Descriptor from given pool
  52. *
  53. * @param soc Handle to DP SoC structure
  54. * @param pool_id
  55. *
  56. * Return:
  57. */
  58. static inline struct dp_tx_desc_s *dp_tx_desc_alloc(struct dp_soc *soc,
  59. uint8_t desc_pool_id)
  60. {
  61. struct dp_tx_desc_s *tx_desc = NULL;
  62. TX_DESC_LOCK_LOCK(&soc->tx_desc[desc_pool_id].lock);
  63. tx_desc = soc->tx_desc[desc_pool_id].freelist;
  64. /* Pool is exhausted */
  65. if (!tx_desc) {
  66. TX_DESC_LOCK_UNLOCK(&soc->tx_desc[desc_pool_id].lock);
  67. return NULL;
  68. }
  69. if (soc->tx_desc[desc_pool_id].freelist) {
  70. soc->tx_desc[desc_pool_id].freelist =
  71. soc->tx_desc[desc_pool_id].freelist->next;
  72. soc->tx_desc[desc_pool_id].num_allocated++;
  73. soc->tx_desc[desc_pool_id].num_free--;
  74. }
  75. DP_STATS_INC(soc, tx.desc_in_use, 1);
  76. tx_desc->flags = DP_TX_DESC_FLAG_ALLOCATED;
  77. TX_DESC_LOCK_UNLOCK(&soc->tx_desc[desc_pool_id].lock);
  78. return tx_desc;
  79. }
  80. /**
  81. * dp_tx_desc_alloc_multiple() - Allocate batch of software Tx Descriptors
  82. * from given pool
  83. * @soc: Handle to DP SoC structure
  84. * @pool_id: pool id should pick up
  85. * @num_requested: number of required descriptor
  86. *
  87. * allocate multiple tx descriptor and make a link
  88. *
  89. * Return: h_desc first descriptor pointer
  90. */
  91. static inline struct dp_tx_desc_s *dp_tx_desc_alloc_multiple(
  92. struct dp_soc *soc, uint8_t desc_pool_id, uint8_t num_requested)
  93. {
  94. struct dp_tx_desc_s *c_desc = NULL, *h_desc = NULL;
  95. uint8_t count;
  96. TX_DESC_LOCK_LOCK(&soc->tx_desc[desc_pool_id].lock);
  97. if ((num_requested == 0) ||
  98. (soc->tx_desc[desc_pool_id].num_free < num_requested)) {
  99. TX_DESC_LOCK_UNLOCK(&soc->tx_desc[desc_pool_id].lock);
  100. QDF_TRACE(QDF_MODULE_ID_DP, QDF_TRACE_LEVEL_ERROR,
  101. "%s, No Free Desc: Available(%d) num_requested(%d)",
  102. __func__, soc->tx_desc[desc_pool_id].num_free,
  103. num_requested);
  104. return NULL;
  105. }
  106. h_desc = soc->tx_desc[desc_pool_id].freelist;
  107. /* h_desc should never be NULL since num_free > requested */
  108. qdf_assert_always(h_desc);
  109. c_desc = h_desc;
  110. for (count = 0; count < (num_requested - 1); count++) {
  111. c_desc->flags = DP_TX_DESC_FLAG_ALLOCATED;
  112. c_desc = c_desc->next;
  113. }
  114. soc->tx_desc[desc_pool_id].num_free -= count;
  115. soc->tx_desc[desc_pool_id].num_allocated += count;
  116. soc->tx_desc[desc_pool_id].freelist = c_desc->next;
  117. c_desc->next = NULL;
  118. TX_DESC_LOCK_UNLOCK(&soc->tx_desc[desc_pool_id].lock);
  119. return h_desc;
  120. }
  121. /**
  122. * dp_tx_desc_free() - Fee a tx descriptor and attach it to free list
  123. *
  124. * @soc Handle to DP SoC structure
  125. * @pool_id
  126. * @tx_desc
  127. */
  128. static inline void
  129. dp_tx_desc_free(struct dp_soc *soc, struct dp_tx_desc_s *tx_desc,
  130. uint8_t desc_pool_id)
  131. {
  132. TX_DESC_LOCK_LOCK(&soc->tx_desc[desc_pool_id].lock);
  133. tx_desc->flags = 0;
  134. tx_desc->next = soc->tx_desc[desc_pool_id].freelist;
  135. soc->tx_desc[desc_pool_id].freelist = tx_desc;
  136. DP_STATS_DEC(soc, tx.desc_in_use, 1);
  137. soc->tx_desc[desc_pool_id].num_allocated--;
  138. soc->tx_desc[desc_pool_id].num_free++;
  139. TX_DESC_LOCK_UNLOCK(&soc->tx_desc[desc_pool_id].lock);
  140. }
  141. /**
  142. * dp_tx_desc_find() - find dp tx descriptor from cokie
  143. * @soc - handle for the device sending the data
  144. * @tx_desc_id - the ID of the descriptor in question
  145. * @return the descriptor object that has the specified ID
  146. *
  147. * Use a tx descriptor ID to find the corresponding descriptor object.
  148. *
  149. */
  150. static inline struct dp_tx_desc_s *dp_tx_desc_find(struct dp_soc *soc,
  151. uint8_t pool_id, uint16_t page_id, uint16_t offset)
  152. {
  153. return soc->tx_desc[pool_id].desc_pages.cacheable_pages[page_id] +
  154. soc->tx_desc[pool_id].elem_size * offset;
  155. }
  156. /**
  157. * dp_tx_ext_desc_alloc() - Get tx extension descriptor from pool
  158. * @soc: handle for the device sending the data
  159. * @pool_id: target pool id
  160. *
  161. * Return: None
  162. */
  163. static inline
  164. struct dp_tx_ext_desc_elem_s *dp_tx_ext_desc_alloc(struct dp_soc *soc,
  165. uint8_t desc_pool_id)
  166. {
  167. struct dp_tx_ext_desc_elem_s *c_elem;
  168. TX_DESC_LOCK_LOCK(&soc->tx_ext_desc[desc_pool_id].lock);
  169. c_elem = soc->tx_ext_desc[desc_pool_id].freelist;
  170. soc->tx_ext_desc[desc_pool_id].freelist =
  171. soc->tx_ext_desc[desc_pool_id].freelist->next;
  172. TX_DESC_LOCK_UNLOCK(&soc->tx_ext_desc[desc_pool_id].lock);
  173. return c_elem;
  174. }
  175. /**
  176. * dp_tx_ext_desc_free() - Release tx extension descriptor to the pool
  177. * @soc: handle for the device sending the data
  178. * @pool_id: target pool id
  179. * @elem: ext descriptor pointer should release
  180. *
  181. * Return: None
  182. */
  183. static inline void dp_tx_ext_desc_free(struct dp_soc *soc,
  184. struct dp_tx_ext_desc_elem_s *elem, uint8_t desc_pool_id)
  185. {
  186. TX_DESC_LOCK_LOCK(&soc->tx_ext_desc[desc_pool_id].lock);
  187. elem->next = soc->tx_ext_desc[desc_pool_id].freelist;
  188. soc->tx_ext_desc[desc_pool_id].freelist = elem;
  189. TX_DESC_LOCK_UNLOCK(&soc->tx_ext_desc[desc_pool_id].lock);
  190. return;
  191. }
  192. /**
  193. * dp_tx_ext_desc_free_multiple() - Fee multiple tx extension descriptor and
  194. * attach it to free list
  195. * @soc: Handle to DP SoC structure
  196. * @desc_pool_id: pool id should pick up
  197. * @elem: tx descriptor should be freed
  198. * @num_free: number of descriptors should be freed
  199. *
  200. * Return: none
  201. */
  202. static inline void dp_tx_ext_desc_free_multiple(struct dp_soc *soc,
  203. struct dp_tx_ext_desc_elem_s *elem, uint8_t desc_pool_id,
  204. uint8_t num_free)
  205. {
  206. struct dp_tx_ext_desc_elem_s *head, *tail, *c_elem;
  207. uint8_t freed = num_free;
  208. /* caller should always guarantee atleast list of num_free nodes */
  209. qdf_assert_always(head);
  210. head = elem;
  211. c_elem = head;
  212. tail = head;
  213. while (c_elem && freed) {
  214. tail = c_elem;
  215. c_elem = c_elem->next;
  216. freed--;
  217. }
  218. /* caller should always guarantee atleast list of num_free nodes */
  219. qdf_assert_always(tail);
  220. TX_DESC_LOCK_LOCK(&soc->tx_ext_desc[desc_pool_id].lock);
  221. tail->next = soc->tx_ext_desc[desc_pool_id].freelist;
  222. soc->tx_ext_desc[desc_pool_id].freelist = head;
  223. soc->tx_ext_desc[desc_pool_id].num_free += num_free;
  224. TX_DESC_LOCK_UNLOCK(&soc->tx_ext_desc[desc_pool_id].lock);
  225. return;
  226. }
  227. /**
  228. * dp_tx_tso_desc_alloc() - function to allocate a TSO segment
  229. * @soc: device soc instance
  230. * @pool_id: pool id should pick up tso descriptor
  231. *
  232. * Allocates a TSO segment element from the free list held in
  233. * the soc
  234. *
  235. * Return: tso_seg, tso segment memory pointer
  236. */
  237. static inline struct qdf_tso_seg_elem_t *dp_tx_tso_desc_alloc(
  238. struct dp_soc *soc, uint8_t pool_id)
  239. {
  240. struct qdf_tso_seg_elem_t *tso_seg = NULL;
  241. TX_DESC_LOCK_LOCK(&soc->tx_tso_desc[pool_id].lock);
  242. if (soc->tx_tso_desc[pool_id].freelist) {
  243. soc->tx_tso_desc[pool_id].num_free--;
  244. tso_seg = soc->tx_tso_desc[pool_id].freelist;
  245. soc->tx_tso_desc[pool_id].freelist =
  246. soc->tx_tso_desc[pool_id].freelist->next;
  247. }
  248. TX_DESC_LOCK_UNLOCK(&soc->tx_tso_desc[pool_id].lock);
  249. return tso_seg;
  250. }
  251. /**
  252. * dp_tx_tso_desc_free() - function to free a TSO segment
  253. * @soc: device soc instance
  254. * @pool_id: pool id should pick up tso descriptor
  255. * @tso_seg: tso segment memory pointer
  256. *
  257. * Returns a TSO segment element to the free list held in the
  258. * HTT pdev
  259. *
  260. * Return: none
  261. */
  262. static inline void dp_tx_tso_desc_free(struct dp_soc *soc,
  263. uint8_t pool_id, struct qdf_tso_seg_elem_t *tso_seg)
  264. {
  265. TX_DESC_LOCK_LOCK(&soc->tx_tso_desc[pool_id].lock);
  266. tso_seg->next = soc->tx_tso_desc[pool_id].freelist;
  267. soc->tx_tso_desc[pool_id].freelist = tso_seg;
  268. soc->tx_tso_desc[pool_id].num_free++;
  269. TX_DESC_LOCK_UNLOCK(&soc->tx_tso_desc[pool_id].lock);
  270. }
  271. /*
  272. * dp_tx_me_alloc_buf() Alloc descriptor from me pool
  273. * @pdev DP_PDEV handle for datapath
  274. *
  275. * Return:dp_tx_me_buf_t(buf)
  276. */
  277. static inline struct dp_tx_me_buf_t*
  278. dp_tx_me_alloc_buf(struct dp_pdev *pdev)
  279. {
  280. struct dp_tx_me_buf_t *buf = NULL;
  281. qdf_spin_lock_bh(&pdev->tx_mutex);
  282. if (pdev->me_buf.freelist) {
  283. buf = pdev->me_buf.freelist;
  284. pdev->me_buf.freelist = pdev->me_buf.freelist->next;
  285. pdev->me_buf.buf_in_use++;
  286. } else {
  287. QDF_TRACE(QDF_MODULE_ID_DP, QDF_TRACE_LEVEL_ERROR,
  288. "Error allocating memory in pool");
  289. qdf_spin_unlock_bh(&pdev->tx_mutex);
  290. return NULL;
  291. }
  292. qdf_spin_unlock_bh(&pdev->tx_mutex);
  293. return buf;
  294. }
  295. /*
  296. * dp_tx_me_free_buf() - Free me descriptor and add it to pool
  297. * @pdev: DP_PDEV handle for datapath
  298. * @buf : Allocated ME BUF
  299. *
  300. * Return:void
  301. */
  302. static inline void
  303. dp_tx_me_free_buf(struct dp_pdev *pdev, struct dp_tx_me_buf_t *buf)
  304. {
  305. qdf_spin_lock_bh(&pdev->tx_mutex);
  306. buf->next = pdev->me_buf.freelist;
  307. pdev->me_buf.freelist = buf;
  308. pdev->me_buf.buf_in_use--;
  309. qdf_spin_unlock_bh(&pdev->tx_mutex);
  310. }
  311. #endif /* DP_TX_DESC_H */