blob: bb3f432e2fb6e35abe4403edc38cb6f00d0abd7a [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"
Eli Cohene126ba92013-07-07 17:25:49 +030042
43/* not supported currently */
44static int wq_signature;
45
46enum {
47 MLX5_IB_ACK_REQ_FREQ = 8,
48};
49
50enum {
51 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
52 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
53 MLX5_IB_LINK_TYPE_IB = 0,
54 MLX5_IB_LINK_TYPE_ETH = 1
55};
56
57enum {
58 MLX5_IB_SQ_STRIDE = 6,
Idan Burstein064e5262018-05-02 13:16:39 +030059 MLX5_IB_SQ_UMR_INLINE_THRESHOLD = 64,
Eli Cohene126ba92013-07-07 17:25:49 +030060};
61
62static const u32 mlx5_ib_opcode[] = {
63 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020064 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030065 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
66 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
67 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
68 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
69 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
70 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
71 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
72 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030073 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030074 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
75 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
76 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
77};
78
Erez Shitritf0313962016-02-21 16:27:17 +020079struct mlx5_wqe_eth_pad {
80 u8 rsvd0[16];
81};
Eli Cohene126ba92013-07-07 17:25:49 +030082
Alex Veskereb49ab02016-08-28 12:25:53 +030083enum raw_qp_set_mask_map {
84 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020085 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030086};
87
Alex Vesker0680efa2016-08-28 12:25:52 +030088struct mlx5_modify_raw_qp_param {
89 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030090
91 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang61147f32018-03-19 15:10:30 +020092
93 struct mlx5_rate_limit rl;
94
Alex Veskereb49ab02016-08-28 12:25:53 +030095 u8 rq_q_ctr_id;
Mark Blochd5ed8ac2019-03-28 15:27:38 +020096 u16 port;
Alex Vesker0680efa2016-08-28 12:25:52 +030097};
98
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030099static void get_cqs(enum ib_qp_type qp_type,
100 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
101 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
102
Eli Cohene126ba92013-07-07 17:25:49 +0300103static int is_qp0(enum ib_qp_type qp_type)
104{
105 return qp_type == IB_QPT_SMI;
106}
107
Eli Cohene126ba92013-07-07 17:25:49 +0300108static int is_sqp(enum ib_qp_type qp_type)
109{
110 return is_qp0(qp_type) || is_qp1(qp_type);
111}
112
Haggai Eranc1395a22014-12-11 17:04:14 +0200113/**
Moni Shouafbeb4072019-01-22 08:48:46 +0200114 * mlx5_ib_read_user_wqe_common() - Copy a WQE (or part of) from user WQ
115 * to kernel buffer
Haggai Eranc1395a22014-12-11 17:04:14 +0200116 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200117 * @umem: User space memory where the WQ is
118 * @buffer: buffer to copy to
119 * @buflen: buffer length
120 * @wqe_index: index of WQE to copy from
121 * @wq_offset: offset to start of WQ
122 * @wq_wqe_cnt: number of WQEs in WQ
123 * @wq_wqe_shift: log2 of WQE size
124 * @bcnt: number of bytes to copy
125 * @bytes_copied: number of bytes to copy (return value)
Haggai Eranc1395a22014-12-11 17:04:14 +0200126 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200127 * Copies from start of WQE bcnt or less bytes.
128 * Does not gurantee to copy the entire WQE.
Haggai Eranc1395a22014-12-11 17:04:14 +0200129 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200130 * Return: zero on success, or an error code.
Haggai Eranc1395a22014-12-11 17:04:14 +0200131 */
Moni Shouafbeb4072019-01-22 08:48:46 +0200132static int mlx5_ib_read_user_wqe_common(struct ib_umem *umem,
133 void *buffer,
134 u32 buflen,
135 int wqe_index,
136 int wq_offset,
137 int wq_wqe_cnt,
138 int wq_wqe_shift,
139 int bcnt,
140 size_t *bytes_copied)
Haggai Eranc1395a22014-12-11 17:04:14 +0200141{
Moni Shouafbeb4072019-01-22 08:48:46 +0200142 size_t offset = wq_offset + ((wqe_index % wq_wqe_cnt) << wq_wqe_shift);
143 size_t wq_end = wq_offset + (wq_wqe_cnt << wq_wqe_shift);
144 size_t copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200145 int ret;
146
Moni Shouafbeb4072019-01-22 08:48:46 +0200147 /* don't copy more than requested, more than buffer length or
148 * beyond WQ end
149 */
150 copy_length = min_t(u32, buflen, wq_end - offset);
151 copy_length = min_t(u32, copy_length, bcnt);
Haggai Eranc1395a22014-12-11 17:04:14 +0200152
Moni Shouafbeb4072019-01-22 08:48:46 +0200153 ret = ib_umem_copy_from(buffer, umem, offset, copy_length);
Haggai Eranc1395a22014-12-11 17:04:14 +0200154 if (ret)
155 return ret;
156
Moni Shouafbeb4072019-01-22 08:48:46 +0200157 if (!ret && bytes_copied)
158 *bytes_copied = copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200159
Moni Shouafbeb4072019-01-22 08:48:46 +0200160 return 0;
161}
Haggai Eranc1395a22014-12-11 17:04:14 +0200162
Moni Shouafbeb4072019-01-22 08:48:46 +0200163int mlx5_ib_read_user_wqe_sq(struct mlx5_ib_qp *qp,
164 int wqe_index,
165 void *buffer,
166 int buflen,
167 size_t *bc)
168{
169 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
170 struct ib_umem *umem = base->ubuffer.umem;
171 struct mlx5_ib_wq *wq = &qp->sq;
172 struct mlx5_wqe_ctrl_seg *ctrl;
173 size_t bytes_copied;
174 size_t bytes_copied2;
175 size_t wqe_length;
176 int ret;
177 int ds;
Haggai Eranc1395a22014-12-11 17:04:14 +0200178
Moni Shouafbeb4072019-01-22 08:48:46 +0200179 if (buflen < sizeof(*ctrl))
180 return -EINVAL;
181
182 /* at first read as much as possible */
183 ret = mlx5_ib_read_user_wqe_common(umem,
184 buffer,
185 buflen,
186 wqe_index,
187 wq->offset,
188 wq->wqe_cnt,
189 wq->wqe_shift,
190 buflen,
191 &bytes_copied);
Haggai Eranc1395a22014-12-11 17:04:14 +0200192 if (ret)
193 return ret;
194
Moni Shouafbeb4072019-01-22 08:48:46 +0200195 /* we need at least control segment size to proceed */
196 if (bytes_copied < sizeof(*ctrl))
197 return -EINVAL;
198
199 ctrl = buffer;
200 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
201 wqe_length = ds * MLX5_WQE_DS_UNITS;
202
203 /* if we copied enough then we are done */
204 if (bytes_copied >= wqe_length) {
205 *bc = bytes_copied;
206 return 0;
207 }
208
209 /* otherwise this a wrapped around wqe
210 * so read the remaining bytes starting
211 * from wqe_index 0
212 */
213 ret = mlx5_ib_read_user_wqe_common(umem,
214 buffer + bytes_copied,
215 buflen - bytes_copied,
216 0,
217 wq->offset,
218 wq->wqe_cnt,
219 wq->wqe_shift,
220 wqe_length - bytes_copied,
221 &bytes_copied2);
222
223 if (ret)
224 return ret;
225 *bc = bytes_copied + bytes_copied2;
226 return 0;
227}
228
229int mlx5_ib_read_user_wqe_rq(struct mlx5_ib_qp *qp,
230 int wqe_index,
231 void *buffer,
232 int buflen,
233 size_t *bc)
234{
235 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
236 struct ib_umem *umem = base->ubuffer.umem;
237 struct mlx5_ib_wq *wq = &qp->rq;
238 size_t bytes_copied;
239 int ret;
240
241 ret = mlx5_ib_read_user_wqe_common(umem,
242 buffer,
243 buflen,
244 wqe_index,
245 wq->offset,
246 wq->wqe_cnt,
247 wq->wqe_shift,
248 buflen,
249 &bytes_copied);
250
251 if (ret)
252 return ret;
253 *bc = bytes_copied;
254 return 0;
255}
256
257int mlx5_ib_read_user_wqe_srq(struct mlx5_ib_srq *srq,
258 int wqe_index,
259 void *buffer,
260 int buflen,
261 size_t *bc)
262{
263 struct ib_umem *umem = srq->umem;
264 size_t bytes_copied;
265 int ret;
266
267 ret = mlx5_ib_read_user_wqe_common(umem,
268 buffer,
269 buflen,
270 wqe_index,
271 0,
272 srq->msrq.max,
273 srq->msrq.wqe_shift,
274 buflen,
275 &bytes_copied);
276
277 if (ret)
278 return ret;
279 *bc = bytes_copied;
280 return 0;
Haggai Eranc1395a22014-12-11 17:04:14 +0200281}
282
Eli Cohene126ba92013-07-07 17:25:49 +0300283static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
284{
285 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
286 struct ib_event event;
287
majd@mellanox.com19098df2016-01-14 19:13:03 +0200288 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
289 /* This event is only valid for trans_qps */
290 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
291 }
Eli Cohene126ba92013-07-07 17:25:49 +0300292
293 if (ibqp->event_handler) {
294 event.device = ibqp->device;
295 event.element.qp = ibqp;
296 switch (type) {
297 case MLX5_EVENT_TYPE_PATH_MIG:
298 event.event = IB_EVENT_PATH_MIG;
299 break;
300 case MLX5_EVENT_TYPE_COMM_EST:
301 event.event = IB_EVENT_COMM_EST;
302 break;
303 case MLX5_EVENT_TYPE_SQ_DRAINED:
304 event.event = IB_EVENT_SQ_DRAINED;
305 break;
306 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
307 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
308 break;
309 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
310 event.event = IB_EVENT_QP_FATAL;
311 break;
312 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
313 event.event = IB_EVENT_PATH_MIG_ERR;
314 break;
315 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
316 event.event = IB_EVENT_QP_REQ_ERR;
317 break;
318 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
319 event.event = IB_EVENT_QP_ACCESS_ERR;
320 break;
321 default:
322 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
323 return;
324 }
325
326 ibqp->event_handler(&event, ibqp->qp_context);
327 }
328}
329
330static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
331 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
332{
333 int wqe_size;
334 int wq_size;
335
336 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300337 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300338 return -EINVAL;
339
340 if (!has_rq) {
341 qp->rq.max_gs = 0;
342 qp->rq.wqe_cnt = 0;
343 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300344 cap->max_recv_wr = 0;
345 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300346 } else {
347 if (ucmd) {
348 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300349 if (ucmd->rq_wqe_shift > BITS_PER_BYTE * sizeof(ucmd->rq_wqe_shift))
350 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300351 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300352 if ((1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) < qp->wq_sig)
353 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300354 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
355 qp->rq.max_post = qp->rq.wqe_cnt;
356 } else {
357 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
358 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
359 wqe_size = roundup_pow_of_two(wqe_size);
360 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
361 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
362 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300363 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300364 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
365 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300366 MLX5_CAP_GEN(dev->mdev,
367 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300368 return -EINVAL;
369 }
370 qp->rq.wqe_shift = ilog2(wqe_size);
371 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
372 qp->rq.max_post = qp->rq.wqe_cnt;
373 }
374 }
375
376 return 0;
377}
378
Erez Shitritf0313962016-02-21 16:27:17 +0200379static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300380{
Andi Shyti618af382013-07-16 15:35:01 +0200381 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300382
Erez Shitritf0313962016-02-21 16:27:17 +0200383 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300384 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300385 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300386 /* fall through */
387 case IB_QPT_RC:
388 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200389 max(sizeof(struct mlx5_wqe_atomic_seg) +
390 sizeof(struct mlx5_wqe_raddr_seg),
391 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
Idan Burstein064e5262018-05-02 13:16:39 +0300392 sizeof(struct mlx5_mkey_seg) +
393 MLX5_IB_SQ_UMR_INLINE_THRESHOLD /
394 MLX5_IB_UMR_OCTOWORD);
Eli Cohene126ba92013-07-07 17:25:49 +0300395 break;
396
Eli Cohenb125a542013-09-11 16:35:22 +0300397 case IB_QPT_XRC_TGT:
398 return 0;
399
Eli Cohene126ba92013-07-07 17:25:49 +0300400 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300401 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200402 max(sizeof(struct mlx5_wqe_raddr_seg),
403 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
404 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300405 break;
406
407 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200408 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
409 size += sizeof(struct mlx5_wqe_eth_pad) +
410 sizeof(struct mlx5_wqe_eth_seg);
411 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300412 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200413 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300414 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300415 sizeof(struct mlx5_wqe_datagram_seg);
416 break;
417
418 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300419 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300420 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
421 sizeof(struct mlx5_mkey_seg);
422 break;
423
424 default:
425 return -EINVAL;
426 }
427
428 return size;
429}
430
431static int calc_send_wqe(struct ib_qp_init_attr *attr)
432{
433 int inl_size = 0;
434 int size;
435
Erez Shitritf0313962016-02-21 16:27:17 +0200436 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300437 if (size < 0)
438 return size;
439
440 if (attr->cap.max_inline_data) {
441 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
442 attr->cap.max_inline_data;
443 }
444
445 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300446 if (attr->create_flags & IB_QP_CREATE_INTEGRITY_EN &&
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200447 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300448 return MLX5_SIG_WQE_SIZE;
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200449 else
450 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300451}
452
Eli Cohen288c01b2016-10-27 16:36:45 +0300453static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
454{
455 int max_sge;
456
457 if (attr->qp_type == IB_QPT_RC)
458 max_sge = (min_t(int, wqe_size, 512) -
459 sizeof(struct mlx5_wqe_ctrl_seg) -
460 sizeof(struct mlx5_wqe_raddr_seg)) /
461 sizeof(struct mlx5_wqe_data_seg);
462 else if (attr->qp_type == IB_QPT_XRC_INI)
463 max_sge = (min_t(int, wqe_size, 512) -
464 sizeof(struct mlx5_wqe_ctrl_seg) -
465 sizeof(struct mlx5_wqe_xrc_seg) -
466 sizeof(struct mlx5_wqe_raddr_seg)) /
467 sizeof(struct mlx5_wqe_data_seg);
468 else
469 max_sge = (wqe_size - sq_overhead(attr)) /
470 sizeof(struct mlx5_wqe_data_seg);
471
472 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
473 sizeof(struct mlx5_wqe_data_seg));
474}
475
Eli Cohene126ba92013-07-07 17:25:49 +0300476static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
477 struct mlx5_ib_qp *qp)
478{
479 int wqe_size;
480 int wq_size;
481
482 if (!attr->cap.max_send_wr)
483 return 0;
484
485 wqe_size = calc_send_wqe(attr);
486 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
487 if (wqe_size < 0)
488 return wqe_size;
489
Saeed Mahameed938fe832015-05-28 22:28:41 +0300490 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300491 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300492 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300493 return -EINVAL;
494 }
495
Erez Shitritf0313962016-02-21 16:27:17 +0200496 qp->max_inline_data = wqe_size - sq_overhead(attr) -
497 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300498 attr->cap.max_inline_data = qp->max_inline_data;
499
500 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
501 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300502 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800503 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
504 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300505 qp->sq.wqe_cnt,
506 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300507 return -ENOMEM;
508 }
Eli Cohene126ba92013-07-07 17:25:49 +0300509 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300510 qp->sq.max_gs = get_send_sge(attr, wqe_size);
511 if (qp->sq.max_gs < attr->cap.max_send_sge)
512 return -ENOMEM;
513
514 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300515 qp->sq.max_post = wq_size / wqe_size;
516 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300517
518 return wq_size;
519}
520
521static int set_user_buf_size(struct mlx5_ib_dev *dev,
522 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200523 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200524 struct mlx5_ib_qp_base *base,
525 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300526{
527 int desc_sz = 1 << qp->sq.wqe_shift;
528
Saeed Mahameed938fe832015-05-28 22:28:41 +0300529 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300530 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300531 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300532 return -EINVAL;
533 }
534
Gal Pressmanaf8b38e2019-02-06 15:45:35 +0200535 if (ucmd->sq_wqe_count && !is_power_of_2(ucmd->sq_wqe_count)) {
536 mlx5_ib_warn(dev, "sq_wqe_count %d is not a power of two\n",
537 ucmd->sq_wqe_count);
Eli Cohene126ba92013-07-07 17:25:49 +0300538 return -EINVAL;
539 }
540
541 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
542
Saeed Mahameed938fe832015-05-28 22:28:41 +0300543 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300544 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300545 qp->sq.wqe_cnt,
546 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300547 return -EINVAL;
548 }
549
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300550 if (attr->qp_type == IB_QPT_RAW_PACKET ||
551 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200552 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
553 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
554 } else {
555 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
556 (qp->sq.wqe_cnt << 6);
557 }
Eli Cohene126ba92013-07-07 17:25:49 +0300558
559 return 0;
560}
561
562static int qp_has_rq(struct ib_qp_init_attr *attr)
563{
564 if (attr->qp_type == IB_QPT_XRC_INI ||
565 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
566 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
567 !attr->cap.max_recv_wr)
568 return 0;
569
570 return 1;
571}
572
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200573enum {
574 /* this is the first blue flame register in the array of bfregs assigned
575 * to a processes. Since we do not use it for blue flame but rather
576 * regular 64 bit doorbells, we do not need a lock for maintaiing
577 * "odd/even" order
578 */
579 NUM_NON_BLUE_FLAME_BFREGS = 1,
580};
581
Eli Cohenb037c292017-01-03 23:55:26 +0200582static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
583{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200584 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200585}
586
587static int num_med_bfreg(struct mlx5_ib_dev *dev,
588 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200589{
590 int n;
591
Eli Cohenb037c292017-01-03 23:55:26 +0200592 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
593 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200594
595 return n >= 0 ? n : 0;
596}
597
Yishai Hadas18b03622018-05-07 10:20:01 +0300598static int first_med_bfreg(struct mlx5_ib_dev *dev,
599 struct mlx5_bfreg_info *bfregi)
600{
601 return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
602}
603
Eli Cohenb037c292017-01-03 23:55:26 +0200604static int first_hi_bfreg(struct mlx5_ib_dev *dev,
605 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200606{
607 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200608
Eli Cohenb037c292017-01-03 23:55:26 +0200609 med = num_med_bfreg(dev, bfregi);
610 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200611}
612
Eli Cohenb037c292017-01-03 23:55:26 +0200613static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
614 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300615{
Eli Cohene126ba92013-07-07 17:25:49 +0300616 int i;
617
Eli Cohenb037c292017-01-03 23:55:26 +0200618 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
619 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200620 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300621 return i;
622 }
623 }
624
625 return -ENOMEM;
626}
627
Eli Cohenb037c292017-01-03 23:55:26 +0200628static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
629 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300630{
Yishai Hadas18b03622018-05-07 10:20:01 +0300631 int minidx = first_med_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300632 int i;
633
Yishai Hadas18b03622018-05-07 10:20:01 +0300634 if (minidx < 0)
635 return minidx;
636
637 for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200638 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300639 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200640 if (!bfregi->count[minidx])
641 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300642 }
643
Eli Cohen2f5ff262017-01-03 23:55:21 +0200644 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300645 return minidx;
646}
647
Eli Cohenb037c292017-01-03 23:55:26 +0200648static int alloc_bfreg(struct mlx5_ib_dev *dev,
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300649 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300650{
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300651 int bfregn = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +0300652
Eli Cohen2f5ff262017-01-03 23:55:21 +0200653 mutex_lock(&bfregi->lock);
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300654 if (bfregi->ver >= 2) {
655 bfregn = alloc_high_class_bfreg(dev, bfregi);
656 if (bfregn < 0)
657 bfregn = alloc_med_class_bfreg(dev, bfregi);
658 }
659
660 if (bfregn < 0) {
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200661 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200662 bfregn = 0;
663 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300664 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200665 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300666
Eli Cohen2f5ff262017-01-03 23:55:21 +0200667 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300668}
669
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200670void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300671{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200672 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200673 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200674 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300675}
676
677static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
678{
679 switch (state) {
680 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
681 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
682 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
683 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
684 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
685 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
686 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
687 default: return -1;
688 }
689}
690
691static int to_mlx5_st(enum ib_qp_type type)
692{
693 switch (type) {
694 case IB_QPT_RC: return MLX5_QP_ST_RC;
695 case IB_QPT_UC: return MLX5_QP_ST_UC;
696 case IB_QPT_UD: return MLX5_QP_ST_UD;
697 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
698 case IB_QPT_XRC_INI:
699 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
700 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200701 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200702 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300703 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300704 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200705 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300706 case IB_QPT_MAX:
707 default: return -EINVAL;
708 }
709}
710
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300711static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
712 struct mlx5_ib_cq *recv_cq);
713static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
714 struct mlx5_ib_cq *recv_cq);
715
Yishai Hadas7c043e92018-06-17 13:00:03 +0300716int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300717 struct mlx5_bfreg_info *bfregi, u32 bfregn,
Yishai Hadas7c043e92018-06-17 13:00:03 +0300718 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300719{
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300720 unsigned int bfregs_per_sys_page;
721 u32 index_of_sys_page;
722 u32 offset;
Eli Cohenb037c292017-01-03 23:55:26 +0200723
724 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
725 MLX5_NON_FP_BFREGS_PER_UAR;
726 index_of_sys_page = bfregn / bfregs_per_sys_page;
727
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200728 if (dyn_bfreg) {
729 index_of_sys_page += bfregi->num_static_sys_pages;
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300730
731 if (index_of_sys_page >= bfregi->num_sys_pages)
732 return -EINVAL;
733
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200734 if (bfregn > bfregi->num_dyn_bfregs ||
735 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
736 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
737 return -EINVAL;
738 }
739 }
Eli Cohenb037c292017-01-03 23:55:26 +0200740
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200741 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200742 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300743}
744
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200745static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200746 unsigned long addr, size_t size,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200747 struct ib_umem **umem, int *npages, int *page_shift,
748 int *ncont, u32 *offset)
majd@mellanox.com19098df2016-01-14 19:13:03 +0200749{
750 int err;
751
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200752 *umem = ib_umem_get(udata, addr, size, 0, 0);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200753 if (IS_ERR(*umem)) {
754 mlx5_ib_dbg(dev, "umem_get failed\n");
755 return PTR_ERR(*umem);
756 }
757
Majd Dibbiny762f8992016-10-27 16:36:47 +0300758 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200759
760 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
761 if (err) {
762 mlx5_ib_warn(dev, "bad offset\n");
763 goto err_umem;
764 }
765
766 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
767 addr, size, *npages, *page_shift, *ncont, *offset);
768
769 return 0;
770
771err_umem:
772 ib_umem_release(*umem);
773 *umem = NULL;
774
775 return err;
776}
777
Maor Gottliebfe248c32017-05-30 10:29:14 +0300778static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300779 struct mlx5_ib_rwq *rwq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300780{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300781 struct mlx5_ib_ucontext *context =
782 rdma_udata_to_drv_context(
783 udata,
784 struct mlx5_ib_ucontext,
785 ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300786
Maor Gottliebfe248c32017-05-30 10:29:14 +0300787 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
788 atomic_dec(&dev->delay_drop.rqs_cnt);
789
Yishai Hadas79b20a62016-05-23 15:20:50 +0300790 mlx5_ib_db_unmap_user(context, &rwq->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300791 ib_umem_release(rwq->umem);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300792}
793
794static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200795 struct ib_udata *udata, struct mlx5_ib_rwq *rwq,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300796 struct mlx5_ib_create_wq *ucmd)
797{
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200798 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
799 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300800 int page_shift = 0;
801 int npages;
802 u32 offset = 0;
803 int ncont = 0;
804 int err;
805
806 if (!ucmd->buf_addr)
807 return -EINVAL;
808
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200809 rwq->umem = ib_umem_get(udata, ucmd->buf_addr, rwq->buf_size, 0, 0);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300810 if (IS_ERR(rwq->umem)) {
811 mlx5_ib_dbg(dev, "umem_get failed\n");
812 err = PTR_ERR(rwq->umem);
813 return err;
814 }
815
Majd Dibbiny762f8992016-10-27 16:36:47 +0300816 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300817 &ncont, NULL);
818 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
819 &rwq->rq_page_offset);
820 if (err) {
821 mlx5_ib_warn(dev, "bad offset\n");
822 goto err_umem;
823 }
824
825 rwq->rq_num_pas = ncont;
826 rwq->page_shift = page_shift;
827 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
828 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
829
830 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
831 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
832 npages, page_shift, ncont, offset);
833
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200834 err = mlx5_ib_db_map_user(ucontext, udata, ucmd->db_addr, &rwq->db);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300835 if (err) {
836 mlx5_ib_dbg(dev, "map failed\n");
837 goto err_umem;
838 }
839
840 rwq->create_type = MLX5_WQ_USER;
841 return 0;
842
843err_umem:
844 ib_umem_release(rwq->umem);
845 return err;
846}
847
Eli Cohenb037c292017-01-03 23:55:26 +0200848static int adjust_bfregn(struct mlx5_ib_dev *dev,
849 struct mlx5_bfreg_info *bfregi, int bfregn)
850{
851 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
852 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
853}
854
Eli Cohene126ba92013-07-07 17:25:49 +0300855static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
856 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200857 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300858 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200859 struct mlx5_ib_create_qp_resp *resp, int *inlen,
860 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300861{
862 struct mlx5_ib_ucontext *context;
863 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200864 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200865 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200866 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300867 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200868 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200869 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200870 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300871 __be64 *pas;
872 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300873 int err;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200874 u16 uid;
Eli Cohene126ba92013-07-07 17:25:49 +0300875
876 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
877 if (err) {
878 mlx5_ib_dbg(dev, "copy failed\n");
879 return err;
880 }
881
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200882 context = rdma_udata_to_drv_context(udata, struct mlx5_ib_ucontext,
883 ibucontext);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200884 if (ucmd.flags & MLX5_QP_FLAG_BFREG_INDEX) {
885 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
886 ucmd.bfreg_index, true);
887 if (uar_index < 0)
888 return uar_index;
889
890 bfregn = MLX5_IB_INVALID_BFREG;
891 } else if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL) {
892 /*
893 * TBD: should come from the verbs when we have the API
894 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200895 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200896 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200897 }
Leon Romanovsky051f2632015-12-20 12:16:11 +0200898 else {
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300899 bfregn = alloc_bfreg(dev, &context->bfregi);
900 if (bfregn < 0)
901 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300902 }
903
Eli Cohen2f5ff262017-01-03 23:55:21 +0200904 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200905 if (bfregn != MLX5_IB_INVALID_BFREG)
906 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
907 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300908
Haggai Eran48fea832014-05-22 14:50:11 +0300909 qp->rq.offset = 0;
910 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
911 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
912
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200913 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300914 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200915 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300916
majd@mellanox.com19098df2016-01-14 19:13:03 +0200917 if (ucmd.buf_addr && ubuffer->buf_size) {
918 ubuffer->buf_addr = ucmd.buf_addr;
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200919 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr,
920 ubuffer->buf_size, &ubuffer->umem,
921 &npages, &page_shift, &ncont, &offset);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200922 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200923 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200924 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200925 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300926 }
Eli Cohene126ba92013-07-07 17:25:49 +0300927
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300928 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
929 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300930 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300931 if (!*in) {
932 err = -ENOMEM;
933 goto err_umem;
934 }
Eli Cohene126ba92013-07-07 17:25:49 +0300935
Yishai Hadas7422edc2018-12-23 13:12:21 +0200936 uid = (attr->qp_type != IB_QPT_XRC_TGT &&
937 attr->qp_type != IB_QPT_XRC_INI) ? to_mpd(pd)->uid : 0;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200938 MLX5_SET(create_qp_in, *in, uid, uid);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300939 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
940 if (ubuffer->umem)
941 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
942
943 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
944
945 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
946 MLX5_SET(qpc, qpc, page_offset, offset);
947
948 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200949 if (bfregn != MLX5_IB_INVALID_BFREG)
950 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
951 else
952 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200953 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300954
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200955 err = mlx5_ib_db_map_user(context, udata, ucmd.db_addr, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300956 if (err) {
957 mlx5_ib_dbg(dev, "map failed\n");
958 goto err_free;
959 }
960
Jason Gunthorpe41d902c2018-04-03 10:00:53 +0300961 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
Eli Cohene126ba92013-07-07 17:25:49 +0300962 if (err) {
963 mlx5_ib_dbg(dev, "copy failed\n");
964 goto err_unmap;
965 }
966 qp->create_type = MLX5_QP_USER;
967
968 return 0;
969
970err_unmap:
971 mlx5_ib_db_unmap_user(context, &qp->db);
972
973err_free:
Al Viro479163f2014-11-20 08:13:57 +0000974 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300975
976err_umem:
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300977 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300978
Eli Cohen2f5ff262017-01-03 23:55:21 +0200979err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200980 if (bfregn != MLX5_IB_INVALID_BFREG)
981 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300982 return err;
983}
984
Eli Cohenb037c292017-01-03 23:55:26 +0200985static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300986 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base,
987 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +0300988{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300989 struct mlx5_ib_ucontext *context =
990 rdma_udata_to_drv_context(
991 udata,
992 struct mlx5_ib_ucontext,
993 ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +0300994
Eli Cohene126ba92013-07-07 17:25:49 +0300995 mlx5_ib_db_unmap_user(context, &qp->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300996 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200997
998 /*
999 * Free only the BFREGs which are handled by the kernel.
1000 * BFREGs of UARs allocated dynamically are handled by user.
1001 */
1002 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
1003 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001004}
1005
Guy Levi34f4c952018-11-26 08:15:50 +02001006/* get_sq_edge - Get the next nearby edge.
1007 *
1008 * An 'edge' is defined as the first following address after the end
1009 * of the fragment or the SQ. Accordingly, during the WQE construction
1010 * which repetitively increases the pointer to write the next data, it
1011 * simply should check if it gets to an edge.
1012 *
1013 * @sq - SQ buffer.
1014 * @idx - Stride index in the SQ buffer.
1015 *
1016 * Return:
1017 * The new edge.
1018 */
1019static void *get_sq_edge(struct mlx5_ib_wq *sq, u32 idx)
1020{
1021 void *fragment_end;
1022
1023 fragment_end = mlx5_frag_buf_get_wqe
1024 (&sq->fbc,
1025 mlx5_frag_buf_get_idx_last_contig_stride(&sq->fbc, idx));
1026
1027 return fragment_end + MLX5_SEND_WQE_BB;
1028}
1029
Eli Cohene126ba92013-07-07 17:25:49 +03001030static int create_kernel_qp(struct mlx5_ib_dev *dev,
1031 struct ib_qp_init_attr *init_attr,
1032 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001033 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +02001034 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +03001035{
Eli Cohene126ba92013-07-07 17:25:49 +03001036 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001037 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +03001038 int err;
1039
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +03001040 if (init_attr->create_flags & ~(IB_QP_CREATE_INTEGRITY_EN |
Erez Shitritf0313962016-02-21 16:27:17 +02001041 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +02001042 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +03001043 IB_QP_CREATE_NETIF_QP |
Michael Guralnik3f89b012019-10-20 09:43:59 +03001044 MLX5_IB_QP_CREATE_SQPN_QP1))
Eli Cohen1a4c3a32014-02-06 17:41:25 +02001045 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001046
1047 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001048 qp->bf.bfreg = &dev->fp_bfreg;
1049 else
1050 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +03001051
Eli Cohend8030b02017-02-09 19:31:47 +02001052 /* We need to divide by two since each register is comprised of
1053 * two buffers of identical size, namely odd and even
1054 */
1055 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001056 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +03001057
1058 err = calc_sq_size(dev, init_attr, qp);
1059 if (err < 0) {
1060 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001061 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001062 }
1063
1064 qp->rq.offset = 0;
1065 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001066 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +03001067
Guy Levi34f4c952018-11-26 08:15:50 +02001068 err = mlx5_frag_buf_alloc_node(dev->mdev, base->ubuffer.buf_size,
1069 &qp->buf, dev->mdev->priv.numa_node);
Eli Cohene126ba92013-07-07 17:25:49 +03001070 if (err) {
1071 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001072 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001073 }
1074
Guy Levi34f4c952018-11-26 08:15:50 +02001075 if (qp->rq.wqe_cnt)
1076 mlx5_init_fbc(qp->buf.frags, qp->rq.wqe_shift,
1077 ilog2(qp->rq.wqe_cnt), &qp->rq.fbc);
1078
1079 if (qp->sq.wqe_cnt) {
1080 int sq_strides_offset = (qp->sq.offset & (PAGE_SIZE - 1)) /
1081 MLX5_SEND_WQE_BB;
1082 mlx5_init_fbc_offset(qp->buf.frags +
1083 (qp->sq.offset / PAGE_SIZE),
1084 ilog2(MLX5_SEND_WQE_BB),
1085 ilog2(qp->sq.wqe_cnt),
1086 sq_strides_offset, &qp->sq.fbc);
1087
1088 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
1089 }
1090
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001091 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
1092 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001093 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001094 if (!*in) {
1095 err = -ENOMEM;
1096 goto err_buf;
1097 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001098
1099 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1100 MLX5_SET(qpc, qpc, uar_page, uar_index);
1101 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1102
Eli Cohene126ba92013-07-07 17:25:49 +03001103 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001104 MLX5_SET(qpc, qpc, fre, 1);
1105 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001106
Michael Guralnik3f89b012019-10-20 09:43:59 +03001107 if (init_attr->create_flags & MLX5_IB_QP_CREATE_SQPN_QP1) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001108 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +02001109 qp->flags |= MLX5_IB_QP_SQPN_QP1;
1110 }
1111
Guy Levi34f4c952018-11-26 08:15:50 +02001112 mlx5_fill_page_frag_array(&qp->buf,
1113 (__be64 *)MLX5_ADDR_OF(create_qp_in,
1114 *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +03001115
Jack Morgenstein9603b612014-07-28 23:30:22 +03001116 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001117 if (err) {
1118 mlx5_ib_dbg(dev, "err %d\n", err);
1119 goto err_free;
1120 }
1121
Li Dongyangb5883002017-08-16 23:31:22 +10001122 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
1123 sizeof(*qp->sq.wrid), GFP_KERNEL);
1124 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1125 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1126 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1127 sizeof(*qp->rq.wrid), GFP_KERNEL);
1128 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1129 sizeof(*qp->sq.w_list), GFP_KERNEL);
1130 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1131 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001132
1133 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1134 !qp->sq.w_list || !qp->sq.wqe_head) {
1135 err = -ENOMEM;
1136 goto err_wrid;
1137 }
1138 qp->create_type = MLX5_QP_KERNEL;
1139
1140 return 0;
1141
1142err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001143 kvfree(qp->sq.wqe_head);
1144 kvfree(qp->sq.w_list);
1145 kvfree(qp->sq.wrid);
1146 kvfree(qp->sq.wr_data);
1147 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001148 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001149
1150err_free:
Al Viro479163f2014-11-20 08:13:57 +00001151 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001152
1153err_buf:
Guy Levi34f4c952018-11-26 08:15:50 +02001154 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001155 return err;
1156}
1157
1158static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1159{
Li Dongyangb5883002017-08-16 23:31:22 +10001160 kvfree(qp->sq.wqe_head);
1161 kvfree(qp->sq.w_list);
1162 kvfree(qp->sq.wrid);
1163 kvfree(qp->sq.wr_data);
1164 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001165 mlx5_db_free(dev->mdev, &qp->db);
Guy Levi34f4c952018-11-26 08:15:50 +02001166 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001167}
1168
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001169static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001170{
1171 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Moni Shouac32a4f22018-01-02 16:19:32 +02001172 (attr->qp_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001173 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001174 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001175 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001176 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001177 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001178 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001179}
1180
1181static int is_connected(enum ib_qp_type qp_type)
1182{
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001183 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC ||
1184 qp_type == MLX5_IB_QPT_DCI)
Eli Cohene126ba92013-07-07 17:25:49 +03001185 return 1;
1186
1187 return 0;
1188}
1189
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001190static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001191 struct mlx5_ib_qp *qp,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001192 struct mlx5_ib_sq *sq, u32 tdn,
1193 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001194{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001195 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001196 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1197
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001198 MLX5_SET(create_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001199 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001200 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1201 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1202
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001203 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1204}
1205
1206static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001207 struct mlx5_ib_sq *sq, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001208{
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001209 mlx5_cmd_destroy_tis(dev->mdev, sq->tisn, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001210}
1211
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001212static void destroy_flow_rule_vport_sq(struct mlx5_ib_sq *sq)
Mark Blochb96c9dd2018-01-29 10:40:37 +00001213{
1214 if (sq->flow_rule)
1215 mlx5_del_flow_rules(sq->flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001216 sq->flow_rule = NULL;
Mark Blochb96c9dd2018-01-29 10:40:37 +00001217}
1218
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001219static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001220 struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001221 struct mlx5_ib_sq *sq, void *qpin,
1222 struct ib_pd *pd)
1223{
1224 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1225 __be64 *pas;
1226 void *in;
1227 void *sqc;
1228 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1229 void *wq;
1230 int inlen;
1231 int err;
1232 int page_shift = 0;
1233 int npages;
1234 int ncont = 0;
1235 u32 offset = 0;
1236
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001237 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr, ubuffer->buf_size,
1238 &sq->ubuffer.umem, &npages, &page_shift, &ncont,
1239 &offset);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001240 if (err)
1241 return err;
1242
1243 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001244 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001245 if (!in) {
1246 err = -ENOMEM;
1247 goto err_umem;
1248 }
1249
Yishai Hadasc14003f2018-09-20 21:39:22 +03001250 MLX5_SET(create_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001251 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1252 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001253 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1254 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001255 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1256 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1257 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1258 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1259 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001260 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1261 MLX5_CAP_ETH(dev->mdev, swp))
1262 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001263
1264 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1265 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1266 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1267 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1268 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1269 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1270 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1271 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1272 MLX5_SET(wq, wq, page_offset, offset);
1273
1274 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1275 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1276
1277 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1278
1279 kvfree(in);
1280
1281 if (err)
1282 goto err_umem;
1283
1284 return 0;
1285
1286err_umem:
1287 ib_umem_release(sq->ubuffer.umem);
1288 sq->ubuffer.umem = NULL;
1289
1290 return err;
1291}
1292
1293static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1294 struct mlx5_ib_sq *sq)
1295{
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001296 destroy_flow_rule_vport_sq(sq);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001297 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1298 ib_umem_release(sq->ubuffer.umem);
1299}
1300
Boris Pismenny2c292db2018-03-08 15:51:40 +02001301static size_t get_rq_pas_size(void *qpc)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001302{
1303 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1304 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1305 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1306 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1307 u32 po_quanta = 1 << (log_page_size - 6);
1308 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1309 u32 page_size = 1 << log_page_size;
1310 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1311 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1312
1313 return rq_num_pas * sizeof(u64);
1314}
1315
1316static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001317 struct mlx5_ib_rq *rq, void *qpin,
Yishai Hadas34d57582018-09-20 21:39:21 +03001318 size_t qpinlen, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001319{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001320 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001321 __be64 *pas;
1322 __be64 *qp_pas;
1323 void *in;
1324 void *rqc;
1325 void *wq;
1326 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001327 size_t rq_pas_size = get_rq_pas_size(qpc);
1328 size_t inlen;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001329 int err;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001330
1331 if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
1332 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001333
1334 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001335 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001336 if (!in)
1337 return -ENOMEM;
1338
Yishai Hadas34d57582018-09-20 21:39:21 +03001339 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001340 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001341 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1342 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001343 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1344 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1345 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1346 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1347 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1348
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001349 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1350 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1351
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001352 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1353 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001354 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1355 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001356 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1357 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1358 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1359 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1360 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1361 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1362
1363 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1364 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1365 memcpy(pas, qp_pas, rq_pas_size);
1366
1367 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1368
1369 kvfree(in);
1370
1371 return err;
1372}
1373
1374static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1375 struct mlx5_ib_rq *rq)
1376{
1377 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1378}
1379
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001380static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1381{
1382 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1383 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1384 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1385}
1386
Mark Bloch0042f9e2018-09-17 13:30:49 +03001387static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1388 struct mlx5_ib_rq *rq,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001389 u32 qp_flags_en,
1390 struct ib_pd *pd)
Mark Bloch0042f9e2018-09-17 13:30:49 +03001391{
1392 if (qp_flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1393 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1394 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001395 mlx5_cmd_destroy_tir(dev->mdev, rq->tirn, to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001396}
1397
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001398static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001399 struct mlx5_ib_rq *rq, u32 tdn,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001400 u32 *qp_flags_en,
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001401 struct ib_pd *pd,
1402 u32 *out, int outlen)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001403{
Mark Bloch175edba2018-09-17 13:30:48 +03001404 u8 lb_flag = 0;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001405 u32 *in;
1406 void *tirc;
1407 int inlen;
1408 int err;
1409
1410 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001411 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001412 if (!in)
1413 return -ENOMEM;
1414
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001415 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001416 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1417 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1418 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1419 MLX5_SET(tirc, tirc, transport_domain, tdn);
Mark Bloch175edba2018-09-17 13:30:48 +03001420 if (*qp_flags_en & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001421 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001422
Mark Bloch175edba2018-09-17 13:30:48 +03001423 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
1424 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1425
1426 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
1427 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1428
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001429 if (dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001430 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1431 *qp_flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1432 }
1433
1434 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
Mark Blochec9c2fb2018-01-15 13:11:37 +00001435
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001436 err = mlx5_core_create_tir_out(dev->mdev, in, inlen, out, outlen);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001437
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001438 rq->tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001439 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1440 err = mlx5_ib_enable_lb(dev, false, true);
1441
1442 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001443 destroy_raw_packet_qp_tir(dev, rq, 0, pd);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001444 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001445 kvfree(in);
1446
1447 return err;
1448}
1449
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001450static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001451 u32 *in, size_t inlen,
Yishai Hadas7f720522018-09-20 21:45:18 +03001452 struct ib_pd *pd,
1453 struct ib_udata *udata,
1454 struct mlx5_ib_create_qp_resp *resp)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001455{
1456 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1457 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1458 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001459 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1460 udata, struct mlx5_ib_ucontext, ibucontext);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001461 int err;
1462 u32 tdn = mucontext->tdn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001463 u16 uid = to_mpd(pd)->uid;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001464 u32 out[MLX5_ST_SZ_DW(create_tir_out)] = {};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001465
1466 if (qp->sq.wqe_cnt) {
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001467 err = create_raw_packet_qp_tis(dev, qp, sq, tdn, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001468 if (err)
1469 return err;
1470
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001471 err = create_raw_packet_qp_sq(dev, udata, sq, in, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001472 if (err)
1473 goto err_destroy_tis;
1474
Yishai Hadas7f720522018-09-20 21:45:18 +03001475 if (uid) {
1476 resp->tisn = sq->tisn;
1477 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TISN;
1478 resp->sqn = sq->base.mqp.qpn;
1479 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_SQN;
1480 }
1481
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001482 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001483 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001484 }
1485
1486 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001487 rq->base.container_mibqp = qp;
1488
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001489 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1490 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001491 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1492 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
Yishai Hadas34d57582018-09-20 21:39:21 +03001493 err = create_raw_packet_qp_rq(dev, rq, in, inlen, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001494 if (err)
1495 goto err_destroy_sq;
1496
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001497 err = create_raw_packet_qp_tir(
1498 dev, rq, tdn, &qp->flags_en, pd, out,
1499 MLX5_ST_SZ_BYTES(create_tir_out));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001500 if (err)
1501 goto err_destroy_rq;
Yishai Hadas7f720522018-09-20 21:45:18 +03001502
1503 if (uid) {
1504 resp->rqn = rq->base.mqp.qpn;
1505 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_RQN;
1506 resp->tirn = rq->tirn;
1507 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001508 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1509 resp->tir_icm_addr = MLX5_GET(
1510 create_tir_out, out, icm_address_31_0);
1511 resp->tir_icm_addr |=
1512 (u64)MLX5_GET(create_tir_out, out,
1513 icm_address_39_32)
1514 << 32;
1515 resp->tir_icm_addr |=
1516 (u64)MLX5_GET(create_tir_out, out,
1517 icm_address_63_40)
1518 << 40;
1519 resp->comp_mask |=
1520 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1521 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001522 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001523 }
1524
1525 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1526 rq->base.mqp.qpn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001527 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
1528 if (err)
1529 goto err_destroy_tir;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001530
1531 return 0;
1532
Yishai Hadas7f720522018-09-20 21:45:18 +03001533err_destroy_tir:
1534 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001535err_destroy_rq:
1536 destroy_raw_packet_qp_rq(dev, rq);
1537err_destroy_sq:
1538 if (!qp->sq.wqe_cnt)
1539 return err;
1540 destroy_raw_packet_qp_sq(dev, sq);
1541err_destroy_tis:
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001542 destroy_raw_packet_qp_tis(dev, sq, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001543
1544 return err;
1545}
1546
1547static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1548 struct mlx5_ib_qp *qp)
1549{
1550 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1551 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1552 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1553
1554 if (qp->rq.wqe_cnt) {
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001555 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001556 destroy_raw_packet_qp_rq(dev, rq);
1557 }
1558
1559 if (qp->sq.wqe_cnt) {
1560 destroy_raw_packet_qp_sq(dev, sq);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001561 destroy_raw_packet_qp_tis(dev, sq, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001562 }
1563}
1564
1565static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1566 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1567{
1568 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1569 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1570
1571 sq->sq = &qp->sq;
1572 rq->rq = &qp->rq;
1573 sq->doorbell = &qp->db;
1574 rq->doorbell = &qp->db;
1575}
1576
Yishai Hadas28d61372016-05-23 15:20:56 +03001577static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1578{
Mark Bloch0042f9e2018-09-17 13:30:49 +03001579 if (qp->flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1580 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1581 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001582 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1583 to_mpd(qp->ibqp.pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001584}
1585
1586static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1587 struct ib_pd *pd,
1588 struct ib_qp_init_attr *init_attr,
1589 struct ib_udata *udata)
1590{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001591 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1592 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas28d61372016-05-23 15:20:56 +03001593 struct mlx5_ib_create_qp_resp resp = {};
1594 int inlen;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001595 int outlen;
Yishai Hadas28d61372016-05-23 15:20:56 +03001596 int err;
1597 u32 *in;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001598 u32 *out;
Yishai Hadas28d61372016-05-23 15:20:56 +03001599 void *tirc;
1600 void *hfso;
1601 u32 selected_fields = 0;
Matan Barak2d93fc82018-03-28 09:27:55 +03001602 u32 outer_l4;
Yishai Hadas28d61372016-05-23 15:20:56 +03001603 size_t min_resp_len;
1604 u32 tdn = mucontext->tdn;
1605 struct mlx5_ib_create_qp_rss ucmd = {};
1606 size_t required_cmd_sz;
Mark Bloch175edba2018-09-17 13:30:48 +03001607 u8 lb_flag = 0;
Yishai Hadas28d61372016-05-23 15:20:56 +03001608
1609 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1610 return -EOPNOTSUPP;
1611
1612 if (init_attr->create_flags || init_attr->send_cq)
1613 return -EINVAL;
1614
Eli Cohen2f5ff262017-01-03 23:55:21 +02001615 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001616 if (udata->outlen < min_resp_len)
1617 return -EINVAL;
1618
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001619 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001620 if (udata->inlen < required_cmd_sz) {
1621 mlx5_ib_dbg(dev, "invalid inlen\n");
1622 return -EINVAL;
1623 }
1624
1625 if (udata->inlen > sizeof(ucmd) &&
1626 !ib_is_udata_cleared(udata, sizeof(ucmd),
1627 udata->inlen - sizeof(ucmd))) {
1628 mlx5_ib_dbg(dev, "inlen is not supported\n");
1629 return -EOPNOTSUPP;
1630 }
1631
1632 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1633 mlx5_ib_dbg(dev, "copy failed\n");
1634 return -EFAULT;
1635 }
1636
1637 if (ucmd.comp_mask) {
1638 mlx5_ib_dbg(dev, "invalid comp mask\n");
1639 return -EOPNOTSUPP;
1640 }
1641
Mark Bloch175edba2018-09-17 13:30:48 +03001642 if (ucmd.flags & ~(MLX5_QP_FLAG_TUNNEL_OFFLOADS |
1643 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1644 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)) {
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001645 mlx5_ib_dbg(dev, "invalid flags\n");
1646 return -EOPNOTSUPP;
1647 }
1648
1649 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1650 !tunnel_offload_supported(dev->mdev)) {
1651 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001652 return -EOPNOTSUPP;
1653 }
1654
Maor Gottlieb309fa342017-10-19 08:25:56 +03001655 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1656 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1657 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1658 return -EOPNOTSUPP;
1659 }
1660
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001661 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC || dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001662 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1663 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1664 }
1665
1666 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
1667 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1668 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
1669 }
1670
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001671 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
Yishai Hadas28d61372016-05-23 15:20:56 +03001672 if (err) {
1673 mlx5_ib_dbg(dev, "copy failed\n");
1674 return -EINVAL;
1675 }
1676
1677 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001678 outlen = MLX5_ST_SZ_BYTES(create_tir_out);
1679 in = kvzalloc(inlen + outlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001680 if (!in)
1681 return -ENOMEM;
1682
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001683 out = in + MLX5_ST_SZ_DW(create_tir_in);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001684 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001685 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1686 MLX5_SET(tirc, tirc, disp_type,
1687 MLX5_TIRC_DISP_TYPE_INDIRECT);
1688 MLX5_SET(tirc, tirc, indirect_table,
1689 init_attr->rwq_ind_tbl->ind_tbl_num);
1690 MLX5_SET(tirc, tirc, transport_domain, tdn);
1691
1692 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001693
1694 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1695 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1696
Mark Bloch175edba2018-09-17 13:30:48 +03001697 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
1698
Maor Gottlieb309fa342017-10-19 08:25:56 +03001699 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1700 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1701 else
1702 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1703
Yishai Hadas28d61372016-05-23 15:20:56 +03001704 switch (ucmd.rx_hash_function) {
1705 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1706 {
1707 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1708 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1709
1710 if (len != ucmd.rx_key_len) {
1711 err = -EINVAL;
1712 goto err;
1713 }
1714
1715 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
Yishai Hadas28d61372016-05-23 15:20:56 +03001716 memcpy(rss_key, ucmd.rx_hash_key, len);
1717 break;
1718 }
1719 default:
1720 err = -EOPNOTSUPP;
1721 goto err;
1722 }
1723
1724 if (!ucmd.rx_hash_fields_mask) {
1725 /* special case when this TIR serves as steering entry without hashing */
1726 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1727 goto create_tir;
1728 err = -EINVAL;
1729 goto err;
1730 }
1731
1732 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1733 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1734 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1735 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1736 err = -EINVAL;
1737 goto err;
1738 }
1739
1740 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1741 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1742 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1743 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1744 MLX5_L3_PROT_TYPE_IPV4);
1745 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1746 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1747 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1748 MLX5_L3_PROT_TYPE_IPV6);
1749
Matan Barak2d93fc82018-03-28 09:27:55 +03001750 outer_l4 = ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1751 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) << 0 |
1752 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1753 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP)) << 1 |
1754 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI) << 2;
1755
1756 /* Check that only one l4 protocol is set */
1757 if (outer_l4 & (outer_l4 - 1)) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001758 err = -EINVAL;
1759 goto err;
1760 }
1761
1762 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1763 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1764 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1765 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1766 MLX5_L4_PROT_TYPE_TCP);
1767 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1768 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1769 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1770 MLX5_L4_PROT_TYPE_UDP);
1771
1772 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1773 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1774 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1775
1776 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1777 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1778 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1779
1780 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1781 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1782 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1783
1784 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1785 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1786 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1787
Matan Barak2d93fc82018-03-28 09:27:55 +03001788 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI)
1789 selected_fields |= MLX5_HASH_FIELD_SEL_IPSEC_SPI;
1790
Yishai Hadas28d61372016-05-23 15:20:56 +03001791 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1792
1793create_tir:
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001794 err = mlx5_core_create_tir_out(dev->mdev, in, inlen, out, outlen);
Yishai Hadas28d61372016-05-23 15:20:56 +03001795
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001796 qp->rss_qp.tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001797 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1798 err = mlx5_ib_enable_lb(dev, false, true);
1799
1800 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001801 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1802 to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001803 }
1804
Yishai Hadas28d61372016-05-23 15:20:56 +03001805 if (err)
1806 goto err;
1807
Yishai Hadas7f720522018-09-20 21:45:18 +03001808 if (mucontext->devx_uid) {
1809 resp.comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1810 resp.tirn = qp->rss_qp.tirn;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001811 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1812 resp.tir_icm_addr =
1813 MLX5_GET(create_tir_out, out, icm_address_31_0);
1814 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1815 icm_address_39_32)
1816 << 32;
1817 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1818 icm_address_63_40)
1819 << 40;
1820 resp.comp_mask |=
1821 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1822 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001823 }
1824
1825 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
1826 if (err)
1827 goto err_copy;
1828
Yishai Hadas28d61372016-05-23 15:20:56 +03001829 kvfree(in);
1830 /* qpn is reserved for that QP */
1831 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001832 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001833 return 0;
1834
Yishai Hadas7f720522018-09-20 21:45:18 +03001835err_copy:
1836 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn, mucontext->devx_uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001837err:
1838 kvfree(in);
1839 return err;
1840}
1841
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001842static void configure_responder_scat_cqe(struct ib_qp_init_attr *init_attr,
1843 void *qpc)
1844{
1845 int rcqe_sz;
1846
1847 if (init_attr->qp_type == MLX5_IB_QPT_DCI)
1848 return;
1849
1850 rcqe_sz = mlx5_ib_get_cqe_size(init_attr->recv_cq);
1851
Guy Levi7249c8e2019-04-10 10:59:45 +03001852 if (init_attr->qp_type == MLX5_IB_QPT_DCT) {
1853 if (rcqe_sz == 128)
1854 MLX5_SET(dctc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
1855
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001856 return;
1857 }
1858
Guy Levi7249c8e2019-04-10 10:59:45 +03001859 MLX5_SET(qpc, qpc, cs_res,
1860 rcqe_sz == 128 ? MLX5_RES_SCAT_DATA64_CQE :
1861 MLX5_RES_SCAT_DATA32_CQE);
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001862}
1863
1864static void configure_requester_scat_cqe(struct mlx5_ib_dev *dev,
1865 struct ib_qp_init_attr *init_attr,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001866 struct mlx5_ib_create_qp *ucmd,
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001867 void *qpc)
1868{
1869 enum ib_qp_type qpt = init_attr->qp_type;
1870 int scqe_sz;
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001871 bool allow_scat_cqe = 0;
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001872
1873 if (qpt == IB_QPT_UC || qpt == IB_QPT_UD)
1874 return;
1875
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001876 if (ucmd)
1877 allow_scat_cqe = ucmd->flags & MLX5_QP_FLAG_ALLOW_SCATTER_CQE;
1878
1879 if (!allow_scat_cqe && init_attr->sq_sig_type != IB_SIGNAL_ALL_WR)
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001880 return;
1881
1882 scqe_sz = mlx5_ib_get_cqe_size(init_attr->send_cq);
1883 if (scqe_sz == 128) {
1884 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
1885 return;
1886 }
1887
1888 if (init_attr->qp_type != MLX5_IB_QPT_DCI ||
1889 MLX5_CAP_GEN(dev->mdev, dc_req_scat_data_cqe))
1890 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
1891}
1892
Yonatan Cohena60109d2018-10-10 09:25:16 +03001893static int atomic_size_to_mode(int size_mask)
1894{
1895 /* driver does not support atomic_size > 256B
1896 * and does not know how to translate bigger sizes
1897 */
1898 int supported_size_mask = size_mask & 0x1ff;
1899 int log_max_size;
1900
1901 if (!supported_size_mask)
1902 return -EOPNOTSUPP;
1903
1904 log_max_size = __fls(supported_size_mask);
1905
1906 if (log_max_size > 3)
1907 return log_max_size;
1908
1909 return MLX5_ATOMIC_MODE_8B;
1910}
1911
1912static int get_atomic_mode(struct mlx5_ib_dev *dev,
1913 enum ib_qp_type qp_type)
1914{
1915 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
1916 u8 atomic = MLX5_CAP_GEN(dev->mdev, atomic);
1917 int atomic_mode = -EOPNOTSUPP;
1918 int atomic_size_mask;
1919
1920 if (!atomic)
1921 return -EOPNOTSUPP;
1922
1923 if (qp_type == MLX5_IB_QPT_DCT)
1924 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
1925 else
1926 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
1927
1928 if ((atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_CMP_SWAP) ||
1929 (atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_FETCH_ADD))
1930 atomic_mode = atomic_size_to_mode(atomic_size_mask);
1931
1932 if (atomic_mode <= 0 &&
1933 (atomic_operations & MLX5_ATOMIC_OPS_CMP_SWAP &&
1934 atomic_operations & MLX5_ATOMIC_OPS_FETCH_ADD))
1935 atomic_mode = MLX5_ATOMIC_MODE_IB_COMP;
1936
1937 return atomic_mode;
1938}
1939
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03001940static inline bool check_flags_mask(uint64_t input, uint64_t supported)
1941{
1942 return (input & ~supported) == 0;
1943}
1944
Eli Cohene126ba92013-07-07 17:25:49 +03001945static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1946 struct ib_qp_init_attr *init_attr,
1947 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1948{
1949 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001950 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001951 struct mlx5_core_dev *mdev = dev->mdev;
Jason Gunthorpe0625b4b2018-08-14 15:33:52 -06001952 struct mlx5_ib_create_qp_resp resp = {};
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001953 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
1954 udata, struct mlx5_ib_ucontext, ibucontext);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001955 struct mlx5_ib_cq *send_cq;
1956 struct mlx5_ib_cq *recv_cq;
1957 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001958 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001959 struct mlx5_ib_create_qp ucmd;
1960 struct mlx5_ib_qp_base *base;
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001961 int mlx5_st;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001962 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001963 u32 *in;
1964 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001965
1966 mutex_init(&qp->mutex);
1967 spin_lock_init(&qp->sq.lock);
1968 spin_lock_init(&qp->rq.lock);
1969
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001970 mlx5_st = to_mlx5_st(init_attr->qp_type);
1971 if (mlx5_st < 0)
1972 return -EINVAL;
1973
Yishai Hadas28d61372016-05-23 15:20:56 +03001974 if (init_attr->rwq_ind_tbl) {
1975 if (!udata)
1976 return -ENOSYS;
1977
1978 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1979 return err;
1980 }
1981
Eli Cohenf360d882014-04-02 00:10:16 +03001982 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001983 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001984 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1985 return -EINVAL;
1986 } else {
1987 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1988 }
1989 }
1990
Leon Romanovsky051f2632015-12-20 12:16:11 +02001991 if (init_attr->create_flags &
1992 (IB_QP_CREATE_CROSS_CHANNEL |
1993 IB_QP_CREATE_MANAGED_SEND |
1994 IB_QP_CREATE_MANAGED_RECV)) {
1995 if (!MLX5_CAP_GEN(mdev, cd)) {
1996 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1997 return -EINVAL;
1998 }
1999 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
2000 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
2001 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
2002 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
2003 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
2004 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
2005 }
Erez Shitritf0313962016-02-21 16:27:17 +02002006
2007 if (init_attr->qp_type == IB_QPT_UD &&
2008 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
2009 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
2010 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
2011 return -EOPNOTSUPP;
2012 }
2013
Majd Dibbiny358e42e2016-04-17 17:19:37 +03002014 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
2015 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2016 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
2017 return -EOPNOTSUPP;
2018 }
2019 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
2020 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
2021 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
2022 return -EOPNOTSUPP;
2023 }
2024 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
2025 }
2026
Eli Cohene126ba92013-07-07 17:25:49 +03002027 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
2028 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
2029
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02002030 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
2031 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
2032 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
2033 (init_attr->qp_type != IB_QPT_RAW_PACKET))
2034 return -EOPNOTSUPP;
2035 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
2036 }
2037
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002038 if (udata) {
Eli Cohene126ba92013-07-07 17:25:49 +03002039 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
2040 mlx5_ib_dbg(dev, "copy failed\n");
2041 return -EFAULT;
2042 }
2043
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002044 if (!check_flags_mask(ucmd.flags,
Mark Bloch8af526e2019-01-15 16:45:32 +02002045 MLX5_QP_FLAG_ALLOW_SCATTER_CQE |
2046 MLX5_QP_FLAG_BFREG_INDEX |
2047 MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE |
2048 MLX5_QP_FLAG_SCATTER_CQE |
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002049 MLX5_QP_FLAG_SIGNATURE |
Mark Bloch8af526e2019-01-15 16:45:32 +02002050 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC |
2051 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
2052 MLX5_QP_FLAG_TUNNEL_OFFLOADS |
2053 MLX5_QP_FLAG_TYPE_DCI |
2054 MLX5_QP_FLAG_TYPE_DCT))
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002055 return -EINVAL;
2056
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002057 err = get_qp_user_index(ucontext, &ucmd, udata->inlen, &uidx);
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002058 if (err)
2059 return err;
2060
Eli Cohene126ba92013-07-07 17:25:49 +03002061 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002062 if (MLX5_CAP_GEN(dev->mdev, sctr_data_cqe))
2063 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002064 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
2065 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
2066 !tunnel_offload_supported(mdev)) {
2067 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
2068 return -EOPNOTSUPP;
2069 }
Mark Bloch175edba2018-09-17 13:30:48 +03002070 qp->flags_en |= MLX5_QP_FLAG_TUNNEL_OFFLOADS;
2071 }
2072
2073 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC) {
2074 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2075 mlx5_ib_dbg(dev, "Self-LB UC isn't supported\n");
2076 return -EOPNOTSUPP;
2077 }
2078 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
2079 }
2080
2081 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
2082 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2083 mlx5_ib_dbg(dev, "Self-LB UM isn't supported\n");
2084 return -EOPNOTSUPP;
2085 }
2086 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002087 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002088
Danit Goldberg569c6652018-11-30 13:22:05 +02002089 if (ucmd.flags & MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE) {
2090 if (init_attr->qp_type != IB_QPT_RC ||
2091 !MLX5_CAP_GEN(dev->mdev, qp_packet_based)) {
2092 mlx5_ib_dbg(dev, "packet based credit mode isn't supported\n");
2093 return -EOPNOTSUPP;
2094 }
2095 qp->flags |= MLX5_IB_QP_PACKET_BASED_CREDIT;
2096 }
2097
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002098 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
2099 if (init_attr->qp_type != IB_QPT_UD ||
2100 (MLX5_CAP_GEN(dev->mdev, port_type) !=
2101 MLX5_CAP_PORT_TYPE_IB) ||
2102 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
2103 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
2104 return -EOPNOTSUPP;
2105 }
2106
2107 qp->flags |= MLX5_IB_QP_UNDERLAY;
2108 qp->underlay_qpn = init_attr->source_qpn;
2109 }
Eli Cohene126ba92013-07-07 17:25:49 +03002110 } else {
2111 qp->wq_sig = !!wq_signature;
2112 }
2113
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002114 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2115 qp->flags & MLX5_IB_QP_UNDERLAY) ?
2116 &qp->raw_packet_qp.rq.base :
2117 &qp->trans_qp.base;
2118
Eli Cohene126ba92013-07-07 17:25:49 +03002119 qp->has_rq = qp_has_rq(init_attr);
2120 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002121 qp, udata ? &ucmd : NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002122 if (err) {
2123 mlx5_ib_dbg(dev, "err %d\n", err);
2124 return err;
2125 }
2126
2127 if (pd) {
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002128 if (udata) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002129 __u32 max_wqes =
2130 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03002131 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
2132 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
2133 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
2134 mlx5_ib_dbg(dev, "invalid rq params\n");
2135 return -EINVAL;
2136 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002137 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03002138 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03002139 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03002140 return -EINVAL;
2141 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02002142 if (init_attr->create_flags &
Michael Guralnik3f89b012019-10-20 09:43:59 +03002143 MLX5_IB_QP_CREATE_SQPN_QP1) {
Haggai Eranb11a4f92016-02-29 15:45:03 +02002144 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
2145 return -EINVAL;
2146 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002147 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
2148 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002149 if (err)
2150 mlx5_ib_dbg(dev, "err %d\n", err);
2151 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002152 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
2153 base);
Eli Cohene126ba92013-07-07 17:25:49 +03002154 if (err)
2155 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03002156 }
2157
2158 if (err)
2159 return err;
2160 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002161 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03002162 if (!in)
2163 return -ENOMEM;
2164
2165 qp->create_type = MLX5_QP_EMPTY;
2166 }
2167
2168 if (is_sqp(init_attr->qp_type))
2169 qp->port = init_attr->port_num;
2170
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002171 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
2172
Noa Osheroviche7b169f2018-02-25 13:39:51 +02002173 MLX5_SET(qpc, qpc, st, mlx5_st);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002174 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03002175
2176 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002177 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002178 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002179 MLX5_SET(qpc, qpc, latency_sensitive, 1);
2180
Eli Cohene126ba92013-07-07 17:25:49 +03002181
2182 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002183 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002184
Eli Cohenf360d882014-04-02 00:10:16 +03002185 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002186 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03002187
Leon Romanovsky051f2632015-12-20 12:16:11 +02002188 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002189 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002190 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002191 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002192 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002193 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Danit Goldberg569c6652018-11-30 13:22:05 +02002194 if (qp->flags & MLX5_IB_QP_PACKET_BASED_CREDIT)
2195 MLX5_SET(qpc, qpc, req_e2e_credit_mode, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002196 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002197 configure_responder_scat_cqe(init_attr, qpc);
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002198 configure_requester_scat_cqe(dev, init_attr,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002199 udata ? &ucmd : NULL,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002200 qpc);
Eli Cohene126ba92013-07-07 17:25:49 +03002201 }
2202
2203 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002204 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
2205 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03002206 }
2207
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002208 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03002209
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002210 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002211 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002212 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002213 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002214 if (init_attr->srq &&
2215 init_attr->srq->srq_type == IB_SRQT_TM)
2216 MLX5_SET(qpc, qpc, offload_type,
2217 MLX5_QPC_OFFLOAD_TYPE_RNDV);
2218 }
Eli Cohene126ba92013-07-07 17:25:49 +03002219
2220 /* Set default resources */
2221 switch (init_attr->qp_type) {
2222 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002223 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2224 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
2225 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
2226 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002227 break;
2228 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002229 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2230 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2231 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002232 break;
2233 default:
2234 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002235 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
2236 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002237 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002238 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2239 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002240 }
2241 }
2242
2243 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002244 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002245
2246 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002247 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002248
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002249 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03002250
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002251 /* 0xffffff means we ask to work with cqe version 0 */
2252 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002253 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002254
Erez Shitritf0313962016-02-21 16:27:17 +02002255 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
2256 if (init_attr->qp_type == IB_QPT_UD &&
2257 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02002258 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
2259 qp->flags |= MLX5_IB_QP_LSO;
2260 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002261
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002262 if (init_attr->create_flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
2263 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
2264 mlx5_ib_dbg(dev, "scatter end padding is not supported\n");
2265 err = -EOPNOTSUPP;
2266 goto err;
2267 } else if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2268 MLX5_SET(qpc, qpc, end_padding_mode,
2269 MLX5_WQ_END_PAD_MODE_ALIGN);
2270 } else {
2271 qp->flags |= MLX5_IB_QP_PCI_WRITE_END_PADDING;
2272 }
2273 }
2274
Boris Pismenny2c292db2018-03-08 15:51:40 +02002275 if (inlen < 0) {
2276 err = -EINVAL;
2277 goto err;
2278 }
2279
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002280 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2281 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002282 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
2283 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
Yishai Hadas7f720522018-09-20 21:45:18 +03002284 err = create_raw_packet_qp(dev, qp, in, inlen, pd, udata,
2285 &resp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002286 } else {
2287 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
2288 }
2289
Eli Cohene126ba92013-07-07 17:25:49 +03002290 if (err) {
2291 mlx5_ib_dbg(dev, "create qp failed\n");
2292 goto err_create;
2293 }
2294
Al Viro479163f2014-11-20 08:13:57 +00002295 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002296
majd@mellanox.com19098df2016-01-14 19:13:03 +02002297 base->container_mibqp = qp;
2298 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03002299
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002300 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
2301 &send_cq, &recv_cq);
2302 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2303 mlx5_ib_lock_cqs(send_cq, recv_cq);
2304 /* Maintain device to QPs access, needed for further handling via reset
2305 * flow
2306 */
2307 list_add_tail(&qp->qps_list, &dev->qp_list);
2308 /* Maintain CQ to QPs access, needed for further handling via reset flow
2309 */
2310 if (send_cq)
2311 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
2312 if (recv_cq)
2313 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
2314 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2315 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
2316
Eli Cohene126ba92013-07-07 17:25:49 +03002317 return 0;
2318
2319err_create:
2320 if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002321 destroy_qp_user(dev, pd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002322 else if (qp->create_type == MLX5_QP_KERNEL)
2323 destroy_qp_kernel(dev, qp);
2324
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002325err:
Al Viro479163f2014-11-20 08:13:57 +00002326 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002327 return err;
2328}
2329
2330static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2331 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
2332{
2333 if (send_cq) {
2334 if (recv_cq) {
2335 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002336 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002337 spin_lock_nested(&recv_cq->lock,
2338 SINGLE_DEPTH_NESTING);
2339 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002340 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002341 __acquire(&recv_cq->lock);
2342 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002343 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002344 spin_lock_nested(&send_cq->lock,
2345 SINGLE_DEPTH_NESTING);
2346 }
2347 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002348 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002349 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002350 }
2351 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002352 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002353 __acquire(&send_cq->lock);
2354 } else {
2355 __acquire(&send_cq->lock);
2356 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002357 }
2358}
2359
2360static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2361 __releases(&send_cq->lock) __releases(&recv_cq->lock)
2362{
2363 if (send_cq) {
2364 if (recv_cq) {
2365 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
2366 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002367 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002368 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
2369 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002370 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002371 } else {
2372 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002373 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002374 }
2375 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002376 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002377 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002378 }
2379 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002380 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002381 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002382 } else {
2383 __release(&recv_cq->lock);
2384 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002385 }
2386}
2387
2388static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
2389{
2390 return to_mpd(qp->ibqp.pd);
2391}
2392
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002393static void get_cqs(enum ib_qp_type qp_type,
2394 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03002395 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
2396{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002397 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002398 case IB_QPT_XRC_TGT:
2399 *send_cq = NULL;
2400 *recv_cq = NULL;
2401 break;
2402 case MLX5_IB_QPT_REG_UMR:
2403 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002404 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002405 *recv_cq = NULL;
2406 break;
2407
2408 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002409 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002410 case IB_QPT_RC:
2411 case IB_QPT_UC:
2412 case IB_QPT_UD:
2413 case IB_QPT_RAW_IPV6:
2414 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002415 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002416 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2417 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002418 break;
2419
Eli Cohene126ba92013-07-07 17:25:49 +03002420 case IB_QPT_MAX:
2421 default:
2422 *send_cq = NULL;
2423 *recv_cq = NULL;
2424 break;
2425 }
2426}
2427
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002428static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002429 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2430 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002431
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002432static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2433 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002434{
2435 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002436 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002437 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002438 int err;
2439
Yishai Hadas28d61372016-05-23 15:20:56 +03002440 if (qp->ibqp.rwq_ind_tbl) {
2441 destroy_rss_raw_qp_tir(dev, qp);
2442 return;
2443 }
2444
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002445 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2446 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002447 &qp->raw_packet_qp.rq.base :
2448 &qp->trans_qp.base;
2449
Haggai Eran6aec21f2014-12-11 17:04:23 +02002450 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002451 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2452 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002453 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002454 MLX5_CMD_OP_2RST_QP, 0,
2455 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002456 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002457 struct mlx5_modify_raw_qp_param raw_qp_param = {
2458 .operation = MLX5_CMD_OP_2RST_QP
2459 };
2460
Aviv Heller13eab212016-09-18 20:48:04 +03002461 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002462 }
2463 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002464 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002465 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002466 }
Eli Cohene126ba92013-07-07 17:25:49 +03002467
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002468 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2469 &send_cq, &recv_cq);
2470
2471 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2472 mlx5_ib_lock_cqs(send_cq, recv_cq);
2473 /* del from lists under both locks above to protect reset flow paths */
2474 list_del(&qp->qps_list);
2475 if (send_cq)
2476 list_del(&qp->cq_send_list);
2477
2478 if (recv_cq)
2479 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002480
2481 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002482 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002483 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2484 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002485 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2486 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002487 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002488 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2489 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002490
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002491 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2492 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002493 destroy_raw_packet_qp(dev, qp);
2494 } else {
2495 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
2496 if (err)
2497 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2498 base->mqp.qpn);
2499 }
Eli Cohene126ba92013-07-07 17:25:49 +03002500
Eli Cohene126ba92013-07-07 17:25:49 +03002501 if (qp->create_type == MLX5_QP_KERNEL)
2502 destroy_qp_kernel(dev, qp);
2503 else if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002504 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002505}
2506
2507static const char *ib_qp_type_str(enum ib_qp_type type)
2508{
2509 switch (type) {
2510 case IB_QPT_SMI:
2511 return "IB_QPT_SMI";
2512 case IB_QPT_GSI:
2513 return "IB_QPT_GSI";
2514 case IB_QPT_RC:
2515 return "IB_QPT_RC";
2516 case IB_QPT_UC:
2517 return "IB_QPT_UC";
2518 case IB_QPT_UD:
2519 return "IB_QPT_UD";
2520 case IB_QPT_RAW_IPV6:
2521 return "IB_QPT_RAW_IPV6";
2522 case IB_QPT_RAW_ETHERTYPE:
2523 return "IB_QPT_RAW_ETHERTYPE";
2524 case IB_QPT_XRC_INI:
2525 return "IB_QPT_XRC_INI";
2526 case IB_QPT_XRC_TGT:
2527 return "IB_QPT_XRC_TGT";
2528 case IB_QPT_RAW_PACKET:
2529 return "IB_QPT_RAW_PACKET";
2530 case MLX5_IB_QPT_REG_UMR:
2531 return "MLX5_IB_QPT_REG_UMR";
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002532 case IB_QPT_DRIVER:
2533 return "IB_QPT_DRIVER";
Eli Cohene126ba92013-07-07 17:25:49 +03002534 case IB_QPT_MAX:
2535 default:
2536 return "Invalid QP type";
2537 }
2538}
2539
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002540static struct ib_qp *mlx5_ib_create_dct(struct ib_pd *pd,
2541 struct ib_qp_init_attr *attr,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002542 struct mlx5_ib_create_qp *ucmd,
2543 struct ib_udata *udata)
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002544{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002545 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2546 udata, struct mlx5_ib_ucontext, ibucontext);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002547 struct mlx5_ib_qp *qp;
2548 int err = 0;
2549 u32 uidx = MLX5_IB_DEFAULT_UIDX;
2550 void *dctc;
2551
2552 if (!attr->srq || !attr->recv_cq)
2553 return ERR_PTR(-EINVAL);
2554
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002555 err = get_qp_user_index(ucontext, ucmd, sizeof(*ucmd), &uidx);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002556 if (err)
2557 return ERR_PTR(err);
2558
2559 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2560 if (!qp)
2561 return ERR_PTR(-ENOMEM);
2562
2563 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
2564 if (!qp->dct.in) {
2565 err = -ENOMEM;
2566 goto err_free;
2567 }
2568
Yishai Hadasa01a5862018-09-20 21:39:24 +03002569 MLX5_SET(create_dct_in, qp->dct.in, uid, to_mpd(pd)->uid);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002570 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shoua776a3902018-01-02 16:19:33 +02002571 qp->qp_sub_type = MLX5_IB_QPT_DCT;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002572 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2573 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2574 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2575 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2576 MLX5_SET(dctc, dctc, user_index, uidx);
2577
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002578 if (ucmd->flags & MLX5_QP_FLAG_SCATTER_CQE)
2579 configure_responder_scat_cqe(attr, dctc);
2580
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002581 qp->state = IB_QPS_RESET;
2582
2583 return &qp->ibqp;
2584err_free:
2585 kfree(qp);
2586 return ERR_PTR(err);
2587}
2588
2589static int set_mlx_qp_type(struct mlx5_ib_dev *dev,
2590 struct ib_qp_init_attr *init_attr,
2591 struct mlx5_ib_create_qp *ucmd,
2592 struct ib_udata *udata)
2593{
2594 enum { MLX_QP_FLAGS = MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI };
2595 int err;
2596
2597 if (!udata)
2598 return -EINVAL;
2599
2600 if (udata->inlen < sizeof(*ucmd)) {
2601 mlx5_ib_dbg(dev, "create_qp user command is smaller than expected\n");
2602 return -EINVAL;
2603 }
2604 err = ib_copy_from_udata(ucmd, udata, sizeof(*ucmd));
2605 if (err)
2606 return err;
2607
2608 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCI) {
2609 init_attr->qp_type = MLX5_IB_QPT_DCI;
2610 } else {
2611 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCT) {
2612 init_attr->qp_type = MLX5_IB_QPT_DCT;
2613 } else {
2614 mlx5_ib_dbg(dev, "Invalid QP flags\n");
2615 return -EINVAL;
2616 }
2617 }
2618
2619 if (!MLX5_CAP_GEN(dev->mdev, dct)) {
2620 mlx5_ib_dbg(dev, "DC transport is not supported\n");
2621 return -EOPNOTSUPP;
2622 }
2623
2624 return 0;
2625}
2626
Eli Cohene126ba92013-07-07 17:25:49 +03002627struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002628 struct ib_qp_init_attr *verbs_init_attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002629 struct ib_udata *udata)
2630{
2631 struct mlx5_ib_dev *dev;
2632 struct mlx5_ib_qp *qp;
2633 u16 xrcdn = 0;
2634 int err;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002635 struct ib_qp_init_attr mlx_init_attr;
2636 struct ib_qp_init_attr *init_attr = verbs_init_attr;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002637 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2638 udata, struct mlx5_ib_ucontext, ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03002639
2640 if (pd) {
2641 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002642
2643 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002644 if (!ucontext) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002645 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2646 return ERR_PTR(-EINVAL);
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002647 } else if (!ucontext->cqe_version) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002648 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2649 return ERR_PTR(-EINVAL);
2650 }
2651 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002652 } else {
2653 /* being cautious here */
2654 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2655 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2656 pr_warn("%s: no PD for transport %s\n", __func__,
2657 ib_qp_type_str(init_attr->qp_type));
2658 return ERR_PTR(-EINVAL);
2659 }
2660 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002661 }
2662
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002663 if (init_attr->qp_type == IB_QPT_DRIVER) {
2664 struct mlx5_ib_create_qp ucmd;
2665
2666 init_attr = &mlx_init_attr;
2667 memcpy(init_attr, verbs_init_attr, sizeof(*verbs_init_attr));
2668 err = set_mlx_qp_type(dev, init_attr, &ucmd, udata);
2669 if (err)
2670 return ERR_PTR(err);
Moni Shouac32a4f22018-01-02 16:19:32 +02002671
2672 if (init_attr->qp_type == MLX5_IB_QPT_DCI) {
2673 if (init_attr->cap.max_recv_wr ||
2674 init_attr->cap.max_recv_sge) {
2675 mlx5_ib_dbg(dev, "DCI QP requires zero size receive queue\n");
2676 return ERR_PTR(-EINVAL);
2677 }
Moni Shoua776a3902018-01-02 16:19:33 +02002678 } else {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002679 return mlx5_ib_create_dct(pd, init_attr, &ucmd, udata);
Moni Shouac32a4f22018-01-02 16:19:32 +02002680 }
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002681 }
2682
Eli Cohene126ba92013-07-07 17:25:49 +03002683 switch (init_attr->qp_type) {
2684 case IB_QPT_XRC_TGT:
2685 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002686 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002687 mlx5_ib_dbg(dev, "XRC not supported\n");
2688 return ERR_PTR(-ENOSYS);
2689 }
2690 init_attr->recv_cq = NULL;
2691 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2692 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2693 init_attr->send_cq = NULL;
2694 }
2695
2696 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002697 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002698 case IB_QPT_RC:
2699 case IB_QPT_UC:
2700 case IB_QPT_UD:
2701 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002702 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002703 case MLX5_IB_QPT_REG_UMR:
Moni Shouac32a4f22018-01-02 16:19:32 +02002704 case MLX5_IB_QPT_DCI:
Eli Cohene126ba92013-07-07 17:25:49 +03002705 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2706 if (!qp)
2707 return ERR_PTR(-ENOMEM);
2708
2709 err = create_qp_common(dev, pd, init_attr, udata, qp);
2710 if (err) {
2711 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2712 kfree(qp);
2713 return ERR_PTR(err);
2714 }
2715
2716 if (is_qp0(init_attr->qp_type))
2717 qp->ibqp.qp_num = 0;
2718 else if (is_qp1(init_attr->qp_type))
2719 qp->ibqp.qp_num = 1;
2720 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002721 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002722
2723 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 +02002724 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002725 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2726 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002727
majd@mellanox.com19098df2016-01-14 19:13:03 +02002728 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002729
2730 break;
2731
Haggai Erand16e91d2016-02-29 15:45:05 +02002732 case IB_QPT_GSI:
2733 return mlx5_ib_gsi_create_qp(pd, init_attr);
2734
Eli Cohene126ba92013-07-07 17:25:49 +03002735 case IB_QPT_RAW_IPV6:
2736 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002737 case IB_QPT_MAX:
2738 default:
2739 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2740 init_attr->qp_type);
2741 /* Don't support raw QPs */
2742 return ERR_PTR(-EINVAL);
2743 }
2744
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002745 if (verbs_init_attr->qp_type == IB_QPT_DRIVER)
2746 qp->qp_sub_type = init_attr->qp_type;
2747
Eli Cohene126ba92013-07-07 17:25:49 +03002748 return &qp->ibqp;
2749}
2750
Moni Shoua776a3902018-01-02 16:19:33 +02002751static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2752{
2753 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2754
2755 if (mqp->state == IB_QPS_RTR) {
2756 int err;
2757
2758 err = mlx5_core_destroy_dct(dev->mdev, &mqp->dct.mdct);
2759 if (err) {
2760 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2761 return err;
2762 }
2763 }
2764
2765 kfree(mqp->dct.in);
2766 kfree(mqp);
2767 return 0;
2768}
2769
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002770int mlx5_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002771{
2772 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2773 struct mlx5_ib_qp *mqp = to_mqp(qp);
2774
Haggai Erand16e91d2016-02-29 15:45:05 +02002775 if (unlikely(qp->qp_type == IB_QPT_GSI))
2776 return mlx5_ib_gsi_destroy_qp(qp);
2777
Moni Shoua776a3902018-01-02 16:19:33 +02002778 if (mqp->qp_sub_type == MLX5_IB_QPT_DCT)
2779 return mlx5_ib_destroy_dct(mqp);
2780
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002781 destroy_qp_common(dev, mqp, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002782
2783 kfree(mqp);
2784
2785 return 0;
2786}
2787
Yonatan Cohena60109d2018-10-10 09:25:16 +03002788static int to_mlx5_access_flags(struct mlx5_ib_qp *qp,
2789 const struct ib_qp_attr *attr,
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002790 int attr_mask, __be32 *hw_access_flags_be)
Eli Cohene126ba92013-07-07 17:25:49 +03002791{
Eli Cohene126ba92013-07-07 17:25:49 +03002792 u8 dest_rd_atomic;
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002793 u32 access_flags, hw_access_flags = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002794
Yonatan Cohena60109d2018-10-10 09:25:16 +03002795 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
2796
Eli Cohene126ba92013-07-07 17:25:49 +03002797 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2798 dest_rd_atomic = attr->max_dest_rd_atomic;
2799 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002800 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002801
2802 if (attr_mask & IB_QP_ACCESS_FLAGS)
2803 access_flags = attr->qp_access_flags;
2804 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002805 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002806
2807 if (!dest_rd_atomic)
2808 access_flags &= IB_ACCESS_REMOTE_WRITE;
2809
2810 if (access_flags & IB_ACCESS_REMOTE_READ)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002811 hw_access_flags |= MLX5_QP_BIT_RRE;
Yonatan Cohen13f8d9c2018-11-21 13:48:39 +02002812 if (access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03002813 int atomic_mode;
Eli Cohene126ba92013-07-07 17:25:49 +03002814
Yonatan Cohena60109d2018-10-10 09:25:16 +03002815 atomic_mode = get_atomic_mode(dev, qp->ibqp.qp_type);
2816 if (atomic_mode < 0)
2817 return -EOPNOTSUPP;
2818
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002819 hw_access_flags |= MLX5_QP_BIT_RAE;
2820 hw_access_flags |= atomic_mode << MLX5_ATOMIC_MODE_OFFSET;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002821 }
2822
2823 if (access_flags & IB_ACCESS_REMOTE_WRITE)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002824 hw_access_flags |= MLX5_QP_BIT_RWE;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002825
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002826 *hw_access_flags_be = cpu_to_be32(hw_access_flags);
Yonatan Cohena60109d2018-10-10 09:25:16 +03002827
2828 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002829}
2830
2831enum {
2832 MLX5_PATH_FLAG_FL = 1 << 0,
2833 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2834 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2835};
2836
2837static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2838{
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002839 if (rate == IB_RATE_PORT_CURRENT)
Eli Cohene126ba92013-07-07 17:25:49 +03002840 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002841
Michael Guralnika5a5d192018-12-09 11:49:50 +02002842 if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_600_GBPS)
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002843 return -EINVAL;
2844
2845 while (rate != IB_RATE_PORT_CURRENT &&
2846 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
2847 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
2848 --rate;
2849
2850 return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
Eli Cohene126ba92013-07-07 17:25:49 +03002851}
2852
majd@mellanox.com75850d02016-01-14 19:13:06 +02002853static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002854 struct mlx5_ib_sq *sq, u8 sl,
2855 struct ib_pd *pd)
majd@mellanox.com75850d02016-01-14 19:13:06 +02002856{
2857 void *in;
2858 void *tisc;
2859 int inlen;
2860 int err;
2861
2862 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002863 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002864 if (!in)
2865 return -ENOMEM;
2866
2867 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002868 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002869
2870 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2871 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2872
2873 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2874
2875 kvfree(in);
2876
2877 return err;
2878}
2879
Aviv Heller13eab212016-09-18 20:48:04 +03002880static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002881 struct mlx5_ib_sq *sq, u8 tx_affinity,
2882 struct ib_pd *pd)
Aviv Heller13eab212016-09-18 20:48:04 +03002883{
2884 void *in;
2885 void *tisc;
2886 int inlen;
2887 int err;
2888
2889 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002890 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002891 if (!in)
2892 return -ENOMEM;
2893
2894 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002895 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
Aviv Heller13eab212016-09-18 20:48:04 +03002896
2897 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2898 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2899
2900 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2901
2902 kvfree(in);
2903
2904 return err;
2905}
2906
majd@mellanox.com75850d02016-01-14 19:13:06 +02002907static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002908 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002909 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002910 u32 path_flags, const struct ib_qp_attr *attr,
2911 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002912{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002913 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002914 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002915 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002916 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2917 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002918
Eli Cohene126ba92013-07-07 17:25:49 +03002919 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002920 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2921 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002922
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002923 if (ah_flags & IB_AH_GRH) {
2924 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002925 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002926 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002927 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002928 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002929 return -EINVAL;
2930 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002931 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002932
2933 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002934 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002935 return -EINVAL;
Parav Pandit47ec3862018-06-13 10:22:06 +03002936
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002937 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02002938 if (qp->ibqp.qp_type == IB_QPT_RC ||
2939 qp->ibqp.qp_type == IB_QPT_UC ||
2940 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
2941 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
Parav Pandit47ec3862018-06-13 10:22:06 +03002942 path->udp_sport =
2943 mlx5_get_roce_udp_sport(dev, ah->grh.sgid_attr);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002944 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Parav Pandit47ec3862018-06-13 10:22:06 +03002945 gid_type = ah->grh.sgid_attr->gid_type;
Majd Dibbinyed884512017-01-18 14:10:35 +02002946 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002947 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002948 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002949 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2950 path->fl_free_ar |=
2951 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002952 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2953 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2954 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002955 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002956 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002957 }
2958
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002959 if (ah_flags & IB_AH_GRH) {
2960 path->mgid_index = grh->sgid_index;
2961 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002962 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002963 cpu_to_be32((grh->traffic_class << 20) |
2964 (grh->flow_label));
2965 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002966 }
2967
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002968 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002969 if (err < 0)
2970 return err;
2971 path->static_rate = err;
2972 path->port = port;
2973
Eli Cohene126ba92013-07-07 17:25:49 +03002974 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002975 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002976
majd@mellanox.com75850d02016-01-14 19:13:06 +02002977 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2978 return modify_raw_packet_eth_prio(dev->mdev,
2979 &qp->raw_packet_qp.sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002980 sl & 0xf, qp->ibqp.pd);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002981
Eli Cohene126ba92013-07-07 17:25:49 +03002982 return 0;
2983}
2984
2985static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2986 [MLX5_QP_STATE_INIT] = {
2987 [MLX5_QP_STATE_INIT] = {
2988 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2989 MLX5_QP_OPTPAR_RAE |
2990 MLX5_QP_OPTPAR_RWE |
2991 MLX5_QP_OPTPAR_PKEY_INDEX |
2992 MLX5_QP_OPTPAR_PRI_PORT,
2993 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2994 MLX5_QP_OPTPAR_PKEY_INDEX |
2995 MLX5_QP_OPTPAR_PRI_PORT,
2996 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2997 MLX5_QP_OPTPAR_Q_KEY |
2998 MLX5_QP_OPTPAR_PRI_PORT,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03002999 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3000 MLX5_QP_OPTPAR_RAE |
3001 MLX5_QP_OPTPAR_RWE |
3002 MLX5_QP_OPTPAR_PKEY_INDEX |
3003 MLX5_QP_OPTPAR_PRI_PORT,
Eli Cohene126ba92013-07-07 17:25:49 +03003004 },
3005 [MLX5_QP_STATE_RTR] = {
3006 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3007 MLX5_QP_OPTPAR_RRE |
3008 MLX5_QP_OPTPAR_RAE |
3009 MLX5_QP_OPTPAR_RWE |
3010 MLX5_QP_OPTPAR_PKEY_INDEX,
3011 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3012 MLX5_QP_OPTPAR_RWE |
3013 MLX5_QP_OPTPAR_PKEY_INDEX,
3014 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3015 MLX5_QP_OPTPAR_Q_KEY,
3016 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
3017 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03003018 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3019 MLX5_QP_OPTPAR_RRE |
3020 MLX5_QP_OPTPAR_RAE |
3021 MLX5_QP_OPTPAR_RWE |
3022 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03003023 },
3024 },
3025 [MLX5_QP_STATE_RTR] = {
3026 [MLX5_QP_STATE_RTS] = {
3027 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3028 MLX5_QP_OPTPAR_RRE |
3029 MLX5_QP_OPTPAR_RAE |
3030 MLX5_QP_OPTPAR_RWE |
3031 MLX5_QP_OPTPAR_PM_STATE |
3032 MLX5_QP_OPTPAR_RNR_TIMEOUT,
3033 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3034 MLX5_QP_OPTPAR_RWE |
3035 MLX5_QP_OPTPAR_PM_STATE,
3036 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003037 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3038 MLX5_QP_OPTPAR_RRE |
3039 MLX5_QP_OPTPAR_RAE |
3040 MLX5_QP_OPTPAR_RWE |
3041 MLX5_QP_OPTPAR_PM_STATE |
3042 MLX5_QP_OPTPAR_RNR_TIMEOUT,
Eli Cohene126ba92013-07-07 17:25:49 +03003043 },
3044 },
3045 [MLX5_QP_STATE_RTS] = {
3046 [MLX5_QP_STATE_RTS] = {
3047 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3048 MLX5_QP_OPTPAR_RAE |
3049 MLX5_QP_OPTPAR_RWE |
3050 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03003051 MLX5_QP_OPTPAR_PM_STATE |
3052 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003053 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03003054 MLX5_QP_OPTPAR_PM_STATE |
3055 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003056 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
3057 MLX5_QP_OPTPAR_SRQN |
3058 MLX5_QP_OPTPAR_CQN_RCV,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003059 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3060 MLX5_QP_OPTPAR_RAE |
3061 MLX5_QP_OPTPAR_RWE |
3062 MLX5_QP_OPTPAR_RNR_TIMEOUT |
3063 MLX5_QP_OPTPAR_PM_STATE |
3064 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003065 },
3066 },
3067 [MLX5_QP_STATE_SQER] = {
3068 [MLX5_QP_STATE_RTS] = {
3069 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
3070 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03003071 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03003072 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3073 MLX5_QP_OPTPAR_RWE |
3074 MLX5_QP_OPTPAR_RAE |
3075 MLX5_QP_OPTPAR_RRE,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003076 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3077 MLX5_QP_OPTPAR_RWE |
3078 MLX5_QP_OPTPAR_RAE |
3079 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03003080 },
3081 },
3082};
3083
3084static int ib_nr_to_mlx5_nr(int ib_mask)
3085{
3086 switch (ib_mask) {
3087 case IB_QP_STATE:
3088 return 0;
3089 case IB_QP_CUR_STATE:
3090 return 0;
3091 case IB_QP_EN_SQD_ASYNC_NOTIFY:
3092 return 0;
3093 case IB_QP_ACCESS_FLAGS:
3094 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
3095 MLX5_QP_OPTPAR_RAE;
3096 case IB_QP_PKEY_INDEX:
3097 return MLX5_QP_OPTPAR_PKEY_INDEX;
3098 case IB_QP_PORT:
3099 return MLX5_QP_OPTPAR_PRI_PORT;
3100 case IB_QP_QKEY:
3101 return MLX5_QP_OPTPAR_Q_KEY;
3102 case IB_QP_AV:
3103 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
3104 MLX5_QP_OPTPAR_PRI_PORT;
3105 case IB_QP_PATH_MTU:
3106 return 0;
3107 case IB_QP_TIMEOUT:
3108 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
3109 case IB_QP_RETRY_CNT:
3110 return MLX5_QP_OPTPAR_RETRY_COUNT;
3111 case IB_QP_RNR_RETRY:
3112 return MLX5_QP_OPTPAR_RNR_RETRY;
3113 case IB_QP_RQ_PSN:
3114 return 0;
3115 case IB_QP_MAX_QP_RD_ATOMIC:
3116 return MLX5_QP_OPTPAR_SRA_MAX;
3117 case IB_QP_ALT_PATH:
3118 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
3119 case IB_QP_MIN_RNR_TIMER:
3120 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
3121 case IB_QP_SQ_PSN:
3122 return 0;
3123 case IB_QP_MAX_DEST_RD_ATOMIC:
3124 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
3125 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
3126 case IB_QP_PATH_MIG_STATE:
3127 return MLX5_QP_OPTPAR_PM_STATE;
3128 case IB_QP_CAP:
3129 return 0;
3130 case IB_QP_DEST_QPN:
3131 return 0;
3132 }
3133 return 0;
3134}
3135
3136static int ib_mask_to_mlx5_opt(int ib_mask)
3137{
3138 int result = 0;
3139 int i;
3140
3141 for (i = 0; i < 8 * sizeof(int); i++) {
3142 if ((1 << i) & ib_mask)
3143 result |= ib_nr_to_mlx5_nr(1 << i);
3144 }
3145
3146 return result;
3147}
3148
Yishai Hadas34d57582018-09-20 21:39:21 +03003149static int modify_raw_packet_qp_rq(
3150 struct mlx5_ib_dev *dev, struct mlx5_ib_rq *rq, int new_state,
3151 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003152{
3153 void *in;
3154 void *rqc;
3155 int inlen;
3156 int err;
3157
3158 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003159 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003160 if (!in)
3161 return -ENOMEM;
3162
3163 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
Yishai Hadas34d57582018-09-20 21:39:21 +03003164 MLX5_SET(modify_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003165
3166 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
3167 MLX5_SET(rqc, rqc, state, new_state);
3168
Alex Veskereb49ab02016-08-28 12:25:53 +03003169 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
3170 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
3171 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02003172 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03003173 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
3174 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06003175 dev_info_once(
3176 &dev->ib_dev.dev,
3177 "RAW PACKET QP counters are not supported on current FW\n");
Alex Veskereb49ab02016-08-28 12:25:53 +03003178 }
3179
3180 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003181 if (err)
3182 goto out;
3183
3184 rq->state = new_state;
3185
3186out:
3187 kvfree(in);
3188 return err;
3189}
3190
Yishai Hadasc14003f2018-09-20 21:39:22 +03003191static int modify_raw_packet_qp_sq(
3192 struct mlx5_core_dev *dev, struct mlx5_ib_sq *sq, int new_state,
3193 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003194{
Bodong Wang7d29f342016-12-01 13:43:16 +02003195 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
Bodong Wang61147f32018-03-19 15:10:30 +02003196 struct mlx5_rate_limit old_rl = ibqp->rl;
3197 struct mlx5_rate_limit new_rl = old_rl;
3198 bool new_rate_added = false;
Bodong Wang7d29f342016-12-01 13:43:16 +02003199 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003200 void *in;
3201 void *sqc;
3202 int inlen;
3203 int err;
3204
3205 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003206 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003207 if (!in)
3208 return -ENOMEM;
3209
Yishai Hadasc14003f2018-09-20 21:39:22 +03003210 MLX5_SET(modify_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003211 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
3212
3213 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
3214 MLX5_SET(sqc, sqc, state, new_state);
3215
Bodong Wang7d29f342016-12-01 13:43:16 +02003216 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
3217 if (new_state != MLX5_SQC_STATE_RDY)
3218 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
3219 __func__);
3220 else
Bodong Wang61147f32018-03-19 15:10:30 +02003221 new_rl = raw_qp_param->rl;
Bodong Wang7d29f342016-12-01 13:43:16 +02003222 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003223
Bodong Wang61147f32018-03-19 15:10:30 +02003224 if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
3225 if (new_rl.rate) {
3226 err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003227 if (err) {
Bodong Wang61147f32018-03-19 15:10:30 +02003228 pr_err("Failed configuring rate limit(err %d): \
3229 rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
3230 err, new_rl.rate, new_rl.max_burst_sz,
3231 new_rl.typical_pkt_sz);
3232
Bodong Wang7d29f342016-12-01 13:43:16 +02003233 goto out;
3234 }
Bodong Wang61147f32018-03-19 15:10:30 +02003235 new_rate_added = true;
Bodong Wang7d29f342016-12-01 13:43:16 +02003236 }
3237
3238 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
Bodong Wang61147f32018-03-19 15:10:30 +02003239 /* index 0 means no limit */
Bodong Wang7d29f342016-12-01 13:43:16 +02003240 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
3241 }
3242
3243 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
3244 if (err) {
3245 /* Remove new rate from table if failed */
Bodong Wang61147f32018-03-19 15:10:30 +02003246 if (new_rate_added)
3247 mlx5_rl_remove_rate(dev, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003248 goto out;
3249 }
3250
3251 /* Only remove the old rate after new rate was set */
Bodong Wang61147f32018-03-19 15:10:30 +02003252 if ((old_rl.rate &&
3253 !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
Bodong Wang7d29f342016-12-01 13:43:16 +02003254 (new_state != MLX5_SQC_STATE_RDY))
Bodong Wang61147f32018-03-19 15:10:30 +02003255 mlx5_rl_remove_rate(dev, &old_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003256
Bodong Wang61147f32018-03-19 15:10:30 +02003257 ibqp->rl = new_rl;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003258 sq->state = new_state;
3259
3260out:
3261 kvfree(in);
3262 return err;
3263}
3264
3265static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03003266 const struct mlx5_modify_raw_qp_param *raw_qp_param,
3267 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003268{
3269 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
3270 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
3271 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02003272 int modify_rq = !!qp->rq.wqe_cnt;
3273 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003274 int rq_state;
3275 int sq_state;
3276 int err;
3277
Alex Vesker0680efa2016-08-28 12:25:52 +03003278 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003279 case MLX5_CMD_OP_RST2INIT_QP:
3280 rq_state = MLX5_RQC_STATE_RDY;
3281 sq_state = MLX5_SQC_STATE_RDY;
3282 break;
3283 case MLX5_CMD_OP_2ERR_QP:
3284 rq_state = MLX5_RQC_STATE_ERR;
3285 sq_state = MLX5_SQC_STATE_ERR;
3286 break;
3287 case MLX5_CMD_OP_2RST_QP:
3288 rq_state = MLX5_RQC_STATE_RST;
3289 sq_state = MLX5_SQC_STATE_RST;
3290 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003291 case MLX5_CMD_OP_RTR2RTS_QP:
3292 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02003293 if (raw_qp_param->set_mask ==
3294 MLX5_RAW_QP_RATE_LIMIT) {
3295 modify_rq = 0;
3296 sq_state = sq->state;
3297 } else {
3298 return raw_qp_param->set_mask ? -EINVAL : 0;
3299 }
3300 break;
3301 case MLX5_CMD_OP_INIT2INIT_QP:
3302 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03003303 if (raw_qp_param->set_mask)
3304 return -EINVAL;
3305 else
3306 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003307 default:
3308 WARN_ON(1);
3309 return -EINVAL;
3310 }
3311
Bodong Wang7d29f342016-12-01 13:43:16 +02003312 if (modify_rq) {
Yishai Hadas34d57582018-09-20 21:39:21 +03003313 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param,
3314 qp->ibqp.pd);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003315 if (err)
3316 return err;
3317 }
3318
Bodong Wang7d29f342016-12-01 13:43:16 +02003319 if (modify_sq) {
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003320 struct mlx5_flow_handle *flow_rule;
3321
Aviv Heller13eab212016-09-18 20:48:04 +03003322 if (tx_affinity) {
3323 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003324 tx_affinity,
3325 qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003326 if (err)
3327 return err;
3328 }
3329
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003330 flow_rule = create_flow_rule_vport_sq(dev, sq,
3331 raw_qp_param->port);
3332 if (IS_ERR(flow_rule))
Colin Ian King1db86312019-04-12 11:40:17 +01003333 return PTR_ERR(flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003334
3335 err = modify_raw_packet_qp_sq(dev->mdev, sq, sq_state,
3336 raw_qp_param, qp->ibqp.pd);
3337 if (err) {
3338 if (flow_rule)
3339 mlx5_del_flow_rules(flow_rule);
3340 return err;
3341 }
3342
3343 if (flow_rule) {
3344 destroy_flow_rule_vport_sq(sq);
3345 sq->flow_rule = flow_rule;
3346 }
3347
3348 return err;
Aviv Heller13eab212016-09-18 20:48:04 +03003349 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003350
3351 return 0;
3352}
3353
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003354static unsigned int get_tx_affinity(struct mlx5_ib_dev *dev,
3355 struct mlx5_ib_pd *pd,
3356 struct mlx5_ib_qp_base *qp_base,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003357 u8 port_num, struct ib_udata *udata)
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003358{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003359 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
3360 udata, struct mlx5_ib_ucontext, ibucontext);
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003361 unsigned int tx_port_affinity;
3362
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003363 if (ucontext) {
3364 tx_port_affinity = (unsigned int)atomic_add_return(
3365 1, &ucontext->tx_port_affinity) %
3366 MLX5_MAX_PORTS +
3367 1;
3368 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x ucontext %p\n",
3369 tx_port_affinity, qp_base->mqp.qpn, ucontext);
3370 } else {
3371 tx_port_affinity =
3372 (unsigned int)atomic_add_return(
Mark Bloch95579e72019-03-28 15:27:33 +02003373 1, &dev->port[port_num].roce.tx_port_affinity) %
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003374 MLX5_MAX_PORTS +
3375 1;
3376 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x\n",
3377 tx_port_affinity, qp_base->mqp.qpn);
3378 }
3379
3380 return tx_port_affinity;
3381}
3382
Mark Zhangd14133d2019-07-02 13:02:36 +03003383static int __mlx5_ib_qp_set_counter(struct ib_qp *qp,
3384 struct rdma_counter *counter)
3385{
3386 struct mlx5_ib_dev *dev = to_mdev(qp->device);
3387 struct mlx5_ib_qp *mqp = to_mqp(qp);
3388 struct mlx5_qp_context context = {};
Mark Zhangd14133d2019-07-02 13:02:36 +03003389 struct mlx5_ib_qp_base *base;
3390 u32 set_id;
3391
3392 if (!MLX5_CAP_GEN(dev->mdev, rts2rts_qp_counters_set_id))
3393 return 0;
3394
Parav Pandit3e1f0002019-07-23 10:31:17 +03003395 if (counter)
Mark Zhangd14133d2019-07-02 13:02:36 +03003396 set_id = counter->id;
Parav Pandit3e1f0002019-07-23 10:31:17 +03003397 else
3398 set_id = mlx5_ib_get_counters_id(dev, mqp->port - 1);
Mark Zhangd14133d2019-07-02 13:02:36 +03003399
3400 base = &mqp->trans_qp.base;
3401 context.qp_counter_set_usr_page &= cpu_to_be32(0xffffff);
3402 context.qp_counter_set_usr_page |= cpu_to_be32(set_id << 24);
3403 return mlx5_core_qp_modify(dev->mdev,
3404 MLX5_CMD_OP_RTS2RTS_QP,
3405 MLX5_QP_OPTPAR_COUNTER_SET_ID,
3406 &context, &base->mqp);
3407}
3408
Eli Cohene126ba92013-07-07 17:25:49 +03003409static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
3410 const struct ib_qp_attr *attr, int attr_mask,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003411 enum ib_qp_state cur_state,
3412 enum ib_qp_state new_state,
3413 const struct mlx5_ib_modify_qp *ucmd,
3414 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003415{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003416 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
3417 [MLX5_QP_STATE_RST] = {
3418 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3419 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3420 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
3421 },
3422 [MLX5_QP_STATE_INIT] = {
3423 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3424 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3425 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
3426 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
3427 },
3428 [MLX5_QP_STATE_RTR] = {
3429 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3430 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3431 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
3432 },
3433 [MLX5_QP_STATE_RTS] = {
3434 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3435 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3436 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
3437 },
3438 [MLX5_QP_STATE_SQD] = {
3439 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3440 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3441 },
3442 [MLX5_QP_STATE_SQER] = {
3443 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3444 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3445 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
3446 },
3447 [MLX5_QP_STATE_ERR] = {
3448 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3449 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3450 }
3451 };
3452
Eli Cohene126ba92013-07-07 17:25:49 +03003453 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3454 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02003455 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03003456 struct mlx5_ib_cq *send_cq, *recv_cq;
3457 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03003458 struct mlx5_ib_pd *pd;
3459 enum mlx5_qp_state mlx5_cur, mlx5_new;
3460 enum mlx5_qp_optpar optpar;
Mark Zhangd14133d2019-07-02 13:02:36 +03003461 u32 set_id = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003462 int mlx5_st;
3463 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003464 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03003465 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003466
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003467 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
3468 qp->qp_sub_type : ibqp->qp_type);
3469 if (mlx5_st < 0)
3470 return -EINVAL;
3471
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003472 context = kzalloc(sizeof(*context), GFP_KERNEL);
3473 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03003474 return -ENOMEM;
3475
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003476 pd = get_pd(qp);
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003477 context->flags = cpu_to_be32(mlx5_st << 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003478
3479 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
3480 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3481 } else {
3482 switch (attr->path_mig_state) {
3483 case IB_MIG_MIGRATED:
3484 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3485 break;
3486 case IB_MIG_REARM:
3487 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
3488 break;
3489 case IB_MIG_ARMED:
3490 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
3491 break;
3492 }
3493 }
3494
Aviv Heller13eab212016-09-18 20:48:04 +03003495 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
3496 if ((ibqp->qp_type == IB_QPT_RC) ||
3497 (ibqp->qp_type == IB_QPT_UD &&
3498 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
3499 (ibqp->qp_type == IB_QPT_UC) ||
3500 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
3501 (ibqp->qp_type == IB_QPT_XRC_INI) ||
3502 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
Aviv Heller7c34ec12018-08-23 13:47:53 +03003503 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02003504 u8 p = mlx5_core_native_port_num(dev->mdev) - 1;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003505 tx_affinity = get_tx_affinity(dev, pd, base, p,
3506 udata);
Aviv Heller13eab212016-09-18 20:48:04 +03003507 context->flags |= cpu_to_be32(tx_affinity << 24);
3508 }
3509 }
3510 }
3511
Haggai Erand16e91d2016-02-29 15:45:05 +02003512 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003513 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003514 } else if ((ibqp->qp_type == IB_QPT_UD &&
3515 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003516 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
3517 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
3518 } else if (attr_mask & IB_QP_PATH_MTU) {
3519 if (attr->path_mtu < IB_MTU_256 ||
3520 attr->path_mtu > IB_MTU_4096) {
3521 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3522 err = -EINVAL;
3523 goto out;
3524 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003525 context->mtu_msgmax = (attr->path_mtu << 5) |
3526 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03003527 }
3528
3529 if (attr_mask & IB_QP_DEST_QPN)
3530 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
3531
3532 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003533 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003534
3535 /* todo implement counter_index functionality */
3536
3537 if (is_sqp(ibqp->qp_type))
3538 context->pri_path.port = qp->port;
3539
3540 if (attr_mask & IB_QP_PORT)
3541 context->pri_path.port = attr->port_num;
3542
3543 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003544 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003545 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003546 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003547 if (err)
3548 goto out;
3549 }
3550
3551 if (attr_mask & IB_QP_TIMEOUT)
3552 context->pri_path.ackto_lt |= attr->timeout << 3;
3553
3554 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003555 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3556 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003557 attr->alt_port_num,
3558 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3559 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003560 if (err)
3561 goto out;
3562 }
3563
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003564 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3565 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003566
3567 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3568 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3569 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3570 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3571
3572 if (attr_mask & IB_QP_RNR_RETRY)
3573 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3574
3575 if (attr_mask & IB_QP_RETRY_CNT)
3576 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3577
3578 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3579 if (attr->max_rd_atomic)
3580 context->params1 |=
3581 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3582 }
3583
3584 if (attr_mask & IB_QP_SQ_PSN)
3585 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3586
3587 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3588 if (attr->max_dest_rd_atomic)
3589 context->params2 |=
3590 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3591 }
3592
Yonatan Cohena60109d2018-10-10 09:25:16 +03003593 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08003594 __be32 access_flags;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003595
3596 err = to_mlx5_access_flags(qp, attr, attr_mask, &access_flags);
3597 if (err)
3598 goto out;
3599
3600 context->params2 |= access_flags;
3601 }
Eli Cohene126ba92013-07-07 17:25:49 +03003602
3603 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3604 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3605
3606 if (attr_mask & IB_QP_RQ_PSN)
3607 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3608
3609 if (attr_mask & IB_QP_QKEY)
3610 context->qkey = cpu_to_be32(attr->qkey);
3611
3612 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3613 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3614
Mark Bloch0837e862016-06-17 15:10:55 +03003615 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3616 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3617 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003618
3619 /* Underlay port should be used - index 0 function per port */
3620 if (qp->flags & MLX5_IB_QP_UNDERLAY)
3621 port_num = 0;
3622
Mark Zhangd14133d2019-07-02 13:02:36 +03003623 if (ibqp->counter)
3624 set_id = ibqp->counter->id;
3625 else
Parav Pandit3e1f0002019-07-23 10:31:17 +03003626 set_id = mlx5_ib_get_counters_id(dev, port_num);
Mark Bloch0837e862016-06-17 15:10:55 +03003627 context->qp_counter_set_usr_page |=
Mark Zhangd14133d2019-07-02 13:02:36 +03003628 cpu_to_be32(set_id << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003629 }
3630
Eli Cohene126ba92013-07-07 17:25:49 +03003631 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3632 context->sq_crq_size |= cpu_to_be16(1 << 4);
3633
Haggai Eranb11a4f92016-02-29 15:45:03 +02003634 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
3635 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003636
3637 mlx5_cur = to_mlx5_state(cur_state);
3638 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003639
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003640 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003641 !optab[mlx5_cur][mlx5_new]) {
3642 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003643 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003644 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003645
3646 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003647 optpar = ib_mask_to_mlx5_opt(attr_mask);
3648 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003649
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003650 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3651 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003652 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3653
3654 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003655 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Mark Zhangd14133d2019-07-02 13:02:36 +03003656 raw_qp_param.rq_q_ctr_id = set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003657 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3658 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003659
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003660 if (attr_mask & IB_QP_PORT)
3661 raw_qp_param.port = attr->port_num;
3662
Bodong Wang7d29f342016-12-01 13:43:16 +02003663 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003664 raw_qp_param.rl.rate = attr->rate_limit;
3665
3666 if (ucmd->burst_info.max_burst_sz) {
3667 if (attr->rate_limit &&
3668 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3669 raw_qp_param.rl.max_burst_sz =
3670 ucmd->burst_info.max_burst_sz;
3671 } else {
3672 err = -EINVAL;
3673 goto out;
3674 }
3675 }
3676
3677 if (ucmd->burst_info.typical_pkt_sz) {
3678 if (attr->rate_limit &&
3679 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3680 raw_qp_param.rl.typical_pkt_sz =
3681 ucmd->burst_info.typical_pkt_sz;
3682 } else {
3683 err = -EINVAL;
3684 goto out;
3685 }
3686 }
3687
Bodong Wang7d29f342016-12-01 13:43:16 +02003688 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3689 }
3690
Aviv Heller13eab212016-09-18 20:48:04 +03003691 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003692 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003693 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003694 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003695 }
3696
Eli Cohene126ba92013-07-07 17:25:49 +03003697 if (err)
3698 goto out;
3699
3700 qp->state = new_state;
3701
3702 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003703 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003704 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003705 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003706 if (attr_mask & IB_QP_PORT)
3707 qp->port = attr->port_num;
3708 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003709 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003710
3711 /*
3712 * If we moved a kernel QP to RESET, clean up all old CQ
3713 * entries and reinitialize the QP.
3714 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02003715 if (new_state == IB_QPS_RESET &&
3716 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003717 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003718 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3719 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003720 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003721
3722 qp->rq.head = 0;
3723 qp->rq.tail = 0;
3724 qp->sq.head = 0;
3725 qp->sq.tail = 0;
3726 qp->sq.cur_post = 0;
Guy Levi34f4c952018-11-26 08:15:50 +02003727 if (qp->sq.wqe_cnt)
3728 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003729 qp->db.db[MLX5_RCV_DBR] = 0;
3730 qp->db.db[MLX5_SND_DBR] = 0;
3731 }
3732
Mark Zhangd14133d2019-07-02 13:02:36 +03003733 if ((new_state == IB_QPS_RTS) && qp->counter_pending) {
3734 err = __mlx5_ib_qp_set_counter(ibqp, ibqp->counter);
3735 if (!err)
3736 qp->counter_pending = 0;
3737 }
3738
Eli Cohene126ba92013-07-07 17:25:49 +03003739out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003740 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003741 return err;
3742}
3743
Moni Shouac32a4f22018-01-02 16:19:32 +02003744static inline bool is_valid_mask(int mask, int req, int opt)
3745{
3746 if ((mask & req) != req)
3747 return false;
3748
3749 if (mask & ~(req | opt))
3750 return false;
3751
3752 return true;
3753}
3754
3755/* check valid transition for driver QP types
3756 * for now the only QP type that this function supports is DCI
3757 */
3758static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3759 enum ib_qp_attr_mask attr_mask)
3760{
3761 int req = IB_QP_STATE;
3762 int opt = 0;
3763
Moni Shoua99ed7482018-09-12 09:33:55 +03003764 if (new_state == IB_QPS_RESET) {
3765 return is_valid_mask(attr_mask, req, opt);
3766 } else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Moni Shouac32a4f22018-01-02 16:19:32 +02003767 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
3768 return is_valid_mask(attr_mask, req, opt);
3769 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3770 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3771 return is_valid_mask(attr_mask, req, opt);
3772 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3773 req |= IB_QP_PATH_MTU;
Artemy Kovalyov5ec03042018-11-05 08:12:07 +02003774 opt = IB_QP_PKEY_INDEX | IB_QP_AV;
Moni Shouac32a4f22018-01-02 16:19:32 +02003775 return is_valid_mask(attr_mask, req, opt);
3776 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3777 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3778 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3779 opt = IB_QP_MIN_RNR_TIMER;
3780 return is_valid_mask(attr_mask, req, opt);
3781 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3782 opt = IB_QP_MIN_RNR_TIMER;
3783 return is_valid_mask(attr_mask, req, opt);
3784 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3785 return is_valid_mask(attr_mask, req, opt);
3786 }
3787 return false;
3788}
3789
Moni Shoua776a3902018-01-02 16:19:33 +02003790/* mlx5_ib_modify_dct: modify a DCT QP
3791 * valid transitions are:
3792 * RESET to INIT: must set access_flags, pkey_index and port
3793 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3794 * mtu, gid_index and hop_limit
3795 * Other transitions and attributes are illegal
3796 */
3797static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3798 int attr_mask, struct ib_udata *udata)
3799{
3800 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3801 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3802 enum ib_qp_state cur_state, new_state;
3803 int err = 0;
3804 int required = IB_QP_STATE;
3805 void *dctc;
3806
3807 if (!(attr_mask & IB_QP_STATE))
3808 return -EINVAL;
3809
3810 cur_state = qp->state;
3811 new_state = attr->qp_state;
3812
3813 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3814 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03003815 u16 set_id;
3816
Moni Shoua776a3902018-01-02 16:19:33 +02003817 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3818 if (!is_valid_mask(attr_mask, required, 0))
3819 return -EINVAL;
3820
3821 if (attr->port_num == 0 ||
3822 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3823 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3824 attr->port_num, dev->num_ports);
3825 return -EINVAL;
3826 }
3827 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3828 MLX5_SET(dctc, dctc, rre, 1);
3829 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3830 MLX5_SET(dctc, dctc, rwe, 1);
3831 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03003832 int atomic_mode;
3833
3834 atomic_mode = get_atomic_mode(dev, MLX5_IB_QPT_DCT);
3835 if (atomic_mode < 0)
Moni Shoua776a3902018-01-02 16:19:33 +02003836 return -EOPNOTSUPP;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003837
3838 MLX5_SET(dctc, dctc, atomic_mode, atomic_mode);
Moni Shoua776a3902018-01-02 16:19:33 +02003839 MLX5_SET(dctc, dctc, rae, 1);
Moni Shoua776a3902018-01-02 16:19:33 +02003840 }
3841 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3842 MLX5_SET(dctc, dctc, port, attr->port_num);
Parav Pandit3e1f0002019-07-23 10:31:17 +03003843
3844 set_id = mlx5_ib_get_counters_id(dev, attr->port_num - 1);
3845 MLX5_SET(dctc, dctc, counter_set_id, set_id);
Moni Shoua776a3902018-01-02 16:19:33 +02003846
3847 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3848 struct mlx5_ib_modify_qp_resp resp = {};
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003849 u32 out[MLX5_ST_SZ_DW(create_dct_out)] = {0};
Moni Shoua776a3902018-01-02 16:19:33 +02003850 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3851 sizeof(resp.dctn);
3852
3853 if (udata->outlen < min_resp_len)
3854 return -EINVAL;
3855 resp.response_length = min_resp_len;
3856
3857 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3858 if (!is_valid_mask(attr_mask, required, 0))
3859 return -EINVAL;
3860 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3861 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3862 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3863 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3864 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3865 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3866
3867 err = mlx5_core_create_dct(dev->mdev, &qp->dct.mdct, qp->dct.in,
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003868 MLX5_ST_SZ_BYTES(create_dct_in), out,
3869 sizeof(out));
Moni Shoua776a3902018-01-02 16:19:33 +02003870 if (err)
3871 return err;
3872 resp.dctn = qp->dct.mdct.mqp.qpn;
3873 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3874 if (err) {
3875 mlx5_core_destroy_dct(dev->mdev, &qp->dct.mdct);
3876 return err;
3877 }
3878 } else {
3879 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3880 return -EINVAL;
3881 }
3882 if (err)
3883 qp->state = IB_QPS_ERR;
3884 else
3885 qp->state = new_state;
3886 return err;
3887}
3888
Eli Cohene126ba92013-07-07 17:25:49 +03003889int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3890 int attr_mask, struct ib_udata *udata)
3891{
3892 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3893 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02003894 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02003895 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003896 enum ib_qp_state cur_state, new_state;
Bodong Wang61147f32018-03-19 15:10:30 +02003897 size_t required_cmd_sz;
Eli Cohene126ba92013-07-07 17:25:49 +03003898 int err = -EINVAL;
3899 int port;
3900
Yishai Hadas28d61372016-05-23 15:20:56 +03003901 if (ibqp->rwq_ind_tbl)
3902 return -ENOSYS;
3903
Bodong Wang61147f32018-03-19 15:10:30 +02003904 if (udata && udata->inlen) {
3905 required_cmd_sz = offsetof(typeof(ucmd), reserved) +
3906 sizeof(ucmd.reserved);
3907 if (udata->inlen < required_cmd_sz)
3908 return -EINVAL;
3909
3910 if (udata->inlen > sizeof(ucmd) &&
3911 !ib_is_udata_cleared(udata, sizeof(ucmd),
3912 udata->inlen - sizeof(ucmd)))
3913 return -EOPNOTSUPP;
3914
3915 if (ib_copy_from_udata(&ucmd, udata,
3916 min(udata->inlen, sizeof(ucmd))))
3917 return -EFAULT;
3918
3919 if (ucmd.comp_mask ||
3920 memchr_inv(&ucmd.reserved, 0, sizeof(ucmd.reserved)) ||
3921 memchr_inv(&ucmd.burst_info.reserved, 0,
3922 sizeof(ucmd.burst_info.reserved)))
3923 return -EOPNOTSUPP;
3924 }
3925
Haggai Erand16e91d2016-02-29 15:45:05 +02003926 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3927 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3928
Moni Shouac32a4f22018-01-02 16:19:32 +02003929 if (ibqp->qp_type == IB_QPT_DRIVER)
3930 qp_type = qp->qp_sub_type;
3931 else
3932 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3933 IB_QPT_GSI : ibqp->qp_type;
3934
Moni Shoua776a3902018-01-02 16:19:33 +02003935 if (qp_type == MLX5_IB_QPT_DCT)
3936 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02003937
Eli Cohene126ba92013-07-07 17:25:49 +03003938 mutex_lock(&qp->mutex);
3939
3940 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
3941 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
3942
Achiad Shochat2811ba52015-12-23 18:47:24 +02003943 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
3944 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003945 }
3946
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003947 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
3948 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
3949 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
3950 attr_mask);
3951 goto out;
3952 }
3953 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02003954 qp_type != MLX5_IB_QPT_DCI &&
Kamal Heibd31131b2018-10-02 16:11:21 +03003955 !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
3956 attr_mask)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003957 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3958 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003959 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02003960 } else if (qp_type == MLX5_IB_QPT_DCI &&
3961 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
3962 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3963 cur_state, new_state, qp_type, attr_mask);
3964 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003965 }
Eli Cohene126ba92013-07-07 17:25:49 +03003966
3967 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003968 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003969 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003970 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3971 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003972 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003973 }
Eli Cohene126ba92013-07-07 17:25:49 +03003974
3975 if (attr_mask & IB_QP_PKEY_INDEX) {
3976 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003977 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02003978 dev->mdev->port_caps[port - 1].pkey_table_len) {
3979 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
3980 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003981 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003982 }
Eli Cohene126ba92013-07-07 17:25:49 +03003983 }
3984
3985 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003986 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003987 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
3988 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
3989 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003990 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003991 }
Eli Cohene126ba92013-07-07 17:25:49 +03003992
3993 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003994 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003995 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
3996 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
3997 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003998 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003999 }
Eli Cohene126ba92013-07-07 17:25:49 +03004000
4001 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
4002 err = 0;
4003 goto out;
4004 }
4005
Bodong Wang61147f32018-03-19 15:10:30 +02004006 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02004007 new_state, &ucmd, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03004008
4009out:
4010 mutex_unlock(&qp->mutex);
4011 return err;
4012}
4013
Guy Levi34f4c952018-11-26 08:15:50 +02004014static void _handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
4015 u32 wqe_sz, void **cur_edge)
4016{
4017 u32 idx;
4018
4019 idx = (sq->cur_post + (wqe_sz >> 2)) & (sq->wqe_cnt - 1);
4020 *cur_edge = get_sq_edge(sq, idx);
4021
4022 *seg = mlx5_frag_buf_get_wqe(&sq->fbc, idx);
4023}
4024
4025/* handle_post_send_edge - Check if we get to SQ edge. If yes, update to the
4026 * next nearby edge and get new address translation for current WQE position.
4027 * @sq - SQ buffer.
4028 * @seg: Current WQE position (16B aligned).
4029 * @wqe_sz: Total current WQE size [16B].
4030 * @cur_edge: Updated current edge.
4031 */
4032static inline void handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
4033 u32 wqe_sz, void **cur_edge)
4034{
4035 if (likely(*seg != *cur_edge))
4036 return;
4037
4038 _handle_post_send_edge(sq, seg, wqe_sz, cur_edge);
4039}
4040
4041/* memcpy_send_wqe - copy data from src to WQE and update the relevant WQ's
4042 * pointers. At the end @seg is aligned to 16B regardless the copied size.
4043 * @sq - SQ buffer.
4044 * @cur_edge: Updated current edge.
4045 * @seg: Current WQE position (16B aligned).
4046 * @wqe_sz: Total current WQE size [16B].
4047 * @src: Pointer to copy from.
4048 * @n: Number of bytes to copy.
4049 */
4050static inline void memcpy_send_wqe(struct mlx5_ib_wq *sq, void **cur_edge,
4051 void **seg, u32 *wqe_sz, const void *src,
4052 size_t n)
4053{
4054 while (likely(n)) {
4055 size_t leftlen = *cur_edge - *seg;
4056 size_t copysz = min_t(size_t, leftlen, n);
4057 size_t stride;
4058
4059 memcpy(*seg, src, copysz);
4060
4061 n -= copysz;
4062 src += copysz;
4063 stride = !n ? ALIGN(copysz, 16) : copysz;
4064 *seg += stride;
4065 *wqe_sz += stride >> 4;
4066 handle_post_send_edge(sq, seg, *wqe_sz, cur_edge);
4067 }
4068}
4069
Eli Cohene126ba92013-07-07 17:25:49 +03004070static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
4071{
4072 struct mlx5_ib_cq *cq;
4073 unsigned cur;
4074
4075 cur = wq->head - wq->tail;
4076 if (likely(cur + nreq < wq->max_post))
4077 return 0;
4078
4079 cq = to_mcq(ib_cq);
4080 spin_lock(&cq->lock);
4081 cur = wq->head - wq->tail;
4082 spin_unlock(&cq->lock);
4083
4084 return cur + nreq >= wq->max_post;
4085}
4086
4087static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
4088 u64 remote_addr, u32 rkey)
4089{
4090 rseg->raddr = cpu_to_be64(remote_addr);
4091 rseg->rkey = cpu_to_be32(rkey);
4092 rseg->reserved = 0;
4093}
4094
Guy Levi34f4c952018-11-26 08:15:50 +02004095static void set_eth_seg(const struct ib_send_wr *wr, struct mlx5_ib_qp *qp,
4096 void **seg, int *size, void **cur_edge)
Erez Shitritf0313962016-02-21 16:27:17 +02004097{
Guy Levi34f4c952018-11-26 08:15:50 +02004098 struct mlx5_wqe_eth_seg *eseg = *seg;
Erez Shitritf0313962016-02-21 16:27:17 +02004099
4100 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
4101
4102 if (wr->send_flags & IB_SEND_IP_CSUM)
4103 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
4104 MLX5_ETH_WQE_L4_CSUM;
4105
Erez Shitritf0313962016-02-21 16:27:17 +02004106 if (wr->opcode == IB_WR_LSO) {
4107 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Guy Levi34f4c952018-11-26 08:15:50 +02004108 size_t left, copysz;
Erez Shitritf0313962016-02-21 16:27:17 +02004109 void *pdata = ud_wr->header;
Guy Levi34f4c952018-11-26 08:15:50 +02004110 size_t stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004111
4112 left = ud_wr->hlen;
4113 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02004114 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02004115
Guy Levi34f4c952018-11-26 08:15:50 +02004116 /* memcpy_send_wqe should get a 16B align address. Hence, we
4117 * first copy up to the current edge and then, if needed,
4118 * fall-through to memcpy_send_wqe.
Erez Shitritf0313962016-02-21 16:27:17 +02004119 */
Guy Levi34f4c952018-11-26 08:15:50 +02004120 copysz = min_t(u64, *cur_edge - (void *)eseg->inline_hdr.start,
4121 left);
4122 memcpy(eseg->inline_hdr.start, pdata, copysz);
4123 stride = ALIGN(sizeof(struct mlx5_wqe_eth_seg) -
4124 sizeof(eseg->inline_hdr.start) + copysz, 16);
4125 *size += stride / 16;
4126 *seg += stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004127
Guy Levi34f4c952018-11-26 08:15:50 +02004128 if (copysz < left) {
4129 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02004130 left -= copysz;
4131 pdata += copysz;
Guy Levi34f4c952018-11-26 08:15:50 +02004132 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, pdata,
4133 left);
Erez Shitritf0313962016-02-21 16:27:17 +02004134 }
Guy Levi34f4c952018-11-26 08:15:50 +02004135
4136 return;
Erez Shitritf0313962016-02-21 16:27:17 +02004137 }
4138
Guy Levi34f4c952018-11-26 08:15:50 +02004139 *seg += sizeof(struct mlx5_wqe_eth_seg);
4140 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
Erez Shitritf0313962016-02-21 16:27:17 +02004141}
4142
Eli Cohene126ba92013-07-07 17:25:49 +03004143static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004144 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004145{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004146 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
4147 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
4148 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004149}
4150
4151static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
4152{
4153 dseg->byte_count = cpu_to_be32(sg->length);
4154 dseg->lkey = cpu_to_be32(sg->lkey);
4155 dseg->addr = cpu_to_be64(sg->addr);
4156}
4157
Artemy Kovalyov31616252017-01-02 11:37:42 +02004158static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03004159{
Artemy Kovalyov31616252017-01-02 11:37:42 +02004160 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
4161 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03004162}
4163
Moni Shoua841b07f2019-08-15 11:38:34 +03004164static __be64 frwr_mkey_mask(bool atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004165{
4166 u64 result;
4167
4168 result = MLX5_MKEY_MASK_LEN |
4169 MLX5_MKEY_MASK_PAGE_SIZE |
4170 MLX5_MKEY_MASK_START_ADDR |
4171 MLX5_MKEY_MASK_EN_RINVAL |
4172 MLX5_MKEY_MASK_KEY |
4173 MLX5_MKEY_MASK_LR |
4174 MLX5_MKEY_MASK_LW |
4175 MLX5_MKEY_MASK_RR |
4176 MLX5_MKEY_MASK_RW |
Eli Cohene126ba92013-07-07 17:25:49 +03004177 MLX5_MKEY_MASK_SMALL_FENCE |
4178 MLX5_MKEY_MASK_FREE;
4179
Moni Shoua841b07f2019-08-15 11:38:34 +03004180 if (atomic)
4181 result |= MLX5_MKEY_MASK_A;
4182
Eli Cohene126ba92013-07-07 17:25:49 +03004183 return cpu_to_be64(result);
4184}
4185
Sagi Grimberge6631812014-02-23 14:19:11 +02004186static __be64 sig_mkey_mask(void)
4187{
4188 u64 result;
4189
4190 result = MLX5_MKEY_MASK_LEN |
4191 MLX5_MKEY_MASK_PAGE_SIZE |
4192 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004193 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02004194 MLX5_MKEY_MASK_EN_RINVAL |
4195 MLX5_MKEY_MASK_KEY |
4196 MLX5_MKEY_MASK_LR |
4197 MLX5_MKEY_MASK_LW |
4198 MLX5_MKEY_MASK_RR |
4199 MLX5_MKEY_MASK_RW |
4200 MLX5_MKEY_MASK_SMALL_FENCE |
4201 MLX5_MKEY_MASK_FREE |
4202 MLX5_MKEY_MASK_BSF_EN;
4203
4204 return cpu_to_be64(result);
4205}
4206
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004207static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Moni Shoua841b07f2019-08-15 11:38:34 +03004208 struct mlx5_ib_mr *mr, u8 flags, bool atomic)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004209{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004210 int size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004211
4212 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004213
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004214 umr->flags = flags;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004215 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Moni Shoua841b07f2019-08-15 11:38:34 +03004216 umr->mkey_mask = frwr_mkey_mask(atomic);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004217}
4218
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004219static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03004220{
4221 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004222 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03004223 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03004224}
4225
Artemy Kovalyov31616252017-01-02 11:37:42 +02004226static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004227{
4228 u64 result;
4229
Artemy Kovalyov31616252017-01-02 11:37:42 +02004230 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02004231 MLX5_MKEY_MASK_FREE;
4232
4233 return cpu_to_be64(result);
4234}
4235
Artemy Kovalyov31616252017-01-02 11:37:42 +02004236static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004237{
4238 u64 result;
4239
4240 result = MLX5_MKEY_MASK_FREE;
4241
4242 return cpu_to_be64(result);
4243}
4244
Noa Osherovich56e11d62016-02-29 16:46:51 +02004245static __be64 get_umr_update_translation_mask(void)
4246{
4247 u64 result;
4248
4249 result = MLX5_MKEY_MASK_LEN |
4250 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004251 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004252
4253 return cpu_to_be64(result);
4254}
4255
Artemy Kovalyov31616252017-01-02 11:37:42 +02004256static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02004257{
4258 u64 result;
4259
Artemy Kovalyov31616252017-01-02 11:37:42 +02004260 result = MLX5_MKEY_MASK_LR |
4261 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004262 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004263 MLX5_MKEY_MASK_RW;
4264
4265 if (atomic)
4266 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004267
4268 return cpu_to_be64(result);
4269}
4270
4271static __be64 get_umr_update_pd_mask(void)
4272{
4273 u64 result;
4274
Artemy Kovalyov31616252017-01-02 11:37:42 +02004275 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004276
4277 return cpu_to_be64(result);
4278}
4279
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004280static int umr_check_mkey_mask(struct mlx5_ib_dev *dev, u64 mask)
4281{
4282 if ((mask & MLX5_MKEY_MASK_PAGE_SIZE &&
4283 MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) ||
4284 (mask & MLX5_MKEY_MASK_A &&
4285 MLX5_CAP_GEN(dev->mdev, umr_modify_atomic_disabled)))
4286 return -EPERM;
4287 return 0;
4288}
4289
4290static int set_reg_umr_segment(struct mlx5_ib_dev *dev,
4291 struct mlx5_wqe_umr_ctrl_seg *umr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004292 const struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004293{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004294 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03004295
4296 memset(umr, 0, sizeof(*umr));
4297
Yishai Hadas6a053952019-07-23 09:57:25 +03004298 if (!umrwr->ignore_free_state) {
4299 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
4300 /* fail if free */
4301 umr->flags = MLX5_UMR_CHECK_FREE;
4302 else
4303 /* fail if not free */
4304 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
4305 }
Haggai Eran968e78d2014-12-11 17:04:11 +02004306
Artemy Kovalyov31616252017-01-02 11:37:42 +02004307 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
4308 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
4309 u64 offset = get_xlt_octo(umrwr->offset);
4310
4311 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
4312 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
4313 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004314 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02004315 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
4316 umr->mkey_mask |= get_umr_update_translation_mask();
4317 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
4318 umr->mkey_mask |= get_umr_update_access_mask(atomic);
4319 umr->mkey_mask |= get_umr_update_pd_mask();
4320 }
4321 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
4322 umr->mkey_mask |= get_umr_enable_mr_mask();
4323 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
4324 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03004325
4326 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02004327 umr->flags |= MLX5_UMR_INLINE;
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004328
4329 return umr_check_mkey_mask(dev, be64_to_cpu(umr->mkey_mask));
Eli Cohene126ba92013-07-07 17:25:49 +03004330}
4331
4332static u8 get_umr_flags(int acc)
4333{
4334 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
4335 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
4336 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
4337 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02004338 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004339}
4340
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004341static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
4342 struct mlx5_ib_mr *mr,
4343 u32 key, int access)
4344{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004345 int ndescs = ALIGN(mr->ndescs + mr->meta_ndescs, 8) >> 1;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004346
4347 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004348
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004349 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004350 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004351 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004352 /* KLMs take twice the size of MTTs */
4353 ndescs *= 2;
4354
4355 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004356 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
4357 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
4358 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
4359 seg->len = cpu_to_be64(mr->ibmr.length);
4360 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004361}
4362
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004363static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03004364{
4365 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004366 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004367}
4368
Bart Van Asschef696bf62018-07-18 09:25:14 -07004369static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg,
4370 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004371{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004372 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004373
Eli Cohene126ba92013-07-07 17:25:49 +03004374 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02004375 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02004376 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004377
Haggai Eran968e78d2014-12-11 17:04:11 +02004378 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004379 if (umrwr->pd)
4380 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
4381 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
4382 !umrwr->length)
4383 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
4384
4385 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004386 seg->len = cpu_to_be64(umrwr->length);
4387 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03004388 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02004389 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03004390}
4391
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004392static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
4393 struct mlx5_ib_mr *mr,
4394 struct mlx5_ib_pd *pd)
4395{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004396 int bcount = mr->desc_size * (mr->ndescs + mr->meta_ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004397
4398 dseg->addr = cpu_to_be64(mr->desc_map);
4399 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
4400 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
4401}
4402
Bart Van Asschef696bf62018-07-18 09:25:14 -07004403static __be32 send_ieth(const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004404{
4405 switch (wr->opcode) {
4406 case IB_WR_SEND_WITH_IMM:
4407 case IB_WR_RDMA_WRITE_WITH_IMM:
4408 return wr->ex.imm_data;
4409
4410 case IB_WR_SEND_WITH_INV:
4411 return cpu_to_be32(wr->ex.invalidate_rkey);
4412
4413 default:
4414 return 0;
4415 }
4416}
4417
4418static u8 calc_sig(void *wqe, int size)
4419{
4420 u8 *p = wqe;
4421 u8 res = 0;
4422 int i;
4423
4424 for (i = 0; i < size; i++)
4425 res ^= p[i];
4426
4427 return ~res;
4428}
4429
4430static u8 wq_sig(void *wqe)
4431{
4432 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
4433}
4434
Bart Van Asschef696bf62018-07-18 09:25:14 -07004435static int set_data_inl_seg(struct mlx5_ib_qp *qp, const struct ib_send_wr *wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004436 void **wqe, int *wqe_sz, void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004437{
4438 struct mlx5_wqe_inline_seg *seg;
Guy Levi34f4c952018-11-26 08:15:50 +02004439 size_t offset;
Eli Cohene126ba92013-07-07 17:25:49 +03004440 int inl = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004441 int i;
4442
Guy Levi34f4c952018-11-26 08:15:50 +02004443 seg = *wqe;
4444 *wqe += sizeof(*seg);
4445 offset = sizeof(*seg);
4446
Eli Cohene126ba92013-07-07 17:25:49 +03004447 for (i = 0; i < wr->num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02004448 size_t len = wr->sg_list[i].length;
4449 void *addr = (void *)(unsigned long)(wr->sg_list[i].addr);
4450
Eli Cohene126ba92013-07-07 17:25:49 +03004451 inl += len;
4452
4453 if (unlikely(inl > qp->max_inline_data))
4454 return -ENOMEM;
4455
Guy Levi34f4c952018-11-26 08:15:50 +02004456 while (likely(len)) {
4457 size_t leftlen;
4458 size_t copysz;
4459
4460 handle_post_send_edge(&qp->sq, wqe,
4461 *wqe_sz + (offset >> 4),
4462 cur_edge);
4463
4464 leftlen = *cur_edge - *wqe;
4465 copysz = min_t(size_t, leftlen, len);
4466
4467 memcpy(*wqe, addr, copysz);
4468 len -= copysz;
4469 addr += copysz;
4470 *wqe += copysz;
4471 offset += copysz;
Eli Cohene126ba92013-07-07 17:25:49 +03004472 }
Eli Cohene126ba92013-07-07 17:25:49 +03004473 }
4474
4475 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
4476
Guy Levi34f4c952018-11-26 08:15:50 +02004477 *wqe_sz += ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004478
4479 return 0;
4480}
4481
Sagi Grimberge6631812014-02-23 14:19:11 +02004482static u16 prot_field_size(enum ib_signature_type type)
4483{
4484 switch (type) {
4485 case IB_SIG_TYPE_T10_DIF:
4486 return MLX5_DIF_SIZE;
4487 default:
4488 return 0;
4489 }
4490}
4491
4492static u8 bs_selector(int block_size)
4493{
4494 switch (block_size) {
4495 case 512: return 0x1;
4496 case 520: return 0x2;
4497 case 4096: return 0x3;
4498 case 4160: return 0x4;
4499 case 1073741824: return 0x5;
4500 default: return 0;
4501 }
4502}
4503
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004504static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
4505 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02004506{
Sagi Grimberg142537f2014-08-13 19:54:32 +03004507 /* Valid inline section and allow BSF refresh */
4508 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
4509 MLX5_BSF_REFRESH_DIF);
4510 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
4511 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004512 /* repeating block */
4513 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
4514 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
4515 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004516
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004517 if (domain->sig.dif.ref_remap)
4518 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02004519
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004520 if (domain->sig.dif.app_escape) {
4521 if (domain->sig.dif.ref_escape)
4522 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
4523 else
4524 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02004525 }
4526
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004527 inl->dif_app_bitmask_check =
4528 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02004529}
4530
4531static int mlx5_set_bsf(struct ib_mr *sig_mr,
4532 struct ib_sig_attrs *sig_attrs,
4533 struct mlx5_bsf *bsf, u32 data_size)
4534{
4535 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
4536 struct mlx5_bsf_basic *basic = &bsf->basic;
4537 struct ib_sig_domain *mem = &sig_attrs->mem;
4538 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02004539
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004540 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02004541
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004542 /* Basic + Extended + Inline */
4543 basic->bsf_size_sbs = 1 << 7;
4544 /* Input domain check byte mask */
4545 basic->check_byte_mask = sig_attrs->check_mask;
4546 basic->raw_data_size = cpu_to_be32(data_size);
4547
4548 /* Memory domain */
4549 switch (sig_attrs->mem.sig_type) {
4550 case IB_SIG_TYPE_NONE:
4551 break;
4552 case IB_SIG_TYPE_T10_DIF:
4553 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
4554 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
4555 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
4556 break;
4557 default:
4558 return -EINVAL;
4559 }
4560
4561 /* Wire domain */
4562 switch (sig_attrs->wire.sig_type) {
4563 case IB_SIG_TYPE_NONE:
4564 break;
4565 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02004566 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004567 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004568 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03004569 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02004570 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004571 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004572 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004573 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004574 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004575 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02004576 } else
4577 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
4578
Sagi Grimberg142537f2014-08-13 19:54:32 +03004579 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004580 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02004581 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004582 default:
4583 return -EINVAL;
4584 }
4585
4586 return 0;
4587}
4588
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004589static int set_sig_data_segment(const struct ib_send_wr *send_wr,
4590 struct ib_mr *sig_mr,
4591 struct ib_sig_attrs *sig_attrs,
4592 struct mlx5_ib_qp *qp, void **seg, int *size,
4593 void **cur_edge)
Sagi Grimberge6631812014-02-23 14:19:11 +02004594{
Sagi Grimberge6631812014-02-23 14:19:11 +02004595 struct mlx5_bsf *bsf;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004596 u32 data_len;
4597 u32 data_key;
4598 u64 data_va;
4599 u32 prot_len = 0;
4600 u32 prot_key = 0;
4601 u64 prot_va = 0;
4602 bool prot = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004603 int ret;
4604 int wqe_size;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004605 struct mlx5_ib_mr *mr = to_mmr(sig_mr);
4606 struct mlx5_ib_mr *pi_mr = mr->pi_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004607
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004608 data_len = pi_mr->data_length;
4609 data_key = pi_mr->ibmr.lkey;
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03004610 data_va = pi_mr->data_iova;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004611 if (pi_mr->meta_ndescs) {
4612 prot_len = pi_mr->meta_length;
4613 prot_key = pi_mr->ibmr.lkey;
Israel Rukshinde0ae952019-06-11 18:52:55 +03004614 prot_va = pi_mr->pi_iova;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004615 prot = true;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004616 }
4617
4618 if (!prot || (data_key == prot_key && data_va == prot_va &&
4619 data_len == prot_len)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004620 /**
4621 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004622 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02004623 * So need construct:
4624 * ------------------
4625 * | data_klm |
4626 * ------------------
4627 * | BSF |
4628 * ------------------
4629 **/
4630 struct mlx5_klm *data_klm = *seg;
4631
4632 data_klm->bcount = cpu_to_be32(data_len);
4633 data_klm->key = cpu_to_be32(data_key);
4634 data_klm->va = cpu_to_be64(data_va);
4635 wqe_size = ALIGN(sizeof(*data_klm), 64);
4636 } else {
4637 /**
4638 * Source domain contains signature information
4639 * So need construct a strided block format:
4640 * ---------------------------
4641 * | stride_block_ctrl |
4642 * ---------------------------
4643 * | data_klm |
4644 * ---------------------------
4645 * | prot_klm |
4646 * ---------------------------
4647 * | BSF |
4648 * ---------------------------
4649 **/
4650 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
4651 struct mlx5_stride_block_entry *data_sentry;
4652 struct mlx5_stride_block_entry *prot_sentry;
Sagi Grimberge6631812014-02-23 14:19:11 +02004653 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
4654 int prot_size;
4655
4656 sblock_ctrl = *seg;
4657 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
4658 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
4659
4660 prot_size = prot_field_size(sig_attrs->mem.sig_type);
4661 if (!prot_size) {
4662 pr_err("Bad block size given: %u\n", block_size);
4663 return -EINVAL;
4664 }
4665 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
4666 prot_size);
4667 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
4668 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
4669 sblock_ctrl->num_entries = cpu_to_be16(2);
4670
4671 data_sentry->bcount = cpu_to_be16(block_size);
4672 data_sentry->key = cpu_to_be32(data_key);
4673 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004674 data_sentry->stride = cpu_to_be16(block_size);
4675
Sagi Grimberge6631812014-02-23 14:19:11 +02004676 prot_sentry->bcount = cpu_to_be16(prot_size);
4677 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004678 prot_sentry->va = cpu_to_be64(prot_va);
4679 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004680
Sagi Grimberge6631812014-02-23 14:19:11 +02004681 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
4682 sizeof(*prot_sentry), 64);
4683 }
4684
4685 *seg += wqe_size;
4686 *size += wqe_size / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004687 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004688
4689 bsf = *seg;
4690 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
4691 if (ret)
4692 return -EINVAL;
4693
4694 *seg += sizeof(*bsf);
4695 *size += sizeof(*bsf) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004696 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004697
4698 return 0;
4699}
4700
4701static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004702 struct ib_mr *sig_mr, int access_flags,
4703 u32 size, u32 length, u32 pdn)
Sagi Grimberge6631812014-02-23 14:19:11 +02004704{
Sagi Grimberge6631812014-02-23 14:19:11 +02004705 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004706 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004707
4708 memset(seg, 0, sizeof(*seg));
4709
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004710 seg->flags = get_umr_flags(access_flags) | MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004711 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004712 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004713 MLX5_MKEY_BSF_EN | pdn);
4714 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004715 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004716 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4717}
4718
4719static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004720 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004721{
4722 memset(umr, 0, sizeof(*umr));
4723
4724 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004725 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004726 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4727 umr->mkey_mask = sig_mkey_mask();
4728}
4729
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004730static int set_pi_umr_wr(const struct ib_send_wr *send_wr,
4731 struct mlx5_ib_qp *qp, void **seg, int *size,
4732 void **cur_edge)
4733{
4734 const struct ib_reg_wr *wr = reg_wr(send_wr);
4735 struct mlx5_ib_mr *sig_mr = to_mmr(wr->mr);
4736 struct mlx5_ib_mr *pi_mr = sig_mr->pi_mr;
4737 struct ib_sig_attrs *sig_attrs = sig_mr->ibmr.sig_attrs;
4738 u32 pdn = get_pd(qp)->pdn;
4739 u32 xlt_size;
4740 int region_len, ret;
4741
4742 if (unlikely(send_wr->num_sge != 0) ||
4743 unlikely(wr->access & IB_ACCESS_REMOTE_ATOMIC) ||
Max Gurtovoy185eddc2019-06-11 18:52:51 +03004744 unlikely(!sig_mr->sig) || unlikely(!qp->ibqp.integrity_en) ||
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004745 unlikely(!sig_mr->sig->sig_status_checked))
4746 return -EINVAL;
4747
4748 /* length of the protected region, data + protection */
4749 region_len = pi_mr->ibmr.length;
4750
4751 /**
4752 * KLM octoword size - if protection was provided
4753 * then we use strided block format (3 octowords),
4754 * else we use single KLM (1 octoword)
4755 **/
4756 if (sig_attrs->mem.sig_type != IB_SIG_TYPE_NONE)
4757 xlt_size = 0x30;
4758 else
4759 xlt_size = sizeof(struct mlx5_klm);
4760
4761 set_sig_umr_segment(*seg, xlt_size);
4762 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4763 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4764 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4765
4766 set_sig_mkey_segment(*seg, wr->mr, wr->access, xlt_size, region_len,
4767 pdn);
4768 *seg += sizeof(struct mlx5_mkey_seg);
4769 *size += sizeof(struct mlx5_mkey_seg) / 16;
4770 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4771
4772 ret = set_sig_data_segment(send_wr, wr->mr, sig_attrs, qp, seg, size,
4773 cur_edge);
4774 if (ret)
4775 return ret;
4776
4777 sig_mr->sig->sig_status_checked = false;
4778 return 0;
4779}
Sagi Grimberge6631812014-02-23 14:19:11 +02004780
Sagi Grimberge6631812014-02-23 14:19:11 +02004781static int set_psv_wr(struct ib_sig_domain *domain,
4782 u32 psv_idx, void **seg, int *size)
4783{
4784 struct mlx5_seg_set_psv *psv_seg = *seg;
4785
4786 memset(psv_seg, 0, sizeof(*psv_seg));
4787 psv_seg->psv_num = cpu_to_be32(psv_idx);
4788 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004789 case IB_SIG_TYPE_NONE:
4790 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004791 case IB_SIG_TYPE_T10_DIF:
4792 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4793 domain->sig.dif.app_tag);
4794 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004795 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004796 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004797 pr_err("Bad signature type (%d) is given.\n",
4798 domain->sig_type);
4799 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004800 }
4801
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004802 *seg += sizeof(*psv_seg);
4803 *size += sizeof(*psv_seg) / 16;
4804
Sagi Grimberge6631812014-02-23 14:19:11 +02004805 return 0;
4806}
4807
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004808static int set_reg_wr(struct mlx5_ib_qp *qp,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004809 const struct ib_reg_wr *wr,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004810 void **seg, int *size, void **cur_edge,
4811 bool check_not_free)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004812{
4813 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4814 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
Moni Shoua841b07f2019-08-15 11:38:34 +03004815 struct mlx5_ib_dev *dev = to_mdev(pd->ibpd.device);
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004816 int mr_list_size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Idan Burstein064e5262018-05-02 13:16:39 +03004817 bool umr_inline = mr_list_size <= MLX5_IB_SQ_UMR_INLINE_THRESHOLD;
Moni Shoua841b07f2019-08-15 11:38:34 +03004818 bool atomic = wr->access & IB_ACCESS_REMOTE_ATOMIC;
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004819 u8 flags = 0;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004820
Moni Shoua841b07f2019-08-15 11:38:34 +03004821 if (!mlx5_ib_can_use_umr(dev, atomic)) {
4822 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4823 "Fast update of %s for MR is disabled\n",
4824 (MLX5_CAP_GEN(dev->mdev,
4825 umr_modify_entity_size_disabled)) ?
4826 "entity size" :
4827 "atomic access");
4828 return -EINVAL;
4829 }
4830
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004831 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4832 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4833 "Invalid IB_SEND_INLINE send flag\n");
4834 return -EINVAL;
4835 }
4836
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004837 if (check_not_free)
4838 flags |= MLX5_UMR_CHECK_NOT_FREE;
4839 if (umr_inline)
4840 flags |= MLX5_UMR_INLINE;
4841
Moni Shoua841b07f2019-08-15 11:38:34 +03004842 set_reg_umr_seg(*seg, mr, flags, atomic);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004843 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4844 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004845 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004846
4847 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4848 *seg += sizeof(struct mlx5_mkey_seg);
4849 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004850 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004851
Idan Burstein064e5262018-05-02 13:16:39 +03004852 if (umr_inline) {
Guy Levi34f4c952018-11-26 08:15:50 +02004853 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, mr->descs,
4854 mr_list_size);
4855 *size = ALIGN(*size, MLX5_SEND_WQE_BB >> 4);
Idan Burstein064e5262018-05-02 13:16:39 +03004856 } else {
4857 set_reg_data_seg(*seg, mr, pd);
4858 *seg += sizeof(struct mlx5_wqe_data_seg);
4859 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4860 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004861 return 0;
4862}
4863
Guy Levi34f4c952018-11-26 08:15:50 +02004864static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size,
4865 void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004866{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004867 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004868 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4869 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004870 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004871 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004872 *seg += sizeof(struct mlx5_mkey_seg);
4873 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004874 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03004875}
4876
Guy Levi34f4c952018-11-26 08:15:50 +02004877static void dump_wqe(struct mlx5_ib_qp *qp, u32 idx, int size_16)
Eli Cohene126ba92013-07-07 17:25:49 +03004878{
4879 __be32 *p = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004880 int i, j;
4881
Guy Levi34f4c952018-11-26 08:15:50 +02004882 pr_debug("dump WQE index %u:\n", idx);
Eli Cohene126ba92013-07-07 17:25:49 +03004883 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4884 if ((i & 0xf) == 0) {
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004885 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, idx);
Guy Levi34f4c952018-11-26 08:15:50 +02004886 pr_debug("WQBB at %p:\n", (void *)p);
Eli Cohene126ba92013-07-07 17:25:49 +03004887 j = 0;
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004888 idx = (idx + 1) & (qp->sq.wqe_cnt - 1);
Eli Cohene126ba92013-07-07 17:25:49 +03004889 }
4890 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4891 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4892 be32_to_cpu(p[j + 3]));
4893 }
4894}
4895
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004896static int __begin_wqe(struct mlx5_ib_qp *qp, void **seg,
Guy Levi34f4c952018-11-26 08:15:50 +02004897 struct mlx5_wqe_ctrl_seg **ctrl,
4898 const struct ib_send_wr *wr, unsigned int *idx,
4899 int *size, void **cur_edge, int nreq,
4900 bool send_signaled, bool solicited)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004901{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004902 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4903 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004904
4905 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
Guy Levi34f4c952018-11-26 08:15:50 +02004906 *seg = mlx5_frag_buf_get_wqe(&qp->sq.fbc, *idx);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004907 *ctrl = *seg;
4908 *(uint32_t *)(*seg + 8) = 0;
4909 (*ctrl)->imm = send_ieth(wr);
4910 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004911 (send_signaled ? MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4912 (solicited ? MLX5_WQE_CTRL_SOLICITED : 0);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004913
4914 *seg += sizeof(**ctrl);
4915 *size = sizeof(**ctrl) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004916 *cur_edge = qp->sq.cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004917
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004918 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004919}
4920
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004921static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4922 struct mlx5_wqe_ctrl_seg **ctrl,
4923 const struct ib_send_wr *wr, unsigned *idx,
Guy Levi34f4c952018-11-26 08:15:50 +02004924 int *size, void **cur_edge, int nreq)
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004925{
Guy Levi34f4c952018-11-26 08:15:50 +02004926 return __begin_wqe(qp, seg, ctrl, wr, idx, size, cur_edge, nreq,
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004927 wr->send_flags & IB_SEND_SIGNALED,
4928 wr->send_flags & IB_SEND_SOLICITED);
4929}
4930
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004931static void finish_wqe(struct mlx5_ib_qp *qp,
4932 struct mlx5_wqe_ctrl_seg *ctrl,
Guy Levi34f4c952018-11-26 08:15:50 +02004933 void *seg, u8 size, void *cur_edge,
4934 unsigned int idx, u64 wr_id, int nreq, u8 fence,
4935 u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004936{
4937 u8 opmod = 0;
4938
4939 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
4940 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02004941 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004942 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004943 if (unlikely(qp->wq_sig))
4944 ctrl->signature = wq_sig(ctrl);
4945
4946 qp->sq.wrid[idx] = wr_id;
4947 qp->sq.w_list[idx].opcode = mlx5_opcode;
4948 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
4949 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
4950 qp->sq.w_list[idx].next = qp->sq.cur_post;
Guy Levi34f4c952018-11-26 08:15:50 +02004951
4952 /* We save the edge which was possibly updated during the WQE
4953 * construction, into SQ's cache.
4954 */
4955 seg = PTR_ALIGN(seg, MLX5_SEND_WQE_BB);
4956 qp->sq.cur_edge = (unlikely(seg == cur_edge)) ?
4957 get_sq_edge(&qp->sq, qp->sq.cur_post &
4958 (qp->sq.wqe_cnt - 1)) :
4959 cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004960}
4961
Bart Van Assched34ac5c2018-07-18 09:25:32 -07004962static int _mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
4963 const struct ib_send_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03004964{
4965 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
4966 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004967 struct mlx5_core_dev *mdev = dev->mdev;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004968 struct ib_reg_wr reg_pi_wr;
Haggai Erand16e91d2016-02-29 15:45:05 +02004969 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02004970 struct mlx5_ib_mr *mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004971 struct mlx5_ib_mr *pi_mr;
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03004972 struct mlx5_ib_mr pa_pi_mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004973 struct ib_sig_attrs *sig_attrs;
Eli Cohene126ba92013-07-07 17:25:49 +03004974 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02004975 struct mlx5_bf *bf;
Guy Levi34f4c952018-11-26 08:15:50 +02004976 void *cur_edge;
Eli Cohene126ba92013-07-07 17:25:49 +03004977 int uninitialized_var(size);
Eli Cohene126ba92013-07-07 17:25:49 +03004978 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03004979 unsigned idx;
4980 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004981 int num_sge;
4982 void *seg;
4983 int nreq;
4984 int i;
4985 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004986 u8 fence;
4987
Parav Pandit6c755202018-08-28 14:45:29 +03004988 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
4989 !drain)) {
4990 *bad_wr = wr;
4991 return -EIO;
4992 }
4993
Haggai Erand16e91d2016-02-29 15:45:05 +02004994 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4995 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
4996
4997 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004998 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02004999
Eli Cohene126ba92013-07-07 17:25:49 +03005000 spin_lock_irqsave(&qp->sq.lock, flags);
5001
5002 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04005003 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03005004 mlx5_ib_warn(dev, "\n");
5005 err = -EINVAL;
5006 *bad_wr = wr;
5007 goto out;
5008 }
5009
Eli Cohene126ba92013-07-07 17:25:49 +03005010 num_sge = wr->num_sge;
5011 if (unlikely(num_sge > qp->sq.max_gs)) {
5012 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03005013 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03005014 *bad_wr = wr;
5015 goto out;
5016 }
5017
Guy Levi34f4c952018-11-26 08:15:50 +02005018 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, &cur_edge,
5019 nreq);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005020 if (err) {
5021 mlx5_ib_warn(dev, "\n");
5022 err = -ENOMEM;
5023 *bad_wr = wr;
5024 goto out;
5025 }
Eli Cohene126ba92013-07-07 17:25:49 +03005026
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005027 if (wr->opcode == IB_WR_REG_MR ||
5028 wr->opcode == IB_WR_REG_MR_INTEGRITY) {
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005029 fence = dev->umr_fence;
5030 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Majd Dibbiny074fca32018-11-05 08:07:37 +02005031 } else {
5032 if (wr->send_flags & IB_SEND_FENCE) {
5033 if (qp->next_fence)
5034 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
5035 else
5036 fence = MLX5_FENCE_MODE_FENCE;
5037 } else {
5038 fence = qp->next_fence;
5039 }
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005040 }
5041
Eli Cohene126ba92013-07-07 17:25:49 +03005042 switch (ibqp->qp_type) {
5043 case IB_QPT_XRC_INI:
5044 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03005045 seg += sizeof(*xrc);
5046 size += sizeof(*xrc) / 16;
5047 /* fall through */
5048 case IB_QPT_RC:
5049 switch (wr->opcode) {
5050 case IB_WR_RDMA_READ:
5051 case IB_WR_RDMA_WRITE:
5052 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005053 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5054 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005055 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005056 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5057 break;
5058
5059 case IB_WR_ATOMIC_CMP_AND_SWP:
5060 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03005061 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03005062 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
5063 err = -ENOSYS;
5064 *bad_wr = wr;
5065 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005066
5067 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03005068 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
5069 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Guy Levi34f4c952018-11-26 08:15:50 +02005070 set_linv_wr(qp, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005071 num_sge = 0;
5072 break;
5073
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005074 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005075 qp->sq.wr_data[idx] = IB_WR_REG_MR;
5076 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
Guy Levi34f4c952018-11-26 08:15:50 +02005077 err = set_reg_wr(qp, reg_wr(wr), &seg, &size,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03005078 &cur_edge, true);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005079 if (err) {
5080 *bad_wr = wr;
5081 goto out;
5082 }
5083 num_sge = 0;
5084 break;
5085
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005086 case IB_WR_REG_MR_INTEGRITY:
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005087 qp->sq.wr_data[idx] = IB_WR_REG_MR_INTEGRITY;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005088
5089 mr = to_mmr(reg_wr(wr)->mr);
5090 pi_mr = mr->pi_mr;
5091
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005092 if (pi_mr) {
5093 memset(&reg_pi_wr, 0,
5094 sizeof(struct ib_reg_wr));
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005095
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005096 reg_pi_wr.mr = &pi_mr->ibmr;
5097 reg_pi_wr.access = reg_wr(wr)->access;
5098 reg_pi_wr.key = pi_mr->ibmr.rkey;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005099
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005100 ctrl->imm = cpu_to_be32(reg_pi_wr.key);
5101 /* UMR for data + prot registration */
5102 err = set_reg_wr(qp, &reg_pi_wr, &seg,
5103 &size, &cur_edge,
5104 false);
5105 if (err) {
5106 *bad_wr = wr;
5107 goto out;
5108 }
5109 finish_wqe(qp, ctrl, seg, size,
5110 cur_edge, idx, wr->wr_id,
5111 nreq, fence,
5112 MLX5_OPCODE_UMR);
5113
5114 err = begin_wqe(qp, &seg, &ctrl, wr,
5115 &idx, &size, &cur_edge,
5116 nreq);
5117 if (err) {
5118 mlx5_ib_warn(dev, "\n");
5119 err = -ENOMEM;
5120 *bad_wr = wr;
5121 goto out;
5122 }
5123 } else {
5124 memset(&pa_pi_mr, 0,
5125 sizeof(struct mlx5_ib_mr));
5126 /* No UMR, use local_dma_lkey */
5127 pa_pi_mr.ibmr.lkey =
5128 mr->ibmr.pd->local_dma_lkey;
5129
5130 pa_pi_mr.ndescs = mr->ndescs;
5131 pa_pi_mr.data_length = mr->data_length;
5132 pa_pi_mr.data_iova = mr->data_iova;
5133 if (mr->meta_ndescs) {
5134 pa_pi_mr.meta_ndescs =
5135 mr->meta_ndescs;
5136 pa_pi_mr.meta_length =
5137 mr->meta_length;
5138 pa_pi_mr.pi_iova = mr->pi_iova;
5139 }
5140
5141 pa_pi_mr.ibmr.length = mr->ibmr.length;
5142 mr->pi_mr = &pa_pi_mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005143 }
5144 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
5145 /* UMR for sig MR */
5146 err = set_pi_umr_wr(wr, qp, &seg, &size,
5147 &cur_edge);
5148 if (err) {
5149 mlx5_ib_warn(dev, "\n");
5150 *bad_wr = wr;
5151 goto out;
5152 }
5153 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5154 wr->wr_id, nreq, fence,
5155 MLX5_OPCODE_UMR);
5156
5157 /*
5158 * SET_PSV WQEs are not signaled and solicited
5159 * on error
5160 */
5161 sig_attrs = mr->ibmr.sig_attrs;
5162 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5163 &size, &cur_edge, nreq, false,
5164 true);
5165 if (err) {
5166 mlx5_ib_warn(dev, "\n");
5167 err = -ENOMEM;
5168 *bad_wr = wr;
5169 goto out;
5170 }
5171 err = set_psv_wr(&sig_attrs->mem,
5172 mr->sig->psv_memory.psv_idx,
5173 &seg, &size);
5174 if (err) {
5175 mlx5_ib_warn(dev, "\n");
5176 *bad_wr = wr;
5177 goto out;
5178 }
5179 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5180 wr->wr_id, nreq, next_fence,
5181 MLX5_OPCODE_SET_PSV);
5182
5183 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5184 &size, &cur_edge, nreq, false,
5185 true);
5186 if (err) {
5187 mlx5_ib_warn(dev, "\n");
5188 err = -ENOMEM;
5189 *bad_wr = wr;
5190 goto out;
5191 }
5192 err = set_psv_wr(&sig_attrs->wire,
5193 mr->sig->psv_wire.psv_idx,
5194 &seg, &size);
5195 if (err) {
5196 mlx5_ib_warn(dev, "\n");
5197 *bad_wr = wr;
5198 goto out;
5199 }
5200 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5201 wr->wr_id, nreq, next_fence,
5202 MLX5_OPCODE_SET_PSV);
5203
5204 qp->next_fence =
5205 MLX5_FENCE_MODE_INITIATOR_SMALL;
5206 num_sge = 0;
5207 goto skip_psv;
5208
Eli Cohene126ba92013-07-07 17:25:49 +03005209 default:
5210 break;
5211 }
5212 break;
5213
5214 case IB_QPT_UC:
5215 switch (wr->opcode) {
5216 case IB_WR_RDMA_WRITE:
5217 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005218 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5219 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03005220 seg += sizeof(struct mlx5_wqe_raddr_seg);
5221 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5222 break;
5223
5224 default:
5225 break;
5226 }
5227 break;
5228
Eli Cohene126ba92013-07-07 17:25:49 +03005229 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02005230 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
5231 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
5232 err = -EPERM;
5233 *bad_wr = wr;
5234 goto out;
5235 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07005236 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02005237 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03005238 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005239 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005240 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005241 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
5242
Eli Cohene126ba92013-07-07 17:25:49 +03005243 break;
Erez Shitritf0313962016-02-21 16:27:17 +02005244 case IB_QPT_UD:
5245 set_datagram_seg(seg, wr);
5246 seg += sizeof(struct mlx5_wqe_datagram_seg);
5247 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005248 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005249
5250 /* handle qp that supports ud offload */
5251 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
5252 struct mlx5_wqe_eth_pad *pad;
5253
5254 pad = seg;
5255 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
5256 seg += sizeof(struct mlx5_wqe_eth_pad);
5257 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005258 set_eth_seg(wr, qp, &seg, &size, &cur_edge);
5259 handle_post_send_edge(&qp->sq, &seg, size,
5260 &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005261 }
5262 break;
Eli Cohene126ba92013-07-07 17:25:49 +03005263 case MLX5_IB_QPT_REG_UMR:
5264 if (wr->opcode != MLX5_IB_WR_UMR) {
5265 err = -EINVAL;
5266 mlx5_ib_warn(dev, "bad opcode\n");
5267 goto out;
5268 }
5269 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005270 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02005271 err = set_reg_umr_segment(dev, seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
5272 if (unlikely(err))
5273 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005274 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
5275 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005276 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005277 set_reg_mkey_segment(seg, wr);
5278 seg += sizeof(struct mlx5_mkey_seg);
5279 size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005280 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005281 break;
5282
5283 default:
5284 break;
5285 }
5286
5287 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
Guy Levi34f4c952018-11-26 08:15:50 +02005288 err = set_data_inl_seg(qp, wr, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005289 if (unlikely(err)) {
5290 mlx5_ib_warn(dev, "\n");
5291 *bad_wr = wr;
5292 goto out;
5293 }
Eli Cohene126ba92013-07-07 17:25:49 +03005294 } else {
Eli Cohene126ba92013-07-07 17:25:49 +03005295 for (i = 0; i < num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02005296 handle_post_send_edge(&qp->sq, &seg, size,
5297 &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005298 if (likely(wr->sg_list[i].length)) {
Guy Levi34f4c952018-11-26 08:15:50 +02005299 set_data_ptr_seg
5300 ((struct mlx5_wqe_data_seg *)seg,
5301 wr->sg_list + i);
Eli Cohene126ba92013-07-07 17:25:49 +03005302 size += sizeof(struct mlx5_wqe_data_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005303 seg += sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005304 }
5305 }
5306 }
5307
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005308 qp->next_fence = next_fence;
Guy Levi34f4c952018-11-26 08:15:50 +02005309 finish_wqe(qp, ctrl, seg, size, cur_edge, idx, wr->wr_id, nreq,
5310 fence, mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02005311skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03005312 if (0)
5313 dump_wqe(qp, idx, size);
5314 }
5315
5316out:
5317 if (likely(nreq)) {
5318 qp->sq.head += nreq;
5319
5320 /* Make sure that descriptors are written before
5321 * updating doorbell record and ringing the doorbell
5322 */
5323 wmb();
5324
5325 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
5326
Eli Cohenada388f2014-01-14 17:45:16 +02005327 /* Make sure doorbell record is visible to the HCA before
5328 * we hit doorbell */
5329 wmb();
5330
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005331 /* currently we support only regular doorbells */
Maxim Mikityanskiybbf29f62019-03-29 15:37:52 -07005332 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005333 /* Make sure doorbells don't leak out of SQ spinlock
5334 * and reach the HCA out of order.
5335 */
Eli Cohene126ba92013-07-07 17:25:49 +03005336 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03005337 }
5338
5339 spin_unlock_irqrestore(&qp->sq.lock, flags);
5340
5341 return err;
5342}
5343
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005344int mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
5345 const struct ib_send_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005346{
5347 return _mlx5_ib_post_send(ibqp, wr, bad_wr, false);
5348}
5349
Eli Cohene126ba92013-07-07 17:25:49 +03005350static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
5351{
5352 sig->signature = calc_sig(sig, size);
5353}
5354
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005355static int _mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5356 const struct ib_recv_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03005357{
5358 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5359 struct mlx5_wqe_data_seg *scat;
5360 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03005361 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5362 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005363 unsigned long flags;
5364 int err = 0;
5365 int nreq;
5366 int ind;
5367 int i;
5368
Parav Pandit6c755202018-08-28 14:45:29 +03005369 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5370 !drain)) {
5371 *bad_wr = wr;
5372 return -EIO;
5373 }
5374
Haggai Erand16e91d2016-02-29 15:45:05 +02005375 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5376 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
5377
Eli Cohene126ba92013-07-07 17:25:49 +03005378 spin_lock_irqsave(&qp->rq.lock, flags);
5379
5380 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
5381
5382 for (nreq = 0; wr; nreq++, wr = wr->next) {
5383 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
5384 err = -ENOMEM;
5385 *bad_wr = wr;
5386 goto out;
5387 }
5388
5389 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
5390 err = -EINVAL;
5391 *bad_wr = wr;
5392 goto out;
5393 }
5394
Guy Levi34f4c952018-11-26 08:15:50 +02005395 scat = mlx5_frag_buf_get_wqe(&qp->rq.fbc, ind);
Eli Cohene126ba92013-07-07 17:25:49 +03005396 if (qp->wq_sig)
5397 scat++;
5398
5399 for (i = 0; i < wr->num_sge; i++)
5400 set_data_ptr_seg(scat + i, wr->sg_list + i);
5401
5402 if (i < qp->rq.max_gs) {
5403 scat[i].byte_count = 0;
5404 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
5405 scat[i].addr = 0;
5406 }
5407
5408 if (qp->wq_sig) {
5409 sig = (struct mlx5_rwqe_sig *)scat;
5410 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
5411 }
5412
5413 qp->rq.wrid[ind] = wr->wr_id;
5414
5415 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
5416 }
5417
5418out:
5419 if (likely(nreq)) {
5420 qp->rq.head += nreq;
5421
5422 /* Make sure that descriptors are written before
5423 * doorbell record.
5424 */
5425 wmb();
5426
5427 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
5428 }
5429
5430 spin_unlock_irqrestore(&qp->rq.lock, flags);
5431
5432 return err;
5433}
5434
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005435int mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5436 const struct ib_recv_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005437{
5438 return _mlx5_ib_post_recv(ibqp, wr, bad_wr, false);
5439}
5440
Eli Cohene126ba92013-07-07 17:25:49 +03005441static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
5442{
5443 switch (mlx5_state) {
5444 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
5445 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
5446 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
5447 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
5448 case MLX5_QP_STATE_SQ_DRAINING:
5449 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
5450 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
5451 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
5452 default: return -1;
5453 }
5454}
5455
5456static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
5457{
5458 switch (mlx5_mig_state) {
5459 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
5460 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
5461 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
5462 default: return -1;
5463 }
5464}
5465
5466static int to_ib_qp_access_flags(int mlx5_flags)
5467{
5468 int ib_flags = 0;
5469
5470 if (mlx5_flags & MLX5_QP_BIT_RRE)
5471 ib_flags |= IB_ACCESS_REMOTE_READ;
5472 if (mlx5_flags & MLX5_QP_BIT_RWE)
5473 ib_flags |= IB_ACCESS_REMOTE_WRITE;
5474 if (mlx5_flags & MLX5_QP_BIT_RAE)
5475 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
5476
5477 return ib_flags;
5478}
5479
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005480static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005481 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005482 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03005483{
Eli Cohene126ba92013-07-07 17:25:49 +03005484
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005485 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03005486
Jason Gunthorpee7996a92018-01-29 13:26:40 -07005487 if (!path->port || path->port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03005488 return;
5489
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02005490 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
5491
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005492 rdma_ah_set_port_num(ah_attr, path->port);
5493 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03005494
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005495 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
5496 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
5497 rdma_ah_set_static_rate(ah_attr,
5498 path->static_rate ? path->static_rate - 5 : 0);
5499 if (path->grh_mlid & (1 << 7)) {
5500 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
5501
5502 rdma_ah_set_grh(ah_attr, NULL,
5503 tc_fl & 0xfffff,
5504 path->mgid_index,
5505 path->hop_limit,
5506 (tc_fl >> 20) & 0xff);
5507 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03005508 }
5509}
5510
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005511static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
5512 struct mlx5_ib_sq *sq,
5513 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03005514{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005515 int err;
5516
Eran Ben Elisha28160772017-12-26 15:17:05 +02005517 err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005518 if (err)
5519 goto out;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005520 sq->state = *sq_state;
5521
5522out:
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005523 return err;
5524}
5525
5526static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
5527 struct mlx5_ib_rq *rq,
5528 u8 *rq_state)
5529{
5530 void *out;
5531 void *rqc;
5532 int inlen;
5533 int err;
5534
5535 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005536 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005537 if (!out)
5538 return -ENOMEM;
5539
5540 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
5541 if (err)
5542 goto out;
5543
5544 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
5545 *rq_state = MLX5_GET(rqc, rqc, state);
5546 rq->state = *rq_state;
5547
5548out:
5549 kvfree(out);
5550 return err;
5551}
5552
5553static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
5554 struct mlx5_ib_qp *qp, u8 *qp_state)
5555{
5556 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
5557 [MLX5_RQC_STATE_RST] = {
5558 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5559 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5560 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
5561 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
5562 },
5563 [MLX5_RQC_STATE_RDY] = {
5564 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5565 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5566 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
5567 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
5568 },
5569 [MLX5_RQC_STATE_ERR] = {
5570 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5571 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5572 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
5573 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
5574 },
5575 [MLX5_RQ_STATE_NA] = {
5576 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5577 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5578 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
5579 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
5580 },
5581 };
5582
5583 *qp_state = sqrq_trans[rq_state][sq_state];
5584
5585 if (*qp_state == MLX5_QP_STATE_BAD) {
5586 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
5587 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
5588 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
5589 return -EINVAL;
5590 }
5591
5592 if (*qp_state == MLX5_QP_STATE)
5593 *qp_state = qp->state;
5594
5595 return 0;
5596}
5597
5598static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
5599 struct mlx5_ib_qp *qp,
5600 u8 *raw_packet_qp_state)
5601{
5602 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
5603 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
5604 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
5605 int err;
5606 u8 sq_state = MLX5_SQ_STATE_NA;
5607 u8 rq_state = MLX5_RQ_STATE_NA;
5608
5609 if (qp->sq.wqe_cnt) {
5610 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
5611 if (err)
5612 return err;
5613 }
5614
5615 if (qp->rq.wqe_cnt) {
5616 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
5617 if (err)
5618 return err;
5619 }
5620
5621 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
5622 raw_packet_qp_state);
5623}
5624
5625static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
5626 struct ib_qp_attr *qp_attr)
5627{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005628 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03005629 struct mlx5_qp_context *context;
5630 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005631 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03005632 int err = 0;
5633
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005634 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005635 if (!outb)
5636 return -ENOMEM;
5637
majd@mellanox.com19098df2016-01-14 19:13:03 +02005638 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005639 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03005640 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005641 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005642
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005643 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
5644 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
5645
Eli Cohene126ba92013-07-07 17:25:49 +03005646 mlx5_state = be32_to_cpu(context->flags) >> 28;
5647
5648 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03005649 qp_attr->path_mtu = context->mtu_msgmax >> 5;
5650 qp_attr->path_mig_state =
5651 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
5652 qp_attr->qkey = be32_to_cpu(context->qkey);
5653 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
5654 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
5655 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
5656 qp_attr->qp_access_flags =
5657 to_ib_qp_access_flags(be32_to_cpu(context->params2));
5658
5659 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005660 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
5661 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005662 qp_attr->alt_pkey_index =
5663 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005664 qp_attr->alt_port_num =
5665 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03005666 }
5667
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005668 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03005669 qp_attr->port_num = context->pri_path.port;
5670
5671 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
5672 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
5673
5674 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
5675
5676 qp_attr->max_dest_rd_atomic =
5677 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
5678 qp_attr->min_rnr_timer =
5679 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
5680 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
5681 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
5682 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
5683 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005684
5685out:
5686 kfree(outb);
5687 return err;
5688}
5689
Moni Shoua776a3902018-01-02 16:19:33 +02005690static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
5691 struct ib_qp_attr *qp_attr, int qp_attr_mask,
5692 struct ib_qp_init_attr *qp_init_attr)
5693{
5694 struct mlx5_core_dct *dct = &mqp->dct.mdct;
5695 u32 *out;
5696 u32 access_flags = 0;
5697 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
5698 void *dctc;
5699 int err;
5700 int supported_mask = IB_QP_STATE |
5701 IB_QP_ACCESS_FLAGS |
5702 IB_QP_PORT |
5703 IB_QP_MIN_RNR_TIMER |
5704 IB_QP_AV |
5705 IB_QP_PATH_MTU |
5706 IB_QP_PKEY_INDEX;
5707
5708 if (qp_attr_mask & ~supported_mask)
5709 return -EINVAL;
5710 if (mqp->state != IB_QPS_RTR)
5711 return -EINVAL;
5712
5713 out = kzalloc(outlen, GFP_KERNEL);
5714 if (!out)
5715 return -ENOMEM;
5716
5717 err = mlx5_core_dct_query(dev->mdev, dct, out, outlen);
5718 if (err)
5719 goto out;
5720
5721 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
5722
5723 if (qp_attr_mask & IB_QP_STATE)
5724 qp_attr->qp_state = IB_QPS_RTR;
5725
5726 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
5727 if (MLX5_GET(dctc, dctc, rre))
5728 access_flags |= IB_ACCESS_REMOTE_READ;
5729 if (MLX5_GET(dctc, dctc, rwe))
5730 access_flags |= IB_ACCESS_REMOTE_WRITE;
5731 if (MLX5_GET(dctc, dctc, rae))
5732 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
5733 qp_attr->qp_access_flags = access_flags;
5734 }
5735
5736 if (qp_attr_mask & IB_QP_PORT)
5737 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
5738 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
5739 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
5740 if (qp_attr_mask & IB_QP_AV) {
5741 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
5742 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
5743 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
5744 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
5745 }
5746 if (qp_attr_mask & IB_QP_PATH_MTU)
5747 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
5748 if (qp_attr_mask & IB_QP_PKEY_INDEX)
5749 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
5750out:
5751 kfree(out);
5752 return err;
5753}
5754
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005755int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5756 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
5757{
5758 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5759 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5760 int err = 0;
5761 u8 raw_packet_qp_state;
5762
Yishai Hadas28d61372016-05-23 15:20:56 +03005763 if (ibqp->rwq_ind_tbl)
5764 return -ENOSYS;
5765
Haggai Erand16e91d2016-02-29 15:45:05 +02005766 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5767 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
5768 qp_init_attr);
5769
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005770 /* Not all of output fields are applicable, make sure to zero them */
5771 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5772 memset(qp_attr, 0, sizeof(*qp_attr));
5773
Moni Shoua776a3902018-01-02 16:19:33 +02005774 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
5775 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
5776 qp_attr_mask, qp_init_attr);
5777
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005778 mutex_lock(&qp->mutex);
5779
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005780 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
5781 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005782 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
5783 if (err)
5784 goto out;
5785 qp->state = raw_packet_qp_state;
5786 qp_attr->port_num = 1;
5787 } else {
5788 err = query_qp_attr(dev, qp, qp_attr);
5789 if (err)
5790 goto out;
5791 }
5792
5793 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03005794 qp_attr->cur_qp_state = qp_attr->qp_state;
5795 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
5796 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
5797
5798 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005799 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005800 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005801 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005802 } else {
5803 qp_attr->cap.max_send_wr = 0;
5804 qp_attr->cap.max_send_sge = 0;
5805 }
5806
Noa Osherovich0540d812016-06-04 15:15:32 +03005807 qp_init_attr->qp_type = ibqp->qp_type;
5808 qp_init_attr->recv_cq = ibqp->recv_cq;
5809 qp_init_attr->send_cq = ibqp->send_cq;
5810 qp_init_attr->srq = ibqp->srq;
5811 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005812
5813 qp_init_attr->cap = qp_attr->cap;
5814
5815 qp_init_attr->create_flags = 0;
5816 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
5817 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
5818
Leon Romanovsky051f2632015-12-20 12:16:11 +02005819 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
5820 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
5821 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
5822 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
5823 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
5824 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02005825 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
Michael Guralnik3f89b012019-10-20 09:43:59 +03005826 qp_init_attr->create_flags |= MLX5_IB_QP_CREATE_SQPN_QP1;
Leon Romanovsky051f2632015-12-20 12:16:11 +02005827
Eli Cohene126ba92013-07-07 17:25:49 +03005828 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5829 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5830
Eli Cohene126ba92013-07-07 17:25:49 +03005831out:
5832 mutex_unlock(&qp->mutex);
5833 return err;
5834}
5835
5836struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03005837 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005838{
5839 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5840 struct mlx5_ib_xrcd *xrcd;
5841 int err;
5842
Saeed Mahameed938fe832015-05-28 22:28:41 +03005843 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005844 return ERR_PTR(-ENOSYS);
5845
5846 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5847 if (!xrcd)
5848 return ERR_PTR(-ENOMEM);
5849
Yishai Hadas5aa37712018-11-26 08:28:38 +02005850 err = mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03005851 if (err) {
5852 kfree(xrcd);
5853 return ERR_PTR(-ENOMEM);
5854 }
5855
5856 return &xrcd->ibxrcd;
5857}
5858
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005859int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005860{
5861 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5862 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
5863 int err;
5864
Yishai Hadas5aa37712018-11-26 08:28:38 +02005865 err = mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, 0);
Leon Romanovskyb0818082018-01-28 11:25:30 +02005866 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03005867 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005868
5869 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03005870 return 0;
5871}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005872
Yishai Hadas350d0e42016-08-28 14:58:18 +03005873static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5874{
5875 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5876 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5877 struct ib_event event;
5878
5879 if (rwq->ibwq.event_handler) {
5880 event.device = rwq->ibwq.device;
5881 event.element.wq = &rwq->ibwq;
5882 switch (type) {
5883 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5884 event.event = IB_EVENT_WQ_FATAL;
5885 break;
5886 default:
5887 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5888 return;
5889 }
5890
5891 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5892 }
5893}
5894
Maor Gottlieb03404e82017-05-30 10:29:13 +03005895static int set_delay_drop(struct mlx5_ib_dev *dev)
5896{
5897 int err = 0;
5898
5899 mutex_lock(&dev->delay_drop.lock);
5900 if (dev->delay_drop.activate)
5901 goto out;
5902
5903 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
5904 if (err)
5905 goto out;
5906
5907 dev->delay_drop.activate = true;
5908out:
5909 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005910
5911 if (!err)
5912 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005913 return err;
5914}
5915
Yishai Hadas79b20a62016-05-23 15:20:50 +03005916static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5917 struct ib_wq_init_attr *init_attr)
5918{
5919 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005920 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005921 __be64 *rq_pas0;
5922 void *in;
5923 void *rqc;
5924 void *wq;
5925 int inlen;
5926 int err;
5927
5928 dev = to_mdev(pd->device);
5929
5930 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005931 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005932 if (!in)
5933 return -ENOMEM;
5934
Yishai Hadas34d57582018-09-20 21:39:21 +03005935 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005936 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
5937 MLX5_SET(rqc, rqc, mem_rq_type,
5938 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
5939 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
5940 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
5941 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
5942 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
5943 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03005944 MLX5_SET(wq, wq, wq_type,
5945 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
5946 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005947 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5948 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
5949 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
5950 err = -EOPNOTSUPP;
5951 goto out;
5952 } else {
5953 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
5954 }
5955 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005956 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03005957 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
5958 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
5959 MLX5_SET(wq, wq, log_wqe_stride_size,
5960 rwq->single_stride_log_num_of_bytes -
5961 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
5962 MLX5_SET(wq, wq, log_wqe_num_of_strides, rwq->log_num_strides -
5963 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES);
5964 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005965 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
5966 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
5967 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
5968 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
5969 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
5970 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02005971 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005972 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02005973 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005974 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
5975 err = -EOPNOTSUPP;
5976 goto out;
5977 }
5978 } else {
5979 MLX5_SET(rqc, rqc, vsd, 1);
5980 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02005981 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
5982 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
5983 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
5984 err = -EOPNOTSUPP;
5985 goto out;
5986 }
5987 MLX5_SET(rqc, rqc, scatter_fcs, 1);
5988 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03005989 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5990 if (!(dev->ib_dev.attrs.raw_packet_caps &
5991 IB_RAW_PACKET_CAP_DELAY_DROP)) {
5992 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
5993 err = -EOPNOTSUPP;
5994 goto out;
5995 }
5996 MLX5_SET(rqc, rqc, delay_drop_en, 1);
5997 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005998 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
5999 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03006000 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03006001 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
6002 err = set_delay_drop(dev);
6003 if (err) {
6004 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
6005 err);
6006 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
6007 } else {
6008 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
6009 }
6010 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006011out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03006012 kvfree(in);
6013 return err;
6014}
6015
6016static int set_user_rq_size(struct mlx5_ib_dev *dev,
6017 struct ib_wq_init_attr *wq_init_attr,
6018 struct mlx5_ib_create_wq *ucmd,
6019 struct mlx5_ib_rwq *rwq)
6020{
6021 /* Sanity check RQ size before proceeding */
6022 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
6023 return -EINVAL;
6024
6025 if (!ucmd->rq_wqe_count)
6026 return -EINVAL;
6027
6028 rwq->wqe_count = ucmd->rq_wqe_count;
6029 rwq->wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky0dfe4522018-08-01 14:25:41 -07006030 if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
6031 return -EINVAL;
6032
Yishai Hadas79b20a62016-05-23 15:20:50 +03006033 rwq->log_rq_stride = rwq->wqe_shift;
6034 rwq->log_rq_size = ilog2(rwq->wqe_count);
6035 return 0;
6036}
6037
6038static int prepare_user_rq(struct ib_pd *pd,
6039 struct ib_wq_init_attr *init_attr,
6040 struct ib_udata *udata,
6041 struct mlx5_ib_rwq *rwq)
6042{
6043 struct mlx5_ib_dev *dev = to_mdev(pd->device);
6044 struct mlx5_ib_create_wq ucmd = {};
6045 int err;
6046 size_t required_cmd_sz;
6047
Noa Osherovichccc87082017-10-17 18:01:13 +03006048 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
6049 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006050 if (udata->inlen < required_cmd_sz) {
6051 mlx5_ib_dbg(dev, "invalid inlen\n");
6052 return -EINVAL;
6053 }
6054
6055 if (udata->inlen > sizeof(ucmd) &&
6056 !ib_is_udata_cleared(udata, sizeof(ucmd),
6057 udata->inlen - sizeof(ucmd))) {
6058 mlx5_ib_dbg(dev, "inlen is not supported\n");
6059 return -EOPNOTSUPP;
6060 }
6061
6062 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
6063 mlx5_ib_dbg(dev, "copy failed\n");
6064 return -EFAULT;
6065 }
6066
Noa Osherovichccc87082017-10-17 18:01:13 +03006067 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03006068 mlx5_ib_dbg(dev, "invalid comp mask\n");
6069 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03006070 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
6071 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
6072 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
6073 return -EOPNOTSUPP;
6074 }
6075 if ((ucmd.single_stride_log_num_of_bytes <
6076 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
6077 (ucmd.single_stride_log_num_of_bytes >
6078 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
6079 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
6080 ucmd.single_stride_log_num_of_bytes,
6081 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
6082 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
6083 return -EINVAL;
6084 }
6085 if ((ucmd.single_wqe_log_num_of_strides >
6086 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
6087 (ucmd.single_wqe_log_num_of_strides <
6088 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES)) {
6089 mlx5_ib_dbg(dev, "Invalid log num strides (%u. Range is %u - %u)\n",
6090 ucmd.single_wqe_log_num_of_strides,
6091 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
6092 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
6093 return -EINVAL;
6094 }
6095 rwq->single_stride_log_num_of_bytes =
6096 ucmd.single_stride_log_num_of_bytes;
6097 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
6098 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
6099 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006100 }
6101
6102 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
6103 if (err) {
6104 mlx5_ib_dbg(dev, "err %d\n", err);
6105 return err;
6106 }
6107
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02006108 err = create_user_rq(dev, pd, udata, rwq, &ucmd);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006109 if (err) {
6110 mlx5_ib_dbg(dev, "err %d\n", err);
Gal Pressman645ba592018-10-08 19:44:03 +03006111 return err;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006112 }
6113
6114 rwq->user_index = ucmd.user_index;
6115 return 0;
6116}
6117
6118struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
6119 struct ib_wq_init_attr *init_attr,
6120 struct ib_udata *udata)
6121{
6122 struct mlx5_ib_dev *dev;
6123 struct mlx5_ib_rwq *rwq;
6124 struct mlx5_ib_create_wq_resp resp = {};
6125 size_t min_resp_len;
6126 int err;
6127
6128 if (!udata)
6129 return ERR_PTR(-ENOSYS);
6130
6131 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6132 if (udata->outlen && udata->outlen < min_resp_len)
6133 return ERR_PTR(-EINVAL);
6134
6135 dev = to_mdev(pd->device);
6136 switch (init_attr->wq_type) {
6137 case IB_WQT_RQ:
6138 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
6139 if (!rwq)
6140 return ERR_PTR(-ENOMEM);
6141 err = prepare_user_rq(pd, init_attr, udata, rwq);
6142 if (err)
6143 goto err;
6144 err = create_rq(rwq, pd, init_attr);
6145 if (err)
6146 goto err_user_rq;
6147 break;
6148 default:
6149 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
6150 init_attr->wq_type);
6151 return ERR_PTR(-EINVAL);
6152 }
6153
Yishai Hadas350d0e42016-08-28 14:58:18 +03006154 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006155 rwq->ibwq.state = IB_WQS_RESET;
6156 if (udata->outlen) {
6157 resp.response_length = offsetof(typeof(resp), response_length) +
6158 sizeof(resp.response_length);
6159 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6160 if (err)
6161 goto err_copy;
6162 }
6163
Yishai Hadas350d0e42016-08-28 14:58:18 +03006164 rwq->core_qp.event = mlx5_ib_wq_event;
6165 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006166 return &rwq->ibwq;
6167
6168err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03006169 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006170err_user_rq:
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006171 destroy_user_rq(dev, pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006172err:
6173 kfree(rwq);
6174 return ERR_PTR(err);
6175}
6176
Leon Romanovskya49b1dc2019-06-12 15:27:41 +03006177void mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +03006178{
6179 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6180 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6181
Yishai Hadas350d0e42016-08-28 14:58:18 +03006182 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006183 destroy_user_rq(dev, wq->pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006184 kfree(rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006185}
6186
Yishai Hadasc5f90922016-05-23 15:20:53 +03006187struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
6188 struct ib_rwq_ind_table_init_attr *init_attr,
6189 struct ib_udata *udata)
6190{
6191 struct mlx5_ib_dev *dev = to_mdev(device);
6192 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
6193 int sz = 1 << init_attr->log_ind_tbl_size;
6194 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
6195 size_t min_resp_len;
6196 int inlen;
6197 int err;
6198 int i;
6199 u32 *in;
6200 void *rqtc;
6201
6202 if (udata->inlen > 0 &&
6203 !ib_is_udata_cleared(udata, 0,
6204 udata->inlen))
6205 return ERR_PTR(-EOPNOTSUPP);
6206
Maor Gottliebefd7f402016-10-27 16:36:40 +03006207 if (init_attr->log_ind_tbl_size >
6208 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
6209 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
6210 init_attr->log_ind_tbl_size,
6211 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
6212 return ERR_PTR(-EINVAL);
6213 }
6214
Yishai Hadasc5f90922016-05-23 15:20:53 +03006215 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6216 if (udata->outlen && udata->outlen < min_resp_len)
6217 return ERR_PTR(-EINVAL);
6218
6219 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
6220 if (!rwq_ind_tbl)
6221 return ERR_PTR(-ENOMEM);
6222
6223 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006224 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006225 if (!in) {
6226 err = -ENOMEM;
6227 goto err;
6228 }
6229
6230 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
6231
6232 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
6233 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
6234
6235 for (i = 0; i < sz; i++)
6236 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
6237
Yishai Hadas5deba862018-09-20 21:39:28 +03006238 rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
6239 MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);
6240
Yishai Hadasc5f90922016-05-23 15:20:53 +03006241 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
6242 kvfree(in);
6243
6244 if (err)
6245 goto err;
6246
6247 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
6248 if (udata->outlen) {
6249 resp.response_length = offsetof(typeof(resp), response_length) +
6250 sizeof(resp.response_length);
6251 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6252 if (err)
6253 goto err_copy;
6254 }
6255
6256 return &rwq_ind_tbl->ib_rwq_ind_tbl;
6257
6258err_copy:
Yishai Hadas5deba862018-09-20 21:39:28 +03006259 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006260err:
6261 kfree(rwq_ind_tbl);
6262 return ERR_PTR(err);
6263}
6264
6265int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
6266{
6267 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
6268 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
6269
Yishai Hadas5deba862018-09-20 21:39:28 +03006270 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006271
6272 kfree(rwq_ind_tbl);
6273 return 0;
6274}
6275
Yishai Hadas79b20a62016-05-23 15:20:50 +03006276int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
6277 u32 wq_attr_mask, struct ib_udata *udata)
6278{
6279 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6280 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6281 struct mlx5_ib_modify_wq ucmd = {};
6282 size_t required_cmd_sz;
6283 int curr_wq_state;
6284 int wq_state;
6285 int inlen;
6286 int err;
6287 void *rqc;
6288 void *in;
6289
6290 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
6291 if (udata->inlen < required_cmd_sz)
6292 return -EINVAL;
6293
6294 if (udata->inlen > sizeof(ucmd) &&
6295 !ib_is_udata_cleared(udata, sizeof(ucmd),
6296 udata->inlen - sizeof(ucmd)))
6297 return -EOPNOTSUPP;
6298
6299 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
6300 return -EFAULT;
6301
6302 if (ucmd.comp_mask || ucmd.reserved)
6303 return -EOPNOTSUPP;
6304
6305 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006306 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006307 if (!in)
6308 return -ENOMEM;
6309
6310 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
6311
6312 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
6313 wq_attr->curr_wq_state : wq->state;
6314 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
6315 wq_attr->wq_state : curr_wq_state;
6316 if (curr_wq_state == IB_WQS_ERR)
6317 curr_wq_state = MLX5_RQC_STATE_ERR;
6318 if (wq_state == IB_WQS_ERR)
6319 wq_state = MLX5_RQC_STATE_ERR;
6320 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
Yishai Hadas34d57582018-09-20 21:39:21 +03006321 MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006322 MLX5_SET(rqc, rqc, state, wq_state);
6323
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006324 if (wq_attr_mask & IB_WQ_FLAGS) {
6325 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
6326 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
6327 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
6328 mlx5_ib_dbg(dev, "VLAN offloads are not "
6329 "supported\n");
6330 err = -EOPNOTSUPP;
6331 goto out;
6332 }
6333 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6334 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
6335 MLX5_SET(rqc, rqc, vsd,
6336 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
6337 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02006338
6339 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6340 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
6341 err = -EOPNOTSUPP;
6342 goto out;
6343 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006344 }
6345
Majd Dibbiny23a69642017-01-18 15:25:10 +02006346 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03006347 u16 set_id;
6348
6349 set_id = mlx5_ib_get_counters_id(dev, 0);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006350 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
6351 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6352 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandit3e1f0002019-07-23 10:31:17 +03006353 MLX5_SET(rqc, rqc, counter_set_id, set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006354 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06006355 dev_info_once(
6356 &dev->ib_dev.dev,
6357 "Receive WQ counters are not supported on current FW\n");
Majd Dibbiny23a69642017-01-18 15:25:10 +02006358 }
6359
Yishai Hadas350d0e42016-08-28 14:58:18 +03006360 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006361 if (!err)
6362 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
6363
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006364out:
6365 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006366 return err;
6367}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006368
6369struct mlx5_ib_drain_cqe {
6370 struct ib_cqe cqe;
6371 struct completion done;
6372};
6373
6374static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
6375{
6376 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
6377 struct mlx5_ib_drain_cqe,
6378 cqe);
6379
6380 complete(&cqe->done);
6381}
6382
6383/* This function returns only once the drained WR was completed */
6384static void handle_drain_completion(struct ib_cq *cq,
6385 struct mlx5_ib_drain_cqe *sdrain,
6386 struct mlx5_ib_dev *dev)
6387{
6388 struct mlx5_core_dev *mdev = dev->mdev;
6389
6390 if (cq->poll_ctx == IB_POLL_DIRECT) {
6391 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
6392 ib_process_cq_direct(cq, -1);
6393 return;
6394 }
6395
6396 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6397 struct mlx5_ib_cq *mcq = to_mcq(cq);
6398 bool triggered = false;
6399 unsigned long flags;
6400
6401 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
6402 /* Make sure that the CQ handler won't run if wasn't run yet */
6403 if (!mcq->mcq.reset_notify_added)
6404 mcq->mcq.reset_notify_added = 1;
6405 else
6406 triggered = true;
6407 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
6408
6409 if (triggered) {
6410 /* Wait for any scheduled/running task to be ended */
6411 switch (cq->poll_ctx) {
6412 case IB_POLL_SOFTIRQ:
6413 irq_poll_disable(&cq->iop);
6414 irq_poll_enable(&cq->iop);
6415 break;
6416 case IB_POLL_WORKQUEUE:
6417 cancel_work_sync(&cq->work);
6418 break;
6419 default:
6420 WARN_ON_ONCE(1);
6421 }
6422 }
6423
6424 /* Run the CQ handler - this makes sure that the drain WR will
6425 * be processed if wasn't processed yet.
6426 */
Yishai Hadas4e0e2ea2019-06-30 19:23:27 +03006427 mcq->mcq.comp(&mcq->mcq, NULL);
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006428 }
6429
6430 wait_for_completion(&sdrain->done);
6431}
6432
6433void mlx5_ib_drain_sq(struct ib_qp *qp)
6434{
6435 struct ib_cq *cq = qp->send_cq;
6436 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6437 struct mlx5_ib_drain_cqe sdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006438 const struct ib_send_wr *bad_swr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006439 struct ib_rdma_wr swr = {
6440 .wr = {
6441 .next = NULL,
6442 { .wr_cqe = &sdrain.cqe, },
6443 .opcode = IB_WR_RDMA_WRITE,
6444 },
6445 };
6446 int ret;
6447 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6448 struct mlx5_core_dev *mdev = dev->mdev;
6449
6450 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6451 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6452 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6453 return;
6454 }
6455
6456 sdrain.cqe.done = mlx5_ib_drain_qp_done;
6457 init_completion(&sdrain.done);
6458
6459 ret = _mlx5_ib_post_send(qp, &swr.wr, &bad_swr, true);
6460 if (ret) {
6461 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6462 return;
6463 }
6464
6465 handle_drain_completion(cq, &sdrain, dev);
6466}
6467
6468void mlx5_ib_drain_rq(struct ib_qp *qp)
6469{
6470 struct ib_cq *cq = qp->recv_cq;
6471 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6472 struct mlx5_ib_drain_cqe rdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006473 struct ib_recv_wr rwr = {};
6474 const struct ib_recv_wr *bad_rwr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006475 int ret;
6476 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6477 struct mlx5_core_dev *mdev = dev->mdev;
6478
6479 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6480 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6481 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6482 return;
6483 }
6484
6485 rwr.wr_cqe = &rdrain.cqe;
6486 rdrain.cqe.done = mlx5_ib_drain_qp_done;
6487 init_completion(&rdrain.done);
6488
6489 ret = _mlx5_ib_post_recv(qp, &rwr, &bad_rwr, true);
6490 if (ret) {
6491 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6492 return;
6493 }
6494
6495 handle_drain_completion(cq, &rdrain, dev);
6496}
Mark Zhangd14133d2019-07-02 13:02:36 +03006497
6498/**
6499 * Bind a qp to a counter. If @counter is NULL then bind the qp to
6500 * the default counter
6501 */
6502int mlx5_ib_qp_set_counter(struct ib_qp *qp, struct rdma_counter *counter)
6503{
6504 struct mlx5_ib_qp *mqp = to_mqp(qp);
6505 int err = 0;
6506
6507 mutex_lock(&mqp->mutex);
6508 if (mqp->state == IB_QPS_RESET) {
6509 qp->counter = counter;
6510 goto out;
6511 }
6512
6513 if (mqp->state == IB_QPS_RTS) {
6514 err = __mlx5_ib_qp_set_counter(qp, counter);
6515 if (!err)
6516 qp->counter = counter;
6517
6518 goto out;
6519 }
6520
6521 mqp->counter_pending = 1;
6522 qp->counter = counter;
6523
6524out:
6525 mutex_unlock(&mqp->mutex);
6526 return err;
6527}