mr.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2013, Mellanox Technologies inc. 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/mlx5/driver.h>
  35. #include <linux/mlx5/cmd.h>
  36. #include "mlx5_core.h"
  37. void mlx5_init_mr_table(struct mlx5_core_dev *dev)
  38. {
  39. struct mlx5_mr_table *table = &dev->priv.mr_table;
  40. rwlock_init(&table->lock);
  41. INIT_RADIX_TREE(&table->tree, GFP_ATOMIC);
  42. }
  43. void mlx5_cleanup_mr_table(struct mlx5_core_dev *dev)
  44. {
  45. }
  46. int mlx5_core_create_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mr,
  47. struct mlx5_create_mkey_mbox_in *in, int inlen,
  48. mlx5_cmd_cbk_t callback, void *context,
  49. struct mlx5_create_mkey_mbox_out *out)
  50. {
  51. struct mlx5_mr_table *table = &dev->priv.mr_table;
  52. struct mlx5_create_mkey_mbox_out lout;
  53. int err;
  54. u8 key;
  55. memset(&lout, 0, sizeof(lout));
  56. spin_lock_irq(&dev->priv.mkey_lock);
  57. key = dev->priv.mkey_key++;
  58. spin_unlock_irq(&dev->priv.mkey_lock);
  59. in->seg.qpn_mkey7_0 |= cpu_to_be32(key);
  60. in->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_CREATE_MKEY);
  61. if (callback) {
  62. err = mlx5_cmd_exec_cb(dev, in, inlen, out, sizeof(*out),
  63. callback, context);
  64. return err;
  65. } else {
  66. err = mlx5_cmd_exec(dev, in, inlen, &lout, sizeof(lout));
  67. }
  68. if (err) {
  69. mlx5_core_dbg(dev, "cmd exec failed %d\n", err);
  70. return err;
  71. }
  72. if (lout.hdr.status) {
  73. mlx5_core_dbg(dev, "status %d\n", lout.hdr.status);
  74. return mlx5_cmd_status_to_err(&lout.hdr);
  75. }
  76. mr->iova = be64_to_cpu(in->seg.start_addr);
  77. mr->size = be64_to_cpu(in->seg.len);
  78. mr->key = mlx5_idx_to_mkey(be32_to_cpu(lout.mkey) & 0xffffff) | key;
  79. mr->pd = be32_to_cpu(in->seg.flags_pd) & 0xffffff;
  80. mlx5_core_dbg(dev, "out 0x%x, key 0x%x, mkey 0x%x\n",
  81. be32_to_cpu(lout.mkey), key, mr->key);
  82. /* connect to MR tree */
  83. write_lock_irq(&table->lock);
  84. err = radix_tree_insert(&table->tree, mlx5_base_mkey(mr->key), mr);
  85. write_unlock_irq(&table->lock);
  86. if (err) {
  87. mlx5_core_warn(dev, "failed radix tree insert of mr 0x%x, %d\n",
  88. mlx5_base_mkey(mr->key), err);
  89. mlx5_core_destroy_mkey(dev, mr);
  90. }
  91. return err;
  92. }
  93. EXPORT_SYMBOL(mlx5_core_create_mkey);
  94. int mlx5_core_destroy_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mr)
  95. {
  96. struct mlx5_mr_table *table = &dev->priv.mr_table;
  97. struct mlx5_destroy_mkey_mbox_in in;
  98. struct mlx5_destroy_mkey_mbox_out out;
  99. struct mlx5_core_mr *deleted_mr;
  100. unsigned long flags;
  101. int err;
  102. memset(&in, 0, sizeof(in));
  103. memset(&out, 0, sizeof(out));
  104. write_lock_irqsave(&table->lock, flags);
  105. deleted_mr = radix_tree_delete(&table->tree, mlx5_base_mkey(mr->key));
  106. write_unlock_irqrestore(&table->lock, flags);
  107. if (!deleted_mr) {
  108. mlx5_core_warn(dev, "failed radix tree delete of mr 0x%x\n",
  109. mlx5_base_mkey(mr->key));
  110. return -ENOENT;
  111. }
  112. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_DESTROY_MKEY);
  113. in.mkey = cpu_to_be32(mlx5_mkey_to_idx(mr->key));
  114. err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
  115. if (err)
  116. return err;
  117. if (out.hdr.status)
  118. return mlx5_cmd_status_to_err(&out.hdr);
  119. return err;
  120. }
  121. EXPORT_SYMBOL(mlx5_core_destroy_mkey);
  122. int mlx5_core_query_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mr,
  123. struct mlx5_query_mkey_mbox_out *out, int outlen)
  124. {
  125. struct mlx5_destroy_mkey_mbox_in in;
  126. int err;
  127. memset(&in, 0, sizeof(in));
  128. memset(out, 0, outlen);
  129. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_QUERY_MKEY);
  130. in.mkey = cpu_to_be32(mlx5_mkey_to_idx(mr->key));
  131. err = mlx5_cmd_exec(dev, &in, sizeof(in), out, outlen);
  132. if (err)
  133. return err;
  134. if (out->hdr.status)
  135. return mlx5_cmd_status_to_err(&out->hdr);
  136. return err;
  137. }
  138. EXPORT_SYMBOL(mlx5_core_query_mkey);
  139. int mlx5_core_dump_fill_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mr,
  140. u32 *mkey)
  141. {
  142. struct mlx5_query_special_ctxs_mbox_in in;
  143. struct mlx5_query_special_ctxs_mbox_out out;
  144. int err;
  145. memset(&in, 0, sizeof(in));
  146. memset(&out, 0, sizeof(out));
  147. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS);
  148. err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
  149. if (err)
  150. return err;
  151. if (out.hdr.status)
  152. return mlx5_cmd_status_to_err(&out.hdr);
  153. *mkey = be32_to_cpu(out.dump_fill_mkey);
  154. return err;
  155. }
  156. EXPORT_SYMBOL(mlx5_core_dump_fill_mkey);
  157. int mlx5_core_create_psv(struct mlx5_core_dev *dev, u32 pdn,
  158. int npsvs, u32 *sig_index)
  159. {
  160. struct mlx5_allocate_psv_in in;
  161. struct mlx5_allocate_psv_out out;
  162. int i, err;
  163. if (npsvs > MLX5_MAX_PSVS)
  164. return -EINVAL;
  165. memset(&in, 0, sizeof(in));
  166. memset(&out, 0, sizeof(out));
  167. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_CREATE_PSV);
  168. in.npsv_pd = cpu_to_be32((npsvs << 28) | pdn);
  169. err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
  170. if (err) {
  171. mlx5_core_err(dev, "cmd exec failed %d\n", err);
  172. return err;
  173. }
  174. if (out.hdr.status) {
  175. mlx5_core_err(dev, "create_psv bad status %d\n",
  176. out.hdr.status);
  177. return mlx5_cmd_status_to_err(&out.hdr);
  178. }
  179. for (i = 0; i < npsvs; i++)
  180. sig_index[i] = be32_to_cpu(out.psv_idx[i]) & 0xffffff;
  181. return err;
  182. }
  183. EXPORT_SYMBOL(mlx5_core_create_psv);
  184. int mlx5_core_destroy_psv(struct mlx5_core_dev *dev, int psv_num)
  185. {
  186. struct mlx5_destroy_psv_in in;
  187. struct mlx5_destroy_psv_out out;
  188. int err;
  189. memset(&in, 0, sizeof(in));
  190. memset(&out, 0, sizeof(out));
  191. in.psv_number = cpu_to_be32(psv_num);
  192. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_DESTROY_PSV);
  193. err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
  194. if (err) {
  195. mlx5_core_err(dev, "destroy_psv cmd exec failed %d\n", err);
  196. goto out;
  197. }
  198. if (out.hdr.status) {
  199. mlx5_core_err(dev, "destroy_psv bad status %d\n",
  200. out.hdr.status);
  201. err = mlx5_cmd_status_to_err(&out.hdr);
  202. goto out;
  203. }
  204. out:
  205. return err;
  206. }
  207. EXPORT_SYMBOL(mlx5_core_destroy_psv);