blob: 2210759843ba88e3e9033d0dd2572a2ab31c1839 [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/module.h>
34#include <rdma/ib_umem.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020035#include <rdma/ib_cache.h>
Haggai Abramovskycfb5e082016-01-14 19:12:57 +020036#include <rdma/ib_user_verbs.h>
Mark Zhangd14133d2019-07-02 13:02:36 +030037#include <rdma/rdma_counter.h>
Yishai Hadasc2e53b22017-06-08 16:15:08 +030038#include <linux/mlx5/fs.h>
Eli Cohene126ba92013-07-07 17:25:49 +030039#include "mlx5_ib.h"
Mark Blochb96c9dd2018-01-29 10:40:37 +000040#include "ib_rep.h"
Yishai Hadas443c1cf2018-09-20 21:39:26 +030041#include "cmd.h"
Eli Cohene126ba92013-07-07 17:25:49 +030042
43/* not supported currently */
44static int wq_signature;
45
46enum {
47 MLX5_IB_ACK_REQ_FREQ = 8,
48};
49
50enum {
51 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
52 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
53 MLX5_IB_LINK_TYPE_IB = 0,
54 MLX5_IB_LINK_TYPE_ETH = 1
55};
56
57enum {
58 MLX5_IB_SQ_STRIDE = 6,
Idan Burstein064e5262018-05-02 13:16:39 +030059 MLX5_IB_SQ_UMR_INLINE_THRESHOLD = 64,
Eli Cohene126ba92013-07-07 17:25:49 +030060};
61
62static const u32 mlx5_ib_opcode[] = {
63 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020064 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030065 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
66 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
67 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
68 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
69 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
70 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
71 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
72 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030073 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030074 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
75 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
76 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
77};
78
Erez Shitritf0313962016-02-21 16:27:17 +020079struct mlx5_wqe_eth_pad {
80 u8 rsvd0[16];
81};
Eli Cohene126ba92013-07-07 17:25:49 +030082
Alex Veskereb49ab02016-08-28 12:25:53 +030083enum raw_qp_set_mask_map {
84 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020085 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030086};
87
Alex Vesker0680efa2016-08-28 12:25:52 +030088struct mlx5_modify_raw_qp_param {
89 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030090
91 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang61147f32018-03-19 15:10:30 +020092
93 struct mlx5_rate_limit rl;
94
Alex Veskereb49ab02016-08-28 12:25:53 +030095 u8 rq_q_ctr_id;
Mark Blochd5ed8ac2019-03-28 15:27:38 +020096 u16 port;
Alex Vesker0680efa2016-08-28 12:25:52 +030097};
98
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030099static void get_cqs(enum ib_qp_type qp_type,
100 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
101 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
102
Eli Cohene126ba92013-07-07 17:25:49 +0300103static int is_qp0(enum ib_qp_type qp_type)
104{
105 return qp_type == IB_QPT_SMI;
106}
107
Eli Cohene126ba92013-07-07 17:25:49 +0300108static int is_sqp(enum ib_qp_type qp_type)
109{
110 return is_qp0(qp_type) || is_qp1(qp_type);
111}
112
Haggai Eranc1395a22014-12-11 17:04:14 +0200113/**
Moni Shouafbeb4072019-01-22 08:48:46 +0200114 * mlx5_ib_read_user_wqe_common() - Copy a WQE (or part of) from user WQ
115 * to kernel buffer
Haggai Eranc1395a22014-12-11 17:04:14 +0200116 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200117 * @umem: User space memory where the WQ is
118 * @buffer: buffer to copy to
119 * @buflen: buffer length
120 * @wqe_index: index of WQE to copy from
121 * @wq_offset: offset to start of WQ
122 * @wq_wqe_cnt: number of WQEs in WQ
123 * @wq_wqe_shift: log2 of WQE size
124 * @bcnt: number of bytes to copy
125 * @bytes_copied: number of bytes to copy (return value)
Haggai Eranc1395a22014-12-11 17:04:14 +0200126 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200127 * Copies from start of WQE bcnt or less bytes.
128 * Does not gurantee to copy the entire WQE.
Haggai Eranc1395a22014-12-11 17:04:14 +0200129 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200130 * Return: zero on success, or an error code.
Haggai Eranc1395a22014-12-11 17:04:14 +0200131 */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200132static int mlx5_ib_read_user_wqe_common(struct ib_umem *umem, void *buffer,
133 size_t buflen, int wqe_index,
134 int wq_offset, int wq_wqe_cnt,
135 int wq_wqe_shift, int bcnt,
Moni Shouafbeb4072019-01-22 08:48:46 +0200136 size_t *bytes_copied)
Haggai Eranc1395a22014-12-11 17:04:14 +0200137{
Moni Shouafbeb4072019-01-22 08:48:46 +0200138 size_t offset = wq_offset + ((wqe_index % wq_wqe_cnt) << wq_wqe_shift);
139 size_t wq_end = wq_offset + (wq_wqe_cnt << wq_wqe_shift);
140 size_t copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200141 int ret;
142
Moni Shouafbeb4072019-01-22 08:48:46 +0200143 /* don't copy more than requested, more than buffer length or
144 * beyond WQ end
145 */
146 copy_length = min_t(u32, buflen, wq_end - offset);
147 copy_length = min_t(u32, copy_length, bcnt);
Haggai Eranc1395a22014-12-11 17:04:14 +0200148
Moni Shouafbeb4072019-01-22 08:48:46 +0200149 ret = ib_umem_copy_from(buffer, umem, offset, copy_length);
Haggai Eranc1395a22014-12-11 17:04:14 +0200150 if (ret)
151 return ret;
152
Moni Shouafbeb4072019-01-22 08:48:46 +0200153 if (!ret && bytes_copied)
154 *bytes_copied = copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200155
Moni Shouafbeb4072019-01-22 08:48:46 +0200156 return 0;
157}
Haggai Eranc1395a22014-12-11 17:04:14 +0200158
Moni Shouada9ee9d2020-01-15 14:43:34 +0200159static int mlx5_ib_read_kernel_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index,
160 void *buffer, size_t buflen, size_t *bc)
161{
162 struct mlx5_wqe_ctrl_seg *ctrl;
163 size_t bytes_copied = 0;
164 size_t wqe_length;
165 void *p;
166 int ds;
167
168 wqe_index = wqe_index & qp->sq.fbc.sz_m1;
169
170 /* read the control segment first */
171 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, wqe_index);
172 ctrl = p;
173 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
174 wqe_length = ds * MLX5_WQE_DS_UNITS;
175
176 /* read rest of WQE if it spreads over more than one stride */
177 while (bytes_copied < wqe_length) {
178 size_t copy_length =
179 min_t(size_t, buflen - bytes_copied, MLX5_SEND_WQE_BB);
180
181 if (!copy_length)
182 break;
183
184 memcpy(buffer + bytes_copied, p, copy_length);
185 bytes_copied += copy_length;
186
187 wqe_index = (wqe_index + 1) & qp->sq.fbc.sz_m1;
188 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, wqe_index);
189 }
190 *bc = bytes_copied;
191 return 0;
192}
193
194static int mlx5_ib_read_user_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index,
195 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200196{
197 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
198 struct ib_umem *umem = base->ubuffer.umem;
199 struct mlx5_ib_wq *wq = &qp->sq;
200 struct mlx5_wqe_ctrl_seg *ctrl;
201 size_t bytes_copied;
202 size_t bytes_copied2;
203 size_t wqe_length;
204 int ret;
205 int ds;
Haggai Eranc1395a22014-12-11 17:04:14 +0200206
Moni Shouafbeb4072019-01-22 08:48:46 +0200207 /* at first read as much as possible */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200208 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index,
209 wq->offset, wq->wqe_cnt,
210 wq->wqe_shift, buflen,
Moni Shouafbeb4072019-01-22 08:48:46 +0200211 &bytes_copied);
Haggai Eranc1395a22014-12-11 17:04:14 +0200212 if (ret)
213 return ret;
214
Moni Shouafbeb4072019-01-22 08:48:46 +0200215 /* we need at least control segment size to proceed */
216 if (bytes_copied < sizeof(*ctrl))
217 return -EINVAL;
218
219 ctrl = buffer;
220 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
221 wqe_length = ds * MLX5_WQE_DS_UNITS;
222
223 /* if we copied enough then we are done */
224 if (bytes_copied >= wqe_length) {
225 *bc = bytes_copied;
226 return 0;
227 }
228
229 /* otherwise this a wrapped around wqe
230 * so read the remaining bytes starting
231 * from wqe_index 0
232 */
Moni Shouada9ee9d2020-01-15 14:43:34 +0200233 ret = mlx5_ib_read_user_wqe_common(umem, buffer + bytes_copied,
234 buflen - bytes_copied, 0, wq->offset,
235 wq->wqe_cnt, wq->wqe_shift,
Moni Shouafbeb4072019-01-22 08:48:46 +0200236 wqe_length - bytes_copied,
237 &bytes_copied2);
238
239 if (ret)
240 return ret;
241 *bc = bytes_copied + bytes_copied2;
242 return 0;
243}
244
Moni Shouada9ee9d2020-01-15 14:43:34 +0200245int mlx5_ib_read_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
246 size_t buflen, size_t *bc)
247{
248 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
249 struct ib_umem *umem = base->ubuffer.umem;
250
251 if (buflen < sizeof(struct mlx5_wqe_ctrl_seg))
252 return -EINVAL;
253
254 if (!umem)
255 return mlx5_ib_read_kernel_wqe_sq(qp, wqe_index, buffer,
256 buflen, bc);
257
258 return mlx5_ib_read_user_wqe_sq(qp, wqe_index, buffer, buflen, bc);
259}
260
261static int mlx5_ib_read_user_wqe_rq(struct mlx5_ib_qp *qp, int wqe_index,
262 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200263{
264 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
265 struct ib_umem *umem = base->ubuffer.umem;
266 struct mlx5_ib_wq *wq = &qp->rq;
267 size_t bytes_copied;
268 int ret;
269
Moni Shouada9ee9d2020-01-15 14:43:34 +0200270 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index,
271 wq->offset, wq->wqe_cnt,
272 wq->wqe_shift, buflen,
Moni Shouafbeb4072019-01-22 08:48:46 +0200273 &bytes_copied);
274
275 if (ret)
276 return ret;
277 *bc = bytes_copied;
278 return 0;
279}
280
Moni Shouada9ee9d2020-01-15 14:43:34 +0200281int mlx5_ib_read_wqe_rq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
282 size_t buflen, size_t *bc)
283{
284 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
285 struct ib_umem *umem = base->ubuffer.umem;
286 struct mlx5_ib_wq *wq = &qp->rq;
287 size_t wqe_size = 1 << wq->wqe_shift;
288
289 if (buflen < wqe_size)
290 return -EINVAL;
291
292 if (!umem)
293 return -EOPNOTSUPP;
294
295 return mlx5_ib_read_user_wqe_rq(qp, wqe_index, buffer, buflen, bc);
296}
297
298static int mlx5_ib_read_user_wqe_srq(struct mlx5_ib_srq *srq, int wqe_index,
299 void *buffer, size_t buflen, size_t *bc)
Moni Shouafbeb4072019-01-22 08:48:46 +0200300{
301 struct ib_umem *umem = srq->umem;
302 size_t bytes_copied;
303 int ret;
304
Moni Shouada9ee9d2020-01-15 14:43:34 +0200305 ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index, 0,
306 srq->msrq.max, srq->msrq.wqe_shift,
307 buflen, &bytes_copied);
Moni Shouafbeb4072019-01-22 08:48:46 +0200308
309 if (ret)
310 return ret;
311 *bc = bytes_copied;
312 return 0;
Haggai Eranc1395a22014-12-11 17:04:14 +0200313}
314
Moni Shouada9ee9d2020-01-15 14:43:34 +0200315int mlx5_ib_read_wqe_srq(struct mlx5_ib_srq *srq, int wqe_index, void *buffer,
316 size_t buflen, size_t *bc)
317{
318 struct ib_umem *umem = srq->umem;
319 size_t wqe_size = 1 << srq->msrq.wqe_shift;
320
321 if (buflen < wqe_size)
322 return -EINVAL;
323
324 if (!umem)
325 return -EOPNOTSUPP;
326
327 return mlx5_ib_read_user_wqe_srq(srq, wqe_index, buffer, buflen, bc);
328}
329
Eli Cohene126ba92013-07-07 17:25:49 +0300330static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
331{
332 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
333 struct ib_event event;
334
majd@mellanox.com19098df2016-01-14 19:13:03 +0200335 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
336 /* This event is only valid for trans_qps */
337 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
338 }
Eli Cohene126ba92013-07-07 17:25:49 +0300339
340 if (ibqp->event_handler) {
341 event.device = ibqp->device;
342 event.element.qp = ibqp;
343 switch (type) {
344 case MLX5_EVENT_TYPE_PATH_MIG:
345 event.event = IB_EVENT_PATH_MIG;
346 break;
347 case MLX5_EVENT_TYPE_COMM_EST:
348 event.event = IB_EVENT_COMM_EST;
349 break;
350 case MLX5_EVENT_TYPE_SQ_DRAINED:
351 event.event = IB_EVENT_SQ_DRAINED;
352 break;
353 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
354 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
355 break;
356 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
357 event.event = IB_EVENT_QP_FATAL;
358 break;
359 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
360 event.event = IB_EVENT_PATH_MIG_ERR;
361 break;
362 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
363 event.event = IB_EVENT_QP_REQ_ERR;
364 break;
365 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
366 event.event = IB_EVENT_QP_ACCESS_ERR;
367 break;
368 default:
369 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
370 return;
371 }
372
373 ibqp->event_handler(&event, ibqp->qp_context);
374 }
375}
376
377static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
378 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
379{
380 int wqe_size;
381 int wq_size;
382
383 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300384 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300385 return -EINVAL;
386
387 if (!has_rq) {
388 qp->rq.max_gs = 0;
389 qp->rq.wqe_cnt = 0;
390 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300391 cap->max_recv_wr = 0;
392 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300393 } else {
394 if (ucmd) {
395 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300396 if (ucmd->rq_wqe_shift > BITS_PER_BYTE * sizeof(ucmd->rq_wqe_shift))
397 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300398 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300399 if ((1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) < qp->wq_sig)
400 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300401 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
402 qp->rq.max_post = qp->rq.wqe_cnt;
403 } else {
404 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
405 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
406 wqe_size = roundup_pow_of_two(wqe_size);
407 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
408 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
409 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300410 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300411 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
412 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300413 MLX5_CAP_GEN(dev->mdev,
414 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300415 return -EINVAL;
416 }
417 qp->rq.wqe_shift = ilog2(wqe_size);
418 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
419 qp->rq.max_post = qp->rq.wqe_cnt;
420 }
421 }
422
423 return 0;
424}
425
Erez Shitritf0313962016-02-21 16:27:17 +0200426static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300427{
Andi Shyti618af382013-07-16 15:35:01 +0200428 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300429
Erez Shitritf0313962016-02-21 16:27:17 +0200430 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300431 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300432 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300433 /* fall through */
434 case IB_QPT_RC:
435 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200436 max(sizeof(struct mlx5_wqe_atomic_seg) +
437 sizeof(struct mlx5_wqe_raddr_seg),
438 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
Idan Burstein064e5262018-05-02 13:16:39 +0300439 sizeof(struct mlx5_mkey_seg) +
440 MLX5_IB_SQ_UMR_INLINE_THRESHOLD /
441 MLX5_IB_UMR_OCTOWORD);
Eli Cohene126ba92013-07-07 17:25:49 +0300442 break;
443
Eli Cohenb125a542013-09-11 16:35:22 +0300444 case IB_QPT_XRC_TGT:
445 return 0;
446
Eli Cohene126ba92013-07-07 17:25:49 +0300447 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300448 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200449 max(sizeof(struct mlx5_wqe_raddr_seg),
450 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
451 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300452 break;
453
454 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200455 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
456 size += sizeof(struct mlx5_wqe_eth_pad) +
457 sizeof(struct mlx5_wqe_eth_seg);
458 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300459 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200460 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300461 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300462 sizeof(struct mlx5_wqe_datagram_seg);
463 break;
464
465 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300466 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300467 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
468 sizeof(struct mlx5_mkey_seg);
469 break;
470
471 default:
472 return -EINVAL;
473 }
474
475 return size;
476}
477
478static int calc_send_wqe(struct ib_qp_init_attr *attr)
479{
480 int inl_size = 0;
481 int size;
482
Erez Shitritf0313962016-02-21 16:27:17 +0200483 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300484 if (size < 0)
485 return size;
486
487 if (attr->cap.max_inline_data) {
488 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
489 attr->cap.max_inline_data;
490 }
491
492 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300493 if (attr->create_flags & IB_QP_CREATE_INTEGRITY_EN &&
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200494 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300495 return MLX5_SIG_WQE_SIZE;
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200496 else
497 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300498}
499
Eli Cohen288c01b2016-10-27 16:36:45 +0300500static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
501{
502 int max_sge;
503
504 if (attr->qp_type == IB_QPT_RC)
505 max_sge = (min_t(int, wqe_size, 512) -
506 sizeof(struct mlx5_wqe_ctrl_seg) -
507 sizeof(struct mlx5_wqe_raddr_seg)) /
508 sizeof(struct mlx5_wqe_data_seg);
509 else if (attr->qp_type == IB_QPT_XRC_INI)
510 max_sge = (min_t(int, wqe_size, 512) -
511 sizeof(struct mlx5_wqe_ctrl_seg) -
512 sizeof(struct mlx5_wqe_xrc_seg) -
513 sizeof(struct mlx5_wqe_raddr_seg)) /
514 sizeof(struct mlx5_wqe_data_seg);
515 else
516 max_sge = (wqe_size - sq_overhead(attr)) /
517 sizeof(struct mlx5_wqe_data_seg);
518
519 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
520 sizeof(struct mlx5_wqe_data_seg));
521}
522
Eli Cohene126ba92013-07-07 17:25:49 +0300523static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
524 struct mlx5_ib_qp *qp)
525{
526 int wqe_size;
527 int wq_size;
528
529 if (!attr->cap.max_send_wr)
530 return 0;
531
532 wqe_size = calc_send_wqe(attr);
533 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
534 if (wqe_size < 0)
535 return wqe_size;
536
Saeed Mahameed938fe832015-05-28 22:28:41 +0300537 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300538 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300539 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300540 return -EINVAL;
541 }
542
Erez Shitritf0313962016-02-21 16:27:17 +0200543 qp->max_inline_data = wqe_size - sq_overhead(attr) -
544 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300545 attr->cap.max_inline_data = qp->max_inline_data;
546
547 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
548 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300549 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800550 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
551 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300552 qp->sq.wqe_cnt,
553 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300554 return -ENOMEM;
555 }
Eli Cohene126ba92013-07-07 17:25:49 +0300556 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300557 qp->sq.max_gs = get_send_sge(attr, wqe_size);
558 if (qp->sq.max_gs < attr->cap.max_send_sge)
559 return -ENOMEM;
560
561 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300562 qp->sq.max_post = wq_size / wqe_size;
563 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300564
565 return wq_size;
566}
567
568static int set_user_buf_size(struct mlx5_ib_dev *dev,
569 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200570 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200571 struct mlx5_ib_qp_base *base,
572 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300573{
574 int desc_sz = 1 << qp->sq.wqe_shift;
575
Saeed Mahameed938fe832015-05-28 22:28:41 +0300576 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300577 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300578 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300579 return -EINVAL;
580 }
581
Gal Pressmanaf8b38e2019-02-06 15:45:35 +0200582 if (ucmd->sq_wqe_count && !is_power_of_2(ucmd->sq_wqe_count)) {
583 mlx5_ib_warn(dev, "sq_wqe_count %d is not a power of two\n",
584 ucmd->sq_wqe_count);
Eli Cohene126ba92013-07-07 17:25:49 +0300585 return -EINVAL;
586 }
587
588 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
589
Saeed Mahameed938fe832015-05-28 22:28:41 +0300590 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300591 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300592 qp->sq.wqe_cnt,
593 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300594 return -EINVAL;
595 }
596
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300597 if (attr->qp_type == IB_QPT_RAW_PACKET ||
598 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200599 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
600 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
601 } else {
602 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
603 (qp->sq.wqe_cnt << 6);
604 }
Eli Cohene126ba92013-07-07 17:25:49 +0300605
606 return 0;
607}
608
609static int qp_has_rq(struct ib_qp_init_attr *attr)
610{
611 if (attr->qp_type == IB_QPT_XRC_INI ||
612 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
613 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
614 !attr->cap.max_recv_wr)
615 return 0;
616
617 return 1;
618}
619
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200620enum {
621 /* this is the first blue flame register in the array of bfregs assigned
622 * to a processes. Since we do not use it for blue flame but rather
623 * regular 64 bit doorbells, we do not need a lock for maintaiing
624 * "odd/even" order
625 */
626 NUM_NON_BLUE_FLAME_BFREGS = 1,
627};
628
Eli Cohenb037c292017-01-03 23:55:26 +0200629static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
630{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200631 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200632}
633
634static int num_med_bfreg(struct mlx5_ib_dev *dev,
635 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200636{
637 int n;
638
Eli Cohenb037c292017-01-03 23:55:26 +0200639 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
640 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200641
642 return n >= 0 ? n : 0;
643}
644
Yishai Hadas18b03622018-05-07 10:20:01 +0300645static int first_med_bfreg(struct mlx5_ib_dev *dev,
646 struct mlx5_bfreg_info *bfregi)
647{
648 return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
649}
650
Eli Cohenb037c292017-01-03 23:55:26 +0200651static int first_hi_bfreg(struct mlx5_ib_dev *dev,
652 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200653{
654 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200655
Eli Cohenb037c292017-01-03 23:55:26 +0200656 med = num_med_bfreg(dev, bfregi);
657 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200658}
659
Eli Cohenb037c292017-01-03 23:55:26 +0200660static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
661 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300662{
Eli Cohene126ba92013-07-07 17:25:49 +0300663 int i;
664
Eli Cohenb037c292017-01-03 23:55:26 +0200665 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
666 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200667 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300668 return i;
669 }
670 }
671
672 return -ENOMEM;
673}
674
Eli Cohenb037c292017-01-03 23:55:26 +0200675static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
676 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300677{
Yishai Hadas18b03622018-05-07 10:20:01 +0300678 int minidx = first_med_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300679 int i;
680
Yishai Hadas18b03622018-05-07 10:20:01 +0300681 if (minidx < 0)
682 return minidx;
683
684 for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200685 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300686 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200687 if (!bfregi->count[minidx])
688 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300689 }
690
Eli Cohen2f5ff262017-01-03 23:55:21 +0200691 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300692 return minidx;
693}
694
Eli Cohenb037c292017-01-03 23:55:26 +0200695static int alloc_bfreg(struct mlx5_ib_dev *dev,
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300696 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300697{
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300698 int bfregn = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +0300699
Yishai Hadas0a2fd012020-03-24 08:01:43 +0200700 if (bfregi->lib_uar_dyn)
701 return -EINVAL;
702
Eli Cohen2f5ff262017-01-03 23:55:21 +0200703 mutex_lock(&bfregi->lock);
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300704 if (bfregi->ver >= 2) {
705 bfregn = alloc_high_class_bfreg(dev, bfregi);
706 if (bfregn < 0)
707 bfregn = alloc_med_class_bfreg(dev, bfregi);
708 }
709
710 if (bfregn < 0) {
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200711 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200712 bfregn = 0;
713 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300714 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200715 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300716
Eli Cohen2f5ff262017-01-03 23:55:21 +0200717 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300718}
719
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200720void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300721{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200722 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200723 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200724 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300725}
726
727static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
728{
729 switch (state) {
730 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
731 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
732 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
733 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
734 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
735 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
736 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
737 default: return -1;
738 }
739}
740
741static int to_mlx5_st(enum ib_qp_type type)
742{
743 switch (type) {
744 case IB_QPT_RC: return MLX5_QP_ST_RC;
745 case IB_QPT_UC: return MLX5_QP_ST_UC;
746 case IB_QPT_UD: return MLX5_QP_ST_UD;
747 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
748 case IB_QPT_XRC_INI:
749 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
750 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200751 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200752 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300753 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300754 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200755 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300756 case IB_QPT_MAX:
757 default: return -EINVAL;
758 }
759}
760
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300761static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
762 struct mlx5_ib_cq *recv_cq);
763static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
764 struct mlx5_ib_cq *recv_cq);
765
Yishai Hadas7c043e92018-06-17 13:00:03 +0300766int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300767 struct mlx5_bfreg_info *bfregi, u32 bfregn,
Yishai Hadas7c043e92018-06-17 13:00:03 +0300768 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300769{
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300770 unsigned int bfregs_per_sys_page;
771 u32 index_of_sys_page;
772 u32 offset;
Eli Cohenb037c292017-01-03 23:55:26 +0200773
Yishai Hadas0a2fd012020-03-24 08:01:43 +0200774 if (bfregi->lib_uar_dyn)
775 return -EINVAL;
776
Eli Cohenb037c292017-01-03 23:55:26 +0200777 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
778 MLX5_NON_FP_BFREGS_PER_UAR;
779 index_of_sys_page = bfregn / bfregs_per_sys_page;
780
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200781 if (dyn_bfreg) {
782 index_of_sys_page += bfregi->num_static_sys_pages;
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300783
784 if (index_of_sys_page >= bfregi->num_sys_pages)
785 return -EINVAL;
786
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200787 if (bfregn > bfregi->num_dyn_bfregs ||
788 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
789 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
790 return -EINVAL;
791 }
792 }
Eli Cohenb037c292017-01-03 23:55:26 +0200793
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200794 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200795 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300796}
797
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200798static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200799 unsigned long addr, size_t size,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200800 struct ib_umem **umem, int *npages, int *page_shift,
801 int *ncont, u32 *offset)
majd@mellanox.com19098df2016-01-14 19:13:03 +0200802{
803 int err;
804
Moni Shouac320e522020-01-15 14:43:31 +0200805 *umem = ib_umem_get(&dev->ib_dev, addr, size, 0);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200806 if (IS_ERR(*umem)) {
807 mlx5_ib_dbg(dev, "umem_get failed\n");
808 return PTR_ERR(*umem);
809 }
810
Majd Dibbiny762f8992016-10-27 16:36:47 +0300811 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200812
813 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
814 if (err) {
815 mlx5_ib_warn(dev, "bad offset\n");
816 goto err_umem;
817 }
818
819 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
820 addr, size, *npages, *page_shift, *ncont, *offset);
821
822 return 0;
823
824err_umem:
825 ib_umem_release(*umem);
826 *umem = NULL;
827
828 return err;
829}
830
Maor Gottliebfe248c32017-05-30 10:29:14 +0300831static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300832 struct mlx5_ib_rwq *rwq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300833{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300834 struct mlx5_ib_ucontext *context =
835 rdma_udata_to_drv_context(
836 udata,
837 struct mlx5_ib_ucontext,
838 ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300839
Maor Gottliebfe248c32017-05-30 10:29:14 +0300840 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
841 atomic_dec(&dev->delay_drop.rqs_cnt);
842
Yishai Hadas79b20a62016-05-23 15:20:50 +0300843 mlx5_ib_db_unmap_user(context, &rwq->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300844 ib_umem_release(rwq->umem);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300845}
846
847static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200848 struct ib_udata *udata, struct mlx5_ib_rwq *rwq,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300849 struct mlx5_ib_create_wq *ucmd)
850{
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200851 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
852 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300853 int page_shift = 0;
854 int npages;
855 u32 offset = 0;
856 int ncont = 0;
857 int err;
858
859 if (!ucmd->buf_addr)
860 return -EINVAL;
861
Moni Shouac320e522020-01-15 14:43:31 +0200862 rwq->umem = ib_umem_get(&dev->ib_dev, ucmd->buf_addr, rwq->buf_size, 0);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300863 if (IS_ERR(rwq->umem)) {
864 mlx5_ib_dbg(dev, "umem_get failed\n");
865 err = PTR_ERR(rwq->umem);
866 return err;
867 }
868
Majd Dibbiny762f8992016-10-27 16:36:47 +0300869 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300870 &ncont, NULL);
871 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
872 &rwq->rq_page_offset);
873 if (err) {
874 mlx5_ib_warn(dev, "bad offset\n");
875 goto err_umem;
876 }
877
878 rwq->rq_num_pas = ncont;
879 rwq->page_shift = page_shift;
880 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
881 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
882
883 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
884 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
885 npages, page_shift, ncont, offset);
886
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200887 err = mlx5_ib_db_map_user(ucontext, udata, ucmd->db_addr, &rwq->db);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300888 if (err) {
889 mlx5_ib_dbg(dev, "map failed\n");
890 goto err_umem;
891 }
892
893 rwq->create_type = MLX5_WQ_USER;
894 return 0;
895
896err_umem:
897 ib_umem_release(rwq->umem);
898 return err;
899}
900
Eli Cohenb037c292017-01-03 23:55:26 +0200901static int adjust_bfregn(struct mlx5_ib_dev *dev,
902 struct mlx5_bfreg_info *bfregi, int bfregn)
903{
904 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
905 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
906}
907
Eli Cohene126ba92013-07-07 17:25:49 +0300908static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
909 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200910 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300911 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200912 struct mlx5_ib_create_qp_resp *resp, int *inlen,
913 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300914{
915 struct mlx5_ib_ucontext *context;
916 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200917 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200918 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200919 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300920 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200921 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200922 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200923 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300924 __be64 *pas;
925 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300926 int err;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200927 u16 uid;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200928 u32 uar_flags;
Eli Cohene126ba92013-07-07 17:25:49 +0300929
930 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
931 if (err) {
932 mlx5_ib_dbg(dev, "copy failed\n");
933 return err;
934 }
935
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200936 context = rdma_udata_to_drv_context(udata, struct mlx5_ib_ucontext,
937 ibucontext);
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200938 uar_flags = ucmd.flags & (MLX5_QP_FLAG_UAR_PAGE_INDEX |
939 MLX5_QP_FLAG_BFREG_INDEX);
940 switch (uar_flags) {
941 case MLX5_QP_FLAG_UAR_PAGE_INDEX:
942 uar_index = ucmd.bfreg_index;
943 bfregn = MLX5_IB_INVALID_BFREG;
944 break;
945 case MLX5_QP_FLAG_BFREG_INDEX:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200946 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
947 ucmd.bfreg_index, true);
948 if (uar_index < 0)
949 return uar_index;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200950 bfregn = MLX5_IB_INVALID_BFREG;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200951 break;
952 case 0:
953 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
954 return -EINVAL;
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300955 bfregn = alloc_bfreg(dev, &context->bfregi);
956 if (bfregn < 0)
957 return bfregn;
Yishai Hadasac42a5e2020-03-24 08:01:41 +0200958 break;
959 default:
960 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300961 }
962
Eli Cohen2f5ff262017-01-03 23:55:21 +0200963 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200964 if (bfregn != MLX5_IB_INVALID_BFREG)
965 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
966 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300967
Haggai Eran48fea832014-05-22 14:50:11 +0300968 qp->rq.offset = 0;
969 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
970 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
971
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200972 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300973 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200974 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300975
majd@mellanox.com19098df2016-01-14 19:13:03 +0200976 if (ucmd.buf_addr && ubuffer->buf_size) {
977 ubuffer->buf_addr = ucmd.buf_addr;
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200978 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr,
979 ubuffer->buf_size, &ubuffer->umem,
980 &npages, &page_shift, &ncont, &offset);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200981 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200982 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200983 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200984 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300985 }
Eli Cohene126ba92013-07-07 17:25:49 +0300986
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300987 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
988 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300989 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300990 if (!*in) {
991 err = -ENOMEM;
992 goto err_umem;
993 }
Eli Cohene126ba92013-07-07 17:25:49 +0300994
Yishai Hadas7422edc2018-12-23 13:12:21 +0200995 uid = (attr->qp_type != IB_QPT_XRC_TGT &&
996 attr->qp_type != IB_QPT_XRC_INI) ? to_mpd(pd)->uid : 0;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200997 MLX5_SET(create_qp_in, *in, uid, uid);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300998 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
999 if (ubuffer->umem)
1000 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
1001
1002 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1003
1004 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1005 MLX5_SET(qpc, qpc, page_offset, offset);
1006
1007 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001008 if (bfregn != MLX5_IB_INVALID_BFREG)
1009 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
1010 else
1011 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001012 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +03001013
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001014 err = mlx5_ib_db_map_user(context, udata, ucmd.db_addr, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001015 if (err) {
1016 mlx5_ib_dbg(dev, "map failed\n");
1017 goto err_free;
1018 }
1019
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001020 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
Eli Cohene126ba92013-07-07 17:25:49 +03001021 if (err) {
1022 mlx5_ib_dbg(dev, "copy failed\n");
1023 goto err_unmap;
1024 }
1025 qp->create_type = MLX5_QP_USER;
1026
1027 return 0;
1028
1029err_unmap:
1030 mlx5_ib_db_unmap_user(context, &qp->db);
1031
1032err_free:
Al Viro479163f2014-11-20 08:13:57 +00001033 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001034
1035err_umem:
Leon Romanovsky836a0fb2019-06-16 15:05:20 +03001036 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +03001037
Eli Cohen2f5ff262017-01-03 23:55:21 +02001038err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001039 if (bfregn != MLX5_IB_INVALID_BFREG)
1040 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001041 return err;
1042}
1043
Eli Cohenb037c292017-01-03 23:55:26 +02001044static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03001045 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base,
1046 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03001047{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03001048 struct mlx5_ib_ucontext *context =
1049 rdma_udata_to_drv_context(
1050 udata,
1051 struct mlx5_ib_ucontext,
1052 ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03001053
Eli Cohene126ba92013-07-07 17:25:49 +03001054 mlx5_ib_db_unmap_user(context, &qp->db);
Leon Romanovsky836a0fb2019-06-16 15:05:20 +03001055 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001056
1057 /*
1058 * Free only the BFREGs which are handled by the kernel.
1059 * BFREGs of UARs allocated dynamically are handled by user.
1060 */
1061 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
1062 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001063}
1064
Guy Levi34f4c952018-11-26 08:15:50 +02001065/* get_sq_edge - Get the next nearby edge.
1066 *
1067 * An 'edge' is defined as the first following address after the end
1068 * of the fragment or the SQ. Accordingly, during the WQE construction
1069 * which repetitively increases the pointer to write the next data, it
1070 * simply should check if it gets to an edge.
1071 *
1072 * @sq - SQ buffer.
1073 * @idx - Stride index in the SQ buffer.
1074 *
1075 * Return:
1076 * The new edge.
1077 */
1078static void *get_sq_edge(struct mlx5_ib_wq *sq, u32 idx)
1079{
1080 void *fragment_end;
1081
1082 fragment_end = mlx5_frag_buf_get_wqe
1083 (&sq->fbc,
1084 mlx5_frag_buf_get_idx_last_contig_stride(&sq->fbc, idx));
1085
1086 return fragment_end + MLX5_SEND_WQE_BB;
1087}
1088
Eli Cohene126ba92013-07-07 17:25:49 +03001089static int create_kernel_qp(struct mlx5_ib_dev *dev,
1090 struct ib_qp_init_attr *init_attr,
1091 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001092 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +02001093 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +03001094{
Eli Cohene126ba92013-07-07 17:25:49 +03001095 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001096 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +03001097 int err;
1098
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +03001099 if (init_attr->create_flags & ~(IB_QP_CREATE_INTEGRITY_EN |
Erez Shitritf0313962016-02-21 16:27:17 +02001100 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +02001101 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +03001102 IB_QP_CREATE_NETIF_QP |
Michael Guralnik11f552e2019-06-10 15:21:24 +03001103 MLX5_IB_QP_CREATE_SQPN_QP1 |
1104 MLX5_IB_QP_CREATE_WC_TEST))
Eli Cohen1a4c3a32014-02-06 17:41:25 +02001105 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001106
1107 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001108 qp->bf.bfreg = &dev->fp_bfreg;
Michael Guralnik11f552e2019-06-10 15:21:24 +03001109 else if (init_attr->create_flags & MLX5_IB_QP_CREATE_WC_TEST)
1110 qp->bf.bfreg = &dev->wc_bfreg;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001111 else
1112 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +03001113
Eli Cohend8030b02017-02-09 19:31:47 +02001114 /* We need to divide by two since each register is comprised of
1115 * two buffers of identical size, namely odd and even
1116 */
1117 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001118 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +03001119
1120 err = calc_sq_size(dev, init_attr, qp);
1121 if (err < 0) {
1122 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001123 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001124 }
1125
1126 qp->rq.offset = 0;
1127 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001128 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +03001129
Guy Levi34f4c952018-11-26 08:15:50 +02001130 err = mlx5_frag_buf_alloc_node(dev->mdev, base->ubuffer.buf_size,
1131 &qp->buf, dev->mdev->priv.numa_node);
Eli Cohene126ba92013-07-07 17:25:49 +03001132 if (err) {
1133 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001134 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001135 }
1136
Guy Levi34f4c952018-11-26 08:15:50 +02001137 if (qp->rq.wqe_cnt)
1138 mlx5_init_fbc(qp->buf.frags, qp->rq.wqe_shift,
1139 ilog2(qp->rq.wqe_cnt), &qp->rq.fbc);
1140
1141 if (qp->sq.wqe_cnt) {
1142 int sq_strides_offset = (qp->sq.offset & (PAGE_SIZE - 1)) /
1143 MLX5_SEND_WQE_BB;
1144 mlx5_init_fbc_offset(qp->buf.frags +
1145 (qp->sq.offset / PAGE_SIZE),
1146 ilog2(MLX5_SEND_WQE_BB),
1147 ilog2(qp->sq.wqe_cnt),
1148 sq_strides_offset, &qp->sq.fbc);
1149
1150 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
1151 }
1152
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001153 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
1154 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001155 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001156 if (!*in) {
1157 err = -ENOMEM;
1158 goto err_buf;
1159 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001160
1161 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1162 MLX5_SET(qpc, qpc, uar_page, uar_index);
1163 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1164
Eli Cohene126ba92013-07-07 17:25:49 +03001165 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001166 MLX5_SET(qpc, qpc, fre, 1);
1167 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001168
Michael Guralnik3f89b012019-10-20 09:43:59 +03001169 if (init_attr->create_flags & MLX5_IB_QP_CREATE_SQPN_QP1) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001170 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +02001171 qp->flags |= MLX5_IB_QP_SQPN_QP1;
1172 }
1173
Guy Levi34f4c952018-11-26 08:15:50 +02001174 mlx5_fill_page_frag_array(&qp->buf,
1175 (__be64 *)MLX5_ADDR_OF(create_qp_in,
1176 *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +03001177
Jack Morgenstein9603b612014-07-28 23:30:22 +03001178 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001179 if (err) {
1180 mlx5_ib_dbg(dev, "err %d\n", err);
1181 goto err_free;
1182 }
1183
Li Dongyangb5883002017-08-16 23:31:22 +10001184 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
1185 sizeof(*qp->sq.wrid), GFP_KERNEL);
1186 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1187 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1188 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1189 sizeof(*qp->rq.wrid), GFP_KERNEL);
1190 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1191 sizeof(*qp->sq.w_list), GFP_KERNEL);
1192 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1193 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001194
1195 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1196 !qp->sq.w_list || !qp->sq.wqe_head) {
1197 err = -ENOMEM;
1198 goto err_wrid;
1199 }
1200 qp->create_type = MLX5_QP_KERNEL;
1201
1202 return 0;
1203
1204err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001205 kvfree(qp->sq.wqe_head);
1206 kvfree(qp->sq.w_list);
1207 kvfree(qp->sq.wrid);
1208 kvfree(qp->sq.wr_data);
1209 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001210 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001211
1212err_free:
Al Viro479163f2014-11-20 08:13:57 +00001213 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001214
1215err_buf:
Guy Levi34f4c952018-11-26 08:15:50 +02001216 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001217 return err;
1218}
1219
1220static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1221{
Li Dongyangb5883002017-08-16 23:31:22 +10001222 kvfree(qp->sq.wqe_head);
1223 kvfree(qp->sq.w_list);
1224 kvfree(qp->sq.wrid);
1225 kvfree(qp->sq.wr_data);
1226 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001227 mlx5_db_free(dev->mdev, &qp->db);
Guy Levi34f4c952018-11-26 08:15:50 +02001228 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001229}
1230
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001231static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001232{
1233 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Moni Shouac32a4f22018-01-02 16:19:32 +02001234 (attr->qp_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001235 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001236 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001237 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001238 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001239 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001240 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001241}
1242
1243static int is_connected(enum ib_qp_type qp_type)
1244{
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001245 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC ||
1246 qp_type == MLX5_IB_QPT_DCI)
Eli Cohene126ba92013-07-07 17:25:49 +03001247 return 1;
1248
1249 return 0;
1250}
1251
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001252static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001253 struct mlx5_ib_qp *qp,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001254 struct mlx5_ib_sq *sq, u32 tdn,
1255 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001256{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001257 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001258 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1259
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001260 MLX5_SET(create_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001261 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001262 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1263 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1264
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001265 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1266}
1267
1268static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001269 struct mlx5_ib_sq *sq, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001270{
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001271 mlx5_cmd_destroy_tis(dev->mdev, sq->tisn, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001272}
1273
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001274static void destroy_flow_rule_vport_sq(struct mlx5_ib_sq *sq)
Mark Blochb96c9dd2018-01-29 10:40:37 +00001275{
1276 if (sq->flow_rule)
1277 mlx5_del_flow_rules(sq->flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001278 sq->flow_rule = NULL;
Mark Blochb96c9dd2018-01-29 10:40:37 +00001279}
1280
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001281static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001282 struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001283 struct mlx5_ib_sq *sq, void *qpin,
1284 struct ib_pd *pd)
1285{
1286 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1287 __be64 *pas;
1288 void *in;
1289 void *sqc;
1290 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1291 void *wq;
1292 int inlen;
1293 int err;
1294 int page_shift = 0;
1295 int npages;
1296 int ncont = 0;
1297 u32 offset = 0;
1298
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001299 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr, ubuffer->buf_size,
1300 &sq->ubuffer.umem, &npages, &page_shift, &ncont,
1301 &offset);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001302 if (err)
1303 return err;
1304
1305 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001306 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001307 if (!in) {
1308 err = -ENOMEM;
1309 goto err_umem;
1310 }
1311
Yishai Hadasc14003f2018-09-20 21:39:22 +03001312 MLX5_SET(create_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001313 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1314 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001315 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1316 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001317 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1318 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1319 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1320 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1321 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001322 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1323 MLX5_CAP_ETH(dev->mdev, swp))
1324 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001325
1326 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1327 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1328 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1329 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1330 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1331 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1332 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1333 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1334 MLX5_SET(wq, wq, page_offset, offset);
1335
1336 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1337 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1338
1339 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1340
1341 kvfree(in);
1342
1343 if (err)
1344 goto err_umem;
1345
1346 return 0;
1347
1348err_umem:
1349 ib_umem_release(sq->ubuffer.umem);
1350 sq->ubuffer.umem = NULL;
1351
1352 return err;
1353}
1354
1355static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1356 struct mlx5_ib_sq *sq)
1357{
Mark Blochd5ed8ac2019-03-28 15:27:38 +02001358 destroy_flow_rule_vport_sq(sq);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001359 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1360 ib_umem_release(sq->ubuffer.umem);
1361}
1362
Boris Pismenny2c292db2018-03-08 15:51:40 +02001363static size_t get_rq_pas_size(void *qpc)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001364{
1365 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1366 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1367 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1368 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1369 u32 po_quanta = 1 << (log_page_size - 6);
1370 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1371 u32 page_size = 1 << log_page_size;
1372 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1373 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1374
1375 return rq_num_pas * sizeof(u64);
1376}
1377
1378static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001379 struct mlx5_ib_rq *rq, void *qpin,
Yishai Hadas34d57582018-09-20 21:39:21 +03001380 size_t qpinlen, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001381{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001382 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001383 __be64 *pas;
1384 __be64 *qp_pas;
1385 void *in;
1386 void *rqc;
1387 void *wq;
1388 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001389 size_t rq_pas_size = get_rq_pas_size(qpc);
1390 size_t inlen;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001391 int err;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001392
1393 if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
1394 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001395
1396 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001397 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001398 if (!in)
1399 return -ENOMEM;
1400
Yishai Hadas34d57582018-09-20 21:39:21 +03001401 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001402 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001403 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1404 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001405 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1406 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1407 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1408 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1409 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1410
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001411 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1412 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1413
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001414 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1415 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001416 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1417 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001418 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1419 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1420 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1421 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1422 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1423 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1424
1425 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1426 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1427 memcpy(pas, qp_pas, rq_pas_size);
1428
1429 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1430
1431 kvfree(in);
1432
1433 return err;
1434}
1435
1436static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1437 struct mlx5_ib_rq *rq)
1438{
1439 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1440}
1441
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001442static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1443{
1444 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1445 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1446 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1447}
1448
Mark Bloch0042f9e2018-09-17 13:30:49 +03001449static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1450 struct mlx5_ib_rq *rq,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001451 u32 qp_flags_en,
1452 struct ib_pd *pd)
Mark Bloch0042f9e2018-09-17 13:30:49 +03001453{
1454 if (qp_flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1455 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1456 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001457 mlx5_cmd_destroy_tir(dev->mdev, rq->tirn, to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001458}
1459
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001460static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001461 struct mlx5_ib_rq *rq, u32 tdn,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001462 u32 *qp_flags_en,
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001463 struct ib_pd *pd,
1464 u32 *out, int outlen)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001465{
Mark Bloch175edba2018-09-17 13:30:48 +03001466 u8 lb_flag = 0;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001467 u32 *in;
1468 void *tirc;
1469 int inlen;
1470 int err;
1471
1472 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001473 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001474 if (!in)
1475 return -ENOMEM;
1476
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001477 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001478 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1479 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1480 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1481 MLX5_SET(tirc, tirc, transport_domain, tdn);
Mark Bloch175edba2018-09-17 13:30:48 +03001482 if (*qp_flags_en & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001483 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001484
Mark Bloch175edba2018-09-17 13:30:48 +03001485 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
1486 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1487
1488 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
1489 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1490
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001491 if (dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001492 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1493 *qp_flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1494 }
1495
1496 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
Mark Blochec9c2fb2018-01-15 13:11:37 +00001497
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001498 err = mlx5_core_create_tir_out(dev->mdev, in, inlen, out, outlen);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001499
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001500 rq->tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001501 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1502 err = mlx5_ib_enable_lb(dev, false, true);
1503
1504 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001505 destroy_raw_packet_qp_tir(dev, rq, 0, pd);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001506 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001507 kvfree(in);
1508
1509 return err;
1510}
1511
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001512static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001513 u32 *in, size_t inlen,
Yishai Hadas7f720522018-09-20 21:45:18 +03001514 struct ib_pd *pd,
1515 struct ib_udata *udata,
1516 struct mlx5_ib_create_qp_resp *resp)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001517{
1518 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1519 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1520 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001521 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1522 udata, struct mlx5_ib_ucontext, ibucontext);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001523 int err;
1524 u32 tdn = mucontext->tdn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001525 u16 uid = to_mpd(pd)->uid;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001526 u32 out[MLX5_ST_SZ_DW(create_tir_out)] = {};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001527
1528 if (qp->sq.wqe_cnt) {
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001529 err = create_raw_packet_qp_tis(dev, qp, sq, tdn, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001530 if (err)
1531 return err;
1532
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001533 err = create_raw_packet_qp_sq(dev, udata, sq, in, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001534 if (err)
1535 goto err_destroy_tis;
1536
Yishai Hadas7f720522018-09-20 21:45:18 +03001537 if (uid) {
1538 resp->tisn = sq->tisn;
1539 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TISN;
1540 resp->sqn = sq->base.mqp.qpn;
1541 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_SQN;
1542 }
1543
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001544 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001545 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001546 }
1547
1548 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001549 rq->base.container_mibqp = qp;
1550
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001551 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1552 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001553 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1554 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
Yishai Hadas34d57582018-09-20 21:39:21 +03001555 err = create_raw_packet_qp_rq(dev, rq, in, inlen, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001556 if (err)
1557 goto err_destroy_sq;
1558
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001559 err = create_raw_packet_qp_tir(
1560 dev, rq, tdn, &qp->flags_en, pd, out,
1561 MLX5_ST_SZ_BYTES(create_tir_out));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001562 if (err)
1563 goto err_destroy_rq;
Yishai Hadas7f720522018-09-20 21:45:18 +03001564
1565 if (uid) {
1566 resp->rqn = rq->base.mqp.qpn;
1567 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_RQN;
1568 resp->tirn = rq->tirn;
1569 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001570 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1571 resp->tir_icm_addr = MLX5_GET(
1572 create_tir_out, out, icm_address_31_0);
1573 resp->tir_icm_addr |=
1574 (u64)MLX5_GET(create_tir_out, out,
1575 icm_address_39_32)
1576 << 32;
1577 resp->tir_icm_addr |=
1578 (u64)MLX5_GET(create_tir_out, out,
1579 icm_address_63_40)
1580 << 40;
1581 resp->comp_mask |=
1582 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1583 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001584 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001585 }
1586
1587 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1588 rq->base.mqp.qpn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001589 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
1590 if (err)
1591 goto err_destroy_tir;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001592
1593 return 0;
1594
Yishai Hadas7f720522018-09-20 21:45:18 +03001595err_destroy_tir:
1596 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001597err_destroy_rq:
1598 destroy_raw_packet_qp_rq(dev, rq);
1599err_destroy_sq:
1600 if (!qp->sq.wqe_cnt)
1601 return err;
1602 destroy_raw_packet_qp_sq(dev, sq);
1603err_destroy_tis:
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001604 destroy_raw_packet_qp_tis(dev, sq, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001605
1606 return err;
1607}
1608
1609static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1610 struct mlx5_ib_qp *qp)
1611{
1612 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1613 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1614 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1615
1616 if (qp->rq.wqe_cnt) {
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001617 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001618 destroy_raw_packet_qp_rq(dev, rq);
1619 }
1620
1621 if (qp->sq.wqe_cnt) {
1622 destroy_raw_packet_qp_sq(dev, sq);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001623 destroy_raw_packet_qp_tis(dev, sq, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001624 }
1625}
1626
1627static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1628 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1629{
1630 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1631 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1632
1633 sq->sq = &qp->sq;
1634 rq->rq = &qp->rq;
1635 sq->doorbell = &qp->db;
1636 rq->doorbell = &qp->db;
1637}
1638
Yishai Hadas28d61372016-05-23 15:20:56 +03001639static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1640{
Mark Bloch0042f9e2018-09-17 13:30:49 +03001641 if (qp->flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1642 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1643 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001644 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1645 to_mpd(qp->ibqp.pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001646}
1647
1648static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1649 struct ib_pd *pd,
1650 struct ib_qp_init_attr *init_attr,
1651 struct ib_udata *udata)
1652{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001653 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1654 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas28d61372016-05-23 15:20:56 +03001655 struct mlx5_ib_create_qp_resp resp = {};
1656 int inlen;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001657 int outlen;
Yishai Hadas28d61372016-05-23 15:20:56 +03001658 int err;
1659 u32 *in;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001660 u32 *out;
Yishai Hadas28d61372016-05-23 15:20:56 +03001661 void *tirc;
1662 void *hfso;
1663 u32 selected_fields = 0;
Matan Barak2d93fc82018-03-28 09:27:55 +03001664 u32 outer_l4;
Yishai Hadas28d61372016-05-23 15:20:56 +03001665 size_t min_resp_len;
1666 u32 tdn = mucontext->tdn;
1667 struct mlx5_ib_create_qp_rss ucmd = {};
1668 size_t required_cmd_sz;
Mark Bloch175edba2018-09-17 13:30:48 +03001669 u8 lb_flag = 0;
Yishai Hadas28d61372016-05-23 15:20:56 +03001670
1671 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1672 return -EOPNOTSUPP;
1673
1674 if (init_attr->create_flags || init_attr->send_cq)
1675 return -EINVAL;
1676
Eli Cohen2f5ff262017-01-03 23:55:21 +02001677 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001678 if (udata->outlen < min_resp_len)
1679 return -EINVAL;
1680
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001681 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001682 if (udata->inlen < required_cmd_sz) {
1683 mlx5_ib_dbg(dev, "invalid inlen\n");
1684 return -EINVAL;
1685 }
1686
1687 if (udata->inlen > sizeof(ucmd) &&
1688 !ib_is_udata_cleared(udata, sizeof(ucmd),
1689 udata->inlen - sizeof(ucmd))) {
1690 mlx5_ib_dbg(dev, "inlen is not supported\n");
1691 return -EOPNOTSUPP;
1692 }
1693
1694 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1695 mlx5_ib_dbg(dev, "copy failed\n");
1696 return -EFAULT;
1697 }
1698
1699 if (ucmd.comp_mask) {
1700 mlx5_ib_dbg(dev, "invalid comp mask\n");
1701 return -EOPNOTSUPP;
1702 }
1703
Mark Bloch175edba2018-09-17 13:30:48 +03001704 if (ucmd.flags & ~(MLX5_QP_FLAG_TUNNEL_OFFLOADS |
1705 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1706 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)) {
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001707 mlx5_ib_dbg(dev, "invalid flags\n");
1708 return -EOPNOTSUPP;
1709 }
1710
1711 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1712 !tunnel_offload_supported(dev->mdev)) {
1713 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001714 return -EOPNOTSUPP;
1715 }
1716
Maor Gottlieb309fa342017-10-19 08:25:56 +03001717 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1718 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1719 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1720 return -EOPNOTSUPP;
1721 }
1722
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001723 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC || dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001724 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1725 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1726 }
1727
1728 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
1729 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1730 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
1731 }
1732
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001733 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
Yishai Hadas28d61372016-05-23 15:20:56 +03001734 if (err) {
1735 mlx5_ib_dbg(dev, "copy failed\n");
1736 return -EINVAL;
1737 }
1738
1739 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001740 outlen = MLX5_ST_SZ_BYTES(create_tir_out);
1741 in = kvzalloc(inlen + outlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001742 if (!in)
1743 return -ENOMEM;
1744
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001745 out = in + MLX5_ST_SZ_DW(create_tir_in);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001746 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001747 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1748 MLX5_SET(tirc, tirc, disp_type,
1749 MLX5_TIRC_DISP_TYPE_INDIRECT);
1750 MLX5_SET(tirc, tirc, indirect_table,
1751 init_attr->rwq_ind_tbl->ind_tbl_num);
1752 MLX5_SET(tirc, tirc, transport_domain, tdn);
1753
1754 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001755
1756 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1757 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1758
Mark Bloch175edba2018-09-17 13:30:48 +03001759 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
1760
Maor Gottlieb309fa342017-10-19 08:25:56 +03001761 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1762 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1763 else
1764 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1765
Yishai Hadas28d61372016-05-23 15:20:56 +03001766 switch (ucmd.rx_hash_function) {
1767 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1768 {
1769 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1770 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1771
1772 if (len != ucmd.rx_key_len) {
1773 err = -EINVAL;
1774 goto err;
1775 }
1776
1777 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
Yishai Hadas28d61372016-05-23 15:20:56 +03001778 memcpy(rss_key, ucmd.rx_hash_key, len);
1779 break;
1780 }
1781 default:
1782 err = -EOPNOTSUPP;
1783 goto err;
1784 }
1785
1786 if (!ucmd.rx_hash_fields_mask) {
1787 /* special case when this TIR serves as steering entry without hashing */
1788 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1789 goto create_tir;
1790 err = -EINVAL;
1791 goto err;
1792 }
1793
1794 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1795 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1796 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1797 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1798 err = -EINVAL;
1799 goto err;
1800 }
1801
1802 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1803 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1804 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1805 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1806 MLX5_L3_PROT_TYPE_IPV4);
1807 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1808 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1809 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1810 MLX5_L3_PROT_TYPE_IPV6);
1811
Matan Barak2d93fc82018-03-28 09:27:55 +03001812 outer_l4 = ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1813 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) << 0 |
1814 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1815 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP)) << 1 |
1816 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI) << 2;
1817
1818 /* Check that only one l4 protocol is set */
1819 if (outer_l4 & (outer_l4 - 1)) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001820 err = -EINVAL;
1821 goto err;
1822 }
1823
1824 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1825 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1826 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1827 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1828 MLX5_L4_PROT_TYPE_TCP);
1829 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1830 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1831 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1832 MLX5_L4_PROT_TYPE_UDP);
1833
1834 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1835 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1836 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1837
1838 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1839 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1840 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1841
1842 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1843 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1844 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1845
1846 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1847 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1848 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1849
Matan Barak2d93fc82018-03-28 09:27:55 +03001850 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI)
1851 selected_fields |= MLX5_HASH_FIELD_SEL_IPSEC_SPI;
1852
Yishai Hadas28d61372016-05-23 15:20:56 +03001853 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1854
1855create_tir:
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001856 err = mlx5_core_create_tir_out(dev->mdev, in, inlen, out, outlen);
Yishai Hadas28d61372016-05-23 15:20:56 +03001857
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001858 qp->rss_qp.tirn = MLX5_GET(create_tir_out, out, tirn);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001859 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1860 err = mlx5_ib_enable_lb(dev, false, true);
1861
1862 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001863 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1864 to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001865 }
1866
Yishai Hadas28d61372016-05-23 15:20:56 +03001867 if (err)
1868 goto err;
1869
Yishai Hadas7f720522018-09-20 21:45:18 +03001870 if (mucontext->devx_uid) {
1871 resp.comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1872 resp.tirn = qp->rss_qp.tirn;
Ariel Levkovich1f1d6ab2019-03-31 19:44:50 +03001873 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1874 resp.tir_icm_addr =
1875 MLX5_GET(create_tir_out, out, icm_address_31_0);
1876 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1877 icm_address_39_32)
1878 << 32;
1879 resp.tir_icm_addr |= (u64)MLX5_GET(create_tir_out, out,
1880 icm_address_63_40)
1881 << 40;
1882 resp.comp_mask |=
1883 MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
1884 }
Yishai Hadas7f720522018-09-20 21:45:18 +03001885 }
1886
1887 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
1888 if (err)
1889 goto err_copy;
1890
Yishai Hadas28d61372016-05-23 15:20:56 +03001891 kvfree(in);
1892 /* qpn is reserved for that QP */
1893 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001894 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001895 return 0;
1896
Yishai Hadas7f720522018-09-20 21:45:18 +03001897err_copy:
1898 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn, mucontext->devx_uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001899err:
1900 kvfree(in);
1901 return err;
1902}
1903
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001904static void configure_responder_scat_cqe(struct ib_qp_init_attr *init_attr,
1905 void *qpc)
1906{
1907 int rcqe_sz;
1908
1909 if (init_attr->qp_type == MLX5_IB_QPT_DCI)
1910 return;
1911
1912 rcqe_sz = mlx5_ib_get_cqe_size(init_attr->recv_cq);
1913
Guy Levi7249c8e2019-04-10 10:59:45 +03001914 if (init_attr->qp_type == MLX5_IB_QPT_DCT) {
1915 if (rcqe_sz == 128)
1916 MLX5_SET(dctc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
1917
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001918 return;
1919 }
1920
Guy Levi7249c8e2019-04-10 10:59:45 +03001921 MLX5_SET(qpc, qpc, cs_res,
1922 rcqe_sz == 128 ? MLX5_RES_SCAT_DATA64_CQE :
1923 MLX5_RES_SCAT_DATA32_CQE);
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001924}
1925
1926static void configure_requester_scat_cqe(struct mlx5_ib_dev *dev,
1927 struct ib_qp_init_attr *init_attr,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001928 struct mlx5_ib_create_qp *ucmd,
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001929 void *qpc)
1930{
1931 enum ib_qp_type qpt = init_attr->qp_type;
1932 int scqe_sz;
zhengbin2ab367a2019-12-24 16:40:12 +08001933 bool allow_scat_cqe = false;
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001934
1935 if (qpt == IB_QPT_UC || qpt == IB_QPT_UD)
1936 return;
1937
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001938 if (ucmd)
1939 allow_scat_cqe = ucmd->flags & MLX5_QP_FLAG_ALLOW_SCATTER_CQE;
1940
1941 if (!allow_scat_cqe && init_attr->sq_sig_type != IB_SIGNAL_ALL_WR)
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001942 return;
1943
1944 scqe_sz = mlx5_ib_get_cqe_size(init_attr->send_cq);
1945 if (scqe_sz == 128) {
1946 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
1947 return;
1948 }
1949
1950 if (init_attr->qp_type != MLX5_IB_QPT_DCI ||
1951 MLX5_CAP_GEN(dev->mdev, dc_req_scat_data_cqe))
1952 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
1953}
1954
Yonatan Cohena60109d2018-10-10 09:25:16 +03001955static int atomic_size_to_mode(int size_mask)
1956{
1957 /* driver does not support atomic_size > 256B
1958 * and does not know how to translate bigger sizes
1959 */
1960 int supported_size_mask = size_mask & 0x1ff;
1961 int log_max_size;
1962
1963 if (!supported_size_mask)
1964 return -EOPNOTSUPP;
1965
1966 log_max_size = __fls(supported_size_mask);
1967
1968 if (log_max_size > 3)
1969 return log_max_size;
1970
1971 return MLX5_ATOMIC_MODE_8B;
1972}
1973
1974static int get_atomic_mode(struct mlx5_ib_dev *dev,
1975 enum ib_qp_type qp_type)
1976{
1977 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
1978 u8 atomic = MLX5_CAP_GEN(dev->mdev, atomic);
1979 int atomic_mode = -EOPNOTSUPP;
1980 int atomic_size_mask;
1981
1982 if (!atomic)
1983 return -EOPNOTSUPP;
1984
1985 if (qp_type == MLX5_IB_QPT_DCT)
1986 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
1987 else
1988 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
1989
1990 if ((atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_CMP_SWAP) ||
1991 (atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_FETCH_ADD))
1992 atomic_mode = atomic_size_to_mode(atomic_size_mask);
1993
1994 if (atomic_mode <= 0 &&
1995 (atomic_operations & MLX5_ATOMIC_OPS_CMP_SWAP &&
1996 atomic_operations & MLX5_ATOMIC_OPS_FETCH_ADD))
1997 atomic_mode = MLX5_ATOMIC_MODE_IB_COMP;
1998
1999 return atomic_mode;
2000}
2001
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002002static inline bool check_flags_mask(uint64_t input, uint64_t supported)
2003{
2004 return (input & ~supported) == 0;
2005}
2006
Eli Cohene126ba92013-07-07 17:25:49 +03002007static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
2008 struct ib_qp_init_attr *init_attr,
2009 struct ib_udata *udata, struct mlx5_ib_qp *qp)
2010{
2011 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002012 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03002013 struct mlx5_core_dev *mdev = dev->mdev;
Jason Gunthorpe0625b4b2018-08-14 15:33:52 -06002014 struct mlx5_ib_create_qp_resp resp = {};
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002015 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2016 udata, struct mlx5_ib_ucontext, ibucontext);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002017 struct mlx5_ib_cq *send_cq;
2018 struct mlx5_ib_cq *recv_cq;
2019 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002020 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002021 struct mlx5_ib_create_qp ucmd;
2022 struct mlx5_ib_qp_base *base;
Noa Osheroviche7b169f2018-02-25 13:39:51 +02002023 int mlx5_st;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002024 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002025 u32 *in;
2026 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002027
2028 mutex_init(&qp->mutex);
2029 spin_lock_init(&qp->sq.lock);
2030 spin_lock_init(&qp->rq.lock);
2031
Noa Osheroviche7b169f2018-02-25 13:39:51 +02002032 mlx5_st = to_mlx5_st(init_attr->qp_type);
2033 if (mlx5_st < 0)
2034 return -EINVAL;
2035
Yishai Hadas28d61372016-05-23 15:20:56 +03002036 if (init_attr->rwq_ind_tbl) {
2037 if (!udata)
2038 return -ENOSYS;
2039
2040 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
2041 return err;
2042 }
2043
Eli Cohenf360d882014-04-02 00:10:16 +03002044 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002045 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03002046 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
2047 return -EINVAL;
2048 } else {
2049 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
2050 }
2051 }
2052
Leon Romanovsky051f2632015-12-20 12:16:11 +02002053 if (init_attr->create_flags &
2054 (IB_QP_CREATE_CROSS_CHANNEL |
2055 IB_QP_CREATE_MANAGED_SEND |
2056 IB_QP_CREATE_MANAGED_RECV)) {
2057 if (!MLX5_CAP_GEN(mdev, cd)) {
2058 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
2059 return -EINVAL;
2060 }
2061 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
2062 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
2063 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
2064 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
2065 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
2066 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
2067 }
Erez Shitritf0313962016-02-21 16:27:17 +02002068
2069 if (init_attr->qp_type == IB_QPT_UD &&
2070 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
2071 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
2072 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
2073 return -EOPNOTSUPP;
2074 }
2075
Majd Dibbiny358e42e2016-04-17 17:19:37 +03002076 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
2077 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2078 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
2079 return -EOPNOTSUPP;
2080 }
2081 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
2082 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
2083 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
2084 return -EOPNOTSUPP;
2085 }
2086 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
2087 }
2088
Eli Cohene126ba92013-07-07 17:25:49 +03002089 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
2090 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
2091
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02002092 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
2093 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
2094 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
2095 (init_attr->qp_type != IB_QPT_RAW_PACKET))
2096 return -EOPNOTSUPP;
2097 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
2098 }
2099
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002100 if (udata) {
Eli Cohene126ba92013-07-07 17:25:49 +03002101 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
2102 mlx5_ib_dbg(dev, "copy failed\n");
2103 return -EFAULT;
2104 }
2105
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002106 if (!check_flags_mask(ucmd.flags,
Mark Bloch8af526e2019-01-15 16:45:32 +02002107 MLX5_QP_FLAG_ALLOW_SCATTER_CQE |
2108 MLX5_QP_FLAG_BFREG_INDEX |
2109 MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE |
2110 MLX5_QP_FLAG_SCATTER_CQE |
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002111 MLX5_QP_FLAG_SIGNATURE |
Mark Bloch8af526e2019-01-15 16:45:32 +02002112 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC |
2113 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
2114 MLX5_QP_FLAG_TUNNEL_OFFLOADS |
Yishai Hadasac42a5e2020-03-24 08:01:41 +02002115 MLX5_QP_FLAG_UAR_PAGE_INDEX |
Mark Bloch8af526e2019-01-15 16:45:32 +02002116 MLX5_QP_FLAG_TYPE_DCI |
2117 MLX5_QP_FLAG_TYPE_DCT))
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002118 return -EINVAL;
2119
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002120 err = get_qp_user_index(ucontext, &ucmd, udata->inlen, &uidx);
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002121 if (err)
2122 return err;
2123
Eli Cohene126ba92013-07-07 17:25:49 +03002124 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002125 if (MLX5_CAP_GEN(dev->mdev, sctr_data_cqe))
2126 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002127 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
2128 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
2129 !tunnel_offload_supported(mdev)) {
2130 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
2131 return -EOPNOTSUPP;
2132 }
Mark Bloch175edba2018-09-17 13:30:48 +03002133 qp->flags_en |= MLX5_QP_FLAG_TUNNEL_OFFLOADS;
2134 }
2135
2136 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC) {
2137 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2138 mlx5_ib_dbg(dev, "Self-LB UC isn't supported\n");
2139 return -EOPNOTSUPP;
2140 }
2141 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
2142 }
2143
2144 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
2145 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2146 mlx5_ib_dbg(dev, "Self-LB UM isn't supported\n");
2147 return -EOPNOTSUPP;
2148 }
2149 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002150 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002151
Danit Goldberg569c6652018-11-30 13:22:05 +02002152 if (ucmd.flags & MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE) {
2153 if (init_attr->qp_type != IB_QPT_RC ||
2154 !MLX5_CAP_GEN(dev->mdev, qp_packet_based)) {
2155 mlx5_ib_dbg(dev, "packet based credit mode isn't supported\n");
2156 return -EOPNOTSUPP;
2157 }
2158 qp->flags |= MLX5_IB_QP_PACKET_BASED_CREDIT;
2159 }
2160
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002161 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
2162 if (init_attr->qp_type != IB_QPT_UD ||
2163 (MLX5_CAP_GEN(dev->mdev, port_type) !=
2164 MLX5_CAP_PORT_TYPE_IB) ||
2165 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
2166 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
2167 return -EOPNOTSUPP;
2168 }
2169
2170 qp->flags |= MLX5_IB_QP_UNDERLAY;
2171 qp->underlay_qpn = init_attr->source_qpn;
2172 }
Eli Cohene126ba92013-07-07 17:25:49 +03002173 } else {
2174 qp->wq_sig = !!wq_signature;
2175 }
2176
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002177 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2178 qp->flags & MLX5_IB_QP_UNDERLAY) ?
2179 &qp->raw_packet_qp.rq.base :
2180 &qp->trans_qp.base;
2181
Eli Cohene126ba92013-07-07 17:25:49 +03002182 qp->has_rq = qp_has_rq(init_attr);
2183 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002184 qp, udata ? &ucmd : NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002185 if (err) {
2186 mlx5_ib_dbg(dev, "err %d\n", err);
2187 return err;
2188 }
2189
2190 if (pd) {
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002191 if (udata) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002192 __u32 max_wqes =
2193 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03002194 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
2195 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
2196 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
2197 mlx5_ib_dbg(dev, "invalid rq params\n");
2198 return -EINVAL;
2199 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002200 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03002201 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03002202 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03002203 return -EINVAL;
2204 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02002205 if (init_attr->create_flags &
Michael Guralnik3f89b012019-10-20 09:43:59 +03002206 MLX5_IB_QP_CREATE_SQPN_QP1) {
Haggai Eranb11a4f92016-02-29 15:45:03 +02002207 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
2208 return -EINVAL;
2209 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002210 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
2211 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002212 if (err)
2213 mlx5_ib_dbg(dev, "err %d\n", err);
2214 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002215 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
2216 base);
Eli Cohene126ba92013-07-07 17:25:49 +03002217 if (err)
2218 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03002219 }
2220
2221 if (err)
2222 return err;
2223 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002224 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03002225 if (!in)
2226 return -ENOMEM;
2227
2228 qp->create_type = MLX5_QP_EMPTY;
2229 }
2230
2231 if (is_sqp(init_attr->qp_type))
2232 qp->port = init_attr->port_num;
2233
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002234 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
2235
Noa Osheroviche7b169f2018-02-25 13:39:51 +02002236 MLX5_SET(qpc, qpc, st, mlx5_st);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002237 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03002238
2239 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002240 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002241 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002242 MLX5_SET(qpc, qpc, latency_sensitive, 1);
2243
Eli Cohene126ba92013-07-07 17:25:49 +03002244
2245 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002246 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002247
Eli Cohenf360d882014-04-02 00:10:16 +03002248 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002249 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03002250
Leon Romanovsky051f2632015-12-20 12:16:11 +02002251 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002252 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002253 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002254 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002255 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002256 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Danit Goldberg569c6652018-11-30 13:22:05 +02002257 if (qp->flags & MLX5_IB_QP_PACKET_BASED_CREDIT)
2258 MLX5_SET(qpc, qpc, req_e2e_credit_mode, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002259 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002260 configure_responder_scat_cqe(init_attr, qpc);
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002261 configure_requester_scat_cqe(dev, init_attr,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002262 udata ? &ucmd : NULL,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002263 qpc);
Eli Cohene126ba92013-07-07 17:25:49 +03002264 }
2265
2266 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002267 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
2268 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03002269 }
2270
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002271 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03002272
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002273 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002274 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002275 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002276 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002277 if (init_attr->srq &&
2278 init_attr->srq->srq_type == IB_SRQT_TM)
2279 MLX5_SET(qpc, qpc, offload_type,
2280 MLX5_QPC_OFFLOAD_TYPE_RNDV);
2281 }
Eli Cohene126ba92013-07-07 17:25:49 +03002282
2283 /* Set default resources */
2284 switch (init_attr->qp_type) {
2285 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002286 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2287 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
2288 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
2289 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002290 break;
2291 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002292 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2293 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2294 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002295 break;
2296 default:
2297 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002298 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
2299 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002300 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002301 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2302 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002303 }
2304 }
2305
2306 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002307 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002308
2309 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002310 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002311
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002312 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03002313
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002314 /* 0xffffff means we ask to work with cqe version 0 */
2315 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002316 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002317
Erez Shitritf0313962016-02-21 16:27:17 +02002318 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
2319 if (init_attr->qp_type == IB_QPT_UD &&
2320 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02002321 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
2322 qp->flags |= MLX5_IB_QP_LSO;
2323 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002324
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002325 if (init_attr->create_flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
2326 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
2327 mlx5_ib_dbg(dev, "scatter end padding is not supported\n");
2328 err = -EOPNOTSUPP;
2329 goto err;
2330 } else if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2331 MLX5_SET(qpc, qpc, end_padding_mode,
2332 MLX5_WQ_END_PAD_MODE_ALIGN);
2333 } else {
2334 qp->flags |= MLX5_IB_QP_PCI_WRITE_END_PADDING;
2335 }
2336 }
2337
Boris Pismenny2c292db2018-03-08 15:51:40 +02002338 if (inlen < 0) {
2339 err = -EINVAL;
2340 goto err;
2341 }
2342
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002343 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2344 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002345 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
2346 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
Yishai Hadas7f720522018-09-20 21:45:18 +03002347 err = create_raw_packet_qp(dev, qp, in, inlen, pd, udata,
2348 &resp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002349 } else {
2350 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
2351 }
2352
Eli Cohene126ba92013-07-07 17:25:49 +03002353 if (err) {
2354 mlx5_ib_dbg(dev, "create qp failed\n");
2355 goto err_create;
2356 }
2357
Al Viro479163f2014-11-20 08:13:57 +00002358 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002359
majd@mellanox.com19098df2016-01-14 19:13:03 +02002360 base->container_mibqp = qp;
2361 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03002362
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002363 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
2364 &send_cq, &recv_cq);
2365 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2366 mlx5_ib_lock_cqs(send_cq, recv_cq);
2367 /* Maintain device to QPs access, needed for further handling via reset
2368 * flow
2369 */
2370 list_add_tail(&qp->qps_list, &dev->qp_list);
2371 /* Maintain CQ to QPs access, needed for further handling via reset flow
2372 */
2373 if (send_cq)
2374 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
2375 if (recv_cq)
2376 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
2377 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2378 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
2379
Eli Cohene126ba92013-07-07 17:25:49 +03002380 return 0;
2381
2382err_create:
2383 if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002384 destroy_qp_user(dev, pd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002385 else if (qp->create_type == MLX5_QP_KERNEL)
2386 destroy_qp_kernel(dev, qp);
2387
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002388err:
Al Viro479163f2014-11-20 08:13:57 +00002389 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002390 return err;
2391}
2392
2393static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2394 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
2395{
2396 if (send_cq) {
2397 if (recv_cq) {
2398 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002399 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002400 spin_lock_nested(&recv_cq->lock,
2401 SINGLE_DEPTH_NESTING);
2402 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002403 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002404 __acquire(&recv_cq->lock);
2405 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002406 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002407 spin_lock_nested(&send_cq->lock,
2408 SINGLE_DEPTH_NESTING);
2409 }
2410 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002411 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002412 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002413 }
2414 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002415 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002416 __acquire(&send_cq->lock);
2417 } else {
2418 __acquire(&send_cq->lock);
2419 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002420 }
2421}
2422
2423static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2424 __releases(&send_cq->lock) __releases(&recv_cq->lock)
2425{
2426 if (send_cq) {
2427 if (recv_cq) {
2428 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
2429 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002430 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002431 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
2432 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002433 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002434 } else {
2435 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002436 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002437 }
2438 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002439 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002440 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002441 }
2442 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002443 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002444 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002445 } else {
2446 __release(&recv_cq->lock);
2447 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002448 }
2449}
2450
2451static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
2452{
2453 return to_mpd(qp->ibqp.pd);
2454}
2455
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002456static void get_cqs(enum ib_qp_type qp_type,
2457 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03002458 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
2459{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002460 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002461 case IB_QPT_XRC_TGT:
2462 *send_cq = NULL;
2463 *recv_cq = NULL;
2464 break;
2465 case MLX5_IB_QPT_REG_UMR:
2466 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002467 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002468 *recv_cq = NULL;
2469 break;
2470
2471 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002472 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002473 case IB_QPT_RC:
2474 case IB_QPT_UC:
2475 case IB_QPT_UD:
2476 case IB_QPT_RAW_IPV6:
2477 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002478 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002479 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2480 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002481 break;
2482
Eli Cohene126ba92013-07-07 17:25:49 +03002483 case IB_QPT_MAX:
2484 default:
2485 *send_cq = NULL;
2486 *recv_cq = NULL;
2487 break;
2488 }
2489}
2490
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002491static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002492 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2493 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002494
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002495static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2496 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002497{
2498 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002499 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002500 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002501 int err;
2502
Yishai Hadas28d61372016-05-23 15:20:56 +03002503 if (qp->ibqp.rwq_ind_tbl) {
2504 destroy_rss_raw_qp_tir(dev, qp);
2505 return;
2506 }
2507
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002508 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2509 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002510 &qp->raw_packet_qp.rq.base :
2511 &qp->trans_qp.base;
2512
Haggai Eran6aec21f2014-12-11 17:04:23 +02002513 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002514 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2515 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002516 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002517 MLX5_CMD_OP_2RST_QP, 0,
2518 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002519 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002520 struct mlx5_modify_raw_qp_param raw_qp_param = {
2521 .operation = MLX5_CMD_OP_2RST_QP
2522 };
2523
Aviv Heller13eab212016-09-18 20:48:04 +03002524 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002525 }
2526 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002527 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002528 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002529 }
Eli Cohene126ba92013-07-07 17:25:49 +03002530
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002531 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2532 &send_cq, &recv_cq);
2533
2534 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2535 mlx5_ib_lock_cqs(send_cq, recv_cq);
2536 /* del from lists under both locks above to protect reset flow paths */
2537 list_del(&qp->qps_list);
2538 if (send_cq)
2539 list_del(&qp->cq_send_list);
2540
2541 if (recv_cq)
2542 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002543
2544 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002545 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002546 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2547 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002548 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2549 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002550 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002551 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2552 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002553
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002554 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2555 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002556 destroy_raw_packet_qp(dev, qp);
2557 } else {
2558 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
2559 if (err)
2560 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2561 base->mqp.qpn);
2562 }
Eli Cohene126ba92013-07-07 17:25:49 +03002563
Eli Cohene126ba92013-07-07 17:25:49 +03002564 if (qp->create_type == MLX5_QP_KERNEL)
2565 destroy_qp_kernel(dev, qp);
2566 else if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002567 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002568}
2569
2570static const char *ib_qp_type_str(enum ib_qp_type type)
2571{
2572 switch (type) {
2573 case IB_QPT_SMI:
2574 return "IB_QPT_SMI";
2575 case IB_QPT_GSI:
2576 return "IB_QPT_GSI";
2577 case IB_QPT_RC:
2578 return "IB_QPT_RC";
2579 case IB_QPT_UC:
2580 return "IB_QPT_UC";
2581 case IB_QPT_UD:
2582 return "IB_QPT_UD";
2583 case IB_QPT_RAW_IPV6:
2584 return "IB_QPT_RAW_IPV6";
2585 case IB_QPT_RAW_ETHERTYPE:
2586 return "IB_QPT_RAW_ETHERTYPE";
2587 case IB_QPT_XRC_INI:
2588 return "IB_QPT_XRC_INI";
2589 case IB_QPT_XRC_TGT:
2590 return "IB_QPT_XRC_TGT";
2591 case IB_QPT_RAW_PACKET:
2592 return "IB_QPT_RAW_PACKET";
2593 case MLX5_IB_QPT_REG_UMR:
2594 return "MLX5_IB_QPT_REG_UMR";
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002595 case IB_QPT_DRIVER:
2596 return "IB_QPT_DRIVER";
Eli Cohene126ba92013-07-07 17:25:49 +03002597 case IB_QPT_MAX:
2598 default:
2599 return "Invalid QP type";
2600 }
2601}
2602
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002603static struct ib_qp *mlx5_ib_create_dct(struct ib_pd *pd,
2604 struct ib_qp_init_attr *attr,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002605 struct mlx5_ib_create_qp *ucmd,
2606 struct ib_udata *udata)
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002607{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002608 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2609 udata, struct mlx5_ib_ucontext, ibucontext);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002610 struct mlx5_ib_qp *qp;
2611 int err = 0;
2612 u32 uidx = MLX5_IB_DEFAULT_UIDX;
2613 void *dctc;
2614
2615 if (!attr->srq || !attr->recv_cq)
2616 return ERR_PTR(-EINVAL);
2617
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002618 err = get_qp_user_index(ucontext, ucmd, sizeof(*ucmd), &uidx);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002619 if (err)
2620 return ERR_PTR(err);
2621
2622 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2623 if (!qp)
2624 return ERR_PTR(-ENOMEM);
2625
2626 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
2627 if (!qp->dct.in) {
2628 err = -ENOMEM;
2629 goto err_free;
2630 }
2631
Yishai Hadasa01a5862018-09-20 21:39:24 +03002632 MLX5_SET(create_dct_in, qp->dct.in, uid, to_mpd(pd)->uid);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002633 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shoua776a3902018-01-02 16:19:33 +02002634 qp->qp_sub_type = MLX5_IB_QPT_DCT;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002635 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2636 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2637 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2638 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2639 MLX5_SET(dctc, dctc, user_index, uidx);
2640
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002641 if (ucmd->flags & MLX5_QP_FLAG_SCATTER_CQE)
2642 configure_responder_scat_cqe(attr, dctc);
2643
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002644 qp->state = IB_QPS_RESET;
2645
2646 return &qp->ibqp;
2647err_free:
2648 kfree(qp);
2649 return ERR_PTR(err);
2650}
2651
2652static int set_mlx_qp_type(struct mlx5_ib_dev *dev,
2653 struct ib_qp_init_attr *init_attr,
2654 struct mlx5_ib_create_qp *ucmd,
2655 struct ib_udata *udata)
2656{
2657 enum { MLX_QP_FLAGS = MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI };
2658 int err;
2659
2660 if (!udata)
2661 return -EINVAL;
2662
2663 if (udata->inlen < sizeof(*ucmd)) {
2664 mlx5_ib_dbg(dev, "create_qp user command is smaller than expected\n");
2665 return -EINVAL;
2666 }
2667 err = ib_copy_from_udata(ucmd, udata, sizeof(*ucmd));
2668 if (err)
2669 return err;
2670
2671 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCI) {
2672 init_attr->qp_type = MLX5_IB_QPT_DCI;
2673 } else {
2674 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCT) {
2675 init_attr->qp_type = MLX5_IB_QPT_DCT;
2676 } else {
2677 mlx5_ib_dbg(dev, "Invalid QP flags\n");
2678 return -EINVAL;
2679 }
2680 }
2681
2682 if (!MLX5_CAP_GEN(dev->mdev, dct)) {
2683 mlx5_ib_dbg(dev, "DC transport is not supported\n");
2684 return -EOPNOTSUPP;
2685 }
2686
2687 return 0;
2688}
2689
Eli Cohene126ba92013-07-07 17:25:49 +03002690struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002691 struct ib_qp_init_attr *verbs_init_attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002692 struct ib_udata *udata)
2693{
2694 struct mlx5_ib_dev *dev;
2695 struct mlx5_ib_qp *qp;
2696 u16 xrcdn = 0;
2697 int err;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002698 struct ib_qp_init_attr mlx_init_attr;
2699 struct ib_qp_init_attr *init_attr = verbs_init_attr;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002700 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2701 udata, struct mlx5_ib_ucontext, ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03002702
2703 if (pd) {
2704 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002705
2706 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002707 if (!ucontext) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002708 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2709 return ERR_PTR(-EINVAL);
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002710 } else if (!ucontext->cqe_version) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002711 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2712 return ERR_PTR(-EINVAL);
2713 }
2714 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002715 } else {
2716 /* being cautious here */
2717 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2718 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2719 pr_warn("%s: no PD for transport %s\n", __func__,
2720 ib_qp_type_str(init_attr->qp_type));
2721 return ERR_PTR(-EINVAL);
2722 }
2723 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002724 }
2725
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002726 if (init_attr->qp_type == IB_QPT_DRIVER) {
2727 struct mlx5_ib_create_qp ucmd;
2728
2729 init_attr = &mlx_init_attr;
2730 memcpy(init_attr, verbs_init_attr, sizeof(*verbs_init_attr));
2731 err = set_mlx_qp_type(dev, init_attr, &ucmd, udata);
2732 if (err)
2733 return ERR_PTR(err);
Moni Shouac32a4f22018-01-02 16:19:32 +02002734
2735 if (init_attr->qp_type == MLX5_IB_QPT_DCI) {
2736 if (init_attr->cap.max_recv_wr ||
2737 init_attr->cap.max_recv_sge) {
2738 mlx5_ib_dbg(dev, "DCI QP requires zero size receive queue\n");
2739 return ERR_PTR(-EINVAL);
2740 }
Moni Shoua776a3902018-01-02 16:19:33 +02002741 } else {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002742 return mlx5_ib_create_dct(pd, init_attr, &ucmd, udata);
Moni Shouac32a4f22018-01-02 16:19:32 +02002743 }
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002744 }
2745
Eli Cohene126ba92013-07-07 17:25:49 +03002746 switch (init_attr->qp_type) {
2747 case IB_QPT_XRC_TGT:
2748 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002749 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002750 mlx5_ib_dbg(dev, "XRC not supported\n");
2751 return ERR_PTR(-ENOSYS);
2752 }
2753 init_attr->recv_cq = NULL;
2754 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2755 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2756 init_attr->send_cq = NULL;
2757 }
2758
2759 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002760 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002761 case IB_QPT_RC:
2762 case IB_QPT_UC:
2763 case IB_QPT_UD:
2764 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002765 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002766 case MLX5_IB_QPT_REG_UMR:
Moni Shouac32a4f22018-01-02 16:19:32 +02002767 case MLX5_IB_QPT_DCI:
Eli Cohene126ba92013-07-07 17:25:49 +03002768 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2769 if (!qp)
2770 return ERR_PTR(-ENOMEM);
2771
2772 err = create_qp_common(dev, pd, init_attr, udata, qp);
2773 if (err) {
2774 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2775 kfree(qp);
2776 return ERR_PTR(err);
2777 }
2778
2779 if (is_qp0(init_attr->qp_type))
2780 qp->ibqp.qp_num = 0;
2781 else if (is_qp1(init_attr->qp_type))
2782 qp->ibqp.qp_num = 1;
2783 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002784 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002785
2786 mlx5_ib_dbg(dev, "ib qpnum 0x%x, mlx qpn 0x%x, rcqn 0x%x, scqn 0x%x\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002787 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002788 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2789 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002790
majd@mellanox.com19098df2016-01-14 19:13:03 +02002791 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002792
2793 break;
2794
Haggai Erand16e91d2016-02-29 15:45:05 +02002795 case IB_QPT_GSI:
2796 return mlx5_ib_gsi_create_qp(pd, init_attr);
2797
Eli Cohene126ba92013-07-07 17:25:49 +03002798 case IB_QPT_RAW_IPV6:
2799 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002800 case IB_QPT_MAX:
2801 default:
2802 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2803 init_attr->qp_type);
2804 /* Don't support raw QPs */
Kamal Heibbb8865f2020-01-30 10:20:49 +02002805 return ERR_PTR(-EOPNOTSUPP);
Eli Cohene126ba92013-07-07 17:25:49 +03002806 }
2807
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002808 if (verbs_init_attr->qp_type == IB_QPT_DRIVER)
2809 qp->qp_sub_type = init_attr->qp_type;
2810
Eli Cohene126ba92013-07-07 17:25:49 +03002811 return &qp->ibqp;
2812}
2813
Moni Shoua776a3902018-01-02 16:19:33 +02002814static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2815{
2816 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2817
2818 if (mqp->state == IB_QPS_RTR) {
2819 int err;
2820
2821 err = mlx5_core_destroy_dct(dev->mdev, &mqp->dct.mdct);
2822 if (err) {
2823 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2824 return err;
2825 }
2826 }
2827
2828 kfree(mqp->dct.in);
2829 kfree(mqp);
2830 return 0;
2831}
2832
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002833int mlx5_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002834{
2835 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2836 struct mlx5_ib_qp *mqp = to_mqp(qp);
2837
Haggai Erand16e91d2016-02-29 15:45:05 +02002838 if (unlikely(qp->qp_type == IB_QPT_GSI))
2839 return mlx5_ib_gsi_destroy_qp(qp);
2840
Moni Shoua776a3902018-01-02 16:19:33 +02002841 if (mqp->qp_sub_type == MLX5_IB_QPT_DCT)
2842 return mlx5_ib_destroy_dct(mqp);
2843
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002844 destroy_qp_common(dev, mqp, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002845
2846 kfree(mqp);
2847
2848 return 0;
2849}
2850
Yonatan Cohena60109d2018-10-10 09:25:16 +03002851static int to_mlx5_access_flags(struct mlx5_ib_qp *qp,
2852 const struct ib_qp_attr *attr,
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002853 int attr_mask, __be32 *hw_access_flags_be)
Eli Cohene126ba92013-07-07 17:25:49 +03002854{
Eli Cohene126ba92013-07-07 17:25:49 +03002855 u8 dest_rd_atomic;
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002856 u32 access_flags, hw_access_flags = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002857
Yonatan Cohena60109d2018-10-10 09:25:16 +03002858 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
2859
Eli Cohene126ba92013-07-07 17:25:49 +03002860 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2861 dest_rd_atomic = attr->max_dest_rd_atomic;
2862 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002863 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002864
2865 if (attr_mask & IB_QP_ACCESS_FLAGS)
2866 access_flags = attr->qp_access_flags;
2867 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002868 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002869
2870 if (!dest_rd_atomic)
2871 access_flags &= IB_ACCESS_REMOTE_WRITE;
2872
2873 if (access_flags & IB_ACCESS_REMOTE_READ)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002874 hw_access_flags |= MLX5_QP_BIT_RRE;
Yonatan Cohen13f8d9c2018-11-21 13:48:39 +02002875 if (access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03002876 int atomic_mode;
Eli Cohene126ba92013-07-07 17:25:49 +03002877
Yonatan Cohena60109d2018-10-10 09:25:16 +03002878 atomic_mode = get_atomic_mode(dev, qp->ibqp.qp_type);
2879 if (atomic_mode < 0)
2880 return -EOPNOTSUPP;
2881
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002882 hw_access_flags |= MLX5_QP_BIT_RAE;
2883 hw_access_flags |= atomic_mode << MLX5_ATOMIC_MODE_OFFSET;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002884 }
2885
2886 if (access_flags & IB_ACCESS_REMOTE_WRITE)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002887 hw_access_flags |= MLX5_QP_BIT_RWE;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002888
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002889 *hw_access_flags_be = cpu_to_be32(hw_access_flags);
Yonatan Cohena60109d2018-10-10 09:25:16 +03002890
2891 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002892}
2893
2894enum {
2895 MLX5_PATH_FLAG_FL = 1 << 0,
2896 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2897 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2898};
2899
2900static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2901{
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002902 if (rate == IB_RATE_PORT_CURRENT)
Eli Cohene126ba92013-07-07 17:25:49 +03002903 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002904
Michael Guralnika5a5d192018-12-09 11:49:50 +02002905 if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_600_GBPS)
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002906 return -EINVAL;
2907
2908 while (rate != IB_RATE_PORT_CURRENT &&
2909 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
2910 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
2911 --rate;
2912
2913 return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
Eli Cohene126ba92013-07-07 17:25:49 +03002914}
2915
majd@mellanox.com75850d02016-01-14 19:13:06 +02002916static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002917 struct mlx5_ib_sq *sq, u8 sl,
2918 struct ib_pd *pd)
majd@mellanox.com75850d02016-01-14 19:13:06 +02002919{
2920 void *in;
2921 void *tisc;
2922 int inlen;
2923 int err;
2924
2925 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002926 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002927 if (!in)
2928 return -ENOMEM;
2929
2930 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002931 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002932
2933 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2934 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2935
2936 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2937
2938 kvfree(in);
2939
2940 return err;
2941}
2942
Aviv Heller13eab212016-09-18 20:48:04 +03002943static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002944 struct mlx5_ib_sq *sq, u8 tx_affinity,
2945 struct ib_pd *pd)
Aviv Heller13eab212016-09-18 20:48:04 +03002946{
2947 void *in;
2948 void *tisc;
2949 int inlen;
2950 int err;
2951
2952 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002953 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002954 if (!in)
2955 return -ENOMEM;
2956
2957 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002958 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
Aviv Heller13eab212016-09-18 20:48:04 +03002959
2960 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2961 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2962
2963 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2964
2965 kvfree(in);
2966
2967 return err;
2968}
2969
majd@mellanox.com75850d02016-01-14 19:13:06 +02002970static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002971 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002972 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002973 u32 path_flags, const struct ib_qp_attr *attr,
2974 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002975{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002976 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002977 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002978 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002979 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2980 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002981
Eli Cohene126ba92013-07-07 17:25:49 +03002982 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002983 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2984 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002985
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002986 if (ah_flags & IB_AH_GRH) {
2987 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002988 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002989 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002990 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002991 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002992 return -EINVAL;
2993 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002994 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002995
2996 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002997 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002998 return -EINVAL;
Parav Pandit47ec3862018-06-13 10:22:06 +03002999
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04003000 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02003001 if (qp->ibqp.qp_type == IB_QPT_RC ||
3002 qp->ibqp.qp_type == IB_QPT_UC ||
3003 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
3004 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
Parav Pandit47ec3862018-06-13 10:22:06 +03003005 path->udp_sport =
3006 mlx5_get_roce_udp_sport(dev, ah->grh.sgid_attr);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003007 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Parav Pandit47ec3862018-06-13 10:22:06 +03003008 gid_type = ah->grh.sgid_attr->gid_type;
Majd Dibbinyed884512017-01-18 14:10:35 +02003009 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003010 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003011 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003012 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
3013 path->fl_free_ar |=
3014 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003015 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
3016 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
3017 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02003018 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003019 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003020 }
3021
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003022 if (ah_flags & IB_AH_GRH) {
3023 path->mgid_index = grh->sgid_index;
3024 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03003025 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003026 cpu_to_be32((grh->traffic_class << 20) |
3027 (grh->flow_label));
3028 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003029 }
3030
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04003031 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03003032 if (err < 0)
3033 return err;
3034 path->static_rate = err;
3035 path->port = port;
3036
Eli Cohene126ba92013-07-07 17:25:49 +03003037 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03003038 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03003039
majd@mellanox.com75850d02016-01-14 19:13:06 +02003040 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
3041 return modify_raw_packet_eth_prio(dev->mdev,
3042 &qp->raw_packet_qp.sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003043 sl & 0xf, qp->ibqp.pd);
majd@mellanox.com75850d02016-01-14 19:13:06 +02003044
Eli Cohene126ba92013-07-07 17:25:49 +03003045 return 0;
3046}
3047
3048static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
3049 [MLX5_QP_STATE_INIT] = {
3050 [MLX5_QP_STATE_INIT] = {
3051 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3052 MLX5_QP_OPTPAR_RAE |
3053 MLX5_QP_OPTPAR_RWE |
3054 MLX5_QP_OPTPAR_PKEY_INDEX |
3055 MLX5_QP_OPTPAR_PRI_PORT,
3056 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
3057 MLX5_QP_OPTPAR_PKEY_INDEX |
3058 MLX5_QP_OPTPAR_PRI_PORT,
3059 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3060 MLX5_QP_OPTPAR_Q_KEY |
3061 MLX5_QP_OPTPAR_PRI_PORT,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003062 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3063 MLX5_QP_OPTPAR_RAE |
3064 MLX5_QP_OPTPAR_RWE |
3065 MLX5_QP_OPTPAR_PKEY_INDEX |
3066 MLX5_QP_OPTPAR_PRI_PORT,
Eli Cohene126ba92013-07-07 17:25:49 +03003067 },
3068 [MLX5_QP_STATE_RTR] = {
3069 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3070 MLX5_QP_OPTPAR_RRE |
3071 MLX5_QP_OPTPAR_RAE |
3072 MLX5_QP_OPTPAR_RWE |
3073 MLX5_QP_OPTPAR_PKEY_INDEX,
3074 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3075 MLX5_QP_OPTPAR_RWE |
3076 MLX5_QP_OPTPAR_PKEY_INDEX,
3077 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
3078 MLX5_QP_OPTPAR_Q_KEY,
3079 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
3080 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03003081 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3082 MLX5_QP_OPTPAR_RRE |
3083 MLX5_QP_OPTPAR_RAE |
3084 MLX5_QP_OPTPAR_RWE |
3085 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03003086 },
3087 },
3088 [MLX5_QP_STATE_RTR] = {
3089 [MLX5_QP_STATE_RTS] = {
3090 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3091 MLX5_QP_OPTPAR_RRE |
3092 MLX5_QP_OPTPAR_RAE |
3093 MLX5_QP_OPTPAR_RWE |
3094 MLX5_QP_OPTPAR_PM_STATE |
3095 MLX5_QP_OPTPAR_RNR_TIMEOUT,
3096 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3097 MLX5_QP_OPTPAR_RWE |
3098 MLX5_QP_OPTPAR_PM_STATE,
3099 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003100 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3101 MLX5_QP_OPTPAR_RRE |
3102 MLX5_QP_OPTPAR_RAE |
3103 MLX5_QP_OPTPAR_RWE |
3104 MLX5_QP_OPTPAR_PM_STATE |
3105 MLX5_QP_OPTPAR_RNR_TIMEOUT,
Eli Cohene126ba92013-07-07 17:25:49 +03003106 },
3107 },
3108 [MLX5_QP_STATE_RTS] = {
3109 [MLX5_QP_STATE_RTS] = {
3110 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3111 MLX5_QP_OPTPAR_RAE |
3112 MLX5_QP_OPTPAR_RWE |
3113 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03003114 MLX5_QP_OPTPAR_PM_STATE |
3115 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003116 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03003117 MLX5_QP_OPTPAR_PM_STATE |
3118 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003119 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
3120 MLX5_QP_OPTPAR_SRQN |
3121 MLX5_QP_OPTPAR_CQN_RCV,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003122 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE |
3123 MLX5_QP_OPTPAR_RAE |
3124 MLX5_QP_OPTPAR_RWE |
3125 MLX5_QP_OPTPAR_RNR_TIMEOUT |
3126 MLX5_QP_OPTPAR_PM_STATE |
3127 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003128 },
3129 },
3130 [MLX5_QP_STATE_SQER] = {
3131 [MLX5_QP_STATE_RTS] = {
3132 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
3133 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03003134 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03003135 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3136 MLX5_QP_OPTPAR_RWE |
3137 MLX5_QP_OPTPAR_RAE |
3138 MLX5_QP_OPTPAR_RRE,
Jack Morgenstein8f4426a2019-05-01 08:38:30 +03003139 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3140 MLX5_QP_OPTPAR_RWE |
3141 MLX5_QP_OPTPAR_RAE |
3142 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03003143 },
3144 },
3145};
3146
3147static int ib_nr_to_mlx5_nr(int ib_mask)
3148{
3149 switch (ib_mask) {
3150 case IB_QP_STATE:
3151 return 0;
3152 case IB_QP_CUR_STATE:
3153 return 0;
3154 case IB_QP_EN_SQD_ASYNC_NOTIFY:
3155 return 0;
3156 case IB_QP_ACCESS_FLAGS:
3157 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
3158 MLX5_QP_OPTPAR_RAE;
3159 case IB_QP_PKEY_INDEX:
3160 return MLX5_QP_OPTPAR_PKEY_INDEX;
3161 case IB_QP_PORT:
3162 return MLX5_QP_OPTPAR_PRI_PORT;
3163 case IB_QP_QKEY:
3164 return MLX5_QP_OPTPAR_Q_KEY;
3165 case IB_QP_AV:
3166 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
3167 MLX5_QP_OPTPAR_PRI_PORT;
3168 case IB_QP_PATH_MTU:
3169 return 0;
3170 case IB_QP_TIMEOUT:
3171 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
3172 case IB_QP_RETRY_CNT:
3173 return MLX5_QP_OPTPAR_RETRY_COUNT;
3174 case IB_QP_RNR_RETRY:
3175 return MLX5_QP_OPTPAR_RNR_RETRY;
3176 case IB_QP_RQ_PSN:
3177 return 0;
3178 case IB_QP_MAX_QP_RD_ATOMIC:
3179 return MLX5_QP_OPTPAR_SRA_MAX;
3180 case IB_QP_ALT_PATH:
3181 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
3182 case IB_QP_MIN_RNR_TIMER:
3183 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
3184 case IB_QP_SQ_PSN:
3185 return 0;
3186 case IB_QP_MAX_DEST_RD_ATOMIC:
3187 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
3188 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
3189 case IB_QP_PATH_MIG_STATE:
3190 return MLX5_QP_OPTPAR_PM_STATE;
3191 case IB_QP_CAP:
3192 return 0;
3193 case IB_QP_DEST_QPN:
3194 return 0;
3195 }
3196 return 0;
3197}
3198
3199static int ib_mask_to_mlx5_opt(int ib_mask)
3200{
3201 int result = 0;
3202 int i;
3203
3204 for (i = 0; i < 8 * sizeof(int); i++) {
3205 if ((1 << i) & ib_mask)
3206 result |= ib_nr_to_mlx5_nr(1 << i);
3207 }
3208
3209 return result;
3210}
3211
Yishai Hadas34d57582018-09-20 21:39:21 +03003212static int modify_raw_packet_qp_rq(
3213 struct mlx5_ib_dev *dev, struct mlx5_ib_rq *rq, int new_state,
3214 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003215{
3216 void *in;
3217 void *rqc;
3218 int inlen;
3219 int err;
3220
3221 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003222 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003223 if (!in)
3224 return -ENOMEM;
3225
3226 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
Yishai Hadas34d57582018-09-20 21:39:21 +03003227 MLX5_SET(modify_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003228
3229 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
3230 MLX5_SET(rqc, rqc, state, new_state);
3231
Alex Veskereb49ab02016-08-28 12:25:53 +03003232 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
3233 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
3234 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02003235 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03003236 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
3237 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06003238 dev_info_once(
3239 &dev->ib_dev.dev,
3240 "RAW PACKET QP counters are not supported on current FW\n");
Alex Veskereb49ab02016-08-28 12:25:53 +03003241 }
3242
3243 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003244 if (err)
3245 goto out;
3246
3247 rq->state = new_state;
3248
3249out:
3250 kvfree(in);
3251 return err;
3252}
3253
Yishai Hadasc14003f2018-09-20 21:39:22 +03003254static int modify_raw_packet_qp_sq(
3255 struct mlx5_core_dev *dev, struct mlx5_ib_sq *sq, int new_state,
3256 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003257{
Bodong Wang7d29f342016-12-01 13:43:16 +02003258 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
Bodong Wang61147f32018-03-19 15:10:30 +02003259 struct mlx5_rate_limit old_rl = ibqp->rl;
3260 struct mlx5_rate_limit new_rl = old_rl;
3261 bool new_rate_added = false;
Bodong Wang7d29f342016-12-01 13:43:16 +02003262 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003263 void *in;
3264 void *sqc;
3265 int inlen;
3266 int err;
3267
3268 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003269 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003270 if (!in)
3271 return -ENOMEM;
3272
Yishai Hadasc14003f2018-09-20 21:39:22 +03003273 MLX5_SET(modify_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003274 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
3275
3276 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
3277 MLX5_SET(sqc, sqc, state, new_state);
3278
Bodong Wang7d29f342016-12-01 13:43:16 +02003279 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
3280 if (new_state != MLX5_SQC_STATE_RDY)
3281 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
3282 __func__);
3283 else
Bodong Wang61147f32018-03-19 15:10:30 +02003284 new_rl = raw_qp_param->rl;
Bodong Wang7d29f342016-12-01 13:43:16 +02003285 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003286
Bodong Wang61147f32018-03-19 15:10:30 +02003287 if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
3288 if (new_rl.rate) {
3289 err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003290 if (err) {
Bodong Wang61147f32018-03-19 15:10:30 +02003291 pr_err("Failed configuring rate limit(err %d): \
3292 rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
3293 err, new_rl.rate, new_rl.max_burst_sz,
3294 new_rl.typical_pkt_sz);
3295
Bodong Wang7d29f342016-12-01 13:43:16 +02003296 goto out;
3297 }
Bodong Wang61147f32018-03-19 15:10:30 +02003298 new_rate_added = true;
Bodong Wang7d29f342016-12-01 13:43:16 +02003299 }
3300
3301 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
Bodong Wang61147f32018-03-19 15:10:30 +02003302 /* index 0 means no limit */
Bodong Wang7d29f342016-12-01 13:43:16 +02003303 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
3304 }
3305
3306 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
3307 if (err) {
3308 /* Remove new rate from table if failed */
Bodong Wang61147f32018-03-19 15:10:30 +02003309 if (new_rate_added)
3310 mlx5_rl_remove_rate(dev, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003311 goto out;
3312 }
3313
3314 /* Only remove the old rate after new rate was set */
Rafi Wienerc8973df2019-10-02 15:02:43 +03003315 if ((old_rl.rate && !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
3316 (new_state != MLX5_SQC_STATE_RDY)) {
Bodong Wang61147f32018-03-19 15:10:30 +02003317 mlx5_rl_remove_rate(dev, &old_rl);
Rafi Wienerc8973df2019-10-02 15:02:43 +03003318 if (new_state != MLX5_SQC_STATE_RDY)
3319 memset(&new_rl, 0, sizeof(new_rl));
3320 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003321
Bodong Wang61147f32018-03-19 15:10:30 +02003322 ibqp->rl = new_rl;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003323 sq->state = new_state;
3324
3325out:
3326 kvfree(in);
3327 return err;
3328}
3329
3330static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03003331 const struct mlx5_modify_raw_qp_param *raw_qp_param,
3332 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003333{
3334 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
3335 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
3336 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02003337 int modify_rq = !!qp->rq.wqe_cnt;
3338 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003339 int rq_state;
3340 int sq_state;
3341 int err;
3342
Alex Vesker0680efa2016-08-28 12:25:52 +03003343 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003344 case MLX5_CMD_OP_RST2INIT_QP:
3345 rq_state = MLX5_RQC_STATE_RDY;
3346 sq_state = MLX5_SQC_STATE_RDY;
3347 break;
3348 case MLX5_CMD_OP_2ERR_QP:
3349 rq_state = MLX5_RQC_STATE_ERR;
3350 sq_state = MLX5_SQC_STATE_ERR;
3351 break;
3352 case MLX5_CMD_OP_2RST_QP:
3353 rq_state = MLX5_RQC_STATE_RST;
3354 sq_state = MLX5_SQC_STATE_RST;
3355 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003356 case MLX5_CMD_OP_RTR2RTS_QP:
3357 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02003358 if (raw_qp_param->set_mask ==
3359 MLX5_RAW_QP_RATE_LIMIT) {
3360 modify_rq = 0;
3361 sq_state = sq->state;
3362 } else {
3363 return raw_qp_param->set_mask ? -EINVAL : 0;
3364 }
3365 break;
3366 case MLX5_CMD_OP_INIT2INIT_QP:
3367 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03003368 if (raw_qp_param->set_mask)
3369 return -EINVAL;
3370 else
3371 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003372 default:
3373 WARN_ON(1);
3374 return -EINVAL;
3375 }
3376
Bodong Wang7d29f342016-12-01 13:43:16 +02003377 if (modify_rq) {
Yishai Hadas34d57582018-09-20 21:39:21 +03003378 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param,
3379 qp->ibqp.pd);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003380 if (err)
3381 return err;
3382 }
3383
Bodong Wang7d29f342016-12-01 13:43:16 +02003384 if (modify_sq) {
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003385 struct mlx5_flow_handle *flow_rule;
3386
Aviv Heller13eab212016-09-18 20:48:04 +03003387 if (tx_affinity) {
3388 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003389 tx_affinity,
3390 qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003391 if (err)
3392 return err;
3393 }
3394
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003395 flow_rule = create_flow_rule_vport_sq(dev, sq,
3396 raw_qp_param->port);
3397 if (IS_ERR(flow_rule))
Colin Ian King1db86312019-04-12 11:40:17 +01003398 return PTR_ERR(flow_rule);
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003399
3400 err = modify_raw_packet_qp_sq(dev->mdev, sq, sq_state,
3401 raw_qp_param, qp->ibqp.pd);
3402 if (err) {
3403 if (flow_rule)
3404 mlx5_del_flow_rules(flow_rule);
3405 return err;
3406 }
3407
3408 if (flow_rule) {
3409 destroy_flow_rule_vport_sq(sq);
3410 sq->flow_rule = flow_rule;
3411 }
3412
3413 return err;
Aviv Heller13eab212016-09-18 20:48:04 +03003414 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003415
3416 return 0;
3417}
3418
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003419static unsigned int get_tx_affinity(struct mlx5_ib_dev *dev,
3420 struct mlx5_ib_pd *pd,
3421 struct mlx5_ib_qp_base *qp_base,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003422 u8 port_num, struct ib_udata *udata)
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003423{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003424 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
3425 udata, struct mlx5_ib_ucontext, ibucontext);
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003426 unsigned int tx_port_affinity;
3427
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003428 if (ucontext) {
3429 tx_port_affinity = (unsigned int)atomic_add_return(
3430 1, &ucontext->tx_port_affinity) %
3431 MLX5_MAX_PORTS +
3432 1;
3433 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x ucontext %p\n",
3434 tx_port_affinity, qp_base->mqp.qpn, ucontext);
3435 } else {
3436 tx_port_affinity =
3437 (unsigned int)atomic_add_return(
Mark Bloch95579e72019-03-28 15:27:33 +02003438 1, &dev->port[port_num].roce.tx_port_affinity) %
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003439 MLX5_MAX_PORTS +
3440 1;
3441 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x\n",
3442 tx_port_affinity, qp_base->mqp.qpn);
3443 }
3444
3445 return tx_port_affinity;
3446}
3447
Mark Zhangd14133d2019-07-02 13:02:36 +03003448static int __mlx5_ib_qp_set_counter(struct ib_qp *qp,
3449 struct rdma_counter *counter)
3450{
3451 struct mlx5_ib_dev *dev = to_mdev(qp->device);
3452 struct mlx5_ib_qp *mqp = to_mqp(qp);
3453 struct mlx5_qp_context context = {};
Mark Zhangd14133d2019-07-02 13:02:36 +03003454 struct mlx5_ib_qp_base *base;
3455 u32 set_id;
3456
Parav Pandit3e1f0002019-07-23 10:31:17 +03003457 if (counter)
Mark Zhangd14133d2019-07-02 13:02:36 +03003458 set_id = counter->id;
Parav Pandit3e1f0002019-07-23 10:31:17 +03003459 else
3460 set_id = mlx5_ib_get_counters_id(dev, mqp->port - 1);
Mark Zhangd14133d2019-07-02 13:02:36 +03003461
3462 base = &mqp->trans_qp.base;
3463 context.qp_counter_set_usr_page &= cpu_to_be32(0xffffff);
3464 context.qp_counter_set_usr_page |= cpu_to_be32(set_id << 24);
3465 return mlx5_core_qp_modify(dev->mdev,
3466 MLX5_CMD_OP_RTS2RTS_QP,
3467 MLX5_QP_OPTPAR_COUNTER_SET_ID,
3468 &context, &base->mqp);
3469}
3470
Eli Cohene126ba92013-07-07 17:25:49 +03003471static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
3472 const struct ib_qp_attr *attr, int attr_mask,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003473 enum ib_qp_state cur_state,
3474 enum ib_qp_state new_state,
3475 const struct mlx5_ib_modify_qp *ucmd,
3476 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003477{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003478 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
3479 [MLX5_QP_STATE_RST] = {
3480 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3481 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3482 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
3483 },
3484 [MLX5_QP_STATE_INIT] = {
3485 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3486 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3487 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
3488 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
3489 },
3490 [MLX5_QP_STATE_RTR] = {
3491 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3492 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3493 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
3494 },
3495 [MLX5_QP_STATE_RTS] = {
3496 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3497 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3498 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
3499 },
3500 [MLX5_QP_STATE_SQD] = {
3501 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3502 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3503 },
3504 [MLX5_QP_STATE_SQER] = {
3505 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3506 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3507 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
3508 },
3509 [MLX5_QP_STATE_ERR] = {
3510 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3511 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3512 }
3513 };
3514
Eli Cohene126ba92013-07-07 17:25:49 +03003515 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3516 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02003517 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03003518 struct mlx5_ib_cq *send_cq, *recv_cq;
3519 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03003520 struct mlx5_ib_pd *pd;
3521 enum mlx5_qp_state mlx5_cur, mlx5_new;
3522 enum mlx5_qp_optpar optpar;
Mark Zhangd14133d2019-07-02 13:02:36 +03003523 u32 set_id = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003524 int mlx5_st;
3525 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003526 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03003527 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003528
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003529 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
3530 qp->qp_sub_type : ibqp->qp_type);
3531 if (mlx5_st < 0)
3532 return -EINVAL;
3533
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003534 context = kzalloc(sizeof(*context), GFP_KERNEL);
3535 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03003536 return -ENOMEM;
3537
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003538 pd = get_pd(qp);
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003539 context->flags = cpu_to_be32(mlx5_st << 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003540
3541 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
3542 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3543 } else {
3544 switch (attr->path_mig_state) {
3545 case IB_MIG_MIGRATED:
3546 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3547 break;
3548 case IB_MIG_REARM:
3549 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
3550 break;
3551 case IB_MIG_ARMED:
3552 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
3553 break;
3554 }
3555 }
3556
Aviv Heller13eab212016-09-18 20:48:04 +03003557 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
3558 if ((ibqp->qp_type == IB_QPT_RC) ||
3559 (ibqp->qp_type == IB_QPT_UD &&
3560 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
3561 (ibqp->qp_type == IB_QPT_UC) ||
3562 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
3563 (ibqp->qp_type == IB_QPT_XRC_INI) ||
3564 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
Aviv Heller7c34ec12018-08-23 13:47:53 +03003565 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02003566 u8 p = mlx5_core_native_port_num(dev->mdev) - 1;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003567 tx_affinity = get_tx_affinity(dev, pd, base, p,
3568 udata);
Aviv Heller13eab212016-09-18 20:48:04 +03003569 context->flags |= cpu_to_be32(tx_affinity << 24);
3570 }
3571 }
3572 }
3573
Haggai Erand16e91d2016-02-29 15:45:05 +02003574 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003575 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003576 } else if ((ibqp->qp_type == IB_QPT_UD &&
3577 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003578 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
3579 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
3580 } else if (attr_mask & IB_QP_PATH_MTU) {
3581 if (attr->path_mtu < IB_MTU_256 ||
3582 attr->path_mtu > IB_MTU_4096) {
3583 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3584 err = -EINVAL;
3585 goto out;
3586 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003587 context->mtu_msgmax = (attr->path_mtu << 5) |
3588 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03003589 }
3590
3591 if (attr_mask & IB_QP_DEST_QPN)
3592 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
3593
3594 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003595 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003596
3597 /* todo implement counter_index functionality */
3598
3599 if (is_sqp(ibqp->qp_type))
3600 context->pri_path.port = qp->port;
3601
3602 if (attr_mask & IB_QP_PORT)
3603 context->pri_path.port = attr->port_num;
3604
3605 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003606 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003607 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003608 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003609 if (err)
3610 goto out;
3611 }
3612
3613 if (attr_mask & IB_QP_TIMEOUT)
3614 context->pri_path.ackto_lt |= attr->timeout << 3;
3615
3616 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003617 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3618 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003619 attr->alt_port_num,
3620 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3621 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003622 if (err)
3623 goto out;
3624 }
3625
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003626 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3627 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003628
3629 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3630 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3631 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3632 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3633
3634 if (attr_mask & IB_QP_RNR_RETRY)
3635 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3636
3637 if (attr_mask & IB_QP_RETRY_CNT)
3638 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3639
3640 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3641 if (attr->max_rd_atomic)
3642 context->params1 |=
3643 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3644 }
3645
3646 if (attr_mask & IB_QP_SQ_PSN)
3647 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3648
3649 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3650 if (attr->max_dest_rd_atomic)
3651 context->params2 |=
3652 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3653 }
3654
Yonatan Cohena60109d2018-10-10 09:25:16 +03003655 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08003656 __be32 access_flags;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003657
3658 err = to_mlx5_access_flags(qp, attr, attr_mask, &access_flags);
3659 if (err)
3660 goto out;
3661
3662 context->params2 |= access_flags;
3663 }
Eli Cohene126ba92013-07-07 17:25:49 +03003664
3665 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3666 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3667
3668 if (attr_mask & IB_QP_RQ_PSN)
3669 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3670
3671 if (attr_mask & IB_QP_QKEY)
3672 context->qkey = cpu_to_be32(attr->qkey);
3673
3674 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3675 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3676
Mark Bloch0837e862016-06-17 15:10:55 +03003677 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3678 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3679 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003680
3681 /* Underlay port should be used - index 0 function per port */
3682 if (qp->flags & MLX5_IB_QP_UNDERLAY)
3683 port_num = 0;
3684
Mark Zhangd14133d2019-07-02 13:02:36 +03003685 if (ibqp->counter)
3686 set_id = ibqp->counter->id;
3687 else
Parav Pandit3e1f0002019-07-23 10:31:17 +03003688 set_id = mlx5_ib_get_counters_id(dev, port_num);
Mark Bloch0837e862016-06-17 15:10:55 +03003689 context->qp_counter_set_usr_page |=
Mark Zhangd14133d2019-07-02 13:02:36 +03003690 cpu_to_be32(set_id << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003691 }
3692
Eli Cohene126ba92013-07-07 17:25:49 +03003693 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3694 context->sq_crq_size |= cpu_to_be16(1 << 4);
3695
Haggai Eranb11a4f92016-02-29 15:45:03 +02003696 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
3697 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003698
3699 mlx5_cur = to_mlx5_state(cur_state);
3700 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003701
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003702 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003703 !optab[mlx5_cur][mlx5_new]) {
3704 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003705 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003706 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003707
3708 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003709 optpar = ib_mask_to_mlx5_opt(attr_mask);
3710 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003711
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003712 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3713 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003714 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3715
3716 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003717 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Mark Zhangd14133d2019-07-02 13:02:36 +03003718 raw_qp_param.rq_q_ctr_id = set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003719 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3720 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003721
Mark Blochd5ed8ac2019-03-28 15:27:38 +02003722 if (attr_mask & IB_QP_PORT)
3723 raw_qp_param.port = attr->port_num;
3724
Bodong Wang7d29f342016-12-01 13:43:16 +02003725 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003726 raw_qp_param.rl.rate = attr->rate_limit;
3727
3728 if (ucmd->burst_info.max_burst_sz) {
3729 if (attr->rate_limit &&
3730 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3731 raw_qp_param.rl.max_burst_sz =
3732 ucmd->burst_info.max_burst_sz;
3733 } else {
3734 err = -EINVAL;
3735 goto out;
3736 }
3737 }
3738
3739 if (ucmd->burst_info.typical_pkt_sz) {
3740 if (attr->rate_limit &&
3741 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3742 raw_qp_param.rl.typical_pkt_sz =
3743 ucmd->burst_info.typical_pkt_sz;
3744 } else {
3745 err = -EINVAL;
3746 goto out;
3747 }
3748 }
3749
Bodong Wang7d29f342016-12-01 13:43:16 +02003750 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3751 }
3752
Aviv Heller13eab212016-09-18 20:48:04 +03003753 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003754 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003755 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003756 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003757 }
3758
Eli Cohene126ba92013-07-07 17:25:49 +03003759 if (err)
3760 goto out;
3761
3762 qp->state = new_state;
3763
3764 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003765 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003766 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003767 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003768 if (attr_mask & IB_QP_PORT)
3769 qp->port = attr->port_num;
3770 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003771 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003772
3773 /*
3774 * If we moved a kernel QP to RESET, clean up all old CQ
3775 * entries and reinitialize the QP.
3776 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02003777 if (new_state == IB_QPS_RESET &&
3778 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003779 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003780 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3781 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003782 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003783
3784 qp->rq.head = 0;
3785 qp->rq.tail = 0;
3786 qp->sq.head = 0;
3787 qp->sq.tail = 0;
3788 qp->sq.cur_post = 0;
Guy Levi34f4c952018-11-26 08:15:50 +02003789 if (qp->sq.wqe_cnt)
3790 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
Leon Romanovsky950bf4f2020-03-18 11:16:40 +02003791 qp->sq.last_poll = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003792 qp->db.db[MLX5_RCV_DBR] = 0;
3793 qp->db.db[MLX5_SND_DBR] = 0;
3794 }
3795
Mark Zhangd14133d2019-07-02 13:02:36 +03003796 if ((new_state == IB_QPS_RTS) && qp->counter_pending) {
3797 err = __mlx5_ib_qp_set_counter(ibqp, ibqp->counter);
3798 if (!err)
3799 qp->counter_pending = 0;
3800 }
3801
Eli Cohene126ba92013-07-07 17:25:49 +03003802out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003803 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003804 return err;
3805}
3806
Moni Shouac32a4f22018-01-02 16:19:32 +02003807static inline bool is_valid_mask(int mask, int req, int opt)
3808{
3809 if ((mask & req) != req)
3810 return false;
3811
3812 if (mask & ~(req | opt))
3813 return false;
3814
3815 return true;
3816}
3817
3818/* check valid transition for driver QP types
3819 * for now the only QP type that this function supports is DCI
3820 */
3821static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3822 enum ib_qp_attr_mask attr_mask)
3823{
3824 int req = IB_QP_STATE;
3825 int opt = 0;
3826
Moni Shoua99ed7482018-09-12 09:33:55 +03003827 if (new_state == IB_QPS_RESET) {
3828 return is_valid_mask(attr_mask, req, opt);
3829 } else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Moni Shouac32a4f22018-01-02 16:19:32 +02003830 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
3831 return is_valid_mask(attr_mask, req, opt);
3832 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3833 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3834 return is_valid_mask(attr_mask, req, opt);
3835 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3836 req |= IB_QP_PATH_MTU;
Artemy Kovalyov5ec03042018-11-05 08:12:07 +02003837 opt = IB_QP_PKEY_INDEX | IB_QP_AV;
Moni Shouac32a4f22018-01-02 16:19:32 +02003838 return is_valid_mask(attr_mask, req, opt);
3839 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3840 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3841 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3842 opt = IB_QP_MIN_RNR_TIMER;
3843 return is_valid_mask(attr_mask, req, opt);
3844 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3845 opt = IB_QP_MIN_RNR_TIMER;
3846 return is_valid_mask(attr_mask, req, opt);
3847 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3848 return is_valid_mask(attr_mask, req, opt);
3849 }
3850 return false;
3851}
3852
Moni Shoua776a3902018-01-02 16:19:33 +02003853/* mlx5_ib_modify_dct: modify a DCT QP
3854 * valid transitions are:
3855 * RESET to INIT: must set access_flags, pkey_index and port
3856 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3857 * mtu, gid_index and hop_limit
3858 * Other transitions and attributes are illegal
3859 */
3860static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3861 int attr_mask, struct ib_udata *udata)
3862{
3863 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3864 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3865 enum ib_qp_state cur_state, new_state;
3866 int err = 0;
3867 int required = IB_QP_STATE;
3868 void *dctc;
3869
3870 if (!(attr_mask & IB_QP_STATE))
3871 return -EINVAL;
3872
3873 cur_state = qp->state;
3874 new_state = attr->qp_state;
3875
3876 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3877 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03003878 u16 set_id;
3879
Moni Shoua776a3902018-01-02 16:19:33 +02003880 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3881 if (!is_valid_mask(attr_mask, required, 0))
3882 return -EINVAL;
3883
3884 if (attr->port_num == 0 ||
3885 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3886 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3887 attr->port_num, dev->num_ports);
3888 return -EINVAL;
3889 }
3890 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3891 MLX5_SET(dctc, dctc, rre, 1);
3892 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3893 MLX5_SET(dctc, dctc, rwe, 1);
3894 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03003895 int atomic_mode;
3896
3897 atomic_mode = get_atomic_mode(dev, MLX5_IB_QPT_DCT);
3898 if (atomic_mode < 0)
Moni Shoua776a3902018-01-02 16:19:33 +02003899 return -EOPNOTSUPP;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003900
3901 MLX5_SET(dctc, dctc, atomic_mode, atomic_mode);
Moni Shoua776a3902018-01-02 16:19:33 +02003902 MLX5_SET(dctc, dctc, rae, 1);
Moni Shoua776a3902018-01-02 16:19:33 +02003903 }
3904 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3905 MLX5_SET(dctc, dctc, port, attr->port_num);
Parav Pandit3e1f0002019-07-23 10:31:17 +03003906
3907 set_id = mlx5_ib_get_counters_id(dev, attr->port_num - 1);
3908 MLX5_SET(dctc, dctc, counter_set_id, set_id);
Moni Shoua776a3902018-01-02 16:19:33 +02003909
3910 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3911 struct mlx5_ib_modify_qp_resp resp = {};
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003912 u32 out[MLX5_ST_SZ_DW(create_dct_out)] = {0};
Moni Shoua776a3902018-01-02 16:19:33 +02003913 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3914 sizeof(resp.dctn);
3915
3916 if (udata->outlen < min_resp_len)
3917 return -EINVAL;
3918 resp.response_length = min_resp_len;
3919
3920 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3921 if (!is_valid_mask(attr_mask, required, 0))
3922 return -EINVAL;
3923 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3924 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3925 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3926 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3927 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3928 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3929
3930 err = mlx5_core_create_dct(dev->mdev, &qp->dct.mdct, qp->dct.in,
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003931 MLX5_ST_SZ_BYTES(create_dct_in), out,
3932 sizeof(out));
Moni Shoua776a3902018-01-02 16:19:33 +02003933 if (err)
3934 return err;
3935 resp.dctn = qp->dct.mdct.mqp.qpn;
3936 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3937 if (err) {
3938 mlx5_core_destroy_dct(dev->mdev, &qp->dct.mdct);
3939 return err;
3940 }
3941 } else {
3942 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3943 return -EINVAL;
3944 }
3945 if (err)
3946 qp->state = IB_QPS_ERR;
3947 else
3948 qp->state = new_state;
3949 return err;
3950}
3951
Eli Cohene126ba92013-07-07 17:25:49 +03003952int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3953 int attr_mask, struct ib_udata *udata)
3954{
3955 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3956 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02003957 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02003958 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003959 enum ib_qp_state cur_state, new_state;
Bodong Wang61147f32018-03-19 15:10:30 +02003960 size_t required_cmd_sz;
Eli Cohene126ba92013-07-07 17:25:49 +03003961 int err = -EINVAL;
3962 int port;
3963
Yishai Hadas28d61372016-05-23 15:20:56 +03003964 if (ibqp->rwq_ind_tbl)
3965 return -ENOSYS;
3966
Bodong Wang61147f32018-03-19 15:10:30 +02003967 if (udata && udata->inlen) {
3968 required_cmd_sz = offsetof(typeof(ucmd), reserved) +
3969 sizeof(ucmd.reserved);
3970 if (udata->inlen < required_cmd_sz)
3971 return -EINVAL;
3972
3973 if (udata->inlen > sizeof(ucmd) &&
3974 !ib_is_udata_cleared(udata, sizeof(ucmd),
3975 udata->inlen - sizeof(ucmd)))
3976 return -EOPNOTSUPP;
3977
3978 if (ib_copy_from_udata(&ucmd, udata,
3979 min(udata->inlen, sizeof(ucmd))))
3980 return -EFAULT;
3981
3982 if (ucmd.comp_mask ||
3983 memchr_inv(&ucmd.reserved, 0, sizeof(ucmd.reserved)) ||
3984 memchr_inv(&ucmd.burst_info.reserved, 0,
3985 sizeof(ucmd.burst_info.reserved)))
3986 return -EOPNOTSUPP;
3987 }
3988
Haggai Erand16e91d2016-02-29 15:45:05 +02003989 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3990 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3991
Moni Shouac32a4f22018-01-02 16:19:32 +02003992 if (ibqp->qp_type == IB_QPT_DRIVER)
3993 qp_type = qp->qp_sub_type;
3994 else
3995 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3996 IB_QPT_GSI : ibqp->qp_type;
3997
Moni Shoua776a3902018-01-02 16:19:33 +02003998 if (qp_type == MLX5_IB_QPT_DCT)
3999 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02004000
Eli Cohene126ba92013-07-07 17:25:49 +03004001 mutex_lock(&qp->mutex);
4002
4003 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
4004 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
4005
Achiad Shochat2811ba52015-12-23 18:47:24 +02004006 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
4007 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02004008 }
4009
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004010 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
4011 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
4012 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
4013 attr_mask);
4014 goto out;
4015 }
4016 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02004017 qp_type != MLX5_IB_QPT_DCI &&
Kamal Heibd31131b2018-10-02 16:11:21 +03004018 !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
4019 attr_mask)) {
Haggai Eran158abf82016-02-29 15:45:04 +02004020 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
4021 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03004022 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02004023 } else if (qp_type == MLX5_IB_QPT_DCI &&
4024 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
4025 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
4026 cur_state, new_state, qp_type, attr_mask);
4027 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004028 }
Eli Cohene126ba92013-07-07 17:25:49 +03004029
4030 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004031 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02004032 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02004033 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
4034 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03004035 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004036 }
Eli Cohene126ba92013-07-07 17:25:49 +03004037
4038 if (attr_mask & IB_QP_PKEY_INDEX) {
4039 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03004040 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02004041 dev->mdev->port_caps[port - 1].pkey_table_len) {
4042 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
4043 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004044 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004045 }
Eli Cohene126ba92013-07-07 17:25:49 +03004046 }
4047
4048 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004049 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02004050 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
4051 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
4052 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03004053 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004054 }
Eli Cohene126ba92013-07-07 17:25:49 +03004055
4056 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03004057 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02004058 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
4059 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
4060 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03004061 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02004062 }
Eli Cohene126ba92013-07-07 17:25:49 +03004063
4064 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
4065 err = 0;
4066 goto out;
4067 }
4068
Bodong Wang61147f32018-03-19 15:10:30 +02004069 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02004070 new_state, &ucmd, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03004071
4072out:
4073 mutex_unlock(&qp->mutex);
4074 return err;
4075}
4076
Guy Levi34f4c952018-11-26 08:15:50 +02004077static void _handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
4078 u32 wqe_sz, void **cur_edge)
4079{
4080 u32 idx;
4081
4082 idx = (sq->cur_post + (wqe_sz >> 2)) & (sq->wqe_cnt - 1);
4083 *cur_edge = get_sq_edge(sq, idx);
4084
4085 *seg = mlx5_frag_buf_get_wqe(&sq->fbc, idx);
4086}
4087
4088/* handle_post_send_edge - Check if we get to SQ edge. If yes, update to the
4089 * next nearby edge and get new address translation for current WQE position.
4090 * @sq - SQ buffer.
4091 * @seg: Current WQE position (16B aligned).
4092 * @wqe_sz: Total current WQE size [16B].
4093 * @cur_edge: Updated current edge.
4094 */
4095static inline void handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
4096 u32 wqe_sz, void **cur_edge)
4097{
4098 if (likely(*seg != *cur_edge))
4099 return;
4100
4101 _handle_post_send_edge(sq, seg, wqe_sz, cur_edge);
4102}
4103
4104/* memcpy_send_wqe - copy data from src to WQE and update the relevant WQ's
4105 * pointers. At the end @seg is aligned to 16B regardless the copied size.
4106 * @sq - SQ buffer.
4107 * @cur_edge: Updated current edge.
4108 * @seg: Current WQE position (16B aligned).
4109 * @wqe_sz: Total current WQE size [16B].
4110 * @src: Pointer to copy from.
4111 * @n: Number of bytes to copy.
4112 */
4113static inline void memcpy_send_wqe(struct mlx5_ib_wq *sq, void **cur_edge,
4114 void **seg, u32 *wqe_sz, const void *src,
4115 size_t n)
4116{
4117 while (likely(n)) {
4118 size_t leftlen = *cur_edge - *seg;
4119 size_t copysz = min_t(size_t, leftlen, n);
4120 size_t stride;
4121
4122 memcpy(*seg, src, copysz);
4123
4124 n -= copysz;
4125 src += copysz;
4126 stride = !n ? ALIGN(copysz, 16) : copysz;
4127 *seg += stride;
4128 *wqe_sz += stride >> 4;
4129 handle_post_send_edge(sq, seg, *wqe_sz, cur_edge);
4130 }
4131}
4132
Eli Cohene126ba92013-07-07 17:25:49 +03004133static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
4134{
4135 struct mlx5_ib_cq *cq;
4136 unsigned cur;
4137
4138 cur = wq->head - wq->tail;
4139 if (likely(cur + nreq < wq->max_post))
4140 return 0;
4141
4142 cq = to_mcq(ib_cq);
4143 spin_lock(&cq->lock);
4144 cur = wq->head - wq->tail;
4145 spin_unlock(&cq->lock);
4146
4147 return cur + nreq >= wq->max_post;
4148}
4149
4150static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
4151 u64 remote_addr, u32 rkey)
4152{
4153 rseg->raddr = cpu_to_be64(remote_addr);
4154 rseg->rkey = cpu_to_be32(rkey);
4155 rseg->reserved = 0;
4156}
4157
Guy Levi34f4c952018-11-26 08:15:50 +02004158static void set_eth_seg(const struct ib_send_wr *wr, struct mlx5_ib_qp *qp,
4159 void **seg, int *size, void **cur_edge)
Erez Shitritf0313962016-02-21 16:27:17 +02004160{
Guy Levi34f4c952018-11-26 08:15:50 +02004161 struct mlx5_wqe_eth_seg *eseg = *seg;
Erez Shitritf0313962016-02-21 16:27:17 +02004162
4163 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
4164
4165 if (wr->send_flags & IB_SEND_IP_CSUM)
4166 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
4167 MLX5_ETH_WQE_L4_CSUM;
4168
Erez Shitritf0313962016-02-21 16:27:17 +02004169 if (wr->opcode == IB_WR_LSO) {
4170 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Guy Levi34f4c952018-11-26 08:15:50 +02004171 size_t left, copysz;
Erez Shitritf0313962016-02-21 16:27:17 +02004172 void *pdata = ud_wr->header;
Guy Levi34f4c952018-11-26 08:15:50 +02004173 size_t stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004174
4175 left = ud_wr->hlen;
4176 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02004177 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02004178
Guy Levi34f4c952018-11-26 08:15:50 +02004179 /* memcpy_send_wqe should get a 16B align address. Hence, we
4180 * first copy up to the current edge and then, if needed,
4181 * fall-through to memcpy_send_wqe.
Erez Shitritf0313962016-02-21 16:27:17 +02004182 */
Guy Levi34f4c952018-11-26 08:15:50 +02004183 copysz = min_t(u64, *cur_edge - (void *)eseg->inline_hdr.start,
4184 left);
4185 memcpy(eseg->inline_hdr.start, pdata, copysz);
4186 stride = ALIGN(sizeof(struct mlx5_wqe_eth_seg) -
4187 sizeof(eseg->inline_hdr.start) + copysz, 16);
4188 *size += stride / 16;
4189 *seg += stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004190
Guy Levi34f4c952018-11-26 08:15:50 +02004191 if (copysz < left) {
4192 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02004193 left -= copysz;
4194 pdata += copysz;
Guy Levi34f4c952018-11-26 08:15:50 +02004195 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, pdata,
4196 left);
Erez Shitritf0313962016-02-21 16:27:17 +02004197 }
Guy Levi34f4c952018-11-26 08:15:50 +02004198
4199 return;
Erez Shitritf0313962016-02-21 16:27:17 +02004200 }
4201
Guy Levi34f4c952018-11-26 08:15:50 +02004202 *seg += sizeof(struct mlx5_wqe_eth_seg);
4203 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
Erez Shitritf0313962016-02-21 16:27:17 +02004204}
4205
Eli Cohene126ba92013-07-07 17:25:49 +03004206static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004207 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004208{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004209 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
4210 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
4211 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004212}
4213
4214static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
4215{
4216 dseg->byte_count = cpu_to_be32(sg->length);
4217 dseg->lkey = cpu_to_be32(sg->lkey);
4218 dseg->addr = cpu_to_be64(sg->addr);
4219}
4220
Artemy Kovalyov31616252017-01-02 11:37:42 +02004221static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03004222{
Artemy Kovalyov31616252017-01-02 11:37:42 +02004223 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
4224 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03004225}
4226
Moni Shoua841b07f2019-08-15 11:38:34 +03004227static __be64 frwr_mkey_mask(bool atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004228{
4229 u64 result;
4230
4231 result = MLX5_MKEY_MASK_LEN |
4232 MLX5_MKEY_MASK_PAGE_SIZE |
4233 MLX5_MKEY_MASK_START_ADDR |
4234 MLX5_MKEY_MASK_EN_RINVAL |
4235 MLX5_MKEY_MASK_KEY |
4236 MLX5_MKEY_MASK_LR |
4237 MLX5_MKEY_MASK_LW |
4238 MLX5_MKEY_MASK_RR |
4239 MLX5_MKEY_MASK_RW |
Eli Cohene126ba92013-07-07 17:25:49 +03004240 MLX5_MKEY_MASK_SMALL_FENCE |
4241 MLX5_MKEY_MASK_FREE;
4242
Moni Shoua841b07f2019-08-15 11:38:34 +03004243 if (atomic)
4244 result |= MLX5_MKEY_MASK_A;
4245
Eli Cohene126ba92013-07-07 17:25:49 +03004246 return cpu_to_be64(result);
4247}
4248
Sagi Grimberge6631812014-02-23 14:19:11 +02004249static __be64 sig_mkey_mask(void)
4250{
4251 u64 result;
4252
4253 result = MLX5_MKEY_MASK_LEN |
4254 MLX5_MKEY_MASK_PAGE_SIZE |
4255 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004256 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02004257 MLX5_MKEY_MASK_EN_RINVAL |
4258 MLX5_MKEY_MASK_KEY |
4259 MLX5_MKEY_MASK_LR |
4260 MLX5_MKEY_MASK_LW |
4261 MLX5_MKEY_MASK_RR |
4262 MLX5_MKEY_MASK_RW |
4263 MLX5_MKEY_MASK_SMALL_FENCE |
4264 MLX5_MKEY_MASK_FREE |
4265 MLX5_MKEY_MASK_BSF_EN;
4266
4267 return cpu_to_be64(result);
4268}
4269
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004270static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Moni Shoua841b07f2019-08-15 11:38:34 +03004271 struct mlx5_ib_mr *mr, u8 flags, bool atomic)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004272{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004273 int size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004274
4275 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004276
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004277 umr->flags = flags;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004278 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Moni Shoua841b07f2019-08-15 11:38:34 +03004279 umr->mkey_mask = frwr_mkey_mask(atomic);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004280}
4281
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004282static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03004283{
4284 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004285 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03004286 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03004287}
4288
Artemy Kovalyov31616252017-01-02 11:37:42 +02004289static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004290{
4291 u64 result;
4292
Artemy Kovalyov31616252017-01-02 11:37:42 +02004293 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02004294 MLX5_MKEY_MASK_FREE;
4295
4296 return cpu_to_be64(result);
4297}
4298
Artemy Kovalyov31616252017-01-02 11:37:42 +02004299static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004300{
4301 u64 result;
4302
4303 result = MLX5_MKEY_MASK_FREE;
4304
4305 return cpu_to_be64(result);
4306}
4307
Noa Osherovich56e11d62016-02-29 16:46:51 +02004308static __be64 get_umr_update_translation_mask(void)
4309{
4310 u64 result;
4311
4312 result = MLX5_MKEY_MASK_LEN |
4313 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004314 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004315
4316 return cpu_to_be64(result);
4317}
4318
Artemy Kovalyov31616252017-01-02 11:37:42 +02004319static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02004320{
4321 u64 result;
4322
Artemy Kovalyov31616252017-01-02 11:37:42 +02004323 result = MLX5_MKEY_MASK_LR |
4324 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004325 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004326 MLX5_MKEY_MASK_RW;
4327
4328 if (atomic)
4329 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004330
4331 return cpu_to_be64(result);
4332}
4333
4334static __be64 get_umr_update_pd_mask(void)
4335{
4336 u64 result;
4337
Artemy Kovalyov31616252017-01-02 11:37:42 +02004338 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004339
4340 return cpu_to_be64(result);
4341}
4342
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004343static int umr_check_mkey_mask(struct mlx5_ib_dev *dev, u64 mask)
4344{
4345 if ((mask & MLX5_MKEY_MASK_PAGE_SIZE &&
4346 MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) ||
4347 (mask & MLX5_MKEY_MASK_A &&
4348 MLX5_CAP_GEN(dev->mdev, umr_modify_atomic_disabled)))
4349 return -EPERM;
4350 return 0;
4351}
4352
4353static int set_reg_umr_segment(struct mlx5_ib_dev *dev,
4354 struct mlx5_wqe_umr_ctrl_seg *umr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004355 const struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004356{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004357 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03004358
4359 memset(umr, 0, sizeof(*umr));
4360
Yishai Hadas6a053952019-07-23 09:57:25 +03004361 if (!umrwr->ignore_free_state) {
4362 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
4363 /* fail if free */
4364 umr->flags = MLX5_UMR_CHECK_FREE;
4365 else
4366 /* fail if not free */
4367 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
4368 }
Haggai Eran968e78d2014-12-11 17:04:11 +02004369
Artemy Kovalyov31616252017-01-02 11:37:42 +02004370 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
4371 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
4372 u64 offset = get_xlt_octo(umrwr->offset);
4373
4374 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
4375 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
4376 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004377 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02004378 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
4379 umr->mkey_mask |= get_umr_update_translation_mask();
4380 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
4381 umr->mkey_mask |= get_umr_update_access_mask(atomic);
4382 umr->mkey_mask |= get_umr_update_pd_mask();
4383 }
4384 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
4385 umr->mkey_mask |= get_umr_enable_mr_mask();
4386 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
4387 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03004388
4389 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02004390 umr->flags |= MLX5_UMR_INLINE;
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004391
4392 return umr_check_mkey_mask(dev, be64_to_cpu(umr->mkey_mask));
Eli Cohene126ba92013-07-07 17:25:49 +03004393}
4394
4395static u8 get_umr_flags(int acc)
4396{
4397 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
4398 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
4399 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
4400 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02004401 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004402}
4403
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004404static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
4405 struct mlx5_ib_mr *mr,
4406 u32 key, int access)
4407{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004408 int ndescs = ALIGN(mr->ndescs + mr->meta_ndescs, 8) >> 1;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004409
4410 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004411
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004412 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004413 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004414 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004415 /* KLMs take twice the size of MTTs */
4416 ndescs *= 2;
4417
4418 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004419 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
4420 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
4421 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
4422 seg->len = cpu_to_be64(mr->ibmr.length);
4423 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004424}
4425
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004426static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03004427{
4428 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004429 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004430}
4431
Bart Van Asschef696bf62018-07-18 09:25:14 -07004432static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg,
4433 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004434{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004435 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004436
Eli Cohene126ba92013-07-07 17:25:49 +03004437 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02004438 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02004439 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004440
Haggai Eran968e78d2014-12-11 17:04:11 +02004441 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004442 if (umrwr->pd)
4443 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
4444 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
4445 !umrwr->length)
4446 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
4447
4448 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004449 seg->len = cpu_to_be64(umrwr->length);
4450 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03004451 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02004452 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03004453}
4454
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004455static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
4456 struct mlx5_ib_mr *mr,
4457 struct mlx5_ib_pd *pd)
4458{
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004459 int bcount = mr->desc_size * (mr->ndescs + mr->meta_ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004460
4461 dseg->addr = cpu_to_be64(mr->desc_map);
4462 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
4463 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
4464}
4465
Bart Van Asschef696bf62018-07-18 09:25:14 -07004466static __be32 send_ieth(const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004467{
4468 switch (wr->opcode) {
4469 case IB_WR_SEND_WITH_IMM:
4470 case IB_WR_RDMA_WRITE_WITH_IMM:
4471 return wr->ex.imm_data;
4472
4473 case IB_WR_SEND_WITH_INV:
4474 return cpu_to_be32(wr->ex.invalidate_rkey);
4475
4476 default:
4477 return 0;
4478 }
4479}
4480
4481static u8 calc_sig(void *wqe, int size)
4482{
4483 u8 *p = wqe;
4484 u8 res = 0;
4485 int i;
4486
4487 for (i = 0; i < size; i++)
4488 res ^= p[i];
4489
4490 return ~res;
4491}
4492
4493static u8 wq_sig(void *wqe)
4494{
4495 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
4496}
4497
Bart Van Asschef696bf62018-07-18 09:25:14 -07004498static int set_data_inl_seg(struct mlx5_ib_qp *qp, const struct ib_send_wr *wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004499 void **wqe, int *wqe_sz, void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004500{
4501 struct mlx5_wqe_inline_seg *seg;
Guy Levi34f4c952018-11-26 08:15:50 +02004502 size_t offset;
Eli Cohene126ba92013-07-07 17:25:49 +03004503 int inl = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004504 int i;
4505
Guy Levi34f4c952018-11-26 08:15:50 +02004506 seg = *wqe;
4507 *wqe += sizeof(*seg);
4508 offset = sizeof(*seg);
4509
Eli Cohene126ba92013-07-07 17:25:49 +03004510 for (i = 0; i < wr->num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02004511 size_t len = wr->sg_list[i].length;
4512 void *addr = (void *)(unsigned long)(wr->sg_list[i].addr);
4513
Eli Cohene126ba92013-07-07 17:25:49 +03004514 inl += len;
4515
4516 if (unlikely(inl > qp->max_inline_data))
4517 return -ENOMEM;
4518
Guy Levi34f4c952018-11-26 08:15:50 +02004519 while (likely(len)) {
4520 size_t leftlen;
4521 size_t copysz;
4522
4523 handle_post_send_edge(&qp->sq, wqe,
4524 *wqe_sz + (offset >> 4),
4525 cur_edge);
4526
4527 leftlen = *cur_edge - *wqe;
4528 copysz = min_t(size_t, leftlen, len);
4529
4530 memcpy(*wqe, addr, copysz);
4531 len -= copysz;
4532 addr += copysz;
4533 *wqe += copysz;
4534 offset += copysz;
Eli Cohene126ba92013-07-07 17:25:49 +03004535 }
Eli Cohene126ba92013-07-07 17:25:49 +03004536 }
4537
4538 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
4539
Guy Levi34f4c952018-11-26 08:15:50 +02004540 *wqe_sz += ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004541
4542 return 0;
4543}
4544
Sagi Grimberge6631812014-02-23 14:19:11 +02004545static u16 prot_field_size(enum ib_signature_type type)
4546{
4547 switch (type) {
4548 case IB_SIG_TYPE_T10_DIF:
4549 return MLX5_DIF_SIZE;
4550 default:
4551 return 0;
4552 }
4553}
4554
4555static u8 bs_selector(int block_size)
4556{
4557 switch (block_size) {
4558 case 512: return 0x1;
4559 case 520: return 0x2;
4560 case 4096: return 0x3;
4561 case 4160: return 0x4;
4562 case 1073741824: return 0x5;
4563 default: return 0;
4564 }
4565}
4566
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004567static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
4568 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02004569{
Sagi Grimberg142537f2014-08-13 19:54:32 +03004570 /* Valid inline section and allow BSF refresh */
4571 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
4572 MLX5_BSF_REFRESH_DIF);
4573 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
4574 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004575 /* repeating block */
4576 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
4577 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
4578 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004579
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004580 if (domain->sig.dif.ref_remap)
4581 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02004582
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004583 if (domain->sig.dif.app_escape) {
4584 if (domain->sig.dif.ref_escape)
4585 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
4586 else
4587 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02004588 }
4589
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004590 inl->dif_app_bitmask_check =
4591 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02004592}
4593
4594static int mlx5_set_bsf(struct ib_mr *sig_mr,
4595 struct ib_sig_attrs *sig_attrs,
4596 struct mlx5_bsf *bsf, u32 data_size)
4597{
4598 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
4599 struct mlx5_bsf_basic *basic = &bsf->basic;
4600 struct ib_sig_domain *mem = &sig_attrs->mem;
4601 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02004602
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004603 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02004604
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004605 /* Basic + Extended + Inline */
4606 basic->bsf_size_sbs = 1 << 7;
4607 /* Input domain check byte mask */
4608 basic->check_byte_mask = sig_attrs->check_mask;
4609 basic->raw_data_size = cpu_to_be32(data_size);
4610
4611 /* Memory domain */
4612 switch (sig_attrs->mem.sig_type) {
4613 case IB_SIG_TYPE_NONE:
4614 break;
4615 case IB_SIG_TYPE_T10_DIF:
4616 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
4617 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
4618 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
4619 break;
4620 default:
4621 return -EINVAL;
4622 }
4623
4624 /* Wire domain */
4625 switch (sig_attrs->wire.sig_type) {
4626 case IB_SIG_TYPE_NONE:
4627 break;
4628 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02004629 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004630 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004631 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03004632 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02004633 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004634 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004635 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004636 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004637 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004638 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02004639 } else
4640 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
4641
Sagi Grimberg142537f2014-08-13 19:54:32 +03004642 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004643 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02004644 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004645 default:
4646 return -EINVAL;
4647 }
4648
4649 return 0;
4650}
4651
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004652static int set_sig_data_segment(const struct ib_send_wr *send_wr,
4653 struct ib_mr *sig_mr,
4654 struct ib_sig_attrs *sig_attrs,
4655 struct mlx5_ib_qp *qp, void **seg, int *size,
4656 void **cur_edge)
Sagi Grimberge6631812014-02-23 14:19:11 +02004657{
Sagi Grimberge6631812014-02-23 14:19:11 +02004658 struct mlx5_bsf *bsf;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004659 u32 data_len;
4660 u32 data_key;
4661 u64 data_va;
4662 u32 prot_len = 0;
4663 u32 prot_key = 0;
4664 u64 prot_va = 0;
4665 bool prot = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004666 int ret;
4667 int wqe_size;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004668 struct mlx5_ib_mr *mr = to_mmr(sig_mr);
4669 struct mlx5_ib_mr *pi_mr = mr->pi_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004670
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004671 data_len = pi_mr->data_length;
4672 data_key = pi_mr->ibmr.lkey;
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03004673 data_va = pi_mr->data_iova;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004674 if (pi_mr->meta_ndescs) {
4675 prot_len = pi_mr->meta_length;
4676 prot_key = pi_mr->ibmr.lkey;
Israel Rukshinde0ae952019-06-11 18:52:55 +03004677 prot_va = pi_mr->pi_iova;
Israel Rukshin5c171cb2019-06-11 18:52:54 +03004678 prot = true;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004679 }
4680
4681 if (!prot || (data_key == prot_key && data_va == prot_va &&
4682 data_len == prot_len)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004683 /**
4684 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004685 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02004686 * So need construct:
4687 * ------------------
4688 * | data_klm |
4689 * ------------------
4690 * | BSF |
4691 * ------------------
4692 **/
4693 struct mlx5_klm *data_klm = *seg;
4694
4695 data_klm->bcount = cpu_to_be32(data_len);
4696 data_klm->key = cpu_to_be32(data_key);
4697 data_klm->va = cpu_to_be64(data_va);
4698 wqe_size = ALIGN(sizeof(*data_klm), 64);
4699 } else {
4700 /**
4701 * Source domain contains signature information
4702 * So need construct a strided block format:
4703 * ---------------------------
4704 * | stride_block_ctrl |
4705 * ---------------------------
4706 * | data_klm |
4707 * ---------------------------
4708 * | prot_klm |
4709 * ---------------------------
4710 * | BSF |
4711 * ---------------------------
4712 **/
4713 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
4714 struct mlx5_stride_block_entry *data_sentry;
4715 struct mlx5_stride_block_entry *prot_sentry;
Sagi Grimberge6631812014-02-23 14:19:11 +02004716 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
4717 int prot_size;
4718
4719 sblock_ctrl = *seg;
4720 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
4721 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
4722
4723 prot_size = prot_field_size(sig_attrs->mem.sig_type);
4724 if (!prot_size) {
4725 pr_err("Bad block size given: %u\n", block_size);
4726 return -EINVAL;
4727 }
4728 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
4729 prot_size);
4730 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
4731 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
4732 sblock_ctrl->num_entries = cpu_to_be16(2);
4733
4734 data_sentry->bcount = cpu_to_be16(block_size);
4735 data_sentry->key = cpu_to_be32(data_key);
4736 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004737 data_sentry->stride = cpu_to_be16(block_size);
4738
Sagi Grimberge6631812014-02-23 14:19:11 +02004739 prot_sentry->bcount = cpu_to_be16(prot_size);
4740 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004741 prot_sentry->va = cpu_to_be64(prot_va);
4742 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004743
Sagi Grimberge6631812014-02-23 14:19:11 +02004744 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
4745 sizeof(*prot_sentry), 64);
4746 }
4747
4748 *seg += wqe_size;
4749 *size += wqe_size / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004750 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004751
4752 bsf = *seg;
4753 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
4754 if (ret)
4755 return -EINVAL;
4756
4757 *seg += sizeof(*bsf);
4758 *size += sizeof(*bsf) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004759 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004760
4761 return 0;
4762}
4763
4764static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004765 struct ib_mr *sig_mr, int access_flags,
4766 u32 size, u32 length, u32 pdn)
Sagi Grimberge6631812014-02-23 14:19:11 +02004767{
Sagi Grimberge6631812014-02-23 14:19:11 +02004768 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004769 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004770
4771 memset(seg, 0, sizeof(*seg));
4772
Max Gurtovoy22465bb2019-06-11 18:52:45 +03004773 seg->flags = get_umr_flags(access_flags) | MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004774 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004775 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004776 MLX5_MKEY_BSF_EN | pdn);
4777 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004778 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004779 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4780}
4781
4782static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004783 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004784{
4785 memset(umr, 0, sizeof(*umr));
4786
4787 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004788 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004789 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4790 umr->mkey_mask = sig_mkey_mask();
4791}
4792
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004793static int set_pi_umr_wr(const struct ib_send_wr *send_wr,
4794 struct mlx5_ib_qp *qp, void **seg, int *size,
4795 void **cur_edge)
4796{
4797 const struct ib_reg_wr *wr = reg_wr(send_wr);
4798 struct mlx5_ib_mr *sig_mr = to_mmr(wr->mr);
4799 struct mlx5_ib_mr *pi_mr = sig_mr->pi_mr;
4800 struct ib_sig_attrs *sig_attrs = sig_mr->ibmr.sig_attrs;
4801 u32 pdn = get_pd(qp)->pdn;
4802 u32 xlt_size;
4803 int region_len, ret;
4804
4805 if (unlikely(send_wr->num_sge != 0) ||
4806 unlikely(wr->access & IB_ACCESS_REMOTE_ATOMIC) ||
Max Gurtovoy185eddc2019-06-11 18:52:51 +03004807 unlikely(!sig_mr->sig) || unlikely(!qp->ibqp.integrity_en) ||
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004808 unlikely(!sig_mr->sig->sig_status_checked))
4809 return -EINVAL;
4810
4811 /* length of the protected region, data + protection */
4812 region_len = pi_mr->ibmr.length;
4813
4814 /**
4815 * KLM octoword size - if protection was provided
4816 * then we use strided block format (3 octowords),
4817 * else we use single KLM (1 octoword)
4818 **/
4819 if (sig_attrs->mem.sig_type != IB_SIG_TYPE_NONE)
4820 xlt_size = 0x30;
4821 else
4822 xlt_size = sizeof(struct mlx5_klm);
4823
4824 set_sig_umr_segment(*seg, xlt_size);
4825 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4826 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4827 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4828
4829 set_sig_mkey_segment(*seg, wr->mr, wr->access, xlt_size, region_len,
4830 pdn);
4831 *seg += sizeof(struct mlx5_mkey_seg);
4832 *size += sizeof(struct mlx5_mkey_seg) / 16;
4833 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
4834
4835 ret = set_sig_data_segment(send_wr, wr->mr, sig_attrs, qp, seg, size,
4836 cur_edge);
4837 if (ret)
4838 return ret;
4839
4840 sig_mr->sig->sig_status_checked = false;
4841 return 0;
4842}
Sagi Grimberge6631812014-02-23 14:19:11 +02004843
Sagi Grimberge6631812014-02-23 14:19:11 +02004844static int set_psv_wr(struct ib_sig_domain *domain,
4845 u32 psv_idx, void **seg, int *size)
4846{
4847 struct mlx5_seg_set_psv *psv_seg = *seg;
4848
4849 memset(psv_seg, 0, sizeof(*psv_seg));
4850 psv_seg->psv_num = cpu_to_be32(psv_idx);
4851 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004852 case IB_SIG_TYPE_NONE:
4853 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004854 case IB_SIG_TYPE_T10_DIF:
4855 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4856 domain->sig.dif.app_tag);
4857 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004858 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004859 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004860 pr_err("Bad signature type (%d) is given.\n",
4861 domain->sig_type);
4862 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004863 }
4864
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004865 *seg += sizeof(*psv_seg);
4866 *size += sizeof(*psv_seg) / 16;
4867
Sagi Grimberge6631812014-02-23 14:19:11 +02004868 return 0;
4869}
4870
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004871static int set_reg_wr(struct mlx5_ib_qp *qp,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004872 const struct ib_reg_wr *wr,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004873 void **seg, int *size, void **cur_edge,
4874 bool check_not_free)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004875{
4876 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4877 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
Moni Shoua841b07f2019-08-15 11:38:34 +03004878 struct mlx5_ib_dev *dev = to_mdev(pd->ibpd.device);
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03004879 int mr_list_size = (mr->ndescs + mr->meta_ndescs) * mr->desc_size;
Idan Burstein064e5262018-05-02 13:16:39 +03004880 bool umr_inline = mr_list_size <= MLX5_IB_SQ_UMR_INLINE_THRESHOLD;
Moni Shoua841b07f2019-08-15 11:38:34 +03004881 bool atomic = wr->access & IB_ACCESS_REMOTE_ATOMIC;
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004882 u8 flags = 0;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004883
Michael Guralnikd6de0bb2020-01-08 20:05:40 +02004884 if (!mlx5_ib_can_use_umr(dev, atomic, wr->access)) {
Moni Shoua841b07f2019-08-15 11:38:34 +03004885 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4886 "Fast update of %s for MR is disabled\n",
4887 (MLX5_CAP_GEN(dev->mdev,
4888 umr_modify_entity_size_disabled)) ?
4889 "entity size" :
4890 "atomic access");
4891 return -EINVAL;
4892 }
4893
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004894 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4895 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4896 "Invalid IB_SEND_INLINE send flag\n");
4897 return -EINVAL;
4898 }
4899
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03004900 if (check_not_free)
4901 flags |= MLX5_UMR_CHECK_NOT_FREE;
4902 if (umr_inline)
4903 flags |= MLX5_UMR_INLINE;
4904
Moni Shoua841b07f2019-08-15 11:38:34 +03004905 set_reg_umr_seg(*seg, mr, flags, atomic);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004906 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4907 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004908 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004909
4910 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4911 *seg += sizeof(struct mlx5_mkey_seg);
4912 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004913 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004914
Idan Burstein064e5262018-05-02 13:16:39 +03004915 if (umr_inline) {
Guy Levi34f4c952018-11-26 08:15:50 +02004916 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, mr->descs,
4917 mr_list_size);
4918 *size = ALIGN(*size, MLX5_SEND_WQE_BB >> 4);
Idan Burstein064e5262018-05-02 13:16:39 +03004919 } else {
4920 set_reg_data_seg(*seg, mr, pd);
4921 *seg += sizeof(struct mlx5_wqe_data_seg);
4922 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4923 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004924 return 0;
4925}
4926
Guy Levi34f4c952018-11-26 08:15:50 +02004927static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size,
4928 void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004929{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004930 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004931 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4932 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004933 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004934 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004935 *seg += sizeof(struct mlx5_mkey_seg);
4936 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004937 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03004938}
4939
Guy Levi34f4c952018-11-26 08:15:50 +02004940static void dump_wqe(struct mlx5_ib_qp *qp, u32 idx, int size_16)
Eli Cohene126ba92013-07-07 17:25:49 +03004941{
4942 __be32 *p = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004943 int i, j;
4944
Guy Levi34f4c952018-11-26 08:15:50 +02004945 pr_debug("dump WQE index %u:\n", idx);
Eli Cohene126ba92013-07-07 17:25:49 +03004946 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4947 if ((i & 0xf) == 0) {
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004948 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, idx);
Guy Levi34f4c952018-11-26 08:15:50 +02004949 pr_debug("WQBB at %p:\n", (void *)p);
Eli Cohene126ba92013-07-07 17:25:49 +03004950 j = 0;
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004951 idx = (idx + 1) & (qp->sq.wqe_cnt - 1);
Eli Cohene126ba92013-07-07 17:25:49 +03004952 }
4953 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4954 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4955 be32_to_cpu(p[j + 3]));
4956 }
4957}
4958
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004959static int __begin_wqe(struct mlx5_ib_qp *qp, void **seg,
Guy Levi34f4c952018-11-26 08:15:50 +02004960 struct mlx5_wqe_ctrl_seg **ctrl,
4961 const struct ib_send_wr *wr, unsigned int *idx,
4962 int *size, void **cur_edge, int nreq,
4963 bool send_signaled, bool solicited)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004964{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004965 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4966 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004967
4968 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
Guy Levi34f4c952018-11-26 08:15:50 +02004969 *seg = mlx5_frag_buf_get_wqe(&qp->sq.fbc, *idx);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004970 *ctrl = *seg;
4971 *(uint32_t *)(*seg + 8) = 0;
4972 (*ctrl)->imm = send_ieth(wr);
4973 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004974 (send_signaled ? MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4975 (solicited ? MLX5_WQE_CTRL_SOLICITED : 0);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004976
4977 *seg += sizeof(**ctrl);
4978 *size = sizeof(**ctrl) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004979 *cur_edge = qp->sq.cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004980
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004981 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004982}
4983
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004984static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4985 struct mlx5_wqe_ctrl_seg **ctrl,
4986 const struct ib_send_wr *wr, unsigned *idx,
Guy Levi34f4c952018-11-26 08:15:50 +02004987 int *size, void **cur_edge, int nreq)
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004988{
Guy Levi34f4c952018-11-26 08:15:50 +02004989 return __begin_wqe(qp, seg, ctrl, wr, idx, size, cur_edge, nreq,
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004990 wr->send_flags & IB_SEND_SIGNALED,
4991 wr->send_flags & IB_SEND_SOLICITED);
4992}
4993
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004994static void finish_wqe(struct mlx5_ib_qp *qp,
4995 struct mlx5_wqe_ctrl_seg *ctrl,
Guy Levi34f4c952018-11-26 08:15:50 +02004996 void *seg, u8 size, void *cur_edge,
4997 unsigned int idx, u64 wr_id, int nreq, u8 fence,
4998 u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004999{
5000 u8 opmod = 0;
5001
5002 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
5003 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02005004 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005005 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005006 if (unlikely(qp->wq_sig))
5007 ctrl->signature = wq_sig(ctrl);
5008
5009 qp->sq.wrid[idx] = wr_id;
5010 qp->sq.w_list[idx].opcode = mlx5_opcode;
5011 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
5012 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
5013 qp->sq.w_list[idx].next = qp->sq.cur_post;
Guy Levi34f4c952018-11-26 08:15:50 +02005014
5015 /* We save the edge which was possibly updated during the WQE
5016 * construction, into SQ's cache.
5017 */
5018 seg = PTR_ALIGN(seg, MLX5_SEND_WQE_BB);
5019 qp->sq.cur_edge = (unlikely(seg == cur_edge)) ?
5020 get_sq_edge(&qp->sq, qp->sq.cur_post &
5021 (qp->sq.wqe_cnt - 1)) :
5022 cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005023}
5024
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005025static int _mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
5026 const struct ib_send_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03005027{
5028 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
5029 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03005030 struct mlx5_core_dev *mdev = dev->mdev;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005031 struct ib_reg_wr reg_pi_wr;
Haggai Erand16e91d2016-02-29 15:45:05 +02005032 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02005033 struct mlx5_ib_mr *mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005034 struct mlx5_ib_mr *pi_mr;
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005035 struct mlx5_ib_mr pa_pi_mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005036 struct ib_sig_attrs *sig_attrs;
Eli Cohene126ba92013-07-07 17:25:49 +03005037 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02005038 struct mlx5_bf *bf;
Guy Levi34f4c952018-11-26 08:15:50 +02005039 void *cur_edge;
Eli Cohene126ba92013-07-07 17:25:49 +03005040 int uninitialized_var(size);
Eli Cohene126ba92013-07-07 17:25:49 +03005041 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03005042 unsigned idx;
5043 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03005044 int num_sge;
5045 void *seg;
5046 int nreq;
5047 int i;
5048 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03005049 u8 fence;
5050
Parav Pandit6c755202018-08-28 14:45:29 +03005051 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5052 !drain)) {
5053 *bad_wr = wr;
5054 return -EIO;
5055 }
5056
Haggai Erand16e91d2016-02-29 15:45:05 +02005057 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5058 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
5059
5060 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005061 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02005062
Eli Cohene126ba92013-07-07 17:25:49 +03005063 spin_lock_irqsave(&qp->sq.lock, flags);
5064
5065 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04005066 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03005067 mlx5_ib_warn(dev, "\n");
5068 err = -EINVAL;
5069 *bad_wr = wr;
5070 goto out;
5071 }
5072
Eli Cohene126ba92013-07-07 17:25:49 +03005073 num_sge = wr->num_sge;
5074 if (unlikely(num_sge > qp->sq.max_gs)) {
5075 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03005076 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03005077 *bad_wr = wr;
5078 goto out;
5079 }
5080
Guy Levi34f4c952018-11-26 08:15:50 +02005081 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, &cur_edge,
5082 nreq);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02005083 if (err) {
5084 mlx5_ib_warn(dev, "\n");
5085 err = -ENOMEM;
5086 *bad_wr = wr;
5087 goto out;
5088 }
Eli Cohene126ba92013-07-07 17:25:49 +03005089
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005090 if (wr->opcode == IB_WR_REG_MR ||
5091 wr->opcode == IB_WR_REG_MR_INTEGRITY) {
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005092 fence = dev->umr_fence;
5093 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Majd Dibbiny074fca32018-11-05 08:07:37 +02005094 } else {
5095 if (wr->send_flags & IB_SEND_FENCE) {
5096 if (qp->next_fence)
5097 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
5098 else
5099 fence = MLX5_FENCE_MODE_FENCE;
5100 } else {
5101 fence = qp->next_fence;
5102 }
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005103 }
5104
Eli Cohene126ba92013-07-07 17:25:49 +03005105 switch (ibqp->qp_type) {
5106 case IB_QPT_XRC_INI:
5107 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03005108 seg += sizeof(*xrc);
5109 size += sizeof(*xrc) / 16;
5110 /* fall through */
5111 case IB_QPT_RC:
5112 switch (wr->opcode) {
5113 case IB_WR_RDMA_READ:
5114 case IB_WR_RDMA_WRITE:
5115 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005116 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5117 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005118 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005119 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5120 break;
5121
5122 case IB_WR_ATOMIC_CMP_AND_SWP:
5123 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03005124 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03005125 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
5126 err = -ENOSYS;
5127 *bad_wr = wr;
5128 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005129
5130 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03005131 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
5132 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Guy Levi34f4c952018-11-26 08:15:50 +02005133 set_linv_wr(qp, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005134 num_sge = 0;
5135 break;
5136
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005137 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005138 qp->sq.wr_data[idx] = IB_WR_REG_MR;
5139 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
Guy Levi34f4c952018-11-26 08:15:50 +02005140 err = set_reg_wr(qp, reg_wr(wr), &seg, &size,
Max Gurtovoy9ac7c4b2019-06-11 18:52:44 +03005141 &cur_edge, true);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005142 if (err) {
5143 *bad_wr = wr;
5144 goto out;
5145 }
5146 num_sge = 0;
5147 break;
5148
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005149 case IB_WR_REG_MR_INTEGRITY:
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005150 qp->sq.wr_data[idx] = IB_WR_REG_MR_INTEGRITY;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005151
5152 mr = to_mmr(reg_wr(wr)->mr);
5153 pi_mr = mr->pi_mr;
5154
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005155 if (pi_mr) {
5156 memset(&reg_pi_wr, 0,
5157 sizeof(struct ib_reg_wr));
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005158
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005159 reg_pi_wr.mr = &pi_mr->ibmr;
5160 reg_pi_wr.access = reg_wr(wr)->access;
5161 reg_pi_wr.key = pi_mr->ibmr.rkey;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005162
Max Gurtovoy2563e2f2019-06-11 18:52:56 +03005163 ctrl->imm = cpu_to_be32(reg_pi_wr.key);
5164 /* UMR for data + prot registration */
5165 err = set_reg_wr(qp, &reg_pi_wr, &seg,
5166 &size, &cur_edge,
5167 false);
5168 if (err) {
5169 *bad_wr = wr;
5170 goto out;
5171 }
5172 finish_wqe(qp, ctrl, seg, size,
5173 cur_edge, idx, wr->wr_id,
5174 nreq, fence,
5175 MLX5_OPCODE_UMR);
5176
5177 err = begin_wqe(qp, &seg, &ctrl, wr,
5178 &idx, &size, &cur_edge,
5179 nreq);
5180 if (err) {
5181 mlx5_ib_warn(dev, "\n");
5182 err = -ENOMEM;
5183 *bad_wr = wr;
5184 goto out;
5185 }
5186 } else {
5187 memset(&pa_pi_mr, 0,
5188 sizeof(struct mlx5_ib_mr));
5189 /* No UMR, use local_dma_lkey */
5190 pa_pi_mr.ibmr.lkey =
5191 mr->ibmr.pd->local_dma_lkey;
5192
5193 pa_pi_mr.ndescs = mr->ndescs;
5194 pa_pi_mr.data_length = mr->data_length;
5195 pa_pi_mr.data_iova = mr->data_iova;
5196 if (mr->meta_ndescs) {
5197 pa_pi_mr.meta_ndescs =
5198 mr->meta_ndescs;
5199 pa_pi_mr.meta_length =
5200 mr->meta_length;
5201 pa_pi_mr.pi_iova = mr->pi_iova;
5202 }
5203
5204 pa_pi_mr.ibmr.length = mr->ibmr.length;
5205 mr->pi_mr = &pa_pi_mr;
Max Gurtovoy38ca87c2019-06-11 18:52:46 +03005206 }
5207 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
5208 /* UMR for sig MR */
5209 err = set_pi_umr_wr(wr, qp, &seg, &size,
5210 &cur_edge);
5211 if (err) {
5212 mlx5_ib_warn(dev, "\n");
5213 *bad_wr = wr;
5214 goto out;
5215 }
5216 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5217 wr->wr_id, nreq, fence,
5218 MLX5_OPCODE_UMR);
5219
5220 /*
5221 * SET_PSV WQEs are not signaled and solicited
5222 * on error
5223 */
5224 sig_attrs = mr->ibmr.sig_attrs;
5225 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5226 &size, &cur_edge, nreq, false,
5227 true);
5228 if (err) {
5229 mlx5_ib_warn(dev, "\n");
5230 err = -ENOMEM;
5231 *bad_wr = wr;
5232 goto out;
5233 }
5234 err = set_psv_wr(&sig_attrs->mem,
5235 mr->sig->psv_memory.psv_idx,
5236 &seg, &size);
5237 if (err) {
5238 mlx5_ib_warn(dev, "\n");
5239 *bad_wr = wr;
5240 goto out;
5241 }
5242 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5243 wr->wr_id, nreq, next_fence,
5244 MLX5_OPCODE_SET_PSV);
5245
5246 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
5247 &size, &cur_edge, nreq, false,
5248 true);
5249 if (err) {
5250 mlx5_ib_warn(dev, "\n");
5251 err = -ENOMEM;
5252 *bad_wr = wr;
5253 goto out;
5254 }
5255 err = set_psv_wr(&sig_attrs->wire,
5256 mr->sig->psv_wire.psv_idx,
5257 &seg, &size);
5258 if (err) {
5259 mlx5_ib_warn(dev, "\n");
5260 *bad_wr = wr;
5261 goto out;
5262 }
5263 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5264 wr->wr_id, nreq, next_fence,
5265 MLX5_OPCODE_SET_PSV);
5266
5267 qp->next_fence =
5268 MLX5_FENCE_MODE_INITIATOR_SMALL;
5269 num_sge = 0;
5270 goto skip_psv;
5271
Eli Cohene126ba92013-07-07 17:25:49 +03005272 default:
5273 break;
5274 }
5275 break;
5276
5277 case IB_QPT_UC:
5278 switch (wr->opcode) {
5279 case IB_WR_RDMA_WRITE:
5280 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005281 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5282 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03005283 seg += sizeof(struct mlx5_wqe_raddr_seg);
5284 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5285 break;
5286
5287 default:
5288 break;
5289 }
5290 break;
5291
Eli Cohene126ba92013-07-07 17:25:49 +03005292 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02005293 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
5294 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
5295 err = -EPERM;
5296 *bad_wr = wr;
5297 goto out;
5298 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07005299 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02005300 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03005301 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005302 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005303 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005304 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
5305
Eli Cohene126ba92013-07-07 17:25:49 +03005306 break;
Erez Shitritf0313962016-02-21 16:27:17 +02005307 case IB_QPT_UD:
5308 set_datagram_seg(seg, wr);
5309 seg += sizeof(struct mlx5_wqe_datagram_seg);
5310 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005311 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005312
5313 /* handle qp that supports ud offload */
5314 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
5315 struct mlx5_wqe_eth_pad *pad;
5316
5317 pad = seg;
5318 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
5319 seg += sizeof(struct mlx5_wqe_eth_pad);
5320 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005321 set_eth_seg(wr, qp, &seg, &size, &cur_edge);
5322 handle_post_send_edge(&qp->sq, &seg, size,
5323 &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005324 }
5325 break;
Eli Cohene126ba92013-07-07 17:25:49 +03005326 case MLX5_IB_QPT_REG_UMR:
5327 if (wr->opcode != MLX5_IB_WR_UMR) {
5328 err = -EINVAL;
5329 mlx5_ib_warn(dev, "bad opcode\n");
5330 goto out;
5331 }
5332 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005333 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02005334 err = set_reg_umr_segment(dev, seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
5335 if (unlikely(err))
5336 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005337 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
5338 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005339 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005340 set_reg_mkey_segment(seg, wr);
5341 seg += sizeof(struct mlx5_mkey_seg);
5342 size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005343 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005344 break;
5345
5346 default:
5347 break;
5348 }
5349
5350 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
Guy Levi34f4c952018-11-26 08:15:50 +02005351 err = set_data_inl_seg(qp, wr, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005352 if (unlikely(err)) {
5353 mlx5_ib_warn(dev, "\n");
5354 *bad_wr = wr;
5355 goto out;
5356 }
Eli Cohene126ba92013-07-07 17:25:49 +03005357 } else {
Eli Cohene126ba92013-07-07 17:25:49 +03005358 for (i = 0; i < num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02005359 handle_post_send_edge(&qp->sq, &seg, size,
5360 &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005361 if (likely(wr->sg_list[i].length)) {
Guy Levi34f4c952018-11-26 08:15:50 +02005362 set_data_ptr_seg
5363 ((struct mlx5_wqe_data_seg *)seg,
5364 wr->sg_list + i);
Eli Cohene126ba92013-07-07 17:25:49 +03005365 size += sizeof(struct mlx5_wqe_data_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005366 seg += sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005367 }
5368 }
5369 }
5370
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005371 qp->next_fence = next_fence;
Guy Levi34f4c952018-11-26 08:15:50 +02005372 finish_wqe(qp, ctrl, seg, size, cur_edge, idx, wr->wr_id, nreq,
5373 fence, mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02005374skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03005375 if (0)
5376 dump_wqe(qp, idx, size);
5377 }
5378
5379out:
5380 if (likely(nreq)) {
5381 qp->sq.head += nreq;
5382
5383 /* Make sure that descriptors are written before
5384 * updating doorbell record and ringing the doorbell
5385 */
5386 wmb();
5387
5388 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
5389
Eli Cohenada388f2014-01-14 17:45:16 +02005390 /* Make sure doorbell record is visible to the HCA before
5391 * we hit doorbell */
5392 wmb();
5393
Maxim Mikityanskiybbf29f62019-03-29 15:37:52 -07005394 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005395 /* Make sure doorbells don't leak out of SQ spinlock
5396 * and reach the HCA out of order.
5397 */
Eli Cohene126ba92013-07-07 17:25:49 +03005398 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03005399 }
5400
5401 spin_unlock_irqrestore(&qp->sq.lock, flags);
5402
5403 return err;
5404}
5405
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005406int mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
5407 const struct ib_send_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005408{
5409 return _mlx5_ib_post_send(ibqp, wr, bad_wr, false);
5410}
5411
Eli Cohene126ba92013-07-07 17:25:49 +03005412static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
5413{
5414 sig->signature = calc_sig(sig, size);
5415}
5416
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005417static int _mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5418 const struct ib_recv_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03005419{
5420 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5421 struct mlx5_wqe_data_seg *scat;
5422 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03005423 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5424 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005425 unsigned long flags;
5426 int err = 0;
5427 int nreq;
5428 int ind;
5429 int i;
5430
Parav Pandit6c755202018-08-28 14:45:29 +03005431 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5432 !drain)) {
5433 *bad_wr = wr;
5434 return -EIO;
5435 }
5436
Haggai Erand16e91d2016-02-29 15:45:05 +02005437 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5438 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
5439
Eli Cohene126ba92013-07-07 17:25:49 +03005440 spin_lock_irqsave(&qp->rq.lock, flags);
5441
5442 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
5443
5444 for (nreq = 0; wr; nreq++, wr = wr->next) {
5445 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
5446 err = -ENOMEM;
5447 *bad_wr = wr;
5448 goto out;
5449 }
5450
5451 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
5452 err = -EINVAL;
5453 *bad_wr = wr;
5454 goto out;
5455 }
5456
Guy Levi34f4c952018-11-26 08:15:50 +02005457 scat = mlx5_frag_buf_get_wqe(&qp->rq.fbc, ind);
Eli Cohene126ba92013-07-07 17:25:49 +03005458 if (qp->wq_sig)
5459 scat++;
5460
5461 for (i = 0; i < wr->num_sge; i++)
5462 set_data_ptr_seg(scat + i, wr->sg_list + i);
5463
5464 if (i < qp->rq.max_gs) {
5465 scat[i].byte_count = 0;
5466 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
5467 scat[i].addr = 0;
5468 }
5469
5470 if (qp->wq_sig) {
5471 sig = (struct mlx5_rwqe_sig *)scat;
5472 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
5473 }
5474
5475 qp->rq.wrid[ind] = wr->wr_id;
5476
5477 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
5478 }
5479
5480out:
5481 if (likely(nreq)) {
5482 qp->rq.head += nreq;
5483
5484 /* Make sure that descriptors are written before
5485 * doorbell record.
5486 */
5487 wmb();
5488
5489 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
5490 }
5491
5492 spin_unlock_irqrestore(&qp->rq.lock, flags);
5493
5494 return err;
5495}
5496
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005497int mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5498 const struct ib_recv_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005499{
5500 return _mlx5_ib_post_recv(ibqp, wr, bad_wr, false);
5501}
5502
Eli Cohene126ba92013-07-07 17:25:49 +03005503static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
5504{
5505 switch (mlx5_state) {
5506 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
5507 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
5508 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
5509 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
5510 case MLX5_QP_STATE_SQ_DRAINING:
5511 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
5512 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
5513 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
5514 default: return -1;
5515 }
5516}
5517
5518static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
5519{
5520 switch (mlx5_mig_state) {
5521 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
5522 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
5523 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
5524 default: return -1;
5525 }
5526}
5527
5528static int to_ib_qp_access_flags(int mlx5_flags)
5529{
5530 int ib_flags = 0;
5531
5532 if (mlx5_flags & MLX5_QP_BIT_RRE)
5533 ib_flags |= IB_ACCESS_REMOTE_READ;
5534 if (mlx5_flags & MLX5_QP_BIT_RWE)
5535 ib_flags |= IB_ACCESS_REMOTE_WRITE;
5536 if (mlx5_flags & MLX5_QP_BIT_RAE)
5537 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
5538
5539 return ib_flags;
5540}
5541
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005542static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005543 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005544 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03005545{
Eli Cohene126ba92013-07-07 17:25:49 +03005546
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005547 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03005548
Jason Gunthorpee7996a92018-01-29 13:26:40 -07005549 if (!path->port || path->port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03005550 return;
5551
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02005552 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
5553
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005554 rdma_ah_set_port_num(ah_attr, path->port);
5555 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03005556
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005557 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
5558 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
5559 rdma_ah_set_static_rate(ah_attr,
5560 path->static_rate ? path->static_rate - 5 : 0);
Aharon Landau2d7e3ff2020-04-13 16:20:28 +03005561
5562 if (path->grh_mlid & (1 << 7) ||
5563 ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005564 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
5565
5566 rdma_ah_set_grh(ah_attr, NULL,
5567 tc_fl & 0xfffff,
5568 path->mgid_index,
5569 path->hop_limit,
5570 (tc_fl >> 20) & 0xff);
5571 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03005572 }
5573}
5574
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005575static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
5576 struct mlx5_ib_sq *sq,
5577 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03005578{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005579 int err;
5580
Eran Ben Elisha28160772017-12-26 15:17:05 +02005581 err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005582 if (err)
5583 goto out;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005584 sq->state = *sq_state;
5585
5586out:
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005587 return err;
5588}
5589
5590static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
5591 struct mlx5_ib_rq *rq,
5592 u8 *rq_state)
5593{
5594 void *out;
5595 void *rqc;
5596 int inlen;
5597 int err;
5598
5599 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005600 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005601 if (!out)
5602 return -ENOMEM;
5603
5604 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
5605 if (err)
5606 goto out;
5607
5608 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
5609 *rq_state = MLX5_GET(rqc, rqc, state);
5610 rq->state = *rq_state;
5611
5612out:
5613 kvfree(out);
5614 return err;
5615}
5616
5617static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
5618 struct mlx5_ib_qp *qp, u8 *qp_state)
5619{
5620 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
5621 [MLX5_RQC_STATE_RST] = {
5622 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5623 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5624 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
5625 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
5626 },
5627 [MLX5_RQC_STATE_RDY] = {
5628 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5629 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5630 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
5631 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
5632 },
5633 [MLX5_RQC_STATE_ERR] = {
5634 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5635 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5636 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
5637 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
5638 },
5639 [MLX5_RQ_STATE_NA] = {
5640 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5641 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5642 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
5643 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
5644 },
5645 };
5646
5647 *qp_state = sqrq_trans[rq_state][sq_state];
5648
5649 if (*qp_state == MLX5_QP_STATE_BAD) {
5650 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
5651 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
5652 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
5653 return -EINVAL;
5654 }
5655
5656 if (*qp_state == MLX5_QP_STATE)
5657 *qp_state = qp->state;
5658
5659 return 0;
5660}
5661
5662static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
5663 struct mlx5_ib_qp *qp,
5664 u8 *raw_packet_qp_state)
5665{
5666 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
5667 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
5668 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
5669 int err;
5670 u8 sq_state = MLX5_SQ_STATE_NA;
5671 u8 rq_state = MLX5_RQ_STATE_NA;
5672
5673 if (qp->sq.wqe_cnt) {
5674 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
5675 if (err)
5676 return err;
5677 }
5678
5679 if (qp->rq.wqe_cnt) {
5680 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
5681 if (err)
5682 return err;
5683 }
5684
5685 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
5686 raw_packet_qp_state);
5687}
5688
5689static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
5690 struct ib_qp_attr *qp_attr)
5691{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005692 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03005693 struct mlx5_qp_context *context;
5694 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005695 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03005696 int err = 0;
5697
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005698 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005699 if (!outb)
5700 return -ENOMEM;
5701
majd@mellanox.com19098df2016-01-14 19:13:03 +02005702 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005703 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03005704 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005705 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005706
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005707 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
5708 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
5709
Eli Cohene126ba92013-07-07 17:25:49 +03005710 mlx5_state = be32_to_cpu(context->flags) >> 28;
5711
5712 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03005713 qp_attr->path_mtu = context->mtu_msgmax >> 5;
5714 qp_attr->path_mig_state =
5715 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
5716 qp_attr->qkey = be32_to_cpu(context->qkey);
5717 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
5718 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
5719 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
5720 qp_attr->qp_access_flags =
5721 to_ib_qp_access_flags(be32_to_cpu(context->params2));
5722
5723 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005724 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
5725 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005726 qp_attr->alt_pkey_index =
5727 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005728 qp_attr->alt_port_num =
5729 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03005730 }
5731
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005732 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03005733 qp_attr->port_num = context->pri_path.port;
5734
5735 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
5736 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
5737
5738 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
5739
5740 qp_attr->max_dest_rd_atomic =
5741 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
5742 qp_attr->min_rnr_timer =
5743 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
5744 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
5745 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
5746 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
5747 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005748
5749out:
5750 kfree(outb);
5751 return err;
5752}
5753
Moni Shoua776a3902018-01-02 16:19:33 +02005754static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
5755 struct ib_qp_attr *qp_attr, int qp_attr_mask,
5756 struct ib_qp_init_attr *qp_init_attr)
5757{
5758 struct mlx5_core_dct *dct = &mqp->dct.mdct;
5759 u32 *out;
5760 u32 access_flags = 0;
5761 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
5762 void *dctc;
5763 int err;
5764 int supported_mask = IB_QP_STATE |
5765 IB_QP_ACCESS_FLAGS |
5766 IB_QP_PORT |
5767 IB_QP_MIN_RNR_TIMER |
5768 IB_QP_AV |
5769 IB_QP_PATH_MTU |
5770 IB_QP_PKEY_INDEX;
5771
5772 if (qp_attr_mask & ~supported_mask)
5773 return -EINVAL;
5774 if (mqp->state != IB_QPS_RTR)
5775 return -EINVAL;
5776
5777 out = kzalloc(outlen, GFP_KERNEL);
5778 if (!out)
5779 return -ENOMEM;
5780
5781 err = mlx5_core_dct_query(dev->mdev, dct, out, outlen);
5782 if (err)
5783 goto out;
5784
5785 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
5786
5787 if (qp_attr_mask & IB_QP_STATE)
5788 qp_attr->qp_state = IB_QPS_RTR;
5789
5790 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
5791 if (MLX5_GET(dctc, dctc, rre))
5792 access_flags |= IB_ACCESS_REMOTE_READ;
5793 if (MLX5_GET(dctc, dctc, rwe))
5794 access_flags |= IB_ACCESS_REMOTE_WRITE;
5795 if (MLX5_GET(dctc, dctc, rae))
5796 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
5797 qp_attr->qp_access_flags = access_flags;
5798 }
5799
5800 if (qp_attr_mask & IB_QP_PORT)
5801 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
5802 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
5803 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
5804 if (qp_attr_mask & IB_QP_AV) {
5805 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
5806 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
5807 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
5808 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
5809 }
5810 if (qp_attr_mask & IB_QP_PATH_MTU)
5811 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
5812 if (qp_attr_mask & IB_QP_PKEY_INDEX)
5813 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
5814out:
5815 kfree(out);
5816 return err;
5817}
5818
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005819int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5820 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
5821{
5822 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5823 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5824 int err = 0;
5825 u8 raw_packet_qp_state;
5826
Yishai Hadas28d61372016-05-23 15:20:56 +03005827 if (ibqp->rwq_ind_tbl)
5828 return -ENOSYS;
5829
Haggai Erand16e91d2016-02-29 15:45:05 +02005830 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5831 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
5832 qp_init_attr);
5833
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005834 /* Not all of output fields are applicable, make sure to zero them */
5835 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5836 memset(qp_attr, 0, sizeof(*qp_attr));
5837
Moni Shoua776a3902018-01-02 16:19:33 +02005838 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
5839 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
5840 qp_attr_mask, qp_init_attr);
5841
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005842 mutex_lock(&qp->mutex);
5843
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005844 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
5845 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005846 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
5847 if (err)
5848 goto out;
5849 qp->state = raw_packet_qp_state;
5850 qp_attr->port_num = 1;
5851 } else {
5852 err = query_qp_attr(dev, qp, qp_attr);
5853 if (err)
5854 goto out;
5855 }
5856
5857 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03005858 qp_attr->cur_qp_state = qp_attr->qp_state;
5859 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
5860 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
5861
5862 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005863 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005864 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005865 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005866 } else {
5867 qp_attr->cap.max_send_wr = 0;
5868 qp_attr->cap.max_send_sge = 0;
5869 }
5870
Noa Osherovich0540d812016-06-04 15:15:32 +03005871 qp_init_attr->qp_type = ibqp->qp_type;
5872 qp_init_attr->recv_cq = ibqp->recv_cq;
5873 qp_init_attr->send_cq = ibqp->send_cq;
5874 qp_init_attr->srq = ibqp->srq;
5875 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005876
5877 qp_init_attr->cap = qp_attr->cap;
5878
5879 qp_init_attr->create_flags = 0;
5880 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
5881 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
5882
Leon Romanovsky051f2632015-12-20 12:16:11 +02005883 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
5884 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
5885 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
5886 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
5887 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
5888 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02005889 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
Michael Guralnik3f89b012019-10-20 09:43:59 +03005890 qp_init_attr->create_flags |= MLX5_IB_QP_CREATE_SQPN_QP1;
Leon Romanovsky051f2632015-12-20 12:16:11 +02005891
Eli Cohene126ba92013-07-07 17:25:49 +03005892 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5893 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5894
Eli Cohene126ba92013-07-07 17:25:49 +03005895out:
5896 mutex_unlock(&qp->mutex);
5897 return err;
5898}
5899
5900struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03005901 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005902{
5903 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5904 struct mlx5_ib_xrcd *xrcd;
5905 int err;
5906
Saeed Mahameed938fe832015-05-28 22:28:41 +03005907 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005908 return ERR_PTR(-ENOSYS);
5909
5910 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5911 if (!xrcd)
5912 return ERR_PTR(-ENOMEM);
5913
Yishai Hadas5aa37712018-11-26 08:28:38 +02005914 err = mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03005915 if (err) {
5916 kfree(xrcd);
5917 return ERR_PTR(-ENOMEM);
5918 }
5919
5920 return &xrcd->ibxrcd;
5921}
5922
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005923int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005924{
5925 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5926 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
5927 int err;
5928
Yishai Hadas5aa37712018-11-26 08:28:38 +02005929 err = mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, 0);
Leon Romanovskyb0818082018-01-28 11:25:30 +02005930 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03005931 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005932
5933 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03005934 return 0;
5935}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005936
Yishai Hadas350d0e42016-08-28 14:58:18 +03005937static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5938{
5939 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5940 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5941 struct ib_event event;
5942
5943 if (rwq->ibwq.event_handler) {
5944 event.device = rwq->ibwq.device;
5945 event.element.wq = &rwq->ibwq;
5946 switch (type) {
5947 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5948 event.event = IB_EVENT_WQ_FATAL;
5949 break;
5950 default:
5951 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5952 return;
5953 }
5954
5955 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5956 }
5957}
5958
Maor Gottlieb03404e82017-05-30 10:29:13 +03005959static int set_delay_drop(struct mlx5_ib_dev *dev)
5960{
5961 int err = 0;
5962
5963 mutex_lock(&dev->delay_drop.lock);
5964 if (dev->delay_drop.activate)
5965 goto out;
5966
5967 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
5968 if (err)
5969 goto out;
5970
5971 dev->delay_drop.activate = true;
5972out:
5973 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005974
5975 if (!err)
5976 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005977 return err;
5978}
5979
Yishai Hadas79b20a62016-05-23 15:20:50 +03005980static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5981 struct ib_wq_init_attr *init_attr)
5982{
5983 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005984 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005985 __be64 *rq_pas0;
5986 void *in;
5987 void *rqc;
5988 void *wq;
5989 int inlen;
5990 int err;
5991
5992 dev = to_mdev(pd->device);
5993
5994 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005995 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005996 if (!in)
5997 return -ENOMEM;
5998
Yishai Hadas34d57582018-09-20 21:39:21 +03005999 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006000 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
6001 MLX5_SET(rqc, rqc, mem_rq_type,
6002 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
6003 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
6004 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
6005 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
6006 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
6007 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03006008 MLX5_SET(wq, wq, wq_type,
6009 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
6010 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02006011 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6012 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
6013 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
6014 err = -EOPNOTSUPP;
6015 goto out;
6016 } else {
6017 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
6018 }
6019 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006020 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03006021 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
Mark Zhangc16339b2019-11-15 17:45:55 +02006022 /*
6023 * In Firmware number of strides in each WQE is:
6024 * "512 * 2^single_wqe_log_num_of_strides"
6025 * Values 3 to 8 are accepted as 10 to 15, 9 to 18 are
6026 * accepted as 0 to 9
6027 */
6028 static const u8 fw_map[] = { 10, 11, 12, 13, 14, 15, 0, 1,
6029 2, 3, 4, 5, 6, 7, 8, 9 };
Noa Osherovichccc87082017-10-17 18:01:13 +03006030 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
6031 MLX5_SET(wq, wq, log_wqe_stride_size,
6032 rwq->single_stride_log_num_of_bytes -
6033 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
Mark Zhangc16339b2019-11-15 17:45:55 +02006034 MLX5_SET(wq, wq, log_wqe_num_of_strides,
6035 fw_map[rwq->log_num_strides -
6036 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES]);
Noa Osherovichccc87082017-10-17 18:01:13 +03006037 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006038 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
6039 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
6040 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
6041 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
6042 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
6043 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02006044 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006045 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02006046 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006047 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
6048 err = -EOPNOTSUPP;
6049 goto out;
6050 }
6051 } else {
6052 MLX5_SET(rqc, rqc, vsd, 1);
6053 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02006054 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
6055 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
6056 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
6057 err = -EOPNOTSUPP;
6058 goto out;
6059 }
6060 MLX5_SET(rqc, rqc, scatter_fcs, 1);
6061 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03006062 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
6063 if (!(dev->ib_dev.attrs.raw_packet_caps &
6064 IB_RAW_PACKET_CAP_DELAY_DROP)) {
6065 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
6066 err = -EOPNOTSUPP;
6067 goto out;
6068 }
6069 MLX5_SET(rqc, rqc, delay_drop_en, 1);
6070 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03006071 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
6072 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03006073 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03006074 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
6075 err = set_delay_drop(dev);
6076 if (err) {
6077 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
6078 err);
6079 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
6080 } else {
6081 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
6082 }
6083 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006084out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03006085 kvfree(in);
6086 return err;
6087}
6088
6089static int set_user_rq_size(struct mlx5_ib_dev *dev,
6090 struct ib_wq_init_attr *wq_init_attr,
6091 struct mlx5_ib_create_wq *ucmd,
6092 struct mlx5_ib_rwq *rwq)
6093{
6094 /* Sanity check RQ size before proceeding */
6095 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
6096 return -EINVAL;
6097
6098 if (!ucmd->rq_wqe_count)
6099 return -EINVAL;
6100
6101 rwq->wqe_count = ucmd->rq_wqe_count;
6102 rwq->wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky0dfe4522018-08-01 14:25:41 -07006103 if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
6104 return -EINVAL;
6105
Yishai Hadas79b20a62016-05-23 15:20:50 +03006106 rwq->log_rq_stride = rwq->wqe_shift;
6107 rwq->log_rq_size = ilog2(rwq->wqe_count);
6108 return 0;
6109}
6110
Mark Zhangc16339b2019-11-15 17:45:55 +02006111static bool log_of_strides_valid(struct mlx5_ib_dev *dev, u32 log_num_strides)
6112{
6113 if ((log_num_strides > MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
6114 (log_num_strides < MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
6115 return false;
6116
6117 if (!MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) &&
6118 (log_num_strides < MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
6119 return false;
6120
6121 return true;
6122}
6123
Yishai Hadas79b20a62016-05-23 15:20:50 +03006124static int prepare_user_rq(struct ib_pd *pd,
6125 struct ib_wq_init_attr *init_attr,
6126 struct ib_udata *udata,
6127 struct mlx5_ib_rwq *rwq)
6128{
6129 struct mlx5_ib_dev *dev = to_mdev(pd->device);
6130 struct mlx5_ib_create_wq ucmd = {};
6131 int err;
6132 size_t required_cmd_sz;
6133
Noa Osherovichccc87082017-10-17 18:01:13 +03006134 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
6135 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006136 if (udata->inlen < required_cmd_sz) {
6137 mlx5_ib_dbg(dev, "invalid inlen\n");
6138 return -EINVAL;
6139 }
6140
6141 if (udata->inlen > sizeof(ucmd) &&
6142 !ib_is_udata_cleared(udata, sizeof(ucmd),
6143 udata->inlen - sizeof(ucmd))) {
6144 mlx5_ib_dbg(dev, "inlen is not supported\n");
6145 return -EOPNOTSUPP;
6146 }
6147
6148 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
6149 mlx5_ib_dbg(dev, "copy failed\n");
6150 return -EFAULT;
6151 }
6152
Noa Osherovichccc87082017-10-17 18:01:13 +03006153 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03006154 mlx5_ib_dbg(dev, "invalid comp mask\n");
6155 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03006156 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
6157 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
6158 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
6159 return -EOPNOTSUPP;
6160 }
6161 if ((ucmd.single_stride_log_num_of_bytes <
6162 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
6163 (ucmd.single_stride_log_num_of_bytes >
6164 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
6165 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
6166 ucmd.single_stride_log_num_of_bytes,
6167 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
6168 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
6169 return -EINVAL;
6170 }
Mark Zhangc16339b2019-11-15 17:45:55 +02006171 if (!log_of_strides_valid(dev,
6172 ucmd.single_wqe_log_num_of_strides)) {
6173 mlx5_ib_dbg(
6174 dev,
6175 "Invalid log num strides (%u. Range is %u - %u)\n",
6176 ucmd.single_wqe_log_num_of_strides,
6177 MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) ?
6178 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES :
6179 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
6180 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
Noa Osherovichccc87082017-10-17 18:01:13 +03006181 return -EINVAL;
6182 }
6183 rwq->single_stride_log_num_of_bytes =
6184 ucmd.single_stride_log_num_of_bytes;
6185 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
6186 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
6187 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006188 }
6189
6190 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
6191 if (err) {
6192 mlx5_ib_dbg(dev, "err %d\n", err);
6193 return err;
6194 }
6195
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02006196 err = create_user_rq(dev, pd, udata, rwq, &ucmd);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006197 if (err) {
6198 mlx5_ib_dbg(dev, "err %d\n", err);
Gal Pressman645ba592018-10-08 19:44:03 +03006199 return err;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006200 }
6201
6202 rwq->user_index = ucmd.user_index;
6203 return 0;
6204}
6205
6206struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
6207 struct ib_wq_init_attr *init_attr,
6208 struct ib_udata *udata)
6209{
6210 struct mlx5_ib_dev *dev;
6211 struct mlx5_ib_rwq *rwq;
6212 struct mlx5_ib_create_wq_resp resp = {};
6213 size_t min_resp_len;
6214 int err;
6215
6216 if (!udata)
6217 return ERR_PTR(-ENOSYS);
6218
6219 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6220 if (udata->outlen && udata->outlen < min_resp_len)
6221 return ERR_PTR(-EINVAL);
6222
Maor Gottliebba800132020-03-22 14:49:06 +02006223 if (!capable(CAP_SYS_RAWIO) &&
6224 init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP)
6225 return ERR_PTR(-EPERM);
6226
Yishai Hadas79b20a62016-05-23 15:20:50 +03006227 dev = to_mdev(pd->device);
6228 switch (init_attr->wq_type) {
6229 case IB_WQT_RQ:
6230 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
6231 if (!rwq)
6232 return ERR_PTR(-ENOMEM);
6233 err = prepare_user_rq(pd, init_attr, udata, rwq);
6234 if (err)
6235 goto err;
6236 err = create_rq(rwq, pd, init_attr);
6237 if (err)
6238 goto err_user_rq;
6239 break;
6240 default:
6241 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
6242 init_attr->wq_type);
6243 return ERR_PTR(-EINVAL);
6244 }
6245
Yishai Hadas350d0e42016-08-28 14:58:18 +03006246 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006247 rwq->ibwq.state = IB_WQS_RESET;
6248 if (udata->outlen) {
6249 resp.response_length = offsetof(typeof(resp), response_length) +
6250 sizeof(resp.response_length);
6251 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6252 if (err)
6253 goto err_copy;
6254 }
6255
Yishai Hadas350d0e42016-08-28 14:58:18 +03006256 rwq->core_qp.event = mlx5_ib_wq_event;
6257 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03006258 return &rwq->ibwq;
6259
6260err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03006261 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006262err_user_rq:
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006263 destroy_user_rq(dev, pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006264err:
6265 kfree(rwq);
6266 return ERR_PTR(err);
6267}
6268
Leon Romanovskya49b1dc2019-06-12 15:27:41 +03006269void mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +03006270{
6271 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6272 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6273
Yishai Hadas350d0e42016-08-28 14:58:18 +03006274 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03006275 destroy_user_rq(dev, wq->pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006276 kfree(rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006277}
6278
Yishai Hadasc5f90922016-05-23 15:20:53 +03006279struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
6280 struct ib_rwq_ind_table_init_attr *init_attr,
6281 struct ib_udata *udata)
6282{
6283 struct mlx5_ib_dev *dev = to_mdev(device);
6284 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
6285 int sz = 1 << init_attr->log_ind_tbl_size;
6286 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
6287 size_t min_resp_len;
6288 int inlen;
6289 int err;
6290 int i;
6291 u32 *in;
6292 void *rqtc;
6293
6294 if (udata->inlen > 0 &&
6295 !ib_is_udata_cleared(udata, 0,
6296 udata->inlen))
6297 return ERR_PTR(-EOPNOTSUPP);
6298
Maor Gottliebefd7f402016-10-27 16:36:40 +03006299 if (init_attr->log_ind_tbl_size >
6300 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
6301 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
6302 init_attr->log_ind_tbl_size,
6303 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
6304 return ERR_PTR(-EINVAL);
6305 }
6306
Yishai Hadasc5f90922016-05-23 15:20:53 +03006307 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6308 if (udata->outlen && udata->outlen < min_resp_len)
6309 return ERR_PTR(-EINVAL);
6310
6311 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
6312 if (!rwq_ind_tbl)
6313 return ERR_PTR(-ENOMEM);
6314
6315 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006316 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006317 if (!in) {
6318 err = -ENOMEM;
6319 goto err;
6320 }
6321
6322 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
6323
6324 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
6325 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
6326
6327 for (i = 0; i < sz; i++)
6328 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
6329
Yishai Hadas5deba862018-09-20 21:39:28 +03006330 rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
6331 MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);
6332
Yishai Hadasc5f90922016-05-23 15:20:53 +03006333 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
6334 kvfree(in);
6335
6336 if (err)
6337 goto err;
6338
6339 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
6340 if (udata->outlen) {
6341 resp.response_length = offsetof(typeof(resp), response_length) +
6342 sizeof(resp.response_length);
6343 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6344 if (err)
6345 goto err_copy;
6346 }
6347
6348 return &rwq_ind_tbl->ib_rwq_ind_tbl;
6349
6350err_copy:
Yishai Hadas5deba862018-09-20 21:39:28 +03006351 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006352err:
6353 kfree(rwq_ind_tbl);
6354 return ERR_PTR(err);
6355}
6356
6357int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
6358{
6359 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
6360 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
6361
Yishai Hadas5deba862018-09-20 21:39:28 +03006362 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006363
6364 kfree(rwq_ind_tbl);
6365 return 0;
6366}
6367
Yishai Hadas79b20a62016-05-23 15:20:50 +03006368int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
6369 u32 wq_attr_mask, struct ib_udata *udata)
6370{
6371 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6372 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6373 struct mlx5_ib_modify_wq ucmd = {};
6374 size_t required_cmd_sz;
6375 int curr_wq_state;
6376 int wq_state;
6377 int inlen;
6378 int err;
6379 void *rqc;
6380 void *in;
6381
6382 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
6383 if (udata->inlen < required_cmd_sz)
6384 return -EINVAL;
6385
6386 if (udata->inlen > sizeof(ucmd) &&
6387 !ib_is_udata_cleared(udata, sizeof(ucmd),
6388 udata->inlen - sizeof(ucmd)))
6389 return -EOPNOTSUPP;
6390
6391 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
6392 return -EFAULT;
6393
6394 if (ucmd.comp_mask || ucmd.reserved)
6395 return -EOPNOTSUPP;
6396
6397 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006398 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006399 if (!in)
6400 return -ENOMEM;
6401
6402 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
6403
6404 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
6405 wq_attr->curr_wq_state : wq->state;
6406 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
6407 wq_attr->wq_state : curr_wq_state;
6408 if (curr_wq_state == IB_WQS_ERR)
6409 curr_wq_state = MLX5_RQC_STATE_ERR;
6410 if (wq_state == IB_WQS_ERR)
6411 wq_state = MLX5_RQC_STATE_ERR;
6412 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
Yishai Hadas34d57582018-09-20 21:39:21 +03006413 MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006414 MLX5_SET(rqc, rqc, state, wq_state);
6415
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006416 if (wq_attr_mask & IB_WQ_FLAGS) {
6417 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
6418 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
6419 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
6420 mlx5_ib_dbg(dev, "VLAN offloads are not "
6421 "supported\n");
6422 err = -EOPNOTSUPP;
6423 goto out;
6424 }
6425 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6426 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
6427 MLX5_SET(rqc, rqc, vsd,
6428 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
6429 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02006430
6431 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6432 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
6433 err = -EOPNOTSUPP;
6434 goto out;
6435 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006436 }
6437
Majd Dibbiny23a69642017-01-18 15:25:10 +02006438 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
Parav Pandit3e1f0002019-07-23 10:31:17 +03006439 u16 set_id;
6440
6441 set_id = mlx5_ib_get_counters_id(dev, 0);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006442 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
6443 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6444 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandit3e1f0002019-07-23 10:31:17 +03006445 MLX5_SET(rqc, rqc, counter_set_id, set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006446 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06006447 dev_info_once(
6448 &dev->ib_dev.dev,
6449 "Receive WQ counters are not supported on current FW\n");
Majd Dibbiny23a69642017-01-18 15:25:10 +02006450 }
6451
Yishai Hadas350d0e42016-08-28 14:58:18 +03006452 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006453 if (!err)
6454 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
6455
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006456out:
6457 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006458 return err;
6459}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006460
6461struct mlx5_ib_drain_cqe {
6462 struct ib_cqe cqe;
6463 struct completion done;
6464};
6465
6466static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
6467{
6468 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
6469 struct mlx5_ib_drain_cqe,
6470 cqe);
6471
6472 complete(&cqe->done);
6473}
6474
6475/* This function returns only once the drained WR was completed */
6476static void handle_drain_completion(struct ib_cq *cq,
6477 struct mlx5_ib_drain_cqe *sdrain,
6478 struct mlx5_ib_dev *dev)
6479{
6480 struct mlx5_core_dev *mdev = dev->mdev;
6481
6482 if (cq->poll_ctx == IB_POLL_DIRECT) {
6483 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
6484 ib_process_cq_direct(cq, -1);
6485 return;
6486 }
6487
6488 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6489 struct mlx5_ib_cq *mcq = to_mcq(cq);
6490 bool triggered = false;
6491 unsigned long flags;
6492
6493 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
6494 /* Make sure that the CQ handler won't run if wasn't run yet */
6495 if (!mcq->mcq.reset_notify_added)
6496 mcq->mcq.reset_notify_added = 1;
6497 else
6498 triggered = true;
6499 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
6500
6501 if (triggered) {
6502 /* Wait for any scheduled/running task to be ended */
6503 switch (cq->poll_ctx) {
6504 case IB_POLL_SOFTIRQ:
6505 irq_poll_disable(&cq->iop);
6506 irq_poll_enable(&cq->iop);
6507 break;
6508 case IB_POLL_WORKQUEUE:
6509 cancel_work_sync(&cq->work);
6510 break;
6511 default:
6512 WARN_ON_ONCE(1);
6513 }
6514 }
6515
6516 /* Run the CQ handler - this makes sure that the drain WR will
6517 * be processed if wasn't processed yet.
6518 */
Yishai Hadas4e0e2ea2019-06-30 19:23:27 +03006519 mcq->mcq.comp(&mcq->mcq, NULL);
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006520 }
6521
6522 wait_for_completion(&sdrain->done);
6523}
6524
6525void mlx5_ib_drain_sq(struct ib_qp *qp)
6526{
6527 struct ib_cq *cq = qp->send_cq;
6528 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6529 struct mlx5_ib_drain_cqe sdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006530 const struct ib_send_wr *bad_swr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006531 struct ib_rdma_wr swr = {
6532 .wr = {
6533 .next = NULL,
6534 { .wr_cqe = &sdrain.cqe, },
6535 .opcode = IB_WR_RDMA_WRITE,
6536 },
6537 };
6538 int ret;
6539 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6540 struct mlx5_core_dev *mdev = dev->mdev;
6541
6542 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6543 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6544 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6545 return;
6546 }
6547
6548 sdrain.cqe.done = mlx5_ib_drain_qp_done;
6549 init_completion(&sdrain.done);
6550
6551 ret = _mlx5_ib_post_send(qp, &swr.wr, &bad_swr, true);
6552 if (ret) {
6553 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6554 return;
6555 }
6556
6557 handle_drain_completion(cq, &sdrain, dev);
6558}
6559
6560void mlx5_ib_drain_rq(struct ib_qp *qp)
6561{
6562 struct ib_cq *cq = qp->recv_cq;
6563 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6564 struct mlx5_ib_drain_cqe rdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006565 struct ib_recv_wr rwr = {};
6566 const struct ib_recv_wr *bad_rwr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006567 int ret;
6568 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6569 struct mlx5_core_dev *mdev = dev->mdev;
6570
6571 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6572 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6573 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6574 return;
6575 }
6576
6577 rwr.wr_cqe = &rdrain.cqe;
6578 rdrain.cqe.done = mlx5_ib_drain_qp_done;
6579 init_completion(&rdrain.done);
6580
6581 ret = _mlx5_ib_post_recv(qp, &rwr, &bad_rwr, true);
6582 if (ret) {
6583 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6584 return;
6585 }
6586
6587 handle_drain_completion(cq, &rdrain, dev);
6588}
Mark Zhangd14133d2019-07-02 13:02:36 +03006589
6590/**
6591 * Bind a qp to a counter. If @counter is NULL then bind the qp to
6592 * the default counter
6593 */
6594int mlx5_ib_qp_set_counter(struct ib_qp *qp, struct rdma_counter *counter)
6595{
Mark Zhang10189e82020-01-26 19:17:08 +02006596 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Mark Zhangd14133d2019-07-02 13:02:36 +03006597 struct mlx5_ib_qp *mqp = to_mqp(qp);
6598 int err = 0;
6599
6600 mutex_lock(&mqp->mutex);
6601 if (mqp->state == IB_QPS_RESET) {
6602 qp->counter = counter;
6603 goto out;
6604 }
6605
Mark Zhang10189e82020-01-26 19:17:08 +02006606 if (!MLX5_CAP_GEN(dev->mdev, rts2rts_qp_counters_set_id)) {
6607 err = -EOPNOTSUPP;
6608 goto out;
6609 }
6610
Mark Zhangd14133d2019-07-02 13:02:36 +03006611 if (mqp->state == IB_QPS_RTS) {
6612 err = __mlx5_ib_qp_set_counter(qp, counter);
6613 if (!err)
6614 qp->counter = counter;
6615
6616 goto out;
6617 }
6618
6619 mqp->counter_pending = 1;
6620 qp->counter = counter;
6621
6622out:
6623 mutex_unlock(&mqp->mutex);
6624 return err;
6625}