client-buffers.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ISHTP Ring Buffers
  4. *
  5. * Copyright (c) 2003-2016, Intel Corporation.
  6. */
  7. #include <linux/slab.h>
  8. #include "client.h"
  9. /**
  10. * ishtp_cl_alloc_rx_ring() - Allocate RX ring buffers
  11. * @cl: client device instance
  12. *
  13. * Allocate and initialize RX ring buffers
  14. *
  15. * Return: 0 on success else -ENOMEM
  16. */
  17. int ishtp_cl_alloc_rx_ring(struct ishtp_cl *cl)
  18. {
  19. size_t len = cl->device->fw_client->props.max_msg_length;
  20. int j;
  21. struct ishtp_cl_rb *rb;
  22. int ret = 0;
  23. unsigned long flags;
  24. for (j = 0; j < cl->rx_ring_size; ++j) {
  25. rb = ishtp_io_rb_init(cl);
  26. if (!rb) {
  27. ret = -ENOMEM;
  28. goto out;
  29. }
  30. ret = ishtp_io_rb_alloc_buf(rb, len);
  31. if (ret)
  32. goto out;
  33. spin_lock_irqsave(&cl->free_list_spinlock, flags);
  34. list_add_tail(&rb->list, &cl->free_rb_list.list);
  35. spin_unlock_irqrestore(&cl->free_list_spinlock, flags);
  36. }
  37. return 0;
  38. out:
  39. dev_err(&cl->device->dev, "error in allocating Rx buffers\n");
  40. ishtp_cl_free_rx_ring(cl);
  41. return ret;
  42. }
  43. /**
  44. * ishtp_cl_alloc_tx_ring() - Allocate TX ring buffers
  45. * @cl: client device instance
  46. *
  47. * Allocate and initialize TX ring buffers
  48. *
  49. * Return: 0 on success else -ENOMEM
  50. */
  51. int ishtp_cl_alloc_tx_ring(struct ishtp_cl *cl)
  52. {
  53. size_t len = cl->device->fw_client->props.max_msg_length;
  54. int j;
  55. unsigned long flags;
  56. cl->tx_ring_free_size = 0;
  57. /* Allocate pool to free Tx bufs */
  58. for (j = 0; j < cl->tx_ring_size; ++j) {
  59. struct ishtp_cl_tx_ring *tx_buf;
  60. tx_buf = kzalloc(sizeof(struct ishtp_cl_tx_ring), GFP_KERNEL);
  61. if (!tx_buf)
  62. goto out;
  63. tx_buf->send_buf.data = kmalloc(len, GFP_KERNEL);
  64. if (!tx_buf->send_buf.data) {
  65. kfree(tx_buf);
  66. goto out;
  67. }
  68. spin_lock_irqsave(&cl->tx_free_list_spinlock, flags);
  69. list_add_tail(&tx_buf->list, &cl->tx_free_list.list);
  70. ++cl->tx_ring_free_size;
  71. spin_unlock_irqrestore(&cl->tx_free_list_spinlock, flags);
  72. }
  73. return 0;
  74. out:
  75. dev_err(&cl->device->dev, "error in allocating Tx pool\n");
  76. ishtp_cl_free_tx_ring(cl);
  77. return -ENOMEM;
  78. }
  79. /**
  80. * ishtp_cl_free_rx_ring() - Free RX ring buffers
  81. * @cl: client device instance
  82. *
  83. * Free RX ring buffers
  84. */
  85. void ishtp_cl_free_rx_ring(struct ishtp_cl *cl)
  86. {
  87. struct ishtp_cl_rb *rb;
  88. unsigned long flags;
  89. /* release allocated memory - pass over free_rb_list */
  90. spin_lock_irqsave(&cl->free_list_spinlock, flags);
  91. while (!list_empty(&cl->free_rb_list.list)) {
  92. rb = list_entry(cl->free_rb_list.list.next, struct ishtp_cl_rb,
  93. list);
  94. list_del(&rb->list);
  95. kfree(rb->buffer.data);
  96. kfree(rb);
  97. }
  98. spin_unlock_irqrestore(&cl->free_list_spinlock, flags);
  99. /* release allocated memory - pass over in_process_list */
  100. spin_lock_irqsave(&cl->in_process_spinlock, flags);
  101. while (!list_empty(&cl->in_process_list.list)) {
  102. rb = list_entry(cl->in_process_list.list.next,
  103. struct ishtp_cl_rb, list);
  104. list_del(&rb->list);
  105. kfree(rb->buffer.data);
  106. kfree(rb);
  107. }
  108. spin_unlock_irqrestore(&cl->in_process_spinlock, flags);
  109. }
  110. /**
  111. * ishtp_cl_free_tx_ring() - Free TX ring buffers
  112. * @cl: client device instance
  113. *
  114. * Free TX ring buffers
  115. */
  116. void ishtp_cl_free_tx_ring(struct ishtp_cl *cl)
  117. {
  118. struct ishtp_cl_tx_ring *tx_buf;
  119. unsigned long flags;
  120. spin_lock_irqsave(&cl->tx_free_list_spinlock, flags);
  121. /* release allocated memory - pass over tx_free_list */
  122. while (!list_empty(&cl->tx_free_list.list)) {
  123. tx_buf = list_entry(cl->tx_free_list.list.next,
  124. struct ishtp_cl_tx_ring, list);
  125. list_del(&tx_buf->list);
  126. --cl->tx_ring_free_size;
  127. kfree(tx_buf->send_buf.data);
  128. kfree(tx_buf);
  129. }
  130. spin_unlock_irqrestore(&cl->tx_free_list_spinlock, flags);
  131. spin_lock_irqsave(&cl->tx_list_spinlock, flags);
  132. /* release allocated memory - pass over tx_list */
  133. while (!list_empty(&cl->tx_list.list)) {
  134. tx_buf = list_entry(cl->tx_list.list.next,
  135. struct ishtp_cl_tx_ring, list);
  136. list_del(&tx_buf->list);
  137. kfree(tx_buf->send_buf.data);
  138. kfree(tx_buf);
  139. }
  140. spin_unlock_irqrestore(&cl->tx_list_spinlock, flags);
  141. }
  142. /**
  143. * ishtp_io_rb_free() - Free IO request block
  144. * @rb: IO request block
  145. *
  146. * Free io request block memory
  147. */
  148. void ishtp_io_rb_free(struct ishtp_cl_rb *rb)
  149. {
  150. if (rb == NULL)
  151. return;
  152. kfree(rb->buffer.data);
  153. kfree(rb);
  154. }
  155. /**
  156. * ishtp_io_rb_init() - Allocate and init IO request block
  157. * @cl: client device instance
  158. *
  159. * Allocate and initialize request block
  160. *
  161. * Return: Allocted IO request block pointer
  162. */
  163. struct ishtp_cl_rb *ishtp_io_rb_init(struct ishtp_cl *cl)
  164. {
  165. struct ishtp_cl_rb *rb;
  166. rb = kzalloc(sizeof(struct ishtp_cl_rb), GFP_KERNEL);
  167. if (!rb)
  168. return NULL;
  169. INIT_LIST_HEAD(&rb->list);
  170. rb->cl = cl;
  171. rb->buf_idx = 0;
  172. return rb;
  173. }
  174. /**
  175. * ishtp_io_rb_alloc_buf() - Allocate and init response buffer
  176. * @rb: IO request block
  177. * @length: length of response buffer
  178. *
  179. * Allocate respose buffer
  180. *
  181. * Return: 0 on success else -ENOMEM
  182. */
  183. int ishtp_io_rb_alloc_buf(struct ishtp_cl_rb *rb, size_t length)
  184. {
  185. if (!rb)
  186. return -EINVAL;
  187. if (length == 0)
  188. return 0;
  189. rb->buffer.data = kmalloc(length, GFP_KERNEL);
  190. if (!rb->buffer.data)
  191. return -ENOMEM;
  192. rb->buffer.size = length;
  193. return 0;
  194. }
  195. /**
  196. * ishtp_cl_io_rb_recycle() - Recycle IO request blocks
  197. * @rb: IO request block
  198. *
  199. * Re-append rb to its client's free list and send flow control if needed
  200. *
  201. * Return: 0 on success else -EFAULT
  202. */
  203. int ishtp_cl_io_rb_recycle(struct ishtp_cl_rb *rb)
  204. {
  205. struct ishtp_cl *cl;
  206. int rets = 0;
  207. unsigned long flags;
  208. if (!rb || !rb->cl)
  209. return -EFAULT;
  210. cl = rb->cl;
  211. spin_lock_irqsave(&cl->free_list_spinlock, flags);
  212. list_add_tail(&rb->list, &cl->free_rb_list.list);
  213. spin_unlock_irqrestore(&cl->free_list_spinlock, flags);
  214. /*
  215. * If we returned the first buffer to empty 'free' list,
  216. * send flow control
  217. */
  218. if (!cl->out_flow_ctrl_creds)
  219. rets = ishtp_cl_read_start(cl);
  220. return rets;
  221. }
  222. EXPORT_SYMBOL(ishtp_cl_io_rb_recycle);
  223. /**
  224. * ishtp_cl_tx_empty() -test whether client device tx buffer is empty
  225. * @cl: Pointer to client device instance
  226. *
  227. * Look client device tx buffer list, and check whether this list is empty
  228. *
  229. * Return: true if client tx buffer list is empty else false
  230. */
  231. bool ishtp_cl_tx_empty(struct ishtp_cl *cl)
  232. {
  233. int tx_list_empty;
  234. unsigned long tx_flags;
  235. spin_lock_irqsave(&cl->tx_list_spinlock, tx_flags);
  236. tx_list_empty = list_empty(&cl->tx_list.list);
  237. spin_unlock_irqrestore(&cl->tx_list_spinlock, tx_flags);
  238. return !!tx_list_empty;
  239. }
  240. EXPORT_SYMBOL(ishtp_cl_tx_empty);
  241. /**
  242. * ishtp_cl_rx_get_rb() -Get a rb from client device rx buffer list
  243. * @cl: Pointer to client device instance
  244. *
  245. * Check client device in-processing buffer list and get a rb from it.
  246. *
  247. * Return: rb pointer if buffer list isn't empty else NULL
  248. */
  249. struct ishtp_cl_rb *ishtp_cl_rx_get_rb(struct ishtp_cl *cl)
  250. {
  251. unsigned long rx_flags;
  252. struct ishtp_cl_rb *rb;
  253. spin_lock_irqsave(&cl->in_process_spinlock, rx_flags);
  254. rb = list_first_entry_or_null(&cl->in_process_list.list,
  255. struct ishtp_cl_rb, list);
  256. if (rb)
  257. list_del_init(&rb->list);
  258. spin_unlock_irqrestore(&cl->in_process_spinlock, rx_flags);
  259. return rb;
  260. }
  261. EXPORT_SYMBOL(ishtp_cl_rx_get_rb);