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 
33bee76d7aSAriel Levkovich #include "uverbs.h"
34bee76d7aSAriel Levkovich #include <rdma/uverbs_std_types.h>
35bee76d7aSAriel Levkovich 
36bee76d7aSAriel Levkovich static int uverbs_free_dm(struct ib_uobject *uobject,
37bee76d7aSAriel Levkovich 			  enum rdma_remove_reason why)
38bee76d7aSAriel Levkovich {
39bee76d7aSAriel Levkovich 	struct ib_dm *dm = uobject->object;
401c77483eSYishai Hadas 	int ret;
41bee76d7aSAriel Levkovich 
421c77483eSYishai Hadas 	ret = ib_destroy_usecnt(&dm->usecnt, why, uobject);
431c77483eSYishai Hadas 	if (ret)
441c77483eSYishai Hadas 		return ret;
45bee76d7aSAriel Levkovich 
46bee76d7aSAriel Levkovich 	return dm->device->dealloc_dm(dm);
47bee76d7aSAriel Levkovich }
48bee76d7aSAriel Levkovich 
49e83f0ecdSJason Gunthorpe static int
50e83f0ecdSJason Gunthorpe UVERBS_HANDLER(UVERBS_METHOD_DM_ALLOC)(struct ib_uverbs_file *file,
51bee76d7aSAriel Levkovich 				       struct uverbs_attr_bundle *attrs)
52bee76d7aSAriel Levkovich {
53bee76d7aSAriel Levkovich 	struct ib_dm_alloc_attr attr = {};
54e83f0ecdSJason Gunthorpe 	struct ib_uobject *uobj =
55e83f0ecdSJason Gunthorpe 		uverbs_attr_get(attrs, UVERBS_ATTR_ALLOC_DM_HANDLE)
56e83f0ecdSJason Gunthorpe 			->obj_attr.uobject;
57e83f0ecdSJason Gunthorpe 	struct ib_device *ib_dev = uobj->context->device;
58bee76d7aSAriel Levkovich 	struct ib_dm *dm;
59bee76d7aSAriel Levkovich 	int ret;
60bee76d7aSAriel Levkovich 
61bee76d7aSAriel Levkovich 	if (!ib_dev->alloc_dm)
62bee76d7aSAriel Levkovich 		return -EOPNOTSUPP;
63bee76d7aSAriel Levkovich 
64bee76d7aSAriel Levkovich 	ret = uverbs_copy_from(&attr.length, attrs,
65bee76d7aSAriel Levkovich 			       UVERBS_ATTR_ALLOC_DM_LENGTH);
66bee76d7aSAriel Levkovich 	if (ret)
67bee76d7aSAriel Levkovich 		return ret;
68bee76d7aSAriel Levkovich 
69bee76d7aSAriel Levkovich 	ret = uverbs_copy_from(&attr.alignment, attrs,
70bee76d7aSAriel Levkovich 			       UVERBS_ATTR_ALLOC_DM_ALIGNMENT);
71bee76d7aSAriel Levkovich 	if (ret)
72bee76d7aSAriel Levkovich 		return ret;
73bee76d7aSAriel Levkovich 
7422fa27fbSJason Gunthorpe 	dm = ib_dev->alloc_dm(ib_dev, uobj->context, &attr, attrs);
75bee76d7aSAriel Levkovich 	if (IS_ERR(dm))
76bee76d7aSAriel Levkovich 		return PTR_ERR(dm);
77bee76d7aSAriel Levkovich 
78bee76d7aSAriel Levkovich 	dm->device  = ib_dev;
79bee76d7aSAriel Levkovich 	dm->length  = attr.length;
80bee76d7aSAriel Levkovich 	dm->uobject = uobj;
81bee76d7aSAriel Levkovich 	atomic_set(&dm->usecnt, 0);
82bee76d7aSAriel Levkovich 
83bee76d7aSAriel Levkovich 	uobj->object = dm;
84bee76d7aSAriel Levkovich 
85bee76d7aSAriel Levkovich 	return 0;
86bee76d7aSAriel Levkovich }
87bee76d7aSAriel Levkovich 
889a119cd5SJason Gunthorpe DECLARE_UVERBS_NAMED_METHOD(
899a119cd5SJason Gunthorpe 	UVERBS_METHOD_DM_ALLOC,
909a119cd5SJason Gunthorpe 	UVERBS_ATTR_IDR(UVERBS_ATTR_ALLOC_DM_HANDLE,
919a119cd5SJason Gunthorpe 			UVERBS_OBJECT_DM,
92bee76d7aSAriel Levkovich 			UVERBS_ACCESS_NEW,
9383bb4442SJason Gunthorpe 			UA_MANDATORY),
949a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_ALLOC_DM_LENGTH,
95bee76d7aSAriel Levkovich 			   UVERBS_ATTR_TYPE(u64),
9683bb4442SJason Gunthorpe 			   UA_MANDATORY),
979a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(UVERBS_ATTR_ALLOC_DM_ALIGNMENT,
98bee76d7aSAriel Levkovich 			   UVERBS_ATTR_TYPE(u32),
9983bb4442SJason Gunthorpe 			   UA_MANDATORY));
100bee76d7aSAriel Levkovich 
1019a119cd5SJason Gunthorpe DECLARE_UVERBS_NAMED_METHOD_DESTROY(
1029a119cd5SJason Gunthorpe 	UVERBS_METHOD_DM_FREE,
1039a119cd5SJason Gunthorpe 	UVERBS_ATTR_IDR(UVERBS_ATTR_FREE_DM_HANDLE,
104bee76d7aSAriel Levkovich 			UVERBS_OBJECT_DM,
105bee76d7aSAriel Levkovich 			UVERBS_ACCESS_DESTROY,
10683bb4442SJason Gunthorpe 			UA_MANDATORY));
107bee76d7aSAriel Levkovich 
108bee76d7aSAriel Levkovich DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_DM,
1099a119cd5SJason Gunthorpe 			    UVERBS_TYPE_ALLOC_IDR(uverbs_free_dm),
110bee76d7aSAriel Levkovich 			    &UVERBS_METHOD(UVERBS_METHOD_DM_ALLOC),
111bee76d7aSAriel Levkovich 			    &UVERBS_METHOD(UVERBS_METHOD_DM_FREE));
1120bd01f3dSJason Gunthorpe 
1130bd01f3dSJason Gunthorpe const struct uapi_definition uverbs_def_obj_dm[] = {
1140bd01f3dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_DM,
1150bd01f3dSJason Gunthorpe 				      UAPI_DEF_OBJ_NEEDS_FN(dealloc_dm)),
1160bd01f3dSJason Gunthorpe 	{}
1170bd01f3dSJason Gunthorpe };
118