blob: 879162da63e3f2cfd158e1dbaafd9376eafec5a9 [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
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300499 if (attr->create_flags & IB_QP_CREATE_INTEGRITY_EN)
500 qp->integrity_en = true;
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200501
Eli Cohene126ba92013-07-07 17:25:49 +0300502 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
503 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300504 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800505 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
506 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300507 qp->sq.wqe_cnt,
508 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300509 return -ENOMEM;
510 }
Eli Cohene126ba92013-07-07 17:25:49 +0300511 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300512 qp->sq.max_gs = get_send_sge(attr, wqe_size);
513 if (qp->sq.max_gs < attr->cap.max_send_sge)
514 return -ENOMEM;
515
516 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300517 qp->sq.max_post = wq_size / wqe_size;
518 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300519
520 return wq_size;
521}
522
523static int set_user_buf_size(struct mlx5_ib_dev *dev,
524 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200525 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200526 struct mlx5_ib_qp_base *base,
527 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300528{
529 int desc_sz = 1 << qp->sq.wqe_shift;
530
Saeed Mahameed938fe832015-05-28 22:28:41 +0300531 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300532 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300533 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300534 return -EINVAL;
535 }
536
Gal Pressmanaf8b38e2019-02-06 15:45:35 +0200537 if (ucmd->sq_wqe_count && !is_power_of_2(ucmd->sq_wqe_count)) {
538 mlx5_ib_warn(dev, "sq_wqe_count %d is not a power of two\n",
539 ucmd->sq_wqe_count);
Eli Cohene126ba92013-07-07 17:25:49 +0300540 return -EINVAL;
541 }
542
543 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
544
Saeed Mahameed938fe832015-05-28 22:28:41 +0300545 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300546 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300547 qp->sq.wqe_cnt,
548 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300549 return -EINVAL;
550 }
551
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300552 if (attr->qp_type == IB_QPT_RAW_PACKET ||
553 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200554 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
555 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
556 } else {
557 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
558 (qp->sq.wqe_cnt << 6);
559 }
Eli Cohene126ba92013-07-07 17:25:49 +0300560
561 return 0;
562}
563
564static int qp_has_rq(struct ib_qp_init_attr *attr)
565{
566 if (attr->qp_type == IB_QPT_XRC_INI ||
567 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
568 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
569 !attr->cap.max_recv_wr)
570 return 0;
571
572 return 1;
573}
574
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200575enum {
576 /* this is the first blue flame register in the array of bfregs assigned
577 * to a processes. Since we do not use it for blue flame but rather
578 * regular 64 bit doorbells, we do not need a lock for maintaiing
579 * "odd/even" order
580 */
581 NUM_NON_BLUE_FLAME_BFREGS = 1,
582};
583
Eli Cohenb037c292017-01-03 23:55:26 +0200584static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
585{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200586 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200587}
588
589static int num_med_bfreg(struct mlx5_ib_dev *dev,
590 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200591{
592 int n;
593
Eli Cohenb037c292017-01-03 23:55:26 +0200594 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
595 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200596
597 return n >= 0 ? n : 0;
598}
599
Yishai Hadas18b03622018-05-07 10:20:01 +0300600static int first_med_bfreg(struct mlx5_ib_dev *dev,
601 struct mlx5_bfreg_info *bfregi)
602{
603 return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
604}
605
Eli Cohenb037c292017-01-03 23:55:26 +0200606static int first_hi_bfreg(struct mlx5_ib_dev *dev,
607 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200608{
609 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200610
Eli Cohenb037c292017-01-03 23:55:26 +0200611 med = num_med_bfreg(dev, bfregi);
612 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200613}
614
Eli Cohenb037c292017-01-03 23:55:26 +0200615static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
616 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300617{
Eli Cohene126ba92013-07-07 17:25:49 +0300618 int i;
619
Eli Cohenb037c292017-01-03 23:55:26 +0200620 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
621 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200622 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300623 return i;
624 }
625 }
626
627 return -ENOMEM;
628}
629
Eli Cohenb037c292017-01-03 23:55:26 +0200630static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
631 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300632{
Yishai Hadas18b03622018-05-07 10:20:01 +0300633 int minidx = first_med_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300634 int i;
635
Yishai Hadas18b03622018-05-07 10:20:01 +0300636 if (minidx < 0)
637 return minidx;
638
639 for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200640 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300641 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200642 if (!bfregi->count[minidx])
643 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300644 }
645
Eli Cohen2f5ff262017-01-03 23:55:21 +0200646 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300647 return minidx;
648}
649
Eli Cohenb037c292017-01-03 23:55:26 +0200650static int alloc_bfreg(struct mlx5_ib_dev *dev,
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300651 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300652{
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300653 int bfregn = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +0300654
Eli Cohen2f5ff262017-01-03 23:55:21 +0200655 mutex_lock(&bfregi->lock);
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300656 if (bfregi->ver >= 2) {
657 bfregn = alloc_high_class_bfreg(dev, bfregi);
658 if (bfregn < 0)
659 bfregn = alloc_med_class_bfreg(dev, bfregi);
660 }
661
662 if (bfregn < 0) {
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200663 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200664 bfregn = 0;
665 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300666 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200667 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300668
Eli Cohen2f5ff262017-01-03 23:55:21 +0200669 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300670}
671
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200672void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300673{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200674 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200675 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200676 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300677}
678
679static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
680{
681 switch (state) {
682 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
683 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
684 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
685 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
686 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
687 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
688 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
689 default: return -1;
690 }
691}
692
693static int to_mlx5_st(enum ib_qp_type type)
694{
695 switch (type) {
696 case IB_QPT_RC: return MLX5_QP_ST_RC;
697 case IB_QPT_UC: return MLX5_QP_ST_UC;
698 case IB_QPT_UD: return MLX5_QP_ST_UD;
699 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
700 case IB_QPT_XRC_INI:
701 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
702 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200703 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200704 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300705 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300706 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200707 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300708 case IB_QPT_MAX:
709 default: return -EINVAL;
710 }
711}
712
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300713static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
714 struct mlx5_ib_cq *recv_cq);
715static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
716 struct mlx5_ib_cq *recv_cq);
717
Yishai Hadas7c043e92018-06-17 13:00:03 +0300718int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300719 struct mlx5_bfreg_info *bfregi, u32 bfregn,
Yishai Hadas7c043e92018-06-17 13:00:03 +0300720 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300721{
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300722 unsigned int bfregs_per_sys_page;
723 u32 index_of_sys_page;
724 u32 offset;
Eli Cohenb037c292017-01-03 23:55:26 +0200725
726 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
727 MLX5_NON_FP_BFREGS_PER_UAR;
728 index_of_sys_page = bfregn / bfregs_per_sys_page;
729
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200730 if (dyn_bfreg) {
731 index_of_sys_page += bfregi->num_static_sys_pages;
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300732
733 if (index_of_sys_page >= bfregi->num_sys_pages)
734 return -EINVAL;
735
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200736 if (bfregn > bfregi->num_dyn_bfregs ||
737 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
738 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
739 return -EINVAL;
740 }
741 }
Eli Cohenb037c292017-01-03 23:55:26 +0200742
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200743 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200744 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300745}
746
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200747static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200748 unsigned long addr, size_t size,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200749 struct ib_umem **umem, int *npages, int *page_shift,
750 int *ncont, u32 *offset)
majd@mellanox.com19098df2016-01-14 19:13:03 +0200751{
752 int err;
753
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200754 *umem = ib_umem_get(udata, addr, size, 0, 0);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200755 if (IS_ERR(*umem)) {
756 mlx5_ib_dbg(dev, "umem_get failed\n");
757 return PTR_ERR(*umem);
758 }
759
Majd Dibbiny762f8992016-10-27 16:36:47 +0300760 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200761
762 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
763 if (err) {
764 mlx5_ib_warn(dev, "bad offset\n");
765 goto err_umem;
766 }
767
768 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
769 addr, size, *npages, *page_shift, *ncont, *offset);
770
771 return 0;
772
773err_umem:
774 ib_umem_release(*umem);
775 *umem = NULL;
776
777 return err;
778}
779
Maor Gottliebfe248c32017-05-30 10:29:14 +0300780static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300781 struct mlx5_ib_rwq *rwq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300782{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300783 struct mlx5_ib_ucontext *context =
784 rdma_udata_to_drv_context(
785 udata,
786 struct mlx5_ib_ucontext,
787 ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300788
Maor Gottliebfe248c32017-05-30 10:29:14 +0300789 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
790 atomic_dec(&dev->delay_drop.rqs_cnt);
791
Yishai Hadas79b20a62016-05-23 15:20:50 +0300792 mlx5_ib_db_unmap_user(context, &rwq->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300793 ib_umem_release(rwq->umem);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300794}
795
796static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200797 struct ib_udata *udata, struct mlx5_ib_rwq *rwq,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300798 struct mlx5_ib_create_wq *ucmd)
799{
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200800 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
801 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300802 int page_shift = 0;
803 int npages;
804 u32 offset = 0;
805 int ncont = 0;
806 int err;
807
808 if (!ucmd->buf_addr)
809 return -EINVAL;
810
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200811 rwq->umem = ib_umem_get(udata, ucmd->buf_addr, rwq->buf_size, 0, 0);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300812 if (IS_ERR(rwq->umem)) {
813 mlx5_ib_dbg(dev, "umem_get failed\n");
814 err = PTR_ERR(rwq->umem);
815 return err;
816 }
817
Majd Dibbiny762f8992016-10-27 16:36:47 +0300818 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300819 &ncont, NULL);
820 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
821 &rwq->rq_page_offset);
822 if (err) {
823 mlx5_ib_warn(dev, "bad offset\n");
824 goto err_umem;
825 }
826
827 rwq->rq_num_pas = ncont;
828 rwq->page_shift = page_shift;
829 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
830 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
831
832 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
833 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
834 npages, page_shift, ncont, offset);
835
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200836 err = mlx5_ib_db_map_user(ucontext, udata, ucmd->db_addr, &rwq->db);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300837 if (err) {
838 mlx5_ib_dbg(dev, "map failed\n");
839 goto err_umem;
840 }
841
842 rwq->create_type = MLX5_WQ_USER;
843 return 0;
844
845err_umem:
846 ib_umem_release(rwq->umem);
847 return err;
848}
849
Eli Cohenb037c292017-01-03 23:55:26 +0200850static int adjust_bfregn(struct mlx5_ib_dev *dev,
851 struct mlx5_bfreg_info *bfregi, int bfregn)
852{
853 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
854 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
855}
856
Eli Cohene126ba92013-07-07 17:25:49 +0300857static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
858 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200859 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300860 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200861 struct mlx5_ib_create_qp_resp *resp, int *inlen,
862 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300863{
864 struct mlx5_ib_ucontext *context;
865 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200866 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200867 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200868 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300869 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200870 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200871 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200872 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300873 __be64 *pas;
874 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300875 int err;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200876 u16 uid;
Eli Cohene126ba92013-07-07 17:25:49 +0300877
878 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
879 if (err) {
880 mlx5_ib_dbg(dev, "copy failed\n");
881 return err;
882 }
883
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200884 context = rdma_udata_to_drv_context(udata, struct mlx5_ib_ucontext,
885 ibucontext);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200886 if (ucmd.flags & MLX5_QP_FLAG_BFREG_INDEX) {
887 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
888 ucmd.bfreg_index, true);
889 if (uar_index < 0)
890 return uar_index;
891
892 bfregn = MLX5_IB_INVALID_BFREG;
893 } else if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL) {
894 /*
895 * TBD: should come from the verbs when we have the API
896 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200897 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200898 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200899 }
Leon Romanovsky051f2632015-12-20 12:16:11 +0200900 else {
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300901 bfregn = alloc_bfreg(dev, &context->bfregi);
902 if (bfregn < 0)
903 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300904 }
905
Eli Cohen2f5ff262017-01-03 23:55:21 +0200906 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200907 if (bfregn != MLX5_IB_INVALID_BFREG)
908 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
909 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300910
Haggai Eran48fea832014-05-22 14:50:11 +0300911 qp->rq.offset = 0;
912 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
913 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
914
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200915 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300916 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200917 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300918
majd@mellanox.com19098df2016-01-14 19:13:03 +0200919 if (ucmd.buf_addr && ubuffer->buf_size) {
920 ubuffer->buf_addr = ucmd.buf_addr;
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200921 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr,
922 ubuffer->buf_size, &ubuffer->umem,
923 &npages, &page_shift, &ncont, &offset);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200924 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200925 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200926 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200927 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300928 }
Eli Cohene126ba92013-07-07 17:25:49 +0300929
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300930 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
931 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300932 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300933 if (!*in) {
934 err = -ENOMEM;
935 goto err_umem;
936 }
Eli Cohene126ba92013-07-07 17:25:49 +0300937
Yishai Hadas7422edc2018-12-23 13:12:21 +0200938 uid = (attr->qp_type != IB_QPT_XRC_TGT &&
939 attr->qp_type != IB_QPT_XRC_INI) ? to_mpd(pd)->uid : 0;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200940 MLX5_SET(create_qp_in, *in, uid, uid);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300941 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
942 if (ubuffer->umem)
943 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
944
945 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
946
947 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
948 MLX5_SET(qpc, qpc, page_offset, offset);
949
950 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200951 if (bfregn != MLX5_IB_INVALID_BFREG)
952 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
953 else
954 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200955 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300956
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200957 err = mlx5_ib_db_map_user(context, udata, ucmd.db_addr, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300958 if (err) {
959 mlx5_ib_dbg(dev, "map failed\n");
960 goto err_free;
961 }
962
Jason Gunthorpe41d902c2018-04-03 10:00:53 +0300963 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
Eli Cohene126ba92013-07-07 17:25:49 +0300964 if (err) {
965 mlx5_ib_dbg(dev, "copy failed\n");
966 goto err_unmap;
967 }
968 qp->create_type = MLX5_QP_USER;
969
970 return 0;
971
972err_unmap:
973 mlx5_ib_db_unmap_user(context, &qp->db);
974
975err_free:
Al Viro479163f2014-11-20 08:13:57 +0000976 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300977
978err_umem:
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300979 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300980
Eli Cohen2f5ff262017-01-03 23:55:21 +0200981err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200982 if (bfregn != MLX5_IB_INVALID_BFREG)
983 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300984 return err;
985}
986
Eli Cohenb037c292017-01-03 23:55:26 +0200987static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300988 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base,
989 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +0300990{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300991 struct mlx5_ib_ucontext *context =
992 rdma_udata_to_drv_context(
993 udata,
994 struct mlx5_ib_ucontext,
995 ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +0300996
Eli Cohene126ba92013-07-07 17:25:49 +0300997 mlx5_ib_db_unmap_user(context, &qp->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300998 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200999
1000 /*
1001 * Free only the BFREGs which are handled by the kernel.
1002 * BFREGs of UARs allocated dynamically are handled by user.
1003 */
1004 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
1005 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001006}
1007
Guy Levi34f4c952018-11-26 08:15:50 +02001008/* get_sq_edge - Get the next nearby edge.
1009 *
1010 * An 'edge' is defined as the first following address after the end
1011 * of the fragment or the SQ. Accordingly, during the WQE construction
1012 * which repetitively increases the pointer to write the next data, it
1013 * simply should check if it gets to an edge.
1014 *
1015 * @sq - SQ buffer.
1016 * @idx - Stride index in the SQ buffer.
1017 *
1018 * Return:
1019 * The new edge.
1020 */
1021static void *get_sq_edge(struct mlx5_ib_wq *sq, u32 idx)
1022{
1023 void *fragment_end;
1024
1025 fragment_end = mlx5_frag_buf_get_wqe
1026 (&sq->fbc,
1027 mlx5_frag_buf_get_idx_last_contig_stride(&sq->fbc, idx));
1028
1029 return fragment_end + MLX5_SEND_WQE_BB;
1030}
1031
Eli Cohene126ba92013-07-07 17:25:49 +03001032static int create_kernel_qp(struct mlx5_ib_dev *dev,
1033 struct ib_qp_init_attr *init_attr,
1034 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001035 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +02001036 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +03001037{
Eli Cohene126ba92013-07-07 17:25:49 +03001038 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001039 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +03001040 int err;
1041
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +03001042 if (init_attr->create_flags & ~(IB_QP_CREATE_INTEGRITY_EN |
Erez Shitritf0313962016-02-21 16:27:17 +02001043 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +02001044 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +03001045 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +02001046 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +02001047 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001048
1049 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001050 qp->bf.bfreg = &dev->fp_bfreg;
1051 else
1052 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +03001053
Eli Cohend8030b02017-02-09 19:31:47 +02001054 /* We need to divide by two since each register is comprised of
1055 * two buffers of identical size, namely odd and even
1056 */
1057 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001058 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +03001059
1060 err = calc_sq_size(dev, init_attr, qp);
1061 if (err < 0) {
1062 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001063 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001064 }
1065
1066 qp->rq.offset = 0;
1067 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001068 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +03001069
Guy Levi34f4c952018-11-26 08:15:50 +02001070 err = mlx5_frag_buf_alloc_node(dev->mdev, base->ubuffer.buf_size,
1071 &qp->buf, dev->mdev->priv.numa_node);
Eli Cohene126ba92013-07-07 17:25:49 +03001072 if (err) {
1073 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001074 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001075 }
1076
Guy Levi34f4c952018-11-26 08:15:50 +02001077 if (qp->rq.wqe_cnt)
1078 mlx5_init_fbc(qp->buf.frags, qp->rq.wqe_shift,
1079 ilog2(qp->rq.wqe_cnt), &qp->rq.fbc);
1080
1081 if (qp->sq.wqe_cnt) {
1082 int sq_strides_offset = (qp->sq.offset & (PAGE_SIZE - 1)) /
1083 MLX5_SEND_WQE_BB;
1084 mlx5_init_fbc_offset(qp->buf.frags +
1085 (qp->sq.offset / PAGE_SIZE),
1086 ilog2(MLX5_SEND_WQE_BB),
1087 ilog2(qp->sq.wqe_cnt),
1088 sq_strides_offset, &qp->sq.fbc);
1089
1090 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
1091 }
1092
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001093 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
1094 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001095 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001096 if (!*in) {
1097 err = -ENOMEM;
1098 goto err_buf;
1099 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001100
1101 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1102 MLX5_SET(qpc, qpc, uar_page, uar_index);
1103 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1104
Eli Cohene126ba92013-07-07 17:25:49 +03001105 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001106 MLX5_SET(qpc, qpc, fre, 1);
1107 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001108
Haggai Eranb11a4f92016-02-29 15:45:03 +02001109 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001110 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +02001111 qp->flags |= MLX5_IB_QP_SQPN_QP1;
1112 }
1113
Guy Levi34f4c952018-11-26 08:15:50 +02001114 mlx5_fill_page_frag_array(&qp->buf,
1115 (__be64 *)MLX5_ADDR_OF(create_qp_in,
1116 *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +03001117
Jack Morgenstein9603b612014-07-28 23:30:22 +03001118 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001119 if (err) {
1120 mlx5_ib_dbg(dev, "err %d\n", err);
1121 goto err_free;
1122 }
1123
Li Dongyangb5883002017-08-16 23:31:22 +10001124 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
1125 sizeof(*qp->sq.wrid), GFP_KERNEL);
1126 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1127 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1128 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1129 sizeof(*qp->rq.wrid), GFP_KERNEL);
1130 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1131 sizeof(*qp->sq.w_list), GFP_KERNEL);
1132 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1133 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001134
1135 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1136 !qp->sq.w_list || !qp->sq.wqe_head) {
1137 err = -ENOMEM;
1138 goto err_wrid;
1139 }
1140 qp->create_type = MLX5_QP_KERNEL;
1141
1142 return 0;
1143
1144err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001145 kvfree(qp->sq.wqe_head);
1146 kvfree(qp->sq.w_list);
1147 kvfree(qp->sq.wrid);
1148 kvfree(qp->sq.wr_data);
1149 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001150 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001151
1152err_free:
Al Viro479163f2014-11-20 08:13:57 +00001153 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001154
1155err_buf:
Guy Levi34f4c952018-11-26 08:15:50 +02001156 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001157 return err;
1158}
1159
1160static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1161{
Li Dongyangb5883002017-08-16 23:31:22 +10001162 kvfree(qp->sq.wqe_head);
1163 kvfree(qp->sq.w_list);
1164 kvfree(qp->sq.wrid);
1165 kvfree(qp->sq.wr_data);
1166 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001167 mlx5_db_free(dev->mdev, &qp->db);
Guy Levi34f4c952018-11-26 08:15:50 +02001168 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001169}
1170
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001171static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001172{
1173 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Moni Shouac32a4f22018-01-02 16:19:32 +02001174 (attr->qp_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001175 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001176 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001177 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001178 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001179 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001180 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001181}
1182
1183static int is_connected(enum ib_qp_type qp_type)
1184{
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001185 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC ||
1186 qp_type == MLX5_IB_QPT_DCI)
Eli Cohene126ba92013-07-07 17:25:49 +03001187 return 1;
1188
1189 return 0;
1190}
1191
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001192static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001193 struct mlx5_ib_qp *qp,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001194 struct mlx5_ib_sq *sq, u32 tdn,
1195 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001196{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001197 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001198 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1199
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001200 MLX5_SET(create_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001201 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001202 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1203 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1204
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001205 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1206}
1207
1208static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001209 struct mlx5_ib_sq *sq, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001210{
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001211 mlx5_cmd_destroy_tis(dev->mdev, sq->tisn, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001212}
1213
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001214static void destroy_flow_rule_vport_sq(struct mlx5_ib_sq *sq)
Mark Blochb96c9dd2018-01-29 10:40:37 +00001215{
1216 if (sq->flow_rule)
1217 mlx5_del_flow_rules(sq->flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001218 sq->flow_rule = NULL;
Mark Blochb96c9dd2018-01-29 10:40:37 +00001219}
1220
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001221static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001222 struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001223 struct mlx5_ib_sq *sq, void *qpin,
1224 struct ib_pd *pd)
1225{
1226 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1227 __be64 *pas;
1228 void *in;
1229 void *sqc;
1230 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1231 void *wq;
1232 int inlen;
1233 int err;
1234 int page_shift = 0;
1235 int npages;
1236 int ncont = 0;
1237 u32 offset = 0;
1238
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001239 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr, ubuffer->buf_size,
1240 &sq->ubuffer.umem, &npages, &page_shift, &ncont,
1241 &offset);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001242 if (err)
1243 return err;
1244
1245 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001246 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001247 if (!in) {
1248 err = -ENOMEM;
1249 goto err_umem;
1250 }
1251
Yishai Hadasc14003f2018-09-20 21:39:22 +03001252 MLX5_SET(create_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001253 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1254 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001255 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1256 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001257 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1258 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1259 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1260 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1261 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001262 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1263 MLX5_CAP_ETH(dev->mdev, swp))
1264 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001265
1266 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1267 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1268 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1269 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1270 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1271 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1272 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1273 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1274 MLX5_SET(wq, wq, page_offset, offset);
1275
1276 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1277 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1278
1279 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1280
1281 kvfree(in);
1282
1283 if (err)
1284 goto err_umem;
1285
1286 return 0;
1287
1288err_umem:
1289 ib_umem_release(sq->ubuffer.umem);
1290 sq->ubuffer.umem = NULL;
1291
1292 return err;
1293}
1294
1295static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1296 struct mlx5_ib_sq *sq)
1297{
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001298 destroy_flow_rule_vport_sq(sq);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001299 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1300 ib_umem_release(sq->ubuffer.umem);
1301}
1302
Boris Pismenny2c292db2018-03-08 15:51:40 +02001303static size_t get_rq_pas_size(void *qpc)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001304{
1305 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1306 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1307 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1308 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1309 u32 po_quanta = 1 << (log_page_size - 6);
1310 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1311 u32 page_size = 1 << log_page_size;
1312 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1313 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1314
1315 return rq_num_pas * sizeof(u64);
1316}
1317
1318static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001319 struct mlx5_ib_rq *rq, void *qpin,
Yishai Hadas34d57582018-09-20 21:39:21 +03001320 size_t qpinlen, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001321{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001322 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001323 __be64 *pas;
1324 __be64 *qp_pas;
1325 void *in;
1326 void *rqc;
1327 void *wq;
1328 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001329 size_t rq_pas_size = get_rq_pas_size(qpc);
1330 size_t inlen;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001331 int err;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001332
1333 if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
1334 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001335
1336 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001337 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001338 if (!in)
1339 return -ENOMEM;
1340
Yishai Hadas34d57582018-09-20 21:39:21 +03001341 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001342 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001343 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1344 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001345 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1346 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1347 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1348 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1349 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1350
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001351 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1352 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1353
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001354 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1355 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001356 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1357 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001358 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1359 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1360 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1361 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1362 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1363 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1364
1365 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1366 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1367 memcpy(pas, qp_pas, rq_pas_size);
1368
1369 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1370
1371 kvfree(in);
1372
1373 return err;
1374}
1375
1376static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1377 struct mlx5_ib_rq *rq)
1378{
1379 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1380}
1381
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001382static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1383{
1384 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1385 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1386 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1387}
1388
Mark Bloch0042f9e2018-09-17 13:30:49 +03001389static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1390 struct mlx5_ib_rq *rq,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001391 u32 qp_flags_en,
1392 struct ib_pd *pd)
Mark Bloch0042f9e2018-09-17 13:30:49 +03001393{
1394 if (qp_flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1395 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1396 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001397 mlx5_cmd_destroy_tir(dev->mdev, rq->tirn, to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001398}
1399
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001400static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001401 struct mlx5_ib_rq *rq, u32 tdn,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001402 u32 *qp_flags_en,
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001403 struct ib_pd *pd,
1404 u32 *out, int outlen)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001405{
Mark Bloch175edba2018-09-17 13:30:48 +03001406 u8 lb_flag = 0;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001407 u32 *in;
1408 void *tirc;
1409 int inlen;
1410 int err;
1411
1412 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001413 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001414 if (!in)
1415 return -ENOMEM;
1416
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001417 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001418 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1419 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1420 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1421 MLX5_SET(tirc, tirc, transport_domain, tdn);
Mark Bloch175edba2018-09-17 13:30:48 +03001422 if (*qp_flags_en & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001423 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001424
Mark Bloch175edba2018-09-17 13:30:48 +03001425 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
1426 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1427
1428 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
1429 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1430
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001431 if (dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001432 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1433 *qp_flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1434 }
1435
1436 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
Mark Blochec9c2fb2018-01-15 13:11:37 +00001437
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001438 err = mlx5_core_create_tir_out(dev->mdev, in, inlen, out, outlen);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001439
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001440 rq->tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001441 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1442 err = mlx5_ib_enable_lb(dev, false, true);
1443
1444 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001445 destroy_raw_packet_qp_tir(dev, rq, 0, pd);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001446 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001447 kvfree(in);
1448
1449 return err;
1450}
1451
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001452static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001453 u32 *in, size_t inlen,
Yishai Hadas7f720522018-09-20 21:45:18 +03001454 struct ib_pd *pd,
1455 struct ib_udata *udata,
1456 struct mlx5_ib_create_qp_resp *resp)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001457{
1458 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1459 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1460 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001461 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1462 udata, struct mlx5_ib_ucontext, ibucontext);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001463 int err;
1464 u32 tdn = mucontext->tdn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001465 u16 uid = to_mpd(pd)->uid;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001466 u32 out[MLX5_ST_SZ_DW(create_tir_out)] = {};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001467
1468 if (qp->sq.wqe_cnt) {
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001469 err = create_raw_packet_qp_tis(dev, qp, sq, tdn, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001470 if (err)
1471 return err;
1472
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001473 err = create_raw_packet_qp_sq(dev, udata, sq, in, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001474 if (err)
1475 goto err_destroy_tis;
1476
Yishai Hadas7f720522018-09-20 21:45:18 +03001477 if (uid) {
1478 resp->tisn = sq->tisn;
1479 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TISN;
1480 resp->sqn = sq->base.mqp.qpn;
1481 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_SQN;
1482 }
1483
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001484 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001485 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001486 }
1487
1488 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001489 rq->base.container_mibqp = qp;
1490
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001491 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1492 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001493 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1494 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
Yishai Hadas34d57582018-09-20 21:39:21 +03001495 err = create_raw_packet_qp_rq(dev, rq, in, inlen, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001496 if (err)
1497 goto err_destroy_sq;
1498
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001499 err = create_raw_packet_qp_tir(
1500 dev, rq, tdn, &qp->flags_en, pd, out,
1501 MLX5_ST_SZ_BYTES(create_tir_out));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001502 if (err)
1503 goto err_destroy_rq;
Yishai Hadas7f720522018-09-20 21:45:18 +03001504
1505 if (uid) {
1506 resp->rqn = rq->base.mqp.qpn;
1507 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_RQN;
1508 resp->tirn = rq->tirn;
1509 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001510 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1511 resp->tir_icm_addr = MLX5_GET(
1512 create_tir_out, out, icm_address_31_0);
1513 resp->tir_icm_addr |=
1514 (u64)MLX5_GET(create_tir_out, out,
1515 icm_address_39_32)
1516 << 32;
1517 resp->tir_icm_addr |=
1518 (u64)MLX5_GET(create_tir_out, out,
1519 icm_address_63_40)
1520 << 40;
1521 resp->comp_mask |=
1522 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1523 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001524 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001525 }
1526
1527 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1528 rq->base.mqp.qpn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001529 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
1530 if (err)
1531 goto err_destroy_tir;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001532
1533 return 0;
1534
Yishai Hadas7f720522018-09-20 21:45:18 +03001535err_destroy_tir:
1536 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001537err_destroy_rq:
1538 destroy_raw_packet_qp_rq(dev, rq);
1539err_destroy_sq:
1540 if (!qp->sq.wqe_cnt)
1541 return err;
1542 destroy_raw_packet_qp_sq(dev, sq);
1543err_destroy_tis:
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001544 destroy_raw_packet_qp_tis(dev, sq, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001545
1546 return err;
1547}
1548
1549static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1550 struct mlx5_ib_qp *qp)
1551{
1552 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1553 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1554 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1555
1556 if (qp->rq.wqe_cnt) {
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001557 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001558 destroy_raw_packet_qp_rq(dev, rq);
1559 }
1560
1561 if (qp->sq.wqe_cnt) {
1562 destroy_raw_packet_qp_sq(dev, sq);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001563 destroy_raw_packet_qp_tis(dev, sq, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001564 }
1565}
1566
1567static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1568 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1569{
1570 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1571 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1572
1573 sq->sq = &qp->sq;
1574 rq->rq = &qp->rq;
1575 sq->doorbell = &qp->db;
1576 rq->doorbell = &qp->db;
1577}
1578
Yishai Hadas28d61372016-05-23 15:20:56 +03001579static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1580{
Mark Bloch0042f9e2018-09-17 13:30:49 +03001581 if (qp->flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1582 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1583 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001584 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1585 to_mpd(qp->ibqp.pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001586}
1587
1588static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1589 struct ib_pd *pd,
1590 struct ib_qp_init_attr *init_attr,
1591 struct ib_udata *udata)
1592{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001593 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1594 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas28d61372016-05-23 15:20:56 +03001595 struct mlx5_ib_create_qp_resp resp = {};
1596 int inlen;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001597 int outlen;
Yishai Hadas28d61372016-05-23 15:20:56 +03001598 int err;
1599 u32 *in;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001600 u32 *out;
Yishai Hadas28d61372016-05-23 15:20:56 +03001601 void *tirc;
1602 void *hfso;
1603 u32 selected_fields = 0;
Matan Barak2d93fc82018-03-28 09:27:55 +03001604 u32 outer_l4;
Yishai Hadas28d61372016-05-23 15:20:56 +03001605 size_t min_resp_len;
1606 u32 tdn = mucontext->tdn;
1607 struct mlx5_ib_create_qp_rss ucmd = {};
1608 size_t required_cmd_sz;
Mark Bloch175edba2018-09-17 13:30:48 +03001609 u8 lb_flag = 0;
Yishai Hadas28d61372016-05-23 15:20:56 +03001610
1611 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1612 return -EOPNOTSUPP;
1613
1614 if (init_attr->create_flags || init_attr->send_cq)
1615 return -EINVAL;
1616
Eli Cohen2f5ff262017-01-03 23:55:21 +02001617 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001618 if (udata->outlen < min_resp_len)
1619 return -EINVAL;
1620
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001621 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001622 if (udata->inlen < required_cmd_sz) {
1623 mlx5_ib_dbg(dev, "invalid inlen\n");
1624 return -EINVAL;
1625 }
1626
1627 if (udata->inlen > sizeof(ucmd) &&
1628 !ib_is_udata_cleared(udata, sizeof(ucmd),
1629 udata->inlen - sizeof(ucmd))) {
1630 mlx5_ib_dbg(dev, "inlen is not supported\n");
1631 return -EOPNOTSUPP;
1632 }
1633
1634 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1635 mlx5_ib_dbg(dev, "copy failed\n");
1636 return -EFAULT;
1637 }
1638
1639 if (ucmd.comp_mask) {
1640 mlx5_ib_dbg(dev, "invalid comp mask\n");
1641 return -EOPNOTSUPP;
1642 }
1643
Mark Bloch175edba2018-09-17 13:30:48 +03001644 if (ucmd.flags & ~(MLX5_QP_FLAG_TUNNEL_OFFLOADS |
1645 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1646 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)) {
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001647 mlx5_ib_dbg(dev, "invalid flags\n");
1648 return -EOPNOTSUPP;
1649 }
1650
1651 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1652 !tunnel_offload_supported(dev->mdev)) {
1653 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001654 return -EOPNOTSUPP;
1655 }
1656
Maor Gottlieb309fa342017-10-19 08:25:56 +03001657 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1658 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1659 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1660 return -EOPNOTSUPP;
1661 }
1662
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001663 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC || dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001664 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1665 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1666 }
1667
1668 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
1669 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1670 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
1671 }
1672
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001673 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
Yishai Hadas28d61372016-05-23 15:20:56 +03001674 if (err) {
1675 mlx5_ib_dbg(dev, "copy failed\n");
1676 return -EINVAL;
1677 }
1678
1679 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001680 outlen = MLX5_ST_SZ_BYTES(create_tir_out);
1681 in = kvzalloc(inlen + outlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001682 if (!in)
1683 return -ENOMEM;
1684
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001685 out = in + MLX5_ST_SZ_DW(create_tir_in);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001686 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001687 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1688 MLX5_SET(tirc, tirc, disp_type,
1689 MLX5_TIRC_DISP_TYPE_INDIRECT);
1690 MLX5_SET(tirc, tirc, indirect_table,
1691 init_attr->rwq_ind_tbl->ind_tbl_num);
1692 MLX5_SET(tirc, tirc, transport_domain, tdn);
1693
1694 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001695
1696 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1697 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1698
Mark Bloch175edba2018-09-17 13:30:48 +03001699 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
1700
Maor Gottlieb309fa342017-10-19 08:25:56 +03001701 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1702 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1703 else
1704 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1705
Yishai Hadas28d61372016-05-23 15:20:56 +03001706 switch (ucmd.rx_hash_function) {
1707 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1708 {
1709 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1710 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1711
1712 if (len != ucmd.rx_key_len) {
1713 err = -EINVAL;
1714 goto err;
1715 }
1716
1717 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1718 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1719 memcpy(rss_key, ucmd.rx_hash_key, len);
1720 break;
1721 }
1722 default:
1723 err = -EOPNOTSUPP;
1724 goto err;
1725 }
1726
1727 if (!ucmd.rx_hash_fields_mask) {
1728 /* special case when this TIR serves as steering entry without hashing */
1729 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1730 goto create_tir;
1731 err = -EINVAL;
1732 goto err;
1733 }
1734
1735 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1736 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1737 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1738 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1739 err = -EINVAL;
1740 goto err;
1741 }
1742
1743 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1744 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1745 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1746 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1747 MLX5_L3_PROT_TYPE_IPV4);
1748 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1749 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1750 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1751 MLX5_L3_PROT_TYPE_IPV6);
1752
Matan Barak2d93fc82018-03-28 09:27:55 +03001753 outer_l4 = ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1754 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) << 0 |
1755 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1756 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP)) << 1 |
1757 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI) << 2;
1758
1759 /* Check that only one l4 protocol is set */
1760 if (outer_l4 & (outer_l4 - 1)) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001761 err = -EINVAL;
1762 goto err;
1763 }
1764
1765 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1766 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1767 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1768 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1769 MLX5_L4_PROT_TYPE_TCP);
1770 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1771 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1772 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1773 MLX5_L4_PROT_TYPE_UDP);
1774
1775 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1776 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1777 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1778
1779 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1780 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1781 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1782
1783 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1784 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1785 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1786
1787 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1788 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1789 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1790
Matan Barak2d93fc82018-03-28 09:27:55 +03001791 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI)
1792 selected_fields |= MLX5_HASH_FIELD_SEL_IPSEC_SPI;
1793
Yishai Hadas28d61372016-05-23 15:20:56 +03001794 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1795
1796create_tir:
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001797 err = mlx5_core_create_tir_out(dev->mdev, in, inlen, out, outlen);
Yishai Hadas28d61372016-05-23 15:20:56 +03001798
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001799 qp->rss_qp.tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001800 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1801 err = mlx5_ib_enable_lb(dev, false, true);
1802
1803 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001804 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1805 to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001806 }
1807
Yishai Hadas28d61372016-05-23 15:20:56 +03001808 if (err)
1809 goto err;
1810
Yishai Hadas7f720522018-09-20 21:45:18 +03001811 if (mucontext->devx_uid) {
1812 resp.comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1813 resp.tirn = qp->rss_qp.tirn;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001814 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1815 resp.tir_icm_addr =
1816 MLX5_GET(create_tir_out, out, icm_address_31_0);
1817 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1818 icm_address_39_32)
1819 << 32;
1820 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1821 icm_address_63_40)
1822 << 40;
1823 resp.comp_mask |=
1824 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1825 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001826 }
1827
1828 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
1829 if (err)
1830 goto err_copy;
1831
Yishai Hadas28d61372016-05-23 15:20:56 +03001832 kvfree(in);
1833 /* qpn is reserved for that QP */
1834 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001835 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001836 return 0;
1837
Yishai Hadas7f720522018-09-20 21:45:18 +03001838err_copy:
1839 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn, mucontext->devx_uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001840err:
1841 kvfree(in);
1842 return err;
1843}
1844
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001845static void configure_responder_scat_cqe(struct ib_qp_init_attr *init_attr,
1846 void *qpc)
1847{
1848 int rcqe_sz;
1849
1850 if (init_attr->qp_type == MLX5_IB_QPT_DCI)
1851 return;
1852
1853 rcqe_sz = mlx5_ib_get_cqe_size(init_attr->recv_cq);
1854
Guy Levi7249c8e2019-04-10 10:59:45 +03001855 if (init_attr->qp_type == MLX5_IB_QPT_DCT) {
1856 if (rcqe_sz == 128)
1857 MLX5_SET(dctc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
1858
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001859 return;
1860 }
1861
Guy Levi7249c8e2019-04-10 10:59:45 +03001862 MLX5_SET(qpc, qpc, cs_res,
1863 rcqe_sz == 128 ? MLX5_RES_SCAT_DATA64_CQE :
1864 MLX5_RES_SCAT_DATA32_CQE);
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001865}
1866
1867static void configure_requester_scat_cqe(struct mlx5_ib_dev *dev,
1868 struct ib_qp_init_attr *init_attr,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001869 struct mlx5_ib_create_qp *ucmd,
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001870 void *qpc)
1871{
1872 enum ib_qp_type qpt = init_attr->qp_type;
1873 int scqe_sz;
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001874 bool allow_scat_cqe = 0;
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001875
1876 if (qpt == IB_QPT_UC || qpt == IB_QPT_UD)
1877 return;
1878
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001879 if (ucmd)
1880 allow_scat_cqe = ucmd->flags & MLX5_QP_FLAG_ALLOW_SCATTER_CQE;
1881
1882 if (!allow_scat_cqe && init_attr->sq_sig_type != IB_SIGNAL_ALL_WR)
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001883 return;
1884
1885 scqe_sz = mlx5_ib_get_cqe_size(init_attr->send_cq);
1886 if (scqe_sz == 128) {
1887 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
1888 return;
1889 }
1890
1891 if (init_attr->qp_type != MLX5_IB_QPT_DCI ||
1892 MLX5_CAP_GEN(dev->mdev, dc_req_scat_data_cqe))
1893 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
1894}
1895
Yonatan Cohena60109d2018-10-10 09:25:16 +03001896static int atomic_size_to_mode(int size_mask)
1897{
1898 /* driver does not support atomic_size > 256B
1899 * and does not know how to translate bigger sizes
1900 */
1901 int supported_size_mask = size_mask & 0x1ff;
1902 int log_max_size;
1903
1904 if (!supported_size_mask)
1905 return -EOPNOTSUPP;
1906
1907 log_max_size = __fls(supported_size_mask);
1908
1909 if (log_max_size > 3)
1910 return log_max_size;
1911
1912 return MLX5_ATOMIC_MODE_8B;
1913}
1914
1915static int get_atomic_mode(struct mlx5_ib_dev *dev,
1916 enum ib_qp_type qp_type)
1917{
1918 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
1919 u8 atomic = MLX5_CAP_GEN(dev->mdev, atomic);
1920 int atomic_mode = -EOPNOTSUPP;
1921 int atomic_size_mask;
1922
1923 if (!atomic)
1924 return -EOPNOTSUPP;
1925
1926 if (qp_type == MLX5_IB_QPT_DCT)
1927 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
1928 else
1929 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
1930
1931 if ((atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_CMP_SWAP) ||
1932 (atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_FETCH_ADD))
1933 atomic_mode = atomic_size_to_mode(atomic_size_mask);
1934
1935 if (atomic_mode <= 0 &&
1936 (atomic_operations & MLX5_ATOMIC_OPS_CMP_SWAP &&
1937 atomic_operations & MLX5_ATOMIC_OPS_FETCH_ADD))
1938 atomic_mode = MLX5_ATOMIC_MODE_IB_COMP;
1939
1940 return atomic_mode;
1941}
1942
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03001943static inline bool check_flags_mask(uint64_t input, uint64_t supported)
1944{
1945 return (input & ~supported) == 0;
1946}
1947
Eli Cohene126ba92013-07-07 17:25:49 +03001948static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1949 struct ib_qp_init_attr *init_attr,
1950 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1951{
1952 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001953 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001954 struct mlx5_core_dev *mdev = dev->mdev;
Jason Gunthorpe0625b4b2018-08-14 15:33:52 -06001955 struct mlx5_ib_create_qp_resp resp = {};
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001956 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
1957 udata, struct mlx5_ib_ucontext, ibucontext);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001958 struct mlx5_ib_cq *send_cq;
1959 struct mlx5_ib_cq *recv_cq;
1960 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001961 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001962 struct mlx5_ib_create_qp ucmd;
1963 struct mlx5_ib_qp_base *base;
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001964 int mlx5_st;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001965 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001966 u32 *in;
1967 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001968
1969 mutex_init(&qp->mutex);
1970 spin_lock_init(&qp->sq.lock);
1971 spin_lock_init(&qp->rq.lock);
1972
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001973 mlx5_st = to_mlx5_st(init_attr->qp_type);
1974 if (mlx5_st < 0)
1975 return -EINVAL;
1976
Yishai Hadas28d61372016-05-23 15:20:56 +03001977 if (init_attr->rwq_ind_tbl) {
1978 if (!udata)
1979 return -ENOSYS;
1980
1981 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1982 return err;
1983 }
1984
Eli Cohenf360d882014-04-02 00:10:16 +03001985 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001986 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001987 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1988 return -EINVAL;
1989 } else {
1990 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1991 }
1992 }
1993
Leon Romanovsky051f2632015-12-20 12:16:11 +02001994 if (init_attr->create_flags &
1995 (IB_QP_CREATE_CROSS_CHANNEL |
1996 IB_QP_CREATE_MANAGED_SEND |
1997 IB_QP_CREATE_MANAGED_RECV)) {
1998 if (!MLX5_CAP_GEN(mdev, cd)) {
1999 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
2000 return -EINVAL;
2001 }
2002 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
2003 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
2004 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
2005 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
2006 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
2007 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
2008 }
Erez Shitritf0313962016-02-21 16:27:17 +02002009
2010 if (init_attr->qp_type == IB_QPT_UD &&
2011 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
2012 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
2013 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
2014 return -EOPNOTSUPP;
2015 }
2016
Majd Dibbiny358e42e2016-04-17 17:19:37 +03002017 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
2018 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2019 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
2020 return -EOPNOTSUPP;
2021 }
2022 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
2023 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
2024 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
2025 return -EOPNOTSUPP;
2026 }
2027 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
2028 }
2029
Eli Cohene126ba92013-07-07 17:25:49 +03002030 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
2031 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
2032
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02002033 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
2034 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
2035 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
2036 (init_attr->qp_type != IB_QPT_RAW_PACKET))
2037 return -EOPNOTSUPP;
2038 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
2039 }
2040
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002041 if (udata) {
Eli Cohene126ba92013-07-07 17:25:49 +03002042 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
2043 mlx5_ib_dbg(dev, "copy failed\n");
2044 return -EFAULT;
2045 }
2046
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002047 if (!check_flags_mask(ucmd.flags,
Mark Bloch8af526e2019-01-15 16:45:32 +02002048 MLX5_QP_FLAG_ALLOW_SCATTER_CQE |
2049 MLX5_QP_FLAG_BFREG_INDEX |
2050 MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE |
2051 MLX5_QP_FLAG_SCATTER_CQE |
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002052 MLX5_QP_FLAG_SIGNATURE |
Mark Bloch8af526e2019-01-15 16:45:32 +02002053 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC |
2054 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
2055 MLX5_QP_FLAG_TUNNEL_OFFLOADS |
2056 MLX5_QP_FLAG_TYPE_DCI |
2057 MLX5_QP_FLAG_TYPE_DCT))
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002058 return -EINVAL;
2059
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002060 err = get_qp_user_index(ucontext, &ucmd, udata->inlen, &uidx);
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002061 if (err)
2062 return err;
2063
Eli Cohene126ba92013-07-07 17:25:49 +03002064 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002065 if (MLX5_CAP_GEN(dev->mdev, sctr_data_cqe))
2066 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002067 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
2068 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
2069 !tunnel_offload_supported(mdev)) {
2070 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
2071 return -EOPNOTSUPP;
2072 }
Mark Bloch175edba2018-09-17 13:30:48 +03002073 qp->flags_en |= MLX5_QP_FLAG_TUNNEL_OFFLOADS;
2074 }
2075
2076 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC) {
2077 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2078 mlx5_ib_dbg(dev, "Self-LB UC isn't supported\n");
2079 return -EOPNOTSUPP;
2080 }
2081 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
2082 }
2083
2084 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
2085 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2086 mlx5_ib_dbg(dev, "Self-LB UM isn't supported\n");
2087 return -EOPNOTSUPP;
2088 }
2089 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002090 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002091
Danit Goldberg569c6652018-11-30 13:22:05 +02002092 if (ucmd.flags & MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE) {
2093 if (init_attr->qp_type != IB_QPT_RC ||
2094 !MLX5_CAP_GEN(dev->mdev, qp_packet_based)) {
2095 mlx5_ib_dbg(dev, "packet based credit mode isn't supported\n");
2096 return -EOPNOTSUPP;
2097 }
2098 qp->flags |= MLX5_IB_QP_PACKET_BASED_CREDIT;
2099 }
2100
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002101 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
2102 if (init_attr->qp_type != IB_QPT_UD ||
2103 (MLX5_CAP_GEN(dev->mdev, port_type) !=
2104 MLX5_CAP_PORT_TYPE_IB) ||
2105 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
2106 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
2107 return -EOPNOTSUPP;
2108 }
2109
2110 qp->flags |= MLX5_IB_QP_UNDERLAY;
2111 qp->underlay_qpn = init_attr->source_qpn;
2112 }
Eli Cohene126ba92013-07-07 17:25:49 +03002113 } else {
2114 qp->wq_sig = !!wq_signature;
2115 }
2116
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002117 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2118 qp->flags & MLX5_IB_QP_UNDERLAY) ?
2119 &qp->raw_packet_qp.rq.base :
2120 &qp->trans_qp.base;
2121
Eli Cohene126ba92013-07-07 17:25:49 +03002122 qp->has_rq = qp_has_rq(init_attr);
2123 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002124 qp, udata ? &ucmd : NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002125 if (err) {
2126 mlx5_ib_dbg(dev, "err %d\n", err);
2127 return err;
2128 }
2129
2130 if (pd) {
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002131 if (udata) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002132 __u32 max_wqes =
2133 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03002134 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
2135 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
2136 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
2137 mlx5_ib_dbg(dev, "invalid rq params\n");
2138 return -EINVAL;
2139 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002140 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03002141 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03002142 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03002143 return -EINVAL;
2144 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02002145 if (init_attr->create_flags &
2146 mlx5_ib_create_qp_sqpn_qp1()) {
2147 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
2148 return -EINVAL;
2149 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002150 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
2151 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002152 if (err)
2153 mlx5_ib_dbg(dev, "err %d\n", err);
2154 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002155 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
2156 base);
Eli Cohene126ba92013-07-07 17:25:49 +03002157 if (err)
2158 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03002159 }
2160
2161 if (err)
2162 return err;
2163 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002164 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03002165 if (!in)
2166 return -ENOMEM;
2167
2168 qp->create_type = MLX5_QP_EMPTY;
2169 }
2170
2171 if (is_sqp(init_attr->qp_type))
2172 qp->port = init_attr->port_num;
2173
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002174 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
2175
Noa Osheroviche7b169f2018-02-25 13:39:51 +02002176 MLX5_SET(qpc, qpc, st, mlx5_st);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002177 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03002178
2179 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002180 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002181 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002182 MLX5_SET(qpc, qpc, latency_sensitive, 1);
2183
Eli Cohene126ba92013-07-07 17:25:49 +03002184
2185 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002186 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002187
Eli Cohenf360d882014-04-02 00:10:16 +03002188 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002189 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03002190
Leon Romanovsky051f2632015-12-20 12:16:11 +02002191 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002192 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002193 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002194 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002195 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002196 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Danit Goldberg569c6652018-11-30 13:22:05 +02002197 if (qp->flags & MLX5_IB_QP_PACKET_BASED_CREDIT)
2198 MLX5_SET(qpc, qpc, req_e2e_credit_mode, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002199 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002200 configure_responder_scat_cqe(init_attr, qpc);
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002201 configure_requester_scat_cqe(dev, init_attr,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002202 udata ? &ucmd : NULL,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002203 qpc);
Eli Cohene126ba92013-07-07 17:25:49 +03002204 }
2205
2206 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002207 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
2208 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03002209 }
2210
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002211 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03002212
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002213 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002214 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002215 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002216 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002217 if (init_attr->srq &&
2218 init_attr->srq->srq_type == IB_SRQT_TM)
2219 MLX5_SET(qpc, qpc, offload_type,
2220 MLX5_QPC_OFFLOAD_TYPE_RNDV);
2221 }
Eli Cohene126ba92013-07-07 17:25:49 +03002222
2223 /* Set default resources */
2224 switch (init_attr->qp_type) {
2225 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002226 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2227 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
2228 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
2229 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002230 break;
2231 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002232 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2233 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2234 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002235 break;
2236 default:
2237 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002238 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
2239 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002240 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002241 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2242 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002243 }
2244 }
2245
2246 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002247 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002248
2249 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002250 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002251
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002252 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03002253
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002254 /* 0xffffff means we ask to work with cqe version 0 */
2255 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002256 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002257
Erez Shitritf0313962016-02-21 16:27:17 +02002258 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
2259 if (init_attr->qp_type == IB_QPT_UD &&
2260 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02002261 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
2262 qp->flags |= MLX5_IB_QP_LSO;
2263 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002264
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002265 if (init_attr->create_flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
2266 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
2267 mlx5_ib_dbg(dev, "scatter end padding is not supported\n");
2268 err = -EOPNOTSUPP;
2269 goto err;
2270 } else if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2271 MLX5_SET(qpc, qpc, end_padding_mode,
2272 MLX5_WQ_END_PAD_MODE_ALIGN);
2273 } else {
2274 qp->flags |= MLX5_IB_QP_PCI_WRITE_END_PADDING;
2275 }
2276 }
2277
Boris Pismenny2c292db2018-03-08 15:51:40 +02002278 if (inlen < 0) {
2279 err = -EINVAL;
2280 goto err;
2281 }
2282
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002283 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2284 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002285 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
2286 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
Yishai Hadas7f720522018-09-20 21:45:18 +03002287 err = create_raw_packet_qp(dev, qp, in, inlen, pd, udata,
2288 &resp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002289 } else {
2290 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
2291 }
2292
Eli Cohene126ba92013-07-07 17:25:49 +03002293 if (err) {
2294 mlx5_ib_dbg(dev, "create qp failed\n");
2295 goto err_create;
2296 }
2297
Al Viro479163f2014-11-20 08:13:57 +00002298 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002299
majd@mellanox.com19098df2016-01-14 19:13:03 +02002300 base->container_mibqp = qp;
2301 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03002302
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002303 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
2304 &send_cq, &recv_cq);
2305 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2306 mlx5_ib_lock_cqs(send_cq, recv_cq);
2307 /* Maintain device to QPs access, needed for further handling via reset
2308 * flow
2309 */
2310 list_add_tail(&qp->qps_list, &dev->qp_list);
2311 /* Maintain CQ to QPs access, needed for further handling via reset flow
2312 */
2313 if (send_cq)
2314 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
2315 if (recv_cq)
2316 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
2317 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2318 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
2319
Eli Cohene126ba92013-07-07 17:25:49 +03002320 return 0;
2321
2322err_create:
2323 if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002324 destroy_qp_user(dev, pd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002325 else if (qp->create_type == MLX5_QP_KERNEL)
2326 destroy_qp_kernel(dev, qp);
2327
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002328err:
Al Viro479163f2014-11-20 08:13:57 +00002329 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002330 return err;
2331}
2332
2333static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2334 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
2335{
2336 if (send_cq) {
2337 if (recv_cq) {
2338 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002339 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002340 spin_lock_nested(&recv_cq->lock,
2341 SINGLE_DEPTH_NESTING);
2342 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002343 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002344 __acquire(&recv_cq->lock);
2345 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002346 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002347 spin_lock_nested(&send_cq->lock,
2348 SINGLE_DEPTH_NESTING);
2349 }
2350 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002351 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002352 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002353 }
2354 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002355 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002356 __acquire(&send_cq->lock);
2357 } else {
2358 __acquire(&send_cq->lock);
2359 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002360 }
2361}
2362
2363static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2364 __releases(&send_cq->lock) __releases(&recv_cq->lock)
2365{
2366 if (send_cq) {
2367 if (recv_cq) {
2368 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
2369 spin_unlock(&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 if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
2372 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002373 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002374 } else {
2375 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002376 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002377 }
2378 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002379 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002380 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002381 }
2382 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002383 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002384 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002385 } else {
2386 __release(&recv_cq->lock);
2387 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002388 }
2389}
2390
2391static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
2392{
2393 return to_mpd(qp->ibqp.pd);
2394}
2395
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002396static void get_cqs(enum ib_qp_type qp_type,
2397 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03002398 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
2399{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002400 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002401 case IB_QPT_XRC_TGT:
2402 *send_cq = NULL;
2403 *recv_cq = NULL;
2404 break;
2405 case MLX5_IB_QPT_REG_UMR:
2406 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002407 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002408 *recv_cq = NULL;
2409 break;
2410
2411 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002412 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002413 case IB_QPT_RC:
2414 case IB_QPT_UC:
2415 case IB_QPT_UD:
2416 case IB_QPT_RAW_IPV6:
2417 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002418 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002419 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2420 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002421 break;
2422
Eli Cohene126ba92013-07-07 17:25:49 +03002423 case IB_QPT_MAX:
2424 default:
2425 *send_cq = NULL;
2426 *recv_cq = NULL;
2427 break;
2428 }
2429}
2430
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002431static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002432 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2433 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002434
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002435static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2436 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002437{
2438 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002439 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002440 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002441 int err;
2442
Yishai Hadas28d61372016-05-23 15:20:56 +03002443 if (qp->ibqp.rwq_ind_tbl) {
2444 destroy_rss_raw_qp_tir(dev, qp);
2445 return;
2446 }
2447
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002448 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2449 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002450 &qp->raw_packet_qp.rq.base :
2451 &qp->trans_qp.base;
2452
Haggai Eran6aec21f2014-12-11 17:04:23 +02002453 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002454 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2455 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002456 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002457 MLX5_CMD_OP_2RST_QP, 0,
2458 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002459 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002460 struct mlx5_modify_raw_qp_param raw_qp_param = {
2461 .operation = MLX5_CMD_OP_2RST_QP
2462 };
2463
Aviv Heller13eab212016-09-18 20:48:04 +03002464 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002465 }
2466 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002467 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002468 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002469 }
Eli Cohene126ba92013-07-07 17:25:49 +03002470
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002471 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2472 &send_cq, &recv_cq);
2473
2474 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2475 mlx5_ib_lock_cqs(send_cq, recv_cq);
2476 /* del from lists under both locks above to protect reset flow paths */
2477 list_del(&qp->qps_list);
2478 if (send_cq)
2479 list_del(&qp->cq_send_list);
2480
2481 if (recv_cq)
2482 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002483
2484 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002485 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002486 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2487 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002488 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2489 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002490 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002491 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2492 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002493
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002494 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2495 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002496 destroy_raw_packet_qp(dev, qp);
2497 } else {
2498 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
2499 if (err)
2500 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2501 base->mqp.qpn);
2502 }
Eli Cohene126ba92013-07-07 17:25:49 +03002503
Eli Cohene126ba92013-07-07 17:25:49 +03002504 if (qp->create_type == MLX5_QP_KERNEL)
2505 destroy_qp_kernel(dev, qp);
2506 else if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002507 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002508}
2509
2510static const char *ib_qp_type_str(enum ib_qp_type type)
2511{
2512 switch (type) {
2513 case IB_QPT_SMI:
2514 return "IB_QPT_SMI";
2515 case IB_QPT_GSI:
2516 return "IB_QPT_GSI";
2517 case IB_QPT_RC:
2518 return "IB_QPT_RC";
2519 case IB_QPT_UC:
2520 return "IB_QPT_UC";
2521 case IB_QPT_UD:
2522 return "IB_QPT_UD";
2523 case IB_QPT_RAW_IPV6:
2524 return "IB_QPT_RAW_IPV6";
2525 case IB_QPT_RAW_ETHERTYPE:
2526 return "IB_QPT_RAW_ETHERTYPE";
2527 case IB_QPT_XRC_INI:
2528 return "IB_QPT_XRC_INI";
2529 case IB_QPT_XRC_TGT:
2530 return "IB_QPT_XRC_TGT";
2531 case IB_QPT_RAW_PACKET:
2532 return "IB_QPT_RAW_PACKET";
2533 case MLX5_IB_QPT_REG_UMR:
2534 return "MLX5_IB_QPT_REG_UMR";
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002535 case IB_QPT_DRIVER:
2536 return "IB_QPT_DRIVER";
Eli Cohene126ba92013-07-07 17:25:49 +03002537 case IB_QPT_MAX:
2538 default:
2539 return "Invalid QP type";
2540 }
2541}
2542
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002543static struct ib_qp *mlx5_ib_create_dct(struct ib_pd *pd,
2544 struct ib_qp_init_attr *attr,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002545 struct mlx5_ib_create_qp *ucmd,
2546 struct ib_udata *udata)
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002547{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002548 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2549 udata, struct mlx5_ib_ucontext, ibucontext);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002550 struct mlx5_ib_qp *qp;
2551 int err = 0;
2552 u32 uidx = MLX5_IB_DEFAULT_UIDX;
2553 void *dctc;
2554
2555 if (!attr->srq || !attr->recv_cq)
2556 return ERR_PTR(-EINVAL);
2557
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002558 err = get_qp_user_index(ucontext, ucmd, sizeof(*ucmd), &uidx);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002559 if (err)
2560 return ERR_PTR(err);
2561
2562 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2563 if (!qp)
2564 return ERR_PTR(-ENOMEM);
2565
2566 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
2567 if (!qp->dct.in) {
2568 err = -ENOMEM;
2569 goto err_free;
2570 }
2571
Yishai Hadasa01a5862018-09-20 21:39:24 +03002572 MLX5_SET(create_dct_in, qp->dct.in, uid, to_mpd(pd)->uid);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002573 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shoua776a3902018-01-02 16:19:33 +02002574 qp->qp_sub_type = MLX5_IB_QPT_DCT;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002575 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2576 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2577 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2578 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2579 MLX5_SET(dctc, dctc, user_index, uidx);
2580
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002581 if (ucmd->flags & MLX5_QP_FLAG_SCATTER_CQE)
2582 configure_responder_scat_cqe(attr, dctc);
2583
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002584 qp->state = IB_QPS_RESET;
2585
2586 return &qp->ibqp;
2587err_free:
2588 kfree(qp);
2589 return ERR_PTR(err);
2590}
2591
2592static int set_mlx_qp_type(struct mlx5_ib_dev *dev,
2593 struct ib_qp_init_attr *init_attr,
2594 struct mlx5_ib_create_qp *ucmd,
2595 struct ib_udata *udata)
2596{
2597 enum { MLX_QP_FLAGS = MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI };
2598 int err;
2599
2600 if (!udata)
2601 return -EINVAL;
2602
2603 if (udata->inlen < sizeof(*ucmd)) {
2604 mlx5_ib_dbg(dev, "create_qp user command is smaller than expected\n");
2605 return -EINVAL;
2606 }
2607 err = ib_copy_from_udata(ucmd, udata, sizeof(*ucmd));
2608 if (err)
2609 return err;
2610
2611 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCI) {
2612 init_attr->qp_type = MLX5_IB_QPT_DCI;
2613 } else {
2614 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCT) {
2615 init_attr->qp_type = MLX5_IB_QPT_DCT;
2616 } else {
2617 mlx5_ib_dbg(dev, "Invalid QP flags\n");
2618 return -EINVAL;
2619 }
2620 }
2621
2622 if (!MLX5_CAP_GEN(dev->mdev, dct)) {
2623 mlx5_ib_dbg(dev, "DC transport is not supported\n");
2624 return -EOPNOTSUPP;
2625 }
2626
2627 return 0;
2628}
2629
Eli Cohene126ba92013-07-07 17:25:49 +03002630struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002631 struct ib_qp_init_attr *verbs_init_attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002632 struct ib_udata *udata)
2633{
2634 struct mlx5_ib_dev *dev;
2635 struct mlx5_ib_qp *qp;
2636 u16 xrcdn = 0;
2637 int err;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002638 struct ib_qp_init_attr mlx_init_attr;
2639 struct ib_qp_init_attr *init_attr = verbs_init_attr;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002640 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2641 udata, struct mlx5_ib_ucontext, ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03002642
2643 if (pd) {
2644 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002645
2646 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002647 if (!ucontext) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002648 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2649 return ERR_PTR(-EINVAL);
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002650 } else if (!ucontext->cqe_version) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002651 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2652 return ERR_PTR(-EINVAL);
2653 }
2654 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002655 } else {
2656 /* being cautious here */
2657 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2658 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2659 pr_warn("%s: no PD for transport %s\n", __func__,
2660 ib_qp_type_str(init_attr->qp_type));
2661 return ERR_PTR(-EINVAL);
2662 }
2663 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002664 }
2665
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002666 if (init_attr->qp_type == IB_QPT_DRIVER) {
2667 struct mlx5_ib_create_qp ucmd;
2668
2669 init_attr = &mlx_init_attr;
2670 memcpy(init_attr, verbs_init_attr, sizeof(*verbs_init_attr));
2671 err = set_mlx_qp_type(dev, init_attr, &ucmd, udata);
2672 if (err)
2673 return ERR_PTR(err);
Moni Shouac32a4f22018-01-02 16:19:32 +02002674
2675 if (init_attr->qp_type == MLX5_IB_QPT_DCI) {
2676 if (init_attr->cap.max_recv_wr ||
2677 init_attr->cap.max_recv_sge) {
2678 mlx5_ib_dbg(dev, "DCI QP requires zero size receive queue\n");
2679 return ERR_PTR(-EINVAL);
2680 }
Moni Shoua776a3902018-01-02 16:19:33 +02002681 } else {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002682 return mlx5_ib_create_dct(pd, init_attr, &ucmd, udata);
Moni Shouac32a4f22018-01-02 16:19:32 +02002683 }
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002684 }
2685
Eli Cohene126ba92013-07-07 17:25:49 +03002686 switch (init_attr->qp_type) {
2687 case IB_QPT_XRC_TGT:
2688 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002689 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002690 mlx5_ib_dbg(dev, "XRC not supported\n");
2691 return ERR_PTR(-ENOSYS);
2692 }
2693 init_attr->recv_cq = NULL;
2694 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2695 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2696 init_attr->send_cq = NULL;
2697 }
2698
2699 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002700 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002701 case IB_QPT_RC:
2702 case IB_QPT_UC:
2703 case IB_QPT_UD:
2704 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002705 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002706 case MLX5_IB_QPT_REG_UMR:
Moni Shouac32a4f22018-01-02 16:19:32 +02002707 case MLX5_IB_QPT_DCI:
Eli Cohene126ba92013-07-07 17:25:49 +03002708 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2709 if (!qp)
2710 return ERR_PTR(-ENOMEM);
2711
2712 err = create_qp_common(dev, pd, init_attr, udata, qp);
2713 if (err) {
2714 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2715 kfree(qp);
2716 return ERR_PTR(err);
2717 }
2718
2719 if (is_qp0(init_attr->qp_type))
2720 qp->ibqp.qp_num = 0;
2721 else if (is_qp1(init_attr->qp_type))
2722 qp->ibqp.qp_num = 1;
2723 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002724 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002725
2726 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 +02002727 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002728 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2729 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002730
majd@mellanox.com19098df2016-01-14 19:13:03 +02002731 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002732
2733 break;
2734
Haggai Erand16e91d2016-02-29 15:45:05 +02002735 case IB_QPT_GSI:
2736 return mlx5_ib_gsi_create_qp(pd, init_attr);
2737
Eli Cohene126ba92013-07-07 17:25:49 +03002738 case IB_QPT_RAW_IPV6:
2739 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002740 case IB_QPT_MAX:
2741 default:
2742 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2743 init_attr->qp_type);
2744 /* Don't support raw QPs */
2745 return ERR_PTR(-EINVAL);
2746 }
2747
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002748 if (verbs_init_attr->qp_type == IB_QPT_DRIVER)
2749 qp->qp_sub_type = init_attr->qp_type;
2750
Eli Cohene126ba92013-07-07 17:25:49 +03002751 return &qp->ibqp;
2752}
2753
Moni Shoua776a3902018-01-02 16:19:33 +02002754static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2755{
2756 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2757
2758 if (mqp->state == IB_QPS_RTR) {
2759 int err;
2760
2761 err = mlx5_core_destroy_dct(dev->mdev, &mqp->dct.mdct);
2762 if (err) {
2763 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2764 return err;
2765 }
2766 }
2767
2768 kfree(mqp->dct.in);
2769 kfree(mqp);
2770 return 0;
2771}
2772
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002773int mlx5_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002774{
2775 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2776 struct mlx5_ib_qp *mqp = to_mqp(qp);
2777
Haggai Erand16e91d2016-02-29 15:45:05 +02002778 if (unlikely(qp->qp_type == IB_QPT_GSI))
2779 return mlx5_ib_gsi_destroy_qp(qp);
2780
Moni Shoua776a3902018-01-02 16:19:33 +02002781 if (mqp->qp_sub_type == MLX5_IB_QPT_DCT)
2782 return mlx5_ib_destroy_dct(mqp);
2783
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002784 destroy_qp_common(dev, mqp, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002785
2786 kfree(mqp);
2787
2788 return 0;
2789}
2790
Yonatan Cohena60109d2018-10-10 09:25:16 +03002791static int to_mlx5_access_flags(struct mlx5_ib_qp *qp,
2792 const struct ib_qp_attr *attr,
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002793 int attr_mask, __be32 *hw_access_flags_be)
Eli Cohene126ba92013-07-07 17:25:49 +03002794{
Eli Cohene126ba92013-07-07 17:25:49 +03002795 u8 dest_rd_atomic;
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002796 u32 access_flags, hw_access_flags = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002797
Yonatan Cohena60109d2018-10-10 09:25:16 +03002798 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
2799
Eli Cohene126ba92013-07-07 17:25:49 +03002800 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2801 dest_rd_atomic = attr->max_dest_rd_atomic;
2802 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002803 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002804
2805 if (attr_mask & IB_QP_ACCESS_FLAGS)
2806 access_flags = attr->qp_access_flags;
2807 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002808 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002809
2810 if (!dest_rd_atomic)
2811 access_flags &= IB_ACCESS_REMOTE_WRITE;
2812
2813 if (access_flags & IB_ACCESS_REMOTE_READ)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002814 hw_access_flags |= MLX5_QP_BIT_RRE;
Yonatan Cohen13f8d9c2018-11-21 13:48:39 +02002815 if (access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03002816 int atomic_mode;
Eli Cohene126ba92013-07-07 17:25:49 +03002817
Yonatan Cohena60109d2018-10-10 09:25:16 +03002818 atomic_mode = get_atomic_mode(dev, qp->ibqp.qp_type);
2819 if (atomic_mode < 0)
2820 return -EOPNOTSUPP;
2821
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002822 hw_access_flags |= MLX5_QP_BIT_RAE;
2823 hw_access_flags |= atomic_mode << MLX5_ATOMIC_MODE_OFFSET;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002824 }
2825
2826 if (access_flags & IB_ACCESS_REMOTE_WRITE)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002827 hw_access_flags |= MLX5_QP_BIT_RWE;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002828
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002829 *hw_access_flags_be = cpu_to_be32(hw_access_flags);
Yonatan Cohena60109d2018-10-10 09:25:16 +03002830
2831 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002832}
2833
2834enum {
2835 MLX5_PATH_FLAG_FL = 1 << 0,
2836 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2837 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2838};
2839
2840static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2841{
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002842 if (rate == IB_RATE_PORT_CURRENT)
Eli Cohene126ba92013-07-07 17:25:49 +03002843 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002844
Michael Guralnika5a5d192018-12-09 11:49:50 +02002845 if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_600_GBPS)
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002846 return -EINVAL;
2847
2848 while (rate != IB_RATE_PORT_CURRENT &&
2849 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
2850 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
2851 --rate;
2852
2853 return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
Eli Cohene126ba92013-07-07 17:25:49 +03002854}
2855
majd@mellanox.com75850d02016-01-14 19:13:06 +02002856static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002857 struct mlx5_ib_sq *sq, u8 sl,
2858 struct ib_pd *pd)
majd@mellanox.com75850d02016-01-14 19:13:06 +02002859{
2860 void *in;
2861 void *tisc;
2862 int inlen;
2863 int err;
2864
2865 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002866 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002867 if (!in)
2868 return -ENOMEM;
2869
2870 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002871 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002872
2873 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2874 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2875
2876 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2877
2878 kvfree(in);
2879
2880 return err;
2881}
2882
Aviv Heller13eab212016-09-18 20:48:04 +03002883static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002884 struct mlx5_ib_sq *sq, u8 tx_affinity,
2885 struct ib_pd *pd)
Aviv Heller13eab212016-09-18 20:48:04 +03002886{
2887 void *in;
2888 void *tisc;
2889 int inlen;
2890 int err;
2891
2892 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002893 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002894 if (!in)
2895 return -ENOMEM;
2896
2897 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002898 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
Aviv Heller13eab212016-09-18 20:48:04 +03002899
2900 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2901 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2902
2903 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2904
2905 kvfree(in);
2906
2907 return err;
2908}
2909
majd@mellanox.com75850d02016-01-14 19:13:06 +02002910static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002911 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002912 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002913 u32 path_flags, const struct ib_qp_attr *attr,
2914 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002915{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002916 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002917 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002918 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002919 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2920 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002921
Eli Cohene126ba92013-07-07 17:25:49 +03002922 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002923 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2924 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002925
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002926 if (ah_flags & IB_AH_GRH) {
2927 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002928 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002929 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002930 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002931 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002932 return -EINVAL;
2933 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002934 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002935
2936 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002937 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002938 return -EINVAL;
Parav Pandit47ec3862018-06-13 10:22:06 +03002939
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002940 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02002941 if (qp->ibqp.qp_type == IB_QPT_RC ||
2942 qp->ibqp.qp_type == IB_QPT_UC ||
2943 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
2944 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
Parav Pandit47ec3862018-06-13 10:22:06 +03002945 path->udp_sport =
2946 mlx5_get_roce_udp_sport(dev, ah->grh.sgid_attr);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002947 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Parav Pandit47ec3862018-06-13 10:22:06 +03002948 gid_type = ah->grh.sgid_attr->gid_type;
Majd Dibbinyed884512017-01-18 14:10:35 +02002949 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002950 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002951 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002952 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2953 path->fl_free_ar |=
2954 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002955 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2956 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2957 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002958 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002959 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002960 }
2961
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002962 if (ah_flags & IB_AH_GRH) {
2963 path->mgid_index = grh->sgid_index;
2964 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002965 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002966 cpu_to_be32((grh->traffic_class << 20) |
2967 (grh->flow_label));
2968 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002969 }
2970
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002971 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002972 if (err < 0)
2973 return err;
2974 path->static_rate = err;
2975 path->port = port;
2976
Eli Cohene126ba92013-07-07 17:25:49 +03002977 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002978 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002979
majd@mellanox.com75850d02016-01-14 19:13:06 +02002980 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2981 return modify_raw_packet_eth_prio(dev->mdev,
2982 &qp->raw_packet_qp.sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002983 sl & 0xf, qp->ibqp.pd);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002984
Eli Cohene126ba92013-07-07 17:25:49 +03002985 return 0;
2986}
2987
2988static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2989 [MLX5_QP_STATE_INIT] = {
2990 [MLX5_QP_STATE_INIT] = {
2991 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2992 MLX5_QP_OPTPAR_RAE |
2993 MLX5_QP_OPTPAR_RWE |
2994 MLX5_QP_OPTPAR_PKEY_INDEX |
2995 MLX5_QP_OPTPAR_PRI_PORT,
2996 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2997 MLX5_QP_OPTPAR_PKEY_INDEX |
2998 MLX5_QP_OPTPAR_PRI_PORT,
2999 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3000 MLX5_QP_OPTPAR_Q_KEY |
3001 MLX5_QP_OPTPAR_PRI_PORT,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003002 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3003 MLX5_QP_OPTPAR_RAE |
3004 MLX5_QP_OPTPAR_RWE |
3005 MLX5_QP_OPTPAR_PKEY_INDEX |
3006 MLX5_QP_OPTPAR_PRI_PORT,
Eli Cohene126ba92013-07-07 17:25:49 +03003007 },
3008 [MLX5_QP_STATE_RTR] = {
3009 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3010 MLX5_QP_OPTPAR_RRE |
3011 MLX5_QP_OPTPAR_RAE |
3012 MLX5_QP_OPTPAR_RWE |
3013 MLX5_QP_OPTPAR_PKEY_INDEX,
3014 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3015 MLX5_QP_OPTPAR_RWE |
3016 MLX5_QP_OPTPAR_PKEY_INDEX,
3017 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3018 MLX5_QP_OPTPAR_Q_KEY,
3019 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
3020 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03003021 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3022 MLX5_QP_OPTPAR_RRE |
3023 MLX5_QP_OPTPAR_RAE |
3024 MLX5_QP_OPTPAR_RWE |
3025 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03003026 },
3027 },
3028 [MLX5_QP_STATE_RTR] = {
3029 [MLX5_QP_STATE_RTS] = {
3030 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3031 MLX5_QP_OPTPAR_RRE |
3032 MLX5_QP_OPTPAR_RAE |
3033 MLX5_QP_OPTPAR_RWE |
3034 MLX5_QP_OPTPAR_PM_STATE |
3035 MLX5_QP_OPTPAR_RNR_TIMEOUT,
3036 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3037 MLX5_QP_OPTPAR_RWE |
3038 MLX5_QP_OPTPAR_PM_STATE,
3039 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003040 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3041 MLX5_QP_OPTPAR_RRE |
3042 MLX5_QP_OPTPAR_RAE |
3043 MLX5_QP_OPTPAR_RWE |
3044 MLX5_QP_OPTPAR_PM_STATE |
3045 MLX5_QP_OPTPAR_RNR_TIMEOUT,
Eli Cohene126ba92013-07-07 17:25:49 +03003046 },
3047 },
3048 [MLX5_QP_STATE_RTS] = {
3049 [MLX5_QP_STATE_RTS] = {
3050 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3051 MLX5_QP_OPTPAR_RAE |
3052 MLX5_QP_OPTPAR_RWE |
3053 MLX5_QP_OPTPAR_RNR_TIMEOUT |
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_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03003057 MLX5_QP_OPTPAR_PM_STATE |
3058 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003059 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
3060 MLX5_QP_OPTPAR_SRQN |
3061 MLX5_QP_OPTPAR_CQN_RCV,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003062 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3063 MLX5_QP_OPTPAR_RAE |
3064 MLX5_QP_OPTPAR_RWE |
3065 MLX5_QP_OPTPAR_RNR_TIMEOUT |
3066 MLX5_QP_OPTPAR_PM_STATE |
3067 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003068 },
3069 },
3070 [MLX5_QP_STATE_SQER] = {
3071 [MLX5_QP_STATE_RTS] = {
3072 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
3073 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03003074 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03003075 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3076 MLX5_QP_OPTPAR_RWE |
3077 MLX5_QP_OPTPAR_RAE |
3078 MLX5_QP_OPTPAR_RRE,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003079 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3080 MLX5_QP_OPTPAR_RWE |
3081 MLX5_QP_OPTPAR_RAE |
3082 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03003083 },
3084 },
3085};
3086
3087static int ib_nr_to_mlx5_nr(int ib_mask)
3088{
3089 switch (ib_mask) {
3090 case IB_QP_STATE:
3091 return 0;
3092 case IB_QP_CUR_STATE:
3093 return 0;
3094 case IB_QP_EN_SQD_ASYNC_NOTIFY:
3095 return 0;
3096 case IB_QP_ACCESS_FLAGS:
3097 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
3098 MLX5_QP_OPTPAR_RAE;
3099 case IB_QP_PKEY_INDEX:
3100 return MLX5_QP_OPTPAR_PKEY_INDEX;
3101 case IB_QP_PORT:
3102 return MLX5_QP_OPTPAR_PRI_PORT;
3103 case IB_QP_QKEY:
3104 return MLX5_QP_OPTPAR_Q_KEY;
3105 case IB_QP_AV:
3106 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
3107 MLX5_QP_OPTPAR_PRI_PORT;
3108 case IB_QP_PATH_MTU:
3109 return 0;
3110 case IB_QP_TIMEOUT:
3111 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
3112 case IB_QP_RETRY_CNT:
3113 return MLX5_QP_OPTPAR_RETRY_COUNT;
3114 case IB_QP_RNR_RETRY:
3115 return MLX5_QP_OPTPAR_RNR_RETRY;
3116 case IB_QP_RQ_PSN:
3117 return 0;
3118 case IB_QP_MAX_QP_RD_ATOMIC:
3119 return MLX5_QP_OPTPAR_SRA_MAX;
3120 case IB_QP_ALT_PATH:
3121 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
3122 case IB_QP_MIN_RNR_TIMER:
3123 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
3124 case IB_QP_SQ_PSN:
3125 return 0;
3126 case IB_QP_MAX_DEST_RD_ATOMIC:
3127 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
3128 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
3129 case IB_QP_PATH_MIG_STATE:
3130 return MLX5_QP_OPTPAR_PM_STATE;
3131 case IB_QP_CAP:
3132 return 0;
3133 case IB_QP_DEST_QPN:
3134 return 0;
3135 }
3136 return 0;
3137}
3138
3139static int ib_mask_to_mlx5_opt(int ib_mask)
3140{
3141 int result = 0;
3142 int i;
3143
3144 for (i = 0; i < 8 * sizeof(int); i++) {
3145 if ((1 << i) & ib_mask)
3146 result |= ib_nr_to_mlx5_nr(1 << i);
3147 }
3148
3149 return result;
3150}
3151
Yishai Hadas34d57582018-09-20 21:39:21 +03003152static int modify_raw_packet_qp_rq(
3153 struct mlx5_ib_dev *dev, struct mlx5_ib_rq *rq, int new_state,
3154 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003155{
3156 void *in;
3157 void *rqc;
3158 int inlen;
3159 int err;
3160
3161 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003162 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003163 if (!in)
3164 return -ENOMEM;
3165
3166 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
Yishai Hadas34d57582018-09-20 21:39:21 +03003167 MLX5_SET(modify_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003168
3169 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
3170 MLX5_SET(rqc, rqc, state, new_state);
3171
Alex Veskereb49ab02016-08-28 12:25:53 +03003172 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
3173 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
3174 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02003175 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03003176 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
3177 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06003178 dev_info_once(
3179 &dev->ib_dev.dev,
3180 "RAW PACKET QP counters are not supported on current FW\n");
Alex Veskereb49ab02016-08-28 12:25:53 +03003181 }
3182
3183 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003184 if (err)
3185 goto out;
3186
3187 rq->state = new_state;
3188
3189out:
3190 kvfree(in);
3191 return err;
3192}
3193
Yishai Hadasc14003f2018-09-20 21:39:22 +03003194static int modify_raw_packet_qp_sq(
3195 struct mlx5_core_dev *dev, struct mlx5_ib_sq *sq, int new_state,
3196 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003197{
Bodong Wang7d29f342016-12-01 13:43:16 +02003198 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
Bodong Wang61147f32018-03-19 15:10:30 +02003199 struct mlx5_rate_limit old_rl = ibqp->rl;
3200 struct mlx5_rate_limit new_rl = old_rl;
3201 bool new_rate_added = false;
Bodong Wang7d29f342016-12-01 13:43:16 +02003202 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003203 void *in;
3204 void *sqc;
3205 int inlen;
3206 int err;
3207
3208 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003209 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003210 if (!in)
3211 return -ENOMEM;
3212
Yishai Hadasc14003f2018-09-20 21:39:22 +03003213 MLX5_SET(modify_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003214 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
3215
3216 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
3217 MLX5_SET(sqc, sqc, state, new_state);
3218
Bodong Wang7d29f342016-12-01 13:43:16 +02003219 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
3220 if (new_state != MLX5_SQC_STATE_RDY)
3221 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
3222 __func__);
3223 else
Bodong Wang61147f32018-03-19 15:10:30 +02003224 new_rl = raw_qp_param->rl;
Bodong Wang7d29f342016-12-01 13:43:16 +02003225 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003226
Bodong Wang61147f32018-03-19 15:10:30 +02003227 if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
3228 if (new_rl.rate) {
3229 err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003230 if (err) {
Bodong Wang61147f32018-03-19 15:10:30 +02003231 pr_err("Failed configuring rate limit(err %d): \
3232 rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
3233 err, new_rl.rate, new_rl.max_burst_sz,
3234 new_rl.typical_pkt_sz);
3235
Bodong Wang7d29f342016-12-01 13:43:16 +02003236 goto out;
3237 }
Bodong Wang61147f32018-03-19 15:10:30 +02003238 new_rate_added = true;
Bodong Wang7d29f342016-12-01 13:43:16 +02003239 }
3240
3241 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
Bodong Wang61147f32018-03-19 15:10:30 +02003242 /* index 0 means no limit */
Bodong Wang7d29f342016-12-01 13:43:16 +02003243 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
3244 }
3245
3246 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
3247 if (err) {
3248 /* Remove new rate from table if failed */
Bodong Wang61147f32018-03-19 15:10:30 +02003249 if (new_rate_added)
3250 mlx5_rl_remove_rate(dev, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003251 goto out;
3252 }
3253
3254 /* Only remove the old rate after new rate was set */
Bodong Wang61147f32018-03-19 15:10:30 +02003255 if ((old_rl.rate &&
3256 !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
Bodong Wang7d29f342016-12-01 13:43:16 +02003257 (new_state != MLX5_SQC_STATE_RDY))
Bodong Wang61147f32018-03-19 15:10:30 +02003258 mlx5_rl_remove_rate(dev, &old_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003259
Bodong Wang61147f32018-03-19 15:10:30 +02003260 ibqp->rl = new_rl;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003261 sq->state = new_state;
3262
3263out:
3264 kvfree(in);
3265 return err;
3266}
3267
3268static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03003269 const struct mlx5_modify_raw_qp_param *raw_qp_param,
3270 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003271{
3272 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
3273 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
3274 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02003275 int modify_rq = !!qp->rq.wqe_cnt;
3276 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003277 int rq_state;
3278 int sq_state;
3279 int err;
3280
Alex Vesker0680efa2016-08-28 12:25:52 +03003281 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003282 case MLX5_CMD_OP_RST2INIT_QP:
3283 rq_state = MLX5_RQC_STATE_RDY;
3284 sq_state = MLX5_SQC_STATE_RDY;
3285 break;
3286 case MLX5_CMD_OP_2ERR_QP:
3287 rq_state = MLX5_RQC_STATE_ERR;
3288 sq_state = MLX5_SQC_STATE_ERR;
3289 break;
3290 case MLX5_CMD_OP_2RST_QP:
3291 rq_state = MLX5_RQC_STATE_RST;
3292 sq_state = MLX5_SQC_STATE_RST;
3293 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003294 case MLX5_CMD_OP_RTR2RTS_QP:
3295 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02003296 if (raw_qp_param->set_mask ==
3297 MLX5_RAW_QP_RATE_LIMIT) {
3298 modify_rq = 0;
3299 sq_state = sq->state;
3300 } else {
3301 return raw_qp_param->set_mask ? -EINVAL : 0;
3302 }
3303 break;
3304 case MLX5_CMD_OP_INIT2INIT_QP:
3305 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03003306 if (raw_qp_param->set_mask)
3307 return -EINVAL;
3308 else
3309 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003310 default:
3311 WARN_ON(1);
3312 return -EINVAL;
3313 }
3314
Bodong Wang7d29f342016-12-01 13:43:16 +02003315 if (modify_rq) {
Yishai Hadas34d57582018-09-20 21:39:21 +03003316 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param,
3317 qp->ibqp.pd);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003318 if (err)
3319 return err;
3320 }
3321
Bodong Wang7d29f342016-12-01 13:43:16 +02003322 if (modify_sq) {
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003323 struct mlx5_flow_handle *flow_rule;
3324
Aviv Heller13eab212016-09-18 20:48:04 +03003325 if (tx_affinity) {
3326 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003327 tx_affinity,
3328 qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003329 if (err)
3330 return err;
3331 }
3332
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003333 flow_rule = create_flow_rule_vport_sq(dev, sq,
3334 raw_qp_param->port);
3335 if (IS_ERR(flow_rule))
Colin Ian King1db86312019-04-12 11:40:17 +01003336 return PTR_ERR(flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003337
3338 err = modify_raw_packet_qp_sq(dev->mdev, sq, sq_state,
3339 raw_qp_param, qp->ibqp.pd);
3340 if (err) {
3341 if (flow_rule)
3342 mlx5_del_flow_rules(flow_rule);
3343 return err;
3344 }
3345
3346 if (flow_rule) {
3347 destroy_flow_rule_vport_sq(sq);
3348 sq->flow_rule = flow_rule;
3349 }
3350
3351 return err;
Aviv Heller13eab212016-09-18 20:48:04 +03003352 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003353
3354 return 0;
3355}
3356
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003357static unsigned int get_tx_affinity(struct mlx5_ib_dev *dev,
3358 struct mlx5_ib_pd *pd,
3359 struct mlx5_ib_qp_base *qp_base,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003360 u8 port_num, struct ib_udata *udata)
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003361{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003362 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
3363 udata, struct mlx5_ib_ucontext, ibucontext);
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003364 unsigned int tx_port_affinity;
3365
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003366 if (ucontext) {
3367 tx_port_affinity = (unsigned int)atomic_add_return(
3368 1, &ucontext->tx_port_affinity) %
3369 MLX5_MAX_PORTS +
3370 1;
3371 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x ucontext %p\n",
3372 tx_port_affinity, qp_base->mqp.qpn, ucontext);
3373 } else {
3374 tx_port_affinity =
3375 (unsigned int)atomic_add_return(
Mark Bloch95579e72019-03-28 15:27:33 +02003376 1, &dev->port[port_num].roce.tx_port_affinity) %
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003377 MLX5_MAX_PORTS +
3378 1;
3379 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x\n",
3380 tx_port_affinity, qp_base->mqp.qpn);
3381 }
3382
3383 return tx_port_affinity;
3384}
3385
Eli Cohene126ba92013-07-07 17:25:49 +03003386static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
3387 const struct ib_qp_attr *attr, int attr_mask,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003388 enum ib_qp_state cur_state,
3389 enum ib_qp_state new_state,
3390 const struct mlx5_ib_modify_qp *ucmd,
3391 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003392{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003393 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
3394 [MLX5_QP_STATE_RST] = {
3395 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3396 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3397 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
3398 },
3399 [MLX5_QP_STATE_INIT] = {
3400 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3401 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3402 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
3403 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
3404 },
3405 [MLX5_QP_STATE_RTR] = {
3406 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3407 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3408 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
3409 },
3410 [MLX5_QP_STATE_RTS] = {
3411 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3412 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3413 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
3414 },
3415 [MLX5_QP_STATE_SQD] = {
3416 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3417 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3418 },
3419 [MLX5_QP_STATE_SQER] = {
3420 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3421 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3422 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
3423 },
3424 [MLX5_QP_STATE_ERR] = {
3425 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3426 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3427 }
3428 };
3429
Eli Cohene126ba92013-07-07 17:25:49 +03003430 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3431 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02003432 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03003433 struct mlx5_ib_cq *send_cq, *recv_cq;
3434 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03003435 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03003436 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003437 enum mlx5_qp_state mlx5_cur, mlx5_new;
3438 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03003439 int mlx5_st;
3440 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003441 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03003442 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003443
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003444 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
3445 qp->qp_sub_type : ibqp->qp_type);
3446 if (mlx5_st < 0)
3447 return -EINVAL;
3448
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003449 context = kzalloc(sizeof(*context), GFP_KERNEL);
3450 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03003451 return -ENOMEM;
3452
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003453 pd = get_pd(qp);
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003454 context->flags = cpu_to_be32(mlx5_st << 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003455
3456 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
3457 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3458 } else {
3459 switch (attr->path_mig_state) {
3460 case IB_MIG_MIGRATED:
3461 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3462 break;
3463 case IB_MIG_REARM:
3464 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
3465 break;
3466 case IB_MIG_ARMED:
3467 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
3468 break;
3469 }
3470 }
3471
Aviv Heller13eab212016-09-18 20:48:04 +03003472 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
3473 if ((ibqp->qp_type == IB_QPT_RC) ||
3474 (ibqp->qp_type == IB_QPT_UD &&
3475 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
3476 (ibqp->qp_type == IB_QPT_UC) ||
3477 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
3478 (ibqp->qp_type == IB_QPT_XRC_INI) ||
3479 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
Aviv Heller7c34ec12018-08-23 13:47:53 +03003480 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02003481 u8 p = mlx5_core_native_port_num(dev->mdev) - 1;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003482 tx_affinity = get_tx_affinity(dev, pd, base, p,
3483 udata);
Aviv Heller13eab212016-09-18 20:48:04 +03003484 context->flags |= cpu_to_be32(tx_affinity << 24);
3485 }
3486 }
3487 }
3488
Haggai Erand16e91d2016-02-29 15:45:05 +02003489 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003490 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003491 } else if ((ibqp->qp_type == IB_QPT_UD &&
3492 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003493 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
3494 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
3495 } else if (attr_mask & IB_QP_PATH_MTU) {
3496 if (attr->path_mtu < IB_MTU_256 ||
3497 attr->path_mtu > IB_MTU_4096) {
3498 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3499 err = -EINVAL;
3500 goto out;
3501 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003502 context->mtu_msgmax = (attr->path_mtu << 5) |
3503 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03003504 }
3505
3506 if (attr_mask & IB_QP_DEST_QPN)
3507 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
3508
3509 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003510 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003511
3512 /* todo implement counter_index functionality */
3513
3514 if (is_sqp(ibqp->qp_type))
3515 context->pri_path.port = qp->port;
3516
3517 if (attr_mask & IB_QP_PORT)
3518 context->pri_path.port = attr->port_num;
3519
3520 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003521 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003522 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003523 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003524 if (err)
3525 goto out;
3526 }
3527
3528 if (attr_mask & IB_QP_TIMEOUT)
3529 context->pri_path.ackto_lt |= attr->timeout << 3;
3530
3531 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003532 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3533 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003534 attr->alt_port_num,
3535 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3536 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003537 if (err)
3538 goto out;
3539 }
3540
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003541 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3542 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003543
3544 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3545 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3546 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3547 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3548
3549 if (attr_mask & IB_QP_RNR_RETRY)
3550 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3551
3552 if (attr_mask & IB_QP_RETRY_CNT)
3553 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3554
3555 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3556 if (attr->max_rd_atomic)
3557 context->params1 |=
3558 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3559 }
3560
3561 if (attr_mask & IB_QP_SQ_PSN)
3562 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3563
3564 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3565 if (attr->max_dest_rd_atomic)
3566 context->params2 |=
3567 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3568 }
3569
Yonatan Cohena60109d2018-10-10 09:25:16 +03003570 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08003571 __be32 access_flags;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003572
3573 err = to_mlx5_access_flags(qp, attr, attr_mask, &access_flags);
3574 if (err)
3575 goto out;
3576
3577 context->params2 |= access_flags;
3578 }
Eli Cohene126ba92013-07-07 17:25:49 +03003579
3580 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3581 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3582
3583 if (attr_mask & IB_QP_RQ_PSN)
3584 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3585
3586 if (attr_mask & IB_QP_QKEY)
3587 context->qkey = cpu_to_be32(attr->qkey);
3588
3589 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3590 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3591
Mark Bloch0837e862016-06-17 15:10:55 +03003592 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3593 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3594 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003595
3596 /* Underlay port should be used - index 0 function per port */
3597 if (qp->flags & MLX5_IB_QP_UNDERLAY)
3598 port_num = 0;
3599
Alex Veskereb49ab02016-08-28 12:25:53 +03003600 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03003601 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03003602 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003603 }
3604
Eli Cohene126ba92013-07-07 17:25:49 +03003605 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3606 context->sq_crq_size |= cpu_to_be16(1 << 4);
3607
Haggai Eranb11a4f92016-02-29 15:45:03 +02003608 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
3609 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003610
3611 mlx5_cur = to_mlx5_state(cur_state);
3612 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003613
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003614 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003615 !optab[mlx5_cur][mlx5_new]) {
3616 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003617 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003618 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003619
3620 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003621 optpar = ib_mask_to_mlx5_opt(attr_mask);
3622 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003623
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003624 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3625 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003626 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3627
3628 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003629 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03003630 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003631 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3632 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003633
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003634 if (attr_mask & IB_QP_PORT)
3635 raw_qp_param.port = attr->port_num;
3636
Bodong Wang7d29f342016-12-01 13:43:16 +02003637 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003638 raw_qp_param.rl.rate = attr->rate_limit;
3639
3640 if (ucmd->burst_info.max_burst_sz) {
3641 if (attr->rate_limit &&
3642 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3643 raw_qp_param.rl.max_burst_sz =
3644 ucmd->burst_info.max_burst_sz;
3645 } else {
3646 err = -EINVAL;
3647 goto out;
3648 }
3649 }
3650
3651 if (ucmd->burst_info.typical_pkt_sz) {
3652 if (attr->rate_limit &&
3653 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3654 raw_qp_param.rl.typical_pkt_sz =
3655 ucmd->burst_info.typical_pkt_sz;
3656 } else {
3657 err = -EINVAL;
3658 goto out;
3659 }
3660 }
3661
Bodong Wang7d29f342016-12-01 13:43:16 +02003662 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3663 }
3664
Aviv Heller13eab212016-09-18 20:48:04 +03003665 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003666 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003667 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003668 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003669 }
3670
Eli Cohene126ba92013-07-07 17:25:49 +03003671 if (err)
3672 goto out;
3673
3674 qp->state = new_state;
3675
3676 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003677 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003678 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003679 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003680 if (attr_mask & IB_QP_PORT)
3681 qp->port = attr->port_num;
3682 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003683 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003684
3685 /*
3686 * If we moved a kernel QP to RESET, clean up all old CQ
3687 * entries and reinitialize the QP.
3688 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02003689 if (new_state == IB_QPS_RESET &&
3690 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003691 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003692 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3693 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003694 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003695
3696 qp->rq.head = 0;
3697 qp->rq.tail = 0;
3698 qp->sq.head = 0;
3699 qp->sq.tail = 0;
3700 qp->sq.cur_post = 0;
Guy Levi34f4c952018-11-26 08:15:50 +02003701 if (qp->sq.wqe_cnt)
3702 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003703 qp->db.db[MLX5_RCV_DBR] = 0;
3704 qp->db.db[MLX5_SND_DBR] = 0;
3705 }
3706
3707out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003708 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003709 return err;
3710}
3711
Moni Shouac32a4f22018-01-02 16:19:32 +02003712static inline bool is_valid_mask(int mask, int req, int opt)
3713{
3714 if ((mask & req) != req)
3715 return false;
3716
3717 if (mask & ~(req | opt))
3718 return false;
3719
3720 return true;
3721}
3722
3723/* check valid transition for driver QP types
3724 * for now the only QP type that this function supports is DCI
3725 */
3726static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3727 enum ib_qp_attr_mask attr_mask)
3728{
3729 int req = IB_QP_STATE;
3730 int opt = 0;
3731
Moni Shoua99ed7482018-09-12 09:33:55 +03003732 if (new_state == IB_QPS_RESET) {
3733 return is_valid_mask(attr_mask, req, opt);
3734 } else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Moni Shouac32a4f22018-01-02 16:19:32 +02003735 req |= 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_INIT) {
3738 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3739 return is_valid_mask(attr_mask, req, opt);
3740 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3741 req |= IB_QP_PATH_MTU;
Artemy Kovalyov5ec03042018-11-05 08:12:07 +02003742 opt = IB_QP_PKEY_INDEX | IB_QP_AV;
Moni Shouac32a4f22018-01-02 16:19:32 +02003743 return is_valid_mask(attr_mask, req, opt);
3744 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3745 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3746 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3747 opt = IB_QP_MIN_RNR_TIMER;
3748 return is_valid_mask(attr_mask, req, opt);
3749 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3750 opt = IB_QP_MIN_RNR_TIMER;
3751 return is_valid_mask(attr_mask, req, opt);
3752 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3753 return is_valid_mask(attr_mask, req, opt);
3754 }
3755 return false;
3756}
3757
Moni Shoua776a3902018-01-02 16:19:33 +02003758/* mlx5_ib_modify_dct: modify a DCT QP
3759 * valid transitions are:
3760 * RESET to INIT: must set access_flags, pkey_index and port
3761 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3762 * mtu, gid_index and hop_limit
3763 * Other transitions and attributes are illegal
3764 */
3765static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3766 int attr_mask, struct ib_udata *udata)
3767{
3768 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3769 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3770 enum ib_qp_state cur_state, new_state;
3771 int err = 0;
3772 int required = IB_QP_STATE;
3773 void *dctc;
3774
3775 if (!(attr_mask & IB_QP_STATE))
3776 return -EINVAL;
3777
3778 cur_state = qp->state;
3779 new_state = attr->qp_state;
3780
3781 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3782 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3783 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3784 if (!is_valid_mask(attr_mask, required, 0))
3785 return -EINVAL;
3786
3787 if (attr->port_num == 0 ||
3788 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3789 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3790 attr->port_num, dev->num_ports);
3791 return -EINVAL;
3792 }
3793 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3794 MLX5_SET(dctc, dctc, rre, 1);
3795 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3796 MLX5_SET(dctc, dctc, rwe, 1);
3797 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03003798 int atomic_mode;
3799
3800 atomic_mode = get_atomic_mode(dev, MLX5_IB_QPT_DCT);
3801 if (atomic_mode < 0)
Moni Shoua776a3902018-01-02 16:19:33 +02003802 return -EOPNOTSUPP;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003803
3804 MLX5_SET(dctc, dctc, atomic_mode, atomic_mode);
Moni Shoua776a3902018-01-02 16:19:33 +02003805 MLX5_SET(dctc, dctc, rae, 1);
Moni Shoua776a3902018-01-02 16:19:33 +02003806 }
3807 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3808 MLX5_SET(dctc, dctc, port, attr->port_num);
3809 MLX5_SET(dctc, dctc, counter_set_id, dev->port[attr->port_num - 1].cnts.set_id);
3810
3811 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3812 struct mlx5_ib_modify_qp_resp resp = {};
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003813 u32 out[MLX5_ST_SZ_DW(create_dct_out)] = {0};
Moni Shoua776a3902018-01-02 16:19:33 +02003814 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3815 sizeof(resp.dctn);
3816
3817 if (udata->outlen < min_resp_len)
3818 return -EINVAL;
3819 resp.response_length = min_resp_len;
3820
3821 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3822 if (!is_valid_mask(attr_mask, required, 0))
3823 return -EINVAL;
3824 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3825 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3826 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3827 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3828 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3829 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3830
3831 err = mlx5_core_create_dct(dev->mdev, &qp->dct.mdct, qp->dct.in,
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003832 MLX5_ST_SZ_BYTES(create_dct_in), out,
3833 sizeof(out));
Moni Shoua776a3902018-01-02 16:19:33 +02003834 if (err)
3835 return err;
3836 resp.dctn = qp->dct.mdct.mqp.qpn;
3837 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3838 if (err) {
3839 mlx5_core_destroy_dct(dev->mdev, &qp->dct.mdct);
3840 return err;
3841 }
3842 } else {
3843 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3844 return -EINVAL;
3845 }
3846 if (err)
3847 qp->state = IB_QPS_ERR;
3848 else
3849 qp->state = new_state;
3850 return err;
3851}
3852
Eli Cohene126ba92013-07-07 17:25:49 +03003853int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3854 int attr_mask, struct ib_udata *udata)
3855{
3856 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3857 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02003858 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02003859 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003860 enum ib_qp_state cur_state, new_state;
Bodong Wang61147f32018-03-19 15:10:30 +02003861 size_t required_cmd_sz;
Eli Cohene126ba92013-07-07 17:25:49 +03003862 int err = -EINVAL;
3863 int port;
3864
Yishai Hadas28d61372016-05-23 15:20:56 +03003865 if (ibqp->rwq_ind_tbl)
3866 return -ENOSYS;
3867
Bodong Wang61147f32018-03-19 15:10:30 +02003868 if (udata && udata->inlen) {
3869 required_cmd_sz = offsetof(typeof(ucmd), reserved) +
3870 sizeof(ucmd.reserved);
3871 if (udata->inlen < required_cmd_sz)
3872 return -EINVAL;
3873
3874 if (udata->inlen > sizeof(ucmd) &&
3875 !ib_is_udata_cleared(udata, sizeof(ucmd),
3876 udata->inlen - sizeof(ucmd)))
3877 return -EOPNOTSUPP;
3878
3879 if (ib_copy_from_udata(&ucmd, udata,
3880 min(udata->inlen, sizeof(ucmd))))
3881 return -EFAULT;
3882
3883 if (ucmd.comp_mask ||
3884 memchr_inv(&ucmd.reserved, 0, sizeof(ucmd.reserved)) ||
3885 memchr_inv(&ucmd.burst_info.reserved, 0,
3886 sizeof(ucmd.burst_info.reserved)))
3887 return -EOPNOTSUPP;
3888 }
3889
Haggai Erand16e91d2016-02-29 15:45:05 +02003890 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3891 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3892
Moni Shouac32a4f22018-01-02 16:19:32 +02003893 if (ibqp->qp_type == IB_QPT_DRIVER)
3894 qp_type = qp->qp_sub_type;
3895 else
3896 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3897 IB_QPT_GSI : ibqp->qp_type;
3898
Moni Shoua776a3902018-01-02 16:19:33 +02003899 if (qp_type == MLX5_IB_QPT_DCT)
3900 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02003901
Eli Cohene126ba92013-07-07 17:25:49 +03003902 mutex_lock(&qp->mutex);
3903
3904 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
3905 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
3906
Achiad Shochat2811ba52015-12-23 18:47:24 +02003907 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
3908 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003909 }
3910
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003911 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
3912 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
3913 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
3914 attr_mask);
3915 goto out;
3916 }
3917 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02003918 qp_type != MLX5_IB_QPT_DCI &&
Kamal Heibd31131b2018-10-02 16:11:21 +03003919 !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
3920 attr_mask)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003921 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3922 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003923 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02003924 } else if (qp_type == MLX5_IB_QPT_DCI &&
3925 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
3926 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3927 cur_state, new_state, qp_type, attr_mask);
3928 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003929 }
Eli Cohene126ba92013-07-07 17:25:49 +03003930
3931 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003932 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003933 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003934 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3935 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003936 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003937 }
Eli Cohene126ba92013-07-07 17:25:49 +03003938
3939 if (attr_mask & IB_QP_PKEY_INDEX) {
3940 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003941 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02003942 dev->mdev->port_caps[port - 1].pkey_table_len) {
3943 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
3944 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003945 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003946 }
Eli Cohene126ba92013-07-07 17:25:49 +03003947 }
3948
3949 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003950 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003951 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
3952 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
3953 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003954 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003955 }
Eli Cohene126ba92013-07-07 17:25:49 +03003956
3957 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003958 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003959 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
3960 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
3961 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003962 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003963 }
Eli Cohene126ba92013-07-07 17:25:49 +03003964
3965 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
3966 err = 0;
3967 goto out;
3968 }
3969
Bodong Wang61147f32018-03-19 15:10:30 +02003970 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003971 new_state, &ucmd, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03003972
3973out:
3974 mutex_unlock(&qp->mutex);
3975 return err;
3976}
3977
Guy Levi34f4c952018-11-26 08:15:50 +02003978static void _handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
3979 u32 wqe_sz, void **cur_edge)
3980{
3981 u32 idx;
3982
3983 idx = (sq->cur_post + (wqe_sz >> 2)) & (sq->wqe_cnt - 1);
3984 *cur_edge = get_sq_edge(sq, idx);
3985
3986 *seg = mlx5_frag_buf_get_wqe(&sq->fbc, idx);
3987}
3988
3989/* handle_post_send_edge - Check if we get to SQ edge. If yes, update to the
3990 * next nearby edge and get new address translation for current WQE position.
3991 * @sq - SQ buffer.
3992 * @seg: Current WQE position (16B aligned).
3993 * @wqe_sz: Total current WQE size [16B].
3994 * @cur_edge: Updated current edge.
3995 */
3996static inline void handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
3997 u32 wqe_sz, void **cur_edge)
3998{
3999 if (likely(*seg != *cur_edge))
4000 return;
4001
4002 _handle_post_send_edge(sq, seg, wqe_sz, cur_edge);
4003}
4004
4005/* memcpy_send_wqe - copy data from src to WQE and update the relevant WQ's
4006 * pointers. At the end @seg is aligned to 16B regardless the copied size.
4007 * @sq - SQ buffer.
4008 * @cur_edge: Updated current edge.
4009 * @seg: Current WQE position (16B aligned).
4010 * @wqe_sz: Total current WQE size [16B].
4011 * @src: Pointer to copy from.
4012 * @n: Number of bytes to copy.
4013 */
4014static inline void memcpy_send_wqe(struct mlx5_ib_wq *sq, void **cur_edge,
4015 void **seg, u32 *wqe_sz, const void *src,
4016 size_t n)
4017{
4018 while (likely(n)) {
4019 size_t leftlen = *cur_edge - *seg;
4020 size_t copysz = min_t(size_t, leftlen, n);
4021 size_t stride;
4022
4023 memcpy(*seg, src, copysz);
4024
4025 n -= copysz;
4026 src += copysz;
4027 stride = !n ? ALIGN(copysz, 16) : copysz;
4028 *seg += stride;
4029 *wqe_sz += stride >> 4;
4030 handle_post_send_edge(sq, seg, *wqe_sz, cur_edge);
4031 }
4032}
4033
Eli Cohene126ba92013-07-07 17:25:49 +03004034static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
4035{
4036 struct mlx5_ib_cq *cq;
4037 unsigned cur;
4038
4039 cur = wq->head - wq->tail;
4040 if (likely(cur + nreq < wq->max_post))
4041 return 0;
4042
4043 cq = to_mcq(ib_cq);
4044 spin_lock(&cq->lock);
4045 cur = wq->head - wq->tail;
4046 spin_unlock(&cq->lock);
4047
4048 return cur + nreq >= wq->max_post;
4049}
4050
4051static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
4052 u64 remote_addr, u32 rkey)
4053{
4054 rseg->raddr = cpu_to_be64(remote_addr);
4055 rseg->rkey = cpu_to_be32(rkey);
4056 rseg->reserved = 0;
4057}
4058
Guy Levi34f4c952018-11-26 08:15:50 +02004059static void set_eth_seg(const struct ib_send_wr *wr, struct mlx5_ib_qp *qp,
4060 void **seg, int *size, void **cur_edge)
Erez Shitritf0313962016-02-21 16:27:17 +02004061{
Guy Levi34f4c952018-11-26 08:15:50 +02004062 struct mlx5_wqe_eth_seg *eseg = *seg;
Erez Shitritf0313962016-02-21 16:27:17 +02004063
4064 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
4065
4066 if (wr->send_flags & IB_SEND_IP_CSUM)
4067 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
4068 MLX5_ETH_WQE_L4_CSUM;
4069
Erez Shitritf0313962016-02-21 16:27:17 +02004070 if (wr->opcode == IB_WR_LSO) {
4071 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Guy Levi34f4c952018-11-26 08:15:50 +02004072 size_t left, copysz;
Erez Shitritf0313962016-02-21 16:27:17 +02004073 void *pdata = ud_wr->header;
Guy Levi34f4c952018-11-26 08:15:50 +02004074 size_t stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004075
4076 left = ud_wr->hlen;
4077 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02004078 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02004079
Guy Levi34f4c952018-11-26 08:15:50 +02004080 /* memcpy_send_wqe should get a 16B align address. Hence, we
4081 * first copy up to the current edge and then, if needed,
4082 * fall-through to memcpy_send_wqe.
Erez Shitritf0313962016-02-21 16:27:17 +02004083 */
Guy Levi34f4c952018-11-26 08:15:50 +02004084 copysz = min_t(u64, *cur_edge - (void *)eseg->inline_hdr.start,
4085 left);
4086 memcpy(eseg->inline_hdr.start, pdata, copysz);
4087 stride = ALIGN(sizeof(struct mlx5_wqe_eth_seg) -
4088 sizeof(eseg->inline_hdr.start) + copysz, 16);
4089 *size += stride / 16;
4090 *seg += stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004091
Guy Levi34f4c952018-11-26 08:15:50 +02004092 if (copysz < left) {
4093 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02004094 left -= copysz;
4095 pdata += copysz;
Guy Levi34f4c952018-11-26 08:15:50 +02004096 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, pdata,
4097 left);
Erez Shitritf0313962016-02-21 16:27:17 +02004098 }
Guy Levi34f4c952018-11-26 08:15:50 +02004099
4100 return;
Erez Shitritf0313962016-02-21 16:27:17 +02004101 }
4102
Guy Levi34f4c952018-11-26 08:15:50 +02004103 *seg += sizeof(struct mlx5_wqe_eth_seg);
4104 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
Erez Shitritf0313962016-02-21 16:27:17 +02004105}
4106
Eli Cohene126ba92013-07-07 17:25:49 +03004107static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004108 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004109{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004110 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
4111 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
4112 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004113}
4114
4115static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
4116{
4117 dseg->byte_count = cpu_to_be32(sg->length);
4118 dseg->lkey = cpu_to_be32(sg->lkey);
4119 dseg->addr = cpu_to_be64(sg->addr);
4120}
4121
Artemy Kovalyov31616252017-01-02 11:37:42 +02004122static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03004123{
Artemy Kovalyov31616252017-01-02 11:37:42 +02004124 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
4125 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03004126}
4127
4128static __be64 frwr_mkey_mask(void)
4129{
4130 u64 result;
4131
4132 result = MLX5_MKEY_MASK_LEN |
4133 MLX5_MKEY_MASK_PAGE_SIZE |
4134 MLX5_MKEY_MASK_START_ADDR |
4135 MLX5_MKEY_MASK_EN_RINVAL |
4136 MLX5_MKEY_MASK_KEY |
4137 MLX5_MKEY_MASK_LR |
4138 MLX5_MKEY_MASK_LW |
4139 MLX5_MKEY_MASK_RR |
4140 MLX5_MKEY_MASK_RW |
4141 MLX5_MKEY_MASK_A |
4142 MLX5_MKEY_MASK_SMALL_FENCE |
4143 MLX5_MKEY_MASK_FREE;
4144
4145 return cpu_to_be64(result);
4146}
4147
Sagi Grimberge6631812014-02-23 14:19:11 +02004148static __be64 sig_mkey_mask(void)
4149{
4150 u64 result;
4151
4152 result = MLX5_MKEY_MASK_LEN |
4153 MLX5_MKEY_MASK_PAGE_SIZE |
4154 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004155 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02004156 MLX5_MKEY_MASK_EN_RINVAL |
4157 MLX5_MKEY_MASK_KEY |
4158 MLX5_MKEY_MASK_LR |
4159 MLX5_MKEY_MASK_LW |
4160 MLX5_MKEY_MASK_RR |
4161 MLX5_MKEY_MASK_RW |
4162 MLX5_MKEY_MASK_SMALL_FENCE |
4163 MLX5_MKEY_MASK_FREE |
4164 MLX5_MKEY_MASK_BSF_EN;
4165
4166 return cpu_to_be64(result);
4167}
4168
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004169static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004170 struct mlx5_ib_mr *mr, u8 flags)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004171{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004172 int size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004173
4174 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004175
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004176 umr->flags = flags;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004177 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004178 umr->mkey_mask = frwr_mkey_mask();
4179}
4180
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004181static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03004182{
4183 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004184 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03004185 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03004186}
4187
Artemy Kovalyov31616252017-01-02 11:37:42 +02004188static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004189{
4190 u64 result;
4191
Artemy Kovalyov31616252017-01-02 11:37:42 +02004192 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02004193 MLX5_MKEY_MASK_FREE;
4194
4195 return cpu_to_be64(result);
4196}
4197
Artemy Kovalyov31616252017-01-02 11:37:42 +02004198static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004199{
4200 u64 result;
4201
4202 result = MLX5_MKEY_MASK_FREE;
4203
4204 return cpu_to_be64(result);
4205}
4206
Noa Osherovich56e11d62016-02-29 16:46:51 +02004207static __be64 get_umr_update_translation_mask(void)
4208{
4209 u64 result;
4210
4211 result = MLX5_MKEY_MASK_LEN |
4212 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004213 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004214
4215 return cpu_to_be64(result);
4216}
4217
Artemy Kovalyov31616252017-01-02 11:37:42 +02004218static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02004219{
4220 u64 result;
4221
Artemy Kovalyov31616252017-01-02 11:37:42 +02004222 result = MLX5_MKEY_MASK_LR |
4223 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004224 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004225 MLX5_MKEY_MASK_RW;
4226
4227 if (atomic)
4228 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004229
4230 return cpu_to_be64(result);
4231}
4232
4233static __be64 get_umr_update_pd_mask(void)
4234{
4235 u64 result;
4236
Artemy Kovalyov31616252017-01-02 11:37:42 +02004237 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004238
4239 return cpu_to_be64(result);
4240}
4241
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004242static int umr_check_mkey_mask(struct mlx5_ib_dev *dev, u64 mask)
4243{
4244 if ((mask & MLX5_MKEY_MASK_PAGE_SIZE &&
4245 MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) ||
4246 (mask & MLX5_MKEY_MASK_A &&
4247 MLX5_CAP_GEN(dev->mdev, umr_modify_atomic_disabled)))
4248 return -EPERM;
4249 return 0;
4250}
4251
4252static int set_reg_umr_segment(struct mlx5_ib_dev *dev,
4253 struct mlx5_wqe_umr_ctrl_seg *umr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004254 const struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004255{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004256 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03004257
4258 memset(umr, 0, sizeof(*umr));
4259
Haggai Eran968e78d2014-12-11 17:04:11 +02004260 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
4261 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
4262 else
4263 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
4264
Artemy Kovalyov31616252017-01-02 11:37:42 +02004265 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
4266 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
4267 u64 offset = get_xlt_octo(umrwr->offset);
4268
4269 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
4270 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
4271 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004272 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02004273 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
4274 umr->mkey_mask |= get_umr_update_translation_mask();
4275 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
4276 umr->mkey_mask |= get_umr_update_access_mask(atomic);
4277 umr->mkey_mask |= get_umr_update_pd_mask();
4278 }
4279 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
4280 umr->mkey_mask |= get_umr_enable_mr_mask();
4281 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
4282 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03004283
4284 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02004285 umr->flags |= MLX5_UMR_INLINE;
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004286
4287 return umr_check_mkey_mask(dev, be64_to_cpu(umr->mkey_mask));
Eli Cohene126ba92013-07-07 17:25:49 +03004288}
4289
4290static u8 get_umr_flags(int acc)
4291{
4292 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
4293 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
4294 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
4295 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02004296 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004297}
4298
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004299static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
4300 struct mlx5_ib_mr *mr,
4301 u32 key, int access)
4302{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004303 int ndescs = ALIGN(mr->ndescs + mr->meta_ndescs, 8) >> 1;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004304
4305 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004306
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004307 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004308 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004309 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004310 /* KLMs take twice the size of MTTs */
4311 ndescs *= 2;
4312
4313 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004314 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
4315 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
4316 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
4317 seg->len = cpu_to_be64(mr->ibmr.length);
4318 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004319}
4320
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004321static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03004322{
4323 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004324 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004325}
4326
Bart Van Asschef696bf62018-07-18 09:25:14 -07004327static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg,
4328 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004329{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004330 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004331
Eli Cohene126ba92013-07-07 17:25:49 +03004332 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02004333 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02004334 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004335
Haggai Eran968e78d2014-12-11 17:04:11 +02004336 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004337 if (umrwr->pd)
4338 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
4339 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
4340 !umrwr->length)
4341 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
4342
4343 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004344 seg->len = cpu_to_be64(umrwr->length);
4345 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03004346 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02004347 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03004348}
4349
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004350static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
4351 struct mlx5_ib_mr *mr,
4352 struct mlx5_ib_pd *pd)
4353{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004354 int bcount = mr->desc_size * (mr->ndescs + mr->meta_ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004355
4356 dseg->addr = cpu_to_be64(mr->desc_map);
4357 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
4358 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
4359}
4360
Bart Van Asschef696bf62018-07-18 09:25:14 -07004361static __be32 send_ieth(const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004362{
4363 switch (wr->opcode) {
4364 case IB_WR_SEND_WITH_IMM:
4365 case IB_WR_RDMA_WRITE_WITH_IMM:
4366 return wr->ex.imm_data;
4367
4368 case IB_WR_SEND_WITH_INV:
4369 return cpu_to_be32(wr->ex.invalidate_rkey);
4370
4371 default:
4372 return 0;
4373 }
4374}
4375
4376static u8 calc_sig(void *wqe, int size)
4377{
4378 u8 *p = wqe;
4379 u8 res = 0;
4380 int i;
4381
4382 for (i = 0; i < size; i++)
4383 res ^= p[i];
4384
4385 return ~res;
4386}
4387
4388static u8 wq_sig(void *wqe)
4389{
4390 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
4391}
4392
Bart Van Asschef696bf62018-07-18 09:25:14 -07004393static int set_data_inl_seg(struct mlx5_ib_qp *qp, const struct ib_send_wr *wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004394 void **wqe, int *wqe_sz, void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004395{
4396 struct mlx5_wqe_inline_seg *seg;
Guy Levi34f4c952018-11-26 08:15:50 +02004397 size_t offset;
Eli Cohene126ba92013-07-07 17:25:49 +03004398 int inl = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004399 int i;
4400
Guy Levi34f4c952018-11-26 08:15:50 +02004401 seg = *wqe;
4402 *wqe += sizeof(*seg);
4403 offset = sizeof(*seg);
4404
Eli Cohene126ba92013-07-07 17:25:49 +03004405 for (i = 0; i < wr->num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02004406 size_t len = wr->sg_list[i].length;
4407 void *addr = (void *)(unsigned long)(wr->sg_list[i].addr);
4408
Eli Cohene126ba92013-07-07 17:25:49 +03004409 inl += len;
4410
4411 if (unlikely(inl > qp->max_inline_data))
4412 return -ENOMEM;
4413
Guy Levi34f4c952018-11-26 08:15:50 +02004414 while (likely(len)) {
4415 size_t leftlen;
4416 size_t copysz;
4417
4418 handle_post_send_edge(&qp->sq, wqe,
4419 *wqe_sz + (offset >> 4),
4420 cur_edge);
4421
4422 leftlen = *cur_edge - *wqe;
4423 copysz = min_t(size_t, leftlen, len);
4424
4425 memcpy(*wqe, addr, copysz);
4426 len -= copysz;
4427 addr += copysz;
4428 *wqe += copysz;
4429 offset += copysz;
Eli Cohene126ba92013-07-07 17:25:49 +03004430 }
Eli Cohene126ba92013-07-07 17:25:49 +03004431 }
4432
4433 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
4434
Guy Levi34f4c952018-11-26 08:15:50 +02004435 *wqe_sz += ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004436
4437 return 0;
4438}
4439
Sagi Grimberge6631812014-02-23 14:19:11 +02004440static u16 prot_field_size(enum ib_signature_type type)
4441{
4442 switch (type) {
4443 case IB_SIG_TYPE_T10_DIF:
4444 return MLX5_DIF_SIZE;
4445 default:
4446 return 0;
4447 }
4448}
4449
4450static u8 bs_selector(int block_size)
4451{
4452 switch (block_size) {
4453 case 512: return 0x1;
4454 case 520: return 0x2;
4455 case 4096: return 0x3;
4456 case 4160: return 0x4;
4457 case 1073741824: return 0x5;
4458 default: return 0;
4459 }
4460}
4461
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004462static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
4463 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02004464{
Sagi Grimberg142537f2014-08-13 19:54:32 +03004465 /* Valid inline section and allow BSF refresh */
4466 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
4467 MLX5_BSF_REFRESH_DIF);
4468 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
4469 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004470 /* repeating block */
4471 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
4472 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
4473 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004474
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004475 if (domain->sig.dif.ref_remap)
4476 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02004477
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004478 if (domain->sig.dif.app_escape) {
4479 if (domain->sig.dif.ref_escape)
4480 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
4481 else
4482 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02004483 }
4484
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004485 inl->dif_app_bitmask_check =
4486 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02004487}
4488
4489static int mlx5_set_bsf(struct ib_mr *sig_mr,
4490 struct ib_sig_attrs *sig_attrs,
4491 struct mlx5_bsf *bsf, u32 data_size)
4492{
4493 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
4494 struct mlx5_bsf_basic *basic = &bsf->basic;
4495 struct ib_sig_domain *mem = &sig_attrs->mem;
4496 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02004497
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004498 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02004499
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004500 /* Basic + Extended + Inline */
4501 basic->bsf_size_sbs = 1 << 7;
4502 /* Input domain check byte mask */
4503 basic->check_byte_mask = sig_attrs->check_mask;
4504 basic->raw_data_size = cpu_to_be32(data_size);
4505
4506 /* Memory domain */
4507 switch (sig_attrs->mem.sig_type) {
4508 case IB_SIG_TYPE_NONE:
4509 break;
4510 case IB_SIG_TYPE_T10_DIF:
4511 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
4512 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
4513 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
4514 break;
4515 default:
4516 return -EINVAL;
4517 }
4518
4519 /* Wire domain */
4520 switch (sig_attrs->wire.sig_type) {
4521 case IB_SIG_TYPE_NONE:
4522 break;
4523 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02004524 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004525 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004526 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03004527 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02004528 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004529 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004530 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004531 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004532 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004533 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02004534 } else
4535 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
4536
Sagi Grimberg142537f2014-08-13 19:54:32 +03004537 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004538 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02004539 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004540 default:
4541 return -EINVAL;
4542 }
4543
4544 return 0;
4545}
4546
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004547static int set_sig_data_segment(const struct ib_send_wr *send_wr,
4548 struct ib_mr *sig_mr,
4549 struct ib_sig_attrs *sig_attrs,
4550 struct mlx5_ib_qp *qp, void **seg, int *size,
4551 void **cur_edge)
Sagi Grimberge6631812014-02-23 14:19:11 +02004552{
Sagi Grimberge6631812014-02-23 14:19:11 +02004553 struct mlx5_bsf *bsf;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004554 u32 data_len;
4555 u32 data_key;
4556 u64 data_va;
4557 u32 prot_len = 0;
4558 u32 prot_key = 0;
4559 u64 prot_va = 0;
4560 bool prot = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004561 int ret;
4562 int wqe_size;
4563
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004564 if (send_wr->opcode == IB_WR_REG_SIG_MR) {
4565 const struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
4566
4567 data_len = wr->wr.sg_list->length;
4568 data_key = wr->wr.sg_list->lkey;
4569 data_va = wr->wr.sg_list->addr;
4570 if (wr->prot) {
4571 prot_len = wr->prot->length;
4572 prot_key = wr->prot->lkey;
4573 prot_va = wr->prot->addr;
4574 prot = true;
4575 }
4576 } else {
4577 struct mlx5_ib_mr *mr = to_mmr(sig_mr);
4578 struct mlx5_ib_mr *pi_mr = mr->pi_mr;
4579
4580 data_len = pi_mr->data_length;
4581 data_key = pi_mr->ibmr.lkey;
4582 data_va = pi_mr->ibmr.iova;
4583 if (pi_mr->meta_ndescs) {
4584 prot_len = pi_mr->meta_length;
4585 prot_key = pi_mr->ibmr.lkey;
4586 prot_va = pi_mr->ibmr.iova + data_len;
4587 prot = true;
4588 }
4589 }
4590
4591 if (!prot || (data_key == prot_key && data_va == prot_va &&
4592 data_len == prot_len)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004593 /**
4594 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004595 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02004596 * So need construct:
4597 * ------------------
4598 * | data_klm |
4599 * ------------------
4600 * | BSF |
4601 * ------------------
4602 **/
4603 struct mlx5_klm *data_klm = *seg;
4604
4605 data_klm->bcount = cpu_to_be32(data_len);
4606 data_klm->key = cpu_to_be32(data_key);
4607 data_klm->va = cpu_to_be64(data_va);
4608 wqe_size = ALIGN(sizeof(*data_klm), 64);
4609 } else {
4610 /**
4611 * Source domain contains signature information
4612 * So need construct a strided block format:
4613 * ---------------------------
4614 * | stride_block_ctrl |
4615 * ---------------------------
4616 * | data_klm |
4617 * ---------------------------
4618 * | prot_klm |
4619 * ---------------------------
4620 * | BSF |
4621 * ---------------------------
4622 **/
4623 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
4624 struct mlx5_stride_block_entry *data_sentry;
4625 struct mlx5_stride_block_entry *prot_sentry;
Sagi Grimberge6631812014-02-23 14:19:11 +02004626 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
4627 int prot_size;
4628
4629 sblock_ctrl = *seg;
4630 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
4631 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
4632
4633 prot_size = prot_field_size(sig_attrs->mem.sig_type);
4634 if (!prot_size) {
4635 pr_err("Bad block size given: %u\n", block_size);
4636 return -EINVAL;
4637 }
4638 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
4639 prot_size);
4640 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
4641 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
4642 sblock_ctrl->num_entries = cpu_to_be16(2);
4643
4644 data_sentry->bcount = cpu_to_be16(block_size);
4645 data_sentry->key = cpu_to_be32(data_key);
4646 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004647 data_sentry->stride = cpu_to_be16(block_size);
4648
Sagi Grimberge6631812014-02-23 14:19:11 +02004649 prot_sentry->bcount = cpu_to_be16(prot_size);
4650 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004651 prot_sentry->va = cpu_to_be64(prot_va);
4652 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004653
Sagi Grimberge6631812014-02-23 14:19:11 +02004654 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
4655 sizeof(*prot_sentry), 64);
4656 }
4657
4658 *seg += wqe_size;
4659 *size += wqe_size / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004660 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004661
4662 bsf = *seg;
4663 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
4664 if (ret)
4665 return -EINVAL;
4666
4667 *seg += sizeof(*bsf);
4668 *size += sizeof(*bsf) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004669 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004670
4671 return 0;
4672}
4673
4674static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004675 struct ib_mr *sig_mr, int access_flags,
4676 u32 size, u32 length, u32 pdn)
Sagi Grimberge6631812014-02-23 14:19:11 +02004677{
Sagi Grimberge6631812014-02-23 14:19:11 +02004678 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004679 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004680
4681 memset(seg, 0, sizeof(*seg));
4682
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004683 seg->flags = get_umr_flags(access_flags) | MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004684 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004685 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004686 MLX5_MKEY_BSF_EN | pdn);
4687 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004688 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004689 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4690}
4691
4692static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004693 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004694{
4695 memset(umr, 0, sizeof(*umr));
4696
4697 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004698 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004699 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4700 umr->mkey_mask = sig_mkey_mask();
4701}
4702
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004703static int set_pi_umr_wr(const struct ib_send_wr *send_wr,
4704 struct mlx5_ib_qp *qp, void **seg, int *size,
4705 void **cur_edge)
4706{
4707 const struct ib_reg_wr *wr = reg_wr(send_wr);
4708 struct mlx5_ib_mr *sig_mr = to_mmr(wr->mr);
4709 struct mlx5_ib_mr *pi_mr = sig_mr->pi_mr;
4710 struct ib_sig_attrs *sig_attrs = sig_mr->ibmr.sig_attrs;
4711 u32 pdn = get_pd(qp)->pdn;
4712 u32 xlt_size;
4713 int region_len, ret;
4714
4715 if (unlikely(send_wr->num_sge != 0) ||
4716 unlikely(wr->access & IB_ACCESS_REMOTE_ATOMIC) ||
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +03004717 unlikely(!sig_mr->sig) || unlikely(!qp->integrity_en) ||
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004718 unlikely(!sig_mr->sig->sig_status_checked))
4719 return -EINVAL;
4720
4721 /* length of the protected region, data + protection */
4722 region_len = pi_mr->ibmr.length;
4723
4724 /**
4725 * KLM octoword size - if protection was provided
4726 * then we use strided block format (3 octowords),
4727 * else we use single KLM (1 octoword)
4728 **/
4729 if (sig_attrs->mem.sig_type != IB_SIG_TYPE_NONE)
4730 xlt_size = 0x30;
4731 else
4732 xlt_size = sizeof(struct mlx5_klm);
4733
4734 set_sig_umr_segment(*seg, xlt_size);
4735 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4736 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4737 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4738
4739 set_sig_mkey_segment(*seg, wr->mr, wr->access, xlt_size, region_len,
4740 pdn);
4741 *seg += sizeof(struct mlx5_mkey_seg);
4742 *size += sizeof(struct mlx5_mkey_seg) / 16;
4743 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4744
4745 ret = set_sig_data_segment(send_wr, wr->mr, sig_attrs, qp, seg, size,
4746 cur_edge);
4747 if (ret)
4748 return ret;
4749
4750 sig_mr->sig->sig_status_checked = false;
4751 return 0;
4752}
Sagi Grimberge6631812014-02-23 14:19:11 +02004753
Bart Van Asschef696bf62018-07-18 09:25:14 -07004754static int set_sig_umr_wr(const struct ib_send_wr *send_wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004755 struct mlx5_ib_qp *qp, void **seg, int *size,
4756 void **cur_edge)
Sagi Grimberge6631812014-02-23 14:19:11 +02004757{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004758 const struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004759 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004760 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004761 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02004762 int region_len, ret;
4763
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004764 if (unlikely(wr->wr.num_sge != 1) ||
4765 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +03004766 unlikely(!sig_mr->sig) || unlikely(!qp->integrity_en) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004767 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02004768 return -EINVAL;
4769
4770 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004771 region_len = wr->wr.sg_list->length;
4772 if (wr->prot &&
4773 (wr->prot->lkey != wr->wr.sg_list->lkey ||
4774 wr->prot->addr != wr->wr.sg_list->addr ||
4775 wr->prot->length != wr->wr.sg_list->length))
4776 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02004777
4778 /**
4779 * KLM octoword size - if protection was provided
4780 * then we use strided block format (3 octowords),
4781 * else we use single KLM (1 octoword)
4782 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02004783 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02004784
Artemy Kovalyov31616252017-01-02 11:37:42 +02004785 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004786 *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 Grimberge6631812014-02-23 14:19:11 +02004789
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004790 set_sig_mkey_segment(*seg, wr->sig_mr, wr->access_flags, xlt_size,
4791 region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02004792 *seg += sizeof(struct mlx5_mkey_seg);
4793 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004794 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004795
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004796 ret = set_sig_data_segment(send_wr, wr->sig_mr, wr->sig_attrs, qp, seg,
4797 size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004798 if (ret)
4799 return ret;
4800
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004801 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004802 return 0;
4803}
4804
4805static int set_psv_wr(struct ib_sig_domain *domain,
4806 u32 psv_idx, void **seg, int *size)
4807{
4808 struct mlx5_seg_set_psv *psv_seg = *seg;
4809
4810 memset(psv_seg, 0, sizeof(*psv_seg));
4811 psv_seg->psv_num = cpu_to_be32(psv_idx);
4812 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004813 case IB_SIG_TYPE_NONE:
4814 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004815 case IB_SIG_TYPE_T10_DIF:
4816 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4817 domain->sig.dif.app_tag);
4818 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004819 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004820 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004821 pr_err("Bad signature type (%d) is given.\n",
4822 domain->sig_type);
4823 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004824 }
4825
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004826 *seg += sizeof(*psv_seg);
4827 *size += sizeof(*psv_seg) / 16;
4828
Sagi Grimberge6631812014-02-23 14:19:11 +02004829 return 0;
4830}
4831
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004832static int set_reg_wr(struct mlx5_ib_qp *qp,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004833 const struct ib_reg_wr *wr,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004834 void **seg, int *size, void **cur_edge,
4835 bool check_not_free)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004836{
4837 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4838 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004839 int mr_list_size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Idan Burstein064e5262018-05-02 13:16:39 +03004840 bool umr_inline = mr_list_size <= MLX5_IB_SQ_UMR_INLINE_THRESHOLD;
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004841 u8 flags = 0;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004842
4843 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4844 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4845 "Invalid IB_SEND_INLINE send flag\n");
4846 return -EINVAL;
4847 }
4848
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004849 if (check_not_free)
4850 flags |= MLX5_UMR_CHECK_NOT_FREE;
4851 if (umr_inline)
4852 flags |= MLX5_UMR_INLINE;
4853
4854 set_reg_umr_seg(*seg, mr, flags);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004855 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4856 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004857 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004858
4859 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4860 *seg += sizeof(struct mlx5_mkey_seg);
4861 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004862 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004863
Idan Burstein064e5262018-05-02 13:16:39 +03004864 if (umr_inline) {
Guy Levi34f4c952018-11-26 08:15:50 +02004865 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, mr->descs,
4866 mr_list_size);
4867 *size = ALIGN(*size, MLX5_SEND_WQE_BB >> 4);
Idan Burstein064e5262018-05-02 13:16:39 +03004868 } else {
4869 set_reg_data_seg(*seg, mr, pd);
4870 *seg += sizeof(struct mlx5_wqe_data_seg);
4871 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4872 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004873 return 0;
4874}
4875
Guy Levi34f4c952018-11-26 08:15:50 +02004876static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size,
4877 void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004878{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004879 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004880 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4881 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004882 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004883 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004884 *seg += sizeof(struct mlx5_mkey_seg);
4885 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004886 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03004887}
4888
Guy Levi34f4c952018-11-26 08:15:50 +02004889static void dump_wqe(struct mlx5_ib_qp *qp, u32 idx, int size_16)
Eli Cohene126ba92013-07-07 17:25:49 +03004890{
4891 __be32 *p = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004892 int i, j;
4893
Guy Levi34f4c952018-11-26 08:15:50 +02004894 pr_debug("dump WQE index %u:\n", idx);
Eli Cohene126ba92013-07-07 17:25:49 +03004895 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4896 if ((i & 0xf) == 0) {
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004897 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, idx);
Guy Levi34f4c952018-11-26 08:15:50 +02004898 pr_debug("WQBB at %p:\n", (void *)p);
Eli Cohene126ba92013-07-07 17:25:49 +03004899 j = 0;
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004900 idx = (idx + 1) & (qp->sq.wqe_cnt - 1);
Eli Cohene126ba92013-07-07 17:25:49 +03004901 }
4902 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4903 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4904 be32_to_cpu(p[j + 3]));
4905 }
4906}
4907
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004908static int __begin_wqe(struct mlx5_ib_qp *qp, void **seg,
Guy Levi34f4c952018-11-26 08:15:50 +02004909 struct mlx5_wqe_ctrl_seg **ctrl,
4910 const struct ib_send_wr *wr, unsigned int *idx,
4911 int *size, void **cur_edge, int nreq,
4912 bool send_signaled, bool solicited)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004913{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004914 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4915 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004916
4917 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
Guy Levi34f4c952018-11-26 08:15:50 +02004918 *seg = mlx5_frag_buf_get_wqe(&qp->sq.fbc, *idx);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004919 *ctrl = *seg;
4920 *(uint32_t *)(*seg + 8) = 0;
4921 (*ctrl)->imm = send_ieth(wr);
4922 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004923 (send_signaled ? MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4924 (solicited ? MLX5_WQE_CTRL_SOLICITED : 0);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004925
4926 *seg += sizeof(**ctrl);
4927 *size = sizeof(**ctrl) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004928 *cur_edge = qp->sq.cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004929
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004930 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004931}
4932
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004933static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4934 struct mlx5_wqe_ctrl_seg **ctrl,
4935 const struct ib_send_wr *wr, unsigned *idx,
Guy Levi34f4c952018-11-26 08:15:50 +02004936 int *size, void **cur_edge, int nreq)
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004937{
Guy Levi34f4c952018-11-26 08:15:50 +02004938 return __begin_wqe(qp, seg, ctrl, wr, idx, size, cur_edge, nreq,
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004939 wr->send_flags & IB_SEND_SIGNALED,
4940 wr->send_flags & IB_SEND_SOLICITED);
4941}
4942
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004943static void finish_wqe(struct mlx5_ib_qp *qp,
4944 struct mlx5_wqe_ctrl_seg *ctrl,
Guy Levi34f4c952018-11-26 08:15:50 +02004945 void *seg, u8 size, void *cur_edge,
4946 unsigned int idx, u64 wr_id, int nreq, u8 fence,
4947 u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004948{
4949 u8 opmod = 0;
4950
4951 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
4952 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02004953 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004954 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004955 if (unlikely(qp->wq_sig))
4956 ctrl->signature = wq_sig(ctrl);
4957
4958 qp->sq.wrid[idx] = wr_id;
4959 qp->sq.w_list[idx].opcode = mlx5_opcode;
4960 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
4961 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
4962 qp->sq.w_list[idx].next = qp->sq.cur_post;
Guy Levi34f4c952018-11-26 08:15:50 +02004963
4964 /* We save the edge which was possibly updated during the WQE
4965 * construction, into SQ's cache.
4966 */
4967 seg = PTR_ALIGN(seg, MLX5_SEND_WQE_BB);
4968 qp->sq.cur_edge = (unlikely(seg == cur_edge)) ?
4969 get_sq_edge(&qp->sq, qp->sq.cur_post &
4970 (qp->sq.wqe_cnt - 1)) :
4971 cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004972}
4973
Bart Van Assched34ac5c2018-07-18 09:25:32 -07004974static int _mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
4975 const struct ib_send_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03004976{
4977 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
4978 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004979 struct mlx5_core_dev *mdev = dev->mdev;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004980 struct ib_reg_wr reg_pi_wr;
Haggai Erand16e91d2016-02-29 15:45:05 +02004981 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02004982 struct mlx5_ib_mr *mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004983 struct mlx5_ib_mr *pi_mr;
4984 struct ib_sig_attrs *sig_attrs;
Eli Cohene126ba92013-07-07 17:25:49 +03004985 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02004986 struct mlx5_bf *bf;
Guy Levi34f4c952018-11-26 08:15:50 +02004987 void *cur_edge;
Eli Cohene126ba92013-07-07 17:25:49 +03004988 int uninitialized_var(size);
Eli Cohene126ba92013-07-07 17:25:49 +03004989 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03004990 unsigned idx;
4991 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004992 int num_sge;
4993 void *seg;
4994 int nreq;
4995 int i;
4996 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004997 u8 fence;
4998
Parav Pandit6c755202018-08-28 14:45:29 +03004999 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5000 !drain)) {
5001 *bad_wr = wr;
5002 return -EIO;
5003 }
5004
Haggai Erand16e91d2016-02-29 15:45:05 +02005005 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5006 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
5007
5008 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005009 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02005010
Eli Cohene126ba92013-07-07 17:25:49 +03005011 spin_lock_irqsave(&qp->sq.lock, flags);
5012
5013 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04005014 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03005015 mlx5_ib_warn(dev, "\n");
5016 err = -EINVAL;
5017 *bad_wr = wr;
5018 goto out;
5019 }
5020
Eli Cohene126ba92013-07-07 17:25:49 +03005021 num_sge = wr->num_sge;
5022 if (unlikely(num_sge > qp->sq.max_gs)) {
5023 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03005024 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03005025 *bad_wr = wr;
5026 goto out;
5027 }
5028
Guy Levi34f4c952018-11-26 08:15:50 +02005029 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, &cur_edge,
5030 nreq);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005031 if (err) {
5032 mlx5_ib_warn(dev, "\n");
5033 err = -ENOMEM;
5034 *bad_wr = wr;
5035 goto out;
5036 }
Eli Cohene126ba92013-07-07 17:25:49 +03005037
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005038 if (wr->opcode == IB_WR_REG_MR ||
5039 wr->opcode == IB_WR_REG_MR_INTEGRITY) {
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005040 fence = dev->umr_fence;
5041 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Majd Dibbiny074fca32018-11-05 08:07:37 +02005042 } else {
5043 if (wr->send_flags & IB_SEND_FENCE) {
5044 if (qp->next_fence)
5045 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
5046 else
5047 fence = MLX5_FENCE_MODE_FENCE;
5048 } else {
5049 fence = qp->next_fence;
5050 }
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005051 }
5052
Eli Cohene126ba92013-07-07 17:25:49 +03005053 switch (ibqp->qp_type) {
5054 case IB_QPT_XRC_INI:
5055 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03005056 seg += sizeof(*xrc);
5057 size += sizeof(*xrc) / 16;
5058 /* fall through */
5059 case IB_QPT_RC:
5060 switch (wr->opcode) {
5061 case IB_WR_RDMA_READ:
5062 case IB_WR_RDMA_WRITE:
5063 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005064 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5065 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005066 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005067 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5068 break;
5069
5070 case IB_WR_ATOMIC_CMP_AND_SWP:
5071 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03005072 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03005073 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
5074 err = -ENOSYS;
5075 *bad_wr = wr;
5076 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005077
5078 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03005079 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
5080 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Guy Levi34f4c952018-11-26 08:15:50 +02005081 set_linv_wr(qp, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005082 num_sge = 0;
5083 break;
5084
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005085 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005086 qp->sq.wr_data[idx] = IB_WR_REG_MR;
5087 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
Guy Levi34f4c952018-11-26 08:15:50 +02005088 err = set_reg_wr(qp, reg_wr(wr), &seg, &size,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03005089 &cur_edge, true);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005090 if (err) {
5091 *bad_wr = wr;
5092 goto out;
5093 }
5094 num_sge = 0;
5095 break;
5096
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005097 case IB_WR_REG_MR_INTEGRITY:
5098 memset(&reg_pi_wr, 0, sizeof(struct ib_reg_wr));
5099
5100 mr = to_mmr(reg_wr(wr)->mr);
5101 pi_mr = mr->pi_mr;
5102
5103 reg_pi_wr.mr = &pi_mr->ibmr;
5104 reg_pi_wr.access = reg_wr(wr)->access;
5105 reg_pi_wr.key = pi_mr->ibmr.rkey;
5106
5107 qp->sq.wr_data[idx] = IB_WR_REG_MR_INTEGRITY;
5108 ctrl->imm = cpu_to_be32(reg_pi_wr.key);
5109 /* UMR for data + protection registration */
5110 err = set_reg_wr(qp, &reg_pi_wr, &seg, &size,
5111 &cur_edge, false);
5112 if (err) {
5113 *bad_wr = wr;
5114 goto out;
5115 }
5116 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5117 wr->wr_id, nreq, fence,
5118 MLX5_OPCODE_UMR);
5119
5120 err = begin_wqe(qp, &seg, &ctrl, wr, &idx,
5121 &size, &cur_edge, nreq);
5122 if (err) {
5123 mlx5_ib_warn(dev, "\n");
5124 err = -ENOMEM;
5125 *bad_wr = wr;
5126 goto out;
5127 }
5128 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
5129 /* UMR for sig MR */
5130 err = set_pi_umr_wr(wr, qp, &seg, &size,
5131 &cur_edge);
5132 if (err) {
5133 mlx5_ib_warn(dev, "\n");
5134 *bad_wr = wr;
5135 goto out;
5136 }
5137 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5138 wr->wr_id, nreq, fence,
5139 MLX5_OPCODE_UMR);
5140
5141 /*
5142 * SET_PSV WQEs are not signaled and solicited
5143 * on error
5144 */
5145 sig_attrs = mr->ibmr.sig_attrs;
5146 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5147 &size, &cur_edge, nreq, false,
5148 true);
5149 if (err) {
5150 mlx5_ib_warn(dev, "\n");
5151 err = -ENOMEM;
5152 *bad_wr = wr;
5153 goto out;
5154 }
5155 err = set_psv_wr(&sig_attrs->mem,
5156 mr->sig->psv_memory.psv_idx,
5157 &seg, &size);
5158 if (err) {
5159 mlx5_ib_warn(dev, "\n");
5160 *bad_wr = wr;
5161 goto out;
5162 }
5163 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5164 wr->wr_id, nreq, next_fence,
5165 MLX5_OPCODE_SET_PSV);
5166
5167 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5168 &size, &cur_edge, nreq, false,
5169 true);
5170 if (err) {
5171 mlx5_ib_warn(dev, "\n");
5172 err = -ENOMEM;
5173 *bad_wr = wr;
5174 goto out;
5175 }
5176 err = set_psv_wr(&sig_attrs->wire,
5177 mr->sig->psv_wire.psv_idx,
5178 &seg, &size);
5179 if (err) {
5180 mlx5_ib_warn(dev, "\n");
5181 *bad_wr = wr;
5182 goto out;
5183 }
5184 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5185 wr->wr_id, nreq, next_fence,
5186 MLX5_OPCODE_SET_PSV);
5187
5188 qp->next_fence =
5189 MLX5_FENCE_MODE_INITIATOR_SMALL;
5190 num_sge = 0;
5191 goto skip_psv;
5192
Sagi Grimberge6631812014-02-23 14:19:11 +02005193 case IB_WR_REG_SIG_MR:
5194 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005195 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02005196
5197 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
Guy Levi34f4c952018-11-26 08:15:50 +02005198 err = set_sig_umr_wr(wr, qp, &seg, &size,
5199 &cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02005200 if (err) {
5201 mlx5_ib_warn(dev, "\n");
5202 *bad_wr = wr;
5203 goto out;
5204 }
5205
Guy Levi34f4c952018-11-26 08:15:50 +02005206 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5207 wr->wr_id, nreq, fence,
5208 MLX5_OPCODE_UMR);
Sagi Grimberge6631812014-02-23 14:19:11 +02005209 /*
5210 * SET_PSV WQEs are not signaled and solicited
5211 * on error
5212 */
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07005213 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
Guy Levi34f4c952018-11-26 08:15:50 +02005214 &size, &cur_edge, nreq, false,
5215 true);
Sagi Grimberge6631812014-02-23 14:19:11 +02005216 if (err) {
5217 mlx5_ib_warn(dev, "\n");
5218 err = -ENOMEM;
5219 *bad_wr = wr;
5220 goto out;
5221 }
5222
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005223 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02005224 mr->sig->psv_memory.psv_idx, &seg,
5225 &size);
5226 if (err) {
5227 mlx5_ib_warn(dev, "\n");
5228 *bad_wr = wr;
5229 goto out;
5230 }
5231
Guy Levi34f4c952018-11-26 08:15:50 +02005232 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5233 wr->wr_id, nreq, fence,
5234 MLX5_OPCODE_SET_PSV);
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07005235 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
Guy Levi34f4c952018-11-26 08:15:50 +02005236 &size, &cur_edge, nreq, false,
5237 true);
Sagi Grimberge6631812014-02-23 14:19:11 +02005238 if (err) {
5239 mlx5_ib_warn(dev, "\n");
5240 err = -ENOMEM;
5241 *bad_wr = wr;
5242 goto out;
5243 }
5244
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005245 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02005246 mr->sig->psv_wire.psv_idx, &seg,
5247 &size);
5248 if (err) {
5249 mlx5_ib_warn(dev, "\n");
5250 *bad_wr = wr;
5251 goto out;
5252 }
5253
Guy Levi34f4c952018-11-26 08:15:50 +02005254 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5255 wr->wr_id, nreq, fence,
5256 MLX5_OPCODE_SET_PSV);
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005257 qp->next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Sagi Grimberge6631812014-02-23 14:19:11 +02005258 num_sge = 0;
5259 goto skip_psv;
5260
Eli Cohene126ba92013-07-07 17:25:49 +03005261 default:
5262 break;
5263 }
5264 break;
5265
5266 case IB_QPT_UC:
5267 switch (wr->opcode) {
5268 case IB_WR_RDMA_WRITE:
5269 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005270 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5271 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03005272 seg += sizeof(struct mlx5_wqe_raddr_seg);
5273 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5274 break;
5275
5276 default:
5277 break;
5278 }
5279 break;
5280
Eli Cohene126ba92013-07-07 17:25:49 +03005281 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02005282 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
5283 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
5284 err = -EPERM;
5285 *bad_wr = wr;
5286 goto out;
5287 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07005288 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02005289 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03005290 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005291 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005292 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005293 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
5294
Eli Cohene126ba92013-07-07 17:25:49 +03005295 break;
Erez Shitritf0313962016-02-21 16:27:17 +02005296 case IB_QPT_UD:
5297 set_datagram_seg(seg, wr);
5298 seg += sizeof(struct mlx5_wqe_datagram_seg);
5299 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005300 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005301
5302 /* handle qp that supports ud offload */
5303 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
5304 struct mlx5_wqe_eth_pad *pad;
5305
5306 pad = seg;
5307 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
5308 seg += sizeof(struct mlx5_wqe_eth_pad);
5309 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005310 set_eth_seg(wr, qp, &seg, &size, &cur_edge);
5311 handle_post_send_edge(&qp->sq, &seg, size,
5312 &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005313 }
5314 break;
Eli Cohene126ba92013-07-07 17:25:49 +03005315 case MLX5_IB_QPT_REG_UMR:
5316 if (wr->opcode != MLX5_IB_WR_UMR) {
5317 err = -EINVAL;
5318 mlx5_ib_warn(dev, "bad opcode\n");
5319 goto out;
5320 }
5321 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005322 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02005323 err = set_reg_umr_segment(dev, seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
5324 if (unlikely(err))
5325 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005326 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
5327 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005328 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005329 set_reg_mkey_segment(seg, wr);
5330 seg += sizeof(struct mlx5_mkey_seg);
5331 size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005332 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005333 break;
5334
5335 default:
5336 break;
5337 }
5338
5339 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
Guy Levi34f4c952018-11-26 08:15:50 +02005340 err = set_data_inl_seg(qp, wr, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005341 if (unlikely(err)) {
5342 mlx5_ib_warn(dev, "\n");
5343 *bad_wr = wr;
5344 goto out;
5345 }
Eli Cohene126ba92013-07-07 17:25:49 +03005346 } else {
Eli Cohene126ba92013-07-07 17:25:49 +03005347 for (i = 0; i < num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02005348 handle_post_send_edge(&qp->sq, &seg, size,
5349 &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005350 if (likely(wr->sg_list[i].length)) {
Guy Levi34f4c952018-11-26 08:15:50 +02005351 set_data_ptr_seg
5352 ((struct mlx5_wqe_data_seg *)seg,
5353 wr->sg_list + i);
Eli Cohene126ba92013-07-07 17:25:49 +03005354 size += sizeof(struct mlx5_wqe_data_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005355 seg += sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005356 }
5357 }
5358 }
5359
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005360 qp->next_fence = next_fence;
Guy Levi34f4c952018-11-26 08:15:50 +02005361 finish_wqe(qp, ctrl, seg, size, cur_edge, idx, wr->wr_id, nreq,
5362 fence, mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02005363skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03005364 if (0)
5365 dump_wqe(qp, idx, size);
5366 }
5367
5368out:
5369 if (likely(nreq)) {
5370 qp->sq.head += nreq;
5371
5372 /* Make sure that descriptors are written before
5373 * updating doorbell record and ringing the doorbell
5374 */
5375 wmb();
5376
5377 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
5378
Eli Cohenada388f2014-01-14 17:45:16 +02005379 /* Make sure doorbell record is visible to the HCA before
5380 * we hit doorbell */
5381 wmb();
5382
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005383 /* currently we support only regular doorbells */
Maxim Mikityanskiybbf29f62019-03-29 15:37:52 -07005384 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005385 /* Make sure doorbells don't leak out of SQ spinlock
5386 * and reach the HCA out of order.
5387 */
Eli Cohene126ba92013-07-07 17:25:49 +03005388 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03005389 }
5390
5391 spin_unlock_irqrestore(&qp->sq.lock, flags);
5392
5393 return err;
5394}
5395
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005396int mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
5397 const struct ib_send_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005398{
5399 return _mlx5_ib_post_send(ibqp, wr, bad_wr, false);
5400}
5401
Eli Cohene126ba92013-07-07 17:25:49 +03005402static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
5403{
5404 sig->signature = calc_sig(sig, size);
5405}
5406
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005407static int _mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5408 const struct ib_recv_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03005409{
5410 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5411 struct mlx5_wqe_data_seg *scat;
5412 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03005413 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5414 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005415 unsigned long flags;
5416 int err = 0;
5417 int nreq;
5418 int ind;
5419 int i;
5420
Parav Pandit6c755202018-08-28 14:45:29 +03005421 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5422 !drain)) {
5423 *bad_wr = wr;
5424 return -EIO;
5425 }
5426
Haggai Erand16e91d2016-02-29 15:45:05 +02005427 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5428 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
5429
Eli Cohene126ba92013-07-07 17:25:49 +03005430 spin_lock_irqsave(&qp->rq.lock, flags);
5431
5432 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
5433
5434 for (nreq = 0; wr; nreq++, wr = wr->next) {
5435 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
5436 err = -ENOMEM;
5437 *bad_wr = wr;
5438 goto out;
5439 }
5440
5441 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
5442 err = -EINVAL;
5443 *bad_wr = wr;
5444 goto out;
5445 }
5446
Guy Levi34f4c952018-11-26 08:15:50 +02005447 scat = mlx5_frag_buf_get_wqe(&qp->rq.fbc, ind);
Eli Cohene126ba92013-07-07 17:25:49 +03005448 if (qp->wq_sig)
5449 scat++;
5450
5451 for (i = 0; i < wr->num_sge; i++)
5452 set_data_ptr_seg(scat + i, wr->sg_list + i);
5453
5454 if (i < qp->rq.max_gs) {
5455 scat[i].byte_count = 0;
5456 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
5457 scat[i].addr = 0;
5458 }
5459
5460 if (qp->wq_sig) {
5461 sig = (struct mlx5_rwqe_sig *)scat;
5462 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
5463 }
5464
5465 qp->rq.wrid[ind] = wr->wr_id;
5466
5467 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
5468 }
5469
5470out:
5471 if (likely(nreq)) {
5472 qp->rq.head += nreq;
5473
5474 /* Make sure that descriptors are written before
5475 * doorbell record.
5476 */
5477 wmb();
5478
5479 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
5480 }
5481
5482 spin_unlock_irqrestore(&qp->rq.lock, flags);
5483
5484 return err;
5485}
5486
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005487int mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5488 const struct ib_recv_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005489{
5490 return _mlx5_ib_post_recv(ibqp, wr, bad_wr, false);
5491}
5492
Eli Cohene126ba92013-07-07 17:25:49 +03005493static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
5494{
5495 switch (mlx5_state) {
5496 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
5497 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
5498 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
5499 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
5500 case MLX5_QP_STATE_SQ_DRAINING:
5501 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
5502 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
5503 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
5504 default: return -1;
5505 }
5506}
5507
5508static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
5509{
5510 switch (mlx5_mig_state) {
5511 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
5512 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
5513 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
5514 default: return -1;
5515 }
5516}
5517
5518static int to_ib_qp_access_flags(int mlx5_flags)
5519{
5520 int ib_flags = 0;
5521
5522 if (mlx5_flags & MLX5_QP_BIT_RRE)
5523 ib_flags |= IB_ACCESS_REMOTE_READ;
5524 if (mlx5_flags & MLX5_QP_BIT_RWE)
5525 ib_flags |= IB_ACCESS_REMOTE_WRITE;
5526 if (mlx5_flags & MLX5_QP_BIT_RAE)
5527 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
5528
5529 return ib_flags;
5530}
5531
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005532static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005533 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005534 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03005535{
Eli Cohene126ba92013-07-07 17:25:49 +03005536
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005537 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03005538
Jason Gunthorpee7996a92018-01-29 13:26:40 -07005539 if (!path->port || path->port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03005540 return;
5541
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02005542 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
5543
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005544 rdma_ah_set_port_num(ah_attr, path->port);
5545 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03005546
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005547 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
5548 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
5549 rdma_ah_set_static_rate(ah_attr,
5550 path->static_rate ? path->static_rate - 5 : 0);
5551 if (path->grh_mlid & (1 << 7)) {
5552 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
5553
5554 rdma_ah_set_grh(ah_attr, NULL,
5555 tc_fl & 0xfffff,
5556 path->mgid_index,
5557 path->hop_limit,
5558 (tc_fl >> 20) & 0xff);
5559 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03005560 }
5561}
5562
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005563static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
5564 struct mlx5_ib_sq *sq,
5565 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03005566{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005567 int err;
5568
Eran Ben Elisha28160772017-12-26 15:17:05 +02005569 err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005570 if (err)
5571 goto out;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005572 sq->state = *sq_state;
5573
5574out:
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005575 return err;
5576}
5577
5578static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
5579 struct mlx5_ib_rq *rq,
5580 u8 *rq_state)
5581{
5582 void *out;
5583 void *rqc;
5584 int inlen;
5585 int err;
5586
5587 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005588 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005589 if (!out)
5590 return -ENOMEM;
5591
5592 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
5593 if (err)
5594 goto out;
5595
5596 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
5597 *rq_state = MLX5_GET(rqc, rqc, state);
5598 rq->state = *rq_state;
5599
5600out:
5601 kvfree(out);
5602 return err;
5603}
5604
5605static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
5606 struct mlx5_ib_qp *qp, u8 *qp_state)
5607{
5608 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
5609 [MLX5_RQC_STATE_RST] = {
5610 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5611 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5612 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
5613 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
5614 },
5615 [MLX5_RQC_STATE_RDY] = {
5616 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5617 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5618 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
5619 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
5620 },
5621 [MLX5_RQC_STATE_ERR] = {
5622 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5623 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5624 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
5625 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
5626 },
5627 [MLX5_RQ_STATE_NA] = {
5628 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5629 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5630 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
5631 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
5632 },
5633 };
5634
5635 *qp_state = sqrq_trans[rq_state][sq_state];
5636
5637 if (*qp_state == MLX5_QP_STATE_BAD) {
5638 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
5639 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
5640 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
5641 return -EINVAL;
5642 }
5643
5644 if (*qp_state == MLX5_QP_STATE)
5645 *qp_state = qp->state;
5646
5647 return 0;
5648}
5649
5650static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
5651 struct mlx5_ib_qp *qp,
5652 u8 *raw_packet_qp_state)
5653{
5654 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
5655 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
5656 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
5657 int err;
5658 u8 sq_state = MLX5_SQ_STATE_NA;
5659 u8 rq_state = MLX5_RQ_STATE_NA;
5660
5661 if (qp->sq.wqe_cnt) {
5662 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
5663 if (err)
5664 return err;
5665 }
5666
5667 if (qp->rq.wqe_cnt) {
5668 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
5669 if (err)
5670 return err;
5671 }
5672
5673 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
5674 raw_packet_qp_state);
5675}
5676
5677static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
5678 struct ib_qp_attr *qp_attr)
5679{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005680 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03005681 struct mlx5_qp_context *context;
5682 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005683 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03005684 int err = 0;
5685
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005686 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005687 if (!outb)
5688 return -ENOMEM;
5689
majd@mellanox.com19098df2016-01-14 19:13:03 +02005690 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005691 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03005692 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005693 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005694
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005695 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
5696 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
5697
Eli Cohene126ba92013-07-07 17:25:49 +03005698 mlx5_state = be32_to_cpu(context->flags) >> 28;
5699
5700 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03005701 qp_attr->path_mtu = context->mtu_msgmax >> 5;
5702 qp_attr->path_mig_state =
5703 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
5704 qp_attr->qkey = be32_to_cpu(context->qkey);
5705 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
5706 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
5707 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
5708 qp_attr->qp_access_flags =
5709 to_ib_qp_access_flags(be32_to_cpu(context->params2));
5710
5711 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005712 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
5713 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005714 qp_attr->alt_pkey_index =
5715 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005716 qp_attr->alt_port_num =
5717 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03005718 }
5719
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005720 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03005721 qp_attr->port_num = context->pri_path.port;
5722
5723 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
5724 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
5725
5726 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
5727
5728 qp_attr->max_dest_rd_atomic =
5729 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
5730 qp_attr->min_rnr_timer =
5731 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
5732 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
5733 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
5734 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
5735 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005736
5737out:
5738 kfree(outb);
5739 return err;
5740}
5741
Moni Shoua776a3902018-01-02 16:19:33 +02005742static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
5743 struct ib_qp_attr *qp_attr, int qp_attr_mask,
5744 struct ib_qp_init_attr *qp_init_attr)
5745{
5746 struct mlx5_core_dct *dct = &mqp->dct.mdct;
5747 u32 *out;
5748 u32 access_flags = 0;
5749 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
5750 void *dctc;
5751 int err;
5752 int supported_mask = IB_QP_STATE |
5753 IB_QP_ACCESS_FLAGS |
5754 IB_QP_PORT |
5755 IB_QP_MIN_RNR_TIMER |
5756 IB_QP_AV |
5757 IB_QP_PATH_MTU |
5758 IB_QP_PKEY_INDEX;
5759
5760 if (qp_attr_mask & ~supported_mask)
5761 return -EINVAL;
5762 if (mqp->state != IB_QPS_RTR)
5763 return -EINVAL;
5764
5765 out = kzalloc(outlen, GFP_KERNEL);
5766 if (!out)
5767 return -ENOMEM;
5768
5769 err = mlx5_core_dct_query(dev->mdev, dct, out, outlen);
5770 if (err)
5771 goto out;
5772
5773 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
5774
5775 if (qp_attr_mask & IB_QP_STATE)
5776 qp_attr->qp_state = IB_QPS_RTR;
5777
5778 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
5779 if (MLX5_GET(dctc, dctc, rre))
5780 access_flags |= IB_ACCESS_REMOTE_READ;
5781 if (MLX5_GET(dctc, dctc, rwe))
5782 access_flags |= IB_ACCESS_REMOTE_WRITE;
5783 if (MLX5_GET(dctc, dctc, rae))
5784 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
5785 qp_attr->qp_access_flags = access_flags;
5786 }
5787
5788 if (qp_attr_mask & IB_QP_PORT)
5789 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
5790 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
5791 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
5792 if (qp_attr_mask & IB_QP_AV) {
5793 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
5794 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
5795 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
5796 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
5797 }
5798 if (qp_attr_mask & IB_QP_PATH_MTU)
5799 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
5800 if (qp_attr_mask & IB_QP_PKEY_INDEX)
5801 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
5802out:
5803 kfree(out);
5804 return err;
5805}
5806
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005807int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5808 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
5809{
5810 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5811 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5812 int err = 0;
5813 u8 raw_packet_qp_state;
5814
Yishai Hadas28d61372016-05-23 15:20:56 +03005815 if (ibqp->rwq_ind_tbl)
5816 return -ENOSYS;
5817
Haggai Erand16e91d2016-02-29 15:45:05 +02005818 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5819 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
5820 qp_init_attr);
5821
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005822 /* Not all of output fields are applicable, make sure to zero them */
5823 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5824 memset(qp_attr, 0, sizeof(*qp_attr));
5825
Moni Shoua776a3902018-01-02 16:19:33 +02005826 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
5827 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
5828 qp_attr_mask, qp_init_attr);
5829
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005830 mutex_lock(&qp->mutex);
5831
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005832 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
5833 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005834 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
5835 if (err)
5836 goto out;
5837 qp->state = raw_packet_qp_state;
5838 qp_attr->port_num = 1;
5839 } else {
5840 err = query_qp_attr(dev, qp, qp_attr);
5841 if (err)
5842 goto out;
5843 }
5844
5845 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03005846 qp_attr->cur_qp_state = qp_attr->qp_state;
5847 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
5848 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
5849
5850 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005851 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005852 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005853 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005854 } else {
5855 qp_attr->cap.max_send_wr = 0;
5856 qp_attr->cap.max_send_sge = 0;
5857 }
5858
Noa Osherovich0540d812016-06-04 15:15:32 +03005859 qp_init_attr->qp_type = ibqp->qp_type;
5860 qp_init_attr->recv_cq = ibqp->recv_cq;
5861 qp_init_attr->send_cq = ibqp->send_cq;
5862 qp_init_attr->srq = ibqp->srq;
5863 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005864
5865 qp_init_attr->cap = qp_attr->cap;
5866
5867 qp_init_attr->create_flags = 0;
5868 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
5869 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
5870
Leon Romanovsky051f2632015-12-20 12:16:11 +02005871 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
5872 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
5873 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
5874 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
5875 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
5876 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02005877 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
5878 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02005879
Eli Cohene126ba92013-07-07 17:25:49 +03005880 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5881 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5882
Eli Cohene126ba92013-07-07 17:25:49 +03005883out:
5884 mutex_unlock(&qp->mutex);
5885 return err;
5886}
5887
5888struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03005889 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005890{
5891 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5892 struct mlx5_ib_xrcd *xrcd;
5893 int err;
5894
Saeed Mahameed938fe832015-05-28 22:28:41 +03005895 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005896 return ERR_PTR(-ENOSYS);
5897
5898 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5899 if (!xrcd)
5900 return ERR_PTR(-ENOMEM);
5901
Yishai Hadas5aa37712018-11-26 08:28:38 +02005902 err = mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03005903 if (err) {
5904 kfree(xrcd);
5905 return ERR_PTR(-ENOMEM);
5906 }
5907
5908 return &xrcd->ibxrcd;
5909}
5910
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005911int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005912{
5913 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5914 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
5915 int err;
5916
Yishai Hadas5aa37712018-11-26 08:28:38 +02005917 err = mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, 0);
Leon Romanovskyb0818082018-01-28 11:25:30 +02005918 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03005919 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005920
5921 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03005922 return 0;
5923}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005924
Yishai Hadas350d0e42016-08-28 14:58:18 +03005925static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5926{
5927 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5928 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5929 struct ib_event event;
5930
5931 if (rwq->ibwq.event_handler) {
5932 event.device = rwq->ibwq.device;
5933 event.element.wq = &rwq->ibwq;
5934 switch (type) {
5935 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5936 event.event = IB_EVENT_WQ_FATAL;
5937 break;
5938 default:
5939 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5940 return;
5941 }
5942
5943 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5944 }
5945}
5946
Maor Gottlieb03404e82017-05-30 10:29:13 +03005947static int set_delay_drop(struct mlx5_ib_dev *dev)
5948{
5949 int err = 0;
5950
5951 mutex_lock(&dev->delay_drop.lock);
5952 if (dev->delay_drop.activate)
5953 goto out;
5954
5955 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
5956 if (err)
5957 goto out;
5958
5959 dev->delay_drop.activate = true;
5960out:
5961 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005962
5963 if (!err)
5964 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005965 return err;
5966}
5967
Yishai Hadas79b20a62016-05-23 15:20:50 +03005968static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5969 struct ib_wq_init_attr *init_attr)
5970{
5971 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005972 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005973 __be64 *rq_pas0;
5974 void *in;
5975 void *rqc;
5976 void *wq;
5977 int inlen;
5978 int err;
5979
5980 dev = to_mdev(pd->device);
5981
5982 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005983 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005984 if (!in)
5985 return -ENOMEM;
5986
Yishai Hadas34d57582018-09-20 21:39:21 +03005987 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005988 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
5989 MLX5_SET(rqc, rqc, mem_rq_type,
5990 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
5991 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
5992 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
5993 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
5994 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
5995 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03005996 MLX5_SET(wq, wq, wq_type,
5997 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
5998 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005999 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6000 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
6001 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
6002 err = -EOPNOTSUPP;
6003 goto out;
6004 } else {
6005 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
6006 }
6007 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006008 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03006009 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
6010 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
6011 MLX5_SET(wq, wq, log_wqe_stride_size,
6012 rwq->single_stride_log_num_of_bytes -
6013 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
6014 MLX5_SET(wq, wq, log_wqe_num_of_strides, rwq->log_num_strides -
6015 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES);
6016 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006017 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
6018 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
6019 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
6020 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
6021 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
6022 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02006023 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006024 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02006025 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006026 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
6027 err = -EOPNOTSUPP;
6028 goto out;
6029 }
6030 } else {
6031 MLX5_SET(rqc, rqc, vsd, 1);
6032 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02006033 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
6034 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
6035 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
6036 err = -EOPNOTSUPP;
6037 goto out;
6038 }
6039 MLX5_SET(rqc, rqc, scatter_fcs, 1);
6040 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03006041 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
6042 if (!(dev->ib_dev.attrs.raw_packet_caps &
6043 IB_RAW_PACKET_CAP_DELAY_DROP)) {
6044 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
6045 err = -EOPNOTSUPP;
6046 goto out;
6047 }
6048 MLX5_SET(rqc, rqc, delay_drop_en, 1);
6049 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006050 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
6051 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03006052 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03006053 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
6054 err = set_delay_drop(dev);
6055 if (err) {
6056 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
6057 err);
6058 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
6059 } else {
6060 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
6061 }
6062 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006063out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03006064 kvfree(in);
6065 return err;
6066}
6067
6068static int set_user_rq_size(struct mlx5_ib_dev *dev,
6069 struct ib_wq_init_attr *wq_init_attr,
6070 struct mlx5_ib_create_wq *ucmd,
6071 struct mlx5_ib_rwq *rwq)
6072{
6073 /* Sanity check RQ size before proceeding */
6074 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
6075 return -EINVAL;
6076
6077 if (!ucmd->rq_wqe_count)
6078 return -EINVAL;
6079
6080 rwq->wqe_count = ucmd->rq_wqe_count;
6081 rwq->wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky0dfe4522018-08-01 14:25:41 -07006082 if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
6083 return -EINVAL;
6084
Yishai Hadas79b20a62016-05-23 15:20:50 +03006085 rwq->log_rq_stride = rwq->wqe_shift;
6086 rwq->log_rq_size = ilog2(rwq->wqe_count);
6087 return 0;
6088}
6089
6090static int prepare_user_rq(struct ib_pd *pd,
6091 struct ib_wq_init_attr *init_attr,
6092 struct ib_udata *udata,
6093 struct mlx5_ib_rwq *rwq)
6094{
6095 struct mlx5_ib_dev *dev = to_mdev(pd->device);
6096 struct mlx5_ib_create_wq ucmd = {};
6097 int err;
6098 size_t required_cmd_sz;
6099
Noa Osherovichccc87082017-10-17 18:01:13 +03006100 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
6101 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006102 if (udata->inlen < required_cmd_sz) {
6103 mlx5_ib_dbg(dev, "invalid inlen\n");
6104 return -EINVAL;
6105 }
6106
6107 if (udata->inlen > sizeof(ucmd) &&
6108 !ib_is_udata_cleared(udata, sizeof(ucmd),
6109 udata->inlen - sizeof(ucmd))) {
6110 mlx5_ib_dbg(dev, "inlen is not supported\n");
6111 return -EOPNOTSUPP;
6112 }
6113
6114 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
6115 mlx5_ib_dbg(dev, "copy failed\n");
6116 return -EFAULT;
6117 }
6118
Noa Osherovichccc87082017-10-17 18:01:13 +03006119 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03006120 mlx5_ib_dbg(dev, "invalid comp mask\n");
6121 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03006122 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
6123 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
6124 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
6125 return -EOPNOTSUPP;
6126 }
6127 if ((ucmd.single_stride_log_num_of_bytes <
6128 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
6129 (ucmd.single_stride_log_num_of_bytes >
6130 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
6131 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
6132 ucmd.single_stride_log_num_of_bytes,
6133 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
6134 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
6135 return -EINVAL;
6136 }
6137 if ((ucmd.single_wqe_log_num_of_strides >
6138 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
6139 (ucmd.single_wqe_log_num_of_strides <
6140 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES)) {
6141 mlx5_ib_dbg(dev, "Invalid log num strides (%u. Range is %u - %u)\n",
6142 ucmd.single_wqe_log_num_of_strides,
6143 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
6144 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
6145 return -EINVAL;
6146 }
6147 rwq->single_stride_log_num_of_bytes =
6148 ucmd.single_stride_log_num_of_bytes;
6149 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
6150 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
6151 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006152 }
6153
6154 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
6155 if (err) {
6156 mlx5_ib_dbg(dev, "err %d\n", err);
6157 return err;
6158 }
6159
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02006160 err = create_user_rq(dev, pd, udata, rwq, &ucmd);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006161 if (err) {
6162 mlx5_ib_dbg(dev, "err %d\n", err);
Gal Pressman645ba592018-10-08 19:44:03 +03006163 return err;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006164 }
6165
6166 rwq->user_index = ucmd.user_index;
6167 return 0;
6168}
6169
6170struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
6171 struct ib_wq_init_attr *init_attr,
6172 struct ib_udata *udata)
6173{
6174 struct mlx5_ib_dev *dev;
6175 struct mlx5_ib_rwq *rwq;
6176 struct mlx5_ib_create_wq_resp resp = {};
6177 size_t min_resp_len;
6178 int err;
6179
6180 if (!udata)
6181 return ERR_PTR(-ENOSYS);
6182
6183 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6184 if (udata->outlen && udata->outlen < min_resp_len)
6185 return ERR_PTR(-EINVAL);
6186
6187 dev = to_mdev(pd->device);
6188 switch (init_attr->wq_type) {
6189 case IB_WQT_RQ:
6190 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
6191 if (!rwq)
6192 return ERR_PTR(-ENOMEM);
6193 err = prepare_user_rq(pd, init_attr, udata, rwq);
6194 if (err)
6195 goto err;
6196 err = create_rq(rwq, pd, init_attr);
6197 if (err)
6198 goto err_user_rq;
6199 break;
6200 default:
6201 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
6202 init_attr->wq_type);
6203 return ERR_PTR(-EINVAL);
6204 }
6205
Yishai Hadas350d0e42016-08-28 14:58:18 +03006206 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006207 rwq->ibwq.state = IB_WQS_RESET;
6208 if (udata->outlen) {
6209 resp.response_length = offsetof(typeof(resp), response_length) +
6210 sizeof(resp.response_length);
6211 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6212 if (err)
6213 goto err_copy;
6214 }
6215
Yishai Hadas350d0e42016-08-28 14:58:18 +03006216 rwq->core_qp.event = mlx5_ib_wq_event;
6217 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006218 return &rwq->ibwq;
6219
6220err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03006221 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006222err_user_rq:
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006223 destroy_user_rq(dev, pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006224err:
6225 kfree(rwq);
6226 return ERR_PTR(err);
6227}
6228
Leon Romanovskya49b1dc2019-06-12 15:27:41 +03006229void mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +03006230{
6231 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6232 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6233
Yishai Hadas350d0e42016-08-28 14:58:18 +03006234 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006235 destroy_user_rq(dev, wq->pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006236 kfree(rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006237}
6238
Yishai Hadasc5f90922016-05-23 15:20:53 +03006239struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
6240 struct ib_rwq_ind_table_init_attr *init_attr,
6241 struct ib_udata *udata)
6242{
6243 struct mlx5_ib_dev *dev = to_mdev(device);
6244 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
6245 int sz = 1 << init_attr->log_ind_tbl_size;
6246 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
6247 size_t min_resp_len;
6248 int inlen;
6249 int err;
6250 int i;
6251 u32 *in;
6252 void *rqtc;
6253
6254 if (udata->inlen > 0 &&
6255 !ib_is_udata_cleared(udata, 0,
6256 udata->inlen))
6257 return ERR_PTR(-EOPNOTSUPP);
6258
Maor Gottliebefd7f402016-10-27 16:36:40 +03006259 if (init_attr->log_ind_tbl_size >
6260 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
6261 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
6262 init_attr->log_ind_tbl_size,
6263 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
6264 return ERR_PTR(-EINVAL);
6265 }
6266
Yishai Hadasc5f90922016-05-23 15:20:53 +03006267 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6268 if (udata->outlen && udata->outlen < min_resp_len)
6269 return ERR_PTR(-EINVAL);
6270
6271 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
6272 if (!rwq_ind_tbl)
6273 return ERR_PTR(-ENOMEM);
6274
6275 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006276 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006277 if (!in) {
6278 err = -ENOMEM;
6279 goto err;
6280 }
6281
6282 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
6283
6284 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
6285 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
6286
6287 for (i = 0; i < sz; i++)
6288 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
6289
Yishai Hadas5deba862018-09-20 21:39:28 +03006290 rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
6291 MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);
6292
Yishai Hadasc5f90922016-05-23 15:20:53 +03006293 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
6294 kvfree(in);
6295
6296 if (err)
6297 goto err;
6298
6299 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
6300 if (udata->outlen) {
6301 resp.response_length = offsetof(typeof(resp), response_length) +
6302 sizeof(resp.response_length);
6303 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6304 if (err)
6305 goto err_copy;
6306 }
6307
6308 return &rwq_ind_tbl->ib_rwq_ind_tbl;
6309
6310err_copy:
Yishai Hadas5deba862018-09-20 21:39:28 +03006311 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006312err:
6313 kfree(rwq_ind_tbl);
6314 return ERR_PTR(err);
6315}
6316
6317int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
6318{
6319 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
6320 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
6321
Yishai Hadas5deba862018-09-20 21:39:28 +03006322 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006323
6324 kfree(rwq_ind_tbl);
6325 return 0;
6326}
6327
Yishai Hadas79b20a62016-05-23 15:20:50 +03006328int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
6329 u32 wq_attr_mask, struct ib_udata *udata)
6330{
6331 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6332 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6333 struct mlx5_ib_modify_wq ucmd = {};
6334 size_t required_cmd_sz;
6335 int curr_wq_state;
6336 int wq_state;
6337 int inlen;
6338 int err;
6339 void *rqc;
6340 void *in;
6341
6342 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
6343 if (udata->inlen < required_cmd_sz)
6344 return -EINVAL;
6345
6346 if (udata->inlen > sizeof(ucmd) &&
6347 !ib_is_udata_cleared(udata, sizeof(ucmd),
6348 udata->inlen - sizeof(ucmd)))
6349 return -EOPNOTSUPP;
6350
6351 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
6352 return -EFAULT;
6353
6354 if (ucmd.comp_mask || ucmd.reserved)
6355 return -EOPNOTSUPP;
6356
6357 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006358 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006359 if (!in)
6360 return -ENOMEM;
6361
6362 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
6363
6364 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
6365 wq_attr->curr_wq_state : wq->state;
6366 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
6367 wq_attr->wq_state : curr_wq_state;
6368 if (curr_wq_state == IB_WQS_ERR)
6369 curr_wq_state = MLX5_RQC_STATE_ERR;
6370 if (wq_state == IB_WQS_ERR)
6371 wq_state = MLX5_RQC_STATE_ERR;
6372 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
Yishai Hadas34d57582018-09-20 21:39:21 +03006373 MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006374 MLX5_SET(rqc, rqc, state, wq_state);
6375
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006376 if (wq_attr_mask & IB_WQ_FLAGS) {
6377 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
6378 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
6379 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
6380 mlx5_ib_dbg(dev, "VLAN offloads are not "
6381 "supported\n");
6382 err = -EOPNOTSUPP;
6383 goto out;
6384 }
6385 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6386 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
6387 MLX5_SET(rqc, rqc, vsd,
6388 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
6389 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02006390
6391 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6392 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
6393 err = -EOPNOTSUPP;
6394 goto out;
6395 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006396 }
6397
Majd Dibbiny23a69642017-01-18 15:25:10 +02006398 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
6399 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
6400 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6401 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03006402 MLX5_SET(rqc, rqc, counter_set_id,
6403 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006404 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06006405 dev_info_once(
6406 &dev->ib_dev.dev,
6407 "Receive WQ counters are not supported on current FW\n");
Majd Dibbiny23a69642017-01-18 15:25:10 +02006408 }
6409
Yishai Hadas350d0e42016-08-28 14:58:18 +03006410 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006411 if (!err)
6412 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
6413
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006414out:
6415 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006416 return err;
6417}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006418
6419struct mlx5_ib_drain_cqe {
6420 struct ib_cqe cqe;
6421 struct completion done;
6422};
6423
6424static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
6425{
6426 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
6427 struct mlx5_ib_drain_cqe,
6428 cqe);
6429
6430 complete(&cqe->done);
6431}
6432
6433/* This function returns only once the drained WR was completed */
6434static void handle_drain_completion(struct ib_cq *cq,
6435 struct mlx5_ib_drain_cqe *sdrain,
6436 struct mlx5_ib_dev *dev)
6437{
6438 struct mlx5_core_dev *mdev = dev->mdev;
6439
6440 if (cq->poll_ctx == IB_POLL_DIRECT) {
6441 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
6442 ib_process_cq_direct(cq, -1);
6443 return;
6444 }
6445
6446 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6447 struct mlx5_ib_cq *mcq = to_mcq(cq);
6448 bool triggered = false;
6449 unsigned long flags;
6450
6451 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
6452 /* Make sure that the CQ handler won't run if wasn't run yet */
6453 if (!mcq->mcq.reset_notify_added)
6454 mcq->mcq.reset_notify_added = 1;
6455 else
6456 triggered = true;
6457 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
6458
6459 if (triggered) {
6460 /* Wait for any scheduled/running task to be ended */
6461 switch (cq->poll_ctx) {
6462 case IB_POLL_SOFTIRQ:
6463 irq_poll_disable(&cq->iop);
6464 irq_poll_enable(&cq->iop);
6465 break;
6466 case IB_POLL_WORKQUEUE:
6467 cancel_work_sync(&cq->work);
6468 break;
6469 default:
6470 WARN_ON_ONCE(1);
6471 }
6472 }
6473
6474 /* Run the CQ handler - this makes sure that the drain WR will
6475 * be processed if wasn't processed yet.
6476 */
6477 mcq->mcq.comp(&mcq->mcq);
6478 }
6479
6480 wait_for_completion(&sdrain->done);
6481}
6482
6483void mlx5_ib_drain_sq(struct ib_qp *qp)
6484{
6485 struct ib_cq *cq = qp->send_cq;
6486 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6487 struct mlx5_ib_drain_cqe sdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006488 const struct ib_send_wr *bad_swr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006489 struct ib_rdma_wr swr = {
6490 .wr = {
6491 .next = NULL,
6492 { .wr_cqe = &sdrain.cqe, },
6493 .opcode = IB_WR_RDMA_WRITE,
6494 },
6495 };
6496 int ret;
6497 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6498 struct mlx5_core_dev *mdev = dev->mdev;
6499
6500 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6501 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6502 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6503 return;
6504 }
6505
6506 sdrain.cqe.done = mlx5_ib_drain_qp_done;
6507 init_completion(&sdrain.done);
6508
6509 ret = _mlx5_ib_post_send(qp, &swr.wr, &bad_swr, true);
6510 if (ret) {
6511 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6512 return;
6513 }
6514
6515 handle_drain_completion(cq, &sdrain, dev);
6516}
6517
6518void mlx5_ib_drain_rq(struct ib_qp *qp)
6519{
6520 struct ib_cq *cq = qp->recv_cq;
6521 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6522 struct mlx5_ib_drain_cqe rdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006523 struct ib_recv_wr rwr = {};
6524 const struct ib_recv_wr *bad_rwr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006525 int ret;
6526 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6527 struct mlx5_core_dev *mdev = dev->mdev;
6528
6529 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6530 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6531 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6532 return;
6533 }
6534
6535 rwr.wr_cqe = &rdrain.cqe;
6536 rdrain.cqe.done = mlx5_ib_drain_qp_done;
6537 init_completion(&rdrain.done);
6538
6539 ret = _mlx5_ib_post_recv(qp, &rwr, &bad_rwr, true);
6540 if (ret) {
6541 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6542 return;
6543 }
6544
6545 handle_drain_completion(cq, &rdrain, dev);
6546}