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(sizeof *entry, GFP_KERNEL);
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,
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 (!capable(CAP_NET_RAW))
1316 return -EPERM;
1317 break;
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 (!capable(CAP_NET_RAW)) {
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(sizeof *attr, GFP_KERNEL);
1676 init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
1677 if (!attr || !init_attr) {
1678 ret = -ENOMEM;
1679 goto out;
1680 }
1681
1682 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
1683 if (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(sizeof(*attr), GFP_KERNEL);
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 ret = -EPERM;
1882 goto release_qp;
1883 }
1884 attr->qkey = cmd->base.qkey;
1885 }
1886 if (cmd->base.attr_mask & IB_QP_RQ_PSN)
1887 attr->rq_psn = cmd->base.rq_psn;
1888 if (cmd->base.attr_mask & IB_QP_SQ_PSN)
1889 attr->sq_psn = cmd->base.sq_psn;
1890 if (cmd->base.attr_mask & IB_QP_DEST_QPN)
1891 attr->dest_qp_num = cmd->base.dest_qp_num;
1892 if (cmd->base.attr_mask & IB_QP_ACCESS_FLAGS)
1893 attr->qp_access_flags = cmd->base.qp_access_flags;
1894 if (cmd->base.attr_mask & IB_QP_PKEY_INDEX)
1895 attr->pkey_index = cmd->base.pkey_index;
1896 if (cmd->base.attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
1897 attr->en_sqd_async_notify = cmd->base.en_sqd_async_notify;
1898 if (cmd->base.attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
1899 attr->max_rd_atomic = cmd->base.max_rd_atomic;
1900 if (cmd->base.attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1901 attr->max_dest_rd_atomic = cmd->base.max_dest_rd_atomic;
1902 if (cmd->base.attr_mask & IB_QP_MIN_RNR_TIMER)
1903 attr->min_rnr_timer = cmd->base.min_rnr_timer;
1904 if (cmd->base.attr_mask & IB_QP_PORT)
1905 attr->port_num = cmd->base.port_num;
1906 if (cmd->base.attr_mask & IB_QP_TIMEOUT)
1907 attr->timeout = cmd->base.timeout;
1908 if (cmd->base.attr_mask & IB_QP_RETRY_CNT)
1909 attr->retry_cnt = cmd->base.retry_cnt;
1910 if (cmd->base.attr_mask & IB_QP_RNR_RETRY)
1911 attr->rnr_retry = cmd->base.rnr_retry;
1912 if (cmd->base.attr_mask & IB_QP_ALT_PATH) {
1913 attr->alt_port_num = cmd->base.alt_port_num;
1914 attr->alt_timeout = cmd->base.alt_timeout;
1915 attr->alt_pkey_index = cmd->base.alt_pkey_index;
1916 }
1917 if (cmd->base.attr_mask & IB_QP_RATE_LIMIT)
1918 attr->rate_limit = cmd->rate_limit;
1919
1920 if (cmd->base.attr_mask & IB_QP_AV)
1921 copy_ah_attr_from_uverbs(qp->device, &attr->ah_attr,
1922 &cmd->base.dest);
1923
1924 if (cmd->base.attr_mask & IB_QP_ALT_PATH)
1925 copy_ah_attr_from_uverbs(qp->device, &attr->alt_ah_attr,
1926 &cmd->base.alt_dest);
1927
1928 ret = ib_modify_qp_with_udata(qp, attr,
1929 modify_qp_mask(qp->qp_type,
1930 cmd->base.attr_mask),
1931 &attrs->driver_udata);
1932
1933 release_qp:
1934 rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
1935 UVERBS_LOOKUP_READ);
1936 out:
1937 kfree(attr);
1938
1939 return ret;
1940 }
1941
ib_uverbs_modify_qp(struct uverbs_attr_bundle * attrs)1942 static int ib_uverbs_modify_qp(struct uverbs_attr_bundle *attrs)
1943 {
1944 struct ib_uverbs_ex_modify_qp cmd;
1945 int ret;
1946
1947 ret = uverbs_request(attrs, &cmd.base, sizeof(cmd.base));
1948 if (ret)
1949 return ret;
1950
1951 if (cmd.base.attr_mask & ~IB_QP_ATTR_STANDARD_BITS)
1952 return -EOPNOTSUPP;
1953
1954 return modify_qp(attrs, &cmd);
1955 }
1956
ib_uverbs_ex_modify_qp(struct uverbs_attr_bundle * attrs)1957 static int ib_uverbs_ex_modify_qp(struct uverbs_attr_bundle *attrs)
1958 {
1959 struct ib_uverbs_ex_modify_qp cmd;
1960 struct ib_uverbs_ex_modify_qp_resp resp = {
1961 .response_length = uverbs_response_length(attrs, sizeof(resp))
1962 };
1963 int ret;
1964
1965 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1966 if (ret)
1967 return ret;
1968
1969 /*
1970 * Last bit is reserved for extending the attr_mask by
1971 * using another field.
1972 */
1973 if (cmd.base.attr_mask & ~(IB_QP_ATTR_STANDARD_BITS | IB_QP_RATE_LIMIT))
1974 return -EOPNOTSUPP;
1975
1976 ret = modify_qp(attrs, &cmd);
1977 if (ret)
1978 return ret;
1979
1980 return uverbs_response(attrs, &resp, sizeof(resp));
1981 }
1982
ib_uverbs_destroy_qp(struct uverbs_attr_bundle * attrs)1983 static int ib_uverbs_destroy_qp(struct uverbs_attr_bundle *attrs)
1984 {
1985 struct ib_uverbs_destroy_qp cmd;
1986 struct ib_uverbs_destroy_qp_resp resp;
1987 struct ib_uobject *uobj;
1988 struct ib_uqp_object *obj;
1989 int ret;
1990
1991 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
1992 if (ret)
1993 return ret;
1994
1995 uobj = uobj_get_destroy(UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
1996 if (IS_ERR(uobj))
1997 return PTR_ERR(uobj);
1998
1999 obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
2000 memset(&resp, 0, sizeof(resp));
2001 resp.events_reported = obj->uevent.events_reported;
2002
2003 uobj_put_destroy(uobj);
2004
2005 return uverbs_response(attrs, &resp, sizeof(resp));
2006 }
2007
alloc_wr(size_t wr_size,__u32 num_sge)2008 static void *alloc_wr(size_t wr_size, __u32 num_sge)
2009 {
2010 if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof(struct ib_sge))) /
2011 sizeof(struct ib_sge))
2012 return NULL;
2013
2014 return kmalloc(ALIGN(wr_size, sizeof(struct ib_sge)) +
2015 num_sge * sizeof(struct ib_sge),
2016 GFP_KERNEL);
2017 }
2018
ib_uverbs_post_send(struct uverbs_attr_bundle * attrs)2019 static int ib_uverbs_post_send(struct uverbs_attr_bundle *attrs)
2020 {
2021 struct ib_uverbs_post_send cmd;
2022 struct ib_uverbs_post_send_resp resp;
2023 struct ib_uverbs_send_wr *user_wr;
2024 struct ib_send_wr *wr = NULL, *last, *next;
2025 const struct ib_send_wr *bad_wr;
2026 struct ib_qp *qp;
2027 int i, sg_ind;
2028 int is_ud;
2029 int ret, ret2;
2030 size_t next_size;
2031 const struct ib_sge __user *sgls;
2032 const void __user *wqes;
2033 struct uverbs_req_iter iter;
2034
2035 ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2036 if (ret)
2037 return ret;
2038 wqes = uverbs_request_next_ptr(&iter, size_mul(cmd.wqe_size,
2039 cmd.wr_count));
2040 if (IS_ERR(wqes))
2041 return PTR_ERR(wqes);
2042 sgls = uverbs_request_next_ptr(&iter,
2043 size_mul(cmd.sge_count,
2044 sizeof(struct ib_uverbs_sge)));
2045 if (IS_ERR(sgls))
2046 return PTR_ERR(sgls);
2047 ret = uverbs_request_finish(&iter);
2048 if (ret)
2049 return ret;
2050
2051 user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
2052 if (!user_wr)
2053 return -ENOMEM;
2054
2055 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2056 if (IS_ERR(qp)) {
2057 ret = PTR_ERR(qp);
2058 goto out;
2059 }
2060
2061 is_ud = qp->qp_type == IB_QPT_UD;
2062 sg_ind = 0;
2063 last = NULL;
2064 for (i = 0; i < cmd.wr_count; ++i) {
2065 if (copy_from_user(user_wr, wqes + i * cmd.wqe_size,
2066 cmd.wqe_size)) {
2067 ret = -EFAULT;
2068 goto out_put;
2069 }
2070
2071 if (user_wr->num_sge + sg_ind > cmd.sge_count) {
2072 ret = -EINVAL;
2073 goto out_put;
2074 }
2075
2076 if (is_ud) {
2077 struct ib_ud_wr *ud;
2078
2079 if (user_wr->opcode != IB_WR_SEND &&
2080 user_wr->opcode != IB_WR_SEND_WITH_IMM) {
2081 ret = -EINVAL;
2082 goto out_put;
2083 }
2084
2085 next_size = sizeof(*ud);
2086 ud = alloc_wr(next_size, user_wr->num_sge);
2087 if (!ud) {
2088 ret = -ENOMEM;
2089 goto out_put;
2090 }
2091
2092 ud->ah = uobj_get_obj_read(ah, UVERBS_OBJECT_AH,
2093 user_wr->wr.ud.ah, attrs);
2094 if (IS_ERR(ud->ah)) {
2095 ret = PTR_ERR(ud->ah);
2096 kfree(ud);
2097 goto out_put;
2098 }
2099 ud->remote_qpn = user_wr->wr.ud.remote_qpn;
2100 ud->remote_qkey = user_wr->wr.ud.remote_qkey;
2101
2102 next = &ud->wr;
2103 } else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
2104 user_wr->opcode == IB_WR_RDMA_WRITE ||
2105 user_wr->opcode == IB_WR_RDMA_READ) {
2106 struct ib_rdma_wr *rdma;
2107
2108 next_size = sizeof(*rdma);
2109 rdma = alloc_wr(next_size, user_wr->num_sge);
2110 if (!rdma) {
2111 ret = -ENOMEM;
2112 goto out_put;
2113 }
2114
2115 rdma->remote_addr = user_wr->wr.rdma.remote_addr;
2116 rdma->rkey = user_wr->wr.rdma.rkey;
2117
2118 next = &rdma->wr;
2119 } else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
2120 user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
2121 struct ib_atomic_wr *atomic;
2122
2123 next_size = sizeof(*atomic);
2124 atomic = alloc_wr(next_size, user_wr->num_sge);
2125 if (!atomic) {
2126 ret = -ENOMEM;
2127 goto out_put;
2128 }
2129
2130 atomic->remote_addr = user_wr->wr.atomic.remote_addr;
2131 atomic->compare_add = user_wr->wr.atomic.compare_add;
2132 atomic->swap = user_wr->wr.atomic.swap;
2133 atomic->rkey = user_wr->wr.atomic.rkey;
2134
2135 next = &atomic->wr;
2136 } else if (user_wr->opcode == IB_WR_SEND ||
2137 user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2138 user_wr->opcode == IB_WR_SEND_WITH_INV) {
2139 next_size = sizeof(*next);
2140 next = alloc_wr(next_size, user_wr->num_sge);
2141 if (!next) {
2142 ret = -ENOMEM;
2143 goto out_put;
2144 }
2145 } else {
2146 ret = -EINVAL;
2147 goto out_put;
2148 }
2149
2150 if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
2151 user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
2152 next->ex.imm_data =
2153 (__be32 __force) user_wr->ex.imm_data;
2154 } else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
2155 next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
2156 }
2157
2158 if (!last)
2159 wr = next;
2160 else
2161 last->next = next;
2162 last = next;
2163
2164 next->next = NULL;
2165 next->wr_id = user_wr->wr_id;
2166 next->num_sge = user_wr->num_sge;
2167 next->opcode = user_wr->opcode;
2168 next->send_flags = user_wr->send_flags;
2169
2170 if (next->num_sge) {
2171 next->sg_list = (void *) next +
2172 ALIGN(next_size, sizeof(struct ib_sge));
2173 if (copy_from_user(next->sg_list, sgls + sg_ind,
2174 next->num_sge *
2175 sizeof(struct ib_sge))) {
2176 ret = -EFAULT;
2177 goto out_put;
2178 }
2179 sg_ind += next->num_sge;
2180 } else
2181 next->sg_list = NULL;
2182 }
2183
2184 resp.bad_wr = 0;
2185 ret = qp->device->ops.post_send(qp->real_qp, wr, &bad_wr);
2186 if (ret)
2187 for (next = wr; next; next = next->next) {
2188 ++resp.bad_wr;
2189 if (next == bad_wr)
2190 break;
2191 }
2192
2193 ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2194 if (ret2)
2195 ret = ret2;
2196
2197 out_put:
2198 rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2199 UVERBS_LOOKUP_READ);
2200
2201 while (wr) {
2202 if (is_ud && ud_wr(wr)->ah)
2203 uobj_put_obj_read(ud_wr(wr)->ah);
2204 next = wr->next;
2205 kfree(wr);
2206 wr = next;
2207 }
2208
2209 out:
2210 kfree(user_wr);
2211
2212 return ret;
2213 }
2214
2215 static struct ib_recv_wr *
ib_uverbs_unmarshall_recv(struct uverbs_req_iter * iter,u32 wr_count,u32 wqe_size,u32 sge_count)2216 ib_uverbs_unmarshall_recv(struct uverbs_req_iter *iter, u32 wr_count,
2217 u32 wqe_size, u32 sge_count)
2218 {
2219 struct ib_uverbs_recv_wr *user_wr;
2220 struct ib_recv_wr *wr = NULL, *last, *next;
2221 int sg_ind;
2222 int i;
2223 int ret;
2224 const struct ib_sge __user *sgls;
2225 const void __user *wqes;
2226
2227 if (wqe_size < sizeof(struct ib_uverbs_recv_wr))
2228 return ERR_PTR(-EINVAL);
2229
2230 wqes = uverbs_request_next_ptr(iter, size_mul(wqe_size, wr_count));
2231 if (IS_ERR(wqes))
2232 return ERR_CAST(wqes);
2233 sgls = uverbs_request_next_ptr(iter, size_mul(sge_count,
2234 sizeof(struct ib_uverbs_sge)));
2235 if (IS_ERR(sgls))
2236 return ERR_CAST(sgls);
2237 ret = uverbs_request_finish(iter);
2238 if (ret)
2239 return ERR_PTR(ret);
2240
2241 user_wr = kmalloc(wqe_size, GFP_KERNEL);
2242 if (!user_wr)
2243 return ERR_PTR(-ENOMEM);
2244
2245 sg_ind = 0;
2246 last = NULL;
2247 for (i = 0; i < wr_count; ++i) {
2248 if (copy_from_user(user_wr, wqes + i * wqe_size,
2249 wqe_size)) {
2250 ret = -EFAULT;
2251 goto err;
2252 }
2253
2254 if (user_wr->num_sge + sg_ind > sge_count) {
2255 ret = -EINVAL;
2256 goto err;
2257 }
2258
2259 if (user_wr->num_sge >=
2260 (U32_MAX - ALIGN(sizeof(*next), sizeof(struct ib_sge))) /
2261 sizeof(struct ib_sge)) {
2262 ret = -EINVAL;
2263 goto err;
2264 }
2265
2266 next = kmalloc(ALIGN(sizeof(*next), sizeof(struct ib_sge)) +
2267 user_wr->num_sge * sizeof(struct ib_sge),
2268 GFP_KERNEL);
2269 if (!next) {
2270 ret = -ENOMEM;
2271 goto err;
2272 }
2273
2274 if (!last)
2275 wr = next;
2276 else
2277 last->next = next;
2278 last = next;
2279
2280 next->next = NULL;
2281 next->wr_id = user_wr->wr_id;
2282 next->num_sge = user_wr->num_sge;
2283
2284 if (next->num_sge) {
2285 next->sg_list = (void *)next +
2286 ALIGN(sizeof(*next), sizeof(struct ib_sge));
2287 if (copy_from_user(next->sg_list, sgls + sg_ind,
2288 next->num_sge *
2289 sizeof(struct ib_sge))) {
2290 ret = -EFAULT;
2291 goto err;
2292 }
2293 sg_ind += next->num_sge;
2294 } else
2295 next->sg_list = NULL;
2296 }
2297
2298 kfree(user_wr);
2299 return wr;
2300
2301 err:
2302 kfree(user_wr);
2303
2304 while (wr) {
2305 next = wr->next;
2306 kfree(wr);
2307 wr = next;
2308 }
2309
2310 return ERR_PTR(ret);
2311 }
2312
ib_uverbs_post_recv(struct uverbs_attr_bundle * attrs)2313 static int ib_uverbs_post_recv(struct uverbs_attr_bundle *attrs)
2314 {
2315 struct ib_uverbs_post_recv cmd;
2316 struct ib_uverbs_post_recv_resp resp;
2317 struct ib_recv_wr *wr, *next;
2318 const struct ib_recv_wr *bad_wr;
2319 struct ib_qp *qp;
2320 int ret, ret2;
2321 struct uverbs_req_iter iter;
2322
2323 ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2324 if (ret)
2325 return ret;
2326
2327 wr = ib_uverbs_unmarshall_recv(&iter, cmd.wr_count, cmd.wqe_size,
2328 cmd.sge_count);
2329 if (IS_ERR(wr))
2330 return PTR_ERR(wr);
2331
2332 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2333 if (IS_ERR(qp)) {
2334 ret = PTR_ERR(qp);
2335 goto out;
2336 }
2337
2338 resp.bad_wr = 0;
2339 ret = qp->device->ops.post_recv(qp->real_qp, wr, &bad_wr);
2340
2341 rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2342 UVERBS_LOOKUP_READ);
2343 if (ret) {
2344 for (next = wr; next; next = next->next) {
2345 ++resp.bad_wr;
2346 if (next == bad_wr)
2347 break;
2348 }
2349 }
2350
2351 ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2352 if (ret2)
2353 ret = ret2;
2354 out:
2355 while (wr) {
2356 next = wr->next;
2357 kfree(wr);
2358 wr = next;
2359 }
2360
2361 return ret;
2362 }
2363
ib_uverbs_post_srq_recv(struct uverbs_attr_bundle * attrs)2364 static int ib_uverbs_post_srq_recv(struct uverbs_attr_bundle *attrs)
2365 {
2366 struct ib_uverbs_post_srq_recv cmd;
2367 struct ib_uverbs_post_srq_recv_resp resp;
2368 struct ib_recv_wr *wr, *next;
2369 const struct ib_recv_wr *bad_wr;
2370 struct ib_srq *srq;
2371 int ret, ret2;
2372 struct uverbs_req_iter iter;
2373
2374 ret = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
2375 if (ret)
2376 return ret;
2377
2378 wr = ib_uverbs_unmarshall_recv(&iter, cmd.wr_count, cmd.wqe_size,
2379 cmd.sge_count);
2380 if (IS_ERR(wr))
2381 return PTR_ERR(wr);
2382
2383 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
2384 if (IS_ERR(srq)) {
2385 ret = PTR_ERR(srq);
2386 goto out;
2387 }
2388
2389 resp.bad_wr = 0;
2390 ret = srq->device->ops.post_srq_recv(srq, wr, &bad_wr);
2391
2392 rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
2393 UVERBS_LOOKUP_READ);
2394
2395 if (ret)
2396 for (next = wr; next; next = next->next) {
2397 ++resp.bad_wr;
2398 if (next == bad_wr)
2399 break;
2400 }
2401
2402 ret2 = uverbs_response(attrs, &resp, sizeof(resp));
2403 if (ret2)
2404 ret = ret2;
2405
2406 out:
2407 while (wr) {
2408 next = wr->next;
2409 kfree(wr);
2410 wr = next;
2411 }
2412
2413 return ret;
2414 }
2415
ib_uverbs_create_ah(struct uverbs_attr_bundle * attrs)2416 static int ib_uverbs_create_ah(struct uverbs_attr_bundle *attrs)
2417 {
2418 struct ib_uverbs_create_ah cmd;
2419 struct ib_uverbs_create_ah_resp resp;
2420 struct ib_uobject *uobj;
2421 struct ib_pd *pd;
2422 struct ib_ah *ah;
2423 struct rdma_ah_attr attr = {};
2424 int ret;
2425 struct ib_device *ib_dev;
2426
2427 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2428 if (ret)
2429 return ret;
2430
2431 uobj = uobj_alloc(UVERBS_OBJECT_AH, attrs, &ib_dev);
2432 if (IS_ERR(uobj))
2433 return PTR_ERR(uobj);
2434
2435 if (!rdma_is_port_valid(ib_dev, cmd.attr.port_num)) {
2436 ret = -EINVAL;
2437 goto err;
2438 }
2439
2440 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
2441 if (IS_ERR(pd)) {
2442 ret = PTR_ERR(pd);
2443 goto err;
2444 }
2445
2446 attr.type = rdma_ah_find_type(ib_dev, cmd.attr.port_num);
2447 rdma_ah_set_make_grd(&attr, false);
2448 rdma_ah_set_dlid(&attr, cmd.attr.dlid);
2449 rdma_ah_set_sl(&attr, cmd.attr.sl);
2450 rdma_ah_set_path_bits(&attr, cmd.attr.src_path_bits);
2451 rdma_ah_set_static_rate(&attr, cmd.attr.static_rate);
2452 rdma_ah_set_port_num(&attr, cmd.attr.port_num);
2453
2454 if (cmd.attr.is_global) {
2455 rdma_ah_set_grh(&attr, NULL, cmd.attr.grh.flow_label,
2456 cmd.attr.grh.sgid_index,
2457 cmd.attr.grh.hop_limit,
2458 cmd.attr.grh.traffic_class);
2459 rdma_ah_set_dgid_raw(&attr, cmd.attr.grh.dgid);
2460 } else {
2461 rdma_ah_set_ah_flags(&attr, 0);
2462 }
2463
2464 ah = rdma_create_user_ah(pd, &attr, &attrs->driver_udata);
2465 if (IS_ERR(ah)) {
2466 ret = PTR_ERR(ah);
2467 goto err_put;
2468 }
2469
2470 ah->uobject = uobj;
2471 uobj->user_handle = cmd.user_handle;
2472 uobj->object = ah;
2473 uobj_put_obj_read(pd);
2474 uobj_finalize_uobj_create(uobj, attrs);
2475
2476 resp.ah_handle = uobj->id;
2477 return uverbs_response(attrs, &resp, sizeof(resp));
2478
2479 err_put:
2480 uobj_put_obj_read(pd);
2481 err:
2482 uobj_alloc_abort(uobj, attrs);
2483 return ret;
2484 }
2485
ib_uverbs_destroy_ah(struct uverbs_attr_bundle * attrs)2486 static int ib_uverbs_destroy_ah(struct uverbs_attr_bundle *attrs)
2487 {
2488 struct ib_uverbs_destroy_ah cmd;
2489 int ret;
2490
2491 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2492 if (ret)
2493 return ret;
2494
2495 return uobj_perform_destroy(UVERBS_OBJECT_AH, cmd.ah_handle, attrs);
2496 }
2497
ib_uverbs_attach_mcast(struct uverbs_attr_bundle * attrs)2498 static int ib_uverbs_attach_mcast(struct uverbs_attr_bundle *attrs)
2499 {
2500 struct ib_uverbs_attach_mcast cmd;
2501 struct ib_qp *qp;
2502 struct ib_uqp_object *obj;
2503 struct ib_uverbs_mcast_entry *mcast;
2504 int ret;
2505
2506 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2507 if (ret)
2508 return ret;
2509
2510 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2511 if (IS_ERR(qp))
2512 return PTR_ERR(qp);
2513
2514 obj = qp->uobject;
2515
2516 mutex_lock(&obj->mcast_lock);
2517 list_for_each_entry(mcast, &obj->mcast_list, list)
2518 if (cmd.mlid == mcast->lid &&
2519 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2520 ret = 0;
2521 goto out_put;
2522 }
2523
2524 mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
2525 if (!mcast) {
2526 ret = -ENOMEM;
2527 goto out_put;
2528 }
2529
2530 mcast->lid = cmd.mlid;
2531 memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
2532
2533 ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
2534 if (!ret)
2535 list_add_tail(&mcast->list, &obj->mcast_list);
2536 else
2537 kfree(mcast);
2538
2539 out_put:
2540 mutex_unlock(&obj->mcast_lock);
2541 rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2542 UVERBS_LOOKUP_READ);
2543
2544 return ret;
2545 }
2546
ib_uverbs_detach_mcast(struct uverbs_attr_bundle * attrs)2547 static int ib_uverbs_detach_mcast(struct uverbs_attr_bundle *attrs)
2548 {
2549 struct ib_uverbs_detach_mcast cmd;
2550 struct ib_uqp_object *obj;
2551 struct ib_qp *qp;
2552 struct ib_uverbs_mcast_entry *mcast;
2553 int ret;
2554 bool found = false;
2555
2556 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
2557 if (ret)
2558 return ret;
2559
2560 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
2561 if (IS_ERR(qp))
2562 return PTR_ERR(qp);
2563
2564 obj = qp->uobject;
2565 mutex_lock(&obj->mcast_lock);
2566
2567 list_for_each_entry(mcast, &obj->mcast_list, list)
2568 if (cmd.mlid == mcast->lid &&
2569 !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
2570 list_del(&mcast->list);
2571 kfree(mcast);
2572 found = true;
2573 break;
2574 }
2575
2576 if (!found) {
2577 ret = -EINVAL;
2578 goto out_put;
2579 }
2580
2581 ret = ib_detach_mcast(qp, (union ib_gid *)cmd.gid, cmd.mlid);
2582
2583 out_put:
2584 mutex_unlock(&obj->mcast_lock);
2585 rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
2586 UVERBS_LOOKUP_READ);
2587 return ret;
2588 }
2589
flow_resources_alloc(size_t num_specs)2590 struct ib_uflow_resources *flow_resources_alloc(size_t num_specs)
2591 {
2592 struct ib_uflow_resources *resources;
2593
2594 resources = kzalloc(sizeof(*resources), GFP_KERNEL);
2595
2596 if (!resources)
2597 return NULL;
2598
2599 if (!num_specs)
2600 goto out;
2601
2602 resources->counters =
2603 kcalloc(num_specs, sizeof(*resources->counters), GFP_KERNEL);
2604 resources->collection =
2605 kcalloc(num_specs, sizeof(*resources->collection), GFP_KERNEL);
2606
2607 if (!resources->counters || !resources->collection)
2608 goto err;
2609
2610 out:
2611 resources->max = num_specs;
2612 return resources;
2613
2614 err:
2615 kfree(resources->counters);
2616 kfree(resources);
2617
2618 return NULL;
2619 }
2620 EXPORT_SYMBOL(flow_resources_alloc);
2621
ib_uverbs_flow_resources_free(struct ib_uflow_resources * uflow_res)2622 void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res)
2623 {
2624 unsigned int i;
2625
2626 if (!uflow_res)
2627 return;
2628
2629 for (i = 0; i < uflow_res->collection_num; i++)
2630 atomic_dec(&uflow_res->collection[i]->usecnt);
2631
2632 for (i = 0; i < uflow_res->counters_num; i++)
2633 atomic_dec(&uflow_res->counters[i]->usecnt);
2634
2635 kfree(uflow_res->collection);
2636 kfree(uflow_res->counters);
2637 kfree(uflow_res);
2638 }
2639 EXPORT_SYMBOL(ib_uverbs_flow_resources_free);
2640
flow_resources_add(struct ib_uflow_resources * uflow_res,enum ib_flow_spec_type type,void * ibobj)2641 void flow_resources_add(struct ib_uflow_resources *uflow_res,
2642 enum ib_flow_spec_type type,
2643 void *ibobj)
2644 {
2645 WARN_ON(uflow_res->num >= uflow_res->max);
2646
2647 switch (type) {
2648 case IB_FLOW_SPEC_ACTION_HANDLE:
2649 atomic_inc(&((struct ib_flow_action *)ibobj)->usecnt);
2650 uflow_res->collection[uflow_res->collection_num++] =
2651 (struct ib_flow_action *)ibobj;
2652 break;
2653 case IB_FLOW_SPEC_ACTION_COUNT:
2654 atomic_inc(&((struct ib_counters *)ibobj)->usecnt);
2655 uflow_res->counters[uflow_res->counters_num++] =
2656 (struct ib_counters *)ibobj;
2657 break;
2658 default:
2659 WARN_ON(1);
2660 }
2661
2662 uflow_res->num++;
2663 }
2664 EXPORT_SYMBOL(flow_resources_add);
2665
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)2666 static int kern_spec_to_ib_spec_action(struct uverbs_attr_bundle *attrs,
2667 struct ib_uverbs_flow_spec *kern_spec,
2668 union ib_flow_spec *ib_spec,
2669 struct ib_uflow_resources *uflow_res)
2670 {
2671 ib_spec->type = kern_spec->type;
2672 switch (ib_spec->type) {
2673 case IB_FLOW_SPEC_ACTION_TAG:
2674 if (kern_spec->flow_tag.size !=
2675 sizeof(struct ib_uverbs_flow_spec_action_tag))
2676 return -EINVAL;
2677
2678 ib_spec->flow_tag.size = sizeof(struct ib_flow_spec_action_tag);
2679 ib_spec->flow_tag.tag_id = kern_spec->flow_tag.tag_id;
2680 break;
2681 case IB_FLOW_SPEC_ACTION_DROP:
2682 if (kern_spec->drop.size !=
2683 sizeof(struct ib_uverbs_flow_spec_action_drop))
2684 return -EINVAL;
2685
2686 ib_spec->drop.size = sizeof(struct ib_flow_spec_action_drop);
2687 break;
2688 case IB_FLOW_SPEC_ACTION_HANDLE:
2689 if (kern_spec->action.size !=
2690 sizeof(struct ib_uverbs_flow_spec_action_handle))
2691 return -EOPNOTSUPP;
2692 ib_spec->action.act = uobj_get_obj_read(flow_action,
2693 UVERBS_OBJECT_FLOW_ACTION,
2694 kern_spec->action.handle,
2695 attrs);
2696 if (IS_ERR(ib_spec->action.act))
2697 return PTR_ERR(ib_spec->action.act);
2698 ib_spec->action.size =
2699 sizeof(struct ib_flow_spec_action_handle);
2700 flow_resources_add(uflow_res,
2701 IB_FLOW_SPEC_ACTION_HANDLE,
2702 ib_spec->action.act);
2703 uobj_put_obj_read(ib_spec->action.act);
2704 break;
2705 case IB_FLOW_SPEC_ACTION_COUNT:
2706 if (kern_spec->flow_count.size !=
2707 sizeof(struct ib_uverbs_flow_spec_action_count))
2708 return -EINVAL;
2709 ib_spec->flow_count.counters =
2710 uobj_get_obj_read(counters,
2711 UVERBS_OBJECT_COUNTERS,
2712 kern_spec->flow_count.handle,
2713 attrs);
2714 if (IS_ERR(ib_spec->flow_count.counters))
2715 return PTR_ERR(ib_spec->flow_count.counters);
2716 ib_spec->flow_count.size =
2717 sizeof(struct ib_flow_spec_action_count);
2718 flow_resources_add(uflow_res,
2719 IB_FLOW_SPEC_ACTION_COUNT,
2720 ib_spec->flow_count.counters);
2721 uobj_put_obj_read(ib_spec->flow_count.counters);
2722 break;
2723 default:
2724 return -EINVAL;
2725 }
2726 return 0;
2727 }
2728
spec_filter_size(const void * kern_spec_filter,u16 kern_filter_size,u16 ib_real_filter_sz)2729 static ssize_t spec_filter_size(const void *kern_spec_filter, u16 kern_filter_size,
2730 u16 ib_real_filter_sz)
2731 {
2732 /*
2733 * User space filter structures must be 64 bit aligned, otherwise this
2734 * may pass, but we won't handle additional new attributes.
2735 */
2736
2737 if (kern_filter_size > ib_real_filter_sz) {
2738 if (memchr_inv(kern_spec_filter +
2739 ib_real_filter_sz, 0,
2740 kern_filter_size - ib_real_filter_sz))
2741 return -EINVAL;
2742 return ib_real_filter_sz;
2743 }
2744 return kern_filter_size;
2745 }
2746
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)2747 int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
2748 const void *kern_spec_mask,
2749 const void *kern_spec_val,
2750 size_t kern_filter_sz,
2751 union ib_flow_spec *ib_spec)
2752 {
2753 ssize_t actual_filter_sz;
2754 ssize_t ib_filter_sz;
2755
2756 /* User flow spec size must be aligned to 4 bytes */
2757 if (kern_filter_sz != ALIGN(kern_filter_sz, 4))
2758 return -EINVAL;
2759
2760 ib_spec->type = type;
2761
2762 if (ib_spec->type == (IB_FLOW_SPEC_INNER | IB_FLOW_SPEC_VXLAN_TUNNEL))
2763 return -EINVAL;
2764
2765 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2766 case IB_FLOW_SPEC_ETH:
2767 ib_filter_sz = sizeof(struct ib_flow_eth_filter);
2768 actual_filter_sz = spec_filter_size(kern_spec_mask,
2769 kern_filter_sz,
2770 ib_filter_sz);
2771 if (actual_filter_sz <= 0)
2772 return -EINVAL;
2773 ib_spec->size = sizeof(struct ib_flow_spec_eth);
2774 memcpy(&ib_spec->eth.val, kern_spec_val, actual_filter_sz);
2775 memcpy(&ib_spec->eth.mask, kern_spec_mask, actual_filter_sz);
2776 break;
2777 case IB_FLOW_SPEC_IPV4:
2778 ib_filter_sz = sizeof(struct ib_flow_ipv4_filter);
2779 actual_filter_sz = spec_filter_size(kern_spec_mask,
2780 kern_filter_sz,
2781 ib_filter_sz);
2782 if (actual_filter_sz <= 0)
2783 return -EINVAL;
2784 ib_spec->size = sizeof(struct ib_flow_spec_ipv4);
2785 memcpy(&ib_spec->ipv4.val, kern_spec_val, actual_filter_sz);
2786 memcpy(&ib_spec->ipv4.mask, kern_spec_mask, actual_filter_sz);
2787 break;
2788 case IB_FLOW_SPEC_IPV6:
2789 ib_filter_sz = sizeof(struct ib_flow_ipv6_filter);
2790 actual_filter_sz = spec_filter_size(kern_spec_mask,
2791 kern_filter_sz,
2792 ib_filter_sz);
2793 if (actual_filter_sz <= 0)
2794 return -EINVAL;
2795 ib_spec->size = sizeof(struct ib_flow_spec_ipv6);
2796 memcpy(&ib_spec->ipv6.val, kern_spec_val, actual_filter_sz);
2797 memcpy(&ib_spec->ipv6.mask, kern_spec_mask, actual_filter_sz);
2798
2799 if ((ntohl(ib_spec->ipv6.mask.flow_label)) >= BIT(20) ||
2800 (ntohl(ib_spec->ipv6.val.flow_label)) >= BIT(20))
2801 return -EINVAL;
2802 break;
2803 case IB_FLOW_SPEC_TCP:
2804 case IB_FLOW_SPEC_UDP:
2805 ib_filter_sz = sizeof(struct ib_flow_tcp_udp_filter);
2806 actual_filter_sz = spec_filter_size(kern_spec_mask,
2807 kern_filter_sz,
2808 ib_filter_sz);
2809 if (actual_filter_sz <= 0)
2810 return -EINVAL;
2811 ib_spec->size = sizeof(struct ib_flow_spec_tcp_udp);
2812 memcpy(&ib_spec->tcp_udp.val, kern_spec_val, actual_filter_sz);
2813 memcpy(&ib_spec->tcp_udp.mask, kern_spec_mask, actual_filter_sz);
2814 break;
2815 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2816 ib_filter_sz = sizeof(struct ib_flow_tunnel_filter);
2817 actual_filter_sz = spec_filter_size(kern_spec_mask,
2818 kern_filter_sz,
2819 ib_filter_sz);
2820 if (actual_filter_sz <= 0)
2821 return -EINVAL;
2822 ib_spec->tunnel.size = sizeof(struct ib_flow_spec_tunnel);
2823 memcpy(&ib_spec->tunnel.val, kern_spec_val, actual_filter_sz);
2824 memcpy(&ib_spec->tunnel.mask, kern_spec_mask, actual_filter_sz);
2825
2826 if ((ntohl(ib_spec->tunnel.mask.tunnel_id)) >= BIT(24) ||
2827 (ntohl(ib_spec->tunnel.val.tunnel_id)) >= BIT(24))
2828 return -EINVAL;
2829 break;
2830 case IB_FLOW_SPEC_ESP:
2831 ib_filter_sz = sizeof(struct ib_flow_esp_filter);
2832 actual_filter_sz = spec_filter_size(kern_spec_mask,
2833 kern_filter_sz,
2834 ib_filter_sz);
2835 if (actual_filter_sz <= 0)
2836 return -EINVAL;
2837 ib_spec->esp.size = sizeof(struct ib_flow_spec_esp);
2838 memcpy(&ib_spec->esp.val, kern_spec_val, actual_filter_sz);
2839 memcpy(&ib_spec->esp.mask, kern_spec_mask, actual_filter_sz);
2840 break;
2841 case IB_FLOW_SPEC_GRE:
2842 ib_filter_sz = sizeof(struct ib_flow_gre_filter);
2843 actual_filter_sz = spec_filter_size(kern_spec_mask,
2844 kern_filter_sz,
2845 ib_filter_sz);
2846 if (actual_filter_sz <= 0)
2847 return -EINVAL;
2848 ib_spec->gre.size = sizeof(struct ib_flow_spec_gre);
2849 memcpy(&ib_spec->gre.val, kern_spec_val, actual_filter_sz);
2850 memcpy(&ib_spec->gre.mask, kern_spec_mask, actual_filter_sz);
2851 break;
2852 case IB_FLOW_SPEC_MPLS:
2853 ib_filter_sz = sizeof(struct ib_flow_mpls_filter);
2854 actual_filter_sz = spec_filter_size(kern_spec_mask,
2855 kern_filter_sz,
2856 ib_filter_sz);
2857 if (actual_filter_sz <= 0)
2858 return -EINVAL;
2859 ib_spec->mpls.size = sizeof(struct ib_flow_spec_mpls);
2860 memcpy(&ib_spec->mpls.val, kern_spec_val, actual_filter_sz);
2861 memcpy(&ib_spec->mpls.mask, kern_spec_mask, actual_filter_sz);
2862 break;
2863 default:
2864 return -EINVAL;
2865 }
2866 return 0;
2867 }
2868
kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec * kern_spec,union ib_flow_spec * ib_spec)2869 static int kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec *kern_spec,
2870 union ib_flow_spec *ib_spec)
2871 {
2872 size_t kern_filter_sz;
2873 void *kern_spec_mask;
2874 void *kern_spec_val;
2875
2876 if (check_sub_overflow((size_t)kern_spec->hdr.size,
2877 sizeof(struct ib_uverbs_flow_spec_hdr),
2878 &kern_filter_sz))
2879 return -EINVAL;
2880
2881 kern_filter_sz /= 2;
2882
2883 kern_spec_val = (void *)kern_spec +
2884 sizeof(struct ib_uverbs_flow_spec_hdr);
2885 kern_spec_mask = kern_spec_val + kern_filter_sz;
2886
2887 return ib_uverbs_kern_spec_to_ib_spec_filter(kern_spec->type,
2888 kern_spec_mask,
2889 kern_spec_val,
2890 kern_filter_sz, ib_spec);
2891 }
2892
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)2893 static int kern_spec_to_ib_spec(struct uverbs_attr_bundle *attrs,
2894 struct ib_uverbs_flow_spec *kern_spec,
2895 union ib_flow_spec *ib_spec,
2896 struct ib_uflow_resources *uflow_res)
2897 {
2898 if (kern_spec->reserved)
2899 return -EINVAL;
2900
2901 if (kern_spec->type >= IB_FLOW_SPEC_ACTION_TAG)
2902 return kern_spec_to_ib_spec_action(attrs, kern_spec, ib_spec,
2903 uflow_res);
2904 else
2905 return kern_spec_to_ib_spec_filter(kern_spec, ib_spec);
2906 }
2907
ib_uverbs_ex_create_wq(struct uverbs_attr_bundle * attrs)2908 static int ib_uverbs_ex_create_wq(struct uverbs_attr_bundle *attrs)
2909 {
2910 struct ib_uverbs_ex_create_wq cmd;
2911 struct ib_uverbs_ex_create_wq_resp resp = {};
2912 struct ib_uwq_object *obj;
2913 int err = 0;
2914 struct ib_cq *cq;
2915 struct ib_pd *pd;
2916 struct ib_wq *wq;
2917 struct ib_wq_init_attr wq_init_attr = {};
2918 struct ib_device *ib_dev;
2919
2920 err = uverbs_request(attrs, &cmd, sizeof(cmd));
2921 if (err)
2922 return err;
2923
2924 if (cmd.comp_mask)
2925 return -EOPNOTSUPP;
2926
2927 obj = (struct ib_uwq_object *)uobj_alloc(UVERBS_OBJECT_WQ, attrs,
2928 &ib_dev);
2929 if (IS_ERR(obj))
2930 return PTR_ERR(obj);
2931
2932 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, attrs);
2933 if (IS_ERR(pd)) {
2934 err = PTR_ERR(pd);
2935 goto err_uobj;
2936 }
2937
2938 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
2939 if (IS_ERR(cq)) {
2940 err = PTR_ERR(cq);
2941 goto err_put_pd;
2942 }
2943
2944 wq_init_attr.cq = cq;
2945 wq_init_attr.max_sge = cmd.max_sge;
2946 wq_init_attr.max_wr = cmd.max_wr;
2947 wq_init_attr.wq_type = cmd.wq_type;
2948 wq_init_attr.event_handler = ib_uverbs_wq_event_handler;
2949 wq_init_attr.create_flags = cmd.create_flags;
2950 INIT_LIST_HEAD(&obj->uevent.event_list);
2951 obj->uevent.uobject.user_handle = cmd.user_handle;
2952
2953 wq = pd->device->ops.create_wq(pd, &wq_init_attr, &attrs->driver_udata);
2954 if (IS_ERR(wq)) {
2955 err = PTR_ERR(wq);
2956 goto err_put_cq;
2957 }
2958
2959 wq->uobject = obj;
2960 obj->uevent.uobject.object = wq;
2961 wq->wq_type = wq_init_attr.wq_type;
2962 wq->cq = cq;
2963 wq->pd = pd;
2964 wq->device = pd->device;
2965 atomic_set(&wq->usecnt, 0);
2966 atomic_inc(&pd->usecnt);
2967 atomic_inc(&cq->usecnt);
2968 obj->uevent.event_file = READ_ONCE(attrs->ufile->default_async_file);
2969 if (obj->uevent.event_file)
2970 uverbs_uobject_get(&obj->uevent.event_file->uobj);
2971
2972 uobj_put_obj_read(pd);
2973 rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
2974 UVERBS_LOOKUP_READ);
2975 uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
2976
2977 resp.wq_handle = obj->uevent.uobject.id;
2978 resp.max_sge = wq_init_attr.max_sge;
2979 resp.max_wr = wq_init_attr.max_wr;
2980 resp.wqn = wq->wq_num;
2981 resp.response_length = uverbs_response_length(attrs, sizeof(resp));
2982 return uverbs_response(attrs, &resp, sizeof(resp));
2983
2984 err_put_cq:
2985 rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
2986 UVERBS_LOOKUP_READ);
2987 err_put_pd:
2988 uobj_put_obj_read(pd);
2989 err_uobj:
2990 uobj_alloc_abort(&obj->uevent.uobject, attrs);
2991
2992 return err;
2993 }
2994
ib_uverbs_ex_destroy_wq(struct uverbs_attr_bundle * attrs)2995 static int ib_uverbs_ex_destroy_wq(struct uverbs_attr_bundle *attrs)
2996 {
2997 struct ib_uverbs_ex_destroy_wq cmd;
2998 struct ib_uverbs_ex_destroy_wq_resp resp = {};
2999 struct ib_uobject *uobj;
3000 struct ib_uwq_object *obj;
3001 int ret;
3002
3003 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3004 if (ret)
3005 return ret;
3006
3007 if (cmd.comp_mask)
3008 return -EOPNOTSUPP;
3009
3010 resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3011 uobj = uobj_get_destroy(UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3012 if (IS_ERR(uobj))
3013 return PTR_ERR(uobj);
3014
3015 obj = container_of(uobj, struct ib_uwq_object, uevent.uobject);
3016 resp.events_reported = obj->uevent.events_reported;
3017
3018 uobj_put_destroy(uobj);
3019
3020 return uverbs_response(attrs, &resp, sizeof(resp));
3021 }
3022
ib_uverbs_ex_modify_wq(struct uverbs_attr_bundle * attrs)3023 static int ib_uverbs_ex_modify_wq(struct uverbs_attr_bundle *attrs)
3024 {
3025 struct ib_uverbs_ex_modify_wq cmd;
3026 struct ib_wq *wq;
3027 struct ib_wq_attr wq_attr = {};
3028 int ret;
3029
3030 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3031 if (ret)
3032 return ret;
3033
3034 if (!cmd.attr_mask)
3035 return -EINVAL;
3036
3037 if (cmd.attr_mask > (IB_WQ_STATE | IB_WQ_CUR_STATE | IB_WQ_FLAGS))
3038 return -EINVAL;
3039
3040 wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ, cmd.wq_handle, attrs);
3041 if (IS_ERR(wq))
3042 return PTR_ERR(wq);
3043
3044 if (cmd.attr_mask & IB_WQ_FLAGS) {
3045 wq_attr.flags = cmd.flags;
3046 wq_attr.flags_mask = cmd.flags_mask;
3047 }
3048
3049 if (cmd.attr_mask & IB_WQ_CUR_STATE) {
3050 if (cmd.curr_wq_state > IB_WQS_ERR)
3051 return -EINVAL;
3052
3053 wq_attr.curr_wq_state = cmd.curr_wq_state;
3054 } else {
3055 wq_attr.curr_wq_state = wq->state;
3056 }
3057
3058 if (cmd.attr_mask & IB_WQ_STATE) {
3059 if (cmd.wq_state > IB_WQS_ERR)
3060 return -EINVAL;
3061
3062 wq_attr.wq_state = cmd.wq_state;
3063 } else {
3064 wq_attr.wq_state = wq_attr.curr_wq_state;
3065 }
3066
3067 ret = wq->device->ops.modify_wq(wq, &wq_attr, cmd.attr_mask,
3068 &attrs->driver_udata);
3069 rdma_lookup_put_uobject(&wq->uobject->uevent.uobject,
3070 UVERBS_LOOKUP_READ);
3071 return ret;
3072 }
3073
ib_uverbs_ex_create_rwq_ind_table(struct uverbs_attr_bundle * attrs)3074 static int ib_uverbs_ex_create_rwq_ind_table(struct uverbs_attr_bundle *attrs)
3075 {
3076 struct ib_uverbs_ex_create_rwq_ind_table cmd;
3077 struct ib_uverbs_ex_create_rwq_ind_table_resp resp = {};
3078 struct ib_uobject *uobj;
3079 int err;
3080 struct ib_rwq_ind_table_init_attr init_attr = {};
3081 struct ib_rwq_ind_table *rwq_ind_tbl;
3082 struct ib_wq **wqs = NULL;
3083 u32 *wqs_handles = NULL;
3084 struct ib_wq *wq = NULL;
3085 int i, num_read_wqs;
3086 u32 num_wq_handles;
3087 struct uverbs_req_iter iter;
3088 struct ib_device *ib_dev;
3089
3090 err = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
3091 if (err)
3092 return err;
3093
3094 if (cmd.comp_mask)
3095 return -EOPNOTSUPP;
3096
3097 if (cmd.log_ind_tbl_size > IB_USER_VERBS_MAX_LOG_IND_TBL_SIZE)
3098 return -EINVAL;
3099
3100 num_wq_handles = 1 << cmd.log_ind_tbl_size;
3101 wqs_handles = kcalloc(num_wq_handles, sizeof(*wqs_handles),
3102 GFP_KERNEL);
3103 if (!wqs_handles)
3104 return -ENOMEM;
3105
3106 err = uverbs_request_next(&iter, wqs_handles,
3107 num_wq_handles * sizeof(__u32));
3108 if (err)
3109 goto err_free;
3110
3111 err = uverbs_request_finish(&iter);
3112 if (err)
3113 goto err_free;
3114
3115 wqs = kcalloc(num_wq_handles, sizeof(*wqs), GFP_KERNEL);
3116 if (!wqs) {
3117 err = -ENOMEM;
3118 goto err_free;
3119 }
3120
3121 for (num_read_wqs = 0; num_read_wqs < num_wq_handles;
3122 num_read_wqs++) {
3123 wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ,
3124 wqs_handles[num_read_wqs], attrs);
3125 if (IS_ERR(wq)) {
3126 err = PTR_ERR(wq);
3127 goto put_wqs;
3128 }
3129
3130 wqs[num_read_wqs] = wq;
3131 atomic_inc(&wqs[num_read_wqs]->usecnt);
3132 }
3133
3134 uobj = uobj_alloc(UVERBS_OBJECT_RWQ_IND_TBL, attrs, &ib_dev);
3135 if (IS_ERR(uobj)) {
3136 err = PTR_ERR(uobj);
3137 goto put_wqs;
3138 }
3139
3140 rwq_ind_tbl = rdma_zalloc_drv_obj(ib_dev, ib_rwq_ind_table);
3141 if (!rwq_ind_tbl) {
3142 err = -ENOMEM;
3143 goto err_uobj;
3144 }
3145
3146 init_attr.log_ind_tbl_size = cmd.log_ind_tbl_size;
3147 init_attr.ind_tbl = wqs;
3148
3149 rwq_ind_tbl->ind_tbl = wqs;
3150 rwq_ind_tbl->log_ind_tbl_size = init_attr.log_ind_tbl_size;
3151 rwq_ind_tbl->uobject = uobj;
3152 uobj->object = rwq_ind_tbl;
3153 rwq_ind_tbl->device = ib_dev;
3154 atomic_set(&rwq_ind_tbl->usecnt, 0);
3155
3156 err = ib_dev->ops.create_rwq_ind_table(rwq_ind_tbl, &init_attr,
3157 &attrs->driver_udata);
3158 if (err)
3159 goto err_create;
3160
3161 for (i = 0; i < num_wq_handles; i++)
3162 rdma_lookup_put_uobject(&wqs[i]->uobject->uevent.uobject,
3163 UVERBS_LOOKUP_READ);
3164 kfree(wqs_handles);
3165 uobj_finalize_uobj_create(uobj, attrs);
3166
3167 resp.ind_tbl_handle = uobj->id;
3168 resp.ind_tbl_num = rwq_ind_tbl->ind_tbl_num;
3169 resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3170 return uverbs_response(attrs, &resp, sizeof(resp));
3171
3172 err_create:
3173 kfree(rwq_ind_tbl);
3174 err_uobj:
3175 uobj_alloc_abort(uobj, attrs);
3176 put_wqs:
3177 for (i = 0; i < num_read_wqs; i++) {
3178 rdma_lookup_put_uobject(&wqs[i]->uobject->uevent.uobject,
3179 UVERBS_LOOKUP_READ);
3180 atomic_dec(&wqs[i]->usecnt);
3181 }
3182 err_free:
3183 kfree(wqs_handles);
3184 kfree(wqs);
3185 return err;
3186 }
3187
ib_uverbs_ex_destroy_rwq_ind_table(struct uverbs_attr_bundle * attrs)3188 static int ib_uverbs_ex_destroy_rwq_ind_table(struct uverbs_attr_bundle *attrs)
3189 {
3190 struct ib_uverbs_ex_destroy_rwq_ind_table cmd;
3191 int ret;
3192
3193 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3194 if (ret)
3195 return ret;
3196
3197 if (cmd.comp_mask)
3198 return -EOPNOTSUPP;
3199
3200 return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL,
3201 cmd.ind_tbl_handle, attrs);
3202 }
3203
ib_uverbs_ex_create_flow(struct uverbs_attr_bundle * attrs)3204 static int ib_uverbs_ex_create_flow(struct uverbs_attr_bundle *attrs)
3205 {
3206 struct ib_uverbs_create_flow cmd;
3207 struct ib_uverbs_create_flow_resp resp = {};
3208 struct ib_uobject *uobj;
3209 struct ib_flow *flow_id;
3210 struct ib_uverbs_flow_attr *kern_flow_attr;
3211 struct ib_flow_attr *flow_attr;
3212 struct ib_qp *qp;
3213 struct ib_uflow_resources *uflow_res;
3214 struct ib_uverbs_flow_spec_hdr *kern_spec;
3215 struct uverbs_req_iter iter;
3216 int err;
3217 void *ib_spec;
3218 int i;
3219 struct ib_device *ib_dev;
3220
3221 err = uverbs_request_start(attrs, &iter, &cmd, sizeof(cmd));
3222 if (err)
3223 return err;
3224
3225 if (cmd.comp_mask)
3226 return -EINVAL;
3227
3228 if (!capable(CAP_NET_RAW))
3229 return -EPERM;
3230
3231 if (cmd.flow_attr.flags >= IB_FLOW_ATTR_FLAGS_RESERVED)
3232 return -EINVAL;
3233
3234 if ((cmd.flow_attr.flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
3235 ((cmd.flow_attr.type == IB_FLOW_ATTR_ALL_DEFAULT) ||
3236 (cmd.flow_attr.type == IB_FLOW_ATTR_MC_DEFAULT)))
3237 return -EINVAL;
3238
3239 if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
3240 return -EINVAL;
3241
3242 if (cmd.flow_attr.size >
3243 (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
3244 return -EINVAL;
3245
3246 if (cmd.flow_attr.reserved[0] ||
3247 cmd.flow_attr.reserved[1])
3248 return -EINVAL;
3249
3250 if (cmd.flow_attr.num_of_specs) {
3251 kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
3252 GFP_KERNEL);
3253 if (!kern_flow_attr)
3254 return -ENOMEM;
3255
3256 *kern_flow_attr = cmd.flow_attr;
3257 err = uverbs_request_next(&iter, &kern_flow_attr->flow_specs,
3258 cmd.flow_attr.size);
3259 if (err)
3260 goto err_free_attr;
3261 } else {
3262 kern_flow_attr = &cmd.flow_attr;
3263 }
3264
3265 err = uverbs_request_finish(&iter);
3266 if (err)
3267 goto err_free_attr;
3268
3269 uobj = uobj_alloc(UVERBS_OBJECT_FLOW, attrs, &ib_dev);
3270 if (IS_ERR(uobj)) {
3271 err = PTR_ERR(uobj);
3272 goto err_free_attr;
3273 }
3274
3275 if (!rdma_is_port_valid(uobj->context->device, cmd.flow_attr.port)) {
3276 err = -EINVAL;
3277 goto err_uobj;
3278 }
3279
3280 qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, attrs);
3281 if (IS_ERR(qp)) {
3282 err = PTR_ERR(qp);
3283 goto err_uobj;
3284 }
3285
3286 if (qp->qp_type != IB_QPT_UD && qp->qp_type != IB_QPT_RAW_PACKET) {
3287 err = -EINVAL;
3288 goto err_put;
3289 }
3290
3291 flow_attr = kzalloc(struct_size(flow_attr, flows,
3292 cmd.flow_attr.num_of_specs), GFP_KERNEL);
3293 if (!flow_attr) {
3294 err = -ENOMEM;
3295 goto err_put;
3296 }
3297 uflow_res = flow_resources_alloc(cmd.flow_attr.num_of_specs);
3298 if (!uflow_res) {
3299 err = -ENOMEM;
3300 goto err_free_flow_attr;
3301 }
3302
3303 flow_attr->type = kern_flow_attr->type;
3304 flow_attr->priority = kern_flow_attr->priority;
3305 flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
3306 flow_attr->port = kern_flow_attr->port;
3307 flow_attr->flags = kern_flow_attr->flags;
3308 flow_attr->size = sizeof(*flow_attr);
3309
3310 kern_spec = kern_flow_attr->flow_specs;
3311 ib_spec = flow_attr + 1;
3312 for (i = 0; i < flow_attr->num_of_specs &&
3313 cmd.flow_attr.size >= sizeof(*kern_spec) &&
3314 cmd.flow_attr.size >= kern_spec->size;
3315 i++) {
3316 err = kern_spec_to_ib_spec(
3317 attrs, (struct ib_uverbs_flow_spec *)kern_spec,
3318 ib_spec, uflow_res);
3319 if (err)
3320 goto err_free;
3321
3322 flow_attr->size +=
3323 ((union ib_flow_spec *) ib_spec)->size;
3324 cmd.flow_attr.size -= kern_spec->size;
3325 kern_spec = ((void *)kern_spec) + kern_spec->size;
3326 ib_spec += ((union ib_flow_spec *) ib_spec)->size;
3327 }
3328 if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
3329 pr_warn("create flow failed, flow %d: %u bytes left from uverb cmd\n",
3330 i, cmd.flow_attr.size);
3331 err = -EINVAL;
3332 goto err_free;
3333 }
3334
3335 flow_id = qp->device->ops.create_flow(qp, flow_attr,
3336 &attrs->driver_udata);
3337
3338 if (IS_ERR(flow_id)) {
3339 err = PTR_ERR(flow_id);
3340 goto err_free;
3341 }
3342
3343 ib_set_flow(uobj, flow_id, qp, qp->device, uflow_res);
3344
3345 rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
3346 UVERBS_LOOKUP_READ);
3347 kfree(flow_attr);
3348
3349 if (cmd.flow_attr.num_of_specs)
3350 kfree(kern_flow_attr);
3351 uobj_finalize_uobj_create(uobj, attrs);
3352
3353 resp.flow_handle = uobj->id;
3354 return uverbs_response(attrs, &resp, sizeof(resp));
3355
3356 err_free:
3357 ib_uverbs_flow_resources_free(uflow_res);
3358 err_free_flow_attr:
3359 kfree(flow_attr);
3360 err_put:
3361 rdma_lookup_put_uobject(&qp->uobject->uevent.uobject,
3362 UVERBS_LOOKUP_READ);
3363 err_uobj:
3364 uobj_alloc_abort(uobj, attrs);
3365 err_free_attr:
3366 if (cmd.flow_attr.num_of_specs)
3367 kfree(kern_flow_attr);
3368 return err;
3369 }
3370
ib_uverbs_ex_destroy_flow(struct uverbs_attr_bundle * attrs)3371 static int ib_uverbs_ex_destroy_flow(struct uverbs_attr_bundle *attrs)
3372 {
3373 struct ib_uverbs_destroy_flow cmd;
3374 int ret;
3375
3376 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3377 if (ret)
3378 return ret;
3379
3380 if (cmd.comp_mask)
3381 return -EINVAL;
3382
3383 return uobj_perform_destroy(UVERBS_OBJECT_FLOW, cmd.flow_handle, attrs);
3384 }
3385
__uverbs_create_xsrq(struct uverbs_attr_bundle * attrs,struct ib_uverbs_create_xsrq * cmd,struct ib_udata * udata)3386 static int __uverbs_create_xsrq(struct uverbs_attr_bundle *attrs,
3387 struct ib_uverbs_create_xsrq *cmd,
3388 struct ib_udata *udata)
3389 {
3390 struct ib_uverbs_create_srq_resp resp = {};
3391 struct ib_usrq_object *obj;
3392 struct ib_pd *pd;
3393 struct ib_srq *srq;
3394 struct ib_srq_init_attr attr;
3395 int ret;
3396 struct ib_uobject *xrcd_uobj;
3397 struct ib_device *ib_dev;
3398
3399 obj = (struct ib_usrq_object *)uobj_alloc(UVERBS_OBJECT_SRQ, attrs,
3400 &ib_dev);
3401 if (IS_ERR(obj))
3402 return PTR_ERR(obj);
3403
3404 if (cmd->srq_type == IB_SRQT_TM)
3405 attr.ext.tag_matching.max_num_tags = cmd->max_num_tags;
3406
3407 if (cmd->srq_type == IB_SRQT_XRC) {
3408 xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->xrcd_handle,
3409 attrs);
3410 if (IS_ERR(xrcd_uobj)) {
3411 ret = -EINVAL;
3412 goto err;
3413 }
3414
3415 attr.ext.xrc.xrcd = (struct ib_xrcd *)xrcd_uobj->object;
3416 if (!attr.ext.xrc.xrcd) {
3417 ret = -EINVAL;
3418 goto err_put_xrcd;
3419 }
3420
3421 obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
3422 atomic_inc(&obj->uxrcd->refcnt);
3423 }
3424
3425 if (ib_srq_has_cq(cmd->srq_type)) {
3426 attr.ext.cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
3427 cmd->cq_handle, attrs);
3428 if (IS_ERR(attr.ext.cq)) {
3429 ret = PTR_ERR(attr.ext.cq);
3430 goto err_put_xrcd;
3431 }
3432 }
3433
3434 pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle, attrs);
3435 if (IS_ERR(pd)) {
3436 ret = PTR_ERR(pd);
3437 goto err_put_cq;
3438 }
3439
3440 attr.event_handler = ib_uverbs_srq_event_handler;
3441 attr.srq_type = cmd->srq_type;
3442 attr.attr.max_wr = cmd->max_wr;
3443 attr.attr.max_sge = cmd->max_sge;
3444 attr.attr.srq_limit = cmd->srq_limit;
3445
3446 INIT_LIST_HEAD(&obj->uevent.event_list);
3447 obj->uevent.uobject.user_handle = cmd->user_handle;
3448
3449 srq = ib_create_srq_user(pd, &attr, obj, udata);
3450 if (IS_ERR(srq)) {
3451 ret = PTR_ERR(srq);
3452 goto err_put_pd;
3453 }
3454
3455 obj->uevent.uobject.object = srq;
3456 obj->uevent.uobject.user_handle = cmd->user_handle;
3457 obj->uevent.event_file = READ_ONCE(attrs->ufile->default_async_file);
3458 if (obj->uevent.event_file)
3459 uverbs_uobject_get(&obj->uevent.event_file->uobj);
3460
3461 if (cmd->srq_type == IB_SRQT_XRC)
3462 resp.srqn = srq->ext.xrc.srq_num;
3463
3464 if (cmd->srq_type == IB_SRQT_XRC)
3465 uobj_put_read(xrcd_uobj);
3466
3467 if (ib_srq_has_cq(cmd->srq_type))
3468 rdma_lookup_put_uobject(&attr.ext.cq->uobject->uevent.uobject,
3469 UVERBS_LOOKUP_READ);
3470
3471 uobj_put_obj_read(pd);
3472 uobj_finalize_uobj_create(&obj->uevent.uobject, attrs);
3473
3474 resp.srq_handle = obj->uevent.uobject.id;
3475 resp.max_wr = attr.attr.max_wr;
3476 resp.max_sge = attr.attr.max_sge;
3477 return uverbs_response(attrs, &resp, sizeof(resp));
3478
3479 err_put_pd:
3480 uobj_put_obj_read(pd);
3481 err_put_cq:
3482 if (ib_srq_has_cq(cmd->srq_type))
3483 rdma_lookup_put_uobject(&attr.ext.cq->uobject->uevent.uobject,
3484 UVERBS_LOOKUP_READ);
3485
3486 err_put_xrcd:
3487 if (cmd->srq_type == IB_SRQT_XRC) {
3488 atomic_dec(&obj->uxrcd->refcnt);
3489 uobj_put_read(xrcd_uobj);
3490 }
3491
3492 err:
3493 uobj_alloc_abort(&obj->uevent.uobject, attrs);
3494 return ret;
3495 }
3496
ib_uverbs_create_srq(struct uverbs_attr_bundle * attrs)3497 static int ib_uverbs_create_srq(struct uverbs_attr_bundle *attrs)
3498 {
3499 struct ib_uverbs_create_srq cmd;
3500 struct ib_uverbs_create_xsrq xcmd;
3501 int ret;
3502
3503 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3504 if (ret)
3505 return ret;
3506
3507 memset(&xcmd, 0, sizeof(xcmd));
3508 xcmd.response = cmd.response;
3509 xcmd.user_handle = cmd.user_handle;
3510 xcmd.srq_type = IB_SRQT_BASIC;
3511 xcmd.pd_handle = cmd.pd_handle;
3512 xcmd.max_wr = cmd.max_wr;
3513 xcmd.max_sge = cmd.max_sge;
3514 xcmd.srq_limit = cmd.srq_limit;
3515
3516 return __uverbs_create_xsrq(attrs, &xcmd, &attrs->driver_udata);
3517 }
3518
ib_uverbs_create_xsrq(struct uverbs_attr_bundle * attrs)3519 static int ib_uverbs_create_xsrq(struct uverbs_attr_bundle *attrs)
3520 {
3521 struct ib_uverbs_create_xsrq cmd;
3522 int ret;
3523
3524 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3525 if (ret)
3526 return ret;
3527
3528 return __uverbs_create_xsrq(attrs, &cmd, &attrs->driver_udata);
3529 }
3530
ib_uverbs_modify_srq(struct uverbs_attr_bundle * attrs)3531 static int ib_uverbs_modify_srq(struct uverbs_attr_bundle *attrs)
3532 {
3533 struct ib_uverbs_modify_srq cmd;
3534 struct ib_srq *srq;
3535 struct ib_srq_attr attr;
3536 int ret;
3537
3538 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3539 if (ret)
3540 return ret;
3541
3542 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3543 if (IS_ERR(srq))
3544 return PTR_ERR(srq);
3545
3546 attr.max_wr = cmd.max_wr;
3547 attr.srq_limit = cmd.srq_limit;
3548
3549 ret = srq->device->ops.modify_srq(srq, &attr, cmd.attr_mask,
3550 &attrs->driver_udata);
3551
3552 rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
3553 UVERBS_LOOKUP_READ);
3554
3555 return ret;
3556 }
3557
ib_uverbs_query_srq(struct uverbs_attr_bundle * attrs)3558 static int ib_uverbs_query_srq(struct uverbs_attr_bundle *attrs)
3559 {
3560 struct ib_uverbs_query_srq cmd;
3561 struct ib_uverbs_query_srq_resp resp;
3562 struct ib_srq_attr attr;
3563 struct ib_srq *srq;
3564 int ret;
3565
3566 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3567 if (ret)
3568 return ret;
3569
3570 srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3571 if (IS_ERR(srq))
3572 return PTR_ERR(srq);
3573
3574 ret = ib_query_srq(srq, &attr);
3575
3576 rdma_lookup_put_uobject(&srq->uobject->uevent.uobject,
3577 UVERBS_LOOKUP_READ);
3578
3579 if (ret)
3580 return ret;
3581
3582 memset(&resp, 0, sizeof resp);
3583
3584 resp.max_wr = attr.max_wr;
3585 resp.max_sge = attr.max_sge;
3586 resp.srq_limit = attr.srq_limit;
3587
3588 return uverbs_response(attrs, &resp, sizeof(resp));
3589 }
3590
ib_uverbs_destroy_srq(struct uverbs_attr_bundle * attrs)3591 static int ib_uverbs_destroy_srq(struct uverbs_attr_bundle *attrs)
3592 {
3593 struct ib_uverbs_destroy_srq cmd;
3594 struct ib_uverbs_destroy_srq_resp resp;
3595 struct ib_uobject *uobj;
3596 struct ib_uevent_object *obj;
3597 int ret;
3598
3599 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3600 if (ret)
3601 return ret;
3602
3603 uobj = uobj_get_destroy(UVERBS_OBJECT_SRQ, cmd.srq_handle, attrs);
3604 if (IS_ERR(uobj))
3605 return PTR_ERR(uobj);
3606
3607 obj = container_of(uobj, struct ib_uevent_object, uobject);
3608 memset(&resp, 0, sizeof(resp));
3609 resp.events_reported = obj->events_reported;
3610
3611 uobj_put_destroy(uobj);
3612
3613 return uverbs_response(attrs, &resp, sizeof(resp));
3614 }
3615
ib_uverbs_ex_query_device(struct uverbs_attr_bundle * attrs)3616 static int ib_uverbs_ex_query_device(struct uverbs_attr_bundle *attrs)
3617 {
3618 struct ib_uverbs_ex_query_device_resp resp = {};
3619 struct ib_uverbs_ex_query_device cmd;
3620 struct ib_device_attr attr = {0};
3621 struct ib_ucontext *ucontext;
3622 struct ib_device *ib_dev;
3623 int err;
3624
3625 ucontext = ib_uverbs_get_ucontext(attrs);
3626 if (IS_ERR(ucontext))
3627 return PTR_ERR(ucontext);
3628 ib_dev = ucontext->device;
3629
3630 err = uverbs_request(attrs, &cmd, sizeof(cmd));
3631 if (err)
3632 return err;
3633
3634 if (cmd.comp_mask)
3635 return -EINVAL;
3636
3637 if (cmd.reserved)
3638 return -EINVAL;
3639
3640 err = ib_dev->ops.query_device(ib_dev, &attr, &attrs->driver_udata);
3641 if (err)
3642 return err;
3643
3644 copy_query_dev_fields(ucontext, &resp.base, &attr);
3645
3646 resp.odp_caps.general_caps = attr.odp_caps.general_caps;
3647 resp.odp_caps.per_transport_caps.rc_odp_caps =
3648 attr.odp_caps.per_transport_caps.rc_odp_caps;
3649 resp.odp_caps.per_transport_caps.uc_odp_caps =
3650 attr.odp_caps.per_transport_caps.uc_odp_caps;
3651 resp.odp_caps.per_transport_caps.ud_odp_caps =
3652 attr.odp_caps.per_transport_caps.ud_odp_caps;
3653 resp.xrc_odp_caps = attr.odp_caps.per_transport_caps.xrc_odp_caps;
3654
3655 resp.timestamp_mask = attr.timestamp_mask;
3656 resp.hca_core_clock = attr.hca_core_clock;
3657 resp.device_cap_flags_ex = attr.device_cap_flags;
3658 resp.rss_caps.supported_qpts = attr.rss_caps.supported_qpts;
3659 resp.rss_caps.max_rwq_indirection_tables =
3660 attr.rss_caps.max_rwq_indirection_tables;
3661 resp.rss_caps.max_rwq_indirection_table_size =
3662 attr.rss_caps.max_rwq_indirection_table_size;
3663 resp.max_wq_type_rq = attr.max_wq_type_rq;
3664 resp.raw_packet_caps = attr.raw_packet_caps;
3665 resp.tm_caps.max_rndv_hdr_size = attr.tm_caps.max_rndv_hdr_size;
3666 resp.tm_caps.max_num_tags = attr.tm_caps.max_num_tags;
3667 resp.tm_caps.max_ops = attr.tm_caps.max_ops;
3668 resp.tm_caps.max_sge = attr.tm_caps.max_sge;
3669 resp.tm_caps.flags = attr.tm_caps.flags;
3670 resp.cq_moderation_caps.max_cq_moderation_count =
3671 attr.cq_caps.max_cq_moderation_count;
3672 resp.cq_moderation_caps.max_cq_moderation_period =
3673 attr.cq_caps.max_cq_moderation_period;
3674 resp.max_dm_size = attr.max_dm_size;
3675 resp.response_length = uverbs_response_length(attrs, sizeof(resp));
3676
3677 return uverbs_response(attrs, &resp, sizeof(resp));
3678 }
3679
ib_uverbs_ex_modify_cq(struct uverbs_attr_bundle * attrs)3680 static int ib_uverbs_ex_modify_cq(struct uverbs_attr_bundle *attrs)
3681 {
3682 struct ib_uverbs_ex_modify_cq cmd;
3683 struct ib_cq *cq;
3684 int ret;
3685
3686 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
3687 if (ret)
3688 return ret;
3689
3690 if (!cmd.attr_mask || cmd.reserved)
3691 return -EINVAL;
3692
3693 if (cmd.attr_mask > IB_CQ_MODERATE)
3694 return -EOPNOTSUPP;
3695
3696 cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, attrs);
3697 if (IS_ERR(cq))
3698 return PTR_ERR(cq);
3699
3700 ret = rdma_set_cq_moderation(cq, cmd.attr.cq_count, cmd.attr.cq_period);
3701
3702 rdma_lookup_put_uobject(&cq->uobject->uevent.uobject,
3703 UVERBS_LOOKUP_READ);
3704 return ret;
3705 }
3706
3707 /*
3708 * Describe the input structs for write(). Some write methods have an input
3709 * only struct, most have an input and output. If the struct has an output then
3710 * the 'response' u64 must be the first field in the request structure.
3711 *
3712 * If udata is present then both the request and response structs have a
3713 * trailing driver_data flex array. In this case the size of the base struct
3714 * cannot be changed.
3715 */
3716 #define UAPI_DEF_WRITE_IO(req, resp) \
3717 .write.has_resp = 1 + \
3718 BUILD_BUG_ON_ZERO(offsetof(req, response) != 0) + \
3719 BUILD_BUG_ON_ZERO(sizeof_field(req, response) != \
3720 sizeof(u64)), \
3721 .write.req_size = sizeof(req), .write.resp_size = sizeof(resp)
3722
3723 #define UAPI_DEF_WRITE_I(req) .write.req_size = sizeof(req)
3724
3725 #define UAPI_DEF_WRITE_UDATA_IO(req, resp) \
3726 UAPI_DEF_WRITE_IO(req, resp), \
3727 .write.has_udata = \
3728 1 + \
3729 BUILD_BUG_ON_ZERO(offsetof(req, driver_data) != \
3730 sizeof(req)) + \
3731 BUILD_BUG_ON_ZERO(offsetof(resp, driver_data) != \
3732 sizeof(resp))
3733
3734 #define UAPI_DEF_WRITE_UDATA_I(req) \
3735 UAPI_DEF_WRITE_I(req), \
3736 .write.has_udata = \
3737 1 + BUILD_BUG_ON_ZERO(offsetof(req, driver_data) != \
3738 sizeof(req))
3739
3740 /*
3741 * The _EX versions are for use with WRITE_EX and allow the last struct member
3742 * to be specified. Buffers that do not include that member will be rejected.
3743 */
3744 #define UAPI_DEF_WRITE_IO_EX(req, req_last_member, resp, resp_last_member) \
3745 .write.has_resp = 1, \
3746 .write.req_size = offsetofend(req, req_last_member), \
3747 .write.resp_size = offsetofend(resp, resp_last_member)
3748
3749 #define UAPI_DEF_WRITE_I_EX(req, req_last_member) \
3750 .write.req_size = offsetofend(req, req_last_member)
3751
3752 const struct uapi_definition uverbs_def_write_intf[] = {
3753 DECLARE_UVERBS_OBJECT(
3754 UVERBS_OBJECT_AH,
3755 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_AH,
3756 ib_uverbs_create_ah,
3757 UAPI_DEF_WRITE_UDATA_IO(
3758 struct ib_uverbs_create_ah,
3759 struct ib_uverbs_create_ah_resp)),
3760 DECLARE_UVERBS_WRITE(
3761 IB_USER_VERBS_CMD_DESTROY_AH,
3762 ib_uverbs_destroy_ah,
3763 UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_ah)),
3764 UAPI_DEF_OBJ_NEEDS_FN(create_user_ah),
3765 UAPI_DEF_OBJ_NEEDS_FN(destroy_ah)),
3766
3767 DECLARE_UVERBS_OBJECT(
3768 UVERBS_OBJECT_COMP_CHANNEL,
3769 DECLARE_UVERBS_WRITE(
3770 IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL,
3771 ib_uverbs_create_comp_channel,
3772 UAPI_DEF_WRITE_IO(
3773 struct ib_uverbs_create_comp_channel,
3774 struct ib_uverbs_create_comp_channel_resp))),
3775
3776 DECLARE_UVERBS_OBJECT(
3777 UVERBS_OBJECT_CQ,
3778 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_CQ,
3779 ib_uverbs_create_cq,
3780 UAPI_DEF_WRITE_UDATA_IO(
3781 struct ib_uverbs_create_cq,
3782 struct ib_uverbs_create_cq_resp),
3783 UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
3784 DECLARE_UVERBS_WRITE(
3785 IB_USER_VERBS_CMD_DESTROY_CQ,
3786 ib_uverbs_destroy_cq,
3787 UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_cq,
3788 struct ib_uverbs_destroy_cq_resp),
3789 UAPI_DEF_METHOD_NEEDS_FN(destroy_cq)),
3790 DECLARE_UVERBS_WRITE(
3791 IB_USER_VERBS_CMD_POLL_CQ,
3792 ib_uverbs_poll_cq,
3793 UAPI_DEF_WRITE_IO(struct ib_uverbs_poll_cq,
3794 struct ib_uverbs_poll_cq_resp),
3795 UAPI_DEF_METHOD_NEEDS_FN(poll_cq)),
3796 DECLARE_UVERBS_WRITE(
3797 IB_USER_VERBS_CMD_REQ_NOTIFY_CQ,
3798 ib_uverbs_req_notify_cq,
3799 UAPI_DEF_WRITE_I(struct ib_uverbs_req_notify_cq),
3800 UAPI_DEF_METHOD_NEEDS_FN(req_notify_cq)),
3801 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_RESIZE_CQ,
3802 ib_uverbs_resize_cq,
3803 UAPI_DEF_WRITE_UDATA_IO(
3804 struct ib_uverbs_resize_cq,
3805 struct ib_uverbs_resize_cq_resp),
3806 UAPI_DEF_METHOD_NEEDS_FN(resize_cq)),
3807 DECLARE_UVERBS_WRITE_EX(
3808 IB_USER_VERBS_EX_CMD_CREATE_CQ,
3809 ib_uverbs_ex_create_cq,
3810 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_cq,
3811 reserved,
3812 struct ib_uverbs_ex_create_cq_resp,
3813 response_length),
3814 UAPI_DEF_METHOD_NEEDS_FN(create_cq)),
3815 DECLARE_UVERBS_WRITE_EX(
3816 IB_USER_VERBS_EX_CMD_MODIFY_CQ,
3817 ib_uverbs_ex_modify_cq,
3818 UAPI_DEF_WRITE_I(struct ib_uverbs_ex_modify_cq),
3819 UAPI_DEF_METHOD_NEEDS_FN(modify_cq))),
3820
3821 DECLARE_UVERBS_OBJECT(
3822 UVERBS_OBJECT_DEVICE,
3823 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_GET_CONTEXT,
3824 ib_uverbs_get_context,
3825 UAPI_DEF_WRITE_UDATA_IO(
3826 struct ib_uverbs_get_context,
3827 struct ib_uverbs_get_context_resp)),
3828 DECLARE_UVERBS_WRITE(
3829 IB_USER_VERBS_CMD_QUERY_DEVICE,
3830 ib_uverbs_query_device,
3831 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_device,
3832 struct ib_uverbs_query_device_resp)),
3833 DECLARE_UVERBS_WRITE(
3834 IB_USER_VERBS_CMD_QUERY_PORT,
3835 ib_uverbs_query_port,
3836 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_port,
3837 struct ib_uverbs_query_port_resp),
3838 UAPI_DEF_METHOD_NEEDS_FN(query_port)),
3839 DECLARE_UVERBS_WRITE_EX(
3840 IB_USER_VERBS_EX_CMD_QUERY_DEVICE,
3841 ib_uverbs_ex_query_device,
3842 UAPI_DEF_WRITE_IO_EX(
3843 struct ib_uverbs_ex_query_device,
3844 reserved,
3845 struct ib_uverbs_ex_query_device_resp,
3846 response_length),
3847 UAPI_DEF_METHOD_NEEDS_FN(query_device)),
3848 UAPI_DEF_OBJ_NEEDS_FN(alloc_ucontext),
3849 UAPI_DEF_OBJ_NEEDS_FN(dealloc_ucontext)),
3850
3851 DECLARE_UVERBS_OBJECT(
3852 UVERBS_OBJECT_FLOW,
3853 DECLARE_UVERBS_WRITE_EX(
3854 IB_USER_VERBS_EX_CMD_CREATE_FLOW,
3855 ib_uverbs_ex_create_flow,
3856 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_create_flow,
3857 flow_attr,
3858 struct ib_uverbs_create_flow_resp,
3859 flow_handle),
3860 UAPI_DEF_METHOD_NEEDS_FN(create_flow)),
3861 DECLARE_UVERBS_WRITE_EX(
3862 IB_USER_VERBS_EX_CMD_DESTROY_FLOW,
3863 ib_uverbs_ex_destroy_flow,
3864 UAPI_DEF_WRITE_I(struct ib_uverbs_destroy_flow),
3865 UAPI_DEF_METHOD_NEEDS_FN(destroy_flow))),
3866
3867 DECLARE_UVERBS_OBJECT(
3868 UVERBS_OBJECT_MR,
3869 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_DEREG_MR,
3870 ib_uverbs_dereg_mr,
3871 UAPI_DEF_WRITE_I(struct ib_uverbs_dereg_mr),
3872 UAPI_DEF_METHOD_NEEDS_FN(dereg_mr)),
3873 DECLARE_UVERBS_WRITE(
3874 IB_USER_VERBS_CMD_REG_MR,
3875 ib_uverbs_reg_mr,
3876 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_reg_mr,
3877 struct ib_uverbs_reg_mr_resp),
3878 UAPI_DEF_METHOD_NEEDS_FN(reg_user_mr)),
3879 DECLARE_UVERBS_WRITE(
3880 IB_USER_VERBS_CMD_REREG_MR,
3881 ib_uverbs_rereg_mr,
3882 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_rereg_mr,
3883 struct ib_uverbs_rereg_mr_resp),
3884 UAPI_DEF_METHOD_NEEDS_FN(rereg_user_mr))),
3885
3886 DECLARE_UVERBS_OBJECT(
3887 UVERBS_OBJECT_MW,
3888 DECLARE_UVERBS_WRITE(
3889 IB_USER_VERBS_CMD_ALLOC_MW,
3890 ib_uverbs_alloc_mw,
3891 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_mw,
3892 struct ib_uverbs_alloc_mw_resp),
3893 UAPI_DEF_METHOD_NEEDS_FN(alloc_mw)),
3894 DECLARE_UVERBS_WRITE(
3895 IB_USER_VERBS_CMD_DEALLOC_MW,
3896 ib_uverbs_dealloc_mw,
3897 UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_mw),
3898 UAPI_DEF_METHOD_NEEDS_FN(dealloc_mw))),
3899
3900 DECLARE_UVERBS_OBJECT(
3901 UVERBS_OBJECT_PD,
3902 DECLARE_UVERBS_WRITE(
3903 IB_USER_VERBS_CMD_ALLOC_PD,
3904 ib_uverbs_alloc_pd,
3905 UAPI_DEF_WRITE_UDATA_IO(struct ib_uverbs_alloc_pd,
3906 struct ib_uverbs_alloc_pd_resp),
3907 UAPI_DEF_METHOD_NEEDS_FN(alloc_pd)),
3908 DECLARE_UVERBS_WRITE(
3909 IB_USER_VERBS_CMD_DEALLOC_PD,
3910 ib_uverbs_dealloc_pd,
3911 UAPI_DEF_WRITE_I(struct ib_uverbs_dealloc_pd),
3912 UAPI_DEF_METHOD_NEEDS_FN(dealloc_pd))),
3913
3914 DECLARE_UVERBS_OBJECT(
3915 UVERBS_OBJECT_QP,
3916 DECLARE_UVERBS_WRITE(
3917 IB_USER_VERBS_CMD_ATTACH_MCAST,
3918 ib_uverbs_attach_mcast,
3919 UAPI_DEF_WRITE_I(struct ib_uverbs_attach_mcast),
3920 UAPI_DEF_METHOD_NEEDS_FN(attach_mcast),
3921 UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
3922 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_QP,
3923 ib_uverbs_create_qp,
3924 UAPI_DEF_WRITE_UDATA_IO(
3925 struct ib_uverbs_create_qp,
3926 struct ib_uverbs_create_qp_resp),
3927 UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
3928 DECLARE_UVERBS_WRITE(
3929 IB_USER_VERBS_CMD_DESTROY_QP,
3930 ib_uverbs_destroy_qp,
3931 UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_qp,
3932 struct ib_uverbs_destroy_qp_resp),
3933 UAPI_DEF_METHOD_NEEDS_FN(destroy_qp)),
3934 DECLARE_UVERBS_WRITE(
3935 IB_USER_VERBS_CMD_DETACH_MCAST,
3936 ib_uverbs_detach_mcast,
3937 UAPI_DEF_WRITE_I(struct ib_uverbs_detach_mcast),
3938 UAPI_DEF_METHOD_NEEDS_FN(detach_mcast)),
3939 DECLARE_UVERBS_WRITE(
3940 IB_USER_VERBS_CMD_MODIFY_QP,
3941 ib_uverbs_modify_qp,
3942 UAPI_DEF_WRITE_I(struct ib_uverbs_modify_qp),
3943 UAPI_DEF_METHOD_NEEDS_FN(modify_qp)),
3944 DECLARE_UVERBS_WRITE(
3945 IB_USER_VERBS_CMD_POST_RECV,
3946 ib_uverbs_post_recv,
3947 UAPI_DEF_WRITE_IO(struct ib_uverbs_post_recv,
3948 struct ib_uverbs_post_recv_resp),
3949 UAPI_DEF_METHOD_NEEDS_FN(post_recv)),
3950 DECLARE_UVERBS_WRITE(
3951 IB_USER_VERBS_CMD_POST_SEND,
3952 ib_uverbs_post_send,
3953 UAPI_DEF_WRITE_IO(struct ib_uverbs_post_send,
3954 struct ib_uverbs_post_send_resp),
3955 UAPI_DEF_METHOD_NEEDS_FN(post_send)),
3956 DECLARE_UVERBS_WRITE(
3957 IB_USER_VERBS_CMD_QUERY_QP,
3958 ib_uverbs_query_qp,
3959 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_qp,
3960 struct ib_uverbs_query_qp_resp),
3961 UAPI_DEF_METHOD_NEEDS_FN(query_qp)),
3962 DECLARE_UVERBS_WRITE_EX(
3963 IB_USER_VERBS_EX_CMD_CREATE_QP,
3964 ib_uverbs_ex_create_qp,
3965 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_qp,
3966 comp_mask,
3967 struct ib_uverbs_ex_create_qp_resp,
3968 response_length),
3969 UAPI_DEF_METHOD_NEEDS_FN(create_qp)),
3970 DECLARE_UVERBS_WRITE_EX(
3971 IB_USER_VERBS_EX_CMD_MODIFY_QP,
3972 ib_uverbs_ex_modify_qp,
3973 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_modify_qp,
3974 base,
3975 struct ib_uverbs_ex_modify_qp_resp,
3976 response_length),
3977 UAPI_DEF_METHOD_NEEDS_FN(modify_qp))),
3978
3979 DECLARE_UVERBS_OBJECT(
3980 UVERBS_OBJECT_RWQ_IND_TBL,
3981 DECLARE_UVERBS_WRITE_EX(
3982 IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL,
3983 ib_uverbs_ex_create_rwq_ind_table,
3984 UAPI_DEF_WRITE_IO_EX(
3985 struct ib_uverbs_ex_create_rwq_ind_table,
3986 log_ind_tbl_size,
3987 struct ib_uverbs_ex_create_rwq_ind_table_resp,
3988 ind_tbl_num),
3989 UAPI_DEF_METHOD_NEEDS_FN(create_rwq_ind_table)),
3990 DECLARE_UVERBS_WRITE_EX(
3991 IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL,
3992 ib_uverbs_ex_destroy_rwq_ind_table,
3993 UAPI_DEF_WRITE_I(
3994 struct ib_uverbs_ex_destroy_rwq_ind_table),
3995 UAPI_DEF_METHOD_NEEDS_FN(destroy_rwq_ind_table))),
3996
3997 DECLARE_UVERBS_OBJECT(
3998 UVERBS_OBJECT_WQ,
3999 DECLARE_UVERBS_WRITE_EX(
4000 IB_USER_VERBS_EX_CMD_CREATE_WQ,
4001 ib_uverbs_ex_create_wq,
4002 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_create_wq,
4003 max_sge,
4004 struct ib_uverbs_ex_create_wq_resp,
4005 wqn),
4006 UAPI_DEF_METHOD_NEEDS_FN(create_wq)),
4007 DECLARE_UVERBS_WRITE_EX(
4008 IB_USER_VERBS_EX_CMD_DESTROY_WQ,
4009 ib_uverbs_ex_destroy_wq,
4010 UAPI_DEF_WRITE_IO_EX(struct ib_uverbs_ex_destroy_wq,
4011 wq_handle,
4012 struct ib_uverbs_ex_destroy_wq_resp,
4013 reserved),
4014 UAPI_DEF_METHOD_NEEDS_FN(destroy_wq)),
4015 DECLARE_UVERBS_WRITE_EX(
4016 IB_USER_VERBS_EX_CMD_MODIFY_WQ,
4017 ib_uverbs_ex_modify_wq,
4018 UAPI_DEF_WRITE_I_EX(struct ib_uverbs_ex_modify_wq,
4019 curr_wq_state),
4020 UAPI_DEF_METHOD_NEEDS_FN(modify_wq))),
4021
4022 DECLARE_UVERBS_OBJECT(
4023 UVERBS_OBJECT_SRQ,
4024 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_SRQ,
4025 ib_uverbs_create_srq,
4026 UAPI_DEF_WRITE_UDATA_IO(
4027 struct ib_uverbs_create_srq,
4028 struct ib_uverbs_create_srq_resp),
4029 UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4030 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_CREATE_XSRQ,
4031 ib_uverbs_create_xsrq,
4032 UAPI_DEF_WRITE_UDATA_IO(
4033 struct ib_uverbs_create_xsrq,
4034 struct ib_uverbs_create_srq_resp),
4035 UAPI_DEF_METHOD_NEEDS_FN(create_srq)),
4036 DECLARE_UVERBS_WRITE(
4037 IB_USER_VERBS_CMD_DESTROY_SRQ,
4038 ib_uverbs_destroy_srq,
4039 UAPI_DEF_WRITE_IO(struct ib_uverbs_destroy_srq,
4040 struct ib_uverbs_destroy_srq_resp),
4041 UAPI_DEF_METHOD_NEEDS_FN(destroy_srq)),
4042 DECLARE_UVERBS_WRITE(
4043 IB_USER_VERBS_CMD_MODIFY_SRQ,
4044 ib_uverbs_modify_srq,
4045 UAPI_DEF_WRITE_UDATA_I(struct ib_uverbs_modify_srq),
4046 UAPI_DEF_METHOD_NEEDS_FN(modify_srq)),
4047 DECLARE_UVERBS_WRITE(
4048 IB_USER_VERBS_CMD_POST_SRQ_RECV,
4049 ib_uverbs_post_srq_recv,
4050 UAPI_DEF_WRITE_IO(struct ib_uverbs_post_srq_recv,
4051 struct ib_uverbs_post_srq_recv_resp),
4052 UAPI_DEF_METHOD_NEEDS_FN(post_srq_recv)),
4053 DECLARE_UVERBS_WRITE(
4054 IB_USER_VERBS_CMD_QUERY_SRQ,
4055 ib_uverbs_query_srq,
4056 UAPI_DEF_WRITE_IO(struct ib_uverbs_query_srq,
4057 struct ib_uverbs_query_srq_resp),
4058 UAPI_DEF_METHOD_NEEDS_FN(query_srq))),
4059
4060 DECLARE_UVERBS_OBJECT(
4061 UVERBS_OBJECT_XRCD,
4062 DECLARE_UVERBS_WRITE(
4063 IB_USER_VERBS_CMD_CLOSE_XRCD,
4064 ib_uverbs_close_xrcd,
4065 UAPI_DEF_WRITE_I(struct ib_uverbs_close_xrcd)),
4066 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_QP,
4067 ib_uverbs_open_qp,
4068 UAPI_DEF_WRITE_UDATA_IO(
4069 struct ib_uverbs_open_qp,
4070 struct ib_uverbs_create_qp_resp)),
4071 DECLARE_UVERBS_WRITE(IB_USER_VERBS_CMD_OPEN_XRCD,
4072 ib_uverbs_open_xrcd,
4073 UAPI_DEF_WRITE_UDATA_IO(
4074 struct ib_uverbs_open_xrcd,
4075 struct ib_uverbs_open_xrcd_resp)),
4076 UAPI_DEF_OBJ_NEEDS_FN(alloc_xrcd),
4077 UAPI_DEF_OBJ_NEEDS_FN(dealloc_xrcd)),
4078
4079 {},
4080 };
4081