mr.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/mlx5/driver.h>
  35. #include <linux/mlx5/cmd.h>
  36. #include "mlx5_core.h"
  37. void mlx5_init_mkey_table(struct mlx5_core_dev *dev)
  38. {
  39. struct mlx5_mkey_table *table = &dev->priv.mkey_table;
  40. memset(table, 0, sizeof(*table));
  41. rwlock_init(&table->lock);
  42. INIT_RADIX_TREE(&table->tree, GFP_ATOMIC);
  43. }
  44. void mlx5_cleanup_mkey_table(struct mlx5_core_dev *dev)
  45. {
  46. }
  47. int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev,
  48. struct mlx5_core_mkey *mkey,
  49. u32 *in, int inlen,
  50. u32 *out, int outlen,
  51. mlx5_cmd_cbk_t callback, void *context)
  52. {
  53. struct mlx5_mkey_table *table = &dev->priv.mkey_table;
  54. u32 lout[MLX5_ST_SZ_DW(create_mkey_out)] = {0};
  55. u32 mkey_index;
  56. void *mkc;
  57. int err;
  58. u8 key;
  59. spin_lock_irq(&dev->priv.mkey_lock);
  60. key = dev->priv.mkey_key++;
  61. spin_unlock_irq(&dev->priv.mkey_lock);
  62. mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
  63. MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY);
  64. MLX5_SET(mkc, mkc, mkey_7_0, key);
  65. if (callback)
  66. return mlx5_cmd_exec_cb(dev, in, inlen, out, outlen,
  67. callback, context);
  68. err = mlx5_cmd_exec(dev, in, inlen, lout, sizeof(lout));
  69. if (err)
  70. return err;
  71. mkey_index = MLX5_GET(create_mkey_out, lout, mkey_index);
  72. mkey->iova = MLX5_GET64(mkc, mkc, start_addr);
  73. mkey->size = MLX5_GET64(mkc, mkc, len);
  74. mkey->key = mlx5_idx_to_mkey(mkey_index) | key;
  75. mkey->pd = MLX5_GET(mkc, mkc, pd);
  76. mlx5_core_dbg(dev, "out 0x%x, key 0x%x, mkey 0x%x\n",
  77. mkey_index, key, mkey->key);
  78. /* connect to mkey tree */
  79. write_lock_irq(&table->lock);
  80. err = radix_tree_insert(&table->tree, mlx5_base_mkey(mkey->key), mkey);
  81. write_unlock_irq(&table->lock);
  82. if (err) {
  83. mlx5_core_warn(dev, "failed radix tree insert of mkey 0x%x, %d\n",
  84. mlx5_base_mkey(mkey->key), err);
  85. mlx5_core_destroy_mkey(dev, mkey);
  86. }
  87. return err;
  88. }
  89. EXPORT_SYMBOL(mlx5_core_create_mkey_cb);
  90. int mlx5_core_create_mkey(struct mlx5_core_dev *dev,
  91. struct mlx5_core_mkey *mkey,
  92. u32 *in, int inlen)
  93. {
  94. return mlx5_core_create_mkey_cb(dev, mkey, in, inlen,
  95. NULL, 0, NULL, NULL);
  96. }
  97. EXPORT_SYMBOL(mlx5_core_create_mkey);
  98. int mlx5_core_destroy_mkey(struct mlx5_core_dev *dev,
  99. struct mlx5_core_mkey *mkey)
  100. {
  101. struct mlx5_mkey_table *table = &dev->priv.mkey_table;
  102. u32 out[MLX5_ST_SZ_DW(destroy_mkey_out)] = {0};
  103. u32 in[MLX5_ST_SZ_DW(destroy_mkey_in)] = {0};
  104. struct mlx5_core_mkey *deleted_mkey;
  105. unsigned long flags;
  106. write_lock_irqsave(&table->lock, flags);
  107. deleted_mkey = radix_tree_delete(&table->tree, mlx5_base_mkey(mkey->key));
  108. write_unlock_irqrestore(&table->lock, flags);
  109. if (!deleted_mkey) {
  110. mlx5_core_warn(dev, "failed radix tree delete of mkey 0x%x\n",
  111. mlx5_base_mkey(mkey->key));
  112. return -ENOENT;
  113. }
  114. MLX5_SET(destroy_mkey_in, in, opcode, MLX5_CMD_OP_DESTROY_MKEY);
  115. MLX5_SET(destroy_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key));
  116. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  117. }
  118. EXPORT_SYMBOL(mlx5_core_destroy_mkey);
  119. int mlx5_core_query_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mkey *mkey,
  120. u32 *out, int outlen)
  121. {
  122. u32 in[MLX5_ST_SZ_DW(query_mkey_in)] = {0};
  123. memset(out, 0, outlen);
  124. MLX5_SET(query_mkey_in, in, opcode, MLX5_CMD_OP_QUERY_MKEY);
  125. MLX5_SET(query_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key));
  126. return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
  127. }
  128. EXPORT_SYMBOL(mlx5_core_query_mkey);
  129. int mlx5_core_dump_fill_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mkey *_mkey,
  130. u32 *mkey)
  131. {
  132. u32 out[MLX5_ST_SZ_DW(query_special_contexts_out)] = {0};
  133. u32 in[MLX5_ST_SZ_DW(query_special_contexts_in)] = {0};
  134. int err;
  135. MLX5_SET(query_special_contexts_in, in, opcode,
  136. MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS);
  137. err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  138. if (!err)
  139. *mkey = MLX5_GET(query_special_contexts_out, out,
  140. dump_fill_mkey);
  141. return err;
  142. }
  143. EXPORT_SYMBOL(mlx5_core_dump_fill_mkey);
  144. static inline u32 mlx5_get_psv(u32 *out, int psv_index)
  145. {
  146. switch (psv_index) {
  147. case 1: return MLX5_GET(create_psv_out, out, psv1_index);
  148. case 2: return MLX5_GET(create_psv_out, out, psv2_index);
  149. case 3: return MLX5_GET(create_psv_out, out, psv3_index);
  150. default: return MLX5_GET(create_psv_out, out, psv0_index);
  151. }
  152. }
  153. int mlx5_core_create_psv(struct mlx5_core_dev *dev, u32 pdn,
  154. int npsvs, u32 *sig_index)
  155. {
  156. u32 out[MLX5_ST_SZ_DW(create_psv_out)] = {0};
  157. u32 in[MLX5_ST_SZ_DW(create_psv_in)] = {0};
  158. int i, err;
  159. if (npsvs > MLX5_MAX_PSVS)
  160. return -EINVAL;
  161. MLX5_SET(create_psv_in, in, opcode, MLX5_CMD_OP_CREATE_PSV);
  162. MLX5_SET(create_psv_in, in, pd, pdn);
  163. MLX5_SET(create_psv_in, in, num_psv, npsvs);
  164. err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  165. if (err)
  166. return err;
  167. for (i = 0; i < npsvs; i++)
  168. sig_index[i] = mlx5_get_psv(out, i);
  169. return err;
  170. }
  171. EXPORT_SYMBOL(mlx5_core_create_psv);
  172. int mlx5_core_destroy_psv(struct mlx5_core_dev *dev, int psv_num)
  173. {
  174. u32 out[MLX5_ST_SZ_DW(destroy_psv_out)] = {0};
  175. u32 in[MLX5_ST_SZ_DW(destroy_psv_in)] = {0};
  176. MLX5_SET(destroy_psv_in, in, opcode, MLX5_CMD_OP_DESTROY_PSV);
  177. MLX5_SET(destroy_psv_in, in, psvn, psv_num);
  178. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  179. }
  180. EXPORT_SYMBOL(mlx5_core_destroy_psv);