cq.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. void mlx5_cq_completion(struct mlx5_core_dev *dev, u32 cqn)
  41. {
  42. struct mlx5_core_cq *cq;
  43. struct mlx5_cq_table *table = &dev->priv.cq_table;
  44. spin_lock(&table->lock);
  45. cq = radix_tree_lookup(&table->tree, cqn);
  46. if (likely(cq))
  47. atomic_inc(&cq->refcount);
  48. spin_unlock(&table->lock);
  49. if (!cq) {
  50. mlx5_core_warn(dev, "Completion event for bogus CQ 0x%x\n", cqn);
  51. return;
  52. }
  53. ++cq->arm_sn;
  54. cq->comp(cq);
  55. if (atomic_dec_and_test(&cq->refcount))
  56. complete(&cq->free);
  57. }
  58. void mlx5_cq_event(struct mlx5_core_dev *dev, u32 cqn, int event_type)
  59. {
  60. struct mlx5_cq_table *table = &dev->priv.cq_table;
  61. struct mlx5_core_cq *cq;
  62. spin_lock(&table->lock);
  63. cq = radix_tree_lookup(&table->tree, cqn);
  64. if (cq)
  65. atomic_inc(&cq->refcount);
  66. spin_unlock(&table->lock);
  67. if (!cq) {
  68. mlx5_core_warn(dev, "Async event for bogus CQ 0x%x\n", cqn);
  69. return;
  70. }
  71. cq->event(cq, event_type);
  72. if (atomic_dec_and_test(&cq->refcount))
  73. complete(&cq->free);
  74. }
  75. int mlx5_core_create_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
  76. struct mlx5_create_cq_mbox_in *in, int inlen)
  77. {
  78. int err;
  79. struct mlx5_cq_table *table = &dev->priv.cq_table;
  80. struct mlx5_create_cq_mbox_out out;
  81. struct mlx5_destroy_cq_mbox_in din;
  82. struct mlx5_destroy_cq_mbox_out dout;
  83. in->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_CREATE_CQ);
  84. memset(&out, 0, sizeof(out));
  85. err = mlx5_cmd_exec(dev, in, inlen, &out, sizeof(out));
  86. if (err)
  87. return err;
  88. if (out.hdr.status)
  89. return mlx5_cmd_status_to_err(&out.hdr);
  90. cq->cqn = be32_to_cpu(out.cqn) & 0xffffff;
  91. cq->cons_index = 0;
  92. cq->arm_sn = 0;
  93. atomic_set(&cq->refcount, 1);
  94. init_completion(&cq->free);
  95. spin_lock_irq(&table->lock);
  96. err = radix_tree_insert(&table->tree, cq->cqn, cq);
  97. spin_unlock_irq(&table->lock);
  98. if (err)
  99. goto err_cmd;
  100. cq->pid = current->pid;
  101. err = mlx5_debug_cq_add(dev, cq);
  102. if (err)
  103. mlx5_core_dbg(dev, "failed adding CP 0x%x to debug file system\n",
  104. cq->cqn);
  105. return 0;
  106. err_cmd:
  107. memset(&din, 0, sizeof(din));
  108. memset(&dout, 0, sizeof(dout));
  109. din.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_DESTROY_CQ);
  110. mlx5_cmd_exec(dev, &din, sizeof(din), &dout, sizeof(dout));
  111. return err;
  112. }
  113. EXPORT_SYMBOL(mlx5_core_create_cq);
  114. int mlx5_core_destroy_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
  115. {
  116. struct mlx5_cq_table *table = &dev->priv.cq_table;
  117. struct mlx5_destroy_cq_mbox_in in;
  118. struct mlx5_destroy_cq_mbox_out out;
  119. struct mlx5_core_cq *tmp;
  120. int err;
  121. spin_lock_irq(&table->lock);
  122. tmp = radix_tree_delete(&table->tree, cq->cqn);
  123. spin_unlock_irq(&table->lock);
  124. if (!tmp) {
  125. mlx5_core_warn(dev, "cq 0x%x not found in tree\n", cq->cqn);
  126. return -EINVAL;
  127. }
  128. if (tmp != cq) {
  129. mlx5_core_warn(dev, "corruption on srqn 0x%x\n", cq->cqn);
  130. return -EINVAL;
  131. }
  132. memset(&in, 0, sizeof(in));
  133. memset(&out, 0, sizeof(out));
  134. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_DESTROY_CQ);
  135. in.cqn = cpu_to_be32(cq->cqn);
  136. err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
  137. if (err)
  138. return err;
  139. if (out.hdr.status)
  140. return mlx5_cmd_status_to_err(&out.hdr);
  141. synchronize_irq(cq->irqn);
  142. mlx5_debug_cq_remove(dev, cq);
  143. if (atomic_dec_and_test(&cq->refcount))
  144. complete(&cq->free);
  145. wait_for_completion(&cq->free);
  146. return 0;
  147. }
  148. EXPORT_SYMBOL(mlx5_core_destroy_cq);
  149. int mlx5_core_query_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
  150. struct mlx5_query_cq_mbox_out *out)
  151. {
  152. struct mlx5_query_cq_mbox_in in;
  153. int err;
  154. memset(&in, 0, sizeof(in));
  155. memset(out, 0, sizeof(*out));
  156. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_QUERY_CQ);
  157. in.cqn = cpu_to_be32(cq->cqn);
  158. err = mlx5_cmd_exec(dev, &in, sizeof(in), out, sizeof(*out));
  159. if (err)
  160. return err;
  161. if (out->hdr.status)
  162. return mlx5_cmd_status_to_err(&out->hdr);
  163. return err;
  164. }
  165. EXPORT_SYMBOL(mlx5_core_query_cq);
  166. int mlx5_core_modify_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
  167. struct mlx5_modify_cq_mbox_in *in, int in_sz)
  168. {
  169. struct mlx5_modify_cq_mbox_out out;
  170. int err;
  171. memset(&out, 0, sizeof(out));
  172. in->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_MODIFY_CQ);
  173. err = mlx5_cmd_exec(dev, in, in_sz, &out, sizeof(out));
  174. if (err)
  175. return err;
  176. if (out.hdr.status)
  177. return mlx5_cmd_status_to_err(&out.hdr);
  178. return 0;
  179. }
  180. EXPORT_SYMBOL(mlx5_core_modify_cq);
  181. int mlx5_core_modify_cq_moderation(struct mlx5_core_dev *dev,
  182. struct mlx5_core_cq *cq,
  183. u16 cq_period,
  184. u16 cq_max_count)
  185. {
  186. struct mlx5_modify_cq_mbox_in in;
  187. memset(&in, 0, sizeof(in));
  188. in.cqn = cpu_to_be32(cq->cqn);
  189. in.ctx.cq_period = cpu_to_be16(cq_period);
  190. in.ctx.cq_max_count = cpu_to_be16(cq_max_count);
  191. in.field_select = cpu_to_be32(MLX5_CQ_MODIFY_PERIOD |
  192. MLX5_CQ_MODIFY_COUNT);
  193. return mlx5_core_modify_cq(dev, cq, &in, sizeof(in));
  194. }
  195. int mlx5_init_cq_table(struct mlx5_core_dev *dev)
  196. {
  197. struct mlx5_cq_table *table = &dev->priv.cq_table;
  198. int err;
  199. spin_lock_init(&table->lock);
  200. INIT_RADIX_TREE(&table->tree, GFP_ATOMIC);
  201. err = mlx5_cq_debugfs_init(dev);
  202. return err;
  203. }
  204. void mlx5_cleanup_cq_table(struct mlx5_core_dev *dev)
  205. {
  206. mlx5_cq_debugfs_cleanup(dev);
  207. }