Skip to content

Commit 751c968

Browse files
committed
libxscale: Add support for pd and mr
This patch adds support for pd and mr operations including: 1. alloc_pd 2. dealloc_pd 3. reg_mr 4. dereg_mr Signed-off-by: Tian Xin <tianx@yunsilicon.com> Signed-off-by: Wei Honggang <weihg@yunsilicon.com> Signed-off-by: Zhao Qianwei <zhaoqw@yunsilicon.com> Signed-off-by: Li Qiang <liq@yunsilicon.com> Signed-off-by: Yan Lei <jacky@yunsilicon.com>
1 parent 23b8647 commit 751c968

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

providers/xscale/verbs.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,91 @@ int xsc_query_port(struct ibv_context *context, u8 port,
3636
return ibv_cmd_query_port(context, port, attr, &cmd, sizeof(cmd));
3737
}
3838

39+
struct ibv_pd *xsc_alloc_pd(struct ibv_context *context)
40+
{
41+
struct ibv_alloc_pd cmd;
42+
struct xsc_alloc_pd_resp resp;
43+
struct xsc_pd *pd;
44+
45+
pd = calloc(1, sizeof(*pd));
46+
if (!pd)
47+
return NULL;
48+
49+
if (ibv_cmd_alloc_pd(context, &pd->ibv_pd, &cmd, sizeof(cmd),
50+
&resp.ibv_resp, sizeof(resp))) {
51+
free(pd);
52+
return NULL;
53+
}
54+
55+
atomic_init(&pd->refcount, 1);
56+
pd->pdn = resp.pdn;
57+
xsc_dbg(to_xctx(context)->dbg_fp, XSC_DBG_PD, "pd number:%u\n",
58+
pd->pdn);
59+
60+
return &pd->ibv_pd;
61+
}
62+
63+
int xsc_free_pd(struct ibv_pd *pd)
64+
{
65+
int ret;
66+
struct xsc_pd *xpd = to_xpd(pd);
67+
68+
if (atomic_load(&xpd->refcount) > 1)
69+
return EBUSY;
70+
71+
ret = ibv_cmd_dealloc_pd(pd);
72+
if (ret)
73+
return ret;
74+
75+
xsc_dbg(to_xctx(pd->context)->dbg_fp, XSC_DBG_PD, "dealloc pd\n");
76+
free(xpd);
77+
78+
return 0;
79+
}
80+
81+
struct ibv_mr *xsc_reg_mr(struct ibv_pd *pd, void *addr, size_t length,
82+
u64 hca_va, int acc)
83+
{
84+
struct xsc_mr *mr;
85+
struct ibv_reg_mr cmd;
86+
int ret;
87+
enum ibv_access_flags access = (enum ibv_access_flags)acc;
88+
struct ib_uverbs_reg_mr_resp resp;
89+
90+
mr = calloc(1, sizeof(*mr));
91+
if (!mr)
92+
return NULL;
93+
94+
ret = ibv_cmd_reg_mr(pd, addr, length, hca_va, access, &mr->vmr, &cmd,
95+
sizeof(cmd), &resp, sizeof(resp));
96+
if (ret) {
97+
free(mr);
98+
return NULL;
99+
}
100+
mr->alloc_flags = acc;
101+
102+
xsc_dbg(to_xctx(pd->context)->dbg_fp, XSC_DBG_MR, "lkey:%u, rkey:%u\n",
103+
mr->vmr.ibv_mr.lkey, mr->vmr.ibv_mr.rkey);
104+
105+
return &mr->vmr.ibv_mr;
106+
}
107+
108+
int xsc_dereg_mr(struct verbs_mr *vmr)
109+
{
110+
int ret;
111+
112+
if (vmr->mr_type == IBV_MR_TYPE_NULL_MR)
113+
goto free;
114+
115+
ret = ibv_cmd_dereg_mr(vmr);
116+
if (ret)
117+
return ret;
118+
119+
free:
120+
free(vmr);
121+
return 0;
122+
}
123+
39124
static void xsc_set_fw_version(struct ibv_device_attr *attr,
40125
union xsc_ib_fw_ver *fw_ver)
41126
{

providers/xscale/xscale.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ static void xsc_free_context(struct ibv_context *ibctx);
3333

3434
static const struct verbs_context_ops xsc_ctx_common_ops = {
3535
.query_port = xsc_query_port,
36+
.alloc_pd = xsc_alloc_pd,
37+
.dealloc_pd = xsc_free_pd,
38+
.reg_mr = xsc_reg_mr,
39+
.dereg_mr = xsc_dereg_mr,
3640
.query_device_ex = xsc_query_device_ex,
3741
.free_context = xsc_free_context,
3842
};

providers/xscale/xscale.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ struct xsc_context {
120120
struct xsc_hw_ops *hw_ops;
121121
};
122122

123+
struct xsc_pd {
124+
struct ibv_pd ibv_pd;
125+
u32 pdn;
126+
atomic_int refcount;
127+
};
128+
129+
struct xsc_mr {
130+
struct verbs_mr vmr;
131+
u32 alloc_flags;
132+
};
133+
123134
union xsc_ib_fw_ver {
124135
u64 data;
125136
struct {
@@ -154,11 +165,29 @@ static inline struct xsc_context *to_xctx(struct ibv_context *ibctx)
154165
return container_of(ibctx, struct xsc_context, ibv_ctx.context);
155166
}
156167

168+
/* to_xpd always returns the real xsc_pd object ie the protection domain. */
169+
static inline struct xsc_pd *to_xpd(struct ibv_pd *ibpd)
170+
{
171+
return container_of(ibpd, struct xsc_pd, ibv_pd);
172+
}
173+
174+
static inline struct xsc_mr *to_xmr(struct ibv_mr *ibmr)
175+
{
176+
return container_of(ibmr, struct xsc_mr, vmr.ibv_mr);
177+
}
178+
157179
int xsc_query_device(struct ibv_context *context, struct ibv_device_attr *attr);
158180
int xsc_query_device_ex(struct ibv_context *context,
159181
const struct ibv_query_device_ex_input *input,
160182
struct ibv_device_attr_ex *attr, size_t attr_size);
161183
int xsc_query_port(struct ibv_context *context, u8 port,
162184
struct ibv_port_attr *attr);
163185

186+
struct ibv_pd *xsc_alloc_pd(struct ibv_context *context);
187+
int xsc_free_pd(struct ibv_pd *pd);
188+
189+
struct ibv_mr *xsc_reg_mr(struct ibv_pd *pd, void *addr, size_t length,
190+
u64 hca_va, int access);
191+
int xsc_dereg_mr(struct verbs_mr *mr);
192+
164193
#endif /* XSC_H */

0 commit comments

Comments
 (0)