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