blob: 546d3e9932071313d2f9e0c66d2d5555a5baf139 [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
2 * Copyright (c) 2013, Mellanox Technologies inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/module.h>
34#include <rdma/ib_umem.h>
35#include "mlx5_ib.h"
36#include "user.h"
37
38/* not supported currently */
39static int wq_signature;
40
41enum {
42 MLX5_IB_ACK_REQ_FREQ = 8,
43};
44
45enum {
46 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
47 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
48 MLX5_IB_LINK_TYPE_IB = 0,
49 MLX5_IB_LINK_TYPE_ETH = 1
50};
51
52enum {
53 MLX5_IB_SQ_STRIDE = 6,
54 MLX5_IB_CACHE_LINE_SIZE = 64,
55};
56
57static const u32 mlx5_ib_opcode[] = {
58 [IB_WR_SEND] = MLX5_OPCODE_SEND,
59 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
60 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
61 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
62 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
63 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
64 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
65 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
66 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
67 [IB_WR_FAST_REG_MR] = MLX5_OPCODE_UMR,
68 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
69 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
70 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
71};
72
73struct umr_wr {
74 u64 virt_addr;
75 struct ib_pd *pd;
76 unsigned int page_shift;
77 unsigned int npages;
78 u32 length;
79 int access_flags;
80 u32 mkey;
81};
82
83static int is_qp0(enum ib_qp_type qp_type)
84{
85 return qp_type == IB_QPT_SMI;
86}
87
88static int is_qp1(enum ib_qp_type qp_type)
89{
90 return qp_type == IB_QPT_GSI;
91}
92
93static int is_sqp(enum ib_qp_type qp_type)
94{
95 return is_qp0(qp_type) || is_qp1(qp_type);
96}
97
98static void *get_wqe(struct mlx5_ib_qp *qp, int offset)
99{
100 return mlx5_buf_offset(&qp->buf, offset);
101}
102
103static void *get_recv_wqe(struct mlx5_ib_qp *qp, int n)
104{
105 return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
106}
107
108void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n)
109{
110 return get_wqe(qp, qp->sq.offset + (n << MLX5_IB_SQ_STRIDE));
111}
112
113static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
114{
115 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
116 struct ib_event event;
117
118 if (type == MLX5_EVENT_TYPE_PATH_MIG)
119 to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
120
121 if (ibqp->event_handler) {
122 event.device = ibqp->device;
123 event.element.qp = ibqp;
124 switch (type) {
125 case MLX5_EVENT_TYPE_PATH_MIG:
126 event.event = IB_EVENT_PATH_MIG;
127 break;
128 case MLX5_EVENT_TYPE_COMM_EST:
129 event.event = IB_EVENT_COMM_EST;
130 break;
131 case MLX5_EVENT_TYPE_SQ_DRAINED:
132 event.event = IB_EVENT_SQ_DRAINED;
133 break;
134 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
135 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
136 break;
137 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
138 event.event = IB_EVENT_QP_FATAL;
139 break;
140 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
141 event.event = IB_EVENT_PATH_MIG_ERR;
142 break;
143 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
144 event.event = IB_EVENT_QP_REQ_ERR;
145 break;
146 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
147 event.event = IB_EVENT_QP_ACCESS_ERR;
148 break;
149 default:
150 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
151 return;
152 }
153
154 ibqp->event_handler(&event, ibqp->qp_context);
155 }
156}
157
158static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
159 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
160{
161 int wqe_size;
162 int wq_size;
163
164 /* Sanity check RQ size before proceeding */
165 if (cap->max_recv_wr > dev->mdev.caps.max_wqes)
166 return -EINVAL;
167
168 if (!has_rq) {
169 qp->rq.max_gs = 0;
170 qp->rq.wqe_cnt = 0;
171 qp->rq.wqe_shift = 0;
172 } else {
173 if (ucmd) {
174 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
175 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
176 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
177 qp->rq.max_post = qp->rq.wqe_cnt;
178 } else {
179 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
180 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
181 wqe_size = roundup_pow_of_two(wqe_size);
182 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
183 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
184 qp->rq.wqe_cnt = wq_size / wqe_size;
185 if (wqe_size > dev->mdev.caps.max_rq_desc_sz) {
186 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
187 wqe_size,
188 dev->mdev.caps.max_rq_desc_sz);
189 return -EINVAL;
190 }
191 qp->rq.wqe_shift = ilog2(wqe_size);
192 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
193 qp->rq.max_post = qp->rq.wqe_cnt;
194 }
195 }
196
197 return 0;
198}
199
200static int sq_overhead(enum ib_qp_type qp_type)
201{
Andi Shyti618af382013-07-16 15:35:01 +0200202 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300203
204 switch (qp_type) {
205 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300206 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300207 /* fall through */
208 case IB_QPT_RC:
209 size += sizeof(struct mlx5_wqe_ctrl_seg) +
210 sizeof(struct mlx5_wqe_atomic_seg) +
211 sizeof(struct mlx5_wqe_raddr_seg);
212 break;
213
Eli Cohenb125a542013-09-11 16:35:22 +0300214 case IB_QPT_XRC_TGT:
215 return 0;
216
Eli Cohene126ba92013-07-07 17:25:49 +0300217 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300218 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300219 sizeof(struct mlx5_wqe_raddr_seg);
220 break;
221
222 case IB_QPT_UD:
223 case IB_QPT_SMI:
224 case IB_QPT_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300225 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300226 sizeof(struct mlx5_wqe_datagram_seg);
227 break;
228
229 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300230 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300231 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
232 sizeof(struct mlx5_mkey_seg);
233 break;
234
235 default:
236 return -EINVAL;
237 }
238
239 return size;
240}
241
242static int calc_send_wqe(struct ib_qp_init_attr *attr)
243{
244 int inl_size = 0;
245 int size;
246
247 size = sq_overhead(attr->qp_type);
248 if (size < 0)
249 return size;
250
251 if (attr->cap.max_inline_data) {
252 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
253 attr->cap.max_inline_data;
254 }
255
256 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
257
258 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
259}
260
261static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
262 struct mlx5_ib_qp *qp)
263{
264 int wqe_size;
265 int wq_size;
266
267 if (!attr->cap.max_send_wr)
268 return 0;
269
270 wqe_size = calc_send_wqe(attr);
271 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
272 if (wqe_size < 0)
273 return wqe_size;
274
275 if (wqe_size > dev->mdev.caps.max_sq_desc_sz) {
Eli Cohenb125a542013-09-11 16:35:22 +0300276 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
277 wqe_size, dev->mdev.caps.max_sq_desc_sz);
Eli Cohene126ba92013-07-07 17:25:49 +0300278 return -EINVAL;
279 }
280
281 qp->max_inline_data = wqe_size - sq_overhead(attr->qp_type) -
282 sizeof(struct mlx5_wqe_inline_seg);
283 attr->cap.max_inline_data = qp->max_inline_data;
284
285 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
286 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Eli Cohenb125a542013-09-11 16:35:22 +0300287 if (qp->sq.wqe_cnt > dev->mdev.caps.max_wqes) {
288 mlx5_ib_dbg(dev, "wqe count(%d) exceeds limits(%d)\n",
289 qp->sq.wqe_cnt, dev->mdev.caps.max_wqes);
290 return -ENOMEM;
291 }
Eli Cohene126ba92013-07-07 17:25:49 +0300292 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
293 qp->sq.max_gs = attr->cap.max_send_sge;
Eli Cohenb125a542013-09-11 16:35:22 +0300294 qp->sq.max_post = wq_size / wqe_size;
295 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300296
297 return wq_size;
298}
299
300static int set_user_buf_size(struct mlx5_ib_dev *dev,
301 struct mlx5_ib_qp *qp,
302 struct mlx5_ib_create_qp *ucmd)
303{
304 int desc_sz = 1 << qp->sq.wqe_shift;
305
306 if (desc_sz > dev->mdev.caps.max_sq_desc_sz) {
307 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
308 desc_sz, dev->mdev.caps.max_sq_desc_sz);
309 return -EINVAL;
310 }
311
312 if (ucmd->sq_wqe_count && ((1 << ilog2(ucmd->sq_wqe_count)) != ucmd->sq_wqe_count)) {
313 mlx5_ib_warn(dev, "sq_wqe_count %d, sq_wqe_count %d\n",
314 ucmd->sq_wqe_count, ucmd->sq_wqe_count);
315 return -EINVAL;
316 }
317
318 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
319
320 if (qp->sq.wqe_cnt > dev->mdev.caps.max_wqes) {
321 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
322 qp->sq.wqe_cnt, dev->mdev.caps.max_wqes);
323 return -EINVAL;
324 }
325
326 qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
327 (qp->sq.wqe_cnt << 6);
328
329 return 0;
330}
331
332static int qp_has_rq(struct ib_qp_init_attr *attr)
333{
334 if (attr->qp_type == IB_QPT_XRC_INI ||
335 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
336 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
337 !attr->cap.max_recv_wr)
338 return 0;
339
340 return 1;
341}
342
343static int alloc_high_class_uuar(struct mlx5_uuar_info *uuari)
344{
345 int nuuars = uuari->num_uars * MLX5_BF_REGS_PER_PAGE;
346 int start_uuar;
347 int i;
348
349 start_uuar = nuuars - uuari->num_low_latency_uuars;
350 for (i = start_uuar; i < nuuars; i++) {
351 if (!test_bit(i, uuari->bitmap)) {
352 set_bit(i, uuari->bitmap);
353 uuari->count[i]++;
354 return i;
355 }
356 }
357
358 return -ENOMEM;
359}
360
361static int alloc_med_class_uuar(struct mlx5_uuar_info *uuari)
362{
363 int nuuars = uuari->num_uars * MLX5_BF_REGS_PER_PAGE;
364 int minidx = 1;
365 int uuarn;
366 int end;
367 int i;
368
369 end = nuuars - uuari->num_low_latency_uuars;
370
371 for (i = 1; i < end; i++) {
372 uuarn = i & 3;
373 if (uuarn == 2 || uuarn == 3)
374 continue;
375
376 if (uuari->count[i] < uuari->count[minidx])
377 minidx = i;
378 }
379
380 uuari->count[minidx]++;
381 return minidx;
382}
383
384static int alloc_uuar(struct mlx5_uuar_info *uuari,
385 enum mlx5_ib_latency_class lat)
386{
387 int uuarn = -EINVAL;
388
389 mutex_lock(&uuari->lock);
390 switch (lat) {
391 case MLX5_IB_LATENCY_CLASS_LOW:
392 uuarn = 0;
393 uuari->count[uuarn]++;
394 break;
395
396 case MLX5_IB_LATENCY_CLASS_MEDIUM:
397 uuarn = alloc_med_class_uuar(uuari);
398 break;
399
400 case MLX5_IB_LATENCY_CLASS_HIGH:
401 uuarn = alloc_high_class_uuar(uuari);
402 break;
403
404 case MLX5_IB_LATENCY_CLASS_FAST_PATH:
405 uuarn = 2;
406 break;
407 }
408 mutex_unlock(&uuari->lock);
409
410 return uuarn;
411}
412
413static void free_med_class_uuar(struct mlx5_uuar_info *uuari, int uuarn)
414{
415 clear_bit(uuarn, uuari->bitmap);
416 --uuari->count[uuarn];
417}
418
419static void free_high_class_uuar(struct mlx5_uuar_info *uuari, int uuarn)
420{
421 clear_bit(uuarn, uuari->bitmap);
422 --uuari->count[uuarn];
423}
424
425static void free_uuar(struct mlx5_uuar_info *uuari, int uuarn)
426{
427 int nuuars = uuari->num_uars * MLX5_BF_REGS_PER_PAGE;
428 int high_uuar = nuuars - uuari->num_low_latency_uuars;
429
430 mutex_lock(&uuari->lock);
431 if (uuarn == 0) {
432 --uuari->count[uuarn];
433 goto out;
434 }
435
436 if (uuarn < high_uuar) {
437 free_med_class_uuar(uuari, uuarn);
438 goto out;
439 }
440
441 free_high_class_uuar(uuari, uuarn);
442
443out:
444 mutex_unlock(&uuari->lock);
445}
446
447static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
448{
449 switch (state) {
450 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
451 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
452 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
453 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
454 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
455 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
456 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
457 default: return -1;
458 }
459}
460
461static int to_mlx5_st(enum ib_qp_type type)
462{
463 switch (type) {
464 case IB_QPT_RC: return MLX5_QP_ST_RC;
465 case IB_QPT_UC: return MLX5_QP_ST_UC;
466 case IB_QPT_UD: return MLX5_QP_ST_UD;
467 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
468 case IB_QPT_XRC_INI:
469 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
470 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
471 case IB_QPT_GSI: return MLX5_QP_ST_QP1;
472 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
473 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
474 case IB_QPT_RAW_PACKET:
475 case IB_QPT_MAX:
476 default: return -EINVAL;
477 }
478}
479
480static int uuarn_to_uar_index(struct mlx5_uuar_info *uuari, int uuarn)
481{
482 return uuari->uars[uuarn / MLX5_BF_REGS_PER_PAGE].index;
483}
484
485static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
486 struct mlx5_ib_qp *qp, struct ib_udata *udata,
487 struct mlx5_create_qp_mbox_in **in,
488 struct mlx5_ib_create_qp_resp *resp, int *inlen)
489{
490 struct mlx5_ib_ucontext *context;
491 struct mlx5_ib_create_qp ucmd;
492 int page_shift;
493 int uar_index;
494 int npages;
495 u32 offset;
496 int uuarn;
497 int ncont;
498 int err;
499
500 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
501 if (err) {
502 mlx5_ib_dbg(dev, "copy failed\n");
503 return err;
504 }
505
506 context = to_mucontext(pd->uobject->context);
507 /*
508 * TBD: should come from the verbs when we have the API
509 */
510 uuarn = alloc_uuar(&context->uuari, MLX5_IB_LATENCY_CLASS_HIGH);
511 if (uuarn < 0) {
512 mlx5_ib_dbg(dev, "failed to allocate low latency UUAR\n");
513 mlx5_ib_dbg(dev, "reverting to high latency\n");
514 uuarn = alloc_uuar(&context->uuari, MLX5_IB_LATENCY_CLASS_LOW);
515 if (uuarn < 0) {
516 mlx5_ib_dbg(dev, "uuar allocation failed\n");
517 return uuarn;
518 }
519 }
520
521 uar_index = uuarn_to_uar_index(&context->uuari, uuarn);
522 mlx5_ib_dbg(dev, "uuarn 0x%x, uar_index 0x%x\n", uuarn, uar_index);
523
524 err = set_user_buf_size(dev, qp, &ucmd);
525 if (err)
526 goto err_uuar;
527
528 qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
529 qp->buf_size, 0, 0);
530 if (IS_ERR(qp->umem)) {
531 mlx5_ib_dbg(dev, "umem_get failed\n");
532 err = PTR_ERR(qp->umem);
533 goto err_uuar;
534 }
535
536 mlx5_ib_cont_pages(qp->umem, ucmd.buf_addr, &npages, &page_shift,
537 &ncont, NULL);
538 err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift, &offset);
539 if (err) {
540 mlx5_ib_warn(dev, "bad offset\n");
541 goto err_umem;
542 }
543 mlx5_ib_dbg(dev, "addr 0x%llx, size %d, npages %d, page_shift %d, ncont %d, offset %d\n",
544 ucmd.buf_addr, qp->buf_size, npages, page_shift, ncont, offset);
545
546 *inlen = sizeof(**in) + sizeof(*(*in)->pas) * ncont;
547 *in = mlx5_vzalloc(*inlen);
548 if (!*in) {
549 err = -ENOMEM;
550 goto err_umem;
551 }
552 mlx5_ib_populate_pas(dev, qp->umem, page_shift, (*in)->pas, 0);
553 (*in)->ctx.log_pg_sz_remote_qpn =
554 cpu_to_be32((page_shift - PAGE_SHIFT) << 24);
555 (*in)->ctx.params2 = cpu_to_be32(offset << 6);
556
557 (*in)->ctx.qp_counter_set_usr_page = cpu_to_be32(uar_index);
558 resp->uuar_index = uuarn;
559 qp->uuarn = uuarn;
560
561 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
562 if (err) {
563 mlx5_ib_dbg(dev, "map failed\n");
564 goto err_free;
565 }
566
567 err = ib_copy_to_udata(udata, resp, sizeof(*resp));
568 if (err) {
569 mlx5_ib_dbg(dev, "copy failed\n");
570 goto err_unmap;
571 }
572 qp->create_type = MLX5_QP_USER;
573
574 return 0;
575
576err_unmap:
577 mlx5_ib_db_unmap_user(context, &qp->db);
578
579err_free:
580 mlx5_vfree(*in);
581
582err_umem:
583 ib_umem_release(qp->umem);
584
585err_uuar:
586 free_uuar(&context->uuari, uuarn);
587 return err;
588}
589
590static void destroy_qp_user(struct ib_pd *pd, struct mlx5_ib_qp *qp)
591{
592 struct mlx5_ib_ucontext *context;
593
594 context = to_mucontext(pd->uobject->context);
595 mlx5_ib_db_unmap_user(context, &qp->db);
596 ib_umem_release(qp->umem);
597 free_uuar(&context->uuari, qp->uuarn);
598}
599
600static int create_kernel_qp(struct mlx5_ib_dev *dev,
601 struct ib_qp_init_attr *init_attr,
602 struct mlx5_ib_qp *qp,
603 struct mlx5_create_qp_mbox_in **in, int *inlen)
604{
605 enum mlx5_ib_latency_class lc = MLX5_IB_LATENCY_CLASS_LOW;
606 struct mlx5_uuar_info *uuari;
607 int uar_index;
608 int uuarn;
609 int err;
610
611 uuari = &dev->mdev.priv.uuari;
612 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
613 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
614
615 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
616 lc = MLX5_IB_LATENCY_CLASS_FAST_PATH;
617
618 uuarn = alloc_uuar(uuari, lc);
619 if (uuarn < 0) {
620 mlx5_ib_dbg(dev, "\n");
621 return -ENOMEM;
622 }
623
624 qp->bf = &uuari->bfs[uuarn];
625 uar_index = qp->bf->uar->index;
626
627 err = calc_sq_size(dev, init_attr, qp);
628 if (err < 0) {
629 mlx5_ib_dbg(dev, "err %d\n", err);
630 goto err_uuar;
631 }
632
633 qp->rq.offset = 0;
634 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
635 qp->buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
636
637 err = mlx5_buf_alloc(&dev->mdev, qp->buf_size, PAGE_SIZE * 2, &qp->buf);
638 if (err) {
639 mlx5_ib_dbg(dev, "err %d\n", err);
640 goto err_uuar;
641 }
642
643 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
644 *inlen = sizeof(**in) + sizeof(*(*in)->pas) * qp->buf.npages;
645 *in = mlx5_vzalloc(*inlen);
646 if (!*in) {
647 err = -ENOMEM;
648 goto err_buf;
649 }
650 (*in)->ctx.qp_counter_set_usr_page = cpu_to_be32(uar_index);
651 (*in)->ctx.log_pg_sz_remote_qpn = cpu_to_be32((qp->buf.page_shift - PAGE_SHIFT) << 24);
652 /* Set "fast registration enabled" for all kernel QPs */
653 (*in)->ctx.params1 |= cpu_to_be32(1 << 11);
654 (*in)->ctx.sq_crq_size |= cpu_to_be16(1 << 4);
655
656 mlx5_fill_page_array(&qp->buf, (*in)->pas);
657
658 err = mlx5_db_alloc(&dev->mdev, &qp->db);
659 if (err) {
660 mlx5_ib_dbg(dev, "err %d\n", err);
661 goto err_free;
662 }
663
664 qp->db.db[0] = 0;
665 qp->db.db[1] = 0;
666
667 qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wrid), GFP_KERNEL);
668 qp->sq.wr_data = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wr_data), GFP_KERNEL);
669 qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof(*qp->rq.wrid), GFP_KERNEL);
670 qp->sq.w_list = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.w_list), GFP_KERNEL);
671 qp->sq.wqe_head = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wqe_head), GFP_KERNEL);
672
673 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
674 !qp->sq.w_list || !qp->sq.wqe_head) {
675 err = -ENOMEM;
676 goto err_wrid;
677 }
678 qp->create_type = MLX5_QP_KERNEL;
679
680 return 0;
681
682err_wrid:
683 mlx5_db_free(&dev->mdev, &qp->db);
684 kfree(qp->sq.wqe_head);
685 kfree(qp->sq.w_list);
686 kfree(qp->sq.wrid);
687 kfree(qp->sq.wr_data);
688 kfree(qp->rq.wrid);
689
690err_free:
691 mlx5_vfree(*in);
692
693err_buf:
694 mlx5_buf_free(&dev->mdev, &qp->buf);
695
696err_uuar:
697 free_uuar(&dev->mdev.priv.uuari, uuarn);
698 return err;
699}
700
701static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
702{
703 mlx5_db_free(&dev->mdev, &qp->db);
704 kfree(qp->sq.wqe_head);
705 kfree(qp->sq.w_list);
706 kfree(qp->sq.wrid);
707 kfree(qp->sq.wr_data);
708 kfree(qp->rq.wrid);
709 mlx5_buf_free(&dev->mdev, &qp->buf);
710 free_uuar(&dev->mdev.priv.uuari, qp->bf->uuarn);
711}
712
713static __be32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
714{
715 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
716 (attr->qp_type == IB_QPT_XRC_INI))
717 return cpu_to_be32(MLX5_SRQ_RQ);
718 else if (!qp->has_rq)
719 return cpu_to_be32(MLX5_ZERO_LEN_RQ);
720 else
721 return cpu_to_be32(MLX5_NON_ZERO_RQ);
722}
723
724static int is_connected(enum ib_qp_type qp_type)
725{
726 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
727 return 1;
728
729 return 0;
730}
731
732static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
733 struct ib_qp_init_attr *init_attr,
734 struct ib_udata *udata, struct mlx5_ib_qp *qp)
735{
736 struct mlx5_ib_resources *devr = &dev->devr;
737 struct mlx5_ib_create_qp_resp resp;
738 struct mlx5_create_qp_mbox_in *in;
739 struct mlx5_ib_create_qp ucmd;
740 int inlen = sizeof(*in);
741 int err;
742
743 mutex_init(&qp->mutex);
744 spin_lock_init(&qp->sq.lock);
745 spin_lock_init(&qp->rq.lock);
746
747 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
748 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
749
750 if (pd && pd->uobject) {
751 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
752 mlx5_ib_dbg(dev, "copy failed\n");
753 return -EFAULT;
754 }
755
756 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
757 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
758 } else {
759 qp->wq_sig = !!wq_signature;
760 }
761
762 qp->has_rq = qp_has_rq(init_attr);
763 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
764 qp, (pd && pd->uobject) ? &ucmd : NULL);
765 if (err) {
766 mlx5_ib_dbg(dev, "err %d\n", err);
767 return err;
768 }
769
770 if (pd) {
771 if (pd->uobject) {
772 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
773 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
774 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
775 mlx5_ib_dbg(dev, "invalid rq params\n");
776 return -EINVAL;
777 }
778 if (ucmd.sq_wqe_count > dev->mdev.caps.max_wqes) {
779 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
780 ucmd.sq_wqe_count, dev->mdev.caps.max_wqes);
781 return -EINVAL;
782 }
783 err = create_user_qp(dev, pd, qp, udata, &in, &resp, &inlen);
784 if (err)
785 mlx5_ib_dbg(dev, "err %d\n", err);
786 } else {
787 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen);
788 if (err)
789 mlx5_ib_dbg(dev, "err %d\n", err);
790 else
791 qp->pa_lkey = to_mpd(pd)->pa_lkey;
792 }
793
794 if (err)
795 return err;
796 } else {
797 in = mlx5_vzalloc(sizeof(*in));
798 if (!in)
799 return -ENOMEM;
800
801 qp->create_type = MLX5_QP_EMPTY;
802 }
803
804 if (is_sqp(init_attr->qp_type))
805 qp->port = init_attr->port_num;
806
807 in->ctx.flags = cpu_to_be32(to_mlx5_st(init_attr->qp_type) << 16 |
808 MLX5_QP_PM_MIGRATED << 11);
809
810 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
811 in->ctx.flags_pd = cpu_to_be32(to_mpd(pd ? pd : devr->p0)->pdn);
812 else
813 in->ctx.flags_pd = cpu_to_be32(MLX5_QP_LAT_SENSITIVE);
814
815 if (qp->wq_sig)
816 in->ctx.flags_pd |= cpu_to_be32(MLX5_QP_ENABLE_SIG);
817
818 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
819 int rcqe_sz;
820 int scqe_sz;
821
822 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
823 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
824
825 if (rcqe_sz == 128)
826 in->ctx.cs_res = MLX5_RES_SCAT_DATA64_CQE;
827 else
828 in->ctx.cs_res = MLX5_RES_SCAT_DATA32_CQE;
829
830 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
831 if (scqe_sz == 128)
832 in->ctx.cs_req = MLX5_REQ_SCAT_DATA64_CQE;
833 else
834 in->ctx.cs_req = MLX5_REQ_SCAT_DATA32_CQE;
835 }
836 }
837
838 if (qp->rq.wqe_cnt) {
839 in->ctx.rq_size_stride = (qp->rq.wqe_shift - 4);
840 in->ctx.rq_size_stride |= ilog2(qp->rq.wqe_cnt) << 3;
841 }
842
843 in->ctx.rq_type_srqn = get_rx_type(qp, init_attr);
844
845 if (qp->sq.wqe_cnt)
846 in->ctx.sq_crq_size |= cpu_to_be16(ilog2(qp->sq.wqe_cnt) << 11);
847 else
848 in->ctx.sq_crq_size |= cpu_to_be16(0x8000);
849
850 /* Set default resources */
851 switch (init_attr->qp_type) {
852 case IB_QPT_XRC_TGT:
853 in->ctx.cqn_recv = cpu_to_be32(to_mcq(devr->c0)->mcq.cqn);
854 in->ctx.cqn_send = cpu_to_be32(to_mcq(devr->c0)->mcq.cqn);
855 in->ctx.rq_type_srqn |= cpu_to_be32(to_msrq(devr->s0)->msrq.srqn);
856 in->ctx.xrcd = cpu_to_be32(to_mxrcd(init_attr->xrcd)->xrcdn);
857 break;
858 case IB_QPT_XRC_INI:
859 in->ctx.cqn_recv = cpu_to_be32(to_mcq(devr->c0)->mcq.cqn);
860 in->ctx.xrcd = cpu_to_be32(to_mxrcd(devr->x1)->xrcdn);
861 in->ctx.rq_type_srqn |= cpu_to_be32(to_msrq(devr->s0)->msrq.srqn);
862 break;
863 default:
864 if (init_attr->srq) {
865 in->ctx.xrcd = cpu_to_be32(to_mxrcd(devr->x0)->xrcdn);
866 in->ctx.rq_type_srqn |= cpu_to_be32(to_msrq(init_attr->srq)->msrq.srqn);
867 } else {
868 in->ctx.xrcd = cpu_to_be32(to_mxrcd(devr->x1)->xrcdn);
869 in->ctx.rq_type_srqn |= cpu_to_be32(to_msrq(devr->s0)->msrq.srqn);
870 }
871 }
872
873 if (init_attr->send_cq)
874 in->ctx.cqn_send = cpu_to_be32(to_mcq(init_attr->send_cq)->mcq.cqn);
875
876 if (init_attr->recv_cq)
877 in->ctx.cqn_recv = cpu_to_be32(to_mcq(init_attr->recv_cq)->mcq.cqn);
878
879 in->ctx.db_rec_addr = cpu_to_be64(qp->db.dma);
880
881 err = mlx5_core_create_qp(&dev->mdev, &qp->mqp, in, inlen);
882 if (err) {
883 mlx5_ib_dbg(dev, "create qp failed\n");
884 goto err_create;
885 }
886
887 mlx5_vfree(in);
888 /* Hardware wants QPN written in big-endian order (after
889 * shifting) for send doorbell. Precompute this value to save
890 * a little bit when posting sends.
891 */
892 qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
893
894 qp->mqp.event = mlx5_ib_qp_event;
895
896 return 0;
897
898err_create:
899 if (qp->create_type == MLX5_QP_USER)
900 destroy_qp_user(pd, qp);
901 else if (qp->create_type == MLX5_QP_KERNEL)
902 destroy_qp_kernel(dev, qp);
903
904 mlx5_vfree(in);
905 return err;
906}
907
908static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
909 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
910{
911 if (send_cq) {
912 if (recv_cq) {
913 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
914 spin_lock_irq(&send_cq->lock);
915 spin_lock_nested(&recv_cq->lock,
916 SINGLE_DEPTH_NESTING);
917 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
918 spin_lock_irq(&send_cq->lock);
919 __acquire(&recv_cq->lock);
920 } else {
921 spin_lock_irq(&recv_cq->lock);
922 spin_lock_nested(&send_cq->lock,
923 SINGLE_DEPTH_NESTING);
924 }
925 } else {
926 spin_lock_irq(&send_cq->lock);
927 }
928 } else if (recv_cq) {
929 spin_lock_irq(&recv_cq->lock);
930 }
931}
932
933static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
934 __releases(&send_cq->lock) __releases(&recv_cq->lock)
935{
936 if (send_cq) {
937 if (recv_cq) {
938 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
939 spin_unlock(&recv_cq->lock);
940 spin_unlock_irq(&send_cq->lock);
941 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
942 __release(&recv_cq->lock);
943 spin_unlock_irq(&send_cq->lock);
944 } else {
945 spin_unlock(&send_cq->lock);
946 spin_unlock_irq(&recv_cq->lock);
947 }
948 } else {
949 spin_unlock_irq(&send_cq->lock);
950 }
951 } else if (recv_cq) {
952 spin_unlock_irq(&recv_cq->lock);
953 }
954}
955
956static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
957{
958 return to_mpd(qp->ibqp.pd);
959}
960
961static void get_cqs(struct mlx5_ib_qp *qp,
962 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
963{
964 switch (qp->ibqp.qp_type) {
965 case IB_QPT_XRC_TGT:
966 *send_cq = NULL;
967 *recv_cq = NULL;
968 break;
969 case MLX5_IB_QPT_REG_UMR:
970 case IB_QPT_XRC_INI:
971 *send_cq = to_mcq(qp->ibqp.send_cq);
972 *recv_cq = NULL;
973 break;
974
975 case IB_QPT_SMI:
976 case IB_QPT_GSI:
977 case IB_QPT_RC:
978 case IB_QPT_UC:
979 case IB_QPT_UD:
980 case IB_QPT_RAW_IPV6:
981 case IB_QPT_RAW_ETHERTYPE:
982 *send_cq = to_mcq(qp->ibqp.send_cq);
983 *recv_cq = to_mcq(qp->ibqp.recv_cq);
984 break;
985
986 case IB_QPT_RAW_PACKET:
987 case IB_QPT_MAX:
988 default:
989 *send_cq = NULL;
990 *recv_cq = NULL;
991 break;
992 }
993}
994
995static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
996{
997 struct mlx5_ib_cq *send_cq, *recv_cq;
998 struct mlx5_modify_qp_mbox_in *in;
999 int err;
1000
1001 in = kzalloc(sizeof(*in), GFP_KERNEL);
1002 if (!in)
1003 return;
1004 if (qp->state != IB_QPS_RESET)
1005 if (mlx5_core_qp_modify(&dev->mdev, to_mlx5_state(qp->state),
1006 MLX5_QP_STATE_RST, in, sizeof(*in), &qp->mqp))
1007 mlx5_ib_warn(dev, "mlx5_ib: modify QP %06x to RESET failed\n",
1008 qp->mqp.qpn);
1009
1010 get_cqs(qp, &send_cq, &recv_cq);
1011
1012 if (qp->create_type == MLX5_QP_KERNEL) {
1013 mlx5_ib_lock_cqs(send_cq, recv_cq);
1014 __mlx5_ib_cq_clean(recv_cq, qp->mqp.qpn,
1015 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
1016 if (send_cq != recv_cq)
1017 __mlx5_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
1018 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1019 }
1020
1021 err = mlx5_core_destroy_qp(&dev->mdev, &qp->mqp);
1022 if (err)
1023 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n", qp->mqp.qpn);
1024 kfree(in);
1025
1026
1027 if (qp->create_type == MLX5_QP_KERNEL)
1028 destroy_qp_kernel(dev, qp);
1029 else if (qp->create_type == MLX5_QP_USER)
1030 destroy_qp_user(&get_pd(qp)->ibpd, qp);
1031}
1032
1033static const char *ib_qp_type_str(enum ib_qp_type type)
1034{
1035 switch (type) {
1036 case IB_QPT_SMI:
1037 return "IB_QPT_SMI";
1038 case IB_QPT_GSI:
1039 return "IB_QPT_GSI";
1040 case IB_QPT_RC:
1041 return "IB_QPT_RC";
1042 case IB_QPT_UC:
1043 return "IB_QPT_UC";
1044 case IB_QPT_UD:
1045 return "IB_QPT_UD";
1046 case IB_QPT_RAW_IPV6:
1047 return "IB_QPT_RAW_IPV6";
1048 case IB_QPT_RAW_ETHERTYPE:
1049 return "IB_QPT_RAW_ETHERTYPE";
1050 case IB_QPT_XRC_INI:
1051 return "IB_QPT_XRC_INI";
1052 case IB_QPT_XRC_TGT:
1053 return "IB_QPT_XRC_TGT";
1054 case IB_QPT_RAW_PACKET:
1055 return "IB_QPT_RAW_PACKET";
1056 case MLX5_IB_QPT_REG_UMR:
1057 return "MLX5_IB_QPT_REG_UMR";
1058 case IB_QPT_MAX:
1059 default:
1060 return "Invalid QP type";
1061 }
1062}
1063
1064struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
1065 struct ib_qp_init_attr *init_attr,
1066 struct ib_udata *udata)
1067{
1068 struct mlx5_ib_dev *dev;
1069 struct mlx5_ib_qp *qp;
1070 u16 xrcdn = 0;
1071 int err;
1072
1073 if (pd) {
1074 dev = to_mdev(pd->device);
1075 } else {
1076 /* being cautious here */
1077 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
1078 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
1079 pr_warn("%s: no PD for transport %s\n", __func__,
1080 ib_qp_type_str(init_attr->qp_type));
1081 return ERR_PTR(-EINVAL);
1082 }
1083 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
1084 }
1085
1086 switch (init_attr->qp_type) {
1087 case IB_QPT_XRC_TGT:
1088 case IB_QPT_XRC_INI:
1089 if (!(dev->mdev.caps.flags & MLX5_DEV_CAP_FLAG_XRC)) {
1090 mlx5_ib_dbg(dev, "XRC not supported\n");
1091 return ERR_PTR(-ENOSYS);
1092 }
1093 init_attr->recv_cq = NULL;
1094 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
1095 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
1096 init_attr->send_cq = NULL;
1097 }
1098
1099 /* fall through */
1100 case IB_QPT_RC:
1101 case IB_QPT_UC:
1102 case IB_QPT_UD:
1103 case IB_QPT_SMI:
1104 case IB_QPT_GSI:
1105 case MLX5_IB_QPT_REG_UMR:
1106 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1107 if (!qp)
1108 return ERR_PTR(-ENOMEM);
1109
1110 err = create_qp_common(dev, pd, init_attr, udata, qp);
1111 if (err) {
1112 mlx5_ib_dbg(dev, "create_qp_common failed\n");
1113 kfree(qp);
1114 return ERR_PTR(err);
1115 }
1116
1117 if (is_qp0(init_attr->qp_type))
1118 qp->ibqp.qp_num = 0;
1119 else if (is_qp1(init_attr->qp_type))
1120 qp->ibqp.qp_num = 1;
1121 else
1122 qp->ibqp.qp_num = qp->mqp.qpn;
1123
1124 mlx5_ib_dbg(dev, "ib qpnum 0x%x, mlx qpn 0x%x, rcqn 0x%x, scqn 0x%x\n",
1125 qp->ibqp.qp_num, qp->mqp.qpn, to_mcq(init_attr->recv_cq)->mcq.cqn,
1126 to_mcq(init_attr->send_cq)->mcq.cqn);
1127
1128 qp->xrcdn = xrcdn;
1129
1130 break;
1131
1132 case IB_QPT_RAW_IPV6:
1133 case IB_QPT_RAW_ETHERTYPE:
1134 case IB_QPT_RAW_PACKET:
1135 case IB_QPT_MAX:
1136 default:
1137 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
1138 init_attr->qp_type);
1139 /* Don't support raw QPs */
1140 return ERR_PTR(-EINVAL);
1141 }
1142
1143 return &qp->ibqp;
1144}
1145
1146int mlx5_ib_destroy_qp(struct ib_qp *qp)
1147{
1148 struct mlx5_ib_dev *dev = to_mdev(qp->device);
1149 struct mlx5_ib_qp *mqp = to_mqp(qp);
1150
1151 destroy_qp_common(dev, mqp);
1152
1153 kfree(mqp);
1154
1155 return 0;
1156}
1157
1158static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
1159 int attr_mask)
1160{
1161 u32 hw_access_flags = 0;
1162 u8 dest_rd_atomic;
1163 u32 access_flags;
1164
1165 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1166 dest_rd_atomic = attr->max_dest_rd_atomic;
1167 else
1168 dest_rd_atomic = qp->resp_depth;
1169
1170 if (attr_mask & IB_QP_ACCESS_FLAGS)
1171 access_flags = attr->qp_access_flags;
1172 else
1173 access_flags = qp->atomic_rd_en;
1174
1175 if (!dest_rd_atomic)
1176 access_flags &= IB_ACCESS_REMOTE_WRITE;
1177
1178 if (access_flags & IB_ACCESS_REMOTE_READ)
1179 hw_access_flags |= MLX5_QP_BIT_RRE;
1180 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
1181 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
1182 if (access_flags & IB_ACCESS_REMOTE_WRITE)
1183 hw_access_flags |= MLX5_QP_BIT_RWE;
1184
1185 return cpu_to_be32(hw_access_flags);
1186}
1187
1188enum {
1189 MLX5_PATH_FLAG_FL = 1 << 0,
1190 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
1191 MLX5_PATH_FLAG_COUNTER = 1 << 2,
1192};
1193
1194static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
1195{
1196 if (rate == IB_RATE_PORT_CURRENT) {
1197 return 0;
1198 } else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
1199 return -EINVAL;
1200 } else {
1201 while (rate != IB_RATE_2_5_GBPS &&
1202 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
1203 dev->mdev.caps.stat_rate_support))
1204 --rate;
1205 }
1206
1207 return rate + MLX5_STAT_RATE_OFFSET;
1208}
1209
1210static int mlx5_set_path(struct mlx5_ib_dev *dev, const struct ib_ah_attr *ah,
1211 struct mlx5_qp_path *path, u8 port, int attr_mask,
1212 u32 path_flags, const struct ib_qp_attr *attr)
1213{
1214 int err;
1215
1216 path->fl = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
1217 path->free_ar = (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x80 : 0;
1218
1219 if (attr_mask & IB_QP_PKEY_INDEX)
1220 path->pkey_index = attr->pkey_index;
1221
1222 path->grh_mlid = ah->src_path_bits & 0x7f;
1223 path->rlid = cpu_to_be16(ah->dlid);
1224
1225 if (ah->ah_flags & IB_AH_GRH) {
1226 path->grh_mlid |= 1 << 7;
1227 path->mgid_index = ah->grh.sgid_index;
1228 path->hop_limit = ah->grh.hop_limit;
1229 path->tclass_flowlabel =
1230 cpu_to_be32((ah->grh.traffic_class << 20) |
1231 (ah->grh.flow_label));
1232 memcpy(path->rgid, ah->grh.dgid.raw, 16);
1233 }
1234
1235 err = ib_rate_to_mlx5(dev, ah->static_rate);
1236 if (err < 0)
1237 return err;
1238 path->static_rate = err;
1239 path->port = port;
1240
1241 if (ah->ah_flags & IB_AH_GRH) {
1242 if (ah->grh.sgid_index >= dev->mdev.caps.port[port - 1].gid_table_len) {
1243 pr_err(KERN_ERR "sgid_index (%u) too large. max is %d\n",
1244 ah->grh.sgid_index, dev->mdev.caps.port[port - 1].gid_table_len);
1245 return -EINVAL;
1246 }
1247
1248 path->grh_mlid |= 1 << 7;
1249 path->mgid_index = ah->grh.sgid_index;
1250 path->hop_limit = ah->grh.hop_limit;
1251 path->tclass_flowlabel =
1252 cpu_to_be32((ah->grh.traffic_class << 20) |
1253 (ah->grh.flow_label));
1254 memcpy(path->rgid, ah->grh.dgid.raw, 16);
1255 }
1256
1257 if (attr_mask & IB_QP_TIMEOUT)
1258 path->ackto_lt = attr->timeout << 3;
1259
1260 path->sl = ah->sl & 0xf;
1261
1262 return 0;
1263}
1264
1265static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
1266 [MLX5_QP_STATE_INIT] = {
1267 [MLX5_QP_STATE_INIT] = {
1268 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
1269 MLX5_QP_OPTPAR_RAE |
1270 MLX5_QP_OPTPAR_RWE |
1271 MLX5_QP_OPTPAR_PKEY_INDEX |
1272 MLX5_QP_OPTPAR_PRI_PORT,
1273 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
1274 MLX5_QP_OPTPAR_PKEY_INDEX |
1275 MLX5_QP_OPTPAR_PRI_PORT,
1276 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
1277 MLX5_QP_OPTPAR_Q_KEY |
1278 MLX5_QP_OPTPAR_PRI_PORT,
1279 },
1280 [MLX5_QP_STATE_RTR] = {
1281 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
1282 MLX5_QP_OPTPAR_RRE |
1283 MLX5_QP_OPTPAR_RAE |
1284 MLX5_QP_OPTPAR_RWE |
1285 MLX5_QP_OPTPAR_PKEY_INDEX,
1286 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
1287 MLX5_QP_OPTPAR_RWE |
1288 MLX5_QP_OPTPAR_PKEY_INDEX,
1289 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
1290 MLX5_QP_OPTPAR_Q_KEY,
1291 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
1292 MLX5_QP_OPTPAR_Q_KEY,
1293 },
1294 },
1295 [MLX5_QP_STATE_RTR] = {
1296 [MLX5_QP_STATE_RTS] = {
1297 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
1298 MLX5_QP_OPTPAR_RRE |
1299 MLX5_QP_OPTPAR_RAE |
1300 MLX5_QP_OPTPAR_RWE |
1301 MLX5_QP_OPTPAR_PM_STATE |
1302 MLX5_QP_OPTPAR_RNR_TIMEOUT,
1303 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
1304 MLX5_QP_OPTPAR_RWE |
1305 MLX5_QP_OPTPAR_PM_STATE,
1306 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
1307 },
1308 },
1309 [MLX5_QP_STATE_RTS] = {
1310 [MLX5_QP_STATE_RTS] = {
1311 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
1312 MLX5_QP_OPTPAR_RAE |
1313 MLX5_QP_OPTPAR_RWE |
1314 MLX5_QP_OPTPAR_RNR_TIMEOUT |
1315 MLX5_QP_OPTPAR_PM_STATE,
1316 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
1317 MLX5_QP_OPTPAR_PM_STATE,
1318 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
1319 MLX5_QP_OPTPAR_SRQN |
1320 MLX5_QP_OPTPAR_CQN_RCV,
1321 },
1322 },
1323 [MLX5_QP_STATE_SQER] = {
1324 [MLX5_QP_STATE_RTS] = {
1325 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
1326 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03001327 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohene126ba92013-07-07 17:25:49 +03001328 },
1329 },
1330};
1331
1332static int ib_nr_to_mlx5_nr(int ib_mask)
1333{
1334 switch (ib_mask) {
1335 case IB_QP_STATE:
1336 return 0;
1337 case IB_QP_CUR_STATE:
1338 return 0;
1339 case IB_QP_EN_SQD_ASYNC_NOTIFY:
1340 return 0;
1341 case IB_QP_ACCESS_FLAGS:
1342 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
1343 MLX5_QP_OPTPAR_RAE;
1344 case IB_QP_PKEY_INDEX:
1345 return MLX5_QP_OPTPAR_PKEY_INDEX;
1346 case IB_QP_PORT:
1347 return MLX5_QP_OPTPAR_PRI_PORT;
1348 case IB_QP_QKEY:
1349 return MLX5_QP_OPTPAR_Q_KEY;
1350 case IB_QP_AV:
1351 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
1352 MLX5_QP_OPTPAR_PRI_PORT;
1353 case IB_QP_PATH_MTU:
1354 return 0;
1355 case IB_QP_TIMEOUT:
1356 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
1357 case IB_QP_RETRY_CNT:
1358 return MLX5_QP_OPTPAR_RETRY_COUNT;
1359 case IB_QP_RNR_RETRY:
1360 return MLX5_QP_OPTPAR_RNR_RETRY;
1361 case IB_QP_RQ_PSN:
1362 return 0;
1363 case IB_QP_MAX_QP_RD_ATOMIC:
1364 return MLX5_QP_OPTPAR_SRA_MAX;
1365 case IB_QP_ALT_PATH:
1366 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
1367 case IB_QP_MIN_RNR_TIMER:
1368 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
1369 case IB_QP_SQ_PSN:
1370 return 0;
1371 case IB_QP_MAX_DEST_RD_ATOMIC:
1372 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
1373 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
1374 case IB_QP_PATH_MIG_STATE:
1375 return MLX5_QP_OPTPAR_PM_STATE;
1376 case IB_QP_CAP:
1377 return 0;
1378 case IB_QP_DEST_QPN:
1379 return 0;
1380 }
1381 return 0;
1382}
1383
1384static int ib_mask_to_mlx5_opt(int ib_mask)
1385{
1386 int result = 0;
1387 int i;
1388
1389 for (i = 0; i < 8 * sizeof(int); i++) {
1390 if ((1 << i) & ib_mask)
1391 result |= ib_nr_to_mlx5_nr(1 << i);
1392 }
1393
1394 return result;
1395}
1396
1397static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
1398 const struct ib_qp_attr *attr, int attr_mask,
1399 enum ib_qp_state cur_state, enum ib_qp_state new_state)
1400{
1401 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1402 struct mlx5_ib_qp *qp = to_mqp(ibqp);
1403 struct mlx5_ib_cq *send_cq, *recv_cq;
1404 struct mlx5_qp_context *context;
1405 struct mlx5_modify_qp_mbox_in *in;
1406 struct mlx5_ib_pd *pd;
1407 enum mlx5_qp_state mlx5_cur, mlx5_new;
1408 enum mlx5_qp_optpar optpar;
1409 int sqd_event;
1410 int mlx5_st;
1411 int err;
1412
1413 in = kzalloc(sizeof(*in), GFP_KERNEL);
1414 if (!in)
1415 return -ENOMEM;
1416
1417 context = &in->ctx;
1418 err = to_mlx5_st(ibqp->qp_type);
1419 if (err < 0)
1420 goto out;
1421
1422 context->flags = cpu_to_be32(err << 16);
1423
1424 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
1425 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
1426 } else {
1427 switch (attr->path_mig_state) {
1428 case IB_MIG_MIGRATED:
1429 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
1430 break;
1431 case IB_MIG_REARM:
1432 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
1433 break;
1434 case IB_MIG_ARMED:
1435 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
1436 break;
1437 }
1438 }
1439
1440 if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI) {
1441 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
1442 } else if (ibqp->qp_type == IB_QPT_UD ||
1443 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
1444 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
1445 } else if (attr_mask & IB_QP_PATH_MTU) {
1446 if (attr->path_mtu < IB_MTU_256 ||
1447 attr->path_mtu > IB_MTU_4096) {
1448 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
1449 err = -EINVAL;
1450 goto out;
1451 }
1452 context->mtu_msgmax = (attr->path_mtu << 5) | dev->mdev.caps.log_max_msg;
1453 }
1454
1455 if (attr_mask & IB_QP_DEST_QPN)
1456 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
1457
1458 if (attr_mask & IB_QP_PKEY_INDEX)
1459 context->pri_path.pkey_index = attr->pkey_index;
1460
1461 /* todo implement counter_index functionality */
1462
1463 if (is_sqp(ibqp->qp_type))
1464 context->pri_path.port = qp->port;
1465
1466 if (attr_mask & IB_QP_PORT)
1467 context->pri_path.port = attr->port_num;
1468
1469 if (attr_mask & IB_QP_AV) {
1470 err = mlx5_set_path(dev, &attr->ah_attr, &context->pri_path,
1471 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
1472 attr_mask, 0, attr);
1473 if (err)
1474 goto out;
1475 }
1476
1477 if (attr_mask & IB_QP_TIMEOUT)
1478 context->pri_path.ackto_lt |= attr->timeout << 3;
1479
1480 if (attr_mask & IB_QP_ALT_PATH) {
1481 err = mlx5_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
1482 attr->alt_port_num, attr_mask, 0, attr);
1483 if (err)
1484 goto out;
1485 }
1486
1487 pd = get_pd(qp);
1488 get_cqs(qp, &send_cq, &recv_cq);
1489
1490 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
1491 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
1492 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
1493 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
1494
1495 if (attr_mask & IB_QP_RNR_RETRY)
1496 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
1497
1498 if (attr_mask & IB_QP_RETRY_CNT)
1499 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
1500
1501 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
1502 if (attr->max_rd_atomic)
1503 context->params1 |=
1504 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
1505 }
1506
1507 if (attr_mask & IB_QP_SQ_PSN)
1508 context->next_send_psn = cpu_to_be32(attr->sq_psn);
1509
1510 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
1511 if (attr->max_dest_rd_atomic)
1512 context->params2 |=
1513 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
1514 }
1515
1516 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
1517 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
1518
1519 if (attr_mask & IB_QP_MIN_RNR_TIMER)
1520 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
1521
1522 if (attr_mask & IB_QP_RQ_PSN)
1523 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
1524
1525 if (attr_mask & IB_QP_QKEY)
1526 context->qkey = cpu_to_be32(attr->qkey);
1527
1528 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1529 context->db_rec_addr = cpu_to_be64(qp->db.dma);
1530
1531 if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
1532 attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
1533 sqd_event = 1;
1534 else
1535 sqd_event = 0;
1536
1537 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1538 context->sq_crq_size |= cpu_to_be16(1 << 4);
1539
1540
1541 mlx5_cur = to_mlx5_state(cur_state);
1542 mlx5_new = to_mlx5_state(new_state);
1543 mlx5_st = to_mlx5_st(ibqp->qp_type);
1544 if (mlx5_cur < 0 || mlx5_new < 0 || mlx5_st < 0)
1545 goto out;
1546
1547 optpar = ib_mask_to_mlx5_opt(attr_mask);
1548 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
1549 in->optparam = cpu_to_be32(optpar);
1550 err = mlx5_core_qp_modify(&dev->mdev, to_mlx5_state(cur_state),
1551 to_mlx5_state(new_state), in, sqd_event,
1552 &qp->mqp);
1553 if (err)
1554 goto out;
1555
1556 qp->state = new_state;
1557
1558 if (attr_mask & IB_QP_ACCESS_FLAGS)
1559 qp->atomic_rd_en = attr->qp_access_flags;
1560 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1561 qp->resp_depth = attr->max_dest_rd_atomic;
1562 if (attr_mask & IB_QP_PORT)
1563 qp->port = attr->port_num;
1564 if (attr_mask & IB_QP_ALT_PATH)
1565 qp->alt_port = attr->alt_port_num;
1566
1567 /*
1568 * If we moved a kernel QP to RESET, clean up all old CQ
1569 * entries and reinitialize the QP.
1570 */
1571 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
1572 mlx5_ib_cq_clean(recv_cq, qp->mqp.qpn,
1573 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
1574 if (send_cq != recv_cq)
1575 mlx5_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
1576
1577 qp->rq.head = 0;
1578 qp->rq.tail = 0;
1579 qp->sq.head = 0;
1580 qp->sq.tail = 0;
1581 qp->sq.cur_post = 0;
1582 qp->sq.last_poll = 0;
1583 qp->db.db[MLX5_RCV_DBR] = 0;
1584 qp->db.db[MLX5_SND_DBR] = 0;
1585 }
1586
1587out:
1588 kfree(in);
1589 return err;
1590}
1591
1592int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1593 int attr_mask, struct ib_udata *udata)
1594{
1595 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1596 struct mlx5_ib_qp *qp = to_mqp(ibqp);
1597 enum ib_qp_state cur_state, new_state;
1598 int err = -EINVAL;
1599 int port;
1600
1601 mutex_lock(&qp->mutex);
1602
1603 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
1604 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1605
1606 if (ibqp->qp_type != MLX5_IB_QPT_REG_UMR &&
1607 !ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
1608 goto out;
1609
1610 if ((attr_mask & IB_QP_PORT) &&
1611 (attr->port_num == 0 || attr->port_num > dev->mdev.caps.num_ports))
1612 goto out;
1613
1614 if (attr_mask & IB_QP_PKEY_INDEX) {
1615 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
1616 if (attr->pkey_index >= dev->mdev.caps.port[port - 1].pkey_table_len)
1617 goto out;
1618 }
1619
1620 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
1621 attr->max_rd_atomic > dev->mdev.caps.max_ra_res_qp)
1622 goto out;
1623
1624 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
1625 attr->max_dest_rd_atomic > dev->mdev.caps.max_ra_req_qp)
1626 goto out;
1627
1628 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
1629 err = 0;
1630 goto out;
1631 }
1632
1633 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
1634
1635out:
1636 mutex_unlock(&qp->mutex);
1637 return err;
1638}
1639
1640static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
1641{
1642 struct mlx5_ib_cq *cq;
1643 unsigned cur;
1644
1645 cur = wq->head - wq->tail;
1646 if (likely(cur + nreq < wq->max_post))
1647 return 0;
1648
1649 cq = to_mcq(ib_cq);
1650 spin_lock(&cq->lock);
1651 cur = wq->head - wq->tail;
1652 spin_unlock(&cq->lock);
1653
1654 return cur + nreq >= wq->max_post;
1655}
1656
1657static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
1658 u64 remote_addr, u32 rkey)
1659{
1660 rseg->raddr = cpu_to_be64(remote_addr);
1661 rseg->rkey = cpu_to_be32(rkey);
1662 rseg->reserved = 0;
1663}
1664
Eli Cohene126ba92013-07-07 17:25:49 +03001665static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
1666 struct ib_send_wr *wr)
1667{
1668 memcpy(&dseg->av, &to_mah(wr->wr.ud.ah)->av, sizeof(struct mlx5_av));
1669 dseg->av.dqp_dct = cpu_to_be32(wr->wr.ud.remote_qpn | MLX5_EXTENDED_UD_AV);
1670 dseg->av.key.qkey.qkey = cpu_to_be32(wr->wr.ud.remote_qkey);
1671}
1672
1673static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
1674{
1675 dseg->byte_count = cpu_to_be32(sg->length);
1676 dseg->lkey = cpu_to_be32(sg->lkey);
1677 dseg->addr = cpu_to_be64(sg->addr);
1678}
1679
1680static __be16 get_klm_octo(int npages)
1681{
1682 return cpu_to_be16(ALIGN(npages, 8) / 2);
1683}
1684
1685static __be64 frwr_mkey_mask(void)
1686{
1687 u64 result;
1688
1689 result = MLX5_MKEY_MASK_LEN |
1690 MLX5_MKEY_MASK_PAGE_SIZE |
1691 MLX5_MKEY_MASK_START_ADDR |
1692 MLX5_MKEY_MASK_EN_RINVAL |
1693 MLX5_MKEY_MASK_KEY |
1694 MLX5_MKEY_MASK_LR |
1695 MLX5_MKEY_MASK_LW |
1696 MLX5_MKEY_MASK_RR |
1697 MLX5_MKEY_MASK_RW |
1698 MLX5_MKEY_MASK_A |
1699 MLX5_MKEY_MASK_SMALL_FENCE |
1700 MLX5_MKEY_MASK_FREE;
1701
1702 return cpu_to_be64(result);
1703}
1704
1705static void set_frwr_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
1706 struct ib_send_wr *wr, int li)
1707{
1708 memset(umr, 0, sizeof(*umr));
1709
1710 if (li) {
1711 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
1712 umr->flags = 1 << 7;
1713 return;
1714 }
1715
1716 umr->flags = (1 << 5); /* fail if not free */
1717 umr->klm_octowords = get_klm_octo(wr->wr.fast_reg.page_list_len);
1718 umr->mkey_mask = frwr_mkey_mask();
1719}
1720
1721static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
1722 struct ib_send_wr *wr)
1723{
1724 struct umr_wr *umrwr = (struct umr_wr *)&wr->wr.fast_reg;
1725 u64 mask;
1726
1727 memset(umr, 0, sizeof(*umr));
1728
1729 if (!(wr->send_flags & MLX5_IB_SEND_UMR_UNREG)) {
1730 umr->flags = 1 << 5; /* fail if not free */
1731 umr->klm_octowords = get_klm_octo(umrwr->npages);
1732 mask = MLX5_MKEY_MASK_LEN |
1733 MLX5_MKEY_MASK_PAGE_SIZE |
1734 MLX5_MKEY_MASK_START_ADDR |
1735 MLX5_MKEY_MASK_PD |
1736 MLX5_MKEY_MASK_LR |
1737 MLX5_MKEY_MASK_LW |
1738 MLX5_MKEY_MASK_RR |
1739 MLX5_MKEY_MASK_RW |
1740 MLX5_MKEY_MASK_A |
1741 MLX5_MKEY_MASK_FREE;
1742 umr->mkey_mask = cpu_to_be64(mask);
1743 } else {
1744 umr->flags = 2 << 5; /* fail if free */
1745 mask = MLX5_MKEY_MASK_FREE;
1746 umr->mkey_mask = cpu_to_be64(mask);
1747 }
1748
1749 if (!wr->num_sge)
1750 umr->flags |= (1 << 7); /* inline */
1751}
1752
1753static u8 get_umr_flags(int acc)
1754{
1755 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
1756 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
1757 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
1758 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
1759 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN | MLX5_ACCESS_MODE_MTT;
1760}
1761
1762static void set_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr,
1763 int li, int *writ)
1764{
1765 memset(seg, 0, sizeof(*seg));
1766 if (li) {
1767 seg->status = 1 << 6;
1768 return;
1769 }
1770
1771 seg->flags = get_umr_flags(wr->wr.fast_reg.access_flags);
1772 *writ = seg->flags & (MLX5_PERM_LOCAL_WRITE | IB_ACCESS_REMOTE_WRITE);
1773 seg->qpn_mkey7_0 = cpu_to_be32((wr->wr.fast_reg.rkey & 0xff) | 0xffffff00);
1774 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
1775 seg->start_addr = cpu_to_be64(wr->wr.fast_reg.iova_start);
1776 seg->len = cpu_to_be64(wr->wr.fast_reg.length);
1777 seg->xlt_oct_size = cpu_to_be32((wr->wr.fast_reg.page_list_len + 1) / 2);
1778 seg->log2_page_size = wr->wr.fast_reg.page_shift;
1779}
1780
1781static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
1782{
1783 memset(seg, 0, sizeof(*seg));
1784 if (wr->send_flags & MLX5_IB_SEND_UMR_UNREG) {
1785 seg->status = 1 << 6;
1786 return;
1787 }
1788
1789 seg->flags = convert_access(wr->wr.fast_reg.access_flags);
1790 seg->flags_pd = cpu_to_be32(to_mpd((struct ib_pd *)wr->wr.fast_reg.page_list)->pdn);
1791 seg->start_addr = cpu_to_be64(wr->wr.fast_reg.iova_start);
1792 seg->len = cpu_to_be64(wr->wr.fast_reg.length);
1793 seg->log2_page_size = wr->wr.fast_reg.page_shift;
1794 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
1795}
1796
1797static void set_frwr_pages(struct mlx5_wqe_data_seg *dseg,
1798 struct ib_send_wr *wr,
1799 struct mlx5_core_dev *mdev,
1800 struct mlx5_ib_pd *pd,
1801 int writ)
1802{
1803 struct mlx5_ib_fast_reg_page_list *mfrpl = to_mfrpl(wr->wr.fast_reg.page_list);
1804 u64 *page_list = wr->wr.fast_reg.page_list->page_list;
1805 u64 perm = MLX5_EN_RD | (writ ? MLX5_EN_WR : 0);
1806 int i;
1807
1808 for (i = 0; i < wr->wr.fast_reg.page_list_len; i++)
1809 mfrpl->mapped_page_list[i] = cpu_to_be64(page_list[i] | perm);
1810 dseg->addr = cpu_to_be64(mfrpl->map);
1811 dseg->byte_count = cpu_to_be32(ALIGN(sizeof(u64) * wr->wr.fast_reg.page_list_len, 64));
1812 dseg->lkey = cpu_to_be32(pd->pa_lkey);
1813}
1814
1815static __be32 send_ieth(struct ib_send_wr *wr)
1816{
1817 switch (wr->opcode) {
1818 case IB_WR_SEND_WITH_IMM:
1819 case IB_WR_RDMA_WRITE_WITH_IMM:
1820 return wr->ex.imm_data;
1821
1822 case IB_WR_SEND_WITH_INV:
1823 return cpu_to_be32(wr->ex.invalidate_rkey);
1824
1825 default:
1826 return 0;
1827 }
1828}
1829
1830static u8 calc_sig(void *wqe, int size)
1831{
1832 u8 *p = wqe;
1833 u8 res = 0;
1834 int i;
1835
1836 for (i = 0; i < size; i++)
1837 res ^= p[i];
1838
1839 return ~res;
1840}
1841
1842static u8 wq_sig(void *wqe)
1843{
1844 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
1845}
1846
1847static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
1848 void *wqe, int *sz)
1849{
1850 struct mlx5_wqe_inline_seg *seg;
1851 void *qend = qp->sq.qend;
1852 void *addr;
1853 int inl = 0;
1854 int copy;
1855 int len;
1856 int i;
1857
1858 seg = wqe;
1859 wqe += sizeof(*seg);
1860 for (i = 0; i < wr->num_sge; i++) {
1861 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
1862 len = wr->sg_list[i].length;
1863 inl += len;
1864
1865 if (unlikely(inl > qp->max_inline_data))
1866 return -ENOMEM;
1867
1868 if (unlikely(wqe + len > qend)) {
1869 copy = qend - wqe;
1870 memcpy(wqe, addr, copy);
1871 addr += copy;
1872 len -= copy;
1873 wqe = mlx5_get_send_wqe(qp, 0);
1874 }
1875 memcpy(wqe, addr, len);
1876 wqe += len;
1877 }
1878
1879 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
1880
1881 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
1882
1883 return 0;
1884}
1885
1886static int set_frwr_li_wr(void **seg, struct ib_send_wr *wr, int *size,
1887 struct mlx5_core_dev *mdev, struct mlx5_ib_pd *pd, struct mlx5_ib_qp *qp)
1888{
1889 int writ = 0;
1890 int li;
1891
1892 li = wr->opcode == IB_WR_LOCAL_INV ? 1 : 0;
1893 if (unlikely(wr->send_flags & IB_SEND_INLINE))
1894 return -EINVAL;
1895
1896 set_frwr_umr_segment(*seg, wr, li);
1897 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
1898 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
1899 if (unlikely((*seg == qp->sq.qend)))
1900 *seg = mlx5_get_send_wqe(qp, 0);
1901 set_mkey_segment(*seg, wr, li, &writ);
1902 *seg += sizeof(struct mlx5_mkey_seg);
1903 *size += sizeof(struct mlx5_mkey_seg) / 16;
1904 if (unlikely((*seg == qp->sq.qend)))
1905 *seg = mlx5_get_send_wqe(qp, 0);
1906 if (!li) {
1907 set_frwr_pages(*seg, wr, mdev, pd, writ);
1908 *seg += sizeof(struct mlx5_wqe_data_seg);
1909 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
1910 }
1911 return 0;
1912}
1913
1914static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
1915{
1916 __be32 *p = NULL;
1917 int tidx = idx;
1918 int i, j;
1919
1920 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
1921 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
1922 if ((i & 0xf) == 0) {
1923 void *buf = mlx5_get_send_wqe(qp, tidx);
1924 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
1925 p = buf;
1926 j = 0;
1927 }
1928 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
1929 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
1930 be32_to_cpu(p[j + 3]));
1931 }
1932}
1933
1934static void mlx5_bf_copy(u64 __iomem *dst, u64 *src,
1935 unsigned bytecnt, struct mlx5_ib_qp *qp)
1936{
1937 while (bytecnt > 0) {
1938 __iowrite64_copy(dst++, src++, 8);
1939 __iowrite64_copy(dst++, src++, 8);
1940 __iowrite64_copy(dst++, src++, 8);
1941 __iowrite64_copy(dst++, src++, 8);
1942 __iowrite64_copy(dst++, src++, 8);
1943 __iowrite64_copy(dst++, src++, 8);
1944 __iowrite64_copy(dst++, src++, 8);
1945 __iowrite64_copy(dst++, src++, 8);
1946 bytecnt -= 64;
1947 if (unlikely(src == qp->sq.qend))
1948 src = mlx5_get_send_wqe(qp, 0);
1949 }
1950}
1951
1952static u8 get_fence(u8 fence, struct ib_send_wr *wr)
1953{
1954 if (unlikely(wr->opcode == IB_WR_LOCAL_INV &&
1955 wr->send_flags & IB_SEND_FENCE))
1956 return MLX5_FENCE_MODE_STRONG_ORDERING;
1957
1958 if (unlikely(fence)) {
1959 if (wr->send_flags & IB_SEND_FENCE)
1960 return MLX5_FENCE_MODE_SMALL_AND_FENCE;
1961 else
1962 return fence;
1963
1964 } else {
1965 return 0;
1966 }
1967}
1968
1969int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1970 struct ib_send_wr **bad_wr)
1971{
1972 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
1973 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1974 struct mlx5_core_dev *mdev = &dev->mdev;
1975 struct mlx5_ib_qp *qp = to_mqp(ibqp);
1976 struct mlx5_wqe_data_seg *dpseg;
1977 struct mlx5_wqe_xrc_seg *xrc;
1978 struct mlx5_bf *bf = qp->bf;
1979 int uninitialized_var(size);
1980 void *qend = qp->sq.qend;
1981 unsigned long flags;
1982 u32 mlx5_opcode;
1983 unsigned idx;
1984 int err = 0;
1985 int inl = 0;
1986 int num_sge;
1987 void *seg;
1988 int nreq;
1989 int i;
1990 u8 next_fence = 0;
1991 u8 opmod = 0;
1992 u8 fence;
1993
1994 spin_lock_irqsave(&qp->sq.lock, flags);
1995
1996 for (nreq = 0; wr; nreq++, wr = wr->next) {
1997 if (unlikely(wr->opcode >= sizeof(mlx5_ib_opcode) / sizeof(mlx5_ib_opcode[0]))) {
1998 mlx5_ib_warn(dev, "\n");
1999 err = -EINVAL;
2000 *bad_wr = wr;
2001 goto out;
2002 }
2003
2004 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq))) {
2005 mlx5_ib_warn(dev, "\n");
2006 err = -ENOMEM;
2007 *bad_wr = wr;
2008 goto out;
2009 }
2010
2011 fence = qp->fm_cache;
2012 num_sge = wr->num_sge;
2013 if (unlikely(num_sge > qp->sq.max_gs)) {
2014 mlx5_ib_warn(dev, "\n");
2015 err = -ENOMEM;
2016 *bad_wr = wr;
2017 goto out;
2018 }
2019
2020 idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
2021 seg = mlx5_get_send_wqe(qp, idx);
2022 ctrl = seg;
2023 *(uint32_t *)(seg + 8) = 0;
2024 ctrl->imm = send_ieth(wr);
2025 ctrl->fm_ce_se = qp->sq_signal_bits |
2026 (wr->send_flags & IB_SEND_SIGNALED ?
2027 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
2028 (wr->send_flags & IB_SEND_SOLICITED ?
2029 MLX5_WQE_CTRL_SOLICITED : 0);
2030
2031 seg += sizeof(*ctrl);
2032 size = sizeof(*ctrl) / 16;
2033
2034 switch (ibqp->qp_type) {
2035 case IB_QPT_XRC_INI:
2036 xrc = seg;
2037 xrc->xrc_srqn = htonl(wr->xrc_remote_srq_num);
2038 seg += sizeof(*xrc);
2039 size += sizeof(*xrc) / 16;
2040 /* fall through */
2041 case IB_QPT_RC:
2042 switch (wr->opcode) {
2043 case IB_WR_RDMA_READ:
2044 case IB_WR_RDMA_WRITE:
2045 case IB_WR_RDMA_WRITE_WITH_IMM:
2046 set_raddr_seg(seg, wr->wr.rdma.remote_addr,
2047 wr->wr.rdma.rkey);
2048 seg += sizeof(struct mlx5_wqe_raddr_seg);
2049 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
2050 break;
2051
2052 case IB_WR_ATOMIC_CMP_AND_SWP:
2053 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03002054 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03002055 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
2056 err = -ENOSYS;
2057 *bad_wr = wr;
2058 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03002059
2060 case IB_WR_LOCAL_INV:
2061 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
2062 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
2063 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
2064 err = set_frwr_li_wr(&seg, wr, &size, mdev, to_mpd(ibqp->pd), qp);
2065 if (err) {
2066 mlx5_ib_warn(dev, "\n");
2067 *bad_wr = wr;
2068 goto out;
2069 }
2070 num_sge = 0;
2071 break;
2072
2073 case IB_WR_FAST_REG_MR:
2074 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
2075 qp->sq.wr_data[idx] = IB_WR_FAST_REG_MR;
2076 ctrl->imm = cpu_to_be32(wr->wr.fast_reg.rkey);
2077 err = set_frwr_li_wr(&seg, wr, &size, mdev, to_mpd(ibqp->pd), qp);
2078 if (err) {
2079 mlx5_ib_warn(dev, "\n");
2080 *bad_wr = wr;
2081 goto out;
2082 }
2083 num_sge = 0;
2084 break;
2085
2086 default:
2087 break;
2088 }
2089 break;
2090
2091 case IB_QPT_UC:
2092 switch (wr->opcode) {
2093 case IB_WR_RDMA_WRITE:
2094 case IB_WR_RDMA_WRITE_WITH_IMM:
2095 set_raddr_seg(seg, wr->wr.rdma.remote_addr,
2096 wr->wr.rdma.rkey);
2097 seg += sizeof(struct mlx5_wqe_raddr_seg);
2098 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
2099 break;
2100
2101 default:
2102 break;
2103 }
2104 break;
2105
2106 case IB_QPT_UD:
2107 case IB_QPT_SMI:
2108 case IB_QPT_GSI:
2109 set_datagram_seg(seg, wr);
2110 seg += sizeof(struct mlx5_wqe_datagram_seg);
2111 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
2112 if (unlikely((seg == qend)))
2113 seg = mlx5_get_send_wqe(qp, 0);
2114 break;
2115
2116 case MLX5_IB_QPT_REG_UMR:
2117 if (wr->opcode != MLX5_IB_WR_UMR) {
2118 err = -EINVAL;
2119 mlx5_ib_warn(dev, "bad opcode\n");
2120 goto out;
2121 }
2122 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
2123 ctrl->imm = cpu_to_be32(wr->wr.fast_reg.rkey);
2124 set_reg_umr_segment(seg, wr);
2125 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
2126 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
2127 if (unlikely((seg == qend)))
2128 seg = mlx5_get_send_wqe(qp, 0);
2129 set_reg_mkey_segment(seg, wr);
2130 seg += sizeof(struct mlx5_mkey_seg);
2131 size += sizeof(struct mlx5_mkey_seg) / 16;
2132 if (unlikely((seg == qend)))
2133 seg = mlx5_get_send_wqe(qp, 0);
2134 break;
2135
2136 default:
2137 break;
2138 }
2139
2140 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
2141 int uninitialized_var(sz);
2142
2143 err = set_data_inl_seg(qp, wr, seg, &sz);
2144 if (unlikely(err)) {
2145 mlx5_ib_warn(dev, "\n");
2146 *bad_wr = wr;
2147 goto out;
2148 }
2149 inl = 1;
2150 size += sz;
2151 } else {
2152 dpseg = seg;
2153 for (i = 0; i < num_sge; i++) {
2154 if (unlikely(dpseg == qend)) {
2155 seg = mlx5_get_send_wqe(qp, 0);
2156 dpseg = seg;
2157 }
2158 if (likely(wr->sg_list[i].length)) {
2159 set_data_ptr_seg(dpseg, wr->sg_list + i);
2160 size += sizeof(struct mlx5_wqe_data_seg) / 16;
2161 dpseg++;
2162 }
2163 }
2164 }
2165
2166 mlx5_opcode = mlx5_ib_opcode[wr->opcode];
2167 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
2168 mlx5_opcode |
2169 ((u32)opmod << 24));
2170 ctrl->qpn_ds = cpu_to_be32(size | (qp->mqp.qpn << 8));
2171 ctrl->fm_ce_se |= get_fence(fence, wr);
2172 qp->fm_cache = next_fence;
2173 if (unlikely(qp->wq_sig))
2174 ctrl->signature = wq_sig(ctrl);
2175
2176 qp->sq.wrid[idx] = wr->wr_id;
2177 qp->sq.w_list[idx].opcode = mlx5_opcode;
2178 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
2179 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
2180 qp->sq.w_list[idx].next = qp->sq.cur_post;
2181
2182 if (0)
2183 dump_wqe(qp, idx, size);
2184 }
2185
2186out:
2187 if (likely(nreq)) {
2188 qp->sq.head += nreq;
2189
2190 /* Make sure that descriptors are written before
2191 * updating doorbell record and ringing the doorbell
2192 */
2193 wmb();
2194
2195 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
2196
2197 if (bf->need_lock)
2198 spin_lock(&bf->lock);
2199
2200 /* TBD enable WC */
2201 if (0 && nreq == 1 && bf->uuarn && inl && size > 1 && size <= bf->buf_size / 16) {
2202 mlx5_bf_copy(bf->reg + bf->offset, (u64 *)ctrl, ALIGN(size * 16, 64), qp);
2203 /* wc_wmb(); */
2204 } else {
2205 mlx5_write64((__be32 *)ctrl, bf->regreg + bf->offset,
2206 MLX5_GET_DOORBELL_LOCK(&bf->lock32));
2207 /* Make sure doorbells don't leak out of SQ spinlock
2208 * and reach the HCA out of order.
2209 */
2210 mmiowb();
2211 }
2212 bf->offset ^= bf->buf_size;
2213 if (bf->need_lock)
2214 spin_unlock(&bf->lock);
2215 }
2216
2217 spin_unlock_irqrestore(&qp->sq.lock, flags);
2218
2219 return err;
2220}
2221
2222static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
2223{
2224 sig->signature = calc_sig(sig, size);
2225}
2226
2227int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
2228 struct ib_recv_wr **bad_wr)
2229{
2230 struct mlx5_ib_qp *qp = to_mqp(ibqp);
2231 struct mlx5_wqe_data_seg *scat;
2232 struct mlx5_rwqe_sig *sig;
2233 unsigned long flags;
2234 int err = 0;
2235 int nreq;
2236 int ind;
2237 int i;
2238
2239 spin_lock_irqsave(&qp->rq.lock, flags);
2240
2241 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
2242
2243 for (nreq = 0; wr; nreq++, wr = wr->next) {
2244 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
2245 err = -ENOMEM;
2246 *bad_wr = wr;
2247 goto out;
2248 }
2249
2250 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
2251 err = -EINVAL;
2252 *bad_wr = wr;
2253 goto out;
2254 }
2255
2256 scat = get_recv_wqe(qp, ind);
2257 if (qp->wq_sig)
2258 scat++;
2259
2260 for (i = 0; i < wr->num_sge; i++)
2261 set_data_ptr_seg(scat + i, wr->sg_list + i);
2262
2263 if (i < qp->rq.max_gs) {
2264 scat[i].byte_count = 0;
2265 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
2266 scat[i].addr = 0;
2267 }
2268
2269 if (qp->wq_sig) {
2270 sig = (struct mlx5_rwqe_sig *)scat;
2271 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
2272 }
2273
2274 qp->rq.wrid[ind] = wr->wr_id;
2275
2276 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
2277 }
2278
2279out:
2280 if (likely(nreq)) {
2281 qp->rq.head += nreq;
2282
2283 /* Make sure that descriptors are written before
2284 * doorbell record.
2285 */
2286 wmb();
2287
2288 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
2289 }
2290
2291 spin_unlock_irqrestore(&qp->rq.lock, flags);
2292
2293 return err;
2294}
2295
2296static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
2297{
2298 switch (mlx5_state) {
2299 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
2300 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
2301 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
2302 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
2303 case MLX5_QP_STATE_SQ_DRAINING:
2304 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
2305 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
2306 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
2307 default: return -1;
2308 }
2309}
2310
2311static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
2312{
2313 switch (mlx5_mig_state) {
2314 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
2315 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
2316 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
2317 default: return -1;
2318 }
2319}
2320
2321static int to_ib_qp_access_flags(int mlx5_flags)
2322{
2323 int ib_flags = 0;
2324
2325 if (mlx5_flags & MLX5_QP_BIT_RRE)
2326 ib_flags |= IB_ACCESS_REMOTE_READ;
2327 if (mlx5_flags & MLX5_QP_BIT_RWE)
2328 ib_flags |= IB_ACCESS_REMOTE_WRITE;
2329 if (mlx5_flags & MLX5_QP_BIT_RAE)
2330 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
2331
2332 return ib_flags;
2333}
2334
2335static void to_ib_ah_attr(struct mlx5_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
2336 struct mlx5_qp_path *path)
2337{
2338 struct mlx5_core_dev *dev = &ibdev->mdev;
2339
2340 memset(ib_ah_attr, 0, sizeof(*ib_ah_attr));
2341 ib_ah_attr->port_num = path->port;
2342
2343 if (ib_ah_attr->port_num == 0 || ib_ah_attr->port_num > dev->caps.num_ports)
2344 return;
2345
2346 ib_ah_attr->sl = path->sl & 0xf;
2347
2348 ib_ah_attr->dlid = be16_to_cpu(path->rlid);
2349 ib_ah_attr->src_path_bits = path->grh_mlid & 0x7f;
2350 ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
2351 ib_ah_attr->ah_flags = (path->grh_mlid & (1 << 7)) ? IB_AH_GRH : 0;
2352 if (ib_ah_attr->ah_flags) {
2353 ib_ah_attr->grh.sgid_index = path->mgid_index;
2354 ib_ah_attr->grh.hop_limit = path->hop_limit;
2355 ib_ah_attr->grh.traffic_class =
2356 (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
2357 ib_ah_attr->grh.flow_label =
2358 be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
2359 memcpy(ib_ah_attr->grh.dgid.raw,
2360 path->rgid, sizeof(ib_ah_attr->grh.dgid.raw));
2361 }
2362}
2363
2364int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
2365 struct ib_qp_init_attr *qp_init_attr)
2366{
2367 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2368 struct mlx5_ib_qp *qp = to_mqp(ibqp);
2369 struct mlx5_query_qp_mbox_out *outb;
2370 struct mlx5_qp_context *context;
2371 int mlx5_state;
2372 int err = 0;
2373
2374 mutex_lock(&qp->mutex);
2375 outb = kzalloc(sizeof(*outb), GFP_KERNEL);
2376 if (!outb) {
2377 err = -ENOMEM;
2378 goto out;
2379 }
2380 context = &outb->ctx;
2381 err = mlx5_core_qp_query(&dev->mdev, &qp->mqp, outb, sizeof(*outb));
2382 if (err)
2383 goto out_free;
2384
2385 mlx5_state = be32_to_cpu(context->flags) >> 28;
2386
2387 qp->state = to_ib_qp_state(mlx5_state);
2388 qp_attr->qp_state = qp->state;
2389 qp_attr->path_mtu = context->mtu_msgmax >> 5;
2390 qp_attr->path_mig_state =
2391 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
2392 qp_attr->qkey = be32_to_cpu(context->qkey);
2393 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
2394 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
2395 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
2396 qp_attr->qp_access_flags =
2397 to_ib_qp_access_flags(be32_to_cpu(context->params2));
2398
2399 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
2400 to_ib_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
2401 to_ib_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
2402 qp_attr->alt_pkey_index = context->alt_path.pkey_index & 0x7f;
2403 qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
2404 }
2405
2406 qp_attr->pkey_index = context->pri_path.pkey_index & 0x7f;
2407 qp_attr->port_num = context->pri_path.port;
2408
2409 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
2410 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
2411
2412 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
2413
2414 qp_attr->max_dest_rd_atomic =
2415 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
2416 qp_attr->min_rnr_timer =
2417 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
2418 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
2419 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
2420 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
2421 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
2422 qp_attr->cur_qp_state = qp_attr->qp_state;
2423 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
2424 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
2425
2426 if (!ibqp->uobject) {
2427 qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
2428 qp_attr->cap.max_send_sge = qp->sq.max_gs;
2429 } else {
2430 qp_attr->cap.max_send_wr = 0;
2431 qp_attr->cap.max_send_sge = 0;
2432 }
2433
2434 /* We don't support inline sends for kernel QPs (yet), and we
2435 * don't know what userspace's value should be.
2436 */
2437 qp_attr->cap.max_inline_data = 0;
2438
2439 qp_init_attr->cap = qp_attr->cap;
2440
2441 qp_init_attr->create_flags = 0;
2442 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
2443 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
2444
2445 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
2446 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
2447
2448out_free:
2449 kfree(outb);
2450
2451out:
2452 mutex_unlock(&qp->mutex);
2453 return err;
2454}
2455
2456struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
2457 struct ib_ucontext *context,
2458 struct ib_udata *udata)
2459{
2460 struct mlx5_ib_dev *dev = to_mdev(ibdev);
2461 struct mlx5_ib_xrcd *xrcd;
2462 int err;
2463
2464 if (!(dev->mdev.caps.flags & MLX5_DEV_CAP_FLAG_XRC))
2465 return ERR_PTR(-ENOSYS);
2466
2467 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
2468 if (!xrcd)
2469 return ERR_PTR(-ENOMEM);
2470
2471 err = mlx5_core_xrcd_alloc(&dev->mdev, &xrcd->xrcdn);
2472 if (err) {
2473 kfree(xrcd);
2474 return ERR_PTR(-ENOMEM);
2475 }
2476
2477 return &xrcd->ibxrcd;
2478}
2479
2480int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
2481{
2482 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
2483 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
2484 int err;
2485
2486 err = mlx5_core_xrcd_dealloc(&dev->mdev, xrcdn);
2487 if (err) {
2488 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
2489 return err;
2490 }
2491
2492 kfree(xrcd);
2493
2494 return 0;
2495}