1bee76d7aSAriel Levkovich /*
2bee76d7aSAriel Levkovich  * Copyright (c) 2018, Mellanox Technologies inc.  All rights reserved.
3bee76d7aSAriel Levkovich  *
4bee76d7aSAriel Levkovich  * This software is available to you under a choice of one of two
5bee76d7aSAriel Levkovich  * licenses.  You may choose to be licensed under the terms of the GNU
6bee76d7aSAriel Levkovich  * General Public License (GPL) Version 2, available from the file
7bee76d7aSAriel Levkovich  * COPYING in the main directory of this source tree, or the
8bee76d7aSAriel Levkovich  * OpenIB.org BSD license below:
9bee76d7aSAriel Levkovich  *
10bee76d7aSAriel Levkovich  *     Redistribution and use in source and binary forms, with or
11bee76d7aSAriel Levkovich  *     without modification, are permitted provided that the following
12bee76d7aSAriel Levkovich  *     conditions are met:
13bee76d7aSAriel Levkovich  *
14bee76d7aSAriel Levkovich  *      - Redistributions of source code must retain the above
15bee76d7aSAriel Levkovich  *        copyright notice, this list of conditions and the following
16bee76d7aSAriel Levkovich  *        disclaimer.
17bee76d7aSAriel Levkovich  *
18bee76d7aSAriel Levkovich  *      - Redistributions in binary form must reproduce the above
19bee76d7aSAriel Levkovich  *        copyright notice, this list of conditions and the following
20bee76d7aSAriel Levkovich  *        disclaimer in the documentation and/or other materials
21bee76d7aSAriel Levkovich  *        provided with the distribution.
22bee76d7aSAriel Levkovich  *
23bee76d7aSAriel Levkovich  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24bee76d7aSAriel Levkovich  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25bee76d7aSAriel Levkovich  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26bee76d7aSAriel Levkovich  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27bee76d7aSAriel Levkovich  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28bee76d7aSAriel Levkovich  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29bee76d7aSAriel Levkovich  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30bee76d7aSAriel Levkovich  * SOFTWARE.
31bee76d7aSAriel Levkovich  */
32bee76d7aSAriel Levkovich 
330080aed4SBart Van Assche #include "rdma_core.h"
34bee76d7aSAriel Levkovich #include "uverbs.h"
35bee76d7aSAriel Levkovich #include <rdma/uverbs_std_types.h>
36bee76d7aSAriel Levkovich 
uverbs_free_dm(struct ib_uobject * uobject,enum rdma_remove_reason why,struct uverbs_attr_bundle * attrs)37bee76d7aSAriel Levkovich static int uverbs_free_dm(struct ib_uobject *uobject,
38a6a3797dSShamir Rabinovitch 			  enum rdma_remove_reason why,
39a6a3797dSShamir Rabinovitch 			  struct uverbs_attr_bundle *attrs)
40bee76d7aSAriel Levkovich {
41bee76d7aSAriel Levkovich 	struct ib_dm *dm = uobject->object;
42bee76d7aSAriel Levkovich 
43*efa968eeSLeon Romanovsky 	if (atomic_read(&dm->usecnt))
44*efa968eeSLeon Romanovsky 		return -EBUSY;
45bee76d7aSAriel Levkovich 
46c4367a26SShamir Rabinovitch 	return dm->device->ops.dealloc_dm(dm, attrs);
47bee76d7aSAriel Levkovich }
48bee76d7aSAriel Levkovich 
UVERBS_HANDLER(UVERBS_METHOD_DM_ALLOC)4915a1b4beSJason Gunthorpe static int UVERBS_HANDLER(UVERBS_METHOD_DM_ALLOC)(
50bee76d7aSAriel Levkovich 	struct uverbs_attr_bundle *attrs)
51bee76d7aSAriel Levkovich {
52bee76d7aSAriel Levkovich 	struct ib_dm_alloc_attr attr = {};
53e83f0ecdSJason Gunthorpe 	struct ib_uobject *uobj =
54e83f0ecdSJason Gunthorpe 		uverbs_attr_get(attrs, UVERBS_ATTR_ALLOC_DM_HANDLE)
55e83f0ecdSJason Gunthorpe 			->obj_attr.uobject;
56feec576aSJason Gunthorpe 	struct ib_device *ib_dev = attrs->context->device;
57bee76d7aSAriel Levkovich 	struct ib_dm *dm;
58bee76d7aSAriel Levkovich 	int ret;
59bee76d7aSAriel Levkovich 
603023a1e9SKamal Heib 	if (!ib_dev->ops.alloc_dm)
61bee76d7aSAriel Levkovich 		return -EOPNOTSUPP;
62bee76d7aSAriel Levkovich 
63bee76d7aSAriel Levkovich 	ret = uverbs_copy_from(&attr.length, attrs,
64bee76d7aSAriel Levkovich 			       UVERBS_ATTR_ALLOC_DM_LENGTH);
65bee76d7aSAriel Levkovich 	if (ret)
66bee76d7aSAriel Levkovich 		return ret;
67bee76d7aSAriel Levkovich 
68bee76d7aSAriel Levkovich 	ret = uverbs_copy_from(&attr.alignment, attrs,
69bee76d7aSAriel Levkovich 			       UVERBS_ATTR_ALLOC_DM_ALIGNMENT);
70bee76d7aSAriel Levkovich 	if (ret)
71bee76d7aSAriel Levkovich 		return ret;
72bee76d7aSAriel Levkovich 
73feec576aSJason Gunthorpe 	dm = ib_dev->ops.alloc_dm(ib_dev, attrs->context, &attr, attrs);
74bee76d7aSAriel Levkovich 	if (IS_ERR(dm))
75bee76d7aSAriel Levkovich 		return PTR_ERR(dm);
76bee76d7aSAriel Levkovich 
77bee76d7aSAriel Levkovich 	dm->device  = ib_dev;
78bee76d7aSAriel Levkovich 	dm->length  = attr.length;
79bee76d7aSAriel Levkovich 	dm->uobject = uobj;
80bee76d7aSAriel Levkovich 	atomic_set(&dm->usecnt, 0);
81bee76d7aSAriel Levkovich 
82bee76d7aSAriel Levkovich 	uobj->object = dm;
83bee76d7aSAriel Levkovich 
84bee76d7aSAriel Levkovich 	return 0;
85bee76d7aSAriel Levkovich }
86bee76d7aSAriel Levkovich 
879a119cd5SJason Gunthorpe DECLARE_UVERBS_NAMED_METHOD(
889a119cd5SJason Gunthorpe 	UVERBS_METHOD_DM_ALLOC,
899a119cd5SJason Gunthorpe 	UVERBS_ATTR_IDR(UVERBS_ATTR_ALLOC_DM_HANDLE,
909a119cd5SJason Gunthorpe 			UVERBS_OBJECT_DM,
91bee76d7aSAriel Levkovich 			UVERBS_ACCESS_NEW,
9283bb4442SJason Gunthorpe 			UA_MANDATORY),
939a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_ALLOC_DM_LENGTH,
94bee76d7aSAriel Levkovich 			   UVERBS_ATTR_TYPE(u64),
9583bb4442SJason Gunthorpe 			   UA_MANDATORY),
969a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_ALLOC_DM_ALIGNMENT,
97bee76d7aSAriel Levkovich 			   UVERBS_ATTR_TYPE(u32),
9883bb4442SJason Gunthorpe 			   UA_MANDATORY));
99bee76d7aSAriel Levkovich 
1009a119cd5SJason Gunthorpe DECLARE_UVERBS_NAMED_METHOD_DESTROY(
1019a119cd5SJason Gunthorpe 	UVERBS_METHOD_DM_FREE,
1029a119cd5SJason Gunthorpe 	UVERBS_ATTR_IDR(UVERBS_ATTR_FREE_DM_HANDLE,
103bee76d7aSAriel Levkovich 			UVERBS_OBJECT_DM,
104bee76d7aSAriel Levkovich 			UVERBS_ACCESS_DESTROY,
10583bb4442SJason Gunthorpe 			UA_MANDATORY));
106bee76d7aSAriel Levkovich 
107bee76d7aSAriel Levkovich DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_DM,
1089a119cd5SJason Gunthorpe 			    UVERBS_TYPE_ALLOC_IDR(uverbs_free_dm),
109bee76d7aSAriel Levkovich 			    &UVERBS_METHOD(UVERBS_METHOD_DM_ALLOC),
110bee76d7aSAriel Levkovich 			    &UVERBS_METHOD(UVERBS_METHOD_DM_FREE));
1110bd01f3dSJason Gunthorpe 
1120bd01f3dSJason Gunthorpe const struct uapi_definition uverbs_def_obj_dm[] = {
1130bd01f3dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_DM,
1140bd01f3dSJason Gunthorpe 				      UAPI_DEF_OBJ_NEEDS_FN(dealloc_dm)),
1150bd01f3dSJason Gunthorpe 	{}
1160bd01f3dSJason Gunthorpe };
117