main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*
  3. * Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES
  4. */
  5. #include <linux/fwctl.h>
  6. #include <linux/auxiliary_bus.h>
  7. #include <linux/mlx5/device.h>
  8. #include <linux/mlx5/driver.h>
  9. #include <uapi/fwctl/mlx5.h>
  10. #define mlx5ctl_err(mcdev, format, ...) \
  11. dev_err(&mcdev->fwctl.dev, format, ##__VA_ARGS__)
  12. #define mlx5ctl_dbg(mcdev, format, ...) \
  13. dev_dbg(&mcdev->fwctl.dev, "PID %u: " format, current->pid, \
  14. ##__VA_ARGS__)
  15. struct mlx5ctl_uctx {
  16. struct fwctl_uctx uctx;
  17. u32 uctx_caps;
  18. u32 uctx_uid;
  19. };
  20. struct mlx5ctl_dev {
  21. struct fwctl_device fwctl;
  22. struct mlx5_core_dev *mdev;
  23. };
  24. DEFINE_FREE(mlx5ctl, struct mlx5ctl_dev *, if (_T) fwctl_put(&_T->fwctl));
  25. struct mlx5_ifc_mbox_in_hdr_bits {
  26. u8 opcode[0x10];
  27. u8 uid[0x10];
  28. u8 reserved_at_20[0x10];
  29. u8 op_mod[0x10];
  30. u8 reserved_at_40[0x40];
  31. };
  32. struct mlx5_ifc_mbox_out_hdr_bits {
  33. u8 status[0x8];
  34. u8 reserved_at_8[0x18];
  35. u8 syndrome[0x20];
  36. u8 reserved_at_40[0x40];
  37. };
  38. enum {
  39. MLX5_UCTX_OBJECT_CAP_TOOLS_RESOURCES = 0x4,
  40. };
  41. enum {
  42. MLX5_CMD_OP_QUERY_DRIVER_VERSION = 0x10c,
  43. MLX5_CMD_OP_QUERY_OTHER_HCA_CAP = 0x10e,
  44. MLX5_CMD_OP_QUERY_RDB = 0x512,
  45. MLX5_CMD_OP_QUERY_PSV = 0x602,
  46. MLX5_CMD_OP_QUERY_DC_CNAK_TRACE = 0x716,
  47. MLX5_CMD_OP_QUERY_NVMF_BACKEND_CONTROLLER = 0x722,
  48. MLX5_CMD_OP_QUERY_NVMF_NAMESPACE_CONTEXT = 0x728,
  49. MLX5_CMD_OP_QUERY_ADJACENT_FUNCTIONS_ID = 0x730,
  50. MLX5_CMD_OP_DELEGATE_VHCA_MANAGEMENT = 0x731,
  51. MLX5_CMD_OP_QUERY_DELEGATED_VHCA = 0x732,
  52. MLX5_CMD_OP_QUERY_BURST_SIZE = 0x813,
  53. MLX5_CMD_OP_QUERY_DIAGNOSTIC_PARAMS = 0x819,
  54. MLX5_CMD_OP_SET_DIAGNOSTIC_PARAMS = 0x820,
  55. MLX5_CMD_OP_QUERY_DIAGNOSTIC_COUNTERS = 0x821,
  56. MLX5_CMD_OP_QUERY_DELAY_DROP_PARAMS = 0x911,
  57. MLX5_CMD_OP_QUERY_AFU = 0x971,
  58. MLX5_CMD_OP_QUERY_CAPI_PEC = 0x981,
  59. MLX5_CMD_OP_QUERY_UCTX = 0xa05,
  60. MLX5_CMD_OP_QUERY_UMEM = 0xa09,
  61. MLX5_CMD_OP_QUERY_NVMF_CC_RESPONSE = 0xb02,
  62. MLX5_CMD_OP_QUERY_EMULATED_FUNCTIONS_INFO = 0xb03,
  63. MLX5_CMD_OP_QUERY_REGEXP_PARAMS = 0xb05,
  64. MLX5_CMD_OP_QUERY_REGEXP_REGISTER = 0xb07,
  65. MLX5_CMD_OP_USER_QUERY_XRQ_DC_PARAMS_ENTRY = 0xb08,
  66. MLX5_CMD_OP_USER_QUERY_XRQ_ERROR_PARAMS = 0xb0a,
  67. MLX5_CMD_OP_ACCESS_REGISTER_USER = 0xb0c,
  68. MLX5_CMD_OP_QUERY_EMULATION_DEVICE_EQ_MSIX_MAPPING = 0xb0f,
  69. MLX5_CMD_OP_QUERY_MATCH_SAMPLE_INFO = 0xb13,
  70. MLX5_CMD_OP_QUERY_CRYPTO_STATE = 0xb14,
  71. MLX5_CMD_OP_QUERY_VUID = 0xb22,
  72. MLX5_CMD_OP_QUERY_DPA_PARTITION = 0xb28,
  73. MLX5_CMD_OP_QUERY_DPA_PARTITIONS = 0xb2a,
  74. MLX5_CMD_OP_POSTPONE_CONNECTED_QP_TIMEOUT = 0xb2e,
  75. MLX5_CMD_OP_QUERY_EMULATED_RESOURCES_INFO = 0xb2f,
  76. MLX5_CMD_OP_QUERY_RSV_RESOURCES = 0x8000,
  77. MLX5_CMD_OP_QUERY_MTT = 0x8001,
  78. MLX5_CMD_OP_QUERY_SCHED_QUEUE = 0x8006,
  79. };
  80. static int mlx5ctl_alloc_uid(struct mlx5ctl_dev *mcdev, u32 cap)
  81. {
  82. u32 out[MLX5_ST_SZ_DW(create_uctx_out)] = {};
  83. u32 in[MLX5_ST_SZ_DW(create_uctx_in)] = {};
  84. void *uctx;
  85. int ret;
  86. u16 uid;
  87. uctx = MLX5_ADDR_OF(create_uctx_in, in, uctx);
  88. mlx5ctl_dbg(mcdev, "%s: caps 0x%x\n", __func__, cap);
  89. MLX5_SET(create_uctx_in, in, opcode, MLX5_CMD_OP_CREATE_UCTX);
  90. MLX5_SET(uctx, uctx, cap, cap);
  91. ret = mlx5_cmd_exec(mcdev->mdev, in, sizeof(in), out, sizeof(out));
  92. if (ret)
  93. return ret;
  94. uid = MLX5_GET(create_uctx_out, out, uid);
  95. mlx5ctl_dbg(mcdev, "allocated uid %u with caps 0x%x\n", uid, cap);
  96. return uid;
  97. }
  98. static void mlx5ctl_release_uid(struct mlx5ctl_dev *mcdev, u16 uid)
  99. {
  100. u32 in[MLX5_ST_SZ_DW(destroy_uctx_in)] = {};
  101. struct mlx5_core_dev *mdev = mcdev->mdev;
  102. int ret;
  103. MLX5_SET(destroy_uctx_in, in, opcode, MLX5_CMD_OP_DESTROY_UCTX);
  104. MLX5_SET(destroy_uctx_in, in, uid, uid);
  105. ret = mlx5_cmd_exec_in(mdev, destroy_uctx, in);
  106. mlx5ctl_dbg(mcdev, "released uid %u %pe\n", uid, ERR_PTR(ret));
  107. }
  108. static int mlx5ctl_open_uctx(struct fwctl_uctx *uctx)
  109. {
  110. struct mlx5ctl_uctx *mfd =
  111. container_of(uctx, struct mlx5ctl_uctx, uctx);
  112. struct mlx5ctl_dev *mcdev =
  113. container_of(uctx->fwctl, struct mlx5ctl_dev, fwctl);
  114. int uid;
  115. /*
  116. * New FW supports the TOOLS_RESOURCES uid security label
  117. * which allows commands to manipulate the global device state.
  118. * Otherwise only basic existing RDMA devx privilege are allowed.
  119. */
  120. if (MLX5_CAP_GEN(mcdev->mdev, uctx_cap) &
  121. MLX5_UCTX_OBJECT_CAP_TOOLS_RESOURCES)
  122. mfd->uctx_caps |= MLX5_UCTX_OBJECT_CAP_TOOLS_RESOURCES;
  123. uid = mlx5ctl_alloc_uid(mcdev, mfd->uctx_caps);
  124. if (uid < 0)
  125. return uid;
  126. mfd->uctx_uid = uid;
  127. return 0;
  128. }
  129. static void mlx5ctl_close_uctx(struct fwctl_uctx *uctx)
  130. {
  131. struct mlx5ctl_dev *mcdev =
  132. container_of(uctx->fwctl, struct mlx5ctl_dev, fwctl);
  133. struct mlx5ctl_uctx *mfd =
  134. container_of(uctx, struct mlx5ctl_uctx, uctx);
  135. mlx5ctl_release_uid(mcdev, mfd->uctx_uid);
  136. }
  137. static void *mlx5ctl_info(struct fwctl_uctx *uctx, size_t *length)
  138. {
  139. struct mlx5ctl_uctx *mfd =
  140. container_of(uctx, struct mlx5ctl_uctx, uctx);
  141. struct fwctl_info_mlx5 *info;
  142. info = kzalloc_obj(*info);
  143. if (!info)
  144. return ERR_PTR(-ENOMEM);
  145. info->uid = mfd->uctx_uid;
  146. info->uctx_caps = mfd->uctx_caps;
  147. *length = sizeof(*info);
  148. return info;
  149. }
  150. static bool mlx5ctl_validate_rpc(const void *in, enum fwctl_rpc_scope scope)
  151. {
  152. u16 opcode = MLX5_GET(mbox_in_hdr, in, opcode);
  153. u16 op_mod = MLX5_GET(mbox_in_hdr, in, op_mod);
  154. /*
  155. * Currently the driver can't keep track of commands that allocate
  156. * objects in the FW, these commands are safe from a security
  157. * perspective but nothing will free the memory when the FD is closed.
  158. * For now permit only query commands and set commands that don't alter
  159. * objects. Also the caps for the scope have not been defined yet,
  160. * filter commands manually for now.
  161. */
  162. switch (opcode) {
  163. case MLX5_CMD_OP_MODIFY_CONG_STATUS:
  164. case MLX5_CMD_OP_POSTPONE_CONNECTED_QP_TIMEOUT:
  165. case MLX5_CMD_OP_QUERY_ADAPTER:
  166. case MLX5_CMD_OP_QUERY_ESW_FUNCTIONS:
  167. case MLX5_CMD_OP_QUERY_HCA_CAP:
  168. case MLX5_CMD_OP_QUERY_HCA_VPORT_CONTEXT:
  169. case MLX5_CMD_OP_QUERY_OTHER_HCA_CAP:
  170. case MLX5_CMD_OP_QUERY_ROCE_ADDRESS:
  171. case MLX5_CMD_OPCODE_QUERY_VUID:
  172. case MLX5_CMD_OP_DELEGATE_VHCA_MANAGEMENT:
  173. /*
  174. * FW limits SET_HCA_CAP on the tools UID to only the other function
  175. * mode which is used for function pre-configuration
  176. */
  177. case MLX5_CMD_OP_SET_HCA_CAP:
  178. return true; /* scope >= FWCTL_RPC_CONFIGURATION; */
  179. case MLX5_CMD_OP_FPGA_QUERY_QP_COUNTERS:
  180. case MLX5_CMD_OP_FPGA_QUERY_QP:
  181. case MLX5_CMD_OP_NOP:
  182. case MLX5_CMD_OP_QUERY_AFU:
  183. case MLX5_CMD_OP_QUERY_BURST_SIZE:
  184. case MLX5_CMD_OP_QUERY_CAPI_PEC:
  185. case MLX5_CMD_OP_QUERY_CONG_PARAMS:
  186. case MLX5_CMD_OP_QUERY_CONG_STATISTICS:
  187. case MLX5_CMD_OP_QUERY_CONG_STATUS:
  188. case MLX5_CMD_OP_QUERY_CQ:
  189. case MLX5_CMD_OP_QUERY_CRYPTO_STATE:
  190. case MLX5_CMD_OP_QUERY_DC_CNAK_TRACE:
  191. case MLX5_CMD_OP_QUERY_DCT:
  192. case MLX5_CMD_OP_QUERY_DELAY_DROP_PARAMS:
  193. case MLX5_CMD_OP_QUERY_DIAGNOSTIC_COUNTERS:
  194. case MLX5_CMD_OP_QUERY_DIAGNOSTIC_PARAMS:
  195. case MLX5_CMD_OP_QUERY_DPA_PARTITION:
  196. case MLX5_CMD_OP_QUERY_DPA_PARTITIONS:
  197. case MLX5_CMD_OP_QUERY_DRIVER_VERSION:
  198. case MLX5_CMD_OP_QUERY_EMULATED_FUNCTIONS_INFO:
  199. case MLX5_CMD_OP_QUERY_EMULATED_RESOURCES_INFO:
  200. case MLX5_CMD_OP_QUERY_EMULATION_DEVICE_EQ_MSIX_MAPPING:
  201. case MLX5_CMD_OP_QUERY_EQ:
  202. case MLX5_CMD_OP_QUERY_ESW_VPORT_CONTEXT:
  203. case MLX5_CMD_OP_QUERY_FLOW_COUNTER:
  204. case MLX5_CMD_OP_QUERY_FLOW_GROUP:
  205. case MLX5_CMD_OP_QUERY_FLOW_TABLE_ENTRY:
  206. case MLX5_CMD_OP_QUERY_FLOW_TABLE:
  207. case MLX5_CMD_OP_QUERY_GENERAL_OBJECT:
  208. case MLX5_CMD_OP_QUERY_HCA_VPORT_GID:
  209. case MLX5_CMD_OP_QUERY_HCA_VPORT_PKEY:
  210. case MLX5_CMD_OP_QUERY_ISSI:
  211. case MLX5_CMD_OP_QUERY_L2_TABLE_ENTRY:
  212. case MLX5_CMD_OP_QUERY_LAG:
  213. case MLX5_CMD_OP_QUERY_MAD_DEMUX:
  214. case MLX5_CMD_OP_QUERY_MATCH_SAMPLE_INFO:
  215. case MLX5_CMD_OP_QUERY_MKEY:
  216. case MLX5_CMD_OP_QUERY_MODIFY_HEADER_CONTEXT:
  217. case MLX5_CMD_OP_QUERY_MTT:
  218. case MLX5_CMD_OP_QUERY_NIC_VPORT_CONTEXT:
  219. case MLX5_CMD_OP_QUERY_NVMF_BACKEND_CONTROLLER:
  220. case MLX5_CMD_OP_QUERY_NVMF_CC_RESPONSE:
  221. case MLX5_CMD_OP_QUERY_NVMF_NAMESPACE_CONTEXT:
  222. case MLX5_CMD_OP_QUERY_PACKET_REFORMAT_CONTEXT:
  223. case MLX5_CMD_OP_QUERY_PAGES:
  224. case MLX5_CMD_OP_QUERY_PSV:
  225. case MLX5_CMD_OP_QUERY_Q_COUNTER:
  226. case MLX5_CMD_OP_QUERY_QP:
  227. case MLX5_CMD_OP_QUERY_RATE_LIMIT:
  228. case MLX5_CMD_OP_QUERY_RDB:
  229. case MLX5_CMD_OP_QUERY_REGEXP_PARAMS:
  230. case MLX5_CMD_OP_QUERY_REGEXP_REGISTER:
  231. case MLX5_CMD_OP_QUERY_RMP:
  232. case MLX5_CMD_OP_QUERY_RQ:
  233. case MLX5_CMD_OP_QUERY_RQT:
  234. case MLX5_CMD_OP_QUERY_RSV_RESOURCES:
  235. case MLX5_CMD_OP_QUERY_SCHED_QUEUE:
  236. case MLX5_CMD_OP_QUERY_SCHEDULING_ELEMENT:
  237. case MLX5_CMD_OP_QUERY_SF_PARTITION:
  238. case MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS:
  239. case MLX5_CMD_OP_QUERY_SQ:
  240. case MLX5_CMD_OP_QUERY_SRQ:
  241. case MLX5_CMD_OP_QUERY_TIR:
  242. case MLX5_CMD_OP_QUERY_TIS:
  243. case MLX5_CMD_OP_QUERY_UCTX:
  244. case MLX5_CMD_OP_QUERY_UMEM:
  245. case MLX5_CMD_OP_QUERY_VHCA_MIGRATION_STATE:
  246. case MLX5_CMD_OP_QUERY_VHCA_STATE:
  247. case MLX5_CMD_OP_QUERY_VNIC_ENV:
  248. case MLX5_CMD_OP_QUERY_VPORT_COUNTER:
  249. case MLX5_CMD_OP_QUERY_VPORT_STATE:
  250. case MLX5_CMD_OP_QUERY_WOL_ROL:
  251. case MLX5_CMD_OP_QUERY_XRC_SRQ:
  252. case MLX5_CMD_OP_QUERY_XRQ_DC_PARAMS_ENTRY:
  253. case MLX5_CMD_OP_QUERY_XRQ_ERROR_PARAMS:
  254. case MLX5_CMD_OP_QUERY_XRQ:
  255. case MLX5_CMD_OP_USER_QUERY_XRQ_DC_PARAMS_ENTRY:
  256. case MLX5_CMD_OP_USER_QUERY_XRQ_ERROR_PARAMS:
  257. case MLX5_CMD_OP_QUERY_ADJACENT_FUNCTIONS_ID:
  258. case MLX5_CMD_OP_QUERY_DELEGATED_VHCA:
  259. return scope >= FWCTL_RPC_DEBUG_READ_ONLY;
  260. case MLX5_CMD_OP_SET_DIAGNOSTIC_PARAMS:
  261. return scope >= FWCTL_RPC_DEBUG_WRITE;
  262. case MLX5_CMD_OP_ACCESS_REG:
  263. case MLX5_CMD_OP_ACCESS_REGISTER_USER:
  264. if (op_mod == 0) /* write */
  265. return true; /* scope >= FWCTL_RPC_CONFIGURATION; */
  266. return scope >= FWCTL_RPC_DEBUG_READ_ONLY;
  267. default:
  268. return false;
  269. }
  270. }
  271. static void *mlx5ctl_fw_rpc(struct fwctl_uctx *uctx, enum fwctl_rpc_scope scope,
  272. void *rpc_in, size_t in_len, size_t *out_len)
  273. {
  274. struct mlx5ctl_dev *mcdev =
  275. container_of(uctx->fwctl, struct mlx5ctl_dev, fwctl);
  276. struct mlx5ctl_uctx *mfd =
  277. container_of(uctx, struct mlx5ctl_uctx, uctx);
  278. void *rpc_out;
  279. int ret;
  280. if (in_len < MLX5_ST_SZ_BYTES(mbox_in_hdr) ||
  281. *out_len < MLX5_ST_SZ_BYTES(mbox_out_hdr))
  282. return ERR_PTR(-EMSGSIZE);
  283. mlx5ctl_dbg(mcdev, "[UID %d] cmdif: opcode 0x%x inlen %zu outlen %zu\n",
  284. mfd->uctx_uid, MLX5_GET(mbox_in_hdr, rpc_in, opcode),
  285. in_len, *out_len);
  286. if (!mlx5ctl_validate_rpc(rpc_in, scope))
  287. return ERR_PTR(-EBADMSG);
  288. /*
  289. * mlx5_cmd_do() copies the input message to its own buffer before
  290. * executing it, so we can reuse the allocation for the output.
  291. */
  292. if (*out_len <= in_len) {
  293. rpc_out = rpc_in;
  294. } else {
  295. rpc_out = kvzalloc(*out_len, GFP_KERNEL);
  296. if (!rpc_out)
  297. return ERR_PTR(-ENOMEM);
  298. }
  299. /* Enforce the user context for the command */
  300. MLX5_SET(mbox_in_hdr, rpc_in, uid, mfd->uctx_uid);
  301. ret = mlx5_cmd_do(mcdev->mdev, rpc_in, in_len, rpc_out, *out_len);
  302. mlx5ctl_dbg(mcdev,
  303. "[UID %d] cmdif: opcode 0x%x status 0x%x retval %pe\n",
  304. mfd->uctx_uid, MLX5_GET(mbox_in_hdr, rpc_in, opcode),
  305. MLX5_GET(mbox_out_hdr, rpc_out, status), ERR_PTR(ret));
  306. /*
  307. * -EREMOTEIO means execution succeeded and the out is valid,
  308. * but an error code was returned inside out. Everything else
  309. * means the RPC did not make it to the device.
  310. */
  311. if (ret && ret != -EREMOTEIO) {
  312. if (rpc_out != rpc_in)
  313. kvfree(rpc_out);
  314. return ERR_PTR(ret);
  315. }
  316. return rpc_out;
  317. }
  318. static const struct fwctl_ops mlx5ctl_ops = {
  319. .device_type = FWCTL_DEVICE_TYPE_MLX5,
  320. .uctx_size = sizeof(struct mlx5ctl_uctx),
  321. .open_uctx = mlx5ctl_open_uctx,
  322. .close_uctx = mlx5ctl_close_uctx,
  323. .info = mlx5ctl_info,
  324. .fw_rpc = mlx5ctl_fw_rpc,
  325. };
  326. static int mlx5ctl_probe(struct auxiliary_device *adev,
  327. const struct auxiliary_device_id *id)
  328. {
  329. struct mlx5_adev *madev = container_of(adev, struct mlx5_adev, adev);
  330. struct mlx5_core_dev *mdev = madev->mdev;
  331. struct mlx5ctl_dev *mcdev __free(mlx5ctl) = fwctl_alloc_device(
  332. &mdev->pdev->dev, &mlx5ctl_ops, struct mlx5ctl_dev, fwctl);
  333. int ret;
  334. if (!mcdev)
  335. return -ENOMEM;
  336. mcdev->mdev = mdev;
  337. ret = fwctl_register(&mcdev->fwctl);
  338. if (ret)
  339. return ret;
  340. auxiliary_set_drvdata(adev, no_free_ptr(mcdev));
  341. return 0;
  342. }
  343. static void mlx5ctl_remove(struct auxiliary_device *adev)
  344. {
  345. struct mlx5ctl_dev *mcdev = auxiliary_get_drvdata(adev);
  346. fwctl_unregister(&mcdev->fwctl);
  347. fwctl_put(&mcdev->fwctl);
  348. }
  349. static const struct auxiliary_device_id mlx5ctl_id_table[] = {
  350. {.name = MLX5_ADEV_NAME ".fwctl",},
  351. {}
  352. };
  353. MODULE_DEVICE_TABLE(auxiliary, mlx5ctl_id_table);
  354. static struct auxiliary_driver mlx5ctl_driver = {
  355. .name = "mlx5_fwctl",
  356. .probe = mlx5ctl_probe,
  357. .remove = mlx5ctl_remove,
  358. .id_table = mlx5ctl_id_table,
  359. };
  360. module_auxiliary_driver(mlx5ctl_driver);
  361. MODULE_IMPORT_NS("FWCTL");
  362. MODULE_DESCRIPTION("mlx5 ConnectX fwctl driver");
  363. MODULE_AUTHOR("Saeed Mahameed <saeedm@nvidia.com>");
  364. MODULE_LICENSE("Dual BSD/GPL");