cmd.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2017, 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 "cmd.h"
  33. int mlx5_cmd_null_mkey(struct mlx5_core_dev *dev, u32 *null_mkey)
  34. {
  35. u32 out[MLX5_ST_SZ_DW(query_special_contexts_out)] = {};
  36. u32 in[MLX5_ST_SZ_DW(query_special_contexts_in)] = {};
  37. int err;
  38. MLX5_SET(query_special_contexts_in, in, opcode,
  39. MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS);
  40. err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  41. if (!err)
  42. *null_mkey = MLX5_GET(query_special_contexts_out, out,
  43. null_mkey);
  44. return err;
  45. }
  46. int mlx5_cmd_query_cong_params(struct mlx5_core_dev *dev, int cong_point,
  47. void *out, int out_size)
  48. {
  49. u32 in[MLX5_ST_SZ_DW(query_cong_params_in)] = { };
  50. MLX5_SET(query_cong_params_in, in, opcode,
  51. MLX5_CMD_OP_QUERY_CONG_PARAMS);
  52. MLX5_SET(query_cong_params_in, in, cong_protocol, cong_point);
  53. return mlx5_cmd_exec(dev, in, sizeof(in), out, out_size);
  54. }
  55. int mlx5_cmd_modify_cong_params(struct mlx5_core_dev *dev,
  56. void *in, int in_size)
  57. {
  58. u32 out[MLX5_ST_SZ_DW(modify_cong_params_out)] = { };
  59. return mlx5_cmd_exec(dev, in, in_size, out, sizeof(out));
  60. }
  61. int mlx5_cmd_alloc_memic(struct mlx5_memic *memic, phys_addr_t *addr,
  62. u64 length, u32 alignment)
  63. {
  64. struct mlx5_core_dev *dev = memic->dev;
  65. u64 num_memic_hw_pages = MLX5_CAP_DEV_MEM(dev, memic_bar_size)
  66. >> PAGE_SHIFT;
  67. u64 hw_start_addr = MLX5_CAP64_DEV_MEM(dev, memic_bar_start_addr);
  68. u32 max_alignment = MLX5_CAP_DEV_MEM(dev, log_max_memic_addr_alignment);
  69. u32 num_pages = DIV_ROUND_UP(length, PAGE_SIZE);
  70. u32 out[MLX5_ST_SZ_DW(alloc_memic_out)] = {};
  71. u32 in[MLX5_ST_SZ_DW(alloc_memic_in)] = {};
  72. u32 mlx5_alignment;
  73. u64 page_idx = 0;
  74. int ret = 0;
  75. if (!length || (length & MLX5_MEMIC_ALLOC_SIZE_MASK))
  76. return -EINVAL;
  77. /* mlx5 device sets alignment as 64*2^driver_value
  78. * so normalizing is needed.
  79. */
  80. mlx5_alignment = (alignment < MLX5_MEMIC_BASE_ALIGN) ? 0 :
  81. alignment - MLX5_MEMIC_BASE_ALIGN;
  82. if (mlx5_alignment > max_alignment)
  83. return -EINVAL;
  84. MLX5_SET(alloc_memic_in, in, opcode, MLX5_CMD_OP_ALLOC_MEMIC);
  85. MLX5_SET(alloc_memic_in, in, range_size, num_pages * PAGE_SIZE);
  86. MLX5_SET(alloc_memic_in, in, memic_size, length);
  87. MLX5_SET(alloc_memic_in, in, log_memic_addr_alignment,
  88. mlx5_alignment);
  89. do {
  90. spin_lock(&memic->memic_lock);
  91. page_idx = bitmap_find_next_zero_area(memic->memic_alloc_pages,
  92. num_memic_hw_pages,
  93. page_idx,
  94. num_pages, 0);
  95. if (page_idx + num_pages <= num_memic_hw_pages)
  96. bitmap_set(memic->memic_alloc_pages,
  97. page_idx, num_pages);
  98. else
  99. ret = -ENOMEM;
  100. spin_unlock(&memic->memic_lock);
  101. if (ret)
  102. return ret;
  103. MLX5_SET64(alloc_memic_in, in, range_start_addr,
  104. hw_start_addr + (page_idx * PAGE_SIZE));
  105. ret = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  106. if (ret) {
  107. spin_lock(&memic->memic_lock);
  108. bitmap_clear(memic->memic_alloc_pages,
  109. page_idx, num_pages);
  110. spin_unlock(&memic->memic_lock);
  111. if (ret == -EAGAIN) {
  112. page_idx++;
  113. continue;
  114. }
  115. return ret;
  116. }
  117. *addr = pci_resource_start(dev->pdev, 0) +
  118. MLX5_GET64(alloc_memic_out, out, memic_start_addr);
  119. return ret;
  120. } while (page_idx < num_memic_hw_pages);
  121. return ret;
  122. }
  123. int mlx5_cmd_dealloc_memic(struct mlx5_memic *memic, u64 addr, u64 length)
  124. {
  125. struct mlx5_core_dev *dev = memic->dev;
  126. u64 hw_start_addr = MLX5_CAP64_DEV_MEM(dev, memic_bar_start_addr);
  127. u32 num_pages = DIV_ROUND_UP(length, PAGE_SIZE);
  128. u32 out[MLX5_ST_SZ_DW(dealloc_memic_out)] = {0};
  129. u32 in[MLX5_ST_SZ_DW(dealloc_memic_in)] = {0};
  130. u64 start_page_idx;
  131. int err;
  132. addr -= pci_resource_start(dev->pdev, 0);
  133. start_page_idx = (addr - hw_start_addr) >> PAGE_SHIFT;
  134. MLX5_SET(dealloc_memic_in, in, opcode, MLX5_CMD_OP_DEALLOC_MEMIC);
  135. MLX5_SET64(dealloc_memic_in, in, memic_start_addr, addr);
  136. MLX5_SET(dealloc_memic_in, in, memic_size, length);
  137. err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  138. if (!err) {
  139. spin_lock(&memic->memic_lock);
  140. bitmap_clear(memic->memic_alloc_pages,
  141. start_page_idx, num_pages);
  142. spin_unlock(&memic->memic_lock);
  143. }
  144. return err;
  145. }