blob: f0385965a694a07e596a02c878a3035ca80cc947 [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/module.h>
34#include <rdma/ib_umem.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020035#include <rdma/ib_cache.h>
Haggai Abramovskycfb5e082016-01-14 19:12:57 +020036#include <rdma/ib_user_verbs.h>
Mark Zhangd14133d2019-07-02 13:02:36 +030037#include <rdma/rdma_counter.h>
Yishai Hadasc2e53b22017-06-08 16:15:08 +030038#include <linux/mlx5/fs.h>
Eli Cohene126ba92013-07-07 17:25:49 +030039#include "mlx5_ib.h"
Mark Blochb96c9dd2018-01-29 10:40:37 +000040#include "ib_rep.h"
Yishai Hadas443c1cf2018-09-20 21:39:26 +030041#include "cmd.h"
Leon Romanovsky333fbaa2020-04-04 10:40:24 +030042#include "qp.h"
Eli Cohene126ba92013-07-07 17:25:49 +030043
Eli Cohene126ba92013-07-07 17:25:49 +030044enum {
45 MLX5_IB_ACK_REQ_FREQ = 8,
46};
47
48enum {
49 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
50 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
51 MLX5_IB_LINK_TYPE_IB = 0,
52 MLX5_IB_LINK_TYPE_ETH = 1
53};
54
55enum {
56 MLX5_IB_SQ_STRIDE = 6,
Idan Burstein064e5262018-05-02 13:16:39 +030057 MLX5_IB_SQ_UMR_INLINE_THRESHOLD = 64,
Eli Cohene126ba92013-07-07 17:25:49 +030058};
59
60static const u32 mlx5_ib_opcode[] = {
61 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020062 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030063 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
64 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
65 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
66 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
67 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
68 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
69 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
70 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030071 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030072 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
73 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
74 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
75};
76
Erez Shitritf0313962016-02-21 16:27:17 +020077struct mlx5_wqe_eth_pad {
78 u8 rsvd0[16];
79};
Eli Cohene126ba92013-07-07 17:25:49 +030080
Alex Veskereb49ab02016-08-28 12:25:53 +030081enum raw_qp_set_mask_map {
82 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020083 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030084};
85
Alex Vesker0680efa2016-08-28 12:25:52 +030086struct mlx5_modify_raw_qp_param {
87 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030088
89 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang61147f32018-03-19 15:10:30 +020090
91 struct mlx5_rate_limit rl;
92
Alex Veskereb49ab02016-08-28 12:25:53 +030093 u8 rq_q_ctr_id;
Mark Blochd5ed8ac2019-03-28 15:27:38 +020094 u16 port;
Alex Vesker0680efa2016-08-28 12:25:52 +030095};
96
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030097static void get_cqs(enum ib_qp_type qp_type,
98 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
99 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
100
Eli Cohene126ba92013-07-07 17:25:49 +0300101static int is_qp0(enum ib_qp_type qp_type)
102{
103 return qp_type == IB_QPT_SMI;
104}
105
Eli Cohene126ba92013-07-07 17:25:49 +0300106static int is_sqp(enum ib_qp_type qp_type)
107{
108 return is_qp0(qp_type) || is_qp1(qp_type);
109}
110
Haggai Eranc1395a22014-12-11 17:04:14 +0200111/**
Moni Shouafbeb4072019-01-22 08:48:46 +0200112 * mlx5_ib_read_user_wqe_common() - Copy a WQE (or part of) from user WQ
113 * to kernel buffer
Haggai Eranc1395a22014-12-11 17:04:14 +0200114 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200115 * @umem: User space memory where the WQ is
116 * @buffer: buffer to copy to
117 * @buflen: buffer length
118 * @wqe_index: index of WQE to copy from
119 * @wq_offset: offset to start of WQ
120 * @wq_wqe_cnt: number of WQEs in WQ
121 * @wq_wqe_shift: log2 of WQE size
122 * @bcnt: number of bytes to copy
123 * @bytes_copied: number of bytes to copy (return value)
Haggai Eranc1395a22014-12-11 17:04:14 +0200124 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200125 * Copies from start of WQE bcnt or less bytes.
126 * Does not gurantee to copy the entire WQE.
Haggai Eranc1395a22014-12-11 17:04:14 +0200127 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200128 * Return: zero on success, or an error code.
Haggai Eranc1395a22014-12-11 17:04:14 +0200129 */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200130static int mlx5_ib_read_user_wqe_common(struct ib_umem *umem, void *buffer,
131 size_t buflen, int wqe_index,
132 int wq_offset, int wq_wqe_cnt,
133 int wq_wqe_shift, int bcnt,
Moni Shouafbeb4072019-01-22 08:48:46 +0200134 size_t *bytes_copied)
Haggai Eranc1395a22014-12-11 17:04:14 +0200135{
Moni Shouafbeb4072019-01-22 08:48:46 +0200136 size_t offset = wq_offset + ((wqe_index % wq_wqe_cnt) << wq_wqe_shift);
137 size_t wq_end = wq_offset + (wq_wqe_cnt << wq_wqe_shift);
138 size_t copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200139 int ret;
140
Moni Shouafbeb4072019-01-22 08:48:46 +0200141 /* don't copy more than requested, more than buffer length or
142 * beyond WQ end
143 */
144 copy_length = min_t(u32, buflen, wq_end - offset);
145 copy_length = min_t(u32, copy_length, bcnt);
Haggai Eranc1395a22014-12-11 17:04:14 +0200146
Moni Shouafbeb4072019-01-22 08:48:46 +0200147 ret = ib_umem_copy_from(buffer, umem, offset, copy_length);
Haggai Eranc1395a22014-12-11 17:04:14 +0200148 if (ret)
149 return ret;
150
Moni Shouafbeb4072019-01-22 08:48:46 +0200151 if (!ret && bytes_copied)
152 *bytes_copied = copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200153
Moni Shouafbeb4072019-01-22 08:48:46 +0200154 return 0;
155}
Haggai Eranc1395a22014-12-11 17:04:14 +0200156
Moni Shouada9ee9d2020-01-15 14:43:34 +0200157static int mlx5_ib_read_kernel_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index,
158 void *buffer, size_t buflen, size_t *bc)
159{
160 struct mlx5_wqe_ctrl_seg *ctrl;
161 size_t bytes_copied = 0;
162 size_t wqe_length;
163 void *p;
164 int ds;
165
166 wqe_index = wqe_index & qp->sq.fbc.sz_m1;
167
168 /* read the control segment first */
169 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, wqe_index);
170 ctrl = p;
171 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
172 wqe_length = ds * MLX5_WQE_DS_UNITS;
173
174 /* read rest of WQE if it spreads over more than one stride */
175 while (bytes_copied < wqe_length) {
176 size_t copy_length =
177 min_t(size_t, buflen - bytes_copied, MLX5_SEND_WQE_BB);
178
179 if (!copy_length)
180 break;
181
182 memcpy(buffer + bytes_copied, p, copy_length);
183 bytes_copied += copy_length;
184
185 wqe_index = (wqe_index + 1) & qp->sq.fbc.sz_m1;
186 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, wqe_index);
187 }
188 *bc = bytes_copied;
189 return 0;
190}
191
192static int mlx5_ib_read_user_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index,
193 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200194{
195 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
196 struct ib_umem *umem = base->ubuffer.umem;
197 struct mlx5_ib_wq *wq = &qp->sq;
198 struct mlx5_wqe_ctrl_seg *ctrl;
199 size_t bytes_copied;
200 size_t bytes_copied2;
201 size_t wqe_length;
202 int ret;
203 int ds;
Haggai Eranc1395a22014-12-11 17:04:14 +0200204
Moni Shouafbeb4072019-01-22 08:48:46 +0200205 /* at first read as much as possible */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200206 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index,
207 wq->offset, wq->wqe_cnt,
208 wq->wqe_shift, buflen,
Moni Shouafbeb4072019-01-22 08:48:46 +0200209 &bytes_copied);
Haggai Eranc1395a22014-12-11 17:04:14 +0200210 if (ret)
211 return ret;
212
Moni Shouafbeb4072019-01-22 08:48:46 +0200213 /* we need at least control segment size to proceed */
214 if (bytes_copied < sizeof(*ctrl))
215 return -EINVAL;
216
217 ctrl = buffer;
218 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
219 wqe_length = ds * MLX5_WQE_DS_UNITS;
220
221 /* if we copied enough then we are done */
222 if (bytes_copied >= wqe_length) {
223 *bc = bytes_copied;
224 return 0;
225 }
226
227 /* otherwise this a wrapped around wqe
228 * so read the remaining bytes starting
229 * from wqe_index 0
230 */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200231 ret = mlx5_ib_read_user_wqe_common(umem, buffer + bytes_copied,
232 buflen - bytes_copied, 0, wq->offset,
233 wq->wqe_cnt, wq->wqe_shift,
Moni Shouafbeb4072019-01-22 08:48:46 +0200234 wqe_length - bytes_copied,
235 &bytes_copied2);
236
237 if (ret)
238 return ret;
239 *bc = bytes_copied + bytes_copied2;
240 return 0;
241}
242
Moni Shouada9ee9d2020-01-15 14:43:34 +0200243int mlx5_ib_read_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
244 size_t buflen, size_t *bc)
245{
246 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
247 struct ib_umem *umem = base->ubuffer.umem;
248
249 if (buflen < sizeof(struct mlx5_wqe_ctrl_seg))
250 return -EINVAL;
251
252 if (!umem)
253 return mlx5_ib_read_kernel_wqe_sq(qp, wqe_index, buffer,
254 buflen, bc);
255
256 return mlx5_ib_read_user_wqe_sq(qp, wqe_index, buffer, buflen, bc);
257}
258
259static int mlx5_ib_read_user_wqe_rq(struct mlx5_ib_qp *qp, int wqe_index,
260 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200261{
262 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
263 struct ib_umem *umem = base->ubuffer.umem;
264 struct mlx5_ib_wq *wq = &qp->rq;
265 size_t bytes_copied;
266 int ret;
267
Moni Shouada9ee9d2020-01-15 14:43:34 +0200268 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index,
269 wq->offset, wq->wqe_cnt,
270 wq->wqe_shift, buflen,
Moni Shouafbeb4072019-01-22 08:48:46 +0200271 &bytes_copied);
272
273 if (ret)
274 return ret;
275 *bc = bytes_copied;
276 return 0;
277}
278
Moni Shouada9ee9d2020-01-15 14:43:34 +0200279int mlx5_ib_read_wqe_rq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
280 size_t buflen, size_t *bc)
281{
282 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
283 struct ib_umem *umem = base->ubuffer.umem;
284 struct mlx5_ib_wq *wq = &qp->rq;
285 size_t wqe_size = 1 << wq->wqe_shift;
286
287 if (buflen < wqe_size)
288 return -EINVAL;
289
290 if (!umem)
291 return -EOPNOTSUPP;
292
293 return mlx5_ib_read_user_wqe_rq(qp, wqe_index, buffer, buflen, bc);
294}
295
296static int mlx5_ib_read_user_wqe_srq(struct mlx5_ib_srq *srq, int wqe_index,
297 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200298{
299 struct ib_umem *umem = srq->umem;
300 size_t bytes_copied;
301 int ret;
302
Moni Shouada9ee9d2020-01-15 14:43:34 +0200303 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index, 0,
304 srq->msrq.max, srq->msrq.wqe_shift,
305 buflen, &bytes_copied);
Moni Shouafbeb4072019-01-22 08:48:46 +0200306
307 if (ret)
308 return ret;
309 *bc = bytes_copied;
310 return 0;
Haggai Eranc1395a22014-12-11 17:04:14 +0200311}
312
Moni Shouada9ee9d2020-01-15 14:43:34 +0200313int mlx5_ib_read_wqe_srq(struct mlx5_ib_srq *srq, int wqe_index, void *buffer,
314 size_t buflen, size_t *bc)
315{
316 struct ib_umem *umem = srq->umem;
317 size_t wqe_size = 1 << srq->msrq.wqe_shift;
318
319 if (buflen < wqe_size)
320 return -EINVAL;
321
322 if (!umem)
323 return -EOPNOTSUPP;
324
325 return mlx5_ib_read_user_wqe_srq(srq, wqe_index, buffer, buflen, bc);
326}
327
Eli Cohene126ba92013-07-07 17:25:49 +0300328static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
329{
330 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
331 struct ib_event event;
332
majd@mellanox.com19098df2016-01-14 19:13:03 +0200333 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
334 /* This event is only valid for trans_qps */
335 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
336 }
Eli Cohene126ba92013-07-07 17:25:49 +0300337
338 if (ibqp->event_handler) {
339 event.device = ibqp->device;
340 event.element.qp = ibqp;
341 switch (type) {
342 case MLX5_EVENT_TYPE_PATH_MIG:
343 event.event = IB_EVENT_PATH_MIG;
344 break;
345 case MLX5_EVENT_TYPE_COMM_EST:
346 event.event = IB_EVENT_COMM_EST;
347 break;
348 case MLX5_EVENT_TYPE_SQ_DRAINED:
349 event.event = IB_EVENT_SQ_DRAINED;
350 break;
351 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
352 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
353 break;
354 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
355 event.event = IB_EVENT_QP_FATAL;
356 break;
357 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
358 event.event = IB_EVENT_PATH_MIG_ERR;
359 break;
360 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
361 event.event = IB_EVENT_QP_REQ_ERR;
362 break;
363 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
364 event.event = IB_EVENT_QP_ACCESS_ERR;
365 break;
366 default:
367 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
368 return;
369 }
370
371 ibqp->event_handler(&event, ibqp->qp_context);
372 }
373}
374
375static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
376 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
377{
378 int wqe_size;
379 int wq_size;
380
381 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300382 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300383 return -EINVAL;
384
385 if (!has_rq) {
386 qp->rq.max_gs = 0;
387 qp->rq.wqe_cnt = 0;
388 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300389 cap->max_recv_wr = 0;
390 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300391 } else {
Leon Romanovskyc95e6d52020-04-27 18:46:15 +0300392 int wq_sig = !!(qp->flags_en & MLX5_QP_FLAG_SIGNATURE);
393
Eli Cohene126ba92013-07-07 17:25:49 +0300394 if (ucmd) {
395 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300396 if (ucmd->rq_wqe_shift > BITS_PER_BYTE * sizeof(ucmd->rq_wqe_shift))
397 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300398 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovskyc95e6d52020-04-27 18:46:15 +0300399 if ((1 << qp->rq.wqe_shift) /
400 sizeof(struct mlx5_wqe_data_seg) <
401 wq_sig)
Leon Romanovsky002bf222018-04-23 17:01:53 +0300402 return -EINVAL;
Leon Romanovskyc95e6d52020-04-27 18:46:15 +0300403 qp->rq.max_gs =
404 (1 << qp->rq.wqe_shift) /
405 sizeof(struct mlx5_wqe_data_seg) -
406 wq_sig;
Eli Cohene126ba92013-07-07 17:25:49 +0300407 qp->rq.max_post = qp->rq.wqe_cnt;
408 } else {
Leon Romanovskyc95e6d52020-04-27 18:46:15 +0300409 wqe_size =
410 wq_sig ? sizeof(struct mlx5_wqe_signature_seg) :
411 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300412 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
413 wqe_size = roundup_pow_of_two(wqe_size);
414 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
415 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
416 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300417 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300418 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
419 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300420 MLX5_CAP_GEN(dev->mdev,
421 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300422 return -EINVAL;
423 }
424 qp->rq.wqe_shift = ilog2(wqe_size);
Leon Romanovskyc95e6d52020-04-27 18:46:15 +0300425 qp->rq.max_gs =
426 (1 << qp->rq.wqe_shift) /
427 sizeof(struct mlx5_wqe_data_seg) -
428 wq_sig;
Eli Cohene126ba92013-07-07 17:25:49 +0300429 qp->rq.max_post = qp->rq.wqe_cnt;
430 }
431 }
432
433 return 0;
434}
435
Erez Shitritf0313962016-02-21 16:27:17 +0200436static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300437{
Andi Shyti618af382013-07-16 15:35:01 +0200438 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300439
Erez Shitritf0313962016-02-21 16:27:17 +0200440 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300441 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300442 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300443 /* fall through */
444 case IB_QPT_RC:
445 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200446 max(sizeof(struct mlx5_wqe_atomic_seg) +
447 sizeof(struct mlx5_wqe_raddr_seg),
448 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
Idan Burstein064e5262018-05-02 13:16:39 +0300449 sizeof(struct mlx5_mkey_seg) +
450 MLX5_IB_SQ_UMR_INLINE_THRESHOLD /
451 MLX5_IB_UMR_OCTOWORD);
Eli Cohene126ba92013-07-07 17:25:49 +0300452 break;
453
Eli Cohenb125a542013-09-11 16:35:22 +0300454 case IB_QPT_XRC_TGT:
455 return 0;
456
Eli Cohene126ba92013-07-07 17:25:49 +0300457 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300458 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200459 max(sizeof(struct mlx5_wqe_raddr_seg),
460 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
461 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300462 break;
463
464 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200465 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
466 size += sizeof(struct mlx5_wqe_eth_pad) +
467 sizeof(struct mlx5_wqe_eth_seg);
468 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300469 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200470 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300471 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300472 sizeof(struct mlx5_wqe_datagram_seg);
473 break;
474
475 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300476 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300477 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
478 sizeof(struct mlx5_mkey_seg);
479 break;
480
481 default:
482 return -EINVAL;
483 }
484
485 return size;
486}
487
488static int calc_send_wqe(struct ib_qp_init_attr *attr)
489{
490 int inl_size = 0;
491 int size;
492
Erez Shitritf0313962016-02-21 16:27:17 +0200493 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300494 if (size < 0)
495 return size;
496
497 if (attr->cap.max_inline_data) {
498 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
499 attr->cap.max_inline_data;
500 }
501
502 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300503 if (attr->create_flags & IB_QP_CREATE_INTEGRITY_EN &&
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200504 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300505 return MLX5_SIG_WQE_SIZE;
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200506 else
507 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300508}
509
Eli Cohen288c01b2016-10-27 16:36:45 +0300510static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
511{
512 int max_sge;
513
514 if (attr->qp_type == IB_QPT_RC)
515 max_sge = (min_t(int, wqe_size, 512) -
516 sizeof(struct mlx5_wqe_ctrl_seg) -
517 sizeof(struct mlx5_wqe_raddr_seg)) /
518 sizeof(struct mlx5_wqe_data_seg);
519 else if (attr->qp_type == IB_QPT_XRC_INI)
520 max_sge = (min_t(int, wqe_size, 512) -
521 sizeof(struct mlx5_wqe_ctrl_seg) -
522 sizeof(struct mlx5_wqe_xrc_seg) -
523 sizeof(struct mlx5_wqe_raddr_seg)) /
524 sizeof(struct mlx5_wqe_data_seg);
525 else
526 max_sge = (wqe_size - sq_overhead(attr)) /
527 sizeof(struct mlx5_wqe_data_seg);
528
529 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
530 sizeof(struct mlx5_wqe_data_seg));
531}
532
Eli Cohene126ba92013-07-07 17:25:49 +0300533static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
534 struct mlx5_ib_qp *qp)
535{
536 int wqe_size;
537 int wq_size;
538
539 if (!attr->cap.max_send_wr)
540 return 0;
541
542 wqe_size = calc_send_wqe(attr);
543 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
544 if (wqe_size < 0)
545 return wqe_size;
546
Saeed Mahameed938fe832015-05-28 22:28:41 +0300547 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300548 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300549 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300550 return -EINVAL;
551 }
552
Erez Shitritf0313962016-02-21 16:27:17 +0200553 qp->max_inline_data = wqe_size - sq_overhead(attr) -
554 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300555 attr->cap.max_inline_data = qp->max_inline_data;
556
557 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
558 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300559 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800560 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
561 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300562 qp->sq.wqe_cnt,
563 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300564 return -ENOMEM;
565 }
Eli Cohene126ba92013-07-07 17:25:49 +0300566 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300567 qp->sq.max_gs = get_send_sge(attr, wqe_size);
568 if (qp->sq.max_gs < attr->cap.max_send_sge)
569 return -ENOMEM;
570
571 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300572 qp->sq.max_post = wq_size / wqe_size;
573 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300574
575 return wq_size;
576}
577
578static int set_user_buf_size(struct mlx5_ib_dev *dev,
579 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200580 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200581 struct mlx5_ib_qp_base *base,
582 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300583{
584 int desc_sz = 1 << qp->sq.wqe_shift;
585
Saeed Mahameed938fe832015-05-28 22:28:41 +0300586 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300587 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300588 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300589 return -EINVAL;
590 }
591
Gal Pressmanaf8b38e2019-02-06 15:45:35 +0200592 if (ucmd->sq_wqe_count && !is_power_of_2(ucmd->sq_wqe_count)) {
593 mlx5_ib_warn(dev, "sq_wqe_count %d is not a power of two\n",
594 ucmd->sq_wqe_count);
Eli Cohene126ba92013-07-07 17:25:49 +0300595 return -EINVAL;
596 }
597
598 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
599
Saeed Mahameed938fe832015-05-28 22:28:41 +0300600 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300601 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300602 qp->sq.wqe_cnt,
603 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300604 return -EINVAL;
605 }
606
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300607 if (attr->qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +0300608 qp->flags & IB_QP_CREATE_SOURCE_QPN) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200609 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
610 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
611 } else {
612 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
613 (qp->sq.wqe_cnt << 6);
614 }
Eli Cohene126ba92013-07-07 17:25:49 +0300615
616 return 0;
617}
618
619static int qp_has_rq(struct ib_qp_init_attr *attr)
620{
621 if (attr->qp_type == IB_QPT_XRC_INI ||
622 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
623 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
624 !attr->cap.max_recv_wr)
625 return 0;
626
627 return 1;
628}
629
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200630enum {
631 /* this is the first blue flame register in the array of bfregs assigned
632 * to a processes. Since we do not use it for blue flame but rather
633 * regular 64 bit doorbells, we do not need a lock for maintaiing
634 * "odd/even" order
635 */
636 NUM_NON_BLUE_FLAME_BFREGS = 1,
637};
638
Eli Cohenb037c292017-01-03 23:55:26 +0200639static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
640{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200641 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200642}
643
644static int num_med_bfreg(struct mlx5_ib_dev *dev,
645 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200646{
647 int n;
648
Eli Cohenb037c292017-01-03 23:55:26 +0200649 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
650 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200651
652 return n >= 0 ? n : 0;
653}
654
Yishai Hadas18b03622018-05-07 10:20:01 +0300655static int first_med_bfreg(struct mlx5_ib_dev *dev,
656 struct mlx5_bfreg_info *bfregi)
657{
658 return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
659}
660
Eli Cohenb037c292017-01-03 23:55:26 +0200661static int first_hi_bfreg(struct mlx5_ib_dev *dev,
662 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200663{
664 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200665
Eli Cohenb037c292017-01-03 23:55:26 +0200666 med = num_med_bfreg(dev, bfregi);
667 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200668}
669
Eli Cohenb037c292017-01-03 23:55:26 +0200670static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
671 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300672{
Eli Cohene126ba92013-07-07 17:25:49 +0300673 int i;
674
Eli Cohenb037c292017-01-03 23:55:26 +0200675 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
676 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200677 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300678 return i;
679 }
680 }
681
682 return -ENOMEM;
683}
684
Eli Cohenb037c292017-01-03 23:55:26 +0200685static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
686 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300687{
Yishai Hadas18b03622018-05-07 10:20:01 +0300688 int minidx = first_med_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300689 int i;
690
Yishai Hadas18b03622018-05-07 10:20:01 +0300691 if (minidx < 0)
692 return minidx;
693
694 for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200695 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300696 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200697 if (!bfregi->count[minidx])
698 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300699 }
700
Eli Cohen2f5ff262017-01-03 23:55:21 +0200701 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300702 return minidx;
703}
704
Eli Cohenb037c292017-01-03 23:55:26 +0200705static int alloc_bfreg(struct mlx5_ib_dev *dev,
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300706 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300707{
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300708 int bfregn = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +0300709
Yishai Hadas0a2fd012020-03-24 08:01:43 +0200710 if (bfregi->lib_uar_dyn)
711 return -EINVAL;
712
Eli Cohen2f5ff262017-01-03 23:55:21 +0200713 mutex_lock(&bfregi->lock);
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300714 if (bfregi->ver >= 2) {
715 bfregn = alloc_high_class_bfreg(dev, bfregi);
716 if (bfregn < 0)
717 bfregn = alloc_med_class_bfreg(dev, bfregi);
718 }
719
720 if (bfregn < 0) {
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200721 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200722 bfregn = 0;
723 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300724 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200725 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300726
Eli Cohen2f5ff262017-01-03 23:55:21 +0200727 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300728}
729
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200730void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300731{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200732 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200733 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200734 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300735}
736
737static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
738{
739 switch (state) {
740 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
741 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
742 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
743 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
744 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
745 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
746 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
747 default: return -1;
748 }
749}
750
751static int to_mlx5_st(enum ib_qp_type type)
752{
753 switch (type) {
754 case IB_QPT_RC: return MLX5_QP_ST_RC;
755 case IB_QPT_UC: return MLX5_QP_ST_UC;
756 case IB_QPT_UD: return MLX5_QP_ST_UD;
757 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
758 case IB_QPT_XRC_INI:
759 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
760 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200761 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200762 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300763 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300764 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200765 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300766 case IB_QPT_MAX:
767 default: return -EINVAL;
768 }
769}
770
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300771static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
772 struct mlx5_ib_cq *recv_cq);
773static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
774 struct mlx5_ib_cq *recv_cq);
775
Yishai Hadas7c043e92018-06-17 13:00:03 +0300776int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300777 struct mlx5_bfreg_info *bfregi, u32 bfregn,
Yishai Hadas7c043e92018-06-17 13:00:03 +0300778 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300779{
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300780 unsigned int bfregs_per_sys_page;
781 u32 index_of_sys_page;
782 u32 offset;
Eli Cohenb037c292017-01-03 23:55:26 +0200783
Yishai Hadas0a2fd012020-03-24 08:01:43 +0200784 if (bfregi->lib_uar_dyn)
785 return -EINVAL;
786
Eli Cohenb037c292017-01-03 23:55:26 +0200787 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
788 MLX5_NON_FP_BFREGS_PER_UAR;
789 index_of_sys_page = bfregn / bfregs_per_sys_page;
790
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200791 if (dyn_bfreg) {
792 index_of_sys_page += bfregi->num_static_sys_pages;
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300793
794 if (index_of_sys_page >= bfregi->num_sys_pages)
795 return -EINVAL;
796
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200797 if (bfregn > bfregi->num_dyn_bfregs ||
798 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
799 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
800 return -EINVAL;
801 }
802 }
Eli Cohenb037c292017-01-03 23:55:26 +0200803
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200804 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200805 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300806}
807
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200808static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200809 unsigned long addr, size_t size,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200810 struct ib_umem **umem, int *npages, int *page_shift,
811 int *ncont, u32 *offset)
majd@mellanox.com19098df2016-01-14 19:13:03 +0200812{
813 int err;
814
Moni Shouac320e522020-01-15 14:43:31 +0200815 *umem = ib_umem_get(&dev->ib_dev, addr, size, 0);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200816 if (IS_ERR(*umem)) {
817 mlx5_ib_dbg(dev, "umem_get failed\n");
818 return PTR_ERR(*umem);
819 }
820
Majd Dibbiny762f8992016-10-27 16:36:47 +0300821 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200822
823 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
824 if (err) {
825 mlx5_ib_warn(dev, "bad offset\n");
826 goto err_umem;
827 }
828
829 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
830 addr, size, *npages, *page_shift, *ncont, *offset);
831
832 return 0;
833
834err_umem:
835 ib_umem_release(*umem);
836 *umem = NULL;
837
838 return err;
839}
840
Maor Gottliebfe248c32017-05-30 10:29:14 +0300841static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300842 struct mlx5_ib_rwq *rwq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300843{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300844 struct mlx5_ib_ucontext *context =
845 rdma_udata_to_drv_context(
846 udata,
847 struct mlx5_ib_ucontext,
848 ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300849
Maor Gottliebfe248c32017-05-30 10:29:14 +0300850 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
851 atomic_dec(&dev->delay_drop.rqs_cnt);
852
Yishai Hadas79b20a62016-05-23 15:20:50 +0300853 mlx5_ib_db_unmap_user(context, &rwq->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300854 ib_umem_release(rwq->umem);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300855}
856
857static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200858 struct ib_udata *udata, struct mlx5_ib_rwq *rwq,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300859 struct mlx5_ib_create_wq *ucmd)
860{
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200861 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
862 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300863 int page_shift = 0;
864 int npages;
865 u32 offset = 0;
866 int ncont = 0;
867 int err;
868
869 if (!ucmd->buf_addr)
870 return -EINVAL;
871
Moni Shouac320e522020-01-15 14:43:31 +0200872 rwq->umem = ib_umem_get(&dev->ib_dev, ucmd->buf_addr, rwq->buf_size, 0);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300873 if (IS_ERR(rwq->umem)) {
874 mlx5_ib_dbg(dev, "umem_get failed\n");
875 err = PTR_ERR(rwq->umem);
876 return err;
877 }
878
Majd Dibbiny762f8992016-10-27 16:36:47 +0300879 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300880 &ncont, NULL);
881 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
882 &rwq->rq_page_offset);
883 if (err) {
884 mlx5_ib_warn(dev, "bad offset\n");
885 goto err_umem;
886 }
887
888 rwq->rq_num_pas = ncont;
889 rwq->page_shift = page_shift;
890 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
891 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
892
893 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
894 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
895 npages, page_shift, ncont, offset);
896
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200897 err = mlx5_ib_db_map_user(ucontext, udata, ucmd->db_addr, &rwq->db);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300898 if (err) {
899 mlx5_ib_dbg(dev, "map failed\n");
900 goto err_umem;
901 }
902
903 rwq->create_type = MLX5_WQ_USER;
904 return 0;
905
906err_umem:
907 ib_umem_release(rwq->umem);
908 return err;
909}
910
Eli Cohenb037c292017-01-03 23:55:26 +0200911static int adjust_bfregn(struct mlx5_ib_dev *dev,
912 struct mlx5_bfreg_info *bfregi, int bfregn)
913{
914 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
915 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
916}
917
Eli Cohene126ba92013-07-07 17:25:49 +0300918static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
919 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200920 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300921 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200922 struct mlx5_ib_create_qp_resp *resp, int *inlen,
923 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300924{
925 struct mlx5_ib_ucontext *context;
926 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200927 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200928 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200929 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300930 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200931 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200932 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200933 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300934 __be64 *pas;
935 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300936 int err;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200937 u16 uid;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200938 u32 uar_flags;
Eli Cohene126ba92013-07-07 17:25:49 +0300939
940 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
941 if (err) {
942 mlx5_ib_dbg(dev, "copy failed\n");
943 return err;
944 }
945
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200946 context = rdma_udata_to_drv_context(udata, struct mlx5_ib_ucontext,
947 ibucontext);
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200948 uar_flags = ucmd.flags & (MLX5_QP_FLAG_UAR_PAGE_INDEX |
949 MLX5_QP_FLAG_BFREG_INDEX);
950 switch (uar_flags) {
951 case MLX5_QP_FLAG_UAR_PAGE_INDEX:
952 uar_index = ucmd.bfreg_index;
953 bfregn = MLX5_IB_INVALID_BFREG;
954 break;
955 case MLX5_QP_FLAG_BFREG_INDEX:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200956 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
957 ucmd.bfreg_index, true);
958 if (uar_index < 0)
959 return uar_index;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200960 bfregn = MLX5_IB_INVALID_BFREG;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200961 break;
962 case 0:
Leon Romanovsky2be08c32020-04-27 18:46:13 +0300963 if (qp->flags & IB_QP_CREATE_CROSS_CHANNEL)
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200964 return -EINVAL;
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300965 bfregn = alloc_bfreg(dev, &context->bfregi);
966 if (bfregn < 0)
967 return bfregn;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200968 break;
969 default:
970 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300971 }
972
Eli Cohen2f5ff262017-01-03 23:55:21 +0200973 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200974 if (bfregn != MLX5_IB_INVALID_BFREG)
975 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
976 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300977
Haggai Eran48fea832014-05-22 14:50:11 +0300978 qp->rq.offset = 0;
979 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
980 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
981
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200982 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300983 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200984 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300985
majd@mellanox.com19098df2016-01-14 19:13:03 +0200986 if (ucmd.buf_addr && ubuffer->buf_size) {
987 ubuffer->buf_addr = ucmd.buf_addr;
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200988 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr,
989 ubuffer->buf_size, &ubuffer->umem,
990 &npages, &page_shift, &ncont, &offset);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200991 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200992 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200993 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200994 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300995 }
Eli Cohene126ba92013-07-07 17:25:49 +0300996
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300997 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
998 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300999 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001000 if (!*in) {
1001 err = -ENOMEM;
1002 goto err_umem;
1003 }
Eli Cohene126ba92013-07-07 17:25:49 +03001004
Yishai Hadas7422edc2018-12-23 13:12:21 +02001005 uid = (attr->qp_type != IB_QPT_XRC_TGT &&
1006 attr->qp_type != IB_QPT_XRC_INI) ? to_mpd(pd)->uid : 0;
Yishai Hadas5aa37712018-11-26 08:28:38 +02001007 MLX5_SET(create_qp_in, *in, uid, uid);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001008 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
1009 if (ubuffer->umem)
1010 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
1011
1012 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1013
1014 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1015 MLX5_SET(qpc, qpc, page_offset, offset);
1016
1017 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001018 if (bfregn != MLX5_IB_INVALID_BFREG)
1019 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
1020 else
1021 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001022 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +03001023
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001024 err = mlx5_ib_db_map_user(context, udata, ucmd.db_addr, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001025 if (err) {
1026 mlx5_ib_dbg(dev, "map failed\n");
1027 goto err_free;
1028 }
1029
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001030 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
Eli Cohene126ba92013-07-07 17:25:49 +03001031 if (err) {
1032 mlx5_ib_dbg(dev, "copy failed\n");
1033 goto err_unmap;
1034 }
1035 qp->create_type = MLX5_QP_USER;
1036
1037 return 0;
1038
1039err_unmap:
1040 mlx5_ib_db_unmap_user(context, &qp->db);
1041
1042err_free:
Al Viro479163f2014-11-20 08:13:57 +00001043 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001044
1045err_umem:
Leon Romanovsky836a0fb2019-06-16 15:05:20 +03001046 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +03001047
Eli Cohen2f5ff262017-01-03 23:55:21 +02001048err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001049 if (bfregn != MLX5_IB_INVALID_BFREG)
1050 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001051 return err;
1052}
1053
Eli Cohenb037c292017-01-03 23:55:26 +02001054static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03001055 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base,
1056 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03001057{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03001058 struct mlx5_ib_ucontext *context =
1059 rdma_udata_to_drv_context(
1060 udata,
1061 struct mlx5_ib_ucontext,
1062 ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03001063
Eli Cohene126ba92013-07-07 17:25:49 +03001064 mlx5_ib_db_unmap_user(context, &qp->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +03001065 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001066
1067 /*
1068 * Free only the BFREGs which are handled by the kernel.
1069 * BFREGs of UARs allocated dynamically are handled by user.
1070 */
1071 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
1072 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001073}
1074
Guy Levi34f4c952018-11-26 08:15:50 +02001075/* get_sq_edge - Get the next nearby edge.
1076 *
1077 * An 'edge' is defined as the first following address after the end
1078 * of the fragment or the SQ. Accordingly, during the WQE construction
1079 * which repetitively increases the pointer to write the next data, it
1080 * simply should check if it gets to an edge.
1081 *
1082 * @sq - SQ buffer.
1083 * @idx - Stride index in the SQ buffer.
1084 *
1085 * Return:
1086 * The new edge.
1087 */
1088static void *get_sq_edge(struct mlx5_ib_wq *sq, u32 idx)
1089{
1090 void *fragment_end;
1091
1092 fragment_end = mlx5_frag_buf_get_wqe
1093 (&sq->fbc,
1094 mlx5_frag_buf_get_idx_last_contig_stride(&sq->fbc, idx));
1095
1096 return fragment_end + MLX5_SEND_WQE_BB;
1097}
1098
Eli Cohene126ba92013-07-07 17:25:49 +03001099static int create_kernel_qp(struct mlx5_ib_dev *dev,
1100 struct ib_qp_init_attr *init_attr,
1101 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001102 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +02001103 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +03001104{
Eli Cohene126ba92013-07-07 17:25:49 +03001105 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001106 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +03001107 int err;
1108
Eli Cohene126ba92013-07-07 17:25:49 +03001109 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001110 qp->bf.bfreg = &dev->fp_bfreg;
Leon Romanovsky29789752020-04-27 18:46:14 +03001111 else if (qp->flags & MLX5_IB_QP_CREATE_WC_TEST)
Michael Guralnik11f552e2019-06-10 15:21:24 +03001112 qp->bf.bfreg = &dev->wc_bfreg;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001113 else
1114 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +03001115
Eli Cohend8030b02017-02-09 19:31:47 +02001116 /* We need to divide by two since each register is comprised of
1117 * two buffers of identical size, namely odd and even
1118 */
1119 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001120 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +03001121
1122 err = calc_sq_size(dev, init_attr, qp);
1123 if (err < 0) {
1124 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001125 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001126 }
1127
1128 qp->rq.offset = 0;
1129 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001130 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +03001131
Guy Levi34f4c952018-11-26 08:15:50 +02001132 err = mlx5_frag_buf_alloc_node(dev->mdev, base->ubuffer.buf_size,
1133 &qp->buf, dev->mdev->priv.numa_node);
Eli Cohene126ba92013-07-07 17:25:49 +03001134 if (err) {
1135 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001136 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001137 }
1138
Guy Levi34f4c952018-11-26 08:15:50 +02001139 if (qp->rq.wqe_cnt)
1140 mlx5_init_fbc(qp->buf.frags, qp->rq.wqe_shift,
1141 ilog2(qp->rq.wqe_cnt), &qp->rq.fbc);
1142
1143 if (qp->sq.wqe_cnt) {
1144 int sq_strides_offset = (qp->sq.offset & (PAGE_SIZE - 1)) /
1145 MLX5_SEND_WQE_BB;
1146 mlx5_init_fbc_offset(qp->buf.frags +
1147 (qp->sq.offset / PAGE_SIZE),
1148 ilog2(MLX5_SEND_WQE_BB),
1149 ilog2(qp->sq.wqe_cnt),
1150 sq_strides_offset, &qp->sq.fbc);
1151
1152 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
1153 }
1154
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001155 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
1156 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001157 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001158 if (!*in) {
1159 err = -ENOMEM;
1160 goto err_buf;
1161 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001162
1163 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1164 MLX5_SET(qpc, qpc, uar_page, uar_index);
1165 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1166
Eli Cohene126ba92013-07-07 17:25:49 +03001167 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001168 MLX5_SET(qpc, qpc, fre, 1);
1169 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001170
Leon Romanovsky29789752020-04-27 18:46:14 +03001171 if (qp->flags & MLX5_IB_QP_CREATE_SQPN_QP1)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001172 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +02001173
Guy Levi34f4c952018-11-26 08:15:50 +02001174 mlx5_fill_page_frag_array(&qp->buf,
1175 (__be64 *)MLX5_ADDR_OF(create_qp_in,
1176 *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +03001177
Jack Morgenstein9603b612014-07-28 23:30:22 +03001178 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001179 if (err) {
1180 mlx5_ib_dbg(dev, "err %d\n", err);
1181 goto err_free;
1182 }
1183
Li Dongyangb5883002017-08-16 23:31:22 +10001184 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
1185 sizeof(*qp->sq.wrid), GFP_KERNEL);
1186 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1187 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1188 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1189 sizeof(*qp->rq.wrid), GFP_KERNEL);
1190 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1191 sizeof(*qp->sq.w_list), GFP_KERNEL);
1192 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1193 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001194
1195 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1196 !qp->sq.w_list || !qp->sq.wqe_head) {
1197 err = -ENOMEM;
1198 goto err_wrid;
1199 }
1200 qp->create_type = MLX5_QP_KERNEL;
1201
1202 return 0;
1203
1204err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001205 kvfree(qp->sq.wqe_head);
1206 kvfree(qp->sq.w_list);
1207 kvfree(qp->sq.wrid);
1208 kvfree(qp->sq.wr_data);
1209 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001210 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001211
1212err_free:
Al Viro479163f2014-11-20 08:13:57 +00001213 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001214
1215err_buf:
Guy Levi34f4c952018-11-26 08:15:50 +02001216 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001217 return err;
1218}
1219
1220static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1221{
Li Dongyangb5883002017-08-16 23:31:22 +10001222 kvfree(qp->sq.wqe_head);
1223 kvfree(qp->sq.w_list);
1224 kvfree(qp->sq.wrid);
1225 kvfree(qp->sq.wr_data);
1226 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001227 mlx5_db_free(dev->mdev, &qp->db);
Guy Levi34f4c952018-11-26 08:15:50 +02001228 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001229}
1230
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001231static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001232{
1233 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Leon Romanovsky8bde2c52020-04-27 18:46:09 +03001234 (qp->qp_sub_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001235 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001236 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001237 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001238 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001239 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001240 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001241}
1242
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001243static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001244 struct mlx5_ib_qp *qp,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001245 struct mlx5_ib_sq *sq, u32 tdn,
1246 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001247{
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001248 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001249 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1250
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001251 MLX5_SET(create_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001252 MLX5_SET(tisc, tisc, transport_domain, tdn);
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001253 if (qp->flags & IB_QP_CREATE_SOURCE_QPN)
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001254 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1255
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001256 return mlx5_core_create_tis(dev->mdev, in, &sq->tisn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001257}
1258
1259static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001260 struct mlx5_ib_sq *sq, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001261{
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001262 mlx5_cmd_destroy_tis(dev->mdev, sq->tisn, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001263}
1264
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001265static void destroy_flow_rule_vport_sq(struct mlx5_ib_sq *sq)
Mark Blochb96c9dd2018-01-29 10:40:37 +00001266{
1267 if (sq->flow_rule)
1268 mlx5_del_flow_rules(sq->flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001269 sq->flow_rule = NULL;
Mark Blochb96c9dd2018-01-29 10:40:37 +00001270}
1271
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001272static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001273 struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001274 struct mlx5_ib_sq *sq, void *qpin,
1275 struct ib_pd *pd)
1276{
1277 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1278 __be64 *pas;
1279 void *in;
1280 void *sqc;
1281 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1282 void *wq;
1283 int inlen;
1284 int err;
1285 int page_shift = 0;
1286 int npages;
1287 int ncont = 0;
1288 u32 offset = 0;
1289
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001290 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr, ubuffer->buf_size,
1291 &sq->ubuffer.umem, &npages, &page_shift, &ncont,
1292 &offset);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001293 if (err)
1294 return err;
1295
1296 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001297 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001298 if (!in) {
1299 err = -ENOMEM;
1300 goto err_umem;
1301 }
1302
Yishai Hadasc14003f2018-09-20 21:39:22 +03001303 MLX5_SET(create_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001304 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1305 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001306 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1307 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001308 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1309 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1310 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1311 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1312 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001313 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1314 MLX5_CAP_ETH(dev->mdev, swp))
1315 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001316
1317 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1318 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1319 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1320 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1321 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1322 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1323 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1324 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1325 MLX5_SET(wq, wq, page_offset, offset);
1326
1327 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1328 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1329
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001330 err = mlx5_core_create_sq_tracked(dev, in, inlen, &sq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001331
1332 kvfree(in);
1333
1334 if (err)
1335 goto err_umem;
1336
1337 return 0;
1338
1339err_umem:
1340 ib_umem_release(sq->ubuffer.umem);
1341 sq->ubuffer.umem = NULL;
1342
1343 return err;
1344}
1345
1346static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1347 struct mlx5_ib_sq *sq)
1348{
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001349 destroy_flow_rule_vport_sq(sq);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001350 mlx5_core_destroy_sq_tracked(dev, &sq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001351 ib_umem_release(sq->ubuffer.umem);
1352}
1353
Boris Pismenny2c292db2018-03-08 15:51:40 +02001354static size_t get_rq_pas_size(void *qpc)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001355{
1356 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1357 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1358 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1359 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1360 u32 po_quanta = 1 << (log_page_size - 6);
1361 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1362 u32 page_size = 1 << log_page_size;
1363 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1364 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1365
1366 return rq_num_pas * sizeof(u64);
1367}
1368
1369static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001370 struct mlx5_ib_rq *rq, void *qpin,
Yishai Hadas34d57582018-09-20 21:39:21 +03001371 size_t qpinlen, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001372{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001373 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001374 __be64 *pas;
1375 __be64 *qp_pas;
1376 void *in;
1377 void *rqc;
1378 void *wq;
1379 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001380 size_t rq_pas_size = get_rq_pas_size(qpc);
1381 size_t inlen;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001382 int err;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001383
1384 if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
1385 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001386
1387 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001388 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001389 if (!in)
1390 return -ENOMEM;
1391
Yishai Hadas34d57582018-09-20 21:39:21 +03001392 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001393 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001394 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1395 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001396 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1397 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1398 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1399 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1400 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1401
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001402 if (mqp->flags & IB_QP_CREATE_SCATTER_FCS)
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001403 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1404
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001405 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1406 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001407 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1408 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001409 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1410 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1411 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1412 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1413 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1414 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1415
1416 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1417 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1418 memcpy(pas, qp_pas, rq_pas_size);
1419
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001420 err = mlx5_core_create_rq_tracked(dev, in, inlen, &rq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001421
1422 kvfree(in);
1423
1424 return err;
1425}
1426
1427static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1428 struct mlx5_ib_rq *rq)
1429{
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001430 mlx5_core_destroy_rq_tracked(dev, &rq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001431}
1432
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001433static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1434{
1435 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1436 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1437 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1438}
1439
Mark Bloch0042f9e2018-09-17 13:30:49 +03001440static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1441 struct mlx5_ib_rq *rq,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001442 u32 qp_flags_en,
1443 struct ib_pd *pd)
Mark Bloch0042f9e2018-09-17 13:30:49 +03001444{
1445 if (qp_flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1446 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1447 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001448 mlx5_cmd_destroy_tir(dev->mdev, rq->tirn, to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001449}
1450
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001451static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001452 struct mlx5_ib_rq *rq, u32 tdn,
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001453 u32 *qp_flags_en, struct ib_pd *pd,
1454 u32 *out)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001455{
Mark Bloch175edba2018-09-17 13:30:48 +03001456 u8 lb_flag = 0;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001457 u32 *in;
1458 void *tirc;
1459 int inlen;
1460 int err;
1461
1462 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001463 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001464 if (!in)
1465 return -ENOMEM;
1466
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001467 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001468 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1469 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1470 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1471 MLX5_SET(tirc, tirc, transport_domain, tdn);
Mark Bloch175edba2018-09-17 13:30:48 +03001472 if (*qp_flags_en & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001473 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001474
Mark Bloch175edba2018-09-17 13:30:48 +03001475 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
1476 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1477
1478 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
1479 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1480
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001481 if (dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001482 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1483 *qp_flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1484 }
1485
1486 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001487 MLX5_SET(create_tir_in, in, opcode, MLX5_CMD_OP_CREATE_TIR);
1488 err = mlx5_cmd_exec_inout(dev->mdev, create_tir, in, out);
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001489 rq->tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001490 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1491 err = mlx5_ib_enable_lb(dev, false, true);
1492
1493 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001494 destroy_raw_packet_qp_tir(dev, rq, 0, pd);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001495 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001496 kvfree(in);
1497
1498 return err;
1499}
1500
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001501static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001502 u32 *in, size_t inlen,
Yishai Hadas7f720522018-09-20 21:45:18 +03001503 struct ib_pd *pd,
1504 struct ib_udata *udata,
1505 struct mlx5_ib_create_qp_resp *resp)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001506{
1507 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1508 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1509 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001510 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1511 udata, struct mlx5_ib_ucontext, ibucontext);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001512 int err;
1513 u32 tdn = mucontext->tdn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001514 u16 uid = to_mpd(pd)->uid;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001515 u32 out[MLX5_ST_SZ_DW(create_tir_out)] = {};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001516
1517 if (qp->sq.wqe_cnt) {
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001518 err = create_raw_packet_qp_tis(dev, qp, sq, tdn, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001519 if (err)
1520 return err;
1521
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001522 err = create_raw_packet_qp_sq(dev, udata, sq, in, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001523 if (err)
1524 goto err_destroy_tis;
1525
Yishai Hadas7f720522018-09-20 21:45:18 +03001526 if (uid) {
1527 resp->tisn = sq->tisn;
1528 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TISN;
1529 resp->sqn = sq->base.mqp.qpn;
1530 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_SQN;
1531 }
1532
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001533 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001534 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001535 }
1536
1537 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001538 rq->base.container_mibqp = qp;
1539
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001540 if (qp->flags & IB_QP_CREATE_CVLAN_STRIPPING)
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001541 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001542 if (qp->flags & IB_QP_CREATE_PCI_WRITE_END_PADDING)
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001543 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
Yishai Hadas34d57582018-09-20 21:39:21 +03001544 err = create_raw_packet_qp_rq(dev, rq, in, inlen, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001545 if (err)
1546 goto err_destroy_sq;
1547
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001548 err = create_raw_packet_qp_tir(dev, rq, tdn, &qp->flags_en, pd,
1549 out);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001550 if (err)
1551 goto err_destroy_rq;
Yishai Hadas7f720522018-09-20 21:45:18 +03001552
1553 if (uid) {
1554 resp->rqn = rq->base.mqp.qpn;
1555 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_RQN;
1556 resp->tirn = rq->tirn;
1557 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001558 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1559 resp->tir_icm_addr = MLX5_GET(
1560 create_tir_out, out, icm_address_31_0);
1561 resp->tir_icm_addr |=
1562 (u64)MLX5_GET(create_tir_out, out,
1563 icm_address_39_32)
1564 << 32;
1565 resp->tir_icm_addr |=
1566 (u64)MLX5_GET(create_tir_out, out,
1567 icm_address_63_40)
1568 << 40;
1569 resp->comp_mask |=
1570 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1571 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001572 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001573 }
1574
1575 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1576 rq->base.mqp.qpn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001577 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
1578 if (err)
1579 goto err_destroy_tir;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001580
1581 return 0;
1582
Yishai Hadas7f720522018-09-20 21:45:18 +03001583err_destroy_tir:
1584 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001585err_destroy_rq:
1586 destroy_raw_packet_qp_rq(dev, rq);
1587err_destroy_sq:
1588 if (!qp->sq.wqe_cnt)
1589 return err;
1590 destroy_raw_packet_qp_sq(dev, sq);
1591err_destroy_tis:
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001592 destroy_raw_packet_qp_tis(dev, sq, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001593
1594 return err;
1595}
1596
1597static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1598 struct mlx5_ib_qp *qp)
1599{
1600 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1601 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1602 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1603
1604 if (qp->rq.wqe_cnt) {
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001605 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001606 destroy_raw_packet_qp_rq(dev, rq);
1607 }
1608
1609 if (qp->sq.wqe_cnt) {
1610 destroy_raw_packet_qp_sq(dev, sq);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001611 destroy_raw_packet_qp_tis(dev, sq, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001612 }
1613}
1614
1615static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1616 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1617{
1618 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1619 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1620
1621 sq->sq = &qp->sq;
1622 rq->rq = &qp->rq;
1623 sq->doorbell = &qp->db;
1624 rq->doorbell = &qp->db;
1625}
1626
Yishai Hadas28d61372016-05-23 15:20:56 +03001627static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1628{
Mark Bloch0042f9e2018-09-17 13:30:49 +03001629 if (qp->flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1630 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1631 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001632 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1633 to_mpd(qp->ibqp.pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001634}
1635
Leon Romanovsky5d0dc3d2020-04-27 18:46:12 +03001636static int create_rss_raw_qp_tir(struct ib_pd *pd, struct mlx5_ib_qp *qp,
Yishai Hadas28d61372016-05-23 15:20:56 +03001637 struct ib_qp_init_attr *init_attr,
1638 struct ib_udata *udata)
1639{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001640 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1641 udata, struct mlx5_ib_ucontext, ibucontext);
Leon Romanovsky5d0dc3d2020-04-27 18:46:12 +03001642 struct mlx5_ib_dev *dev = to_mdev(pd->device);
Yishai Hadas28d61372016-05-23 15:20:56 +03001643 struct mlx5_ib_create_qp_resp resp = {};
1644 int inlen;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001645 int outlen;
Yishai Hadas28d61372016-05-23 15:20:56 +03001646 int err;
1647 u32 *in;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001648 u32 *out;
Yishai Hadas28d61372016-05-23 15:20:56 +03001649 void *tirc;
1650 void *hfso;
1651 u32 selected_fields = 0;
Matan Barak2d93fc82018-03-28 09:27:55 +03001652 u32 outer_l4;
Yishai Hadas28d61372016-05-23 15:20:56 +03001653 size_t min_resp_len;
1654 u32 tdn = mucontext->tdn;
1655 struct mlx5_ib_create_qp_rss ucmd = {};
1656 size_t required_cmd_sz;
Mark Bloch175edba2018-09-17 13:30:48 +03001657 u8 lb_flag = 0;
Yishai Hadas28d61372016-05-23 15:20:56 +03001658
Leon Romanovsky29789752020-04-27 18:46:14 +03001659 if (init_attr->send_cq)
Yishai Hadas28d61372016-05-23 15:20:56 +03001660 return -EINVAL;
1661
Eli Cohen2f5ff262017-01-03 23:55:21 +02001662 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001663 if (udata->outlen < min_resp_len)
1664 return -EINVAL;
1665
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001666 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001667 if (udata->inlen < required_cmd_sz) {
1668 mlx5_ib_dbg(dev, "invalid inlen\n");
1669 return -EINVAL;
1670 }
1671
1672 if (udata->inlen > sizeof(ucmd) &&
1673 !ib_is_udata_cleared(udata, sizeof(ucmd),
1674 udata->inlen - sizeof(ucmd))) {
1675 mlx5_ib_dbg(dev, "inlen is not supported\n");
1676 return -EOPNOTSUPP;
1677 }
1678
1679 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1680 mlx5_ib_dbg(dev, "copy failed\n");
1681 return -EFAULT;
1682 }
1683
1684 if (ucmd.comp_mask) {
1685 mlx5_ib_dbg(dev, "invalid comp mask\n");
1686 return -EOPNOTSUPP;
1687 }
1688
Mark Bloch175edba2018-09-17 13:30:48 +03001689 if (ucmd.flags & ~(MLX5_QP_FLAG_TUNNEL_OFFLOADS |
1690 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1691 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)) {
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001692 mlx5_ib_dbg(dev, "invalid flags\n");
1693 return -EOPNOTSUPP;
1694 }
1695
1696 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1697 !tunnel_offload_supported(dev->mdev)) {
1698 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001699 return -EOPNOTSUPP;
1700 }
1701
Maor Gottlieb309fa342017-10-19 08:25:56 +03001702 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1703 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1704 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1705 return -EOPNOTSUPP;
1706 }
1707
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001708 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC || dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001709 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1710 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1711 }
1712
1713 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
1714 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1715 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
1716 }
1717
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001718 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
Yishai Hadas28d61372016-05-23 15:20:56 +03001719 if (err) {
1720 mlx5_ib_dbg(dev, "copy failed\n");
1721 return -EINVAL;
1722 }
1723
1724 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001725 outlen = MLX5_ST_SZ_BYTES(create_tir_out);
1726 in = kvzalloc(inlen + outlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001727 if (!in)
1728 return -ENOMEM;
1729
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001730 out = in + MLX5_ST_SZ_DW(create_tir_in);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001731 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001732 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1733 MLX5_SET(tirc, tirc, disp_type,
1734 MLX5_TIRC_DISP_TYPE_INDIRECT);
1735 MLX5_SET(tirc, tirc, indirect_table,
1736 init_attr->rwq_ind_tbl->ind_tbl_num);
1737 MLX5_SET(tirc, tirc, transport_domain, tdn);
1738
1739 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001740
1741 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1742 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1743
Mark Bloch175edba2018-09-17 13:30:48 +03001744 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
1745
Maor Gottlieb309fa342017-10-19 08:25:56 +03001746 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1747 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1748 else
1749 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1750
Yishai Hadas28d61372016-05-23 15:20:56 +03001751 switch (ucmd.rx_hash_function) {
1752 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1753 {
1754 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1755 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1756
1757 if (len != ucmd.rx_key_len) {
1758 err = -EINVAL;
1759 goto err;
1760 }
1761
1762 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
Yishai Hadas28d61372016-05-23 15:20:56 +03001763 memcpy(rss_key, ucmd.rx_hash_key, len);
1764 break;
1765 }
1766 default:
1767 err = -EOPNOTSUPP;
1768 goto err;
1769 }
1770
1771 if (!ucmd.rx_hash_fields_mask) {
1772 /* special case when this TIR serves as steering entry without hashing */
1773 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1774 goto create_tir;
1775 err = -EINVAL;
1776 goto err;
1777 }
1778
1779 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1780 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1781 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1782 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1783 err = -EINVAL;
1784 goto err;
1785 }
1786
1787 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1788 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1789 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1790 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1791 MLX5_L3_PROT_TYPE_IPV4);
1792 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1793 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1794 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1795 MLX5_L3_PROT_TYPE_IPV6);
1796
Matan Barak2d93fc82018-03-28 09:27:55 +03001797 outer_l4 = ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1798 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) << 0 |
1799 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1800 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP)) << 1 |
1801 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI) << 2;
1802
1803 /* Check that only one l4 protocol is set */
1804 if (outer_l4 & (outer_l4 - 1)) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001805 err = -EINVAL;
1806 goto err;
1807 }
1808
1809 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1810 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1811 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1812 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1813 MLX5_L4_PROT_TYPE_TCP);
1814 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1815 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1816 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1817 MLX5_L4_PROT_TYPE_UDP);
1818
1819 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1820 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1821 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1822
1823 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1824 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1825 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1826
1827 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1828 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1829 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1830
1831 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1832 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1833 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1834
Matan Barak2d93fc82018-03-28 09:27:55 +03001835 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI)
1836 selected_fields |= MLX5_HASH_FIELD_SEL_IPSEC_SPI;
1837
Yishai Hadas28d61372016-05-23 15:20:56 +03001838 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1839
1840create_tir:
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001841 MLX5_SET(create_tir_in, in, opcode, MLX5_CMD_OP_CREATE_TIR);
1842 err = mlx5_cmd_exec_inout(dev->mdev, create_tir, in, out);
Yishai Hadas28d61372016-05-23 15:20:56 +03001843
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001844 qp->rss_qp.tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001845 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1846 err = mlx5_ib_enable_lb(dev, false, true);
1847
1848 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001849 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1850 to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001851 }
1852
Yishai Hadas28d61372016-05-23 15:20:56 +03001853 if (err)
1854 goto err;
1855
Yishai Hadas7f720522018-09-20 21:45:18 +03001856 if (mucontext->devx_uid) {
1857 resp.comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1858 resp.tirn = qp->rss_qp.tirn;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001859 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1860 resp.tir_icm_addr =
1861 MLX5_GET(create_tir_out, out, icm_address_31_0);
1862 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1863 icm_address_39_32)
1864 << 32;
1865 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1866 icm_address_63_40)
1867 << 40;
1868 resp.comp_mask |=
1869 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1870 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001871 }
1872
1873 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
1874 if (err)
1875 goto err_copy;
1876
Yishai Hadas28d61372016-05-23 15:20:56 +03001877 kvfree(in);
1878 /* qpn is reserved for that QP */
1879 qp->trans_qp.base.mqp.qpn = 0;
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001880 qp->is_rss = true;
Yishai Hadas28d61372016-05-23 15:20:56 +03001881 return 0;
1882
Yishai Hadas7f720522018-09-20 21:45:18 +03001883err_copy:
1884 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn, mucontext->devx_uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001885err:
1886 kvfree(in);
1887 return err;
1888}
1889
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001890static void configure_requester_scat_cqe(struct mlx5_ib_dev *dev,
1891 struct ib_qp_init_attr *init_attr,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001892 struct mlx5_ib_create_qp *ucmd,
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001893 void *qpc)
1894{
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001895 int scqe_sz;
zhengbin2ab367a2019-12-24 16:40:12 +08001896 bool allow_scat_cqe = false;
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001897
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001898 if (ucmd)
1899 allow_scat_cqe = ucmd->flags & MLX5_QP_FLAG_ALLOW_SCATTER_CQE;
1900
1901 if (!allow_scat_cqe && init_attr->sq_sig_type != IB_SIGNAL_ALL_WR)
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001902 return;
1903
1904 scqe_sz = mlx5_ib_get_cqe_size(init_attr->send_cq);
1905 if (scqe_sz == 128) {
1906 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
1907 return;
1908 }
1909
1910 if (init_attr->qp_type != MLX5_IB_QPT_DCI ||
1911 MLX5_CAP_GEN(dev->mdev, dc_req_scat_data_cqe))
1912 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
1913}
1914
Yonatan Cohena60109d2018-10-10 09:25:16 +03001915static int atomic_size_to_mode(int size_mask)
1916{
1917 /* driver does not support atomic_size > 256B
1918 * and does not know how to translate bigger sizes
1919 */
1920 int supported_size_mask = size_mask & 0x1ff;
1921 int log_max_size;
1922
1923 if (!supported_size_mask)
1924 return -EOPNOTSUPP;
1925
1926 log_max_size = __fls(supported_size_mask);
1927
1928 if (log_max_size > 3)
1929 return log_max_size;
1930
1931 return MLX5_ATOMIC_MODE_8B;
1932}
1933
1934static int get_atomic_mode(struct mlx5_ib_dev *dev,
1935 enum ib_qp_type qp_type)
1936{
1937 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
1938 u8 atomic = MLX5_CAP_GEN(dev->mdev, atomic);
1939 int atomic_mode = -EOPNOTSUPP;
1940 int atomic_size_mask;
1941
1942 if (!atomic)
1943 return -EOPNOTSUPP;
1944
1945 if (qp_type == MLX5_IB_QPT_DCT)
1946 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
1947 else
1948 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
1949
1950 if ((atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_CMP_SWAP) ||
1951 (atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_FETCH_ADD))
1952 atomic_mode = atomic_size_to_mode(atomic_size_mask);
1953
1954 if (atomic_mode <= 0 &&
1955 (atomic_operations & MLX5_ATOMIC_OPS_CMP_SWAP &&
1956 atomic_operations & MLX5_ATOMIC_OPS_FETCH_ADD))
1957 atomic_mode = MLX5_ATOMIC_MODE_IB_COMP;
1958
1959 return atomic_mode;
1960}
1961
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03001962static inline bool check_flags_mask(uint64_t input, uint64_t supported)
1963{
1964 return (input & ~supported) == 0;
1965}
1966
Eli Cohene126ba92013-07-07 17:25:49 +03001967static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1968 struct ib_qp_init_attr *init_attr,
Leon Romanovsky2dfac922020-04-27 18:46:11 +03001969 struct mlx5_ib_create_qp *ucmd,
Eli Cohene126ba92013-07-07 17:25:49 +03001970 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1971{
1972 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001973 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001974 struct mlx5_core_dev *mdev = dev->mdev;
Jason Gunthorpe0625b4b2018-08-14 15:33:52 -06001975 struct mlx5_ib_create_qp_resp resp = {};
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001976 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
1977 udata, struct mlx5_ib_ucontext, ibucontext);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001978 struct mlx5_ib_cq *send_cq;
1979 struct mlx5_ib_cq *recv_cq;
1980 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001981 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001982 struct mlx5_ib_qp_base *base;
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001983 int mlx5_st;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001984 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001985 u32 *in;
1986 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001987
1988 mutex_init(&qp->mutex);
1989 spin_lock_init(&qp->sq.lock);
1990 spin_lock_init(&qp->rq.lock);
1991
Leon Romanovsky8bde2c52020-04-27 18:46:09 +03001992 mlx5_st = to_mlx5_st((init_attr->qp_type != IB_QPT_DRIVER) ?
1993 init_attr->qp_type :
1994 qp->qp_sub_type);
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001995 if (mlx5_st < 0)
1996 return -EINVAL;
1997
Eli Cohene126ba92013-07-07 17:25:49 +03001998 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1999 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
2000
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002001 if (udata) {
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002002 if (!check_flags_mask(ucmd->flags,
Mark Bloch8af526e2019-01-15 16:45:32 +02002003 MLX5_QP_FLAG_ALLOW_SCATTER_CQE |
2004 MLX5_QP_FLAG_BFREG_INDEX |
2005 MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE |
2006 MLX5_QP_FLAG_SCATTER_CQE |
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002007 MLX5_QP_FLAG_SIGNATURE |
Mark Bloch8af526e2019-01-15 16:45:32 +02002008 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC |
2009 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
2010 MLX5_QP_FLAG_TUNNEL_OFFLOADS |
Yishai Hadasac42a5e2020-03-24 08:01:41 +02002011 MLX5_QP_FLAG_UAR_PAGE_INDEX |
Mark Bloch8af526e2019-01-15 16:45:32 +02002012 MLX5_QP_FLAG_TYPE_DCI |
2013 MLX5_QP_FLAG_TYPE_DCT))
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002014 return -EINVAL;
2015
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002016 err = get_qp_user_index(ucontext, ucmd, udata->inlen, &uidx);
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002017 if (err)
2018 return err;
2019
Leon Romanovskyc95e6d52020-04-27 18:46:15 +03002020 if (ucmd->flags & MLX5_QP_FLAG_SIGNATURE)
2021 qp->flags_en |= MLX5_QP_FLAG_SIGNATURE;
Leon Romanovsky90ecb372020-04-27 18:46:16 +03002022 if (ucmd->flags & MLX5_QP_FLAG_SCATTER_CQE &&
2023 MLX5_CAP_GEN(dev->mdev, sctr_data_cqe))
2024 qp->flags_en |= MLX5_QP_FLAG_SCATTER_CQE;
2025
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002026 if (ucmd->flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002027 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
2028 !tunnel_offload_supported(mdev)) {
2029 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
2030 return -EOPNOTSUPP;
2031 }
Mark Bloch175edba2018-09-17 13:30:48 +03002032 qp->flags_en |= MLX5_QP_FLAG_TUNNEL_OFFLOADS;
2033 }
2034
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002035 if (ucmd->flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC) {
Mark Bloch175edba2018-09-17 13:30:48 +03002036 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2037 mlx5_ib_dbg(dev, "Self-LB UC isn't supported\n");
2038 return -EOPNOTSUPP;
2039 }
2040 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
2041 }
2042
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002043 if (ucmd->flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
Mark Bloch175edba2018-09-17 13:30:48 +03002044 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2045 mlx5_ib_dbg(dev, "Self-LB UM isn't supported\n");
2046 return -EOPNOTSUPP;
2047 }
2048 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002049 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002050
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002051 if (ucmd->flags & MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE) {
Danit Goldberg569c6652018-11-30 13:22:05 +02002052 if (init_attr->qp_type != IB_QPT_RC ||
2053 !MLX5_CAP_GEN(dev->mdev, qp_packet_based)) {
2054 mlx5_ib_dbg(dev, "packet based credit mode isn't supported\n");
2055 return -EOPNOTSUPP;
2056 }
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002057 qp->flags_en |= MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE;
Danit Goldberg569c6652018-11-30 13:22:05 +02002058 }
Eli Cohene126ba92013-07-07 17:25:49 +03002059 }
2060
Leon Romanovsky29789752020-04-27 18:46:14 +03002061 if (qp->flags & IB_QP_CREATE_SOURCE_QPN)
2062 qp->underlay_qpn = init_attr->source_qpn;
2063
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002064 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002065 qp->flags & IB_QP_CREATE_SOURCE_QPN) ?
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002066 &qp->raw_packet_qp.rq.base :
2067 &qp->trans_qp.base;
2068
Eli Cohene126ba92013-07-07 17:25:49 +03002069 qp->has_rq = qp_has_rq(init_attr);
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002070 err = set_rq_size(dev, &init_attr->cap, qp->has_rq, qp, ucmd);
Eli Cohene126ba92013-07-07 17:25:49 +03002071 if (err) {
2072 mlx5_ib_dbg(dev, "err %d\n", err);
2073 return err;
2074 }
2075
2076 if (pd) {
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002077 if (udata) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002078 __u32 max_wqes =
2079 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002080 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n",
2081 ucmd->sq_wqe_count);
2082 if (ucmd->rq_wqe_shift != qp->rq.wqe_shift ||
2083 ucmd->rq_wqe_count != qp->rq.wqe_cnt) {
Eli Cohene126ba92013-07-07 17:25:49 +03002084 mlx5_ib_dbg(dev, "invalid rq params\n");
2085 return -EINVAL;
2086 }
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002087 if (ucmd->sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03002088 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002089 ucmd->sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03002090 return -EINVAL;
2091 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002092 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
2093 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002094 if (err)
2095 mlx5_ib_dbg(dev, "err %d\n", err);
2096 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002097 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
2098 base);
Eli Cohene126ba92013-07-07 17:25:49 +03002099 if (err)
2100 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03002101 }
2102
2103 if (err)
2104 return err;
2105 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002106 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03002107 if (!in)
2108 return -ENOMEM;
2109
2110 qp->create_type = MLX5_QP_EMPTY;
2111 }
2112
2113 if (is_sqp(init_attr->qp_type))
2114 qp->port = init_attr->port_num;
2115
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002116 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
2117
Noa Osheroviche7b169f2018-02-25 13:39:51 +02002118 MLX5_SET(qpc, qpc, st, mlx5_st);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002119 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03002120
2121 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002122 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002123 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002124 MLX5_SET(qpc, qpc, latency_sensitive, 1);
2125
Eli Cohene126ba92013-07-07 17:25:49 +03002126
Leon Romanovskyc95e6d52020-04-27 18:46:15 +03002127 if (qp->flags_en & MLX5_QP_FLAG_SIGNATURE)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002128 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002129
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002130 if (qp->flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002131 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03002132
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002133 if (qp->flags & IB_QP_CREATE_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002134 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002135 if (qp->flags & IB_QP_CREATE_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002136 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002137 if (qp->flags & IB_QP_CREATE_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002138 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002139 if (qp->flags_en & MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE)
Danit Goldberg569c6652018-11-30 13:22:05 +02002140 MLX5_SET(qpc, qpc, req_e2e_credit_mode, 1);
Leon Romanovsky90ecb372020-04-27 18:46:16 +03002141 if ((qp->flags_en & MLX5_QP_FLAG_SCATTER_CQE) &&
2142 (init_attr->qp_type == IB_QPT_RC ||
2143 init_attr->qp_type == IB_QPT_UC)) {
Leon Romanovsky8bde2c52020-04-27 18:46:09 +03002144 int rcqe_sz = rcqe_sz =
2145 mlx5_ib_get_cqe_size(init_attr->recv_cq);
2146
2147 MLX5_SET(qpc, qpc, cs_res,
2148 rcqe_sz == 128 ? MLX5_RES_SCAT_DATA64_CQE :
2149 MLX5_RES_SCAT_DATA32_CQE);
2150 }
Leon Romanovsky90ecb372020-04-27 18:46:16 +03002151 if ((qp->flags_en & MLX5_QP_FLAG_SCATTER_CQE) &&
2152 (qp->qp_sub_type == MLX5_IB_QPT_DCI ||
2153 init_attr->qp_type == IB_QPT_RC))
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002154 configure_requester_scat_cqe(dev, init_attr, ucmd, qpc);
Eli Cohene126ba92013-07-07 17:25:49 +03002155
2156 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002157 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
2158 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03002159 }
2160
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002161 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03002162
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002163 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002164 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002165 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002166 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002167 if (init_attr->srq &&
2168 init_attr->srq->srq_type == IB_SRQT_TM)
2169 MLX5_SET(qpc, qpc, offload_type,
2170 MLX5_QPC_OFFLOAD_TYPE_RNDV);
2171 }
Eli Cohene126ba92013-07-07 17:25:49 +03002172
2173 /* Set default resources */
2174 switch (init_attr->qp_type) {
2175 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002176 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2177 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
2178 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
2179 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002180 break;
2181 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002182 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2183 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2184 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002185 break;
2186 default:
2187 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002188 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
2189 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002190 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002191 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2192 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002193 }
2194 }
2195
2196 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002197 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002198
2199 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002200 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002201
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002202 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03002203
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002204 /* 0xffffff means we ask to work with cqe version 0 */
2205 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002206 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002207
Erez Shitritf0313962016-02-21 16:27:17 +02002208 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
Leon Romanovsky29789752020-04-27 18:46:14 +03002209 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO)
Erez Shitritf0313962016-02-21 16:27:17 +02002210 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002211
Leon Romanovsky29789752020-04-27 18:46:14 +03002212 if (qp->flags & IB_QP_CREATE_PCI_WRITE_END_PADDING &&
2213 init_attr->qp_type != IB_QPT_RAW_PACKET) {
2214 MLX5_SET(qpc, qpc, end_padding_mode,
2215 MLX5_WQ_END_PAD_MODE_ALIGN);
2216 /* Special case to clean flag */
2217 qp->flags &= ~IB_QP_CREATE_PCI_WRITE_END_PADDING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002218 }
2219
Boris Pismenny2c292db2018-03-08 15:51:40 +02002220 if (inlen < 0) {
2221 err = -EINVAL;
2222 goto err;
2223 }
2224
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002225 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002226 qp->flags & IB_QP_CREATE_SOURCE_QPN) {
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002227 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd->sq_buf_addr;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002228 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
Yishai Hadas7f720522018-09-20 21:45:18 +03002229 err = create_raw_packet_qp(dev, qp, in, inlen, pd, udata,
2230 &resp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002231 } else {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03002232 err = mlx5_core_create_qp(dev, &base->mqp, in, inlen);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002233 }
2234
Eli Cohene126ba92013-07-07 17:25:49 +03002235 if (err) {
2236 mlx5_ib_dbg(dev, "create qp failed\n");
2237 goto err_create;
2238 }
2239
Al Viro479163f2014-11-20 08:13:57 +00002240 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002241
majd@mellanox.com19098df2016-01-14 19:13:03 +02002242 base->container_mibqp = qp;
2243 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03002244
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002245 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
2246 &send_cq, &recv_cq);
2247 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2248 mlx5_ib_lock_cqs(send_cq, recv_cq);
2249 /* Maintain device to QPs access, needed for further handling via reset
2250 * flow
2251 */
2252 list_add_tail(&qp->qps_list, &dev->qp_list);
2253 /* Maintain CQ to QPs access, needed for further handling via reset flow
2254 */
2255 if (send_cq)
2256 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
2257 if (recv_cq)
2258 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
2259 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2260 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
2261
Eli Cohene126ba92013-07-07 17:25:49 +03002262 return 0;
2263
2264err_create:
2265 if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002266 destroy_qp_user(dev, pd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002267 else if (qp->create_type == MLX5_QP_KERNEL)
2268 destroy_qp_kernel(dev, qp);
2269
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002270err:
Al Viro479163f2014-11-20 08:13:57 +00002271 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002272 return err;
2273}
2274
2275static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2276 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
2277{
2278 if (send_cq) {
2279 if (recv_cq) {
2280 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002281 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002282 spin_lock_nested(&recv_cq->lock,
2283 SINGLE_DEPTH_NESTING);
2284 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002285 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002286 __acquire(&recv_cq->lock);
2287 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002288 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002289 spin_lock_nested(&send_cq->lock,
2290 SINGLE_DEPTH_NESTING);
2291 }
2292 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002293 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002294 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002295 }
2296 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002297 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002298 __acquire(&send_cq->lock);
2299 } else {
2300 __acquire(&send_cq->lock);
2301 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002302 }
2303}
2304
2305static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2306 __releases(&send_cq->lock) __releases(&recv_cq->lock)
2307{
2308 if (send_cq) {
2309 if (recv_cq) {
2310 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
2311 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002312 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002313 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
2314 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002315 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002316 } else {
2317 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002318 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002319 }
2320 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002321 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002322 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002323 }
2324 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002325 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002326 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002327 } else {
2328 __release(&recv_cq->lock);
2329 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002330 }
2331}
2332
2333static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
2334{
2335 return to_mpd(qp->ibqp.pd);
2336}
2337
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002338static void get_cqs(enum ib_qp_type qp_type,
2339 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03002340 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
2341{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002342 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002343 case IB_QPT_XRC_TGT:
2344 *send_cq = NULL;
2345 *recv_cq = NULL;
2346 break;
2347 case MLX5_IB_QPT_REG_UMR:
2348 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002349 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002350 *recv_cq = NULL;
2351 break;
2352
2353 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002354 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002355 case IB_QPT_RC:
2356 case IB_QPT_UC:
2357 case IB_QPT_UD:
2358 case IB_QPT_RAW_IPV6:
2359 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002360 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002361 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2362 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002363 break;
2364
Eli Cohene126ba92013-07-07 17:25:49 +03002365 case IB_QPT_MAX:
2366 default:
2367 *send_cq = NULL;
2368 *recv_cq = NULL;
2369 break;
2370 }
2371}
2372
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002373static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002374 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2375 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002376
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002377static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2378 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002379{
2380 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002381 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002382 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002383 int err;
2384
Yishai Hadas28d61372016-05-23 15:20:56 +03002385 if (qp->ibqp.rwq_ind_tbl) {
2386 destroy_rss_raw_qp_tir(dev, qp);
2387 return;
2388 }
2389
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002390 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002391 qp->flags & IB_QP_CREATE_SOURCE_QPN) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002392 &qp->raw_packet_qp.rq.base :
2393 &qp->trans_qp.base;
2394
Haggai Eran6aec21f2014-12-11 17:04:23 +02002395 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002396 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002397 !(qp->flags & IB_QP_CREATE_SOURCE_QPN)) {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03002398 err = mlx5_core_qp_modify(dev, MLX5_CMD_OP_2RST_QP, 0,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002399 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002400 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002401 struct mlx5_modify_raw_qp_param raw_qp_param = {
2402 .operation = MLX5_CMD_OP_2RST_QP
2403 };
2404
Aviv Heller13eab212016-09-18 20:48:04 +03002405 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002406 }
2407 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002408 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002409 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002410 }
Eli Cohene126ba92013-07-07 17:25:49 +03002411
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002412 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2413 &send_cq, &recv_cq);
2414
2415 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2416 mlx5_ib_lock_cqs(send_cq, recv_cq);
2417 /* del from lists under both locks above to protect reset flow paths */
2418 list_del(&qp->qps_list);
2419 if (send_cq)
2420 list_del(&qp->cq_send_list);
2421
2422 if (recv_cq)
2423 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002424
2425 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002426 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002427 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2428 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002429 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2430 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002431 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002432 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2433 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002434
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002435 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002436 qp->flags & IB_QP_CREATE_SOURCE_QPN) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002437 destroy_raw_packet_qp(dev, qp);
2438 } else {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03002439 err = mlx5_core_destroy_qp(dev, &base->mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002440 if (err)
2441 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2442 base->mqp.qpn);
2443 }
Eli Cohene126ba92013-07-07 17:25:49 +03002444
Eli Cohene126ba92013-07-07 17:25:49 +03002445 if (qp->create_type == MLX5_QP_KERNEL)
2446 destroy_qp_kernel(dev, qp);
2447 else if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002448 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002449}
2450
Leon Romanovsky47c80612020-04-27 18:46:07 +03002451static int create_dct(struct ib_pd *pd, struct mlx5_ib_qp *qp,
2452 struct ib_qp_init_attr *attr,
2453 struct mlx5_ib_create_qp *ucmd, struct ib_udata *udata)
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002454{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002455 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2456 udata, struct mlx5_ib_ucontext, ibucontext);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002457 int err = 0;
2458 u32 uidx = MLX5_IB_DEFAULT_UIDX;
2459 void *dctc;
2460
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002461 err = get_qp_user_index(ucontext, ucmd, sizeof(*ucmd), &uidx);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002462 if (err)
Leon Romanovsky47c80612020-04-27 18:46:07 +03002463 return err;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002464
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002465 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002466 if (!qp->dct.in)
Leon Romanovsky47c80612020-04-27 18:46:07 +03002467 return -ENOMEM;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002468
Yishai Hadasa01a5862018-09-20 21:39:24 +03002469 MLX5_SET(create_dct_in, qp->dct.in, uid, to_mpd(pd)->uid);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002470 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002471 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2472 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2473 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2474 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2475 MLX5_SET(dctc, dctc, user_index, uidx);
2476
Leon Romanovskyfd9dab72020-04-27 18:46:08 +03002477 if (ucmd->flags & MLX5_QP_FLAG_SCATTER_CQE) {
2478 int rcqe_sz = mlx5_ib_get_cqe_size(attr->recv_cq);
2479
2480 if (rcqe_sz == 128)
2481 MLX5_SET(dctc, dctc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
2482 }
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002483
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002484 qp->state = IB_QPS_RESET;
2485
Leon Romanovsky47c80612020-04-27 18:46:07 +03002486 return 0;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002487}
2488
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002489static int check_qp_type(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr)
2490{
2491 if (attr->qp_type == IB_QPT_DRIVER && !MLX5_CAP_GEN(dev->mdev, dct))
2492 goto out;
2493
2494 switch (attr->qp_type) {
2495 case IB_QPT_XRC_TGT:
2496 case IB_QPT_XRC_INI:
2497 if (!MLX5_CAP_GEN(dev->mdev, xrc))
2498 goto out;
2499 fallthrough;
2500 case IB_QPT_RAW_PACKET:
2501 case IB_QPT_RC:
2502 case IB_QPT_UC:
2503 case IB_QPT_UD:
2504 case IB_QPT_SMI:
2505 case MLX5_IB_QPT_HW_GSI:
2506 case MLX5_IB_QPT_REG_UMR:
2507 case IB_QPT_DRIVER:
2508 case IB_QPT_GSI:
2509 return 0;
2510 case IB_QPT_RAW_IPV6:
2511 case IB_QPT_RAW_ETHERTYPE:
2512 case IB_QPT_MAX:
2513 default:
2514 goto out;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002515 }
2516
2517 return 0;
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002518
2519out:
2520 mlx5_ib_dbg(dev, "Unsupported QP type %d\n", attr->qp_type);
2521 return -EOPNOTSUPP;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002522}
2523
Leon Romanovsky2242cc22020-04-27 18:46:03 +03002524static int check_valid_flow(struct mlx5_ib_dev *dev, struct ib_pd *pd,
2525 struct ib_qp_init_attr *attr,
2526 struct ib_udata *udata)
2527{
2528 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2529 udata, struct mlx5_ib_ucontext, ibucontext);
2530
2531 if (!udata) {
2532 /* Kernel create_qp callers */
2533 if (attr->rwq_ind_tbl)
2534 return -EOPNOTSUPP;
2535
2536 switch (attr->qp_type) {
2537 case IB_QPT_RAW_PACKET:
2538 case IB_QPT_DRIVER:
2539 return -EOPNOTSUPP;
2540 default:
2541 return 0;
2542 }
2543 }
2544
2545 /* Userspace create_qp callers */
2546 if (attr->qp_type == IB_QPT_RAW_PACKET && !ucontext->cqe_version) {
2547 mlx5_ib_dbg(dev,
2548 "Raw Packet QP is only supported for CQE version > 0\n");
2549 return -EINVAL;
2550 }
2551
2552 if (attr->qp_type != IB_QPT_RAW_PACKET && attr->rwq_ind_tbl) {
2553 mlx5_ib_dbg(dev,
2554 "Wrong QP type %d for the RWQ indirect table\n",
2555 attr->qp_type);
2556 return -EINVAL;
2557 }
2558
2559 switch (attr->qp_type) {
2560 case IB_QPT_SMI:
2561 case MLX5_IB_QPT_HW_GSI:
2562 case MLX5_IB_QPT_REG_UMR:
2563 case IB_QPT_GSI:
2564 mlx5_ib_dbg(dev, "Kernel doesn't support QP type %d\n",
2565 attr->qp_type);
2566 return -EINVAL;
2567 default:
2568 break;
2569 }
2570
2571 /*
2572 * We don't need to see this warning, it means that kernel code
2573 * missing ib_pd. Placed here to catch developer's mistakes.
2574 */
2575 WARN_ONCE(!pd && attr->qp_type != IB_QPT_XRC_TGT,
2576 "There is a missing PD pointer assignment\n");
2577 return 0;
2578}
2579
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002580static int process_vendor_flags(struct mlx5_ib_qp *qp,
2581 struct ib_qp_init_attr *attr,
2582 struct mlx5_ib_create_qp *ucmd)
2583{
2584 switch (ucmd->flags & (MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI)) {
2585 case MLX5_QP_FLAG_TYPE_DCI:
2586 qp->qp_sub_type = MLX5_IB_QPT_DCI;
2587 break;
2588 case MLX5_QP_FLAG_TYPE_DCT:
2589 qp->qp_sub_type = MLX5_IB_QPT_DCT;
2590 break;
2591 default:
2592 return -EINVAL;
2593 }
2594
2595 return 0;
2596}
2597
Leon Romanovsky29789752020-04-27 18:46:14 +03002598static void process_create_flag(struct mlx5_ib_dev *dev, int *flags, int flag,
2599 bool cond, struct mlx5_ib_qp *qp)
2600{
2601 if (!(*flags & flag))
2602 return;
2603
2604 if (cond) {
2605 qp->flags |= flag;
2606 *flags &= ~flag;
2607 return;
2608 }
2609
2610 if (flag == MLX5_IB_QP_CREATE_WC_TEST) {
2611 /*
2612 * Special case, if condition didn't meet, it won't be error,
2613 * just different in-kernel flow.
2614 */
2615 *flags &= ~MLX5_IB_QP_CREATE_WC_TEST;
2616 return;
2617 }
2618 mlx5_ib_dbg(dev, "Verbs create QP flag 0x%X is not supported\n", flag);
2619}
2620
2621static int process_create_flags(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2622 struct ib_qp_init_attr *attr)
2623{
2624 enum ib_qp_type qp_type = attr->qp_type;
2625 struct mlx5_core_dev *mdev = dev->mdev;
2626 int create_flags = attr->create_flags;
2627 bool cond;
2628
2629 if (qp->qp_sub_type == MLX5_IB_QPT_DCT)
2630 return (create_flags) ? -EINVAL : 0;
2631
2632 if (qp_type == IB_QPT_RAW_PACKET && attr->rwq_ind_tbl)
2633 return (create_flags) ? -EINVAL : 0;
2634
2635 process_create_flag(dev, &create_flags,
2636 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK,
2637 MLX5_CAP_GEN(mdev, block_lb_mc), qp);
2638 process_create_flag(dev, &create_flags, IB_QP_CREATE_CROSS_CHANNEL,
2639 MLX5_CAP_GEN(mdev, cd), qp);
2640 process_create_flag(dev, &create_flags, IB_QP_CREATE_MANAGED_SEND,
2641 MLX5_CAP_GEN(mdev, cd), qp);
2642 process_create_flag(dev, &create_flags, IB_QP_CREATE_MANAGED_RECV,
2643 MLX5_CAP_GEN(mdev, cd), qp);
2644
2645 if (qp_type == IB_QPT_UD) {
2646 process_create_flag(dev, &create_flags,
2647 IB_QP_CREATE_IPOIB_UD_LSO,
2648 MLX5_CAP_GEN(mdev, ipoib_basic_offloads),
2649 qp);
2650 cond = MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_IB;
2651 process_create_flag(dev, &create_flags, IB_QP_CREATE_SOURCE_QPN,
2652 cond, qp);
2653 }
2654
2655 if (qp_type == IB_QPT_RAW_PACKET) {
2656 cond = MLX5_CAP_GEN(mdev, eth_net_offloads) &&
2657 MLX5_CAP_ETH(mdev, scatter_fcs);
2658 process_create_flag(dev, &create_flags,
2659 IB_QP_CREATE_SCATTER_FCS, cond, qp);
2660
2661 cond = MLX5_CAP_GEN(mdev, eth_net_offloads) &&
2662 MLX5_CAP_ETH(mdev, vlan_cap);
2663 process_create_flag(dev, &create_flags,
2664 IB_QP_CREATE_CVLAN_STRIPPING, cond, qp);
2665 }
2666
2667 process_create_flag(dev, &create_flags,
2668 IB_QP_CREATE_PCI_WRITE_END_PADDING,
2669 MLX5_CAP_GEN(mdev, end_pad), qp);
2670
2671 process_create_flag(dev, &create_flags, MLX5_IB_QP_CREATE_WC_TEST,
2672 qp_type != MLX5_IB_QPT_REG_UMR, qp);
2673 process_create_flag(dev, &create_flags, MLX5_IB_QP_CREATE_SQPN_QP1,
2674 true, qp);
2675
2676 if (create_flags)
2677 mlx5_ib_dbg(dev, "Create QP has unsupported flags 0x%X\n",
2678 create_flags);
2679
2680 return (create_flags) ? -EINVAL : 0;
2681}
2682
Leon Romanovsky47c80612020-04-27 18:46:07 +03002683static int create_driver_qp(struct ib_pd *pd, struct mlx5_ib_qp *qp,
2684 struct ib_qp_init_attr *attr,
2685 struct mlx5_ib_create_qp *ucmd,
2686 struct ib_udata *udata)
2687{
2688 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2689 int ret = -EINVAL;
2690
2691 switch (qp->qp_sub_type) {
2692 case MLX5_IB_QPT_DCT:
2693 if (!attr->srq || !attr->recv_cq)
2694 goto out;
2695
2696 ret = create_dct(pd, qp, attr, ucmd, udata);
2697 break;
2698 case MLX5_IB_QPT_DCI:
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002699 if (attr->cap.max_recv_wr || attr->cap.max_recv_sge)
2700 goto out;
2701
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002702 ret = create_qp_common(mdev, pd, attr, ucmd, udata, qp);
Leon Romanovsky47c80612020-04-27 18:46:07 +03002703 break;
2704 default:
2705 return -EINVAL;
2706 }
2707
2708out: return ret;
2709}
2710
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002711static size_t process_udata_size(struct ib_qp_init_attr *attr,
2712 struct ib_udata *udata)
2713{
2714 size_t ucmd = sizeof(struct mlx5_ib_create_qp);
2715
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002716 if (attr->qp_type == IB_QPT_DRIVER)
2717 return (udata->inlen < ucmd) ? 0 : ucmd;
2718
2719 return ucmd;
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002720}
2721
Leon Romanovsky5d0dc3d2020-04-27 18:46:12 +03002722static int create_raw_qp(struct ib_pd *pd, struct mlx5_ib_qp *qp,
2723 struct ib_qp_init_attr *attr,
2724 struct mlx5_ib_create_qp *ucmd, struct ib_udata *udata)
2725{
2726 struct mlx5_ib_dev *dev = to_mdev(pd->device);
2727
2728 if (attr->rwq_ind_tbl)
2729 return create_rss_raw_qp_tir(pd, qp, attr, udata);
2730
2731 return create_qp_common(dev, pd, attr, ucmd, udata, qp);
2732}
2733
Eli Cohene126ba92013-07-07 17:25:49 +03002734struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002735 struct ib_qp_init_attr *init_attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002736 struct ib_udata *udata)
2737{
Leon Romanovsky47c80612020-04-27 18:46:07 +03002738 struct mlx5_ib_create_qp ucmd = {};
Eli Cohene126ba92013-07-07 17:25:49 +03002739 struct mlx5_ib_dev *dev;
2740 struct mlx5_ib_qp *qp;
2741 u16 xrcdn = 0;
2742 int err;
2743
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002744 dev = pd ? to_mdev(pd->device) :
2745 to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002746
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002747 err = check_qp_type(dev, init_attr);
2748 if (err) {
2749 mlx5_ib_dbg(dev, "Unsupported QP type %d\n",
2750 init_attr->qp_type);
2751 return ERR_PTR(err);
2752 }
2753
Leon Romanovsky2242cc22020-04-27 18:46:03 +03002754 err = check_valid_flow(dev, pd, init_attr, udata);
2755 if (err)
2756 return ERR_PTR(err);
Eli Cohene126ba92013-07-07 17:25:49 +03002757
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002758 if (init_attr->qp_type == IB_QPT_GSI)
2759 return mlx5_ib_gsi_create_qp(pd, init_attr);
2760
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002761 if (udata && !init_attr->rwq_ind_tbl) {
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002762 size_t inlen =
2763 process_udata_size(init_attr, udata);
2764
2765 if (!inlen)
2766 return ERR_PTR(-EINVAL);
2767
2768 err = ib_copy_from_udata(&ucmd, udata, inlen);
2769 if (err)
2770 return ERR_PTR(err);
2771 }
2772
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002773 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2774 if (!qp)
2775 return ERR_PTR(-ENOMEM);
2776
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002777 if (init_attr->qp_type == IB_QPT_DRIVER) {
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002778 err = process_vendor_flags(qp, init_attr, &ucmd);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002779 if (err)
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002780 goto free_qp;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002781 }
Leon Romanovsky29789752020-04-27 18:46:14 +03002782 err = process_create_flags(dev, qp, init_attr);
2783 if (err)
2784 goto free_qp;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002785
Leon Romanovskyc86936e2020-04-27 18:46:05 +03002786 if (init_attr->qp_type == IB_QPT_XRC_TGT)
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002787 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002788
Leon Romanovsky47c80612020-04-27 18:46:07 +03002789 switch (init_attr->qp_type) {
2790 case IB_QPT_DRIVER:
2791 err = create_driver_qp(pd, qp, init_attr, &ucmd, udata);
2792 break;
Leon Romanovsky5d0dc3d2020-04-27 18:46:12 +03002793 case IB_QPT_RAW_PACKET:
2794 err = create_raw_qp(pd, qp, init_attr, &ucmd, udata);
2795 break;
Leon Romanovsky47c80612020-04-27 18:46:07 +03002796 default:
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002797 err = create_qp_common(dev, pd, init_attr,
2798 (udata) ? &ucmd : NULL, udata, qp);
Leon Romanovsky47c80612020-04-27 18:46:07 +03002799 }
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002800 if (err) {
2801 mlx5_ib_dbg(dev, "create_qp_common failed\n");
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002802 goto free_qp;
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002803 }
2804
2805 if (is_qp0(init_attr->qp_type))
2806 qp->ibqp.qp_num = 0;
2807 else if (is_qp1(init_attr->qp_type))
2808 qp->ibqp.qp_num = 1;
2809 else
2810 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
2811
2812 qp->trans_qp.xrcdn = xrcdn;
2813
Eli Cohene126ba92013-07-07 17:25:49 +03002814 return &qp->ibqp;
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002815
2816free_qp:
2817 kfree(qp);
2818 return ERR_PTR(err);
Eli Cohene126ba92013-07-07 17:25:49 +03002819}
2820
Moni Shoua776a3902018-01-02 16:19:33 +02002821static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2822{
2823 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2824
2825 if (mqp->state == IB_QPS_RTR) {
2826 int err;
2827
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03002828 err = mlx5_core_destroy_dct(dev, &mqp->dct.mdct);
Moni Shoua776a3902018-01-02 16:19:33 +02002829 if (err) {
2830 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2831 return err;
2832 }
2833 }
2834
2835 kfree(mqp->dct.in);
2836 kfree(mqp);
2837 return 0;
2838}
2839
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002840int mlx5_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002841{
2842 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2843 struct mlx5_ib_qp *mqp = to_mqp(qp);
2844
Haggai Erand16e91d2016-02-29 15:45:05 +02002845 if (unlikely(qp->qp_type == IB_QPT_GSI))
2846 return mlx5_ib_gsi_destroy_qp(qp);
2847
Moni Shoua776a3902018-01-02 16:19:33 +02002848 if (mqp->qp_sub_type == MLX5_IB_QPT_DCT)
2849 return mlx5_ib_destroy_dct(mqp);
2850
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002851 destroy_qp_common(dev, mqp, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002852
2853 kfree(mqp);
2854
2855 return 0;
2856}
2857
Yonatan Cohena60109d2018-10-10 09:25:16 +03002858static int to_mlx5_access_flags(struct mlx5_ib_qp *qp,
2859 const struct ib_qp_attr *attr,
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002860 int attr_mask, __be32 *hw_access_flags_be)
Eli Cohene126ba92013-07-07 17:25:49 +03002861{
Eli Cohene126ba92013-07-07 17:25:49 +03002862 u8 dest_rd_atomic;
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002863 u32 access_flags, hw_access_flags = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002864
Yonatan Cohena60109d2018-10-10 09:25:16 +03002865 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
2866
Eli Cohene126ba92013-07-07 17:25:49 +03002867 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2868 dest_rd_atomic = attr->max_dest_rd_atomic;
2869 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002870 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002871
2872 if (attr_mask & IB_QP_ACCESS_FLAGS)
2873 access_flags = attr->qp_access_flags;
2874 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002875 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002876
2877 if (!dest_rd_atomic)
2878 access_flags &= IB_ACCESS_REMOTE_WRITE;
2879
2880 if (access_flags & IB_ACCESS_REMOTE_READ)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002881 hw_access_flags |= MLX5_QP_BIT_RRE;
Yonatan Cohen13f8d9c2018-11-21 13:48:39 +02002882 if (access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03002883 int atomic_mode;
Eli Cohene126ba92013-07-07 17:25:49 +03002884
Yonatan Cohena60109d2018-10-10 09:25:16 +03002885 atomic_mode = get_atomic_mode(dev, qp->ibqp.qp_type);
2886 if (atomic_mode < 0)
2887 return -EOPNOTSUPP;
2888
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002889 hw_access_flags |= MLX5_QP_BIT_RAE;
2890 hw_access_flags |= atomic_mode << MLX5_ATOMIC_MODE_OFFSET;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002891 }
2892
2893 if (access_flags & IB_ACCESS_REMOTE_WRITE)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002894 hw_access_flags |= MLX5_QP_BIT_RWE;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002895
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002896 *hw_access_flags_be = cpu_to_be32(hw_access_flags);
Yonatan Cohena60109d2018-10-10 09:25:16 +03002897
2898 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002899}
2900
2901enum {
2902 MLX5_PATH_FLAG_FL = 1 << 0,
2903 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2904 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2905};
2906
2907static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2908{
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002909 if (rate == IB_RATE_PORT_CURRENT)
Eli Cohene126ba92013-07-07 17:25:49 +03002910 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002911
Michael Guralnika5a5d192018-12-09 11:49:50 +02002912 if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_600_GBPS)
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002913 return -EINVAL;
2914
2915 while (rate != IB_RATE_PORT_CURRENT &&
2916 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
2917 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
2918 --rate;
2919
2920 return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
Eli Cohene126ba92013-07-07 17:25:49 +03002921}
2922
majd@mellanox.com75850d02016-01-14 19:13:06 +02002923static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002924 struct mlx5_ib_sq *sq, u8 sl,
2925 struct ib_pd *pd)
majd@mellanox.com75850d02016-01-14 19:13:06 +02002926{
2927 void *in;
2928 void *tisc;
2929 int inlen;
2930 int err;
2931
2932 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002933 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002934 if (!in)
2935 return -ENOMEM;
2936
2937 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002938 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002939
2940 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2941 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2942
Leon Romanovskye0b4b472020-04-09 21:03:33 +03002943 err = mlx5_core_modify_tis(dev, sq->tisn, in);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002944
2945 kvfree(in);
2946
2947 return err;
2948}
2949
Aviv Heller13eab212016-09-18 20:48:04 +03002950static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002951 struct mlx5_ib_sq *sq, u8 tx_affinity,
2952 struct ib_pd *pd)
Aviv Heller13eab212016-09-18 20:48:04 +03002953{
2954 void *in;
2955 void *tisc;
2956 int inlen;
2957 int err;
2958
2959 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002960 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002961 if (!in)
2962 return -ENOMEM;
2963
2964 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002965 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
Aviv Heller13eab212016-09-18 20:48:04 +03002966
2967 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2968 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2969
Leon Romanovskye0b4b472020-04-09 21:03:33 +03002970 err = mlx5_core_modify_tis(dev, sq->tisn, in);
Aviv Heller13eab212016-09-18 20:48:04 +03002971
2972 kvfree(in);
2973
2974 return err;
2975}
2976
majd@mellanox.com75850d02016-01-14 19:13:06 +02002977static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002978 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002979 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002980 u32 path_flags, const struct ib_qp_attr *attr,
2981 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002982{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002983 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002984 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002985 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002986 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2987 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002988
Eli Cohene126ba92013-07-07 17:25:49 +03002989 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002990 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2991 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002992
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002993 if (ah_flags & IB_AH_GRH) {
2994 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002995 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002996 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002997 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002998 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002999 return -EINVAL;
3000 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02003001 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04003002
3003 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003004 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02003005 return -EINVAL;
Parav Pandit47ec3862018-06-13 10:22:06 +03003006
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04003007 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02003008 if (qp->ibqp.qp_type == IB_QPT_RC ||
3009 qp->ibqp.qp_type == IB_QPT_UC ||
3010 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
3011 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
Parav Pandit47ec3862018-06-13 10:22:06 +03003012 path->udp_sport =
3013 mlx5_get_roce_udp_sport(dev, ah->grh.sgid_attr);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003014 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Parav Pandit47ec3862018-06-13 10:22:06 +03003015 gid_type = ah->grh.sgid_attr->gid_type;
Majd Dibbinyed884512017-01-18 14:10:35 +02003016 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003017 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003018 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003019 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
3020 path->fl_free_ar |=
3021 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003022 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
3023 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
3024 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02003025 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003026 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003027 }
3028
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003029 if (ah_flags & IB_AH_GRH) {
3030 path->mgid_index = grh->sgid_index;
3031 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03003032 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003033 cpu_to_be32((grh->traffic_class << 20) |
3034 (grh->flow_label));
3035 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003036 }
3037
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003038 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03003039 if (err < 0)
3040 return err;
3041 path->static_rate = err;
3042 path->port = port;
3043
Eli Cohene126ba92013-07-07 17:25:49 +03003044 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03003045 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03003046
majd@mellanox.com75850d02016-01-14 19:13:06 +02003047 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
3048 return modify_raw_packet_eth_prio(dev->mdev,
3049 &qp->raw_packet_qp.sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003050 sl & 0xf, qp->ibqp.pd);
majd@mellanox.com75850d02016-01-14 19:13:06 +02003051
Eli Cohene126ba92013-07-07 17:25:49 +03003052 return 0;
3053}
3054
3055static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
3056 [MLX5_QP_STATE_INIT] = {
3057 [MLX5_QP_STATE_INIT] = {
3058 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3059 MLX5_QP_OPTPAR_RAE |
3060 MLX5_QP_OPTPAR_RWE |
3061 MLX5_QP_OPTPAR_PKEY_INDEX |
3062 MLX5_QP_OPTPAR_PRI_PORT,
3063 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
3064 MLX5_QP_OPTPAR_PKEY_INDEX |
3065 MLX5_QP_OPTPAR_PRI_PORT,
3066 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3067 MLX5_QP_OPTPAR_Q_KEY |
3068 MLX5_QP_OPTPAR_PRI_PORT,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003069 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3070 MLX5_QP_OPTPAR_RAE |
3071 MLX5_QP_OPTPAR_RWE |
3072 MLX5_QP_OPTPAR_PKEY_INDEX |
3073 MLX5_QP_OPTPAR_PRI_PORT,
Eli Cohene126ba92013-07-07 17:25:49 +03003074 },
3075 [MLX5_QP_STATE_RTR] = {
3076 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3077 MLX5_QP_OPTPAR_RRE |
3078 MLX5_QP_OPTPAR_RAE |
3079 MLX5_QP_OPTPAR_RWE |
3080 MLX5_QP_OPTPAR_PKEY_INDEX,
3081 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3082 MLX5_QP_OPTPAR_RWE |
3083 MLX5_QP_OPTPAR_PKEY_INDEX,
3084 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3085 MLX5_QP_OPTPAR_Q_KEY,
3086 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
3087 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03003088 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3089 MLX5_QP_OPTPAR_RRE |
3090 MLX5_QP_OPTPAR_RAE |
3091 MLX5_QP_OPTPAR_RWE |
3092 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03003093 },
3094 },
3095 [MLX5_QP_STATE_RTR] = {
3096 [MLX5_QP_STATE_RTS] = {
3097 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3098 MLX5_QP_OPTPAR_RRE |
3099 MLX5_QP_OPTPAR_RAE |
3100 MLX5_QP_OPTPAR_RWE |
3101 MLX5_QP_OPTPAR_PM_STATE |
3102 MLX5_QP_OPTPAR_RNR_TIMEOUT,
3103 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3104 MLX5_QP_OPTPAR_RWE |
3105 MLX5_QP_OPTPAR_PM_STATE,
3106 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003107 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3108 MLX5_QP_OPTPAR_RRE |
3109 MLX5_QP_OPTPAR_RAE |
3110 MLX5_QP_OPTPAR_RWE |
3111 MLX5_QP_OPTPAR_PM_STATE |
3112 MLX5_QP_OPTPAR_RNR_TIMEOUT,
Eli Cohene126ba92013-07-07 17:25:49 +03003113 },
3114 },
3115 [MLX5_QP_STATE_RTS] = {
3116 [MLX5_QP_STATE_RTS] = {
3117 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3118 MLX5_QP_OPTPAR_RAE |
3119 MLX5_QP_OPTPAR_RWE |
3120 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03003121 MLX5_QP_OPTPAR_PM_STATE |
3122 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003123 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03003124 MLX5_QP_OPTPAR_PM_STATE |
3125 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003126 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
3127 MLX5_QP_OPTPAR_SRQN |
3128 MLX5_QP_OPTPAR_CQN_RCV,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003129 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3130 MLX5_QP_OPTPAR_RAE |
3131 MLX5_QP_OPTPAR_RWE |
3132 MLX5_QP_OPTPAR_RNR_TIMEOUT |
3133 MLX5_QP_OPTPAR_PM_STATE |
3134 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003135 },
3136 },
3137 [MLX5_QP_STATE_SQER] = {
3138 [MLX5_QP_STATE_RTS] = {
3139 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
3140 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03003141 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03003142 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3143 MLX5_QP_OPTPAR_RWE |
3144 MLX5_QP_OPTPAR_RAE |
3145 MLX5_QP_OPTPAR_RRE,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003146 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3147 MLX5_QP_OPTPAR_RWE |
3148 MLX5_QP_OPTPAR_RAE |
3149 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03003150 },
3151 },
3152};
3153
3154static int ib_nr_to_mlx5_nr(int ib_mask)
3155{
3156 switch (ib_mask) {
3157 case IB_QP_STATE:
3158 return 0;
3159 case IB_QP_CUR_STATE:
3160 return 0;
3161 case IB_QP_EN_SQD_ASYNC_NOTIFY:
3162 return 0;
3163 case IB_QP_ACCESS_FLAGS:
3164 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
3165 MLX5_QP_OPTPAR_RAE;
3166 case IB_QP_PKEY_INDEX:
3167 return MLX5_QP_OPTPAR_PKEY_INDEX;
3168 case IB_QP_PORT:
3169 return MLX5_QP_OPTPAR_PRI_PORT;
3170 case IB_QP_QKEY:
3171 return MLX5_QP_OPTPAR_Q_KEY;
3172 case IB_QP_AV:
3173 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
3174 MLX5_QP_OPTPAR_PRI_PORT;
3175 case IB_QP_PATH_MTU:
3176 return 0;
3177 case IB_QP_TIMEOUT:
3178 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
3179 case IB_QP_RETRY_CNT:
3180 return MLX5_QP_OPTPAR_RETRY_COUNT;
3181 case IB_QP_RNR_RETRY:
3182 return MLX5_QP_OPTPAR_RNR_RETRY;
3183 case IB_QP_RQ_PSN:
3184 return 0;
3185 case IB_QP_MAX_QP_RD_ATOMIC:
3186 return MLX5_QP_OPTPAR_SRA_MAX;
3187 case IB_QP_ALT_PATH:
3188 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
3189 case IB_QP_MIN_RNR_TIMER:
3190 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
3191 case IB_QP_SQ_PSN:
3192 return 0;
3193 case IB_QP_MAX_DEST_RD_ATOMIC:
3194 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
3195 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
3196 case IB_QP_PATH_MIG_STATE:
3197 return MLX5_QP_OPTPAR_PM_STATE;
3198 case IB_QP_CAP:
3199 return 0;
3200 case IB_QP_DEST_QPN:
3201 return 0;
3202 }
3203 return 0;
3204}
3205
3206static int ib_mask_to_mlx5_opt(int ib_mask)
3207{
3208 int result = 0;
3209 int i;
3210
3211 for (i = 0; i < 8 * sizeof(int); i++) {
3212 if ((1 << i) & ib_mask)
3213 result |= ib_nr_to_mlx5_nr(1 << i);
3214 }
3215
3216 return result;
3217}
3218
Yishai Hadas34d57582018-09-20 21:39:21 +03003219static int modify_raw_packet_qp_rq(
3220 struct mlx5_ib_dev *dev, struct mlx5_ib_rq *rq, int new_state,
3221 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003222{
3223 void *in;
3224 void *rqc;
3225 int inlen;
3226 int err;
3227
3228 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003229 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003230 if (!in)
3231 return -ENOMEM;
3232
3233 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
Yishai Hadas34d57582018-09-20 21:39:21 +03003234 MLX5_SET(modify_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003235
3236 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
3237 MLX5_SET(rqc, rqc, state, new_state);
3238
Alex Veskereb49ab02016-08-28 12:25:53 +03003239 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
3240 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
3241 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02003242 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03003243 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
3244 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06003245 dev_info_once(
3246 &dev->ib_dev.dev,
3247 "RAW PACKET QP counters are not supported on current FW\n");
Alex Veskereb49ab02016-08-28 12:25:53 +03003248 }
3249
Leon Romanovskye0b4b472020-04-09 21:03:33 +03003250 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003251 if (err)
3252 goto out;
3253
3254 rq->state = new_state;
3255
3256out:
3257 kvfree(in);
3258 return err;
3259}
3260
Yishai Hadasc14003f2018-09-20 21:39:22 +03003261static int modify_raw_packet_qp_sq(
3262 struct mlx5_core_dev *dev, struct mlx5_ib_sq *sq, int new_state,
3263 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003264{
Bodong Wang7d29f342016-12-01 13:43:16 +02003265 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
Bodong Wang61147f32018-03-19 15:10:30 +02003266 struct mlx5_rate_limit old_rl = ibqp->rl;
3267 struct mlx5_rate_limit new_rl = old_rl;
3268 bool new_rate_added = false;
Bodong Wang7d29f342016-12-01 13:43:16 +02003269 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003270 void *in;
3271 void *sqc;
3272 int inlen;
3273 int err;
3274
3275 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003276 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003277 if (!in)
3278 return -ENOMEM;
3279
Yishai Hadasc14003f2018-09-20 21:39:22 +03003280 MLX5_SET(modify_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003281 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
3282
3283 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
3284 MLX5_SET(sqc, sqc, state, new_state);
3285
Bodong Wang7d29f342016-12-01 13:43:16 +02003286 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
3287 if (new_state != MLX5_SQC_STATE_RDY)
3288 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
3289 __func__);
3290 else
Bodong Wang61147f32018-03-19 15:10:30 +02003291 new_rl = raw_qp_param->rl;
Bodong Wang7d29f342016-12-01 13:43:16 +02003292 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003293
Bodong Wang61147f32018-03-19 15:10:30 +02003294 if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
3295 if (new_rl.rate) {
3296 err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003297 if (err) {
Bodong Wang61147f32018-03-19 15:10:30 +02003298 pr_err("Failed configuring rate limit(err %d): \
3299 rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
3300 err, new_rl.rate, new_rl.max_burst_sz,
3301 new_rl.typical_pkt_sz);
3302
Bodong Wang7d29f342016-12-01 13:43:16 +02003303 goto out;
3304 }
Bodong Wang61147f32018-03-19 15:10:30 +02003305 new_rate_added = true;
Bodong Wang7d29f342016-12-01 13:43:16 +02003306 }
3307
3308 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
Bodong Wang61147f32018-03-19 15:10:30 +02003309 /* index 0 means no limit */
Bodong Wang7d29f342016-12-01 13:43:16 +02003310 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
3311 }
3312
Leon Romanovskye0b4b472020-04-09 21:03:33 +03003313 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in);
Bodong Wang7d29f342016-12-01 13:43:16 +02003314 if (err) {
3315 /* Remove new rate from table if failed */
Bodong Wang61147f32018-03-19 15:10:30 +02003316 if (new_rate_added)
3317 mlx5_rl_remove_rate(dev, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003318 goto out;
3319 }
3320
3321 /* Only remove the old rate after new rate was set */
Rafi Wienerc8973df2019-10-02 15:02:43 +03003322 if ((old_rl.rate && !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
3323 (new_state != MLX5_SQC_STATE_RDY)) {
Bodong Wang61147f32018-03-19 15:10:30 +02003324 mlx5_rl_remove_rate(dev, &old_rl);
Rafi Wienerc8973df2019-10-02 15:02:43 +03003325 if (new_state != MLX5_SQC_STATE_RDY)
3326 memset(&new_rl, 0, sizeof(new_rl));
3327 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003328
Bodong Wang61147f32018-03-19 15:10:30 +02003329 ibqp->rl = new_rl;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003330 sq->state = new_state;
3331
3332out:
3333 kvfree(in);
3334 return err;
3335}
3336
3337static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03003338 const struct mlx5_modify_raw_qp_param *raw_qp_param,
3339 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003340{
3341 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
3342 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
3343 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02003344 int modify_rq = !!qp->rq.wqe_cnt;
3345 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003346 int rq_state;
3347 int sq_state;
3348 int err;
3349
Alex Vesker0680efa2016-08-28 12:25:52 +03003350 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003351 case MLX5_CMD_OP_RST2INIT_QP:
3352 rq_state = MLX5_RQC_STATE_RDY;
3353 sq_state = MLX5_SQC_STATE_RDY;
3354 break;
3355 case MLX5_CMD_OP_2ERR_QP:
3356 rq_state = MLX5_RQC_STATE_ERR;
3357 sq_state = MLX5_SQC_STATE_ERR;
3358 break;
3359 case MLX5_CMD_OP_2RST_QP:
3360 rq_state = MLX5_RQC_STATE_RST;
3361 sq_state = MLX5_SQC_STATE_RST;
3362 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003363 case MLX5_CMD_OP_RTR2RTS_QP:
3364 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02003365 if (raw_qp_param->set_mask ==
3366 MLX5_RAW_QP_RATE_LIMIT) {
3367 modify_rq = 0;
3368 sq_state = sq->state;
3369 } else {
3370 return raw_qp_param->set_mask ? -EINVAL : 0;
3371 }
3372 break;
3373 case MLX5_CMD_OP_INIT2INIT_QP:
3374 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03003375 if (raw_qp_param->set_mask)
3376 return -EINVAL;
3377 else
3378 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003379 default:
3380 WARN_ON(1);
3381 return -EINVAL;
3382 }
3383
Bodong Wang7d29f342016-12-01 13:43:16 +02003384 if (modify_rq) {
Yishai Hadas34d57582018-09-20 21:39:21 +03003385 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param,
3386 qp->ibqp.pd);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003387 if (err)
3388 return err;
3389 }
3390
Bodong Wang7d29f342016-12-01 13:43:16 +02003391 if (modify_sq) {
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003392 struct mlx5_flow_handle *flow_rule;
3393
Aviv Heller13eab212016-09-18 20:48:04 +03003394 if (tx_affinity) {
3395 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003396 tx_affinity,
3397 qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003398 if (err)
3399 return err;
3400 }
3401
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003402 flow_rule = create_flow_rule_vport_sq(dev, sq,
3403 raw_qp_param->port);
3404 if (IS_ERR(flow_rule))
Colin Ian King1db86312019-04-12 11:40:17 +01003405 return PTR_ERR(flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003406
3407 err = modify_raw_packet_qp_sq(dev->mdev, sq, sq_state,
3408 raw_qp_param, qp->ibqp.pd);
3409 if (err) {
3410 if (flow_rule)
3411 mlx5_del_flow_rules(flow_rule);
3412 return err;
3413 }
3414
3415 if (flow_rule) {
3416 destroy_flow_rule_vport_sq(sq);
3417 sq->flow_rule = flow_rule;
3418 }
3419
3420 return err;
Aviv Heller13eab212016-09-18 20:48:04 +03003421 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003422
3423 return 0;
3424}
3425
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003426static unsigned int get_tx_affinity(struct mlx5_ib_dev *dev,
3427 struct mlx5_ib_pd *pd,
3428 struct mlx5_ib_qp_base *qp_base,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003429 u8 port_num, struct ib_udata *udata)
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003430{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003431 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
3432 udata, struct mlx5_ib_ucontext, ibucontext);
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003433 unsigned int tx_port_affinity;
3434
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003435 if (ucontext) {
3436 tx_port_affinity = (unsigned int)atomic_add_return(
3437 1, &ucontext->tx_port_affinity) %
3438 MLX5_MAX_PORTS +
3439 1;
3440 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x ucontext %p\n",
3441 tx_port_affinity, qp_base->mqp.qpn, ucontext);
3442 } else {
3443 tx_port_affinity =
3444 (unsigned int)atomic_add_return(
Mark Bloch95579e72019-03-28 15:27:33 +02003445 1, &dev->port[port_num].roce.tx_port_affinity) %
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003446 MLX5_MAX_PORTS +
3447 1;
3448 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x\n",
3449 tx_port_affinity, qp_base->mqp.qpn);
3450 }
3451
3452 return tx_port_affinity;
3453}
3454
Mark Zhangd14133d2019-07-02 13:02:36 +03003455static int __mlx5_ib_qp_set_counter(struct ib_qp *qp,
3456 struct rdma_counter *counter)
3457{
3458 struct mlx5_ib_dev *dev = to_mdev(qp->device);
3459 struct mlx5_ib_qp *mqp = to_mqp(qp);
3460 struct mlx5_qp_context context = {};
Mark Zhangd14133d2019-07-02 13:02:36 +03003461 struct mlx5_ib_qp_base *base;
3462 u32 set_id;
3463
Parav Pandit3e1f0002019-07-23 10:31:17 +03003464 if (counter)
Mark Zhangd14133d2019-07-02 13:02:36 +03003465 set_id = counter->id;
Parav Pandit3e1f0002019-07-23 10:31:17 +03003466 else
3467 set_id = mlx5_ib_get_counters_id(dev, mqp->port - 1);
Mark Zhangd14133d2019-07-02 13:02:36 +03003468
3469 base = &mqp->trans_qp.base;
3470 context.qp_counter_set_usr_page &= cpu_to_be32(0xffffff);
3471 context.qp_counter_set_usr_page |= cpu_to_be32(set_id << 24);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03003472 return mlx5_core_qp_modify(dev, MLX5_CMD_OP_RTS2RTS_QP,
3473 MLX5_QP_OPTPAR_COUNTER_SET_ID, &context,
3474 &base->mqp);
Mark Zhangd14133d2019-07-02 13:02:36 +03003475}
3476
Eli Cohene126ba92013-07-07 17:25:49 +03003477static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
3478 const struct ib_qp_attr *attr, int attr_mask,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003479 enum ib_qp_state cur_state,
3480 enum ib_qp_state new_state,
3481 const struct mlx5_ib_modify_qp *ucmd,
3482 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003483{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003484 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
3485 [MLX5_QP_STATE_RST] = {
3486 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3487 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3488 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
3489 },
3490 [MLX5_QP_STATE_INIT] = {
3491 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3492 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3493 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
3494 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
3495 },
3496 [MLX5_QP_STATE_RTR] = {
3497 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3498 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3499 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
3500 },
3501 [MLX5_QP_STATE_RTS] = {
3502 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3503 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3504 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
3505 },
3506 [MLX5_QP_STATE_SQD] = {
3507 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3508 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3509 },
3510 [MLX5_QP_STATE_SQER] = {
3511 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3512 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3513 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
3514 },
3515 [MLX5_QP_STATE_ERR] = {
3516 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3517 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3518 }
3519 };
3520
Eli Cohene126ba92013-07-07 17:25:49 +03003521 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3522 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02003523 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03003524 struct mlx5_ib_cq *send_cq, *recv_cq;
3525 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03003526 struct mlx5_ib_pd *pd;
3527 enum mlx5_qp_state mlx5_cur, mlx5_new;
3528 enum mlx5_qp_optpar optpar;
Mark Zhangd14133d2019-07-02 13:02:36 +03003529 u32 set_id = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003530 int mlx5_st;
3531 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003532 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03003533 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003534
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003535 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
3536 qp->qp_sub_type : ibqp->qp_type);
3537 if (mlx5_st < 0)
3538 return -EINVAL;
3539
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003540 context = kzalloc(sizeof(*context), GFP_KERNEL);
3541 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03003542 return -ENOMEM;
3543
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003544 pd = get_pd(qp);
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003545 context->flags = cpu_to_be32(mlx5_st << 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003546
3547 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
3548 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3549 } else {
3550 switch (attr->path_mig_state) {
3551 case IB_MIG_MIGRATED:
3552 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3553 break;
3554 case IB_MIG_REARM:
3555 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
3556 break;
3557 case IB_MIG_ARMED:
3558 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
3559 break;
3560 }
3561 }
3562
Aviv Heller13eab212016-09-18 20:48:04 +03003563 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
3564 if ((ibqp->qp_type == IB_QPT_RC) ||
3565 (ibqp->qp_type == IB_QPT_UD &&
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003566 !(qp->flags & MLX5_IB_QP_CREATE_SQPN_QP1)) ||
Aviv Heller13eab212016-09-18 20:48:04 +03003567 (ibqp->qp_type == IB_QPT_UC) ||
3568 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
3569 (ibqp->qp_type == IB_QPT_XRC_INI) ||
3570 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
Aviv Heller7c34ec12018-08-23 13:47:53 +03003571 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02003572 u8 p = mlx5_core_native_port_num(dev->mdev) - 1;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003573 tx_affinity = get_tx_affinity(dev, pd, base, p,
3574 udata);
Aviv Heller13eab212016-09-18 20:48:04 +03003575 context->flags |= cpu_to_be32(tx_affinity << 24);
3576 }
3577 }
3578 }
3579
Haggai Erand16e91d2016-02-29 15:45:05 +02003580 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003581 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003582 } else if ((ibqp->qp_type == IB_QPT_UD &&
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003583 !(qp->flags & IB_QP_CREATE_SOURCE_QPN)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003584 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
3585 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
3586 } else if (attr_mask & IB_QP_PATH_MTU) {
3587 if (attr->path_mtu < IB_MTU_256 ||
3588 attr->path_mtu > IB_MTU_4096) {
3589 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3590 err = -EINVAL;
3591 goto out;
3592 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003593 context->mtu_msgmax = (attr->path_mtu << 5) |
3594 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03003595 }
3596
3597 if (attr_mask & IB_QP_DEST_QPN)
3598 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
3599
3600 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003601 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003602
3603 /* todo implement counter_index functionality */
3604
3605 if (is_sqp(ibqp->qp_type))
3606 context->pri_path.port = qp->port;
3607
3608 if (attr_mask & IB_QP_PORT)
3609 context->pri_path.port = attr->port_num;
3610
3611 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003612 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003613 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003614 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003615 if (err)
3616 goto out;
3617 }
3618
3619 if (attr_mask & IB_QP_TIMEOUT)
3620 context->pri_path.ackto_lt |= attr->timeout << 3;
3621
3622 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003623 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3624 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003625 attr->alt_port_num,
3626 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3627 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003628 if (err)
3629 goto out;
3630 }
3631
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003632 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3633 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003634
3635 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3636 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3637 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3638 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3639
3640 if (attr_mask & IB_QP_RNR_RETRY)
3641 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3642
3643 if (attr_mask & IB_QP_RETRY_CNT)
3644 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3645
3646 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3647 if (attr->max_rd_atomic)
3648 context->params1 |=
3649 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3650 }
3651
3652 if (attr_mask & IB_QP_SQ_PSN)
3653 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3654
3655 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3656 if (attr->max_dest_rd_atomic)
3657 context->params2 |=
3658 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3659 }
3660
Yonatan Cohena60109d2018-10-10 09:25:16 +03003661 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08003662 __be32 access_flags;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003663
3664 err = to_mlx5_access_flags(qp, attr, attr_mask, &access_flags);
3665 if (err)
3666 goto out;
3667
3668 context->params2 |= access_flags;
3669 }
Eli Cohene126ba92013-07-07 17:25:49 +03003670
3671 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3672 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3673
3674 if (attr_mask & IB_QP_RQ_PSN)
3675 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3676
3677 if (attr_mask & IB_QP_QKEY)
3678 context->qkey = cpu_to_be32(attr->qkey);
3679
3680 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3681 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3682
Mark Bloch0837e862016-06-17 15:10:55 +03003683 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3684 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3685 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003686
3687 /* Underlay port should be used - index 0 function per port */
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003688 if (qp->flags & IB_QP_CREATE_SOURCE_QPN)
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003689 port_num = 0;
3690
Mark Zhangd14133d2019-07-02 13:02:36 +03003691 if (ibqp->counter)
3692 set_id = ibqp->counter->id;
3693 else
Parav Pandit3e1f0002019-07-23 10:31:17 +03003694 set_id = mlx5_ib_get_counters_id(dev, port_num);
Mark Bloch0837e862016-06-17 15:10:55 +03003695 context->qp_counter_set_usr_page |=
Mark Zhangd14133d2019-07-02 13:02:36 +03003696 cpu_to_be32(set_id << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003697 }
3698
Eli Cohene126ba92013-07-07 17:25:49 +03003699 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3700 context->sq_crq_size |= cpu_to_be16(1 << 4);
3701
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003702 if (qp->flags & MLX5_IB_QP_CREATE_SQPN_QP1)
Haggai Eranb11a4f92016-02-29 15:45:03 +02003703 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003704
3705 mlx5_cur = to_mlx5_state(cur_state);
3706 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003707
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003708 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003709 !optab[mlx5_cur][mlx5_new]) {
3710 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003711 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003712 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003713
3714 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003715 optpar = ib_mask_to_mlx5_opt(attr_mask);
3716 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003717
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003718 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003719 qp->flags & IB_QP_CREATE_SOURCE_QPN) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003720 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3721
3722 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003723 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Mark Zhangd14133d2019-07-02 13:02:36 +03003724 raw_qp_param.rq_q_ctr_id = set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003725 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3726 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003727
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003728 if (attr_mask & IB_QP_PORT)
3729 raw_qp_param.port = attr->port_num;
3730
Bodong Wang7d29f342016-12-01 13:43:16 +02003731 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003732 raw_qp_param.rl.rate = attr->rate_limit;
3733
3734 if (ucmd->burst_info.max_burst_sz) {
3735 if (attr->rate_limit &&
3736 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3737 raw_qp_param.rl.max_burst_sz =
3738 ucmd->burst_info.max_burst_sz;
3739 } else {
3740 err = -EINVAL;
3741 goto out;
3742 }
3743 }
3744
3745 if (ucmd->burst_info.typical_pkt_sz) {
3746 if (attr->rate_limit &&
3747 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3748 raw_qp_param.rl.typical_pkt_sz =
3749 ucmd->burst_info.typical_pkt_sz;
3750 } else {
3751 err = -EINVAL;
3752 goto out;
3753 }
3754 }
3755
Bodong Wang7d29f342016-12-01 13:43:16 +02003756 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3757 }
3758
Aviv Heller13eab212016-09-18 20:48:04 +03003759 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003760 } else {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03003761 err = mlx5_core_qp_modify(dev, op, optpar, context, &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003762 }
3763
Eli Cohene126ba92013-07-07 17:25:49 +03003764 if (err)
3765 goto out;
3766
3767 qp->state = new_state;
3768
3769 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003770 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003771 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003772 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003773 if (attr_mask & IB_QP_PORT)
3774 qp->port = attr->port_num;
3775 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003776 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003777
3778 /*
3779 * If we moved a kernel QP to RESET, clean up all old CQ
3780 * entries and reinitialize the QP.
3781 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02003782 if (new_state == IB_QPS_RESET &&
3783 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003784 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003785 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3786 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003787 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003788
3789 qp->rq.head = 0;
3790 qp->rq.tail = 0;
3791 qp->sq.head = 0;
3792 qp->sq.tail = 0;
3793 qp->sq.cur_post = 0;
Guy Levi34f4c952018-11-26 08:15:50 +02003794 if (qp->sq.wqe_cnt)
3795 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
Leon Romanovsky950bf4f2020-03-18 11:16:40 +02003796 qp->sq.last_poll = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003797 qp->db.db[MLX5_RCV_DBR] = 0;
3798 qp->db.db[MLX5_SND_DBR] = 0;
3799 }
3800
Mark Zhangd14133d2019-07-02 13:02:36 +03003801 if ((new_state == IB_QPS_RTS) && qp->counter_pending) {
3802 err = __mlx5_ib_qp_set_counter(ibqp, ibqp->counter);
3803 if (!err)
3804 qp->counter_pending = 0;
3805 }
3806
Eli Cohene126ba92013-07-07 17:25:49 +03003807out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003808 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003809 return err;
3810}
3811
Moni Shouac32a4f22018-01-02 16:19:32 +02003812static inline bool is_valid_mask(int mask, int req, int opt)
3813{
3814 if ((mask & req) != req)
3815 return false;
3816
3817 if (mask & ~(req | opt))
3818 return false;
3819
3820 return true;
3821}
3822
3823/* check valid transition for driver QP types
3824 * for now the only QP type that this function supports is DCI
3825 */
3826static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3827 enum ib_qp_attr_mask attr_mask)
3828{
3829 int req = IB_QP_STATE;
3830 int opt = 0;
3831
Moni Shoua99ed7482018-09-12 09:33:55 +03003832 if (new_state == IB_QPS_RESET) {
3833 return is_valid_mask(attr_mask, req, opt);
3834 } else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Moni Shouac32a4f22018-01-02 16:19:32 +02003835 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
3836 return is_valid_mask(attr_mask, req, opt);
3837 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3838 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3839 return is_valid_mask(attr_mask, req, opt);
3840 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3841 req |= IB_QP_PATH_MTU;
Artemy Kovalyov5ec03042018-11-05 08:12:07 +02003842 opt = IB_QP_PKEY_INDEX | IB_QP_AV;
Moni Shouac32a4f22018-01-02 16:19:32 +02003843 return is_valid_mask(attr_mask, req, opt);
3844 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3845 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3846 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3847 opt = IB_QP_MIN_RNR_TIMER;
3848 return is_valid_mask(attr_mask, req, opt);
3849 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3850 opt = IB_QP_MIN_RNR_TIMER;
3851 return is_valid_mask(attr_mask, req, opt);
3852 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3853 return is_valid_mask(attr_mask, req, opt);
3854 }
3855 return false;
3856}
3857
Moni Shoua776a3902018-01-02 16:19:33 +02003858/* mlx5_ib_modify_dct: modify a DCT QP
3859 * valid transitions are:
3860 * RESET to INIT: must set access_flags, pkey_index and port
3861 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3862 * mtu, gid_index and hop_limit
3863 * Other transitions and attributes are illegal
3864 */
3865static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3866 int attr_mask, struct ib_udata *udata)
3867{
3868 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3869 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3870 enum ib_qp_state cur_state, new_state;
3871 int err = 0;
3872 int required = IB_QP_STATE;
3873 void *dctc;
3874
3875 if (!(attr_mask & IB_QP_STATE))
3876 return -EINVAL;
3877
3878 cur_state = qp->state;
3879 new_state = attr->qp_state;
3880
3881 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3882 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03003883 u16 set_id;
3884
Moni Shoua776a3902018-01-02 16:19:33 +02003885 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3886 if (!is_valid_mask(attr_mask, required, 0))
3887 return -EINVAL;
3888
3889 if (attr->port_num == 0 ||
3890 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3891 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3892 attr->port_num, dev->num_ports);
3893 return -EINVAL;
3894 }
3895 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3896 MLX5_SET(dctc, dctc, rre, 1);
3897 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3898 MLX5_SET(dctc, dctc, rwe, 1);
3899 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03003900 int atomic_mode;
3901
3902 atomic_mode = get_atomic_mode(dev, MLX5_IB_QPT_DCT);
3903 if (atomic_mode < 0)
Moni Shoua776a3902018-01-02 16:19:33 +02003904 return -EOPNOTSUPP;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003905
3906 MLX5_SET(dctc, dctc, atomic_mode, atomic_mode);
Moni Shoua776a3902018-01-02 16:19:33 +02003907 MLX5_SET(dctc, dctc, rae, 1);
Moni Shoua776a3902018-01-02 16:19:33 +02003908 }
3909 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3910 MLX5_SET(dctc, dctc, port, attr->port_num);
Parav Pandit3e1f0002019-07-23 10:31:17 +03003911
3912 set_id = mlx5_ib_get_counters_id(dev, attr->port_num - 1);
3913 MLX5_SET(dctc, dctc, counter_set_id, set_id);
Moni Shoua776a3902018-01-02 16:19:33 +02003914
3915 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3916 struct mlx5_ib_modify_qp_resp resp = {};
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003917 u32 out[MLX5_ST_SZ_DW(create_dct_out)] = {0};
Moni Shoua776a3902018-01-02 16:19:33 +02003918 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3919 sizeof(resp.dctn);
3920
3921 if (udata->outlen < min_resp_len)
3922 return -EINVAL;
3923 resp.response_length = min_resp_len;
3924
3925 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3926 if (!is_valid_mask(attr_mask, required, 0))
3927 return -EINVAL;
3928 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3929 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3930 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3931 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3932 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3933 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3934
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03003935 err = mlx5_core_create_dct(dev, &qp->dct.mdct, qp->dct.in,
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003936 MLX5_ST_SZ_BYTES(create_dct_in), out,
3937 sizeof(out));
Moni Shoua776a3902018-01-02 16:19:33 +02003938 if (err)
3939 return err;
3940 resp.dctn = qp->dct.mdct.mqp.qpn;
3941 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3942 if (err) {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03003943 mlx5_core_destroy_dct(dev, &qp->dct.mdct);
Moni Shoua776a3902018-01-02 16:19:33 +02003944 return err;
3945 }
3946 } else {
3947 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3948 return -EINVAL;
3949 }
3950 if (err)
3951 qp->state = IB_QPS_ERR;
3952 else
3953 qp->state = new_state;
3954 return err;
3955}
3956
Eli Cohene126ba92013-07-07 17:25:49 +03003957int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3958 int attr_mask, struct ib_udata *udata)
3959{
3960 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3961 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02003962 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02003963 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003964 enum ib_qp_state cur_state, new_state;
Bodong Wang61147f32018-03-19 15:10:30 +02003965 size_t required_cmd_sz;
Eli Cohene126ba92013-07-07 17:25:49 +03003966 int err = -EINVAL;
3967 int port;
3968
Yishai Hadas28d61372016-05-23 15:20:56 +03003969 if (ibqp->rwq_ind_tbl)
3970 return -ENOSYS;
3971
Bodong Wang61147f32018-03-19 15:10:30 +02003972 if (udata && udata->inlen) {
3973 required_cmd_sz = offsetof(typeof(ucmd), reserved) +
3974 sizeof(ucmd.reserved);
3975 if (udata->inlen < required_cmd_sz)
3976 return -EINVAL;
3977
3978 if (udata->inlen > sizeof(ucmd) &&
3979 !ib_is_udata_cleared(udata, sizeof(ucmd),
3980 udata->inlen - sizeof(ucmd)))
3981 return -EOPNOTSUPP;
3982
3983 if (ib_copy_from_udata(&ucmd, udata,
3984 min(udata->inlen, sizeof(ucmd))))
3985 return -EFAULT;
3986
3987 if (ucmd.comp_mask ||
3988 memchr_inv(&ucmd.reserved, 0, sizeof(ucmd.reserved)) ||
3989 memchr_inv(&ucmd.burst_info.reserved, 0,
3990 sizeof(ucmd.burst_info.reserved)))
3991 return -EOPNOTSUPP;
3992 }
3993
Haggai Erand16e91d2016-02-29 15:45:05 +02003994 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3995 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3996
Moni Shouac32a4f22018-01-02 16:19:32 +02003997 if (ibqp->qp_type == IB_QPT_DRIVER)
3998 qp_type = qp->qp_sub_type;
3999 else
4000 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
4001 IB_QPT_GSI : ibqp->qp_type;
4002
Moni Shoua776a3902018-01-02 16:19:33 +02004003 if (qp_type == MLX5_IB_QPT_DCT)
4004 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02004005
Eli Cohene126ba92013-07-07 17:25:49 +03004006 mutex_lock(&qp->mutex);
4007
4008 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
4009 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
4010
Achiad Shochat2811ba52015-12-23 18:47:24 +02004011 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
4012 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02004013 }
4014
Leon Romanovsky2be08c32020-04-27 18:46:13 +03004015 if (qp->flags & IB_QP_CREATE_SOURCE_QPN) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004016 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
4017 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
4018 attr_mask);
4019 goto out;
4020 }
4021 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02004022 qp_type != MLX5_IB_QPT_DCI &&
Kamal Heibd31131b2018-10-02 16:11:21 +03004023 !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
4024 attr_mask)) {
Haggai Eran158abf82016-02-29 15:45:04 +02004025 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
4026 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03004027 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02004028 } else if (qp_type == MLX5_IB_QPT_DCI &&
4029 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
4030 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
4031 cur_state, new_state, qp_type, attr_mask);
4032 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004033 }
Eli Cohene126ba92013-07-07 17:25:49 +03004034
4035 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004036 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02004037 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02004038 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
4039 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03004040 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004041 }
Eli Cohene126ba92013-07-07 17:25:49 +03004042
4043 if (attr_mask & IB_QP_PKEY_INDEX) {
4044 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03004045 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02004046 dev->mdev->port_caps[port - 1].pkey_table_len) {
4047 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
4048 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004049 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004050 }
Eli Cohene126ba92013-07-07 17:25:49 +03004051 }
4052
4053 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004054 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02004055 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
4056 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
4057 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03004058 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004059 }
Eli Cohene126ba92013-07-07 17:25:49 +03004060
4061 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004062 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02004063 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
4064 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
4065 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03004066 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004067 }
Eli Cohene126ba92013-07-07 17:25:49 +03004068
4069 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
4070 err = 0;
4071 goto out;
4072 }
4073
Bodong Wang61147f32018-03-19 15:10:30 +02004074 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02004075 new_state, &ucmd, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03004076
4077out:
4078 mutex_unlock(&qp->mutex);
4079 return err;
4080}
4081
Guy Levi34f4c952018-11-26 08:15:50 +02004082static void _handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
4083 u32 wqe_sz, void **cur_edge)
4084{
4085 u32 idx;
4086
4087 idx = (sq->cur_post + (wqe_sz >> 2)) & (sq->wqe_cnt - 1);
4088 *cur_edge = get_sq_edge(sq, idx);
4089
4090 *seg = mlx5_frag_buf_get_wqe(&sq->fbc, idx);
4091}
4092
4093/* handle_post_send_edge - Check if we get to SQ edge. If yes, update to the
4094 * next nearby edge and get new address translation for current WQE position.
4095 * @sq - SQ buffer.
4096 * @seg: Current WQE position (16B aligned).
4097 * @wqe_sz: Total current WQE size [16B].
4098 * @cur_edge: Updated current edge.
4099 */
4100static inline void handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
4101 u32 wqe_sz, void **cur_edge)
4102{
4103 if (likely(*seg != *cur_edge))
4104 return;
4105
4106 _handle_post_send_edge(sq, seg, wqe_sz, cur_edge);
4107}
4108
4109/* memcpy_send_wqe - copy data from src to WQE and update the relevant WQ's
4110 * pointers. At the end @seg is aligned to 16B regardless the copied size.
4111 * @sq - SQ buffer.
4112 * @cur_edge: Updated current edge.
4113 * @seg: Current WQE position (16B aligned).
4114 * @wqe_sz: Total current WQE size [16B].
4115 * @src: Pointer to copy from.
4116 * @n: Number of bytes to copy.
4117 */
4118static inline void memcpy_send_wqe(struct mlx5_ib_wq *sq, void **cur_edge,
4119 void **seg, u32 *wqe_sz, const void *src,
4120 size_t n)
4121{
4122 while (likely(n)) {
4123 size_t leftlen = *cur_edge - *seg;
4124 size_t copysz = min_t(size_t, leftlen, n);
4125 size_t stride;
4126
4127 memcpy(*seg, src, copysz);
4128
4129 n -= copysz;
4130 src += copysz;
4131 stride = !n ? ALIGN(copysz, 16) : copysz;
4132 *seg += stride;
4133 *wqe_sz += stride >> 4;
4134 handle_post_send_edge(sq, seg, *wqe_sz, cur_edge);
4135 }
4136}
4137
Eli Cohene126ba92013-07-07 17:25:49 +03004138static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
4139{
4140 struct mlx5_ib_cq *cq;
4141 unsigned cur;
4142
4143 cur = wq->head - wq->tail;
4144 if (likely(cur + nreq < wq->max_post))
4145 return 0;
4146
4147 cq = to_mcq(ib_cq);
4148 spin_lock(&cq->lock);
4149 cur = wq->head - wq->tail;
4150 spin_unlock(&cq->lock);
4151
4152 return cur + nreq >= wq->max_post;
4153}
4154
4155static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
4156 u64 remote_addr, u32 rkey)
4157{
4158 rseg->raddr = cpu_to_be64(remote_addr);
4159 rseg->rkey = cpu_to_be32(rkey);
4160 rseg->reserved = 0;
4161}
4162
Guy Levi34f4c952018-11-26 08:15:50 +02004163static void set_eth_seg(const struct ib_send_wr *wr, struct mlx5_ib_qp *qp,
4164 void **seg, int *size, void **cur_edge)
Erez Shitritf0313962016-02-21 16:27:17 +02004165{
Guy Levi34f4c952018-11-26 08:15:50 +02004166 struct mlx5_wqe_eth_seg *eseg = *seg;
Erez Shitritf0313962016-02-21 16:27:17 +02004167
4168 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
4169
4170 if (wr->send_flags & IB_SEND_IP_CSUM)
4171 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
4172 MLX5_ETH_WQE_L4_CSUM;
4173
Erez Shitritf0313962016-02-21 16:27:17 +02004174 if (wr->opcode == IB_WR_LSO) {
4175 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Guy Levi34f4c952018-11-26 08:15:50 +02004176 size_t left, copysz;
Erez Shitritf0313962016-02-21 16:27:17 +02004177 void *pdata = ud_wr->header;
Guy Levi34f4c952018-11-26 08:15:50 +02004178 size_t stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004179
4180 left = ud_wr->hlen;
4181 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02004182 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02004183
Guy Levi34f4c952018-11-26 08:15:50 +02004184 /* memcpy_send_wqe should get a 16B align address. Hence, we
4185 * first copy up to the current edge and then, if needed,
4186 * fall-through to memcpy_send_wqe.
Erez Shitritf0313962016-02-21 16:27:17 +02004187 */
Guy Levi34f4c952018-11-26 08:15:50 +02004188 copysz = min_t(u64, *cur_edge - (void *)eseg->inline_hdr.start,
4189 left);
4190 memcpy(eseg->inline_hdr.start, pdata, copysz);
4191 stride = ALIGN(sizeof(struct mlx5_wqe_eth_seg) -
4192 sizeof(eseg->inline_hdr.start) + copysz, 16);
4193 *size += stride / 16;
4194 *seg += stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004195
Guy Levi34f4c952018-11-26 08:15:50 +02004196 if (copysz < left) {
4197 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02004198 left -= copysz;
4199 pdata += copysz;
Guy Levi34f4c952018-11-26 08:15:50 +02004200 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, pdata,
4201 left);
Erez Shitritf0313962016-02-21 16:27:17 +02004202 }
Guy Levi34f4c952018-11-26 08:15:50 +02004203
4204 return;
Erez Shitritf0313962016-02-21 16:27:17 +02004205 }
4206
Guy Levi34f4c952018-11-26 08:15:50 +02004207 *seg += sizeof(struct mlx5_wqe_eth_seg);
4208 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
Erez Shitritf0313962016-02-21 16:27:17 +02004209}
4210
Eli Cohene126ba92013-07-07 17:25:49 +03004211static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004212 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004213{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004214 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
4215 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
4216 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004217}
4218
4219static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
4220{
4221 dseg->byte_count = cpu_to_be32(sg->length);
4222 dseg->lkey = cpu_to_be32(sg->lkey);
4223 dseg->addr = cpu_to_be64(sg->addr);
4224}
4225
Artemy Kovalyov31616252017-01-02 11:37:42 +02004226static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03004227{
Artemy Kovalyov31616252017-01-02 11:37:42 +02004228 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
4229 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03004230}
4231
Moni Shoua841b07f2019-08-15 11:38:34 +03004232static __be64 frwr_mkey_mask(bool atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004233{
4234 u64 result;
4235
4236 result = MLX5_MKEY_MASK_LEN |
4237 MLX5_MKEY_MASK_PAGE_SIZE |
4238 MLX5_MKEY_MASK_START_ADDR |
4239 MLX5_MKEY_MASK_EN_RINVAL |
4240 MLX5_MKEY_MASK_KEY |
4241 MLX5_MKEY_MASK_LR |
4242 MLX5_MKEY_MASK_LW |
4243 MLX5_MKEY_MASK_RR |
4244 MLX5_MKEY_MASK_RW |
Eli Cohene126ba92013-07-07 17:25:49 +03004245 MLX5_MKEY_MASK_SMALL_FENCE |
4246 MLX5_MKEY_MASK_FREE;
4247
Moni Shoua841b07f2019-08-15 11:38:34 +03004248 if (atomic)
4249 result |= MLX5_MKEY_MASK_A;
4250
Eli Cohene126ba92013-07-07 17:25:49 +03004251 return cpu_to_be64(result);
4252}
4253
Sagi Grimberge6631812014-02-23 14:19:11 +02004254static __be64 sig_mkey_mask(void)
4255{
4256 u64 result;
4257
4258 result = MLX5_MKEY_MASK_LEN |
4259 MLX5_MKEY_MASK_PAGE_SIZE |
4260 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004261 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02004262 MLX5_MKEY_MASK_EN_RINVAL |
4263 MLX5_MKEY_MASK_KEY |
4264 MLX5_MKEY_MASK_LR |
4265 MLX5_MKEY_MASK_LW |
4266 MLX5_MKEY_MASK_RR |
4267 MLX5_MKEY_MASK_RW |
4268 MLX5_MKEY_MASK_SMALL_FENCE |
4269 MLX5_MKEY_MASK_FREE |
4270 MLX5_MKEY_MASK_BSF_EN;
4271
4272 return cpu_to_be64(result);
4273}
4274
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004275static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Moni Shoua841b07f2019-08-15 11:38:34 +03004276 struct mlx5_ib_mr *mr, u8 flags, bool atomic)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004277{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004278 int size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004279
4280 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004281
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004282 umr->flags = flags;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004283 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Moni Shoua841b07f2019-08-15 11:38:34 +03004284 umr->mkey_mask = frwr_mkey_mask(atomic);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004285}
4286
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004287static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03004288{
4289 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004290 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03004291 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03004292}
4293
Artemy Kovalyov31616252017-01-02 11:37:42 +02004294static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004295{
4296 u64 result;
4297
Artemy Kovalyov31616252017-01-02 11:37:42 +02004298 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02004299 MLX5_MKEY_MASK_FREE;
4300
4301 return cpu_to_be64(result);
4302}
4303
Artemy Kovalyov31616252017-01-02 11:37:42 +02004304static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004305{
4306 u64 result;
4307
4308 result = MLX5_MKEY_MASK_FREE;
4309
4310 return cpu_to_be64(result);
4311}
4312
Noa Osherovich56e11d62016-02-29 16:46:51 +02004313static __be64 get_umr_update_translation_mask(void)
4314{
4315 u64 result;
4316
4317 result = MLX5_MKEY_MASK_LEN |
4318 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004319 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004320
4321 return cpu_to_be64(result);
4322}
4323
Artemy Kovalyov31616252017-01-02 11:37:42 +02004324static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02004325{
4326 u64 result;
4327
Artemy Kovalyov31616252017-01-02 11:37:42 +02004328 result = MLX5_MKEY_MASK_LR |
4329 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004330 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004331 MLX5_MKEY_MASK_RW;
4332
4333 if (atomic)
4334 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004335
4336 return cpu_to_be64(result);
4337}
4338
4339static __be64 get_umr_update_pd_mask(void)
4340{
4341 u64 result;
4342
Artemy Kovalyov31616252017-01-02 11:37:42 +02004343 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004344
4345 return cpu_to_be64(result);
4346}
4347
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004348static int umr_check_mkey_mask(struct mlx5_ib_dev *dev, u64 mask)
4349{
4350 if ((mask & MLX5_MKEY_MASK_PAGE_SIZE &&
4351 MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) ||
4352 (mask & MLX5_MKEY_MASK_A &&
4353 MLX5_CAP_GEN(dev->mdev, umr_modify_atomic_disabled)))
4354 return -EPERM;
4355 return 0;
4356}
4357
4358static int set_reg_umr_segment(struct mlx5_ib_dev *dev,
4359 struct mlx5_wqe_umr_ctrl_seg *umr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004360 const struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004361{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004362 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03004363
4364 memset(umr, 0, sizeof(*umr));
4365
Yishai Hadas6a053952019-07-23 09:57:25 +03004366 if (!umrwr->ignore_free_state) {
4367 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
4368 /* fail if free */
4369 umr->flags = MLX5_UMR_CHECK_FREE;
4370 else
4371 /* fail if not free */
4372 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
4373 }
Haggai Eran968e78d2014-12-11 17:04:11 +02004374
Artemy Kovalyov31616252017-01-02 11:37:42 +02004375 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
4376 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
4377 u64 offset = get_xlt_octo(umrwr->offset);
4378
4379 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
4380 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
4381 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004382 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02004383 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
4384 umr->mkey_mask |= get_umr_update_translation_mask();
4385 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
4386 umr->mkey_mask |= get_umr_update_access_mask(atomic);
4387 umr->mkey_mask |= get_umr_update_pd_mask();
4388 }
4389 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
4390 umr->mkey_mask |= get_umr_enable_mr_mask();
4391 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
4392 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03004393
4394 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02004395 umr->flags |= MLX5_UMR_INLINE;
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004396
4397 return umr_check_mkey_mask(dev, be64_to_cpu(umr->mkey_mask));
Eli Cohene126ba92013-07-07 17:25:49 +03004398}
4399
4400static u8 get_umr_flags(int acc)
4401{
4402 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
4403 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
4404 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
4405 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02004406 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004407}
4408
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004409static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
4410 struct mlx5_ib_mr *mr,
4411 u32 key, int access)
4412{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004413 int ndescs = ALIGN(mr->ndescs + mr->meta_ndescs, 8) >> 1;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004414
4415 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004416
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004417 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004418 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004419 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004420 /* KLMs take twice the size of MTTs */
4421 ndescs *= 2;
4422
4423 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004424 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
4425 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
4426 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
4427 seg->len = cpu_to_be64(mr->ibmr.length);
4428 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004429}
4430
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004431static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03004432{
4433 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004434 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004435}
4436
Bart Van Asschef696bf62018-07-18 09:25:14 -07004437static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg,
4438 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004439{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004440 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004441
Eli Cohene126ba92013-07-07 17:25:49 +03004442 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02004443 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02004444 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004445
Haggai Eran968e78d2014-12-11 17:04:11 +02004446 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004447 if (umrwr->pd)
4448 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
4449 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
4450 !umrwr->length)
4451 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
4452
4453 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004454 seg->len = cpu_to_be64(umrwr->length);
4455 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03004456 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02004457 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03004458}
4459
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004460static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
4461 struct mlx5_ib_mr *mr,
4462 struct mlx5_ib_pd *pd)
4463{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004464 int bcount = mr->desc_size * (mr->ndescs + mr->meta_ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004465
4466 dseg->addr = cpu_to_be64(mr->desc_map);
4467 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
4468 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
4469}
4470
Bart Van Asschef696bf62018-07-18 09:25:14 -07004471static __be32 send_ieth(const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004472{
4473 switch (wr->opcode) {
4474 case IB_WR_SEND_WITH_IMM:
4475 case IB_WR_RDMA_WRITE_WITH_IMM:
4476 return wr->ex.imm_data;
4477
4478 case IB_WR_SEND_WITH_INV:
4479 return cpu_to_be32(wr->ex.invalidate_rkey);
4480
4481 default:
4482 return 0;
4483 }
4484}
4485
4486static u8 calc_sig(void *wqe, int size)
4487{
4488 u8 *p = wqe;
4489 u8 res = 0;
4490 int i;
4491
4492 for (i = 0; i < size; i++)
4493 res ^= p[i];
4494
4495 return ~res;
4496}
4497
4498static u8 wq_sig(void *wqe)
4499{
4500 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
4501}
4502
Bart Van Asschef696bf62018-07-18 09:25:14 -07004503static int set_data_inl_seg(struct mlx5_ib_qp *qp, const struct ib_send_wr *wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004504 void **wqe, int *wqe_sz, void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004505{
4506 struct mlx5_wqe_inline_seg *seg;
Guy Levi34f4c952018-11-26 08:15:50 +02004507 size_t offset;
Eli Cohene126ba92013-07-07 17:25:49 +03004508 int inl = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004509 int i;
4510
Guy Levi34f4c952018-11-26 08:15:50 +02004511 seg = *wqe;
4512 *wqe += sizeof(*seg);
4513 offset = sizeof(*seg);
4514
Eli Cohene126ba92013-07-07 17:25:49 +03004515 for (i = 0; i < wr->num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02004516 size_t len = wr->sg_list[i].length;
4517 void *addr = (void *)(unsigned long)(wr->sg_list[i].addr);
4518
Eli Cohene126ba92013-07-07 17:25:49 +03004519 inl += len;
4520
4521 if (unlikely(inl > qp->max_inline_data))
4522 return -ENOMEM;
4523
Guy Levi34f4c952018-11-26 08:15:50 +02004524 while (likely(len)) {
4525 size_t leftlen;
4526 size_t copysz;
4527
4528 handle_post_send_edge(&qp->sq, wqe,
4529 *wqe_sz + (offset >> 4),
4530 cur_edge);
4531
4532 leftlen = *cur_edge - *wqe;
4533 copysz = min_t(size_t, leftlen, len);
4534
4535 memcpy(*wqe, addr, copysz);
4536 len -= copysz;
4537 addr += copysz;
4538 *wqe += copysz;
4539 offset += copysz;
Eli Cohene126ba92013-07-07 17:25:49 +03004540 }
Eli Cohene126ba92013-07-07 17:25:49 +03004541 }
4542
4543 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
4544
Guy Levi34f4c952018-11-26 08:15:50 +02004545 *wqe_sz += ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004546
4547 return 0;
4548}
4549
Sagi Grimberge6631812014-02-23 14:19:11 +02004550static u16 prot_field_size(enum ib_signature_type type)
4551{
4552 switch (type) {
4553 case IB_SIG_TYPE_T10_DIF:
4554 return MLX5_DIF_SIZE;
4555 default:
4556 return 0;
4557 }
4558}
4559
4560static u8 bs_selector(int block_size)
4561{
4562 switch (block_size) {
4563 case 512: return 0x1;
4564 case 520: return 0x2;
4565 case 4096: return 0x3;
4566 case 4160: return 0x4;
4567 case 1073741824: return 0x5;
4568 default: return 0;
4569 }
4570}
4571
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004572static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
4573 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02004574{
Sagi Grimberg142537f2014-08-13 19:54:32 +03004575 /* Valid inline section and allow BSF refresh */
4576 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
4577 MLX5_BSF_REFRESH_DIF);
4578 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
4579 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004580 /* repeating block */
4581 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
4582 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
4583 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004584
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004585 if (domain->sig.dif.ref_remap)
4586 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02004587
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004588 if (domain->sig.dif.app_escape) {
4589 if (domain->sig.dif.ref_escape)
4590 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
4591 else
4592 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02004593 }
4594
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004595 inl->dif_app_bitmask_check =
4596 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02004597}
4598
4599static int mlx5_set_bsf(struct ib_mr *sig_mr,
4600 struct ib_sig_attrs *sig_attrs,
4601 struct mlx5_bsf *bsf, u32 data_size)
4602{
4603 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
4604 struct mlx5_bsf_basic *basic = &bsf->basic;
4605 struct ib_sig_domain *mem = &sig_attrs->mem;
4606 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02004607
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004608 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02004609
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004610 /* Basic + Extended + Inline */
4611 basic->bsf_size_sbs = 1 << 7;
4612 /* Input domain check byte mask */
4613 basic->check_byte_mask = sig_attrs->check_mask;
4614 basic->raw_data_size = cpu_to_be32(data_size);
4615
4616 /* Memory domain */
4617 switch (sig_attrs->mem.sig_type) {
4618 case IB_SIG_TYPE_NONE:
4619 break;
4620 case IB_SIG_TYPE_T10_DIF:
4621 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
4622 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
4623 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
4624 break;
4625 default:
4626 return -EINVAL;
4627 }
4628
4629 /* Wire domain */
4630 switch (sig_attrs->wire.sig_type) {
4631 case IB_SIG_TYPE_NONE:
4632 break;
4633 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02004634 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004635 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004636 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03004637 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02004638 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004639 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004640 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004641 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004642 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004643 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02004644 } else
4645 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
4646
Sagi Grimberg142537f2014-08-13 19:54:32 +03004647 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004648 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02004649 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004650 default:
4651 return -EINVAL;
4652 }
4653
4654 return 0;
4655}
4656
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004657static int set_sig_data_segment(const struct ib_send_wr *send_wr,
4658 struct ib_mr *sig_mr,
4659 struct ib_sig_attrs *sig_attrs,
4660 struct mlx5_ib_qp *qp, void **seg, int *size,
4661 void **cur_edge)
Sagi Grimberge6631812014-02-23 14:19:11 +02004662{
Sagi Grimberge6631812014-02-23 14:19:11 +02004663 struct mlx5_bsf *bsf;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004664 u32 data_len;
4665 u32 data_key;
4666 u64 data_va;
4667 u32 prot_len = 0;
4668 u32 prot_key = 0;
4669 u64 prot_va = 0;
4670 bool prot = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004671 int ret;
4672 int wqe_size;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004673 struct mlx5_ib_mr *mr = to_mmr(sig_mr);
4674 struct mlx5_ib_mr *pi_mr = mr->pi_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004675
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004676 data_len = pi_mr->data_length;
4677 data_key = pi_mr->ibmr.lkey;
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03004678 data_va = pi_mr->data_iova;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004679 if (pi_mr->meta_ndescs) {
4680 prot_len = pi_mr->meta_length;
4681 prot_key = pi_mr->ibmr.lkey;
Israel Rukshinde0ae952019-06-11 18:52:55 +03004682 prot_va = pi_mr->pi_iova;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004683 prot = true;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004684 }
4685
4686 if (!prot || (data_key == prot_key && data_va == prot_va &&
4687 data_len == prot_len)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004688 /**
4689 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004690 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02004691 * So need construct:
4692 * ------------------
4693 * | data_klm |
4694 * ------------------
4695 * | BSF |
4696 * ------------------
4697 **/
4698 struct mlx5_klm *data_klm = *seg;
4699
4700 data_klm->bcount = cpu_to_be32(data_len);
4701 data_klm->key = cpu_to_be32(data_key);
4702 data_klm->va = cpu_to_be64(data_va);
4703 wqe_size = ALIGN(sizeof(*data_klm), 64);
4704 } else {
4705 /**
4706 * Source domain contains signature information
4707 * So need construct a strided block format:
4708 * ---------------------------
4709 * | stride_block_ctrl |
4710 * ---------------------------
4711 * | data_klm |
4712 * ---------------------------
4713 * | prot_klm |
4714 * ---------------------------
4715 * | BSF |
4716 * ---------------------------
4717 **/
4718 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
4719 struct mlx5_stride_block_entry *data_sentry;
4720 struct mlx5_stride_block_entry *prot_sentry;
Sagi Grimberge6631812014-02-23 14:19:11 +02004721 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
4722 int prot_size;
4723
4724 sblock_ctrl = *seg;
4725 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
4726 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
4727
4728 prot_size = prot_field_size(sig_attrs->mem.sig_type);
4729 if (!prot_size) {
4730 pr_err("Bad block size given: %u\n", block_size);
4731 return -EINVAL;
4732 }
4733 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
4734 prot_size);
4735 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
4736 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
4737 sblock_ctrl->num_entries = cpu_to_be16(2);
4738
4739 data_sentry->bcount = cpu_to_be16(block_size);
4740 data_sentry->key = cpu_to_be32(data_key);
4741 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004742 data_sentry->stride = cpu_to_be16(block_size);
4743
Sagi Grimberge6631812014-02-23 14:19:11 +02004744 prot_sentry->bcount = cpu_to_be16(prot_size);
4745 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004746 prot_sentry->va = cpu_to_be64(prot_va);
4747 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004748
Sagi Grimberge6631812014-02-23 14:19:11 +02004749 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
4750 sizeof(*prot_sentry), 64);
4751 }
4752
4753 *seg += wqe_size;
4754 *size += wqe_size / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004755 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004756
4757 bsf = *seg;
4758 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
4759 if (ret)
4760 return -EINVAL;
4761
4762 *seg += sizeof(*bsf);
4763 *size += sizeof(*bsf) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004764 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004765
4766 return 0;
4767}
4768
4769static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004770 struct ib_mr *sig_mr, int access_flags,
4771 u32 size, u32 length, u32 pdn)
Sagi Grimberge6631812014-02-23 14:19:11 +02004772{
Sagi Grimberge6631812014-02-23 14:19:11 +02004773 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004774 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004775
4776 memset(seg, 0, sizeof(*seg));
4777
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004778 seg->flags = get_umr_flags(access_flags) | MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004779 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004780 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004781 MLX5_MKEY_BSF_EN | pdn);
4782 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004783 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004784 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4785}
4786
4787static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004788 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004789{
4790 memset(umr, 0, sizeof(*umr));
4791
4792 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004793 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004794 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4795 umr->mkey_mask = sig_mkey_mask();
4796}
4797
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004798static int set_pi_umr_wr(const struct ib_send_wr *send_wr,
4799 struct mlx5_ib_qp *qp, void **seg, int *size,
4800 void **cur_edge)
4801{
4802 const struct ib_reg_wr *wr = reg_wr(send_wr);
4803 struct mlx5_ib_mr *sig_mr = to_mmr(wr->mr);
4804 struct mlx5_ib_mr *pi_mr = sig_mr->pi_mr;
4805 struct ib_sig_attrs *sig_attrs = sig_mr->ibmr.sig_attrs;
4806 u32 pdn = get_pd(qp)->pdn;
4807 u32 xlt_size;
4808 int region_len, ret;
4809
4810 if (unlikely(send_wr->num_sge != 0) ||
4811 unlikely(wr->access & IB_ACCESS_REMOTE_ATOMIC) ||
Max Gurtovoy185eddc2019-06-11 18:52:51 +03004812 unlikely(!sig_mr->sig) || unlikely(!qp->ibqp.integrity_en) ||
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004813 unlikely(!sig_mr->sig->sig_status_checked))
4814 return -EINVAL;
4815
4816 /* length of the protected region, data + protection */
4817 region_len = pi_mr->ibmr.length;
4818
4819 /**
4820 * KLM octoword size - if protection was provided
4821 * then we use strided block format (3 octowords),
4822 * else we use single KLM (1 octoword)
4823 **/
4824 if (sig_attrs->mem.sig_type != IB_SIG_TYPE_NONE)
4825 xlt_size = 0x30;
4826 else
4827 xlt_size = sizeof(struct mlx5_klm);
4828
4829 set_sig_umr_segment(*seg, xlt_size);
4830 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4831 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4832 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4833
4834 set_sig_mkey_segment(*seg, wr->mr, wr->access, xlt_size, region_len,
4835 pdn);
4836 *seg += sizeof(struct mlx5_mkey_seg);
4837 *size += sizeof(struct mlx5_mkey_seg) / 16;
4838 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4839
4840 ret = set_sig_data_segment(send_wr, wr->mr, sig_attrs, qp, seg, size,
4841 cur_edge);
4842 if (ret)
4843 return ret;
4844
4845 sig_mr->sig->sig_status_checked = false;
4846 return 0;
4847}
Sagi Grimberge6631812014-02-23 14:19:11 +02004848
Sagi Grimberge6631812014-02-23 14:19:11 +02004849static int set_psv_wr(struct ib_sig_domain *domain,
4850 u32 psv_idx, void **seg, int *size)
4851{
4852 struct mlx5_seg_set_psv *psv_seg = *seg;
4853
4854 memset(psv_seg, 0, sizeof(*psv_seg));
4855 psv_seg->psv_num = cpu_to_be32(psv_idx);
4856 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004857 case IB_SIG_TYPE_NONE:
4858 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004859 case IB_SIG_TYPE_T10_DIF:
4860 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4861 domain->sig.dif.app_tag);
4862 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004863 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004864 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004865 pr_err("Bad signature type (%d) is given.\n",
4866 domain->sig_type);
4867 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004868 }
4869
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004870 *seg += sizeof(*psv_seg);
4871 *size += sizeof(*psv_seg) / 16;
4872
Sagi Grimberge6631812014-02-23 14:19:11 +02004873 return 0;
4874}
4875
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004876static int set_reg_wr(struct mlx5_ib_qp *qp,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004877 const struct ib_reg_wr *wr,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004878 void **seg, int *size, void **cur_edge,
4879 bool check_not_free)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004880{
4881 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4882 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
Moni Shoua841b07f2019-08-15 11:38:34 +03004883 struct mlx5_ib_dev *dev = to_mdev(pd->ibpd.device);
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004884 int mr_list_size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Idan Burstein064e5262018-05-02 13:16:39 +03004885 bool umr_inline = mr_list_size <= MLX5_IB_SQ_UMR_INLINE_THRESHOLD;
Moni Shoua841b07f2019-08-15 11:38:34 +03004886 bool atomic = wr->access & IB_ACCESS_REMOTE_ATOMIC;
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004887 u8 flags = 0;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004888
Michael Guralnikd6de0bb2020-01-08 20:05:40 +02004889 if (!mlx5_ib_can_use_umr(dev, atomic, wr->access)) {
Moni Shoua841b07f2019-08-15 11:38:34 +03004890 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4891 "Fast update of %s for MR is disabled\n",
4892 (MLX5_CAP_GEN(dev->mdev,
4893 umr_modify_entity_size_disabled)) ?
4894 "entity size" :
4895 "atomic access");
4896 return -EINVAL;
4897 }
4898
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004899 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4900 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4901 "Invalid IB_SEND_INLINE send flag\n");
4902 return -EINVAL;
4903 }
4904
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004905 if (check_not_free)
4906 flags |= MLX5_UMR_CHECK_NOT_FREE;
4907 if (umr_inline)
4908 flags |= MLX5_UMR_INLINE;
4909
Moni Shoua841b07f2019-08-15 11:38:34 +03004910 set_reg_umr_seg(*seg, mr, flags, atomic);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004911 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4912 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004913 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004914
4915 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4916 *seg += sizeof(struct mlx5_mkey_seg);
4917 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004918 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004919
Idan Burstein064e5262018-05-02 13:16:39 +03004920 if (umr_inline) {
Guy Levi34f4c952018-11-26 08:15:50 +02004921 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, mr->descs,
4922 mr_list_size);
4923 *size = ALIGN(*size, MLX5_SEND_WQE_BB >> 4);
Idan Burstein064e5262018-05-02 13:16:39 +03004924 } else {
4925 set_reg_data_seg(*seg, mr, pd);
4926 *seg += sizeof(struct mlx5_wqe_data_seg);
4927 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4928 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004929 return 0;
4930}
4931
Guy Levi34f4c952018-11-26 08:15:50 +02004932static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size,
4933 void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004934{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004935 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004936 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4937 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004938 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004939 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004940 *seg += sizeof(struct mlx5_mkey_seg);
4941 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004942 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03004943}
4944
Guy Levi34f4c952018-11-26 08:15:50 +02004945static void dump_wqe(struct mlx5_ib_qp *qp, u32 idx, int size_16)
Eli Cohene126ba92013-07-07 17:25:49 +03004946{
4947 __be32 *p = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004948 int i, j;
4949
Guy Levi34f4c952018-11-26 08:15:50 +02004950 pr_debug("dump WQE index %u:\n", idx);
Eli Cohene126ba92013-07-07 17:25:49 +03004951 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4952 if ((i & 0xf) == 0) {
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004953 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, idx);
Guy Levi34f4c952018-11-26 08:15:50 +02004954 pr_debug("WQBB at %p:\n", (void *)p);
Eli Cohene126ba92013-07-07 17:25:49 +03004955 j = 0;
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004956 idx = (idx + 1) & (qp->sq.wqe_cnt - 1);
Eli Cohene126ba92013-07-07 17:25:49 +03004957 }
4958 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4959 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4960 be32_to_cpu(p[j + 3]));
4961 }
4962}
4963
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004964static int __begin_wqe(struct mlx5_ib_qp *qp, void **seg,
Guy Levi34f4c952018-11-26 08:15:50 +02004965 struct mlx5_wqe_ctrl_seg **ctrl,
4966 const struct ib_send_wr *wr, unsigned int *idx,
4967 int *size, void **cur_edge, int nreq,
4968 bool send_signaled, bool solicited)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004969{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004970 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4971 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004972
4973 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
Guy Levi34f4c952018-11-26 08:15:50 +02004974 *seg = mlx5_frag_buf_get_wqe(&qp->sq.fbc, *idx);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004975 *ctrl = *seg;
4976 *(uint32_t *)(*seg + 8) = 0;
4977 (*ctrl)->imm = send_ieth(wr);
4978 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004979 (send_signaled ? MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4980 (solicited ? MLX5_WQE_CTRL_SOLICITED : 0);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004981
4982 *seg += sizeof(**ctrl);
4983 *size = sizeof(**ctrl) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004984 *cur_edge = qp->sq.cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004985
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004986 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004987}
4988
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004989static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4990 struct mlx5_wqe_ctrl_seg **ctrl,
4991 const struct ib_send_wr *wr, unsigned *idx,
Guy Levi34f4c952018-11-26 08:15:50 +02004992 int *size, void **cur_edge, int nreq)
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004993{
Guy Levi34f4c952018-11-26 08:15:50 +02004994 return __begin_wqe(qp, seg, ctrl, wr, idx, size, cur_edge, nreq,
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004995 wr->send_flags & IB_SEND_SIGNALED,
4996 wr->send_flags & IB_SEND_SOLICITED);
4997}
4998
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004999static void finish_wqe(struct mlx5_ib_qp *qp,
5000 struct mlx5_wqe_ctrl_seg *ctrl,
Guy Levi34f4c952018-11-26 08:15:50 +02005001 void *seg, u8 size, void *cur_edge,
5002 unsigned int idx, u64 wr_id, int nreq, u8 fence,
5003 u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005004{
5005 u8 opmod = 0;
5006
5007 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
5008 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02005009 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005010 ctrl->fm_ce_se |= fence;
Leon Romanovskyc95e6d52020-04-27 18:46:15 +03005011 if (unlikely(qp->flags_en & MLX5_QP_FLAG_SIGNATURE))
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005012 ctrl->signature = wq_sig(ctrl);
5013
5014 qp->sq.wrid[idx] = wr_id;
5015 qp->sq.w_list[idx].opcode = mlx5_opcode;
5016 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
5017 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
5018 qp->sq.w_list[idx].next = qp->sq.cur_post;
Guy Levi34f4c952018-11-26 08:15:50 +02005019
5020 /* We save the edge which was possibly updated during the WQE
5021 * construction, into SQ's cache.
5022 */
5023 seg = PTR_ALIGN(seg, MLX5_SEND_WQE_BB);
5024 qp->sq.cur_edge = (unlikely(seg == cur_edge)) ?
5025 get_sq_edge(&qp->sq, qp->sq.cur_post &
5026 (qp->sq.wqe_cnt - 1)) :
5027 cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005028}
5029
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005030static int _mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
5031 const struct ib_send_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03005032{
5033 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
5034 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03005035 struct mlx5_core_dev *mdev = dev->mdev;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005036 struct ib_reg_wr reg_pi_wr;
Haggai Erand16e91d2016-02-29 15:45:05 +02005037 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02005038 struct mlx5_ib_mr *mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005039 struct mlx5_ib_mr *pi_mr;
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005040 struct mlx5_ib_mr pa_pi_mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005041 struct ib_sig_attrs *sig_attrs;
Eli Cohene126ba92013-07-07 17:25:49 +03005042 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02005043 struct mlx5_bf *bf;
Guy Levi34f4c952018-11-26 08:15:50 +02005044 void *cur_edge;
Eli Cohene126ba92013-07-07 17:25:49 +03005045 int uninitialized_var(size);
Eli Cohene126ba92013-07-07 17:25:49 +03005046 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03005047 unsigned idx;
5048 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03005049 int num_sge;
5050 void *seg;
5051 int nreq;
5052 int i;
5053 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03005054 u8 fence;
5055
Parav Pandit6c755202018-08-28 14:45:29 +03005056 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5057 !drain)) {
5058 *bad_wr = wr;
5059 return -EIO;
5060 }
5061
Haggai Erand16e91d2016-02-29 15:45:05 +02005062 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5063 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
5064
5065 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005066 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02005067
Eli Cohene126ba92013-07-07 17:25:49 +03005068 spin_lock_irqsave(&qp->sq.lock, flags);
5069
5070 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04005071 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03005072 mlx5_ib_warn(dev, "\n");
5073 err = -EINVAL;
5074 *bad_wr = wr;
5075 goto out;
5076 }
5077
Eli Cohene126ba92013-07-07 17:25:49 +03005078 num_sge = wr->num_sge;
5079 if (unlikely(num_sge > qp->sq.max_gs)) {
5080 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03005081 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03005082 *bad_wr = wr;
5083 goto out;
5084 }
5085
Guy Levi34f4c952018-11-26 08:15:50 +02005086 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, &cur_edge,
5087 nreq);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005088 if (err) {
5089 mlx5_ib_warn(dev, "\n");
5090 err = -ENOMEM;
5091 *bad_wr = wr;
5092 goto out;
5093 }
Eli Cohene126ba92013-07-07 17:25:49 +03005094
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005095 if (wr->opcode == IB_WR_REG_MR ||
5096 wr->opcode == IB_WR_REG_MR_INTEGRITY) {
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005097 fence = dev->umr_fence;
5098 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Majd Dibbiny074fca32018-11-05 08:07:37 +02005099 } else {
5100 if (wr->send_flags & IB_SEND_FENCE) {
5101 if (qp->next_fence)
5102 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
5103 else
5104 fence = MLX5_FENCE_MODE_FENCE;
5105 } else {
5106 fence = qp->next_fence;
5107 }
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005108 }
5109
Eli Cohene126ba92013-07-07 17:25:49 +03005110 switch (ibqp->qp_type) {
5111 case IB_QPT_XRC_INI:
5112 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03005113 seg += sizeof(*xrc);
5114 size += sizeof(*xrc) / 16;
5115 /* fall through */
5116 case IB_QPT_RC:
5117 switch (wr->opcode) {
5118 case IB_WR_RDMA_READ:
5119 case IB_WR_RDMA_WRITE:
5120 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005121 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5122 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005123 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005124 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5125 break;
5126
5127 case IB_WR_ATOMIC_CMP_AND_SWP:
5128 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03005129 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03005130 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
5131 err = -ENOSYS;
5132 *bad_wr = wr;
5133 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005134
5135 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03005136 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
5137 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Guy Levi34f4c952018-11-26 08:15:50 +02005138 set_linv_wr(qp, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005139 num_sge = 0;
5140 break;
5141
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005142 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005143 qp->sq.wr_data[idx] = IB_WR_REG_MR;
5144 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
Guy Levi34f4c952018-11-26 08:15:50 +02005145 err = set_reg_wr(qp, reg_wr(wr), &seg, &size,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03005146 &cur_edge, true);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005147 if (err) {
5148 *bad_wr = wr;
5149 goto out;
5150 }
5151 num_sge = 0;
5152 break;
5153
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005154 case IB_WR_REG_MR_INTEGRITY:
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005155 qp->sq.wr_data[idx] = IB_WR_REG_MR_INTEGRITY;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005156
5157 mr = to_mmr(reg_wr(wr)->mr);
5158 pi_mr = mr->pi_mr;
5159
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005160 if (pi_mr) {
5161 memset(&reg_pi_wr, 0,
5162 sizeof(struct ib_reg_wr));
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005163
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005164 reg_pi_wr.mr = &pi_mr->ibmr;
5165 reg_pi_wr.access = reg_wr(wr)->access;
5166 reg_pi_wr.key = pi_mr->ibmr.rkey;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005167
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005168 ctrl->imm = cpu_to_be32(reg_pi_wr.key);
5169 /* UMR for data + prot registration */
5170 err = set_reg_wr(qp, &reg_pi_wr, &seg,
5171 &size, &cur_edge,
5172 false);
5173 if (err) {
5174 *bad_wr = wr;
5175 goto out;
5176 }
5177 finish_wqe(qp, ctrl, seg, size,
5178 cur_edge, idx, wr->wr_id,
5179 nreq, fence,
5180 MLX5_OPCODE_UMR);
5181
5182 err = begin_wqe(qp, &seg, &ctrl, wr,
5183 &idx, &size, &cur_edge,
5184 nreq);
5185 if (err) {
5186 mlx5_ib_warn(dev, "\n");
5187 err = -ENOMEM;
5188 *bad_wr = wr;
5189 goto out;
5190 }
5191 } else {
5192 memset(&pa_pi_mr, 0,
5193 sizeof(struct mlx5_ib_mr));
5194 /* No UMR, use local_dma_lkey */
5195 pa_pi_mr.ibmr.lkey =
5196 mr->ibmr.pd->local_dma_lkey;
5197
5198 pa_pi_mr.ndescs = mr->ndescs;
5199 pa_pi_mr.data_length = mr->data_length;
5200 pa_pi_mr.data_iova = mr->data_iova;
5201 if (mr->meta_ndescs) {
5202 pa_pi_mr.meta_ndescs =
5203 mr->meta_ndescs;
5204 pa_pi_mr.meta_length =
5205 mr->meta_length;
5206 pa_pi_mr.pi_iova = mr->pi_iova;
5207 }
5208
5209 pa_pi_mr.ibmr.length = mr->ibmr.length;
5210 mr->pi_mr = &pa_pi_mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005211 }
5212 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
5213 /* UMR for sig MR */
5214 err = set_pi_umr_wr(wr, qp, &seg, &size,
5215 &cur_edge);
5216 if (err) {
5217 mlx5_ib_warn(dev, "\n");
5218 *bad_wr = wr;
5219 goto out;
5220 }
5221 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5222 wr->wr_id, nreq, fence,
5223 MLX5_OPCODE_UMR);
5224
5225 /*
5226 * SET_PSV WQEs are not signaled and solicited
5227 * on error
5228 */
5229 sig_attrs = mr->ibmr.sig_attrs;
5230 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5231 &size, &cur_edge, nreq, false,
5232 true);
5233 if (err) {
5234 mlx5_ib_warn(dev, "\n");
5235 err = -ENOMEM;
5236 *bad_wr = wr;
5237 goto out;
5238 }
5239 err = set_psv_wr(&sig_attrs->mem,
5240 mr->sig->psv_memory.psv_idx,
5241 &seg, &size);
5242 if (err) {
5243 mlx5_ib_warn(dev, "\n");
5244 *bad_wr = wr;
5245 goto out;
5246 }
5247 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5248 wr->wr_id, nreq, next_fence,
5249 MLX5_OPCODE_SET_PSV);
5250
5251 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5252 &size, &cur_edge, nreq, false,
5253 true);
5254 if (err) {
5255 mlx5_ib_warn(dev, "\n");
5256 err = -ENOMEM;
5257 *bad_wr = wr;
5258 goto out;
5259 }
5260 err = set_psv_wr(&sig_attrs->wire,
5261 mr->sig->psv_wire.psv_idx,
5262 &seg, &size);
5263 if (err) {
5264 mlx5_ib_warn(dev, "\n");
5265 *bad_wr = wr;
5266 goto out;
5267 }
5268 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5269 wr->wr_id, nreq, next_fence,
5270 MLX5_OPCODE_SET_PSV);
5271
5272 qp->next_fence =
5273 MLX5_FENCE_MODE_INITIATOR_SMALL;
5274 num_sge = 0;
5275 goto skip_psv;
5276
Eli Cohene126ba92013-07-07 17:25:49 +03005277 default:
5278 break;
5279 }
5280 break;
5281
5282 case IB_QPT_UC:
5283 switch (wr->opcode) {
5284 case IB_WR_RDMA_WRITE:
5285 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005286 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5287 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03005288 seg += sizeof(struct mlx5_wqe_raddr_seg);
5289 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5290 break;
5291
5292 default:
5293 break;
5294 }
5295 break;
5296
Eli Cohene126ba92013-07-07 17:25:49 +03005297 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02005298 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
5299 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
5300 err = -EPERM;
5301 *bad_wr = wr;
5302 goto out;
5303 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07005304 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02005305 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03005306 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005307 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005308 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005309 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
5310
Eli Cohene126ba92013-07-07 17:25:49 +03005311 break;
Erez Shitritf0313962016-02-21 16:27:17 +02005312 case IB_QPT_UD:
5313 set_datagram_seg(seg, wr);
5314 seg += sizeof(struct mlx5_wqe_datagram_seg);
5315 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005316 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005317
5318 /* handle qp that supports ud offload */
5319 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
5320 struct mlx5_wqe_eth_pad *pad;
5321
5322 pad = seg;
5323 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
5324 seg += sizeof(struct mlx5_wqe_eth_pad);
5325 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005326 set_eth_seg(wr, qp, &seg, &size, &cur_edge);
5327 handle_post_send_edge(&qp->sq, &seg, size,
5328 &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005329 }
5330 break;
Eli Cohene126ba92013-07-07 17:25:49 +03005331 case MLX5_IB_QPT_REG_UMR:
5332 if (wr->opcode != MLX5_IB_WR_UMR) {
5333 err = -EINVAL;
5334 mlx5_ib_warn(dev, "bad opcode\n");
5335 goto out;
5336 }
5337 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005338 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02005339 err = set_reg_umr_segment(dev, seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
5340 if (unlikely(err))
5341 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005342 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
5343 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005344 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005345 set_reg_mkey_segment(seg, wr);
5346 seg += sizeof(struct mlx5_mkey_seg);
5347 size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005348 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005349 break;
5350
5351 default:
5352 break;
5353 }
5354
5355 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
Guy Levi34f4c952018-11-26 08:15:50 +02005356 err = set_data_inl_seg(qp, wr, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005357 if (unlikely(err)) {
5358 mlx5_ib_warn(dev, "\n");
5359 *bad_wr = wr;
5360 goto out;
5361 }
Eli Cohene126ba92013-07-07 17:25:49 +03005362 } else {
Eli Cohene126ba92013-07-07 17:25:49 +03005363 for (i = 0; i < num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02005364 handle_post_send_edge(&qp->sq, &seg, size,
5365 &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005366 if (likely(wr->sg_list[i].length)) {
Guy Levi34f4c952018-11-26 08:15:50 +02005367 set_data_ptr_seg
5368 ((struct mlx5_wqe_data_seg *)seg,
5369 wr->sg_list + i);
Eli Cohene126ba92013-07-07 17:25:49 +03005370 size += sizeof(struct mlx5_wqe_data_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005371 seg += sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005372 }
5373 }
5374 }
5375
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005376 qp->next_fence = next_fence;
Guy Levi34f4c952018-11-26 08:15:50 +02005377 finish_wqe(qp, ctrl, seg, size, cur_edge, idx, wr->wr_id, nreq,
5378 fence, mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02005379skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03005380 if (0)
5381 dump_wqe(qp, idx, size);
5382 }
5383
5384out:
5385 if (likely(nreq)) {
5386 qp->sq.head += nreq;
5387
5388 /* Make sure that descriptors are written before
5389 * updating doorbell record and ringing the doorbell
5390 */
5391 wmb();
5392
5393 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
5394
Eli Cohenada388f2014-01-14 17:45:16 +02005395 /* Make sure doorbell record is visible to the HCA before
5396 * we hit doorbell */
5397 wmb();
5398
Maxim Mikityanskiybbf29f62019-03-29 15:37:52 -07005399 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005400 /* Make sure doorbells don't leak out of SQ spinlock
5401 * and reach the HCA out of order.
5402 */
Eli Cohene126ba92013-07-07 17:25:49 +03005403 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03005404 }
5405
5406 spin_unlock_irqrestore(&qp->sq.lock, flags);
5407
5408 return err;
5409}
5410
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005411int mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
5412 const struct ib_send_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005413{
5414 return _mlx5_ib_post_send(ibqp, wr, bad_wr, false);
5415}
5416
Eli Cohene126ba92013-07-07 17:25:49 +03005417static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
5418{
5419 sig->signature = calc_sig(sig, size);
5420}
5421
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005422static int _mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5423 const struct ib_recv_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03005424{
5425 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5426 struct mlx5_wqe_data_seg *scat;
5427 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03005428 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5429 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005430 unsigned long flags;
5431 int err = 0;
5432 int nreq;
5433 int ind;
5434 int i;
5435
Parav Pandit6c755202018-08-28 14:45:29 +03005436 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5437 !drain)) {
5438 *bad_wr = wr;
5439 return -EIO;
5440 }
5441
Haggai Erand16e91d2016-02-29 15:45:05 +02005442 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5443 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
5444
Eli Cohene126ba92013-07-07 17:25:49 +03005445 spin_lock_irqsave(&qp->rq.lock, flags);
5446
5447 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
5448
5449 for (nreq = 0; wr; nreq++, wr = wr->next) {
5450 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
5451 err = -ENOMEM;
5452 *bad_wr = wr;
5453 goto out;
5454 }
5455
5456 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
5457 err = -EINVAL;
5458 *bad_wr = wr;
5459 goto out;
5460 }
5461
Guy Levi34f4c952018-11-26 08:15:50 +02005462 scat = mlx5_frag_buf_get_wqe(&qp->rq.fbc, ind);
Leon Romanovskyc95e6d52020-04-27 18:46:15 +03005463 if (qp->flags_en & MLX5_QP_FLAG_SIGNATURE)
Eli Cohene126ba92013-07-07 17:25:49 +03005464 scat++;
5465
5466 for (i = 0; i < wr->num_sge; i++)
5467 set_data_ptr_seg(scat + i, wr->sg_list + i);
5468
5469 if (i < qp->rq.max_gs) {
5470 scat[i].byte_count = 0;
5471 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
5472 scat[i].addr = 0;
5473 }
5474
Leon Romanovskyc95e6d52020-04-27 18:46:15 +03005475 if (qp->flags_en & MLX5_QP_FLAG_SIGNATURE) {
Eli Cohene126ba92013-07-07 17:25:49 +03005476 sig = (struct mlx5_rwqe_sig *)scat;
5477 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
5478 }
5479
5480 qp->rq.wrid[ind] = wr->wr_id;
5481
5482 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
5483 }
5484
5485out:
5486 if (likely(nreq)) {
5487 qp->rq.head += nreq;
5488
5489 /* Make sure that descriptors are written before
5490 * doorbell record.
5491 */
5492 wmb();
5493
5494 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
5495 }
5496
5497 spin_unlock_irqrestore(&qp->rq.lock, flags);
5498
5499 return err;
5500}
5501
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005502int mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5503 const struct ib_recv_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005504{
5505 return _mlx5_ib_post_recv(ibqp, wr, bad_wr, false);
5506}
5507
Eli Cohene126ba92013-07-07 17:25:49 +03005508static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
5509{
5510 switch (mlx5_state) {
5511 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
5512 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
5513 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
5514 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
5515 case MLX5_QP_STATE_SQ_DRAINING:
5516 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
5517 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
5518 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
5519 default: return -1;
5520 }
5521}
5522
5523static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
5524{
5525 switch (mlx5_mig_state) {
5526 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
5527 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
5528 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
5529 default: return -1;
5530 }
5531}
5532
5533static int to_ib_qp_access_flags(int mlx5_flags)
5534{
5535 int ib_flags = 0;
5536
5537 if (mlx5_flags & MLX5_QP_BIT_RRE)
5538 ib_flags |= IB_ACCESS_REMOTE_READ;
5539 if (mlx5_flags & MLX5_QP_BIT_RWE)
5540 ib_flags |= IB_ACCESS_REMOTE_WRITE;
5541 if (mlx5_flags & MLX5_QP_BIT_RAE)
5542 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
5543
5544 return ib_flags;
5545}
5546
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005547static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005548 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005549 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03005550{
Eli Cohene126ba92013-07-07 17:25:49 +03005551
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005552 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03005553
Jason Gunthorpee7996a92018-01-29 13:26:40 -07005554 if (!path->port || path->port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03005555 return;
5556
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02005557 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
5558
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005559 rdma_ah_set_port_num(ah_attr, path->port);
5560 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03005561
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005562 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
5563 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
5564 rdma_ah_set_static_rate(ah_attr,
5565 path->static_rate ? path->static_rate - 5 : 0);
5566 if (path->grh_mlid & (1 << 7)) {
5567 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
5568
5569 rdma_ah_set_grh(ah_attr, NULL,
5570 tc_fl & 0xfffff,
5571 path->mgid_index,
5572 path->hop_limit,
5573 (tc_fl >> 20) & 0xff);
5574 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03005575 }
5576}
5577
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005578static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
5579 struct mlx5_ib_sq *sq,
5580 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03005581{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005582 int err;
5583
Eran Ben Elisha28160772017-12-26 15:17:05 +02005584 err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005585 if (err)
5586 goto out;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005587 sq->state = *sq_state;
5588
5589out:
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005590 return err;
5591}
5592
5593static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
5594 struct mlx5_ib_rq *rq,
5595 u8 *rq_state)
5596{
5597 void *out;
5598 void *rqc;
5599 int inlen;
5600 int err;
5601
5602 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005603 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005604 if (!out)
5605 return -ENOMEM;
5606
5607 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
5608 if (err)
5609 goto out;
5610
5611 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
5612 *rq_state = MLX5_GET(rqc, rqc, state);
5613 rq->state = *rq_state;
5614
5615out:
5616 kvfree(out);
5617 return err;
5618}
5619
5620static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
5621 struct mlx5_ib_qp *qp, u8 *qp_state)
5622{
5623 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
5624 [MLX5_RQC_STATE_RST] = {
5625 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5626 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5627 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
5628 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
5629 },
5630 [MLX5_RQC_STATE_RDY] = {
5631 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5632 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5633 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
5634 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
5635 },
5636 [MLX5_RQC_STATE_ERR] = {
5637 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5638 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5639 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
5640 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
5641 },
5642 [MLX5_RQ_STATE_NA] = {
5643 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5644 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5645 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
5646 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
5647 },
5648 };
5649
5650 *qp_state = sqrq_trans[rq_state][sq_state];
5651
5652 if (*qp_state == MLX5_QP_STATE_BAD) {
5653 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
5654 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
5655 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
5656 return -EINVAL;
5657 }
5658
5659 if (*qp_state == MLX5_QP_STATE)
5660 *qp_state = qp->state;
5661
5662 return 0;
5663}
5664
5665static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
5666 struct mlx5_ib_qp *qp,
5667 u8 *raw_packet_qp_state)
5668{
5669 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
5670 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
5671 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
5672 int err;
5673 u8 sq_state = MLX5_SQ_STATE_NA;
5674 u8 rq_state = MLX5_RQ_STATE_NA;
5675
5676 if (qp->sq.wqe_cnt) {
5677 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
5678 if (err)
5679 return err;
5680 }
5681
5682 if (qp->rq.wqe_cnt) {
5683 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
5684 if (err)
5685 return err;
5686 }
5687
5688 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
5689 raw_packet_qp_state);
5690}
5691
5692static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
5693 struct ib_qp_attr *qp_attr)
5694{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005695 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03005696 struct mlx5_qp_context *context;
5697 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005698 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03005699 int err = 0;
5700
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005701 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005702 if (!outb)
5703 return -ENOMEM;
5704
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03005705 err = mlx5_core_qp_query(dev, &qp->trans_qp.base.mqp, outb, outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03005706 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005707 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005708
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005709 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
5710 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
5711
Eli Cohene126ba92013-07-07 17:25:49 +03005712 mlx5_state = be32_to_cpu(context->flags) >> 28;
5713
5714 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03005715 qp_attr->path_mtu = context->mtu_msgmax >> 5;
5716 qp_attr->path_mig_state =
5717 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
5718 qp_attr->qkey = be32_to_cpu(context->qkey);
5719 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
5720 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
5721 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
5722 qp_attr->qp_access_flags =
5723 to_ib_qp_access_flags(be32_to_cpu(context->params2));
5724
5725 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005726 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
5727 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005728 qp_attr->alt_pkey_index =
5729 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005730 qp_attr->alt_port_num =
5731 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03005732 }
5733
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005734 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03005735 qp_attr->port_num = context->pri_path.port;
5736
5737 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
5738 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
5739
5740 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
5741
5742 qp_attr->max_dest_rd_atomic =
5743 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
5744 qp_attr->min_rnr_timer =
5745 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
5746 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
5747 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
5748 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
5749 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005750
5751out:
5752 kfree(outb);
5753 return err;
5754}
5755
Moni Shoua776a3902018-01-02 16:19:33 +02005756static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
5757 struct ib_qp_attr *qp_attr, int qp_attr_mask,
5758 struct ib_qp_init_attr *qp_init_attr)
5759{
5760 struct mlx5_core_dct *dct = &mqp->dct.mdct;
5761 u32 *out;
5762 u32 access_flags = 0;
5763 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
5764 void *dctc;
5765 int err;
5766 int supported_mask = IB_QP_STATE |
5767 IB_QP_ACCESS_FLAGS |
5768 IB_QP_PORT |
5769 IB_QP_MIN_RNR_TIMER |
5770 IB_QP_AV |
5771 IB_QP_PATH_MTU |
5772 IB_QP_PKEY_INDEX;
5773
5774 if (qp_attr_mask & ~supported_mask)
5775 return -EINVAL;
5776 if (mqp->state != IB_QPS_RTR)
5777 return -EINVAL;
5778
5779 out = kzalloc(outlen, GFP_KERNEL);
5780 if (!out)
5781 return -ENOMEM;
5782
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03005783 err = mlx5_core_dct_query(dev, dct, out, outlen);
Moni Shoua776a3902018-01-02 16:19:33 +02005784 if (err)
5785 goto out;
5786
5787 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
5788
5789 if (qp_attr_mask & IB_QP_STATE)
5790 qp_attr->qp_state = IB_QPS_RTR;
5791
5792 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
5793 if (MLX5_GET(dctc, dctc, rre))
5794 access_flags |= IB_ACCESS_REMOTE_READ;
5795 if (MLX5_GET(dctc, dctc, rwe))
5796 access_flags |= IB_ACCESS_REMOTE_WRITE;
5797 if (MLX5_GET(dctc, dctc, rae))
5798 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
5799 qp_attr->qp_access_flags = access_flags;
5800 }
5801
5802 if (qp_attr_mask & IB_QP_PORT)
5803 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
5804 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
5805 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
5806 if (qp_attr_mask & IB_QP_AV) {
5807 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
5808 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
5809 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
5810 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
5811 }
5812 if (qp_attr_mask & IB_QP_PATH_MTU)
5813 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
5814 if (qp_attr_mask & IB_QP_PKEY_INDEX)
5815 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
5816out:
5817 kfree(out);
5818 return err;
5819}
5820
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005821int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5822 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
5823{
5824 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5825 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5826 int err = 0;
5827 u8 raw_packet_qp_state;
5828
Yishai Hadas28d61372016-05-23 15:20:56 +03005829 if (ibqp->rwq_ind_tbl)
5830 return -ENOSYS;
5831
Haggai Erand16e91d2016-02-29 15:45:05 +02005832 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5833 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
5834 qp_init_attr);
5835
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005836 /* Not all of output fields are applicable, make sure to zero them */
5837 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5838 memset(qp_attr, 0, sizeof(*qp_attr));
5839
Moni Shoua776a3902018-01-02 16:19:33 +02005840 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
5841 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
5842 qp_attr_mask, qp_init_attr);
5843
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005844 mutex_lock(&qp->mutex);
5845
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005846 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03005847 qp->flags & IB_QP_CREATE_SOURCE_QPN) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005848 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
5849 if (err)
5850 goto out;
5851 qp->state = raw_packet_qp_state;
5852 qp_attr->port_num = 1;
5853 } else {
5854 err = query_qp_attr(dev, qp, qp_attr);
5855 if (err)
5856 goto out;
5857 }
5858
5859 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03005860 qp_attr->cur_qp_state = qp_attr->qp_state;
5861 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
5862 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
5863
5864 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005865 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005866 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005867 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005868 } else {
5869 qp_attr->cap.max_send_wr = 0;
5870 qp_attr->cap.max_send_sge = 0;
5871 }
5872
Noa Osherovich0540d812016-06-04 15:15:32 +03005873 qp_init_attr->qp_type = ibqp->qp_type;
5874 qp_init_attr->recv_cq = ibqp->recv_cq;
5875 qp_init_attr->send_cq = ibqp->send_cq;
5876 qp_init_attr->srq = ibqp->srq;
5877 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005878
5879 qp_init_attr->cap = qp_attr->cap;
5880
Leon Romanovskya8f3ea62020-04-27 18:46:17 +03005881 qp_init_attr->create_flags = qp->flags;
Leon Romanovsky051f2632015-12-20 12:16:11 +02005882
Eli Cohene126ba92013-07-07 17:25:49 +03005883 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5884 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5885
Eli Cohene126ba92013-07-07 17:25:49 +03005886out:
5887 mutex_unlock(&qp->mutex);
5888 return err;
5889}
5890
5891struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03005892 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005893{
5894 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5895 struct mlx5_ib_xrcd *xrcd;
5896 int err;
5897
Saeed Mahameed938fe832015-05-28 22:28:41 +03005898 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005899 return ERR_PTR(-ENOSYS);
5900
5901 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5902 if (!xrcd)
5903 return ERR_PTR(-ENOMEM);
5904
Yishai Hadas5aa37712018-11-26 08:28:38 +02005905 err = mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03005906 if (err) {
5907 kfree(xrcd);
5908 return ERR_PTR(-ENOMEM);
5909 }
5910
5911 return &xrcd->ibxrcd;
5912}
5913
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005914int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005915{
5916 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5917 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
5918 int err;
5919
Yishai Hadas5aa37712018-11-26 08:28:38 +02005920 err = mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, 0);
Leon Romanovskyb0818082018-01-28 11:25:30 +02005921 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03005922 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005923
5924 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03005925 return 0;
5926}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005927
Yishai Hadas350d0e42016-08-28 14:58:18 +03005928static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5929{
5930 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5931 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5932 struct ib_event event;
5933
5934 if (rwq->ibwq.event_handler) {
5935 event.device = rwq->ibwq.device;
5936 event.element.wq = &rwq->ibwq;
5937 switch (type) {
5938 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5939 event.event = IB_EVENT_WQ_FATAL;
5940 break;
5941 default:
5942 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5943 return;
5944 }
5945
5946 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5947 }
5948}
5949
Maor Gottlieb03404e82017-05-30 10:29:13 +03005950static int set_delay_drop(struct mlx5_ib_dev *dev)
5951{
5952 int err = 0;
5953
5954 mutex_lock(&dev->delay_drop.lock);
5955 if (dev->delay_drop.activate)
5956 goto out;
5957
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03005958 err = mlx5_core_set_delay_drop(dev, dev->delay_drop.timeout);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005959 if (err)
5960 goto out;
5961
5962 dev->delay_drop.activate = true;
5963out:
5964 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005965
5966 if (!err)
5967 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005968 return err;
5969}
5970
Yishai Hadas79b20a62016-05-23 15:20:50 +03005971static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5972 struct ib_wq_init_attr *init_attr)
5973{
5974 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005975 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005976 __be64 *rq_pas0;
5977 void *in;
5978 void *rqc;
5979 void *wq;
5980 int inlen;
5981 int err;
5982
5983 dev = to_mdev(pd->device);
5984
5985 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005986 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005987 if (!in)
5988 return -ENOMEM;
5989
Yishai Hadas34d57582018-09-20 21:39:21 +03005990 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005991 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
5992 MLX5_SET(rqc, rqc, mem_rq_type,
5993 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
5994 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
5995 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
5996 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
5997 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
5998 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03005999 MLX5_SET(wq, wq, wq_type,
6000 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
6001 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02006002 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6003 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
6004 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
6005 err = -EOPNOTSUPP;
6006 goto out;
6007 } else {
6008 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
6009 }
6010 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006011 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03006012 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
Mark Zhangc16339b2019-11-15 17:45:55 +02006013 /*
6014 * In Firmware number of strides in each WQE is:
6015 * "512 * 2^single_wqe_log_num_of_strides"
6016 * Values 3 to 8 are accepted as 10 to 15, 9 to 18 are
6017 * accepted as 0 to 9
6018 */
6019 static const u8 fw_map[] = { 10, 11, 12, 13, 14, 15, 0, 1,
6020 2, 3, 4, 5, 6, 7, 8, 9 };
Noa Osherovichccc87082017-10-17 18:01:13 +03006021 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
6022 MLX5_SET(wq, wq, log_wqe_stride_size,
6023 rwq->single_stride_log_num_of_bytes -
6024 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
Mark Zhangc16339b2019-11-15 17:45:55 +02006025 MLX5_SET(wq, wq, log_wqe_num_of_strides,
6026 fw_map[rwq->log_num_strides -
6027 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES]);
Noa Osherovichccc87082017-10-17 18:01:13 +03006028 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006029 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
6030 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
6031 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
6032 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
6033 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
6034 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02006035 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006036 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02006037 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006038 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
6039 err = -EOPNOTSUPP;
6040 goto out;
6041 }
6042 } else {
6043 MLX5_SET(rqc, rqc, vsd, 1);
6044 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02006045 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
6046 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
6047 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
6048 err = -EOPNOTSUPP;
6049 goto out;
6050 }
6051 MLX5_SET(rqc, rqc, scatter_fcs, 1);
6052 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03006053 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
6054 if (!(dev->ib_dev.attrs.raw_packet_caps &
6055 IB_RAW_PACKET_CAP_DELAY_DROP)) {
6056 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
6057 err = -EOPNOTSUPP;
6058 goto out;
6059 }
6060 MLX5_SET(rqc, rqc, delay_drop_en, 1);
6061 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006062 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
6063 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03006064 err = mlx5_core_create_rq_tracked(dev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03006065 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
6066 err = set_delay_drop(dev);
6067 if (err) {
6068 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
6069 err);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03006070 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03006071 } else {
6072 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
6073 }
6074 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006075out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03006076 kvfree(in);
6077 return err;
6078}
6079
6080static int set_user_rq_size(struct mlx5_ib_dev *dev,
6081 struct ib_wq_init_attr *wq_init_attr,
6082 struct mlx5_ib_create_wq *ucmd,
6083 struct mlx5_ib_rwq *rwq)
6084{
6085 /* Sanity check RQ size before proceeding */
6086 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
6087 return -EINVAL;
6088
6089 if (!ucmd->rq_wqe_count)
6090 return -EINVAL;
6091
6092 rwq->wqe_count = ucmd->rq_wqe_count;
6093 rwq->wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky0dfe4522018-08-01 14:25:41 -07006094 if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
6095 return -EINVAL;
6096
Yishai Hadas79b20a62016-05-23 15:20:50 +03006097 rwq->log_rq_stride = rwq->wqe_shift;
6098 rwq->log_rq_size = ilog2(rwq->wqe_count);
6099 return 0;
6100}
6101
Mark Zhangc16339b2019-11-15 17:45:55 +02006102static bool log_of_strides_valid(struct mlx5_ib_dev *dev, u32 log_num_strides)
6103{
6104 if ((log_num_strides > MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
6105 (log_num_strides < MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
6106 return false;
6107
6108 if (!MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) &&
6109 (log_num_strides < MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
6110 return false;
6111
6112 return true;
6113}
6114
Yishai Hadas79b20a62016-05-23 15:20:50 +03006115static int prepare_user_rq(struct ib_pd *pd,
6116 struct ib_wq_init_attr *init_attr,
6117 struct ib_udata *udata,
6118 struct mlx5_ib_rwq *rwq)
6119{
6120 struct mlx5_ib_dev *dev = to_mdev(pd->device);
6121 struct mlx5_ib_create_wq ucmd = {};
6122 int err;
6123 size_t required_cmd_sz;
6124
Noa Osherovichccc87082017-10-17 18:01:13 +03006125 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
6126 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006127 if (udata->inlen < required_cmd_sz) {
6128 mlx5_ib_dbg(dev, "invalid inlen\n");
6129 return -EINVAL;
6130 }
6131
6132 if (udata->inlen > sizeof(ucmd) &&
6133 !ib_is_udata_cleared(udata, sizeof(ucmd),
6134 udata->inlen - sizeof(ucmd))) {
6135 mlx5_ib_dbg(dev, "inlen is not supported\n");
6136 return -EOPNOTSUPP;
6137 }
6138
6139 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
6140 mlx5_ib_dbg(dev, "copy failed\n");
6141 return -EFAULT;
6142 }
6143
Noa Osherovichccc87082017-10-17 18:01:13 +03006144 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03006145 mlx5_ib_dbg(dev, "invalid comp mask\n");
6146 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03006147 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
6148 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
6149 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
6150 return -EOPNOTSUPP;
6151 }
6152 if ((ucmd.single_stride_log_num_of_bytes <
6153 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
6154 (ucmd.single_stride_log_num_of_bytes >
6155 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
6156 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
6157 ucmd.single_stride_log_num_of_bytes,
6158 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
6159 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
6160 return -EINVAL;
6161 }
Mark Zhangc16339b2019-11-15 17:45:55 +02006162 if (!log_of_strides_valid(dev,
6163 ucmd.single_wqe_log_num_of_strides)) {
6164 mlx5_ib_dbg(
6165 dev,
6166 "Invalid log num strides (%u. Range is %u - %u)\n",
6167 ucmd.single_wqe_log_num_of_strides,
6168 MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) ?
6169 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES :
6170 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
6171 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
Noa Osherovichccc87082017-10-17 18:01:13 +03006172 return -EINVAL;
6173 }
6174 rwq->single_stride_log_num_of_bytes =
6175 ucmd.single_stride_log_num_of_bytes;
6176 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
6177 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
6178 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006179 }
6180
6181 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
6182 if (err) {
6183 mlx5_ib_dbg(dev, "err %d\n", err);
6184 return err;
6185 }
6186
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02006187 err = create_user_rq(dev, pd, udata, rwq, &ucmd);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006188 if (err) {
6189 mlx5_ib_dbg(dev, "err %d\n", err);
Gal Pressman645ba592018-10-08 19:44:03 +03006190 return err;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006191 }
6192
6193 rwq->user_index = ucmd.user_index;
6194 return 0;
6195}
6196
6197struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
6198 struct ib_wq_init_attr *init_attr,
6199 struct ib_udata *udata)
6200{
6201 struct mlx5_ib_dev *dev;
6202 struct mlx5_ib_rwq *rwq;
6203 struct mlx5_ib_create_wq_resp resp = {};
6204 size_t min_resp_len;
6205 int err;
6206
6207 if (!udata)
6208 return ERR_PTR(-ENOSYS);
6209
6210 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6211 if (udata->outlen && udata->outlen < min_resp_len)
6212 return ERR_PTR(-EINVAL);
6213
Maor Gottliebba800132020-03-22 14:49:06 +02006214 if (!capable(CAP_SYS_RAWIO) &&
6215 init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP)
6216 return ERR_PTR(-EPERM);
6217
Yishai Hadas79b20a62016-05-23 15:20:50 +03006218 dev = to_mdev(pd->device);
6219 switch (init_attr->wq_type) {
6220 case IB_WQT_RQ:
6221 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
6222 if (!rwq)
6223 return ERR_PTR(-ENOMEM);
6224 err = prepare_user_rq(pd, init_attr, udata, rwq);
6225 if (err)
6226 goto err;
6227 err = create_rq(rwq, pd, init_attr);
6228 if (err)
6229 goto err_user_rq;
6230 break;
6231 default:
6232 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
6233 init_attr->wq_type);
6234 return ERR_PTR(-EINVAL);
6235 }
6236
Yishai Hadas350d0e42016-08-28 14:58:18 +03006237 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006238 rwq->ibwq.state = IB_WQS_RESET;
6239 if (udata->outlen) {
6240 resp.response_length = offsetof(typeof(resp), response_length) +
6241 sizeof(resp.response_length);
6242 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6243 if (err)
6244 goto err_copy;
6245 }
6246
Yishai Hadas350d0e42016-08-28 14:58:18 +03006247 rwq->core_qp.event = mlx5_ib_wq_event;
6248 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006249 return &rwq->ibwq;
6250
6251err_copy:
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03006252 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006253err_user_rq:
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006254 destroy_user_rq(dev, pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006255err:
6256 kfree(rwq);
6257 return ERR_PTR(err);
6258}
6259
Leon Romanovskya49b1dc2019-06-12 15:27:41 +03006260void mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +03006261{
6262 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6263 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6264
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03006265 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006266 destroy_user_rq(dev, wq->pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006267 kfree(rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006268}
6269
Yishai Hadasc5f90922016-05-23 15:20:53 +03006270struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
6271 struct ib_rwq_ind_table_init_attr *init_attr,
6272 struct ib_udata *udata)
6273{
6274 struct mlx5_ib_dev *dev = to_mdev(device);
6275 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
6276 int sz = 1 << init_attr->log_ind_tbl_size;
6277 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
6278 size_t min_resp_len;
6279 int inlen;
6280 int err;
6281 int i;
6282 u32 *in;
6283 void *rqtc;
6284
6285 if (udata->inlen > 0 &&
6286 !ib_is_udata_cleared(udata, 0,
6287 udata->inlen))
6288 return ERR_PTR(-EOPNOTSUPP);
6289
Maor Gottliebefd7f402016-10-27 16:36:40 +03006290 if (init_attr->log_ind_tbl_size >
6291 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
6292 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
6293 init_attr->log_ind_tbl_size,
6294 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
6295 return ERR_PTR(-EINVAL);
6296 }
6297
Yishai Hadasc5f90922016-05-23 15:20:53 +03006298 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6299 if (udata->outlen && udata->outlen < min_resp_len)
6300 return ERR_PTR(-EINVAL);
6301
6302 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
6303 if (!rwq_ind_tbl)
6304 return ERR_PTR(-ENOMEM);
6305
6306 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006307 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006308 if (!in) {
6309 err = -ENOMEM;
6310 goto err;
6311 }
6312
6313 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
6314
6315 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
6316 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
6317
6318 for (i = 0; i < sz; i++)
6319 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
6320
Yishai Hadas5deba862018-09-20 21:39:28 +03006321 rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
6322 MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);
6323
Yishai Hadasc5f90922016-05-23 15:20:53 +03006324 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
6325 kvfree(in);
6326
6327 if (err)
6328 goto err;
6329
6330 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
6331 if (udata->outlen) {
6332 resp.response_length = offsetof(typeof(resp), response_length) +
6333 sizeof(resp.response_length);
6334 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6335 if (err)
6336 goto err_copy;
6337 }
6338
6339 return &rwq_ind_tbl->ib_rwq_ind_tbl;
6340
6341err_copy:
Yishai Hadas5deba862018-09-20 21:39:28 +03006342 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006343err:
6344 kfree(rwq_ind_tbl);
6345 return ERR_PTR(err);
6346}
6347
6348int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
6349{
6350 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
6351 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
6352
Yishai Hadas5deba862018-09-20 21:39:28 +03006353 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006354
6355 kfree(rwq_ind_tbl);
6356 return 0;
6357}
6358
Yishai Hadas79b20a62016-05-23 15:20:50 +03006359int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
6360 u32 wq_attr_mask, struct ib_udata *udata)
6361{
6362 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6363 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6364 struct mlx5_ib_modify_wq ucmd = {};
6365 size_t required_cmd_sz;
6366 int curr_wq_state;
6367 int wq_state;
6368 int inlen;
6369 int err;
6370 void *rqc;
6371 void *in;
6372
6373 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
6374 if (udata->inlen < required_cmd_sz)
6375 return -EINVAL;
6376
6377 if (udata->inlen > sizeof(ucmd) &&
6378 !ib_is_udata_cleared(udata, sizeof(ucmd),
6379 udata->inlen - sizeof(ucmd)))
6380 return -EOPNOTSUPP;
6381
6382 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
6383 return -EFAULT;
6384
6385 if (ucmd.comp_mask || ucmd.reserved)
6386 return -EOPNOTSUPP;
6387
6388 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006389 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006390 if (!in)
6391 return -ENOMEM;
6392
6393 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
6394
6395 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
6396 wq_attr->curr_wq_state : wq->state;
6397 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
6398 wq_attr->wq_state : curr_wq_state;
6399 if (curr_wq_state == IB_WQS_ERR)
6400 curr_wq_state = MLX5_RQC_STATE_ERR;
6401 if (wq_state == IB_WQS_ERR)
6402 wq_state = MLX5_RQC_STATE_ERR;
6403 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
Yishai Hadas34d57582018-09-20 21:39:21 +03006404 MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006405 MLX5_SET(rqc, rqc, state, wq_state);
6406
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006407 if (wq_attr_mask & IB_WQ_FLAGS) {
6408 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
6409 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
6410 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
6411 mlx5_ib_dbg(dev, "VLAN offloads are not "
6412 "supported\n");
6413 err = -EOPNOTSUPP;
6414 goto out;
6415 }
6416 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6417 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
6418 MLX5_SET(rqc, rqc, vsd,
6419 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
6420 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02006421
6422 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6423 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
6424 err = -EOPNOTSUPP;
6425 goto out;
6426 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006427 }
6428
Majd Dibbiny23a69642017-01-18 15:25:10 +02006429 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03006430 u16 set_id;
6431
6432 set_id = mlx5_ib_get_counters_id(dev, 0);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006433 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
6434 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6435 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandit3e1f0002019-07-23 10:31:17 +03006436 MLX5_SET(rqc, rqc, counter_set_id, set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006437 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06006438 dev_info_once(
6439 &dev->ib_dev.dev,
6440 "Receive WQ counters are not supported on current FW\n");
Majd Dibbiny23a69642017-01-18 15:25:10 +02006441 }
6442
Leon Romanovskye0b4b472020-04-09 21:03:33 +03006443 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006444 if (!err)
6445 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
6446
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006447out:
6448 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006449 return err;
6450}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006451
6452struct mlx5_ib_drain_cqe {
6453 struct ib_cqe cqe;
6454 struct completion done;
6455};
6456
6457static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
6458{
6459 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
6460 struct mlx5_ib_drain_cqe,
6461 cqe);
6462
6463 complete(&cqe->done);
6464}
6465
6466/* This function returns only once the drained WR was completed */
6467static void handle_drain_completion(struct ib_cq *cq,
6468 struct mlx5_ib_drain_cqe *sdrain,
6469 struct mlx5_ib_dev *dev)
6470{
6471 struct mlx5_core_dev *mdev = dev->mdev;
6472
6473 if (cq->poll_ctx == IB_POLL_DIRECT) {
6474 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
6475 ib_process_cq_direct(cq, -1);
6476 return;
6477 }
6478
6479 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6480 struct mlx5_ib_cq *mcq = to_mcq(cq);
6481 bool triggered = false;
6482 unsigned long flags;
6483
6484 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
6485 /* Make sure that the CQ handler won't run if wasn't run yet */
6486 if (!mcq->mcq.reset_notify_added)
6487 mcq->mcq.reset_notify_added = 1;
6488 else
6489 triggered = true;
6490 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
6491
6492 if (triggered) {
6493 /* Wait for any scheduled/running task to be ended */
6494 switch (cq->poll_ctx) {
6495 case IB_POLL_SOFTIRQ:
6496 irq_poll_disable(&cq->iop);
6497 irq_poll_enable(&cq->iop);
6498 break;
6499 case IB_POLL_WORKQUEUE:
6500 cancel_work_sync(&cq->work);
6501 break;
6502 default:
6503 WARN_ON_ONCE(1);
6504 }
6505 }
6506
6507 /* Run the CQ handler - this makes sure that the drain WR will
6508 * be processed if wasn't processed yet.
6509 */
Yishai Hadas4e0e2ea2019-06-30 19:23:27 +03006510 mcq->mcq.comp(&mcq->mcq, NULL);
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006511 }
6512
6513 wait_for_completion(&sdrain->done);
6514}
6515
6516void mlx5_ib_drain_sq(struct ib_qp *qp)
6517{
6518 struct ib_cq *cq = qp->send_cq;
6519 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6520 struct mlx5_ib_drain_cqe sdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006521 const struct ib_send_wr *bad_swr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006522 struct ib_rdma_wr swr = {
6523 .wr = {
6524 .next = NULL,
6525 { .wr_cqe = &sdrain.cqe, },
6526 .opcode = IB_WR_RDMA_WRITE,
6527 },
6528 };
6529 int ret;
6530 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6531 struct mlx5_core_dev *mdev = dev->mdev;
6532
6533 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6534 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6535 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6536 return;
6537 }
6538
6539 sdrain.cqe.done = mlx5_ib_drain_qp_done;
6540 init_completion(&sdrain.done);
6541
6542 ret = _mlx5_ib_post_send(qp, &swr.wr, &bad_swr, true);
6543 if (ret) {
6544 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6545 return;
6546 }
6547
6548 handle_drain_completion(cq, &sdrain, dev);
6549}
6550
6551void mlx5_ib_drain_rq(struct ib_qp *qp)
6552{
6553 struct ib_cq *cq = qp->recv_cq;
6554 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6555 struct mlx5_ib_drain_cqe rdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006556 struct ib_recv_wr rwr = {};
6557 const struct ib_recv_wr *bad_rwr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006558 int ret;
6559 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6560 struct mlx5_core_dev *mdev = dev->mdev;
6561
6562 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6563 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6564 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6565 return;
6566 }
6567
6568 rwr.wr_cqe = &rdrain.cqe;
6569 rdrain.cqe.done = mlx5_ib_drain_qp_done;
6570 init_completion(&rdrain.done);
6571
6572 ret = _mlx5_ib_post_recv(qp, &rwr, &bad_rwr, true);
6573 if (ret) {
6574 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6575 return;
6576 }
6577
6578 handle_drain_completion(cq, &rdrain, dev);
6579}
Mark Zhangd14133d2019-07-02 13:02:36 +03006580
6581/**
6582 * Bind a qp to a counter. If @counter is NULL then bind the qp to
6583 * the default counter
6584 */
6585int mlx5_ib_qp_set_counter(struct ib_qp *qp, struct rdma_counter *counter)
6586{
Mark Zhang10189e82020-01-26 19:17:08 +02006587 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Mark Zhangd14133d2019-07-02 13:02:36 +03006588 struct mlx5_ib_qp *mqp = to_mqp(qp);
6589 int err = 0;
6590
6591 mutex_lock(&mqp->mutex);
6592 if (mqp->state == IB_QPS_RESET) {
6593 qp->counter = counter;
6594 goto out;
6595 }
6596
Mark Zhang10189e82020-01-26 19:17:08 +02006597 if (!MLX5_CAP_GEN(dev->mdev, rts2rts_qp_counters_set_id)) {
6598 err = -EOPNOTSUPP;
6599 goto out;
6600 }
6601
Mark Zhangd14133d2019-07-02 13:02:36 +03006602 if (mqp->state == IB_QPS_RTS) {
6603 err = __mlx5_ib_qp_set_counter(qp, counter);
6604 if (!err)
6605 qp->counter = counter;
6606
6607 goto out;
6608 }
6609
6610 mqp->counter_pending = 1;
6611 qp->counter = counter;
6612
6613out:
6614 mutex_unlock(&mqp->mutex);
6615 return err;
6616}