xref: /linux/drivers/infiniband/core/uverbs_cmd.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1 /*
2  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005, 2006, 2007 Cisco Systems.  All rights reserved.
4  * Copyright (c) 2005 PathScale, Inc.  All rights reserved.
5  * Copyright (c) 2006 Mellanox Technologies.  All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  */
35 
36 #include <linux/file.h>
37 #include <linux/fs.h>
38 #include <linux/slab.h>
39 #include <linux/sched.h>
40 
41 #include <linux/uaccess.h>
42 
43 #include <rdma/uverbs_types.h>
44 #include <rdma/uverbs_std_types.h>
45 #include <rdma/ib_ucaps.h>
46 #include "rdma_core.h"
47 
48 #include "uverbs.h"
49 #include "core_priv.h"
50 
51 /*
52  * Copy a response to userspace. If the provided 'resp' is larger than the
53  * user buffer it is silently truncated. If the user provided a larger buffer
54  * then the trailing portion is zero filled.
55  *
56  * These semantics are intended to support future extension of the output
57  * structures.
58  */
uverbs_response(struct uverbs_attr_bundle * attrs,const void * resp,size_t resp_len)59 static int uverbs_response(struct uverbs_attr_bundle *attrs, const void *resp,
60 			   size_t resp_len)
61 {
62 	int ret;
63 
64 	if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CORE_OUT))
65 		return uverbs_copy_to_struct_or_zero(
66 			attrs, UVERBS_ATTR_CORE_OUT, resp, resp_len);
67 
68 	if (copy_to_user(attrs->ucore.outbuf, resp,
69 			 min(attrs->ucore.outlen, resp_len)))
70 		return -EFAULT;
71 
72 	if (resp_len < attrs->ucore.outlen) {
73 		/*
74 		 * Zero fill any extra memory that user
75 		 * space might have provided.
76 		 */
77 		ret = clear_user(attrs->ucore.outbuf + resp_len,
78 				 attrs->ucore.outlen - resp_len);
79 		if (ret)
80 			return -EFAULT;
81 	}
82 
83 	return 0;
84 }
85 
86 /*
87  * Copy a request from userspace. If the provided 'req' is larger than the
88  * user buffer then the user buffer is zero extended into the 'req'. If 'req'
89  * is smaller than the user buffer then the uncopied bytes in the user buffer
90  * must be zero.
91  */
uverbs_request(struct uverbs_attr_bundle * attrs,void * req,size_t req_len)92 static int uverbs_request(struct uverbs_attr_bundle *attrs, void *req,
93 			  size_t req_len)
94 {
95 	if (copy_from_user(req, attrs->ucore.inbuf,
96 			   min(attrs->ucore.inlen, req_len)))
97 		return -EFAULT;
98 
99 	if (attrs->ucore.inlen < req_len) {
100 		memset(req + attrs->ucore.inlen, 0,
101 		       req_len - attrs->ucore.inlen);
102 	} else if (attrs->ucore.inlen > req_len) {
103 		if (!ib_is_buffer_cleared(attrs->ucore.inbuf + req_len,
104 					  attrs->ucore.inlen - req_len))
105 			return -EOPNOTSUPP;
106 	}
107 	return 0;
108 }
109 
110 /*
111  * Generate the value for the 'response_length' protocol used by write_ex.
112  * This is the number of bytes the kernel actually wrote. Userspace can use
113  * this to detect what structure members in the response the kernel
114  * understood.
115  */
uverbs_response_length(struct uverbs_attr_bundle * attrs,size_t resp_len)116 static u32 uverbs_response_length(struct uverbs_attr_bundle *attrs,
117 				  size_t resp_len)
118 {
119 	return min_t(size_t, attrs->ucore.outlen, resp_len);
120 }
121 
122 /*
123  * The iterator version of the request interface is for handlers that need to
124  * step over a flex array at the end of a command header.
125  */
126 struct uverbs_req_iter {
127 	const void __user *cur;
128 	const void __user *end;
129 };
130 
uverbs_request_start(struct uverbs_attr_bundle * attrs,struct uverbs_req_iter * iter,void * req,size_t req_len)131 static int uverbs_request_start(struct uverbs_attr_bundle *attrs,
132 				struct uverbs_req_iter *iter,
133 				void *req,
134 				size_t req_len)
135 {
136 	if (attrs->ucore.inlen < req_len)
137 		return -ENOSPC;
138 
139 	if (copy_from_user(req, attrs->ucore.inbuf, req_len))
140 		return -EFAULT;
141 
142 	iter->cur = attrs->ucore.inbuf + req_len;
143 	iter->end = attrs->ucore.inbuf + attrs->ucore.inlen;
144 	return 0;
145 }
146 
uverbs_request_next(struct uverbs_req_iter * iter,void * val,size_t len)147 static int uverbs_request_next(struct uverbs_req_iter *iter, void *val,
148 			       size_t len)
149 {
150 	if (iter->cur + len > iter->end)
151 		return -ENOSPC;
152 
153 	if (copy_from_user(val, iter->cur, len))
154 		return -EFAULT;
155 
156 	iter->cur += len;
157 	return 0;
158 }
159 
uverbs_request_next_ptr(struct uverbs_req_iter * iter,size_t len)160 static const void __user *uverbs_request_next_ptr(struct uverbs_req_iter *iter,
161 						  size_t len)
162 {
163 	const void __user *res = iter->cur;
164 
165 	if (len > iter->end - iter->cur)
166 		return (void __force __user *)ERR_PTR(-ENOSPC);
167 	iter->cur += len;
168 	return res;
169 }
170 
uverbs_request_finish(struct uverbs_req_iter * iter)171 static int uverbs_request_finish(struct uverbs_req_iter *iter)
172 {
173 	if (!ib_is_buffer_cleared(iter->cur, iter->end - iter->cur))
174 		return -EOPNOTSUPP;
175 	return 0;
176 }
177 
178 /*
179  * When calling a destroy function during an error unwind we need to pass in
180  * the udata that is sanitized of all user arguments. Ie from the driver
181  * perspective it looks like no udata was passed.
182  */
uverbs_get_cleared_udata(struct uverbs_attr_bundle * attrs)183 struct ib_udata *uverbs_get_cleared_udata(struct uverbs_attr_bundle *attrs)
184 {
185 	attrs->driver_udata = (struct ib_udata){};
186 	return &attrs->driver_udata;
187 }
188 
189 static struct ib_uverbs_completion_event_file *
_ib_uverbs_lookup_comp_file(s32 fd,struct uverbs_attr_bundle * attrs)190 _ib_uverbs_lookup_comp_file(s32 fd, struct uverbs_attr_bundle *attrs)
191 {
192 	struct ib_uobject *uobj = ufd_get_read(UVERBS_OBJECT_COMP_CHANNEL,
193 					       fd, attrs);
194 
195 	if (IS_ERR(uobj))
196 		return ERR_CAST(uobj);
197 
198 	uverbs_uobject_get(uobj);
199 	uobj_put_read(uobj);
200 
201 	return container_of(uobj, struct ib_uverbs_completion_event_file,
202 			    uobj);
203 }
204 #define ib_uverbs_lookup_comp_file(_fd, _ufile)                                \
205 	_ib_uverbs_lookup_comp_file((_fd)*typecheck(s32, _fd), _ufile)
206 
ib_alloc_ucontext(struct uverbs_attr_bundle * attrs)207 int ib_alloc_ucontext(struct uverbs_attr_bundle *attrs)
208 {
209 	struct ib_uverbs_file *ufile = attrs->ufile;
210 	struct ib_ucontext *ucontext;
211 	struct ib_device *ib_dev;
212 
213 	ib_dev = srcu_dereference(ufile->device->ib_dev,
214 				  &ufile->device->disassociate_srcu);
215 	if (!ib_dev)
216 		return -EIO;
217 
218 	ucontext = rdma_zalloc_drv_obj(ib_dev, ib_ucontext);
219 	if (!ucontext)
220 		return -ENOMEM;
221 
222 	ucontext->device = ib_dev;
223 	ucontext->ufile = ufile;
224 	xa_init_flags(&ucontext->mmap_xa, XA_FLAGS_ALLOC);
225 
226 	rdma_restrack_new(&ucontext->res, RDMA_RESTRACK_CTX);
227 	rdma_restrack_set_name(&ucontext->res, NULL);
228 	attrs->context = ucontext;
229 	return 0;
230 }
231 
ib_init_ucontext(struct uverbs_attr_bundle * attrs)232 int ib_init_ucontext(struct uverbs_attr_bundle *attrs)
233 {
234 	struct ib_ucontext *ucontext = attrs->context;
235 	struct ib_uverbs_file *file = attrs->ufile;
236 	int *fd_array;
237 	int fd_count;
238 	int ret;
239 
240 	if (!down_read_trylock(&file->hw_destroy_rwsem))
241 		return -EIO;
242 	mutex_lock(&file->ucontext_lock);
243 	if (file->ucontext) {
244 		ret = -EINVAL;
245 		goto err;
246 	}
247 
248 	ret = ib_rdmacg_try_charge(&ucontext->cg_obj, ucontext->device,
249 				   RDMACG_RESOURCE_HCA_HANDLE);
250 	if (ret)
251 		goto err;
252 
253 	if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_GET_CONTEXT_FD_ARR)) {
254 		fd_count = uverbs_attr_ptr_get_array_size(attrs,
255 							  UVERBS_ATTR_GET_CONTEXT_FD_ARR,
256 							  sizeof(int));
257 		if (fd_count < 0) {
258 			ret = fd_count;
259 			goto err_uncharge;
260 		}
261 
262 		fd_array = uverbs_attr_get_alloced_ptr(attrs,
263 						       UVERBS_ATTR_GET_CONTEXT_FD_ARR);
264 		ret = ib_get_ucaps(fd_array, fd_count, &ucontext->enabled_caps);
265 		if (ret)
266 			goto err_uncharge;
267 	}
268 
269 	ret = ucontext->device->ops.alloc_ucontext(ucontext,
270 						   &attrs->driver_udata);
271 	if (ret)
272 		goto err_uncharge;
273 
274 	rdma_restrack_add(&ucontext->res);
275 
276 	/*
277 	 * Make sure that ib_uverbs_get_ucontext() sees the pointer update
278 	 * only after all writes to setup the ucontext have completed
279 	 */
280 	smp_store_release(&file->ucontext, ucontext);
281 
282 	mutex_unlock(&file->ucontext_lock);
283 	up_read(&file->hw_destroy_rwsem);
284 	return 0;
285 
286 err_uncharge:
287 	ib_rdmacg_uncharge(&ucontext->cg_obj, ucontext->device,
288 			   RDMACG_RESOURCE_HCA_HANDLE);
289 err:
290 	mutex_unlock(&file->ucontext_lock);
291 	up_read(&file->hw_destroy_rwsem);
292 	return ret;
293 }
294 
ib_uverbs_get_context(struct uverbs_attr_bundle * attrs)295 static int ib_uverbs_get_context(struct uverbs_attr_bundle *attrs)
296 {
297 	struct ib_uverbs_get_context_resp resp;
298 	struct ib_uverbs_get_context cmd;
299 	struct ib_device *ib_dev;
300 	struct ib_uobject *uobj;
301 	int ret;
302 
303 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
304 	if (ret)
305 		return ret;
306 
307 	ret = ib_alloc_ucontext(attrs);
308 	if (ret)
309 		return ret;
310 
311 	uobj = uobj_alloc(UVERBS_OBJECT_ASYNC_EVENT, attrs, &ib_dev);
312 	if (IS_ERR(uobj)) {
313 		ret = PTR_ERR(uobj);
314 		goto err_ucontext;
315 	}
316 
317 	resp = (struct ib_uverbs_get_context_resp){
318 		.num_comp_vectors = attrs->ufile->device->num_comp_vectors,
319 		.async_fd = uobj->id,
320 	};
321 	ret = uverbs_response(attrs, &resp, sizeof(resp));
322 	if (ret)
323 		goto err_uobj;
324 
325 	ret = ib_init_ucontext(attrs);
326 	if (ret)
327 		goto err_uobj;
328 
329 	ib_uverbs_init_async_event_file(
330 		container_of(uobj, struct ib_uverbs_async_event_file, uobj));
331 	rdma_alloc_commit_uobject(uobj, attrs);
332 	return 0;
333 
334 err_uobj:
335 	rdma_alloc_abort_uobject(uobj, attrs, false);
336 err_ucontext:
337 	rdma_restrack_put(&attrs->context->res);
338 	kfree(attrs->context);
339 	attrs->context = NULL;
340 	return ret;
341 }
342 
copy_query_dev_fields(struct ib_ucontext * ucontext,struct ib_uverbs_query_device_resp * resp,struct ib_device_attr * attr)343 static void copy_query_dev_fields(struct ib_ucontext *ucontext,
344 				  struct ib_uverbs_query_device_resp *resp,
345 				  struct ib_device_attr *attr)
346 {
347 	struct ib_device *ib_dev = ucontext->device;
348 
349 	resp->fw_ver		= attr->fw_ver;
350 	resp->node_guid		= ib_dev->node_guid;
351 	resp->sys_image_guid	= attr->sys_image_guid;
352 	resp->max_mr_size	= attr->max_mr_size;
353 	resp->page_size_cap	= attr->page_size_cap;
354 	resp->vendor_id		= attr->vendor_id;
355 	resp->vendor_part_id	= attr->vendor_part_id;
356 	resp->hw_ver		= attr->hw_ver;
357 	resp->max_qp		= attr->max_qp;
358 	resp->max_qp_wr		= attr->max_qp_wr;
359 	resp->device_cap_flags  = lower_32_bits(attr->device_cap_flags);
360 	resp->max_sge		= min(attr->max_send_sge, attr->max_recv_sge);
361 	resp->max_sge_rd	= attr->max_sge_rd;
362 	resp->max_cq		= attr->max_cq;
363 	resp->max_cqe		= attr->max_cqe;
364 	resp->max_mr		= attr->max_mr;
365 	resp->max_pd		= attr->max_pd;
366 	resp->max_qp_rd_atom	= attr->max_qp_rd_atom;
367 	resp->max_ee_rd_atom	= attr->max_ee_rd_atom;
368 	resp->max_res_rd_atom	= attr->max_res_rd_atom;
369 	resp->max_qp_init_rd_atom	= attr->max_qp_init_rd_atom;
370 	resp->max_ee_init_rd_atom	= attr->max_ee_init_rd_atom;
371 	resp->atomic_cap		= attr->atomic_cap;
372 	resp->max_ee			= attr->max_ee;
373 	resp->max_rdd			= attr->max_rdd;
374 	resp->max_mw			= attr->max_mw;
375 	resp->max_raw_ipv6_qp		= attr->max_raw_ipv6_qp;
376 	resp->max_raw_ethy_qp		= attr->max_raw_ethy_qp;
377 	resp->max_mcast_grp		= attr->max_mcast_grp;
378 	resp->max_mcast_qp_attach	= attr->max_mcast_qp_attach;
379 	resp->max_total_mcast_qp_attach	= attr->max_total_mcast_qp_attach;
380 	resp->max_ah			= attr->max_ah;
381 	resp->max_srq			= attr->max_srq;
382 	resp->max_srq_wr		= attr->max_srq_wr;
383 	resp->max_srq_sge		= attr->max_srq_sge;
384 	resp->max_pkeys			= attr->max_pkeys;
385 	resp->local_ca_ack_delay	= attr->local_ca_ack_delay;
386 	resp->phys_port_cnt = min_t(u32, ib_dev->phys_port_cnt, U8_MAX);
387 }
388 
ib_uverbs_query_device(struct uverbs_attr_bundle * attrs)389 static int ib_uverbs_query_device(struct uverbs_attr_bundle *attrs)
390 {
391 	struct ib_uverbs_query_device      cmd;
392 	struct ib_uverbs_query_device_resp resp;
393 	struct ib_ucontext *ucontext;
394 	int ret;
395 
396 	ucontext = ib_uverbs_get_ucontext(attrs);
397 	if (IS_ERR(ucontext))
398 		return PTR_ERR(ucontext);
399 
400 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
401 	if (ret)
402 		return ret;
403 
404 	memset(&resp, 0, sizeof resp);
405 	copy_query_dev_fields(ucontext, &resp, &ucontext->device->attrs);
406 
407 	return uverbs_response(attrs, &resp, sizeof(resp));
408 }
409 
ib_uverbs_query_port(struct uverbs_attr_bundle * attrs)410 static int ib_uverbs_query_port(struct uverbs_attr_bundle *attrs)
411 {
412 	struct ib_uverbs_query_port      cmd;
413 	struct ib_uverbs_query_port_resp resp;
414 	struct ib_port_attr              attr;
415 	int                              ret;
416 	struct ib_ucontext *ucontext;
417 	struct ib_device *ib_dev;
418 
419 	ucontext = ib_uverbs_get_ucontext(attrs);
420 	if (IS_ERR(ucontext))
421 		return PTR_ERR(ucontext);
422 	ib_dev = ucontext->device;
423 
424 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
425 	if (ret)
426 		return ret;
427 
428 	ret = ib_query_port(ib_dev, cmd.port_num, &attr);
429 	if (ret)
430 		return ret;
431 
432 	memset(&resp, 0, sizeof resp);
433 	copy_port_attr_to_resp(&attr, &resp, ib_dev, cmd.port_num);
434 
435 	return uverbs_response(attrs, &resp, sizeof(resp));
436 }
437 
ib_uverbs_alloc_pd(struct uverbs_attr_bundle * attrs)438 static int ib_uverbs_alloc_pd(struct uverbs_attr_bundle *attrs)
439 {
440 	struct ib_uverbs_alloc_pd_resp resp = {};
441 	struct ib_uverbs_alloc_pd      cmd;
442 	struct ib_uobject             *uobj;
443 	struct ib_pd                  *pd;
444 	int                            ret;
445 	struct ib_device *ib_dev;
446 
447 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
448 	if (ret)
449 		return ret;
450 
451 	uobj = uobj_alloc(UVERBS_OBJECT_PD, attrs, &ib_dev);
452 	if (IS_ERR(uobj))
453 		return PTR_ERR(uobj);
454 
455 	pd = rdma_zalloc_drv_obj(ib_dev, ib_pd);
456 	if (!pd) {
457 		ret = -ENOMEM;
458 		goto err;
459 	}
460 
461 	pd->device  = ib_dev;
462 	pd->uobject = uobj;
463 	atomic_set(&pd->usecnt, 0);
464 
465 	rdma_restrack_new(&pd->res, RDMA_RESTRACK_PD);
466 	rdma_restrack_set_name(&pd->res, NULL);
467 
468 	ret = ib_dev->ops.alloc_pd(pd, &attrs->driver_udata);
469 	if (ret)
470 		goto err_alloc;
471 	rdma_restrack_add(&pd->res);
472 
473 	uobj->object = pd;
474 	uobj_finalize_uobj_create(uobj, attrs);
475 
476 	resp.pd_handle = uobj->id;
477 	return uverbs_response(attrs, &resp, sizeof(resp));
478 
479 err_alloc:
480 	rdma_restrack_put(&pd->res);
481 	kfree(pd);
482 err:
483 	uobj_alloc_abort(uobj, attrs);
484 	return ret;
485 }
486 
ib_uverbs_dealloc_pd(struct uverbs_attr_bundle * attrs)487 static int ib_uverbs_dealloc_pd(struct uverbs_attr_bundle *attrs)
488 {
489 	struct ib_uverbs_dealloc_pd cmd;
490 	int ret;
491 
492 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
493 	if (ret)
494 		return ret;
495 
496 	return uobj_perform_destroy(UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
497 }
498 
499 struct xrcd_table_entry {
500 	struct rb_node  node;
501 	struct ib_xrcd *xrcd;
502 	struct inode   *inode;
503 };
504 
xrcd_table_insert(struct ib_uverbs_device * dev,struct inode * inode,struct ib_xrcd * xrcd)505 static int xrcd_table_insert(struct ib_uverbs_device *dev,
506 			    struct inode *inode,
507 			    struct ib_xrcd *xrcd)
508 {
509 	struct xrcd_table_entry *entry, *scan;
510 	struct rb_node **p = &dev->xrcd_tree.rb_node;
511 	struct rb_node *parent = NULL;
512 
513 	entry = kmalloc(sizeof *entry, GFP_KERNEL);
514 	if (!entry)
515 		return -ENOMEM;
516 
517 	entry->xrcd  = xrcd;
518 	entry->inode = inode;
519 
520 	while (*p) {
521 		parent = *p;
522 		scan = rb_entry(parent, struct xrcd_table_entry, node);
523 
524 		if (inode < scan->inode) {
525 			p = &(*p)->rb_left;
526 		} else if (inode > scan->inode) {
527 			p = &(*p)->rb_right;
528 		} else {
529 			kfree(entry);
530 			return -EEXIST;
531 		}
532 	}
533 
534 	rb_link_node(&entry->node, parent, p);
535 	rb_insert_color(&entry->node, &dev->xrcd_tree);
536 	igrab(inode);
537 	return 0;
538 }
539 
xrcd_table_search(struct ib_uverbs_device * dev,struct inode * inode)540 static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
541 						  struct inode *inode)
542 {
543 	struct xrcd_table_entry *entry;
544 	struct rb_node *p = dev->xrcd_tree.rb_node;
545 
546 	while (p) {
547 		entry = rb_entry(p, struct xrcd_table_entry, node);
548 
549 		if (inode < entry->inode)
550 			p = p->rb_left;
551 		else if (inode > entry->inode)
552 			p = p->rb_right;
553 		else
554 			return entry;
555 	}
556 
557 	return NULL;
558 }
559 
find_xrcd(struct ib_uverbs_device * dev,struct inode * inode)560 static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
561 {
562 	struct xrcd_table_entry *entry;
563 
564 	entry = xrcd_table_search(dev, inode);
565 	if (!entry)
566 		return NULL;
567 
568 	return entry->xrcd;
569 }
570 
xrcd_table_delete(struct ib_uverbs_device * dev,struct inode * inode)571 static void xrcd_table_delete(struct ib_uverbs_device *dev,
572 			      struct inode *inode)
573 {
574 	struct xrcd_table_entry *entry;
575 
576 	entry = xrcd_table_search(dev, inode);
577 	if (entry) {
578 		iput(inode);
579 		rb_erase(&entry->node, &dev->xrcd_tree);
580 		kfree(entry);
581 	}
582 }
583 
ib_uverbs_open_xrcd(struct uverbs_attr_bundle * attrs)584 static int ib_uverbs_open_xrcd(struct uverbs_attr_bundle *attrs)
585 {
586 	struct ib_uverbs_device *ibudev = attrs->ufile->device;
587 	struct ib_uverbs_open_xrcd_resp	resp = {};
588 	struct ib_uverbs_open_xrcd	cmd;
589 	struct ib_uxrcd_object         *obj;
590 	struct ib_xrcd                 *xrcd = NULL;
591 	struct inode                   *inode = NULL;
592 	int				new_xrcd = 0;
593 	struct ib_device *ib_dev;
594 	struct fd f = EMPTY_FD;
595 	int ret;
596 
597 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
598 	if (ret)
599 		return ret;
600 
601 	mutex_lock(&ibudev->xrcd_tree_mutex);
602 
603 	if (cmd.fd != -1) {
604 		/* search for file descriptor */
605 		f = fdget(cmd.fd);
606 		if (fd_empty(f)) {
607 			ret = -EBADF;
608 			goto err_tree_mutex_unlock;
609 		}
610 
611 		inode = file_inode(fd_file(f));
612 		xrcd = find_xrcd(ibudev, inode);
613 		if (!xrcd && !(cmd.oflags & O_CREAT)) {
614 			/* no file descriptor. Need CREATE flag */
615 			ret = -EAGAIN;
616 			goto err_tree_mutex_unlock;
617 		}
618 
619 		if (xrcd && cmd.oflags & O_EXCL) {
620 			ret = -EINVAL;
621 			goto err_tree_mutex_unlock;
622 		}
623 	}
624 
625 	obj = (struct ib_uxrcd_object *)uobj_alloc(UVERBS_OBJECT_XRCD, attrs,
626 						   &ib_dev);
627 	if (IS_ERR(obj)) {
628 		ret = PTR_ERR(obj);
629 		goto err_tree_mutex_unlock;
630 	}
631 
632 	if (!xrcd) {
633 		xrcd = ib_alloc_xrcd_user(ib_dev, inode, &attrs->driver_udata);
634 		if (IS_ERR(xrcd)) {
635 			ret = PTR_ERR(xrcd);
636 			goto err;
637 		}
638 		new_xrcd = 1;
639 	}
640 
641 	atomic_set(&obj->refcnt, 0);
642 	obj->uobject.object = xrcd;
643 
644 	if (inode) {
645 		if (new_xrcd) {
646 			/* create new inode/xrcd table entry */
647 			ret = xrcd_table_insert(ibudev, inode, xrcd);
648 			if (ret)
649 				goto err_dealloc_xrcd;
650 		}
651 		atomic_inc(&xrcd->usecnt);
652 	}
653 
654 	fdput(f);
655 
656 	mutex_unlock(&ibudev->xrcd_tree_mutex);
657 	uobj_finalize_uobj_create(&obj->uobject, attrs);
658 
659 	resp.xrcd_handle = obj->uobject.id;
660 	return uverbs_response(attrs, &resp, sizeof(resp));
661 
662 err_dealloc_xrcd:
663 	ib_dealloc_xrcd_user(xrcd, uverbs_get_cleared_udata(attrs));
664 
665 err:
666 	uobj_alloc_abort(&obj->uobject, attrs);
667 
668 err_tree_mutex_unlock:
669 	fdput(f);
670 
671 	mutex_unlock(&ibudev->xrcd_tree_mutex);
672 
673 	return ret;
674 }
675 
ib_uverbs_close_xrcd(struct uverbs_attr_bundle * attrs)676 static int ib_uverbs_close_xrcd(struct uverbs_attr_bundle *attrs)
677 {
678 	struct ib_uverbs_close_xrcd cmd;
679 	int ret;
680 
681 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
682 	if (ret)
683 		return ret;
684 
685 	return uobj_perform_destroy(UVERBS_OBJECT_XRCD, cmd.xrcd_handle, attrs);
686 }
687 
ib_uverbs_dealloc_xrcd(struct ib_uobject * uobject,struct ib_xrcd * xrcd,enum rdma_remove_reason why,struct uverbs_attr_bundle * attrs)688 int ib_uverbs_dealloc_xrcd(struct ib_uobject *uobject, struct ib_xrcd *xrcd,
689 			   enum rdma_remove_reason why,
690 			   struct uverbs_attr_bundle *attrs)
691 {
692 	struct inode *inode;
693 	int ret;
694 	struct ib_uverbs_device *dev = attrs->ufile->device;
695 
696 	inode = xrcd->inode;
697 	if (inode && !atomic_dec_and_test(&xrcd->usecnt))
698 		return 0;
699 
700 	ret = ib_dealloc_xrcd_user(xrcd, &attrs->driver_udata);
701 	if (ret) {
702 		atomic_inc(&xrcd->usecnt);
703 		return ret;
704 	}
705 
706 	if (inode)
707 		xrcd_table_delete(dev, inode);
708 
709 	return 0;
710 }
711 
ib_uverbs_reg_mr(struct uverbs_attr_bundle * attrs)712 static int ib_uverbs_reg_mr(struct uverbs_attr_bundle *attrs)
713 {
714 	struct ib_uverbs_reg_mr_resp resp = {};
715 	struct ib_uverbs_reg_mr      cmd;
716 	struct ib_uobject           *uobj;
717 	struct ib_pd                *pd;
718 	struct ib_mr                *mr;
719 	int                          ret;
720 	struct ib_device *ib_dev;
721 
722 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
723 	if (ret)
724 		return ret;
725 
726 	if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
727 		return -EINVAL;
728 
729 	uobj = uobj_alloc(UVERBS_OBJECT_MR, attrs, &ib_dev);
730 	if (IS_ERR(uobj))
731 		return PTR_ERR(uobj);
732 
733 	ret = ib_check_mr_access(ib_dev, cmd.access_flags);
734 	if (ret)
735 		goto err_free;
736 
737 	pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
738 	if (IS_ERR(pd)) {
739 		ret = PTR_ERR(pd);
740 		goto err_free;
741 	}
742 
743 	mr = pd->device->ops.reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
744 					 cmd.access_flags, NULL,
745 					 &attrs->driver_udata);
746 	if (IS_ERR(mr)) {
747 		ret = PTR_ERR(mr);
748 		goto err_put;
749 	}
750 
751 	mr->device  = pd->device;
752 	mr->pd      = pd;
753 	mr->type    = IB_MR_TYPE_USER;
754 	mr->dm	    = NULL;
755 	mr->sig_attrs = NULL;
756 	mr->uobject = uobj;
757 	atomic_inc(&pd->usecnt);
758 	mr->iova = cmd.hca_va;
759 	mr->length = cmd.length;
760 
761 	rdma_restrack_new(&mr->res, RDMA_RESTRACK_MR);
762 	rdma_restrack_set_name(&mr->res, NULL);
763 	rdma_restrack_add(&mr->res);
764 
765 	uobj->object = mr;
766 	uobj_put_obj_read(pd);
767 	uobj_finalize_uobj_create(uobj, attrs);
768 
769 	resp.lkey = mr->lkey;
770 	resp.rkey = mr->rkey;
771 	resp.mr_handle = uobj->id;
772 	return uverbs_response(attrs, &resp, sizeof(resp));
773 
774 err_put:
775 	uobj_put_obj_read(pd);
776 err_free:
777 	uobj_alloc_abort(uobj, attrs);
778 	return ret;
779 }
780 
ib_uverbs_rereg_mr(struct uverbs_attr_bundle * attrs)781 static int ib_uverbs_rereg_mr(struct uverbs_attr_bundle *attrs)
782 {
783 	struct ib_uverbs_rereg_mr      cmd;
784 	struct ib_uverbs_rereg_mr_resp resp;
785 	struct ib_mr                *mr;
786 	int                          ret;
787 	struct ib_uobject	    *uobj;
788 	struct ib_uobject *new_uobj;
789 	struct ib_device *ib_dev;
790 	struct ib_pd *orig_pd;
791 	struct ib_pd *new_pd;
792 	struct ib_mr *new_mr;
793 
794 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
795 	if (ret)
796 		return ret;
797 
798 	if (!cmd.flags)
799 		return -EINVAL;
800 
801 	if (cmd.flags & ~IB_MR_REREG_SUPPORTED)
802 		return -EOPNOTSUPP;
803 
804 	if ((cmd.flags & IB_MR_REREG_TRANS) &&
805 	    (cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
806 		return -EINVAL;
807 
808 	uobj = uobj_get_write(UVERBS_OBJECT_MR, cmd.mr_handle, attrs);
809 	if (IS_ERR(uobj))
810 		return PTR_ERR(uobj);
811 
812 	mr = uobj->object;
813 
814 	if (mr->dm) {
815 		ret = -EINVAL;
816 		goto put_uobjs;
817 	}
818 
819 	if (cmd.flags & IB_MR_REREG_ACCESS) {
820 		ret = ib_check_mr_access(mr->device, cmd.access_flags);
821 		if (ret)
822 			goto put_uobjs;
823 	}
824 
825 	orig_pd = mr->pd;
826 	if (cmd.flags & IB_MR_REREG_PD) {
827 		new_pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle,
828 					   attrs);
829 		if (IS_ERR(new_pd)) {
830 			ret = PTR_ERR(new_pd);
831 			goto put_uobjs;
832 		}
833 	} else {
834 		new_pd = mr->pd;
835 	}
836 
837 	/*
838 	 * The driver might create a new HW object as part of the rereg, we need
839 	 * to have a uobject ready to hold it.
840 	 */
841 	new_uobj = uobj_alloc(UVERBS_OBJECT_MR, attrs, &ib_dev);
842 	if (IS_ERR(new_uobj)) {
843 		ret = PTR_ERR(new_uobj);
844 		goto put_uobj_pd;
845 	}
846 
847 	new_mr = ib_dev->ops.rereg_user_mr(mr, cmd.flags, cmd.start, cmd.length,
848 					   cmd.hca_va, cmd.access_flags, new_pd,
849 					   &attrs->driver_udata);
850 	if (IS_ERR(new_mr)) {
851 		ret = PTR_ERR(new_mr);
852 		goto put_new_uobj;
853 	}
854 	if (new_mr) {
855 		new_mr->device = new_pd->device;
856 		new_mr->pd = new_pd;
857 		new_mr->type = IB_MR_TYPE_USER;
858 		new_mr->uobject = uobj;
859 		atomic_inc(&new_pd->usecnt);
860 		new_uobj->object = new_mr;
861 
862 		rdma_restrack_new(&new_mr->res, RDMA_RESTRACK_MR);
863 		rdma_restrack_set_name(&new_mr->res, NULL);
864 		rdma_restrack_add(&new_mr->res);
865 
866 		/*
867 		 * The new uobj for the new HW object is put into the same spot
868 		 * in the IDR and the old uobj & HW object is deleted.
869 		 */
870 		rdma_assign_uobject(uobj, new_uobj, attrs);
871 		rdma_alloc_commit_uobject(new_uobj, attrs);
872 		uobj_put_destroy(uobj);
873 		new_uobj = NULL;
874 		uobj = NULL;
875 		mr = new_mr;
876 	} else {
877 		if (cmd.flags & IB_MR_REREG_PD) {
878 			atomic_dec(&orig_pd->usecnt);
879 			mr->pd = new_pd;
880 			atomic_inc(&new_pd->usecnt);
881 		}
882 		if (cmd.flags & IB_MR_REREG_TRANS) {
883 			mr->iova = cmd.hca_va;
884 			mr->length = cmd.length;
885 		}
886 	}
887 
888 	memset(&resp, 0, sizeof(resp));
889 	resp.lkey      = mr->lkey;
890 	resp.rkey      = mr->rkey;
891 
892 	ret = uverbs_response(attrs, &resp, sizeof(resp));
893 
894 put_new_uobj:
895 	if (new_uobj)
896 		uobj_alloc_abort(new_uobj, attrs);
897 put_uobj_pd:
898 	if (cmd.flags & IB_MR_REREG_PD)
899 		uobj_put_obj_read(new_pd);
900 
901 put_uobjs:
902 	if (uobj)
903 		uobj_put_write(uobj);
904 
905 	return ret;
906 }
907 
ib_uverbs_dereg_mr(struct uverbs_attr_bundle * attrs)908 static int ib_uverbs_dereg_mr(struct uverbs_attr_bundle *attrs)
909 {
910 	struct ib_uverbs_dereg_mr cmd;
911 	int ret;
912 
913 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
914 	if (ret)
915 		return ret;
916 
917 	return uobj_perform_destroy(UVERBS_OBJECT_MR, cmd.mr_handle, attrs);
918 }
919 
ib_uverbs_alloc_mw(struct uverbs_attr_bundle * attrs)920 static int ib_uverbs_alloc_mw(struct uverbs_attr_bundle *attrs)
921 {
922 	struct ib_uverbs_alloc_mw      cmd;
923 	struct ib_uverbs_alloc_mw_resp resp = {};
924 	struct ib_uobject             *uobj;
925 	struct ib_pd                  *pd;
926 	struct ib_mw                  *mw;
927 	int                            ret;
928 	struct ib_device *ib_dev;
929 
930 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
931 	if (ret)
932 		return ret;
933 
934 	uobj = uobj_alloc(UVERBS_OBJECT_MW, attrs, &ib_dev);
935 	if (IS_ERR(uobj))
936 		return PTR_ERR(uobj);
937 
938 	pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
939 	if (IS_ERR(pd)) {
940 		ret = PTR_ERR(pd);
941 		goto err_free;
942 	}
943 
944 	if (cmd.mw_type != IB_MW_TYPE_1 && cmd.mw_type != IB_MW_TYPE_2) {
945 		ret = -EINVAL;
946 		goto err_put;
947 	}
948 
949 	mw = rdma_zalloc_drv_obj(ib_dev, ib_mw);
950 	if (!mw) {
951 		ret = -ENOMEM;
952 		goto err_put;
953 	}
954 
955 	mw->device = ib_dev;
956 	mw->pd = pd;
957 	mw->uobject = uobj;
958 	mw->type = cmd.mw_type;
959 
960 	ret = pd->device->ops.alloc_mw(mw, &attrs->driver_udata);
961 	if (ret)
962 		goto err_alloc;
963 
964 	atomic_inc(&pd->usecnt);
965 
966 	uobj->object = mw;
967 	uobj_put_obj_read(pd);
968 	uobj_finalize_uobj_create(uobj, attrs);
969 
970 	resp.rkey = mw->rkey;
971 	resp.mw_handle = uobj->id;
972 	return uverbs_response(attrs, &resp, sizeof(resp));
973 
974 err_alloc:
975 	kfree(mw);
976 err_put:
977 	uobj_put_obj_read(pd);
978 err_free:
979 	uobj_alloc_abort(uobj, attrs);
980 	return ret;
981 }
982 
ib_uverbs_dealloc_mw(struct uverbs_attr_bundle * attrs)983 static int ib_uverbs_dealloc_mw(struct uverbs_attr_bundle *attrs)
984 {
985 	struct ib_uverbs_dealloc_mw cmd;
986 	int ret;
987 
988 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
989 	if (ret)
990 		return ret;
991 
992 	return uobj_perform_destroy(UVERBS_OBJECT_MW, cmd.mw_handle, attrs);
993 }
994 
ib_uverbs_create_comp_channel(struct uverbs_attr_bundle * attrs)995 static int ib_uverbs_create_comp_channel(struct uverbs_attr_bundle *attrs)
996 {
997 	struct ib_uverbs_create_comp_channel	   cmd;
998 	struct ib_uverbs_create_comp_channel_resp  resp;
999 	struct ib_uobject			  *uobj;
1000 	struct ib_uverbs_completion_event_file	  *ev_file;
1001 	struct ib_device *ib_dev;
1002 	int ret;
1003 
1004 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1005 	if (ret)
1006 		return ret;
1007 
1008 	uobj = uobj_alloc(UVERBS_OBJECT_COMP_CHANNEL, attrs, &ib_dev);
1009 	if (IS_ERR(uobj))
1010 		return PTR_ERR(uobj);
1011 
1012 	ev_file = container_of(uobj, struct ib_uverbs_completion_event_file,
1013 			       uobj);
1014 	ib_uverbs_init_event_queue(&ev_file->ev_queue);
1015 	uobj_finalize_uobj_create(uobj, attrs);
1016 
1017 	resp.fd = uobj->id;
1018 	return uverbs_response(attrs, &resp, sizeof(resp));
1019 }
1020 
create_cq(struct uverbs_attr_bundle * attrs,struct ib_uverbs_ex_create_cq * cmd)1021 static int create_cq(struct uverbs_attr_bundle *attrs,
1022 		     struct ib_uverbs_ex_create_cq *cmd)
1023 {
1024 	struct ib_ucq_object           *obj;
1025 	struct ib_uverbs_completion_event_file    *ev_file = NULL;
1026 	struct ib_cq                   *cq;
1027 	int                             ret;
1028 	struct ib_uverbs_ex_create_cq_resp resp = {};
1029 	struct ib_cq_init_attr attr = {};
1030 	struct ib_device *ib_dev;
1031 
1032 	if (cmd->comp_vector >= attrs->ufile->device->num_comp_vectors)
1033 		return -EINVAL;
1034 
1035 	obj = (struct ib_ucq_object *)uobj_alloc(UVERBS_OBJECT_CQ, attrs,
1036 						 &ib_dev);
1037 	if (IS_ERR(obj))
1038 		return PTR_ERR(obj);
1039 
1040 	if (cmd->comp_channel >= 0) {
1041 		ev_file = ib_uverbs_lookup_comp_file(cmd->comp_channel, attrs);
1042 		if (IS_ERR(ev_file)) {
1043 			ret = PTR_ERR(ev_file);
1044 			goto err;
1045 		}
1046 	}
1047 
1048 	obj->uevent.uobject.user_handle = cmd->user_handle;
1049 	INIT_LIST_HEAD(&obj->comp_list);
1050 	INIT_LIST_HEAD(&obj->uevent.event_list);
1051 
1052 	attr.cqe = cmd->cqe;
1053 	attr.comp_vector = cmd->comp_vector;
1054 	attr.flags = cmd->flags;
1055 
1056 	cq = rdma_zalloc_drv_obj(ib_dev, ib_cq);
1057 	if (!cq) {
1058 		ret = -ENOMEM;
1059 		goto err_file;
1060 	}
1061 	cq->device        = ib_dev;
1062 	cq->uobject       = obj;
1063 	cq->comp_handler  = ib_uverbs_comp_handler;
1064 	cq->event_handler = ib_uverbs_cq_event_handler;
1065 	cq->cq_context    = ev_file ? &ev_file->ev_queue : NULL;
1066 	atomic_set(&cq->usecnt, 0);
1067 
1068 	rdma_restrack_new(&cq->res, RDMA_RESTRACK_CQ);
1069 	rdma_restrack_set_name(&cq->res, NULL);
1070 
1071 	ret = ib_dev->ops.create_cq(cq, &attr, attrs);
1072 	if (ret)
1073 		goto err_free;
1074 	rdma_restrack_add(&cq->res);
1075 
1076 	obj->uevent.uobject.object = cq;
1077 	obj->uevent.event_file = READ_ONCE(attrs->ufile->default_async_file);
1078 	if (obj->uevent.event_file)
1079 		uverbs_uobject_get(&obj->uevent.event_file->uobj);
1080 	uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
1081 
1082 	resp.base.cq_handle = obj->uevent.uobject.id;
1083 	resp.base.cqe = cq->cqe;
1084 	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
1085 	return uverbs_response(attrs, &resp, sizeof(resp));
1086 
1087 err_free:
1088 	rdma_restrack_put(&cq->res);
1089 	kfree(cq);
1090 err_file:
1091 	if (ev_file)
1092 		ib_uverbs_release_ucq(ev_file, obj);
1093 err:
1094 	uobj_alloc_abort(&obj->uevent.uobject, attrs);
1095 	return ret;
1096 }
1097 
ib_uverbs_create_cq(struct uverbs_attr_bundle * attrs)1098 static int ib_uverbs_create_cq(struct uverbs_attr_bundle *attrs)
1099 {
1100 	struct ib_uverbs_create_cq      cmd;
1101 	struct ib_uverbs_ex_create_cq	cmd_ex;
1102 	int ret;
1103 
1104 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1105 	if (ret)
1106 		return ret;
1107 
1108 	memset(&cmd_ex, 0, sizeof(cmd_ex));
1109 	cmd_ex.user_handle = cmd.user_handle;
1110 	cmd_ex.cqe = cmd.cqe;
1111 	cmd_ex.comp_vector = cmd.comp_vector;
1112 	cmd_ex.comp_channel = cmd.comp_channel;
1113 
1114 	return create_cq(attrs, &cmd_ex);
1115 }
1116 
ib_uverbs_ex_create_cq(struct uverbs_attr_bundle * attrs)1117 static int ib_uverbs_ex_create_cq(struct uverbs_attr_bundle *attrs)
1118 {
1119 	struct ib_uverbs_ex_create_cq  cmd;
1120 	int ret;
1121 
1122 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1123 	if (ret)
1124 		return ret;
1125 
1126 	if (cmd.comp_mask)
1127 		return -EINVAL;
1128 
1129 	if (cmd.reserved)
1130 		return -EINVAL;
1131 
1132 	return create_cq(attrs, &cmd);
1133 }
1134 
ib_uverbs_resize_cq(struct uverbs_attr_bundle * attrs)1135 static int ib_uverbs_resize_cq(struct uverbs_attr_bundle *attrs)
1136 {
1137 	struct ib_uverbs_resize_cq	cmd;
1138 	struct ib_uverbs_resize_cq_resp	resp = {};
1139 	struct ib_cq			*cq;
1140 	int ret;
1141 
1142 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1143 	if (ret)
1144 		return ret;
1145 
1146 	cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1147 	if (IS_ERR(cq))
1148 		return PTR_ERR(cq);
1149 
1150 	ret = cq->device->ops.resize_cq(cq, cmd.cqe, &attrs->driver_udata);
1151 	if (ret)
1152 		goto out;
1153 
1154 	resp.cqe = cq->cqe;
1155 
1156 	ret = uverbs_response(attrs, &resp, sizeof(resp));
1157 out:
1158 	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
1159 				UVERBS_LOOKUP_READ);
1160 
1161 	return ret;
1162 }
1163 
copy_wc_to_user(struct ib_device * ib_dev,void __user * dest,struct ib_wc * wc)1164 static int copy_wc_to_user(struct ib_device *ib_dev, void __user *dest,
1165 			   struct ib_wc *wc)
1166 {
1167 	struct ib_uverbs_wc tmp;
1168 
1169 	tmp.wr_id		= wc->wr_id;
1170 	tmp.status		= wc->status;
1171 	tmp.opcode		= wc->opcode;
1172 	tmp.vendor_err		= wc->vendor_err;
1173 	tmp.byte_len		= wc->byte_len;
1174 	tmp.ex.imm_data		= wc->ex.imm_data;
1175 	tmp.qp_num		= wc->qp->qp_num;
1176 	tmp.src_qp		= wc->src_qp;
1177 	tmp.wc_flags		= wc->wc_flags;
1178 	tmp.pkey_index		= wc->pkey_index;
1179 	if (rdma_cap_opa_ah(ib_dev, wc->port_num))
1180 		tmp.slid	= OPA_TO_IB_UCAST_LID(wc->slid);
1181 	else
1182 		tmp.slid	= ib_lid_cpu16(wc->slid);
1183 	tmp.sl			= wc->sl;
1184 	tmp.dlid_path_bits	= wc->dlid_path_bits;
1185 	tmp.port_num		= wc->port_num;
1186 	tmp.reserved		= 0;
1187 
1188 	if (copy_to_user(dest, &tmp, sizeof tmp))
1189 		return -EFAULT;
1190 
1191 	return 0;
1192 }
1193 
ib_uverbs_poll_cq(struct uverbs_attr_bundle * attrs)1194 static int ib_uverbs_poll_cq(struct uverbs_attr_bundle *attrs)
1195 {
1196 	struct ib_uverbs_poll_cq       cmd;
1197 	struct ib_uverbs_poll_cq_resp  resp;
1198 	u8 __user                     *header_ptr;
1199 	u8 __user                     *data_ptr;
1200 	struct ib_cq                  *cq;
1201 	struct ib_wc                   wc;
1202 	int                            ret;
1203 
1204 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1205 	if (ret)
1206 		return ret;
1207 
1208 	cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1209 	if (IS_ERR(cq))
1210 		return PTR_ERR(cq);
1211 
1212 	/* we copy a struct ib_uverbs_poll_cq_resp to user space */
1213 	header_ptr = attrs->ucore.outbuf;
1214 	data_ptr = header_ptr + sizeof resp;
1215 
1216 	memset(&resp, 0, sizeof resp);
1217 	while (resp.count < cmd.ne) {
1218 		ret = ib_poll_cq(cq, 1, &wc);
1219 		if (ret < 0)
1220 			goto out_put;
1221 		if (!ret)
1222 			break;
1223 
1224 		ret = copy_wc_to_user(cq->device, data_ptr, &wc);
1225 		if (ret)
1226 			goto out_put;
1227 
1228 		data_ptr += sizeof(struct ib_uverbs_wc);
1229 		++resp.count;
1230 	}
1231 
1232 	if (copy_to_user(header_ptr, &resp, sizeof resp)) {
1233 		ret = -EFAULT;
1234 		goto out_put;
1235 	}
1236 	ret = 0;
1237 
1238 	if (uverbs_attr_is_valid(attrs, UVERBS_ATTR_CORE_OUT))
1239 		ret = uverbs_output_written(attrs, UVERBS_ATTR_CORE_OUT);
1240 
1241 out_put:
1242 	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
1243 				UVERBS_LOOKUP_READ);
1244 	return ret;
1245 }
1246 
ib_uverbs_req_notify_cq(struct uverbs_attr_bundle * attrs)1247 static int ib_uverbs_req_notify_cq(struct uverbs_attr_bundle *attrs)
1248 {
1249 	struct ib_uverbs_req_notify_cq cmd;
1250 	struct ib_cq                  *cq;
1251 	int ret;
1252 
1253 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1254 	if (ret)
1255 		return ret;
1256 
1257 	cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1258 	if (IS_ERR(cq))
1259 		return PTR_ERR(cq);
1260 
1261 	ib_req_notify_cq(cq, cmd.solicited_only ?
1262 			 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
1263 
1264 	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
1265 				UVERBS_LOOKUP_READ);
1266 	return 0;
1267 }
1268 
ib_uverbs_destroy_cq(struct uverbs_attr_bundle * attrs)1269 static int ib_uverbs_destroy_cq(struct uverbs_attr_bundle *attrs)
1270 {
1271 	struct ib_uverbs_destroy_cq      cmd;
1272 	struct ib_uverbs_destroy_cq_resp resp;
1273 	struct ib_uobject		*uobj;
1274 	struct ib_ucq_object        	*obj;
1275 	int ret;
1276 
1277 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1278 	if (ret)
1279 		return ret;
1280 
1281 	uobj = uobj_get_destroy(UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
1282 	if (IS_ERR(uobj))
1283 		return PTR_ERR(uobj);
1284 
1285 	obj = container_of(uobj, struct ib_ucq_object, uevent.uobject);
1286 	memset(&resp, 0, sizeof(resp));
1287 	resp.comp_events_reported  = obj->comp_events_reported;
1288 	resp.async_events_reported = obj->uevent.events_reported;
1289 
1290 	uobj_put_destroy(uobj);
1291 
1292 	return uverbs_response(attrs, &resp, sizeof(resp));
1293 }
1294 
create_qp(struct uverbs_attr_bundle * attrs,struct ib_uverbs_ex_create_qp * cmd)1295 static int create_qp(struct uverbs_attr_bundle *attrs,
1296 		     struct ib_uverbs_ex_create_qp *cmd)
1297 {
1298 	struct ib_uqp_object		*obj;
1299 	struct ib_device		*device;
1300 	struct ib_pd			*pd = NULL;
1301 	struct ib_xrcd			*xrcd = NULL;
1302 	struct ib_uobject		*xrcd_uobj = ERR_PTR(-ENOENT);
1303 	struct ib_cq			*scq = NULL, *rcq = NULL;
1304 	struct ib_srq			*srq = NULL;
1305 	struct ib_qp			*qp;
1306 	struct ib_qp_init_attr		attr = {};
1307 	struct ib_uverbs_ex_create_qp_resp resp = {};
1308 	int				ret;
1309 	struct ib_rwq_ind_table *ind_tbl = NULL;
1310 	bool has_sq = true;
1311 	struct ib_device *ib_dev;
1312 
1313 	switch (cmd->qp_type) {
1314 	case IB_QPT_RAW_PACKET:
1315 		if (!rdma_uattrs_has_raw_cap(attrs))
1316 			return -EPERM;
1317 		fallthrough;
1318 	case IB_QPT_RC:
1319 	case IB_QPT_UC:
1320 	case IB_QPT_UD:
1321 	case IB_QPT_XRC_INI:
1322 	case IB_QPT_XRC_TGT:
1323 	case IB_QPT_DRIVER:
1324 		break;
1325 	default:
1326 		return -EINVAL;
1327 	}
1328 
1329 	obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, attrs,
1330 						 &ib_dev);
1331 	if (IS_ERR(obj))
1332 		return PTR_ERR(obj);
1333 	obj->uxrcd = NULL;
1334 	obj->uevent.uobject.user_handle = cmd->user_handle;
1335 	mutex_init(&obj->mcast_lock);
1336 
1337 	if (cmd->comp_mask & IB_UVERBS_CREATE_QP_MASK_IND_TABLE) {
1338 		ind_tbl = uobj_get_obj_read(rwq_ind_table,
1339 					    UVERBS_OBJECT_RWQ_IND_TBL,
1340 					    cmd->rwq_ind_tbl_handle, attrs);
1341 		if (IS_ERR(ind_tbl)) {
1342 			ret = PTR_ERR(ind_tbl);
1343 			goto err_put;
1344 		}
1345 
1346 		attr.rwq_ind_tbl = ind_tbl;
1347 	}
1348 
1349 	if (ind_tbl && (cmd->max_recv_wr || cmd->max_recv_sge || cmd->is_srq)) {
1350 		ret = -EINVAL;
1351 		goto err_put;
1352 	}
1353 
1354 	if (ind_tbl && !cmd->max_send_wr)
1355 		has_sq = false;
1356 
1357 	if (cmd->qp_type == IB_QPT_XRC_TGT) {
1358 		xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->pd_handle,
1359 					  attrs);
1360 
1361 		if (IS_ERR(xrcd_uobj)) {
1362 			ret = -EINVAL;
1363 			goto err_put;
1364 		}
1365 
1366 		xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1367 		if (!xrcd) {
1368 			ret = -EINVAL;
1369 			goto err_put;
1370 		}
1371 		device = xrcd->device;
1372 	} else {
1373 		if (cmd->qp_type == IB_QPT_XRC_INI) {
1374 			cmd->max_recv_wr = 0;
1375 			cmd->max_recv_sge = 0;
1376 		} else {
1377 			if (cmd->is_srq) {
1378 				srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ,
1379 							cmd->srq_handle, attrs);
1380 				if (IS_ERR(srq) ||
1381 				    srq->srq_type == IB_SRQT_XRC) {
1382 					ret = IS_ERR(srq) ? PTR_ERR(srq) :
1383 								  -EINVAL;
1384 					goto err_put;
1385 				}
1386 			}
1387 
1388 			if (!ind_tbl) {
1389 				if (cmd->recv_cq_handle != cmd->send_cq_handle) {
1390 					rcq = uobj_get_obj_read(
1391 						cq, UVERBS_OBJECT_CQ,
1392 						cmd->recv_cq_handle, attrs);
1393 					if (IS_ERR(rcq)) {
1394 						ret = PTR_ERR(rcq);
1395 						goto err_put;
1396 					}
1397 				}
1398 			}
1399 		}
1400 
1401 		if (has_sq) {
1402 			scq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
1403 						cmd->send_cq_handle, attrs);
1404 			if (IS_ERR(scq)) {
1405 				ret = PTR_ERR(scq);
1406 				goto err_put;
1407 			}
1408 		}
1409 
1410 		if (!ind_tbl && cmd->qp_type != IB_QPT_XRC_INI)
1411 			rcq = rcq ?: scq;
1412 		pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle,
1413 				       attrs);
1414 		if (IS_ERR(pd)) {
1415 			ret = PTR_ERR(pd);
1416 			goto err_put;
1417 		}
1418 
1419 		device = pd->device;
1420 	}
1421 
1422 	attr.event_handler = ib_uverbs_qp_event_handler;
1423 	attr.send_cq       = scq;
1424 	attr.recv_cq       = rcq;
1425 	attr.srq           = srq;
1426 	attr.xrcd	   = xrcd;
1427 	attr.sq_sig_type   = cmd->sq_sig_all ? IB_SIGNAL_ALL_WR :
1428 					      IB_SIGNAL_REQ_WR;
1429 	attr.qp_type       = cmd->qp_type;
1430 
1431 	attr.cap.max_send_wr     = cmd->max_send_wr;
1432 	attr.cap.max_recv_wr     = cmd->max_recv_wr;
1433 	attr.cap.max_send_sge    = cmd->max_send_sge;
1434 	attr.cap.max_recv_sge    = cmd->max_recv_sge;
1435 	attr.cap.max_inline_data = cmd->max_inline_data;
1436 
1437 	INIT_LIST_HEAD(&obj->uevent.event_list);
1438 	INIT_LIST_HEAD(&obj->mcast_list);
1439 
1440 	attr.create_flags = cmd->create_flags;
1441 	if (attr.create_flags & ~(IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
1442 				IB_QP_CREATE_CROSS_CHANNEL |
1443 				IB_QP_CREATE_MANAGED_SEND |
1444 				IB_QP_CREATE_MANAGED_RECV |
1445 				IB_QP_CREATE_SCATTER_FCS |
1446 				IB_QP_CREATE_CVLAN_STRIPPING |
1447 				IB_QP_CREATE_SOURCE_QPN |
1448 				IB_QP_CREATE_PCI_WRITE_END_PADDING)) {
1449 		ret = -EINVAL;
1450 		goto err_put;
1451 	}
1452 
1453 	if (attr.create_flags & IB_QP_CREATE_SOURCE_QPN) {
1454 		if (!rdma_uattrs_has_raw_cap(attrs)) {
1455 			ret = -EPERM;
1456 			goto err_put;
1457 		}
1458 
1459 		attr.source_qpn = cmd->source_qpn;
1460 	}
1461 
1462 	qp = ib_create_qp_user(device, pd, &attr, &attrs->driver_udata, obj,
1463 			       KBUILD_MODNAME);
1464 	if (IS_ERR(qp)) {
1465 		ret = PTR_ERR(qp);
1466 		goto err_put;
1467 	}
1468 	ib_qp_usecnt_inc(qp);
1469 
1470 	obj->uevent.uobject.object = qp;
1471 	obj->uevent.event_file = READ_ONCE(attrs->ufile->default_async_file);
1472 	if (obj->uevent.event_file)
1473 		uverbs_uobject_get(&obj->uevent.event_file->uobj);
1474 
1475 	if (xrcd) {
1476 		obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
1477 					  uobject);
1478 		atomic_inc(&obj->uxrcd->refcnt);
1479 		uobj_put_read(xrcd_uobj);
1480 	}
1481 
1482 	if (pd)
1483 		uobj_put_obj_read(pd);
1484 	if (scq)
1485 		rdma_lookup_put_uobject(&scq->uobject->uevent.uobject,
1486 					UVERBS_LOOKUP_READ);
1487 	if (rcq && rcq != scq)
1488 		rdma_lookup_put_uobject(&rcq->uobject->uevent.uobject,
1489 					UVERBS_LOOKUP_READ);
1490 	if (srq)
1491 		rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
1492 					UVERBS_LOOKUP_READ);
1493 	if (ind_tbl)
1494 		uobj_put_obj_read(ind_tbl);
1495 	uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
1496 
1497 	resp.base.qpn             = qp->qp_num;
1498 	resp.base.qp_handle       = obj->uevent.uobject.id;
1499 	resp.base.max_recv_sge    = attr.cap.max_recv_sge;
1500 	resp.base.max_send_sge    = attr.cap.max_send_sge;
1501 	resp.base.max_recv_wr     = attr.cap.max_recv_wr;
1502 	resp.base.max_send_wr     = attr.cap.max_send_wr;
1503 	resp.base.max_inline_data = attr.cap.max_inline_data;
1504 	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
1505 	return uverbs_response(attrs, &resp, sizeof(resp));
1506 
1507 err_put:
1508 	if (!IS_ERR(xrcd_uobj))
1509 		uobj_put_read(xrcd_uobj);
1510 	if (!IS_ERR_OR_NULL(pd))
1511 		uobj_put_obj_read(pd);
1512 	if (!IS_ERR_OR_NULL(scq))
1513 		rdma_lookup_put_uobject(&scq->uobject->uevent.uobject,
1514 					UVERBS_LOOKUP_READ);
1515 	if (!IS_ERR_OR_NULL(rcq) && rcq != scq)
1516 		rdma_lookup_put_uobject(&rcq->uobject->uevent.uobject,
1517 					UVERBS_LOOKUP_READ);
1518 	if (!IS_ERR_OR_NULL(srq))
1519 		rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
1520 					UVERBS_LOOKUP_READ);
1521 	if (!IS_ERR_OR_NULL(ind_tbl))
1522 		uobj_put_obj_read(ind_tbl);
1523 
1524 	uobj_alloc_abort(&obj->uevent.uobject, attrs);
1525 	return ret;
1526 }
1527 
ib_uverbs_create_qp(struct uverbs_attr_bundle * attrs)1528 static int ib_uverbs_create_qp(struct uverbs_attr_bundle *attrs)
1529 {
1530 	struct ib_uverbs_create_qp      cmd;
1531 	struct ib_uverbs_ex_create_qp	cmd_ex;
1532 	int ret;
1533 
1534 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1535 	if (ret)
1536 		return ret;
1537 
1538 	memset(&cmd_ex, 0, sizeof(cmd_ex));
1539 	cmd_ex.user_handle = cmd.user_handle;
1540 	cmd_ex.pd_handle = cmd.pd_handle;
1541 	cmd_ex.send_cq_handle = cmd.send_cq_handle;
1542 	cmd_ex.recv_cq_handle = cmd.recv_cq_handle;
1543 	cmd_ex.srq_handle = cmd.srq_handle;
1544 	cmd_ex.max_send_wr = cmd.max_send_wr;
1545 	cmd_ex.max_recv_wr = cmd.max_recv_wr;
1546 	cmd_ex.max_send_sge = cmd.max_send_sge;
1547 	cmd_ex.max_recv_sge = cmd.max_recv_sge;
1548 	cmd_ex.max_inline_data = cmd.max_inline_data;
1549 	cmd_ex.sq_sig_all = cmd.sq_sig_all;
1550 	cmd_ex.qp_type = cmd.qp_type;
1551 	cmd_ex.is_srq = cmd.is_srq;
1552 
1553 	return create_qp(attrs, &cmd_ex);
1554 }
1555 
ib_uverbs_ex_create_qp(struct uverbs_attr_bundle * attrs)1556 static int ib_uverbs_ex_create_qp(struct uverbs_attr_bundle *attrs)
1557 {
1558 	struct ib_uverbs_ex_create_qp cmd;
1559 	int ret;
1560 
1561 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1562 	if (ret)
1563 		return ret;
1564 
1565 	if (cmd.comp_mask & ~IB_UVERBS_CREATE_QP_SUP_COMP_MASK)
1566 		return -EINVAL;
1567 
1568 	if (cmd.reserved)
1569 		return -EINVAL;
1570 
1571 	return create_qp(attrs, &cmd);
1572 }
1573 
ib_uverbs_open_qp(struct uverbs_attr_bundle * attrs)1574 static int ib_uverbs_open_qp(struct uverbs_attr_bundle *attrs)
1575 {
1576 	struct ib_uverbs_create_qp_resp resp = {};
1577 	struct ib_uverbs_open_qp        cmd;
1578 	struct ib_uqp_object           *obj;
1579 	struct ib_xrcd		       *xrcd;
1580 	struct ib_qp                   *qp;
1581 	struct ib_qp_open_attr          attr = {};
1582 	int ret;
1583 	struct ib_uobject *xrcd_uobj;
1584 	struct ib_device *ib_dev;
1585 
1586 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1587 	if (ret)
1588 		return ret;
1589 
1590 	obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, attrs,
1591 						 &ib_dev);
1592 	if (IS_ERR(obj))
1593 		return PTR_ERR(obj);
1594 
1595 	xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd.pd_handle, attrs);
1596 	if (IS_ERR(xrcd_uobj)) {
1597 		ret = -EINVAL;
1598 		goto err_put;
1599 	}
1600 
1601 	xrcd = (struct ib_xrcd *)xrcd_uobj->object;
1602 	if (!xrcd) {
1603 		ret = -EINVAL;
1604 		goto err_xrcd;
1605 	}
1606 
1607 	attr.event_handler = ib_uverbs_qp_event_handler;
1608 	attr.qp_num        = cmd.qpn;
1609 	attr.qp_type       = cmd.qp_type;
1610 
1611 	INIT_LIST_HEAD(&obj->uevent.event_list);
1612 	INIT_LIST_HEAD(&obj->mcast_list);
1613 
1614 	qp = ib_open_qp(xrcd, &attr);
1615 	if (IS_ERR(qp)) {
1616 		ret = PTR_ERR(qp);
1617 		goto err_xrcd;
1618 	}
1619 
1620 	obj->uevent.uobject.object = qp;
1621 	obj->uevent.uobject.user_handle = cmd.user_handle;
1622 
1623 	obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
1624 	atomic_inc(&obj->uxrcd->refcnt);
1625 	qp->uobject = obj;
1626 	uobj_put_read(xrcd_uobj);
1627 	uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
1628 
1629 	resp.qpn = qp->qp_num;
1630 	resp.qp_handle = obj->uevent.uobject.id;
1631 	return uverbs_response(attrs, &resp, sizeof(resp));
1632 
1633 err_xrcd:
1634 	uobj_put_read(xrcd_uobj);
1635 err_put:
1636 	uobj_alloc_abort(&obj->uevent.uobject, attrs);
1637 	return ret;
1638 }
1639 
copy_ah_attr_to_uverbs(struct ib_uverbs_qp_dest * uverb_attr,struct rdma_ah_attr * rdma_attr)1640 static void copy_ah_attr_to_uverbs(struct ib_uverbs_qp_dest *uverb_attr,
1641 				   struct rdma_ah_attr *rdma_attr)
1642 {
1643 	const struct ib_global_route   *grh;
1644 
1645 	uverb_attr->dlid              = rdma_ah_get_dlid(rdma_attr);
1646 	uverb_attr->sl                = rdma_ah_get_sl(rdma_attr);
1647 	uverb_attr->src_path_bits     = rdma_ah_get_path_bits(rdma_attr);
1648 	uverb_attr->static_rate       = rdma_ah_get_static_rate(rdma_attr);
1649 	uverb_attr->is_global         = !!(rdma_ah_get_ah_flags(rdma_attr) &
1650 					 IB_AH_GRH);
1651 	if (uverb_attr->is_global) {
1652 		grh = rdma_ah_read_grh(rdma_attr);
1653 		memcpy(uverb_attr->dgid, grh->dgid.raw, 16);
1654 		uverb_attr->flow_label        = grh->flow_label;
1655 		uverb_attr->sgid_index        = grh->sgid_index;
1656 		uverb_attr->hop_limit         = grh->hop_limit;
1657 		uverb_attr->traffic_class     = grh->traffic_class;
1658 	}
1659 	uverb_attr->port_num          = rdma_ah_get_port_num(rdma_attr);
1660 }
1661 
ib_uverbs_query_qp(struct uverbs_attr_bundle * attrs)1662 static int ib_uverbs_query_qp(struct uverbs_attr_bundle *attrs)
1663 {
1664 	struct ib_uverbs_query_qp      cmd;
1665 	struct ib_uverbs_query_qp_resp resp;
1666 	struct ib_qp                   *qp;
1667 	struct ib_qp_attr              *attr;
1668 	struct ib_qp_init_attr         *init_attr;
1669 	int                            ret;
1670 
1671 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1672 	if (ret)
1673 		return ret;
1674 
1675 	attr      = kmalloc(sizeof *attr, GFP_KERNEL);
1676 	init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1677 	if (!attr || !init_attr) {
1678 		ret = -ENOMEM;
1679 		goto out;
1680 	}
1681 
1682 	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
1683 	if (IS_ERR(qp)) {
1684 		ret = PTR_ERR(qp);
1685 		goto out;
1686 	}
1687 
1688 	ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
1689 
1690 	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
1691 				UVERBS_LOOKUP_READ);
1692 
1693 	if (ret)
1694 		goto out;
1695 
1696 	memset(&resp, 0, sizeof resp);
1697 
1698 	resp.qp_state               = attr->qp_state;
1699 	resp.cur_qp_state           = attr->cur_qp_state;
1700 	resp.path_mtu               = attr->path_mtu;
1701 	resp.path_mig_state         = attr->path_mig_state;
1702 	resp.qkey                   = attr->qkey;
1703 	resp.rq_psn                 = attr->rq_psn;
1704 	resp.sq_psn                 = attr->sq_psn;
1705 	resp.dest_qp_num            = attr->dest_qp_num;
1706 	resp.qp_access_flags        = attr->qp_access_flags;
1707 	resp.pkey_index             = attr->pkey_index;
1708 	resp.alt_pkey_index         = attr->alt_pkey_index;
1709 	resp.sq_draining            = attr->sq_draining;
1710 	resp.max_rd_atomic          = attr->max_rd_atomic;
1711 	resp.max_dest_rd_atomic     = attr->max_dest_rd_atomic;
1712 	resp.min_rnr_timer          = attr->min_rnr_timer;
1713 	resp.port_num               = attr->port_num;
1714 	resp.timeout                = attr->timeout;
1715 	resp.retry_cnt              = attr->retry_cnt;
1716 	resp.rnr_retry              = attr->rnr_retry;
1717 	resp.alt_port_num           = attr->alt_port_num;
1718 	resp.alt_timeout            = attr->alt_timeout;
1719 
1720 	copy_ah_attr_to_uverbs(&resp.dest, &attr->ah_attr);
1721 	copy_ah_attr_to_uverbs(&resp.alt_dest, &attr->alt_ah_attr);
1722 
1723 	resp.max_send_wr            = init_attr->cap.max_send_wr;
1724 	resp.max_recv_wr            = init_attr->cap.max_recv_wr;
1725 	resp.max_send_sge           = init_attr->cap.max_send_sge;
1726 	resp.max_recv_sge           = init_attr->cap.max_recv_sge;
1727 	resp.max_inline_data        = init_attr->cap.max_inline_data;
1728 	resp.sq_sig_all             = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1729 
1730 	ret = uverbs_response(attrs, &resp, sizeof(resp));
1731 
1732 out:
1733 	kfree(attr);
1734 	kfree(init_attr);
1735 
1736 	return ret;
1737 }
1738 
1739 /* Remove ignored fields set in the attribute mask */
modify_qp_mask(enum ib_qp_type qp_type,int mask)1740 static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
1741 {
1742 	switch (qp_type) {
1743 	case IB_QPT_XRC_INI:
1744 		return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
1745 	case IB_QPT_XRC_TGT:
1746 		return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
1747 				IB_QP_RNR_RETRY);
1748 	default:
1749 		return mask;
1750 	}
1751 }
1752 
copy_ah_attr_from_uverbs(struct ib_device * dev,struct rdma_ah_attr * rdma_attr,struct ib_uverbs_qp_dest * uverb_attr)1753 static void copy_ah_attr_from_uverbs(struct ib_device *dev,
1754 				     struct rdma_ah_attr *rdma_attr,
1755 				     struct ib_uverbs_qp_dest *uverb_attr)
1756 {
1757 	rdma_attr->type = rdma_ah_find_type(dev, uverb_attr->port_num);
1758 	if (uverb_attr->is_global) {
1759 		rdma_ah_set_grh(rdma_attr, NULL,
1760 				uverb_attr->flow_label,
1761 				uverb_attr->sgid_index,
1762 				uverb_attr->hop_limit,
1763 				uverb_attr->traffic_class);
1764 		rdma_ah_set_dgid_raw(rdma_attr, uverb_attr->dgid);
1765 	} else {
1766 		rdma_ah_set_ah_flags(rdma_attr, 0);
1767 	}
1768 	rdma_ah_set_dlid(rdma_attr, uverb_attr->dlid);
1769 	rdma_ah_set_sl(rdma_attr, uverb_attr->sl);
1770 	rdma_ah_set_path_bits(rdma_attr, uverb_attr->src_path_bits);
1771 	rdma_ah_set_static_rate(rdma_attr, uverb_attr->static_rate);
1772 	rdma_ah_set_port_num(rdma_attr, uverb_attr->port_num);
1773 	rdma_ah_set_make_grd(rdma_attr, false);
1774 }
1775 
modify_qp(struct uverbs_attr_bundle * attrs,struct ib_uverbs_ex_modify_qp * cmd)1776 static int modify_qp(struct uverbs_attr_bundle *attrs,
1777 		     struct ib_uverbs_ex_modify_qp *cmd)
1778 {
1779 	struct ib_qp_attr *attr;
1780 	struct ib_qp *qp;
1781 	int ret;
1782 
1783 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1784 	if (!attr)
1785 		return -ENOMEM;
1786 
1787 	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd->base.qp_handle,
1788 			       attrs);
1789 	if (IS_ERR(qp)) {
1790 		ret = PTR_ERR(qp);
1791 		goto out;
1792 	}
1793 
1794 	if ((cmd->base.attr_mask & IB_QP_PORT) &&
1795 	    !rdma_is_port_valid(qp->device, cmd->base.port_num)) {
1796 		ret = -EINVAL;
1797 		goto release_qp;
1798 	}
1799 
1800 	if ((cmd->base.attr_mask & IB_QP_AV)) {
1801 		if (!rdma_is_port_valid(qp->device, cmd->base.dest.port_num)) {
1802 			ret = -EINVAL;
1803 			goto release_qp;
1804 		}
1805 
1806 		if (cmd->base.attr_mask & IB_QP_STATE &&
1807 		    cmd->base.qp_state == IB_QPS_RTR) {
1808 		/* We are in INIT->RTR TRANSITION (if we are not,
1809 		 * this transition will be rejected in subsequent checks).
1810 		 * In the INIT->RTR transition, we cannot have IB_QP_PORT set,
1811 		 * but the IB_QP_STATE flag is required.
1812 		 *
1813 		 * Since kernel 3.14 (commit dbf727de7440), the uverbs driver,
1814 		 * when IB_QP_AV is set, has required inclusion of a valid
1815 		 * port number in the primary AV. (AVs are created and handled
1816 		 * differently for infiniband and ethernet (RoCE) ports).
1817 		 *
1818 		 * Check the port number included in the primary AV against
1819 		 * the port number in the qp struct, which was set (and saved)
1820 		 * in the RST->INIT transition.
1821 		 */
1822 			if (cmd->base.dest.port_num != qp->real_qp->port) {
1823 				ret = -EINVAL;
1824 				goto release_qp;
1825 			}
1826 		} else {
1827 		/* We are in SQD->SQD. (If we are not, this transition will
1828 		 * be rejected later in the verbs layer checks).
1829 		 * Check for both IB_QP_PORT and IB_QP_AV, these can be set
1830 		 * together in the SQD->SQD transition.
1831 		 *
1832 		 * If only IP_QP_AV was set, add in IB_QP_PORT as well (the
1833 		 * verbs layer driver does not track primary port changes
1834 		 * resulting from path migration. Thus, in SQD, if the primary
1835 		 * AV is modified, the primary port should also be modified).
1836 		 *
1837 		 * Note that in this transition, the IB_QP_STATE flag
1838 		 * is not allowed.
1839 		 */
1840 			if (((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
1841 			     == (IB_QP_AV | IB_QP_PORT)) &&
1842 			    cmd->base.port_num != cmd->base.dest.port_num) {
1843 				ret = -EINVAL;
1844 				goto release_qp;
1845 			}
1846 			if ((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
1847 			    == IB_QP_AV) {
1848 				cmd->base.attr_mask |= IB_QP_PORT;
1849 				cmd->base.port_num = cmd->base.dest.port_num;
1850 			}
1851 		}
1852 	}
1853 
1854 	if ((cmd->base.attr_mask & IB_QP_ALT_PATH) &&
1855 	    (!rdma_is_port_valid(qp->device, cmd->base.alt_port_num) ||
1856 	    !rdma_is_port_valid(qp->device, cmd->base.alt_dest.port_num) ||
1857 	    cmd->base.alt_port_num != cmd->base.alt_dest.port_num)) {
1858 		ret = -EINVAL;
1859 		goto release_qp;
1860 	}
1861 
1862 	if ((cmd->base.attr_mask & IB_QP_CUR_STATE &&
1863 	    cmd->base.cur_qp_state > IB_QPS_ERR) ||
1864 	    (cmd->base.attr_mask & IB_QP_STATE &&
1865 	    cmd->base.qp_state > IB_QPS_ERR)) {
1866 		ret = -EINVAL;
1867 		goto release_qp;
1868 	}
1869 
1870 	if (cmd->base.attr_mask & IB_QP_STATE)
1871 		attr->qp_state = cmd->base.qp_state;
1872 	if (cmd->base.attr_mask & IB_QP_CUR_STATE)
1873 		attr->cur_qp_state = cmd->base.cur_qp_state;
1874 	if (cmd->base.attr_mask & IB_QP_PATH_MTU)
1875 		attr->path_mtu = cmd->base.path_mtu;
1876 	if (cmd->base.attr_mask & IB_QP_PATH_MIG_STATE)
1877 		attr->path_mig_state = cmd->base.path_mig_state;
1878 	if (cmd->base.attr_mask & IB_QP_QKEY) {
1879 		if (cmd->base.qkey & IB_QP_SET_QKEY &&
1880 		    !(rdma_nl_get_privileged_qkey() ||
1881 		      rdma_uattrs_has_raw_cap(attrs))) {
1882 			ret = -EPERM;
1883 			goto release_qp;
1884 		}
1885 		attr->qkey = cmd->base.qkey;
1886 	}
1887 	if (cmd->base.attr_mask & IB_QP_RQ_PSN)
1888 		attr->rq_psn = cmd->base.rq_psn;
1889 	if (cmd->base.attr_mask & IB_QP_SQ_PSN)
1890 		attr->sq_psn = cmd->base.sq_psn;
1891 	if (cmd->base.attr_mask & IB_QP_DEST_QPN)
1892 		attr->dest_qp_num = cmd->base.dest_qp_num;
1893 	if (cmd->base.attr_mask & IB_QP_ACCESS_FLAGS)
1894 		attr->qp_access_flags = cmd->base.qp_access_flags;
1895 	if (cmd->base.attr_mask & IB_QP_PKEY_INDEX)
1896 		attr->pkey_index = cmd->base.pkey_index;
1897 	if (cmd->base.attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
1898 		attr->en_sqd_async_notify = cmd->base.en_sqd_async_notify;
1899 	if (cmd->base.attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1900 		attr->max_rd_atomic = cmd->base.max_rd_atomic;
1901 	if (cmd->base.attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1902 		attr->max_dest_rd_atomic = cmd->base.max_dest_rd_atomic;
1903 	if (cmd->base.attr_mask & IB_QP_MIN_RNR_TIMER)
1904 		attr->min_rnr_timer = cmd->base.min_rnr_timer;
1905 	if (cmd->base.attr_mask & IB_QP_PORT)
1906 		attr->port_num = cmd->base.port_num;
1907 	if (cmd->base.attr_mask & IB_QP_TIMEOUT)
1908 		attr->timeout = cmd->base.timeout;
1909 	if (cmd->base.attr_mask & IB_QP_RETRY_CNT)
1910 		attr->retry_cnt = cmd->base.retry_cnt;
1911 	if (cmd->base.attr_mask & IB_QP_RNR_RETRY)
1912 		attr->rnr_retry = cmd->base.rnr_retry;
1913 	if (cmd->base.attr_mask & IB_QP_ALT_PATH) {
1914 		attr->alt_port_num = cmd->base.alt_port_num;
1915 		attr->alt_timeout = cmd->base.alt_timeout;
1916 		attr->alt_pkey_index = cmd->base.alt_pkey_index;
1917 	}
1918 	if (cmd->base.attr_mask & IB_QP_RATE_LIMIT)
1919 		attr->rate_limit = cmd->rate_limit;
1920 
1921 	if (cmd->base.attr_mask & IB_QP_AV)
1922 		copy_ah_attr_from_uverbs(qp->device, &attr->ah_attr,
1923 					 &cmd->base.dest);
1924 
1925 	if (cmd->base.attr_mask & IB_QP_ALT_PATH)
1926 		copy_ah_attr_from_uverbs(qp->device, &attr->alt_ah_attr,
1927 					 &cmd->base.alt_dest);
1928 
1929 	ret = ib_modify_qp_with_udata(qp, attr,
1930 				      modify_qp_mask(qp->qp_type,
1931 						     cmd->base.attr_mask),
1932 				      &attrs->driver_udata);
1933 
1934 release_qp:
1935 	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
1936 				UVERBS_LOOKUP_READ);
1937 out:
1938 	kfree(attr);
1939 
1940 	return ret;
1941 }
1942 
ib_uverbs_modify_qp(struct uverbs_attr_bundle * attrs)1943 static int ib_uverbs_modify_qp(struct uverbs_attr_bundle *attrs)
1944 {
1945 	struct ib_uverbs_ex_modify_qp cmd;
1946 	int ret;
1947 
1948 	ret = uverbs_request(attrs, &cmd.base, sizeof(cmd.base));
1949 	if (ret)
1950 		return ret;
1951 
1952 	if (cmd.base.attr_mask & ~IB_QP_ATTR_STANDARD_BITS)
1953 		return -EOPNOTSUPP;
1954 
1955 	return modify_qp(attrs, &cmd);
1956 }
1957 
ib_uverbs_ex_modify_qp(struct uverbs_attr_bundle * attrs)1958 static int ib_uverbs_ex_modify_qp(struct uverbs_attr_bundle *attrs)
1959 {
1960 	struct ib_uverbs_ex_modify_qp cmd;
1961 	struct ib_uverbs_ex_modify_qp_resp resp = {
1962 		.response_length = uverbs_response_length(attrs, sizeof(resp))
1963 	};
1964 	int ret;
1965 
1966 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1967 	if (ret)
1968 		return ret;
1969 
1970 	/*
1971 	 * Last bit is reserved for extending the attr_mask by
1972 	 * using another field.
1973 	 */
1974 	if (cmd.base.attr_mask & ~(IB_QP_ATTR_STANDARD_BITS | IB_QP_RATE_LIMIT))
1975 		return -EOPNOTSUPP;
1976 
1977 	ret = modify_qp(attrs, &cmd);
1978 	if (ret)
1979 		return ret;
1980 
1981 	return uverbs_response(attrs, &resp, sizeof(resp));
1982 }
1983 
ib_uverbs_destroy_qp(struct uverbs_attr_bundle * attrs)1984 static int ib_uverbs_destroy_qp(struct uverbs_attr_bundle *attrs)
1985 {
1986 	struct ib_uverbs_destroy_qp      cmd;
1987 	struct ib_uverbs_destroy_qp_resp resp;
1988 	struct ib_uobject		*uobj;
1989 	struct ib_uqp_object        	*obj;
1990 	int ret;
1991 
1992 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1993 	if (ret)
1994 		return ret;
1995 
1996 	uobj = uobj_get_destroy(UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
1997 	if (IS_ERR(uobj))
1998 		return PTR_ERR(uobj);
1999 
2000 	obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
2001 	memset(&resp, 0, sizeof(resp));
2002 	resp.events_reported = obj->uevent.events_reported;
2003 
2004 	uobj_put_destroy(uobj);
2005 
2006 	return uverbs_response(attrs, &resp, sizeof(resp));
2007 }
2008 
alloc_wr(size_t wr_size,__u32 num_sge)2009 static void *alloc_wr(size_t wr_size, __u32 num_sge)
2010 {
2011 	if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof(struct ib_sge))) /
2012 			       sizeof(struct ib_sge))
2013 		return NULL;
2014 
2015 	return kmalloc(ALIGN(wr_size, sizeof(struct ib_sge)) +
2016 			       num_sge * sizeof(struct ib_sge),
2017 		       GFP_KERNEL);
2018 }
2019 
ib_uverbs_post_send(struct uverbs_attr_bundle * attrs)2020 static int ib_uverbs_post_send(struct uverbs_attr_bundle *attrs)
2021 {
2022 	struct ib_uverbs_post_send      cmd;
2023 	struct ib_uverbs_post_send_resp resp;
2024 	struct ib_uverbs_send_wr       *user_wr;
2025 	struct ib_send_wr              *wr = NULL, *last, *next;
2026 	const struct ib_send_wr	       *bad_wr;
2027 	struct ib_qp                   *qp;
2028 	int                             i, sg_ind;
2029 	int				is_ud;
2030 	int ret, ret2;
2031 	size_t                          next_size;
2032 	const struct ib_sge __user *sgls;
2033 	const void __user *wqes;
2034 	struct uverbs_req_iter iter;
2035 
2036 	ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2037 	if (ret)
2038 		return ret;
2039 	wqes = uverbs_request_next_ptr(&iter, size_mul(cmd.wqe_size,
2040 						       cmd.wr_count));
2041 	if (IS_ERR(wqes))
2042 		return PTR_ERR(wqes);
2043 	sgls = uverbs_request_next_ptr(&iter,
2044 				       size_mul(cmd.sge_count,
2045 						sizeof(struct ib_uverbs_sge)));
2046 	if (IS_ERR(sgls))
2047 		return PTR_ERR(sgls);
2048 	ret = uverbs_request_finish(&iter);
2049 	if (ret)
2050 		return ret;
2051 
2052 	user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2053 	if (!user_wr)
2054 		return -ENOMEM;
2055 
2056 	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2057 	if (IS_ERR(qp)) {
2058 		ret = PTR_ERR(qp);
2059 		goto out;
2060 	}
2061 
2062 	is_ud = qp->qp_type == IB_QPT_UD;
2063 	sg_ind = 0;
2064 	last = NULL;
2065 	for (i = 0; i < cmd.wr_count; ++i) {
2066 		if (copy_from_user(user_wr, wqes + i * cmd.wqe_size,
2067 				   cmd.wqe_size)) {
2068 			ret = -EFAULT;
2069 			goto out_put;
2070 		}
2071 
2072 		if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2073 			ret = -EINVAL;
2074 			goto out_put;
2075 		}
2076 
2077 		if (is_ud) {
2078 			struct ib_ud_wr *ud;
2079 
2080 			if (user_wr->opcode != IB_WR_SEND &&
2081 			    user_wr->opcode != IB_WR_SEND_WITH_IMM) {
2082 				ret = -EINVAL;
2083 				goto out_put;
2084 			}
2085 
2086 			next_size = sizeof(*ud);
2087 			ud = alloc_wr(next_size, user_wr->num_sge);
2088 			if (!ud) {
2089 				ret = -ENOMEM;
2090 				goto out_put;
2091 			}
2092 
2093 			ud->ah = uobj_get_obj_read(ah, UVERBS_OBJECT_AH,
2094 						   user_wr->wr.ud.ah, attrs);
2095 			if (IS_ERR(ud->ah)) {
2096 				ret = PTR_ERR(ud->ah);
2097 				kfree(ud);
2098 				goto out_put;
2099 			}
2100 			ud->remote_qpn = user_wr->wr.ud.remote_qpn;
2101 			ud->remote_qkey = user_wr->wr.ud.remote_qkey;
2102 
2103 			next = &ud->wr;
2104 		} else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
2105 			   user_wr->opcode == IB_WR_RDMA_WRITE ||
2106 			   user_wr->opcode == IB_WR_RDMA_READ) {
2107 			struct ib_rdma_wr *rdma;
2108 
2109 			next_size = sizeof(*rdma);
2110 			rdma = alloc_wr(next_size, user_wr->num_sge);
2111 			if (!rdma) {
2112 				ret = -ENOMEM;
2113 				goto out_put;
2114 			}
2115 
2116 			rdma->remote_addr = user_wr->wr.rdma.remote_addr;
2117 			rdma->rkey = user_wr->wr.rdma.rkey;
2118 
2119 			next = &rdma->wr;
2120 		} else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
2121 			   user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
2122 			struct ib_atomic_wr *atomic;
2123 
2124 			next_size = sizeof(*atomic);
2125 			atomic = alloc_wr(next_size, user_wr->num_sge);
2126 			if (!atomic) {
2127 				ret = -ENOMEM;
2128 				goto out_put;
2129 			}
2130 
2131 			atomic->remote_addr = user_wr->wr.atomic.remote_addr;
2132 			atomic->compare_add = user_wr->wr.atomic.compare_add;
2133 			atomic->swap = user_wr->wr.atomic.swap;
2134 			atomic->rkey = user_wr->wr.atomic.rkey;
2135 
2136 			next = &atomic->wr;
2137 		} else if (user_wr->opcode == IB_WR_SEND ||
2138 			   user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2139 			   user_wr->opcode == IB_WR_SEND_WITH_INV) {
2140 			next_size = sizeof(*next);
2141 			next = alloc_wr(next_size, user_wr->num_sge);
2142 			if (!next) {
2143 				ret = -ENOMEM;
2144 				goto out_put;
2145 			}
2146 		} else {
2147 			ret = -EINVAL;
2148 			goto out_put;
2149 		}
2150 
2151 		if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2152 		    user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
2153 			next->ex.imm_data =
2154 					(__be32 __force) user_wr->ex.imm_data;
2155 		} else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
2156 			next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
2157 		}
2158 
2159 		if (!last)
2160 			wr = next;
2161 		else
2162 			last->next = next;
2163 		last = next;
2164 
2165 		next->next       = NULL;
2166 		next->wr_id      = user_wr->wr_id;
2167 		next->num_sge    = user_wr->num_sge;
2168 		next->opcode     = user_wr->opcode;
2169 		next->send_flags = user_wr->send_flags;
2170 
2171 		if (next->num_sge) {
2172 			next->sg_list = (void *) next +
2173 				ALIGN(next_size, sizeof(struct ib_sge));
2174 			if (copy_from_user(next->sg_list, sgls + sg_ind,
2175 					   next->num_sge *
2176 						   sizeof(struct ib_sge))) {
2177 				ret = -EFAULT;
2178 				goto out_put;
2179 			}
2180 			sg_ind += next->num_sge;
2181 		} else
2182 			next->sg_list = NULL;
2183 	}
2184 
2185 	resp.bad_wr = 0;
2186 	ret = qp->device->ops.post_send(qp->real_qp, wr, &bad_wr);
2187 	if (ret)
2188 		for (next = wr; next; next = next->next) {
2189 			++resp.bad_wr;
2190 			if (next == bad_wr)
2191 				break;
2192 		}
2193 
2194 	ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2195 	if (ret2)
2196 		ret = ret2;
2197 
2198 out_put:
2199 	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2200 				UVERBS_LOOKUP_READ);
2201 
2202 	while (wr) {
2203 		if (is_ud && ud_wr(wr)->ah)
2204 			uobj_put_obj_read(ud_wr(wr)->ah);
2205 		next = wr->next;
2206 		kfree(wr);
2207 		wr = next;
2208 	}
2209 
2210 out:
2211 	kfree(user_wr);
2212 
2213 	return ret;
2214 }
2215 
2216 static struct ib_recv_wr *
ib_uverbs_unmarshall_recv(struct uverbs_req_iter * iter,u32 wr_count,u32 wqe_size,u32 sge_count)2217 ib_uverbs_unmarshall_recv(struct uverbs_req_iter *iter, u32 wr_count,
2218 			  u32 wqe_size, u32 sge_count)
2219 {
2220 	struct ib_uverbs_recv_wr *user_wr;
2221 	struct ib_recv_wr        *wr = NULL, *last, *next;
2222 	int                       sg_ind;
2223 	int                       i;
2224 	int                       ret;
2225 	const struct ib_sge __user *sgls;
2226 	const void __user *wqes;
2227 
2228 	if (wqe_size < sizeof(struct ib_uverbs_recv_wr))
2229 		return ERR_PTR(-EINVAL);
2230 
2231 	wqes = uverbs_request_next_ptr(iter, size_mul(wqe_size, wr_count));
2232 	if (IS_ERR(wqes))
2233 		return ERR_CAST(wqes);
2234 	sgls = uverbs_request_next_ptr(iter, size_mul(sge_count,
2235 						      sizeof(struct ib_uverbs_sge)));
2236 	if (IS_ERR(sgls))
2237 		return ERR_CAST(sgls);
2238 	ret = uverbs_request_finish(iter);
2239 	if (ret)
2240 		return ERR_PTR(ret);
2241 
2242 	user_wr = kmalloc(wqe_size, GFP_KERNEL);
2243 	if (!user_wr)
2244 		return ERR_PTR(-ENOMEM);
2245 
2246 	sg_ind = 0;
2247 	last = NULL;
2248 	for (i = 0; i < wr_count; ++i) {
2249 		if (copy_from_user(user_wr, wqes + i * wqe_size,
2250 				   wqe_size)) {
2251 			ret = -EFAULT;
2252 			goto err;
2253 		}
2254 
2255 		if (user_wr->num_sge + sg_ind > sge_count) {
2256 			ret = -EINVAL;
2257 			goto err;
2258 		}
2259 
2260 		if (user_wr->num_sge >=
2261 		    (U32_MAX - ALIGN(sizeof(*next), sizeof(struct ib_sge))) /
2262 			    sizeof(struct ib_sge)) {
2263 			ret = -EINVAL;
2264 			goto err;
2265 		}
2266 
2267 		next = kmalloc(ALIGN(sizeof(*next), sizeof(struct ib_sge)) +
2268 				       user_wr->num_sge * sizeof(struct ib_sge),
2269 			       GFP_KERNEL);
2270 		if (!next) {
2271 			ret = -ENOMEM;
2272 			goto err;
2273 		}
2274 
2275 		if (!last)
2276 			wr = next;
2277 		else
2278 			last->next = next;
2279 		last = next;
2280 
2281 		next->next       = NULL;
2282 		next->wr_id      = user_wr->wr_id;
2283 		next->num_sge    = user_wr->num_sge;
2284 
2285 		if (next->num_sge) {
2286 			next->sg_list = (void *)next +
2287 				ALIGN(sizeof(*next), sizeof(struct ib_sge));
2288 			if (copy_from_user(next->sg_list, sgls + sg_ind,
2289 					   next->num_sge *
2290 						   sizeof(struct ib_sge))) {
2291 				ret = -EFAULT;
2292 				goto err;
2293 			}
2294 			sg_ind += next->num_sge;
2295 		} else
2296 			next->sg_list = NULL;
2297 	}
2298 
2299 	kfree(user_wr);
2300 	return wr;
2301 
2302 err:
2303 	kfree(user_wr);
2304 
2305 	while (wr) {
2306 		next = wr->next;
2307 		kfree(wr);
2308 		wr = next;
2309 	}
2310 
2311 	return ERR_PTR(ret);
2312 }
2313 
ib_uverbs_post_recv(struct uverbs_attr_bundle * attrs)2314 static int ib_uverbs_post_recv(struct uverbs_attr_bundle *attrs)
2315 {
2316 	struct ib_uverbs_post_recv      cmd;
2317 	struct ib_uverbs_post_recv_resp resp;
2318 	struct ib_recv_wr              *wr, *next;
2319 	const struct ib_recv_wr	       *bad_wr;
2320 	struct ib_qp                   *qp;
2321 	int ret, ret2;
2322 	struct uverbs_req_iter iter;
2323 
2324 	ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2325 	if (ret)
2326 		return ret;
2327 
2328 	wr = ib_uverbs_unmarshall_recv(&iter, cmd.wr_count, cmd.wqe_size,
2329 				       cmd.sge_count);
2330 	if (IS_ERR(wr))
2331 		return PTR_ERR(wr);
2332 
2333 	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2334 	if (IS_ERR(qp)) {
2335 		ret = PTR_ERR(qp);
2336 		goto out;
2337 	}
2338 
2339 	resp.bad_wr = 0;
2340 	ret = qp->device->ops.post_recv(qp->real_qp, wr, &bad_wr);
2341 
2342 	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2343 				UVERBS_LOOKUP_READ);
2344 	if (ret) {
2345 		for (next = wr; next; next = next->next) {
2346 			++resp.bad_wr;
2347 			if (next == bad_wr)
2348 				break;
2349 		}
2350 	}
2351 
2352 	ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2353 	if (ret2)
2354 		ret = ret2;
2355 out:
2356 	while (wr) {
2357 		next = wr->next;
2358 		kfree(wr);
2359 		wr = next;
2360 	}
2361 
2362 	return ret;
2363 }
2364 
ib_uverbs_post_srq_recv(struct uverbs_attr_bundle * attrs)2365 static int ib_uverbs_post_srq_recv(struct uverbs_attr_bundle *attrs)
2366 {
2367 	struct ib_uverbs_post_srq_recv      cmd;
2368 	struct ib_uverbs_post_srq_recv_resp resp;
2369 	struct ib_recv_wr                  *wr, *next;
2370 	const struct ib_recv_wr		   *bad_wr;
2371 	struct ib_srq                      *srq;
2372 	int ret, ret2;
2373 	struct uverbs_req_iter iter;
2374 
2375 	ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2376 	if (ret)
2377 		return ret;
2378 
2379 	wr = ib_uverbs_unmarshall_recv(&iter, cmd.wr_count, cmd.wqe_size,
2380 				       cmd.sge_count);
2381 	if (IS_ERR(wr))
2382 		return PTR_ERR(wr);
2383 
2384 	srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
2385 	if (IS_ERR(srq)) {
2386 		ret = PTR_ERR(srq);
2387 		goto out;
2388 	}
2389 
2390 	resp.bad_wr = 0;
2391 	ret = srq->device->ops.post_srq_recv(srq, wr, &bad_wr);
2392 
2393 	rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
2394 				UVERBS_LOOKUP_READ);
2395 
2396 	if (ret)
2397 		for (next = wr; next; next = next->next) {
2398 			++resp.bad_wr;
2399 			if (next == bad_wr)
2400 				break;
2401 		}
2402 
2403 	ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2404 	if (ret2)
2405 		ret = ret2;
2406 
2407 out:
2408 	while (wr) {
2409 		next = wr->next;
2410 		kfree(wr);
2411 		wr = next;
2412 	}
2413 
2414 	return ret;
2415 }
2416 
ib_uverbs_create_ah(struct uverbs_attr_bundle * attrs)2417 static int ib_uverbs_create_ah(struct uverbs_attr_bundle *attrs)
2418 {
2419 	struct ib_uverbs_create_ah	 cmd;
2420 	struct ib_uverbs_create_ah_resp	 resp;
2421 	struct ib_uobject		*uobj;
2422 	struct ib_pd			*pd;
2423 	struct ib_ah			*ah;
2424 	struct rdma_ah_attr		attr = {};
2425 	int ret;
2426 	struct ib_device *ib_dev;
2427 
2428 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2429 	if (ret)
2430 		return ret;
2431 
2432 	uobj = uobj_alloc(UVERBS_OBJECT_AH, attrs, &ib_dev);
2433 	if (IS_ERR(uobj))
2434 		return PTR_ERR(uobj);
2435 
2436 	if (!rdma_is_port_valid(ib_dev, cmd.attr.port_num)) {
2437 		ret = -EINVAL;
2438 		goto err;
2439 	}
2440 
2441 	pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
2442 	if (IS_ERR(pd)) {
2443 		ret = PTR_ERR(pd);
2444 		goto err;
2445 	}
2446 
2447 	attr.type = rdma_ah_find_type(ib_dev, cmd.attr.port_num);
2448 	rdma_ah_set_make_grd(&attr, false);
2449 	rdma_ah_set_dlid(&attr, cmd.attr.dlid);
2450 	rdma_ah_set_sl(&attr, cmd.attr.sl);
2451 	rdma_ah_set_path_bits(&attr, cmd.attr.src_path_bits);
2452 	rdma_ah_set_static_rate(&attr, cmd.attr.static_rate);
2453 	rdma_ah_set_port_num(&attr, cmd.attr.port_num);
2454 
2455 	if (cmd.attr.is_global) {
2456 		rdma_ah_set_grh(&attr, NULL, cmd.attr.grh.flow_label,
2457 				cmd.attr.grh.sgid_index,
2458 				cmd.attr.grh.hop_limit,
2459 				cmd.attr.grh.traffic_class);
2460 		rdma_ah_set_dgid_raw(&attr, cmd.attr.grh.dgid);
2461 	} else {
2462 		rdma_ah_set_ah_flags(&attr, 0);
2463 	}
2464 
2465 	ah = rdma_create_user_ah(pd, &attr, &attrs->driver_udata);
2466 	if (IS_ERR(ah)) {
2467 		ret = PTR_ERR(ah);
2468 		goto err_put;
2469 	}
2470 
2471 	ah->uobject  = uobj;
2472 	uobj->user_handle = cmd.user_handle;
2473 	uobj->object = ah;
2474 	uobj_put_obj_read(pd);
2475 	uobj_finalize_uobj_create(uobj, attrs);
2476 
2477 	resp.ah_handle = uobj->id;
2478 	return uverbs_response(attrs, &resp, sizeof(resp));
2479 
2480 err_put:
2481 	uobj_put_obj_read(pd);
2482 err:
2483 	uobj_alloc_abort(uobj, attrs);
2484 	return ret;
2485 }
2486 
ib_uverbs_destroy_ah(struct uverbs_attr_bundle * attrs)2487 static int ib_uverbs_destroy_ah(struct uverbs_attr_bundle *attrs)
2488 {
2489 	struct ib_uverbs_destroy_ah cmd;
2490 	int ret;
2491 
2492 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2493 	if (ret)
2494 		return ret;
2495 
2496 	return uobj_perform_destroy(UVERBS_OBJECT_AH, cmd.ah_handle, attrs);
2497 }
2498 
ib_uverbs_attach_mcast(struct uverbs_attr_bundle * attrs)2499 static int ib_uverbs_attach_mcast(struct uverbs_attr_bundle *attrs)
2500 {
2501 	struct ib_uverbs_attach_mcast cmd;
2502 	struct ib_qp                 *qp;
2503 	struct ib_uqp_object         *obj;
2504 	struct ib_uverbs_mcast_entry *mcast;
2505 	int                           ret;
2506 
2507 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2508 	if (ret)
2509 		return ret;
2510 
2511 	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2512 	if (IS_ERR(qp))
2513 		return PTR_ERR(qp);
2514 
2515 	obj = qp->uobject;
2516 
2517 	mutex_lock(&obj->mcast_lock);
2518 	list_for_each_entry(mcast, &obj->mcast_list, list)
2519 		if (cmd.mlid == mcast->lid &&
2520 		    !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2521 			ret = 0;
2522 			goto out_put;
2523 		}
2524 
2525 	mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2526 	if (!mcast) {
2527 		ret = -ENOMEM;
2528 		goto out_put;
2529 	}
2530 
2531 	mcast->lid = cmd.mlid;
2532 	memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2533 
2534 	ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2535 	if (!ret)
2536 		list_add_tail(&mcast->list, &obj->mcast_list);
2537 	else
2538 		kfree(mcast);
2539 
2540 out_put:
2541 	mutex_unlock(&obj->mcast_lock);
2542 	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2543 				UVERBS_LOOKUP_READ);
2544 
2545 	return ret;
2546 }
2547 
ib_uverbs_detach_mcast(struct uverbs_attr_bundle * attrs)2548 static int ib_uverbs_detach_mcast(struct uverbs_attr_bundle *attrs)
2549 {
2550 	struct ib_uverbs_detach_mcast cmd;
2551 	struct ib_uqp_object         *obj;
2552 	struct ib_qp                 *qp;
2553 	struct ib_uverbs_mcast_entry *mcast;
2554 	int                           ret;
2555 	bool                          found = false;
2556 
2557 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2558 	if (ret)
2559 		return ret;
2560 
2561 	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2562 	if (IS_ERR(qp))
2563 		return PTR_ERR(qp);
2564 
2565 	obj = qp->uobject;
2566 	mutex_lock(&obj->mcast_lock);
2567 
2568 	list_for_each_entry(mcast, &obj->mcast_list, list)
2569 		if (cmd.mlid == mcast->lid &&
2570 		    !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2571 			list_del(&mcast->list);
2572 			kfree(mcast);
2573 			found = true;
2574 			break;
2575 		}
2576 
2577 	if (!found) {
2578 		ret = -EINVAL;
2579 		goto out_put;
2580 	}
2581 
2582 	ret = ib_detach_mcast(qp, (union ib_gid *)cmd.gid, cmd.mlid);
2583 
2584 out_put:
2585 	mutex_unlock(&obj->mcast_lock);
2586 	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2587 				UVERBS_LOOKUP_READ);
2588 	return ret;
2589 }
2590 
flow_resources_alloc(size_t num_specs)2591 struct ib_uflow_resources *flow_resources_alloc(size_t num_specs)
2592 {
2593 	struct ib_uflow_resources *resources;
2594 
2595 	resources = kzalloc(sizeof(*resources), GFP_KERNEL);
2596 
2597 	if (!resources)
2598 		return NULL;
2599 
2600 	if (!num_specs)
2601 		goto out;
2602 
2603 	resources->counters =
2604 		kcalloc(num_specs, sizeof(*resources->counters), GFP_KERNEL);
2605 	resources->collection =
2606 		kcalloc(num_specs, sizeof(*resources->collection), GFP_KERNEL);
2607 
2608 	if (!resources->counters || !resources->collection)
2609 		goto err;
2610 
2611 out:
2612 	resources->max = num_specs;
2613 	return resources;
2614 
2615 err:
2616 	kfree(resources->counters);
2617 	kfree(resources);
2618 
2619 	return NULL;
2620 }
2621 EXPORT_SYMBOL(flow_resources_alloc);
2622 
ib_uverbs_flow_resources_free(struct ib_uflow_resources * uflow_res)2623 void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res)
2624 {
2625 	unsigned int i;
2626 
2627 	if (!uflow_res)
2628 		return;
2629 
2630 	for (i = 0; i < uflow_res->collection_num; i++)
2631 		atomic_dec(&uflow_res->collection[i]->usecnt);
2632 
2633 	for (i = 0; i < uflow_res->counters_num; i++)
2634 		atomic_dec(&uflow_res->counters[i]->usecnt);
2635 
2636 	kfree(uflow_res->collection);
2637 	kfree(uflow_res->counters);
2638 	kfree(uflow_res);
2639 }
2640 EXPORT_SYMBOL(ib_uverbs_flow_resources_free);
2641 
flow_resources_add(struct ib_uflow_resources * uflow_res,enum ib_flow_spec_type type,void * ibobj)2642 void flow_resources_add(struct ib_uflow_resources *uflow_res,
2643 			enum ib_flow_spec_type type,
2644 			void *ibobj)
2645 {
2646 	WARN_ON(uflow_res->num >= uflow_res->max);
2647 
2648 	switch (type) {
2649 	case IB_FLOW_SPEC_ACTION_HANDLE:
2650 		atomic_inc(&((struct ib_flow_action *)ibobj)->usecnt);
2651 		uflow_res->collection[uflow_res->collection_num++] =
2652 			(struct ib_flow_action *)ibobj;
2653 		break;
2654 	case IB_FLOW_SPEC_ACTION_COUNT:
2655 		atomic_inc(&((struct ib_counters *)ibobj)->usecnt);
2656 		uflow_res->counters[uflow_res->counters_num++] =
2657 			(struct ib_counters *)ibobj;
2658 		break;
2659 	default:
2660 		WARN_ON(1);
2661 	}
2662 
2663 	uflow_res->num++;
2664 }
2665 EXPORT_SYMBOL(flow_resources_add);
2666 
kern_spec_to_ib_spec_action(struct uverbs_attr_bundle * attrs,struct ib_uverbs_flow_spec * kern_spec,union ib_flow_spec * ib_spec,struct ib_uflow_resources * uflow_res)2667 static int kern_spec_to_ib_spec_action(struct uverbs_attr_bundle *attrs,
2668 				       struct ib_uverbs_flow_spec *kern_spec,
2669 				       union ib_flow_spec *ib_spec,
2670 				       struct ib_uflow_resources *uflow_res)
2671 {
2672 	ib_spec->type = kern_spec->type;
2673 	switch (ib_spec->type) {
2674 	case IB_FLOW_SPEC_ACTION_TAG:
2675 		if (kern_spec->flow_tag.size !=
2676 		    sizeof(struct ib_uverbs_flow_spec_action_tag))
2677 			return -EINVAL;
2678 
2679 		ib_spec->flow_tag.size = sizeof(struct ib_flow_spec_action_tag);
2680 		ib_spec->flow_tag.tag_id = kern_spec->flow_tag.tag_id;
2681 		break;
2682 	case IB_FLOW_SPEC_ACTION_DROP:
2683 		if (kern_spec->drop.size !=
2684 		    sizeof(struct ib_uverbs_flow_spec_action_drop))
2685 			return -EINVAL;
2686 
2687 		ib_spec->drop.size = sizeof(struct ib_flow_spec_action_drop);
2688 		break;
2689 	case IB_FLOW_SPEC_ACTION_HANDLE:
2690 		if (kern_spec->action.size !=
2691 		    sizeof(struct ib_uverbs_flow_spec_action_handle))
2692 			return -EOPNOTSUPP;
2693 		ib_spec->action.act = uobj_get_obj_read(flow_action,
2694 							UVERBS_OBJECT_FLOW_ACTION,
2695 							kern_spec->action.handle,
2696 							attrs);
2697 		if (IS_ERR(ib_spec->action.act))
2698 			return PTR_ERR(ib_spec->action.act);
2699 		ib_spec->action.size =
2700 			sizeof(struct ib_flow_spec_action_handle);
2701 		flow_resources_add(uflow_res,
2702 				   IB_FLOW_SPEC_ACTION_HANDLE,
2703 				   ib_spec->action.act);
2704 		uobj_put_obj_read(ib_spec->action.act);
2705 		break;
2706 	case IB_FLOW_SPEC_ACTION_COUNT:
2707 		if (kern_spec->flow_count.size !=
2708 			sizeof(struct ib_uverbs_flow_spec_action_count))
2709 			return -EINVAL;
2710 		ib_spec->flow_count.counters =
2711 			uobj_get_obj_read(counters,
2712 					  UVERBS_OBJECT_COUNTERS,
2713 					  kern_spec->flow_count.handle,
2714 					  attrs);
2715 		if (IS_ERR(ib_spec->flow_count.counters))
2716 			return PTR_ERR(ib_spec->flow_count.counters);
2717 		ib_spec->flow_count.size =
2718 				sizeof(struct ib_flow_spec_action_count);
2719 		flow_resources_add(uflow_res,
2720 				   IB_FLOW_SPEC_ACTION_COUNT,
2721 				   ib_spec->flow_count.counters);
2722 		uobj_put_obj_read(ib_spec->flow_count.counters);
2723 		break;
2724 	default:
2725 		return -EINVAL;
2726 	}
2727 	return 0;
2728 }
2729 
spec_filter_size(const void * kern_spec_filter,u16 kern_filter_size,u16 ib_real_filter_sz)2730 static ssize_t spec_filter_size(const void *kern_spec_filter, u16 kern_filter_size,
2731 				u16 ib_real_filter_sz)
2732 {
2733 	/*
2734 	 * User space filter structures must be 64 bit aligned, otherwise this
2735 	 * may pass, but we won't handle additional new attributes.
2736 	 */
2737 
2738 	if (kern_filter_size > ib_real_filter_sz) {
2739 		if (memchr_inv(kern_spec_filter +
2740 			       ib_real_filter_sz, 0,
2741 			       kern_filter_size - ib_real_filter_sz))
2742 			return -EINVAL;
2743 		return ib_real_filter_sz;
2744 	}
2745 	return kern_filter_size;
2746 }
2747 
ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,const void * kern_spec_mask,const void * kern_spec_val,size_t kern_filter_sz,union ib_flow_spec * ib_spec)2748 int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
2749 					  const void *kern_spec_mask,
2750 					  const void *kern_spec_val,
2751 					  size_t kern_filter_sz,
2752 					  union ib_flow_spec *ib_spec)
2753 {
2754 	ssize_t actual_filter_sz;
2755 	ssize_t ib_filter_sz;
2756 
2757 	/* User flow spec size must be aligned to 4 bytes */
2758 	if (kern_filter_sz != ALIGN(kern_filter_sz, 4))
2759 		return -EINVAL;
2760 
2761 	ib_spec->type = type;
2762 
2763 	if (ib_spec->type == (IB_FLOW_SPEC_INNER | IB_FLOW_SPEC_VXLAN_TUNNEL))
2764 		return -EINVAL;
2765 
2766 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2767 	case IB_FLOW_SPEC_ETH:
2768 		ib_filter_sz = sizeof(struct ib_flow_eth_filter);
2769 		actual_filter_sz = spec_filter_size(kern_spec_mask,
2770 						    kern_filter_sz,
2771 						    ib_filter_sz);
2772 		if (actual_filter_sz <= 0)
2773 			return -EINVAL;
2774 		ib_spec->size = sizeof(struct ib_flow_spec_eth);
2775 		memcpy(&ib_spec->eth.val, kern_spec_val, actual_filter_sz);
2776 		memcpy(&ib_spec->eth.mask, kern_spec_mask, actual_filter_sz);
2777 		break;
2778 	case IB_FLOW_SPEC_IPV4:
2779 		ib_filter_sz = sizeof(struct ib_flow_ipv4_filter);
2780 		actual_filter_sz = spec_filter_size(kern_spec_mask,
2781 						    kern_filter_sz,
2782 						    ib_filter_sz);
2783 		if (actual_filter_sz <= 0)
2784 			return -EINVAL;
2785 		ib_spec->size = sizeof(struct ib_flow_spec_ipv4);
2786 		memcpy(&ib_spec->ipv4.val, kern_spec_val, actual_filter_sz);
2787 		memcpy(&ib_spec->ipv4.mask, kern_spec_mask, actual_filter_sz);
2788 		break;
2789 	case IB_FLOW_SPEC_IPV6:
2790 		ib_filter_sz = sizeof(struct ib_flow_ipv6_filter);
2791 		actual_filter_sz = spec_filter_size(kern_spec_mask,
2792 						    kern_filter_sz,
2793 						    ib_filter_sz);
2794 		if (actual_filter_sz <= 0)
2795 			return -EINVAL;
2796 		ib_spec->size = sizeof(struct ib_flow_spec_ipv6);
2797 		memcpy(&ib_spec->ipv6.val, kern_spec_val, actual_filter_sz);
2798 		memcpy(&ib_spec->ipv6.mask, kern_spec_mask, actual_filter_sz);
2799 
2800 		if ((ntohl(ib_spec->ipv6.mask.flow_label)) >= BIT(20) ||
2801 		    (ntohl(ib_spec->ipv6.val.flow_label)) >= BIT(20))
2802 			return -EINVAL;
2803 		break;
2804 	case IB_FLOW_SPEC_TCP:
2805 	case IB_FLOW_SPEC_UDP:
2806 		ib_filter_sz = sizeof(struct ib_flow_tcp_udp_filter);
2807 		actual_filter_sz = spec_filter_size(kern_spec_mask,
2808 						    kern_filter_sz,
2809 						    ib_filter_sz);
2810 		if (actual_filter_sz <= 0)
2811 			return -EINVAL;
2812 		ib_spec->size = sizeof(struct ib_flow_spec_tcp_udp);
2813 		memcpy(&ib_spec->tcp_udp.val, kern_spec_val, actual_filter_sz);
2814 		memcpy(&ib_spec->tcp_udp.mask, kern_spec_mask, actual_filter_sz);
2815 		break;
2816 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2817 		ib_filter_sz = sizeof(struct ib_flow_tunnel_filter);
2818 		actual_filter_sz = spec_filter_size(kern_spec_mask,
2819 						    kern_filter_sz,
2820 						    ib_filter_sz);
2821 		if (actual_filter_sz <= 0)
2822 			return -EINVAL;
2823 		ib_spec->tunnel.size = sizeof(struct ib_flow_spec_tunnel);
2824 		memcpy(&ib_spec->tunnel.val, kern_spec_val, actual_filter_sz);
2825 		memcpy(&ib_spec->tunnel.mask, kern_spec_mask, actual_filter_sz);
2826 
2827 		if ((ntohl(ib_spec->tunnel.mask.tunnel_id)) >= BIT(24) ||
2828 		    (ntohl(ib_spec->tunnel.val.tunnel_id)) >= BIT(24))
2829 			return -EINVAL;
2830 		break;
2831 	case IB_FLOW_SPEC_ESP:
2832 		ib_filter_sz = sizeof(struct ib_flow_esp_filter);
2833 		actual_filter_sz = spec_filter_size(kern_spec_mask,
2834 						    kern_filter_sz,
2835 						    ib_filter_sz);
2836 		if (actual_filter_sz <= 0)
2837 			return -EINVAL;
2838 		ib_spec->esp.size = sizeof(struct ib_flow_spec_esp);
2839 		memcpy(&ib_spec->esp.val, kern_spec_val, actual_filter_sz);
2840 		memcpy(&ib_spec->esp.mask, kern_spec_mask, actual_filter_sz);
2841 		break;
2842 	case IB_FLOW_SPEC_GRE:
2843 		ib_filter_sz = sizeof(struct ib_flow_gre_filter);
2844 		actual_filter_sz = spec_filter_size(kern_spec_mask,
2845 						    kern_filter_sz,
2846 						    ib_filter_sz);
2847 		if (actual_filter_sz <= 0)
2848 			return -EINVAL;
2849 		ib_spec->gre.size = sizeof(struct ib_flow_spec_gre);
2850 		memcpy(&ib_spec->gre.val, kern_spec_val, actual_filter_sz);
2851 		memcpy(&ib_spec->gre.mask, kern_spec_mask, actual_filter_sz);
2852 		break;
2853 	case IB_FLOW_SPEC_MPLS:
2854 		ib_filter_sz = sizeof(struct ib_flow_mpls_filter);
2855 		actual_filter_sz = spec_filter_size(kern_spec_mask,
2856 						    kern_filter_sz,
2857 						    ib_filter_sz);
2858 		if (actual_filter_sz <= 0)
2859 			return -EINVAL;
2860 		ib_spec->mpls.size = sizeof(struct ib_flow_spec_mpls);
2861 		memcpy(&ib_spec->mpls.val, kern_spec_val, actual_filter_sz);
2862 		memcpy(&ib_spec->mpls.mask, kern_spec_mask, actual_filter_sz);
2863 		break;
2864 	default:
2865 		return -EINVAL;
2866 	}
2867 	return 0;
2868 }
2869 
kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec * kern_spec,union ib_flow_spec * ib_spec)2870 static int kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec *kern_spec,
2871 				       union ib_flow_spec *ib_spec)
2872 {
2873 	size_t kern_filter_sz;
2874 	void *kern_spec_mask;
2875 	void *kern_spec_val;
2876 
2877 	if (check_sub_overflow((size_t)kern_spec->hdr.size,
2878 			       sizeof(struct ib_uverbs_flow_spec_hdr),
2879 			       &kern_filter_sz))
2880 		return -EINVAL;
2881 
2882 	kern_filter_sz /= 2;
2883 
2884 	kern_spec_val = (void *)kern_spec +
2885 		sizeof(struct ib_uverbs_flow_spec_hdr);
2886 	kern_spec_mask = kern_spec_val + kern_filter_sz;
2887 
2888 	return ib_uverbs_kern_spec_to_ib_spec_filter(kern_spec->type,
2889 						     kern_spec_mask,
2890 						     kern_spec_val,
2891 						     kern_filter_sz, ib_spec);
2892 }
2893 
kern_spec_to_ib_spec(struct uverbs_attr_bundle * attrs,struct ib_uverbs_flow_spec * kern_spec,union ib_flow_spec * ib_spec,struct ib_uflow_resources * uflow_res)2894 static int kern_spec_to_ib_spec(struct uverbs_attr_bundle *attrs,
2895 				struct ib_uverbs_flow_spec *kern_spec,
2896 				union ib_flow_spec *ib_spec,
2897 				struct ib_uflow_resources *uflow_res)
2898 {
2899 	if (kern_spec->reserved)
2900 		return -EINVAL;
2901 
2902 	if (kern_spec->type >= IB_FLOW_SPEC_ACTION_TAG)
2903 		return kern_spec_to_ib_spec_action(attrs, kern_spec, ib_spec,
2904 						   uflow_res);
2905 	else
2906 		return kern_spec_to_ib_spec_filter(kern_spec, ib_spec);
2907 }
2908 
ib_uverbs_ex_create_wq(struct uverbs_attr_bundle * attrs)2909 static int ib_uverbs_ex_create_wq(struct uverbs_attr_bundle *attrs)
2910 {
2911 	struct ib_uverbs_ex_create_wq cmd;
2912 	struct ib_uverbs_ex_create_wq_resp resp = {};
2913 	struct ib_uwq_object           *obj;
2914 	int err = 0;
2915 	struct ib_cq *cq;
2916 	struct ib_pd *pd;
2917 	struct ib_wq *wq;
2918 	struct ib_wq_init_attr wq_init_attr = {};
2919 	struct ib_device *ib_dev;
2920 
2921 	err = uverbs_request(attrs, &cmd, sizeof(cmd));
2922 	if (err)
2923 		return err;
2924 
2925 	if (cmd.comp_mask)
2926 		return -EOPNOTSUPP;
2927 
2928 	obj = (struct ib_uwq_object *)uobj_alloc(UVERBS_OBJECT_WQ, attrs,
2929 						 &ib_dev);
2930 	if (IS_ERR(obj))
2931 		return PTR_ERR(obj);
2932 
2933 	pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
2934 	if (IS_ERR(pd)) {
2935 		err = PTR_ERR(pd);
2936 		goto err_uobj;
2937 	}
2938 
2939 	cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
2940 	if (IS_ERR(cq)) {
2941 		err = PTR_ERR(cq);
2942 		goto err_put_pd;
2943 	}
2944 
2945 	wq_init_attr.cq = cq;
2946 	wq_init_attr.max_sge = cmd.max_sge;
2947 	wq_init_attr.max_wr = cmd.max_wr;
2948 	wq_init_attr.wq_type = cmd.wq_type;
2949 	wq_init_attr.event_handler = ib_uverbs_wq_event_handler;
2950 	wq_init_attr.create_flags = cmd.create_flags;
2951 	INIT_LIST_HEAD(&obj->uevent.event_list);
2952 	obj->uevent.uobject.user_handle = cmd.user_handle;
2953 
2954 	wq = pd->device->ops.create_wq(pd, &wq_init_attr, &attrs->driver_udata);
2955 	if (IS_ERR(wq)) {
2956 		err = PTR_ERR(wq);
2957 		goto err_put_cq;
2958 	}
2959 
2960 	wq->uobject = obj;
2961 	obj->uevent.uobject.object = wq;
2962 	wq->wq_type = wq_init_attr.wq_type;
2963 	wq->cq = cq;
2964 	wq->pd = pd;
2965 	wq->device = pd->device;
2966 	atomic_set(&wq->usecnt, 0);
2967 	atomic_inc(&pd->usecnt);
2968 	atomic_inc(&cq->usecnt);
2969 	obj->uevent.event_file = READ_ONCE(attrs->ufile->default_async_file);
2970 	if (obj->uevent.event_file)
2971 		uverbs_uobject_get(&obj->uevent.event_file->uobj);
2972 
2973 	uobj_put_obj_read(pd);
2974 	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
2975 				UVERBS_LOOKUP_READ);
2976 	uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
2977 
2978 	resp.wq_handle = obj->uevent.uobject.id;
2979 	resp.max_sge = wq_init_attr.max_sge;
2980 	resp.max_wr = wq_init_attr.max_wr;
2981 	resp.wqn = wq->wq_num;
2982 	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
2983 	return uverbs_response(attrs, &resp, sizeof(resp));
2984 
2985 err_put_cq:
2986 	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
2987 				UVERBS_LOOKUP_READ);
2988 err_put_pd:
2989 	uobj_put_obj_read(pd);
2990 err_uobj:
2991 	uobj_alloc_abort(&obj->uevent.uobject, attrs);
2992 
2993 	return err;
2994 }
2995 
ib_uverbs_ex_destroy_wq(struct uverbs_attr_bundle * attrs)2996 static int ib_uverbs_ex_destroy_wq(struct uverbs_attr_bundle *attrs)
2997 {
2998 	struct ib_uverbs_ex_destroy_wq	cmd;
2999 	struct ib_uverbs_ex_destroy_wq_resp	resp = {};
3000 	struct ib_uobject		*uobj;
3001 	struct ib_uwq_object		*obj;
3002 	int				ret;
3003 
3004 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3005 	if (ret)
3006 		return ret;
3007 
3008 	if (cmd.comp_mask)
3009 		return -EOPNOTSUPP;
3010 
3011 	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3012 	uobj = uobj_get_destroy(UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3013 	if (IS_ERR(uobj))
3014 		return PTR_ERR(uobj);
3015 
3016 	obj = container_of(uobj, struct ib_uwq_object, uevent.uobject);
3017 	resp.events_reported = obj->uevent.events_reported;
3018 
3019 	uobj_put_destroy(uobj);
3020 
3021 	return uverbs_response(attrs, &resp, sizeof(resp));
3022 }
3023 
ib_uverbs_ex_modify_wq(struct uverbs_attr_bundle * attrs)3024 static int ib_uverbs_ex_modify_wq(struct uverbs_attr_bundle *attrs)
3025 {
3026 	struct ib_uverbs_ex_modify_wq cmd;
3027 	struct ib_wq *wq;
3028 	struct ib_wq_attr wq_attr = {};
3029 	int ret;
3030 
3031 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3032 	if (ret)
3033 		return ret;
3034 
3035 	if (!cmd.attr_mask)
3036 		return -EINVAL;
3037 
3038 	if (cmd.attr_mask > (IB_WQ_STATE | IB_WQ_CUR_STATE | IB_WQ_FLAGS))
3039 		return -EINVAL;
3040 
3041 	wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3042 	if (IS_ERR(wq))
3043 		return PTR_ERR(wq);
3044 
3045 	if (cmd.attr_mask & IB_WQ_FLAGS) {
3046 		wq_attr.flags = cmd.flags;
3047 		wq_attr.flags_mask = cmd.flags_mask;
3048 	}
3049 
3050 	if (cmd.attr_mask & IB_WQ_CUR_STATE) {
3051 		if (cmd.curr_wq_state > IB_WQS_ERR)
3052 			return -EINVAL;
3053 
3054 		wq_attr.curr_wq_state = cmd.curr_wq_state;
3055 	} else {
3056 		wq_attr.curr_wq_state = wq->state;
3057 	}
3058 
3059 	if (cmd.attr_mask & IB_WQ_STATE) {
3060 		if (cmd.wq_state > IB_WQS_ERR)
3061 			return -EINVAL;
3062 
3063 		wq_attr.wq_state = cmd.wq_state;
3064 	} else {
3065 		wq_attr.wq_state = wq_attr.curr_wq_state;
3066 	}
3067 
3068 	ret = wq->device->ops.modify_wq(wq, &wq_attr, cmd.attr_mask,
3069 					&attrs->driver_udata);
3070 	rdma_lookup_put_uobject(&wq->uobject->uevent.uobject,
3071 				UVERBS_LOOKUP_READ);
3072 	return ret;
3073 }
3074 
ib_uverbs_ex_create_rwq_ind_table(struct uverbs_attr_bundle * attrs)3075 static int ib_uverbs_ex_create_rwq_ind_table(struct uverbs_attr_bundle *attrs)
3076 {
3077 	struct ib_uverbs_ex_create_rwq_ind_table cmd;
3078 	struct ib_uverbs_ex_create_rwq_ind_table_resp  resp = {};
3079 	struct ib_uobject *uobj;
3080 	int err;
3081 	struct ib_rwq_ind_table_init_attr init_attr = {};
3082 	struct ib_rwq_ind_table *rwq_ind_tbl;
3083 	struct ib_wq **wqs = NULL;
3084 	u32 *wqs_handles = NULL;
3085 	struct ib_wq	*wq = NULL;
3086 	int i, num_read_wqs;
3087 	u32 num_wq_handles;
3088 	struct uverbs_req_iter iter;
3089 	struct ib_device *ib_dev;
3090 
3091 	err = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
3092 	if (err)
3093 		return err;
3094 
3095 	if (cmd.comp_mask)
3096 		return -EOPNOTSUPP;
3097 
3098 	if (cmd.log_ind_tbl_size > IB_USER_VERBS_MAX_LOG_IND_TBL_SIZE)
3099 		return -EINVAL;
3100 
3101 	num_wq_handles = 1 << cmd.log_ind_tbl_size;
3102 	wqs_handles = kcalloc(num_wq_handles, sizeof(*wqs_handles),
3103 			      GFP_KERNEL);
3104 	if (!wqs_handles)
3105 		return -ENOMEM;
3106 
3107 	err = uverbs_request_next(&iter, wqs_handles,
3108 				  num_wq_handles * sizeof(__u32));
3109 	if (err)
3110 		goto err_free;
3111 
3112 	err = uverbs_request_finish(&iter);
3113 	if (err)
3114 		goto err_free;
3115 
3116 	wqs = kcalloc(num_wq_handles, sizeof(*wqs), GFP_KERNEL);
3117 	if (!wqs) {
3118 		err = -ENOMEM;
3119 		goto  err_free;
3120 	}
3121 
3122 	for (num_read_wqs = 0; num_read_wqs < num_wq_handles;
3123 			num_read_wqs++) {
3124 		wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ,
3125 				       wqs_handles[num_read_wqs], attrs);
3126 		if (IS_ERR(wq)) {
3127 			err = PTR_ERR(wq);
3128 			goto put_wqs;
3129 		}
3130 
3131 		wqs[num_read_wqs] = wq;
3132 		atomic_inc(&wqs[num_read_wqs]->usecnt);
3133 	}
3134 
3135 	uobj = uobj_alloc(UVERBS_OBJECT_RWQ_IND_TBL, attrs, &ib_dev);
3136 	if (IS_ERR(uobj)) {
3137 		err = PTR_ERR(uobj);
3138 		goto put_wqs;
3139 	}
3140 
3141 	rwq_ind_tbl = rdma_zalloc_drv_obj(ib_dev, ib_rwq_ind_table);
3142 	if (!rwq_ind_tbl) {
3143 		err = -ENOMEM;
3144 		goto err_uobj;
3145 	}
3146 
3147 	init_attr.log_ind_tbl_size = cmd.log_ind_tbl_size;
3148 	init_attr.ind_tbl = wqs;
3149 
3150 	rwq_ind_tbl->ind_tbl = wqs;
3151 	rwq_ind_tbl->log_ind_tbl_size = init_attr.log_ind_tbl_size;
3152 	rwq_ind_tbl->uobject = uobj;
3153 	uobj->object = rwq_ind_tbl;
3154 	rwq_ind_tbl->device = ib_dev;
3155 	atomic_set(&rwq_ind_tbl->usecnt, 0);
3156 
3157 	err = ib_dev->ops.create_rwq_ind_table(rwq_ind_tbl, &init_attr,
3158 					       &attrs->driver_udata);
3159 	if (err)
3160 		goto err_create;
3161 
3162 	for (i = 0; i < num_wq_handles; i++)
3163 		rdma_lookup_put_uobject(&wqs[i]->uobject->uevent.uobject,
3164 					UVERBS_LOOKUP_READ);
3165 	kfree(wqs_handles);
3166 	uobj_finalize_uobj_create(uobj, attrs);
3167 
3168 	resp.ind_tbl_handle = uobj->id;
3169 	resp.ind_tbl_num = rwq_ind_tbl->ind_tbl_num;
3170 	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3171 	return uverbs_response(attrs, &resp, sizeof(resp));
3172 
3173 err_create:
3174 	kfree(rwq_ind_tbl);
3175 err_uobj:
3176 	uobj_alloc_abort(uobj, attrs);
3177 put_wqs:
3178 	for (i = 0; i < num_read_wqs; i++) {
3179 		rdma_lookup_put_uobject(&wqs[i]->uobject->uevent.uobject,
3180 					UVERBS_LOOKUP_READ);
3181 		atomic_dec(&wqs[i]->usecnt);
3182 	}
3183 err_free:
3184 	kfree(wqs_handles);
3185 	kfree(wqs);
3186 	return err;
3187 }
3188 
ib_uverbs_ex_destroy_rwq_ind_table(struct uverbs_attr_bundle * attrs)3189 static int ib_uverbs_ex_destroy_rwq_ind_table(struct uverbs_attr_bundle *attrs)
3190 {
3191 	struct ib_uverbs_ex_destroy_rwq_ind_table cmd;
3192 	int ret;
3193 
3194 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3195 	if (ret)
3196 		return ret;
3197 
3198 	if (cmd.comp_mask)
3199 		return -EOPNOTSUPP;
3200 
3201 	return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL,
3202 				    cmd.ind_tbl_handle, attrs);
3203 }
3204 
ib_uverbs_ex_create_flow(struct uverbs_attr_bundle * attrs)3205 static int ib_uverbs_ex_create_flow(struct uverbs_attr_bundle *attrs)
3206 {
3207 	struct ib_uverbs_create_flow	  cmd;
3208 	struct ib_uverbs_create_flow_resp resp = {};
3209 	struct ib_uobject		  *uobj;
3210 	struct ib_flow			  *flow_id;
3211 	struct ib_uverbs_flow_attr	  *kern_flow_attr;
3212 	struct ib_flow_attr		  *flow_attr;
3213 	struct ib_qp			  *qp;
3214 	struct ib_uflow_resources	  *uflow_res;
3215 	struct ib_uverbs_flow_spec_hdr	  *kern_spec;
3216 	struct uverbs_req_iter iter;
3217 	int err;
3218 	void *ib_spec;
3219 	int i;
3220 	struct ib_device *ib_dev;
3221 
3222 	err = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
3223 	if (err)
3224 		return err;
3225 
3226 	if (cmd.comp_mask)
3227 		return -EINVAL;
3228 
3229 	if (!rdma_uattrs_has_raw_cap(attrs))
3230 		return -EPERM;
3231 
3232 	if (cmd.flow_attr.flags >= IB_FLOW_ATTR_FLAGS_RESERVED)
3233 		return -EINVAL;
3234 
3235 	if ((cmd.flow_attr.flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
3236 	    ((cmd.flow_attr.type == IB_FLOW_ATTR_ALL_DEFAULT) ||
3237 	     (cmd.flow_attr.type == IB_FLOW_ATTR_MC_DEFAULT)))
3238 		return -EINVAL;
3239 
3240 	if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
3241 		return -EINVAL;
3242 
3243 	if (cmd.flow_attr.size >
3244 	    (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
3245 		return -EINVAL;
3246 
3247 	if (cmd.flow_attr.reserved[0] ||
3248 	    cmd.flow_attr.reserved[1])
3249 		return -EINVAL;
3250 
3251 	if (cmd.flow_attr.num_of_specs) {
3252 		kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
3253 					 GFP_KERNEL);
3254 		if (!kern_flow_attr)
3255 			return -ENOMEM;
3256 
3257 		*kern_flow_attr = cmd.flow_attr;
3258 		err = uverbs_request_next(&iter, &kern_flow_attr->flow_specs,
3259 					  cmd.flow_attr.size);
3260 		if (err)
3261 			goto err_free_attr;
3262 	} else {
3263 		kern_flow_attr = &cmd.flow_attr;
3264 	}
3265 
3266 	err = uverbs_request_finish(&iter);
3267 	if (err)
3268 		goto err_free_attr;
3269 
3270 	uobj = uobj_alloc(UVERBS_OBJECT_FLOW, attrs, &ib_dev);
3271 	if (IS_ERR(uobj)) {
3272 		err = PTR_ERR(uobj);
3273 		goto err_free_attr;
3274 	}
3275 
3276 	if (!rdma_is_port_valid(uobj->context->device, cmd.flow_attr.port)) {
3277 		err = -EINVAL;
3278 		goto err_uobj;
3279 	}
3280 
3281 	qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
3282 	if (IS_ERR(qp)) {
3283 		err = PTR_ERR(qp);
3284 		goto err_uobj;
3285 	}
3286 
3287 	if (qp->qp_type != IB_QPT_UD && qp->qp_type != IB_QPT_RAW_PACKET) {
3288 		err = -EINVAL;
3289 		goto err_put;
3290 	}
3291 
3292 	flow_attr = kzalloc(struct_size(flow_attr, flows,
3293 				cmd.flow_attr.num_of_specs), GFP_KERNEL);
3294 	if (!flow_attr) {
3295 		err = -ENOMEM;
3296 		goto err_put;
3297 	}
3298 	uflow_res = flow_resources_alloc(cmd.flow_attr.num_of_specs);
3299 	if (!uflow_res) {
3300 		err = -ENOMEM;
3301 		goto err_free_flow_attr;
3302 	}
3303 
3304 	flow_attr->type = kern_flow_attr->type;
3305 	flow_attr->priority = kern_flow_attr->priority;
3306 	flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
3307 	flow_attr->port = kern_flow_attr->port;
3308 	flow_attr->flags = kern_flow_attr->flags;
3309 	flow_attr->size = sizeof(*flow_attr);
3310 
3311 	kern_spec = kern_flow_attr->flow_specs;
3312 	ib_spec = flow_attr + 1;
3313 	for (i = 0; i < flow_attr->num_of_specs &&
3314 			cmd.flow_attr.size >= sizeof(*kern_spec) &&
3315 			cmd.flow_attr.size >= kern_spec->size;
3316 	     i++) {
3317 		err = kern_spec_to_ib_spec(
3318 				attrs, (struct ib_uverbs_flow_spec *)kern_spec,
3319 				ib_spec, uflow_res);
3320 		if (err)
3321 			goto err_free;
3322 
3323 		flow_attr->size +=
3324 			((union ib_flow_spec *) ib_spec)->size;
3325 		cmd.flow_attr.size -= kern_spec->size;
3326 		kern_spec = ((void *)kern_spec) + kern_spec->size;
3327 		ib_spec += ((union ib_flow_spec *) ib_spec)->size;
3328 	}
3329 	if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
3330 		pr_warn("create flow failed, flow %d: %u bytes left from uverb cmd\n",
3331 			i, cmd.flow_attr.size);
3332 		err = -EINVAL;
3333 		goto err_free;
3334 	}
3335 
3336 	flow_id = qp->device->ops.create_flow(qp, flow_attr,
3337 					      &attrs->driver_udata);
3338 
3339 	if (IS_ERR(flow_id)) {
3340 		err = PTR_ERR(flow_id);
3341 		goto err_free;
3342 	}
3343 
3344 	ib_set_flow(uobj, flow_id, qp, qp->device, uflow_res);
3345 
3346 	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
3347 				UVERBS_LOOKUP_READ);
3348 	kfree(flow_attr);
3349 
3350 	if (cmd.flow_attr.num_of_specs)
3351 		kfree(kern_flow_attr);
3352 	uobj_finalize_uobj_create(uobj, attrs);
3353 
3354 	resp.flow_handle = uobj->id;
3355 	return uverbs_response(attrs, &resp, sizeof(resp));
3356 
3357 err_free:
3358 	ib_uverbs_flow_resources_free(uflow_res);
3359 err_free_flow_attr:
3360 	kfree(flow_attr);
3361 err_put:
3362 	rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
3363 				UVERBS_LOOKUP_READ);
3364 err_uobj:
3365 	uobj_alloc_abort(uobj, attrs);
3366 err_free_attr:
3367 	if (cmd.flow_attr.num_of_specs)
3368 		kfree(kern_flow_attr);
3369 	return err;
3370 }
3371 
ib_uverbs_ex_destroy_flow(struct uverbs_attr_bundle * attrs)3372 static int ib_uverbs_ex_destroy_flow(struct uverbs_attr_bundle *attrs)
3373 {
3374 	struct ib_uverbs_destroy_flow	cmd;
3375 	int				ret;
3376 
3377 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3378 	if (ret)
3379 		return ret;
3380 
3381 	if (cmd.comp_mask)
3382 		return -EINVAL;
3383 
3384 	return uobj_perform_destroy(UVERBS_OBJECT_FLOW, cmd.flow_handle, attrs);
3385 }
3386 
__uverbs_create_xsrq(struct uverbs_attr_bundle * attrs,struct ib_uverbs_create_xsrq * cmd,struct ib_udata * udata)3387 static int __uverbs_create_xsrq(struct uverbs_attr_bundle *attrs,
3388 				struct ib_uverbs_create_xsrq *cmd,
3389 				struct ib_udata *udata)
3390 {
3391 	struct ib_uverbs_create_srq_resp resp = {};
3392 	struct ib_usrq_object           *obj;
3393 	struct ib_pd                    *pd;
3394 	struct ib_srq                   *srq;
3395 	struct ib_srq_init_attr          attr;
3396 	int ret;
3397 	struct ib_uobject *xrcd_uobj;
3398 	struct ib_device *ib_dev;
3399 
3400 	obj = (struct ib_usrq_object *)uobj_alloc(UVERBS_OBJECT_SRQ, attrs,
3401 						  &ib_dev);
3402 	if (IS_ERR(obj))
3403 		return PTR_ERR(obj);
3404 
3405 	if (cmd->srq_type == IB_SRQT_TM)
3406 		attr.ext.tag_matching.max_num_tags = cmd->max_num_tags;
3407 
3408 	if (cmd->srq_type == IB_SRQT_XRC) {
3409 		xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->xrcd_handle,
3410 					  attrs);
3411 		if (IS_ERR(xrcd_uobj)) {
3412 			ret = -EINVAL;
3413 			goto err;
3414 		}
3415 
3416 		attr.ext.xrc.xrcd = (struct ib_xrcd *)xrcd_uobj->object;
3417 		if (!attr.ext.xrc.xrcd) {
3418 			ret = -EINVAL;
3419 			goto err_put_xrcd;
3420 		}
3421 
3422 		obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
3423 		atomic_inc(&obj->uxrcd->refcnt);
3424 	}
3425 
3426 	if (ib_srq_has_cq(cmd->srq_type)) {
3427 		attr.ext.cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
3428 						cmd->cq_handle, attrs);
3429 		if (IS_ERR(attr.ext.cq)) {
3430 			ret = PTR_ERR(attr.ext.cq);
3431 			goto err_put_xrcd;
3432 		}
3433 	}
3434 
3435 	pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle, attrs);
3436 	if (IS_ERR(pd)) {
3437 		ret = PTR_ERR(pd);
3438 		goto err_put_cq;
3439 	}
3440 
3441 	attr.event_handler  = ib_uverbs_srq_event_handler;
3442 	attr.srq_type       = cmd->srq_type;
3443 	attr.attr.max_wr    = cmd->max_wr;
3444 	attr.attr.max_sge   = cmd->max_sge;
3445 	attr.attr.srq_limit = cmd->srq_limit;
3446 
3447 	INIT_LIST_HEAD(&obj->uevent.event_list);
3448 	obj->uevent.uobject.user_handle = cmd->user_handle;
3449 
3450 	srq = ib_create_srq_user(pd, &attr, obj, udata);
3451 	if (IS_ERR(srq)) {
3452 		ret = PTR_ERR(srq);
3453 		goto err_put_pd;
3454 	}
3455 
3456 	obj->uevent.uobject.object = srq;
3457 	obj->uevent.uobject.user_handle = cmd->user_handle;
3458 	obj->uevent.event_file = READ_ONCE(attrs->ufile->default_async_file);
3459 	if (obj->uevent.event_file)
3460 		uverbs_uobject_get(&obj->uevent.event_file->uobj);
3461 
3462 	if (cmd->srq_type == IB_SRQT_XRC)
3463 		resp.srqn = srq->ext.xrc.srq_num;
3464 
3465 	if (cmd->srq_type == IB_SRQT_XRC)
3466 		uobj_put_read(xrcd_uobj);
3467 
3468 	if (ib_srq_has_cq(cmd->srq_type))
3469 		rdma_lookup_put_uobject(&attr.ext.cq->uobject->uevent.uobject,
3470 					UVERBS_LOOKUP_READ);
3471 
3472 	uobj_put_obj_read(pd);
3473 	uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
3474 
3475 	resp.srq_handle = obj->uevent.uobject.id;
3476 	resp.max_wr = attr.attr.max_wr;
3477 	resp.max_sge = attr.attr.max_sge;
3478 	return uverbs_response(attrs, &resp, sizeof(resp));
3479 
3480 err_put_pd:
3481 	uobj_put_obj_read(pd);
3482 err_put_cq:
3483 	if (ib_srq_has_cq(cmd->srq_type))
3484 		rdma_lookup_put_uobject(&attr.ext.cq->uobject->uevent.uobject,
3485 					UVERBS_LOOKUP_READ);
3486 
3487 err_put_xrcd:
3488 	if (cmd->srq_type == IB_SRQT_XRC) {
3489 		atomic_dec(&obj->uxrcd->refcnt);
3490 		uobj_put_read(xrcd_uobj);
3491 	}
3492 
3493 err:
3494 	uobj_alloc_abort(&obj->uevent.uobject, attrs);
3495 	return ret;
3496 }
3497 
ib_uverbs_create_srq(struct uverbs_attr_bundle * attrs)3498 static int ib_uverbs_create_srq(struct uverbs_attr_bundle *attrs)
3499 {
3500 	struct ib_uverbs_create_srq      cmd;
3501 	struct ib_uverbs_create_xsrq     xcmd;
3502 	int ret;
3503 
3504 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3505 	if (ret)
3506 		return ret;
3507 
3508 	memset(&xcmd, 0, sizeof(xcmd));
3509 	xcmd.response	 = cmd.response;
3510 	xcmd.user_handle = cmd.user_handle;
3511 	xcmd.srq_type	 = IB_SRQT_BASIC;
3512 	xcmd.pd_handle	 = cmd.pd_handle;
3513 	xcmd.max_wr	 = cmd.max_wr;
3514 	xcmd.max_sge	 = cmd.max_sge;
3515 	xcmd.srq_limit	 = cmd.srq_limit;
3516 
3517 	return __uverbs_create_xsrq(attrs, &xcmd, &attrs->driver_udata);
3518 }
3519 
ib_uverbs_create_xsrq(struct uverbs_attr_bundle * attrs)3520 static int ib_uverbs_create_xsrq(struct uverbs_attr_bundle *attrs)
3521 {
3522 	struct ib_uverbs_create_xsrq     cmd;
3523 	int ret;
3524 
3525 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3526 	if (ret)
3527 		return ret;
3528 
3529 	return __uverbs_create_xsrq(attrs, &cmd, &attrs->driver_udata);
3530 }
3531 
ib_uverbs_modify_srq(struct uverbs_attr_bundle * attrs)3532 static int ib_uverbs_modify_srq(struct uverbs_attr_bundle *attrs)
3533 {
3534 	struct ib_uverbs_modify_srq cmd;
3535 	struct ib_srq              *srq;
3536 	struct ib_srq_attr          attr;
3537 	int                         ret;
3538 
3539 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3540 	if (ret)
3541 		return ret;
3542 
3543 	srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3544 	if (IS_ERR(srq))
3545 		return PTR_ERR(srq);
3546 
3547 	attr.max_wr    = cmd.max_wr;
3548 	attr.srq_limit = cmd.srq_limit;
3549 
3550 	ret = srq->device->ops.modify_srq(srq, &attr, cmd.attr_mask,
3551 					  &attrs->driver_udata);
3552 
3553 	rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
3554 				UVERBS_LOOKUP_READ);
3555 
3556 	return ret;
3557 }
3558 
ib_uverbs_query_srq(struct uverbs_attr_bundle * attrs)3559 static int ib_uverbs_query_srq(struct uverbs_attr_bundle *attrs)
3560 {
3561 	struct ib_uverbs_query_srq      cmd;
3562 	struct ib_uverbs_query_srq_resp resp;
3563 	struct ib_srq_attr              attr;
3564 	struct ib_srq                   *srq;
3565 	int                             ret;
3566 
3567 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3568 	if (ret)
3569 		return ret;
3570 
3571 	srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3572 	if (IS_ERR(srq))
3573 		return PTR_ERR(srq);
3574 
3575 	ret = ib_query_srq(srq, &attr);
3576 
3577 	rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
3578 				UVERBS_LOOKUP_READ);
3579 
3580 	if (ret)
3581 		return ret;
3582 
3583 	memset(&resp, 0, sizeof resp);
3584 
3585 	resp.max_wr    = attr.max_wr;
3586 	resp.max_sge   = attr.max_sge;
3587 	resp.srq_limit = attr.srq_limit;
3588 
3589 	return uverbs_response(attrs, &resp, sizeof(resp));
3590 }
3591 
ib_uverbs_destroy_srq(struct uverbs_attr_bundle * attrs)3592 static int ib_uverbs_destroy_srq(struct uverbs_attr_bundle *attrs)
3593 {
3594 	struct ib_uverbs_destroy_srq      cmd;
3595 	struct ib_uverbs_destroy_srq_resp resp;
3596 	struct ib_uobject		 *uobj;
3597 	struct ib_uevent_object        	 *obj;
3598 	int ret;
3599 
3600 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3601 	if (ret)
3602 		return ret;
3603 
3604 	uobj = uobj_get_destroy(UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3605 	if (IS_ERR(uobj))
3606 		return PTR_ERR(uobj);
3607 
3608 	obj = container_of(uobj, struct ib_uevent_object, uobject);
3609 	memset(&resp, 0, sizeof(resp));
3610 	resp.events_reported = obj->events_reported;
3611 
3612 	uobj_put_destroy(uobj);
3613 
3614 	return uverbs_response(attrs, &resp, sizeof(resp));
3615 }
3616 
ib_uverbs_ex_query_device(struct uverbs_attr_bundle * attrs)3617 static int ib_uverbs_ex_query_device(struct uverbs_attr_bundle *attrs)
3618 {
3619 	struct ib_uverbs_ex_query_device_resp resp = {};
3620 	struct ib_uverbs_ex_query_device  cmd;
3621 	struct ib_device_attr attr = {0};
3622 	struct ib_ucontext *ucontext;
3623 	struct ib_device *ib_dev;
3624 	int err;
3625 
3626 	ucontext = ib_uverbs_get_ucontext(attrs);
3627 	if (IS_ERR(ucontext))
3628 		return PTR_ERR(ucontext);
3629 	ib_dev = ucontext->device;
3630 
3631 	err = uverbs_request(attrs, &cmd, sizeof(cmd));
3632 	if (err)
3633 		return err;
3634 
3635 	if (cmd.comp_mask)
3636 		return -EINVAL;
3637 
3638 	if (cmd.reserved)
3639 		return -EINVAL;
3640 
3641 	err = ib_dev->ops.query_device(ib_dev, &attr, &attrs->driver_udata);
3642 	if (err)
3643 		return err;
3644 
3645 	copy_query_dev_fields(ucontext, &resp.base, &attr);
3646 
3647 	resp.odp_caps.general_caps = attr.odp_caps.general_caps;
3648 	resp.odp_caps.per_transport_caps.rc_odp_caps =
3649 		attr.odp_caps.per_transport_caps.rc_odp_caps;
3650 	resp.odp_caps.per_transport_caps.uc_odp_caps =
3651 		attr.odp_caps.per_transport_caps.uc_odp_caps;
3652 	resp.odp_caps.per_transport_caps.ud_odp_caps =
3653 		attr.odp_caps.per_transport_caps.ud_odp_caps;
3654 	resp.xrc_odp_caps = attr.odp_caps.per_transport_caps.xrc_odp_caps;
3655 
3656 	resp.timestamp_mask = attr.timestamp_mask;
3657 	resp.hca_core_clock = attr.hca_core_clock;
3658 	resp.device_cap_flags_ex = attr.device_cap_flags;
3659 	resp.rss_caps.supported_qpts = attr.rss_caps.supported_qpts;
3660 	resp.rss_caps.max_rwq_indirection_tables =
3661 		attr.rss_caps.max_rwq_indirection_tables;
3662 	resp.rss_caps.max_rwq_indirection_table_size =
3663 		attr.rss_caps.max_rwq_indirection_table_size;
3664 	resp.max_wq_type_rq = attr.max_wq_type_rq;
3665 	resp.raw_packet_caps = attr.raw_packet_caps;
3666 	resp.tm_caps.max_rndv_hdr_size	= attr.tm_caps.max_rndv_hdr_size;
3667 	resp.tm_caps.max_num_tags	= attr.tm_caps.max_num_tags;
3668 	resp.tm_caps.max_ops		= attr.tm_caps.max_ops;
3669 	resp.tm_caps.max_sge		= attr.tm_caps.max_sge;
3670 	resp.tm_caps.flags		= attr.tm_caps.flags;
3671 	resp.cq_moderation_caps.max_cq_moderation_count  =
3672 		attr.cq_caps.max_cq_moderation_count;
3673 	resp.cq_moderation_caps.max_cq_moderation_period =
3674 		attr.cq_caps.max_cq_moderation_period;
3675 	resp.max_dm_size = attr.max_dm_size;
3676 	resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3677 
3678 	return uverbs_response(attrs, &resp, sizeof(resp));
3679 }
3680 
ib_uverbs_ex_modify_cq(struct uverbs_attr_bundle * attrs)3681 static int ib_uverbs_ex_modify_cq(struct uverbs_attr_bundle *attrs)
3682 {
3683 	struct ib_uverbs_ex_modify_cq cmd;
3684 	struct ib_cq *cq;
3685 	int ret;
3686 
3687 	ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3688 	if (ret)
3689 		return ret;
3690 
3691 	if (!cmd.attr_mask || cmd.reserved)
3692 		return -EINVAL;
3693 
3694 	if (cmd.attr_mask > IB_CQ_MODERATE)
3695 		return -EOPNOTSUPP;
3696 
3697 	cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
3698 	if (IS_ERR(cq))
3699 		return PTR_ERR(cq);
3700 
3701 	ret = rdma_set_cq_moderation(cq, cmd.attr.cq_count, cmd.attr.cq_period);
3702 
3703 	rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
3704 				UVERBS_LOOKUP_READ);
3705 	return ret;
3706 }
3707 
3708 /*
3709  * Describe the input structs for write(). Some write methods have an input
3710  * only struct, most have an input and output. If the struct has an output then
3711  * the 'response' u64 must be the first field in the request structure.
3712  *
3713  * If udata is present then both the request and response structs have a
3714  * trailing driver_data flex array. In this case the size of the base struct
3715  * cannot be changed.
3716  */
3717 #define UAPI_DEF_WRITE_IO(req, resp)                                           \
3718 	.write.has_resp = 1 +                                                  \
3719 			  BUILD_BUG_ON_ZERO(offsetof(req, response) != 0) +    \
3720 			  BUILD_BUG_ON_ZERO(sizeof_field(req, response) !=    \
3721 					    sizeof(u64)),                      \
3722 	.write.req_size = sizeof(req), .write.resp_size = sizeof(resp)
3723 
3724 #define UAPI_DEF_WRITE_I(req) .write.req_size = sizeof(req)
3725 
3726 #define UAPI_DEF_WRITE_UDATA_IO(req, resp)                                     \
3727 	UAPI_DEF_WRITE_IO(req, resp),                                          \
3728 		.write.has_udata =                                             \
3729 			1 +                                                    \
3730 			BUILD_BUG_ON_ZERO(offsetof(req, driver_data) !=        \
3731 					  sizeof(req)) +                       \
3732 			BUILD_BUG_ON_ZERO(offsetof(resp, driver_data) !=       \
3733 					  sizeof(resp))
3734 
3735 #define UAPI_DEF_WRITE_UDATA_I(req)                                            \
3736 	UAPI_DEF_WRITE_I(req),                                                 \
3737 		.write.has_udata =                                             \
3738 			1 + BUILD_BUG_ON_ZERO(offsetof(req, driver_data) !=    \
3739 					      sizeof(req))
3740 
3741 /*
3742  * The _EX versions are for use with WRITE_EX and allow the last struct member
3743  * to be specified. Buffers that do not include that member will be rejected.
3744  */
3745 #define UAPI_DEF_WRITE_IO_EX(req, req_last_member, resp, resp_last_member)     \
3746 	.write.has_resp = 1,                                                   \
3747 	.write.req_size = offsetofend(req, req_last_member),                   \
3748 	.write.resp_size = offsetofend(resp, resp_last_member)
3749 
3750 #define UAPI_DEF_WRITE_I_EX(req, req_last_member)                              \
3751 	.write.req_size = offsetofend(req, req_last_member)
3752 
3753 const struct uapi_definition uverbs_def_write_intf[] = {
3754 	DECLARE_UVERBS_OBJECT(
3755 		UVERBS_OBJECT_AH,
3756 		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_AH,
3757 				     ib_uverbs_create_ah,
3758 				     UAPI_DEF_WRITE_UDATA_IO(
3759 					     struct ib_uverbs_create_ah,
3760 					     struct ib_uverbs_create_ah_resp)),
3761 		DECLARE_UVERBS_WRITE(
3762 			IB_USER_VERBS_CMD_DESTROY_AH,
3763 			ib_uverbs_destroy_ah,
3764 			UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_ah)),
3765 		UAPI_DEF_OBJ_NEEDS_FN(create_user_ah),
3766 		UAPI_DEF_OBJ_NEEDS_FN(destroy_ah)),
3767 
3768 	DECLARE_UVERBS_OBJECT(
3769 		UVERBS_OBJECT_COMP_CHANNEL,
3770 		DECLARE_UVERBS_WRITE(
3771 			IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL,
3772 			ib_uverbs_create_comp_channel,
3773 			UAPI_DEF_WRITE_IO(
3774 				struct ib_uverbs_create_comp_channel,
3775 				struct ib_uverbs_create_comp_channel_resp))),
3776 
3777 	DECLARE_UVERBS_OBJECT(
3778 		UVERBS_OBJECT_CQ,
3779 		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_CQ,
3780 				     ib_uverbs_create_cq,
3781 				     UAPI_DEF_WRITE_UDATA_IO(
3782 					     struct ib_uverbs_create_cq,
3783 					     struct ib_uverbs_create_cq_resp),
3784 				     UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
3785 		DECLARE_UVERBS_WRITE(
3786 			IB_USER_VERBS_CMD_DESTROY_CQ,
3787 			ib_uverbs_destroy_cq,
3788 			UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_cq,
3789 					  struct ib_uverbs_destroy_cq_resp),
3790 			UAPI_DEF_METHOD_NEEDS_FN(destroy_cq)),
3791 		DECLARE_UVERBS_WRITE(
3792 			IB_USER_VERBS_CMD_POLL_CQ,
3793 			ib_uverbs_poll_cq,
3794 			UAPI_DEF_WRITE_IO(struct ib_uverbs_poll_cq,
3795 					  struct ib_uverbs_poll_cq_resp),
3796 			UAPI_DEF_METHOD_NEEDS_FN(poll_cq)),
3797 		DECLARE_UVERBS_WRITE(
3798 			IB_USER_VERBS_CMD_REQ_NOTIFY_CQ,
3799 			ib_uverbs_req_notify_cq,
3800 			UAPI_DEF_WRITE_I(struct ib_uverbs_req_notify_cq),
3801 			UAPI_DEF_METHOD_NEEDS_FN(req_notify_cq)),
3802 		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_RESIZE_CQ,
3803 				     ib_uverbs_resize_cq,
3804 				     UAPI_DEF_WRITE_UDATA_IO(
3805 					     struct ib_uverbs_resize_cq,
3806 					     struct ib_uverbs_resize_cq_resp),
3807 				     UAPI_DEF_METHOD_NEEDS_FN(resize_cq)),
3808 		DECLARE_UVERBS_WRITE_EX(
3809 			IB_USER_VERBS_EX_CMD_CREATE_CQ,
3810 			ib_uverbs_ex_create_cq,
3811 			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_cq,
3812 					     reserved,
3813 					     struct ib_uverbs_ex_create_cq_resp,
3814 					     response_length),
3815 			UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
3816 		DECLARE_UVERBS_WRITE_EX(
3817 			IB_USER_VERBS_EX_CMD_MODIFY_CQ,
3818 			ib_uverbs_ex_modify_cq,
3819 			UAPI_DEF_WRITE_I(struct ib_uverbs_ex_modify_cq),
3820 			UAPI_DEF_METHOD_NEEDS_FN(modify_cq))),
3821 
3822 	DECLARE_UVERBS_OBJECT(
3823 		UVERBS_OBJECT_DEVICE,
3824 		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_GET_CONTEXT,
3825 				     ib_uverbs_get_context,
3826 				     UAPI_DEF_WRITE_UDATA_IO(
3827 					     struct ib_uverbs_get_context,
3828 					     struct ib_uverbs_get_context_resp)),
3829 		DECLARE_UVERBS_WRITE(
3830 			IB_USER_VERBS_CMD_QUERY_DEVICE,
3831 			ib_uverbs_query_device,
3832 			UAPI_DEF_WRITE_IO(struct ib_uverbs_query_device,
3833 					  struct ib_uverbs_query_device_resp)),
3834 		DECLARE_UVERBS_WRITE(
3835 			IB_USER_VERBS_CMD_QUERY_PORT,
3836 			ib_uverbs_query_port,
3837 			UAPI_DEF_WRITE_IO(struct ib_uverbs_query_port,
3838 					  struct ib_uverbs_query_port_resp),
3839 			UAPI_DEF_METHOD_NEEDS_FN(query_port)),
3840 		DECLARE_UVERBS_WRITE_EX(
3841 			IB_USER_VERBS_EX_CMD_QUERY_DEVICE,
3842 			ib_uverbs_ex_query_device,
3843 			UAPI_DEF_WRITE_IO_EX(
3844 				struct ib_uverbs_ex_query_device,
3845 				reserved,
3846 				struct ib_uverbs_ex_query_device_resp,
3847 				response_length),
3848 			UAPI_DEF_METHOD_NEEDS_FN(query_device)),
3849 		UAPI_DEF_OBJ_NEEDS_FN(alloc_ucontext),
3850 		UAPI_DEF_OBJ_NEEDS_FN(dealloc_ucontext)),
3851 
3852 	DECLARE_UVERBS_OBJECT(
3853 		UVERBS_OBJECT_FLOW,
3854 		DECLARE_UVERBS_WRITE_EX(
3855 			IB_USER_VERBS_EX_CMD_CREATE_FLOW,
3856 			ib_uverbs_ex_create_flow,
3857 			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_create_flow,
3858 					     flow_attr,
3859 					     struct ib_uverbs_create_flow_resp,
3860 					     flow_handle),
3861 			UAPI_DEF_METHOD_NEEDS_FN(create_flow)),
3862 		DECLARE_UVERBS_WRITE_EX(
3863 			IB_USER_VERBS_EX_CMD_DESTROY_FLOW,
3864 			ib_uverbs_ex_destroy_flow,
3865 			UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_flow),
3866 			UAPI_DEF_METHOD_NEEDS_FN(destroy_flow))),
3867 
3868 	DECLARE_UVERBS_OBJECT(
3869 		UVERBS_OBJECT_MR,
3870 		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DEREG_MR,
3871 				     ib_uverbs_dereg_mr,
3872 				     UAPI_DEF_WRITE_I(struct ib_uverbs_dereg_mr),
3873 				     UAPI_DEF_METHOD_NEEDS_FN(dereg_mr)),
3874 		DECLARE_UVERBS_WRITE(
3875 			IB_USER_VERBS_CMD_REG_MR,
3876 			ib_uverbs_reg_mr,
3877 			UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_reg_mr,
3878 						struct ib_uverbs_reg_mr_resp),
3879 			UAPI_DEF_METHOD_NEEDS_FN(reg_user_mr)),
3880 		DECLARE_UVERBS_WRITE(
3881 			IB_USER_VERBS_CMD_REREG_MR,
3882 			ib_uverbs_rereg_mr,
3883 			UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_rereg_mr,
3884 						struct ib_uverbs_rereg_mr_resp),
3885 			UAPI_DEF_METHOD_NEEDS_FN(rereg_user_mr))),
3886 
3887 	DECLARE_UVERBS_OBJECT(
3888 		UVERBS_OBJECT_MW,
3889 		DECLARE_UVERBS_WRITE(
3890 			IB_USER_VERBS_CMD_ALLOC_MW,
3891 			ib_uverbs_alloc_mw,
3892 			UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_mw,
3893 						struct ib_uverbs_alloc_mw_resp),
3894 			UAPI_DEF_METHOD_NEEDS_FN(alloc_mw)),
3895 		DECLARE_UVERBS_WRITE(
3896 			IB_USER_VERBS_CMD_DEALLOC_MW,
3897 			ib_uverbs_dealloc_mw,
3898 			UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_mw),
3899 			UAPI_DEF_METHOD_NEEDS_FN(dealloc_mw))),
3900 
3901 	DECLARE_UVERBS_OBJECT(
3902 		UVERBS_OBJECT_PD,
3903 		DECLARE_UVERBS_WRITE(
3904 			IB_USER_VERBS_CMD_ALLOC_PD,
3905 			ib_uverbs_alloc_pd,
3906 			UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_pd,
3907 						struct ib_uverbs_alloc_pd_resp),
3908 			UAPI_DEF_METHOD_NEEDS_FN(alloc_pd)),
3909 		DECLARE_UVERBS_WRITE(
3910 			IB_USER_VERBS_CMD_DEALLOC_PD,
3911 			ib_uverbs_dealloc_pd,
3912 			UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_pd),
3913 			UAPI_DEF_METHOD_NEEDS_FN(dealloc_pd))),
3914 
3915 	DECLARE_UVERBS_OBJECT(
3916 		UVERBS_OBJECT_QP,
3917 		DECLARE_UVERBS_WRITE(
3918 			IB_USER_VERBS_CMD_ATTACH_MCAST,
3919 			ib_uverbs_attach_mcast,
3920 			UAPI_DEF_WRITE_I(struct ib_uverbs_attach_mcast),
3921 			UAPI_DEF_METHOD_NEEDS_FN(attach_mcast),
3922 			UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
3923 		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_QP,
3924 				     ib_uverbs_create_qp,
3925 				     UAPI_DEF_WRITE_UDATA_IO(
3926 					     struct ib_uverbs_create_qp,
3927 					     struct ib_uverbs_create_qp_resp),
3928 				     UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
3929 		DECLARE_UVERBS_WRITE(
3930 			IB_USER_VERBS_CMD_DESTROY_QP,
3931 			ib_uverbs_destroy_qp,
3932 			UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_qp,
3933 					  struct ib_uverbs_destroy_qp_resp),
3934 			UAPI_DEF_METHOD_NEEDS_FN(destroy_qp)),
3935 		DECLARE_UVERBS_WRITE(
3936 			IB_USER_VERBS_CMD_DETACH_MCAST,
3937 			ib_uverbs_detach_mcast,
3938 			UAPI_DEF_WRITE_I(struct ib_uverbs_detach_mcast),
3939 			UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
3940 		DECLARE_UVERBS_WRITE(
3941 			IB_USER_VERBS_CMD_MODIFY_QP,
3942 			ib_uverbs_modify_qp,
3943 			UAPI_DEF_WRITE_I(struct ib_uverbs_modify_qp),
3944 			UAPI_DEF_METHOD_NEEDS_FN(modify_qp)),
3945 		DECLARE_UVERBS_WRITE(
3946 			IB_USER_VERBS_CMD_POST_RECV,
3947 			ib_uverbs_post_recv,
3948 			UAPI_DEF_WRITE_IO(struct ib_uverbs_post_recv,
3949 					  struct ib_uverbs_post_recv_resp),
3950 			UAPI_DEF_METHOD_NEEDS_FN(post_recv)),
3951 		DECLARE_UVERBS_WRITE(
3952 			IB_USER_VERBS_CMD_POST_SEND,
3953 			ib_uverbs_post_send,
3954 			UAPI_DEF_WRITE_IO(struct ib_uverbs_post_send,
3955 					  struct ib_uverbs_post_send_resp),
3956 			UAPI_DEF_METHOD_NEEDS_FN(post_send)),
3957 		DECLARE_UVERBS_WRITE(
3958 			IB_USER_VERBS_CMD_QUERY_QP,
3959 			ib_uverbs_query_qp,
3960 			UAPI_DEF_WRITE_IO(struct ib_uverbs_query_qp,
3961 					  struct ib_uverbs_query_qp_resp),
3962 			UAPI_DEF_METHOD_NEEDS_FN(query_qp)),
3963 		DECLARE_UVERBS_WRITE_EX(
3964 			IB_USER_VERBS_EX_CMD_CREATE_QP,
3965 			ib_uverbs_ex_create_qp,
3966 			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_qp,
3967 					     comp_mask,
3968 					     struct ib_uverbs_ex_create_qp_resp,
3969 					     response_length),
3970 			UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
3971 		DECLARE_UVERBS_WRITE_EX(
3972 			IB_USER_VERBS_EX_CMD_MODIFY_QP,
3973 			ib_uverbs_ex_modify_qp,
3974 			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_modify_qp,
3975 					     base,
3976 					     struct ib_uverbs_ex_modify_qp_resp,
3977 					     response_length),
3978 			UAPI_DEF_METHOD_NEEDS_FN(modify_qp))),
3979 
3980 	DECLARE_UVERBS_OBJECT(
3981 		UVERBS_OBJECT_RWQ_IND_TBL,
3982 		DECLARE_UVERBS_WRITE_EX(
3983 			IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL,
3984 			ib_uverbs_ex_create_rwq_ind_table,
3985 			UAPI_DEF_WRITE_IO_EX(
3986 				struct ib_uverbs_ex_create_rwq_ind_table,
3987 				log_ind_tbl_size,
3988 				struct ib_uverbs_ex_create_rwq_ind_table_resp,
3989 				ind_tbl_num),
3990 			UAPI_DEF_METHOD_NEEDS_FN(create_rwq_ind_table)),
3991 		DECLARE_UVERBS_WRITE_EX(
3992 			IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL,
3993 			ib_uverbs_ex_destroy_rwq_ind_table,
3994 			UAPI_DEF_WRITE_I(
3995 				struct ib_uverbs_ex_destroy_rwq_ind_table),
3996 			UAPI_DEF_METHOD_NEEDS_FN(destroy_rwq_ind_table))),
3997 
3998 	DECLARE_UVERBS_OBJECT(
3999 		UVERBS_OBJECT_WQ,
4000 		DECLARE_UVERBS_WRITE_EX(
4001 			IB_USER_VERBS_EX_CMD_CREATE_WQ,
4002 			ib_uverbs_ex_create_wq,
4003 			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_wq,
4004 					     max_sge,
4005 					     struct ib_uverbs_ex_create_wq_resp,
4006 					     wqn),
4007 			UAPI_DEF_METHOD_NEEDS_FN(create_wq)),
4008 		DECLARE_UVERBS_WRITE_EX(
4009 			IB_USER_VERBS_EX_CMD_DESTROY_WQ,
4010 			ib_uverbs_ex_destroy_wq,
4011 			UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_destroy_wq,
4012 					     wq_handle,
4013 					     struct ib_uverbs_ex_destroy_wq_resp,
4014 					     reserved),
4015 			UAPI_DEF_METHOD_NEEDS_FN(destroy_wq)),
4016 		DECLARE_UVERBS_WRITE_EX(
4017 			IB_USER_VERBS_EX_CMD_MODIFY_WQ,
4018 			ib_uverbs_ex_modify_wq,
4019 			UAPI_DEF_WRITE_I_EX(struct ib_uverbs_ex_modify_wq,
4020 					    curr_wq_state),
4021 			UAPI_DEF_METHOD_NEEDS_FN(modify_wq))),
4022 
4023 	DECLARE_UVERBS_OBJECT(
4024 		UVERBS_OBJECT_SRQ,
4025 		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_SRQ,
4026 				     ib_uverbs_create_srq,
4027 				     UAPI_DEF_WRITE_UDATA_IO(
4028 					     struct ib_uverbs_create_srq,
4029 					     struct ib_uverbs_create_srq_resp),
4030 				     UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4031 		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_XSRQ,
4032 				     ib_uverbs_create_xsrq,
4033 				     UAPI_DEF_WRITE_UDATA_IO(
4034 					     struct ib_uverbs_create_xsrq,
4035 					     struct ib_uverbs_create_srq_resp),
4036 				     UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4037 		DECLARE_UVERBS_WRITE(
4038 			IB_USER_VERBS_CMD_DESTROY_SRQ,
4039 			ib_uverbs_destroy_srq,
4040 			UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_srq,
4041 					  struct ib_uverbs_destroy_srq_resp),
4042 			UAPI_DEF_METHOD_NEEDS_FN(destroy_srq)),
4043 		DECLARE_UVERBS_WRITE(
4044 			IB_USER_VERBS_CMD_MODIFY_SRQ,
4045 			ib_uverbs_modify_srq,
4046 			UAPI_DEF_WRITE_UDATA_I(struct ib_uverbs_modify_srq),
4047 			UAPI_DEF_METHOD_NEEDS_FN(modify_srq)),
4048 		DECLARE_UVERBS_WRITE(
4049 			IB_USER_VERBS_CMD_POST_SRQ_RECV,
4050 			ib_uverbs_post_srq_recv,
4051 			UAPI_DEF_WRITE_IO(struct ib_uverbs_post_srq_recv,
4052 					  struct ib_uverbs_post_srq_recv_resp),
4053 			UAPI_DEF_METHOD_NEEDS_FN(post_srq_recv)),
4054 		DECLARE_UVERBS_WRITE(
4055 			IB_USER_VERBS_CMD_QUERY_SRQ,
4056 			ib_uverbs_query_srq,
4057 			UAPI_DEF_WRITE_IO(struct ib_uverbs_query_srq,
4058 					  struct ib_uverbs_query_srq_resp),
4059 			UAPI_DEF_METHOD_NEEDS_FN(query_srq))),
4060 
4061 	DECLARE_UVERBS_OBJECT(
4062 		UVERBS_OBJECT_XRCD,
4063 		DECLARE_UVERBS_WRITE(
4064 			IB_USER_VERBS_CMD_CLOSE_XRCD,
4065 			ib_uverbs_close_xrcd,
4066 			UAPI_DEF_WRITE_I(struct ib_uverbs_close_xrcd)),
4067 		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_QP,
4068 				     ib_uverbs_open_qp,
4069 				     UAPI_DEF_WRITE_UDATA_IO(
4070 					     struct ib_uverbs_open_qp,
4071 					     struct ib_uverbs_create_qp_resp)),
4072 		DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_XRCD,
4073 				     ib_uverbs_open_xrcd,
4074 				     UAPI_DEF_WRITE_UDATA_IO(
4075 					     struct ib_uverbs_open_xrcd,
4076 					     struct ib_uverbs_open_xrcd_resp)),
4077 		UAPI_DEF_OBJ_NEEDS_FN(alloc_xrcd),
4078 		UAPI_DEF_OBJ_NEEDS_FN(dealloc_xrcd)),
4079 
4080 	{},
4081 };
4082