blob: 4fbf60fed3742e4712009e2b94c017a7fb6179cd [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>
Yishai Hadasc2e53b22017-06-08 16:15:08 +030037#include <linux/mlx5/fs.h>
Eli Cohene126ba92013-07-07 17:25:49 +030038#include "mlx5_ib.h"
Mark Blochb96c9dd2018-01-29 10:40:37 +000039#include "ib_rep.h"
Yishai Hadas443c1cf2018-09-20 21:39:26 +030040#include "cmd.h"
Eli Cohene126ba92013-07-07 17:25:49 +030041
42/* not supported currently */
43static int wq_signature;
44
45enum {
46 MLX5_IB_ACK_REQ_FREQ = 8,
47};
48
49enum {
50 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
51 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
52 MLX5_IB_LINK_TYPE_IB = 0,
53 MLX5_IB_LINK_TYPE_ETH = 1
54};
55
56enum {
57 MLX5_IB_SQ_STRIDE = 6,
Idan Burstein064e5262018-05-02 13:16:39 +030058 MLX5_IB_SQ_UMR_INLINE_THRESHOLD = 64,
Eli Cohene126ba92013-07-07 17:25:49 +030059};
60
61static const u32 mlx5_ib_opcode[] = {
62 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020063 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030064 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
65 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
66 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
67 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
68 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
69 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
70 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
71 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030072 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030073 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
74 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
75 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
76};
77
Erez Shitritf0313962016-02-21 16:27:17 +020078struct mlx5_wqe_eth_pad {
79 u8 rsvd0[16];
80};
Eli Cohene126ba92013-07-07 17:25:49 +030081
Alex Veskereb49ab02016-08-28 12:25:53 +030082enum raw_qp_set_mask_map {
83 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020084 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030085};
86
Alex Vesker0680efa2016-08-28 12:25:52 +030087struct mlx5_modify_raw_qp_param {
88 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030089
90 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang61147f32018-03-19 15:10:30 +020091
92 struct mlx5_rate_limit rl;
93
Alex Veskereb49ab02016-08-28 12:25:53 +030094 u8 rq_q_ctr_id;
Mark Blochd5ed8ac2019-03-28 15:27:38 +020095 u16 port;
Alex Vesker0680efa2016-08-28 12:25:52 +030096};
97
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030098static void get_cqs(enum ib_qp_type qp_type,
99 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
100 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
101
Eli Cohene126ba92013-07-07 17:25:49 +0300102static int is_qp0(enum ib_qp_type qp_type)
103{
104 return qp_type == IB_QPT_SMI;
105}
106
Eli Cohene126ba92013-07-07 17:25:49 +0300107static int is_sqp(enum ib_qp_type qp_type)
108{
109 return is_qp0(qp_type) || is_qp1(qp_type);
110}
111
Haggai Eranc1395a22014-12-11 17:04:14 +0200112/**
Moni Shouafbeb4072019-01-22 08:48:46 +0200113 * mlx5_ib_read_user_wqe_common() - Copy a WQE (or part of) from user WQ
114 * to kernel buffer
Haggai Eranc1395a22014-12-11 17:04:14 +0200115 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200116 * @umem: User space memory where the WQ is
117 * @buffer: buffer to copy to
118 * @buflen: buffer length
119 * @wqe_index: index of WQE to copy from
120 * @wq_offset: offset to start of WQ
121 * @wq_wqe_cnt: number of WQEs in WQ
122 * @wq_wqe_shift: log2 of WQE size
123 * @bcnt: number of bytes to copy
124 * @bytes_copied: number of bytes to copy (return value)
Haggai Eranc1395a22014-12-11 17:04:14 +0200125 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200126 * Copies from start of WQE bcnt or less bytes.
127 * Does not gurantee to copy the entire WQE.
Haggai Eranc1395a22014-12-11 17:04:14 +0200128 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200129 * Return: zero on success, or an error code.
Haggai Eranc1395a22014-12-11 17:04:14 +0200130 */
Moni Shouafbeb4072019-01-22 08:48:46 +0200131static int mlx5_ib_read_user_wqe_common(struct ib_umem *umem,
132 void *buffer,
133 u32 buflen,
134 int wqe_index,
135 int wq_offset,
136 int wq_wqe_cnt,
137 int wq_wqe_shift,
138 int bcnt,
139 size_t *bytes_copied)
Haggai Eranc1395a22014-12-11 17:04:14 +0200140{
Moni Shouafbeb4072019-01-22 08:48:46 +0200141 size_t offset = wq_offset + ((wqe_index % wq_wqe_cnt) << wq_wqe_shift);
142 size_t wq_end = wq_offset + (wq_wqe_cnt << wq_wqe_shift);
143 size_t copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200144 int ret;
145
Moni Shouafbeb4072019-01-22 08:48:46 +0200146 /* don't copy more than requested, more than buffer length or
147 * beyond WQ end
148 */
149 copy_length = min_t(u32, buflen, wq_end - offset);
150 copy_length = min_t(u32, copy_length, bcnt);
Haggai Eranc1395a22014-12-11 17:04:14 +0200151
Moni Shouafbeb4072019-01-22 08:48:46 +0200152 ret = ib_umem_copy_from(buffer, umem, offset, copy_length);
Haggai Eranc1395a22014-12-11 17:04:14 +0200153 if (ret)
154 return ret;
155
Moni Shouafbeb4072019-01-22 08:48:46 +0200156 if (!ret && bytes_copied)
157 *bytes_copied = copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200158
Moni Shouafbeb4072019-01-22 08:48:46 +0200159 return 0;
160}
Haggai Eranc1395a22014-12-11 17:04:14 +0200161
Moni Shouafbeb4072019-01-22 08:48:46 +0200162int mlx5_ib_read_user_wqe_sq(struct mlx5_ib_qp *qp,
163 int wqe_index,
164 void *buffer,
165 int buflen,
166 size_t *bc)
167{
168 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
169 struct ib_umem *umem = base->ubuffer.umem;
170 struct mlx5_ib_wq *wq = &qp->sq;
171 struct mlx5_wqe_ctrl_seg *ctrl;
172 size_t bytes_copied;
173 size_t bytes_copied2;
174 size_t wqe_length;
175 int ret;
176 int ds;
Haggai Eranc1395a22014-12-11 17:04:14 +0200177
Moni Shouafbeb4072019-01-22 08:48:46 +0200178 if (buflen < sizeof(*ctrl))
179 return -EINVAL;
180
181 /* at first read as much as possible */
182 ret = mlx5_ib_read_user_wqe_common(umem,
183 buffer,
184 buflen,
185 wqe_index,
186 wq->offset,
187 wq->wqe_cnt,
188 wq->wqe_shift,
189 buflen,
190 &bytes_copied);
Haggai Eranc1395a22014-12-11 17:04:14 +0200191 if (ret)
192 return ret;
193
Moni Shouafbeb4072019-01-22 08:48:46 +0200194 /* we need at least control segment size to proceed */
195 if (bytes_copied < sizeof(*ctrl))
196 return -EINVAL;
197
198 ctrl = buffer;
199 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
200 wqe_length = ds * MLX5_WQE_DS_UNITS;
201
202 /* if we copied enough then we are done */
203 if (bytes_copied >= wqe_length) {
204 *bc = bytes_copied;
205 return 0;
206 }
207
208 /* otherwise this a wrapped around wqe
209 * so read the remaining bytes starting
210 * from wqe_index 0
211 */
212 ret = mlx5_ib_read_user_wqe_common(umem,
213 buffer + bytes_copied,
214 buflen - bytes_copied,
215 0,
216 wq->offset,
217 wq->wqe_cnt,
218 wq->wqe_shift,
219 wqe_length - bytes_copied,
220 &bytes_copied2);
221
222 if (ret)
223 return ret;
224 *bc = bytes_copied + bytes_copied2;
225 return 0;
226}
227
228int mlx5_ib_read_user_wqe_rq(struct mlx5_ib_qp *qp,
229 int wqe_index,
230 void *buffer,
231 int buflen,
232 size_t *bc)
233{
234 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
235 struct ib_umem *umem = base->ubuffer.umem;
236 struct mlx5_ib_wq *wq = &qp->rq;
237 size_t bytes_copied;
238 int ret;
239
240 ret = mlx5_ib_read_user_wqe_common(umem,
241 buffer,
242 buflen,
243 wqe_index,
244 wq->offset,
245 wq->wqe_cnt,
246 wq->wqe_shift,
247 buflen,
248 &bytes_copied);
249
250 if (ret)
251 return ret;
252 *bc = bytes_copied;
253 return 0;
254}
255
256int mlx5_ib_read_user_wqe_srq(struct mlx5_ib_srq *srq,
257 int wqe_index,
258 void *buffer,
259 int buflen,
260 size_t *bc)
261{
262 struct ib_umem *umem = srq->umem;
263 size_t bytes_copied;
264 int ret;
265
266 ret = mlx5_ib_read_user_wqe_common(umem,
267 buffer,
268 buflen,
269 wqe_index,
270 0,
271 srq->msrq.max,
272 srq->msrq.wqe_shift,
273 buflen,
274 &bytes_copied);
275
276 if (ret)
277 return ret;
278 *bc = bytes_copied;
279 return 0;
Haggai Eranc1395a22014-12-11 17:04:14 +0200280}
281
Eli Cohene126ba92013-07-07 17:25:49 +0300282static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
283{
284 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
285 struct ib_event event;
286
majd@mellanox.com19098df2016-01-14 19:13:03 +0200287 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
288 /* This event is only valid for trans_qps */
289 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
290 }
Eli Cohene126ba92013-07-07 17:25:49 +0300291
292 if (ibqp->event_handler) {
293 event.device = ibqp->device;
294 event.element.qp = ibqp;
295 switch (type) {
296 case MLX5_EVENT_TYPE_PATH_MIG:
297 event.event = IB_EVENT_PATH_MIG;
298 break;
299 case MLX5_EVENT_TYPE_COMM_EST:
300 event.event = IB_EVENT_COMM_EST;
301 break;
302 case MLX5_EVENT_TYPE_SQ_DRAINED:
303 event.event = IB_EVENT_SQ_DRAINED;
304 break;
305 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
306 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
307 break;
308 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
309 event.event = IB_EVENT_QP_FATAL;
310 break;
311 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
312 event.event = IB_EVENT_PATH_MIG_ERR;
313 break;
314 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
315 event.event = IB_EVENT_QP_REQ_ERR;
316 break;
317 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
318 event.event = IB_EVENT_QP_ACCESS_ERR;
319 break;
320 default:
321 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
322 return;
323 }
324
325 ibqp->event_handler(&event, ibqp->qp_context);
326 }
327}
328
329static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
330 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
331{
332 int wqe_size;
333 int wq_size;
334
335 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300336 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300337 return -EINVAL;
338
339 if (!has_rq) {
340 qp->rq.max_gs = 0;
341 qp->rq.wqe_cnt = 0;
342 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300343 cap->max_recv_wr = 0;
344 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300345 } else {
346 if (ucmd) {
347 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300348 if (ucmd->rq_wqe_shift > BITS_PER_BYTE * sizeof(ucmd->rq_wqe_shift))
349 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300350 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300351 if ((1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) < qp->wq_sig)
352 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300353 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
354 qp->rq.max_post = qp->rq.wqe_cnt;
355 } else {
356 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
357 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
358 wqe_size = roundup_pow_of_two(wqe_size);
359 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
360 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
361 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300362 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300363 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
364 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300365 MLX5_CAP_GEN(dev->mdev,
366 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300367 return -EINVAL;
368 }
369 qp->rq.wqe_shift = ilog2(wqe_size);
370 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
371 qp->rq.max_post = qp->rq.wqe_cnt;
372 }
373 }
374
375 return 0;
376}
377
Erez Shitritf0313962016-02-21 16:27:17 +0200378static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300379{
Andi Shyti618af382013-07-16 15:35:01 +0200380 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300381
Erez Shitritf0313962016-02-21 16:27:17 +0200382 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300383 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300384 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300385 /* fall through */
386 case IB_QPT_RC:
387 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200388 max(sizeof(struct mlx5_wqe_atomic_seg) +
389 sizeof(struct mlx5_wqe_raddr_seg),
390 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
Idan Burstein064e5262018-05-02 13:16:39 +0300391 sizeof(struct mlx5_mkey_seg) +
392 MLX5_IB_SQ_UMR_INLINE_THRESHOLD /
393 MLX5_IB_UMR_OCTOWORD);
Eli Cohene126ba92013-07-07 17:25:49 +0300394 break;
395
Eli Cohenb125a542013-09-11 16:35:22 +0300396 case IB_QPT_XRC_TGT:
397 return 0;
398
Eli Cohene126ba92013-07-07 17:25:49 +0300399 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300400 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200401 max(sizeof(struct mlx5_wqe_raddr_seg),
402 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
403 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300404 break;
405
406 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200407 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
408 size += sizeof(struct mlx5_wqe_eth_pad) +
409 sizeof(struct mlx5_wqe_eth_seg);
410 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300411 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200412 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300413 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300414 sizeof(struct mlx5_wqe_datagram_seg);
415 break;
416
417 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300418 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300419 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
420 sizeof(struct mlx5_mkey_seg);
421 break;
422
423 default:
424 return -EINVAL;
425 }
426
427 return size;
428}
429
430static int calc_send_wqe(struct ib_qp_init_attr *attr)
431{
432 int inl_size = 0;
433 int size;
434
Erez Shitritf0313962016-02-21 16:27:17 +0200435 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300436 if (size < 0)
437 return size;
438
439 if (attr->cap.max_inline_data) {
440 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
441 attr->cap.max_inline_data;
442 }
443
444 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300445 if (attr->create_flags & IB_QP_CREATE_INTEGRITY_EN &&
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200446 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300447 return MLX5_SIG_WQE_SIZE;
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200448 else
449 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300450}
451
Eli Cohen288c01b2016-10-27 16:36:45 +0300452static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
453{
454 int max_sge;
455
456 if (attr->qp_type == IB_QPT_RC)
457 max_sge = (min_t(int, wqe_size, 512) -
458 sizeof(struct mlx5_wqe_ctrl_seg) -
459 sizeof(struct mlx5_wqe_raddr_seg)) /
460 sizeof(struct mlx5_wqe_data_seg);
461 else if (attr->qp_type == IB_QPT_XRC_INI)
462 max_sge = (min_t(int, wqe_size, 512) -
463 sizeof(struct mlx5_wqe_ctrl_seg) -
464 sizeof(struct mlx5_wqe_xrc_seg) -
465 sizeof(struct mlx5_wqe_raddr_seg)) /
466 sizeof(struct mlx5_wqe_data_seg);
467 else
468 max_sge = (wqe_size - sq_overhead(attr)) /
469 sizeof(struct mlx5_wqe_data_seg);
470
471 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
472 sizeof(struct mlx5_wqe_data_seg));
473}
474
Eli Cohene126ba92013-07-07 17:25:49 +0300475static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
476 struct mlx5_ib_qp *qp)
477{
478 int wqe_size;
479 int wq_size;
480
481 if (!attr->cap.max_send_wr)
482 return 0;
483
484 wqe_size = calc_send_wqe(attr);
485 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
486 if (wqe_size < 0)
487 return wqe_size;
488
Saeed Mahameed938fe832015-05-28 22:28:41 +0300489 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300490 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300491 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300492 return -EINVAL;
493 }
494
Erez Shitritf0313962016-02-21 16:27:17 +0200495 qp->max_inline_data = wqe_size - sq_overhead(attr) -
496 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300497 attr->cap.max_inline_data = qp->max_inline_data;
498
499 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
500 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300501 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800502 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
503 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300504 qp->sq.wqe_cnt,
505 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300506 return -ENOMEM;
507 }
Eli Cohene126ba92013-07-07 17:25:49 +0300508 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300509 qp->sq.max_gs = get_send_sge(attr, wqe_size);
510 if (qp->sq.max_gs < attr->cap.max_send_sge)
511 return -ENOMEM;
512
513 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300514 qp->sq.max_post = wq_size / wqe_size;
515 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300516
517 return wq_size;
518}
519
520static int set_user_buf_size(struct mlx5_ib_dev *dev,
521 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200522 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200523 struct mlx5_ib_qp_base *base,
524 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300525{
526 int desc_sz = 1 << qp->sq.wqe_shift;
527
Saeed Mahameed938fe832015-05-28 22:28:41 +0300528 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300529 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300530 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300531 return -EINVAL;
532 }
533
Gal Pressmanaf8b38e2019-02-06 15:45:35 +0200534 if (ucmd->sq_wqe_count && !is_power_of_2(ucmd->sq_wqe_count)) {
535 mlx5_ib_warn(dev, "sq_wqe_count %d is not a power of two\n",
536 ucmd->sq_wqe_count);
Eli Cohene126ba92013-07-07 17:25:49 +0300537 return -EINVAL;
538 }
539
540 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
541
Saeed Mahameed938fe832015-05-28 22:28:41 +0300542 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300543 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300544 qp->sq.wqe_cnt,
545 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300546 return -EINVAL;
547 }
548
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300549 if (attr->qp_type == IB_QPT_RAW_PACKET ||
550 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200551 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
552 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
553 } else {
554 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
555 (qp->sq.wqe_cnt << 6);
556 }
Eli Cohene126ba92013-07-07 17:25:49 +0300557
558 return 0;
559}
560
561static int qp_has_rq(struct ib_qp_init_attr *attr)
562{
563 if (attr->qp_type == IB_QPT_XRC_INI ||
564 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
565 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
566 !attr->cap.max_recv_wr)
567 return 0;
568
569 return 1;
570}
571
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200572enum {
573 /* this is the first blue flame register in the array of bfregs assigned
574 * to a processes. Since we do not use it for blue flame but rather
575 * regular 64 bit doorbells, we do not need a lock for maintaiing
576 * "odd/even" order
577 */
578 NUM_NON_BLUE_FLAME_BFREGS = 1,
579};
580
Eli Cohenb037c292017-01-03 23:55:26 +0200581static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
582{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200583 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200584}
585
586static int num_med_bfreg(struct mlx5_ib_dev *dev,
587 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200588{
589 int n;
590
Eli Cohenb037c292017-01-03 23:55:26 +0200591 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
592 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200593
594 return n >= 0 ? n : 0;
595}
596
Yishai Hadas18b03622018-05-07 10:20:01 +0300597static int first_med_bfreg(struct mlx5_ib_dev *dev,
598 struct mlx5_bfreg_info *bfregi)
599{
600 return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
601}
602
Eli Cohenb037c292017-01-03 23:55:26 +0200603static int first_hi_bfreg(struct mlx5_ib_dev *dev,
604 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200605{
606 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200607
Eli Cohenb037c292017-01-03 23:55:26 +0200608 med = num_med_bfreg(dev, bfregi);
609 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200610}
611
Eli Cohenb037c292017-01-03 23:55:26 +0200612static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
613 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300614{
Eli Cohene126ba92013-07-07 17:25:49 +0300615 int i;
616
Eli Cohenb037c292017-01-03 23:55:26 +0200617 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
618 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200619 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300620 return i;
621 }
622 }
623
624 return -ENOMEM;
625}
626
Eli Cohenb037c292017-01-03 23:55:26 +0200627static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
628 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300629{
Yishai Hadas18b03622018-05-07 10:20:01 +0300630 int minidx = first_med_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300631 int i;
632
Yishai Hadas18b03622018-05-07 10:20:01 +0300633 if (minidx < 0)
634 return minidx;
635
636 for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200637 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300638 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200639 if (!bfregi->count[minidx])
640 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300641 }
642
Eli Cohen2f5ff262017-01-03 23:55:21 +0200643 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300644 return minidx;
645}
646
Eli Cohenb037c292017-01-03 23:55:26 +0200647static int alloc_bfreg(struct mlx5_ib_dev *dev,
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300648 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300649{
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300650 int bfregn = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +0300651
Eli Cohen2f5ff262017-01-03 23:55:21 +0200652 mutex_lock(&bfregi->lock);
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300653 if (bfregi->ver >= 2) {
654 bfregn = alloc_high_class_bfreg(dev, bfregi);
655 if (bfregn < 0)
656 bfregn = alloc_med_class_bfreg(dev, bfregi);
657 }
658
659 if (bfregn < 0) {
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200660 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200661 bfregn = 0;
662 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300663 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200664 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300665
Eli Cohen2f5ff262017-01-03 23:55:21 +0200666 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300667}
668
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200669void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300670{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200671 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200672 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200673 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300674}
675
676static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
677{
678 switch (state) {
679 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
680 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
681 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
682 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
683 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
684 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
685 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
686 default: return -1;
687 }
688}
689
690static int to_mlx5_st(enum ib_qp_type type)
691{
692 switch (type) {
693 case IB_QPT_RC: return MLX5_QP_ST_RC;
694 case IB_QPT_UC: return MLX5_QP_ST_UC;
695 case IB_QPT_UD: return MLX5_QP_ST_UD;
696 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
697 case IB_QPT_XRC_INI:
698 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
699 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200700 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200701 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300702 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300703 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200704 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300705 case IB_QPT_MAX:
706 default: return -EINVAL;
707 }
708}
709
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300710static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
711 struct mlx5_ib_cq *recv_cq);
712static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
713 struct mlx5_ib_cq *recv_cq);
714
Yishai Hadas7c043e92018-06-17 13:00:03 +0300715int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300716 struct mlx5_bfreg_info *bfregi, u32 bfregn,
Yishai Hadas7c043e92018-06-17 13:00:03 +0300717 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300718{
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300719 unsigned int bfregs_per_sys_page;
720 u32 index_of_sys_page;
721 u32 offset;
Eli Cohenb037c292017-01-03 23:55:26 +0200722
723 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
724 MLX5_NON_FP_BFREGS_PER_UAR;
725 index_of_sys_page = bfregn / bfregs_per_sys_page;
726
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200727 if (dyn_bfreg) {
728 index_of_sys_page += bfregi->num_static_sys_pages;
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300729
730 if (index_of_sys_page >= bfregi->num_sys_pages)
731 return -EINVAL;
732
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200733 if (bfregn > bfregi->num_dyn_bfregs ||
734 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
735 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
736 return -EINVAL;
737 }
738 }
Eli Cohenb037c292017-01-03 23:55:26 +0200739
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200740 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200741 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300742}
743
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200744static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200745 unsigned long addr, size_t size,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200746 struct ib_umem **umem, int *npages, int *page_shift,
747 int *ncont, u32 *offset)
majd@mellanox.com19098df2016-01-14 19:13:03 +0200748{
749 int err;
750
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200751 *umem = ib_umem_get(udata, addr, size, 0, 0);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200752 if (IS_ERR(*umem)) {
753 mlx5_ib_dbg(dev, "umem_get failed\n");
754 return PTR_ERR(*umem);
755 }
756
Majd Dibbiny762f8992016-10-27 16:36:47 +0300757 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200758
759 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
760 if (err) {
761 mlx5_ib_warn(dev, "bad offset\n");
762 goto err_umem;
763 }
764
765 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
766 addr, size, *npages, *page_shift, *ncont, *offset);
767
768 return 0;
769
770err_umem:
771 ib_umem_release(*umem);
772 *umem = NULL;
773
774 return err;
775}
776
Maor Gottliebfe248c32017-05-30 10:29:14 +0300777static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300778 struct mlx5_ib_rwq *rwq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300779{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300780 struct mlx5_ib_ucontext *context =
781 rdma_udata_to_drv_context(
782 udata,
783 struct mlx5_ib_ucontext,
784 ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300785
Maor Gottliebfe248c32017-05-30 10:29:14 +0300786 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
787 atomic_dec(&dev->delay_drop.rqs_cnt);
788
Yishai Hadas79b20a62016-05-23 15:20:50 +0300789 mlx5_ib_db_unmap_user(context, &rwq->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300790 ib_umem_release(rwq->umem);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300791}
792
793static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200794 struct ib_udata *udata, struct mlx5_ib_rwq *rwq,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300795 struct mlx5_ib_create_wq *ucmd)
796{
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200797 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
798 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300799 int page_shift = 0;
800 int npages;
801 u32 offset = 0;
802 int ncont = 0;
803 int err;
804
805 if (!ucmd->buf_addr)
806 return -EINVAL;
807
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200808 rwq->umem = ib_umem_get(udata, ucmd->buf_addr, rwq->buf_size, 0, 0);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300809 if (IS_ERR(rwq->umem)) {
810 mlx5_ib_dbg(dev, "umem_get failed\n");
811 err = PTR_ERR(rwq->umem);
812 return err;
813 }
814
Majd Dibbiny762f8992016-10-27 16:36:47 +0300815 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300816 &ncont, NULL);
817 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
818 &rwq->rq_page_offset);
819 if (err) {
820 mlx5_ib_warn(dev, "bad offset\n");
821 goto err_umem;
822 }
823
824 rwq->rq_num_pas = ncont;
825 rwq->page_shift = page_shift;
826 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
827 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
828
829 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
830 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
831 npages, page_shift, ncont, offset);
832
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200833 err = mlx5_ib_db_map_user(ucontext, udata, ucmd->db_addr, &rwq->db);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300834 if (err) {
835 mlx5_ib_dbg(dev, "map failed\n");
836 goto err_umem;
837 }
838
839 rwq->create_type = MLX5_WQ_USER;
840 return 0;
841
842err_umem:
843 ib_umem_release(rwq->umem);
844 return err;
845}
846
Eli Cohenb037c292017-01-03 23:55:26 +0200847static int adjust_bfregn(struct mlx5_ib_dev *dev,
848 struct mlx5_bfreg_info *bfregi, int bfregn)
849{
850 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
851 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
852}
853
Eli Cohene126ba92013-07-07 17:25:49 +0300854static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
855 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200856 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300857 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200858 struct mlx5_ib_create_qp_resp *resp, int *inlen,
859 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300860{
861 struct mlx5_ib_ucontext *context;
862 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200863 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200864 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200865 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300866 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200867 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200868 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200869 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300870 __be64 *pas;
871 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300872 int err;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200873 u16 uid;
Eli Cohene126ba92013-07-07 17:25:49 +0300874
875 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
876 if (err) {
877 mlx5_ib_dbg(dev, "copy failed\n");
878 return err;
879 }
880
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200881 context = rdma_udata_to_drv_context(udata, struct mlx5_ib_ucontext,
882 ibucontext);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200883 if (ucmd.flags & MLX5_QP_FLAG_BFREG_INDEX) {
884 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
885 ucmd.bfreg_index, true);
886 if (uar_index < 0)
887 return uar_index;
888
889 bfregn = MLX5_IB_INVALID_BFREG;
890 } else if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL) {
891 /*
892 * TBD: should come from the verbs when we have the API
893 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200894 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200895 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200896 }
Leon Romanovsky051f2632015-12-20 12:16:11 +0200897 else {
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300898 bfregn = alloc_bfreg(dev, &context->bfregi);
899 if (bfregn < 0)
900 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300901 }
902
Eli Cohen2f5ff262017-01-03 23:55:21 +0200903 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200904 if (bfregn != MLX5_IB_INVALID_BFREG)
905 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
906 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300907
Haggai Eran48fea832014-05-22 14:50:11 +0300908 qp->rq.offset = 0;
909 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
910 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
911
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200912 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300913 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200914 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300915
majd@mellanox.com19098df2016-01-14 19:13:03 +0200916 if (ucmd.buf_addr && ubuffer->buf_size) {
917 ubuffer->buf_addr = ucmd.buf_addr;
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200918 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr,
919 ubuffer->buf_size, &ubuffer->umem,
920 &npages, &page_shift, &ncont, &offset);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200921 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200922 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200923 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200924 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300925 }
Eli Cohene126ba92013-07-07 17:25:49 +0300926
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300927 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
928 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300929 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300930 if (!*in) {
931 err = -ENOMEM;
932 goto err_umem;
933 }
Eli Cohene126ba92013-07-07 17:25:49 +0300934
Yishai Hadas7422edc2018-12-23 13:12:21 +0200935 uid = (attr->qp_type != IB_QPT_XRC_TGT &&
936 attr->qp_type != IB_QPT_XRC_INI) ? to_mpd(pd)->uid : 0;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200937 MLX5_SET(create_qp_in, *in, uid, uid);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300938 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
939 if (ubuffer->umem)
940 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
941
942 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
943
944 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
945 MLX5_SET(qpc, qpc, page_offset, offset);
946
947 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200948 if (bfregn != MLX5_IB_INVALID_BFREG)
949 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
950 else
951 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200952 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300953
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200954 err = mlx5_ib_db_map_user(context, udata, ucmd.db_addr, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300955 if (err) {
956 mlx5_ib_dbg(dev, "map failed\n");
957 goto err_free;
958 }
959
Jason Gunthorpe41d902c2018-04-03 10:00:53 +0300960 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
Eli Cohene126ba92013-07-07 17:25:49 +0300961 if (err) {
962 mlx5_ib_dbg(dev, "copy failed\n");
963 goto err_unmap;
964 }
965 qp->create_type = MLX5_QP_USER;
966
967 return 0;
968
969err_unmap:
970 mlx5_ib_db_unmap_user(context, &qp->db);
971
972err_free:
Al Viro479163f2014-11-20 08:13:57 +0000973 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300974
975err_umem:
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300976 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300977
Eli Cohen2f5ff262017-01-03 23:55:21 +0200978err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200979 if (bfregn != MLX5_IB_INVALID_BFREG)
980 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300981 return err;
982}
983
Eli Cohenb037c292017-01-03 23:55:26 +0200984static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300985 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base,
986 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +0300987{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300988 struct mlx5_ib_ucontext *context =
989 rdma_udata_to_drv_context(
990 udata,
991 struct mlx5_ib_ucontext,
992 ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +0300993
Eli Cohene126ba92013-07-07 17:25:49 +0300994 mlx5_ib_db_unmap_user(context, &qp->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300995 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200996
997 /*
998 * Free only the BFREGs which are handled by the kernel.
999 * BFREGs of UARs allocated dynamically are handled by user.
1000 */
1001 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
1002 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001003}
1004
Guy Levi34f4c952018-11-26 08:15:50 +02001005/* get_sq_edge - Get the next nearby edge.
1006 *
1007 * An 'edge' is defined as the first following address after the end
1008 * of the fragment or the SQ. Accordingly, during the WQE construction
1009 * which repetitively increases the pointer to write the next data, it
1010 * simply should check if it gets to an edge.
1011 *
1012 * @sq - SQ buffer.
1013 * @idx - Stride index in the SQ buffer.
1014 *
1015 * Return:
1016 * The new edge.
1017 */
1018static void *get_sq_edge(struct mlx5_ib_wq *sq, u32 idx)
1019{
1020 void *fragment_end;
1021
1022 fragment_end = mlx5_frag_buf_get_wqe
1023 (&sq->fbc,
1024 mlx5_frag_buf_get_idx_last_contig_stride(&sq->fbc, idx));
1025
1026 return fragment_end + MLX5_SEND_WQE_BB;
1027}
1028
Eli Cohene126ba92013-07-07 17:25:49 +03001029static int create_kernel_qp(struct mlx5_ib_dev *dev,
1030 struct ib_qp_init_attr *init_attr,
1031 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001032 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +02001033 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +03001034{
Eli Cohene126ba92013-07-07 17:25:49 +03001035 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001036 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +03001037 int err;
1038
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +03001039 if (init_attr->create_flags & ~(IB_QP_CREATE_INTEGRITY_EN |
Erez Shitritf0313962016-02-21 16:27:17 +02001040 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +02001041 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +03001042 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +02001043 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +02001044 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001045
1046 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001047 qp->bf.bfreg = &dev->fp_bfreg;
1048 else
1049 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +03001050
Eli Cohend8030b02017-02-09 19:31:47 +02001051 /* We need to divide by two since each register is comprised of
1052 * two buffers of identical size, namely odd and even
1053 */
1054 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001055 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +03001056
1057 err = calc_sq_size(dev, init_attr, qp);
1058 if (err < 0) {
1059 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001060 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001061 }
1062
1063 qp->rq.offset = 0;
1064 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001065 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +03001066
Guy Levi34f4c952018-11-26 08:15:50 +02001067 err = mlx5_frag_buf_alloc_node(dev->mdev, base->ubuffer.buf_size,
1068 &qp->buf, dev->mdev->priv.numa_node);
Eli Cohene126ba92013-07-07 17:25:49 +03001069 if (err) {
1070 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001071 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001072 }
1073
Guy Levi34f4c952018-11-26 08:15:50 +02001074 if (qp->rq.wqe_cnt)
1075 mlx5_init_fbc(qp->buf.frags, qp->rq.wqe_shift,
1076 ilog2(qp->rq.wqe_cnt), &qp->rq.fbc);
1077
1078 if (qp->sq.wqe_cnt) {
1079 int sq_strides_offset = (qp->sq.offset & (PAGE_SIZE - 1)) /
1080 MLX5_SEND_WQE_BB;
1081 mlx5_init_fbc_offset(qp->buf.frags +
1082 (qp->sq.offset / PAGE_SIZE),
1083 ilog2(MLX5_SEND_WQE_BB),
1084 ilog2(qp->sq.wqe_cnt),
1085 sq_strides_offset, &qp->sq.fbc);
1086
1087 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
1088 }
1089
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001090 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
1091 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001092 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001093 if (!*in) {
1094 err = -ENOMEM;
1095 goto err_buf;
1096 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001097
1098 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1099 MLX5_SET(qpc, qpc, uar_page, uar_index);
1100 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1101
Eli Cohene126ba92013-07-07 17:25:49 +03001102 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001103 MLX5_SET(qpc, qpc, fre, 1);
1104 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001105
Haggai Eranb11a4f92016-02-29 15:45:03 +02001106 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001107 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +02001108 qp->flags |= MLX5_IB_QP_SQPN_QP1;
1109 }
1110
Guy Levi34f4c952018-11-26 08:15:50 +02001111 mlx5_fill_page_frag_array(&qp->buf,
1112 (__be64 *)MLX5_ADDR_OF(create_qp_in,
1113 *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +03001114
Jack Morgenstein9603b612014-07-28 23:30:22 +03001115 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001116 if (err) {
1117 mlx5_ib_dbg(dev, "err %d\n", err);
1118 goto err_free;
1119 }
1120
Li Dongyangb5883002017-08-16 23:31:22 +10001121 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
1122 sizeof(*qp->sq.wrid), GFP_KERNEL);
1123 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1124 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1125 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1126 sizeof(*qp->rq.wrid), GFP_KERNEL);
1127 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1128 sizeof(*qp->sq.w_list), GFP_KERNEL);
1129 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1130 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001131
1132 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1133 !qp->sq.w_list || !qp->sq.wqe_head) {
1134 err = -ENOMEM;
1135 goto err_wrid;
1136 }
1137 qp->create_type = MLX5_QP_KERNEL;
1138
1139 return 0;
1140
1141err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001142 kvfree(qp->sq.wqe_head);
1143 kvfree(qp->sq.w_list);
1144 kvfree(qp->sq.wrid);
1145 kvfree(qp->sq.wr_data);
1146 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001147 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001148
1149err_free:
Al Viro479163f2014-11-20 08:13:57 +00001150 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001151
1152err_buf:
Guy Levi34f4c952018-11-26 08:15:50 +02001153 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001154 return err;
1155}
1156
1157static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1158{
Li Dongyangb5883002017-08-16 23:31:22 +10001159 kvfree(qp->sq.wqe_head);
1160 kvfree(qp->sq.w_list);
1161 kvfree(qp->sq.wrid);
1162 kvfree(qp->sq.wr_data);
1163 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001164 mlx5_db_free(dev->mdev, &qp->db);
Guy Levi34f4c952018-11-26 08:15:50 +02001165 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001166}
1167
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001168static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001169{
1170 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Moni Shouac32a4f22018-01-02 16:19:32 +02001171 (attr->qp_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001172 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001173 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001174 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001175 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001176 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001177 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001178}
1179
1180static int is_connected(enum ib_qp_type qp_type)
1181{
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001182 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC ||
1183 qp_type == MLX5_IB_QPT_DCI)
Eli Cohene126ba92013-07-07 17:25:49 +03001184 return 1;
1185
1186 return 0;
1187}
1188
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001189static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001190 struct mlx5_ib_qp *qp,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001191 struct mlx5_ib_sq *sq, u32 tdn,
1192 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001193{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001194 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001195 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1196
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001197 MLX5_SET(create_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001198 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001199 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1200 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1201
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001202 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1203}
1204
1205static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001206 struct mlx5_ib_sq *sq, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001207{
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001208 mlx5_cmd_destroy_tis(dev->mdev, sq->tisn, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001209}
1210
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001211static void destroy_flow_rule_vport_sq(struct mlx5_ib_sq *sq)
Mark Blochb96c9dd2018-01-29 10:40:37 +00001212{
1213 if (sq->flow_rule)
1214 mlx5_del_flow_rules(sq->flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001215 sq->flow_rule = NULL;
Mark Blochb96c9dd2018-01-29 10:40:37 +00001216}
1217
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001218static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001219 struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001220 struct mlx5_ib_sq *sq, void *qpin,
1221 struct ib_pd *pd)
1222{
1223 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1224 __be64 *pas;
1225 void *in;
1226 void *sqc;
1227 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1228 void *wq;
1229 int inlen;
1230 int err;
1231 int page_shift = 0;
1232 int npages;
1233 int ncont = 0;
1234 u32 offset = 0;
1235
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001236 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr, ubuffer->buf_size,
1237 &sq->ubuffer.umem, &npages, &page_shift, &ncont,
1238 &offset);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001239 if (err)
1240 return err;
1241
1242 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001243 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001244 if (!in) {
1245 err = -ENOMEM;
1246 goto err_umem;
1247 }
1248
Yishai Hadasc14003f2018-09-20 21:39:22 +03001249 MLX5_SET(create_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001250 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1251 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001252 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1253 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001254 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1255 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1256 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1257 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1258 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001259 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1260 MLX5_CAP_ETH(dev->mdev, swp))
1261 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001262
1263 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1264 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1265 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1266 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1267 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1268 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1269 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1270 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1271 MLX5_SET(wq, wq, page_offset, offset);
1272
1273 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1274 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1275
1276 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1277
1278 kvfree(in);
1279
1280 if (err)
1281 goto err_umem;
1282
1283 return 0;
1284
1285err_umem:
1286 ib_umem_release(sq->ubuffer.umem);
1287 sq->ubuffer.umem = NULL;
1288
1289 return err;
1290}
1291
1292static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1293 struct mlx5_ib_sq *sq)
1294{
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001295 destroy_flow_rule_vport_sq(sq);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001296 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1297 ib_umem_release(sq->ubuffer.umem);
1298}
1299
Boris Pismenny2c292db2018-03-08 15:51:40 +02001300static size_t get_rq_pas_size(void *qpc)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001301{
1302 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1303 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1304 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1305 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1306 u32 po_quanta = 1 << (log_page_size - 6);
1307 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1308 u32 page_size = 1 << log_page_size;
1309 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1310 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1311
1312 return rq_num_pas * sizeof(u64);
1313}
1314
1315static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001316 struct mlx5_ib_rq *rq, void *qpin,
Yishai Hadas34d57582018-09-20 21:39:21 +03001317 size_t qpinlen, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001318{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001319 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001320 __be64 *pas;
1321 __be64 *qp_pas;
1322 void *in;
1323 void *rqc;
1324 void *wq;
1325 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001326 size_t rq_pas_size = get_rq_pas_size(qpc);
1327 size_t inlen;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001328 int err;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001329
1330 if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
1331 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001332
1333 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001334 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001335 if (!in)
1336 return -ENOMEM;
1337
Yishai Hadas34d57582018-09-20 21:39:21 +03001338 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001339 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001340 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1341 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001342 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1343 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1344 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1345 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1346 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1347
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001348 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1349 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1350
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001351 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1352 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001353 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1354 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001355 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1356 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1357 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1358 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1359 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1360 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1361
1362 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1363 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1364 memcpy(pas, qp_pas, rq_pas_size);
1365
1366 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1367
1368 kvfree(in);
1369
1370 return err;
1371}
1372
1373static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1374 struct mlx5_ib_rq *rq)
1375{
1376 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1377}
1378
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001379static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1380{
1381 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1382 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1383 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1384}
1385
Mark Bloch0042f9e2018-09-17 13:30:49 +03001386static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1387 struct mlx5_ib_rq *rq,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001388 u32 qp_flags_en,
1389 struct ib_pd *pd)
Mark Bloch0042f9e2018-09-17 13:30:49 +03001390{
1391 if (qp_flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1392 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1393 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001394 mlx5_cmd_destroy_tir(dev->mdev, rq->tirn, to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001395}
1396
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001397static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001398 struct mlx5_ib_rq *rq, u32 tdn,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001399 u32 *qp_flags_en,
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001400 struct ib_pd *pd,
1401 u32 *out, int outlen)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001402{
Mark Bloch175edba2018-09-17 13:30:48 +03001403 u8 lb_flag = 0;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001404 u32 *in;
1405 void *tirc;
1406 int inlen;
1407 int err;
1408
1409 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001410 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001411 if (!in)
1412 return -ENOMEM;
1413
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001414 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001415 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1416 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1417 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1418 MLX5_SET(tirc, tirc, transport_domain, tdn);
Mark Bloch175edba2018-09-17 13:30:48 +03001419 if (*qp_flags_en & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001420 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001421
Mark Bloch175edba2018-09-17 13:30:48 +03001422 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
1423 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1424
1425 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
1426 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1427
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001428 if (dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001429 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1430 *qp_flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1431 }
1432
1433 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
Mark Blochec9c2fb2018-01-15 13:11:37 +00001434
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001435 err = mlx5_core_create_tir_out(dev->mdev, in, inlen, out, outlen);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001436
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001437 rq->tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001438 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1439 err = mlx5_ib_enable_lb(dev, false, true);
1440
1441 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001442 destroy_raw_packet_qp_tir(dev, rq, 0, pd);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001443 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001444 kvfree(in);
1445
1446 return err;
1447}
1448
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001449static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001450 u32 *in, size_t inlen,
Yishai Hadas7f720522018-09-20 21:45:18 +03001451 struct ib_pd *pd,
1452 struct ib_udata *udata,
1453 struct mlx5_ib_create_qp_resp *resp)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001454{
1455 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1456 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1457 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001458 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1459 udata, struct mlx5_ib_ucontext, ibucontext);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001460 int err;
1461 u32 tdn = mucontext->tdn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001462 u16 uid = to_mpd(pd)->uid;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001463 u32 out[MLX5_ST_SZ_DW(create_tir_out)] = {};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001464
1465 if (qp->sq.wqe_cnt) {
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001466 err = create_raw_packet_qp_tis(dev, qp, sq, tdn, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001467 if (err)
1468 return err;
1469
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001470 err = create_raw_packet_qp_sq(dev, udata, sq, in, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001471 if (err)
1472 goto err_destroy_tis;
1473
Yishai Hadas7f720522018-09-20 21:45:18 +03001474 if (uid) {
1475 resp->tisn = sq->tisn;
1476 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TISN;
1477 resp->sqn = sq->base.mqp.qpn;
1478 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_SQN;
1479 }
1480
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001481 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001482 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001483 }
1484
1485 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001486 rq->base.container_mibqp = qp;
1487
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001488 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1489 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001490 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1491 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
Yishai Hadas34d57582018-09-20 21:39:21 +03001492 err = create_raw_packet_qp_rq(dev, rq, in, inlen, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001493 if (err)
1494 goto err_destroy_sq;
1495
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001496 err = create_raw_packet_qp_tir(
1497 dev, rq, tdn, &qp->flags_en, pd, out,
1498 MLX5_ST_SZ_BYTES(create_tir_out));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001499 if (err)
1500 goto err_destroy_rq;
Yishai Hadas7f720522018-09-20 21:45:18 +03001501
1502 if (uid) {
1503 resp->rqn = rq->base.mqp.qpn;
1504 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_RQN;
1505 resp->tirn = rq->tirn;
1506 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001507 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1508 resp->tir_icm_addr = MLX5_GET(
1509 create_tir_out, out, icm_address_31_0);
1510 resp->tir_icm_addr |=
1511 (u64)MLX5_GET(create_tir_out, out,
1512 icm_address_39_32)
1513 << 32;
1514 resp->tir_icm_addr |=
1515 (u64)MLX5_GET(create_tir_out, out,
1516 icm_address_63_40)
1517 << 40;
1518 resp->comp_mask |=
1519 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1520 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001521 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001522 }
1523
1524 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1525 rq->base.mqp.qpn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001526 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
1527 if (err)
1528 goto err_destroy_tir;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001529
1530 return 0;
1531
Yishai Hadas7f720522018-09-20 21:45:18 +03001532err_destroy_tir:
1533 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001534err_destroy_rq:
1535 destroy_raw_packet_qp_rq(dev, rq);
1536err_destroy_sq:
1537 if (!qp->sq.wqe_cnt)
1538 return err;
1539 destroy_raw_packet_qp_sq(dev, sq);
1540err_destroy_tis:
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001541 destroy_raw_packet_qp_tis(dev, sq, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001542
1543 return err;
1544}
1545
1546static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1547 struct mlx5_ib_qp *qp)
1548{
1549 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1550 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1551 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1552
1553 if (qp->rq.wqe_cnt) {
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001554 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001555 destroy_raw_packet_qp_rq(dev, rq);
1556 }
1557
1558 if (qp->sq.wqe_cnt) {
1559 destroy_raw_packet_qp_sq(dev, sq);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001560 destroy_raw_packet_qp_tis(dev, sq, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001561 }
1562}
1563
1564static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1565 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1566{
1567 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1568 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1569
1570 sq->sq = &qp->sq;
1571 rq->rq = &qp->rq;
1572 sq->doorbell = &qp->db;
1573 rq->doorbell = &qp->db;
1574}
1575
Yishai Hadas28d61372016-05-23 15:20:56 +03001576static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1577{
Mark Bloch0042f9e2018-09-17 13:30:49 +03001578 if (qp->flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1579 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1580 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001581 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1582 to_mpd(qp->ibqp.pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001583}
1584
1585static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1586 struct ib_pd *pd,
1587 struct ib_qp_init_attr *init_attr,
1588 struct ib_udata *udata)
1589{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001590 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1591 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas28d61372016-05-23 15:20:56 +03001592 struct mlx5_ib_create_qp_resp resp = {};
1593 int inlen;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001594 int outlen;
Yishai Hadas28d61372016-05-23 15:20:56 +03001595 int err;
1596 u32 *in;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001597 u32 *out;
Yishai Hadas28d61372016-05-23 15:20:56 +03001598 void *tirc;
1599 void *hfso;
1600 u32 selected_fields = 0;
Matan Barak2d93fc82018-03-28 09:27:55 +03001601 u32 outer_l4;
Yishai Hadas28d61372016-05-23 15:20:56 +03001602 size_t min_resp_len;
1603 u32 tdn = mucontext->tdn;
1604 struct mlx5_ib_create_qp_rss ucmd = {};
1605 size_t required_cmd_sz;
Mark Bloch175edba2018-09-17 13:30:48 +03001606 u8 lb_flag = 0;
Yishai Hadas28d61372016-05-23 15:20:56 +03001607
1608 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1609 return -EOPNOTSUPP;
1610
1611 if (init_attr->create_flags || init_attr->send_cq)
1612 return -EINVAL;
1613
Eli Cohen2f5ff262017-01-03 23:55:21 +02001614 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001615 if (udata->outlen < min_resp_len)
1616 return -EINVAL;
1617
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001618 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001619 if (udata->inlen < required_cmd_sz) {
1620 mlx5_ib_dbg(dev, "invalid inlen\n");
1621 return -EINVAL;
1622 }
1623
1624 if (udata->inlen > sizeof(ucmd) &&
1625 !ib_is_udata_cleared(udata, sizeof(ucmd),
1626 udata->inlen - sizeof(ucmd))) {
1627 mlx5_ib_dbg(dev, "inlen is not supported\n");
1628 return -EOPNOTSUPP;
1629 }
1630
1631 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1632 mlx5_ib_dbg(dev, "copy failed\n");
1633 return -EFAULT;
1634 }
1635
1636 if (ucmd.comp_mask) {
1637 mlx5_ib_dbg(dev, "invalid comp mask\n");
1638 return -EOPNOTSUPP;
1639 }
1640
Mark Bloch175edba2018-09-17 13:30:48 +03001641 if (ucmd.flags & ~(MLX5_QP_FLAG_TUNNEL_OFFLOADS |
1642 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1643 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)) {
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001644 mlx5_ib_dbg(dev, "invalid flags\n");
1645 return -EOPNOTSUPP;
1646 }
1647
1648 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1649 !tunnel_offload_supported(dev->mdev)) {
1650 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001651 return -EOPNOTSUPP;
1652 }
1653
Maor Gottlieb309fa342017-10-19 08:25:56 +03001654 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1655 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1656 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1657 return -EOPNOTSUPP;
1658 }
1659
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001660 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC || dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001661 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1662 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1663 }
1664
1665 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
1666 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1667 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
1668 }
1669
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001670 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
Yishai Hadas28d61372016-05-23 15:20:56 +03001671 if (err) {
1672 mlx5_ib_dbg(dev, "copy failed\n");
1673 return -EINVAL;
1674 }
1675
1676 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001677 outlen = MLX5_ST_SZ_BYTES(create_tir_out);
1678 in = kvzalloc(inlen + outlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001679 if (!in)
1680 return -ENOMEM;
1681
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001682 out = in + MLX5_ST_SZ_DW(create_tir_in);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001683 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001684 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1685 MLX5_SET(tirc, tirc, disp_type,
1686 MLX5_TIRC_DISP_TYPE_INDIRECT);
1687 MLX5_SET(tirc, tirc, indirect_table,
1688 init_attr->rwq_ind_tbl->ind_tbl_num);
1689 MLX5_SET(tirc, tirc, transport_domain, tdn);
1690
1691 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001692
1693 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1694 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1695
Mark Bloch175edba2018-09-17 13:30:48 +03001696 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
1697
Maor Gottlieb309fa342017-10-19 08:25:56 +03001698 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1699 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1700 else
1701 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1702
Yishai Hadas28d61372016-05-23 15:20:56 +03001703 switch (ucmd.rx_hash_function) {
1704 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1705 {
1706 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1707 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1708
1709 if (len != ucmd.rx_key_len) {
1710 err = -EINVAL;
1711 goto err;
1712 }
1713
1714 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1715 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1716 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 &
2143 mlx5_ib_create_qp_sqpn_qp1()) {
2144 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
Eli Cohene126ba92013-07-07 17:25:49 +03003383static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
3384 const struct ib_qp_attr *attr, int attr_mask,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003385 enum ib_qp_state cur_state,
3386 enum ib_qp_state new_state,
3387 const struct mlx5_ib_modify_qp *ucmd,
3388 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003389{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003390 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
3391 [MLX5_QP_STATE_RST] = {
3392 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3393 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3394 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
3395 },
3396 [MLX5_QP_STATE_INIT] = {
3397 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3398 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3399 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
3400 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
3401 },
3402 [MLX5_QP_STATE_RTR] = {
3403 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3404 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3405 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
3406 },
3407 [MLX5_QP_STATE_RTS] = {
3408 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3409 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3410 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
3411 },
3412 [MLX5_QP_STATE_SQD] = {
3413 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3414 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3415 },
3416 [MLX5_QP_STATE_SQER] = {
3417 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3418 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3419 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
3420 },
3421 [MLX5_QP_STATE_ERR] = {
3422 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3423 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3424 }
3425 };
3426
Eli Cohene126ba92013-07-07 17:25:49 +03003427 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3428 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02003429 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03003430 struct mlx5_ib_cq *send_cq, *recv_cq;
3431 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03003432 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03003433 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003434 enum mlx5_qp_state mlx5_cur, mlx5_new;
3435 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03003436 int mlx5_st;
3437 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003438 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03003439 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003440
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003441 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
3442 qp->qp_sub_type : ibqp->qp_type);
3443 if (mlx5_st < 0)
3444 return -EINVAL;
3445
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003446 context = kzalloc(sizeof(*context), GFP_KERNEL);
3447 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03003448 return -ENOMEM;
3449
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003450 pd = get_pd(qp);
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003451 context->flags = cpu_to_be32(mlx5_st << 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003452
3453 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
3454 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3455 } else {
3456 switch (attr->path_mig_state) {
3457 case IB_MIG_MIGRATED:
3458 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3459 break;
3460 case IB_MIG_REARM:
3461 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
3462 break;
3463 case IB_MIG_ARMED:
3464 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
3465 break;
3466 }
3467 }
3468
Aviv Heller13eab212016-09-18 20:48:04 +03003469 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
3470 if ((ibqp->qp_type == IB_QPT_RC) ||
3471 (ibqp->qp_type == IB_QPT_UD &&
3472 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
3473 (ibqp->qp_type == IB_QPT_UC) ||
3474 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
3475 (ibqp->qp_type == IB_QPT_XRC_INI) ||
3476 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
Aviv Heller7c34ec12018-08-23 13:47:53 +03003477 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02003478 u8 p = mlx5_core_native_port_num(dev->mdev) - 1;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003479 tx_affinity = get_tx_affinity(dev, pd, base, p,
3480 udata);
Aviv Heller13eab212016-09-18 20:48:04 +03003481 context->flags |= cpu_to_be32(tx_affinity << 24);
3482 }
3483 }
3484 }
3485
Haggai Erand16e91d2016-02-29 15:45:05 +02003486 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003487 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003488 } else if ((ibqp->qp_type == IB_QPT_UD &&
3489 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003490 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
3491 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
3492 } else if (attr_mask & IB_QP_PATH_MTU) {
3493 if (attr->path_mtu < IB_MTU_256 ||
3494 attr->path_mtu > IB_MTU_4096) {
3495 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3496 err = -EINVAL;
3497 goto out;
3498 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003499 context->mtu_msgmax = (attr->path_mtu << 5) |
3500 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03003501 }
3502
3503 if (attr_mask & IB_QP_DEST_QPN)
3504 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
3505
3506 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003507 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003508
3509 /* todo implement counter_index functionality */
3510
3511 if (is_sqp(ibqp->qp_type))
3512 context->pri_path.port = qp->port;
3513
3514 if (attr_mask & IB_QP_PORT)
3515 context->pri_path.port = attr->port_num;
3516
3517 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003518 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003519 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003520 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003521 if (err)
3522 goto out;
3523 }
3524
3525 if (attr_mask & IB_QP_TIMEOUT)
3526 context->pri_path.ackto_lt |= attr->timeout << 3;
3527
3528 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003529 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3530 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003531 attr->alt_port_num,
3532 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3533 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003534 if (err)
3535 goto out;
3536 }
3537
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003538 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3539 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003540
3541 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3542 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3543 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3544 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3545
3546 if (attr_mask & IB_QP_RNR_RETRY)
3547 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3548
3549 if (attr_mask & IB_QP_RETRY_CNT)
3550 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3551
3552 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3553 if (attr->max_rd_atomic)
3554 context->params1 |=
3555 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3556 }
3557
3558 if (attr_mask & IB_QP_SQ_PSN)
3559 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3560
3561 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3562 if (attr->max_dest_rd_atomic)
3563 context->params2 |=
3564 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3565 }
3566
Yonatan Cohena60109d2018-10-10 09:25:16 +03003567 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08003568 __be32 access_flags;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003569
3570 err = to_mlx5_access_flags(qp, attr, attr_mask, &access_flags);
3571 if (err)
3572 goto out;
3573
3574 context->params2 |= access_flags;
3575 }
Eli Cohene126ba92013-07-07 17:25:49 +03003576
3577 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3578 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3579
3580 if (attr_mask & IB_QP_RQ_PSN)
3581 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3582
3583 if (attr_mask & IB_QP_QKEY)
3584 context->qkey = cpu_to_be32(attr->qkey);
3585
3586 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3587 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3588
Mark Bloch0837e862016-06-17 15:10:55 +03003589 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3590 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3591 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003592
3593 /* Underlay port should be used - index 0 function per port */
3594 if (qp->flags & MLX5_IB_QP_UNDERLAY)
3595 port_num = 0;
3596
Alex Veskereb49ab02016-08-28 12:25:53 +03003597 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03003598 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03003599 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003600 }
3601
Eli Cohene126ba92013-07-07 17:25:49 +03003602 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3603 context->sq_crq_size |= cpu_to_be16(1 << 4);
3604
Haggai Eranb11a4f92016-02-29 15:45:03 +02003605 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
3606 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003607
3608 mlx5_cur = to_mlx5_state(cur_state);
3609 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003610
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003611 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003612 !optab[mlx5_cur][mlx5_new]) {
3613 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003614 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003615 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003616
3617 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003618 optpar = ib_mask_to_mlx5_opt(attr_mask);
3619 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003620
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003621 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3622 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003623 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3624
3625 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003626 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03003627 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003628 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3629 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003630
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003631 if (attr_mask & IB_QP_PORT)
3632 raw_qp_param.port = attr->port_num;
3633
Bodong Wang7d29f342016-12-01 13:43:16 +02003634 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003635 raw_qp_param.rl.rate = attr->rate_limit;
3636
3637 if (ucmd->burst_info.max_burst_sz) {
3638 if (attr->rate_limit &&
3639 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3640 raw_qp_param.rl.max_burst_sz =
3641 ucmd->burst_info.max_burst_sz;
3642 } else {
3643 err = -EINVAL;
3644 goto out;
3645 }
3646 }
3647
3648 if (ucmd->burst_info.typical_pkt_sz) {
3649 if (attr->rate_limit &&
3650 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3651 raw_qp_param.rl.typical_pkt_sz =
3652 ucmd->burst_info.typical_pkt_sz;
3653 } else {
3654 err = -EINVAL;
3655 goto out;
3656 }
3657 }
3658
Bodong Wang7d29f342016-12-01 13:43:16 +02003659 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3660 }
3661
Aviv Heller13eab212016-09-18 20:48:04 +03003662 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003663 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003664 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003665 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003666 }
3667
Eli Cohene126ba92013-07-07 17:25:49 +03003668 if (err)
3669 goto out;
3670
3671 qp->state = new_state;
3672
3673 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003674 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003675 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003676 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003677 if (attr_mask & IB_QP_PORT)
3678 qp->port = attr->port_num;
3679 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003680 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003681
3682 /*
3683 * If we moved a kernel QP to RESET, clean up all old CQ
3684 * entries and reinitialize the QP.
3685 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02003686 if (new_state == IB_QPS_RESET &&
3687 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003688 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003689 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3690 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003691 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003692
3693 qp->rq.head = 0;
3694 qp->rq.tail = 0;
3695 qp->sq.head = 0;
3696 qp->sq.tail = 0;
3697 qp->sq.cur_post = 0;
Guy Levi34f4c952018-11-26 08:15:50 +02003698 if (qp->sq.wqe_cnt)
3699 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003700 qp->db.db[MLX5_RCV_DBR] = 0;
3701 qp->db.db[MLX5_SND_DBR] = 0;
3702 }
3703
3704out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003705 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003706 return err;
3707}
3708
Moni Shouac32a4f22018-01-02 16:19:32 +02003709static inline bool is_valid_mask(int mask, int req, int opt)
3710{
3711 if ((mask & req) != req)
3712 return false;
3713
3714 if (mask & ~(req | opt))
3715 return false;
3716
3717 return true;
3718}
3719
3720/* check valid transition for driver QP types
3721 * for now the only QP type that this function supports is DCI
3722 */
3723static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3724 enum ib_qp_attr_mask attr_mask)
3725{
3726 int req = IB_QP_STATE;
3727 int opt = 0;
3728
Moni Shoua99ed7482018-09-12 09:33:55 +03003729 if (new_state == IB_QPS_RESET) {
3730 return is_valid_mask(attr_mask, req, opt);
3731 } else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Moni Shouac32a4f22018-01-02 16:19:32 +02003732 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
3733 return is_valid_mask(attr_mask, req, opt);
3734 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3735 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3736 return is_valid_mask(attr_mask, req, opt);
3737 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3738 req |= IB_QP_PATH_MTU;
Artemy Kovalyov5ec03042018-11-05 08:12:07 +02003739 opt = IB_QP_PKEY_INDEX | IB_QP_AV;
Moni Shouac32a4f22018-01-02 16:19:32 +02003740 return is_valid_mask(attr_mask, req, opt);
3741 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3742 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3743 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3744 opt = IB_QP_MIN_RNR_TIMER;
3745 return is_valid_mask(attr_mask, req, opt);
3746 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3747 opt = IB_QP_MIN_RNR_TIMER;
3748 return is_valid_mask(attr_mask, req, opt);
3749 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3750 return is_valid_mask(attr_mask, req, opt);
3751 }
3752 return false;
3753}
3754
Moni Shoua776a3902018-01-02 16:19:33 +02003755/* mlx5_ib_modify_dct: modify a DCT QP
3756 * valid transitions are:
3757 * RESET to INIT: must set access_flags, pkey_index and port
3758 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3759 * mtu, gid_index and hop_limit
3760 * Other transitions and attributes are illegal
3761 */
3762static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3763 int attr_mask, struct ib_udata *udata)
3764{
3765 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3766 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3767 enum ib_qp_state cur_state, new_state;
3768 int err = 0;
3769 int required = IB_QP_STATE;
3770 void *dctc;
3771
3772 if (!(attr_mask & IB_QP_STATE))
3773 return -EINVAL;
3774
3775 cur_state = qp->state;
3776 new_state = attr->qp_state;
3777
3778 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3779 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3780 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3781 if (!is_valid_mask(attr_mask, required, 0))
3782 return -EINVAL;
3783
3784 if (attr->port_num == 0 ||
3785 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3786 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3787 attr->port_num, dev->num_ports);
3788 return -EINVAL;
3789 }
3790 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3791 MLX5_SET(dctc, dctc, rre, 1);
3792 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3793 MLX5_SET(dctc, dctc, rwe, 1);
3794 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03003795 int atomic_mode;
3796
3797 atomic_mode = get_atomic_mode(dev, MLX5_IB_QPT_DCT);
3798 if (atomic_mode < 0)
Moni Shoua776a3902018-01-02 16:19:33 +02003799 return -EOPNOTSUPP;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003800
3801 MLX5_SET(dctc, dctc, atomic_mode, atomic_mode);
Moni Shoua776a3902018-01-02 16:19:33 +02003802 MLX5_SET(dctc, dctc, rae, 1);
Moni Shoua776a3902018-01-02 16:19:33 +02003803 }
3804 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3805 MLX5_SET(dctc, dctc, port, attr->port_num);
3806 MLX5_SET(dctc, dctc, counter_set_id, dev->port[attr->port_num - 1].cnts.set_id);
3807
3808 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3809 struct mlx5_ib_modify_qp_resp resp = {};
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003810 u32 out[MLX5_ST_SZ_DW(create_dct_out)] = {0};
Moni Shoua776a3902018-01-02 16:19:33 +02003811 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3812 sizeof(resp.dctn);
3813
3814 if (udata->outlen < min_resp_len)
3815 return -EINVAL;
3816 resp.response_length = min_resp_len;
3817
3818 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3819 if (!is_valid_mask(attr_mask, required, 0))
3820 return -EINVAL;
3821 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3822 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3823 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3824 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3825 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3826 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3827
3828 err = mlx5_core_create_dct(dev->mdev, &qp->dct.mdct, qp->dct.in,
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003829 MLX5_ST_SZ_BYTES(create_dct_in), out,
3830 sizeof(out));
Moni Shoua776a3902018-01-02 16:19:33 +02003831 if (err)
3832 return err;
3833 resp.dctn = qp->dct.mdct.mqp.qpn;
3834 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3835 if (err) {
3836 mlx5_core_destroy_dct(dev->mdev, &qp->dct.mdct);
3837 return err;
3838 }
3839 } else {
3840 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3841 return -EINVAL;
3842 }
3843 if (err)
3844 qp->state = IB_QPS_ERR;
3845 else
3846 qp->state = new_state;
3847 return err;
3848}
3849
Eli Cohene126ba92013-07-07 17:25:49 +03003850int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3851 int attr_mask, struct ib_udata *udata)
3852{
3853 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3854 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02003855 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02003856 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003857 enum ib_qp_state cur_state, new_state;
Bodong Wang61147f32018-03-19 15:10:30 +02003858 size_t required_cmd_sz;
Eli Cohene126ba92013-07-07 17:25:49 +03003859 int err = -EINVAL;
3860 int port;
3861
Yishai Hadas28d61372016-05-23 15:20:56 +03003862 if (ibqp->rwq_ind_tbl)
3863 return -ENOSYS;
3864
Bodong Wang61147f32018-03-19 15:10:30 +02003865 if (udata && udata->inlen) {
3866 required_cmd_sz = offsetof(typeof(ucmd), reserved) +
3867 sizeof(ucmd.reserved);
3868 if (udata->inlen < required_cmd_sz)
3869 return -EINVAL;
3870
3871 if (udata->inlen > sizeof(ucmd) &&
3872 !ib_is_udata_cleared(udata, sizeof(ucmd),
3873 udata->inlen - sizeof(ucmd)))
3874 return -EOPNOTSUPP;
3875
3876 if (ib_copy_from_udata(&ucmd, udata,
3877 min(udata->inlen, sizeof(ucmd))))
3878 return -EFAULT;
3879
3880 if (ucmd.comp_mask ||
3881 memchr_inv(&ucmd.reserved, 0, sizeof(ucmd.reserved)) ||
3882 memchr_inv(&ucmd.burst_info.reserved, 0,
3883 sizeof(ucmd.burst_info.reserved)))
3884 return -EOPNOTSUPP;
3885 }
3886
Haggai Erand16e91d2016-02-29 15:45:05 +02003887 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3888 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3889
Moni Shouac32a4f22018-01-02 16:19:32 +02003890 if (ibqp->qp_type == IB_QPT_DRIVER)
3891 qp_type = qp->qp_sub_type;
3892 else
3893 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3894 IB_QPT_GSI : ibqp->qp_type;
3895
Moni Shoua776a3902018-01-02 16:19:33 +02003896 if (qp_type == MLX5_IB_QPT_DCT)
3897 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02003898
Eli Cohene126ba92013-07-07 17:25:49 +03003899 mutex_lock(&qp->mutex);
3900
3901 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
3902 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
3903
Achiad Shochat2811ba52015-12-23 18:47:24 +02003904 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
3905 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003906 }
3907
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003908 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
3909 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
3910 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
3911 attr_mask);
3912 goto out;
3913 }
3914 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02003915 qp_type != MLX5_IB_QPT_DCI &&
Kamal Heibd31131b2018-10-02 16:11:21 +03003916 !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
3917 attr_mask)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003918 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3919 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003920 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02003921 } else if (qp_type == MLX5_IB_QPT_DCI &&
3922 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
3923 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3924 cur_state, new_state, qp_type, attr_mask);
3925 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003926 }
Eli Cohene126ba92013-07-07 17:25:49 +03003927
3928 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003929 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003930 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003931 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3932 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003933 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003934 }
Eli Cohene126ba92013-07-07 17:25:49 +03003935
3936 if (attr_mask & IB_QP_PKEY_INDEX) {
3937 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003938 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02003939 dev->mdev->port_caps[port - 1].pkey_table_len) {
3940 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
3941 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003942 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003943 }
Eli Cohene126ba92013-07-07 17:25:49 +03003944 }
3945
3946 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003947 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003948 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
3949 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
3950 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003951 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003952 }
Eli Cohene126ba92013-07-07 17:25:49 +03003953
3954 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003955 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003956 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
3957 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
3958 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003959 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003960 }
Eli Cohene126ba92013-07-07 17:25:49 +03003961
3962 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
3963 err = 0;
3964 goto out;
3965 }
3966
Bodong Wang61147f32018-03-19 15:10:30 +02003967 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003968 new_state, &ucmd, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03003969
3970out:
3971 mutex_unlock(&qp->mutex);
3972 return err;
3973}
3974
Guy Levi34f4c952018-11-26 08:15:50 +02003975static void _handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
3976 u32 wqe_sz, void **cur_edge)
3977{
3978 u32 idx;
3979
3980 idx = (sq->cur_post + (wqe_sz >> 2)) & (sq->wqe_cnt - 1);
3981 *cur_edge = get_sq_edge(sq, idx);
3982
3983 *seg = mlx5_frag_buf_get_wqe(&sq->fbc, idx);
3984}
3985
3986/* handle_post_send_edge - Check if we get to SQ edge. If yes, update to the
3987 * next nearby edge and get new address translation for current WQE position.
3988 * @sq - SQ buffer.
3989 * @seg: Current WQE position (16B aligned).
3990 * @wqe_sz: Total current WQE size [16B].
3991 * @cur_edge: Updated current edge.
3992 */
3993static inline void handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
3994 u32 wqe_sz, void **cur_edge)
3995{
3996 if (likely(*seg != *cur_edge))
3997 return;
3998
3999 _handle_post_send_edge(sq, seg, wqe_sz, cur_edge);
4000}
4001
4002/* memcpy_send_wqe - copy data from src to WQE and update the relevant WQ's
4003 * pointers. At the end @seg is aligned to 16B regardless the copied size.
4004 * @sq - SQ buffer.
4005 * @cur_edge: Updated current edge.
4006 * @seg: Current WQE position (16B aligned).
4007 * @wqe_sz: Total current WQE size [16B].
4008 * @src: Pointer to copy from.
4009 * @n: Number of bytes to copy.
4010 */
4011static inline void memcpy_send_wqe(struct mlx5_ib_wq *sq, void **cur_edge,
4012 void **seg, u32 *wqe_sz, const void *src,
4013 size_t n)
4014{
4015 while (likely(n)) {
4016 size_t leftlen = *cur_edge - *seg;
4017 size_t copysz = min_t(size_t, leftlen, n);
4018 size_t stride;
4019
4020 memcpy(*seg, src, copysz);
4021
4022 n -= copysz;
4023 src += copysz;
4024 stride = !n ? ALIGN(copysz, 16) : copysz;
4025 *seg += stride;
4026 *wqe_sz += stride >> 4;
4027 handle_post_send_edge(sq, seg, *wqe_sz, cur_edge);
4028 }
4029}
4030
Eli Cohene126ba92013-07-07 17:25:49 +03004031static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
4032{
4033 struct mlx5_ib_cq *cq;
4034 unsigned cur;
4035
4036 cur = wq->head - wq->tail;
4037 if (likely(cur + nreq < wq->max_post))
4038 return 0;
4039
4040 cq = to_mcq(ib_cq);
4041 spin_lock(&cq->lock);
4042 cur = wq->head - wq->tail;
4043 spin_unlock(&cq->lock);
4044
4045 return cur + nreq >= wq->max_post;
4046}
4047
4048static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
4049 u64 remote_addr, u32 rkey)
4050{
4051 rseg->raddr = cpu_to_be64(remote_addr);
4052 rseg->rkey = cpu_to_be32(rkey);
4053 rseg->reserved = 0;
4054}
4055
Guy Levi34f4c952018-11-26 08:15:50 +02004056static void set_eth_seg(const struct ib_send_wr *wr, struct mlx5_ib_qp *qp,
4057 void **seg, int *size, void **cur_edge)
Erez Shitritf0313962016-02-21 16:27:17 +02004058{
Guy Levi34f4c952018-11-26 08:15:50 +02004059 struct mlx5_wqe_eth_seg *eseg = *seg;
Erez Shitritf0313962016-02-21 16:27:17 +02004060
4061 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
4062
4063 if (wr->send_flags & IB_SEND_IP_CSUM)
4064 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
4065 MLX5_ETH_WQE_L4_CSUM;
4066
Erez Shitritf0313962016-02-21 16:27:17 +02004067 if (wr->opcode == IB_WR_LSO) {
4068 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Guy Levi34f4c952018-11-26 08:15:50 +02004069 size_t left, copysz;
Erez Shitritf0313962016-02-21 16:27:17 +02004070 void *pdata = ud_wr->header;
Guy Levi34f4c952018-11-26 08:15:50 +02004071 size_t stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004072
4073 left = ud_wr->hlen;
4074 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02004075 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02004076
Guy Levi34f4c952018-11-26 08:15:50 +02004077 /* memcpy_send_wqe should get a 16B align address. Hence, we
4078 * first copy up to the current edge and then, if needed,
4079 * fall-through to memcpy_send_wqe.
Erez Shitritf0313962016-02-21 16:27:17 +02004080 */
Guy Levi34f4c952018-11-26 08:15:50 +02004081 copysz = min_t(u64, *cur_edge - (void *)eseg->inline_hdr.start,
4082 left);
4083 memcpy(eseg->inline_hdr.start, pdata, copysz);
4084 stride = ALIGN(sizeof(struct mlx5_wqe_eth_seg) -
4085 sizeof(eseg->inline_hdr.start) + copysz, 16);
4086 *size += stride / 16;
4087 *seg += stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004088
Guy Levi34f4c952018-11-26 08:15:50 +02004089 if (copysz < left) {
4090 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02004091 left -= copysz;
4092 pdata += copysz;
Guy Levi34f4c952018-11-26 08:15:50 +02004093 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, pdata,
4094 left);
Erez Shitritf0313962016-02-21 16:27:17 +02004095 }
Guy Levi34f4c952018-11-26 08:15:50 +02004096
4097 return;
Erez Shitritf0313962016-02-21 16:27:17 +02004098 }
4099
Guy Levi34f4c952018-11-26 08:15:50 +02004100 *seg += sizeof(struct mlx5_wqe_eth_seg);
4101 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
Erez Shitritf0313962016-02-21 16:27:17 +02004102}
4103
Eli Cohene126ba92013-07-07 17:25:49 +03004104static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004105 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004106{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004107 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
4108 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
4109 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004110}
4111
4112static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
4113{
4114 dseg->byte_count = cpu_to_be32(sg->length);
4115 dseg->lkey = cpu_to_be32(sg->lkey);
4116 dseg->addr = cpu_to_be64(sg->addr);
4117}
4118
Artemy Kovalyov31616252017-01-02 11:37:42 +02004119static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03004120{
Artemy Kovalyov31616252017-01-02 11:37:42 +02004121 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
4122 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03004123}
4124
4125static __be64 frwr_mkey_mask(void)
4126{
4127 u64 result;
4128
4129 result = MLX5_MKEY_MASK_LEN |
4130 MLX5_MKEY_MASK_PAGE_SIZE |
4131 MLX5_MKEY_MASK_START_ADDR |
4132 MLX5_MKEY_MASK_EN_RINVAL |
4133 MLX5_MKEY_MASK_KEY |
4134 MLX5_MKEY_MASK_LR |
4135 MLX5_MKEY_MASK_LW |
4136 MLX5_MKEY_MASK_RR |
4137 MLX5_MKEY_MASK_RW |
4138 MLX5_MKEY_MASK_A |
4139 MLX5_MKEY_MASK_SMALL_FENCE |
4140 MLX5_MKEY_MASK_FREE;
4141
4142 return cpu_to_be64(result);
4143}
4144
Sagi Grimberge6631812014-02-23 14:19:11 +02004145static __be64 sig_mkey_mask(void)
4146{
4147 u64 result;
4148
4149 result = MLX5_MKEY_MASK_LEN |
4150 MLX5_MKEY_MASK_PAGE_SIZE |
4151 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004152 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02004153 MLX5_MKEY_MASK_EN_RINVAL |
4154 MLX5_MKEY_MASK_KEY |
4155 MLX5_MKEY_MASK_LR |
4156 MLX5_MKEY_MASK_LW |
4157 MLX5_MKEY_MASK_RR |
4158 MLX5_MKEY_MASK_RW |
4159 MLX5_MKEY_MASK_SMALL_FENCE |
4160 MLX5_MKEY_MASK_FREE |
4161 MLX5_MKEY_MASK_BSF_EN;
4162
4163 return cpu_to_be64(result);
4164}
4165
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004166static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004167 struct mlx5_ib_mr *mr, u8 flags)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004168{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004169 int size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004170
4171 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004172
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004173 umr->flags = flags;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004174 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004175 umr->mkey_mask = frwr_mkey_mask();
4176}
4177
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004178static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03004179{
4180 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004181 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03004182 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03004183}
4184
Artemy Kovalyov31616252017-01-02 11:37:42 +02004185static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004186{
4187 u64 result;
4188
Artemy Kovalyov31616252017-01-02 11:37:42 +02004189 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02004190 MLX5_MKEY_MASK_FREE;
4191
4192 return cpu_to_be64(result);
4193}
4194
Artemy Kovalyov31616252017-01-02 11:37:42 +02004195static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004196{
4197 u64 result;
4198
4199 result = MLX5_MKEY_MASK_FREE;
4200
4201 return cpu_to_be64(result);
4202}
4203
Noa Osherovich56e11d62016-02-29 16:46:51 +02004204static __be64 get_umr_update_translation_mask(void)
4205{
4206 u64 result;
4207
4208 result = MLX5_MKEY_MASK_LEN |
4209 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004210 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004211
4212 return cpu_to_be64(result);
4213}
4214
Artemy Kovalyov31616252017-01-02 11:37:42 +02004215static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02004216{
4217 u64 result;
4218
Artemy Kovalyov31616252017-01-02 11:37:42 +02004219 result = MLX5_MKEY_MASK_LR |
4220 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004221 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004222 MLX5_MKEY_MASK_RW;
4223
4224 if (atomic)
4225 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004226
4227 return cpu_to_be64(result);
4228}
4229
4230static __be64 get_umr_update_pd_mask(void)
4231{
4232 u64 result;
4233
Artemy Kovalyov31616252017-01-02 11:37:42 +02004234 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004235
4236 return cpu_to_be64(result);
4237}
4238
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004239static int umr_check_mkey_mask(struct mlx5_ib_dev *dev, u64 mask)
4240{
4241 if ((mask & MLX5_MKEY_MASK_PAGE_SIZE &&
4242 MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) ||
4243 (mask & MLX5_MKEY_MASK_A &&
4244 MLX5_CAP_GEN(dev->mdev, umr_modify_atomic_disabled)))
4245 return -EPERM;
4246 return 0;
4247}
4248
4249static int set_reg_umr_segment(struct mlx5_ib_dev *dev,
4250 struct mlx5_wqe_umr_ctrl_seg *umr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004251 const struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004252{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004253 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03004254
4255 memset(umr, 0, sizeof(*umr));
4256
Haggai Eran968e78d2014-12-11 17:04:11 +02004257 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
4258 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
4259 else
4260 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
4261
Artemy Kovalyov31616252017-01-02 11:37:42 +02004262 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
4263 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
4264 u64 offset = get_xlt_octo(umrwr->offset);
4265
4266 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
4267 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
4268 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004269 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02004270 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
4271 umr->mkey_mask |= get_umr_update_translation_mask();
4272 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
4273 umr->mkey_mask |= get_umr_update_access_mask(atomic);
4274 umr->mkey_mask |= get_umr_update_pd_mask();
4275 }
4276 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
4277 umr->mkey_mask |= get_umr_enable_mr_mask();
4278 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
4279 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03004280
4281 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02004282 umr->flags |= MLX5_UMR_INLINE;
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004283
4284 return umr_check_mkey_mask(dev, be64_to_cpu(umr->mkey_mask));
Eli Cohene126ba92013-07-07 17:25:49 +03004285}
4286
4287static u8 get_umr_flags(int acc)
4288{
4289 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
4290 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
4291 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
4292 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02004293 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004294}
4295
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004296static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
4297 struct mlx5_ib_mr *mr,
4298 u32 key, int access)
4299{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004300 int ndescs = ALIGN(mr->ndescs + mr->meta_ndescs, 8) >> 1;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004301
4302 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004303
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004304 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004305 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004306 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004307 /* KLMs take twice the size of MTTs */
4308 ndescs *= 2;
4309
4310 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004311 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
4312 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
4313 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
4314 seg->len = cpu_to_be64(mr->ibmr.length);
4315 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004316}
4317
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004318static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03004319{
4320 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004321 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004322}
4323
Bart Van Asschef696bf62018-07-18 09:25:14 -07004324static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg,
4325 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004326{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004327 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004328
Eli Cohene126ba92013-07-07 17:25:49 +03004329 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02004330 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02004331 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004332
Haggai Eran968e78d2014-12-11 17:04:11 +02004333 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004334 if (umrwr->pd)
4335 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
4336 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
4337 !umrwr->length)
4338 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
4339
4340 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004341 seg->len = cpu_to_be64(umrwr->length);
4342 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03004343 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02004344 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03004345}
4346
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004347static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
4348 struct mlx5_ib_mr *mr,
4349 struct mlx5_ib_pd *pd)
4350{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004351 int bcount = mr->desc_size * (mr->ndescs + mr->meta_ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004352
4353 dseg->addr = cpu_to_be64(mr->desc_map);
4354 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
4355 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
4356}
4357
Bart Van Asschef696bf62018-07-18 09:25:14 -07004358static __be32 send_ieth(const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004359{
4360 switch (wr->opcode) {
4361 case IB_WR_SEND_WITH_IMM:
4362 case IB_WR_RDMA_WRITE_WITH_IMM:
4363 return wr->ex.imm_data;
4364
4365 case IB_WR_SEND_WITH_INV:
4366 return cpu_to_be32(wr->ex.invalidate_rkey);
4367
4368 default:
4369 return 0;
4370 }
4371}
4372
4373static u8 calc_sig(void *wqe, int size)
4374{
4375 u8 *p = wqe;
4376 u8 res = 0;
4377 int i;
4378
4379 for (i = 0; i < size; i++)
4380 res ^= p[i];
4381
4382 return ~res;
4383}
4384
4385static u8 wq_sig(void *wqe)
4386{
4387 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
4388}
4389
Bart Van Asschef696bf62018-07-18 09:25:14 -07004390static int set_data_inl_seg(struct mlx5_ib_qp *qp, const struct ib_send_wr *wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004391 void **wqe, int *wqe_sz, void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004392{
4393 struct mlx5_wqe_inline_seg *seg;
Guy Levi34f4c952018-11-26 08:15:50 +02004394 size_t offset;
Eli Cohene126ba92013-07-07 17:25:49 +03004395 int inl = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004396 int i;
4397
Guy Levi34f4c952018-11-26 08:15:50 +02004398 seg = *wqe;
4399 *wqe += sizeof(*seg);
4400 offset = sizeof(*seg);
4401
Eli Cohene126ba92013-07-07 17:25:49 +03004402 for (i = 0; i < wr->num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02004403 size_t len = wr->sg_list[i].length;
4404 void *addr = (void *)(unsigned long)(wr->sg_list[i].addr);
4405
Eli Cohene126ba92013-07-07 17:25:49 +03004406 inl += len;
4407
4408 if (unlikely(inl > qp->max_inline_data))
4409 return -ENOMEM;
4410
Guy Levi34f4c952018-11-26 08:15:50 +02004411 while (likely(len)) {
4412 size_t leftlen;
4413 size_t copysz;
4414
4415 handle_post_send_edge(&qp->sq, wqe,
4416 *wqe_sz + (offset >> 4),
4417 cur_edge);
4418
4419 leftlen = *cur_edge - *wqe;
4420 copysz = min_t(size_t, leftlen, len);
4421
4422 memcpy(*wqe, addr, copysz);
4423 len -= copysz;
4424 addr += copysz;
4425 *wqe += copysz;
4426 offset += copysz;
Eli Cohene126ba92013-07-07 17:25:49 +03004427 }
Eli Cohene126ba92013-07-07 17:25:49 +03004428 }
4429
4430 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
4431
Guy Levi34f4c952018-11-26 08:15:50 +02004432 *wqe_sz += ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004433
4434 return 0;
4435}
4436
Sagi Grimberge6631812014-02-23 14:19:11 +02004437static u16 prot_field_size(enum ib_signature_type type)
4438{
4439 switch (type) {
4440 case IB_SIG_TYPE_T10_DIF:
4441 return MLX5_DIF_SIZE;
4442 default:
4443 return 0;
4444 }
4445}
4446
4447static u8 bs_selector(int block_size)
4448{
4449 switch (block_size) {
4450 case 512: return 0x1;
4451 case 520: return 0x2;
4452 case 4096: return 0x3;
4453 case 4160: return 0x4;
4454 case 1073741824: return 0x5;
4455 default: return 0;
4456 }
4457}
4458
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004459static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
4460 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02004461{
Sagi Grimberg142537f2014-08-13 19:54:32 +03004462 /* Valid inline section and allow BSF refresh */
4463 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
4464 MLX5_BSF_REFRESH_DIF);
4465 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
4466 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004467 /* repeating block */
4468 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
4469 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
4470 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004471
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004472 if (domain->sig.dif.ref_remap)
4473 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02004474
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004475 if (domain->sig.dif.app_escape) {
4476 if (domain->sig.dif.ref_escape)
4477 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
4478 else
4479 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02004480 }
4481
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004482 inl->dif_app_bitmask_check =
4483 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02004484}
4485
4486static int mlx5_set_bsf(struct ib_mr *sig_mr,
4487 struct ib_sig_attrs *sig_attrs,
4488 struct mlx5_bsf *bsf, u32 data_size)
4489{
4490 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
4491 struct mlx5_bsf_basic *basic = &bsf->basic;
4492 struct ib_sig_domain *mem = &sig_attrs->mem;
4493 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02004494
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004495 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02004496
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004497 /* Basic + Extended + Inline */
4498 basic->bsf_size_sbs = 1 << 7;
4499 /* Input domain check byte mask */
4500 basic->check_byte_mask = sig_attrs->check_mask;
4501 basic->raw_data_size = cpu_to_be32(data_size);
4502
4503 /* Memory domain */
4504 switch (sig_attrs->mem.sig_type) {
4505 case IB_SIG_TYPE_NONE:
4506 break;
4507 case IB_SIG_TYPE_T10_DIF:
4508 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
4509 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
4510 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
4511 break;
4512 default:
4513 return -EINVAL;
4514 }
4515
4516 /* Wire domain */
4517 switch (sig_attrs->wire.sig_type) {
4518 case IB_SIG_TYPE_NONE:
4519 break;
4520 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02004521 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004522 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004523 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03004524 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02004525 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004526 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004527 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004528 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004529 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004530 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02004531 } else
4532 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
4533
Sagi Grimberg142537f2014-08-13 19:54:32 +03004534 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004535 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02004536 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004537 default:
4538 return -EINVAL;
4539 }
4540
4541 return 0;
4542}
4543
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004544static int set_sig_data_segment(const struct ib_send_wr *send_wr,
4545 struct ib_mr *sig_mr,
4546 struct ib_sig_attrs *sig_attrs,
4547 struct mlx5_ib_qp *qp, void **seg, int *size,
4548 void **cur_edge)
Sagi Grimberge6631812014-02-23 14:19:11 +02004549{
Sagi Grimberge6631812014-02-23 14:19:11 +02004550 struct mlx5_bsf *bsf;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004551 u32 data_len;
4552 u32 data_key;
4553 u64 data_va;
4554 u32 prot_len = 0;
4555 u32 prot_key = 0;
4556 u64 prot_va = 0;
4557 bool prot = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004558 int ret;
4559 int wqe_size;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004560 struct mlx5_ib_mr *mr = to_mmr(sig_mr);
4561 struct mlx5_ib_mr *pi_mr = mr->pi_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004562
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004563 data_len = pi_mr->data_length;
4564 data_key = pi_mr->ibmr.lkey;
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03004565 data_va = pi_mr->data_iova;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004566 if (pi_mr->meta_ndescs) {
4567 prot_len = pi_mr->meta_length;
4568 prot_key = pi_mr->ibmr.lkey;
Israel Rukshinde0ae952019-06-11 18:52:55 +03004569 prot_va = pi_mr->pi_iova;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004570 prot = true;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004571 }
4572
4573 if (!prot || (data_key == prot_key && data_va == prot_va &&
4574 data_len == prot_len)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004575 /**
4576 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004577 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02004578 * So need construct:
4579 * ------------------
4580 * | data_klm |
4581 * ------------------
4582 * | BSF |
4583 * ------------------
4584 **/
4585 struct mlx5_klm *data_klm = *seg;
4586
4587 data_klm->bcount = cpu_to_be32(data_len);
4588 data_klm->key = cpu_to_be32(data_key);
4589 data_klm->va = cpu_to_be64(data_va);
4590 wqe_size = ALIGN(sizeof(*data_klm), 64);
4591 } else {
4592 /**
4593 * Source domain contains signature information
4594 * So need construct a strided block format:
4595 * ---------------------------
4596 * | stride_block_ctrl |
4597 * ---------------------------
4598 * | data_klm |
4599 * ---------------------------
4600 * | prot_klm |
4601 * ---------------------------
4602 * | BSF |
4603 * ---------------------------
4604 **/
4605 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
4606 struct mlx5_stride_block_entry *data_sentry;
4607 struct mlx5_stride_block_entry *prot_sentry;
Sagi Grimberge6631812014-02-23 14:19:11 +02004608 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
4609 int prot_size;
4610
4611 sblock_ctrl = *seg;
4612 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
4613 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
4614
4615 prot_size = prot_field_size(sig_attrs->mem.sig_type);
4616 if (!prot_size) {
4617 pr_err("Bad block size given: %u\n", block_size);
4618 return -EINVAL;
4619 }
4620 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
4621 prot_size);
4622 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
4623 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
4624 sblock_ctrl->num_entries = cpu_to_be16(2);
4625
4626 data_sentry->bcount = cpu_to_be16(block_size);
4627 data_sentry->key = cpu_to_be32(data_key);
4628 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004629 data_sentry->stride = cpu_to_be16(block_size);
4630
Sagi Grimberge6631812014-02-23 14:19:11 +02004631 prot_sentry->bcount = cpu_to_be16(prot_size);
4632 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004633 prot_sentry->va = cpu_to_be64(prot_va);
4634 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004635
Sagi Grimberge6631812014-02-23 14:19:11 +02004636 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
4637 sizeof(*prot_sentry), 64);
4638 }
4639
4640 *seg += wqe_size;
4641 *size += wqe_size / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004642 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004643
4644 bsf = *seg;
4645 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
4646 if (ret)
4647 return -EINVAL;
4648
4649 *seg += sizeof(*bsf);
4650 *size += sizeof(*bsf) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004651 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004652
4653 return 0;
4654}
4655
4656static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004657 struct ib_mr *sig_mr, int access_flags,
4658 u32 size, u32 length, u32 pdn)
Sagi Grimberge6631812014-02-23 14:19:11 +02004659{
Sagi Grimberge6631812014-02-23 14:19:11 +02004660 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004661 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004662
4663 memset(seg, 0, sizeof(*seg));
4664
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004665 seg->flags = get_umr_flags(access_flags) | MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004666 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004667 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004668 MLX5_MKEY_BSF_EN | pdn);
4669 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004670 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004671 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4672}
4673
4674static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004675 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004676{
4677 memset(umr, 0, sizeof(*umr));
4678
4679 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004680 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004681 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4682 umr->mkey_mask = sig_mkey_mask();
4683}
4684
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004685static int set_pi_umr_wr(const struct ib_send_wr *send_wr,
4686 struct mlx5_ib_qp *qp, void **seg, int *size,
4687 void **cur_edge)
4688{
4689 const struct ib_reg_wr *wr = reg_wr(send_wr);
4690 struct mlx5_ib_mr *sig_mr = to_mmr(wr->mr);
4691 struct mlx5_ib_mr *pi_mr = sig_mr->pi_mr;
4692 struct ib_sig_attrs *sig_attrs = sig_mr->ibmr.sig_attrs;
4693 u32 pdn = get_pd(qp)->pdn;
4694 u32 xlt_size;
4695 int region_len, ret;
4696
4697 if (unlikely(send_wr->num_sge != 0) ||
4698 unlikely(wr->access & IB_ACCESS_REMOTE_ATOMIC) ||
Max Gurtovoy185eddc2019-06-11 18:52:51 +03004699 unlikely(!sig_mr->sig) || unlikely(!qp->ibqp.integrity_en) ||
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004700 unlikely(!sig_mr->sig->sig_status_checked))
4701 return -EINVAL;
4702
4703 /* length of the protected region, data + protection */
4704 region_len = pi_mr->ibmr.length;
4705
4706 /**
4707 * KLM octoword size - if protection was provided
4708 * then we use strided block format (3 octowords),
4709 * else we use single KLM (1 octoword)
4710 **/
4711 if (sig_attrs->mem.sig_type != IB_SIG_TYPE_NONE)
4712 xlt_size = 0x30;
4713 else
4714 xlt_size = sizeof(struct mlx5_klm);
4715
4716 set_sig_umr_segment(*seg, xlt_size);
4717 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4718 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4719 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4720
4721 set_sig_mkey_segment(*seg, wr->mr, wr->access, xlt_size, region_len,
4722 pdn);
4723 *seg += sizeof(struct mlx5_mkey_seg);
4724 *size += sizeof(struct mlx5_mkey_seg) / 16;
4725 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4726
4727 ret = set_sig_data_segment(send_wr, wr->mr, sig_attrs, qp, seg, size,
4728 cur_edge);
4729 if (ret)
4730 return ret;
4731
4732 sig_mr->sig->sig_status_checked = false;
4733 return 0;
4734}
Sagi Grimberge6631812014-02-23 14:19:11 +02004735
Sagi Grimberge6631812014-02-23 14:19:11 +02004736static int set_psv_wr(struct ib_sig_domain *domain,
4737 u32 psv_idx, void **seg, int *size)
4738{
4739 struct mlx5_seg_set_psv *psv_seg = *seg;
4740
4741 memset(psv_seg, 0, sizeof(*psv_seg));
4742 psv_seg->psv_num = cpu_to_be32(psv_idx);
4743 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004744 case IB_SIG_TYPE_NONE:
4745 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004746 case IB_SIG_TYPE_T10_DIF:
4747 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4748 domain->sig.dif.app_tag);
4749 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004750 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004751 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004752 pr_err("Bad signature type (%d) is given.\n",
4753 domain->sig_type);
4754 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004755 }
4756
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004757 *seg += sizeof(*psv_seg);
4758 *size += sizeof(*psv_seg) / 16;
4759
Sagi Grimberge6631812014-02-23 14:19:11 +02004760 return 0;
4761}
4762
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004763static int set_reg_wr(struct mlx5_ib_qp *qp,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004764 const struct ib_reg_wr *wr,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004765 void **seg, int *size, void **cur_edge,
4766 bool check_not_free)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004767{
4768 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4769 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004770 int mr_list_size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Idan Burstein064e5262018-05-02 13:16:39 +03004771 bool umr_inline = mr_list_size <= MLX5_IB_SQ_UMR_INLINE_THRESHOLD;
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004772 u8 flags = 0;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004773
4774 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4775 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4776 "Invalid IB_SEND_INLINE send flag\n");
4777 return -EINVAL;
4778 }
4779
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004780 if (check_not_free)
4781 flags |= MLX5_UMR_CHECK_NOT_FREE;
4782 if (umr_inline)
4783 flags |= MLX5_UMR_INLINE;
4784
4785 set_reg_umr_seg(*seg, mr, flags);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004786 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4787 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004788 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004789
4790 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4791 *seg += sizeof(struct mlx5_mkey_seg);
4792 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004793 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004794
Idan Burstein064e5262018-05-02 13:16:39 +03004795 if (umr_inline) {
Guy Levi34f4c952018-11-26 08:15:50 +02004796 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, mr->descs,
4797 mr_list_size);
4798 *size = ALIGN(*size, MLX5_SEND_WQE_BB >> 4);
Idan Burstein064e5262018-05-02 13:16:39 +03004799 } else {
4800 set_reg_data_seg(*seg, mr, pd);
4801 *seg += sizeof(struct mlx5_wqe_data_seg);
4802 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4803 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004804 return 0;
4805}
4806
Guy Levi34f4c952018-11-26 08:15:50 +02004807static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size,
4808 void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004809{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004810 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004811 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4812 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004813 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004814 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004815 *seg += sizeof(struct mlx5_mkey_seg);
4816 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004817 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03004818}
4819
Guy Levi34f4c952018-11-26 08:15:50 +02004820static void dump_wqe(struct mlx5_ib_qp *qp, u32 idx, int size_16)
Eli Cohene126ba92013-07-07 17:25:49 +03004821{
4822 __be32 *p = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004823 int i, j;
4824
Guy Levi34f4c952018-11-26 08:15:50 +02004825 pr_debug("dump WQE index %u:\n", idx);
Eli Cohene126ba92013-07-07 17:25:49 +03004826 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4827 if ((i & 0xf) == 0) {
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004828 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, idx);
Guy Levi34f4c952018-11-26 08:15:50 +02004829 pr_debug("WQBB at %p:\n", (void *)p);
Eli Cohene126ba92013-07-07 17:25:49 +03004830 j = 0;
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004831 idx = (idx + 1) & (qp->sq.wqe_cnt - 1);
Eli Cohene126ba92013-07-07 17:25:49 +03004832 }
4833 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4834 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4835 be32_to_cpu(p[j + 3]));
4836 }
4837}
4838
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004839static int __begin_wqe(struct mlx5_ib_qp *qp, void **seg,
Guy Levi34f4c952018-11-26 08:15:50 +02004840 struct mlx5_wqe_ctrl_seg **ctrl,
4841 const struct ib_send_wr *wr, unsigned int *idx,
4842 int *size, void **cur_edge, int nreq,
4843 bool send_signaled, bool solicited)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004844{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004845 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4846 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004847
4848 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
Guy Levi34f4c952018-11-26 08:15:50 +02004849 *seg = mlx5_frag_buf_get_wqe(&qp->sq.fbc, *idx);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004850 *ctrl = *seg;
4851 *(uint32_t *)(*seg + 8) = 0;
4852 (*ctrl)->imm = send_ieth(wr);
4853 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004854 (send_signaled ? MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4855 (solicited ? MLX5_WQE_CTRL_SOLICITED : 0);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004856
4857 *seg += sizeof(**ctrl);
4858 *size = sizeof(**ctrl) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004859 *cur_edge = qp->sq.cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004860
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004861 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004862}
4863
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004864static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4865 struct mlx5_wqe_ctrl_seg **ctrl,
4866 const struct ib_send_wr *wr, unsigned *idx,
Guy Levi34f4c952018-11-26 08:15:50 +02004867 int *size, void **cur_edge, int nreq)
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004868{
Guy Levi34f4c952018-11-26 08:15:50 +02004869 return __begin_wqe(qp, seg, ctrl, wr, idx, size, cur_edge, nreq,
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004870 wr->send_flags & IB_SEND_SIGNALED,
4871 wr->send_flags & IB_SEND_SOLICITED);
4872}
4873
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004874static void finish_wqe(struct mlx5_ib_qp *qp,
4875 struct mlx5_wqe_ctrl_seg *ctrl,
Guy Levi34f4c952018-11-26 08:15:50 +02004876 void *seg, u8 size, void *cur_edge,
4877 unsigned int idx, u64 wr_id, int nreq, u8 fence,
4878 u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004879{
4880 u8 opmod = 0;
4881
4882 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
4883 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02004884 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004885 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004886 if (unlikely(qp->wq_sig))
4887 ctrl->signature = wq_sig(ctrl);
4888
4889 qp->sq.wrid[idx] = wr_id;
4890 qp->sq.w_list[idx].opcode = mlx5_opcode;
4891 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
4892 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
4893 qp->sq.w_list[idx].next = qp->sq.cur_post;
Guy Levi34f4c952018-11-26 08:15:50 +02004894
4895 /* We save the edge which was possibly updated during the WQE
4896 * construction, into SQ's cache.
4897 */
4898 seg = PTR_ALIGN(seg, MLX5_SEND_WQE_BB);
4899 qp->sq.cur_edge = (unlikely(seg == cur_edge)) ?
4900 get_sq_edge(&qp->sq, qp->sq.cur_post &
4901 (qp->sq.wqe_cnt - 1)) :
4902 cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004903}
4904
Bart Van Assched34ac5c2018-07-18 09:25:32 -07004905static int _mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
4906 const struct ib_send_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03004907{
4908 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
4909 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004910 struct mlx5_core_dev *mdev = dev->mdev;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004911 struct ib_reg_wr reg_pi_wr;
Haggai Erand16e91d2016-02-29 15:45:05 +02004912 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02004913 struct mlx5_ib_mr *mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004914 struct mlx5_ib_mr *pi_mr;
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03004915 struct mlx5_ib_mr pa_pi_mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004916 struct ib_sig_attrs *sig_attrs;
Eli Cohene126ba92013-07-07 17:25:49 +03004917 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02004918 struct mlx5_bf *bf;
Guy Levi34f4c952018-11-26 08:15:50 +02004919 void *cur_edge;
Eli Cohene126ba92013-07-07 17:25:49 +03004920 int uninitialized_var(size);
Eli Cohene126ba92013-07-07 17:25:49 +03004921 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03004922 unsigned idx;
4923 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004924 int num_sge;
4925 void *seg;
4926 int nreq;
4927 int i;
4928 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004929 u8 fence;
4930
Parav Pandit6c755202018-08-28 14:45:29 +03004931 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
4932 !drain)) {
4933 *bad_wr = wr;
4934 return -EIO;
4935 }
4936
Haggai Erand16e91d2016-02-29 15:45:05 +02004937 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4938 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
4939
4940 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004941 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02004942
Eli Cohene126ba92013-07-07 17:25:49 +03004943 spin_lock_irqsave(&qp->sq.lock, flags);
4944
4945 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04004946 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03004947 mlx5_ib_warn(dev, "\n");
4948 err = -EINVAL;
4949 *bad_wr = wr;
4950 goto out;
4951 }
4952
Eli Cohene126ba92013-07-07 17:25:49 +03004953 num_sge = wr->num_sge;
4954 if (unlikely(num_sge > qp->sq.max_gs)) {
4955 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03004956 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03004957 *bad_wr = wr;
4958 goto out;
4959 }
4960
Guy Levi34f4c952018-11-26 08:15:50 +02004961 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, &cur_edge,
4962 nreq);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004963 if (err) {
4964 mlx5_ib_warn(dev, "\n");
4965 err = -ENOMEM;
4966 *bad_wr = wr;
4967 goto out;
4968 }
Eli Cohene126ba92013-07-07 17:25:49 +03004969
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004970 if (wr->opcode == IB_WR_REG_MR ||
4971 wr->opcode == IB_WR_REG_MR_INTEGRITY) {
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004972 fence = dev->umr_fence;
4973 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Majd Dibbiny074fca32018-11-05 08:07:37 +02004974 } else {
4975 if (wr->send_flags & IB_SEND_FENCE) {
4976 if (qp->next_fence)
4977 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
4978 else
4979 fence = MLX5_FENCE_MODE_FENCE;
4980 } else {
4981 fence = qp->next_fence;
4982 }
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004983 }
4984
Eli Cohene126ba92013-07-07 17:25:49 +03004985 switch (ibqp->qp_type) {
4986 case IB_QPT_XRC_INI:
4987 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03004988 seg += sizeof(*xrc);
4989 size += sizeof(*xrc) / 16;
4990 /* fall through */
4991 case IB_QPT_RC:
4992 switch (wr->opcode) {
4993 case IB_WR_RDMA_READ:
4994 case IB_WR_RDMA_WRITE:
4995 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004996 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4997 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004998 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004999 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5000 break;
5001
5002 case IB_WR_ATOMIC_CMP_AND_SWP:
5003 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03005004 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03005005 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
5006 err = -ENOSYS;
5007 *bad_wr = wr;
5008 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005009
5010 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03005011 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
5012 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Guy Levi34f4c952018-11-26 08:15:50 +02005013 set_linv_wr(qp, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005014 num_sge = 0;
5015 break;
5016
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005017 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005018 qp->sq.wr_data[idx] = IB_WR_REG_MR;
5019 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
Guy Levi34f4c952018-11-26 08:15:50 +02005020 err = set_reg_wr(qp, reg_wr(wr), &seg, &size,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03005021 &cur_edge, true);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005022 if (err) {
5023 *bad_wr = wr;
5024 goto out;
5025 }
5026 num_sge = 0;
5027 break;
5028
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005029 case IB_WR_REG_MR_INTEGRITY:
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005030 qp->sq.wr_data[idx] = IB_WR_REG_MR_INTEGRITY;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005031
5032 mr = to_mmr(reg_wr(wr)->mr);
5033 pi_mr = mr->pi_mr;
5034
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005035 if (pi_mr) {
5036 memset(&reg_pi_wr, 0,
5037 sizeof(struct ib_reg_wr));
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005038
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005039 reg_pi_wr.mr = &pi_mr->ibmr;
5040 reg_pi_wr.access = reg_wr(wr)->access;
5041 reg_pi_wr.key = pi_mr->ibmr.rkey;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005042
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005043 ctrl->imm = cpu_to_be32(reg_pi_wr.key);
5044 /* UMR for data + prot registration */
5045 err = set_reg_wr(qp, &reg_pi_wr, &seg,
5046 &size, &cur_edge,
5047 false);
5048 if (err) {
5049 *bad_wr = wr;
5050 goto out;
5051 }
5052 finish_wqe(qp, ctrl, seg, size,
5053 cur_edge, idx, wr->wr_id,
5054 nreq, fence,
5055 MLX5_OPCODE_UMR);
5056
5057 err = begin_wqe(qp, &seg, &ctrl, wr,
5058 &idx, &size, &cur_edge,
5059 nreq);
5060 if (err) {
5061 mlx5_ib_warn(dev, "\n");
5062 err = -ENOMEM;
5063 *bad_wr = wr;
5064 goto out;
5065 }
5066 } else {
5067 memset(&pa_pi_mr, 0,
5068 sizeof(struct mlx5_ib_mr));
5069 /* No UMR, use local_dma_lkey */
5070 pa_pi_mr.ibmr.lkey =
5071 mr->ibmr.pd->local_dma_lkey;
5072
5073 pa_pi_mr.ndescs = mr->ndescs;
5074 pa_pi_mr.data_length = mr->data_length;
5075 pa_pi_mr.data_iova = mr->data_iova;
5076 if (mr->meta_ndescs) {
5077 pa_pi_mr.meta_ndescs =
5078 mr->meta_ndescs;
5079 pa_pi_mr.meta_length =
5080 mr->meta_length;
5081 pa_pi_mr.pi_iova = mr->pi_iova;
5082 }
5083
5084 pa_pi_mr.ibmr.length = mr->ibmr.length;
5085 mr->pi_mr = &pa_pi_mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005086 }
5087 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
5088 /* UMR for sig MR */
5089 err = set_pi_umr_wr(wr, qp, &seg, &size,
5090 &cur_edge);
5091 if (err) {
5092 mlx5_ib_warn(dev, "\n");
5093 *bad_wr = wr;
5094 goto out;
5095 }
5096 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5097 wr->wr_id, nreq, fence,
5098 MLX5_OPCODE_UMR);
5099
5100 /*
5101 * SET_PSV WQEs are not signaled and solicited
5102 * on error
5103 */
5104 sig_attrs = mr->ibmr.sig_attrs;
5105 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5106 &size, &cur_edge, nreq, false,
5107 true);
5108 if (err) {
5109 mlx5_ib_warn(dev, "\n");
5110 err = -ENOMEM;
5111 *bad_wr = wr;
5112 goto out;
5113 }
5114 err = set_psv_wr(&sig_attrs->mem,
5115 mr->sig->psv_memory.psv_idx,
5116 &seg, &size);
5117 if (err) {
5118 mlx5_ib_warn(dev, "\n");
5119 *bad_wr = wr;
5120 goto out;
5121 }
5122 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5123 wr->wr_id, nreq, next_fence,
5124 MLX5_OPCODE_SET_PSV);
5125
5126 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5127 &size, &cur_edge, nreq, false,
5128 true);
5129 if (err) {
5130 mlx5_ib_warn(dev, "\n");
5131 err = -ENOMEM;
5132 *bad_wr = wr;
5133 goto out;
5134 }
5135 err = set_psv_wr(&sig_attrs->wire,
5136 mr->sig->psv_wire.psv_idx,
5137 &seg, &size);
5138 if (err) {
5139 mlx5_ib_warn(dev, "\n");
5140 *bad_wr = wr;
5141 goto out;
5142 }
5143 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5144 wr->wr_id, nreq, next_fence,
5145 MLX5_OPCODE_SET_PSV);
5146
5147 qp->next_fence =
5148 MLX5_FENCE_MODE_INITIATOR_SMALL;
5149 num_sge = 0;
5150 goto skip_psv;
5151
Eli Cohene126ba92013-07-07 17:25:49 +03005152 default:
5153 break;
5154 }
5155 break;
5156
5157 case IB_QPT_UC:
5158 switch (wr->opcode) {
5159 case IB_WR_RDMA_WRITE:
5160 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005161 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5162 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03005163 seg += sizeof(struct mlx5_wqe_raddr_seg);
5164 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5165 break;
5166
5167 default:
5168 break;
5169 }
5170 break;
5171
Eli Cohene126ba92013-07-07 17:25:49 +03005172 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02005173 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
5174 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
5175 err = -EPERM;
5176 *bad_wr = wr;
5177 goto out;
5178 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07005179 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02005180 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03005181 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005182 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005183 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005184 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
5185
Eli Cohene126ba92013-07-07 17:25:49 +03005186 break;
Erez Shitritf0313962016-02-21 16:27:17 +02005187 case IB_QPT_UD:
5188 set_datagram_seg(seg, wr);
5189 seg += sizeof(struct mlx5_wqe_datagram_seg);
5190 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005191 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005192
5193 /* handle qp that supports ud offload */
5194 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
5195 struct mlx5_wqe_eth_pad *pad;
5196
5197 pad = seg;
5198 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
5199 seg += sizeof(struct mlx5_wqe_eth_pad);
5200 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005201 set_eth_seg(wr, qp, &seg, &size, &cur_edge);
5202 handle_post_send_edge(&qp->sq, &seg, size,
5203 &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005204 }
5205 break;
Eli Cohene126ba92013-07-07 17:25:49 +03005206 case MLX5_IB_QPT_REG_UMR:
5207 if (wr->opcode != MLX5_IB_WR_UMR) {
5208 err = -EINVAL;
5209 mlx5_ib_warn(dev, "bad opcode\n");
5210 goto out;
5211 }
5212 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005213 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02005214 err = set_reg_umr_segment(dev, seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
5215 if (unlikely(err))
5216 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005217 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
5218 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005219 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005220 set_reg_mkey_segment(seg, wr);
5221 seg += sizeof(struct mlx5_mkey_seg);
5222 size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005223 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005224 break;
5225
5226 default:
5227 break;
5228 }
5229
5230 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
Guy Levi34f4c952018-11-26 08:15:50 +02005231 err = set_data_inl_seg(qp, wr, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005232 if (unlikely(err)) {
5233 mlx5_ib_warn(dev, "\n");
5234 *bad_wr = wr;
5235 goto out;
5236 }
Eli Cohene126ba92013-07-07 17:25:49 +03005237 } else {
Eli Cohene126ba92013-07-07 17:25:49 +03005238 for (i = 0; i < num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02005239 handle_post_send_edge(&qp->sq, &seg, size,
5240 &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005241 if (likely(wr->sg_list[i].length)) {
Guy Levi34f4c952018-11-26 08:15:50 +02005242 set_data_ptr_seg
5243 ((struct mlx5_wqe_data_seg *)seg,
5244 wr->sg_list + i);
Eli Cohene126ba92013-07-07 17:25:49 +03005245 size += sizeof(struct mlx5_wqe_data_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005246 seg += sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005247 }
5248 }
5249 }
5250
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005251 qp->next_fence = next_fence;
Guy Levi34f4c952018-11-26 08:15:50 +02005252 finish_wqe(qp, ctrl, seg, size, cur_edge, idx, wr->wr_id, nreq,
5253 fence, mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02005254skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03005255 if (0)
5256 dump_wqe(qp, idx, size);
5257 }
5258
5259out:
5260 if (likely(nreq)) {
5261 qp->sq.head += nreq;
5262
5263 /* Make sure that descriptors are written before
5264 * updating doorbell record and ringing the doorbell
5265 */
5266 wmb();
5267
5268 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
5269
Eli Cohenada388f2014-01-14 17:45:16 +02005270 /* Make sure doorbell record is visible to the HCA before
5271 * we hit doorbell */
5272 wmb();
5273
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005274 /* currently we support only regular doorbells */
Maxim Mikityanskiybbf29f62019-03-29 15:37:52 -07005275 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005276 /* Make sure doorbells don't leak out of SQ spinlock
5277 * and reach the HCA out of order.
5278 */
Eli Cohene126ba92013-07-07 17:25:49 +03005279 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03005280 }
5281
5282 spin_unlock_irqrestore(&qp->sq.lock, flags);
5283
5284 return err;
5285}
5286
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005287int mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
5288 const struct ib_send_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005289{
5290 return _mlx5_ib_post_send(ibqp, wr, bad_wr, false);
5291}
5292
Eli Cohene126ba92013-07-07 17:25:49 +03005293static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
5294{
5295 sig->signature = calc_sig(sig, size);
5296}
5297
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005298static int _mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5299 const struct ib_recv_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03005300{
5301 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5302 struct mlx5_wqe_data_seg *scat;
5303 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03005304 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5305 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005306 unsigned long flags;
5307 int err = 0;
5308 int nreq;
5309 int ind;
5310 int i;
5311
Parav Pandit6c755202018-08-28 14:45:29 +03005312 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5313 !drain)) {
5314 *bad_wr = wr;
5315 return -EIO;
5316 }
5317
Haggai Erand16e91d2016-02-29 15:45:05 +02005318 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5319 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
5320
Eli Cohene126ba92013-07-07 17:25:49 +03005321 spin_lock_irqsave(&qp->rq.lock, flags);
5322
5323 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
5324
5325 for (nreq = 0; wr; nreq++, wr = wr->next) {
5326 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
5327 err = -ENOMEM;
5328 *bad_wr = wr;
5329 goto out;
5330 }
5331
5332 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
5333 err = -EINVAL;
5334 *bad_wr = wr;
5335 goto out;
5336 }
5337
Guy Levi34f4c952018-11-26 08:15:50 +02005338 scat = mlx5_frag_buf_get_wqe(&qp->rq.fbc, ind);
Eli Cohene126ba92013-07-07 17:25:49 +03005339 if (qp->wq_sig)
5340 scat++;
5341
5342 for (i = 0; i < wr->num_sge; i++)
5343 set_data_ptr_seg(scat + i, wr->sg_list + i);
5344
5345 if (i < qp->rq.max_gs) {
5346 scat[i].byte_count = 0;
5347 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
5348 scat[i].addr = 0;
5349 }
5350
5351 if (qp->wq_sig) {
5352 sig = (struct mlx5_rwqe_sig *)scat;
5353 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
5354 }
5355
5356 qp->rq.wrid[ind] = wr->wr_id;
5357
5358 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
5359 }
5360
5361out:
5362 if (likely(nreq)) {
5363 qp->rq.head += nreq;
5364
5365 /* Make sure that descriptors are written before
5366 * doorbell record.
5367 */
5368 wmb();
5369
5370 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
5371 }
5372
5373 spin_unlock_irqrestore(&qp->rq.lock, flags);
5374
5375 return err;
5376}
5377
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005378int mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5379 const struct ib_recv_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005380{
5381 return _mlx5_ib_post_recv(ibqp, wr, bad_wr, false);
5382}
5383
Eli Cohene126ba92013-07-07 17:25:49 +03005384static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
5385{
5386 switch (mlx5_state) {
5387 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
5388 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
5389 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
5390 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
5391 case MLX5_QP_STATE_SQ_DRAINING:
5392 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
5393 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
5394 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
5395 default: return -1;
5396 }
5397}
5398
5399static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
5400{
5401 switch (mlx5_mig_state) {
5402 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
5403 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
5404 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
5405 default: return -1;
5406 }
5407}
5408
5409static int to_ib_qp_access_flags(int mlx5_flags)
5410{
5411 int ib_flags = 0;
5412
5413 if (mlx5_flags & MLX5_QP_BIT_RRE)
5414 ib_flags |= IB_ACCESS_REMOTE_READ;
5415 if (mlx5_flags & MLX5_QP_BIT_RWE)
5416 ib_flags |= IB_ACCESS_REMOTE_WRITE;
5417 if (mlx5_flags & MLX5_QP_BIT_RAE)
5418 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
5419
5420 return ib_flags;
5421}
5422
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005423static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005424 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005425 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03005426{
Eli Cohene126ba92013-07-07 17:25:49 +03005427
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005428 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03005429
Jason Gunthorpee7996a92018-01-29 13:26:40 -07005430 if (!path->port || path->port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03005431 return;
5432
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02005433 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
5434
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005435 rdma_ah_set_port_num(ah_attr, path->port);
5436 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03005437
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005438 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
5439 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
5440 rdma_ah_set_static_rate(ah_attr,
5441 path->static_rate ? path->static_rate - 5 : 0);
5442 if (path->grh_mlid & (1 << 7)) {
5443 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
5444
5445 rdma_ah_set_grh(ah_attr, NULL,
5446 tc_fl & 0xfffff,
5447 path->mgid_index,
5448 path->hop_limit,
5449 (tc_fl >> 20) & 0xff);
5450 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03005451 }
5452}
5453
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005454static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
5455 struct mlx5_ib_sq *sq,
5456 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03005457{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005458 int err;
5459
Eran Ben Elisha28160772017-12-26 15:17:05 +02005460 err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005461 if (err)
5462 goto out;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005463 sq->state = *sq_state;
5464
5465out:
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005466 return err;
5467}
5468
5469static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
5470 struct mlx5_ib_rq *rq,
5471 u8 *rq_state)
5472{
5473 void *out;
5474 void *rqc;
5475 int inlen;
5476 int err;
5477
5478 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005479 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005480 if (!out)
5481 return -ENOMEM;
5482
5483 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
5484 if (err)
5485 goto out;
5486
5487 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
5488 *rq_state = MLX5_GET(rqc, rqc, state);
5489 rq->state = *rq_state;
5490
5491out:
5492 kvfree(out);
5493 return err;
5494}
5495
5496static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
5497 struct mlx5_ib_qp *qp, u8 *qp_state)
5498{
5499 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
5500 [MLX5_RQC_STATE_RST] = {
5501 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5502 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5503 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
5504 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
5505 },
5506 [MLX5_RQC_STATE_RDY] = {
5507 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5508 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5509 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
5510 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
5511 },
5512 [MLX5_RQC_STATE_ERR] = {
5513 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5514 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5515 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
5516 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
5517 },
5518 [MLX5_RQ_STATE_NA] = {
5519 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5520 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5521 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
5522 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
5523 },
5524 };
5525
5526 *qp_state = sqrq_trans[rq_state][sq_state];
5527
5528 if (*qp_state == MLX5_QP_STATE_BAD) {
5529 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
5530 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
5531 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
5532 return -EINVAL;
5533 }
5534
5535 if (*qp_state == MLX5_QP_STATE)
5536 *qp_state = qp->state;
5537
5538 return 0;
5539}
5540
5541static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
5542 struct mlx5_ib_qp *qp,
5543 u8 *raw_packet_qp_state)
5544{
5545 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
5546 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
5547 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
5548 int err;
5549 u8 sq_state = MLX5_SQ_STATE_NA;
5550 u8 rq_state = MLX5_RQ_STATE_NA;
5551
5552 if (qp->sq.wqe_cnt) {
5553 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
5554 if (err)
5555 return err;
5556 }
5557
5558 if (qp->rq.wqe_cnt) {
5559 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
5560 if (err)
5561 return err;
5562 }
5563
5564 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
5565 raw_packet_qp_state);
5566}
5567
5568static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
5569 struct ib_qp_attr *qp_attr)
5570{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005571 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03005572 struct mlx5_qp_context *context;
5573 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005574 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03005575 int err = 0;
5576
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005577 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005578 if (!outb)
5579 return -ENOMEM;
5580
majd@mellanox.com19098df2016-01-14 19:13:03 +02005581 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005582 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03005583 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005584 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005585
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005586 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
5587 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
5588
Eli Cohene126ba92013-07-07 17:25:49 +03005589 mlx5_state = be32_to_cpu(context->flags) >> 28;
5590
5591 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03005592 qp_attr->path_mtu = context->mtu_msgmax >> 5;
5593 qp_attr->path_mig_state =
5594 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
5595 qp_attr->qkey = be32_to_cpu(context->qkey);
5596 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
5597 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
5598 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
5599 qp_attr->qp_access_flags =
5600 to_ib_qp_access_flags(be32_to_cpu(context->params2));
5601
5602 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005603 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
5604 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005605 qp_attr->alt_pkey_index =
5606 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005607 qp_attr->alt_port_num =
5608 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03005609 }
5610
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005611 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03005612 qp_attr->port_num = context->pri_path.port;
5613
5614 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
5615 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
5616
5617 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
5618
5619 qp_attr->max_dest_rd_atomic =
5620 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
5621 qp_attr->min_rnr_timer =
5622 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
5623 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
5624 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
5625 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
5626 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005627
5628out:
5629 kfree(outb);
5630 return err;
5631}
5632
Moni Shoua776a3902018-01-02 16:19:33 +02005633static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
5634 struct ib_qp_attr *qp_attr, int qp_attr_mask,
5635 struct ib_qp_init_attr *qp_init_attr)
5636{
5637 struct mlx5_core_dct *dct = &mqp->dct.mdct;
5638 u32 *out;
5639 u32 access_flags = 0;
5640 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
5641 void *dctc;
5642 int err;
5643 int supported_mask = IB_QP_STATE |
5644 IB_QP_ACCESS_FLAGS |
5645 IB_QP_PORT |
5646 IB_QP_MIN_RNR_TIMER |
5647 IB_QP_AV |
5648 IB_QP_PATH_MTU |
5649 IB_QP_PKEY_INDEX;
5650
5651 if (qp_attr_mask & ~supported_mask)
5652 return -EINVAL;
5653 if (mqp->state != IB_QPS_RTR)
5654 return -EINVAL;
5655
5656 out = kzalloc(outlen, GFP_KERNEL);
5657 if (!out)
5658 return -ENOMEM;
5659
5660 err = mlx5_core_dct_query(dev->mdev, dct, out, outlen);
5661 if (err)
5662 goto out;
5663
5664 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
5665
5666 if (qp_attr_mask & IB_QP_STATE)
5667 qp_attr->qp_state = IB_QPS_RTR;
5668
5669 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
5670 if (MLX5_GET(dctc, dctc, rre))
5671 access_flags |= IB_ACCESS_REMOTE_READ;
5672 if (MLX5_GET(dctc, dctc, rwe))
5673 access_flags |= IB_ACCESS_REMOTE_WRITE;
5674 if (MLX5_GET(dctc, dctc, rae))
5675 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
5676 qp_attr->qp_access_flags = access_flags;
5677 }
5678
5679 if (qp_attr_mask & IB_QP_PORT)
5680 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
5681 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
5682 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
5683 if (qp_attr_mask & IB_QP_AV) {
5684 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
5685 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
5686 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
5687 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
5688 }
5689 if (qp_attr_mask & IB_QP_PATH_MTU)
5690 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
5691 if (qp_attr_mask & IB_QP_PKEY_INDEX)
5692 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
5693out:
5694 kfree(out);
5695 return err;
5696}
5697
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005698int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5699 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
5700{
5701 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5702 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5703 int err = 0;
5704 u8 raw_packet_qp_state;
5705
Yishai Hadas28d61372016-05-23 15:20:56 +03005706 if (ibqp->rwq_ind_tbl)
5707 return -ENOSYS;
5708
Haggai Erand16e91d2016-02-29 15:45:05 +02005709 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5710 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
5711 qp_init_attr);
5712
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005713 /* Not all of output fields are applicable, make sure to zero them */
5714 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5715 memset(qp_attr, 0, sizeof(*qp_attr));
5716
Moni Shoua776a3902018-01-02 16:19:33 +02005717 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
5718 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
5719 qp_attr_mask, qp_init_attr);
5720
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005721 mutex_lock(&qp->mutex);
5722
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005723 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
5724 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005725 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
5726 if (err)
5727 goto out;
5728 qp->state = raw_packet_qp_state;
5729 qp_attr->port_num = 1;
5730 } else {
5731 err = query_qp_attr(dev, qp, qp_attr);
5732 if (err)
5733 goto out;
5734 }
5735
5736 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03005737 qp_attr->cur_qp_state = qp_attr->qp_state;
5738 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
5739 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
5740
5741 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005742 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005743 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005744 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005745 } else {
5746 qp_attr->cap.max_send_wr = 0;
5747 qp_attr->cap.max_send_sge = 0;
5748 }
5749
Noa Osherovich0540d812016-06-04 15:15:32 +03005750 qp_init_attr->qp_type = ibqp->qp_type;
5751 qp_init_attr->recv_cq = ibqp->recv_cq;
5752 qp_init_attr->send_cq = ibqp->send_cq;
5753 qp_init_attr->srq = ibqp->srq;
5754 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005755
5756 qp_init_attr->cap = qp_attr->cap;
5757
5758 qp_init_attr->create_flags = 0;
5759 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
5760 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
5761
Leon Romanovsky051f2632015-12-20 12:16:11 +02005762 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
5763 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
5764 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
5765 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
5766 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
5767 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02005768 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
5769 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02005770
Eli Cohene126ba92013-07-07 17:25:49 +03005771 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5772 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5773
Eli Cohene126ba92013-07-07 17:25:49 +03005774out:
5775 mutex_unlock(&qp->mutex);
5776 return err;
5777}
5778
5779struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03005780 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005781{
5782 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5783 struct mlx5_ib_xrcd *xrcd;
5784 int err;
5785
Saeed Mahameed938fe832015-05-28 22:28:41 +03005786 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005787 return ERR_PTR(-ENOSYS);
5788
5789 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5790 if (!xrcd)
5791 return ERR_PTR(-ENOMEM);
5792
Yishai Hadas5aa37712018-11-26 08:28:38 +02005793 err = mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03005794 if (err) {
5795 kfree(xrcd);
5796 return ERR_PTR(-ENOMEM);
5797 }
5798
5799 return &xrcd->ibxrcd;
5800}
5801
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005802int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005803{
5804 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5805 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
5806 int err;
5807
Yishai Hadas5aa37712018-11-26 08:28:38 +02005808 err = mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, 0);
Leon Romanovskyb0818082018-01-28 11:25:30 +02005809 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03005810 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005811
5812 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03005813 return 0;
5814}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005815
Yishai Hadas350d0e42016-08-28 14:58:18 +03005816static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5817{
5818 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5819 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5820 struct ib_event event;
5821
5822 if (rwq->ibwq.event_handler) {
5823 event.device = rwq->ibwq.device;
5824 event.element.wq = &rwq->ibwq;
5825 switch (type) {
5826 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5827 event.event = IB_EVENT_WQ_FATAL;
5828 break;
5829 default:
5830 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5831 return;
5832 }
5833
5834 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5835 }
5836}
5837
Maor Gottlieb03404e82017-05-30 10:29:13 +03005838static int set_delay_drop(struct mlx5_ib_dev *dev)
5839{
5840 int err = 0;
5841
5842 mutex_lock(&dev->delay_drop.lock);
5843 if (dev->delay_drop.activate)
5844 goto out;
5845
5846 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
5847 if (err)
5848 goto out;
5849
5850 dev->delay_drop.activate = true;
5851out:
5852 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005853
5854 if (!err)
5855 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005856 return err;
5857}
5858
Yishai Hadas79b20a62016-05-23 15:20:50 +03005859static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5860 struct ib_wq_init_attr *init_attr)
5861{
5862 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005863 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005864 __be64 *rq_pas0;
5865 void *in;
5866 void *rqc;
5867 void *wq;
5868 int inlen;
5869 int err;
5870
5871 dev = to_mdev(pd->device);
5872
5873 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005874 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005875 if (!in)
5876 return -ENOMEM;
5877
Yishai Hadas34d57582018-09-20 21:39:21 +03005878 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005879 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
5880 MLX5_SET(rqc, rqc, mem_rq_type,
5881 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
5882 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
5883 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
5884 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
5885 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
5886 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03005887 MLX5_SET(wq, wq, wq_type,
5888 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
5889 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005890 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5891 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
5892 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
5893 err = -EOPNOTSUPP;
5894 goto out;
5895 } else {
5896 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
5897 }
5898 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005899 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03005900 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
5901 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
5902 MLX5_SET(wq, wq, log_wqe_stride_size,
5903 rwq->single_stride_log_num_of_bytes -
5904 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
5905 MLX5_SET(wq, wq, log_wqe_num_of_strides, rwq->log_num_strides -
5906 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES);
5907 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005908 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
5909 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
5910 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
5911 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
5912 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
5913 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02005914 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005915 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02005916 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005917 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
5918 err = -EOPNOTSUPP;
5919 goto out;
5920 }
5921 } else {
5922 MLX5_SET(rqc, rqc, vsd, 1);
5923 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02005924 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
5925 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
5926 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
5927 err = -EOPNOTSUPP;
5928 goto out;
5929 }
5930 MLX5_SET(rqc, rqc, scatter_fcs, 1);
5931 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03005932 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5933 if (!(dev->ib_dev.attrs.raw_packet_caps &
5934 IB_RAW_PACKET_CAP_DELAY_DROP)) {
5935 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
5936 err = -EOPNOTSUPP;
5937 goto out;
5938 }
5939 MLX5_SET(rqc, rqc, delay_drop_en, 1);
5940 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005941 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
5942 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03005943 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005944 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5945 err = set_delay_drop(dev);
5946 if (err) {
5947 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
5948 err);
5949 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
5950 } else {
5951 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
5952 }
5953 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005954out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03005955 kvfree(in);
5956 return err;
5957}
5958
5959static int set_user_rq_size(struct mlx5_ib_dev *dev,
5960 struct ib_wq_init_attr *wq_init_attr,
5961 struct mlx5_ib_create_wq *ucmd,
5962 struct mlx5_ib_rwq *rwq)
5963{
5964 /* Sanity check RQ size before proceeding */
5965 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
5966 return -EINVAL;
5967
5968 if (!ucmd->rq_wqe_count)
5969 return -EINVAL;
5970
5971 rwq->wqe_count = ucmd->rq_wqe_count;
5972 rwq->wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky0dfe4522018-08-01 14:25:41 -07005973 if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
5974 return -EINVAL;
5975
Yishai Hadas79b20a62016-05-23 15:20:50 +03005976 rwq->log_rq_stride = rwq->wqe_shift;
5977 rwq->log_rq_size = ilog2(rwq->wqe_count);
5978 return 0;
5979}
5980
5981static int prepare_user_rq(struct ib_pd *pd,
5982 struct ib_wq_init_attr *init_attr,
5983 struct ib_udata *udata,
5984 struct mlx5_ib_rwq *rwq)
5985{
5986 struct mlx5_ib_dev *dev = to_mdev(pd->device);
5987 struct mlx5_ib_create_wq ucmd = {};
5988 int err;
5989 size_t required_cmd_sz;
5990
Noa Osherovichccc87082017-10-17 18:01:13 +03005991 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
5992 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005993 if (udata->inlen < required_cmd_sz) {
5994 mlx5_ib_dbg(dev, "invalid inlen\n");
5995 return -EINVAL;
5996 }
5997
5998 if (udata->inlen > sizeof(ucmd) &&
5999 !ib_is_udata_cleared(udata, sizeof(ucmd),
6000 udata->inlen - sizeof(ucmd))) {
6001 mlx5_ib_dbg(dev, "inlen is not supported\n");
6002 return -EOPNOTSUPP;
6003 }
6004
6005 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
6006 mlx5_ib_dbg(dev, "copy failed\n");
6007 return -EFAULT;
6008 }
6009
Noa Osherovichccc87082017-10-17 18:01:13 +03006010 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03006011 mlx5_ib_dbg(dev, "invalid comp mask\n");
6012 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03006013 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
6014 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
6015 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
6016 return -EOPNOTSUPP;
6017 }
6018 if ((ucmd.single_stride_log_num_of_bytes <
6019 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
6020 (ucmd.single_stride_log_num_of_bytes >
6021 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
6022 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
6023 ucmd.single_stride_log_num_of_bytes,
6024 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
6025 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
6026 return -EINVAL;
6027 }
6028 if ((ucmd.single_wqe_log_num_of_strides >
6029 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
6030 (ucmd.single_wqe_log_num_of_strides <
6031 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES)) {
6032 mlx5_ib_dbg(dev, "Invalid log num strides (%u. Range is %u - %u)\n",
6033 ucmd.single_wqe_log_num_of_strides,
6034 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
6035 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
6036 return -EINVAL;
6037 }
6038 rwq->single_stride_log_num_of_bytes =
6039 ucmd.single_stride_log_num_of_bytes;
6040 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
6041 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
6042 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006043 }
6044
6045 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
6046 if (err) {
6047 mlx5_ib_dbg(dev, "err %d\n", err);
6048 return err;
6049 }
6050
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02006051 err = create_user_rq(dev, pd, udata, rwq, &ucmd);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006052 if (err) {
6053 mlx5_ib_dbg(dev, "err %d\n", err);
Gal Pressman645ba592018-10-08 19:44:03 +03006054 return err;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006055 }
6056
6057 rwq->user_index = ucmd.user_index;
6058 return 0;
6059}
6060
6061struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
6062 struct ib_wq_init_attr *init_attr,
6063 struct ib_udata *udata)
6064{
6065 struct mlx5_ib_dev *dev;
6066 struct mlx5_ib_rwq *rwq;
6067 struct mlx5_ib_create_wq_resp resp = {};
6068 size_t min_resp_len;
6069 int err;
6070
6071 if (!udata)
6072 return ERR_PTR(-ENOSYS);
6073
6074 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6075 if (udata->outlen && udata->outlen < min_resp_len)
6076 return ERR_PTR(-EINVAL);
6077
6078 dev = to_mdev(pd->device);
6079 switch (init_attr->wq_type) {
6080 case IB_WQT_RQ:
6081 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
6082 if (!rwq)
6083 return ERR_PTR(-ENOMEM);
6084 err = prepare_user_rq(pd, init_attr, udata, rwq);
6085 if (err)
6086 goto err;
6087 err = create_rq(rwq, pd, init_attr);
6088 if (err)
6089 goto err_user_rq;
6090 break;
6091 default:
6092 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
6093 init_attr->wq_type);
6094 return ERR_PTR(-EINVAL);
6095 }
6096
Yishai Hadas350d0e42016-08-28 14:58:18 +03006097 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006098 rwq->ibwq.state = IB_WQS_RESET;
6099 if (udata->outlen) {
6100 resp.response_length = offsetof(typeof(resp), response_length) +
6101 sizeof(resp.response_length);
6102 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6103 if (err)
6104 goto err_copy;
6105 }
6106
Yishai Hadas350d0e42016-08-28 14:58:18 +03006107 rwq->core_qp.event = mlx5_ib_wq_event;
6108 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006109 return &rwq->ibwq;
6110
6111err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03006112 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006113err_user_rq:
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006114 destroy_user_rq(dev, pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006115err:
6116 kfree(rwq);
6117 return ERR_PTR(err);
6118}
6119
Leon Romanovskya49b1dc2019-06-12 15:27:41 +03006120void mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +03006121{
6122 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6123 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6124
Yishai Hadas350d0e42016-08-28 14:58:18 +03006125 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006126 destroy_user_rq(dev, wq->pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006127 kfree(rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006128}
6129
Yishai Hadasc5f90922016-05-23 15:20:53 +03006130struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
6131 struct ib_rwq_ind_table_init_attr *init_attr,
6132 struct ib_udata *udata)
6133{
6134 struct mlx5_ib_dev *dev = to_mdev(device);
6135 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
6136 int sz = 1 << init_attr->log_ind_tbl_size;
6137 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
6138 size_t min_resp_len;
6139 int inlen;
6140 int err;
6141 int i;
6142 u32 *in;
6143 void *rqtc;
6144
6145 if (udata->inlen > 0 &&
6146 !ib_is_udata_cleared(udata, 0,
6147 udata->inlen))
6148 return ERR_PTR(-EOPNOTSUPP);
6149
Maor Gottliebefd7f402016-10-27 16:36:40 +03006150 if (init_attr->log_ind_tbl_size >
6151 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
6152 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
6153 init_attr->log_ind_tbl_size,
6154 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
6155 return ERR_PTR(-EINVAL);
6156 }
6157
Yishai Hadasc5f90922016-05-23 15:20:53 +03006158 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6159 if (udata->outlen && udata->outlen < min_resp_len)
6160 return ERR_PTR(-EINVAL);
6161
6162 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
6163 if (!rwq_ind_tbl)
6164 return ERR_PTR(-ENOMEM);
6165
6166 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006167 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006168 if (!in) {
6169 err = -ENOMEM;
6170 goto err;
6171 }
6172
6173 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
6174
6175 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
6176 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
6177
6178 for (i = 0; i < sz; i++)
6179 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
6180
Yishai Hadas5deba862018-09-20 21:39:28 +03006181 rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
6182 MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);
6183
Yishai Hadasc5f90922016-05-23 15:20:53 +03006184 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
6185 kvfree(in);
6186
6187 if (err)
6188 goto err;
6189
6190 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
6191 if (udata->outlen) {
6192 resp.response_length = offsetof(typeof(resp), response_length) +
6193 sizeof(resp.response_length);
6194 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6195 if (err)
6196 goto err_copy;
6197 }
6198
6199 return &rwq_ind_tbl->ib_rwq_ind_tbl;
6200
6201err_copy:
Yishai Hadas5deba862018-09-20 21:39:28 +03006202 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006203err:
6204 kfree(rwq_ind_tbl);
6205 return ERR_PTR(err);
6206}
6207
6208int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
6209{
6210 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
6211 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
6212
Yishai Hadas5deba862018-09-20 21:39:28 +03006213 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006214
6215 kfree(rwq_ind_tbl);
6216 return 0;
6217}
6218
Yishai Hadas79b20a62016-05-23 15:20:50 +03006219int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
6220 u32 wq_attr_mask, struct ib_udata *udata)
6221{
6222 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6223 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6224 struct mlx5_ib_modify_wq ucmd = {};
6225 size_t required_cmd_sz;
6226 int curr_wq_state;
6227 int wq_state;
6228 int inlen;
6229 int err;
6230 void *rqc;
6231 void *in;
6232
6233 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
6234 if (udata->inlen < required_cmd_sz)
6235 return -EINVAL;
6236
6237 if (udata->inlen > sizeof(ucmd) &&
6238 !ib_is_udata_cleared(udata, sizeof(ucmd),
6239 udata->inlen - sizeof(ucmd)))
6240 return -EOPNOTSUPP;
6241
6242 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
6243 return -EFAULT;
6244
6245 if (ucmd.comp_mask || ucmd.reserved)
6246 return -EOPNOTSUPP;
6247
6248 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006249 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006250 if (!in)
6251 return -ENOMEM;
6252
6253 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
6254
6255 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
6256 wq_attr->curr_wq_state : wq->state;
6257 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
6258 wq_attr->wq_state : curr_wq_state;
6259 if (curr_wq_state == IB_WQS_ERR)
6260 curr_wq_state = MLX5_RQC_STATE_ERR;
6261 if (wq_state == IB_WQS_ERR)
6262 wq_state = MLX5_RQC_STATE_ERR;
6263 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
Yishai Hadas34d57582018-09-20 21:39:21 +03006264 MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006265 MLX5_SET(rqc, rqc, state, wq_state);
6266
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006267 if (wq_attr_mask & IB_WQ_FLAGS) {
6268 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
6269 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
6270 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
6271 mlx5_ib_dbg(dev, "VLAN offloads are not "
6272 "supported\n");
6273 err = -EOPNOTSUPP;
6274 goto out;
6275 }
6276 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6277 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
6278 MLX5_SET(rqc, rqc, vsd,
6279 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
6280 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02006281
6282 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6283 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
6284 err = -EOPNOTSUPP;
6285 goto out;
6286 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006287 }
6288
Majd Dibbiny23a69642017-01-18 15:25:10 +02006289 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
6290 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
6291 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6292 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03006293 MLX5_SET(rqc, rqc, counter_set_id,
6294 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006295 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06006296 dev_info_once(
6297 &dev->ib_dev.dev,
6298 "Receive WQ counters are not supported on current FW\n");
Majd Dibbiny23a69642017-01-18 15:25:10 +02006299 }
6300
Yishai Hadas350d0e42016-08-28 14:58:18 +03006301 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006302 if (!err)
6303 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
6304
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006305out:
6306 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006307 return err;
6308}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006309
6310struct mlx5_ib_drain_cqe {
6311 struct ib_cqe cqe;
6312 struct completion done;
6313};
6314
6315static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
6316{
6317 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
6318 struct mlx5_ib_drain_cqe,
6319 cqe);
6320
6321 complete(&cqe->done);
6322}
6323
6324/* This function returns only once the drained WR was completed */
6325static void handle_drain_completion(struct ib_cq *cq,
6326 struct mlx5_ib_drain_cqe *sdrain,
6327 struct mlx5_ib_dev *dev)
6328{
6329 struct mlx5_core_dev *mdev = dev->mdev;
6330
6331 if (cq->poll_ctx == IB_POLL_DIRECT) {
6332 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
6333 ib_process_cq_direct(cq, -1);
6334 return;
6335 }
6336
6337 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6338 struct mlx5_ib_cq *mcq = to_mcq(cq);
6339 bool triggered = false;
6340 unsigned long flags;
6341
6342 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
6343 /* Make sure that the CQ handler won't run if wasn't run yet */
6344 if (!mcq->mcq.reset_notify_added)
6345 mcq->mcq.reset_notify_added = 1;
6346 else
6347 triggered = true;
6348 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
6349
6350 if (triggered) {
6351 /* Wait for any scheduled/running task to be ended */
6352 switch (cq->poll_ctx) {
6353 case IB_POLL_SOFTIRQ:
6354 irq_poll_disable(&cq->iop);
6355 irq_poll_enable(&cq->iop);
6356 break;
6357 case IB_POLL_WORKQUEUE:
6358 cancel_work_sync(&cq->work);
6359 break;
6360 default:
6361 WARN_ON_ONCE(1);
6362 }
6363 }
6364
6365 /* Run the CQ handler - this makes sure that the drain WR will
6366 * be processed if wasn't processed yet.
6367 */
6368 mcq->mcq.comp(&mcq->mcq);
6369 }
6370
6371 wait_for_completion(&sdrain->done);
6372}
6373
6374void mlx5_ib_drain_sq(struct ib_qp *qp)
6375{
6376 struct ib_cq *cq = qp->send_cq;
6377 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6378 struct mlx5_ib_drain_cqe sdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006379 const struct ib_send_wr *bad_swr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006380 struct ib_rdma_wr swr = {
6381 .wr = {
6382 .next = NULL,
6383 { .wr_cqe = &sdrain.cqe, },
6384 .opcode = IB_WR_RDMA_WRITE,
6385 },
6386 };
6387 int ret;
6388 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6389 struct mlx5_core_dev *mdev = dev->mdev;
6390
6391 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6392 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6393 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6394 return;
6395 }
6396
6397 sdrain.cqe.done = mlx5_ib_drain_qp_done;
6398 init_completion(&sdrain.done);
6399
6400 ret = _mlx5_ib_post_send(qp, &swr.wr, &bad_swr, true);
6401 if (ret) {
6402 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6403 return;
6404 }
6405
6406 handle_drain_completion(cq, &sdrain, dev);
6407}
6408
6409void mlx5_ib_drain_rq(struct ib_qp *qp)
6410{
6411 struct ib_cq *cq = qp->recv_cq;
6412 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6413 struct mlx5_ib_drain_cqe rdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006414 struct ib_recv_wr rwr = {};
6415 const struct ib_recv_wr *bad_rwr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006416 int ret;
6417 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6418 struct mlx5_core_dev *mdev = dev->mdev;
6419
6420 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6421 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6422 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6423 return;
6424 }
6425
6426 rwr.wr_cqe = &rdrain.cqe;
6427 rdrain.cqe.done = mlx5_ib_drain_qp_done;
6428 init_completion(&rdrain.done);
6429
6430 ret = _mlx5_ib_post_recv(qp, &rwr, &bad_rwr, true);
6431 if (ret) {
6432 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6433 return;
6434 }
6435
6436 handle_drain_completion(cq, &rdrain, dev);
6437}