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