Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / infiniband / sw / siw / siw_verbs.c
1 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
2
3 /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
4 /* Copyright (c) 2008-2019, IBM Corporation */
5
6 #include <linux/errno.h>
7 #include <linux/types.h>
8 #include <linux/uaccess.h>
9 #include <linux/vmalloc.h>
10 #include <linux/xarray.h>
11
12 #include <rdma/iw_cm.h>
13 #include <rdma/ib_verbs.h>
14 #include <rdma/ib_user_verbs.h>
15 #include <rdma/uverbs_ioctl.h>
16
17 #include "siw.h"
18 #include "siw_verbs.h"
19 #include "siw_mem.h"
20
21 static int ib_qp_state_to_siw_qp_state[IB_QPS_ERR + 1] = {
22         [IB_QPS_RESET] = SIW_QP_STATE_IDLE,
23         [IB_QPS_INIT] = SIW_QP_STATE_IDLE,
24         [IB_QPS_RTR] = SIW_QP_STATE_RTR,
25         [IB_QPS_RTS] = SIW_QP_STATE_RTS,
26         [IB_QPS_SQD] = SIW_QP_STATE_CLOSING,
27         [IB_QPS_SQE] = SIW_QP_STATE_TERMINATE,
28         [IB_QPS_ERR] = SIW_QP_STATE_ERROR
29 };
30
31 static char ib_qp_state_to_string[IB_QPS_ERR + 1][sizeof("RESET")] = {
32         [IB_QPS_RESET] = "RESET", [IB_QPS_INIT] = "INIT", [IB_QPS_RTR] = "RTR",
33         [IB_QPS_RTS] = "RTS",     [IB_QPS_SQD] = "SQD",   [IB_QPS_SQE] = "SQE",
34         [IB_QPS_ERR] = "ERR"
35 };
36
37 static u32 siw_create_uobj(struct siw_ucontext *uctx, void *vaddr, u32 size)
38 {
39         struct siw_uobj *uobj;
40         struct xa_limit limit = XA_LIMIT(0, SIW_UOBJ_MAX_KEY);
41         u32 key;
42
43         uobj = kzalloc(sizeof(*uobj), GFP_KERNEL);
44         if (!uobj)
45                 return SIW_INVAL_UOBJ_KEY;
46
47         if (xa_alloc_cyclic(&uctx->xa, &key, uobj, limit, &uctx->uobj_nextkey,
48                             GFP_KERNEL) < 0) {
49                 kfree(uobj);
50                 return SIW_INVAL_UOBJ_KEY;
51         }
52         uobj->size = PAGE_ALIGN(size);
53         uobj->addr = vaddr;
54
55         return key;
56 }
57
58 static struct siw_uobj *siw_get_uobj(struct siw_ucontext *uctx,
59                                      unsigned long off, u32 size)
60 {
61         struct siw_uobj *uobj = xa_load(&uctx->xa, off);
62
63         if (uobj && uobj->size == size)
64                 return uobj;
65
66         return NULL;
67 }
68
69 int siw_mmap(struct ib_ucontext *ctx, struct vm_area_struct *vma)
70 {
71         struct siw_ucontext *uctx = to_siw_ctx(ctx);
72         struct siw_uobj *uobj;
73         unsigned long off = vma->vm_pgoff;
74         int size = vma->vm_end - vma->vm_start;
75         int rv = -EINVAL;
76
77         /*
78          * Must be page aligned
79          */
80         if (vma->vm_start & (PAGE_SIZE - 1)) {
81                 pr_warn("siw: mmap not page aligned\n");
82                 goto out;
83         }
84         uobj = siw_get_uobj(uctx, off, size);
85         if (!uobj) {
86                 siw_dbg(&uctx->sdev->base_dev, "mmap lookup failed: %lu, %u\n",
87                         off, size);
88                 goto out;
89         }
90         rv = remap_vmalloc_range(vma, uobj->addr, 0);
91         if (rv)
92                 pr_warn("remap_vmalloc_range failed: %lu, %u\n", off, size);
93 out:
94         return rv;
95 }
96
97 int siw_alloc_ucontext(struct ib_ucontext *base_ctx, struct ib_udata *udata)
98 {
99         struct siw_device *sdev = to_siw_dev(base_ctx->device);
100         struct siw_ucontext *ctx = to_siw_ctx(base_ctx);
101         struct siw_uresp_alloc_ctx uresp = {};
102         int rv;
103
104         if (atomic_inc_return(&sdev->num_ctx) > SIW_MAX_CONTEXT) {
105                 rv = -ENOMEM;
106                 goto err_out;
107         }
108         xa_init_flags(&ctx->xa, XA_FLAGS_ALLOC);
109         ctx->uobj_nextkey = 0;
110         ctx->sdev = sdev;
111
112         uresp.dev_id = sdev->vendor_part_id;
113
114         if (udata->outlen < sizeof(uresp)) {
115                 rv = -EINVAL;
116                 goto err_out;
117         }
118         rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
119         if (rv)
120                 goto err_out;
121
122         siw_dbg(base_ctx->device, "success. now %d context(s)\n",
123                 atomic_read(&sdev->num_ctx));
124
125         return 0;
126
127 err_out:
128         atomic_dec(&sdev->num_ctx);
129         siw_dbg(base_ctx->device, "failure %d. now %d context(s)\n", rv,
130                 atomic_read(&sdev->num_ctx));
131
132         return rv;
133 }
134
135 void siw_dealloc_ucontext(struct ib_ucontext *base_ctx)
136 {
137         struct siw_ucontext *uctx = to_siw_ctx(base_ctx);
138         void *entry;
139         unsigned long index;
140
141         /*
142          * Make sure all user mmap objects are gone. Since QP, CQ
143          * and SRQ destroy routines destroy related objects, nothing
144          * should be found here.
145          */
146         xa_for_each(&uctx->xa, index, entry) {
147                 kfree(xa_erase(&uctx->xa, index));
148                 pr_warn("siw: dropping orphaned uobj at %lu\n", index);
149         }
150         xa_destroy(&uctx->xa);
151         atomic_dec(&uctx->sdev->num_ctx);
152 }
153
154 int siw_query_device(struct ib_device *base_dev, struct ib_device_attr *attr,
155                      struct ib_udata *udata)
156 {
157         struct siw_device *sdev = to_siw_dev(base_dev);
158
159         if (udata->inlen || udata->outlen)
160                 return -EINVAL;
161
162         memset(attr, 0, sizeof(*attr));
163
164         /* Revisit atomic caps if RFC 7306 gets supported */
165         attr->atomic_cap = 0;
166         attr->device_cap_flags =
167                 IB_DEVICE_MEM_MGT_EXTENSIONS | IB_DEVICE_ALLOW_USER_UNREG;
168         attr->max_cq = sdev->attrs.max_cq;
169         attr->max_cqe = sdev->attrs.max_cqe;
170         attr->max_fast_reg_page_list_len = SIW_MAX_SGE_PBL;
171         attr->max_fmr = sdev->attrs.max_fmr;
172         attr->max_mr = sdev->attrs.max_mr;
173         attr->max_mw = sdev->attrs.max_mw;
174         attr->max_mr_size = ~0ull;
175         attr->max_pd = sdev->attrs.max_pd;
176         attr->max_qp = sdev->attrs.max_qp;
177         attr->max_qp_init_rd_atom = sdev->attrs.max_ird;
178         attr->max_qp_rd_atom = sdev->attrs.max_ord;
179         attr->max_qp_wr = sdev->attrs.max_qp_wr;
180         attr->max_recv_sge = sdev->attrs.max_sge;
181         attr->max_res_rd_atom = sdev->attrs.max_qp * sdev->attrs.max_ird;
182         attr->max_send_sge = sdev->attrs.max_sge;
183         attr->max_sge_rd = sdev->attrs.max_sge_rd;
184         attr->max_srq = sdev->attrs.max_srq;
185         attr->max_srq_sge = sdev->attrs.max_srq_sge;
186         attr->max_srq_wr = sdev->attrs.max_srq_wr;
187         attr->page_size_cap = PAGE_SIZE;
188         attr->vendor_id = SIW_VENDOR_ID;
189         attr->vendor_part_id = sdev->vendor_part_id;
190
191         memcpy(&attr->sys_image_guid, sdev->netdev->dev_addr, 6);
192
193         return 0;
194 }
195
196 int siw_query_port(struct ib_device *base_dev, u8 port,
197                    struct ib_port_attr *attr)
198 {
199         struct siw_device *sdev = to_siw_dev(base_dev);
200
201         memset(attr, 0, sizeof(*attr));
202
203         attr->active_mtu = attr->max_mtu;
204         attr->active_speed = 2;
205         attr->active_width = 2;
206         attr->gid_tbl_len = 1;
207         attr->max_msg_sz = -1;
208         attr->max_mtu = ib_mtu_int_to_enum(sdev->netdev->mtu);
209         attr->phys_state = sdev->state == IB_PORT_ACTIVE ? 5 : 3;
210         attr->pkey_tbl_len = 1;
211         attr->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_DEVICE_MGMT_SUP;
212         attr->state = sdev->state;
213         /*
214          * All zero
215          *
216          * attr->lid = 0;
217          * attr->bad_pkey_cntr = 0;
218          * attr->qkey_viol_cntr = 0;
219          * attr->sm_lid = 0;
220          * attr->lmc = 0;
221          * attr->max_vl_num = 0;
222          * attr->sm_sl = 0;
223          * attr->subnet_timeout = 0;
224          * attr->init_type_repy = 0;
225          */
226         return 0;
227 }
228
229 int siw_get_port_immutable(struct ib_device *base_dev, u8 port,
230                            struct ib_port_immutable *port_immutable)
231 {
232         struct ib_port_attr attr;
233         int rv = siw_query_port(base_dev, port, &attr);
234
235         if (rv)
236                 return rv;
237
238         port_immutable->pkey_tbl_len = attr.pkey_tbl_len;
239         port_immutable->gid_tbl_len = attr.gid_tbl_len;
240         port_immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
241
242         return 0;
243 }
244
245 int siw_query_pkey(struct ib_device *base_dev, u8 port, u16 idx, u16 *pkey)
246 {
247         /* Report the default pkey */
248         *pkey = 0xffff;
249         return 0;
250 }
251
252 int siw_query_gid(struct ib_device *base_dev, u8 port, int idx,
253                   union ib_gid *gid)
254 {
255         struct siw_device *sdev = to_siw_dev(base_dev);
256
257         /* subnet_prefix == interface_id == 0; */
258         memset(gid, 0, sizeof(*gid));
259         memcpy(&gid->raw[0], sdev->netdev->dev_addr, 6);
260
261         return 0;
262 }
263
264 int siw_alloc_pd(struct ib_pd *pd, struct ib_udata *udata)
265 {
266         struct siw_device *sdev = to_siw_dev(pd->device);
267
268         if (atomic_inc_return(&sdev->num_pd) > SIW_MAX_PD) {
269                 atomic_dec(&sdev->num_pd);
270                 return -ENOMEM;
271         }
272         siw_dbg_pd(pd, "now %d PD's(s)\n", atomic_read(&sdev->num_pd));
273
274         return 0;
275 }
276
277 void siw_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
278 {
279         struct siw_device *sdev = to_siw_dev(pd->device);
280
281         siw_dbg_pd(pd, "free PD\n");
282         atomic_dec(&sdev->num_pd);
283 }
284
285 void siw_qp_get_ref(struct ib_qp *base_qp)
286 {
287         siw_qp_get(to_siw_qp(base_qp));
288 }
289
290 void siw_qp_put_ref(struct ib_qp *base_qp)
291 {
292         siw_qp_put(to_siw_qp(base_qp));
293 }
294
295 /*
296  * siw_create_qp()
297  *
298  * Create QP of requested size on given device.
299  *
300  * @pd:         Protection Domain
301  * @attrs:      Initial QP attributes.
302  * @udata:      used to provide QP ID, SQ and RQ size back to user.
303  */
304
305 struct ib_qp *siw_create_qp(struct ib_pd *pd,
306                             struct ib_qp_init_attr *attrs,
307                             struct ib_udata *udata)
308 {
309         struct siw_qp *qp = NULL;
310         struct siw_base_qp *siw_base_qp = NULL;
311         struct ib_device *base_dev = pd->device;
312         struct siw_device *sdev = to_siw_dev(base_dev);
313         struct siw_ucontext *uctx =
314                 rdma_udata_to_drv_context(udata, struct siw_ucontext,
315                                           base_ucontext);
316         struct siw_cq *scq = NULL, *rcq = NULL;
317         unsigned long flags;
318         int num_sqe, num_rqe, rv = 0;
319
320         siw_dbg(base_dev, "create new QP\n");
321
322         if (atomic_inc_return(&sdev->num_qp) > SIW_MAX_QP) {
323                 siw_dbg(base_dev, "too many QP's\n");
324                 rv = -ENOMEM;
325                 goto err_out;
326         }
327         if (attrs->qp_type != IB_QPT_RC) {
328                 siw_dbg(base_dev, "only RC QP's supported\n");
329                 rv = -EINVAL;
330                 goto err_out;
331         }
332         if ((attrs->cap.max_send_wr > SIW_MAX_QP_WR) ||
333             (attrs->cap.max_recv_wr > SIW_MAX_QP_WR) ||
334             (attrs->cap.max_send_sge > SIW_MAX_SGE) ||
335             (attrs->cap.max_recv_sge > SIW_MAX_SGE)) {
336                 siw_dbg(base_dev, "QP size error\n");
337                 rv = -EINVAL;
338                 goto err_out;
339         }
340         if (attrs->cap.max_inline_data > SIW_MAX_INLINE) {
341                 siw_dbg(base_dev, "max inline send: %d > %d\n",
342                         attrs->cap.max_inline_data, (int)SIW_MAX_INLINE);
343                 rv = -EINVAL;
344                 goto err_out;
345         }
346         /*
347          * NOTE: we allow for zero element SQ and RQ WQE's SGL's
348          * but not for a QP unable to hold any WQE (SQ + RQ)
349          */
350         if (attrs->cap.max_send_wr + attrs->cap.max_recv_wr == 0) {
351                 siw_dbg(base_dev, "QP must have send or receive queue\n");
352                 rv = -EINVAL;
353                 goto err_out;
354         }
355         scq = to_siw_cq(attrs->send_cq);
356         rcq = to_siw_cq(attrs->recv_cq);
357
358         if (!scq || (!rcq && !attrs->srq)) {
359                 siw_dbg(base_dev, "send CQ or receive CQ invalid\n");
360                 rv = -EINVAL;
361                 goto err_out;
362         }
363         siw_base_qp = kzalloc(sizeof(*siw_base_qp), GFP_KERNEL);
364         if (!siw_base_qp) {
365                 rv = -ENOMEM;
366                 goto err_out;
367         }
368         qp = kzalloc(sizeof(*qp), GFP_KERNEL);
369         if (!qp) {
370                 rv = -ENOMEM;
371                 goto err_out;
372         }
373         siw_base_qp->qp = qp;
374         qp->ib_qp = &siw_base_qp->base_qp;
375
376         init_rwsem(&qp->state_lock);
377         spin_lock_init(&qp->sq_lock);
378         spin_lock_init(&qp->rq_lock);
379         spin_lock_init(&qp->orq_lock);
380
381         qp->kernel_verbs = !udata;
382         qp->xa_sq_index = SIW_INVAL_UOBJ_KEY;
383         qp->xa_rq_index = SIW_INVAL_UOBJ_KEY;
384
385         rv = siw_qp_add(sdev, qp);
386         if (rv)
387                 goto err_out;
388
389         /* All queue indices are derived from modulo operations
390          * on a free running 'get' (consumer) and 'put' (producer)
391          * unsigned counter. Having queue sizes at power of two
392          * avoids handling counter wrap around.
393          */
394         num_sqe = roundup_pow_of_two(attrs->cap.max_send_wr);
395         num_rqe = roundup_pow_of_two(attrs->cap.max_recv_wr);
396
397         if (qp->kernel_verbs)
398                 qp->sendq = vzalloc(num_sqe * sizeof(struct siw_sqe));
399         else
400                 qp->sendq = vmalloc_user(num_sqe * sizeof(struct siw_sqe));
401
402         if (qp->sendq == NULL) {
403                 siw_dbg(base_dev, "SQ size %d alloc failed\n", num_sqe);
404                 rv = -ENOMEM;
405                 goto err_out_xa;
406         }
407         if (attrs->sq_sig_type != IB_SIGNAL_REQ_WR) {
408                 if (attrs->sq_sig_type == IB_SIGNAL_ALL_WR)
409                         qp->attrs.flags |= SIW_SIGNAL_ALL_WR;
410                 else {
411                         rv = -EINVAL;
412                         goto err_out_xa;
413                 }
414         }
415         qp->pd = pd;
416         qp->scq = scq;
417         qp->rcq = rcq;
418
419         if (attrs->srq) {
420                 /*
421                  * SRQ support.
422                  * Verbs 6.3.7: ignore RQ size, if SRQ present
423                  * Verbs 6.3.5: do not check PD of SRQ against PD of QP
424                  */
425                 qp->srq = to_siw_srq(attrs->srq);
426                 qp->attrs.rq_size = 0;
427                 siw_dbg(base_dev, "QP [%u]: SRQ attached\n", qp->qp_num);
428         } else if (num_rqe) {
429                 if (qp->kernel_verbs)
430                         qp->recvq = vzalloc(num_rqe * sizeof(struct siw_rqe));
431                 else
432                         qp->recvq =
433                                 vmalloc_user(num_rqe * sizeof(struct siw_rqe));
434
435                 if (qp->recvq == NULL) {
436                         siw_dbg(base_dev, "RQ size %d alloc failed\n", num_rqe);
437                         rv = -ENOMEM;
438                         goto err_out_xa;
439                 }
440                 qp->attrs.rq_size = num_rqe;
441         }
442         qp->attrs.sq_size = num_sqe;
443         qp->attrs.sq_max_sges = attrs->cap.max_send_sge;
444         qp->attrs.rq_max_sges = attrs->cap.max_recv_sge;
445
446         /* Make those two tunables fixed for now. */
447         qp->tx_ctx.gso_seg_limit = 1;
448         qp->tx_ctx.zcopy_tx = zcopy_tx;
449
450         qp->attrs.state = SIW_QP_STATE_IDLE;
451
452         if (udata) {
453                 struct siw_uresp_create_qp uresp = {};
454
455                 uresp.num_sqe = num_sqe;
456                 uresp.num_rqe = num_rqe;
457                 uresp.qp_id = qp_id(qp);
458
459                 if (qp->sendq) {
460                         qp->xa_sq_index =
461                                 siw_create_uobj(uctx, qp->sendq,
462                                         num_sqe * sizeof(struct siw_sqe));
463                 }
464                 if (qp->recvq) {
465                         qp->xa_rq_index =
466                                  siw_create_uobj(uctx, qp->recvq,
467                                         num_rqe * sizeof(struct siw_rqe));
468                 }
469                 if (qp->xa_sq_index == SIW_INVAL_UOBJ_KEY ||
470                     qp->xa_rq_index == SIW_INVAL_UOBJ_KEY) {
471                         rv = -ENOMEM;
472                         goto err_out_xa;
473                 }
474                 uresp.sq_key = qp->xa_sq_index << PAGE_SHIFT;
475                 uresp.rq_key = qp->xa_rq_index << PAGE_SHIFT;
476
477                 if (udata->outlen < sizeof(uresp)) {
478                         rv = -EINVAL;
479                         goto err_out_xa;
480                 }
481                 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
482                 if (rv)
483                         goto err_out_xa;
484         }
485         qp->tx_cpu = siw_get_tx_cpu(sdev);
486         if (qp->tx_cpu < 0) {
487                 rv = -EINVAL;
488                 goto err_out_xa;
489         }
490         INIT_LIST_HEAD(&qp->devq);
491         spin_lock_irqsave(&sdev->lock, flags);
492         list_add_tail(&qp->devq, &sdev->qp_list);
493         spin_unlock_irqrestore(&sdev->lock, flags);
494
495         return qp->ib_qp;
496
497 err_out_xa:
498         xa_erase(&sdev->qp_xa, qp_id(qp));
499 err_out:
500         kfree(siw_base_qp);
501
502         if (qp) {
503                 if (qp->xa_sq_index != SIW_INVAL_UOBJ_KEY)
504                         kfree(xa_erase(&uctx->xa, qp->xa_sq_index));
505                 if (qp->xa_rq_index != SIW_INVAL_UOBJ_KEY)
506                         kfree(xa_erase(&uctx->xa, qp->xa_rq_index));
507
508                 vfree(qp->sendq);
509                 vfree(qp->recvq);
510                 kfree(qp);
511         }
512         atomic_dec(&sdev->num_qp);
513
514         return ERR_PTR(rv);
515 }
516
517 /*
518  * Minimum siw_query_qp() verb interface.
519  *
520  * @qp_attr_mask is not used but all available information is provided
521  */
522 int siw_query_qp(struct ib_qp *base_qp, struct ib_qp_attr *qp_attr,
523                  int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
524 {
525         struct siw_qp *qp;
526         struct siw_device *sdev;
527
528         if (base_qp && qp_attr && qp_init_attr) {
529                 qp = to_siw_qp(base_qp);
530                 sdev = to_siw_dev(base_qp->device);
531         } else {
532                 return -EINVAL;
533         }
534         qp_attr->cap.max_inline_data = SIW_MAX_INLINE;
535         qp_attr->cap.max_send_wr = qp->attrs.sq_size;
536         qp_attr->cap.max_send_sge = qp->attrs.sq_max_sges;
537         qp_attr->cap.max_recv_wr = qp->attrs.rq_size;
538         qp_attr->cap.max_recv_sge = qp->attrs.rq_max_sges;
539         qp_attr->path_mtu = ib_mtu_int_to_enum(sdev->netdev->mtu);
540         qp_attr->max_rd_atomic = qp->attrs.irq_size;
541         qp_attr->max_dest_rd_atomic = qp->attrs.orq_size;
542
543         qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
544                                    IB_ACCESS_REMOTE_WRITE |
545                                    IB_ACCESS_REMOTE_READ;
546
547         qp_init_attr->qp_type = base_qp->qp_type;
548         qp_init_attr->send_cq = base_qp->send_cq;
549         qp_init_attr->recv_cq = base_qp->recv_cq;
550         qp_init_attr->srq = base_qp->srq;
551
552         qp_init_attr->cap = qp_attr->cap;
553
554         return 0;
555 }
556
557 int siw_verbs_modify_qp(struct ib_qp *base_qp, struct ib_qp_attr *attr,
558                         int attr_mask, struct ib_udata *udata)
559 {
560         struct siw_qp_attrs new_attrs;
561         enum siw_qp_attr_mask siw_attr_mask = 0;
562         struct siw_qp *qp = to_siw_qp(base_qp);
563         int rv = 0;
564
565         if (!attr_mask)
566                 return 0;
567
568         memset(&new_attrs, 0, sizeof(new_attrs));
569
570         if (attr_mask & IB_QP_ACCESS_FLAGS) {
571                 siw_attr_mask = SIW_QP_ATTR_ACCESS_FLAGS;
572
573                 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
574                         new_attrs.flags |= SIW_RDMA_READ_ENABLED;
575                 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
576                         new_attrs.flags |= SIW_RDMA_WRITE_ENABLED;
577                 if (attr->qp_access_flags & IB_ACCESS_MW_BIND)
578                         new_attrs.flags |= SIW_RDMA_BIND_ENABLED;
579         }
580         if (attr_mask & IB_QP_STATE) {
581                 siw_dbg_qp(qp, "desired IB QP state: %s\n",
582                            ib_qp_state_to_string[attr->qp_state]);
583
584                 new_attrs.state = ib_qp_state_to_siw_qp_state[attr->qp_state];
585
586                 if (new_attrs.state > SIW_QP_STATE_RTS)
587                         qp->tx_ctx.tx_suspend = 1;
588
589                 siw_attr_mask |= SIW_QP_ATTR_STATE;
590         }
591         if (!siw_attr_mask)
592                 goto out;
593
594         down_write(&qp->state_lock);
595
596         rv = siw_qp_modify(qp, &new_attrs, siw_attr_mask);
597
598         up_write(&qp->state_lock);
599 out:
600         return rv;
601 }
602
603 int siw_destroy_qp(struct ib_qp *base_qp, struct ib_udata *udata)
604 {
605         struct siw_qp *qp = to_siw_qp(base_qp);
606         struct siw_ucontext *uctx =
607                 rdma_udata_to_drv_context(udata, struct siw_ucontext,
608                                           base_ucontext);
609         struct siw_qp_attrs qp_attrs;
610
611         siw_dbg_qp(qp, "state %d\n", qp->attrs.state);
612
613         /*
614          * Mark QP as in process of destruction to prevent from
615          * any async callbacks to RDMA core
616          */
617         qp->attrs.flags |= SIW_QP_IN_DESTROY;
618         qp->rx_stream.rx_suspend = 1;
619
620         if (uctx && qp->xa_sq_index != SIW_INVAL_UOBJ_KEY)
621                 kfree(xa_erase(&uctx->xa, qp->xa_sq_index));
622         if (uctx && qp->xa_rq_index != SIW_INVAL_UOBJ_KEY)
623                 kfree(xa_erase(&uctx->xa, qp->xa_rq_index));
624
625         down_write(&qp->state_lock);
626
627         qp_attrs.state = SIW_QP_STATE_ERROR;
628         siw_qp_modify(qp, &qp_attrs, SIW_QP_ATTR_STATE);
629
630         if (qp->cep) {
631                 siw_cep_put(qp->cep);
632                 qp->cep = NULL;
633         }
634         up_write(&qp->state_lock);
635
636         kfree(qp->tx_ctx.mpa_crc_hd);
637         kfree(qp->rx_stream.mpa_crc_hd);
638
639         qp->scq = qp->rcq = NULL;
640
641         siw_qp_put(qp);
642
643         return 0;
644 }
645
646 /*
647  * siw_copy_inline_sgl()
648  *
649  * Prepare sgl of inlined data for sending. For userland callers
650  * function checks if given buffer addresses and len's are within
651  * process context bounds.
652  * Data from all provided sge's are copied together into the wqe,
653  * referenced by a single sge.
654  */
655 static int siw_copy_inline_sgl(const struct ib_send_wr *core_wr,
656                                struct siw_sqe *sqe)
657 {
658         struct ib_sge *core_sge = core_wr->sg_list;
659         void *kbuf = &sqe->sge[1];
660         int num_sge = core_wr->num_sge, bytes = 0;
661
662         sqe->sge[0].laddr = (uintptr_t)kbuf;
663         sqe->sge[0].lkey = 0;
664
665         while (num_sge--) {
666                 if (!core_sge->length) {
667                         core_sge++;
668                         continue;
669                 }
670                 bytes += core_sge->length;
671                 if (bytes > SIW_MAX_INLINE) {
672                         bytes = -EINVAL;
673                         break;
674                 }
675                 memcpy(kbuf, (void *)(uintptr_t)core_sge->addr,
676                        core_sge->length);
677
678                 kbuf += core_sge->length;
679                 core_sge++;
680         }
681         sqe->sge[0].length = bytes > 0 ? bytes : 0;
682         sqe->num_sge = bytes > 0 ? 1 : 0;
683
684         return bytes;
685 }
686
687 /*
688  * siw_post_send()
689  *
690  * Post a list of S-WR's to a SQ.
691  *
692  * @base_qp:    Base QP contained in siw QP
693  * @wr:         Null terminated list of user WR's
694  * @bad_wr:     Points to failing WR in case of synchronous failure.
695  */
696 int siw_post_send(struct ib_qp *base_qp, const struct ib_send_wr *wr,
697                   const struct ib_send_wr **bad_wr)
698 {
699         struct siw_qp *qp = to_siw_qp(base_qp);
700         struct siw_wqe *wqe = tx_wqe(qp);
701
702         unsigned long flags;
703         int rv = 0;
704
705         /*
706          * Try to acquire QP state lock. Must be non-blocking
707          * to accommodate kernel clients needs.
708          */
709         if (!down_read_trylock(&qp->state_lock)) {
710                 *bad_wr = wr;
711                 siw_dbg_qp(qp, "QP locked, state %d\n", qp->attrs.state);
712                 return -ENOTCONN;
713         }
714         if (unlikely(qp->attrs.state != SIW_QP_STATE_RTS)) {
715                 up_read(&qp->state_lock);
716                 *bad_wr = wr;
717                 siw_dbg_qp(qp, "QP out of state %d\n", qp->attrs.state);
718                 return -ENOTCONN;
719         }
720         if (wr && !qp->kernel_verbs) {
721                 siw_dbg_qp(qp, "wr must be empty for user mapped sq\n");
722                 up_read(&qp->state_lock);
723                 *bad_wr = wr;
724                 return -EINVAL;
725         }
726         spin_lock_irqsave(&qp->sq_lock, flags);
727
728         while (wr) {
729                 u32 idx = qp->sq_put % qp->attrs.sq_size;
730                 struct siw_sqe *sqe = &qp->sendq[idx];
731
732                 if (sqe->flags) {
733                         siw_dbg_qp(qp, "sq full\n");
734                         rv = -ENOMEM;
735                         break;
736                 }
737                 if (wr->num_sge > qp->attrs.sq_max_sges) {
738                         siw_dbg_qp(qp, "too many sge's: %d\n", wr->num_sge);
739                         rv = -EINVAL;
740                         break;
741                 }
742                 sqe->id = wr->wr_id;
743
744                 if ((wr->send_flags & IB_SEND_SIGNALED) ||
745                     (qp->attrs.flags & SIW_SIGNAL_ALL_WR))
746                         sqe->flags |= SIW_WQE_SIGNALLED;
747
748                 if (wr->send_flags & IB_SEND_FENCE)
749                         sqe->flags |= SIW_WQE_READ_FENCE;
750
751                 switch (wr->opcode) {
752                 case IB_WR_SEND:
753                 case IB_WR_SEND_WITH_INV:
754                         if (wr->send_flags & IB_SEND_SOLICITED)
755                                 sqe->flags |= SIW_WQE_SOLICITED;
756
757                         if (!(wr->send_flags & IB_SEND_INLINE)) {
758                                 siw_copy_sgl(wr->sg_list, sqe->sge,
759                                              wr->num_sge);
760                                 sqe->num_sge = wr->num_sge;
761                         } else {
762                                 rv = siw_copy_inline_sgl(wr, sqe);
763                                 if (rv <= 0) {
764                                         rv = -EINVAL;
765                                         break;
766                                 }
767                                 sqe->flags |= SIW_WQE_INLINE;
768                                 sqe->num_sge = 1;
769                         }
770                         if (wr->opcode == IB_WR_SEND)
771                                 sqe->opcode = SIW_OP_SEND;
772                         else {
773                                 sqe->opcode = SIW_OP_SEND_REMOTE_INV;
774                                 sqe->rkey = wr->ex.invalidate_rkey;
775                         }
776                         break;
777
778                 case IB_WR_RDMA_READ_WITH_INV:
779                 case IB_WR_RDMA_READ:
780                         /*
781                          * iWarp restricts RREAD sink to SGL containing
782                          * 1 SGE only. we could relax to SGL with multiple
783                          * elements referring the SAME ltag or even sending
784                          * a private per-rreq tag referring to a checked
785                          * local sgl with MULTIPLE ltag's.
786                          */
787                         if (unlikely(wr->num_sge != 1)) {
788                                 rv = -EINVAL;
789                                 break;
790                         }
791                         siw_copy_sgl(wr->sg_list, &sqe->sge[0], 1);
792                         /*
793                          * NOTE: zero length RREAD is allowed!
794                          */
795                         sqe->raddr = rdma_wr(wr)->remote_addr;
796                         sqe->rkey = rdma_wr(wr)->rkey;
797                         sqe->num_sge = 1;
798
799                         if (wr->opcode == IB_WR_RDMA_READ)
800                                 sqe->opcode = SIW_OP_READ;
801                         else
802                                 sqe->opcode = SIW_OP_READ_LOCAL_INV;
803                         break;
804
805                 case IB_WR_RDMA_WRITE:
806                         if (!(wr->send_flags & IB_SEND_INLINE)) {
807                                 siw_copy_sgl(wr->sg_list, &sqe->sge[0],
808                                              wr->num_sge);
809                                 sqe->num_sge = wr->num_sge;
810                         } else {
811                                 rv = siw_copy_inline_sgl(wr, sqe);
812                                 if (unlikely(rv < 0)) {
813                                         rv = -EINVAL;
814                                         break;
815                                 }
816                                 sqe->flags |= SIW_WQE_INLINE;
817                                 sqe->num_sge = 1;
818                         }
819                         sqe->raddr = rdma_wr(wr)->remote_addr;
820                         sqe->rkey = rdma_wr(wr)->rkey;
821                         sqe->opcode = SIW_OP_WRITE;
822                         break;
823
824                 case IB_WR_REG_MR:
825                         sqe->base_mr = (uintptr_t)reg_wr(wr)->mr;
826                         sqe->rkey = reg_wr(wr)->key;
827                         sqe->access = reg_wr(wr)->access & IWARP_ACCESS_MASK;
828                         sqe->opcode = SIW_OP_REG_MR;
829                         break;
830
831                 case IB_WR_LOCAL_INV:
832                         sqe->rkey = wr->ex.invalidate_rkey;
833                         sqe->opcode = SIW_OP_INVAL_STAG;
834                         break;
835
836                 default:
837                         siw_dbg_qp(qp, "ib wr type %d unsupported\n",
838                                    wr->opcode);
839                         rv = -EINVAL;
840                         break;
841                 }
842                 siw_dbg_qp(qp, "opcode %d, flags 0x%x, wr_id 0x%pK\n",
843                            sqe->opcode, sqe->flags,
844                            (void *)(uintptr_t)sqe->id);
845
846                 if (unlikely(rv < 0))
847                         break;
848
849                 /* make SQE only valid after completely written */
850                 smp_wmb();
851                 sqe->flags |= SIW_WQE_VALID;
852
853                 qp->sq_put++;
854                 wr = wr->next;
855         }
856
857         /*
858          * Send directly if SQ processing is not in progress.
859          * Eventual immediate errors (rv < 0) do not affect the involved
860          * RI resources (Verbs, 8.3.1) and thus do not prevent from SQ
861          * processing, if new work is already pending. But rv must be passed
862          * to caller.
863          */
864         if (wqe->wr_status != SIW_WR_IDLE) {
865                 spin_unlock_irqrestore(&qp->sq_lock, flags);
866                 goto skip_direct_sending;
867         }
868         rv = siw_activate_tx(qp);
869         spin_unlock_irqrestore(&qp->sq_lock, flags);
870
871         if (rv <= 0)
872                 goto skip_direct_sending;
873
874         if (qp->kernel_verbs) {
875                 rv = siw_sq_start(qp);
876         } else {
877                 qp->tx_ctx.in_syscall = 1;
878
879                 if (siw_qp_sq_process(qp) != 0 && !(qp->tx_ctx.tx_suspend))
880                         siw_qp_cm_drop(qp, 0);
881
882                 qp->tx_ctx.in_syscall = 0;
883         }
884 skip_direct_sending:
885
886         up_read(&qp->state_lock);
887
888         if (rv >= 0)
889                 return 0;
890         /*
891          * Immediate error
892          */
893         siw_dbg_qp(qp, "error %d\n", rv);
894
895         *bad_wr = wr;
896         return rv;
897 }
898
899 /*
900  * siw_post_receive()
901  *
902  * Post a list of R-WR's to a RQ.
903  *
904  * @base_qp:    Base QP contained in siw QP
905  * @wr:         Null terminated list of user WR's
906  * @bad_wr:     Points to failing WR in case of synchronous failure.
907  */
908 int siw_post_receive(struct ib_qp *base_qp, const struct ib_recv_wr *wr,
909                      const struct ib_recv_wr **bad_wr)
910 {
911         struct siw_qp *qp = to_siw_qp(base_qp);
912         unsigned long flags;
913         int rv = 0;
914
915         if (qp->srq) {
916                 *bad_wr = wr;
917                 return -EOPNOTSUPP; /* what else from errno.h? */
918         }
919         /*
920          * Try to acquire QP state lock. Must be non-blocking
921          * to accommodate kernel clients needs.
922          */
923         if (!down_read_trylock(&qp->state_lock)) {
924                 *bad_wr = wr;
925                 return -ENOTCONN;
926         }
927         if (!qp->kernel_verbs) {
928                 siw_dbg_qp(qp, "no kernel post_recv for user mapped sq\n");
929                 up_read(&qp->state_lock);
930                 *bad_wr = wr;
931                 return -EINVAL;
932         }
933         if (qp->attrs.state > SIW_QP_STATE_RTS) {
934                 up_read(&qp->state_lock);
935                 *bad_wr = wr;
936                 return -EINVAL;
937         }
938         /*
939          * Serialize potentially multiple producers.
940          * Not needed for single threaded consumer side.
941          */
942         spin_lock_irqsave(&qp->rq_lock, flags);
943
944         while (wr) {
945                 u32 idx = qp->rq_put % qp->attrs.rq_size;
946                 struct siw_rqe *rqe = &qp->recvq[idx];
947
948                 if (rqe->flags) {
949                         siw_dbg_qp(qp, "RQ full\n");
950                         rv = -ENOMEM;
951                         break;
952                 }
953                 if (wr->num_sge > qp->attrs.rq_max_sges) {
954                         siw_dbg_qp(qp, "too many sge's: %d\n", wr->num_sge);
955                         rv = -EINVAL;
956                         break;
957                 }
958                 rqe->id = wr->wr_id;
959                 rqe->num_sge = wr->num_sge;
960                 siw_copy_sgl(wr->sg_list, rqe->sge, wr->num_sge);
961
962                 /* make sure RQE is completely written before valid */
963                 smp_wmb();
964
965                 rqe->flags = SIW_WQE_VALID;
966
967                 qp->rq_put++;
968                 wr = wr->next;
969         }
970         spin_unlock_irqrestore(&qp->rq_lock, flags);
971
972         up_read(&qp->state_lock);
973
974         if (rv < 0) {
975                 siw_dbg_qp(qp, "error %d\n", rv);
976                 *bad_wr = wr;
977         }
978         return rv > 0 ? 0 : rv;
979 }
980
981 void siw_destroy_cq(struct ib_cq *base_cq, struct ib_udata *udata)
982 {
983         struct siw_cq *cq = to_siw_cq(base_cq);
984         struct siw_device *sdev = to_siw_dev(base_cq->device);
985         struct siw_ucontext *ctx =
986                 rdma_udata_to_drv_context(udata, struct siw_ucontext,
987                                           base_ucontext);
988
989         siw_dbg_cq(cq, "free CQ resources\n");
990
991         siw_cq_flush(cq);
992
993         if (ctx && cq->xa_cq_index != SIW_INVAL_UOBJ_KEY)
994                 kfree(xa_erase(&ctx->xa, cq->xa_cq_index));
995
996         atomic_dec(&sdev->num_cq);
997
998         vfree(cq->queue);
999 }
1000
1001 /*
1002  * siw_create_cq()
1003  *
1004  * Populate CQ of requested size
1005  *
1006  * @base_cq: CQ as allocated by RDMA midlayer
1007  * @attr: Initial CQ attributes
1008  * @udata: relates to user context
1009  */
1010
1011 int siw_create_cq(struct ib_cq *base_cq, const struct ib_cq_init_attr *attr,
1012                   struct ib_udata *udata)
1013 {
1014         struct siw_device *sdev = to_siw_dev(base_cq->device);
1015         struct siw_cq *cq = to_siw_cq(base_cq);
1016         int rv, size = attr->cqe;
1017
1018         if (atomic_inc_return(&sdev->num_cq) > SIW_MAX_CQ) {
1019                 siw_dbg(base_cq->device, "too many CQ's\n");
1020                 rv = -ENOMEM;
1021                 goto err_out;
1022         }
1023         if (size < 1 || size > sdev->attrs.max_cqe) {
1024                 siw_dbg(base_cq->device, "CQ size error: %d\n", size);
1025                 rv = -EINVAL;
1026                 goto err_out;
1027         }
1028         size = roundup_pow_of_two(size);
1029         cq->base_cq.cqe = size;
1030         cq->num_cqe = size;
1031         cq->xa_cq_index = SIW_INVAL_UOBJ_KEY;
1032
1033         if (!udata) {
1034                 cq->kernel_verbs = 1;
1035                 cq->queue = vzalloc(size * sizeof(struct siw_cqe) +
1036                                     sizeof(struct siw_cq_ctrl));
1037         } else {
1038                 cq->queue = vmalloc_user(size * sizeof(struct siw_cqe) +
1039                                          sizeof(struct siw_cq_ctrl));
1040         }
1041         if (cq->queue == NULL) {
1042                 rv = -ENOMEM;
1043                 goto err_out;
1044         }
1045         get_random_bytes(&cq->id, 4);
1046         siw_dbg(base_cq->device, "new CQ [%u]\n", cq->id);
1047
1048         spin_lock_init(&cq->lock);
1049
1050         cq->notify = (struct siw_cq_ctrl *)&cq->queue[size];
1051
1052         if (udata) {
1053                 struct siw_uresp_create_cq uresp = {};
1054                 struct siw_ucontext *ctx =
1055                         rdma_udata_to_drv_context(udata, struct siw_ucontext,
1056                                                   base_ucontext);
1057
1058                 cq->xa_cq_index =
1059                         siw_create_uobj(ctx, cq->queue,
1060                                         size * sizeof(struct siw_cqe) +
1061                                                 sizeof(struct siw_cq_ctrl));
1062                 if (cq->xa_cq_index == SIW_INVAL_UOBJ_KEY) {
1063                         rv = -ENOMEM;
1064                         goto err_out;
1065                 }
1066                 uresp.cq_key = cq->xa_cq_index << PAGE_SHIFT;
1067                 uresp.cq_id = cq->id;
1068                 uresp.num_cqe = size;
1069
1070                 if (udata->outlen < sizeof(uresp)) {
1071                         rv = -EINVAL;
1072                         goto err_out;
1073                 }
1074                 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1075                 if (rv)
1076                         goto err_out;
1077         }
1078         return 0;
1079
1080 err_out:
1081         siw_dbg(base_cq->device, "CQ creation failed: %d", rv);
1082
1083         if (cq && cq->queue) {
1084                 struct siw_ucontext *ctx =
1085                         rdma_udata_to_drv_context(udata, struct siw_ucontext,
1086                                                   base_ucontext);
1087                 if (cq->xa_cq_index != SIW_INVAL_UOBJ_KEY)
1088                         kfree(xa_erase(&ctx->xa, cq->xa_cq_index));
1089                 vfree(cq->queue);
1090         }
1091         atomic_dec(&sdev->num_cq);
1092
1093         return rv;
1094 }
1095
1096 /*
1097  * siw_poll_cq()
1098  *
1099  * Reap CQ entries if available and copy work completion status into
1100  * array of WC's provided by caller. Returns number of reaped CQE's.
1101  *
1102  * @base_cq:    Base CQ contained in siw CQ.
1103  * @num_cqe:    Maximum number of CQE's to reap.
1104  * @wc:         Array of work completions to be filled by siw.
1105  */
1106 int siw_poll_cq(struct ib_cq *base_cq, int num_cqe, struct ib_wc *wc)
1107 {
1108         struct siw_cq *cq = to_siw_cq(base_cq);
1109         int i;
1110
1111         for (i = 0; i < num_cqe; i++) {
1112                 if (!siw_reap_cqe(cq, wc))
1113                         break;
1114                 wc++;
1115         }
1116         return i;
1117 }
1118
1119 /*
1120  * siw_req_notify_cq()
1121  *
1122  * Request notification for new CQE's added to that CQ.
1123  * Defined flags:
1124  * o SIW_CQ_NOTIFY_SOLICITED lets siw trigger a notification
1125  *   event if a WQE with notification flag set enters the CQ
1126  * o SIW_CQ_NOTIFY_NEXT_COMP lets siw trigger a notification
1127  *   event if a WQE enters the CQ.
1128  * o IB_CQ_REPORT_MISSED_EVENTS: return value will provide the
1129  *   number of not reaped CQE's regardless of its notification
1130  *   type and current or new CQ notification settings.
1131  *
1132  * @base_cq:    Base CQ contained in siw CQ.
1133  * @flags:      Requested notification flags.
1134  */
1135 int siw_req_notify_cq(struct ib_cq *base_cq, enum ib_cq_notify_flags flags)
1136 {
1137         struct siw_cq *cq = to_siw_cq(base_cq);
1138
1139         siw_dbg_cq(cq, "flags: 0x%02x\n", flags);
1140
1141         if ((flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
1142                 /*
1143                  * Enable CQ event for next solicited completion.
1144                  * and make it visible to all associated producers.
1145                  */
1146                 smp_store_mb(cq->notify->flags, SIW_NOTIFY_SOLICITED);
1147         else
1148                 /*
1149                  * Enable CQ event for any signalled completion.
1150                  * and make it visible to all associated producers.
1151                  */
1152                 smp_store_mb(cq->notify->flags, SIW_NOTIFY_ALL);
1153
1154         if (flags & IB_CQ_REPORT_MISSED_EVENTS)
1155                 return cq->cq_put - cq->cq_get;
1156
1157         return 0;
1158 }
1159
1160 /*
1161  * siw_dereg_mr()
1162  *
1163  * Release Memory Region.
1164  *
1165  * @base_mr: Base MR contained in siw MR.
1166  * @udata: points to user context, unused.
1167  */
1168 int siw_dereg_mr(struct ib_mr *base_mr, struct ib_udata *udata)
1169 {
1170         struct siw_mr *mr = to_siw_mr(base_mr);
1171         struct siw_device *sdev = to_siw_dev(base_mr->device);
1172
1173         siw_dbg_mem(mr->mem, "deregister MR\n");
1174
1175         atomic_dec(&sdev->num_mr);
1176
1177         siw_mr_drop_mem(mr);
1178         kfree_rcu(mr, rcu);
1179
1180         return 0;
1181 }
1182
1183 /*
1184  * siw_reg_user_mr()
1185  *
1186  * Register Memory Region.
1187  *
1188  * @pd:         Protection Domain
1189  * @start:      starting address of MR (virtual address)
1190  * @len:        len of MR
1191  * @rnic_va:    not used by siw
1192  * @rights:     MR access rights
1193  * @udata:      user buffer to communicate STag and Key.
1194  */
1195 struct ib_mr *siw_reg_user_mr(struct ib_pd *pd, u64 start, u64 len,
1196                               u64 rnic_va, int rights, struct ib_udata *udata)
1197 {
1198         struct siw_mr *mr = NULL;
1199         struct siw_umem *umem = NULL;
1200         struct siw_ureq_reg_mr ureq;
1201         struct siw_device *sdev = to_siw_dev(pd->device);
1202
1203         unsigned long mem_limit = rlimit(RLIMIT_MEMLOCK);
1204         int rv;
1205
1206         siw_dbg_pd(pd, "start: 0x%pK, va: 0x%pK, len: %llu\n",
1207                    (void *)(uintptr_t)start, (void *)(uintptr_t)rnic_va,
1208                    (unsigned long long)len);
1209
1210         if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1211                 siw_dbg_pd(pd, "too many mr's\n");
1212                 rv = -ENOMEM;
1213                 goto err_out;
1214         }
1215         if (!len) {
1216                 rv = -EINVAL;
1217                 goto err_out;
1218         }
1219         if (mem_limit != RLIM_INFINITY) {
1220                 unsigned long num_pages =
1221                         (PAGE_ALIGN(len + (start & ~PAGE_MASK))) >> PAGE_SHIFT;
1222                 mem_limit >>= PAGE_SHIFT;
1223
1224                 if (num_pages > mem_limit - current->mm->locked_vm) {
1225                         siw_dbg_pd(pd, "pages req %lu, max %lu, lock %lu\n",
1226                                    num_pages, mem_limit,
1227                                    current->mm->locked_vm);
1228                         rv = -ENOMEM;
1229                         goto err_out;
1230                 }
1231         }
1232         umem = siw_umem_get(start, len, ib_access_writable(rights));
1233         if (IS_ERR(umem)) {
1234                 rv = PTR_ERR(umem);
1235                 siw_dbg_pd(pd, "getting user memory failed: %d\n", rv);
1236                 umem = NULL;
1237                 goto err_out;
1238         }
1239         mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1240         if (!mr) {
1241                 rv = -ENOMEM;
1242                 goto err_out;
1243         }
1244         rv = siw_mr_add_mem(mr, pd, umem, start, len, rights);
1245         if (rv)
1246                 goto err_out;
1247
1248         if (udata) {
1249                 struct siw_uresp_reg_mr uresp = {};
1250                 struct siw_mem *mem = mr->mem;
1251
1252                 if (udata->inlen < sizeof(ureq)) {
1253                         rv = -EINVAL;
1254                         goto err_out;
1255                 }
1256                 rv = ib_copy_from_udata(&ureq, udata, sizeof(ureq));
1257                 if (rv)
1258                         goto err_out;
1259
1260                 mr->base_mr.lkey |= ureq.stag_key;
1261                 mr->base_mr.rkey |= ureq.stag_key;
1262                 mem->stag |= ureq.stag_key;
1263                 uresp.stag = mem->stag;
1264
1265                 if (udata->outlen < sizeof(uresp)) {
1266                         rv = -EINVAL;
1267                         goto err_out;
1268                 }
1269                 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1270                 if (rv)
1271                         goto err_out;
1272         }
1273         mr->mem->stag_valid = 1;
1274
1275         return &mr->base_mr;
1276
1277 err_out:
1278         atomic_dec(&sdev->num_mr);
1279         if (mr) {
1280                 if (mr->mem)
1281                         siw_mr_drop_mem(mr);
1282                 kfree_rcu(mr, rcu);
1283         } else {
1284                 if (umem)
1285                         siw_umem_release(umem, false);
1286         }
1287         return ERR_PTR(rv);
1288 }
1289
1290 struct ib_mr *siw_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
1291                            u32 max_sge, struct ib_udata *udata)
1292 {
1293         struct siw_device *sdev = to_siw_dev(pd->device);
1294         struct siw_mr *mr = NULL;
1295         struct siw_pbl *pbl = NULL;
1296         int rv;
1297
1298         if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1299                 siw_dbg_pd(pd, "too many mr's\n");
1300                 rv = -ENOMEM;
1301                 goto err_out;
1302         }
1303         if (mr_type != IB_MR_TYPE_MEM_REG) {
1304                 siw_dbg_pd(pd, "mr type %d unsupported\n", mr_type);
1305                 rv = -EOPNOTSUPP;
1306                 goto err_out;
1307         }
1308         if (max_sge > SIW_MAX_SGE_PBL) {
1309                 siw_dbg_pd(pd, "too many sge's: %d\n", max_sge);
1310                 rv = -ENOMEM;
1311                 goto err_out;
1312         }
1313         pbl = siw_pbl_alloc(max_sge);
1314         if (IS_ERR(pbl)) {
1315                 rv = PTR_ERR(pbl);
1316                 siw_dbg_pd(pd, "pbl allocation failed: %d\n", rv);
1317                 pbl = NULL;
1318                 goto err_out;
1319         }
1320         mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1321         if (!mr) {
1322                 rv = -ENOMEM;
1323                 goto err_out;
1324         }
1325         rv = siw_mr_add_mem(mr, pd, pbl, 0, max_sge * PAGE_SIZE, 0);
1326         if (rv)
1327                 goto err_out;
1328
1329         mr->mem->is_pbl = 1;
1330
1331         siw_dbg_pd(pd, "[MEM %u]: success\n", mr->mem->stag);
1332
1333         return &mr->base_mr;
1334
1335 err_out:
1336         atomic_dec(&sdev->num_mr);
1337
1338         if (!mr) {
1339                 kfree(pbl);
1340         } else {
1341                 if (mr->mem)
1342                         siw_mr_drop_mem(mr);
1343                 kfree_rcu(mr, rcu);
1344         }
1345         siw_dbg_pd(pd, "failed: %d\n", rv);
1346
1347         return ERR_PTR(rv);
1348 }
1349
1350 /* Just used to count number of pages being mapped */
1351 static int siw_set_pbl_page(struct ib_mr *base_mr, u64 buf_addr)
1352 {
1353         return 0;
1354 }
1355
1356 int siw_map_mr_sg(struct ib_mr *base_mr, struct scatterlist *sl, int num_sle,
1357                   unsigned int *sg_off)
1358 {
1359         struct scatterlist *slp;
1360         struct siw_mr *mr = to_siw_mr(base_mr);
1361         struct siw_mem *mem = mr->mem;
1362         struct siw_pbl *pbl = mem->pbl;
1363         struct siw_pble *pble;
1364         unsigned long pbl_size;
1365         int i, rv;
1366
1367         if (!pbl) {
1368                 siw_dbg_mem(mem, "no PBL allocated\n");
1369                 return -EINVAL;
1370         }
1371         pble = pbl->pbe;
1372
1373         if (pbl->max_buf < num_sle) {
1374                 siw_dbg_mem(mem, "too many SGE's: %d > %d\n",
1375                             mem->pbl->max_buf, num_sle);
1376                 return -ENOMEM;
1377         }
1378         for_each_sg(sl, slp, num_sle, i) {
1379                 if (sg_dma_len(slp) == 0) {
1380                         siw_dbg_mem(mem, "empty SGE\n");
1381                         return -EINVAL;
1382                 }
1383                 if (i == 0) {
1384                         pble->addr = sg_dma_address(slp);
1385                         pble->size = sg_dma_len(slp);
1386                         pble->pbl_off = 0;
1387                         pbl_size = pble->size;
1388                         pbl->num_buf = 1;
1389                 } else {
1390                         /* Merge PBL entries if adjacent */
1391                         if (pble->addr + pble->size == sg_dma_address(slp)) {
1392                                 pble->size += sg_dma_len(slp);
1393                         } else {
1394                                 pble++;
1395                                 pbl->num_buf++;
1396                                 pble->addr = sg_dma_address(slp);
1397                                 pble->size = sg_dma_len(slp);
1398                                 pble->pbl_off = pbl_size;
1399                         }
1400                         pbl_size += sg_dma_len(slp);
1401                 }
1402                 siw_dbg_mem(mem,
1403                         "sge[%d], size %u, addr 0x%p, total %lu\n",
1404                         i, pble->size, (void *)(uintptr_t)pble->addr,
1405                         pbl_size);
1406         }
1407         rv = ib_sg_to_pages(base_mr, sl, num_sle, sg_off, siw_set_pbl_page);
1408         if (rv > 0) {
1409                 mem->len = base_mr->length;
1410                 mem->va = base_mr->iova;
1411                 siw_dbg_mem(mem,
1412                         "%llu bytes, start 0x%pK, %u SLE to %u entries\n",
1413                         mem->len, (void *)(uintptr_t)mem->va, num_sle,
1414                         pbl->num_buf);
1415         }
1416         return rv;
1417 }
1418
1419 /*
1420  * siw_get_dma_mr()
1421  *
1422  * Create a (empty) DMA memory region, where no umem is attached.
1423  */
1424 struct ib_mr *siw_get_dma_mr(struct ib_pd *pd, int rights)
1425 {
1426         struct siw_device *sdev = to_siw_dev(pd->device);
1427         struct siw_mr *mr = NULL;
1428         int rv;
1429
1430         if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1431                 siw_dbg_pd(pd, "too many mr's\n");
1432                 rv = -ENOMEM;
1433                 goto err_out;
1434         }
1435         mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1436         if (!mr) {
1437                 rv = -ENOMEM;
1438                 goto err_out;
1439         }
1440         rv = siw_mr_add_mem(mr, pd, NULL, 0, ULONG_MAX, rights);
1441         if (rv)
1442                 goto err_out;
1443
1444         mr->mem->stag_valid = 1;
1445
1446         siw_dbg_pd(pd, "[MEM %u]: success\n", mr->mem->stag);
1447
1448         return &mr->base_mr;
1449
1450 err_out:
1451         if (rv)
1452                 kfree(mr);
1453
1454         atomic_dec(&sdev->num_mr);
1455
1456         return ERR_PTR(rv);
1457 }
1458
1459 /*
1460  * siw_create_srq()
1461  *
1462  * Create Shared Receive Queue of attributes @init_attrs
1463  * within protection domain given by @pd.
1464  *
1465  * @base_srq:   Base SRQ contained in siw SRQ.
1466  * @init_attrs: SRQ init attributes.
1467  * @udata:      points to user context
1468  */
1469 int siw_create_srq(struct ib_srq *base_srq,
1470                    struct ib_srq_init_attr *init_attrs, struct ib_udata *udata)
1471 {
1472         struct siw_srq *srq = to_siw_srq(base_srq);
1473         struct ib_srq_attr *attrs = &init_attrs->attr;
1474         struct siw_device *sdev = to_siw_dev(base_srq->device);
1475         struct siw_ucontext *ctx =
1476                 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1477                                           base_ucontext);
1478         int rv;
1479
1480         if (atomic_inc_return(&sdev->num_srq) > SIW_MAX_SRQ) {
1481                 siw_dbg_pd(base_srq->pd, "too many SRQ's\n");
1482                 rv = -ENOMEM;
1483                 goto err_out;
1484         }
1485         if (attrs->max_wr == 0 || attrs->max_wr > SIW_MAX_SRQ_WR ||
1486             attrs->max_sge > SIW_MAX_SGE || attrs->srq_limit > attrs->max_wr) {
1487                 rv = -EINVAL;
1488                 goto err_out;
1489         }
1490         srq->max_sge = attrs->max_sge;
1491         srq->num_rqe = roundup_pow_of_two(attrs->max_wr);
1492         srq->xa_srq_index = SIW_INVAL_UOBJ_KEY;
1493         srq->limit = attrs->srq_limit;
1494         if (srq->limit)
1495                 srq->armed = 1;
1496
1497         srq->kernel_verbs = !udata;
1498
1499         if (udata)
1500                 srq->recvq =
1501                         vmalloc_user(srq->num_rqe * sizeof(struct siw_rqe));
1502         else
1503                 srq->recvq = vzalloc(srq->num_rqe * sizeof(struct siw_rqe));
1504
1505         if (srq->recvq == NULL) {
1506                 rv = -ENOMEM;
1507                 goto err_out;
1508         }
1509         if (udata) {
1510                 struct siw_uresp_create_srq uresp = {};
1511
1512                 srq->xa_srq_index = siw_create_uobj(
1513                         ctx, srq->recvq, srq->num_rqe * sizeof(struct siw_rqe));
1514
1515                 if (srq->xa_srq_index == SIW_INVAL_UOBJ_KEY) {
1516                         rv = -ENOMEM;
1517                         goto err_out;
1518                 }
1519                 uresp.srq_key = srq->xa_srq_index;
1520                 uresp.num_rqe = srq->num_rqe;
1521
1522                 if (udata->outlen < sizeof(uresp)) {
1523                         rv = -EINVAL;
1524                         goto err_out;
1525                 }
1526                 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1527                 if (rv)
1528                         goto err_out;
1529         }
1530         spin_lock_init(&srq->lock);
1531
1532         siw_dbg_pd(base_srq->pd, "[SRQ]: success\n");
1533
1534         return 0;
1535
1536 err_out:
1537         if (srq->recvq) {
1538                 if (ctx && srq->xa_srq_index != SIW_INVAL_UOBJ_KEY)
1539                         kfree(xa_erase(&ctx->xa, srq->xa_srq_index));
1540                 vfree(srq->recvq);
1541         }
1542         atomic_dec(&sdev->num_srq);
1543
1544         return rv;
1545 }
1546
1547 /*
1548  * siw_modify_srq()
1549  *
1550  * Modify SRQ. The caller may resize SRQ and/or set/reset notification
1551  * limit and (re)arm IB_EVENT_SRQ_LIMIT_REACHED notification.
1552  *
1553  * NOTE: it is unclear if RDMA core allows for changing the MAX_SGE
1554  * parameter. siw_modify_srq() does not check the attrs->max_sge param.
1555  */
1556 int siw_modify_srq(struct ib_srq *base_srq, struct ib_srq_attr *attrs,
1557                    enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
1558 {
1559         struct siw_srq *srq = to_siw_srq(base_srq);
1560         unsigned long flags;
1561         int rv = 0;
1562
1563         spin_lock_irqsave(&srq->lock, flags);
1564
1565         if (attr_mask & IB_SRQ_MAX_WR) {
1566                 /* resize request not yet supported */
1567                 rv = -EOPNOTSUPP;
1568                 goto out;
1569         }
1570         if (attr_mask & IB_SRQ_LIMIT) {
1571                 if (attrs->srq_limit) {
1572                         if (unlikely(attrs->srq_limit > srq->num_rqe)) {
1573                                 rv = -EINVAL;
1574                                 goto out;
1575                         }
1576                         srq->armed = 1;
1577                 } else {
1578                         srq->armed = 0;
1579                 }
1580                 srq->limit = attrs->srq_limit;
1581         }
1582 out:
1583         spin_unlock_irqrestore(&srq->lock, flags);
1584
1585         return rv;
1586 }
1587
1588 /*
1589  * siw_query_srq()
1590  *
1591  * Query SRQ attributes.
1592  */
1593 int siw_query_srq(struct ib_srq *base_srq, struct ib_srq_attr *attrs)
1594 {
1595         struct siw_srq *srq = to_siw_srq(base_srq);
1596         unsigned long flags;
1597
1598         spin_lock_irqsave(&srq->lock, flags);
1599
1600         attrs->max_wr = srq->num_rqe;
1601         attrs->max_sge = srq->max_sge;
1602         attrs->srq_limit = srq->limit;
1603
1604         spin_unlock_irqrestore(&srq->lock, flags);
1605
1606         return 0;
1607 }
1608
1609 /*
1610  * siw_destroy_srq()
1611  *
1612  * Destroy SRQ.
1613  * It is assumed that the SRQ is not referenced by any
1614  * QP anymore - the code trusts the RDMA core environment to keep track
1615  * of QP references.
1616  */
1617 void siw_destroy_srq(struct ib_srq *base_srq, struct ib_udata *udata)
1618 {
1619         struct siw_srq *srq = to_siw_srq(base_srq);
1620         struct siw_device *sdev = to_siw_dev(base_srq->device);
1621         struct siw_ucontext *ctx =
1622                 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1623                                           base_ucontext);
1624
1625         if (ctx && srq->xa_srq_index != SIW_INVAL_UOBJ_KEY)
1626                 kfree(xa_erase(&ctx->xa, srq->xa_srq_index));
1627
1628         vfree(srq->recvq);
1629         atomic_dec(&sdev->num_srq);
1630 }
1631
1632 /*
1633  * siw_post_srq_recv()
1634  *
1635  * Post a list of receive queue elements to SRQ.
1636  * NOTE: The function does not check or lock a certain SRQ state
1637  *       during the post operation. The code simply trusts the
1638  *       RDMA core environment.
1639  *
1640  * @base_srq:   Base SRQ contained in siw SRQ
1641  * @wr:         List of R-WR's
1642  * @bad_wr:     Updated to failing WR if posting fails.
1643  */
1644 int siw_post_srq_recv(struct ib_srq *base_srq, const struct ib_recv_wr *wr,
1645                       const struct ib_recv_wr **bad_wr)
1646 {
1647         struct siw_srq *srq = to_siw_srq(base_srq);
1648         unsigned long flags;
1649         int rv = 0;
1650
1651         if (unlikely(!srq->kernel_verbs)) {
1652                 siw_dbg_pd(base_srq->pd,
1653                            "[SRQ]: no kernel post_recv for mapped srq\n");
1654                 rv = -EINVAL;
1655                 goto out;
1656         }
1657         /*
1658          * Serialize potentially multiple producers.
1659          * Also needed to serialize potentially multiple
1660          * consumers.
1661          */
1662         spin_lock_irqsave(&srq->lock, flags);
1663
1664         while (wr) {
1665                 u32 idx = srq->rq_put % srq->num_rqe;
1666                 struct siw_rqe *rqe = &srq->recvq[idx];
1667
1668                 if (rqe->flags) {
1669                         siw_dbg_pd(base_srq->pd, "SRQ full\n");
1670                         rv = -ENOMEM;
1671                         break;
1672                 }
1673                 if (unlikely(wr->num_sge > srq->max_sge)) {
1674                         siw_dbg_pd(base_srq->pd,
1675                                    "[SRQ]: too many sge's: %d\n", wr->num_sge);
1676                         rv = -EINVAL;
1677                         break;
1678                 }
1679                 rqe->id = wr->wr_id;
1680                 rqe->num_sge = wr->num_sge;
1681                 siw_copy_sgl(wr->sg_list, rqe->sge, wr->num_sge);
1682
1683                 /* Make sure S-RQE is completely written before valid */
1684                 smp_wmb();
1685
1686                 rqe->flags = SIW_WQE_VALID;
1687
1688                 srq->rq_put++;
1689                 wr = wr->next;
1690         }
1691         spin_unlock_irqrestore(&srq->lock, flags);
1692 out:
1693         if (unlikely(rv < 0)) {
1694                 siw_dbg_pd(base_srq->pd, "[SRQ]: error %d\n", rv);
1695                 *bad_wr = wr;
1696         }
1697         return rv;
1698 }
1699
1700 void siw_qp_event(struct siw_qp *qp, enum ib_event_type etype)
1701 {
1702         struct ib_event event;
1703         struct ib_qp *base_qp = qp->ib_qp;
1704
1705         /*
1706          * Do not report asynchronous errors on QP which gets
1707          * destroyed via verbs interface (siw_destroy_qp())
1708          */
1709         if (qp->attrs.flags & SIW_QP_IN_DESTROY)
1710                 return;
1711
1712         event.event = etype;
1713         event.device = base_qp->device;
1714         event.element.qp = base_qp;
1715
1716         if (base_qp->event_handler) {
1717                 siw_dbg_qp(qp, "reporting event %d\n", etype);
1718                 base_qp->event_handler(&event, base_qp->qp_context);
1719         }
1720 }
1721
1722 void siw_cq_event(struct siw_cq *cq, enum ib_event_type etype)
1723 {
1724         struct ib_event event;
1725         struct ib_cq *base_cq = &cq->base_cq;
1726
1727         event.event = etype;
1728         event.device = base_cq->device;
1729         event.element.cq = base_cq;
1730
1731         if (base_cq->event_handler) {
1732                 siw_dbg_cq(cq, "reporting CQ event %d\n", etype);
1733                 base_cq->event_handler(&event, base_cq->cq_context);
1734         }
1735 }
1736
1737 void siw_srq_event(struct siw_srq *srq, enum ib_event_type etype)
1738 {
1739         struct ib_event event;
1740         struct ib_srq *base_srq = &srq->base_srq;
1741
1742         event.event = etype;
1743         event.device = base_srq->device;
1744         event.element.srq = base_srq;
1745
1746         if (base_srq->event_handler) {
1747                 siw_dbg_pd(srq->base_srq.pd,
1748                            "reporting SRQ event %d\n", etype);
1749                 base_srq->event_handler(&event, base_srq->srq_context);
1750         }
1751 }
1752
1753 void siw_port_event(struct siw_device *sdev, u8 port, enum ib_event_type etype)
1754 {
1755         struct ib_event event;
1756
1757         event.event = etype;
1758         event.device = &sdev->base_dev;
1759         event.element.port_num = port;
1760
1761         siw_dbg(&sdev->base_dev, "reporting port event %d\n", etype);
1762
1763         ib_dispatch_event(&event);
1764 }