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