2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/module.h>
34 #include <rdma/ib_umem.h>
35 #include <rdma/ib_cache.h>
36 #include <rdma/ib_user_verbs.h>
39 /* not supported currently */
40 static int wq_signature
;
43 MLX5_IB_ACK_REQ_FREQ
= 8,
47 MLX5_IB_DEFAULT_SCHED_QUEUE
= 0x83,
48 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE
= 0x3f,
49 MLX5_IB_LINK_TYPE_IB
= 0,
50 MLX5_IB_LINK_TYPE_ETH
= 1
54 MLX5_IB_SQ_STRIDE
= 6,
57 static const u32 mlx5_ib_opcode
[] = {
58 [IB_WR_SEND
] = MLX5_OPCODE_SEND
,
59 [IB_WR_LSO
] = MLX5_OPCODE_LSO
,
60 [IB_WR_SEND_WITH_IMM
] = MLX5_OPCODE_SEND_IMM
,
61 [IB_WR_RDMA_WRITE
] = MLX5_OPCODE_RDMA_WRITE
,
62 [IB_WR_RDMA_WRITE_WITH_IMM
] = MLX5_OPCODE_RDMA_WRITE_IMM
,
63 [IB_WR_RDMA_READ
] = MLX5_OPCODE_RDMA_READ
,
64 [IB_WR_ATOMIC_CMP_AND_SWP
] = MLX5_OPCODE_ATOMIC_CS
,
65 [IB_WR_ATOMIC_FETCH_AND_ADD
] = MLX5_OPCODE_ATOMIC_FA
,
66 [IB_WR_SEND_WITH_INV
] = MLX5_OPCODE_SEND_INVAL
,
67 [IB_WR_LOCAL_INV
] = MLX5_OPCODE_UMR
,
68 [IB_WR_REG_MR
] = MLX5_OPCODE_UMR
,
69 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP
] = MLX5_OPCODE_ATOMIC_MASKED_CS
,
70 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD
] = MLX5_OPCODE_ATOMIC_MASKED_FA
,
71 [MLX5_IB_WR_UMR
] = MLX5_OPCODE_UMR
,
74 struct mlx5_wqe_eth_pad
{
78 enum raw_qp_set_mask_map
{
79 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID
= 1UL << 0,
82 struct mlx5_modify_raw_qp_param
{
85 u32 set_mask
; /* raw_qp_set_mask_map */
89 static void get_cqs(enum ib_qp_type qp_type
,
90 struct ib_cq
*ib_send_cq
, struct ib_cq
*ib_recv_cq
,
91 struct mlx5_ib_cq
**send_cq
, struct mlx5_ib_cq
**recv_cq
);
93 static int is_qp0(enum ib_qp_type qp_type
)
95 return qp_type
== IB_QPT_SMI
;
98 static int is_sqp(enum ib_qp_type qp_type
)
100 return is_qp0(qp_type
) || is_qp1(qp_type
);
103 static void *get_wqe(struct mlx5_ib_qp
*qp
, int offset
)
105 return mlx5_buf_offset(&qp
->buf
, offset
);
108 static void *get_recv_wqe(struct mlx5_ib_qp
*qp
, int n
)
110 return get_wqe(qp
, qp
->rq
.offset
+ (n
<< qp
->rq
.wqe_shift
));
113 void *mlx5_get_send_wqe(struct mlx5_ib_qp
*qp
, int n
)
115 return get_wqe(qp
, qp
->sq
.offset
+ (n
<< MLX5_IB_SQ_STRIDE
));
119 * mlx5_ib_read_user_wqe() - Copy a user-space WQE to kernel space.
121 * @qp: QP to copy from.
122 * @send: copy from the send queue when non-zero, use the receive queue
124 * @wqe_index: index to start copying from. For send work queues, the
125 * wqe_index is in units of MLX5_SEND_WQE_BB.
126 * For receive work queue, it is the number of work queue
127 * element in the queue.
128 * @buffer: destination buffer.
129 * @length: maximum number of bytes to copy.
131 * Copies at least a single WQE, but may copy more data.
133 * Return: the number of bytes copied, or an error code.
135 int mlx5_ib_read_user_wqe(struct mlx5_ib_qp
*qp
, int send
, int wqe_index
,
136 void *buffer
, u32 length
,
137 struct mlx5_ib_qp_base
*base
)
139 struct ib_device
*ibdev
= qp
->ibqp
.device
;
140 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
141 struct mlx5_ib_wq
*wq
= send
? &qp
->sq
: &qp
->rq
;
144 struct ib_umem
*umem
= base
->ubuffer
.umem
;
145 u32 first_copy_length
;
149 if (wq
->wqe_cnt
== 0) {
150 mlx5_ib_dbg(dev
, "mlx5_ib_read_user_wqe for a QP with wqe_cnt == 0. qp_type: 0x%x\n",
155 offset
= wq
->offset
+ ((wqe_index
% wq
->wqe_cnt
) << wq
->wqe_shift
);
156 wq_end
= wq
->offset
+ (wq
->wqe_cnt
<< wq
->wqe_shift
);
158 if (send
&& length
< sizeof(struct mlx5_wqe_ctrl_seg
))
161 if (offset
> umem
->length
||
162 (send
&& offset
+ sizeof(struct mlx5_wqe_ctrl_seg
) > umem
->length
))
165 first_copy_length
= min_t(u32
, offset
+ length
, wq_end
) - offset
;
166 ret
= ib_umem_copy_from(buffer
, umem
, offset
, first_copy_length
);
171 struct mlx5_wqe_ctrl_seg
*ctrl
= buffer
;
172 int ds
= be32_to_cpu(ctrl
->qpn_ds
) & MLX5_WQE_CTRL_DS_MASK
;
174 wqe_length
= ds
* MLX5_WQE_DS_UNITS
;
176 wqe_length
= 1 << wq
->wqe_shift
;
179 if (wqe_length
<= first_copy_length
)
180 return first_copy_length
;
182 ret
= ib_umem_copy_from(buffer
+ first_copy_length
, umem
, wq
->offset
,
183 wqe_length
- first_copy_length
);
190 static void mlx5_ib_qp_event(struct mlx5_core_qp
*qp
, int type
)
192 struct ib_qp
*ibqp
= &to_mibqp(qp
)->ibqp
;
193 struct ib_event event
;
195 if (type
== MLX5_EVENT_TYPE_PATH_MIG
) {
196 /* This event is only valid for trans_qps */
197 to_mibqp(qp
)->port
= to_mibqp(qp
)->trans_qp
.alt_port
;
200 if (ibqp
->event_handler
) {
201 event
.device
= ibqp
->device
;
202 event
.element
.qp
= ibqp
;
204 case MLX5_EVENT_TYPE_PATH_MIG
:
205 event
.event
= IB_EVENT_PATH_MIG
;
207 case MLX5_EVENT_TYPE_COMM_EST
:
208 event
.event
= IB_EVENT_COMM_EST
;
210 case MLX5_EVENT_TYPE_SQ_DRAINED
:
211 event
.event
= IB_EVENT_SQ_DRAINED
;
213 case MLX5_EVENT_TYPE_SRQ_LAST_WQE
:
214 event
.event
= IB_EVENT_QP_LAST_WQE_REACHED
;
216 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR
:
217 event
.event
= IB_EVENT_QP_FATAL
;
219 case MLX5_EVENT_TYPE_PATH_MIG_FAILED
:
220 event
.event
= IB_EVENT_PATH_MIG_ERR
;
222 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR
:
223 event
.event
= IB_EVENT_QP_REQ_ERR
;
225 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR
:
226 event
.event
= IB_EVENT_QP_ACCESS_ERR
;
229 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type
, qp
->qpn
);
233 ibqp
->event_handler(&event
, ibqp
->qp_context
);
237 static int set_rq_size(struct mlx5_ib_dev
*dev
, struct ib_qp_cap
*cap
,
238 int has_rq
, struct mlx5_ib_qp
*qp
, struct mlx5_ib_create_qp
*ucmd
)
243 /* Sanity check RQ size before proceeding */
244 if (cap
->max_recv_wr
> (1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp_sz
)))
250 qp
->rq
.wqe_shift
= 0;
251 cap
->max_recv_wr
= 0;
252 cap
->max_recv_sge
= 0;
255 qp
->rq
.wqe_cnt
= ucmd
->rq_wqe_count
;
256 qp
->rq
.wqe_shift
= ucmd
->rq_wqe_shift
;
257 qp
->rq
.max_gs
= (1 << qp
->rq
.wqe_shift
) / sizeof(struct mlx5_wqe_data_seg
) - qp
->wq_sig
;
258 qp
->rq
.max_post
= qp
->rq
.wqe_cnt
;
260 wqe_size
= qp
->wq_sig
? sizeof(struct mlx5_wqe_signature_seg
) : 0;
261 wqe_size
+= cap
->max_recv_sge
* sizeof(struct mlx5_wqe_data_seg
);
262 wqe_size
= roundup_pow_of_two(wqe_size
);
263 wq_size
= roundup_pow_of_two(cap
->max_recv_wr
) * wqe_size
;
264 wq_size
= max_t(int, wq_size
, MLX5_SEND_WQE_BB
);
265 qp
->rq
.wqe_cnt
= wq_size
/ wqe_size
;
266 if (wqe_size
> MLX5_CAP_GEN(dev
->mdev
, max_wqe_sz_rq
)) {
267 mlx5_ib_dbg(dev
, "wqe_size %d, max %d\n",
269 MLX5_CAP_GEN(dev
->mdev
,
273 qp
->rq
.wqe_shift
= ilog2(wqe_size
);
274 qp
->rq
.max_gs
= (1 << qp
->rq
.wqe_shift
) / sizeof(struct mlx5_wqe_data_seg
) - qp
->wq_sig
;
275 qp
->rq
.max_post
= qp
->rq
.wqe_cnt
;
282 static int sq_overhead(struct ib_qp_init_attr
*attr
)
286 switch (attr
->qp_type
) {
288 size
+= sizeof(struct mlx5_wqe_xrc_seg
);
291 size
+= sizeof(struct mlx5_wqe_ctrl_seg
) +
292 max(sizeof(struct mlx5_wqe_atomic_seg
) +
293 sizeof(struct mlx5_wqe_raddr_seg
),
294 sizeof(struct mlx5_wqe_umr_ctrl_seg
) +
295 sizeof(struct mlx5_mkey_seg
));
302 size
+= sizeof(struct mlx5_wqe_ctrl_seg
) +
303 max(sizeof(struct mlx5_wqe_raddr_seg
),
304 sizeof(struct mlx5_wqe_umr_ctrl_seg
) +
305 sizeof(struct mlx5_mkey_seg
));
309 if (attr
->create_flags
& IB_QP_CREATE_IPOIB_UD_LSO
)
310 size
+= sizeof(struct mlx5_wqe_eth_pad
) +
311 sizeof(struct mlx5_wqe_eth_seg
);
314 case MLX5_IB_QPT_HW_GSI
:
315 size
+= sizeof(struct mlx5_wqe_ctrl_seg
) +
316 sizeof(struct mlx5_wqe_datagram_seg
);
319 case MLX5_IB_QPT_REG_UMR
:
320 size
+= sizeof(struct mlx5_wqe_ctrl_seg
) +
321 sizeof(struct mlx5_wqe_umr_ctrl_seg
) +
322 sizeof(struct mlx5_mkey_seg
);
332 static int calc_send_wqe(struct ib_qp_init_attr
*attr
)
337 size
= sq_overhead(attr
);
341 if (attr
->cap
.max_inline_data
) {
342 inl_size
= size
+ sizeof(struct mlx5_wqe_inline_seg
) +
343 attr
->cap
.max_inline_data
;
346 size
+= attr
->cap
.max_send_sge
* sizeof(struct mlx5_wqe_data_seg
);
347 if (attr
->create_flags
& IB_QP_CREATE_SIGNATURE_EN
&&
348 ALIGN(max_t(int, inl_size
, size
), MLX5_SEND_WQE_BB
) < MLX5_SIG_WQE_SIZE
)
349 return MLX5_SIG_WQE_SIZE
;
351 return ALIGN(max_t(int, inl_size
, size
), MLX5_SEND_WQE_BB
);
354 static int calc_sq_size(struct mlx5_ib_dev
*dev
, struct ib_qp_init_attr
*attr
,
355 struct mlx5_ib_qp
*qp
)
360 if (!attr
->cap
.max_send_wr
)
363 wqe_size
= calc_send_wqe(attr
);
364 mlx5_ib_dbg(dev
, "wqe_size %d\n", wqe_size
);
368 if (wqe_size
> MLX5_CAP_GEN(dev
->mdev
, max_wqe_sz_sq
)) {
369 mlx5_ib_dbg(dev
, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
370 wqe_size
, MLX5_CAP_GEN(dev
->mdev
, max_wqe_sz_sq
));
374 qp
->max_inline_data
= wqe_size
- sq_overhead(attr
) -
375 sizeof(struct mlx5_wqe_inline_seg
);
376 attr
->cap
.max_inline_data
= qp
->max_inline_data
;
378 if (attr
->create_flags
& IB_QP_CREATE_SIGNATURE_EN
)
379 qp
->signature_en
= true;
381 wq_size
= roundup_pow_of_two(attr
->cap
.max_send_wr
* wqe_size
);
382 qp
->sq
.wqe_cnt
= wq_size
/ MLX5_SEND_WQE_BB
;
383 if (qp
->sq
.wqe_cnt
> (1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp_sz
))) {
384 mlx5_ib_dbg(dev
, "wqe count(%d) exceeds limits(%d)\n",
386 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp_sz
));
389 qp
->sq
.wqe_shift
= ilog2(MLX5_SEND_WQE_BB
);
390 qp
->sq
.max_gs
= attr
->cap
.max_send_sge
;
391 qp
->sq
.max_post
= wq_size
/ wqe_size
;
392 attr
->cap
.max_send_wr
= qp
->sq
.max_post
;
397 static int set_user_buf_size(struct mlx5_ib_dev
*dev
,
398 struct mlx5_ib_qp
*qp
,
399 struct mlx5_ib_create_qp
*ucmd
,
400 struct mlx5_ib_qp_base
*base
,
401 struct ib_qp_init_attr
*attr
)
403 int desc_sz
= 1 << qp
->sq
.wqe_shift
;
405 if (desc_sz
> MLX5_CAP_GEN(dev
->mdev
, max_wqe_sz_sq
)) {
406 mlx5_ib_warn(dev
, "desc_sz %d, max_sq_desc_sz %d\n",
407 desc_sz
, MLX5_CAP_GEN(dev
->mdev
, max_wqe_sz_sq
));
411 if (ucmd
->sq_wqe_count
&& ((1 << ilog2(ucmd
->sq_wqe_count
)) != ucmd
->sq_wqe_count
)) {
412 mlx5_ib_warn(dev
, "sq_wqe_count %d, sq_wqe_count %d\n",
413 ucmd
->sq_wqe_count
, ucmd
->sq_wqe_count
);
417 qp
->sq
.wqe_cnt
= ucmd
->sq_wqe_count
;
419 if (qp
->sq
.wqe_cnt
> (1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp_sz
))) {
420 mlx5_ib_warn(dev
, "wqe_cnt %d, max_wqes %d\n",
422 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp_sz
));
426 if (attr
->qp_type
== IB_QPT_RAW_PACKET
) {
427 base
->ubuffer
.buf_size
= qp
->rq
.wqe_cnt
<< qp
->rq
.wqe_shift
;
428 qp
->raw_packet_qp
.sq
.ubuffer
.buf_size
= qp
->sq
.wqe_cnt
<< 6;
430 base
->ubuffer
.buf_size
= (qp
->rq
.wqe_cnt
<< qp
->rq
.wqe_shift
) +
431 (qp
->sq
.wqe_cnt
<< 6);
437 static int qp_has_rq(struct ib_qp_init_attr
*attr
)
439 if (attr
->qp_type
== IB_QPT_XRC_INI
||
440 attr
->qp_type
== IB_QPT_XRC_TGT
|| attr
->srq
||
441 attr
->qp_type
== MLX5_IB_QPT_REG_UMR
||
442 !attr
->cap
.max_recv_wr
)
448 static int first_med_uuar(void)
453 static int next_uuar(int n
)
457 while (((n
% 4) & 2))
463 static int num_med_uuar(struct mlx5_uuar_info
*uuari
)
467 n
= uuari
->num_uars
* MLX5_NON_FP_BF_REGS_PER_PAGE
-
468 uuari
->num_low_latency_uuars
- 1;
470 return n
>= 0 ? n
: 0;
473 static int max_uuari(struct mlx5_uuar_info
*uuari
)
475 return uuari
->num_uars
* 4;
478 static int first_hi_uuar(struct mlx5_uuar_info
*uuari
)
484 med
= num_med_uuar(uuari
);
485 for (t
= 0, i
= first_med_uuar();; i
= next_uuar(i
)) {
494 static int alloc_high_class_uuar(struct mlx5_uuar_info
*uuari
)
498 for (i
= first_hi_uuar(uuari
); i
< max_uuari(uuari
); i
= next_uuar(i
)) {
499 if (!test_bit(i
, uuari
->bitmap
)) {
500 set_bit(i
, uuari
->bitmap
);
509 static int alloc_med_class_uuar(struct mlx5_uuar_info
*uuari
)
511 int minidx
= first_med_uuar();
514 for (i
= first_med_uuar(); i
< first_hi_uuar(uuari
); i
= next_uuar(i
)) {
515 if (uuari
->count
[i
] < uuari
->count
[minidx
])
519 uuari
->count
[minidx
]++;
523 static int alloc_uuar(struct mlx5_uuar_info
*uuari
,
524 enum mlx5_ib_latency_class lat
)
528 mutex_lock(&uuari
->lock
);
530 case MLX5_IB_LATENCY_CLASS_LOW
:
532 uuari
->count
[uuarn
]++;
535 case MLX5_IB_LATENCY_CLASS_MEDIUM
:
539 uuarn
= alloc_med_class_uuar(uuari
);
542 case MLX5_IB_LATENCY_CLASS_HIGH
:
546 uuarn
= alloc_high_class_uuar(uuari
);
549 case MLX5_IB_LATENCY_CLASS_FAST_PATH
:
553 mutex_unlock(&uuari
->lock
);
558 static void free_med_class_uuar(struct mlx5_uuar_info
*uuari
, int uuarn
)
560 clear_bit(uuarn
, uuari
->bitmap
);
561 --uuari
->count
[uuarn
];
564 static void free_high_class_uuar(struct mlx5_uuar_info
*uuari
, int uuarn
)
566 clear_bit(uuarn
, uuari
->bitmap
);
567 --uuari
->count
[uuarn
];
570 static void free_uuar(struct mlx5_uuar_info
*uuari
, int uuarn
)
572 int nuuars
= uuari
->num_uars
* MLX5_BF_REGS_PER_PAGE
;
573 int high_uuar
= nuuars
- uuari
->num_low_latency_uuars
;
575 mutex_lock(&uuari
->lock
);
577 --uuari
->count
[uuarn
];
581 if (uuarn
< high_uuar
) {
582 free_med_class_uuar(uuari
, uuarn
);
586 free_high_class_uuar(uuari
, uuarn
);
589 mutex_unlock(&uuari
->lock
);
592 static enum mlx5_qp_state
to_mlx5_state(enum ib_qp_state state
)
595 case IB_QPS_RESET
: return MLX5_QP_STATE_RST
;
596 case IB_QPS_INIT
: return MLX5_QP_STATE_INIT
;
597 case IB_QPS_RTR
: return MLX5_QP_STATE_RTR
;
598 case IB_QPS_RTS
: return MLX5_QP_STATE_RTS
;
599 case IB_QPS_SQD
: return MLX5_QP_STATE_SQD
;
600 case IB_QPS_SQE
: return MLX5_QP_STATE_SQER
;
601 case IB_QPS_ERR
: return MLX5_QP_STATE_ERR
;
606 static int to_mlx5_st(enum ib_qp_type type
)
609 case IB_QPT_RC
: return MLX5_QP_ST_RC
;
610 case IB_QPT_UC
: return MLX5_QP_ST_UC
;
611 case IB_QPT_UD
: return MLX5_QP_ST_UD
;
612 case MLX5_IB_QPT_REG_UMR
: return MLX5_QP_ST_REG_UMR
;
614 case IB_QPT_XRC_TGT
: return MLX5_QP_ST_XRC
;
615 case IB_QPT_SMI
: return MLX5_QP_ST_QP0
;
616 case MLX5_IB_QPT_HW_GSI
: return MLX5_QP_ST_QP1
;
617 case IB_QPT_RAW_IPV6
: return MLX5_QP_ST_RAW_IPV6
;
618 case IB_QPT_RAW_PACKET
:
619 case IB_QPT_RAW_ETHERTYPE
: return MLX5_QP_ST_RAW_ETHERTYPE
;
621 default: return -EINVAL
;
625 static void mlx5_ib_lock_cqs(struct mlx5_ib_cq
*send_cq
,
626 struct mlx5_ib_cq
*recv_cq
);
627 static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq
*send_cq
,
628 struct mlx5_ib_cq
*recv_cq
);
630 static int uuarn_to_uar_index(struct mlx5_uuar_info
*uuari
, int uuarn
)
632 return uuari
->uars
[uuarn
/ MLX5_BF_REGS_PER_PAGE
].index
;
635 static int mlx5_ib_umem_get(struct mlx5_ib_dev
*dev
,
637 unsigned long addr
, size_t size
,
638 struct ib_umem
**umem
,
639 int *npages
, int *page_shift
, int *ncont
,
644 *umem
= ib_umem_get(pd
->uobject
->context
, addr
, size
, 0, 0);
646 mlx5_ib_dbg(dev
, "umem_get failed\n");
647 return PTR_ERR(*umem
);
650 mlx5_ib_cont_pages(*umem
, addr
, npages
, page_shift
, ncont
, NULL
);
652 err
= mlx5_ib_get_buf_offset(addr
, *page_shift
, offset
);
654 mlx5_ib_warn(dev
, "bad offset\n");
658 mlx5_ib_dbg(dev
, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
659 addr
, size
, *npages
, *page_shift
, *ncont
, *offset
);
664 ib_umem_release(*umem
);
670 static void destroy_user_rq(struct ib_pd
*pd
, struct mlx5_ib_rwq
*rwq
)
672 struct mlx5_ib_ucontext
*context
;
674 context
= to_mucontext(pd
->uobject
->context
);
675 mlx5_ib_db_unmap_user(context
, &rwq
->db
);
677 ib_umem_release(rwq
->umem
);
680 static int create_user_rq(struct mlx5_ib_dev
*dev
, struct ib_pd
*pd
,
681 struct mlx5_ib_rwq
*rwq
,
682 struct mlx5_ib_create_wq
*ucmd
)
684 struct mlx5_ib_ucontext
*context
;
694 context
= to_mucontext(pd
->uobject
->context
);
695 rwq
->umem
= ib_umem_get(pd
->uobject
->context
, ucmd
->buf_addr
,
696 rwq
->buf_size
, 0, 0);
697 if (IS_ERR(rwq
->umem
)) {
698 mlx5_ib_dbg(dev
, "umem_get failed\n");
699 err
= PTR_ERR(rwq
->umem
);
703 mlx5_ib_cont_pages(rwq
->umem
, ucmd
->buf_addr
, &npages
, &page_shift
,
705 err
= mlx5_ib_get_buf_offset(ucmd
->buf_addr
, page_shift
,
706 &rwq
->rq_page_offset
);
708 mlx5_ib_warn(dev
, "bad offset\n");
712 rwq
->rq_num_pas
= ncont
;
713 rwq
->page_shift
= page_shift
;
714 rwq
->log_page_size
= page_shift
- MLX5_ADAPTER_PAGE_SHIFT
;
715 rwq
->wq_sig
= !!(ucmd
->flags
& MLX5_WQ_FLAG_SIGNATURE
);
717 mlx5_ib_dbg(dev
, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
718 (unsigned long long)ucmd
->buf_addr
, rwq
->buf_size
,
719 npages
, page_shift
, ncont
, offset
);
721 err
= mlx5_ib_db_map_user(context
, ucmd
->db_addr
, &rwq
->db
);
723 mlx5_ib_dbg(dev
, "map failed\n");
727 rwq
->create_type
= MLX5_WQ_USER
;
731 ib_umem_release(rwq
->umem
);
735 static int create_user_qp(struct mlx5_ib_dev
*dev
, struct ib_pd
*pd
,
736 struct mlx5_ib_qp
*qp
, struct ib_udata
*udata
,
737 struct ib_qp_init_attr
*attr
,
739 struct mlx5_ib_create_qp_resp
*resp
, int *inlen
,
740 struct mlx5_ib_qp_base
*base
)
742 struct mlx5_ib_ucontext
*context
;
743 struct mlx5_ib_create_qp ucmd
;
744 struct mlx5_ib_ubuffer
*ubuffer
= &base
->ubuffer
;
755 err
= ib_copy_from_udata(&ucmd
, udata
, sizeof(ucmd
));
757 mlx5_ib_dbg(dev
, "copy failed\n");
761 context
= to_mucontext(pd
->uobject
->context
);
763 * TBD: should come from the verbs when we have the API
765 if (qp
->flags
& MLX5_IB_QP_CROSS_CHANNEL
)
766 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
767 uuarn
= MLX5_CROSS_CHANNEL_UUAR
;
769 uuarn
= alloc_uuar(&context
->uuari
, MLX5_IB_LATENCY_CLASS_HIGH
);
771 mlx5_ib_dbg(dev
, "failed to allocate low latency UUAR\n");
772 mlx5_ib_dbg(dev
, "reverting to medium latency\n");
773 uuarn
= alloc_uuar(&context
->uuari
, MLX5_IB_LATENCY_CLASS_MEDIUM
);
775 mlx5_ib_dbg(dev
, "failed to allocate medium latency UUAR\n");
776 mlx5_ib_dbg(dev
, "reverting to high latency\n");
777 uuarn
= alloc_uuar(&context
->uuari
, MLX5_IB_LATENCY_CLASS_LOW
);
779 mlx5_ib_warn(dev
, "uuar allocation failed\n");
786 uar_index
= uuarn_to_uar_index(&context
->uuari
, uuarn
);
787 mlx5_ib_dbg(dev
, "uuarn 0x%x, uar_index 0x%x\n", uuarn
, uar_index
);
790 qp
->sq
.wqe_shift
= ilog2(MLX5_SEND_WQE_BB
);
791 qp
->sq
.offset
= qp
->rq
.wqe_cnt
<< qp
->rq
.wqe_shift
;
793 err
= set_user_buf_size(dev
, qp
, &ucmd
, base
, attr
);
797 if (ucmd
.buf_addr
&& ubuffer
->buf_size
) {
798 ubuffer
->buf_addr
= ucmd
.buf_addr
;
799 err
= mlx5_ib_umem_get(dev
, pd
, ubuffer
->buf_addr
,
801 &ubuffer
->umem
, &npages
, &page_shift
,
806 ubuffer
->umem
= NULL
;
809 *inlen
= MLX5_ST_SZ_BYTES(create_qp_in
) +
810 MLX5_FLD_SZ_BYTES(create_qp_in
, pas
[0]) * ncont
;
811 *in
= mlx5_vzalloc(*inlen
);
817 pas
= (__be64
*)MLX5_ADDR_OF(create_qp_in
, *in
, pas
);
819 mlx5_ib_populate_pas(dev
, ubuffer
->umem
, page_shift
, pas
, 0);
821 qpc
= MLX5_ADDR_OF(create_qp_in
, *in
, qpc
);
823 MLX5_SET(qpc
, qpc
, log_page_size
, page_shift
- MLX5_ADAPTER_PAGE_SHIFT
);
824 MLX5_SET(qpc
, qpc
, page_offset
, offset
);
826 MLX5_SET(qpc
, qpc
, uar_page
, uar_index
);
827 resp
->uuar_index
= uuarn
;
830 err
= mlx5_ib_db_map_user(context
, ucmd
.db_addr
, &qp
->db
);
832 mlx5_ib_dbg(dev
, "map failed\n");
836 err
= ib_copy_to_udata(udata
, resp
, sizeof(*resp
));
838 mlx5_ib_dbg(dev
, "copy failed\n");
841 qp
->create_type
= MLX5_QP_USER
;
846 mlx5_ib_db_unmap_user(context
, &qp
->db
);
853 ib_umem_release(ubuffer
->umem
);
856 free_uuar(&context
->uuari
, uuarn
);
860 static void destroy_qp_user(struct ib_pd
*pd
, struct mlx5_ib_qp
*qp
,
861 struct mlx5_ib_qp_base
*base
)
863 struct mlx5_ib_ucontext
*context
;
865 context
= to_mucontext(pd
->uobject
->context
);
866 mlx5_ib_db_unmap_user(context
, &qp
->db
);
867 if (base
->ubuffer
.umem
)
868 ib_umem_release(base
->ubuffer
.umem
);
869 free_uuar(&context
->uuari
, qp
->uuarn
);
872 static int create_kernel_qp(struct mlx5_ib_dev
*dev
,
873 struct ib_qp_init_attr
*init_attr
,
874 struct mlx5_ib_qp
*qp
,
875 u32
**in
, int *inlen
,
876 struct mlx5_ib_qp_base
*base
)
878 enum mlx5_ib_latency_class lc
= MLX5_IB_LATENCY_CLASS_LOW
;
879 struct mlx5_uuar_info
*uuari
;
885 uuari
= &dev
->mdev
->priv
.uuari
;
886 if (init_attr
->create_flags
& ~(IB_QP_CREATE_SIGNATURE_EN
|
887 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK
|
888 IB_QP_CREATE_IPOIB_UD_LSO
|
889 mlx5_ib_create_qp_sqpn_qp1()))
892 if (init_attr
->qp_type
== MLX5_IB_QPT_REG_UMR
)
893 lc
= MLX5_IB_LATENCY_CLASS_FAST_PATH
;
895 uuarn
= alloc_uuar(uuari
, lc
);
897 mlx5_ib_dbg(dev
, "\n");
901 qp
->bf
= &uuari
->bfs
[uuarn
];
902 uar_index
= qp
->bf
->uar
->index
;
904 err
= calc_sq_size(dev
, init_attr
, qp
);
906 mlx5_ib_dbg(dev
, "err %d\n", err
);
911 qp
->sq
.offset
= qp
->rq
.wqe_cnt
<< qp
->rq
.wqe_shift
;
912 base
->ubuffer
.buf_size
= err
+ (qp
->rq
.wqe_cnt
<< qp
->rq
.wqe_shift
);
914 err
= mlx5_buf_alloc(dev
->mdev
, base
->ubuffer
.buf_size
, &qp
->buf
);
916 mlx5_ib_dbg(dev
, "err %d\n", err
);
920 qp
->sq
.qend
= mlx5_get_send_wqe(qp
, qp
->sq
.wqe_cnt
);
921 *inlen
= MLX5_ST_SZ_BYTES(create_qp_in
) +
922 MLX5_FLD_SZ_BYTES(create_qp_in
, pas
[0]) * qp
->buf
.npages
;
923 *in
= mlx5_vzalloc(*inlen
);
929 qpc
= MLX5_ADDR_OF(create_qp_in
, *in
, qpc
);
930 MLX5_SET(qpc
, qpc
, uar_page
, uar_index
);
931 MLX5_SET(qpc
, qpc
, log_page_size
, qp
->buf
.page_shift
- MLX5_ADAPTER_PAGE_SHIFT
);
933 /* Set "fast registration enabled" for all kernel QPs */
934 MLX5_SET(qpc
, qpc
, fre
, 1);
935 MLX5_SET(qpc
, qpc
, rlky
, 1);
937 if (init_attr
->create_flags
& mlx5_ib_create_qp_sqpn_qp1()) {
938 MLX5_SET(qpc
, qpc
, deth_sqpn
, 1);
939 qp
->flags
|= MLX5_IB_QP_SQPN_QP1
;
942 mlx5_fill_page_array(&qp
->buf
,
943 (__be64
*)MLX5_ADDR_OF(create_qp_in
, *in
, pas
));
945 err
= mlx5_db_alloc(dev
->mdev
, &qp
->db
);
947 mlx5_ib_dbg(dev
, "err %d\n", err
);
951 qp
->sq
.wrid
= kmalloc(qp
->sq
.wqe_cnt
* sizeof(*qp
->sq
.wrid
), GFP_KERNEL
);
952 qp
->sq
.wr_data
= kmalloc(qp
->sq
.wqe_cnt
* sizeof(*qp
->sq
.wr_data
), GFP_KERNEL
);
953 qp
->rq
.wrid
= kmalloc(qp
->rq
.wqe_cnt
* sizeof(*qp
->rq
.wrid
), GFP_KERNEL
);
954 qp
->sq
.w_list
= kmalloc(qp
->sq
.wqe_cnt
* sizeof(*qp
->sq
.w_list
), GFP_KERNEL
);
955 qp
->sq
.wqe_head
= kmalloc(qp
->sq
.wqe_cnt
* sizeof(*qp
->sq
.wqe_head
), GFP_KERNEL
);
957 if (!qp
->sq
.wrid
|| !qp
->sq
.wr_data
|| !qp
->rq
.wrid
||
958 !qp
->sq
.w_list
|| !qp
->sq
.wqe_head
) {
962 qp
->create_type
= MLX5_QP_KERNEL
;
967 mlx5_db_free(dev
->mdev
, &qp
->db
);
968 kfree(qp
->sq
.wqe_head
);
969 kfree(qp
->sq
.w_list
);
971 kfree(qp
->sq
.wr_data
);
978 mlx5_buf_free(dev
->mdev
, &qp
->buf
);
981 free_uuar(&dev
->mdev
->priv
.uuari
, uuarn
);
985 static void destroy_qp_kernel(struct mlx5_ib_dev
*dev
, struct mlx5_ib_qp
*qp
)
987 mlx5_db_free(dev
->mdev
, &qp
->db
);
988 kfree(qp
->sq
.wqe_head
);
989 kfree(qp
->sq
.w_list
);
991 kfree(qp
->sq
.wr_data
);
993 mlx5_buf_free(dev
->mdev
, &qp
->buf
);
994 free_uuar(&dev
->mdev
->priv
.uuari
, qp
->bf
->uuarn
);
997 static u32
get_rx_type(struct mlx5_ib_qp
*qp
, struct ib_qp_init_attr
*attr
)
999 if (attr
->srq
|| (attr
->qp_type
== IB_QPT_XRC_TGT
) ||
1000 (attr
->qp_type
== IB_QPT_XRC_INI
))
1002 else if (!qp
->has_rq
)
1003 return MLX5_ZERO_LEN_RQ
;
1005 return MLX5_NON_ZERO_RQ
;
1008 static int is_connected(enum ib_qp_type qp_type
)
1010 if (qp_type
== IB_QPT_RC
|| qp_type
== IB_QPT_UC
)
1016 static int create_raw_packet_qp_tis(struct mlx5_ib_dev
*dev
,
1017 struct mlx5_ib_sq
*sq
, u32 tdn
)
1019 u32 in
[MLX5_ST_SZ_DW(create_tis_in
)] = {0};
1020 void *tisc
= MLX5_ADDR_OF(create_tis_in
, in
, ctx
);
1022 MLX5_SET(tisc
, tisc
, transport_domain
, tdn
);
1023 return mlx5_core_create_tis(dev
->mdev
, in
, sizeof(in
), &sq
->tisn
);
1026 static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev
*dev
,
1027 struct mlx5_ib_sq
*sq
)
1029 mlx5_core_destroy_tis(dev
->mdev
, sq
->tisn
);
1032 static int create_raw_packet_qp_sq(struct mlx5_ib_dev
*dev
,
1033 struct mlx5_ib_sq
*sq
, void *qpin
,
1036 struct mlx5_ib_ubuffer
*ubuffer
= &sq
->ubuffer
;
1040 void *qpc
= MLX5_ADDR_OF(create_qp_in
, qpin
, qpc
);
1049 err
= mlx5_ib_umem_get(dev
, pd
, ubuffer
->buf_addr
, ubuffer
->buf_size
,
1050 &sq
->ubuffer
.umem
, &npages
, &page_shift
,
1055 inlen
= MLX5_ST_SZ_BYTES(create_sq_in
) + sizeof(u64
) * ncont
;
1056 in
= mlx5_vzalloc(inlen
);
1062 sqc
= MLX5_ADDR_OF(create_sq_in
, in
, ctx
);
1063 MLX5_SET(sqc
, sqc
, flush_in_error_en
, 1);
1064 MLX5_SET(sqc
, sqc
, state
, MLX5_SQC_STATE_RST
);
1065 MLX5_SET(sqc
, sqc
, user_index
, MLX5_GET(qpc
, qpc
, user_index
));
1066 MLX5_SET(sqc
, sqc
, cqn
, MLX5_GET(qpc
, qpc
, cqn_snd
));
1067 MLX5_SET(sqc
, sqc
, tis_lst_sz
, 1);
1068 MLX5_SET(sqc
, sqc
, tis_num_0
, sq
->tisn
);
1070 wq
= MLX5_ADDR_OF(sqc
, sqc
, wq
);
1071 MLX5_SET(wq
, wq
, wq_type
, MLX5_WQ_TYPE_CYCLIC
);
1072 MLX5_SET(wq
, wq
, pd
, MLX5_GET(qpc
, qpc
, pd
));
1073 MLX5_SET(wq
, wq
, uar_page
, MLX5_GET(qpc
, qpc
, uar_page
));
1074 MLX5_SET64(wq
, wq
, dbr_addr
, MLX5_GET64(qpc
, qpc
, dbr_addr
));
1075 MLX5_SET(wq
, wq
, log_wq_stride
, ilog2(MLX5_SEND_WQE_BB
));
1076 MLX5_SET(wq
, wq
, log_wq_sz
, MLX5_GET(qpc
, qpc
, log_sq_size
));
1077 MLX5_SET(wq
, wq
, log_wq_pg_sz
, page_shift
- MLX5_ADAPTER_PAGE_SHIFT
);
1078 MLX5_SET(wq
, wq
, page_offset
, offset
);
1080 pas
= (__be64
*)MLX5_ADDR_OF(wq
, wq
, pas
);
1081 mlx5_ib_populate_pas(dev
, sq
->ubuffer
.umem
, page_shift
, pas
, 0);
1083 err
= mlx5_core_create_sq_tracked(dev
->mdev
, in
, inlen
, &sq
->base
.mqp
);
1093 ib_umem_release(sq
->ubuffer
.umem
);
1094 sq
->ubuffer
.umem
= NULL
;
1099 static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev
*dev
,
1100 struct mlx5_ib_sq
*sq
)
1102 mlx5_core_destroy_sq_tracked(dev
->mdev
, &sq
->base
.mqp
);
1103 ib_umem_release(sq
->ubuffer
.umem
);
1106 static int get_rq_pas_size(void *qpc
)
1108 u32 log_page_size
= MLX5_GET(qpc
, qpc
, log_page_size
) + 12;
1109 u32 log_rq_stride
= MLX5_GET(qpc
, qpc
, log_rq_stride
);
1110 u32 log_rq_size
= MLX5_GET(qpc
, qpc
, log_rq_size
);
1111 u32 page_offset
= MLX5_GET(qpc
, qpc
, page_offset
);
1112 u32 po_quanta
= 1 << (log_page_size
- 6);
1113 u32 rq_sz
= 1 << (log_rq_size
+ 4 + log_rq_stride
);
1114 u32 page_size
= 1 << log_page_size
;
1115 u32 rq_sz_po
= rq_sz
+ (page_offset
* po_quanta
);
1116 u32 rq_num_pas
= (rq_sz_po
+ page_size
- 1) / page_size
;
1118 return rq_num_pas
* sizeof(u64
);
1121 static int create_raw_packet_qp_rq(struct mlx5_ib_dev
*dev
,
1122 struct mlx5_ib_rq
*rq
, void *qpin
)
1124 struct mlx5_ib_qp
*mqp
= rq
->base
.container_mibqp
;
1130 void *qpc
= MLX5_ADDR_OF(create_qp_in
, qpin
, qpc
);
1133 u32 rq_pas_size
= get_rq_pas_size(qpc
);
1135 inlen
= MLX5_ST_SZ_BYTES(create_rq_in
) + rq_pas_size
;
1136 in
= mlx5_vzalloc(inlen
);
1140 rqc
= MLX5_ADDR_OF(create_rq_in
, in
, ctx
);
1141 MLX5_SET(rqc
, rqc
, vsd
, 1);
1142 MLX5_SET(rqc
, rqc
, mem_rq_type
, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE
);
1143 MLX5_SET(rqc
, rqc
, state
, MLX5_RQC_STATE_RST
);
1144 MLX5_SET(rqc
, rqc
, flush_in_error_en
, 1);
1145 MLX5_SET(rqc
, rqc
, user_index
, MLX5_GET(qpc
, qpc
, user_index
));
1146 MLX5_SET(rqc
, rqc
, cqn
, MLX5_GET(qpc
, qpc
, cqn_rcv
));
1148 if (mqp
->flags
& MLX5_IB_QP_CAP_SCATTER_FCS
)
1149 MLX5_SET(rqc
, rqc
, scatter_fcs
, 1);
1151 wq
= MLX5_ADDR_OF(rqc
, rqc
, wq
);
1152 MLX5_SET(wq
, wq
, wq_type
, MLX5_WQ_TYPE_CYCLIC
);
1153 MLX5_SET(wq
, wq
, end_padding_mode
,
1154 MLX5_GET(qpc
, qpc
, end_padding_mode
));
1155 MLX5_SET(wq
, wq
, page_offset
, MLX5_GET(qpc
, qpc
, page_offset
));
1156 MLX5_SET(wq
, wq
, pd
, MLX5_GET(qpc
, qpc
, pd
));
1157 MLX5_SET64(wq
, wq
, dbr_addr
, MLX5_GET64(qpc
, qpc
, dbr_addr
));
1158 MLX5_SET(wq
, wq
, log_wq_stride
, MLX5_GET(qpc
, qpc
, log_rq_stride
) + 4);
1159 MLX5_SET(wq
, wq
, log_wq_pg_sz
, MLX5_GET(qpc
, qpc
, log_page_size
));
1160 MLX5_SET(wq
, wq
, log_wq_sz
, MLX5_GET(qpc
, qpc
, log_rq_size
));
1162 pas
= (__be64
*)MLX5_ADDR_OF(wq
, wq
, pas
);
1163 qp_pas
= (__be64
*)MLX5_ADDR_OF(create_qp_in
, qpin
, pas
);
1164 memcpy(pas
, qp_pas
, rq_pas_size
);
1166 err
= mlx5_core_create_rq_tracked(dev
->mdev
, in
, inlen
, &rq
->base
.mqp
);
1173 static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev
*dev
,
1174 struct mlx5_ib_rq
*rq
)
1176 mlx5_core_destroy_rq_tracked(dev
->mdev
, &rq
->base
.mqp
);
1179 static int create_raw_packet_qp_tir(struct mlx5_ib_dev
*dev
,
1180 struct mlx5_ib_rq
*rq
, u32 tdn
)
1187 inlen
= MLX5_ST_SZ_BYTES(create_tir_in
);
1188 in
= mlx5_vzalloc(inlen
);
1192 tirc
= MLX5_ADDR_OF(create_tir_in
, in
, ctx
);
1193 MLX5_SET(tirc
, tirc
, disp_type
, MLX5_TIRC_DISP_TYPE_DIRECT
);
1194 MLX5_SET(tirc
, tirc
, inline_rqn
, rq
->base
.mqp
.qpn
);
1195 MLX5_SET(tirc
, tirc
, transport_domain
, tdn
);
1197 err
= mlx5_core_create_tir(dev
->mdev
, in
, inlen
, &rq
->tirn
);
1204 static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev
*dev
,
1205 struct mlx5_ib_rq
*rq
)
1207 mlx5_core_destroy_tir(dev
->mdev
, rq
->tirn
);
1210 static int create_raw_packet_qp(struct mlx5_ib_dev
*dev
, struct mlx5_ib_qp
*qp
,
1214 struct mlx5_ib_raw_packet_qp
*raw_packet_qp
= &qp
->raw_packet_qp
;
1215 struct mlx5_ib_sq
*sq
= &raw_packet_qp
->sq
;
1216 struct mlx5_ib_rq
*rq
= &raw_packet_qp
->rq
;
1217 struct ib_uobject
*uobj
= pd
->uobject
;
1218 struct ib_ucontext
*ucontext
= uobj
->context
;
1219 struct mlx5_ib_ucontext
*mucontext
= to_mucontext(ucontext
);
1221 u32 tdn
= mucontext
->tdn
;
1223 if (qp
->sq
.wqe_cnt
) {
1224 err
= create_raw_packet_qp_tis(dev
, sq
, tdn
);
1228 err
= create_raw_packet_qp_sq(dev
, sq
, in
, pd
);
1230 goto err_destroy_tis
;
1232 sq
->base
.container_mibqp
= qp
;
1235 if (qp
->rq
.wqe_cnt
) {
1236 rq
->base
.container_mibqp
= qp
;
1238 err
= create_raw_packet_qp_rq(dev
, rq
, in
);
1240 goto err_destroy_sq
;
1243 err
= create_raw_packet_qp_tir(dev
, rq
, tdn
);
1245 goto err_destroy_rq
;
1248 qp
->trans_qp
.base
.mqp
.qpn
= qp
->sq
.wqe_cnt
? sq
->base
.mqp
.qpn
:
1254 destroy_raw_packet_qp_rq(dev
, rq
);
1256 if (!qp
->sq
.wqe_cnt
)
1258 destroy_raw_packet_qp_sq(dev
, sq
);
1260 destroy_raw_packet_qp_tis(dev
, sq
);
1265 static void destroy_raw_packet_qp(struct mlx5_ib_dev
*dev
,
1266 struct mlx5_ib_qp
*qp
)
1268 struct mlx5_ib_raw_packet_qp
*raw_packet_qp
= &qp
->raw_packet_qp
;
1269 struct mlx5_ib_sq
*sq
= &raw_packet_qp
->sq
;
1270 struct mlx5_ib_rq
*rq
= &raw_packet_qp
->rq
;
1272 if (qp
->rq
.wqe_cnt
) {
1273 destroy_raw_packet_qp_tir(dev
, rq
);
1274 destroy_raw_packet_qp_rq(dev
, rq
);
1277 if (qp
->sq
.wqe_cnt
) {
1278 destroy_raw_packet_qp_sq(dev
, sq
);
1279 destroy_raw_packet_qp_tis(dev
, sq
);
1283 static void raw_packet_qp_copy_info(struct mlx5_ib_qp
*qp
,
1284 struct mlx5_ib_raw_packet_qp
*raw_packet_qp
)
1286 struct mlx5_ib_sq
*sq
= &raw_packet_qp
->sq
;
1287 struct mlx5_ib_rq
*rq
= &raw_packet_qp
->rq
;
1291 sq
->doorbell
= &qp
->db
;
1292 rq
->doorbell
= &qp
->db
;
1295 static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev
*dev
, struct mlx5_ib_qp
*qp
)
1297 mlx5_core_destroy_tir(dev
->mdev
, qp
->rss_qp
.tirn
);
1300 static int create_rss_raw_qp_tir(struct mlx5_ib_dev
*dev
, struct mlx5_ib_qp
*qp
,
1302 struct ib_qp_init_attr
*init_attr
,
1303 struct ib_udata
*udata
)
1305 struct ib_uobject
*uobj
= pd
->uobject
;
1306 struct ib_ucontext
*ucontext
= uobj
->context
;
1307 struct mlx5_ib_ucontext
*mucontext
= to_mucontext(ucontext
);
1308 struct mlx5_ib_create_qp_resp resp
= {};
1314 u32 selected_fields
= 0;
1315 size_t min_resp_len
;
1316 u32 tdn
= mucontext
->tdn
;
1317 struct mlx5_ib_create_qp_rss ucmd
= {};
1318 size_t required_cmd_sz
;
1320 if (init_attr
->qp_type
!= IB_QPT_RAW_PACKET
)
1323 if (init_attr
->create_flags
|| init_attr
->send_cq
)
1326 min_resp_len
= offsetof(typeof(resp
), uuar_index
) + sizeof(resp
.uuar_index
);
1327 if (udata
->outlen
< min_resp_len
)
1330 required_cmd_sz
= offsetof(typeof(ucmd
), reserved1
) + sizeof(ucmd
.reserved1
);
1331 if (udata
->inlen
< required_cmd_sz
) {
1332 mlx5_ib_dbg(dev
, "invalid inlen\n");
1336 if (udata
->inlen
> sizeof(ucmd
) &&
1337 !ib_is_udata_cleared(udata
, sizeof(ucmd
),
1338 udata
->inlen
- sizeof(ucmd
))) {
1339 mlx5_ib_dbg(dev
, "inlen is not supported\n");
1343 if (ib_copy_from_udata(&ucmd
, udata
, min(sizeof(ucmd
), udata
->inlen
))) {
1344 mlx5_ib_dbg(dev
, "copy failed\n");
1348 if (ucmd
.comp_mask
) {
1349 mlx5_ib_dbg(dev
, "invalid comp mask\n");
1353 if (memchr_inv(ucmd
.reserved
, 0, sizeof(ucmd
.reserved
)) || ucmd
.reserved1
) {
1354 mlx5_ib_dbg(dev
, "invalid reserved\n");
1358 err
= ib_copy_to_udata(udata
, &resp
, min_resp_len
);
1360 mlx5_ib_dbg(dev
, "copy failed\n");
1364 inlen
= MLX5_ST_SZ_BYTES(create_tir_in
);
1365 in
= mlx5_vzalloc(inlen
);
1369 tirc
= MLX5_ADDR_OF(create_tir_in
, in
, ctx
);
1370 MLX5_SET(tirc
, tirc
, disp_type
,
1371 MLX5_TIRC_DISP_TYPE_INDIRECT
);
1372 MLX5_SET(tirc
, tirc
, indirect_table
,
1373 init_attr
->rwq_ind_tbl
->ind_tbl_num
);
1374 MLX5_SET(tirc
, tirc
, transport_domain
, tdn
);
1376 hfso
= MLX5_ADDR_OF(tirc
, tirc
, rx_hash_field_selector_outer
);
1377 switch (ucmd
.rx_hash_function
) {
1378 case MLX5_RX_HASH_FUNC_TOEPLITZ
:
1380 void *rss_key
= MLX5_ADDR_OF(tirc
, tirc
, rx_hash_toeplitz_key
);
1381 size_t len
= MLX5_FLD_SZ_BYTES(tirc
, rx_hash_toeplitz_key
);
1383 if (len
!= ucmd
.rx_key_len
) {
1388 MLX5_SET(tirc
, tirc
, rx_hash_fn
, MLX5_RX_HASH_FN_TOEPLITZ
);
1389 MLX5_SET(tirc
, tirc
, rx_hash_symmetric
, 1);
1390 memcpy(rss_key
, ucmd
.rx_hash_key
, len
);
1398 if (!ucmd
.rx_hash_fields_mask
) {
1399 /* special case when this TIR serves as steering entry without hashing */
1400 if (!init_attr
->rwq_ind_tbl
->log_ind_tbl_size
)
1406 if (((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_IPV4
) ||
1407 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_IPV4
)) &&
1408 ((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_IPV6
) ||
1409 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_IPV6
))) {
1414 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1415 if ((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_IPV4
) ||
1416 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_IPV4
))
1417 MLX5_SET(rx_hash_field_select
, hfso
, l3_prot_type
,
1418 MLX5_L3_PROT_TYPE_IPV4
);
1419 else if ((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_IPV6
) ||
1420 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_IPV6
))
1421 MLX5_SET(rx_hash_field_select
, hfso
, l3_prot_type
,
1422 MLX5_L3_PROT_TYPE_IPV6
);
1424 if (((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_PORT_TCP
) ||
1425 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_PORT_TCP
)) &&
1426 ((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_PORT_UDP
) ||
1427 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_PORT_UDP
))) {
1432 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1433 if ((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_PORT_TCP
) ||
1434 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_PORT_TCP
))
1435 MLX5_SET(rx_hash_field_select
, hfso
, l4_prot_type
,
1436 MLX5_L4_PROT_TYPE_TCP
);
1437 else if ((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_PORT_UDP
) ||
1438 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_PORT_UDP
))
1439 MLX5_SET(rx_hash_field_select
, hfso
, l4_prot_type
,
1440 MLX5_L4_PROT_TYPE_UDP
);
1442 if ((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_IPV4
) ||
1443 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_IPV6
))
1444 selected_fields
|= MLX5_HASH_FIELD_SEL_SRC_IP
;
1446 if ((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_IPV4
) ||
1447 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_IPV6
))
1448 selected_fields
|= MLX5_HASH_FIELD_SEL_DST_IP
;
1450 if ((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_PORT_TCP
) ||
1451 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_SRC_PORT_UDP
))
1452 selected_fields
|= MLX5_HASH_FIELD_SEL_L4_SPORT
;
1454 if ((ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_PORT_TCP
) ||
1455 (ucmd
.rx_hash_fields_mask
& MLX5_RX_HASH_DST_PORT_UDP
))
1456 selected_fields
|= MLX5_HASH_FIELD_SEL_L4_DPORT
;
1458 MLX5_SET(rx_hash_field_select
, hfso
, selected_fields
, selected_fields
);
1461 err
= mlx5_core_create_tir(dev
->mdev
, in
, inlen
, &qp
->rss_qp
.tirn
);
1467 /* qpn is reserved for that QP */
1468 qp
->trans_qp
.base
.mqp
.qpn
= 0;
1469 qp
->flags
|= MLX5_IB_QP_RSS
;
1477 static int create_qp_common(struct mlx5_ib_dev
*dev
, struct ib_pd
*pd
,
1478 struct ib_qp_init_attr
*init_attr
,
1479 struct ib_udata
*udata
, struct mlx5_ib_qp
*qp
)
1481 struct mlx5_ib_resources
*devr
= &dev
->devr
;
1482 int inlen
= MLX5_ST_SZ_BYTES(create_qp_in
);
1483 struct mlx5_core_dev
*mdev
= dev
->mdev
;
1484 struct mlx5_ib_create_qp_resp resp
;
1485 struct mlx5_ib_cq
*send_cq
;
1486 struct mlx5_ib_cq
*recv_cq
;
1487 unsigned long flags
;
1488 u32 uidx
= MLX5_IB_DEFAULT_UIDX
;
1489 struct mlx5_ib_create_qp ucmd
;
1490 struct mlx5_ib_qp_base
*base
;
1495 base
= init_attr
->qp_type
== IB_QPT_RAW_PACKET
?
1496 &qp
->raw_packet_qp
.rq
.base
:
1499 if (init_attr
->qp_type
!= IB_QPT_RAW_PACKET
)
1500 mlx5_ib_odp_create_qp(qp
);
1502 mutex_init(&qp
->mutex
);
1503 spin_lock_init(&qp
->sq
.lock
);
1504 spin_lock_init(&qp
->rq
.lock
);
1506 if (init_attr
->rwq_ind_tbl
) {
1510 err
= create_rss_raw_qp_tir(dev
, qp
, pd
, init_attr
, udata
);
1514 if (init_attr
->create_flags
& IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK
) {
1515 if (!MLX5_CAP_GEN(mdev
, block_lb_mc
)) {
1516 mlx5_ib_dbg(dev
, "block multicast loopback isn't supported\n");
1519 qp
->flags
|= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK
;
1523 if (init_attr
->create_flags
&
1524 (IB_QP_CREATE_CROSS_CHANNEL
|
1525 IB_QP_CREATE_MANAGED_SEND
|
1526 IB_QP_CREATE_MANAGED_RECV
)) {
1527 if (!MLX5_CAP_GEN(mdev
, cd
)) {
1528 mlx5_ib_dbg(dev
, "cross-channel isn't supported\n");
1531 if (init_attr
->create_flags
& IB_QP_CREATE_CROSS_CHANNEL
)
1532 qp
->flags
|= MLX5_IB_QP_CROSS_CHANNEL
;
1533 if (init_attr
->create_flags
& IB_QP_CREATE_MANAGED_SEND
)
1534 qp
->flags
|= MLX5_IB_QP_MANAGED_SEND
;
1535 if (init_attr
->create_flags
& IB_QP_CREATE_MANAGED_RECV
)
1536 qp
->flags
|= MLX5_IB_QP_MANAGED_RECV
;
1539 if (init_attr
->qp_type
== IB_QPT_UD
&&
1540 (init_attr
->create_flags
& IB_QP_CREATE_IPOIB_UD_LSO
))
1541 if (!MLX5_CAP_GEN(mdev
, ipoib_basic_offloads
)) {
1542 mlx5_ib_dbg(dev
, "ipoib UD lso qp isn't supported\n");
1546 if (init_attr
->create_flags
& IB_QP_CREATE_SCATTER_FCS
) {
1547 if (init_attr
->qp_type
!= IB_QPT_RAW_PACKET
) {
1548 mlx5_ib_dbg(dev
, "Scatter FCS is supported only for Raw Packet QPs");
1551 if (!MLX5_CAP_GEN(dev
->mdev
, eth_net_offloads
) ||
1552 !MLX5_CAP_ETH(dev
->mdev
, scatter_fcs
)) {
1553 mlx5_ib_dbg(dev
, "Scatter FCS isn't supported\n");
1556 qp
->flags
|= MLX5_IB_QP_CAP_SCATTER_FCS
;
1559 if (init_attr
->sq_sig_type
== IB_SIGNAL_ALL_WR
)
1560 qp
->sq_signal_bits
= MLX5_WQE_CTRL_CQ_UPDATE
;
1562 if (pd
&& pd
->uobject
) {
1563 if (ib_copy_from_udata(&ucmd
, udata
, sizeof(ucmd
))) {
1564 mlx5_ib_dbg(dev
, "copy failed\n");
1568 err
= get_qp_user_index(to_mucontext(pd
->uobject
->context
),
1569 &ucmd
, udata
->inlen
, &uidx
);
1573 qp
->wq_sig
= !!(ucmd
.flags
& MLX5_QP_FLAG_SIGNATURE
);
1574 qp
->scat_cqe
= !!(ucmd
.flags
& MLX5_QP_FLAG_SCATTER_CQE
);
1576 qp
->wq_sig
= !!wq_signature
;
1579 qp
->has_rq
= qp_has_rq(init_attr
);
1580 err
= set_rq_size(dev
, &init_attr
->cap
, qp
->has_rq
,
1581 qp
, (pd
&& pd
->uobject
) ? &ucmd
: NULL
);
1583 mlx5_ib_dbg(dev
, "err %d\n", err
);
1590 1 << MLX5_CAP_GEN(mdev
, log_max_qp_sz
);
1591 mlx5_ib_dbg(dev
, "requested sq_wqe_count (%d)\n", ucmd
.sq_wqe_count
);
1592 if (ucmd
.rq_wqe_shift
!= qp
->rq
.wqe_shift
||
1593 ucmd
.rq_wqe_count
!= qp
->rq
.wqe_cnt
) {
1594 mlx5_ib_dbg(dev
, "invalid rq params\n");
1597 if (ucmd
.sq_wqe_count
> max_wqes
) {
1598 mlx5_ib_dbg(dev
, "requested sq_wqe_count (%d) > max allowed (%d)\n",
1599 ucmd
.sq_wqe_count
, max_wqes
);
1602 if (init_attr
->create_flags
&
1603 mlx5_ib_create_qp_sqpn_qp1()) {
1604 mlx5_ib_dbg(dev
, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1607 err
= create_user_qp(dev
, pd
, qp
, udata
, init_attr
, &in
,
1608 &resp
, &inlen
, base
);
1610 mlx5_ib_dbg(dev
, "err %d\n", err
);
1612 err
= create_kernel_qp(dev
, init_attr
, qp
, &in
, &inlen
,
1615 mlx5_ib_dbg(dev
, "err %d\n", err
);
1621 in
= mlx5_vzalloc(inlen
);
1625 qp
->create_type
= MLX5_QP_EMPTY
;
1628 if (is_sqp(init_attr
->qp_type
))
1629 qp
->port
= init_attr
->port_num
;
1631 qpc
= MLX5_ADDR_OF(create_qp_in
, in
, qpc
);
1633 MLX5_SET(qpc
, qpc
, st
, to_mlx5_st(init_attr
->qp_type
));
1634 MLX5_SET(qpc
, qpc
, pm_state
, MLX5_QP_PM_MIGRATED
);
1636 if (init_attr
->qp_type
!= MLX5_IB_QPT_REG_UMR
)
1637 MLX5_SET(qpc
, qpc
, pd
, to_mpd(pd
? pd
: devr
->p0
)->pdn
);
1639 MLX5_SET(qpc
, qpc
, latency_sensitive
, 1);
1643 MLX5_SET(qpc
, qpc
, wq_signature
, 1);
1645 if (qp
->flags
& MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK
)
1646 MLX5_SET(qpc
, qpc
, block_lb_mc
, 1);
1648 if (qp
->flags
& MLX5_IB_QP_CROSS_CHANNEL
)
1649 MLX5_SET(qpc
, qpc
, cd_master
, 1);
1650 if (qp
->flags
& MLX5_IB_QP_MANAGED_SEND
)
1651 MLX5_SET(qpc
, qpc
, cd_slave_send
, 1);
1652 if (qp
->flags
& MLX5_IB_QP_MANAGED_RECV
)
1653 MLX5_SET(qpc
, qpc
, cd_slave_receive
, 1);
1655 if (qp
->scat_cqe
&& is_connected(init_attr
->qp_type
)) {
1659 rcqe_sz
= mlx5_ib_get_cqe_size(dev
, init_attr
->recv_cq
);
1660 scqe_sz
= mlx5_ib_get_cqe_size(dev
, init_attr
->send_cq
);
1663 MLX5_SET(qpc
, qpc
, cs_res
, MLX5_RES_SCAT_DATA64_CQE
);
1665 MLX5_SET(qpc
, qpc
, cs_res
, MLX5_RES_SCAT_DATA32_CQE
);
1667 if (init_attr
->sq_sig_type
== IB_SIGNAL_ALL_WR
) {
1669 MLX5_SET(qpc
, qpc
, cs_req
, MLX5_REQ_SCAT_DATA64_CQE
);
1671 MLX5_SET(qpc
, qpc
, cs_req
, MLX5_REQ_SCAT_DATA32_CQE
);
1675 if (qp
->rq
.wqe_cnt
) {
1676 MLX5_SET(qpc
, qpc
, log_rq_stride
, qp
->rq
.wqe_shift
- 4);
1677 MLX5_SET(qpc
, qpc
, log_rq_size
, ilog2(qp
->rq
.wqe_cnt
));
1680 MLX5_SET(qpc
, qpc
, rq_type
, get_rx_type(qp
, init_attr
));
1683 MLX5_SET(qpc
, qpc
, log_sq_size
, ilog2(qp
->sq
.wqe_cnt
));
1685 MLX5_SET(qpc
, qpc
, no_sq
, 1);
1687 /* Set default resources */
1688 switch (init_attr
->qp_type
) {
1689 case IB_QPT_XRC_TGT
:
1690 MLX5_SET(qpc
, qpc
, cqn_rcv
, to_mcq(devr
->c0
)->mcq
.cqn
);
1691 MLX5_SET(qpc
, qpc
, cqn_snd
, to_mcq(devr
->c0
)->mcq
.cqn
);
1692 MLX5_SET(qpc
, qpc
, srqn_rmpn_xrqn
, to_msrq(devr
->s0
)->msrq
.srqn
);
1693 MLX5_SET(qpc
, qpc
, xrcd
, to_mxrcd(init_attr
->xrcd
)->xrcdn
);
1695 case IB_QPT_XRC_INI
:
1696 MLX5_SET(qpc
, qpc
, cqn_rcv
, to_mcq(devr
->c0
)->mcq
.cqn
);
1697 MLX5_SET(qpc
, qpc
, xrcd
, to_mxrcd(devr
->x1
)->xrcdn
);
1698 MLX5_SET(qpc
, qpc
, srqn_rmpn_xrqn
, to_msrq(devr
->s0
)->msrq
.srqn
);
1701 if (init_attr
->srq
) {
1702 MLX5_SET(qpc
, qpc
, xrcd
, to_mxrcd(devr
->x0
)->xrcdn
);
1703 MLX5_SET(qpc
, qpc
, srqn_rmpn_xrqn
, to_msrq(init_attr
->srq
)->msrq
.srqn
);
1705 MLX5_SET(qpc
, qpc
, xrcd
, to_mxrcd(devr
->x1
)->xrcdn
);
1706 MLX5_SET(qpc
, qpc
, srqn_rmpn_xrqn
, to_msrq(devr
->s1
)->msrq
.srqn
);
1710 if (init_attr
->send_cq
)
1711 MLX5_SET(qpc
, qpc
, cqn_snd
, to_mcq(init_attr
->send_cq
)->mcq
.cqn
);
1713 if (init_attr
->recv_cq
)
1714 MLX5_SET(qpc
, qpc
, cqn_rcv
, to_mcq(init_attr
->recv_cq
)->mcq
.cqn
);
1716 MLX5_SET64(qpc
, qpc
, dbr_addr
, qp
->db
.dma
);
1718 /* 0xffffff means we ask to work with cqe version 0 */
1719 if (MLX5_CAP_GEN(mdev
, cqe_version
) == MLX5_CQE_VERSION_V1
)
1720 MLX5_SET(qpc
, qpc
, user_index
, uidx
);
1722 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1723 if (init_attr
->qp_type
== IB_QPT_UD
&&
1724 (init_attr
->create_flags
& IB_QP_CREATE_IPOIB_UD_LSO
)) {
1725 MLX5_SET(qpc
, qpc
, ulp_stateless_offload_mode
, 1);
1726 qp
->flags
|= MLX5_IB_QP_LSO
;
1729 if (init_attr
->qp_type
== IB_QPT_RAW_PACKET
) {
1730 qp
->raw_packet_qp
.sq
.ubuffer
.buf_addr
= ucmd
.sq_buf_addr
;
1731 raw_packet_qp_copy_info(qp
, &qp
->raw_packet_qp
);
1732 err
= create_raw_packet_qp(dev
, qp
, in
, pd
);
1734 err
= mlx5_core_create_qp(dev
->mdev
, &base
->mqp
, in
, inlen
);
1738 mlx5_ib_dbg(dev
, "create qp failed\n");
1744 base
->container_mibqp
= qp
;
1745 base
->mqp
.event
= mlx5_ib_qp_event
;
1747 get_cqs(init_attr
->qp_type
, init_attr
->send_cq
, init_attr
->recv_cq
,
1748 &send_cq
, &recv_cq
);
1749 spin_lock_irqsave(&dev
->reset_flow_resource_lock
, flags
);
1750 mlx5_ib_lock_cqs(send_cq
, recv_cq
);
1751 /* Maintain device to QPs access, needed for further handling via reset
1754 list_add_tail(&qp
->qps_list
, &dev
->qp_list
);
1755 /* Maintain CQ to QPs access, needed for further handling via reset flow
1758 list_add_tail(&qp
->cq_send_list
, &send_cq
->list_send_qp
);
1760 list_add_tail(&qp
->cq_recv_list
, &recv_cq
->list_recv_qp
);
1761 mlx5_ib_unlock_cqs(send_cq
, recv_cq
);
1762 spin_unlock_irqrestore(&dev
->reset_flow_resource_lock
, flags
);
1767 if (qp
->create_type
== MLX5_QP_USER
)
1768 destroy_qp_user(pd
, qp
, base
);
1769 else if (qp
->create_type
== MLX5_QP_KERNEL
)
1770 destroy_qp_kernel(dev
, qp
);
1776 static void mlx5_ib_lock_cqs(struct mlx5_ib_cq
*send_cq
, struct mlx5_ib_cq
*recv_cq
)
1777 __acquires(&send_cq
->lock
) __acquires(&recv_cq
->lock
)
1781 if (send_cq
->mcq
.cqn
< recv_cq
->mcq
.cqn
) {
1782 spin_lock(&send_cq
->lock
);
1783 spin_lock_nested(&recv_cq
->lock
,
1784 SINGLE_DEPTH_NESTING
);
1785 } else if (send_cq
->mcq
.cqn
== recv_cq
->mcq
.cqn
) {
1786 spin_lock(&send_cq
->lock
);
1787 __acquire(&recv_cq
->lock
);
1789 spin_lock(&recv_cq
->lock
);
1790 spin_lock_nested(&send_cq
->lock
,
1791 SINGLE_DEPTH_NESTING
);
1794 spin_lock(&send_cq
->lock
);
1795 __acquire(&recv_cq
->lock
);
1797 } else if (recv_cq
) {
1798 spin_lock(&recv_cq
->lock
);
1799 __acquire(&send_cq
->lock
);
1801 __acquire(&send_cq
->lock
);
1802 __acquire(&recv_cq
->lock
);
1806 static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq
*send_cq
, struct mlx5_ib_cq
*recv_cq
)
1807 __releases(&send_cq
->lock
) __releases(&recv_cq
->lock
)
1811 if (send_cq
->mcq
.cqn
< recv_cq
->mcq
.cqn
) {
1812 spin_unlock(&recv_cq
->lock
);
1813 spin_unlock(&send_cq
->lock
);
1814 } else if (send_cq
->mcq
.cqn
== recv_cq
->mcq
.cqn
) {
1815 __release(&recv_cq
->lock
);
1816 spin_unlock(&send_cq
->lock
);
1818 spin_unlock(&send_cq
->lock
);
1819 spin_unlock(&recv_cq
->lock
);
1822 __release(&recv_cq
->lock
);
1823 spin_unlock(&send_cq
->lock
);
1825 } else if (recv_cq
) {
1826 __release(&send_cq
->lock
);
1827 spin_unlock(&recv_cq
->lock
);
1829 __release(&recv_cq
->lock
);
1830 __release(&send_cq
->lock
);
1834 static struct mlx5_ib_pd
*get_pd(struct mlx5_ib_qp
*qp
)
1836 return to_mpd(qp
->ibqp
.pd
);
1839 static void get_cqs(enum ib_qp_type qp_type
,
1840 struct ib_cq
*ib_send_cq
, struct ib_cq
*ib_recv_cq
,
1841 struct mlx5_ib_cq
**send_cq
, struct mlx5_ib_cq
**recv_cq
)
1844 case IB_QPT_XRC_TGT
:
1848 case MLX5_IB_QPT_REG_UMR
:
1849 case IB_QPT_XRC_INI
:
1850 *send_cq
= ib_send_cq
? to_mcq(ib_send_cq
) : NULL
;
1855 case MLX5_IB_QPT_HW_GSI
:
1859 case IB_QPT_RAW_IPV6
:
1860 case IB_QPT_RAW_ETHERTYPE
:
1861 case IB_QPT_RAW_PACKET
:
1862 *send_cq
= ib_send_cq
? to_mcq(ib_send_cq
) : NULL
;
1863 *recv_cq
= ib_recv_cq
? to_mcq(ib_recv_cq
) : NULL
;
1874 static int modify_raw_packet_qp(struct mlx5_ib_dev
*dev
, struct mlx5_ib_qp
*qp
,
1875 const struct mlx5_modify_raw_qp_param
*raw_qp_param
,
1876 u8 lag_tx_affinity
);
1878 static void destroy_qp_common(struct mlx5_ib_dev
*dev
, struct mlx5_ib_qp
*qp
)
1880 struct mlx5_ib_cq
*send_cq
, *recv_cq
;
1881 struct mlx5_ib_qp_base
*base
= &qp
->trans_qp
.base
;
1882 unsigned long flags
;
1885 if (qp
->ibqp
.rwq_ind_tbl
) {
1886 destroy_rss_raw_qp_tir(dev
, qp
);
1890 base
= qp
->ibqp
.qp_type
== IB_QPT_RAW_PACKET
?
1891 &qp
->raw_packet_qp
.rq
.base
:
1894 if (qp
->state
!= IB_QPS_RESET
) {
1895 if (qp
->ibqp
.qp_type
!= IB_QPT_RAW_PACKET
) {
1896 mlx5_ib_qp_disable_pagefaults(qp
);
1897 err
= mlx5_core_qp_modify(dev
->mdev
,
1898 MLX5_CMD_OP_2RST_QP
, 0,
1901 struct mlx5_modify_raw_qp_param raw_qp_param
= {
1902 .operation
= MLX5_CMD_OP_2RST_QP
1905 err
= modify_raw_packet_qp(dev
, qp
, &raw_qp_param
, 0);
1908 mlx5_ib_warn(dev
, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
1912 get_cqs(qp
->ibqp
.qp_type
, qp
->ibqp
.send_cq
, qp
->ibqp
.recv_cq
,
1913 &send_cq
, &recv_cq
);
1915 spin_lock_irqsave(&dev
->reset_flow_resource_lock
, flags
);
1916 mlx5_ib_lock_cqs(send_cq
, recv_cq
);
1917 /* del from lists under both locks above to protect reset flow paths */
1918 list_del(&qp
->qps_list
);
1920 list_del(&qp
->cq_send_list
);
1923 list_del(&qp
->cq_recv_list
);
1925 if (qp
->create_type
== MLX5_QP_KERNEL
) {
1926 __mlx5_ib_cq_clean(recv_cq
, base
->mqp
.qpn
,
1927 qp
->ibqp
.srq
? to_msrq(qp
->ibqp
.srq
) : NULL
);
1928 if (send_cq
!= recv_cq
)
1929 __mlx5_ib_cq_clean(send_cq
, base
->mqp
.qpn
,
1932 mlx5_ib_unlock_cqs(send_cq
, recv_cq
);
1933 spin_unlock_irqrestore(&dev
->reset_flow_resource_lock
, flags
);
1935 if (qp
->ibqp
.qp_type
== IB_QPT_RAW_PACKET
) {
1936 destroy_raw_packet_qp(dev
, qp
);
1938 err
= mlx5_core_destroy_qp(dev
->mdev
, &base
->mqp
);
1940 mlx5_ib_warn(dev
, "failed to destroy QP 0x%x\n",
1944 if (qp
->create_type
== MLX5_QP_KERNEL
)
1945 destroy_qp_kernel(dev
, qp
);
1946 else if (qp
->create_type
== MLX5_QP_USER
)
1947 destroy_qp_user(&get_pd(qp
)->ibpd
, qp
, base
);
1950 static const char *ib_qp_type_str(enum ib_qp_type type
)
1954 return "IB_QPT_SMI";
1956 return "IB_QPT_GSI";
1963 case IB_QPT_RAW_IPV6
:
1964 return "IB_QPT_RAW_IPV6";
1965 case IB_QPT_RAW_ETHERTYPE
:
1966 return "IB_QPT_RAW_ETHERTYPE";
1967 case IB_QPT_XRC_INI
:
1968 return "IB_QPT_XRC_INI";
1969 case IB_QPT_XRC_TGT
:
1970 return "IB_QPT_XRC_TGT";
1971 case IB_QPT_RAW_PACKET
:
1972 return "IB_QPT_RAW_PACKET";
1973 case MLX5_IB_QPT_REG_UMR
:
1974 return "MLX5_IB_QPT_REG_UMR";
1977 return "Invalid QP type";
1981 struct ib_qp
*mlx5_ib_create_qp(struct ib_pd
*pd
,
1982 struct ib_qp_init_attr
*init_attr
,
1983 struct ib_udata
*udata
)
1985 struct mlx5_ib_dev
*dev
;
1986 struct mlx5_ib_qp
*qp
;
1991 dev
= to_mdev(pd
->device
);
1993 if (init_attr
->qp_type
== IB_QPT_RAW_PACKET
) {
1995 mlx5_ib_dbg(dev
, "Raw Packet QP is not supported for kernel consumers\n");
1996 return ERR_PTR(-EINVAL
);
1997 } else if (!to_mucontext(pd
->uobject
->context
)->cqe_version
) {
1998 mlx5_ib_dbg(dev
, "Raw Packet QP is only supported for CQE version > 0\n");
1999 return ERR_PTR(-EINVAL
);
2003 /* being cautious here */
2004 if (init_attr
->qp_type
!= IB_QPT_XRC_TGT
&&
2005 init_attr
->qp_type
!= MLX5_IB_QPT_REG_UMR
) {
2006 pr_warn("%s: no PD for transport %s\n", __func__
,
2007 ib_qp_type_str(init_attr
->qp_type
));
2008 return ERR_PTR(-EINVAL
);
2010 dev
= to_mdev(to_mxrcd(init_attr
->xrcd
)->ibxrcd
.device
);
2013 switch (init_attr
->qp_type
) {
2014 case IB_QPT_XRC_TGT
:
2015 case IB_QPT_XRC_INI
:
2016 if (!MLX5_CAP_GEN(dev
->mdev
, xrc
)) {
2017 mlx5_ib_dbg(dev
, "XRC not supported\n");
2018 return ERR_PTR(-ENOSYS
);
2020 init_attr
->recv_cq
= NULL
;
2021 if (init_attr
->qp_type
== IB_QPT_XRC_TGT
) {
2022 xrcdn
= to_mxrcd(init_attr
->xrcd
)->xrcdn
;
2023 init_attr
->send_cq
= NULL
;
2027 case IB_QPT_RAW_PACKET
:
2032 case MLX5_IB_QPT_HW_GSI
:
2033 case MLX5_IB_QPT_REG_UMR
:
2034 qp
= kzalloc(sizeof(*qp
), GFP_KERNEL
);
2036 return ERR_PTR(-ENOMEM
);
2038 err
= create_qp_common(dev
, pd
, init_attr
, udata
, qp
);
2040 mlx5_ib_dbg(dev
, "create_qp_common failed\n");
2042 return ERR_PTR(err
);
2045 if (is_qp0(init_attr
->qp_type
))
2046 qp
->ibqp
.qp_num
= 0;
2047 else if (is_qp1(init_attr
->qp_type
))
2048 qp
->ibqp
.qp_num
= 1;
2050 qp
->ibqp
.qp_num
= qp
->trans_qp
.base
.mqp
.qpn
;
2052 mlx5_ib_dbg(dev
, "ib qpnum 0x%x, mlx qpn 0x%x, rcqn 0x%x, scqn 0x%x\n",
2053 qp
->ibqp
.qp_num
, qp
->trans_qp
.base
.mqp
.qpn
,
2054 init_attr
->recv_cq
? to_mcq(init_attr
->recv_cq
)->mcq
.cqn
: -1,
2055 init_attr
->send_cq
? to_mcq(init_attr
->send_cq
)->mcq
.cqn
: -1);
2057 qp
->trans_qp
.xrcdn
= xrcdn
;
2062 return mlx5_ib_gsi_create_qp(pd
, init_attr
);
2064 case IB_QPT_RAW_IPV6
:
2065 case IB_QPT_RAW_ETHERTYPE
:
2068 mlx5_ib_dbg(dev
, "unsupported qp type %d\n",
2069 init_attr
->qp_type
);
2070 /* Don't support raw QPs */
2071 return ERR_PTR(-EINVAL
);
2077 int mlx5_ib_destroy_qp(struct ib_qp
*qp
)
2079 struct mlx5_ib_dev
*dev
= to_mdev(qp
->device
);
2080 struct mlx5_ib_qp
*mqp
= to_mqp(qp
);
2082 if (unlikely(qp
->qp_type
== IB_QPT_GSI
))
2083 return mlx5_ib_gsi_destroy_qp(qp
);
2085 destroy_qp_common(dev
, mqp
);
2092 static __be32
to_mlx5_access_flags(struct mlx5_ib_qp
*qp
, const struct ib_qp_attr
*attr
,
2095 u32 hw_access_flags
= 0;
2099 if (attr_mask
& IB_QP_MAX_DEST_RD_ATOMIC
)
2100 dest_rd_atomic
= attr
->max_dest_rd_atomic
;
2102 dest_rd_atomic
= qp
->trans_qp
.resp_depth
;
2104 if (attr_mask
& IB_QP_ACCESS_FLAGS
)
2105 access_flags
= attr
->qp_access_flags
;
2107 access_flags
= qp
->trans_qp
.atomic_rd_en
;
2109 if (!dest_rd_atomic
)
2110 access_flags
&= IB_ACCESS_REMOTE_WRITE
;
2112 if (access_flags
& IB_ACCESS_REMOTE_READ
)
2113 hw_access_flags
|= MLX5_QP_BIT_RRE
;
2114 if (access_flags
& IB_ACCESS_REMOTE_ATOMIC
)
2115 hw_access_flags
|= (MLX5_QP_BIT_RAE
| MLX5_ATOMIC_MODE_CX
);
2116 if (access_flags
& IB_ACCESS_REMOTE_WRITE
)
2117 hw_access_flags
|= MLX5_QP_BIT_RWE
;
2119 return cpu_to_be32(hw_access_flags
);
2123 MLX5_PATH_FLAG_FL
= 1 << 0,
2124 MLX5_PATH_FLAG_FREE_AR
= 1 << 1,
2125 MLX5_PATH_FLAG_COUNTER
= 1 << 2,
2128 static int ib_rate_to_mlx5(struct mlx5_ib_dev
*dev
, u8 rate
)
2130 if (rate
== IB_RATE_PORT_CURRENT
) {
2132 } else if (rate
< IB_RATE_2_5_GBPS
|| rate
> IB_RATE_300_GBPS
) {
2135 while (rate
!= IB_RATE_2_5_GBPS
&&
2136 !(1 << (rate
+ MLX5_STAT_RATE_OFFSET
) &
2137 MLX5_CAP_GEN(dev
->mdev
, stat_rate_support
)))
2141 return rate
+ MLX5_STAT_RATE_OFFSET
;
2144 static int modify_raw_packet_eth_prio(struct mlx5_core_dev
*dev
,
2145 struct mlx5_ib_sq
*sq
, u8 sl
)
2152 inlen
= MLX5_ST_SZ_BYTES(modify_tis_in
);
2153 in
= mlx5_vzalloc(inlen
);
2157 MLX5_SET(modify_tis_in
, in
, bitmask
.prio
, 1);
2159 tisc
= MLX5_ADDR_OF(modify_tis_in
, in
, ctx
);
2160 MLX5_SET(tisc
, tisc
, prio
, ((sl
& 0x7) << 1));
2162 err
= mlx5_core_modify_tis(dev
, sq
->tisn
, in
, inlen
);
2169 static int modify_raw_packet_tx_affinity(struct mlx5_core_dev
*dev
,
2170 struct mlx5_ib_sq
*sq
, u8 tx_affinity
)
2177 inlen
= MLX5_ST_SZ_BYTES(modify_tis_in
);
2178 in
= mlx5_vzalloc(inlen
);
2182 MLX5_SET(modify_tis_in
, in
, bitmask
.lag_tx_port_affinity
, 1);
2184 tisc
= MLX5_ADDR_OF(modify_tis_in
, in
, ctx
);
2185 MLX5_SET(tisc
, tisc
, lag_tx_port_affinity
, tx_affinity
);
2187 err
= mlx5_core_modify_tis(dev
, sq
->tisn
, in
, inlen
);
2194 static int mlx5_set_path(struct mlx5_ib_dev
*dev
, struct mlx5_ib_qp
*qp
,
2195 const struct ib_ah_attr
*ah
,
2196 struct mlx5_qp_path
*path
, u8 port
, int attr_mask
,
2197 u32 path_flags
, const struct ib_qp_attr
*attr
,
2200 enum rdma_link_layer ll
= rdma_port_get_link_layer(&dev
->ib_dev
, port
);
2203 if (attr_mask
& IB_QP_PKEY_INDEX
)
2204 path
->pkey_index
= cpu_to_be16(alt
? attr
->alt_pkey_index
:
2207 if (ah
->ah_flags
& IB_AH_GRH
) {
2208 if (ah
->grh
.sgid_index
>=
2209 dev
->mdev
->port_caps
[port
- 1].gid_table_len
) {
2210 pr_err("sgid_index (%u) too large. max is %d\n",
2212 dev
->mdev
->port_caps
[port
- 1].gid_table_len
);
2217 if (ll
== IB_LINK_LAYER_ETHERNET
) {
2218 if (!(ah
->ah_flags
& IB_AH_GRH
))
2220 memcpy(path
->rmac
, ah
->dmac
, sizeof(ah
->dmac
));
2221 path
->udp_sport
= mlx5_get_roce_udp_sport(dev
, port
,
2222 ah
->grh
.sgid_index
);
2223 path
->dci_cfi_prio_sl
= (ah
->sl
& 0x7) << 4;
2225 path
->fl_free_ar
= (path_flags
& MLX5_PATH_FLAG_FL
) ? 0x80 : 0;
2227 (path_flags
& MLX5_PATH_FLAG_FREE_AR
) ? 0x40 : 0;
2228 path
->rlid
= cpu_to_be16(ah
->dlid
);
2229 path
->grh_mlid
= ah
->src_path_bits
& 0x7f;
2230 if (ah
->ah_flags
& IB_AH_GRH
)
2231 path
->grh_mlid
|= 1 << 7;
2232 path
->dci_cfi_prio_sl
= ah
->sl
& 0xf;
2235 if (ah
->ah_flags
& IB_AH_GRH
) {
2236 path
->mgid_index
= ah
->grh
.sgid_index
;
2237 path
->hop_limit
= ah
->grh
.hop_limit
;
2238 path
->tclass_flowlabel
=
2239 cpu_to_be32((ah
->grh
.traffic_class
<< 20) |
2240 (ah
->grh
.flow_label
));
2241 memcpy(path
->rgid
, ah
->grh
.dgid
.raw
, 16);
2244 err
= ib_rate_to_mlx5(dev
, ah
->static_rate
);
2247 path
->static_rate
= err
;
2250 if (attr_mask
& IB_QP_TIMEOUT
)
2251 path
->ackto_lt
= (alt
? attr
->alt_timeout
: attr
->timeout
) << 3;
2253 if ((qp
->ibqp
.qp_type
== IB_QPT_RAW_PACKET
) && qp
->sq
.wqe_cnt
)
2254 return modify_raw_packet_eth_prio(dev
->mdev
,
2255 &qp
->raw_packet_qp
.sq
,
2261 static enum mlx5_qp_optpar opt_mask
[MLX5_QP_NUM_STATE
][MLX5_QP_NUM_STATE
][MLX5_QP_ST_MAX
] = {
2262 [MLX5_QP_STATE_INIT
] = {
2263 [MLX5_QP_STATE_INIT
] = {
2264 [MLX5_QP_ST_RC
] = MLX5_QP_OPTPAR_RRE
|
2265 MLX5_QP_OPTPAR_RAE
|
2266 MLX5_QP_OPTPAR_RWE
|
2267 MLX5_QP_OPTPAR_PKEY_INDEX
|
2268 MLX5_QP_OPTPAR_PRI_PORT
,
2269 [MLX5_QP_ST_UC
] = MLX5_QP_OPTPAR_RWE
|
2270 MLX5_QP_OPTPAR_PKEY_INDEX
|
2271 MLX5_QP_OPTPAR_PRI_PORT
,
2272 [MLX5_QP_ST_UD
] = MLX5_QP_OPTPAR_PKEY_INDEX
|
2273 MLX5_QP_OPTPAR_Q_KEY
|
2274 MLX5_QP_OPTPAR_PRI_PORT
,
2276 [MLX5_QP_STATE_RTR
] = {
2277 [MLX5_QP_ST_RC
] = MLX5_QP_OPTPAR_ALT_ADDR_PATH
|
2278 MLX5_QP_OPTPAR_RRE
|
2279 MLX5_QP_OPTPAR_RAE
|
2280 MLX5_QP_OPTPAR_RWE
|
2281 MLX5_QP_OPTPAR_PKEY_INDEX
,
2282 [MLX5_QP_ST_UC
] = MLX5_QP_OPTPAR_ALT_ADDR_PATH
|
2283 MLX5_QP_OPTPAR_RWE
|
2284 MLX5_QP_OPTPAR_PKEY_INDEX
,
2285 [MLX5_QP_ST_UD
] = MLX5_QP_OPTPAR_PKEY_INDEX
|
2286 MLX5_QP_OPTPAR_Q_KEY
,
2287 [MLX5_QP_ST_MLX
] = MLX5_QP_OPTPAR_PKEY_INDEX
|
2288 MLX5_QP_OPTPAR_Q_KEY
,
2289 [MLX5_QP_ST_XRC
] = MLX5_QP_OPTPAR_ALT_ADDR_PATH
|
2290 MLX5_QP_OPTPAR_RRE
|
2291 MLX5_QP_OPTPAR_RAE
|
2292 MLX5_QP_OPTPAR_RWE
|
2293 MLX5_QP_OPTPAR_PKEY_INDEX
,
2296 [MLX5_QP_STATE_RTR
] = {
2297 [MLX5_QP_STATE_RTS
] = {
2298 [MLX5_QP_ST_RC
] = MLX5_QP_OPTPAR_ALT_ADDR_PATH
|
2299 MLX5_QP_OPTPAR_RRE
|
2300 MLX5_QP_OPTPAR_RAE
|
2301 MLX5_QP_OPTPAR_RWE
|
2302 MLX5_QP_OPTPAR_PM_STATE
|
2303 MLX5_QP_OPTPAR_RNR_TIMEOUT
,
2304 [MLX5_QP_ST_UC
] = MLX5_QP_OPTPAR_ALT_ADDR_PATH
|
2305 MLX5_QP_OPTPAR_RWE
|
2306 MLX5_QP_OPTPAR_PM_STATE
,
2307 [MLX5_QP_ST_UD
] = MLX5_QP_OPTPAR_Q_KEY
,
2310 [MLX5_QP_STATE_RTS
] = {
2311 [MLX5_QP_STATE_RTS
] = {
2312 [MLX5_QP_ST_RC
] = MLX5_QP_OPTPAR_RRE
|
2313 MLX5_QP_OPTPAR_RAE
|
2314 MLX5_QP_OPTPAR_RWE
|
2315 MLX5_QP_OPTPAR_RNR_TIMEOUT
|
2316 MLX5_QP_OPTPAR_PM_STATE
|
2317 MLX5_QP_OPTPAR_ALT_ADDR_PATH
,
2318 [MLX5_QP_ST_UC
] = MLX5_QP_OPTPAR_RWE
|
2319 MLX5_QP_OPTPAR_PM_STATE
|
2320 MLX5_QP_OPTPAR_ALT_ADDR_PATH
,
2321 [MLX5_QP_ST_UD
] = MLX5_QP_OPTPAR_Q_KEY
|
2322 MLX5_QP_OPTPAR_SRQN
|
2323 MLX5_QP_OPTPAR_CQN_RCV
,
2326 [MLX5_QP_STATE_SQER
] = {
2327 [MLX5_QP_STATE_RTS
] = {
2328 [MLX5_QP_ST_UD
] = MLX5_QP_OPTPAR_Q_KEY
,
2329 [MLX5_QP_ST_MLX
] = MLX5_QP_OPTPAR_Q_KEY
,
2330 [MLX5_QP_ST_UC
] = MLX5_QP_OPTPAR_RWE
,
2331 [MLX5_QP_ST_RC
] = MLX5_QP_OPTPAR_RNR_TIMEOUT
|
2332 MLX5_QP_OPTPAR_RWE
|
2333 MLX5_QP_OPTPAR_RAE
|
2339 static int ib_nr_to_mlx5_nr(int ib_mask
)
2344 case IB_QP_CUR_STATE
:
2346 case IB_QP_EN_SQD_ASYNC_NOTIFY
:
2348 case IB_QP_ACCESS_FLAGS
:
2349 return MLX5_QP_OPTPAR_RWE
| MLX5_QP_OPTPAR_RRE
|
2351 case IB_QP_PKEY_INDEX
:
2352 return MLX5_QP_OPTPAR_PKEY_INDEX
;
2354 return MLX5_QP_OPTPAR_PRI_PORT
;
2356 return MLX5_QP_OPTPAR_Q_KEY
;
2358 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH
|
2359 MLX5_QP_OPTPAR_PRI_PORT
;
2360 case IB_QP_PATH_MTU
:
2363 return MLX5_QP_OPTPAR_ACK_TIMEOUT
;
2364 case IB_QP_RETRY_CNT
:
2365 return MLX5_QP_OPTPAR_RETRY_COUNT
;
2366 case IB_QP_RNR_RETRY
:
2367 return MLX5_QP_OPTPAR_RNR_RETRY
;
2370 case IB_QP_MAX_QP_RD_ATOMIC
:
2371 return MLX5_QP_OPTPAR_SRA_MAX
;
2372 case IB_QP_ALT_PATH
:
2373 return MLX5_QP_OPTPAR_ALT_ADDR_PATH
;
2374 case IB_QP_MIN_RNR_TIMER
:
2375 return MLX5_QP_OPTPAR_RNR_TIMEOUT
;
2378 case IB_QP_MAX_DEST_RD_ATOMIC
:
2379 return MLX5_QP_OPTPAR_RRA_MAX
| MLX5_QP_OPTPAR_RWE
|
2380 MLX5_QP_OPTPAR_RRE
| MLX5_QP_OPTPAR_RAE
;
2381 case IB_QP_PATH_MIG_STATE
:
2382 return MLX5_QP_OPTPAR_PM_STATE
;
2385 case IB_QP_DEST_QPN
:
2391 static int ib_mask_to_mlx5_opt(int ib_mask
)
2396 for (i
= 0; i
< 8 * sizeof(int); i
++) {
2397 if ((1 << i
) & ib_mask
)
2398 result
|= ib_nr_to_mlx5_nr(1 << i
);
2404 static int modify_raw_packet_qp_rq(struct mlx5_ib_dev
*dev
,
2405 struct mlx5_ib_rq
*rq
, int new_state
,
2406 const struct mlx5_modify_raw_qp_param
*raw_qp_param
)
2413 inlen
= MLX5_ST_SZ_BYTES(modify_rq_in
);
2414 in
= mlx5_vzalloc(inlen
);
2418 MLX5_SET(modify_rq_in
, in
, rq_state
, rq
->state
);
2420 rqc
= MLX5_ADDR_OF(modify_rq_in
, in
, ctx
);
2421 MLX5_SET(rqc
, rqc
, state
, new_state
);
2423 if (raw_qp_param
->set_mask
& MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID
) {
2424 if (MLX5_CAP_GEN(dev
->mdev
, modify_rq_counter_set_id
)) {
2425 MLX5_SET64(modify_rq_in
, in
, modify_bitmask
,
2426 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_MODIFY_RQ_COUNTER_SET_ID
);
2427 MLX5_SET(rqc
, rqc
, counter_set_id
, raw_qp_param
->rq_q_ctr_id
);
2429 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2433 err
= mlx5_core_modify_rq(dev
->mdev
, rq
->base
.mqp
.qpn
, in
, inlen
);
2437 rq
->state
= new_state
;
2444 static int modify_raw_packet_qp_sq(struct mlx5_core_dev
*dev
,
2445 struct mlx5_ib_sq
*sq
, int new_state
)
2452 inlen
= MLX5_ST_SZ_BYTES(modify_sq_in
);
2453 in
= mlx5_vzalloc(inlen
);
2457 MLX5_SET(modify_sq_in
, in
, sq_state
, sq
->state
);
2459 sqc
= MLX5_ADDR_OF(modify_sq_in
, in
, ctx
);
2460 MLX5_SET(sqc
, sqc
, state
, new_state
);
2462 err
= mlx5_core_modify_sq(dev
, sq
->base
.mqp
.qpn
, in
, inlen
);
2466 sq
->state
= new_state
;
2473 static int modify_raw_packet_qp(struct mlx5_ib_dev
*dev
, struct mlx5_ib_qp
*qp
,
2474 const struct mlx5_modify_raw_qp_param
*raw_qp_param
,
2477 struct mlx5_ib_raw_packet_qp
*raw_packet_qp
= &qp
->raw_packet_qp
;
2478 struct mlx5_ib_rq
*rq
= &raw_packet_qp
->rq
;
2479 struct mlx5_ib_sq
*sq
= &raw_packet_qp
->sq
;
2484 switch (raw_qp_param
->operation
) {
2485 case MLX5_CMD_OP_RST2INIT_QP
:
2486 rq_state
= MLX5_RQC_STATE_RDY
;
2487 sq_state
= MLX5_SQC_STATE_RDY
;
2489 case MLX5_CMD_OP_2ERR_QP
:
2490 rq_state
= MLX5_RQC_STATE_ERR
;
2491 sq_state
= MLX5_SQC_STATE_ERR
;
2493 case MLX5_CMD_OP_2RST_QP
:
2494 rq_state
= MLX5_RQC_STATE_RST
;
2495 sq_state
= MLX5_SQC_STATE_RST
;
2497 case MLX5_CMD_OP_INIT2INIT_QP
:
2498 case MLX5_CMD_OP_INIT2RTR_QP
:
2499 case MLX5_CMD_OP_RTR2RTS_QP
:
2500 case MLX5_CMD_OP_RTS2RTS_QP
:
2501 if (raw_qp_param
->set_mask
)
2510 if (qp
->rq
.wqe_cnt
) {
2511 err
= modify_raw_packet_qp_rq(dev
, rq
, rq_state
, raw_qp_param
);
2516 if (qp
->sq
.wqe_cnt
) {
2518 err
= modify_raw_packet_tx_affinity(dev
->mdev
, sq
,
2524 return modify_raw_packet_qp_sq(dev
->mdev
, sq
, sq_state
);
2530 static int __mlx5_ib_modify_qp(struct ib_qp
*ibqp
,
2531 const struct ib_qp_attr
*attr
, int attr_mask
,
2532 enum ib_qp_state cur_state
, enum ib_qp_state new_state
)
2534 static const u16 optab
[MLX5_QP_NUM_STATE
][MLX5_QP_NUM_STATE
] = {
2535 [MLX5_QP_STATE_RST
] = {
2536 [MLX5_QP_STATE_RST
] = MLX5_CMD_OP_2RST_QP
,
2537 [MLX5_QP_STATE_ERR
] = MLX5_CMD_OP_2ERR_QP
,
2538 [MLX5_QP_STATE_INIT
] = MLX5_CMD_OP_RST2INIT_QP
,
2540 [MLX5_QP_STATE_INIT
] = {
2541 [MLX5_QP_STATE_RST
] = MLX5_CMD_OP_2RST_QP
,
2542 [MLX5_QP_STATE_ERR
] = MLX5_CMD_OP_2ERR_QP
,
2543 [MLX5_QP_STATE_INIT
] = MLX5_CMD_OP_INIT2INIT_QP
,
2544 [MLX5_QP_STATE_RTR
] = MLX5_CMD_OP_INIT2RTR_QP
,
2546 [MLX5_QP_STATE_RTR
] = {
2547 [MLX5_QP_STATE_RST
] = MLX5_CMD_OP_2RST_QP
,
2548 [MLX5_QP_STATE_ERR
] = MLX5_CMD_OP_2ERR_QP
,
2549 [MLX5_QP_STATE_RTS
] = MLX5_CMD_OP_RTR2RTS_QP
,
2551 [MLX5_QP_STATE_RTS
] = {
2552 [MLX5_QP_STATE_RST
] = MLX5_CMD_OP_2RST_QP
,
2553 [MLX5_QP_STATE_ERR
] = MLX5_CMD_OP_2ERR_QP
,
2554 [MLX5_QP_STATE_RTS
] = MLX5_CMD_OP_RTS2RTS_QP
,
2556 [MLX5_QP_STATE_SQD
] = {
2557 [MLX5_QP_STATE_RST
] = MLX5_CMD_OP_2RST_QP
,
2558 [MLX5_QP_STATE_ERR
] = MLX5_CMD_OP_2ERR_QP
,
2560 [MLX5_QP_STATE_SQER
] = {
2561 [MLX5_QP_STATE_RST
] = MLX5_CMD_OP_2RST_QP
,
2562 [MLX5_QP_STATE_ERR
] = MLX5_CMD_OP_2ERR_QP
,
2563 [MLX5_QP_STATE_RTS
] = MLX5_CMD_OP_SQERR2RTS_QP
,
2565 [MLX5_QP_STATE_ERR
] = {
2566 [MLX5_QP_STATE_RST
] = MLX5_CMD_OP_2RST_QP
,
2567 [MLX5_QP_STATE_ERR
] = MLX5_CMD_OP_2ERR_QP
,
2571 struct mlx5_ib_dev
*dev
= to_mdev(ibqp
->device
);
2572 struct mlx5_ib_qp
*qp
= to_mqp(ibqp
);
2573 struct mlx5_ib_qp_base
*base
= &qp
->trans_qp
.base
;
2574 struct mlx5_ib_cq
*send_cq
, *recv_cq
;
2575 struct mlx5_qp_context
*context
;
2576 struct mlx5_ib_pd
*pd
;
2577 struct mlx5_ib_port
*mibport
= NULL
;
2578 enum mlx5_qp_state mlx5_cur
, mlx5_new
;
2579 enum mlx5_qp_optpar optpar
;
2586 context
= kzalloc(sizeof(*context
), GFP_KERNEL
);
2590 err
= to_mlx5_st(ibqp
->qp_type
);
2592 mlx5_ib_dbg(dev
, "unsupported qp type %d\n", ibqp
->qp_type
);
2596 context
->flags
= cpu_to_be32(err
<< 16);
2598 if (!(attr_mask
& IB_QP_PATH_MIG_STATE
)) {
2599 context
->flags
|= cpu_to_be32(MLX5_QP_PM_MIGRATED
<< 11);
2601 switch (attr
->path_mig_state
) {
2602 case IB_MIG_MIGRATED
:
2603 context
->flags
|= cpu_to_be32(MLX5_QP_PM_MIGRATED
<< 11);
2606 context
->flags
|= cpu_to_be32(MLX5_QP_PM_REARM
<< 11);
2609 context
->flags
|= cpu_to_be32(MLX5_QP_PM_ARMED
<< 11);
2614 if ((cur_state
== IB_QPS_RESET
) && (new_state
== IB_QPS_INIT
)) {
2615 if ((ibqp
->qp_type
== IB_QPT_RC
) ||
2616 (ibqp
->qp_type
== IB_QPT_UD
&&
2617 !(qp
->flags
& MLX5_IB_QP_SQPN_QP1
)) ||
2618 (ibqp
->qp_type
== IB_QPT_UC
) ||
2619 (ibqp
->qp_type
== IB_QPT_RAW_PACKET
) ||
2620 (ibqp
->qp_type
== IB_QPT_XRC_INI
) ||
2621 (ibqp
->qp_type
== IB_QPT_XRC_TGT
)) {
2622 if (mlx5_lag_is_active(dev
->mdev
)) {
2623 tx_affinity
= (unsigned int)atomic_add_return(1,
2624 &dev
->roce
.next_port
) %
2626 context
->flags
|= cpu_to_be32(tx_affinity
<< 24);
2631 if (is_sqp(ibqp
->qp_type
)) {
2632 context
->mtu_msgmax
= (IB_MTU_256
<< 5) | 8;
2633 } else if (ibqp
->qp_type
== IB_QPT_UD
||
2634 ibqp
->qp_type
== MLX5_IB_QPT_REG_UMR
) {
2635 context
->mtu_msgmax
= (IB_MTU_4096
<< 5) | 12;
2636 } else if (attr_mask
& IB_QP_PATH_MTU
) {
2637 if (attr
->path_mtu
< IB_MTU_256
||
2638 attr
->path_mtu
> IB_MTU_4096
) {
2639 mlx5_ib_warn(dev
, "invalid mtu %d\n", attr
->path_mtu
);
2643 context
->mtu_msgmax
= (attr
->path_mtu
<< 5) |
2644 (u8
)MLX5_CAP_GEN(dev
->mdev
, log_max_msg
);
2647 if (attr_mask
& IB_QP_DEST_QPN
)
2648 context
->log_pg_sz_remote_qpn
= cpu_to_be32(attr
->dest_qp_num
);
2650 if (attr_mask
& IB_QP_PKEY_INDEX
)
2651 context
->pri_path
.pkey_index
= cpu_to_be16(attr
->pkey_index
);
2653 /* todo implement counter_index functionality */
2655 if (is_sqp(ibqp
->qp_type
))
2656 context
->pri_path
.port
= qp
->port
;
2658 if (attr_mask
& IB_QP_PORT
)
2659 context
->pri_path
.port
= attr
->port_num
;
2661 if (attr_mask
& IB_QP_AV
) {
2662 err
= mlx5_set_path(dev
, qp
, &attr
->ah_attr
, &context
->pri_path
,
2663 attr_mask
& IB_QP_PORT
? attr
->port_num
: qp
->port
,
2664 attr_mask
, 0, attr
, false);
2669 if (attr_mask
& IB_QP_TIMEOUT
)
2670 context
->pri_path
.ackto_lt
|= attr
->timeout
<< 3;
2672 if (attr_mask
& IB_QP_ALT_PATH
) {
2673 err
= mlx5_set_path(dev
, qp
, &attr
->alt_ah_attr
,
2676 attr_mask
| IB_QP_PKEY_INDEX
| IB_QP_TIMEOUT
,
2683 get_cqs(qp
->ibqp
.qp_type
, qp
->ibqp
.send_cq
, qp
->ibqp
.recv_cq
,
2684 &send_cq
, &recv_cq
);
2686 context
->flags_pd
= cpu_to_be32(pd
? pd
->pdn
: to_mpd(dev
->devr
.p0
)->pdn
);
2687 context
->cqn_send
= send_cq
? cpu_to_be32(send_cq
->mcq
.cqn
) : 0;
2688 context
->cqn_recv
= recv_cq
? cpu_to_be32(recv_cq
->mcq
.cqn
) : 0;
2689 context
->params1
= cpu_to_be32(MLX5_IB_ACK_REQ_FREQ
<< 28);
2691 if (attr_mask
& IB_QP_RNR_RETRY
)
2692 context
->params1
|= cpu_to_be32(attr
->rnr_retry
<< 13);
2694 if (attr_mask
& IB_QP_RETRY_CNT
)
2695 context
->params1
|= cpu_to_be32(attr
->retry_cnt
<< 16);
2697 if (attr_mask
& IB_QP_MAX_QP_RD_ATOMIC
) {
2698 if (attr
->max_rd_atomic
)
2700 cpu_to_be32(fls(attr
->max_rd_atomic
- 1) << 21);
2703 if (attr_mask
& IB_QP_SQ_PSN
)
2704 context
->next_send_psn
= cpu_to_be32(attr
->sq_psn
);
2706 if (attr_mask
& IB_QP_MAX_DEST_RD_ATOMIC
) {
2707 if (attr
->max_dest_rd_atomic
)
2709 cpu_to_be32(fls(attr
->max_dest_rd_atomic
- 1) << 21);
2712 if (attr_mask
& (IB_QP_ACCESS_FLAGS
| IB_QP_MAX_DEST_RD_ATOMIC
))
2713 context
->params2
|= to_mlx5_access_flags(qp
, attr
, attr_mask
);
2715 if (attr_mask
& IB_QP_MIN_RNR_TIMER
)
2716 context
->rnr_nextrecvpsn
|= cpu_to_be32(attr
->min_rnr_timer
<< 24);
2718 if (attr_mask
& IB_QP_RQ_PSN
)
2719 context
->rnr_nextrecvpsn
|= cpu_to_be32(attr
->rq_psn
);
2721 if (attr_mask
& IB_QP_QKEY
)
2722 context
->qkey
= cpu_to_be32(attr
->qkey
);
2724 if (qp
->rq
.wqe_cnt
&& cur_state
== IB_QPS_RESET
&& new_state
== IB_QPS_INIT
)
2725 context
->db_rec_addr
= cpu_to_be64(qp
->db
.dma
);
2727 if (cur_state
== IB_QPS_RTS
&& new_state
== IB_QPS_SQD
&&
2728 attr_mask
& IB_QP_EN_SQD_ASYNC_NOTIFY
&& attr
->en_sqd_async_notify
)
2733 if (cur_state
== IB_QPS_RESET
&& new_state
== IB_QPS_INIT
) {
2734 u8 port_num
= (attr_mask
& IB_QP_PORT
? attr
->port_num
:
2736 mibport
= &dev
->port
[port_num
];
2737 context
->qp_counter_set_usr_page
|=
2738 cpu_to_be32((u32
)(mibport
->q_cnt_id
) << 24);
2741 if (!ibqp
->uobject
&& cur_state
== IB_QPS_RESET
&& new_state
== IB_QPS_INIT
)
2742 context
->sq_crq_size
|= cpu_to_be16(1 << 4);
2744 if (qp
->flags
& MLX5_IB_QP_SQPN_QP1
)
2745 context
->deth_sqpn
= cpu_to_be32(1);
2747 mlx5_cur
= to_mlx5_state(cur_state
);
2748 mlx5_new
= to_mlx5_state(new_state
);
2749 mlx5_st
= to_mlx5_st(ibqp
->qp_type
);
2753 /* If moving to a reset or error state, we must disable page faults on
2754 * this QP and flush all current page faults. Otherwise a stale page
2755 * fault may attempt to work on this QP after it is reset and moved
2756 * again to RTS, and may cause the driver and the device to get out of
2758 if (cur_state
!= IB_QPS_RESET
&& cur_state
!= IB_QPS_ERR
&&
2759 (new_state
== IB_QPS_RESET
|| new_state
== IB_QPS_ERR
) &&
2760 (qp
->ibqp
.qp_type
!= IB_QPT_RAW_PACKET
))
2761 mlx5_ib_qp_disable_pagefaults(qp
);
2763 if (mlx5_cur
>= MLX5_QP_NUM_STATE
|| mlx5_new
>= MLX5_QP_NUM_STATE
||
2764 !optab
[mlx5_cur
][mlx5_new
])
2767 op
= optab
[mlx5_cur
][mlx5_new
];
2768 optpar
= ib_mask_to_mlx5_opt(attr_mask
);
2769 optpar
&= opt_mask
[mlx5_cur
][mlx5_new
][mlx5_st
];
2771 if (qp
->ibqp
.qp_type
== IB_QPT_RAW_PACKET
) {
2772 struct mlx5_modify_raw_qp_param raw_qp_param
= {};
2774 raw_qp_param
.operation
= op
;
2775 if (cur_state
== IB_QPS_RESET
&& new_state
== IB_QPS_INIT
) {
2776 raw_qp_param
.rq_q_ctr_id
= mibport
->q_cnt_id
;
2777 raw_qp_param
.set_mask
|= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID
;
2779 err
= modify_raw_packet_qp(dev
, qp
, &raw_qp_param
, tx_affinity
);
2781 err
= mlx5_core_qp_modify(dev
->mdev
, op
, optpar
, context
,
2788 if (cur_state
== IB_QPS_RESET
&& new_state
== IB_QPS_INIT
&&
2789 (qp
->ibqp
.qp_type
!= IB_QPT_RAW_PACKET
))
2790 mlx5_ib_qp_enable_pagefaults(qp
);
2792 qp
->state
= new_state
;
2794 if (attr_mask
& IB_QP_ACCESS_FLAGS
)
2795 qp
->trans_qp
.atomic_rd_en
= attr
->qp_access_flags
;
2796 if (attr_mask
& IB_QP_MAX_DEST_RD_ATOMIC
)
2797 qp
->trans_qp
.resp_depth
= attr
->max_dest_rd_atomic
;
2798 if (attr_mask
& IB_QP_PORT
)
2799 qp
->port
= attr
->port_num
;
2800 if (attr_mask
& IB_QP_ALT_PATH
)
2801 qp
->trans_qp
.alt_port
= attr
->alt_port_num
;
2804 * If we moved a kernel QP to RESET, clean up all old CQ
2805 * entries and reinitialize the QP.
2807 if (new_state
== IB_QPS_RESET
&& !ibqp
->uobject
) {
2808 mlx5_ib_cq_clean(recv_cq
, base
->mqp
.qpn
,
2809 ibqp
->srq
? to_msrq(ibqp
->srq
) : NULL
);
2810 if (send_cq
!= recv_cq
)
2811 mlx5_ib_cq_clean(send_cq
, base
->mqp
.qpn
, NULL
);
2817 qp
->sq
.cur_post
= 0;
2818 qp
->sq
.last_poll
= 0;
2819 qp
->db
.db
[MLX5_RCV_DBR
] = 0;
2820 qp
->db
.db
[MLX5_SND_DBR
] = 0;
2828 int mlx5_ib_modify_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*attr
,
2829 int attr_mask
, struct ib_udata
*udata
)
2831 struct mlx5_ib_dev
*dev
= to_mdev(ibqp
->device
);
2832 struct mlx5_ib_qp
*qp
= to_mqp(ibqp
);
2833 enum ib_qp_type qp_type
;
2834 enum ib_qp_state cur_state
, new_state
;
2837 enum rdma_link_layer ll
= IB_LINK_LAYER_UNSPECIFIED
;
2839 if (ibqp
->rwq_ind_tbl
)
2842 if (unlikely(ibqp
->qp_type
== IB_QPT_GSI
))
2843 return mlx5_ib_gsi_modify_qp(ibqp
, attr
, attr_mask
);
2845 qp_type
= (unlikely(ibqp
->qp_type
== MLX5_IB_QPT_HW_GSI
)) ?
2846 IB_QPT_GSI
: ibqp
->qp_type
;
2848 mutex_lock(&qp
->mutex
);
2850 cur_state
= attr_mask
& IB_QP_CUR_STATE
? attr
->cur_qp_state
: qp
->state
;
2851 new_state
= attr_mask
& IB_QP_STATE
? attr
->qp_state
: cur_state
;
2853 if (!(cur_state
== new_state
&& cur_state
== IB_QPS_RESET
)) {
2854 port
= attr_mask
& IB_QP_PORT
? attr
->port_num
: qp
->port
;
2855 ll
= dev
->ib_dev
.get_link_layer(&dev
->ib_dev
, port
);
2858 if (qp_type
!= MLX5_IB_QPT_REG_UMR
&&
2859 !ib_modify_qp_is_ok(cur_state
, new_state
, qp_type
, attr_mask
, ll
)) {
2860 mlx5_ib_dbg(dev
, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
2861 cur_state
, new_state
, ibqp
->qp_type
, attr_mask
);
2865 if ((attr_mask
& IB_QP_PORT
) &&
2866 (attr
->port_num
== 0 ||
2867 attr
->port_num
> MLX5_CAP_GEN(dev
->mdev
, num_ports
))) {
2868 mlx5_ib_dbg(dev
, "invalid port number %d. number of ports is %d\n",
2869 attr
->port_num
, dev
->num_ports
);
2873 if (attr_mask
& IB_QP_PKEY_INDEX
) {
2874 port
= attr_mask
& IB_QP_PORT
? attr
->port_num
: qp
->port
;
2875 if (attr
->pkey_index
>=
2876 dev
->mdev
->port_caps
[port
- 1].pkey_table_len
) {
2877 mlx5_ib_dbg(dev
, "invalid pkey index %d\n",
2883 if (attr_mask
& IB_QP_MAX_QP_RD_ATOMIC
&&
2884 attr
->max_rd_atomic
>
2885 (1 << MLX5_CAP_GEN(dev
->mdev
, log_max_ra_res_qp
))) {
2886 mlx5_ib_dbg(dev
, "invalid max_rd_atomic value %d\n",
2887 attr
->max_rd_atomic
);
2891 if (attr_mask
& IB_QP_MAX_DEST_RD_ATOMIC
&&
2892 attr
->max_dest_rd_atomic
>
2893 (1 << MLX5_CAP_GEN(dev
->mdev
, log_max_ra_req_qp
))) {
2894 mlx5_ib_dbg(dev
, "invalid max_dest_rd_atomic value %d\n",
2895 attr
->max_dest_rd_atomic
);
2899 if (cur_state
== new_state
&& cur_state
== IB_QPS_RESET
) {
2904 err
= __mlx5_ib_modify_qp(ibqp
, attr
, attr_mask
, cur_state
, new_state
);
2907 mutex_unlock(&qp
->mutex
);
2911 static int mlx5_wq_overflow(struct mlx5_ib_wq
*wq
, int nreq
, struct ib_cq
*ib_cq
)
2913 struct mlx5_ib_cq
*cq
;
2916 cur
= wq
->head
- wq
->tail
;
2917 if (likely(cur
+ nreq
< wq
->max_post
))
2921 spin_lock(&cq
->lock
);
2922 cur
= wq
->head
- wq
->tail
;
2923 spin_unlock(&cq
->lock
);
2925 return cur
+ nreq
>= wq
->max_post
;
2928 static __always_inline
void set_raddr_seg(struct mlx5_wqe_raddr_seg
*rseg
,
2929 u64 remote_addr
, u32 rkey
)
2931 rseg
->raddr
= cpu_to_be64(remote_addr
);
2932 rseg
->rkey
= cpu_to_be32(rkey
);
2936 static void *set_eth_seg(struct mlx5_wqe_eth_seg
*eseg
,
2937 struct ib_send_wr
*wr
, void *qend
,
2938 struct mlx5_ib_qp
*qp
, int *size
)
2942 memset(eseg
, 0, sizeof(struct mlx5_wqe_eth_seg
));
2944 if (wr
->send_flags
& IB_SEND_IP_CSUM
)
2945 eseg
->cs_flags
= MLX5_ETH_WQE_L3_CSUM
|
2946 MLX5_ETH_WQE_L4_CSUM
;
2948 seg
+= sizeof(struct mlx5_wqe_eth_seg
);
2949 *size
+= sizeof(struct mlx5_wqe_eth_seg
) / 16;
2951 if (wr
->opcode
== IB_WR_LSO
) {
2952 struct ib_ud_wr
*ud_wr
= container_of(wr
, struct ib_ud_wr
, wr
);
2953 int size_of_inl_hdr_start
= sizeof(eseg
->inline_hdr_start
);
2954 u64 left
, leftlen
, copysz
;
2955 void *pdata
= ud_wr
->header
;
2958 eseg
->mss
= cpu_to_be16(ud_wr
->mss
);
2959 eseg
->inline_hdr_sz
= cpu_to_be16(left
);
2962 * check if there is space till the end of queue, if yes,
2963 * copy all in one shot, otherwise copy till the end of queue,
2964 * rollback and than the copy the left
2966 leftlen
= qend
- (void *)eseg
->inline_hdr_start
;
2967 copysz
= min_t(u64
, leftlen
, left
);
2969 memcpy(seg
- size_of_inl_hdr_start
, pdata
, copysz
);
2971 if (likely(copysz
> size_of_inl_hdr_start
)) {
2972 seg
+= ALIGN(copysz
- size_of_inl_hdr_start
, 16);
2973 *size
+= ALIGN(copysz
- size_of_inl_hdr_start
, 16) / 16;
2976 if (unlikely(copysz
< left
)) { /* the last wqe in the queue */
2977 seg
= mlx5_get_send_wqe(qp
, 0);
2980 memcpy(seg
, pdata
, left
);
2981 seg
+= ALIGN(left
, 16);
2982 *size
+= ALIGN(left
, 16) / 16;
2989 static void set_datagram_seg(struct mlx5_wqe_datagram_seg
*dseg
,
2990 struct ib_send_wr
*wr
)
2992 memcpy(&dseg
->av
, &to_mah(ud_wr(wr
)->ah
)->av
, sizeof(struct mlx5_av
));
2993 dseg
->av
.dqp_dct
= cpu_to_be32(ud_wr(wr
)->remote_qpn
| MLX5_EXTENDED_UD_AV
);
2994 dseg
->av
.key
.qkey
.qkey
= cpu_to_be32(ud_wr(wr
)->remote_qkey
);
2997 static void set_data_ptr_seg(struct mlx5_wqe_data_seg
*dseg
, struct ib_sge
*sg
)
2999 dseg
->byte_count
= cpu_to_be32(sg
->length
);
3000 dseg
->lkey
= cpu_to_be32(sg
->lkey
);
3001 dseg
->addr
= cpu_to_be64(sg
->addr
);
3004 static __be16
get_klm_octo(int npages
)
3006 return cpu_to_be16(ALIGN(npages
, 8) / 2);
3009 static __be64
frwr_mkey_mask(void)
3013 result
= MLX5_MKEY_MASK_LEN
|
3014 MLX5_MKEY_MASK_PAGE_SIZE
|
3015 MLX5_MKEY_MASK_START_ADDR
|
3016 MLX5_MKEY_MASK_EN_RINVAL
|
3017 MLX5_MKEY_MASK_KEY
|
3023 MLX5_MKEY_MASK_SMALL_FENCE
|
3024 MLX5_MKEY_MASK_FREE
;
3026 return cpu_to_be64(result
);
3029 static __be64
sig_mkey_mask(void)
3033 result
= MLX5_MKEY_MASK_LEN
|
3034 MLX5_MKEY_MASK_PAGE_SIZE
|
3035 MLX5_MKEY_MASK_START_ADDR
|
3036 MLX5_MKEY_MASK_EN_SIGERR
|
3037 MLX5_MKEY_MASK_EN_RINVAL
|
3038 MLX5_MKEY_MASK_KEY
|
3043 MLX5_MKEY_MASK_SMALL_FENCE
|
3044 MLX5_MKEY_MASK_FREE
|
3045 MLX5_MKEY_MASK_BSF_EN
;
3047 return cpu_to_be64(result
);
3050 static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg
*umr
,
3051 struct mlx5_ib_mr
*mr
)
3053 int ndescs
= mr
->ndescs
;
3055 memset(umr
, 0, sizeof(*umr
));
3057 if (mr
->access_mode
== MLX5_MKC_ACCESS_MODE_KLMS
)
3058 /* KLMs take twice the size of MTTs */
3061 umr
->flags
= MLX5_UMR_CHECK_NOT_FREE
;
3062 umr
->klm_octowords
= get_klm_octo(ndescs
);
3063 umr
->mkey_mask
= frwr_mkey_mask();
3066 static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg
*umr
)
3068 memset(umr
, 0, sizeof(*umr
));
3069 umr
->mkey_mask
= cpu_to_be64(MLX5_MKEY_MASK_FREE
);
3070 umr
->flags
= 1 << 7;
3073 static __be64
get_umr_reg_mr_mask(void)
3077 result
= MLX5_MKEY_MASK_LEN
|
3078 MLX5_MKEY_MASK_PAGE_SIZE
|
3079 MLX5_MKEY_MASK_START_ADDR
|
3083 MLX5_MKEY_MASK_KEY
|
3087 MLX5_MKEY_MASK_FREE
;
3089 return cpu_to_be64(result
);
3092 static __be64
get_umr_unreg_mr_mask(void)
3096 result
= MLX5_MKEY_MASK_FREE
;
3098 return cpu_to_be64(result
);
3101 static __be64
get_umr_update_mtt_mask(void)
3105 result
= MLX5_MKEY_MASK_FREE
;
3107 return cpu_to_be64(result
);
3110 static __be64
get_umr_update_translation_mask(void)
3114 result
= MLX5_MKEY_MASK_LEN
|
3115 MLX5_MKEY_MASK_PAGE_SIZE
|
3116 MLX5_MKEY_MASK_START_ADDR
|
3117 MLX5_MKEY_MASK_KEY
|
3118 MLX5_MKEY_MASK_FREE
;
3120 return cpu_to_be64(result
);
3123 static __be64
get_umr_update_access_mask(void)
3127 result
= MLX5_MKEY_MASK_LW
|
3131 MLX5_MKEY_MASK_KEY
|
3132 MLX5_MKEY_MASK_FREE
;
3134 return cpu_to_be64(result
);
3137 static __be64
get_umr_update_pd_mask(void)
3141 result
= MLX5_MKEY_MASK_PD
|
3142 MLX5_MKEY_MASK_KEY
|
3143 MLX5_MKEY_MASK_FREE
;
3145 return cpu_to_be64(result
);
3148 static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg
*umr
,
3149 struct ib_send_wr
*wr
)
3151 struct mlx5_umr_wr
*umrwr
= umr_wr(wr
);
3153 memset(umr
, 0, sizeof(*umr
));
3155 if (wr
->send_flags
& MLX5_IB_SEND_UMR_FAIL_IF_FREE
)
3156 umr
->flags
= MLX5_UMR_CHECK_FREE
; /* fail if free */
3158 umr
->flags
= MLX5_UMR_CHECK_NOT_FREE
; /* fail if not free */
3160 if (!(wr
->send_flags
& MLX5_IB_SEND_UMR_UNREG
)) {
3161 umr
->klm_octowords
= get_klm_octo(umrwr
->npages
);
3162 if (wr
->send_flags
& MLX5_IB_SEND_UMR_UPDATE_MTT
) {
3163 umr
->mkey_mask
= get_umr_update_mtt_mask();
3164 umr
->bsf_octowords
= get_klm_octo(umrwr
->target
.offset
);
3165 umr
->flags
|= MLX5_UMR_TRANSLATION_OFFSET_EN
;
3167 if (wr
->send_flags
& MLX5_IB_SEND_UMR_UPDATE_TRANSLATION
)
3168 umr
->mkey_mask
|= get_umr_update_translation_mask();
3169 if (wr
->send_flags
& MLX5_IB_SEND_UMR_UPDATE_ACCESS
)
3170 umr
->mkey_mask
|= get_umr_update_access_mask();
3171 if (wr
->send_flags
& MLX5_IB_SEND_UMR_UPDATE_PD
)
3172 umr
->mkey_mask
|= get_umr_update_pd_mask();
3173 if (!umr
->mkey_mask
)
3174 umr
->mkey_mask
= get_umr_reg_mr_mask();
3176 umr
->mkey_mask
= get_umr_unreg_mr_mask();
3180 umr
->flags
|= MLX5_UMR_INLINE
;
3183 static u8
get_umr_flags(int acc
)
3185 return (acc
& IB_ACCESS_REMOTE_ATOMIC
? MLX5_PERM_ATOMIC
: 0) |
3186 (acc
& IB_ACCESS_REMOTE_WRITE
? MLX5_PERM_REMOTE_WRITE
: 0) |
3187 (acc
& IB_ACCESS_REMOTE_READ
? MLX5_PERM_REMOTE_READ
: 0) |
3188 (acc
& IB_ACCESS_LOCAL_WRITE
? MLX5_PERM_LOCAL_WRITE
: 0) |
3189 MLX5_PERM_LOCAL_READ
| MLX5_PERM_UMR_EN
;
3192 static void set_reg_mkey_seg(struct mlx5_mkey_seg
*seg
,
3193 struct mlx5_ib_mr
*mr
,
3194 u32 key
, int access
)
3196 int ndescs
= ALIGN(mr
->ndescs
, 8) >> 1;
3198 memset(seg
, 0, sizeof(*seg
));
3200 if (mr
->access_mode
== MLX5_MKC_ACCESS_MODE_MTT
)
3201 seg
->log2_page_size
= ilog2(mr
->ibmr
.page_size
);
3202 else if (mr
->access_mode
== MLX5_MKC_ACCESS_MODE_KLMS
)
3203 /* KLMs take twice the size of MTTs */
3206 seg
->flags
= get_umr_flags(access
) | mr
->access_mode
;
3207 seg
->qpn_mkey7_0
= cpu_to_be32((key
& 0xff) | 0xffffff00);
3208 seg
->flags_pd
= cpu_to_be32(MLX5_MKEY_REMOTE_INVAL
);
3209 seg
->start_addr
= cpu_to_be64(mr
->ibmr
.iova
);
3210 seg
->len
= cpu_to_be64(mr
->ibmr
.length
);
3211 seg
->xlt_oct_size
= cpu_to_be32(ndescs
);
3214 static void set_linv_mkey_seg(struct mlx5_mkey_seg
*seg
)
3216 memset(seg
, 0, sizeof(*seg
));
3217 seg
->status
= MLX5_MKEY_STATUS_FREE
;
3220 static void set_reg_mkey_segment(struct mlx5_mkey_seg
*seg
, struct ib_send_wr
*wr
)
3222 struct mlx5_umr_wr
*umrwr
= umr_wr(wr
);
3224 memset(seg
, 0, sizeof(*seg
));
3225 if (wr
->send_flags
& MLX5_IB_SEND_UMR_UNREG
) {
3226 seg
->status
= MLX5_MKEY_STATUS_FREE
;
3230 seg
->flags
= convert_access(umrwr
->access_flags
);
3231 if (!(wr
->send_flags
& MLX5_IB_SEND_UMR_UPDATE_MTT
)) {
3233 seg
->flags_pd
= cpu_to_be32(to_mpd(umrwr
->pd
)->pdn
);
3234 seg
->start_addr
= cpu_to_be64(umrwr
->target
.virt_addr
);
3236 seg
->len
= cpu_to_be64(umrwr
->length
);
3237 seg
->log2_page_size
= umrwr
->page_shift
;
3238 seg
->qpn_mkey7_0
= cpu_to_be32(0xffffff00 |
3239 mlx5_mkey_variant(umrwr
->mkey
));
3242 static void set_reg_data_seg(struct mlx5_wqe_data_seg
*dseg
,
3243 struct mlx5_ib_mr
*mr
,
3244 struct mlx5_ib_pd
*pd
)
3246 int bcount
= mr
->desc_size
* mr
->ndescs
;
3248 dseg
->addr
= cpu_to_be64(mr
->desc_map
);
3249 dseg
->byte_count
= cpu_to_be32(ALIGN(bcount
, 64));
3250 dseg
->lkey
= cpu_to_be32(pd
->ibpd
.local_dma_lkey
);
3253 static __be32
send_ieth(struct ib_send_wr
*wr
)
3255 switch (wr
->opcode
) {
3256 case IB_WR_SEND_WITH_IMM
:
3257 case IB_WR_RDMA_WRITE_WITH_IMM
:
3258 return wr
->ex
.imm_data
;
3260 case IB_WR_SEND_WITH_INV
:
3261 return cpu_to_be32(wr
->ex
.invalidate_rkey
);
3268 static u8
calc_sig(void *wqe
, int size
)
3274 for (i
= 0; i
< size
; i
++)
3280 static u8
wq_sig(void *wqe
)
3282 return calc_sig(wqe
, (*((u8
*)wqe
+ 8) & 0x3f) << 4);
3285 static int set_data_inl_seg(struct mlx5_ib_qp
*qp
, struct ib_send_wr
*wr
,
3288 struct mlx5_wqe_inline_seg
*seg
;
3289 void *qend
= qp
->sq
.qend
;
3297 wqe
+= sizeof(*seg
);
3298 for (i
= 0; i
< wr
->num_sge
; i
++) {
3299 addr
= (void *)(unsigned long)(wr
->sg_list
[i
].addr
);
3300 len
= wr
->sg_list
[i
].length
;
3303 if (unlikely(inl
> qp
->max_inline_data
))
3306 if (unlikely(wqe
+ len
> qend
)) {
3308 memcpy(wqe
, addr
, copy
);
3311 wqe
= mlx5_get_send_wqe(qp
, 0);
3313 memcpy(wqe
, addr
, len
);
3317 seg
->byte_count
= cpu_to_be32(inl
| MLX5_INLINE_SEG
);
3319 *sz
= ALIGN(inl
+ sizeof(seg
->byte_count
), 16) / 16;
3324 static u16
prot_field_size(enum ib_signature_type type
)
3327 case IB_SIG_TYPE_T10_DIF
:
3328 return MLX5_DIF_SIZE
;
3334 static u8
bs_selector(int block_size
)
3336 switch (block_size
) {
3337 case 512: return 0x1;
3338 case 520: return 0x2;
3339 case 4096: return 0x3;
3340 case 4160: return 0x4;
3341 case 1073741824: return 0x5;
3346 static void mlx5_fill_inl_bsf(struct ib_sig_domain
*domain
,
3347 struct mlx5_bsf_inl
*inl
)
3349 /* Valid inline section and allow BSF refresh */
3350 inl
->vld_refresh
= cpu_to_be16(MLX5_BSF_INL_VALID
|
3351 MLX5_BSF_REFRESH_DIF
);
3352 inl
->dif_apptag
= cpu_to_be16(domain
->sig
.dif
.app_tag
);
3353 inl
->dif_reftag
= cpu_to_be32(domain
->sig
.dif
.ref_tag
);
3354 /* repeating block */
3355 inl
->rp_inv_seed
= MLX5_BSF_REPEAT_BLOCK
;
3356 inl
->sig_type
= domain
->sig
.dif
.bg_type
== IB_T10DIF_CRC
?
3357 MLX5_DIF_CRC
: MLX5_DIF_IPCS
;
3359 if (domain
->sig
.dif
.ref_remap
)
3360 inl
->dif_inc_ref_guard_check
|= MLX5_BSF_INC_REFTAG
;
3362 if (domain
->sig
.dif
.app_escape
) {
3363 if (domain
->sig
.dif
.ref_escape
)
3364 inl
->dif_inc_ref_guard_check
|= MLX5_BSF_APPREF_ESCAPE
;
3366 inl
->dif_inc_ref_guard_check
|= MLX5_BSF_APPTAG_ESCAPE
;
3369 inl
->dif_app_bitmask_check
=
3370 cpu_to_be16(domain
->sig
.dif
.apptag_check_mask
);
3373 static int mlx5_set_bsf(struct ib_mr
*sig_mr
,
3374 struct ib_sig_attrs
*sig_attrs
,
3375 struct mlx5_bsf
*bsf
, u32 data_size
)
3377 struct mlx5_core_sig_ctx
*msig
= to_mmr(sig_mr
)->sig
;
3378 struct mlx5_bsf_basic
*basic
= &bsf
->basic
;
3379 struct ib_sig_domain
*mem
= &sig_attrs
->mem
;
3380 struct ib_sig_domain
*wire
= &sig_attrs
->wire
;
3382 memset(bsf
, 0, sizeof(*bsf
));
3384 /* Basic + Extended + Inline */
3385 basic
->bsf_size_sbs
= 1 << 7;
3386 /* Input domain check byte mask */
3387 basic
->check_byte_mask
= sig_attrs
->check_mask
;
3388 basic
->raw_data_size
= cpu_to_be32(data_size
);
3391 switch (sig_attrs
->mem
.sig_type
) {
3392 case IB_SIG_TYPE_NONE
:
3394 case IB_SIG_TYPE_T10_DIF
:
3395 basic
->mem
.bs_selector
= bs_selector(mem
->sig
.dif
.pi_interval
);
3396 basic
->m_bfs_psv
= cpu_to_be32(msig
->psv_memory
.psv_idx
);
3397 mlx5_fill_inl_bsf(mem
, &bsf
->m_inl
);
3404 switch (sig_attrs
->wire
.sig_type
) {
3405 case IB_SIG_TYPE_NONE
:
3407 case IB_SIG_TYPE_T10_DIF
:
3408 if (mem
->sig
.dif
.pi_interval
== wire
->sig
.dif
.pi_interval
&&
3409 mem
->sig_type
== wire
->sig_type
) {
3410 /* Same block structure */
3411 basic
->bsf_size_sbs
|= 1 << 4;
3412 if (mem
->sig
.dif
.bg_type
== wire
->sig
.dif
.bg_type
)
3413 basic
->wire
.copy_byte_mask
|= MLX5_CPY_GRD_MASK
;
3414 if (mem
->sig
.dif
.app_tag
== wire
->sig
.dif
.app_tag
)
3415 basic
->wire
.copy_byte_mask
|= MLX5_CPY_APP_MASK
;
3416 if (mem
->sig
.dif
.ref_tag
== wire
->sig
.dif
.ref_tag
)
3417 basic
->wire
.copy_byte_mask
|= MLX5_CPY_REF_MASK
;
3419 basic
->wire
.bs_selector
= bs_selector(wire
->sig
.dif
.pi_interval
);
3421 basic
->w_bfs_psv
= cpu_to_be32(msig
->psv_wire
.psv_idx
);
3422 mlx5_fill_inl_bsf(wire
, &bsf
->w_inl
);
3431 static int set_sig_data_segment(struct ib_sig_handover_wr
*wr
,
3432 struct mlx5_ib_qp
*qp
, void **seg
, int *size
)
3434 struct ib_sig_attrs
*sig_attrs
= wr
->sig_attrs
;
3435 struct ib_mr
*sig_mr
= wr
->sig_mr
;
3436 struct mlx5_bsf
*bsf
;
3437 u32 data_len
= wr
->wr
.sg_list
->length
;
3438 u32 data_key
= wr
->wr
.sg_list
->lkey
;
3439 u64 data_va
= wr
->wr
.sg_list
->addr
;
3444 (data_key
== wr
->prot
->lkey
&&
3445 data_va
== wr
->prot
->addr
&&
3446 data_len
== wr
->prot
->length
)) {
3448 * Source domain doesn't contain signature information
3449 * or data and protection are interleaved in memory.
3450 * So need construct:
3451 * ------------------
3453 * ------------------
3455 * ------------------
3457 struct mlx5_klm
*data_klm
= *seg
;
3459 data_klm
->bcount
= cpu_to_be32(data_len
);
3460 data_klm
->key
= cpu_to_be32(data_key
);
3461 data_klm
->va
= cpu_to_be64(data_va
);
3462 wqe_size
= ALIGN(sizeof(*data_klm
), 64);
3465 * Source domain contains signature information
3466 * So need construct a strided block format:
3467 * ---------------------------
3468 * | stride_block_ctrl |
3469 * ---------------------------
3471 * ---------------------------
3473 * ---------------------------
3475 * ---------------------------
3477 struct mlx5_stride_block_ctrl_seg
*sblock_ctrl
;
3478 struct mlx5_stride_block_entry
*data_sentry
;
3479 struct mlx5_stride_block_entry
*prot_sentry
;
3480 u32 prot_key
= wr
->prot
->lkey
;
3481 u64 prot_va
= wr
->prot
->addr
;
3482 u16 block_size
= sig_attrs
->mem
.sig
.dif
.pi_interval
;
3486 data_sentry
= (void *)sblock_ctrl
+ sizeof(*sblock_ctrl
);
3487 prot_sentry
= (void *)data_sentry
+ sizeof(*data_sentry
);
3489 prot_size
= prot_field_size(sig_attrs
->mem
.sig_type
);
3491 pr_err("Bad block size given: %u\n", block_size
);
3494 sblock_ctrl
->bcount_per_cycle
= cpu_to_be32(block_size
+
3496 sblock_ctrl
->op
= cpu_to_be32(MLX5_STRIDE_BLOCK_OP
);
3497 sblock_ctrl
->repeat_count
= cpu_to_be32(data_len
/ block_size
);
3498 sblock_ctrl
->num_entries
= cpu_to_be16(2);
3500 data_sentry
->bcount
= cpu_to_be16(block_size
);
3501 data_sentry
->key
= cpu_to_be32(data_key
);
3502 data_sentry
->va
= cpu_to_be64(data_va
);
3503 data_sentry
->stride
= cpu_to_be16(block_size
);
3505 prot_sentry
->bcount
= cpu_to_be16(prot_size
);
3506 prot_sentry
->key
= cpu_to_be32(prot_key
);
3507 prot_sentry
->va
= cpu_to_be64(prot_va
);
3508 prot_sentry
->stride
= cpu_to_be16(prot_size
);
3510 wqe_size
= ALIGN(sizeof(*sblock_ctrl
) + sizeof(*data_sentry
) +
3511 sizeof(*prot_sentry
), 64);
3515 *size
+= wqe_size
/ 16;
3516 if (unlikely((*seg
== qp
->sq
.qend
)))
3517 *seg
= mlx5_get_send_wqe(qp
, 0);
3520 ret
= mlx5_set_bsf(sig_mr
, sig_attrs
, bsf
, data_len
);
3524 *seg
+= sizeof(*bsf
);
3525 *size
+= sizeof(*bsf
) / 16;
3526 if (unlikely((*seg
== qp
->sq
.qend
)))
3527 *seg
= mlx5_get_send_wqe(qp
, 0);
3532 static void set_sig_mkey_segment(struct mlx5_mkey_seg
*seg
,
3533 struct ib_sig_handover_wr
*wr
, u32 nelements
,
3534 u32 length
, u32 pdn
)
3536 struct ib_mr
*sig_mr
= wr
->sig_mr
;
3537 u32 sig_key
= sig_mr
->rkey
;
3538 u8 sigerr
= to_mmr(sig_mr
)->sig
->sigerr_count
& 1;
3540 memset(seg
, 0, sizeof(*seg
));
3542 seg
->flags
= get_umr_flags(wr
->access_flags
) |
3543 MLX5_MKC_ACCESS_MODE_KLMS
;
3544 seg
->qpn_mkey7_0
= cpu_to_be32((sig_key
& 0xff) | 0xffffff00);
3545 seg
->flags_pd
= cpu_to_be32(MLX5_MKEY_REMOTE_INVAL
| sigerr
<< 26 |
3546 MLX5_MKEY_BSF_EN
| pdn
);
3547 seg
->len
= cpu_to_be64(length
);
3548 seg
->xlt_oct_size
= cpu_to_be32(be16_to_cpu(get_klm_octo(nelements
)));
3549 seg
->bsfs_octo_size
= cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE
);
3552 static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg
*umr
,
3555 memset(umr
, 0, sizeof(*umr
));
3557 umr
->flags
= MLX5_FLAGS_INLINE
| MLX5_FLAGS_CHECK_FREE
;
3558 umr
->klm_octowords
= get_klm_octo(nelements
);
3559 umr
->bsf_octowords
= cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE
);
3560 umr
->mkey_mask
= sig_mkey_mask();
3564 static int set_sig_umr_wr(struct ib_send_wr
*send_wr
, struct mlx5_ib_qp
*qp
,
3565 void **seg
, int *size
)
3567 struct ib_sig_handover_wr
*wr
= sig_handover_wr(send_wr
);
3568 struct mlx5_ib_mr
*sig_mr
= to_mmr(wr
->sig_mr
);
3569 u32 pdn
= get_pd(qp
)->pdn
;
3571 int region_len
, ret
;
3573 if (unlikely(wr
->wr
.num_sge
!= 1) ||
3574 unlikely(wr
->access_flags
& IB_ACCESS_REMOTE_ATOMIC
) ||
3575 unlikely(!sig_mr
->sig
) || unlikely(!qp
->signature_en
) ||
3576 unlikely(!sig_mr
->sig
->sig_status_checked
))
3579 /* length of the protected region, data + protection */
3580 region_len
= wr
->wr
.sg_list
->length
;
3582 (wr
->prot
->lkey
!= wr
->wr
.sg_list
->lkey
||
3583 wr
->prot
->addr
!= wr
->wr
.sg_list
->addr
||
3584 wr
->prot
->length
!= wr
->wr
.sg_list
->length
))
3585 region_len
+= wr
->prot
->length
;
3588 * KLM octoword size - if protection was provided
3589 * then we use strided block format (3 octowords),
3590 * else we use single KLM (1 octoword)
3592 klm_oct_size
= wr
->prot
? 3 : 1;
3594 set_sig_umr_segment(*seg
, klm_oct_size
);
3595 *seg
+= sizeof(struct mlx5_wqe_umr_ctrl_seg
);
3596 *size
+= sizeof(struct mlx5_wqe_umr_ctrl_seg
) / 16;
3597 if (unlikely((*seg
== qp
->sq
.qend
)))
3598 *seg
= mlx5_get_send_wqe(qp
, 0);
3600 set_sig_mkey_segment(*seg
, wr
, klm_oct_size
, region_len
, pdn
);
3601 *seg
+= sizeof(struct mlx5_mkey_seg
);
3602 *size
+= sizeof(struct mlx5_mkey_seg
) / 16;
3603 if (unlikely((*seg
== qp
->sq
.qend
)))
3604 *seg
= mlx5_get_send_wqe(qp
, 0);
3606 ret
= set_sig_data_segment(wr
, qp
, seg
, size
);
3610 sig_mr
->sig
->sig_status_checked
= false;
3614 static int set_psv_wr(struct ib_sig_domain
*domain
,
3615 u32 psv_idx
, void **seg
, int *size
)
3617 struct mlx5_seg_set_psv
*psv_seg
= *seg
;
3619 memset(psv_seg
, 0, sizeof(*psv_seg
));
3620 psv_seg
->psv_num
= cpu_to_be32(psv_idx
);
3621 switch (domain
->sig_type
) {
3622 case IB_SIG_TYPE_NONE
:
3624 case IB_SIG_TYPE_T10_DIF
:
3625 psv_seg
->transient_sig
= cpu_to_be32(domain
->sig
.dif
.bg
<< 16 |
3626 domain
->sig
.dif
.app_tag
);
3627 psv_seg
->ref_tag
= cpu_to_be32(domain
->sig
.dif
.ref_tag
);
3630 pr_err("Bad signature type given.\n");
3634 *seg
+= sizeof(*psv_seg
);
3635 *size
+= sizeof(*psv_seg
) / 16;
3640 static int set_reg_wr(struct mlx5_ib_qp
*qp
,
3641 struct ib_reg_wr
*wr
,
3642 void **seg
, int *size
)
3644 struct mlx5_ib_mr
*mr
= to_mmr(wr
->mr
);
3645 struct mlx5_ib_pd
*pd
= to_mpd(qp
->ibqp
.pd
);
3647 if (unlikely(wr
->wr
.send_flags
& IB_SEND_INLINE
)) {
3648 mlx5_ib_warn(to_mdev(qp
->ibqp
.device
),
3649 "Invalid IB_SEND_INLINE send flag\n");
3653 set_reg_umr_seg(*seg
, mr
);
3654 *seg
+= sizeof(struct mlx5_wqe_umr_ctrl_seg
);
3655 *size
+= sizeof(struct mlx5_wqe_umr_ctrl_seg
) / 16;
3656 if (unlikely((*seg
== qp
->sq
.qend
)))
3657 *seg
= mlx5_get_send_wqe(qp
, 0);
3659 set_reg_mkey_seg(*seg
, mr
, wr
->key
, wr
->access
);
3660 *seg
+= sizeof(struct mlx5_mkey_seg
);
3661 *size
+= sizeof(struct mlx5_mkey_seg
) / 16;
3662 if (unlikely((*seg
== qp
->sq
.qend
)))
3663 *seg
= mlx5_get_send_wqe(qp
, 0);
3665 set_reg_data_seg(*seg
, mr
, pd
);
3666 *seg
+= sizeof(struct mlx5_wqe_data_seg
);
3667 *size
+= (sizeof(struct mlx5_wqe_data_seg
) / 16);
3672 static void set_linv_wr(struct mlx5_ib_qp
*qp
, void **seg
, int *size
)
3674 set_linv_umr_seg(*seg
);
3675 *seg
+= sizeof(struct mlx5_wqe_umr_ctrl_seg
);
3676 *size
+= sizeof(struct mlx5_wqe_umr_ctrl_seg
) / 16;
3677 if (unlikely((*seg
== qp
->sq
.qend
)))
3678 *seg
= mlx5_get_send_wqe(qp
, 0);
3679 set_linv_mkey_seg(*seg
);
3680 *seg
+= sizeof(struct mlx5_mkey_seg
);
3681 *size
+= sizeof(struct mlx5_mkey_seg
) / 16;
3682 if (unlikely((*seg
== qp
->sq
.qend
)))
3683 *seg
= mlx5_get_send_wqe(qp
, 0);
3686 static void dump_wqe(struct mlx5_ib_qp
*qp
, int idx
, int size_16
)
3692 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp
, tidx
));
3693 for (i
= 0, j
= 0; i
< size_16
* 4; i
+= 4, j
+= 4) {
3694 if ((i
& 0xf) == 0) {
3695 void *buf
= mlx5_get_send_wqe(qp
, tidx
);
3696 tidx
= (tidx
+ 1) & (qp
->sq
.wqe_cnt
- 1);
3700 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p
[j
]),
3701 be32_to_cpu(p
[j
+ 1]), be32_to_cpu(p
[j
+ 2]),
3702 be32_to_cpu(p
[j
+ 3]));
3706 static void mlx5_bf_copy(u64 __iomem
*dst
, u64
*src
,
3707 unsigned bytecnt
, struct mlx5_ib_qp
*qp
)
3709 while (bytecnt
> 0) {
3710 __iowrite64_copy(dst
++, src
++, 8);
3711 __iowrite64_copy(dst
++, src
++, 8);
3712 __iowrite64_copy(dst
++, src
++, 8);
3713 __iowrite64_copy(dst
++, src
++, 8);
3714 __iowrite64_copy(dst
++, src
++, 8);
3715 __iowrite64_copy(dst
++, src
++, 8);
3716 __iowrite64_copy(dst
++, src
++, 8);
3717 __iowrite64_copy(dst
++, src
++, 8);
3719 if (unlikely(src
== qp
->sq
.qend
))
3720 src
= mlx5_get_send_wqe(qp
, 0);
3724 static u8
get_fence(u8 fence
, struct ib_send_wr
*wr
)
3726 if (unlikely(wr
->opcode
== IB_WR_LOCAL_INV
&&
3727 wr
->send_flags
& IB_SEND_FENCE
))
3728 return MLX5_FENCE_MODE_STRONG_ORDERING
;
3730 if (unlikely(fence
)) {
3731 if (wr
->send_flags
& IB_SEND_FENCE
)
3732 return MLX5_FENCE_MODE_SMALL_AND_FENCE
;
3735 } else if (unlikely(wr
->send_flags
& IB_SEND_FENCE
)) {
3736 return MLX5_FENCE_MODE_FENCE
;
3742 static int begin_wqe(struct mlx5_ib_qp
*qp
, void **seg
,
3743 struct mlx5_wqe_ctrl_seg
**ctrl
,
3744 struct ib_send_wr
*wr
, unsigned *idx
,
3745 int *size
, int nreq
)
3747 if (unlikely(mlx5_wq_overflow(&qp
->sq
, nreq
, qp
->ibqp
.send_cq
)))
3750 *idx
= qp
->sq
.cur_post
& (qp
->sq
.wqe_cnt
- 1);
3751 *seg
= mlx5_get_send_wqe(qp
, *idx
);
3753 *(uint32_t *)(*seg
+ 8) = 0;
3754 (*ctrl
)->imm
= send_ieth(wr
);
3755 (*ctrl
)->fm_ce_se
= qp
->sq_signal_bits
|
3756 (wr
->send_flags
& IB_SEND_SIGNALED
?
3757 MLX5_WQE_CTRL_CQ_UPDATE
: 0) |
3758 (wr
->send_flags
& IB_SEND_SOLICITED
?
3759 MLX5_WQE_CTRL_SOLICITED
: 0);
3761 *seg
+= sizeof(**ctrl
);
3762 *size
= sizeof(**ctrl
) / 16;
3767 static void finish_wqe(struct mlx5_ib_qp
*qp
,
3768 struct mlx5_wqe_ctrl_seg
*ctrl
,
3769 u8 size
, unsigned idx
, u64 wr_id
,
3770 int nreq
, u8 fence
, u8 next_fence
,
3775 ctrl
->opmod_idx_opcode
= cpu_to_be32(((u32
)(qp
->sq
.cur_post
) << 8) |
3776 mlx5_opcode
| ((u32
)opmod
<< 24));
3777 ctrl
->qpn_ds
= cpu_to_be32(size
| (qp
->trans_qp
.base
.mqp
.qpn
<< 8));
3778 ctrl
->fm_ce_se
|= fence
;
3779 qp
->fm_cache
= next_fence
;
3780 if (unlikely(qp
->wq_sig
))
3781 ctrl
->signature
= wq_sig(ctrl
);
3783 qp
->sq
.wrid
[idx
] = wr_id
;
3784 qp
->sq
.w_list
[idx
].opcode
= mlx5_opcode
;
3785 qp
->sq
.wqe_head
[idx
] = qp
->sq
.head
+ nreq
;
3786 qp
->sq
.cur_post
+= DIV_ROUND_UP(size
* 16, MLX5_SEND_WQE_BB
);
3787 qp
->sq
.w_list
[idx
].next
= qp
->sq
.cur_post
;
3791 int mlx5_ib_post_send(struct ib_qp
*ibqp
, struct ib_send_wr
*wr
,
3792 struct ib_send_wr
**bad_wr
)
3794 struct mlx5_wqe_ctrl_seg
*ctrl
= NULL
; /* compiler warning */
3795 struct mlx5_ib_dev
*dev
= to_mdev(ibqp
->device
);
3796 struct mlx5_core_dev
*mdev
= dev
->mdev
;
3797 struct mlx5_ib_qp
*qp
;
3798 struct mlx5_ib_mr
*mr
;
3799 struct mlx5_wqe_data_seg
*dpseg
;
3800 struct mlx5_wqe_xrc_seg
*xrc
;
3802 int uninitialized_var(size
);
3804 unsigned long flags
;
3815 if (unlikely(ibqp
->qp_type
== IB_QPT_GSI
))
3816 return mlx5_ib_gsi_post_send(ibqp
, wr
, bad_wr
);
3822 spin_lock_irqsave(&qp
->sq
.lock
, flags
);
3824 if (mdev
->state
== MLX5_DEVICE_STATE_INTERNAL_ERROR
) {
3831 for (nreq
= 0; wr
; nreq
++, wr
= wr
->next
) {
3832 if (unlikely(wr
->opcode
>= ARRAY_SIZE(mlx5_ib_opcode
))) {
3833 mlx5_ib_warn(dev
, "\n");
3839 fence
= qp
->fm_cache
;
3840 num_sge
= wr
->num_sge
;
3841 if (unlikely(num_sge
> qp
->sq
.max_gs
)) {
3842 mlx5_ib_warn(dev
, "\n");
3848 err
= begin_wqe(qp
, &seg
, &ctrl
, wr
, &idx
, &size
, nreq
);
3850 mlx5_ib_warn(dev
, "\n");
3856 switch (ibqp
->qp_type
) {
3857 case IB_QPT_XRC_INI
:
3859 seg
+= sizeof(*xrc
);
3860 size
+= sizeof(*xrc
) / 16;
3863 switch (wr
->opcode
) {
3864 case IB_WR_RDMA_READ
:
3865 case IB_WR_RDMA_WRITE
:
3866 case IB_WR_RDMA_WRITE_WITH_IMM
:
3867 set_raddr_seg(seg
, rdma_wr(wr
)->remote_addr
,
3869 seg
+= sizeof(struct mlx5_wqe_raddr_seg
);
3870 size
+= sizeof(struct mlx5_wqe_raddr_seg
) / 16;
3873 case IB_WR_ATOMIC_CMP_AND_SWP
:
3874 case IB_WR_ATOMIC_FETCH_AND_ADD
:
3875 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP
:
3876 mlx5_ib_warn(dev
, "Atomic operations are not supported yet\n");
3881 case IB_WR_LOCAL_INV
:
3882 next_fence
= MLX5_FENCE_MODE_INITIATOR_SMALL
;
3883 qp
->sq
.wr_data
[idx
] = IB_WR_LOCAL_INV
;
3884 ctrl
->imm
= cpu_to_be32(wr
->ex
.invalidate_rkey
);
3885 set_linv_wr(qp
, &seg
, &size
);
3890 next_fence
= MLX5_FENCE_MODE_INITIATOR_SMALL
;
3891 qp
->sq
.wr_data
[idx
] = IB_WR_REG_MR
;
3892 ctrl
->imm
= cpu_to_be32(reg_wr(wr
)->key
);
3893 err
= set_reg_wr(qp
, reg_wr(wr
), &seg
, &size
);
3901 case IB_WR_REG_SIG_MR
:
3902 qp
->sq
.wr_data
[idx
] = IB_WR_REG_SIG_MR
;
3903 mr
= to_mmr(sig_handover_wr(wr
)->sig_mr
);
3905 ctrl
->imm
= cpu_to_be32(mr
->ibmr
.rkey
);
3906 err
= set_sig_umr_wr(wr
, qp
, &seg
, &size
);
3908 mlx5_ib_warn(dev
, "\n");
3913 finish_wqe(qp
, ctrl
, size
, idx
, wr
->wr_id
,
3914 nreq
, get_fence(fence
, wr
),
3915 next_fence
, MLX5_OPCODE_UMR
);
3917 * SET_PSV WQEs are not signaled and solicited
3920 wr
->send_flags
&= ~IB_SEND_SIGNALED
;
3921 wr
->send_flags
|= IB_SEND_SOLICITED
;
3922 err
= begin_wqe(qp
, &seg
, &ctrl
, wr
,
3925 mlx5_ib_warn(dev
, "\n");
3931 err
= set_psv_wr(&sig_handover_wr(wr
)->sig_attrs
->mem
,
3932 mr
->sig
->psv_memory
.psv_idx
, &seg
,
3935 mlx5_ib_warn(dev
, "\n");
3940 finish_wqe(qp
, ctrl
, size
, idx
, wr
->wr_id
,
3941 nreq
, get_fence(fence
, wr
),
3942 next_fence
, MLX5_OPCODE_SET_PSV
);
3943 err
= begin_wqe(qp
, &seg
, &ctrl
, wr
,
3946 mlx5_ib_warn(dev
, "\n");
3952 next_fence
= MLX5_FENCE_MODE_INITIATOR_SMALL
;
3953 err
= set_psv_wr(&sig_handover_wr(wr
)->sig_attrs
->wire
,
3954 mr
->sig
->psv_wire
.psv_idx
, &seg
,
3957 mlx5_ib_warn(dev
, "\n");
3962 finish_wqe(qp
, ctrl
, size
, idx
, wr
->wr_id
,
3963 nreq
, get_fence(fence
, wr
),
3964 next_fence
, MLX5_OPCODE_SET_PSV
);
3974 switch (wr
->opcode
) {
3975 case IB_WR_RDMA_WRITE
:
3976 case IB_WR_RDMA_WRITE_WITH_IMM
:
3977 set_raddr_seg(seg
, rdma_wr(wr
)->remote_addr
,
3979 seg
+= sizeof(struct mlx5_wqe_raddr_seg
);
3980 size
+= sizeof(struct mlx5_wqe_raddr_seg
) / 16;
3989 case MLX5_IB_QPT_HW_GSI
:
3990 set_datagram_seg(seg
, wr
);
3991 seg
+= sizeof(struct mlx5_wqe_datagram_seg
);
3992 size
+= sizeof(struct mlx5_wqe_datagram_seg
) / 16;
3993 if (unlikely((seg
== qend
)))
3994 seg
= mlx5_get_send_wqe(qp
, 0);
3997 set_datagram_seg(seg
, wr
);
3998 seg
+= sizeof(struct mlx5_wqe_datagram_seg
);
3999 size
+= sizeof(struct mlx5_wqe_datagram_seg
) / 16;
4001 if (unlikely((seg
== qend
)))
4002 seg
= mlx5_get_send_wqe(qp
, 0);
4004 /* handle qp that supports ud offload */
4005 if (qp
->flags
& IB_QP_CREATE_IPOIB_UD_LSO
) {
4006 struct mlx5_wqe_eth_pad
*pad
;
4009 memset(pad
, 0, sizeof(struct mlx5_wqe_eth_pad
));
4010 seg
+= sizeof(struct mlx5_wqe_eth_pad
);
4011 size
+= sizeof(struct mlx5_wqe_eth_pad
) / 16;
4013 seg
= set_eth_seg(seg
, wr
, qend
, qp
, &size
);
4015 if (unlikely((seg
== qend
)))
4016 seg
= mlx5_get_send_wqe(qp
, 0);
4019 case MLX5_IB_QPT_REG_UMR
:
4020 if (wr
->opcode
!= MLX5_IB_WR_UMR
) {
4022 mlx5_ib_warn(dev
, "bad opcode\n");
4025 qp
->sq
.wr_data
[idx
] = MLX5_IB_WR_UMR
;
4026 ctrl
->imm
= cpu_to_be32(umr_wr(wr
)->mkey
);
4027 set_reg_umr_segment(seg
, wr
);
4028 seg
+= sizeof(struct mlx5_wqe_umr_ctrl_seg
);
4029 size
+= sizeof(struct mlx5_wqe_umr_ctrl_seg
) / 16;
4030 if (unlikely((seg
== qend
)))
4031 seg
= mlx5_get_send_wqe(qp
, 0);
4032 set_reg_mkey_segment(seg
, wr
);
4033 seg
+= sizeof(struct mlx5_mkey_seg
);
4034 size
+= sizeof(struct mlx5_mkey_seg
) / 16;
4035 if (unlikely((seg
== qend
)))
4036 seg
= mlx5_get_send_wqe(qp
, 0);
4043 if (wr
->send_flags
& IB_SEND_INLINE
&& num_sge
) {
4044 int uninitialized_var(sz
);
4046 err
= set_data_inl_seg(qp
, wr
, seg
, &sz
);
4047 if (unlikely(err
)) {
4048 mlx5_ib_warn(dev
, "\n");
4056 for (i
= 0; i
< num_sge
; i
++) {
4057 if (unlikely(dpseg
== qend
)) {
4058 seg
= mlx5_get_send_wqe(qp
, 0);
4061 if (likely(wr
->sg_list
[i
].length
)) {
4062 set_data_ptr_seg(dpseg
, wr
->sg_list
+ i
);
4063 size
+= sizeof(struct mlx5_wqe_data_seg
) / 16;
4069 finish_wqe(qp
, ctrl
, size
, idx
, wr
->wr_id
, nreq
,
4070 get_fence(fence
, wr
), next_fence
,
4071 mlx5_ib_opcode
[wr
->opcode
]);
4074 dump_wqe(qp
, idx
, size
);
4079 qp
->sq
.head
+= nreq
;
4081 /* Make sure that descriptors are written before
4082 * updating doorbell record and ringing the doorbell
4086 qp
->db
.db
[MLX5_SND_DBR
] = cpu_to_be32(qp
->sq
.cur_post
);
4088 /* Make sure doorbell record is visible to the HCA before
4089 * we hit doorbell */
4093 spin_lock(&bf
->lock
);
4095 __acquire(&bf
->lock
);
4098 if (0 && nreq
== 1 && bf
->uuarn
&& inl
&& size
> 1 && size
<= bf
->buf_size
/ 16) {
4099 mlx5_bf_copy(bf
->reg
+ bf
->offset
, (u64
*)ctrl
, ALIGN(size
* 16, 64), qp
);
4102 mlx5_write64((__be32
*)ctrl
, bf
->regreg
+ bf
->offset
,
4103 MLX5_GET_DOORBELL_LOCK(&bf
->lock32
));
4104 /* Make sure doorbells don't leak out of SQ spinlock
4105 * and reach the HCA out of order.
4109 bf
->offset
^= bf
->buf_size
;
4111 spin_unlock(&bf
->lock
);
4113 __release(&bf
->lock
);
4116 spin_unlock_irqrestore(&qp
->sq
.lock
, flags
);
4121 static void set_sig_seg(struct mlx5_rwqe_sig
*sig
, int size
)
4123 sig
->signature
= calc_sig(sig
, size
);
4126 int mlx5_ib_post_recv(struct ib_qp
*ibqp
, struct ib_recv_wr
*wr
,
4127 struct ib_recv_wr
**bad_wr
)
4129 struct mlx5_ib_qp
*qp
= to_mqp(ibqp
);
4130 struct mlx5_wqe_data_seg
*scat
;
4131 struct mlx5_rwqe_sig
*sig
;
4132 struct mlx5_ib_dev
*dev
= to_mdev(ibqp
->device
);
4133 struct mlx5_core_dev
*mdev
= dev
->mdev
;
4134 unsigned long flags
;
4140 if (unlikely(ibqp
->qp_type
== IB_QPT_GSI
))
4141 return mlx5_ib_gsi_post_recv(ibqp
, wr
, bad_wr
);
4143 spin_lock_irqsave(&qp
->rq
.lock
, flags
);
4145 if (mdev
->state
== MLX5_DEVICE_STATE_INTERNAL_ERROR
) {
4152 ind
= qp
->rq
.head
& (qp
->rq
.wqe_cnt
- 1);
4154 for (nreq
= 0; wr
; nreq
++, wr
= wr
->next
) {
4155 if (mlx5_wq_overflow(&qp
->rq
, nreq
, qp
->ibqp
.recv_cq
)) {
4161 if (unlikely(wr
->num_sge
> qp
->rq
.max_gs
)) {
4167 scat
= get_recv_wqe(qp
, ind
);
4171 for (i
= 0; i
< wr
->num_sge
; i
++)
4172 set_data_ptr_seg(scat
+ i
, wr
->sg_list
+ i
);
4174 if (i
< qp
->rq
.max_gs
) {
4175 scat
[i
].byte_count
= 0;
4176 scat
[i
].lkey
= cpu_to_be32(MLX5_INVALID_LKEY
);
4181 sig
= (struct mlx5_rwqe_sig
*)scat
;
4182 set_sig_seg(sig
, (qp
->rq
.max_gs
+ 1) << 2);
4185 qp
->rq
.wrid
[ind
] = wr
->wr_id
;
4187 ind
= (ind
+ 1) & (qp
->rq
.wqe_cnt
- 1);
4192 qp
->rq
.head
+= nreq
;
4194 /* Make sure that descriptors are written before
4199 *qp
->db
.db
= cpu_to_be32(qp
->rq
.head
& 0xffff);
4202 spin_unlock_irqrestore(&qp
->rq
.lock
, flags
);
4207 static inline enum ib_qp_state
to_ib_qp_state(enum mlx5_qp_state mlx5_state
)
4209 switch (mlx5_state
) {
4210 case MLX5_QP_STATE_RST
: return IB_QPS_RESET
;
4211 case MLX5_QP_STATE_INIT
: return IB_QPS_INIT
;
4212 case MLX5_QP_STATE_RTR
: return IB_QPS_RTR
;
4213 case MLX5_QP_STATE_RTS
: return IB_QPS_RTS
;
4214 case MLX5_QP_STATE_SQ_DRAINING
:
4215 case MLX5_QP_STATE_SQD
: return IB_QPS_SQD
;
4216 case MLX5_QP_STATE_SQER
: return IB_QPS_SQE
;
4217 case MLX5_QP_STATE_ERR
: return IB_QPS_ERR
;
4222 static inline enum ib_mig_state
to_ib_mig_state(int mlx5_mig_state
)
4224 switch (mlx5_mig_state
) {
4225 case MLX5_QP_PM_ARMED
: return IB_MIG_ARMED
;
4226 case MLX5_QP_PM_REARM
: return IB_MIG_REARM
;
4227 case MLX5_QP_PM_MIGRATED
: return IB_MIG_MIGRATED
;
4232 static int to_ib_qp_access_flags(int mlx5_flags
)
4236 if (mlx5_flags
& MLX5_QP_BIT_RRE
)
4237 ib_flags
|= IB_ACCESS_REMOTE_READ
;
4238 if (mlx5_flags
& MLX5_QP_BIT_RWE
)
4239 ib_flags
|= IB_ACCESS_REMOTE_WRITE
;
4240 if (mlx5_flags
& MLX5_QP_BIT_RAE
)
4241 ib_flags
|= IB_ACCESS_REMOTE_ATOMIC
;
4246 static void to_ib_ah_attr(struct mlx5_ib_dev
*ibdev
, struct ib_ah_attr
*ib_ah_attr
,
4247 struct mlx5_qp_path
*path
)
4249 struct mlx5_core_dev
*dev
= ibdev
->mdev
;
4251 memset(ib_ah_attr
, 0, sizeof(*ib_ah_attr
));
4252 ib_ah_attr
->port_num
= path
->port
;
4254 if (ib_ah_attr
->port_num
== 0 ||
4255 ib_ah_attr
->port_num
> MLX5_CAP_GEN(dev
, num_ports
))
4258 ib_ah_attr
->sl
= path
->dci_cfi_prio_sl
& 0xf;
4260 ib_ah_attr
->dlid
= be16_to_cpu(path
->rlid
);
4261 ib_ah_attr
->src_path_bits
= path
->grh_mlid
& 0x7f;
4262 ib_ah_attr
->static_rate
= path
->static_rate
? path
->static_rate
- 5 : 0;
4263 ib_ah_attr
->ah_flags
= (path
->grh_mlid
& (1 << 7)) ? IB_AH_GRH
: 0;
4264 if (ib_ah_attr
->ah_flags
) {
4265 ib_ah_attr
->grh
.sgid_index
= path
->mgid_index
;
4266 ib_ah_attr
->grh
.hop_limit
= path
->hop_limit
;
4267 ib_ah_attr
->grh
.traffic_class
=
4268 (be32_to_cpu(path
->tclass_flowlabel
) >> 20) & 0xff;
4269 ib_ah_attr
->grh
.flow_label
=
4270 be32_to_cpu(path
->tclass_flowlabel
) & 0xfffff;
4271 memcpy(ib_ah_attr
->grh
.dgid
.raw
,
4272 path
->rgid
, sizeof(ib_ah_attr
->grh
.dgid
.raw
));
4276 static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev
*dev
,
4277 struct mlx5_ib_sq
*sq
,
4285 inlen
= MLX5_ST_SZ_BYTES(query_sq_out
);
4286 out
= mlx5_vzalloc(inlen
);
4290 err
= mlx5_core_query_sq(dev
->mdev
, sq
->base
.mqp
.qpn
, out
);
4294 sqc
= MLX5_ADDR_OF(query_sq_out
, out
, sq_context
);
4295 *sq_state
= MLX5_GET(sqc
, sqc
, state
);
4296 sq
->state
= *sq_state
;
4303 static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev
*dev
,
4304 struct mlx5_ib_rq
*rq
,
4312 inlen
= MLX5_ST_SZ_BYTES(query_rq_out
);
4313 out
= mlx5_vzalloc(inlen
);
4317 err
= mlx5_core_query_rq(dev
->mdev
, rq
->base
.mqp
.qpn
, out
);
4321 rqc
= MLX5_ADDR_OF(query_rq_out
, out
, rq_context
);
4322 *rq_state
= MLX5_GET(rqc
, rqc
, state
);
4323 rq
->state
= *rq_state
;
4330 static int sqrq_state_to_qp_state(u8 sq_state
, u8 rq_state
,
4331 struct mlx5_ib_qp
*qp
, u8
*qp_state
)
4333 static const u8 sqrq_trans
[MLX5_RQ_NUM_STATE
][MLX5_SQ_NUM_STATE
] = {
4334 [MLX5_RQC_STATE_RST
] = {
4335 [MLX5_SQC_STATE_RST
] = IB_QPS_RESET
,
4336 [MLX5_SQC_STATE_RDY
] = MLX5_QP_STATE_BAD
,
4337 [MLX5_SQC_STATE_ERR
] = MLX5_QP_STATE_BAD
,
4338 [MLX5_SQ_STATE_NA
] = IB_QPS_RESET
,
4340 [MLX5_RQC_STATE_RDY
] = {
4341 [MLX5_SQC_STATE_RST
] = MLX5_QP_STATE_BAD
,
4342 [MLX5_SQC_STATE_RDY
] = MLX5_QP_STATE
,
4343 [MLX5_SQC_STATE_ERR
] = IB_QPS_SQE
,
4344 [MLX5_SQ_STATE_NA
] = MLX5_QP_STATE
,
4346 [MLX5_RQC_STATE_ERR
] = {
4347 [MLX5_SQC_STATE_RST
] = MLX5_QP_STATE_BAD
,
4348 [MLX5_SQC_STATE_RDY
] = MLX5_QP_STATE_BAD
,
4349 [MLX5_SQC_STATE_ERR
] = IB_QPS_ERR
,
4350 [MLX5_SQ_STATE_NA
] = IB_QPS_ERR
,
4352 [MLX5_RQ_STATE_NA
] = {
4353 [MLX5_SQC_STATE_RST
] = IB_QPS_RESET
,
4354 [MLX5_SQC_STATE_RDY
] = MLX5_QP_STATE
,
4355 [MLX5_SQC_STATE_ERR
] = MLX5_QP_STATE
,
4356 [MLX5_SQ_STATE_NA
] = MLX5_QP_STATE_BAD
,
4360 *qp_state
= sqrq_trans
[rq_state
][sq_state
];
4362 if (*qp_state
== MLX5_QP_STATE_BAD
) {
4363 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4364 qp
->raw_packet_qp
.sq
.base
.mqp
.qpn
, sq_state
,
4365 qp
->raw_packet_qp
.rq
.base
.mqp
.qpn
, rq_state
);
4369 if (*qp_state
== MLX5_QP_STATE
)
4370 *qp_state
= qp
->state
;
4375 static int query_raw_packet_qp_state(struct mlx5_ib_dev
*dev
,
4376 struct mlx5_ib_qp
*qp
,
4377 u8
*raw_packet_qp_state
)
4379 struct mlx5_ib_raw_packet_qp
*raw_packet_qp
= &qp
->raw_packet_qp
;
4380 struct mlx5_ib_sq
*sq
= &raw_packet_qp
->sq
;
4381 struct mlx5_ib_rq
*rq
= &raw_packet_qp
->rq
;
4383 u8 sq_state
= MLX5_SQ_STATE_NA
;
4384 u8 rq_state
= MLX5_RQ_STATE_NA
;
4386 if (qp
->sq
.wqe_cnt
) {
4387 err
= query_raw_packet_qp_sq_state(dev
, sq
, &sq_state
);
4392 if (qp
->rq
.wqe_cnt
) {
4393 err
= query_raw_packet_qp_rq_state(dev
, rq
, &rq_state
);
4398 return sqrq_state_to_qp_state(sq_state
, rq_state
, qp
,
4399 raw_packet_qp_state
);
4402 static int query_qp_attr(struct mlx5_ib_dev
*dev
, struct mlx5_ib_qp
*qp
,
4403 struct ib_qp_attr
*qp_attr
)
4405 int outlen
= MLX5_ST_SZ_BYTES(query_qp_out
);
4406 struct mlx5_qp_context
*context
;
4411 outb
= kzalloc(outlen
, GFP_KERNEL
);
4415 err
= mlx5_core_qp_query(dev
->mdev
, &qp
->trans_qp
.base
.mqp
, outb
,
4420 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
4421 context
= (struct mlx5_qp_context
*)MLX5_ADDR_OF(query_qp_out
, outb
, qpc
);
4423 mlx5_state
= be32_to_cpu(context
->flags
) >> 28;
4425 qp
->state
= to_ib_qp_state(mlx5_state
);
4426 qp_attr
->path_mtu
= context
->mtu_msgmax
>> 5;
4427 qp_attr
->path_mig_state
=
4428 to_ib_mig_state((be32_to_cpu(context
->flags
) >> 11) & 0x3);
4429 qp_attr
->qkey
= be32_to_cpu(context
->qkey
);
4430 qp_attr
->rq_psn
= be32_to_cpu(context
->rnr_nextrecvpsn
) & 0xffffff;
4431 qp_attr
->sq_psn
= be32_to_cpu(context
->next_send_psn
) & 0xffffff;
4432 qp_attr
->dest_qp_num
= be32_to_cpu(context
->log_pg_sz_remote_qpn
) & 0xffffff;
4433 qp_attr
->qp_access_flags
=
4434 to_ib_qp_access_flags(be32_to_cpu(context
->params2
));
4436 if (qp
->ibqp
.qp_type
== IB_QPT_RC
|| qp
->ibqp
.qp_type
== IB_QPT_UC
) {
4437 to_ib_ah_attr(dev
, &qp_attr
->ah_attr
, &context
->pri_path
);
4438 to_ib_ah_attr(dev
, &qp_attr
->alt_ah_attr
, &context
->alt_path
);
4439 qp_attr
->alt_pkey_index
=
4440 be16_to_cpu(context
->alt_path
.pkey_index
);
4441 qp_attr
->alt_port_num
= qp_attr
->alt_ah_attr
.port_num
;
4444 qp_attr
->pkey_index
= be16_to_cpu(context
->pri_path
.pkey_index
);
4445 qp_attr
->port_num
= context
->pri_path
.port
;
4447 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4448 qp_attr
->sq_draining
= mlx5_state
== MLX5_QP_STATE_SQ_DRAINING
;
4450 qp_attr
->max_rd_atomic
= 1 << ((be32_to_cpu(context
->params1
) >> 21) & 0x7);
4452 qp_attr
->max_dest_rd_atomic
=
4453 1 << ((be32_to_cpu(context
->params2
) >> 21) & 0x7);
4454 qp_attr
->min_rnr_timer
=
4455 (be32_to_cpu(context
->rnr_nextrecvpsn
) >> 24) & 0x1f;
4456 qp_attr
->timeout
= context
->pri_path
.ackto_lt
>> 3;
4457 qp_attr
->retry_cnt
= (be32_to_cpu(context
->params1
) >> 16) & 0x7;
4458 qp_attr
->rnr_retry
= (be32_to_cpu(context
->params1
) >> 13) & 0x7;
4459 qp_attr
->alt_timeout
= context
->alt_path
.ackto_lt
>> 3;
4466 int mlx5_ib_query_qp(struct ib_qp
*ibqp
, struct ib_qp_attr
*qp_attr
,
4467 int qp_attr_mask
, struct ib_qp_init_attr
*qp_init_attr
)
4469 struct mlx5_ib_dev
*dev
= to_mdev(ibqp
->device
);
4470 struct mlx5_ib_qp
*qp
= to_mqp(ibqp
);
4472 u8 raw_packet_qp_state
;
4474 if (ibqp
->rwq_ind_tbl
)
4477 if (unlikely(ibqp
->qp_type
== IB_QPT_GSI
))
4478 return mlx5_ib_gsi_query_qp(ibqp
, qp_attr
, qp_attr_mask
,
4481 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4483 * Wait for any outstanding page faults, in case the user frees memory
4484 * based upon this query's result.
4486 flush_workqueue(mlx5_ib_page_fault_wq
);
4489 mutex_lock(&qp
->mutex
);
4491 if (qp
->ibqp
.qp_type
== IB_QPT_RAW_PACKET
) {
4492 err
= query_raw_packet_qp_state(dev
, qp
, &raw_packet_qp_state
);
4495 qp
->state
= raw_packet_qp_state
;
4496 qp_attr
->port_num
= 1;
4498 err
= query_qp_attr(dev
, qp
, qp_attr
);
4503 qp_attr
->qp_state
= qp
->state
;
4504 qp_attr
->cur_qp_state
= qp_attr
->qp_state
;
4505 qp_attr
->cap
.max_recv_wr
= qp
->rq
.wqe_cnt
;
4506 qp_attr
->cap
.max_recv_sge
= qp
->rq
.max_gs
;
4508 if (!ibqp
->uobject
) {
4509 qp_attr
->cap
.max_send_wr
= qp
->sq
.max_post
;
4510 qp_attr
->cap
.max_send_sge
= qp
->sq
.max_gs
;
4511 qp_init_attr
->qp_context
= ibqp
->qp_context
;
4513 qp_attr
->cap
.max_send_wr
= 0;
4514 qp_attr
->cap
.max_send_sge
= 0;
4517 qp_init_attr
->qp_type
= ibqp
->qp_type
;
4518 qp_init_attr
->recv_cq
= ibqp
->recv_cq
;
4519 qp_init_attr
->send_cq
= ibqp
->send_cq
;
4520 qp_init_attr
->srq
= ibqp
->srq
;
4521 qp_attr
->cap
.max_inline_data
= qp
->max_inline_data
;
4523 qp_init_attr
->cap
= qp_attr
->cap
;
4525 qp_init_attr
->create_flags
= 0;
4526 if (qp
->flags
& MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK
)
4527 qp_init_attr
->create_flags
|= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK
;
4529 if (qp
->flags
& MLX5_IB_QP_CROSS_CHANNEL
)
4530 qp_init_attr
->create_flags
|= IB_QP_CREATE_CROSS_CHANNEL
;
4531 if (qp
->flags
& MLX5_IB_QP_MANAGED_SEND
)
4532 qp_init_attr
->create_flags
|= IB_QP_CREATE_MANAGED_SEND
;
4533 if (qp
->flags
& MLX5_IB_QP_MANAGED_RECV
)
4534 qp_init_attr
->create_flags
|= IB_QP_CREATE_MANAGED_RECV
;
4535 if (qp
->flags
& MLX5_IB_QP_SQPN_QP1
)
4536 qp_init_attr
->create_flags
|= mlx5_ib_create_qp_sqpn_qp1();
4538 qp_init_attr
->sq_sig_type
= qp
->sq_signal_bits
& MLX5_WQE_CTRL_CQ_UPDATE
?
4539 IB_SIGNAL_ALL_WR
: IB_SIGNAL_REQ_WR
;
4542 mutex_unlock(&qp
->mutex
);
4546 struct ib_xrcd
*mlx5_ib_alloc_xrcd(struct ib_device
*ibdev
,
4547 struct ib_ucontext
*context
,
4548 struct ib_udata
*udata
)
4550 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
4551 struct mlx5_ib_xrcd
*xrcd
;
4554 if (!MLX5_CAP_GEN(dev
->mdev
, xrc
))
4555 return ERR_PTR(-ENOSYS
);
4557 xrcd
= kmalloc(sizeof(*xrcd
), GFP_KERNEL
);
4559 return ERR_PTR(-ENOMEM
);
4561 err
= mlx5_core_xrcd_alloc(dev
->mdev
, &xrcd
->xrcdn
);
4564 return ERR_PTR(-ENOMEM
);
4567 return &xrcd
->ibxrcd
;
4570 int mlx5_ib_dealloc_xrcd(struct ib_xrcd
*xrcd
)
4572 struct mlx5_ib_dev
*dev
= to_mdev(xrcd
->device
);
4573 u32 xrcdn
= to_mxrcd(xrcd
)->xrcdn
;
4576 err
= mlx5_core_xrcd_dealloc(dev
->mdev
, xrcdn
);
4578 mlx5_ib_warn(dev
, "failed to dealloc xrcdn 0x%x\n", xrcdn
);
4587 static void mlx5_ib_wq_event(struct mlx5_core_qp
*core_qp
, int type
)
4589 struct mlx5_ib_rwq
*rwq
= to_mibrwq(core_qp
);
4590 struct mlx5_ib_dev
*dev
= to_mdev(rwq
->ibwq
.device
);
4591 struct ib_event event
;
4593 if (rwq
->ibwq
.event_handler
) {
4594 event
.device
= rwq
->ibwq
.device
;
4595 event
.element
.wq
= &rwq
->ibwq
;
4597 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR
:
4598 event
.event
= IB_EVENT_WQ_FATAL
;
4601 mlx5_ib_warn(dev
, "Unexpected event type %d on WQ %06x\n", type
, core_qp
->qpn
);
4605 rwq
->ibwq
.event_handler(&event
, rwq
->ibwq
.wq_context
);
4609 static int create_rq(struct mlx5_ib_rwq
*rwq
, struct ib_pd
*pd
,
4610 struct ib_wq_init_attr
*init_attr
)
4612 struct mlx5_ib_dev
*dev
;
4620 dev
= to_mdev(pd
->device
);
4622 inlen
= MLX5_ST_SZ_BYTES(create_rq_in
) + sizeof(u64
) * rwq
->rq_num_pas
;
4623 in
= mlx5_vzalloc(inlen
);
4627 rqc
= MLX5_ADDR_OF(create_rq_in
, in
, ctx
);
4628 MLX5_SET(rqc
, rqc
, mem_rq_type
,
4629 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE
);
4630 MLX5_SET(rqc
, rqc
, user_index
, rwq
->user_index
);
4631 MLX5_SET(rqc
, rqc
, cqn
, to_mcq(init_attr
->cq
)->mcq
.cqn
);
4632 MLX5_SET(rqc
, rqc
, state
, MLX5_RQC_STATE_RST
);
4633 MLX5_SET(rqc
, rqc
, flush_in_error_en
, 1);
4634 wq
= MLX5_ADDR_OF(rqc
, rqc
, wq
);
4635 MLX5_SET(wq
, wq
, wq_type
, MLX5_WQ_TYPE_CYCLIC
);
4636 MLX5_SET(wq
, wq
, end_padding_mode
, MLX5_WQ_END_PAD_MODE_ALIGN
);
4637 MLX5_SET(wq
, wq
, log_wq_stride
, rwq
->log_rq_stride
);
4638 MLX5_SET(wq
, wq
, log_wq_sz
, rwq
->log_rq_size
);
4639 MLX5_SET(wq
, wq
, pd
, to_mpd(pd
)->pdn
);
4640 MLX5_SET(wq
, wq
, page_offset
, rwq
->rq_page_offset
);
4641 MLX5_SET(wq
, wq
, log_wq_pg_sz
, rwq
->log_page_size
);
4642 MLX5_SET(wq
, wq
, wq_signature
, rwq
->wq_sig
);
4643 MLX5_SET64(wq
, wq
, dbr_addr
, rwq
->db
.dma
);
4644 rq_pas0
= (__be64
*)MLX5_ADDR_OF(wq
, wq
, pas
);
4645 mlx5_ib_populate_pas(dev
, rwq
->umem
, rwq
->page_shift
, rq_pas0
, 0);
4646 err
= mlx5_core_create_rq_tracked(dev
->mdev
, in
, inlen
, &rwq
->core_qp
);
4651 static int set_user_rq_size(struct mlx5_ib_dev
*dev
,
4652 struct ib_wq_init_attr
*wq_init_attr
,
4653 struct mlx5_ib_create_wq
*ucmd
,
4654 struct mlx5_ib_rwq
*rwq
)
4656 /* Sanity check RQ size before proceeding */
4657 if (wq_init_attr
->max_wr
> (1 << MLX5_CAP_GEN(dev
->mdev
, log_max_wq_sz
)))
4660 if (!ucmd
->rq_wqe_count
)
4663 rwq
->wqe_count
= ucmd
->rq_wqe_count
;
4664 rwq
->wqe_shift
= ucmd
->rq_wqe_shift
;
4665 rwq
->buf_size
= (rwq
->wqe_count
<< rwq
->wqe_shift
);
4666 rwq
->log_rq_stride
= rwq
->wqe_shift
;
4667 rwq
->log_rq_size
= ilog2(rwq
->wqe_count
);
4671 static int prepare_user_rq(struct ib_pd
*pd
,
4672 struct ib_wq_init_attr
*init_attr
,
4673 struct ib_udata
*udata
,
4674 struct mlx5_ib_rwq
*rwq
)
4676 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
4677 struct mlx5_ib_create_wq ucmd
= {};
4679 size_t required_cmd_sz
;
4681 required_cmd_sz
= offsetof(typeof(ucmd
), reserved
) + sizeof(ucmd
.reserved
);
4682 if (udata
->inlen
< required_cmd_sz
) {
4683 mlx5_ib_dbg(dev
, "invalid inlen\n");
4687 if (udata
->inlen
> sizeof(ucmd
) &&
4688 !ib_is_udata_cleared(udata
, sizeof(ucmd
),
4689 udata
->inlen
- sizeof(ucmd
))) {
4690 mlx5_ib_dbg(dev
, "inlen is not supported\n");
4694 if (ib_copy_from_udata(&ucmd
, udata
, min(sizeof(ucmd
), udata
->inlen
))) {
4695 mlx5_ib_dbg(dev
, "copy failed\n");
4699 if (ucmd
.comp_mask
) {
4700 mlx5_ib_dbg(dev
, "invalid comp mask\n");
4704 if (ucmd
.reserved
) {
4705 mlx5_ib_dbg(dev
, "invalid reserved\n");
4709 err
= set_user_rq_size(dev
, init_attr
, &ucmd
, rwq
);
4711 mlx5_ib_dbg(dev
, "err %d\n", err
);
4715 err
= create_user_rq(dev
, pd
, rwq
, &ucmd
);
4717 mlx5_ib_dbg(dev
, "err %d\n", err
);
4722 rwq
->user_index
= ucmd
.user_index
;
4726 struct ib_wq
*mlx5_ib_create_wq(struct ib_pd
*pd
,
4727 struct ib_wq_init_attr
*init_attr
,
4728 struct ib_udata
*udata
)
4730 struct mlx5_ib_dev
*dev
;
4731 struct mlx5_ib_rwq
*rwq
;
4732 struct mlx5_ib_create_wq_resp resp
= {};
4733 size_t min_resp_len
;
4737 return ERR_PTR(-ENOSYS
);
4739 min_resp_len
= offsetof(typeof(resp
), reserved
) + sizeof(resp
.reserved
);
4740 if (udata
->outlen
&& udata
->outlen
< min_resp_len
)
4741 return ERR_PTR(-EINVAL
);
4743 dev
= to_mdev(pd
->device
);
4744 switch (init_attr
->wq_type
) {
4746 rwq
= kzalloc(sizeof(*rwq
), GFP_KERNEL
);
4748 return ERR_PTR(-ENOMEM
);
4749 err
= prepare_user_rq(pd
, init_attr
, udata
, rwq
);
4752 err
= create_rq(rwq
, pd
, init_attr
);
4757 mlx5_ib_dbg(dev
, "unsupported wq type %d\n",
4758 init_attr
->wq_type
);
4759 return ERR_PTR(-EINVAL
);
4762 rwq
->ibwq
.wq_num
= rwq
->core_qp
.qpn
;
4763 rwq
->ibwq
.state
= IB_WQS_RESET
;
4764 if (udata
->outlen
) {
4765 resp
.response_length
= offsetof(typeof(resp
), response_length
) +
4766 sizeof(resp
.response_length
);
4767 err
= ib_copy_to_udata(udata
, &resp
, resp
.response_length
);
4772 rwq
->core_qp
.event
= mlx5_ib_wq_event
;
4773 rwq
->ibwq
.event_handler
= init_attr
->event_handler
;
4777 mlx5_core_destroy_rq_tracked(dev
->mdev
, &rwq
->core_qp
);
4779 destroy_user_rq(pd
, rwq
);
4782 return ERR_PTR(err
);
4785 int mlx5_ib_destroy_wq(struct ib_wq
*wq
)
4787 struct mlx5_ib_dev
*dev
= to_mdev(wq
->device
);
4788 struct mlx5_ib_rwq
*rwq
= to_mrwq(wq
);
4790 mlx5_core_destroy_rq_tracked(dev
->mdev
, &rwq
->core_qp
);
4791 destroy_user_rq(wq
->pd
, rwq
);
4797 struct ib_rwq_ind_table
*mlx5_ib_create_rwq_ind_table(struct ib_device
*device
,
4798 struct ib_rwq_ind_table_init_attr
*init_attr
,
4799 struct ib_udata
*udata
)
4801 struct mlx5_ib_dev
*dev
= to_mdev(device
);
4802 struct mlx5_ib_rwq_ind_table
*rwq_ind_tbl
;
4803 int sz
= 1 << init_attr
->log_ind_tbl_size
;
4804 struct mlx5_ib_create_rwq_ind_tbl_resp resp
= {};
4805 size_t min_resp_len
;
4812 if (udata
->inlen
> 0 &&
4813 !ib_is_udata_cleared(udata
, 0,
4815 return ERR_PTR(-EOPNOTSUPP
);
4817 if (init_attr
->log_ind_tbl_size
>
4818 MLX5_CAP_GEN(dev
->mdev
, log_max_rqt_size
)) {
4819 mlx5_ib_dbg(dev
, "log_ind_tbl_size = %d is bigger than supported = %d\n",
4820 init_attr
->log_ind_tbl_size
,
4821 MLX5_CAP_GEN(dev
->mdev
, log_max_rqt_size
));
4822 return ERR_PTR(-EINVAL
);
4825 min_resp_len
= offsetof(typeof(resp
), reserved
) + sizeof(resp
.reserved
);
4826 if (udata
->outlen
&& udata
->outlen
< min_resp_len
)
4827 return ERR_PTR(-EINVAL
);
4829 rwq_ind_tbl
= kzalloc(sizeof(*rwq_ind_tbl
), GFP_KERNEL
);
4831 return ERR_PTR(-ENOMEM
);
4833 inlen
= MLX5_ST_SZ_BYTES(create_rqt_in
) + sizeof(u32
) * sz
;
4834 in
= mlx5_vzalloc(inlen
);
4840 rqtc
= MLX5_ADDR_OF(create_rqt_in
, in
, rqt_context
);
4842 MLX5_SET(rqtc
, rqtc
, rqt_actual_size
, sz
);
4843 MLX5_SET(rqtc
, rqtc
, rqt_max_size
, sz
);
4845 for (i
= 0; i
< sz
; i
++)
4846 MLX5_SET(rqtc
, rqtc
, rq_num
[i
], init_attr
->ind_tbl
[i
]->wq_num
);
4848 err
= mlx5_core_create_rqt(dev
->mdev
, in
, inlen
, &rwq_ind_tbl
->rqtn
);
4854 rwq_ind_tbl
->ib_rwq_ind_tbl
.ind_tbl_num
= rwq_ind_tbl
->rqtn
;
4855 if (udata
->outlen
) {
4856 resp
.response_length
= offsetof(typeof(resp
), response_length
) +
4857 sizeof(resp
.response_length
);
4858 err
= ib_copy_to_udata(udata
, &resp
, resp
.response_length
);
4863 return &rwq_ind_tbl
->ib_rwq_ind_tbl
;
4866 mlx5_core_destroy_rqt(dev
->mdev
, rwq_ind_tbl
->rqtn
);
4869 return ERR_PTR(err
);
4872 int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table
*ib_rwq_ind_tbl
)
4874 struct mlx5_ib_rwq_ind_table
*rwq_ind_tbl
= to_mrwq_ind_table(ib_rwq_ind_tbl
);
4875 struct mlx5_ib_dev
*dev
= to_mdev(ib_rwq_ind_tbl
->device
);
4877 mlx5_core_destroy_rqt(dev
->mdev
, rwq_ind_tbl
->rqtn
);
4883 int mlx5_ib_modify_wq(struct ib_wq
*wq
, struct ib_wq_attr
*wq_attr
,
4884 u32 wq_attr_mask
, struct ib_udata
*udata
)
4886 struct mlx5_ib_dev
*dev
= to_mdev(wq
->device
);
4887 struct mlx5_ib_rwq
*rwq
= to_mrwq(wq
);
4888 struct mlx5_ib_modify_wq ucmd
= {};
4889 size_t required_cmd_sz
;
4897 required_cmd_sz
= offsetof(typeof(ucmd
), reserved
) + sizeof(ucmd
.reserved
);
4898 if (udata
->inlen
< required_cmd_sz
)
4901 if (udata
->inlen
> sizeof(ucmd
) &&
4902 !ib_is_udata_cleared(udata
, sizeof(ucmd
),
4903 udata
->inlen
- sizeof(ucmd
)))
4906 if (ib_copy_from_udata(&ucmd
, udata
, min(sizeof(ucmd
), udata
->inlen
)))
4909 if (ucmd
.comp_mask
|| ucmd
.reserved
)
4912 inlen
= MLX5_ST_SZ_BYTES(modify_rq_in
);
4913 in
= mlx5_vzalloc(inlen
);
4917 rqc
= MLX5_ADDR_OF(modify_rq_in
, in
, ctx
);
4919 curr_wq_state
= (wq_attr_mask
& IB_WQ_CUR_STATE
) ?
4920 wq_attr
->curr_wq_state
: wq
->state
;
4921 wq_state
= (wq_attr_mask
& IB_WQ_STATE
) ?
4922 wq_attr
->wq_state
: curr_wq_state
;
4923 if (curr_wq_state
== IB_WQS_ERR
)
4924 curr_wq_state
= MLX5_RQC_STATE_ERR
;
4925 if (wq_state
== IB_WQS_ERR
)
4926 wq_state
= MLX5_RQC_STATE_ERR
;
4927 MLX5_SET(modify_rq_in
, in
, rq_state
, curr_wq_state
);
4928 MLX5_SET(rqc
, rqc
, state
, wq_state
);
4930 err
= mlx5_core_modify_rq(dev
->mdev
, rwq
->core_qp
.qpn
, in
, inlen
);
4933 rwq
->ibwq
.state
= (wq_state
== MLX5_RQC_STATE_ERR
) ? IB_WQS_ERR
: wq_state
;