1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/module.h>
34#include <linux/mlx5/qp.h>
35#include <linux/mlx5/srq.h>
36#include <linux/slab.h>
37#include <rdma/ib_umem.h>
38#include <rdma/ib_user_verbs.h>
39
40#include "mlx5_ib.h"
41
42
43static int srq_signature;
44
45static void *get_wqe(struct mlx5_ib_srq *srq, int n)
46{
47 return mlx5_buf_offset(&srq->buf, n << srq->msrq.wqe_shift);
48}
49
50static void mlx5_ib_srq_event(struct mlx5_core_srq *srq, enum mlx5_event type)
51{
52 struct ib_event event;
53 struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq;
54
55 if (ibsrq->event_handler) {
56 event.device = ibsrq->device;
57 event.element.srq = ibsrq;
58 switch (type) {
59 case MLX5_EVENT_TYPE_SRQ_RQ_LIMIT:
60 event.event = IB_EVENT_SRQ_LIMIT_REACHED;
61 break;
62 case MLX5_EVENT_TYPE_SRQ_CATAS_ERROR:
63 event.event = IB_EVENT_SRQ_ERR;
64 break;
65 default:
66 pr_warn("mlx5_ib: Unexpected event type %d on SRQ %06x\n",
67 type, srq->srqn);
68 return;
69 }
70
71 ibsrq->event_handler(&event, ibsrq->srq_context);
72 }
73}
74
75static int create_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq,
76 struct mlx5_srq_attr *in,
77 struct ib_udata *udata, int buf_size)
78{
79 struct mlx5_ib_dev *dev = to_mdev(pd->device);
80 struct mlx5_ib_create_srq ucmd = {};
81 size_t ucmdlen;
82 int err;
83 int npages;
84 int page_shift;
85 int ncont;
86 u32 offset;
87 u32 uidx = MLX5_IB_DEFAULT_UIDX;
88
89 ucmdlen = min(udata->inlen, sizeof(ucmd));
90
91 if (ib_copy_from_udata(&ucmd, udata, ucmdlen)) {
92 mlx5_ib_dbg(dev, "failed copy udata\n");
93 return -EFAULT;
94 }
95
96 if (ucmd.reserved0 || ucmd.reserved1)
97 return -EINVAL;
98
99 if (udata->inlen > sizeof(ucmd) &&
100 !ib_is_udata_cleared(udata, sizeof(ucmd),
101 udata->inlen - sizeof(ucmd)))
102 return -EINVAL;
103
104 if (in->type == IB_SRQT_XRC) {
105 err = get_srq_user_index(to_mucontext(pd->uobject->context),
106 &ucmd, udata->inlen, &uidx);
107 if (err)
108 return err;
109 }
110
111 srq->wq_sig = !!(ucmd.flags & MLX5_SRQ_FLAG_SIGNATURE);
112
113 srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr, buf_size,
114 0, 0);
115 if (IS_ERR(srq->umem)) {
116 mlx5_ib_dbg(dev, "failed umem get, size %d\n", buf_size);
117 err = PTR_ERR(srq->umem);
118 return err;
119 }
120
121 mlx5_ib_cont_pages(srq->umem, ucmd.buf_addr, &npages,
122 &page_shift, &ncont, NULL);
123 err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift,
124 &offset);
125 if (err) {
126 mlx5_ib_warn(dev, "bad offset\n");
127 goto err_umem;
128 }
129
130 in->pas = mlx5_vzalloc(sizeof(*in->pas) * ncont);
131 if (!in->pas) {
132 err = -ENOMEM;
133 goto err_umem;
134 }
135
136 mlx5_ib_populate_pas(dev, srq->umem, page_shift, in->pas, 0);
137
138 err = mlx5_ib_db_map_user(to_mucontext(pd->uobject->context),
139 ucmd.db_addr, &srq->db);
140 if (err) {
141 mlx5_ib_dbg(dev, "map doorbell failed\n");
142 goto err_in;
143 }
144
145 in->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
146 in->page_offset = offset;
147 if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 &&
148 in->type == IB_SRQT_XRC)
149 in->user_index = uidx;
150
151 return 0;
152
153err_in:
154 kvfree(in->pas);
155
156err_umem:
157 ib_umem_release(srq->umem);
158
159 return err;
160}
161
162static int create_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq,
163 struct mlx5_srq_attr *in, int buf_size)
164{
165 int err;
166 int i;
167 struct mlx5_wqe_srq_next_seg *next;
168 int page_shift;
169 int npages;
170
171 err = mlx5_db_alloc(dev->mdev, &srq->db);
172 if (err) {
173 mlx5_ib_warn(dev, "alloc dbell rec failed\n");
174 return err;
175 }
176
177 if (mlx5_buf_alloc(dev->mdev, buf_size, &srq->buf)) {
178 mlx5_ib_dbg(dev, "buf alloc failed\n");
179 err = -ENOMEM;
180 goto err_db;
181 }
182 page_shift = srq->buf.page_shift;
183
184 srq->head = 0;
185 srq->tail = srq->msrq.max - 1;
186 srq->wqe_ctr = 0;
187
188 for (i = 0; i < srq->msrq.max; i++) {
189 next = get_wqe(srq, i);
190 next->next_wqe_index =
191 cpu_to_be16((i + 1) & (srq->msrq.max - 1));
192 }
193
194 npages = DIV_ROUND_UP(srq->buf.npages, 1 << (page_shift - PAGE_SHIFT));
195 mlx5_ib_dbg(dev, "buf_size %d, page_shift %d, npages %d, calc npages %d\n",
196 buf_size, page_shift, srq->buf.npages, npages);
197 in->pas = mlx5_vzalloc(sizeof(*in->pas) * npages);
198 if (!in->pas) {
199 err = -ENOMEM;
200 goto err_buf;
201 }
202 mlx5_fill_page_array(&srq->buf, in->pas);
203
204 srq->wrid = kmalloc(srq->msrq.max * sizeof(u64), GFP_KERNEL);
205 if (!srq->wrid) {
206 mlx5_ib_dbg(dev, "kmalloc failed %lu\n",
207 (unsigned long)(srq->msrq.max * sizeof(u64)));
208 err = -ENOMEM;
209 goto err_in;
210 }
211 srq->wq_sig = !!srq_signature;
212
213 in->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
214 if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 &&
215 in->type == IB_SRQT_XRC)
216 in->user_index = MLX5_IB_DEFAULT_UIDX;
217
218 return 0;
219
220err_in:
221 kvfree(in->pas);
222
223err_buf:
224 mlx5_buf_free(dev->mdev, &srq->buf);
225
226err_db:
227 mlx5_db_free(dev->mdev, &srq->db);
228 return err;
229}
230
231static void destroy_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq)
232{
233 mlx5_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db);
234 ib_umem_release(srq->umem);
235}
236
237
238static void destroy_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq)
239{
240 kfree(srq->wrid);
241 mlx5_buf_free(dev->mdev, &srq->buf);
242 mlx5_db_free(dev->mdev, &srq->db);
243}
244
245struct ib_srq *mlx5_ib_create_srq(struct ib_pd *pd,
246 struct ib_srq_init_attr *init_attr,
247 struct ib_udata *udata)
248{
249 struct mlx5_ib_dev *dev = to_mdev(pd->device);
250 struct mlx5_ib_srq *srq;
251 int desc_size;
252 int buf_size;
253 int err;
254 struct mlx5_srq_attr in = {0};
255 __u32 max_srq_wqes = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
256
257
258 if (init_attr->attr.max_wr >= max_srq_wqes) {
259 mlx5_ib_dbg(dev, "max_wr %d, cap %d\n",
260 init_attr->attr.max_wr,
261 max_srq_wqes);
262 return ERR_PTR(-EINVAL);
263 }
264
265 srq = kmalloc(sizeof(*srq), GFP_KERNEL);
266 if (!srq)
267 return ERR_PTR(-ENOMEM);
268
269 mutex_init(&srq->mutex);
270 spin_lock_init(&srq->lock);
271 srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1);
272 srq->msrq.max_gs = init_attr->attr.max_sge;
273
274 desc_size = sizeof(struct mlx5_wqe_srq_next_seg) +
275 srq->msrq.max_gs * sizeof(struct mlx5_wqe_data_seg);
276 desc_size = roundup_pow_of_two(desc_size);
277 desc_size = max_t(int, 32, desc_size);
278 srq->msrq.max_avail_gather = (desc_size - sizeof(struct mlx5_wqe_srq_next_seg)) /
279 sizeof(struct mlx5_wqe_data_seg);
280 srq->msrq.wqe_shift = ilog2(desc_size);
281 buf_size = srq->msrq.max * desc_size;
282 mlx5_ib_dbg(dev, "desc_size 0x%x, req wr 0x%x, srq size 0x%x, max_gs 0x%x, max_avail_gather 0x%x\n",
283 desc_size, init_attr->attr.max_wr, srq->msrq.max, srq->msrq.max_gs,
284 srq->msrq.max_avail_gather);
285
286 if (pd->uobject)
287 err = create_srq_user(pd, srq, &in, udata, buf_size);
288 else
289 err = create_srq_kernel(dev, srq, &in, buf_size);
290
291 if (err) {
292 mlx5_ib_warn(dev, "create srq %s failed, err %d\n",
293 pd->uobject ? "user" : "kernel", err);
294 goto err_srq;
295 }
296
297 in.type = init_attr->srq_type;
298 in.log_size = ilog2(srq->msrq.max);
299 in.wqe_shift = srq->msrq.wqe_shift - 4;
300 if (srq->wq_sig)
301 in.flags |= MLX5_SRQ_FLAG_WQ_SIG;
302 if (init_attr->srq_type == IB_SRQT_XRC) {
303 in.xrcd = to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn;
304 in.cqn = to_mcq(init_attr->ext.xrc.cq)->mcq.cqn;
305 } else if (init_attr->srq_type == IB_SRQT_BASIC) {
306 in.xrcd = to_mxrcd(dev->devr.x0)->xrcdn;
307 in.cqn = to_mcq(dev->devr.c0)->mcq.cqn;
308 }
309
310 in.pd = to_mpd(pd)->pdn;
311 in.db_record = srq->db.dma;
312 err = mlx5_core_create_srq(dev->mdev, &srq->msrq, &in);
313 kvfree(in.pas);
314 if (err) {
315 mlx5_ib_dbg(dev, "create SRQ failed, err %d\n", err);
316 goto err_usr_kern_srq;
317 }
318
319 mlx5_ib_dbg(dev, "create SRQ with srqn 0x%x\n", srq->msrq.srqn);
320
321 srq->msrq.event = mlx5_ib_srq_event;
322 srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn;
323
324 if (pd->uobject)
325 if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof(__u32))) {
326 mlx5_ib_dbg(dev, "copy to user failed\n");
327 err = -EFAULT;
328 goto err_core;
329 }
330
331 init_attr->attr.max_wr = srq->msrq.max - 1;
332
333 return &srq->ibsrq;
334
335err_core:
336 mlx5_core_destroy_srq(dev->mdev, &srq->msrq);
337
338err_usr_kern_srq:
339 if (pd->uobject)
340 destroy_srq_user(pd, srq);
341 else
342 destroy_srq_kernel(dev, srq);
343
344err_srq:
345 kfree(srq);
346
347 return ERR_PTR(err);
348}
349
350int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
351 enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
352{
353 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
354 struct mlx5_ib_srq *srq = to_msrq(ibsrq);
355 int ret;
356
357
358 if (attr_mask & IB_SRQ_MAX_WR)
359 return -EINVAL;
360
361 if (attr_mask & IB_SRQ_LIMIT) {
362 if (attr->srq_limit >= srq->msrq.max)
363 return -EINVAL;
364
365 mutex_lock(&srq->mutex);
366 ret = mlx5_core_arm_srq(dev->mdev, &srq->msrq, attr->srq_limit, 1);
367 mutex_unlock(&srq->mutex);
368
369 if (ret)
370 return ret;
371 }
372
373 return 0;
374}
375
376int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
377{
378 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
379 struct mlx5_ib_srq *srq = to_msrq(ibsrq);
380 int ret;
381 struct mlx5_srq_attr *out;
382
383 out = kzalloc(sizeof(*out), GFP_KERNEL);
384 if (!out)
385 return -ENOMEM;
386
387 ret = mlx5_core_query_srq(dev->mdev, &srq->msrq, out);
388 if (ret)
389 goto out_box;
390
391 srq_attr->srq_limit = out->lwm;
392 srq_attr->max_wr = srq->msrq.max - 1;
393 srq_attr->max_sge = srq->msrq.max_gs;
394
395out_box:
396 kfree(out);
397 return ret;
398}
399
400int mlx5_ib_destroy_srq(struct ib_srq *srq)
401{
402 struct mlx5_ib_dev *dev = to_mdev(srq->device);
403 struct mlx5_ib_srq *msrq = to_msrq(srq);
404
405 mlx5_core_destroy_srq(dev->mdev, &msrq->msrq);
406
407 if (srq->uobject) {
408 mlx5_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db);
409 ib_umem_release(msrq->umem);
410 } else {
411 destroy_srq_kernel(dev, msrq);
412 }
413
414 kfree(srq);
415 return 0;
416}
417
418void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index)
419{
420 struct mlx5_wqe_srq_next_seg *next;
421
422
423 spin_lock(&srq->lock);
424
425 next = get_wqe(srq, srq->tail);
426 next->next_wqe_index = cpu_to_be16(wqe_index);
427 srq->tail = wqe_index;
428
429 spin_unlock(&srq->lock);
430}
431
432int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
433 struct ib_recv_wr **bad_wr)
434{
435 struct mlx5_ib_srq *srq = to_msrq(ibsrq);
436 struct mlx5_wqe_srq_next_seg *next;
437 struct mlx5_wqe_data_seg *scat;
438 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
439 struct mlx5_core_dev *mdev = dev->mdev;
440 unsigned long flags;
441 int err = 0;
442 int nreq;
443 int i;
444
445 spin_lock_irqsave(&srq->lock, flags);
446
447 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
448 err = -EIO;
449 *bad_wr = wr;
450 goto out;
451 }
452
453 for (nreq = 0; wr; nreq++, wr = wr->next) {
454 if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
455 err = -EINVAL;
456 *bad_wr = wr;
457 break;
458 }
459
460 if (unlikely(srq->head == srq->tail)) {
461 err = -ENOMEM;
462 *bad_wr = wr;
463 break;
464 }
465
466 srq->wrid[srq->head] = wr->wr_id;
467
468 next = get_wqe(srq, srq->head);
469 srq->head = be16_to_cpu(next->next_wqe_index);
470 scat = (struct mlx5_wqe_data_seg *)(next + 1);
471
472 for (i = 0; i < wr->num_sge; i++) {
473 scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
474 scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
475 scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
476 }
477
478 if (i < srq->msrq.max_avail_gather) {
479 scat[i].byte_count = 0;
480 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
481 scat[i].addr = 0;
482 }
483 }
484
485 if (likely(nreq)) {
486 srq->wqe_ctr += nreq;
487
488
489
490
491 wmb();
492
493 *srq->db.db = cpu_to_be32(srq->wqe_ctr);
494 }
495out:
496 spin_unlock_irqrestore(&srq->lock, flags);
497
498 return err;
499}
500