blob: 535f6ad038b21dea42b59c711fc89b2d0ceeff8f [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"
Eli Cohene126ba92013-07-07 17:25:49 +030040
41/* not supported currently */
42static int wq_signature;
43
44enum {
45 MLX5_IB_ACK_REQ_FREQ = 8,
46};
47
48enum {
49 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
50 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
51 MLX5_IB_LINK_TYPE_IB = 0,
52 MLX5_IB_LINK_TYPE_ETH = 1
53};
54
55enum {
56 MLX5_IB_SQ_STRIDE = 6,
Idan Burstein064e5262018-05-02 13:16:39 +030057 MLX5_IB_SQ_UMR_INLINE_THRESHOLD = 64,
Eli Cohene126ba92013-07-07 17:25:49 +030058};
59
60static const u32 mlx5_ib_opcode[] = {
61 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020062 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030063 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
64 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
65 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
66 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
67 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
68 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
69 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
70 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030071 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030072 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
73 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
74 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
75};
76
Erez Shitritf0313962016-02-21 16:27:17 +020077struct mlx5_wqe_eth_pad {
78 u8 rsvd0[16];
79};
Eli Cohene126ba92013-07-07 17:25:49 +030080
Alex Veskereb49ab02016-08-28 12:25:53 +030081enum raw_qp_set_mask_map {
82 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020083 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030084};
85
Alex Vesker0680efa2016-08-28 12:25:52 +030086struct mlx5_modify_raw_qp_param {
87 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030088
89 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang61147f32018-03-19 15:10:30 +020090
91 struct mlx5_rate_limit rl;
92
Alex Veskereb49ab02016-08-28 12:25:53 +030093 u8 rq_q_ctr_id;
Alex Vesker0680efa2016-08-28 12:25:52 +030094};
95
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030096static void get_cqs(enum ib_qp_type qp_type,
97 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
98 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
99
Eli Cohene126ba92013-07-07 17:25:49 +0300100static int is_qp0(enum ib_qp_type qp_type)
101{
102 return qp_type == IB_QPT_SMI;
103}
104
Eli Cohene126ba92013-07-07 17:25:49 +0300105static int is_sqp(enum ib_qp_type qp_type)
106{
107 return is_qp0(qp_type) || is_qp1(qp_type);
108}
109
110static void *get_wqe(struct mlx5_ib_qp *qp, int offset)
111{
112 return mlx5_buf_offset(&qp->buf, offset);
113}
114
115static void *get_recv_wqe(struct mlx5_ib_qp *qp, int n)
116{
117 return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
118}
119
120void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n)
121{
122 return get_wqe(qp, qp->sq.offset + (n << MLX5_IB_SQ_STRIDE));
123}
124
Haggai Eranc1395a22014-12-11 17:04:14 +0200125/**
126 * mlx5_ib_read_user_wqe() - Copy a user-space WQE to kernel space.
127 *
128 * @qp: QP to copy from.
129 * @send: copy from the send queue when non-zero, use the receive queue
130 * otherwise.
131 * @wqe_index: index to start copying from. For send work queues, the
132 * wqe_index is in units of MLX5_SEND_WQE_BB.
133 * For receive work queue, it is the number of work queue
134 * element in the queue.
135 * @buffer: destination buffer.
136 * @length: maximum number of bytes to copy.
137 *
138 * Copies at least a single WQE, but may copy more data.
139 *
140 * Return: the number of bytes copied, or an error code.
141 */
142int mlx5_ib_read_user_wqe(struct mlx5_ib_qp *qp, int send, int wqe_index,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200143 void *buffer, u32 length,
144 struct mlx5_ib_qp_base *base)
Haggai Eranc1395a22014-12-11 17:04:14 +0200145{
146 struct ib_device *ibdev = qp->ibqp.device;
147 struct mlx5_ib_dev *dev = to_mdev(ibdev);
148 struct mlx5_ib_wq *wq = send ? &qp->sq : &qp->rq;
149 size_t offset;
150 size_t wq_end;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200151 struct ib_umem *umem = base->ubuffer.umem;
Haggai Eranc1395a22014-12-11 17:04:14 +0200152 u32 first_copy_length;
153 int wqe_length;
154 int ret;
155
156 if (wq->wqe_cnt == 0) {
157 mlx5_ib_dbg(dev, "mlx5_ib_read_user_wqe for a QP with wqe_cnt == 0. qp_type: 0x%x\n",
158 qp->ibqp.qp_type);
159 return -EINVAL;
160 }
161
162 offset = wq->offset + ((wqe_index % wq->wqe_cnt) << wq->wqe_shift);
163 wq_end = wq->offset + (wq->wqe_cnt << wq->wqe_shift);
164
165 if (send && length < sizeof(struct mlx5_wqe_ctrl_seg))
166 return -EINVAL;
167
168 if (offset > umem->length ||
169 (send && offset + sizeof(struct mlx5_wqe_ctrl_seg) > umem->length))
170 return -EINVAL;
171
172 first_copy_length = min_t(u32, offset + length, wq_end) - offset;
173 ret = ib_umem_copy_from(buffer, umem, offset, first_copy_length);
174 if (ret)
175 return ret;
176
177 if (send) {
178 struct mlx5_wqe_ctrl_seg *ctrl = buffer;
179 int ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
180
181 wqe_length = ds * MLX5_WQE_DS_UNITS;
182 } else {
183 wqe_length = 1 << wq->wqe_shift;
184 }
185
186 if (wqe_length <= first_copy_length)
187 return first_copy_length;
188
189 ret = ib_umem_copy_from(buffer + first_copy_length, umem, wq->offset,
190 wqe_length - first_copy_length);
191 if (ret)
192 return ret;
193
194 return wqe_length;
195}
196
Eli Cohene126ba92013-07-07 17:25:49 +0300197static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
198{
199 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
200 struct ib_event event;
201
majd@mellanox.com19098df2016-01-14 19:13:03 +0200202 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
203 /* This event is only valid for trans_qps */
204 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
205 }
Eli Cohene126ba92013-07-07 17:25:49 +0300206
207 if (ibqp->event_handler) {
208 event.device = ibqp->device;
209 event.element.qp = ibqp;
210 switch (type) {
211 case MLX5_EVENT_TYPE_PATH_MIG:
212 event.event = IB_EVENT_PATH_MIG;
213 break;
214 case MLX5_EVENT_TYPE_COMM_EST:
215 event.event = IB_EVENT_COMM_EST;
216 break;
217 case MLX5_EVENT_TYPE_SQ_DRAINED:
218 event.event = IB_EVENT_SQ_DRAINED;
219 break;
220 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
221 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
222 break;
223 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
224 event.event = IB_EVENT_QP_FATAL;
225 break;
226 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
227 event.event = IB_EVENT_PATH_MIG_ERR;
228 break;
229 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
230 event.event = IB_EVENT_QP_REQ_ERR;
231 break;
232 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
233 event.event = IB_EVENT_QP_ACCESS_ERR;
234 break;
235 default:
236 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
237 return;
238 }
239
240 ibqp->event_handler(&event, ibqp->qp_context);
241 }
242}
243
244static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
245 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
246{
247 int wqe_size;
248 int wq_size;
249
250 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300251 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300252 return -EINVAL;
253
254 if (!has_rq) {
255 qp->rq.max_gs = 0;
256 qp->rq.wqe_cnt = 0;
257 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300258 cap->max_recv_wr = 0;
259 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300260 } else {
261 if (ucmd) {
262 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300263 if (ucmd->rq_wqe_shift > BITS_PER_BYTE * sizeof(ucmd->rq_wqe_shift))
264 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300265 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
Leon Romanovsky002bf222018-04-23 17:01:53 +0300266 if ((1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) < qp->wq_sig)
267 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300268 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
269 qp->rq.max_post = qp->rq.wqe_cnt;
270 } else {
271 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
272 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
273 wqe_size = roundup_pow_of_two(wqe_size);
274 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
275 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
276 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300277 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300278 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
279 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300280 MLX5_CAP_GEN(dev->mdev,
281 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300282 return -EINVAL;
283 }
284 qp->rq.wqe_shift = ilog2(wqe_size);
285 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
286 qp->rq.max_post = qp->rq.wqe_cnt;
287 }
288 }
289
290 return 0;
291}
292
Erez Shitritf0313962016-02-21 16:27:17 +0200293static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300294{
Andi Shyti618af382013-07-16 15:35:01 +0200295 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300296
Erez Shitritf0313962016-02-21 16:27:17 +0200297 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300298 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300299 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300300 /* fall through */
301 case IB_QPT_RC:
302 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200303 max(sizeof(struct mlx5_wqe_atomic_seg) +
304 sizeof(struct mlx5_wqe_raddr_seg),
305 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
Idan Burstein064e5262018-05-02 13:16:39 +0300306 sizeof(struct mlx5_mkey_seg) +
307 MLX5_IB_SQ_UMR_INLINE_THRESHOLD /
308 MLX5_IB_UMR_OCTOWORD);
Eli Cohene126ba92013-07-07 17:25:49 +0300309 break;
310
Eli Cohenb125a542013-09-11 16:35:22 +0300311 case IB_QPT_XRC_TGT:
312 return 0;
313
Eli Cohene126ba92013-07-07 17:25:49 +0300314 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300315 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200316 max(sizeof(struct mlx5_wqe_raddr_seg),
317 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
318 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300319 break;
320
321 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200322 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
323 size += sizeof(struct mlx5_wqe_eth_pad) +
324 sizeof(struct mlx5_wqe_eth_seg);
325 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300326 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200327 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300328 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300329 sizeof(struct mlx5_wqe_datagram_seg);
330 break;
331
332 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300333 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300334 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
335 sizeof(struct mlx5_mkey_seg);
336 break;
337
338 default:
339 return -EINVAL;
340 }
341
342 return size;
343}
344
345static int calc_send_wqe(struct ib_qp_init_attr *attr)
346{
347 int inl_size = 0;
348 int size;
349
Erez Shitritf0313962016-02-21 16:27:17 +0200350 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300351 if (size < 0)
352 return size;
353
354 if (attr->cap.max_inline_data) {
355 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
356 attr->cap.max_inline_data;
357 }
358
359 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200360 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN &&
361 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
362 return MLX5_SIG_WQE_SIZE;
363 else
364 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300365}
366
Eli Cohen288c01b2016-10-27 16:36:45 +0300367static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
368{
369 int max_sge;
370
371 if (attr->qp_type == IB_QPT_RC)
372 max_sge = (min_t(int, wqe_size, 512) -
373 sizeof(struct mlx5_wqe_ctrl_seg) -
374 sizeof(struct mlx5_wqe_raddr_seg)) /
375 sizeof(struct mlx5_wqe_data_seg);
376 else if (attr->qp_type == IB_QPT_XRC_INI)
377 max_sge = (min_t(int, wqe_size, 512) -
378 sizeof(struct mlx5_wqe_ctrl_seg) -
379 sizeof(struct mlx5_wqe_xrc_seg) -
380 sizeof(struct mlx5_wqe_raddr_seg)) /
381 sizeof(struct mlx5_wqe_data_seg);
382 else
383 max_sge = (wqe_size - sq_overhead(attr)) /
384 sizeof(struct mlx5_wqe_data_seg);
385
386 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
387 sizeof(struct mlx5_wqe_data_seg));
388}
389
Eli Cohene126ba92013-07-07 17:25:49 +0300390static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
391 struct mlx5_ib_qp *qp)
392{
393 int wqe_size;
394 int wq_size;
395
396 if (!attr->cap.max_send_wr)
397 return 0;
398
399 wqe_size = calc_send_wqe(attr);
400 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
401 if (wqe_size < 0)
402 return wqe_size;
403
Saeed Mahameed938fe832015-05-28 22:28:41 +0300404 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300405 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300406 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300407 return -EINVAL;
408 }
409
Erez Shitritf0313962016-02-21 16:27:17 +0200410 qp->max_inline_data = wqe_size - sq_overhead(attr) -
411 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300412 attr->cap.max_inline_data = qp->max_inline_data;
413
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200414 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN)
415 qp->signature_en = true;
416
Eli Cohene126ba92013-07-07 17:25:49 +0300417 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
418 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300419 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800420 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
421 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300422 qp->sq.wqe_cnt,
423 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300424 return -ENOMEM;
425 }
Eli Cohene126ba92013-07-07 17:25:49 +0300426 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300427 qp->sq.max_gs = get_send_sge(attr, wqe_size);
428 if (qp->sq.max_gs < attr->cap.max_send_sge)
429 return -ENOMEM;
430
431 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300432 qp->sq.max_post = wq_size / wqe_size;
433 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300434
435 return wq_size;
436}
437
438static int set_user_buf_size(struct mlx5_ib_dev *dev,
439 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200440 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200441 struct mlx5_ib_qp_base *base,
442 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300443{
444 int desc_sz = 1 << qp->sq.wqe_shift;
445
Saeed Mahameed938fe832015-05-28 22:28:41 +0300446 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300447 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300448 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300449 return -EINVAL;
450 }
451
452 if (ucmd->sq_wqe_count && ((1 << ilog2(ucmd->sq_wqe_count)) != ucmd->sq_wqe_count)) {
453 mlx5_ib_warn(dev, "sq_wqe_count %d, sq_wqe_count %d\n",
454 ucmd->sq_wqe_count, ucmd->sq_wqe_count);
455 return -EINVAL;
456 }
457
458 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
459
Saeed Mahameed938fe832015-05-28 22:28:41 +0300460 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300461 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300462 qp->sq.wqe_cnt,
463 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300464 return -EINVAL;
465 }
466
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300467 if (attr->qp_type == IB_QPT_RAW_PACKET ||
468 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200469 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
470 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
471 } else {
472 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
473 (qp->sq.wqe_cnt << 6);
474 }
Eli Cohene126ba92013-07-07 17:25:49 +0300475
476 return 0;
477}
478
479static int qp_has_rq(struct ib_qp_init_attr *attr)
480{
481 if (attr->qp_type == IB_QPT_XRC_INI ||
482 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
483 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
484 !attr->cap.max_recv_wr)
485 return 0;
486
487 return 1;
488}
489
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200490enum {
491 /* this is the first blue flame register in the array of bfregs assigned
492 * to a processes. Since we do not use it for blue flame but rather
493 * regular 64 bit doorbells, we do not need a lock for maintaiing
494 * "odd/even" order
495 */
496 NUM_NON_BLUE_FLAME_BFREGS = 1,
497};
498
Eli Cohenb037c292017-01-03 23:55:26 +0200499static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
500{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200501 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200502}
503
504static int num_med_bfreg(struct mlx5_ib_dev *dev,
505 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200506{
507 int n;
508
Eli Cohenb037c292017-01-03 23:55:26 +0200509 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
510 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200511
512 return n >= 0 ? n : 0;
513}
514
Yishai Hadas18b03622018-05-07 10:20:01 +0300515static int first_med_bfreg(struct mlx5_ib_dev *dev,
516 struct mlx5_bfreg_info *bfregi)
517{
518 return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
519}
520
Eli Cohenb037c292017-01-03 23:55:26 +0200521static int first_hi_bfreg(struct mlx5_ib_dev *dev,
522 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200523{
524 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200525
Eli Cohenb037c292017-01-03 23:55:26 +0200526 med = num_med_bfreg(dev, bfregi);
527 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200528}
529
Eli Cohenb037c292017-01-03 23:55:26 +0200530static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
531 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300532{
Eli Cohene126ba92013-07-07 17:25:49 +0300533 int i;
534
Eli Cohenb037c292017-01-03 23:55:26 +0200535 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
536 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200537 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300538 return i;
539 }
540 }
541
542 return -ENOMEM;
543}
544
Eli Cohenb037c292017-01-03 23:55:26 +0200545static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
546 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300547{
Yishai Hadas18b03622018-05-07 10:20:01 +0300548 int minidx = first_med_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300549 int i;
550
Yishai Hadas18b03622018-05-07 10:20:01 +0300551 if (minidx < 0)
552 return minidx;
553
554 for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200555 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300556 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200557 if (!bfregi->count[minidx])
558 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300559 }
560
Eli Cohen2f5ff262017-01-03 23:55:21 +0200561 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300562 return minidx;
563}
564
Eli Cohenb037c292017-01-03 23:55:26 +0200565static int alloc_bfreg(struct mlx5_ib_dev *dev,
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300566 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300567{
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300568 int bfregn = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +0300569
Eli Cohen2f5ff262017-01-03 23:55:21 +0200570 mutex_lock(&bfregi->lock);
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300571 if (bfregi->ver >= 2) {
572 bfregn = alloc_high_class_bfreg(dev, bfregi);
573 if (bfregn < 0)
574 bfregn = alloc_med_class_bfreg(dev, bfregi);
575 }
576
577 if (bfregn < 0) {
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200578 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200579 bfregn = 0;
580 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300581 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200582 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300583
Eli Cohen2f5ff262017-01-03 23:55:21 +0200584 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300585}
586
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200587void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300588{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200589 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200590 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200591 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300592}
593
594static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
595{
596 switch (state) {
597 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
598 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
599 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
600 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
601 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
602 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
603 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
604 default: return -1;
605 }
606}
607
608static int to_mlx5_st(enum ib_qp_type type)
609{
610 switch (type) {
611 case IB_QPT_RC: return MLX5_QP_ST_RC;
612 case IB_QPT_UC: return MLX5_QP_ST_UC;
613 case IB_QPT_UD: return MLX5_QP_ST_UD;
614 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
615 case IB_QPT_XRC_INI:
616 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
617 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200618 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200619 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300620 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300621 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200622 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300623 case IB_QPT_MAX:
624 default: return -EINVAL;
625 }
626}
627
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300628static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
629 struct mlx5_ib_cq *recv_cq);
630static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
631 struct mlx5_ib_cq *recv_cq);
632
Yishai Hadas7c043e92018-06-17 13:00:03 +0300633int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300634 struct mlx5_bfreg_info *bfregi, u32 bfregn,
Yishai Hadas7c043e92018-06-17 13:00:03 +0300635 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300636{
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300637 unsigned int bfregs_per_sys_page;
638 u32 index_of_sys_page;
639 u32 offset;
Eli Cohenb037c292017-01-03 23:55:26 +0200640
641 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
642 MLX5_NON_FP_BFREGS_PER_UAR;
643 index_of_sys_page = bfregn / bfregs_per_sys_page;
644
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200645 if (dyn_bfreg) {
646 index_of_sys_page += bfregi->num_static_sys_pages;
Leon Romanovsky05f58ce2018-07-08 13:50:21 +0300647
648 if (index_of_sys_page >= bfregi->num_sys_pages)
649 return -EINVAL;
650
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200651 if (bfregn > bfregi->num_dyn_bfregs ||
652 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
653 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
654 return -EINVAL;
655 }
656 }
Eli Cohenb037c292017-01-03 23:55:26 +0200657
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200658 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200659 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300660}
661
majd@mellanox.com19098df2016-01-14 19:13:03 +0200662static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev,
663 struct ib_pd *pd,
664 unsigned long addr, size_t size,
665 struct ib_umem **umem,
666 int *npages, int *page_shift, int *ncont,
667 u32 *offset)
668{
669 int err;
670
671 *umem = ib_umem_get(pd->uobject->context, addr, size, 0, 0);
672 if (IS_ERR(*umem)) {
673 mlx5_ib_dbg(dev, "umem_get failed\n");
674 return PTR_ERR(*umem);
675 }
676
Majd Dibbiny762f8992016-10-27 16:36:47 +0300677 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200678
679 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
680 if (err) {
681 mlx5_ib_warn(dev, "bad offset\n");
682 goto err_umem;
683 }
684
685 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
686 addr, size, *npages, *page_shift, *ncont, *offset);
687
688 return 0;
689
690err_umem:
691 ib_umem_release(*umem);
692 *umem = NULL;
693
694 return err;
695}
696
Maor Gottliebfe248c32017-05-30 10:29:14 +0300697static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
698 struct mlx5_ib_rwq *rwq)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300699{
700 struct mlx5_ib_ucontext *context;
701
Maor Gottliebfe248c32017-05-30 10:29:14 +0300702 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
703 atomic_dec(&dev->delay_drop.rqs_cnt);
704
Yishai Hadas79b20a62016-05-23 15:20:50 +0300705 context = to_mucontext(pd->uobject->context);
706 mlx5_ib_db_unmap_user(context, &rwq->db);
707 if (rwq->umem)
708 ib_umem_release(rwq->umem);
709}
710
711static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
712 struct mlx5_ib_rwq *rwq,
713 struct mlx5_ib_create_wq *ucmd)
714{
715 struct mlx5_ib_ucontext *context;
716 int page_shift = 0;
717 int npages;
718 u32 offset = 0;
719 int ncont = 0;
720 int err;
721
722 if (!ucmd->buf_addr)
723 return -EINVAL;
724
725 context = to_mucontext(pd->uobject->context);
726 rwq->umem = ib_umem_get(pd->uobject->context, ucmd->buf_addr,
727 rwq->buf_size, 0, 0);
728 if (IS_ERR(rwq->umem)) {
729 mlx5_ib_dbg(dev, "umem_get failed\n");
730 err = PTR_ERR(rwq->umem);
731 return err;
732 }
733
Majd Dibbiny762f8992016-10-27 16:36:47 +0300734 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300735 &ncont, NULL);
736 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
737 &rwq->rq_page_offset);
738 if (err) {
739 mlx5_ib_warn(dev, "bad offset\n");
740 goto err_umem;
741 }
742
743 rwq->rq_num_pas = ncont;
744 rwq->page_shift = page_shift;
745 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
746 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
747
748 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
749 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
750 npages, page_shift, ncont, offset);
751
752 err = mlx5_ib_db_map_user(context, ucmd->db_addr, &rwq->db);
753 if (err) {
754 mlx5_ib_dbg(dev, "map failed\n");
755 goto err_umem;
756 }
757
758 rwq->create_type = MLX5_WQ_USER;
759 return 0;
760
761err_umem:
762 ib_umem_release(rwq->umem);
763 return err;
764}
765
Eli Cohenb037c292017-01-03 23:55:26 +0200766static int adjust_bfregn(struct mlx5_ib_dev *dev,
767 struct mlx5_bfreg_info *bfregi, int bfregn)
768{
769 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
770 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
771}
772
Eli Cohene126ba92013-07-07 17:25:49 +0300773static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
774 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200775 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300776 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200777 struct mlx5_ib_create_qp_resp *resp, int *inlen,
778 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300779{
780 struct mlx5_ib_ucontext *context;
781 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200782 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200783 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200784 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300785 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200786 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200787 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200788 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300789 __be64 *pas;
790 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300791 int err;
792
793 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
794 if (err) {
795 mlx5_ib_dbg(dev, "copy failed\n");
796 return err;
797 }
798
799 context = to_mucontext(pd->uobject->context);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200800 if (ucmd.flags & MLX5_QP_FLAG_BFREG_INDEX) {
801 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
802 ucmd.bfreg_index, true);
803 if (uar_index < 0)
804 return uar_index;
805
806 bfregn = MLX5_IB_INVALID_BFREG;
807 } else if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL) {
808 /*
809 * TBD: should come from the verbs when we have the API
810 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200811 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200812 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200813 }
Leon Romanovsky051f2632015-12-20 12:16:11 +0200814 else {
Leon Romanovskyffaf58d2018-07-08 13:50:20 +0300815 bfregn = alloc_bfreg(dev, &context->bfregi);
816 if (bfregn < 0)
817 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300818 }
819
Eli Cohen2f5ff262017-01-03 23:55:21 +0200820 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200821 if (bfregn != MLX5_IB_INVALID_BFREG)
822 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
823 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300824
Haggai Eran48fea832014-05-22 14:50:11 +0300825 qp->rq.offset = 0;
826 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
827 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
828
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200829 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300830 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200831 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300832
majd@mellanox.com19098df2016-01-14 19:13:03 +0200833 if (ucmd.buf_addr && ubuffer->buf_size) {
834 ubuffer->buf_addr = ucmd.buf_addr;
835 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr,
836 ubuffer->buf_size,
837 &ubuffer->umem, &npages, &page_shift,
838 &ncont, &offset);
839 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200840 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200841 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200842 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300843 }
Eli Cohene126ba92013-07-07 17:25:49 +0300844
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300845 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
846 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300847 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300848 if (!*in) {
849 err = -ENOMEM;
850 goto err_umem;
851 }
Eli Cohene126ba92013-07-07 17:25:49 +0300852
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300853 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
854 if (ubuffer->umem)
855 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
856
857 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
858
859 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
860 MLX5_SET(qpc, qpc, page_offset, offset);
861
862 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200863 if (bfregn != MLX5_IB_INVALID_BFREG)
864 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
865 else
866 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200867 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300868
869 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
870 if (err) {
871 mlx5_ib_dbg(dev, "map failed\n");
872 goto err_free;
873 }
874
Jason Gunthorpe41d902c2018-04-03 10:00:53 +0300875 err = ib_copy_to_udata(udata, resp, min(udata->outlen, sizeof(*resp)));
Eli Cohene126ba92013-07-07 17:25:49 +0300876 if (err) {
877 mlx5_ib_dbg(dev, "copy failed\n");
878 goto err_unmap;
879 }
880 qp->create_type = MLX5_QP_USER;
881
882 return 0;
883
884err_unmap:
885 mlx5_ib_db_unmap_user(context, &qp->db);
886
887err_free:
Al Viro479163f2014-11-20 08:13:57 +0000888 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300889
890err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200891 if (ubuffer->umem)
892 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300893
Eli Cohen2f5ff262017-01-03 23:55:21 +0200894err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200895 if (bfregn != MLX5_IB_INVALID_BFREG)
896 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300897 return err;
898}
899
Eli Cohenb037c292017-01-03 23:55:26 +0200900static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
901 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300902{
903 struct mlx5_ib_ucontext *context;
904
905 context = to_mucontext(pd->uobject->context);
906 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200907 if (base->ubuffer.umem)
908 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200909
910 /*
911 * Free only the BFREGs which are handled by the kernel.
912 * BFREGs of UARs allocated dynamically are handled by user.
913 */
914 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
915 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300916}
917
918static int create_kernel_qp(struct mlx5_ib_dev *dev,
919 struct ib_qp_init_attr *init_attr,
920 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300921 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200922 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300923{
Eli Cohene126ba92013-07-07 17:25:49 +0300924 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300925 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300926 int err;
927
Erez Shitritf0313962016-02-21 16:27:17 +0200928 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
929 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200930 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +0300931 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200932 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200933 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300934
935 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200936 qp->bf.bfreg = &dev->fp_bfreg;
937 else
938 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300939
Eli Cohend8030b02017-02-09 19:31:47 +0200940 /* We need to divide by two since each register is comprised of
941 * two buffers of identical size, namely odd and even
942 */
943 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200944 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +0300945
946 err = calc_sq_size(dev, init_attr, qp);
947 if (err < 0) {
948 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200949 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300950 }
951
952 qp->rq.offset = 0;
953 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200954 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300955
majd@mellanox.com19098df2016-01-14 19:13:03 +0200956 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300957 if (err) {
958 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200959 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300960 }
961
962 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300963 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
964 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300965 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300966 if (!*in) {
967 err = -ENOMEM;
968 goto err_buf;
969 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300970
971 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
972 MLX5_SET(qpc, qpc, uar_page, uar_index);
973 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
974
Eli Cohene126ba92013-07-07 17:25:49 +0300975 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300976 MLX5_SET(qpc, qpc, fre, 1);
977 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300978
Haggai Eranb11a4f92016-02-29 15:45:03 +0200979 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300980 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200981 qp->flags |= MLX5_IB_QP_SQPN_QP1;
982 }
983
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300984 mlx5_fill_page_array(&qp->buf,
985 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300986
Jack Morgenstein9603b612014-07-28 23:30:22 +0300987 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300988 if (err) {
989 mlx5_ib_dbg(dev, "err %d\n", err);
990 goto err_free;
991 }
992
Li Dongyangb5883002017-08-16 23:31:22 +1000993 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
994 sizeof(*qp->sq.wrid), GFP_KERNEL);
995 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
996 sizeof(*qp->sq.wr_data), GFP_KERNEL);
997 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
998 sizeof(*qp->rq.wrid), GFP_KERNEL);
999 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1000 sizeof(*qp->sq.w_list), GFP_KERNEL);
1001 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1002 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001003
1004 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1005 !qp->sq.w_list || !qp->sq.wqe_head) {
1006 err = -ENOMEM;
1007 goto err_wrid;
1008 }
1009 qp->create_type = MLX5_QP_KERNEL;
1010
1011 return 0;
1012
1013err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001014 kvfree(qp->sq.wqe_head);
1015 kvfree(qp->sq.w_list);
1016 kvfree(qp->sq.wrid);
1017 kvfree(qp->sq.wr_data);
1018 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001019 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001020
1021err_free:
Al Viro479163f2014-11-20 08:13:57 +00001022 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001023
1024err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001025 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001026 return err;
1027}
1028
1029static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1030{
Li Dongyangb5883002017-08-16 23:31:22 +10001031 kvfree(qp->sq.wqe_head);
1032 kvfree(qp->sq.w_list);
1033 kvfree(qp->sq.wrid);
1034 kvfree(qp->sq.wr_data);
1035 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001036 mlx5_db_free(dev->mdev, &qp->db);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001037 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001038}
1039
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001040static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001041{
1042 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Moni Shouac32a4f22018-01-02 16:19:32 +02001043 (attr->qp_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001044 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001045 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001046 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001047 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001048 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001049 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001050}
1051
1052static int is_connected(enum ib_qp_type qp_type)
1053{
1054 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1055 return 1;
1056
1057 return 0;
1058}
1059
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001060static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001061 struct mlx5_ib_qp *qp,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001062 struct mlx5_ib_sq *sq, u32 tdn)
1063{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001064 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001065 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1066
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001067 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001068 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1069 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1070
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001071 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1072}
1073
1074static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1075 struct mlx5_ib_sq *sq)
1076{
1077 mlx5_core_destroy_tis(dev->mdev, sq->tisn);
1078}
1079
Mark Blochb96c9dd2018-01-29 10:40:37 +00001080static void destroy_flow_rule_vport_sq(struct mlx5_ib_dev *dev,
1081 struct mlx5_ib_sq *sq)
1082{
1083 if (sq->flow_rule)
1084 mlx5_del_flow_rules(sq->flow_rule);
1085}
1086
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001087static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1088 struct mlx5_ib_sq *sq, void *qpin,
1089 struct ib_pd *pd)
1090{
1091 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1092 __be64 *pas;
1093 void *in;
1094 void *sqc;
1095 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1096 void *wq;
1097 int inlen;
1098 int err;
1099 int page_shift = 0;
1100 int npages;
1101 int ncont = 0;
1102 u32 offset = 0;
1103
1104 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1105 &sq->ubuffer.umem, &npages, &page_shift,
1106 &ncont, &offset);
1107 if (err)
1108 return err;
1109
1110 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001111 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001112 if (!in) {
1113 err = -ENOMEM;
1114 goto err_umem;
1115 }
1116
1117 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1118 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001119 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1120 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001121 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1122 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1123 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1124 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1125 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001126 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1127 MLX5_CAP_ETH(dev->mdev, swp))
1128 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001129
1130 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1131 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1132 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1133 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1134 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1135 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1136 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1137 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1138 MLX5_SET(wq, wq, page_offset, offset);
1139
1140 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1141 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1142
1143 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1144
1145 kvfree(in);
1146
1147 if (err)
1148 goto err_umem;
1149
Mark Blochb96c9dd2018-01-29 10:40:37 +00001150 err = create_flow_rule_vport_sq(dev, sq);
1151 if (err)
1152 goto err_flow;
1153
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001154 return 0;
1155
Mark Blochb96c9dd2018-01-29 10:40:37 +00001156err_flow:
1157 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1158
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001159err_umem:
1160 ib_umem_release(sq->ubuffer.umem);
1161 sq->ubuffer.umem = NULL;
1162
1163 return err;
1164}
1165
1166static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1167 struct mlx5_ib_sq *sq)
1168{
Mark Blochb96c9dd2018-01-29 10:40:37 +00001169 destroy_flow_rule_vport_sq(dev, sq);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001170 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1171 ib_umem_release(sq->ubuffer.umem);
1172}
1173
Boris Pismenny2c292db2018-03-08 15:51:40 +02001174static size_t get_rq_pas_size(void *qpc)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001175{
1176 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1177 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1178 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1179 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1180 u32 po_quanta = 1 << (log_page_size - 6);
1181 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1182 u32 page_size = 1 << log_page_size;
1183 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1184 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1185
1186 return rq_num_pas * sizeof(u64);
1187}
1188
1189static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001190 struct mlx5_ib_rq *rq, void *qpin,
1191 size_t qpinlen)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001192{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001193 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001194 __be64 *pas;
1195 __be64 *qp_pas;
1196 void *in;
1197 void *rqc;
1198 void *wq;
1199 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001200 size_t rq_pas_size = get_rq_pas_size(qpc);
1201 size_t inlen;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001202 int err;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001203
1204 if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
1205 return -EINVAL;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001206
1207 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001208 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001209 if (!in)
1210 return -ENOMEM;
1211
1212 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001213 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1214 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001215 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1216 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1217 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1218 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1219 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1220
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001221 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1222 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1223
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001224 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1225 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001226 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1227 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001228 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1229 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1230 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1231 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1232 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1233 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1234
1235 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1236 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1237 memcpy(pas, qp_pas, rq_pas_size);
1238
1239 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1240
1241 kvfree(in);
1242
1243 return err;
1244}
1245
1246static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1247 struct mlx5_ib_rq *rq)
1248{
1249 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1250}
1251
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001252static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1253{
1254 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1255 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1256 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1257}
1258
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001259static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001260 struct mlx5_ib_rq *rq, u32 tdn,
1261 bool tunnel_offload_en)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001262{
1263 u32 *in;
1264 void *tirc;
1265 int inlen;
1266 int err;
1267
1268 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001269 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001270 if (!in)
1271 return -ENOMEM;
1272
1273 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1274 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1275 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1276 MLX5_SET(tirc, tirc, transport_domain, tdn);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001277 if (tunnel_offload_en)
1278 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001279
Mark Blochec9c2fb2018-01-15 13:11:37 +00001280 if (dev->rep)
1281 MLX5_SET(tirc, tirc, self_lb_block,
1282 MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST_);
1283
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001284 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1285
1286 kvfree(in);
1287
1288 return err;
1289}
1290
1291static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1292 struct mlx5_ib_rq *rq)
1293{
1294 mlx5_core_destroy_tir(dev->mdev, rq->tirn);
1295}
1296
1297static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Boris Pismenny2c292db2018-03-08 15:51:40 +02001298 u32 *in, size_t inlen,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001299 struct ib_pd *pd)
1300{
1301 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1302 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1303 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1304 struct ib_uobject *uobj = pd->uobject;
1305 struct ib_ucontext *ucontext = uobj->context;
1306 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1307 int err;
1308 u32 tdn = mucontext->tdn;
1309
1310 if (qp->sq.wqe_cnt) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001311 err = create_raw_packet_qp_tis(dev, qp, sq, tdn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001312 if (err)
1313 return err;
1314
1315 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1316 if (err)
1317 goto err_destroy_tis;
1318
1319 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001320 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001321 }
1322
1323 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001324 rq->base.container_mibqp = qp;
1325
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001326 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1327 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001328 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1329 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
Boris Pismenny2c292db2018-03-08 15:51:40 +02001330 err = create_raw_packet_qp_rq(dev, rq, in, inlen);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001331 if (err)
1332 goto err_destroy_sq;
1333
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001334
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001335 err = create_raw_packet_qp_tir(dev, rq, tdn,
1336 qp->tunnel_offload_en);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001337 if (err)
1338 goto err_destroy_rq;
1339 }
1340
1341 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1342 rq->base.mqp.qpn;
1343
1344 return 0;
1345
1346err_destroy_rq:
1347 destroy_raw_packet_qp_rq(dev, rq);
1348err_destroy_sq:
1349 if (!qp->sq.wqe_cnt)
1350 return err;
1351 destroy_raw_packet_qp_sq(dev, sq);
1352err_destroy_tis:
1353 destroy_raw_packet_qp_tis(dev, sq);
1354
1355 return err;
1356}
1357
1358static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1359 struct mlx5_ib_qp *qp)
1360{
1361 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1362 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1363 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1364
1365 if (qp->rq.wqe_cnt) {
1366 destroy_raw_packet_qp_tir(dev, rq);
1367 destroy_raw_packet_qp_rq(dev, rq);
1368 }
1369
1370 if (qp->sq.wqe_cnt) {
1371 destroy_raw_packet_qp_sq(dev, sq);
1372 destroy_raw_packet_qp_tis(dev, sq);
1373 }
1374}
1375
1376static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1377 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1378{
1379 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1380 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1381
1382 sq->sq = &qp->sq;
1383 rq->rq = &qp->rq;
1384 sq->doorbell = &qp->db;
1385 rq->doorbell = &qp->db;
1386}
1387
Yishai Hadas28d61372016-05-23 15:20:56 +03001388static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1389{
1390 mlx5_core_destroy_tir(dev->mdev, qp->rss_qp.tirn);
1391}
1392
1393static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1394 struct ib_pd *pd,
1395 struct ib_qp_init_attr *init_attr,
1396 struct ib_udata *udata)
1397{
1398 struct ib_uobject *uobj = pd->uobject;
1399 struct ib_ucontext *ucontext = uobj->context;
1400 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1401 struct mlx5_ib_create_qp_resp resp = {};
1402 int inlen;
1403 int err;
1404 u32 *in;
1405 void *tirc;
1406 void *hfso;
1407 u32 selected_fields = 0;
Matan Barak2d93fc82018-03-28 09:27:55 +03001408 u32 outer_l4;
Yishai Hadas28d61372016-05-23 15:20:56 +03001409 size_t min_resp_len;
1410 u32 tdn = mucontext->tdn;
1411 struct mlx5_ib_create_qp_rss ucmd = {};
1412 size_t required_cmd_sz;
1413
1414 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1415 return -EOPNOTSUPP;
1416
1417 if (init_attr->create_flags || init_attr->send_cq)
1418 return -EINVAL;
1419
Eli Cohen2f5ff262017-01-03 23:55:21 +02001420 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001421 if (udata->outlen < min_resp_len)
1422 return -EINVAL;
1423
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001424 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001425 if (udata->inlen < required_cmd_sz) {
1426 mlx5_ib_dbg(dev, "invalid inlen\n");
1427 return -EINVAL;
1428 }
1429
1430 if (udata->inlen > sizeof(ucmd) &&
1431 !ib_is_udata_cleared(udata, sizeof(ucmd),
1432 udata->inlen - sizeof(ucmd))) {
1433 mlx5_ib_dbg(dev, "inlen is not supported\n");
1434 return -EOPNOTSUPP;
1435 }
1436
1437 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1438 mlx5_ib_dbg(dev, "copy failed\n");
1439 return -EFAULT;
1440 }
1441
1442 if (ucmd.comp_mask) {
1443 mlx5_ib_dbg(dev, "invalid comp mask\n");
1444 return -EOPNOTSUPP;
1445 }
1446
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001447 if (ucmd.flags & ~MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
1448 mlx5_ib_dbg(dev, "invalid flags\n");
1449 return -EOPNOTSUPP;
1450 }
1451
1452 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1453 !tunnel_offload_supported(dev->mdev)) {
1454 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001455 return -EOPNOTSUPP;
1456 }
1457
Maor Gottlieb309fa342017-10-19 08:25:56 +03001458 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1459 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1460 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1461 return -EOPNOTSUPP;
1462 }
1463
Jason Gunthorpe41d902c2018-04-03 10:00:53 +03001464 err = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
Yishai Hadas28d61372016-05-23 15:20:56 +03001465 if (err) {
1466 mlx5_ib_dbg(dev, "copy failed\n");
1467 return -EINVAL;
1468 }
1469
1470 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001471 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001472 if (!in)
1473 return -ENOMEM;
1474
1475 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1476 MLX5_SET(tirc, tirc, disp_type,
1477 MLX5_TIRC_DISP_TYPE_INDIRECT);
1478 MLX5_SET(tirc, tirc, indirect_table,
1479 init_attr->rwq_ind_tbl->ind_tbl_num);
1480 MLX5_SET(tirc, tirc, transport_domain, tdn);
1481
1482 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001483
1484 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1485 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1486
Maor Gottlieb309fa342017-10-19 08:25:56 +03001487 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1488 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1489 else
1490 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1491
Yishai Hadas28d61372016-05-23 15:20:56 +03001492 switch (ucmd.rx_hash_function) {
1493 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1494 {
1495 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1496 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1497
1498 if (len != ucmd.rx_key_len) {
1499 err = -EINVAL;
1500 goto err;
1501 }
1502
1503 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1504 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1505 memcpy(rss_key, ucmd.rx_hash_key, len);
1506 break;
1507 }
1508 default:
1509 err = -EOPNOTSUPP;
1510 goto err;
1511 }
1512
1513 if (!ucmd.rx_hash_fields_mask) {
1514 /* special case when this TIR serves as steering entry without hashing */
1515 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1516 goto create_tir;
1517 err = -EINVAL;
1518 goto err;
1519 }
1520
1521 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1522 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1523 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1524 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1525 err = -EINVAL;
1526 goto err;
1527 }
1528
1529 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1530 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1531 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1532 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1533 MLX5_L3_PROT_TYPE_IPV4);
1534 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1535 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1536 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1537 MLX5_L3_PROT_TYPE_IPV6);
1538
Matan Barak2d93fc82018-03-28 09:27:55 +03001539 outer_l4 = ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1540 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) << 0 |
1541 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1542 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP)) << 1 |
1543 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI) << 2;
1544
1545 /* Check that only one l4 protocol is set */
1546 if (outer_l4 & (outer_l4 - 1)) {
Yishai Hadas28d61372016-05-23 15:20:56 +03001547 err = -EINVAL;
1548 goto err;
1549 }
1550
1551 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1552 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1553 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1554 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1555 MLX5_L4_PROT_TYPE_TCP);
1556 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1557 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1558 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1559 MLX5_L4_PROT_TYPE_UDP);
1560
1561 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1562 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1563 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1564
1565 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1566 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1567 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1568
1569 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1570 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1571 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1572
1573 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1574 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1575 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1576
Matan Barak2d93fc82018-03-28 09:27:55 +03001577 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI)
1578 selected_fields |= MLX5_HASH_FIELD_SEL_IPSEC_SPI;
1579
Yishai Hadas28d61372016-05-23 15:20:56 +03001580 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1581
1582create_tir:
Mark Blochec9c2fb2018-01-15 13:11:37 +00001583 if (dev->rep)
1584 MLX5_SET(tirc, tirc, self_lb_block,
1585 MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST_);
1586
Yishai Hadas28d61372016-05-23 15:20:56 +03001587 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1588
1589 if (err)
1590 goto err;
1591
1592 kvfree(in);
1593 /* qpn is reserved for that QP */
1594 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001595 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001596 return 0;
1597
1598err:
1599 kvfree(in);
1600 return err;
1601}
1602
Eli Cohene126ba92013-07-07 17:25:49 +03001603static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1604 struct ib_qp_init_attr *init_attr,
1605 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1606{
1607 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001608 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001609 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001610 struct mlx5_ib_create_qp_resp resp;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001611 struct mlx5_ib_cq *send_cq;
1612 struct mlx5_ib_cq *recv_cq;
1613 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001614 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001615 struct mlx5_ib_create_qp ucmd;
1616 struct mlx5_ib_qp_base *base;
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001617 int mlx5_st;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001618 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001619 u32 *in;
1620 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001621
1622 mutex_init(&qp->mutex);
1623 spin_lock_init(&qp->sq.lock);
1624 spin_lock_init(&qp->rq.lock);
1625
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001626 mlx5_st = to_mlx5_st(init_attr->qp_type);
1627 if (mlx5_st < 0)
1628 return -EINVAL;
1629
Yishai Hadas28d61372016-05-23 15:20:56 +03001630 if (init_attr->rwq_ind_tbl) {
1631 if (!udata)
1632 return -ENOSYS;
1633
1634 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1635 return err;
1636 }
1637
Eli Cohenf360d882014-04-02 00:10:16 +03001638 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001639 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001640 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1641 return -EINVAL;
1642 } else {
1643 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1644 }
1645 }
1646
Leon Romanovsky051f2632015-12-20 12:16:11 +02001647 if (init_attr->create_flags &
1648 (IB_QP_CREATE_CROSS_CHANNEL |
1649 IB_QP_CREATE_MANAGED_SEND |
1650 IB_QP_CREATE_MANAGED_RECV)) {
1651 if (!MLX5_CAP_GEN(mdev, cd)) {
1652 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1653 return -EINVAL;
1654 }
1655 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1656 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1657 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1658 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1659 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1660 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1661 }
Erez Shitritf0313962016-02-21 16:27:17 +02001662
1663 if (init_attr->qp_type == IB_QPT_UD &&
1664 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1665 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1666 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1667 return -EOPNOTSUPP;
1668 }
1669
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001670 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1671 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1672 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1673 return -EOPNOTSUPP;
1674 }
1675 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1676 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1677 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1678 return -EOPNOTSUPP;
1679 }
1680 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1681 }
1682
Eli Cohene126ba92013-07-07 17:25:49 +03001683 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1684 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1685
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001686 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
1687 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1688 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
1689 (init_attr->qp_type != IB_QPT_RAW_PACKET))
1690 return -EOPNOTSUPP;
1691 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
1692 }
1693
Eli Cohene126ba92013-07-07 17:25:49 +03001694 if (pd && pd->uobject) {
1695 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1696 mlx5_ib_dbg(dev, "copy failed\n");
1697 return -EFAULT;
1698 }
1699
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001700 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1701 &ucmd, udata->inlen, &uidx);
1702 if (err)
1703 return err;
1704
Eli Cohene126ba92013-07-07 17:25:49 +03001705 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1706 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001707 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
1708 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
1709 !tunnel_offload_supported(mdev)) {
1710 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
1711 return -EOPNOTSUPP;
1712 }
1713 qp->tunnel_offload_en = true;
1714 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001715
1716 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
1717 if (init_attr->qp_type != IB_QPT_UD ||
1718 (MLX5_CAP_GEN(dev->mdev, port_type) !=
1719 MLX5_CAP_PORT_TYPE_IB) ||
1720 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
1721 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
1722 return -EOPNOTSUPP;
1723 }
1724
1725 qp->flags |= MLX5_IB_QP_UNDERLAY;
1726 qp->underlay_qpn = init_attr->source_qpn;
1727 }
Eli Cohene126ba92013-07-07 17:25:49 +03001728 } else {
1729 qp->wq_sig = !!wq_signature;
1730 }
1731
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001732 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1733 qp->flags & MLX5_IB_QP_UNDERLAY) ?
1734 &qp->raw_packet_qp.rq.base :
1735 &qp->trans_qp.base;
1736
Eli Cohene126ba92013-07-07 17:25:49 +03001737 qp->has_rq = qp_has_rq(init_attr);
1738 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1739 qp, (pd && pd->uobject) ? &ucmd : NULL);
1740 if (err) {
1741 mlx5_ib_dbg(dev, "err %d\n", err);
1742 return err;
1743 }
1744
1745 if (pd) {
1746 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001747 __u32 max_wqes =
1748 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001749 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1750 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1751 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1752 mlx5_ib_dbg(dev, "invalid rq params\n");
1753 return -EINVAL;
1754 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001755 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001756 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001757 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001758 return -EINVAL;
1759 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001760 if (init_attr->create_flags &
1761 mlx5_ib_create_qp_sqpn_qp1()) {
1762 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1763 return -EINVAL;
1764 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001765 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1766 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001767 if (err)
1768 mlx5_ib_dbg(dev, "err %d\n", err);
1769 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001770 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1771 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001772 if (err)
1773 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001774 }
1775
1776 if (err)
1777 return err;
1778 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001779 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001780 if (!in)
1781 return -ENOMEM;
1782
1783 qp->create_type = MLX5_QP_EMPTY;
1784 }
1785
1786 if (is_sqp(init_attr->qp_type))
1787 qp->port = init_attr->port_num;
1788
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001789 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1790
Noa Osheroviche7b169f2018-02-25 13:39:51 +02001791 MLX5_SET(qpc, qpc, st, mlx5_st);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001792 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001793
1794 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001795 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001796 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001797 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1798
Eli Cohene126ba92013-07-07 17:25:49 +03001799
1800 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001801 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001802
Eli Cohenf360d882014-04-02 00:10:16 +03001803 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001804 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001805
Leon Romanovsky051f2632015-12-20 12:16:11 +02001806 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001807 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001808 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001809 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001810 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001811 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001812
Eli Cohene126ba92013-07-07 17:25:49 +03001813 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1814 int rcqe_sz;
1815 int scqe_sz;
1816
1817 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1818 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1819
1820 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001821 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001822 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001823 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001824
1825 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1826 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001827 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001828 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001829 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001830 }
1831 }
1832
1833 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001834 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1835 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001836 }
1837
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001838 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001839
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001840 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001841 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001842 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001843 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001844 if (init_attr->srq &&
1845 init_attr->srq->srq_type == IB_SRQT_TM)
1846 MLX5_SET(qpc, qpc, offload_type,
1847 MLX5_QPC_OFFLOAD_TYPE_RNDV);
1848 }
Eli Cohene126ba92013-07-07 17:25:49 +03001849
1850 /* Set default resources */
1851 switch (init_attr->qp_type) {
1852 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001853 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1854 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1855 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1856 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001857 break;
1858 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001859 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1860 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1861 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001862 break;
1863 default:
1864 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001865 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1866 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001867 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001868 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1869 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001870 }
1871 }
1872
1873 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001874 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001875
1876 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001877 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001878
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001879 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001880
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001881 /* 0xffffff means we ask to work with cqe version 0 */
1882 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001883 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001884
Erez Shitritf0313962016-02-21 16:27:17 +02001885 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1886 if (init_attr->qp_type == IB_QPT_UD &&
1887 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001888 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1889 qp->flags |= MLX5_IB_QP_LSO;
1890 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001891
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001892 if (init_attr->create_flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
1893 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
1894 mlx5_ib_dbg(dev, "scatter end padding is not supported\n");
1895 err = -EOPNOTSUPP;
1896 goto err;
1897 } else if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1898 MLX5_SET(qpc, qpc, end_padding_mode,
1899 MLX5_WQ_END_PAD_MODE_ALIGN);
1900 } else {
1901 qp->flags |= MLX5_IB_QP_PCI_WRITE_END_PADDING;
1902 }
1903 }
1904
Boris Pismenny2c292db2018-03-08 15:51:40 +02001905 if (inlen < 0) {
1906 err = -EINVAL;
1907 goto err;
1908 }
1909
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001910 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1911 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001912 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
1913 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
Boris Pismenny2c292db2018-03-08 15:51:40 +02001914 err = create_raw_packet_qp(dev, qp, in, inlen, pd);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001915 } else {
1916 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
1917 }
1918
Eli Cohene126ba92013-07-07 17:25:49 +03001919 if (err) {
1920 mlx5_ib_dbg(dev, "create qp failed\n");
1921 goto err_create;
1922 }
1923
Al Viro479163f2014-11-20 08:13:57 +00001924 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001925
majd@mellanox.com19098df2016-01-14 19:13:03 +02001926 base->container_mibqp = qp;
1927 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03001928
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001929 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
1930 &send_cq, &recv_cq);
1931 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1932 mlx5_ib_lock_cqs(send_cq, recv_cq);
1933 /* Maintain device to QPs access, needed for further handling via reset
1934 * flow
1935 */
1936 list_add_tail(&qp->qps_list, &dev->qp_list);
1937 /* Maintain CQ to QPs access, needed for further handling via reset flow
1938 */
1939 if (send_cq)
1940 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
1941 if (recv_cq)
1942 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
1943 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1944 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1945
Eli Cohene126ba92013-07-07 17:25:49 +03001946 return 0;
1947
1948err_create:
1949 if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02001950 destroy_qp_user(dev, pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001951 else if (qp->create_type == MLX5_QP_KERNEL)
1952 destroy_qp_kernel(dev, qp);
1953
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001954err:
Al Viro479163f2014-11-20 08:13:57 +00001955 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001956 return err;
1957}
1958
1959static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1960 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1961{
1962 if (send_cq) {
1963 if (recv_cq) {
1964 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001965 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001966 spin_lock_nested(&recv_cq->lock,
1967 SINGLE_DEPTH_NESTING);
1968 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001969 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001970 __acquire(&recv_cq->lock);
1971 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001972 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001973 spin_lock_nested(&send_cq->lock,
1974 SINGLE_DEPTH_NESTING);
1975 }
1976 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001977 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001978 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001979 }
1980 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001981 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001982 __acquire(&send_cq->lock);
1983 } else {
1984 __acquire(&send_cq->lock);
1985 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001986 }
1987}
1988
1989static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1990 __releases(&send_cq->lock) __releases(&recv_cq->lock)
1991{
1992 if (send_cq) {
1993 if (recv_cq) {
1994 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1995 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001996 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001997 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1998 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001999 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002000 } else {
2001 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002002 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002003 }
2004 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002005 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002006 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002007 }
2008 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02002009 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002010 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02002011 } else {
2012 __release(&recv_cq->lock);
2013 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002014 }
2015}
2016
2017static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
2018{
2019 return to_mpd(qp->ibqp.pd);
2020}
2021
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002022static void get_cqs(enum ib_qp_type qp_type,
2023 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03002024 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
2025{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002026 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002027 case IB_QPT_XRC_TGT:
2028 *send_cq = NULL;
2029 *recv_cq = NULL;
2030 break;
2031 case MLX5_IB_QPT_REG_UMR:
2032 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002033 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002034 *recv_cq = NULL;
2035 break;
2036
2037 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002038 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002039 case IB_QPT_RC:
2040 case IB_QPT_UC:
2041 case IB_QPT_UD:
2042 case IB_QPT_RAW_IPV6:
2043 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002044 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002045 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2046 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002047 break;
2048
Eli Cohene126ba92013-07-07 17:25:49 +03002049 case IB_QPT_MAX:
2050 default:
2051 *send_cq = NULL;
2052 *recv_cq = NULL;
2053 break;
2054 }
2055}
2056
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002057static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002058 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2059 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002060
Eli Cohene126ba92013-07-07 17:25:49 +03002061static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
2062{
2063 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002064 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002065 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002066 int err;
2067
Yishai Hadas28d61372016-05-23 15:20:56 +03002068 if (qp->ibqp.rwq_ind_tbl) {
2069 destroy_rss_raw_qp_tir(dev, qp);
2070 return;
2071 }
2072
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002073 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2074 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002075 &qp->raw_packet_qp.rq.base :
2076 &qp->trans_qp.base;
2077
Haggai Eran6aec21f2014-12-11 17:04:23 +02002078 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002079 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2080 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002081 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002082 MLX5_CMD_OP_2RST_QP, 0,
2083 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002084 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002085 struct mlx5_modify_raw_qp_param raw_qp_param = {
2086 .operation = MLX5_CMD_OP_2RST_QP
2087 };
2088
Aviv Heller13eab212016-09-18 20:48:04 +03002089 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002090 }
2091 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002092 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002093 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002094 }
Eli Cohene126ba92013-07-07 17:25:49 +03002095
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002096 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2097 &send_cq, &recv_cq);
2098
2099 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2100 mlx5_ib_lock_cqs(send_cq, recv_cq);
2101 /* del from lists under both locks above to protect reset flow paths */
2102 list_del(&qp->qps_list);
2103 if (send_cq)
2104 list_del(&qp->cq_send_list);
2105
2106 if (recv_cq)
2107 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002108
2109 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002110 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002111 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2112 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002113 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2114 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002115 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002116 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2117 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002118
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002119 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2120 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002121 destroy_raw_packet_qp(dev, qp);
2122 } else {
2123 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
2124 if (err)
2125 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2126 base->mqp.qpn);
2127 }
Eli Cohene126ba92013-07-07 17:25:49 +03002128
Eli Cohene126ba92013-07-07 17:25:49 +03002129 if (qp->create_type == MLX5_QP_KERNEL)
2130 destroy_qp_kernel(dev, qp);
2131 else if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02002132 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002133}
2134
2135static const char *ib_qp_type_str(enum ib_qp_type type)
2136{
2137 switch (type) {
2138 case IB_QPT_SMI:
2139 return "IB_QPT_SMI";
2140 case IB_QPT_GSI:
2141 return "IB_QPT_GSI";
2142 case IB_QPT_RC:
2143 return "IB_QPT_RC";
2144 case IB_QPT_UC:
2145 return "IB_QPT_UC";
2146 case IB_QPT_UD:
2147 return "IB_QPT_UD";
2148 case IB_QPT_RAW_IPV6:
2149 return "IB_QPT_RAW_IPV6";
2150 case IB_QPT_RAW_ETHERTYPE:
2151 return "IB_QPT_RAW_ETHERTYPE";
2152 case IB_QPT_XRC_INI:
2153 return "IB_QPT_XRC_INI";
2154 case IB_QPT_XRC_TGT:
2155 return "IB_QPT_XRC_TGT";
2156 case IB_QPT_RAW_PACKET:
2157 return "IB_QPT_RAW_PACKET";
2158 case MLX5_IB_QPT_REG_UMR:
2159 return "MLX5_IB_QPT_REG_UMR";
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002160 case IB_QPT_DRIVER:
2161 return "IB_QPT_DRIVER";
Eli Cohene126ba92013-07-07 17:25:49 +03002162 case IB_QPT_MAX:
2163 default:
2164 return "Invalid QP type";
2165 }
2166}
2167
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002168static struct ib_qp *mlx5_ib_create_dct(struct ib_pd *pd,
2169 struct ib_qp_init_attr *attr,
2170 struct mlx5_ib_create_qp *ucmd)
2171{
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002172 struct mlx5_ib_qp *qp;
2173 int err = 0;
2174 u32 uidx = MLX5_IB_DEFAULT_UIDX;
2175 void *dctc;
2176
2177 if (!attr->srq || !attr->recv_cq)
2178 return ERR_PTR(-EINVAL);
2179
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002180 err = get_qp_user_index(to_mucontext(pd->uobject->context),
2181 ucmd, sizeof(*ucmd), &uidx);
2182 if (err)
2183 return ERR_PTR(err);
2184
2185 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2186 if (!qp)
2187 return ERR_PTR(-ENOMEM);
2188
2189 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
2190 if (!qp->dct.in) {
2191 err = -ENOMEM;
2192 goto err_free;
2193 }
2194
2195 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shoua776a3902018-01-02 16:19:33 +02002196 qp->qp_sub_type = MLX5_IB_QPT_DCT;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002197 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2198 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2199 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2200 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2201 MLX5_SET(dctc, dctc, user_index, uidx);
2202
2203 qp->state = IB_QPS_RESET;
2204
2205 return &qp->ibqp;
2206err_free:
2207 kfree(qp);
2208 return ERR_PTR(err);
2209}
2210
2211static int set_mlx_qp_type(struct mlx5_ib_dev *dev,
2212 struct ib_qp_init_attr *init_attr,
2213 struct mlx5_ib_create_qp *ucmd,
2214 struct ib_udata *udata)
2215{
2216 enum { MLX_QP_FLAGS = MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI };
2217 int err;
2218
2219 if (!udata)
2220 return -EINVAL;
2221
2222 if (udata->inlen < sizeof(*ucmd)) {
2223 mlx5_ib_dbg(dev, "create_qp user command is smaller than expected\n");
2224 return -EINVAL;
2225 }
2226 err = ib_copy_from_udata(ucmd, udata, sizeof(*ucmd));
2227 if (err)
2228 return err;
2229
2230 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCI) {
2231 init_attr->qp_type = MLX5_IB_QPT_DCI;
2232 } else {
2233 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCT) {
2234 init_attr->qp_type = MLX5_IB_QPT_DCT;
2235 } else {
2236 mlx5_ib_dbg(dev, "Invalid QP flags\n");
2237 return -EINVAL;
2238 }
2239 }
2240
2241 if (!MLX5_CAP_GEN(dev->mdev, dct)) {
2242 mlx5_ib_dbg(dev, "DC transport is not supported\n");
2243 return -EOPNOTSUPP;
2244 }
2245
2246 return 0;
2247}
2248
Eli Cohene126ba92013-07-07 17:25:49 +03002249struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002250 struct ib_qp_init_attr *verbs_init_attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002251 struct ib_udata *udata)
2252{
2253 struct mlx5_ib_dev *dev;
2254 struct mlx5_ib_qp *qp;
2255 u16 xrcdn = 0;
2256 int err;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002257 struct ib_qp_init_attr mlx_init_attr;
2258 struct ib_qp_init_attr *init_attr = verbs_init_attr;
Eli Cohene126ba92013-07-07 17:25:49 +03002259
2260 if (pd) {
2261 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002262
2263 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2264 if (!pd->uobject) {
2265 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2266 return ERR_PTR(-EINVAL);
2267 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2268 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2269 return ERR_PTR(-EINVAL);
2270 }
2271 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002272 } else {
2273 /* being cautious here */
2274 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2275 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2276 pr_warn("%s: no PD for transport %s\n", __func__,
2277 ib_qp_type_str(init_attr->qp_type));
2278 return ERR_PTR(-EINVAL);
2279 }
2280 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002281 }
2282
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002283 if (init_attr->qp_type == IB_QPT_DRIVER) {
2284 struct mlx5_ib_create_qp ucmd;
2285
2286 init_attr = &mlx_init_attr;
2287 memcpy(init_attr, verbs_init_attr, sizeof(*verbs_init_attr));
2288 err = set_mlx_qp_type(dev, init_attr, &ucmd, udata);
2289 if (err)
2290 return ERR_PTR(err);
Moni Shouac32a4f22018-01-02 16:19:32 +02002291
2292 if (init_attr->qp_type == MLX5_IB_QPT_DCI) {
2293 if (init_attr->cap.max_recv_wr ||
2294 init_attr->cap.max_recv_sge) {
2295 mlx5_ib_dbg(dev, "DCI QP requires zero size receive queue\n");
2296 return ERR_PTR(-EINVAL);
2297 }
Moni Shoua776a3902018-01-02 16:19:33 +02002298 } else {
2299 return mlx5_ib_create_dct(pd, init_attr, &ucmd);
Moni Shouac32a4f22018-01-02 16:19:32 +02002300 }
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002301 }
2302
Eli Cohene126ba92013-07-07 17:25:49 +03002303 switch (init_attr->qp_type) {
2304 case IB_QPT_XRC_TGT:
2305 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002306 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002307 mlx5_ib_dbg(dev, "XRC not supported\n");
2308 return ERR_PTR(-ENOSYS);
2309 }
2310 init_attr->recv_cq = NULL;
2311 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2312 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2313 init_attr->send_cq = NULL;
2314 }
2315
2316 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002317 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002318 case IB_QPT_RC:
2319 case IB_QPT_UC:
2320 case IB_QPT_UD:
2321 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002322 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002323 case MLX5_IB_QPT_REG_UMR:
Moni Shouac32a4f22018-01-02 16:19:32 +02002324 case MLX5_IB_QPT_DCI:
Eli Cohene126ba92013-07-07 17:25:49 +03002325 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2326 if (!qp)
2327 return ERR_PTR(-ENOMEM);
2328
2329 err = create_qp_common(dev, pd, init_attr, udata, qp);
2330 if (err) {
2331 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2332 kfree(qp);
2333 return ERR_PTR(err);
2334 }
2335
2336 if (is_qp0(init_attr->qp_type))
2337 qp->ibqp.qp_num = 0;
2338 else if (is_qp1(init_attr->qp_type))
2339 qp->ibqp.qp_num = 1;
2340 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002341 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002342
2343 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 +02002344 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002345 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2346 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002347
majd@mellanox.com19098df2016-01-14 19:13:03 +02002348 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002349
2350 break;
2351
Haggai Erand16e91d2016-02-29 15:45:05 +02002352 case IB_QPT_GSI:
2353 return mlx5_ib_gsi_create_qp(pd, init_attr);
2354
Eli Cohene126ba92013-07-07 17:25:49 +03002355 case IB_QPT_RAW_IPV6:
2356 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002357 case IB_QPT_MAX:
2358 default:
2359 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2360 init_attr->qp_type);
2361 /* Don't support raw QPs */
2362 return ERR_PTR(-EINVAL);
2363 }
2364
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002365 if (verbs_init_attr->qp_type == IB_QPT_DRIVER)
2366 qp->qp_sub_type = init_attr->qp_type;
2367
Eli Cohene126ba92013-07-07 17:25:49 +03002368 return &qp->ibqp;
2369}
2370
Moni Shoua776a3902018-01-02 16:19:33 +02002371static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2372{
2373 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2374
2375 if (mqp->state == IB_QPS_RTR) {
2376 int err;
2377
2378 err = mlx5_core_destroy_dct(dev->mdev, &mqp->dct.mdct);
2379 if (err) {
2380 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2381 return err;
2382 }
2383 }
2384
2385 kfree(mqp->dct.in);
2386 kfree(mqp);
2387 return 0;
2388}
2389
Eli Cohene126ba92013-07-07 17:25:49 +03002390int mlx5_ib_destroy_qp(struct ib_qp *qp)
2391{
2392 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2393 struct mlx5_ib_qp *mqp = to_mqp(qp);
2394
Haggai Erand16e91d2016-02-29 15:45:05 +02002395 if (unlikely(qp->qp_type == IB_QPT_GSI))
2396 return mlx5_ib_gsi_destroy_qp(qp);
2397
Moni Shoua776a3902018-01-02 16:19:33 +02002398 if (mqp->qp_sub_type == MLX5_IB_QPT_DCT)
2399 return mlx5_ib_destroy_dct(mqp);
2400
Eli Cohene126ba92013-07-07 17:25:49 +03002401 destroy_qp_common(dev, mqp);
2402
2403 kfree(mqp);
2404
2405 return 0;
2406}
2407
2408static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2409 int attr_mask)
2410{
2411 u32 hw_access_flags = 0;
2412 u8 dest_rd_atomic;
2413 u32 access_flags;
2414
2415 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2416 dest_rd_atomic = attr->max_dest_rd_atomic;
2417 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002418 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002419
2420 if (attr_mask & IB_QP_ACCESS_FLAGS)
2421 access_flags = attr->qp_access_flags;
2422 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002423 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002424
2425 if (!dest_rd_atomic)
2426 access_flags &= IB_ACCESS_REMOTE_WRITE;
2427
2428 if (access_flags & IB_ACCESS_REMOTE_READ)
2429 hw_access_flags |= MLX5_QP_BIT_RRE;
2430 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2431 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2432 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2433 hw_access_flags |= MLX5_QP_BIT_RWE;
2434
2435 return cpu_to_be32(hw_access_flags);
2436}
2437
2438enum {
2439 MLX5_PATH_FLAG_FL = 1 << 0,
2440 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2441 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2442};
2443
2444static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2445{
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002446 if (rate == IB_RATE_PORT_CURRENT)
Eli Cohene126ba92013-07-07 17:25:49 +03002447 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002448
Danit Goldberg4f32ac22018-04-23 17:01:54 +03002449 if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS)
2450 return -EINVAL;
2451
2452 while (rate != IB_RATE_PORT_CURRENT &&
2453 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
2454 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
2455 --rate;
2456
2457 return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
Eli Cohene126ba92013-07-07 17:25:49 +03002458}
2459
majd@mellanox.com75850d02016-01-14 19:13:06 +02002460static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
2461 struct mlx5_ib_sq *sq, u8 sl)
2462{
2463 void *in;
2464 void *tisc;
2465 int inlen;
2466 int err;
2467
2468 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002469 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002470 if (!in)
2471 return -ENOMEM;
2472
2473 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
2474
2475 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2476 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2477
2478 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2479
2480 kvfree(in);
2481
2482 return err;
2483}
2484
Aviv Heller13eab212016-09-18 20:48:04 +03002485static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
2486 struct mlx5_ib_sq *sq, u8 tx_affinity)
2487{
2488 void *in;
2489 void *tisc;
2490 int inlen;
2491 int err;
2492
2493 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002494 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002495 if (!in)
2496 return -ENOMEM;
2497
2498 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
2499
2500 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2501 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2502
2503 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2504
2505 kvfree(in);
2506
2507 return err;
2508}
2509
majd@mellanox.com75850d02016-01-14 19:13:06 +02002510static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002511 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002512 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002513 u32 path_flags, const struct ib_qp_attr *attr,
2514 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002515{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002516 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002517 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002518 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002519 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2520 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002521
Eli Cohene126ba92013-07-07 17:25:49 +03002522 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002523 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2524 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002525
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002526 if (ah_flags & IB_AH_GRH) {
2527 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002528 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002529 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002530 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002531 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002532 return -EINVAL;
2533 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002534 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002535
2536 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002537 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002538 return -EINVAL;
Parav Pandit47ec3862018-06-13 10:22:06 +03002539
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002540 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02002541 if (qp->ibqp.qp_type == IB_QPT_RC ||
2542 qp->ibqp.qp_type == IB_QPT_UC ||
2543 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
2544 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
Parav Pandit47ec3862018-06-13 10:22:06 +03002545 path->udp_sport =
2546 mlx5_get_roce_udp_sport(dev, ah->grh.sgid_attr);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002547 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Parav Pandit47ec3862018-06-13 10:22:06 +03002548 gid_type = ah->grh.sgid_attr->gid_type;
Majd Dibbinyed884512017-01-18 14:10:35 +02002549 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002550 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002551 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002552 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2553 path->fl_free_ar |=
2554 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002555 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2556 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2557 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002558 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002559 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002560 }
2561
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002562 if (ah_flags & IB_AH_GRH) {
2563 path->mgid_index = grh->sgid_index;
2564 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002565 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002566 cpu_to_be32((grh->traffic_class << 20) |
2567 (grh->flow_label));
2568 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002569 }
2570
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002571 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002572 if (err < 0)
2573 return err;
2574 path->static_rate = err;
2575 path->port = port;
2576
Eli Cohene126ba92013-07-07 17:25:49 +03002577 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002578 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002579
majd@mellanox.com75850d02016-01-14 19:13:06 +02002580 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2581 return modify_raw_packet_eth_prio(dev->mdev,
2582 &qp->raw_packet_qp.sq,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002583 sl & 0xf);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002584
Eli Cohene126ba92013-07-07 17:25:49 +03002585 return 0;
2586}
2587
2588static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2589 [MLX5_QP_STATE_INIT] = {
2590 [MLX5_QP_STATE_INIT] = {
2591 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2592 MLX5_QP_OPTPAR_RAE |
2593 MLX5_QP_OPTPAR_RWE |
2594 MLX5_QP_OPTPAR_PKEY_INDEX |
2595 MLX5_QP_OPTPAR_PRI_PORT,
2596 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2597 MLX5_QP_OPTPAR_PKEY_INDEX |
2598 MLX5_QP_OPTPAR_PRI_PORT,
2599 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2600 MLX5_QP_OPTPAR_Q_KEY |
2601 MLX5_QP_OPTPAR_PRI_PORT,
2602 },
2603 [MLX5_QP_STATE_RTR] = {
2604 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2605 MLX5_QP_OPTPAR_RRE |
2606 MLX5_QP_OPTPAR_RAE |
2607 MLX5_QP_OPTPAR_RWE |
2608 MLX5_QP_OPTPAR_PKEY_INDEX,
2609 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2610 MLX5_QP_OPTPAR_RWE |
2611 MLX5_QP_OPTPAR_PKEY_INDEX,
2612 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2613 MLX5_QP_OPTPAR_Q_KEY,
2614 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2615 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002616 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2617 MLX5_QP_OPTPAR_RRE |
2618 MLX5_QP_OPTPAR_RAE |
2619 MLX5_QP_OPTPAR_RWE |
2620 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002621 },
2622 },
2623 [MLX5_QP_STATE_RTR] = {
2624 [MLX5_QP_STATE_RTS] = {
2625 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2626 MLX5_QP_OPTPAR_RRE |
2627 MLX5_QP_OPTPAR_RAE |
2628 MLX5_QP_OPTPAR_RWE |
2629 MLX5_QP_OPTPAR_PM_STATE |
2630 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2631 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2632 MLX5_QP_OPTPAR_RWE |
2633 MLX5_QP_OPTPAR_PM_STATE,
2634 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2635 },
2636 },
2637 [MLX5_QP_STATE_RTS] = {
2638 [MLX5_QP_STATE_RTS] = {
2639 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2640 MLX5_QP_OPTPAR_RAE |
2641 MLX5_QP_OPTPAR_RWE |
2642 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002643 MLX5_QP_OPTPAR_PM_STATE |
2644 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002645 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03002646 MLX5_QP_OPTPAR_PM_STATE |
2647 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002648 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
2649 MLX5_QP_OPTPAR_SRQN |
2650 MLX5_QP_OPTPAR_CQN_RCV,
2651 },
2652 },
2653 [MLX5_QP_STATE_SQER] = {
2654 [MLX5_QP_STATE_RTS] = {
2655 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2656 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002657 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002658 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2659 MLX5_QP_OPTPAR_RWE |
2660 MLX5_QP_OPTPAR_RAE |
2661 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002662 },
2663 },
2664};
2665
2666static int ib_nr_to_mlx5_nr(int ib_mask)
2667{
2668 switch (ib_mask) {
2669 case IB_QP_STATE:
2670 return 0;
2671 case IB_QP_CUR_STATE:
2672 return 0;
2673 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2674 return 0;
2675 case IB_QP_ACCESS_FLAGS:
2676 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2677 MLX5_QP_OPTPAR_RAE;
2678 case IB_QP_PKEY_INDEX:
2679 return MLX5_QP_OPTPAR_PKEY_INDEX;
2680 case IB_QP_PORT:
2681 return MLX5_QP_OPTPAR_PRI_PORT;
2682 case IB_QP_QKEY:
2683 return MLX5_QP_OPTPAR_Q_KEY;
2684 case IB_QP_AV:
2685 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2686 MLX5_QP_OPTPAR_PRI_PORT;
2687 case IB_QP_PATH_MTU:
2688 return 0;
2689 case IB_QP_TIMEOUT:
2690 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2691 case IB_QP_RETRY_CNT:
2692 return MLX5_QP_OPTPAR_RETRY_COUNT;
2693 case IB_QP_RNR_RETRY:
2694 return MLX5_QP_OPTPAR_RNR_RETRY;
2695 case IB_QP_RQ_PSN:
2696 return 0;
2697 case IB_QP_MAX_QP_RD_ATOMIC:
2698 return MLX5_QP_OPTPAR_SRA_MAX;
2699 case IB_QP_ALT_PATH:
2700 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2701 case IB_QP_MIN_RNR_TIMER:
2702 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2703 case IB_QP_SQ_PSN:
2704 return 0;
2705 case IB_QP_MAX_DEST_RD_ATOMIC:
2706 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2707 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2708 case IB_QP_PATH_MIG_STATE:
2709 return MLX5_QP_OPTPAR_PM_STATE;
2710 case IB_QP_CAP:
2711 return 0;
2712 case IB_QP_DEST_QPN:
2713 return 0;
2714 }
2715 return 0;
2716}
2717
2718static int ib_mask_to_mlx5_opt(int ib_mask)
2719{
2720 int result = 0;
2721 int i;
2722
2723 for (i = 0; i < 8 * sizeof(int); i++) {
2724 if ((1 << i) & ib_mask)
2725 result |= ib_nr_to_mlx5_nr(1 << i);
2726 }
2727
2728 return result;
2729}
2730
Alex Veskereb49ab02016-08-28 12:25:53 +03002731static int modify_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
2732 struct mlx5_ib_rq *rq, int new_state,
2733 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002734{
2735 void *in;
2736 void *rqc;
2737 int inlen;
2738 int err;
2739
2740 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002741 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002742 if (!in)
2743 return -ENOMEM;
2744
2745 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
2746
2747 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2748 MLX5_SET(rqc, rqc, state, new_state);
2749
Alex Veskereb49ab02016-08-28 12:25:53 +03002750 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2751 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2752 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02002753 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03002754 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2755 } else
2756 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2757 dev->ib_dev.name);
2758 }
2759
2760 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002761 if (err)
2762 goto out;
2763
2764 rq->state = new_state;
2765
2766out:
2767 kvfree(in);
2768 return err;
2769}
2770
2771static int modify_raw_packet_qp_sq(struct mlx5_core_dev *dev,
Bodong Wang7d29f342016-12-01 13:43:16 +02002772 struct mlx5_ib_sq *sq,
2773 int new_state,
2774 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002775{
Bodong Wang7d29f342016-12-01 13:43:16 +02002776 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
Bodong Wang61147f32018-03-19 15:10:30 +02002777 struct mlx5_rate_limit old_rl = ibqp->rl;
2778 struct mlx5_rate_limit new_rl = old_rl;
2779 bool new_rate_added = false;
Bodong Wang7d29f342016-12-01 13:43:16 +02002780 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002781 void *in;
2782 void *sqc;
2783 int inlen;
2784 int err;
2785
2786 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002787 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002788 if (!in)
2789 return -ENOMEM;
2790
2791 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2792
2793 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2794 MLX5_SET(sqc, sqc, state, new_state);
2795
Bodong Wang7d29f342016-12-01 13:43:16 +02002796 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2797 if (new_state != MLX5_SQC_STATE_RDY)
2798 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2799 __func__);
2800 else
Bodong Wang61147f32018-03-19 15:10:30 +02002801 new_rl = raw_qp_param->rl;
Bodong Wang7d29f342016-12-01 13:43:16 +02002802 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002803
Bodong Wang61147f32018-03-19 15:10:30 +02002804 if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
2805 if (new_rl.rate) {
2806 err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02002807 if (err) {
Bodong Wang61147f32018-03-19 15:10:30 +02002808 pr_err("Failed configuring rate limit(err %d): \
2809 rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
2810 err, new_rl.rate, new_rl.max_burst_sz,
2811 new_rl.typical_pkt_sz);
2812
Bodong Wang7d29f342016-12-01 13:43:16 +02002813 goto out;
2814 }
Bodong Wang61147f32018-03-19 15:10:30 +02002815 new_rate_added = true;
Bodong Wang7d29f342016-12-01 13:43:16 +02002816 }
2817
2818 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
Bodong Wang61147f32018-03-19 15:10:30 +02002819 /* index 0 means no limit */
Bodong Wang7d29f342016-12-01 13:43:16 +02002820 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2821 }
2822
2823 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2824 if (err) {
2825 /* Remove new rate from table if failed */
Bodong Wang61147f32018-03-19 15:10:30 +02002826 if (new_rate_added)
2827 mlx5_rl_remove_rate(dev, &new_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02002828 goto out;
2829 }
2830
2831 /* Only remove the old rate after new rate was set */
Bodong Wang61147f32018-03-19 15:10:30 +02002832 if ((old_rl.rate &&
2833 !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
Bodong Wang7d29f342016-12-01 13:43:16 +02002834 (new_state != MLX5_SQC_STATE_RDY))
Bodong Wang61147f32018-03-19 15:10:30 +02002835 mlx5_rl_remove_rate(dev, &old_rl);
Bodong Wang7d29f342016-12-01 13:43:16 +02002836
Bodong Wang61147f32018-03-19 15:10:30 +02002837 ibqp->rl = new_rl;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002838 sq->state = new_state;
2839
2840out:
2841 kvfree(in);
2842 return err;
2843}
2844
2845static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002846 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2847 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002848{
2849 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2850 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2851 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002852 int modify_rq = !!qp->rq.wqe_cnt;
2853 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002854 int rq_state;
2855 int sq_state;
2856 int err;
2857
Alex Vesker0680efa2016-08-28 12:25:52 +03002858 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002859 case MLX5_CMD_OP_RST2INIT_QP:
2860 rq_state = MLX5_RQC_STATE_RDY;
2861 sq_state = MLX5_SQC_STATE_RDY;
2862 break;
2863 case MLX5_CMD_OP_2ERR_QP:
2864 rq_state = MLX5_RQC_STATE_ERR;
2865 sq_state = MLX5_SQC_STATE_ERR;
2866 break;
2867 case MLX5_CMD_OP_2RST_QP:
2868 rq_state = MLX5_RQC_STATE_RST;
2869 sq_state = MLX5_SQC_STATE_RST;
2870 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002871 case MLX5_CMD_OP_RTR2RTS_QP:
2872 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002873 if (raw_qp_param->set_mask ==
2874 MLX5_RAW_QP_RATE_LIMIT) {
2875 modify_rq = 0;
2876 sq_state = sq->state;
2877 } else {
2878 return raw_qp_param->set_mask ? -EINVAL : 0;
2879 }
2880 break;
2881 case MLX5_CMD_OP_INIT2INIT_QP:
2882 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002883 if (raw_qp_param->set_mask)
2884 return -EINVAL;
2885 else
2886 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002887 default:
2888 WARN_ON(1);
2889 return -EINVAL;
2890 }
2891
Bodong Wang7d29f342016-12-01 13:43:16 +02002892 if (modify_rq) {
2893 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002894 if (err)
2895 return err;
2896 }
2897
Bodong Wang7d29f342016-12-01 13:43:16 +02002898 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03002899 if (tx_affinity) {
2900 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
2901 tx_affinity);
2902 if (err)
2903 return err;
2904 }
2905
Bodong Wang7d29f342016-12-01 13:43:16 +02002906 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state, raw_qp_param);
Aviv Heller13eab212016-09-18 20:48:04 +03002907 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002908
2909 return 0;
2910}
2911
Eli Cohene126ba92013-07-07 17:25:49 +03002912static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
2913 const struct ib_qp_attr *attr, int attr_mask,
Bodong Wang61147f32018-03-19 15:10:30 +02002914 enum ib_qp_state cur_state, enum ib_qp_state new_state,
2915 const struct mlx5_ib_modify_qp *ucmd)
Eli Cohene126ba92013-07-07 17:25:49 +03002916{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002917 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
2918 [MLX5_QP_STATE_RST] = {
2919 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2920 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2921 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
2922 },
2923 [MLX5_QP_STATE_INIT] = {
2924 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2925 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2926 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
2927 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
2928 },
2929 [MLX5_QP_STATE_RTR] = {
2930 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2931 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2932 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
2933 },
2934 [MLX5_QP_STATE_RTS] = {
2935 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2936 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2937 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
2938 },
2939 [MLX5_QP_STATE_SQD] = {
2940 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2941 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2942 },
2943 [MLX5_QP_STATE_SQER] = {
2944 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2945 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2946 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
2947 },
2948 [MLX5_QP_STATE_ERR] = {
2949 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2950 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2951 }
2952 };
2953
Eli Cohene126ba92013-07-07 17:25:49 +03002954 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2955 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02002956 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03002957 struct mlx5_ib_cq *send_cq, *recv_cq;
2958 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03002959 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03002960 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002961 enum mlx5_qp_state mlx5_cur, mlx5_new;
2962 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03002963 int mlx5_st;
2964 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002965 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03002966 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002967
Leon Romanovsky55de9a72018-02-25 13:39:52 +02002968 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
2969 qp->qp_sub_type : ibqp->qp_type);
2970 if (mlx5_st < 0)
2971 return -EINVAL;
2972
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002973 context = kzalloc(sizeof(*context), GFP_KERNEL);
2974 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03002975 return -ENOMEM;
2976
Leon Romanovsky55de9a72018-02-25 13:39:52 +02002977 context->flags = cpu_to_be32(mlx5_st << 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002978
2979 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
2980 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2981 } else {
2982 switch (attr->path_mig_state) {
2983 case IB_MIG_MIGRATED:
2984 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2985 break;
2986 case IB_MIG_REARM:
2987 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
2988 break;
2989 case IB_MIG_ARMED:
2990 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
2991 break;
2992 }
2993 }
2994
Aviv Heller13eab212016-09-18 20:48:04 +03002995 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2996 if ((ibqp->qp_type == IB_QPT_RC) ||
2997 (ibqp->qp_type == IB_QPT_UD &&
2998 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
2999 (ibqp->qp_type == IB_QPT_UC) ||
3000 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
3001 (ibqp->qp_type == IB_QPT_XRC_INI) ||
3002 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
3003 if (mlx5_lag_is_active(dev->mdev)) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003004 u8 p = mlx5_core_native_port_num(dev->mdev);
Aviv Heller13eab212016-09-18 20:48:04 +03003005 tx_affinity = (unsigned int)atomic_add_return(1,
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003006 &dev->roce[p].next_port) %
Aviv Heller13eab212016-09-18 20:48:04 +03003007 MLX5_MAX_PORTS + 1;
3008 context->flags |= cpu_to_be32(tx_affinity << 24);
3009 }
3010 }
3011 }
3012
Haggai Erand16e91d2016-02-29 15:45:05 +02003013 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003014 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003015 } else if ((ibqp->qp_type == IB_QPT_UD &&
3016 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03003017 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
3018 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
3019 } else if (attr_mask & IB_QP_PATH_MTU) {
3020 if (attr->path_mtu < IB_MTU_256 ||
3021 attr->path_mtu > IB_MTU_4096) {
3022 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3023 err = -EINVAL;
3024 goto out;
3025 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003026 context->mtu_msgmax = (attr->path_mtu << 5) |
3027 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03003028 }
3029
3030 if (attr_mask & IB_QP_DEST_QPN)
3031 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
3032
3033 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003034 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003035
3036 /* todo implement counter_index functionality */
3037
3038 if (is_sqp(ibqp->qp_type))
3039 context->pri_path.port = qp->port;
3040
3041 if (attr_mask & IB_QP_PORT)
3042 context->pri_path.port = attr->port_num;
3043
3044 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003045 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003046 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003047 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003048 if (err)
3049 goto out;
3050 }
3051
3052 if (attr_mask & IB_QP_TIMEOUT)
3053 context->pri_path.ackto_lt |= attr->timeout << 3;
3054
3055 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003056 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3057 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003058 attr->alt_port_num,
3059 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3060 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003061 if (err)
3062 goto out;
3063 }
3064
3065 pd = get_pd(qp);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003066 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3067 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003068
3069 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3070 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3071 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3072 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3073
3074 if (attr_mask & IB_QP_RNR_RETRY)
3075 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3076
3077 if (attr_mask & IB_QP_RETRY_CNT)
3078 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3079
3080 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3081 if (attr->max_rd_atomic)
3082 context->params1 |=
3083 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3084 }
3085
3086 if (attr_mask & IB_QP_SQ_PSN)
3087 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3088
3089 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3090 if (attr->max_dest_rd_atomic)
3091 context->params2 |=
3092 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3093 }
3094
3095 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
3096 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
3097
3098 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3099 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3100
3101 if (attr_mask & IB_QP_RQ_PSN)
3102 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3103
3104 if (attr_mask & IB_QP_QKEY)
3105 context->qkey = cpu_to_be32(attr->qkey);
3106
3107 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3108 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3109
Mark Bloch0837e862016-06-17 15:10:55 +03003110 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3111 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3112 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003113
3114 /* Underlay port should be used - index 0 function per port */
3115 if (qp->flags & MLX5_IB_QP_UNDERLAY)
3116 port_num = 0;
3117
Alex Veskereb49ab02016-08-28 12:25:53 +03003118 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03003119 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03003120 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003121 }
3122
Eli Cohene126ba92013-07-07 17:25:49 +03003123 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3124 context->sq_crq_size |= cpu_to_be16(1 << 4);
3125
Haggai Eranb11a4f92016-02-29 15:45:03 +02003126 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
3127 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003128
3129 mlx5_cur = to_mlx5_state(cur_state);
3130 mlx5_new = to_mlx5_state(new_state);
Eli Cohene126ba92013-07-07 17:25:49 +03003131
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003132 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
Dan Carpenter5d414b12018-03-06 13:00:31 +03003133 !optab[mlx5_cur][mlx5_new]) {
3134 err = -EINVAL;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003135 goto out;
Dan Carpenter5d414b12018-03-06 13:00:31 +03003136 }
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003137
3138 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003139 optpar = ib_mask_to_mlx5_opt(attr_mask);
3140 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003141
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003142 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3143 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003144 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3145
3146 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003147 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03003148 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003149 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3150 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003151
3152 if (attr_mask & IB_QP_RATE_LIMIT) {
Bodong Wang61147f32018-03-19 15:10:30 +02003153 raw_qp_param.rl.rate = attr->rate_limit;
3154
3155 if (ucmd->burst_info.max_burst_sz) {
3156 if (attr->rate_limit &&
3157 MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
3158 raw_qp_param.rl.max_burst_sz =
3159 ucmd->burst_info.max_burst_sz;
3160 } else {
3161 err = -EINVAL;
3162 goto out;
3163 }
3164 }
3165
3166 if (ucmd->burst_info.typical_pkt_sz) {
3167 if (attr->rate_limit &&
3168 MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
3169 raw_qp_param.rl.typical_pkt_sz =
3170 ucmd->burst_info.typical_pkt_sz;
3171 } else {
3172 err = -EINVAL;
3173 goto out;
3174 }
3175 }
3176
Bodong Wang7d29f342016-12-01 13:43:16 +02003177 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3178 }
3179
Aviv Heller13eab212016-09-18 20:48:04 +03003180 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003181 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003182 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003183 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003184 }
3185
Eli Cohene126ba92013-07-07 17:25:49 +03003186 if (err)
3187 goto out;
3188
3189 qp->state = new_state;
3190
3191 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003192 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003193 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003194 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003195 if (attr_mask & IB_QP_PORT)
3196 qp->port = attr->port_num;
3197 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003198 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003199
3200 /*
3201 * If we moved a kernel QP to RESET, clean up all old CQ
3202 * entries and reinitialize the QP.
3203 */
Leon Romanovsky75a45982018-03-11 13:51:32 +02003204 if (new_state == IB_QPS_RESET &&
3205 !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003206 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003207 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3208 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003209 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003210
3211 qp->rq.head = 0;
3212 qp->rq.tail = 0;
3213 qp->sq.head = 0;
3214 qp->sq.tail = 0;
3215 qp->sq.cur_post = 0;
3216 qp->sq.last_poll = 0;
3217 qp->db.db[MLX5_RCV_DBR] = 0;
3218 qp->db.db[MLX5_SND_DBR] = 0;
3219 }
3220
3221out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003222 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003223 return err;
3224}
3225
Moni Shouac32a4f22018-01-02 16:19:32 +02003226static inline bool is_valid_mask(int mask, int req, int opt)
3227{
3228 if ((mask & req) != req)
3229 return false;
3230
3231 if (mask & ~(req | opt))
3232 return false;
3233
3234 return true;
3235}
3236
3237/* check valid transition for driver QP types
3238 * for now the only QP type that this function supports is DCI
3239 */
3240static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3241 enum ib_qp_attr_mask attr_mask)
3242{
3243 int req = IB_QP_STATE;
3244 int opt = 0;
3245
3246 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3247 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
3248 return is_valid_mask(attr_mask, req, opt);
3249 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3250 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3251 return is_valid_mask(attr_mask, req, opt);
3252 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3253 req |= IB_QP_PATH_MTU;
3254 opt = IB_QP_PKEY_INDEX;
3255 return is_valid_mask(attr_mask, req, opt);
3256 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3257 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3258 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3259 opt = IB_QP_MIN_RNR_TIMER;
3260 return is_valid_mask(attr_mask, req, opt);
3261 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3262 opt = IB_QP_MIN_RNR_TIMER;
3263 return is_valid_mask(attr_mask, req, opt);
3264 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3265 return is_valid_mask(attr_mask, req, opt);
3266 }
3267 return false;
3268}
3269
Moni Shoua776a3902018-01-02 16:19:33 +02003270/* mlx5_ib_modify_dct: modify a DCT QP
3271 * valid transitions are:
3272 * RESET to INIT: must set access_flags, pkey_index and port
3273 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3274 * mtu, gid_index and hop_limit
3275 * Other transitions and attributes are illegal
3276 */
3277static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3278 int attr_mask, struct ib_udata *udata)
3279{
3280 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3281 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3282 enum ib_qp_state cur_state, new_state;
3283 int err = 0;
3284 int required = IB_QP_STATE;
3285 void *dctc;
3286
3287 if (!(attr_mask & IB_QP_STATE))
3288 return -EINVAL;
3289
3290 cur_state = qp->state;
3291 new_state = attr->qp_state;
3292
3293 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3294 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3295 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3296 if (!is_valid_mask(attr_mask, required, 0))
3297 return -EINVAL;
3298
3299 if (attr->port_num == 0 ||
3300 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3301 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3302 attr->port_num, dev->num_ports);
3303 return -EINVAL;
3304 }
3305 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3306 MLX5_SET(dctc, dctc, rre, 1);
3307 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3308 MLX5_SET(dctc, dctc, rwe, 1);
3309 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
3310 if (!mlx5_ib_dc_atomic_is_supported(dev))
3311 return -EOPNOTSUPP;
3312 MLX5_SET(dctc, dctc, rae, 1);
3313 MLX5_SET(dctc, dctc, atomic_mode, MLX5_ATOMIC_MODE_DCT_CX);
3314 }
3315 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3316 MLX5_SET(dctc, dctc, port, attr->port_num);
3317 MLX5_SET(dctc, dctc, counter_set_id, dev->port[attr->port_num - 1].cnts.set_id);
3318
3319 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3320 struct mlx5_ib_modify_qp_resp resp = {};
3321 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3322 sizeof(resp.dctn);
3323
3324 if (udata->outlen < min_resp_len)
3325 return -EINVAL;
3326 resp.response_length = min_resp_len;
3327
3328 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3329 if (!is_valid_mask(attr_mask, required, 0))
3330 return -EINVAL;
3331 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3332 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3333 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3334 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3335 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3336 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3337
3338 err = mlx5_core_create_dct(dev->mdev, &qp->dct.mdct, qp->dct.in,
3339 MLX5_ST_SZ_BYTES(create_dct_in));
3340 if (err)
3341 return err;
3342 resp.dctn = qp->dct.mdct.mqp.qpn;
3343 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3344 if (err) {
3345 mlx5_core_destroy_dct(dev->mdev, &qp->dct.mdct);
3346 return err;
3347 }
3348 } else {
3349 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3350 return -EINVAL;
3351 }
3352 if (err)
3353 qp->state = IB_QPS_ERR;
3354 else
3355 qp->state = new_state;
3356 return err;
3357}
3358
Eli Cohene126ba92013-07-07 17:25:49 +03003359int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3360 int attr_mask, struct ib_udata *udata)
3361{
3362 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3363 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Bodong Wang61147f32018-03-19 15:10:30 +02003364 struct mlx5_ib_modify_qp ucmd = {};
Haggai Erand16e91d2016-02-29 15:45:05 +02003365 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003366 enum ib_qp_state cur_state, new_state;
Bodong Wang61147f32018-03-19 15:10:30 +02003367 size_t required_cmd_sz;
Eli Cohene126ba92013-07-07 17:25:49 +03003368 int err = -EINVAL;
3369 int port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003370 enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
Eli Cohene126ba92013-07-07 17:25:49 +03003371
Yishai Hadas28d61372016-05-23 15:20:56 +03003372 if (ibqp->rwq_ind_tbl)
3373 return -ENOSYS;
3374
Bodong Wang61147f32018-03-19 15:10:30 +02003375 if (udata && udata->inlen) {
3376 required_cmd_sz = offsetof(typeof(ucmd), reserved) +
3377 sizeof(ucmd.reserved);
3378 if (udata->inlen < required_cmd_sz)
3379 return -EINVAL;
3380
3381 if (udata->inlen > sizeof(ucmd) &&
3382 !ib_is_udata_cleared(udata, sizeof(ucmd),
3383 udata->inlen - sizeof(ucmd)))
3384 return -EOPNOTSUPP;
3385
3386 if (ib_copy_from_udata(&ucmd, udata,
3387 min(udata->inlen, sizeof(ucmd))))
3388 return -EFAULT;
3389
3390 if (ucmd.comp_mask ||
3391 memchr_inv(&ucmd.reserved, 0, sizeof(ucmd.reserved)) ||
3392 memchr_inv(&ucmd.burst_info.reserved, 0,
3393 sizeof(ucmd.burst_info.reserved)))
3394 return -EOPNOTSUPP;
3395 }
3396
Haggai Erand16e91d2016-02-29 15:45:05 +02003397 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3398 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3399
Moni Shouac32a4f22018-01-02 16:19:32 +02003400 if (ibqp->qp_type == IB_QPT_DRIVER)
3401 qp_type = qp->qp_sub_type;
3402 else
3403 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3404 IB_QPT_GSI : ibqp->qp_type;
3405
Moni Shoua776a3902018-01-02 16:19:33 +02003406 if (qp_type == MLX5_IB_QPT_DCT)
3407 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02003408
Eli Cohene126ba92013-07-07 17:25:49 +03003409 mutex_lock(&qp->mutex);
3410
3411 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
3412 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
3413
Achiad Shochat2811ba52015-12-23 18:47:24 +02003414 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
3415 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
3416 ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
3417 }
3418
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003419 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
3420 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
3421 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
3422 attr_mask);
3423 goto out;
3424 }
3425 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02003426 qp_type != MLX5_IB_QPT_DCI &&
3427 !ib_modify_qp_is_ok(cur_state, new_state, qp_type, attr_mask, ll)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003428 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3429 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003430 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02003431 } else if (qp_type == MLX5_IB_QPT_DCI &&
3432 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
3433 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3434 cur_state, new_state, qp_type, attr_mask);
3435 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003436 }
Eli Cohene126ba92013-07-07 17:25:49 +03003437
3438 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003439 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003440 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003441 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3442 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003443 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003444 }
Eli Cohene126ba92013-07-07 17:25:49 +03003445
3446 if (attr_mask & IB_QP_PKEY_INDEX) {
3447 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003448 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02003449 dev->mdev->port_caps[port - 1].pkey_table_len) {
3450 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
3451 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003452 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003453 }
Eli Cohene126ba92013-07-07 17:25:49 +03003454 }
3455
3456 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003457 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003458 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
3459 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
3460 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003461 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003462 }
Eli Cohene126ba92013-07-07 17:25:49 +03003463
3464 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003465 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003466 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
3467 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
3468 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003469 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003470 }
Eli Cohene126ba92013-07-07 17:25:49 +03003471
3472 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
3473 err = 0;
3474 goto out;
3475 }
3476
Bodong Wang61147f32018-03-19 15:10:30 +02003477 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
3478 new_state, &ucmd);
Eli Cohene126ba92013-07-07 17:25:49 +03003479
3480out:
3481 mutex_unlock(&qp->mutex);
3482 return err;
3483}
3484
3485static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
3486{
3487 struct mlx5_ib_cq *cq;
3488 unsigned cur;
3489
3490 cur = wq->head - wq->tail;
3491 if (likely(cur + nreq < wq->max_post))
3492 return 0;
3493
3494 cq = to_mcq(ib_cq);
3495 spin_lock(&cq->lock);
3496 cur = wq->head - wq->tail;
3497 spin_unlock(&cq->lock);
3498
3499 return cur + nreq >= wq->max_post;
3500}
3501
3502static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
3503 u64 remote_addr, u32 rkey)
3504{
3505 rseg->raddr = cpu_to_be64(remote_addr);
3506 rseg->rkey = cpu_to_be32(rkey);
3507 rseg->reserved = 0;
3508}
3509
Erez Shitritf0313962016-02-21 16:27:17 +02003510static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07003511 const struct ib_send_wr *wr, void *qend,
Erez Shitritf0313962016-02-21 16:27:17 +02003512 struct mlx5_ib_qp *qp, int *size)
3513{
3514 void *seg = eseg;
3515
3516 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
3517
3518 if (wr->send_flags & IB_SEND_IP_CSUM)
3519 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
3520 MLX5_ETH_WQE_L4_CSUM;
3521
3522 seg += sizeof(struct mlx5_wqe_eth_seg);
3523 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3524
3525 if (wr->opcode == IB_WR_LSO) {
3526 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003527 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr.start);
Erez Shitritf0313962016-02-21 16:27:17 +02003528 u64 left, leftlen, copysz;
3529 void *pdata = ud_wr->header;
3530
3531 left = ud_wr->hlen;
3532 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003533 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02003534
3535 /*
3536 * check if there is space till the end of queue, if yes,
3537 * copy all in one shot, otherwise copy till the end of queue,
3538 * rollback and than the copy the left
3539 */
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003540 leftlen = qend - (void *)eseg->inline_hdr.start;
Erez Shitritf0313962016-02-21 16:27:17 +02003541 copysz = min_t(u64, leftlen, left);
3542
3543 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3544
3545 if (likely(copysz > size_of_inl_hdr_start)) {
3546 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3547 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3548 }
3549
3550 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3551 seg = mlx5_get_send_wqe(qp, 0);
3552 left -= copysz;
3553 pdata += copysz;
3554 memcpy(seg, pdata, left);
3555 seg += ALIGN(left, 16);
3556 *size += ALIGN(left, 16) / 16;
3557 }
3558 }
3559
3560 return seg;
3561}
3562
Eli Cohene126ba92013-07-07 17:25:49 +03003563static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07003564 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03003565{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003566 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3567 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3568 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003569}
3570
3571static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3572{
3573 dseg->byte_count = cpu_to_be32(sg->length);
3574 dseg->lkey = cpu_to_be32(sg->lkey);
3575 dseg->addr = cpu_to_be64(sg->addr);
3576}
3577
Artemy Kovalyov31616252017-01-02 11:37:42 +02003578static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03003579{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003580 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
3581 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03003582}
3583
3584static __be64 frwr_mkey_mask(void)
3585{
3586 u64 result;
3587
3588 result = MLX5_MKEY_MASK_LEN |
3589 MLX5_MKEY_MASK_PAGE_SIZE |
3590 MLX5_MKEY_MASK_START_ADDR |
3591 MLX5_MKEY_MASK_EN_RINVAL |
3592 MLX5_MKEY_MASK_KEY |
3593 MLX5_MKEY_MASK_LR |
3594 MLX5_MKEY_MASK_LW |
3595 MLX5_MKEY_MASK_RR |
3596 MLX5_MKEY_MASK_RW |
3597 MLX5_MKEY_MASK_A |
3598 MLX5_MKEY_MASK_SMALL_FENCE |
3599 MLX5_MKEY_MASK_FREE;
3600
3601 return cpu_to_be64(result);
3602}
3603
Sagi Grimberge6631812014-02-23 14:19:11 +02003604static __be64 sig_mkey_mask(void)
3605{
3606 u64 result;
3607
3608 result = MLX5_MKEY_MASK_LEN |
3609 MLX5_MKEY_MASK_PAGE_SIZE |
3610 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003611 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003612 MLX5_MKEY_MASK_EN_RINVAL |
3613 MLX5_MKEY_MASK_KEY |
3614 MLX5_MKEY_MASK_LR |
3615 MLX5_MKEY_MASK_LW |
3616 MLX5_MKEY_MASK_RR |
3617 MLX5_MKEY_MASK_RW |
3618 MLX5_MKEY_MASK_SMALL_FENCE |
3619 MLX5_MKEY_MASK_FREE |
3620 MLX5_MKEY_MASK_BSF_EN;
3621
3622 return cpu_to_be64(result);
3623}
3624
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003625static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Idan Burstein064e5262018-05-02 13:16:39 +03003626 struct mlx5_ib_mr *mr, bool umr_inline)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003627{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003628 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003629
3630 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003631
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003632 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Idan Burstein064e5262018-05-02 13:16:39 +03003633 if (umr_inline)
3634 umr->flags |= MLX5_UMR_INLINE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003635 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003636 umr->mkey_mask = frwr_mkey_mask();
3637}
3638
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003639static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003640{
3641 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003642 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003643 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003644}
3645
Artemy Kovalyov31616252017-01-02 11:37:42 +02003646static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003647{
3648 u64 result;
3649
Artemy Kovalyov31616252017-01-02 11:37:42 +02003650 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02003651 MLX5_MKEY_MASK_FREE;
3652
3653 return cpu_to_be64(result);
3654}
3655
Artemy Kovalyov31616252017-01-02 11:37:42 +02003656static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003657{
3658 u64 result;
3659
3660 result = MLX5_MKEY_MASK_FREE;
3661
3662 return cpu_to_be64(result);
3663}
3664
Noa Osherovich56e11d62016-02-29 16:46:51 +02003665static __be64 get_umr_update_translation_mask(void)
3666{
3667 u64 result;
3668
3669 result = MLX5_MKEY_MASK_LEN |
3670 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003671 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003672
3673 return cpu_to_be64(result);
3674}
3675
Artemy Kovalyov31616252017-01-02 11:37:42 +02003676static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02003677{
3678 u64 result;
3679
Artemy Kovalyov31616252017-01-02 11:37:42 +02003680 result = MLX5_MKEY_MASK_LR |
3681 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003682 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003683 MLX5_MKEY_MASK_RW;
3684
3685 if (atomic)
3686 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003687
3688 return cpu_to_be64(result);
3689}
3690
3691static __be64 get_umr_update_pd_mask(void)
3692{
3693 u64 result;
3694
Artemy Kovalyov31616252017-01-02 11:37:42 +02003695 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003696
3697 return cpu_to_be64(result);
3698}
3699
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02003700static int umr_check_mkey_mask(struct mlx5_ib_dev *dev, u64 mask)
3701{
3702 if ((mask & MLX5_MKEY_MASK_PAGE_SIZE &&
3703 MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) ||
3704 (mask & MLX5_MKEY_MASK_A &&
3705 MLX5_CAP_GEN(dev->mdev, umr_modify_atomic_disabled)))
3706 return -EPERM;
3707 return 0;
3708}
3709
3710static int set_reg_umr_segment(struct mlx5_ib_dev *dev,
3711 struct mlx5_wqe_umr_ctrl_seg *umr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07003712 const struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003713{
Bart Van Asschef696bf62018-07-18 09:25:14 -07003714 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003715
3716 memset(umr, 0, sizeof(*umr));
3717
Haggai Eran968e78d2014-12-11 17:04:11 +02003718 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3719 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3720 else
3721 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3722
Artemy Kovalyov31616252017-01-02 11:37:42 +02003723 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
3724 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
3725 u64 offset = get_xlt_octo(umrwr->offset);
3726
3727 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
3728 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
3729 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003730 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02003731 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3732 umr->mkey_mask |= get_umr_update_translation_mask();
3733 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
3734 umr->mkey_mask |= get_umr_update_access_mask(atomic);
3735 umr->mkey_mask |= get_umr_update_pd_mask();
3736 }
3737 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
3738 umr->mkey_mask |= get_umr_enable_mr_mask();
3739 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
3740 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003741
3742 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003743 umr->flags |= MLX5_UMR_INLINE;
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02003744
3745 return umr_check_mkey_mask(dev, be64_to_cpu(umr->mkey_mask));
Eli Cohene126ba92013-07-07 17:25:49 +03003746}
3747
3748static u8 get_umr_flags(int acc)
3749{
3750 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3751 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3752 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3753 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003754 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003755}
3756
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003757static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3758 struct mlx5_ib_mr *mr,
3759 u32 key, int access)
3760{
3761 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3762
3763 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003764
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003765 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003766 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003767 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003768 /* KLMs take twice the size of MTTs */
3769 ndescs *= 2;
3770
3771 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003772 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3773 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3774 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3775 seg->len = cpu_to_be64(mr->ibmr.length);
3776 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003777}
3778
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003779static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003780{
3781 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003782 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003783}
3784
Bart Van Asschef696bf62018-07-18 09:25:14 -07003785static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg,
3786 const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03003787{
Bart Van Asschef696bf62018-07-18 09:25:14 -07003788 const struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003789
Eli Cohene126ba92013-07-07 17:25:49 +03003790 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02003791 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02003792 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003793
Haggai Eran968e78d2014-12-11 17:04:11 +02003794 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003795 if (umrwr->pd)
3796 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
3797 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
3798 !umrwr->length)
3799 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
3800
3801 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003802 seg->len = cpu_to_be64(umrwr->length);
3803 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003804 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003805 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003806}
3807
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003808static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3809 struct mlx5_ib_mr *mr,
3810 struct mlx5_ib_pd *pd)
3811{
3812 int bcount = mr->desc_size * mr->ndescs;
3813
3814 dseg->addr = cpu_to_be64(mr->desc_map);
3815 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3816 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3817}
3818
Idan Burstein064e5262018-05-02 13:16:39 +03003819static void set_reg_umr_inline_seg(void *seg, struct mlx5_ib_qp *qp,
3820 struct mlx5_ib_mr *mr, int mr_list_size)
3821{
3822 void *qend = qp->sq.qend;
3823 void *addr = mr->descs;
3824 int copy;
3825
3826 if (unlikely(seg + mr_list_size > qend)) {
3827 copy = qend - seg;
3828 memcpy(seg, addr, copy);
3829 addr += copy;
3830 mr_list_size -= copy;
3831 seg = mlx5_get_send_wqe(qp, 0);
3832 }
3833 memcpy(seg, addr, mr_list_size);
3834 seg += mr_list_size;
3835}
3836
Bart Van Asschef696bf62018-07-18 09:25:14 -07003837static __be32 send_ieth(const struct ib_send_wr *wr)
Eli Cohene126ba92013-07-07 17:25:49 +03003838{
3839 switch (wr->opcode) {
3840 case IB_WR_SEND_WITH_IMM:
3841 case IB_WR_RDMA_WRITE_WITH_IMM:
3842 return wr->ex.imm_data;
3843
3844 case IB_WR_SEND_WITH_INV:
3845 return cpu_to_be32(wr->ex.invalidate_rkey);
3846
3847 default:
3848 return 0;
3849 }
3850}
3851
3852static u8 calc_sig(void *wqe, int size)
3853{
3854 u8 *p = wqe;
3855 u8 res = 0;
3856 int i;
3857
3858 for (i = 0; i < size; i++)
3859 res ^= p[i];
3860
3861 return ~res;
3862}
3863
3864static u8 wq_sig(void *wqe)
3865{
3866 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
3867}
3868
Bart Van Asschef696bf62018-07-18 09:25:14 -07003869static int set_data_inl_seg(struct mlx5_ib_qp *qp, const struct ib_send_wr *wr,
Eli Cohene126ba92013-07-07 17:25:49 +03003870 void *wqe, int *sz)
3871{
3872 struct mlx5_wqe_inline_seg *seg;
3873 void *qend = qp->sq.qend;
3874 void *addr;
3875 int inl = 0;
3876 int copy;
3877 int len;
3878 int i;
3879
3880 seg = wqe;
3881 wqe += sizeof(*seg);
3882 for (i = 0; i < wr->num_sge; i++) {
3883 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
3884 len = wr->sg_list[i].length;
3885 inl += len;
3886
3887 if (unlikely(inl > qp->max_inline_data))
3888 return -ENOMEM;
3889
3890 if (unlikely(wqe + len > qend)) {
3891 copy = qend - wqe;
3892 memcpy(wqe, addr, copy);
3893 addr += copy;
3894 len -= copy;
3895 wqe = mlx5_get_send_wqe(qp, 0);
3896 }
3897 memcpy(wqe, addr, len);
3898 wqe += len;
3899 }
3900
3901 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
3902
3903 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
3904
3905 return 0;
3906}
3907
Sagi Grimberge6631812014-02-23 14:19:11 +02003908static u16 prot_field_size(enum ib_signature_type type)
3909{
3910 switch (type) {
3911 case IB_SIG_TYPE_T10_DIF:
3912 return MLX5_DIF_SIZE;
3913 default:
3914 return 0;
3915 }
3916}
3917
3918static u8 bs_selector(int block_size)
3919{
3920 switch (block_size) {
3921 case 512: return 0x1;
3922 case 520: return 0x2;
3923 case 4096: return 0x3;
3924 case 4160: return 0x4;
3925 case 1073741824: return 0x5;
3926 default: return 0;
3927 }
3928}
3929
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003930static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
3931 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02003932{
Sagi Grimberg142537f2014-08-13 19:54:32 +03003933 /* Valid inline section and allow BSF refresh */
3934 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
3935 MLX5_BSF_REFRESH_DIF);
3936 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
3937 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003938 /* repeating block */
3939 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
3940 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
3941 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003942
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003943 if (domain->sig.dif.ref_remap)
3944 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02003945
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003946 if (domain->sig.dif.app_escape) {
3947 if (domain->sig.dif.ref_escape)
3948 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
3949 else
3950 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02003951 }
3952
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003953 inl->dif_app_bitmask_check =
3954 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02003955}
3956
3957static int mlx5_set_bsf(struct ib_mr *sig_mr,
3958 struct ib_sig_attrs *sig_attrs,
3959 struct mlx5_bsf *bsf, u32 data_size)
3960{
3961 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
3962 struct mlx5_bsf_basic *basic = &bsf->basic;
3963 struct ib_sig_domain *mem = &sig_attrs->mem;
3964 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02003965
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003966 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02003967
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003968 /* Basic + Extended + Inline */
3969 basic->bsf_size_sbs = 1 << 7;
3970 /* Input domain check byte mask */
3971 basic->check_byte_mask = sig_attrs->check_mask;
3972 basic->raw_data_size = cpu_to_be32(data_size);
3973
3974 /* Memory domain */
3975 switch (sig_attrs->mem.sig_type) {
3976 case IB_SIG_TYPE_NONE:
3977 break;
3978 case IB_SIG_TYPE_T10_DIF:
3979 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
3980 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
3981 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
3982 break;
3983 default:
3984 return -EINVAL;
3985 }
3986
3987 /* Wire domain */
3988 switch (sig_attrs->wire.sig_type) {
3989 case IB_SIG_TYPE_NONE:
3990 break;
3991 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02003992 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003993 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003994 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03003995 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02003996 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003997 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003998 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003999 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03004000 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03004001 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02004002 } else
4003 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
4004
Sagi Grimberg142537f2014-08-13 19:54:32 +03004005 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004006 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02004007 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004008 default:
4009 return -EINVAL;
4010 }
4011
4012 return 0;
4013}
4014
Bart Van Asschef696bf62018-07-18 09:25:14 -07004015static int set_sig_data_segment(const struct ib_sig_handover_wr *wr,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004016 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004017{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004018 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
4019 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004020 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004021 u32 data_len = wr->wr.sg_list->length;
4022 u32 data_key = wr->wr.sg_list->lkey;
4023 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004024 int ret;
4025 int wqe_size;
4026
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004027 if (!wr->prot ||
4028 (data_key == wr->prot->lkey &&
4029 data_va == wr->prot->addr &&
4030 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02004031 /**
4032 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004033 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02004034 * So need construct:
4035 * ------------------
4036 * | data_klm |
4037 * ------------------
4038 * | BSF |
4039 * ------------------
4040 **/
4041 struct mlx5_klm *data_klm = *seg;
4042
4043 data_klm->bcount = cpu_to_be32(data_len);
4044 data_klm->key = cpu_to_be32(data_key);
4045 data_klm->va = cpu_to_be64(data_va);
4046 wqe_size = ALIGN(sizeof(*data_klm), 64);
4047 } else {
4048 /**
4049 * Source domain contains signature information
4050 * So need construct a strided block format:
4051 * ---------------------------
4052 * | stride_block_ctrl |
4053 * ---------------------------
4054 * | data_klm |
4055 * ---------------------------
4056 * | prot_klm |
4057 * ---------------------------
4058 * | BSF |
4059 * ---------------------------
4060 **/
4061 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
4062 struct mlx5_stride_block_entry *data_sentry;
4063 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004064 u32 prot_key = wr->prot->lkey;
4065 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004066 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
4067 int prot_size;
4068
4069 sblock_ctrl = *seg;
4070 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
4071 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
4072
4073 prot_size = prot_field_size(sig_attrs->mem.sig_type);
4074 if (!prot_size) {
4075 pr_err("Bad block size given: %u\n", block_size);
4076 return -EINVAL;
4077 }
4078 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
4079 prot_size);
4080 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
4081 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
4082 sblock_ctrl->num_entries = cpu_to_be16(2);
4083
4084 data_sentry->bcount = cpu_to_be16(block_size);
4085 data_sentry->key = cpu_to_be32(data_key);
4086 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004087 data_sentry->stride = cpu_to_be16(block_size);
4088
Sagi Grimberge6631812014-02-23 14:19:11 +02004089 prot_sentry->bcount = cpu_to_be16(prot_size);
4090 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03004091 prot_sentry->va = cpu_to_be64(prot_va);
4092 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004093
Sagi Grimberge6631812014-02-23 14:19:11 +02004094 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
4095 sizeof(*prot_sentry), 64);
4096 }
4097
4098 *seg += wqe_size;
4099 *size += wqe_size / 16;
4100 if (unlikely((*seg == qp->sq.qend)))
4101 *seg = mlx5_get_send_wqe(qp, 0);
4102
4103 bsf = *seg;
4104 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
4105 if (ret)
4106 return -EINVAL;
4107
4108 *seg += sizeof(*bsf);
4109 *size += sizeof(*bsf) / 16;
4110 if (unlikely((*seg == qp->sq.qend)))
4111 *seg = mlx5_get_send_wqe(qp, 0);
4112
4113 return 0;
4114}
4115
4116static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004117 const struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02004118 u32 length, u32 pdn)
4119{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004120 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004121 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004122 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004123
4124 memset(seg, 0, sizeof(*seg));
4125
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004126 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004127 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004128 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004129 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004130 MLX5_MKEY_BSF_EN | pdn);
4131 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004132 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004133 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4134}
4135
4136static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004137 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004138{
4139 memset(umr, 0, sizeof(*umr));
4140
4141 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004142 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004143 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4144 umr->mkey_mask = sig_mkey_mask();
4145}
4146
4147
Bart Van Asschef696bf62018-07-18 09:25:14 -07004148static int set_sig_umr_wr(const struct ib_send_wr *send_wr,
4149 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004150{
Bart Van Asschef696bf62018-07-18 09:25:14 -07004151 const struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004152 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004153 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004154 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02004155 int region_len, ret;
4156
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004157 if (unlikely(wr->wr.num_sge != 1) ||
4158 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004159 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
4160 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02004161 return -EINVAL;
4162
4163 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004164 region_len = wr->wr.sg_list->length;
4165 if (wr->prot &&
4166 (wr->prot->lkey != wr->wr.sg_list->lkey ||
4167 wr->prot->addr != wr->wr.sg_list->addr ||
4168 wr->prot->length != wr->wr.sg_list->length))
4169 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02004170
4171 /**
4172 * KLM octoword size - if protection was provided
4173 * then we use strided block format (3 octowords),
4174 * else we use single KLM (1 octoword)
4175 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02004176 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02004177
Artemy Kovalyov31616252017-01-02 11:37:42 +02004178 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004179 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4180 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4181 if (unlikely((*seg == qp->sq.qend)))
4182 *seg = mlx5_get_send_wqe(qp, 0);
4183
Artemy Kovalyov31616252017-01-02 11:37:42 +02004184 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02004185 *seg += sizeof(struct mlx5_mkey_seg);
4186 *size += sizeof(struct mlx5_mkey_seg) / 16;
4187 if (unlikely((*seg == qp->sq.qend)))
4188 *seg = mlx5_get_send_wqe(qp, 0);
4189
4190 ret = set_sig_data_segment(wr, qp, seg, size);
4191 if (ret)
4192 return ret;
4193
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004194 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004195 return 0;
4196}
4197
4198static int set_psv_wr(struct ib_sig_domain *domain,
4199 u32 psv_idx, void **seg, int *size)
4200{
4201 struct mlx5_seg_set_psv *psv_seg = *seg;
4202
4203 memset(psv_seg, 0, sizeof(*psv_seg));
4204 psv_seg->psv_num = cpu_to_be32(psv_idx);
4205 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004206 case IB_SIG_TYPE_NONE:
4207 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004208 case IB_SIG_TYPE_T10_DIF:
4209 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4210 domain->sig.dif.app_tag);
4211 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004212 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004213 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004214 pr_err("Bad signature type (%d) is given.\n",
4215 domain->sig_type);
4216 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004217 }
4218
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004219 *seg += sizeof(*psv_seg);
4220 *size += sizeof(*psv_seg) / 16;
4221
Sagi Grimberge6631812014-02-23 14:19:11 +02004222 return 0;
4223}
4224
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004225static int set_reg_wr(struct mlx5_ib_qp *qp,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004226 const struct ib_reg_wr *wr,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004227 void **seg, int *size)
4228{
4229 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4230 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
Idan Burstein064e5262018-05-02 13:16:39 +03004231 int mr_list_size = mr->ndescs * mr->desc_size;
4232 bool umr_inline = mr_list_size <= MLX5_IB_SQ_UMR_INLINE_THRESHOLD;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004233
4234 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4235 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4236 "Invalid IB_SEND_INLINE send flag\n");
4237 return -EINVAL;
4238 }
4239
Idan Burstein064e5262018-05-02 13:16:39 +03004240 set_reg_umr_seg(*seg, mr, umr_inline);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004241 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4242 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4243 if (unlikely((*seg == qp->sq.qend)))
4244 *seg = mlx5_get_send_wqe(qp, 0);
4245
4246 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4247 *seg += sizeof(struct mlx5_mkey_seg);
4248 *size += sizeof(struct mlx5_mkey_seg) / 16;
4249 if (unlikely((*seg == qp->sq.qend)))
4250 *seg = mlx5_get_send_wqe(qp, 0);
4251
Idan Burstein064e5262018-05-02 13:16:39 +03004252 if (umr_inline) {
4253 set_reg_umr_inline_seg(*seg, qp, mr, mr_list_size);
4254 *size += get_xlt_octo(mr_list_size);
4255 } else {
4256 set_reg_data_seg(*seg, mr, pd);
4257 *seg += sizeof(struct mlx5_wqe_data_seg);
4258 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4259 }
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004260 return 0;
4261}
4262
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004263static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
Eli Cohene126ba92013-07-07 17:25:49 +03004264{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004265 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004266 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4267 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4268 if (unlikely((*seg == qp->sq.qend)))
4269 *seg = mlx5_get_send_wqe(qp, 0);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004270 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004271 *seg += sizeof(struct mlx5_mkey_seg);
4272 *size += sizeof(struct mlx5_mkey_seg) / 16;
4273 if (unlikely((*seg == qp->sq.qend)))
4274 *seg = mlx5_get_send_wqe(qp, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004275}
4276
4277static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
4278{
4279 __be32 *p = NULL;
4280 int tidx = idx;
4281 int i, j;
4282
4283 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
4284 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4285 if ((i & 0xf) == 0) {
4286 void *buf = mlx5_get_send_wqe(qp, tidx);
4287 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
4288 p = buf;
4289 j = 0;
4290 }
4291 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4292 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4293 be32_to_cpu(p[j + 3]));
4294 }
4295}
4296
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004297static int __begin_wqe(struct mlx5_ib_qp *qp, void **seg,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004298 struct mlx5_wqe_ctrl_seg **ctrl,
Bart Van Asschef696bf62018-07-18 09:25:14 -07004299 const struct ib_send_wr *wr, unsigned *idx,
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004300 int *size, int nreq, bool send_signaled, bool solicited)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004301{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004302 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4303 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004304
4305 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
4306 *seg = mlx5_get_send_wqe(qp, *idx);
4307 *ctrl = *seg;
4308 *(uint32_t *)(*seg + 8) = 0;
4309 (*ctrl)->imm = send_ieth(wr);
4310 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004311 (send_signaled ? MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4312 (solicited ? MLX5_WQE_CTRL_SOLICITED : 0);
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004313
4314 *seg += sizeof(**ctrl);
4315 *size = sizeof(**ctrl) / 16;
4316
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004317 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004318}
4319
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004320static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4321 struct mlx5_wqe_ctrl_seg **ctrl,
4322 const struct ib_send_wr *wr, unsigned *idx,
4323 int *size, int nreq)
4324{
4325 return __begin_wqe(qp, seg, ctrl, wr, idx, size, nreq,
4326 wr->send_flags & IB_SEND_SIGNALED,
4327 wr->send_flags & IB_SEND_SOLICITED);
4328}
4329
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004330static void finish_wqe(struct mlx5_ib_qp *qp,
4331 struct mlx5_wqe_ctrl_seg *ctrl,
4332 u8 size, unsigned idx, u64 wr_id,
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004333 int nreq, u8 fence, u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004334{
4335 u8 opmod = 0;
4336
4337 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
4338 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02004339 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004340 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004341 if (unlikely(qp->wq_sig))
4342 ctrl->signature = wq_sig(ctrl);
4343
4344 qp->sq.wrid[idx] = wr_id;
4345 qp->sq.w_list[idx].opcode = mlx5_opcode;
4346 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
4347 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
4348 qp->sq.w_list[idx].next = qp->sq.cur_post;
4349}
4350
Yishai Hadasd0e84c02018-06-19 10:43:55 +03004351static int _mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
4352 struct ib_send_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03004353{
4354 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
4355 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004356 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02004357 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02004358 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004359 struct mlx5_wqe_data_seg *dpseg;
4360 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02004361 struct mlx5_bf *bf;
Eli Cohene126ba92013-07-07 17:25:49 +03004362 int uninitialized_var(size);
Haggai Erand16e91d2016-02-29 15:45:05 +02004363 void *qend;
Eli Cohene126ba92013-07-07 17:25:49 +03004364 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03004365 unsigned idx;
4366 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004367 int num_sge;
4368 void *seg;
4369 int nreq;
4370 int i;
4371 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004372 u8 fence;
4373
Haggai Erand16e91d2016-02-29 15:45:05 +02004374 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4375 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
4376
4377 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004378 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02004379 qend = qp->sq.qend;
4380
Eli Cohene126ba92013-07-07 17:25:49 +03004381 spin_lock_irqsave(&qp->sq.lock, flags);
4382
Yishai Hadasd0e84c02018-06-19 10:43:55 +03004383 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR && !drain) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004384 err = -EIO;
4385 *bad_wr = wr;
4386 nreq = 0;
4387 goto out;
4388 }
4389
Eli Cohene126ba92013-07-07 17:25:49 +03004390 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04004391 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03004392 mlx5_ib_warn(dev, "\n");
4393 err = -EINVAL;
4394 *bad_wr = wr;
4395 goto out;
4396 }
4397
Eli Cohene126ba92013-07-07 17:25:49 +03004398 num_sge = wr->num_sge;
4399 if (unlikely(num_sge > qp->sq.max_gs)) {
4400 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03004401 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03004402 *bad_wr = wr;
4403 goto out;
4404 }
4405
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004406 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
4407 if (err) {
4408 mlx5_ib_warn(dev, "\n");
4409 err = -ENOMEM;
4410 *bad_wr = wr;
4411 goto out;
4412 }
Eli Cohene126ba92013-07-07 17:25:49 +03004413
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004414 if (wr->opcode == IB_WR_LOCAL_INV ||
4415 wr->opcode == IB_WR_REG_MR) {
4416 fence = dev->umr_fence;
4417 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
4418 } else if (wr->send_flags & IB_SEND_FENCE) {
4419 if (qp->next_fence)
4420 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
4421 else
4422 fence = MLX5_FENCE_MODE_FENCE;
4423 } else {
4424 fence = qp->next_fence;
4425 }
4426
Eli Cohene126ba92013-07-07 17:25:49 +03004427 switch (ibqp->qp_type) {
4428 case IB_QPT_XRC_INI:
4429 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03004430 seg += sizeof(*xrc);
4431 size += sizeof(*xrc) / 16;
4432 /* fall through */
4433 case IB_QPT_RC:
4434 switch (wr->opcode) {
4435 case IB_WR_RDMA_READ:
4436 case IB_WR_RDMA_WRITE:
4437 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004438 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4439 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004440 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004441 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4442 break;
4443
4444 case IB_WR_ATOMIC_CMP_AND_SWP:
4445 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03004446 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03004447 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
4448 err = -ENOSYS;
4449 *bad_wr = wr;
4450 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004451
4452 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03004453 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
4454 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004455 set_linv_wr(qp, &seg, &size);
Eli Cohene126ba92013-07-07 17:25:49 +03004456 num_sge = 0;
4457 break;
4458
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004459 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004460 qp->sq.wr_data[idx] = IB_WR_REG_MR;
4461 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
4462 err = set_reg_wr(qp, reg_wr(wr), &seg, &size);
4463 if (err) {
4464 *bad_wr = wr;
4465 goto out;
4466 }
4467 num_sge = 0;
4468 break;
4469
Sagi Grimberge6631812014-02-23 14:19:11 +02004470 case IB_WR_REG_SIG_MR:
4471 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004472 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004473
4474 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
4475 err = set_sig_umr_wr(wr, qp, &seg, &size);
4476 if (err) {
4477 mlx5_ib_warn(dev, "\n");
4478 *bad_wr = wr;
4479 goto out;
4480 }
4481
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004482 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4483 fence, MLX5_OPCODE_UMR);
Sagi Grimberge6631812014-02-23 14:19:11 +02004484 /*
4485 * SET_PSV WQEs are not signaled and solicited
4486 * on error
4487 */
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004488 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
4489 &size, nreq, false, true);
Sagi Grimberge6631812014-02-23 14:19:11 +02004490 if (err) {
4491 mlx5_ib_warn(dev, "\n");
4492 err = -ENOMEM;
4493 *bad_wr = wr;
4494 goto out;
4495 }
4496
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004497 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02004498 mr->sig->psv_memory.psv_idx, &seg,
4499 &size);
4500 if (err) {
4501 mlx5_ib_warn(dev, "\n");
4502 *bad_wr = wr;
4503 goto out;
4504 }
4505
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004506 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4507 fence, MLX5_OPCODE_SET_PSV);
Bart Van Assche7bb1faf2018-07-18 09:25:15 -07004508 err = __begin_wqe(qp, &seg, &ctrl, wr, &idx,
4509 &size, nreq, false, true);
Sagi Grimberge6631812014-02-23 14:19:11 +02004510 if (err) {
4511 mlx5_ib_warn(dev, "\n");
4512 err = -ENOMEM;
4513 *bad_wr = wr;
4514 goto out;
4515 }
4516
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004517 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02004518 mr->sig->psv_wire.psv_idx, &seg,
4519 &size);
4520 if (err) {
4521 mlx5_ib_warn(dev, "\n");
4522 *bad_wr = wr;
4523 goto out;
4524 }
4525
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004526 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4527 fence, MLX5_OPCODE_SET_PSV);
4528 qp->next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004529 num_sge = 0;
4530 goto skip_psv;
4531
Eli Cohene126ba92013-07-07 17:25:49 +03004532 default:
4533 break;
4534 }
4535 break;
4536
4537 case IB_QPT_UC:
4538 switch (wr->opcode) {
4539 case IB_WR_RDMA_WRITE:
4540 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004541 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4542 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004543 seg += sizeof(struct mlx5_wqe_raddr_seg);
4544 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4545 break;
4546
4547 default:
4548 break;
4549 }
4550 break;
4551
Eli Cohene126ba92013-07-07 17:25:49 +03004552 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02004553 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
4554 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
4555 err = -EPERM;
4556 *bad_wr = wr;
4557 goto out;
4558 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07004559 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02004560 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004561 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004562 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004563 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4564 if (unlikely((seg == qend)))
4565 seg = mlx5_get_send_wqe(qp, 0);
4566 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004567 case IB_QPT_UD:
4568 set_datagram_seg(seg, wr);
4569 seg += sizeof(struct mlx5_wqe_datagram_seg);
4570 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004571
Erez Shitritf0313962016-02-21 16:27:17 +02004572 if (unlikely((seg == qend)))
4573 seg = mlx5_get_send_wqe(qp, 0);
4574
4575 /* handle qp that supports ud offload */
4576 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4577 struct mlx5_wqe_eth_pad *pad;
4578
4579 pad = seg;
4580 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4581 seg += sizeof(struct mlx5_wqe_eth_pad);
4582 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4583
4584 seg = set_eth_seg(seg, wr, qend, qp, &size);
4585
4586 if (unlikely((seg == qend)))
4587 seg = mlx5_get_send_wqe(qp, 0);
4588 }
4589 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004590 case MLX5_IB_QPT_REG_UMR:
4591 if (wr->opcode != MLX5_IB_WR_UMR) {
4592 err = -EINVAL;
4593 mlx5_ib_warn(dev, "bad opcode\n");
4594 goto out;
4595 }
4596 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004597 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Majd Dibbinyc8d75a92018-03-22 15:34:04 +02004598 err = set_reg_umr_segment(dev, seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
4599 if (unlikely(err))
4600 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004601 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4602 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4603 if (unlikely((seg == qend)))
4604 seg = mlx5_get_send_wqe(qp, 0);
4605 set_reg_mkey_segment(seg, wr);
4606 seg += sizeof(struct mlx5_mkey_seg);
4607 size += sizeof(struct mlx5_mkey_seg) / 16;
4608 if (unlikely((seg == qend)))
4609 seg = mlx5_get_send_wqe(qp, 0);
4610 break;
4611
4612 default:
4613 break;
4614 }
4615
4616 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4617 int uninitialized_var(sz);
4618
4619 err = set_data_inl_seg(qp, wr, seg, &sz);
4620 if (unlikely(err)) {
4621 mlx5_ib_warn(dev, "\n");
4622 *bad_wr = wr;
4623 goto out;
4624 }
Eli Cohene126ba92013-07-07 17:25:49 +03004625 size += sz;
4626 } else {
4627 dpseg = seg;
4628 for (i = 0; i < num_sge; i++) {
4629 if (unlikely(dpseg == qend)) {
4630 seg = mlx5_get_send_wqe(qp, 0);
4631 dpseg = seg;
4632 }
4633 if (likely(wr->sg_list[i].length)) {
4634 set_data_ptr_seg(dpseg, wr->sg_list + i);
4635 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4636 dpseg++;
4637 }
4638 }
4639 }
4640
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004641 qp->next_fence = next_fence;
4642 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq, fence,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004643 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004644skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004645 if (0)
4646 dump_wqe(qp, idx, size);
4647 }
4648
4649out:
4650 if (likely(nreq)) {
4651 qp->sq.head += nreq;
4652
4653 /* Make sure that descriptors are written before
4654 * updating doorbell record and ringing the doorbell
4655 */
4656 wmb();
4657
4658 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4659
Eli Cohenada388f2014-01-14 17:45:16 +02004660 /* Make sure doorbell record is visible to the HCA before
4661 * we hit doorbell */
4662 wmb();
4663
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004664 /* currently we support only regular doorbells */
4665 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset, NULL);
4666 /* Make sure doorbells don't leak out of SQ spinlock
4667 * and reach the HCA out of order.
4668 */
4669 mmiowb();
Eli Cohene126ba92013-07-07 17:25:49 +03004670 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03004671 }
4672
4673 spin_unlock_irqrestore(&qp->sq.lock, flags);
4674
4675 return err;
4676}
4677
Yishai Hadasd0e84c02018-06-19 10:43:55 +03004678int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
4679 struct ib_send_wr **bad_wr)
4680{
4681 return _mlx5_ib_post_send(ibqp, wr, bad_wr, false);
4682}
4683
Eli Cohene126ba92013-07-07 17:25:49 +03004684static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4685{
4686 sig->signature = calc_sig(sig, size);
4687}
4688
Yishai Hadasd0e84c02018-06-19 10:43:55 +03004689static int _mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4690 struct ib_recv_wr **bad_wr, bool drain)
Eli Cohene126ba92013-07-07 17:25:49 +03004691{
4692 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4693 struct mlx5_wqe_data_seg *scat;
4694 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004695 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4696 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004697 unsigned long flags;
4698 int err = 0;
4699 int nreq;
4700 int ind;
4701 int i;
4702
Haggai Erand16e91d2016-02-29 15:45:05 +02004703 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4704 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4705
Eli Cohene126ba92013-07-07 17:25:49 +03004706 spin_lock_irqsave(&qp->rq.lock, flags);
4707
Yishai Hadasd0e84c02018-06-19 10:43:55 +03004708 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR && !drain) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004709 err = -EIO;
4710 *bad_wr = wr;
4711 nreq = 0;
4712 goto out;
4713 }
4714
Eli Cohene126ba92013-07-07 17:25:49 +03004715 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4716
4717 for (nreq = 0; wr; nreq++, wr = wr->next) {
4718 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4719 err = -ENOMEM;
4720 *bad_wr = wr;
4721 goto out;
4722 }
4723
4724 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4725 err = -EINVAL;
4726 *bad_wr = wr;
4727 goto out;
4728 }
4729
4730 scat = get_recv_wqe(qp, ind);
4731 if (qp->wq_sig)
4732 scat++;
4733
4734 for (i = 0; i < wr->num_sge; i++)
4735 set_data_ptr_seg(scat + i, wr->sg_list + i);
4736
4737 if (i < qp->rq.max_gs) {
4738 scat[i].byte_count = 0;
4739 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4740 scat[i].addr = 0;
4741 }
4742
4743 if (qp->wq_sig) {
4744 sig = (struct mlx5_rwqe_sig *)scat;
4745 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4746 }
4747
4748 qp->rq.wrid[ind] = wr->wr_id;
4749
4750 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4751 }
4752
4753out:
4754 if (likely(nreq)) {
4755 qp->rq.head += nreq;
4756
4757 /* Make sure that descriptors are written before
4758 * doorbell record.
4759 */
4760 wmb();
4761
4762 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4763 }
4764
4765 spin_unlock_irqrestore(&qp->rq.lock, flags);
4766
4767 return err;
4768}
4769
Yishai Hadasd0e84c02018-06-19 10:43:55 +03004770int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4771 struct ib_recv_wr **bad_wr)
4772{
4773 return _mlx5_ib_post_recv(ibqp, wr, bad_wr, false);
4774}
4775
Eli Cohene126ba92013-07-07 17:25:49 +03004776static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4777{
4778 switch (mlx5_state) {
4779 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4780 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4781 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4782 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4783 case MLX5_QP_STATE_SQ_DRAINING:
4784 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4785 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4786 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4787 default: return -1;
4788 }
4789}
4790
4791static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4792{
4793 switch (mlx5_mig_state) {
4794 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4795 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4796 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4797 default: return -1;
4798 }
4799}
4800
4801static int to_ib_qp_access_flags(int mlx5_flags)
4802{
4803 int ib_flags = 0;
4804
4805 if (mlx5_flags & MLX5_QP_BIT_RRE)
4806 ib_flags |= IB_ACCESS_REMOTE_READ;
4807 if (mlx5_flags & MLX5_QP_BIT_RWE)
4808 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4809 if (mlx5_flags & MLX5_QP_BIT_RAE)
4810 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4811
4812 return ib_flags;
4813}
4814
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004815static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004816 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004817 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03004818{
Eli Cohene126ba92013-07-07 17:25:49 +03004819
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004820 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03004821
Jason Gunthorpee7996a92018-01-29 13:26:40 -07004822 if (!path->port || path->port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03004823 return;
4824
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02004825 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
4826
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004827 rdma_ah_set_port_num(ah_attr, path->port);
4828 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03004829
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004830 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
4831 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
4832 rdma_ah_set_static_rate(ah_attr,
4833 path->static_rate ? path->static_rate - 5 : 0);
4834 if (path->grh_mlid & (1 << 7)) {
4835 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
4836
4837 rdma_ah_set_grh(ah_attr, NULL,
4838 tc_fl & 0xfffff,
4839 path->mgid_index,
4840 path->hop_limit,
4841 (tc_fl >> 20) & 0xff);
4842 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03004843 }
4844}
4845
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004846static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4847 struct mlx5_ib_sq *sq,
4848 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004849{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004850 int err;
4851
Eran Ben Elisha28160772017-12-26 15:17:05 +02004852 err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004853 if (err)
4854 goto out;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004855 sq->state = *sq_state;
4856
4857out:
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004858 return err;
4859}
4860
4861static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
4862 struct mlx5_ib_rq *rq,
4863 u8 *rq_state)
4864{
4865 void *out;
4866 void *rqc;
4867 int inlen;
4868 int err;
4869
4870 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004871 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004872 if (!out)
4873 return -ENOMEM;
4874
4875 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
4876 if (err)
4877 goto out;
4878
4879 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
4880 *rq_state = MLX5_GET(rqc, rqc, state);
4881 rq->state = *rq_state;
4882
4883out:
4884 kvfree(out);
4885 return err;
4886}
4887
4888static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
4889 struct mlx5_ib_qp *qp, u8 *qp_state)
4890{
4891 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
4892 [MLX5_RQC_STATE_RST] = {
4893 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4894 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4895 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
4896 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
4897 },
4898 [MLX5_RQC_STATE_RDY] = {
4899 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4900 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4901 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
4902 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
4903 },
4904 [MLX5_RQC_STATE_ERR] = {
4905 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4906 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4907 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
4908 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
4909 },
4910 [MLX5_RQ_STATE_NA] = {
4911 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4912 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4913 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
4914 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
4915 },
4916 };
4917
4918 *qp_state = sqrq_trans[rq_state][sq_state];
4919
4920 if (*qp_state == MLX5_QP_STATE_BAD) {
4921 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4922 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
4923 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
4924 return -EINVAL;
4925 }
4926
4927 if (*qp_state == MLX5_QP_STATE)
4928 *qp_state = qp->state;
4929
4930 return 0;
4931}
4932
4933static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
4934 struct mlx5_ib_qp *qp,
4935 u8 *raw_packet_qp_state)
4936{
4937 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
4938 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
4939 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
4940 int err;
4941 u8 sq_state = MLX5_SQ_STATE_NA;
4942 u8 rq_state = MLX5_RQ_STATE_NA;
4943
4944 if (qp->sq.wqe_cnt) {
4945 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
4946 if (err)
4947 return err;
4948 }
4949
4950 if (qp->rq.wqe_cnt) {
4951 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
4952 if (err)
4953 return err;
4954 }
4955
4956 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
4957 raw_packet_qp_state);
4958}
4959
4960static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
4961 struct ib_qp_attr *qp_attr)
4962{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004963 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03004964 struct mlx5_qp_context *context;
4965 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004966 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03004967 int err = 0;
4968
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004969 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004970 if (!outb)
4971 return -ENOMEM;
4972
majd@mellanox.com19098df2016-01-14 19:13:03 +02004973 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004974 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03004975 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004976 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004977
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004978 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
4979 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
4980
Eli Cohene126ba92013-07-07 17:25:49 +03004981 mlx5_state = be32_to_cpu(context->flags) >> 28;
4982
4983 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03004984 qp_attr->path_mtu = context->mtu_msgmax >> 5;
4985 qp_attr->path_mig_state =
4986 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
4987 qp_attr->qkey = be32_to_cpu(context->qkey);
4988 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
4989 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
4990 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
4991 qp_attr->qp_access_flags =
4992 to_ib_qp_access_flags(be32_to_cpu(context->params2));
4993
4994 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004995 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
4996 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004997 qp_attr->alt_pkey_index =
4998 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004999 qp_attr->alt_port_num =
5000 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03005001 }
5002
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03005003 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03005004 qp_attr->port_num = context->pri_path.port;
5005
5006 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
5007 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
5008
5009 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
5010
5011 qp_attr->max_dest_rd_atomic =
5012 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
5013 qp_attr->min_rnr_timer =
5014 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
5015 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
5016 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
5017 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
5018 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005019
5020out:
5021 kfree(outb);
5022 return err;
5023}
5024
Moni Shoua776a3902018-01-02 16:19:33 +02005025static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
5026 struct ib_qp_attr *qp_attr, int qp_attr_mask,
5027 struct ib_qp_init_attr *qp_init_attr)
5028{
5029 struct mlx5_core_dct *dct = &mqp->dct.mdct;
5030 u32 *out;
5031 u32 access_flags = 0;
5032 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
5033 void *dctc;
5034 int err;
5035 int supported_mask = IB_QP_STATE |
5036 IB_QP_ACCESS_FLAGS |
5037 IB_QP_PORT |
5038 IB_QP_MIN_RNR_TIMER |
5039 IB_QP_AV |
5040 IB_QP_PATH_MTU |
5041 IB_QP_PKEY_INDEX;
5042
5043 if (qp_attr_mask & ~supported_mask)
5044 return -EINVAL;
5045 if (mqp->state != IB_QPS_RTR)
5046 return -EINVAL;
5047
5048 out = kzalloc(outlen, GFP_KERNEL);
5049 if (!out)
5050 return -ENOMEM;
5051
5052 err = mlx5_core_dct_query(dev->mdev, dct, out, outlen);
5053 if (err)
5054 goto out;
5055
5056 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
5057
5058 if (qp_attr_mask & IB_QP_STATE)
5059 qp_attr->qp_state = IB_QPS_RTR;
5060
5061 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
5062 if (MLX5_GET(dctc, dctc, rre))
5063 access_flags |= IB_ACCESS_REMOTE_READ;
5064 if (MLX5_GET(dctc, dctc, rwe))
5065 access_flags |= IB_ACCESS_REMOTE_WRITE;
5066 if (MLX5_GET(dctc, dctc, rae))
5067 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
5068 qp_attr->qp_access_flags = access_flags;
5069 }
5070
5071 if (qp_attr_mask & IB_QP_PORT)
5072 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
5073 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
5074 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
5075 if (qp_attr_mask & IB_QP_AV) {
5076 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
5077 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
5078 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
5079 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
5080 }
5081 if (qp_attr_mask & IB_QP_PATH_MTU)
5082 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
5083 if (qp_attr_mask & IB_QP_PKEY_INDEX)
5084 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
5085out:
5086 kfree(out);
5087 return err;
5088}
5089
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005090int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
5091 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
5092{
5093 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
5094 struct mlx5_ib_qp *qp = to_mqp(ibqp);
5095 int err = 0;
5096 u8 raw_packet_qp_state;
5097
Yishai Hadas28d61372016-05-23 15:20:56 +03005098 if (ibqp->rwq_ind_tbl)
5099 return -ENOSYS;
5100
Haggai Erand16e91d2016-02-29 15:45:05 +02005101 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
5102 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
5103 qp_init_attr);
5104
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005105 /* Not all of output fields are applicable, make sure to zero them */
5106 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
5107 memset(qp_attr, 0, sizeof(*qp_attr));
5108
Moni Shoua776a3902018-01-02 16:19:33 +02005109 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
5110 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
5111 qp_attr_mask, qp_init_attr);
5112
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005113 mutex_lock(&qp->mutex);
5114
Yishai Hadasc2e53b22017-06-08 16:15:08 +03005115 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
5116 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005117 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
5118 if (err)
5119 goto out;
5120 qp->state = raw_packet_qp_state;
5121 qp_attr->port_num = 1;
5122 } else {
5123 err = query_qp_attr(dev, qp, qp_attr);
5124 if (err)
5125 goto out;
5126 }
5127
5128 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03005129 qp_attr->cur_qp_state = qp_attr->qp_state;
5130 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
5131 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
5132
5133 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005134 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005135 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005136 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005137 } else {
5138 qp_attr->cap.max_send_wr = 0;
5139 qp_attr->cap.max_send_sge = 0;
5140 }
5141
Noa Osherovich0540d812016-06-04 15:15:32 +03005142 qp_init_attr->qp_type = ibqp->qp_type;
5143 qp_init_attr->recv_cq = ibqp->recv_cq;
5144 qp_init_attr->send_cq = ibqp->send_cq;
5145 qp_init_attr->srq = ibqp->srq;
5146 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005147
5148 qp_init_attr->cap = qp_attr->cap;
5149
5150 qp_init_attr->create_flags = 0;
5151 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
5152 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
5153
Leon Romanovsky051f2632015-12-20 12:16:11 +02005154 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
5155 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
5156 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
5157 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
5158 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
5159 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02005160 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
5161 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02005162
Eli Cohene126ba92013-07-07 17:25:49 +03005163 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5164 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5165
Eli Cohene126ba92013-07-07 17:25:49 +03005166out:
5167 mutex_unlock(&qp->mutex);
5168 return err;
5169}
5170
5171struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
5172 struct ib_ucontext *context,
5173 struct ib_udata *udata)
5174{
5175 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5176 struct mlx5_ib_xrcd *xrcd;
5177 int err;
5178
Saeed Mahameed938fe832015-05-28 22:28:41 +03005179 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005180 return ERR_PTR(-ENOSYS);
5181
5182 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5183 if (!xrcd)
5184 return ERR_PTR(-ENOMEM);
5185
Jack Morgenstein9603b612014-07-28 23:30:22 +03005186 err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005187 if (err) {
5188 kfree(xrcd);
5189 return ERR_PTR(-ENOMEM);
5190 }
5191
5192 return &xrcd->ibxrcd;
5193}
5194
5195int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
5196{
5197 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5198 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
5199 int err;
5200
Jack Morgenstein9603b612014-07-28 23:30:22 +03005201 err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
Leon Romanovskyb0818082018-01-28 11:25:30 +02005202 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03005203 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005204
5205 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03005206 return 0;
5207}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005208
Yishai Hadas350d0e42016-08-28 14:58:18 +03005209static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5210{
5211 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5212 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5213 struct ib_event event;
5214
5215 if (rwq->ibwq.event_handler) {
5216 event.device = rwq->ibwq.device;
5217 event.element.wq = &rwq->ibwq;
5218 switch (type) {
5219 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5220 event.event = IB_EVENT_WQ_FATAL;
5221 break;
5222 default:
5223 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5224 return;
5225 }
5226
5227 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5228 }
5229}
5230
Maor Gottlieb03404e82017-05-30 10:29:13 +03005231static int set_delay_drop(struct mlx5_ib_dev *dev)
5232{
5233 int err = 0;
5234
5235 mutex_lock(&dev->delay_drop.lock);
5236 if (dev->delay_drop.activate)
5237 goto out;
5238
5239 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
5240 if (err)
5241 goto out;
5242
5243 dev->delay_drop.activate = true;
5244out:
5245 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005246
5247 if (!err)
5248 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005249 return err;
5250}
5251
Yishai Hadas79b20a62016-05-23 15:20:50 +03005252static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5253 struct ib_wq_init_attr *init_attr)
5254{
5255 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005256 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005257 __be64 *rq_pas0;
5258 void *in;
5259 void *rqc;
5260 void *wq;
5261 int inlen;
5262 int err;
5263
5264 dev = to_mdev(pd->device);
5265
5266 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005267 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005268 if (!in)
5269 return -ENOMEM;
5270
5271 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
5272 MLX5_SET(rqc, rqc, mem_rq_type,
5273 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
5274 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
5275 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
5276 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
5277 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
5278 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03005279 MLX5_SET(wq, wq, wq_type,
5280 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
5281 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005282 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5283 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
5284 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
5285 err = -EOPNOTSUPP;
5286 goto out;
5287 } else {
5288 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
5289 }
5290 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005291 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03005292 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
5293 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
5294 MLX5_SET(wq, wq, log_wqe_stride_size,
5295 rwq->single_stride_log_num_of_bytes -
5296 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
5297 MLX5_SET(wq, wq, log_wqe_num_of_strides, rwq->log_num_strides -
5298 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES);
5299 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005300 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
5301 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
5302 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
5303 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
5304 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
5305 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02005306 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005307 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02005308 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005309 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
5310 err = -EOPNOTSUPP;
5311 goto out;
5312 }
5313 } else {
5314 MLX5_SET(rqc, rqc, vsd, 1);
5315 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02005316 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
5317 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
5318 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
5319 err = -EOPNOTSUPP;
5320 goto out;
5321 }
5322 MLX5_SET(rqc, rqc, scatter_fcs, 1);
5323 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03005324 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5325 if (!(dev->ib_dev.attrs.raw_packet_caps &
5326 IB_RAW_PACKET_CAP_DELAY_DROP)) {
5327 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
5328 err = -EOPNOTSUPP;
5329 goto out;
5330 }
5331 MLX5_SET(rqc, rqc, delay_drop_en, 1);
5332 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005333 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
5334 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03005335 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005336 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5337 err = set_delay_drop(dev);
5338 if (err) {
5339 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
5340 err);
5341 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
5342 } else {
5343 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
5344 }
5345 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005346out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03005347 kvfree(in);
5348 return err;
5349}
5350
5351static int set_user_rq_size(struct mlx5_ib_dev *dev,
5352 struct ib_wq_init_attr *wq_init_attr,
5353 struct mlx5_ib_create_wq *ucmd,
5354 struct mlx5_ib_rwq *rwq)
5355{
5356 /* Sanity check RQ size before proceeding */
5357 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
5358 return -EINVAL;
5359
5360 if (!ucmd->rq_wqe_count)
5361 return -EINVAL;
5362
5363 rwq->wqe_count = ucmd->rq_wqe_count;
5364 rwq->wqe_shift = ucmd->rq_wqe_shift;
5365 rwq->buf_size = (rwq->wqe_count << rwq->wqe_shift);
5366 rwq->log_rq_stride = rwq->wqe_shift;
5367 rwq->log_rq_size = ilog2(rwq->wqe_count);
5368 return 0;
5369}
5370
5371static int prepare_user_rq(struct ib_pd *pd,
5372 struct ib_wq_init_attr *init_attr,
5373 struct ib_udata *udata,
5374 struct mlx5_ib_rwq *rwq)
5375{
5376 struct mlx5_ib_dev *dev = to_mdev(pd->device);
5377 struct mlx5_ib_create_wq ucmd = {};
5378 int err;
5379 size_t required_cmd_sz;
5380
Noa Osherovichccc87082017-10-17 18:01:13 +03005381 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
5382 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005383 if (udata->inlen < required_cmd_sz) {
5384 mlx5_ib_dbg(dev, "invalid inlen\n");
5385 return -EINVAL;
5386 }
5387
5388 if (udata->inlen > sizeof(ucmd) &&
5389 !ib_is_udata_cleared(udata, sizeof(ucmd),
5390 udata->inlen - sizeof(ucmd))) {
5391 mlx5_ib_dbg(dev, "inlen is not supported\n");
5392 return -EOPNOTSUPP;
5393 }
5394
5395 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
5396 mlx5_ib_dbg(dev, "copy failed\n");
5397 return -EFAULT;
5398 }
5399
Noa Osherovichccc87082017-10-17 18:01:13 +03005400 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03005401 mlx5_ib_dbg(dev, "invalid comp mask\n");
5402 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03005403 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
5404 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
5405 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
5406 return -EOPNOTSUPP;
5407 }
5408 if ((ucmd.single_stride_log_num_of_bytes <
5409 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
5410 (ucmd.single_stride_log_num_of_bytes >
5411 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
5412 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
5413 ucmd.single_stride_log_num_of_bytes,
5414 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
5415 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
5416 return -EINVAL;
5417 }
5418 if ((ucmd.single_wqe_log_num_of_strides >
5419 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
5420 (ucmd.single_wqe_log_num_of_strides <
5421 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES)) {
5422 mlx5_ib_dbg(dev, "Invalid log num strides (%u. Range is %u - %u)\n",
5423 ucmd.single_wqe_log_num_of_strides,
5424 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
5425 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
5426 return -EINVAL;
5427 }
5428 rwq->single_stride_log_num_of_bytes =
5429 ucmd.single_stride_log_num_of_bytes;
5430 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
5431 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
5432 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005433 }
5434
5435 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
5436 if (err) {
5437 mlx5_ib_dbg(dev, "err %d\n", err);
5438 return err;
5439 }
5440
5441 err = create_user_rq(dev, pd, rwq, &ucmd);
5442 if (err) {
5443 mlx5_ib_dbg(dev, "err %d\n", err);
5444 if (err)
5445 return err;
5446 }
5447
5448 rwq->user_index = ucmd.user_index;
5449 return 0;
5450}
5451
5452struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
5453 struct ib_wq_init_attr *init_attr,
5454 struct ib_udata *udata)
5455{
5456 struct mlx5_ib_dev *dev;
5457 struct mlx5_ib_rwq *rwq;
5458 struct mlx5_ib_create_wq_resp resp = {};
5459 size_t min_resp_len;
5460 int err;
5461
5462 if (!udata)
5463 return ERR_PTR(-ENOSYS);
5464
5465 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5466 if (udata->outlen && udata->outlen < min_resp_len)
5467 return ERR_PTR(-EINVAL);
5468
5469 dev = to_mdev(pd->device);
5470 switch (init_attr->wq_type) {
5471 case IB_WQT_RQ:
5472 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
5473 if (!rwq)
5474 return ERR_PTR(-ENOMEM);
5475 err = prepare_user_rq(pd, init_attr, udata, rwq);
5476 if (err)
5477 goto err;
5478 err = create_rq(rwq, pd, init_attr);
5479 if (err)
5480 goto err_user_rq;
5481 break;
5482 default:
5483 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
5484 init_attr->wq_type);
5485 return ERR_PTR(-EINVAL);
5486 }
5487
Yishai Hadas350d0e42016-08-28 14:58:18 +03005488 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005489 rwq->ibwq.state = IB_WQS_RESET;
5490 if (udata->outlen) {
5491 resp.response_length = offsetof(typeof(resp), response_length) +
5492 sizeof(resp.response_length);
5493 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5494 if (err)
5495 goto err_copy;
5496 }
5497
Yishai Hadas350d0e42016-08-28 14:58:18 +03005498 rwq->core_qp.event = mlx5_ib_wq_event;
5499 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005500 return &rwq->ibwq;
5501
5502err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03005503 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005504err_user_rq:
Maor Gottliebfe248c32017-05-30 10:29:14 +03005505 destroy_user_rq(dev, pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005506err:
5507 kfree(rwq);
5508 return ERR_PTR(err);
5509}
5510
5511int mlx5_ib_destroy_wq(struct ib_wq *wq)
5512{
5513 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5514 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5515
Yishai Hadas350d0e42016-08-28 14:58:18 +03005516 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005517 destroy_user_rq(dev, wq->pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005518 kfree(rwq);
5519
5520 return 0;
5521}
5522
Yishai Hadasc5f90922016-05-23 15:20:53 +03005523struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
5524 struct ib_rwq_ind_table_init_attr *init_attr,
5525 struct ib_udata *udata)
5526{
5527 struct mlx5_ib_dev *dev = to_mdev(device);
5528 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
5529 int sz = 1 << init_attr->log_ind_tbl_size;
5530 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
5531 size_t min_resp_len;
5532 int inlen;
5533 int err;
5534 int i;
5535 u32 *in;
5536 void *rqtc;
5537
5538 if (udata->inlen > 0 &&
5539 !ib_is_udata_cleared(udata, 0,
5540 udata->inlen))
5541 return ERR_PTR(-EOPNOTSUPP);
5542
Maor Gottliebefd7f402016-10-27 16:36:40 +03005543 if (init_attr->log_ind_tbl_size >
5544 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
5545 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
5546 init_attr->log_ind_tbl_size,
5547 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
5548 return ERR_PTR(-EINVAL);
5549 }
5550
Yishai Hadasc5f90922016-05-23 15:20:53 +03005551 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5552 if (udata->outlen && udata->outlen < min_resp_len)
5553 return ERR_PTR(-EINVAL);
5554
5555 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
5556 if (!rwq_ind_tbl)
5557 return ERR_PTR(-ENOMEM);
5558
5559 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005560 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005561 if (!in) {
5562 err = -ENOMEM;
5563 goto err;
5564 }
5565
5566 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
5567
5568 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
5569 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
5570
5571 for (i = 0; i < sz; i++)
5572 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
5573
5574 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
5575 kvfree(in);
5576
5577 if (err)
5578 goto err;
5579
5580 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
5581 if (udata->outlen) {
5582 resp.response_length = offsetof(typeof(resp), response_length) +
5583 sizeof(resp.response_length);
5584 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5585 if (err)
5586 goto err_copy;
5587 }
5588
5589 return &rwq_ind_tbl->ib_rwq_ind_tbl;
5590
5591err_copy:
5592 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
5593err:
5594 kfree(rwq_ind_tbl);
5595 return ERR_PTR(err);
5596}
5597
5598int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
5599{
5600 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
5601 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
5602
5603 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
5604
5605 kfree(rwq_ind_tbl);
5606 return 0;
5607}
5608
Yishai Hadas79b20a62016-05-23 15:20:50 +03005609int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
5610 u32 wq_attr_mask, struct ib_udata *udata)
5611{
5612 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5613 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5614 struct mlx5_ib_modify_wq ucmd = {};
5615 size_t required_cmd_sz;
5616 int curr_wq_state;
5617 int wq_state;
5618 int inlen;
5619 int err;
5620 void *rqc;
5621 void *in;
5622
5623 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
5624 if (udata->inlen < required_cmd_sz)
5625 return -EINVAL;
5626
5627 if (udata->inlen > sizeof(ucmd) &&
5628 !ib_is_udata_cleared(udata, sizeof(ucmd),
5629 udata->inlen - sizeof(ucmd)))
5630 return -EOPNOTSUPP;
5631
5632 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
5633 return -EFAULT;
5634
5635 if (ucmd.comp_mask || ucmd.reserved)
5636 return -EOPNOTSUPP;
5637
5638 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005639 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005640 if (!in)
5641 return -ENOMEM;
5642
5643 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
5644
5645 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
5646 wq_attr->curr_wq_state : wq->state;
5647 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
5648 wq_attr->wq_state : curr_wq_state;
5649 if (curr_wq_state == IB_WQS_ERR)
5650 curr_wq_state = MLX5_RQC_STATE_ERR;
5651 if (wq_state == IB_WQS_ERR)
5652 wq_state = MLX5_RQC_STATE_ERR;
5653 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
5654 MLX5_SET(rqc, rqc, state, wq_state);
5655
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005656 if (wq_attr_mask & IB_WQ_FLAGS) {
5657 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
5658 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
5659 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
5660 mlx5_ib_dbg(dev, "VLAN offloads are not "
5661 "supported\n");
5662 err = -EOPNOTSUPP;
5663 goto out;
5664 }
5665 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5666 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
5667 MLX5_SET(rqc, rqc, vsd,
5668 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
5669 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005670
5671 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5672 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
5673 err = -EOPNOTSUPP;
5674 goto out;
5675 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005676 }
5677
Majd Dibbiny23a69642017-01-18 15:25:10 +02005678 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
5679 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
5680 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5681 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03005682 MLX5_SET(rqc, rqc, counter_set_id,
5683 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005684 } else
5685 pr_info_once("%s: Receive WQ counters are not supported on current FW\n",
5686 dev->ib_dev.name);
5687 }
5688
Yishai Hadas350d0e42016-08-28 14:58:18 +03005689 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005690 if (!err)
5691 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
5692
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005693out:
5694 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005695 return err;
5696}
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005697
5698struct mlx5_ib_drain_cqe {
5699 struct ib_cqe cqe;
5700 struct completion done;
5701};
5702
5703static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
5704{
5705 struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
5706 struct mlx5_ib_drain_cqe,
5707 cqe);
5708
5709 complete(&cqe->done);
5710}
5711
5712/* This function returns only once the drained WR was completed */
5713static void handle_drain_completion(struct ib_cq *cq,
5714 struct mlx5_ib_drain_cqe *sdrain,
5715 struct mlx5_ib_dev *dev)
5716{
5717 struct mlx5_core_dev *mdev = dev->mdev;
5718
5719 if (cq->poll_ctx == IB_POLL_DIRECT) {
5720 while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
5721 ib_process_cq_direct(cq, -1);
5722 return;
5723 }
5724
5725 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5726 struct mlx5_ib_cq *mcq = to_mcq(cq);
5727 bool triggered = false;
5728 unsigned long flags;
5729
5730 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
5731 /* Make sure that the CQ handler won't run if wasn't run yet */
5732 if (!mcq->mcq.reset_notify_added)
5733 mcq->mcq.reset_notify_added = 1;
5734 else
5735 triggered = true;
5736 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
5737
5738 if (triggered) {
5739 /* Wait for any scheduled/running task to be ended */
5740 switch (cq->poll_ctx) {
5741 case IB_POLL_SOFTIRQ:
5742 irq_poll_disable(&cq->iop);
5743 irq_poll_enable(&cq->iop);
5744 break;
5745 case IB_POLL_WORKQUEUE:
5746 cancel_work_sync(&cq->work);
5747 break;
5748 default:
5749 WARN_ON_ONCE(1);
5750 }
5751 }
5752
5753 /* Run the CQ handler - this makes sure that the drain WR will
5754 * be processed if wasn't processed yet.
5755 */
5756 mcq->mcq.comp(&mcq->mcq);
5757 }
5758
5759 wait_for_completion(&sdrain->done);
5760}
5761
5762void mlx5_ib_drain_sq(struct ib_qp *qp)
5763{
5764 struct ib_cq *cq = qp->send_cq;
5765 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
5766 struct mlx5_ib_drain_cqe sdrain;
5767 struct ib_send_wr *bad_swr;
5768 struct ib_rdma_wr swr = {
5769 .wr = {
5770 .next = NULL,
5771 { .wr_cqe = &sdrain.cqe, },
5772 .opcode = IB_WR_RDMA_WRITE,
5773 },
5774 };
5775 int ret;
5776 struct mlx5_ib_dev *dev = to_mdev(qp->device);
5777 struct mlx5_core_dev *mdev = dev->mdev;
5778
5779 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
5780 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5781 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
5782 return;
5783 }
5784
5785 sdrain.cqe.done = mlx5_ib_drain_qp_done;
5786 init_completion(&sdrain.done);
5787
5788 ret = _mlx5_ib_post_send(qp, &swr.wr, &bad_swr, true);
5789 if (ret) {
5790 WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
5791 return;
5792 }
5793
5794 handle_drain_completion(cq, &sdrain, dev);
5795}
5796
5797void mlx5_ib_drain_rq(struct ib_qp *qp)
5798{
5799 struct ib_cq *cq = qp->recv_cq;
5800 struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
5801 struct mlx5_ib_drain_cqe rdrain;
5802 struct ib_recv_wr rwr = {}, *bad_rwr;
5803 int ret;
5804 struct mlx5_ib_dev *dev = to_mdev(qp->device);
5805 struct mlx5_core_dev *mdev = dev->mdev;
5806
5807 ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
5808 if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
5809 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
5810 return;
5811 }
5812
5813 rwr.wr_cqe = &rdrain.cqe;
5814 rdrain.cqe.done = mlx5_ib_drain_qp_done;
5815 init_completion(&rdrain.done);
5816
5817 ret = _mlx5_ib_post_recv(qp, &rwr, &bad_rwr, true);
5818 if (ret) {
5819 WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
5820 return;
5821 }
5822
5823 handle_drain_completion(cq, &rdrain, dev);
5824}