cq.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/hardirq.h>
  35. #include <linux/mlx5/driver.h>
  36. #include <linux/mlx5/cmd.h>
  37. #include <rdma/ib_verbs.h>
  38. #include <linux/mlx5/cq.h>
  39. #include "mlx5_core.h"
  40. #define TASKLET_MAX_TIME 2
  41. #define TASKLET_MAX_TIME_JIFFIES msecs_to_jiffies(TASKLET_MAX_TIME)
  42. void mlx5_cq_tasklet_cb(unsigned long data)
  43. {
  44. unsigned long flags;
  45. unsigned long end = jiffies + TASKLET_MAX_TIME_JIFFIES;
  46. struct mlx5_eq_tasklet *ctx = (struct mlx5_eq_tasklet *)data;
  47. struct mlx5_core_cq *mcq;
  48. struct mlx5_core_cq *temp;
  49. spin_lock_irqsave(&ctx->lock, flags);
  50. list_splice_tail_init(&ctx->list, &ctx->process_list);
  51. spin_unlock_irqrestore(&ctx->lock, flags);
  52. list_for_each_entry_safe(mcq, temp, &ctx->process_list,
  53. tasklet_ctx.list) {
  54. list_del_init(&mcq->tasklet_ctx.list);
  55. mcq->tasklet_ctx.comp(mcq);
  56. if (atomic_dec_and_test(&mcq->refcount))
  57. complete(&mcq->free);
  58. if (time_after(jiffies, end))
  59. break;
  60. }
  61. if (!list_empty(&ctx->process_list))
  62. tasklet_schedule(&ctx->task);
  63. }
  64. static void mlx5_add_cq_to_tasklet(struct mlx5_core_cq *cq)
  65. {
  66. unsigned long flags;
  67. struct mlx5_eq_tasklet *tasklet_ctx = cq->tasklet_ctx.priv;
  68. spin_lock_irqsave(&tasklet_ctx->lock, flags);
  69. /* When migrating CQs between EQs will be implemented, please note
  70. * that you need to sync this point. It is possible that
  71. * while migrating a CQ, completions on the old EQs could
  72. * still arrive.
  73. */
  74. if (list_empty_careful(&cq->tasklet_ctx.list)) {
  75. atomic_inc(&cq->refcount);
  76. list_add_tail(&cq->tasklet_ctx.list, &tasklet_ctx->list);
  77. }
  78. spin_unlock_irqrestore(&tasklet_ctx->lock, flags);
  79. }
  80. void mlx5_cq_completion(struct mlx5_core_dev *dev, u32 cqn)
  81. {
  82. struct mlx5_core_cq *cq;
  83. struct mlx5_cq_table *table = &dev->priv.cq_table;
  84. spin_lock(&table->lock);
  85. cq = radix_tree_lookup(&table->tree, cqn);
  86. if (likely(cq))
  87. atomic_inc(&cq->refcount);
  88. spin_unlock(&table->lock);
  89. if (!cq) {
  90. mlx5_core_warn(dev, "Completion event for bogus CQ 0x%x\n", cqn);
  91. return;
  92. }
  93. ++cq->arm_sn;
  94. cq->comp(cq);
  95. if (atomic_dec_and_test(&cq->refcount))
  96. complete(&cq->free);
  97. }
  98. void mlx5_cq_event(struct mlx5_core_dev *dev, u32 cqn, int event_type)
  99. {
  100. struct mlx5_cq_table *table = &dev->priv.cq_table;
  101. struct mlx5_core_cq *cq;
  102. spin_lock(&table->lock);
  103. cq = radix_tree_lookup(&table->tree, cqn);
  104. if (cq)
  105. atomic_inc(&cq->refcount);
  106. spin_unlock(&table->lock);
  107. if (!cq) {
  108. mlx5_core_warn(dev, "Async event for bogus CQ 0x%x\n", cqn);
  109. return;
  110. }
  111. cq->event(cq, event_type);
  112. if (atomic_dec_and_test(&cq->refcount))
  113. complete(&cq->free);
  114. }
  115. int mlx5_core_create_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
  116. u32 *in, int inlen)
  117. {
  118. struct mlx5_cq_table *table = &dev->priv.cq_table;
  119. u32 out[MLX5_ST_SZ_DW(create_cq_out)];
  120. u32 din[MLX5_ST_SZ_DW(destroy_cq_in)];
  121. u32 dout[MLX5_ST_SZ_DW(destroy_cq_out)];
  122. int eqn = MLX5_GET(cqc, MLX5_ADDR_OF(create_cq_in, in, cq_context),
  123. c_eqn);
  124. struct mlx5_eq *eq;
  125. int err;
  126. eq = mlx5_eqn2eq(dev, eqn);
  127. if (IS_ERR(eq))
  128. return PTR_ERR(eq);
  129. memset(out, 0, sizeof(out));
  130. MLX5_SET(create_cq_in, in, opcode, MLX5_CMD_OP_CREATE_CQ);
  131. err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
  132. if (err)
  133. return err;
  134. cq->cqn = MLX5_GET(create_cq_out, out, cqn);
  135. cq->cons_index = 0;
  136. cq->arm_sn = 0;
  137. atomic_set(&cq->refcount, 1);
  138. init_completion(&cq->free);
  139. if (!cq->comp)
  140. cq->comp = mlx5_add_cq_to_tasklet;
  141. /* assuming CQ will be deleted before the EQ */
  142. cq->tasklet_ctx.priv = &eq->tasklet_ctx;
  143. INIT_LIST_HEAD(&cq->tasklet_ctx.list);
  144. spin_lock_irq(&table->lock);
  145. err = radix_tree_insert(&table->tree, cq->cqn, cq);
  146. spin_unlock_irq(&table->lock);
  147. if (err)
  148. goto err_cmd;
  149. cq->pid = current->pid;
  150. err = mlx5_debug_cq_add(dev, cq);
  151. if (err)
  152. mlx5_core_dbg(dev, "failed adding CP 0x%x to debug file system\n",
  153. cq->cqn);
  154. return 0;
  155. err_cmd:
  156. memset(din, 0, sizeof(din));
  157. memset(dout, 0, sizeof(dout));
  158. MLX5_SET(destroy_cq_in, din, opcode, MLX5_CMD_OP_DESTROY_CQ);
  159. MLX5_SET(destroy_cq_in, din, cqn, cq->cqn);
  160. mlx5_cmd_exec(dev, din, sizeof(din), dout, sizeof(dout));
  161. return err;
  162. }
  163. EXPORT_SYMBOL(mlx5_core_create_cq);
  164. int mlx5_core_destroy_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
  165. {
  166. struct mlx5_cq_table *table = &dev->priv.cq_table;
  167. u32 out[MLX5_ST_SZ_DW(destroy_cq_out)] = {0};
  168. u32 in[MLX5_ST_SZ_DW(destroy_cq_in)] = {0};
  169. struct mlx5_core_cq *tmp;
  170. int err;
  171. spin_lock_irq(&table->lock);
  172. tmp = radix_tree_delete(&table->tree, cq->cqn);
  173. spin_unlock_irq(&table->lock);
  174. if (!tmp) {
  175. mlx5_core_warn(dev, "cq 0x%x not found in tree\n", cq->cqn);
  176. return -EINVAL;
  177. }
  178. if (tmp != cq) {
  179. mlx5_core_warn(dev, "corruption on srqn 0x%x\n", cq->cqn);
  180. return -EINVAL;
  181. }
  182. MLX5_SET(destroy_cq_in, in, opcode, MLX5_CMD_OP_DESTROY_CQ);
  183. MLX5_SET(destroy_cq_in, in, cqn, cq->cqn);
  184. err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  185. if (err)
  186. return err;
  187. synchronize_irq(cq->irqn);
  188. mlx5_debug_cq_remove(dev, cq);
  189. if (atomic_dec_and_test(&cq->refcount))
  190. complete(&cq->free);
  191. wait_for_completion(&cq->free);
  192. return 0;
  193. }
  194. EXPORT_SYMBOL(mlx5_core_destroy_cq);
  195. int mlx5_core_query_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
  196. u32 *out, int outlen)
  197. {
  198. u32 in[MLX5_ST_SZ_DW(query_cq_in)] = {0};
  199. MLX5_SET(query_cq_in, in, opcode, MLX5_CMD_OP_QUERY_CQ);
  200. MLX5_SET(query_cq_in, in, cqn, cq->cqn);
  201. return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
  202. }
  203. EXPORT_SYMBOL(mlx5_core_query_cq);
  204. int mlx5_core_modify_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
  205. u32 *in, int inlen)
  206. {
  207. u32 out[MLX5_ST_SZ_DW(modify_cq_out)] = {0};
  208. MLX5_SET(modify_cq_in, in, opcode, MLX5_CMD_OP_MODIFY_CQ);
  209. return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
  210. }
  211. EXPORT_SYMBOL(mlx5_core_modify_cq);
  212. int mlx5_core_modify_cq_moderation(struct mlx5_core_dev *dev,
  213. struct mlx5_core_cq *cq,
  214. u16 cq_period,
  215. u16 cq_max_count)
  216. {
  217. u32 in[MLX5_ST_SZ_DW(modify_cq_in)] = {0};
  218. void *cqc;
  219. MLX5_SET(modify_cq_in, in, cqn, cq->cqn);
  220. cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
  221. MLX5_SET(cqc, cqc, cq_period, cq_period);
  222. MLX5_SET(cqc, cqc, cq_max_count, cq_max_count);
  223. MLX5_SET(modify_cq_in, in,
  224. modify_field_select_resize_field_select.modify_field_select.modify_field_select,
  225. MLX5_CQ_MODIFY_PERIOD | MLX5_CQ_MODIFY_COUNT);
  226. return mlx5_core_modify_cq(dev, cq, in, sizeof(in));
  227. }
  228. EXPORT_SYMBOL(mlx5_core_modify_cq_moderation);
  229. int mlx5_init_cq_table(struct mlx5_core_dev *dev)
  230. {
  231. struct mlx5_cq_table *table = &dev->priv.cq_table;
  232. int err;
  233. memset(table, 0, sizeof(*table));
  234. spin_lock_init(&table->lock);
  235. INIT_RADIX_TREE(&table->tree, GFP_ATOMIC);
  236. err = mlx5_cq_debugfs_init(dev);
  237. return err;
  238. }
  239. void mlx5_cleanup_cq_table(struct mlx5_core_dev *dev)
  240. {
  241. mlx5_cq_debugfs_cleanup(dev);
  242. }