counters.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2013-2020, Mellanox Technologies inc. All rights reserved.
  4. */
  5. #include "mlx5_ib.h"
  6. #include <linux/mlx5/eswitch.h>
  7. #include "counters.h"
  8. #include "ib_rep.h"
  9. #include "qp.h"
  10. struct mlx5_ib_counter {
  11. const char *name;
  12. size_t offset;
  13. u32 type;
  14. };
  15. #define INIT_Q_COUNTER(_name) \
  16. { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
  17. static const struct mlx5_ib_counter basic_q_cnts[] = {
  18. INIT_Q_COUNTER(rx_write_requests),
  19. INIT_Q_COUNTER(rx_read_requests),
  20. INIT_Q_COUNTER(rx_atomic_requests),
  21. INIT_Q_COUNTER(out_of_buffer),
  22. };
  23. static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
  24. INIT_Q_COUNTER(out_of_sequence),
  25. };
  26. static const struct mlx5_ib_counter retrans_q_cnts[] = {
  27. INIT_Q_COUNTER(duplicate_request),
  28. INIT_Q_COUNTER(rnr_nak_retry_err),
  29. INIT_Q_COUNTER(packet_seq_err),
  30. INIT_Q_COUNTER(implied_nak_seq_err),
  31. INIT_Q_COUNTER(local_ack_timeout_err),
  32. };
  33. #define INIT_CONG_COUNTER(_name) \
  34. { .name = #_name, .offset = \
  35. MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
  36. static const struct mlx5_ib_counter cong_cnts[] = {
  37. INIT_CONG_COUNTER(rp_cnp_ignored),
  38. INIT_CONG_COUNTER(rp_cnp_handled),
  39. INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
  40. INIT_CONG_COUNTER(np_cnp_sent),
  41. };
  42. static const struct mlx5_ib_counter extended_err_cnts[] = {
  43. INIT_Q_COUNTER(resp_local_length_error),
  44. INIT_Q_COUNTER(resp_cqe_error),
  45. INIT_Q_COUNTER(req_cqe_error),
  46. INIT_Q_COUNTER(req_remote_invalid_request),
  47. INIT_Q_COUNTER(req_remote_access_errors),
  48. INIT_Q_COUNTER(resp_remote_access_errors),
  49. INIT_Q_COUNTER(resp_cqe_flush_error),
  50. INIT_Q_COUNTER(req_cqe_flush_error),
  51. };
  52. static const struct mlx5_ib_counter roce_accl_cnts[] = {
  53. INIT_Q_COUNTER(roce_adp_retrans),
  54. INIT_Q_COUNTER(roce_adp_retrans_to),
  55. INIT_Q_COUNTER(roce_slow_restart),
  56. INIT_Q_COUNTER(roce_slow_restart_cnps),
  57. INIT_Q_COUNTER(roce_slow_restart_trans),
  58. };
  59. #define INIT_EXT_PPCNT_COUNTER(_name) \
  60. { .name = #_name, .offset = \
  61. MLX5_BYTE_OFF(ppcnt_reg, \
  62. counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
  63. static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
  64. INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
  65. };
  66. #define INIT_OP_COUNTER(_name, _type) \
  67. { .name = #_name, .type = MLX5_IB_OPCOUNTER_##_type}
  68. static const struct mlx5_ib_counter basic_op_cnts[] = {
  69. INIT_OP_COUNTER(cc_rx_ce_pkts, CC_RX_CE_PKTS),
  70. };
  71. static const struct mlx5_ib_counter rdmarx_cnp_op_cnts[] = {
  72. INIT_OP_COUNTER(cc_rx_cnp_pkts, CC_RX_CNP_PKTS),
  73. };
  74. static const struct mlx5_ib_counter rdmatx_cnp_op_cnts[] = {
  75. INIT_OP_COUNTER(cc_tx_cnp_pkts, CC_TX_CNP_PKTS),
  76. };
  77. static int mlx5_ib_read_counters(struct ib_counters *counters,
  78. struct ib_counters_read_attr *read_attr,
  79. struct uverbs_attr_bundle *attrs)
  80. {
  81. struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
  82. struct mlx5_read_counters_attr mread_attr = {};
  83. struct mlx5_ib_flow_counters_desc *desc;
  84. int ret, i;
  85. mutex_lock(&mcounters->mcntrs_mutex);
  86. if (mcounters->cntrs_max_index > read_attr->ncounters) {
  87. ret = -EINVAL;
  88. goto err_bound;
  89. }
  90. mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
  91. GFP_KERNEL);
  92. if (!mread_attr.out) {
  93. ret = -ENOMEM;
  94. goto err_bound;
  95. }
  96. mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
  97. mread_attr.flags = read_attr->flags;
  98. ret = mcounters->read_counters(counters->device, &mread_attr);
  99. if (ret)
  100. goto err_read;
  101. /* do the pass over the counters data array to assign according to the
  102. * descriptions and indexing pairs
  103. */
  104. desc = mcounters->counters_data;
  105. for (i = 0; i < mcounters->ncounters; i++)
  106. read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
  107. err_read:
  108. kfree(mread_attr.out);
  109. err_bound:
  110. mutex_unlock(&mcounters->mcntrs_mutex);
  111. return ret;
  112. }
  113. static int mlx5_ib_destroy_counters(struct ib_counters *counters)
  114. {
  115. struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
  116. mlx5_ib_counters_clear_description(counters);
  117. if (mcounters->hw_cntrs_hndl)
  118. mlx5_fc_destroy(to_mdev(counters->device)->mdev,
  119. mcounters->hw_cntrs_hndl);
  120. return 0;
  121. }
  122. static int mlx5_ib_create_counters(struct ib_counters *counters,
  123. struct uverbs_attr_bundle *attrs)
  124. {
  125. struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
  126. mutex_init(&mcounters->mcntrs_mutex);
  127. return 0;
  128. }
  129. static const struct mlx5_ib_counters *get_counters(struct mlx5_ib_dev *dev,
  130. u32 port_num)
  131. {
  132. return is_mdev_switchdev_mode(dev->mdev) ? &dev->port[0].cnts :
  133. &dev->port[port_num].cnts;
  134. }
  135. /**
  136. * mlx5_ib_get_counters_id - Returns counters id to use for device+port
  137. * @dev: Pointer to mlx5 IB device
  138. * @port_num: Zero based port number
  139. *
  140. * mlx5_ib_get_counters_id() Returns counters set id to use for given
  141. * device port combination in switchdev and non switchdev mode of the
  142. * parent device.
  143. */
  144. u16 mlx5_ib_get_counters_id(struct mlx5_ib_dev *dev, u32 port_num)
  145. {
  146. const struct mlx5_ib_counters *cnts = get_counters(dev, port_num);
  147. return cnts->set_id;
  148. }
  149. static struct rdma_hw_stats *do_alloc_stats(const struct mlx5_ib_counters *cnts)
  150. {
  151. struct rdma_hw_stats *stats;
  152. u32 num_hw_counters;
  153. int i;
  154. num_hw_counters = cnts->num_q_counters + cnts->num_cong_counters +
  155. cnts->num_ext_ppcnt_counters;
  156. stats = rdma_alloc_hw_stats_struct(cnts->descs,
  157. num_hw_counters +
  158. cnts->num_op_counters,
  159. RDMA_HW_STATS_DEFAULT_LIFESPAN);
  160. if (!stats)
  161. return NULL;
  162. for (i = 0; i < cnts->num_op_counters; i++)
  163. set_bit(num_hw_counters + i, stats->is_disabled);
  164. return stats;
  165. }
  166. static struct rdma_hw_stats *
  167. mlx5_ib_alloc_hw_device_stats(struct ib_device *ibdev)
  168. {
  169. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  170. const struct mlx5_ib_counters *cnts = &dev->port[0].cnts;
  171. return do_alloc_stats(cnts);
  172. }
  173. static struct rdma_hw_stats *
  174. mlx5_ib_alloc_hw_port_stats(struct ib_device *ibdev, u32 port_num)
  175. {
  176. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  177. const struct mlx5_ib_counters *cnts = &dev->port[port_num - 1].cnts;
  178. return do_alloc_stats(cnts);
  179. }
  180. static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
  181. const struct mlx5_ib_counters *cnts,
  182. struct rdma_hw_stats *stats,
  183. u16 set_id)
  184. {
  185. u32 out[MLX5_ST_SZ_DW(query_q_counter_out)] = {};
  186. u32 in[MLX5_ST_SZ_DW(query_q_counter_in)] = {};
  187. __be32 val;
  188. int ret, i;
  189. MLX5_SET(query_q_counter_in, in, opcode, MLX5_CMD_OP_QUERY_Q_COUNTER);
  190. MLX5_SET(query_q_counter_in, in, counter_set_id, set_id);
  191. ret = mlx5_cmd_exec_inout(mdev, query_q_counter, in, out);
  192. if (ret)
  193. return ret;
  194. for (i = 0; i < cnts->num_q_counters; i++) {
  195. val = *(__be32 *)((void *)out + cnts->offsets[i]);
  196. stats->value[i] = (u64)be32_to_cpu(val);
  197. }
  198. return 0;
  199. }
  200. static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
  201. const struct mlx5_ib_counters *cnts,
  202. struct rdma_hw_stats *stats)
  203. {
  204. int offset = cnts->num_q_counters + cnts->num_cong_counters;
  205. u32 in[MLX5_ST_SZ_DW(ppcnt_reg)] = {};
  206. int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
  207. int ret, i;
  208. void *out;
  209. out = kvzalloc(sz, GFP_KERNEL);
  210. if (!out)
  211. return -ENOMEM;
  212. MLX5_SET(ppcnt_reg, in, local_port, 1);
  213. MLX5_SET(ppcnt_reg, in, grp, MLX5_ETHERNET_EXTENDED_COUNTERS_GROUP);
  214. ret = mlx5_core_access_reg(dev->mdev, in, sz, out, sz, MLX5_REG_PPCNT,
  215. 0, 0);
  216. if (ret)
  217. goto free;
  218. for (i = 0; i < cnts->num_ext_ppcnt_counters; i++)
  219. stats->value[i + offset] =
  220. be64_to_cpup((__be64 *)(out +
  221. cnts->offsets[i + offset]));
  222. free:
  223. kvfree(out);
  224. return ret;
  225. }
  226. static int do_get_hw_stats(struct ib_device *ibdev,
  227. struct rdma_hw_stats *stats,
  228. u32 port_num, int index)
  229. {
  230. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  231. const struct mlx5_ib_counters *cnts = get_counters(dev, port_num - 1);
  232. struct mlx5_core_dev *mdev;
  233. int ret, num_counters;
  234. if (!stats)
  235. return -EINVAL;
  236. num_counters = cnts->num_q_counters +
  237. cnts->num_cong_counters +
  238. cnts->num_ext_ppcnt_counters;
  239. /* q_counters are per IB device, query the master mdev */
  240. ret = mlx5_ib_query_q_counters(dev->mdev, cnts, stats, cnts->set_id);
  241. if (ret)
  242. return ret;
  243. if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
  244. ret = mlx5_ib_query_ext_ppcnt_counters(dev, cnts, stats);
  245. if (ret)
  246. return ret;
  247. }
  248. if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
  249. if (!port_num)
  250. port_num = 1;
  251. mdev = mlx5_ib_get_native_port_mdev(dev, port_num, NULL);
  252. if (!mdev) {
  253. /* If port is not affiliated yet, its in down state
  254. * which doesn't have any counters yet, so it would be
  255. * zero. So no need to read from the HCA.
  256. */
  257. goto done;
  258. }
  259. ret = mlx5_lag_query_cong_counters(dev->mdev,
  260. stats->value +
  261. cnts->num_q_counters,
  262. cnts->num_cong_counters,
  263. cnts->offsets +
  264. cnts->num_q_counters);
  265. mlx5_ib_put_native_port_mdev(dev, port_num);
  266. if (ret)
  267. return ret;
  268. }
  269. done:
  270. return num_counters;
  271. }
  272. static int do_get_op_stat(struct ib_device *ibdev,
  273. struct rdma_hw_stats *stats,
  274. u32 port_num, int index)
  275. {
  276. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  277. const struct mlx5_ib_counters *cnts;
  278. const struct mlx5_ib_op_fc *opfcs;
  279. u64 packets = 0, bytes;
  280. u32 type;
  281. int ret;
  282. cnts = get_counters(dev, port_num - 1);
  283. opfcs = cnts->opfcs;
  284. type = *(u32 *)cnts->descs[index].priv;
  285. if (type >= MLX5_IB_OPCOUNTER_MAX)
  286. return -EINVAL;
  287. if (!opfcs[type].fc)
  288. goto out;
  289. ret = mlx5_fc_query(dev->mdev, opfcs[type].fc,
  290. &packets, &bytes);
  291. if (ret)
  292. return ret;
  293. out:
  294. stats->value[index] = packets;
  295. return index;
  296. }
  297. static int do_get_op_stats(struct ib_device *ibdev,
  298. struct rdma_hw_stats *stats,
  299. u32 port_num)
  300. {
  301. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  302. const struct mlx5_ib_counters *cnts;
  303. int index, ret, num_hw_counters;
  304. cnts = get_counters(dev, port_num - 1);
  305. num_hw_counters = cnts->num_q_counters + cnts->num_cong_counters +
  306. cnts->num_ext_ppcnt_counters;
  307. for (index = num_hw_counters;
  308. index < (num_hw_counters + cnts->num_op_counters); index++) {
  309. ret = do_get_op_stat(ibdev, stats, port_num, index);
  310. if (ret != index)
  311. return ret;
  312. }
  313. return cnts->num_op_counters;
  314. }
  315. static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
  316. struct rdma_hw_stats *stats,
  317. u32 port_num, int index)
  318. {
  319. int num_counters, num_hw_counters, num_op_counters;
  320. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  321. const struct mlx5_ib_counters *cnts;
  322. cnts = get_counters(dev, port_num - 1);
  323. num_hw_counters = cnts->num_q_counters + cnts->num_cong_counters +
  324. cnts->num_ext_ppcnt_counters;
  325. num_counters = num_hw_counters + cnts->num_op_counters;
  326. if (index < 0 || index > num_counters)
  327. return -EINVAL;
  328. else if (index > 0 && index < num_hw_counters)
  329. return do_get_hw_stats(ibdev, stats, port_num, index);
  330. else if (index >= num_hw_counters && index < num_counters)
  331. return do_get_op_stat(ibdev, stats, port_num, index);
  332. num_hw_counters = do_get_hw_stats(ibdev, stats, port_num, index);
  333. if (num_hw_counters < 0)
  334. return num_hw_counters;
  335. num_op_counters = do_get_op_stats(ibdev, stats, port_num);
  336. if (num_op_counters < 0)
  337. return num_op_counters;
  338. return num_hw_counters + num_op_counters;
  339. }
  340. static struct rdma_hw_stats *
  341. mlx5_ib_counter_alloc_stats(struct rdma_counter *counter)
  342. {
  343. struct mlx5_ib_dev *dev = to_mdev(counter->device);
  344. const struct mlx5_ib_counters *cnts =
  345. get_counters(dev, counter->port - 1);
  346. return do_alloc_stats(cnts);
  347. }
  348. static int mlx5_ib_counter_update_stats(struct rdma_counter *counter)
  349. {
  350. struct mlx5_ib_dev *dev = to_mdev(counter->device);
  351. const struct mlx5_ib_counters *cnts =
  352. get_counters(dev, counter->port - 1);
  353. return mlx5_ib_query_q_counters(dev->mdev, cnts,
  354. counter->stats, counter->id);
  355. }
  356. static int mlx5_ib_counter_dealloc(struct rdma_counter *counter)
  357. {
  358. struct mlx5_ib_dev *dev = to_mdev(counter->device);
  359. u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {};
  360. if (!counter->id)
  361. return 0;
  362. MLX5_SET(dealloc_q_counter_in, in, opcode,
  363. MLX5_CMD_OP_DEALLOC_Q_COUNTER);
  364. MLX5_SET(dealloc_q_counter_in, in, counter_set_id, counter->id);
  365. return mlx5_cmd_exec_in(dev->mdev, dealloc_q_counter, in);
  366. }
  367. static int mlx5_ib_counter_bind_qp(struct rdma_counter *counter,
  368. struct ib_qp *qp)
  369. {
  370. struct mlx5_ib_dev *dev = to_mdev(qp->device);
  371. int err;
  372. if (!counter->id) {
  373. u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {};
  374. u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {};
  375. MLX5_SET(alloc_q_counter_in, in, opcode,
  376. MLX5_CMD_OP_ALLOC_Q_COUNTER);
  377. MLX5_SET(alloc_q_counter_in, in, uid, MLX5_SHARED_RESOURCE_UID);
  378. err = mlx5_cmd_exec_inout(dev->mdev, alloc_q_counter, in, out);
  379. if (err)
  380. return err;
  381. counter->id =
  382. MLX5_GET(alloc_q_counter_out, out, counter_set_id);
  383. }
  384. err = mlx5_ib_qp_set_counter(qp, counter);
  385. if (err)
  386. goto fail_set_counter;
  387. return 0;
  388. fail_set_counter:
  389. mlx5_ib_counter_dealloc(counter);
  390. counter->id = 0;
  391. return err;
  392. }
  393. static int mlx5_ib_counter_unbind_qp(struct ib_qp *qp)
  394. {
  395. return mlx5_ib_qp_set_counter(qp, NULL);
  396. }
  397. static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
  398. struct rdma_stat_desc *descs, size_t *offsets)
  399. {
  400. int i;
  401. int j = 0;
  402. for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
  403. descs[j].name = basic_q_cnts[i].name;
  404. offsets[j] = basic_q_cnts[i].offset;
  405. }
  406. if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
  407. for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
  408. descs[j].name = out_of_seq_q_cnts[i].name;
  409. offsets[j] = out_of_seq_q_cnts[i].offset;
  410. }
  411. }
  412. if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
  413. for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
  414. descs[j].name = retrans_q_cnts[i].name;
  415. offsets[j] = retrans_q_cnts[i].offset;
  416. }
  417. }
  418. if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
  419. for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
  420. descs[j].name = extended_err_cnts[i].name;
  421. offsets[j] = extended_err_cnts[i].offset;
  422. }
  423. }
  424. if (MLX5_CAP_GEN(dev->mdev, roce_accl)) {
  425. for (i = 0; i < ARRAY_SIZE(roce_accl_cnts); i++, j++) {
  426. descs[j].name = roce_accl_cnts[i].name;
  427. offsets[j] = roce_accl_cnts[i].offset;
  428. }
  429. }
  430. if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
  431. for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
  432. descs[j].name = cong_cnts[i].name;
  433. offsets[j] = cong_cnts[i].offset;
  434. }
  435. }
  436. if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
  437. for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
  438. descs[j].name = ext_ppcnt_cnts[i].name;
  439. offsets[j] = ext_ppcnt_cnts[i].offset;
  440. }
  441. }
  442. for (i = 0; i < ARRAY_SIZE(basic_op_cnts); i++, j++) {
  443. descs[j].name = basic_op_cnts[i].name;
  444. descs[j].flags |= IB_STAT_FLAG_OPTIONAL;
  445. descs[j].priv = &basic_op_cnts[i].type;
  446. }
  447. if (MLX5_CAP_FLOWTABLE(dev->mdev,
  448. ft_field_support_2_nic_receive_rdma.bth_opcode)) {
  449. for (i = 0; i < ARRAY_SIZE(rdmarx_cnp_op_cnts); i++, j++) {
  450. descs[j].name = rdmarx_cnp_op_cnts[i].name;
  451. descs[j].flags |= IB_STAT_FLAG_OPTIONAL;
  452. descs[j].priv = &rdmarx_cnp_op_cnts[i].type;
  453. }
  454. }
  455. if (MLX5_CAP_FLOWTABLE(dev->mdev,
  456. ft_field_support_2_nic_transmit_rdma.bth_opcode)) {
  457. for (i = 0; i < ARRAY_SIZE(rdmatx_cnp_op_cnts); i++, j++) {
  458. descs[j].name = rdmatx_cnp_op_cnts[i].name;
  459. descs[j].flags |= IB_STAT_FLAG_OPTIONAL;
  460. descs[j].priv = &rdmatx_cnp_op_cnts[i].type;
  461. }
  462. }
  463. }
  464. static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
  465. struct mlx5_ib_counters *cnts)
  466. {
  467. u32 num_counters, num_op_counters;
  468. num_counters = ARRAY_SIZE(basic_q_cnts);
  469. if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
  470. num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
  471. if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
  472. num_counters += ARRAY_SIZE(retrans_q_cnts);
  473. if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
  474. num_counters += ARRAY_SIZE(extended_err_cnts);
  475. if (MLX5_CAP_GEN(dev->mdev, roce_accl))
  476. num_counters += ARRAY_SIZE(roce_accl_cnts);
  477. cnts->num_q_counters = num_counters;
  478. if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
  479. cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
  480. num_counters += ARRAY_SIZE(cong_cnts);
  481. }
  482. if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
  483. cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
  484. num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
  485. }
  486. num_op_counters = ARRAY_SIZE(basic_op_cnts);
  487. if (MLX5_CAP_FLOWTABLE(dev->mdev,
  488. ft_field_support_2_nic_receive_rdma.bth_opcode))
  489. num_op_counters += ARRAY_SIZE(rdmarx_cnp_op_cnts);
  490. if (MLX5_CAP_FLOWTABLE(dev->mdev,
  491. ft_field_support_2_nic_transmit_rdma.bth_opcode))
  492. num_op_counters += ARRAY_SIZE(rdmatx_cnp_op_cnts);
  493. cnts->num_op_counters = num_op_counters;
  494. num_counters += num_op_counters;
  495. cnts->descs = kcalloc(num_counters,
  496. sizeof(struct rdma_stat_desc), GFP_KERNEL);
  497. if (!cnts->descs)
  498. return -ENOMEM;
  499. cnts->offsets = kcalloc(num_counters,
  500. sizeof(*cnts->offsets), GFP_KERNEL);
  501. if (!cnts->offsets)
  502. goto err;
  503. return 0;
  504. err:
  505. kfree(cnts->descs);
  506. cnts->descs = NULL;
  507. return -ENOMEM;
  508. }
  509. static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
  510. {
  511. u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {};
  512. int num_cnt_ports;
  513. int i, j;
  514. num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
  515. MLX5_SET(dealloc_q_counter_in, in, opcode,
  516. MLX5_CMD_OP_DEALLOC_Q_COUNTER);
  517. for (i = 0; i < num_cnt_ports; i++) {
  518. if (dev->port[i].cnts.set_id) {
  519. MLX5_SET(dealloc_q_counter_in, in, counter_set_id,
  520. dev->port[i].cnts.set_id);
  521. mlx5_cmd_exec_in(dev->mdev, dealloc_q_counter, in);
  522. }
  523. kfree(dev->port[i].cnts.descs);
  524. kfree(dev->port[i].cnts.offsets);
  525. for (j = 0; j < MLX5_IB_OPCOUNTER_MAX; j++) {
  526. if (!dev->port[i].cnts.opfcs[j].fc)
  527. continue;
  528. if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
  529. mlx5_ib_fs_remove_op_fc(dev,
  530. &dev->port[i].cnts.opfcs[j], j);
  531. mlx5_fc_destroy(dev->mdev,
  532. dev->port[i].cnts.opfcs[j].fc);
  533. dev->port[i].cnts.opfcs[j].fc = NULL;
  534. }
  535. }
  536. }
  537. static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
  538. {
  539. u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {};
  540. u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {};
  541. int num_cnt_ports;
  542. int err = 0;
  543. int i;
  544. bool is_shared;
  545. MLX5_SET(alloc_q_counter_in, in, opcode, MLX5_CMD_OP_ALLOC_Q_COUNTER);
  546. is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
  547. num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
  548. for (i = 0; i < num_cnt_ports; i++) {
  549. err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
  550. if (err)
  551. goto err_alloc;
  552. mlx5_ib_fill_counters(dev, dev->port[i].cnts.descs,
  553. dev->port[i].cnts.offsets);
  554. MLX5_SET(alloc_q_counter_in, in, uid,
  555. is_shared ? MLX5_SHARED_RESOURCE_UID : 0);
  556. err = mlx5_cmd_exec_inout(dev->mdev, alloc_q_counter, in, out);
  557. if (err) {
  558. mlx5_ib_warn(dev,
  559. "couldn't allocate queue counter for port %d, err %d\n",
  560. i + 1, err);
  561. goto err_alloc;
  562. }
  563. dev->port[i].cnts.set_id =
  564. MLX5_GET(alloc_q_counter_out, out, counter_set_id);
  565. }
  566. return 0;
  567. err_alloc:
  568. mlx5_ib_dealloc_counters(dev);
  569. return err;
  570. }
  571. static int read_flow_counters(struct ib_device *ibdev,
  572. struct mlx5_read_counters_attr *read_attr)
  573. {
  574. struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
  575. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  576. return mlx5_fc_query(dev->mdev, fc,
  577. &read_attr->out[IB_COUNTER_PACKETS],
  578. &read_attr->out[IB_COUNTER_BYTES]);
  579. }
  580. /* flow counters currently expose two counters packets and bytes */
  581. #define FLOW_COUNTERS_NUM 2
  582. static int counters_set_description(
  583. struct ib_counters *counters, enum mlx5_ib_counters_type counters_type,
  584. struct mlx5_ib_flow_counters_desc *desc_data, u32 ncounters)
  585. {
  586. struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
  587. u32 cntrs_max_index = 0;
  588. int i;
  589. if (counters_type != MLX5_IB_COUNTERS_FLOW)
  590. return -EINVAL;
  591. /* init the fields for the object */
  592. mcounters->type = counters_type;
  593. mcounters->read_counters = read_flow_counters;
  594. mcounters->counters_num = FLOW_COUNTERS_NUM;
  595. mcounters->ncounters = ncounters;
  596. /* each counter entry have both description and index pair */
  597. for (i = 0; i < ncounters; i++) {
  598. if (desc_data[i].description > IB_COUNTER_BYTES)
  599. return -EINVAL;
  600. if (cntrs_max_index <= desc_data[i].index)
  601. cntrs_max_index = desc_data[i].index + 1;
  602. }
  603. mutex_lock(&mcounters->mcntrs_mutex);
  604. mcounters->counters_data = desc_data;
  605. mcounters->cntrs_max_index = cntrs_max_index;
  606. mutex_unlock(&mcounters->mcntrs_mutex);
  607. return 0;
  608. }
  609. #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
  610. int mlx5_ib_flow_counters_set_data(struct ib_counters *ibcounters,
  611. struct mlx5_ib_create_flow *ucmd)
  612. {
  613. struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
  614. struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
  615. struct mlx5_ib_flow_counters_desc *desc_data = NULL;
  616. bool hw_hndl = false;
  617. int ret = 0;
  618. if (ucmd && ucmd->ncounters_data != 0) {
  619. cntrs_data = ucmd->data;
  620. if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
  621. return -EINVAL;
  622. desc_data = kcalloc(cntrs_data->ncounters,
  623. sizeof(*desc_data),
  624. GFP_KERNEL);
  625. if (!desc_data)
  626. return -ENOMEM;
  627. if (copy_from_user(desc_data,
  628. u64_to_user_ptr(cntrs_data->counters_data),
  629. sizeof(*desc_data) * cntrs_data->ncounters)) {
  630. ret = -EFAULT;
  631. goto free;
  632. }
  633. }
  634. if (!mcounters->hw_cntrs_hndl) {
  635. mcounters->hw_cntrs_hndl = mlx5_fc_create(
  636. to_mdev(ibcounters->device)->mdev, false);
  637. if (IS_ERR(mcounters->hw_cntrs_hndl)) {
  638. ret = PTR_ERR(mcounters->hw_cntrs_hndl);
  639. goto free;
  640. }
  641. hw_hndl = true;
  642. }
  643. if (desc_data) {
  644. /* counters already bound to at least one flow */
  645. if (mcounters->cntrs_max_index) {
  646. ret = -EINVAL;
  647. goto free_hndl;
  648. }
  649. ret = counters_set_description(ibcounters,
  650. MLX5_IB_COUNTERS_FLOW,
  651. desc_data,
  652. cntrs_data->ncounters);
  653. if (ret)
  654. goto free_hndl;
  655. } else if (!mcounters->cntrs_max_index) {
  656. /* counters not bound yet, must have udata passed */
  657. ret = -EINVAL;
  658. goto free_hndl;
  659. }
  660. return 0;
  661. free_hndl:
  662. if (hw_hndl) {
  663. mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
  664. mcounters->hw_cntrs_hndl);
  665. mcounters->hw_cntrs_hndl = NULL;
  666. }
  667. free:
  668. kfree(desc_data);
  669. return ret;
  670. }
  671. void mlx5_ib_counters_clear_description(struct ib_counters *counters)
  672. {
  673. struct mlx5_ib_mcounters *mcounters;
  674. if (!counters || atomic_read(&counters->usecnt) != 1)
  675. return;
  676. mcounters = to_mcounters(counters);
  677. mutex_lock(&mcounters->mcntrs_mutex);
  678. kfree(mcounters->counters_data);
  679. mcounters->counters_data = NULL;
  680. mcounters->cntrs_max_index = 0;
  681. mutex_unlock(&mcounters->mcntrs_mutex);
  682. }
  683. static int mlx5_ib_modify_stat(struct ib_device *device, u32 port,
  684. unsigned int index, bool enable)
  685. {
  686. struct mlx5_ib_dev *dev = to_mdev(device);
  687. struct mlx5_ib_counters *cnts;
  688. struct mlx5_ib_op_fc *opfc;
  689. u32 num_hw_counters, type;
  690. int ret;
  691. cnts = &dev->port[port - 1].cnts;
  692. num_hw_counters = cnts->num_q_counters + cnts->num_cong_counters +
  693. cnts->num_ext_ppcnt_counters;
  694. if (index < num_hw_counters ||
  695. index >= (num_hw_counters + cnts->num_op_counters))
  696. return -EINVAL;
  697. if (!(cnts->descs[index].flags & IB_STAT_FLAG_OPTIONAL))
  698. return -EINVAL;
  699. type = *(u32 *)cnts->descs[index].priv;
  700. if (type >= MLX5_IB_OPCOUNTER_MAX)
  701. return -EINVAL;
  702. opfc = &cnts->opfcs[type];
  703. if (enable) {
  704. if (opfc->fc)
  705. return -EEXIST;
  706. opfc->fc = mlx5_fc_create(dev->mdev, false);
  707. if (IS_ERR(opfc->fc))
  708. return PTR_ERR(opfc->fc);
  709. ret = mlx5_ib_fs_add_op_fc(dev, port, opfc, type);
  710. if (ret) {
  711. mlx5_fc_destroy(dev->mdev, opfc->fc);
  712. opfc->fc = NULL;
  713. }
  714. return ret;
  715. }
  716. if (!opfc->fc)
  717. return -EINVAL;
  718. mlx5_ib_fs_remove_op_fc(dev, opfc, type);
  719. mlx5_fc_destroy(dev->mdev, opfc->fc);
  720. opfc->fc = NULL;
  721. return 0;
  722. }
  723. static const struct ib_device_ops hw_stats_ops = {
  724. .alloc_hw_port_stats = mlx5_ib_alloc_hw_port_stats,
  725. .get_hw_stats = mlx5_ib_get_hw_stats,
  726. .counter_bind_qp = mlx5_ib_counter_bind_qp,
  727. .counter_unbind_qp = mlx5_ib_counter_unbind_qp,
  728. .counter_dealloc = mlx5_ib_counter_dealloc,
  729. .counter_alloc_stats = mlx5_ib_counter_alloc_stats,
  730. .counter_update_stats = mlx5_ib_counter_update_stats,
  731. .modify_hw_stat = IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) ?
  732. mlx5_ib_modify_stat : NULL,
  733. };
  734. static const struct ib_device_ops hw_switchdev_stats_ops = {
  735. .alloc_hw_device_stats = mlx5_ib_alloc_hw_device_stats,
  736. .get_hw_stats = mlx5_ib_get_hw_stats,
  737. .counter_bind_qp = mlx5_ib_counter_bind_qp,
  738. .counter_unbind_qp = mlx5_ib_counter_unbind_qp,
  739. .counter_dealloc = mlx5_ib_counter_dealloc,
  740. .counter_alloc_stats = mlx5_ib_counter_alloc_stats,
  741. .counter_update_stats = mlx5_ib_counter_update_stats,
  742. };
  743. static const struct ib_device_ops counters_ops = {
  744. .create_counters = mlx5_ib_create_counters,
  745. .destroy_counters = mlx5_ib_destroy_counters,
  746. .read_counters = mlx5_ib_read_counters,
  747. INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs),
  748. };
  749. int mlx5_ib_counters_init(struct mlx5_ib_dev *dev)
  750. {
  751. ib_set_device_ops(&dev->ib_dev, &counters_ops);
  752. if (!MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
  753. return 0;
  754. if (is_mdev_switchdev_mode(dev->mdev))
  755. ib_set_device_ops(&dev->ib_dev, &hw_switchdev_stats_ops);
  756. else
  757. ib_set_device_ops(&dev->ib_dev, &hw_stats_ops);
  758. return mlx5_ib_alloc_counters(dev);
  759. }
  760. void mlx5_ib_counters_cleanup(struct mlx5_ib_dev *dev)
  761. {
  762. if (!MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
  763. return;
  764. mlx5_ib_dealloc_counters(dev);
  765. }