blob: 3ecd1864b3c885007daa327f2d2652a39433770f [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/module.h>
34#include <rdma/ib_umem.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020035#include <rdma/ib_cache.h>
Haggai Abramovskycfb5e082016-01-14 19:12:57 +020036#include <rdma/ib_user_verbs.h>
Mark Zhangd14133d2019-07-02 13:02:36 +030037#include <rdma/rdma_counter.h>
Yishai Hadasc2e53b22017-06-08 16:15:08 +030038#include <linux/mlx5/fs.h>
Eli Cohene126ba92013-07-07 17:25:49 +030039#include "mlx5_ib.h"
Mark Blochb96c9dd2018-01-29 10:40:37 +000040#include "ib_rep.h"
Yishai Hadas443c1cf2018-09-20 21:39:26 +030041#include "cmd.h"
Leon Romanovsky333fbaa2020-04-04 10:40:24 +030042#include "qp.h"
Eli Cohene126ba92013-07-07 17:25:49 +030043
44/* not supported currently */
45static int wq_signature;
46
47enum {
48 MLX5_IB_ACK_REQ_FREQ = 8,
49};
50
51enum {
52 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
53 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
54 MLX5_IB_LINK_TYPE_IB = 0,
55 MLX5_IB_LINK_TYPE_ETH = 1
56};
57
58enum {
59 MLX5_IB_SQ_STRIDE = 6,
Idan Burstein064e5262018-05-02 13:16:39 +030060 MLX5_IB_SQ_UMR_INLINE_THRESHOLD = 64,
Eli Cohene126ba92013-07-07 17:25:49 +030061};
62
63static const u32 mlx5_ib_opcode[] = {
64 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020065 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030066 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
67 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
68 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
69 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
70 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
71 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
72 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
73 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030074 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030075 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
76 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
77 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
78};
79
Erez Shitritf0313962016-02-21 16:27:17 +020080struct mlx5_wqe_eth_pad {
81 u8 rsvd0[16];
82};
Eli Cohene126ba92013-07-07 17:25:49 +030083
Alex Veskereb49ab02016-08-28 12:25:53 +030084enum raw_qp_set_mask_map {
85 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020086 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030087};
88
Alex Vesker0680efa2016-08-28 12:25:52 +030089struct mlx5_modify_raw_qp_param {
90 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030091
92 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang61147f32018-03-19 15:10:30 +020093
94 struct mlx5_rate_limit rl;
95
Alex Veskereb49ab02016-08-28 12:25:53 +030096 u8 rq_q_ctr_id;
Mark Blochd5ed8ac2019-03-28 15:27:38 +020097 u16 port;
Alex Vesker0680efa2016-08-28 12:25:52 +030098};
99
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300100static void get_cqs(enum ib_qp_type qp_type,
101 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
102 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
103
Eli Cohene126ba92013-07-07 17:25:49 +0300104static int is_qp0(enum ib_qp_type qp_type)
105{
106 return qp_type == IB_QPT_SMI;
107}
108
Eli Cohene126ba92013-07-07 17:25:49 +0300109static int is_sqp(enum ib_qp_type qp_type)
110{
111 return is_qp0(qp_type) || is_qp1(qp_type);
112}
113
Haggai Eranc1395a22014-12-11 17:04:14 +0200114/**
Moni Shouafbeb4072019-01-22 08:48:46 +0200115 * mlx5_ib_read_user_wqe_common() - Copy a WQE (or part of) from user WQ
116 * to kernel buffer
Haggai Eranc1395a22014-12-11 17:04:14 +0200117 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200118 * @umem: User space memory where the WQ is
119 * @buffer: buffer to copy to
120 * @buflen: buffer length
121 * @wqe_index: index of WQE to copy from
122 * @wq_offset: offset to start of WQ
123 * @wq_wqe_cnt: number of WQEs in WQ
124 * @wq_wqe_shift: log2 of WQE size
125 * @bcnt: number of bytes to copy
126 * @bytes_copied: number of bytes to copy (return value)
Haggai Eranc1395a22014-12-11 17:04:14 +0200127 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200128 * Copies from start of WQE bcnt or less bytes.
129 * Does not gurantee to copy the entire WQE.
Haggai Eranc1395a22014-12-11 17:04:14 +0200130 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200131 * Return: zero on success, or an error code.
Haggai Eranc1395a22014-12-11 17:04:14 +0200132 */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200133static int mlx5_ib_read_user_wqe_common(struct ib_umem *umem, void *buffer,
134 size_t buflen, int wqe_index,
135 int wq_offset, int wq_wqe_cnt,
136 int wq_wqe_shift, int bcnt,
Moni Shouafbeb4072019-01-22 08:48:46 +0200137 size_t *bytes_copied)
Haggai Eranc1395a22014-12-11 17:04:14 +0200138{
Moni Shouafbeb4072019-01-22 08:48:46 +0200139 size_t offset = wq_offset + ((wqe_index % wq_wqe_cnt) << wq_wqe_shift);
140 size_t wq_end = wq_offset + (wq_wqe_cnt << wq_wqe_shift);
141 size_t copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200142 int ret;
143
Moni Shouafbeb4072019-01-22 08:48:46 +0200144 /* don't copy more than requested, more than buffer length or
145 * beyond WQ end
146 */
147 copy_length = min_t(u32, buflen, wq_end - offset);
148 copy_length = min_t(u32, copy_length, bcnt);
Haggai Eranc1395a22014-12-11 17:04:14 +0200149
Moni Shouafbeb4072019-01-22 08:48:46 +0200150 ret = ib_umem_copy_from(buffer, umem, offset, copy_length);
Haggai Eranc1395a22014-12-11 17:04:14 +0200151 if (ret)
152 return ret;
153
Moni Shouafbeb4072019-01-22 08:48:46 +0200154 if (!ret && bytes_copied)
155 *bytes_copied = copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200156
Moni Shouafbeb4072019-01-22 08:48:46 +0200157 return 0;
158}
Haggai Eranc1395a22014-12-11 17:04:14 +0200159
Moni Shouada9ee9d2020-01-15 14:43:34 +0200160static int mlx5_ib_read_kernel_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index,
161 void *buffer, size_t buflen, size_t *bc)
162{
163 struct mlx5_wqe_ctrl_seg *ctrl;
164 size_t bytes_copied = 0;
165 size_t wqe_length;
166 void *p;
167 int ds;
168
169 wqe_index = wqe_index & qp->sq.fbc.sz_m1;
170
171 /* read the control segment first */
172 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, wqe_index);
173 ctrl = p;
174 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
175 wqe_length = ds * MLX5_WQE_DS_UNITS;
176
177 /* read rest of WQE if it spreads over more than one stride */
178 while (bytes_copied < wqe_length) {
179 size_t copy_length =
180 min_t(size_t, buflen - bytes_copied, MLX5_SEND_WQE_BB);
181
182 if (!copy_length)
183 break;
184
185 memcpy(buffer + bytes_copied, p, copy_length);
186 bytes_copied += copy_length;
187
188 wqe_index = (wqe_index + 1) & qp->sq.fbc.sz_m1;
189 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, wqe_index);
190 }
191 *bc = bytes_copied;
192 return 0;
193}
194
195static int mlx5_ib_read_user_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index,
196 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200197{
198 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
199 struct ib_umem *umem = base->ubuffer.umem;
200 struct mlx5_ib_wq *wq = &qp->sq;
201 struct mlx5_wqe_ctrl_seg *ctrl;
202 size_t bytes_copied;
203 size_t bytes_copied2;
204 size_t wqe_length;
205 int ret;
206 int ds;
Haggai Eranc1395a22014-12-11 17:04:14 +0200207
Moni Shouafbeb4072019-01-22 08:48:46 +0200208 /* at first read as much as possible */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200209 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index,
210 wq->offset, wq->wqe_cnt,
211 wq->wqe_shift, buflen,
Moni Shouafbeb4072019-01-22 08:48:46 +0200212 &bytes_copied);
Haggai Eranc1395a22014-12-11 17:04:14 +0200213 if (ret)
214 return ret;
215
Moni Shouafbeb4072019-01-22 08:48:46 +0200216 /* we need at least control segment size to proceed */
217 if (bytes_copied < sizeof(*ctrl))
218 return -EINVAL;
219
220 ctrl = buffer;
221 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
222 wqe_length = ds * MLX5_WQE_DS_UNITS;
223
224 /* if we copied enough then we are done */
225 if (bytes_copied >= wqe_length) {
226 *bc = bytes_copied;
227 return 0;
228 }
229
230 /* otherwise this a wrapped around wqe
231 * so read the remaining bytes starting
232 * from wqe_index 0
233 */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200234 ret = mlx5_ib_read_user_wqe_common(umem, buffer + bytes_copied,
235 buflen - bytes_copied, 0, wq->offset,
236 wq->wqe_cnt, wq->wqe_shift,
Moni Shouafbeb4072019-01-22 08:48:46 +0200237 wqe_length - bytes_copied,
238 &bytes_copied2);
239
240 if (ret)
241 return ret;
242 *bc = bytes_copied + bytes_copied2;
243 return 0;
244}
245
Moni Shouada9ee9d2020-01-15 14:43:34 +0200246int mlx5_ib_read_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
247 size_t buflen, size_t *bc)
248{
249 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
250 struct ib_umem *umem = base->ubuffer.umem;
251
252 if (buflen < sizeof(struct mlx5_wqe_ctrl_seg))
253 return -EINVAL;
254
255 if (!umem)
256 return mlx5_ib_read_kernel_wqe_sq(qp, wqe_index, buffer,
257 buflen, bc);
258
259 return mlx5_ib_read_user_wqe_sq(qp, wqe_index, buffer, buflen, bc);
260}
261
262static int mlx5_ib_read_user_wqe_rq(struct mlx5_ib_qp *qp, int wqe_index,
263 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200264{
265 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
266 struct ib_umem *umem = base->ubuffer.umem;
267 struct mlx5_ib_wq *wq = &qp->rq;
268 size_t bytes_copied;
269 int ret;
270
Moni Shouada9ee9d2020-01-15 14:43:34 +0200271 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index,
272 wq->offset, wq->wqe_cnt,
273 wq->wqe_shift, buflen,
Moni Shouafbeb4072019-01-22 08:48:46 +0200274 &bytes_copied);
275
276 if (ret)
277 return ret;
278 *bc = bytes_copied;
279 return 0;
280}
281
Moni Shouada9ee9d2020-01-15 14:43:34 +0200282int mlx5_ib_read_wqe_rq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
283 size_t buflen, size_t *bc)
284{
285 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
286 struct ib_umem *umem = base->ubuffer.umem;
287 struct mlx5_ib_wq *wq = &qp->rq;
288 size_t wqe_size = 1 << wq->wqe_shift;
289
290 if (buflen < wqe_size)
291 return -EINVAL;
292
293 if (!umem)
294 return -EOPNOTSUPP;
295
296 return mlx5_ib_read_user_wqe_rq(qp, wqe_index, buffer, buflen, bc);
297}
298
299static int mlx5_ib_read_user_wqe_srq(struct mlx5_ib_srq *srq, int wqe_index,
300 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200301{
302 struct ib_umem *umem = srq->umem;
303 size_t bytes_copied;
304 int ret;
305
Moni Shouada9ee9d2020-01-15 14:43:34 +0200306 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index, 0,
307 srq->msrq.max, srq->msrq.wqe_shift,
308 buflen, &bytes_copied);
Moni Shouafbeb4072019-01-22 08:48:46 +0200309
310 if (ret)
311 return ret;
312 *bc = bytes_copied;
313 return 0;
Haggai Eranc1395a22014-12-11 17:04:14 +0200314}
315
Moni Shouada9ee9d2020-01-15 14:43:34 +0200316int mlx5_ib_read_wqe_srq(struct mlx5_ib_srq *srq, int wqe_index, void *buffer,
317 size_t buflen, size_t *bc)
318{
319 struct ib_umem *umem = srq->umem;
320 size_t wqe_size = 1 << srq->msrq.wqe_shift;
321
322 if (buflen < wqe_size)
323 return -EINVAL;
324
325 if (!umem)
326 return -EOPNOTSUPP;
327
328 return mlx5_ib_read_user_wqe_srq(srq, wqe_index, buffer, buflen, bc);
329}
330
Eli Cohene126ba92013-07-07 17:25:49 +0300331static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
332{
333 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
334 struct ib_event event;
335
majd@mellanox.com19098df2016-01-14 19:13:03 +0200336 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
337 /* This event is only valid for trans_qps */
338 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
339 }
Eli Cohene126ba92013-07-07 17:25:49 +0300340
341 if (ibqp->event_handler) {
342 event.device = ibqp->device;
343 event.element.qp = ibqp;
344 switch (type) {
345 case MLX5_EVENT_TYPE_PATH_MIG:
346 event.event = IB_EVENT_PATH_MIG;
347 break;
348 case MLX5_EVENT_TYPE_COMM_EST:
349 event.event = IB_EVENT_COMM_EST;
350 break;
351 case MLX5_EVENT_TYPE_SQ_DRAINED:
352 event.event = IB_EVENT_SQ_DRAINED;
353 break;
354 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
355 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
356 break;
357 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
358 event.event = IB_EVENT_QP_FATAL;
359 break;
360 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
361 event.event = IB_EVENT_PATH_MIG_ERR;
362 break;
363 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
364 event.event = IB_EVENT_QP_REQ_ERR;
365 break;
366 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
367 event.event = IB_EVENT_QP_ACCESS_ERR;
368 break;
369 default:
370 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
371 return;
372 }
373
374 ibqp->event_handler(&event, ibqp->qp_context);
375 }
376}
377
378static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
379 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
380{
381 int wqe_size;
382 int wq_size;
383
384 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300385 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300386 return -EINVAL;
387
388 if (!has_rq) {
389 qp->rq.max_gs = 0;
390 qp->rq.wqe_cnt = 0;
391 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300392 cap->max_recv_wr = 0;
393 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300394 } else {
395 if (ucmd) {
396 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300397 if (ucmd->rq_wqe_shift > BITS_PER_BYTE * sizeof(ucmd->rq_wqe_shift))
398 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300399 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300400 if ((1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) < qp->wq_sig)
401 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300402 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
403 qp->rq.max_post = qp->rq.wqe_cnt;
404 } else {
405 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
406 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
407 wqe_size = roundup_pow_of_two(wqe_size);
408 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
409 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
410 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300411 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300412 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
413 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300414 MLX5_CAP_GEN(dev->mdev,
415 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300416 return -EINVAL;
417 }
418 qp->rq.wqe_shift = ilog2(wqe_size);
419 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
420 qp->rq.max_post = qp->rq.wqe_cnt;
421 }
422 }
423
424 return 0;
425}
426
Erez Shitritf0313962016-02-21 16:27:17 +0200427static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300428{
Andi Shyti618af382013-07-16 15:35:01 +0200429 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300430
Erez Shitritf0313962016-02-21 16:27:17 +0200431 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300432 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300433 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300434 /* fall through */
435 case IB_QPT_RC:
436 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200437 max(sizeof(struct mlx5_wqe_atomic_seg) +
438 sizeof(struct mlx5_wqe_raddr_seg),
439 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
Idan Burstein064e5262018-05-02 13:16:39 +0300440 sizeof(struct mlx5_mkey_seg) +
441 MLX5_IB_SQ_UMR_INLINE_THRESHOLD /
442 MLX5_IB_UMR_OCTOWORD);
Eli Cohene126ba92013-07-07 17:25:49 +0300443 break;
444
Eli Cohenb125a542013-09-11 16:35:22 +0300445 case IB_QPT_XRC_TGT:
446 return 0;
447
Eli Cohene126ba92013-07-07 17:25:49 +0300448 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300449 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200450 max(sizeof(struct mlx5_wqe_raddr_seg),
451 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
452 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300453 break;
454
455 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200456 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
457 size += sizeof(struct mlx5_wqe_eth_pad) +
458 sizeof(struct mlx5_wqe_eth_seg);
459 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300460 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200461 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300462 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300463 sizeof(struct mlx5_wqe_datagram_seg);
464 break;
465
466 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300467 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300468 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
469 sizeof(struct mlx5_mkey_seg);
470 break;
471
472 default:
473 return -EINVAL;
474 }
475
476 return size;
477}
478
479static int calc_send_wqe(struct ib_qp_init_attr *attr)
480{
481 int inl_size = 0;
482 int size;
483
Erez Shitritf0313962016-02-21 16:27:17 +0200484 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300485 if (size < 0)
486 return size;
487
488 if (attr->cap.max_inline_data) {
489 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
490 attr->cap.max_inline_data;
491 }
492
493 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300494 if (attr->create_flags & IB_QP_CREATE_INTEGRITY_EN &&
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200495 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300496 return MLX5_SIG_WQE_SIZE;
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200497 else
498 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300499}
500
Eli Cohen288c01b2016-10-27 16:36:45 +0300501static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
502{
503 int max_sge;
504
505 if (attr->qp_type == IB_QPT_RC)
506 max_sge = (min_t(int, wqe_size, 512) -
507 sizeof(struct mlx5_wqe_ctrl_seg) -
508 sizeof(struct mlx5_wqe_raddr_seg)) /
509 sizeof(struct mlx5_wqe_data_seg);
510 else if (attr->qp_type == IB_QPT_XRC_INI)
511 max_sge = (min_t(int, wqe_size, 512) -
512 sizeof(struct mlx5_wqe_ctrl_seg) -
513 sizeof(struct mlx5_wqe_xrc_seg) -
514 sizeof(struct mlx5_wqe_raddr_seg)) /
515 sizeof(struct mlx5_wqe_data_seg);
516 else
517 max_sge = (wqe_size - sq_overhead(attr)) /
518 sizeof(struct mlx5_wqe_data_seg);
519
520 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
521 sizeof(struct mlx5_wqe_data_seg));
522}
523
Eli Cohene126ba92013-07-07 17:25:49 +0300524static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
525 struct mlx5_ib_qp *qp)
526{
527 int wqe_size;
528 int wq_size;
529
530 if (!attr->cap.max_send_wr)
531 return 0;
532
533 wqe_size = calc_send_wqe(attr);
534 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
535 if (wqe_size < 0)
536 return wqe_size;
537
Saeed Mahameed938fe832015-05-28 22:28:41 +0300538 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300539 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300540 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300541 return -EINVAL;
542 }
543
Erez Shitritf0313962016-02-21 16:27:17 +0200544 qp->max_inline_data = wqe_size - sq_overhead(attr) -
545 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300546 attr->cap.max_inline_data = qp->max_inline_data;
547
548 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
549 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300550 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800551 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
552 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300553 qp->sq.wqe_cnt,
554 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300555 return -ENOMEM;
556 }
Eli Cohene126ba92013-07-07 17:25:49 +0300557 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300558 qp->sq.max_gs = get_send_sge(attr, wqe_size);
559 if (qp->sq.max_gs < attr->cap.max_send_sge)
560 return -ENOMEM;
561
562 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300563 qp->sq.max_post = wq_size / wqe_size;
564 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300565
566 return wq_size;
567}
568
569static int set_user_buf_size(struct mlx5_ib_dev *dev,
570 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200571 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200572 struct mlx5_ib_qp_base *base,
573 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300574{
575 int desc_sz = 1 << qp->sq.wqe_shift;
576
Saeed Mahameed938fe832015-05-28 22:28:41 +0300577 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300578 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300579 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300580 return -EINVAL;
581 }
582
Gal Pressmanaf8b38e2019-02-06 15:45:35 +0200583 if (ucmd->sq_wqe_count && !is_power_of_2(ucmd->sq_wqe_count)) {
584 mlx5_ib_warn(dev, "sq_wqe_count %d is not a power of two\n",
585 ucmd->sq_wqe_count);
Eli Cohene126ba92013-07-07 17:25:49 +0300586 return -EINVAL;
587 }
588
589 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
590
Saeed Mahameed938fe832015-05-28 22:28:41 +0300591 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300592 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300593 qp->sq.wqe_cnt,
594 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300595 return -EINVAL;
596 }
597
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300598 if (attr->qp_type == IB_QPT_RAW_PACKET ||
599 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200600 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
601 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
602 } else {
603 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
604 (qp->sq.wqe_cnt << 6);
605 }
Eli Cohene126ba92013-07-07 17:25:49 +0300606
607 return 0;
608}
609
610static int qp_has_rq(struct ib_qp_init_attr *attr)
611{
612 if (attr->qp_type == IB_QPT_XRC_INI ||
613 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
614 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
615 !attr->cap.max_recv_wr)
616 return 0;
617
618 return 1;
619}
620
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200621enum {
622 /* this is the first blue flame register in the array of bfregs assigned
623 * to a processes. Since we do not use it for blue flame but rather
624 * regular 64 bit doorbells, we do not need a lock for maintaiing
625 * "odd/even" order
626 */
627 NUM_NON_BLUE_FLAME_BFREGS = 1,
628};
629
Eli Cohenb037c292017-01-03 23:55:26 +0200630static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
631{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200632 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200633}
634
635static int num_med_bfreg(struct mlx5_ib_dev *dev,
636 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200637{
638 int n;
639
Eli Cohenb037c292017-01-03 23:55:26 +0200640 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
641 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200642
643 return n >= 0 ? n : 0;
644}
645
Yishai Hadas18b03622018-05-07 10:20:01 +0300646static int first_med_bfreg(struct mlx5_ib_dev *dev,
647 struct mlx5_bfreg_info *bfregi)
648{
649 return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
650}
651
Eli Cohenb037c292017-01-03 23:55:26 +0200652static int first_hi_bfreg(struct mlx5_ib_dev *dev,
653 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200654{
655 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200656
Eli Cohenb037c292017-01-03 23:55:26 +0200657 med = num_med_bfreg(dev, bfregi);
658 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200659}
660
Eli Cohenb037c292017-01-03 23:55:26 +0200661static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
662 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300663{
Eli Cohene126ba92013-07-07 17:25:49 +0300664 int i;
665
Eli Cohenb037c292017-01-03 23:55:26 +0200666 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
667 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200668 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300669 return i;
670 }
671 }
672
673 return -ENOMEM;
674}
675
Eli Cohenb037c292017-01-03 23:55:26 +0200676static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
677 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300678{
Yishai Hadas18b03622018-05-07 10:20:01 +0300679 int minidx = first_med_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300680 int i;
681
Yishai Hadas18b03622018-05-07 10:20:01 +0300682 if (minidx < 0)
683 return minidx;
684
685 for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200686 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300687 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200688 if (!bfregi->count[minidx])
689 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300690 }
691
Eli Cohen2f5ff262017-01-03 23:55:21 +0200692 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300693 return minidx;
694}
695
Eli Cohenb037c292017-01-03 23:55:26 +0200696static int alloc_bfreg(struct mlx5_ib_dev *dev,
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300697 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300698{
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300699 int bfregn = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +0300700
Yishai Hadas0a2fd012020-03-24 08:01:43 +0200701 if (bfregi->lib_uar_dyn)
702 return -EINVAL;
703
Eli Cohen2f5ff262017-01-03 23:55:21 +0200704 mutex_lock(&bfregi->lock);
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300705 if (bfregi->ver >= 2) {
706 bfregn = alloc_high_class_bfreg(dev, bfregi);
707 if (bfregn < 0)
708 bfregn = alloc_med_class_bfreg(dev, bfregi);
709 }
710
711 if (bfregn < 0) {
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200712 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200713 bfregn = 0;
714 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300715 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200716 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300717
Eli Cohen2f5ff262017-01-03 23:55:21 +0200718 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300719}
720
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200721void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300722{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200723 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200724 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200725 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300726}
727
728static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
729{
730 switch (state) {
731 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
732 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
733 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
734 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
735 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
736 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
737 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
738 default: return -1;
739 }
740}
741
742static int to_mlx5_st(enum ib_qp_type type)
743{
744 switch (type) {
745 case IB_QPT_RC: return MLX5_QP_ST_RC;
746 case IB_QPT_UC: return MLX5_QP_ST_UC;
747 case IB_QPT_UD: return MLX5_QP_ST_UD;
748 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
749 case IB_QPT_XRC_INI:
750 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
751 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200752 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200753 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300754 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300755 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200756 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300757 case IB_QPT_MAX:
758 default: return -EINVAL;
759 }
760}
761
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300762static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
763 struct mlx5_ib_cq *recv_cq);
764static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
765 struct mlx5_ib_cq *recv_cq);
766
Yishai Hadas7c043e92018-06-17 13:00:03 +0300767int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300768 struct mlx5_bfreg_info *bfregi, u32 bfregn,
Yishai Hadas7c043e92018-06-17 13:00:03 +0300769 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300770{
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300771 unsigned int bfregs_per_sys_page;
772 u32 index_of_sys_page;
773 u32 offset;
Eli Cohenb037c292017-01-03 23:55:26 +0200774
Yishai Hadas0a2fd012020-03-24 08:01:43 +0200775 if (bfregi->lib_uar_dyn)
776 return -EINVAL;
777
Eli Cohenb037c292017-01-03 23:55:26 +0200778 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
779 MLX5_NON_FP_BFREGS_PER_UAR;
780 index_of_sys_page = bfregn / bfregs_per_sys_page;
781
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200782 if (dyn_bfreg) {
783 index_of_sys_page += bfregi->num_static_sys_pages;
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300784
785 if (index_of_sys_page >= bfregi->num_sys_pages)
786 return -EINVAL;
787
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200788 if (bfregn > bfregi->num_dyn_bfregs ||
789 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
790 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
791 return -EINVAL;
792 }
793 }
Eli Cohenb037c292017-01-03 23:55:26 +0200794
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200795 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200796 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300797}
798
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200799static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200800 unsigned long addr, size_t size,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200801 struct ib_umem **umem, int *npages, int *page_shift,
802 int *ncont, u32 *offset)
majd@mellanox.com19098df2016-01-14 19:13:03 +0200803{
804 int err;
805
Moni Shouac320e522020-01-15 14:43:31 +0200806 *umem = ib_umem_get(&dev->ib_dev, addr, size, 0);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200807 if (IS_ERR(*umem)) {
808 mlx5_ib_dbg(dev, "umem_get failed\n");
809 return PTR_ERR(*umem);
810 }
811
Majd Dibbiny762f8992016-10-27 16:36:47 +0300812 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200813
814 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
815 if (err) {
816 mlx5_ib_warn(dev, "bad offset\n");
817 goto err_umem;
818 }
819
820 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
821 addr, size, *npages, *page_shift, *ncont, *offset);
822
823 return 0;
824
825err_umem:
826 ib_umem_release(*umem);
827 *umem = NULL;
828
829 return err;
830}
831
Maor Gottliebfe248c32017-05-30 10:29:14 +0300832static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300833 struct mlx5_ib_rwq *rwq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300834{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300835 struct mlx5_ib_ucontext *context =
836 rdma_udata_to_drv_context(
837 udata,
838 struct mlx5_ib_ucontext,
839 ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300840
Maor Gottliebfe248c32017-05-30 10:29:14 +0300841 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
842 atomic_dec(&dev->delay_drop.rqs_cnt);
843
Yishai Hadas79b20a62016-05-23 15:20:50 +0300844 mlx5_ib_db_unmap_user(context, &rwq->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300845 ib_umem_release(rwq->umem);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300846}
847
848static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200849 struct ib_udata *udata, struct mlx5_ib_rwq *rwq,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300850 struct mlx5_ib_create_wq *ucmd)
851{
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200852 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
853 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300854 int page_shift = 0;
855 int npages;
856 u32 offset = 0;
857 int ncont = 0;
858 int err;
859
860 if (!ucmd->buf_addr)
861 return -EINVAL;
862
Moni Shouac320e522020-01-15 14:43:31 +0200863 rwq->umem = ib_umem_get(&dev->ib_dev, ucmd->buf_addr, rwq->buf_size, 0);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300864 if (IS_ERR(rwq->umem)) {
865 mlx5_ib_dbg(dev, "umem_get failed\n");
866 err = PTR_ERR(rwq->umem);
867 return err;
868 }
869
Majd Dibbiny762f8992016-10-27 16:36:47 +0300870 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300871 &ncont, NULL);
872 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
873 &rwq->rq_page_offset);
874 if (err) {
875 mlx5_ib_warn(dev, "bad offset\n");
876 goto err_umem;
877 }
878
879 rwq->rq_num_pas = ncont;
880 rwq->page_shift = page_shift;
881 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
882 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
883
884 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
885 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
886 npages, page_shift, ncont, offset);
887
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200888 err = mlx5_ib_db_map_user(ucontext, udata, ucmd->db_addr, &rwq->db);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300889 if (err) {
890 mlx5_ib_dbg(dev, "map failed\n");
891 goto err_umem;
892 }
893
894 rwq->create_type = MLX5_WQ_USER;
895 return 0;
896
897err_umem:
898 ib_umem_release(rwq->umem);
899 return err;
900}
901
Eli Cohenb037c292017-01-03 23:55:26 +0200902static int adjust_bfregn(struct mlx5_ib_dev *dev,
903 struct mlx5_bfreg_info *bfregi, int bfregn)
904{
905 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
906 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
907}
908
Eli Cohene126ba92013-07-07 17:25:49 +0300909static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
910 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200911 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300912 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200913 struct mlx5_ib_create_qp_resp *resp, int *inlen,
914 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300915{
916 struct mlx5_ib_ucontext *context;
917 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200918 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200919 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200920 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300921 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200922 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200923 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200924 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300925 __be64 *pas;
926 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300927 int err;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200928 u16 uid;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200929 u32 uar_flags;
Eli Cohene126ba92013-07-07 17:25:49 +0300930
931 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
932 if (err) {
933 mlx5_ib_dbg(dev, "copy failed\n");
934 return err;
935 }
936
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200937 context = rdma_udata_to_drv_context(udata, struct mlx5_ib_ucontext,
938 ibucontext);
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200939 uar_flags = ucmd.flags & (MLX5_QP_FLAG_UAR_PAGE_INDEX |
940 MLX5_QP_FLAG_BFREG_INDEX);
941 switch (uar_flags) {
942 case MLX5_QP_FLAG_UAR_PAGE_INDEX:
943 uar_index = ucmd.bfreg_index;
944 bfregn = MLX5_IB_INVALID_BFREG;
945 break;
946 case MLX5_QP_FLAG_BFREG_INDEX:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200947 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
948 ucmd.bfreg_index, true);
949 if (uar_index < 0)
950 return uar_index;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200951 bfregn = MLX5_IB_INVALID_BFREG;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200952 break;
953 case 0:
954 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
955 return -EINVAL;
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300956 bfregn = alloc_bfreg(dev, &context->bfregi);
957 if (bfregn < 0)
958 return bfregn;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200959 break;
960 default:
961 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300962 }
963
Eli Cohen2f5ff262017-01-03 23:55:21 +0200964 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200965 if (bfregn != MLX5_IB_INVALID_BFREG)
966 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
967 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300968
Haggai Eran48fea832014-05-22 14:50:11 +0300969 qp->rq.offset = 0;
970 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
971 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
972
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200973 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300974 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200975 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300976
majd@mellanox.com19098df2016-01-14 19:13:03 +0200977 if (ucmd.buf_addr && ubuffer->buf_size) {
978 ubuffer->buf_addr = ucmd.buf_addr;
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200979 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr,
980 ubuffer->buf_size, &ubuffer->umem,
981 &npages, &page_shift, &ncont, &offset);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200982 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200983 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200984 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200985 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300986 }
Eli Cohene126ba92013-07-07 17:25:49 +0300987
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300988 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
989 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300990 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300991 if (!*in) {
992 err = -ENOMEM;
993 goto err_umem;
994 }
Eli Cohene126ba92013-07-07 17:25:49 +0300995
Yishai Hadas7422edc2018-12-23 13:12:21 +0200996 uid = (attr->qp_type != IB_QPT_XRC_TGT &&
997 attr->qp_type != IB_QPT_XRC_INI) ? to_mpd(pd)->uid : 0;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200998 MLX5_SET(create_qp_in, *in, uid, uid);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300999 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
1000 if (ubuffer->umem)
1001 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
1002
1003 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1004
1005 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1006 MLX5_SET(qpc, qpc, page_offset, offset);
1007
1008 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001009 if (bfregn != MLX5_IB_INVALID_BFREG)
1010 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
1011 else
1012 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001013 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +03001014
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001015 err = mlx5_ib_db_map_user(context, udata, ucmd.db_addr, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001016 if (err) {
1017 mlx5_ib_dbg(dev, "map failed\n");
1018 goto err_free;
1019 }
1020
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001021 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
Eli Cohene126ba92013-07-07 17:25:49 +03001022 if (err) {
1023 mlx5_ib_dbg(dev, "copy failed\n");
1024 goto err_unmap;
1025 }
1026 qp->create_type = MLX5_QP_USER;
1027
1028 return 0;
1029
1030err_unmap:
1031 mlx5_ib_db_unmap_user(context, &qp->db);
1032
1033err_free:
Al Viro479163f2014-11-20 08:13:57 +00001034 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001035
1036err_umem:
Leon Romanovsky836a0fb2019-06-16 15:05:20 +03001037 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +03001038
Eli Cohen2f5ff262017-01-03 23:55:21 +02001039err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001040 if (bfregn != MLX5_IB_INVALID_BFREG)
1041 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001042 return err;
1043}
1044
Eli Cohenb037c292017-01-03 23:55:26 +02001045static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03001046 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base,
1047 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03001048{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03001049 struct mlx5_ib_ucontext *context =
1050 rdma_udata_to_drv_context(
1051 udata,
1052 struct mlx5_ib_ucontext,
1053 ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03001054
Eli Cohene126ba92013-07-07 17:25:49 +03001055 mlx5_ib_db_unmap_user(context, &qp->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +03001056 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001057
1058 /*
1059 * Free only the BFREGs which are handled by the kernel.
1060 * BFREGs of UARs allocated dynamically are handled by user.
1061 */
1062 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
1063 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001064}
1065
Guy Levi34f4c952018-11-26 08:15:50 +02001066/* get_sq_edge - Get the next nearby edge.
1067 *
1068 * An 'edge' is defined as the first following address after the end
1069 * of the fragment or the SQ. Accordingly, during the WQE construction
1070 * which repetitively increases the pointer to write the next data, it
1071 * simply should check if it gets to an edge.
1072 *
1073 * @sq - SQ buffer.
1074 * @idx - Stride index in the SQ buffer.
1075 *
1076 * Return:
1077 * The new edge.
1078 */
1079static void *get_sq_edge(struct mlx5_ib_wq *sq, u32 idx)
1080{
1081 void *fragment_end;
1082
1083 fragment_end = mlx5_frag_buf_get_wqe
1084 (&sq->fbc,
1085 mlx5_frag_buf_get_idx_last_contig_stride(&sq->fbc, idx));
1086
1087 return fragment_end + MLX5_SEND_WQE_BB;
1088}
1089
Eli Cohene126ba92013-07-07 17:25:49 +03001090static int create_kernel_qp(struct mlx5_ib_dev *dev,
1091 struct ib_qp_init_attr *init_attr,
1092 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001093 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +02001094 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +03001095{
Eli Cohene126ba92013-07-07 17:25:49 +03001096 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001097 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +03001098 int err;
1099
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +03001100 if (init_attr->create_flags & ~(IB_QP_CREATE_INTEGRITY_EN |
Erez Shitritf0313962016-02-21 16:27:17 +02001101 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +02001102 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +03001103 IB_QP_CREATE_NETIF_QP |
Michael Guralnik11f552e2019-06-10 15:21:24 +03001104 MLX5_IB_QP_CREATE_SQPN_QP1 |
1105 MLX5_IB_QP_CREATE_WC_TEST))
Eli Cohen1a4c3a32014-02-06 17:41:25 +02001106 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001107
1108 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001109 qp->bf.bfreg = &dev->fp_bfreg;
Michael Guralnik11f552e2019-06-10 15:21:24 +03001110 else if (init_attr->create_flags & MLX5_IB_QP_CREATE_WC_TEST)
1111 qp->bf.bfreg = &dev->wc_bfreg;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001112 else
1113 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +03001114
Eli Cohend8030b02017-02-09 19:31:47 +02001115 /* We need to divide by two since each register is comprised of
1116 * two buffers of identical size, namely odd and even
1117 */
1118 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001119 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +03001120
1121 err = calc_sq_size(dev, init_attr, qp);
1122 if (err < 0) {
1123 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001124 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001125 }
1126
1127 qp->rq.offset = 0;
1128 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001129 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +03001130
Guy Levi34f4c952018-11-26 08:15:50 +02001131 err = mlx5_frag_buf_alloc_node(dev->mdev, base->ubuffer.buf_size,
1132 &qp->buf, dev->mdev->priv.numa_node);
Eli Cohene126ba92013-07-07 17:25:49 +03001133 if (err) {
1134 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001135 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001136 }
1137
Guy Levi34f4c952018-11-26 08:15:50 +02001138 if (qp->rq.wqe_cnt)
1139 mlx5_init_fbc(qp->buf.frags, qp->rq.wqe_shift,
1140 ilog2(qp->rq.wqe_cnt), &qp->rq.fbc);
1141
1142 if (qp->sq.wqe_cnt) {
1143 int sq_strides_offset = (qp->sq.offset & (PAGE_SIZE - 1)) /
1144 MLX5_SEND_WQE_BB;
1145 mlx5_init_fbc_offset(qp->buf.frags +
1146 (qp->sq.offset / PAGE_SIZE),
1147 ilog2(MLX5_SEND_WQE_BB),
1148 ilog2(qp->sq.wqe_cnt),
1149 sq_strides_offset, &qp->sq.fbc);
1150
1151 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
1152 }
1153
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001154 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
1155 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001156 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001157 if (!*in) {
1158 err = -ENOMEM;
1159 goto err_buf;
1160 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001161
1162 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1163 MLX5_SET(qpc, qpc, uar_page, uar_index);
1164 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1165
Eli Cohene126ba92013-07-07 17:25:49 +03001166 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001167 MLX5_SET(qpc, qpc, fre, 1);
1168 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001169
Michael Guralnik3f89b012019-10-20 09:43:59 +03001170 if (init_attr->create_flags & MLX5_IB_QP_CREATE_SQPN_QP1) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001171 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +02001172 qp->flags |= MLX5_IB_QP_SQPN_QP1;
1173 }
1174
Guy Levi34f4c952018-11-26 08:15:50 +02001175 mlx5_fill_page_frag_array(&qp->buf,
1176 (__be64 *)MLX5_ADDR_OF(create_qp_in,
1177 *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +03001178
Jack Morgenstein9603b612014-07-28 23:30:22 +03001179 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001180 if (err) {
1181 mlx5_ib_dbg(dev, "err %d\n", err);
1182 goto err_free;
1183 }
1184
Li Dongyangb5883002017-08-16 23:31:22 +10001185 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
1186 sizeof(*qp->sq.wrid), GFP_KERNEL);
1187 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1188 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1189 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1190 sizeof(*qp->rq.wrid), GFP_KERNEL);
1191 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1192 sizeof(*qp->sq.w_list), GFP_KERNEL);
1193 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1194 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001195
1196 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1197 !qp->sq.w_list || !qp->sq.wqe_head) {
1198 err = -ENOMEM;
1199 goto err_wrid;
1200 }
1201 qp->create_type = MLX5_QP_KERNEL;
1202
1203 return 0;
1204
1205err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001206 kvfree(qp->sq.wqe_head);
1207 kvfree(qp->sq.w_list);
1208 kvfree(qp->sq.wrid);
1209 kvfree(qp->sq.wr_data);
1210 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001211 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001212
1213err_free:
Al Viro479163f2014-11-20 08:13:57 +00001214 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001215
1216err_buf:
Guy Levi34f4c952018-11-26 08:15:50 +02001217 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001218 return err;
1219}
1220
1221static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1222{
Li Dongyangb5883002017-08-16 23:31:22 +10001223 kvfree(qp->sq.wqe_head);
1224 kvfree(qp->sq.w_list);
1225 kvfree(qp->sq.wrid);
1226 kvfree(qp->sq.wr_data);
1227 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001228 mlx5_db_free(dev->mdev, &qp->db);
Guy Levi34f4c952018-11-26 08:15:50 +02001229 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001230}
1231
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001232static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001233{
1234 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Moni Shouac32a4f22018-01-02 16:19:32 +02001235 (attr->qp_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001236 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001237 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001238 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001239 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001240 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001241 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001242}
1243
1244static int is_connected(enum ib_qp_type qp_type)
1245{
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001246 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC ||
1247 qp_type == MLX5_IB_QPT_DCI)
Eli Cohene126ba92013-07-07 17:25:49 +03001248 return 1;
1249
1250 return 0;
1251}
1252
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001253static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001254 struct mlx5_ib_qp *qp,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001255 struct mlx5_ib_sq *sq, u32 tdn,
1256 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001257{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001258 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001259 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1260
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001261 MLX5_SET(create_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001262 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001263 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1264 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1265
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001266 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1267}
1268
1269static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001270 struct mlx5_ib_sq *sq, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001271{
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001272 mlx5_cmd_destroy_tis(dev->mdev, sq->tisn, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001273}
1274
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001275static void destroy_flow_rule_vport_sq(struct mlx5_ib_sq *sq)
Mark Blochb96c9dd2018-01-29 10:40:37 +00001276{
1277 if (sq->flow_rule)
1278 mlx5_del_flow_rules(sq->flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001279 sq->flow_rule = NULL;
Mark Blochb96c9dd2018-01-29 10:40:37 +00001280}
1281
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001282static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001283 struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001284 struct mlx5_ib_sq *sq, void *qpin,
1285 struct ib_pd *pd)
1286{
1287 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1288 __be64 *pas;
1289 void *in;
1290 void *sqc;
1291 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1292 void *wq;
1293 int inlen;
1294 int err;
1295 int page_shift = 0;
1296 int npages;
1297 int ncont = 0;
1298 u32 offset = 0;
1299
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001300 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr, ubuffer->buf_size,
1301 &sq->ubuffer.umem, &npages, &page_shift, &ncont,
1302 &offset);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001303 if (err)
1304 return err;
1305
1306 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001307 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001308 if (!in) {
1309 err = -ENOMEM;
1310 goto err_umem;
1311 }
1312
Yishai Hadasc14003f2018-09-20 21:39:22 +03001313 MLX5_SET(create_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001314 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1315 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001316 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1317 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001318 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1319 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1320 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1321 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1322 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001323 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1324 MLX5_CAP_ETH(dev->mdev, swp))
1325 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001326
1327 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1328 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1329 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1330 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1331 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1332 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1333 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1334 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1335 MLX5_SET(wq, wq, page_offset, offset);
1336
1337 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1338 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1339
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001340 err = mlx5_core_create_sq_tracked(dev, in, inlen, &sq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001341
1342 kvfree(in);
1343
1344 if (err)
1345 goto err_umem;
1346
1347 return 0;
1348
1349err_umem:
1350 ib_umem_release(sq->ubuffer.umem);
1351 sq->ubuffer.umem = NULL;
1352
1353 return err;
1354}
1355
1356static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1357 struct mlx5_ib_sq *sq)
1358{
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001359 destroy_flow_rule_vport_sq(sq);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001360 mlx5_core_destroy_sq_tracked(dev, &sq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001361 ib_umem_release(sq->ubuffer.umem);
1362}
1363
Boris Pismenny2c292db2018-03-08 15:51:40 +02001364static size_t get_rq_pas_size(void *qpc)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001365{
1366 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1367 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1368 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1369 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1370 u32 po_quanta = 1 << (log_page_size - 6);
1371 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1372 u32 page_size = 1 << log_page_size;
1373 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1374 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1375
1376 return rq_num_pas * sizeof(u64);
1377}
1378
1379static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001380 struct mlx5_ib_rq *rq, void *qpin,
Yishai Hadas34d57582018-09-20 21:39:21 +03001381 size_t qpinlen, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001382{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001383 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001384 __be64 *pas;
1385 __be64 *qp_pas;
1386 void *in;
1387 void *rqc;
1388 void *wq;
1389 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001390 size_t rq_pas_size = get_rq_pas_size(qpc);
1391 size_t inlen;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001392 int err;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001393
1394 if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
1395 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001396
1397 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001398 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001399 if (!in)
1400 return -ENOMEM;
1401
Yishai Hadas34d57582018-09-20 21:39:21 +03001402 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001403 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001404 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1405 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001406 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1407 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1408 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1409 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1410 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1411
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001412 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1413 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1414
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001415 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1416 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001417 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1418 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001419 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1420 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1421 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1422 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1423 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1424 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1425
1426 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1427 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1428 memcpy(pas, qp_pas, rq_pas_size);
1429
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001430 err = mlx5_core_create_rq_tracked(dev, in, inlen, &rq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001431
1432 kvfree(in);
1433
1434 return err;
1435}
1436
1437static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1438 struct mlx5_ib_rq *rq)
1439{
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03001440 mlx5_core_destroy_rq_tracked(dev, &rq->base.mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001441}
1442
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001443static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1444{
1445 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1446 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1447 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1448}
1449
Mark Bloch0042f9e2018-09-17 13:30:49 +03001450static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1451 struct mlx5_ib_rq *rq,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001452 u32 qp_flags_en,
1453 struct ib_pd *pd)
Mark Bloch0042f9e2018-09-17 13:30:49 +03001454{
1455 if (qp_flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1456 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1457 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001458 mlx5_cmd_destroy_tir(dev->mdev, rq->tirn, to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001459}
1460
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001461static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001462 struct mlx5_ib_rq *rq, u32 tdn,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001463 u32 *qp_flags_en,
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001464 struct ib_pd *pd,
1465 u32 *out, int outlen)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001466{
Mark Bloch175edba2018-09-17 13:30:48 +03001467 u8 lb_flag = 0;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001468 u32 *in;
1469 void *tirc;
1470 int inlen;
1471 int err;
1472
1473 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001474 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001475 if (!in)
1476 return -ENOMEM;
1477
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001478 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001479 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1480 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1481 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1482 MLX5_SET(tirc, tirc, transport_domain, tdn);
Mark Bloch175edba2018-09-17 13:30:48 +03001483 if (*qp_flags_en & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001484 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001485
Mark Bloch175edba2018-09-17 13:30:48 +03001486 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
1487 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1488
1489 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
1490 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1491
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001492 if (dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001493 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1494 *qp_flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1495 }
1496
1497 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
Mark Blochec9c2fb2018-01-15 13:11:37 +00001498
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001499 err = mlx5_core_create_tir_out(dev->mdev, in, inlen, out, outlen);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001500
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001501 rq->tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001502 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1503 err = mlx5_ib_enable_lb(dev, false, true);
1504
1505 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001506 destroy_raw_packet_qp_tir(dev, rq, 0, pd);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001507 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001508 kvfree(in);
1509
1510 return err;
1511}
1512
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001513static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001514 u32 *in, size_t inlen,
Yishai Hadas7f720522018-09-20 21:45:18 +03001515 struct ib_pd *pd,
1516 struct ib_udata *udata,
1517 struct mlx5_ib_create_qp_resp *resp)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001518{
1519 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1520 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1521 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001522 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1523 udata, struct mlx5_ib_ucontext, ibucontext);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001524 int err;
1525 u32 tdn = mucontext->tdn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001526 u16 uid = to_mpd(pd)->uid;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001527 u32 out[MLX5_ST_SZ_DW(create_tir_out)] = {};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001528
1529 if (qp->sq.wqe_cnt) {
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001530 err = create_raw_packet_qp_tis(dev, qp, sq, tdn, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001531 if (err)
1532 return err;
1533
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001534 err = create_raw_packet_qp_sq(dev, udata, sq, in, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001535 if (err)
1536 goto err_destroy_tis;
1537
Yishai Hadas7f720522018-09-20 21:45:18 +03001538 if (uid) {
1539 resp->tisn = sq->tisn;
1540 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TISN;
1541 resp->sqn = sq->base.mqp.qpn;
1542 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_SQN;
1543 }
1544
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001545 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001546 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001547 }
1548
1549 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001550 rq->base.container_mibqp = qp;
1551
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001552 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1553 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001554 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1555 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
Yishai Hadas34d57582018-09-20 21:39:21 +03001556 err = create_raw_packet_qp_rq(dev, rq, in, inlen, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001557 if (err)
1558 goto err_destroy_sq;
1559
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001560 err = create_raw_packet_qp_tir(
1561 dev, rq, tdn, &qp->flags_en, pd, out,
1562 MLX5_ST_SZ_BYTES(create_tir_out));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001563 if (err)
1564 goto err_destroy_rq;
Yishai Hadas7f720522018-09-20 21:45:18 +03001565
1566 if (uid) {
1567 resp->rqn = rq->base.mqp.qpn;
1568 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_RQN;
1569 resp->tirn = rq->tirn;
1570 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001571 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1572 resp->tir_icm_addr = MLX5_GET(
1573 create_tir_out, out, icm_address_31_0);
1574 resp->tir_icm_addr |=
1575 (u64)MLX5_GET(create_tir_out, out,
1576 icm_address_39_32)
1577 << 32;
1578 resp->tir_icm_addr |=
1579 (u64)MLX5_GET(create_tir_out, out,
1580 icm_address_63_40)
1581 << 40;
1582 resp->comp_mask |=
1583 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1584 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001585 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001586 }
1587
1588 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1589 rq->base.mqp.qpn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001590 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
1591 if (err)
1592 goto err_destroy_tir;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001593
1594 return 0;
1595
Yishai Hadas7f720522018-09-20 21:45:18 +03001596err_destroy_tir:
1597 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001598err_destroy_rq:
1599 destroy_raw_packet_qp_rq(dev, rq);
1600err_destroy_sq:
1601 if (!qp->sq.wqe_cnt)
1602 return err;
1603 destroy_raw_packet_qp_sq(dev, sq);
1604err_destroy_tis:
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001605 destroy_raw_packet_qp_tis(dev, sq, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001606
1607 return err;
1608}
1609
1610static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1611 struct mlx5_ib_qp *qp)
1612{
1613 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1614 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1615 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1616
1617 if (qp->rq.wqe_cnt) {
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001618 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001619 destroy_raw_packet_qp_rq(dev, rq);
1620 }
1621
1622 if (qp->sq.wqe_cnt) {
1623 destroy_raw_packet_qp_sq(dev, sq);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001624 destroy_raw_packet_qp_tis(dev, sq, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001625 }
1626}
1627
1628static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1629 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1630{
1631 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1632 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1633
1634 sq->sq = &qp->sq;
1635 rq->rq = &qp->rq;
1636 sq->doorbell = &qp->db;
1637 rq->doorbell = &qp->db;
1638}
1639
Yishai Hadas28d61372016-05-23 15:20:56 +03001640static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1641{
Mark Bloch0042f9e2018-09-17 13:30:49 +03001642 if (qp->flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1643 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1644 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001645 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1646 to_mpd(qp->ibqp.pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001647}
1648
1649static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1650 struct ib_pd *pd,
1651 struct ib_qp_init_attr *init_attr,
1652 struct ib_udata *udata)
1653{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001654 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1655 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas28d61372016-05-23 15:20:56 +03001656 struct mlx5_ib_create_qp_resp resp = {};
1657 int inlen;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001658 int outlen;
Yishai Hadas28d61372016-05-23 15:20:56 +03001659 int err;
1660 u32 *in;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001661 u32 *out;
Yishai Hadas28d61372016-05-23 15:20:56 +03001662 void *tirc;
1663 void *hfso;
1664 u32 selected_fields = 0;
Matan Barak2d93fc82018-03-28 09:27:55 +03001665 u32 outer_l4;
Yishai Hadas28d61372016-05-23 15:20:56 +03001666 size_t min_resp_len;
1667 u32 tdn = mucontext->tdn;
1668 struct mlx5_ib_create_qp_rss ucmd = {};
1669 size_t required_cmd_sz;
Mark Bloch175edba2018-09-17 13:30:48 +03001670 u8 lb_flag = 0;
Yishai Hadas28d61372016-05-23 15:20:56 +03001671
1672 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1673 return -EOPNOTSUPP;
1674
1675 if (init_attr->create_flags || init_attr->send_cq)
1676 return -EINVAL;
1677
Eli Cohen2f5ff262017-01-03 23:55:21 +02001678 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001679 if (udata->outlen < min_resp_len)
1680 return -EINVAL;
1681
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001682 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001683 if (udata->inlen < required_cmd_sz) {
1684 mlx5_ib_dbg(dev, "invalid inlen\n");
1685 return -EINVAL;
1686 }
1687
1688 if (udata->inlen > sizeof(ucmd) &&
1689 !ib_is_udata_cleared(udata, sizeof(ucmd),
1690 udata->inlen - sizeof(ucmd))) {
1691 mlx5_ib_dbg(dev, "inlen is not supported\n");
1692 return -EOPNOTSUPP;
1693 }
1694
1695 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1696 mlx5_ib_dbg(dev, "copy failed\n");
1697 return -EFAULT;
1698 }
1699
1700 if (ucmd.comp_mask) {
1701 mlx5_ib_dbg(dev, "invalid comp mask\n");
1702 return -EOPNOTSUPP;
1703 }
1704
Mark Bloch175edba2018-09-17 13:30:48 +03001705 if (ucmd.flags & ~(MLX5_QP_FLAG_TUNNEL_OFFLOADS |
1706 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1707 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)) {
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001708 mlx5_ib_dbg(dev, "invalid flags\n");
1709 return -EOPNOTSUPP;
1710 }
1711
1712 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1713 !tunnel_offload_supported(dev->mdev)) {
1714 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001715 return -EOPNOTSUPP;
1716 }
1717
Maor Gottlieb309fa342017-10-19 08:25:56 +03001718 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1719 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1720 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1721 return -EOPNOTSUPP;
1722 }
1723
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001724 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC || dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001725 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1726 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1727 }
1728
1729 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
1730 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1731 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
1732 }
1733
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001734 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
Yishai Hadas28d61372016-05-23 15:20:56 +03001735 if (err) {
1736 mlx5_ib_dbg(dev, "copy failed\n");
1737 return -EINVAL;
1738 }
1739
1740 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001741 outlen = MLX5_ST_SZ_BYTES(create_tir_out);
1742 in = kvzalloc(inlen + outlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001743 if (!in)
1744 return -ENOMEM;
1745
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001746 out = in + MLX5_ST_SZ_DW(create_tir_in);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001747 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001748 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1749 MLX5_SET(tirc, tirc, disp_type,
1750 MLX5_TIRC_DISP_TYPE_INDIRECT);
1751 MLX5_SET(tirc, tirc, indirect_table,
1752 init_attr->rwq_ind_tbl->ind_tbl_num);
1753 MLX5_SET(tirc, tirc, transport_domain, tdn);
1754
1755 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001756
1757 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1758 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1759
Mark Bloch175edba2018-09-17 13:30:48 +03001760 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
1761
Maor Gottlieb309fa342017-10-19 08:25:56 +03001762 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1763 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1764 else
1765 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1766
Yishai Hadas28d61372016-05-23 15:20:56 +03001767 switch (ucmd.rx_hash_function) {
1768 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1769 {
1770 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1771 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1772
1773 if (len != ucmd.rx_key_len) {
1774 err = -EINVAL;
1775 goto err;
1776 }
1777
1778 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
Yishai Hadas28d61372016-05-23 15:20:56 +03001779 memcpy(rss_key, ucmd.rx_hash_key, len);
1780 break;
1781 }
1782 default:
1783 err = -EOPNOTSUPP;
1784 goto err;
1785 }
1786
1787 if (!ucmd.rx_hash_fields_mask) {
1788 /* special case when this TIR serves as steering entry without hashing */
1789 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1790 goto create_tir;
1791 err = -EINVAL;
1792 goto err;
1793 }
1794
1795 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1796 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1797 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1798 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1799 err = -EINVAL;
1800 goto err;
1801 }
1802
1803 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1804 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1805 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1806 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1807 MLX5_L3_PROT_TYPE_IPV4);
1808 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1809 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1810 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1811 MLX5_L3_PROT_TYPE_IPV6);
1812
Matan Barak2d93fc82018-03-28 09:27:55 +03001813 outer_l4 = ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1814 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) << 0 |
1815 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1816 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP)) << 1 |
1817 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI) << 2;
1818
1819 /* Check that only one l4 protocol is set */
1820 if (outer_l4 & (outer_l4 - 1)) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001821 err = -EINVAL;
1822 goto err;
1823 }
1824
1825 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1826 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1827 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1828 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1829 MLX5_L4_PROT_TYPE_TCP);
1830 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1831 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1832 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1833 MLX5_L4_PROT_TYPE_UDP);
1834
1835 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1836 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1837 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1838
1839 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1840 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1841 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1842
1843 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1844 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1845 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1846
1847 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1848 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1849 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1850
Matan Barak2d93fc82018-03-28 09:27:55 +03001851 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI)
1852 selected_fields |= MLX5_HASH_FIELD_SEL_IPSEC_SPI;
1853
Yishai Hadas28d61372016-05-23 15:20:56 +03001854 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1855
1856create_tir:
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001857 err = mlx5_core_create_tir_out(dev->mdev, in, inlen, out, outlen);
Yishai Hadas28d61372016-05-23 15:20:56 +03001858
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001859 qp->rss_qp.tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001860 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1861 err = mlx5_ib_enable_lb(dev, false, true);
1862
1863 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001864 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1865 to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001866 }
1867
Yishai Hadas28d61372016-05-23 15:20:56 +03001868 if (err)
1869 goto err;
1870
Yishai Hadas7f720522018-09-20 21:45:18 +03001871 if (mucontext->devx_uid) {
1872 resp.comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1873 resp.tirn = qp->rss_qp.tirn;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001874 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1875 resp.tir_icm_addr =
1876 MLX5_GET(create_tir_out, out, icm_address_31_0);
1877 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1878 icm_address_39_32)
1879 << 32;
1880 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1881 icm_address_63_40)
1882 << 40;
1883 resp.comp_mask |=
1884 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1885 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001886 }
1887
1888 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
1889 if (err)
1890 goto err_copy;
1891
Yishai Hadas28d61372016-05-23 15:20:56 +03001892 kvfree(in);
1893 /* qpn is reserved for that QP */
1894 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001895 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001896 return 0;
1897
Yishai Hadas7f720522018-09-20 21:45:18 +03001898err_copy:
1899 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn, mucontext->devx_uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001900err:
1901 kvfree(in);
1902 return err;
1903}
1904
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001905static void configure_responder_scat_cqe(struct ib_qp_init_attr *init_attr,
1906 void *qpc)
1907{
1908 int rcqe_sz;
1909
1910 if (init_attr->qp_type == MLX5_IB_QPT_DCI)
1911 return;
1912
1913 rcqe_sz = mlx5_ib_get_cqe_size(init_attr->recv_cq);
1914
Guy Levi7249c8e2019-04-10 10:59:45 +03001915 if (init_attr->qp_type == MLX5_IB_QPT_DCT) {
1916 if (rcqe_sz == 128)
1917 MLX5_SET(dctc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
1918
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001919 return;
1920 }
1921
Guy Levi7249c8e2019-04-10 10:59:45 +03001922 MLX5_SET(qpc, qpc, cs_res,
1923 rcqe_sz == 128 ? MLX5_RES_SCAT_DATA64_CQE :
1924 MLX5_RES_SCAT_DATA32_CQE);
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001925}
1926
1927static void configure_requester_scat_cqe(struct mlx5_ib_dev *dev,
1928 struct ib_qp_init_attr *init_attr,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001929 struct mlx5_ib_create_qp *ucmd,
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001930 void *qpc)
1931{
1932 enum ib_qp_type qpt = init_attr->qp_type;
1933 int scqe_sz;
zhengbin2ab367a2019-12-24 16:40:12 +08001934 bool allow_scat_cqe = false;
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001935
1936 if (qpt == IB_QPT_UC || qpt == IB_QPT_UD)
1937 return;
1938
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001939 if (ucmd)
1940 allow_scat_cqe = ucmd->flags & MLX5_QP_FLAG_ALLOW_SCATTER_CQE;
1941
1942 if (!allow_scat_cqe && init_attr->sq_sig_type != IB_SIGNAL_ALL_WR)
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001943 return;
1944
1945 scqe_sz = mlx5_ib_get_cqe_size(init_attr->send_cq);
1946 if (scqe_sz == 128) {
1947 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
1948 return;
1949 }
1950
1951 if (init_attr->qp_type != MLX5_IB_QPT_DCI ||
1952 MLX5_CAP_GEN(dev->mdev, dc_req_scat_data_cqe))
1953 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
1954}
1955
Yonatan Cohena60109d2018-10-10 09:25:16 +03001956static int atomic_size_to_mode(int size_mask)
1957{
1958 /* driver does not support atomic_size > 256B
1959 * and does not know how to translate bigger sizes
1960 */
1961 int supported_size_mask = size_mask & 0x1ff;
1962 int log_max_size;
1963
1964 if (!supported_size_mask)
1965 return -EOPNOTSUPP;
1966
1967 log_max_size = __fls(supported_size_mask);
1968
1969 if (log_max_size > 3)
1970 return log_max_size;
1971
1972 return MLX5_ATOMIC_MODE_8B;
1973}
1974
1975static int get_atomic_mode(struct mlx5_ib_dev *dev,
1976 enum ib_qp_type qp_type)
1977{
1978 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
1979 u8 atomic = MLX5_CAP_GEN(dev->mdev, atomic);
1980 int atomic_mode = -EOPNOTSUPP;
1981 int atomic_size_mask;
1982
1983 if (!atomic)
1984 return -EOPNOTSUPP;
1985
1986 if (qp_type == MLX5_IB_QPT_DCT)
1987 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
1988 else
1989 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
1990
1991 if ((atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_CMP_SWAP) ||
1992 (atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_FETCH_ADD))
1993 atomic_mode = atomic_size_to_mode(atomic_size_mask);
1994
1995 if (atomic_mode <= 0 &&
1996 (atomic_operations & MLX5_ATOMIC_OPS_CMP_SWAP &&
1997 atomic_operations & MLX5_ATOMIC_OPS_FETCH_ADD))
1998 atomic_mode = MLX5_ATOMIC_MODE_IB_COMP;
1999
2000 return atomic_mode;
2001}
2002
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002003static inline bool check_flags_mask(uint64_t input, uint64_t supported)
2004{
2005 return (input & ~supported) == 0;
2006}
2007
Eli Cohene126ba92013-07-07 17:25:49 +03002008static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
2009 struct ib_qp_init_attr *init_attr,
2010 struct ib_udata *udata, struct mlx5_ib_qp *qp)
2011{
2012 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002013 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03002014 struct mlx5_core_dev *mdev = dev->mdev;
Jason Gunthorpe0625b4b2018-08-14 15:33:52 -06002015 struct mlx5_ib_create_qp_resp resp = {};
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002016 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2017 udata, struct mlx5_ib_ucontext, ibucontext);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002018 struct mlx5_ib_cq *send_cq;
2019 struct mlx5_ib_cq *recv_cq;
2020 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002021 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002022 struct mlx5_ib_create_qp ucmd;
2023 struct mlx5_ib_qp_base *base;
Noa Osheroviche7b169f2018-02-25 13:39:51 +02002024 int mlx5_st;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002025 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002026 u32 *in;
2027 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002028
2029 mutex_init(&qp->mutex);
2030 spin_lock_init(&qp->sq.lock);
2031 spin_lock_init(&qp->rq.lock);
2032
Noa Osheroviche7b169f2018-02-25 13:39:51 +02002033 mlx5_st = to_mlx5_st(init_attr->qp_type);
2034 if (mlx5_st < 0)
2035 return -EINVAL;
2036
Yishai Hadas28d61372016-05-23 15:20:56 +03002037 if (init_attr->rwq_ind_tbl) {
2038 if (!udata)
2039 return -ENOSYS;
2040
2041 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
2042 return err;
2043 }
2044
Eli Cohenf360d882014-04-02 00:10:16 +03002045 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002046 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03002047 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
2048 return -EINVAL;
2049 } else {
2050 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
2051 }
2052 }
2053
Leon Romanovsky051f2632015-12-20 12:16:11 +02002054 if (init_attr->create_flags &
2055 (IB_QP_CREATE_CROSS_CHANNEL |
2056 IB_QP_CREATE_MANAGED_SEND |
2057 IB_QP_CREATE_MANAGED_RECV)) {
2058 if (!MLX5_CAP_GEN(mdev, cd)) {
2059 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
2060 return -EINVAL;
2061 }
2062 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
2063 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
2064 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
2065 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
2066 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
2067 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
2068 }
Erez Shitritf0313962016-02-21 16:27:17 +02002069
2070 if (init_attr->qp_type == IB_QPT_UD &&
2071 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
2072 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
2073 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
2074 return -EOPNOTSUPP;
2075 }
2076
Majd Dibbiny358e42e2016-04-17 17:19:37 +03002077 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
2078 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2079 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
2080 return -EOPNOTSUPP;
2081 }
2082 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
2083 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
2084 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
2085 return -EOPNOTSUPP;
2086 }
2087 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
2088 }
2089
Eli Cohene126ba92013-07-07 17:25:49 +03002090 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
2091 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
2092
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02002093 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
2094 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
2095 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
2096 (init_attr->qp_type != IB_QPT_RAW_PACKET))
2097 return -EOPNOTSUPP;
2098 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
2099 }
2100
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002101 if (udata) {
Eli Cohene126ba92013-07-07 17:25:49 +03002102 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
2103 mlx5_ib_dbg(dev, "copy failed\n");
2104 return -EFAULT;
2105 }
2106
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002107 if (!check_flags_mask(ucmd.flags,
Mark Bloch8af526e2019-01-15 16:45:32 +02002108 MLX5_QP_FLAG_ALLOW_SCATTER_CQE |
2109 MLX5_QP_FLAG_BFREG_INDEX |
2110 MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE |
2111 MLX5_QP_FLAG_SCATTER_CQE |
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002112 MLX5_QP_FLAG_SIGNATURE |
Mark Bloch8af526e2019-01-15 16:45:32 +02002113 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC |
2114 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
2115 MLX5_QP_FLAG_TUNNEL_OFFLOADS |
Yishai Hadasac42a5e2020-03-24 08:01:41 +02002116 MLX5_QP_FLAG_UAR_PAGE_INDEX |
Mark Bloch8af526e2019-01-15 16:45:32 +02002117 MLX5_QP_FLAG_TYPE_DCI |
2118 MLX5_QP_FLAG_TYPE_DCT))
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002119 return -EINVAL;
2120
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002121 err = get_qp_user_index(ucontext, &ucmd, udata->inlen, &uidx);
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002122 if (err)
2123 return err;
2124
Eli Cohene126ba92013-07-07 17:25:49 +03002125 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002126 if (MLX5_CAP_GEN(dev->mdev, sctr_data_cqe))
2127 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002128 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
2129 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
2130 !tunnel_offload_supported(mdev)) {
2131 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
2132 return -EOPNOTSUPP;
2133 }
Mark Bloch175edba2018-09-17 13:30:48 +03002134 qp->flags_en |= MLX5_QP_FLAG_TUNNEL_OFFLOADS;
2135 }
2136
2137 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC) {
2138 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2139 mlx5_ib_dbg(dev, "Self-LB UC isn't supported\n");
2140 return -EOPNOTSUPP;
2141 }
2142 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
2143 }
2144
2145 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
2146 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2147 mlx5_ib_dbg(dev, "Self-LB UM isn't supported\n");
2148 return -EOPNOTSUPP;
2149 }
2150 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002151 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002152
Danit Goldberg569c6652018-11-30 13:22:05 +02002153 if (ucmd.flags & MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE) {
2154 if (init_attr->qp_type != IB_QPT_RC ||
2155 !MLX5_CAP_GEN(dev->mdev, qp_packet_based)) {
2156 mlx5_ib_dbg(dev, "packet based credit mode isn't supported\n");
2157 return -EOPNOTSUPP;
2158 }
2159 qp->flags |= MLX5_IB_QP_PACKET_BASED_CREDIT;
2160 }
2161
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002162 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
2163 if (init_attr->qp_type != IB_QPT_UD ||
2164 (MLX5_CAP_GEN(dev->mdev, port_type) !=
2165 MLX5_CAP_PORT_TYPE_IB) ||
2166 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
2167 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
2168 return -EOPNOTSUPP;
2169 }
2170
2171 qp->flags |= MLX5_IB_QP_UNDERLAY;
2172 qp->underlay_qpn = init_attr->source_qpn;
2173 }
Eli Cohene126ba92013-07-07 17:25:49 +03002174 } else {
2175 qp->wq_sig = !!wq_signature;
2176 }
2177
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002178 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2179 qp->flags & MLX5_IB_QP_UNDERLAY) ?
2180 &qp->raw_packet_qp.rq.base :
2181 &qp->trans_qp.base;
2182
Eli Cohene126ba92013-07-07 17:25:49 +03002183 qp->has_rq = qp_has_rq(init_attr);
2184 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002185 qp, udata ? &ucmd : NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002186 if (err) {
2187 mlx5_ib_dbg(dev, "err %d\n", err);
2188 return err;
2189 }
2190
2191 if (pd) {
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002192 if (udata) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002193 __u32 max_wqes =
2194 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03002195 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
2196 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
2197 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
2198 mlx5_ib_dbg(dev, "invalid rq params\n");
2199 return -EINVAL;
2200 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002201 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03002202 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03002203 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03002204 return -EINVAL;
2205 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02002206 if (init_attr->create_flags &
Michael Guralnik3f89b012019-10-20 09:43:59 +03002207 MLX5_IB_QP_CREATE_SQPN_QP1) {
Haggai Eranb11a4f92016-02-29 15:45:03 +02002208 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
2209 return -EINVAL;
2210 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002211 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
2212 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002213 if (err)
2214 mlx5_ib_dbg(dev, "err %d\n", err);
2215 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002216 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
2217 base);
Eli Cohene126ba92013-07-07 17:25:49 +03002218 if (err)
2219 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03002220 }
2221
2222 if (err)
2223 return err;
2224 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002225 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03002226 if (!in)
2227 return -ENOMEM;
2228
2229 qp->create_type = MLX5_QP_EMPTY;
2230 }
2231
2232 if (is_sqp(init_attr->qp_type))
2233 qp->port = init_attr->port_num;
2234
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002235 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
2236
Noa Osheroviche7b169f2018-02-25 13:39:51 +02002237 MLX5_SET(qpc, qpc, st, mlx5_st);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002238 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03002239
2240 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002241 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002242 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002243 MLX5_SET(qpc, qpc, latency_sensitive, 1);
2244
Eli Cohene126ba92013-07-07 17:25:49 +03002245
2246 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002247 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002248
Eli Cohenf360d882014-04-02 00:10:16 +03002249 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002250 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03002251
Leon Romanovsky051f2632015-12-20 12:16:11 +02002252 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002253 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002254 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002255 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002256 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002257 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Danit Goldberg569c6652018-11-30 13:22:05 +02002258 if (qp->flags & MLX5_IB_QP_PACKET_BASED_CREDIT)
2259 MLX5_SET(qpc, qpc, req_e2e_credit_mode, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002260 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002261 configure_responder_scat_cqe(init_attr, qpc);
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002262 configure_requester_scat_cqe(dev, init_attr,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002263 udata ? &ucmd : NULL,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002264 qpc);
Eli Cohene126ba92013-07-07 17:25:49 +03002265 }
2266
2267 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002268 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
2269 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03002270 }
2271
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002272 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03002273
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002274 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002275 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002276 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002277 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002278 if (init_attr->srq &&
2279 init_attr->srq->srq_type == IB_SRQT_TM)
2280 MLX5_SET(qpc, qpc, offload_type,
2281 MLX5_QPC_OFFLOAD_TYPE_RNDV);
2282 }
Eli Cohene126ba92013-07-07 17:25:49 +03002283
2284 /* Set default resources */
2285 switch (init_attr->qp_type) {
2286 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002287 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2288 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
2289 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
2290 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002291 break;
2292 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002293 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2294 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2295 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002296 break;
2297 default:
2298 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002299 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
2300 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002301 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002302 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2303 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002304 }
2305 }
2306
2307 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002308 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002309
2310 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002311 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002312
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002313 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03002314
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002315 /* 0xffffff means we ask to work with cqe version 0 */
2316 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002317 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002318
Erez Shitritf0313962016-02-21 16:27:17 +02002319 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
2320 if (init_attr->qp_type == IB_QPT_UD &&
2321 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02002322 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
2323 qp->flags |= MLX5_IB_QP_LSO;
2324 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002325
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002326 if (init_attr->create_flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
2327 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
2328 mlx5_ib_dbg(dev, "scatter end padding is not supported\n");
2329 err = -EOPNOTSUPP;
2330 goto err;
2331 } else if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2332 MLX5_SET(qpc, qpc, end_padding_mode,
2333 MLX5_WQ_END_PAD_MODE_ALIGN);
2334 } else {
2335 qp->flags |= MLX5_IB_QP_PCI_WRITE_END_PADDING;
2336 }
2337 }
2338
Boris Pismenny2c292db2018-03-08 15:51:40 +02002339 if (inlen < 0) {
2340 err = -EINVAL;
2341 goto err;
2342 }
2343
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002344 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2345 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002346 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
2347 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
Yishai Hadas7f720522018-09-20 21:45:18 +03002348 err = create_raw_packet_qp(dev, qp, in, inlen, pd, udata,
2349 &resp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002350 } else {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03002351 err = mlx5_core_create_qp(dev, &base->mqp, in, inlen);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002352 }
2353
Eli Cohene126ba92013-07-07 17:25:49 +03002354 if (err) {
2355 mlx5_ib_dbg(dev, "create qp failed\n");
2356 goto err_create;
2357 }
2358
Al Viro479163f2014-11-20 08:13:57 +00002359 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002360
majd@mellanox.com19098df2016-01-14 19:13:03 +02002361 base->container_mibqp = qp;
2362 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03002363
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002364 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
2365 &send_cq, &recv_cq);
2366 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2367 mlx5_ib_lock_cqs(send_cq, recv_cq);
2368 /* Maintain device to QPs access, needed for further handling via reset
2369 * flow
2370 */
2371 list_add_tail(&qp->qps_list, &dev->qp_list);
2372 /* Maintain CQ to QPs access, needed for further handling via reset flow
2373 */
2374 if (send_cq)
2375 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
2376 if (recv_cq)
2377 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
2378 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2379 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
2380
Eli Cohene126ba92013-07-07 17:25:49 +03002381 return 0;
2382
2383err_create:
2384 if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002385 destroy_qp_user(dev, pd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002386 else if (qp->create_type == MLX5_QP_KERNEL)
2387 destroy_qp_kernel(dev, qp);
2388
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002389err:
Al Viro479163f2014-11-20 08:13:57 +00002390 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002391 return err;
2392}
2393
2394static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2395 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
2396{
2397 if (send_cq) {
2398 if (recv_cq) {
2399 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002400 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002401 spin_lock_nested(&recv_cq->lock,
2402 SINGLE_DEPTH_NESTING);
2403 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002404 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002405 __acquire(&recv_cq->lock);
2406 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002407 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002408 spin_lock_nested(&send_cq->lock,
2409 SINGLE_DEPTH_NESTING);
2410 }
2411 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002412 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002413 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002414 }
2415 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002416 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002417 __acquire(&send_cq->lock);
2418 } else {
2419 __acquire(&send_cq->lock);
2420 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002421 }
2422}
2423
2424static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2425 __releases(&send_cq->lock) __releases(&recv_cq->lock)
2426{
2427 if (send_cq) {
2428 if (recv_cq) {
2429 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
2430 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002431 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002432 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
2433 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002434 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002435 } else {
2436 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002437 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002438 }
2439 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002440 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002441 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002442 }
2443 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002444 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002445 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002446 } else {
2447 __release(&recv_cq->lock);
2448 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002449 }
2450}
2451
2452static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
2453{
2454 return to_mpd(qp->ibqp.pd);
2455}
2456
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002457static void get_cqs(enum ib_qp_type qp_type,
2458 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03002459 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
2460{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002461 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002462 case IB_QPT_XRC_TGT:
2463 *send_cq = NULL;
2464 *recv_cq = NULL;
2465 break;
2466 case MLX5_IB_QPT_REG_UMR:
2467 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002468 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002469 *recv_cq = NULL;
2470 break;
2471
2472 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002473 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002474 case IB_QPT_RC:
2475 case IB_QPT_UC:
2476 case IB_QPT_UD:
2477 case IB_QPT_RAW_IPV6:
2478 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002479 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002480 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2481 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002482 break;
2483
Eli Cohene126ba92013-07-07 17:25:49 +03002484 case IB_QPT_MAX:
2485 default:
2486 *send_cq = NULL;
2487 *recv_cq = NULL;
2488 break;
2489 }
2490}
2491
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002492static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002493 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2494 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002495
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002496static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2497 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002498{
2499 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002500 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002501 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002502 int err;
2503
Yishai Hadas28d61372016-05-23 15:20:56 +03002504 if (qp->ibqp.rwq_ind_tbl) {
2505 destroy_rss_raw_qp_tir(dev, qp);
2506 return;
2507 }
2508
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002509 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2510 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002511 &qp->raw_packet_qp.rq.base :
2512 &qp->trans_qp.base;
2513
Haggai Eran6aec21f2014-12-11 17:04:23 +02002514 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002515 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2516 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03002517 err = mlx5_core_qp_modify(dev, MLX5_CMD_OP_2RST_QP, 0,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002518 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002519 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002520 struct mlx5_modify_raw_qp_param raw_qp_param = {
2521 .operation = MLX5_CMD_OP_2RST_QP
2522 };
2523
Aviv Heller13eab212016-09-18 20:48:04 +03002524 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002525 }
2526 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002527 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002528 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002529 }
Eli Cohene126ba92013-07-07 17:25:49 +03002530
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002531 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2532 &send_cq, &recv_cq);
2533
2534 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2535 mlx5_ib_lock_cqs(send_cq, recv_cq);
2536 /* del from lists under both locks above to protect reset flow paths */
2537 list_del(&qp->qps_list);
2538 if (send_cq)
2539 list_del(&qp->cq_send_list);
2540
2541 if (recv_cq)
2542 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002543
2544 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002545 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002546 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2547 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002548 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2549 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002550 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002551 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2552 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002553
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002554 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2555 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002556 destroy_raw_packet_qp(dev, qp);
2557 } else {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03002558 err = mlx5_core_destroy_qp(dev, &base->mqp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002559 if (err)
2560 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2561 base->mqp.qpn);
2562 }
Eli Cohene126ba92013-07-07 17:25:49 +03002563
Eli Cohene126ba92013-07-07 17:25:49 +03002564 if (qp->create_type == MLX5_QP_KERNEL)
2565 destroy_qp_kernel(dev, qp);
2566 else if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002567 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002568}
2569
2570static const char *ib_qp_type_str(enum ib_qp_type type)
2571{
2572 switch (type) {
2573 case IB_QPT_SMI:
2574 return "IB_QPT_SMI";
2575 case IB_QPT_GSI:
2576 return "IB_QPT_GSI";
2577 case IB_QPT_RC:
2578 return "IB_QPT_RC";
2579 case IB_QPT_UC:
2580 return "IB_QPT_UC";
2581 case IB_QPT_UD:
2582 return "IB_QPT_UD";
2583 case IB_QPT_RAW_IPV6:
2584 return "IB_QPT_RAW_IPV6";
2585 case IB_QPT_RAW_ETHERTYPE:
2586 return "IB_QPT_RAW_ETHERTYPE";
2587 case IB_QPT_XRC_INI:
2588 return "IB_QPT_XRC_INI";
2589 case IB_QPT_XRC_TGT:
2590 return "IB_QPT_XRC_TGT";
2591 case IB_QPT_RAW_PACKET:
2592 return "IB_QPT_RAW_PACKET";
2593 case MLX5_IB_QPT_REG_UMR:
2594 return "MLX5_IB_QPT_REG_UMR";
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002595 case IB_QPT_DRIVER:
2596 return "IB_QPT_DRIVER";
Eli Cohene126ba92013-07-07 17:25:49 +03002597 case IB_QPT_MAX:
2598 default:
2599 return "Invalid QP type";
2600 }
2601}
2602
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002603static struct ib_qp *mlx5_ib_create_dct(struct ib_pd *pd,
2604 struct ib_qp_init_attr *attr,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002605 struct mlx5_ib_create_qp *ucmd,
2606 struct ib_udata *udata)
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002607{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002608 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2609 udata, struct mlx5_ib_ucontext, ibucontext);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002610 struct mlx5_ib_qp *qp;
2611 int err = 0;
2612 u32 uidx = MLX5_IB_DEFAULT_UIDX;
2613 void *dctc;
2614
2615 if (!attr->srq || !attr->recv_cq)
2616 return ERR_PTR(-EINVAL);
2617
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002618 err = get_qp_user_index(ucontext, ucmd, sizeof(*ucmd), &uidx);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002619 if (err)
2620 return ERR_PTR(err);
2621
2622 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2623 if (!qp)
2624 return ERR_PTR(-ENOMEM);
2625
2626 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
2627 if (!qp->dct.in) {
2628 err = -ENOMEM;
2629 goto err_free;
2630 }
2631
Yishai Hadasa01a5862018-09-20 21:39:24 +03002632 MLX5_SET(create_dct_in, qp->dct.in, uid, to_mpd(pd)->uid);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002633 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shoua776a3902018-01-02 16:19:33 +02002634 qp->qp_sub_type = MLX5_IB_QPT_DCT;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002635 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2636 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2637 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2638 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2639 MLX5_SET(dctc, dctc, user_index, uidx);
2640
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002641 if (ucmd->flags & MLX5_QP_FLAG_SCATTER_CQE)
2642 configure_responder_scat_cqe(attr, dctc);
2643
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002644 qp->state = IB_QPS_RESET;
2645
2646 return &qp->ibqp;
2647err_free:
2648 kfree(qp);
2649 return ERR_PTR(err);
2650}
2651
2652static int set_mlx_qp_type(struct mlx5_ib_dev *dev,
2653 struct ib_qp_init_attr *init_attr,
2654 struct mlx5_ib_create_qp *ucmd,
2655 struct ib_udata *udata)
2656{
2657 enum { MLX_QP_FLAGS = MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI };
2658 int err;
2659
2660 if (!udata)
2661 return -EINVAL;
2662
2663 if (udata->inlen < sizeof(*ucmd)) {
2664 mlx5_ib_dbg(dev, "create_qp user command is smaller than expected\n");
2665 return -EINVAL;
2666 }
2667 err = ib_copy_from_udata(ucmd, udata, sizeof(*ucmd));
2668 if (err)
2669 return err;
2670
2671 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCI) {
2672 init_attr->qp_type = MLX5_IB_QPT_DCI;
2673 } else {
2674 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCT) {
2675 init_attr->qp_type = MLX5_IB_QPT_DCT;
2676 } else {
2677 mlx5_ib_dbg(dev, "Invalid QP flags\n");
2678 return -EINVAL;
2679 }
2680 }
2681
2682 if (!MLX5_CAP_GEN(dev->mdev, dct)) {
2683 mlx5_ib_dbg(dev, "DC transport is not supported\n");
2684 return -EOPNOTSUPP;
2685 }
2686
2687 return 0;
2688}
2689
Eli Cohene126ba92013-07-07 17:25:49 +03002690struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002691 struct ib_qp_init_attr *verbs_init_attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002692 struct ib_udata *udata)
2693{
2694 struct mlx5_ib_dev *dev;
2695 struct mlx5_ib_qp *qp;
2696 u16 xrcdn = 0;
2697 int err;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002698 struct ib_qp_init_attr mlx_init_attr;
2699 struct ib_qp_init_attr *init_attr = verbs_init_attr;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002700 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2701 udata, struct mlx5_ib_ucontext, ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03002702
2703 if (pd) {
2704 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002705
2706 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002707 if (!ucontext) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002708 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2709 return ERR_PTR(-EINVAL);
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002710 } else if (!ucontext->cqe_version) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002711 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2712 return ERR_PTR(-EINVAL);
2713 }
2714 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002715 } else {
2716 /* being cautious here */
2717 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2718 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2719 pr_warn("%s: no PD for transport %s\n", __func__,
2720 ib_qp_type_str(init_attr->qp_type));
2721 return ERR_PTR(-EINVAL);
2722 }
2723 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002724 }
2725
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002726 if (init_attr->qp_type == IB_QPT_DRIVER) {
2727 struct mlx5_ib_create_qp ucmd;
2728
2729 init_attr = &mlx_init_attr;
2730 memcpy(init_attr, verbs_init_attr, sizeof(*verbs_init_attr));
2731 err = set_mlx_qp_type(dev, init_attr, &ucmd, udata);
2732 if (err)
2733 return ERR_PTR(err);
Moni Shouac32a4f22018-01-02 16:19:32 +02002734
2735 if (init_attr->qp_type == MLX5_IB_QPT_DCI) {
2736 if (init_attr->cap.max_recv_wr ||
2737 init_attr->cap.max_recv_sge) {
2738 mlx5_ib_dbg(dev, "DCI QP requires zero size receive queue\n");
2739 return ERR_PTR(-EINVAL);
2740 }
Moni Shoua776a3902018-01-02 16:19:33 +02002741 } else {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002742 return mlx5_ib_create_dct(pd, init_attr, &ucmd, udata);
Moni Shouac32a4f22018-01-02 16:19:32 +02002743 }
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002744 }
2745
Eli Cohene126ba92013-07-07 17:25:49 +03002746 switch (init_attr->qp_type) {
2747 case IB_QPT_XRC_TGT:
2748 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002749 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002750 mlx5_ib_dbg(dev, "XRC not supported\n");
2751 return ERR_PTR(-ENOSYS);
2752 }
2753 init_attr->recv_cq = NULL;
2754 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2755 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2756 init_attr->send_cq = NULL;
2757 }
2758
2759 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002760 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002761 case IB_QPT_RC:
2762 case IB_QPT_UC:
2763 case IB_QPT_UD:
2764 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002765 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002766 case MLX5_IB_QPT_REG_UMR:
Moni Shouac32a4f22018-01-02 16:19:32 +02002767 case MLX5_IB_QPT_DCI:
Eli Cohene126ba92013-07-07 17:25:49 +03002768 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2769 if (!qp)
2770 return ERR_PTR(-ENOMEM);
2771
2772 err = create_qp_common(dev, pd, init_attr, udata, qp);
2773 if (err) {
2774 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2775 kfree(qp);
2776 return ERR_PTR(err);
2777 }
2778
2779 if (is_qp0(init_attr->qp_type))
2780 qp->ibqp.qp_num = 0;
2781 else if (is_qp1(init_attr->qp_type))
2782 qp->ibqp.qp_num = 1;
2783 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002784 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002785
2786 mlx5_ib_dbg(dev, "ib qpnum 0x%x, mlx qpn 0x%x, rcqn 0x%x, scqn 0x%x\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002787 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002788 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2789 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002790
majd@mellanox.com19098df2016-01-14 19:13:03 +02002791 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002792
2793 break;
2794
Haggai Erand16e91d2016-02-29 15:45:05 +02002795 case IB_QPT_GSI:
2796 return mlx5_ib_gsi_create_qp(pd, init_attr);
2797
Eli Cohene126ba92013-07-07 17:25:49 +03002798 case IB_QPT_RAW_IPV6:
2799 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002800 case IB_QPT_MAX:
2801 default:
2802 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2803 init_attr->qp_type);
2804 /* Don't support raw QPs */
Kamal Heibbb8865f2020-01-30 10:20:49 +02002805 return ERR_PTR(-EOPNOTSUPP);
Eli Cohene126ba92013-07-07 17:25:49 +03002806 }
2807
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002808 if (verbs_init_attr->qp_type == IB_QPT_DRIVER)
2809 qp->qp_sub_type = init_attr->qp_type;
2810
Eli Cohene126ba92013-07-07 17:25:49 +03002811 return &qp->ibqp;
2812}
2813
Moni Shoua776a3902018-01-02 16:19:33 +02002814static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2815{
2816 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2817
2818 if (mqp->state == IB_QPS_RTR) {
2819 int err;
2820
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03002821 err = mlx5_core_destroy_dct(dev, &mqp->dct.mdct);
Moni Shoua776a3902018-01-02 16:19:33 +02002822 if (err) {
2823 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2824 return err;
2825 }
2826 }
2827
2828 kfree(mqp->dct.in);
2829 kfree(mqp);
2830 return 0;
2831}
2832
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002833int mlx5_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002834{
2835 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2836 struct mlx5_ib_qp *mqp = to_mqp(qp);
2837
Haggai Erand16e91d2016-02-29 15:45:05 +02002838 if (unlikely(qp->qp_type == IB_QPT_GSI))
2839 return mlx5_ib_gsi_destroy_qp(qp);
2840
Moni Shoua776a3902018-01-02 16:19:33 +02002841 if (mqp->qp_sub_type == MLX5_IB_QPT_DCT)
2842 return mlx5_ib_destroy_dct(mqp);
2843
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002844 destroy_qp_common(dev, mqp, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002845
2846 kfree(mqp);
2847
2848 return 0;
2849}
2850
Yonatan Cohena60109d2018-10-10 09:25:16 +03002851static int to_mlx5_access_flags(struct mlx5_ib_qp *qp,
2852 const struct ib_qp_attr *attr,
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002853 int attr_mask, __be32 *hw_access_flags_be)
Eli Cohene126ba92013-07-07 17:25:49 +03002854{
Eli Cohene126ba92013-07-07 17:25:49 +03002855 u8 dest_rd_atomic;
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002856 u32 access_flags, hw_access_flags = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002857
Yonatan Cohena60109d2018-10-10 09:25:16 +03002858 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
2859
Eli Cohene126ba92013-07-07 17:25:49 +03002860 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2861 dest_rd_atomic = attr->max_dest_rd_atomic;
2862 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002863 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002864
2865 if (attr_mask & IB_QP_ACCESS_FLAGS)
2866 access_flags = attr->qp_access_flags;
2867 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002868 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002869
2870 if (!dest_rd_atomic)
2871 access_flags &= IB_ACCESS_REMOTE_WRITE;
2872
2873 if (access_flags & IB_ACCESS_REMOTE_READ)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002874 hw_access_flags |= MLX5_QP_BIT_RRE;
Yonatan Cohen13f8d9c2018-11-21 13:48:39 +02002875 if (access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03002876 int atomic_mode;
Eli Cohene126ba92013-07-07 17:25:49 +03002877
Yonatan Cohena60109d2018-10-10 09:25:16 +03002878 atomic_mode = get_atomic_mode(dev, qp->ibqp.qp_type);
2879 if (atomic_mode < 0)
2880 return -EOPNOTSUPP;
2881
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002882 hw_access_flags |= MLX5_QP_BIT_RAE;
2883 hw_access_flags |= atomic_mode << MLX5_ATOMIC_MODE_OFFSET;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002884 }
2885
2886 if (access_flags & IB_ACCESS_REMOTE_WRITE)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002887 hw_access_flags |= MLX5_QP_BIT_RWE;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002888
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002889 *hw_access_flags_be = cpu_to_be32(hw_access_flags);
Yonatan Cohena60109d2018-10-10 09:25:16 +03002890
2891 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002892}
2893
2894enum {
2895 MLX5_PATH_FLAG_FL = 1 << 0,
2896 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2897 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2898};
2899
2900static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2901{
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002902 if (rate == IB_RATE_PORT_CURRENT)
Eli Cohene126ba92013-07-07 17:25:49 +03002903 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002904
Michael Guralnika5a5d192018-12-09 11:49:50 +02002905 if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_600_GBPS)
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002906 return -EINVAL;
2907
2908 while (rate != IB_RATE_PORT_CURRENT &&
2909 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
2910 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
2911 --rate;
2912
2913 return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
Eli Cohene126ba92013-07-07 17:25:49 +03002914}
2915
majd@mellanox.com75850d02016-01-14 19:13:06 +02002916static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002917 struct mlx5_ib_sq *sq, u8 sl,
2918 struct ib_pd *pd)
majd@mellanox.com75850d02016-01-14 19:13:06 +02002919{
2920 void *in;
2921 void *tisc;
2922 int inlen;
2923 int err;
2924
2925 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002926 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002927 if (!in)
2928 return -ENOMEM;
2929
2930 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002931 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002932
2933 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2934 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2935
2936 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2937
2938 kvfree(in);
2939
2940 return err;
2941}
2942
Aviv Heller13eab212016-09-18 20:48:04 +03002943static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002944 struct mlx5_ib_sq *sq, u8 tx_affinity,
2945 struct ib_pd *pd)
Aviv Heller13eab212016-09-18 20:48:04 +03002946{
2947 void *in;
2948 void *tisc;
2949 int inlen;
2950 int err;
2951
2952 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002953 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002954 if (!in)
2955 return -ENOMEM;
2956
2957 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002958 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
Aviv Heller13eab212016-09-18 20:48:04 +03002959
2960 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2961 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2962
2963 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2964
2965 kvfree(in);
2966
2967 return err;
2968}
2969
majd@mellanox.com75850d02016-01-14 19:13:06 +02002970static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002971 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002972 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002973 u32 path_flags, const struct ib_qp_attr *attr,
2974 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002975{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002976 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002977 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002978 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002979 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2980 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002981
Eli Cohene126ba92013-07-07 17:25:49 +03002982 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002983 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2984 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002985
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002986 if (ah_flags & IB_AH_GRH) {
2987 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002988 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002989 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002990 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002991 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002992 return -EINVAL;
2993 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002994 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002995
2996 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002997 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002998 return -EINVAL;
Parav Pandit47ec3862018-06-13 10:22:06 +03002999
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04003000 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02003001 if (qp->ibqp.qp_type == IB_QPT_RC ||
3002 qp->ibqp.qp_type == IB_QPT_UC ||
3003 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
3004 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
Parav Pandit47ec3862018-06-13 10:22:06 +03003005 path->udp_sport =
3006 mlx5_get_roce_udp_sport(dev, ah->grh.sgid_attr);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003007 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Parav Pandit47ec3862018-06-13 10:22:06 +03003008 gid_type = ah->grh.sgid_attr->gid_type;
Majd Dibbinyed884512017-01-18 14:10:35 +02003009 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003010 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003011 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003012 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
3013 path->fl_free_ar |=
3014 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003015 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
3016 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
3017 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02003018 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003019 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003020 }
3021
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003022 if (ah_flags & IB_AH_GRH) {
3023 path->mgid_index = grh->sgid_index;
3024 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03003025 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003026 cpu_to_be32((grh->traffic_class << 20) |
3027 (grh->flow_label));
3028 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003029 }
3030
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003031 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03003032 if (err < 0)
3033 return err;
3034 path->static_rate = err;
3035 path->port = port;
3036
Eli Cohene126ba92013-07-07 17:25:49 +03003037 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03003038 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03003039
majd@mellanox.com75850d02016-01-14 19:13:06 +02003040 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
3041 return modify_raw_packet_eth_prio(dev->mdev,
3042 &qp->raw_packet_qp.sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003043 sl & 0xf, qp->ibqp.pd);
majd@mellanox.com75850d02016-01-14 19:13:06 +02003044
Eli Cohene126ba92013-07-07 17:25:49 +03003045 return 0;
3046}
3047
3048static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
3049 [MLX5_QP_STATE_INIT] = {
3050 [MLX5_QP_STATE_INIT] = {
3051 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3052 MLX5_QP_OPTPAR_RAE |
3053 MLX5_QP_OPTPAR_RWE |
3054 MLX5_QP_OPTPAR_PKEY_INDEX |
3055 MLX5_QP_OPTPAR_PRI_PORT,
3056 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
3057 MLX5_QP_OPTPAR_PKEY_INDEX |
3058 MLX5_QP_OPTPAR_PRI_PORT,
3059 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3060 MLX5_QP_OPTPAR_Q_KEY |
3061 MLX5_QP_OPTPAR_PRI_PORT,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003062 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3063 MLX5_QP_OPTPAR_RAE |
3064 MLX5_QP_OPTPAR_RWE |
3065 MLX5_QP_OPTPAR_PKEY_INDEX |
3066 MLX5_QP_OPTPAR_PRI_PORT,
Eli Cohene126ba92013-07-07 17:25:49 +03003067 },
3068 [MLX5_QP_STATE_RTR] = {
3069 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3070 MLX5_QP_OPTPAR_RRE |
3071 MLX5_QP_OPTPAR_RAE |
3072 MLX5_QP_OPTPAR_RWE |
3073 MLX5_QP_OPTPAR_PKEY_INDEX,
3074 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3075 MLX5_QP_OPTPAR_RWE |
3076 MLX5_QP_OPTPAR_PKEY_INDEX,
3077 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3078 MLX5_QP_OPTPAR_Q_KEY,
3079 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
3080 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03003081 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3082 MLX5_QP_OPTPAR_RRE |
3083 MLX5_QP_OPTPAR_RAE |
3084 MLX5_QP_OPTPAR_RWE |
3085 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03003086 },
3087 },
3088 [MLX5_QP_STATE_RTR] = {
3089 [MLX5_QP_STATE_RTS] = {
3090 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3091 MLX5_QP_OPTPAR_RRE |
3092 MLX5_QP_OPTPAR_RAE |
3093 MLX5_QP_OPTPAR_RWE |
3094 MLX5_QP_OPTPAR_PM_STATE |
3095 MLX5_QP_OPTPAR_RNR_TIMEOUT,
3096 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3097 MLX5_QP_OPTPAR_RWE |
3098 MLX5_QP_OPTPAR_PM_STATE,
3099 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003100 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3101 MLX5_QP_OPTPAR_RRE |
3102 MLX5_QP_OPTPAR_RAE |
3103 MLX5_QP_OPTPAR_RWE |
3104 MLX5_QP_OPTPAR_PM_STATE |
3105 MLX5_QP_OPTPAR_RNR_TIMEOUT,
Eli Cohene126ba92013-07-07 17:25:49 +03003106 },
3107 },
3108 [MLX5_QP_STATE_RTS] = {
3109 [MLX5_QP_STATE_RTS] = {
3110 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3111 MLX5_QP_OPTPAR_RAE |
3112 MLX5_QP_OPTPAR_RWE |
3113 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03003114 MLX5_QP_OPTPAR_PM_STATE |
3115 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003116 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03003117 MLX5_QP_OPTPAR_PM_STATE |
3118 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003119 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
3120 MLX5_QP_OPTPAR_SRQN |
3121 MLX5_QP_OPTPAR_CQN_RCV,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003122 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3123 MLX5_QP_OPTPAR_RAE |
3124 MLX5_QP_OPTPAR_RWE |
3125 MLX5_QP_OPTPAR_RNR_TIMEOUT |
3126 MLX5_QP_OPTPAR_PM_STATE |
3127 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003128 },
3129 },
3130 [MLX5_QP_STATE_SQER] = {
3131 [MLX5_QP_STATE_RTS] = {
3132 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
3133 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03003134 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03003135 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3136 MLX5_QP_OPTPAR_RWE |
3137 MLX5_QP_OPTPAR_RAE |
3138 MLX5_QP_OPTPAR_RRE,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003139 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3140 MLX5_QP_OPTPAR_RWE |
3141 MLX5_QP_OPTPAR_RAE |
3142 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03003143 },
3144 },
3145};
3146
3147static int ib_nr_to_mlx5_nr(int ib_mask)
3148{
3149 switch (ib_mask) {
3150 case IB_QP_STATE:
3151 return 0;
3152 case IB_QP_CUR_STATE:
3153 return 0;
3154 case IB_QP_EN_SQD_ASYNC_NOTIFY:
3155 return 0;
3156 case IB_QP_ACCESS_FLAGS:
3157 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
3158 MLX5_QP_OPTPAR_RAE;
3159 case IB_QP_PKEY_INDEX:
3160 return MLX5_QP_OPTPAR_PKEY_INDEX;
3161 case IB_QP_PORT:
3162 return MLX5_QP_OPTPAR_PRI_PORT;
3163 case IB_QP_QKEY:
3164 return MLX5_QP_OPTPAR_Q_KEY;
3165 case IB_QP_AV:
3166 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
3167 MLX5_QP_OPTPAR_PRI_PORT;
3168 case IB_QP_PATH_MTU:
3169 return 0;
3170 case IB_QP_TIMEOUT:
3171 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
3172 case IB_QP_RETRY_CNT:
3173 return MLX5_QP_OPTPAR_RETRY_COUNT;
3174 case IB_QP_RNR_RETRY:
3175 return MLX5_QP_OPTPAR_RNR_RETRY;
3176 case IB_QP_RQ_PSN:
3177 return 0;
3178 case IB_QP_MAX_QP_RD_ATOMIC:
3179 return MLX5_QP_OPTPAR_SRA_MAX;
3180 case IB_QP_ALT_PATH:
3181 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
3182 case IB_QP_MIN_RNR_TIMER:
3183 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
3184 case IB_QP_SQ_PSN:
3185 return 0;
3186 case IB_QP_MAX_DEST_RD_ATOMIC:
3187 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
3188 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
3189 case IB_QP_PATH_MIG_STATE:
3190 return MLX5_QP_OPTPAR_PM_STATE;
3191 case IB_QP_CAP:
3192 return 0;
3193 case IB_QP_DEST_QPN:
3194 return 0;
3195 }
3196 return 0;
3197}
3198
3199static int ib_mask_to_mlx5_opt(int ib_mask)
3200{
3201 int result = 0;
3202 int i;
3203
3204 for (i = 0; i < 8 * sizeof(int); i++) {
3205 if ((1 << i) & ib_mask)
3206 result |= ib_nr_to_mlx5_nr(1 << i);
3207 }
3208
3209 return result;
3210}
3211
Yishai Hadas34d57582018-09-20 21:39:21 +03003212static int modify_raw_packet_qp_rq(
3213 struct mlx5_ib_dev *dev, struct mlx5_ib_rq *rq, int new_state,
3214 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003215{
3216 void *in;
3217 void *rqc;
3218 int inlen;
3219 int err;
3220
3221 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003222 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003223 if (!in)
3224 return -ENOMEM;
3225
3226 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
Yishai Hadas34d57582018-09-20 21:39:21 +03003227 MLX5_SET(modify_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003228
3229 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
3230 MLX5_SET(rqc, rqc, state, new_state);
3231
Alex Veskereb49ab02016-08-28 12:25:53 +03003232 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
3233 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
3234 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02003235 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03003236 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
3237 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06003238 dev_info_once(
3239 &dev->ib_dev.dev,
3240 "RAW PACKET QP counters are not supported on current FW\n");
Alex Veskereb49ab02016-08-28 12:25:53 +03003241 }
3242
3243 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003244 if (err)
3245 goto out;
3246
3247 rq->state = new_state;
3248
3249out:
3250 kvfree(in);
3251 return err;
3252}
3253
Yishai Hadasc14003f2018-09-20 21:39:22 +03003254static int modify_raw_packet_qp_sq(
3255 struct mlx5_core_dev *dev, struct mlx5_ib_sq *sq, int new_state,
3256 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003257{
Bodong Wang7d29f342016-12-01 13:43:16 +02003258 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
Bodong Wang61147f32018-03-19 15:10:30 +02003259 struct mlx5_rate_limit old_rl = ibqp->rl;
3260 struct mlx5_rate_limit new_rl = old_rl;
3261 bool new_rate_added = false;
Bodong Wang7d29f342016-12-01 13:43:16 +02003262 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003263 void *in;
3264 void *sqc;
3265 int inlen;
3266 int err;
3267
3268 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003269 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003270 if (!in)
3271 return -ENOMEM;
3272
Yishai Hadasc14003f2018-09-20 21:39:22 +03003273 MLX5_SET(modify_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003274 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
3275
3276 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
3277 MLX5_SET(sqc, sqc, state, new_state);
3278
Bodong Wang7d29f342016-12-01 13:43:16 +02003279 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
3280 if (new_state != MLX5_SQC_STATE_RDY)
3281 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
3282 __func__);
3283 else
Bodong Wang61147f32018-03-19 15:10:30 +02003284 new_rl = raw_qp_param->rl;
Bodong Wang7d29f342016-12-01 13:43:16 +02003285 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003286
Bodong Wang61147f32018-03-19 15:10:30 +02003287 if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
3288 if (new_rl.rate) {
3289 err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003290 if (err) {
Bodong Wang61147f32018-03-19 15:10:30 +02003291 pr_err("Failed configuring rate limit(err %d): \
3292 rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
3293 err, new_rl.rate, new_rl.max_burst_sz,
3294 new_rl.typical_pkt_sz);
3295
Bodong Wang7d29f342016-12-01 13:43:16 +02003296 goto out;
3297 }
Bodong Wang61147f32018-03-19 15:10:30 +02003298 new_rate_added = true;
Bodong Wang7d29f342016-12-01 13:43:16 +02003299 }
3300
3301 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
Bodong Wang61147f32018-03-19 15:10:30 +02003302 /* index 0 means no limit */
Bodong Wang7d29f342016-12-01 13:43:16 +02003303 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
3304 }
3305
3306 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
3307 if (err) {
3308 /* Remove new rate from table if failed */
Bodong Wang61147f32018-03-19 15:10:30 +02003309 if (new_rate_added)
3310 mlx5_rl_remove_rate(dev, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003311 goto out;
3312 }
3313
3314 /* Only remove the old rate after new rate was set */
Rafi Wienerc8973df2019-10-02 15:02:43 +03003315 if ((old_rl.rate && !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
3316 (new_state != MLX5_SQC_STATE_RDY)) {
Bodong Wang61147f32018-03-19 15:10:30 +02003317 mlx5_rl_remove_rate(dev, &old_rl);
Rafi Wienerc8973df2019-10-02 15:02:43 +03003318 if (new_state != MLX5_SQC_STATE_RDY)
3319 memset(&new_rl, 0, sizeof(new_rl));
3320 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003321
Bodong Wang61147f32018-03-19 15:10:30 +02003322 ibqp->rl = new_rl;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003323 sq->state = new_state;
3324
3325out:
3326 kvfree(in);
3327 return err;
3328}
3329
3330static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03003331 const struct mlx5_modify_raw_qp_param *raw_qp_param,
3332 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003333{
3334 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
3335 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
3336 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02003337 int modify_rq = !!qp->rq.wqe_cnt;
3338 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003339 int rq_state;
3340 int sq_state;
3341 int err;
3342
Alex Vesker0680efa2016-08-28 12:25:52 +03003343 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003344 case MLX5_CMD_OP_RST2INIT_QP:
3345 rq_state = MLX5_RQC_STATE_RDY;
3346 sq_state = MLX5_SQC_STATE_RDY;
3347 break;
3348 case MLX5_CMD_OP_2ERR_QP:
3349 rq_state = MLX5_RQC_STATE_ERR;
3350 sq_state = MLX5_SQC_STATE_ERR;
3351 break;
3352 case MLX5_CMD_OP_2RST_QP:
3353 rq_state = MLX5_RQC_STATE_RST;
3354 sq_state = MLX5_SQC_STATE_RST;
3355 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003356 case MLX5_CMD_OP_RTR2RTS_QP:
3357 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02003358 if (raw_qp_param->set_mask ==
3359 MLX5_RAW_QP_RATE_LIMIT) {
3360 modify_rq = 0;
3361 sq_state = sq->state;
3362 } else {
3363 return raw_qp_param->set_mask ? -EINVAL : 0;
3364 }
3365 break;
3366 case MLX5_CMD_OP_INIT2INIT_QP:
3367 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03003368 if (raw_qp_param->set_mask)
3369 return -EINVAL;
3370 else
3371 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003372 default:
3373 WARN_ON(1);
3374 return -EINVAL;
3375 }
3376
Bodong Wang7d29f342016-12-01 13:43:16 +02003377 if (modify_rq) {
Yishai Hadas34d57582018-09-20 21:39:21 +03003378 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param,
3379 qp->ibqp.pd);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003380 if (err)
3381 return err;
3382 }
3383
Bodong Wang7d29f342016-12-01 13:43:16 +02003384 if (modify_sq) {
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003385 struct mlx5_flow_handle *flow_rule;
3386
Aviv Heller13eab212016-09-18 20:48:04 +03003387 if (tx_affinity) {
3388 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003389 tx_affinity,
3390 qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003391 if (err)
3392 return err;
3393 }
3394
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003395 flow_rule = create_flow_rule_vport_sq(dev, sq,
3396 raw_qp_param->port);
3397 if (IS_ERR(flow_rule))
Colin Ian King1db86312019-04-12 11:40:17 +01003398 return PTR_ERR(flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003399
3400 err = modify_raw_packet_qp_sq(dev->mdev, sq, sq_state,
3401 raw_qp_param, qp->ibqp.pd);
3402 if (err) {
3403 if (flow_rule)
3404 mlx5_del_flow_rules(flow_rule);
3405 return err;
3406 }
3407
3408 if (flow_rule) {
3409 destroy_flow_rule_vport_sq(sq);
3410 sq->flow_rule = flow_rule;
3411 }
3412
3413 return err;
Aviv Heller13eab212016-09-18 20:48:04 +03003414 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003415
3416 return 0;
3417}
3418
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003419static unsigned int get_tx_affinity(struct mlx5_ib_dev *dev,
3420 struct mlx5_ib_pd *pd,
3421 struct mlx5_ib_qp_base *qp_base,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003422 u8 port_num, struct ib_udata *udata)
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003423{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003424 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
3425 udata, struct mlx5_ib_ucontext, ibucontext);
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003426 unsigned int tx_port_affinity;
3427
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003428 if (ucontext) {
3429 tx_port_affinity = (unsigned int)atomic_add_return(
3430 1, &ucontext->tx_port_affinity) %
3431 MLX5_MAX_PORTS +
3432 1;
3433 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x ucontext %p\n",
3434 tx_port_affinity, qp_base->mqp.qpn, ucontext);
3435 } else {
3436 tx_port_affinity =
3437 (unsigned int)atomic_add_return(
Mark Bloch95579e72019-03-28 15:27:33 +02003438 1, &dev->port[port_num].roce.tx_port_affinity) %
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003439 MLX5_MAX_PORTS +
3440 1;
3441 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x\n",
3442 tx_port_affinity, qp_base->mqp.qpn);
3443 }
3444
3445 return tx_port_affinity;
3446}
3447
Mark Zhangd14133d2019-07-02 13:02:36 +03003448static int __mlx5_ib_qp_set_counter(struct ib_qp *qp,
3449 struct rdma_counter *counter)
3450{
3451 struct mlx5_ib_dev *dev = to_mdev(qp->device);
3452 struct mlx5_ib_qp *mqp = to_mqp(qp);
3453 struct mlx5_qp_context context = {};
Mark Zhangd14133d2019-07-02 13:02:36 +03003454 struct mlx5_ib_qp_base *base;
3455 u32 set_id;
3456
Parav Pandit3e1f0002019-07-23 10:31:17 +03003457 if (counter)
Mark Zhangd14133d2019-07-02 13:02:36 +03003458 set_id = counter->id;
Parav Pandit3e1f0002019-07-23 10:31:17 +03003459 else
3460 set_id = mlx5_ib_get_counters_id(dev, mqp->port - 1);
Mark Zhangd14133d2019-07-02 13:02:36 +03003461
3462 base = &mqp->trans_qp.base;
3463 context.qp_counter_set_usr_page &= cpu_to_be32(0xffffff);
3464 context.qp_counter_set_usr_page |= cpu_to_be32(set_id << 24);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03003465 return mlx5_core_qp_modify(dev, MLX5_CMD_OP_RTS2RTS_QP,
3466 MLX5_QP_OPTPAR_COUNTER_SET_ID, &context,
3467 &base->mqp);
Mark Zhangd14133d2019-07-02 13:02:36 +03003468}
3469
Eli Cohene126ba92013-07-07 17:25:49 +03003470static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
3471 const struct ib_qp_attr *attr, int attr_mask,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003472 enum ib_qp_state cur_state,
3473 enum ib_qp_state new_state,
3474 const struct mlx5_ib_modify_qp *ucmd,
3475 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003476{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003477 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
3478 [MLX5_QP_STATE_RST] = {
3479 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3480 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3481 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
3482 },
3483 [MLX5_QP_STATE_INIT] = {
3484 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3485 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3486 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
3487 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
3488 },
3489 [MLX5_QP_STATE_RTR] = {
3490 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3491 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3492 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
3493 },
3494 [MLX5_QP_STATE_RTS] = {
3495 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3496 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3497 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
3498 },
3499 [MLX5_QP_STATE_SQD] = {
3500 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3501 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3502 },
3503 [MLX5_QP_STATE_SQER] = {
3504 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3505 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3506 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
3507 },
3508 [MLX5_QP_STATE_ERR] = {
3509 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3510 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3511 }
3512 };
3513
Eli Cohene126ba92013-07-07 17:25:49 +03003514 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3515 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02003516 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03003517 struct mlx5_ib_cq *send_cq, *recv_cq;
3518 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03003519 struct mlx5_ib_pd *pd;
3520 enum mlx5_qp_state mlx5_cur, mlx5_new;
3521 enum mlx5_qp_optpar optpar;
Mark Zhangd14133d2019-07-02 13:02:36 +03003522 u32 set_id = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003523 int mlx5_st;
3524 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003525 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03003526 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003527
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003528 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
3529 qp->qp_sub_type : ibqp->qp_type);
3530 if (mlx5_st < 0)
3531 return -EINVAL;
3532
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003533 context = kzalloc(sizeof(*context), GFP_KERNEL);
3534 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03003535 return -ENOMEM;
3536
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003537 pd = get_pd(qp);
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003538 context->flags = cpu_to_be32(mlx5_st << 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003539
3540 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
3541 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3542 } else {
3543 switch (attr->path_mig_state) {
3544 case IB_MIG_MIGRATED:
3545 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3546 break;
3547 case IB_MIG_REARM:
3548 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
3549 break;
3550 case IB_MIG_ARMED:
3551 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
3552 break;
3553 }
3554 }
3555
Aviv Heller13eab212016-09-18 20:48:04 +03003556 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
3557 if ((ibqp->qp_type == IB_QPT_RC) ||
3558 (ibqp->qp_type == IB_QPT_UD &&
3559 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
3560 (ibqp->qp_type == IB_QPT_UC) ||
3561 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
3562 (ibqp->qp_type == IB_QPT_XRC_INI) ||
3563 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
Aviv Heller7c34ec12018-08-23 13:47:53 +03003564 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02003565 u8 p = mlx5_core_native_port_num(dev->mdev) - 1;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003566 tx_affinity = get_tx_affinity(dev, pd, base, p,
3567 udata);
Aviv Heller13eab212016-09-18 20:48:04 +03003568 context->flags |= cpu_to_be32(tx_affinity << 24);
3569 }
3570 }
3571 }
3572
Haggai Erand16e91d2016-02-29 15:45:05 +02003573 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003574 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003575 } else if ((ibqp->qp_type == IB_QPT_UD &&
3576 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003577 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
3578 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
3579 } else if (attr_mask & IB_QP_PATH_MTU) {
3580 if (attr->path_mtu < IB_MTU_256 ||
3581 attr->path_mtu > IB_MTU_4096) {
3582 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3583 err = -EINVAL;
3584 goto out;
3585 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003586 context->mtu_msgmax = (attr->path_mtu << 5) |
3587 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03003588 }
3589
3590 if (attr_mask & IB_QP_DEST_QPN)
3591 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
3592
3593 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003594 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003595
3596 /* todo implement counter_index functionality */
3597
3598 if (is_sqp(ibqp->qp_type))
3599 context->pri_path.port = qp->port;
3600
3601 if (attr_mask & IB_QP_PORT)
3602 context->pri_path.port = attr->port_num;
3603
3604 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003605 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003606 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003607 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003608 if (err)
3609 goto out;
3610 }
3611
3612 if (attr_mask & IB_QP_TIMEOUT)
3613 context->pri_path.ackto_lt |= attr->timeout << 3;
3614
3615 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003616 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3617 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003618 attr->alt_port_num,
3619 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3620 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003621 if (err)
3622 goto out;
3623 }
3624
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003625 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3626 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003627
3628 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3629 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3630 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3631 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3632
3633 if (attr_mask & IB_QP_RNR_RETRY)
3634 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3635
3636 if (attr_mask & IB_QP_RETRY_CNT)
3637 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3638
3639 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3640 if (attr->max_rd_atomic)
3641 context->params1 |=
3642 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3643 }
3644
3645 if (attr_mask & IB_QP_SQ_PSN)
3646 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3647
3648 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3649 if (attr->max_dest_rd_atomic)
3650 context->params2 |=
3651 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3652 }
3653
Yonatan Cohena60109d2018-10-10 09:25:16 +03003654 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08003655 __be32 access_flags;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003656
3657 err = to_mlx5_access_flags(qp, attr, attr_mask, &access_flags);
3658 if (err)
3659 goto out;
3660
3661 context->params2 |= access_flags;
3662 }
Eli Cohene126ba92013-07-07 17:25:49 +03003663
3664 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3665 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3666
3667 if (attr_mask & IB_QP_RQ_PSN)
3668 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3669
3670 if (attr_mask & IB_QP_QKEY)
3671 context->qkey = cpu_to_be32(attr->qkey);
3672
3673 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3674 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3675
Mark Bloch0837e862016-06-17 15:10:55 +03003676 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3677 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3678 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003679
3680 /* Underlay port should be used - index 0 function per port */
3681 if (qp->flags & MLX5_IB_QP_UNDERLAY)
3682 port_num = 0;
3683
Mark Zhangd14133d2019-07-02 13:02:36 +03003684 if (ibqp->counter)
3685 set_id = ibqp->counter->id;
3686 else
Parav Pandit3e1f0002019-07-23 10:31:17 +03003687 set_id = mlx5_ib_get_counters_id(dev, port_num);
Mark Bloch0837e862016-06-17 15:10:55 +03003688 context->qp_counter_set_usr_page |=
Mark Zhangd14133d2019-07-02 13:02:36 +03003689 cpu_to_be32(set_id << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003690 }
3691
Eli Cohene126ba92013-07-07 17:25:49 +03003692 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3693 context->sq_crq_size |= cpu_to_be16(1 << 4);
3694
Haggai Eranb11a4f92016-02-29 15:45:03 +02003695 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
3696 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003697
3698 mlx5_cur = to_mlx5_state(cur_state);
3699 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003700
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003701 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003702 !optab[mlx5_cur][mlx5_new]) {
3703 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003704 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003705 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003706
3707 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003708 optpar = ib_mask_to_mlx5_opt(attr_mask);
3709 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003710
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003711 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3712 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003713 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3714
3715 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003716 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Mark Zhangd14133d2019-07-02 13:02:36 +03003717 raw_qp_param.rq_q_ctr_id = set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003718 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3719 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003720
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003721 if (attr_mask & IB_QP_PORT)
3722 raw_qp_param.port = attr->port_num;
3723
Bodong Wang7d29f342016-12-01 13:43:16 +02003724 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003725 raw_qp_param.rl.rate = attr->rate_limit;
3726
3727 if (ucmd->burst_info.max_burst_sz) {
3728 if (attr->rate_limit &&
3729 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3730 raw_qp_param.rl.max_burst_sz =
3731 ucmd->burst_info.max_burst_sz;
3732 } else {
3733 err = -EINVAL;
3734 goto out;
3735 }
3736 }
3737
3738 if (ucmd->burst_info.typical_pkt_sz) {
3739 if (attr->rate_limit &&
3740 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3741 raw_qp_param.rl.typical_pkt_sz =
3742 ucmd->burst_info.typical_pkt_sz;
3743 } else {
3744 err = -EINVAL;
3745 goto out;
3746 }
3747 }
3748
Bodong Wang7d29f342016-12-01 13:43:16 +02003749 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3750 }
3751
Aviv Heller13eab212016-09-18 20:48:04 +03003752 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003753 } else {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03003754 err = mlx5_core_qp_modify(dev, op, optpar, context, &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003755 }
3756
Eli Cohene126ba92013-07-07 17:25:49 +03003757 if (err)
3758 goto out;
3759
3760 qp->state = new_state;
3761
3762 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003763 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003764 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003765 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003766 if (attr_mask & IB_QP_PORT)
3767 qp->port = attr->port_num;
3768 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003769 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003770
3771 /*
3772 * If we moved a kernel QP to RESET, clean up all old CQ
3773 * entries and reinitialize the QP.
3774 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02003775 if (new_state == IB_QPS_RESET &&
3776 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003777 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003778 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3779 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003780 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003781
3782 qp->rq.head = 0;
3783 qp->rq.tail = 0;
3784 qp->sq.head = 0;
3785 qp->sq.tail = 0;
3786 qp->sq.cur_post = 0;
Guy Levi34f4c952018-11-26 08:15:50 +02003787 if (qp->sq.wqe_cnt)
3788 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
Leon Romanovsky950bf4f2020-03-18 11:16:40 +02003789 qp->sq.last_poll = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003790 qp->db.db[MLX5_RCV_DBR] = 0;
3791 qp->db.db[MLX5_SND_DBR] = 0;
3792 }
3793
Mark Zhangd14133d2019-07-02 13:02:36 +03003794 if ((new_state == IB_QPS_RTS) && qp->counter_pending) {
3795 err = __mlx5_ib_qp_set_counter(ibqp, ibqp->counter);
3796 if (!err)
3797 qp->counter_pending = 0;
3798 }
3799
Eli Cohene126ba92013-07-07 17:25:49 +03003800out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003801 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003802 return err;
3803}
3804
Moni Shouac32a4f22018-01-02 16:19:32 +02003805static inline bool is_valid_mask(int mask, int req, int opt)
3806{
3807 if ((mask & req) != req)
3808 return false;
3809
3810 if (mask & ~(req | opt))
3811 return false;
3812
3813 return true;
3814}
3815
3816/* check valid transition for driver QP types
3817 * for now the only QP type that this function supports is DCI
3818 */
3819static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3820 enum ib_qp_attr_mask attr_mask)
3821{
3822 int req = IB_QP_STATE;
3823 int opt = 0;
3824
Moni Shoua99ed7482018-09-12 09:33:55 +03003825 if (new_state == IB_QPS_RESET) {
3826 return is_valid_mask(attr_mask, req, opt);
3827 } else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Moni Shouac32a4f22018-01-02 16:19:32 +02003828 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
3829 return is_valid_mask(attr_mask, req, opt);
3830 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3831 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3832 return is_valid_mask(attr_mask, req, opt);
3833 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3834 req |= IB_QP_PATH_MTU;
Artemy Kovalyov5ec03042018-11-05 08:12:07 +02003835 opt = IB_QP_PKEY_INDEX | IB_QP_AV;
Moni Shouac32a4f22018-01-02 16:19:32 +02003836 return is_valid_mask(attr_mask, req, opt);
3837 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3838 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3839 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3840 opt = IB_QP_MIN_RNR_TIMER;
3841 return is_valid_mask(attr_mask, req, opt);
3842 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3843 opt = IB_QP_MIN_RNR_TIMER;
3844 return is_valid_mask(attr_mask, req, opt);
3845 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3846 return is_valid_mask(attr_mask, req, opt);
3847 }
3848 return false;
3849}
3850
Moni Shoua776a3902018-01-02 16:19:33 +02003851/* mlx5_ib_modify_dct: modify a DCT QP
3852 * valid transitions are:
3853 * RESET to INIT: must set access_flags, pkey_index and port
3854 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3855 * mtu, gid_index and hop_limit
3856 * Other transitions and attributes are illegal
3857 */
3858static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3859 int attr_mask, struct ib_udata *udata)
3860{
3861 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3862 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3863 enum ib_qp_state cur_state, new_state;
3864 int err = 0;
3865 int required = IB_QP_STATE;
3866 void *dctc;
3867
3868 if (!(attr_mask & IB_QP_STATE))
3869 return -EINVAL;
3870
3871 cur_state = qp->state;
3872 new_state = attr->qp_state;
3873
3874 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3875 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03003876 u16 set_id;
3877
Moni Shoua776a3902018-01-02 16:19:33 +02003878 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3879 if (!is_valid_mask(attr_mask, required, 0))
3880 return -EINVAL;
3881
3882 if (attr->port_num == 0 ||
3883 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3884 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3885 attr->port_num, dev->num_ports);
3886 return -EINVAL;
3887 }
3888 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3889 MLX5_SET(dctc, dctc, rre, 1);
3890 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3891 MLX5_SET(dctc, dctc, rwe, 1);
3892 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03003893 int atomic_mode;
3894
3895 atomic_mode = get_atomic_mode(dev, MLX5_IB_QPT_DCT);
3896 if (atomic_mode < 0)
Moni Shoua776a3902018-01-02 16:19:33 +02003897 return -EOPNOTSUPP;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003898
3899 MLX5_SET(dctc, dctc, atomic_mode, atomic_mode);
Moni Shoua776a3902018-01-02 16:19:33 +02003900 MLX5_SET(dctc, dctc, rae, 1);
Moni Shoua776a3902018-01-02 16:19:33 +02003901 }
3902 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3903 MLX5_SET(dctc, dctc, port, attr->port_num);
Parav Pandit3e1f0002019-07-23 10:31:17 +03003904
3905 set_id = mlx5_ib_get_counters_id(dev, attr->port_num - 1);
3906 MLX5_SET(dctc, dctc, counter_set_id, set_id);
Moni Shoua776a3902018-01-02 16:19:33 +02003907
3908 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3909 struct mlx5_ib_modify_qp_resp resp = {};
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003910 u32 out[MLX5_ST_SZ_DW(create_dct_out)] = {0};
Moni Shoua776a3902018-01-02 16:19:33 +02003911 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3912 sizeof(resp.dctn);
3913
3914 if (udata->outlen < min_resp_len)
3915 return -EINVAL;
3916 resp.response_length = min_resp_len;
3917
3918 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3919 if (!is_valid_mask(attr_mask, required, 0))
3920 return -EINVAL;
3921 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3922 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3923 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3924 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3925 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3926 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3927
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03003928 err = mlx5_core_create_dct(dev, &qp->dct.mdct, qp->dct.in,
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003929 MLX5_ST_SZ_BYTES(create_dct_in), out,
3930 sizeof(out));
Moni Shoua776a3902018-01-02 16:19:33 +02003931 if (err)
3932 return err;
3933 resp.dctn = qp->dct.mdct.mqp.qpn;
3934 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3935 if (err) {
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03003936 mlx5_core_destroy_dct(dev, &qp->dct.mdct);
Moni Shoua776a3902018-01-02 16:19:33 +02003937 return err;
3938 }
3939 } else {
3940 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3941 return -EINVAL;
3942 }
3943 if (err)
3944 qp->state = IB_QPS_ERR;
3945 else
3946 qp->state = new_state;
3947 return err;
3948}
3949
Eli Cohene126ba92013-07-07 17:25:49 +03003950int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3951 int attr_mask, struct ib_udata *udata)
3952{
3953 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3954 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02003955 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02003956 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003957 enum ib_qp_state cur_state, new_state;
Bodong Wang61147f32018-03-19 15:10:30 +02003958 size_t required_cmd_sz;
Eli Cohene126ba92013-07-07 17:25:49 +03003959 int err = -EINVAL;
3960 int port;
3961
Yishai Hadas28d61372016-05-23 15:20:56 +03003962 if (ibqp->rwq_ind_tbl)
3963 return -ENOSYS;
3964
Bodong Wang61147f32018-03-19 15:10:30 +02003965 if (udata && udata->inlen) {
3966 required_cmd_sz = offsetof(typeof(ucmd), reserved) +
3967 sizeof(ucmd.reserved);
3968 if (udata->inlen < required_cmd_sz)
3969 return -EINVAL;
3970
3971 if (udata->inlen > sizeof(ucmd) &&
3972 !ib_is_udata_cleared(udata, sizeof(ucmd),
3973 udata->inlen - sizeof(ucmd)))
3974 return -EOPNOTSUPP;
3975
3976 if (ib_copy_from_udata(&ucmd, udata,
3977 min(udata->inlen, sizeof(ucmd))))
3978 return -EFAULT;
3979
3980 if (ucmd.comp_mask ||
3981 memchr_inv(&ucmd.reserved, 0, sizeof(ucmd.reserved)) ||
3982 memchr_inv(&ucmd.burst_info.reserved, 0,
3983 sizeof(ucmd.burst_info.reserved)))
3984 return -EOPNOTSUPP;
3985 }
3986
Haggai Erand16e91d2016-02-29 15:45:05 +02003987 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3988 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3989
Moni Shouac32a4f22018-01-02 16:19:32 +02003990 if (ibqp->qp_type == IB_QPT_DRIVER)
3991 qp_type = qp->qp_sub_type;
3992 else
3993 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3994 IB_QPT_GSI : ibqp->qp_type;
3995
Moni Shoua776a3902018-01-02 16:19:33 +02003996 if (qp_type == MLX5_IB_QPT_DCT)
3997 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02003998
Eli Cohene126ba92013-07-07 17:25:49 +03003999 mutex_lock(&qp->mutex);
4000
4001 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
4002 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
4003
Achiad Shochat2811ba52015-12-23 18:47:24 +02004004 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
4005 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02004006 }
4007
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004008 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
4009 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
4010 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
4011 attr_mask);
4012 goto out;
4013 }
4014 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02004015 qp_type != MLX5_IB_QPT_DCI &&
Kamal Heibd31131b2018-10-02 16:11:21 +03004016 !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
4017 attr_mask)) {
Haggai Eran158abf82016-02-29 15:45:04 +02004018 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
4019 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03004020 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02004021 } else if (qp_type == MLX5_IB_QPT_DCI &&
4022 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
4023 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
4024 cur_state, new_state, qp_type, attr_mask);
4025 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004026 }
Eli Cohene126ba92013-07-07 17:25:49 +03004027
4028 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004029 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02004030 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02004031 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
4032 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03004033 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004034 }
Eli Cohene126ba92013-07-07 17:25:49 +03004035
4036 if (attr_mask & IB_QP_PKEY_INDEX) {
4037 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03004038 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02004039 dev->mdev->port_caps[port - 1].pkey_table_len) {
4040 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
4041 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004042 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004043 }
Eli Cohene126ba92013-07-07 17:25:49 +03004044 }
4045
4046 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004047 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02004048 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
4049 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
4050 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03004051 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004052 }
Eli Cohene126ba92013-07-07 17:25:49 +03004053
4054 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004055 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02004056 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
4057 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
4058 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03004059 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004060 }
Eli Cohene126ba92013-07-07 17:25:49 +03004061
4062 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
4063 err = 0;
4064 goto out;
4065 }
4066
Bodong Wang61147f32018-03-19 15:10:30 +02004067 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02004068 new_state, &ucmd, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03004069
4070out:
4071 mutex_unlock(&qp->mutex);
4072 return err;
4073}
4074
Guy Levi34f4c952018-11-26 08:15:50 +02004075static void _handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
4076 u32 wqe_sz, void **cur_edge)
4077{
4078 u32 idx;
4079
4080 idx = (sq->cur_post + (wqe_sz >> 2)) & (sq->wqe_cnt - 1);
4081 *cur_edge = get_sq_edge(sq, idx);
4082
4083 *seg = mlx5_frag_buf_get_wqe(&sq->fbc, idx);
4084}
4085
4086/* handle_post_send_edge - Check if we get to SQ edge. If yes, update to the
4087 * next nearby edge and get new address translation for current WQE position.
4088 * @sq - SQ buffer.
4089 * @seg: Current WQE position (16B aligned).
4090 * @wqe_sz: Total current WQE size [16B].
4091 * @cur_edge: Updated current edge.
4092 */
4093static inline void handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
4094 u32 wqe_sz, void **cur_edge)
4095{
4096 if (likely(*seg != *cur_edge))
4097 return;
4098
4099 _handle_post_send_edge(sq, seg, wqe_sz, cur_edge);
4100}
4101
4102/* memcpy_send_wqe - copy data from src to WQE and update the relevant WQ's
4103 * pointers. At the end @seg is aligned to 16B regardless the copied size.
4104 * @sq - SQ buffer.
4105 * @cur_edge: Updated current edge.
4106 * @seg: Current WQE position (16B aligned).
4107 * @wqe_sz: Total current WQE size [16B].
4108 * @src: Pointer to copy from.
4109 * @n: Number of bytes to copy.
4110 */
4111static inline void memcpy_send_wqe(struct mlx5_ib_wq *sq, void **cur_edge,
4112 void **seg, u32 *wqe_sz, const void *src,
4113 size_t n)
4114{
4115 while (likely(n)) {
4116 size_t leftlen = *cur_edge - *seg;
4117 size_t copysz = min_t(size_t, leftlen, n);
4118 size_t stride;
4119
4120 memcpy(*seg, src, copysz);
4121
4122 n -= copysz;
4123 src += copysz;
4124 stride = !n ? ALIGN(copysz, 16) : copysz;
4125 *seg += stride;
4126 *wqe_sz += stride >> 4;
4127 handle_post_send_edge(sq, seg, *wqe_sz, cur_edge);
4128 }
4129}
4130
Eli Cohene126ba92013-07-07 17:25:49 +03004131static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
4132{
4133 struct mlx5_ib_cq *cq;
4134 unsigned cur;
4135
4136 cur = wq->head - wq->tail;
4137 if (likely(cur + nreq < wq->max_post))
4138 return 0;
4139
4140 cq = to_mcq(ib_cq);
4141 spin_lock(&cq->lock);
4142 cur = wq->head - wq->tail;
4143 spin_unlock(&cq->lock);
4144
4145 return cur + nreq >= wq->max_post;
4146}
4147
4148static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
4149 u64 remote_addr, u32 rkey)
4150{
4151 rseg->raddr = cpu_to_be64(remote_addr);
4152 rseg->rkey = cpu_to_be32(rkey);
4153 rseg->reserved = 0;
4154}
4155
Guy Levi34f4c952018-11-26 08:15:50 +02004156static void set_eth_seg(const struct ib_send_wr *wr, struct mlx5_ib_qp *qp,
4157 void **seg, int *size, void **cur_edge)
Erez Shitritf0313962016-02-21 16:27:17 +02004158{
Guy Levi34f4c952018-11-26 08:15:50 +02004159 struct mlx5_wqe_eth_seg *eseg = *seg;
Erez Shitritf0313962016-02-21 16:27:17 +02004160
4161 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
4162
4163 if (wr->send_flags & IB_SEND_IP_CSUM)
4164 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
4165 MLX5_ETH_WQE_L4_CSUM;
4166
Erez Shitritf0313962016-02-21 16:27:17 +02004167 if (wr->opcode == IB_WR_LSO) {
4168 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Guy Levi34f4c952018-11-26 08:15:50 +02004169 size_t left, copysz;
Erez Shitritf0313962016-02-21 16:27:17 +02004170 void *pdata = ud_wr->header;
Guy Levi34f4c952018-11-26 08:15:50 +02004171 size_t stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004172
4173 left = ud_wr->hlen;
4174 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02004175 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02004176
Guy Levi34f4c952018-11-26 08:15:50 +02004177 /* memcpy_send_wqe should get a 16B align address. Hence, we
4178 * first copy up to the current edge and then, if needed,
4179 * fall-through to memcpy_send_wqe.
Erez Shitritf0313962016-02-21 16:27:17 +02004180 */
Guy Levi34f4c952018-11-26 08:15:50 +02004181 copysz = min_t(u64, *cur_edge - (void *)eseg->inline_hdr.start,
4182 left);
4183 memcpy(eseg->inline_hdr.start, pdata, copysz);
4184 stride = ALIGN(sizeof(struct mlx5_wqe_eth_seg) -
4185 sizeof(eseg->inline_hdr.start) + copysz, 16);
4186 *size += stride / 16;
4187 *seg += stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004188
Guy Levi34f4c952018-11-26 08:15:50 +02004189 if (copysz < left) {
4190 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02004191 left -= copysz;
4192 pdata += copysz;
Guy Levi34f4c952018-11-26 08:15:50 +02004193 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, pdata,
4194 left);
Erez Shitritf0313962016-02-21 16:27:17 +02004195 }
Guy Levi34f4c952018-11-26 08:15:50 +02004196
4197 return;
Erez Shitritf0313962016-02-21 16:27:17 +02004198 }
4199
Guy Levi34f4c952018-11-26 08:15:50 +02004200 *seg += sizeof(struct mlx5_wqe_eth_seg);
4201 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
Erez Shitritf0313962016-02-21 16:27:17 +02004202}
4203
Eli Cohene126ba92013-07-07 17:25:49 +03004204static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004205 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004206{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004207 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
4208 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
4209 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004210}
4211
4212static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
4213{
4214 dseg->byte_count = cpu_to_be32(sg->length);
4215 dseg->lkey = cpu_to_be32(sg->lkey);
4216 dseg->addr = cpu_to_be64(sg->addr);
4217}
4218
Artemy Kovalyov31616252017-01-02 11:37:42 +02004219static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03004220{
Artemy Kovalyov31616252017-01-02 11:37:42 +02004221 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
4222 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03004223}
4224
Moni Shoua841b07f2019-08-15 11:38:34 +03004225static __be64 frwr_mkey_mask(bool atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004226{
4227 u64 result;
4228
4229 result = MLX5_MKEY_MASK_LEN |
4230 MLX5_MKEY_MASK_PAGE_SIZE |
4231 MLX5_MKEY_MASK_START_ADDR |
4232 MLX5_MKEY_MASK_EN_RINVAL |
4233 MLX5_MKEY_MASK_KEY |
4234 MLX5_MKEY_MASK_LR |
4235 MLX5_MKEY_MASK_LW |
4236 MLX5_MKEY_MASK_RR |
4237 MLX5_MKEY_MASK_RW |
Eli Cohene126ba92013-07-07 17:25:49 +03004238 MLX5_MKEY_MASK_SMALL_FENCE |
4239 MLX5_MKEY_MASK_FREE;
4240
Moni Shoua841b07f2019-08-15 11:38:34 +03004241 if (atomic)
4242 result |= MLX5_MKEY_MASK_A;
4243
Eli Cohene126ba92013-07-07 17:25:49 +03004244 return cpu_to_be64(result);
4245}
4246
Sagi Grimberge6631812014-02-23 14:19:11 +02004247static __be64 sig_mkey_mask(void)
4248{
4249 u64 result;
4250
4251 result = MLX5_MKEY_MASK_LEN |
4252 MLX5_MKEY_MASK_PAGE_SIZE |
4253 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004254 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02004255 MLX5_MKEY_MASK_EN_RINVAL |
4256 MLX5_MKEY_MASK_KEY |
4257 MLX5_MKEY_MASK_LR |
4258 MLX5_MKEY_MASK_LW |
4259 MLX5_MKEY_MASK_RR |
4260 MLX5_MKEY_MASK_RW |
4261 MLX5_MKEY_MASK_SMALL_FENCE |
4262 MLX5_MKEY_MASK_FREE |
4263 MLX5_MKEY_MASK_BSF_EN;
4264
4265 return cpu_to_be64(result);
4266}
4267
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004268static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Moni Shoua841b07f2019-08-15 11:38:34 +03004269 struct mlx5_ib_mr *mr, u8 flags, bool atomic)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004270{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004271 int size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004272
4273 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004274
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004275 umr->flags = flags;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004276 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Moni Shoua841b07f2019-08-15 11:38:34 +03004277 umr->mkey_mask = frwr_mkey_mask(atomic);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004278}
4279
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004280static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03004281{
4282 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004283 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03004284 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03004285}
4286
Artemy Kovalyov31616252017-01-02 11:37:42 +02004287static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004288{
4289 u64 result;
4290
Artemy Kovalyov31616252017-01-02 11:37:42 +02004291 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02004292 MLX5_MKEY_MASK_FREE;
4293
4294 return cpu_to_be64(result);
4295}
4296
Artemy Kovalyov31616252017-01-02 11:37:42 +02004297static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004298{
4299 u64 result;
4300
4301 result = MLX5_MKEY_MASK_FREE;
4302
4303 return cpu_to_be64(result);
4304}
4305
Noa Osherovich56e11d62016-02-29 16:46:51 +02004306static __be64 get_umr_update_translation_mask(void)
4307{
4308 u64 result;
4309
4310 result = MLX5_MKEY_MASK_LEN |
4311 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004312 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004313
4314 return cpu_to_be64(result);
4315}
4316
Artemy Kovalyov31616252017-01-02 11:37:42 +02004317static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02004318{
4319 u64 result;
4320
Artemy Kovalyov31616252017-01-02 11:37:42 +02004321 result = MLX5_MKEY_MASK_LR |
4322 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004323 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004324 MLX5_MKEY_MASK_RW;
4325
4326 if (atomic)
4327 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004328
4329 return cpu_to_be64(result);
4330}
4331
4332static __be64 get_umr_update_pd_mask(void)
4333{
4334 u64 result;
4335
Artemy Kovalyov31616252017-01-02 11:37:42 +02004336 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004337
4338 return cpu_to_be64(result);
4339}
4340
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004341static int umr_check_mkey_mask(struct mlx5_ib_dev *dev, u64 mask)
4342{
4343 if ((mask & MLX5_MKEY_MASK_PAGE_SIZE &&
4344 MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) ||
4345 (mask & MLX5_MKEY_MASK_A &&
4346 MLX5_CAP_GEN(dev->mdev, umr_modify_atomic_disabled)))
4347 return -EPERM;
4348 return 0;
4349}
4350
4351static int set_reg_umr_segment(struct mlx5_ib_dev *dev,
4352 struct mlx5_wqe_umr_ctrl_seg *umr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004353 const struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004354{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004355 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03004356
4357 memset(umr, 0, sizeof(*umr));
4358
Yishai Hadas6a053952019-07-23 09:57:25 +03004359 if (!umrwr->ignore_free_state) {
4360 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
4361 /* fail if free */
4362 umr->flags = MLX5_UMR_CHECK_FREE;
4363 else
4364 /* fail if not free */
4365 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
4366 }
Haggai Eran968e78d2014-12-11 17:04:11 +02004367
Artemy Kovalyov31616252017-01-02 11:37:42 +02004368 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
4369 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
4370 u64 offset = get_xlt_octo(umrwr->offset);
4371
4372 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
4373 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
4374 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004375 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02004376 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
4377 umr->mkey_mask |= get_umr_update_translation_mask();
4378 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
4379 umr->mkey_mask |= get_umr_update_access_mask(atomic);
4380 umr->mkey_mask |= get_umr_update_pd_mask();
4381 }
4382 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
4383 umr->mkey_mask |= get_umr_enable_mr_mask();
4384 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
4385 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03004386
4387 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02004388 umr->flags |= MLX5_UMR_INLINE;
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004389
4390 return umr_check_mkey_mask(dev, be64_to_cpu(umr->mkey_mask));
Eli Cohene126ba92013-07-07 17:25:49 +03004391}
4392
4393static u8 get_umr_flags(int acc)
4394{
4395 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
4396 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
4397 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
4398 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02004399 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004400}
4401
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004402static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
4403 struct mlx5_ib_mr *mr,
4404 u32 key, int access)
4405{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004406 int ndescs = ALIGN(mr->ndescs + mr->meta_ndescs, 8) >> 1;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004407
4408 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004409
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004410 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004411 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004412 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004413 /* KLMs take twice the size of MTTs */
4414 ndescs *= 2;
4415
4416 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004417 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
4418 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
4419 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
4420 seg->len = cpu_to_be64(mr->ibmr.length);
4421 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004422}
4423
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004424static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03004425{
4426 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004427 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004428}
4429
Bart Van Asschef696bf62018-07-18 09:25:14 -07004430static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg,
4431 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004432{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004433 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004434
Eli Cohene126ba92013-07-07 17:25:49 +03004435 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02004436 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02004437 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004438
Haggai Eran968e78d2014-12-11 17:04:11 +02004439 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004440 if (umrwr->pd)
4441 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
4442 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
4443 !umrwr->length)
4444 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
4445
4446 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004447 seg->len = cpu_to_be64(umrwr->length);
4448 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03004449 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02004450 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03004451}
4452
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004453static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
4454 struct mlx5_ib_mr *mr,
4455 struct mlx5_ib_pd *pd)
4456{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004457 int bcount = mr->desc_size * (mr->ndescs + mr->meta_ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004458
4459 dseg->addr = cpu_to_be64(mr->desc_map);
4460 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
4461 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
4462}
4463
Bart Van Asschef696bf62018-07-18 09:25:14 -07004464static __be32 send_ieth(const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004465{
4466 switch (wr->opcode) {
4467 case IB_WR_SEND_WITH_IMM:
4468 case IB_WR_RDMA_WRITE_WITH_IMM:
4469 return wr->ex.imm_data;
4470
4471 case IB_WR_SEND_WITH_INV:
4472 return cpu_to_be32(wr->ex.invalidate_rkey);
4473
4474 default:
4475 return 0;
4476 }
4477}
4478
4479static u8 calc_sig(void *wqe, int size)
4480{
4481 u8 *p = wqe;
4482 u8 res = 0;
4483 int i;
4484
4485 for (i = 0; i < size; i++)
4486 res ^= p[i];
4487
4488 return ~res;
4489}
4490
4491static u8 wq_sig(void *wqe)
4492{
4493 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
4494}
4495
Bart Van Asschef696bf62018-07-18 09:25:14 -07004496static int set_data_inl_seg(struct mlx5_ib_qp *qp, const struct ib_send_wr *wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004497 void **wqe, int *wqe_sz, void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004498{
4499 struct mlx5_wqe_inline_seg *seg;
Guy Levi34f4c952018-11-26 08:15:50 +02004500 size_t offset;
Eli Cohene126ba92013-07-07 17:25:49 +03004501 int inl = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004502 int i;
4503
Guy Levi34f4c952018-11-26 08:15:50 +02004504 seg = *wqe;
4505 *wqe += sizeof(*seg);
4506 offset = sizeof(*seg);
4507
Eli Cohene126ba92013-07-07 17:25:49 +03004508 for (i = 0; i < wr->num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02004509 size_t len = wr->sg_list[i].length;
4510 void *addr = (void *)(unsigned long)(wr->sg_list[i].addr);
4511
Eli Cohene126ba92013-07-07 17:25:49 +03004512 inl += len;
4513
4514 if (unlikely(inl > qp->max_inline_data))
4515 return -ENOMEM;
4516
Guy Levi34f4c952018-11-26 08:15:50 +02004517 while (likely(len)) {
4518 size_t leftlen;
4519 size_t copysz;
4520
4521 handle_post_send_edge(&qp->sq, wqe,
4522 *wqe_sz + (offset >> 4),
4523 cur_edge);
4524
4525 leftlen = *cur_edge - *wqe;
4526 copysz = min_t(size_t, leftlen, len);
4527
4528 memcpy(*wqe, addr, copysz);
4529 len -= copysz;
4530 addr += copysz;
4531 *wqe += copysz;
4532 offset += copysz;
Eli Cohene126ba92013-07-07 17:25:49 +03004533 }
Eli Cohene126ba92013-07-07 17:25:49 +03004534 }
4535
4536 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
4537
Guy Levi34f4c952018-11-26 08:15:50 +02004538 *wqe_sz += ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004539
4540 return 0;
4541}
4542
Sagi Grimberge6631812014-02-23 14:19:11 +02004543static u16 prot_field_size(enum ib_signature_type type)
4544{
4545 switch (type) {
4546 case IB_SIG_TYPE_T10_DIF:
4547 return MLX5_DIF_SIZE;
4548 default:
4549 return 0;
4550 }
4551}
4552
4553static u8 bs_selector(int block_size)
4554{
4555 switch (block_size) {
4556 case 512: return 0x1;
4557 case 520: return 0x2;
4558 case 4096: return 0x3;
4559 case 4160: return 0x4;
4560 case 1073741824: return 0x5;
4561 default: return 0;
4562 }
4563}
4564
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004565static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
4566 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02004567{
Sagi Grimberg142537f2014-08-13 19:54:32 +03004568 /* Valid inline section and allow BSF refresh */
4569 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
4570 MLX5_BSF_REFRESH_DIF);
4571 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
4572 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004573 /* repeating block */
4574 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
4575 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
4576 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004577
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004578 if (domain->sig.dif.ref_remap)
4579 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02004580
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004581 if (domain->sig.dif.app_escape) {
4582 if (domain->sig.dif.ref_escape)
4583 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
4584 else
4585 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02004586 }
4587
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004588 inl->dif_app_bitmask_check =
4589 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02004590}
4591
4592static int mlx5_set_bsf(struct ib_mr *sig_mr,
4593 struct ib_sig_attrs *sig_attrs,
4594 struct mlx5_bsf *bsf, u32 data_size)
4595{
4596 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
4597 struct mlx5_bsf_basic *basic = &bsf->basic;
4598 struct ib_sig_domain *mem = &sig_attrs->mem;
4599 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02004600
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004601 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02004602
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004603 /* Basic + Extended + Inline */
4604 basic->bsf_size_sbs = 1 << 7;
4605 /* Input domain check byte mask */
4606 basic->check_byte_mask = sig_attrs->check_mask;
4607 basic->raw_data_size = cpu_to_be32(data_size);
4608
4609 /* Memory domain */
4610 switch (sig_attrs->mem.sig_type) {
4611 case IB_SIG_TYPE_NONE:
4612 break;
4613 case IB_SIG_TYPE_T10_DIF:
4614 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
4615 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
4616 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
4617 break;
4618 default:
4619 return -EINVAL;
4620 }
4621
4622 /* Wire domain */
4623 switch (sig_attrs->wire.sig_type) {
4624 case IB_SIG_TYPE_NONE:
4625 break;
4626 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02004627 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004628 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004629 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03004630 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02004631 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004632 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004633 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004634 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004635 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004636 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02004637 } else
4638 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
4639
Sagi Grimberg142537f2014-08-13 19:54:32 +03004640 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004641 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02004642 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004643 default:
4644 return -EINVAL;
4645 }
4646
4647 return 0;
4648}
4649
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004650static int set_sig_data_segment(const struct ib_send_wr *send_wr,
4651 struct ib_mr *sig_mr,
4652 struct ib_sig_attrs *sig_attrs,
4653 struct mlx5_ib_qp *qp, void **seg, int *size,
4654 void **cur_edge)
Sagi Grimberge6631812014-02-23 14:19:11 +02004655{
Sagi Grimberge6631812014-02-23 14:19:11 +02004656 struct mlx5_bsf *bsf;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004657 u32 data_len;
4658 u32 data_key;
4659 u64 data_va;
4660 u32 prot_len = 0;
4661 u32 prot_key = 0;
4662 u64 prot_va = 0;
4663 bool prot = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004664 int ret;
4665 int wqe_size;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004666 struct mlx5_ib_mr *mr = to_mmr(sig_mr);
4667 struct mlx5_ib_mr *pi_mr = mr->pi_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004668
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004669 data_len = pi_mr->data_length;
4670 data_key = pi_mr->ibmr.lkey;
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03004671 data_va = pi_mr->data_iova;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004672 if (pi_mr->meta_ndescs) {
4673 prot_len = pi_mr->meta_length;
4674 prot_key = pi_mr->ibmr.lkey;
Israel Rukshinde0ae952019-06-11 18:52:55 +03004675 prot_va = pi_mr->pi_iova;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004676 prot = true;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004677 }
4678
4679 if (!prot || (data_key == prot_key && data_va == prot_va &&
4680 data_len == prot_len)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004681 /**
4682 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004683 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02004684 * So need construct:
4685 * ------------------
4686 * | data_klm |
4687 * ------------------
4688 * | BSF |
4689 * ------------------
4690 **/
4691 struct mlx5_klm *data_klm = *seg;
4692
4693 data_klm->bcount = cpu_to_be32(data_len);
4694 data_klm->key = cpu_to_be32(data_key);
4695 data_klm->va = cpu_to_be64(data_va);
4696 wqe_size = ALIGN(sizeof(*data_klm), 64);
4697 } else {
4698 /**
4699 * Source domain contains signature information
4700 * So need construct a strided block format:
4701 * ---------------------------
4702 * | stride_block_ctrl |
4703 * ---------------------------
4704 * | data_klm |
4705 * ---------------------------
4706 * | prot_klm |
4707 * ---------------------------
4708 * | BSF |
4709 * ---------------------------
4710 **/
4711 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
4712 struct mlx5_stride_block_entry *data_sentry;
4713 struct mlx5_stride_block_entry *prot_sentry;
Sagi Grimberge6631812014-02-23 14:19:11 +02004714 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
4715 int prot_size;
4716
4717 sblock_ctrl = *seg;
4718 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
4719 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
4720
4721 prot_size = prot_field_size(sig_attrs->mem.sig_type);
4722 if (!prot_size) {
4723 pr_err("Bad block size given: %u\n", block_size);
4724 return -EINVAL;
4725 }
4726 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
4727 prot_size);
4728 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
4729 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
4730 sblock_ctrl->num_entries = cpu_to_be16(2);
4731
4732 data_sentry->bcount = cpu_to_be16(block_size);
4733 data_sentry->key = cpu_to_be32(data_key);
4734 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004735 data_sentry->stride = cpu_to_be16(block_size);
4736
Sagi Grimberge6631812014-02-23 14:19:11 +02004737 prot_sentry->bcount = cpu_to_be16(prot_size);
4738 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004739 prot_sentry->va = cpu_to_be64(prot_va);
4740 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004741
Sagi Grimberge6631812014-02-23 14:19:11 +02004742 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
4743 sizeof(*prot_sentry), 64);
4744 }
4745
4746 *seg += wqe_size;
4747 *size += wqe_size / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004748 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004749
4750 bsf = *seg;
4751 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
4752 if (ret)
4753 return -EINVAL;
4754
4755 *seg += sizeof(*bsf);
4756 *size += sizeof(*bsf) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004757 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004758
4759 return 0;
4760}
4761
4762static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004763 struct ib_mr *sig_mr, int access_flags,
4764 u32 size, u32 length, u32 pdn)
Sagi Grimberge6631812014-02-23 14:19:11 +02004765{
Sagi Grimberge6631812014-02-23 14:19:11 +02004766 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004767 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004768
4769 memset(seg, 0, sizeof(*seg));
4770
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004771 seg->flags = get_umr_flags(access_flags) | MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004772 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004773 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004774 MLX5_MKEY_BSF_EN | pdn);
4775 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004776 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004777 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4778}
4779
4780static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004781 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004782{
4783 memset(umr, 0, sizeof(*umr));
4784
4785 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004786 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004787 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4788 umr->mkey_mask = sig_mkey_mask();
4789}
4790
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004791static int set_pi_umr_wr(const struct ib_send_wr *send_wr,
4792 struct mlx5_ib_qp *qp, void **seg, int *size,
4793 void **cur_edge)
4794{
4795 const struct ib_reg_wr *wr = reg_wr(send_wr);
4796 struct mlx5_ib_mr *sig_mr = to_mmr(wr->mr);
4797 struct mlx5_ib_mr *pi_mr = sig_mr->pi_mr;
4798 struct ib_sig_attrs *sig_attrs = sig_mr->ibmr.sig_attrs;
4799 u32 pdn = get_pd(qp)->pdn;
4800 u32 xlt_size;
4801 int region_len, ret;
4802
4803 if (unlikely(send_wr->num_sge != 0) ||
4804 unlikely(wr->access & IB_ACCESS_REMOTE_ATOMIC) ||
Max Gurtovoy185eddc2019-06-11 18:52:51 +03004805 unlikely(!sig_mr->sig) || unlikely(!qp->ibqp.integrity_en) ||
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004806 unlikely(!sig_mr->sig->sig_status_checked))
4807 return -EINVAL;
4808
4809 /* length of the protected region, data + protection */
4810 region_len = pi_mr->ibmr.length;
4811
4812 /**
4813 * KLM octoword size - if protection was provided
4814 * then we use strided block format (3 octowords),
4815 * else we use single KLM (1 octoword)
4816 **/
4817 if (sig_attrs->mem.sig_type != IB_SIG_TYPE_NONE)
4818 xlt_size = 0x30;
4819 else
4820 xlt_size = sizeof(struct mlx5_klm);
4821
4822 set_sig_umr_segment(*seg, xlt_size);
4823 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4824 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4825 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4826
4827 set_sig_mkey_segment(*seg, wr->mr, wr->access, xlt_size, region_len,
4828 pdn);
4829 *seg += sizeof(struct mlx5_mkey_seg);
4830 *size += sizeof(struct mlx5_mkey_seg) / 16;
4831 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4832
4833 ret = set_sig_data_segment(send_wr, wr->mr, sig_attrs, qp, seg, size,
4834 cur_edge);
4835 if (ret)
4836 return ret;
4837
4838 sig_mr->sig->sig_status_checked = false;
4839 return 0;
4840}
Sagi Grimberge6631812014-02-23 14:19:11 +02004841
Sagi Grimberge6631812014-02-23 14:19:11 +02004842static int set_psv_wr(struct ib_sig_domain *domain,
4843 u32 psv_idx, void **seg, int *size)
4844{
4845 struct mlx5_seg_set_psv *psv_seg = *seg;
4846
4847 memset(psv_seg, 0, sizeof(*psv_seg));
4848 psv_seg->psv_num = cpu_to_be32(psv_idx);
4849 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004850 case IB_SIG_TYPE_NONE:
4851 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004852 case IB_SIG_TYPE_T10_DIF:
4853 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4854 domain->sig.dif.app_tag);
4855 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004856 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004857 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004858 pr_err("Bad signature type (%d) is given.\n",
4859 domain->sig_type);
4860 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004861 }
4862
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004863 *seg += sizeof(*psv_seg);
4864 *size += sizeof(*psv_seg) / 16;
4865
Sagi Grimberge6631812014-02-23 14:19:11 +02004866 return 0;
4867}
4868
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004869static int set_reg_wr(struct mlx5_ib_qp *qp,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004870 const struct ib_reg_wr *wr,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004871 void **seg, int *size, void **cur_edge,
4872 bool check_not_free)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004873{
4874 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4875 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
Moni Shoua841b07f2019-08-15 11:38:34 +03004876 struct mlx5_ib_dev *dev = to_mdev(pd->ibpd.device);
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004877 int mr_list_size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Idan Burstein064e5262018-05-02 13:16:39 +03004878 bool umr_inline = mr_list_size <= MLX5_IB_SQ_UMR_INLINE_THRESHOLD;
Moni Shoua841b07f2019-08-15 11:38:34 +03004879 bool atomic = wr->access & IB_ACCESS_REMOTE_ATOMIC;
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004880 u8 flags = 0;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004881
Michael Guralnikd6de0bb2020-01-08 20:05:40 +02004882 if (!mlx5_ib_can_use_umr(dev, atomic, wr->access)) {
Moni Shoua841b07f2019-08-15 11:38:34 +03004883 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4884 "Fast update of %s for MR is disabled\n",
4885 (MLX5_CAP_GEN(dev->mdev,
4886 umr_modify_entity_size_disabled)) ?
4887 "entity size" :
4888 "atomic access");
4889 return -EINVAL;
4890 }
4891
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004892 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4893 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4894 "Invalid IB_SEND_INLINE send flag\n");
4895 return -EINVAL;
4896 }
4897
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004898 if (check_not_free)
4899 flags |= MLX5_UMR_CHECK_NOT_FREE;
4900 if (umr_inline)
4901 flags |= MLX5_UMR_INLINE;
4902
Moni Shoua841b07f2019-08-15 11:38:34 +03004903 set_reg_umr_seg(*seg, mr, flags, atomic);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004904 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4905 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004906 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004907
4908 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4909 *seg += sizeof(struct mlx5_mkey_seg);
4910 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004911 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004912
Idan Burstein064e5262018-05-02 13:16:39 +03004913 if (umr_inline) {
Guy Levi34f4c952018-11-26 08:15:50 +02004914 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, mr->descs,
4915 mr_list_size);
4916 *size = ALIGN(*size, MLX5_SEND_WQE_BB >> 4);
Idan Burstein064e5262018-05-02 13:16:39 +03004917 } else {
4918 set_reg_data_seg(*seg, mr, pd);
4919 *seg += sizeof(struct mlx5_wqe_data_seg);
4920 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4921 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004922 return 0;
4923}
4924
Guy Levi34f4c952018-11-26 08:15:50 +02004925static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size,
4926 void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004927{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004928 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004929 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4930 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004931 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004932 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004933 *seg += sizeof(struct mlx5_mkey_seg);
4934 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004935 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03004936}
4937
Guy Levi34f4c952018-11-26 08:15:50 +02004938static void dump_wqe(struct mlx5_ib_qp *qp, u32 idx, int size_16)
Eli Cohene126ba92013-07-07 17:25:49 +03004939{
4940 __be32 *p = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004941 int i, j;
4942
Guy Levi34f4c952018-11-26 08:15:50 +02004943 pr_debug("dump WQE index %u:\n", idx);
Eli Cohene126ba92013-07-07 17:25:49 +03004944 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4945 if ((i & 0xf) == 0) {
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004946 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, idx);
Guy Levi34f4c952018-11-26 08:15:50 +02004947 pr_debug("WQBB at %p:\n", (void *)p);
Eli Cohene126ba92013-07-07 17:25:49 +03004948 j = 0;
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004949 idx = (idx + 1) & (qp->sq.wqe_cnt - 1);
Eli Cohene126ba92013-07-07 17:25:49 +03004950 }
4951 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4952 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4953 be32_to_cpu(p[j + 3]));
4954 }
4955}
4956
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004957static int __begin_wqe(struct mlx5_ib_qp *qp, void **seg,
Guy Levi34f4c952018-11-26 08:15:50 +02004958 struct mlx5_wqe_ctrl_seg **ctrl,
4959 const struct ib_send_wr *wr, unsigned int *idx,
4960 int *size, void **cur_edge, int nreq,
4961 bool send_signaled, bool solicited)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004962{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004963 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4964 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004965
4966 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
Guy Levi34f4c952018-11-26 08:15:50 +02004967 *seg = mlx5_frag_buf_get_wqe(&qp->sq.fbc, *idx);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004968 *ctrl = *seg;
4969 *(uint32_t *)(*seg + 8) = 0;
4970 (*ctrl)->imm = send_ieth(wr);
4971 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004972 (send_signaled ? MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4973 (solicited ? MLX5_WQE_CTRL_SOLICITED : 0);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004974
4975 *seg += sizeof(**ctrl);
4976 *size = sizeof(**ctrl) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004977 *cur_edge = qp->sq.cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004978
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004979 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004980}
4981
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004982static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4983 struct mlx5_wqe_ctrl_seg **ctrl,
4984 const struct ib_send_wr *wr, unsigned *idx,
Guy Levi34f4c952018-11-26 08:15:50 +02004985 int *size, void **cur_edge, int nreq)
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004986{
Guy Levi34f4c952018-11-26 08:15:50 +02004987 return __begin_wqe(qp, seg, ctrl, wr, idx, size, cur_edge, nreq,
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004988 wr->send_flags & IB_SEND_SIGNALED,
4989 wr->send_flags & IB_SEND_SOLICITED);
4990}
4991
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004992static void finish_wqe(struct mlx5_ib_qp *qp,
4993 struct mlx5_wqe_ctrl_seg *ctrl,
Guy Levi34f4c952018-11-26 08:15:50 +02004994 void *seg, u8 size, void *cur_edge,
4995 unsigned int idx, u64 wr_id, int nreq, u8 fence,
4996 u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004997{
4998 u8 opmod = 0;
4999
5000 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
5001 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02005002 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005003 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005004 if (unlikely(qp->wq_sig))
5005 ctrl->signature = wq_sig(ctrl);
5006
5007 qp->sq.wrid[idx] = wr_id;
5008 qp->sq.w_list[idx].opcode = mlx5_opcode;
5009 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
5010 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
5011 qp->sq.w_list[idx].next = qp->sq.cur_post;
Guy Levi34f4c952018-11-26 08:15:50 +02005012
5013 /* We save the edge which was possibly updated during the WQE
5014 * construction, into SQ's cache.
5015 */
5016 seg = PTR_ALIGN(seg, MLX5_SEND_WQE_BB);
5017 qp->sq.cur_edge = (unlikely(seg == cur_edge)) ?
5018 get_sq_edge(&qp->sq, qp->sq.cur_post &
5019 (qp->sq.wqe_cnt - 1)) :
5020 cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005021}
5022
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005023static int _mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
5024 const struct ib_send_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03005025{
5026 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
5027 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03005028 struct mlx5_core_dev *mdev = dev->mdev;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005029 struct ib_reg_wr reg_pi_wr;
Haggai Erand16e91d2016-02-29 15:45:05 +02005030 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02005031 struct mlx5_ib_mr *mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005032 struct mlx5_ib_mr *pi_mr;
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005033 struct mlx5_ib_mr pa_pi_mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005034 struct ib_sig_attrs *sig_attrs;
Eli Cohene126ba92013-07-07 17:25:49 +03005035 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02005036 struct mlx5_bf *bf;
Guy Levi34f4c952018-11-26 08:15:50 +02005037 void *cur_edge;
Eli Cohene126ba92013-07-07 17:25:49 +03005038 int uninitialized_var(size);
Eli Cohene126ba92013-07-07 17:25:49 +03005039 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03005040 unsigned idx;
5041 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03005042 int num_sge;
5043 void *seg;
5044 int nreq;
5045 int i;
5046 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03005047 u8 fence;
5048
Parav Pandit6c755202018-08-28 14:45:29 +03005049 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5050 !drain)) {
5051 *bad_wr = wr;
5052 return -EIO;
5053 }
5054
Haggai Erand16e91d2016-02-29 15:45:05 +02005055 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5056 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
5057
5058 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005059 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02005060
Eli Cohene126ba92013-07-07 17:25:49 +03005061 spin_lock_irqsave(&qp->sq.lock, flags);
5062
5063 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04005064 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03005065 mlx5_ib_warn(dev, "\n");
5066 err = -EINVAL;
5067 *bad_wr = wr;
5068 goto out;
5069 }
5070
Eli Cohene126ba92013-07-07 17:25:49 +03005071 num_sge = wr->num_sge;
5072 if (unlikely(num_sge > qp->sq.max_gs)) {
5073 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03005074 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03005075 *bad_wr = wr;
5076 goto out;
5077 }
5078
Guy Levi34f4c952018-11-26 08:15:50 +02005079 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, &cur_edge,
5080 nreq);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005081 if (err) {
5082 mlx5_ib_warn(dev, "\n");
5083 err = -ENOMEM;
5084 *bad_wr = wr;
5085 goto out;
5086 }
Eli Cohene126ba92013-07-07 17:25:49 +03005087
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005088 if (wr->opcode == IB_WR_REG_MR ||
5089 wr->opcode == IB_WR_REG_MR_INTEGRITY) {
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005090 fence = dev->umr_fence;
5091 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Majd Dibbiny074fca32018-11-05 08:07:37 +02005092 } else {
5093 if (wr->send_flags & IB_SEND_FENCE) {
5094 if (qp->next_fence)
5095 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
5096 else
5097 fence = MLX5_FENCE_MODE_FENCE;
5098 } else {
5099 fence = qp->next_fence;
5100 }
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005101 }
5102
Eli Cohene126ba92013-07-07 17:25:49 +03005103 switch (ibqp->qp_type) {
5104 case IB_QPT_XRC_INI:
5105 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03005106 seg += sizeof(*xrc);
5107 size += sizeof(*xrc) / 16;
5108 /* fall through */
5109 case IB_QPT_RC:
5110 switch (wr->opcode) {
5111 case IB_WR_RDMA_READ:
5112 case IB_WR_RDMA_WRITE:
5113 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005114 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5115 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005116 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005117 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5118 break;
5119
5120 case IB_WR_ATOMIC_CMP_AND_SWP:
5121 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03005122 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03005123 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
5124 err = -ENOSYS;
5125 *bad_wr = wr;
5126 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005127
5128 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03005129 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
5130 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Guy Levi34f4c952018-11-26 08:15:50 +02005131 set_linv_wr(qp, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005132 num_sge = 0;
5133 break;
5134
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005135 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005136 qp->sq.wr_data[idx] = IB_WR_REG_MR;
5137 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
Guy Levi34f4c952018-11-26 08:15:50 +02005138 err = set_reg_wr(qp, reg_wr(wr), &seg, &size,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03005139 &cur_edge, true);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005140 if (err) {
5141 *bad_wr = wr;
5142 goto out;
5143 }
5144 num_sge = 0;
5145 break;
5146
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005147 case IB_WR_REG_MR_INTEGRITY:
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005148 qp->sq.wr_data[idx] = IB_WR_REG_MR_INTEGRITY;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005149
5150 mr = to_mmr(reg_wr(wr)->mr);
5151 pi_mr = mr->pi_mr;
5152
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005153 if (pi_mr) {
5154 memset(&reg_pi_wr, 0,
5155 sizeof(struct ib_reg_wr));
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005156
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005157 reg_pi_wr.mr = &pi_mr->ibmr;
5158 reg_pi_wr.access = reg_wr(wr)->access;
5159 reg_pi_wr.key = pi_mr->ibmr.rkey;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005160
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005161 ctrl->imm = cpu_to_be32(reg_pi_wr.key);
5162 /* UMR for data + prot registration */
5163 err = set_reg_wr(qp, &reg_pi_wr, &seg,
5164 &size, &cur_edge,
5165 false);
5166 if (err) {
5167 *bad_wr = wr;
5168 goto out;
5169 }
5170 finish_wqe(qp, ctrl, seg, size,
5171 cur_edge, idx, wr->wr_id,
5172 nreq, fence,
5173 MLX5_OPCODE_UMR);
5174
5175 err = begin_wqe(qp, &seg, &ctrl, wr,
5176 &idx, &size, &cur_edge,
5177 nreq);
5178 if (err) {
5179 mlx5_ib_warn(dev, "\n");
5180 err = -ENOMEM;
5181 *bad_wr = wr;
5182 goto out;
5183 }
5184 } else {
5185 memset(&pa_pi_mr, 0,
5186 sizeof(struct mlx5_ib_mr));
5187 /* No UMR, use local_dma_lkey */
5188 pa_pi_mr.ibmr.lkey =
5189 mr->ibmr.pd->local_dma_lkey;
5190
5191 pa_pi_mr.ndescs = mr->ndescs;
5192 pa_pi_mr.data_length = mr->data_length;
5193 pa_pi_mr.data_iova = mr->data_iova;
5194 if (mr->meta_ndescs) {
5195 pa_pi_mr.meta_ndescs =
5196 mr->meta_ndescs;
5197 pa_pi_mr.meta_length =
5198 mr->meta_length;
5199 pa_pi_mr.pi_iova = mr->pi_iova;
5200 }
5201
5202 pa_pi_mr.ibmr.length = mr->ibmr.length;
5203 mr->pi_mr = &pa_pi_mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005204 }
5205 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
5206 /* UMR for sig MR */
5207 err = set_pi_umr_wr(wr, qp, &seg, &size,
5208 &cur_edge);
5209 if (err) {
5210 mlx5_ib_warn(dev, "\n");
5211 *bad_wr = wr;
5212 goto out;
5213 }
5214 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5215 wr->wr_id, nreq, fence,
5216 MLX5_OPCODE_UMR);
5217
5218 /*
5219 * SET_PSV WQEs are not signaled and solicited
5220 * on error
5221 */
5222 sig_attrs = mr->ibmr.sig_attrs;
5223 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5224 &size, &cur_edge, nreq, false,
5225 true);
5226 if (err) {
5227 mlx5_ib_warn(dev, "\n");
5228 err = -ENOMEM;
5229 *bad_wr = wr;
5230 goto out;
5231 }
5232 err = set_psv_wr(&sig_attrs->mem,
5233 mr->sig->psv_memory.psv_idx,
5234 &seg, &size);
5235 if (err) {
5236 mlx5_ib_warn(dev, "\n");
5237 *bad_wr = wr;
5238 goto out;
5239 }
5240 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5241 wr->wr_id, nreq, next_fence,
5242 MLX5_OPCODE_SET_PSV);
5243
5244 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5245 &size, &cur_edge, nreq, false,
5246 true);
5247 if (err) {
5248 mlx5_ib_warn(dev, "\n");
5249 err = -ENOMEM;
5250 *bad_wr = wr;
5251 goto out;
5252 }
5253 err = set_psv_wr(&sig_attrs->wire,
5254 mr->sig->psv_wire.psv_idx,
5255 &seg, &size);
5256 if (err) {
5257 mlx5_ib_warn(dev, "\n");
5258 *bad_wr = wr;
5259 goto out;
5260 }
5261 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5262 wr->wr_id, nreq, next_fence,
5263 MLX5_OPCODE_SET_PSV);
5264
5265 qp->next_fence =
5266 MLX5_FENCE_MODE_INITIATOR_SMALL;
5267 num_sge = 0;
5268 goto skip_psv;
5269
Eli Cohene126ba92013-07-07 17:25:49 +03005270 default:
5271 break;
5272 }
5273 break;
5274
5275 case IB_QPT_UC:
5276 switch (wr->opcode) {
5277 case IB_WR_RDMA_WRITE:
5278 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005279 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5280 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03005281 seg += sizeof(struct mlx5_wqe_raddr_seg);
5282 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5283 break;
5284
5285 default:
5286 break;
5287 }
5288 break;
5289
Eli Cohene126ba92013-07-07 17:25:49 +03005290 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02005291 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
5292 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
5293 err = -EPERM;
5294 *bad_wr = wr;
5295 goto out;
5296 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07005297 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02005298 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03005299 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005300 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005301 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005302 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
5303
Eli Cohene126ba92013-07-07 17:25:49 +03005304 break;
Erez Shitritf0313962016-02-21 16:27:17 +02005305 case IB_QPT_UD:
5306 set_datagram_seg(seg, wr);
5307 seg += sizeof(struct mlx5_wqe_datagram_seg);
5308 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005309 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005310
5311 /* handle qp that supports ud offload */
5312 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
5313 struct mlx5_wqe_eth_pad *pad;
5314
5315 pad = seg;
5316 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
5317 seg += sizeof(struct mlx5_wqe_eth_pad);
5318 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005319 set_eth_seg(wr, qp, &seg, &size, &cur_edge);
5320 handle_post_send_edge(&qp->sq, &seg, size,
5321 &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005322 }
5323 break;
Eli Cohene126ba92013-07-07 17:25:49 +03005324 case MLX5_IB_QPT_REG_UMR:
5325 if (wr->opcode != MLX5_IB_WR_UMR) {
5326 err = -EINVAL;
5327 mlx5_ib_warn(dev, "bad opcode\n");
5328 goto out;
5329 }
5330 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005331 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02005332 err = set_reg_umr_segment(dev, seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
5333 if (unlikely(err))
5334 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005335 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
5336 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005337 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005338 set_reg_mkey_segment(seg, wr);
5339 seg += sizeof(struct mlx5_mkey_seg);
5340 size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005341 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005342 break;
5343
5344 default:
5345 break;
5346 }
5347
5348 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
Guy Levi34f4c952018-11-26 08:15:50 +02005349 err = set_data_inl_seg(qp, wr, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005350 if (unlikely(err)) {
5351 mlx5_ib_warn(dev, "\n");
5352 *bad_wr = wr;
5353 goto out;
5354 }
Eli Cohene126ba92013-07-07 17:25:49 +03005355 } else {
Eli Cohene126ba92013-07-07 17:25:49 +03005356 for (i = 0; i < num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02005357 handle_post_send_edge(&qp->sq, &seg, size,
5358 &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005359 if (likely(wr->sg_list[i].length)) {
Guy Levi34f4c952018-11-26 08:15:50 +02005360 set_data_ptr_seg
5361 ((struct mlx5_wqe_data_seg *)seg,
5362 wr->sg_list + i);
Eli Cohene126ba92013-07-07 17:25:49 +03005363 size += sizeof(struct mlx5_wqe_data_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005364 seg += sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005365 }
5366 }
5367 }
5368
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005369 qp->next_fence = next_fence;
Guy Levi34f4c952018-11-26 08:15:50 +02005370 finish_wqe(qp, ctrl, seg, size, cur_edge, idx, wr->wr_id, nreq,
5371 fence, mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02005372skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03005373 if (0)
5374 dump_wqe(qp, idx, size);
5375 }
5376
5377out:
5378 if (likely(nreq)) {
5379 qp->sq.head += nreq;
5380
5381 /* Make sure that descriptors are written before
5382 * updating doorbell record and ringing the doorbell
5383 */
5384 wmb();
5385
5386 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
5387
Eli Cohenada388f2014-01-14 17:45:16 +02005388 /* Make sure doorbell record is visible to the HCA before
5389 * we hit doorbell */
5390 wmb();
5391
Maxim Mikityanskiybbf29f62019-03-29 15:37:52 -07005392 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005393 /* Make sure doorbells don't leak out of SQ spinlock
5394 * and reach the HCA out of order.
5395 */
Eli Cohene126ba92013-07-07 17:25:49 +03005396 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03005397 }
5398
5399 spin_unlock_irqrestore(&qp->sq.lock, flags);
5400
5401 return err;
5402}
5403
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005404int mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
5405 const struct ib_send_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005406{
5407 return _mlx5_ib_post_send(ibqp, wr, bad_wr, false);
5408}
5409
Eli Cohene126ba92013-07-07 17:25:49 +03005410static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
5411{
5412 sig->signature = calc_sig(sig, size);
5413}
5414
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005415static int _mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5416 const struct ib_recv_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03005417{
5418 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5419 struct mlx5_wqe_data_seg *scat;
5420 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03005421 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5422 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005423 unsigned long flags;
5424 int err = 0;
5425 int nreq;
5426 int ind;
5427 int i;
5428
Parav Pandit6c755202018-08-28 14:45:29 +03005429 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5430 !drain)) {
5431 *bad_wr = wr;
5432 return -EIO;
5433 }
5434
Haggai Erand16e91d2016-02-29 15:45:05 +02005435 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5436 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
5437
Eli Cohene126ba92013-07-07 17:25:49 +03005438 spin_lock_irqsave(&qp->rq.lock, flags);
5439
5440 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
5441
5442 for (nreq = 0; wr; nreq++, wr = wr->next) {
5443 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
5444 err = -ENOMEM;
5445 *bad_wr = wr;
5446 goto out;
5447 }
5448
5449 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
5450 err = -EINVAL;
5451 *bad_wr = wr;
5452 goto out;
5453 }
5454
Guy Levi34f4c952018-11-26 08:15:50 +02005455 scat = mlx5_frag_buf_get_wqe(&qp->rq.fbc, ind);
Eli Cohene126ba92013-07-07 17:25:49 +03005456 if (qp->wq_sig)
5457 scat++;
5458
5459 for (i = 0; i < wr->num_sge; i++)
5460 set_data_ptr_seg(scat + i, wr->sg_list + i);
5461
5462 if (i < qp->rq.max_gs) {
5463 scat[i].byte_count = 0;
5464 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
5465 scat[i].addr = 0;
5466 }
5467
5468 if (qp->wq_sig) {
5469 sig = (struct mlx5_rwqe_sig *)scat;
5470 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
5471 }
5472
5473 qp->rq.wrid[ind] = wr->wr_id;
5474
5475 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
5476 }
5477
5478out:
5479 if (likely(nreq)) {
5480 qp->rq.head += nreq;
5481
5482 /* Make sure that descriptors are written before
5483 * doorbell record.
5484 */
5485 wmb();
5486
5487 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
5488 }
5489
5490 spin_unlock_irqrestore(&qp->rq.lock, flags);
5491
5492 return err;
5493}
5494
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005495int mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5496 const struct ib_recv_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005497{
5498 return _mlx5_ib_post_recv(ibqp, wr, bad_wr, false);
5499}
5500
Eli Cohene126ba92013-07-07 17:25:49 +03005501static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
5502{
5503 switch (mlx5_state) {
5504 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
5505 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
5506 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
5507 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
5508 case MLX5_QP_STATE_SQ_DRAINING:
5509 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
5510 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
5511 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
5512 default: return -1;
5513 }
5514}
5515
5516static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
5517{
5518 switch (mlx5_mig_state) {
5519 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
5520 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
5521 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
5522 default: return -1;
5523 }
5524}
5525
5526static int to_ib_qp_access_flags(int mlx5_flags)
5527{
5528 int ib_flags = 0;
5529
5530 if (mlx5_flags & MLX5_QP_BIT_RRE)
5531 ib_flags |= IB_ACCESS_REMOTE_READ;
5532 if (mlx5_flags & MLX5_QP_BIT_RWE)
5533 ib_flags |= IB_ACCESS_REMOTE_WRITE;
5534 if (mlx5_flags & MLX5_QP_BIT_RAE)
5535 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
5536
5537 return ib_flags;
5538}
5539
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005540static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005541 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005542 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03005543{
Eli Cohene126ba92013-07-07 17:25:49 +03005544
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005545 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03005546
Jason Gunthorpee7996a92018-01-29 13:26:40 -07005547 if (!path->port || path->port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03005548 return;
5549
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02005550 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
5551
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005552 rdma_ah_set_port_num(ah_attr, path->port);
5553 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03005554
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005555 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
5556 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
5557 rdma_ah_set_static_rate(ah_attr,
5558 path->static_rate ? path->static_rate - 5 : 0);
5559 if (path->grh_mlid & (1 << 7)) {
5560 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
5561
5562 rdma_ah_set_grh(ah_attr, NULL,
5563 tc_fl & 0xfffff,
5564 path->mgid_index,
5565 path->hop_limit,
5566 (tc_fl >> 20) & 0xff);
5567 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03005568 }
5569}
5570
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005571static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
5572 struct mlx5_ib_sq *sq,
5573 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03005574{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005575 int err;
5576
Eran Ben Elisha28160772017-12-26 15:17:05 +02005577 err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005578 if (err)
5579 goto out;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005580 sq->state = *sq_state;
5581
5582out:
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005583 return err;
5584}
5585
5586static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
5587 struct mlx5_ib_rq *rq,
5588 u8 *rq_state)
5589{
5590 void *out;
5591 void *rqc;
5592 int inlen;
5593 int err;
5594
5595 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005596 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005597 if (!out)
5598 return -ENOMEM;
5599
5600 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
5601 if (err)
5602 goto out;
5603
5604 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
5605 *rq_state = MLX5_GET(rqc, rqc, state);
5606 rq->state = *rq_state;
5607
5608out:
5609 kvfree(out);
5610 return err;
5611}
5612
5613static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
5614 struct mlx5_ib_qp *qp, u8 *qp_state)
5615{
5616 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
5617 [MLX5_RQC_STATE_RST] = {
5618 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5619 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5620 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
5621 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
5622 },
5623 [MLX5_RQC_STATE_RDY] = {
5624 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5625 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5626 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
5627 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
5628 },
5629 [MLX5_RQC_STATE_ERR] = {
5630 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5631 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5632 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
5633 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
5634 },
5635 [MLX5_RQ_STATE_NA] = {
5636 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5637 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5638 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
5639 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
5640 },
5641 };
5642
5643 *qp_state = sqrq_trans[rq_state][sq_state];
5644
5645 if (*qp_state == MLX5_QP_STATE_BAD) {
5646 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
5647 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
5648 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
5649 return -EINVAL;
5650 }
5651
5652 if (*qp_state == MLX5_QP_STATE)
5653 *qp_state = qp->state;
5654
5655 return 0;
5656}
5657
5658static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
5659 struct mlx5_ib_qp *qp,
5660 u8 *raw_packet_qp_state)
5661{
5662 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
5663 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
5664 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
5665 int err;
5666 u8 sq_state = MLX5_SQ_STATE_NA;
5667 u8 rq_state = MLX5_RQ_STATE_NA;
5668
5669 if (qp->sq.wqe_cnt) {
5670 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
5671 if (err)
5672 return err;
5673 }
5674
5675 if (qp->rq.wqe_cnt) {
5676 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
5677 if (err)
5678 return err;
5679 }
5680
5681 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
5682 raw_packet_qp_state);
5683}
5684
5685static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
5686 struct ib_qp_attr *qp_attr)
5687{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005688 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03005689 struct mlx5_qp_context *context;
5690 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005691 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03005692 int err = 0;
5693
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005694 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005695 if (!outb)
5696 return -ENOMEM;
5697
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03005698 err = mlx5_core_qp_query(dev, &qp->trans_qp.base.mqp, outb, outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03005699 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005700 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005701
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005702 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
5703 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
5704
Eli Cohene126ba92013-07-07 17:25:49 +03005705 mlx5_state = be32_to_cpu(context->flags) >> 28;
5706
5707 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03005708 qp_attr->path_mtu = context->mtu_msgmax >> 5;
5709 qp_attr->path_mig_state =
5710 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
5711 qp_attr->qkey = be32_to_cpu(context->qkey);
5712 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
5713 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
5714 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
5715 qp_attr->qp_access_flags =
5716 to_ib_qp_access_flags(be32_to_cpu(context->params2));
5717
5718 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005719 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
5720 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005721 qp_attr->alt_pkey_index =
5722 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005723 qp_attr->alt_port_num =
5724 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03005725 }
5726
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005727 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03005728 qp_attr->port_num = context->pri_path.port;
5729
5730 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
5731 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
5732
5733 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
5734
5735 qp_attr->max_dest_rd_atomic =
5736 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
5737 qp_attr->min_rnr_timer =
5738 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
5739 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
5740 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
5741 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
5742 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005743
5744out:
5745 kfree(outb);
5746 return err;
5747}
5748
Moni Shoua776a3902018-01-02 16:19:33 +02005749static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
5750 struct ib_qp_attr *qp_attr, int qp_attr_mask,
5751 struct ib_qp_init_attr *qp_init_attr)
5752{
5753 struct mlx5_core_dct *dct = &mqp->dct.mdct;
5754 u32 *out;
5755 u32 access_flags = 0;
5756 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
5757 void *dctc;
5758 int err;
5759 int supported_mask = IB_QP_STATE |
5760 IB_QP_ACCESS_FLAGS |
5761 IB_QP_PORT |
5762 IB_QP_MIN_RNR_TIMER |
5763 IB_QP_AV |
5764 IB_QP_PATH_MTU |
5765 IB_QP_PKEY_INDEX;
5766
5767 if (qp_attr_mask & ~supported_mask)
5768 return -EINVAL;
5769 if (mqp->state != IB_QPS_RTR)
5770 return -EINVAL;
5771
5772 out = kzalloc(outlen, GFP_KERNEL);
5773 if (!out)
5774 return -ENOMEM;
5775
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03005776 err = mlx5_core_dct_query(dev, dct, out, outlen);
Moni Shoua776a3902018-01-02 16:19:33 +02005777 if (err)
5778 goto out;
5779
5780 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
5781
5782 if (qp_attr_mask & IB_QP_STATE)
5783 qp_attr->qp_state = IB_QPS_RTR;
5784
5785 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
5786 if (MLX5_GET(dctc, dctc, rre))
5787 access_flags |= IB_ACCESS_REMOTE_READ;
5788 if (MLX5_GET(dctc, dctc, rwe))
5789 access_flags |= IB_ACCESS_REMOTE_WRITE;
5790 if (MLX5_GET(dctc, dctc, rae))
5791 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
5792 qp_attr->qp_access_flags = access_flags;
5793 }
5794
5795 if (qp_attr_mask & IB_QP_PORT)
5796 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
5797 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
5798 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
5799 if (qp_attr_mask & IB_QP_AV) {
5800 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
5801 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
5802 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
5803 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
5804 }
5805 if (qp_attr_mask & IB_QP_PATH_MTU)
5806 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
5807 if (qp_attr_mask & IB_QP_PKEY_INDEX)
5808 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
5809out:
5810 kfree(out);
5811 return err;
5812}
5813
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005814int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5815 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
5816{
5817 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5818 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5819 int err = 0;
5820 u8 raw_packet_qp_state;
5821
Yishai Hadas28d61372016-05-23 15:20:56 +03005822 if (ibqp->rwq_ind_tbl)
5823 return -ENOSYS;
5824
Haggai Erand16e91d2016-02-29 15:45:05 +02005825 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5826 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
5827 qp_init_attr);
5828
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005829 /* Not all of output fields are applicable, make sure to zero them */
5830 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5831 memset(qp_attr, 0, sizeof(*qp_attr));
5832
Moni Shoua776a3902018-01-02 16:19:33 +02005833 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
5834 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
5835 qp_attr_mask, qp_init_attr);
5836
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005837 mutex_lock(&qp->mutex);
5838
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005839 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
5840 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005841 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
5842 if (err)
5843 goto out;
5844 qp->state = raw_packet_qp_state;
5845 qp_attr->port_num = 1;
5846 } else {
5847 err = query_qp_attr(dev, qp, qp_attr);
5848 if (err)
5849 goto out;
5850 }
5851
5852 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03005853 qp_attr->cur_qp_state = qp_attr->qp_state;
5854 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
5855 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
5856
5857 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005858 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005859 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005860 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005861 } else {
5862 qp_attr->cap.max_send_wr = 0;
5863 qp_attr->cap.max_send_sge = 0;
5864 }
5865
Noa Osherovich0540d812016-06-04 15:15:32 +03005866 qp_init_attr->qp_type = ibqp->qp_type;
5867 qp_init_attr->recv_cq = ibqp->recv_cq;
5868 qp_init_attr->send_cq = ibqp->send_cq;
5869 qp_init_attr->srq = ibqp->srq;
5870 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005871
5872 qp_init_attr->cap = qp_attr->cap;
5873
5874 qp_init_attr->create_flags = 0;
5875 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
5876 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
5877
Leon Romanovsky051f2632015-12-20 12:16:11 +02005878 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
5879 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
5880 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
5881 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
5882 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
5883 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02005884 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
Michael Guralnik3f89b012019-10-20 09:43:59 +03005885 qp_init_attr->create_flags |= MLX5_IB_QP_CREATE_SQPN_QP1;
Leon Romanovsky051f2632015-12-20 12:16:11 +02005886
Eli Cohene126ba92013-07-07 17:25:49 +03005887 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5888 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5889
Eli Cohene126ba92013-07-07 17:25:49 +03005890out:
5891 mutex_unlock(&qp->mutex);
5892 return err;
5893}
5894
5895struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03005896 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005897{
5898 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5899 struct mlx5_ib_xrcd *xrcd;
5900 int err;
5901
Saeed Mahameed938fe832015-05-28 22:28:41 +03005902 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005903 return ERR_PTR(-ENOSYS);
5904
5905 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5906 if (!xrcd)
5907 return ERR_PTR(-ENOMEM);
5908
Yishai Hadas5aa37712018-11-26 08:28:38 +02005909 err = mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03005910 if (err) {
5911 kfree(xrcd);
5912 return ERR_PTR(-ENOMEM);
5913 }
5914
5915 return &xrcd->ibxrcd;
5916}
5917
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005918int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005919{
5920 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5921 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
5922 int err;
5923
Yishai Hadas5aa37712018-11-26 08:28:38 +02005924 err = mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, 0);
Leon Romanovskyb0818082018-01-28 11:25:30 +02005925 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03005926 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005927
5928 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03005929 return 0;
5930}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005931
Yishai Hadas350d0e42016-08-28 14:58:18 +03005932static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5933{
5934 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5935 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5936 struct ib_event event;
5937
5938 if (rwq->ibwq.event_handler) {
5939 event.device = rwq->ibwq.device;
5940 event.element.wq = &rwq->ibwq;
5941 switch (type) {
5942 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5943 event.event = IB_EVENT_WQ_FATAL;
5944 break;
5945 default:
5946 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5947 return;
5948 }
5949
5950 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5951 }
5952}
5953
Maor Gottlieb03404e82017-05-30 10:29:13 +03005954static int set_delay_drop(struct mlx5_ib_dev *dev)
5955{
5956 int err = 0;
5957
5958 mutex_lock(&dev->delay_drop.lock);
5959 if (dev->delay_drop.activate)
5960 goto out;
5961
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03005962 err = mlx5_core_set_delay_drop(dev, dev->delay_drop.timeout);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005963 if (err)
5964 goto out;
5965
5966 dev->delay_drop.activate = true;
5967out:
5968 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005969
5970 if (!err)
5971 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005972 return err;
5973}
5974
Yishai Hadas79b20a62016-05-23 15:20:50 +03005975static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5976 struct ib_wq_init_attr *init_attr)
5977{
5978 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005979 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005980 __be64 *rq_pas0;
5981 void *in;
5982 void *rqc;
5983 void *wq;
5984 int inlen;
5985 int err;
5986
5987 dev = to_mdev(pd->device);
5988
5989 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005990 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005991 if (!in)
5992 return -ENOMEM;
5993
Yishai Hadas34d57582018-09-20 21:39:21 +03005994 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005995 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
5996 MLX5_SET(rqc, rqc, mem_rq_type,
5997 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
5998 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
5999 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
6000 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
6001 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
6002 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03006003 MLX5_SET(wq, wq, wq_type,
6004 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
6005 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02006006 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6007 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
6008 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
6009 err = -EOPNOTSUPP;
6010 goto out;
6011 } else {
6012 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
6013 }
6014 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006015 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03006016 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
Mark Zhangc16339b2019-11-15 17:45:55 +02006017 /*
6018 * In Firmware number of strides in each WQE is:
6019 * "512 * 2^single_wqe_log_num_of_strides"
6020 * Values 3 to 8 are accepted as 10 to 15, 9 to 18 are
6021 * accepted as 0 to 9
6022 */
6023 static const u8 fw_map[] = { 10, 11, 12, 13, 14, 15, 0, 1,
6024 2, 3, 4, 5, 6, 7, 8, 9 };
Noa Osherovichccc87082017-10-17 18:01:13 +03006025 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
6026 MLX5_SET(wq, wq, log_wqe_stride_size,
6027 rwq->single_stride_log_num_of_bytes -
6028 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
Mark Zhangc16339b2019-11-15 17:45:55 +02006029 MLX5_SET(wq, wq, log_wqe_num_of_strides,
6030 fw_map[rwq->log_num_strides -
6031 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES]);
Noa Osherovichccc87082017-10-17 18:01:13 +03006032 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006033 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
6034 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
6035 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
6036 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
6037 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
6038 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02006039 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006040 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02006041 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006042 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
6043 err = -EOPNOTSUPP;
6044 goto out;
6045 }
6046 } else {
6047 MLX5_SET(rqc, rqc, vsd, 1);
6048 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02006049 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
6050 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
6051 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
6052 err = -EOPNOTSUPP;
6053 goto out;
6054 }
6055 MLX5_SET(rqc, rqc, scatter_fcs, 1);
6056 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03006057 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
6058 if (!(dev->ib_dev.attrs.raw_packet_caps &
6059 IB_RAW_PACKET_CAP_DELAY_DROP)) {
6060 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
6061 err = -EOPNOTSUPP;
6062 goto out;
6063 }
6064 MLX5_SET(rqc, rqc, delay_drop_en, 1);
6065 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006066 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
6067 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03006068 err = mlx5_core_create_rq_tracked(dev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03006069 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
6070 err = set_delay_drop(dev);
6071 if (err) {
6072 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
6073 err);
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03006074 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03006075 } else {
6076 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
6077 }
6078 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006079out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03006080 kvfree(in);
6081 return err;
6082}
6083
6084static int set_user_rq_size(struct mlx5_ib_dev *dev,
6085 struct ib_wq_init_attr *wq_init_attr,
6086 struct mlx5_ib_create_wq *ucmd,
6087 struct mlx5_ib_rwq *rwq)
6088{
6089 /* Sanity check RQ size before proceeding */
6090 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
6091 return -EINVAL;
6092
6093 if (!ucmd->rq_wqe_count)
6094 return -EINVAL;
6095
6096 rwq->wqe_count = ucmd->rq_wqe_count;
6097 rwq->wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky0dfe4522018-08-01 14:25:41 -07006098 if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
6099 return -EINVAL;
6100
Yishai Hadas79b20a62016-05-23 15:20:50 +03006101 rwq->log_rq_stride = rwq->wqe_shift;
6102 rwq->log_rq_size = ilog2(rwq->wqe_count);
6103 return 0;
6104}
6105
Mark Zhangc16339b2019-11-15 17:45:55 +02006106static bool log_of_strides_valid(struct mlx5_ib_dev *dev, u32 log_num_strides)
6107{
6108 if ((log_num_strides > MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
6109 (log_num_strides < MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
6110 return false;
6111
6112 if (!MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) &&
6113 (log_num_strides < MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
6114 return false;
6115
6116 return true;
6117}
6118
Yishai Hadas79b20a62016-05-23 15:20:50 +03006119static int prepare_user_rq(struct ib_pd *pd,
6120 struct ib_wq_init_attr *init_attr,
6121 struct ib_udata *udata,
6122 struct mlx5_ib_rwq *rwq)
6123{
6124 struct mlx5_ib_dev *dev = to_mdev(pd->device);
6125 struct mlx5_ib_create_wq ucmd = {};
6126 int err;
6127 size_t required_cmd_sz;
6128
Noa Osherovichccc87082017-10-17 18:01:13 +03006129 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
6130 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006131 if (udata->inlen < required_cmd_sz) {
6132 mlx5_ib_dbg(dev, "invalid inlen\n");
6133 return -EINVAL;
6134 }
6135
6136 if (udata->inlen > sizeof(ucmd) &&
6137 !ib_is_udata_cleared(udata, sizeof(ucmd),
6138 udata->inlen - sizeof(ucmd))) {
6139 mlx5_ib_dbg(dev, "inlen is not supported\n");
6140 return -EOPNOTSUPP;
6141 }
6142
6143 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
6144 mlx5_ib_dbg(dev, "copy failed\n");
6145 return -EFAULT;
6146 }
6147
Noa Osherovichccc87082017-10-17 18:01:13 +03006148 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03006149 mlx5_ib_dbg(dev, "invalid comp mask\n");
6150 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03006151 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
6152 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
6153 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
6154 return -EOPNOTSUPP;
6155 }
6156 if ((ucmd.single_stride_log_num_of_bytes <
6157 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
6158 (ucmd.single_stride_log_num_of_bytes >
6159 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
6160 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
6161 ucmd.single_stride_log_num_of_bytes,
6162 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
6163 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
6164 return -EINVAL;
6165 }
Mark Zhangc16339b2019-11-15 17:45:55 +02006166 if (!log_of_strides_valid(dev,
6167 ucmd.single_wqe_log_num_of_strides)) {
6168 mlx5_ib_dbg(
6169 dev,
6170 "Invalid log num strides (%u. Range is %u - %u)\n",
6171 ucmd.single_wqe_log_num_of_strides,
6172 MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) ?
6173 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES :
6174 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
6175 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
Noa Osherovichccc87082017-10-17 18:01:13 +03006176 return -EINVAL;
6177 }
6178 rwq->single_stride_log_num_of_bytes =
6179 ucmd.single_stride_log_num_of_bytes;
6180 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
6181 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
6182 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006183 }
6184
6185 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
6186 if (err) {
6187 mlx5_ib_dbg(dev, "err %d\n", err);
6188 return err;
6189 }
6190
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02006191 err = create_user_rq(dev, pd, udata, rwq, &ucmd);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006192 if (err) {
6193 mlx5_ib_dbg(dev, "err %d\n", err);
Gal Pressman645ba592018-10-08 19:44:03 +03006194 return err;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006195 }
6196
6197 rwq->user_index = ucmd.user_index;
6198 return 0;
6199}
6200
6201struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
6202 struct ib_wq_init_attr *init_attr,
6203 struct ib_udata *udata)
6204{
6205 struct mlx5_ib_dev *dev;
6206 struct mlx5_ib_rwq *rwq;
6207 struct mlx5_ib_create_wq_resp resp = {};
6208 size_t min_resp_len;
6209 int err;
6210
6211 if (!udata)
6212 return ERR_PTR(-ENOSYS);
6213
6214 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6215 if (udata->outlen && udata->outlen < min_resp_len)
6216 return ERR_PTR(-EINVAL);
6217
Maor Gottliebba800132020-03-22 14:49:06 +02006218 if (!capable(CAP_SYS_RAWIO) &&
6219 init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP)
6220 return ERR_PTR(-EPERM);
6221
Yishai Hadas79b20a62016-05-23 15:20:50 +03006222 dev = to_mdev(pd->device);
6223 switch (init_attr->wq_type) {
6224 case IB_WQT_RQ:
6225 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
6226 if (!rwq)
6227 return ERR_PTR(-ENOMEM);
6228 err = prepare_user_rq(pd, init_attr, udata, rwq);
6229 if (err)
6230 goto err;
6231 err = create_rq(rwq, pd, init_attr);
6232 if (err)
6233 goto err_user_rq;
6234 break;
6235 default:
6236 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
6237 init_attr->wq_type);
6238 return ERR_PTR(-EINVAL);
6239 }
6240
Yishai Hadas350d0e42016-08-28 14:58:18 +03006241 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006242 rwq->ibwq.state = IB_WQS_RESET;
6243 if (udata->outlen) {
6244 resp.response_length = offsetof(typeof(resp), response_length) +
6245 sizeof(resp.response_length);
6246 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6247 if (err)
6248 goto err_copy;
6249 }
6250
Yishai Hadas350d0e42016-08-28 14:58:18 +03006251 rwq->core_qp.event = mlx5_ib_wq_event;
6252 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006253 return &rwq->ibwq;
6254
6255err_copy:
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03006256 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006257err_user_rq:
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006258 destroy_user_rq(dev, pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006259err:
6260 kfree(rwq);
6261 return ERR_PTR(err);
6262}
6263
Leon Romanovskya49b1dc2019-06-12 15:27:41 +03006264void mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +03006265{
6266 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6267 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6268
Leon Romanovsky333fbaa2020-04-04 10:40:24 +03006269 mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006270 destroy_user_rq(dev, wq->pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006271 kfree(rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006272}
6273
Yishai Hadasc5f90922016-05-23 15:20:53 +03006274struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
6275 struct ib_rwq_ind_table_init_attr *init_attr,
6276 struct ib_udata *udata)
6277{
6278 struct mlx5_ib_dev *dev = to_mdev(device);
6279 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
6280 int sz = 1 << init_attr->log_ind_tbl_size;
6281 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
6282 size_t min_resp_len;
6283 int inlen;
6284 int err;
6285 int i;
6286 u32 *in;
6287 void *rqtc;
6288
6289 if (udata->inlen > 0 &&
6290 !ib_is_udata_cleared(udata, 0,
6291 udata->inlen))
6292 return ERR_PTR(-EOPNOTSUPP);
6293
Maor Gottliebefd7f402016-10-27 16:36:40 +03006294 if (init_attr->log_ind_tbl_size >
6295 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
6296 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
6297 init_attr->log_ind_tbl_size,
6298 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
6299 return ERR_PTR(-EINVAL);
6300 }
6301
Yishai Hadasc5f90922016-05-23 15:20:53 +03006302 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6303 if (udata->outlen && udata->outlen < min_resp_len)
6304 return ERR_PTR(-EINVAL);
6305
6306 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
6307 if (!rwq_ind_tbl)
6308 return ERR_PTR(-ENOMEM);
6309
6310 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006311 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006312 if (!in) {
6313 err = -ENOMEM;
6314 goto err;
6315 }
6316
6317 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
6318
6319 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
6320 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
6321
6322 for (i = 0; i < sz; i++)
6323 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
6324
Yishai Hadas5deba862018-09-20 21:39:28 +03006325 rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
6326 MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);
6327
Yishai Hadasc5f90922016-05-23 15:20:53 +03006328 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
6329 kvfree(in);
6330
6331 if (err)
6332 goto err;
6333
6334 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
6335 if (udata->outlen) {
6336 resp.response_length = offsetof(typeof(resp), response_length) +
6337 sizeof(resp.response_length);
6338 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6339 if (err)
6340 goto err_copy;
6341 }
6342
6343 return &rwq_ind_tbl->ib_rwq_ind_tbl;
6344
6345err_copy:
Yishai Hadas5deba862018-09-20 21:39:28 +03006346 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006347err:
6348 kfree(rwq_ind_tbl);
6349 return ERR_PTR(err);
6350}
6351
6352int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
6353{
6354 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
6355 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
6356
Yishai Hadas5deba862018-09-20 21:39:28 +03006357 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006358
6359 kfree(rwq_ind_tbl);
6360 return 0;
6361}
6362
Yishai Hadas79b20a62016-05-23 15:20:50 +03006363int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
6364 u32 wq_attr_mask, struct ib_udata *udata)
6365{
6366 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6367 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6368 struct mlx5_ib_modify_wq ucmd = {};
6369 size_t required_cmd_sz;
6370 int curr_wq_state;
6371 int wq_state;
6372 int inlen;
6373 int err;
6374 void *rqc;
6375 void *in;
6376
6377 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
6378 if (udata->inlen < required_cmd_sz)
6379 return -EINVAL;
6380
6381 if (udata->inlen > sizeof(ucmd) &&
6382 !ib_is_udata_cleared(udata, sizeof(ucmd),
6383 udata->inlen - sizeof(ucmd)))
6384 return -EOPNOTSUPP;
6385
6386 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
6387 return -EFAULT;
6388
6389 if (ucmd.comp_mask || ucmd.reserved)
6390 return -EOPNOTSUPP;
6391
6392 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006393 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006394 if (!in)
6395 return -ENOMEM;
6396
6397 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
6398
6399 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
6400 wq_attr->curr_wq_state : wq->state;
6401 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
6402 wq_attr->wq_state : curr_wq_state;
6403 if (curr_wq_state == IB_WQS_ERR)
6404 curr_wq_state = MLX5_RQC_STATE_ERR;
6405 if (wq_state == IB_WQS_ERR)
6406 wq_state = MLX5_RQC_STATE_ERR;
6407 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
Yishai Hadas34d57582018-09-20 21:39:21 +03006408 MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006409 MLX5_SET(rqc, rqc, state, wq_state);
6410
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006411 if (wq_attr_mask & IB_WQ_FLAGS) {
6412 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
6413 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
6414 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
6415 mlx5_ib_dbg(dev, "VLAN offloads are not "
6416 "supported\n");
6417 err = -EOPNOTSUPP;
6418 goto out;
6419 }
6420 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6421 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
6422 MLX5_SET(rqc, rqc, vsd,
6423 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
6424 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02006425
6426 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6427 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
6428 err = -EOPNOTSUPP;
6429 goto out;
6430 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006431 }
6432
Majd Dibbiny23a69642017-01-18 15:25:10 +02006433 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03006434 u16 set_id;
6435
6436 set_id = mlx5_ib_get_counters_id(dev, 0);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006437 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
6438 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6439 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandit3e1f0002019-07-23 10:31:17 +03006440 MLX5_SET(rqc, rqc, counter_set_id, set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006441 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06006442 dev_info_once(
6443 &dev->ib_dev.dev,
6444 "Receive WQ counters are not supported on current FW\n");
Majd Dibbiny23a69642017-01-18 15:25:10 +02006445 }
6446
Yishai Hadas350d0e42016-08-28 14:58:18 +03006447 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006448 if (!err)
6449 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
6450
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006451out:
6452 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006453 return err;
6454}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006455
6456struct mlx5_ib_drain_cqe {
6457 struct ib_cqe cqe;
6458 struct completion done;
6459};
6460
6461static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
6462{
6463 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
6464 struct mlx5_ib_drain_cqe,
6465 cqe);
6466
6467 complete(&cqe->done);
6468}
6469
6470/* This function returns only once the drained WR was completed */
6471static void handle_drain_completion(struct ib_cq *cq,
6472 struct mlx5_ib_drain_cqe *sdrain,
6473 struct mlx5_ib_dev *dev)
6474{
6475 struct mlx5_core_dev *mdev = dev->mdev;
6476
6477 if (cq->poll_ctx == IB_POLL_DIRECT) {
6478 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
6479 ib_process_cq_direct(cq, -1);
6480 return;
6481 }
6482
6483 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6484 struct mlx5_ib_cq *mcq = to_mcq(cq);
6485 bool triggered = false;
6486 unsigned long flags;
6487
6488 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
6489 /* Make sure that the CQ handler won't run if wasn't run yet */
6490 if (!mcq->mcq.reset_notify_added)
6491 mcq->mcq.reset_notify_added = 1;
6492 else
6493 triggered = true;
6494 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
6495
6496 if (triggered) {
6497 /* Wait for any scheduled/running task to be ended */
6498 switch (cq->poll_ctx) {
6499 case IB_POLL_SOFTIRQ:
6500 irq_poll_disable(&cq->iop);
6501 irq_poll_enable(&cq->iop);
6502 break;
6503 case IB_POLL_WORKQUEUE:
6504 cancel_work_sync(&cq->work);
6505 break;
6506 default:
6507 WARN_ON_ONCE(1);
6508 }
6509 }
6510
6511 /* Run the CQ handler - this makes sure that the drain WR will
6512 * be processed if wasn't processed yet.
6513 */
Yishai Hadas4e0e2ea2019-06-30 19:23:27 +03006514 mcq->mcq.comp(&mcq->mcq, NULL);
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006515 }
6516
6517 wait_for_completion(&sdrain->done);
6518}
6519
6520void mlx5_ib_drain_sq(struct ib_qp *qp)
6521{
6522 struct ib_cq *cq = qp->send_cq;
6523 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6524 struct mlx5_ib_drain_cqe sdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006525 const struct ib_send_wr *bad_swr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006526 struct ib_rdma_wr swr = {
6527 .wr = {
6528 .next = NULL,
6529 { .wr_cqe = &sdrain.cqe, },
6530 .opcode = IB_WR_RDMA_WRITE,
6531 },
6532 };
6533 int ret;
6534 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6535 struct mlx5_core_dev *mdev = dev->mdev;
6536
6537 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6538 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6539 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6540 return;
6541 }
6542
6543 sdrain.cqe.done = mlx5_ib_drain_qp_done;
6544 init_completion(&sdrain.done);
6545
6546 ret = _mlx5_ib_post_send(qp, &swr.wr, &bad_swr, true);
6547 if (ret) {
6548 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6549 return;
6550 }
6551
6552 handle_drain_completion(cq, &sdrain, dev);
6553}
6554
6555void mlx5_ib_drain_rq(struct ib_qp *qp)
6556{
6557 struct ib_cq *cq = qp->recv_cq;
6558 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6559 struct mlx5_ib_drain_cqe rdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006560 struct ib_recv_wr rwr = {};
6561 const struct ib_recv_wr *bad_rwr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006562 int ret;
6563 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6564 struct mlx5_core_dev *mdev = dev->mdev;
6565
6566 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6567 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6568 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6569 return;
6570 }
6571
6572 rwr.wr_cqe = &rdrain.cqe;
6573 rdrain.cqe.done = mlx5_ib_drain_qp_done;
6574 init_completion(&rdrain.done);
6575
6576 ret = _mlx5_ib_post_recv(qp, &rwr, &bad_rwr, true);
6577 if (ret) {
6578 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6579 return;
6580 }
6581
6582 handle_drain_completion(cq, &rdrain, dev);
6583}
Mark Zhangd14133d2019-07-02 13:02:36 +03006584
6585/**
6586 * Bind a qp to a counter. If @counter is NULL then bind the qp to
6587 * the default counter
6588 */
6589int mlx5_ib_qp_set_counter(struct ib_qp *qp, struct rdma_counter *counter)
6590{
Mark Zhang10189e82020-01-26 19:17:08 +02006591 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Mark Zhangd14133d2019-07-02 13:02:36 +03006592 struct mlx5_ib_qp *mqp = to_mqp(qp);
6593 int err = 0;
6594
6595 mutex_lock(&mqp->mutex);
6596 if (mqp->state == IB_QPS_RESET) {
6597 qp->counter = counter;
6598 goto out;
6599 }
6600
Mark Zhang10189e82020-01-26 19:17:08 +02006601 if (!MLX5_CAP_GEN(dev->mdev, rts2rts_qp_counters_set_id)) {
6602 err = -EOPNOTSUPP;
6603 goto out;
6604 }
6605
Mark Zhangd14133d2019-07-02 13:02:36 +03006606 if (mqp->state == IB_QPS_RTS) {
6607 err = __mlx5_ib_qp_set_counter(qp, counter);
6608 if (!err)
6609 qp->counter = counter;
6610
6611 goto out;
6612 }
6613
6614 mqp->counter_pending = 1;
6615 qp->counter = counter;
6616
6617out:
6618 mutex_unlock(&mqp->mutex);
6619 return err;
6620}