blob: c6a4db9f6a0bbf15a50a053cd0424e54a8122e34 [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);
Leon Romanovskyf4375442020-07-06 15:27:14 +03002038 MLX5_SET(qpc, qpc, xrcd, devr->xrcdn1);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002039 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) {
Leon Romanovskyf4375442020-07-06 15:27:14 +03002043 MLX5_SET(qpc, qpc, xrcd, devr->xrcdn0);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002044 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002045 } else {
Leon Romanovskyf4375442020-07-06 15:27:14 +03002046 MLX5_SET(qpc, qpc, xrcd, devr->xrcdn1);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002047 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) {
Leon Romanovskyf4375442020-07-06 15:27:14 +03002186 MLX5_SET(qpc, qpc, xrcd, devr->xrcdn0);
Leon Romanovsky98fc1122020-04-27 18:46:28 +03002187 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn,
2188 to_msrq(attr->srq)->msrq.srqn);
2189 } else {
Leon Romanovskyf4375442020-07-06 15:27:14 +03002190 MLX5_SET(qpc, qpc, xrcd, devr->xrcdn1);
Leon Romanovsky98fc1122020-04-27 18:46:28 +03002191 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
Yishai Hadas28d61372016-05-23 15:20:56 +03002344 if (qp->ibqp.rwq_ind_tbl) {
2345 destroy_rss_raw_qp_tir(dev, qp);
2346 return;
2347 }
2348
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002349 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03002350 qp->flags & IB_QP_CREATE_SOURCE_QPN) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002351 &qp->raw_packet_qp.rq.base :
2352 &qp->trans_qp.base;
2353
Haggai Eran6aec21f2014-12-11 17:04:23 +02002354 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002355 if (qp->ibqp.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
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002371 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2372 &send_cq, &recv_cq);
2373
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
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002394 if (qp->ibqp.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,
2672 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK,
2673 MLX5_CAP_GEN(mdev, block_lb_mc), qp);
2674 process_create_flag(dev, &create_flags, IB_QP_CREATE_CROSS_CHANNEL,
2675 MLX5_CAP_GEN(mdev, cd), qp);
2676 process_create_flag(dev, &create_flags, IB_QP_CREATE_MANAGED_SEND,
2677 MLX5_CAP_GEN(mdev, cd), qp);
2678 process_create_flag(dev, &create_flags, IB_QP_CREATE_MANAGED_RECV,
2679 MLX5_CAP_GEN(mdev, cd), qp);
2680
2681 if (qp_type == IB_QPT_UD) {
2682 process_create_flag(dev, &create_flags,
2683 IB_QP_CREATE_IPOIB_UD_LSO,
2684 MLX5_CAP_GEN(mdev, ipoib_basic_offloads),
2685 qp);
2686 cond = MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_IB;
2687 process_create_flag(dev, &create_flags, IB_QP_CREATE_SOURCE_QPN,
2688 cond, qp);
2689 }
2690
2691 if (qp_type == IB_QPT_RAW_PACKET) {
2692 cond = MLX5_CAP_GEN(mdev, eth_net_offloads) &&
2693 MLX5_CAP_ETH(mdev, scatter_fcs);
2694 process_create_flag(dev, &create_flags,
2695 IB_QP_CREATE_SCATTER_FCS, cond, qp);
2696
2697 cond = MLX5_CAP_GEN(mdev, eth_net_offloads) &&
2698 MLX5_CAP_ETH(mdev, vlan_cap);
2699 process_create_flag(dev, &create_flags,
2700 IB_QP_CREATE_CVLAN_STRIPPING, cond, qp);
2701 }
2702
2703 process_create_flag(dev, &create_flags,
2704 IB_QP_CREATE_PCI_WRITE_END_PADDING,
2705 MLX5_CAP_GEN(mdev, end_pad), qp);
2706
2707 process_create_flag(dev, &create_flags, MLX5_IB_QP_CREATE_WC_TEST,
2708 qp_type != MLX5_IB_QPT_REG_UMR, qp);
2709 process_create_flag(dev, &create_flags, MLX5_IB_QP_CREATE_SQPN_QP1,
2710 true, qp);
2711
2712 if (create_flags)
2713 mlx5_ib_dbg(dev, "Create QP has unsupported flags 0x%X\n",
2714 create_flags);
2715
2716 return (create_flags) ? -EINVAL : 0;
2717}
2718
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002719static int process_udata_size(struct mlx5_ib_dev *dev,
2720 struct mlx5_create_qp_params *params)
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002721{
2722 size_t ucmd = sizeof(struct mlx5_ib_create_qp);
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002723 struct ib_udata *udata = params->udata;
2724 size_t outlen = udata->outlen;
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002725 size_t inlen = udata->inlen;
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002726
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002727 params->outlen = min(outlen, sizeof(struct mlx5_ib_create_qp_resp));
Leon Romanovskye3830852020-05-26 14:54:35 +03002728 params->ucmd_size = ucmd;
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002729 if (!params->is_rss_raw) {
Leon Romanovskye3830852020-05-26 14:54:35 +03002730 /* User has old rdma-core, which doesn't support ECE */
2731 size_t min_inlen =
2732 offsetof(struct mlx5_ib_create_qp, ece_options);
2733
2734 /*
2735 * We will check in check_ucmd_data() that user
2736 * cleared everything after inlen.
2737 */
2738 params->inlen = (inlen < min_inlen) ? 0 : min(inlen, ucmd);
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002739 goto out;
2740 }
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002741
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002742 /* RSS RAW QP */
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002743 if (inlen < offsetofend(struct mlx5_ib_create_qp_rss, flags))
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002744 return -EINVAL;
2745
2746 if (outlen < offsetofend(struct mlx5_ib_create_qp_resp, bfreg_index))
2747 return -EINVAL;
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002748
2749 ucmd = sizeof(struct mlx5_ib_create_qp_rss);
Leon Romanovskye3830852020-05-26 14:54:35 +03002750 params->ucmd_size = ucmd;
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002751 if (inlen > ucmd && !ib_is_udata_cleared(udata, ucmd, inlen - ucmd))
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002752 return -EINVAL;
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002753
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002754 params->inlen = min(ucmd, inlen);
2755out:
2756 if (!params->inlen)
Leon Romanovskye3830852020-05-26 14:54:35 +03002757 mlx5_ib_dbg(dev, "udata is too small\n");
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002758
2759 return (params->inlen) ? 0 : -EINVAL;
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002760}
2761
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002762static int create_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
2763 struct mlx5_ib_qp *qp,
2764 struct mlx5_create_qp_params *params)
Leon Romanovsky5d0dc3d2020-04-27 18:46:12 +03002765{
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002766 int err;
Leon Romanovsky5d0dc3d2020-04-27 18:46:12 +03002767
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002768 if (params->is_rss_raw) {
2769 err = create_rss_raw_qp_tir(dev, pd, qp, params);
2770 goto out;
2771 }
2772
2773 if (qp->type == MLX5_IB_QPT_DCT) {
Leon Romanovskya645a892020-06-02 15:55:48 +03002774 err = create_dct(dev, pd, qp, params);
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002775 goto out;
2776 }
2777
2778 if (qp->type == IB_QPT_XRC_TGT) {
2779 err = create_xrc_tgt_qp(dev, qp, params);
2780 goto out;
2781 }
2782
2783 if (params->udata)
2784 err = create_user_qp(dev, pd, qp, params);
2785 else
2786 err = create_kernel_qp(dev, pd, qp, params);
2787
2788out:
2789 if (err) {
2790 mlx5_ib_err(dev, "Create QP type %d failed\n", qp->type);
2791 return err;
2792 }
2793
2794 if (is_qp0(qp->type))
2795 qp->ibqp.qp_num = 0;
2796 else if (is_qp1(qp->type))
2797 qp->ibqp.qp_num = 1;
2798 else
2799 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
2800
2801 mlx5_ib_dbg(dev,
Leon Romanovsky3e09a422020-05-26 14:54:34 +03002802 "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 +03002803 qp->type, qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
2804 params->attr->recv_cq ? to_mcq(params->attr->recv_cq)->mcq.cqn :
2805 -1,
2806 params->attr->send_cq ? to_mcq(params->attr->send_cq)->mcq.cqn :
Leon Romanovsky3e09a422020-05-26 14:54:34 +03002807 -1,
2808 params->resp.ece_options);
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002809
2810 return 0;
Leon Romanovsky5d0dc3d2020-04-27 18:46:12 +03002811}
2812
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002813static int check_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2814 struct ib_qp_init_attr *attr)
2815{
2816 int ret = 0;
2817
2818 switch (qp->type) {
2819 case MLX5_IB_QPT_DCT:
2820 ret = (!attr->srq || !attr->recv_cq) ? -EINVAL : 0;
2821 break;
2822 case MLX5_IB_QPT_DCI:
2823 ret = (attr->cap.max_recv_wr || attr->cap.max_recv_sge) ?
2824 -EINVAL :
2825 0;
2826 break;
Leon Romanovsky266424e2020-04-27 18:46:21 +03002827 case IB_QPT_RAW_PACKET:
2828 ret = (attr->rwq_ind_tbl && attr->send_cq) ? -EINVAL : 0;
2829 break;
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002830 default:
2831 break;
2832 }
2833
2834 if (ret)
2835 mlx5_ib_dbg(dev, "QP type %d has wrong attributes\n", qp->type);
2836
2837 return ret;
2838}
2839
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002840static int get_qp_uidx(struct mlx5_ib_qp *qp,
2841 struct mlx5_create_qp_params *params)
Leon Romanovsky21aad802020-04-27 18:46:26 +03002842{
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002843 struct mlx5_ib_create_qp *ucmd = params->ucmd;
2844 struct ib_udata *udata = params->udata;
Leon Romanovsky21aad802020-04-27 18:46:26 +03002845 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2846 udata, struct mlx5_ib_ucontext, ibucontext);
2847
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002848 if (params->is_rss_raw)
Leon Romanovsky21aad802020-04-27 18:46:26 +03002849 return 0;
2850
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002851 return get_qp_user_index(ucontext, ucmd, sizeof(*ucmd), &params->uidx);
Leon Romanovsky21aad802020-04-27 18:46:26 +03002852}
2853
Leon Romanovsky08d539762020-04-27 18:46:33 +03002854static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2855{
2856 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2857
2858 if (mqp->state == IB_QPS_RTR) {
2859 int err;
2860
2861 err = mlx5_core_destroy_dct(dev, &mqp->dct.mdct);
2862 if (err) {
2863 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2864 return err;
2865 }
2866 }
2867
2868 kfree(mqp->dct.in);
2869 kfree(mqp);
2870 return 0;
2871}
2872
Leon Romanovskye3830852020-05-26 14:54:35 +03002873static int check_ucmd_data(struct mlx5_ib_dev *dev,
2874 struct mlx5_create_qp_params *params)
2875{
2876 struct ib_qp_init_attr *attr = params->attr;
2877 struct ib_udata *udata = params->udata;
2878 size_t size, last;
2879 int ret;
2880
2881 if (params->is_rss_raw)
2882 /*
2883 * These QPs don't have "reserved" field in their
2884 * create_qp input struct, so their data is always valid.
2885 */
2886 last = sizeof(struct mlx5_ib_create_qp_rss);
2887 else
Leon Romanovskya645a892020-06-02 15:55:48 +03002888 /* IB_QPT_RAW_PACKET doesn't have ECE data */
Leon Romanovskye3830852020-05-26 14:54:35 +03002889 switch (attr->qp_type) {
Leon Romanovskye3830852020-05-26 14:54:35 +03002890 case IB_QPT_RAW_PACKET:
2891 last = offsetof(struct mlx5_ib_create_qp, ece_options);
2892 break;
2893 default:
2894 last = offsetof(struct mlx5_ib_create_qp, reserved);
2895 }
2896
2897 if (udata->inlen <= last)
2898 return 0;
2899
2900 /*
2901 * User provides different create_qp structures based on the
2902 * flow and we need to know if he cleared memory after our
2903 * struct create_qp ends.
2904 */
2905 size = udata->inlen - last;
2906 ret = ib_is_udata_cleared(params->udata, last, size);
2907 if (!ret)
2908 mlx5_ib_dbg(
2909 dev,
2910 "udata is not cleared, inlen = %lu, ucmd = %lu, last = %lu, size = %lu\n",
2911 udata->inlen, params->ucmd_size, last, size);
2912 return ret ? 0 : -EINVAL;
2913}
2914
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002915struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002916 struct ib_udata *udata)
2917{
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002918 struct mlx5_create_qp_params params = {};
Eli Cohene126ba92013-07-07 17:25:49 +03002919 struct mlx5_ib_dev *dev;
2920 struct mlx5_ib_qp *qp;
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002921 enum ib_qp_type type;
Eli Cohene126ba92013-07-07 17:25:49 +03002922 int err;
2923
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002924 dev = pd ? to_mdev(pd->device) :
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002925 to_mdev(to_mxrcd(attr->xrcd)->ibxrcd.device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002926
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002927 err = check_qp_type(dev, attr, &type);
Leon Romanovsky2242cc22020-04-27 18:46:03 +03002928 if (err)
2929 return ERR_PTR(err);
Eli Cohene126ba92013-07-07 17:25:49 +03002930
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002931 err = check_valid_flow(dev, pd, attr, udata);
2932 if (err)
2933 return ERR_PTR(err);
2934
2935 if (attr->qp_type == IB_QPT_GSI)
2936 return mlx5_ib_gsi_create_qp(pd, attr);
2937
2938 params.udata = udata;
2939 params.uidx = MLX5_IB_DEFAULT_UIDX;
2940 params.attr = attr;
2941 params.is_rss_raw = !!attr->rwq_ind_tbl;
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002942
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002943 if (udata) {
Leon Romanovsky6f2cf762020-04-27 18:46:32 +03002944 err = process_udata_size(dev, &params);
2945 if (err)
2946 return ERR_PTR(err);
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002947
Leon Romanovskye3830852020-05-26 14:54:35 +03002948 err = check_ucmd_data(dev, &params);
2949 if (err)
2950 return ERR_PTR(err);
2951
2952 params.ucmd = kzalloc(params.ucmd_size, GFP_KERNEL);
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002953 if (!params.ucmd)
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002954 return ERR_PTR(-ENOMEM);
2955
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002956 err = ib_copy_from_udata(params.ucmd, udata, params.inlen);
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002957 if (err)
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002958 goto free_ucmd;
Leon Romanovsky2fdddbd2020-04-27 18:46:10 +03002959 }
2960
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002961 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002962 if (!qp) {
2963 err = -ENOMEM;
2964 goto free_ucmd;
2965 }
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002966
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002967 qp->type = type;
Leon Romanovsky37518fa2020-04-27 18:46:18 +03002968 if (udata) {
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002969 err = process_vendor_flags(dev, qp, params.ucmd, attr);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002970 if (err)
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002971 goto free_qp;
Leon Romanovsky21aad802020-04-27 18:46:26 +03002972
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002973 err = get_qp_uidx(qp, &params);
Leon Romanovsky21aad802020-04-27 18:46:26 +03002974 if (err)
2975 goto free_qp;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002976 }
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002977 err = process_create_flags(dev, qp, attr);
Leon Romanovsky29789752020-04-27 18:46:14 +03002978 if (err)
2979 goto free_qp;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002980
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002981 err = check_qp_attr(dev, qp, attr);
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03002982 if (err)
2983 goto free_qp;
2984
Leon Romanovsky968f0b62020-04-27 18:46:35 +03002985 err = create_qp(dev, pd, qp, &params);
2986 if (err)
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03002987 goto free_qp;
Leon Romanovsky6eb7edf2020-04-27 18:46:01 +03002988
Leon Romanovskyf78d3582020-04-27 18:46:30 +03002989 kfree(params.ucmd);
Leon Romanovsky08d539762020-04-27 18:46:33 +03002990 params.ucmd = NULL;
Leon Romanovsky5ce05922020-04-27 18:46:22 +03002991
Leon Romanovsky08d539762020-04-27 18:46:33 +03002992 if (udata)
2993 /*
2994 * It is safe to copy response for all user create QP flows,
2995 * including MLX5_IB_QPT_DCT, which doesn't need it.
2996 * In that case, resp will be filled with zeros.
2997 */
2998 err = ib_copy_to_udata(udata, &params.resp, params.outlen);
2999 if (err)
3000 goto destroy_qp;
3001
Eli Cohene126ba92013-07-07 17:25:49 +03003002 return &qp->ibqp;
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03003003
Leon Romanovsky08d539762020-04-27 18:46:33 +03003004destroy_qp:
3005 if (qp->type == MLX5_IB_QPT_DCT)
3006 mlx5_ib_destroy_dct(qp);
3007 else
3008 destroy_qp_common(dev, qp, udata);
3009 qp = NULL;
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03003010free_qp:
3011 kfree(qp);
Leon Romanovsky5ce05922020-04-27 18:46:22 +03003012free_ucmd:
Leon Romanovskyf78d3582020-04-27 18:46:30 +03003013 kfree(params.ucmd);
Leon Romanovsky9c2ba4e2020-04-27 18:46:04 +03003014 return ERR_PTR(err);
Eli Cohene126ba92013-07-07 17:25:49 +03003015}
3016
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03003017int mlx5_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003018{
3019 struct mlx5_ib_dev *dev = to_mdev(qp->device);
3020 struct mlx5_ib_qp *mqp = to_mqp(qp);
3021
Haggai Erand16e91d2016-02-29 15:45:05 +02003022 if (unlikely(qp->qp_type == IB_QPT_GSI))
3023 return mlx5_ib_gsi_destroy_qp(qp);
3024
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03003025 if (mqp->type == MLX5_IB_QPT_DCT)
Moni Shoua776a3902018-01-02 16:19:33 +02003026 return mlx5_ib_destroy_dct(mqp);
3027
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03003028 destroy_qp_common(dev, mqp, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03003029
3030 kfree(mqp);
3031
3032 return 0;
3033}
3034
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003035static int set_qpc_atomic_flags(struct mlx5_ib_qp *qp,
3036 const struct ib_qp_attr *attr, int attr_mask,
3037 void *qpc)
Eli Cohene126ba92013-07-07 17:25:49 +03003038{
Yonatan Cohena60109d2018-10-10 09:25:16 +03003039 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003040 u8 dest_rd_atomic;
3041 u32 access_flags;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003042
Eli Cohene126ba92013-07-07 17:25:49 +03003043 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
3044 dest_rd_atomic = attr->max_dest_rd_atomic;
3045 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02003046 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03003047
3048 if (attr_mask & IB_QP_ACCESS_FLAGS)
3049 access_flags = attr->qp_access_flags;
3050 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02003051 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03003052
3053 if (!dest_rd_atomic)
3054 access_flags &= IB_ACCESS_REMOTE_WRITE;
3055
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003056 MLX5_SET(qpc, qpc, rre, !!(access_flags & IB_ACCESS_REMOTE_READ));
3057
Yonatan Cohen13f8d9c2018-11-21 13:48:39 +02003058 if (access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03003059 int atomic_mode;
Eli Cohene126ba92013-07-07 17:25:49 +03003060
Yonatan Cohena60109d2018-10-10 09:25:16 +03003061 atomic_mode = get_atomic_mode(dev, qp->ibqp.qp_type);
3062 if (atomic_mode < 0)
3063 return -EOPNOTSUPP;
3064
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003065 MLX5_SET(qpc, qpc, rae, 1);
3066 MLX5_SET(qpc, qpc, atomic_mode, atomic_mode);
Yonatan Cohena60109d2018-10-10 09:25:16 +03003067 }
3068
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003069 MLX5_SET(qpc, qpc, rwe, !!(access_flags & IB_ACCESS_REMOTE_WRITE));
Yonatan Cohena60109d2018-10-10 09:25:16 +03003070 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003071}
3072
3073enum {
3074 MLX5_PATH_FLAG_FL = 1 << 0,
3075 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
3076 MLX5_PATH_FLAG_COUNTER = 1 << 2,
3077};
3078
3079static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
3080{
Danit Goldberg4f32ac22018-04-23 17:01:54 +03003081 if (rate == IB_RATE_PORT_CURRENT)
Eli Cohene126ba92013-07-07 17:25:49 +03003082 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003083
Michael Guralnika5a5d192018-12-09 11:49:50 +02003084 if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_600_GBPS)
Danit Goldberg4f32ac22018-04-23 17:01:54 +03003085 return -EINVAL;
3086
3087 while (rate != IB_RATE_PORT_CURRENT &&
3088 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
3089 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
3090 --rate;
3091
3092 return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
Eli Cohene126ba92013-07-07 17:25:49 +03003093}
3094
majd@mellanox.com75850d02016-01-14 19:13:06 +02003095static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003096 struct mlx5_ib_sq *sq, u8 sl,
3097 struct ib_pd *pd)
majd@mellanox.com75850d02016-01-14 19:13:06 +02003098{
3099 void *in;
3100 void *tisc;
3101 int inlen;
3102 int err;
3103
3104 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003105 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02003106 if (!in)
3107 return -ENOMEM;
3108
3109 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003110 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com75850d02016-01-14 19:13:06 +02003111
3112 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
3113 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
3114
Leon Romanovskye0b4b472020-04-09 21:03:33 +03003115 err = mlx5_core_modify_tis(dev, sq->tisn, in);
majd@mellanox.com75850d02016-01-14 19:13:06 +02003116
3117 kvfree(in);
3118
3119 return err;
3120}
3121
Aviv Heller13eab212016-09-18 20:48:04 +03003122static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003123 struct mlx5_ib_sq *sq, u8 tx_affinity,
3124 struct ib_pd *pd)
Aviv Heller13eab212016-09-18 20:48:04 +03003125{
3126 void *in;
3127 void *tisc;
3128 int inlen;
3129 int err;
3130
3131 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003132 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03003133 if (!in)
3134 return -ENOMEM;
3135
3136 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003137 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
Aviv Heller13eab212016-09-18 20:48:04 +03003138
3139 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
3140 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
3141
Leon Romanovskye0b4b472020-04-09 21:03:33 +03003142 err = mlx5_core_modify_tis(dev, sq->tisn, in);
Aviv Heller13eab212016-09-18 20:48:04 +03003143
3144 kvfree(in);
3145
3146 return err;
3147}
3148
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003149static void mlx5_set_path_udp_sport(void *path, const struct rdma_ah_attr *ah,
Mark Zhang2b880b2e2020-05-04 08:19:33 +03003150 u32 lqpn, u32 rqpn)
3151
3152{
3153 u32 fl = ah->grh.flow_label;
Mark Zhang2b880b2e2020-05-04 08:19:33 +03003154
3155 if (!fl)
3156 fl = rdma_calc_flow_label(lqpn, rqpn);
3157
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003158 MLX5_SET(ads, path, udp_sport, rdma_flow_label_to_udp_sport(fl));
Mark Zhang2b880b2e2020-05-04 08:19:33 +03003159}
3160
majd@mellanox.com75850d02016-01-14 19:13:06 +02003161static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003162 const struct rdma_ah_attr *ah, void *path, u8 port,
3163 int attr_mask, u32 path_flags,
3164 const struct ib_qp_attr *attr, bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03003165{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003166 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03003167 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02003168 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003169 u8 ah_flags = rdma_ah_get_ah_flags(ah);
3170 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03003171
Eli Cohene126ba92013-07-07 17:25:49 +03003172 if (attr_mask & IB_QP_PKEY_INDEX)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003173 MLX5_SET(ads, path, pkey_index,
3174 alt ? attr->alt_pkey_index : attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003175
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003176 if (ah_flags & IB_AH_GRH) {
3177 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03003178 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07003179 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003180 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03003181 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03003182 return -EINVAL;
3183 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02003184 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04003185
3186 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003187 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02003188 return -EINVAL;
Parav Pandit47ec3862018-06-13 10:22:06 +03003189
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003190 ether_addr_copy(MLX5_ADDR_OF(ads, path, rmac_47_32),
3191 ah->roce.dmac);
Mark Zhang2b880b2e2020-05-04 08:19:33 +03003192 if ((qp->ibqp.qp_type == IB_QPT_RC ||
3193 qp->ibqp.qp_type == IB_QPT_UC ||
3194 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
3195 qp->ibqp.qp_type == IB_QPT_XRC_TGT) &&
3196 (grh->sgid_attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) &&
3197 (attr_mask & IB_QP_DEST_QPN))
3198 mlx5_set_path_udp_sport(path, ah,
3199 qp->ibqp.qp_num,
3200 attr->dest_qp_num);
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003201 MLX5_SET(ads, path, eth_prio, sl & 0x7);
Parav Pandit47ec3862018-06-13 10:22:06 +03003202 gid_type = ah->grh.sgid_attr->gid_type;
Majd Dibbinyed884512017-01-18 14:10:35 +02003203 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003204 MLX5_SET(ads, path, dscp, grh->traffic_class >> 2);
Achiad Shochat2811ba52015-12-23 18:47:24 +02003205 } else {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003206 MLX5_SET(ads, path, fl, !!(path_flags & MLX5_PATH_FLAG_FL));
3207 MLX5_SET(ads, path, free_ar,
3208 !!(path_flags & MLX5_PATH_FLAG_FREE_AR));
3209 MLX5_SET(ads, path, rlid, rdma_ah_get_dlid(ah));
3210 MLX5_SET(ads, path, mlid, rdma_ah_get_path_bits(ah));
3211 MLX5_SET(ads, path, grh, !!(ah_flags & IB_AH_GRH));
3212 MLX5_SET(ads, path, sl, sl);
Achiad Shochat2811ba52015-12-23 18:47:24 +02003213 }
3214
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003215 if (ah_flags & IB_AH_GRH) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003216 MLX5_SET(ads, path, src_addr_index, grh->sgid_index);
3217 MLX5_SET(ads, path, hop_limit, grh->hop_limit);
3218 MLX5_SET(ads, path, tclass, grh->traffic_class);
3219 MLX5_SET(ads, path, flow_label, grh->flow_label);
3220 memcpy(MLX5_ADDR_OF(ads, path, rgid_rip), grh->dgid.raw,
3221 sizeof(grh->dgid.raw));
Eli Cohene126ba92013-07-07 17:25:49 +03003222 }
3223
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003224 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03003225 if (err < 0)
3226 return err;
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003227 MLX5_SET(ads, path, stat_rate, err);
3228 MLX5_SET(ads, path, vhca_port_num, port);
Eli Cohene126ba92013-07-07 17:25:49 +03003229
Eli Cohene126ba92013-07-07 17:25:49 +03003230 if (attr_mask & IB_QP_TIMEOUT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003231 MLX5_SET(ads, path, ack_timeout,
3232 alt ? attr->alt_timeout : attr->timeout);
Eli Cohene126ba92013-07-07 17:25:49 +03003233
majd@mellanox.com75850d02016-01-14 19:13:06 +02003234 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
3235 return modify_raw_packet_eth_prio(dev->mdev,
3236 &qp->raw_packet_qp.sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003237 sl & 0xf, qp->ibqp.pd);
majd@mellanox.com75850d02016-01-14 19:13:06 +02003238
Eli Cohene126ba92013-07-07 17:25:49 +03003239 return 0;
3240}
3241
3242static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
3243 [MLX5_QP_STATE_INIT] = {
3244 [MLX5_QP_STATE_INIT] = {
3245 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3246 MLX5_QP_OPTPAR_RAE |
3247 MLX5_QP_OPTPAR_RWE |
3248 MLX5_QP_OPTPAR_PKEY_INDEX |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003249 MLX5_QP_OPTPAR_PRI_PORT |
3250 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003251 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
3252 MLX5_QP_OPTPAR_PKEY_INDEX |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003253 MLX5_QP_OPTPAR_PRI_PORT |
3254 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003255 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3256 MLX5_QP_OPTPAR_Q_KEY |
3257 MLX5_QP_OPTPAR_PRI_PORT,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003258 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3259 MLX5_QP_OPTPAR_RAE |
3260 MLX5_QP_OPTPAR_RWE |
3261 MLX5_QP_OPTPAR_PKEY_INDEX |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003262 MLX5_QP_OPTPAR_PRI_PORT |
3263 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003264 },
3265 [MLX5_QP_STATE_RTR] = {
3266 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3267 MLX5_QP_OPTPAR_RRE |
3268 MLX5_QP_OPTPAR_RAE |
3269 MLX5_QP_OPTPAR_RWE |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003270 MLX5_QP_OPTPAR_PKEY_INDEX |
3271 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003272 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3273 MLX5_QP_OPTPAR_RWE |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003274 MLX5_QP_OPTPAR_PKEY_INDEX |
3275 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003276 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3277 MLX5_QP_OPTPAR_Q_KEY,
3278 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
3279 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03003280 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3281 MLX5_QP_OPTPAR_RRE |
3282 MLX5_QP_OPTPAR_RAE |
3283 MLX5_QP_OPTPAR_RWE |
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003284 MLX5_QP_OPTPAR_PKEY_INDEX |
3285 MLX5_QP_OPTPAR_LAG_TX_AFF,
Eli Cohene126ba92013-07-07 17:25:49 +03003286 },
3287 },
3288 [MLX5_QP_STATE_RTR] = {
3289 [MLX5_QP_STATE_RTS] = {
3290 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3291 MLX5_QP_OPTPAR_RRE |
3292 MLX5_QP_OPTPAR_RAE |
3293 MLX5_QP_OPTPAR_RWE |
3294 MLX5_QP_OPTPAR_PM_STATE |
3295 MLX5_QP_OPTPAR_RNR_TIMEOUT,
3296 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3297 MLX5_QP_OPTPAR_RWE |
3298 MLX5_QP_OPTPAR_PM_STATE,
3299 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003300 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3301 MLX5_QP_OPTPAR_RRE |
3302 MLX5_QP_OPTPAR_RAE |
3303 MLX5_QP_OPTPAR_RWE |
3304 MLX5_QP_OPTPAR_PM_STATE |
3305 MLX5_QP_OPTPAR_RNR_TIMEOUT,
Eli Cohene126ba92013-07-07 17:25:49 +03003306 },
3307 },
3308 [MLX5_QP_STATE_RTS] = {
3309 [MLX5_QP_STATE_RTS] = {
3310 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3311 MLX5_QP_OPTPAR_RAE |
3312 MLX5_QP_OPTPAR_RWE |
3313 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03003314 MLX5_QP_OPTPAR_PM_STATE |
3315 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003316 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
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_UD] = MLX5_QP_OPTPAR_Q_KEY |
3320 MLX5_QP_OPTPAR_SRQN |
3321 MLX5_QP_OPTPAR_CQN_RCV,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003322 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3323 MLX5_QP_OPTPAR_RAE |
3324 MLX5_QP_OPTPAR_RWE |
3325 MLX5_QP_OPTPAR_RNR_TIMEOUT |
3326 MLX5_QP_OPTPAR_PM_STATE |
3327 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003328 },
3329 },
3330 [MLX5_QP_STATE_SQER] = {
3331 [MLX5_QP_STATE_RTS] = {
3332 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
3333 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03003334 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03003335 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3336 MLX5_QP_OPTPAR_RWE |
3337 MLX5_QP_OPTPAR_RAE |
3338 MLX5_QP_OPTPAR_RRE,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003339 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3340 MLX5_QP_OPTPAR_RWE |
3341 MLX5_QP_OPTPAR_RAE |
3342 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03003343 },
3344 },
3345};
3346
3347static int ib_nr_to_mlx5_nr(int ib_mask)
3348{
3349 switch (ib_mask) {
3350 case IB_QP_STATE:
3351 return 0;
3352 case IB_QP_CUR_STATE:
3353 return 0;
3354 case IB_QP_EN_SQD_ASYNC_NOTIFY:
3355 return 0;
3356 case IB_QP_ACCESS_FLAGS:
3357 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
3358 MLX5_QP_OPTPAR_RAE;
3359 case IB_QP_PKEY_INDEX:
3360 return MLX5_QP_OPTPAR_PKEY_INDEX;
3361 case IB_QP_PORT:
3362 return MLX5_QP_OPTPAR_PRI_PORT;
3363 case IB_QP_QKEY:
3364 return MLX5_QP_OPTPAR_Q_KEY;
3365 case IB_QP_AV:
3366 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
3367 MLX5_QP_OPTPAR_PRI_PORT;
3368 case IB_QP_PATH_MTU:
3369 return 0;
3370 case IB_QP_TIMEOUT:
3371 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
3372 case IB_QP_RETRY_CNT:
3373 return MLX5_QP_OPTPAR_RETRY_COUNT;
3374 case IB_QP_RNR_RETRY:
3375 return MLX5_QP_OPTPAR_RNR_RETRY;
3376 case IB_QP_RQ_PSN:
3377 return 0;
3378 case IB_QP_MAX_QP_RD_ATOMIC:
3379 return MLX5_QP_OPTPAR_SRA_MAX;
3380 case IB_QP_ALT_PATH:
3381 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
3382 case IB_QP_MIN_RNR_TIMER:
3383 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
3384 case IB_QP_SQ_PSN:
3385 return 0;
3386 case IB_QP_MAX_DEST_RD_ATOMIC:
3387 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
3388 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
3389 case IB_QP_PATH_MIG_STATE:
3390 return MLX5_QP_OPTPAR_PM_STATE;
3391 case IB_QP_CAP:
3392 return 0;
3393 case IB_QP_DEST_QPN:
3394 return 0;
3395 }
3396 return 0;
3397}
3398
3399static int ib_mask_to_mlx5_opt(int ib_mask)
3400{
3401 int result = 0;
3402 int i;
3403
3404 for (i = 0; i < 8 * sizeof(int); i++) {
3405 if ((1 << i) & ib_mask)
3406 result |= ib_nr_to_mlx5_nr(1 << i);
3407 }
3408
3409 return result;
3410}
3411
Yishai Hadas34d57582018-09-20 21:39:21 +03003412static int modify_raw_packet_qp_rq(
3413 struct mlx5_ib_dev *dev, struct mlx5_ib_rq *rq, int new_state,
3414 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003415{
3416 void *in;
3417 void *rqc;
3418 int inlen;
3419 int err;
3420
3421 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003422 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003423 if (!in)
3424 return -ENOMEM;
3425
3426 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
Yishai Hadas34d57582018-09-20 21:39:21 +03003427 MLX5_SET(modify_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003428
3429 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
3430 MLX5_SET(rqc, rqc, state, new_state);
3431
Alex Veskereb49ab02016-08-28 12:25:53 +03003432 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
3433 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
3434 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02003435 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03003436 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
3437 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06003438 dev_info_once(
3439 &dev->ib_dev.dev,
3440 "RAW PACKET QP counters are not supported on current FW\n");
Alex Veskereb49ab02016-08-28 12:25:53 +03003441 }
3442
Leon Romanovskye0b4b472020-04-09 21:03:33 +03003443 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003444 if (err)
3445 goto out;
3446
3447 rq->state = new_state;
3448
3449out:
3450 kvfree(in);
3451 return err;
3452}
3453
Yishai Hadasc14003f2018-09-20 21:39:22 +03003454static int modify_raw_packet_qp_sq(
3455 struct mlx5_core_dev *dev, struct mlx5_ib_sq *sq, int new_state,
3456 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003457{
Bodong Wang7d29f342016-12-01 13:43:16 +02003458 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
Bodong Wang61147f32018-03-19 15:10:30 +02003459 struct mlx5_rate_limit old_rl = ibqp->rl;
3460 struct mlx5_rate_limit new_rl = old_rl;
3461 bool new_rate_added = false;
Bodong Wang7d29f342016-12-01 13:43:16 +02003462 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003463 void *in;
3464 void *sqc;
3465 int inlen;
3466 int err;
3467
3468 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003469 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003470 if (!in)
3471 return -ENOMEM;
3472
Yishai Hadasc14003f2018-09-20 21:39:22 +03003473 MLX5_SET(modify_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003474 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
3475
3476 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
3477 MLX5_SET(sqc, sqc, state, new_state);
3478
Bodong Wang7d29f342016-12-01 13:43:16 +02003479 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
3480 if (new_state != MLX5_SQC_STATE_RDY)
3481 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
3482 __func__);
3483 else
Bodong Wang61147f32018-03-19 15:10:30 +02003484 new_rl = raw_qp_param->rl;
Bodong Wang7d29f342016-12-01 13:43:16 +02003485 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003486
Bodong Wang61147f32018-03-19 15:10:30 +02003487 if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
3488 if (new_rl.rate) {
3489 err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003490 if (err) {
Bodong Wang61147f32018-03-19 15:10:30 +02003491 pr_err("Failed configuring rate limit(err %d): \
3492 rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
3493 err, new_rl.rate, new_rl.max_burst_sz,
3494 new_rl.typical_pkt_sz);
3495
Bodong Wang7d29f342016-12-01 13:43:16 +02003496 goto out;
3497 }
Bodong Wang61147f32018-03-19 15:10:30 +02003498 new_rate_added = true;
Bodong Wang7d29f342016-12-01 13:43:16 +02003499 }
3500
3501 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
Bodong Wang61147f32018-03-19 15:10:30 +02003502 /* index 0 means no limit */
Bodong Wang7d29f342016-12-01 13:43:16 +02003503 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
3504 }
3505
Leon Romanovskye0b4b472020-04-09 21:03:33 +03003506 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in);
Bodong Wang7d29f342016-12-01 13:43:16 +02003507 if (err) {
3508 /* Remove new rate from table if failed */
Bodong Wang61147f32018-03-19 15:10:30 +02003509 if (new_rate_added)
3510 mlx5_rl_remove_rate(dev, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003511 goto out;
3512 }
3513
3514 /* Only remove the old rate after new rate was set */
Rafi Wienerc8973df2019-10-02 15:02:43 +03003515 if ((old_rl.rate && !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
3516 (new_state != MLX5_SQC_STATE_RDY)) {
Bodong Wang61147f32018-03-19 15:10:30 +02003517 mlx5_rl_remove_rate(dev, &old_rl);
Rafi Wienerc8973df2019-10-02 15:02:43 +03003518 if (new_state != MLX5_SQC_STATE_RDY)
3519 memset(&new_rl, 0, sizeof(new_rl));
3520 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003521
Bodong Wang61147f32018-03-19 15:10:30 +02003522 ibqp->rl = new_rl;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003523 sq->state = new_state;
3524
3525out:
3526 kvfree(in);
3527 return err;
3528}
3529
3530static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03003531 const struct mlx5_modify_raw_qp_param *raw_qp_param,
3532 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003533{
3534 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
3535 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
3536 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02003537 int modify_rq = !!qp->rq.wqe_cnt;
3538 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003539 int rq_state;
3540 int sq_state;
3541 int err;
3542
Alex Vesker0680efa2016-08-28 12:25:52 +03003543 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003544 case MLX5_CMD_OP_RST2INIT_QP:
3545 rq_state = MLX5_RQC_STATE_RDY;
3546 sq_state = MLX5_SQC_STATE_RDY;
3547 break;
3548 case MLX5_CMD_OP_2ERR_QP:
3549 rq_state = MLX5_RQC_STATE_ERR;
3550 sq_state = MLX5_SQC_STATE_ERR;
3551 break;
3552 case MLX5_CMD_OP_2RST_QP:
3553 rq_state = MLX5_RQC_STATE_RST;
3554 sq_state = MLX5_SQC_STATE_RST;
3555 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003556 case MLX5_CMD_OP_RTR2RTS_QP:
3557 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02003558 if (raw_qp_param->set_mask ==
3559 MLX5_RAW_QP_RATE_LIMIT) {
3560 modify_rq = 0;
3561 sq_state = sq->state;
3562 } else {
3563 return raw_qp_param->set_mask ? -EINVAL : 0;
3564 }
3565 break;
3566 case MLX5_CMD_OP_INIT2INIT_QP:
3567 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03003568 if (raw_qp_param->set_mask)
3569 return -EINVAL;
3570 else
3571 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003572 default:
3573 WARN_ON(1);
3574 return -EINVAL;
3575 }
3576
Bodong Wang7d29f342016-12-01 13:43:16 +02003577 if (modify_rq) {
Yishai Hadas34d57582018-09-20 21:39:21 +03003578 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param,
3579 qp->ibqp.pd);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003580 if (err)
3581 return err;
3582 }
3583
Bodong Wang7d29f342016-12-01 13:43:16 +02003584 if (modify_sq) {
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003585 struct mlx5_flow_handle *flow_rule;
3586
Aviv Heller13eab212016-09-18 20:48:04 +03003587 if (tx_affinity) {
3588 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003589 tx_affinity,
3590 qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003591 if (err)
3592 return err;
3593 }
3594
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003595 flow_rule = create_flow_rule_vport_sq(dev, sq,
3596 raw_qp_param->port);
3597 if (IS_ERR(flow_rule))
Colin Ian King1db86312019-04-12 11:40:17 +01003598 return PTR_ERR(flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003599
3600 err = modify_raw_packet_qp_sq(dev->mdev, sq, sq_state,
3601 raw_qp_param, qp->ibqp.pd);
3602 if (err) {
3603 if (flow_rule)
3604 mlx5_del_flow_rules(flow_rule);
3605 return err;
3606 }
3607
3608 if (flow_rule) {
3609 destroy_flow_rule_vport_sq(sq);
3610 sq->flow_rule = flow_rule;
3611 }
3612
3613 return err;
Aviv Heller13eab212016-09-18 20:48:04 +03003614 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003615
3616 return 0;
3617}
3618
Maor Gottlieb5163b272020-04-30 22:21:45 +03003619static unsigned int get_tx_affinity_rr(struct mlx5_ib_dev *dev,
3620 struct ib_udata *udata)
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003621{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003622 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
3623 udata, struct mlx5_ib_ucontext, ibucontext);
Maor Gottlieb5163b272020-04-30 22:21:45 +03003624 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
3625 atomic_t *tx_port_affinity;
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003626
Maor Gottlieb5163b272020-04-30 22:21:45 +03003627 if (ucontext)
3628 tx_port_affinity = &ucontext->tx_port_affinity;
3629 else
3630 tx_port_affinity = &dev->port[port_num].roce.tx_port_affinity;
3631
3632 return (unsigned int)atomic_add_return(1, tx_port_affinity) %
3633 MLX5_MAX_PORTS + 1;
3634}
3635
3636static bool qp_supports_affinity(struct ib_qp *qp)
3637{
Maor Gottlieb5163b272020-04-30 22:21:45 +03003638 if ((qp->qp_type == IB_QPT_RC) ||
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003639 (qp->qp_type == IB_QPT_UD) ||
Maor Gottlieb5163b272020-04-30 22:21:45 +03003640 (qp->qp_type == IB_QPT_UC) ||
3641 (qp->qp_type == IB_QPT_RAW_PACKET) ||
3642 (qp->qp_type == IB_QPT_XRC_INI) ||
3643 (qp->qp_type == IB_QPT_XRC_TGT))
3644 return true;
3645 return false;
3646}
3647
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003648static unsigned int get_tx_affinity(struct ib_qp *qp,
3649 const struct ib_qp_attr *attr,
3650 int attr_mask, u8 init,
Maor Gottlieb5163b272020-04-30 22:21:45 +03003651 struct ib_udata *udata)
3652{
3653 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
3654 udata, struct mlx5_ib_ucontext, ibucontext);
3655 struct mlx5_ib_dev *dev = to_mdev(qp->device);
3656 struct mlx5_ib_qp *mqp = to_mqp(qp);
3657 struct mlx5_ib_qp_base *qp_base;
3658 unsigned int tx_affinity;
3659
Mark Zhang802dcc72020-05-27 08:50:14 +03003660 if (!(mlx5_ib_lag_should_assign_affinity(dev) &&
3661 qp_supports_affinity(qp)))
Maor Gottlieb5163b272020-04-30 22:21:45 +03003662 return 0;
3663
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003664 if (mqp->flags & MLX5_IB_QP_CREATE_SQPN_QP1)
3665 tx_affinity = mqp->gsi_lag_port;
3666 else if (init)
3667 tx_affinity = get_tx_affinity_rr(dev, udata);
3668 else if ((attr_mask & IB_QP_AV) && attr->xmit_slave)
3669 tx_affinity =
3670 mlx5_lag_get_slave_port(dev->mdev, attr->xmit_slave);
3671 else
3672 return 0;
Maor Gottlieb5163b272020-04-30 22:21:45 +03003673
3674 qp_base = &mqp->trans_qp.base;
3675 if (ucontext)
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003676 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x ucontext %p\n",
Maor Gottlieb5163b272020-04-30 22:21:45 +03003677 tx_affinity, qp_base->mqp.qpn, ucontext);
3678 else
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003679 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x\n",
Maor Gottlieb5163b272020-04-30 22:21:45 +03003680 tx_affinity, qp_base->mqp.qpn);
3681 return tx_affinity;
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003682}
3683
Mark Zhangd14133d2019-07-02 13:02:36 +03003684static int __mlx5_ib_qp_set_counter(struct ib_qp *qp,
3685 struct rdma_counter *counter)
3686{
3687 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Leon Romanovsky64bae2d2020-05-26 14:54:36 +03003688 u32 in[MLX5_ST_SZ_DW(rts2rts_qp_in)] = {};
Mark Zhangd14133d2019-07-02 13:02:36 +03003689 struct mlx5_ib_qp *mqp = to_mqp(qp);
Mark Zhangd14133d2019-07-02 13:02:36 +03003690 struct mlx5_ib_qp_base *base;
3691 u32 set_id;
Leon Romanovsky64bae2d2020-05-26 14:54:36 +03003692 u32 *qpc;
Mark Zhangd14133d2019-07-02 13:02:36 +03003693
Parav Pandit3e1f0002019-07-23 10:31:17 +03003694 if (counter)
Mark Zhangd14133d2019-07-02 13:02:36 +03003695 set_id = counter->id;
Parav Pandit3e1f0002019-07-23 10:31:17 +03003696 else
3697 set_id = mlx5_ib_get_counters_id(dev, mqp->port - 1);
Mark Zhangd14133d2019-07-02 13:02:36 +03003698
3699 base = &mqp->trans_qp.base;
Leon Romanovsky64bae2d2020-05-26 14:54:36 +03003700 MLX5_SET(rts2rts_qp_in, in, opcode, MLX5_CMD_OP_RTS2RTS_QP);
3701 MLX5_SET(rts2rts_qp_in, in, qpn, base->mqp.qpn);
3702 MLX5_SET(rts2rts_qp_in, in, uid, base->mqp.uid);
3703 MLX5_SET(rts2rts_qp_in, in, opt_param_mask,
3704 MLX5_QP_OPTPAR_COUNTER_SET_ID);
3705
3706 qpc = MLX5_ADDR_OF(rts2rts_qp_in, in, qpc);
3707 MLX5_SET(qpc, qpc, counter_set_id, set_id);
3708 return mlx5_cmd_exec_in(dev->mdev, rts2rts_qp, in);
Mark Zhangd14133d2019-07-02 13:02:36 +03003709}
3710
Eli Cohene126ba92013-07-07 17:25:49 +03003711static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
3712 const struct ib_qp_attr *attr, int attr_mask,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003713 enum ib_qp_state cur_state,
3714 enum ib_qp_state new_state,
3715 const struct mlx5_ib_modify_qp *ucmd,
Leon Romanovsky50aec2c2020-05-26 14:54:40 +03003716 struct mlx5_ib_modify_qp_resp *resp,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003717 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003718{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003719 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
3720 [MLX5_QP_STATE_RST] = {
3721 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3722 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3723 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
3724 },
3725 [MLX5_QP_STATE_INIT] = {
3726 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3727 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3728 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
3729 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
3730 },
3731 [MLX5_QP_STATE_RTR] = {
3732 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3733 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3734 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
3735 },
3736 [MLX5_QP_STATE_RTS] = {
3737 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3738 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3739 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
3740 },
3741 [MLX5_QP_STATE_SQD] = {
3742 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3743 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3744 },
3745 [MLX5_QP_STATE_SQER] = {
3746 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3747 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3748 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
3749 },
3750 [MLX5_QP_STATE_ERR] = {
3751 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3752 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3753 }
3754 };
3755
Eli Cohene126ba92013-07-07 17:25:49 +03003756 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3757 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02003758 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03003759 struct mlx5_ib_cq *send_cq, *recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003760 struct mlx5_ib_pd *pd;
3761 enum mlx5_qp_state mlx5_cur, mlx5_new;
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003762 void *qpc, *pri_path, *alt_path;
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003763 enum mlx5_qp_optpar optpar = 0;
Mark Zhangd14133d2019-07-02 13:02:36 +03003764 u32 set_id = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003765 int mlx5_st;
3766 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003767 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03003768 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003769
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03003770 mlx5_st = to_mlx5_st(qp->type);
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003771 if (mlx5_st < 0)
3772 return -EINVAL;
3773
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003774 qpc = kzalloc(MLX5_ST_SZ_BYTES(qpc), GFP_KERNEL);
3775 if (!qpc)
Eli Cohene126ba92013-07-07 17:25:49 +03003776 return -ENOMEM;
3777
Leon Romanovsky029e88f2020-05-06 09:55:13 +03003778 pd = to_mpd(qp->ibqp.pd);
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003779 MLX5_SET(qpc, qpc, st, mlx5_st);
Eli Cohene126ba92013-07-07 17:25:49 +03003780
3781 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003782 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03003783 } else {
3784 switch (attr->path_mig_state) {
3785 case IB_MIG_MIGRATED:
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003786 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03003787 break;
3788 case IB_MIG_REARM:
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003789 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_REARM);
Eli Cohene126ba92013-07-07 17:25:49 +03003790 break;
3791 case IB_MIG_ARMED:
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003792 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_ARMED);
Eli Cohene126ba92013-07-07 17:25:49 +03003793 break;
3794 }
3795 }
3796
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003797 tx_affinity = get_tx_affinity(ibqp, attr, attr_mask,
Maor Gottlieb5163b272020-04-30 22:21:45 +03003798 cur_state == IB_QPS_RESET &&
3799 new_state == IB_QPS_INIT, udata);
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003800
3801 MLX5_SET(qpc, qpc, lag_tx_port_affinity, tx_affinity);
3802 if (tx_affinity && new_state == IB_QPS_RTR &&
3803 MLX5_CAP_GEN(dev->mdev, init2_lag_tx_port_affinity))
3804 optpar |= MLX5_QP_OPTPAR_LAG_TX_AFF;
Aviv Heller13eab212016-09-18 20:48:04 +03003805
Haggai Erand16e91d2016-02-29 15:45:05 +02003806 if (is_sqp(ibqp->qp_type)) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003807 MLX5_SET(qpc, qpc, mtu, IB_MTU_256);
3808 MLX5_SET(qpc, qpc, log_msg_max, 8);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003809 } else if ((ibqp->qp_type == IB_QPT_UD &&
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003810 !(qp->flags & IB_QP_CREATE_SOURCE_QPN)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003811 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003812 MLX5_SET(qpc, qpc, mtu, IB_MTU_4096);
3813 MLX5_SET(qpc, qpc, log_msg_max, 12);
Eli Cohene126ba92013-07-07 17:25:49 +03003814 } else if (attr_mask & IB_QP_PATH_MTU) {
3815 if (attr->path_mtu < IB_MTU_256 ||
3816 attr->path_mtu > IB_MTU_4096) {
3817 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3818 err = -EINVAL;
3819 goto out;
3820 }
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003821 MLX5_SET(qpc, qpc, mtu, attr->path_mtu);
3822 MLX5_SET(qpc, qpc, log_msg_max,
3823 MLX5_CAP_GEN(dev->mdev, log_max_msg));
Eli Cohene126ba92013-07-07 17:25:49 +03003824 }
3825
3826 if (attr_mask & IB_QP_DEST_QPN)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003827 MLX5_SET(qpc, qpc, remote_qpn, attr->dest_qp_num);
3828
3829 pri_path = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
3830 alt_path = MLX5_ADDR_OF(qpc, qpc, secondary_address_path);
Eli Cohene126ba92013-07-07 17:25:49 +03003831
3832 if (attr_mask & IB_QP_PKEY_INDEX)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003833 MLX5_SET(ads, pri_path, pkey_index, attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003834
3835 /* todo implement counter_index functionality */
3836
3837 if (is_sqp(ibqp->qp_type))
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003838 MLX5_SET(ads, pri_path, vhca_port_num, qp->port);
Eli Cohene126ba92013-07-07 17:25:49 +03003839
3840 if (attr_mask & IB_QP_PORT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003841 MLX5_SET(ads, pri_path, vhca_port_num, attr->port_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003842
3843 if (attr_mask & IB_QP_AV) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003844 err = mlx5_set_path(dev, qp, &attr->ah_attr, pri_path,
3845 attr_mask & IB_QP_PORT ? attr->port_num :
3846 qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003847 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003848 if (err)
3849 goto out;
3850 }
3851
3852 if (attr_mask & IB_QP_TIMEOUT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003853 MLX5_SET(ads, pri_path, ack_timeout, attr->timeout);
Eli Cohene126ba92013-07-07 17:25:49 +03003854
3855 if (attr_mask & IB_QP_ALT_PATH) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003856 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr, alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003857 attr->alt_port_num,
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003858 attr_mask | IB_QP_PKEY_INDEX |
3859 IB_QP_TIMEOUT,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003860 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003861 if (err)
3862 goto out;
3863 }
3864
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003865 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3866 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003867
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003868 MLX5_SET(qpc, qpc, pd, pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3869 if (send_cq)
3870 MLX5_SET(qpc, qpc, cqn_snd, send_cq->mcq.cqn);
3871 if (recv_cq)
3872 MLX5_SET(qpc, qpc, cqn_rcv, recv_cq->mcq.cqn);
3873
3874 MLX5_SET(qpc, qpc, log_ack_req_freq, MLX5_IB_ACK_REQ_FREQ);
Eli Cohene126ba92013-07-07 17:25:49 +03003875
3876 if (attr_mask & IB_QP_RNR_RETRY)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003877 MLX5_SET(qpc, qpc, rnr_retry, attr->rnr_retry);
Eli Cohene126ba92013-07-07 17:25:49 +03003878
3879 if (attr_mask & IB_QP_RETRY_CNT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003880 MLX5_SET(qpc, qpc, retry_count, attr->retry_cnt);
Eli Cohene126ba92013-07-07 17:25:49 +03003881
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003882 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC && attr->max_rd_atomic)
3883 MLX5_SET(qpc, qpc, log_sra_max, ilog2(attr->max_rd_atomic));
Eli Cohene126ba92013-07-07 17:25:49 +03003884
3885 if (attr_mask & IB_QP_SQ_PSN)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003886 MLX5_SET(qpc, qpc, next_send_psn, attr->sq_psn);
Eli Cohene126ba92013-07-07 17:25:49 +03003887
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003888 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC && attr->max_dest_rd_atomic)
3889 MLX5_SET(qpc, qpc, log_rra_max,
3890 ilog2(attr->max_dest_rd_atomic));
Eli Cohene126ba92013-07-07 17:25:49 +03003891
Yonatan Cohena60109d2018-10-10 09:25:16 +03003892 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003893 err = set_qpc_atomic_flags(qp, attr, attr_mask, qpc);
Yonatan Cohena60109d2018-10-10 09:25:16 +03003894 if (err)
3895 goto out;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003896 }
Eli Cohene126ba92013-07-07 17:25:49 +03003897
3898 if (attr_mask & IB_QP_MIN_RNR_TIMER)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003899 MLX5_SET(qpc, qpc, min_rnr_nak, attr->min_rnr_timer);
Eli Cohene126ba92013-07-07 17:25:49 +03003900
3901 if (attr_mask & IB_QP_RQ_PSN)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003902 MLX5_SET(qpc, qpc, next_rcv_psn, attr->rq_psn);
Eli Cohene126ba92013-07-07 17:25:49 +03003903
3904 if (attr_mask & IB_QP_QKEY)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003905 MLX5_SET(qpc, qpc, q_key, attr->qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003906
3907 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003908 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03003909
Mark Bloch0837e862016-06-17 15:10:55 +03003910 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3911 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3912 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003913
3914 /* Underlay port should be used - index 0 function per port */
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003915 if (qp->flags & IB_QP_CREATE_SOURCE_QPN)
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003916 port_num = 0;
3917
Mark Zhangd14133d2019-07-02 13:02:36 +03003918 if (ibqp->counter)
3919 set_id = ibqp->counter->id;
3920 else
Parav Pandit3e1f0002019-07-23 10:31:17 +03003921 set_id = mlx5_ib_get_counters_id(dev, port_num);
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003922 MLX5_SET(qpc, qpc, counter_set_id, set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003923 }
3924
Eli Cohene126ba92013-07-07 17:25:49 +03003925 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003926 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03003927
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003928 if (qp->flags & MLX5_IB_QP_CREATE_SQPN_QP1)
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03003929 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03003930
3931 mlx5_cur = to_mlx5_state(cur_state);
3932 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003933
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003934 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003935 !optab[mlx5_cur][mlx5_new]) {
3936 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003937 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003938 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003939
3940 op = optab[mlx5_cur][mlx5_new];
Maor Gottliebcfc1a892020-04-30 22:21:46 +03003941 optpar |= ib_mask_to_mlx5_opt(attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003942 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003943
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003944 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
Leon Romanovsky2be08c32020-04-27 18:46:13 +03003945 qp->flags & IB_QP_CREATE_SOURCE_QPN) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003946 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3947
3948 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003949 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Mark Zhangd14133d2019-07-02 13:02:36 +03003950 raw_qp_param.rq_q_ctr_id = set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003951 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3952 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003953
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003954 if (attr_mask & IB_QP_PORT)
3955 raw_qp_param.port = attr->port_num;
3956
Bodong Wang7d29f342016-12-01 13:43:16 +02003957 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003958 raw_qp_param.rl.rate = attr->rate_limit;
3959
3960 if (ucmd->burst_info.max_burst_sz) {
3961 if (attr->rate_limit &&
3962 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3963 raw_qp_param.rl.max_burst_sz =
3964 ucmd->burst_info.max_burst_sz;
3965 } else {
3966 err = -EINVAL;
3967 goto out;
3968 }
3969 }
3970
3971 if (ucmd->burst_info.typical_pkt_sz) {
3972 if (attr->rate_limit &&
3973 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3974 raw_qp_param.rl.typical_pkt_sz =
3975 ucmd->burst_info.typical_pkt_sz;
3976 } else {
3977 err = -EINVAL;
3978 goto out;
3979 }
3980 }
3981
Bodong Wang7d29f342016-12-01 13:43:16 +02003982 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3983 }
3984
Aviv Heller13eab212016-09-18 20:48:04 +03003985 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003986 } else {
Leon Romanovsky50aec2c2020-05-26 14:54:40 +03003987 if (udata) {
3988 /* For the kernel flows, the resp will stay zero */
3989 resp->ece_options =
3990 MLX5_CAP_GEN(dev->mdev, ece_support) ?
3991 ucmd->ece_options : 0;
3992 resp->response_length = sizeof(*resp);
3993 }
Leon Romanovsky5f62a522020-05-26 14:54:39 +03003994 err = mlx5_core_qp_modify(dev, op, optpar, qpc, &base->mqp,
Leon Romanovsky50aec2c2020-05-26 14:54:40 +03003995 &resp->ece_options);
Alex Vesker0680efa2016-08-28 12:25:52 +03003996 }
3997
Eli Cohene126ba92013-07-07 17:25:49 +03003998 if (err)
3999 goto out;
4000
4001 qp->state = new_state;
4002
4003 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02004004 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03004005 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02004006 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03004007 if (attr_mask & IB_QP_PORT)
4008 qp->port = attr->port_num;
4009 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02004010 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03004011
4012 /*
4013 * If we moved a kernel QP to RESET, clean up all old CQ
4014 * entries and reinitialize the QP.
4015 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02004016 if (new_state == IB_QPS_RESET &&
4017 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02004018 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03004019 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
4020 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02004021 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004022
4023 qp->rq.head = 0;
4024 qp->rq.tail = 0;
4025 qp->sq.head = 0;
4026 qp->sq.tail = 0;
4027 qp->sq.cur_post = 0;
Guy Levi34f4c952018-11-26 08:15:50 +02004028 if (qp->sq.wqe_cnt)
4029 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
Leon Romanovsky950bf4f2020-03-18 11:16:40 +02004030 qp->sq.last_poll = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004031 qp->db.db[MLX5_RCV_DBR] = 0;
4032 qp->db.db[MLX5_SND_DBR] = 0;
4033 }
4034
Mark Zhangd14133d2019-07-02 13:02:36 +03004035 if ((new_state == IB_QPS_RTS) && qp->counter_pending) {
4036 err = __mlx5_ib_qp_set_counter(ibqp, ibqp->counter);
4037 if (!err)
4038 qp->counter_pending = 0;
4039 }
4040
Eli Cohene126ba92013-07-07 17:25:49 +03004041out:
Leon Romanovskyf18e26a2020-05-26 14:54:38 +03004042 kfree(qpc);
Eli Cohene126ba92013-07-07 17:25:49 +03004043 return err;
4044}
4045
Moni Shouac32a4f22018-01-02 16:19:32 +02004046static inline bool is_valid_mask(int mask, int req, int opt)
4047{
4048 if ((mask & req) != req)
4049 return false;
4050
4051 if (mask & ~(req | opt))
4052 return false;
4053
4054 return true;
4055}
4056
4057/* check valid transition for driver QP types
4058 * for now the only QP type that this function supports is DCI
4059 */
4060static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
4061 enum ib_qp_attr_mask attr_mask)
4062{
4063 int req = IB_QP_STATE;
4064 int opt = 0;
4065
Moni Shoua99ed7482018-09-12 09:33:55 +03004066 if (new_state == IB_QPS_RESET) {
4067 return is_valid_mask(attr_mask, req, opt);
4068 } else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Moni Shouac32a4f22018-01-02 16:19:32 +02004069 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
4070 return is_valid_mask(attr_mask, req, opt);
4071 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
4072 opt = 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_RTR) {
4075 req |= IB_QP_PATH_MTU;
Artemy Kovalyov5ec03042018-11-05 08:12:07 +02004076 opt = IB_QP_PKEY_INDEX | IB_QP_AV;
Moni Shouac32a4f22018-01-02 16:19:32 +02004077 return is_valid_mask(attr_mask, req, opt);
4078 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
4079 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
4080 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
4081 opt = IB_QP_MIN_RNR_TIMER;
4082 return is_valid_mask(attr_mask, req, opt);
4083 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
4084 opt = IB_QP_MIN_RNR_TIMER;
4085 return is_valid_mask(attr_mask, req, opt);
4086 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
4087 return is_valid_mask(attr_mask, req, opt);
4088 }
4089 return false;
4090}
4091
Moni Shoua776a3902018-01-02 16:19:33 +02004092/* mlx5_ib_modify_dct: modify a DCT QP
4093 * valid transitions are:
4094 * RESET to INIT: must set access_flags, pkey_index and port
4095 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
4096 * mtu, gid_index and hop_limit
4097 * Other transitions and attributes are illegal
4098 */
4099static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
Leon Romanovskya645a892020-06-02 15:55:48 +03004100 int attr_mask, struct mlx5_ib_modify_qp *ucmd,
4101 struct ib_udata *udata)
Moni Shoua776a3902018-01-02 16:19:33 +02004102{
4103 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4104 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4105 enum ib_qp_state cur_state, new_state;
4106 int err = 0;
4107 int required = IB_QP_STATE;
4108 void *dctc;
4109
4110 if (!(attr_mask & IB_QP_STATE))
4111 return -EINVAL;
4112
4113 cur_state = qp->state;
4114 new_state = attr->qp_state;
4115
4116 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Leon Romanovskya645a892020-06-02 15:55:48 +03004117 if (MLX5_CAP_GEN(dev->mdev, ece_support) && ucmd->ece_options)
4118 /*
4119 * DCT doesn't initialize QP till modify command is executed,
4120 * so we need to overwrite previously set ECE field if user
4121 * provided any value except zero, which means not set/not
4122 * valid.
4123 */
4124 MLX5_SET(dctc, dctc, ece, ucmd->ece_options);
4125
Moni Shoua776a3902018-01-02 16:19:33 +02004126 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03004127 u16 set_id;
4128
Moni Shoua776a3902018-01-02 16:19:33 +02004129 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
4130 if (!is_valid_mask(attr_mask, required, 0))
4131 return -EINVAL;
4132
4133 if (attr->port_num == 0 ||
4134 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
4135 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
4136 attr->port_num, dev->num_ports);
4137 return -EINVAL;
4138 }
4139 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
4140 MLX5_SET(dctc, dctc, rre, 1);
4141 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
4142 MLX5_SET(dctc, dctc, rwe, 1);
4143 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03004144 int atomic_mode;
4145
4146 atomic_mode = get_atomic_mode(dev, MLX5_IB_QPT_DCT);
4147 if (atomic_mode < 0)
Moni Shoua776a3902018-01-02 16:19:33 +02004148 return -EOPNOTSUPP;
Yonatan Cohena60109d2018-10-10 09:25:16 +03004149
4150 MLX5_SET(dctc, dctc, atomic_mode, atomic_mode);
Moni Shoua776a3902018-01-02 16:19:33 +02004151 MLX5_SET(dctc, dctc, rae, 1);
Moni Shoua776a3902018-01-02 16:19:33 +02004152 }
4153 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
4154 MLX5_SET(dctc, dctc, port, attr->port_num);
Parav Pandit3e1f0002019-07-23 10:31:17 +03004155
4156 set_id = mlx5_ib_get_counters_id(dev, attr->port_num - 1);
4157 MLX5_SET(dctc, dctc, counter_set_id, set_id);
Moni Shoua776a3902018-01-02 16:19:33 +02004158 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
4159 struct mlx5_ib_modify_qp_resp resp = {};
Leon Romanovskya645a892020-06-02 15:55:48 +03004160 u32 out[MLX5_ST_SZ_DW(create_dct_out)] = {};
4161 u32 min_resp_len = offsetofend(typeof(resp), dctn);
Moni Shoua776a3902018-01-02 16:19:33 +02004162
4163 if (udata->outlen < min_resp_len)
4164 return -EINVAL;
4165 resp.response_length = min_resp_len;
4166
Leon Romanovskya645a892020-06-02 15:55:48 +03004167 /*
4168 * If we don't have enough space for the ECE options,
4169 * simply indicate it with resp.response_length.
4170 */
4171 resp.response_length = (udata->outlen < sizeof(resp)) ?
4172 min_resp_len :
4173 sizeof(resp);
4174
Moni Shoua776a3902018-01-02 16:19:33 +02004175 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
4176 if (!is_valid_mask(attr_mask, required, 0))
4177 return -EINVAL;
4178 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
4179 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
4180 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
4181 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
4182 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
4183 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
4184
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004185 err = mlx5_core_create_dct(dev, &qp->dct.mdct, qp->dct.in,
Yishai Hadasc5ae1952019-03-06 19:21:42 +02004186 MLX5_ST_SZ_BYTES(create_dct_in), out,
4187 sizeof(out));
Moni Shoua776a3902018-01-02 16:19:33 +02004188 if (err)
4189 return err;
4190 resp.dctn = qp->dct.mdct.mqp.qpn;
Leon Romanovskya645a892020-06-02 15:55:48 +03004191 if (MLX5_CAP_GEN(dev->mdev, ece_support))
4192 resp.ece_options = MLX5_GET(create_dct_out, out, ece);
Moni Shoua776a3902018-01-02 16:19:33 +02004193 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4194 if (err) {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004195 mlx5_core_destroy_dct(dev, &qp->dct.mdct);
Moni Shoua776a3902018-01-02 16:19:33 +02004196 return err;
4197 }
4198 } else {
4199 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
4200 return -EINVAL;
4201 }
4202 if (err)
4203 qp->state = IB_QPS_ERR;
4204 else
4205 qp->state = new_state;
4206 return err;
4207}
4208
Eli Cohene126ba92013-07-07 17:25:49 +03004209int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
4210 int attr_mask, struct ib_udata *udata)
4211{
4212 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Leon Romanovsky50aec2c2020-05-26 14:54:40 +03004213 struct mlx5_ib_modify_qp_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03004214 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02004215 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02004216 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03004217 enum ib_qp_state cur_state, new_state;
4218 int err = -EINVAL;
4219 int port;
4220
Yishai Hadas28d61372016-05-23 15:20:56 +03004221 if (ibqp->rwq_ind_tbl)
4222 return -ENOSYS;
4223
Bodong Wang61147f32018-03-19 15:10:30 +02004224 if (udata && udata->inlen) {
Leon Romanovsky5f62a522020-05-26 14:54:39 +03004225 if (udata->inlen < offsetofend(typeof(ucmd), ece_options))
Bodong Wang61147f32018-03-19 15:10:30 +02004226 return -EINVAL;
4227
4228 if (udata->inlen > sizeof(ucmd) &&
4229 !ib_is_udata_cleared(udata, sizeof(ucmd),
4230 udata->inlen - sizeof(ucmd)))
4231 return -EOPNOTSUPP;
4232
4233 if (ib_copy_from_udata(&ucmd, udata,
4234 min(udata->inlen, sizeof(ucmd))))
4235 return -EFAULT;
4236
4237 if (ucmd.comp_mask ||
Bodong Wang61147f32018-03-19 15:10:30 +02004238 memchr_inv(&ucmd.burst_info.reserved, 0,
4239 sizeof(ucmd.burst_info.reserved)))
4240 return -EOPNOTSUPP;
Leon Romanovsky5f62a522020-05-26 14:54:39 +03004241
Bodong Wang61147f32018-03-19 15:10:30 +02004242 }
4243
Haggai Erand16e91d2016-02-29 15:45:05 +02004244 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4245 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
4246
Leon Romanovsky7aede1a22020-04-27 18:46:20 +03004247 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ? IB_QPT_GSI :
4248 qp->type;
Moni Shouac32a4f22018-01-02 16:19:32 +02004249
Leon Romanovskya645a892020-06-02 15:55:48 +03004250 if (qp_type == MLX5_IB_QPT_DCT)
4251 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, &ucmd, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02004252
Eli Cohene126ba92013-07-07 17:25:49 +03004253 mutex_lock(&qp->mutex);
4254
4255 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
4256 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
4257
Achiad Shochat2811ba52015-12-23 18:47:24 +02004258 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
4259 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02004260 }
4261
Leon Romanovsky2be08c32020-04-27 18:46:13 +03004262 if (qp->flags & IB_QP_CREATE_SOURCE_QPN) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004263 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
4264 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
4265 attr_mask);
4266 goto out;
4267 }
4268 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02004269 qp_type != MLX5_IB_QPT_DCI &&
Kamal Heibd31131b2018-10-02 16:11:21 +03004270 !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
4271 attr_mask)) {
Haggai Eran158abf82016-02-29 15:45:04 +02004272 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
4273 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03004274 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02004275 } else if (qp_type == MLX5_IB_QPT_DCI &&
4276 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
4277 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
4278 cur_state, new_state, qp_type, attr_mask);
4279 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004280 }
Eli Cohene126ba92013-07-07 17:25:49 +03004281
4282 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004283 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02004284 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02004285 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
4286 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03004287 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004288 }
Eli Cohene126ba92013-07-07 17:25:49 +03004289
4290 if (attr_mask & IB_QP_PKEY_INDEX) {
4291 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03004292 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02004293 dev->mdev->port_caps[port - 1].pkey_table_len) {
4294 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
4295 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004296 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004297 }
Eli Cohene126ba92013-07-07 17:25:49 +03004298 }
4299
4300 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004301 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02004302 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
4303 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
4304 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03004305 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004306 }
Eli Cohene126ba92013-07-07 17:25:49 +03004307
4308 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004309 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02004310 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
4311 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
4312 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03004313 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004314 }
Eli Cohene126ba92013-07-07 17:25:49 +03004315
4316 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
4317 err = 0;
4318 goto out;
4319 }
4320
Bodong Wang61147f32018-03-19 15:10:30 +02004321 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
Leon Romanovsky50aec2c2020-05-26 14:54:40 +03004322 new_state, &ucmd, &resp, udata);
4323
4324 /* resp.response_length is set in ECE supported flows only */
4325 if (!err && resp.response_length &&
4326 udata->outlen >= resp.response_length)
Leon Romanovsky6512f112020-06-02 15:55:46 +03004327 /* Return -EFAULT to the user and expect him to destroy QP. */
4328 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03004329
4330out:
4331 mutex_unlock(&qp->mutex);
4332 return err;
4333}
4334
Eli Cohene126ba92013-07-07 17:25:49 +03004335static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4336{
4337 switch (mlx5_state) {
4338 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4339 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4340 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4341 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4342 case MLX5_QP_STATE_SQ_DRAINING:
4343 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4344 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4345 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4346 default: return -1;
4347 }
4348}
4349
4350static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4351{
4352 switch (mlx5_mig_state) {
4353 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4354 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4355 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4356 default: return -1;
4357 }
4358}
4359
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004360static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004361 struct rdma_ah_attr *ah_attr, void *path)
Eli Cohene126ba92013-07-07 17:25:49 +03004362{
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004363 int port = MLX5_GET(ads, path, vhca_port_num);
4364 int static_rate;
Eli Cohene126ba92013-07-07 17:25:49 +03004365
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004366 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03004367
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004368 if (!port || port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03004369 return;
4370
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004371 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port);
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02004372
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004373 rdma_ah_set_port_num(ah_attr, port);
4374 rdma_ah_set_sl(ah_attr, MLX5_GET(ads, path, sl));
Eli Cohene126ba92013-07-07 17:25:49 +03004375
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004376 rdma_ah_set_dlid(ah_attr, MLX5_GET(ads, path, rlid));
4377 rdma_ah_set_path_bits(ah_attr, MLX5_GET(ads, path, mlid));
Aharon Landau2d7e3ff2020-04-13 16:20:28 +03004378
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004379 static_rate = MLX5_GET(ads, path, stat_rate);
4380 rdma_ah_set_static_rate(ah_attr, static_rate ? static_rate - 5 : 0);
4381 if (MLX5_GET(ads, path, grh) ||
Aharon Landau2d7e3ff2020-04-13 16:20:28 +03004382 ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
Leon Romanovsky70bd7fb2020-05-26 14:54:37 +03004383 rdma_ah_set_grh(ah_attr, NULL, MLX5_GET(ads, path, flow_label),
4384 MLX5_GET(ads, path, src_addr_index),
4385 MLX5_GET(ads, path, hop_limit),
4386 MLX5_GET(ads, path, tclass));
4387 memcpy(ah_attr, MLX5_ADDR_OF(ads, path, rgid_rip),
4388 MLX5_FLD_SZ_BYTES(ads, 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
Leon Romanovsky28ad5f652020-06-30 13:18:54 +03004703int mlx5_ib_alloc_xrcd(struct ib_xrcd *ibxrcd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03004704{
Leon Romanovsky28ad5f652020-06-30 13:18:54 +03004705 struct mlx5_ib_dev *dev = to_mdev(ibxrcd->device);
4706 struct mlx5_ib_xrcd *xrcd = to_mxrcd(ibxrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03004707
Saeed Mahameed938fe832015-05-28 22:28:41 +03004708 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Leon Romanovsky28ad5f652020-06-30 13:18:54 +03004709 return -EOPNOTSUPP;
Eli Cohene126ba92013-07-07 17:25:49 +03004710
Leon Romanovsky28ad5f652020-06-30 13:18:54 +03004711 return mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004712}
4713
Leon Romanovsky28ad5f652020-06-30 13:18:54 +03004714void mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03004715{
4716 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
4717 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03004718
Leon Romanovsky28ad5f652020-06-30 13:18:54 +03004719 mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004720}
Yishai Hadas79b20a62016-05-23 15:20:50 +03004721
Yishai Hadas350d0e42016-08-28 14:58:18 +03004722static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
4723{
4724 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
4725 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
4726 struct ib_event event;
4727
4728 if (rwq->ibwq.event_handler) {
4729 event.device = rwq->ibwq.device;
4730 event.element.wq = &rwq->ibwq;
4731 switch (type) {
4732 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
4733 event.event = IB_EVENT_WQ_FATAL;
4734 break;
4735 default:
4736 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
4737 return;
4738 }
4739
4740 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
4741 }
4742}
4743
Maor Gottlieb03404e82017-05-30 10:29:13 +03004744static int set_delay_drop(struct mlx5_ib_dev *dev)
4745{
4746 int err = 0;
4747
4748 mutex_lock(&dev->delay_drop.lock);
4749 if (dev->delay_drop.activate)
4750 goto out;
4751
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004752 err = mlx5_core_set_delay_drop(dev, dev->delay_drop.timeout);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004753 if (err)
4754 goto out;
4755
4756 dev->delay_drop.activate = true;
4757out:
4758 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004759
4760 if (!err)
4761 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004762 return err;
4763}
4764
Yishai Hadas79b20a62016-05-23 15:20:50 +03004765static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
4766 struct ib_wq_init_attr *init_attr)
4767{
4768 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02004769 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004770 __be64 *rq_pas0;
4771 void *in;
4772 void *rqc;
4773 void *wq;
4774 int inlen;
4775 int err;
4776
4777 dev = to_mdev(pd->device);
4778
4779 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004780 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004781 if (!in)
4782 return -ENOMEM;
4783
Yishai Hadas34d57582018-09-20 21:39:21 +03004784 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004785 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
4786 MLX5_SET(rqc, rqc, mem_rq_type,
4787 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
4788 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
4789 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
4790 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
4791 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
4792 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03004793 MLX5_SET(wq, wq, wq_type,
4794 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
4795 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02004796 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
4797 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
4798 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
4799 err = -EOPNOTSUPP;
4800 goto out;
4801 } else {
4802 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
4803 }
4804 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004805 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03004806 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
Mark Zhangc16339b2019-11-15 17:45:55 +02004807 /*
4808 * In Firmware number of strides in each WQE is:
4809 * "512 * 2^single_wqe_log_num_of_strides"
4810 * Values 3 to 8 are accepted as 10 to 15, 9 to 18 are
4811 * accepted as 0 to 9
4812 */
4813 static const u8 fw_map[] = { 10, 11, 12, 13, 14, 15, 0, 1,
4814 2, 3, 4, 5, 6, 7, 8, 9 };
Noa Osherovichccc87082017-10-17 18:01:13 +03004815 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
4816 MLX5_SET(wq, wq, log_wqe_stride_size,
4817 rwq->single_stride_log_num_of_bytes -
4818 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
Mark Zhangc16339b2019-11-15 17:45:55 +02004819 MLX5_SET(wq, wq, log_wqe_num_of_strides,
4820 fw_map[rwq->log_num_strides -
4821 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES]);
Noa Osherovichccc87082017-10-17 18:01:13 +03004822 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004823 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
4824 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
4825 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
4826 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
4827 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
4828 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02004829 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004830 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02004831 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004832 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
4833 err = -EOPNOTSUPP;
4834 goto out;
4835 }
4836 } else {
4837 MLX5_SET(rqc, rqc, vsd, 1);
4838 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02004839 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
4840 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
4841 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
4842 err = -EOPNOTSUPP;
4843 goto out;
4844 }
4845 MLX5_SET(rqc, rqc, scatter_fcs, 1);
4846 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03004847 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4848 if (!(dev->ib_dev.attrs.raw_packet_caps &
4849 IB_RAW_PACKET_CAP_DELAY_DROP)) {
4850 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
4851 err = -EOPNOTSUPP;
4852 goto out;
4853 }
4854 MLX5_SET(rqc, rqc, delay_drop_en, 1);
4855 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004856 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
4857 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004858 err = mlx5_core_create_rq_tracked(dev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004859 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4860 err = set_delay_drop(dev);
4861 if (err) {
4862 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
4863 err);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03004864 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004865 } else {
4866 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
4867 }
4868 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004869out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03004870 kvfree(in);
4871 return err;
4872}
4873
4874static int set_user_rq_size(struct mlx5_ib_dev *dev,
4875 struct ib_wq_init_attr *wq_init_attr,
4876 struct mlx5_ib_create_wq *ucmd,
4877 struct mlx5_ib_rwq *rwq)
4878{
4879 /* Sanity check RQ size before proceeding */
4880 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
4881 return -EINVAL;
4882
4883 if (!ucmd->rq_wqe_count)
4884 return -EINVAL;
4885
4886 rwq->wqe_count = ucmd->rq_wqe_count;
4887 rwq->wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky0dfe4522018-08-01 14:25:41 -07004888 if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
4889 return -EINVAL;
4890
Yishai Hadas79b20a62016-05-23 15:20:50 +03004891 rwq->log_rq_stride = rwq->wqe_shift;
4892 rwq->log_rq_size = ilog2(rwq->wqe_count);
4893 return 0;
4894}
4895
Mark Zhangc16339b2019-11-15 17:45:55 +02004896static bool log_of_strides_valid(struct mlx5_ib_dev *dev, u32 log_num_strides)
4897{
4898 if ((log_num_strides > MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
4899 (log_num_strides < MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
4900 return false;
4901
4902 if (!MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) &&
4903 (log_num_strides < MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
4904 return false;
4905
4906 return true;
4907}
4908
Yishai Hadas79b20a62016-05-23 15:20:50 +03004909static int prepare_user_rq(struct ib_pd *pd,
4910 struct ib_wq_init_attr *init_attr,
4911 struct ib_udata *udata,
4912 struct mlx5_ib_rwq *rwq)
4913{
4914 struct mlx5_ib_dev *dev = to_mdev(pd->device);
4915 struct mlx5_ib_create_wq ucmd = {};
4916 int err;
4917 size_t required_cmd_sz;
4918
Noa Osherovichccc87082017-10-17 18:01:13 +03004919 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
4920 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004921 if (udata->inlen < required_cmd_sz) {
4922 mlx5_ib_dbg(dev, "invalid inlen\n");
4923 return -EINVAL;
4924 }
4925
4926 if (udata->inlen > sizeof(ucmd) &&
4927 !ib_is_udata_cleared(udata, sizeof(ucmd),
4928 udata->inlen - sizeof(ucmd))) {
4929 mlx5_ib_dbg(dev, "inlen is not supported\n");
4930 return -EOPNOTSUPP;
4931 }
4932
4933 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
4934 mlx5_ib_dbg(dev, "copy failed\n");
4935 return -EFAULT;
4936 }
4937
Noa Osherovichccc87082017-10-17 18:01:13 +03004938 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03004939 mlx5_ib_dbg(dev, "invalid comp mask\n");
4940 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03004941 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
4942 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
4943 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
4944 return -EOPNOTSUPP;
4945 }
4946 if ((ucmd.single_stride_log_num_of_bytes <
4947 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
4948 (ucmd.single_stride_log_num_of_bytes >
4949 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
4950 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
4951 ucmd.single_stride_log_num_of_bytes,
4952 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
4953 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
4954 return -EINVAL;
4955 }
Mark Zhangc16339b2019-11-15 17:45:55 +02004956 if (!log_of_strides_valid(dev,
4957 ucmd.single_wqe_log_num_of_strides)) {
4958 mlx5_ib_dbg(
4959 dev,
4960 "Invalid log num strides (%u. Range is %u - %u)\n",
4961 ucmd.single_wqe_log_num_of_strides,
4962 MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) ?
4963 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES :
4964 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
4965 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
Noa Osherovichccc87082017-10-17 18:01:13 +03004966 return -EINVAL;
4967 }
4968 rwq->single_stride_log_num_of_bytes =
4969 ucmd.single_stride_log_num_of_bytes;
4970 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
4971 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
4972 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004973 }
4974
4975 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
4976 if (err) {
4977 mlx5_ib_dbg(dev, "err %d\n", err);
4978 return err;
4979 }
4980
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02004981 err = create_user_rq(dev, pd, udata, rwq, &ucmd);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004982 if (err) {
4983 mlx5_ib_dbg(dev, "err %d\n", err);
Gal Pressman645ba592018-10-08 19:44:03 +03004984 return err;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004985 }
4986
4987 rwq->user_index = ucmd.user_index;
4988 return 0;
4989}
4990
4991struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
4992 struct ib_wq_init_attr *init_attr,
4993 struct ib_udata *udata)
4994{
4995 struct mlx5_ib_dev *dev;
4996 struct mlx5_ib_rwq *rwq;
4997 struct mlx5_ib_create_wq_resp resp = {};
4998 size_t min_resp_len;
4999 int err;
5000
5001 if (!udata)
5002 return ERR_PTR(-ENOSYS);
5003
5004 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5005 if (udata->outlen && udata->outlen < min_resp_len)
5006 return ERR_PTR(-EINVAL);
5007
Maor Gottliebba800132020-03-22 14:49:06 +02005008 if (!capable(CAP_SYS_RAWIO) &&
5009 init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP)
5010 return ERR_PTR(-EPERM);
5011
Yishai Hadas79b20a62016-05-23 15:20:50 +03005012 dev = to_mdev(pd->device);
5013 switch (init_attr->wq_type) {
5014 case IB_WQT_RQ:
5015 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
5016 if (!rwq)
5017 return ERR_PTR(-ENOMEM);
5018 err = prepare_user_rq(pd, init_attr, udata, rwq);
5019 if (err)
5020 goto err;
5021 err = create_rq(rwq, pd, init_attr);
5022 if (err)
5023 goto err_user_rq;
5024 break;
5025 default:
5026 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
5027 init_attr->wq_type);
5028 return ERR_PTR(-EINVAL);
5029 }
5030
Yishai Hadas350d0e42016-08-28 14:58:18 +03005031 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005032 rwq->ibwq.state = IB_WQS_RESET;
5033 if (udata->outlen) {
5034 resp.response_length = offsetof(typeof(resp), response_length) +
5035 sizeof(resp.response_length);
5036 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5037 if (err)
5038 goto err_copy;
5039 }
5040
Yishai Hadas350d0e42016-08-28 14:58:18 +03005041 rwq->core_qp.event = mlx5_ib_wq_event;
5042 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005043 return &rwq->ibwq;
5044
5045err_copy:
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03005046 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005047err_user_rq:
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03005048 destroy_user_rq(dev, pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005049err:
5050 kfree(rwq);
5051 return ERR_PTR(err);
5052}
5053
Leon Romanovskya49b1dc2019-06-12 15:27:41 +03005054void mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +03005055{
5056 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5057 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5058
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03005059 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03005060 destroy_user_rq(dev, wq->pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005061 kfree(rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005062}
5063
Yishai Hadasc5f90922016-05-23 15:20:53 +03005064struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
5065 struct ib_rwq_ind_table_init_attr *init_attr,
5066 struct ib_udata *udata)
5067{
5068 struct mlx5_ib_dev *dev = to_mdev(device);
5069 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
5070 int sz = 1 << init_attr->log_ind_tbl_size;
5071 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
5072 size_t min_resp_len;
5073 int inlen;
5074 int err;
5075 int i;
5076 u32 *in;
5077 void *rqtc;
5078
5079 if (udata->inlen > 0 &&
5080 !ib_is_udata_cleared(udata, 0,
5081 udata->inlen))
5082 return ERR_PTR(-EOPNOTSUPP);
5083
Maor Gottliebefd7f402016-10-27 16:36:40 +03005084 if (init_attr->log_ind_tbl_size >
5085 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
5086 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
5087 init_attr->log_ind_tbl_size,
5088 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
5089 return ERR_PTR(-EINVAL);
5090 }
5091
Yishai Hadasc5f90922016-05-23 15:20:53 +03005092 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5093 if (udata->outlen && udata->outlen < min_resp_len)
5094 return ERR_PTR(-EINVAL);
5095
5096 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
5097 if (!rwq_ind_tbl)
5098 return ERR_PTR(-ENOMEM);
5099
5100 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005101 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005102 if (!in) {
5103 err = -ENOMEM;
5104 goto err;
5105 }
5106
5107 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
5108
5109 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
5110 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
5111
5112 for (i = 0; i < sz; i++)
5113 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
5114
Yishai Hadas5deba862018-09-20 21:39:28 +03005115 rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
5116 MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);
5117
Yishai Hadasc5f90922016-05-23 15:20:53 +03005118 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
5119 kvfree(in);
5120
5121 if (err)
5122 goto err;
5123
5124 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
5125 if (udata->outlen) {
5126 resp.response_length = offsetof(typeof(resp), response_length) +
5127 sizeof(resp.response_length);
5128 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5129 if (err)
5130 goto err_copy;
5131 }
5132
5133 return &rwq_ind_tbl->ib_rwq_ind_tbl;
5134
5135err_copy:
Yishai Hadas5deba862018-09-20 21:39:28 +03005136 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005137err:
5138 kfree(rwq_ind_tbl);
5139 return ERR_PTR(err);
5140}
5141
5142int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
5143{
5144 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
5145 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
5146
Yishai Hadas5deba862018-09-20 21:39:28 +03005147 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005148
5149 kfree(rwq_ind_tbl);
5150 return 0;
5151}
5152
Yishai Hadas79b20a62016-05-23 15:20:50 +03005153int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
5154 u32 wq_attr_mask, struct ib_udata *udata)
5155{
5156 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5157 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5158 struct mlx5_ib_modify_wq ucmd = {};
5159 size_t required_cmd_sz;
5160 int curr_wq_state;
5161 int wq_state;
5162 int inlen;
5163 int err;
5164 void *rqc;
5165 void *in;
5166
5167 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
5168 if (udata->inlen < required_cmd_sz)
5169 return -EINVAL;
5170
5171 if (udata->inlen > sizeof(ucmd) &&
5172 !ib_is_udata_cleared(udata, sizeof(ucmd),
5173 udata->inlen - sizeof(ucmd)))
5174 return -EOPNOTSUPP;
5175
5176 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
5177 return -EFAULT;
5178
5179 if (ucmd.comp_mask || ucmd.reserved)
5180 return -EOPNOTSUPP;
5181
5182 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005183 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005184 if (!in)
5185 return -ENOMEM;
5186
5187 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
5188
5189 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
5190 wq_attr->curr_wq_state : wq->state;
5191 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
5192 wq_attr->wq_state : curr_wq_state;
5193 if (curr_wq_state == IB_WQS_ERR)
5194 curr_wq_state = MLX5_RQC_STATE_ERR;
5195 if (wq_state == IB_WQS_ERR)
5196 wq_state = MLX5_RQC_STATE_ERR;
5197 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
Yishai Hadas34d57582018-09-20 21:39:21 +03005198 MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005199 MLX5_SET(rqc, rqc, state, wq_state);
5200
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005201 if (wq_attr_mask & IB_WQ_FLAGS) {
5202 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
5203 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
5204 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
5205 mlx5_ib_dbg(dev, "VLAN offloads are not "
5206 "supported\n");
5207 err = -EOPNOTSUPP;
5208 goto out;
5209 }
5210 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5211 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
5212 MLX5_SET(rqc, rqc, vsd,
5213 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
5214 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005215
5216 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5217 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
5218 err = -EOPNOTSUPP;
5219 goto out;
5220 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005221 }
5222
Majd Dibbiny23a69642017-01-18 15:25:10 +02005223 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03005224 u16 set_id;
5225
5226 set_id = mlx5_ib_get_counters_id(dev, 0);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005227 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
5228 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5229 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandit3e1f0002019-07-23 10:31:17 +03005230 MLX5_SET(rqc, rqc, counter_set_id, set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005231 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06005232 dev_info_once(
5233 &dev->ib_dev.dev,
5234 "Receive WQ counters are not supported on current FW\n");
Majd Dibbiny23a69642017-01-18 15:25:10 +02005235 }
5236
Leon Romanovskye0b4b472020-04-09 21:03:33 +03005237 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005238 if (!err)
5239 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
5240
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005241out:
5242 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005243 return err;
5244}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005245
5246struct mlx5_ib_drain_cqe {
5247 struct ib_cqe cqe;
5248 struct completion done;
5249};
5250
5251static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
5252{
5253 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
5254 struct mlx5_ib_drain_cqe,
5255 cqe);
5256
5257 complete(&cqe->done);
5258}
5259
5260/* This function returns only once the drained WR was completed */
5261static void handle_drain_completion(struct ib_cq *cq,
5262 struct mlx5_ib_drain_cqe *sdrain,
5263 struct mlx5_ib_dev *dev)
5264{
5265 struct mlx5_core_dev *mdev = dev->mdev;
5266
5267 if (cq->poll_ctx == IB_POLL_DIRECT) {
5268 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
5269 ib_process_cq_direct(cq, -1);
5270 return;
5271 }
5272
5273 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5274 struct mlx5_ib_cq *mcq = to_mcq(cq);
5275 bool triggered = false;
5276 unsigned long flags;
5277
5278 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
5279 /* Make sure that the CQ handler won't run if wasn't run yet */
5280 if (!mcq->mcq.reset_notify_added)
5281 mcq->mcq.reset_notify_added = 1;
5282 else
5283 triggered = true;
5284 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
5285
5286 if (triggered) {
5287 /* Wait for any scheduled/running task to be ended */
5288 switch (cq->poll_ctx) {
5289 case IB_POLL_SOFTIRQ:
5290 irq_poll_disable(&cq->iop);
5291 irq_poll_enable(&cq->iop);
5292 break;
5293 case IB_POLL_WORKQUEUE:
5294 cancel_work_sync(&cq->work);
5295 break;
5296 default:
5297 WARN_ON_ONCE(1);
5298 }
5299 }
5300
5301 /* Run the CQ handler - this makes sure that the drain WR will
5302 * be processed if wasn't processed yet.
5303 */
Yishai Hadas4e0e2ea2019-06-30 19:23:27 +03005304 mcq->mcq.comp(&mcq->mcq, NULL);
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005305 }
5306
5307 wait_for_completion(&sdrain->done);
5308}
5309
5310void mlx5_ib_drain_sq(struct ib_qp *qp)
5311{
5312 struct ib_cq *cq = qp->send_cq;
5313 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
5314 struct mlx5_ib_drain_cqe sdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005315 const struct ib_send_wr *bad_swr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005316 struct ib_rdma_wr swr = {
5317 .wr = {
5318 .next = NULL,
5319 { .wr_cqe = &sdrain.cqe, },
5320 .opcode = IB_WR_RDMA_WRITE,
5321 },
5322 };
5323 int ret;
5324 struct mlx5_ib_dev *dev = to_mdev(qp->device);
5325 struct mlx5_core_dev *mdev = dev->mdev;
5326
5327 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
5328 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5329 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
5330 return;
5331 }
5332
5333 sdrain.cqe.done = mlx5_ib_drain_qp_done;
5334 init_completion(&sdrain.done);
5335
Leon Romanovsky029e88f2020-05-06 09:55:13 +03005336 ret = mlx5_ib_post_send_drain(qp, &swr.wr, &bad_swr);
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005337 if (ret) {
5338 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
5339 return;
5340 }
5341
5342 handle_drain_completion(cq, &sdrain, dev);
5343}
5344
5345void mlx5_ib_drain_rq(struct ib_qp *qp)
5346{
5347 struct ib_cq *cq = qp->recv_cq;
5348 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
5349 struct mlx5_ib_drain_cqe rdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005350 struct ib_recv_wr rwr = {};
5351 const struct ib_recv_wr *bad_rwr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005352 int ret;
5353 struct mlx5_ib_dev *dev = to_mdev(qp->device);
5354 struct mlx5_core_dev *mdev = dev->mdev;
5355
5356 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
5357 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5358 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
5359 return;
5360 }
5361
5362 rwr.wr_cqe = &rdrain.cqe;
5363 rdrain.cqe.done = mlx5_ib_drain_qp_done;
5364 init_completion(&rdrain.done);
5365
Leon Romanovsky029e88f2020-05-06 09:55:13 +03005366 ret = mlx5_ib_post_recv_drain(qp, &rwr, &bad_rwr);
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005367 if (ret) {
5368 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
5369 return;
5370 }
5371
5372 handle_drain_completion(cq, &rdrain, dev);
5373}
Mark Zhangd14133d2019-07-02 13:02:36 +03005374
5375/**
5376 * Bind a qp to a counter. If @counter is NULL then bind the qp to
5377 * the default counter
5378 */
5379int mlx5_ib_qp_set_counter(struct ib_qp *qp, struct rdma_counter *counter)
5380{
Mark Zhang10189e82020-01-26 19:17:08 +02005381 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Mark Zhangd14133d2019-07-02 13:02:36 +03005382 struct mlx5_ib_qp *mqp = to_mqp(qp);
5383 int err = 0;
5384
5385 mutex_lock(&mqp->mutex);
5386 if (mqp->state == IB_QPS_RESET) {
5387 qp->counter = counter;
5388 goto out;
5389 }
5390
Mark Zhang10189e82020-01-26 19:17:08 +02005391 if (!MLX5_CAP_GEN(dev->mdev, rts2rts_qp_counters_set_id)) {
5392 err = -EOPNOTSUPP;
5393 goto out;
5394 }
5395
Mark Zhangd14133d2019-07-02 13:02:36 +03005396 if (mqp->state == IB_QPS_RTS) {
5397 err = __mlx5_ib_qp_set_counter(qp, counter);
5398 if (!err)
5399 qp->counter = counter;
5400
5401 goto out;
5402 }
5403
5404 mqp->counter_pending = 1;
5405 qp->counter = counter;
5406
5407out:
5408 mutex_unlock(&mqp->mutex);
5409 return err;
5410}