1// SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
2// Copyright (c) 2019 Hisilicon Limited.
3
4#include <rdma/rdma_cm.h>
5#include <rdma/restrack.h>
6#include <uapi/rdma/rdma_netlink.h>
7#include "hnae3.h"
8#include "hns_roce_common.h"
9#include "hns_roce_device.h"
10#include "hns_roce_hw_v2.h"
11
12static int hns_roce_fill_cq(struct sk_buff *msg,
13			    struct hns_roce_v2_cq_context *context)
14{
15	if (rdma_nl_put_driver_u32(msg, "state",
16				   roce_get_field(context->byte_4_pg_ceqn,
17						  V2_CQC_BYTE_4_ARM_ST_M,
18						  V2_CQC_BYTE_4_ARM_ST_S)))
19		goto err;
20
21	if (rdma_nl_put_driver_u32(msg, "ceqn",
22				   roce_get_field(context->byte_4_pg_ceqn,
23						  V2_CQC_BYTE_4_CEQN_M,
24						  V2_CQC_BYTE_4_CEQN_S)))
25		goto err;
26
27	if (rdma_nl_put_driver_u32(msg, "cqn",
28				   roce_get_field(context->byte_8_cqn,
29						  V2_CQC_BYTE_8_CQN_M,
30						  V2_CQC_BYTE_8_CQN_S)))
31		goto err;
32
33	if (rdma_nl_put_driver_u32(msg, "hopnum",
34				   roce_get_field(context->byte_16_hop_addr,
35						  V2_CQC_BYTE_16_CQE_HOP_NUM_M,
36						  V2_CQC_BYTE_16_CQE_HOP_NUM_S)))
37		goto err;
38
39	if (rdma_nl_put_driver_u32(
40		    msg, "pi",
41		    roce_get_field(context->byte_28_cq_pi,
42				   V2_CQC_BYTE_28_CQ_PRODUCER_IDX_M,
43				   V2_CQC_BYTE_28_CQ_PRODUCER_IDX_S)))
44		goto err;
45
46	if (rdma_nl_put_driver_u32(
47		    msg, "ci",
48		    roce_get_field(context->byte_32_cq_ci,
49				   V2_CQC_BYTE_32_CQ_CONSUMER_IDX_M,
50				   V2_CQC_BYTE_32_CQ_CONSUMER_IDX_S)))
51		goto err;
52
53	if (rdma_nl_put_driver_u32(
54		    msg, "coalesce",
55		    roce_get_field(context->byte_56_cqe_period_maxcnt,
56				   V2_CQC_BYTE_56_CQ_MAX_CNT_M,
57				   V2_CQC_BYTE_56_CQ_MAX_CNT_S)))
58		goto err;
59
60	if (rdma_nl_put_driver_u32(
61		    msg, "period",
62		    roce_get_field(context->byte_56_cqe_period_maxcnt,
63				   V2_CQC_BYTE_56_CQ_PERIOD_M,
64				   V2_CQC_BYTE_56_CQ_PERIOD_S)))
65		goto err;
66
67	if (rdma_nl_put_driver_u32(msg, "cnt",
68				   roce_get_field(context->byte_52_cqe_cnt,
69						  V2_CQC_BYTE_52_CQE_CNT_M,
70						  V2_CQC_BYTE_52_CQE_CNT_S)))
71		goto err;
72
73	return 0;
74
75err:
76	return -EMSGSIZE;
77}
78
79int hns_roce_fill_res_cq_entry(struct sk_buff *msg,
80			       struct ib_cq *ib_cq)
81{
82	struct hns_roce_dev *hr_dev = to_hr_dev(ib_cq->device);
83	struct hns_roce_cq *hr_cq = to_hr_cq(ib_cq);
84	struct hns_roce_v2_cq_context *context;
85	struct nlattr *table_attr;
86	int ret;
87
88	if (!hr_dev->dfx->query_cqc_info)
89		return -EINVAL;
90
91	context = kzalloc(sizeof(struct hns_roce_v2_cq_context), GFP_KERNEL);
92	if (!context)
93		return -ENOMEM;
94
95	ret = hr_dev->dfx->query_cqc_info(hr_dev, hr_cq->cqn, (int *)context);
96	if (ret)
97		goto err;
98
99	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
100	if (!table_attr) {
101		ret = -EMSGSIZE;
102		goto err;
103	}
104
105	if (hns_roce_fill_cq(msg, context)) {
106		ret = -EMSGSIZE;
107		goto err_cancel_table;
108	}
109
110	nla_nest_end(msg, table_attr);
111	kfree(context);
112
113	return 0;
114
115err_cancel_table:
116	nla_nest_cancel(msg, table_attr);
117err:
118	kfree(context);
119	return ret;
120}
121