blob: d77a64c551ead0e90ede0a95f644d46459fc8daa [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/module.h>
34#include <rdma/ib_umem.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020035#include <rdma/ib_cache.h>
Haggai Abramovskycfb5e082016-01-14 19:12:57 +020036#include <rdma/ib_user_verbs.h>
Yishai Hadasc2e53b22017-06-08 16:15:08 +030037#include <linux/mlx5/fs.h>
Eli Cohene126ba92013-07-07 17:25:49 +030038#include "mlx5_ib.h"
Mark Blochb96c9dd2018-01-29 10:40:37 +000039#include "ib_rep.h"
Yishai Hadas443c1cf2018-09-20 21:39:26 +030040#include "cmd.h"
Eli Cohene126ba92013-07-07 17:25:49 +030041
42/* not supported currently */
43static int wq_signature;
44
45enum {
46 MLX5_IB_ACK_REQ_FREQ = 8,
47};
48
49enum {
50 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
51 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
52 MLX5_IB_LINK_TYPE_IB = 0,
53 MLX5_IB_LINK_TYPE_ETH = 1
54};
55
56enum {
57 MLX5_IB_SQ_STRIDE = 6,
Idan Burstein064e5262018-05-02 13:16:39 +030058 MLX5_IB_SQ_UMR_INLINE_THRESHOLD = 64,
Eli Cohene126ba92013-07-07 17:25:49 +030059};
60
61static const u32 mlx5_ib_opcode[] = {
62 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020063 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030064 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
65 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
66 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
67 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
68 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
69 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
70 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
71 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030072 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030073 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
74 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
75 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
76};
77
Erez Shitritf0313962016-02-21 16:27:17 +020078struct mlx5_wqe_eth_pad {
79 u8 rsvd0[16];
80};
Eli Cohene126ba92013-07-07 17:25:49 +030081
Alex Veskereb49ab02016-08-28 12:25:53 +030082enum raw_qp_set_mask_map {
83 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020084 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030085};
86
Alex Vesker0680efa2016-08-28 12:25:52 +030087struct mlx5_modify_raw_qp_param {
88 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030089
90 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang61147f32018-03-19 15:10:30 +020091
92 struct mlx5_rate_limit rl;
93
Alex Veskereb49ab02016-08-28 12:25:53 +030094 u8 rq_q_ctr_id;
Mark Blochd5ed8ac2019-03-28 15:27:38 +020095 u16 port;
Alex Vesker0680efa2016-08-28 12:25:52 +030096};
97
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030098static void get_cqs(enum ib_qp_type qp_type,
99 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
100 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
101
Eli Cohene126ba92013-07-07 17:25:49 +0300102static int is_qp0(enum ib_qp_type qp_type)
103{
104 return qp_type == IB_QPT_SMI;
105}
106
Eli Cohene126ba92013-07-07 17:25:49 +0300107static int is_sqp(enum ib_qp_type qp_type)
108{
109 return is_qp0(qp_type) || is_qp1(qp_type);
110}
111
Haggai Eranc1395a22014-12-11 17:04:14 +0200112/**
Moni Shouafbeb4072019-01-22 08:48:46 +0200113 * mlx5_ib_read_user_wqe_common() - Copy a WQE (or part of) from user WQ
114 * to kernel buffer
Haggai Eranc1395a22014-12-11 17:04:14 +0200115 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200116 * @umem: User space memory where the WQ is
117 * @buffer: buffer to copy to
118 * @buflen: buffer length
119 * @wqe_index: index of WQE to copy from
120 * @wq_offset: offset to start of WQ
121 * @wq_wqe_cnt: number of WQEs in WQ
122 * @wq_wqe_shift: log2 of WQE size
123 * @bcnt: number of bytes to copy
124 * @bytes_copied: number of bytes to copy (return value)
Haggai Eranc1395a22014-12-11 17:04:14 +0200125 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200126 * Copies from start of WQE bcnt or less bytes.
127 * Does not gurantee to copy the entire WQE.
Haggai Eranc1395a22014-12-11 17:04:14 +0200128 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200129 * Return: zero on success, or an error code.
Haggai Eranc1395a22014-12-11 17:04:14 +0200130 */
Moni Shouafbeb4072019-01-22 08:48:46 +0200131static int mlx5_ib_read_user_wqe_common(struct ib_umem *umem,
132 void *buffer,
133 u32 buflen,
134 int wqe_index,
135 int wq_offset,
136 int wq_wqe_cnt,
137 int wq_wqe_shift,
138 int bcnt,
139 size_t *bytes_copied)
Haggai Eranc1395a22014-12-11 17:04:14 +0200140{
Moni Shouafbeb4072019-01-22 08:48:46 +0200141 size_t offset = wq_offset + ((wqe_index % wq_wqe_cnt) << wq_wqe_shift);
142 size_t wq_end = wq_offset + (wq_wqe_cnt << wq_wqe_shift);
143 size_t copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200144 int ret;
145
Moni Shouafbeb4072019-01-22 08:48:46 +0200146 /* don't copy more than requested, more than buffer length or
147 * beyond WQ end
148 */
149 copy_length = min_t(u32, buflen, wq_end - offset);
150 copy_length = min_t(u32, copy_length, bcnt);
Haggai Eranc1395a22014-12-11 17:04:14 +0200151
Moni Shouafbeb4072019-01-22 08:48:46 +0200152 ret = ib_umem_copy_from(buffer, umem, offset, copy_length);
Haggai Eranc1395a22014-12-11 17:04:14 +0200153 if (ret)
154 return ret;
155
Moni Shouafbeb4072019-01-22 08:48:46 +0200156 if (!ret && bytes_copied)
157 *bytes_copied = copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200158
Moni Shouafbeb4072019-01-22 08:48:46 +0200159 return 0;
160}
Haggai Eranc1395a22014-12-11 17:04:14 +0200161
Moni Shouafbeb4072019-01-22 08:48:46 +0200162int mlx5_ib_read_user_wqe_sq(struct mlx5_ib_qp *qp,
163 int wqe_index,
164 void *buffer,
165 int buflen,
166 size_t *bc)
167{
168 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
169 struct ib_umem *umem = base->ubuffer.umem;
170 struct mlx5_ib_wq *wq = &qp->sq;
171 struct mlx5_wqe_ctrl_seg *ctrl;
172 size_t bytes_copied;
173 size_t bytes_copied2;
174 size_t wqe_length;
175 int ret;
176 int ds;
Haggai Eranc1395a22014-12-11 17:04:14 +0200177
Moni Shouafbeb4072019-01-22 08:48:46 +0200178 if (buflen < sizeof(*ctrl))
179 return -EINVAL;
180
181 /* at first read as much as possible */
182 ret = mlx5_ib_read_user_wqe_common(umem,
183 buffer,
184 buflen,
185 wqe_index,
186 wq->offset,
187 wq->wqe_cnt,
188 wq->wqe_shift,
189 buflen,
190 &bytes_copied);
Haggai Eranc1395a22014-12-11 17:04:14 +0200191 if (ret)
192 return ret;
193
Moni Shouafbeb4072019-01-22 08:48:46 +0200194 /* we need at least control segment size to proceed */
195 if (bytes_copied < sizeof(*ctrl))
196 return -EINVAL;
197
198 ctrl = buffer;
199 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
200 wqe_length = ds * MLX5_WQE_DS_UNITS;
201
202 /* if we copied enough then we are done */
203 if (bytes_copied >= wqe_length) {
204 *bc = bytes_copied;
205 return 0;
206 }
207
208 /* otherwise this a wrapped around wqe
209 * so read the remaining bytes starting
210 * from wqe_index 0
211 */
212 ret = mlx5_ib_read_user_wqe_common(umem,
213 buffer + bytes_copied,
214 buflen - bytes_copied,
215 0,
216 wq->offset,
217 wq->wqe_cnt,
218 wq->wqe_shift,
219 wqe_length - bytes_copied,
220 &bytes_copied2);
221
222 if (ret)
223 return ret;
224 *bc = bytes_copied + bytes_copied2;
225 return 0;
226}
227
228int mlx5_ib_read_user_wqe_rq(struct mlx5_ib_qp *qp,
229 int wqe_index,
230 void *buffer,
231 int buflen,
232 size_t *bc)
233{
234 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
235 struct ib_umem *umem = base->ubuffer.umem;
236 struct mlx5_ib_wq *wq = &qp->rq;
237 size_t bytes_copied;
238 int ret;
239
240 ret = mlx5_ib_read_user_wqe_common(umem,
241 buffer,
242 buflen,
243 wqe_index,
244 wq->offset,
245 wq->wqe_cnt,
246 wq->wqe_shift,
247 buflen,
248 &bytes_copied);
249
250 if (ret)
251 return ret;
252 *bc = bytes_copied;
253 return 0;
254}
255
256int mlx5_ib_read_user_wqe_srq(struct mlx5_ib_srq *srq,
257 int wqe_index,
258 void *buffer,
259 int buflen,
260 size_t *bc)
261{
262 struct ib_umem *umem = srq->umem;
263 size_t bytes_copied;
264 int ret;
265
266 ret = mlx5_ib_read_user_wqe_common(umem,
267 buffer,
268 buflen,
269 wqe_index,
270 0,
271 srq->msrq.max,
272 srq->msrq.wqe_shift,
273 buflen,
274 &bytes_copied);
275
276 if (ret)
277 return ret;
278 *bc = bytes_copied;
279 return 0;
Haggai Eranc1395a22014-12-11 17:04:14 +0200280}
281
Eli Cohene126ba92013-07-07 17:25:49 +0300282static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
283{
284 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
285 struct ib_event event;
286
majd@mellanox.com19098df2016-01-14 19:13:03 +0200287 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
288 /* This event is only valid for trans_qps */
289 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
290 }
Eli Cohene126ba92013-07-07 17:25:49 +0300291
292 if (ibqp->event_handler) {
293 event.device = ibqp->device;
294 event.element.qp = ibqp;
295 switch (type) {
296 case MLX5_EVENT_TYPE_PATH_MIG:
297 event.event = IB_EVENT_PATH_MIG;
298 break;
299 case MLX5_EVENT_TYPE_COMM_EST:
300 event.event = IB_EVENT_COMM_EST;
301 break;
302 case MLX5_EVENT_TYPE_SQ_DRAINED:
303 event.event = IB_EVENT_SQ_DRAINED;
304 break;
305 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
306 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
307 break;
308 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
309 event.event = IB_EVENT_QP_FATAL;
310 break;
311 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
312 event.event = IB_EVENT_PATH_MIG_ERR;
313 break;
314 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
315 event.event = IB_EVENT_QP_REQ_ERR;
316 break;
317 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
318 event.event = IB_EVENT_QP_ACCESS_ERR;
319 break;
320 default:
321 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
322 return;
323 }
324
325 ibqp->event_handler(&event, ibqp->qp_context);
326 }
327}
328
329static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
330 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
331{
332 int wqe_size;
333 int wq_size;
334
335 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300336 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300337 return -EINVAL;
338
339 if (!has_rq) {
340 qp->rq.max_gs = 0;
341 qp->rq.wqe_cnt = 0;
342 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300343 cap->max_recv_wr = 0;
344 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300345 } else {
346 if (ucmd) {
347 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300348 if (ucmd->rq_wqe_shift > BITS_PER_BYTE * sizeof(ucmd->rq_wqe_shift))
349 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300350 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300351 if ((1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) < qp->wq_sig)
352 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300353 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
354 qp->rq.max_post = qp->rq.wqe_cnt;
355 } else {
356 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
357 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
358 wqe_size = roundup_pow_of_two(wqe_size);
359 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
360 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
361 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300362 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300363 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
364 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300365 MLX5_CAP_GEN(dev->mdev,
366 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300367 return -EINVAL;
368 }
369 qp->rq.wqe_shift = ilog2(wqe_size);
370 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
371 qp->rq.max_post = qp->rq.wqe_cnt;
372 }
373 }
374
375 return 0;
376}
377
Erez Shitritf0313962016-02-21 16:27:17 +0200378static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300379{
Andi Shyti618af382013-07-16 15:35:01 +0200380 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300381
Erez Shitritf0313962016-02-21 16:27:17 +0200382 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300383 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300384 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300385 /* fall through */
386 case IB_QPT_RC:
387 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200388 max(sizeof(struct mlx5_wqe_atomic_seg) +
389 sizeof(struct mlx5_wqe_raddr_seg),
390 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
Idan Burstein064e5262018-05-02 13:16:39 +0300391 sizeof(struct mlx5_mkey_seg) +
392 MLX5_IB_SQ_UMR_INLINE_THRESHOLD /
393 MLX5_IB_UMR_OCTOWORD);
Eli Cohene126ba92013-07-07 17:25:49 +0300394 break;
395
Eli Cohenb125a542013-09-11 16:35:22 +0300396 case IB_QPT_XRC_TGT:
397 return 0;
398
Eli Cohene126ba92013-07-07 17:25:49 +0300399 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300400 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200401 max(sizeof(struct mlx5_wqe_raddr_seg),
402 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
403 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300404 break;
405
406 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200407 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
408 size += sizeof(struct mlx5_wqe_eth_pad) +
409 sizeof(struct mlx5_wqe_eth_seg);
410 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300411 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200412 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300413 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300414 sizeof(struct mlx5_wqe_datagram_seg);
415 break;
416
417 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300418 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300419 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
420 sizeof(struct mlx5_mkey_seg);
421 break;
422
423 default:
424 return -EINVAL;
425 }
426
427 return size;
428}
429
430static int calc_send_wqe(struct ib_qp_init_attr *attr)
431{
432 int inl_size = 0;
433 int size;
434
Erez Shitritf0313962016-02-21 16:27:17 +0200435 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300436 if (size < 0)
437 return size;
438
439 if (attr->cap.max_inline_data) {
440 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
441 attr->cap.max_inline_data;
442 }
443
444 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300445 if (attr->create_flags & IB_QP_CREATE_INTEGRITY_EN &&
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200446 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300447 return MLX5_SIG_WQE_SIZE;
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200448 else
449 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300450}
451
Eli Cohen288c01b2016-10-27 16:36:45 +0300452static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
453{
454 int max_sge;
455
456 if (attr->qp_type == IB_QPT_RC)
457 max_sge = (min_t(int, wqe_size, 512) -
458 sizeof(struct mlx5_wqe_ctrl_seg) -
459 sizeof(struct mlx5_wqe_raddr_seg)) /
460 sizeof(struct mlx5_wqe_data_seg);
461 else if (attr->qp_type == IB_QPT_XRC_INI)
462 max_sge = (min_t(int, wqe_size, 512) -
463 sizeof(struct mlx5_wqe_ctrl_seg) -
464 sizeof(struct mlx5_wqe_xrc_seg) -
465 sizeof(struct mlx5_wqe_raddr_seg)) /
466 sizeof(struct mlx5_wqe_data_seg);
467 else
468 max_sge = (wqe_size - sq_overhead(attr)) /
469 sizeof(struct mlx5_wqe_data_seg);
470
471 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
472 sizeof(struct mlx5_wqe_data_seg));
473}
474
Eli Cohene126ba92013-07-07 17:25:49 +0300475static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
476 struct mlx5_ib_qp *qp)
477{
478 int wqe_size;
479 int wq_size;
480
481 if (!attr->cap.max_send_wr)
482 return 0;
483
484 wqe_size = calc_send_wqe(attr);
485 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
486 if (wqe_size < 0)
487 return wqe_size;
488
Saeed Mahameed938fe832015-05-28 22:28:41 +0300489 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300490 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300491 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300492 return -EINVAL;
493 }
494
Erez Shitritf0313962016-02-21 16:27:17 +0200495 qp->max_inline_data = wqe_size - sq_overhead(attr) -
496 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300497 attr->cap.max_inline_data = qp->max_inline_data;
498
499 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
500 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300501 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800502 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
503 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300504 qp->sq.wqe_cnt,
505 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300506 return -ENOMEM;
507 }
Eli Cohene126ba92013-07-07 17:25:49 +0300508 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300509 qp->sq.max_gs = get_send_sge(attr, wqe_size);
510 if (qp->sq.max_gs < attr->cap.max_send_sge)
511 return -ENOMEM;
512
513 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300514 qp->sq.max_post = wq_size / wqe_size;
515 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300516
517 return wq_size;
518}
519
520static int set_user_buf_size(struct mlx5_ib_dev *dev,
521 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200522 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200523 struct mlx5_ib_qp_base *base,
524 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300525{
526 int desc_sz = 1 << qp->sq.wqe_shift;
527
Saeed Mahameed938fe832015-05-28 22:28:41 +0300528 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300529 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300530 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300531 return -EINVAL;
532 }
533
Gal Pressmanaf8b38e2019-02-06 15:45:35 +0200534 if (ucmd->sq_wqe_count && !is_power_of_2(ucmd->sq_wqe_count)) {
535 mlx5_ib_warn(dev, "sq_wqe_count %d is not a power of two\n",
536 ucmd->sq_wqe_count);
Eli Cohene126ba92013-07-07 17:25:49 +0300537 return -EINVAL;
538 }
539
540 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
541
Saeed Mahameed938fe832015-05-28 22:28:41 +0300542 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300543 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300544 qp->sq.wqe_cnt,
545 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300546 return -EINVAL;
547 }
548
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300549 if (attr->qp_type == IB_QPT_RAW_PACKET ||
550 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200551 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
552 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
553 } else {
554 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
555 (qp->sq.wqe_cnt << 6);
556 }
Eli Cohene126ba92013-07-07 17:25:49 +0300557
558 return 0;
559}
560
561static int qp_has_rq(struct ib_qp_init_attr *attr)
562{
563 if (attr->qp_type == IB_QPT_XRC_INI ||
564 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
565 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
566 !attr->cap.max_recv_wr)
567 return 0;
568
569 return 1;
570}
571
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200572enum {
573 /* this is the first blue flame register in the array of bfregs assigned
574 * to a processes. Since we do not use it for blue flame but rather
575 * regular 64 bit doorbells, we do not need a lock for maintaiing
576 * "odd/even" order
577 */
578 NUM_NON_BLUE_FLAME_BFREGS = 1,
579};
580
Eli Cohenb037c292017-01-03 23:55:26 +0200581static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
582{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200583 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200584}
585
586static int num_med_bfreg(struct mlx5_ib_dev *dev,
587 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200588{
589 int n;
590
Eli Cohenb037c292017-01-03 23:55:26 +0200591 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
592 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200593
594 return n >= 0 ? n : 0;
595}
596
Yishai Hadas18b03622018-05-07 10:20:01 +0300597static int first_med_bfreg(struct mlx5_ib_dev *dev,
598 struct mlx5_bfreg_info *bfregi)
599{
600 return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
601}
602
Eli Cohenb037c292017-01-03 23:55:26 +0200603static int first_hi_bfreg(struct mlx5_ib_dev *dev,
604 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200605{
606 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200607
Eli Cohenb037c292017-01-03 23:55:26 +0200608 med = num_med_bfreg(dev, bfregi);
609 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200610}
611
Eli Cohenb037c292017-01-03 23:55:26 +0200612static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
613 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300614{
Eli Cohene126ba92013-07-07 17:25:49 +0300615 int i;
616
Eli Cohenb037c292017-01-03 23:55:26 +0200617 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
618 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200619 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300620 return i;
621 }
622 }
623
624 return -ENOMEM;
625}
626
Eli Cohenb037c292017-01-03 23:55:26 +0200627static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
628 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300629{
Yishai Hadas18b03622018-05-07 10:20:01 +0300630 int minidx = first_med_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300631 int i;
632
Yishai Hadas18b03622018-05-07 10:20:01 +0300633 if (minidx < 0)
634 return minidx;
635
636 for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200637 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300638 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200639 if (!bfregi->count[minidx])
640 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300641 }
642
Eli Cohen2f5ff262017-01-03 23:55:21 +0200643 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300644 return minidx;
645}
646
Eli Cohenb037c292017-01-03 23:55:26 +0200647static int alloc_bfreg(struct mlx5_ib_dev *dev,
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300648 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300649{
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300650 int bfregn = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +0300651
Eli Cohen2f5ff262017-01-03 23:55:21 +0200652 mutex_lock(&bfregi->lock);
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300653 if (bfregi->ver >= 2) {
654 bfregn = alloc_high_class_bfreg(dev, bfregi);
655 if (bfregn < 0)
656 bfregn = alloc_med_class_bfreg(dev, bfregi);
657 }
658
659 if (bfregn < 0) {
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200660 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200661 bfregn = 0;
662 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300663 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200664 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300665
Eli Cohen2f5ff262017-01-03 23:55:21 +0200666 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300667}
668
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200669void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300670{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200671 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200672 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200673 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300674}
675
676static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
677{
678 switch (state) {
679 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
680 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
681 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
682 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
683 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
684 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
685 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
686 default: return -1;
687 }
688}
689
690static int to_mlx5_st(enum ib_qp_type type)
691{
692 switch (type) {
693 case IB_QPT_RC: return MLX5_QP_ST_RC;
694 case IB_QPT_UC: return MLX5_QP_ST_UC;
695 case IB_QPT_UD: return MLX5_QP_ST_UD;
696 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
697 case IB_QPT_XRC_INI:
698 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
699 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200700 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200701 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300702 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300703 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200704 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300705 case IB_QPT_MAX:
706 default: return -EINVAL;
707 }
708}
709
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300710static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
711 struct mlx5_ib_cq *recv_cq);
712static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
713 struct mlx5_ib_cq *recv_cq);
714
Yishai Hadas7c043e92018-06-17 13:00:03 +0300715int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300716 struct mlx5_bfreg_info *bfregi, u32 bfregn,
Yishai Hadas7c043e92018-06-17 13:00:03 +0300717 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300718{
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300719 unsigned int bfregs_per_sys_page;
720 u32 index_of_sys_page;
721 u32 offset;
Eli Cohenb037c292017-01-03 23:55:26 +0200722
723 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
724 MLX5_NON_FP_BFREGS_PER_UAR;
725 index_of_sys_page = bfregn / bfregs_per_sys_page;
726
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200727 if (dyn_bfreg) {
728 index_of_sys_page += bfregi->num_static_sys_pages;
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300729
730 if (index_of_sys_page >= bfregi->num_sys_pages)
731 return -EINVAL;
732
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200733 if (bfregn > bfregi->num_dyn_bfregs ||
734 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
735 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
736 return -EINVAL;
737 }
738 }
Eli Cohenb037c292017-01-03 23:55:26 +0200739
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200740 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200741 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300742}
743
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200744static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200745 unsigned long addr, size_t size,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200746 struct ib_umem **umem, int *npages, int *page_shift,
747 int *ncont, u32 *offset)
majd@mellanox.com19098df2016-01-14 19:13:03 +0200748{
749 int err;
750
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200751 *umem = ib_umem_get(udata, addr, size, 0, 0);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200752 if (IS_ERR(*umem)) {
753 mlx5_ib_dbg(dev, "umem_get failed\n");
754 return PTR_ERR(*umem);
755 }
756
Majd Dibbiny762f8992016-10-27 16:36:47 +0300757 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200758
759 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
760 if (err) {
761 mlx5_ib_warn(dev, "bad offset\n");
762 goto err_umem;
763 }
764
765 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
766 addr, size, *npages, *page_shift, *ncont, *offset);
767
768 return 0;
769
770err_umem:
771 ib_umem_release(*umem);
772 *umem = NULL;
773
774 return err;
775}
776
Maor Gottliebfe248c32017-05-30 10:29:14 +0300777static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300778 struct mlx5_ib_rwq *rwq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300779{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300780 struct mlx5_ib_ucontext *context =
781 rdma_udata_to_drv_context(
782 udata,
783 struct mlx5_ib_ucontext,
784 ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300785
Maor Gottliebfe248c32017-05-30 10:29:14 +0300786 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
787 atomic_dec(&dev->delay_drop.rqs_cnt);
788
Yishai Hadas79b20a62016-05-23 15:20:50 +0300789 mlx5_ib_db_unmap_user(context, &rwq->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300790 ib_umem_release(rwq->umem);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300791}
792
793static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200794 struct ib_udata *udata, struct mlx5_ib_rwq *rwq,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300795 struct mlx5_ib_create_wq *ucmd)
796{
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200797 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
798 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300799 int page_shift = 0;
800 int npages;
801 u32 offset = 0;
802 int ncont = 0;
803 int err;
804
805 if (!ucmd->buf_addr)
806 return -EINVAL;
807
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200808 rwq->umem = ib_umem_get(udata, ucmd->buf_addr, rwq->buf_size, 0, 0);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300809 if (IS_ERR(rwq->umem)) {
810 mlx5_ib_dbg(dev, "umem_get failed\n");
811 err = PTR_ERR(rwq->umem);
812 return err;
813 }
814
Majd Dibbiny762f8992016-10-27 16:36:47 +0300815 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300816 &ncont, NULL);
817 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
818 &rwq->rq_page_offset);
819 if (err) {
820 mlx5_ib_warn(dev, "bad offset\n");
821 goto err_umem;
822 }
823
824 rwq->rq_num_pas = ncont;
825 rwq->page_shift = page_shift;
826 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
827 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
828
829 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
830 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
831 npages, page_shift, ncont, offset);
832
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200833 err = mlx5_ib_db_map_user(ucontext, udata, ucmd->db_addr, &rwq->db);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300834 if (err) {
835 mlx5_ib_dbg(dev, "map failed\n");
836 goto err_umem;
837 }
838
839 rwq->create_type = MLX5_WQ_USER;
840 return 0;
841
842err_umem:
843 ib_umem_release(rwq->umem);
844 return err;
845}
846
Eli Cohenb037c292017-01-03 23:55:26 +0200847static int adjust_bfregn(struct mlx5_ib_dev *dev,
848 struct mlx5_bfreg_info *bfregi, int bfregn)
849{
850 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
851 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
852}
853
Eli Cohene126ba92013-07-07 17:25:49 +0300854static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
855 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200856 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300857 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200858 struct mlx5_ib_create_qp_resp *resp, int *inlen,
859 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300860{
861 struct mlx5_ib_ucontext *context;
862 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200863 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200864 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200865 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300866 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200867 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200868 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200869 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300870 __be64 *pas;
871 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300872 int err;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200873 u16 uid;
Eli Cohene126ba92013-07-07 17:25:49 +0300874
875 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
876 if (err) {
877 mlx5_ib_dbg(dev, "copy failed\n");
878 return err;
879 }
880
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200881 context = rdma_udata_to_drv_context(udata, struct mlx5_ib_ucontext,
882 ibucontext);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200883 if (ucmd.flags & MLX5_QP_FLAG_BFREG_INDEX) {
884 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
885 ucmd.bfreg_index, true);
886 if (uar_index < 0)
887 return uar_index;
888
889 bfregn = MLX5_IB_INVALID_BFREG;
890 } else if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL) {
891 /*
892 * TBD: should come from the verbs when we have the API
893 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200894 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200895 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200896 }
Leon Romanovsky051f2632015-12-20 12:16:11 +0200897 else {
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300898 bfregn = alloc_bfreg(dev, &context->bfregi);
899 if (bfregn < 0)
900 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300901 }
902
Eli Cohen2f5ff262017-01-03 23:55:21 +0200903 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200904 if (bfregn != MLX5_IB_INVALID_BFREG)
905 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
906 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300907
Haggai Eran48fea832014-05-22 14:50:11 +0300908 qp->rq.offset = 0;
909 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
910 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
911
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200912 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300913 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200914 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300915
majd@mellanox.com19098df2016-01-14 19:13:03 +0200916 if (ucmd.buf_addr && ubuffer->buf_size) {
917 ubuffer->buf_addr = ucmd.buf_addr;
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200918 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr,
919 ubuffer->buf_size, &ubuffer->umem,
920 &npages, &page_shift, &ncont, &offset);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200921 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200922 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200923 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200924 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300925 }
Eli Cohene126ba92013-07-07 17:25:49 +0300926
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300927 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
928 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300929 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300930 if (!*in) {
931 err = -ENOMEM;
932 goto err_umem;
933 }
Eli Cohene126ba92013-07-07 17:25:49 +0300934
Yishai Hadas7422edc2018-12-23 13:12:21 +0200935 uid = (attr->qp_type != IB_QPT_XRC_TGT &&
936 attr->qp_type != IB_QPT_XRC_INI) ? to_mpd(pd)->uid : 0;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200937 MLX5_SET(create_qp_in, *in, uid, uid);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300938 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
939 if (ubuffer->umem)
940 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
941
942 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
943
944 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
945 MLX5_SET(qpc, qpc, page_offset, offset);
946
947 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200948 if (bfregn != MLX5_IB_INVALID_BFREG)
949 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
950 else
951 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200952 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300953
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200954 err = mlx5_ib_db_map_user(context, udata, ucmd.db_addr, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300955 if (err) {
956 mlx5_ib_dbg(dev, "map failed\n");
957 goto err_free;
958 }
959
Jason Gunthorpe41d902c2018-04-03 10:00:53 +0300960 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
Eli Cohene126ba92013-07-07 17:25:49 +0300961 if (err) {
962 mlx5_ib_dbg(dev, "copy failed\n");
963 goto err_unmap;
964 }
965 qp->create_type = MLX5_QP_USER;
966
967 return 0;
968
969err_unmap:
970 mlx5_ib_db_unmap_user(context, &qp->db);
971
972err_free:
Al Viro479163f2014-11-20 08:13:57 +0000973 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300974
975err_umem:
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300976 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300977
Eli Cohen2f5ff262017-01-03 23:55:21 +0200978err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200979 if (bfregn != MLX5_IB_INVALID_BFREG)
980 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300981 return err;
982}
983
Eli Cohenb037c292017-01-03 23:55:26 +0200984static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300985 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base,
986 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +0300987{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300988 struct mlx5_ib_ucontext *context =
989 rdma_udata_to_drv_context(
990 udata,
991 struct mlx5_ib_ucontext,
992 ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +0300993
Eli Cohene126ba92013-07-07 17:25:49 +0300994 mlx5_ib_db_unmap_user(context, &qp->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300995 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200996
997 /*
998 * Free only the BFREGs which are handled by the kernel.
999 * BFREGs of UARs allocated dynamically are handled by user.
1000 */
1001 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
1002 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001003}
1004
Guy Levi34f4c952018-11-26 08:15:50 +02001005/* get_sq_edge - Get the next nearby edge.
1006 *
1007 * An 'edge' is defined as the first following address after the end
1008 * of the fragment or the SQ. Accordingly, during the WQE construction
1009 * which repetitively increases the pointer to write the next data, it
1010 * simply should check if it gets to an edge.
1011 *
1012 * @sq - SQ buffer.
1013 * @idx - Stride index in the SQ buffer.
1014 *
1015 * Return:
1016 * The new edge.
1017 */
1018static void *get_sq_edge(struct mlx5_ib_wq *sq, u32 idx)
1019{
1020 void *fragment_end;
1021
1022 fragment_end = mlx5_frag_buf_get_wqe
1023 (&sq->fbc,
1024 mlx5_frag_buf_get_idx_last_contig_stride(&sq->fbc, idx));
1025
1026 return fragment_end + MLX5_SEND_WQE_BB;
1027}
1028
Eli Cohene126ba92013-07-07 17:25:49 +03001029static int create_kernel_qp(struct mlx5_ib_dev *dev,
1030 struct ib_qp_init_attr *init_attr,
1031 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001032 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +02001033 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +03001034{
Eli Cohene126ba92013-07-07 17:25:49 +03001035 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001036 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +03001037 int err;
1038
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +03001039 if (init_attr->create_flags & ~(IB_QP_CREATE_INTEGRITY_EN |
Erez Shitritf0313962016-02-21 16:27:17 +02001040 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +02001041 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +03001042 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +02001043 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +02001044 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001045
1046 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001047 qp->bf.bfreg = &dev->fp_bfreg;
1048 else
1049 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +03001050
Eli Cohend8030b02017-02-09 19:31:47 +02001051 /* We need to divide by two since each register is comprised of
1052 * two buffers of identical size, namely odd and even
1053 */
1054 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001055 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +03001056
1057 err = calc_sq_size(dev, init_attr, qp);
1058 if (err < 0) {
1059 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001060 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001061 }
1062
1063 qp->rq.offset = 0;
1064 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001065 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +03001066
Guy Levi34f4c952018-11-26 08:15:50 +02001067 err = mlx5_frag_buf_alloc_node(dev->mdev, base->ubuffer.buf_size,
1068 &qp->buf, dev->mdev->priv.numa_node);
Eli Cohene126ba92013-07-07 17:25:49 +03001069 if (err) {
1070 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001071 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001072 }
1073
Guy Levi34f4c952018-11-26 08:15:50 +02001074 if (qp->rq.wqe_cnt)
1075 mlx5_init_fbc(qp->buf.frags, qp->rq.wqe_shift,
1076 ilog2(qp->rq.wqe_cnt), &qp->rq.fbc);
1077
1078 if (qp->sq.wqe_cnt) {
1079 int sq_strides_offset = (qp->sq.offset & (PAGE_SIZE - 1)) /
1080 MLX5_SEND_WQE_BB;
1081 mlx5_init_fbc_offset(qp->buf.frags +
1082 (qp->sq.offset / PAGE_SIZE),
1083 ilog2(MLX5_SEND_WQE_BB),
1084 ilog2(qp->sq.wqe_cnt),
1085 sq_strides_offset, &qp->sq.fbc);
1086
1087 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
1088 }
1089
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001090 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
1091 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001092 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001093 if (!*in) {
1094 err = -ENOMEM;
1095 goto err_buf;
1096 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001097
1098 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1099 MLX5_SET(qpc, qpc, uar_page, uar_index);
1100 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1101
Eli Cohene126ba92013-07-07 17:25:49 +03001102 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001103 MLX5_SET(qpc, qpc, fre, 1);
1104 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001105
Haggai Eranb11a4f92016-02-29 15:45:03 +02001106 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001107 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +02001108 qp->flags |= MLX5_IB_QP_SQPN_QP1;
1109 }
1110
Guy Levi34f4c952018-11-26 08:15:50 +02001111 mlx5_fill_page_frag_array(&qp->buf,
1112 (__be64 *)MLX5_ADDR_OF(create_qp_in,
1113 *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +03001114
Jack Morgenstein9603b612014-07-28 23:30:22 +03001115 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001116 if (err) {
1117 mlx5_ib_dbg(dev, "err %d\n", err);
1118 goto err_free;
1119 }
1120
Li Dongyangb5883002017-08-16 23:31:22 +10001121 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
1122 sizeof(*qp->sq.wrid), GFP_KERNEL);
1123 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1124 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1125 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1126 sizeof(*qp->rq.wrid), GFP_KERNEL);
1127 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1128 sizeof(*qp->sq.w_list), GFP_KERNEL);
1129 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1130 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001131
1132 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1133 !qp->sq.w_list || !qp->sq.wqe_head) {
1134 err = -ENOMEM;
1135 goto err_wrid;
1136 }
1137 qp->create_type = MLX5_QP_KERNEL;
1138
1139 return 0;
1140
1141err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001142 kvfree(qp->sq.wqe_head);
1143 kvfree(qp->sq.w_list);
1144 kvfree(qp->sq.wrid);
1145 kvfree(qp->sq.wr_data);
1146 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001147 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001148
1149err_free:
Al Viro479163f2014-11-20 08:13:57 +00001150 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001151
1152err_buf:
Guy Levi34f4c952018-11-26 08:15:50 +02001153 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001154 return err;
1155}
1156
1157static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1158{
Li Dongyangb5883002017-08-16 23:31:22 +10001159 kvfree(qp->sq.wqe_head);
1160 kvfree(qp->sq.w_list);
1161 kvfree(qp->sq.wrid);
1162 kvfree(qp->sq.wr_data);
1163 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001164 mlx5_db_free(dev->mdev, &qp->db);
Guy Levi34f4c952018-11-26 08:15:50 +02001165 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001166}
1167
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001168static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001169{
1170 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Moni Shouac32a4f22018-01-02 16:19:32 +02001171 (attr->qp_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001172 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001173 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001174 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001175 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001176 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001177 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001178}
1179
1180static int is_connected(enum ib_qp_type qp_type)
1181{
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001182 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC ||
1183 qp_type == MLX5_IB_QPT_DCI)
Eli Cohene126ba92013-07-07 17:25:49 +03001184 return 1;
1185
1186 return 0;
1187}
1188
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001189static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001190 struct mlx5_ib_qp *qp,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001191 struct mlx5_ib_sq *sq, u32 tdn,
1192 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001193{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001194 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001195 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1196
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001197 MLX5_SET(create_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001198 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001199 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1200 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1201
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001202 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1203}
1204
1205static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001206 struct mlx5_ib_sq *sq, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001207{
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001208 mlx5_cmd_destroy_tis(dev->mdev, sq->tisn, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001209}
1210
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001211static void destroy_flow_rule_vport_sq(struct mlx5_ib_sq *sq)
Mark Blochb96c9dd2018-01-29 10:40:37 +00001212{
1213 if (sq->flow_rule)
1214 mlx5_del_flow_rules(sq->flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001215 sq->flow_rule = NULL;
Mark Blochb96c9dd2018-01-29 10:40:37 +00001216}
1217
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001218static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001219 struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001220 struct mlx5_ib_sq *sq, void *qpin,
1221 struct ib_pd *pd)
1222{
1223 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1224 __be64 *pas;
1225 void *in;
1226 void *sqc;
1227 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1228 void *wq;
1229 int inlen;
1230 int err;
1231 int page_shift = 0;
1232 int npages;
1233 int ncont = 0;
1234 u32 offset = 0;
1235
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001236 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr, ubuffer->buf_size,
1237 &sq->ubuffer.umem, &npages, &page_shift, &ncont,
1238 &offset);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001239 if (err)
1240 return err;
1241
1242 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001243 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001244 if (!in) {
1245 err = -ENOMEM;
1246 goto err_umem;
1247 }
1248
Yishai Hadasc14003f2018-09-20 21:39:22 +03001249 MLX5_SET(create_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001250 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1251 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001252 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1253 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001254 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1255 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1256 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1257 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1258 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001259 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1260 MLX5_CAP_ETH(dev->mdev, swp))
1261 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001262
1263 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1264 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1265 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1266 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1267 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1268 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1269 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1270 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1271 MLX5_SET(wq, wq, page_offset, offset);
1272
1273 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1274 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1275
1276 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1277
1278 kvfree(in);
1279
1280 if (err)
1281 goto err_umem;
1282
1283 return 0;
1284
1285err_umem:
1286 ib_umem_release(sq->ubuffer.umem);
1287 sq->ubuffer.umem = NULL;
1288
1289 return err;
1290}
1291
1292static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1293 struct mlx5_ib_sq *sq)
1294{
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001295 destroy_flow_rule_vport_sq(sq);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001296 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1297 ib_umem_release(sq->ubuffer.umem);
1298}
1299
Boris Pismenny2c292db2018-03-08 15:51:40 +02001300static size_t get_rq_pas_size(void *qpc)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001301{
1302 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1303 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1304 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1305 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1306 u32 po_quanta = 1 << (log_page_size - 6);
1307 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1308 u32 page_size = 1 << log_page_size;
1309 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1310 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1311
1312 return rq_num_pas * sizeof(u64);
1313}
1314
1315static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001316 struct mlx5_ib_rq *rq, void *qpin,
Yishai Hadas34d57582018-09-20 21:39:21 +03001317 size_t qpinlen, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001318{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001319 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001320 __be64 *pas;
1321 __be64 *qp_pas;
1322 void *in;
1323 void *rqc;
1324 void *wq;
1325 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001326 size_t rq_pas_size = get_rq_pas_size(qpc);
1327 size_t inlen;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001328 int err;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001329
1330 if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
1331 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001332
1333 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001334 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001335 if (!in)
1336 return -ENOMEM;
1337
Yishai Hadas34d57582018-09-20 21:39:21 +03001338 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001339 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001340 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1341 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001342 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1343 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1344 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1345 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1346 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1347
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001348 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1349 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1350
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001351 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1352 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001353 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1354 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001355 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1356 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1357 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1358 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1359 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1360 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1361
1362 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1363 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1364 memcpy(pas, qp_pas, rq_pas_size);
1365
1366 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1367
1368 kvfree(in);
1369
1370 return err;
1371}
1372
1373static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1374 struct mlx5_ib_rq *rq)
1375{
1376 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1377}
1378
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001379static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1380{
1381 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1382 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1383 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1384}
1385
Mark Bloch0042f9e2018-09-17 13:30:49 +03001386static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1387 struct mlx5_ib_rq *rq,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001388 u32 qp_flags_en,
1389 struct ib_pd *pd)
Mark Bloch0042f9e2018-09-17 13:30:49 +03001390{
1391 if (qp_flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1392 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1393 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001394 mlx5_cmd_destroy_tir(dev->mdev, rq->tirn, to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001395}
1396
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001397static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001398 struct mlx5_ib_rq *rq, u32 tdn,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001399 u32 *qp_flags_en,
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001400 struct ib_pd *pd,
1401 u32 *out, int outlen)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001402{
Mark Bloch175edba2018-09-17 13:30:48 +03001403 u8 lb_flag = 0;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001404 u32 *in;
1405 void *tirc;
1406 int inlen;
1407 int err;
1408
1409 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001410 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001411 if (!in)
1412 return -ENOMEM;
1413
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001414 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001415 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1416 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1417 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1418 MLX5_SET(tirc, tirc, transport_domain, tdn);
Mark Bloch175edba2018-09-17 13:30:48 +03001419 if (*qp_flags_en & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001420 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001421
Mark Bloch175edba2018-09-17 13:30:48 +03001422 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
1423 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1424
1425 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
1426 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1427
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001428 if (dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001429 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1430 *qp_flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1431 }
1432
1433 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
Mark Blochec9c2fb2018-01-15 13:11:37 +00001434
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001435 err = mlx5_core_create_tir_out(dev->mdev, in, inlen, out, outlen);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001436
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001437 rq->tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001438 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1439 err = mlx5_ib_enable_lb(dev, false, true);
1440
1441 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001442 destroy_raw_packet_qp_tir(dev, rq, 0, pd);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001443 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001444 kvfree(in);
1445
1446 return err;
1447}
1448
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001449static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001450 u32 *in, size_t inlen,
Yishai Hadas7f720522018-09-20 21:45:18 +03001451 struct ib_pd *pd,
1452 struct ib_udata *udata,
1453 struct mlx5_ib_create_qp_resp *resp)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001454{
1455 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1456 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1457 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001458 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1459 udata, struct mlx5_ib_ucontext, ibucontext);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001460 int err;
1461 u32 tdn = mucontext->tdn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001462 u16 uid = to_mpd(pd)->uid;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001463 u32 out[MLX5_ST_SZ_DW(create_tir_out)] = {};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001464
1465 if (qp->sq.wqe_cnt) {
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001466 err = create_raw_packet_qp_tis(dev, qp, sq, tdn, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001467 if (err)
1468 return err;
1469
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001470 err = create_raw_packet_qp_sq(dev, udata, sq, in, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001471 if (err)
1472 goto err_destroy_tis;
1473
Yishai Hadas7f720522018-09-20 21:45:18 +03001474 if (uid) {
1475 resp->tisn = sq->tisn;
1476 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TISN;
1477 resp->sqn = sq->base.mqp.qpn;
1478 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_SQN;
1479 }
1480
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001481 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001482 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001483 }
1484
1485 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001486 rq->base.container_mibqp = qp;
1487
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001488 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1489 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001490 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1491 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
Yishai Hadas34d57582018-09-20 21:39:21 +03001492 err = create_raw_packet_qp_rq(dev, rq, in, inlen, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001493 if (err)
1494 goto err_destroy_sq;
1495
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001496 err = create_raw_packet_qp_tir(
1497 dev, rq, tdn, &qp->flags_en, pd, out,
1498 MLX5_ST_SZ_BYTES(create_tir_out));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001499 if (err)
1500 goto err_destroy_rq;
Yishai Hadas7f720522018-09-20 21:45:18 +03001501
1502 if (uid) {
1503 resp->rqn = rq->base.mqp.qpn;
1504 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_RQN;
1505 resp->tirn = rq->tirn;
1506 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001507 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1508 resp->tir_icm_addr = MLX5_GET(
1509 create_tir_out, out, icm_address_31_0);
1510 resp->tir_icm_addr |=
1511 (u64)MLX5_GET(create_tir_out, out,
1512 icm_address_39_32)
1513 << 32;
1514 resp->tir_icm_addr |=
1515 (u64)MLX5_GET(create_tir_out, out,
1516 icm_address_63_40)
1517 << 40;
1518 resp->comp_mask |=
1519 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1520 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001521 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001522 }
1523
1524 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1525 rq->base.mqp.qpn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001526 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
1527 if (err)
1528 goto err_destroy_tir;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001529
1530 return 0;
1531
Yishai Hadas7f720522018-09-20 21:45:18 +03001532err_destroy_tir:
1533 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001534err_destroy_rq:
1535 destroy_raw_packet_qp_rq(dev, rq);
1536err_destroy_sq:
1537 if (!qp->sq.wqe_cnt)
1538 return err;
1539 destroy_raw_packet_qp_sq(dev, sq);
1540err_destroy_tis:
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001541 destroy_raw_packet_qp_tis(dev, sq, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001542
1543 return err;
1544}
1545
1546static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1547 struct mlx5_ib_qp *qp)
1548{
1549 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1550 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1551 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1552
1553 if (qp->rq.wqe_cnt) {
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001554 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001555 destroy_raw_packet_qp_rq(dev, rq);
1556 }
1557
1558 if (qp->sq.wqe_cnt) {
1559 destroy_raw_packet_qp_sq(dev, sq);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001560 destroy_raw_packet_qp_tis(dev, sq, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001561 }
1562}
1563
1564static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1565 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1566{
1567 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1568 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1569
1570 sq->sq = &qp->sq;
1571 rq->rq = &qp->rq;
1572 sq->doorbell = &qp->db;
1573 rq->doorbell = &qp->db;
1574}
1575
Yishai Hadas28d61372016-05-23 15:20:56 +03001576static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1577{
Mark Bloch0042f9e2018-09-17 13:30:49 +03001578 if (qp->flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1579 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1580 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001581 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1582 to_mpd(qp->ibqp.pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001583}
1584
1585static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1586 struct ib_pd *pd,
1587 struct ib_qp_init_attr *init_attr,
1588 struct ib_udata *udata)
1589{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001590 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1591 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas28d61372016-05-23 15:20:56 +03001592 struct mlx5_ib_create_qp_resp resp = {};
1593 int inlen;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001594 int outlen;
Yishai Hadas28d61372016-05-23 15:20:56 +03001595 int err;
1596 u32 *in;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001597 u32 *out;
Yishai Hadas28d61372016-05-23 15:20:56 +03001598 void *tirc;
1599 void *hfso;
1600 u32 selected_fields = 0;
Matan Barak2d93fc82018-03-28 09:27:55 +03001601 u32 outer_l4;
Yishai Hadas28d61372016-05-23 15:20:56 +03001602 size_t min_resp_len;
1603 u32 tdn = mucontext->tdn;
1604 struct mlx5_ib_create_qp_rss ucmd = {};
1605 size_t required_cmd_sz;
Mark Bloch175edba2018-09-17 13:30:48 +03001606 u8 lb_flag = 0;
Yishai Hadas28d61372016-05-23 15:20:56 +03001607
1608 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1609 return -EOPNOTSUPP;
1610
1611 if (init_attr->create_flags || init_attr->send_cq)
1612 return -EINVAL;
1613
Eli Cohen2f5ff262017-01-03 23:55:21 +02001614 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001615 if (udata->outlen < min_resp_len)
1616 return -EINVAL;
1617
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001618 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001619 if (udata->inlen < required_cmd_sz) {
1620 mlx5_ib_dbg(dev, "invalid inlen\n");
1621 return -EINVAL;
1622 }
1623
1624 if (udata->inlen > sizeof(ucmd) &&
1625 !ib_is_udata_cleared(udata, sizeof(ucmd),
1626 udata->inlen - sizeof(ucmd))) {
1627 mlx5_ib_dbg(dev, "inlen is not supported\n");
1628 return -EOPNOTSUPP;
1629 }
1630
1631 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1632 mlx5_ib_dbg(dev, "copy failed\n");
1633 return -EFAULT;
1634 }
1635
1636 if (ucmd.comp_mask) {
1637 mlx5_ib_dbg(dev, "invalid comp mask\n");
1638 return -EOPNOTSUPP;
1639 }
1640
Mark Bloch175edba2018-09-17 13:30:48 +03001641 if (ucmd.flags & ~(MLX5_QP_FLAG_TUNNEL_OFFLOADS |
1642 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1643 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)) {
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001644 mlx5_ib_dbg(dev, "invalid flags\n");
1645 return -EOPNOTSUPP;
1646 }
1647
1648 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1649 !tunnel_offload_supported(dev->mdev)) {
1650 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001651 return -EOPNOTSUPP;
1652 }
1653
Maor Gottlieb309fa342017-10-19 08:25:56 +03001654 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1655 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1656 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1657 return -EOPNOTSUPP;
1658 }
1659
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001660 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC || dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001661 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1662 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1663 }
1664
1665 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
1666 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1667 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
1668 }
1669
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001670 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
Yishai Hadas28d61372016-05-23 15:20:56 +03001671 if (err) {
1672 mlx5_ib_dbg(dev, "copy failed\n");
1673 return -EINVAL;
1674 }
1675
1676 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001677 outlen = MLX5_ST_SZ_BYTES(create_tir_out);
1678 in = kvzalloc(inlen + outlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001679 if (!in)
1680 return -ENOMEM;
1681
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001682 out = in + MLX5_ST_SZ_DW(create_tir_in);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001683 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001684 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1685 MLX5_SET(tirc, tirc, disp_type,
1686 MLX5_TIRC_DISP_TYPE_INDIRECT);
1687 MLX5_SET(tirc, tirc, indirect_table,
1688 init_attr->rwq_ind_tbl->ind_tbl_num);
1689 MLX5_SET(tirc, tirc, transport_domain, tdn);
1690
1691 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001692
1693 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1694 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1695
Mark Bloch175edba2018-09-17 13:30:48 +03001696 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
1697
Maor Gottlieb309fa342017-10-19 08:25:56 +03001698 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1699 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1700 else
1701 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1702
Yishai Hadas28d61372016-05-23 15:20:56 +03001703 switch (ucmd.rx_hash_function) {
1704 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1705 {
1706 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1707 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1708
1709 if (len != ucmd.rx_key_len) {
1710 err = -EINVAL;
1711 goto err;
1712 }
1713
1714 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1715 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1716 memcpy(rss_key, ucmd.rx_hash_key, len);
1717 break;
1718 }
1719 default:
1720 err = -EOPNOTSUPP;
1721 goto err;
1722 }
1723
1724 if (!ucmd.rx_hash_fields_mask) {
1725 /* special case when this TIR serves as steering entry without hashing */
1726 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1727 goto create_tir;
1728 err = -EINVAL;
1729 goto err;
1730 }
1731
1732 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1733 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1734 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1735 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1736 err = -EINVAL;
1737 goto err;
1738 }
1739
1740 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1741 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1742 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1743 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1744 MLX5_L3_PROT_TYPE_IPV4);
1745 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1746 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1747 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1748 MLX5_L3_PROT_TYPE_IPV6);
1749
Matan Barak2d93fc82018-03-28 09:27:55 +03001750 outer_l4 = ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1751 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) << 0 |
1752 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1753 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP)) << 1 |
1754 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI) << 2;
1755
1756 /* Check that only one l4 protocol is set */
1757 if (outer_l4 & (outer_l4 - 1)) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001758 err = -EINVAL;
1759 goto err;
1760 }
1761
1762 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1763 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1764 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1765 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1766 MLX5_L4_PROT_TYPE_TCP);
1767 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1768 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1769 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1770 MLX5_L4_PROT_TYPE_UDP);
1771
1772 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1773 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1774 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1775
1776 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1777 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1778 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1779
1780 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1781 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1782 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1783
1784 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1785 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1786 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1787
Matan Barak2d93fc82018-03-28 09:27:55 +03001788 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI)
1789 selected_fields |= MLX5_HASH_FIELD_SEL_IPSEC_SPI;
1790
Yishai Hadas28d61372016-05-23 15:20:56 +03001791 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1792
1793create_tir:
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001794 err = mlx5_core_create_tir_out(dev->mdev, in, inlen, out, outlen);
Yishai Hadas28d61372016-05-23 15:20:56 +03001795
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001796 qp->rss_qp.tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001797 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1798 err = mlx5_ib_enable_lb(dev, false, true);
1799
1800 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001801 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1802 to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001803 }
1804
Yishai Hadas28d61372016-05-23 15:20:56 +03001805 if (err)
1806 goto err;
1807
Yishai Hadas7f720522018-09-20 21:45:18 +03001808 if (mucontext->devx_uid) {
1809 resp.comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1810 resp.tirn = qp->rss_qp.tirn;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001811 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1812 resp.tir_icm_addr =
1813 MLX5_GET(create_tir_out, out, icm_address_31_0);
1814 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1815 icm_address_39_32)
1816 << 32;
1817 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1818 icm_address_63_40)
1819 << 40;
1820 resp.comp_mask |=
1821 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1822 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001823 }
1824
1825 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
1826 if (err)
1827 goto err_copy;
1828
Yishai Hadas28d61372016-05-23 15:20:56 +03001829 kvfree(in);
1830 /* qpn is reserved for that QP */
1831 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001832 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001833 return 0;
1834
Yishai Hadas7f720522018-09-20 21:45:18 +03001835err_copy:
1836 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn, mucontext->devx_uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001837err:
1838 kvfree(in);
1839 return err;
1840}
1841
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001842static void configure_responder_scat_cqe(struct ib_qp_init_attr *init_attr,
1843 void *qpc)
1844{
1845 int rcqe_sz;
1846
1847 if (init_attr->qp_type == MLX5_IB_QPT_DCI)
1848 return;
1849
1850 rcqe_sz = mlx5_ib_get_cqe_size(init_attr->recv_cq);
1851
Guy Levi7249c8e2019-04-10 10:59:45 +03001852 if (init_attr->qp_type == MLX5_IB_QPT_DCT) {
1853 if (rcqe_sz == 128)
1854 MLX5_SET(dctc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
1855
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001856 return;
1857 }
1858
Guy Levi7249c8e2019-04-10 10:59:45 +03001859 MLX5_SET(qpc, qpc, cs_res,
1860 rcqe_sz == 128 ? MLX5_RES_SCAT_DATA64_CQE :
1861 MLX5_RES_SCAT_DATA32_CQE);
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001862}
1863
1864static void configure_requester_scat_cqe(struct mlx5_ib_dev *dev,
1865 struct ib_qp_init_attr *init_attr,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001866 struct mlx5_ib_create_qp *ucmd,
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001867 void *qpc)
1868{
1869 enum ib_qp_type qpt = init_attr->qp_type;
1870 int scqe_sz;
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001871 bool allow_scat_cqe = 0;
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001872
1873 if (qpt == IB_QPT_UC || qpt == IB_QPT_UD)
1874 return;
1875
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001876 if (ucmd)
1877 allow_scat_cqe = ucmd->flags & MLX5_QP_FLAG_ALLOW_SCATTER_CQE;
1878
1879 if (!allow_scat_cqe && init_attr->sq_sig_type != IB_SIGNAL_ALL_WR)
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001880 return;
1881
1882 scqe_sz = mlx5_ib_get_cqe_size(init_attr->send_cq);
1883 if (scqe_sz == 128) {
1884 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
1885 return;
1886 }
1887
1888 if (init_attr->qp_type != MLX5_IB_QPT_DCI ||
1889 MLX5_CAP_GEN(dev->mdev, dc_req_scat_data_cqe))
1890 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
1891}
1892
Yonatan Cohena60109d2018-10-10 09:25:16 +03001893static int atomic_size_to_mode(int size_mask)
1894{
1895 /* driver does not support atomic_size > 256B
1896 * and does not know how to translate bigger sizes
1897 */
1898 int supported_size_mask = size_mask & 0x1ff;
1899 int log_max_size;
1900
1901 if (!supported_size_mask)
1902 return -EOPNOTSUPP;
1903
1904 log_max_size = __fls(supported_size_mask);
1905
1906 if (log_max_size > 3)
1907 return log_max_size;
1908
1909 return MLX5_ATOMIC_MODE_8B;
1910}
1911
1912static int get_atomic_mode(struct mlx5_ib_dev *dev,
1913 enum ib_qp_type qp_type)
1914{
1915 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
1916 u8 atomic = MLX5_CAP_GEN(dev->mdev, atomic);
1917 int atomic_mode = -EOPNOTSUPP;
1918 int atomic_size_mask;
1919
1920 if (!atomic)
1921 return -EOPNOTSUPP;
1922
1923 if (qp_type == MLX5_IB_QPT_DCT)
1924 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
1925 else
1926 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
1927
1928 if ((atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_CMP_SWAP) ||
1929 (atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_FETCH_ADD))
1930 atomic_mode = atomic_size_to_mode(atomic_size_mask);
1931
1932 if (atomic_mode <= 0 &&
1933 (atomic_operations & MLX5_ATOMIC_OPS_CMP_SWAP &&
1934 atomic_operations & MLX5_ATOMIC_OPS_FETCH_ADD))
1935 atomic_mode = MLX5_ATOMIC_MODE_IB_COMP;
1936
1937 return atomic_mode;
1938}
1939
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03001940static inline bool check_flags_mask(uint64_t input, uint64_t supported)
1941{
1942 return (input & ~supported) == 0;
1943}
1944
Eli Cohene126ba92013-07-07 17:25:49 +03001945static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1946 struct ib_qp_init_attr *init_attr,
1947 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1948{
1949 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001950 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001951 struct mlx5_core_dev *mdev = dev->mdev;
Jason Gunthorpe0625b4b2018-08-14 15:33:52 -06001952 struct mlx5_ib_create_qp_resp resp = {};
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001953 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
1954 udata, struct mlx5_ib_ucontext, ibucontext);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001955 struct mlx5_ib_cq *send_cq;
1956 struct mlx5_ib_cq *recv_cq;
1957 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001958 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001959 struct mlx5_ib_create_qp ucmd;
1960 struct mlx5_ib_qp_base *base;
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001961 int mlx5_st;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001962 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001963 u32 *in;
1964 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001965
1966 mutex_init(&qp->mutex);
1967 spin_lock_init(&qp->sq.lock);
1968 spin_lock_init(&qp->rq.lock);
1969
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001970 mlx5_st = to_mlx5_st(init_attr->qp_type);
1971 if (mlx5_st < 0)
1972 return -EINVAL;
1973
Yishai Hadas28d61372016-05-23 15:20:56 +03001974 if (init_attr->rwq_ind_tbl) {
1975 if (!udata)
1976 return -ENOSYS;
1977
1978 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1979 return err;
1980 }
1981
Eli Cohenf360d882014-04-02 00:10:16 +03001982 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001983 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001984 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1985 return -EINVAL;
1986 } else {
1987 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1988 }
1989 }
1990
Leon Romanovsky051f2632015-12-20 12:16:11 +02001991 if (init_attr->create_flags &
1992 (IB_QP_CREATE_CROSS_CHANNEL |
1993 IB_QP_CREATE_MANAGED_SEND |
1994 IB_QP_CREATE_MANAGED_RECV)) {
1995 if (!MLX5_CAP_GEN(mdev, cd)) {
1996 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1997 return -EINVAL;
1998 }
1999 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
2000 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
2001 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
2002 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
2003 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
2004 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
2005 }
Erez Shitritf0313962016-02-21 16:27:17 +02002006
2007 if (init_attr->qp_type == IB_QPT_UD &&
2008 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
2009 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
2010 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
2011 return -EOPNOTSUPP;
2012 }
2013
Majd Dibbiny358e42e2016-04-17 17:19:37 +03002014 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
2015 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2016 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
2017 return -EOPNOTSUPP;
2018 }
2019 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
2020 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
2021 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
2022 return -EOPNOTSUPP;
2023 }
2024 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
2025 }
2026
Eli Cohene126ba92013-07-07 17:25:49 +03002027 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
2028 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
2029
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02002030 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
2031 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
2032 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
2033 (init_attr->qp_type != IB_QPT_RAW_PACKET))
2034 return -EOPNOTSUPP;
2035 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
2036 }
2037
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002038 if (udata) {
Eli Cohene126ba92013-07-07 17:25:49 +03002039 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
2040 mlx5_ib_dbg(dev, "copy failed\n");
2041 return -EFAULT;
2042 }
2043
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002044 if (!check_flags_mask(ucmd.flags,
Mark Bloch8af526e2019-01-15 16:45:32 +02002045 MLX5_QP_FLAG_ALLOW_SCATTER_CQE |
2046 MLX5_QP_FLAG_BFREG_INDEX |
2047 MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE |
2048 MLX5_QP_FLAG_SCATTER_CQE |
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002049 MLX5_QP_FLAG_SIGNATURE |
Mark Bloch8af526e2019-01-15 16:45:32 +02002050 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC |
2051 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
2052 MLX5_QP_FLAG_TUNNEL_OFFLOADS |
2053 MLX5_QP_FLAG_TYPE_DCI |
2054 MLX5_QP_FLAG_TYPE_DCT))
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002055 return -EINVAL;
2056
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002057 err = get_qp_user_index(ucontext, &ucmd, udata->inlen, &uidx);
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002058 if (err)
2059 return err;
2060
Eli Cohene126ba92013-07-07 17:25:49 +03002061 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002062 if (MLX5_CAP_GEN(dev->mdev, sctr_data_cqe))
2063 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002064 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
2065 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
2066 !tunnel_offload_supported(mdev)) {
2067 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
2068 return -EOPNOTSUPP;
2069 }
Mark Bloch175edba2018-09-17 13:30:48 +03002070 qp->flags_en |= MLX5_QP_FLAG_TUNNEL_OFFLOADS;
2071 }
2072
2073 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC) {
2074 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2075 mlx5_ib_dbg(dev, "Self-LB UC isn't supported\n");
2076 return -EOPNOTSUPP;
2077 }
2078 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
2079 }
2080
2081 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
2082 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2083 mlx5_ib_dbg(dev, "Self-LB UM isn't supported\n");
2084 return -EOPNOTSUPP;
2085 }
2086 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002087 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002088
Danit Goldberg569c6652018-11-30 13:22:05 +02002089 if (ucmd.flags & MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE) {
2090 if (init_attr->qp_type != IB_QPT_RC ||
2091 !MLX5_CAP_GEN(dev->mdev, qp_packet_based)) {
2092 mlx5_ib_dbg(dev, "packet based credit mode isn't supported\n");
2093 return -EOPNOTSUPP;
2094 }
2095 qp->flags |= MLX5_IB_QP_PACKET_BASED_CREDIT;
2096 }
2097
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002098 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
2099 if (init_attr->qp_type != IB_QPT_UD ||
2100 (MLX5_CAP_GEN(dev->mdev, port_type) !=
2101 MLX5_CAP_PORT_TYPE_IB) ||
2102 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
2103 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
2104 return -EOPNOTSUPP;
2105 }
2106
2107 qp->flags |= MLX5_IB_QP_UNDERLAY;
2108 qp->underlay_qpn = init_attr->source_qpn;
2109 }
Eli Cohene126ba92013-07-07 17:25:49 +03002110 } else {
2111 qp->wq_sig = !!wq_signature;
2112 }
2113
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002114 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2115 qp->flags & MLX5_IB_QP_UNDERLAY) ?
2116 &qp->raw_packet_qp.rq.base :
2117 &qp->trans_qp.base;
2118
Eli Cohene126ba92013-07-07 17:25:49 +03002119 qp->has_rq = qp_has_rq(init_attr);
2120 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002121 qp, udata ? &ucmd : NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002122 if (err) {
2123 mlx5_ib_dbg(dev, "err %d\n", err);
2124 return err;
2125 }
2126
2127 if (pd) {
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002128 if (udata) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002129 __u32 max_wqes =
2130 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03002131 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
2132 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
2133 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
2134 mlx5_ib_dbg(dev, "invalid rq params\n");
2135 return -EINVAL;
2136 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002137 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03002138 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03002139 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03002140 return -EINVAL;
2141 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02002142 if (init_attr->create_flags &
2143 mlx5_ib_create_qp_sqpn_qp1()) {
2144 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
2145 return -EINVAL;
2146 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002147 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
2148 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002149 if (err)
2150 mlx5_ib_dbg(dev, "err %d\n", err);
2151 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002152 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
2153 base);
Eli Cohene126ba92013-07-07 17:25:49 +03002154 if (err)
2155 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03002156 }
2157
2158 if (err)
2159 return err;
2160 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002161 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03002162 if (!in)
2163 return -ENOMEM;
2164
2165 qp->create_type = MLX5_QP_EMPTY;
2166 }
2167
2168 if (is_sqp(init_attr->qp_type))
2169 qp->port = init_attr->port_num;
2170
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002171 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
2172
Noa Osheroviche7b169f2018-02-25 13:39:51 +02002173 MLX5_SET(qpc, qpc, st, mlx5_st);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002174 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03002175
2176 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002177 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002178 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002179 MLX5_SET(qpc, qpc, latency_sensitive, 1);
2180
Eli Cohene126ba92013-07-07 17:25:49 +03002181
2182 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002183 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002184
Eli Cohenf360d882014-04-02 00:10:16 +03002185 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002186 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03002187
Leon Romanovsky051f2632015-12-20 12:16:11 +02002188 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002189 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002190 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002191 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002192 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002193 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Danit Goldberg569c6652018-11-30 13:22:05 +02002194 if (qp->flags & MLX5_IB_QP_PACKET_BASED_CREDIT)
2195 MLX5_SET(qpc, qpc, req_e2e_credit_mode, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002196 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002197 configure_responder_scat_cqe(init_attr, qpc);
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002198 configure_requester_scat_cqe(dev, init_attr,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002199 udata ? &ucmd : NULL,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002200 qpc);
Eli Cohene126ba92013-07-07 17:25:49 +03002201 }
2202
2203 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002204 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
2205 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03002206 }
2207
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002208 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03002209
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002210 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002211 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002212 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002213 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002214 if (init_attr->srq &&
2215 init_attr->srq->srq_type == IB_SRQT_TM)
2216 MLX5_SET(qpc, qpc, offload_type,
2217 MLX5_QPC_OFFLOAD_TYPE_RNDV);
2218 }
Eli Cohene126ba92013-07-07 17:25:49 +03002219
2220 /* Set default resources */
2221 switch (init_attr->qp_type) {
2222 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002223 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2224 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
2225 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
2226 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002227 break;
2228 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002229 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2230 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2231 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002232 break;
2233 default:
2234 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002235 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
2236 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002237 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002238 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2239 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002240 }
2241 }
2242
2243 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002244 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002245
2246 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002247 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002248
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002249 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03002250
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002251 /* 0xffffff means we ask to work with cqe version 0 */
2252 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002253 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002254
Erez Shitritf0313962016-02-21 16:27:17 +02002255 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
2256 if (init_attr->qp_type == IB_QPT_UD &&
2257 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02002258 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
2259 qp->flags |= MLX5_IB_QP_LSO;
2260 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002261
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002262 if (init_attr->create_flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
2263 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
2264 mlx5_ib_dbg(dev, "scatter end padding is not supported\n");
2265 err = -EOPNOTSUPP;
2266 goto err;
2267 } else if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2268 MLX5_SET(qpc, qpc, end_padding_mode,
2269 MLX5_WQ_END_PAD_MODE_ALIGN);
2270 } else {
2271 qp->flags |= MLX5_IB_QP_PCI_WRITE_END_PADDING;
2272 }
2273 }
2274
Boris Pismenny2c292db2018-03-08 15:51:40 +02002275 if (inlen < 0) {
2276 err = -EINVAL;
2277 goto err;
2278 }
2279
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002280 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2281 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002282 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
2283 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
Yishai Hadas7f720522018-09-20 21:45:18 +03002284 err = create_raw_packet_qp(dev, qp, in, inlen, pd, udata,
2285 &resp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002286 } else {
2287 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
2288 }
2289
Eli Cohene126ba92013-07-07 17:25:49 +03002290 if (err) {
2291 mlx5_ib_dbg(dev, "create qp failed\n");
2292 goto err_create;
2293 }
2294
Al Viro479163f2014-11-20 08:13:57 +00002295 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002296
majd@mellanox.com19098df2016-01-14 19:13:03 +02002297 base->container_mibqp = qp;
2298 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03002299
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002300 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
2301 &send_cq, &recv_cq);
2302 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2303 mlx5_ib_lock_cqs(send_cq, recv_cq);
2304 /* Maintain device to QPs access, needed for further handling via reset
2305 * flow
2306 */
2307 list_add_tail(&qp->qps_list, &dev->qp_list);
2308 /* Maintain CQ to QPs access, needed for further handling via reset flow
2309 */
2310 if (send_cq)
2311 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
2312 if (recv_cq)
2313 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
2314 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2315 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
2316
Eli Cohene126ba92013-07-07 17:25:49 +03002317 return 0;
2318
2319err_create:
2320 if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002321 destroy_qp_user(dev, pd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002322 else if (qp->create_type == MLX5_QP_KERNEL)
2323 destroy_qp_kernel(dev, qp);
2324
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002325err:
Al Viro479163f2014-11-20 08:13:57 +00002326 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002327 return err;
2328}
2329
2330static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2331 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
2332{
2333 if (send_cq) {
2334 if (recv_cq) {
2335 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002336 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002337 spin_lock_nested(&recv_cq->lock,
2338 SINGLE_DEPTH_NESTING);
2339 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002340 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002341 __acquire(&recv_cq->lock);
2342 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002343 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002344 spin_lock_nested(&send_cq->lock,
2345 SINGLE_DEPTH_NESTING);
2346 }
2347 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002348 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002349 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002350 }
2351 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002352 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002353 __acquire(&send_cq->lock);
2354 } else {
2355 __acquire(&send_cq->lock);
2356 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002357 }
2358}
2359
2360static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2361 __releases(&send_cq->lock) __releases(&recv_cq->lock)
2362{
2363 if (send_cq) {
2364 if (recv_cq) {
2365 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
2366 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002367 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002368 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
2369 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002370 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002371 } else {
2372 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002373 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002374 }
2375 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002376 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002377 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002378 }
2379 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002380 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002381 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002382 } else {
2383 __release(&recv_cq->lock);
2384 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002385 }
2386}
2387
2388static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
2389{
2390 return to_mpd(qp->ibqp.pd);
2391}
2392
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002393static void get_cqs(enum ib_qp_type qp_type,
2394 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03002395 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
2396{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002397 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002398 case IB_QPT_XRC_TGT:
2399 *send_cq = NULL;
2400 *recv_cq = NULL;
2401 break;
2402 case MLX5_IB_QPT_REG_UMR:
2403 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002404 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002405 *recv_cq = NULL;
2406 break;
2407
2408 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002409 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002410 case IB_QPT_RC:
2411 case IB_QPT_UC:
2412 case IB_QPT_UD:
2413 case IB_QPT_RAW_IPV6:
2414 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002415 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002416 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2417 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002418 break;
2419
Eli Cohene126ba92013-07-07 17:25:49 +03002420 case IB_QPT_MAX:
2421 default:
2422 *send_cq = NULL;
2423 *recv_cq = NULL;
2424 break;
2425 }
2426}
2427
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002428static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002429 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2430 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002431
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002432static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2433 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002434{
2435 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002436 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002437 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002438 int err;
2439
Yishai Hadas28d61372016-05-23 15:20:56 +03002440 if (qp->ibqp.rwq_ind_tbl) {
2441 destroy_rss_raw_qp_tir(dev, qp);
2442 return;
2443 }
2444
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002445 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2446 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002447 &qp->raw_packet_qp.rq.base :
2448 &qp->trans_qp.base;
2449
Haggai Eran6aec21f2014-12-11 17:04:23 +02002450 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002451 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2452 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002453 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002454 MLX5_CMD_OP_2RST_QP, 0,
2455 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002456 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002457 struct mlx5_modify_raw_qp_param raw_qp_param = {
2458 .operation = MLX5_CMD_OP_2RST_QP
2459 };
2460
Aviv Heller13eab212016-09-18 20:48:04 +03002461 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002462 }
2463 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002464 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002465 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002466 }
Eli Cohene126ba92013-07-07 17:25:49 +03002467
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002468 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2469 &send_cq, &recv_cq);
2470
2471 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2472 mlx5_ib_lock_cqs(send_cq, recv_cq);
2473 /* del from lists under both locks above to protect reset flow paths */
2474 list_del(&qp->qps_list);
2475 if (send_cq)
2476 list_del(&qp->cq_send_list);
2477
2478 if (recv_cq)
2479 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002480
2481 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002482 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002483 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2484 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002485 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2486 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002487 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002488 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2489 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002490
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002491 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2492 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002493 destroy_raw_packet_qp(dev, qp);
2494 } else {
2495 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
2496 if (err)
2497 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2498 base->mqp.qpn);
2499 }
Eli Cohene126ba92013-07-07 17:25:49 +03002500
Eli Cohene126ba92013-07-07 17:25:49 +03002501 if (qp->create_type == MLX5_QP_KERNEL)
2502 destroy_qp_kernel(dev, qp);
2503 else if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002504 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002505}
2506
2507static const char *ib_qp_type_str(enum ib_qp_type type)
2508{
2509 switch (type) {
2510 case IB_QPT_SMI:
2511 return "IB_QPT_SMI";
2512 case IB_QPT_GSI:
2513 return "IB_QPT_GSI";
2514 case IB_QPT_RC:
2515 return "IB_QPT_RC";
2516 case IB_QPT_UC:
2517 return "IB_QPT_UC";
2518 case IB_QPT_UD:
2519 return "IB_QPT_UD";
2520 case IB_QPT_RAW_IPV6:
2521 return "IB_QPT_RAW_IPV6";
2522 case IB_QPT_RAW_ETHERTYPE:
2523 return "IB_QPT_RAW_ETHERTYPE";
2524 case IB_QPT_XRC_INI:
2525 return "IB_QPT_XRC_INI";
2526 case IB_QPT_XRC_TGT:
2527 return "IB_QPT_XRC_TGT";
2528 case IB_QPT_RAW_PACKET:
2529 return "IB_QPT_RAW_PACKET";
2530 case MLX5_IB_QPT_REG_UMR:
2531 return "MLX5_IB_QPT_REG_UMR";
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002532 case IB_QPT_DRIVER:
2533 return "IB_QPT_DRIVER";
Eli Cohene126ba92013-07-07 17:25:49 +03002534 case IB_QPT_MAX:
2535 default:
2536 return "Invalid QP type";
2537 }
2538}
2539
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002540static struct ib_qp *mlx5_ib_create_dct(struct ib_pd *pd,
2541 struct ib_qp_init_attr *attr,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002542 struct mlx5_ib_create_qp *ucmd,
2543 struct ib_udata *udata)
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002544{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002545 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2546 udata, struct mlx5_ib_ucontext, ibucontext);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002547 struct mlx5_ib_qp *qp;
2548 int err = 0;
2549 u32 uidx = MLX5_IB_DEFAULT_UIDX;
2550 void *dctc;
2551
2552 if (!attr->srq || !attr->recv_cq)
2553 return ERR_PTR(-EINVAL);
2554
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002555 err = get_qp_user_index(ucontext, ucmd, sizeof(*ucmd), &uidx);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002556 if (err)
2557 return ERR_PTR(err);
2558
2559 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2560 if (!qp)
2561 return ERR_PTR(-ENOMEM);
2562
2563 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
2564 if (!qp->dct.in) {
2565 err = -ENOMEM;
2566 goto err_free;
2567 }
2568
Yishai Hadasa01a5862018-09-20 21:39:24 +03002569 MLX5_SET(create_dct_in, qp->dct.in, uid, to_mpd(pd)->uid);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002570 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shoua776a3902018-01-02 16:19:33 +02002571 qp->qp_sub_type = MLX5_IB_QPT_DCT;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002572 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2573 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2574 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2575 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2576 MLX5_SET(dctc, dctc, user_index, uidx);
2577
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002578 if (ucmd->flags & MLX5_QP_FLAG_SCATTER_CQE)
2579 configure_responder_scat_cqe(attr, dctc);
2580
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002581 qp->state = IB_QPS_RESET;
2582
2583 return &qp->ibqp;
2584err_free:
2585 kfree(qp);
2586 return ERR_PTR(err);
2587}
2588
2589static int set_mlx_qp_type(struct mlx5_ib_dev *dev,
2590 struct ib_qp_init_attr *init_attr,
2591 struct mlx5_ib_create_qp *ucmd,
2592 struct ib_udata *udata)
2593{
2594 enum { MLX_QP_FLAGS = MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI };
2595 int err;
2596
2597 if (!udata)
2598 return -EINVAL;
2599
2600 if (udata->inlen < sizeof(*ucmd)) {
2601 mlx5_ib_dbg(dev, "create_qp user command is smaller than expected\n");
2602 return -EINVAL;
2603 }
2604 err = ib_copy_from_udata(ucmd, udata, sizeof(*ucmd));
2605 if (err)
2606 return err;
2607
2608 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCI) {
2609 init_attr->qp_type = MLX5_IB_QPT_DCI;
2610 } else {
2611 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCT) {
2612 init_attr->qp_type = MLX5_IB_QPT_DCT;
2613 } else {
2614 mlx5_ib_dbg(dev, "Invalid QP flags\n");
2615 return -EINVAL;
2616 }
2617 }
2618
2619 if (!MLX5_CAP_GEN(dev->mdev, dct)) {
2620 mlx5_ib_dbg(dev, "DC transport is not supported\n");
2621 return -EOPNOTSUPP;
2622 }
2623
2624 return 0;
2625}
2626
Eli Cohene126ba92013-07-07 17:25:49 +03002627struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002628 struct ib_qp_init_attr *verbs_init_attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002629 struct ib_udata *udata)
2630{
2631 struct mlx5_ib_dev *dev;
2632 struct mlx5_ib_qp *qp;
2633 u16 xrcdn = 0;
2634 int err;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002635 struct ib_qp_init_attr mlx_init_attr;
2636 struct ib_qp_init_attr *init_attr = verbs_init_attr;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002637 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2638 udata, struct mlx5_ib_ucontext, ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03002639
2640 if (pd) {
2641 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002642
2643 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002644 if (!ucontext) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002645 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2646 return ERR_PTR(-EINVAL);
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002647 } else if (!ucontext->cqe_version) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002648 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2649 return ERR_PTR(-EINVAL);
2650 }
2651 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002652 } else {
2653 /* being cautious here */
2654 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2655 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2656 pr_warn("%s: no PD for transport %s\n", __func__,
2657 ib_qp_type_str(init_attr->qp_type));
2658 return ERR_PTR(-EINVAL);
2659 }
2660 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002661 }
2662
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002663 if (init_attr->qp_type == IB_QPT_DRIVER) {
2664 struct mlx5_ib_create_qp ucmd;
2665
2666 init_attr = &mlx_init_attr;
2667 memcpy(init_attr, verbs_init_attr, sizeof(*verbs_init_attr));
2668 err = set_mlx_qp_type(dev, init_attr, &ucmd, udata);
2669 if (err)
2670 return ERR_PTR(err);
Moni Shouac32a4f22018-01-02 16:19:32 +02002671
2672 if (init_attr->qp_type == MLX5_IB_QPT_DCI) {
2673 if (init_attr->cap.max_recv_wr ||
2674 init_attr->cap.max_recv_sge) {
2675 mlx5_ib_dbg(dev, "DCI QP requires zero size receive queue\n");
2676 return ERR_PTR(-EINVAL);
2677 }
Moni Shoua776a3902018-01-02 16:19:33 +02002678 } else {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002679 return mlx5_ib_create_dct(pd, init_attr, &ucmd, udata);
Moni Shouac32a4f22018-01-02 16:19:32 +02002680 }
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002681 }
2682
Eli Cohene126ba92013-07-07 17:25:49 +03002683 switch (init_attr->qp_type) {
2684 case IB_QPT_XRC_TGT:
2685 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002686 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002687 mlx5_ib_dbg(dev, "XRC not supported\n");
2688 return ERR_PTR(-ENOSYS);
2689 }
2690 init_attr->recv_cq = NULL;
2691 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2692 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2693 init_attr->send_cq = NULL;
2694 }
2695
2696 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002697 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002698 case IB_QPT_RC:
2699 case IB_QPT_UC:
2700 case IB_QPT_UD:
2701 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002702 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002703 case MLX5_IB_QPT_REG_UMR:
Moni Shouac32a4f22018-01-02 16:19:32 +02002704 case MLX5_IB_QPT_DCI:
Eli Cohene126ba92013-07-07 17:25:49 +03002705 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2706 if (!qp)
2707 return ERR_PTR(-ENOMEM);
2708
2709 err = create_qp_common(dev, pd, init_attr, udata, qp);
2710 if (err) {
2711 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2712 kfree(qp);
2713 return ERR_PTR(err);
2714 }
2715
2716 if (is_qp0(init_attr->qp_type))
2717 qp->ibqp.qp_num = 0;
2718 else if (is_qp1(init_attr->qp_type))
2719 qp->ibqp.qp_num = 1;
2720 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002721 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002722
2723 mlx5_ib_dbg(dev, "ib qpnum 0x%x, mlx qpn 0x%x, rcqn 0x%x, scqn 0x%x\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002724 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002725 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2726 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002727
majd@mellanox.com19098df2016-01-14 19:13:03 +02002728 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002729
2730 break;
2731
Haggai Erand16e91d2016-02-29 15:45:05 +02002732 case IB_QPT_GSI:
2733 return mlx5_ib_gsi_create_qp(pd, init_attr);
2734
Eli Cohene126ba92013-07-07 17:25:49 +03002735 case IB_QPT_RAW_IPV6:
2736 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002737 case IB_QPT_MAX:
2738 default:
2739 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2740 init_attr->qp_type);
2741 /* Don't support raw QPs */
2742 return ERR_PTR(-EINVAL);
2743 }
2744
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002745 if (verbs_init_attr->qp_type == IB_QPT_DRIVER)
2746 qp->qp_sub_type = init_attr->qp_type;
2747
Eli Cohene126ba92013-07-07 17:25:49 +03002748 return &qp->ibqp;
2749}
2750
Moni Shoua776a3902018-01-02 16:19:33 +02002751static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2752{
2753 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2754
2755 if (mqp->state == IB_QPS_RTR) {
2756 int err;
2757
2758 err = mlx5_core_destroy_dct(dev->mdev, &mqp->dct.mdct);
2759 if (err) {
2760 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2761 return err;
2762 }
2763 }
2764
2765 kfree(mqp->dct.in);
2766 kfree(mqp);
2767 return 0;
2768}
2769
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002770int mlx5_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002771{
2772 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2773 struct mlx5_ib_qp *mqp = to_mqp(qp);
2774
Haggai Erand16e91d2016-02-29 15:45:05 +02002775 if (unlikely(qp->qp_type == IB_QPT_GSI))
2776 return mlx5_ib_gsi_destroy_qp(qp);
2777
Moni Shoua776a3902018-01-02 16:19:33 +02002778 if (mqp->qp_sub_type == MLX5_IB_QPT_DCT)
2779 return mlx5_ib_destroy_dct(mqp);
2780
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002781 destroy_qp_common(dev, mqp, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002782
2783 kfree(mqp);
2784
2785 return 0;
2786}
2787
Yonatan Cohena60109d2018-10-10 09:25:16 +03002788static int to_mlx5_access_flags(struct mlx5_ib_qp *qp,
2789 const struct ib_qp_attr *attr,
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002790 int attr_mask, __be32 *hw_access_flags_be)
Eli Cohene126ba92013-07-07 17:25:49 +03002791{
Eli Cohene126ba92013-07-07 17:25:49 +03002792 u8 dest_rd_atomic;
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002793 u32 access_flags, hw_access_flags = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002794
Yonatan Cohena60109d2018-10-10 09:25:16 +03002795 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
2796
Eli Cohene126ba92013-07-07 17:25:49 +03002797 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2798 dest_rd_atomic = attr->max_dest_rd_atomic;
2799 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002800 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002801
2802 if (attr_mask & IB_QP_ACCESS_FLAGS)
2803 access_flags = attr->qp_access_flags;
2804 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002805 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002806
2807 if (!dest_rd_atomic)
2808 access_flags &= IB_ACCESS_REMOTE_WRITE;
2809
2810 if (access_flags & IB_ACCESS_REMOTE_READ)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002811 hw_access_flags |= MLX5_QP_BIT_RRE;
Yonatan Cohen13f8d9c2018-11-21 13:48:39 +02002812 if (access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03002813 int atomic_mode;
Eli Cohene126ba92013-07-07 17:25:49 +03002814
Yonatan Cohena60109d2018-10-10 09:25:16 +03002815 atomic_mode = get_atomic_mode(dev, qp->ibqp.qp_type);
2816 if (atomic_mode < 0)
2817 return -EOPNOTSUPP;
2818
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002819 hw_access_flags |= MLX5_QP_BIT_RAE;
2820 hw_access_flags |= atomic_mode << MLX5_ATOMIC_MODE_OFFSET;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002821 }
2822
2823 if (access_flags & IB_ACCESS_REMOTE_WRITE)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002824 hw_access_flags |= MLX5_QP_BIT_RWE;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002825
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002826 *hw_access_flags_be = cpu_to_be32(hw_access_flags);
Yonatan Cohena60109d2018-10-10 09:25:16 +03002827
2828 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002829}
2830
2831enum {
2832 MLX5_PATH_FLAG_FL = 1 << 0,
2833 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2834 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2835};
2836
2837static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2838{
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002839 if (rate == IB_RATE_PORT_CURRENT)
Eli Cohene126ba92013-07-07 17:25:49 +03002840 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002841
Michael Guralnika5a5d192018-12-09 11:49:50 +02002842 if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_600_GBPS)
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002843 return -EINVAL;
2844
2845 while (rate != IB_RATE_PORT_CURRENT &&
2846 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
2847 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
2848 --rate;
2849
2850 return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
Eli Cohene126ba92013-07-07 17:25:49 +03002851}
2852
majd@mellanox.com75850d02016-01-14 19:13:06 +02002853static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002854 struct mlx5_ib_sq *sq, u8 sl,
2855 struct ib_pd *pd)
majd@mellanox.com75850d02016-01-14 19:13:06 +02002856{
2857 void *in;
2858 void *tisc;
2859 int inlen;
2860 int err;
2861
2862 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002863 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002864 if (!in)
2865 return -ENOMEM;
2866
2867 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002868 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002869
2870 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2871 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2872
2873 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2874
2875 kvfree(in);
2876
2877 return err;
2878}
2879
Aviv Heller13eab212016-09-18 20:48:04 +03002880static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002881 struct mlx5_ib_sq *sq, u8 tx_affinity,
2882 struct ib_pd *pd)
Aviv Heller13eab212016-09-18 20:48:04 +03002883{
2884 void *in;
2885 void *tisc;
2886 int inlen;
2887 int err;
2888
2889 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002890 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002891 if (!in)
2892 return -ENOMEM;
2893
2894 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002895 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
Aviv Heller13eab212016-09-18 20:48:04 +03002896
2897 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2898 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2899
2900 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2901
2902 kvfree(in);
2903
2904 return err;
2905}
2906
majd@mellanox.com75850d02016-01-14 19:13:06 +02002907static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002908 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002909 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002910 u32 path_flags, const struct ib_qp_attr *attr,
2911 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002912{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002913 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002914 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002915 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002916 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2917 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002918
Eli Cohene126ba92013-07-07 17:25:49 +03002919 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002920 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2921 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002922
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002923 if (ah_flags & IB_AH_GRH) {
2924 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002925 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002926 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002927 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002928 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002929 return -EINVAL;
2930 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002931 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002932
2933 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002934 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002935 return -EINVAL;
Parav Pandit47ec3862018-06-13 10:22:06 +03002936
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002937 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02002938 if (qp->ibqp.qp_type == IB_QPT_RC ||
2939 qp->ibqp.qp_type == IB_QPT_UC ||
2940 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
2941 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
Parav Pandit47ec3862018-06-13 10:22:06 +03002942 path->udp_sport =
2943 mlx5_get_roce_udp_sport(dev, ah->grh.sgid_attr);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002944 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Parav Pandit47ec3862018-06-13 10:22:06 +03002945 gid_type = ah->grh.sgid_attr->gid_type;
Majd Dibbinyed884512017-01-18 14:10:35 +02002946 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002947 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002948 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002949 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2950 path->fl_free_ar |=
2951 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002952 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2953 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2954 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002955 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002956 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002957 }
2958
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002959 if (ah_flags & IB_AH_GRH) {
2960 path->mgid_index = grh->sgid_index;
2961 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002962 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002963 cpu_to_be32((grh->traffic_class << 20) |
2964 (grh->flow_label));
2965 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002966 }
2967
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002968 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002969 if (err < 0)
2970 return err;
2971 path->static_rate = err;
2972 path->port = port;
2973
Eli Cohene126ba92013-07-07 17:25:49 +03002974 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002975 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002976
majd@mellanox.com75850d02016-01-14 19:13:06 +02002977 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2978 return modify_raw_packet_eth_prio(dev->mdev,
2979 &qp->raw_packet_qp.sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002980 sl & 0xf, qp->ibqp.pd);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002981
Eli Cohene126ba92013-07-07 17:25:49 +03002982 return 0;
2983}
2984
2985static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2986 [MLX5_QP_STATE_INIT] = {
2987 [MLX5_QP_STATE_INIT] = {
2988 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2989 MLX5_QP_OPTPAR_RAE |
2990 MLX5_QP_OPTPAR_RWE |
2991 MLX5_QP_OPTPAR_PKEY_INDEX |
2992 MLX5_QP_OPTPAR_PRI_PORT,
2993 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2994 MLX5_QP_OPTPAR_PKEY_INDEX |
2995 MLX5_QP_OPTPAR_PRI_PORT,
2996 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2997 MLX5_QP_OPTPAR_Q_KEY |
2998 MLX5_QP_OPTPAR_PRI_PORT,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03002999 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3000 MLX5_QP_OPTPAR_RAE |
3001 MLX5_QP_OPTPAR_RWE |
3002 MLX5_QP_OPTPAR_PKEY_INDEX |
3003 MLX5_QP_OPTPAR_PRI_PORT,
Eli Cohene126ba92013-07-07 17:25:49 +03003004 },
3005 [MLX5_QP_STATE_RTR] = {
3006 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3007 MLX5_QP_OPTPAR_RRE |
3008 MLX5_QP_OPTPAR_RAE |
3009 MLX5_QP_OPTPAR_RWE |
3010 MLX5_QP_OPTPAR_PKEY_INDEX,
3011 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3012 MLX5_QP_OPTPAR_RWE |
3013 MLX5_QP_OPTPAR_PKEY_INDEX,
3014 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3015 MLX5_QP_OPTPAR_Q_KEY,
3016 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
3017 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03003018 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3019 MLX5_QP_OPTPAR_RRE |
3020 MLX5_QP_OPTPAR_RAE |
3021 MLX5_QP_OPTPAR_RWE |
3022 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03003023 },
3024 },
3025 [MLX5_QP_STATE_RTR] = {
3026 [MLX5_QP_STATE_RTS] = {
3027 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3028 MLX5_QP_OPTPAR_RRE |
3029 MLX5_QP_OPTPAR_RAE |
3030 MLX5_QP_OPTPAR_RWE |
3031 MLX5_QP_OPTPAR_PM_STATE |
3032 MLX5_QP_OPTPAR_RNR_TIMEOUT,
3033 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3034 MLX5_QP_OPTPAR_RWE |
3035 MLX5_QP_OPTPAR_PM_STATE,
3036 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003037 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3038 MLX5_QP_OPTPAR_RRE |
3039 MLX5_QP_OPTPAR_RAE |
3040 MLX5_QP_OPTPAR_RWE |
3041 MLX5_QP_OPTPAR_PM_STATE |
3042 MLX5_QP_OPTPAR_RNR_TIMEOUT,
Eli Cohene126ba92013-07-07 17:25:49 +03003043 },
3044 },
3045 [MLX5_QP_STATE_RTS] = {
3046 [MLX5_QP_STATE_RTS] = {
3047 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3048 MLX5_QP_OPTPAR_RAE |
3049 MLX5_QP_OPTPAR_RWE |
3050 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03003051 MLX5_QP_OPTPAR_PM_STATE |
3052 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003053 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03003054 MLX5_QP_OPTPAR_PM_STATE |
3055 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003056 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
3057 MLX5_QP_OPTPAR_SRQN |
3058 MLX5_QP_OPTPAR_CQN_RCV,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003059 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3060 MLX5_QP_OPTPAR_RAE |
3061 MLX5_QP_OPTPAR_RWE |
3062 MLX5_QP_OPTPAR_RNR_TIMEOUT |
3063 MLX5_QP_OPTPAR_PM_STATE |
3064 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003065 },
3066 },
3067 [MLX5_QP_STATE_SQER] = {
3068 [MLX5_QP_STATE_RTS] = {
3069 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
3070 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03003071 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03003072 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3073 MLX5_QP_OPTPAR_RWE |
3074 MLX5_QP_OPTPAR_RAE |
3075 MLX5_QP_OPTPAR_RRE,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003076 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3077 MLX5_QP_OPTPAR_RWE |
3078 MLX5_QP_OPTPAR_RAE |
3079 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03003080 },
3081 },
3082};
3083
3084static int ib_nr_to_mlx5_nr(int ib_mask)
3085{
3086 switch (ib_mask) {
3087 case IB_QP_STATE:
3088 return 0;
3089 case IB_QP_CUR_STATE:
3090 return 0;
3091 case IB_QP_EN_SQD_ASYNC_NOTIFY:
3092 return 0;
3093 case IB_QP_ACCESS_FLAGS:
3094 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
3095 MLX5_QP_OPTPAR_RAE;
3096 case IB_QP_PKEY_INDEX:
3097 return MLX5_QP_OPTPAR_PKEY_INDEX;
3098 case IB_QP_PORT:
3099 return MLX5_QP_OPTPAR_PRI_PORT;
3100 case IB_QP_QKEY:
3101 return MLX5_QP_OPTPAR_Q_KEY;
3102 case IB_QP_AV:
3103 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
3104 MLX5_QP_OPTPAR_PRI_PORT;
3105 case IB_QP_PATH_MTU:
3106 return 0;
3107 case IB_QP_TIMEOUT:
3108 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
3109 case IB_QP_RETRY_CNT:
3110 return MLX5_QP_OPTPAR_RETRY_COUNT;
3111 case IB_QP_RNR_RETRY:
3112 return MLX5_QP_OPTPAR_RNR_RETRY;
3113 case IB_QP_RQ_PSN:
3114 return 0;
3115 case IB_QP_MAX_QP_RD_ATOMIC:
3116 return MLX5_QP_OPTPAR_SRA_MAX;
3117 case IB_QP_ALT_PATH:
3118 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
3119 case IB_QP_MIN_RNR_TIMER:
3120 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
3121 case IB_QP_SQ_PSN:
3122 return 0;
3123 case IB_QP_MAX_DEST_RD_ATOMIC:
3124 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
3125 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
3126 case IB_QP_PATH_MIG_STATE:
3127 return MLX5_QP_OPTPAR_PM_STATE;
3128 case IB_QP_CAP:
3129 return 0;
3130 case IB_QP_DEST_QPN:
3131 return 0;
3132 }
3133 return 0;
3134}
3135
3136static int ib_mask_to_mlx5_opt(int ib_mask)
3137{
3138 int result = 0;
3139 int i;
3140
3141 for (i = 0; i < 8 * sizeof(int); i++) {
3142 if ((1 << i) & ib_mask)
3143 result |= ib_nr_to_mlx5_nr(1 << i);
3144 }
3145
3146 return result;
3147}
3148
Yishai Hadas34d57582018-09-20 21:39:21 +03003149static int modify_raw_packet_qp_rq(
3150 struct mlx5_ib_dev *dev, struct mlx5_ib_rq *rq, int new_state,
3151 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003152{
3153 void *in;
3154 void *rqc;
3155 int inlen;
3156 int err;
3157
3158 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003159 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003160 if (!in)
3161 return -ENOMEM;
3162
3163 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
Yishai Hadas34d57582018-09-20 21:39:21 +03003164 MLX5_SET(modify_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003165
3166 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
3167 MLX5_SET(rqc, rqc, state, new_state);
3168
Alex Veskereb49ab02016-08-28 12:25:53 +03003169 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
3170 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
3171 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02003172 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03003173 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
3174 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06003175 dev_info_once(
3176 &dev->ib_dev.dev,
3177 "RAW PACKET QP counters are not supported on current FW\n");
Alex Veskereb49ab02016-08-28 12:25:53 +03003178 }
3179
3180 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003181 if (err)
3182 goto out;
3183
3184 rq->state = new_state;
3185
3186out:
3187 kvfree(in);
3188 return err;
3189}
3190
Yishai Hadasc14003f2018-09-20 21:39:22 +03003191static int modify_raw_packet_qp_sq(
3192 struct mlx5_core_dev *dev, struct mlx5_ib_sq *sq, int new_state,
3193 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003194{
Bodong Wang7d29f342016-12-01 13:43:16 +02003195 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
Bodong Wang61147f32018-03-19 15:10:30 +02003196 struct mlx5_rate_limit old_rl = ibqp->rl;
3197 struct mlx5_rate_limit new_rl = old_rl;
3198 bool new_rate_added = false;
Bodong Wang7d29f342016-12-01 13:43:16 +02003199 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003200 void *in;
3201 void *sqc;
3202 int inlen;
3203 int err;
3204
3205 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003206 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003207 if (!in)
3208 return -ENOMEM;
3209
Yishai Hadasc14003f2018-09-20 21:39:22 +03003210 MLX5_SET(modify_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003211 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
3212
3213 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
3214 MLX5_SET(sqc, sqc, state, new_state);
3215
Bodong Wang7d29f342016-12-01 13:43:16 +02003216 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
3217 if (new_state != MLX5_SQC_STATE_RDY)
3218 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
3219 __func__);
3220 else
Bodong Wang61147f32018-03-19 15:10:30 +02003221 new_rl = raw_qp_param->rl;
Bodong Wang7d29f342016-12-01 13:43:16 +02003222 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003223
Bodong Wang61147f32018-03-19 15:10:30 +02003224 if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
3225 if (new_rl.rate) {
3226 err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003227 if (err) {
Bodong Wang61147f32018-03-19 15:10:30 +02003228 pr_err("Failed configuring rate limit(err %d): \
3229 rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
3230 err, new_rl.rate, new_rl.max_burst_sz,
3231 new_rl.typical_pkt_sz);
3232
Bodong Wang7d29f342016-12-01 13:43:16 +02003233 goto out;
3234 }
Bodong Wang61147f32018-03-19 15:10:30 +02003235 new_rate_added = true;
Bodong Wang7d29f342016-12-01 13:43:16 +02003236 }
3237
3238 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
Bodong Wang61147f32018-03-19 15:10:30 +02003239 /* index 0 means no limit */
Bodong Wang7d29f342016-12-01 13:43:16 +02003240 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
3241 }
3242
3243 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
3244 if (err) {
3245 /* Remove new rate from table if failed */
Bodong Wang61147f32018-03-19 15:10:30 +02003246 if (new_rate_added)
3247 mlx5_rl_remove_rate(dev, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003248 goto out;
3249 }
3250
3251 /* Only remove the old rate after new rate was set */
Bodong Wang61147f32018-03-19 15:10:30 +02003252 if ((old_rl.rate &&
3253 !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
Bodong Wang7d29f342016-12-01 13:43:16 +02003254 (new_state != MLX5_SQC_STATE_RDY))
Bodong Wang61147f32018-03-19 15:10:30 +02003255 mlx5_rl_remove_rate(dev, &old_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003256
Bodong Wang61147f32018-03-19 15:10:30 +02003257 ibqp->rl = new_rl;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003258 sq->state = new_state;
3259
3260out:
3261 kvfree(in);
3262 return err;
3263}
3264
3265static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03003266 const struct mlx5_modify_raw_qp_param *raw_qp_param,
3267 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003268{
3269 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
3270 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
3271 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02003272 int modify_rq = !!qp->rq.wqe_cnt;
3273 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003274 int rq_state;
3275 int sq_state;
3276 int err;
3277
Alex Vesker0680efa2016-08-28 12:25:52 +03003278 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003279 case MLX5_CMD_OP_RST2INIT_QP:
3280 rq_state = MLX5_RQC_STATE_RDY;
3281 sq_state = MLX5_SQC_STATE_RDY;
3282 break;
3283 case MLX5_CMD_OP_2ERR_QP:
3284 rq_state = MLX5_RQC_STATE_ERR;
3285 sq_state = MLX5_SQC_STATE_ERR;
3286 break;
3287 case MLX5_CMD_OP_2RST_QP:
3288 rq_state = MLX5_RQC_STATE_RST;
3289 sq_state = MLX5_SQC_STATE_RST;
3290 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003291 case MLX5_CMD_OP_RTR2RTS_QP:
3292 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02003293 if (raw_qp_param->set_mask ==
3294 MLX5_RAW_QP_RATE_LIMIT) {
3295 modify_rq = 0;
3296 sq_state = sq->state;
3297 } else {
3298 return raw_qp_param->set_mask ? -EINVAL : 0;
3299 }
3300 break;
3301 case MLX5_CMD_OP_INIT2INIT_QP:
3302 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03003303 if (raw_qp_param->set_mask)
3304 return -EINVAL;
3305 else
3306 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003307 default:
3308 WARN_ON(1);
3309 return -EINVAL;
3310 }
3311
Bodong Wang7d29f342016-12-01 13:43:16 +02003312 if (modify_rq) {
Yishai Hadas34d57582018-09-20 21:39:21 +03003313 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param,
3314 qp->ibqp.pd);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003315 if (err)
3316 return err;
3317 }
3318
Bodong Wang7d29f342016-12-01 13:43:16 +02003319 if (modify_sq) {
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003320 struct mlx5_flow_handle *flow_rule;
3321
Aviv Heller13eab212016-09-18 20:48:04 +03003322 if (tx_affinity) {
3323 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003324 tx_affinity,
3325 qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003326 if (err)
3327 return err;
3328 }
3329
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003330 flow_rule = create_flow_rule_vport_sq(dev, sq,
3331 raw_qp_param->port);
3332 if (IS_ERR(flow_rule))
Colin Ian King1db86312019-04-12 11:40:17 +01003333 return PTR_ERR(flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003334
3335 err = modify_raw_packet_qp_sq(dev->mdev, sq, sq_state,
3336 raw_qp_param, qp->ibqp.pd);
3337 if (err) {
3338 if (flow_rule)
3339 mlx5_del_flow_rules(flow_rule);
3340 return err;
3341 }
3342
3343 if (flow_rule) {
3344 destroy_flow_rule_vport_sq(sq);
3345 sq->flow_rule = flow_rule;
3346 }
3347
3348 return err;
Aviv Heller13eab212016-09-18 20:48:04 +03003349 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003350
3351 return 0;
3352}
3353
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003354static unsigned int get_tx_affinity(struct mlx5_ib_dev *dev,
3355 struct mlx5_ib_pd *pd,
3356 struct mlx5_ib_qp_base *qp_base,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003357 u8 port_num, struct ib_udata *udata)
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003358{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003359 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
3360 udata, struct mlx5_ib_ucontext, ibucontext);
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003361 unsigned int tx_port_affinity;
3362
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003363 if (ucontext) {
3364 tx_port_affinity = (unsigned int)atomic_add_return(
3365 1, &ucontext->tx_port_affinity) %
3366 MLX5_MAX_PORTS +
3367 1;
3368 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x ucontext %p\n",
3369 tx_port_affinity, qp_base->mqp.qpn, ucontext);
3370 } else {
3371 tx_port_affinity =
3372 (unsigned int)atomic_add_return(
Mark Bloch95579e72019-03-28 15:27:33 +02003373 1, &dev->port[port_num].roce.tx_port_affinity) %
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003374 MLX5_MAX_PORTS +
3375 1;
3376 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x\n",
3377 tx_port_affinity, qp_base->mqp.qpn);
3378 }
3379
3380 return tx_port_affinity;
3381}
3382
Eli Cohene126ba92013-07-07 17:25:49 +03003383static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
3384 const struct ib_qp_attr *attr, int attr_mask,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003385 enum ib_qp_state cur_state,
3386 enum ib_qp_state new_state,
3387 const struct mlx5_ib_modify_qp *ucmd,
3388 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003389{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003390 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
3391 [MLX5_QP_STATE_RST] = {
3392 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3393 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3394 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
3395 },
3396 [MLX5_QP_STATE_INIT] = {
3397 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3398 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3399 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
3400 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
3401 },
3402 [MLX5_QP_STATE_RTR] = {
3403 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3404 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3405 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
3406 },
3407 [MLX5_QP_STATE_RTS] = {
3408 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3409 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3410 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
3411 },
3412 [MLX5_QP_STATE_SQD] = {
3413 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3414 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3415 },
3416 [MLX5_QP_STATE_SQER] = {
3417 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3418 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3419 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
3420 },
3421 [MLX5_QP_STATE_ERR] = {
3422 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3423 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3424 }
3425 };
3426
Eli Cohene126ba92013-07-07 17:25:49 +03003427 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3428 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02003429 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03003430 struct mlx5_ib_cq *send_cq, *recv_cq;
3431 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03003432 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03003433 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003434 enum mlx5_qp_state mlx5_cur, mlx5_new;
3435 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03003436 int mlx5_st;
3437 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003438 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03003439 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003440
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003441 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
3442 qp->qp_sub_type : ibqp->qp_type);
3443 if (mlx5_st < 0)
3444 return -EINVAL;
3445
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003446 context = kzalloc(sizeof(*context), GFP_KERNEL);
3447 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03003448 return -ENOMEM;
3449
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003450 pd = get_pd(qp);
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003451 context->flags = cpu_to_be32(mlx5_st << 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003452
3453 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
3454 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3455 } else {
3456 switch (attr->path_mig_state) {
3457 case IB_MIG_MIGRATED:
3458 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3459 break;
3460 case IB_MIG_REARM:
3461 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
3462 break;
3463 case IB_MIG_ARMED:
3464 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
3465 break;
3466 }
3467 }
3468
Aviv Heller13eab212016-09-18 20:48:04 +03003469 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
3470 if ((ibqp->qp_type == IB_QPT_RC) ||
3471 (ibqp->qp_type == IB_QPT_UD &&
3472 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
3473 (ibqp->qp_type == IB_QPT_UC) ||
3474 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
3475 (ibqp->qp_type == IB_QPT_XRC_INI) ||
3476 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
Aviv Heller7c34ec12018-08-23 13:47:53 +03003477 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02003478 u8 p = mlx5_core_native_port_num(dev->mdev) - 1;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003479 tx_affinity = get_tx_affinity(dev, pd, base, p,
3480 udata);
Aviv Heller13eab212016-09-18 20:48:04 +03003481 context->flags |= cpu_to_be32(tx_affinity << 24);
3482 }
3483 }
3484 }
3485
Haggai Erand16e91d2016-02-29 15:45:05 +02003486 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003487 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003488 } else if ((ibqp->qp_type == IB_QPT_UD &&
3489 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003490 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
3491 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
3492 } else if (attr_mask & IB_QP_PATH_MTU) {
3493 if (attr->path_mtu < IB_MTU_256 ||
3494 attr->path_mtu > IB_MTU_4096) {
3495 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3496 err = -EINVAL;
3497 goto out;
3498 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003499 context->mtu_msgmax = (attr->path_mtu << 5) |
3500 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03003501 }
3502
3503 if (attr_mask & IB_QP_DEST_QPN)
3504 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
3505
3506 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003507 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003508
3509 /* todo implement counter_index functionality */
3510
3511 if (is_sqp(ibqp->qp_type))
3512 context->pri_path.port = qp->port;
3513
3514 if (attr_mask & IB_QP_PORT)
3515 context->pri_path.port = attr->port_num;
3516
3517 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003518 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003519 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003520 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003521 if (err)
3522 goto out;
3523 }
3524
3525 if (attr_mask & IB_QP_TIMEOUT)
3526 context->pri_path.ackto_lt |= attr->timeout << 3;
3527
3528 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003529 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3530 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003531 attr->alt_port_num,
3532 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3533 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003534 if (err)
3535 goto out;
3536 }
3537
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003538 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3539 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003540
3541 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3542 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3543 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3544 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3545
3546 if (attr_mask & IB_QP_RNR_RETRY)
3547 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3548
3549 if (attr_mask & IB_QP_RETRY_CNT)
3550 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3551
3552 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3553 if (attr->max_rd_atomic)
3554 context->params1 |=
3555 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3556 }
3557
3558 if (attr_mask & IB_QP_SQ_PSN)
3559 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3560
3561 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3562 if (attr->max_dest_rd_atomic)
3563 context->params2 |=
3564 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3565 }
3566
Yonatan Cohena60109d2018-10-10 09:25:16 +03003567 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08003568 __be32 access_flags;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003569
3570 err = to_mlx5_access_flags(qp, attr, attr_mask, &access_flags);
3571 if (err)
3572 goto out;
3573
3574 context->params2 |= access_flags;
3575 }
Eli Cohene126ba92013-07-07 17:25:49 +03003576
3577 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3578 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3579
3580 if (attr_mask & IB_QP_RQ_PSN)
3581 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3582
3583 if (attr_mask & IB_QP_QKEY)
3584 context->qkey = cpu_to_be32(attr->qkey);
3585
3586 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3587 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3588
Mark Bloch0837e862016-06-17 15:10:55 +03003589 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3590 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3591 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003592
3593 /* Underlay port should be used - index 0 function per port */
3594 if (qp->flags & MLX5_IB_QP_UNDERLAY)
3595 port_num = 0;
3596
Alex Veskereb49ab02016-08-28 12:25:53 +03003597 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03003598 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03003599 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003600 }
3601
Eli Cohene126ba92013-07-07 17:25:49 +03003602 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3603 context->sq_crq_size |= cpu_to_be16(1 << 4);
3604
Haggai Eranb11a4f92016-02-29 15:45:03 +02003605 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
3606 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003607
3608 mlx5_cur = to_mlx5_state(cur_state);
3609 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003610
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003611 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003612 !optab[mlx5_cur][mlx5_new]) {
3613 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003614 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003615 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003616
3617 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003618 optpar = ib_mask_to_mlx5_opt(attr_mask);
3619 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003620
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003621 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3622 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003623 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3624
3625 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003626 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03003627 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003628 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3629 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003630
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003631 if (attr_mask & IB_QP_PORT)
3632 raw_qp_param.port = attr->port_num;
3633
Bodong Wang7d29f342016-12-01 13:43:16 +02003634 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003635 raw_qp_param.rl.rate = attr->rate_limit;
3636
3637 if (ucmd->burst_info.max_burst_sz) {
3638 if (attr->rate_limit &&
3639 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3640 raw_qp_param.rl.max_burst_sz =
3641 ucmd->burst_info.max_burst_sz;
3642 } else {
3643 err = -EINVAL;
3644 goto out;
3645 }
3646 }
3647
3648 if (ucmd->burst_info.typical_pkt_sz) {
3649 if (attr->rate_limit &&
3650 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3651 raw_qp_param.rl.typical_pkt_sz =
3652 ucmd->burst_info.typical_pkt_sz;
3653 } else {
3654 err = -EINVAL;
3655 goto out;
3656 }
3657 }
3658
Bodong Wang7d29f342016-12-01 13:43:16 +02003659 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3660 }
3661
Aviv Heller13eab212016-09-18 20:48:04 +03003662 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003663 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003664 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003665 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003666 }
3667
Eli Cohene126ba92013-07-07 17:25:49 +03003668 if (err)
3669 goto out;
3670
3671 qp->state = new_state;
3672
3673 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003674 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003675 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003676 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003677 if (attr_mask & IB_QP_PORT)
3678 qp->port = attr->port_num;
3679 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003680 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003681
3682 /*
3683 * If we moved a kernel QP to RESET, clean up all old CQ
3684 * entries and reinitialize the QP.
3685 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02003686 if (new_state == IB_QPS_RESET &&
3687 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003688 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003689 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3690 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003691 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003692
3693 qp->rq.head = 0;
3694 qp->rq.tail = 0;
3695 qp->sq.head = 0;
3696 qp->sq.tail = 0;
3697 qp->sq.cur_post = 0;
Guy Levi34f4c952018-11-26 08:15:50 +02003698 if (qp->sq.wqe_cnt)
3699 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003700 qp->db.db[MLX5_RCV_DBR] = 0;
3701 qp->db.db[MLX5_SND_DBR] = 0;
3702 }
3703
3704out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003705 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003706 return err;
3707}
3708
Moni Shouac32a4f22018-01-02 16:19:32 +02003709static inline bool is_valid_mask(int mask, int req, int opt)
3710{
3711 if ((mask & req) != req)
3712 return false;
3713
3714 if (mask & ~(req | opt))
3715 return false;
3716
3717 return true;
3718}
3719
3720/* check valid transition for driver QP types
3721 * for now the only QP type that this function supports is DCI
3722 */
3723static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3724 enum ib_qp_attr_mask attr_mask)
3725{
3726 int req = IB_QP_STATE;
3727 int opt = 0;
3728
Moni Shoua99ed7482018-09-12 09:33:55 +03003729 if (new_state == IB_QPS_RESET) {
3730 return is_valid_mask(attr_mask, req, opt);
3731 } else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Moni Shouac32a4f22018-01-02 16:19:32 +02003732 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
3733 return is_valid_mask(attr_mask, req, opt);
3734 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3735 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3736 return is_valid_mask(attr_mask, req, opt);
3737 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3738 req |= IB_QP_PATH_MTU;
Artemy Kovalyov5ec03042018-11-05 08:12:07 +02003739 opt = IB_QP_PKEY_INDEX | IB_QP_AV;
Moni Shouac32a4f22018-01-02 16:19:32 +02003740 return is_valid_mask(attr_mask, req, opt);
3741 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3742 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3743 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3744 opt = IB_QP_MIN_RNR_TIMER;
3745 return is_valid_mask(attr_mask, req, opt);
3746 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3747 opt = IB_QP_MIN_RNR_TIMER;
3748 return is_valid_mask(attr_mask, req, opt);
3749 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3750 return is_valid_mask(attr_mask, req, opt);
3751 }
3752 return false;
3753}
3754
Moni Shoua776a3902018-01-02 16:19:33 +02003755/* mlx5_ib_modify_dct: modify a DCT QP
3756 * valid transitions are:
3757 * RESET to INIT: must set access_flags, pkey_index and port
3758 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3759 * mtu, gid_index and hop_limit
3760 * Other transitions and attributes are illegal
3761 */
3762static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3763 int attr_mask, struct ib_udata *udata)
3764{
3765 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3766 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3767 enum ib_qp_state cur_state, new_state;
3768 int err = 0;
3769 int required = IB_QP_STATE;
3770 void *dctc;
3771
3772 if (!(attr_mask & IB_QP_STATE))
3773 return -EINVAL;
3774
3775 cur_state = qp->state;
3776 new_state = attr->qp_state;
3777
3778 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3779 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3780 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3781 if (!is_valid_mask(attr_mask, required, 0))
3782 return -EINVAL;
3783
3784 if (attr->port_num == 0 ||
3785 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3786 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3787 attr->port_num, dev->num_ports);
3788 return -EINVAL;
3789 }
3790 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3791 MLX5_SET(dctc, dctc, rre, 1);
3792 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3793 MLX5_SET(dctc, dctc, rwe, 1);
3794 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03003795 int atomic_mode;
3796
3797 atomic_mode = get_atomic_mode(dev, MLX5_IB_QPT_DCT);
3798 if (atomic_mode < 0)
Moni Shoua776a3902018-01-02 16:19:33 +02003799 return -EOPNOTSUPP;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003800
3801 MLX5_SET(dctc, dctc, atomic_mode, atomic_mode);
Moni Shoua776a3902018-01-02 16:19:33 +02003802 MLX5_SET(dctc, dctc, rae, 1);
Moni Shoua776a3902018-01-02 16:19:33 +02003803 }
3804 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3805 MLX5_SET(dctc, dctc, port, attr->port_num);
3806 MLX5_SET(dctc, dctc, counter_set_id, dev->port[attr->port_num - 1].cnts.set_id);
3807
3808 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3809 struct mlx5_ib_modify_qp_resp resp = {};
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003810 u32 out[MLX5_ST_SZ_DW(create_dct_out)] = {0};
Moni Shoua776a3902018-01-02 16:19:33 +02003811 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3812 sizeof(resp.dctn);
3813
3814 if (udata->outlen < min_resp_len)
3815 return -EINVAL;
3816 resp.response_length = min_resp_len;
3817
3818 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3819 if (!is_valid_mask(attr_mask, required, 0))
3820 return -EINVAL;
3821 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3822 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3823 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3824 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3825 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3826 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3827
3828 err = mlx5_core_create_dct(dev->mdev, &qp->dct.mdct, qp->dct.in,
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003829 MLX5_ST_SZ_BYTES(create_dct_in), out,
3830 sizeof(out));
Moni Shoua776a3902018-01-02 16:19:33 +02003831 if (err)
3832 return err;
3833 resp.dctn = qp->dct.mdct.mqp.qpn;
3834 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3835 if (err) {
3836 mlx5_core_destroy_dct(dev->mdev, &qp->dct.mdct);
3837 return err;
3838 }
3839 } else {
3840 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3841 return -EINVAL;
3842 }
3843 if (err)
3844 qp->state = IB_QPS_ERR;
3845 else
3846 qp->state = new_state;
3847 return err;
3848}
3849
Eli Cohene126ba92013-07-07 17:25:49 +03003850int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3851 int attr_mask, struct ib_udata *udata)
3852{
3853 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3854 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02003855 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02003856 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003857 enum ib_qp_state cur_state, new_state;
Bodong Wang61147f32018-03-19 15:10:30 +02003858 size_t required_cmd_sz;
Eli Cohene126ba92013-07-07 17:25:49 +03003859 int err = -EINVAL;
3860 int port;
3861
Yishai Hadas28d61372016-05-23 15:20:56 +03003862 if (ibqp->rwq_ind_tbl)
3863 return -ENOSYS;
3864
Bodong Wang61147f32018-03-19 15:10:30 +02003865 if (udata && udata->inlen) {
3866 required_cmd_sz = offsetof(typeof(ucmd), reserved) +
3867 sizeof(ucmd.reserved);
3868 if (udata->inlen < required_cmd_sz)
3869 return -EINVAL;
3870
3871 if (udata->inlen > sizeof(ucmd) &&
3872 !ib_is_udata_cleared(udata, sizeof(ucmd),
3873 udata->inlen - sizeof(ucmd)))
3874 return -EOPNOTSUPP;
3875
3876 if (ib_copy_from_udata(&ucmd, udata,
3877 min(udata->inlen, sizeof(ucmd))))
3878 return -EFAULT;
3879
3880 if (ucmd.comp_mask ||
3881 memchr_inv(&ucmd.reserved, 0, sizeof(ucmd.reserved)) ||
3882 memchr_inv(&ucmd.burst_info.reserved, 0,
3883 sizeof(ucmd.burst_info.reserved)))
3884 return -EOPNOTSUPP;
3885 }
3886
Haggai Erand16e91d2016-02-29 15:45:05 +02003887 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3888 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3889
Moni Shouac32a4f22018-01-02 16:19:32 +02003890 if (ibqp->qp_type == IB_QPT_DRIVER)
3891 qp_type = qp->qp_sub_type;
3892 else
3893 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3894 IB_QPT_GSI : ibqp->qp_type;
3895
Moni Shoua776a3902018-01-02 16:19:33 +02003896 if (qp_type == MLX5_IB_QPT_DCT)
3897 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02003898
Eli Cohene126ba92013-07-07 17:25:49 +03003899 mutex_lock(&qp->mutex);
3900
3901 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
3902 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
3903
Achiad Shochat2811ba52015-12-23 18:47:24 +02003904 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
3905 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003906 }
3907
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003908 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
3909 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
3910 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
3911 attr_mask);
3912 goto out;
3913 }
3914 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02003915 qp_type != MLX5_IB_QPT_DCI &&
Kamal Heibd31131b2018-10-02 16:11:21 +03003916 !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
3917 attr_mask)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003918 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3919 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003920 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02003921 } else if (qp_type == MLX5_IB_QPT_DCI &&
3922 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
3923 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3924 cur_state, new_state, qp_type, attr_mask);
3925 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003926 }
Eli Cohene126ba92013-07-07 17:25:49 +03003927
3928 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003929 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003930 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003931 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3932 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003933 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003934 }
Eli Cohene126ba92013-07-07 17:25:49 +03003935
3936 if (attr_mask & IB_QP_PKEY_INDEX) {
3937 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003938 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02003939 dev->mdev->port_caps[port - 1].pkey_table_len) {
3940 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
3941 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003942 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003943 }
Eli Cohene126ba92013-07-07 17:25:49 +03003944 }
3945
3946 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003947 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003948 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
3949 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
3950 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003951 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003952 }
Eli Cohene126ba92013-07-07 17:25:49 +03003953
3954 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003955 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003956 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
3957 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
3958 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003959 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003960 }
Eli Cohene126ba92013-07-07 17:25:49 +03003961
3962 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
3963 err = 0;
3964 goto out;
3965 }
3966
Bodong Wang61147f32018-03-19 15:10:30 +02003967 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003968 new_state, &ucmd, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03003969
3970out:
3971 mutex_unlock(&qp->mutex);
3972 return err;
3973}
3974
Guy Levi34f4c952018-11-26 08:15:50 +02003975static void _handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
3976 u32 wqe_sz, void **cur_edge)
3977{
3978 u32 idx;
3979
3980 idx = (sq->cur_post + (wqe_sz >> 2)) & (sq->wqe_cnt - 1);
3981 *cur_edge = get_sq_edge(sq, idx);
3982
3983 *seg = mlx5_frag_buf_get_wqe(&sq->fbc, idx);
3984}
3985
3986/* handle_post_send_edge - Check if we get to SQ edge. If yes, update to the
3987 * next nearby edge and get new address translation for current WQE position.
3988 * @sq - SQ buffer.
3989 * @seg: Current WQE position (16B aligned).
3990 * @wqe_sz: Total current WQE size [16B].
3991 * @cur_edge: Updated current edge.
3992 */
3993static inline void handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
3994 u32 wqe_sz, void **cur_edge)
3995{
3996 if (likely(*seg != *cur_edge))
3997 return;
3998
3999 _handle_post_send_edge(sq, seg, wqe_sz, cur_edge);
4000}
4001
4002/* memcpy_send_wqe - copy data from src to WQE and update the relevant WQ's
4003 * pointers. At the end @seg is aligned to 16B regardless the copied size.
4004 * @sq - SQ buffer.
4005 * @cur_edge: Updated current edge.
4006 * @seg: Current WQE position (16B aligned).
4007 * @wqe_sz: Total current WQE size [16B].
4008 * @src: Pointer to copy from.
4009 * @n: Number of bytes to copy.
4010 */
4011static inline void memcpy_send_wqe(struct mlx5_ib_wq *sq, void **cur_edge,
4012 void **seg, u32 *wqe_sz, const void *src,
4013 size_t n)
4014{
4015 while (likely(n)) {
4016 size_t leftlen = *cur_edge - *seg;
4017 size_t copysz = min_t(size_t, leftlen, n);
4018 size_t stride;
4019
4020 memcpy(*seg, src, copysz);
4021
4022 n -= copysz;
4023 src += copysz;
4024 stride = !n ? ALIGN(copysz, 16) : copysz;
4025 *seg += stride;
4026 *wqe_sz += stride >> 4;
4027 handle_post_send_edge(sq, seg, *wqe_sz, cur_edge);
4028 }
4029}
4030
Eli Cohene126ba92013-07-07 17:25:49 +03004031static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
4032{
4033 struct mlx5_ib_cq *cq;
4034 unsigned cur;
4035
4036 cur = wq->head - wq->tail;
4037 if (likely(cur + nreq < wq->max_post))
4038 return 0;
4039
4040 cq = to_mcq(ib_cq);
4041 spin_lock(&cq->lock);
4042 cur = wq->head - wq->tail;
4043 spin_unlock(&cq->lock);
4044
4045 return cur + nreq >= wq->max_post;
4046}
4047
4048static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
4049 u64 remote_addr, u32 rkey)
4050{
4051 rseg->raddr = cpu_to_be64(remote_addr);
4052 rseg->rkey = cpu_to_be32(rkey);
4053 rseg->reserved = 0;
4054}
4055
Guy Levi34f4c952018-11-26 08:15:50 +02004056static void set_eth_seg(const struct ib_send_wr *wr, struct mlx5_ib_qp *qp,
4057 void **seg, int *size, void **cur_edge)
Erez Shitritf0313962016-02-21 16:27:17 +02004058{
Guy Levi34f4c952018-11-26 08:15:50 +02004059 struct mlx5_wqe_eth_seg *eseg = *seg;
Erez Shitritf0313962016-02-21 16:27:17 +02004060
4061 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
4062
4063 if (wr->send_flags & IB_SEND_IP_CSUM)
4064 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
4065 MLX5_ETH_WQE_L4_CSUM;
4066
Erez Shitritf0313962016-02-21 16:27:17 +02004067 if (wr->opcode == IB_WR_LSO) {
4068 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Guy Levi34f4c952018-11-26 08:15:50 +02004069 size_t left, copysz;
Erez Shitritf0313962016-02-21 16:27:17 +02004070 void *pdata = ud_wr->header;
Guy Levi34f4c952018-11-26 08:15:50 +02004071 size_t stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004072
4073 left = ud_wr->hlen;
4074 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02004075 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02004076
Guy Levi34f4c952018-11-26 08:15:50 +02004077 /* memcpy_send_wqe should get a 16B align address. Hence, we
4078 * first copy up to the current edge and then, if needed,
4079 * fall-through to memcpy_send_wqe.
Erez Shitritf0313962016-02-21 16:27:17 +02004080 */
Guy Levi34f4c952018-11-26 08:15:50 +02004081 copysz = min_t(u64, *cur_edge - (void *)eseg->inline_hdr.start,
4082 left);
4083 memcpy(eseg->inline_hdr.start, pdata, copysz);
4084 stride = ALIGN(sizeof(struct mlx5_wqe_eth_seg) -
4085 sizeof(eseg->inline_hdr.start) + copysz, 16);
4086 *size += stride / 16;
4087 *seg += stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004088
Guy Levi34f4c952018-11-26 08:15:50 +02004089 if (copysz < left) {
4090 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02004091 left -= copysz;
4092 pdata += copysz;
Guy Levi34f4c952018-11-26 08:15:50 +02004093 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, pdata,
4094 left);
Erez Shitritf0313962016-02-21 16:27:17 +02004095 }
Guy Levi34f4c952018-11-26 08:15:50 +02004096
4097 return;
Erez Shitritf0313962016-02-21 16:27:17 +02004098 }
4099
Guy Levi34f4c952018-11-26 08:15:50 +02004100 *seg += sizeof(struct mlx5_wqe_eth_seg);
4101 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
Erez Shitritf0313962016-02-21 16:27:17 +02004102}
4103
Eli Cohene126ba92013-07-07 17:25:49 +03004104static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004105 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004106{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004107 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
4108 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
4109 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004110}
4111
4112static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
4113{
4114 dseg->byte_count = cpu_to_be32(sg->length);
4115 dseg->lkey = cpu_to_be32(sg->lkey);
4116 dseg->addr = cpu_to_be64(sg->addr);
4117}
4118
Artemy Kovalyov31616252017-01-02 11:37:42 +02004119static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03004120{
Artemy Kovalyov31616252017-01-02 11:37:42 +02004121 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
4122 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03004123}
4124
4125static __be64 frwr_mkey_mask(void)
4126{
4127 u64 result;
4128
4129 result = MLX5_MKEY_MASK_LEN |
4130 MLX5_MKEY_MASK_PAGE_SIZE |
4131 MLX5_MKEY_MASK_START_ADDR |
4132 MLX5_MKEY_MASK_EN_RINVAL |
4133 MLX5_MKEY_MASK_KEY |
4134 MLX5_MKEY_MASK_LR |
4135 MLX5_MKEY_MASK_LW |
4136 MLX5_MKEY_MASK_RR |
4137 MLX5_MKEY_MASK_RW |
4138 MLX5_MKEY_MASK_A |
4139 MLX5_MKEY_MASK_SMALL_FENCE |
4140 MLX5_MKEY_MASK_FREE;
4141
4142 return cpu_to_be64(result);
4143}
4144
Sagi Grimberge6631812014-02-23 14:19:11 +02004145static __be64 sig_mkey_mask(void)
4146{
4147 u64 result;
4148
4149 result = MLX5_MKEY_MASK_LEN |
4150 MLX5_MKEY_MASK_PAGE_SIZE |
4151 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004152 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02004153 MLX5_MKEY_MASK_EN_RINVAL |
4154 MLX5_MKEY_MASK_KEY |
4155 MLX5_MKEY_MASK_LR |
4156 MLX5_MKEY_MASK_LW |
4157 MLX5_MKEY_MASK_RR |
4158 MLX5_MKEY_MASK_RW |
4159 MLX5_MKEY_MASK_SMALL_FENCE |
4160 MLX5_MKEY_MASK_FREE |
4161 MLX5_MKEY_MASK_BSF_EN;
4162
4163 return cpu_to_be64(result);
4164}
4165
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004166static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004167 struct mlx5_ib_mr *mr, u8 flags)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004168{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004169 int size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004170
4171 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004172
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004173 umr->flags = flags;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004174 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004175 umr->mkey_mask = frwr_mkey_mask();
4176}
4177
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004178static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03004179{
4180 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004181 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03004182 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03004183}
4184
Artemy Kovalyov31616252017-01-02 11:37:42 +02004185static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004186{
4187 u64 result;
4188
Artemy Kovalyov31616252017-01-02 11:37:42 +02004189 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02004190 MLX5_MKEY_MASK_FREE;
4191
4192 return cpu_to_be64(result);
4193}
4194
Artemy Kovalyov31616252017-01-02 11:37:42 +02004195static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004196{
4197 u64 result;
4198
4199 result = MLX5_MKEY_MASK_FREE;
4200
4201 return cpu_to_be64(result);
4202}
4203
Noa Osherovich56e11d62016-02-29 16:46:51 +02004204static __be64 get_umr_update_translation_mask(void)
4205{
4206 u64 result;
4207
4208 result = MLX5_MKEY_MASK_LEN |
4209 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004210 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004211
4212 return cpu_to_be64(result);
4213}
4214
Artemy Kovalyov31616252017-01-02 11:37:42 +02004215static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02004216{
4217 u64 result;
4218
Artemy Kovalyov31616252017-01-02 11:37:42 +02004219 result = MLX5_MKEY_MASK_LR |
4220 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004221 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004222 MLX5_MKEY_MASK_RW;
4223
4224 if (atomic)
4225 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004226
4227 return cpu_to_be64(result);
4228}
4229
4230static __be64 get_umr_update_pd_mask(void)
4231{
4232 u64 result;
4233
Artemy Kovalyov31616252017-01-02 11:37:42 +02004234 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004235
4236 return cpu_to_be64(result);
4237}
4238
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004239static int umr_check_mkey_mask(struct mlx5_ib_dev *dev, u64 mask)
4240{
4241 if ((mask & MLX5_MKEY_MASK_PAGE_SIZE &&
4242 MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) ||
4243 (mask & MLX5_MKEY_MASK_A &&
4244 MLX5_CAP_GEN(dev->mdev, umr_modify_atomic_disabled)))
4245 return -EPERM;
4246 return 0;
4247}
4248
4249static int set_reg_umr_segment(struct mlx5_ib_dev *dev,
4250 struct mlx5_wqe_umr_ctrl_seg *umr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004251 const struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004252{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004253 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03004254
4255 memset(umr, 0, sizeof(*umr));
4256
Haggai Eran968e78d2014-12-11 17:04:11 +02004257 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
4258 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
4259 else
4260 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
4261
Artemy Kovalyov31616252017-01-02 11:37:42 +02004262 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
4263 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
4264 u64 offset = get_xlt_octo(umrwr->offset);
4265
4266 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
4267 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
4268 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004269 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02004270 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
4271 umr->mkey_mask |= get_umr_update_translation_mask();
4272 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
4273 umr->mkey_mask |= get_umr_update_access_mask(atomic);
4274 umr->mkey_mask |= get_umr_update_pd_mask();
4275 }
4276 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
4277 umr->mkey_mask |= get_umr_enable_mr_mask();
4278 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
4279 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03004280
4281 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02004282 umr->flags |= MLX5_UMR_INLINE;
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004283
4284 return umr_check_mkey_mask(dev, be64_to_cpu(umr->mkey_mask));
Eli Cohene126ba92013-07-07 17:25:49 +03004285}
4286
4287static u8 get_umr_flags(int acc)
4288{
4289 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
4290 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
4291 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
4292 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02004293 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004294}
4295
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004296static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
4297 struct mlx5_ib_mr *mr,
4298 u32 key, int access)
4299{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004300 int ndescs = ALIGN(mr->ndescs + mr->meta_ndescs, 8) >> 1;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004301
4302 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004303
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004304 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004305 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004306 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004307 /* KLMs take twice the size of MTTs */
4308 ndescs *= 2;
4309
4310 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004311 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
4312 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
4313 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
4314 seg->len = cpu_to_be64(mr->ibmr.length);
4315 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004316}
4317
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004318static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03004319{
4320 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004321 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004322}
4323
Bart Van Asschef696bf62018-07-18 09:25:14 -07004324static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg,
4325 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004326{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004327 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004328
Eli Cohene126ba92013-07-07 17:25:49 +03004329 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02004330 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02004331 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004332
Haggai Eran968e78d2014-12-11 17:04:11 +02004333 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004334 if (umrwr->pd)
4335 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
4336 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
4337 !umrwr->length)
4338 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
4339
4340 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004341 seg->len = cpu_to_be64(umrwr->length);
4342 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03004343 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02004344 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03004345}
4346
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004347static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
4348 struct mlx5_ib_mr *mr,
4349 struct mlx5_ib_pd *pd)
4350{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004351 int bcount = mr->desc_size * (mr->ndescs + mr->meta_ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004352
4353 dseg->addr = cpu_to_be64(mr->desc_map);
4354 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
4355 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
4356}
4357
Bart Van Asschef696bf62018-07-18 09:25:14 -07004358static __be32 send_ieth(const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004359{
4360 switch (wr->opcode) {
4361 case IB_WR_SEND_WITH_IMM:
4362 case IB_WR_RDMA_WRITE_WITH_IMM:
4363 return wr->ex.imm_data;
4364
4365 case IB_WR_SEND_WITH_INV:
4366 return cpu_to_be32(wr->ex.invalidate_rkey);
4367
4368 default:
4369 return 0;
4370 }
4371}
4372
4373static u8 calc_sig(void *wqe, int size)
4374{
4375 u8 *p = wqe;
4376 u8 res = 0;
4377 int i;
4378
4379 for (i = 0; i < size; i++)
4380 res ^= p[i];
4381
4382 return ~res;
4383}
4384
4385static u8 wq_sig(void *wqe)
4386{
4387 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
4388}
4389
Bart Van Asschef696bf62018-07-18 09:25:14 -07004390static int set_data_inl_seg(struct mlx5_ib_qp *qp, const struct ib_send_wr *wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004391 void **wqe, int *wqe_sz, void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004392{
4393 struct mlx5_wqe_inline_seg *seg;
Guy Levi34f4c952018-11-26 08:15:50 +02004394 size_t offset;
Eli Cohene126ba92013-07-07 17:25:49 +03004395 int inl = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004396 int i;
4397
Guy Levi34f4c952018-11-26 08:15:50 +02004398 seg = *wqe;
4399 *wqe += sizeof(*seg);
4400 offset = sizeof(*seg);
4401
Eli Cohene126ba92013-07-07 17:25:49 +03004402 for (i = 0; i < wr->num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02004403 size_t len = wr->sg_list[i].length;
4404 void *addr = (void *)(unsigned long)(wr->sg_list[i].addr);
4405
Eli Cohene126ba92013-07-07 17:25:49 +03004406 inl += len;
4407
4408 if (unlikely(inl > qp->max_inline_data))
4409 return -ENOMEM;
4410
Guy Levi34f4c952018-11-26 08:15:50 +02004411 while (likely(len)) {
4412 size_t leftlen;
4413 size_t copysz;
4414
4415 handle_post_send_edge(&qp->sq, wqe,
4416 *wqe_sz + (offset >> 4),
4417 cur_edge);
4418
4419 leftlen = *cur_edge - *wqe;
4420 copysz = min_t(size_t, leftlen, len);
4421
4422 memcpy(*wqe, addr, copysz);
4423 len -= copysz;
4424 addr += copysz;
4425 *wqe += copysz;
4426 offset += copysz;
Eli Cohene126ba92013-07-07 17:25:49 +03004427 }
Eli Cohene126ba92013-07-07 17:25:49 +03004428 }
4429
4430 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
4431
Guy Levi34f4c952018-11-26 08:15:50 +02004432 *wqe_sz += ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004433
4434 return 0;
4435}
4436
Sagi Grimberge6631812014-02-23 14:19:11 +02004437static u16 prot_field_size(enum ib_signature_type type)
4438{
4439 switch (type) {
4440 case IB_SIG_TYPE_T10_DIF:
4441 return MLX5_DIF_SIZE;
4442 default:
4443 return 0;
4444 }
4445}
4446
4447static u8 bs_selector(int block_size)
4448{
4449 switch (block_size) {
4450 case 512: return 0x1;
4451 case 520: return 0x2;
4452 case 4096: return 0x3;
4453 case 4160: return 0x4;
4454 case 1073741824: return 0x5;
4455 default: return 0;
4456 }
4457}
4458
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004459static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
4460 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02004461{
Sagi Grimberg142537f2014-08-13 19:54:32 +03004462 /* Valid inline section and allow BSF refresh */
4463 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
4464 MLX5_BSF_REFRESH_DIF);
4465 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
4466 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004467 /* repeating block */
4468 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
4469 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
4470 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004471
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004472 if (domain->sig.dif.ref_remap)
4473 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02004474
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004475 if (domain->sig.dif.app_escape) {
4476 if (domain->sig.dif.ref_escape)
4477 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
4478 else
4479 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02004480 }
4481
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004482 inl->dif_app_bitmask_check =
4483 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02004484}
4485
4486static int mlx5_set_bsf(struct ib_mr *sig_mr,
4487 struct ib_sig_attrs *sig_attrs,
4488 struct mlx5_bsf *bsf, u32 data_size)
4489{
4490 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
4491 struct mlx5_bsf_basic *basic = &bsf->basic;
4492 struct ib_sig_domain *mem = &sig_attrs->mem;
4493 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02004494
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004495 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02004496
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004497 /* Basic + Extended + Inline */
4498 basic->bsf_size_sbs = 1 << 7;
4499 /* Input domain check byte mask */
4500 basic->check_byte_mask = sig_attrs->check_mask;
4501 basic->raw_data_size = cpu_to_be32(data_size);
4502
4503 /* Memory domain */
4504 switch (sig_attrs->mem.sig_type) {
4505 case IB_SIG_TYPE_NONE:
4506 break;
4507 case IB_SIG_TYPE_T10_DIF:
4508 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
4509 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
4510 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
4511 break;
4512 default:
4513 return -EINVAL;
4514 }
4515
4516 /* Wire domain */
4517 switch (sig_attrs->wire.sig_type) {
4518 case IB_SIG_TYPE_NONE:
4519 break;
4520 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02004521 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004522 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004523 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03004524 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02004525 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004526 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004527 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004528 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004529 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004530 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02004531 } else
4532 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
4533
Sagi Grimberg142537f2014-08-13 19:54:32 +03004534 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004535 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02004536 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004537 default:
4538 return -EINVAL;
4539 }
4540
4541 return 0;
4542}
4543
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004544static int set_sig_data_segment(const struct ib_send_wr *send_wr,
4545 struct ib_mr *sig_mr,
4546 struct ib_sig_attrs *sig_attrs,
4547 struct mlx5_ib_qp *qp, void **seg, int *size,
4548 void **cur_edge)
Sagi Grimberge6631812014-02-23 14:19:11 +02004549{
Sagi Grimberge6631812014-02-23 14:19:11 +02004550 struct mlx5_bsf *bsf;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004551 u32 data_len;
4552 u32 data_key;
4553 u64 data_va;
4554 u32 prot_len = 0;
4555 u32 prot_key = 0;
4556 u64 prot_va = 0;
4557 bool prot = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004558 int ret;
4559 int wqe_size;
4560
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004561 if (send_wr->opcode == IB_WR_REG_SIG_MR) {
4562 const struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
4563
4564 data_len = wr->wr.sg_list->length;
4565 data_key = wr->wr.sg_list->lkey;
4566 data_va = wr->wr.sg_list->addr;
4567 if (wr->prot) {
4568 prot_len = wr->prot->length;
4569 prot_key = wr->prot->lkey;
4570 prot_va = wr->prot->addr;
4571 prot = true;
4572 }
4573 } else {
4574 struct mlx5_ib_mr *mr = to_mmr(sig_mr);
4575 struct mlx5_ib_mr *pi_mr = mr->pi_mr;
4576
4577 data_len = pi_mr->data_length;
4578 data_key = pi_mr->ibmr.lkey;
4579 data_va = pi_mr->ibmr.iova;
4580 if (pi_mr->meta_ndescs) {
4581 prot_len = pi_mr->meta_length;
4582 prot_key = pi_mr->ibmr.lkey;
4583 prot_va = pi_mr->ibmr.iova + data_len;
4584 prot = true;
4585 }
4586 }
4587
4588 if (!prot || (data_key == prot_key && data_va == prot_va &&
4589 data_len == prot_len)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004590 /**
4591 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004592 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02004593 * So need construct:
4594 * ------------------
4595 * | data_klm |
4596 * ------------------
4597 * | BSF |
4598 * ------------------
4599 **/
4600 struct mlx5_klm *data_klm = *seg;
4601
4602 data_klm->bcount = cpu_to_be32(data_len);
4603 data_klm->key = cpu_to_be32(data_key);
4604 data_klm->va = cpu_to_be64(data_va);
4605 wqe_size = ALIGN(sizeof(*data_klm), 64);
4606 } else {
4607 /**
4608 * Source domain contains signature information
4609 * So need construct a strided block format:
4610 * ---------------------------
4611 * | stride_block_ctrl |
4612 * ---------------------------
4613 * | data_klm |
4614 * ---------------------------
4615 * | prot_klm |
4616 * ---------------------------
4617 * | BSF |
4618 * ---------------------------
4619 **/
4620 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
4621 struct mlx5_stride_block_entry *data_sentry;
4622 struct mlx5_stride_block_entry *prot_sentry;
Sagi Grimberge6631812014-02-23 14:19:11 +02004623 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
4624 int prot_size;
4625
4626 sblock_ctrl = *seg;
4627 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
4628 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
4629
4630 prot_size = prot_field_size(sig_attrs->mem.sig_type);
4631 if (!prot_size) {
4632 pr_err("Bad block size given: %u\n", block_size);
4633 return -EINVAL;
4634 }
4635 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
4636 prot_size);
4637 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
4638 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
4639 sblock_ctrl->num_entries = cpu_to_be16(2);
4640
4641 data_sentry->bcount = cpu_to_be16(block_size);
4642 data_sentry->key = cpu_to_be32(data_key);
4643 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004644 data_sentry->stride = cpu_to_be16(block_size);
4645
Sagi Grimberge6631812014-02-23 14:19:11 +02004646 prot_sentry->bcount = cpu_to_be16(prot_size);
4647 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004648 prot_sentry->va = cpu_to_be64(prot_va);
4649 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004650
Sagi Grimberge6631812014-02-23 14:19:11 +02004651 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
4652 sizeof(*prot_sentry), 64);
4653 }
4654
4655 *seg += wqe_size;
4656 *size += wqe_size / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004657 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004658
4659 bsf = *seg;
4660 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
4661 if (ret)
4662 return -EINVAL;
4663
4664 *seg += sizeof(*bsf);
4665 *size += sizeof(*bsf) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004666 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004667
4668 return 0;
4669}
4670
4671static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004672 struct ib_mr *sig_mr, int access_flags,
4673 u32 size, u32 length, u32 pdn)
Sagi Grimberge6631812014-02-23 14:19:11 +02004674{
Sagi Grimberge6631812014-02-23 14:19:11 +02004675 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004676 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004677
4678 memset(seg, 0, sizeof(*seg));
4679
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004680 seg->flags = get_umr_flags(access_flags) | MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004681 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004682 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004683 MLX5_MKEY_BSF_EN | pdn);
4684 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004685 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004686 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4687}
4688
4689static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004690 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004691{
4692 memset(umr, 0, sizeof(*umr));
4693
4694 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004695 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004696 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4697 umr->mkey_mask = sig_mkey_mask();
4698}
4699
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004700static int set_pi_umr_wr(const struct ib_send_wr *send_wr,
4701 struct mlx5_ib_qp *qp, void **seg, int *size,
4702 void **cur_edge)
4703{
4704 const struct ib_reg_wr *wr = reg_wr(send_wr);
4705 struct mlx5_ib_mr *sig_mr = to_mmr(wr->mr);
4706 struct mlx5_ib_mr *pi_mr = sig_mr->pi_mr;
4707 struct ib_sig_attrs *sig_attrs = sig_mr->ibmr.sig_attrs;
4708 u32 pdn = get_pd(qp)->pdn;
4709 u32 xlt_size;
4710 int region_len, ret;
4711
4712 if (unlikely(send_wr->num_sge != 0) ||
4713 unlikely(wr->access & IB_ACCESS_REMOTE_ATOMIC) ||
Max Gurtovoy185eddc2019-06-11 18:52:51 +03004714 unlikely(!sig_mr->sig) || unlikely(!qp->ibqp.integrity_en) ||
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004715 unlikely(!sig_mr->sig->sig_status_checked))
4716 return -EINVAL;
4717
4718 /* length of the protected region, data + protection */
4719 region_len = pi_mr->ibmr.length;
4720
4721 /**
4722 * KLM octoword size - if protection was provided
4723 * then we use strided block format (3 octowords),
4724 * else we use single KLM (1 octoword)
4725 **/
4726 if (sig_attrs->mem.sig_type != IB_SIG_TYPE_NONE)
4727 xlt_size = 0x30;
4728 else
4729 xlt_size = sizeof(struct mlx5_klm);
4730
4731 set_sig_umr_segment(*seg, xlt_size);
4732 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4733 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4734 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4735
4736 set_sig_mkey_segment(*seg, wr->mr, wr->access, xlt_size, region_len,
4737 pdn);
4738 *seg += sizeof(struct mlx5_mkey_seg);
4739 *size += sizeof(struct mlx5_mkey_seg) / 16;
4740 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4741
4742 ret = set_sig_data_segment(send_wr, wr->mr, sig_attrs, qp, seg, size,
4743 cur_edge);
4744 if (ret)
4745 return ret;
4746
4747 sig_mr->sig->sig_status_checked = false;
4748 return 0;
4749}
Sagi Grimberge6631812014-02-23 14:19:11 +02004750
Bart Van Asschef696bf62018-07-18 09:25:14 -07004751static int set_sig_umr_wr(const struct ib_send_wr *send_wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004752 struct mlx5_ib_qp *qp, void **seg, int *size,
4753 void **cur_edge)
Sagi Grimberge6631812014-02-23 14:19:11 +02004754{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004755 const struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004756 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004757 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004758 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02004759 int region_len, ret;
4760
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004761 if (unlikely(wr->wr.num_sge != 1) ||
4762 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Max Gurtovoy185eddc2019-06-11 18:52:51 +03004763 unlikely(!sig_mr->sig) || unlikely(!qp->ibqp.integrity_en) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004764 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02004765 return -EINVAL;
4766
4767 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004768 region_len = wr->wr.sg_list->length;
4769 if (wr->prot &&
4770 (wr->prot->lkey != wr->wr.sg_list->lkey ||
4771 wr->prot->addr != wr->wr.sg_list->addr ||
4772 wr->prot->length != wr->wr.sg_list->length))
4773 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02004774
4775 /**
4776 * KLM octoword size - if protection was provided
4777 * then we use strided block format (3 octowords),
4778 * else we use single KLM (1 octoword)
4779 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02004780 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02004781
Artemy Kovalyov31616252017-01-02 11:37:42 +02004782 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004783 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4784 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004785 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004786
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004787 set_sig_mkey_segment(*seg, wr->sig_mr, wr->access_flags, xlt_size,
4788 region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02004789 *seg += sizeof(struct mlx5_mkey_seg);
4790 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004791 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004792
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004793 ret = set_sig_data_segment(send_wr, wr->sig_mr, wr->sig_attrs, qp, seg,
4794 size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004795 if (ret)
4796 return ret;
4797
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004798 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004799 return 0;
4800}
4801
4802static int set_psv_wr(struct ib_sig_domain *domain,
4803 u32 psv_idx, void **seg, int *size)
4804{
4805 struct mlx5_seg_set_psv *psv_seg = *seg;
4806
4807 memset(psv_seg, 0, sizeof(*psv_seg));
4808 psv_seg->psv_num = cpu_to_be32(psv_idx);
4809 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004810 case IB_SIG_TYPE_NONE:
4811 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004812 case IB_SIG_TYPE_T10_DIF:
4813 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4814 domain->sig.dif.app_tag);
4815 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004816 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004817 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004818 pr_err("Bad signature type (%d) is given.\n",
4819 domain->sig_type);
4820 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004821 }
4822
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004823 *seg += sizeof(*psv_seg);
4824 *size += sizeof(*psv_seg) / 16;
4825
Sagi Grimberge6631812014-02-23 14:19:11 +02004826 return 0;
4827}
4828
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004829static int set_reg_wr(struct mlx5_ib_qp *qp,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004830 const struct ib_reg_wr *wr,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004831 void **seg, int *size, void **cur_edge,
4832 bool check_not_free)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004833{
4834 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4835 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004836 int mr_list_size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Idan Burstein064e5262018-05-02 13:16:39 +03004837 bool umr_inline = mr_list_size <= MLX5_IB_SQ_UMR_INLINE_THRESHOLD;
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004838 u8 flags = 0;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004839
4840 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4841 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4842 "Invalid IB_SEND_INLINE send flag\n");
4843 return -EINVAL;
4844 }
4845
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004846 if (check_not_free)
4847 flags |= MLX5_UMR_CHECK_NOT_FREE;
4848 if (umr_inline)
4849 flags |= MLX5_UMR_INLINE;
4850
4851 set_reg_umr_seg(*seg, mr, flags);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004852 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4853 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004854 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004855
4856 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4857 *seg += sizeof(struct mlx5_mkey_seg);
4858 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004859 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004860
Idan Burstein064e5262018-05-02 13:16:39 +03004861 if (umr_inline) {
Guy Levi34f4c952018-11-26 08:15:50 +02004862 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, mr->descs,
4863 mr_list_size);
4864 *size = ALIGN(*size, MLX5_SEND_WQE_BB >> 4);
Idan Burstein064e5262018-05-02 13:16:39 +03004865 } else {
4866 set_reg_data_seg(*seg, mr, pd);
4867 *seg += sizeof(struct mlx5_wqe_data_seg);
4868 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4869 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004870 return 0;
4871}
4872
Guy Levi34f4c952018-11-26 08:15:50 +02004873static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size,
4874 void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004875{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004876 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004877 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4878 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004879 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004880 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004881 *seg += sizeof(struct mlx5_mkey_seg);
4882 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004883 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03004884}
4885
Guy Levi34f4c952018-11-26 08:15:50 +02004886static void dump_wqe(struct mlx5_ib_qp *qp, u32 idx, int size_16)
Eli Cohene126ba92013-07-07 17:25:49 +03004887{
4888 __be32 *p = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004889 int i, j;
4890
Guy Levi34f4c952018-11-26 08:15:50 +02004891 pr_debug("dump WQE index %u:\n", idx);
Eli Cohene126ba92013-07-07 17:25:49 +03004892 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4893 if ((i & 0xf) == 0) {
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004894 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, idx);
Guy Levi34f4c952018-11-26 08:15:50 +02004895 pr_debug("WQBB at %p:\n", (void *)p);
Eli Cohene126ba92013-07-07 17:25:49 +03004896 j = 0;
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004897 idx = (idx + 1) & (qp->sq.wqe_cnt - 1);
Eli Cohene126ba92013-07-07 17:25:49 +03004898 }
4899 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4900 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4901 be32_to_cpu(p[j + 3]));
4902 }
4903}
4904
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004905static int __begin_wqe(struct mlx5_ib_qp *qp, void **seg,
Guy Levi34f4c952018-11-26 08:15:50 +02004906 struct mlx5_wqe_ctrl_seg **ctrl,
4907 const struct ib_send_wr *wr, unsigned int *idx,
4908 int *size, void **cur_edge, int nreq,
4909 bool send_signaled, bool solicited)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004910{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004911 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4912 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004913
4914 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
Guy Levi34f4c952018-11-26 08:15:50 +02004915 *seg = mlx5_frag_buf_get_wqe(&qp->sq.fbc, *idx);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004916 *ctrl = *seg;
4917 *(uint32_t *)(*seg + 8) = 0;
4918 (*ctrl)->imm = send_ieth(wr);
4919 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004920 (send_signaled ? MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4921 (solicited ? MLX5_WQE_CTRL_SOLICITED : 0);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004922
4923 *seg += sizeof(**ctrl);
4924 *size = sizeof(**ctrl) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004925 *cur_edge = qp->sq.cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004926
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004927 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004928}
4929
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004930static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4931 struct mlx5_wqe_ctrl_seg **ctrl,
4932 const struct ib_send_wr *wr, unsigned *idx,
Guy Levi34f4c952018-11-26 08:15:50 +02004933 int *size, void **cur_edge, int nreq)
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004934{
Guy Levi34f4c952018-11-26 08:15:50 +02004935 return __begin_wqe(qp, seg, ctrl, wr, idx, size, cur_edge, nreq,
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004936 wr->send_flags & IB_SEND_SIGNALED,
4937 wr->send_flags & IB_SEND_SOLICITED);
4938}
4939
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004940static void finish_wqe(struct mlx5_ib_qp *qp,
4941 struct mlx5_wqe_ctrl_seg *ctrl,
Guy Levi34f4c952018-11-26 08:15:50 +02004942 void *seg, u8 size, void *cur_edge,
4943 unsigned int idx, u64 wr_id, int nreq, u8 fence,
4944 u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004945{
4946 u8 opmod = 0;
4947
4948 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
4949 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02004950 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004951 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004952 if (unlikely(qp->wq_sig))
4953 ctrl->signature = wq_sig(ctrl);
4954
4955 qp->sq.wrid[idx] = wr_id;
4956 qp->sq.w_list[idx].opcode = mlx5_opcode;
4957 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
4958 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
4959 qp->sq.w_list[idx].next = qp->sq.cur_post;
Guy Levi34f4c952018-11-26 08:15:50 +02004960
4961 /* We save the edge which was possibly updated during the WQE
4962 * construction, into SQ's cache.
4963 */
4964 seg = PTR_ALIGN(seg, MLX5_SEND_WQE_BB);
4965 qp->sq.cur_edge = (unlikely(seg == cur_edge)) ?
4966 get_sq_edge(&qp->sq, qp->sq.cur_post &
4967 (qp->sq.wqe_cnt - 1)) :
4968 cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004969}
4970
Bart Van Assched34ac5c2018-07-18 09:25:32 -07004971static int _mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
4972 const struct ib_send_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03004973{
4974 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
4975 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004976 struct mlx5_core_dev *mdev = dev->mdev;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004977 struct ib_reg_wr reg_pi_wr;
Haggai Erand16e91d2016-02-29 15:45:05 +02004978 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02004979 struct mlx5_ib_mr *mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004980 struct mlx5_ib_mr *pi_mr;
4981 struct ib_sig_attrs *sig_attrs;
Eli Cohene126ba92013-07-07 17:25:49 +03004982 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02004983 struct mlx5_bf *bf;
Guy Levi34f4c952018-11-26 08:15:50 +02004984 void *cur_edge;
Eli Cohene126ba92013-07-07 17:25:49 +03004985 int uninitialized_var(size);
Eli Cohene126ba92013-07-07 17:25:49 +03004986 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03004987 unsigned idx;
4988 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004989 int num_sge;
4990 void *seg;
4991 int nreq;
4992 int i;
4993 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004994 u8 fence;
4995
Parav Pandit6c755202018-08-28 14:45:29 +03004996 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
4997 !drain)) {
4998 *bad_wr = wr;
4999 return -EIO;
5000 }
5001
Haggai Erand16e91d2016-02-29 15:45:05 +02005002 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5003 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
5004
5005 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005006 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02005007
Eli Cohene126ba92013-07-07 17:25:49 +03005008 spin_lock_irqsave(&qp->sq.lock, flags);
5009
5010 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04005011 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03005012 mlx5_ib_warn(dev, "\n");
5013 err = -EINVAL;
5014 *bad_wr = wr;
5015 goto out;
5016 }
5017
Eli Cohene126ba92013-07-07 17:25:49 +03005018 num_sge = wr->num_sge;
5019 if (unlikely(num_sge > qp->sq.max_gs)) {
5020 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03005021 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03005022 *bad_wr = wr;
5023 goto out;
5024 }
5025
Guy Levi34f4c952018-11-26 08:15:50 +02005026 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, &cur_edge,
5027 nreq);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005028 if (err) {
5029 mlx5_ib_warn(dev, "\n");
5030 err = -ENOMEM;
5031 *bad_wr = wr;
5032 goto out;
5033 }
Eli Cohene126ba92013-07-07 17:25:49 +03005034
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005035 if (wr->opcode == IB_WR_REG_MR ||
5036 wr->opcode == IB_WR_REG_MR_INTEGRITY) {
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005037 fence = dev->umr_fence;
5038 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Majd Dibbiny074fca32018-11-05 08:07:37 +02005039 } else {
5040 if (wr->send_flags & IB_SEND_FENCE) {
5041 if (qp->next_fence)
5042 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
5043 else
5044 fence = MLX5_FENCE_MODE_FENCE;
5045 } else {
5046 fence = qp->next_fence;
5047 }
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005048 }
5049
Eli Cohene126ba92013-07-07 17:25:49 +03005050 switch (ibqp->qp_type) {
5051 case IB_QPT_XRC_INI:
5052 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03005053 seg += sizeof(*xrc);
5054 size += sizeof(*xrc) / 16;
5055 /* fall through */
5056 case IB_QPT_RC:
5057 switch (wr->opcode) {
5058 case IB_WR_RDMA_READ:
5059 case IB_WR_RDMA_WRITE:
5060 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005061 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5062 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005063 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005064 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5065 break;
5066
5067 case IB_WR_ATOMIC_CMP_AND_SWP:
5068 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03005069 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03005070 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
5071 err = -ENOSYS;
5072 *bad_wr = wr;
5073 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005074
5075 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03005076 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
5077 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Guy Levi34f4c952018-11-26 08:15:50 +02005078 set_linv_wr(qp, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005079 num_sge = 0;
5080 break;
5081
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005082 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005083 qp->sq.wr_data[idx] = IB_WR_REG_MR;
5084 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
Guy Levi34f4c952018-11-26 08:15:50 +02005085 err = set_reg_wr(qp, reg_wr(wr), &seg, &size,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03005086 &cur_edge, true);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005087 if (err) {
5088 *bad_wr = wr;
5089 goto out;
5090 }
5091 num_sge = 0;
5092 break;
5093
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005094 case IB_WR_REG_MR_INTEGRITY:
5095 memset(&reg_pi_wr, 0, sizeof(struct ib_reg_wr));
5096
5097 mr = to_mmr(reg_wr(wr)->mr);
5098 pi_mr = mr->pi_mr;
5099
5100 reg_pi_wr.mr = &pi_mr->ibmr;
5101 reg_pi_wr.access = reg_wr(wr)->access;
5102 reg_pi_wr.key = pi_mr->ibmr.rkey;
5103
5104 qp->sq.wr_data[idx] = IB_WR_REG_MR_INTEGRITY;
5105 ctrl->imm = cpu_to_be32(reg_pi_wr.key);
5106 /* UMR for data + protection registration */
5107 err = set_reg_wr(qp, &reg_pi_wr, &seg, &size,
5108 &cur_edge, false);
5109 if (err) {
5110 *bad_wr = wr;
5111 goto out;
5112 }
5113 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5114 wr->wr_id, nreq, fence,
5115 MLX5_OPCODE_UMR);
5116
5117 err = begin_wqe(qp, &seg, &ctrl, wr, &idx,
5118 &size, &cur_edge, nreq);
5119 if (err) {
5120 mlx5_ib_warn(dev, "\n");
5121 err = -ENOMEM;
5122 *bad_wr = wr;
5123 goto out;
5124 }
5125 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
5126 /* UMR for sig MR */
5127 err = set_pi_umr_wr(wr, qp, &seg, &size,
5128 &cur_edge);
5129 if (err) {
5130 mlx5_ib_warn(dev, "\n");
5131 *bad_wr = wr;
5132 goto out;
5133 }
5134 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5135 wr->wr_id, nreq, fence,
5136 MLX5_OPCODE_UMR);
5137
5138 /*
5139 * SET_PSV WQEs are not signaled and solicited
5140 * on error
5141 */
5142 sig_attrs = mr->ibmr.sig_attrs;
5143 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5144 &size, &cur_edge, nreq, false,
5145 true);
5146 if (err) {
5147 mlx5_ib_warn(dev, "\n");
5148 err = -ENOMEM;
5149 *bad_wr = wr;
5150 goto out;
5151 }
5152 err = set_psv_wr(&sig_attrs->mem,
5153 mr->sig->psv_memory.psv_idx,
5154 &seg, &size);
5155 if (err) {
5156 mlx5_ib_warn(dev, "\n");
5157 *bad_wr = wr;
5158 goto out;
5159 }
5160 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5161 wr->wr_id, nreq, next_fence,
5162 MLX5_OPCODE_SET_PSV);
5163
5164 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5165 &size, &cur_edge, nreq, false,
5166 true);
5167 if (err) {
5168 mlx5_ib_warn(dev, "\n");
5169 err = -ENOMEM;
5170 *bad_wr = wr;
5171 goto out;
5172 }
5173 err = set_psv_wr(&sig_attrs->wire,
5174 mr->sig->psv_wire.psv_idx,
5175 &seg, &size);
5176 if (err) {
5177 mlx5_ib_warn(dev, "\n");
5178 *bad_wr = wr;
5179 goto out;
5180 }
5181 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5182 wr->wr_id, nreq, next_fence,
5183 MLX5_OPCODE_SET_PSV);
5184
5185 qp->next_fence =
5186 MLX5_FENCE_MODE_INITIATOR_SMALL;
5187 num_sge = 0;
5188 goto skip_psv;
5189
Sagi Grimberge6631812014-02-23 14:19:11 +02005190 case IB_WR_REG_SIG_MR:
5191 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005192 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02005193
5194 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
Guy Levi34f4c952018-11-26 08:15:50 +02005195 err = set_sig_umr_wr(wr, qp, &seg, &size,
5196 &cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02005197 if (err) {
5198 mlx5_ib_warn(dev, "\n");
5199 *bad_wr = wr;
5200 goto out;
5201 }
5202
Guy Levi34f4c952018-11-26 08:15:50 +02005203 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5204 wr->wr_id, nreq, fence,
5205 MLX5_OPCODE_UMR);
Sagi Grimberge6631812014-02-23 14:19:11 +02005206 /*
5207 * SET_PSV WQEs are not signaled and solicited
5208 * on error
5209 */
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07005210 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
Guy Levi34f4c952018-11-26 08:15:50 +02005211 &size, &cur_edge, nreq, false,
5212 true);
Sagi Grimberge6631812014-02-23 14:19:11 +02005213 if (err) {
5214 mlx5_ib_warn(dev, "\n");
5215 err = -ENOMEM;
5216 *bad_wr = wr;
5217 goto out;
5218 }
5219
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005220 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02005221 mr->sig->psv_memory.psv_idx, &seg,
5222 &size);
5223 if (err) {
5224 mlx5_ib_warn(dev, "\n");
5225 *bad_wr = wr;
5226 goto out;
5227 }
5228
Guy Levi34f4c952018-11-26 08:15:50 +02005229 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5230 wr->wr_id, nreq, fence,
5231 MLX5_OPCODE_SET_PSV);
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07005232 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
Guy Levi34f4c952018-11-26 08:15:50 +02005233 &size, &cur_edge, nreq, false,
5234 true);
Sagi Grimberge6631812014-02-23 14:19:11 +02005235 if (err) {
5236 mlx5_ib_warn(dev, "\n");
5237 err = -ENOMEM;
5238 *bad_wr = wr;
5239 goto out;
5240 }
5241
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005242 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02005243 mr->sig->psv_wire.psv_idx, &seg,
5244 &size);
5245 if (err) {
5246 mlx5_ib_warn(dev, "\n");
5247 *bad_wr = wr;
5248 goto out;
5249 }
5250
Guy Levi34f4c952018-11-26 08:15:50 +02005251 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5252 wr->wr_id, nreq, fence,
5253 MLX5_OPCODE_SET_PSV);
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005254 qp->next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Sagi Grimberge6631812014-02-23 14:19:11 +02005255 num_sge = 0;
5256 goto skip_psv;
5257
Eli Cohene126ba92013-07-07 17:25:49 +03005258 default:
5259 break;
5260 }
5261 break;
5262
5263 case IB_QPT_UC:
5264 switch (wr->opcode) {
5265 case IB_WR_RDMA_WRITE:
5266 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005267 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5268 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03005269 seg += sizeof(struct mlx5_wqe_raddr_seg);
5270 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5271 break;
5272
5273 default:
5274 break;
5275 }
5276 break;
5277
Eli Cohene126ba92013-07-07 17:25:49 +03005278 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02005279 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
5280 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
5281 err = -EPERM;
5282 *bad_wr = wr;
5283 goto out;
5284 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07005285 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02005286 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03005287 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005288 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005289 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005290 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
5291
Eli Cohene126ba92013-07-07 17:25:49 +03005292 break;
Erez Shitritf0313962016-02-21 16:27:17 +02005293 case IB_QPT_UD:
5294 set_datagram_seg(seg, wr);
5295 seg += sizeof(struct mlx5_wqe_datagram_seg);
5296 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005297 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005298
5299 /* handle qp that supports ud offload */
5300 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
5301 struct mlx5_wqe_eth_pad *pad;
5302
5303 pad = seg;
5304 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
5305 seg += sizeof(struct mlx5_wqe_eth_pad);
5306 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005307 set_eth_seg(wr, qp, &seg, &size, &cur_edge);
5308 handle_post_send_edge(&qp->sq, &seg, size,
5309 &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005310 }
5311 break;
Eli Cohene126ba92013-07-07 17:25:49 +03005312 case MLX5_IB_QPT_REG_UMR:
5313 if (wr->opcode != MLX5_IB_WR_UMR) {
5314 err = -EINVAL;
5315 mlx5_ib_warn(dev, "bad opcode\n");
5316 goto out;
5317 }
5318 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005319 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02005320 err = set_reg_umr_segment(dev, seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
5321 if (unlikely(err))
5322 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005323 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
5324 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005325 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005326 set_reg_mkey_segment(seg, wr);
5327 seg += sizeof(struct mlx5_mkey_seg);
5328 size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005329 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005330 break;
5331
5332 default:
5333 break;
5334 }
5335
5336 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
Guy Levi34f4c952018-11-26 08:15:50 +02005337 err = set_data_inl_seg(qp, wr, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005338 if (unlikely(err)) {
5339 mlx5_ib_warn(dev, "\n");
5340 *bad_wr = wr;
5341 goto out;
5342 }
Eli Cohene126ba92013-07-07 17:25:49 +03005343 } else {
Eli Cohene126ba92013-07-07 17:25:49 +03005344 for (i = 0; i < num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02005345 handle_post_send_edge(&qp->sq, &seg, size,
5346 &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005347 if (likely(wr->sg_list[i].length)) {
Guy Levi34f4c952018-11-26 08:15:50 +02005348 set_data_ptr_seg
5349 ((struct mlx5_wqe_data_seg *)seg,
5350 wr->sg_list + i);
Eli Cohene126ba92013-07-07 17:25:49 +03005351 size += sizeof(struct mlx5_wqe_data_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005352 seg += sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005353 }
5354 }
5355 }
5356
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005357 qp->next_fence = next_fence;
Guy Levi34f4c952018-11-26 08:15:50 +02005358 finish_wqe(qp, ctrl, seg, size, cur_edge, idx, wr->wr_id, nreq,
5359 fence, mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02005360skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03005361 if (0)
5362 dump_wqe(qp, idx, size);
5363 }
5364
5365out:
5366 if (likely(nreq)) {
5367 qp->sq.head += nreq;
5368
5369 /* Make sure that descriptors are written before
5370 * updating doorbell record and ringing the doorbell
5371 */
5372 wmb();
5373
5374 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
5375
Eli Cohenada388f2014-01-14 17:45:16 +02005376 /* Make sure doorbell record is visible to the HCA before
5377 * we hit doorbell */
5378 wmb();
5379
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005380 /* currently we support only regular doorbells */
Maxim Mikityanskiybbf29f62019-03-29 15:37:52 -07005381 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005382 /* Make sure doorbells don't leak out of SQ spinlock
5383 * and reach the HCA out of order.
5384 */
Eli Cohene126ba92013-07-07 17:25:49 +03005385 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03005386 }
5387
5388 spin_unlock_irqrestore(&qp->sq.lock, flags);
5389
5390 return err;
5391}
5392
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005393int mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
5394 const struct ib_send_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005395{
5396 return _mlx5_ib_post_send(ibqp, wr, bad_wr, false);
5397}
5398
Eli Cohene126ba92013-07-07 17:25:49 +03005399static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
5400{
5401 sig->signature = calc_sig(sig, size);
5402}
5403
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005404static int _mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5405 const struct ib_recv_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03005406{
5407 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5408 struct mlx5_wqe_data_seg *scat;
5409 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03005410 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5411 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005412 unsigned long flags;
5413 int err = 0;
5414 int nreq;
5415 int ind;
5416 int i;
5417
Parav Pandit6c755202018-08-28 14:45:29 +03005418 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5419 !drain)) {
5420 *bad_wr = wr;
5421 return -EIO;
5422 }
5423
Haggai Erand16e91d2016-02-29 15:45:05 +02005424 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5425 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
5426
Eli Cohene126ba92013-07-07 17:25:49 +03005427 spin_lock_irqsave(&qp->rq.lock, flags);
5428
5429 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
5430
5431 for (nreq = 0; wr; nreq++, wr = wr->next) {
5432 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
5433 err = -ENOMEM;
5434 *bad_wr = wr;
5435 goto out;
5436 }
5437
5438 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
5439 err = -EINVAL;
5440 *bad_wr = wr;
5441 goto out;
5442 }
5443
Guy Levi34f4c952018-11-26 08:15:50 +02005444 scat = mlx5_frag_buf_get_wqe(&qp->rq.fbc, ind);
Eli Cohene126ba92013-07-07 17:25:49 +03005445 if (qp->wq_sig)
5446 scat++;
5447
5448 for (i = 0; i < wr->num_sge; i++)
5449 set_data_ptr_seg(scat + i, wr->sg_list + i);
5450
5451 if (i < qp->rq.max_gs) {
5452 scat[i].byte_count = 0;
5453 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
5454 scat[i].addr = 0;
5455 }
5456
5457 if (qp->wq_sig) {
5458 sig = (struct mlx5_rwqe_sig *)scat;
5459 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
5460 }
5461
5462 qp->rq.wrid[ind] = wr->wr_id;
5463
5464 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
5465 }
5466
5467out:
5468 if (likely(nreq)) {
5469 qp->rq.head += nreq;
5470
5471 /* Make sure that descriptors are written before
5472 * doorbell record.
5473 */
5474 wmb();
5475
5476 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
5477 }
5478
5479 spin_unlock_irqrestore(&qp->rq.lock, flags);
5480
5481 return err;
5482}
5483
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005484int mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5485 const struct ib_recv_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005486{
5487 return _mlx5_ib_post_recv(ibqp, wr, bad_wr, false);
5488}
5489
Eli Cohene126ba92013-07-07 17:25:49 +03005490static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
5491{
5492 switch (mlx5_state) {
5493 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
5494 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
5495 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
5496 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
5497 case MLX5_QP_STATE_SQ_DRAINING:
5498 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
5499 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
5500 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
5501 default: return -1;
5502 }
5503}
5504
5505static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
5506{
5507 switch (mlx5_mig_state) {
5508 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
5509 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
5510 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
5511 default: return -1;
5512 }
5513}
5514
5515static int to_ib_qp_access_flags(int mlx5_flags)
5516{
5517 int ib_flags = 0;
5518
5519 if (mlx5_flags & MLX5_QP_BIT_RRE)
5520 ib_flags |= IB_ACCESS_REMOTE_READ;
5521 if (mlx5_flags & MLX5_QP_BIT_RWE)
5522 ib_flags |= IB_ACCESS_REMOTE_WRITE;
5523 if (mlx5_flags & MLX5_QP_BIT_RAE)
5524 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
5525
5526 return ib_flags;
5527}
5528
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005529static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005530 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005531 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03005532{
Eli Cohene126ba92013-07-07 17:25:49 +03005533
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005534 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03005535
Jason Gunthorpee7996a92018-01-29 13:26:40 -07005536 if (!path->port || path->port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03005537 return;
5538
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02005539 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
5540
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005541 rdma_ah_set_port_num(ah_attr, path->port);
5542 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03005543
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005544 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
5545 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
5546 rdma_ah_set_static_rate(ah_attr,
5547 path->static_rate ? path->static_rate - 5 : 0);
5548 if (path->grh_mlid & (1 << 7)) {
5549 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
5550
5551 rdma_ah_set_grh(ah_attr, NULL,
5552 tc_fl & 0xfffff,
5553 path->mgid_index,
5554 path->hop_limit,
5555 (tc_fl >> 20) & 0xff);
5556 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03005557 }
5558}
5559
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005560static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
5561 struct mlx5_ib_sq *sq,
5562 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03005563{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005564 int err;
5565
Eran Ben Elisha28160772017-12-26 15:17:05 +02005566 err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005567 if (err)
5568 goto out;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005569 sq->state = *sq_state;
5570
5571out:
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005572 return err;
5573}
5574
5575static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
5576 struct mlx5_ib_rq *rq,
5577 u8 *rq_state)
5578{
5579 void *out;
5580 void *rqc;
5581 int inlen;
5582 int err;
5583
5584 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005585 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005586 if (!out)
5587 return -ENOMEM;
5588
5589 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
5590 if (err)
5591 goto out;
5592
5593 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
5594 *rq_state = MLX5_GET(rqc, rqc, state);
5595 rq->state = *rq_state;
5596
5597out:
5598 kvfree(out);
5599 return err;
5600}
5601
5602static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
5603 struct mlx5_ib_qp *qp, u8 *qp_state)
5604{
5605 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
5606 [MLX5_RQC_STATE_RST] = {
5607 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5608 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5609 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
5610 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
5611 },
5612 [MLX5_RQC_STATE_RDY] = {
5613 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5614 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5615 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
5616 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
5617 },
5618 [MLX5_RQC_STATE_ERR] = {
5619 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5620 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5621 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
5622 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
5623 },
5624 [MLX5_RQ_STATE_NA] = {
5625 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5626 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5627 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
5628 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
5629 },
5630 };
5631
5632 *qp_state = sqrq_trans[rq_state][sq_state];
5633
5634 if (*qp_state == MLX5_QP_STATE_BAD) {
5635 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
5636 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
5637 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
5638 return -EINVAL;
5639 }
5640
5641 if (*qp_state == MLX5_QP_STATE)
5642 *qp_state = qp->state;
5643
5644 return 0;
5645}
5646
5647static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
5648 struct mlx5_ib_qp *qp,
5649 u8 *raw_packet_qp_state)
5650{
5651 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
5652 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
5653 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
5654 int err;
5655 u8 sq_state = MLX5_SQ_STATE_NA;
5656 u8 rq_state = MLX5_RQ_STATE_NA;
5657
5658 if (qp->sq.wqe_cnt) {
5659 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
5660 if (err)
5661 return err;
5662 }
5663
5664 if (qp->rq.wqe_cnt) {
5665 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
5666 if (err)
5667 return err;
5668 }
5669
5670 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
5671 raw_packet_qp_state);
5672}
5673
5674static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
5675 struct ib_qp_attr *qp_attr)
5676{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005677 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03005678 struct mlx5_qp_context *context;
5679 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005680 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03005681 int err = 0;
5682
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005683 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005684 if (!outb)
5685 return -ENOMEM;
5686
majd@mellanox.com19098df2016-01-14 19:13:03 +02005687 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005688 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03005689 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005690 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005691
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005692 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
5693 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
5694
Eli Cohene126ba92013-07-07 17:25:49 +03005695 mlx5_state = be32_to_cpu(context->flags) >> 28;
5696
5697 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03005698 qp_attr->path_mtu = context->mtu_msgmax >> 5;
5699 qp_attr->path_mig_state =
5700 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
5701 qp_attr->qkey = be32_to_cpu(context->qkey);
5702 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
5703 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
5704 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
5705 qp_attr->qp_access_flags =
5706 to_ib_qp_access_flags(be32_to_cpu(context->params2));
5707
5708 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005709 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
5710 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005711 qp_attr->alt_pkey_index =
5712 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005713 qp_attr->alt_port_num =
5714 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03005715 }
5716
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005717 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03005718 qp_attr->port_num = context->pri_path.port;
5719
5720 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
5721 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
5722
5723 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
5724
5725 qp_attr->max_dest_rd_atomic =
5726 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
5727 qp_attr->min_rnr_timer =
5728 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
5729 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
5730 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
5731 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
5732 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005733
5734out:
5735 kfree(outb);
5736 return err;
5737}
5738
Moni Shoua776a3902018-01-02 16:19:33 +02005739static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
5740 struct ib_qp_attr *qp_attr, int qp_attr_mask,
5741 struct ib_qp_init_attr *qp_init_attr)
5742{
5743 struct mlx5_core_dct *dct = &mqp->dct.mdct;
5744 u32 *out;
5745 u32 access_flags = 0;
5746 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
5747 void *dctc;
5748 int err;
5749 int supported_mask = IB_QP_STATE |
5750 IB_QP_ACCESS_FLAGS |
5751 IB_QP_PORT |
5752 IB_QP_MIN_RNR_TIMER |
5753 IB_QP_AV |
5754 IB_QP_PATH_MTU |
5755 IB_QP_PKEY_INDEX;
5756
5757 if (qp_attr_mask & ~supported_mask)
5758 return -EINVAL;
5759 if (mqp->state != IB_QPS_RTR)
5760 return -EINVAL;
5761
5762 out = kzalloc(outlen, GFP_KERNEL);
5763 if (!out)
5764 return -ENOMEM;
5765
5766 err = mlx5_core_dct_query(dev->mdev, dct, out, outlen);
5767 if (err)
5768 goto out;
5769
5770 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
5771
5772 if (qp_attr_mask & IB_QP_STATE)
5773 qp_attr->qp_state = IB_QPS_RTR;
5774
5775 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
5776 if (MLX5_GET(dctc, dctc, rre))
5777 access_flags |= IB_ACCESS_REMOTE_READ;
5778 if (MLX5_GET(dctc, dctc, rwe))
5779 access_flags |= IB_ACCESS_REMOTE_WRITE;
5780 if (MLX5_GET(dctc, dctc, rae))
5781 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
5782 qp_attr->qp_access_flags = access_flags;
5783 }
5784
5785 if (qp_attr_mask & IB_QP_PORT)
5786 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
5787 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
5788 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
5789 if (qp_attr_mask & IB_QP_AV) {
5790 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
5791 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
5792 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
5793 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
5794 }
5795 if (qp_attr_mask & IB_QP_PATH_MTU)
5796 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
5797 if (qp_attr_mask & IB_QP_PKEY_INDEX)
5798 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
5799out:
5800 kfree(out);
5801 return err;
5802}
5803
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005804int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5805 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
5806{
5807 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5808 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5809 int err = 0;
5810 u8 raw_packet_qp_state;
5811
Yishai Hadas28d61372016-05-23 15:20:56 +03005812 if (ibqp->rwq_ind_tbl)
5813 return -ENOSYS;
5814
Haggai Erand16e91d2016-02-29 15:45:05 +02005815 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5816 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
5817 qp_init_attr);
5818
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005819 /* Not all of output fields are applicable, make sure to zero them */
5820 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5821 memset(qp_attr, 0, sizeof(*qp_attr));
5822
Moni Shoua776a3902018-01-02 16:19:33 +02005823 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
5824 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
5825 qp_attr_mask, qp_init_attr);
5826
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005827 mutex_lock(&qp->mutex);
5828
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005829 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
5830 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005831 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
5832 if (err)
5833 goto out;
5834 qp->state = raw_packet_qp_state;
5835 qp_attr->port_num = 1;
5836 } else {
5837 err = query_qp_attr(dev, qp, qp_attr);
5838 if (err)
5839 goto out;
5840 }
5841
5842 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03005843 qp_attr->cur_qp_state = qp_attr->qp_state;
5844 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
5845 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
5846
5847 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005848 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005849 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005850 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005851 } else {
5852 qp_attr->cap.max_send_wr = 0;
5853 qp_attr->cap.max_send_sge = 0;
5854 }
5855
Noa Osherovich0540d812016-06-04 15:15:32 +03005856 qp_init_attr->qp_type = ibqp->qp_type;
5857 qp_init_attr->recv_cq = ibqp->recv_cq;
5858 qp_init_attr->send_cq = ibqp->send_cq;
5859 qp_init_attr->srq = ibqp->srq;
5860 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005861
5862 qp_init_attr->cap = qp_attr->cap;
5863
5864 qp_init_attr->create_flags = 0;
5865 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
5866 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
5867
Leon Romanovsky051f2632015-12-20 12:16:11 +02005868 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
5869 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
5870 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
5871 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
5872 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
5873 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02005874 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
5875 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02005876
Eli Cohene126ba92013-07-07 17:25:49 +03005877 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5878 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5879
Eli Cohene126ba92013-07-07 17:25:49 +03005880out:
5881 mutex_unlock(&qp->mutex);
5882 return err;
5883}
5884
5885struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03005886 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005887{
5888 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5889 struct mlx5_ib_xrcd *xrcd;
5890 int err;
5891
Saeed Mahameed938fe832015-05-28 22:28:41 +03005892 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005893 return ERR_PTR(-ENOSYS);
5894
5895 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5896 if (!xrcd)
5897 return ERR_PTR(-ENOMEM);
5898
Yishai Hadas5aa37712018-11-26 08:28:38 +02005899 err = mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03005900 if (err) {
5901 kfree(xrcd);
5902 return ERR_PTR(-ENOMEM);
5903 }
5904
5905 return &xrcd->ibxrcd;
5906}
5907
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005908int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005909{
5910 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5911 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
5912 int err;
5913
Yishai Hadas5aa37712018-11-26 08:28:38 +02005914 err = mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, 0);
Leon Romanovskyb0818082018-01-28 11:25:30 +02005915 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03005916 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005917
5918 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03005919 return 0;
5920}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005921
Yishai Hadas350d0e42016-08-28 14:58:18 +03005922static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5923{
5924 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5925 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5926 struct ib_event event;
5927
5928 if (rwq->ibwq.event_handler) {
5929 event.device = rwq->ibwq.device;
5930 event.element.wq = &rwq->ibwq;
5931 switch (type) {
5932 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5933 event.event = IB_EVENT_WQ_FATAL;
5934 break;
5935 default:
5936 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5937 return;
5938 }
5939
5940 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5941 }
5942}
5943
Maor Gottlieb03404e82017-05-30 10:29:13 +03005944static int set_delay_drop(struct mlx5_ib_dev *dev)
5945{
5946 int err = 0;
5947
5948 mutex_lock(&dev->delay_drop.lock);
5949 if (dev->delay_drop.activate)
5950 goto out;
5951
5952 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
5953 if (err)
5954 goto out;
5955
5956 dev->delay_drop.activate = true;
5957out:
5958 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005959
5960 if (!err)
5961 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005962 return err;
5963}
5964
Yishai Hadas79b20a62016-05-23 15:20:50 +03005965static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5966 struct ib_wq_init_attr *init_attr)
5967{
5968 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005969 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005970 __be64 *rq_pas0;
5971 void *in;
5972 void *rqc;
5973 void *wq;
5974 int inlen;
5975 int err;
5976
5977 dev = to_mdev(pd->device);
5978
5979 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005980 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005981 if (!in)
5982 return -ENOMEM;
5983
Yishai Hadas34d57582018-09-20 21:39:21 +03005984 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005985 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
5986 MLX5_SET(rqc, rqc, mem_rq_type,
5987 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
5988 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
5989 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
5990 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
5991 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
5992 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03005993 MLX5_SET(wq, wq, wq_type,
5994 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
5995 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005996 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5997 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
5998 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
5999 err = -EOPNOTSUPP;
6000 goto out;
6001 } else {
6002 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
6003 }
6004 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006005 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03006006 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
6007 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
6008 MLX5_SET(wq, wq, log_wqe_stride_size,
6009 rwq->single_stride_log_num_of_bytes -
6010 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
6011 MLX5_SET(wq, wq, log_wqe_num_of_strides, rwq->log_num_strides -
6012 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES);
6013 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006014 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
6015 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
6016 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
6017 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
6018 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
6019 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02006020 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006021 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02006022 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006023 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
6024 err = -EOPNOTSUPP;
6025 goto out;
6026 }
6027 } else {
6028 MLX5_SET(rqc, rqc, vsd, 1);
6029 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02006030 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
6031 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
6032 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
6033 err = -EOPNOTSUPP;
6034 goto out;
6035 }
6036 MLX5_SET(rqc, rqc, scatter_fcs, 1);
6037 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03006038 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
6039 if (!(dev->ib_dev.attrs.raw_packet_caps &
6040 IB_RAW_PACKET_CAP_DELAY_DROP)) {
6041 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
6042 err = -EOPNOTSUPP;
6043 goto out;
6044 }
6045 MLX5_SET(rqc, rqc, delay_drop_en, 1);
6046 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006047 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
6048 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03006049 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03006050 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
6051 err = set_delay_drop(dev);
6052 if (err) {
6053 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
6054 err);
6055 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
6056 } else {
6057 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
6058 }
6059 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006060out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03006061 kvfree(in);
6062 return err;
6063}
6064
6065static int set_user_rq_size(struct mlx5_ib_dev *dev,
6066 struct ib_wq_init_attr *wq_init_attr,
6067 struct mlx5_ib_create_wq *ucmd,
6068 struct mlx5_ib_rwq *rwq)
6069{
6070 /* Sanity check RQ size before proceeding */
6071 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
6072 return -EINVAL;
6073
6074 if (!ucmd->rq_wqe_count)
6075 return -EINVAL;
6076
6077 rwq->wqe_count = ucmd->rq_wqe_count;
6078 rwq->wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky0dfe4522018-08-01 14:25:41 -07006079 if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
6080 return -EINVAL;
6081
Yishai Hadas79b20a62016-05-23 15:20:50 +03006082 rwq->log_rq_stride = rwq->wqe_shift;
6083 rwq->log_rq_size = ilog2(rwq->wqe_count);
6084 return 0;
6085}
6086
6087static int prepare_user_rq(struct ib_pd *pd,
6088 struct ib_wq_init_attr *init_attr,
6089 struct ib_udata *udata,
6090 struct mlx5_ib_rwq *rwq)
6091{
6092 struct mlx5_ib_dev *dev = to_mdev(pd->device);
6093 struct mlx5_ib_create_wq ucmd = {};
6094 int err;
6095 size_t required_cmd_sz;
6096
Noa Osherovichccc87082017-10-17 18:01:13 +03006097 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
6098 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006099 if (udata->inlen < required_cmd_sz) {
6100 mlx5_ib_dbg(dev, "invalid inlen\n");
6101 return -EINVAL;
6102 }
6103
6104 if (udata->inlen > sizeof(ucmd) &&
6105 !ib_is_udata_cleared(udata, sizeof(ucmd),
6106 udata->inlen - sizeof(ucmd))) {
6107 mlx5_ib_dbg(dev, "inlen is not supported\n");
6108 return -EOPNOTSUPP;
6109 }
6110
6111 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
6112 mlx5_ib_dbg(dev, "copy failed\n");
6113 return -EFAULT;
6114 }
6115
Noa Osherovichccc87082017-10-17 18:01:13 +03006116 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03006117 mlx5_ib_dbg(dev, "invalid comp mask\n");
6118 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03006119 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
6120 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
6121 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
6122 return -EOPNOTSUPP;
6123 }
6124 if ((ucmd.single_stride_log_num_of_bytes <
6125 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
6126 (ucmd.single_stride_log_num_of_bytes >
6127 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
6128 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
6129 ucmd.single_stride_log_num_of_bytes,
6130 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
6131 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
6132 return -EINVAL;
6133 }
6134 if ((ucmd.single_wqe_log_num_of_strides >
6135 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
6136 (ucmd.single_wqe_log_num_of_strides <
6137 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES)) {
6138 mlx5_ib_dbg(dev, "Invalid log num strides (%u. Range is %u - %u)\n",
6139 ucmd.single_wqe_log_num_of_strides,
6140 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
6141 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
6142 return -EINVAL;
6143 }
6144 rwq->single_stride_log_num_of_bytes =
6145 ucmd.single_stride_log_num_of_bytes;
6146 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
6147 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
6148 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006149 }
6150
6151 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
6152 if (err) {
6153 mlx5_ib_dbg(dev, "err %d\n", err);
6154 return err;
6155 }
6156
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02006157 err = create_user_rq(dev, pd, udata, rwq, &ucmd);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006158 if (err) {
6159 mlx5_ib_dbg(dev, "err %d\n", err);
Gal Pressman645ba592018-10-08 19:44:03 +03006160 return err;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006161 }
6162
6163 rwq->user_index = ucmd.user_index;
6164 return 0;
6165}
6166
6167struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
6168 struct ib_wq_init_attr *init_attr,
6169 struct ib_udata *udata)
6170{
6171 struct mlx5_ib_dev *dev;
6172 struct mlx5_ib_rwq *rwq;
6173 struct mlx5_ib_create_wq_resp resp = {};
6174 size_t min_resp_len;
6175 int err;
6176
6177 if (!udata)
6178 return ERR_PTR(-ENOSYS);
6179
6180 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6181 if (udata->outlen && udata->outlen < min_resp_len)
6182 return ERR_PTR(-EINVAL);
6183
6184 dev = to_mdev(pd->device);
6185 switch (init_attr->wq_type) {
6186 case IB_WQT_RQ:
6187 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
6188 if (!rwq)
6189 return ERR_PTR(-ENOMEM);
6190 err = prepare_user_rq(pd, init_attr, udata, rwq);
6191 if (err)
6192 goto err;
6193 err = create_rq(rwq, pd, init_attr);
6194 if (err)
6195 goto err_user_rq;
6196 break;
6197 default:
6198 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
6199 init_attr->wq_type);
6200 return ERR_PTR(-EINVAL);
6201 }
6202
Yishai Hadas350d0e42016-08-28 14:58:18 +03006203 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006204 rwq->ibwq.state = IB_WQS_RESET;
6205 if (udata->outlen) {
6206 resp.response_length = offsetof(typeof(resp), response_length) +
6207 sizeof(resp.response_length);
6208 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6209 if (err)
6210 goto err_copy;
6211 }
6212
Yishai Hadas350d0e42016-08-28 14:58:18 +03006213 rwq->core_qp.event = mlx5_ib_wq_event;
6214 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006215 return &rwq->ibwq;
6216
6217err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03006218 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006219err_user_rq:
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006220 destroy_user_rq(dev, pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006221err:
6222 kfree(rwq);
6223 return ERR_PTR(err);
6224}
6225
Leon Romanovskya49b1dc2019-06-12 15:27:41 +03006226void mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +03006227{
6228 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6229 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6230
Yishai Hadas350d0e42016-08-28 14:58:18 +03006231 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006232 destroy_user_rq(dev, wq->pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006233 kfree(rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006234}
6235
Yishai Hadasc5f90922016-05-23 15:20:53 +03006236struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
6237 struct ib_rwq_ind_table_init_attr *init_attr,
6238 struct ib_udata *udata)
6239{
6240 struct mlx5_ib_dev *dev = to_mdev(device);
6241 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
6242 int sz = 1 << init_attr->log_ind_tbl_size;
6243 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
6244 size_t min_resp_len;
6245 int inlen;
6246 int err;
6247 int i;
6248 u32 *in;
6249 void *rqtc;
6250
6251 if (udata->inlen > 0 &&
6252 !ib_is_udata_cleared(udata, 0,
6253 udata->inlen))
6254 return ERR_PTR(-EOPNOTSUPP);
6255
Maor Gottliebefd7f402016-10-27 16:36:40 +03006256 if (init_attr->log_ind_tbl_size >
6257 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
6258 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
6259 init_attr->log_ind_tbl_size,
6260 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
6261 return ERR_PTR(-EINVAL);
6262 }
6263
Yishai Hadasc5f90922016-05-23 15:20:53 +03006264 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6265 if (udata->outlen && udata->outlen < min_resp_len)
6266 return ERR_PTR(-EINVAL);
6267
6268 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
6269 if (!rwq_ind_tbl)
6270 return ERR_PTR(-ENOMEM);
6271
6272 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006273 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006274 if (!in) {
6275 err = -ENOMEM;
6276 goto err;
6277 }
6278
6279 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
6280
6281 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
6282 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
6283
6284 for (i = 0; i < sz; i++)
6285 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
6286
Yishai Hadas5deba862018-09-20 21:39:28 +03006287 rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
6288 MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);
6289
Yishai Hadasc5f90922016-05-23 15:20:53 +03006290 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
6291 kvfree(in);
6292
6293 if (err)
6294 goto err;
6295
6296 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
6297 if (udata->outlen) {
6298 resp.response_length = offsetof(typeof(resp), response_length) +
6299 sizeof(resp.response_length);
6300 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6301 if (err)
6302 goto err_copy;
6303 }
6304
6305 return &rwq_ind_tbl->ib_rwq_ind_tbl;
6306
6307err_copy:
Yishai Hadas5deba862018-09-20 21:39:28 +03006308 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006309err:
6310 kfree(rwq_ind_tbl);
6311 return ERR_PTR(err);
6312}
6313
6314int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
6315{
6316 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
6317 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
6318
Yishai Hadas5deba862018-09-20 21:39:28 +03006319 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006320
6321 kfree(rwq_ind_tbl);
6322 return 0;
6323}
6324
Yishai Hadas79b20a62016-05-23 15:20:50 +03006325int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
6326 u32 wq_attr_mask, struct ib_udata *udata)
6327{
6328 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6329 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6330 struct mlx5_ib_modify_wq ucmd = {};
6331 size_t required_cmd_sz;
6332 int curr_wq_state;
6333 int wq_state;
6334 int inlen;
6335 int err;
6336 void *rqc;
6337 void *in;
6338
6339 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
6340 if (udata->inlen < required_cmd_sz)
6341 return -EINVAL;
6342
6343 if (udata->inlen > sizeof(ucmd) &&
6344 !ib_is_udata_cleared(udata, sizeof(ucmd),
6345 udata->inlen - sizeof(ucmd)))
6346 return -EOPNOTSUPP;
6347
6348 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
6349 return -EFAULT;
6350
6351 if (ucmd.comp_mask || ucmd.reserved)
6352 return -EOPNOTSUPP;
6353
6354 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006355 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006356 if (!in)
6357 return -ENOMEM;
6358
6359 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
6360
6361 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
6362 wq_attr->curr_wq_state : wq->state;
6363 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
6364 wq_attr->wq_state : curr_wq_state;
6365 if (curr_wq_state == IB_WQS_ERR)
6366 curr_wq_state = MLX5_RQC_STATE_ERR;
6367 if (wq_state == IB_WQS_ERR)
6368 wq_state = MLX5_RQC_STATE_ERR;
6369 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
Yishai Hadas34d57582018-09-20 21:39:21 +03006370 MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006371 MLX5_SET(rqc, rqc, state, wq_state);
6372
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006373 if (wq_attr_mask & IB_WQ_FLAGS) {
6374 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
6375 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
6376 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
6377 mlx5_ib_dbg(dev, "VLAN offloads are not "
6378 "supported\n");
6379 err = -EOPNOTSUPP;
6380 goto out;
6381 }
6382 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6383 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
6384 MLX5_SET(rqc, rqc, vsd,
6385 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
6386 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02006387
6388 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6389 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
6390 err = -EOPNOTSUPP;
6391 goto out;
6392 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006393 }
6394
Majd Dibbiny23a69642017-01-18 15:25:10 +02006395 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
6396 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
6397 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6398 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03006399 MLX5_SET(rqc, rqc, counter_set_id,
6400 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006401 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06006402 dev_info_once(
6403 &dev->ib_dev.dev,
6404 "Receive WQ counters are not supported on current FW\n");
Majd Dibbiny23a69642017-01-18 15:25:10 +02006405 }
6406
Yishai Hadas350d0e42016-08-28 14:58:18 +03006407 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006408 if (!err)
6409 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
6410
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006411out:
6412 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006413 return err;
6414}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006415
6416struct mlx5_ib_drain_cqe {
6417 struct ib_cqe cqe;
6418 struct completion done;
6419};
6420
6421static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
6422{
6423 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
6424 struct mlx5_ib_drain_cqe,
6425 cqe);
6426
6427 complete(&cqe->done);
6428}
6429
6430/* This function returns only once the drained WR was completed */
6431static void handle_drain_completion(struct ib_cq *cq,
6432 struct mlx5_ib_drain_cqe *sdrain,
6433 struct mlx5_ib_dev *dev)
6434{
6435 struct mlx5_core_dev *mdev = dev->mdev;
6436
6437 if (cq->poll_ctx == IB_POLL_DIRECT) {
6438 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
6439 ib_process_cq_direct(cq, -1);
6440 return;
6441 }
6442
6443 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6444 struct mlx5_ib_cq *mcq = to_mcq(cq);
6445 bool triggered = false;
6446 unsigned long flags;
6447
6448 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
6449 /* Make sure that the CQ handler won't run if wasn't run yet */
6450 if (!mcq->mcq.reset_notify_added)
6451 mcq->mcq.reset_notify_added = 1;
6452 else
6453 triggered = true;
6454 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
6455
6456 if (triggered) {
6457 /* Wait for any scheduled/running task to be ended */
6458 switch (cq->poll_ctx) {
6459 case IB_POLL_SOFTIRQ:
6460 irq_poll_disable(&cq->iop);
6461 irq_poll_enable(&cq->iop);
6462 break;
6463 case IB_POLL_WORKQUEUE:
6464 cancel_work_sync(&cq->work);
6465 break;
6466 default:
6467 WARN_ON_ONCE(1);
6468 }
6469 }
6470
6471 /* Run the CQ handler - this makes sure that the drain WR will
6472 * be processed if wasn't processed yet.
6473 */
6474 mcq->mcq.comp(&mcq->mcq);
6475 }
6476
6477 wait_for_completion(&sdrain->done);
6478}
6479
6480void mlx5_ib_drain_sq(struct ib_qp *qp)
6481{
6482 struct ib_cq *cq = qp->send_cq;
6483 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6484 struct mlx5_ib_drain_cqe sdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006485 const struct ib_send_wr *bad_swr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006486 struct ib_rdma_wr swr = {
6487 .wr = {
6488 .next = NULL,
6489 { .wr_cqe = &sdrain.cqe, },
6490 .opcode = IB_WR_RDMA_WRITE,
6491 },
6492 };
6493 int ret;
6494 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6495 struct mlx5_core_dev *mdev = dev->mdev;
6496
6497 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6498 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6499 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6500 return;
6501 }
6502
6503 sdrain.cqe.done = mlx5_ib_drain_qp_done;
6504 init_completion(&sdrain.done);
6505
6506 ret = _mlx5_ib_post_send(qp, &swr.wr, &bad_swr, true);
6507 if (ret) {
6508 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6509 return;
6510 }
6511
6512 handle_drain_completion(cq, &sdrain, dev);
6513}
6514
6515void mlx5_ib_drain_rq(struct ib_qp *qp)
6516{
6517 struct ib_cq *cq = qp->recv_cq;
6518 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6519 struct mlx5_ib_drain_cqe rdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006520 struct ib_recv_wr rwr = {};
6521 const struct ib_recv_wr *bad_rwr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006522 int ret;
6523 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6524 struct mlx5_core_dev *mdev = dev->mdev;
6525
6526 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6527 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6528 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6529 return;
6530 }
6531
6532 rwr.wr_cqe = &rdrain.cqe;
6533 rdrain.cqe.done = mlx5_ib_drain_qp_done;
6534 init_completion(&rdrain.done);
6535
6536 ret = _mlx5_ib_post_recv(qp, &rwr, &bad_rwr, true);
6537 if (ret) {
6538 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6539 return;
6540 }
6541
6542 handle_drain_completion(cq, &rdrain, dev);
6543}