cong.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (c) 2013-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 <linux/debugfs.h>
  33. #include "mlx5_ib.h"
  34. #include "cmd.h"
  35. enum mlx5_ib_cong_node_type {
  36. MLX5_IB_RROCE_ECN_RP = 1,
  37. MLX5_IB_RROCE_ECN_NP = 2,
  38. };
  39. static const char * const mlx5_ib_dbg_cc_name[] = {
  40. "rp_clamp_tgt_rate",
  41. "rp_clamp_tgt_rate_ati",
  42. "rp_time_reset",
  43. "rp_byte_reset",
  44. "rp_threshold",
  45. "rp_ai_rate",
  46. "rp_hai_rate",
  47. "rp_min_dec_fac",
  48. "rp_min_rate",
  49. "rp_rate_to_set_on_first_cnp",
  50. "rp_dce_tcp_g",
  51. "rp_dce_tcp_rtt",
  52. "rp_rate_reduce_monitor_period",
  53. "rp_initial_alpha_value",
  54. "rp_gd",
  55. "np_cnp_dscp",
  56. "np_cnp_prio_mode",
  57. "np_cnp_prio",
  58. };
  59. #define MLX5_IB_RP_CLAMP_TGT_RATE_ATTR BIT(1)
  60. #define MLX5_IB_RP_CLAMP_TGT_RATE_ATI_ATTR BIT(2)
  61. #define MLX5_IB_RP_TIME_RESET_ATTR BIT(3)
  62. #define MLX5_IB_RP_BYTE_RESET_ATTR BIT(4)
  63. #define MLX5_IB_RP_THRESHOLD_ATTR BIT(5)
  64. #define MLX5_IB_RP_AI_RATE_ATTR BIT(7)
  65. #define MLX5_IB_RP_HAI_RATE_ATTR BIT(8)
  66. #define MLX5_IB_RP_MIN_DEC_FAC_ATTR BIT(9)
  67. #define MLX5_IB_RP_MIN_RATE_ATTR BIT(10)
  68. #define MLX5_IB_RP_RATE_TO_SET_ON_FIRST_CNP_ATTR BIT(11)
  69. #define MLX5_IB_RP_DCE_TCP_G_ATTR BIT(12)
  70. #define MLX5_IB_RP_DCE_TCP_RTT_ATTR BIT(13)
  71. #define MLX5_IB_RP_RATE_REDUCE_MONITOR_PERIOD_ATTR BIT(14)
  72. #define MLX5_IB_RP_INITIAL_ALPHA_VALUE_ATTR BIT(15)
  73. #define MLX5_IB_RP_GD_ATTR BIT(16)
  74. #define MLX5_IB_NP_CNP_DSCP_ATTR BIT(3)
  75. #define MLX5_IB_NP_CNP_PRIO_MODE_ATTR BIT(4)
  76. static enum mlx5_ib_cong_node_type
  77. mlx5_ib_param_to_node(enum mlx5_ib_dbg_cc_types param_offset)
  78. {
  79. if (param_offset >= MLX5_IB_DBG_CC_RP_CLAMP_TGT_RATE &&
  80. param_offset <= MLX5_IB_DBG_CC_RP_GD)
  81. return MLX5_IB_RROCE_ECN_RP;
  82. else
  83. return MLX5_IB_RROCE_ECN_NP;
  84. }
  85. static u32 mlx5_get_cc_param_val(void *field, int offset)
  86. {
  87. switch (offset) {
  88. case MLX5_IB_DBG_CC_RP_CLAMP_TGT_RATE:
  89. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  90. clamp_tgt_rate);
  91. case MLX5_IB_DBG_CC_RP_CLAMP_TGT_RATE_ATI:
  92. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  93. clamp_tgt_rate_after_time_inc);
  94. case MLX5_IB_DBG_CC_RP_TIME_RESET:
  95. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  96. rpg_time_reset);
  97. case MLX5_IB_DBG_CC_RP_BYTE_RESET:
  98. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  99. rpg_byte_reset);
  100. case MLX5_IB_DBG_CC_RP_THRESHOLD:
  101. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  102. rpg_threshold);
  103. case MLX5_IB_DBG_CC_RP_AI_RATE:
  104. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  105. rpg_ai_rate);
  106. case MLX5_IB_DBG_CC_RP_HAI_RATE:
  107. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  108. rpg_hai_rate);
  109. case MLX5_IB_DBG_CC_RP_MIN_DEC_FAC:
  110. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  111. rpg_min_dec_fac);
  112. case MLX5_IB_DBG_CC_RP_MIN_RATE:
  113. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  114. rpg_min_rate);
  115. case MLX5_IB_DBG_CC_RP_RATE_TO_SET_ON_FIRST_CNP:
  116. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  117. rate_to_set_on_first_cnp);
  118. case MLX5_IB_DBG_CC_RP_DCE_TCP_G:
  119. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  120. dce_tcp_g);
  121. case MLX5_IB_DBG_CC_RP_DCE_TCP_RTT:
  122. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  123. dce_tcp_rtt);
  124. case MLX5_IB_DBG_CC_RP_RATE_REDUCE_MONITOR_PERIOD:
  125. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  126. rate_reduce_monitor_period);
  127. case MLX5_IB_DBG_CC_RP_INITIAL_ALPHA_VALUE:
  128. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  129. initial_alpha_value);
  130. case MLX5_IB_DBG_CC_RP_GD:
  131. return MLX5_GET(cong_control_r_roce_ecn_rp, field,
  132. rpg_gd);
  133. case MLX5_IB_DBG_CC_NP_CNP_DSCP:
  134. return MLX5_GET(cong_control_r_roce_ecn_np, field,
  135. cnp_dscp);
  136. case MLX5_IB_DBG_CC_NP_CNP_PRIO_MODE:
  137. return MLX5_GET(cong_control_r_roce_ecn_np, field,
  138. cnp_prio_mode);
  139. case MLX5_IB_DBG_CC_NP_CNP_PRIO:
  140. return MLX5_GET(cong_control_r_roce_ecn_np, field,
  141. cnp_802p_prio);
  142. default:
  143. return 0;
  144. }
  145. }
  146. static void mlx5_ib_set_cc_param_mask_val(void *field, int offset,
  147. u32 var, u32 *attr_mask)
  148. {
  149. switch (offset) {
  150. case MLX5_IB_DBG_CC_RP_CLAMP_TGT_RATE:
  151. *attr_mask |= MLX5_IB_RP_CLAMP_TGT_RATE_ATTR;
  152. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  153. clamp_tgt_rate, var);
  154. break;
  155. case MLX5_IB_DBG_CC_RP_CLAMP_TGT_RATE_ATI:
  156. *attr_mask |= MLX5_IB_RP_CLAMP_TGT_RATE_ATI_ATTR;
  157. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  158. clamp_tgt_rate_after_time_inc, var);
  159. break;
  160. case MLX5_IB_DBG_CC_RP_TIME_RESET:
  161. *attr_mask |= MLX5_IB_RP_TIME_RESET_ATTR;
  162. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  163. rpg_time_reset, var);
  164. break;
  165. case MLX5_IB_DBG_CC_RP_BYTE_RESET:
  166. *attr_mask |= MLX5_IB_RP_BYTE_RESET_ATTR;
  167. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  168. rpg_byte_reset, var);
  169. break;
  170. case MLX5_IB_DBG_CC_RP_THRESHOLD:
  171. *attr_mask |= MLX5_IB_RP_THRESHOLD_ATTR;
  172. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  173. rpg_threshold, var);
  174. break;
  175. case MLX5_IB_DBG_CC_RP_AI_RATE:
  176. *attr_mask |= MLX5_IB_RP_AI_RATE_ATTR;
  177. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  178. rpg_ai_rate, var);
  179. break;
  180. case MLX5_IB_DBG_CC_RP_HAI_RATE:
  181. *attr_mask |= MLX5_IB_RP_HAI_RATE_ATTR;
  182. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  183. rpg_hai_rate, var);
  184. break;
  185. case MLX5_IB_DBG_CC_RP_MIN_DEC_FAC:
  186. *attr_mask |= MLX5_IB_RP_MIN_DEC_FAC_ATTR;
  187. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  188. rpg_min_dec_fac, var);
  189. break;
  190. case MLX5_IB_DBG_CC_RP_MIN_RATE:
  191. *attr_mask |= MLX5_IB_RP_MIN_RATE_ATTR;
  192. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  193. rpg_min_rate, var);
  194. break;
  195. case MLX5_IB_DBG_CC_RP_RATE_TO_SET_ON_FIRST_CNP:
  196. *attr_mask |= MLX5_IB_RP_RATE_TO_SET_ON_FIRST_CNP_ATTR;
  197. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  198. rate_to_set_on_first_cnp, var);
  199. break;
  200. case MLX5_IB_DBG_CC_RP_DCE_TCP_G:
  201. *attr_mask |= MLX5_IB_RP_DCE_TCP_G_ATTR;
  202. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  203. dce_tcp_g, var);
  204. break;
  205. case MLX5_IB_DBG_CC_RP_DCE_TCP_RTT:
  206. *attr_mask |= MLX5_IB_RP_DCE_TCP_RTT_ATTR;
  207. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  208. dce_tcp_rtt, var);
  209. break;
  210. case MLX5_IB_DBG_CC_RP_RATE_REDUCE_MONITOR_PERIOD:
  211. *attr_mask |= MLX5_IB_RP_RATE_REDUCE_MONITOR_PERIOD_ATTR;
  212. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  213. rate_reduce_monitor_period, var);
  214. break;
  215. case MLX5_IB_DBG_CC_RP_INITIAL_ALPHA_VALUE:
  216. *attr_mask |= MLX5_IB_RP_INITIAL_ALPHA_VALUE_ATTR;
  217. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  218. initial_alpha_value, var);
  219. break;
  220. case MLX5_IB_DBG_CC_RP_GD:
  221. *attr_mask |= MLX5_IB_RP_GD_ATTR;
  222. MLX5_SET(cong_control_r_roce_ecn_rp, field,
  223. rpg_gd, var);
  224. break;
  225. case MLX5_IB_DBG_CC_NP_CNP_DSCP:
  226. *attr_mask |= MLX5_IB_NP_CNP_DSCP_ATTR;
  227. MLX5_SET(cong_control_r_roce_ecn_np, field, cnp_dscp, var);
  228. break;
  229. case MLX5_IB_DBG_CC_NP_CNP_PRIO_MODE:
  230. *attr_mask |= MLX5_IB_NP_CNP_PRIO_MODE_ATTR;
  231. MLX5_SET(cong_control_r_roce_ecn_np, field, cnp_prio_mode, var);
  232. break;
  233. case MLX5_IB_DBG_CC_NP_CNP_PRIO:
  234. *attr_mask |= MLX5_IB_NP_CNP_PRIO_MODE_ATTR;
  235. MLX5_SET(cong_control_r_roce_ecn_np, field, cnp_prio_mode, 0);
  236. MLX5_SET(cong_control_r_roce_ecn_np, field, cnp_802p_prio, var);
  237. break;
  238. }
  239. }
  240. static int mlx5_ib_get_cc_params(struct mlx5_ib_dev *dev, u8 port_num,
  241. int offset, u32 *var)
  242. {
  243. int outlen = MLX5_ST_SZ_BYTES(query_cong_params_out);
  244. void *out;
  245. void *field;
  246. int err;
  247. enum mlx5_ib_cong_node_type node;
  248. struct mlx5_core_dev *mdev;
  249. /* Takes a 1-based port number */
  250. mdev = mlx5_ib_get_native_port_mdev(dev, port_num + 1, NULL);
  251. if (!mdev)
  252. return -ENODEV;
  253. out = kvzalloc(outlen, GFP_KERNEL);
  254. if (!out) {
  255. err = -ENOMEM;
  256. goto alloc_err;
  257. }
  258. node = mlx5_ib_param_to_node(offset);
  259. err = mlx5_cmd_query_cong_params(mdev, node, out, outlen);
  260. if (err)
  261. goto free;
  262. field = MLX5_ADDR_OF(query_cong_params_out, out, congestion_parameters);
  263. *var = mlx5_get_cc_param_val(field, offset);
  264. free:
  265. kvfree(out);
  266. alloc_err:
  267. mlx5_ib_put_native_port_mdev(dev, port_num + 1);
  268. return err;
  269. }
  270. static int mlx5_ib_set_cc_params(struct mlx5_ib_dev *dev, u8 port_num,
  271. int offset, u32 var)
  272. {
  273. int inlen = MLX5_ST_SZ_BYTES(modify_cong_params_in);
  274. void *in;
  275. void *field;
  276. enum mlx5_ib_cong_node_type node;
  277. struct mlx5_core_dev *mdev;
  278. u32 attr_mask = 0;
  279. int err;
  280. /* Takes a 1-based port number */
  281. mdev = mlx5_ib_get_native_port_mdev(dev, port_num + 1, NULL);
  282. if (!mdev)
  283. return -ENODEV;
  284. in = kvzalloc(inlen, GFP_KERNEL);
  285. if (!in) {
  286. err = -ENOMEM;
  287. goto alloc_err;
  288. }
  289. MLX5_SET(modify_cong_params_in, in, opcode,
  290. MLX5_CMD_OP_MODIFY_CONG_PARAMS);
  291. node = mlx5_ib_param_to_node(offset);
  292. MLX5_SET(modify_cong_params_in, in, cong_protocol, node);
  293. field = MLX5_ADDR_OF(modify_cong_params_in, in, congestion_parameters);
  294. mlx5_ib_set_cc_param_mask_val(field, offset, var, &attr_mask);
  295. field = MLX5_ADDR_OF(modify_cong_params_in, in, field_select);
  296. MLX5_SET(field_select_r_roce_rp, field, field_select_r_roce_rp,
  297. attr_mask);
  298. err = mlx5_cmd_modify_cong_params(mdev, in, inlen);
  299. kvfree(in);
  300. alloc_err:
  301. mlx5_ib_put_native_port_mdev(dev, port_num + 1);
  302. return err;
  303. }
  304. static ssize_t set_param(struct file *filp, const char __user *buf,
  305. size_t count, loff_t *pos)
  306. {
  307. struct mlx5_ib_dbg_param *param = filp->private_data;
  308. int offset = param->offset;
  309. char lbuf[11] = { };
  310. u32 var;
  311. int ret;
  312. if (count > sizeof(lbuf))
  313. return -EINVAL;
  314. if (copy_from_user(lbuf, buf, count))
  315. return -EFAULT;
  316. lbuf[sizeof(lbuf) - 1] = '\0';
  317. if (kstrtou32(lbuf, 0, &var))
  318. return -EINVAL;
  319. ret = mlx5_ib_set_cc_params(param->dev, param->port_num, offset, var);
  320. return ret ? ret : count;
  321. }
  322. static ssize_t get_param(struct file *filp, char __user *buf, size_t count,
  323. loff_t *pos)
  324. {
  325. struct mlx5_ib_dbg_param *param = filp->private_data;
  326. int offset = param->offset;
  327. u32 var = 0;
  328. int ret;
  329. char lbuf[11];
  330. ret = mlx5_ib_get_cc_params(param->dev, param->port_num, offset, &var);
  331. if (ret)
  332. return ret;
  333. ret = snprintf(lbuf, sizeof(lbuf), "%d\n", var);
  334. if (ret < 0)
  335. return ret;
  336. return simple_read_from_buffer(buf, count, pos, lbuf, ret);
  337. }
  338. static const struct file_operations dbg_cc_fops = {
  339. .owner = THIS_MODULE,
  340. .open = simple_open,
  341. .write = set_param,
  342. .read = get_param,
  343. };
  344. void mlx5_ib_cleanup_cong_debugfs(struct mlx5_ib_dev *dev, u8 port_num)
  345. {
  346. if (!mlx5_debugfs_root ||
  347. !dev->port[port_num].dbg_cc_params ||
  348. !dev->port[port_num].dbg_cc_params->root)
  349. return;
  350. debugfs_remove_recursive(dev->port[port_num].dbg_cc_params->root);
  351. kfree(dev->port[port_num].dbg_cc_params);
  352. dev->port[port_num].dbg_cc_params = NULL;
  353. }
  354. int mlx5_ib_init_cong_debugfs(struct mlx5_ib_dev *dev, u8 port_num)
  355. {
  356. struct mlx5_ib_dbg_cc_params *dbg_cc_params;
  357. struct mlx5_core_dev *mdev;
  358. int i;
  359. if (!mlx5_debugfs_root)
  360. goto out;
  361. /* Takes a 1-based port number */
  362. mdev = mlx5_ib_get_native_port_mdev(dev, port_num + 1, NULL);
  363. if (!mdev)
  364. goto out;
  365. if (!MLX5_CAP_GEN(mdev, cc_query_allowed) ||
  366. !MLX5_CAP_GEN(mdev, cc_modify_allowed))
  367. goto put_mdev;
  368. dbg_cc_params = kzalloc(sizeof(*dbg_cc_params), GFP_KERNEL);
  369. if (!dbg_cc_params)
  370. goto err;
  371. dev->port[port_num].dbg_cc_params = dbg_cc_params;
  372. dbg_cc_params->root = debugfs_create_dir("cc_params",
  373. mdev->priv.dbg_root);
  374. if (!dbg_cc_params->root)
  375. goto err;
  376. for (i = 0; i < MLX5_IB_DBG_CC_MAX; i++) {
  377. dbg_cc_params->params[i].offset = i;
  378. dbg_cc_params->params[i].dev = dev;
  379. dbg_cc_params->params[i].port_num = port_num;
  380. dbg_cc_params->params[i].dentry =
  381. debugfs_create_file(mlx5_ib_dbg_cc_name[i],
  382. 0600, dbg_cc_params->root,
  383. &dbg_cc_params->params[i],
  384. &dbg_cc_fops);
  385. if (!dbg_cc_params->params[i].dentry)
  386. goto err;
  387. }
  388. put_mdev:
  389. mlx5_ib_put_native_port_mdev(dev, port_num + 1);
  390. out:
  391. return 0;
  392. err:
  393. mlx5_ib_warn(dev, "cong debugfs failure\n");
  394. mlx5_ib_cleanup_cong_debugfs(dev, port_num);
  395. mlx5_ib_put_native_port_mdev(dev, port_num + 1);
  396. /*
  397. * We don't want to fail driver if debugfs failed to initialize,
  398. * so we are not forwarding error to the user.
  399. */
  400. return 0;
  401. }