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