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