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 
49bee76d7aSAriel Levkovich static int UVERBS_HANDLER(UVERBS_METHOD_DM_ALLOC)(struct ib_device *ib_dev,
50bee76d7aSAriel Levkovich 						  struct ib_uverbs_file *file,
51bee76d7aSAriel Levkovich 						  struct uverbs_attr_bundle *attrs)
52bee76d7aSAriel Levkovich {
53bee76d7aSAriel Levkovich 	struct ib_ucontext *ucontext = file->ucontext;
54bee76d7aSAriel Levkovich 	struct ib_dm_alloc_attr attr = {};
55bee76d7aSAriel Levkovich 	struct ib_uobject *uobj;
56bee76d7aSAriel Levkovich 	struct ib_dm *dm;
57bee76d7aSAriel Levkovich 	int ret;
58bee76d7aSAriel Levkovich 
59bee76d7aSAriel Levkovich 	if (!ib_dev->alloc_dm)
60bee76d7aSAriel Levkovich 		return -EOPNOTSUPP;
61bee76d7aSAriel Levkovich 
62bee76d7aSAriel Levkovich 	ret = uverbs_copy_from(&attr.length, attrs,
63bee76d7aSAriel Levkovich 			       UVERBS_ATTR_ALLOC_DM_LENGTH);
64bee76d7aSAriel Levkovich 	if (ret)
65bee76d7aSAriel Levkovich 		return ret;
66bee76d7aSAriel Levkovich 
67bee76d7aSAriel Levkovich 	ret = uverbs_copy_from(&attr.alignment, attrs,
68bee76d7aSAriel Levkovich 			       UVERBS_ATTR_ALLOC_DM_ALIGNMENT);
69bee76d7aSAriel Levkovich 	if (ret)
70bee76d7aSAriel Levkovich 		return ret;
71bee76d7aSAriel Levkovich 
72bee76d7aSAriel Levkovich 	uobj = uverbs_attr_get(attrs, UVERBS_ATTR_ALLOC_DM_HANDLE)->obj_attr.uobject;
73bee76d7aSAriel Levkovich 
74bee76d7aSAriel Levkovich 	dm = ib_dev->alloc_dm(ib_dev, ucontext, &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 
88bee76d7aSAriel Levkovich static DECLARE_UVERBS_NAMED_METHOD(UVERBS_METHOD_DM_ALLOC,
89bee76d7aSAriel Levkovich 	&UVERBS_ATTR_IDR(UVERBS_ATTR_ALLOC_DM_HANDLE, UVERBS_OBJECT_DM,
90bee76d7aSAriel Levkovich 			 UVERBS_ACCESS_NEW,
91bee76d7aSAriel Levkovich 			 UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
92bee76d7aSAriel Levkovich 	&UVERBS_ATTR_PTR_IN(UVERBS_ATTR_ALLOC_DM_LENGTH,
93bee76d7aSAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
94bee76d7aSAriel Levkovich 			    UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
95bee76d7aSAriel Levkovich 	&UVERBS_ATTR_PTR_IN(UVERBS_ATTR_ALLOC_DM_ALIGNMENT,
96bee76d7aSAriel Levkovich 			    UVERBS_ATTR_TYPE(u32),
97bee76d7aSAriel Levkovich 			    UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)));
98bee76d7aSAriel Levkovich 
99bee76d7aSAriel Levkovich static DECLARE_UVERBS_NAMED_METHOD_WITH_HANDLER(UVERBS_METHOD_DM_FREE,
100bee76d7aSAriel Levkovich 	uverbs_destroy_def_handler,
101bee76d7aSAriel Levkovich 	&UVERBS_ATTR_IDR(UVERBS_ATTR_FREE_DM_HANDLE,
102bee76d7aSAriel Levkovich 			 UVERBS_OBJECT_DM,
103bee76d7aSAriel Levkovich 			 UVERBS_ACCESS_DESTROY,
104bee76d7aSAriel Levkovich 			 UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)));
105bee76d7aSAriel Levkovich 
106bee76d7aSAriel Levkovich DECLARE_UVERBS_NAMED_OBJECT(UVERBS_OBJECT_DM,
1071c77483eSYishai Hadas 			    &UVERBS_TYPE_ALLOC_IDR(uverbs_free_dm),
108bee76d7aSAriel Levkovich 			    &UVERBS_METHOD(UVERBS_METHOD_DM_ALLOC),
109bee76d7aSAriel Levkovich 			    &UVERBS_METHOD(UVERBS_METHOD_DM_FREE));
110