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