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