cmd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_dump_fill_mkey(struct mlx5_core_dev *dev, u32 *mkey)
  34. {
  35. u32 out[MLX5_ST_SZ_DW(query_special_contexts_out)] = {0};
  36. u32 in[MLX5_ST_SZ_DW(query_special_contexts_in)] = {0};
  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. *mkey = MLX5_GET(query_special_contexts_out, out,
  43. dump_fill_mkey);
  44. return err;
  45. }
  46. int mlx5_cmd_null_mkey(struct mlx5_core_dev *dev, u32 *null_mkey)
  47. {
  48. u32 out[MLX5_ST_SZ_DW(query_special_contexts_out)] = {};
  49. u32 in[MLX5_ST_SZ_DW(query_special_contexts_in)] = {};
  50. int err;
  51. MLX5_SET(query_special_contexts_in, in, opcode,
  52. MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS);
  53. err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  54. if (!err)
  55. *null_mkey = MLX5_GET(query_special_contexts_out, out,
  56. null_mkey);
  57. return err;
  58. }
  59. int mlx5_cmd_query_cong_params(struct mlx5_core_dev *dev, int cong_point,
  60. void *out, int out_size)
  61. {
  62. u32 in[MLX5_ST_SZ_DW(query_cong_params_in)] = { };
  63. MLX5_SET(query_cong_params_in, in, opcode,
  64. MLX5_CMD_OP_QUERY_CONG_PARAMS);
  65. MLX5_SET(query_cong_params_in, in, cong_protocol, cong_point);
  66. return mlx5_cmd_exec(dev, in, sizeof(in), out, out_size);
  67. }
  68. int mlx5_cmd_modify_cong_params(struct mlx5_core_dev *dev,
  69. void *in, int in_size)
  70. {
  71. u32 out[MLX5_ST_SZ_DW(modify_cong_params_out)] = { };
  72. return mlx5_cmd_exec(dev, in, in_size, out, sizeof(out));
  73. }
  74. int mlx5_cmd_alloc_memic(struct mlx5_memic *memic, phys_addr_t *addr,
  75. u64 length, u32 alignment)
  76. {
  77. struct mlx5_core_dev *dev = memic->dev;
  78. u64 num_memic_hw_pages = MLX5_CAP_DEV_MEM(dev, memic_bar_size)
  79. >> PAGE_SHIFT;
  80. u64 hw_start_addr = MLX5_CAP64_DEV_MEM(dev, memic_bar_start_addr);
  81. u32 max_alignment = MLX5_CAP_DEV_MEM(dev, log_max_memic_addr_alignment);
  82. u32 num_pages = DIV_ROUND_UP(length, PAGE_SIZE);
  83. u32 out[MLX5_ST_SZ_DW(alloc_memic_out)] = {};
  84. u32 in[MLX5_ST_SZ_DW(alloc_memic_in)] = {};
  85. u32 mlx5_alignment;
  86. u64 page_idx = 0;
  87. int ret = 0;
  88. if (!length || (length & MLX5_MEMIC_ALLOC_SIZE_MASK))
  89. return -EINVAL;
  90. /* mlx5 device sets alignment as 64*2^driver_value
  91. * so normalizing is needed.
  92. */
  93. mlx5_alignment = (alignment < MLX5_MEMIC_BASE_ALIGN) ? 0 :
  94. alignment - MLX5_MEMIC_BASE_ALIGN;
  95. if (mlx5_alignment > max_alignment)
  96. return -EINVAL;
  97. MLX5_SET(alloc_memic_in, in, opcode, MLX5_CMD_OP_ALLOC_MEMIC);
  98. MLX5_SET(alloc_memic_in, in, range_size, num_pages * PAGE_SIZE);
  99. MLX5_SET(alloc_memic_in, in, memic_size, length);
  100. MLX5_SET(alloc_memic_in, in, log_memic_addr_alignment,
  101. mlx5_alignment);
  102. while (page_idx < num_memic_hw_pages) {
  103. spin_lock(&memic->memic_lock);
  104. page_idx = bitmap_find_next_zero_area(memic->memic_alloc_pages,
  105. num_memic_hw_pages,
  106. page_idx,
  107. num_pages, 0);
  108. if (page_idx < num_memic_hw_pages)
  109. bitmap_set(memic->memic_alloc_pages,
  110. page_idx, num_pages);
  111. spin_unlock(&memic->memic_lock);
  112. if (page_idx >= num_memic_hw_pages)
  113. break;
  114. MLX5_SET64(alloc_memic_in, in, range_start_addr,
  115. hw_start_addr + (page_idx * PAGE_SIZE));
  116. ret = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  117. if (ret) {
  118. spin_lock(&memic->memic_lock);
  119. bitmap_clear(memic->memic_alloc_pages,
  120. page_idx, num_pages);
  121. spin_unlock(&memic->memic_lock);
  122. if (ret == -EAGAIN) {
  123. page_idx++;
  124. continue;
  125. }
  126. return ret;
  127. }
  128. *addr = pci_resource_start(dev->pdev, 0) +
  129. MLX5_GET64(alloc_memic_out, out, memic_start_addr);
  130. return 0;
  131. }
  132. return -ENOMEM;
  133. }
  134. int mlx5_cmd_dealloc_memic(struct mlx5_memic *memic, u64 addr, u64 length)
  135. {
  136. struct mlx5_core_dev *dev = memic->dev;
  137. u64 hw_start_addr = MLX5_CAP64_DEV_MEM(dev, memic_bar_start_addr);
  138. u32 num_pages = DIV_ROUND_UP(length, PAGE_SIZE);
  139. u32 out[MLX5_ST_SZ_DW(dealloc_memic_out)] = {0};
  140. u32 in[MLX5_ST_SZ_DW(dealloc_memic_in)] = {0};
  141. u64 start_page_idx;
  142. int err;
  143. addr -= pci_resource_start(dev->pdev, 0);
  144. start_page_idx = (addr - hw_start_addr) >> PAGE_SHIFT;
  145. MLX5_SET(dealloc_memic_in, in, opcode, MLX5_CMD_OP_DEALLOC_MEMIC);
  146. MLX5_SET64(dealloc_memic_in, in, memic_start_addr, addr);
  147. MLX5_SET(dealloc_memic_in, in, memic_size, length);
  148. err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  149. if (!err) {
  150. spin_lock(&memic->memic_lock);
  151. bitmap_clear(memic->memic_alloc_pages,
  152. start_page_idx, num_pages);
  153. spin_unlock(&memic->memic_lock);
  154. }
  155. return err;
  156. }
  157. int mlx5_cmd_query_ext_ppcnt_counters(struct mlx5_core_dev *dev, void *out)
  158. {
  159. u32 in[MLX5_ST_SZ_DW(ppcnt_reg)] = {};
  160. int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
  161. MLX5_SET(ppcnt_reg, in, local_port, 1);
  162. MLX5_SET(ppcnt_reg, in, grp, MLX5_ETHERNET_EXTENDED_COUNTERS_GROUP);
  163. return mlx5_core_access_reg(dev, in, sz, out, sz, MLX5_REG_PPCNT,
  164. 0, 0);
  165. }
  166. void mlx5_cmd_destroy_tir(struct mlx5_core_dev *dev, u32 tirn, u16 uid)
  167. {
  168. u32 in[MLX5_ST_SZ_DW(destroy_tir_in)] = {};
  169. u32 out[MLX5_ST_SZ_DW(destroy_tir_out)] = {};
  170. MLX5_SET(destroy_tir_in, in, opcode, MLX5_CMD_OP_DESTROY_TIR);
  171. MLX5_SET(destroy_tir_in, in, tirn, tirn);
  172. MLX5_SET(destroy_tir_in, in, uid, uid);
  173. mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  174. }
  175. void mlx5_cmd_destroy_tis(struct mlx5_core_dev *dev, u32 tisn, u16 uid)
  176. {
  177. u32 in[MLX5_ST_SZ_DW(destroy_tis_in)] = {0};
  178. u32 out[MLX5_ST_SZ_DW(destroy_tis_out)] = {0};
  179. MLX5_SET(destroy_tis_in, in, opcode, MLX5_CMD_OP_DESTROY_TIS);
  180. MLX5_SET(destroy_tis_in, in, tisn, tisn);
  181. MLX5_SET(destroy_tis_in, in, uid, uid);
  182. mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  183. }
  184. void mlx5_cmd_destroy_rqt(struct mlx5_core_dev *dev, u32 rqtn, u16 uid)
  185. {
  186. u32 in[MLX5_ST_SZ_DW(destroy_rqt_in)] = {};
  187. u32 out[MLX5_ST_SZ_DW(destroy_rqt_out)] = {};
  188. MLX5_SET(destroy_rqt_in, in, opcode, MLX5_CMD_OP_DESTROY_RQT);
  189. MLX5_SET(destroy_rqt_in, in, rqtn, rqtn);
  190. MLX5_SET(destroy_rqt_in, in, uid, uid);
  191. mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  192. }
  193. int mlx5_cmd_alloc_transport_domain(struct mlx5_core_dev *dev, u32 *tdn,
  194. u16 uid)
  195. {
  196. u32 in[MLX5_ST_SZ_DW(alloc_transport_domain_in)] = {0};
  197. u32 out[MLX5_ST_SZ_DW(alloc_transport_domain_out)] = {0};
  198. int err;
  199. MLX5_SET(alloc_transport_domain_in, in, opcode,
  200. MLX5_CMD_OP_ALLOC_TRANSPORT_DOMAIN);
  201. err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  202. if (!err)
  203. *tdn = MLX5_GET(alloc_transport_domain_out, out,
  204. transport_domain);
  205. return err;
  206. }
  207. void mlx5_cmd_dealloc_transport_domain(struct mlx5_core_dev *dev, u32 tdn,
  208. u16 uid)
  209. {
  210. u32 in[MLX5_ST_SZ_DW(dealloc_transport_domain_in)] = {0};
  211. u32 out[MLX5_ST_SZ_DW(dealloc_transport_domain_out)] = {0};
  212. MLX5_SET(dealloc_transport_domain_in, in, opcode,
  213. MLX5_CMD_OP_DEALLOC_TRANSPORT_DOMAIN);
  214. MLX5_SET(dealloc_transport_domain_in, in, transport_domain, tdn);
  215. mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  216. }
  217. void mlx5_cmd_dealloc_pd(struct mlx5_core_dev *dev, u32 pdn, u16 uid)
  218. {
  219. u32 out[MLX5_ST_SZ_DW(dealloc_pd_out)] = {};
  220. u32 in[MLX5_ST_SZ_DW(dealloc_pd_in)] = {};
  221. MLX5_SET(dealloc_pd_in, in, opcode, MLX5_CMD_OP_DEALLOC_PD);
  222. MLX5_SET(dealloc_pd_in, in, pd, pdn);
  223. MLX5_SET(dealloc_pd_in, in, uid, uid);
  224. mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  225. }
  226. int mlx5_cmd_attach_mcg(struct mlx5_core_dev *dev, union ib_gid *mgid,
  227. u32 qpn, u16 uid)
  228. {
  229. u32 out[MLX5_ST_SZ_DW(attach_to_mcg_out)] = {};
  230. u32 in[MLX5_ST_SZ_DW(attach_to_mcg_in)] = {};
  231. void *gid;
  232. MLX5_SET(attach_to_mcg_in, in, opcode, MLX5_CMD_OP_ATTACH_TO_MCG);
  233. MLX5_SET(attach_to_mcg_in, in, qpn, qpn);
  234. MLX5_SET(attach_to_mcg_in, in, uid, uid);
  235. gid = MLX5_ADDR_OF(attach_to_mcg_in, in, multicast_gid);
  236. memcpy(gid, mgid, sizeof(*mgid));
  237. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  238. }
  239. int mlx5_cmd_detach_mcg(struct mlx5_core_dev *dev, union ib_gid *mgid,
  240. u32 qpn, u16 uid)
  241. {
  242. u32 out[MLX5_ST_SZ_DW(detach_from_mcg_out)] = {};
  243. u32 in[MLX5_ST_SZ_DW(detach_from_mcg_in)] = {};
  244. void *gid;
  245. MLX5_SET(detach_from_mcg_in, in, opcode, MLX5_CMD_OP_DETACH_FROM_MCG);
  246. MLX5_SET(detach_from_mcg_in, in, qpn, qpn);
  247. MLX5_SET(detach_from_mcg_in, in, uid, uid);
  248. gid = MLX5_ADDR_OF(detach_from_mcg_in, in, multicast_gid);
  249. memcpy(gid, mgid, sizeof(*mgid));
  250. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  251. }
  252. int mlx5_cmd_xrcd_alloc(struct mlx5_core_dev *dev, u32 *xrcdn, u16 uid)
  253. {
  254. u32 out[MLX5_ST_SZ_DW(alloc_xrcd_out)] = {};
  255. u32 in[MLX5_ST_SZ_DW(alloc_xrcd_in)] = {};
  256. int err;
  257. MLX5_SET(alloc_xrcd_in, in, opcode, MLX5_CMD_OP_ALLOC_XRCD);
  258. MLX5_SET(alloc_xrcd_in, in, uid, uid);
  259. err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  260. if (!err)
  261. *xrcdn = MLX5_GET(alloc_xrcd_out, out, xrcd);
  262. return err;
  263. }
  264. int mlx5_cmd_xrcd_dealloc(struct mlx5_core_dev *dev, u32 xrcdn, u16 uid)
  265. {
  266. u32 out[MLX5_ST_SZ_DW(dealloc_xrcd_out)] = {};
  267. u32 in[MLX5_ST_SZ_DW(dealloc_xrcd_in)] = {};
  268. MLX5_SET(dealloc_xrcd_in, in, opcode, MLX5_CMD_OP_DEALLOC_XRCD);
  269. MLX5_SET(dealloc_xrcd_in, in, xrcd, xrcdn);
  270. MLX5_SET(dealloc_xrcd_in, in, uid, uid);
  271. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  272. }