blob: fa8e5dc65cb4e2ddfaeed2d79f5358394144d715 [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
111static void *get_wqe(struct mlx5_ib_qp *qp, int offset)
112{
113 return mlx5_buf_offset(&qp->buf, offset);
114}
115
116static void *get_recv_wqe(struct mlx5_ib_qp *qp, int n)
117{
118 return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
119}
120
121void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n)
122{
123 return get_wqe(qp, qp->sq.offset + (n << MLX5_IB_SQ_STRIDE));
124}
125
Haggai Eranc1395a22014-12-11 17:04:14 +0200126/**
127 * mlx5_ib_read_user_wqe() - Copy a user-space WQE to kernel space.
128 *
129 * @qp: QP to copy from.
130 * @send: copy from the send queue when non-zero, use the receive queue
131 * otherwise.
132 * @wqe_index: index to start copying from. For send work queues, the
133 * wqe_index is in units of MLX5_SEND_WQE_BB.
134 * For receive work queue, it is the number of work queue
135 * element in the queue.
136 * @buffer: destination buffer.
137 * @length: maximum number of bytes to copy.
138 *
139 * Copies at least a single WQE, but may copy more data.
140 *
141 * Return: the number of bytes copied, or an error code.
142 */
143int mlx5_ib_read_user_wqe(struct mlx5_ib_qp *qp, int send, int wqe_index,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200144 void *buffer, u32 length,
145 struct mlx5_ib_qp_base *base)
Haggai Eranc1395a22014-12-11 17:04:14 +0200146{
147 struct ib_device *ibdev = qp->ibqp.device;
148 struct mlx5_ib_dev *dev = to_mdev(ibdev);
149 struct mlx5_ib_wq *wq = send ? &qp->sq : &qp->rq;
150 size_t offset;
151 size_t wq_end;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200152 struct ib_umem *umem = base->ubuffer.umem;
Haggai Eranc1395a22014-12-11 17:04:14 +0200153 u32 first_copy_length;
154 int wqe_length;
155 int ret;
156
157 if (wq->wqe_cnt == 0) {
158 mlx5_ib_dbg(dev, "mlx5_ib_read_user_wqe for a QP with wqe_cnt == 0. qp_type: 0x%x\n",
159 qp->ibqp.qp_type);
160 return -EINVAL;
161 }
162
163 offset = wq->offset + ((wqe_index % wq->wqe_cnt) << wq->wqe_shift);
164 wq_end = wq->offset + (wq->wqe_cnt << wq->wqe_shift);
165
166 if (send && length < sizeof(struct mlx5_wqe_ctrl_seg))
167 return -EINVAL;
168
169 if (offset > umem->length ||
170 (send && offset + sizeof(struct mlx5_wqe_ctrl_seg) > umem->length))
171 return -EINVAL;
172
173 first_copy_length = min_t(u32, offset + length, wq_end) - offset;
174 ret = ib_umem_copy_from(buffer, umem, offset, first_copy_length);
175 if (ret)
176 return ret;
177
178 if (send) {
179 struct mlx5_wqe_ctrl_seg *ctrl = buffer;
180 int ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
181
182 wqe_length = ds * MLX5_WQE_DS_UNITS;
183 } else {
184 wqe_length = 1 << wq->wqe_shift;
185 }
186
187 if (wqe_length <= first_copy_length)
188 return first_copy_length;
189
190 ret = ib_umem_copy_from(buffer + first_copy_length, umem, wq->offset,
191 wqe_length - first_copy_length);
192 if (ret)
193 return ret;
194
195 return wqe_length;
196}
197
Eli Cohene126ba92013-07-07 17:25:49 +0300198static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
199{
200 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
201 struct ib_event event;
202
majd@mellanox.com19098df2016-01-14 19:13:03 +0200203 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
204 /* This event is only valid for trans_qps */
205 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
206 }
Eli Cohene126ba92013-07-07 17:25:49 +0300207
208 if (ibqp->event_handler) {
209 event.device = ibqp->device;
210 event.element.qp = ibqp;
211 switch (type) {
212 case MLX5_EVENT_TYPE_PATH_MIG:
213 event.event = IB_EVENT_PATH_MIG;
214 break;
215 case MLX5_EVENT_TYPE_COMM_EST:
216 event.event = IB_EVENT_COMM_EST;
217 break;
218 case MLX5_EVENT_TYPE_SQ_DRAINED:
219 event.event = IB_EVENT_SQ_DRAINED;
220 break;
221 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
222 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
223 break;
224 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
225 event.event = IB_EVENT_QP_FATAL;
226 break;
227 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
228 event.event = IB_EVENT_PATH_MIG_ERR;
229 break;
230 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
231 event.event = IB_EVENT_QP_REQ_ERR;
232 break;
233 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
234 event.event = IB_EVENT_QP_ACCESS_ERR;
235 break;
236 default:
237 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
238 return;
239 }
240
241 ibqp->event_handler(&event, ibqp->qp_context);
242 }
243}
244
245static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
246 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
247{
248 int wqe_size;
249 int wq_size;
250
251 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300252 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300253 return -EINVAL;
254
255 if (!has_rq) {
256 qp->rq.max_gs = 0;
257 qp->rq.wqe_cnt = 0;
258 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300259 cap->max_recv_wr = 0;
260 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300261 } else {
262 if (ucmd) {
263 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300264 if (ucmd->rq_wqe_shift > BITS_PER_BYTE * sizeof(ucmd->rq_wqe_shift))
265 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300266 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300267 if ((1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) < qp->wq_sig)
268 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300269 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
270 qp->rq.max_post = qp->rq.wqe_cnt;
271 } else {
272 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
273 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
274 wqe_size = roundup_pow_of_two(wqe_size);
275 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
276 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
277 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300278 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300279 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
280 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300281 MLX5_CAP_GEN(dev->mdev,
282 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300283 return -EINVAL;
284 }
285 qp->rq.wqe_shift = ilog2(wqe_size);
286 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
287 qp->rq.max_post = qp->rq.wqe_cnt;
288 }
289 }
290
291 return 0;
292}
293
Erez Shitritf0313962016-02-21 16:27:17 +0200294static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300295{
Andi Shyti618af382013-07-16 15:35:01 +0200296 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300297
Erez Shitritf0313962016-02-21 16:27:17 +0200298 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300299 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300300 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300301 /* fall through */
302 case IB_QPT_RC:
303 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200304 max(sizeof(struct mlx5_wqe_atomic_seg) +
305 sizeof(struct mlx5_wqe_raddr_seg),
306 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
Idan Burstein064e5262018-05-02 13:16:39 +0300307 sizeof(struct mlx5_mkey_seg) +
308 MLX5_IB_SQ_UMR_INLINE_THRESHOLD /
309 MLX5_IB_UMR_OCTOWORD);
Eli Cohene126ba92013-07-07 17:25:49 +0300310 break;
311
Eli Cohenb125a542013-09-11 16:35:22 +0300312 case IB_QPT_XRC_TGT:
313 return 0;
314
Eli Cohene126ba92013-07-07 17:25:49 +0300315 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300316 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200317 max(sizeof(struct mlx5_wqe_raddr_seg),
318 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
319 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300320 break;
321
322 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200323 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
324 size += sizeof(struct mlx5_wqe_eth_pad) +
325 sizeof(struct mlx5_wqe_eth_seg);
326 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300327 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200328 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300329 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300330 sizeof(struct mlx5_wqe_datagram_seg);
331 break;
332
333 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300334 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300335 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
336 sizeof(struct mlx5_mkey_seg);
337 break;
338
339 default:
340 return -EINVAL;
341 }
342
343 return size;
344}
345
346static int calc_send_wqe(struct ib_qp_init_attr *attr)
347{
348 int inl_size = 0;
349 int size;
350
Erez Shitritf0313962016-02-21 16:27:17 +0200351 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300352 if (size < 0)
353 return size;
354
355 if (attr->cap.max_inline_data) {
356 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
357 attr->cap.max_inline_data;
358 }
359
360 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200361 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN &&
362 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
363 return MLX5_SIG_WQE_SIZE;
364 else
365 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300366}
367
Eli Cohen288c01b2016-10-27 16:36:45 +0300368static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
369{
370 int max_sge;
371
372 if (attr->qp_type == IB_QPT_RC)
373 max_sge = (min_t(int, wqe_size, 512) -
374 sizeof(struct mlx5_wqe_ctrl_seg) -
375 sizeof(struct mlx5_wqe_raddr_seg)) /
376 sizeof(struct mlx5_wqe_data_seg);
377 else if (attr->qp_type == IB_QPT_XRC_INI)
378 max_sge = (min_t(int, wqe_size, 512) -
379 sizeof(struct mlx5_wqe_ctrl_seg) -
380 sizeof(struct mlx5_wqe_xrc_seg) -
381 sizeof(struct mlx5_wqe_raddr_seg)) /
382 sizeof(struct mlx5_wqe_data_seg);
383 else
384 max_sge = (wqe_size - sq_overhead(attr)) /
385 sizeof(struct mlx5_wqe_data_seg);
386
387 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
388 sizeof(struct mlx5_wqe_data_seg));
389}
390
Eli Cohene126ba92013-07-07 17:25:49 +0300391static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
392 struct mlx5_ib_qp *qp)
393{
394 int wqe_size;
395 int wq_size;
396
397 if (!attr->cap.max_send_wr)
398 return 0;
399
400 wqe_size = calc_send_wqe(attr);
401 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
402 if (wqe_size < 0)
403 return wqe_size;
404
Saeed Mahameed938fe832015-05-28 22:28:41 +0300405 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300406 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300407 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300408 return -EINVAL;
409 }
410
Erez Shitritf0313962016-02-21 16:27:17 +0200411 qp->max_inline_data = wqe_size - sq_overhead(attr) -
412 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300413 attr->cap.max_inline_data = qp->max_inline_data;
414
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200415 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN)
416 qp->signature_en = true;
417
Eli Cohene126ba92013-07-07 17:25:49 +0300418 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
419 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300420 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800421 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
422 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300423 qp->sq.wqe_cnt,
424 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300425 return -ENOMEM;
426 }
Eli Cohene126ba92013-07-07 17:25:49 +0300427 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300428 qp->sq.max_gs = get_send_sge(attr, wqe_size);
429 if (qp->sq.max_gs < attr->cap.max_send_sge)
430 return -ENOMEM;
431
432 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300433 qp->sq.max_post = wq_size / wqe_size;
434 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300435
436 return wq_size;
437}
438
439static int set_user_buf_size(struct mlx5_ib_dev *dev,
440 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200441 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200442 struct mlx5_ib_qp_base *base,
443 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300444{
445 int desc_sz = 1 << qp->sq.wqe_shift;
446
Saeed Mahameed938fe832015-05-28 22:28:41 +0300447 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300448 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300449 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300450 return -EINVAL;
451 }
452
453 if (ucmd->sq_wqe_count && ((1 << ilog2(ucmd->sq_wqe_count)) != ucmd->sq_wqe_count)) {
454 mlx5_ib_warn(dev, "sq_wqe_count %d, sq_wqe_count %d\n",
455 ucmd->sq_wqe_count, ucmd->sq_wqe_count);
456 return -EINVAL;
457 }
458
459 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
460
Saeed Mahameed938fe832015-05-28 22:28:41 +0300461 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300462 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300463 qp->sq.wqe_cnt,
464 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300465 return -EINVAL;
466 }
467
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300468 if (attr->qp_type == IB_QPT_RAW_PACKET ||
469 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200470 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
471 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
472 } else {
473 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
474 (qp->sq.wqe_cnt << 6);
475 }
Eli Cohene126ba92013-07-07 17:25:49 +0300476
477 return 0;
478}
479
480static int qp_has_rq(struct ib_qp_init_attr *attr)
481{
482 if (attr->qp_type == IB_QPT_XRC_INI ||
483 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
484 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
485 !attr->cap.max_recv_wr)
486 return 0;
487
488 return 1;
489}
490
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200491enum {
492 /* this is the first blue flame register in the array of bfregs assigned
493 * to a processes. Since we do not use it for blue flame but rather
494 * regular 64 bit doorbells, we do not need a lock for maintaiing
495 * "odd/even" order
496 */
497 NUM_NON_BLUE_FLAME_BFREGS = 1,
498};
499
Eli Cohenb037c292017-01-03 23:55:26 +0200500static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
501{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200502 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200503}
504
505static int num_med_bfreg(struct mlx5_ib_dev *dev,
506 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200507{
508 int n;
509
Eli Cohenb037c292017-01-03 23:55:26 +0200510 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
511 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200512
513 return n >= 0 ? n : 0;
514}
515
Yishai Hadas18b03622018-05-07 10:20:01 +0300516static int first_med_bfreg(struct mlx5_ib_dev *dev,
517 struct mlx5_bfreg_info *bfregi)
518{
519 return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
520}
521
Eli Cohenb037c292017-01-03 23:55:26 +0200522static int first_hi_bfreg(struct mlx5_ib_dev *dev,
523 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200524{
525 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200526
Eli Cohenb037c292017-01-03 23:55:26 +0200527 med = num_med_bfreg(dev, bfregi);
528 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200529}
530
Eli Cohenb037c292017-01-03 23:55:26 +0200531static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
532 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300533{
Eli Cohene126ba92013-07-07 17:25:49 +0300534 int i;
535
Eli Cohenb037c292017-01-03 23:55:26 +0200536 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
537 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200538 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300539 return i;
540 }
541 }
542
543 return -ENOMEM;
544}
545
Eli Cohenb037c292017-01-03 23:55:26 +0200546static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
547 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300548{
Yishai Hadas18b03622018-05-07 10:20:01 +0300549 int minidx = first_med_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300550 int i;
551
Yishai Hadas18b03622018-05-07 10:20:01 +0300552 if (minidx < 0)
553 return minidx;
554
555 for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200556 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300557 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200558 if (!bfregi->count[minidx])
559 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300560 }
561
Eli Cohen2f5ff262017-01-03 23:55:21 +0200562 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300563 return minidx;
564}
565
Eli Cohenb037c292017-01-03 23:55:26 +0200566static int alloc_bfreg(struct mlx5_ib_dev *dev,
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300567 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300568{
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300569 int bfregn = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +0300570
Eli Cohen2f5ff262017-01-03 23:55:21 +0200571 mutex_lock(&bfregi->lock);
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300572 if (bfregi->ver >= 2) {
573 bfregn = alloc_high_class_bfreg(dev, bfregi);
574 if (bfregn < 0)
575 bfregn = alloc_med_class_bfreg(dev, bfregi);
576 }
577
578 if (bfregn < 0) {
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200579 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200580 bfregn = 0;
581 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300582 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200583 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300584
Eli Cohen2f5ff262017-01-03 23:55:21 +0200585 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300586}
587
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200588void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300589{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200590 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200591 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200592 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300593}
594
595static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
596{
597 switch (state) {
598 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
599 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
600 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
601 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
602 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
603 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
604 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
605 default: return -1;
606 }
607}
608
609static int to_mlx5_st(enum ib_qp_type type)
610{
611 switch (type) {
612 case IB_QPT_RC: return MLX5_QP_ST_RC;
613 case IB_QPT_UC: return MLX5_QP_ST_UC;
614 case IB_QPT_UD: return MLX5_QP_ST_UD;
615 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
616 case IB_QPT_XRC_INI:
617 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
618 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200619 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200620 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300621 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300622 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200623 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300624 case IB_QPT_MAX:
625 default: return -EINVAL;
626 }
627}
628
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300629static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
630 struct mlx5_ib_cq *recv_cq);
631static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
632 struct mlx5_ib_cq *recv_cq);
633
Yishai Hadas7c043e92018-06-17 13:00:03 +0300634int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300635 struct mlx5_bfreg_info *bfregi, u32 bfregn,
Yishai Hadas7c043e92018-06-17 13:00:03 +0300636 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300637{
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300638 unsigned int bfregs_per_sys_page;
639 u32 index_of_sys_page;
640 u32 offset;
Eli Cohenb037c292017-01-03 23:55:26 +0200641
642 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
643 MLX5_NON_FP_BFREGS_PER_UAR;
644 index_of_sys_page = bfregn / bfregs_per_sys_page;
645
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200646 if (dyn_bfreg) {
647 index_of_sys_page += bfregi->num_static_sys_pages;
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300648
649 if (index_of_sys_page >= bfregi->num_sys_pages)
650 return -EINVAL;
651
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200652 if (bfregn > bfregi->num_dyn_bfregs ||
653 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
654 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
655 return -EINVAL;
656 }
657 }
Eli Cohenb037c292017-01-03 23:55:26 +0200658
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200659 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200660 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300661}
662
majd@mellanox.com19098df2016-01-14 19:13:03 +0200663static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev,
664 struct ib_pd *pd,
665 unsigned long addr, size_t size,
666 struct ib_umem **umem,
667 int *npages, int *page_shift, int *ncont,
668 u32 *offset)
669{
670 int err;
671
672 *umem = ib_umem_get(pd->uobject->context, addr, size, 0, 0);
673 if (IS_ERR(*umem)) {
674 mlx5_ib_dbg(dev, "umem_get failed\n");
675 return PTR_ERR(*umem);
676 }
677
Majd Dibbiny762f8992016-10-27 16:36:47 +0300678 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200679
680 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
681 if (err) {
682 mlx5_ib_warn(dev, "bad offset\n");
683 goto err_umem;
684 }
685
686 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
687 addr, size, *npages, *page_shift, *ncont, *offset);
688
689 return 0;
690
691err_umem:
692 ib_umem_release(*umem);
693 *umem = NULL;
694
695 return err;
696}
697
Maor Gottliebfe248c32017-05-30 10:29:14 +0300698static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
699 struct mlx5_ib_rwq *rwq)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300700{
701 struct mlx5_ib_ucontext *context;
702
Maor Gottliebfe248c32017-05-30 10:29:14 +0300703 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
704 atomic_dec(&dev->delay_drop.rqs_cnt);
705
Yishai Hadas79b20a62016-05-23 15:20:50 +0300706 context = to_mucontext(pd->uobject->context);
707 mlx5_ib_db_unmap_user(context, &rwq->db);
708 if (rwq->umem)
709 ib_umem_release(rwq->umem);
710}
711
712static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
713 struct mlx5_ib_rwq *rwq,
714 struct mlx5_ib_create_wq *ucmd)
715{
716 struct mlx5_ib_ucontext *context;
717 int page_shift = 0;
718 int npages;
719 u32 offset = 0;
720 int ncont = 0;
721 int err;
722
723 if (!ucmd->buf_addr)
724 return -EINVAL;
725
726 context = to_mucontext(pd->uobject->context);
727 rwq->umem = ib_umem_get(pd->uobject->context, ucmd->buf_addr,
728 rwq->buf_size, 0, 0);
729 if (IS_ERR(rwq->umem)) {
730 mlx5_ib_dbg(dev, "umem_get failed\n");
731 err = PTR_ERR(rwq->umem);
732 return err;
733 }
734
Majd Dibbiny762f8992016-10-27 16:36:47 +0300735 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300736 &ncont, NULL);
737 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
738 &rwq->rq_page_offset);
739 if (err) {
740 mlx5_ib_warn(dev, "bad offset\n");
741 goto err_umem;
742 }
743
744 rwq->rq_num_pas = ncont;
745 rwq->page_shift = page_shift;
746 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
747 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
748
749 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
750 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
751 npages, page_shift, ncont, offset);
752
753 err = mlx5_ib_db_map_user(context, ucmd->db_addr, &rwq->db);
754 if (err) {
755 mlx5_ib_dbg(dev, "map failed\n");
756 goto err_umem;
757 }
758
759 rwq->create_type = MLX5_WQ_USER;
760 return 0;
761
762err_umem:
763 ib_umem_release(rwq->umem);
764 return err;
765}
766
Eli Cohenb037c292017-01-03 23:55:26 +0200767static int adjust_bfregn(struct mlx5_ib_dev *dev,
768 struct mlx5_bfreg_info *bfregi, int bfregn)
769{
770 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
771 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
772}
773
Eli Cohene126ba92013-07-07 17:25:49 +0300774static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
775 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200776 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300777 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200778 struct mlx5_ib_create_qp_resp *resp, int *inlen,
779 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300780{
781 struct mlx5_ib_ucontext *context;
782 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200783 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200784 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200785 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300786 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200787 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200788 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200789 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300790 __be64 *pas;
791 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300792 int err;
793
794 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
795 if (err) {
796 mlx5_ib_dbg(dev, "copy failed\n");
797 return err;
798 }
799
800 context = to_mucontext(pd->uobject->context);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200801 if (ucmd.flags & MLX5_QP_FLAG_BFREG_INDEX) {
802 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
803 ucmd.bfreg_index, true);
804 if (uar_index < 0)
805 return uar_index;
806
807 bfregn = MLX5_IB_INVALID_BFREG;
808 } else if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL) {
809 /*
810 * TBD: should come from the verbs when we have the API
811 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200812 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200813 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200814 }
Leon Romanovsky051f2632015-12-20 12:16:11 +0200815 else {
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300816 bfregn = alloc_bfreg(dev, &context->bfregi);
817 if (bfregn < 0)
818 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300819 }
820
Eli Cohen2f5ff262017-01-03 23:55:21 +0200821 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200822 if (bfregn != MLX5_IB_INVALID_BFREG)
823 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
824 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300825
Haggai Eran48fea832014-05-22 14:50:11 +0300826 qp->rq.offset = 0;
827 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
828 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
829
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200830 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300831 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200832 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300833
majd@mellanox.com19098df2016-01-14 19:13:03 +0200834 if (ucmd.buf_addr && ubuffer->buf_size) {
835 ubuffer->buf_addr = ucmd.buf_addr;
836 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr,
837 ubuffer->buf_size,
838 &ubuffer->umem, &npages, &page_shift,
839 &ncont, &offset);
840 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200841 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200842 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200843 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300844 }
Eli Cohene126ba92013-07-07 17:25:49 +0300845
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300846 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
847 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300848 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300849 if (!*in) {
850 err = -ENOMEM;
851 goto err_umem;
852 }
Eli Cohene126ba92013-07-07 17:25:49 +0300853
Yishai Hadas991d2192018-09-20 21:39:20 +0300854 MLX5_SET(create_qp_in, *in, uid, to_mpd(pd)->uid);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300855 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
856 if (ubuffer->umem)
857 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
858
859 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
860
861 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
862 MLX5_SET(qpc, qpc, page_offset, offset);
863
864 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200865 if (bfregn != MLX5_IB_INVALID_BFREG)
866 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
867 else
868 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200869 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300870
871 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
872 if (err) {
873 mlx5_ib_dbg(dev, "map failed\n");
874 goto err_free;
875 }
876
Jason Gunthorpe41d902c2018-04-03 10:00:53 +0300877 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
Eli Cohene126ba92013-07-07 17:25:49 +0300878 if (err) {
879 mlx5_ib_dbg(dev, "copy failed\n");
880 goto err_unmap;
881 }
882 qp->create_type = MLX5_QP_USER;
883
884 return 0;
885
886err_unmap:
887 mlx5_ib_db_unmap_user(context, &qp->db);
888
889err_free:
Al Viro479163f2014-11-20 08:13:57 +0000890 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300891
892err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200893 if (ubuffer->umem)
894 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300895
Eli Cohen2f5ff262017-01-03 23:55:21 +0200896err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200897 if (bfregn != MLX5_IB_INVALID_BFREG)
898 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300899 return err;
900}
901
Eli Cohenb037c292017-01-03 23:55:26 +0200902static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
903 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300904{
905 struct mlx5_ib_ucontext *context;
906
907 context = to_mucontext(pd->uobject->context);
908 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200909 if (base->ubuffer.umem)
910 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200911
912 /*
913 * Free only the BFREGs which are handled by the kernel.
914 * BFREGs of UARs allocated dynamically are handled by user.
915 */
916 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
917 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300918}
919
920static int create_kernel_qp(struct mlx5_ib_dev *dev,
921 struct ib_qp_init_attr *init_attr,
922 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300923 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200924 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300925{
Eli Cohene126ba92013-07-07 17:25:49 +0300926 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300927 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300928 int err;
929
Erez Shitritf0313962016-02-21 16:27:17 +0200930 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
931 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200932 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +0300933 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200934 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200935 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300936
937 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200938 qp->bf.bfreg = &dev->fp_bfreg;
939 else
940 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300941
Eli Cohend8030b02017-02-09 19:31:47 +0200942 /* We need to divide by two since each register is comprised of
943 * two buffers of identical size, namely odd and even
944 */
945 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200946 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +0300947
948 err = calc_sq_size(dev, init_attr, qp);
949 if (err < 0) {
950 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200951 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300952 }
953
954 qp->rq.offset = 0;
955 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200956 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300957
majd@mellanox.com19098df2016-01-14 19:13:03 +0200958 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300959 if (err) {
960 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200961 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300962 }
963
964 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300965 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
966 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300967 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300968 if (!*in) {
969 err = -ENOMEM;
970 goto err_buf;
971 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300972
973 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
974 MLX5_SET(qpc, qpc, uar_page, uar_index);
975 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
976
Eli Cohene126ba92013-07-07 17:25:49 +0300977 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300978 MLX5_SET(qpc, qpc, fre, 1);
979 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300980
Haggai Eranb11a4f92016-02-29 15:45:03 +0200981 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300982 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200983 qp->flags |= MLX5_IB_QP_SQPN_QP1;
984 }
985
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300986 mlx5_fill_page_array(&qp->buf,
987 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300988
Jack Morgenstein9603b612014-07-28 23:30:22 +0300989 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300990 if (err) {
991 mlx5_ib_dbg(dev, "err %d\n", err);
992 goto err_free;
993 }
994
Li Dongyangb5883002017-08-16 23:31:22 +1000995 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
996 sizeof(*qp->sq.wrid), GFP_KERNEL);
997 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
998 sizeof(*qp->sq.wr_data), GFP_KERNEL);
999 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1000 sizeof(*qp->rq.wrid), GFP_KERNEL);
1001 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1002 sizeof(*qp->sq.w_list), GFP_KERNEL);
1003 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1004 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001005
1006 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1007 !qp->sq.w_list || !qp->sq.wqe_head) {
1008 err = -ENOMEM;
1009 goto err_wrid;
1010 }
1011 qp->create_type = MLX5_QP_KERNEL;
1012
1013 return 0;
1014
1015err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001016 kvfree(qp->sq.wqe_head);
1017 kvfree(qp->sq.w_list);
1018 kvfree(qp->sq.wrid);
1019 kvfree(qp->sq.wr_data);
1020 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001021 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001022
1023err_free:
Al Viro479163f2014-11-20 08:13:57 +00001024 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001025
1026err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001027 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001028 return err;
1029}
1030
1031static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1032{
Li Dongyangb5883002017-08-16 23:31:22 +10001033 kvfree(qp->sq.wqe_head);
1034 kvfree(qp->sq.w_list);
1035 kvfree(qp->sq.wrid);
1036 kvfree(qp->sq.wr_data);
1037 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001038 mlx5_db_free(dev->mdev, &qp->db);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001039 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001040}
1041
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001042static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001043{
1044 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Moni Shouac32a4f22018-01-02 16:19:32 +02001045 (attr->qp_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001046 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001047 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001048 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001049 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001050 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001051 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001052}
1053
1054static int is_connected(enum ib_qp_type qp_type)
1055{
1056 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1057 return 1;
1058
1059 return 0;
1060}
1061
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001062static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001063 struct mlx5_ib_qp *qp,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001064 struct mlx5_ib_sq *sq, u32 tdn,
1065 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001066{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001067 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001068 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1069
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001070 MLX5_SET(create_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001071 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001072 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1073 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1074
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001075 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1076}
1077
1078static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001079 struct mlx5_ib_sq *sq, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001080{
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001081 mlx5_cmd_destroy_tis(dev->mdev, sq->tisn, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001082}
1083
Mark Blochb96c9dd2018-01-29 10:40:37 +00001084static void destroy_flow_rule_vport_sq(struct mlx5_ib_dev *dev,
1085 struct mlx5_ib_sq *sq)
1086{
1087 if (sq->flow_rule)
1088 mlx5_del_flow_rules(sq->flow_rule);
1089}
1090
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001091static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1092 struct mlx5_ib_sq *sq, void *qpin,
1093 struct ib_pd *pd)
1094{
1095 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1096 __be64 *pas;
1097 void *in;
1098 void *sqc;
1099 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1100 void *wq;
1101 int inlen;
1102 int err;
1103 int page_shift = 0;
1104 int npages;
1105 int ncont = 0;
1106 u32 offset = 0;
1107
1108 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1109 &sq->ubuffer.umem, &npages, &page_shift,
1110 &ncont, &offset);
1111 if (err)
1112 return err;
1113
1114 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001115 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001116 if (!in) {
1117 err = -ENOMEM;
1118 goto err_umem;
1119 }
1120
Yishai Hadasc14003f2018-09-20 21:39:22 +03001121 MLX5_SET(create_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001122 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1123 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001124 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1125 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001126 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1127 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1128 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1129 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1130 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001131 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1132 MLX5_CAP_ETH(dev->mdev, swp))
1133 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001134
1135 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1136 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1137 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1138 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1139 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1140 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1141 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1142 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1143 MLX5_SET(wq, wq, page_offset, offset);
1144
1145 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1146 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1147
1148 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1149
1150 kvfree(in);
1151
1152 if (err)
1153 goto err_umem;
1154
Mark Blochb96c9dd2018-01-29 10:40:37 +00001155 err = create_flow_rule_vport_sq(dev, sq);
1156 if (err)
1157 goto err_flow;
1158
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001159 return 0;
1160
Mark Blochb96c9dd2018-01-29 10:40:37 +00001161err_flow:
1162 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1163
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001164err_umem:
1165 ib_umem_release(sq->ubuffer.umem);
1166 sq->ubuffer.umem = NULL;
1167
1168 return err;
1169}
1170
1171static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1172 struct mlx5_ib_sq *sq)
1173{
Mark Blochb96c9dd2018-01-29 10:40:37 +00001174 destroy_flow_rule_vport_sq(dev, sq);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001175 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1176 ib_umem_release(sq->ubuffer.umem);
1177}
1178
Boris Pismenny2c292db2018-03-08 15:51:40 +02001179static size_t get_rq_pas_size(void *qpc)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001180{
1181 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1182 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1183 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1184 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1185 u32 po_quanta = 1 << (log_page_size - 6);
1186 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1187 u32 page_size = 1 << log_page_size;
1188 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1189 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1190
1191 return rq_num_pas * sizeof(u64);
1192}
1193
1194static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001195 struct mlx5_ib_rq *rq, void *qpin,
Yishai Hadas34d57582018-09-20 21:39:21 +03001196 size_t qpinlen, struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001197{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001198 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001199 __be64 *pas;
1200 __be64 *qp_pas;
1201 void *in;
1202 void *rqc;
1203 void *wq;
1204 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001205 size_t rq_pas_size = get_rq_pas_size(qpc);
1206 size_t inlen;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001207 int err;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001208
1209 if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
1210 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001211
1212 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001213 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001214 if (!in)
1215 return -ENOMEM;
1216
Yishai Hadas34d57582018-09-20 21:39:21 +03001217 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001218 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001219 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1220 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001221 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1222 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1223 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1224 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1225 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1226
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001227 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1228 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1229
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001230 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1231 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001232 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1233 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001234 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1235 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1236 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1237 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1238 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1239 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1240
1241 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1242 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1243 memcpy(pas, qp_pas, rq_pas_size);
1244
1245 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1246
1247 kvfree(in);
1248
1249 return err;
1250}
1251
1252static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1253 struct mlx5_ib_rq *rq)
1254{
1255 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1256}
1257
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001258static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1259{
1260 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1261 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1262 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1263}
1264
Mark Bloch0042f9e2018-09-17 13:30:49 +03001265static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1266 struct mlx5_ib_rq *rq,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001267 u32 qp_flags_en,
1268 struct ib_pd *pd)
Mark Bloch0042f9e2018-09-17 13:30:49 +03001269{
1270 if (qp_flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1271 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1272 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001273 mlx5_cmd_destroy_tir(dev->mdev, rq->tirn, to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001274}
1275
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001276static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001277 struct mlx5_ib_rq *rq, u32 tdn,
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001278 u32 *qp_flags_en,
1279 struct ib_pd *pd)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001280{
Mark Bloch175edba2018-09-17 13:30:48 +03001281 u8 lb_flag = 0;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001282 u32 *in;
1283 void *tirc;
1284 int inlen;
1285 int err;
1286
1287 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001288 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001289 if (!in)
1290 return -ENOMEM;
1291
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001292 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001293 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1294 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1295 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1296 MLX5_SET(tirc, tirc, transport_domain, tdn);
Mark Bloch175edba2018-09-17 13:30:48 +03001297 if (*qp_flags_en & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001298 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001299
Mark Bloch175edba2018-09-17 13:30:48 +03001300 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
1301 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1302
1303 if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
1304 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1305
1306 if (dev->rep) {
1307 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1308 *qp_flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1309 }
1310
1311 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
Mark Blochec9c2fb2018-01-15 13:11:37 +00001312
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001313 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1314
Mark Bloch0042f9e2018-09-17 13:30:49 +03001315 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1316 err = mlx5_ib_enable_lb(dev, false, true);
1317
1318 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001319 destroy_raw_packet_qp_tir(dev, rq, 0, pd);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001320 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001321 kvfree(in);
1322
1323 return err;
1324}
1325
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001326static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001327 u32 *in, size_t inlen,
Yishai Hadas7f720522018-09-20 21:45:18 +03001328 struct ib_pd *pd,
1329 struct ib_udata *udata,
1330 struct mlx5_ib_create_qp_resp *resp)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001331{
1332 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1333 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1334 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1335 struct ib_uobject *uobj = pd->uobject;
1336 struct ib_ucontext *ucontext = uobj->context;
1337 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1338 int err;
1339 u32 tdn = mucontext->tdn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001340 u16 uid = to_mpd(pd)->uid;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001341
1342 if (qp->sq.wqe_cnt) {
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001343 err = create_raw_packet_qp_tis(dev, qp, sq, tdn, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001344 if (err)
1345 return err;
1346
1347 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1348 if (err)
1349 goto err_destroy_tis;
1350
Yishai Hadas7f720522018-09-20 21:45:18 +03001351 if (uid) {
1352 resp->tisn = sq->tisn;
1353 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TISN;
1354 resp->sqn = sq->base.mqp.qpn;
1355 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_SQN;
1356 }
1357
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001358 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001359 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001360 }
1361
1362 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001363 rq->base.container_mibqp = qp;
1364
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001365 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1366 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001367 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1368 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
Yishai Hadas34d57582018-09-20 21:39:21 +03001369 err = create_raw_packet_qp_rq(dev, rq, in, inlen, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001370 if (err)
1371 goto err_destroy_sq;
1372
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001373 err = create_raw_packet_qp_tir(dev, rq, tdn, &qp->flags_en, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001374 if (err)
1375 goto err_destroy_rq;
Yishai Hadas7f720522018-09-20 21:45:18 +03001376
1377 if (uid) {
1378 resp->rqn = rq->base.mqp.qpn;
1379 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_RQN;
1380 resp->tirn = rq->tirn;
1381 resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1382 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001383 }
1384
1385 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1386 rq->base.mqp.qpn;
Yishai Hadas7f720522018-09-20 21:45:18 +03001387 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
1388 if (err)
1389 goto err_destroy_tir;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001390
1391 return 0;
1392
Yishai Hadas7f720522018-09-20 21:45:18 +03001393err_destroy_tir:
1394 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001395err_destroy_rq:
1396 destroy_raw_packet_qp_rq(dev, rq);
1397err_destroy_sq:
1398 if (!qp->sq.wqe_cnt)
1399 return err;
1400 destroy_raw_packet_qp_sq(dev, sq);
1401err_destroy_tis:
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001402 destroy_raw_packet_qp_tis(dev, sq, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001403
1404 return err;
1405}
1406
1407static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1408 struct mlx5_ib_qp *qp)
1409{
1410 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1411 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1412 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1413
1414 if (qp->rq.wqe_cnt) {
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001415 destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001416 destroy_raw_packet_qp_rq(dev, rq);
1417 }
1418
1419 if (qp->sq.wqe_cnt) {
1420 destroy_raw_packet_qp_sq(dev, sq);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03001421 destroy_raw_packet_qp_tis(dev, sq, qp->ibqp.pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001422 }
1423}
1424
1425static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1426 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1427{
1428 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1429 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1430
1431 sq->sq = &qp->sq;
1432 rq->rq = &qp->rq;
1433 sq->doorbell = &qp->db;
1434 rq->doorbell = &qp->db;
1435}
1436
Yishai Hadas28d61372016-05-23 15:20:56 +03001437static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1438{
Mark Bloch0042f9e2018-09-17 13:30:49 +03001439 if (qp->flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1440 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
1441 mlx5_ib_disable_lb(dev, false, true);
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001442 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1443 to_mpd(qp->ibqp.pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001444}
1445
1446static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1447 struct ib_pd *pd,
1448 struct ib_qp_init_attr *init_attr,
1449 struct ib_udata *udata)
1450{
1451 struct ib_uobject *uobj = pd->uobject;
1452 struct ib_ucontext *ucontext = uobj->context;
1453 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1454 struct mlx5_ib_create_qp_resp resp = {};
1455 int inlen;
1456 int err;
1457 u32 *in;
1458 void *tirc;
1459 void *hfso;
1460 u32 selected_fields = 0;
Matan Barak2d93fc82018-03-28 09:27:55 +03001461 u32 outer_l4;
Yishai Hadas28d61372016-05-23 15:20:56 +03001462 size_t min_resp_len;
1463 u32 tdn = mucontext->tdn;
1464 struct mlx5_ib_create_qp_rss ucmd = {};
1465 size_t required_cmd_sz;
Mark Bloch175edba2018-09-17 13:30:48 +03001466 u8 lb_flag = 0;
Yishai Hadas28d61372016-05-23 15:20:56 +03001467
1468 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1469 return -EOPNOTSUPP;
1470
1471 if (init_attr->create_flags || init_attr->send_cq)
1472 return -EINVAL;
1473
Eli Cohen2f5ff262017-01-03 23:55:21 +02001474 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001475 if (udata->outlen < min_resp_len)
1476 return -EINVAL;
1477
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001478 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001479 if (udata->inlen < required_cmd_sz) {
1480 mlx5_ib_dbg(dev, "invalid inlen\n");
1481 return -EINVAL;
1482 }
1483
1484 if (udata->inlen > sizeof(ucmd) &&
1485 !ib_is_udata_cleared(udata, sizeof(ucmd),
1486 udata->inlen - sizeof(ucmd))) {
1487 mlx5_ib_dbg(dev, "inlen is not supported\n");
1488 return -EOPNOTSUPP;
1489 }
1490
1491 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1492 mlx5_ib_dbg(dev, "copy failed\n");
1493 return -EFAULT;
1494 }
1495
1496 if (ucmd.comp_mask) {
1497 mlx5_ib_dbg(dev, "invalid comp mask\n");
1498 return -EOPNOTSUPP;
1499 }
1500
Mark Bloch175edba2018-09-17 13:30:48 +03001501 if (ucmd.flags & ~(MLX5_QP_FLAG_TUNNEL_OFFLOADS |
1502 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
1503 MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)) {
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001504 mlx5_ib_dbg(dev, "invalid flags\n");
1505 return -EOPNOTSUPP;
1506 }
1507
1508 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1509 !tunnel_offload_supported(dev->mdev)) {
1510 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001511 return -EOPNOTSUPP;
1512 }
1513
Maor Gottlieb309fa342017-10-19 08:25:56 +03001514 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1515 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1516 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1517 return -EOPNOTSUPP;
1518 }
1519
Mark Bloch175edba2018-09-17 13:30:48 +03001520 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC || dev->rep) {
1521 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
1522 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1523 }
1524
1525 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
1526 lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;
1527 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
1528 }
1529
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001530 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
Yishai Hadas28d61372016-05-23 15:20:56 +03001531 if (err) {
1532 mlx5_ib_dbg(dev, "copy failed\n");
1533 return -EINVAL;
1534 }
1535
1536 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001537 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001538 if (!in)
1539 return -ENOMEM;
1540
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001541 MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001542 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1543 MLX5_SET(tirc, tirc, disp_type,
1544 MLX5_TIRC_DISP_TYPE_INDIRECT);
1545 MLX5_SET(tirc, tirc, indirect_table,
1546 init_attr->rwq_ind_tbl->ind_tbl_num);
1547 MLX5_SET(tirc, tirc, transport_domain, tdn);
1548
1549 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001550
1551 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1552 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1553
Mark Bloch175edba2018-09-17 13:30:48 +03001554 MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
1555
Maor Gottlieb309fa342017-10-19 08:25:56 +03001556 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1557 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1558 else
1559 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1560
Yishai Hadas28d61372016-05-23 15:20:56 +03001561 switch (ucmd.rx_hash_function) {
1562 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1563 {
1564 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1565 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1566
1567 if (len != ucmd.rx_key_len) {
1568 err = -EINVAL;
1569 goto err;
1570 }
1571
1572 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1573 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1574 memcpy(rss_key, ucmd.rx_hash_key, len);
1575 break;
1576 }
1577 default:
1578 err = -EOPNOTSUPP;
1579 goto err;
1580 }
1581
1582 if (!ucmd.rx_hash_fields_mask) {
1583 /* special case when this TIR serves as steering entry without hashing */
1584 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1585 goto create_tir;
1586 err = -EINVAL;
1587 goto err;
1588 }
1589
1590 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1591 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1592 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1593 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1594 err = -EINVAL;
1595 goto err;
1596 }
1597
1598 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1599 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1600 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1601 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1602 MLX5_L3_PROT_TYPE_IPV4);
1603 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1604 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1605 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1606 MLX5_L3_PROT_TYPE_IPV6);
1607
Matan Barak2d93fc82018-03-28 09:27:55 +03001608 outer_l4 = ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1609 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) << 0 |
1610 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1611 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP)) << 1 |
1612 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI) << 2;
1613
1614 /* Check that only one l4 protocol is set */
1615 if (outer_l4 & (outer_l4 - 1)) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001616 err = -EINVAL;
1617 goto err;
1618 }
1619
1620 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1621 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1622 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1623 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1624 MLX5_L4_PROT_TYPE_TCP);
1625 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1626 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1627 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1628 MLX5_L4_PROT_TYPE_UDP);
1629
1630 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1631 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1632 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1633
1634 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1635 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1636 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1637
1638 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1639 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1640 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1641
1642 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1643 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1644 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1645
Matan Barak2d93fc82018-03-28 09:27:55 +03001646 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI)
1647 selected_fields |= MLX5_HASH_FIELD_SEL_IPSEC_SPI;
1648
Yishai Hadas28d61372016-05-23 15:20:56 +03001649 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1650
1651create_tir:
1652 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1653
Mark Bloch0042f9e2018-09-17 13:30:49 +03001654 if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
1655 err = mlx5_ib_enable_lb(dev, false, true);
1656
1657 if (err)
Yishai Hadas443c1cf2018-09-20 21:39:26 +03001658 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
1659 to_mpd(pd)->uid);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001660 }
1661
Yishai Hadas28d61372016-05-23 15:20:56 +03001662 if (err)
1663 goto err;
1664
Yishai Hadas7f720522018-09-20 21:45:18 +03001665 if (mucontext->devx_uid) {
1666 resp.comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1667 resp.tirn = qp->rss_qp.tirn;
1668 }
1669
1670 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
1671 if (err)
1672 goto err_copy;
1673
Yishai Hadas28d61372016-05-23 15:20:56 +03001674 kvfree(in);
1675 /* qpn is reserved for that QP */
1676 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001677 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001678 return 0;
1679
Yishai Hadas7f720522018-09-20 21:45:18 +03001680err_copy:
1681 mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn, mucontext->devx_uid);
Yishai Hadas28d61372016-05-23 15:20:56 +03001682err:
1683 kvfree(in);
1684 return err;
1685}
1686
Eli Cohene126ba92013-07-07 17:25:49 +03001687static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1688 struct ib_qp_init_attr *init_attr,
1689 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1690{
1691 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001692 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001693 struct mlx5_core_dev *mdev = dev->mdev;
Jason Gunthorpe0625b4b2018-08-14 15:33:52 -06001694 struct mlx5_ib_create_qp_resp resp = {};
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001695 struct mlx5_ib_cq *send_cq;
1696 struct mlx5_ib_cq *recv_cq;
1697 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001698 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001699 struct mlx5_ib_create_qp ucmd;
1700 struct mlx5_ib_qp_base *base;
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001701 int mlx5_st;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001702 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001703 u32 *in;
1704 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001705
1706 mutex_init(&qp->mutex);
1707 spin_lock_init(&qp->sq.lock);
1708 spin_lock_init(&qp->rq.lock);
1709
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001710 mlx5_st = to_mlx5_st(init_attr->qp_type);
1711 if (mlx5_st < 0)
1712 return -EINVAL;
1713
Yishai Hadas28d61372016-05-23 15:20:56 +03001714 if (init_attr->rwq_ind_tbl) {
1715 if (!udata)
1716 return -ENOSYS;
1717
1718 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1719 return err;
1720 }
1721
Eli Cohenf360d882014-04-02 00:10:16 +03001722 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001723 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001724 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1725 return -EINVAL;
1726 } else {
1727 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1728 }
1729 }
1730
Leon Romanovsky051f2632015-12-20 12:16:11 +02001731 if (init_attr->create_flags &
1732 (IB_QP_CREATE_CROSS_CHANNEL |
1733 IB_QP_CREATE_MANAGED_SEND |
1734 IB_QP_CREATE_MANAGED_RECV)) {
1735 if (!MLX5_CAP_GEN(mdev, cd)) {
1736 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1737 return -EINVAL;
1738 }
1739 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1740 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1741 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1742 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1743 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1744 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1745 }
Erez Shitritf0313962016-02-21 16:27:17 +02001746
1747 if (init_attr->qp_type == IB_QPT_UD &&
1748 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1749 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1750 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1751 return -EOPNOTSUPP;
1752 }
1753
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001754 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1755 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1756 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1757 return -EOPNOTSUPP;
1758 }
1759 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1760 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1761 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1762 return -EOPNOTSUPP;
1763 }
1764 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1765 }
1766
Eli Cohene126ba92013-07-07 17:25:49 +03001767 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1768 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1769
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001770 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
1771 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1772 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
1773 (init_attr->qp_type != IB_QPT_RAW_PACKET))
1774 return -EOPNOTSUPP;
1775 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
1776 }
1777
Eli Cohene126ba92013-07-07 17:25:49 +03001778 if (pd && pd->uobject) {
1779 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1780 mlx5_ib_dbg(dev, "copy failed\n");
1781 return -EFAULT;
1782 }
1783
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001784 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1785 &ucmd, udata->inlen, &uidx);
1786 if (err)
1787 return err;
1788
Eli Cohene126ba92013-07-07 17:25:49 +03001789 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1790 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001791 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
1792 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
1793 !tunnel_offload_supported(mdev)) {
1794 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
1795 return -EOPNOTSUPP;
1796 }
Mark Bloch175edba2018-09-17 13:30:48 +03001797 qp->flags_en |= MLX5_QP_FLAG_TUNNEL_OFFLOADS;
1798 }
1799
1800 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC) {
1801 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1802 mlx5_ib_dbg(dev, "Self-LB UC isn't supported\n");
1803 return -EOPNOTSUPP;
1804 }
1805 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
1806 }
1807
1808 if (ucmd.flags & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC) {
1809 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1810 mlx5_ib_dbg(dev, "Self-LB UM isn't supported\n");
1811 return -EOPNOTSUPP;
1812 }
1813 qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001814 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001815
1816 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
1817 if (init_attr->qp_type != IB_QPT_UD ||
1818 (MLX5_CAP_GEN(dev->mdev, port_type) !=
1819 MLX5_CAP_PORT_TYPE_IB) ||
1820 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
1821 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
1822 return -EOPNOTSUPP;
1823 }
1824
1825 qp->flags |= MLX5_IB_QP_UNDERLAY;
1826 qp->underlay_qpn = init_attr->source_qpn;
1827 }
Eli Cohene126ba92013-07-07 17:25:49 +03001828 } else {
1829 qp->wq_sig = !!wq_signature;
1830 }
1831
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001832 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1833 qp->flags & MLX5_IB_QP_UNDERLAY) ?
1834 &qp->raw_packet_qp.rq.base :
1835 &qp->trans_qp.base;
1836
Eli Cohene126ba92013-07-07 17:25:49 +03001837 qp->has_rq = qp_has_rq(init_attr);
1838 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1839 qp, (pd && pd->uobject) ? &ucmd : NULL);
1840 if (err) {
1841 mlx5_ib_dbg(dev, "err %d\n", err);
1842 return err;
1843 }
1844
1845 if (pd) {
1846 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001847 __u32 max_wqes =
1848 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001849 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1850 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1851 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1852 mlx5_ib_dbg(dev, "invalid rq params\n");
1853 return -EINVAL;
1854 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001855 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001856 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001857 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001858 return -EINVAL;
1859 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001860 if (init_attr->create_flags &
1861 mlx5_ib_create_qp_sqpn_qp1()) {
1862 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1863 return -EINVAL;
1864 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001865 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1866 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001867 if (err)
1868 mlx5_ib_dbg(dev, "err %d\n", err);
1869 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001870 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1871 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001872 if (err)
1873 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001874 }
1875
1876 if (err)
1877 return err;
1878 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001879 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001880 if (!in)
1881 return -ENOMEM;
1882
1883 qp->create_type = MLX5_QP_EMPTY;
1884 }
1885
1886 if (is_sqp(init_attr->qp_type))
1887 qp->port = init_attr->port_num;
1888
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001889 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1890
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001891 MLX5_SET(qpc, qpc, st, mlx5_st);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001892 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001893
1894 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001895 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001896 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001897 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1898
Eli Cohene126ba92013-07-07 17:25:49 +03001899
1900 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001901 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001902
Eli Cohenf360d882014-04-02 00:10:16 +03001903 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001904 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001905
Leon Romanovsky051f2632015-12-20 12:16:11 +02001906 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001907 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001908 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001909 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001910 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001911 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001912
Eli Cohene126ba92013-07-07 17:25:49 +03001913 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1914 int rcqe_sz;
1915 int scqe_sz;
1916
1917 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1918 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1919
1920 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001921 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001922 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001923 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001924
1925 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1926 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001927 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001928 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001929 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001930 }
1931 }
1932
1933 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001934 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1935 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001936 }
1937
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001938 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001939
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001940 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001941 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001942 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001943 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001944 if (init_attr->srq &&
1945 init_attr->srq->srq_type == IB_SRQT_TM)
1946 MLX5_SET(qpc, qpc, offload_type,
1947 MLX5_QPC_OFFLOAD_TYPE_RNDV);
1948 }
Eli Cohene126ba92013-07-07 17:25:49 +03001949
1950 /* Set default resources */
1951 switch (init_attr->qp_type) {
1952 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001953 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1954 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1955 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1956 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001957 break;
1958 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001959 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1960 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1961 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001962 break;
1963 default:
1964 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001965 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1966 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001967 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001968 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1969 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001970 }
1971 }
1972
1973 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001974 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001975
1976 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001977 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001978
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001979 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001980
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001981 /* 0xffffff means we ask to work with cqe version 0 */
1982 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001983 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001984
Erez Shitritf0313962016-02-21 16:27:17 +02001985 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1986 if (init_attr->qp_type == IB_QPT_UD &&
1987 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001988 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1989 qp->flags |= MLX5_IB_QP_LSO;
1990 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001991
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001992 if (init_attr->create_flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
1993 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
1994 mlx5_ib_dbg(dev, "scatter end padding is not supported\n");
1995 err = -EOPNOTSUPP;
1996 goto err;
1997 } else if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1998 MLX5_SET(qpc, qpc, end_padding_mode,
1999 MLX5_WQ_END_PAD_MODE_ALIGN);
2000 } else {
2001 qp->flags |= MLX5_IB_QP_PCI_WRITE_END_PADDING;
2002 }
2003 }
2004
Boris Pismenny2c292db2018-03-08 15:51:40 +02002005 if (inlen < 0) {
2006 err = -EINVAL;
2007 goto err;
2008 }
2009
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002010 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2011 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002012 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
2013 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
Yishai Hadas7f720522018-09-20 21:45:18 +03002014 err = create_raw_packet_qp(dev, qp, in, inlen, pd, udata,
2015 &resp);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002016 } else {
2017 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
2018 }
2019
Eli Cohene126ba92013-07-07 17:25:49 +03002020 if (err) {
2021 mlx5_ib_dbg(dev, "create qp failed\n");
2022 goto err_create;
2023 }
2024
Al Viro479163f2014-11-20 08:13:57 +00002025 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002026
majd@mellanox.com19098df2016-01-14 19:13:03 +02002027 base->container_mibqp = qp;
2028 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03002029
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002030 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
2031 &send_cq, &recv_cq);
2032 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2033 mlx5_ib_lock_cqs(send_cq, recv_cq);
2034 /* Maintain device to QPs access, needed for further handling via reset
2035 * flow
2036 */
2037 list_add_tail(&qp->qps_list, &dev->qp_list);
2038 /* Maintain CQ to QPs access, needed for further handling via reset flow
2039 */
2040 if (send_cq)
2041 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
2042 if (recv_cq)
2043 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
2044 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2045 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
2046
Eli Cohene126ba92013-07-07 17:25:49 +03002047 return 0;
2048
2049err_create:
2050 if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02002051 destroy_qp_user(dev, pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002052 else if (qp->create_type == MLX5_QP_KERNEL)
2053 destroy_qp_kernel(dev, qp);
2054
Noa Osherovichb1383aa2017-10-29 13:59:45 +02002055err:
Al Viro479163f2014-11-20 08:13:57 +00002056 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03002057 return err;
2058}
2059
2060static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2061 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
2062{
2063 if (send_cq) {
2064 if (recv_cq) {
2065 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002066 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002067 spin_lock_nested(&recv_cq->lock,
2068 SINGLE_DEPTH_NESTING);
2069 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002070 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002071 __acquire(&recv_cq->lock);
2072 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002073 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002074 spin_lock_nested(&send_cq->lock,
2075 SINGLE_DEPTH_NESTING);
2076 }
2077 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002078 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002079 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002080 }
2081 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002082 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002083 __acquire(&send_cq->lock);
2084 } else {
2085 __acquire(&send_cq->lock);
2086 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002087 }
2088}
2089
2090static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
2091 __releases(&send_cq->lock) __releases(&recv_cq->lock)
2092{
2093 if (send_cq) {
2094 if (recv_cq) {
2095 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
2096 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002097 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002098 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
2099 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002100 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002101 } else {
2102 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002103 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002104 }
2105 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002106 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002107 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002108 }
2109 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002110 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002111 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002112 } else {
2113 __release(&recv_cq->lock);
2114 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002115 }
2116}
2117
2118static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
2119{
2120 return to_mpd(qp->ibqp.pd);
2121}
2122
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002123static void get_cqs(enum ib_qp_type qp_type,
2124 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03002125 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
2126{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002127 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002128 case IB_QPT_XRC_TGT:
2129 *send_cq = NULL;
2130 *recv_cq = NULL;
2131 break;
2132 case MLX5_IB_QPT_REG_UMR:
2133 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002134 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002135 *recv_cq = NULL;
2136 break;
2137
2138 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002139 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002140 case IB_QPT_RC:
2141 case IB_QPT_UC:
2142 case IB_QPT_UD:
2143 case IB_QPT_RAW_IPV6:
2144 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002145 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002146 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2147 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002148 break;
2149
Eli Cohene126ba92013-07-07 17:25:49 +03002150 case IB_QPT_MAX:
2151 default:
2152 *send_cq = NULL;
2153 *recv_cq = NULL;
2154 break;
2155 }
2156}
2157
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002158static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002159 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2160 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002161
Eli Cohene126ba92013-07-07 17:25:49 +03002162static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
2163{
2164 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002165 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002166 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002167 int err;
2168
Yishai Hadas28d61372016-05-23 15:20:56 +03002169 if (qp->ibqp.rwq_ind_tbl) {
2170 destroy_rss_raw_qp_tir(dev, qp);
2171 return;
2172 }
2173
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002174 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2175 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002176 &qp->raw_packet_qp.rq.base :
2177 &qp->trans_qp.base;
2178
Haggai Eran6aec21f2014-12-11 17:04:23 +02002179 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002180 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2181 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002182 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002183 MLX5_CMD_OP_2RST_QP, 0,
2184 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002185 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002186 struct mlx5_modify_raw_qp_param raw_qp_param = {
2187 .operation = MLX5_CMD_OP_2RST_QP
2188 };
2189
Aviv Heller13eab212016-09-18 20:48:04 +03002190 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002191 }
2192 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002193 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002194 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002195 }
Eli Cohene126ba92013-07-07 17:25:49 +03002196
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002197 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2198 &send_cq, &recv_cq);
2199
2200 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2201 mlx5_ib_lock_cqs(send_cq, recv_cq);
2202 /* del from lists under both locks above to protect reset flow paths */
2203 list_del(&qp->qps_list);
2204 if (send_cq)
2205 list_del(&qp->cq_send_list);
2206
2207 if (recv_cq)
2208 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002209
2210 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002211 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002212 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2213 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002214 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2215 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002216 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002217 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2218 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002219
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002220 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2221 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002222 destroy_raw_packet_qp(dev, qp);
2223 } else {
2224 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
2225 if (err)
2226 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2227 base->mqp.qpn);
2228 }
Eli Cohene126ba92013-07-07 17:25:49 +03002229
Eli Cohene126ba92013-07-07 17:25:49 +03002230 if (qp->create_type == MLX5_QP_KERNEL)
2231 destroy_qp_kernel(dev, qp);
2232 else if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02002233 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002234}
2235
2236static const char *ib_qp_type_str(enum ib_qp_type type)
2237{
2238 switch (type) {
2239 case IB_QPT_SMI:
2240 return "IB_QPT_SMI";
2241 case IB_QPT_GSI:
2242 return "IB_QPT_GSI";
2243 case IB_QPT_RC:
2244 return "IB_QPT_RC";
2245 case IB_QPT_UC:
2246 return "IB_QPT_UC";
2247 case IB_QPT_UD:
2248 return "IB_QPT_UD";
2249 case IB_QPT_RAW_IPV6:
2250 return "IB_QPT_RAW_IPV6";
2251 case IB_QPT_RAW_ETHERTYPE:
2252 return "IB_QPT_RAW_ETHERTYPE";
2253 case IB_QPT_XRC_INI:
2254 return "IB_QPT_XRC_INI";
2255 case IB_QPT_XRC_TGT:
2256 return "IB_QPT_XRC_TGT";
2257 case IB_QPT_RAW_PACKET:
2258 return "IB_QPT_RAW_PACKET";
2259 case MLX5_IB_QPT_REG_UMR:
2260 return "MLX5_IB_QPT_REG_UMR";
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002261 case IB_QPT_DRIVER:
2262 return "IB_QPT_DRIVER";
Eli Cohene126ba92013-07-07 17:25:49 +03002263 case IB_QPT_MAX:
2264 default:
2265 return "Invalid QP type";
2266 }
2267}
2268
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002269static struct ib_qp *mlx5_ib_create_dct(struct ib_pd *pd,
2270 struct ib_qp_init_attr *attr,
2271 struct mlx5_ib_create_qp *ucmd)
2272{
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002273 struct mlx5_ib_qp *qp;
2274 int err = 0;
2275 u32 uidx = MLX5_IB_DEFAULT_UIDX;
2276 void *dctc;
2277
2278 if (!attr->srq || !attr->recv_cq)
2279 return ERR_PTR(-EINVAL);
2280
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002281 err = get_qp_user_index(to_mucontext(pd->uobject->context),
2282 ucmd, sizeof(*ucmd), &uidx);
2283 if (err)
2284 return ERR_PTR(err);
2285
2286 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2287 if (!qp)
2288 return ERR_PTR(-ENOMEM);
2289
2290 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
2291 if (!qp->dct.in) {
2292 err = -ENOMEM;
2293 goto err_free;
2294 }
2295
Yishai Hadasa01a5862018-09-20 21:39:24 +03002296 MLX5_SET(create_dct_in, qp->dct.in, uid, to_mpd(pd)->uid);
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002297 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shoua776a3902018-01-02 16:19:33 +02002298 qp->qp_sub_type = MLX5_IB_QPT_DCT;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002299 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2300 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2301 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2302 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2303 MLX5_SET(dctc, dctc, user_index, uidx);
2304
2305 qp->state = IB_QPS_RESET;
2306
2307 return &qp->ibqp;
2308err_free:
2309 kfree(qp);
2310 return ERR_PTR(err);
2311}
2312
2313static int set_mlx_qp_type(struct mlx5_ib_dev *dev,
2314 struct ib_qp_init_attr *init_attr,
2315 struct mlx5_ib_create_qp *ucmd,
2316 struct ib_udata *udata)
2317{
2318 enum { MLX_QP_FLAGS = MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI };
2319 int err;
2320
2321 if (!udata)
2322 return -EINVAL;
2323
2324 if (udata->inlen < sizeof(*ucmd)) {
2325 mlx5_ib_dbg(dev, "create_qp user command is smaller than expected\n");
2326 return -EINVAL;
2327 }
2328 err = ib_copy_from_udata(ucmd, udata, sizeof(*ucmd));
2329 if (err)
2330 return err;
2331
2332 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCI) {
2333 init_attr->qp_type = MLX5_IB_QPT_DCI;
2334 } else {
2335 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCT) {
2336 init_attr->qp_type = MLX5_IB_QPT_DCT;
2337 } else {
2338 mlx5_ib_dbg(dev, "Invalid QP flags\n");
2339 return -EINVAL;
2340 }
2341 }
2342
2343 if (!MLX5_CAP_GEN(dev->mdev, dct)) {
2344 mlx5_ib_dbg(dev, "DC transport is not supported\n");
2345 return -EOPNOTSUPP;
2346 }
2347
2348 return 0;
2349}
2350
Eli Cohene126ba92013-07-07 17:25:49 +03002351struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002352 struct ib_qp_init_attr *verbs_init_attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002353 struct ib_udata *udata)
2354{
2355 struct mlx5_ib_dev *dev;
2356 struct mlx5_ib_qp *qp;
2357 u16 xrcdn = 0;
2358 int err;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002359 struct ib_qp_init_attr mlx_init_attr;
2360 struct ib_qp_init_attr *init_attr = verbs_init_attr;
Eli Cohene126ba92013-07-07 17:25:49 +03002361
2362 if (pd) {
2363 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002364
2365 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2366 if (!pd->uobject) {
2367 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2368 return ERR_PTR(-EINVAL);
2369 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2370 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2371 return ERR_PTR(-EINVAL);
2372 }
2373 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002374 } else {
2375 /* being cautious here */
2376 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2377 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2378 pr_warn("%s: no PD for transport %s\n", __func__,
2379 ib_qp_type_str(init_attr->qp_type));
2380 return ERR_PTR(-EINVAL);
2381 }
2382 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002383 }
2384
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002385 if (init_attr->qp_type == IB_QPT_DRIVER) {
2386 struct mlx5_ib_create_qp ucmd;
2387
2388 init_attr = &mlx_init_attr;
2389 memcpy(init_attr, verbs_init_attr, sizeof(*verbs_init_attr));
2390 err = set_mlx_qp_type(dev, init_attr, &ucmd, udata);
2391 if (err)
2392 return ERR_PTR(err);
Moni Shouac32a4f22018-01-02 16:19:32 +02002393
2394 if (init_attr->qp_type == MLX5_IB_QPT_DCI) {
2395 if (init_attr->cap.max_recv_wr ||
2396 init_attr->cap.max_recv_sge) {
2397 mlx5_ib_dbg(dev, "DCI QP requires zero size receive queue\n");
2398 return ERR_PTR(-EINVAL);
2399 }
Moni Shoua776a3902018-01-02 16:19:33 +02002400 } else {
2401 return mlx5_ib_create_dct(pd, init_attr, &ucmd);
Moni Shouac32a4f22018-01-02 16:19:32 +02002402 }
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002403 }
2404
Eli Cohene126ba92013-07-07 17:25:49 +03002405 switch (init_attr->qp_type) {
2406 case IB_QPT_XRC_TGT:
2407 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002408 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002409 mlx5_ib_dbg(dev, "XRC not supported\n");
2410 return ERR_PTR(-ENOSYS);
2411 }
2412 init_attr->recv_cq = NULL;
2413 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2414 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2415 init_attr->send_cq = NULL;
2416 }
2417
2418 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002419 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002420 case IB_QPT_RC:
2421 case IB_QPT_UC:
2422 case IB_QPT_UD:
2423 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002424 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002425 case MLX5_IB_QPT_REG_UMR:
Moni Shouac32a4f22018-01-02 16:19:32 +02002426 case MLX5_IB_QPT_DCI:
Eli Cohene126ba92013-07-07 17:25:49 +03002427 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2428 if (!qp)
2429 return ERR_PTR(-ENOMEM);
2430
2431 err = create_qp_common(dev, pd, init_attr, udata, qp);
2432 if (err) {
2433 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2434 kfree(qp);
2435 return ERR_PTR(err);
2436 }
2437
2438 if (is_qp0(init_attr->qp_type))
2439 qp->ibqp.qp_num = 0;
2440 else if (is_qp1(init_attr->qp_type))
2441 qp->ibqp.qp_num = 1;
2442 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002443 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002444
2445 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 +02002446 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002447 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2448 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002449
majd@mellanox.com19098df2016-01-14 19:13:03 +02002450 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002451
2452 break;
2453
Haggai Erand16e91d2016-02-29 15:45:05 +02002454 case IB_QPT_GSI:
2455 return mlx5_ib_gsi_create_qp(pd, init_attr);
2456
Eli Cohene126ba92013-07-07 17:25:49 +03002457 case IB_QPT_RAW_IPV6:
2458 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002459 case IB_QPT_MAX:
2460 default:
2461 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2462 init_attr->qp_type);
2463 /* Don't support raw QPs */
2464 return ERR_PTR(-EINVAL);
2465 }
2466
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002467 if (verbs_init_attr->qp_type == IB_QPT_DRIVER)
2468 qp->qp_sub_type = init_attr->qp_type;
2469
Eli Cohene126ba92013-07-07 17:25:49 +03002470 return &qp->ibqp;
2471}
2472
Moni Shoua776a3902018-01-02 16:19:33 +02002473static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2474{
2475 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2476
2477 if (mqp->state == IB_QPS_RTR) {
2478 int err;
2479
2480 err = mlx5_core_destroy_dct(dev->mdev, &mqp->dct.mdct);
2481 if (err) {
2482 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2483 return err;
2484 }
2485 }
2486
2487 kfree(mqp->dct.in);
2488 kfree(mqp);
2489 return 0;
2490}
2491
Eli Cohene126ba92013-07-07 17:25:49 +03002492int mlx5_ib_destroy_qp(struct ib_qp *qp)
2493{
2494 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2495 struct mlx5_ib_qp *mqp = to_mqp(qp);
2496
Haggai Erand16e91d2016-02-29 15:45:05 +02002497 if (unlikely(qp->qp_type == IB_QPT_GSI))
2498 return mlx5_ib_gsi_destroy_qp(qp);
2499
Moni Shoua776a3902018-01-02 16:19:33 +02002500 if (mqp->qp_sub_type == MLX5_IB_QPT_DCT)
2501 return mlx5_ib_destroy_dct(mqp);
2502
Eli Cohene126ba92013-07-07 17:25:49 +03002503 destroy_qp_common(dev, mqp);
2504
2505 kfree(mqp);
2506
2507 return 0;
2508}
2509
2510static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2511 int attr_mask)
2512{
2513 u32 hw_access_flags = 0;
2514 u8 dest_rd_atomic;
2515 u32 access_flags;
2516
2517 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2518 dest_rd_atomic = attr->max_dest_rd_atomic;
2519 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002520 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002521
2522 if (attr_mask & IB_QP_ACCESS_FLAGS)
2523 access_flags = attr->qp_access_flags;
2524 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002525 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002526
2527 if (!dest_rd_atomic)
2528 access_flags &= IB_ACCESS_REMOTE_WRITE;
2529
2530 if (access_flags & IB_ACCESS_REMOTE_READ)
2531 hw_access_flags |= MLX5_QP_BIT_RRE;
2532 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2533 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2534 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2535 hw_access_flags |= MLX5_QP_BIT_RWE;
2536
2537 return cpu_to_be32(hw_access_flags);
2538}
2539
2540enum {
2541 MLX5_PATH_FLAG_FL = 1 << 0,
2542 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2543 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2544};
2545
2546static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2547{
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002548 if (rate == IB_RATE_PORT_CURRENT)
Eli Cohene126ba92013-07-07 17:25:49 +03002549 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002550
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002551 if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS)
2552 return -EINVAL;
2553
2554 while (rate != IB_RATE_PORT_CURRENT &&
2555 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
2556 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
2557 --rate;
2558
2559 return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
Eli Cohene126ba92013-07-07 17:25:49 +03002560}
2561
majd@mellanox.com75850d02016-01-14 19:13:06 +02002562static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002563 struct mlx5_ib_sq *sq, u8 sl,
2564 struct ib_pd *pd)
majd@mellanox.com75850d02016-01-14 19:13:06 +02002565{
2566 void *in;
2567 void *tisc;
2568 int inlen;
2569 int err;
2570
2571 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002572 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002573 if (!in)
2574 return -ENOMEM;
2575
2576 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002577 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002578
2579 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2580 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2581
2582 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2583
2584 kvfree(in);
2585
2586 return err;
2587}
2588
Aviv Heller13eab212016-09-18 20:48:04 +03002589static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002590 struct mlx5_ib_sq *sq, u8 tx_affinity,
2591 struct ib_pd *pd)
Aviv Heller13eab212016-09-18 20:48:04 +03002592{
2593 void *in;
2594 void *tisc;
2595 int inlen;
2596 int err;
2597
2598 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002599 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002600 if (!in)
2601 return -ENOMEM;
2602
2603 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002604 MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
Aviv Heller13eab212016-09-18 20:48:04 +03002605
2606 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2607 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2608
2609 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2610
2611 kvfree(in);
2612
2613 return err;
2614}
2615
majd@mellanox.com75850d02016-01-14 19:13:06 +02002616static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002617 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002618 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002619 u32 path_flags, const struct ib_qp_attr *attr,
2620 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002621{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002622 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002623 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002624 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002625 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2626 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002627
Eli Cohene126ba92013-07-07 17:25:49 +03002628 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002629 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2630 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002631
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002632 if (ah_flags & IB_AH_GRH) {
2633 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002634 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002635 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002636 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002637 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002638 return -EINVAL;
2639 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002640 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002641
2642 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002643 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002644 return -EINVAL;
Parav Pandit47ec3862018-06-13 10:22:06 +03002645
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002646 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02002647 if (qp->ibqp.qp_type == IB_QPT_RC ||
2648 qp->ibqp.qp_type == IB_QPT_UC ||
2649 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
2650 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
Parav Pandit47ec3862018-06-13 10:22:06 +03002651 path->udp_sport =
2652 mlx5_get_roce_udp_sport(dev, ah->grh.sgid_attr);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002653 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Parav Pandit47ec3862018-06-13 10:22:06 +03002654 gid_type = ah->grh.sgid_attr->gid_type;
Majd Dibbinyed884512017-01-18 14:10:35 +02002655 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002656 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002657 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002658 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2659 path->fl_free_ar |=
2660 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002661 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2662 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2663 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002664 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002665 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002666 }
2667
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002668 if (ah_flags & IB_AH_GRH) {
2669 path->mgid_index = grh->sgid_index;
2670 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002671 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002672 cpu_to_be32((grh->traffic_class << 20) |
2673 (grh->flow_label));
2674 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002675 }
2676
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002677 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002678 if (err < 0)
2679 return err;
2680 path->static_rate = err;
2681 path->port = port;
2682
Eli Cohene126ba92013-07-07 17:25:49 +03002683 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002684 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002685
majd@mellanox.com75850d02016-01-14 19:13:06 +02002686 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2687 return modify_raw_packet_eth_prio(dev->mdev,
2688 &qp->raw_packet_qp.sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03002689 sl & 0xf, qp->ibqp.pd);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002690
Eli Cohene126ba92013-07-07 17:25:49 +03002691 return 0;
2692}
2693
2694static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2695 [MLX5_QP_STATE_INIT] = {
2696 [MLX5_QP_STATE_INIT] = {
2697 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2698 MLX5_QP_OPTPAR_RAE |
2699 MLX5_QP_OPTPAR_RWE |
2700 MLX5_QP_OPTPAR_PKEY_INDEX |
2701 MLX5_QP_OPTPAR_PRI_PORT,
2702 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2703 MLX5_QP_OPTPAR_PKEY_INDEX |
2704 MLX5_QP_OPTPAR_PRI_PORT,
2705 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2706 MLX5_QP_OPTPAR_Q_KEY |
2707 MLX5_QP_OPTPAR_PRI_PORT,
2708 },
2709 [MLX5_QP_STATE_RTR] = {
2710 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2711 MLX5_QP_OPTPAR_RRE |
2712 MLX5_QP_OPTPAR_RAE |
2713 MLX5_QP_OPTPAR_RWE |
2714 MLX5_QP_OPTPAR_PKEY_INDEX,
2715 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2716 MLX5_QP_OPTPAR_RWE |
2717 MLX5_QP_OPTPAR_PKEY_INDEX,
2718 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2719 MLX5_QP_OPTPAR_Q_KEY,
2720 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2721 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002722 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2723 MLX5_QP_OPTPAR_RRE |
2724 MLX5_QP_OPTPAR_RAE |
2725 MLX5_QP_OPTPAR_RWE |
2726 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002727 },
2728 },
2729 [MLX5_QP_STATE_RTR] = {
2730 [MLX5_QP_STATE_RTS] = {
2731 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2732 MLX5_QP_OPTPAR_RRE |
2733 MLX5_QP_OPTPAR_RAE |
2734 MLX5_QP_OPTPAR_RWE |
2735 MLX5_QP_OPTPAR_PM_STATE |
2736 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2737 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2738 MLX5_QP_OPTPAR_RWE |
2739 MLX5_QP_OPTPAR_PM_STATE,
2740 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2741 },
2742 },
2743 [MLX5_QP_STATE_RTS] = {
2744 [MLX5_QP_STATE_RTS] = {
2745 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2746 MLX5_QP_OPTPAR_RAE |
2747 MLX5_QP_OPTPAR_RWE |
2748 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002749 MLX5_QP_OPTPAR_PM_STATE |
2750 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002751 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03002752 MLX5_QP_OPTPAR_PM_STATE |
2753 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002754 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
2755 MLX5_QP_OPTPAR_SRQN |
2756 MLX5_QP_OPTPAR_CQN_RCV,
2757 },
2758 },
2759 [MLX5_QP_STATE_SQER] = {
2760 [MLX5_QP_STATE_RTS] = {
2761 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2762 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002763 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002764 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2765 MLX5_QP_OPTPAR_RWE |
2766 MLX5_QP_OPTPAR_RAE |
2767 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002768 },
2769 },
2770};
2771
2772static int ib_nr_to_mlx5_nr(int ib_mask)
2773{
2774 switch (ib_mask) {
2775 case IB_QP_STATE:
2776 return 0;
2777 case IB_QP_CUR_STATE:
2778 return 0;
2779 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2780 return 0;
2781 case IB_QP_ACCESS_FLAGS:
2782 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2783 MLX5_QP_OPTPAR_RAE;
2784 case IB_QP_PKEY_INDEX:
2785 return MLX5_QP_OPTPAR_PKEY_INDEX;
2786 case IB_QP_PORT:
2787 return MLX5_QP_OPTPAR_PRI_PORT;
2788 case IB_QP_QKEY:
2789 return MLX5_QP_OPTPAR_Q_KEY;
2790 case IB_QP_AV:
2791 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2792 MLX5_QP_OPTPAR_PRI_PORT;
2793 case IB_QP_PATH_MTU:
2794 return 0;
2795 case IB_QP_TIMEOUT:
2796 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2797 case IB_QP_RETRY_CNT:
2798 return MLX5_QP_OPTPAR_RETRY_COUNT;
2799 case IB_QP_RNR_RETRY:
2800 return MLX5_QP_OPTPAR_RNR_RETRY;
2801 case IB_QP_RQ_PSN:
2802 return 0;
2803 case IB_QP_MAX_QP_RD_ATOMIC:
2804 return MLX5_QP_OPTPAR_SRA_MAX;
2805 case IB_QP_ALT_PATH:
2806 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2807 case IB_QP_MIN_RNR_TIMER:
2808 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2809 case IB_QP_SQ_PSN:
2810 return 0;
2811 case IB_QP_MAX_DEST_RD_ATOMIC:
2812 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2813 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2814 case IB_QP_PATH_MIG_STATE:
2815 return MLX5_QP_OPTPAR_PM_STATE;
2816 case IB_QP_CAP:
2817 return 0;
2818 case IB_QP_DEST_QPN:
2819 return 0;
2820 }
2821 return 0;
2822}
2823
2824static int ib_mask_to_mlx5_opt(int ib_mask)
2825{
2826 int result = 0;
2827 int i;
2828
2829 for (i = 0; i < 8 * sizeof(int); i++) {
2830 if ((1 << i) & ib_mask)
2831 result |= ib_nr_to_mlx5_nr(1 << i);
2832 }
2833
2834 return result;
2835}
2836
Yishai Hadas34d57582018-09-20 21:39:21 +03002837static int modify_raw_packet_qp_rq(
2838 struct mlx5_ib_dev *dev, struct mlx5_ib_rq *rq, int new_state,
2839 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002840{
2841 void *in;
2842 void *rqc;
2843 int inlen;
2844 int err;
2845
2846 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002847 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002848 if (!in)
2849 return -ENOMEM;
2850
2851 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
Yishai Hadas34d57582018-09-20 21:39:21 +03002852 MLX5_SET(modify_rq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002853
2854 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2855 MLX5_SET(rqc, rqc, state, new_state);
2856
Alex Veskereb49ab02016-08-28 12:25:53 +03002857 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2858 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2859 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02002860 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03002861 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2862 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06002863 dev_info_once(
2864 &dev->ib_dev.dev,
2865 "RAW PACKET QP counters are not supported on current FW\n");
Alex Veskereb49ab02016-08-28 12:25:53 +03002866 }
2867
2868 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002869 if (err)
2870 goto out;
2871
2872 rq->state = new_state;
2873
2874out:
2875 kvfree(in);
2876 return err;
2877}
2878
Yishai Hadasc14003f2018-09-20 21:39:22 +03002879static int modify_raw_packet_qp_sq(
2880 struct mlx5_core_dev *dev, struct mlx5_ib_sq *sq, int new_state,
2881 const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002882{
Bodong Wang7d29f342016-12-01 13:43:16 +02002883 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
Bodong Wang61147f32018-03-19 15:10:30 +02002884 struct mlx5_rate_limit old_rl = ibqp->rl;
2885 struct mlx5_rate_limit new_rl = old_rl;
2886 bool new_rate_added = false;
Bodong Wang7d29f342016-12-01 13:43:16 +02002887 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002888 void *in;
2889 void *sqc;
2890 int inlen;
2891 int err;
2892
2893 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002894 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002895 if (!in)
2896 return -ENOMEM;
2897
Yishai Hadasc14003f2018-09-20 21:39:22 +03002898 MLX5_SET(modify_sq_in, in, uid, to_mpd(pd)->uid);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002899 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2900
2901 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2902 MLX5_SET(sqc, sqc, state, new_state);
2903
Bodong Wang7d29f342016-12-01 13:43:16 +02002904 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2905 if (new_state != MLX5_SQC_STATE_RDY)
2906 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2907 __func__);
2908 else
Bodong Wang61147f32018-03-19 15:10:30 +02002909 new_rl = raw_qp_param->rl;
Bodong Wang7d29f342016-12-01 13:43:16 +02002910 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002911
Bodong Wang61147f32018-03-19 15:10:30 +02002912 if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
2913 if (new_rl.rate) {
2914 err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02002915 if (err) {
Bodong Wang61147f32018-03-19 15:10:30 +02002916 pr_err("Failed configuring rate limit(err %d): \
2917 rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
2918 err, new_rl.rate, new_rl.max_burst_sz,
2919 new_rl.typical_pkt_sz);
2920
Bodong Wang7d29f342016-12-01 13:43:16 +02002921 goto out;
2922 }
Bodong Wang61147f32018-03-19 15:10:30 +02002923 new_rate_added = true;
Bodong Wang7d29f342016-12-01 13:43:16 +02002924 }
2925
2926 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
Bodong Wang61147f32018-03-19 15:10:30 +02002927 /* index 0 means no limit */
Bodong Wang7d29f342016-12-01 13:43:16 +02002928 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2929 }
2930
2931 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2932 if (err) {
2933 /* Remove new rate from table if failed */
Bodong Wang61147f32018-03-19 15:10:30 +02002934 if (new_rate_added)
2935 mlx5_rl_remove_rate(dev, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02002936 goto out;
2937 }
2938
2939 /* Only remove the old rate after new rate was set */
Bodong Wang61147f32018-03-19 15:10:30 +02002940 if ((old_rl.rate &&
2941 !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
Bodong Wang7d29f342016-12-01 13:43:16 +02002942 (new_state != MLX5_SQC_STATE_RDY))
Bodong Wang61147f32018-03-19 15:10:30 +02002943 mlx5_rl_remove_rate(dev, &old_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02002944
Bodong Wang61147f32018-03-19 15:10:30 +02002945 ibqp->rl = new_rl;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002946 sq->state = new_state;
2947
2948out:
2949 kvfree(in);
2950 return err;
2951}
2952
2953static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002954 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2955 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002956{
2957 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2958 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2959 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002960 int modify_rq = !!qp->rq.wqe_cnt;
2961 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002962 int rq_state;
2963 int sq_state;
2964 int err;
2965
Alex Vesker0680efa2016-08-28 12:25:52 +03002966 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002967 case MLX5_CMD_OP_RST2INIT_QP:
2968 rq_state = MLX5_RQC_STATE_RDY;
2969 sq_state = MLX5_SQC_STATE_RDY;
2970 break;
2971 case MLX5_CMD_OP_2ERR_QP:
2972 rq_state = MLX5_RQC_STATE_ERR;
2973 sq_state = MLX5_SQC_STATE_ERR;
2974 break;
2975 case MLX5_CMD_OP_2RST_QP:
2976 rq_state = MLX5_RQC_STATE_RST;
2977 sq_state = MLX5_SQC_STATE_RST;
2978 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002979 case MLX5_CMD_OP_RTR2RTS_QP:
2980 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002981 if (raw_qp_param->set_mask ==
2982 MLX5_RAW_QP_RATE_LIMIT) {
2983 modify_rq = 0;
2984 sq_state = sq->state;
2985 } else {
2986 return raw_qp_param->set_mask ? -EINVAL : 0;
2987 }
2988 break;
2989 case MLX5_CMD_OP_INIT2INIT_QP:
2990 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002991 if (raw_qp_param->set_mask)
2992 return -EINVAL;
2993 else
2994 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002995 default:
2996 WARN_ON(1);
2997 return -EINVAL;
2998 }
2999
Bodong Wang7d29f342016-12-01 13:43:16 +02003000 if (modify_rq) {
Yishai Hadas34d57582018-09-20 21:39:21 +03003001 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param,
3002 qp->ibqp.pd);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003003 if (err)
3004 return err;
3005 }
3006
Bodong Wang7d29f342016-12-01 13:43:16 +02003007 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03003008 if (tx_affinity) {
3009 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
Yishai Hadas1cd6dbd2018-09-20 21:39:27 +03003010 tx_affinity,
3011 qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003012 if (err)
3013 return err;
3014 }
3015
Yishai Hadasc14003f2018-09-20 21:39:22 +03003016 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state,
3017 raw_qp_param, qp->ibqp.pd);
Aviv Heller13eab212016-09-18 20:48:04 +03003018 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003019
3020 return 0;
3021}
3022
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003023static unsigned int get_tx_affinity(struct mlx5_ib_dev *dev,
3024 struct mlx5_ib_pd *pd,
3025 struct mlx5_ib_qp_base *qp_base,
3026 u8 port_num)
3027{
3028 struct mlx5_ib_ucontext *ucontext = NULL;
3029 unsigned int tx_port_affinity;
3030
3031 if (pd && pd->ibpd.uobject && pd->ibpd.uobject->context)
3032 ucontext = to_mucontext(pd->ibpd.uobject->context);
3033
3034 if (ucontext) {
3035 tx_port_affinity = (unsigned int)atomic_add_return(
3036 1, &ucontext->tx_port_affinity) %
3037 MLX5_MAX_PORTS +
3038 1;
3039 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x ucontext %p\n",
3040 tx_port_affinity, qp_base->mqp.qpn, ucontext);
3041 } else {
3042 tx_port_affinity =
3043 (unsigned int)atomic_add_return(
3044 1, &dev->roce[port_num].tx_port_affinity) %
3045 MLX5_MAX_PORTS +
3046 1;
3047 mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x\n",
3048 tx_port_affinity, qp_base->mqp.qpn);
3049 }
3050
3051 return tx_port_affinity;
3052}
3053
Eli Cohene126ba92013-07-07 17:25:49 +03003054static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
3055 const struct ib_qp_attr *attr, int attr_mask,
Bodong Wang61147f32018-03-19 15:10:30 +02003056 enum ib_qp_state cur_state, enum ib_qp_state new_state,
3057 const struct mlx5_ib_modify_qp *ucmd)
Eli Cohene126ba92013-07-07 17:25:49 +03003058{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003059 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
3060 [MLX5_QP_STATE_RST] = {
3061 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3062 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3063 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
3064 },
3065 [MLX5_QP_STATE_INIT] = {
3066 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3067 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3068 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
3069 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
3070 },
3071 [MLX5_QP_STATE_RTR] = {
3072 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3073 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3074 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
3075 },
3076 [MLX5_QP_STATE_RTS] = {
3077 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3078 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3079 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
3080 },
3081 [MLX5_QP_STATE_SQD] = {
3082 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3083 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3084 },
3085 [MLX5_QP_STATE_SQER] = {
3086 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3087 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3088 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
3089 },
3090 [MLX5_QP_STATE_ERR] = {
3091 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
3092 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
3093 }
3094 };
3095
Eli Cohene126ba92013-07-07 17:25:49 +03003096 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3097 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02003098 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03003099 struct mlx5_ib_cq *send_cq, *recv_cq;
3100 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03003101 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03003102 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003103 enum mlx5_qp_state mlx5_cur, mlx5_new;
3104 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03003105 int mlx5_st;
3106 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003107 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03003108 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003109
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003110 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
3111 qp->qp_sub_type : ibqp->qp_type);
3112 if (mlx5_st < 0)
3113 return -EINVAL;
3114
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003115 context = kzalloc(sizeof(*context), GFP_KERNEL);
3116 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03003117 return -ENOMEM;
3118
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003119 pd = get_pd(qp);
Leon Romanovsky55de9a72018-02-25 13:39:52 +02003120 context->flags = cpu_to_be32(mlx5_st << 16);
Eli Cohene126ba92013-07-07 17:25:49 +03003121
3122 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
3123 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3124 } else {
3125 switch (attr->path_mig_state) {
3126 case IB_MIG_MIGRATED:
3127 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
3128 break;
3129 case IB_MIG_REARM:
3130 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
3131 break;
3132 case IB_MIG_ARMED:
3133 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
3134 break;
3135 }
3136 }
3137
Aviv Heller13eab212016-09-18 20:48:04 +03003138 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
3139 if ((ibqp->qp_type == IB_QPT_RC) ||
3140 (ibqp->qp_type == IB_QPT_UD &&
3141 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
3142 (ibqp->qp_type == IB_QPT_UC) ||
3143 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
3144 (ibqp->qp_type == IB_QPT_XRC_INI) ||
3145 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
3146 if (mlx5_lag_is_active(dev->mdev)) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003147 u8 p = mlx5_core_native_port_num(dev->mdev);
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03003148 tx_affinity = get_tx_affinity(dev, pd, base, p);
Aviv Heller13eab212016-09-18 20:48:04 +03003149 context->flags |= cpu_to_be32(tx_affinity << 24);
3150 }
3151 }
3152 }
3153
Haggai Erand16e91d2016-02-29 15:45:05 +02003154 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003155 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003156 } else if ((ibqp->qp_type == IB_QPT_UD &&
3157 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003158 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
3159 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
3160 } else if (attr_mask & IB_QP_PATH_MTU) {
3161 if (attr->path_mtu < IB_MTU_256 ||
3162 attr->path_mtu > IB_MTU_4096) {
3163 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3164 err = -EINVAL;
3165 goto out;
3166 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003167 context->mtu_msgmax = (attr->path_mtu << 5) |
3168 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03003169 }
3170
3171 if (attr_mask & IB_QP_DEST_QPN)
3172 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
3173
3174 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003175 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003176
3177 /* todo implement counter_index functionality */
3178
3179 if (is_sqp(ibqp->qp_type))
3180 context->pri_path.port = qp->port;
3181
3182 if (attr_mask & IB_QP_PORT)
3183 context->pri_path.port = attr->port_num;
3184
3185 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003186 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003187 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003188 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003189 if (err)
3190 goto out;
3191 }
3192
3193 if (attr_mask & IB_QP_TIMEOUT)
3194 context->pri_path.ackto_lt |= attr->timeout << 3;
3195
3196 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003197 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3198 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003199 attr->alt_port_num,
3200 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3201 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003202 if (err)
3203 goto out;
3204 }
3205
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003206 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3207 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003208
3209 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3210 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3211 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3212 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3213
3214 if (attr_mask & IB_QP_RNR_RETRY)
3215 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3216
3217 if (attr_mask & IB_QP_RETRY_CNT)
3218 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3219
3220 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3221 if (attr->max_rd_atomic)
3222 context->params1 |=
3223 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3224 }
3225
3226 if (attr_mask & IB_QP_SQ_PSN)
3227 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3228
3229 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3230 if (attr->max_dest_rd_atomic)
3231 context->params2 |=
3232 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3233 }
3234
3235 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
3236 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
3237
3238 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3239 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3240
3241 if (attr_mask & IB_QP_RQ_PSN)
3242 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3243
3244 if (attr_mask & IB_QP_QKEY)
3245 context->qkey = cpu_to_be32(attr->qkey);
3246
3247 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3248 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3249
Mark Bloch0837e862016-06-17 15:10:55 +03003250 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3251 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3252 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003253
3254 /* Underlay port should be used - index 0 function per port */
3255 if (qp->flags & MLX5_IB_QP_UNDERLAY)
3256 port_num = 0;
3257
Alex Veskereb49ab02016-08-28 12:25:53 +03003258 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03003259 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03003260 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003261 }
3262
Eli Cohene126ba92013-07-07 17:25:49 +03003263 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3264 context->sq_crq_size |= cpu_to_be16(1 << 4);
3265
Haggai Eranb11a4f92016-02-29 15:45:03 +02003266 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
3267 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003268
3269 mlx5_cur = to_mlx5_state(cur_state);
3270 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003271
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003272 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003273 !optab[mlx5_cur][mlx5_new]) {
3274 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003275 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003276 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003277
3278 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003279 optpar = ib_mask_to_mlx5_opt(attr_mask);
3280 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003281
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003282 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3283 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003284 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3285
3286 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003287 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03003288 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003289 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3290 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003291
3292 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003293 raw_qp_param.rl.rate = attr->rate_limit;
3294
3295 if (ucmd->burst_info.max_burst_sz) {
3296 if (attr->rate_limit &&
3297 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3298 raw_qp_param.rl.max_burst_sz =
3299 ucmd->burst_info.max_burst_sz;
3300 } else {
3301 err = -EINVAL;
3302 goto out;
3303 }
3304 }
3305
3306 if (ucmd->burst_info.typical_pkt_sz) {
3307 if (attr->rate_limit &&
3308 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3309 raw_qp_param.rl.typical_pkt_sz =
3310 ucmd->burst_info.typical_pkt_sz;
3311 } else {
3312 err = -EINVAL;
3313 goto out;
3314 }
3315 }
3316
Bodong Wang7d29f342016-12-01 13:43:16 +02003317 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3318 }
3319
Aviv Heller13eab212016-09-18 20:48:04 +03003320 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003321 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003322 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003323 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003324 }
3325
Eli Cohene126ba92013-07-07 17:25:49 +03003326 if (err)
3327 goto out;
3328
3329 qp->state = new_state;
3330
3331 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003332 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003333 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003334 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003335 if (attr_mask & IB_QP_PORT)
3336 qp->port = attr->port_num;
3337 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003338 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003339
3340 /*
3341 * If we moved a kernel QP to RESET, clean up all old CQ
3342 * entries and reinitialize the QP.
3343 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02003344 if (new_state == IB_QPS_RESET &&
3345 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003346 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003347 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3348 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003349 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003350
3351 qp->rq.head = 0;
3352 qp->rq.tail = 0;
3353 qp->sq.head = 0;
3354 qp->sq.tail = 0;
3355 qp->sq.cur_post = 0;
3356 qp->sq.last_poll = 0;
3357 qp->db.db[MLX5_RCV_DBR] = 0;
3358 qp->db.db[MLX5_SND_DBR] = 0;
3359 }
3360
3361out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003362 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003363 return err;
3364}
3365
Moni Shouac32a4f22018-01-02 16:19:32 +02003366static inline bool is_valid_mask(int mask, int req, int opt)
3367{
3368 if ((mask & req) != req)
3369 return false;
3370
3371 if (mask & ~(req | opt))
3372 return false;
3373
3374 return true;
3375}
3376
3377/* check valid transition for driver QP types
3378 * for now the only QP type that this function supports is DCI
3379 */
3380static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3381 enum ib_qp_attr_mask attr_mask)
3382{
3383 int req = IB_QP_STATE;
3384 int opt = 0;
3385
Moni Shoua99ed7482018-09-12 09:33:55 +03003386 if (new_state == IB_QPS_RESET) {
3387 return is_valid_mask(attr_mask, req, opt);
3388 } else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Moni Shouac32a4f22018-01-02 16:19:32 +02003389 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
3390 return is_valid_mask(attr_mask, req, opt);
3391 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3392 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3393 return is_valid_mask(attr_mask, req, opt);
3394 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3395 req |= IB_QP_PATH_MTU;
3396 opt = IB_QP_PKEY_INDEX;
3397 return is_valid_mask(attr_mask, req, opt);
3398 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3399 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3400 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3401 opt = IB_QP_MIN_RNR_TIMER;
3402 return is_valid_mask(attr_mask, req, opt);
3403 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3404 opt = IB_QP_MIN_RNR_TIMER;
3405 return is_valid_mask(attr_mask, req, opt);
3406 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3407 return is_valid_mask(attr_mask, req, opt);
3408 }
3409 return false;
3410}
3411
Moni Shoua776a3902018-01-02 16:19:33 +02003412/* mlx5_ib_modify_dct: modify a DCT QP
3413 * valid transitions are:
3414 * RESET to INIT: must set access_flags, pkey_index and port
3415 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3416 * mtu, gid_index and hop_limit
3417 * Other transitions and attributes are illegal
3418 */
3419static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3420 int attr_mask, struct ib_udata *udata)
3421{
3422 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3423 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3424 enum ib_qp_state cur_state, new_state;
3425 int err = 0;
3426 int required = IB_QP_STATE;
3427 void *dctc;
3428
3429 if (!(attr_mask & IB_QP_STATE))
3430 return -EINVAL;
3431
3432 cur_state = qp->state;
3433 new_state = attr->qp_state;
3434
3435 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3436 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3437 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3438 if (!is_valid_mask(attr_mask, required, 0))
3439 return -EINVAL;
3440
3441 if (attr->port_num == 0 ||
3442 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3443 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3444 attr->port_num, dev->num_ports);
3445 return -EINVAL;
3446 }
3447 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3448 MLX5_SET(dctc, dctc, rre, 1);
3449 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3450 MLX5_SET(dctc, dctc, rwe, 1);
3451 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
3452 if (!mlx5_ib_dc_atomic_is_supported(dev))
3453 return -EOPNOTSUPP;
3454 MLX5_SET(dctc, dctc, rae, 1);
3455 MLX5_SET(dctc, dctc, atomic_mode, MLX5_ATOMIC_MODE_DCT_CX);
3456 }
3457 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3458 MLX5_SET(dctc, dctc, port, attr->port_num);
3459 MLX5_SET(dctc, dctc, counter_set_id, dev->port[attr->port_num - 1].cnts.set_id);
3460
3461 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3462 struct mlx5_ib_modify_qp_resp resp = {};
3463 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3464 sizeof(resp.dctn);
3465
3466 if (udata->outlen < min_resp_len)
3467 return -EINVAL;
3468 resp.response_length = min_resp_len;
3469
3470 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3471 if (!is_valid_mask(attr_mask, required, 0))
3472 return -EINVAL;
3473 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3474 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3475 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3476 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3477 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3478 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3479
3480 err = mlx5_core_create_dct(dev->mdev, &qp->dct.mdct, qp->dct.in,
3481 MLX5_ST_SZ_BYTES(create_dct_in));
3482 if (err)
3483 return err;
3484 resp.dctn = qp->dct.mdct.mqp.qpn;
3485 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3486 if (err) {
3487 mlx5_core_destroy_dct(dev->mdev, &qp->dct.mdct);
3488 return err;
3489 }
3490 } else {
3491 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3492 return -EINVAL;
3493 }
3494 if (err)
3495 qp->state = IB_QPS_ERR;
3496 else
3497 qp->state = new_state;
3498 return err;
3499}
3500
Eli Cohene126ba92013-07-07 17:25:49 +03003501int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3502 int attr_mask, struct ib_udata *udata)
3503{
3504 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3505 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02003506 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02003507 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003508 enum ib_qp_state cur_state, new_state;
Bodong Wang61147f32018-03-19 15:10:30 +02003509 size_t required_cmd_sz;
Eli Cohene126ba92013-07-07 17:25:49 +03003510 int err = -EINVAL;
3511 int port;
3512
Yishai Hadas28d61372016-05-23 15:20:56 +03003513 if (ibqp->rwq_ind_tbl)
3514 return -ENOSYS;
3515
Bodong Wang61147f32018-03-19 15:10:30 +02003516 if (udata && udata->inlen) {
3517 required_cmd_sz = offsetof(typeof(ucmd), reserved) +
3518 sizeof(ucmd.reserved);
3519 if (udata->inlen < required_cmd_sz)
3520 return -EINVAL;
3521
3522 if (udata->inlen > sizeof(ucmd) &&
3523 !ib_is_udata_cleared(udata, sizeof(ucmd),
3524 udata->inlen - sizeof(ucmd)))
3525 return -EOPNOTSUPP;
3526
3527 if (ib_copy_from_udata(&ucmd, udata,
3528 min(udata->inlen, sizeof(ucmd))))
3529 return -EFAULT;
3530
3531 if (ucmd.comp_mask ||
3532 memchr_inv(&ucmd.reserved, 0, sizeof(ucmd.reserved)) ||
3533 memchr_inv(&ucmd.burst_info.reserved, 0,
3534 sizeof(ucmd.burst_info.reserved)))
3535 return -EOPNOTSUPP;
3536 }
3537
Haggai Erand16e91d2016-02-29 15:45:05 +02003538 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3539 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3540
Moni Shouac32a4f22018-01-02 16:19:32 +02003541 if (ibqp->qp_type == IB_QPT_DRIVER)
3542 qp_type = qp->qp_sub_type;
3543 else
3544 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3545 IB_QPT_GSI : ibqp->qp_type;
3546
Moni Shoua776a3902018-01-02 16:19:33 +02003547 if (qp_type == MLX5_IB_QPT_DCT)
3548 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02003549
Eli Cohene126ba92013-07-07 17:25:49 +03003550 mutex_lock(&qp->mutex);
3551
3552 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
3553 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
3554
Achiad Shochat2811ba52015-12-23 18:47:24 +02003555 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
3556 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003557 }
3558
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003559 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
3560 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
3561 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
3562 attr_mask);
3563 goto out;
3564 }
3565 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02003566 qp_type != MLX5_IB_QPT_DCI &&
Kamal Heibd31131b2018-10-02 16:11:21 +03003567 !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
3568 attr_mask)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003569 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3570 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003571 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02003572 } else if (qp_type == MLX5_IB_QPT_DCI &&
3573 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
3574 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3575 cur_state, new_state, qp_type, attr_mask);
3576 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003577 }
Eli Cohene126ba92013-07-07 17:25:49 +03003578
3579 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003580 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003581 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003582 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3583 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003584 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003585 }
Eli Cohene126ba92013-07-07 17:25:49 +03003586
3587 if (attr_mask & IB_QP_PKEY_INDEX) {
3588 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003589 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02003590 dev->mdev->port_caps[port - 1].pkey_table_len) {
3591 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
3592 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003593 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003594 }
Eli Cohene126ba92013-07-07 17:25:49 +03003595 }
3596
3597 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003598 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003599 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
3600 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
3601 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003602 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003603 }
Eli Cohene126ba92013-07-07 17:25:49 +03003604
3605 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003606 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003607 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
3608 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
3609 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003610 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003611 }
Eli Cohene126ba92013-07-07 17:25:49 +03003612
3613 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
3614 err = 0;
3615 goto out;
3616 }
3617
Bodong Wang61147f32018-03-19 15:10:30 +02003618 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
3619 new_state, &ucmd);
Eli Cohene126ba92013-07-07 17:25:49 +03003620
3621out:
3622 mutex_unlock(&qp->mutex);
3623 return err;
3624}
3625
3626static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
3627{
3628 struct mlx5_ib_cq *cq;
3629 unsigned cur;
3630
3631 cur = wq->head - wq->tail;
3632 if (likely(cur + nreq < wq->max_post))
3633 return 0;
3634
3635 cq = to_mcq(ib_cq);
3636 spin_lock(&cq->lock);
3637 cur = wq->head - wq->tail;
3638 spin_unlock(&cq->lock);
3639
3640 return cur + nreq >= wq->max_post;
3641}
3642
3643static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
3644 u64 remote_addr, u32 rkey)
3645{
3646 rseg->raddr = cpu_to_be64(remote_addr);
3647 rseg->rkey = cpu_to_be32(rkey);
3648 rseg->reserved = 0;
3649}
3650
Erez Shitritf0313962016-02-21 16:27:17 +02003651static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07003652 const struct ib_send_wr *wr, void *qend,
Erez Shitritf0313962016-02-21 16:27:17 +02003653 struct mlx5_ib_qp *qp, int *size)
3654{
3655 void *seg = eseg;
3656
3657 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
3658
3659 if (wr->send_flags & IB_SEND_IP_CSUM)
3660 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
3661 MLX5_ETH_WQE_L4_CSUM;
3662
3663 seg += sizeof(struct mlx5_wqe_eth_seg);
3664 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3665
3666 if (wr->opcode == IB_WR_LSO) {
3667 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003668 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr.start);
Erez Shitritf0313962016-02-21 16:27:17 +02003669 u64 left, leftlen, copysz;
3670 void *pdata = ud_wr->header;
3671
3672 left = ud_wr->hlen;
3673 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003674 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02003675
3676 /*
3677 * check if there is space till the end of queue, if yes,
3678 * copy all in one shot, otherwise copy till the end of queue,
3679 * rollback and than the copy the left
3680 */
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003681 leftlen = qend - (void *)eseg->inline_hdr.start;
Erez Shitritf0313962016-02-21 16:27:17 +02003682 copysz = min_t(u64, leftlen, left);
3683
3684 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3685
3686 if (likely(copysz > size_of_inl_hdr_start)) {
3687 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3688 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3689 }
3690
3691 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3692 seg = mlx5_get_send_wqe(qp, 0);
3693 left -= copysz;
3694 pdata += copysz;
3695 memcpy(seg, pdata, left);
3696 seg += ALIGN(left, 16);
3697 *size += ALIGN(left, 16) / 16;
3698 }
3699 }
3700
3701 return seg;
3702}
3703
Eli Cohene126ba92013-07-07 17:25:49 +03003704static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07003705 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03003706{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003707 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3708 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3709 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003710}
3711
3712static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3713{
3714 dseg->byte_count = cpu_to_be32(sg->length);
3715 dseg->lkey = cpu_to_be32(sg->lkey);
3716 dseg->addr = cpu_to_be64(sg->addr);
3717}
3718
Artemy Kovalyov31616252017-01-02 11:37:42 +02003719static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03003720{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003721 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
3722 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03003723}
3724
3725static __be64 frwr_mkey_mask(void)
3726{
3727 u64 result;
3728
3729 result = MLX5_MKEY_MASK_LEN |
3730 MLX5_MKEY_MASK_PAGE_SIZE |
3731 MLX5_MKEY_MASK_START_ADDR |
3732 MLX5_MKEY_MASK_EN_RINVAL |
3733 MLX5_MKEY_MASK_KEY |
3734 MLX5_MKEY_MASK_LR |
3735 MLX5_MKEY_MASK_LW |
3736 MLX5_MKEY_MASK_RR |
3737 MLX5_MKEY_MASK_RW |
3738 MLX5_MKEY_MASK_A |
3739 MLX5_MKEY_MASK_SMALL_FENCE |
3740 MLX5_MKEY_MASK_FREE;
3741
3742 return cpu_to_be64(result);
3743}
3744
Sagi Grimberge6631812014-02-23 14:19:11 +02003745static __be64 sig_mkey_mask(void)
3746{
3747 u64 result;
3748
3749 result = MLX5_MKEY_MASK_LEN |
3750 MLX5_MKEY_MASK_PAGE_SIZE |
3751 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003752 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003753 MLX5_MKEY_MASK_EN_RINVAL |
3754 MLX5_MKEY_MASK_KEY |
3755 MLX5_MKEY_MASK_LR |
3756 MLX5_MKEY_MASK_LW |
3757 MLX5_MKEY_MASK_RR |
3758 MLX5_MKEY_MASK_RW |
3759 MLX5_MKEY_MASK_SMALL_FENCE |
3760 MLX5_MKEY_MASK_FREE |
3761 MLX5_MKEY_MASK_BSF_EN;
3762
3763 return cpu_to_be64(result);
3764}
3765
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003766static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Idan Burstein064e5262018-05-02 13:16:39 +03003767 struct mlx5_ib_mr *mr, bool umr_inline)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003768{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003769 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003770
3771 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003772
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003773 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Idan Burstein064e5262018-05-02 13:16:39 +03003774 if (umr_inline)
3775 umr->flags |= MLX5_UMR_INLINE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003776 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003777 umr->mkey_mask = frwr_mkey_mask();
3778}
3779
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003780static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003781{
3782 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003783 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003784 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003785}
3786
Artemy Kovalyov31616252017-01-02 11:37:42 +02003787static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003788{
3789 u64 result;
3790
Artemy Kovalyov31616252017-01-02 11:37:42 +02003791 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02003792 MLX5_MKEY_MASK_FREE;
3793
3794 return cpu_to_be64(result);
3795}
3796
Artemy Kovalyov31616252017-01-02 11:37:42 +02003797static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003798{
3799 u64 result;
3800
3801 result = MLX5_MKEY_MASK_FREE;
3802
3803 return cpu_to_be64(result);
3804}
3805
Noa Osherovich56e11d62016-02-29 16:46:51 +02003806static __be64 get_umr_update_translation_mask(void)
3807{
3808 u64 result;
3809
3810 result = MLX5_MKEY_MASK_LEN |
3811 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003812 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003813
3814 return cpu_to_be64(result);
3815}
3816
Artemy Kovalyov31616252017-01-02 11:37:42 +02003817static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02003818{
3819 u64 result;
3820
Artemy Kovalyov31616252017-01-02 11:37:42 +02003821 result = MLX5_MKEY_MASK_LR |
3822 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003823 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003824 MLX5_MKEY_MASK_RW;
3825
3826 if (atomic)
3827 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003828
3829 return cpu_to_be64(result);
3830}
3831
3832static __be64 get_umr_update_pd_mask(void)
3833{
3834 u64 result;
3835
Artemy Kovalyov31616252017-01-02 11:37:42 +02003836 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003837
3838 return cpu_to_be64(result);
3839}
3840
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02003841static int umr_check_mkey_mask(struct mlx5_ib_dev *dev, u64 mask)
3842{
3843 if ((mask & MLX5_MKEY_MASK_PAGE_SIZE &&
3844 MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) ||
3845 (mask & MLX5_MKEY_MASK_A &&
3846 MLX5_CAP_GEN(dev->mdev, umr_modify_atomic_disabled)))
3847 return -EPERM;
3848 return 0;
3849}
3850
3851static int set_reg_umr_segment(struct mlx5_ib_dev *dev,
3852 struct mlx5_wqe_umr_ctrl_seg *umr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07003853 const struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003854{
Bart Van Asschef696bf62018-07-18 09:25:14 -07003855 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003856
3857 memset(umr, 0, sizeof(*umr));
3858
Haggai Eran968e78d2014-12-11 17:04:11 +02003859 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3860 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3861 else
3862 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3863
Artemy Kovalyov31616252017-01-02 11:37:42 +02003864 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
3865 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
3866 u64 offset = get_xlt_octo(umrwr->offset);
3867
3868 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
3869 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
3870 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003871 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02003872 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3873 umr->mkey_mask |= get_umr_update_translation_mask();
3874 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
3875 umr->mkey_mask |= get_umr_update_access_mask(atomic);
3876 umr->mkey_mask |= get_umr_update_pd_mask();
3877 }
3878 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
3879 umr->mkey_mask |= get_umr_enable_mr_mask();
3880 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
3881 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003882
3883 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003884 umr->flags |= MLX5_UMR_INLINE;
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02003885
3886 return umr_check_mkey_mask(dev, be64_to_cpu(umr->mkey_mask));
Eli Cohene126ba92013-07-07 17:25:49 +03003887}
3888
3889static u8 get_umr_flags(int acc)
3890{
3891 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3892 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3893 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3894 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003895 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003896}
3897
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003898static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3899 struct mlx5_ib_mr *mr,
3900 u32 key, int access)
3901{
3902 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3903
3904 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003905
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003906 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003907 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003908 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003909 /* KLMs take twice the size of MTTs */
3910 ndescs *= 2;
3911
3912 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003913 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3914 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3915 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3916 seg->len = cpu_to_be64(mr->ibmr.length);
3917 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003918}
3919
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003920static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003921{
3922 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003923 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003924}
3925
Bart Van Asschef696bf62018-07-18 09:25:14 -07003926static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg,
3927 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03003928{
Bart Van Asschef696bf62018-07-18 09:25:14 -07003929 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003930
Eli Cohene126ba92013-07-07 17:25:49 +03003931 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02003932 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02003933 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003934
Haggai Eran968e78d2014-12-11 17:04:11 +02003935 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003936 if (umrwr->pd)
3937 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
3938 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
3939 !umrwr->length)
3940 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
3941
3942 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003943 seg->len = cpu_to_be64(umrwr->length);
3944 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003945 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003946 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003947}
3948
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003949static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3950 struct mlx5_ib_mr *mr,
3951 struct mlx5_ib_pd *pd)
3952{
3953 int bcount = mr->desc_size * mr->ndescs;
3954
3955 dseg->addr = cpu_to_be64(mr->desc_map);
3956 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3957 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3958}
3959
Idan Burstein064e5262018-05-02 13:16:39 +03003960static void set_reg_umr_inline_seg(void *seg, struct mlx5_ib_qp *qp,
3961 struct mlx5_ib_mr *mr, int mr_list_size)
3962{
3963 void *qend = qp->sq.qend;
3964 void *addr = mr->descs;
3965 int copy;
3966
3967 if (unlikely(seg + mr_list_size > qend)) {
3968 copy = qend - seg;
3969 memcpy(seg, addr, copy);
3970 addr += copy;
3971 mr_list_size -= copy;
3972 seg = mlx5_get_send_wqe(qp, 0);
3973 }
3974 memcpy(seg, addr, mr_list_size);
3975 seg += mr_list_size;
3976}
3977
Bart Van Asschef696bf62018-07-18 09:25:14 -07003978static __be32 send_ieth(const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03003979{
3980 switch (wr->opcode) {
3981 case IB_WR_SEND_WITH_IMM:
3982 case IB_WR_RDMA_WRITE_WITH_IMM:
3983 return wr->ex.imm_data;
3984
3985 case IB_WR_SEND_WITH_INV:
3986 return cpu_to_be32(wr->ex.invalidate_rkey);
3987
3988 default:
3989 return 0;
3990 }
3991}
3992
3993static u8 calc_sig(void *wqe, int size)
3994{
3995 u8 *p = wqe;
3996 u8 res = 0;
3997 int i;
3998
3999 for (i = 0; i < size; i++)
4000 res ^= p[i];
4001
4002 return ~res;
4003}
4004
4005static u8 wq_sig(void *wqe)
4006{
4007 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
4008}
4009
Bart Van Asschef696bf62018-07-18 09:25:14 -07004010static int set_data_inl_seg(struct mlx5_ib_qp *qp, const struct ib_send_wr *wr,
Eli Cohene126ba92013-07-07 17:25:49 +03004011 void *wqe, int *sz)
4012{
4013 struct mlx5_wqe_inline_seg *seg;
4014 void *qend = qp->sq.qend;
4015 void *addr;
4016 int inl = 0;
4017 int copy;
4018 int len;
4019 int i;
4020
4021 seg = wqe;
4022 wqe += sizeof(*seg);
4023 for (i = 0; i < wr->num_sge; i++) {
4024 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
4025 len = wr->sg_list[i].length;
4026 inl += len;
4027
4028 if (unlikely(inl > qp->max_inline_data))
4029 return -ENOMEM;
4030
4031 if (unlikely(wqe + len > qend)) {
4032 copy = qend - wqe;
4033 memcpy(wqe, addr, copy);
4034 addr += copy;
4035 len -= copy;
4036 wqe = mlx5_get_send_wqe(qp, 0);
4037 }
4038 memcpy(wqe, addr, len);
4039 wqe += len;
4040 }
4041
4042 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
4043
4044 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
4045
4046 return 0;
4047}
4048
Sagi Grimberge6631812014-02-23 14:19:11 +02004049static u16 prot_field_size(enum ib_signature_type type)
4050{
4051 switch (type) {
4052 case IB_SIG_TYPE_T10_DIF:
4053 return MLX5_DIF_SIZE;
4054 default:
4055 return 0;
4056 }
4057}
4058
4059static u8 bs_selector(int block_size)
4060{
4061 switch (block_size) {
4062 case 512: return 0x1;
4063 case 520: return 0x2;
4064 case 4096: return 0x3;
4065 case 4160: return 0x4;
4066 case 1073741824: return 0x5;
4067 default: return 0;
4068 }
4069}
4070
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004071static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
4072 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02004073{
Sagi Grimberg142537f2014-08-13 19:54:32 +03004074 /* Valid inline section and allow BSF refresh */
4075 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
4076 MLX5_BSF_REFRESH_DIF);
4077 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
4078 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004079 /* repeating block */
4080 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
4081 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
4082 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004083
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004084 if (domain->sig.dif.ref_remap)
4085 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02004086
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004087 if (domain->sig.dif.app_escape) {
4088 if (domain->sig.dif.ref_escape)
4089 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
4090 else
4091 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02004092 }
4093
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004094 inl->dif_app_bitmask_check =
4095 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02004096}
4097
4098static int mlx5_set_bsf(struct ib_mr *sig_mr,
4099 struct ib_sig_attrs *sig_attrs,
4100 struct mlx5_bsf *bsf, u32 data_size)
4101{
4102 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
4103 struct mlx5_bsf_basic *basic = &bsf->basic;
4104 struct ib_sig_domain *mem = &sig_attrs->mem;
4105 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02004106
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004107 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02004108
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004109 /* Basic + Extended + Inline */
4110 basic->bsf_size_sbs = 1 << 7;
4111 /* Input domain check byte mask */
4112 basic->check_byte_mask = sig_attrs->check_mask;
4113 basic->raw_data_size = cpu_to_be32(data_size);
4114
4115 /* Memory domain */
4116 switch (sig_attrs->mem.sig_type) {
4117 case IB_SIG_TYPE_NONE:
4118 break;
4119 case IB_SIG_TYPE_T10_DIF:
4120 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
4121 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
4122 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
4123 break;
4124 default:
4125 return -EINVAL;
4126 }
4127
4128 /* Wire domain */
4129 switch (sig_attrs->wire.sig_type) {
4130 case IB_SIG_TYPE_NONE:
4131 break;
4132 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02004133 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004134 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004135 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03004136 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02004137 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004138 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004139 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004140 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004141 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004142 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02004143 } else
4144 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
4145
Sagi Grimberg142537f2014-08-13 19:54:32 +03004146 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004147 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02004148 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004149 default:
4150 return -EINVAL;
4151 }
4152
4153 return 0;
4154}
4155
Bart Van Asschef696bf62018-07-18 09:25:14 -07004156static int set_sig_data_segment(const struct ib_sig_handover_wr *wr,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004157 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004158{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004159 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
4160 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004161 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004162 u32 data_len = wr->wr.sg_list->length;
4163 u32 data_key = wr->wr.sg_list->lkey;
4164 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004165 int ret;
4166 int wqe_size;
4167
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004168 if (!wr->prot ||
4169 (data_key == wr->prot->lkey &&
4170 data_va == wr->prot->addr &&
4171 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004172 /**
4173 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004174 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02004175 * So need construct:
4176 * ------------------
4177 * | data_klm |
4178 * ------------------
4179 * | BSF |
4180 * ------------------
4181 **/
4182 struct mlx5_klm *data_klm = *seg;
4183
4184 data_klm->bcount = cpu_to_be32(data_len);
4185 data_klm->key = cpu_to_be32(data_key);
4186 data_klm->va = cpu_to_be64(data_va);
4187 wqe_size = ALIGN(sizeof(*data_klm), 64);
4188 } else {
4189 /**
4190 * Source domain contains signature information
4191 * So need construct a strided block format:
4192 * ---------------------------
4193 * | stride_block_ctrl |
4194 * ---------------------------
4195 * | data_klm |
4196 * ---------------------------
4197 * | prot_klm |
4198 * ---------------------------
4199 * | BSF |
4200 * ---------------------------
4201 **/
4202 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
4203 struct mlx5_stride_block_entry *data_sentry;
4204 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004205 u32 prot_key = wr->prot->lkey;
4206 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004207 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
4208 int prot_size;
4209
4210 sblock_ctrl = *seg;
4211 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
4212 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
4213
4214 prot_size = prot_field_size(sig_attrs->mem.sig_type);
4215 if (!prot_size) {
4216 pr_err("Bad block size given: %u\n", block_size);
4217 return -EINVAL;
4218 }
4219 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
4220 prot_size);
4221 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
4222 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
4223 sblock_ctrl->num_entries = cpu_to_be16(2);
4224
4225 data_sentry->bcount = cpu_to_be16(block_size);
4226 data_sentry->key = cpu_to_be32(data_key);
4227 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004228 data_sentry->stride = cpu_to_be16(block_size);
4229
Sagi Grimberge6631812014-02-23 14:19:11 +02004230 prot_sentry->bcount = cpu_to_be16(prot_size);
4231 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004232 prot_sentry->va = cpu_to_be64(prot_va);
4233 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004234
Sagi Grimberge6631812014-02-23 14:19:11 +02004235 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
4236 sizeof(*prot_sentry), 64);
4237 }
4238
4239 *seg += wqe_size;
4240 *size += wqe_size / 16;
4241 if (unlikely((*seg == qp->sq.qend)))
4242 *seg = mlx5_get_send_wqe(qp, 0);
4243
4244 bsf = *seg;
4245 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
4246 if (ret)
4247 return -EINVAL;
4248
4249 *seg += sizeof(*bsf);
4250 *size += sizeof(*bsf) / 16;
4251 if (unlikely((*seg == qp->sq.qend)))
4252 *seg = mlx5_get_send_wqe(qp, 0);
4253
4254 return 0;
4255}
4256
4257static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004258 const struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02004259 u32 length, u32 pdn)
4260{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004261 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004262 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004263 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004264
4265 memset(seg, 0, sizeof(*seg));
4266
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004267 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004268 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004269 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004270 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004271 MLX5_MKEY_BSF_EN | pdn);
4272 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004273 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004274 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4275}
4276
4277static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004278 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004279{
4280 memset(umr, 0, sizeof(*umr));
4281
4282 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004283 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004284 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4285 umr->mkey_mask = sig_mkey_mask();
4286}
4287
4288
Bart Van Asschef696bf62018-07-18 09:25:14 -07004289static int set_sig_umr_wr(const struct ib_send_wr *send_wr,
4290 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004291{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004292 const struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004293 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004294 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004295 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02004296 int region_len, ret;
4297
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004298 if (unlikely(wr->wr.num_sge != 1) ||
4299 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004300 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
4301 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02004302 return -EINVAL;
4303
4304 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004305 region_len = wr->wr.sg_list->length;
4306 if (wr->prot &&
4307 (wr->prot->lkey != wr->wr.sg_list->lkey ||
4308 wr->prot->addr != wr->wr.sg_list->addr ||
4309 wr->prot->length != wr->wr.sg_list->length))
4310 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02004311
4312 /**
4313 * KLM octoword size - if protection was provided
4314 * then we use strided block format (3 octowords),
4315 * else we use single KLM (1 octoword)
4316 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02004317 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02004318
Artemy Kovalyov31616252017-01-02 11:37:42 +02004319 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004320 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4321 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4322 if (unlikely((*seg == qp->sq.qend)))
4323 *seg = mlx5_get_send_wqe(qp, 0);
4324
Artemy Kovalyov31616252017-01-02 11:37:42 +02004325 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02004326 *seg += sizeof(struct mlx5_mkey_seg);
4327 *size += sizeof(struct mlx5_mkey_seg) / 16;
4328 if (unlikely((*seg == qp->sq.qend)))
4329 *seg = mlx5_get_send_wqe(qp, 0);
4330
4331 ret = set_sig_data_segment(wr, qp, seg, size);
4332 if (ret)
4333 return ret;
4334
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004335 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004336 return 0;
4337}
4338
4339static int set_psv_wr(struct ib_sig_domain *domain,
4340 u32 psv_idx, void **seg, int *size)
4341{
4342 struct mlx5_seg_set_psv *psv_seg = *seg;
4343
4344 memset(psv_seg, 0, sizeof(*psv_seg));
4345 psv_seg->psv_num = cpu_to_be32(psv_idx);
4346 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004347 case IB_SIG_TYPE_NONE:
4348 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004349 case IB_SIG_TYPE_T10_DIF:
4350 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4351 domain->sig.dif.app_tag);
4352 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004353 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004354 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004355 pr_err("Bad signature type (%d) is given.\n",
4356 domain->sig_type);
4357 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004358 }
4359
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004360 *seg += sizeof(*psv_seg);
4361 *size += sizeof(*psv_seg) / 16;
4362
Sagi Grimberge6631812014-02-23 14:19:11 +02004363 return 0;
4364}
4365
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004366static int set_reg_wr(struct mlx5_ib_qp *qp,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004367 const struct ib_reg_wr *wr,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004368 void **seg, int *size)
4369{
4370 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4371 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
Idan Burstein064e5262018-05-02 13:16:39 +03004372 int mr_list_size = mr->ndescs * mr->desc_size;
4373 bool umr_inline = mr_list_size <= MLX5_IB_SQ_UMR_INLINE_THRESHOLD;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004374
4375 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4376 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4377 "Invalid IB_SEND_INLINE send flag\n");
4378 return -EINVAL;
4379 }
4380
Idan Burstein064e5262018-05-02 13:16:39 +03004381 set_reg_umr_seg(*seg, mr, umr_inline);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004382 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4383 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4384 if (unlikely((*seg == qp->sq.qend)))
4385 *seg = mlx5_get_send_wqe(qp, 0);
4386
4387 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4388 *seg += sizeof(struct mlx5_mkey_seg);
4389 *size += sizeof(struct mlx5_mkey_seg) / 16;
4390 if (unlikely((*seg == qp->sq.qend)))
4391 *seg = mlx5_get_send_wqe(qp, 0);
4392
Idan Burstein064e5262018-05-02 13:16:39 +03004393 if (umr_inline) {
4394 set_reg_umr_inline_seg(*seg, qp, mr, mr_list_size);
4395 *size += get_xlt_octo(mr_list_size);
4396 } else {
4397 set_reg_data_seg(*seg, mr, pd);
4398 *seg += sizeof(struct mlx5_wqe_data_seg);
4399 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4400 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004401 return 0;
4402}
4403
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004404static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
Eli Cohene126ba92013-07-07 17:25:49 +03004405{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004406 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004407 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4408 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4409 if (unlikely((*seg == qp->sq.qend)))
4410 *seg = mlx5_get_send_wqe(qp, 0);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004411 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004412 *seg += sizeof(struct mlx5_mkey_seg);
4413 *size += sizeof(struct mlx5_mkey_seg) / 16;
4414 if (unlikely((*seg == qp->sq.qend)))
4415 *seg = mlx5_get_send_wqe(qp, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004416}
4417
4418static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
4419{
4420 __be32 *p = NULL;
4421 int tidx = idx;
4422 int i, j;
4423
4424 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
4425 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4426 if ((i & 0xf) == 0) {
4427 void *buf = mlx5_get_send_wqe(qp, tidx);
4428 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
4429 p = buf;
4430 j = 0;
4431 }
4432 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4433 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4434 be32_to_cpu(p[j + 3]));
4435 }
4436}
4437
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004438static int __begin_wqe(struct mlx5_ib_qp *qp, void **seg,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004439 struct mlx5_wqe_ctrl_seg **ctrl,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004440 const struct ib_send_wr *wr, unsigned *idx,
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004441 int *size, int nreq, bool send_signaled, bool solicited)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004442{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004443 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4444 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004445
4446 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
4447 *seg = mlx5_get_send_wqe(qp, *idx);
4448 *ctrl = *seg;
4449 *(uint32_t *)(*seg + 8) = 0;
4450 (*ctrl)->imm = send_ieth(wr);
4451 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004452 (send_signaled ? MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4453 (solicited ? MLX5_WQE_CTRL_SOLICITED : 0);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004454
4455 *seg += sizeof(**ctrl);
4456 *size = sizeof(**ctrl) / 16;
4457
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004458 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004459}
4460
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004461static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4462 struct mlx5_wqe_ctrl_seg **ctrl,
4463 const struct ib_send_wr *wr, unsigned *idx,
4464 int *size, int nreq)
4465{
4466 return __begin_wqe(qp, seg, ctrl, wr, idx, size, nreq,
4467 wr->send_flags & IB_SEND_SIGNALED,
4468 wr->send_flags & IB_SEND_SOLICITED);
4469}
4470
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004471static void finish_wqe(struct mlx5_ib_qp *qp,
4472 struct mlx5_wqe_ctrl_seg *ctrl,
4473 u8 size, unsigned idx, u64 wr_id,
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004474 int nreq, u8 fence, u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004475{
4476 u8 opmod = 0;
4477
4478 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
4479 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02004480 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004481 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004482 if (unlikely(qp->wq_sig))
4483 ctrl->signature = wq_sig(ctrl);
4484
4485 qp->sq.wrid[idx] = wr_id;
4486 qp->sq.w_list[idx].opcode = mlx5_opcode;
4487 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
4488 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
4489 qp->sq.w_list[idx].next = qp->sq.cur_post;
4490}
4491
Bart Van Assched34ac5c2018-07-18 09:25:32 -07004492static int _mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
4493 const struct ib_send_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03004494{
4495 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
4496 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004497 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02004498 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02004499 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004500 struct mlx5_wqe_data_seg *dpseg;
4501 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02004502 struct mlx5_bf *bf;
Eli Cohene126ba92013-07-07 17:25:49 +03004503 int uninitialized_var(size);
Haggai Erand16e91d2016-02-29 15:45:05 +02004504 void *qend;
Eli Cohene126ba92013-07-07 17:25:49 +03004505 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03004506 unsigned idx;
4507 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004508 int num_sge;
4509 void *seg;
4510 int nreq;
4511 int i;
4512 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004513 u8 fence;
4514
Parav Pandit6c755202018-08-28 14:45:29 +03004515 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
4516 !drain)) {
4517 *bad_wr = wr;
4518 return -EIO;
4519 }
4520
Haggai Erand16e91d2016-02-29 15:45:05 +02004521 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4522 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
4523
4524 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004525 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02004526 qend = qp->sq.qend;
4527
Eli Cohene126ba92013-07-07 17:25:49 +03004528 spin_lock_irqsave(&qp->sq.lock, flags);
4529
4530 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04004531 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03004532 mlx5_ib_warn(dev, "\n");
4533 err = -EINVAL;
4534 *bad_wr = wr;
4535 goto out;
4536 }
4537
Eli Cohene126ba92013-07-07 17:25:49 +03004538 num_sge = wr->num_sge;
4539 if (unlikely(num_sge > qp->sq.max_gs)) {
4540 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03004541 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03004542 *bad_wr = wr;
4543 goto out;
4544 }
4545
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004546 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
4547 if (err) {
4548 mlx5_ib_warn(dev, "\n");
4549 err = -ENOMEM;
4550 *bad_wr = wr;
4551 goto out;
4552 }
Eli Cohene126ba92013-07-07 17:25:49 +03004553
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004554 if (wr->opcode == IB_WR_LOCAL_INV ||
4555 wr->opcode == IB_WR_REG_MR) {
4556 fence = dev->umr_fence;
4557 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
4558 } else if (wr->send_flags & IB_SEND_FENCE) {
4559 if (qp->next_fence)
4560 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
4561 else
4562 fence = MLX5_FENCE_MODE_FENCE;
4563 } else {
4564 fence = qp->next_fence;
4565 }
4566
Eli Cohene126ba92013-07-07 17:25:49 +03004567 switch (ibqp->qp_type) {
4568 case IB_QPT_XRC_INI:
4569 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03004570 seg += sizeof(*xrc);
4571 size += sizeof(*xrc) / 16;
4572 /* fall through */
4573 case IB_QPT_RC:
4574 switch (wr->opcode) {
4575 case IB_WR_RDMA_READ:
4576 case IB_WR_RDMA_WRITE:
4577 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004578 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4579 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004580 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004581 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4582 break;
4583
4584 case IB_WR_ATOMIC_CMP_AND_SWP:
4585 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03004586 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03004587 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
4588 err = -ENOSYS;
4589 *bad_wr = wr;
4590 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004591
4592 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03004593 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
4594 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004595 set_linv_wr(qp, &seg, &size);
Eli Cohene126ba92013-07-07 17:25:49 +03004596 num_sge = 0;
4597 break;
4598
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004599 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004600 qp->sq.wr_data[idx] = IB_WR_REG_MR;
4601 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
4602 err = set_reg_wr(qp, reg_wr(wr), &seg, &size);
4603 if (err) {
4604 *bad_wr = wr;
4605 goto out;
4606 }
4607 num_sge = 0;
4608 break;
4609
Sagi Grimberge6631812014-02-23 14:19:11 +02004610 case IB_WR_REG_SIG_MR:
4611 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004612 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004613
4614 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
4615 err = set_sig_umr_wr(wr, qp, &seg, &size);
4616 if (err) {
4617 mlx5_ib_warn(dev, "\n");
4618 *bad_wr = wr;
4619 goto out;
4620 }
4621
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004622 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4623 fence, MLX5_OPCODE_UMR);
Sagi Grimberge6631812014-02-23 14:19:11 +02004624 /*
4625 * SET_PSV WQEs are not signaled and solicited
4626 * on error
4627 */
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004628 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
4629 &size, nreq, false, true);
Sagi Grimberge6631812014-02-23 14:19:11 +02004630 if (err) {
4631 mlx5_ib_warn(dev, "\n");
4632 err = -ENOMEM;
4633 *bad_wr = wr;
4634 goto out;
4635 }
4636
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004637 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02004638 mr->sig->psv_memory.psv_idx, &seg,
4639 &size);
4640 if (err) {
4641 mlx5_ib_warn(dev, "\n");
4642 *bad_wr = wr;
4643 goto out;
4644 }
4645
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004646 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4647 fence, MLX5_OPCODE_SET_PSV);
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004648 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
4649 &size, nreq, false, true);
Sagi Grimberge6631812014-02-23 14:19:11 +02004650 if (err) {
4651 mlx5_ib_warn(dev, "\n");
4652 err = -ENOMEM;
4653 *bad_wr = wr;
4654 goto out;
4655 }
4656
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004657 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02004658 mr->sig->psv_wire.psv_idx, &seg,
4659 &size);
4660 if (err) {
4661 mlx5_ib_warn(dev, "\n");
4662 *bad_wr = wr;
4663 goto out;
4664 }
4665
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004666 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4667 fence, MLX5_OPCODE_SET_PSV);
4668 qp->next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004669 num_sge = 0;
4670 goto skip_psv;
4671
Eli Cohene126ba92013-07-07 17:25:49 +03004672 default:
4673 break;
4674 }
4675 break;
4676
4677 case IB_QPT_UC:
4678 switch (wr->opcode) {
4679 case IB_WR_RDMA_WRITE:
4680 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004681 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4682 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004683 seg += sizeof(struct mlx5_wqe_raddr_seg);
4684 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4685 break;
4686
4687 default:
4688 break;
4689 }
4690 break;
4691
Eli Cohene126ba92013-07-07 17:25:49 +03004692 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02004693 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
4694 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
4695 err = -EPERM;
4696 *bad_wr = wr;
4697 goto out;
4698 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07004699 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02004700 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004701 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004702 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004703 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4704 if (unlikely((seg == qend)))
4705 seg = mlx5_get_send_wqe(qp, 0);
4706 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004707 case IB_QPT_UD:
4708 set_datagram_seg(seg, wr);
4709 seg += sizeof(struct mlx5_wqe_datagram_seg);
4710 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004711
Erez Shitritf0313962016-02-21 16:27:17 +02004712 if (unlikely((seg == qend)))
4713 seg = mlx5_get_send_wqe(qp, 0);
4714
4715 /* handle qp that supports ud offload */
4716 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4717 struct mlx5_wqe_eth_pad *pad;
4718
4719 pad = seg;
4720 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4721 seg += sizeof(struct mlx5_wqe_eth_pad);
4722 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4723
4724 seg = set_eth_seg(seg, wr, qend, qp, &size);
4725
4726 if (unlikely((seg == qend)))
4727 seg = mlx5_get_send_wqe(qp, 0);
4728 }
4729 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004730 case MLX5_IB_QPT_REG_UMR:
4731 if (wr->opcode != MLX5_IB_WR_UMR) {
4732 err = -EINVAL;
4733 mlx5_ib_warn(dev, "bad opcode\n");
4734 goto out;
4735 }
4736 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004737 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004738 err = set_reg_umr_segment(dev, seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
4739 if (unlikely(err))
4740 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004741 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4742 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4743 if (unlikely((seg == qend)))
4744 seg = mlx5_get_send_wqe(qp, 0);
4745 set_reg_mkey_segment(seg, wr);
4746 seg += sizeof(struct mlx5_mkey_seg);
4747 size += sizeof(struct mlx5_mkey_seg) / 16;
4748 if (unlikely((seg == qend)))
4749 seg = mlx5_get_send_wqe(qp, 0);
4750 break;
4751
4752 default:
4753 break;
4754 }
4755
4756 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4757 int uninitialized_var(sz);
4758
4759 err = set_data_inl_seg(qp, wr, seg, &sz);
4760 if (unlikely(err)) {
4761 mlx5_ib_warn(dev, "\n");
4762 *bad_wr = wr;
4763 goto out;
4764 }
Eli Cohene126ba92013-07-07 17:25:49 +03004765 size += sz;
4766 } else {
4767 dpseg = seg;
4768 for (i = 0; i < num_sge; i++) {
4769 if (unlikely(dpseg == qend)) {
4770 seg = mlx5_get_send_wqe(qp, 0);
4771 dpseg = seg;
4772 }
4773 if (likely(wr->sg_list[i].length)) {
4774 set_data_ptr_seg(dpseg, wr->sg_list + i);
4775 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4776 dpseg++;
4777 }
4778 }
4779 }
4780
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004781 qp->next_fence = next_fence;
4782 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq, fence,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004783 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004784skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004785 if (0)
4786 dump_wqe(qp, idx, size);
4787 }
4788
4789out:
4790 if (likely(nreq)) {
4791 qp->sq.head += nreq;
4792
4793 /* Make sure that descriptors are written before
4794 * updating doorbell record and ringing the doorbell
4795 */
4796 wmb();
4797
4798 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4799
Eli Cohenada388f2014-01-14 17:45:16 +02004800 /* Make sure doorbell record is visible to the HCA before
4801 * we hit doorbell */
4802 wmb();
4803
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004804 /* currently we support only regular doorbells */
4805 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset, NULL);
4806 /* Make sure doorbells don't leak out of SQ spinlock
4807 * and reach the HCA out of order.
4808 */
4809 mmiowb();
Eli Cohene126ba92013-07-07 17:25:49 +03004810 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03004811 }
4812
4813 spin_unlock_irqrestore(&qp->sq.lock, flags);
4814
4815 return err;
4816}
4817
Bart Van Assched34ac5c2018-07-18 09:25:32 -07004818int mlx5_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
4819 const struct ib_send_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03004820{
4821 return _mlx5_ib_post_send(ibqp, wr, bad_wr, false);
4822}
4823
Eli Cohene126ba92013-07-07 17:25:49 +03004824static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4825{
4826 sig->signature = calc_sig(sig, size);
4827}
4828
Bart Van Assched34ac5c2018-07-18 09:25:32 -07004829static int _mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
4830 const struct ib_recv_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03004831{
4832 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4833 struct mlx5_wqe_data_seg *scat;
4834 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004835 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4836 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004837 unsigned long flags;
4838 int err = 0;
4839 int nreq;
4840 int ind;
4841 int i;
4842
Parav Pandit6c755202018-08-28 14:45:29 +03004843 if (unlikely(mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR &&
4844 !drain)) {
4845 *bad_wr = wr;
4846 return -EIO;
4847 }
4848
Haggai Erand16e91d2016-02-29 15:45:05 +02004849 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4850 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4851
Eli Cohene126ba92013-07-07 17:25:49 +03004852 spin_lock_irqsave(&qp->rq.lock, flags);
4853
4854 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4855
4856 for (nreq = 0; wr; nreq++, wr = wr->next) {
4857 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4858 err = -ENOMEM;
4859 *bad_wr = wr;
4860 goto out;
4861 }
4862
4863 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4864 err = -EINVAL;
4865 *bad_wr = wr;
4866 goto out;
4867 }
4868
4869 scat = get_recv_wqe(qp, ind);
4870 if (qp->wq_sig)
4871 scat++;
4872
4873 for (i = 0; i < wr->num_sge; i++)
4874 set_data_ptr_seg(scat + i, wr->sg_list + i);
4875
4876 if (i < qp->rq.max_gs) {
4877 scat[i].byte_count = 0;
4878 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4879 scat[i].addr = 0;
4880 }
4881
4882 if (qp->wq_sig) {
4883 sig = (struct mlx5_rwqe_sig *)scat;
4884 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4885 }
4886
4887 qp->rq.wrid[ind] = wr->wr_id;
4888
4889 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4890 }
4891
4892out:
4893 if (likely(nreq)) {
4894 qp->rq.head += nreq;
4895
4896 /* Make sure that descriptors are written before
4897 * doorbell record.
4898 */
4899 wmb();
4900
4901 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4902 }
4903
4904 spin_unlock_irqrestore(&qp->rq.lock, flags);
4905
4906 return err;
4907}
4908
Bart Van Assched34ac5c2018-07-18 09:25:32 -07004909int mlx5_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
4910 const struct ib_recv_wr **bad_wr)
Yishai Hadasd0e84c02018-06-19 10:43:55 +03004911{
4912 return _mlx5_ib_post_recv(ibqp, wr, bad_wr, false);
4913}
4914
Eli Cohene126ba92013-07-07 17:25:49 +03004915static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4916{
4917 switch (mlx5_state) {
4918 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4919 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4920 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4921 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4922 case MLX5_QP_STATE_SQ_DRAINING:
4923 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4924 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4925 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4926 default: return -1;
4927 }
4928}
4929
4930static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4931{
4932 switch (mlx5_mig_state) {
4933 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4934 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4935 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4936 default: return -1;
4937 }
4938}
4939
4940static int to_ib_qp_access_flags(int mlx5_flags)
4941{
4942 int ib_flags = 0;
4943
4944 if (mlx5_flags & MLX5_QP_BIT_RRE)
4945 ib_flags |= IB_ACCESS_REMOTE_READ;
4946 if (mlx5_flags & MLX5_QP_BIT_RWE)
4947 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4948 if (mlx5_flags & MLX5_QP_BIT_RAE)
4949 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4950
4951 return ib_flags;
4952}
4953
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004954static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004955 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004956 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03004957{
Eli Cohene126ba92013-07-07 17:25:49 +03004958
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004959 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03004960
Jason Gunthorpee7996a92018-01-29 13:26:40 -07004961 if (!path->port || path->port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03004962 return;
4963
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02004964 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
4965
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004966 rdma_ah_set_port_num(ah_attr, path->port);
4967 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03004968
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004969 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
4970 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
4971 rdma_ah_set_static_rate(ah_attr,
4972 path->static_rate ? path->static_rate - 5 : 0);
4973 if (path->grh_mlid & (1 << 7)) {
4974 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
4975
4976 rdma_ah_set_grh(ah_attr, NULL,
4977 tc_fl & 0xfffff,
4978 path->mgid_index,
4979 path->hop_limit,
4980 (tc_fl >> 20) & 0xff);
4981 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03004982 }
4983}
4984
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004985static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4986 struct mlx5_ib_sq *sq,
4987 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004988{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004989 int err;
4990
Eran Ben Elisha28160772017-12-26 15:17:05 +02004991 err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004992 if (err)
4993 goto out;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004994 sq->state = *sq_state;
4995
4996out:
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004997 return err;
4998}
4999
5000static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
5001 struct mlx5_ib_rq *rq,
5002 u8 *rq_state)
5003{
5004 void *out;
5005 void *rqc;
5006 int inlen;
5007 int err;
5008
5009 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005010 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005011 if (!out)
5012 return -ENOMEM;
5013
5014 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
5015 if (err)
5016 goto out;
5017
5018 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
5019 *rq_state = MLX5_GET(rqc, rqc, state);
5020 rq->state = *rq_state;
5021
5022out:
5023 kvfree(out);
5024 return err;
5025}
5026
5027static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
5028 struct mlx5_ib_qp *qp, u8 *qp_state)
5029{
5030 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
5031 [MLX5_RQC_STATE_RST] = {
5032 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5033 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5034 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
5035 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
5036 },
5037 [MLX5_RQC_STATE_RDY] = {
5038 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5039 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5040 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
5041 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
5042 },
5043 [MLX5_RQC_STATE_ERR] = {
5044 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
5045 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
5046 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
5047 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
5048 },
5049 [MLX5_RQ_STATE_NA] = {
5050 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
5051 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
5052 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
5053 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
5054 },
5055 };
5056
5057 *qp_state = sqrq_trans[rq_state][sq_state];
5058
5059 if (*qp_state == MLX5_QP_STATE_BAD) {
5060 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
5061 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
5062 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
5063 return -EINVAL;
5064 }
5065
5066 if (*qp_state == MLX5_QP_STATE)
5067 *qp_state = qp->state;
5068
5069 return 0;
5070}
5071
5072static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
5073 struct mlx5_ib_qp *qp,
5074 u8 *raw_packet_qp_state)
5075{
5076 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
5077 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
5078 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
5079 int err;
5080 u8 sq_state = MLX5_SQ_STATE_NA;
5081 u8 rq_state = MLX5_RQ_STATE_NA;
5082
5083 if (qp->sq.wqe_cnt) {
5084 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
5085 if (err)
5086 return err;
5087 }
5088
5089 if (qp->rq.wqe_cnt) {
5090 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
5091 if (err)
5092 return err;
5093 }
5094
5095 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
5096 raw_packet_qp_state);
5097}
5098
5099static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
5100 struct ib_qp_attr *qp_attr)
5101{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005102 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03005103 struct mlx5_qp_context *context;
5104 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005105 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03005106 int err = 0;
5107
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005108 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005109 if (!outb)
5110 return -ENOMEM;
5111
majd@mellanox.com19098df2016-01-14 19:13:03 +02005112 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005113 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03005114 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005115 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03005116
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03005117 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
5118 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
5119
Eli Cohene126ba92013-07-07 17:25:49 +03005120 mlx5_state = be32_to_cpu(context->flags) >> 28;
5121
5122 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03005123 qp_attr->path_mtu = context->mtu_msgmax >> 5;
5124 qp_attr->path_mig_state =
5125 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
5126 qp_attr->qkey = be32_to_cpu(context->qkey);
5127 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
5128 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
5129 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
5130 qp_attr->qp_access_flags =
5131 to_ib_qp_access_flags(be32_to_cpu(context->params2));
5132
5133 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04005134 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
5135 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005136 qp_attr->alt_pkey_index =
5137 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04005138 qp_attr->alt_port_num =
5139 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03005140 }
5141
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005142 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03005143 qp_attr->port_num = context->pri_path.port;
5144
5145 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
5146 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
5147
5148 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
5149
5150 qp_attr->max_dest_rd_atomic =
5151 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
5152 qp_attr->min_rnr_timer =
5153 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
5154 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
5155 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
5156 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
5157 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005158
5159out:
5160 kfree(outb);
5161 return err;
5162}
5163
Moni Shoua776a3902018-01-02 16:19:33 +02005164static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
5165 struct ib_qp_attr *qp_attr, int qp_attr_mask,
5166 struct ib_qp_init_attr *qp_init_attr)
5167{
5168 struct mlx5_core_dct *dct = &mqp->dct.mdct;
5169 u32 *out;
5170 u32 access_flags = 0;
5171 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
5172 void *dctc;
5173 int err;
5174 int supported_mask = IB_QP_STATE |
5175 IB_QP_ACCESS_FLAGS |
5176 IB_QP_PORT |
5177 IB_QP_MIN_RNR_TIMER |
5178 IB_QP_AV |
5179 IB_QP_PATH_MTU |
5180 IB_QP_PKEY_INDEX;
5181
5182 if (qp_attr_mask & ~supported_mask)
5183 return -EINVAL;
5184 if (mqp->state != IB_QPS_RTR)
5185 return -EINVAL;
5186
5187 out = kzalloc(outlen, GFP_KERNEL);
5188 if (!out)
5189 return -ENOMEM;
5190
5191 err = mlx5_core_dct_query(dev->mdev, dct, out, outlen);
5192 if (err)
5193 goto out;
5194
5195 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
5196
5197 if (qp_attr_mask & IB_QP_STATE)
5198 qp_attr->qp_state = IB_QPS_RTR;
5199
5200 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
5201 if (MLX5_GET(dctc, dctc, rre))
5202 access_flags |= IB_ACCESS_REMOTE_READ;
5203 if (MLX5_GET(dctc, dctc, rwe))
5204 access_flags |= IB_ACCESS_REMOTE_WRITE;
5205 if (MLX5_GET(dctc, dctc, rae))
5206 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
5207 qp_attr->qp_access_flags = access_flags;
5208 }
5209
5210 if (qp_attr_mask & IB_QP_PORT)
5211 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
5212 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
5213 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
5214 if (qp_attr_mask & IB_QP_AV) {
5215 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
5216 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
5217 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
5218 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
5219 }
5220 if (qp_attr_mask & IB_QP_PATH_MTU)
5221 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
5222 if (qp_attr_mask & IB_QP_PKEY_INDEX)
5223 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
5224out:
5225 kfree(out);
5226 return err;
5227}
5228
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005229int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5230 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
5231{
5232 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5233 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5234 int err = 0;
5235 u8 raw_packet_qp_state;
5236
Yishai Hadas28d61372016-05-23 15:20:56 +03005237 if (ibqp->rwq_ind_tbl)
5238 return -ENOSYS;
5239
Haggai Erand16e91d2016-02-29 15:45:05 +02005240 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5241 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
5242 qp_init_attr);
5243
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005244 /* Not all of output fields are applicable, make sure to zero them */
5245 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5246 memset(qp_attr, 0, sizeof(*qp_attr));
5247
Moni Shoua776a3902018-01-02 16:19:33 +02005248 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
5249 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
5250 qp_attr_mask, qp_init_attr);
5251
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005252 mutex_lock(&qp->mutex);
5253
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005254 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
5255 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005256 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
5257 if (err)
5258 goto out;
5259 qp->state = raw_packet_qp_state;
5260 qp_attr->port_num = 1;
5261 } else {
5262 err = query_qp_attr(dev, qp, qp_attr);
5263 if (err)
5264 goto out;
5265 }
5266
5267 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03005268 qp_attr->cur_qp_state = qp_attr->qp_state;
5269 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
5270 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
5271
5272 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005273 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005274 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005275 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005276 } else {
5277 qp_attr->cap.max_send_wr = 0;
5278 qp_attr->cap.max_send_sge = 0;
5279 }
5280
Noa Osherovich0540d812016-06-04 15:15:32 +03005281 qp_init_attr->qp_type = ibqp->qp_type;
5282 qp_init_attr->recv_cq = ibqp->recv_cq;
5283 qp_init_attr->send_cq = ibqp->send_cq;
5284 qp_init_attr->srq = ibqp->srq;
5285 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005286
5287 qp_init_attr->cap = qp_attr->cap;
5288
5289 qp_init_attr->create_flags = 0;
5290 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
5291 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
5292
Leon Romanovsky051f2632015-12-20 12:16:11 +02005293 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
5294 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
5295 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
5296 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
5297 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
5298 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02005299 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
5300 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02005301
Eli Cohene126ba92013-07-07 17:25:49 +03005302 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5303 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5304
Eli Cohene126ba92013-07-07 17:25:49 +03005305out:
5306 mutex_unlock(&qp->mutex);
5307 return err;
5308}
5309
5310struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
5311 struct ib_ucontext *context,
5312 struct ib_udata *udata)
5313{
5314 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5315 struct mlx5_ib_xrcd *xrcd;
5316 int err;
Yishai Hadasd00614c2018-09-20 21:39:31 +03005317 u16 uid;
Eli Cohene126ba92013-07-07 17:25:49 +03005318
Saeed Mahameed938fe832015-05-28 22:28:41 +03005319 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005320 return ERR_PTR(-ENOSYS);
5321
5322 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5323 if (!xrcd)
5324 return ERR_PTR(-ENOMEM);
5325
Yishai Hadasd00614c2018-09-20 21:39:31 +03005326 uid = context ? to_mucontext(context)->devx_uid : 0;
5327 err = mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, uid);
Eli Cohene126ba92013-07-07 17:25:49 +03005328 if (err) {
5329 kfree(xrcd);
5330 return ERR_PTR(-ENOMEM);
5331 }
5332
Yishai Hadasd00614c2018-09-20 21:39:31 +03005333 xrcd->uid = uid;
Eli Cohene126ba92013-07-07 17:25:49 +03005334 return &xrcd->ibxrcd;
5335}
5336
5337int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
5338{
5339 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5340 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
Yishai Hadasd00614c2018-09-20 21:39:31 +03005341 u16 uid = to_mxrcd(xrcd)->uid;
Eli Cohene126ba92013-07-07 17:25:49 +03005342 int err;
5343
Yishai Hadasd00614c2018-09-20 21:39:31 +03005344 err = mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, uid);
Leon Romanovskyb0818082018-01-28 11:25:30 +02005345 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03005346 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005347
5348 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03005349 return 0;
5350}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005351
Yishai Hadas350d0e42016-08-28 14:58:18 +03005352static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5353{
5354 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5355 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5356 struct ib_event event;
5357
5358 if (rwq->ibwq.event_handler) {
5359 event.device = rwq->ibwq.device;
5360 event.element.wq = &rwq->ibwq;
5361 switch (type) {
5362 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5363 event.event = IB_EVENT_WQ_FATAL;
5364 break;
5365 default:
5366 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5367 return;
5368 }
5369
5370 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5371 }
5372}
5373
Maor Gottlieb03404e82017-05-30 10:29:13 +03005374static int set_delay_drop(struct mlx5_ib_dev *dev)
5375{
5376 int err = 0;
5377
5378 mutex_lock(&dev->delay_drop.lock);
5379 if (dev->delay_drop.activate)
5380 goto out;
5381
5382 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
5383 if (err)
5384 goto out;
5385
5386 dev->delay_drop.activate = true;
5387out:
5388 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005389
5390 if (!err)
5391 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005392 return err;
5393}
5394
Yishai Hadas79b20a62016-05-23 15:20:50 +03005395static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5396 struct ib_wq_init_attr *init_attr)
5397{
5398 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005399 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005400 __be64 *rq_pas0;
5401 void *in;
5402 void *rqc;
5403 void *wq;
5404 int inlen;
5405 int err;
5406
5407 dev = to_mdev(pd->device);
5408
5409 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005410 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005411 if (!in)
5412 return -ENOMEM;
5413
Yishai Hadas34d57582018-09-20 21:39:21 +03005414 MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005415 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
5416 MLX5_SET(rqc, rqc, mem_rq_type,
5417 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
5418 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
5419 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
5420 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
5421 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
5422 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03005423 MLX5_SET(wq, wq, wq_type,
5424 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
5425 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005426 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5427 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
5428 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
5429 err = -EOPNOTSUPP;
5430 goto out;
5431 } else {
5432 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
5433 }
5434 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005435 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03005436 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
5437 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
5438 MLX5_SET(wq, wq, log_wqe_stride_size,
5439 rwq->single_stride_log_num_of_bytes -
5440 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
5441 MLX5_SET(wq, wq, log_wqe_num_of_strides, rwq->log_num_strides -
5442 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES);
5443 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005444 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
5445 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
5446 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
5447 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
5448 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
5449 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02005450 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005451 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02005452 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005453 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
5454 err = -EOPNOTSUPP;
5455 goto out;
5456 }
5457 } else {
5458 MLX5_SET(rqc, rqc, vsd, 1);
5459 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02005460 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
5461 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
5462 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
5463 err = -EOPNOTSUPP;
5464 goto out;
5465 }
5466 MLX5_SET(rqc, rqc, scatter_fcs, 1);
5467 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03005468 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5469 if (!(dev->ib_dev.attrs.raw_packet_caps &
5470 IB_RAW_PACKET_CAP_DELAY_DROP)) {
5471 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
5472 err = -EOPNOTSUPP;
5473 goto out;
5474 }
5475 MLX5_SET(rqc, rqc, delay_drop_en, 1);
5476 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005477 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
5478 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03005479 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005480 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5481 err = set_delay_drop(dev);
5482 if (err) {
5483 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
5484 err);
5485 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
5486 } else {
5487 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
5488 }
5489 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005490out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03005491 kvfree(in);
5492 return err;
5493}
5494
5495static int set_user_rq_size(struct mlx5_ib_dev *dev,
5496 struct ib_wq_init_attr *wq_init_attr,
5497 struct mlx5_ib_create_wq *ucmd,
5498 struct mlx5_ib_rwq *rwq)
5499{
5500 /* Sanity check RQ size before proceeding */
5501 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
5502 return -EINVAL;
5503
5504 if (!ucmd->rq_wqe_count)
5505 return -EINVAL;
5506
5507 rwq->wqe_count = ucmd->rq_wqe_count;
5508 rwq->wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky0dfe4522018-08-01 14:25:41 -07005509 if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
5510 return -EINVAL;
5511
Yishai Hadas79b20a62016-05-23 15:20:50 +03005512 rwq->log_rq_stride = rwq->wqe_shift;
5513 rwq->log_rq_size = ilog2(rwq->wqe_count);
5514 return 0;
5515}
5516
5517static int prepare_user_rq(struct ib_pd *pd,
5518 struct ib_wq_init_attr *init_attr,
5519 struct ib_udata *udata,
5520 struct mlx5_ib_rwq *rwq)
5521{
5522 struct mlx5_ib_dev *dev = to_mdev(pd->device);
5523 struct mlx5_ib_create_wq ucmd = {};
5524 int err;
5525 size_t required_cmd_sz;
5526
Noa Osherovichccc87082017-10-17 18:01:13 +03005527 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
5528 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005529 if (udata->inlen < required_cmd_sz) {
5530 mlx5_ib_dbg(dev, "invalid inlen\n");
5531 return -EINVAL;
5532 }
5533
5534 if (udata->inlen > sizeof(ucmd) &&
5535 !ib_is_udata_cleared(udata, sizeof(ucmd),
5536 udata->inlen - sizeof(ucmd))) {
5537 mlx5_ib_dbg(dev, "inlen is not supported\n");
5538 return -EOPNOTSUPP;
5539 }
5540
5541 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
5542 mlx5_ib_dbg(dev, "copy failed\n");
5543 return -EFAULT;
5544 }
5545
Noa Osherovichccc87082017-10-17 18:01:13 +03005546 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03005547 mlx5_ib_dbg(dev, "invalid comp mask\n");
5548 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03005549 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
5550 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
5551 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
5552 return -EOPNOTSUPP;
5553 }
5554 if ((ucmd.single_stride_log_num_of_bytes <
5555 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
5556 (ucmd.single_stride_log_num_of_bytes >
5557 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
5558 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
5559 ucmd.single_stride_log_num_of_bytes,
5560 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
5561 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
5562 return -EINVAL;
5563 }
5564 if ((ucmd.single_wqe_log_num_of_strides >
5565 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
5566 (ucmd.single_wqe_log_num_of_strides <
5567 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES)) {
5568 mlx5_ib_dbg(dev, "Invalid log num strides (%u. Range is %u - %u)\n",
5569 ucmd.single_wqe_log_num_of_strides,
5570 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
5571 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
5572 return -EINVAL;
5573 }
5574 rwq->single_stride_log_num_of_bytes =
5575 ucmd.single_stride_log_num_of_bytes;
5576 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
5577 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
5578 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005579 }
5580
5581 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
5582 if (err) {
5583 mlx5_ib_dbg(dev, "err %d\n", err);
5584 return err;
5585 }
5586
5587 err = create_user_rq(dev, pd, rwq, &ucmd);
5588 if (err) {
5589 mlx5_ib_dbg(dev, "err %d\n", err);
5590 if (err)
5591 return err;
5592 }
5593
5594 rwq->user_index = ucmd.user_index;
5595 return 0;
5596}
5597
5598struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
5599 struct ib_wq_init_attr *init_attr,
5600 struct ib_udata *udata)
5601{
5602 struct mlx5_ib_dev *dev;
5603 struct mlx5_ib_rwq *rwq;
5604 struct mlx5_ib_create_wq_resp resp = {};
5605 size_t min_resp_len;
5606 int err;
5607
5608 if (!udata)
5609 return ERR_PTR(-ENOSYS);
5610
5611 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5612 if (udata->outlen && udata->outlen < min_resp_len)
5613 return ERR_PTR(-EINVAL);
5614
5615 dev = to_mdev(pd->device);
5616 switch (init_attr->wq_type) {
5617 case IB_WQT_RQ:
5618 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
5619 if (!rwq)
5620 return ERR_PTR(-ENOMEM);
5621 err = prepare_user_rq(pd, init_attr, udata, rwq);
5622 if (err)
5623 goto err;
5624 err = create_rq(rwq, pd, init_attr);
5625 if (err)
5626 goto err_user_rq;
5627 break;
5628 default:
5629 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
5630 init_attr->wq_type);
5631 return ERR_PTR(-EINVAL);
5632 }
5633
Yishai Hadas350d0e42016-08-28 14:58:18 +03005634 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005635 rwq->ibwq.state = IB_WQS_RESET;
5636 if (udata->outlen) {
5637 resp.response_length = offsetof(typeof(resp), response_length) +
5638 sizeof(resp.response_length);
5639 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5640 if (err)
5641 goto err_copy;
5642 }
5643
Yishai Hadas350d0e42016-08-28 14:58:18 +03005644 rwq->core_qp.event = mlx5_ib_wq_event;
5645 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005646 return &rwq->ibwq;
5647
5648err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03005649 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005650err_user_rq:
Maor Gottliebfe248c32017-05-30 10:29:14 +03005651 destroy_user_rq(dev, pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005652err:
5653 kfree(rwq);
5654 return ERR_PTR(err);
5655}
5656
5657int mlx5_ib_destroy_wq(struct ib_wq *wq)
5658{
5659 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5660 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5661
Yishai Hadas350d0e42016-08-28 14:58:18 +03005662 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005663 destroy_user_rq(dev, wq->pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005664 kfree(rwq);
5665
5666 return 0;
5667}
5668
Yishai Hadasc5f90922016-05-23 15:20:53 +03005669struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
5670 struct ib_rwq_ind_table_init_attr *init_attr,
5671 struct ib_udata *udata)
5672{
5673 struct mlx5_ib_dev *dev = to_mdev(device);
5674 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
5675 int sz = 1 << init_attr->log_ind_tbl_size;
5676 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
5677 size_t min_resp_len;
5678 int inlen;
5679 int err;
5680 int i;
5681 u32 *in;
5682 void *rqtc;
5683
5684 if (udata->inlen > 0 &&
5685 !ib_is_udata_cleared(udata, 0,
5686 udata->inlen))
5687 return ERR_PTR(-EOPNOTSUPP);
5688
Maor Gottliebefd7f402016-10-27 16:36:40 +03005689 if (init_attr->log_ind_tbl_size >
5690 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
5691 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
5692 init_attr->log_ind_tbl_size,
5693 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
5694 return ERR_PTR(-EINVAL);
5695 }
5696
Yishai Hadasc5f90922016-05-23 15:20:53 +03005697 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5698 if (udata->outlen && udata->outlen < min_resp_len)
5699 return ERR_PTR(-EINVAL);
5700
5701 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
5702 if (!rwq_ind_tbl)
5703 return ERR_PTR(-ENOMEM);
5704
5705 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005706 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005707 if (!in) {
5708 err = -ENOMEM;
5709 goto err;
5710 }
5711
5712 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
5713
5714 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
5715 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
5716
5717 for (i = 0; i < sz; i++)
5718 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
5719
Yishai Hadas5deba862018-09-20 21:39:28 +03005720 rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
5721 MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);
5722
Yishai Hadasc5f90922016-05-23 15:20:53 +03005723 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
5724 kvfree(in);
5725
5726 if (err)
5727 goto err;
5728
5729 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
5730 if (udata->outlen) {
5731 resp.response_length = offsetof(typeof(resp), response_length) +
5732 sizeof(resp.response_length);
5733 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5734 if (err)
5735 goto err_copy;
5736 }
5737
5738 return &rwq_ind_tbl->ib_rwq_ind_tbl;
5739
5740err_copy:
Yishai Hadas5deba862018-09-20 21:39:28 +03005741 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005742err:
5743 kfree(rwq_ind_tbl);
5744 return ERR_PTR(err);
5745}
5746
5747int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
5748{
5749 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
5750 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
5751
Yishai Hadas5deba862018-09-20 21:39:28 +03005752 mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005753
5754 kfree(rwq_ind_tbl);
5755 return 0;
5756}
5757
Yishai Hadas79b20a62016-05-23 15:20:50 +03005758int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
5759 u32 wq_attr_mask, struct ib_udata *udata)
5760{
5761 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5762 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5763 struct mlx5_ib_modify_wq ucmd = {};
5764 size_t required_cmd_sz;
5765 int curr_wq_state;
5766 int wq_state;
5767 int inlen;
5768 int err;
5769 void *rqc;
5770 void *in;
5771
5772 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
5773 if (udata->inlen < required_cmd_sz)
5774 return -EINVAL;
5775
5776 if (udata->inlen > sizeof(ucmd) &&
5777 !ib_is_udata_cleared(udata, sizeof(ucmd),
5778 udata->inlen - sizeof(ucmd)))
5779 return -EOPNOTSUPP;
5780
5781 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
5782 return -EFAULT;
5783
5784 if (ucmd.comp_mask || ucmd.reserved)
5785 return -EOPNOTSUPP;
5786
5787 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005788 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005789 if (!in)
5790 return -ENOMEM;
5791
5792 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
5793
5794 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
5795 wq_attr->curr_wq_state : wq->state;
5796 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
5797 wq_attr->wq_state : curr_wq_state;
5798 if (curr_wq_state == IB_WQS_ERR)
5799 curr_wq_state = MLX5_RQC_STATE_ERR;
5800 if (wq_state == IB_WQS_ERR)
5801 wq_state = MLX5_RQC_STATE_ERR;
5802 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
Yishai Hadas34d57582018-09-20 21:39:21 +03005803 MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005804 MLX5_SET(rqc, rqc, state, wq_state);
5805
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005806 if (wq_attr_mask & IB_WQ_FLAGS) {
5807 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
5808 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
5809 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
5810 mlx5_ib_dbg(dev, "VLAN offloads are not "
5811 "supported\n");
5812 err = -EOPNOTSUPP;
5813 goto out;
5814 }
5815 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5816 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
5817 MLX5_SET(rqc, rqc, vsd,
5818 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
5819 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005820
5821 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5822 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
5823 err = -EOPNOTSUPP;
5824 goto out;
5825 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005826 }
5827
Majd Dibbiny23a69642017-01-18 15:25:10 +02005828 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
5829 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
5830 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5831 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03005832 MLX5_SET(rqc, rqc, counter_set_id,
5833 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005834 } else
Jason Gunthorpe5a738b52018-09-20 16:42:24 -06005835 dev_info_once(
5836 &dev->ib_dev.dev,
5837 "Receive WQ counters are not supported on current FW\n");
Majd Dibbiny23a69642017-01-18 15:25:10 +02005838 }
5839
Yishai Hadas350d0e42016-08-28 14:58:18 +03005840 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005841 if (!err)
5842 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
5843
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005844out:
5845 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005846 return err;
5847}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005848
5849struct mlx5_ib_drain_cqe {
5850 struct ib_cqe cqe;
5851 struct completion done;
5852};
5853
5854static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
5855{
5856 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
5857 struct mlx5_ib_drain_cqe,
5858 cqe);
5859
5860 complete(&cqe->done);
5861}
5862
5863/* This function returns only once the drained WR was completed */
5864static void handle_drain_completion(struct ib_cq *cq,
5865 struct mlx5_ib_drain_cqe *sdrain,
5866 struct mlx5_ib_dev *dev)
5867{
5868 struct mlx5_core_dev *mdev = dev->mdev;
5869
5870 if (cq->poll_ctx == IB_POLL_DIRECT) {
5871 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
5872 ib_process_cq_direct(cq, -1);
5873 return;
5874 }
5875
5876 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5877 struct mlx5_ib_cq *mcq = to_mcq(cq);
5878 bool triggered = false;
5879 unsigned long flags;
5880
5881 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
5882 /* Make sure that the CQ handler won't run if wasn't run yet */
5883 if (!mcq->mcq.reset_notify_added)
5884 mcq->mcq.reset_notify_added = 1;
5885 else
5886 triggered = true;
5887 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
5888
5889 if (triggered) {
5890 /* Wait for any scheduled/running task to be ended */
5891 switch (cq->poll_ctx) {
5892 case IB_POLL_SOFTIRQ:
5893 irq_poll_disable(&cq->iop);
5894 irq_poll_enable(&cq->iop);
5895 break;
5896 case IB_POLL_WORKQUEUE:
5897 cancel_work_sync(&cq->work);
5898 break;
5899 default:
5900 WARN_ON_ONCE(1);
5901 }
5902 }
5903
5904 /* Run the CQ handler - this makes sure that the drain WR will
5905 * be processed if wasn't processed yet.
5906 */
5907 mcq->mcq.comp(&mcq->mcq);
5908 }
5909
5910 wait_for_completion(&sdrain->done);
5911}
5912
5913void mlx5_ib_drain_sq(struct ib_qp *qp)
5914{
5915 struct ib_cq *cq = qp->send_cq;
5916 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
5917 struct mlx5_ib_drain_cqe sdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005918 const struct ib_send_wr *bad_swr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005919 struct ib_rdma_wr swr = {
5920 .wr = {
5921 .next = NULL,
5922 { .wr_cqe = &sdrain.cqe, },
5923 .opcode = IB_WR_RDMA_WRITE,
5924 },
5925 };
5926 int ret;
5927 struct mlx5_ib_dev *dev = to_mdev(qp->device);
5928 struct mlx5_core_dev *mdev = dev->mdev;
5929
5930 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
5931 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5932 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
5933 return;
5934 }
5935
5936 sdrain.cqe.done = mlx5_ib_drain_qp_done;
5937 init_completion(&sdrain.done);
5938
5939 ret = _mlx5_ib_post_send(qp, &swr.wr, &bad_swr, true);
5940 if (ret) {
5941 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
5942 return;
5943 }
5944
5945 handle_drain_completion(cq, &sdrain, dev);
5946}
5947
5948void mlx5_ib_drain_rq(struct ib_qp *qp)
5949{
5950 struct ib_cq *cq = qp->recv_cq;
5951 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
5952 struct mlx5_ib_drain_cqe rdrain;
Bart Van Assched34ac5c2018-07-18 09:25:32 -07005953 struct ib_recv_wr rwr = {};
5954 const struct ib_recv_wr *bad_rwr;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005955 int ret;
5956 struct mlx5_ib_dev *dev = to_mdev(qp->device);
5957 struct mlx5_core_dev *mdev = dev->mdev;
5958
5959 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
5960 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5961 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
5962 return;
5963 }
5964
5965 rwr.wr_cqe = &rdrain.cqe;
5966 rdrain.cqe.done = mlx5_ib_drain_qp_done;
5967 init_completion(&rdrain.done);
5968
5969 ret = _mlx5_ib_post_recv(qp, &rwr, &bad_rwr, true);
5970 if (ret) {
5971 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
5972 return;
5973 }
5974
5975 handle_drain_completion(cq, &rdrain, dev);
5976}