2 * Copyright (c) 2018, Mellanox Technologies inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include "rdma_core.h"
35 #include <rdma/uverbs_std_types.h>
38 static int uverbs_free_mr(struct ib_uobject *uobject,
39 enum rdma_remove_reason why,
40 struct uverbs_attr_bundle *attrs)
42 return ib_dereg_mr_user((struct ib_mr *)uobject->object,
43 &attrs->driver_udata);
46 static int UVERBS_HANDLER(UVERBS_METHOD_ADVISE_MR)(
47 struct uverbs_attr_bundle *attrs)
50 uverbs_attr_get_obj(attrs, UVERBS_ATTR_ADVISE_MR_PD_HANDLE);
51 enum ib_uverbs_advise_mr_advice advice;
52 struct ib_device *ib_dev = pd->device;
53 struct ib_sge *sg_list;
58 /* FIXME: Extend the UAPI_DEF_OBJ_NEEDS_FN stuff.. */
59 if (!ib_dev->ops.advise_mr)
62 ret = uverbs_get_const(&advice, attrs, UVERBS_ATTR_ADVISE_MR_ADVICE);
66 ret = uverbs_get_flags32(&flags, attrs, UVERBS_ATTR_ADVISE_MR_FLAGS,
67 IB_UVERBS_ADVISE_MR_FLAG_FLUSH);
71 num_sge = uverbs_attr_ptr_get_array_size(
72 attrs, UVERBS_ATTR_ADVISE_MR_SGE_LIST, sizeof(struct ib_sge));
76 sg_list = uverbs_attr_get_alloced_ptr(attrs,
77 UVERBS_ATTR_ADVISE_MR_SGE_LIST);
78 return ib_dev->ops.advise_mr(pd, advice, flags, sg_list, num_sge,
82 static int UVERBS_HANDLER(UVERBS_METHOD_DM_MR_REG)(
83 struct uverbs_attr_bundle *attrs)
85 struct ib_dm_mr_attr attr = {};
86 struct ib_uobject *uobj =
87 uverbs_attr_get_uobject(attrs, UVERBS_ATTR_REG_DM_MR_HANDLE);
89 uverbs_attr_get_obj(attrs, UVERBS_ATTR_REG_DM_MR_DM_HANDLE);
91 uverbs_attr_get_obj(attrs, UVERBS_ATTR_REG_DM_MR_PD_HANDLE);
92 struct ib_device *ib_dev = pd->device;
97 if (!ib_dev->ops.reg_dm_mr)
100 ret = uverbs_copy_from(&attr.offset, attrs, UVERBS_ATTR_REG_DM_MR_OFFSET);
104 ret = uverbs_copy_from(&attr.length, attrs,
105 UVERBS_ATTR_REG_DM_MR_LENGTH);
109 ret = uverbs_get_flags32(&attr.access_flags, attrs,
110 UVERBS_ATTR_REG_DM_MR_ACCESS_FLAGS,
111 IB_ACCESS_SUPPORTED);
115 if (!(attr.access_flags & IB_ZERO_BASED))
118 ret = ib_check_mr_access(ib_dev, attr.access_flags);
122 if (attr.offset > dm->length || attr.length > dm->length ||
123 attr.length > dm->length - attr.offset)
126 mr = pd->device->ops.reg_dm_mr(pd, dm, &attr, attrs);
130 mr->device = pd->device;
132 mr->type = IB_MR_TYPE_DM;
135 atomic_inc(&pd->usecnt);
136 atomic_inc(&dm->usecnt);
138 rdma_restrack_new(&mr->res, RDMA_RESTRACK_MR);
139 rdma_restrack_set_name(&mr->res, NULL);
140 rdma_restrack_add(&mr->res);
143 uverbs_finalize_uobj_create(attrs, UVERBS_ATTR_REG_DM_MR_HANDLE);
145 ret = uverbs_copy_to(attrs, UVERBS_ATTR_REG_DM_MR_RESP_LKEY, &mr->lkey,
150 ret = uverbs_copy_to(attrs, UVERBS_ATTR_REG_DM_MR_RESP_RKEY,
151 &mr->rkey, sizeof(mr->rkey));
155 static int UVERBS_HANDLER(UVERBS_METHOD_QUERY_MR)(
156 struct uverbs_attr_bundle *attrs)
159 uverbs_attr_get_obj(attrs, UVERBS_ATTR_QUERY_MR_HANDLE);
162 ret = uverbs_copy_to(attrs, UVERBS_ATTR_QUERY_MR_RESP_LKEY, &mr->lkey,
167 ret = uverbs_copy_to(attrs, UVERBS_ATTR_QUERY_MR_RESP_RKEY,
168 &mr->rkey, sizeof(mr->rkey));
173 ret = uverbs_copy_to(attrs, UVERBS_ATTR_QUERY_MR_RESP_LENGTH,
174 &mr->length, sizeof(mr->length));
179 ret = uverbs_copy_to(attrs, UVERBS_ATTR_QUERY_MR_RESP_IOVA,
180 &mr->iova, sizeof(mr->iova));
182 return IS_UVERBS_COPY_ERR(ret) ? ret : 0;
185 DECLARE_UVERBS_NAMED_METHOD(
186 UVERBS_METHOD_ADVISE_MR,
187 UVERBS_ATTR_IDR(UVERBS_ATTR_ADVISE_MR_PD_HANDLE,
191 UVERBS_ATTR_CONST_IN(UVERBS_ATTR_ADVISE_MR_ADVICE,
192 enum ib_uverbs_advise_mr_advice,
194 UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_ADVISE_MR_FLAGS,
195 enum ib_uverbs_advise_mr_flag,
197 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_ADVISE_MR_SGE_LIST,
198 UVERBS_ATTR_MIN_SIZE(sizeof(struct ib_uverbs_sge)),
202 DECLARE_UVERBS_NAMED_METHOD(
203 UVERBS_METHOD_QUERY_MR,
204 UVERBS_ATTR_IDR(UVERBS_ATTR_QUERY_MR_HANDLE,
208 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_MR_RESP_RKEY,
209 UVERBS_ATTR_TYPE(u32),
211 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_MR_RESP_LKEY,
212 UVERBS_ATTR_TYPE(u32),
214 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_MR_RESP_LENGTH,
215 UVERBS_ATTR_TYPE(u64),
217 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_QUERY_MR_RESP_IOVA,
218 UVERBS_ATTR_TYPE(u64),
221 DECLARE_UVERBS_NAMED_METHOD(
222 UVERBS_METHOD_DM_MR_REG,
223 UVERBS_ATTR_IDR(UVERBS_ATTR_REG_DM_MR_HANDLE,
227 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_REG_DM_MR_OFFSET,
228 UVERBS_ATTR_TYPE(u64),
230 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_REG_DM_MR_LENGTH,
231 UVERBS_ATTR_TYPE(u64),
233 UVERBS_ATTR_IDR(UVERBS_ATTR_REG_DM_MR_PD_HANDLE,
237 UVERBS_ATTR_FLAGS_IN(UVERBS_ATTR_REG_DM_MR_ACCESS_FLAGS,
238 enum ib_access_flags),
239 UVERBS_ATTR_IDR(UVERBS_ATTR_REG_DM_MR_DM_HANDLE,
243 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_REG_DM_MR_RESP_LKEY,
244 UVERBS_ATTR_TYPE(u32),
246 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_REG_DM_MR_RESP_RKEY,
247 UVERBS_ATTR_TYPE(u32),
250 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
251 UVERBS_METHOD_MR_DESTROY,
252 UVERBS_ATTR_IDR(UVERBS_ATTR_DESTROY_MR_HANDLE,
254 UVERBS_ACCESS_DESTROY,
257 DECLARE_UVERBS_NAMED_OBJECT(
259 UVERBS_TYPE_ALLOC_IDR(uverbs_free_mr),
260 &UVERBS_METHOD(UVERBS_METHOD_DM_MR_REG),
261 &UVERBS_METHOD(UVERBS_METHOD_MR_DESTROY),
262 &UVERBS_METHOD(UVERBS_METHOD_ADVISE_MR),
263 &UVERBS_METHOD(UVERBS_METHOD_QUERY_MR));
265 const struct uapi_definition uverbs_def_obj_mr[] = {
266 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_MR,
267 UAPI_DEF_OBJ_NEEDS_FN(dereg_mr)),