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