1 /*
2  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
4  * Copyright (c) 2005-2017 Mellanox Technologies. All rights reserved.
5  * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
6  * Copyright (c) 2005 PathScale, Inc. All rights reserved.
7  *
8  * This software is available to you under a choice of one of two
9  * licenses.  You may choose to be licensed under the terms of the GNU
10  * General Public License (GPL) Version 2, available from the file
11  * COPYING in the main directory of this source tree, or the
12  * OpenIB.org BSD license below:
13  *
14  *     Redistribution and use in source and binary forms, with or
15  *     without modification, are permitted provided that the following
16  *     conditions are met:
17  *
18  *      - Redistributions of source code must retain the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer.
21  *
22  *      - Redistributions in binary form must reproduce the above
23  *        copyright notice, this list of conditions and the following
24  *        disclaimer in the documentation and/or other materials
25  *        provided with the distribution.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34  * SOFTWARE.
35  */
36 
37 #ifndef RDMA_CORE_H
38 #define RDMA_CORE_H
39 
40 #include <linux/idr.h>
41 #include <rdma/uverbs_types.h>
42 #include <rdma/uverbs_ioctl.h>
43 #include <rdma/ib_verbs.h>
44 #include <linux/mutex.h>
45 
46 struct ib_uverbs_device;
47 
48 void uverbs_destroy_ufile_hw(struct ib_uverbs_file *ufile,
49 			     enum rdma_remove_reason reason);
50 
51 int uobj_destroy(struct ib_uobject *uobj, struct uverbs_attr_bundle *attrs);
52 
53 /*
54  * uverbs_uobject_get is called in order to increase the reference count on
55  * an uobject. This is useful when a handler wants to keep the uobject's memory
56  * alive, regardless if this uobject is still alive in the context's objects
57  * repository. Objects are put via uverbs_uobject_put.
58  */
59 void uverbs_uobject_get(struct ib_uobject *uobject);
60 
61 /*
62  * In order to indicate we no longer needs this uobject, uverbs_uobject_put
63  * is called. When the reference count is decreased, the uobject is freed.
64  * For example, this is used when attaching a completion channel to a CQ.
65  */
66 void uverbs_uobject_put(struct ib_uobject *uobject);
67 
68 /* Indicate this fd is no longer used by this consumer, but its memory isn't
69  * necessarily released yet. When the last reference is put, we release the
70  * memory. After this call is executed, calling uverbs_uobject_get isn't
71  * allowed.
72  * This must be called from the release file_operations of the file!
73  */
74 void uverbs_close_fd(struct file *f);
75 
76 /*
77  * Get an ib_uobject that corresponds to the given id from ufile, assuming
78  * the object is from the given type. Lock it to the required access when
79  * applicable.
80  * This function could create (access == NEW), destroy (access == DESTROY)
81  * or unlock (access == READ || access == WRITE) objects if required.
82  * The action will be finalized only when uverbs_finalize_object or
83  * uverbs_finalize_objects are called.
84  */
85 struct ib_uobject *
86 uverbs_get_uobject_from_file(u16 object_id, enum uverbs_obj_access access,
87 			     s64 id, struct uverbs_attr_bundle *attrs);
88 
89 /*
90  * Note that certain finalize stages could return a status:
91  *   (a) alloc_commit could return a failure if the object is committed at the
92  *       same time when the context is destroyed.
93  *   (b) remove_commit could fail if the object wasn't destroyed successfully.
94  * Since multiple objects could be finalized in one transaction, it is very NOT
95  * recommended to have several finalize actions which have side effects.
96  * For example, it's NOT recommended to have a certain action which has both
97  * a commit action and a destroy action or two destroy objects in the same
98  * action. The rule of thumb is to have one destroy or commit action with
99  * multiple lookups.
100  * The first non zero return value of finalize_object is returned from this
101  * function. For example, this could happen when we couldn't destroy an
102  * object.
103  */
104 int uverbs_finalize_object(struct ib_uobject *uobj,
105 			   enum uverbs_obj_access access, bool commit,
106 			   struct uverbs_attr_bundle *attrs);
107 
108 int uverbs_output_written(const struct uverbs_attr_bundle *bundle, size_t idx);
109 
110 void setup_ufile_idr_uobject(struct ib_uverbs_file *ufile);
111 void release_ufile_idr_uobject(struct ib_uverbs_file *ufile);
112 
113 /*
114  * This is the runtime description of the uverbs API, used by the syscall
115  * machinery to validate and dispatch calls.
116  */
117 
118 /*
119  * Depending on ID the slot pointer in the radix tree points at one of these
120  * structs.
121  */
122 
123 struct uverbs_api_ioctl_method {
124 	int(__rcu *handler)(struct uverbs_attr_bundle *attrs);
125 	DECLARE_BITMAP(attr_mandatory, UVERBS_API_ATTR_BKEY_LEN);
126 	u16 bundle_size;
127 	u8 use_stack:1;
128 	u8 driver_method:1;
129 	u8 disabled:1;
130 	u8 has_udata:1;
131 	u8 key_bitmap_len;
132 	u8 destroy_bkey;
133 };
134 
135 struct uverbs_api_write_method {
136 	int (*handler)(struct uverbs_attr_bundle *attrs);
137 	u8 disabled:1;
138 	u8 is_ex:1;
139 	u8 has_udata:1;
140 	u8 has_resp:1;
141 	u8 req_size;
142 	u8 resp_size;
143 };
144 
145 struct uverbs_api_attr {
146 	struct uverbs_attr_spec spec;
147 };
148 
149 struct uverbs_api {
150 	/* radix tree contains struct uverbs_api_* pointers */
151 	struct radix_tree_root radix;
152 	enum rdma_driver_id driver_id;
153 
154 	unsigned int num_write;
155 	unsigned int num_write_ex;
156 	struct uverbs_api_write_method notsupp_method;
157 	const struct uverbs_api_write_method **write_methods;
158 	const struct uverbs_api_write_method **write_ex_methods;
159 };
160 
161 /*
162  * Get an uverbs_api_object that corresponds to the given object_id.
163  * Note:
164  * -ENOMSG means that any object is allowed to match during lookup.
165  */
166 static inline const struct uverbs_api_object *
167 uapi_get_object(struct uverbs_api *uapi, u16 object_id)
168 {
169 	const struct uverbs_api_object *res;
170 
171 	if (object_id == UVERBS_IDR_ANY_OBJECT)
172 		return ERR_PTR(-ENOMSG);
173 
174 	res = radix_tree_lookup(&uapi->radix, uapi_key_obj(object_id));
175 	if (!res)
176 		return ERR_PTR(-ENOENT);
177 
178 	return res;
179 }
180 
181 char *uapi_key_format(char *S, unsigned int key);
182 struct uverbs_api *uverbs_alloc_api(struct ib_device *ibdev);
183 void uverbs_disassociate_api_pre(struct ib_uverbs_device *uverbs_dev);
184 void uverbs_disassociate_api(struct uverbs_api *uapi);
185 void uverbs_destroy_api(struct uverbs_api *uapi);
186 void uapi_compute_bundle_size(struct uverbs_api_ioctl_method *method_elm,
187 			      unsigned int num_attrs);
188 void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile);
189 
190 extern const struct uapi_definition uverbs_def_obj_counters[];
191 extern const struct uapi_definition uverbs_def_obj_cq[];
192 extern const struct uapi_definition uverbs_def_obj_device[];
193 extern const struct uapi_definition uverbs_def_obj_dm[];
194 extern const struct uapi_definition uverbs_def_obj_flow_action[];
195 extern const struct uapi_definition uverbs_def_obj_intf[];
196 extern const struct uapi_definition uverbs_def_obj_mr[];
197 extern const struct uapi_definition uverbs_def_write_intf[];
198 
199 static inline const struct uverbs_api_write_method *
200 uapi_get_method(const struct uverbs_api *uapi, u32 command)
201 {
202 	u32 cmd_idx = command & IB_USER_VERBS_CMD_COMMAND_MASK;
203 
204 	if (command & ~(u32)(IB_USER_VERBS_CMD_FLAG_EXTENDED |
205 			     IB_USER_VERBS_CMD_COMMAND_MASK))
206 		return ERR_PTR(-EINVAL);
207 
208 	if (command & IB_USER_VERBS_CMD_FLAG_EXTENDED) {
209 		if (cmd_idx >= uapi->num_write_ex)
210 			return ERR_PTR(-EOPNOTSUPP);
211 		return uapi->write_ex_methods[cmd_idx];
212 	}
213 
214 	if (cmd_idx >= uapi->num_write)
215 		return ERR_PTR(-EOPNOTSUPP);
216 	return uapi->write_methods[cmd_idx];
217 }
218 
219 void uverbs_fill_udata(struct uverbs_attr_bundle *bundle,
220 		       struct ib_udata *udata, unsigned int attr_in,
221 		       unsigned int attr_out);
222 
223 #endif /* RDMA_CORE_H */
224