blob: 1bb445669c809801bc4b9f7b90b3e6faa3edd332 [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/module.h>
34#include <rdma/ib_umem.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020035#include <rdma/ib_cache.h>
Haggai Abramovskycfb5e082016-01-14 19:12:57 +020036#include <rdma/ib_user_verbs.h>
Yishai Hadasc2e53b22017-06-08 16:15:08 +030037#include <linux/mlx5/fs.h>
Eli Cohene126ba92013-07-07 17:25:49 +030038#include "mlx5_ib.h"
Mark Blochb96c9dd2018-01-29 10:40:37 +000039#include "ib_rep.h"
Yishai Hadas443c1cf2018-09-20 21:39:26 +030040#include "cmd.h"
Eli Cohene126ba92013-07-07 17:25:49 +030041
42/* not supported currently */
43static int wq_signature;
44
45enum {
46 MLX5_IB_ACK_REQ_FREQ = 8,
47};
48
49enum {
50 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
51 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
52 MLX5_IB_LINK_TYPE_IB = 0,
53 MLX5_IB_LINK_TYPE_ETH = 1
54};
55
56enum {
57 MLX5_IB_SQ_STRIDE = 6,
Idan Burstein064e5262018-05-02 13:16:39 +030058 MLX5_IB_SQ_UMR_INLINE_THRESHOLD = 64,
Eli Cohene126ba92013-07-07 17:25:49 +030059};
60
61static const u32 mlx5_ib_opcode[] = {
62 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020063 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030064 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
65 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
66 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
67 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
68 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
69 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
70 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
71 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030072 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030073 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
74 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
75 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
76};
77
Erez Shitritf0313962016-02-21 16:27:17 +020078struct mlx5_wqe_eth_pad {
79 u8 rsvd0[16];
80};
Eli Cohene126ba92013-07-07 17:25:49 +030081
Alex Veskereb49ab02016-08-28 12:25:53 +030082enum raw_qp_set_mask_map {
83 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020084 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030085};
86
Alex Vesker0680efa2016-08-28 12:25:52 +030087struct mlx5_modify_raw_qp_param {
88 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030089
90 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang61147f32018-03-19 15:10:30 +020091
92 struct mlx5_rate_limit rl;
93
Alex Veskereb49ab02016-08-28 12:25:53 +030094 u8 rq_q_ctr_id;
Alex Vesker0680efa2016-08-28 12:25:52 +030095};
96
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030097static void get_cqs(enum ib_qp_type qp_type,
98 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
99 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
100
Eli Cohene126ba92013-07-07 17:25:49 +0300101static int is_qp0(enum ib_qp_type qp_type)
102{
103 return qp_type == IB_QPT_SMI;
104}
105
Eli Cohene126ba92013-07-07 17:25:49 +0300106static int is_sqp(enum ib_qp_type qp_type)
107{
108 return is_qp0(qp_type) || is_qp1(qp_type);
109}
110
Haggai Eranc1395a22014-12-11 17:04:14 +0200111/**
Moni Shouafbeb4072019-01-22 08:48:46 +0200112 * mlx5_ib_read_user_wqe_common() - Copy a WQE (or part of) from user WQ
113 * to kernel buffer
Haggai Eranc1395a22014-12-11 17:04:14 +0200114 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200115 * @umem: User space memory where the WQ is
116 * @buffer: buffer to copy to
117 * @buflen: buffer length
118 * @wqe_index: index of WQE to copy from
119 * @wq_offset: offset to start of WQ
120 * @wq_wqe_cnt: number of WQEs in WQ
121 * @wq_wqe_shift: log2 of WQE size
122 * @bcnt: number of bytes to copy
123 * @bytes_copied: number of bytes to copy (return value)
Haggai Eranc1395a22014-12-11 17:04:14 +0200124 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200125 * Copies from start of WQE bcnt or less bytes.
126 * Does not gurantee to copy the entire WQE.
Haggai Eranc1395a22014-12-11 17:04:14 +0200127 *
Moni Shouafbeb4072019-01-22 08:48:46 +0200128 * Return: zero on success, or an error code.
Haggai Eranc1395a22014-12-11 17:04:14 +0200129 */
Moni Shouafbeb4072019-01-22 08:48:46 +0200130static int mlx5_ib_read_user_wqe_common(struct ib_umem *umem,
131 void *buffer,
132 u32 buflen,
133 int wqe_index,
134 int wq_offset,
135 int wq_wqe_cnt,
136 int wq_wqe_shift,
137 int bcnt,
138 size_t *bytes_copied)
Haggai Eranc1395a22014-12-11 17:04:14 +0200139{
Moni Shouafbeb4072019-01-22 08:48:46 +0200140 size_t offset = wq_offset + ((wqe_index % wq_wqe_cnt) << wq_wqe_shift);
141 size_t wq_end = wq_offset + (wq_wqe_cnt << wq_wqe_shift);
142 size_t copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200143 int ret;
144
Moni Shouafbeb4072019-01-22 08:48:46 +0200145 /* don't copy more than requested, more than buffer length or
146 * beyond WQ end
147 */
148 copy_length = min_t(u32, buflen, wq_end - offset);
149 copy_length = min_t(u32, copy_length, bcnt);
Haggai Eranc1395a22014-12-11 17:04:14 +0200150
Moni Shouafbeb4072019-01-22 08:48:46 +0200151 ret = ib_umem_copy_from(buffer, umem, offset, copy_length);
Haggai Eranc1395a22014-12-11 17:04:14 +0200152 if (ret)
153 return ret;
154
Moni Shouafbeb4072019-01-22 08:48:46 +0200155 if (!ret && bytes_copied)
156 *bytes_copied = copy_length;
Haggai Eranc1395a22014-12-11 17:04:14 +0200157
Moni Shouafbeb4072019-01-22 08:48:46 +0200158 return 0;
159}
Haggai Eranc1395a22014-12-11 17:04:14 +0200160
Moni Shouafbeb4072019-01-22 08:48:46 +0200161int mlx5_ib_read_user_wqe_sq(struct mlx5_ib_qp *qp,
162 int wqe_index,
163 void *buffer,
164 int buflen,
165 size_t *bc)
166{
167 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
168 struct ib_umem *umem = base->ubuffer.umem;
169 struct mlx5_ib_wq *wq = &qp->sq;
170 struct mlx5_wqe_ctrl_seg *ctrl;
171 size_t bytes_copied;
172 size_t bytes_copied2;
173 size_t wqe_length;
174 int ret;
175 int ds;
Haggai Eranc1395a22014-12-11 17:04:14 +0200176
Moni Shouafbeb4072019-01-22 08:48:46 +0200177 if (buflen < sizeof(*ctrl))
178 return -EINVAL;
179
180 /* at first read as much as possible */
181 ret = mlx5_ib_read_user_wqe_common(umem,
182 buffer,
183 buflen,
184 wqe_index,
185 wq->offset,
186 wq->wqe_cnt,
187 wq->wqe_shift,
188 buflen,
189 &bytes_copied);
Haggai Eranc1395a22014-12-11 17:04:14 +0200190 if (ret)
191 return ret;
192
Moni Shouafbeb4072019-01-22 08:48:46 +0200193 /* we need at least control segment size to proceed */
194 if (bytes_copied < sizeof(*ctrl))
195 return -EINVAL;
196
197 ctrl = buffer;
198 ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
199 wqe_length = ds * MLX5_WQE_DS_UNITS;
200
201 /* if we copied enough then we are done */
202 if (bytes_copied >= wqe_length) {
203 *bc = bytes_copied;
204 return 0;
205 }
206
207 /* otherwise this a wrapped around wqe
208 * so read the remaining bytes starting
209 * from wqe_index 0
210 */
211 ret = mlx5_ib_read_user_wqe_common(umem,
212 buffer + bytes_copied,
213 buflen - bytes_copied,
214 0,
215 wq->offset,
216 wq->wqe_cnt,
217 wq->wqe_shift,
218 wqe_length - bytes_copied,
219 &bytes_copied2);
220
221 if (ret)
222 return ret;
223 *bc = bytes_copied + bytes_copied2;
224 return 0;
225}
226
227int mlx5_ib_read_user_wqe_rq(struct mlx5_ib_qp *qp,
228 int wqe_index,
229 void *buffer,
230 int buflen,
231 size_t *bc)
232{
233 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
234 struct ib_umem *umem = base->ubuffer.umem;
235 struct mlx5_ib_wq *wq = &qp->rq;
236 size_t bytes_copied;
237 int ret;
238
239 ret = mlx5_ib_read_user_wqe_common(umem,
240 buffer,
241 buflen,
242 wqe_index,
243 wq->offset,
244 wq->wqe_cnt,
245 wq->wqe_shift,
246 buflen,
247 &bytes_copied);
248
249 if (ret)
250 return ret;
251 *bc = bytes_copied;
252 return 0;
253}
254
255int mlx5_ib_read_user_wqe_srq(struct mlx5_ib_srq *srq,
256 int wqe_index,
257 void *buffer,
258 int buflen,
259 size_t *bc)
260{
261 struct ib_umem *umem = srq->umem;
262 size_t bytes_copied;
263 int ret;
264
265 ret = mlx5_ib_read_user_wqe_common(umem,
266 buffer,
267 buflen,
268 wqe_index,
269 0,
270 srq->msrq.max,
271 srq->msrq.wqe_shift,
272 buflen,
273 &bytes_copied);
274
275 if (ret)
276 return ret;
277 *bc = bytes_copied;
278 return 0;
Haggai Eranc1395a22014-12-11 17:04:14 +0200279}
280
Eli Cohene126ba92013-07-07 17:25:49 +0300281static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
282{
283 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
284 struct ib_event event;
285
majd@mellanox.com19098df2016-01-14 19:13:03 +0200286 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
287 /* This event is only valid for trans_qps */
288 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
289 }
Eli Cohene126ba92013-07-07 17:25:49 +0300290
291 if (ibqp->event_handler) {
292 event.device = ibqp->device;
293 event.element.qp = ibqp;
294 switch (type) {
295 case MLX5_EVENT_TYPE_PATH_MIG:
296 event.event = IB_EVENT_PATH_MIG;
297 break;
298 case MLX5_EVENT_TYPE_COMM_EST:
299 event.event = IB_EVENT_COMM_EST;
300 break;
301 case MLX5_EVENT_TYPE_SQ_DRAINED:
302 event.event = IB_EVENT_SQ_DRAINED;
303 break;
304 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
305 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
306 break;
307 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
308 event.event = IB_EVENT_QP_FATAL;
309 break;
310 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
311 event.event = IB_EVENT_PATH_MIG_ERR;
312 break;
313 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
314 event.event = IB_EVENT_QP_REQ_ERR;
315 break;
316 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
317 event.event = IB_EVENT_QP_ACCESS_ERR;
318 break;
319 default:
320 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
321 return;
322 }
323
324 ibqp->event_handler(&event, ibqp->qp_context);
325 }
326}
327
328static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
329 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
330{
331 int wqe_size;
332 int wq_size;
333
334 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300335 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300336 return -EINVAL;
337
338 if (!has_rq) {
339 qp->rq.max_gs = 0;
340 qp->rq.wqe_cnt = 0;
341 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300342 cap->max_recv_wr = 0;
343 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300344 } else {
345 if (ucmd) {
346 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300347 if (ucmd->rq_wqe_shift > BITS_PER_BYTE * sizeof(ucmd->rq_wqe_shift))
348 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300349 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300350 if ((1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) < qp->wq_sig)
351 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300352 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
353 qp->rq.max_post = qp->rq.wqe_cnt;
354 } else {
355 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
356 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
357 wqe_size = roundup_pow_of_two(wqe_size);
358 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
359 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
360 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300361 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300362 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
363 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300364 MLX5_CAP_GEN(dev->mdev,
365 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300366 return -EINVAL;
367 }
368 qp->rq.wqe_shift = ilog2(wqe_size);
369 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
370 qp->rq.max_post = qp->rq.wqe_cnt;
371 }
372 }
373
374 return 0;
375}
376
Erez Shitritf0313962016-02-21 16:27:17 +0200377static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300378{
Andi Shyti618af382013-07-16 15:35:01 +0200379 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300380
Erez Shitritf0313962016-02-21 16:27:17 +0200381 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300382 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300383 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300384 /* fall through */
385 case IB_QPT_RC:
386 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200387 max(sizeof(struct mlx5_wqe_atomic_seg) +
388 sizeof(struct mlx5_wqe_raddr_seg),
389 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
Idan Burstein064e5262018-05-02 13:16:39 +0300390 sizeof(struct mlx5_mkey_seg) +
391 MLX5_IB_SQ_UMR_INLINE_THRESHOLD /
392 MLX5_IB_UMR_OCTOWORD);
Eli Cohene126ba92013-07-07 17:25:49 +0300393 break;
394
Eli Cohenb125a542013-09-11 16:35:22 +0300395 case IB_QPT_XRC_TGT:
396 return 0;
397
Eli Cohene126ba92013-07-07 17:25:49 +0300398 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300399 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200400 max(sizeof(struct mlx5_wqe_raddr_seg),
401 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
402 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300403 break;
404
405 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200406 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
407 size += sizeof(struct mlx5_wqe_eth_pad) +
408 sizeof(struct mlx5_wqe_eth_seg);
409 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300410 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200411 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300412 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300413 sizeof(struct mlx5_wqe_datagram_seg);
414 break;
415
416 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300417 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300418 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
419 sizeof(struct mlx5_mkey_seg);
420 break;
421
422 default:
423 return -EINVAL;
424 }
425
426 return size;
427}
428
429static int calc_send_wqe(struct ib_qp_init_attr *attr)
430{
431 int inl_size = 0;
432 int size;
433
Erez Shitritf0313962016-02-21 16:27:17 +0200434 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300435 if (size < 0)
436 return size;
437
438 if (attr->cap.max_inline_data) {
439 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
440 attr->cap.max_inline_data;
441 }
442
443 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200444 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN &&
445 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
446 return MLX5_SIG_WQE_SIZE;
447 else
448 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300449}
450
Eli Cohen288c01b2016-10-27 16:36:45 +0300451static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
452{
453 int max_sge;
454
455 if (attr->qp_type == IB_QPT_RC)
456 max_sge = (min_t(int, wqe_size, 512) -
457 sizeof(struct mlx5_wqe_ctrl_seg) -
458 sizeof(struct mlx5_wqe_raddr_seg)) /
459 sizeof(struct mlx5_wqe_data_seg);
460 else if (attr->qp_type == IB_QPT_XRC_INI)
461 max_sge = (min_t(int, wqe_size, 512) -
462 sizeof(struct mlx5_wqe_ctrl_seg) -
463 sizeof(struct mlx5_wqe_xrc_seg) -
464 sizeof(struct mlx5_wqe_raddr_seg)) /
465 sizeof(struct mlx5_wqe_data_seg);
466 else
467 max_sge = (wqe_size - sq_overhead(attr)) /
468 sizeof(struct mlx5_wqe_data_seg);
469
470 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
471 sizeof(struct mlx5_wqe_data_seg));
472}
473
Eli Cohene126ba92013-07-07 17:25:49 +0300474static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
475 struct mlx5_ib_qp *qp)
476{
477 int wqe_size;
478 int wq_size;
479
480 if (!attr->cap.max_send_wr)
481 return 0;
482
483 wqe_size = calc_send_wqe(attr);
484 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
485 if (wqe_size < 0)
486 return wqe_size;
487
Saeed Mahameed938fe832015-05-28 22:28:41 +0300488 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300489 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300490 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300491 return -EINVAL;
492 }
493
Erez Shitritf0313962016-02-21 16:27:17 +0200494 qp->max_inline_data = wqe_size - sq_overhead(attr) -
495 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300496 attr->cap.max_inline_data = qp->max_inline_data;
497
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200498 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN)
499 qp->signature_en = true;
500
Eli Cohene126ba92013-07-07 17:25:49 +0300501 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
502 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300503 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800504 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
505 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300506 qp->sq.wqe_cnt,
507 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300508 return -ENOMEM;
509 }
Eli Cohene126ba92013-07-07 17:25:49 +0300510 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300511 qp->sq.max_gs = get_send_sge(attr, wqe_size);
512 if (qp->sq.max_gs < attr->cap.max_send_sge)
513 return -ENOMEM;
514
515 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300516 qp->sq.max_post = wq_size / wqe_size;
517 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300518
519 return wq_size;
520}
521
522static int set_user_buf_size(struct mlx5_ib_dev *dev,
523 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200524 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200525 struct mlx5_ib_qp_base *base,
526 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300527{
528 int desc_sz = 1 << qp->sq.wqe_shift;
529
Saeed Mahameed938fe832015-05-28 22:28:41 +0300530 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300531 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300532 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300533 return -EINVAL;
534 }
535
Gal Pressmanaf8b38e2019-02-06 15:45:35 +0200536 if (ucmd->sq_wqe_count && !is_power_of_2(ucmd->sq_wqe_count)) {
537 mlx5_ib_warn(dev, "sq_wqe_count %d is not a power of two\n",
538 ucmd->sq_wqe_count);
Eli Cohene126ba92013-07-07 17:25:49 +0300539 return -EINVAL;
540 }
541
542 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
543
Saeed Mahameed938fe832015-05-28 22:28:41 +0300544 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300545 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300546 qp->sq.wqe_cnt,
547 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300548 return -EINVAL;
549 }
550
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300551 if (attr->qp_type == IB_QPT_RAW_PACKET ||
552 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200553 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
554 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
555 } else {
556 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
557 (qp->sq.wqe_cnt << 6);
558 }
Eli Cohene126ba92013-07-07 17:25:49 +0300559
560 return 0;
561}
562
563static int qp_has_rq(struct ib_qp_init_attr *attr)
564{
565 if (attr->qp_type == IB_QPT_XRC_INI ||
566 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
567 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
568 !attr->cap.max_recv_wr)
569 return 0;
570
571 return 1;
572}
573
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200574enum {
575 /* this is the first blue flame register in the array of bfregs assigned
576 * to a processes. Since we do not use it for blue flame but rather
577 * regular 64 bit doorbells, we do not need a lock for maintaiing
578 * "odd/even" order
579 */
580 NUM_NON_BLUE_FLAME_BFREGS = 1,
581};
582
Eli Cohenb037c292017-01-03 23:55:26 +0200583static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
584{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200585 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200586}
587
588static int num_med_bfreg(struct mlx5_ib_dev *dev,
589 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200590{
591 int n;
592
Eli Cohenb037c292017-01-03 23:55:26 +0200593 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
594 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200595
596 return n >= 0 ? n : 0;
597}
598
Yishai Hadas18b03622018-05-07 10:20:01 +0300599static int first_med_bfreg(struct mlx5_ib_dev *dev,
600 struct mlx5_bfreg_info *bfregi)
601{
602 return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
603}
604
Eli Cohenb037c292017-01-03 23:55:26 +0200605static int first_hi_bfreg(struct mlx5_ib_dev *dev,
606 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200607{
608 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200609
Eli Cohenb037c292017-01-03 23:55:26 +0200610 med = num_med_bfreg(dev, bfregi);
611 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200612}
613
Eli Cohenb037c292017-01-03 23:55:26 +0200614static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
615 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300616{
Eli Cohene126ba92013-07-07 17:25:49 +0300617 int i;
618
Eli Cohenb037c292017-01-03 23:55:26 +0200619 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
620 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200621 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300622 return i;
623 }
624 }
625
626 return -ENOMEM;
627}
628
Eli Cohenb037c292017-01-03 23:55:26 +0200629static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
630 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300631{
Yishai Hadas18b03622018-05-07 10:20:01 +0300632 int minidx = first_med_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300633 int i;
634
Yishai Hadas18b03622018-05-07 10:20:01 +0300635 if (minidx < 0)
636 return minidx;
637
638 for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200639 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300640 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200641 if (!bfregi->count[minidx])
642 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300643 }
644
Eli Cohen2f5ff262017-01-03 23:55:21 +0200645 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300646 return minidx;
647}
648
Eli Cohenb037c292017-01-03 23:55:26 +0200649static int alloc_bfreg(struct mlx5_ib_dev *dev,
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300650 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300651{
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300652 int bfregn = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +0300653
Eli Cohen2f5ff262017-01-03 23:55:21 +0200654 mutex_lock(&bfregi->lock);
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300655 if (bfregi->ver >= 2) {
656 bfregn = alloc_high_class_bfreg(dev, bfregi);
657 if (bfregn < 0)
658 bfregn = alloc_med_class_bfreg(dev, bfregi);
659 }
660
661 if (bfregn < 0) {
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200662 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200663 bfregn = 0;
664 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300665 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200666 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300667
Eli Cohen2f5ff262017-01-03 23:55:21 +0200668 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300669}
670
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200671void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300672{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200673 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200674 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200675 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300676}
677
678static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
679{
680 switch (state) {
681 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
682 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
683 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
684 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
685 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
686 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
687 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
688 default: return -1;
689 }
690}
691
692static int to_mlx5_st(enum ib_qp_type type)
693{
694 switch (type) {
695 case IB_QPT_RC: return MLX5_QP_ST_RC;
696 case IB_QPT_UC: return MLX5_QP_ST_UC;
697 case IB_QPT_UD: return MLX5_QP_ST_UD;
698 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
699 case IB_QPT_XRC_INI:
700 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
701 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200702 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200703 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300704 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300705 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200706 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300707 case IB_QPT_MAX:
708 default: return -EINVAL;
709 }
710}
711
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300712static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
713 struct mlx5_ib_cq *recv_cq);
714static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
715 struct mlx5_ib_cq *recv_cq);
716
Yishai Hadas7c043e92018-06-17 13:00:03 +0300717int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300718 struct mlx5_bfreg_info *bfregi, u32 bfregn,
Yishai Hadas7c043e92018-06-17 13:00:03 +0300719 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300720{
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300721 unsigned int bfregs_per_sys_page;
722 u32 index_of_sys_page;
723 u32 offset;
Eli Cohenb037c292017-01-03 23:55:26 +0200724
725 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
726 MLX5_NON_FP_BFREGS_PER_UAR;
727 index_of_sys_page = bfregn / bfregs_per_sys_page;
728
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200729 if (dyn_bfreg) {
730 index_of_sys_page += bfregi->num_static_sys_pages;
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300731
732 if (index_of_sys_page >= bfregi->num_sys_pages)
733 return -EINVAL;
734
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200735 if (bfregn > bfregi->num_dyn_bfregs ||
736 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
737 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
738 return -EINVAL;
739 }
740 }
Eli Cohenb037c292017-01-03 23:55:26 +0200741
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200742 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200743 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300744}
745
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200746static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200747 unsigned long addr, size_t size,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200748 struct ib_umem **umem, int *npages, int *page_shift,
749 int *ncont, u32 *offset)
majd@mellanox.com19098df2016-01-14 19:13:03 +0200750{
751 int err;
752
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200753 *umem = ib_umem_get(udata, addr, size, 0, 0);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200754 if (IS_ERR(*umem)) {
755 mlx5_ib_dbg(dev, "umem_get failed\n");
756 return PTR_ERR(*umem);
757 }
758
Majd Dibbiny762f8992016-10-27 16:36:47 +0300759 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200760
761 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
762 if (err) {
763 mlx5_ib_warn(dev, "bad offset\n");
764 goto err_umem;
765 }
766
767 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
768 addr, size, *npages, *page_shift, *ncont, *offset);
769
770 return 0;
771
772err_umem:
773 ib_umem_release(*umem);
774 *umem = NULL;
775
776 return err;
777}
778
Maor Gottliebfe248c32017-05-30 10:29:14 +0300779static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300780 struct mlx5_ib_rwq *rwq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300781{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300782 struct mlx5_ib_ucontext *context =
783 rdma_udata_to_drv_context(
784 udata,
785 struct mlx5_ib_ucontext,
786 ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300787
Maor Gottliebfe248c32017-05-30 10:29:14 +0300788 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
789 atomic_dec(&dev->delay_drop.rqs_cnt);
790
Yishai Hadas79b20a62016-05-23 15:20:50 +0300791 mlx5_ib_db_unmap_user(context, &rwq->db);
792 if (rwq->umem)
793 ib_umem_release(rwq->umem);
794}
795
796static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200797 struct ib_udata *udata, struct mlx5_ib_rwq *rwq,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300798 struct mlx5_ib_create_wq *ucmd)
799{
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200800 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
801 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300802 int page_shift = 0;
803 int npages;
804 u32 offset = 0;
805 int ncont = 0;
806 int err;
807
808 if (!ucmd->buf_addr)
809 return -EINVAL;
810
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200811 rwq->umem = ib_umem_get(udata, ucmd->buf_addr, rwq->buf_size, 0, 0);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300812 if (IS_ERR(rwq->umem)) {
813 mlx5_ib_dbg(dev, "umem_get failed\n");
814 err = PTR_ERR(rwq->umem);
815 return err;
816 }
817
Majd Dibbiny762f8992016-10-27 16:36:47 +0300818 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300819 &ncont, NULL);
820 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
821 &rwq->rq_page_offset);
822 if (err) {
823 mlx5_ib_warn(dev, "bad offset\n");
824 goto err_umem;
825 }
826
827 rwq->rq_num_pas = ncont;
828 rwq->page_shift = page_shift;
829 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
830 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
831
832 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
833 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
834 npages, page_shift, ncont, offset);
835
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200836 err = mlx5_ib_db_map_user(ucontext, udata, ucmd->db_addr, &rwq->db);
Yishai Hadas79b20a62016-05-23 15:20:50 +0300837 if (err) {
838 mlx5_ib_dbg(dev, "map failed\n");
839 goto err_umem;
840 }
841
842 rwq->create_type = MLX5_WQ_USER;
843 return 0;
844
845err_umem:
846 ib_umem_release(rwq->umem);
847 return err;
848}
849
Eli Cohenb037c292017-01-03 23:55:26 +0200850static int adjust_bfregn(struct mlx5_ib_dev *dev,
851 struct mlx5_bfreg_info *bfregi, int bfregn)
852{
853 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
854 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
855}
856
Eli Cohene126ba92013-07-07 17:25:49 +0300857static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
858 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200859 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300860 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200861 struct mlx5_ib_create_qp_resp *resp, int *inlen,
862 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300863{
864 struct mlx5_ib_ucontext *context;
865 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200866 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200867 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200868 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300869 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200870 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200871 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200872 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300873 __be64 *pas;
874 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300875 int err;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200876 u16 uid;
Eli Cohene126ba92013-07-07 17:25:49 +0300877
878 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
879 if (err) {
880 mlx5_ib_dbg(dev, "copy failed\n");
881 return err;
882 }
883
Shamir Rabinovitch89944452019-02-07 18:44:49 +0200884 context = rdma_udata_to_drv_context(udata, struct mlx5_ib_ucontext,
885 ibucontext);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200886 if (ucmd.flags & MLX5_QP_FLAG_BFREG_INDEX) {
887 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
888 ucmd.bfreg_index, true);
889 if (uar_index < 0)
890 return uar_index;
891
892 bfregn = MLX5_IB_INVALID_BFREG;
893 } else if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL) {
894 /*
895 * TBD: should come from the verbs when we have the API
896 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200897 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200898 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200899 }
Leon Romanovsky051f2632015-12-20 12:16:11 +0200900 else {
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300901 bfregn = alloc_bfreg(dev, &context->bfregi);
902 if (bfregn < 0)
903 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300904 }
905
Eli Cohen2f5ff262017-01-03 23:55:21 +0200906 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200907 if (bfregn != MLX5_IB_INVALID_BFREG)
908 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
909 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300910
Haggai Eran48fea832014-05-22 14:50:11 +0300911 qp->rq.offset = 0;
912 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
913 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
914
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200915 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300916 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200917 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300918
majd@mellanox.com19098df2016-01-14 19:13:03 +0200919 if (ucmd.buf_addr && ubuffer->buf_size) {
920 ubuffer->buf_addr = ucmd.buf_addr;
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200921 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr,
922 ubuffer->buf_size, &ubuffer->umem,
923 &npages, &page_shift, &ncont, &offset);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200924 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200925 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200926 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200927 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300928 }
Eli Cohene126ba92013-07-07 17:25:49 +0300929
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300930 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
931 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300932 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300933 if (!*in) {
934 err = -ENOMEM;
935 goto err_umem;
936 }
Eli Cohene126ba92013-07-07 17:25:49 +0300937
Yishai Hadas7422edc2018-12-23 13:12:21 +0200938 uid = (attr->qp_type != IB_QPT_XRC_TGT &&
939 attr->qp_type != IB_QPT_XRC_INI) ? to_mpd(pd)->uid : 0;
Yishai Hadas5aa37712018-11-26 08:28:38 +0200940 MLX5_SET(create_qp_in, *in, uid, uid);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300941 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
942 if (ubuffer->umem)
943 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
944
945 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
946
947 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
948 MLX5_SET(qpc, qpc, page_offset, offset);
949
950 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200951 if (bfregn != MLX5_IB_INVALID_BFREG)
952 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
953 else
954 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200955 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300956
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +0200957 err = mlx5_ib_db_map_user(context, udata, ucmd.db_addr, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300958 if (err) {
959 mlx5_ib_dbg(dev, "map failed\n");
960 goto err_free;
961 }
962
Jason Gunthorpe41d902c2018-04-03 10:00:53 +0300963 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
Eli Cohene126ba92013-07-07 17:25:49 +0300964 if (err) {
965 mlx5_ib_dbg(dev, "copy failed\n");
966 goto err_unmap;
967 }
968 qp->create_type = MLX5_QP_USER;
969
970 return 0;
971
972err_unmap:
973 mlx5_ib_db_unmap_user(context, &qp->db);
974
975err_free:
Al Viro479163f2014-11-20 08:13:57 +0000976 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300977
978err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200979 if (ubuffer->umem)
980 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300981
Eli Cohen2f5ff262017-01-03 23:55:21 +0200982err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200983 if (bfregn != MLX5_IB_INVALID_BFREG)
984 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300985 return err;
986}
987
Eli Cohenb037c292017-01-03 23:55:26 +0200988static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300989 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base,
990 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +0300991{
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +0300992 struct mlx5_ib_ucontext *context =
993 rdma_udata_to_drv_context(
994 udata,
995 struct mlx5_ib_ucontext,
996 ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +0300997
Eli Cohene126ba92013-07-07 17:25:49 +0300998 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200999 if (base->ubuffer.umem)
1000 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +02001001
1002 /*
1003 * Free only the BFREGs which are handled by the kernel.
1004 * BFREGs of UARs allocated dynamically are handled by user.
1005 */
1006 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
1007 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001008}
1009
Guy Levi34f4c952018-11-26 08:15:50 +02001010/* get_sq_edge - Get the next nearby edge.
1011 *
1012 * An 'edge' is defined as the first following address after the end
1013 * of the fragment or the SQ. Accordingly, during the WQE construction
1014 * which repetitively increases the pointer to write the next data, it
1015 * simply should check if it gets to an edge.
1016 *
1017 * @sq - SQ buffer.
1018 * @idx - Stride index in the SQ buffer.
1019 *
1020 * Return:
1021 * The new edge.
1022 */
1023static void *get_sq_edge(struct mlx5_ib_wq *sq, u32 idx)
1024{
1025 void *fragment_end;
1026
1027 fragment_end = mlx5_frag_buf_get_wqe
1028 (&sq->fbc,
1029 mlx5_frag_buf_get_idx_last_contig_stride(&sq->fbc, idx));
1030
1031 return fragment_end + MLX5_SEND_WQE_BB;
1032}
1033
Eli Cohene126ba92013-07-07 17:25:49 +03001034static int create_kernel_qp(struct mlx5_ib_dev *dev,
1035 struct ib_qp_init_attr *init_attr,
1036 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001037 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +02001038 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +03001039{
Eli Cohene126ba92013-07-07 17:25:49 +03001040 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001041 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +03001042 int err;
1043
Erez Shitritf0313962016-02-21 16:27:17 +02001044 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
1045 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +02001046 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +03001047 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +02001048 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +02001049 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001050
1051 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001052 qp->bf.bfreg = &dev->fp_bfreg;
1053 else
1054 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +03001055
Eli Cohend8030b02017-02-09 19:31:47 +02001056 /* We need to divide by two since each register is comprised of
1057 * two buffers of identical size, namely odd and even
1058 */
1059 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001060 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +03001061
1062 err = calc_sq_size(dev, init_attr, qp);
1063 if (err < 0) {
1064 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001065 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001066 }
1067
1068 qp->rq.offset = 0;
1069 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001070 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +03001071
Guy Levi34f4c952018-11-26 08:15:50 +02001072 err = mlx5_frag_buf_alloc_node(dev->mdev, base->ubuffer.buf_size,
1073 &qp->buf, dev->mdev->priv.numa_node);
Eli Cohene126ba92013-07-07 17:25:49 +03001074 if (err) {
1075 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02001076 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001077 }
1078
Guy Levi34f4c952018-11-26 08:15:50 +02001079 if (qp->rq.wqe_cnt)
1080 mlx5_init_fbc(qp->buf.frags, qp->rq.wqe_shift,
1081 ilog2(qp->rq.wqe_cnt), &qp->rq.fbc);
1082
1083 if (qp->sq.wqe_cnt) {
1084 int sq_strides_offset = (qp->sq.offset & (PAGE_SIZE - 1)) /
1085 MLX5_SEND_WQE_BB;
1086 mlx5_init_fbc_offset(qp->buf.frags +
1087 (qp->sq.offset / PAGE_SIZE),
1088 ilog2(MLX5_SEND_WQE_BB),
1089 ilog2(qp->sq.wqe_cnt),
1090 sq_strides_offset, &qp->sq.fbc);
1091
1092 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
1093 }
1094
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001095 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
1096 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001097 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001098 if (!*in) {
1099 err = -ENOMEM;
1100 goto err_buf;
1101 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001102
1103 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
1104 MLX5_SET(qpc, qpc, uar_page, uar_index);
1105 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1106
Eli Cohene126ba92013-07-07 17:25:49 +03001107 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001108 MLX5_SET(qpc, qpc, fre, 1);
1109 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001110
Haggai Eranb11a4f92016-02-29 15:45:03 +02001111 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001112 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +02001113 qp->flags |= MLX5_IB_QP_SQPN_QP1;
1114 }
1115
Guy Levi34f4c952018-11-26 08:15:50 +02001116 mlx5_fill_page_frag_array(&qp->buf,
1117 (__be64 *)MLX5_ADDR_OF(create_qp_in,
1118 *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +03001119
Jack Morgenstein9603b612014-07-28 23:30:22 +03001120 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001121 if (err) {
1122 mlx5_ib_dbg(dev, "err %d\n", err);
1123 goto err_free;
1124 }
1125
Li Dongyangb5883002017-08-16 23:31:22 +10001126 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
1127 sizeof(*qp->sq.wrid), GFP_KERNEL);
1128 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1129 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1130 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1131 sizeof(*qp->rq.wrid), GFP_KERNEL);
1132 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1133 sizeof(*qp->sq.w_list), GFP_KERNEL);
1134 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1135 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001136
1137 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1138 !qp->sq.w_list || !qp->sq.wqe_head) {
1139 err = -ENOMEM;
1140 goto err_wrid;
1141 }
1142 qp->create_type = MLX5_QP_KERNEL;
1143
1144 return 0;
1145
1146err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001147 kvfree(qp->sq.wqe_head);
1148 kvfree(qp->sq.w_list);
1149 kvfree(qp->sq.wrid);
1150 kvfree(qp->sq.wr_data);
1151 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001152 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001153
1154err_free:
Al Viro479163f2014-11-20 08:13:57 +00001155 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001156
1157err_buf:
Guy Levi34f4c952018-11-26 08:15:50 +02001158 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001159 return err;
1160}
1161
1162static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1163{
Li Dongyangb5883002017-08-16 23:31:22 +10001164 kvfree(qp->sq.wqe_head);
1165 kvfree(qp->sq.w_list);
1166 kvfree(qp->sq.wrid);
1167 kvfree(qp->sq.wr_data);
1168 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001169 mlx5_db_free(dev->mdev, &qp->db);
Guy Levi34f4c952018-11-26 08:15:50 +02001170 mlx5_frag_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001171}
1172
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001173static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001174{
1175 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Moni Shouac32a4f22018-01-02 16:19:32 +02001176 (attr->qp_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001177 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001178 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001179 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001180 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001181 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001182 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001183}
1184
1185static int is_connected(enum ib_qp_type qp_type)
1186{
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001187 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC ||
1188 qp_type == MLX5_IB_QPT_DCI)
Eli Cohene126ba92013-07-07 17:25:49 +03001189 return 1;
1190
1191 return 0;
1192}
1193
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001194static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001195 struct mlx5_ib_qp *qp,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001196 struct mlx5_ib_sq *sq, u32 tdn,
1197 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001198{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001199 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001200 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1201
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001202 MLX5_SET(create_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001203 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001204 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1205 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1206
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001207 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1208}
1209
1210static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001211 struct mlx5_ib_sq *sq, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001212{
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001213 mlx5_cmd_destroy_tis(dev->mdev, sq->tisn, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001214}
1215
Mark Blochb96c9dd2018-01-29 10:40:37 +00001216static void destroy_flow_rule_vport_sq(struct mlx5_ib_dev *dev,
1217 struct mlx5_ib_sq *sq)
1218{
1219 if (sq->flow_rule)
1220 mlx5_del_flow_rules(sq->flow_rule);
1221}
1222
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001223static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001224 struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001225 struct mlx5_ib_sq *sq, void *qpin,
1226 struct ib_pd *pd)
1227{
1228 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1229 __be64 *pas;
1230 void *in;
1231 void *sqc;
1232 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1233 void *wq;
1234 int inlen;
1235 int err;
1236 int page_shift = 0;
1237 int npages;
1238 int ncont = 0;
1239 u32 offset = 0;
1240
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001241 err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr, ubuffer->buf_size,
1242 &sq->ubuffer.umem, &npages, &page_shift, &ncont,
1243 &offset);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001244 if (err)
1245 return err;
1246
1247 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001248 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001249 if (!in) {
1250 err = -ENOMEM;
1251 goto err_umem;
1252 }
1253
Yishai Hadasc14003f2018-09-20 21:39:22 +03001254 MLX5_SET(create_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001255 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1256 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001257 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1258 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001259 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1260 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1261 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1262 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1263 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001264 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1265 MLX5_CAP_ETH(dev->mdev, swp))
1266 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001267
1268 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1269 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1270 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1271 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1272 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1273 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1274 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1275 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1276 MLX5_SET(wq, wq, page_offset, offset);
1277
1278 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1279 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1280
1281 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1282
1283 kvfree(in);
1284
1285 if (err)
1286 goto err_umem;
1287
Mark Blochb96c9dd2018-01-29 10:40:37 +00001288 err = create_flow_rule_vport_sq(dev, sq);
1289 if (err)
1290 goto err_flow;
1291
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001292 return 0;
1293
Mark Blochb96c9dd2018-01-29 10:40:37 +00001294err_flow:
1295 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1296
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001297err_umem:
1298 ib_umem_release(sq->ubuffer.umem);
1299 sq->ubuffer.umem = NULL;
1300
1301 return err;
1302}
1303
1304static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1305 struct mlx5_ib_sq *sq)
1306{
Mark Blochb96c9dd2018-01-29 10:40:37 +00001307 destroy_flow_rule_vport_sq(dev, sq);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001308 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1309 ib_umem_release(sq->ubuffer.umem);
1310}
1311
Boris Pismenny2c292db2018-03-08 15:51:40 +02001312static size_t get_rq_pas_size(void *qpc)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001313{
1314 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1315 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1316 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1317 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1318 u32 po_quanta = 1 << (log_page_size - 6);
1319 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1320 u32 page_size = 1 << log_page_size;
1321 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1322 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1323
1324 return rq_num_pas * sizeof(u64);
1325}
1326
1327static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001328 struct mlx5_ib_rq *rq, void *qpin,
Yishai Hadas34d57582018-09-20 21:39:21 +03001329 size_t qpinlen, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001330{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001331 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001332 __be64 *pas;
1333 __be64 *qp_pas;
1334 void *in;
1335 void *rqc;
1336 void *wq;
1337 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001338 size_t rq_pas_size = get_rq_pas_size(qpc);
1339 size_t inlen;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001340 int err;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001341
1342 if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
1343 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001344
1345 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001346 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001347 if (!in)
1348 return -ENOMEM;
1349
Yishai Hadas34d57582018-09-20 21:39:21 +03001350 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001351 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001352 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1353 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001354 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1355 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1356 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1357 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1358 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1359
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001360 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1361 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1362
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001363 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1364 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001365 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1366 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001367 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1368 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1369 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1370 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1371 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1372 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1373
1374 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1375 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1376 memcpy(pas, qp_pas, rq_pas_size);
1377
1378 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1379
1380 kvfree(in);
1381
1382 return err;
1383}
1384
1385static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1386 struct mlx5_ib_rq *rq)
1387{
1388 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1389}
1390
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001391static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1392{
1393 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1394 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1395 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1396}
1397
Mark Bloch0042f9e2018-09-17 13:30:49 +03001398static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1399 struct mlx5_ib_rq *rq,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001400 u32 qp_flags_en,
1401 struct ib_pd *pd)
Mark Bloch0042f9e2018-09-17 13:30:49 +03001402{
1403 if (qp_flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1404 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1405 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001406 mlx5_cmd_destroy_tir(dev->mdev, rq->tirn, to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001407}
1408
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001409static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001410 struct mlx5_ib_rq *rq, u32 tdn,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001411 u32 *qp_flags_en,
1412 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001413{
Mark Bloch175edba2018-09-17 13:30:48 +03001414 u8 lb_flag = 0;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001415 u32 *in;
1416 void *tirc;
1417 int inlen;
1418 int err;
1419
1420 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001421 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001422 if (!in)
1423 return -ENOMEM;
1424
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001425 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001426 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1427 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1428 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1429 MLX5_SET(tirc, tirc, transport_domain, tdn);
Mark Bloch175edba2018-09-17 13:30:48 +03001430 if (*qp_flags_en & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001431 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001432
Mark Bloch175edba2018-09-17 13:30:48 +03001433 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
1434 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1435
1436 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
1437 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1438
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001439 if (dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001440 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1441 *qp_flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1442 }
1443
1444 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
Mark Blochec9c2fb2018-01-15 13:11:37 +00001445
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001446 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1447
Mark Bloch0042f9e2018-09-17 13:30:49 +03001448 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1449 err = mlx5_ib_enable_lb(dev, false, true);
1450
1451 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001452 destroy_raw_packet_qp_tir(dev, rq, 0, pd);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001453 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001454 kvfree(in);
1455
1456 return err;
1457}
1458
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001459static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001460 u32 *in, size_t inlen,
Yishai Hadas7f720522018-09-20 21:45:18 +03001461 struct ib_pd *pd,
1462 struct ib_udata *udata,
1463 struct mlx5_ib_create_qp_resp *resp)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001464{
1465 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1466 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1467 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001468 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1469 udata, struct mlx5_ib_ucontext, ibucontext);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001470 int err;
1471 u32 tdn = mucontext->tdn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001472 u16 uid = to_mpd(pd)->uid;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001473
1474 if (qp->sq.wqe_cnt) {
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001475 err = create_raw_packet_qp_tis(dev, qp, sq, tdn, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001476 if (err)
1477 return err;
1478
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02001479 err = create_raw_packet_qp_sq(dev, udata, sq, in, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001480 if (err)
1481 goto err_destroy_tis;
1482
Yishai Hadas7f720522018-09-20 21:45:18 +03001483 if (uid) {
1484 resp->tisn = sq->tisn;
1485 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TISN;
1486 resp->sqn = sq->base.mqp.qpn;
1487 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_SQN;
1488 }
1489
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001490 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001491 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001492 }
1493
1494 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001495 rq->base.container_mibqp = qp;
1496
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001497 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1498 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001499 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1500 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
Yishai Hadas34d57582018-09-20 21:39:21 +03001501 err = create_raw_packet_qp_rq(dev, rq, in, inlen, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001502 if (err)
1503 goto err_destroy_sq;
1504
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001505 err = create_raw_packet_qp_tir(dev, rq, tdn, &qp->flags_en, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001506 if (err)
1507 goto err_destroy_rq;
Yishai Hadas7f720522018-09-20 21:45:18 +03001508
1509 if (uid) {
1510 resp->rqn = rq->base.mqp.qpn;
1511 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_RQN;
1512 resp->tirn = rq->tirn;
1513 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1514 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001515 }
1516
1517 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1518 rq->base.mqp.qpn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001519 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
1520 if (err)
1521 goto err_destroy_tir;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001522
1523 return 0;
1524
Yishai Hadas7f720522018-09-20 21:45:18 +03001525err_destroy_tir:
1526 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001527err_destroy_rq:
1528 destroy_raw_packet_qp_rq(dev, rq);
1529err_destroy_sq:
1530 if (!qp->sq.wqe_cnt)
1531 return err;
1532 destroy_raw_packet_qp_sq(dev, sq);
1533err_destroy_tis:
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001534 destroy_raw_packet_qp_tis(dev, sq, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001535
1536 return err;
1537}
1538
1539static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1540 struct mlx5_ib_qp *qp)
1541{
1542 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1543 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1544 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1545
1546 if (qp->rq.wqe_cnt) {
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001547 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001548 destroy_raw_packet_qp_rq(dev, rq);
1549 }
1550
1551 if (qp->sq.wqe_cnt) {
1552 destroy_raw_packet_qp_sq(dev, sq);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001553 destroy_raw_packet_qp_tis(dev, sq, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001554 }
1555}
1556
1557static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1558 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1559{
1560 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1561 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1562
1563 sq->sq = &qp->sq;
1564 rq->rq = &qp->rq;
1565 sq->doorbell = &qp->db;
1566 rq->doorbell = &qp->db;
1567}
1568
Yishai Hadas28d61372016-05-23 15:20:56 +03001569static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1570{
Mark Bloch0042f9e2018-09-17 13:30:49 +03001571 if (qp->flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1572 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1573 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001574 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1575 to_mpd(qp->ibqp.pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001576}
1577
1578static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1579 struct ib_pd *pd,
1580 struct ib_qp_init_attr *init_attr,
1581 struct ib_udata *udata)
1582{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001583 struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
1584 udata, struct mlx5_ib_ucontext, ibucontext);
Yishai Hadas28d61372016-05-23 15:20:56 +03001585 struct mlx5_ib_create_qp_resp resp = {};
1586 int inlen;
1587 int err;
1588 u32 *in;
1589 void *tirc;
1590 void *hfso;
1591 u32 selected_fields = 0;
Matan Barak2d93fc82018-03-28 09:27:55 +03001592 u32 outer_l4;
Yishai Hadas28d61372016-05-23 15:20:56 +03001593 size_t min_resp_len;
1594 u32 tdn = mucontext->tdn;
1595 struct mlx5_ib_create_qp_rss ucmd = {};
1596 size_t required_cmd_sz;
Mark Bloch175edba2018-09-17 13:30:48 +03001597 u8 lb_flag = 0;
Yishai Hadas28d61372016-05-23 15:20:56 +03001598
1599 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1600 return -EOPNOTSUPP;
1601
1602 if (init_attr->create_flags || init_attr->send_cq)
1603 return -EINVAL;
1604
Eli Cohen2f5ff262017-01-03 23:55:21 +02001605 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001606 if (udata->outlen < min_resp_len)
1607 return -EINVAL;
1608
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001609 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001610 if (udata->inlen < required_cmd_sz) {
1611 mlx5_ib_dbg(dev, "invalid inlen\n");
1612 return -EINVAL;
1613 }
1614
1615 if (udata->inlen > sizeof(ucmd) &&
1616 !ib_is_udata_cleared(udata, sizeof(ucmd),
1617 udata->inlen - sizeof(ucmd))) {
1618 mlx5_ib_dbg(dev, "inlen is not supported\n");
1619 return -EOPNOTSUPP;
1620 }
1621
1622 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1623 mlx5_ib_dbg(dev, "copy failed\n");
1624 return -EFAULT;
1625 }
1626
1627 if (ucmd.comp_mask) {
1628 mlx5_ib_dbg(dev, "invalid comp mask\n");
1629 return -EOPNOTSUPP;
1630 }
1631
Mark Bloch175edba2018-09-17 13:30:48 +03001632 if (ucmd.flags & ~(MLX5_QP_FLAG_TUNNEL_OFFLOADS |
1633 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1634 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)) {
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001635 mlx5_ib_dbg(dev, "invalid flags\n");
1636 return -EOPNOTSUPP;
1637 }
1638
1639 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1640 !tunnel_offload_supported(dev->mdev)) {
1641 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001642 return -EOPNOTSUPP;
1643 }
1644
Maor Gottlieb309fa342017-10-19 08:25:56 +03001645 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1646 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1647 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1648 return -EOPNOTSUPP;
1649 }
1650
Mark Bloch6a4d00b2019-03-28 15:27:37 +02001651 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC || dev->is_rep) {
Mark Bloch175edba2018-09-17 13:30:48 +03001652 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1653 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1654 }
1655
1656 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
1657 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1658 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
1659 }
1660
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001661 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
Yishai Hadas28d61372016-05-23 15:20:56 +03001662 if (err) {
1663 mlx5_ib_dbg(dev, "copy failed\n");
1664 return -EINVAL;
1665 }
1666
1667 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001668 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001669 if (!in)
1670 return -ENOMEM;
1671
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001672 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001673 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1674 MLX5_SET(tirc, tirc, disp_type,
1675 MLX5_TIRC_DISP_TYPE_INDIRECT);
1676 MLX5_SET(tirc, tirc, indirect_table,
1677 init_attr->rwq_ind_tbl->ind_tbl_num);
1678 MLX5_SET(tirc, tirc, transport_domain, tdn);
1679
1680 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001681
1682 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1683 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1684
Mark Bloch175edba2018-09-17 13:30:48 +03001685 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
1686
Maor Gottlieb309fa342017-10-19 08:25:56 +03001687 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1688 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1689 else
1690 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1691
Yishai Hadas28d61372016-05-23 15:20:56 +03001692 switch (ucmd.rx_hash_function) {
1693 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1694 {
1695 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1696 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1697
1698 if (len != ucmd.rx_key_len) {
1699 err = -EINVAL;
1700 goto err;
1701 }
1702
1703 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1704 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1705 memcpy(rss_key, ucmd.rx_hash_key, len);
1706 break;
1707 }
1708 default:
1709 err = -EOPNOTSUPP;
1710 goto err;
1711 }
1712
1713 if (!ucmd.rx_hash_fields_mask) {
1714 /* special case when this TIR serves as steering entry without hashing */
1715 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1716 goto create_tir;
1717 err = -EINVAL;
1718 goto err;
1719 }
1720
1721 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1722 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1723 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1724 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1725 err = -EINVAL;
1726 goto err;
1727 }
1728
1729 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1730 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1731 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1732 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1733 MLX5_L3_PROT_TYPE_IPV4);
1734 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1735 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1736 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1737 MLX5_L3_PROT_TYPE_IPV6);
1738
Matan Barak2d93fc82018-03-28 09:27:55 +03001739 outer_l4 = ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1740 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) << 0 |
1741 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1742 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP)) << 1 |
1743 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI) << 2;
1744
1745 /* Check that only one l4 protocol is set */
1746 if (outer_l4 & (outer_l4 - 1)) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001747 err = -EINVAL;
1748 goto err;
1749 }
1750
1751 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1752 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1753 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1754 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1755 MLX5_L4_PROT_TYPE_TCP);
1756 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1757 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1758 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1759 MLX5_L4_PROT_TYPE_UDP);
1760
1761 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1762 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1763 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1764
1765 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1766 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1767 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1768
1769 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1770 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1771 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1772
1773 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1774 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1775 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1776
Matan Barak2d93fc82018-03-28 09:27:55 +03001777 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI)
1778 selected_fields |= MLX5_HASH_FIELD_SEL_IPSEC_SPI;
1779
Yishai Hadas28d61372016-05-23 15:20:56 +03001780 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1781
1782create_tir:
1783 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1784
Mark Bloch0042f9e2018-09-17 13:30:49 +03001785 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1786 err = mlx5_ib_enable_lb(dev, false, true);
1787
1788 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001789 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1790 to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001791 }
1792
Yishai Hadas28d61372016-05-23 15:20:56 +03001793 if (err)
1794 goto err;
1795
Yishai Hadas7f720522018-09-20 21:45:18 +03001796 if (mucontext->devx_uid) {
1797 resp.comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1798 resp.tirn = qp->rss_qp.tirn;
1799 }
1800
1801 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
1802 if (err)
1803 goto err_copy;
1804
Yishai Hadas28d61372016-05-23 15:20:56 +03001805 kvfree(in);
1806 /* qpn is reserved for that QP */
1807 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001808 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001809 return 0;
1810
Yishai Hadas7f720522018-09-20 21:45:18 +03001811err_copy:
1812 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn, mucontext->devx_uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001813err:
1814 kvfree(in);
1815 return err;
1816}
1817
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001818static void configure_responder_scat_cqe(struct ib_qp_init_attr *init_attr,
1819 void *qpc)
1820{
1821 int rcqe_sz;
1822
1823 if (init_attr->qp_type == MLX5_IB_QPT_DCI)
1824 return;
1825
1826 rcqe_sz = mlx5_ib_get_cqe_size(init_attr->recv_cq);
1827
1828 if (rcqe_sz == 128) {
1829 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
1830 return;
1831 }
1832
1833 if (init_attr->qp_type != MLX5_IB_QPT_DCT)
1834 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
1835}
1836
1837static void configure_requester_scat_cqe(struct mlx5_ib_dev *dev,
1838 struct ib_qp_init_attr *init_attr,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001839 struct mlx5_ib_create_qp *ucmd,
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001840 void *qpc)
1841{
1842 enum ib_qp_type qpt = init_attr->qp_type;
1843 int scqe_sz;
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001844 bool allow_scat_cqe = 0;
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001845
1846 if (qpt == IB_QPT_UC || qpt == IB_QPT_UD)
1847 return;
1848
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03001849 if (ucmd)
1850 allow_scat_cqe = ucmd->flags & MLX5_QP_FLAG_ALLOW_SCATTER_CQE;
1851
1852 if (!allow_scat_cqe && init_attr->sq_sig_type != IB_SIGNAL_ALL_WR)
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03001853 return;
1854
1855 scqe_sz = mlx5_ib_get_cqe_size(init_attr->send_cq);
1856 if (scqe_sz == 128) {
1857 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
1858 return;
1859 }
1860
1861 if (init_attr->qp_type != MLX5_IB_QPT_DCI ||
1862 MLX5_CAP_GEN(dev->mdev, dc_req_scat_data_cqe))
1863 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
1864}
1865
Yonatan Cohena60109d2018-10-10 09:25:16 +03001866static int atomic_size_to_mode(int size_mask)
1867{
1868 /* driver does not support atomic_size > 256B
1869 * and does not know how to translate bigger sizes
1870 */
1871 int supported_size_mask = size_mask & 0x1ff;
1872 int log_max_size;
1873
1874 if (!supported_size_mask)
1875 return -EOPNOTSUPP;
1876
1877 log_max_size = __fls(supported_size_mask);
1878
1879 if (log_max_size > 3)
1880 return log_max_size;
1881
1882 return MLX5_ATOMIC_MODE_8B;
1883}
1884
1885static int get_atomic_mode(struct mlx5_ib_dev *dev,
1886 enum ib_qp_type qp_type)
1887{
1888 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
1889 u8 atomic = MLX5_CAP_GEN(dev->mdev, atomic);
1890 int atomic_mode = -EOPNOTSUPP;
1891 int atomic_size_mask;
1892
1893 if (!atomic)
1894 return -EOPNOTSUPP;
1895
1896 if (qp_type == MLX5_IB_QPT_DCT)
1897 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
1898 else
1899 atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
1900
1901 if ((atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_CMP_SWAP) ||
1902 (atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_FETCH_ADD))
1903 atomic_mode = atomic_size_to_mode(atomic_size_mask);
1904
1905 if (atomic_mode <= 0 &&
1906 (atomic_operations & MLX5_ATOMIC_OPS_CMP_SWAP &&
1907 atomic_operations & MLX5_ATOMIC_OPS_FETCH_ADD))
1908 atomic_mode = MLX5_ATOMIC_MODE_IB_COMP;
1909
1910 return atomic_mode;
1911}
1912
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03001913static inline bool check_flags_mask(uint64_t input, uint64_t supported)
1914{
1915 return (input & ~supported) == 0;
1916}
1917
Eli Cohene126ba92013-07-07 17:25:49 +03001918static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1919 struct ib_qp_init_attr *init_attr,
1920 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1921{
1922 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001923 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001924 struct mlx5_core_dev *mdev = dev->mdev;
Jason Gunthorpe0625b4b2018-08-14 15:33:52 -06001925 struct mlx5_ib_create_qp_resp resp = {};
Shamir Rabinovitch89944452019-02-07 18:44:49 +02001926 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
1927 udata, struct mlx5_ib_ucontext, ibucontext);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001928 struct mlx5_ib_cq *send_cq;
1929 struct mlx5_ib_cq *recv_cq;
1930 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001931 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001932 struct mlx5_ib_create_qp ucmd;
1933 struct mlx5_ib_qp_base *base;
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001934 int mlx5_st;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001935 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001936 u32 *in;
1937 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001938
1939 mutex_init(&qp->mutex);
1940 spin_lock_init(&qp->sq.lock);
1941 spin_lock_init(&qp->rq.lock);
1942
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001943 mlx5_st = to_mlx5_st(init_attr->qp_type);
1944 if (mlx5_st < 0)
1945 return -EINVAL;
1946
Yishai Hadas28d61372016-05-23 15:20:56 +03001947 if (init_attr->rwq_ind_tbl) {
1948 if (!udata)
1949 return -ENOSYS;
1950
1951 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1952 return err;
1953 }
1954
Eli Cohenf360d882014-04-02 00:10:16 +03001955 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001956 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001957 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1958 return -EINVAL;
1959 } else {
1960 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1961 }
1962 }
1963
Leon Romanovsky051f2632015-12-20 12:16:11 +02001964 if (init_attr->create_flags &
1965 (IB_QP_CREATE_CROSS_CHANNEL |
1966 IB_QP_CREATE_MANAGED_SEND |
1967 IB_QP_CREATE_MANAGED_RECV)) {
1968 if (!MLX5_CAP_GEN(mdev, cd)) {
1969 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1970 return -EINVAL;
1971 }
1972 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1973 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1974 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1975 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1976 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1977 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1978 }
Erez Shitritf0313962016-02-21 16:27:17 +02001979
1980 if (init_attr->qp_type == IB_QPT_UD &&
1981 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1982 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1983 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1984 return -EOPNOTSUPP;
1985 }
1986
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001987 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1988 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1989 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1990 return -EOPNOTSUPP;
1991 }
1992 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1993 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1994 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1995 return -EOPNOTSUPP;
1996 }
1997 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1998 }
1999
Eli Cohene126ba92013-07-07 17:25:49 +03002000 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
2001 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
2002
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02002003 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
2004 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
2005 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
2006 (init_attr->qp_type != IB_QPT_RAW_PACKET))
2007 return -EOPNOTSUPP;
2008 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
2009 }
2010
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002011 if (udata) {
Eli Cohene126ba92013-07-07 17:25:49 +03002012 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
2013 mlx5_ib_dbg(dev, "copy failed\n");
2014 return -EFAULT;
2015 }
2016
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002017 if (!check_flags_mask(ucmd.flags,
Mark Bloch8af526e2019-01-15 16:45:32 +02002018 MLX5_QP_FLAG_ALLOW_SCATTER_CQE |
2019 MLX5_QP_FLAG_BFREG_INDEX |
2020 MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE |
2021 MLX5_QP_FLAG_SCATTER_CQE |
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002022 MLX5_QP_FLAG_SIGNATURE |
Mark Bloch8af526e2019-01-15 16:45:32 +02002023 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC |
2024 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
2025 MLX5_QP_FLAG_TUNNEL_OFFLOADS |
2026 MLX5_QP_FLAG_TYPE_DCI |
2027 MLX5_QP_FLAG_TYPE_DCT))
Yonatan Cohen2e43bb32018-10-09 12:05:14 +03002028 return -EINVAL;
2029
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002030 err = get_qp_user_index(ucontext, &ucmd, udata->inlen, &uidx);
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002031 if (err)
2032 return err;
2033
Eli Cohene126ba92013-07-07 17:25:49 +03002034 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002035 if (MLX5_CAP_GEN(dev->mdev, sctr_data_cqe))
2036 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002037 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
2038 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
2039 !tunnel_offload_supported(mdev)) {
2040 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
2041 return -EOPNOTSUPP;
2042 }
Mark Bloch175edba2018-09-17 13:30:48 +03002043 qp->flags_en |= MLX5_QP_FLAG_TUNNEL_OFFLOADS;
2044 }
2045
2046 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC) {
2047 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2048 mlx5_ib_dbg(dev, "Self-LB UC isn't supported\n");
2049 return -EOPNOTSUPP;
2050 }
2051 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
2052 }
2053
2054 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
2055 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2056 mlx5_ib_dbg(dev, "Self-LB UM isn't supported\n");
2057 return -EOPNOTSUPP;
2058 }
2059 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03002060 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002061
Danit Goldberg569c6652018-11-30 13:22:05 +02002062 if (ucmd.flags & MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE) {
2063 if (init_attr->qp_type != IB_QPT_RC ||
2064 !MLX5_CAP_GEN(dev->mdev, qp_packet_based)) {
2065 mlx5_ib_dbg(dev, "packet based credit mode isn't supported\n");
2066 return -EOPNOTSUPP;
2067 }
2068 qp->flags |= MLX5_IB_QP_PACKET_BASED_CREDIT;
2069 }
2070
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002071 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
2072 if (init_attr->qp_type != IB_QPT_UD ||
2073 (MLX5_CAP_GEN(dev->mdev, port_type) !=
2074 MLX5_CAP_PORT_TYPE_IB) ||
2075 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
2076 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
2077 return -EOPNOTSUPP;
2078 }
2079
2080 qp->flags |= MLX5_IB_QP_UNDERLAY;
2081 qp->underlay_qpn = init_attr->source_qpn;
2082 }
Eli Cohene126ba92013-07-07 17:25:49 +03002083 } else {
2084 qp->wq_sig = !!wq_signature;
2085 }
2086
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002087 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2088 qp->flags & MLX5_IB_QP_UNDERLAY) ?
2089 &qp->raw_packet_qp.rq.base :
2090 &qp->trans_qp.base;
2091
Eli Cohene126ba92013-07-07 17:25:49 +03002092 qp->has_rq = qp_has_rq(init_attr);
2093 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002094 qp, udata ? &ucmd : NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002095 if (err) {
2096 mlx5_ib_dbg(dev, "err %d\n", err);
2097 return err;
2098 }
2099
2100 if (pd) {
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002101 if (udata) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002102 __u32 max_wqes =
2103 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03002104 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
2105 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
2106 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
2107 mlx5_ib_dbg(dev, "invalid rq params\n");
2108 return -EINVAL;
2109 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002110 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03002111 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03002112 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03002113 return -EINVAL;
2114 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02002115 if (init_attr->create_flags &
2116 mlx5_ib_create_qp_sqpn_qp1()) {
2117 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
2118 return -EINVAL;
2119 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002120 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
2121 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002122 if (err)
2123 mlx5_ib_dbg(dev, "err %d\n", err);
2124 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002125 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
2126 base);
Eli Cohene126ba92013-07-07 17:25:49 +03002127 if (err)
2128 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03002129 }
2130
2131 if (err)
2132 return err;
2133 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002134 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03002135 if (!in)
2136 return -ENOMEM;
2137
2138 qp->create_type = MLX5_QP_EMPTY;
2139 }
2140
2141 if (is_sqp(init_attr->qp_type))
2142 qp->port = init_attr->port_num;
2143
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002144 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
2145
Noa Osheroviche7b169f2018-02-25 13:39:51 +02002146 MLX5_SET(qpc, qpc, st, mlx5_st);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002147 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03002148
2149 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002150 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002151 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002152 MLX5_SET(qpc, qpc, latency_sensitive, 1);
2153
Eli Cohene126ba92013-07-07 17:25:49 +03002154
2155 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002156 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002157
Eli Cohenf360d882014-04-02 00:10:16 +03002158 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002159 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03002160
Leon Romanovsky051f2632015-12-20 12:16:11 +02002161 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002162 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002163 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002164 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02002165 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002166 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Danit Goldberg569c6652018-11-30 13:22:05 +02002167 if (qp->flags & MLX5_IB_QP_PACKET_BASED_CREDIT)
2168 MLX5_SET(qpc, qpc, req_e2e_credit_mode, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03002169 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002170 configure_responder_scat_cqe(init_attr, qpc);
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002171 configure_requester_scat_cqe(dev, init_attr,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02002172 udata ? &ucmd : NULL,
Yonatan Cohen6f4bc0e2018-10-09 12:05:15 +03002173 qpc);
Eli Cohene126ba92013-07-07 17:25:49 +03002174 }
2175
2176 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002177 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
2178 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03002179 }
2180
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002181 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03002182
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002183 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002184 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002185 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002186 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03002187 if (init_attr->srq &&
2188 init_attr->srq->srq_type == IB_SRQT_TM)
2189 MLX5_SET(qpc, qpc, offload_type,
2190 MLX5_QPC_OFFLOAD_TYPE_RNDV);
2191 }
Eli Cohene126ba92013-07-07 17:25:49 +03002192
2193 /* Set default resources */
2194 switch (init_attr->qp_type) {
2195 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002196 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2197 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
2198 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
2199 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002200 break;
2201 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002202 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
2203 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2204 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002205 break;
2206 default:
2207 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002208 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
2209 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002210 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002211 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
2212 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002213 }
2214 }
2215
2216 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002217 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002218
2219 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002220 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03002221
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002222 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03002223
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002224 /* 0xffffff means we ask to work with cqe version 0 */
2225 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002226 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03002227
Erez Shitritf0313962016-02-21 16:27:17 +02002228 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
2229 if (init_attr->qp_type == IB_QPT_UD &&
2230 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02002231 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
2232 qp->flags |= MLX5_IB_QP_LSO;
2233 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02002234
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002235 if (init_attr->create_flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
2236 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
2237 mlx5_ib_dbg(dev, "scatter end padding is not supported\n");
2238 err = -EOPNOTSUPP;
2239 goto err;
2240 } else if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
2241 MLX5_SET(qpc, qpc, end_padding_mode,
2242 MLX5_WQ_END_PAD_MODE_ALIGN);
2243 } else {
2244 qp->flags |= MLX5_IB_QP_PCI_WRITE_END_PADDING;
2245 }
2246 }
2247
Boris Pismenny2c292db2018-03-08 15:51:40 +02002248 if (inlen < 0) {
2249 err = -EINVAL;
2250 goto err;
2251 }
2252
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002253 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2254 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002255 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
2256 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
Yishai Hadas7f720522018-09-20 21:45:18 +03002257 err = create_raw_packet_qp(dev, qp, in, inlen, pd, udata,
2258 &resp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002259 } else {
2260 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
2261 }
2262
Eli Cohene126ba92013-07-07 17:25:49 +03002263 if (err) {
2264 mlx5_ib_dbg(dev, "create qp failed\n");
2265 goto err_create;
2266 }
2267
Al Viro479163f2014-11-20 08:13:57 +00002268 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002269
majd@mellanox.com19098df2016-01-14 19:13:03 +02002270 base->container_mibqp = qp;
2271 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03002272
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002273 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
2274 &send_cq, &recv_cq);
2275 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2276 mlx5_ib_lock_cqs(send_cq, recv_cq);
2277 /* Maintain device to QPs access, needed for further handling via reset
2278 * flow
2279 */
2280 list_add_tail(&qp->qps_list, &dev->qp_list);
2281 /* Maintain CQ to QPs access, needed for further handling via reset flow
2282 */
2283 if (send_cq)
2284 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
2285 if (recv_cq)
2286 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
2287 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2288 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
2289
Eli Cohene126ba92013-07-07 17:25:49 +03002290 return 0;
2291
2292err_create:
2293 if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002294 destroy_qp_user(dev, pd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002295 else if (qp->create_type == MLX5_QP_KERNEL)
2296 destroy_qp_kernel(dev, qp);
2297
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002298err:
Al Viro479163f2014-11-20 08:13:57 +00002299 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002300 return err;
2301}
2302
2303static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2304 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
2305{
2306 if (send_cq) {
2307 if (recv_cq) {
2308 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002309 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002310 spin_lock_nested(&recv_cq->lock,
2311 SINGLE_DEPTH_NESTING);
2312 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002313 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002314 __acquire(&recv_cq->lock);
2315 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002316 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002317 spin_lock_nested(&send_cq->lock,
2318 SINGLE_DEPTH_NESTING);
2319 }
2320 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002321 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002322 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002323 }
2324 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002325 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002326 __acquire(&send_cq->lock);
2327 } else {
2328 __acquire(&send_cq->lock);
2329 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002330 }
2331}
2332
2333static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2334 __releases(&send_cq->lock) __releases(&recv_cq->lock)
2335{
2336 if (send_cq) {
2337 if (recv_cq) {
2338 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
2339 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002340 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002341 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
2342 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002343 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002344 } else {
2345 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002346 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002347 }
2348 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002349 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002350 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002351 }
2352 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002353 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002354 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002355 } else {
2356 __release(&recv_cq->lock);
2357 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002358 }
2359}
2360
2361static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
2362{
2363 return to_mpd(qp->ibqp.pd);
2364}
2365
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002366static void get_cqs(enum ib_qp_type qp_type,
2367 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03002368 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
2369{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002370 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002371 case IB_QPT_XRC_TGT:
2372 *send_cq = NULL;
2373 *recv_cq = NULL;
2374 break;
2375 case MLX5_IB_QPT_REG_UMR:
2376 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002377 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002378 *recv_cq = NULL;
2379 break;
2380
2381 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002382 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002383 case IB_QPT_RC:
2384 case IB_QPT_UC:
2385 case IB_QPT_UD:
2386 case IB_QPT_RAW_IPV6:
2387 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002388 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002389 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2390 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002391 break;
2392
Eli Cohene126ba92013-07-07 17:25:49 +03002393 case IB_QPT_MAX:
2394 default:
2395 *send_cq = NULL;
2396 *recv_cq = NULL;
2397 break;
2398 }
2399}
2400
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002401static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002402 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2403 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002404
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002405static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2406 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002407{
2408 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002409 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002410 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002411 int err;
2412
Yishai Hadas28d61372016-05-23 15:20:56 +03002413 if (qp->ibqp.rwq_ind_tbl) {
2414 destroy_rss_raw_qp_tir(dev, qp);
2415 return;
2416 }
2417
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002418 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2419 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002420 &qp->raw_packet_qp.rq.base :
2421 &qp->trans_qp.base;
2422
Haggai Eran6aec21f2014-12-11 17:04:23 +02002423 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002424 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2425 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002426 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002427 MLX5_CMD_OP_2RST_QP, 0,
2428 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002429 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002430 struct mlx5_modify_raw_qp_param raw_qp_param = {
2431 .operation = MLX5_CMD_OP_2RST_QP
2432 };
2433
Aviv Heller13eab212016-09-18 20:48:04 +03002434 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002435 }
2436 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002437 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002438 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002439 }
Eli Cohene126ba92013-07-07 17:25:49 +03002440
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002441 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2442 &send_cq, &recv_cq);
2443
2444 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2445 mlx5_ib_lock_cqs(send_cq, recv_cq);
2446 /* del from lists under both locks above to protect reset flow paths */
2447 list_del(&qp->qps_list);
2448 if (send_cq)
2449 list_del(&qp->cq_send_list);
2450
2451 if (recv_cq)
2452 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002453
2454 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002455 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002456 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2457 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002458 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2459 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002460 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002461 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2462 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002463
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002464 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2465 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002466 destroy_raw_packet_qp(dev, qp);
2467 } else {
2468 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
2469 if (err)
2470 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2471 base->mqp.qpn);
2472 }
Eli Cohene126ba92013-07-07 17:25:49 +03002473
Eli Cohene126ba92013-07-07 17:25:49 +03002474 if (qp->create_type == MLX5_QP_KERNEL)
2475 destroy_qp_kernel(dev, qp);
2476 else if (qp->create_type == MLX5_QP_USER)
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002477 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002478}
2479
2480static const char *ib_qp_type_str(enum ib_qp_type type)
2481{
2482 switch (type) {
2483 case IB_QPT_SMI:
2484 return "IB_QPT_SMI";
2485 case IB_QPT_GSI:
2486 return "IB_QPT_GSI";
2487 case IB_QPT_RC:
2488 return "IB_QPT_RC";
2489 case IB_QPT_UC:
2490 return "IB_QPT_UC";
2491 case IB_QPT_UD:
2492 return "IB_QPT_UD";
2493 case IB_QPT_RAW_IPV6:
2494 return "IB_QPT_RAW_IPV6";
2495 case IB_QPT_RAW_ETHERTYPE:
2496 return "IB_QPT_RAW_ETHERTYPE";
2497 case IB_QPT_XRC_INI:
2498 return "IB_QPT_XRC_INI";
2499 case IB_QPT_XRC_TGT:
2500 return "IB_QPT_XRC_TGT";
2501 case IB_QPT_RAW_PACKET:
2502 return "IB_QPT_RAW_PACKET";
2503 case MLX5_IB_QPT_REG_UMR:
2504 return "MLX5_IB_QPT_REG_UMR";
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002505 case IB_QPT_DRIVER:
2506 return "IB_QPT_DRIVER";
Eli Cohene126ba92013-07-07 17:25:49 +03002507 case IB_QPT_MAX:
2508 default:
2509 return "Invalid QP type";
2510 }
2511}
2512
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002513static struct ib_qp *mlx5_ib_create_dct(struct ib_pd *pd,
2514 struct ib_qp_init_attr *attr,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002515 struct mlx5_ib_create_qp *ucmd,
2516 struct ib_udata *udata)
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002517{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002518 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2519 udata, struct mlx5_ib_ucontext, ibucontext);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002520 struct mlx5_ib_qp *qp;
2521 int err = 0;
2522 u32 uidx = MLX5_IB_DEFAULT_UIDX;
2523 void *dctc;
2524
2525 if (!attr->srq || !attr->recv_cq)
2526 return ERR_PTR(-EINVAL);
2527
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002528 err = get_qp_user_index(ucontext, ucmd, sizeof(*ucmd), &uidx);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002529 if (err)
2530 return ERR_PTR(err);
2531
2532 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2533 if (!qp)
2534 return ERR_PTR(-ENOMEM);
2535
2536 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
2537 if (!qp->dct.in) {
2538 err = -ENOMEM;
2539 goto err_free;
2540 }
2541
Yishai Hadasa01a5862018-09-20 21:39:24 +03002542 MLX5_SET(create_dct_in, qp->dct.in, uid, to_mpd(pd)->uid);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002543 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shoua776a3902018-01-02 16:19:33 +02002544 qp->qp_sub_type = MLX5_IB_QPT_DCT;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002545 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2546 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2547 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2548 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2549 MLX5_SET(dctc, dctc, user_index, uidx);
2550
Yonatan Cohen5d6ff1b2018-10-09 12:05:13 +03002551 if (ucmd->flags & MLX5_QP_FLAG_SCATTER_CQE)
2552 configure_responder_scat_cqe(attr, dctc);
2553
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002554 qp->state = IB_QPS_RESET;
2555
2556 return &qp->ibqp;
2557err_free:
2558 kfree(qp);
2559 return ERR_PTR(err);
2560}
2561
2562static int set_mlx_qp_type(struct mlx5_ib_dev *dev,
2563 struct ib_qp_init_attr *init_attr,
2564 struct mlx5_ib_create_qp *ucmd,
2565 struct ib_udata *udata)
2566{
2567 enum { MLX_QP_FLAGS = MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI };
2568 int err;
2569
2570 if (!udata)
2571 return -EINVAL;
2572
2573 if (udata->inlen < sizeof(*ucmd)) {
2574 mlx5_ib_dbg(dev, "create_qp user command is smaller than expected\n");
2575 return -EINVAL;
2576 }
2577 err = ib_copy_from_udata(ucmd, udata, sizeof(*ucmd));
2578 if (err)
2579 return err;
2580
2581 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCI) {
2582 init_attr->qp_type = MLX5_IB_QPT_DCI;
2583 } else {
2584 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCT) {
2585 init_attr->qp_type = MLX5_IB_QPT_DCT;
2586 } else {
2587 mlx5_ib_dbg(dev, "Invalid QP flags\n");
2588 return -EINVAL;
2589 }
2590 }
2591
2592 if (!MLX5_CAP_GEN(dev->mdev, dct)) {
2593 mlx5_ib_dbg(dev, "DC transport is not supported\n");
2594 return -EOPNOTSUPP;
2595 }
2596
2597 return 0;
2598}
2599
Eli Cohene126ba92013-07-07 17:25:49 +03002600struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002601 struct ib_qp_init_attr *verbs_init_attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002602 struct ib_udata *udata)
2603{
2604 struct mlx5_ib_dev *dev;
2605 struct mlx5_ib_qp *qp;
2606 u16 xrcdn = 0;
2607 int err;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002608 struct ib_qp_init_attr mlx_init_attr;
2609 struct ib_qp_init_attr *init_attr = verbs_init_attr;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002610 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2611 udata, struct mlx5_ib_ucontext, ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03002612
2613 if (pd) {
2614 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002615
2616 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002617 if (!ucontext) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002618 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2619 return ERR_PTR(-EINVAL);
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002620 } else if (!ucontext->cqe_version) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002621 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2622 return ERR_PTR(-EINVAL);
2623 }
2624 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002625 } else {
2626 /* being cautious here */
2627 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2628 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2629 pr_warn("%s: no PD for transport %s\n", __func__,
2630 ib_qp_type_str(init_attr->qp_type));
2631 return ERR_PTR(-EINVAL);
2632 }
2633 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002634 }
2635
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002636 if (init_attr->qp_type == IB_QPT_DRIVER) {
2637 struct mlx5_ib_create_qp ucmd;
2638
2639 init_attr = &mlx_init_attr;
2640 memcpy(init_attr, verbs_init_attr, sizeof(*verbs_init_attr));
2641 err = set_mlx_qp_type(dev, init_attr, &ucmd, udata);
2642 if (err)
2643 return ERR_PTR(err);
Moni Shouac32a4f22018-01-02 16:19:32 +02002644
2645 if (init_attr->qp_type == MLX5_IB_QPT_DCI) {
2646 if (init_attr->cap.max_recv_wr ||
2647 init_attr->cap.max_recv_sge) {
2648 mlx5_ib_dbg(dev, "DCI QP requires zero size receive queue\n");
2649 return ERR_PTR(-EINVAL);
2650 }
Moni Shoua776a3902018-01-02 16:19:33 +02002651 } else {
Shamir Rabinovitch89944452019-02-07 18:44:49 +02002652 return mlx5_ib_create_dct(pd, init_attr, &ucmd, udata);
Moni Shouac32a4f22018-01-02 16:19:32 +02002653 }
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002654 }
2655
Eli Cohene126ba92013-07-07 17:25:49 +03002656 switch (init_attr->qp_type) {
2657 case IB_QPT_XRC_TGT:
2658 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002659 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002660 mlx5_ib_dbg(dev, "XRC not supported\n");
2661 return ERR_PTR(-ENOSYS);
2662 }
2663 init_attr->recv_cq = NULL;
2664 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2665 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2666 init_attr->send_cq = NULL;
2667 }
2668
2669 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002670 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002671 case IB_QPT_RC:
2672 case IB_QPT_UC:
2673 case IB_QPT_UD:
2674 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002675 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002676 case MLX5_IB_QPT_REG_UMR:
Moni Shouac32a4f22018-01-02 16:19:32 +02002677 case MLX5_IB_QPT_DCI:
Eli Cohene126ba92013-07-07 17:25:49 +03002678 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2679 if (!qp)
2680 return ERR_PTR(-ENOMEM);
2681
2682 err = create_qp_common(dev, pd, init_attr, udata, qp);
2683 if (err) {
2684 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2685 kfree(qp);
2686 return ERR_PTR(err);
2687 }
2688
2689 if (is_qp0(init_attr->qp_type))
2690 qp->ibqp.qp_num = 0;
2691 else if (is_qp1(init_attr->qp_type))
2692 qp->ibqp.qp_num = 1;
2693 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002694 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002695
2696 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 +02002697 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002698 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2699 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002700
majd@mellanox.com19098df2016-01-14 19:13:03 +02002701 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002702
2703 break;
2704
Haggai Erand16e91d2016-02-29 15:45:05 +02002705 case IB_QPT_GSI:
2706 return mlx5_ib_gsi_create_qp(pd, init_attr);
2707
Eli Cohene126ba92013-07-07 17:25:49 +03002708 case IB_QPT_RAW_IPV6:
2709 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002710 case IB_QPT_MAX:
2711 default:
2712 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2713 init_attr->qp_type);
2714 /* Don't support raw QPs */
2715 return ERR_PTR(-EINVAL);
2716 }
2717
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002718 if (verbs_init_attr->qp_type == IB_QPT_DRIVER)
2719 qp->qp_sub_type = init_attr->qp_type;
2720
Eli Cohene126ba92013-07-07 17:25:49 +03002721 return &qp->ibqp;
2722}
2723
Moni Shoua776a3902018-01-02 16:19:33 +02002724static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2725{
2726 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2727
2728 if (mqp->state == IB_QPS_RTR) {
2729 int err;
2730
2731 err = mlx5_core_destroy_dct(dev->mdev, &mqp->dct.mdct);
2732 if (err) {
2733 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2734 return err;
2735 }
2736 }
2737
2738 kfree(mqp->dct.in);
2739 kfree(mqp);
2740 return 0;
2741}
2742
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002743int mlx5_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002744{
2745 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2746 struct mlx5_ib_qp *mqp = to_mqp(qp);
2747
Haggai Erand16e91d2016-02-29 15:45:05 +02002748 if (unlikely(qp->qp_type == IB_QPT_GSI))
2749 return mlx5_ib_gsi_destroy_qp(qp);
2750
Moni Shoua776a3902018-01-02 16:19:33 +02002751 if (mqp->qp_sub_type == MLX5_IB_QPT_DCT)
2752 return mlx5_ib_destroy_dct(mqp);
2753
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002754 destroy_qp_common(dev, mqp, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03002755
2756 kfree(mqp);
2757
2758 return 0;
2759}
2760
Yonatan Cohena60109d2018-10-10 09:25:16 +03002761static int to_mlx5_access_flags(struct mlx5_ib_qp *qp,
2762 const struct ib_qp_attr *attr,
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002763 int attr_mask, __be32 *hw_access_flags_be)
Eli Cohene126ba92013-07-07 17:25:49 +03002764{
Eli Cohene126ba92013-07-07 17:25:49 +03002765 u8 dest_rd_atomic;
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002766 u32 access_flags, hw_access_flags = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002767
Yonatan Cohena60109d2018-10-10 09:25:16 +03002768 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
2769
Eli Cohene126ba92013-07-07 17:25:49 +03002770 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2771 dest_rd_atomic = attr->max_dest_rd_atomic;
2772 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002773 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002774
2775 if (attr_mask & IB_QP_ACCESS_FLAGS)
2776 access_flags = attr->qp_access_flags;
2777 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002778 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002779
2780 if (!dest_rd_atomic)
2781 access_flags &= IB_ACCESS_REMOTE_WRITE;
2782
2783 if (access_flags & IB_ACCESS_REMOTE_READ)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002784 hw_access_flags |= MLX5_QP_BIT_RRE;
Yonatan Cohen13f8d9c2018-11-21 13:48:39 +02002785 if (access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03002786 int atomic_mode;
Eli Cohene126ba92013-07-07 17:25:49 +03002787
Yonatan Cohena60109d2018-10-10 09:25:16 +03002788 atomic_mode = get_atomic_mode(dev, qp->ibqp.qp_type);
2789 if (atomic_mode < 0)
2790 return -EOPNOTSUPP;
2791
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002792 hw_access_flags |= MLX5_QP_BIT_RAE;
2793 hw_access_flags |= atomic_mode << MLX5_ATOMIC_MODE_OFFSET;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002794 }
2795
2796 if (access_flags & IB_ACCESS_REMOTE_WRITE)
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002797 hw_access_flags |= MLX5_QP_BIT_RWE;
Yonatan Cohena60109d2018-10-10 09:25:16 +03002798
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08002799 *hw_access_flags_be = cpu_to_be32(hw_access_flags);
Yonatan Cohena60109d2018-10-10 09:25:16 +03002800
2801 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002802}
2803
2804enum {
2805 MLX5_PATH_FLAG_FL = 1 << 0,
2806 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2807 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2808};
2809
2810static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2811{
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002812 if (rate == IB_RATE_PORT_CURRENT)
Eli Cohene126ba92013-07-07 17:25:49 +03002813 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002814
Michael Guralnika5a5d192018-12-09 11:49:50 +02002815 if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_600_GBPS)
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002816 return -EINVAL;
2817
2818 while (rate != IB_RATE_PORT_CURRENT &&
2819 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
2820 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
2821 --rate;
2822
2823 return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
Eli Cohene126ba92013-07-07 17:25:49 +03002824}
2825
majd@mellanox.com75850d02016-01-14 19:13:06 +02002826static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002827 struct mlx5_ib_sq *sq, u8 sl,
2828 struct ib_pd *pd)
majd@mellanox.com75850d02016-01-14 19:13:06 +02002829{
2830 void *in;
2831 void *tisc;
2832 int inlen;
2833 int err;
2834
2835 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002836 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002837 if (!in)
2838 return -ENOMEM;
2839
2840 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002841 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002842
2843 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2844 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2845
2846 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2847
2848 kvfree(in);
2849
2850 return err;
2851}
2852
Aviv Heller13eab212016-09-18 20:48:04 +03002853static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002854 struct mlx5_ib_sq *sq, u8 tx_affinity,
2855 struct ib_pd *pd)
Aviv Heller13eab212016-09-18 20:48:04 +03002856{
2857 void *in;
2858 void *tisc;
2859 int inlen;
2860 int err;
2861
2862 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002863 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002864 if (!in)
2865 return -ENOMEM;
2866
2867 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002868 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
Aviv Heller13eab212016-09-18 20:48:04 +03002869
2870 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2871 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2872
2873 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2874
2875 kvfree(in);
2876
2877 return err;
2878}
2879
majd@mellanox.com75850d02016-01-14 19:13:06 +02002880static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002881 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002882 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002883 u32 path_flags, const struct ib_qp_attr *attr,
2884 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002885{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002886 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002887 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002888 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002889 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2890 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002891
Eli Cohene126ba92013-07-07 17:25:49 +03002892 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002893 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2894 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002895
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002896 if (ah_flags & IB_AH_GRH) {
2897 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002898 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002899 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002900 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002901 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002902 return -EINVAL;
2903 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002904 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002905
2906 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002907 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002908 return -EINVAL;
Parav Pandit47ec3862018-06-13 10:22:06 +03002909
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002910 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02002911 if (qp->ibqp.qp_type == IB_QPT_RC ||
2912 qp->ibqp.qp_type == IB_QPT_UC ||
2913 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
2914 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
Parav Pandit47ec3862018-06-13 10:22:06 +03002915 path->udp_sport =
2916 mlx5_get_roce_udp_sport(dev, ah->grh.sgid_attr);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002917 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Parav Pandit47ec3862018-06-13 10:22:06 +03002918 gid_type = ah->grh.sgid_attr->gid_type;
Majd Dibbinyed884512017-01-18 14:10:35 +02002919 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002920 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002921 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002922 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2923 path->fl_free_ar |=
2924 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002925 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2926 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2927 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002928 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002929 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002930 }
2931
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002932 if (ah_flags & IB_AH_GRH) {
2933 path->mgid_index = grh->sgid_index;
2934 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002935 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002936 cpu_to_be32((grh->traffic_class << 20) |
2937 (grh->flow_label));
2938 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002939 }
2940
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002941 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002942 if (err < 0)
2943 return err;
2944 path->static_rate = err;
2945 path->port = port;
2946
Eli Cohene126ba92013-07-07 17:25:49 +03002947 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002948 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002949
majd@mellanox.com75850d02016-01-14 19:13:06 +02002950 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2951 return modify_raw_packet_eth_prio(dev->mdev,
2952 &qp->raw_packet_qp.sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002953 sl & 0xf, qp->ibqp.pd);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002954
Eli Cohene126ba92013-07-07 17:25:49 +03002955 return 0;
2956}
2957
2958static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2959 [MLX5_QP_STATE_INIT] = {
2960 [MLX5_QP_STATE_INIT] = {
2961 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2962 MLX5_QP_OPTPAR_RAE |
2963 MLX5_QP_OPTPAR_RWE |
2964 MLX5_QP_OPTPAR_PKEY_INDEX |
2965 MLX5_QP_OPTPAR_PRI_PORT,
2966 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2967 MLX5_QP_OPTPAR_PKEY_INDEX |
2968 MLX5_QP_OPTPAR_PRI_PORT,
2969 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2970 MLX5_QP_OPTPAR_Q_KEY |
2971 MLX5_QP_OPTPAR_PRI_PORT,
2972 },
2973 [MLX5_QP_STATE_RTR] = {
2974 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2975 MLX5_QP_OPTPAR_RRE |
2976 MLX5_QP_OPTPAR_RAE |
2977 MLX5_QP_OPTPAR_RWE |
2978 MLX5_QP_OPTPAR_PKEY_INDEX,
2979 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2980 MLX5_QP_OPTPAR_RWE |
2981 MLX5_QP_OPTPAR_PKEY_INDEX,
2982 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2983 MLX5_QP_OPTPAR_Q_KEY,
2984 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2985 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002986 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2987 MLX5_QP_OPTPAR_RRE |
2988 MLX5_QP_OPTPAR_RAE |
2989 MLX5_QP_OPTPAR_RWE |
2990 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002991 },
2992 },
2993 [MLX5_QP_STATE_RTR] = {
2994 [MLX5_QP_STATE_RTS] = {
2995 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2996 MLX5_QP_OPTPAR_RRE |
2997 MLX5_QP_OPTPAR_RAE |
2998 MLX5_QP_OPTPAR_RWE |
2999 MLX5_QP_OPTPAR_PM_STATE |
3000 MLX5_QP_OPTPAR_RNR_TIMEOUT,
3001 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
3002 MLX5_QP_OPTPAR_RWE |
3003 MLX5_QP_OPTPAR_PM_STATE,
3004 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
3005 },
3006 },
3007 [MLX5_QP_STATE_RTS] = {
3008 [MLX5_QP_STATE_RTS] = {
3009 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
3010 MLX5_QP_OPTPAR_RAE |
3011 MLX5_QP_OPTPAR_RWE |
3012 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03003013 MLX5_QP_OPTPAR_PM_STATE |
3014 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003015 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03003016 MLX5_QP_OPTPAR_PM_STATE |
3017 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03003018 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
3019 MLX5_QP_OPTPAR_SRQN |
3020 MLX5_QP_OPTPAR_CQN_RCV,
3021 },
3022 },
3023 [MLX5_QP_STATE_SQER] = {
3024 [MLX5_QP_STATE_RTS] = {
3025 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
3026 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03003027 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03003028 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
3029 MLX5_QP_OPTPAR_RWE |
3030 MLX5_QP_OPTPAR_RAE |
3031 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03003032 },
3033 },
3034};
3035
3036static int ib_nr_to_mlx5_nr(int ib_mask)
3037{
3038 switch (ib_mask) {
3039 case IB_QP_STATE:
3040 return 0;
3041 case IB_QP_CUR_STATE:
3042 return 0;
3043 case IB_QP_EN_SQD_ASYNC_NOTIFY:
3044 return 0;
3045 case IB_QP_ACCESS_FLAGS:
3046 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
3047 MLX5_QP_OPTPAR_RAE;
3048 case IB_QP_PKEY_INDEX:
3049 return MLX5_QP_OPTPAR_PKEY_INDEX;
3050 case IB_QP_PORT:
3051 return MLX5_QP_OPTPAR_PRI_PORT;
3052 case IB_QP_QKEY:
3053 return MLX5_QP_OPTPAR_Q_KEY;
3054 case IB_QP_AV:
3055 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
3056 MLX5_QP_OPTPAR_PRI_PORT;
3057 case IB_QP_PATH_MTU:
3058 return 0;
3059 case IB_QP_TIMEOUT:
3060 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
3061 case IB_QP_RETRY_CNT:
3062 return MLX5_QP_OPTPAR_RETRY_COUNT;
3063 case IB_QP_RNR_RETRY:
3064 return MLX5_QP_OPTPAR_RNR_RETRY;
3065 case IB_QP_RQ_PSN:
3066 return 0;
3067 case IB_QP_MAX_QP_RD_ATOMIC:
3068 return MLX5_QP_OPTPAR_SRA_MAX;
3069 case IB_QP_ALT_PATH:
3070 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
3071 case IB_QP_MIN_RNR_TIMER:
3072 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
3073 case IB_QP_SQ_PSN:
3074 return 0;
3075 case IB_QP_MAX_DEST_RD_ATOMIC:
3076 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
3077 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
3078 case IB_QP_PATH_MIG_STATE:
3079 return MLX5_QP_OPTPAR_PM_STATE;
3080 case IB_QP_CAP:
3081 return 0;
3082 case IB_QP_DEST_QPN:
3083 return 0;
3084 }
3085 return 0;
3086}
3087
3088static int ib_mask_to_mlx5_opt(int ib_mask)
3089{
3090 int result = 0;
3091 int i;
3092
3093 for (i = 0; i < 8 * sizeof(int); i++) {
3094 if ((1 << i) & ib_mask)
3095 result |= ib_nr_to_mlx5_nr(1 << i);
3096 }
3097
3098 return result;
3099}
3100
Yishai Hadas34d57582018-09-20 21:39:21 +03003101static int modify_raw_packet_qp_rq(
3102 struct mlx5_ib_dev *dev, struct mlx5_ib_rq *rq, int new_state,
3103 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003104{
3105 void *in;
3106 void *rqc;
3107 int inlen;
3108 int err;
3109
3110 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003111 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003112 if (!in)
3113 return -ENOMEM;
3114
3115 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
Yishai Hadas34d57582018-09-20 21:39:21 +03003116 MLX5_SET(modify_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003117
3118 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
3119 MLX5_SET(rqc, rqc, state, new_state);
3120
Alex Veskereb49ab02016-08-28 12:25:53 +03003121 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
3122 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
3123 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02003124 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03003125 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
3126 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06003127 dev_info_once(
3128 &dev->ib_dev.dev,
3129 "RAW PACKET QP counters are not supported on current FW\n");
Alex Veskereb49ab02016-08-28 12:25:53 +03003130 }
3131
3132 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003133 if (err)
3134 goto out;
3135
3136 rq->state = new_state;
3137
3138out:
3139 kvfree(in);
3140 return err;
3141}
3142
Yishai Hadasc14003f2018-09-20 21:39:22 +03003143static int modify_raw_packet_qp_sq(
3144 struct mlx5_core_dev *dev, struct mlx5_ib_sq *sq, int new_state,
3145 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003146{
Bodong Wang7d29f342016-12-01 13:43:16 +02003147 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
Bodong Wang61147f32018-03-19 15:10:30 +02003148 struct mlx5_rate_limit old_rl = ibqp->rl;
3149 struct mlx5_rate_limit new_rl = old_rl;
3150 bool new_rate_added = false;
Bodong Wang7d29f342016-12-01 13:43:16 +02003151 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003152 void *in;
3153 void *sqc;
3154 int inlen;
3155 int err;
3156
3157 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003158 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003159 if (!in)
3160 return -ENOMEM;
3161
Yishai Hadasc14003f2018-09-20 21:39:22 +03003162 MLX5_SET(modify_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003163 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
3164
3165 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
3166 MLX5_SET(sqc, sqc, state, new_state);
3167
Bodong Wang7d29f342016-12-01 13:43:16 +02003168 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
3169 if (new_state != MLX5_SQC_STATE_RDY)
3170 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
3171 __func__);
3172 else
Bodong Wang61147f32018-03-19 15:10:30 +02003173 new_rl = raw_qp_param->rl;
Bodong Wang7d29f342016-12-01 13:43:16 +02003174 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003175
Bodong Wang61147f32018-03-19 15:10:30 +02003176 if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
3177 if (new_rl.rate) {
3178 err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003179 if (err) {
Bodong Wang61147f32018-03-19 15:10:30 +02003180 pr_err("Failed configuring rate limit(err %d): \
3181 rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
3182 err, new_rl.rate, new_rl.max_burst_sz,
3183 new_rl.typical_pkt_sz);
3184
Bodong Wang7d29f342016-12-01 13:43:16 +02003185 goto out;
3186 }
Bodong Wang61147f32018-03-19 15:10:30 +02003187 new_rate_added = true;
Bodong Wang7d29f342016-12-01 13:43:16 +02003188 }
3189
3190 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
Bodong Wang61147f32018-03-19 15:10:30 +02003191 /* index 0 means no limit */
Bodong Wang7d29f342016-12-01 13:43:16 +02003192 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
3193 }
3194
3195 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
3196 if (err) {
3197 /* Remove new rate from table if failed */
Bodong Wang61147f32018-03-19 15:10:30 +02003198 if (new_rate_added)
3199 mlx5_rl_remove_rate(dev, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003200 goto out;
3201 }
3202
3203 /* Only remove the old rate after new rate was set */
Bodong Wang61147f32018-03-19 15:10:30 +02003204 if ((old_rl.rate &&
3205 !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
Bodong Wang7d29f342016-12-01 13:43:16 +02003206 (new_state != MLX5_SQC_STATE_RDY))
Bodong Wang61147f32018-03-19 15:10:30 +02003207 mlx5_rl_remove_rate(dev, &old_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02003208
Bodong Wang61147f32018-03-19 15:10:30 +02003209 ibqp->rl = new_rl;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003210 sq->state = new_state;
3211
3212out:
3213 kvfree(in);
3214 return err;
3215}
3216
3217static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03003218 const struct mlx5_modify_raw_qp_param *raw_qp_param,
3219 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003220{
3221 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
3222 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
3223 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02003224 int modify_rq = !!qp->rq.wqe_cnt;
3225 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003226 int rq_state;
3227 int sq_state;
3228 int err;
3229
Alex Vesker0680efa2016-08-28 12:25:52 +03003230 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003231 case MLX5_CMD_OP_RST2INIT_QP:
3232 rq_state = MLX5_RQC_STATE_RDY;
3233 sq_state = MLX5_SQC_STATE_RDY;
3234 break;
3235 case MLX5_CMD_OP_2ERR_QP:
3236 rq_state = MLX5_RQC_STATE_ERR;
3237 sq_state = MLX5_SQC_STATE_ERR;
3238 break;
3239 case MLX5_CMD_OP_2RST_QP:
3240 rq_state = MLX5_RQC_STATE_RST;
3241 sq_state = MLX5_SQC_STATE_RST;
3242 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003243 case MLX5_CMD_OP_RTR2RTS_QP:
3244 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02003245 if (raw_qp_param->set_mask ==
3246 MLX5_RAW_QP_RATE_LIMIT) {
3247 modify_rq = 0;
3248 sq_state = sq->state;
3249 } else {
3250 return raw_qp_param->set_mask ? -EINVAL : 0;
3251 }
3252 break;
3253 case MLX5_CMD_OP_INIT2INIT_QP:
3254 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03003255 if (raw_qp_param->set_mask)
3256 return -EINVAL;
3257 else
3258 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003259 default:
3260 WARN_ON(1);
3261 return -EINVAL;
3262 }
3263
Bodong Wang7d29f342016-12-01 13:43:16 +02003264 if (modify_rq) {
Yishai Hadas34d57582018-09-20 21:39:21 +03003265 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param,
3266 qp->ibqp.pd);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003267 if (err)
3268 return err;
3269 }
3270
Bodong Wang7d29f342016-12-01 13:43:16 +02003271 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03003272 if (tx_affinity) {
3273 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003274 tx_affinity,
3275 qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003276 if (err)
3277 return err;
3278 }
3279
Yishai Hadasc14003f2018-09-20 21:39:22 +03003280 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state,
3281 raw_qp_param, qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003282 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003283
3284 return 0;
3285}
3286
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003287static unsigned int get_tx_affinity(struct mlx5_ib_dev *dev,
3288 struct mlx5_ib_pd *pd,
3289 struct mlx5_ib_qp_base *qp_base,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003290 u8 port_num, struct ib_udata *udata)
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003291{
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003292 struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
3293 udata, struct mlx5_ib_ucontext, ibucontext);
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003294 unsigned int tx_port_affinity;
3295
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003296 if (ucontext) {
3297 tx_port_affinity = (unsigned int)atomic_add_return(
3298 1, &ucontext->tx_port_affinity) %
3299 MLX5_MAX_PORTS +
3300 1;
3301 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x ucontext %p\n",
3302 tx_port_affinity, qp_base->mqp.qpn, ucontext);
3303 } else {
3304 tx_port_affinity =
3305 (unsigned int)atomic_add_return(
Mark Bloch95579e72019-03-28 15:27:33 +02003306 1, &dev->port[port_num].roce.tx_port_affinity) %
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003307 MLX5_MAX_PORTS +
3308 1;
3309 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x\n",
3310 tx_port_affinity, qp_base->mqp.qpn);
3311 }
3312
3313 return tx_port_affinity;
3314}
3315
Eli Cohene126ba92013-07-07 17:25:49 +03003316static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
3317 const struct ib_qp_attr *attr, int attr_mask,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003318 enum ib_qp_state cur_state,
3319 enum ib_qp_state new_state,
3320 const struct mlx5_ib_modify_qp *ucmd,
3321 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03003322{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003323 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
3324 [MLX5_QP_STATE_RST] = {
3325 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3326 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3327 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
3328 },
3329 [MLX5_QP_STATE_INIT] = {
3330 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3331 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3332 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
3333 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
3334 },
3335 [MLX5_QP_STATE_RTR] = {
3336 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3337 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3338 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
3339 },
3340 [MLX5_QP_STATE_RTS] = {
3341 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3342 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3343 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
3344 },
3345 [MLX5_QP_STATE_SQD] = {
3346 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3347 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3348 },
3349 [MLX5_QP_STATE_SQER] = {
3350 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3351 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3352 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
3353 },
3354 [MLX5_QP_STATE_ERR] = {
3355 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3356 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3357 }
3358 };
3359
Eli Cohene126ba92013-07-07 17:25:49 +03003360 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3361 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02003362 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03003363 struct mlx5_ib_cq *send_cq, *recv_cq;
3364 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03003365 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03003366 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003367 enum mlx5_qp_state mlx5_cur, mlx5_new;
3368 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03003369 int mlx5_st;
3370 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003371 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03003372 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003373
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003374 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
3375 qp->qp_sub_type : ibqp->qp_type);
3376 if (mlx5_st < 0)
3377 return -EINVAL;
3378
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003379 context = kzalloc(sizeof(*context), GFP_KERNEL);
3380 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03003381 return -ENOMEM;
3382
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003383 pd = get_pd(qp);
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003384 context->flags = cpu_to_be32(mlx5_st << 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003385
3386 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
3387 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3388 } else {
3389 switch (attr->path_mig_state) {
3390 case IB_MIG_MIGRATED:
3391 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3392 break;
3393 case IB_MIG_REARM:
3394 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
3395 break;
3396 case IB_MIG_ARMED:
3397 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
3398 break;
3399 }
3400 }
3401
Aviv Heller13eab212016-09-18 20:48:04 +03003402 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
3403 if ((ibqp->qp_type == IB_QPT_RC) ||
3404 (ibqp->qp_type == IB_QPT_UD &&
3405 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
3406 (ibqp->qp_type == IB_QPT_UC) ||
3407 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
3408 (ibqp->qp_type == IB_QPT_XRC_INI) ||
3409 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
Aviv Heller7c34ec12018-08-23 13:47:53 +03003410 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02003411 u8 p = mlx5_core_native_port_num(dev->mdev) - 1;
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003412 tx_affinity = get_tx_affinity(dev, pd, base, p,
3413 udata);
Aviv Heller13eab212016-09-18 20:48:04 +03003414 context->flags |= cpu_to_be32(tx_affinity << 24);
3415 }
3416 }
3417 }
3418
Haggai Erand16e91d2016-02-29 15:45:05 +02003419 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003420 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003421 } else if ((ibqp->qp_type == IB_QPT_UD &&
3422 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003423 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
3424 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
3425 } else if (attr_mask & IB_QP_PATH_MTU) {
3426 if (attr->path_mtu < IB_MTU_256 ||
3427 attr->path_mtu > IB_MTU_4096) {
3428 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3429 err = -EINVAL;
3430 goto out;
3431 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003432 context->mtu_msgmax = (attr->path_mtu << 5) |
3433 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03003434 }
3435
3436 if (attr_mask & IB_QP_DEST_QPN)
3437 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
3438
3439 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003440 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003441
3442 /* todo implement counter_index functionality */
3443
3444 if (is_sqp(ibqp->qp_type))
3445 context->pri_path.port = qp->port;
3446
3447 if (attr_mask & IB_QP_PORT)
3448 context->pri_path.port = attr->port_num;
3449
3450 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003451 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003452 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003453 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003454 if (err)
3455 goto out;
3456 }
3457
3458 if (attr_mask & IB_QP_TIMEOUT)
3459 context->pri_path.ackto_lt |= attr->timeout << 3;
3460
3461 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003462 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3463 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003464 attr->alt_port_num,
3465 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3466 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003467 if (err)
3468 goto out;
3469 }
3470
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003471 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3472 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003473
3474 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3475 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3476 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3477 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3478
3479 if (attr_mask & IB_QP_RNR_RETRY)
3480 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3481
3482 if (attr_mask & IB_QP_RETRY_CNT)
3483 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3484
3485 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3486 if (attr->max_rd_atomic)
3487 context->params1 |=
3488 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3489 }
3490
3491 if (attr_mask & IB_QP_SQ_PSN)
3492 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3493
3494 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3495 if (attr->max_dest_rd_atomic)
3496 context->params2 |=
3497 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3498 }
3499
Yonatan Cohena60109d2018-10-10 09:25:16 +03003500 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
Bart Van Asschebf3b4f02019-01-30 16:30:51 -08003501 __be32 access_flags;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003502
3503 err = to_mlx5_access_flags(qp, attr, attr_mask, &access_flags);
3504 if (err)
3505 goto out;
3506
3507 context->params2 |= access_flags;
3508 }
Eli Cohene126ba92013-07-07 17:25:49 +03003509
3510 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3511 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3512
3513 if (attr_mask & IB_QP_RQ_PSN)
3514 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3515
3516 if (attr_mask & IB_QP_QKEY)
3517 context->qkey = cpu_to_be32(attr->qkey);
3518
3519 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3520 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3521
Mark Bloch0837e862016-06-17 15:10:55 +03003522 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3523 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3524 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003525
3526 /* Underlay port should be used - index 0 function per port */
3527 if (qp->flags & MLX5_IB_QP_UNDERLAY)
3528 port_num = 0;
3529
Alex Veskereb49ab02016-08-28 12:25:53 +03003530 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03003531 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03003532 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003533 }
3534
Eli Cohene126ba92013-07-07 17:25:49 +03003535 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3536 context->sq_crq_size |= cpu_to_be16(1 << 4);
3537
Haggai Eranb11a4f92016-02-29 15:45:03 +02003538 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
3539 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003540
3541 mlx5_cur = to_mlx5_state(cur_state);
3542 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003543
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003544 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003545 !optab[mlx5_cur][mlx5_new]) {
3546 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003547 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003548 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003549
3550 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003551 optpar = ib_mask_to_mlx5_opt(attr_mask);
3552 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003553
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003554 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3555 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003556 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3557
3558 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003559 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03003560 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003561 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3562 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003563
3564 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003565 raw_qp_param.rl.rate = attr->rate_limit;
3566
3567 if (ucmd->burst_info.max_burst_sz) {
3568 if (attr->rate_limit &&
3569 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3570 raw_qp_param.rl.max_burst_sz =
3571 ucmd->burst_info.max_burst_sz;
3572 } else {
3573 err = -EINVAL;
3574 goto out;
3575 }
3576 }
3577
3578 if (ucmd->burst_info.typical_pkt_sz) {
3579 if (attr->rate_limit &&
3580 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3581 raw_qp_param.rl.typical_pkt_sz =
3582 ucmd->burst_info.typical_pkt_sz;
3583 } else {
3584 err = -EINVAL;
3585 goto out;
3586 }
3587 }
3588
Bodong Wang7d29f342016-12-01 13:43:16 +02003589 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3590 }
3591
Aviv Heller13eab212016-09-18 20:48:04 +03003592 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003593 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003594 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003595 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003596 }
3597
Eli Cohene126ba92013-07-07 17:25:49 +03003598 if (err)
3599 goto out;
3600
3601 qp->state = new_state;
3602
3603 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003604 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003605 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003606 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003607 if (attr_mask & IB_QP_PORT)
3608 qp->port = attr->port_num;
3609 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003610 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003611
3612 /*
3613 * If we moved a kernel QP to RESET, clean up all old CQ
3614 * entries and reinitialize the QP.
3615 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02003616 if (new_state == IB_QPS_RESET &&
3617 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003618 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003619 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3620 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003621 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003622
3623 qp->rq.head = 0;
3624 qp->rq.tail = 0;
3625 qp->sq.head = 0;
3626 qp->sq.tail = 0;
3627 qp->sq.cur_post = 0;
Guy Levi34f4c952018-11-26 08:15:50 +02003628 if (qp->sq.wqe_cnt)
3629 qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003630 qp->db.db[MLX5_RCV_DBR] = 0;
3631 qp->db.db[MLX5_SND_DBR] = 0;
3632 }
3633
3634out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003635 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003636 return err;
3637}
3638
Moni Shouac32a4f22018-01-02 16:19:32 +02003639static inline bool is_valid_mask(int mask, int req, int opt)
3640{
3641 if ((mask & req) != req)
3642 return false;
3643
3644 if (mask & ~(req | opt))
3645 return false;
3646
3647 return true;
3648}
3649
3650/* check valid transition for driver QP types
3651 * for now the only QP type that this function supports is DCI
3652 */
3653static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3654 enum ib_qp_attr_mask attr_mask)
3655{
3656 int req = IB_QP_STATE;
3657 int opt = 0;
3658
Moni Shoua99ed7482018-09-12 09:33:55 +03003659 if (new_state == IB_QPS_RESET) {
3660 return is_valid_mask(attr_mask, req, opt);
3661 } else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Moni Shouac32a4f22018-01-02 16:19:32 +02003662 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
3663 return is_valid_mask(attr_mask, req, opt);
3664 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3665 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3666 return is_valid_mask(attr_mask, req, opt);
3667 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3668 req |= IB_QP_PATH_MTU;
Artemy Kovalyov5ec03042018-11-05 08:12:07 +02003669 opt = IB_QP_PKEY_INDEX | IB_QP_AV;
Moni Shouac32a4f22018-01-02 16:19:32 +02003670 return is_valid_mask(attr_mask, req, opt);
3671 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3672 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3673 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3674 opt = IB_QP_MIN_RNR_TIMER;
3675 return is_valid_mask(attr_mask, req, opt);
3676 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3677 opt = IB_QP_MIN_RNR_TIMER;
3678 return is_valid_mask(attr_mask, req, opt);
3679 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3680 return is_valid_mask(attr_mask, req, opt);
3681 }
3682 return false;
3683}
3684
Moni Shoua776a3902018-01-02 16:19:33 +02003685/* mlx5_ib_modify_dct: modify a DCT QP
3686 * valid transitions are:
3687 * RESET to INIT: must set access_flags, pkey_index and port
3688 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3689 * mtu, gid_index and hop_limit
3690 * Other transitions and attributes are illegal
3691 */
3692static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3693 int attr_mask, struct ib_udata *udata)
3694{
3695 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3696 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3697 enum ib_qp_state cur_state, new_state;
3698 int err = 0;
3699 int required = IB_QP_STATE;
3700 void *dctc;
3701
3702 if (!(attr_mask & IB_QP_STATE))
3703 return -EINVAL;
3704
3705 cur_state = qp->state;
3706 new_state = attr->qp_state;
3707
3708 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3709 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3710 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3711 if (!is_valid_mask(attr_mask, required, 0))
3712 return -EINVAL;
3713
3714 if (attr->port_num == 0 ||
3715 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3716 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3717 attr->port_num, dev->num_ports);
3718 return -EINVAL;
3719 }
3720 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3721 MLX5_SET(dctc, dctc, rre, 1);
3722 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3723 MLX5_SET(dctc, dctc, rwe, 1);
3724 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
Yonatan Cohena60109d2018-10-10 09:25:16 +03003725 int atomic_mode;
3726
3727 atomic_mode = get_atomic_mode(dev, MLX5_IB_QPT_DCT);
3728 if (atomic_mode < 0)
Moni Shoua776a3902018-01-02 16:19:33 +02003729 return -EOPNOTSUPP;
Yonatan Cohena60109d2018-10-10 09:25:16 +03003730
3731 MLX5_SET(dctc, dctc, atomic_mode, atomic_mode);
Moni Shoua776a3902018-01-02 16:19:33 +02003732 MLX5_SET(dctc, dctc, rae, 1);
Moni Shoua776a3902018-01-02 16:19:33 +02003733 }
3734 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3735 MLX5_SET(dctc, dctc, port, attr->port_num);
3736 MLX5_SET(dctc, dctc, counter_set_id, dev->port[attr->port_num - 1].cnts.set_id);
3737
3738 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3739 struct mlx5_ib_modify_qp_resp resp = {};
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003740 u32 out[MLX5_ST_SZ_DW(create_dct_out)] = {0};
Moni Shoua776a3902018-01-02 16:19:33 +02003741 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3742 sizeof(resp.dctn);
3743
3744 if (udata->outlen < min_resp_len)
3745 return -EINVAL;
3746 resp.response_length = min_resp_len;
3747
3748 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3749 if (!is_valid_mask(attr_mask, required, 0))
3750 return -EINVAL;
3751 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3752 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3753 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3754 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3755 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3756 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3757
3758 err = mlx5_core_create_dct(dev->mdev, &qp->dct.mdct, qp->dct.in,
Yishai Hadasc5ae1952019-03-06 19:21:42 +02003759 MLX5_ST_SZ_BYTES(create_dct_in), out,
3760 sizeof(out));
Moni Shoua776a3902018-01-02 16:19:33 +02003761 if (err)
3762 return err;
3763 resp.dctn = qp->dct.mdct.mqp.qpn;
3764 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3765 if (err) {
3766 mlx5_core_destroy_dct(dev->mdev, &qp->dct.mdct);
3767 return err;
3768 }
3769 } else {
3770 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3771 return -EINVAL;
3772 }
3773 if (err)
3774 qp->state = IB_QPS_ERR;
3775 else
3776 qp->state = new_state;
3777 return err;
3778}
3779
Eli Cohene126ba92013-07-07 17:25:49 +03003780int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3781 int attr_mask, struct ib_udata *udata)
3782{
3783 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3784 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02003785 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02003786 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003787 enum ib_qp_state cur_state, new_state;
Bodong Wang61147f32018-03-19 15:10:30 +02003788 size_t required_cmd_sz;
Eli Cohene126ba92013-07-07 17:25:49 +03003789 int err = -EINVAL;
3790 int port;
3791
Yishai Hadas28d61372016-05-23 15:20:56 +03003792 if (ibqp->rwq_ind_tbl)
3793 return -ENOSYS;
3794
Bodong Wang61147f32018-03-19 15:10:30 +02003795 if (udata && udata->inlen) {
3796 required_cmd_sz = offsetof(typeof(ucmd), reserved) +
3797 sizeof(ucmd.reserved);
3798 if (udata->inlen < required_cmd_sz)
3799 return -EINVAL;
3800
3801 if (udata->inlen > sizeof(ucmd) &&
3802 !ib_is_udata_cleared(udata, sizeof(ucmd),
3803 udata->inlen - sizeof(ucmd)))
3804 return -EOPNOTSUPP;
3805
3806 if (ib_copy_from_udata(&ucmd, udata,
3807 min(udata->inlen, sizeof(ucmd))))
3808 return -EFAULT;
3809
3810 if (ucmd.comp_mask ||
3811 memchr_inv(&ucmd.reserved, 0, sizeof(ucmd.reserved)) ||
3812 memchr_inv(&ucmd.burst_info.reserved, 0,
3813 sizeof(ucmd.burst_info.reserved)))
3814 return -EOPNOTSUPP;
3815 }
3816
Haggai Erand16e91d2016-02-29 15:45:05 +02003817 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3818 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3819
Moni Shouac32a4f22018-01-02 16:19:32 +02003820 if (ibqp->qp_type == IB_QPT_DRIVER)
3821 qp_type = qp->qp_sub_type;
3822 else
3823 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3824 IB_QPT_GSI : ibqp->qp_type;
3825
Moni Shoua776a3902018-01-02 16:19:33 +02003826 if (qp_type == MLX5_IB_QPT_DCT)
3827 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02003828
Eli Cohene126ba92013-07-07 17:25:49 +03003829 mutex_lock(&qp->mutex);
3830
3831 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
3832 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
3833
Achiad Shochat2811ba52015-12-23 18:47:24 +02003834 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
3835 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003836 }
3837
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003838 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
3839 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
3840 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
3841 attr_mask);
3842 goto out;
3843 }
3844 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02003845 qp_type != MLX5_IB_QPT_DCI &&
Kamal Heibd31131b2018-10-02 16:11:21 +03003846 !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
3847 attr_mask)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003848 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3849 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003850 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02003851 } else if (qp_type == MLX5_IB_QPT_DCI &&
3852 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
3853 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3854 cur_state, new_state, qp_type, attr_mask);
3855 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003856 }
Eli Cohene126ba92013-07-07 17:25:49 +03003857
3858 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003859 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003860 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003861 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3862 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003863 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003864 }
Eli Cohene126ba92013-07-07 17:25:49 +03003865
3866 if (attr_mask & IB_QP_PKEY_INDEX) {
3867 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003868 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02003869 dev->mdev->port_caps[port - 1].pkey_table_len) {
3870 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
3871 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003872 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003873 }
Eli Cohene126ba92013-07-07 17:25:49 +03003874 }
3875
3876 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003877 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003878 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
3879 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
3880 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003881 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003882 }
Eli Cohene126ba92013-07-07 17:25:49 +03003883
3884 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003885 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003886 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
3887 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
3888 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003889 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003890 }
Eli Cohene126ba92013-07-07 17:25:49 +03003891
3892 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
3893 err = 0;
3894 goto out;
3895 }
3896
Bodong Wang61147f32018-03-19 15:10:30 +02003897 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
Shamir Rabinovitch89944452019-02-07 18:44:49 +02003898 new_state, &ucmd, udata);
Eli Cohene126ba92013-07-07 17:25:49 +03003899
3900out:
3901 mutex_unlock(&qp->mutex);
3902 return err;
3903}
3904
Guy Levi34f4c952018-11-26 08:15:50 +02003905static void _handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
3906 u32 wqe_sz, void **cur_edge)
3907{
3908 u32 idx;
3909
3910 idx = (sq->cur_post + (wqe_sz >> 2)) & (sq->wqe_cnt - 1);
3911 *cur_edge = get_sq_edge(sq, idx);
3912
3913 *seg = mlx5_frag_buf_get_wqe(&sq->fbc, idx);
3914}
3915
3916/* handle_post_send_edge - Check if we get to SQ edge. If yes, update to the
3917 * next nearby edge and get new address translation for current WQE position.
3918 * @sq - SQ buffer.
3919 * @seg: Current WQE position (16B aligned).
3920 * @wqe_sz: Total current WQE size [16B].
3921 * @cur_edge: Updated current edge.
3922 */
3923static inline void handle_post_send_edge(struct mlx5_ib_wq *sq, void **seg,
3924 u32 wqe_sz, void **cur_edge)
3925{
3926 if (likely(*seg != *cur_edge))
3927 return;
3928
3929 _handle_post_send_edge(sq, seg, wqe_sz, cur_edge);
3930}
3931
3932/* memcpy_send_wqe - copy data from src to WQE and update the relevant WQ's
3933 * pointers. At the end @seg is aligned to 16B regardless the copied size.
3934 * @sq - SQ buffer.
3935 * @cur_edge: Updated current edge.
3936 * @seg: Current WQE position (16B aligned).
3937 * @wqe_sz: Total current WQE size [16B].
3938 * @src: Pointer to copy from.
3939 * @n: Number of bytes to copy.
3940 */
3941static inline void memcpy_send_wqe(struct mlx5_ib_wq *sq, void **cur_edge,
3942 void **seg, u32 *wqe_sz, const void *src,
3943 size_t n)
3944{
3945 while (likely(n)) {
3946 size_t leftlen = *cur_edge - *seg;
3947 size_t copysz = min_t(size_t, leftlen, n);
3948 size_t stride;
3949
3950 memcpy(*seg, src, copysz);
3951
3952 n -= copysz;
3953 src += copysz;
3954 stride = !n ? ALIGN(copysz, 16) : copysz;
3955 *seg += stride;
3956 *wqe_sz += stride >> 4;
3957 handle_post_send_edge(sq, seg, *wqe_sz, cur_edge);
3958 }
3959}
3960
Eli Cohene126ba92013-07-07 17:25:49 +03003961static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
3962{
3963 struct mlx5_ib_cq *cq;
3964 unsigned cur;
3965
3966 cur = wq->head - wq->tail;
3967 if (likely(cur + nreq < wq->max_post))
3968 return 0;
3969
3970 cq = to_mcq(ib_cq);
3971 spin_lock(&cq->lock);
3972 cur = wq->head - wq->tail;
3973 spin_unlock(&cq->lock);
3974
3975 return cur + nreq >= wq->max_post;
3976}
3977
3978static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
3979 u64 remote_addr, u32 rkey)
3980{
3981 rseg->raddr = cpu_to_be64(remote_addr);
3982 rseg->rkey = cpu_to_be32(rkey);
3983 rseg->reserved = 0;
3984}
3985
Guy Levi34f4c952018-11-26 08:15:50 +02003986static void set_eth_seg(const struct ib_send_wr *wr, struct mlx5_ib_qp *qp,
3987 void **seg, int *size, void **cur_edge)
Erez Shitritf0313962016-02-21 16:27:17 +02003988{
Guy Levi34f4c952018-11-26 08:15:50 +02003989 struct mlx5_wqe_eth_seg *eseg = *seg;
Erez Shitritf0313962016-02-21 16:27:17 +02003990
3991 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
3992
3993 if (wr->send_flags & IB_SEND_IP_CSUM)
3994 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
3995 MLX5_ETH_WQE_L4_CSUM;
3996
Erez Shitritf0313962016-02-21 16:27:17 +02003997 if (wr->opcode == IB_WR_LSO) {
3998 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Guy Levi34f4c952018-11-26 08:15:50 +02003999 size_t left, copysz;
Erez Shitritf0313962016-02-21 16:27:17 +02004000 void *pdata = ud_wr->header;
Guy Levi34f4c952018-11-26 08:15:50 +02004001 size_t stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004002
4003 left = ud_wr->hlen;
4004 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02004005 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02004006
Guy Levi34f4c952018-11-26 08:15:50 +02004007 /* memcpy_send_wqe should get a 16B align address. Hence, we
4008 * first copy up to the current edge and then, if needed,
4009 * fall-through to memcpy_send_wqe.
Erez Shitritf0313962016-02-21 16:27:17 +02004010 */
Guy Levi34f4c952018-11-26 08:15:50 +02004011 copysz = min_t(u64, *cur_edge - (void *)eseg->inline_hdr.start,
4012 left);
4013 memcpy(eseg->inline_hdr.start, pdata, copysz);
4014 stride = ALIGN(sizeof(struct mlx5_wqe_eth_seg) -
4015 sizeof(eseg->inline_hdr.start) + copysz, 16);
4016 *size += stride / 16;
4017 *seg += stride;
Erez Shitritf0313962016-02-21 16:27:17 +02004018
Guy Levi34f4c952018-11-26 08:15:50 +02004019 if (copysz < left) {
4020 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02004021 left -= copysz;
4022 pdata += copysz;
Guy Levi34f4c952018-11-26 08:15:50 +02004023 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, pdata,
4024 left);
Erez Shitritf0313962016-02-21 16:27:17 +02004025 }
Guy Levi34f4c952018-11-26 08:15:50 +02004026
4027 return;
Erez Shitritf0313962016-02-21 16:27:17 +02004028 }
4029
Guy Levi34f4c952018-11-26 08:15:50 +02004030 *seg += sizeof(struct mlx5_wqe_eth_seg);
4031 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
Erez Shitritf0313962016-02-21 16:27:17 +02004032}
4033
Eli Cohene126ba92013-07-07 17:25:49 +03004034static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004035 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004036{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004037 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
4038 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
4039 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004040}
4041
4042static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
4043{
4044 dseg->byte_count = cpu_to_be32(sg->length);
4045 dseg->lkey = cpu_to_be32(sg->lkey);
4046 dseg->addr = cpu_to_be64(sg->addr);
4047}
4048
Artemy Kovalyov31616252017-01-02 11:37:42 +02004049static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03004050{
Artemy Kovalyov31616252017-01-02 11:37:42 +02004051 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
4052 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03004053}
4054
4055static __be64 frwr_mkey_mask(void)
4056{
4057 u64 result;
4058
4059 result = MLX5_MKEY_MASK_LEN |
4060 MLX5_MKEY_MASK_PAGE_SIZE |
4061 MLX5_MKEY_MASK_START_ADDR |
4062 MLX5_MKEY_MASK_EN_RINVAL |
4063 MLX5_MKEY_MASK_KEY |
4064 MLX5_MKEY_MASK_LR |
4065 MLX5_MKEY_MASK_LW |
4066 MLX5_MKEY_MASK_RR |
4067 MLX5_MKEY_MASK_RW |
4068 MLX5_MKEY_MASK_A |
4069 MLX5_MKEY_MASK_SMALL_FENCE |
4070 MLX5_MKEY_MASK_FREE;
4071
4072 return cpu_to_be64(result);
4073}
4074
Sagi Grimberge6631812014-02-23 14:19:11 +02004075static __be64 sig_mkey_mask(void)
4076{
4077 u64 result;
4078
4079 result = MLX5_MKEY_MASK_LEN |
4080 MLX5_MKEY_MASK_PAGE_SIZE |
4081 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004082 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02004083 MLX5_MKEY_MASK_EN_RINVAL |
4084 MLX5_MKEY_MASK_KEY |
4085 MLX5_MKEY_MASK_LR |
4086 MLX5_MKEY_MASK_LW |
4087 MLX5_MKEY_MASK_RR |
4088 MLX5_MKEY_MASK_RW |
4089 MLX5_MKEY_MASK_SMALL_FENCE |
4090 MLX5_MKEY_MASK_FREE |
4091 MLX5_MKEY_MASK_BSF_EN;
4092
4093 return cpu_to_be64(result);
4094}
4095
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004096static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Idan Burstein064e5262018-05-02 13:16:39 +03004097 struct mlx5_ib_mr *mr, bool umr_inline)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004098{
Artemy Kovalyov31616252017-01-02 11:37:42 +02004099 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004100
4101 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004102
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004103 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Idan Burstein064e5262018-05-02 13:16:39 +03004104 if (umr_inline)
4105 umr->flags |= MLX5_UMR_INLINE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004106 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004107 umr->mkey_mask = frwr_mkey_mask();
4108}
4109
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004110static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03004111{
4112 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004113 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03004114 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03004115}
4116
Artemy Kovalyov31616252017-01-02 11:37:42 +02004117static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004118{
4119 u64 result;
4120
Artemy Kovalyov31616252017-01-02 11:37:42 +02004121 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02004122 MLX5_MKEY_MASK_FREE;
4123
4124 return cpu_to_be64(result);
4125}
4126
Artemy Kovalyov31616252017-01-02 11:37:42 +02004127static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02004128{
4129 u64 result;
4130
4131 result = MLX5_MKEY_MASK_FREE;
4132
4133 return cpu_to_be64(result);
4134}
4135
Noa Osherovich56e11d62016-02-29 16:46:51 +02004136static __be64 get_umr_update_translation_mask(void)
4137{
4138 u64 result;
4139
4140 result = MLX5_MKEY_MASK_LEN |
4141 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004142 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004143
4144 return cpu_to_be64(result);
4145}
4146
Artemy Kovalyov31616252017-01-02 11:37:42 +02004147static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02004148{
4149 u64 result;
4150
Artemy Kovalyov31616252017-01-02 11:37:42 +02004151 result = MLX5_MKEY_MASK_LR |
4152 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004153 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02004154 MLX5_MKEY_MASK_RW;
4155
4156 if (atomic)
4157 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004158
4159 return cpu_to_be64(result);
4160}
4161
4162static __be64 get_umr_update_pd_mask(void)
4163{
4164 u64 result;
4165
Artemy Kovalyov31616252017-01-02 11:37:42 +02004166 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004167
4168 return cpu_to_be64(result);
4169}
4170
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004171static int umr_check_mkey_mask(struct mlx5_ib_dev *dev, u64 mask)
4172{
4173 if ((mask & MLX5_MKEY_MASK_PAGE_SIZE &&
4174 MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) ||
4175 (mask & MLX5_MKEY_MASK_A &&
4176 MLX5_CAP_GEN(dev->mdev, umr_modify_atomic_disabled)))
4177 return -EPERM;
4178 return 0;
4179}
4180
4181static int set_reg_umr_segment(struct mlx5_ib_dev *dev,
4182 struct mlx5_wqe_umr_ctrl_seg *umr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004183 const struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03004184{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004185 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03004186
4187 memset(umr, 0, sizeof(*umr));
4188
Haggai Eran968e78d2014-12-11 17:04:11 +02004189 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
4190 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
4191 else
4192 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
4193
Artemy Kovalyov31616252017-01-02 11:37:42 +02004194 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
4195 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
4196 u64 offset = get_xlt_octo(umrwr->offset);
4197
4198 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
4199 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
4200 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004201 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02004202 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
4203 umr->mkey_mask |= get_umr_update_translation_mask();
4204 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
4205 umr->mkey_mask |= get_umr_update_access_mask(atomic);
4206 umr->mkey_mask |= get_umr_update_pd_mask();
4207 }
4208 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
4209 umr->mkey_mask |= get_umr_enable_mr_mask();
4210 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
4211 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03004212
4213 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02004214 umr->flags |= MLX5_UMR_INLINE;
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004215
4216 return umr_check_mkey_mask(dev, be64_to_cpu(umr->mkey_mask));
Eli Cohene126ba92013-07-07 17:25:49 +03004217}
4218
4219static u8 get_umr_flags(int acc)
4220{
4221 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
4222 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
4223 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
4224 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02004225 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03004226}
4227
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004228static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
4229 struct mlx5_ib_mr *mr,
4230 u32 key, int access)
4231{
4232 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
4233
4234 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02004235
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004236 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004237 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004238 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02004239 /* KLMs take twice the size of MTTs */
4240 ndescs *= 2;
4241
4242 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004243 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
4244 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
4245 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
4246 seg->len = cpu_to_be64(mr->ibmr.length);
4247 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004248}
4249
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004250static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03004251{
4252 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004253 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004254}
4255
Bart Van Asschef696bf62018-07-18 09:25:14 -07004256static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg,
4257 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004258{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004259 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004260
Eli Cohene126ba92013-07-07 17:25:49 +03004261 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02004262 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02004263 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03004264
Haggai Eran968e78d2014-12-11 17:04:11 +02004265 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004266 if (umrwr->pd)
4267 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
4268 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
4269 !umrwr->length)
4270 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
4271
4272 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02004273 seg->len = cpu_to_be64(umrwr->length);
4274 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03004275 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02004276 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03004277}
4278
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004279static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
4280 struct mlx5_ib_mr *mr,
4281 struct mlx5_ib_pd *pd)
4282{
4283 int bcount = mr->desc_size * mr->ndescs;
4284
4285 dseg->addr = cpu_to_be64(mr->desc_map);
4286 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
4287 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
4288}
4289
Bart Van Asschef696bf62018-07-18 09:25:14 -07004290static __be32 send_ieth(const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03004291{
4292 switch (wr->opcode) {
4293 case IB_WR_SEND_WITH_IMM:
4294 case IB_WR_RDMA_WRITE_WITH_IMM:
4295 return wr->ex.imm_data;
4296
4297 case IB_WR_SEND_WITH_INV:
4298 return cpu_to_be32(wr->ex.invalidate_rkey);
4299
4300 default:
4301 return 0;
4302 }
4303}
4304
4305static u8 calc_sig(void *wqe, int size)
4306{
4307 u8 *p = wqe;
4308 u8 res = 0;
4309 int i;
4310
4311 for (i = 0; i < size; i++)
4312 res ^= p[i];
4313
4314 return ~res;
4315}
4316
4317static u8 wq_sig(void *wqe)
4318{
4319 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
4320}
4321
Bart Van Asschef696bf62018-07-18 09:25:14 -07004322static int set_data_inl_seg(struct mlx5_ib_qp *qp, const struct ib_send_wr *wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004323 void **wqe, int *wqe_sz, void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004324{
4325 struct mlx5_wqe_inline_seg *seg;
Guy Levi34f4c952018-11-26 08:15:50 +02004326 size_t offset;
Eli Cohene126ba92013-07-07 17:25:49 +03004327 int inl = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004328 int i;
4329
Guy Levi34f4c952018-11-26 08:15:50 +02004330 seg = *wqe;
4331 *wqe += sizeof(*seg);
4332 offset = sizeof(*seg);
4333
Eli Cohene126ba92013-07-07 17:25:49 +03004334 for (i = 0; i < wr->num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02004335 size_t len = wr->sg_list[i].length;
4336 void *addr = (void *)(unsigned long)(wr->sg_list[i].addr);
4337
Eli Cohene126ba92013-07-07 17:25:49 +03004338 inl += len;
4339
4340 if (unlikely(inl > qp->max_inline_data))
4341 return -ENOMEM;
4342
Guy Levi34f4c952018-11-26 08:15:50 +02004343 while (likely(len)) {
4344 size_t leftlen;
4345 size_t copysz;
4346
4347 handle_post_send_edge(&qp->sq, wqe,
4348 *wqe_sz + (offset >> 4),
4349 cur_edge);
4350
4351 leftlen = *cur_edge - *wqe;
4352 copysz = min_t(size_t, leftlen, len);
4353
4354 memcpy(*wqe, addr, copysz);
4355 len -= copysz;
4356 addr += copysz;
4357 *wqe += copysz;
4358 offset += copysz;
Eli Cohene126ba92013-07-07 17:25:49 +03004359 }
Eli Cohene126ba92013-07-07 17:25:49 +03004360 }
4361
4362 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
4363
Guy Levi34f4c952018-11-26 08:15:50 +02004364 *wqe_sz += ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004365
4366 return 0;
4367}
4368
Sagi Grimberge6631812014-02-23 14:19:11 +02004369static u16 prot_field_size(enum ib_signature_type type)
4370{
4371 switch (type) {
4372 case IB_SIG_TYPE_T10_DIF:
4373 return MLX5_DIF_SIZE;
4374 default:
4375 return 0;
4376 }
4377}
4378
4379static u8 bs_selector(int block_size)
4380{
4381 switch (block_size) {
4382 case 512: return 0x1;
4383 case 520: return 0x2;
4384 case 4096: return 0x3;
4385 case 4160: return 0x4;
4386 case 1073741824: return 0x5;
4387 default: return 0;
4388 }
4389}
4390
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004391static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
4392 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02004393{
Sagi Grimberg142537f2014-08-13 19:54:32 +03004394 /* Valid inline section and allow BSF refresh */
4395 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
4396 MLX5_BSF_REFRESH_DIF);
4397 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
4398 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004399 /* repeating block */
4400 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
4401 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
4402 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004403
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004404 if (domain->sig.dif.ref_remap)
4405 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02004406
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004407 if (domain->sig.dif.app_escape) {
4408 if (domain->sig.dif.ref_escape)
4409 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
4410 else
4411 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02004412 }
4413
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004414 inl->dif_app_bitmask_check =
4415 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02004416}
4417
4418static int mlx5_set_bsf(struct ib_mr *sig_mr,
4419 struct ib_sig_attrs *sig_attrs,
4420 struct mlx5_bsf *bsf, u32 data_size)
4421{
4422 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
4423 struct mlx5_bsf_basic *basic = &bsf->basic;
4424 struct ib_sig_domain *mem = &sig_attrs->mem;
4425 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02004426
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004427 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02004428
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004429 /* Basic + Extended + Inline */
4430 basic->bsf_size_sbs = 1 << 7;
4431 /* Input domain check byte mask */
4432 basic->check_byte_mask = sig_attrs->check_mask;
4433 basic->raw_data_size = cpu_to_be32(data_size);
4434
4435 /* Memory domain */
4436 switch (sig_attrs->mem.sig_type) {
4437 case IB_SIG_TYPE_NONE:
4438 break;
4439 case IB_SIG_TYPE_T10_DIF:
4440 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
4441 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
4442 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
4443 break;
4444 default:
4445 return -EINVAL;
4446 }
4447
4448 /* Wire domain */
4449 switch (sig_attrs->wire.sig_type) {
4450 case IB_SIG_TYPE_NONE:
4451 break;
4452 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02004453 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004454 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004455 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03004456 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02004457 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004458 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004459 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004460 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004461 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004462 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02004463 } else
4464 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
4465
Sagi Grimberg142537f2014-08-13 19:54:32 +03004466 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004467 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02004468 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004469 default:
4470 return -EINVAL;
4471 }
4472
4473 return 0;
4474}
4475
Bart Van Asschef696bf62018-07-18 09:25:14 -07004476static int set_sig_data_segment(const struct ib_sig_handover_wr *wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004477 struct mlx5_ib_qp *qp, void **seg,
4478 int *size, void **cur_edge)
Sagi Grimberge6631812014-02-23 14:19:11 +02004479{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004480 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
4481 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004482 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004483 u32 data_len = wr->wr.sg_list->length;
4484 u32 data_key = wr->wr.sg_list->lkey;
4485 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004486 int ret;
4487 int wqe_size;
4488
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004489 if (!wr->prot ||
4490 (data_key == wr->prot->lkey &&
4491 data_va == wr->prot->addr &&
4492 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004493 /**
4494 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004495 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02004496 * So need construct:
4497 * ------------------
4498 * | data_klm |
4499 * ------------------
4500 * | BSF |
4501 * ------------------
4502 **/
4503 struct mlx5_klm *data_klm = *seg;
4504
4505 data_klm->bcount = cpu_to_be32(data_len);
4506 data_klm->key = cpu_to_be32(data_key);
4507 data_klm->va = cpu_to_be64(data_va);
4508 wqe_size = ALIGN(sizeof(*data_klm), 64);
4509 } else {
4510 /**
4511 * Source domain contains signature information
4512 * So need construct a strided block format:
4513 * ---------------------------
4514 * | stride_block_ctrl |
4515 * ---------------------------
4516 * | data_klm |
4517 * ---------------------------
4518 * | prot_klm |
4519 * ---------------------------
4520 * | BSF |
4521 * ---------------------------
4522 **/
4523 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
4524 struct mlx5_stride_block_entry *data_sentry;
4525 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004526 u32 prot_key = wr->prot->lkey;
4527 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004528 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
4529 int prot_size;
4530
4531 sblock_ctrl = *seg;
4532 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
4533 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
4534
4535 prot_size = prot_field_size(sig_attrs->mem.sig_type);
4536 if (!prot_size) {
4537 pr_err("Bad block size given: %u\n", block_size);
4538 return -EINVAL;
4539 }
4540 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
4541 prot_size);
4542 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
4543 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
4544 sblock_ctrl->num_entries = cpu_to_be16(2);
4545
4546 data_sentry->bcount = cpu_to_be16(block_size);
4547 data_sentry->key = cpu_to_be32(data_key);
4548 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004549 data_sentry->stride = cpu_to_be16(block_size);
4550
Sagi Grimberge6631812014-02-23 14:19:11 +02004551 prot_sentry->bcount = cpu_to_be16(prot_size);
4552 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004553 prot_sentry->va = cpu_to_be64(prot_va);
4554 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004555
Sagi Grimberge6631812014-02-23 14:19:11 +02004556 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
4557 sizeof(*prot_sentry), 64);
4558 }
4559
4560 *seg += wqe_size;
4561 *size += wqe_size / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004562 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004563
4564 bsf = *seg;
4565 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
4566 if (ret)
4567 return -EINVAL;
4568
4569 *seg += sizeof(*bsf);
4570 *size += sizeof(*bsf) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004571 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004572
4573 return 0;
4574}
4575
4576static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004577 const struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02004578 u32 length, u32 pdn)
4579{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004580 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004581 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004582 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004583
4584 memset(seg, 0, sizeof(*seg));
4585
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004586 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004587 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004588 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004589 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004590 MLX5_MKEY_BSF_EN | pdn);
4591 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004592 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004593 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4594}
4595
4596static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004597 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004598{
4599 memset(umr, 0, sizeof(*umr));
4600
4601 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004602 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004603 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4604 umr->mkey_mask = sig_mkey_mask();
4605}
4606
4607
Bart Van Asschef696bf62018-07-18 09:25:14 -07004608static int set_sig_umr_wr(const struct ib_send_wr *send_wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004609 struct mlx5_ib_qp *qp, void **seg, int *size,
4610 void **cur_edge)
Sagi Grimberge6631812014-02-23 14:19:11 +02004611{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004612 const struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004613 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004614 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004615 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02004616 int region_len, ret;
4617
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004618 if (unlikely(wr->wr.num_sge != 1) ||
4619 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004620 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
4621 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02004622 return -EINVAL;
4623
4624 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004625 region_len = wr->wr.sg_list->length;
4626 if (wr->prot &&
4627 (wr->prot->lkey != wr->wr.sg_list->lkey ||
4628 wr->prot->addr != wr->wr.sg_list->addr ||
4629 wr->prot->length != wr->wr.sg_list->length))
4630 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02004631
4632 /**
4633 * KLM octoword size - if protection was provided
4634 * then we use strided block format (3 octowords),
4635 * else we use single KLM (1 octoword)
4636 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02004637 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02004638
Artemy Kovalyov31616252017-01-02 11:37:42 +02004639 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004640 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4641 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004642 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004643
Artemy Kovalyov31616252017-01-02 11:37:42 +02004644 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02004645 *seg += sizeof(struct mlx5_mkey_seg);
4646 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004647 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004648
Guy Levi34f4c952018-11-26 08:15:50 +02004649 ret = set_sig_data_segment(wr, qp, seg, size, cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004650 if (ret)
4651 return ret;
4652
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004653 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004654 return 0;
4655}
4656
4657static int set_psv_wr(struct ib_sig_domain *domain,
4658 u32 psv_idx, void **seg, int *size)
4659{
4660 struct mlx5_seg_set_psv *psv_seg = *seg;
4661
4662 memset(psv_seg, 0, sizeof(*psv_seg));
4663 psv_seg->psv_num = cpu_to_be32(psv_idx);
4664 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004665 case IB_SIG_TYPE_NONE:
4666 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004667 case IB_SIG_TYPE_T10_DIF:
4668 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4669 domain->sig.dif.app_tag);
4670 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004671 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004672 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004673 pr_err("Bad signature type (%d) is given.\n",
4674 domain->sig_type);
4675 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004676 }
4677
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004678 *seg += sizeof(*psv_seg);
4679 *size += sizeof(*psv_seg) / 16;
4680
Sagi Grimberge6631812014-02-23 14:19:11 +02004681 return 0;
4682}
4683
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004684static int set_reg_wr(struct mlx5_ib_qp *qp,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004685 const struct ib_reg_wr *wr,
Guy Levi34f4c952018-11-26 08:15:50 +02004686 void **seg, int *size, void **cur_edge)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004687{
4688 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4689 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
Guy Levi34f4c952018-11-26 08:15:50 +02004690 size_t mr_list_size = mr->ndescs * mr->desc_size;
Idan Burstein064e5262018-05-02 13:16:39 +03004691 bool umr_inline = mr_list_size <= MLX5_IB_SQ_UMR_INLINE_THRESHOLD;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004692
4693 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4694 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4695 "Invalid IB_SEND_INLINE send flag\n");
4696 return -EINVAL;
4697 }
4698
Idan Burstein064e5262018-05-02 13:16:39 +03004699 set_reg_umr_seg(*seg, mr, umr_inline);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004700 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4701 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004702 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004703
4704 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4705 *seg += sizeof(struct mlx5_mkey_seg);
4706 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004707 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004708
Idan Burstein064e5262018-05-02 13:16:39 +03004709 if (umr_inline) {
Guy Levi34f4c952018-11-26 08:15:50 +02004710 memcpy_send_wqe(&qp->sq, cur_edge, seg, size, mr->descs,
4711 mr_list_size);
4712 *size = ALIGN(*size, MLX5_SEND_WQE_BB >> 4);
Idan Burstein064e5262018-05-02 13:16:39 +03004713 } else {
4714 set_reg_data_seg(*seg, mr, pd);
4715 *seg += sizeof(struct mlx5_wqe_data_seg);
4716 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4717 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004718 return 0;
4719}
4720
Guy Levi34f4c952018-11-26 08:15:50 +02004721static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size,
4722 void **cur_edge)
Eli Cohene126ba92013-07-07 17:25:49 +03004723{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004724 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004725 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4726 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004727 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004728 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004729 *seg += sizeof(struct mlx5_mkey_seg);
4730 *size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004731 handle_post_send_edge(&qp->sq, seg, *size, cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03004732}
4733
Guy Levi34f4c952018-11-26 08:15:50 +02004734static void dump_wqe(struct mlx5_ib_qp *qp, u32 idx, int size_16)
Eli Cohene126ba92013-07-07 17:25:49 +03004735{
4736 __be32 *p = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004737 int i, j;
4738
Guy Levi34f4c952018-11-26 08:15:50 +02004739 pr_debug("dump WQE index %u:\n", idx);
Eli Cohene126ba92013-07-07 17:25:49 +03004740 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4741 if ((i & 0xf) == 0) {
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004742 p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, idx);
Guy Levi34f4c952018-11-26 08:15:50 +02004743 pr_debug("WQBB at %p:\n", (void *)p);
Eli Cohene126ba92013-07-07 17:25:49 +03004744 j = 0;
Artemy Kovalyov1e5887b2019-03-19 11:24:37 +02004745 idx = (idx + 1) & (qp->sq.wqe_cnt - 1);
Eli Cohene126ba92013-07-07 17:25:49 +03004746 }
4747 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4748 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4749 be32_to_cpu(p[j + 3]));
4750 }
4751}
4752
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004753static int __begin_wqe(struct mlx5_ib_qp *qp, void **seg,
Guy Levi34f4c952018-11-26 08:15:50 +02004754 struct mlx5_wqe_ctrl_seg **ctrl,
4755 const struct ib_send_wr *wr, unsigned int *idx,
4756 int *size, void **cur_edge, int nreq,
4757 bool send_signaled, bool solicited)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004758{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004759 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4760 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004761
4762 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
Guy Levi34f4c952018-11-26 08:15:50 +02004763 *seg = mlx5_frag_buf_get_wqe(&qp->sq.fbc, *idx);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004764 *ctrl = *seg;
4765 *(uint32_t *)(*seg + 8) = 0;
4766 (*ctrl)->imm = send_ieth(wr);
4767 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004768 (send_signaled ? MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4769 (solicited ? MLX5_WQE_CTRL_SOLICITED : 0);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004770
4771 *seg += sizeof(**ctrl);
4772 *size = sizeof(**ctrl) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02004773 *cur_edge = qp->sq.cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004774
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004775 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004776}
4777
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004778static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4779 struct mlx5_wqe_ctrl_seg **ctrl,
4780 const struct ib_send_wr *wr, unsigned *idx,
Guy Levi34f4c952018-11-26 08:15:50 +02004781 int *size, void **cur_edge, int nreq)
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004782{
Guy Levi34f4c952018-11-26 08:15:50 +02004783 return __begin_wqe(qp, seg, ctrl, wr, idx, size, cur_edge, nreq,
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004784 wr->send_flags & IB_SEND_SIGNALED,
4785 wr->send_flags & IB_SEND_SOLICITED);
4786}
4787
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004788static void finish_wqe(struct mlx5_ib_qp *qp,
4789 struct mlx5_wqe_ctrl_seg *ctrl,
Guy Levi34f4c952018-11-26 08:15:50 +02004790 void *seg, u8 size, void *cur_edge,
4791 unsigned int idx, u64 wr_id, int nreq, u8 fence,
4792 u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004793{
4794 u8 opmod = 0;
4795
4796 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
4797 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02004798 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004799 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004800 if (unlikely(qp->wq_sig))
4801 ctrl->signature = wq_sig(ctrl);
4802
4803 qp->sq.wrid[idx] = wr_id;
4804 qp->sq.w_list[idx].opcode = mlx5_opcode;
4805 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
4806 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
4807 qp->sq.w_list[idx].next = qp->sq.cur_post;
Guy Levi34f4c952018-11-26 08:15:50 +02004808
4809 /* We save the edge which was possibly updated during the WQE
4810 * construction, into SQ's cache.
4811 */
4812 seg = PTR_ALIGN(seg, MLX5_SEND_WQE_BB);
4813 qp->sq.cur_edge = (unlikely(seg == cur_edge)) ?
4814 get_sq_edge(&qp->sq, qp->sq.cur_post &
4815 (qp->sq.wqe_cnt - 1)) :
4816 cur_edge;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004817}
4818
Bart Van Assched34ac5c2018-07-18 09:25:32 -07004819static int _mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
4820 const struct ib_send_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03004821{
4822 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
4823 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004824 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02004825 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02004826 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004827 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02004828 struct mlx5_bf *bf;
Guy Levi34f4c952018-11-26 08:15:50 +02004829 void *cur_edge;
Eli Cohene126ba92013-07-07 17:25:49 +03004830 int uninitialized_var(size);
Eli Cohene126ba92013-07-07 17:25:49 +03004831 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03004832 unsigned idx;
4833 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004834 int num_sge;
4835 void *seg;
4836 int nreq;
4837 int i;
4838 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004839 u8 fence;
4840
Parav Pandit6c755202018-08-28 14:45:29 +03004841 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
4842 !drain)) {
4843 *bad_wr = wr;
4844 return -EIO;
4845 }
4846
Haggai Erand16e91d2016-02-29 15:45:05 +02004847 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4848 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
4849
4850 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004851 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02004852
Eli Cohene126ba92013-07-07 17:25:49 +03004853 spin_lock_irqsave(&qp->sq.lock, flags);
4854
4855 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04004856 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03004857 mlx5_ib_warn(dev, "\n");
4858 err = -EINVAL;
4859 *bad_wr = wr;
4860 goto out;
4861 }
4862
Eli Cohene126ba92013-07-07 17:25:49 +03004863 num_sge = wr->num_sge;
4864 if (unlikely(num_sge > qp->sq.max_gs)) {
4865 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03004866 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03004867 *bad_wr = wr;
4868 goto out;
4869 }
4870
Guy Levi34f4c952018-11-26 08:15:50 +02004871 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, &cur_edge,
4872 nreq);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004873 if (err) {
4874 mlx5_ib_warn(dev, "\n");
4875 err = -ENOMEM;
4876 *bad_wr = wr;
4877 goto out;
4878 }
Eli Cohene126ba92013-07-07 17:25:49 +03004879
Majd Dibbiny074fca32018-11-05 08:07:37 +02004880 if (wr->opcode == IB_WR_REG_MR) {
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004881 fence = dev->umr_fence;
4882 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Majd Dibbiny074fca32018-11-05 08:07:37 +02004883 } else {
4884 if (wr->send_flags & IB_SEND_FENCE) {
4885 if (qp->next_fence)
4886 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
4887 else
4888 fence = MLX5_FENCE_MODE_FENCE;
4889 } else {
4890 fence = qp->next_fence;
4891 }
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004892 }
4893
Eli Cohene126ba92013-07-07 17:25:49 +03004894 switch (ibqp->qp_type) {
4895 case IB_QPT_XRC_INI:
4896 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03004897 seg += sizeof(*xrc);
4898 size += sizeof(*xrc) / 16;
4899 /* fall through */
4900 case IB_QPT_RC:
4901 switch (wr->opcode) {
4902 case IB_WR_RDMA_READ:
4903 case IB_WR_RDMA_WRITE:
4904 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004905 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4906 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004907 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004908 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4909 break;
4910
4911 case IB_WR_ATOMIC_CMP_AND_SWP:
4912 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03004913 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03004914 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
4915 err = -ENOSYS;
4916 *bad_wr = wr;
4917 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004918
4919 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03004920 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
4921 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Guy Levi34f4c952018-11-26 08:15:50 +02004922 set_linv_wr(qp, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03004923 num_sge = 0;
4924 break;
4925
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004926 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004927 qp->sq.wr_data[idx] = IB_WR_REG_MR;
4928 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
Guy Levi34f4c952018-11-26 08:15:50 +02004929 err = set_reg_wr(qp, reg_wr(wr), &seg, &size,
4930 &cur_edge);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004931 if (err) {
4932 *bad_wr = wr;
4933 goto out;
4934 }
4935 num_sge = 0;
4936 break;
4937
Sagi Grimberge6631812014-02-23 14:19:11 +02004938 case IB_WR_REG_SIG_MR:
4939 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004940 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004941
4942 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
Guy Levi34f4c952018-11-26 08:15:50 +02004943 err = set_sig_umr_wr(wr, qp, &seg, &size,
4944 &cur_edge);
Sagi Grimberge6631812014-02-23 14:19:11 +02004945 if (err) {
4946 mlx5_ib_warn(dev, "\n");
4947 *bad_wr = wr;
4948 goto out;
4949 }
4950
Guy Levi34f4c952018-11-26 08:15:50 +02004951 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
4952 wr->wr_id, nreq, fence,
4953 MLX5_OPCODE_UMR);
Sagi Grimberge6631812014-02-23 14:19:11 +02004954 /*
4955 * SET_PSV WQEs are not signaled and solicited
4956 * on error
4957 */
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004958 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
Guy Levi34f4c952018-11-26 08:15:50 +02004959 &size, &cur_edge, nreq, false,
4960 true);
Sagi Grimberge6631812014-02-23 14:19:11 +02004961 if (err) {
4962 mlx5_ib_warn(dev, "\n");
4963 err = -ENOMEM;
4964 *bad_wr = wr;
4965 goto out;
4966 }
4967
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004968 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02004969 mr->sig->psv_memory.psv_idx, &seg,
4970 &size);
4971 if (err) {
4972 mlx5_ib_warn(dev, "\n");
4973 *bad_wr = wr;
4974 goto out;
4975 }
4976
Guy Levi34f4c952018-11-26 08:15:50 +02004977 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
4978 wr->wr_id, nreq, fence,
4979 MLX5_OPCODE_SET_PSV);
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004980 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
Guy Levi34f4c952018-11-26 08:15:50 +02004981 &size, &cur_edge, nreq, false,
4982 true);
Sagi Grimberge6631812014-02-23 14:19:11 +02004983 if (err) {
4984 mlx5_ib_warn(dev, "\n");
4985 err = -ENOMEM;
4986 *bad_wr = wr;
4987 goto out;
4988 }
4989
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004990 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02004991 mr->sig->psv_wire.psv_idx, &seg,
4992 &size);
4993 if (err) {
4994 mlx5_ib_warn(dev, "\n");
4995 *bad_wr = wr;
4996 goto out;
4997 }
4998
Guy Levi34f4c952018-11-26 08:15:50 +02004999 finish_wqe(qp, ctrl, seg, size, cur_edge, idx,
5000 wr->wr_id, nreq, fence,
5001 MLX5_OPCODE_SET_PSV);
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005002 qp->next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Sagi Grimberge6631812014-02-23 14:19:11 +02005003 num_sge = 0;
5004 goto skip_psv;
5005
Eli Cohene126ba92013-07-07 17:25:49 +03005006 default:
5007 break;
5008 }
5009 break;
5010
5011 case IB_QPT_UC:
5012 switch (wr->opcode) {
5013 case IB_WR_RDMA_WRITE:
5014 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005015 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
5016 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03005017 seg += sizeof(struct mlx5_wqe_raddr_seg);
5018 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
5019 break;
5020
5021 default:
5022 break;
5023 }
5024 break;
5025
Eli Cohene126ba92013-07-07 17:25:49 +03005026 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02005027 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
5028 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
5029 err = -EPERM;
5030 *bad_wr = wr;
5031 goto out;
5032 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07005033 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02005034 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03005035 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03005036 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005037 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005038 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
5039
Eli Cohene126ba92013-07-07 17:25:49 +03005040 break;
Erez Shitritf0313962016-02-21 16:27:17 +02005041 case IB_QPT_UD:
5042 set_datagram_seg(seg, wr);
5043 seg += sizeof(struct mlx5_wqe_datagram_seg);
5044 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005045 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005046
5047 /* handle qp that supports ud offload */
5048 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
5049 struct mlx5_wqe_eth_pad *pad;
5050
5051 pad = seg;
5052 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
5053 seg += sizeof(struct mlx5_wqe_eth_pad);
5054 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005055 set_eth_seg(wr, qp, &seg, &size, &cur_edge);
5056 handle_post_send_edge(&qp->sq, &seg, size,
5057 &cur_edge);
Erez Shitritf0313962016-02-21 16:27:17 +02005058 }
5059 break;
Eli Cohene126ba92013-07-07 17:25:49 +03005060 case MLX5_IB_QPT_REG_UMR:
5061 if (wr->opcode != MLX5_IB_WR_UMR) {
5062 err = -EINVAL;
5063 mlx5_ib_warn(dev, "bad opcode\n");
5064 goto out;
5065 }
5066 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01005067 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02005068 err = set_reg_umr_segment(dev, seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
5069 if (unlikely(err))
5070 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005071 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
5072 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005073 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005074 set_reg_mkey_segment(seg, wr);
5075 seg += sizeof(struct mlx5_mkey_seg);
5076 size += sizeof(struct mlx5_mkey_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005077 handle_post_send_edge(&qp->sq, &seg, size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005078 break;
5079
5080 default:
5081 break;
5082 }
5083
5084 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
Guy Levi34f4c952018-11-26 08:15:50 +02005085 err = set_data_inl_seg(qp, wr, &seg, &size, &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005086 if (unlikely(err)) {
5087 mlx5_ib_warn(dev, "\n");
5088 *bad_wr = wr;
5089 goto out;
5090 }
Eli Cohene126ba92013-07-07 17:25:49 +03005091 } else {
Eli Cohene126ba92013-07-07 17:25:49 +03005092 for (i = 0; i < num_sge; i++) {
Guy Levi34f4c952018-11-26 08:15:50 +02005093 handle_post_send_edge(&qp->sq, &seg, size,
5094 &cur_edge);
Eli Cohene126ba92013-07-07 17:25:49 +03005095 if (likely(wr->sg_list[i].length)) {
Guy Levi34f4c952018-11-26 08:15:50 +02005096 set_data_ptr_seg
5097 ((struct mlx5_wqe_data_seg *)seg,
5098 wr->sg_list + i);
Eli Cohene126ba92013-07-07 17:25:49 +03005099 size += sizeof(struct mlx5_wqe_data_seg) / 16;
Guy Levi34f4c952018-11-26 08:15:50 +02005100 seg += sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03005101 }
5102 }
5103 }
5104
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005105 qp->next_fence = next_fence;
Guy Levi34f4c952018-11-26 08:15:50 +02005106 finish_wqe(qp, ctrl, seg, size, cur_edge, idx, wr->wr_id, nreq,
5107 fence, mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02005108skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03005109 if (0)
5110 dump_wqe(qp, idx, size);
5111 }
5112
5113out:
5114 if (likely(nreq)) {
5115 qp->sq.head += nreq;
5116
5117 /* Make sure that descriptors are written before
5118 * updating doorbell record and ringing the doorbell
5119 */
5120 wmb();
5121
5122 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
5123
Eli Cohenada388f2014-01-14 17:45:16 +02005124 /* Make sure doorbell record is visible to the HCA before
5125 * we hit doorbell */
5126 wmb();
5127
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005128 /* currently we support only regular doorbells */
Maxim Mikityanskiybbf29f62019-03-29 15:37:52 -07005129 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005130 /* Make sure doorbells don't leak out of SQ spinlock
5131 * and reach the HCA out of order.
5132 */
5133 mmiowb();
Eli Cohene126ba92013-07-07 17:25:49 +03005134 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03005135 }
5136
5137 spin_unlock_irqrestore(&qp->sq.lock, flags);
5138
5139 return err;
5140}
5141
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005142int mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
5143 const struct ib_send_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005144{
5145 return _mlx5_ib_post_send(ibqp, wr, bad_wr, false);
5146}
5147
Eli Cohene126ba92013-07-07 17:25:49 +03005148static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
5149{
5150 sig->signature = calc_sig(sig, size);
5151}
5152
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005153static int _mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5154 const struct ib_recv_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03005155{
5156 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5157 struct mlx5_wqe_data_seg *scat;
5158 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03005159 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5160 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005161 unsigned long flags;
5162 int err = 0;
5163 int nreq;
5164 int ind;
5165 int i;
5166
Parav Pandit6c755202018-08-28 14:45:29 +03005167 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
5168 !drain)) {
5169 *bad_wr = wr;
5170 return -EIO;
5171 }
5172
Haggai Erand16e91d2016-02-29 15:45:05 +02005173 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5174 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
5175
Eli Cohene126ba92013-07-07 17:25:49 +03005176 spin_lock_irqsave(&qp->rq.lock, flags);
5177
5178 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
5179
5180 for (nreq = 0; wr; nreq++, wr = wr->next) {
5181 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
5182 err = -ENOMEM;
5183 *bad_wr = wr;
5184 goto out;
5185 }
5186
5187 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
5188 err = -EINVAL;
5189 *bad_wr = wr;
5190 goto out;
5191 }
5192
Guy Levi34f4c952018-11-26 08:15:50 +02005193 scat = mlx5_frag_buf_get_wqe(&qp->rq.fbc, ind);
Eli Cohene126ba92013-07-07 17:25:49 +03005194 if (qp->wq_sig)
5195 scat++;
5196
5197 for (i = 0; i < wr->num_sge; i++)
5198 set_data_ptr_seg(scat + i, wr->sg_list + i);
5199
5200 if (i < qp->rq.max_gs) {
5201 scat[i].byte_count = 0;
5202 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
5203 scat[i].addr = 0;
5204 }
5205
5206 if (qp->wq_sig) {
5207 sig = (struct mlx5_rwqe_sig *)scat;
5208 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
5209 }
5210
5211 qp->rq.wrid[ind] = wr->wr_id;
5212
5213 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
5214 }
5215
5216out:
5217 if (likely(nreq)) {
5218 qp->rq.head += nreq;
5219
5220 /* Make sure that descriptors are written before
5221 * doorbell record.
5222 */
5223 wmb();
5224
5225 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
5226 }
5227
5228 spin_unlock_irqrestore(&qp->rq.lock, flags);
5229
5230 return err;
5231}
5232
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005233int mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
5234 const struct ib_recv_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005235{
5236 return _mlx5_ib_post_recv(ibqp, wr, bad_wr, false);
5237}
5238
Eli Cohene126ba92013-07-07 17:25:49 +03005239static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
5240{
5241 switch (mlx5_state) {
5242 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
5243 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
5244 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
5245 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
5246 case MLX5_QP_STATE_SQ_DRAINING:
5247 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
5248 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
5249 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
5250 default: return -1;
5251 }
5252}
5253
5254static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
5255{
5256 switch (mlx5_mig_state) {
5257 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
5258 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
5259 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
5260 default: return -1;
5261 }
5262}
5263
5264static int to_ib_qp_access_flags(int mlx5_flags)
5265{
5266 int ib_flags = 0;
5267
5268 if (mlx5_flags & MLX5_QP_BIT_RRE)
5269 ib_flags |= IB_ACCESS_REMOTE_READ;
5270 if (mlx5_flags & MLX5_QP_BIT_RWE)
5271 ib_flags |= IB_ACCESS_REMOTE_WRITE;
5272 if (mlx5_flags & MLX5_QP_BIT_RAE)
5273 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
5274
5275 return ib_flags;
5276}
5277
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005278static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005279 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005280 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03005281{
Eli Cohene126ba92013-07-07 17:25:49 +03005282
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005283 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03005284
Jason Gunthorpee7996a92018-01-29 13:26:40 -07005285 if (!path->port || path->port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03005286 return;
5287
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02005288 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
5289
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005290 rdma_ah_set_port_num(ah_attr, path->port);
5291 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03005292
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005293 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
5294 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
5295 rdma_ah_set_static_rate(ah_attr,
5296 path->static_rate ? path->static_rate - 5 : 0);
5297 if (path->grh_mlid & (1 << 7)) {
5298 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
5299
5300 rdma_ah_set_grh(ah_attr, NULL,
5301 tc_fl & 0xfffff,
5302 path->mgid_index,
5303 path->hop_limit,
5304 (tc_fl >> 20) & 0xff);
5305 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03005306 }
5307}
5308
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005309static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
5310 struct mlx5_ib_sq *sq,
5311 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03005312{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005313 int err;
5314
Eran Ben Elisha28160772017-12-26 15:17:05 +02005315 err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005316 if (err)
5317 goto out;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005318 sq->state = *sq_state;
5319
5320out:
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005321 return err;
5322}
5323
5324static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
5325 struct mlx5_ib_rq *rq,
5326 u8 *rq_state)
5327{
5328 void *out;
5329 void *rqc;
5330 int inlen;
5331 int err;
5332
5333 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005334 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005335 if (!out)
5336 return -ENOMEM;
5337
5338 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
5339 if (err)
5340 goto out;
5341
5342 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
5343 *rq_state = MLX5_GET(rqc, rqc, state);
5344 rq->state = *rq_state;
5345
5346out:
5347 kvfree(out);
5348 return err;
5349}
5350
5351static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
5352 struct mlx5_ib_qp *qp, u8 *qp_state)
5353{
5354 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
5355 [MLX5_RQC_STATE_RST] = {
5356 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5357 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5358 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
5359 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
5360 },
5361 [MLX5_RQC_STATE_RDY] = {
5362 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5363 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5364 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
5365 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
5366 },
5367 [MLX5_RQC_STATE_ERR] = {
5368 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5369 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5370 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
5371 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
5372 },
5373 [MLX5_RQ_STATE_NA] = {
5374 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5375 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5376 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
5377 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
5378 },
5379 };
5380
5381 *qp_state = sqrq_trans[rq_state][sq_state];
5382
5383 if (*qp_state == MLX5_QP_STATE_BAD) {
5384 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
5385 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
5386 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
5387 return -EINVAL;
5388 }
5389
5390 if (*qp_state == MLX5_QP_STATE)
5391 *qp_state = qp->state;
5392
5393 return 0;
5394}
5395
5396static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
5397 struct mlx5_ib_qp *qp,
5398 u8 *raw_packet_qp_state)
5399{
5400 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
5401 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
5402 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
5403 int err;
5404 u8 sq_state = MLX5_SQ_STATE_NA;
5405 u8 rq_state = MLX5_RQ_STATE_NA;
5406
5407 if (qp->sq.wqe_cnt) {
5408 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
5409 if (err)
5410 return err;
5411 }
5412
5413 if (qp->rq.wqe_cnt) {
5414 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
5415 if (err)
5416 return err;
5417 }
5418
5419 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
5420 raw_packet_qp_state);
5421}
5422
5423static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
5424 struct ib_qp_attr *qp_attr)
5425{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005426 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03005427 struct mlx5_qp_context *context;
5428 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005429 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03005430 int err = 0;
5431
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005432 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005433 if (!outb)
5434 return -ENOMEM;
5435
majd@mellanox.com19098df2016-01-14 19:13:03 +02005436 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005437 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03005438 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005439 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005440
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005441 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
5442 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
5443
Eli Cohene126ba92013-07-07 17:25:49 +03005444 mlx5_state = be32_to_cpu(context->flags) >> 28;
5445
5446 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03005447 qp_attr->path_mtu = context->mtu_msgmax >> 5;
5448 qp_attr->path_mig_state =
5449 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
5450 qp_attr->qkey = be32_to_cpu(context->qkey);
5451 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
5452 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
5453 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
5454 qp_attr->qp_access_flags =
5455 to_ib_qp_access_flags(be32_to_cpu(context->params2));
5456
5457 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005458 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
5459 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005460 qp_attr->alt_pkey_index =
5461 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005462 qp_attr->alt_port_num =
5463 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03005464 }
5465
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005466 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03005467 qp_attr->port_num = context->pri_path.port;
5468
5469 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
5470 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
5471
5472 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
5473
5474 qp_attr->max_dest_rd_atomic =
5475 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
5476 qp_attr->min_rnr_timer =
5477 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
5478 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
5479 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
5480 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
5481 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005482
5483out:
5484 kfree(outb);
5485 return err;
5486}
5487
Moni Shoua776a3902018-01-02 16:19:33 +02005488static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
5489 struct ib_qp_attr *qp_attr, int qp_attr_mask,
5490 struct ib_qp_init_attr *qp_init_attr)
5491{
5492 struct mlx5_core_dct *dct = &mqp->dct.mdct;
5493 u32 *out;
5494 u32 access_flags = 0;
5495 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
5496 void *dctc;
5497 int err;
5498 int supported_mask = IB_QP_STATE |
5499 IB_QP_ACCESS_FLAGS |
5500 IB_QP_PORT |
5501 IB_QP_MIN_RNR_TIMER |
5502 IB_QP_AV |
5503 IB_QP_PATH_MTU |
5504 IB_QP_PKEY_INDEX;
5505
5506 if (qp_attr_mask & ~supported_mask)
5507 return -EINVAL;
5508 if (mqp->state != IB_QPS_RTR)
5509 return -EINVAL;
5510
5511 out = kzalloc(outlen, GFP_KERNEL);
5512 if (!out)
5513 return -ENOMEM;
5514
5515 err = mlx5_core_dct_query(dev->mdev, dct, out, outlen);
5516 if (err)
5517 goto out;
5518
5519 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
5520
5521 if (qp_attr_mask & IB_QP_STATE)
5522 qp_attr->qp_state = IB_QPS_RTR;
5523
5524 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
5525 if (MLX5_GET(dctc, dctc, rre))
5526 access_flags |= IB_ACCESS_REMOTE_READ;
5527 if (MLX5_GET(dctc, dctc, rwe))
5528 access_flags |= IB_ACCESS_REMOTE_WRITE;
5529 if (MLX5_GET(dctc, dctc, rae))
5530 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
5531 qp_attr->qp_access_flags = access_flags;
5532 }
5533
5534 if (qp_attr_mask & IB_QP_PORT)
5535 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
5536 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
5537 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
5538 if (qp_attr_mask & IB_QP_AV) {
5539 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
5540 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
5541 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
5542 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
5543 }
5544 if (qp_attr_mask & IB_QP_PATH_MTU)
5545 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
5546 if (qp_attr_mask & IB_QP_PKEY_INDEX)
5547 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
5548out:
5549 kfree(out);
5550 return err;
5551}
5552
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005553int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5554 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
5555{
5556 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5557 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5558 int err = 0;
5559 u8 raw_packet_qp_state;
5560
Yishai Hadas28d61372016-05-23 15:20:56 +03005561 if (ibqp->rwq_ind_tbl)
5562 return -ENOSYS;
5563
Haggai Erand16e91d2016-02-29 15:45:05 +02005564 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5565 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
5566 qp_init_attr);
5567
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005568 /* Not all of output fields are applicable, make sure to zero them */
5569 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5570 memset(qp_attr, 0, sizeof(*qp_attr));
5571
Moni Shoua776a3902018-01-02 16:19:33 +02005572 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
5573 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
5574 qp_attr_mask, qp_init_attr);
5575
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005576 mutex_lock(&qp->mutex);
5577
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005578 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
5579 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005580 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
5581 if (err)
5582 goto out;
5583 qp->state = raw_packet_qp_state;
5584 qp_attr->port_num = 1;
5585 } else {
5586 err = query_qp_attr(dev, qp, qp_attr);
5587 if (err)
5588 goto out;
5589 }
5590
5591 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03005592 qp_attr->cur_qp_state = qp_attr->qp_state;
5593 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
5594 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
5595
5596 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005597 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005598 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005599 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005600 } else {
5601 qp_attr->cap.max_send_wr = 0;
5602 qp_attr->cap.max_send_sge = 0;
5603 }
5604
Noa Osherovich0540d812016-06-04 15:15:32 +03005605 qp_init_attr->qp_type = ibqp->qp_type;
5606 qp_init_attr->recv_cq = ibqp->recv_cq;
5607 qp_init_attr->send_cq = ibqp->send_cq;
5608 qp_init_attr->srq = ibqp->srq;
5609 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005610
5611 qp_init_attr->cap = qp_attr->cap;
5612
5613 qp_init_attr->create_flags = 0;
5614 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
5615 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
5616
Leon Romanovsky051f2632015-12-20 12:16:11 +02005617 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
5618 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
5619 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
5620 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
5621 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
5622 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02005623 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
5624 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02005625
Eli Cohene126ba92013-07-07 17:25:49 +03005626 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5627 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5628
Eli Cohene126ba92013-07-07 17:25:49 +03005629out:
5630 mutex_unlock(&qp->mutex);
5631 return err;
5632}
5633
5634struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03005635 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005636{
5637 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5638 struct mlx5_ib_xrcd *xrcd;
5639 int err;
5640
Saeed Mahameed938fe832015-05-28 22:28:41 +03005641 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005642 return ERR_PTR(-ENOSYS);
5643
5644 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5645 if (!xrcd)
5646 return ERR_PTR(-ENOMEM);
5647
Yishai Hadas5aa37712018-11-26 08:28:38 +02005648 err = mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03005649 if (err) {
5650 kfree(xrcd);
5651 return ERR_PTR(-ENOMEM);
5652 }
5653
5654 return &xrcd->ibxrcd;
5655}
5656
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005657int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03005658{
5659 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5660 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
5661 int err;
5662
Yishai Hadas5aa37712018-11-26 08:28:38 +02005663 err = mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, 0);
Leon Romanovskyb0818082018-01-28 11:25:30 +02005664 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03005665 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005666
5667 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03005668 return 0;
5669}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005670
Yishai Hadas350d0e42016-08-28 14:58:18 +03005671static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5672{
5673 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5674 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5675 struct ib_event event;
5676
5677 if (rwq->ibwq.event_handler) {
5678 event.device = rwq->ibwq.device;
5679 event.element.wq = &rwq->ibwq;
5680 switch (type) {
5681 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5682 event.event = IB_EVENT_WQ_FATAL;
5683 break;
5684 default:
5685 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5686 return;
5687 }
5688
5689 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5690 }
5691}
5692
Maor Gottlieb03404e82017-05-30 10:29:13 +03005693static int set_delay_drop(struct mlx5_ib_dev *dev)
5694{
5695 int err = 0;
5696
5697 mutex_lock(&dev->delay_drop.lock);
5698 if (dev->delay_drop.activate)
5699 goto out;
5700
5701 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
5702 if (err)
5703 goto out;
5704
5705 dev->delay_drop.activate = true;
5706out:
5707 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005708
5709 if (!err)
5710 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005711 return err;
5712}
5713
Yishai Hadas79b20a62016-05-23 15:20:50 +03005714static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5715 struct ib_wq_init_attr *init_attr)
5716{
5717 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005718 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005719 __be64 *rq_pas0;
5720 void *in;
5721 void *rqc;
5722 void *wq;
5723 int inlen;
5724 int err;
5725
5726 dev = to_mdev(pd->device);
5727
5728 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005729 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005730 if (!in)
5731 return -ENOMEM;
5732
Yishai Hadas34d57582018-09-20 21:39:21 +03005733 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005734 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
5735 MLX5_SET(rqc, rqc, mem_rq_type,
5736 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
5737 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
5738 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
5739 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
5740 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
5741 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03005742 MLX5_SET(wq, wq, wq_type,
5743 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
5744 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005745 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5746 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
5747 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
5748 err = -EOPNOTSUPP;
5749 goto out;
5750 } else {
5751 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
5752 }
5753 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005754 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03005755 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
5756 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
5757 MLX5_SET(wq, wq, log_wqe_stride_size,
5758 rwq->single_stride_log_num_of_bytes -
5759 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
5760 MLX5_SET(wq, wq, log_wqe_num_of_strides, rwq->log_num_strides -
5761 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES);
5762 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005763 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
5764 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
5765 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
5766 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
5767 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
5768 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02005769 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005770 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02005771 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005772 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
5773 err = -EOPNOTSUPP;
5774 goto out;
5775 }
5776 } else {
5777 MLX5_SET(rqc, rqc, vsd, 1);
5778 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02005779 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
5780 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
5781 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
5782 err = -EOPNOTSUPP;
5783 goto out;
5784 }
5785 MLX5_SET(rqc, rqc, scatter_fcs, 1);
5786 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03005787 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5788 if (!(dev->ib_dev.attrs.raw_packet_caps &
5789 IB_RAW_PACKET_CAP_DELAY_DROP)) {
5790 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
5791 err = -EOPNOTSUPP;
5792 goto out;
5793 }
5794 MLX5_SET(rqc, rqc, delay_drop_en, 1);
5795 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005796 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
5797 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03005798 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005799 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5800 err = set_delay_drop(dev);
5801 if (err) {
5802 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
5803 err);
5804 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
5805 } else {
5806 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
5807 }
5808 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005809out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03005810 kvfree(in);
5811 return err;
5812}
5813
5814static int set_user_rq_size(struct mlx5_ib_dev *dev,
5815 struct ib_wq_init_attr *wq_init_attr,
5816 struct mlx5_ib_create_wq *ucmd,
5817 struct mlx5_ib_rwq *rwq)
5818{
5819 /* Sanity check RQ size before proceeding */
5820 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
5821 return -EINVAL;
5822
5823 if (!ucmd->rq_wqe_count)
5824 return -EINVAL;
5825
5826 rwq->wqe_count = ucmd->rq_wqe_count;
5827 rwq->wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky0dfe4522018-08-01 14:25:41 -07005828 if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
5829 return -EINVAL;
5830
Yishai Hadas79b20a62016-05-23 15:20:50 +03005831 rwq->log_rq_stride = rwq->wqe_shift;
5832 rwq->log_rq_size = ilog2(rwq->wqe_count);
5833 return 0;
5834}
5835
5836static int prepare_user_rq(struct ib_pd *pd,
5837 struct ib_wq_init_attr *init_attr,
5838 struct ib_udata *udata,
5839 struct mlx5_ib_rwq *rwq)
5840{
5841 struct mlx5_ib_dev *dev = to_mdev(pd->device);
5842 struct mlx5_ib_create_wq ucmd = {};
5843 int err;
5844 size_t required_cmd_sz;
5845
Noa Osherovichccc87082017-10-17 18:01:13 +03005846 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
5847 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005848 if (udata->inlen < required_cmd_sz) {
5849 mlx5_ib_dbg(dev, "invalid inlen\n");
5850 return -EINVAL;
5851 }
5852
5853 if (udata->inlen > sizeof(ucmd) &&
5854 !ib_is_udata_cleared(udata, sizeof(ucmd),
5855 udata->inlen - sizeof(ucmd))) {
5856 mlx5_ib_dbg(dev, "inlen is not supported\n");
5857 return -EOPNOTSUPP;
5858 }
5859
5860 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
5861 mlx5_ib_dbg(dev, "copy failed\n");
5862 return -EFAULT;
5863 }
5864
Noa Osherovichccc87082017-10-17 18:01:13 +03005865 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03005866 mlx5_ib_dbg(dev, "invalid comp mask\n");
5867 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03005868 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
5869 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
5870 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
5871 return -EOPNOTSUPP;
5872 }
5873 if ((ucmd.single_stride_log_num_of_bytes <
5874 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
5875 (ucmd.single_stride_log_num_of_bytes >
5876 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
5877 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
5878 ucmd.single_stride_log_num_of_bytes,
5879 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
5880 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
5881 return -EINVAL;
5882 }
5883 if ((ucmd.single_wqe_log_num_of_strides >
5884 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
5885 (ucmd.single_wqe_log_num_of_strides <
5886 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES)) {
5887 mlx5_ib_dbg(dev, "Invalid log num strides (%u. Range is %u - %u)\n",
5888 ucmd.single_wqe_log_num_of_strides,
5889 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
5890 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
5891 return -EINVAL;
5892 }
5893 rwq->single_stride_log_num_of_bytes =
5894 ucmd.single_stride_log_num_of_bytes;
5895 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
5896 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
5897 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005898 }
5899
5900 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
5901 if (err) {
5902 mlx5_ib_dbg(dev, "err %d\n", err);
5903 return err;
5904 }
5905
Jason Gunthorpeb0ea0fa2019-01-09 11:15:16 +02005906 err = create_user_rq(dev, pd, udata, rwq, &ucmd);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005907 if (err) {
5908 mlx5_ib_dbg(dev, "err %d\n", err);
Gal Pressman645ba592018-10-08 19:44:03 +03005909 return err;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005910 }
5911
5912 rwq->user_index = ucmd.user_index;
5913 return 0;
5914}
5915
5916struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
5917 struct ib_wq_init_attr *init_attr,
5918 struct ib_udata *udata)
5919{
5920 struct mlx5_ib_dev *dev;
5921 struct mlx5_ib_rwq *rwq;
5922 struct mlx5_ib_create_wq_resp resp = {};
5923 size_t min_resp_len;
5924 int err;
5925
5926 if (!udata)
5927 return ERR_PTR(-ENOSYS);
5928
5929 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5930 if (udata->outlen && udata->outlen < min_resp_len)
5931 return ERR_PTR(-EINVAL);
5932
5933 dev = to_mdev(pd->device);
5934 switch (init_attr->wq_type) {
5935 case IB_WQT_RQ:
5936 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
5937 if (!rwq)
5938 return ERR_PTR(-ENOMEM);
5939 err = prepare_user_rq(pd, init_attr, udata, rwq);
5940 if (err)
5941 goto err;
5942 err = create_rq(rwq, pd, init_attr);
5943 if (err)
5944 goto err_user_rq;
5945 break;
5946 default:
5947 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
5948 init_attr->wq_type);
5949 return ERR_PTR(-EINVAL);
5950 }
5951
Yishai Hadas350d0e42016-08-28 14:58:18 +03005952 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005953 rwq->ibwq.state = IB_WQS_RESET;
5954 if (udata->outlen) {
5955 resp.response_length = offsetof(typeof(resp), response_length) +
5956 sizeof(resp.response_length);
5957 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5958 if (err)
5959 goto err_copy;
5960 }
5961
Yishai Hadas350d0e42016-08-28 14:58:18 +03005962 rwq->core_qp.event = mlx5_ib_wq_event;
5963 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005964 return &rwq->ibwq;
5965
5966err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03005967 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005968err_user_rq:
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03005969 destroy_user_rq(dev, pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005970err:
5971 kfree(rwq);
5972 return ERR_PTR(err);
5973}
5974
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005975int mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
Yishai Hadas79b20a62016-05-23 15:20:50 +03005976{
5977 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5978 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5979
Yishai Hadas350d0e42016-08-28 14:58:18 +03005980 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03005981 destroy_user_rq(dev, wq->pd, rwq, udata);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005982 kfree(rwq);
5983
5984 return 0;
5985}
5986
Yishai Hadasc5f90922016-05-23 15:20:53 +03005987struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
5988 struct ib_rwq_ind_table_init_attr *init_attr,
5989 struct ib_udata *udata)
5990{
5991 struct mlx5_ib_dev *dev = to_mdev(device);
5992 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
5993 int sz = 1 << init_attr->log_ind_tbl_size;
5994 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
5995 size_t min_resp_len;
5996 int inlen;
5997 int err;
5998 int i;
5999 u32 *in;
6000 void *rqtc;
6001
6002 if (udata->inlen > 0 &&
6003 !ib_is_udata_cleared(udata, 0,
6004 udata->inlen))
6005 return ERR_PTR(-EOPNOTSUPP);
6006
Maor Gottliebefd7f402016-10-27 16:36:40 +03006007 if (init_attr->log_ind_tbl_size >
6008 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
6009 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
6010 init_attr->log_ind_tbl_size,
6011 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
6012 return ERR_PTR(-EINVAL);
6013 }
6014
Yishai Hadasc5f90922016-05-23 15:20:53 +03006015 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
6016 if (udata->outlen && udata->outlen < min_resp_len)
6017 return ERR_PTR(-EINVAL);
6018
6019 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
6020 if (!rwq_ind_tbl)
6021 return ERR_PTR(-ENOMEM);
6022
6023 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006024 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006025 if (!in) {
6026 err = -ENOMEM;
6027 goto err;
6028 }
6029
6030 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
6031
6032 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
6033 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
6034
6035 for (i = 0; i < sz; i++)
6036 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
6037
Yishai Hadas5deba862018-09-20 21:39:28 +03006038 rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
6039 MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);
6040
Yishai Hadasc5f90922016-05-23 15:20:53 +03006041 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
6042 kvfree(in);
6043
6044 if (err)
6045 goto err;
6046
6047 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
6048 if (udata->outlen) {
6049 resp.response_length = offsetof(typeof(resp), response_length) +
6050 sizeof(resp.response_length);
6051 err = ib_copy_to_udata(udata, &resp, resp.response_length);
6052 if (err)
6053 goto err_copy;
6054 }
6055
6056 return &rwq_ind_tbl->ib_rwq_ind_tbl;
6057
6058err_copy:
Yishai Hadas5deba862018-09-20 21:39:28 +03006059 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006060err:
6061 kfree(rwq_ind_tbl);
6062 return ERR_PTR(err);
6063}
6064
6065int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
6066{
6067 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
6068 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
6069
Yishai Hadas5deba862018-09-20 21:39:28 +03006070 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03006071
6072 kfree(rwq_ind_tbl);
6073 return 0;
6074}
6075
Yishai Hadas79b20a62016-05-23 15:20:50 +03006076int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
6077 u32 wq_attr_mask, struct ib_udata *udata)
6078{
6079 struct mlx5_ib_dev *dev = to_mdev(wq->device);
6080 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
6081 struct mlx5_ib_modify_wq ucmd = {};
6082 size_t required_cmd_sz;
6083 int curr_wq_state;
6084 int wq_state;
6085 int inlen;
6086 int err;
6087 void *rqc;
6088 void *in;
6089
6090 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
6091 if (udata->inlen < required_cmd_sz)
6092 return -EINVAL;
6093
6094 if (udata->inlen > sizeof(ucmd) &&
6095 !ib_is_udata_cleared(udata, sizeof(ucmd),
6096 udata->inlen - sizeof(ucmd)))
6097 return -EOPNOTSUPP;
6098
6099 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
6100 return -EFAULT;
6101
6102 if (ucmd.comp_mask || ucmd.reserved)
6103 return -EOPNOTSUPP;
6104
6105 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03006106 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006107 if (!in)
6108 return -ENOMEM;
6109
6110 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
6111
6112 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
6113 wq_attr->curr_wq_state : wq->state;
6114 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
6115 wq_attr->wq_state : curr_wq_state;
6116 if (curr_wq_state == IB_WQS_ERR)
6117 curr_wq_state = MLX5_RQC_STATE_ERR;
6118 if (wq_state == IB_WQS_ERR)
6119 wq_state = MLX5_RQC_STATE_ERR;
6120 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
Yishai Hadas34d57582018-09-20 21:39:21 +03006121 MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006122 MLX5_SET(rqc, rqc, state, wq_state);
6123
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006124 if (wq_attr_mask & IB_WQ_FLAGS) {
6125 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
6126 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
6127 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
6128 mlx5_ib_dbg(dev, "VLAN offloads are not "
6129 "supported\n");
6130 err = -EOPNOTSUPP;
6131 goto out;
6132 }
6133 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6134 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
6135 MLX5_SET(rqc, rqc, vsd,
6136 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
6137 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02006138
6139 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
6140 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
6141 err = -EOPNOTSUPP;
6142 goto out;
6143 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006144 }
6145
Majd Dibbiny23a69642017-01-18 15:25:10 +02006146 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
6147 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
6148 MLX5_SET64(modify_rq_in, in, modify_bitmask,
6149 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03006150 MLX5_SET(rqc, rqc, counter_set_id,
6151 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02006152 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06006153 dev_info_once(
6154 &dev->ib_dev.dev,
6155 "Receive WQ counters are not supported on current FW\n");
Majd Dibbiny23a69642017-01-18 15:25:10 +02006156 }
6157
Yishai Hadas350d0e42016-08-28 14:58:18 +03006158 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006159 if (!err)
6160 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
6161
Noa Osherovichb1f74a82017-01-18 15:40:02 +02006162out:
6163 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03006164 return err;
6165}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006166
6167struct mlx5_ib_drain_cqe {
6168 struct ib_cqe cqe;
6169 struct completion done;
6170};
6171
6172static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
6173{
6174 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
6175 struct mlx5_ib_drain_cqe,
6176 cqe);
6177
6178 complete(&cqe->done);
6179}
6180
6181/* This function returns only once the drained WR was completed */
6182static void handle_drain_completion(struct ib_cq *cq,
6183 struct mlx5_ib_drain_cqe *sdrain,
6184 struct mlx5_ib_dev *dev)
6185{
6186 struct mlx5_core_dev *mdev = dev->mdev;
6187
6188 if (cq->poll_ctx == IB_POLL_DIRECT) {
6189 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
6190 ib_process_cq_direct(cq, -1);
6191 return;
6192 }
6193
6194 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6195 struct mlx5_ib_cq *mcq = to_mcq(cq);
6196 bool triggered = false;
6197 unsigned long flags;
6198
6199 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
6200 /* Make sure that the CQ handler won't run if wasn't run yet */
6201 if (!mcq->mcq.reset_notify_added)
6202 mcq->mcq.reset_notify_added = 1;
6203 else
6204 triggered = true;
6205 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
6206
6207 if (triggered) {
6208 /* Wait for any scheduled/running task to be ended */
6209 switch (cq->poll_ctx) {
6210 case IB_POLL_SOFTIRQ:
6211 irq_poll_disable(&cq->iop);
6212 irq_poll_enable(&cq->iop);
6213 break;
6214 case IB_POLL_WORKQUEUE:
6215 cancel_work_sync(&cq->work);
6216 break;
6217 default:
6218 WARN_ON_ONCE(1);
6219 }
6220 }
6221
6222 /* Run the CQ handler - this makes sure that the drain WR will
6223 * be processed if wasn't processed yet.
6224 */
6225 mcq->mcq.comp(&mcq->mcq);
6226 }
6227
6228 wait_for_completion(&sdrain->done);
6229}
6230
6231void mlx5_ib_drain_sq(struct ib_qp *qp)
6232{
6233 struct ib_cq *cq = qp->send_cq;
6234 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6235 struct mlx5_ib_drain_cqe sdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006236 const struct ib_send_wr *bad_swr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006237 struct ib_rdma_wr swr = {
6238 .wr = {
6239 .next = NULL,
6240 { .wr_cqe = &sdrain.cqe, },
6241 .opcode = IB_WR_RDMA_WRITE,
6242 },
6243 };
6244 int ret;
6245 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6246 struct mlx5_core_dev *mdev = dev->mdev;
6247
6248 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6249 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6250 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6251 return;
6252 }
6253
6254 sdrain.cqe.done = mlx5_ib_drain_qp_done;
6255 init_completion(&sdrain.done);
6256
6257 ret = _mlx5_ib_post_send(qp, &swr.wr, &bad_swr, true);
6258 if (ret) {
6259 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
6260 return;
6261 }
6262
6263 handle_drain_completion(cq, &sdrain, dev);
6264}
6265
6266void mlx5_ib_drain_rq(struct ib_qp *qp)
6267{
6268 struct ib_cq *cq = qp->recv_cq;
6269 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
6270 struct mlx5_ib_drain_cqe rdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07006271 struct ib_recv_wr rwr = {};
6272 const struct ib_recv_wr *bad_rwr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03006273 int ret;
6274 struct mlx5_ib_dev *dev = to_mdev(qp->device);
6275 struct mlx5_core_dev *mdev = dev->mdev;
6276
6277 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
6278 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
6279 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6280 return;
6281 }
6282
6283 rwr.wr_cqe = &rdrain.cqe;
6284 rdrain.cqe.done = mlx5_ib_drain_qp_done;
6285 init_completion(&rdrain.done);
6286
6287 ret = _mlx5_ib_post_recv(qp, &rwr, &bad_rwr, true);
6288 if (ret) {
6289 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
6290 return;
6291 }
6292
6293 handle_drain_completion(cq, &rdrain, dev);
6294}