blob: a7fcb00e37a58c97cfe642c0d5ac7b16620bf788 [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"
Leon Romanovsky029e88f2020-05-06 09:55:13 +030043#include "wr.h"
Eli Cohene126ba92013-07-07 17:25:49 +030044
Eli Cohene126ba92013-07-07 17:25:49 +030045enum {
46 MLX5_IB_ACK_REQ_FREQ = 8,
47};
48
49enum {
50 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
51 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
52 MLX5_IB_LINK_TYPE_IB = 0,
53 MLX5_IB_LINK_TYPE_ETH = 1
54};
55
Alex Veskereb49ab02016-08-28 12:25:53 +030056enum raw_qp_set_mask_map {
57 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020058 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030059};
60
Alex Vesker0680efa2016-08-28 12:25:52 +030061struct mlx5_modify_raw_qp_param {
62 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030063
64 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang61147f32018-03-19 15:10:30 +020065
66 struct mlx5_rate_limit rl;
67
Alex Veskereb49ab02016-08-28 12:25:53 +030068 u8 rq_q_ctr_id;
Mark Blochd5ed8ac2019-03-28 15:27:38 +020069 u16 port;
Alex Vesker0680efa2016-08-28 12:25:52 +030070};
71
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030072static void get_cqs(enum ib_qp_type qp_type,
73 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
74 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
75
Eli Cohene126ba92013-07-07 17:25:49 +030076static int is_qp0(enum ib_qp_type qp_type)
77{
78 return qp_type == IB_QPT_SMI;
79}
80
Eli Cohene126ba92013-07-07 17:25:49 +030081static int is_sqp(enum ib_qp_type qp_type)
82{
83 return is_qp0(qp_type) || is_qp1(qp_type);
84}
85
Haggai Eranc1395a22014-12-11 17:04:14 +020086/**
Moni Shouafbeb4072019-01-22 08:48:46 +020087 * mlx5_ib_read_user_wqe_common() - Copy a WQE (or part of) from user WQ
88 * to kernel buffer
Haggai Eranc1395a22014-12-11 17:04:14 +020089 *
Moni Shouafbeb4072019-01-22 08:48:46 +020090 * @umem: User space memory where the WQ is
91 * @buffer: buffer to copy to
92 * @buflen: buffer length
93 * @wqe_index: index of WQE to copy from
94 * @wq_offset: offset to start of WQ
95 * @wq_wqe_cnt: number of WQEs in WQ
96 * @wq_wqe_shift: log2 of WQE size
97 * @bcnt: number of bytes to copy
98 * @bytes_copied: number of bytes to copy (return value)
Haggai Eranc1395a22014-12-11 17:04:14 +020099 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200100 * Copies from start of WQE bcnt or less bytes.
101 * Does not gurantee to copy the entire WQE.
Haggai Eranc1395a22014-12-11 17:04:14 +0200102 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200103 * Return: zero on success, or an error code.
Haggai Eranc1395a22014-12-11 17:04:14 +0200104 */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200105static int mlx5_ib_read_user_wqe_common(struct ib_umem *umem, void *buffer,
106 size_t buflen, int wqe_index,
107 int wq_offset, int wq_wqe_cnt,
108 int wq_wqe_shift, int bcnt,
Moni Shouafbeb4072019-01-22 08:48:46 +0200109 size_t *bytes_copied)
Haggai Eranc1395a22014-12-11 17:04:14 +0200110{
Moni Shouafbeb4072019-01-22 08:48:46 +0200111 size_t offset = wq_offset + ((wqe_index % wq_wqe_cnt) << wq_wqe_shift);
112 size_t wq_end = wq_offset + (wq_wqe_cnt << wq_wqe_shift);
113 size_t copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200114 int ret;
115
Moni Shouafbeb4072019-01-22 08:48:46 +0200116 /* don't copy more than requested, more than buffer length or
117 * beyond WQ end
118 */
119 copy_length = min_t(u32, buflen, wq_end - offset);
120 copy_length = min_t(u32, copy_length, bcnt);
Haggai Eranc1395a22014-12-11 17:04:14 +0200121
Moni Shouafbeb4072019-01-22 08:48:46 +0200122 ret = ib_umem_copy_from(buffer, umem, offset, copy_length);
Haggai Eranc1395a22014-12-11 17:04:14 +0200123 if (ret)
124 return ret;
125
Moni Shouafbeb4072019-01-22 08:48:46 +0200126 if (!ret && bytes_copied)
127 *bytes_copied = copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200128
Moni Shouafbeb4072019-01-22 08:48:46 +0200129 return 0;
130}
Haggai Eranc1395a22014-12-11 17:04:14 +0200131
Moni Shouada9ee9d2020-01-15 14:43:34 +0200132static int mlx5_ib_read_kernel_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index,
133 void *buffer, size_t buflen, size_t *bc)
134{
135 struct mlx5_wqe_ctrl_seg *ctrl;
136 size_t bytes_copied = 0;
137 size_t wqe_length;
138 void *p;
139 int ds;
140
141 wqe_index = wqe_index & qp->sq.fbc.sz_m1;
142
143 /* read the control segment first */
144 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, wqe_index);
145 ctrl = p;
146 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
147 wqe_length = ds * MLX5_WQE_DS_UNITS;
148
149 /* read rest of WQE if it spreads over more than one stride */
150 while (bytes_copied < wqe_length) {
151 size_t copy_length =
152 min_t(size_t, buflen - bytes_copied, MLX5_SEND_WQE_BB);
153
154 if (!copy_length)
155 break;
156
157 memcpy(buffer + bytes_copied, p, copy_length);
158 bytes_copied += copy_length;
159
160 wqe_index = (wqe_index + 1) & qp->sq.fbc.sz_m1;
161 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, wqe_index);
162 }
163 *bc = bytes_copied;
164 return 0;
165}
166
167static int mlx5_ib_read_user_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index,
168 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200169{
170 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
171 struct ib_umem *umem = base->ubuffer.umem;
172 struct mlx5_ib_wq *wq = &qp->sq;
173 struct mlx5_wqe_ctrl_seg *ctrl;
174 size_t bytes_copied;
175 size_t bytes_copied2;
176 size_t wqe_length;
177 int ret;
178 int ds;
Haggai Eranc1395a22014-12-11 17:04:14 +0200179
Moni Shouafbeb4072019-01-22 08:48:46 +0200180 /* at first read as much as possible */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200181 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index,
182 wq->offset, wq->wqe_cnt,
183 wq->wqe_shift, buflen,
Moni Shouafbeb4072019-01-22 08:48:46 +0200184 &bytes_copied);
Haggai Eranc1395a22014-12-11 17:04:14 +0200185 if (ret)
186 return ret;
187
Moni Shouafbeb4072019-01-22 08:48:46 +0200188 /* we need at least control segment size to proceed */
189 if (bytes_copied < sizeof(*ctrl))
190 return -EINVAL;
191
192 ctrl = buffer;
193 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
194 wqe_length = ds * MLX5_WQE_DS_UNITS;
195
196 /* if we copied enough then we are done */
197 if (bytes_copied >= wqe_length) {
198 *bc = bytes_copied;
199 return 0;
200 }
201
202 /* otherwise this a wrapped around wqe
203 * so read the remaining bytes starting
204 * from wqe_index 0
205 */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200206 ret = mlx5_ib_read_user_wqe_common(umem, buffer + bytes_copied,
207 buflen - bytes_copied, 0, wq->offset,
208 wq->wqe_cnt, wq->wqe_shift,
Moni Shouafbeb4072019-01-22 08:48:46 +0200209 wqe_length - bytes_copied,
210 &bytes_copied2);
211
212 if (ret)
213 return ret;
214 *bc = bytes_copied + bytes_copied2;
215 return 0;
216}
217
Moni Shouada9ee9d2020-01-15 14:43:34 +0200218int mlx5_ib_read_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
219 size_t buflen, size_t *bc)
220{
221 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
222 struct ib_umem *umem = base->ubuffer.umem;
223
224 if (buflen < sizeof(struct mlx5_wqe_ctrl_seg))
225 return -EINVAL;
226
227 if (!umem)
228 return mlx5_ib_read_kernel_wqe_sq(qp, wqe_index, buffer,
229 buflen, bc);
230
231 return mlx5_ib_read_user_wqe_sq(qp, wqe_index, buffer, buflen, bc);
232}
233
234static int mlx5_ib_read_user_wqe_rq(struct mlx5_ib_qp *qp, int wqe_index,
235 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200236{
237 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
238 struct ib_umem *umem = base->ubuffer.umem;
239 struct mlx5_ib_wq *wq = &qp->rq;
240 size_t bytes_copied;
241 int ret;
242
Moni Shouada9ee9d2020-01-15 14:43:34 +0200243 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index,
244 wq->offset, wq->wqe_cnt,
245 wq->wqe_shift, buflen,
Moni Shouafbeb4072019-01-22 08:48:46 +0200246 &bytes_copied);
247
248 if (ret)
249 return ret;
250 *bc = bytes_copied;
251 return 0;
252}
253
Moni Shouada9ee9d2020-01-15 14:43:34 +0200254int mlx5_ib_read_wqe_rq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
255 size_t buflen, size_t *bc)
256{
257 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
258 struct ib_umem *umem = base->ubuffer.umem;
259 struct mlx5_ib_wq *wq = &qp->rq;
260 size_t wqe_size = 1 << wq->wqe_shift;
261
262 if (buflen < wqe_size)
263 return -EINVAL;
264
265 if (!umem)
266 return -EOPNOTSUPP;
267
268 return mlx5_ib_read_user_wqe_rq(qp, wqe_index, buffer, buflen, bc);
269}
270
271static int mlx5_ib_read_user_wqe_srq(struct mlx5_ib_srq *srq, int wqe_index,
272 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200273{
274 struct ib_umem *umem = srq->umem;
275 size_t bytes_copied;
276 int ret;
277
Moni Shouada9ee9d2020-01-15 14:43:34 +0200278 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index, 0,
279 srq->msrq.max, srq->msrq.wqe_shift,
280 buflen, &bytes_copied);
Moni Shouafbeb4072019-01-22 08:48:46 +0200281
282 if (ret)
283 return ret;
284 *bc = bytes_copied;
285 return 0;
Haggai Eranc1395a22014-12-11 17:04:14 +0200286}
287
Moni Shouada9ee9d2020-01-15 14:43:34 +0200288int mlx5_ib_read_wqe_srq(struct mlx5_ib_srq *srq, int wqe_index, void *buffer,
289 size_t buflen, size_t *bc)
290{
291 struct ib_umem *umem = srq->umem;
292 size_t wqe_size = 1 << srq->msrq.wqe_shift;
293
294 if (buflen < wqe_size)
295 return -EINVAL;
296
297 if (!umem)
298 return -EOPNOTSUPP;
299
300 return mlx5_ib_read_user_wqe_srq(srq, wqe_index, buffer, buflen, bc);
301}
302
Eli Cohene126ba92013-07-07 17:25:49 +0300303static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
304{
305 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
306 struct ib_event event;
307
majd@mellanox.com19098df2016-01-14 19:13:03 +0200308 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
309 /* This event is only valid for trans_qps */
310 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
311 }
Eli Cohene126ba92013-07-07 17:25:49 +0300312
313 if (ibqp->event_handler) {
314 event.device = ibqp->device;
315 event.element.qp = ibqp;
316 switch (type) {
317 case MLX5_EVENT_TYPE_PATH_MIG:
318 event.event = IB_EVENT_PATH_MIG;
319 break;
320 case MLX5_EVENT_TYPE_COMM_EST:
321 event.event = IB_EVENT_COMM_EST;
322 break;
323 case MLX5_EVENT_TYPE_SQ_DRAINED:
324 event.event = IB_EVENT_SQ_DRAINED;
325 break;
326 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
327 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
328 break;
329 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
330 event.event = IB_EVENT_QP_FATAL;
331 break;
332 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
333 event.event = IB_EVENT_PATH_MIG_ERR;
334 break;
335 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
336 event.event = IB_EVENT_QP_REQ_ERR;
337 break;
338 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
339 event.event = IB_EVENT_QP_ACCESS_ERR;
340 break;
341 default:
342 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
343 return;
344 }
345
346 ibqp->event_handler(&event, ibqp->qp_context);
347 }
348}
349
350static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
351 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
352{
353 int wqe_size;
354 int wq_size;
355
356 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300357 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300358 return -EINVAL;
359
360 if (!has_rq) {
361 qp->rq.max_gs = 0;
362 qp->rq.wqe_cnt = 0;
363 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300364 cap->max_recv_wr = 0;
365 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300366 } else {
Leon Romanovskyc95e6d52020-04-27 18:46:15 +0300367 int wq_sig = !!(qp->flags_en & MLX5_QP_FLAG_SIGNATURE);
368
Eli Cohene126ba92013-07-07 17:25:49 +0300369 if (ucmd) {
370 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300371 if (ucmd->rq_wqe_shift > BITS_PER_BYTE * sizeof(ucmd->rq_wqe_shift))
372 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300373 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovskyc95e6d52020-04-27 18:46:15 +0300374 if ((1 << qp->rq.wqe_shift) /
375 sizeof(struct mlx5_wqe_data_seg) <
376 wq_sig)
Leon Romanovsky002bf222018-04-23 17:01:53 +0300377 return -EINVAL;
Leon Romanovskyc95e6d52020-04-27 18:46:15 +0300378 qp->rq.max_gs =
379 (1 << qp->rq.wqe_shift) /
380 sizeof(struct mlx5_wqe_data_seg) -
381 wq_sig;
Eli Cohene126ba92013-07-07 17:25:49 +0300382 qp->rq.max_post = qp->rq.wqe_cnt;
383 } else {
Leon Romanovskyc95e6d52020-04-27 18:46:15 +0300384 wqe_size =
385 wq_sig ? sizeof(struct mlx5_wqe_signature_seg) :
386 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300387 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
388 wqe_size = roundup_pow_of_two(wqe_size);
389 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
390 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
391 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300392 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300393 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
394 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300395 MLX5_CAP_GEN(dev->mdev,
396 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300397 return -EINVAL;
398 }
399 qp->rq.wqe_shift = ilog2(wqe_size);
Leon Romanovskyc95e6d52020-04-27 18:46:15 +0300400 qp->rq.max_gs =
401 (1 << qp->rq.wqe_shift) /
402 sizeof(struct mlx5_wqe_data_seg) -
403 wq_sig;
Eli Cohene126ba92013-07-07 17:25:49 +0300404 qp->rq.max_post = qp->rq.wqe_cnt;
405 }
406 }
407
408 return 0;
409}
410
Erez Shitritf0313962016-02-21 16:27:17 +0200411static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300412{
Andi Shyti618af382013-07-16 15:35:01 +0200413 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300414
Erez Shitritf0313962016-02-21 16:27:17 +0200415 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300416 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300417 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300418 /* fall through */
419 case IB_QPT_RC:
420 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200421 max(sizeof(struct mlx5_wqe_atomic_seg) +
422 sizeof(struct mlx5_wqe_raddr_seg),
423 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
Idan Burstein064e5262018-05-02 13:16:39 +0300424 sizeof(struct mlx5_mkey_seg) +
425 MLX5_IB_SQ_UMR_INLINE_THRESHOLD /
426 MLX5_IB_UMR_OCTOWORD);
Eli Cohene126ba92013-07-07 17:25:49 +0300427 break;
428
Eli Cohenb125a542013-09-11 16:35:22 +0300429 case IB_QPT_XRC_TGT:
430 return 0;
431
Eli Cohene126ba92013-07-07 17:25:49 +0300432 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300433 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200434 max(sizeof(struct mlx5_wqe_raddr_seg),
435 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
436 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300437 break;
438
439 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200440 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
441 size += sizeof(struct mlx5_wqe_eth_pad) +
442 sizeof(struct mlx5_wqe_eth_seg);
443 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300444 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200445 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300446 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300447 sizeof(struct mlx5_wqe_datagram_seg);
448 break;
449
450 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300451 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300452 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
453 sizeof(struct mlx5_mkey_seg);
454 break;
455
456 default:
457 return -EINVAL;
458 }
459
460 return size;
461}
462
463static int calc_send_wqe(struct ib_qp_init_attr *attr)
464{
465 int inl_size = 0;
466 int size;
467
Erez Shitritf0313962016-02-21 16:27:17 +0200468 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300469 if (size < 0)
470 return size;
471
472 if (attr->cap.max_inline_data) {
473 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
474 attr->cap.max_inline_data;
475 }
476
477 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300478 if (attr->create_flags & IB_QP_CREATE_INTEGRITY_EN &&
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200479 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300480 return MLX5_SIG_WQE_SIZE;
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200481 else
482 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300483}
484
Eli Cohen288c01b2016-10-27 16:36:45 +0300485static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
486{
487 int max_sge;
488
489 if (attr->qp_type == IB_QPT_RC)
490 max_sge = (min_t(int, wqe_size, 512) -
491 sizeof(struct mlx5_wqe_ctrl_seg) -
492 sizeof(struct mlx5_wqe_raddr_seg)) /
493 sizeof(struct mlx5_wqe_data_seg);
494 else if (attr->qp_type == IB_QPT_XRC_INI)
495 max_sge = (min_t(int, wqe_size, 512) -
496 sizeof(struct mlx5_wqe_ctrl_seg) -
497 sizeof(struct mlx5_wqe_xrc_seg) -
498 sizeof(struct mlx5_wqe_raddr_seg)) /
499 sizeof(struct mlx5_wqe_data_seg);
500 else
501 max_sge = (wqe_size - sq_overhead(attr)) /
502 sizeof(struct mlx5_wqe_data_seg);
503
504 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
505 sizeof(struct mlx5_wqe_data_seg));
506}
507
Eli Cohene126ba92013-07-07 17:25:49 +0300508static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
509 struct mlx5_ib_qp *qp)
510{
511 int wqe_size;
512 int wq_size;
513
514 if (!attr->cap.max_send_wr)
515 return 0;
516
517 wqe_size = calc_send_wqe(attr);
518 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
519 if (wqe_size < 0)
520 return wqe_size;
521
Saeed Mahameed938fe832015-05-28 22:28:41 +0300522 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300523 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300524 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300525 return -EINVAL;
526 }
527
Erez Shitritf0313962016-02-21 16:27:17 +0200528 qp->max_inline_data = wqe_size - sq_overhead(attr) -
529 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300530 attr->cap.max_inline_data = qp->max_inline_data;
531
532 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
533 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300534 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800535 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
536 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300537 qp->sq.wqe_cnt,
538 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300539 return -ENOMEM;
540 }
Eli Cohene126ba92013-07-07 17:25:49 +0300541 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300542 qp->sq.max_gs = get_send_sge(attr, wqe_size);
543 if (qp->sq.max_gs < attr->cap.max_send_sge)
544 return -ENOMEM;
545
546 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300547 qp->sq.max_post = wq_size / wqe_size;
548 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300549
550 return wq_size;
551}
552
553static int set_user_buf_size(struct mlx5_ib_dev *dev,
554 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200555 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200556 struct mlx5_ib_qp_base *base,
557 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300558{
559 int desc_sz = 1 << qp->sq.wqe_shift;
560
Saeed Mahameed938fe832015-05-28 22:28:41 +0300561 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300562 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300563 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300564 return -EINVAL;
565 }
566
Gal Pressmanaf8b38e2019-02-06 15:45:35 +0200567 if (ucmd->sq_wqe_count && !is_power_of_2(ucmd->sq_wqe_count)) {
568 mlx5_ib_warn(dev, "sq_wqe_count %d is not a power of two\n",
569 ucmd->sq_wqe_count);
Eli Cohene126ba92013-07-07 17:25:49 +0300570 return -EINVAL;
571 }
572
573 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
574
Saeed Mahameed938fe832015-05-28 22:28:41 +0300575 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300576 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300577 qp->sq.wqe_cnt,
578 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300579 return -EINVAL;
580 }
581
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300582 if (attr->qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +0300583 qp->flags & IB_QP_CREATE_SOURCE_QPN) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200584 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
585 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
586 } else {
587 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
588 (qp->sq.wqe_cnt << 6);
589 }
Eli Cohene126ba92013-07-07 17:25:49 +0300590
591 return 0;
592}
593
594static int qp_has_rq(struct ib_qp_init_attr *attr)
595{
596 if (attr->qp_type == IB_QPT_XRC_INI ||
597 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
598 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
599 !attr->cap.max_recv_wr)
600 return 0;
601
602 return 1;
603}
604
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200605enum {
606 /* this is the first blue flame register in the array of bfregs assigned
607 * to a processes. Since we do not use it for blue flame but rather
608 * regular 64 bit doorbells, we do not need a lock for maintaiing
609 * "odd/even" order
610 */
611 NUM_NON_BLUE_FLAME_BFREGS = 1,
612};
613
Eli Cohenb037c292017-01-03 23:55:26 +0200614static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
615{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200616 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200617}
618
619static int num_med_bfreg(struct mlx5_ib_dev *dev,
620 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200621{
622 int n;
623
Eli Cohenb037c292017-01-03 23:55:26 +0200624 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
625 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200626
627 return n >= 0 ? n : 0;
628}
629
Yishai Hadas18b03622018-05-07 10:20:01 +0300630static int first_med_bfreg(struct mlx5_ib_dev *dev,
631 struct mlx5_bfreg_info *bfregi)
632{
633 return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
634}
635
Eli Cohenb037c292017-01-03 23:55:26 +0200636static int first_hi_bfreg(struct mlx5_ib_dev *dev,
637 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200638{
639 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200640
Eli Cohenb037c292017-01-03 23:55:26 +0200641 med = num_med_bfreg(dev, bfregi);
642 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200643}
644
Eli Cohenb037c292017-01-03 23:55:26 +0200645static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
646 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300647{
Eli Cohene126ba92013-07-07 17:25:49 +0300648 int i;
649
Eli Cohenb037c292017-01-03 23:55:26 +0200650 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
651 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200652 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300653 return i;
654 }
655 }
656
657 return -ENOMEM;
658}
659
Eli Cohenb037c292017-01-03 23:55:26 +0200660static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
661 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300662{
Yishai Hadas18b03622018-05-07 10:20:01 +0300663 int minidx = first_med_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300664 int i;
665
Yishai Hadas18b03622018-05-07 10:20:01 +0300666 if (minidx < 0)
667 return minidx;
668
669 for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200670 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300671 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200672 if (!bfregi->count[minidx])
673 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300674 }
675
Eli Cohen2f5ff262017-01-03 23:55:21 +0200676 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300677 return minidx;
678}
679
Eli Cohenb037c292017-01-03 23:55:26 +0200680static int alloc_bfreg(struct mlx5_ib_dev *dev,
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300681 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300682{
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300683 int bfregn = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +0300684
Yishai Hadas0a2fd012020-03-24 08:01:43 +0200685 if (bfregi->lib_uar_dyn)
686 return -EINVAL;
687
Eli Cohen2f5ff262017-01-03 23:55:21 +0200688 mutex_lock(&bfregi->lock);
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300689 if (bfregi->ver >= 2) {
690 bfregn = alloc_high_class_bfreg(dev, bfregi);
691 if (bfregn < 0)
692 bfregn = alloc_med_class_bfreg(dev, bfregi);
693 }
694
695 if (bfregn < 0) {
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200696 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200697 bfregn = 0;
698 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300699 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200700 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300701
Eli Cohen2f5ff262017-01-03 23:55:21 +0200702 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300703}
704
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200705void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300706{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200707 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200708 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200709 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300710}
711
712static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
713{
714 switch (state) {
715 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
716 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
717 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
718 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
719 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
720 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
721 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
722 default: return -1;
723 }
724}
725
726static int to_mlx5_st(enum ib_qp_type type)
727{
728 switch (type) {
729 case IB_QPT_RC: return MLX5_QP_ST_RC;
730 case IB_QPT_UC: return MLX5_QP_ST_UC;
731 case IB_QPT_UD: return MLX5_QP_ST_UD;
732 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
733 case IB_QPT_XRC_INI:
734 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
735 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200736 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200737 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Leon Romanovsky3ae7e662020-04-27 18:46:19 +0300738 case IB_QPT_RAW_PACKET: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300739 default: return -EINVAL;
740 }
741}
742
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300743static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
744 struct mlx5_ib_cq *recv_cq);
745static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
746 struct mlx5_ib_cq *recv_cq);
747
Yishai Hadas7c043e92018-06-17 13:00:03 +0300748int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300749 struct mlx5_bfreg_info *bfregi, u32 bfregn,
Yishai Hadas7c043e92018-06-17 13:00:03 +0300750 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300751{
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300752 unsigned int bfregs_per_sys_page;
753 u32 index_of_sys_page;
754 u32 offset;
Eli Cohenb037c292017-01-03 23:55:26 +0200755
Yishai Hadas0a2fd012020-03-24 08:01:43 +0200756 if (bfregi->lib_uar_dyn)
757 return -EINVAL;
758
Eli Cohenb037c292017-01-03 23:55:26 +0200759 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
760 MLX5_NON_FP_BFREGS_PER_UAR;
761 index_of_sys_page = bfregn / bfregs_per_sys_page;
762
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200763 if (dyn_bfreg) {
764 index_of_sys_page += bfregi->num_static_sys_pages;
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300765
766 if (index_of_sys_page >= bfregi->num_sys_pages)
767 return -EINVAL;
768
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200769 if (bfregn > bfregi->num_dyn_bfregs ||
770 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
771 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
772 return -EINVAL;
773 }
774 }
Eli Cohenb037c292017-01-03 23:55:26 +0200775
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200776 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200777 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300778}
779
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200780static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200781 unsigned long addr, size_t size,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200782 struct ib_umem **umem, int *npages, int *page_shift,
783 int *ncont, u32 *offset)
majd@mellanox.com19098df2016-01-14 19:13:03 +0200784{
785 int err;
786
Moni Shouac320e522020-01-15 14:43:31 +0200787 *umem = ib_umem_get(&dev->ib_dev, addr, size, 0);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200788 if (IS_ERR(*umem)) {
789 mlx5_ib_dbg(dev, "umem_get failed\n");
790 return PTR_ERR(*umem);
791 }
792
Majd Dibbiny762f8992016-10-27 16:36:47 +0300793 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200794
795 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
796 if (err) {
797 mlx5_ib_warn(dev, "bad offset\n");
798 goto err_umem;
799 }
800
801 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
802 addr, size, *npages, *page_shift, *ncont, *offset);
803
804 return 0;
805
806err_umem:
807 ib_umem_release(*umem);
808 *umem = NULL;
809
810 return err;
811}
812
Maor Gottliebfe248c32017-05-30 10:29:14 +0300813static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300814 struct mlx5_ib_rwq *rwq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300815{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300816 struct mlx5_ib_ucontext *context =
817 rdma_udata_to_drv_context(
818 udata,
819 struct mlx5_ib_ucontext,
820 ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300821
Maor Gottliebfe248c32017-05-30 10:29:14 +0300822 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
823 atomic_dec(&dev->delay_drop.rqs_cnt);
824
Yishai Hadas79b20a62016-05-23 15:20:50 +0300825 mlx5_ib_db_unmap_user(context, &rwq->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300826 ib_umem_release(rwq->umem);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300827}
828
829static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200830 struct ib_udata *udata, struct mlx5_ib_rwq *rwq,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300831 struct mlx5_ib_create_wq *ucmd)
832{
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200833 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
834 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300835 int page_shift = 0;
836 int npages;
837 u32 offset = 0;
838 int ncont = 0;
839 int err;
840
841 if (!ucmd->buf_addr)
842 return -EINVAL;
843
Moni Shouac320e522020-01-15 14:43:31 +0200844 rwq->umem = ib_umem_get(&dev->ib_dev, ucmd->buf_addr, rwq->buf_size, 0);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300845 if (IS_ERR(rwq->umem)) {
846 mlx5_ib_dbg(dev, "umem_get failed\n");
847 err = PTR_ERR(rwq->umem);
848 return err;
849 }
850
Majd Dibbiny762f8992016-10-27 16:36:47 +0300851 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300852 &ncont, NULL);
853 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
854 &rwq->rq_page_offset);
855 if (err) {
856 mlx5_ib_warn(dev, "bad offset\n");
857 goto err_umem;
858 }
859
860 rwq->rq_num_pas = ncont;
861 rwq->page_shift = page_shift;
862 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
863 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
864
865 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
866 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
867 npages, page_shift, ncont, offset);
868
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200869 err = mlx5_ib_db_map_user(ucontext, udata, ucmd->db_addr, &rwq->db);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300870 if (err) {
871 mlx5_ib_dbg(dev, "map failed\n");
872 goto err_umem;
873 }
874
Yishai Hadas79b20a62016-05-23 15:20:50 +0300875 return 0;
876
877err_umem:
878 ib_umem_release(rwq->umem);
879 return err;
880}
881
Eli Cohenb037c292017-01-03 23:55:26 +0200882static int adjust_bfregn(struct mlx5_ib_dev *dev,
883 struct mlx5_bfreg_info *bfregi, int bfregn)
884{
885 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
886 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
887}
888
Leon Romanovsky98fc1122020-04-27 18:46:28 +0300889static int _create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
890 struct mlx5_ib_qp *qp, struct ib_udata *udata,
891 struct ib_qp_init_attr *attr, u32 **in,
892 struct mlx5_ib_create_qp_resp *resp, int *inlen,
893 struct mlx5_ib_qp_base *base,
894 struct mlx5_ib_create_qp *ucmd)
Eli Cohene126ba92013-07-07 17:25:49 +0300895{
896 struct mlx5_ib_ucontext *context;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200897 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200898 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200899 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300900 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200901 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200902 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200903 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300904 __be64 *pas;
905 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300906 int err;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200907 u16 uid;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200908 u32 uar_flags;
Eli Cohene126ba92013-07-07 17:25:49 +0300909
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200910 context = rdma_udata_to_drv_context(udata, struct mlx5_ib_ucontext,
911 ibucontext);
Leon Romanovsky76883a62020-04-27 18:46:23 +0300912 uar_flags = qp->flags_en &
913 (MLX5_QP_FLAG_UAR_PAGE_INDEX | MLX5_QP_FLAG_BFREG_INDEX);
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200914 switch (uar_flags) {
915 case MLX5_QP_FLAG_UAR_PAGE_INDEX:
Leon Romanovsky76883a62020-04-27 18:46:23 +0300916 uar_index = ucmd->bfreg_index;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200917 bfregn = MLX5_IB_INVALID_BFREG;
918 break;
919 case MLX5_QP_FLAG_BFREG_INDEX:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200920 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
Leon Romanovsky76883a62020-04-27 18:46:23 +0300921 ucmd->bfreg_index, true);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200922 if (uar_index < 0)
923 return uar_index;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200924 bfregn = MLX5_IB_INVALID_BFREG;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200925 break;
926 case 0:
Leon Romanovsky2be08c32020-04-27 18:46:13 +0300927 if (qp->flags & IB_QP_CREATE_CROSS_CHANNEL)
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200928 return -EINVAL;
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300929 bfregn = alloc_bfreg(dev, &context->bfregi);
930 if (bfregn < 0)
931 return bfregn;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200932 break;
933 default:
934 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300935 }
936
Eli Cohen2f5ff262017-01-03 23:55:21 +0200937 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200938 if (bfregn != MLX5_IB_INVALID_BFREG)
939 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
940 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300941
Haggai Eran48fea832014-05-22 14:50:11 +0300942 qp->rq.offset = 0;
943 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
944 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
945
Leon Romanovsky76883a62020-04-27 18:46:23 +0300946 err = set_user_buf_size(dev, qp, ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300947 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200948 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300949
Leon Romanovsky76883a62020-04-27 18:46:23 +0300950 if (ucmd->buf_addr && ubuffer->buf_size) {
951 ubuffer->buf_addr = ucmd->buf_addr;
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200952 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr,
953 ubuffer->buf_size, &ubuffer->umem,
954 &npages, &page_shift, &ncont, &offset);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200955 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200956 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200957 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200958 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300959 }
Eli Cohene126ba92013-07-07 17:25:49 +0300960
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300961 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
962 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300963 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300964 if (!*in) {
965 err = -ENOMEM;
966 goto err_umem;
967 }
Eli Cohene126ba92013-07-07 17:25:49 +0300968
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +0300969 uid = (attr->qp_type != IB_QPT_XRC_INI) ? to_mpd(pd)->uid : 0;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200970 MLX5_SET(create_qp_in, *in, uid, uid);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300971 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
972 if (ubuffer->umem)
973 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
974
975 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
976
977 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
978 MLX5_SET(qpc, qpc, page_offset, offset);
979
980 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200981 if (bfregn != MLX5_IB_INVALID_BFREG)
982 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
983 else
984 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200985 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300986
Leon Romanovsky76883a62020-04-27 18:46:23 +0300987 err = mlx5_ib_db_map_user(context, udata, ucmd->db_addr, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300988 if (err) {
989 mlx5_ib_dbg(dev, "map failed\n");
990 goto err_free;
991 }
992
Eli Cohene126ba92013-07-07 17:25:49 +0300993 return 0;
994
Eli Cohene126ba92013-07-07 17:25:49 +0300995err_free:
Al Viro479163f2014-11-20 08:13:57 +0000996 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300997
998err_umem:
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300999 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +03001000
Eli Cohen2f5ff262017-01-03 23:55:21 +02001001err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001002 if (bfregn != MLX5_IB_INVALID_BFREG)
1003 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001004 return err;
1005}
1006
Leon Romanovsky747c5192020-04-27 18:46:29 +03001007static void destroy_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1008 struct mlx5_ib_qp_base *base, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03001009{
Leon Romanovsky747c5192020-04-27 18:46:29 +03001010 struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
1011 udata, struct mlx5_ib_ucontext, ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03001012
Leon Romanovsky747c5192020-04-27 18:46:29 +03001013 if (udata) {
1014 /* User QP */
1015 mlx5_ib_db_unmap_user(context, &qp->db);
1016 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001017
Leon Romanovsky747c5192020-04-27 18:46:29 +03001018 /*
1019 * Free only the BFREGs which are handled by the kernel.
1020 * BFREGs of UARs allocated dynamically are handled by user.
1021 */
1022 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
1023 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
1024 return;
1025 }
1026
1027 /* Kernel QP */
1028 kvfree(qp->sq.wqe_head);
1029 kvfree(qp->sq.w_list);
1030 kvfree(qp->sq.wrid);
1031 kvfree(qp->sq.wr_data);
1032 kvfree(qp->rq.wrid);
1033 if (qp->db.db)
1034 mlx5_db_free(dev->mdev, &qp->db);
1035 if (qp->buf.frags)
1036 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001037}
1038
Leon Romanovsky98fc1122020-04-27 18:46:28 +03001039static int _create_kernel_qp(struct mlx5_ib_dev *dev,
1040 struct ib_qp_init_attr *init_attr,
1041 struct mlx5_ib_qp *qp, u32 **in, int *inlen,
1042 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +03001043{
Eli Cohene126ba92013-07-07 17:25:49 +03001044 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001045 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +03001046 int err;
1047
Eli Cohene126ba92013-07-07 17:25:49 +03001048 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001049 qp->bf.bfreg = &dev->fp_bfreg;
Leon Romanovsky29789752020-04-27 18:46:14 +03001050 else if (qp->flags & MLX5_IB_QP_CREATE_WC_TEST)
Michael Guralnik11f552e2019-06-10 15:21:24 +03001051 qp->bf.bfreg = &dev->wc_bfreg;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001052 else
1053 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +03001054
Eli Cohend8030b02017-02-09 19:31:47 +02001055 /* We need to divide by two since each register is comprised of
1056 * two buffers of identical size, namely odd and even
1057 */
1058 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001059 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +03001060
1061 err = calc_sq_size(dev, init_attr, qp);
1062 if (err < 0) {
1063 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001064 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001065 }
1066
1067 qp->rq.offset = 0;
1068 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001069 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +03001070
Guy Levi34f4c952018-11-26 08:15:50 +02001071 err = mlx5_frag_buf_alloc_node(dev->mdev, base->ubuffer.buf_size,
1072 &qp->buf, dev->mdev->priv.numa_node);
Eli Cohene126ba92013-07-07 17:25:49 +03001073 if (err) {
1074 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001075 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001076 }
1077
Guy Levi34f4c952018-11-26 08:15:50 +02001078 if (qp->rq.wqe_cnt)
1079 mlx5_init_fbc(qp->buf.frags, qp->rq.wqe_shift,
1080 ilog2(qp->rq.wqe_cnt), &qp->rq.fbc);
1081
1082 if (qp->sq.wqe_cnt) {
1083 int sq_strides_offset = (qp->sq.offset & (PAGE_SIZE - 1)) /
1084 MLX5_SEND_WQE_BB;
1085 mlx5_init_fbc_offset(qp->buf.frags +
1086 (qp->sq.offset / PAGE_SIZE),
1087 ilog2(MLX5_SEND_WQE_BB),
1088 ilog2(qp->sq.wqe_cnt),
1089 sq_strides_offset, &qp->sq.fbc);
1090
1091 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
1092 }
1093
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001094 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
1095 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001096 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001097 if (!*in) {
1098 err = -ENOMEM;
1099 goto err_buf;
1100 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001101
1102 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1103 MLX5_SET(qpc, qpc, uar_page, uar_index);
1104 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1105
Eli Cohene126ba92013-07-07 17:25:49 +03001106 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001107 MLX5_SET(qpc, qpc, fre, 1);
1108 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001109
Leon Romanovsky29789752020-04-27 18:46:14 +03001110 if (qp->flags & MLX5_IB_QP_CREATE_SQPN_QP1)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001111 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +02001112
Guy Levi34f4c952018-11-26 08:15:50 +02001113 mlx5_fill_page_frag_array(&qp->buf,
1114 (__be64 *)MLX5_ADDR_OF(create_qp_in,
1115 *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +03001116
Jack Morgenstein9603b612014-07-28 23:30:22 +03001117 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001118 if (err) {
1119 mlx5_ib_dbg(dev, "err %d\n", err);
1120 goto err_free;
1121 }
1122
Li Dongyangb5883002017-08-16 23:31:22 +10001123 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
1124 sizeof(*qp->sq.wrid), GFP_KERNEL);
1125 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1126 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1127 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1128 sizeof(*qp->rq.wrid), GFP_KERNEL);
1129 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1130 sizeof(*qp->sq.w_list), GFP_KERNEL);
1131 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1132 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001133
1134 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1135 !qp->sq.w_list || !qp->sq.wqe_head) {
1136 err = -ENOMEM;
1137 goto err_wrid;
1138 }
Eli Cohene126ba92013-07-07 17:25:49 +03001139
1140 return 0;
1141
1142err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001143 kvfree(qp->sq.wqe_head);
1144 kvfree(qp->sq.w_list);
1145 kvfree(qp->sq.wrid);
1146 kvfree(qp->sq.wr_data);
1147 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001148 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001149
1150err_free:
Al Viro479163f2014-11-20 08:13:57 +00001151 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001152
1153err_buf:
Guy Levi34f4c952018-11-26 08:15:50 +02001154 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001155 return err;
1156}
1157
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001158static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001159{
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03001160 if (attr->srq || (qp->type == IB_QPT_XRC_TGT) ||
1161 (qp->type == MLX5_IB_QPT_DCI) || (qp->type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001162 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001163 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001164 return MLX5_ZERO_LEN_RQ;
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03001165
1166 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001167}
1168
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001169static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001170 struct mlx5_ib_qp *qp,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001171 struct mlx5_ib_sq *sq, u32 tdn,
1172 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001173{
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001174 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001175 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1176
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001177 MLX5_SET(create_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001178 MLX5_SET(tisc, tisc, transport_domain, tdn);
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001179 if (qp->flags & IB_QP_CREATE_SOURCE_QPN)
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001180 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1181
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001182 return mlx5_core_create_tis(dev->mdev, in, &sq->tisn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001183}
1184
1185static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001186 struct mlx5_ib_sq *sq, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001187{
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001188 mlx5_cmd_destroy_tis(dev->mdev, sq->tisn, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001189}
1190
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001191static void destroy_flow_rule_vport_sq(struct mlx5_ib_sq *sq)
Mark Blochb96c9dd2018-01-29 10:40:37 +00001192{
1193 if (sq->flow_rule)
1194 mlx5_del_flow_rules(sq->flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001195 sq->flow_rule = NULL;
Mark Blochb96c9dd2018-01-29 10:40:37 +00001196}
1197
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001198static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001199 struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001200 struct mlx5_ib_sq *sq, void *qpin,
1201 struct ib_pd *pd)
1202{
1203 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1204 __be64 *pas;
1205 void *in;
1206 void *sqc;
1207 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1208 void *wq;
1209 int inlen;
1210 int err;
1211 int page_shift = 0;
1212 int npages;
1213 int ncont = 0;
1214 u32 offset = 0;
1215
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001216 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr, ubuffer->buf_size,
1217 &sq->ubuffer.umem, &npages, &page_shift, &ncont,
1218 &offset);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001219 if (err)
1220 return err;
1221
1222 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001223 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001224 if (!in) {
1225 err = -ENOMEM;
1226 goto err_umem;
1227 }
1228
Yishai Hadasc14003f2018-09-20 21:39:22 +03001229 MLX5_SET(create_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001230 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1231 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001232 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1233 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001234 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1235 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1236 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1237 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1238 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001239 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1240 MLX5_CAP_ETH(dev->mdev, swp))
1241 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001242
1243 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1244 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1245 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1246 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1247 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1248 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1249 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1250 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1251 MLX5_SET(wq, wq, page_offset, offset);
1252
1253 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1254 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1255
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001256 err = mlx5_core_create_sq_tracked(dev, in, inlen, &sq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001257
1258 kvfree(in);
1259
1260 if (err)
1261 goto err_umem;
1262
1263 return 0;
1264
1265err_umem:
1266 ib_umem_release(sq->ubuffer.umem);
1267 sq->ubuffer.umem = NULL;
1268
1269 return err;
1270}
1271
1272static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1273 struct mlx5_ib_sq *sq)
1274{
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001275 destroy_flow_rule_vport_sq(sq);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001276 mlx5_core_destroy_sq_tracked(dev, &sq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001277 ib_umem_release(sq->ubuffer.umem);
1278}
1279
Boris Pismenny2c292db2018-03-08 15:51:40 +02001280static size_t get_rq_pas_size(void *qpc)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001281{
1282 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1283 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1284 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1285 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1286 u32 po_quanta = 1 << (log_page_size - 6);
1287 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1288 u32 page_size = 1 << log_page_size;
1289 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1290 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1291
1292 return rq_num_pas * sizeof(u64);
1293}
1294
1295static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001296 struct mlx5_ib_rq *rq, void *qpin,
Yishai Hadas34d57582018-09-20 21:39:21 +03001297 size_t qpinlen, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001298{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001299 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001300 __be64 *pas;
1301 __be64 *qp_pas;
1302 void *in;
1303 void *rqc;
1304 void *wq;
1305 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001306 size_t rq_pas_size = get_rq_pas_size(qpc);
1307 size_t inlen;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001308 int err;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001309
1310 if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
1311 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001312
1313 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001314 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001315 if (!in)
1316 return -ENOMEM;
1317
Yishai Hadas34d57582018-09-20 21:39:21 +03001318 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001319 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001320 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1321 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001322 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1323 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1324 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1325 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1326 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1327
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001328 if (mqp->flags & IB_QP_CREATE_SCATTER_FCS)
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001329 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1330
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001331 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1332 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001333 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1334 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001335 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1336 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1337 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1338 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1339 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1340 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1341
1342 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1343 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1344 memcpy(pas, qp_pas, rq_pas_size);
1345
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001346 err = mlx5_core_create_rq_tracked(dev, in, inlen, &rq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001347
1348 kvfree(in);
1349
1350 return err;
1351}
1352
1353static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1354 struct mlx5_ib_rq *rq)
1355{
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001356 mlx5_core_destroy_rq_tracked(dev, &rq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001357}
1358
Mark Bloch0042f9e2018-09-17 13:30:49 +03001359static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1360 struct mlx5_ib_rq *rq,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001361 u32 qp_flags_en,
1362 struct ib_pd *pd)
Mark Bloch0042f9e2018-09-17 13:30:49 +03001363{
1364 if (qp_flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1365 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1366 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001367 mlx5_cmd_destroy_tir(dev->mdev, rq->tirn, to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001368}
1369
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001370static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001371 struct mlx5_ib_rq *rq, u32 tdn,
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001372 u32 *qp_flags_en, struct ib_pd *pd,
1373 u32 *out)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001374{
Mark Bloch175edba2018-09-17 13:30:48 +03001375 u8 lb_flag = 0;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001376 u32 *in;
1377 void *tirc;
1378 int inlen;
1379 int err;
1380
1381 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001382 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001383 if (!in)
1384 return -ENOMEM;
1385
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001386 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001387 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1388 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1389 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1390 MLX5_SET(tirc, tirc, transport_domain, tdn);
Mark Bloch175edba2018-09-17 13:30:48 +03001391 if (*qp_flags_en & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001392 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001393
Mark Bloch175edba2018-09-17 13:30:48 +03001394 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
1395 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1396
1397 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
1398 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1399
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001400 if (dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001401 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1402 *qp_flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1403 }
1404
1405 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001406 MLX5_SET(create_tir_in, in, opcode, MLX5_CMD_OP_CREATE_TIR);
1407 err = mlx5_cmd_exec_inout(dev->mdev, create_tir, in, out);
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001408 rq->tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001409 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1410 err = mlx5_ib_enable_lb(dev, false, true);
1411
1412 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001413 destroy_raw_packet_qp_tir(dev, rq, 0, pd);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001414 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001415 kvfree(in);
1416
1417 return err;
1418}
1419
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001420static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001421 u32 *in, size_t inlen,
Yishai Hadas7f720522018-09-20 21:45:18 +03001422 struct ib_pd *pd,
1423 struct ib_udata *udata,
1424 struct mlx5_ib_create_qp_resp *resp)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001425{
1426 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1427 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1428 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001429 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1430 udata, struct mlx5_ib_ucontext, ibucontext);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001431 int err;
1432 u32 tdn = mucontext->tdn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001433 u16 uid = to_mpd(pd)->uid;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001434 u32 out[MLX5_ST_SZ_DW(create_tir_out)] = {};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001435
Aharon Landau0eacc572020-04-27 18:46:36 +03001436 if (!qp->sq.wqe_cnt && !qp->rq.wqe_cnt)
1437 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001438 if (qp->sq.wqe_cnt) {
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001439 err = create_raw_packet_qp_tis(dev, qp, sq, tdn, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001440 if (err)
1441 return err;
1442
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001443 err = create_raw_packet_qp_sq(dev, udata, sq, in, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001444 if (err)
1445 goto err_destroy_tis;
1446
Yishai Hadas7f720522018-09-20 21:45:18 +03001447 if (uid) {
1448 resp->tisn = sq->tisn;
1449 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TISN;
1450 resp->sqn = sq->base.mqp.qpn;
1451 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_SQN;
1452 }
1453
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001454 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001455 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001456 }
1457
1458 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001459 rq->base.container_mibqp = qp;
1460
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001461 if (qp->flags & IB_QP_CREATE_CVLAN_STRIPPING)
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001462 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001463 if (qp->flags & IB_QP_CREATE_PCI_WRITE_END_PADDING)
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001464 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
Yishai Hadas34d57582018-09-20 21:39:21 +03001465 err = create_raw_packet_qp_rq(dev, rq, in, inlen, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001466 if (err)
1467 goto err_destroy_sq;
1468
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001469 err = create_raw_packet_qp_tir(dev, rq, tdn, &qp->flags_en, pd,
1470 out);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001471 if (err)
1472 goto err_destroy_rq;
Yishai Hadas7f720522018-09-20 21:45:18 +03001473
1474 if (uid) {
1475 resp->rqn = rq->base.mqp.qpn;
1476 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_RQN;
1477 resp->tirn = rq->tirn;
1478 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001479 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1480 resp->tir_icm_addr = MLX5_GET(
1481 create_tir_out, out, icm_address_31_0);
1482 resp->tir_icm_addr |=
1483 (u64)MLX5_GET(create_tir_out, out,
1484 icm_address_39_32)
1485 << 32;
1486 resp->tir_icm_addr |=
1487 (u64)MLX5_GET(create_tir_out, out,
1488 icm_address_63_40)
1489 << 40;
1490 resp->comp_mask |=
1491 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1492 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001493 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001494 }
1495
1496 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1497 rq->base.mqp.qpn;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001498 return 0;
1499
1500err_destroy_rq:
1501 destroy_raw_packet_qp_rq(dev, rq);
1502err_destroy_sq:
1503 if (!qp->sq.wqe_cnt)
1504 return err;
1505 destroy_raw_packet_qp_sq(dev, sq);
1506err_destroy_tis:
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001507 destroy_raw_packet_qp_tis(dev, sq, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001508
1509 return err;
1510}
1511
1512static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1513 struct mlx5_ib_qp *qp)
1514{
1515 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1516 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1517 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1518
1519 if (qp->rq.wqe_cnt) {
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001520 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001521 destroy_raw_packet_qp_rq(dev, rq);
1522 }
1523
1524 if (qp->sq.wqe_cnt) {
1525 destroy_raw_packet_qp_sq(dev, sq);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001526 destroy_raw_packet_qp_tis(dev, sq, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001527 }
1528}
1529
1530static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1531 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1532{
1533 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1534 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1535
1536 sq->sq = &qp->sq;
1537 rq->rq = &qp->rq;
1538 sq->doorbell = &qp->db;
1539 rq->doorbell = &qp->db;
1540}
1541
Yishai Hadas28d61372016-05-23 15:20:56 +03001542static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1543{
Mark Bloch0042f9e2018-09-17 13:30:49 +03001544 if (qp->flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1545 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1546 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001547 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1548 to_mpd(qp->ibqp.pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001549}
1550
Leon Romanovskyf78d3582020-04-27 18:46:30 +03001551struct mlx5_create_qp_params {
1552 struct ib_udata *udata;
1553 size_t inlen;
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03001554 size_t outlen;
Leon Romanovskye3830852020-05-26 14:54:35 +03001555 size_t ucmd_size;
Leon Romanovskyf78d3582020-04-27 18:46:30 +03001556 void *ucmd;
1557 u8 is_rss_raw : 1;
1558 struct ib_qp_init_attr *attr;
1559 u32 uidx;
Leon Romanovsky08d539762020-04-27 18:46:33 +03001560 struct mlx5_ib_create_qp_resp resp;
Leon Romanovskyf78d3582020-04-27 18:46:30 +03001561};
1562
1563static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1564 struct mlx5_ib_qp *qp,
1565 struct mlx5_create_qp_params *params)
Yishai Hadas28d61372016-05-23 15:20:56 +03001566{
Leon Romanovskyf78d3582020-04-27 18:46:30 +03001567 struct ib_qp_init_attr *init_attr = params->attr;
1568 struct mlx5_ib_create_qp_rss *ucmd = params->ucmd;
1569 struct ib_udata *udata = params->udata;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001570 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1571 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas28d61372016-05-23 15:20:56 +03001572 int inlen;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001573 int outlen;
Yishai Hadas28d61372016-05-23 15:20:56 +03001574 int err;
1575 u32 *in;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001576 u32 *out;
Yishai Hadas28d61372016-05-23 15:20:56 +03001577 void *tirc;
1578 void *hfso;
1579 u32 selected_fields = 0;
Matan Barak2d93fc82018-03-28 09:27:55 +03001580 u32 outer_l4;
Yishai Hadas28d61372016-05-23 15:20:56 +03001581 u32 tdn = mucontext->tdn;
Mark Bloch175edba2018-09-17 13:30:48 +03001582 u8 lb_flag = 0;
Yishai Hadas28d61372016-05-23 15:20:56 +03001583
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001584 if (ucmd->comp_mask) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001585 mlx5_ib_dbg(dev, "invalid comp mask\n");
1586 return -EOPNOTSUPP;
1587 }
1588
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001589 if (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1590 !(ucmd->flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
Maor Gottlieb309fa342017-10-19 08:25:56 +03001591 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1592 return -EOPNOTSUPP;
1593 }
1594
Leon Romanovsky37518fa2020-04-27 18:46:18 +03001595 if (dev->is_rep)
Mark Bloch175edba2018-09-17 13:30:48 +03001596 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
Mark Bloch175edba2018-09-17 13:30:48 +03001597
Leon Romanovsky37518fa2020-04-27 18:46:18 +03001598 if (qp->flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
1599 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1600
1601 if (qp->flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
Mark Bloch175edba2018-09-17 13:30:48 +03001602 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
Mark Bloch175edba2018-09-17 13:30:48 +03001603
Yishai Hadas28d61372016-05-23 15:20:56 +03001604 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001605 outlen = MLX5_ST_SZ_BYTES(create_tir_out);
1606 in = kvzalloc(inlen + outlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001607 if (!in)
1608 return -ENOMEM;
1609
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001610 out = in + MLX5_ST_SZ_DW(create_tir_in);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001611 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001612 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1613 MLX5_SET(tirc, tirc, disp_type,
1614 MLX5_TIRC_DISP_TYPE_INDIRECT);
1615 MLX5_SET(tirc, tirc, indirect_table,
1616 init_attr->rwq_ind_tbl->ind_tbl_num);
1617 MLX5_SET(tirc, tirc, transport_domain, tdn);
1618
1619 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001620
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001621 if (ucmd->flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001622 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1623
Mark Bloch175edba2018-09-17 13:30:48 +03001624 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
1625
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001626 if (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_INNER)
Maor Gottlieb309fa342017-10-19 08:25:56 +03001627 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1628 else
1629 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1630
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001631 switch (ucmd->rx_hash_function) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001632 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1633 {
1634 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1635 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1636
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001637 if (len != ucmd->rx_key_len) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001638 err = -EINVAL;
1639 goto err;
1640 }
1641
1642 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001643 memcpy(rss_key, ucmd->rx_hash_key, len);
Yishai Hadas28d61372016-05-23 15:20:56 +03001644 break;
1645 }
1646 default:
1647 err = -EOPNOTSUPP;
1648 goto err;
1649 }
1650
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001651 if (!ucmd->rx_hash_fields_mask) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001652 /* special case when this TIR serves as steering entry without hashing */
1653 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1654 goto create_tir;
1655 err = -EINVAL;
1656 goto err;
1657 }
1658
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001659 if (((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1660 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1661 ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1662 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001663 err = -EINVAL;
1664 goto err;
1665 }
1666
1667 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001668 if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1669 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
Yishai Hadas28d61372016-05-23 15:20:56 +03001670 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1671 MLX5_L3_PROT_TYPE_IPV4);
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001672 else if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1673 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
Yishai Hadas28d61372016-05-23 15:20:56 +03001674 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1675 MLX5_L3_PROT_TYPE_IPV6);
1676
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001677 outer_l4 = ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1678 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1679 << 0 |
1680 ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1681 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1682 << 1 |
1683 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI) << 2;
Matan Barak2d93fc82018-03-28 09:27:55 +03001684
1685 /* Check that only one l4 protocol is set */
1686 if (outer_l4 & (outer_l4 - 1)) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001687 err = -EINVAL;
1688 goto err;
1689 }
1690
1691 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001692 if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1693 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
Yishai Hadas28d61372016-05-23 15:20:56 +03001694 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1695 MLX5_L4_PROT_TYPE_TCP);
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001696 else if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1697 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
Yishai Hadas28d61372016-05-23 15:20:56 +03001698 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1699 MLX5_L4_PROT_TYPE_UDP);
1700
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001701 if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1702 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
Yishai Hadas28d61372016-05-23 15:20:56 +03001703 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1704
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001705 if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1706 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
Yishai Hadas28d61372016-05-23 15:20:56 +03001707 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1708
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001709 if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1710 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
Yishai Hadas28d61372016-05-23 15:20:56 +03001711 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1712
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001713 if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1714 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
Yishai Hadas28d61372016-05-23 15:20:56 +03001715 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1716
Leon Romanovsky5ce05922020-04-27 18:46:22 +03001717 if (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI)
Matan Barak2d93fc82018-03-28 09:27:55 +03001718 selected_fields |= MLX5_HASH_FIELD_SEL_IPSEC_SPI;
1719
Yishai Hadas28d61372016-05-23 15:20:56 +03001720 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1721
1722create_tir:
Leon Romanovskye0b4b472020-04-09 21:03:33 +03001723 MLX5_SET(create_tir_in, in, opcode, MLX5_CMD_OP_CREATE_TIR);
1724 err = mlx5_cmd_exec_inout(dev->mdev, create_tir, in, out);
Yishai Hadas28d61372016-05-23 15:20:56 +03001725
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001726 qp->rss_qp.tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001727 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1728 err = mlx5_ib_enable_lb(dev, false, true);
1729
1730 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001731 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1732 to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001733 }
1734
Yishai Hadas28d61372016-05-23 15:20:56 +03001735 if (err)
1736 goto err;
1737
Yishai Hadas7f720522018-09-20 21:45:18 +03001738 if (mucontext->devx_uid) {
Leon Romanovsky08d539762020-04-27 18:46:33 +03001739 params->resp.comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1740 params->resp.tirn = qp->rss_qp.tirn;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001741 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
Leon Romanovsky08d539762020-04-27 18:46:33 +03001742 params->resp.tir_icm_addr =
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001743 MLX5_GET(create_tir_out, out, icm_address_31_0);
Leon Romanovsky08d539762020-04-27 18:46:33 +03001744 params->resp.tir_icm_addr |=
1745 (u64)MLX5_GET(create_tir_out, out,
1746 icm_address_39_32)
1747 << 32;
1748 params->resp.tir_icm_addr |=
1749 (u64)MLX5_GET(create_tir_out, out,
1750 icm_address_63_40)
1751 << 40;
1752 params->resp.comp_mask |=
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001753 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1754 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001755 }
1756
Yishai Hadas28d61372016-05-23 15:20:56 +03001757 kvfree(in);
1758 /* qpn is reserved for that QP */
1759 qp->trans_qp.base.mqp.qpn = 0;
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001760 qp->is_rss = true;
Yishai Hadas28d61372016-05-23 15:20:56 +03001761 return 0;
1762
1763err:
1764 kvfree(in);
1765 return err;
1766}
1767
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001768static void configure_requester_scat_cqe(struct mlx5_ib_dev *dev,
1769 struct ib_qp_init_attr *init_attr,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001770 struct mlx5_ib_create_qp *ucmd,
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001771 void *qpc)
1772{
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001773 int scqe_sz;
zhengbin2ab367a2019-12-24 16:40:12 +08001774 bool allow_scat_cqe = false;
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001775
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001776 if (ucmd)
1777 allow_scat_cqe = ucmd->flags & MLX5_QP_FLAG_ALLOW_SCATTER_CQE;
1778
1779 if (!allow_scat_cqe && init_attr->sq_sig_type != IB_SIGNAL_ALL_WR)
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001780 return;
1781
1782 scqe_sz = mlx5_ib_get_cqe_size(init_attr->send_cq);
1783 if (scqe_sz == 128) {
1784 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
1785 return;
1786 }
1787
1788 if (init_attr->qp_type != MLX5_IB_QPT_DCI ||
1789 MLX5_CAP_GEN(dev->mdev, dc_req_scat_data_cqe))
1790 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
1791}
1792
Yonatan Cohena60109d2018-10-10 09:25:16 +03001793static int atomic_size_to_mode(int size_mask)
1794{
1795 /* driver does not support atomic_size > 256B
1796 * and does not know how to translate bigger sizes
1797 */
1798 int supported_size_mask = size_mask & 0x1ff;
1799 int log_max_size;
1800
1801 if (!supported_size_mask)
1802 return -EOPNOTSUPP;
1803
1804 log_max_size = __fls(supported_size_mask);
1805
1806 if (log_max_size > 3)
1807 return log_max_size;
1808
1809 return MLX5_ATOMIC_MODE_8B;
1810}
1811
1812static int get_atomic_mode(struct mlx5_ib_dev *dev,
1813 enum ib_qp_type qp_type)
1814{
1815 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
1816 u8 atomic = MLX5_CAP_GEN(dev->mdev, atomic);
1817 int atomic_mode = -EOPNOTSUPP;
1818 int atomic_size_mask;
1819
1820 if (!atomic)
1821 return -EOPNOTSUPP;
1822
1823 if (qp_type == MLX5_IB_QPT_DCT)
1824 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
1825 else
1826 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
1827
1828 if ((atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_CMP_SWAP) ||
1829 (atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_FETCH_ADD))
1830 atomic_mode = atomic_size_to_mode(atomic_size_mask);
1831
1832 if (atomic_mode <= 0 &&
1833 (atomic_operations & MLX5_ATOMIC_OPS_CMP_SWAP &&
1834 atomic_operations & MLX5_ATOMIC_OPS_FETCH_ADD))
1835 atomic_mode = MLX5_ATOMIC_MODE_IB_COMP;
1836
1837 return atomic_mode;
1838}
1839
Leon Romanovskyf78d3582020-04-27 18:46:30 +03001840static int create_xrc_tgt_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1841 struct mlx5_create_qp_params *params)
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03001842{
Leon Romanovskye3830852020-05-26 14:54:35 +03001843 struct mlx5_ib_create_qp *ucmd = params->ucmd;
Leon Romanovskyf78d3582020-04-27 18:46:30 +03001844 struct ib_qp_init_attr *attr = params->attr;
Leon Romanovskyf78d3582020-04-27 18:46:30 +03001845 u32 uidx = params->uidx;
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03001846 struct mlx5_ib_resources *devr = &dev->devr;
Leon Romanovsky3e09a422020-05-26 14:54:34 +03001847 u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {};
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03001848 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
1849 struct mlx5_core_dev *mdev = dev->mdev;
1850 struct mlx5_ib_qp_base *base;
1851 unsigned long flags;
1852 void *qpc;
1853 u32 *in;
1854 int err;
1855
1856 mutex_init(&qp->mutex);
1857
1858 if (attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1859 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1860
1861 in = kvzalloc(inlen, GFP_KERNEL);
1862 if (!in)
1863 return -ENOMEM;
1864
Leon Romanovskye3830852020-05-26 14:54:35 +03001865 if (MLX5_CAP_GEN(mdev, ece_support))
1866 MLX5_SET(create_qp_in, in, ece, ucmd->ece_options);
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03001867 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1868
1869 MLX5_SET(qpc, qpc, st, MLX5_QP_ST_XRC);
1870 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
1871 MLX5_SET(qpc, qpc, pd, to_mpd(devr->p0)->pdn);
1872
1873 if (qp->flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
1874 MLX5_SET(qpc, qpc, block_lb_mc, 1);
1875 if (qp->flags & IB_QP_CREATE_CROSS_CHANNEL)
1876 MLX5_SET(qpc, qpc, cd_master, 1);
1877 if (qp->flags & IB_QP_CREATE_MANAGED_SEND)
1878 MLX5_SET(qpc, qpc, cd_slave_send, 1);
1879 if (qp->flags & IB_QP_CREATE_MANAGED_RECV)
1880 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
1881
1882 MLX5_SET(qpc, qpc, rq_type, MLX5_SRQ_RQ);
1883 MLX5_SET(qpc, qpc, no_sq, 1);
1884 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1885 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1886 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1887 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(attr->xrcd)->xrcdn);
1888 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
1889
1890 /* 0xffffff means we ask to work with cqe version 0 */
1891 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
1892 MLX5_SET(qpc, qpc, user_index, uidx);
1893
1894 if (qp->flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
1895 MLX5_SET(qpc, qpc, end_padding_mode,
1896 MLX5_WQ_END_PAD_MODE_ALIGN);
1897 /* Special case to clean flag */
1898 qp->flags &= ~IB_QP_CREATE_PCI_WRITE_END_PADDING;
1899 }
1900
1901 base = &qp->trans_qp.base;
Leon Romanovsky3e09a422020-05-26 14:54:34 +03001902 err = mlx5_qpc_create_qp(dev, &base->mqp, in, inlen, out);
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03001903 kvfree(in);
Leon Romanovsky6367da42020-04-27 18:46:34 +03001904 if (err)
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03001905 return err;
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03001906
1907 base->container_mibqp = qp;
1908 base->mqp.event = mlx5_ib_qp_event;
Leon Romanovsky92cd6672020-06-02 15:55:47 +03001909 if (MLX5_CAP_GEN(mdev, ece_support))
1910 params->resp.ece_options = MLX5_GET(create_qp_out, out, ece);
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03001911
1912 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1913 list_add_tail(&qp->qps_list, &dev->qp_list);
1914 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1915
Leon Romanovsky968f0b62020-04-27 18:46:35 +03001916 qp->trans_qp.xrcdn = to_mxrcd(attr->xrcd)->xrcdn;
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03001917 return 0;
1918}
1919
Leon Romanovsky98fc1122020-04-27 18:46:28 +03001920static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Leon Romanovskyf78d3582020-04-27 18:46:30 +03001921 struct mlx5_ib_qp *qp,
1922 struct mlx5_create_qp_params *params)
Eli Cohene126ba92013-07-07 17:25:49 +03001923{
Leon Romanovskyf78d3582020-04-27 18:46:30 +03001924 struct ib_qp_init_attr *init_attr = params->attr;
1925 struct mlx5_ib_create_qp *ucmd = params->ucmd;
Leon Romanovsky3e09a422020-05-26 14:54:34 +03001926 u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {};
Leon Romanovskyf78d3582020-04-27 18:46:30 +03001927 struct ib_udata *udata = params->udata;
1928 u32 uidx = params->uidx;
Eli Cohene126ba92013-07-07 17:25:49 +03001929 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001930 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001931 struct mlx5_core_dev *mdev = dev->mdev;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001932 struct mlx5_ib_cq *send_cq;
1933 struct mlx5_ib_cq *recv_cq;
1934 unsigned long flags;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001935 struct mlx5_ib_qp_base *base;
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001936 int mlx5_st;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001937 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001938 u32 *in;
1939 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001940
1941 mutex_init(&qp->mutex);
1942 spin_lock_init(&qp->sq.lock);
1943 spin_lock_init(&qp->rq.lock);
1944
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03001945 mlx5_st = to_mlx5_st(qp->type);
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001946 if (mlx5_st < 0)
1947 return -EINVAL;
1948
Eli Cohene126ba92013-07-07 17:25:49 +03001949 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1950 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1951
Leon Romanovsky29789752020-04-27 18:46:14 +03001952 if (qp->flags & IB_QP_CREATE_SOURCE_QPN)
1953 qp->underlay_qpn = init_attr->source_qpn;
1954
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001955 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001956 qp->flags & IB_QP_CREATE_SOURCE_QPN) ?
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001957 &qp->raw_packet_qp.rq.base :
1958 &qp->trans_qp.base;
1959
Eli Cohene126ba92013-07-07 17:25:49 +03001960 qp->has_rq = qp_has_rq(init_attr);
Leon Romanovsky2dfac922020-04-27 18:46:11 +03001961 err = set_rq_size(dev, &init_attr->cap, qp->has_rq, qp, ucmd);
Eli Cohene126ba92013-07-07 17:25:49 +03001962 if (err) {
1963 mlx5_ib_dbg(dev, "err %d\n", err);
1964 return err;
1965 }
1966
Leon Romanovsky98fc1122020-04-27 18:46:28 +03001967 if (ucmd->rq_wqe_shift != qp->rq.wqe_shift ||
1968 ucmd->rq_wqe_count != qp->rq.wqe_cnt)
1969 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001970
Leon Romanovsky98fc1122020-04-27 18:46:28 +03001971 if (ucmd->sq_wqe_count > (1 << MLX5_CAP_GEN(mdev, log_max_qp_sz)))
1972 return -EINVAL;
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03001973
Leon Romanovsky08d539762020-04-27 18:46:33 +03001974 err = _create_user_qp(dev, pd, qp, udata, init_attr, &in, &params->resp,
1975 &inlen, base, ucmd);
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03001976 if (err)
1977 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001978
1979 if (is_sqp(init_attr->qp_type))
1980 qp->port = init_attr->port_num;
1981
Leon Romanovskye3830852020-05-26 14:54:35 +03001982 if (MLX5_CAP_GEN(mdev, ece_support))
1983 MLX5_SET(create_qp_in, in, ece, ucmd->ece_options);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001984 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1985
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001986 MLX5_SET(qpc, qpc, st, mlx5_st);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001987 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Leon Romanovsky98fc1122020-04-27 18:46:28 +03001988 MLX5_SET(qpc, qpc, pd, to_mpd(pd)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001989
Leon Romanovskyc95e6d52020-04-27 18:46:15 +03001990 if (qp->flags_en & MLX5_QP_FLAG_SIGNATURE)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001991 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001992
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001993 if (qp->flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001994 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001995
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001996 if (qp->flags & IB_QP_CREATE_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001997 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky2be08c32020-04-27 18:46:13 +03001998 if (qp->flags & IB_QP_CREATE_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001999 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002000 if (qp->flags & IB_QP_CREATE_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002001 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002002 if (qp->flags_en & MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE)
Danit Goldberg569c6652018-11-30 13:22:05 +02002003 MLX5_SET(qpc, qpc, req_e2e_credit_mode, 1);
Leon Romanovsky90ecb372020-04-27 18:46:16 +03002004 if ((qp->flags_en & MLX5_QP_FLAG_SCATTER_CQE) &&
2005 (init_attr->qp_type == IB_QPT_RC ||
2006 init_attr->qp_type == IB_QPT_UC)) {
Colin Ian King52c81f42020-05-07 16:16:10 +01002007 int rcqe_sz = mlx5_ib_get_cqe_size(init_attr->recv_cq);
Leon Romanovsky8bde2c52020-04-27 18:46:09 +03002008
2009 MLX5_SET(qpc, qpc, cs_res,
2010 rcqe_sz == 128 ? MLX5_RES_SCAT_DATA64_CQE :
2011 MLX5_RES_SCAT_DATA32_CQE);
2012 }
Leon Romanovsky90ecb372020-04-27 18:46:16 +03002013 if ((qp->flags_en & MLX5_QP_FLAG_SCATTER_CQE) &&
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002014 (qp->type == MLX5_IB_QPT_DCI || qp->type == IB_QPT_RC))
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002015 configure_requester_scat_cqe(dev, init_attr, ucmd, qpc);
Eli Cohene126ba92013-07-07 17:25:49 +03002016
2017 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002018 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
2019 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03002020 }
2021
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002022 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03002023
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002024 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002025 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002026 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002027 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002028 if (init_attr->srq &&
2029 init_attr->srq->srq_type == IB_SRQT_TM)
2030 MLX5_SET(qpc, qpc, offload_type,
2031 MLX5_QPC_OFFLOAD_TYPE_RNDV);
2032 }
Eli Cohene126ba92013-07-07 17:25:49 +03002033
2034 /* Set default resources */
2035 switch (init_attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002036 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002037 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2038 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2039 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002040 break;
2041 default:
2042 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002043 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
2044 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002045 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002046 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2047 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002048 }
2049 }
2050
2051 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002052 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002053
2054 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002055 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002056
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002057 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03002058
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002059 /* 0xffffff means we ask to work with cqe version 0 */
2060 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002061 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002062
Leon Romanovsky29789752020-04-27 18:46:14 +03002063 if (qp->flags & IB_QP_CREATE_PCI_WRITE_END_PADDING &&
2064 init_attr->qp_type != IB_QPT_RAW_PACKET) {
2065 MLX5_SET(qpc, qpc, end_padding_mode,
2066 MLX5_WQ_END_PAD_MODE_ALIGN);
2067 /* Special case to clean flag */
2068 qp->flags &= ~IB_QP_CREATE_PCI_WRITE_END_PADDING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002069 }
2070
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002071 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002072 qp->flags & IB_QP_CREATE_SOURCE_QPN) {
Leon Romanovsky2dfac922020-04-27 18:46:11 +03002073 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd->sq_buf_addr;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002074 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
Yishai Hadas7f720522018-09-20 21:45:18 +03002075 err = create_raw_packet_qp(dev, qp, in, inlen, pd, udata,
Leon Romanovsky08d539762020-04-27 18:46:33 +03002076 &params->resp);
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03002077 } else
Leon Romanovsky3e09a422020-05-26 14:54:34 +03002078 err = mlx5_qpc_create_qp(dev, &base->mqp, in, inlen, out);
Eli Cohene126ba92013-07-07 17:25:49 +03002079
Al Viro479163f2014-11-20 08:13:57 +00002080 kvfree(in);
Leon Romanovsky04bcc1c2020-04-27 18:46:27 +03002081 if (err)
2082 goto err_create;
Eli Cohene126ba92013-07-07 17:25:49 +03002083
majd@mellanox.com19098df2016-01-14 19:13:03 +02002084 base->container_mibqp = qp;
2085 base->mqp.event = mlx5_ib_qp_event;
Leon Romanovsky92cd6672020-06-02 15:55:47 +03002086 if (MLX5_CAP_GEN(mdev, ece_support))
2087 params->resp.ece_options = MLX5_GET(create_qp_out, out, ece);
Eli Cohene126ba92013-07-07 17:25:49 +03002088
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002089 get_cqs(qp->type, init_attr->send_cq, init_attr->recv_cq,
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002090 &send_cq, &recv_cq);
2091 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2092 mlx5_ib_lock_cqs(send_cq, recv_cq);
2093 /* Maintain device to QPs access, needed for further handling via reset
2094 * flow
2095 */
2096 list_add_tail(&qp->qps_list, &dev->qp_list);
2097 /* Maintain CQ to QPs access, needed for further handling via reset flow
2098 */
2099 if (send_cq)
2100 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
2101 if (recv_cq)
2102 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
2103 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2104 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
2105
Eli Cohene126ba92013-07-07 17:25:49 +03002106 return 0;
2107
2108err_create:
Leon Romanovsky747c5192020-04-27 18:46:29 +03002109 destroy_qp(dev, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002110 return err;
2111}
2112
Leon Romanovsky98fc1122020-04-27 18:46:28 +03002113static int create_kernel_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002114 struct mlx5_ib_qp *qp,
2115 struct mlx5_create_qp_params *params)
Leon Romanovsky98fc1122020-04-27 18:46:28 +03002116{
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002117 struct ib_qp_init_attr *attr = params->attr;
2118 u32 uidx = params->uidx;
Leon Romanovsky98fc1122020-04-27 18:46:28 +03002119 struct mlx5_ib_resources *devr = &dev->devr;
Leon Romanovsky3e09a422020-05-26 14:54:34 +03002120 u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {};
Leon Romanovsky98fc1122020-04-27 18:46:28 +03002121 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
2122 struct mlx5_core_dev *mdev = dev->mdev;
2123 struct mlx5_ib_cq *send_cq;
2124 struct mlx5_ib_cq *recv_cq;
2125 unsigned long flags;
2126 struct mlx5_ib_qp_base *base;
2127 int mlx5_st;
2128 void *qpc;
2129 u32 *in;
2130 int err;
2131
2132 mutex_init(&qp->mutex);
2133 spin_lock_init(&qp->sq.lock);
2134 spin_lock_init(&qp->rq.lock);
2135
2136 mlx5_st = to_mlx5_st(qp->type);
2137 if (mlx5_st < 0)
2138 return -EINVAL;
2139
2140 if (attr->sq_sig_type == IB_SIGNAL_ALL_WR)
2141 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
2142
2143 base = &qp->trans_qp.base;
2144
2145 qp->has_rq = qp_has_rq(attr);
2146 err = set_rq_size(dev, &attr->cap, qp->has_rq, qp, NULL);
2147 if (err) {
2148 mlx5_ib_dbg(dev, "err %d\n", err);
2149 return err;
2150 }
2151
2152 err = _create_kernel_qp(dev, attr, qp, &in, &inlen, base);
2153 if (err)
2154 return err;
2155
2156 if (is_sqp(attr->qp_type))
2157 qp->port = attr->port_num;
2158
2159 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
2160
2161 MLX5_SET(qpc, qpc, st, mlx5_st);
2162 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
2163
2164 if (attr->qp_type != MLX5_IB_QPT_REG_UMR)
2165 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
2166 else
2167 MLX5_SET(qpc, qpc, latency_sensitive, 1);
2168
2169
2170 if (qp->flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
2171 MLX5_SET(qpc, qpc, block_lb_mc, 1);
2172
2173 if (qp->rq.wqe_cnt) {
2174 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
2175 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
2176 }
2177
2178 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, attr));
2179
2180 if (qp->sq.wqe_cnt)
2181 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
2182 else
2183 MLX5_SET(qpc, qpc, no_sq, 1);
2184
2185 if (attr->srq) {
2186 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
2187 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn,
2188 to_msrq(attr->srq)->msrq.srqn);
2189 } else {
2190 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2191 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn,
2192 to_msrq(devr->s1)->msrq.srqn);
2193 }
2194
2195 if (attr->send_cq)
2196 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(attr->send_cq)->mcq.cqn);
2197
2198 if (attr->recv_cq)
2199 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(attr->recv_cq)->mcq.cqn);
2200
2201 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
2202
2203 /* 0xffffff means we ask to work with cqe version 0 */
2204 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
2205 MLX5_SET(qpc, qpc, user_index, uidx);
2206
2207 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
2208 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO)
2209 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
2210
Leon Romanovsky3e09a422020-05-26 14:54:34 +03002211 err = mlx5_qpc_create_qp(dev, &base->mqp, in, inlen, out);
Leon Romanovsky98fc1122020-04-27 18:46:28 +03002212 kvfree(in);
2213 if (err)
2214 goto err_create;
2215
2216 base->container_mibqp = qp;
2217 base->mqp.event = mlx5_ib_qp_event;
2218
2219 get_cqs(qp->type, attr->send_cq, attr->recv_cq,
2220 &send_cq, &recv_cq);
2221 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2222 mlx5_ib_lock_cqs(send_cq, recv_cq);
2223 /* Maintain device to QPs access, needed for further handling via reset
2224 * flow
2225 */
2226 list_add_tail(&qp->qps_list, &dev->qp_list);
2227 /* Maintain CQ to QPs access, needed for further handling via reset flow
2228 */
2229 if (send_cq)
2230 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
2231 if (recv_cq)
2232 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
2233 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2234 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
2235
2236 return 0;
2237
2238err_create:
Leon Romanovsky747c5192020-04-27 18:46:29 +03002239 destroy_qp(dev, qp, base, NULL);
Leon Romanovsky98fc1122020-04-27 18:46:28 +03002240 return err;
2241}
2242
Eli Cohene126ba92013-07-07 17:25:49 +03002243static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2244 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
2245{
2246 if (send_cq) {
2247 if (recv_cq) {
2248 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002249 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002250 spin_lock_nested(&recv_cq->lock,
2251 SINGLE_DEPTH_NESTING);
2252 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002253 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002254 __acquire(&recv_cq->lock);
2255 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002256 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002257 spin_lock_nested(&send_cq->lock,
2258 SINGLE_DEPTH_NESTING);
2259 }
2260 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002261 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002262 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002263 }
2264 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002265 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002266 __acquire(&send_cq->lock);
2267 } else {
2268 __acquire(&send_cq->lock);
2269 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002270 }
2271}
2272
2273static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2274 __releases(&send_cq->lock) __releases(&recv_cq->lock)
2275{
2276 if (send_cq) {
2277 if (recv_cq) {
2278 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
2279 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002280 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002281 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
2282 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002283 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002284 } else {
2285 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002286 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002287 }
2288 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002289 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002290 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002291 }
2292 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002293 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002294 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002295 } else {
2296 __release(&recv_cq->lock);
2297 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002298 }
2299}
2300
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002301static void get_cqs(enum ib_qp_type qp_type,
2302 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03002303 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
2304{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002305 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002306 case IB_QPT_XRC_TGT:
2307 *send_cq = NULL;
2308 *recv_cq = NULL;
2309 break;
2310 case MLX5_IB_QPT_REG_UMR:
2311 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002312 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002313 *recv_cq = NULL;
2314 break;
2315
2316 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002317 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002318 case IB_QPT_RC:
2319 case IB_QPT_UC:
2320 case IB_QPT_UD:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002321 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002322 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2323 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002324 break;
Eli Cohene126ba92013-07-07 17:25:49 +03002325 default:
2326 *send_cq = NULL;
2327 *recv_cq = NULL;
2328 break;
2329 }
2330}
2331
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002332static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002333 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2334 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002335
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002336static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2337 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002338{
2339 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002340 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002341 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002342 int err;
2343
Leon Romanovsky6c419652020-06-17 16:01:48 +03002344 if (qp->is_rss) {
Yishai Hadas28d61372016-05-23 15:20:56 +03002345 destroy_rss_raw_qp_tir(dev, qp);
2346 return;
2347 }
2348
Leon Romanovsky6c419652020-06-17 16:01:48 +03002349 base = (qp->type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002350 qp->flags & IB_QP_CREATE_SOURCE_QPN) ?
Leon Romanovsky6c419652020-06-17 16:01:48 +03002351 &qp->raw_packet_qp.rq.base :
2352 &qp->trans_qp.base;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002353
Haggai Eran6aec21f2014-12-11 17:04:23 +02002354 if (qp->state != IB_QPS_RESET) {
Leon Romanovsky6c419652020-06-17 16:01:48 +03002355 if (qp->type != IB_QPT_RAW_PACKET &&
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002356 !(qp->flags & IB_QP_CREATE_SOURCE_QPN)) {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03002357 err = mlx5_core_qp_modify(dev, MLX5_CMD_OP_2RST_QP, 0,
Leon Romanovsky5f62a522020-05-26 14:54:39 +03002358 NULL, &base->mqp, NULL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002359 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002360 struct mlx5_modify_raw_qp_param raw_qp_param = {
2361 .operation = MLX5_CMD_OP_2RST_QP
2362 };
2363
Aviv Heller13eab212016-09-18 20:48:04 +03002364 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002365 }
2366 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002367 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002368 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002369 }
Eli Cohene126ba92013-07-07 17:25:49 +03002370
Leon Romanovsky6c419652020-06-17 16:01:48 +03002371 get_cqs(qp->type, qp->ibqp.send_cq, qp->ibqp.recv_cq, &send_cq,
2372 &recv_cq);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002373
2374 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2375 mlx5_ib_lock_cqs(send_cq, recv_cq);
2376 /* del from lists under both locks above to protect reset flow paths */
2377 list_del(&qp->qps_list);
2378 if (send_cq)
2379 list_del(&qp->cq_send_list);
2380
2381 if (recv_cq)
2382 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002383
Leon Romanovsky03c40772020-04-27 18:46:24 +03002384 if (!udata) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002385 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002386 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2387 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002388 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2389 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002390 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002391 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2392 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002393
Leon Romanovsky6c419652020-06-17 16:01:48 +03002394 if (qp->type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002395 qp->flags & IB_QP_CREATE_SOURCE_QPN) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002396 destroy_raw_packet_qp(dev, qp);
2397 } else {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03002398 err = mlx5_core_destroy_qp(dev, &base->mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002399 if (err)
2400 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2401 base->mqp.qpn);
2402 }
Eli Cohene126ba92013-07-07 17:25:49 +03002403
Leon Romanovsky747c5192020-04-27 18:46:29 +03002404 destroy_qp(dev, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002405}
2406
Leon Romanovskya645a892020-06-02 15:55:48 +03002407static int create_dct(struct mlx5_ib_dev *dev, struct ib_pd *pd,
2408 struct mlx5_ib_qp *qp,
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002409 struct mlx5_create_qp_params *params)
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002410{
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002411 struct ib_qp_init_attr *attr = params->attr;
2412 struct mlx5_ib_create_qp *ucmd = params->ucmd;
2413 u32 uidx = params->uidx;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002414 void *dctc;
2415
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002416 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002417 if (!qp->dct.in)
Leon Romanovsky47c80612020-04-27 18:46:07 +03002418 return -ENOMEM;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002419
Yishai Hadasa01a5862018-09-20 21:39:24 +03002420 MLX5_SET(create_dct_in, qp->dct.in, uid, to_mpd(pd)->uid);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002421 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002422 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2423 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2424 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2425 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2426 MLX5_SET(dctc, dctc, user_index, uidx);
Leon Romanovskya645a892020-06-02 15:55:48 +03002427 if (MLX5_CAP_GEN(dev->mdev, ece_support))
2428 MLX5_SET(dctc, dctc, ece, ucmd->ece_options);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002429
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002430 if (qp->flags_en & MLX5_QP_FLAG_SCATTER_CQE) {
Leon Romanovskyfd9dab72020-04-27 18:46:08 +03002431 int rcqe_sz = mlx5_ib_get_cqe_size(attr->recv_cq);
2432
2433 if (rcqe_sz == 128)
2434 MLX5_SET(dctc, dctc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
2435 }
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002436
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002437 qp->state = IB_QPS_RESET;
2438
Leon Romanovsky47c80612020-04-27 18:46:07 +03002439 return 0;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002440}
2441
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002442static int check_qp_type(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
2443 enum ib_qp_type *type)
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002444{
2445 if (attr->qp_type == IB_QPT_DRIVER && !MLX5_CAP_GEN(dev->mdev, dct))
2446 goto out;
2447
2448 switch (attr->qp_type) {
2449 case IB_QPT_XRC_TGT:
2450 case IB_QPT_XRC_INI:
2451 if (!MLX5_CAP_GEN(dev->mdev, xrc))
2452 goto out;
2453 fallthrough;
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002454 case IB_QPT_RC:
2455 case IB_QPT_UC:
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002456 case IB_QPT_SMI:
2457 case MLX5_IB_QPT_HW_GSI:
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002458 case IB_QPT_DRIVER:
2459 case IB_QPT_GSI:
Mark Bloch42caf9c2020-05-06 10:16:02 +03002460 if (dev->profile == &raw_eth_profile)
2461 goto out;
2462 case IB_QPT_RAW_PACKET:
2463 case IB_QPT_UD:
2464 case MLX5_IB_QPT_REG_UMR:
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002465 break;
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002466 default:
2467 goto out;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002468 }
2469
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002470 *type = attr->qp_type;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002471 return 0;
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002472
2473out:
2474 mlx5_ib_dbg(dev, "Unsupported QP type %d\n", attr->qp_type);
2475 return -EOPNOTSUPP;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002476}
2477
Leon Romanovsky2242cc22020-04-27 18:46:03 +03002478static int check_valid_flow(struct mlx5_ib_dev *dev, struct ib_pd *pd,
2479 struct ib_qp_init_attr *attr,
2480 struct ib_udata *udata)
2481{
2482 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2483 udata, struct mlx5_ib_ucontext, ibucontext);
2484
2485 if (!udata) {
2486 /* Kernel create_qp callers */
2487 if (attr->rwq_ind_tbl)
2488 return -EOPNOTSUPP;
2489
2490 switch (attr->qp_type) {
2491 case IB_QPT_RAW_PACKET:
2492 case IB_QPT_DRIVER:
2493 return -EOPNOTSUPP;
2494 default:
2495 return 0;
2496 }
2497 }
2498
2499 /* Userspace create_qp callers */
2500 if (attr->qp_type == IB_QPT_RAW_PACKET && !ucontext->cqe_version) {
2501 mlx5_ib_dbg(dev,
2502 "Raw Packet QP is only supported for CQE version > 0\n");
2503 return -EINVAL;
2504 }
2505
2506 if (attr->qp_type != IB_QPT_RAW_PACKET && attr->rwq_ind_tbl) {
2507 mlx5_ib_dbg(dev,
2508 "Wrong QP type %d for the RWQ indirect table\n",
2509 attr->qp_type);
2510 return -EINVAL;
2511 }
2512
2513 switch (attr->qp_type) {
2514 case IB_QPT_SMI:
2515 case MLX5_IB_QPT_HW_GSI:
2516 case MLX5_IB_QPT_REG_UMR:
2517 case IB_QPT_GSI:
2518 mlx5_ib_dbg(dev, "Kernel doesn't support QP type %d\n",
2519 attr->qp_type);
2520 return -EINVAL;
2521 default:
2522 break;
2523 }
2524
2525 /*
2526 * We don't need to see this warning, it means that kernel code
2527 * missing ib_pd. Placed here to catch developer's mistakes.
2528 */
2529 WARN_ONCE(!pd && attr->qp_type != IB_QPT_XRC_TGT,
2530 "There is a missing PD pointer assignment\n");
2531 return 0;
2532}
2533
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002534static void process_vendor_flag(struct mlx5_ib_dev *dev, int *flags, int flag,
2535 bool cond, struct mlx5_ib_qp *qp)
2536{
2537 if (!(*flags & flag))
2538 return;
2539
2540 if (cond) {
2541 qp->flags_en |= flag;
2542 *flags &= ~flag;
2543 return;
2544 }
2545
2546 if (flag == MLX5_QP_FLAG_SCATTER_CQE) {
2547 /*
2548 * We don't return error if this flag was provided,
2549 * and mlx5 doesn't have right capability.
2550 */
2551 *flags &= ~MLX5_QP_FLAG_SCATTER_CQE;
2552 return;
2553 }
2554 mlx5_ib_dbg(dev, "Vendor create QP flag 0x%X is not supported\n", flag);
2555}
2556
2557static int process_vendor_flags(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002558 void *ucmd, struct ib_qp_init_attr *attr)
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002559{
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002560 struct mlx5_core_dev *mdev = dev->mdev;
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002561 bool cond;
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002562 int flags;
2563
2564 if (attr->rwq_ind_tbl)
2565 flags = ((struct mlx5_ib_create_qp_rss *)ucmd)->flags;
2566 else
2567 flags = ((struct mlx5_ib_create_qp *)ucmd)->flags;
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002568
2569 switch (flags & (MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI)) {
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002570 case MLX5_QP_FLAG_TYPE_DCI:
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002571 qp->type = MLX5_IB_QPT_DCI;
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002572 break;
2573 case MLX5_QP_FLAG_TYPE_DCT:
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002574 qp->type = MLX5_IB_QPT_DCT;
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002575 break;
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002576 default:
2577 if (qp->type != IB_QPT_DRIVER)
2578 break;
2579 /*
2580 * It is IB_QPT_DRIVER and or no subtype or
2581 * wrong subtype were provided.
2582 */
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002583 return -EINVAL;
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002584 }
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002585
2586 process_vendor_flag(dev, &flags, MLX5_QP_FLAG_TYPE_DCI, true, qp);
2587 process_vendor_flag(dev, &flags, MLX5_QP_FLAG_TYPE_DCT, true, qp);
2588
2589 process_vendor_flag(dev, &flags, MLX5_QP_FLAG_SIGNATURE, true, qp);
2590 process_vendor_flag(dev, &flags, MLX5_QP_FLAG_SCATTER_CQE,
2591 MLX5_CAP_GEN(mdev, sctr_data_cqe), qp);
2592
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002593 if (qp->type == IB_QPT_RAW_PACKET) {
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002594 cond = MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan) ||
2595 MLX5_CAP_ETH(mdev, tunnel_stateless_gre) ||
2596 MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx);
2597 process_vendor_flag(dev, &flags, MLX5_QP_FLAG_TUNNEL_OFFLOADS,
2598 cond, qp);
2599 process_vendor_flag(dev, &flags,
2600 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC, true,
2601 qp);
2602 process_vendor_flag(dev, &flags,
2603 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC, true,
2604 qp);
2605 }
2606
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002607 if (qp->type == IB_QPT_RC)
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002608 process_vendor_flag(dev, &flags,
2609 MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE,
2610 MLX5_CAP_GEN(mdev, qp_packet_based), qp);
2611
Leon Romanovsky76883a62020-04-27 18:46:23 +03002612 process_vendor_flag(dev, &flags, MLX5_QP_FLAG_BFREG_INDEX, true, qp);
2613 process_vendor_flag(dev, &flags, MLX5_QP_FLAG_UAR_PAGE_INDEX, true, qp);
2614
Leon Romanovsky5d6fffe2020-04-27 18:46:31 +03002615 cond = qp->flags_en & ~(MLX5_QP_FLAG_TUNNEL_OFFLOADS |
2616 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
2617 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC);
2618 if (attr->rwq_ind_tbl && cond) {
2619 mlx5_ib_dbg(dev, "RSS RAW QP has unsupported flags 0x%X\n",
2620 cond);
2621 return -EINVAL;
2622 }
2623
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002624 if (flags)
2625 mlx5_ib_dbg(dev, "udata has unsupported flags 0x%X\n", flags);
2626
2627 return (flags) ? -EINVAL : 0;
Leon Romanovsky5d6fffe2020-04-27 18:46:31 +03002628 }
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002629
Leon Romanovsky29789752020-04-27 18:46:14 +03002630static void process_create_flag(struct mlx5_ib_dev *dev, int *flags, int flag,
2631 bool cond, struct mlx5_ib_qp *qp)
2632{
2633 if (!(*flags & flag))
2634 return;
2635
2636 if (cond) {
2637 qp->flags |= flag;
2638 *flags &= ~flag;
2639 return;
2640 }
2641
2642 if (flag == MLX5_IB_QP_CREATE_WC_TEST) {
2643 /*
2644 * Special case, if condition didn't meet, it won't be error,
2645 * just different in-kernel flow.
2646 */
2647 *flags &= ~MLX5_IB_QP_CREATE_WC_TEST;
2648 return;
2649 }
2650 mlx5_ib_dbg(dev, "Verbs create QP flag 0x%X is not supported\n", flag);
2651}
2652
2653static int process_create_flags(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2654 struct ib_qp_init_attr *attr)
2655{
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002656 enum ib_qp_type qp_type = qp->type;
Leon Romanovsky29789752020-04-27 18:46:14 +03002657 struct mlx5_core_dev *mdev = dev->mdev;
2658 int create_flags = attr->create_flags;
2659 bool cond;
2660
Mark Bloch42caf9c2020-05-06 10:16:02 +03002661 if (qp->type == IB_QPT_UD && dev->profile == &raw_eth_profile)
2662 if (create_flags & ~MLX5_IB_QP_CREATE_WC_TEST)
2663 return -EINVAL;
2664
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002665 if (qp_type == MLX5_IB_QPT_DCT)
Leon Romanovsky29789752020-04-27 18:46:14 +03002666 return (create_flags) ? -EINVAL : 0;
2667
2668 if (qp_type == IB_QPT_RAW_PACKET && attr->rwq_ind_tbl)
2669 return (create_flags) ? -EINVAL : 0;
2670
2671 process_create_flag(dev, &create_flags,
Max Gurtovoy9e0dc7b2020-06-17 16:02:30 +03002672 IB_QP_CREATE_INTEGRITY_EN,
2673 MLX5_CAP_GEN(mdev, sho), qp);
2674 process_create_flag(dev, &create_flags,
Leon Romanovsky29789752020-04-27 18:46:14 +03002675 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK,
2676 MLX5_CAP_GEN(mdev, block_lb_mc), qp);
2677 process_create_flag(dev, &create_flags, IB_QP_CREATE_CROSS_CHANNEL,
2678 MLX5_CAP_GEN(mdev, cd), qp);
2679 process_create_flag(dev, &create_flags, IB_QP_CREATE_MANAGED_SEND,
2680 MLX5_CAP_GEN(mdev, cd), qp);
2681 process_create_flag(dev, &create_flags, IB_QP_CREATE_MANAGED_RECV,
2682 MLX5_CAP_GEN(mdev, cd), qp);
2683
2684 if (qp_type == IB_QPT_UD) {
2685 process_create_flag(dev, &create_flags,
2686 IB_QP_CREATE_IPOIB_UD_LSO,
2687 MLX5_CAP_GEN(mdev, ipoib_basic_offloads),
2688 qp);
2689 cond = MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_IB;
2690 process_create_flag(dev, &create_flags, IB_QP_CREATE_SOURCE_QPN,
2691 cond, qp);
2692 }
2693
2694 if (qp_type == IB_QPT_RAW_PACKET) {
2695 cond = MLX5_CAP_GEN(mdev, eth_net_offloads) &&
2696 MLX5_CAP_ETH(mdev, scatter_fcs);
2697 process_create_flag(dev, &create_flags,
2698 IB_QP_CREATE_SCATTER_FCS, cond, qp);
2699
2700 cond = MLX5_CAP_GEN(mdev, eth_net_offloads) &&
2701 MLX5_CAP_ETH(mdev, vlan_cap);
2702 process_create_flag(dev, &create_flags,
2703 IB_QP_CREATE_CVLAN_STRIPPING, cond, qp);
2704 }
2705
2706 process_create_flag(dev, &create_flags,
2707 IB_QP_CREATE_PCI_WRITE_END_PADDING,
2708 MLX5_CAP_GEN(mdev, end_pad), qp);
2709
2710 process_create_flag(dev, &create_flags, MLX5_IB_QP_CREATE_WC_TEST,
2711 qp_type != MLX5_IB_QPT_REG_UMR, qp);
2712 process_create_flag(dev, &create_flags, MLX5_IB_QP_CREATE_SQPN_QP1,
2713 true, qp);
2714
2715 if (create_flags)
2716 mlx5_ib_dbg(dev, "Create QP has unsupported flags 0x%X\n",
2717 create_flags);
2718
2719 return (create_flags) ? -EINVAL : 0;
2720}
2721
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002722static int process_udata_size(struct mlx5_ib_dev *dev,
2723 struct mlx5_create_qp_params *params)
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002724{
2725 size_t ucmd = sizeof(struct mlx5_ib_create_qp);
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002726 struct ib_udata *udata = params->udata;
2727 size_t outlen = udata->outlen;
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002728 size_t inlen = udata->inlen;
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002729
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002730 params->outlen = min(outlen, sizeof(struct mlx5_ib_create_qp_resp));
Leon Romanovskye3830852020-05-26 14:54:35 +03002731 params->ucmd_size = ucmd;
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002732 if (!params->is_rss_raw) {
Leon Romanovskye3830852020-05-26 14:54:35 +03002733 /* User has old rdma-core, which doesn't support ECE */
2734 size_t min_inlen =
2735 offsetof(struct mlx5_ib_create_qp, ece_options);
2736
2737 /*
2738 * We will check in check_ucmd_data() that user
2739 * cleared everything after inlen.
2740 */
2741 params->inlen = (inlen < min_inlen) ? 0 : min(inlen, ucmd);
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002742 goto out;
2743 }
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002744
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002745 /* RSS RAW QP */
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002746 if (inlen < offsetofend(struct mlx5_ib_create_qp_rss, flags))
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002747 return -EINVAL;
2748
2749 if (outlen < offsetofend(struct mlx5_ib_create_qp_resp, bfreg_index))
2750 return -EINVAL;
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002751
2752 ucmd = sizeof(struct mlx5_ib_create_qp_rss);
Leon Romanovskye3830852020-05-26 14:54:35 +03002753 params->ucmd_size = ucmd;
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002754 if (inlen > ucmd && !ib_is_udata_cleared(udata, ucmd, inlen - ucmd))
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002755 return -EINVAL;
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002756
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002757 params->inlen = min(ucmd, inlen);
2758out:
2759 if (!params->inlen)
Leon Romanovskye3830852020-05-26 14:54:35 +03002760 mlx5_ib_dbg(dev, "udata is too small\n");
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002761
2762 return (params->inlen) ? 0 : -EINVAL;
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002763}
2764
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002765static int create_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
2766 struct mlx5_ib_qp *qp,
2767 struct mlx5_create_qp_params *params)
Leon Romanovsky5d0dc3d2020-04-27 18:46:12 +03002768{
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002769 int err;
Leon Romanovsky5d0dc3d2020-04-27 18:46:12 +03002770
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002771 if (params->is_rss_raw) {
2772 err = create_rss_raw_qp_tir(dev, pd, qp, params);
2773 goto out;
2774 }
2775
2776 if (qp->type == MLX5_IB_QPT_DCT) {
Leon Romanovskya645a892020-06-02 15:55:48 +03002777 err = create_dct(dev, pd, qp, params);
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002778 goto out;
2779 }
2780
2781 if (qp->type == IB_QPT_XRC_TGT) {
2782 err = create_xrc_tgt_qp(dev, qp, params);
2783 goto out;
2784 }
2785
2786 if (params->udata)
2787 err = create_user_qp(dev, pd, qp, params);
2788 else
2789 err = create_kernel_qp(dev, pd, qp, params);
2790
2791out:
2792 if (err) {
2793 mlx5_ib_err(dev, "Create QP type %d failed\n", qp->type);
2794 return err;
2795 }
2796
2797 if (is_qp0(qp->type))
2798 qp->ibqp.qp_num = 0;
2799 else if (is_qp1(qp->type))
2800 qp->ibqp.qp_num = 1;
2801 else
2802 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
2803
2804 mlx5_ib_dbg(dev,
Leon Romanovsky3e09a422020-05-26 14:54:34 +03002805 "QP type %d, ib qpn 0x%X, mlx qpn 0x%x, rcqn 0x%x, scqn 0x%x, ece 0x%x\n",
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002806 qp->type, qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
2807 params->attr->recv_cq ? to_mcq(params->attr->recv_cq)->mcq.cqn :
2808 -1,
2809 params->attr->send_cq ? to_mcq(params->attr->send_cq)->mcq.cqn :
Leon Romanovsky3e09a422020-05-26 14:54:34 +03002810 -1,
2811 params->resp.ece_options);
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002812
2813 return 0;
Leon Romanovsky5d0dc3d2020-04-27 18:46:12 +03002814}
2815
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002816static int check_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2817 struct ib_qp_init_attr *attr)
2818{
2819 int ret = 0;
2820
2821 switch (qp->type) {
2822 case MLX5_IB_QPT_DCT:
2823 ret = (!attr->srq || !attr->recv_cq) ? -EINVAL : 0;
2824 break;
2825 case MLX5_IB_QPT_DCI:
2826 ret = (attr->cap.max_recv_wr || attr->cap.max_recv_sge) ?
2827 -EINVAL :
2828 0;
2829 break;
Leon Romanovsky266424e2020-04-27 18:46:21 +03002830 case IB_QPT_RAW_PACKET:
2831 ret = (attr->rwq_ind_tbl && attr->send_cq) ? -EINVAL : 0;
2832 break;
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002833 default:
2834 break;
2835 }
2836
2837 if (ret)
2838 mlx5_ib_dbg(dev, "QP type %d has wrong attributes\n", qp->type);
2839
2840 return ret;
2841}
2842
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002843static int get_qp_uidx(struct mlx5_ib_qp *qp,
2844 struct mlx5_create_qp_params *params)
Leon Romanovsky21aad802020-04-27 18:46:26 +03002845{
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002846 struct mlx5_ib_create_qp *ucmd = params->ucmd;
2847 struct ib_udata *udata = params->udata;
Leon Romanovsky21aad802020-04-27 18:46:26 +03002848 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2849 udata, struct mlx5_ib_ucontext, ibucontext);
2850
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002851 if (params->is_rss_raw)
Leon Romanovsky21aad802020-04-27 18:46:26 +03002852 return 0;
2853
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002854 return get_qp_user_index(ucontext, ucmd, sizeof(*ucmd), &params->uidx);
Leon Romanovsky21aad802020-04-27 18:46:26 +03002855}
2856
Leon Romanovsky08d539762020-04-27 18:46:33 +03002857static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2858{
2859 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2860
2861 if (mqp->state == IB_QPS_RTR) {
2862 int err;
2863
2864 err = mlx5_core_destroy_dct(dev, &mqp->dct.mdct);
2865 if (err) {
2866 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2867 return err;
2868 }
2869 }
2870
2871 kfree(mqp->dct.in);
2872 kfree(mqp);
2873 return 0;
2874}
2875
Leon Romanovskye3830852020-05-26 14:54:35 +03002876static int check_ucmd_data(struct mlx5_ib_dev *dev,
2877 struct mlx5_create_qp_params *params)
2878{
Leon Romanovskye3830852020-05-26 14:54:35 +03002879 struct ib_udata *udata = params->udata;
2880 size_t size, last;
2881 int ret;
2882
2883 if (params->is_rss_raw)
2884 /*
2885 * These QPs don't have "reserved" field in their
2886 * create_qp input struct, so their data is always valid.
2887 */
2888 last = sizeof(struct mlx5_ib_create_qp_rss);
2889 else
Leon Romanovsky2c0f5292020-06-18 14:25:06 +03002890 last = offsetof(struct mlx5_ib_create_qp, reserved);
Leon Romanovskye3830852020-05-26 14:54:35 +03002891
2892 if (udata->inlen <= last)
2893 return 0;
2894
2895 /*
2896 * User provides different create_qp structures based on the
2897 * flow and we need to know if he cleared memory after our
2898 * struct create_qp ends.
2899 */
2900 size = udata->inlen - last;
2901 ret = ib_is_udata_cleared(params->udata, last, size);
2902 if (!ret)
2903 mlx5_ib_dbg(
2904 dev,
Tom Seewald4f5747c2020-06-04 21:30:12 -05002905 "udata is not cleared, inlen = %zu, ucmd = %zu, last = %zu, size = %zu\n",
Leon Romanovskye3830852020-05-26 14:54:35 +03002906 udata->inlen, params->ucmd_size, last, size);
2907 return ret ? 0 : -EINVAL;
2908}
2909
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002910struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002911 struct ib_udata *udata)
2912{
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002913 struct mlx5_create_qp_params params = {};
Eli Cohene126ba92013-07-07 17:25:49 +03002914 struct mlx5_ib_dev *dev;
2915 struct mlx5_ib_qp *qp;
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002916 enum ib_qp_type type;
Eli Cohene126ba92013-07-07 17:25:49 +03002917 int err;
2918
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002919 dev = pd ? to_mdev(pd->device) :
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002920 to_mdev(to_mxrcd(attr->xrcd)->ibxrcd.device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002921
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002922 err = check_qp_type(dev, attr, &type);
Leon Romanovsky2242cc22020-04-27 18:46:03 +03002923 if (err)
2924 return ERR_PTR(err);
Eli Cohene126ba92013-07-07 17:25:49 +03002925
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002926 err = check_valid_flow(dev, pd, attr, udata);
2927 if (err)
2928 return ERR_PTR(err);
2929
2930 if (attr->qp_type == IB_QPT_GSI)
2931 return mlx5_ib_gsi_create_qp(pd, attr);
2932
2933 params.udata = udata;
2934 params.uidx = MLX5_IB_DEFAULT_UIDX;
2935 params.attr = attr;
2936 params.is_rss_raw = !!attr->rwq_ind_tbl;
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002937
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002938 if (udata) {
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002939 err = process_udata_size(dev, &params);
2940 if (err)
2941 return ERR_PTR(err);
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002942
Leon Romanovskye3830852020-05-26 14:54:35 +03002943 err = check_ucmd_data(dev, &params);
2944 if (err)
2945 return ERR_PTR(err);
2946
2947 params.ucmd = kzalloc(params.ucmd_size, GFP_KERNEL);
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002948 if (!params.ucmd)
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002949 return ERR_PTR(-ENOMEM);
2950
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002951 err = ib_copy_from_udata(params.ucmd, udata, params.inlen);
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002952 if (err)
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002953 goto free_ucmd;
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002954 }
2955
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002956 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002957 if (!qp) {
2958 err = -ENOMEM;
2959 goto free_ucmd;
2960 }
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002961
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002962 qp->type = type;
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002963 if (udata) {
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002964 err = process_vendor_flags(dev, qp, params.ucmd, attr);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002965 if (err)
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002966 goto free_qp;
Leon Romanovsky21aad802020-04-27 18:46:26 +03002967
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002968 err = get_qp_uidx(qp, &params);
Leon Romanovsky21aad802020-04-27 18:46:26 +03002969 if (err)
2970 goto free_qp;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002971 }
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002972 err = process_create_flags(dev, qp, attr);
Leon Romanovsky29789752020-04-27 18:46:14 +03002973 if (err)
2974 goto free_qp;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002975
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002976 err = check_qp_attr(dev, qp, attr);
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002977 if (err)
2978 goto free_qp;
2979
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002980 err = create_qp(dev, pd, qp, &params);
2981 if (err)
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002982 goto free_qp;
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002983
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002984 kfree(params.ucmd);
Leon Romanovsky08d539762020-04-27 18:46:33 +03002985 params.ucmd = NULL;
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002986
Leon Romanovsky08d539762020-04-27 18:46:33 +03002987 if (udata)
2988 /*
2989 * It is safe to copy response for all user create QP flows,
2990 * including MLX5_IB_QPT_DCT, which doesn't need it.
2991 * In that case, resp will be filled with zeros.
2992 */
2993 err = ib_copy_to_udata(udata, &params.resp, params.outlen);
2994 if (err)
2995 goto destroy_qp;
2996
Eli Cohene126ba92013-07-07 17:25:49 +03002997 return &qp->ibqp;
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002998
Leon Romanovsky08d539762020-04-27 18:46:33 +03002999destroy_qp:
Leon Romanovsky6c419652020-06-17 16:01:48 +03003000 if (qp->type == MLX5_IB_QPT_DCT) {
Leon Romanovsky08d539762020-04-27 18:46:33 +03003001 mlx5_ib_destroy_dct(qp);
Leon Romanovsky6c419652020-06-17 16:01:48 +03003002 } else {
3003 /*
3004 * The two lines below are temp solution till QP allocation
3005 * will be moved to be under IB/core responsiblity.
3006 */
3007 qp->ibqp.send_cq = attr->send_cq;
3008 qp->ibqp.recv_cq = attr->recv_cq;
Leon Romanovsky08d539762020-04-27 18:46:33 +03003009 destroy_qp_common(dev, qp, udata);
Leon Romanovsky6c419652020-06-17 16:01:48 +03003010 }
3011
Leon Romanovsky08d539762020-04-27 18:46:33 +03003012 qp = NULL;
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03003013free_qp:
3014 kfree(qp);
Leon Romanovsky5ce05922020-04-27 18:46:22 +03003015free_ucmd:
Leon Romanovskyf78d3582020-04-27 18:46:30 +03003016 kfree(params.ucmd);
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03003017 return ERR_PTR(err);
Eli Cohene126ba92013-07-07 17:25:49 +03003018}
3019
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03003020int mlx5_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003021{
3022 struct mlx5_ib_dev *dev = to_mdev(qp->device);
3023 struct mlx5_ib_qp *mqp = to_mqp(qp);
3024
Haggai Erand16e91d2016-02-29 15:45:05 +02003025 if (unlikely(qp->qp_type == IB_QPT_GSI))
3026 return mlx5_ib_gsi_destroy_qp(qp);
3027
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03003028 if (mqp->type == MLX5_IB_QPT_DCT)
Moni Shoua776a3902018-01-02 16:19:33 +02003029 return mlx5_ib_destroy_dct(mqp);
3030
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03003031 destroy_qp_common(dev, mqp, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03003032
3033 kfree(mqp);
3034
3035 return 0;
3036}
3037
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003038static int set_qpc_atomic_flags(struct mlx5_ib_qp *qp,
3039 const struct ib_qp_attr *attr, int attr_mask,
3040 void *qpc)
Eli Cohene126ba92013-07-07 17:25:49 +03003041{
Yonatan Cohena60109d2018-10-10 09:25:16 +03003042 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003043 u8 dest_rd_atomic;
3044 u32 access_flags;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003045
Eli Cohene126ba92013-07-07 17:25:49 +03003046 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
3047 dest_rd_atomic = attr->max_dest_rd_atomic;
3048 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02003049 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03003050
3051 if (attr_mask & IB_QP_ACCESS_FLAGS)
3052 access_flags = attr->qp_access_flags;
3053 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02003054 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03003055
3056 if (!dest_rd_atomic)
3057 access_flags &= IB_ACCESS_REMOTE_WRITE;
3058
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003059 MLX5_SET(qpc, qpc, rre, !!(access_flags & IB_ACCESS_REMOTE_READ));
3060
Yonatan Cohen13f8d9c2018-11-21 13:48:39 +02003061 if (access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03003062 int atomic_mode;
Eli Cohene126ba92013-07-07 17:25:49 +03003063
Yonatan Cohena60109d2018-10-10 09:25:16 +03003064 atomic_mode = get_atomic_mode(dev, qp->ibqp.qp_type);
3065 if (atomic_mode < 0)
3066 return -EOPNOTSUPP;
3067
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003068 MLX5_SET(qpc, qpc, rae, 1);
3069 MLX5_SET(qpc, qpc, atomic_mode, atomic_mode);
Yonatan Cohena60109d2018-10-10 09:25:16 +03003070 }
3071
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003072 MLX5_SET(qpc, qpc, rwe, !!(access_flags & IB_ACCESS_REMOTE_WRITE));
Yonatan Cohena60109d2018-10-10 09:25:16 +03003073 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003074}
3075
3076enum {
3077 MLX5_PATH_FLAG_FL = 1 << 0,
3078 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
3079 MLX5_PATH_FLAG_COUNTER = 1 << 2,
3080};
3081
3082static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
3083{
Danit Goldberg4f32ac22018-04-23 17:01:54 +03003084 if (rate == IB_RATE_PORT_CURRENT)
Eli Cohene126ba92013-07-07 17:25:49 +03003085 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003086
Michael Guralnika5a5d192018-12-09 11:49:50 +02003087 if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_600_GBPS)
Danit Goldberg4f32ac22018-04-23 17:01:54 +03003088 return -EINVAL;
3089
3090 while (rate != IB_RATE_PORT_CURRENT &&
3091 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
3092 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
3093 --rate;
3094
3095 return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
Eli Cohene126ba92013-07-07 17:25:49 +03003096}
3097
majd@mellanox.com75850d02016-01-14 19:13:06 +02003098static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003099 struct mlx5_ib_sq *sq, u8 sl,
3100 struct ib_pd *pd)
majd@mellanox.com75850d02016-01-14 19:13:06 +02003101{
3102 void *in;
3103 void *tisc;
3104 int inlen;
3105 int err;
3106
3107 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003108 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02003109 if (!in)
3110 return -ENOMEM;
3111
3112 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003113 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com75850d02016-01-14 19:13:06 +02003114
3115 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
3116 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
3117
Leon Romanovskye0b4b472020-04-09 21:03:33 +03003118 err = mlx5_core_modify_tis(dev, sq->tisn, in);
majd@mellanox.com75850d02016-01-14 19:13:06 +02003119
3120 kvfree(in);
3121
3122 return err;
3123}
3124
Aviv Heller13eab212016-09-18 20:48:04 +03003125static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003126 struct mlx5_ib_sq *sq, u8 tx_affinity,
3127 struct ib_pd *pd)
Aviv Heller13eab212016-09-18 20:48:04 +03003128{
3129 void *in;
3130 void *tisc;
3131 int inlen;
3132 int err;
3133
3134 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003135 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03003136 if (!in)
3137 return -ENOMEM;
3138
3139 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003140 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
Aviv Heller13eab212016-09-18 20:48:04 +03003141
3142 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
3143 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
3144
Leon Romanovskye0b4b472020-04-09 21:03:33 +03003145 err = mlx5_core_modify_tis(dev, sq->tisn, in);
Aviv Heller13eab212016-09-18 20:48:04 +03003146
3147 kvfree(in);
3148
3149 return err;
3150}
3151
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003152static void mlx5_set_path_udp_sport(void *path, const struct rdma_ah_attr *ah,
Mark Zhang2b880b2e2020-05-04 08:19:33 +03003153 u32 lqpn, u32 rqpn)
3154
3155{
3156 u32 fl = ah->grh.flow_label;
Mark Zhang2b880b2e2020-05-04 08:19:33 +03003157
3158 if (!fl)
3159 fl = rdma_calc_flow_label(lqpn, rqpn);
3160
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003161 MLX5_SET(ads, path, udp_sport, rdma_flow_label_to_udp_sport(fl));
Mark Zhang2b880b2e2020-05-04 08:19:33 +03003162}
3163
majd@mellanox.com75850d02016-01-14 19:13:06 +02003164static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003165 const struct rdma_ah_attr *ah, void *path, u8 port,
3166 int attr_mask, u32 path_flags,
3167 const struct ib_qp_attr *attr, bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03003168{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003169 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03003170 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02003171 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003172 u8 ah_flags = rdma_ah_get_ah_flags(ah);
3173 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03003174
Eli Cohene126ba92013-07-07 17:25:49 +03003175 if (attr_mask & IB_QP_PKEY_INDEX)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003176 MLX5_SET(ads, path, pkey_index,
3177 alt ? attr->alt_pkey_index : attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003178
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003179 if (ah_flags & IB_AH_GRH) {
3180 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03003181 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07003182 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003183 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03003184 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03003185 return -EINVAL;
3186 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02003187 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04003188
3189 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003190 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02003191 return -EINVAL;
Parav Pandit47ec3862018-06-13 10:22:06 +03003192
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003193 ether_addr_copy(MLX5_ADDR_OF(ads, path, rmac_47_32),
3194 ah->roce.dmac);
Mark Zhang2b880b2e2020-05-04 08:19:33 +03003195 if ((qp->ibqp.qp_type == IB_QPT_RC ||
3196 qp->ibqp.qp_type == IB_QPT_UC ||
3197 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
3198 qp->ibqp.qp_type == IB_QPT_XRC_TGT) &&
3199 (grh->sgid_attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) &&
3200 (attr_mask & IB_QP_DEST_QPN))
3201 mlx5_set_path_udp_sport(path, ah,
3202 qp->ibqp.qp_num,
3203 attr->dest_qp_num);
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003204 MLX5_SET(ads, path, eth_prio, sl & 0x7);
Parav Pandit47ec3862018-06-13 10:22:06 +03003205 gid_type = ah->grh.sgid_attr->gid_type;
Majd Dibbinyed884512017-01-18 14:10:35 +02003206 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003207 MLX5_SET(ads, path, dscp, grh->traffic_class >> 2);
Achiad Shochat2811ba52015-12-23 18:47:24 +02003208 } else {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003209 MLX5_SET(ads, path, fl, !!(path_flags & MLX5_PATH_FLAG_FL));
3210 MLX5_SET(ads, path, free_ar,
3211 !!(path_flags & MLX5_PATH_FLAG_FREE_AR));
3212 MLX5_SET(ads, path, rlid, rdma_ah_get_dlid(ah));
3213 MLX5_SET(ads, path, mlid, rdma_ah_get_path_bits(ah));
3214 MLX5_SET(ads, path, grh, !!(ah_flags & IB_AH_GRH));
3215 MLX5_SET(ads, path, sl, sl);
Achiad Shochat2811ba52015-12-23 18:47:24 +02003216 }
3217
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003218 if (ah_flags & IB_AH_GRH) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003219 MLX5_SET(ads, path, src_addr_index, grh->sgid_index);
3220 MLX5_SET(ads, path, hop_limit, grh->hop_limit);
3221 MLX5_SET(ads, path, tclass, grh->traffic_class);
3222 MLX5_SET(ads, path, flow_label, grh->flow_label);
3223 memcpy(MLX5_ADDR_OF(ads, path, rgid_rip), grh->dgid.raw,
3224 sizeof(grh->dgid.raw));
Eli Cohene126ba92013-07-07 17:25:49 +03003225 }
3226
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003227 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03003228 if (err < 0)
3229 return err;
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003230 MLX5_SET(ads, path, stat_rate, err);
3231 MLX5_SET(ads, path, vhca_port_num, port);
Eli Cohene126ba92013-07-07 17:25:49 +03003232
Eli Cohene126ba92013-07-07 17:25:49 +03003233 if (attr_mask & IB_QP_TIMEOUT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003234 MLX5_SET(ads, path, ack_timeout,
3235 alt ? attr->alt_timeout : attr->timeout);
Eli Cohene126ba92013-07-07 17:25:49 +03003236
majd@mellanox.com75850d02016-01-14 19:13:06 +02003237 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
3238 return modify_raw_packet_eth_prio(dev->mdev,
3239 &qp->raw_packet_qp.sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003240 sl & 0xf, qp->ibqp.pd);
majd@mellanox.com75850d02016-01-14 19:13:06 +02003241
Eli Cohene126ba92013-07-07 17:25:49 +03003242 return 0;
3243}
3244
3245static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
3246 [MLX5_QP_STATE_INIT] = {
3247 [MLX5_QP_STATE_INIT] = {
3248 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3249 MLX5_QP_OPTPAR_RAE |
3250 MLX5_QP_OPTPAR_RWE |
3251 MLX5_QP_OPTPAR_PKEY_INDEX |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003252 MLX5_QP_OPTPAR_PRI_PORT |
3253 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003254 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
3255 MLX5_QP_OPTPAR_PKEY_INDEX |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003256 MLX5_QP_OPTPAR_PRI_PORT |
3257 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003258 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3259 MLX5_QP_OPTPAR_Q_KEY |
3260 MLX5_QP_OPTPAR_PRI_PORT,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003261 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3262 MLX5_QP_OPTPAR_RAE |
3263 MLX5_QP_OPTPAR_RWE |
3264 MLX5_QP_OPTPAR_PKEY_INDEX |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003265 MLX5_QP_OPTPAR_PRI_PORT |
3266 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003267 },
3268 [MLX5_QP_STATE_RTR] = {
3269 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3270 MLX5_QP_OPTPAR_RRE |
3271 MLX5_QP_OPTPAR_RAE |
3272 MLX5_QP_OPTPAR_RWE |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003273 MLX5_QP_OPTPAR_PKEY_INDEX |
3274 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003275 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3276 MLX5_QP_OPTPAR_RWE |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003277 MLX5_QP_OPTPAR_PKEY_INDEX |
3278 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003279 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3280 MLX5_QP_OPTPAR_Q_KEY,
3281 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
3282 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03003283 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3284 MLX5_QP_OPTPAR_RRE |
3285 MLX5_QP_OPTPAR_RAE |
3286 MLX5_QP_OPTPAR_RWE |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003287 MLX5_QP_OPTPAR_PKEY_INDEX |
3288 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003289 },
3290 },
3291 [MLX5_QP_STATE_RTR] = {
3292 [MLX5_QP_STATE_RTS] = {
3293 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3294 MLX5_QP_OPTPAR_RRE |
3295 MLX5_QP_OPTPAR_RAE |
3296 MLX5_QP_OPTPAR_RWE |
3297 MLX5_QP_OPTPAR_PM_STATE |
3298 MLX5_QP_OPTPAR_RNR_TIMEOUT,
3299 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3300 MLX5_QP_OPTPAR_RWE |
3301 MLX5_QP_OPTPAR_PM_STATE,
3302 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003303 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3304 MLX5_QP_OPTPAR_RRE |
3305 MLX5_QP_OPTPAR_RAE |
3306 MLX5_QP_OPTPAR_RWE |
3307 MLX5_QP_OPTPAR_PM_STATE |
3308 MLX5_QP_OPTPAR_RNR_TIMEOUT,
Eli Cohene126ba92013-07-07 17:25:49 +03003309 },
3310 },
3311 [MLX5_QP_STATE_RTS] = {
3312 [MLX5_QP_STATE_RTS] = {
3313 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3314 MLX5_QP_OPTPAR_RAE |
3315 MLX5_QP_OPTPAR_RWE |
3316 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03003317 MLX5_QP_OPTPAR_PM_STATE |
3318 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003319 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03003320 MLX5_QP_OPTPAR_PM_STATE |
3321 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003322 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
3323 MLX5_QP_OPTPAR_SRQN |
3324 MLX5_QP_OPTPAR_CQN_RCV,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003325 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3326 MLX5_QP_OPTPAR_RAE |
3327 MLX5_QP_OPTPAR_RWE |
3328 MLX5_QP_OPTPAR_RNR_TIMEOUT |
3329 MLX5_QP_OPTPAR_PM_STATE |
3330 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003331 },
3332 },
3333 [MLX5_QP_STATE_SQER] = {
3334 [MLX5_QP_STATE_RTS] = {
3335 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
3336 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03003337 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03003338 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3339 MLX5_QP_OPTPAR_RWE |
3340 MLX5_QP_OPTPAR_RAE |
3341 MLX5_QP_OPTPAR_RRE,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003342 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3343 MLX5_QP_OPTPAR_RWE |
3344 MLX5_QP_OPTPAR_RAE |
3345 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03003346 },
3347 },
3348};
3349
3350static int ib_nr_to_mlx5_nr(int ib_mask)
3351{
3352 switch (ib_mask) {
3353 case IB_QP_STATE:
3354 return 0;
3355 case IB_QP_CUR_STATE:
3356 return 0;
3357 case IB_QP_EN_SQD_ASYNC_NOTIFY:
3358 return 0;
3359 case IB_QP_ACCESS_FLAGS:
3360 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
3361 MLX5_QP_OPTPAR_RAE;
3362 case IB_QP_PKEY_INDEX:
3363 return MLX5_QP_OPTPAR_PKEY_INDEX;
3364 case IB_QP_PORT:
3365 return MLX5_QP_OPTPAR_PRI_PORT;
3366 case IB_QP_QKEY:
3367 return MLX5_QP_OPTPAR_Q_KEY;
3368 case IB_QP_AV:
3369 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
3370 MLX5_QP_OPTPAR_PRI_PORT;
3371 case IB_QP_PATH_MTU:
3372 return 0;
3373 case IB_QP_TIMEOUT:
3374 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
3375 case IB_QP_RETRY_CNT:
3376 return MLX5_QP_OPTPAR_RETRY_COUNT;
3377 case IB_QP_RNR_RETRY:
3378 return MLX5_QP_OPTPAR_RNR_RETRY;
3379 case IB_QP_RQ_PSN:
3380 return 0;
3381 case IB_QP_MAX_QP_RD_ATOMIC:
3382 return MLX5_QP_OPTPAR_SRA_MAX;
3383 case IB_QP_ALT_PATH:
3384 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
3385 case IB_QP_MIN_RNR_TIMER:
3386 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
3387 case IB_QP_SQ_PSN:
3388 return 0;
3389 case IB_QP_MAX_DEST_RD_ATOMIC:
3390 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
3391 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
3392 case IB_QP_PATH_MIG_STATE:
3393 return MLX5_QP_OPTPAR_PM_STATE;
3394 case IB_QP_CAP:
3395 return 0;
3396 case IB_QP_DEST_QPN:
3397 return 0;
3398 }
3399 return 0;
3400}
3401
3402static int ib_mask_to_mlx5_opt(int ib_mask)
3403{
3404 int result = 0;
3405 int i;
3406
3407 for (i = 0; i < 8 * sizeof(int); i++) {
3408 if ((1 << i) & ib_mask)
3409 result |= ib_nr_to_mlx5_nr(1 << i);
3410 }
3411
3412 return result;
3413}
3414
Yishai Hadas34d57582018-09-20 21:39:21 +03003415static int modify_raw_packet_qp_rq(
3416 struct mlx5_ib_dev *dev, struct mlx5_ib_rq *rq, int new_state,
3417 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003418{
3419 void *in;
3420 void *rqc;
3421 int inlen;
3422 int err;
3423
3424 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003425 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003426 if (!in)
3427 return -ENOMEM;
3428
3429 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
Yishai Hadas34d57582018-09-20 21:39:21 +03003430 MLX5_SET(modify_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003431
3432 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
3433 MLX5_SET(rqc, rqc, state, new_state);
3434
Alex Veskereb49ab02016-08-28 12:25:53 +03003435 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
3436 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
3437 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02003438 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03003439 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
3440 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06003441 dev_info_once(
3442 &dev->ib_dev.dev,
3443 "RAW PACKET QP counters are not supported on current FW\n");
Alex Veskereb49ab02016-08-28 12:25:53 +03003444 }
3445
Leon Romanovskye0b4b472020-04-09 21:03:33 +03003446 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003447 if (err)
3448 goto out;
3449
3450 rq->state = new_state;
3451
3452out:
3453 kvfree(in);
3454 return err;
3455}
3456
Yishai Hadasc14003f2018-09-20 21:39:22 +03003457static int modify_raw_packet_qp_sq(
3458 struct mlx5_core_dev *dev, struct mlx5_ib_sq *sq, int new_state,
3459 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003460{
Bodong Wang7d29f342016-12-01 13:43:16 +02003461 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
Bodong Wang61147f32018-03-19 15:10:30 +02003462 struct mlx5_rate_limit old_rl = ibqp->rl;
3463 struct mlx5_rate_limit new_rl = old_rl;
3464 bool new_rate_added = false;
Bodong Wang7d29f342016-12-01 13:43:16 +02003465 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003466 void *in;
3467 void *sqc;
3468 int inlen;
3469 int err;
3470
3471 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003472 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003473 if (!in)
3474 return -ENOMEM;
3475
Yishai Hadasc14003f2018-09-20 21:39:22 +03003476 MLX5_SET(modify_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003477 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
3478
3479 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
3480 MLX5_SET(sqc, sqc, state, new_state);
3481
Bodong Wang7d29f342016-12-01 13:43:16 +02003482 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
3483 if (new_state != MLX5_SQC_STATE_RDY)
3484 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
3485 __func__);
3486 else
Bodong Wang61147f32018-03-19 15:10:30 +02003487 new_rl = raw_qp_param->rl;
Bodong Wang7d29f342016-12-01 13:43:16 +02003488 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003489
Bodong Wang61147f32018-03-19 15:10:30 +02003490 if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
3491 if (new_rl.rate) {
3492 err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003493 if (err) {
Bodong Wang61147f32018-03-19 15:10:30 +02003494 pr_err("Failed configuring rate limit(err %d): \
3495 rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
3496 err, new_rl.rate, new_rl.max_burst_sz,
3497 new_rl.typical_pkt_sz);
3498
Bodong Wang7d29f342016-12-01 13:43:16 +02003499 goto out;
3500 }
Bodong Wang61147f32018-03-19 15:10:30 +02003501 new_rate_added = true;
Bodong Wang7d29f342016-12-01 13:43:16 +02003502 }
3503
3504 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
Bodong Wang61147f32018-03-19 15:10:30 +02003505 /* index 0 means no limit */
Bodong Wang7d29f342016-12-01 13:43:16 +02003506 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
3507 }
3508
Leon Romanovskye0b4b472020-04-09 21:03:33 +03003509 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in);
Bodong Wang7d29f342016-12-01 13:43:16 +02003510 if (err) {
3511 /* Remove new rate from table if failed */
Bodong Wang61147f32018-03-19 15:10:30 +02003512 if (new_rate_added)
3513 mlx5_rl_remove_rate(dev, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003514 goto out;
3515 }
3516
3517 /* Only remove the old rate after new rate was set */
Rafi Wienerc8973df2019-10-02 15:02:43 +03003518 if ((old_rl.rate && !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
3519 (new_state != MLX5_SQC_STATE_RDY)) {
Bodong Wang61147f32018-03-19 15:10:30 +02003520 mlx5_rl_remove_rate(dev, &old_rl);
Rafi Wienerc8973df2019-10-02 15:02:43 +03003521 if (new_state != MLX5_SQC_STATE_RDY)
3522 memset(&new_rl, 0, sizeof(new_rl));
3523 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003524
Bodong Wang61147f32018-03-19 15:10:30 +02003525 ibqp->rl = new_rl;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003526 sq->state = new_state;
3527
3528out:
3529 kvfree(in);
3530 return err;
3531}
3532
3533static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03003534 const struct mlx5_modify_raw_qp_param *raw_qp_param,
3535 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003536{
3537 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
3538 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
3539 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02003540 int modify_rq = !!qp->rq.wqe_cnt;
3541 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003542 int rq_state;
3543 int sq_state;
3544 int err;
3545
Alex Vesker0680efa2016-08-28 12:25:52 +03003546 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003547 case MLX5_CMD_OP_RST2INIT_QP:
3548 rq_state = MLX5_RQC_STATE_RDY;
3549 sq_state = MLX5_SQC_STATE_RDY;
3550 break;
3551 case MLX5_CMD_OP_2ERR_QP:
3552 rq_state = MLX5_RQC_STATE_ERR;
3553 sq_state = MLX5_SQC_STATE_ERR;
3554 break;
3555 case MLX5_CMD_OP_2RST_QP:
3556 rq_state = MLX5_RQC_STATE_RST;
3557 sq_state = MLX5_SQC_STATE_RST;
3558 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003559 case MLX5_CMD_OP_RTR2RTS_QP:
3560 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02003561 if (raw_qp_param->set_mask ==
3562 MLX5_RAW_QP_RATE_LIMIT) {
3563 modify_rq = 0;
3564 sq_state = sq->state;
3565 } else {
3566 return raw_qp_param->set_mask ? -EINVAL : 0;
3567 }
3568 break;
3569 case MLX5_CMD_OP_INIT2INIT_QP:
3570 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03003571 if (raw_qp_param->set_mask)
3572 return -EINVAL;
3573 else
3574 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003575 default:
3576 WARN_ON(1);
3577 return -EINVAL;
3578 }
3579
Bodong Wang7d29f342016-12-01 13:43:16 +02003580 if (modify_rq) {
Yishai Hadas34d57582018-09-20 21:39:21 +03003581 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param,
3582 qp->ibqp.pd);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003583 if (err)
3584 return err;
3585 }
3586
Bodong Wang7d29f342016-12-01 13:43:16 +02003587 if (modify_sq) {
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003588 struct mlx5_flow_handle *flow_rule;
3589
Aviv Heller13eab212016-09-18 20:48:04 +03003590 if (tx_affinity) {
3591 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003592 tx_affinity,
3593 qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003594 if (err)
3595 return err;
3596 }
3597
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003598 flow_rule = create_flow_rule_vport_sq(dev, sq,
3599 raw_qp_param->port);
3600 if (IS_ERR(flow_rule))
Colin Ian King1db86312019-04-12 11:40:17 +01003601 return PTR_ERR(flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003602
3603 err = modify_raw_packet_qp_sq(dev->mdev, sq, sq_state,
3604 raw_qp_param, qp->ibqp.pd);
3605 if (err) {
3606 if (flow_rule)
3607 mlx5_del_flow_rules(flow_rule);
3608 return err;
3609 }
3610
3611 if (flow_rule) {
3612 destroy_flow_rule_vport_sq(sq);
3613 sq->flow_rule = flow_rule;
3614 }
3615
3616 return err;
Aviv Heller13eab212016-09-18 20:48:04 +03003617 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003618
3619 return 0;
3620}
3621
Maor Gottlieb5163b272020-04-30 22:21:45 +03003622static unsigned int get_tx_affinity_rr(struct mlx5_ib_dev *dev,
3623 struct ib_udata *udata)
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003624{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003625 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
3626 udata, struct mlx5_ib_ucontext, ibucontext);
Maor Gottlieb5163b272020-04-30 22:21:45 +03003627 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
3628 atomic_t *tx_port_affinity;
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003629
Maor Gottlieb5163b272020-04-30 22:21:45 +03003630 if (ucontext)
3631 tx_port_affinity = &ucontext->tx_port_affinity;
3632 else
3633 tx_port_affinity = &dev->port[port_num].roce.tx_port_affinity;
3634
3635 return (unsigned int)atomic_add_return(1, tx_port_affinity) %
3636 MLX5_MAX_PORTS + 1;
3637}
3638
3639static bool qp_supports_affinity(struct ib_qp *qp)
3640{
Maor Gottlieb5163b272020-04-30 22:21:45 +03003641 if ((qp->qp_type == IB_QPT_RC) ||
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003642 (qp->qp_type == IB_QPT_UD) ||
Maor Gottlieb5163b272020-04-30 22:21:45 +03003643 (qp->qp_type == IB_QPT_UC) ||
3644 (qp->qp_type == IB_QPT_RAW_PACKET) ||
3645 (qp->qp_type == IB_QPT_XRC_INI) ||
3646 (qp->qp_type == IB_QPT_XRC_TGT))
3647 return true;
3648 return false;
3649}
3650
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003651static unsigned int get_tx_affinity(struct ib_qp *qp,
3652 const struct ib_qp_attr *attr,
3653 int attr_mask, u8 init,
Maor Gottlieb5163b272020-04-30 22:21:45 +03003654 struct ib_udata *udata)
3655{
3656 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
3657 udata, struct mlx5_ib_ucontext, ibucontext);
3658 struct mlx5_ib_dev *dev = to_mdev(qp->device);
3659 struct mlx5_ib_qp *mqp = to_mqp(qp);
3660 struct mlx5_ib_qp_base *qp_base;
3661 unsigned int tx_affinity;
3662
Mark Zhang802dcc72020-05-27 08:50:14 +03003663 if (!(mlx5_ib_lag_should_assign_affinity(dev) &&
3664 qp_supports_affinity(qp)))
Maor Gottlieb5163b272020-04-30 22:21:45 +03003665 return 0;
3666
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003667 if (mqp->flags & MLX5_IB_QP_CREATE_SQPN_QP1)
3668 tx_affinity = mqp->gsi_lag_port;
3669 else if (init)
3670 tx_affinity = get_tx_affinity_rr(dev, udata);
3671 else if ((attr_mask & IB_QP_AV) && attr->xmit_slave)
3672 tx_affinity =
3673 mlx5_lag_get_slave_port(dev->mdev, attr->xmit_slave);
3674 else
3675 return 0;
Maor Gottlieb5163b272020-04-30 22:21:45 +03003676
3677 qp_base = &mqp->trans_qp.base;
3678 if (ucontext)
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003679 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x ucontext %p\n",
Maor Gottlieb5163b272020-04-30 22:21:45 +03003680 tx_affinity, qp_base->mqp.qpn, ucontext);
3681 else
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003682 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x\n",
Maor Gottlieb5163b272020-04-30 22:21:45 +03003683 tx_affinity, qp_base->mqp.qpn);
3684 return tx_affinity;
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003685}
3686
Mark Zhangd14133d2019-07-02 13:02:36 +03003687static int __mlx5_ib_qp_set_counter(struct ib_qp *qp,
3688 struct rdma_counter *counter)
3689{
3690 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Leon Romanovsky64bae2d2020-05-26 14:54:36 +03003691 u32 in[MLX5_ST_SZ_DW(rts2rts_qp_in)] = {};
Mark Zhangd14133d2019-07-02 13:02:36 +03003692 struct mlx5_ib_qp *mqp = to_mqp(qp);
Mark Zhangd14133d2019-07-02 13:02:36 +03003693 struct mlx5_ib_qp_base *base;
3694 u32 set_id;
Leon Romanovsky64bae2d2020-05-26 14:54:36 +03003695 u32 *qpc;
Mark Zhangd14133d2019-07-02 13:02:36 +03003696
Parav Pandit3e1f0002019-07-23 10:31:17 +03003697 if (counter)
Mark Zhangd14133d2019-07-02 13:02:36 +03003698 set_id = counter->id;
Parav Pandit3e1f0002019-07-23 10:31:17 +03003699 else
3700 set_id = mlx5_ib_get_counters_id(dev, mqp->port - 1);
Mark Zhangd14133d2019-07-02 13:02:36 +03003701
3702 base = &mqp->trans_qp.base;
Leon Romanovsky64bae2d2020-05-26 14:54:36 +03003703 MLX5_SET(rts2rts_qp_in, in, opcode, MLX5_CMD_OP_RTS2RTS_QP);
3704 MLX5_SET(rts2rts_qp_in, in, qpn, base->mqp.qpn);
3705 MLX5_SET(rts2rts_qp_in, in, uid, base->mqp.uid);
3706 MLX5_SET(rts2rts_qp_in, in, opt_param_mask,
3707 MLX5_QP_OPTPAR_COUNTER_SET_ID);
3708
3709 qpc = MLX5_ADDR_OF(rts2rts_qp_in, in, qpc);
3710 MLX5_SET(qpc, qpc, counter_set_id, set_id);
3711 return mlx5_cmd_exec_in(dev->mdev, rts2rts_qp, in);
Mark Zhangd14133d2019-07-02 13:02:36 +03003712}
3713
Eli Cohene126ba92013-07-07 17:25:49 +03003714static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
3715 const struct ib_qp_attr *attr, int attr_mask,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003716 enum ib_qp_state cur_state,
3717 enum ib_qp_state new_state,
3718 const struct mlx5_ib_modify_qp *ucmd,
Leon Romanovsky50aec2c2020-05-26 14:54:40 +03003719 struct mlx5_ib_modify_qp_resp *resp,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003720 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003721{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003722 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
3723 [MLX5_QP_STATE_RST] = {
3724 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3725 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3726 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
3727 },
3728 [MLX5_QP_STATE_INIT] = {
3729 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3730 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3731 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
3732 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
3733 },
3734 [MLX5_QP_STATE_RTR] = {
3735 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3736 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3737 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
3738 },
3739 [MLX5_QP_STATE_RTS] = {
3740 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3741 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3742 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
3743 },
3744 [MLX5_QP_STATE_SQD] = {
3745 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3746 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3747 },
3748 [MLX5_QP_STATE_SQER] = {
3749 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3750 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3751 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
3752 },
3753 [MLX5_QP_STATE_ERR] = {
3754 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3755 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3756 }
3757 };
3758
Eli Cohene126ba92013-07-07 17:25:49 +03003759 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3760 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02003761 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03003762 struct mlx5_ib_cq *send_cq, *recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003763 struct mlx5_ib_pd *pd;
3764 enum mlx5_qp_state mlx5_cur, mlx5_new;
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003765 void *qpc, *pri_path, *alt_path;
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003766 enum mlx5_qp_optpar optpar = 0;
Mark Zhangd14133d2019-07-02 13:02:36 +03003767 u32 set_id = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003768 int mlx5_st;
3769 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003770 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03003771 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003772
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03003773 mlx5_st = to_mlx5_st(qp->type);
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003774 if (mlx5_st < 0)
3775 return -EINVAL;
3776
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003777 qpc = kzalloc(MLX5_ST_SZ_BYTES(qpc), GFP_KERNEL);
3778 if (!qpc)
Eli Cohene126ba92013-07-07 17:25:49 +03003779 return -ENOMEM;
3780
Leon Romanovsky029e88f2020-05-06 09:55:13 +03003781 pd = to_mpd(qp->ibqp.pd);
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003782 MLX5_SET(qpc, qpc, st, mlx5_st);
Eli Cohene126ba92013-07-07 17:25:49 +03003783
3784 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003785 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03003786 } else {
3787 switch (attr->path_mig_state) {
3788 case IB_MIG_MIGRATED:
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003789 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03003790 break;
3791 case IB_MIG_REARM:
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003792 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_REARM);
Eli Cohene126ba92013-07-07 17:25:49 +03003793 break;
3794 case IB_MIG_ARMED:
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003795 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_ARMED);
Eli Cohene126ba92013-07-07 17:25:49 +03003796 break;
3797 }
3798 }
3799
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003800 tx_affinity = get_tx_affinity(ibqp, attr, attr_mask,
Maor Gottlieb5163b272020-04-30 22:21:45 +03003801 cur_state == IB_QPS_RESET &&
3802 new_state == IB_QPS_INIT, udata);
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003803
3804 MLX5_SET(qpc, qpc, lag_tx_port_affinity, tx_affinity);
3805 if (tx_affinity && new_state == IB_QPS_RTR &&
3806 MLX5_CAP_GEN(dev->mdev, init2_lag_tx_port_affinity))
3807 optpar |= MLX5_QP_OPTPAR_LAG_TX_AFF;
Aviv Heller13eab212016-09-18 20:48:04 +03003808
Haggai Erand16e91d2016-02-29 15:45:05 +02003809 if (is_sqp(ibqp->qp_type)) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003810 MLX5_SET(qpc, qpc, mtu, IB_MTU_256);
3811 MLX5_SET(qpc, qpc, log_msg_max, 8);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003812 } else if ((ibqp->qp_type == IB_QPT_UD &&
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003813 !(qp->flags & IB_QP_CREATE_SOURCE_QPN)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003814 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003815 MLX5_SET(qpc, qpc, mtu, IB_MTU_4096);
3816 MLX5_SET(qpc, qpc, log_msg_max, 12);
Eli Cohene126ba92013-07-07 17:25:49 +03003817 } else if (attr_mask & IB_QP_PATH_MTU) {
3818 if (attr->path_mtu < IB_MTU_256 ||
3819 attr->path_mtu > IB_MTU_4096) {
3820 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3821 err = -EINVAL;
3822 goto out;
3823 }
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003824 MLX5_SET(qpc, qpc, mtu, attr->path_mtu);
3825 MLX5_SET(qpc, qpc, log_msg_max,
3826 MLX5_CAP_GEN(dev->mdev, log_max_msg));
Eli Cohene126ba92013-07-07 17:25:49 +03003827 }
3828
3829 if (attr_mask & IB_QP_DEST_QPN)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003830 MLX5_SET(qpc, qpc, remote_qpn, attr->dest_qp_num);
3831
3832 pri_path = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
3833 alt_path = MLX5_ADDR_OF(qpc, qpc, secondary_address_path);
Eli Cohene126ba92013-07-07 17:25:49 +03003834
3835 if (attr_mask & IB_QP_PKEY_INDEX)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003836 MLX5_SET(ads, pri_path, pkey_index, attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003837
3838 /* todo implement counter_index functionality */
3839
3840 if (is_sqp(ibqp->qp_type))
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003841 MLX5_SET(ads, pri_path, vhca_port_num, qp->port);
Eli Cohene126ba92013-07-07 17:25:49 +03003842
3843 if (attr_mask & IB_QP_PORT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003844 MLX5_SET(ads, pri_path, vhca_port_num, attr->port_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003845
3846 if (attr_mask & IB_QP_AV) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003847 err = mlx5_set_path(dev, qp, &attr->ah_attr, pri_path,
3848 attr_mask & IB_QP_PORT ? attr->port_num :
3849 qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003850 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003851 if (err)
3852 goto out;
3853 }
3854
3855 if (attr_mask & IB_QP_TIMEOUT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003856 MLX5_SET(ads, pri_path, ack_timeout, attr->timeout);
Eli Cohene126ba92013-07-07 17:25:49 +03003857
3858 if (attr_mask & IB_QP_ALT_PATH) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003859 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr, alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003860 attr->alt_port_num,
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003861 attr_mask | IB_QP_PKEY_INDEX |
3862 IB_QP_TIMEOUT,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003863 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003864 if (err)
3865 goto out;
3866 }
3867
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003868 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3869 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003870
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003871 MLX5_SET(qpc, qpc, pd, pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3872 if (send_cq)
3873 MLX5_SET(qpc, qpc, cqn_snd, send_cq->mcq.cqn);
3874 if (recv_cq)
3875 MLX5_SET(qpc, qpc, cqn_rcv, recv_cq->mcq.cqn);
3876
3877 MLX5_SET(qpc, qpc, log_ack_req_freq, MLX5_IB_ACK_REQ_FREQ);
Eli Cohene126ba92013-07-07 17:25:49 +03003878
3879 if (attr_mask & IB_QP_RNR_RETRY)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003880 MLX5_SET(qpc, qpc, rnr_retry, attr->rnr_retry);
Eli Cohene126ba92013-07-07 17:25:49 +03003881
3882 if (attr_mask & IB_QP_RETRY_CNT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003883 MLX5_SET(qpc, qpc, retry_count, attr->retry_cnt);
Eli Cohene126ba92013-07-07 17:25:49 +03003884
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003885 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC && attr->max_rd_atomic)
3886 MLX5_SET(qpc, qpc, log_sra_max, ilog2(attr->max_rd_atomic));
Eli Cohene126ba92013-07-07 17:25:49 +03003887
3888 if (attr_mask & IB_QP_SQ_PSN)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003889 MLX5_SET(qpc, qpc, next_send_psn, attr->sq_psn);
Eli Cohene126ba92013-07-07 17:25:49 +03003890
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003891 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC && attr->max_dest_rd_atomic)
3892 MLX5_SET(qpc, qpc, log_rra_max,
3893 ilog2(attr->max_dest_rd_atomic));
Eli Cohene126ba92013-07-07 17:25:49 +03003894
Yonatan Cohena60109d2018-10-10 09:25:16 +03003895 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003896 err = set_qpc_atomic_flags(qp, attr, attr_mask, qpc);
Yonatan Cohena60109d2018-10-10 09:25:16 +03003897 if (err)
3898 goto out;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003899 }
Eli Cohene126ba92013-07-07 17:25:49 +03003900
3901 if (attr_mask & IB_QP_MIN_RNR_TIMER)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003902 MLX5_SET(qpc, qpc, min_rnr_nak, attr->min_rnr_timer);
Eli Cohene126ba92013-07-07 17:25:49 +03003903
3904 if (attr_mask & IB_QP_RQ_PSN)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003905 MLX5_SET(qpc, qpc, next_rcv_psn, attr->rq_psn);
Eli Cohene126ba92013-07-07 17:25:49 +03003906
3907 if (attr_mask & IB_QP_QKEY)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003908 MLX5_SET(qpc, qpc, q_key, attr->qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003909
3910 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003911 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03003912
Mark Bloch0837e862016-06-17 15:10:55 +03003913 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3914 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3915 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003916
3917 /* Underlay port should be used - index 0 function per port */
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003918 if (qp->flags & IB_QP_CREATE_SOURCE_QPN)
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003919 port_num = 0;
3920
Mark Zhangd14133d2019-07-02 13:02:36 +03003921 if (ibqp->counter)
3922 set_id = ibqp->counter->id;
3923 else
Parav Pandit3e1f0002019-07-23 10:31:17 +03003924 set_id = mlx5_ib_get_counters_id(dev, port_num);
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003925 MLX5_SET(qpc, qpc, counter_set_id, set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003926 }
3927
Eli Cohene126ba92013-07-07 17:25:49 +03003928 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003929 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03003930
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003931 if (qp->flags & MLX5_IB_QP_CREATE_SQPN_QP1)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003932 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03003933
3934 mlx5_cur = to_mlx5_state(cur_state);
3935 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003936
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003937 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003938 !optab[mlx5_cur][mlx5_new]) {
3939 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003940 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003941 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003942
3943 op = optab[mlx5_cur][mlx5_new];
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003944 optpar |= ib_mask_to_mlx5_opt(attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003945 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003946
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003947 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003948 qp->flags & IB_QP_CREATE_SOURCE_QPN) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003949 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3950
3951 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003952 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Mark Zhangd14133d2019-07-02 13:02:36 +03003953 raw_qp_param.rq_q_ctr_id = set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003954 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3955 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003956
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003957 if (attr_mask & IB_QP_PORT)
3958 raw_qp_param.port = attr->port_num;
3959
Bodong Wang7d29f342016-12-01 13:43:16 +02003960 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003961 raw_qp_param.rl.rate = attr->rate_limit;
3962
3963 if (ucmd->burst_info.max_burst_sz) {
3964 if (attr->rate_limit &&
3965 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3966 raw_qp_param.rl.max_burst_sz =
3967 ucmd->burst_info.max_burst_sz;
3968 } else {
3969 err = -EINVAL;
3970 goto out;
3971 }
3972 }
3973
3974 if (ucmd->burst_info.typical_pkt_sz) {
3975 if (attr->rate_limit &&
3976 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3977 raw_qp_param.rl.typical_pkt_sz =
3978 ucmd->burst_info.typical_pkt_sz;
3979 } else {
3980 err = -EINVAL;
3981 goto out;
3982 }
3983 }
3984
Bodong Wang7d29f342016-12-01 13:43:16 +02003985 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3986 }
3987
Aviv Heller13eab212016-09-18 20:48:04 +03003988 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003989 } else {
Leon Romanovsky50aec2c2020-05-26 14:54:40 +03003990 if (udata) {
3991 /* For the kernel flows, the resp will stay zero */
3992 resp->ece_options =
3993 MLX5_CAP_GEN(dev->mdev, ece_support) ?
3994 ucmd->ece_options : 0;
3995 resp->response_length = sizeof(*resp);
3996 }
Leon Romanovsky5f62a522020-05-26 14:54:39 +03003997 err = mlx5_core_qp_modify(dev, op, optpar, qpc, &base->mqp,
Leon Romanovsky50aec2c2020-05-26 14:54:40 +03003998 &resp->ece_options);
Alex Vesker0680efa2016-08-28 12:25:52 +03003999 }
4000
Eli Cohene126ba92013-07-07 17:25:49 +03004001 if (err)
4002 goto out;
4003
4004 qp->state = new_state;
4005
4006 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02004007 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03004008 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02004009 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03004010 if (attr_mask & IB_QP_PORT)
4011 qp->port = attr->port_num;
4012 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02004013 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03004014
4015 /*
4016 * If we moved a kernel QP to RESET, clean up all old CQ
4017 * entries and reinitialize the QP.
4018 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02004019 if (new_state == IB_QPS_RESET &&
4020 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02004021 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03004022 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
4023 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02004024 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004025
4026 qp->rq.head = 0;
4027 qp->rq.tail = 0;
4028 qp->sq.head = 0;
4029 qp->sq.tail = 0;
4030 qp->sq.cur_post = 0;
Guy Levi34f4c952018-11-26 08:15:50 +02004031 if (qp->sq.wqe_cnt)
4032 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
Leon Romanovsky950bf4f2020-03-18 11:16:40 +02004033 qp->sq.last_poll = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004034 qp->db.db[MLX5_RCV_DBR] = 0;
4035 qp->db.db[MLX5_SND_DBR] = 0;
4036 }
4037
Mark Zhangd14133d2019-07-02 13:02:36 +03004038 if ((new_state == IB_QPS_RTS) && qp->counter_pending) {
4039 err = __mlx5_ib_qp_set_counter(ibqp, ibqp->counter);
4040 if (!err)
4041 qp->counter_pending = 0;
4042 }
4043
Eli Cohene126ba92013-07-07 17:25:49 +03004044out:
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03004045 kfree(qpc);
Eli Cohene126ba92013-07-07 17:25:49 +03004046 return err;
4047}
4048
Moni Shouac32a4f22018-01-02 16:19:32 +02004049static inline bool is_valid_mask(int mask, int req, int opt)
4050{
4051 if ((mask & req) != req)
4052 return false;
4053
4054 if (mask & ~(req | opt))
4055 return false;
4056
4057 return true;
4058}
4059
4060/* check valid transition for driver QP types
4061 * for now the only QP type that this function supports is DCI
4062 */
4063static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
4064 enum ib_qp_attr_mask attr_mask)
4065{
4066 int req = IB_QP_STATE;
4067 int opt = 0;
4068
Moni Shoua99ed7482018-09-12 09:33:55 +03004069 if (new_state == IB_QPS_RESET) {
4070 return is_valid_mask(attr_mask, req, opt);
4071 } else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Moni Shouac32a4f22018-01-02 16:19:32 +02004072 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
4073 return is_valid_mask(attr_mask, req, opt);
4074 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
4075 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
4076 return is_valid_mask(attr_mask, req, opt);
4077 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
4078 req |= IB_QP_PATH_MTU;
Artemy Kovalyov5ec03042018-11-05 08:12:07 +02004079 opt = IB_QP_PKEY_INDEX | IB_QP_AV;
Moni Shouac32a4f22018-01-02 16:19:32 +02004080 return is_valid_mask(attr_mask, req, opt);
4081 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
4082 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
4083 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
4084 opt = IB_QP_MIN_RNR_TIMER;
4085 return is_valid_mask(attr_mask, req, opt);
4086 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
4087 opt = IB_QP_MIN_RNR_TIMER;
4088 return is_valid_mask(attr_mask, req, opt);
4089 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
4090 return is_valid_mask(attr_mask, req, opt);
4091 }
4092 return false;
4093}
4094
Moni Shoua776a3902018-01-02 16:19:33 +02004095/* mlx5_ib_modify_dct: modify a DCT QP
4096 * valid transitions are:
4097 * RESET to INIT: must set access_flags, pkey_index and port
4098 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
4099 * mtu, gid_index and hop_limit
4100 * Other transitions and attributes are illegal
4101 */
4102static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
Leon Romanovskya645a892020-06-02 15:55:48 +03004103 int attr_mask, struct mlx5_ib_modify_qp *ucmd,
4104 struct ib_udata *udata)
Moni Shoua776a3902018-01-02 16:19:33 +02004105{
4106 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4107 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4108 enum ib_qp_state cur_state, new_state;
4109 int err = 0;
4110 int required = IB_QP_STATE;
4111 void *dctc;
4112
4113 if (!(attr_mask & IB_QP_STATE))
4114 return -EINVAL;
4115
4116 cur_state = qp->state;
4117 new_state = attr->qp_state;
4118
4119 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Leon Romanovskya645a892020-06-02 15:55:48 +03004120 if (MLX5_CAP_GEN(dev->mdev, ece_support) && ucmd->ece_options)
4121 /*
4122 * DCT doesn't initialize QP till modify command is executed,
4123 * so we need to overwrite previously set ECE field if user
4124 * provided any value except zero, which means not set/not
4125 * valid.
4126 */
4127 MLX5_SET(dctc, dctc, ece, ucmd->ece_options);
4128
Moni Shoua776a3902018-01-02 16:19:33 +02004129 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03004130 u16 set_id;
4131
Moni Shoua776a3902018-01-02 16:19:33 +02004132 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
4133 if (!is_valid_mask(attr_mask, required, 0))
4134 return -EINVAL;
4135
4136 if (attr->port_num == 0 ||
4137 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
4138 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
4139 attr->port_num, dev->num_ports);
4140 return -EINVAL;
4141 }
4142 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
4143 MLX5_SET(dctc, dctc, rre, 1);
4144 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
4145 MLX5_SET(dctc, dctc, rwe, 1);
4146 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03004147 int atomic_mode;
4148
4149 atomic_mode = get_atomic_mode(dev, MLX5_IB_QPT_DCT);
4150 if (atomic_mode < 0)
Moni Shoua776a3902018-01-02 16:19:33 +02004151 return -EOPNOTSUPP;
Yonatan Cohena60109d2018-10-10 09:25:16 +03004152
4153 MLX5_SET(dctc, dctc, atomic_mode, atomic_mode);
Moni Shoua776a3902018-01-02 16:19:33 +02004154 MLX5_SET(dctc, dctc, rae, 1);
Moni Shoua776a3902018-01-02 16:19:33 +02004155 }
4156 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
4157 MLX5_SET(dctc, dctc, port, attr->port_num);
Parav Pandit3e1f0002019-07-23 10:31:17 +03004158
4159 set_id = mlx5_ib_get_counters_id(dev, attr->port_num - 1);
4160 MLX5_SET(dctc, dctc, counter_set_id, set_id);
Moni Shoua776a3902018-01-02 16:19:33 +02004161 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
4162 struct mlx5_ib_modify_qp_resp resp = {};
Leon Romanovskya645a892020-06-02 15:55:48 +03004163 u32 out[MLX5_ST_SZ_DW(create_dct_out)] = {};
4164 u32 min_resp_len = offsetofend(typeof(resp), dctn);
Moni Shoua776a3902018-01-02 16:19:33 +02004165
4166 if (udata->outlen < min_resp_len)
4167 return -EINVAL;
Leon Romanovskya645a892020-06-02 15:55:48 +03004168 /*
4169 * If we don't have enough space for the ECE options,
4170 * simply indicate it with resp.response_length.
4171 */
4172 resp.response_length = (udata->outlen < sizeof(resp)) ?
4173 min_resp_len :
4174 sizeof(resp);
4175
Moni Shoua776a3902018-01-02 16:19:33 +02004176 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
4177 if (!is_valid_mask(attr_mask, required, 0))
4178 return -EINVAL;
4179 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
4180 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
4181 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
4182 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
4183 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
4184 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
4185
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004186 err = mlx5_core_create_dct(dev, &qp->dct.mdct, qp->dct.in,
Yishai Hadasc5ae1952019-03-06 19:21:42 +02004187 MLX5_ST_SZ_BYTES(create_dct_in), out,
4188 sizeof(out));
Moni Shoua776a3902018-01-02 16:19:33 +02004189 if (err)
4190 return err;
4191 resp.dctn = qp->dct.mdct.mqp.qpn;
Leon Romanovskya645a892020-06-02 15:55:48 +03004192 if (MLX5_CAP_GEN(dev->mdev, ece_support))
4193 resp.ece_options = MLX5_GET(create_dct_out, out, ece);
Moni Shoua776a3902018-01-02 16:19:33 +02004194 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4195 if (err) {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004196 mlx5_core_destroy_dct(dev, &qp->dct.mdct);
Moni Shoua776a3902018-01-02 16:19:33 +02004197 return err;
4198 }
4199 } else {
4200 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
4201 return -EINVAL;
4202 }
4203 if (err)
4204 qp->state = IB_QPS_ERR;
4205 else
4206 qp->state = new_state;
4207 return err;
4208}
4209
Eli Cohene126ba92013-07-07 17:25:49 +03004210int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
4211 int attr_mask, struct ib_udata *udata)
4212{
4213 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Leon Romanovsky50aec2c2020-05-26 14:54:40 +03004214 struct mlx5_ib_modify_qp_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03004215 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02004216 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02004217 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03004218 enum ib_qp_state cur_state, new_state;
4219 int err = -EINVAL;
4220 int port;
4221
Yishai Hadas28d61372016-05-23 15:20:56 +03004222 if (ibqp->rwq_ind_tbl)
4223 return -ENOSYS;
4224
Bodong Wang61147f32018-03-19 15:10:30 +02004225 if (udata && udata->inlen) {
Leon Romanovsky5f62a522020-05-26 14:54:39 +03004226 if (udata->inlen < offsetofend(typeof(ucmd), ece_options))
Bodong Wang61147f32018-03-19 15:10:30 +02004227 return -EINVAL;
4228
4229 if (udata->inlen > sizeof(ucmd) &&
4230 !ib_is_udata_cleared(udata, sizeof(ucmd),
4231 udata->inlen - sizeof(ucmd)))
4232 return -EOPNOTSUPP;
4233
4234 if (ib_copy_from_udata(&ucmd, udata,
4235 min(udata->inlen, sizeof(ucmd))))
4236 return -EFAULT;
4237
4238 if (ucmd.comp_mask ||
Bodong Wang61147f32018-03-19 15:10:30 +02004239 memchr_inv(&ucmd.burst_info.reserved, 0,
4240 sizeof(ucmd.burst_info.reserved)))
4241 return -EOPNOTSUPP;
Leon Romanovsky5f62a522020-05-26 14:54:39 +03004242
Bodong Wang61147f32018-03-19 15:10:30 +02004243 }
4244
Haggai Erand16e91d2016-02-29 15:45:05 +02004245 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4246 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
4247
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03004248 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ? IB_QPT_GSI :
4249 qp->type;
Moni Shouac32a4f22018-01-02 16:19:32 +02004250
Leon Romanovskya645a892020-06-02 15:55:48 +03004251 if (qp_type == MLX5_IB_QPT_DCT)
4252 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, &ucmd, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02004253
Eli Cohene126ba92013-07-07 17:25:49 +03004254 mutex_lock(&qp->mutex);
4255
4256 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
4257 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
4258
Achiad Shochat2811ba52015-12-23 18:47:24 +02004259 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
4260 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02004261 }
4262
Leon Romanovsky2be08c32020-04-27 18:46:13 +03004263 if (qp->flags & IB_QP_CREATE_SOURCE_QPN) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004264 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
4265 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
4266 attr_mask);
4267 goto out;
4268 }
4269 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02004270 qp_type != MLX5_IB_QPT_DCI &&
Kamal Heibd31131b2018-10-02 16:11:21 +03004271 !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
4272 attr_mask)) {
Haggai Eran158abf82016-02-29 15:45:04 +02004273 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
4274 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03004275 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02004276 } else if (qp_type == MLX5_IB_QPT_DCI &&
4277 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
4278 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
4279 cur_state, new_state, qp_type, attr_mask);
4280 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004281 }
Eli Cohene126ba92013-07-07 17:25:49 +03004282
4283 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004284 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02004285 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02004286 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
4287 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03004288 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004289 }
Eli Cohene126ba92013-07-07 17:25:49 +03004290
4291 if (attr_mask & IB_QP_PKEY_INDEX) {
4292 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03004293 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02004294 dev->mdev->port_caps[port - 1].pkey_table_len) {
4295 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
4296 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004297 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004298 }
Eli Cohene126ba92013-07-07 17:25:49 +03004299 }
4300
4301 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004302 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02004303 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
4304 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
4305 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03004306 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004307 }
Eli Cohene126ba92013-07-07 17:25:49 +03004308
4309 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004310 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02004311 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
4312 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
4313 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03004314 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004315 }
Eli Cohene126ba92013-07-07 17:25:49 +03004316
4317 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
4318 err = 0;
4319 goto out;
4320 }
4321
Bodong Wang61147f32018-03-19 15:10:30 +02004322 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
Leon Romanovsky50aec2c2020-05-26 14:54:40 +03004323 new_state, &ucmd, &resp, udata);
4324
4325 /* resp.response_length is set in ECE supported flows only */
4326 if (!err && resp.response_length &&
4327 udata->outlen >= resp.response_length)
Leon Romanovsky6512f112020-06-02 15:55:46 +03004328 /* Return -EFAULT to the user and expect him to destroy QP. */
4329 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03004330
4331out:
4332 mutex_unlock(&qp->mutex);
4333 return err;
4334}
4335
Eli Cohene126ba92013-07-07 17:25:49 +03004336static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4337{
4338 switch (mlx5_state) {
4339 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4340 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4341 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4342 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4343 case MLX5_QP_STATE_SQ_DRAINING:
4344 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4345 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4346 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4347 default: return -1;
4348 }
4349}
4350
4351static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4352{
4353 switch (mlx5_mig_state) {
4354 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4355 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4356 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4357 default: return -1;
4358 }
4359}
4360
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004361static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004362 struct rdma_ah_attr *ah_attr, void *path)
Eli Cohene126ba92013-07-07 17:25:49 +03004363{
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004364 int port = MLX5_GET(ads, path, vhca_port_num);
4365 int static_rate;
Eli Cohene126ba92013-07-07 17:25:49 +03004366
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004367 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03004368
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004369 if (!port || port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03004370 return;
4371
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004372 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port);
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02004373
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004374 rdma_ah_set_port_num(ah_attr, port);
4375 rdma_ah_set_sl(ah_attr, MLX5_GET(ads, path, sl));
Eli Cohene126ba92013-07-07 17:25:49 +03004376
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004377 rdma_ah_set_dlid(ah_attr, MLX5_GET(ads, path, rlid));
4378 rdma_ah_set_path_bits(ah_attr, MLX5_GET(ads, path, mlid));
Aharon Landau2d7e3ff2020-04-13 16:20:28 +03004379
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004380 static_rate = MLX5_GET(ads, path, stat_rate);
4381 rdma_ah_set_static_rate(ah_attr, static_rate ? static_rate - 5 : 0);
4382 if (MLX5_GET(ads, path, grh) ||
Aharon Landau2d7e3ff2020-04-13 16:20:28 +03004383 ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004384 rdma_ah_set_grh(ah_attr, NULL, MLX5_GET(ads, path, flow_label),
4385 MLX5_GET(ads, path, src_addr_index),
4386 MLX5_GET(ads, path, hop_limit),
4387 MLX5_GET(ads, path, tclass));
Maor Gottliebd4433552020-06-18 14:25:07 +03004388 rdma_ah_set_dgid_raw(ah_attr, MLX5_ADDR_OF(ads, path, rgid_rip));
Eli Cohene126ba92013-07-07 17:25:49 +03004389 }
4390}
4391
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004392static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4393 struct mlx5_ib_sq *sq,
4394 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004395{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004396 int err;
4397
Eran Ben Elisha28160772017-12-26 15:17:05 +02004398 err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004399 if (err)
4400 goto out;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004401 sq->state = *sq_state;
4402
4403out:
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004404 return err;
4405}
4406
4407static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
4408 struct mlx5_ib_rq *rq,
4409 u8 *rq_state)
4410{
4411 void *out;
4412 void *rqc;
4413 int inlen;
4414 int err;
4415
4416 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004417 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004418 if (!out)
4419 return -ENOMEM;
4420
4421 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
4422 if (err)
4423 goto out;
4424
4425 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
4426 *rq_state = MLX5_GET(rqc, rqc, state);
4427 rq->state = *rq_state;
4428
4429out:
4430 kvfree(out);
4431 return err;
4432}
4433
4434static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
4435 struct mlx5_ib_qp *qp, u8 *qp_state)
4436{
4437 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
4438 [MLX5_RQC_STATE_RST] = {
4439 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4440 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4441 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
4442 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
4443 },
4444 [MLX5_RQC_STATE_RDY] = {
4445 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4446 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4447 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
4448 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
4449 },
4450 [MLX5_RQC_STATE_ERR] = {
4451 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4452 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4453 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
4454 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
4455 },
4456 [MLX5_RQ_STATE_NA] = {
4457 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4458 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4459 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
4460 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
4461 },
4462 };
4463
4464 *qp_state = sqrq_trans[rq_state][sq_state];
4465
4466 if (*qp_state == MLX5_QP_STATE_BAD) {
4467 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4468 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
4469 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
4470 return -EINVAL;
4471 }
4472
4473 if (*qp_state == MLX5_QP_STATE)
4474 *qp_state = qp->state;
4475
4476 return 0;
4477}
4478
4479static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
4480 struct mlx5_ib_qp *qp,
4481 u8 *raw_packet_qp_state)
4482{
4483 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
4484 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
4485 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
4486 int err;
4487 u8 sq_state = MLX5_SQ_STATE_NA;
4488 u8 rq_state = MLX5_RQ_STATE_NA;
4489
4490 if (qp->sq.wqe_cnt) {
4491 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
4492 if (err)
4493 return err;
4494 }
4495
4496 if (qp->rq.wqe_cnt) {
4497 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
4498 if (err)
4499 return err;
4500 }
4501
4502 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
4503 raw_packet_qp_state);
4504}
4505
4506static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
4507 struct ib_qp_attr *qp_attr)
4508{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004509 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004510 void *qpc, *pri_path, *alt_path;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004511 u32 *outb;
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004512 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03004513
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004514 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004515 if (!outb)
4516 return -ENOMEM;
4517
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004518 err = mlx5_core_qp_query(dev, &qp->trans_qp.base.mqp, outb, outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03004519 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004520 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004521
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004522 qpc = MLX5_ADDR_OF(query_qp_out, outb, qpc);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004523
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004524 qp->state = to_ib_qp_state(MLX5_GET(qpc, qpc, state));
4525 if (MLX5_GET(qpc, qpc, state) == MLX5_QP_STATE_SQ_DRAINING)
4526 qp_attr->sq_draining = 1;
Eli Cohene126ba92013-07-07 17:25:49 +03004527
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004528 qp_attr->path_mtu = MLX5_GET(qpc, qpc, mtu);
4529 qp_attr->path_mig_state = to_ib_mig_state(MLX5_GET(qpc, qpc, pm_state));
4530 qp_attr->qkey = MLX5_GET(qpc, qpc, q_key);
4531 qp_attr->rq_psn = MLX5_GET(qpc, qpc, next_rcv_psn);
4532 qp_attr->sq_psn = MLX5_GET(qpc, qpc, next_send_psn);
4533 qp_attr->dest_qp_num = MLX5_GET(qpc, qpc, remote_qpn);
4534
4535 if (MLX5_GET(qpc, qpc, rre))
4536 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ;
4537 if (MLX5_GET(qpc, qpc, rwe))
4538 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_WRITE;
4539 if (MLX5_GET(qpc, qpc, rae))
4540 qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_ATOMIC;
4541
4542 qp_attr->max_rd_atomic = 1 << MLX5_GET(qpc, qpc, log_sra_max);
4543 qp_attr->max_dest_rd_atomic = 1 << MLX5_GET(qpc, qpc, log_rra_max);
4544 qp_attr->min_rnr_timer = MLX5_GET(qpc, qpc, min_rnr_nak);
4545 qp_attr->retry_cnt = MLX5_GET(qpc, qpc, retry_count);
4546 qp_attr->rnr_retry = MLX5_GET(qpc, qpc, rnr_retry);
4547
4548 pri_path = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
4549 alt_path = MLX5_ADDR_OF(qpc, qpc, secondary_address_path);
Eli Cohene126ba92013-07-07 17:25:49 +03004550
4551 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004552 to_rdma_ah_attr(dev, &qp_attr->ah_attr, pri_path);
4553 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, alt_path);
4554 qp_attr->alt_pkey_index = MLX5_GET(ads, alt_path, pkey_index);
4555 qp_attr->alt_port_num = MLX5_GET(ads, alt_path, vhca_port_num);
Eli Cohene126ba92013-07-07 17:25:49 +03004556 }
4557
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004558 qp_attr->pkey_index = MLX5_GET(ads, pri_path, pkey_index);
4559 qp_attr->port_num = MLX5_GET(ads, pri_path, vhca_port_num);
4560 qp_attr->timeout = MLX5_GET(ads, pri_path, ack_timeout);
4561 qp_attr->alt_timeout = MLX5_GET(ads, alt_path, ack_timeout);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004562
4563out:
4564 kfree(outb);
4565 return err;
4566}
4567
Moni Shoua776a3902018-01-02 16:19:33 +02004568static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
4569 struct ib_qp_attr *qp_attr, int qp_attr_mask,
4570 struct ib_qp_init_attr *qp_init_attr)
4571{
4572 struct mlx5_core_dct *dct = &mqp->dct.mdct;
4573 u32 *out;
4574 u32 access_flags = 0;
4575 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
4576 void *dctc;
4577 int err;
4578 int supported_mask = IB_QP_STATE |
4579 IB_QP_ACCESS_FLAGS |
4580 IB_QP_PORT |
4581 IB_QP_MIN_RNR_TIMER |
4582 IB_QP_AV |
4583 IB_QP_PATH_MTU |
4584 IB_QP_PKEY_INDEX;
4585
4586 if (qp_attr_mask & ~supported_mask)
4587 return -EINVAL;
4588 if (mqp->state != IB_QPS_RTR)
4589 return -EINVAL;
4590
4591 out = kzalloc(outlen, GFP_KERNEL);
4592 if (!out)
4593 return -ENOMEM;
4594
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004595 err = mlx5_core_dct_query(dev, dct, out, outlen);
Moni Shoua776a3902018-01-02 16:19:33 +02004596 if (err)
4597 goto out;
4598
4599 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
4600
4601 if (qp_attr_mask & IB_QP_STATE)
4602 qp_attr->qp_state = IB_QPS_RTR;
4603
4604 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
4605 if (MLX5_GET(dctc, dctc, rre))
4606 access_flags |= IB_ACCESS_REMOTE_READ;
4607 if (MLX5_GET(dctc, dctc, rwe))
4608 access_flags |= IB_ACCESS_REMOTE_WRITE;
4609 if (MLX5_GET(dctc, dctc, rae))
4610 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
4611 qp_attr->qp_access_flags = access_flags;
4612 }
4613
4614 if (qp_attr_mask & IB_QP_PORT)
4615 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
4616 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
4617 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
4618 if (qp_attr_mask & IB_QP_AV) {
4619 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
4620 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
4621 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
4622 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
4623 }
4624 if (qp_attr_mask & IB_QP_PATH_MTU)
4625 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
4626 if (qp_attr_mask & IB_QP_PKEY_INDEX)
4627 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
4628out:
4629 kfree(out);
4630 return err;
4631}
4632
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004633int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
4634 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
4635{
4636 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4637 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4638 int err = 0;
4639 u8 raw_packet_qp_state;
4640
Yishai Hadas28d61372016-05-23 15:20:56 +03004641 if (ibqp->rwq_ind_tbl)
4642 return -ENOSYS;
4643
Haggai Erand16e91d2016-02-29 15:45:05 +02004644 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4645 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
4646 qp_init_attr);
4647
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004648 /* Not all of output fields are applicable, make sure to zero them */
4649 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
4650 memset(qp_attr, 0, sizeof(*qp_attr));
4651
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03004652 if (unlikely(qp->type == MLX5_IB_QPT_DCT))
Moni Shoua776a3902018-01-02 16:19:33 +02004653 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
4654 qp_attr_mask, qp_init_attr);
4655
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004656 mutex_lock(&qp->mutex);
4657
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004658 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03004659 qp->flags & IB_QP_CREATE_SOURCE_QPN) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004660 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
4661 if (err)
4662 goto out;
4663 qp->state = raw_packet_qp_state;
4664 qp_attr->port_num = 1;
4665 } else {
4666 err = query_qp_attr(dev, qp, qp_attr);
4667 if (err)
4668 goto out;
4669 }
4670
4671 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03004672 qp_attr->cur_qp_state = qp_attr->qp_state;
4673 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
4674 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
4675
4676 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03004677 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03004678 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03004679 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03004680 } else {
4681 qp_attr->cap.max_send_wr = 0;
4682 qp_attr->cap.max_send_sge = 0;
4683 }
4684
Noa Osherovich0540d812016-06-04 15:15:32 +03004685 qp_init_attr->qp_type = ibqp->qp_type;
4686 qp_init_attr->recv_cq = ibqp->recv_cq;
4687 qp_init_attr->send_cq = ibqp->send_cq;
4688 qp_init_attr->srq = ibqp->srq;
4689 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03004690
4691 qp_init_attr->cap = qp_attr->cap;
4692
Leon Romanovskya8f3ea62020-04-27 18:46:17 +03004693 qp_init_attr->create_flags = qp->flags;
Leon Romanovsky051f2632015-12-20 12:16:11 +02004694
Eli Cohene126ba92013-07-07 17:25:49 +03004695 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
4696 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
4697
Eli Cohene126ba92013-07-07 17:25:49 +03004698out:
4699 mutex_unlock(&qp->mutex);
4700 return err;
4701}
4702
4703struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004704 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03004705{
4706 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4707 struct mlx5_ib_xrcd *xrcd;
4708 int err;
4709
Saeed Mahameed938fe832015-05-28 22:28:41 +03004710 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03004711 return ERR_PTR(-ENOSYS);
4712
4713 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
4714 if (!xrcd)
4715 return ERR_PTR(-ENOMEM);
4716
Yishai Hadas5aa37712018-11-26 08:28:38 +02004717 err = mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004718 if (err) {
4719 kfree(xrcd);
4720 return ERR_PTR(-ENOMEM);
4721 }
4722
4723 return &xrcd->ibxrcd;
4724}
4725
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004726int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03004727{
4728 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
4729 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
4730 int err;
4731
Yishai Hadas5aa37712018-11-26 08:28:38 +02004732 err = mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, 0);
Leon Romanovskyb0818082018-01-28 11:25:30 +02004733 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03004734 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004735
4736 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03004737 return 0;
4738}
Yishai Hadas79b20a62016-05-23 15:20:50 +03004739
Yishai Hadas350d0e42016-08-28 14:58:18 +03004740static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
4741{
4742 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
4743 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
4744 struct ib_event event;
4745
4746 if (rwq->ibwq.event_handler) {
4747 event.device = rwq->ibwq.device;
4748 event.element.wq = &rwq->ibwq;
4749 switch (type) {
4750 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
4751 event.event = IB_EVENT_WQ_FATAL;
4752 break;
4753 default:
4754 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
4755 return;
4756 }
4757
4758 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
4759 }
4760}
4761
Maor Gottlieb03404e82017-05-30 10:29:13 +03004762static int set_delay_drop(struct mlx5_ib_dev *dev)
4763{
4764 int err = 0;
4765
4766 mutex_lock(&dev->delay_drop.lock);
4767 if (dev->delay_drop.activate)
4768 goto out;
4769
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004770 err = mlx5_core_set_delay_drop(dev, dev->delay_drop.timeout);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004771 if (err)
4772 goto out;
4773
4774 dev->delay_drop.activate = true;
4775out:
4776 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004777
4778 if (!err)
4779 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004780 return err;
4781}
4782
Yishai Hadas79b20a62016-05-23 15:20:50 +03004783static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
4784 struct ib_wq_init_attr *init_attr)
4785{
4786 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02004787 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004788 __be64 *rq_pas0;
4789 void *in;
4790 void *rqc;
4791 void *wq;
4792 int inlen;
4793 int err;
4794
4795 dev = to_mdev(pd->device);
4796
4797 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004798 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004799 if (!in)
4800 return -ENOMEM;
4801
Yishai Hadas34d57582018-09-20 21:39:21 +03004802 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004803 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
4804 MLX5_SET(rqc, rqc, mem_rq_type,
4805 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
4806 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
4807 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
4808 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
4809 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
4810 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03004811 MLX5_SET(wq, wq, wq_type,
4812 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
4813 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02004814 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
4815 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
4816 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
4817 err = -EOPNOTSUPP;
4818 goto out;
4819 } else {
4820 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
4821 }
4822 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004823 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03004824 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
Mark Zhangc16339b2019-11-15 17:45:55 +02004825 /*
4826 * In Firmware number of strides in each WQE is:
4827 * "512 * 2^single_wqe_log_num_of_strides"
4828 * Values 3 to 8 are accepted as 10 to 15, 9 to 18 are
4829 * accepted as 0 to 9
4830 */
4831 static const u8 fw_map[] = { 10, 11, 12, 13, 14, 15, 0, 1,
4832 2, 3, 4, 5, 6, 7, 8, 9 };
Noa Osherovichccc87082017-10-17 18:01:13 +03004833 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
4834 MLX5_SET(wq, wq, log_wqe_stride_size,
4835 rwq->single_stride_log_num_of_bytes -
4836 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
Mark Zhangc16339b2019-11-15 17:45:55 +02004837 MLX5_SET(wq, wq, log_wqe_num_of_strides,
4838 fw_map[rwq->log_num_strides -
4839 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES]);
Noa Osherovichccc87082017-10-17 18:01:13 +03004840 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004841 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
4842 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
4843 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
4844 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
4845 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
4846 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02004847 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004848 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02004849 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004850 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
4851 err = -EOPNOTSUPP;
4852 goto out;
4853 }
4854 } else {
4855 MLX5_SET(rqc, rqc, vsd, 1);
4856 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02004857 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
4858 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
4859 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
4860 err = -EOPNOTSUPP;
4861 goto out;
4862 }
4863 MLX5_SET(rqc, rqc, scatter_fcs, 1);
4864 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03004865 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4866 if (!(dev->ib_dev.attrs.raw_packet_caps &
4867 IB_RAW_PACKET_CAP_DELAY_DROP)) {
4868 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
4869 err = -EOPNOTSUPP;
4870 goto out;
4871 }
4872 MLX5_SET(rqc, rqc, delay_drop_en, 1);
4873 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004874 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
4875 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004876 err = mlx5_core_create_rq_tracked(dev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004877 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4878 err = set_delay_drop(dev);
4879 if (err) {
4880 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
4881 err);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004882 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004883 } else {
4884 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
4885 }
4886 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004887out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03004888 kvfree(in);
4889 return err;
4890}
4891
4892static int set_user_rq_size(struct mlx5_ib_dev *dev,
4893 struct ib_wq_init_attr *wq_init_attr,
4894 struct mlx5_ib_create_wq *ucmd,
4895 struct mlx5_ib_rwq *rwq)
4896{
4897 /* Sanity check RQ size before proceeding */
4898 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
4899 return -EINVAL;
4900
4901 if (!ucmd->rq_wqe_count)
4902 return -EINVAL;
4903
4904 rwq->wqe_count = ucmd->rq_wqe_count;
4905 rwq->wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky0dfe4522018-08-01 14:25:41 -07004906 if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
4907 return -EINVAL;
4908
Yishai Hadas79b20a62016-05-23 15:20:50 +03004909 rwq->log_rq_stride = rwq->wqe_shift;
4910 rwq->log_rq_size = ilog2(rwq->wqe_count);
4911 return 0;
4912}
4913
Mark Zhangc16339b2019-11-15 17:45:55 +02004914static bool log_of_strides_valid(struct mlx5_ib_dev *dev, u32 log_num_strides)
4915{
4916 if ((log_num_strides > MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
4917 (log_num_strides < MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
4918 return false;
4919
4920 if (!MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) &&
4921 (log_num_strides < MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
4922 return false;
4923
4924 return true;
4925}
4926
Yishai Hadas79b20a62016-05-23 15:20:50 +03004927static int prepare_user_rq(struct ib_pd *pd,
4928 struct ib_wq_init_attr *init_attr,
4929 struct ib_udata *udata,
4930 struct mlx5_ib_rwq *rwq)
4931{
4932 struct mlx5_ib_dev *dev = to_mdev(pd->device);
4933 struct mlx5_ib_create_wq ucmd = {};
4934 int err;
4935 size_t required_cmd_sz;
4936
Noa Osherovichccc87082017-10-17 18:01:13 +03004937 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
4938 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004939 if (udata->inlen < required_cmd_sz) {
4940 mlx5_ib_dbg(dev, "invalid inlen\n");
4941 return -EINVAL;
4942 }
4943
4944 if (udata->inlen > sizeof(ucmd) &&
4945 !ib_is_udata_cleared(udata, sizeof(ucmd),
4946 udata->inlen - sizeof(ucmd))) {
4947 mlx5_ib_dbg(dev, "inlen is not supported\n");
4948 return -EOPNOTSUPP;
4949 }
4950
4951 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
4952 mlx5_ib_dbg(dev, "copy failed\n");
4953 return -EFAULT;
4954 }
4955
Noa Osherovichccc87082017-10-17 18:01:13 +03004956 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03004957 mlx5_ib_dbg(dev, "invalid comp mask\n");
4958 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03004959 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
4960 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
4961 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
4962 return -EOPNOTSUPP;
4963 }
4964 if ((ucmd.single_stride_log_num_of_bytes <
4965 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
4966 (ucmd.single_stride_log_num_of_bytes >
4967 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
4968 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
4969 ucmd.single_stride_log_num_of_bytes,
4970 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
4971 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
4972 return -EINVAL;
4973 }
Mark Zhangc16339b2019-11-15 17:45:55 +02004974 if (!log_of_strides_valid(dev,
4975 ucmd.single_wqe_log_num_of_strides)) {
4976 mlx5_ib_dbg(
4977 dev,
4978 "Invalid log num strides (%u. Range is %u - %u)\n",
4979 ucmd.single_wqe_log_num_of_strides,
4980 MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) ?
4981 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES :
4982 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
4983 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
Noa Osherovichccc87082017-10-17 18:01:13 +03004984 return -EINVAL;
4985 }
4986 rwq->single_stride_log_num_of_bytes =
4987 ucmd.single_stride_log_num_of_bytes;
4988 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
4989 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
4990 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004991 }
4992
4993 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
4994 if (err) {
4995 mlx5_ib_dbg(dev, "err %d\n", err);
4996 return err;
4997 }
4998
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02004999 err = create_user_rq(dev, pd, udata, rwq, &ucmd);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005000 if (err) {
5001 mlx5_ib_dbg(dev, "err %d\n", err);
Gal Pressman645ba592018-10-08 19:44:03 +03005002 return err;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005003 }
5004
5005 rwq->user_index = ucmd.user_index;
5006 return 0;
5007}
5008
5009struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
5010 struct ib_wq_init_attr *init_attr,
5011 struct ib_udata *udata)
5012{
5013 struct mlx5_ib_dev *dev;
5014 struct mlx5_ib_rwq *rwq;
5015 struct mlx5_ib_create_wq_resp resp = {};
5016 size_t min_resp_len;
5017 int err;
5018
5019 if (!udata)
5020 return ERR_PTR(-ENOSYS);
5021
5022 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5023 if (udata->outlen && udata->outlen < min_resp_len)
5024 return ERR_PTR(-EINVAL);
5025
Maor Gottliebba800132020-03-22 14:49:06 +02005026 if (!capable(CAP_SYS_RAWIO) &&
5027 init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP)
5028 return ERR_PTR(-EPERM);
5029
Yishai Hadas79b20a62016-05-23 15:20:50 +03005030 dev = to_mdev(pd->device);
5031 switch (init_attr->wq_type) {
5032 case IB_WQT_RQ:
5033 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
5034 if (!rwq)
5035 return ERR_PTR(-ENOMEM);
5036 err = prepare_user_rq(pd, init_attr, udata, rwq);
5037 if (err)
5038 goto err;
5039 err = create_rq(rwq, pd, init_attr);
5040 if (err)
5041 goto err_user_rq;
5042 break;
5043 default:
5044 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
5045 init_attr->wq_type);
5046 return ERR_PTR(-EINVAL);
5047 }
5048
Yishai Hadas350d0e42016-08-28 14:58:18 +03005049 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005050 rwq->ibwq.state = IB_WQS_RESET;
5051 if (udata->outlen) {
5052 resp.response_length = offsetof(typeof(resp), response_length) +
5053 sizeof(resp.response_length);
5054 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5055 if (err)
5056 goto err_copy;
5057 }
5058
Yishai Hadas350d0e42016-08-28 14:58:18 +03005059 rwq->core_qp.event = mlx5_ib_wq_event;
5060 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005061 return &rwq->ibwq;
5062
5063err_copy:
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03005064 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005065err_user_rq:
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03005066 destroy_user_rq(dev, pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005067err:
5068 kfree(rwq);
5069 return ERR_PTR(err);
5070}
5071
Leon Romanovskya49b1dc2019-06-12 15:27:41 +03005072void mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +03005073{
5074 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5075 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5076
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03005077 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03005078 destroy_user_rq(dev, wq->pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005079 kfree(rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005080}
5081
Yishai Hadasc5f90922016-05-23 15:20:53 +03005082struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
5083 struct ib_rwq_ind_table_init_attr *init_attr,
5084 struct ib_udata *udata)
5085{
5086 struct mlx5_ib_dev *dev = to_mdev(device);
5087 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
5088 int sz = 1 << init_attr->log_ind_tbl_size;
5089 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
5090 size_t min_resp_len;
5091 int inlen;
5092 int err;
5093 int i;
5094 u32 *in;
5095 void *rqtc;
5096
5097 if (udata->inlen > 0 &&
5098 !ib_is_udata_cleared(udata, 0,
5099 udata->inlen))
5100 return ERR_PTR(-EOPNOTSUPP);
5101
Maor Gottliebefd7f402016-10-27 16:36:40 +03005102 if (init_attr->log_ind_tbl_size >
5103 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
5104 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
5105 init_attr->log_ind_tbl_size,
5106 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
5107 return ERR_PTR(-EINVAL);
5108 }
5109
Yishai Hadasc5f90922016-05-23 15:20:53 +03005110 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5111 if (udata->outlen && udata->outlen < min_resp_len)
5112 return ERR_PTR(-EINVAL);
5113
5114 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
5115 if (!rwq_ind_tbl)
5116 return ERR_PTR(-ENOMEM);
5117
5118 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005119 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005120 if (!in) {
5121 err = -ENOMEM;
5122 goto err;
5123 }
5124
5125 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
5126
5127 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
5128 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
5129
5130 for (i = 0; i < sz; i++)
5131 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
5132
Yishai Hadas5deba862018-09-20 21:39:28 +03005133 rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
5134 MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);
5135
Yishai Hadasc5f90922016-05-23 15:20:53 +03005136 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
5137 kvfree(in);
5138
5139 if (err)
5140 goto err;
5141
5142 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
5143 if (udata->outlen) {
5144 resp.response_length = offsetof(typeof(resp), response_length) +
5145 sizeof(resp.response_length);
5146 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5147 if (err)
5148 goto err_copy;
5149 }
5150
5151 return &rwq_ind_tbl->ib_rwq_ind_tbl;
5152
5153err_copy:
Yishai Hadas5deba862018-09-20 21:39:28 +03005154 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005155err:
5156 kfree(rwq_ind_tbl);
5157 return ERR_PTR(err);
5158}
5159
5160int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
5161{
5162 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
5163 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
5164
Yishai Hadas5deba862018-09-20 21:39:28 +03005165 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005166
5167 kfree(rwq_ind_tbl);
5168 return 0;
5169}
5170
Yishai Hadas79b20a62016-05-23 15:20:50 +03005171int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
5172 u32 wq_attr_mask, struct ib_udata *udata)
5173{
5174 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5175 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5176 struct mlx5_ib_modify_wq ucmd = {};
5177 size_t required_cmd_sz;
5178 int curr_wq_state;
5179 int wq_state;
5180 int inlen;
5181 int err;
5182 void *rqc;
5183 void *in;
5184
5185 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
5186 if (udata->inlen < required_cmd_sz)
5187 return -EINVAL;
5188
5189 if (udata->inlen > sizeof(ucmd) &&
5190 !ib_is_udata_cleared(udata, sizeof(ucmd),
5191 udata->inlen - sizeof(ucmd)))
5192 return -EOPNOTSUPP;
5193
5194 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
5195 return -EFAULT;
5196
5197 if (ucmd.comp_mask || ucmd.reserved)
5198 return -EOPNOTSUPP;
5199
5200 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005201 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005202 if (!in)
5203 return -ENOMEM;
5204
5205 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
5206
5207 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
5208 wq_attr->curr_wq_state : wq->state;
5209 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
5210 wq_attr->wq_state : curr_wq_state;
5211 if (curr_wq_state == IB_WQS_ERR)
5212 curr_wq_state = MLX5_RQC_STATE_ERR;
5213 if (wq_state == IB_WQS_ERR)
5214 wq_state = MLX5_RQC_STATE_ERR;
5215 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
Yishai Hadas34d57582018-09-20 21:39:21 +03005216 MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005217 MLX5_SET(rqc, rqc, state, wq_state);
5218
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005219 if (wq_attr_mask & IB_WQ_FLAGS) {
5220 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
5221 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
5222 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
5223 mlx5_ib_dbg(dev, "VLAN offloads are not "
5224 "supported\n");
5225 err = -EOPNOTSUPP;
5226 goto out;
5227 }
5228 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5229 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
5230 MLX5_SET(rqc, rqc, vsd,
5231 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
5232 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005233
5234 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5235 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
5236 err = -EOPNOTSUPP;
5237 goto out;
5238 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005239 }
5240
Majd Dibbiny23a69642017-01-18 15:25:10 +02005241 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03005242 u16 set_id;
5243
5244 set_id = mlx5_ib_get_counters_id(dev, 0);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005245 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
5246 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5247 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandit3e1f0002019-07-23 10:31:17 +03005248 MLX5_SET(rqc, rqc, counter_set_id, set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005249 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06005250 dev_info_once(
5251 &dev->ib_dev.dev,
5252 "Receive WQ counters are not supported on current FW\n");
Majd Dibbiny23a69642017-01-18 15:25:10 +02005253 }
5254
Leon Romanovskye0b4b472020-04-09 21:03:33 +03005255 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005256 if (!err)
5257 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
5258
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005259out:
5260 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005261 return err;
5262}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005263
5264struct mlx5_ib_drain_cqe {
5265 struct ib_cqe cqe;
5266 struct completion done;
5267};
5268
5269static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
5270{
5271 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
5272 struct mlx5_ib_drain_cqe,
5273 cqe);
5274
5275 complete(&cqe->done);
5276}
5277
5278/* This function returns only once the drained WR was completed */
5279static void handle_drain_completion(struct ib_cq *cq,
5280 struct mlx5_ib_drain_cqe *sdrain,
5281 struct mlx5_ib_dev *dev)
5282{
5283 struct mlx5_core_dev *mdev = dev->mdev;
5284
5285 if (cq->poll_ctx == IB_POLL_DIRECT) {
5286 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
5287 ib_process_cq_direct(cq, -1);
5288 return;
5289 }
5290
5291 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5292 struct mlx5_ib_cq *mcq = to_mcq(cq);
5293 bool triggered = false;
5294 unsigned long flags;
5295
5296 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
5297 /* Make sure that the CQ handler won't run if wasn't run yet */
5298 if (!mcq->mcq.reset_notify_added)
5299 mcq->mcq.reset_notify_added = 1;
5300 else
5301 triggered = true;
5302 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
5303
5304 if (triggered) {
5305 /* Wait for any scheduled/running task to be ended */
5306 switch (cq->poll_ctx) {
5307 case IB_POLL_SOFTIRQ:
5308 irq_poll_disable(&cq->iop);
5309 irq_poll_enable(&cq->iop);
5310 break;
5311 case IB_POLL_WORKQUEUE:
5312 cancel_work_sync(&cq->work);
5313 break;
5314 default:
5315 WARN_ON_ONCE(1);
5316 }
5317 }
5318
5319 /* Run the CQ handler - this makes sure that the drain WR will
5320 * be processed if wasn't processed yet.
5321 */
Yishai Hadas4e0e2ea2019-06-30 19:23:27 +03005322 mcq->mcq.comp(&mcq->mcq, NULL);
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005323 }
5324
5325 wait_for_completion(&sdrain->done);
5326}
5327
5328void mlx5_ib_drain_sq(struct ib_qp *qp)
5329{
5330 struct ib_cq *cq = qp->send_cq;
5331 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
5332 struct mlx5_ib_drain_cqe sdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005333 const struct ib_send_wr *bad_swr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005334 struct ib_rdma_wr swr = {
5335 .wr = {
5336 .next = NULL,
5337 { .wr_cqe = &sdrain.cqe, },
5338 .opcode = IB_WR_RDMA_WRITE,
5339 },
5340 };
5341 int ret;
5342 struct mlx5_ib_dev *dev = to_mdev(qp->device);
5343 struct mlx5_core_dev *mdev = dev->mdev;
5344
5345 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
5346 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5347 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
5348 return;
5349 }
5350
5351 sdrain.cqe.done = mlx5_ib_drain_qp_done;
5352 init_completion(&sdrain.done);
5353
Leon Romanovsky029e88f2020-05-06 09:55:13 +03005354 ret = mlx5_ib_post_send_drain(qp, &swr.wr, &bad_swr);
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005355 if (ret) {
5356 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
5357 return;
5358 }
5359
5360 handle_drain_completion(cq, &sdrain, dev);
5361}
5362
5363void mlx5_ib_drain_rq(struct ib_qp *qp)
5364{
5365 struct ib_cq *cq = qp->recv_cq;
5366 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
5367 struct mlx5_ib_drain_cqe rdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005368 struct ib_recv_wr rwr = {};
5369 const struct ib_recv_wr *bad_rwr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005370 int ret;
5371 struct mlx5_ib_dev *dev = to_mdev(qp->device);
5372 struct mlx5_core_dev *mdev = dev->mdev;
5373
5374 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
5375 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5376 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
5377 return;
5378 }
5379
5380 rwr.wr_cqe = &rdrain.cqe;
5381 rdrain.cqe.done = mlx5_ib_drain_qp_done;
5382 init_completion(&rdrain.done);
5383
Leon Romanovsky029e88f2020-05-06 09:55:13 +03005384 ret = mlx5_ib_post_recv_drain(qp, &rwr, &bad_rwr);
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005385 if (ret) {
5386 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
5387 return;
5388 }
5389
5390 handle_drain_completion(cq, &rdrain, dev);
5391}
Mark Zhangd14133d2019-07-02 13:02:36 +03005392
5393/**
5394 * Bind a qp to a counter. If @counter is NULL then bind the qp to
5395 * the default counter
5396 */
5397int mlx5_ib_qp_set_counter(struct ib_qp *qp, struct rdma_counter *counter)
5398{
Mark Zhang10189e82020-01-26 19:17:08 +02005399 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Mark Zhangd14133d2019-07-02 13:02:36 +03005400 struct mlx5_ib_qp *mqp = to_mqp(qp);
5401 int err = 0;
5402
5403 mutex_lock(&mqp->mutex);
5404 if (mqp->state == IB_QPS_RESET) {
5405 qp->counter = counter;
5406 goto out;
5407 }
5408
Mark Zhang10189e82020-01-26 19:17:08 +02005409 if (!MLX5_CAP_GEN(dev->mdev, rts2rts_qp_counters_set_id)) {
5410 err = -EOPNOTSUPP;
5411 goto out;
5412 }
5413
Mark Zhangd14133d2019-07-02 13:02:36 +03005414 if (mqp->state == IB_QPS_RTS) {
5415 err = __mlx5_ib_qp_set_counter(qp, counter);
5416 if (!err)
5417 qp->counter = counter;
5418
5419 goto out;
5420 }
5421
5422 mqp->counter_pending = 1;
5423 qp->counter = counter;
5424
5425out:
5426 mutex_unlock(&mqp->mutex);
5427 return err;
5428}