blob: 8aed091036c6b59209b3cf1c526d49f24bc711d1 [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,
Eli Cohene126ba92013-07-07 17:25:49 +030057};
58
59static const u32 mlx5_ib_opcode[] = {
60 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020061 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030062 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
63 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
64 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
65 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
66 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
67 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
68 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
69 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030070 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030071 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
72 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
73 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
74};
75
Erez Shitritf0313962016-02-21 16:27:17 +020076struct mlx5_wqe_eth_pad {
77 u8 rsvd0[16];
78};
Eli Cohene126ba92013-07-07 17:25:49 +030079
Alex Veskereb49ab02016-08-28 12:25:53 +030080enum raw_qp_set_mask_map {
81 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020082 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030083};
84
Alex Vesker0680efa2016-08-28 12:25:52 +030085struct mlx5_modify_raw_qp_param {
86 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030087
88 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang7d29f342016-12-01 13:43:16 +020089 u32 rate_limit;
Alex Veskereb49ab02016-08-28 12:25:53 +030090 u8 rq_q_ctr_id;
Alex Vesker0680efa2016-08-28 12:25:52 +030091};
92
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030093static void get_cqs(enum ib_qp_type qp_type,
94 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
95 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
96
Eli Cohene126ba92013-07-07 17:25:49 +030097static int is_qp0(enum ib_qp_type qp_type)
98{
99 return qp_type == IB_QPT_SMI;
100}
101
Eli Cohene126ba92013-07-07 17:25:49 +0300102static int is_sqp(enum ib_qp_type qp_type)
103{
104 return is_qp0(qp_type) || is_qp1(qp_type);
105}
106
107static void *get_wqe(struct mlx5_ib_qp *qp, int offset)
108{
109 return mlx5_buf_offset(&qp->buf, offset);
110}
111
112static void *get_recv_wqe(struct mlx5_ib_qp *qp, int n)
113{
114 return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
115}
116
117void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n)
118{
119 return get_wqe(qp, qp->sq.offset + (n << MLX5_IB_SQ_STRIDE));
120}
121
Haggai Eranc1395a22014-12-11 17:04:14 +0200122/**
123 * mlx5_ib_read_user_wqe() - Copy a user-space WQE to kernel space.
124 *
125 * @qp: QP to copy from.
126 * @send: copy from the send queue when non-zero, use the receive queue
127 * otherwise.
128 * @wqe_index: index to start copying from. For send work queues, the
129 * wqe_index is in units of MLX5_SEND_WQE_BB.
130 * For receive work queue, it is the number of work queue
131 * element in the queue.
132 * @buffer: destination buffer.
133 * @length: maximum number of bytes to copy.
134 *
135 * Copies at least a single WQE, but may copy more data.
136 *
137 * Return: the number of bytes copied, or an error code.
138 */
139int mlx5_ib_read_user_wqe(struct mlx5_ib_qp *qp, int send, int wqe_index,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200140 void *buffer, u32 length,
141 struct mlx5_ib_qp_base *base)
Haggai Eranc1395a22014-12-11 17:04:14 +0200142{
143 struct ib_device *ibdev = qp->ibqp.device;
144 struct mlx5_ib_dev *dev = to_mdev(ibdev);
145 struct mlx5_ib_wq *wq = send ? &qp->sq : &qp->rq;
146 size_t offset;
147 size_t wq_end;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200148 struct ib_umem *umem = base->ubuffer.umem;
Haggai Eranc1395a22014-12-11 17:04:14 +0200149 u32 first_copy_length;
150 int wqe_length;
151 int ret;
152
153 if (wq->wqe_cnt == 0) {
154 mlx5_ib_dbg(dev, "mlx5_ib_read_user_wqe for a QP with wqe_cnt == 0. qp_type: 0x%x\n",
155 qp->ibqp.qp_type);
156 return -EINVAL;
157 }
158
159 offset = wq->offset + ((wqe_index % wq->wqe_cnt) << wq->wqe_shift);
160 wq_end = wq->offset + (wq->wqe_cnt << wq->wqe_shift);
161
162 if (send && length < sizeof(struct mlx5_wqe_ctrl_seg))
163 return -EINVAL;
164
165 if (offset > umem->length ||
166 (send && offset + sizeof(struct mlx5_wqe_ctrl_seg) > umem->length))
167 return -EINVAL;
168
169 first_copy_length = min_t(u32, offset + length, wq_end) - offset;
170 ret = ib_umem_copy_from(buffer, umem, offset, first_copy_length);
171 if (ret)
172 return ret;
173
174 if (send) {
175 struct mlx5_wqe_ctrl_seg *ctrl = buffer;
176 int ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
177
178 wqe_length = ds * MLX5_WQE_DS_UNITS;
179 } else {
180 wqe_length = 1 << wq->wqe_shift;
181 }
182
183 if (wqe_length <= first_copy_length)
184 return first_copy_length;
185
186 ret = ib_umem_copy_from(buffer + first_copy_length, umem, wq->offset,
187 wqe_length - first_copy_length);
188 if (ret)
189 return ret;
190
191 return wqe_length;
192}
193
Eli Cohene126ba92013-07-07 17:25:49 +0300194static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
195{
196 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
197 struct ib_event event;
198
majd@mellanox.com19098df2016-01-14 19:13:03 +0200199 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
200 /* This event is only valid for trans_qps */
201 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
202 }
Eli Cohene126ba92013-07-07 17:25:49 +0300203
204 if (ibqp->event_handler) {
205 event.device = ibqp->device;
206 event.element.qp = ibqp;
207 switch (type) {
208 case MLX5_EVENT_TYPE_PATH_MIG:
209 event.event = IB_EVENT_PATH_MIG;
210 break;
211 case MLX5_EVENT_TYPE_COMM_EST:
212 event.event = IB_EVENT_COMM_EST;
213 break;
214 case MLX5_EVENT_TYPE_SQ_DRAINED:
215 event.event = IB_EVENT_SQ_DRAINED;
216 break;
217 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
218 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
219 break;
220 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
221 event.event = IB_EVENT_QP_FATAL;
222 break;
223 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
224 event.event = IB_EVENT_PATH_MIG_ERR;
225 break;
226 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
227 event.event = IB_EVENT_QP_REQ_ERR;
228 break;
229 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
230 event.event = IB_EVENT_QP_ACCESS_ERR;
231 break;
232 default:
233 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
234 return;
235 }
236
237 ibqp->event_handler(&event, ibqp->qp_context);
238 }
239}
240
241static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
242 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
243{
244 int wqe_size;
245 int wq_size;
246
247 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300248 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300249 return -EINVAL;
250
251 if (!has_rq) {
252 qp->rq.max_gs = 0;
253 qp->rq.wqe_cnt = 0;
254 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300255 cap->max_recv_wr = 0;
256 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300257 } else {
258 if (ucmd) {
259 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
260 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
261 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
262 qp->rq.max_post = qp->rq.wqe_cnt;
263 } else {
264 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
265 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
266 wqe_size = roundup_pow_of_two(wqe_size);
267 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
268 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
269 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300270 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300271 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
272 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300273 MLX5_CAP_GEN(dev->mdev,
274 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300275 return -EINVAL;
276 }
277 qp->rq.wqe_shift = ilog2(wqe_size);
278 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
279 qp->rq.max_post = qp->rq.wqe_cnt;
280 }
281 }
282
283 return 0;
284}
285
Erez Shitritf0313962016-02-21 16:27:17 +0200286static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300287{
Andi Shyti618af382013-07-16 15:35:01 +0200288 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300289
Erez Shitritf0313962016-02-21 16:27:17 +0200290 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300291 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300292 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300293 /* fall through */
294 case IB_QPT_RC:
295 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200296 max(sizeof(struct mlx5_wqe_atomic_seg) +
297 sizeof(struct mlx5_wqe_raddr_seg),
298 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
299 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300300 break;
301
Eli Cohenb125a542013-09-11 16:35:22 +0300302 case IB_QPT_XRC_TGT:
303 return 0;
304
Eli Cohene126ba92013-07-07 17:25:49 +0300305 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300306 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200307 max(sizeof(struct mlx5_wqe_raddr_seg),
308 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
309 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300310 break;
311
312 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200313 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
314 size += sizeof(struct mlx5_wqe_eth_pad) +
315 sizeof(struct mlx5_wqe_eth_seg);
316 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300317 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200318 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300319 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300320 sizeof(struct mlx5_wqe_datagram_seg);
321 break;
322
323 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300324 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300325 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
326 sizeof(struct mlx5_mkey_seg);
327 break;
328
329 default:
330 return -EINVAL;
331 }
332
333 return size;
334}
335
336static int calc_send_wqe(struct ib_qp_init_attr *attr)
337{
338 int inl_size = 0;
339 int size;
340
Erez Shitritf0313962016-02-21 16:27:17 +0200341 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300342 if (size < 0)
343 return size;
344
345 if (attr->cap.max_inline_data) {
346 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
347 attr->cap.max_inline_data;
348 }
349
350 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200351 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN &&
352 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
353 return MLX5_SIG_WQE_SIZE;
354 else
355 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300356}
357
Eli Cohen288c01b2016-10-27 16:36:45 +0300358static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
359{
360 int max_sge;
361
362 if (attr->qp_type == IB_QPT_RC)
363 max_sge = (min_t(int, wqe_size, 512) -
364 sizeof(struct mlx5_wqe_ctrl_seg) -
365 sizeof(struct mlx5_wqe_raddr_seg)) /
366 sizeof(struct mlx5_wqe_data_seg);
367 else if (attr->qp_type == IB_QPT_XRC_INI)
368 max_sge = (min_t(int, wqe_size, 512) -
369 sizeof(struct mlx5_wqe_ctrl_seg) -
370 sizeof(struct mlx5_wqe_xrc_seg) -
371 sizeof(struct mlx5_wqe_raddr_seg)) /
372 sizeof(struct mlx5_wqe_data_seg);
373 else
374 max_sge = (wqe_size - sq_overhead(attr)) /
375 sizeof(struct mlx5_wqe_data_seg);
376
377 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
378 sizeof(struct mlx5_wqe_data_seg));
379}
380
Eli Cohene126ba92013-07-07 17:25:49 +0300381static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
382 struct mlx5_ib_qp *qp)
383{
384 int wqe_size;
385 int wq_size;
386
387 if (!attr->cap.max_send_wr)
388 return 0;
389
390 wqe_size = calc_send_wqe(attr);
391 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
392 if (wqe_size < 0)
393 return wqe_size;
394
Saeed Mahameed938fe832015-05-28 22:28:41 +0300395 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300396 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300397 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300398 return -EINVAL;
399 }
400
Erez Shitritf0313962016-02-21 16:27:17 +0200401 qp->max_inline_data = wqe_size - sq_overhead(attr) -
402 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300403 attr->cap.max_inline_data = qp->max_inline_data;
404
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200405 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN)
406 qp->signature_en = true;
407
Eli Cohene126ba92013-07-07 17:25:49 +0300408 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
409 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300410 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800411 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
412 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300413 qp->sq.wqe_cnt,
414 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300415 return -ENOMEM;
416 }
Eli Cohene126ba92013-07-07 17:25:49 +0300417 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300418 qp->sq.max_gs = get_send_sge(attr, wqe_size);
419 if (qp->sq.max_gs < attr->cap.max_send_sge)
420 return -ENOMEM;
421
422 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300423 qp->sq.max_post = wq_size / wqe_size;
424 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300425
426 return wq_size;
427}
428
429static int set_user_buf_size(struct mlx5_ib_dev *dev,
430 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200431 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200432 struct mlx5_ib_qp_base *base,
433 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300434{
435 int desc_sz = 1 << qp->sq.wqe_shift;
436
Saeed Mahameed938fe832015-05-28 22:28:41 +0300437 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300438 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300439 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300440 return -EINVAL;
441 }
442
443 if (ucmd->sq_wqe_count && ((1 << ilog2(ucmd->sq_wqe_count)) != ucmd->sq_wqe_count)) {
444 mlx5_ib_warn(dev, "sq_wqe_count %d, sq_wqe_count %d\n",
445 ucmd->sq_wqe_count, ucmd->sq_wqe_count);
446 return -EINVAL;
447 }
448
449 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
450
Saeed Mahameed938fe832015-05-28 22:28:41 +0300451 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300452 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300453 qp->sq.wqe_cnt,
454 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300455 return -EINVAL;
456 }
457
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300458 if (attr->qp_type == IB_QPT_RAW_PACKET ||
459 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200460 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
461 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
462 } else {
463 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
464 (qp->sq.wqe_cnt << 6);
465 }
Eli Cohene126ba92013-07-07 17:25:49 +0300466
467 return 0;
468}
469
470static int qp_has_rq(struct ib_qp_init_attr *attr)
471{
472 if (attr->qp_type == IB_QPT_XRC_INI ||
473 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
474 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
475 !attr->cap.max_recv_wr)
476 return 0;
477
478 return 1;
479}
480
Eli Cohen2f5ff262017-01-03 23:55:21 +0200481static int first_med_bfreg(void)
Eli Cohenc1be5232014-01-14 17:45:12 +0200482{
483 return 1;
484}
485
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200486enum {
487 /* this is the first blue flame register in the array of bfregs assigned
488 * to a processes. Since we do not use it for blue flame but rather
489 * regular 64 bit doorbells, we do not need a lock for maintaiing
490 * "odd/even" order
491 */
492 NUM_NON_BLUE_FLAME_BFREGS = 1,
493};
494
Eli Cohenb037c292017-01-03 23:55:26 +0200495static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
496{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200497 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200498}
499
500static int num_med_bfreg(struct mlx5_ib_dev *dev,
501 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200502{
503 int n;
504
Eli Cohenb037c292017-01-03 23:55:26 +0200505 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
506 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200507
508 return n >= 0 ? n : 0;
509}
510
Eli Cohenb037c292017-01-03 23:55:26 +0200511static int first_hi_bfreg(struct mlx5_ib_dev *dev,
512 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200513{
514 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200515
Eli Cohenb037c292017-01-03 23:55:26 +0200516 med = num_med_bfreg(dev, bfregi);
517 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200518}
519
Eli Cohenb037c292017-01-03 23:55:26 +0200520static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
521 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300522{
Eli Cohene126ba92013-07-07 17:25:49 +0300523 int i;
524
Eli Cohenb037c292017-01-03 23:55:26 +0200525 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
526 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200527 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300528 return i;
529 }
530 }
531
532 return -ENOMEM;
533}
534
Eli Cohenb037c292017-01-03 23:55:26 +0200535static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
536 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300537{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200538 int minidx = first_med_bfreg();
Eli Cohene126ba92013-07-07 17:25:49 +0300539 int i;
540
Eli Cohenb037c292017-01-03 23:55:26 +0200541 for (i = first_med_bfreg(); i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200542 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300543 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200544 if (!bfregi->count[minidx])
545 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300546 }
547
Eli Cohen2f5ff262017-01-03 23:55:21 +0200548 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300549 return minidx;
550}
551
Eli Cohenb037c292017-01-03 23:55:26 +0200552static int alloc_bfreg(struct mlx5_ib_dev *dev,
553 struct mlx5_bfreg_info *bfregi,
Eli Cohen2f5ff262017-01-03 23:55:21 +0200554 enum mlx5_ib_latency_class lat)
Eli Cohene126ba92013-07-07 17:25:49 +0300555{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200556 int bfregn = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300557
Eli Cohen2f5ff262017-01-03 23:55:21 +0200558 mutex_lock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300559 switch (lat) {
560 case MLX5_IB_LATENCY_CLASS_LOW:
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200561 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200562 bfregn = 0;
563 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300564 break;
565
566 case MLX5_IB_LATENCY_CLASS_MEDIUM:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200567 if (bfregi->ver < 2)
568 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200569 else
Eli Cohenb037c292017-01-03 23:55:26 +0200570 bfregn = alloc_med_class_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300571 break;
572
573 case MLX5_IB_LATENCY_CLASS_HIGH:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200574 if (bfregi->ver < 2)
575 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200576 else
Eli Cohenb037c292017-01-03 23:55:26 +0200577 bfregn = alloc_high_class_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300578 break;
579 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200580 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300581
Eli Cohen2f5ff262017-01-03 23:55:21 +0200582 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300583}
584
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200585void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300586{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200587 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200588 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200589 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300590}
591
592static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
593{
594 switch (state) {
595 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
596 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
597 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
598 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
599 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
600 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
601 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
602 default: return -1;
603 }
604}
605
606static int to_mlx5_st(enum ib_qp_type type)
607{
608 switch (type) {
609 case IB_QPT_RC: return MLX5_QP_ST_RC;
610 case IB_QPT_UC: return MLX5_QP_ST_UC;
611 case IB_QPT_UD: return MLX5_QP_ST_UD;
612 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
613 case IB_QPT_XRC_INI:
614 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
615 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200616 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200617 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300618 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300619 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200620 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300621 case IB_QPT_MAX:
622 default: return -EINVAL;
623 }
624}
625
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300626static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
627 struct mlx5_ib_cq *recv_cq);
628static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
629 struct mlx5_ib_cq *recv_cq);
630
Eli Cohenb037c292017-01-03 23:55:26 +0200631static int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200632 struct mlx5_bfreg_info *bfregi, int bfregn,
633 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300634{
Eli Cohenb037c292017-01-03 23:55:26 +0200635 int bfregs_per_sys_page;
636 int index_of_sys_page;
637 int offset;
638
639 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
640 MLX5_NON_FP_BFREGS_PER_UAR;
641 index_of_sys_page = bfregn / bfregs_per_sys_page;
642
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200643 if (dyn_bfreg) {
644 index_of_sys_page += bfregi->num_static_sys_pages;
645 if (bfregn > bfregi->num_dyn_bfregs ||
646 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
647 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
648 return -EINVAL;
649 }
650 }
Eli Cohenb037c292017-01-03 23:55:26 +0200651
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200652 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200653 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300654}
655
majd@mellanox.com19098df2016-01-14 19:13:03 +0200656static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev,
657 struct ib_pd *pd,
658 unsigned long addr, size_t size,
659 struct ib_umem **umem,
660 int *npages, int *page_shift, int *ncont,
661 u32 *offset)
662{
663 int err;
664
665 *umem = ib_umem_get(pd->uobject->context, addr, size, 0, 0);
666 if (IS_ERR(*umem)) {
667 mlx5_ib_dbg(dev, "umem_get failed\n");
668 return PTR_ERR(*umem);
669 }
670
Majd Dibbiny762f8992016-10-27 16:36:47 +0300671 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200672
673 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
674 if (err) {
675 mlx5_ib_warn(dev, "bad offset\n");
676 goto err_umem;
677 }
678
679 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
680 addr, size, *npages, *page_shift, *ncont, *offset);
681
682 return 0;
683
684err_umem:
685 ib_umem_release(*umem);
686 *umem = NULL;
687
688 return err;
689}
690
Maor Gottliebfe248c32017-05-30 10:29:14 +0300691static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
692 struct mlx5_ib_rwq *rwq)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300693{
694 struct mlx5_ib_ucontext *context;
695
Maor Gottliebfe248c32017-05-30 10:29:14 +0300696 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
697 atomic_dec(&dev->delay_drop.rqs_cnt);
698
Yishai Hadas79b20a62016-05-23 15:20:50 +0300699 context = to_mucontext(pd->uobject->context);
700 mlx5_ib_db_unmap_user(context, &rwq->db);
701 if (rwq->umem)
702 ib_umem_release(rwq->umem);
703}
704
705static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
706 struct mlx5_ib_rwq *rwq,
707 struct mlx5_ib_create_wq *ucmd)
708{
709 struct mlx5_ib_ucontext *context;
710 int page_shift = 0;
711 int npages;
712 u32 offset = 0;
713 int ncont = 0;
714 int err;
715
716 if (!ucmd->buf_addr)
717 return -EINVAL;
718
719 context = to_mucontext(pd->uobject->context);
720 rwq->umem = ib_umem_get(pd->uobject->context, ucmd->buf_addr,
721 rwq->buf_size, 0, 0);
722 if (IS_ERR(rwq->umem)) {
723 mlx5_ib_dbg(dev, "umem_get failed\n");
724 err = PTR_ERR(rwq->umem);
725 return err;
726 }
727
Majd Dibbiny762f8992016-10-27 16:36:47 +0300728 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300729 &ncont, NULL);
730 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
731 &rwq->rq_page_offset);
732 if (err) {
733 mlx5_ib_warn(dev, "bad offset\n");
734 goto err_umem;
735 }
736
737 rwq->rq_num_pas = ncont;
738 rwq->page_shift = page_shift;
739 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
740 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
741
742 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
743 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
744 npages, page_shift, ncont, offset);
745
746 err = mlx5_ib_db_map_user(context, ucmd->db_addr, &rwq->db);
747 if (err) {
748 mlx5_ib_dbg(dev, "map failed\n");
749 goto err_umem;
750 }
751
752 rwq->create_type = MLX5_WQ_USER;
753 return 0;
754
755err_umem:
756 ib_umem_release(rwq->umem);
757 return err;
758}
759
Eli Cohenb037c292017-01-03 23:55:26 +0200760static int adjust_bfregn(struct mlx5_ib_dev *dev,
761 struct mlx5_bfreg_info *bfregi, int bfregn)
762{
763 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
764 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
765}
766
Eli Cohene126ba92013-07-07 17:25:49 +0300767static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
768 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200769 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300770 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200771 struct mlx5_ib_create_qp_resp *resp, int *inlen,
772 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300773{
774 struct mlx5_ib_ucontext *context;
775 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200776 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200777 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200778 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300779 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200780 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200781 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200782 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300783 __be64 *pas;
784 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300785 int err;
786
787 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
788 if (err) {
789 mlx5_ib_dbg(dev, "copy failed\n");
790 return err;
791 }
792
793 context = to_mucontext(pd->uobject->context);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200794 if (ucmd.flags & MLX5_QP_FLAG_BFREG_INDEX) {
795 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
796 ucmd.bfreg_index, true);
797 if (uar_index < 0)
798 return uar_index;
799
800 bfregn = MLX5_IB_INVALID_BFREG;
801 } else if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL) {
802 /*
803 * TBD: should come from the verbs when we have the API
804 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200805 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200806 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200807 }
Leon Romanovsky051f2632015-12-20 12:16:11 +0200808 else {
Eli Cohenb037c292017-01-03 23:55:26 +0200809 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_HIGH);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200810 if (bfregn < 0) {
811 mlx5_ib_dbg(dev, "failed to allocate low latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200812 mlx5_ib_dbg(dev, "reverting to medium latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200813 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_MEDIUM);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200814 if (bfregn < 0) {
815 mlx5_ib_dbg(dev, "failed to allocate medium latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200816 mlx5_ib_dbg(dev, "reverting to high latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200817 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_LOW);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200818 if (bfregn < 0) {
819 mlx5_ib_warn(dev, "bfreg allocation failed\n");
820 return bfregn;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200821 }
Eli Cohenc1be5232014-01-14 17:45:12 +0200822 }
Eli Cohene126ba92013-07-07 17:25:49 +0300823 }
824 }
825
Eli Cohen2f5ff262017-01-03 23:55:21 +0200826 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200827 if (bfregn != MLX5_IB_INVALID_BFREG)
828 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
829 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300830
Haggai Eran48fea832014-05-22 14:50:11 +0300831 qp->rq.offset = 0;
832 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
833 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
834
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200835 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300836 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200837 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300838
majd@mellanox.com19098df2016-01-14 19:13:03 +0200839 if (ucmd.buf_addr && ubuffer->buf_size) {
840 ubuffer->buf_addr = ucmd.buf_addr;
841 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr,
842 ubuffer->buf_size,
843 &ubuffer->umem, &npages, &page_shift,
844 &ncont, &offset);
845 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200846 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200847 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200848 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300849 }
Eli Cohene126ba92013-07-07 17:25:49 +0300850
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300851 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
852 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300853 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300854 if (!*in) {
855 err = -ENOMEM;
856 goto err_umem;
857 }
Eli Cohene126ba92013-07-07 17:25:49 +0300858
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300859 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
860 if (ubuffer->umem)
861 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
862
863 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
864
865 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
866 MLX5_SET(qpc, qpc, page_offset, offset);
867
868 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200869 if (bfregn != MLX5_IB_INVALID_BFREG)
870 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
871 else
872 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200873 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300874
875 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
876 if (err) {
877 mlx5_ib_dbg(dev, "map failed\n");
878 goto err_free;
879 }
880
881 err = ib_copy_to_udata(udata, resp, sizeof(*resp));
882 if (err) {
883 mlx5_ib_dbg(dev, "copy failed\n");
884 goto err_unmap;
885 }
886 qp->create_type = MLX5_QP_USER;
887
888 return 0;
889
890err_unmap:
891 mlx5_ib_db_unmap_user(context, &qp->db);
892
893err_free:
Al Viro479163f2014-11-20 08:13:57 +0000894 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300895
896err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200897 if (ubuffer->umem)
898 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300899
Eli Cohen2f5ff262017-01-03 23:55:21 +0200900err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200901 if (bfregn != MLX5_IB_INVALID_BFREG)
902 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300903 return err;
904}
905
Eli Cohenb037c292017-01-03 23:55:26 +0200906static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
907 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300908{
909 struct mlx5_ib_ucontext *context;
910
911 context = to_mucontext(pd->uobject->context);
912 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200913 if (base->ubuffer.umem)
914 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200915
916 /*
917 * Free only the BFREGs which are handled by the kernel.
918 * BFREGs of UARs allocated dynamically are handled by user.
919 */
920 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
921 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300922}
923
924static int create_kernel_qp(struct mlx5_ib_dev *dev,
925 struct ib_qp_init_attr *init_attr,
926 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300927 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200928 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300929{
Eli Cohene126ba92013-07-07 17:25:49 +0300930 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300931 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300932 int err;
933
Erez Shitritf0313962016-02-21 16:27:17 +0200934 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
935 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200936 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +0300937 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200938 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200939 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300940
941 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200942 qp->bf.bfreg = &dev->fp_bfreg;
943 else
944 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300945
Eli Cohend8030b02017-02-09 19:31:47 +0200946 /* We need to divide by two since each register is comprised of
947 * two buffers of identical size, namely odd and even
948 */
949 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200950 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +0300951
952 err = calc_sq_size(dev, init_attr, qp);
953 if (err < 0) {
954 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200955 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300956 }
957
958 qp->rq.offset = 0;
959 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200960 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300961
majd@mellanox.com19098df2016-01-14 19:13:03 +0200962 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300963 if (err) {
964 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200965 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300966 }
967
968 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300969 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
970 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300971 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300972 if (!*in) {
973 err = -ENOMEM;
974 goto err_buf;
975 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300976
977 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
978 MLX5_SET(qpc, qpc, uar_page, uar_index);
979 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
980
Eli Cohene126ba92013-07-07 17:25:49 +0300981 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300982 MLX5_SET(qpc, qpc, fre, 1);
983 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300984
Haggai Eranb11a4f92016-02-29 15:45:03 +0200985 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300986 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200987 qp->flags |= MLX5_IB_QP_SQPN_QP1;
988 }
989
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300990 mlx5_fill_page_array(&qp->buf,
991 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300992
Jack Morgenstein9603b612014-07-28 23:30:22 +0300993 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300994 if (err) {
995 mlx5_ib_dbg(dev, "err %d\n", err);
996 goto err_free;
997 }
998
Li Dongyangb5883002017-08-16 23:31:22 +1000999 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
1000 sizeof(*qp->sq.wrid), GFP_KERNEL);
1001 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1002 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1003 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1004 sizeof(*qp->rq.wrid), GFP_KERNEL);
1005 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1006 sizeof(*qp->sq.w_list), GFP_KERNEL);
1007 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1008 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001009
1010 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1011 !qp->sq.w_list || !qp->sq.wqe_head) {
1012 err = -ENOMEM;
1013 goto err_wrid;
1014 }
1015 qp->create_type = MLX5_QP_KERNEL;
1016
1017 return 0;
1018
1019err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001020 kvfree(qp->sq.wqe_head);
1021 kvfree(qp->sq.w_list);
1022 kvfree(qp->sq.wrid);
1023 kvfree(qp->sq.wr_data);
1024 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001025 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001026
1027err_free:
Al Viro479163f2014-11-20 08:13:57 +00001028 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001029
1030err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001031 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001032 return err;
1033}
1034
1035static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1036{
Li Dongyangb5883002017-08-16 23:31:22 +10001037 kvfree(qp->sq.wqe_head);
1038 kvfree(qp->sq.w_list);
1039 kvfree(qp->sq.wrid);
1040 kvfree(qp->sq.wr_data);
1041 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001042 mlx5_db_free(dev->mdev, &qp->db);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001043 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001044}
1045
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001046static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001047{
1048 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Moni Shouac32a4f22018-01-02 16:19:32 +02001049 (attr->qp_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001050 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001051 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001052 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001053 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001054 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001055 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001056}
1057
1058static int is_connected(enum ib_qp_type qp_type)
1059{
1060 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1061 return 1;
1062
1063 return 0;
1064}
1065
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001066static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001067 struct mlx5_ib_qp *qp,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001068 struct mlx5_ib_sq *sq, u32 tdn)
1069{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001070 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001071 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1072
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001073 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001074 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1075 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1076
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001077 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1078}
1079
1080static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1081 struct mlx5_ib_sq *sq)
1082{
1083 mlx5_core_destroy_tis(dev->mdev, sq->tisn);
1084}
1085
Mark Blochb96c9dd2018-01-29 10:40:37 +00001086static void destroy_flow_rule_vport_sq(struct mlx5_ib_dev *dev,
1087 struct mlx5_ib_sq *sq)
1088{
1089 if (sq->flow_rule)
1090 mlx5_del_flow_rules(sq->flow_rule);
1091}
1092
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001093static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1094 struct mlx5_ib_sq *sq, void *qpin,
1095 struct ib_pd *pd)
1096{
1097 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1098 __be64 *pas;
1099 void *in;
1100 void *sqc;
1101 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1102 void *wq;
1103 int inlen;
1104 int err;
1105 int page_shift = 0;
1106 int npages;
1107 int ncont = 0;
1108 u32 offset = 0;
1109
1110 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1111 &sq->ubuffer.umem, &npages, &page_shift,
1112 &ncont, &offset);
1113 if (err)
1114 return err;
1115
1116 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001117 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001118 if (!in) {
1119 err = -ENOMEM;
1120 goto err_umem;
1121 }
1122
1123 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1124 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001125 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1126 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001127 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1128 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1129 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1130 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1131 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001132 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1133 MLX5_CAP_ETH(dev->mdev, swp))
1134 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001135
1136 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1137 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1138 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1139 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1140 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1141 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1142 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1143 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1144 MLX5_SET(wq, wq, page_offset, offset);
1145
1146 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1147 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1148
1149 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1150
1151 kvfree(in);
1152
1153 if (err)
1154 goto err_umem;
1155
Mark Blochb96c9dd2018-01-29 10:40:37 +00001156 err = create_flow_rule_vport_sq(dev, sq);
1157 if (err)
1158 goto err_flow;
1159
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001160 return 0;
1161
Mark Blochb96c9dd2018-01-29 10:40:37 +00001162err_flow:
1163 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1164
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001165err_umem:
1166 ib_umem_release(sq->ubuffer.umem);
1167 sq->ubuffer.umem = NULL;
1168
1169 return err;
1170}
1171
1172static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1173 struct mlx5_ib_sq *sq)
1174{
Mark Blochb96c9dd2018-01-29 10:40:37 +00001175 destroy_flow_rule_vport_sq(dev, sq);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001176 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1177 ib_umem_release(sq->ubuffer.umem);
1178}
1179
1180static int get_rq_pas_size(void *qpc)
1181{
1182 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1183 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1184 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1185 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1186 u32 po_quanta = 1 << (log_page_size - 6);
1187 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1188 u32 page_size = 1 << log_page_size;
1189 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1190 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1191
1192 return rq_num_pas * sizeof(u64);
1193}
1194
1195static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1196 struct mlx5_ib_rq *rq, void *qpin)
1197{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001198 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001199 __be64 *pas;
1200 __be64 *qp_pas;
1201 void *in;
1202 void *rqc;
1203 void *wq;
1204 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1205 int inlen;
1206 int err;
1207 u32 rq_pas_size = get_rq_pas_size(qpc);
1208
1209 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001210 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001211 if (!in)
1212 return -ENOMEM;
1213
1214 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001215 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1216 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001217 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1218 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1219 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1220 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1221 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1222
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001223 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1224 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1225
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001226 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1227 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001228 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1229 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001230 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1231 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1232 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1233 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1234 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1235 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1236
1237 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1238 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1239 memcpy(pas, qp_pas, rq_pas_size);
1240
1241 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1242
1243 kvfree(in);
1244
1245 return err;
1246}
1247
1248static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1249 struct mlx5_ib_rq *rq)
1250{
1251 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1252}
1253
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001254static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1255{
1256 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1257 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1258 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1259}
1260
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001261static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001262 struct mlx5_ib_rq *rq, u32 tdn,
1263 bool tunnel_offload_en)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001264{
1265 u32 *in;
1266 void *tirc;
1267 int inlen;
1268 int err;
1269
1270 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001271 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001272 if (!in)
1273 return -ENOMEM;
1274
1275 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1276 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1277 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1278 MLX5_SET(tirc, tirc, transport_domain, tdn);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001279 if (tunnel_offload_en)
1280 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001281
1282 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1283
1284 kvfree(in);
1285
1286 return err;
1287}
1288
1289static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1290 struct mlx5_ib_rq *rq)
1291{
1292 mlx5_core_destroy_tir(dev->mdev, rq->tirn);
1293}
1294
1295static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001296 u32 *in,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001297 struct ib_pd *pd)
1298{
1299 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1300 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1301 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1302 struct ib_uobject *uobj = pd->uobject;
1303 struct ib_ucontext *ucontext = uobj->context;
1304 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1305 int err;
1306 u32 tdn = mucontext->tdn;
1307
1308 if (qp->sq.wqe_cnt) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001309 err = create_raw_packet_qp_tis(dev, qp, sq, tdn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001310 if (err)
1311 return err;
1312
1313 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1314 if (err)
1315 goto err_destroy_tis;
1316
1317 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001318 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001319 }
1320
1321 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001322 rq->base.container_mibqp = qp;
1323
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001324 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1325 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001326 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1327 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001328 err = create_raw_packet_qp_rq(dev, rq, in);
1329 if (err)
1330 goto err_destroy_sq;
1331
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001332
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001333 err = create_raw_packet_qp_tir(dev, rq, tdn,
1334 qp->tunnel_offload_en);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001335 if (err)
1336 goto err_destroy_rq;
1337 }
1338
1339 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1340 rq->base.mqp.qpn;
1341
1342 return 0;
1343
1344err_destroy_rq:
1345 destroy_raw_packet_qp_rq(dev, rq);
1346err_destroy_sq:
1347 if (!qp->sq.wqe_cnt)
1348 return err;
1349 destroy_raw_packet_qp_sq(dev, sq);
1350err_destroy_tis:
1351 destroy_raw_packet_qp_tis(dev, sq);
1352
1353 return err;
1354}
1355
1356static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1357 struct mlx5_ib_qp *qp)
1358{
1359 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1360 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1361 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1362
1363 if (qp->rq.wqe_cnt) {
1364 destroy_raw_packet_qp_tir(dev, rq);
1365 destroy_raw_packet_qp_rq(dev, rq);
1366 }
1367
1368 if (qp->sq.wqe_cnt) {
1369 destroy_raw_packet_qp_sq(dev, sq);
1370 destroy_raw_packet_qp_tis(dev, sq);
1371 }
1372}
1373
1374static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1375 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1376{
1377 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1378 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1379
1380 sq->sq = &qp->sq;
1381 rq->rq = &qp->rq;
1382 sq->doorbell = &qp->db;
1383 rq->doorbell = &qp->db;
1384}
1385
Yishai Hadas28d61372016-05-23 15:20:56 +03001386static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1387{
1388 mlx5_core_destroy_tir(dev->mdev, qp->rss_qp.tirn);
1389}
1390
1391static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1392 struct ib_pd *pd,
1393 struct ib_qp_init_attr *init_attr,
1394 struct ib_udata *udata)
1395{
1396 struct ib_uobject *uobj = pd->uobject;
1397 struct ib_ucontext *ucontext = uobj->context;
1398 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1399 struct mlx5_ib_create_qp_resp resp = {};
1400 int inlen;
1401 int err;
1402 u32 *in;
1403 void *tirc;
1404 void *hfso;
1405 u32 selected_fields = 0;
1406 size_t min_resp_len;
1407 u32 tdn = mucontext->tdn;
1408 struct mlx5_ib_create_qp_rss ucmd = {};
1409 size_t required_cmd_sz;
1410
1411 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1412 return -EOPNOTSUPP;
1413
1414 if (init_attr->create_flags || init_attr->send_cq)
1415 return -EINVAL;
1416
Eli Cohen2f5ff262017-01-03 23:55:21 +02001417 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001418 if (udata->outlen < min_resp_len)
1419 return -EINVAL;
1420
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001421 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001422 if (udata->inlen < required_cmd_sz) {
1423 mlx5_ib_dbg(dev, "invalid inlen\n");
1424 return -EINVAL;
1425 }
1426
1427 if (udata->inlen > sizeof(ucmd) &&
1428 !ib_is_udata_cleared(udata, sizeof(ucmd),
1429 udata->inlen - sizeof(ucmd))) {
1430 mlx5_ib_dbg(dev, "inlen is not supported\n");
1431 return -EOPNOTSUPP;
1432 }
1433
1434 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1435 mlx5_ib_dbg(dev, "copy failed\n");
1436 return -EFAULT;
1437 }
1438
1439 if (ucmd.comp_mask) {
1440 mlx5_ib_dbg(dev, "invalid comp mask\n");
1441 return -EOPNOTSUPP;
1442 }
1443
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001444 if (ucmd.flags & ~MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
1445 mlx5_ib_dbg(dev, "invalid flags\n");
1446 return -EOPNOTSUPP;
1447 }
1448
1449 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1450 !tunnel_offload_supported(dev->mdev)) {
1451 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001452 return -EOPNOTSUPP;
1453 }
1454
Maor Gottlieb309fa342017-10-19 08:25:56 +03001455 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1456 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1457 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1458 return -EOPNOTSUPP;
1459 }
1460
Yishai Hadas28d61372016-05-23 15:20:56 +03001461 err = ib_copy_to_udata(udata, &resp, min_resp_len);
1462 if (err) {
1463 mlx5_ib_dbg(dev, "copy failed\n");
1464 return -EINVAL;
1465 }
1466
1467 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001468 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001469 if (!in)
1470 return -ENOMEM;
1471
1472 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1473 MLX5_SET(tirc, tirc, disp_type,
1474 MLX5_TIRC_DISP_TYPE_INDIRECT);
1475 MLX5_SET(tirc, tirc, indirect_table,
1476 init_attr->rwq_ind_tbl->ind_tbl_num);
1477 MLX5_SET(tirc, tirc, transport_domain, tdn);
1478
1479 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001480
1481 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1482 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1483
Maor Gottlieb309fa342017-10-19 08:25:56 +03001484 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1485 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1486 else
1487 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1488
Yishai Hadas28d61372016-05-23 15:20:56 +03001489 switch (ucmd.rx_hash_function) {
1490 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1491 {
1492 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1493 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1494
1495 if (len != ucmd.rx_key_len) {
1496 err = -EINVAL;
1497 goto err;
1498 }
1499
1500 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1501 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1502 memcpy(rss_key, ucmd.rx_hash_key, len);
1503 break;
1504 }
1505 default:
1506 err = -EOPNOTSUPP;
1507 goto err;
1508 }
1509
1510 if (!ucmd.rx_hash_fields_mask) {
1511 /* special case when this TIR serves as steering entry without hashing */
1512 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1513 goto create_tir;
1514 err = -EINVAL;
1515 goto err;
1516 }
1517
1518 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1519 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1520 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1521 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1522 err = -EINVAL;
1523 goto err;
1524 }
1525
1526 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1527 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1528 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1529 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1530 MLX5_L3_PROT_TYPE_IPV4);
1531 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1532 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1533 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1534 MLX5_L3_PROT_TYPE_IPV6);
1535
1536 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1537 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) &&
1538 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1539 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))) {
1540 err = -EINVAL;
1541 goto err;
1542 }
1543
1544 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1545 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1546 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1547 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1548 MLX5_L4_PROT_TYPE_TCP);
1549 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1550 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1551 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1552 MLX5_L4_PROT_TYPE_UDP);
1553
1554 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1555 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1556 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1557
1558 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1559 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1560 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1561
1562 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1563 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1564 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1565
1566 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1567 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1568 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1569
1570 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1571
1572create_tir:
1573 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1574
1575 if (err)
1576 goto err;
1577
1578 kvfree(in);
1579 /* qpn is reserved for that QP */
1580 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001581 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001582 return 0;
1583
1584err:
1585 kvfree(in);
1586 return err;
1587}
1588
Eli Cohene126ba92013-07-07 17:25:49 +03001589static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1590 struct ib_qp_init_attr *init_attr,
1591 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1592{
1593 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001594 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001595 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001596 struct mlx5_ib_create_qp_resp resp;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001597 struct mlx5_ib_cq *send_cq;
1598 struct mlx5_ib_cq *recv_cq;
1599 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001600 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001601 struct mlx5_ib_create_qp ucmd;
1602 struct mlx5_ib_qp_base *base;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001603 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001604 u32 *in;
1605 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001606
1607 mutex_init(&qp->mutex);
1608 spin_lock_init(&qp->sq.lock);
1609 spin_lock_init(&qp->rq.lock);
1610
Yishai Hadas28d61372016-05-23 15:20:56 +03001611 if (init_attr->rwq_ind_tbl) {
1612 if (!udata)
1613 return -ENOSYS;
1614
1615 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1616 return err;
1617 }
1618
Eli Cohenf360d882014-04-02 00:10:16 +03001619 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001620 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001621 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1622 return -EINVAL;
1623 } else {
1624 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1625 }
1626 }
1627
Leon Romanovsky051f2632015-12-20 12:16:11 +02001628 if (init_attr->create_flags &
1629 (IB_QP_CREATE_CROSS_CHANNEL |
1630 IB_QP_CREATE_MANAGED_SEND |
1631 IB_QP_CREATE_MANAGED_RECV)) {
1632 if (!MLX5_CAP_GEN(mdev, cd)) {
1633 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1634 return -EINVAL;
1635 }
1636 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1637 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1638 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1639 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1640 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1641 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1642 }
Erez Shitritf0313962016-02-21 16:27:17 +02001643
1644 if (init_attr->qp_type == IB_QPT_UD &&
1645 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1646 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1647 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1648 return -EOPNOTSUPP;
1649 }
1650
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001651 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1652 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1653 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1654 return -EOPNOTSUPP;
1655 }
1656 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1657 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1658 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1659 return -EOPNOTSUPP;
1660 }
1661 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1662 }
1663
Eli Cohene126ba92013-07-07 17:25:49 +03001664 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1665 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1666
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001667 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
1668 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1669 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
1670 (init_attr->qp_type != IB_QPT_RAW_PACKET))
1671 return -EOPNOTSUPP;
1672 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
1673 }
1674
Eli Cohene126ba92013-07-07 17:25:49 +03001675 if (pd && pd->uobject) {
1676 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1677 mlx5_ib_dbg(dev, "copy failed\n");
1678 return -EFAULT;
1679 }
1680
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001681 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1682 &ucmd, udata->inlen, &uidx);
1683 if (err)
1684 return err;
1685
Eli Cohene126ba92013-07-07 17:25:49 +03001686 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1687 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001688 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
1689 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
1690 !tunnel_offload_supported(mdev)) {
1691 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
1692 return -EOPNOTSUPP;
1693 }
1694 qp->tunnel_offload_en = true;
1695 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001696
1697 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
1698 if (init_attr->qp_type != IB_QPT_UD ||
1699 (MLX5_CAP_GEN(dev->mdev, port_type) !=
1700 MLX5_CAP_PORT_TYPE_IB) ||
1701 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
1702 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
1703 return -EOPNOTSUPP;
1704 }
1705
1706 qp->flags |= MLX5_IB_QP_UNDERLAY;
1707 qp->underlay_qpn = init_attr->source_qpn;
1708 }
Eli Cohene126ba92013-07-07 17:25:49 +03001709 } else {
1710 qp->wq_sig = !!wq_signature;
1711 }
1712
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001713 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1714 qp->flags & MLX5_IB_QP_UNDERLAY) ?
1715 &qp->raw_packet_qp.rq.base :
1716 &qp->trans_qp.base;
1717
Eli Cohene126ba92013-07-07 17:25:49 +03001718 qp->has_rq = qp_has_rq(init_attr);
1719 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1720 qp, (pd && pd->uobject) ? &ucmd : NULL);
1721 if (err) {
1722 mlx5_ib_dbg(dev, "err %d\n", err);
1723 return err;
1724 }
1725
1726 if (pd) {
1727 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001728 __u32 max_wqes =
1729 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001730 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1731 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1732 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1733 mlx5_ib_dbg(dev, "invalid rq params\n");
1734 return -EINVAL;
1735 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001736 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001737 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001738 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001739 return -EINVAL;
1740 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001741 if (init_attr->create_flags &
1742 mlx5_ib_create_qp_sqpn_qp1()) {
1743 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1744 return -EINVAL;
1745 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001746 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1747 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001748 if (err)
1749 mlx5_ib_dbg(dev, "err %d\n", err);
1750 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001751 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1752 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001753 if (err)
1754 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001755 }
1756
1757 if (err)
1758 return err;
1759 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001760 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001761 if (!in)
1762 return -ENOMEM;
1763
1764 qp->create_type = MLX5_QP_EMPTY;
1765 }
1766
1767 if (is_sqp(init_attr->qp_type))
1768 qp->port = init_attr->port_num;
1769
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001770 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1771
1772 MLX5_SET(qpc, qpc, st, to_mlx5_st(init_attr->qp_type));
1773 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001774
1775 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001776 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001777 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001778 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1779
Eli Cohene126ba92013-07-07 17:25:49 +03001780
1781 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001782 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001783
Eli Cohenf360d882014-04-02 00:10:16 +03001784 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001785 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001786
Leon Romanovsky051f2632015-12-20 12:16:11 +02001787 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001788 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001789 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001790 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001791 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001792 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001793
Eli Cohene126ba92013-07-07 17:25:49 +03001794 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1795 int rcqe_sz;
1796 int scqe_sz;
1797
1798 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1799 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1800
1801 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001802 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001803 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001804 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001805
1806 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1807 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001808 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001809 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001810 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001811 }
1812 }
1813
1814 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001815 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1816 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001817 }
1818
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001819 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001820
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001821 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001822 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001823 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001824 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001825 if (init_attr->srq &&
1826 init_attr->srq->srq_type == IB_SRQT_TM)
1827 MLX5_SET(qpc, qpc, offload_type,
1828 MLX5_QPC_OFFLOAD_TYPE_RNDV);
1829 }
Eli Cohene126ba92013-07-07 17:25:49 +03001830
1831 /* Set default resources */
1832 switch (init_attr->qp_type) {
1833 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001834 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1835 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1836 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1837 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001838 break;
1839 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001840 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1841 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1842 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001843 break;
1844 default:
1845 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001846 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1847 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001848 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001849 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1850 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001851 }
1852 }
1853
1854 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001855 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001856
1857 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001858 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001859
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001860 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001861
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001862 /* 0xffffff means we ask to work with cqe version 0 */
1863 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001864 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001865
Erez Shitritf0313962016-02-21 16:27:17 +02001866 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1867 if (init_attr->qp_type == IB_QPT_UD &&
1868 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001869 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1870 qp->flags |= MLX5_IB_QP_LSO;
1871 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001872
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001873 if (init_attr->create_flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
1874 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
1875 mlx5_ib_dbg(dev, "scatter end padding is not supported\n");
1876 err = -EOPNOTSUPP;
1877 goto err;
1878 } else if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1879 MLX5_SET(qpc, qpc, end_padding_mode,
1880 MLX5_WQ_END_PAD_MODE_ALIGN);
1881 } else {
1882 qp->flags |= MLX5_IB_QP_PCI_WRITE_END_PADDING;
1883 }
1884 }
1885
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001886 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1887 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001888 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
1889 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
1890 err = create_raw_packet_qp(dev, qp, in, pd);
1891 } else {
1892 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
1893 }
1894
Eli Cohene126ba92013-07-07 17:25:49 +03001895 if (err) {
1896 mlx5_ib_dbg(dev, "create qp failed\n");
1897 goto err_create;
1898 }
1899
Al Viro479163f2014-11-20 08:13:57 +00001900 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001901
majd@mellanox.com19098df2016-01-14 19:13:03 +02001902 base->container_mibqp = qp;
1903 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03001904
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001905 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
1906 &send_cq, &recv_cq);
1907 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1908 mlx5_ib_lock_cqs(send_cq, recv_cq);
1909 /* Maintain device to QPs access, needed for further handling via reset
1910 * flow
1911 */
1912 list_add_tail(&qp->qps_list, &dev->qp_list);
1913 /* Maintain CQ to QPs access, needed for further handling via reset flow
1914 */
1915 if (send_cq)
1916 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
1917 if (recv_cq)
1918 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
1919 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1920 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1921
Eli Cohene126ba92013-07-07 17:25:49 +03001922 return 0;
1923
1924err_create:
1925 if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02001926 destroy_qp_user(dev, pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001927 else if (qp->create_type == MLX5_QP_KERNEL)
1928 destroy_qp_kernel(dev, qp);
1929
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001930err:
Al Viro479163f2014-11-20 08:13:57 +00001931 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001932 return err;
1933}
1934
1935static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1936 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1937{
1938 if (send_cq) {
1939 if (recv_cq) {
1940 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001941 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001942 spin_lock_nested(&recv_cq->lock,
1943 SINGLE_DEPTH_NESTING);
1944 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001945 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001946 __acquire(&recv_cq->lock);
1947 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001948 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001949 spin_lock_nested(&send_cq->lock,
1950 SINGLE_DEPTH_NESTING);
1951 }
1952 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001953 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001954 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001955 }
1956 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001957 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001958 __acquire(&send_cq->lock);
1959 } else {
1960 __acquire(&send_cq->lock);
1961 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001962 }
1963}
1964
1965static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1966 __releases(&send_cq->lock) __releases(&recv_cq->lock)
1967{
1968 if (send_cq) {
1969 if (recv_cq) {
1970 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1971 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001972 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001973 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1974 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001975 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001976 } else {
1977 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001978 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001979 }
1980 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001981 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001982 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001983 }
1984 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001985 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001986 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001987 } else {
1988 __release(&recv_cq->lock);
1989 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001990 }
1991}
1992
1993static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
1994{
1995 return to_mpd(qp->ibqp.pd);
1996}
1997
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001998static void get_cqs(enum ib_qp_type qp_type,
1999 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03002000 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
2001{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002002 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03002003 case IB_QPT_XRC_TGT:
2004 *send_cq = NULL;
2005 *recv_cq = NULL;
2006 break;
2007 case MLX5_IB_QPT_REG_UMR:
2008 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002009 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002010 *recv_cq = NULL;
2011 break;
2012
2013 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002014 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002015 case IB_QPT_RC:
2016 case IB_QPT_UC:
2017 case IB_QPT_UD:
2018 case IB_QPT_RAW_IPV6:
2019 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002020 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002021 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2022 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002023 break;
2024
Eli Cohene126ba92013-07-07 17:25:49 +03002025 case IB_QPT_MAX:
2026 default:
2027 *send_cq = NULL;
2028 *recv_cq = NULL;
2029 break;
2030 }
2031}
2032
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002033static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002034 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2035 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002036
Eli Cohene126ba92013-07-07 17:25:49 +03002037static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
2038{
2039 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002040 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002041 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002042 int err;
2043
Yishai Hadas28d61372016-05-23 15:20:56 +03002044 if (qp->ibqp.rwq_ind_tbl) {
2045 destroy_rss_raw_qp_tir(dev, qp);
2046 return;
2047 }
2048
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002049 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2050 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002051 &qp->raw_packet_qp.rq.base :
2052 &qp->trans_qp.base;
2053
Haggai Eran6aec21f2014-12-11 17:04:23 +02002054 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002055 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2056 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002057 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002058 MLX5_CMD_OP_2RST_QP, 0,
2059 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002060 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002061 struct mlx5_modify_raw_qp_param raw_qp_param = {
2062 .operation = MLX5_CMD_OP_2RST_QP
2063 };
2064
Aviv Heller13eab212016-09-18 20:48:04 +03002065 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002066 }
2067 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002068 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002069 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002070 }
Eli Cohene126ba92013-07-07 17:25:49 +03002071
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002072 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2073 &send_cq, &recv_cq);
2074
2075 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2076 mlx5_ib_lock_cqs(send_cq, recv_cq);
2077 /* del from lists under both locks above to protect reset flow paths */
2078 list_del(&qp->qps_list);
2079 if (send_cq)
2080 list_del(&qp->cq_send_list);
2081
2082 if (recv_cq)
2083 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002084
2085 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002086 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002087 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2088 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002089 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2090 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002091 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002092 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2093 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002094
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002095 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2096 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002097 destroy_raw_packet_qp(dev, qp);
2098 } else {
2099 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
2100 if (err)
2101 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2102 base->mqp.qpn);
2103 }
Eli Cohene126ba92013-07-07 17:25:49 +03002104
Eli Cohene126ba92013-07-07 17:25:49 +03002105 if (qp->create_type == MLX5_QP_KERNEL)
2106 destroy_qp_kernel(dev, qp);
2107 else if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02002108 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002109}
2110
2111static const char *ib_qp_type_str(enum ib_qp_type type)
2112{
2113 switch (type) {
2114 case IB_QPT_SMI:
2115 return "IB_QPT_SMI";
2116 case IB_QPT_GSI:
2117 return "IB_QPT_GSI";
2118 case IB_QPT_RC:
2119 return "IB_QPT_RC";
2120 case IB_QPT_UC:
2121 return "IB_QPT_UC";
2122 case IB_QPT_UD:
2123 return "IB_QPT_UD";
2124 case IB_QPT_RAW_IPV6:
2125 return "IB_QPT_RAW_IPV6";
2126 case IB_QPT_RAW_ETHERTYPE:
2127 return "IB_QPT_RAW_ETHERTYPE";
2128 case IB_QPT_XRC_INI:
2129 return "IB_QPT_XRC_INI";
2130 case IB_QPT_XRC_TGT:
2131 return "IB_QPT_XRC_TGT";
2132 case IB_QPT_RAW_PACKET:
2133 return "IB_QPT_RAW_PACKET";
2134 case MLX5_IB_QPT_REG_UMR:
2135 return "MLX5_IB_QPT_REG_UMR";
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002136 case IB_QPT_DRIVER:
2137 return "IB_QPT_DRIVER";
Eli Cohene126ba92013-07-07 17:25:49 +03002138 case IB_QPT_MAX:
2139 default:
2140 return "Invalid QP type";
2141 }
2142}
2143
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002144static struct ib_qp *mlx5_ib_create_dct(struct ib_pd *pd,
2145 struct ib_qp_init_attr *attr,
2146 struct mlx5_ib_create_qp *ucmd)
2147{
2148 struct mlx5_ib_dev *dev;
2149 struct mlx5_ib_qp *qp;
2150 int err = 0;
2151 u32 uidx = MLX5_IB_DEFAULT_UIDX;
2152 void *dctc;
2153
2154 if (!attr->srq || !attr->recv_cq)
2155 return ERR_PTR(-EINVAL);
2156
2157 dev = to_mdev(pd->device);
2158
2159 err = get_qp_user_index(to_mucontext(pd->uobject->context),
2160 ucmd, sizeof(*ucmd), &uidx);
2161 if (err)
2162 return ERR_PTR(err);
2163
2164 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2165 if (!qp)
2166 return ERR_PTR(-ENOMEM);
2167
2168 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
2169 if (!qp->dct.in) {
2170 err = -ENOMEM;
2171 goto err_free;
2172 }
2173
2174 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shoua776a3902018-01-02 16:19:33 +02002175 qp->qp_sub_type = MLX5_IB_QPT_DCT;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002176 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2177 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2178 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2179 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2180 MLX5_SET(dctc, dctc, user_index, uidx);
2181
2182 qp->state = IB_QPS_RESET;
2183
2184 return &qp->ibqp;
2185err_free:
2186 kfree(qp);
2187 return ERR_PTR(err);
2188}
2189
2190static int set_mlx_qp_type(struct mlx5_ib_dev *dev,
2191 struct ib_qp_init_attr *init_attr,
2192 struct mlx5_ib_create_qp *ucmd,
2193 struct ib_udata *udata)
2194{
2195 enum { MLX_QP_FLAGS = MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI };
2196 int err;
2197
2198 if (!udata)
2199 return -EINVAL;
2200
2201 if (udata->inlen < sizeof(*ucmd)) {
2202 mlx5_ib_dbg(dev, "create_qp user command is smaller than expected\n");
2203 return -EINVAL;
2204 }
2205 err = ib_copy_from_udata(ucmd, udata, sizeof(*ucmd));
2206 if (err)
2207 return err;
2208
2209 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCI) {
2210 init_attr->qp_type = MLX5_IB_QPT_DCI;
2211 } else {
2212 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCT) {
2213 init_attr->qp_type = MLX5_IB_QPT_DCT;
2214 } else {
2215 mlx5_ib_dbg(dev, "Invalid QP flags\n");
2216 return -EINVAL;
2217 }
2218 }
2219
2220 if (!MLX5_CAP_GEN(dev->mdev, dct)) {
2221 mlx5_ib_dbg(dev, "DC transport is not supported\n");
2222 return -EOPNOTSUPP;
2223 }
2224
2225 return 0;
2226}
2227
Eli Cohene126ba92013-07-07 17:25:49 +03002228struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002229 struct ib_qp_init_attr *verbs_init_attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002230 struct ib_udata *udata)
2231{
2232 struct mlx5_ib_dev *dev;
2233 struct mlx5_ib_qp *qp;
2234 u16 xrcdn = 0;
2235 int err;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002236 struct ib_qp_init_attr mlx_init_attr;
2237 struct ib_qp_init_attr *init_attr = verbs_init_attr;
Eli Cohene126ba92013-07-07 17:25:49 +03002238
2239 if (pd) {
2240 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002241
2242 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2243 if (!pd->uobject) {
2244 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2245 return ERR_PTR(-EINVAL);
2246 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2247 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2248 return ERR_PTR(-EINVAL);
2249 }
2250 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002251 } else {
2252 /* being cautious here */
2253 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2254 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2255 pr_warn("%s: no PD for transport %s\n", __func__,
2256 ib_qp_type_str(init_attr->qp_type));
2257 return ERR_PTR(-EINVAL);
2258 }
2259 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002260 }
2261
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002262 if (init_attr->qp_type == IB_QPT_DRIVER) {
2263 struct mlx5_ib_create_qp ucmd;
2264
2265 init_attr = &mlx_init_attr;
2266 memcpy(init_attr, verbs_init_attr, sizeof(*verbs_init_attr));
2267 err = set_mlx_qp_type(dev, init_attr, &ucmd, udata);
2268 if (err)
2269 return ERR_PTR(err);
Moni Shouac32a4f22018-01-02 16:19:32 +02002270
2271 if (init_attr->qp_type == MLX5_IB_QPT_DCI) {
2272 if (init_attr->cap.max_recv_wr ||
2273 init_attr->cap.max_recv_sge) {
2274 mlx5_ib_dbg(dev, "DCI QP requires zero size receive queue\n");
2275 return ERR_PTR(-EINVAL);
2276 }
Moni Shoua776a3902018-01-02 16:19:33 +02002277 } else {
2278 return mlx5_ib_create_dct(pd, init_attr, &ucmd);
Moni Shouac32a4f22018-01-02 16:19:32 +02002279 }
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002280 }
2281
Eli Cohene126ba92013-07-07 17:25:49 +03002282 switch (init_attr->qp_type) {
2283 case IB_QPT_XRC_TGT:
2284 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002285 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002286 mlx5_ib_dbg(dev, "XRC not supported\n");
2287 return ERR_PTR(-ENOSYS);
2288 }
2289 init_attr->recv_cq = NULL;
2290 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2291 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2292 init_attr->send_cq = NULL;
2293 }
2294
2295 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002296 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002297 case IB_QPT_RC:
2298 case IB_QPT_UC:
2299 case IB_QPT_UD:
2300 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002301 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002302 case MLX5_IB_QPT_REG_UMR:
Moni Shouac32a4f22018-01-02 16:19:32 +02002303 case MLX5_IB_QPT_DCI:
Eli Cohene126ba92013-07-07 17:25:49 +03002304 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2305 if (!qp)
2306 return ERR_PTR(-ENOMEM);
2307
2308 err = create_qp_common(dev, pd, init_attr, udata, qp);
2309 if (err) {
2310 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2311 kfree(qp);
2312 return ERR_PTR(err);
2313 }
2314
2315 if (is_qp0(init_attr->qp_type))
2316 qp->ibqp.qp_num = 0;
2317 else if (is_qp1(init_attr->qp_type))
2318 qp->ibqp.qp_num = 1;
2319 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002320 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002321
2322 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 +02002323 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002324 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2325 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002326
majd@mellanox.com19098df2016-01-14 19:13:03 +02002327 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002328
2329 break;
2330
Haggai Erand16e91d2016-02-29 15:45:05 +02002331 case IB_QPT_GSI:
2332 return mlx5_ib_gsi_create_qp(pd, init_attr);
2333
Eli Cohene126ba92013-07-07 17:25:49 +03002334 case IB_QPT_RAW_IPV6:
2335 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002336 case IB_QPT_MAX:
2337 default:
2338 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2339 init_attr->qp_type);
2340 /* Don't support raw QPs */
2341 return ERR_PTR(-EINVAL);
2342 }
2343
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002344 if (verbs_init_attr->qp_type == IB_QPT_DRIVER)
2345 qp->qp_sub_type = init_attr->qp_type;
2346
Eli Cohene126ba92013-07-07 17:25:49 +03002347 return &qp->ibqp;
2348}
2349
Moni Shoua776a3902018-01-02 16:19:33 +02002350static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2351{
2352 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2353
2354 if (mqp->state == IB_QPS_RTR) {
2355 int err;
2356
2357 err = mlx5_core_destroy_dct(dev->mdev, &mqp->dct.mdct);
2358 if (err) {
2359 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2360 return err;
2361 }
2362 }
2363
2364 kfree(mqp->dct.in);
2365 kfree(mqp);
2366 return 0;
2367}
2368
Eli Cohene126ba92013-07-07 17:25:49 +03002369int mlx5_ib_destroy_qp(struct ib_qp *qp)
2370{
2371 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2372 struct mlx5_ib_qp *mqp = to_mqp(qp);
2373
Haggai Erand16e91d2016-02-29 15:45:05 +02002374 if (unlikely(qp->qp_type == IB_QPT_GSI))
2375 return mlx5_ib_gsi_destroy_qp(qp);
2376
Moni Shoua776a3902018-01-02 16:19:33 +02002377 if (mqp->qp_sub_type == MLX5_IB_QPT_DCT)
2378 return mlx5_ib_destroy_dct(mqp);
2379
Eli Cohene126ba92013-07-07 17:25:49 +03002380 destroy_qp_common(dev, mqp);
2381
2382 kfree(mqp);
2383
2384 return 0;
2385}
2386
2387static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2388 int attr_mask)
2389{
2390 u32 hw_access_flags = 0;
2391 u8 dest_rd_atomic;
2392 u32 access_flags;
2393
2394 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2395 dest_rd_atomic = attr->max_dest_rd_atomic;
2396 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002397 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002398
2399 if (attr_mask & IB_QP_ACCESS_FLAGS)
2400 access_flags = attr->qp_access_flags;
2401 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002402 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002403
2404 if (!dest_rd_atomic)
2405 access_flags &= IB_ACCESS_REMOTE_WRITE;
2406
2407 if (access_flags & IB_ACCESS_REMOTE_READ)
2408 hw_access_flags |= MLX5_QP_BIT_RRE;
2409 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2410 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2411 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2412 hw_access_flags |= MLX5_QP_BIT_RWE;
2413
2414 return cpu_to_be32(hw_access_flags);
2415}
2416
2417enum {
2418 MLX5_PATH_FLAG_FL = 1 << 0,
2419 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2420 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2421};
2422
2423static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2424{
2425 if (rate == IB_RATE_PORT_CURRENT) {
2426 return 0;
2427 } else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
2428 return -EINVAL;
2429 } else {
2430 while (rate != IB_RATE_2_5_GBPS &&
2431 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
Saeed Mahameed938fe832015-05-28 22:28:41 +03002432 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
Eli Cohene126ba92013-07-07 17:25:49 +03002433 --rate;
2434 }
2435
2436 return rate + MLX5_STAT_RATE_OFFSET;
2437}
2438
majd@mellanox.com75850d02016-01-14 19:13:06 +02002439static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
2440 struct mlx5_ib_sq *sq, u8 sl)
2441{
2442 void *in;
2443 void *tisc;
2444 int inlen;
2445 int err;
2446
2447 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002448 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002449 if (!in)
2450 return -ENOMEM;
2451
2452 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
2453
2454 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2455 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2456
2457 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2458
2459 kvfree(in);
2460
2461 return err;
2462}
2463
Aviv Heller13eab212016-09-18 20:48:04 +03002464static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
2465 struct mlx5_ib_sq *sq, u8 tx_affinity)
2466{
2467 void *in;
2468 void *tisc;
2469 int inlen;
2470 int err;
2471
2472 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002473 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002474 if (!in)
2475 return -ENOMEM;
2476
2477 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
2478
2479 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2480 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2481
2482 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2483
2484 kvfree(in);
2485
2486 return err;
2487}
2488
majd@mellanox.com75850d02016-01-14 19:13:06 +02002489static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002490 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002491 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002492 u32 path_flags, const struct ib_qp_attr *attr,
2493 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002494{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002495 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002496 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002497 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002498 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2499 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002500
Eli Cohene126ba92013-07-07 17:25:49 +03002501 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002502 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2503 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002504
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002505 if (ah_flags & IB_AH_GRH) {
2506 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002507 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002508 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002509 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002510 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002511 return -EINVAL;
2512 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002513 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002514
2515 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002516 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002517 return -EINVAL;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002518 err = mlx5_get_roce_gid_type(dev, port, grh->sgid_index,
Majd Dibbinyed884512017-01-18 14:10:35 +02002519 &gid_type);
2520 if (err)
2521 return err;
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002522 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02002523 if (qp->ibqp.qp_type == IB_QPT_RC ||
2524 qp->ibqp.qp_type == IB_QPT_UC ||
2525 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
2526 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
2527 path->udp_sport = mlx5_get_roce_udp_sport(dev, port,
2528 grh->sgid_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002529 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Majd Dibbinyed884512017-01-18 14:10:35 +02002530 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002531 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002532 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002533 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2534 path->fl_free_ar |=
2535 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002536 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2537 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2538 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002539 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002540 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002541 }
2542
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002543 if (ah_flags & IB_AH_GRH) {
2544 path->mgid_index = grh->sgid_index;
2545 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002546 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002547 cpu_to_be32((grh->traffic_class << 20) |
2548 (grh->flow_label));
2549 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002550 }
2551
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002552 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002553 if (err < 0)
2554 return err;
2555 path->static_rate = err;
2556 path->port = port;
2557
Eli Cohene126ba92013-07-07 17:25:49 +03002558 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002559 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002560
majd@mellanox.com75850d02016-01-14 19:13:06 +02002561 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2562 return modify_raw_packet_eth_prio(dev->mdev,
2563 &qp->raw_packet_qp.sq,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002564 sl & 0xf);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002565
Eli Cohene126ba92013-07-07 17:25:49 +03002566 return 0;
2567}
2568
2569static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2570 [MLX5_QP_STATE_INIT] = {
2571 [MLX5_QP_STATE_INIT] = {
2572 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2573 MLX5_QP_OPTPAR_RAE |
2574 MLX5_QP_OPTPAR_RWE |
2575 MLX5_QP_OPTPAR_PKEY_INDEX |
2576 MLX5_QP_OPTPAR_PRI_PORT,
2577 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2578 MLX5_QP_OPTPAR_PKEY_INDEX |
2579 MLX5_QP_OPTPAR_PRI_PORT,
2580 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2581 MLX5_QP_OPTPAR_Q_KEY |
2582 MLX5_QP_OPTPAR_PRI_PORT,
2583 },
2584 [MLX5_QP_STATE_RTR] = {
2585 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2586 MLX5_QP_OPTPAR_RRE |
2587 MLX5_QP_OPTPAR_RAE |
2588 MLX5_QP_OPTPAR_RWE |
2589 MLX5_QP_OPTPAR_PKEY_INDEX,
2590 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2591 MLX5_QP_OPTPAR_RWE |
2592 MLX5_QP_OPTPAR_PKEY_INDEX,
2593 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2594 MLX5_QP_OPTPAR_Q_KEY,
2595 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2596 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002597 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2598 MLX5_QP_OPTPAR_RRE |
2599 MLX5_QP_OPTPAR_RAE |
2600 MLX5_QP_OPTPAR_RWE |
2601 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002602 },
2603 },
2604 [MLX5_QP_STATE_RTR] = {
2605 [MLX5_QP_STATE_RTS] = {
2606 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2607 MLX5_QP_OPTPAR_RRE |
2608 MLX5_QP_OPTPAR_RAE |
2609 MLX5_QP_OPTPAR_RWE |
2610 MLX5_QP_OPTPAR_PM_STATE |
2611 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2612 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2613 MLX5_QP_OPTPAR_RWE |
2614 MLX5_QP_OPTPAR_PM_STATE,
2615 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2616 },
2617 },
2618 [MLX5_QP_STATE_RTS] = {
2619 [MLX5_QP_STATE_RTS] = {
2620 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2621 MLX5_QP_OPTPAR_RAE |
2622 MLX5_QP_OPTPAR_RWE |
2623 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002624 MLX5_QP_OPTPAR_PM_STATE |
2625 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002626 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03002627 MLX5_QP_OPTPAR_PM_STATE |
2628 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002629 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
2630 MLX5_QP_OPTPAR_SRQN |
2631 MLX5_QP_OPTPAR_CQN_RCV,
2632 },
2633 },
2634 [MLX5_QP_STATE_SQER] = {
2635 [MLX5_QP_STATE_RTS] = {
2636 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2637 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002638 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002639 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2640 MLX5_QP_OPTPAR_RWE |
2641 MLX5_QP_OPTPAR_RAE |
2642 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002643 },
2644 },
2645};
2646
2647static int ib_nr_to_mlx5_nr(int ib_mask)
2648{
2649 switch (ib_mask) {
2650 case IB_QP_STATE:
2651 return 0;
2652 case IB_QP_CUR_STATE:
2653 return 0;
2654 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2655 return 0;
2656 case IB_QP_ACCESS_FLAGS:
2657 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2658 MLX5_QP_OPTPAR_RAE;
2659 case IB_QP_PKEY_INDEX:
2660 return MLX5_QP_OPTPAR_PKEY_INDEX;
2661 case IB_QP_PORT:
2662 return MLX5_QP_OPTPAR_PRI_PORT;
2663 case IB_QP_QKEY:
2664 return MLX5_QP_OPTPAR_Q_KEY;
2665 case IB_QP_AV:
2666 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2667 MLX5_QP_OPTPAR_PRI_PORT;
2668 case IB_QP_PATH_MTU:
2669 return 0;
2670 case IB_QP_TIMEOUT:
2671 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2672 case IB_QP_RETRY_CNT:
2673 return MLX5_QP_OPTPAR_RETRY_COUNT;
2674 case IB_QP_RNR_RETRY:
2675 return MLX5_QP_OPTPAR_RNR_RETRY;
2676 case IB_QP_RQ_PSN:
2677 return 0;
2678 case IB_QP_MAX_QP_RD_ATOMIC:
2679 return MLX5_QP_OPTPAR_SRA_MAX;
2680 case IB_QP_ALT_PATH:
2681 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2682 case IB_QP_MIN_RNR_TIMER:
2683 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2684 case IB_QP_SQ_PSN:
2685 return 0;
2686 case IB_QP_MAX_DEST_RD_ATOMIC:
2687 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2688 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2689 case IB_QP_PATH_MIG_STATE:
2690 return MLX5_QP_OPTPAR_PM_STATE;
2691 case IB_QP_CAP:
2692 return 0;
2693 case IB_QP_DEST_QPN:
2694 return 0;
2695 }
2696 return 0;
2697}
2698
2699static int ib_mask_to_mlx5_opt(int ib_mask)
2700{
2701 int result = 0;
2702 int i;
2703
2704 for (i = 0; i < 8 * sizeof(int); i++) {
2705 if ((1 << i) & ib_mask)
2706 result |= ib_nr_to_mlx5_nr(1 << i);
2707 }
2708
2709 return result;
2710}
2711
Alex Veskereb49ab02016-08-28 12:25:53 +03002712static int modify_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
2713 struct mlx5_ib_rq *rq, int new_state,
2714 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002715{
2716 void *in;
2717 void *rqc;
2718 int inlen;
2719 int err;
2720
2721 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002722 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002723 if (!in)
2724 return -ENOMEM;
2725
2726 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
2727
2728 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2729 MLX5_SET(rqc, rqc, state, new_state);
2730
Alex Veskereb49ab02016-08-28 12:25:53 +03002731 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2732 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2733 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02002734 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03002735 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2736 } else
2737 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2738 dev->ib_dev.name);
2739 }
2740
2741 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002742 if (err)
2743 goto out;
2744
2745 rq->state = new_state;
2746
2747out:
2748 kvfree(in);
2749 return err;
2750}
2751
2752static int modify_raw_packet_qp_sq(struct mlx5_core_dev *dev,
Bodong Wang7d29f342016-12-01 13:43:16 +02002753 struct mlx5_ib_sq *sq,
2754 int new_state,
2755 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002756{
Bodong Wang7d29f342016-12-01 13:43:16 +02002757 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
2758 u32 old_rate = ibqp->rate_limit;
2759 u32 new_rate = old_rate;
2760 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002761 void *in;
2762 void *sqc;
2763 int inlen;
2764 int err;
2765
2766 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002767 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002768 if (!in)
2769 return -ENOMEM;
2770
2771 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2772
2773 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2774 MLX5_SET(sqc, sqc, state, new_state);
2775
Bodong Wang7d29f342016-12-01 13:43:16 +02002776 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2777 if (new_state != MLX5_SQC_STATE_RDY)
2778 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2779 __func__);
2780 else
2781 new_rate = raw_qp_param->rate_limit;
2782 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002783
Bodong Wang7d29f342016-12-01 13:43:16 +02002784 if (old_rate != new_rate) {
2785 if (new_rate) {
2786 err = mlx5_rl_add_rate(dev, new_rate, &rl_index);
2787 if (err) {
2788 pr_err("Failed configuring rate %u: %d\n",
2789 new_rate, err);
2790 goto out;
2791 }
2792 }
2793
2794 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
2795 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2796 }
2797
2798 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2799 if (err) {
2800 /* Remove new rate from table if failed */
2801 if (new_rate &&
2802 old_rate != new_rate)
2803 mlx5_rl_remove_rate(dev, new_rate);
2804 goto out;
2805 }
2806
2807 /* Only remove the old rate after new rate was set */
2808 if ((old_rate &&
2809 (old_rate != new_rate)) ||
2810 (new_state != MLX5_SQC_STATE_RDY))
2811 mlx5_rl_remove_rate(dev, old_rate);
2812
2813 ibqp->rate_limit = new_rate;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002814 sq->state = new_state;
2815
2816out:
2817 kvfree(in);
2818 return err;
2819}
2820
2821static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002822 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2823 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002824{
2825 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2826 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2827 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002828 int modify_rq = !!qp->rq.wqe_cnt;
2829 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002830 int rq_state;
2831 int sq_state;
2832 int err;
2833
Alex Vesker0680efa2016-08-28 12:25:52 +03002834 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002835 case MLX5_CMD_OP_RST2INIT_QP:
2836 rq_state = MLX5_RQC_STATE_RDY;
2837 sq_state = MLX5_SQC_STATE_RDY;
2838 break;
2839 case MLX5_CMD_OP_2ERR_QP:
2840 rq_state = MLX5_RQC_STATE_ERR;
2841 sq_state = MLX5_SQC_STATE_ERR;
2842 break;
2843 case MLX5_CMD_OP_2RST_QP:
2844 rq_state = MLX5_RQC_STATE_RST;
2845 sq_state = MLX5_SQC_STATE_RST;
2846 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002847 case MLX5_CMD_OP_RTR2RTS_QP:
2848 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002849 if (raw_qp_param->set_mask ==
2850 MLX5_RAW_QP_RATE_LIMIT) {
2851 modify_rq = 0;
2852 sq_state = sq->state;
2853 } else {
2854 return raw_qp_param->set_mask ? -EINVAL : 0;
2855 }
2856 break;
2857 case MLX5_CMD_OP_INIT2INIT_QP:
2858 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002859 if (raw_qp_param->set_mask)
2860 return -EINVAL;
2861 else
2862 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002863 default:
2864 WARN_ON(1);
2865 return -EINVAL;
2866 }
2867
Bodong Wang7d29f342016-12-01 13:43:16 +02002868 if (modify_rq) {
2869 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002870 if (err)
2871 return err;
2872 }
2873
Bodong Wang7d29f342016-12-01 13:43:16 +02002874 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03002875 if (tx_affinity) {
2876 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
2877 tx_affinity);
2878 if (err)
2879 return err;
2880 }
2881
Bodong Wang7d29f342016-12-01 13:43:16 +02002882 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state, raw_qp_param);
Aviv Heller13eab212016-09-18 20:48:04 +03002883 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002884
2885 return 0;
2886}
2887
Eli Cohene126ba92013-07-07 17:25:49 +03002888static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
2889 const struct ib_qp_attr *attr, int attr_mask,
2890 enum ib_qp_state cur_state, enum ib_qp_state new_state)
2891{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002892 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
2893 [MLX5_QP_STATE_RST] = {
2894 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2895 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2896 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
2897 },
2898 [MLX5_QP_STATE_INIT] = {
2899 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2900 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2901 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
2902 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
2903 },
2904 [MLX5_QP_STATE_RTR] = {
2905 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2906 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2907 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
2908 },
2909 [MLX5_QP_STATE_RTS] = {
2910 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2911 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2912 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
2913 },
2914 [MLX5_QP_STATE_SQD] = {
2915 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2916 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2917 },
2918 [MLX5_QP_STATE_SQER] = {
2919 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2920 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2921 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
2922 },
2923 [MLX5_QP_STATE_ERR] = {
2924 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2925 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2926 }
2927 };
2928
Eli Cohene126ba92013-07-07 17:25:49 +03002929 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2930 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02002931 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03002932 struct mlx5_ib_cq *send_cq, *recv_cq;
2933 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03002934 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03002935 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002936 enum mlx5_qp_state mlx5_cur, mlx5_new;
2937 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03002938 int mlx5_st;
2939 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002940 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03002941 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002942
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002943 context = kzalloc(sizeof(*context), GFP_KERNEL);
2944 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03002945 return -ENOMEM;
2946
Moni Shouac32a4f22018-01-02 16:19:32 +02002947 err = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
2948 qp->qp_sub_type : ibqp->qp_type);
Haggai Eran158abf82016-02-29 15:45:04 +02002949 if (err < 0) {
2950 mlx5_ib_dbg(dev, "unsupported qp type %d\n", ibqp->qp_type);
Eli Cohene126ba92013-07-07 17:25:49 +03002951 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002952 }
Eli Cohene126ba92013-07-07 17:25:49 +03002953
2954 context->flags = cpu_to_be32(err << 16);
2955
2956 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
2957 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2958 } else {
2959 switch (attr->path_mig_state) {
2960 case IB_MIG_MIGRATED:
2961 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2962 break;
2963 case IB_MIG_REARM:
2964 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
2965 break;
2966 case IB_MIG_ARMED:
2967 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
2968 break;
2969 }
2970 }
2971
Aviv Heller13eab212016-09-18 20:48:04 +03002972 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2973 if ((ibqp->qp_type == IB_QPT_RC) ||
2974 (ibqp->qp_type == IB_QPT_UD &&
2975 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
2976 (ibqp->qp_type == IB_QPT_UC) ||
2977 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
2978 (ibqp->qp_type == IB_QPT_XRC_INI) ||
2979 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
2980 if (mlx5_lag_is_active(dev->mdev)) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02002981 u8 p = mlx5_core_native_port_num(dev->mdev);
Aviv Heller13eab212016-09-18 20:48:04 +03002982 tx_affinity = (unsigned int)atomic_add_return(1,
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02002983 &dev->roce[p].next_port) %
Aviv Heller13eab212016-09-18 20:48:04 +03002984 MLX5_MAX_PORTS + 1;
2985 context->flags |= cpu_to_be32(tx_affinity << 24);
2986 }
2987 }
2988 }
2989
Haggai Erand16e91d2016-02-29 15:45:05 +02002990 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002991 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002992 } else if ((ibqp->qp_type == IB_QPT_UD &&
2993 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03002994 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
2995 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
2996 } else if (attr_mask & IB_QP_PATH_MTU) {
2997 if (attr->path_mtu < IB_MTU_256 ||
2998 attr->path_mtu > IB_MTU_4096) {
2999 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
3000 err = -EINVAL;
3001 goto out;
3002 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003003 context->mtu_msgmax = (attr->path_mtu << 5) |
3004 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03003005 }
3006
3007 if (attr_mask & IB_QP_DEST_QPN)
3008 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
3009
3010 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03003011 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003012
3013 /* todo implement counter_index functionality */
3014
3015 if (is_sqp(ibqp->qp_type))
3016 context->pri_path.port = qp->port;
3017
3018 if (attr_mask & IB_QP_PORT)
3019 context->pri_path.port = attr->port_num;
3020
3021 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003022 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003023 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003024 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003025 if (err)
3026 goto out;
3027 }
3028
3029 if (attr_mask & IB_QP_TIMEOUT)
3030 context->pri_path.ackto_lt |= attr->timeout << 3;
3031
3032 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003033 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3034 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003035 attr->alt_port_num,
3036 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3037 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003038 if (err)
3039 goto out;
3040 }
3041
3042 pd = get_pd(qp);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003043 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3044 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003045
3046 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3047 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3048 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3049 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3050
3051 if (attr_mask & IB_QP_RNR_RETRY)
3052 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3053
3054 if (attr_mask & IB_QP_RETRY_CNT)
3055 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3056
3057 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3058 if (attr->max_rd_atomic)
3059 context->params1 |=
3060 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3061 }
3062
3063 if (attr_mask & IB_QP_SQ_PSN)
3064 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3065
3066 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3067 if (attr->max_dest_rd_atomic)
3068 context->params2 |=
3069 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3070 }
3071
3072 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
3073 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
3074
3075 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3076 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3077
3078 if (attr_mask & IB_QP_RQ_PSN)
3079 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3080
3081 if (attr_mask & IB_QP_QKEY)
3082 context->qkey = cpu_to_be32(attr->qkey);
3083
3084 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3085 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3086
Mark Bloch0837e862016-06-17 15:10:55 +03003087 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3088 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3089 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003090
3091 /* Underlay port should be used - index 0 function per port */
3092 if (qp->flags & MLX5_IB_QP_UNDERLAY)
3093 port_num = 0;
3094
Alex Veskereb49ab02016-08-28 12:25:53 +03003095 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03003096 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03003097 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003098 }
3099
Eli Cohene126ba92013-07-07 17:25:49 +03003100 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3101 context->sq_crq_size |= cpu_to_be16(1 << 4);
3102
Haggai Eranb11a4f92016-02-29 15:45:03 +02003103 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
3104 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003105
3106 mlx5_cur = to_mlx5_state(cur_state);
3107 mlx5_new = to_mlx5_state(new_state);
Moni Shouac32a4f22018-01-02 16:19:32 +02003108 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
3109 qp->qp_sub_type : ibqp->qp_type);
Eli Cohen07c91132013-10-24 12:01:01 +03003110 if (mlx5_st < 0)
Eli Cohene126ba92013-07-07 17:25:49 +03003111 goto out;
3112
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003113 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
3114 !optab[mlx5_cur][mlx5_new])
3115 goto out;
3116
3117 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003118 optpar = ib_mask_to_mlx5_opt(attr_mask);
3119 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003120
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003121 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3122 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003123 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3124
3125 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003126 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03003127 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003128 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3129 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003130
3131 if (attr_mask & IB_QP_RATE_LIMIT) {
3132 raw_qp_param.rate_limit = attr->rate_limit;
3133 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3134 }
3135
Aviv Heller13eab212016-09-18 20:48:04 +03003136 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003137 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003138 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003139 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003140 }
3141
Eli Cohene126ba92013-07-07 17:25:49 +03003142 if (err)
3143 goto out;
3144
3145 qp->state = new_state;
3146
3147 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003148 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003149 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003150 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003151 if (attr_mask & IB_QP_PORT)
3152 qp->port = attr->port_num;
3153 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003154 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003155
3156 /*
3157 * If we moved a kernel QP to RESET, clean up all old CQ
3158 * entries and reinitialize the QP.
3159 */
3160 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003161 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003162 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3163 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003164 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003165
3166 qp->rq.head = 0;
3167 qp->rq.tail = 0;
3168 qp->sq.head = 0;
3169 qp->sq.tail = 0;
3170 qp->sq.cur_post = 0;
3171 qp->sq.last_poll = 0;
3172 qp->db.db[MLX5_RCV_DBR] = 0;
3173 qp->db.db[MLX5_SND_DBR] = 0;
3174 }
3175
3176out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003177 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003178 return err;
3179}
3180
Moni Shouac32a4f22018-01-02 16:19:32 +02003181static inline bool is_valid_mask(int mask, int req, int opt)
3182{
3183 if ((mask & req) != req)
3184 return false;
3185
3186 if (mask & ~(req | opt))
3187 return false;
3188
3189 return true;
3190}
3191
3192/* check valid transition for driver QP types
3193 * for now the only QP type that this function supports is DCI
3194 */
3195static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3196 enum ib_qp_attr_mask attr_mask)
3197{
3198 int req = IB_QP_STATE;
3199 int opt = 0;
3200
3201 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3202 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
3203 return is_valid_mask(attr_mask, req, opt);
3204 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3205 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3206 return is_valid_mask(attr_mask, req, opt);
3207 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3208 req |= IB_QP_PATH_MTU;
3209 opt = IB_QP_PKEY_INDEX;
3210 return is_valid_mask(attr_mask, req, opt);
3211 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3212 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3213 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3214 opt = IB_QP_MIN_RNR_TIMER;
3215 return is_valid_mask(attr_mask, req, opt);
3216 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3217 opt = IB_QP_MIN_RNR_TIMER;
3218 return is_valid_mask(attr_mask, req, opt);
3219 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3220 return is_valid_mask(attr_mask, req, opt);
3221 }
3222 return false;
3223}
3224
Moni Shoua776a3902018-01-02 16:19:33 +02003225/* mlx5_ib_modify_dct: modify a DCT QP
3226 * valid transitions are:
3227 * RESET to INIT: must set access_flags, pkey_index and port
3228 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3229 * mtu, gid_index and hop_limit
3230 * Other transitions and attributes are illegal
3231 */
3232static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3233 int attr_mask, struct ib_udata *udata)
3234{
3235 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3236 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3237 enum ib_qp_state cur_state, new_state;
3238 int err = 0;
3239 int required = IB_QP_STATE;
3240 void *dctc;
3241
3242 if (!(attr_mask & IB_QP_STATE))
3243 return -EINVAL;
3244
3245 cur_state = qp->state;
3246 new_state = attr->qp_state;
3247
3248 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3249 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3250 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3251 if (!is_valid_mask(attr_mask, required, 0))
3252 return -EINVAL;
3253
3254 if (attr->port_num == 0 ||
3255 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3256 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3257 attr->port_num, dev->num_ports);
3258 return -EINVAL;
3259 }
3260 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3261 MLX5_SET(dctc, dctc, rre, 1);
3262 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3263 MLX5_SET(dctc, dctc, rwe, 1);
3264 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
3265 if (!mlx5_ib_dc_atomic_is_supported(dev))
3266 return -EOPNOTSUPP;
3267 MLX5_SET(dctc, dctc, rae, 1);
3268 MLX5_SET(dctc, dctc, atomic_mode, MLX5_ATOMIC_MODE_DCT_CX);
3269 }
3270 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3271 MLX5_SET(dctc, dctc, port, attr->port_num);
3272 MLX5_SET(dctc, dctc, counter_set_id, dev->port[attr->port_num - 1].cnts.set_id);
3273
3274 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3275 struct mlx5_ib_modify_qp_resp resp = {};
3276 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3277 sizeof(resp.dctn);
3278
3279 if (udata->outlen < min_resp_len)
3280 return -EINVAL;
3281 resp.response_length = min_resp_len;
3282
3283 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3284 if (!is_valid_mask(attr_mask, required, 0))
3285 return -EINVAL;
3286 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3287 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3288 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3289 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3290 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3291 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3292
3293 err = mlx5_core_create_dct(dev->mdev, &qp->dct.mdct, qp->dct.in,
3294 MLX5_ST_SZ_BYTES(create_dct_in));
3295 if (err)
3296 return err;
3297 resp.dctn = qp->dct.mdct.mqp.qpn;
3298 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3299 if (err) {
3300 mlx5_core_destroy_dct(dev->mdev, &qp->dct.mdct);
3301 return err;
3302 }
3303 } else {
3304 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3305 return -EINVAL;
3306 }
3307 if (err)
3308 qp->state = IB_QPS_ERR;
3309 else
3310 qp->state = new_state;
3311 return err;
3312}
3313
Eli Cohene126ba92013-07-07 17:25:49 +03003314int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3315 int attr_mask, struct ib_udata *udata)
3316{
3317 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3318 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Haggai Erand16e91d2016-02-29 15:45:05 +02003319 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003320 enum ib_qp_state cur_state, new_state;
3321 int err = -EINVAL;
3322 int port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003323 enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
Eli Cohene126ba92013-07-07 17:25:49 +03003324
Yishai Hadas28d61372016-05-23 15:20:56 +03003325 if (ibqp->rwq_ind_tbl)
3326 return -ENOSYS;
3327
Haggai Erand16e91d2016-02-29 15:45:05 +02003328 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3329 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3330
Moni Shouac32a4f22018-01-02 16:19:32 +02003331 if (ibqp->qp_type == IB_QPT_DRIVER)
3332 qp_type = qp->qp_sub_type;
3333 else
3334 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3335 IB_QPT_GSI : ibqp->qp_type;
3336
Moni Shoua776a3902018-01-02 16:19:33 +02003337 if (qp_type == MLX5_IB_QPT_DCT)
3338 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02003339
Eli Cohene126ba92013-07-07 17:25:49 +03003340 mutex_lock(&qp->mutex);
3341
3342 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
3343 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
3344
Achiad Shochat2811ba52015-12-23 18:47:24 +02003345 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
3346 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
3347 ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
3348 }
3349
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003350 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
3351 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
3352 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
3353 attr_mask);
3354 goto out;
3355 }
3356 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02003357 qp_type != MLX5_IB_QPT_DCI &&
3358 !ib_modify_qp_is_ok(cur_state, new_state, qp_type, attr_mask, ll)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003359 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3360 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003361 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02003362 } else if (qp_type == MLX5_IB_QPT_DCI &&
3363 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
3364 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3365 cur_state, new_state, qp_type, attr_mask);
3366 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003367 }
Eli Cohene126ba92013-07-07 17:25:49 +03003368
3369 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003370 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003371 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003372 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3373 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003374 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003375 }
Eli Cohene126ba92013-07-07 17:25:49 +03003376
3377 if (attr_mask & IB_QP_PKEY_INDEX) {
3378 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003379 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02003380 dev->mdev->port_caps[port - 1].pkey_table_len) {
3381 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
3382 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003383 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003384 }
Eli Cohene126ba92013-07-07 17:25:49 +03003385 }
3386
3387 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003388 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003389 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
3390 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
3391 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003392 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003393 }
Eli Cohene126ba92013-07-07 17:25:49 +03003394
3395 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003396 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003397 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
3398 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
3399 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003400 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003401 }
Eli Cohene126ba92013-07-07 17:25:49 +03003402
3403 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
3404 err = 0;
3405 goto out;
3406 }
3407
3408 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
3409
3410out:
3411 mutex_unlock(&qp->mutex);
3412 return err;
3413}
3414
3415static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
3416{
3417 struct mlx5_ib_cq *cq;
3418 unsigned cur;
3419
3420 cur = wq->head - wq->tail;
3421 if (likely(cur + nreq < wq->max_post))
3422 return 0;
3423
3424 cq = to_mcq(ib_cq);
3425 spin_lock(&cq->lock);
3426 cur = wq->head - wq->tail;
3427 spin_unlock(&cq->lock);
3428
3429 return cur + nreq >= wq->max_post;
3430}
3431
3432static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
3433 u64 remote_addr, u32 rkey)
3434{
3435 rseg->raddr = cpu_to_be64(remote_addr);
3436 rseg->rkey = cpu_to_be32(rkey);
3437 rseg->reserved = 0;
3438}
3439
Erez Shitritf0313962016-02-21 16:27:17 +02003440static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
3441 struct ib_send_wr *wr, void *qend,
3442 struct mlx5_ib_qp *qp, int *size)
3443{
3444 void *seg = eseg;
3445
3446 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
3447
3448 if (wr->send_flags & IB_SEND_IP_CSUM)
3449 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
3450 MLX5_ETH_WQE_L4_CSUM;
3451
3452 seg += sizeof(struct mlx5_wqe_eth_seg);
3453 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3454
3455 if (wr->opcode == IB_WR_LSO) {
3456 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003457 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr.start);
Erez Shitritf0313962016-02-21 16:27:17 +02003458 u64 left, leftlen, copysz;
3459 void *pdata = ud_wr->header;
3460
3461 left = ud_wr->hlen;
3462 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003463 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02003464
3465 /*
3466 * check if there is space till the end of queue, if yes,
3467 * copy all in one shot, otherwise copy till the end of queue,
3468 * rollback and than the copy the left
3469 */
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003470 leftlen = qend - (void *)eseg->inline_hdr.start;
Erez Shitritf0313962016-02-21 16:27:17 +02003471 copysz = min_t(u64, leftlen, left);
3472
3473 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3474
3475 if (likely(copysz > size_of_inl_hdr_start)) {
3476 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3477 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3478 }
3479
3480 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3481 seg = mlx5_get_send_wqe(qp, 0);
3482 left -= copysz;
3483 pdata += copysz;
3484 memcpy(seg, pdata, left);
3485 seg += ALIGN(left, 16);
3486 *size += ALIGN(left, 16) / 16;
3487 }
3488 }
3489
3490 return seg;
3491}
3492
Eli Cohene126ba92013-07-07 17:25:49 +03003493static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
3494 struct ib_send_wr *wr)
3495{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003496 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3497 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3498 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003499}
3500
3501static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3502{
3503 dseg->byte_count = cpu_to_be32(sg->length);
3504 dseg->lkey = cpu_to_be32(sg->lkey);
3505 dseg->addr = cpu_to_be64(sg->addr);
3506}
3507
Artemy Kovalyov31616252017-01-02 11:37:42 +02003508static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03003509{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003510 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
3511 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03003512}
3513
3514static __be64 frwr_mkey_mask(void)
3515{
3516 u64 result;
3517
3518 result = MLX5_MKEY_MASK_LEN |
3519 MLX5_MKEY_MASK_PAGE_SIZE |
3520 MLX5_MKEY_MASK_START_ADDR |
3521 MLX5_MKEY_MASK_EN_RINVAL |
3522 MLX5_MKEY_MASK_KEY |
3523 MLX5_MKEY_MASK_LR |
3524 MLX5_MKEY_MASK_LW |
3525 MLX5_MKEY_MASK_RR |
3526 MLX5_MKEY_MASK_RW |
3527 MLX5_MKEY_MASK_A |
3528 MLX5_MKEY_MASK_SMALL_FENCE |
3529 MLX5_MKEY_MASK_FREE;
3530
3531 return cpu_to_be64(result);
3532}
3533
Sagi Grimberge6631812014-02-23 14:19:11 +02003534static __be64 sig_mkey_mask(void)
3535{
3536 u64 result;
3537
3538 result = MLX5_MKEY_MASK_LEN |
3539 MLX5_MKEY_MASK_PAGE_SIZE |
3540 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003541 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003542 MLX5_MKEY_MASK_EN_RINVAL |
3543 MLX5_MKEY_MASK_KEY |
3544 MLX5_MKEY_MASK_LR |
3545 MLX5_MKEY_MASK_LW |
3546 MLX5_MKEY_MASK_RR |
3547 MLX5_MKEY_MASK_RW |
3548 MLX5_MKEY_MASK_SMALL_FENCE |
3549 MLX5_MKEY_MASK_FREE |
3550 MLX5_MKEY_MASK_BSF_EN;
3551
3552 return cpu_to_be64(result);
3553}
3554
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003555static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003556 struct mlx5_ib_mr *mr)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003557{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003558 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003559
3560 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003561
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003562 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003563 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003564 umr->mkey_mask = frwr_mkey_mask();
3565}
3566
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003567static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003568{
3569 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003570 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003571 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003572}
3573
Artemy Kovalyov31616252017-01-02 11:37:42 +02003574static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003575{
3576 u64 result;
3577
Artemy Kovalyov31616252017-01-02 11:37:42 +02003578 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02003579 MLX5_MKEY_MASK_FREE;
3580
3581 return cpu_to_be64(result);
3582}
3583
Artemy Kovalyov31616252017-01-02 11:37:42 +02003584static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003585{
3586 u64 result;
3587
3588 result = MLX5_MKEY_MASK_FREE;
3589
3590 return cpu_to_be64(result);
3591}
3592
Noa Osherovich56e11d62016-02-29 16:46:51 +02003593static __be64 get_umr_update_translation_mask(void)
3594{
3595 u64 result;
3596
3597 result = MLX5_MKEY_MASK_LEN |
3598 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003599 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003600
3601 return cpu_to_be64(result);
3602}
3603
Artemy Kovalyov31616252017-01-02 11:37:42 +02003604static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02003605{
3606 u64 result;
3607
Artemy Kovalyov31616252017-01-02 11:37:42 +02003608 result = MLX5_MKEY_MASK_LR |
3609 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003610 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003611 MLX5_MKEY_MASK_RW;
3612
3613 if (atomic)
3614 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003615
3616 return cpu_to_be64(result);
3617}
3618
3619static __be64 get_umr_update_pd_mask(void)
3620{
3621 u64 result;
3622
Artemy Kovalyov31616252017-01-02 11:37:42 +02003623 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003624
3625 return cpu_to_be64(result);
3626}
3627
Eli Cohene126ba92013-07-07 17:25:49 +03003628static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Maor Gottlieb578e7262016-10-27 16:36:37 +03003629 struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003630{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003631 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003632
3633 memset(umr, 0, sizeof(*umr));
3634
Haggai Eran968e78d2014-12-11 17:04:11 +02003635 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3636 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3637 else
3638 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3639
Artemy Kovalyov31616252017-01-02 11:37:42 +02003640 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
3641 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
3642 u64 offset = get_xlt_octo(umrwr->offset);
3643
3644 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
3645 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
3646 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003647 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02003648 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3649 umr->mkey_mask |= get_umr_update_translation_mask();
3650 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
3651 umr->mkey_mask |= get_umr_update_access_mask(atomic);
3652 umr->mkey_mask |= get_umr_update_pd_mask();
3653 }
3654 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
3655 umr->mkey_mask |= get_umr_enable_mr_mask();
3656 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
3657 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003658
3659 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003660 umr->flags |= MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003661}
3662
3663static u8 get_umr_flags(int acc)
3664{
3665 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3666 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3667 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3668 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003669 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003670}
3671
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003672static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3673 struct mlx5_ib_mr *mr,
3674 u32 key, int access)
3675{
3676 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3677
3678 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003679
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003680 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003681 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003682 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003683 /* KLMs take twice the size of MTTs */
3684 ndescs *= 2;
3685
3686 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003687 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3688 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3689 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3690 seg->len = cpu_to_be64(mr->ibmr.length);
3691 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003692}
3693
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003694static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003695{
3696 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003697 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003698}
3699
3700static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
3701{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003702 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003703
Eli Cohene126ba92013-07-07 17:25:49 +03003704 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02003705 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02003706 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003707
Haggai Eran968e78d2014-12-11 17:04:11 +02003708 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003709 if (umrwr->pd)
3710 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
3711 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
3712 !umrwr->length)
3713 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
3714
3715 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003716 seg->len = cpu_to_be64(umrwr->length);
3717 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003718 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003719 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003720}
3721
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003722static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3723 struct mlx5_ib_mr *mr,
3724 struct mlx5_ib_pd *pd)
3725{
3726 int bcount = mr->desc_size * mr->ndescs;
3727
3728 dseg->addr = cpu_to_be64(mr->desc_map);
3729 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3730 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3731}
3732
Eli Cohene126ba92013-07-07 17:25:49 +03003733static __be32 send_ieth(struct ib_send_wr *wr)
3734{
3735 switch (wr->opcode) {
3736 case IB_WR_SEND_WITH_IMM:
3737 case IB_WR_RDMA_WRITE_WITH_IMM:
3738 return wr->ex.imm_data;
3739
3740 case IB_WR_SEND_WITH_INV:
3741 return cpu_to_be32(wr->ex.invalidate_rkey);
3742
3743 default:
3744 return 0;
3745 }
3746}
3747
3748static u8 calc_sig(void *wqe, int size)
3749{
3750 u8 *p = wqe;
3751 u8 res = 0;
3752 int i;
3753
3754 for (i = 0; i < size; i++)
3755 res ^= p[i];
3756
3757 return ~res;
3758}
3759
3760static u8 wq_sig(void *wqe)
3761{
3762 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
3763}
3764
3765static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
3766 void *wqe, int *sz)
3767{
3768 struct mlx5_wqe_inline_seg *seg;
3769 void *qend = qp->sq.qend;
3770 void *addr;
3771 int inl = 0;
3772 int copy;
3773 int len;
3774 int i;
3775
3776 seg = wqe;
3777 wqe += sizeof(*seg);
3778 for (i = 0; i < wr->num_sge; i++) {
3779 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
3780 len = wr->sg_list[i].length;
3781 inl += len;
3782
3783 if (unlikely(inl > qp->max_inline_data))
3784 return -ENOMEM;
3785
3786 if (unlikely(wqe + len > qend)) {
3787 copy = qend - wqe;
3788 memcpy(wqe, addr, copy);
3789 addr += copy;
3790 len -= copy;
3791 wqe = mlx5_get_send_wqe(qp, 0);
3792 }
3793 memcpy(wqe, addr, len);
3794 wqe += len;
3795 }
3796
3797 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
3798
3799 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
3800
3801 return 0;
3802}
3803
Sagi Grimberge6631812014-02-23 14:19:11 +02003804static u16 prot_field_size(enum ib_signature_type type)
3805{
3806 switch (type) {
3807 case IB_SIG_TYPE_T10_DIF:
3808 return MLX5_DIF_SIZE;
3809 default:
3810 return 0;
3811 }
3812}
3813
3814static u8 bs_selector(int block_size)
3815{
3816 switch (block_size) {
3817 case 512: return 0x1;
3818 case 520: return 0x2;
3819 case 4096: return 0x3;
3820 case 4160: return 0x4;
3821 case 1073741824: return 0x5;
3822 default: return 0;
3823 }
3824}
3825
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003826static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
3827 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02003828{
Sagi Grimberg142537f2014-08-13 19:54:32 +03003829 /* Valid inline section and allow BSF refresh */
3830 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
3831 MLX5_BSF_REFRESH_DIF);
3832 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
3833 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003834 /* repeating block */
3835 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
3836 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
3837 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003838
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003839 if (domain->sig.dif.ref_remap)
3840 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02003841
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003842 if (domain->sig.dif.app_escape) {
3843 if (domain->sig.dif.ref_escape)
3844 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
3845 else
3846 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02003847 }
3848
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003849 inl->dif_app_bitmask_check =
3850 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02003851}
3852
3853static int mlx5_set_bsf(struct ib_mr *sig_mr,
3854 struct ib_sig_attrs *sig_attrs,
3855 struct mlx5_bsf *bsf, u32 data_size)
3856{
3857 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
3858 struct mlx5_bsf_basic *basic = &bsf->basic;
3859 struct ib_sig_domain *mem = &sig_attrs->mem;
3860 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02003861
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003862 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02003863
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003864 /* Basic + Extended + Inline */
3865 basic->bsf_size_sbs = 1 << 7;
3866 /* Input domain check byte mask */
3867 basic->check_byte_mask = sig_attrs->check_mask;
3868 basic->raw_data_size = cpu_to_be32(data_size);
3869
3870 /* Memory domain */
3871 switch (sig_attrs->mem.sig_type) {
3872 case IB_SIG_TYPE_NONE:
3873 break;
3874 case IB_SIG_TYPE_T10_DIF:
3875 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
3876 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
3877 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
3878 break;
3879 default:
3880 return -EINVAL;
3881 }
3882
3883 /* Wire domain */
3884 switch (sig_attrs->wire.sig_type) {
3885 case IB_SIG_TYPE_NONE:
3886 break;
3887 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02003888 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003889 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003890 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03003891 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02003892 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003893 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003894 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003895 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003896 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003897 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02003898 } else
3899 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
3900
Sagi Grimberg142537f2014-08-13 19:54:32 +03003901 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003902 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02003903 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003904 default:
3905 return -EINVAL;
3906 }
3907
3908 return 0;
3909}
3910
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003911static int set_sig_data_segment(struct ib_sig_handover_wr *wr,
3912 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003913{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003914 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
3915 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003916 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003917 u32 data_len = wr->wr.sg_list->length;
3918 u32 data_key = wr->wr.sg_list->lkey;
3919 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003920 int ret;
3921 int wqe_size;
3922
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003923 if (!wr->prot ||
3924 (data_key == wr->prot->lkey &&
3925 data_va == wr->prot->addr &&
3926 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003927 /**
3928 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003929 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02003930 * So need construct:
3931 * ------------------
3932 * | data_klm |
3933 * ------------------
3934 * | BSF |
3935 * ------------------
3936 **/
3937 struct mlx5_klm *data_klm = *seg;
3938
3939 data_klm->bcount = cpu_to_be32(data_len);
3940 data_klm->key = cpu_to_be32(data_key);
3941 data_klm->va = cpu_to_be64(data_va);
3942 wqe_size = ALIGN(sizeof(*data_klm), 64);
3943 } else {
3944 /**
3945 * Source domain contains signature information
3946 * So need construct a strided block format:
3947 * ---------------------------
3948 * | stride_block_ctrl |
3949 * ---------------------------
3950 * | data_klm |
3951 * ---------------------------
3952 * | prot_klm |
3953 * ---------------------------
3954 * | BSF |
3955 * ---------------------------
3956 **/
3957 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
3958 struct mlx5_stride_block_entry *data_sentry;
3959 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003960 u32 prot_key = wr->prot->lkey;
3961 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003962 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
3963 int prot_size;
3964
3965 sblock_ctrl = *seg;
3966 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
3967 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
3968
3969 prot_size = prot_field_size(sig_attrs->mem.sig_type);
3970 if (!prot_size) {
3971 pr_err("Bad block size given: %u\n", block_size);
3972 return -EINVAL;
3973 }
3974 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
3975 prot_size);
3976 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
3977 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
3978 sblock_ctrl->num_entries = cpu_to_be16(2);
3979
3980 data_sentry->bcount = cpu_to_be16(block_size);
3981 data_sentry->key = cpu_to_be32(data_key);
3982 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003983 data_sentry->stride = cpu_to_be16(block_size);
3984
Sagi Grimberge6631812014-02-23 14:19:11 +02003985 prot_sentry->bcount = cpu_to_be16(prot_size);
3986 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003987 prot_sentry->va = cpu_to_be64(prot_va);
3988 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003989
Sagi Grimberge6631812014-02-23 14:19:11 +02003990 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
3991 sizeof(*prot_sentry), 64);
3992 }
3993
3994 *seg += wqe_size;
3995 *size += wqe_size / 16;
3996 if (unlikely((*seg == qp->sq.qend)))
3997 *seg = mlx5_get_send_wqe(qp, 0);
3998
3999 bsf = *seg;
4000 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
4001 if (ret)
4002 return -EINVAL;
4003
4004 *seg += sizeof(*bsf);
4005 *size += sizeof(*bsf) / 16;
4006 if (unlikely((*seg == qp->sq.qend)))
4007 *seg = mlx5_get_send_wqe(qp, 0);
4008
4009 return 0;
4010}
4011
4012static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004013 struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02004014 u32 length, u32 pdn)
4015{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004016 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004017 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004018 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004019
4020 memset(seg, 0, sizeof(*seg));
4021
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004022 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004023 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004024 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004025 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004026 MLX5_MKEY_BSF_EN | pdn);
4027 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004028 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004029 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4030}
4031
4032static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004033 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004034{
4035 memset(umr, 0, sizeof(*umr));
4036
4037 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004038 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004039 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4040 umr->mkey_mask = sig_mkey_mask();
4041}
4042
4043
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004044static int set_sig_umr_wr(struct ib_send_wr *send_wr, struct mlx5_ib_qp *qp,
Sagi Grimberge6631812014-02-23 14:19:11 +02004045 void **seg, int *size)
4046{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004047 struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
4048 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004049 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004050 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02004051 int region_len, ret;
4052
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004053 if (unlikely(wr->wr.num_sge != 1) ||
4054 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004055 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
4056 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02004057 return -EINVAL;
4058
4059 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004060 region_len = wr->wr.sg_list->length;
4061 if (wr->prot &&
4062 (wr->prot->lkey != wr->wr.sg_list->lkey ||
4063 wr->prot->addr != wr->wr.sg_list->addr ||
4064 wr->prot->length != wr->wr.sg_list->length))
4065 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02004066
4067 /**
4068 * KLM octoword size - if protection was provided
4069 * then we use strided block format (3 octowords),
4070 * else we use single KLM (1 octoword)
4071 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02004072 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02004073
Artemy Kovalyov31616252017-01-02 11:37:42 +02004074 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004075 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4076 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4077 if (unlikely((*seg == qp->sq.qend)))
4078 *seg = mlx5_get_send_wqe(qp, 0);
4079
Artemy Kovalyov31616252017-01-02 11:37:42 +02004080 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02004081 *seg += sizeof(struct mlx5_mkey_seg);
4082 *size += sizeof(struct mlx5_mkey_seg) / 16;
4083 if (unlikely((*seg == qp->sq.qend)))
4084 *seg = mlx5_get_send_wqe(qp, 0);
4085
4086 ret = set_sig_data_segment(wr, qp, seg, size);
4087 if (ret)
4088 return ret;
4089
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004090 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004091 return 0;
4092}
4093
4094static int set_psv_wr(struct ib_sig_domain *domain,
4095 u32 psv_idx, void **seg, int *size)
4096{
4097 struct mlx5_seg_set_psv *psv_seg = *seg;
4098
4099 memset(psv_seg, 0, sizeof(*psv_seg));
4100 psv_seg->psv_num = cpu_to_be32(psv_idx);
4101 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004102 case IB_SIG_TYPE_NONE:
4103 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004104 case IB_SIG_TYPE_T10_DIF:
4105 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4106 domain->sig.dif.app_tag);
4107 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004108 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004109 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004110 pr_err("Bad signature type (%d) is given.\n",
4111 domain->sig_type);
4112 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004113 }
4114
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004115 *seg += sizeof(*psv_seg);
4116 *size += sizeof(*psv_seg) / 16;
4117
Sagi Grimberge6631812014-02-23 14:19:11 +02004118 return 0;
4119}
4120
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004121static int set_reg_wr(struct mlx5_ib_qp *qp,
4122 struct ib_reg_wr *wr,
4123 void **seg, int *size)
4124{
4125 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4126 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
4127
4128 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4129 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4130 "Invalid IB_SEND_INLINE send flag\n");
4131 return -EINVAL;
4132 }
4133
4134 set_reg_umr_seg(*seg, mr);
4135 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4136 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4137 if (unlikely((*seg == qp->sq.qend)))
4138 *seg = mlx5_get_send_wqe(qp, 0);
4139
4140 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4141 *seg += sizeof(struct mlx5_mkey_seg);
4142 *size += sizeof(struct mlx5_mkey_seg) / 16;
4143 if (unlikely((*seg == qp->sq.qend)))
4144 *seg = mlx5_get_send_wqe(qp, 0);
4145
4146 set_reg_data_seg(*seg, mr, pd);
4147 *seg += sizeof(struct mlx5_wqe_data_seg);
4148 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4149
4150 return 0;
4151}
4152
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004153static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
Eli Cohene126ba92013-07-07 17:25:49 +03004154{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004155 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004156 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4157 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4158 if (unlikely((*seg == qp->sq.qend)))
4159 *seg = mlx5_get_send_wqe(qp, 0);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004160 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004161 *seg += sizeof(struct mlx5_mkey_seg);
4162 *size += sizeof(struct mlx5_mkey_seg) / 16;
4163 if (unlikely((*seg == qp->sq.qend)))
4164 *seg = mlx5_get_send_wqe(qp, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004165}
4166
4167static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
4168{
4169 __be32 *p = NULL;
4170 int tidx = idx;
4171 int i, j;
4172
4173 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
4174 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4175 if ((i & 0xf) == 0) {
4176 void *buf = mlx5_get_send_wqe(qp, tidx);
4177 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
4178 p = buf;
4179 j = 0;
4180 }
4181 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4182 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4183 be32_to_cpu(p[j + 3]));
4184 }
4185}
4186
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004187static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4188 struct mlx5_wqe_ctrl_seg **ctrl,
Eli Cohen6a4f1392014-12-02 12:26:18 +02004189 struct ib_send_wr *wr, unsigned *idx,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004190 int *size, int nreq)
4191{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004192 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4193 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004194
4195 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
4196 *seg = mlx5_get_send_wqe(qp, *idx);
4197 *ctrl = *seg;
4198 *(uint32_t *)(*seg + 8) = 0;
4199 (*ctrl)->imm = send_ieth(wr);
4200 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
4201 (wr->send_flags & IB_SEND_SIGNALED ?
4202 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4203 (wr->send_flags & IB_SEND_SOLICITED ?
4204 MLX5_WQE_CTRL_SOLICITED : 0);
4205
4206 *seg += sizeof(**ctrl);
4207 *size = sizeof(**ctrl) / 16;
4208
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004209 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004210}
4211
4212static void finish_wqe(struct mlx5_ib_qp *qp,
4213 struct mlx5_wqe_ctrl_seg *ctrl,
4214 u8 size, unsigned idx, u64 wr_id,
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004215 int nreq, u8 fence, u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004216{
4217 u8 opmod = 0;
4218
4219 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
4220 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02004221 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004222 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004223 if (unlikely(qp->wq_sig))
4224 ctrl->signature = wq_sig(ctrl);
4225
4226 qp->sq.wrid[idx] = wr_id;
4227 qp->sq.w_list[idx].opcode = mlx5_opcode;
4228 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
4229 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
4230 qp->sq.w_list[idx].next = qp->sq.cur_post;
4231}
4232
4233
Eli Cohene126ba92013-07-07 17:25:49 +03004234int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
4235 struct ib_send_wr **bad_wr)
4236{
4237 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
4238 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004239 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02004240 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02004241 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004242 struct mlx5_wqe_data_seg *dpseg;
4243 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02004244 struct mlx5_bf *bf;
Eli Cohene126ba92013-07-07 17:25:49 +03004245 int uninitialized_var(size);
Haggai Erand16e91d2016-02-29 15:45:05 +02004246 void *qend;
Eli Cohene126ba92013-07-07 17:25:49 +03004247 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03004248 unsigned idx;
4249 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004250 int num_sge;
4251 void *seg;
4252 int nreq;
4253 int i;
4254 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004255 u8 fence;
4256
Haggai Erand16e91d2016-02-29 15:45:05 +02004257 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4258 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
4259
4260 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004261 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02004262 qend = qp->sq.qend;
4263
Eli Cohene126ba92013-07-07 17:25:49 +03004264 spin_lock_irqsave(&qp->sq.lock, flags);
4265
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004266 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4267 err = -EIO;
4268 *bad_wr = wr;
4269 nreq = 0;
4270 goto out;
4271 }
4272
Eli Cohene126ba92013-07-07 17:25:49 +03004273 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04004274 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03004275 mlx5_ib_warn(dev, "\n");
4276 err = -EINVAL;
4277 *bad_wr = wr;
4278 goto out;
4279 }
4280
Eli Cohene126ba92013-07-07 17:25:49 +03004281 num_sge = wr->num_sge;
4282 if (unlikely(num_sge > qp->sq.max_gs)) {
4283 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03004284 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03004285 *bad_wr = wr;
4286 goto out;
4287 }
4288
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004289 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
4290 if (err) {
4291 mlx5_ib_warn(dev, "\n");
4292 err = -ENOMEM;
4293 *bad_wr = wr;
4294 goto out;
4295 }
Eli Cohene126ba92013-07-07 17:25:49 +03004296
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004297 if (wr->opcode == IB_WR_LOCAL_INV ||
4298 wr->opcode == IB_WR_REG_MR) {
4299 fence = dev->umr_fence;
4300 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
4301 } else if (wr->send_flags & IB_SEND_FENCE) {
4302 if (qp->next_fence)
4303 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
4304 else
4305 fence = MLX5_FENCE_MODE_FENCE;
4306 } else {
4307 fence = qp->next_fence;
4308 }
4309
Eli Cohene126ba92013-07-07 17:25:49 +03004310 switch (ibqp->qp_type) {
4311 case IB_QPT_XRC_INI:
4312 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03004313 seg += sizeof(*xrc);
4314 size += sizeof(*xrc) / 16;
4315 /* fall through */
4316 case IB_QPT_RC:
4317 switch (wr->opcode) {
4318 case IB_WR_RDMA_READ:
4319 case IB_WR_RDMA_WRITE:
4320 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004321 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4322 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004323 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004324 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4325 break;
4326
4327 case IB_WR_ATOMIC_CMP_AND_SWP:
4328 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03004329 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03004330 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
4331 err = -ENOSYS;
4332 *bad_wr = wr;
4333 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004334
4335 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03004336 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
4337 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004338 set_linv_wr(qp, &seg, &size);
Eli Cohene126ba92013-07-07 17:25:49 +03004339 num_sge = 0;
4340 break;
4341
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004342 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004343 qp->sq.wr_data[idx] = IB_WR_REG_MR;
4344 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
4345 err = set_reg_wr(qp, reg_wr(wr), &seg, &size);
4346 if (err) {
4347 *bad_wr = wr;
4348 goto out;
4349 }
4350 num_sge = 0;
4351 break;
4352
Sagi Grimberge6631812014-02-23 14:19:11 +02004353 case IB_WR_REG_SIG_MR:
4354 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004355 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004356
4357 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
4358 err = set_sig_umr_wr(wr, qp, &seg, &size);
4359 if (err) {
4360 mlx5_ib_warn(dev, "\n");
4361 *bad_wr = wr;
4362 goto out;
4363 }
4364
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004365 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4366 fence, MLX5_OPCODE_UMR);
Sagi Grimberge6631812014-02-23 14:19:11 +02004367 /*
4368 * SET_PSV WQEs are not signaled and solicited
4369 * on error
4370 */
4371 wr->send_flags &= ~IB_SEND_SIGNALED;
4372 wr->send_flags |= IB_SEND_SOLICITED;
4373 err = begin_wqe(qp, &seg, &ctrl, wr,
4374 &idx, &size, nreq);
4375 if (err) {
4376 mlx5_ib_warn(dev, "\n");
4377 err = -ENOMEM;
4378 *bad_wr = wr;
4379 goto out;
4380 }
4381
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004382 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02004383 mr->sig->psv_memory.psv_idx, &seg,
4384 &size);
4385 if (err) {
4386 mlx5_ib_warn(dev, "\n");
4387 *bad_wr = wr;
4388 goto out;
4389 }
4390
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004391 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4392 fence, MLX5_OPCODE_SET_PSV);
Sagi Grimberge6631812014-02-23 14:19:11 +02004393 err = begin_wqe(qp, &seg, &ctrl, wr,
4394 &idx, &size, nreq);
4395 if (err) {
4396 mlx5_ib_warn(dev, "\n");
4397 err = -ENOMEM;
4398 *bad_wr = wr;
4399 goto out;
4400 }
4401
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004402 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02004403 mr->sig->psv_wire.psv_idx, &seg,
4404 &size);
4405 if (err) {
4406 mlx5_ib_warn(dev, "\n");
4407 *bad_wr = wr;
4408 goto out;
4409 }
4410
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004411 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4412 fence, MLX5_OPCODE_SET_PSV);
4413 qp->next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004414 num_sge = 0;
4415 goto skip_psv;
4416
Eli Cohene126ba92013-07-07 17:25:49 +03004417 default:
4418 break;
4419 }
4420 break;
4421
4422 case IB_QPT_UC:
4423 switch (wr->opcode) {
4424 case IB_WR_RDMA_WRITE:
4425 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004426 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4427 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004428 seg += sizeof(struct mlx5_wqe_raddr_seg);
4429 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4430 break;
4431
4432 default:
4433 break;
4434 }
4435 break;
4436
Eli Cohene126ba92013-07-07 17:25:49 +03004437 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02004438 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
4439 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
4440 err = -EPERM;
4441 *bad_wr = wr;
4442 goto out;
4443 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07004444 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02004445 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004446 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004447 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004448 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4449 if (unlikely((seg == qend)))
4450 seg = mlx5_get_send_wqe(qp, 0);
4451 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004452 case IB_QPT_UD:
4453 set_datagram_seg(seg, wr);
4454 seg += sizeof(struct mlx5_wqe_datagram_seg);
4455 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004456
Erez Shitritf0313962016-02-21 16:27:17 +02004457 if (unlikely((seg == qend)))
4458 seg = mlx5_get_send_wqe(qp, 0);
4459
4460 /* handle qp that supports ud offload */
4461 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4462 struct mlx5_wqe_eth_pad *pad;
4463
4464 pad = seg;
4465 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4466 seg += sizeof(struct mlx5_wqe_eth_pad);
4467 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4468
4469 seg = set_eth_seg(seg, wr, qend, qp, &size);
4470
4471 if (unlikely((seg == qend)))
4472 seg = mlx5_get_send_wqe(qp, 0);
4473 }
4474 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004475 case MLX5_IB_QPT_REG_UMR:
4476 if (wr->opcode != MLX5_IB_WR_UMR) {
4477 err = -EINVAL;
4478 mlx5_ib_warn(dev, "bad opcode\n");
4479 goto out;
4480 }
4481 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004482 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Maor Gottlieb578e7262016-10-27 16:36:37 +03004483 set_reg_umr_segment(seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
Eli Cohene126ba92013-07-07 17:25:49 +03004484 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4485 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4486 if (unlikely((seg == qend)))
4487 seg = mlx5_get_send_wqe(qp, 0);
4488 set_reg_mkey_segment(seg, wr);
4489 seg += sizeof(struct mlx5_mkey_seg);
4490 size += sizeof(struct mlx5_mkey_seg) / 16;
4491 if (unlikely((seg == qend)))
4492 seg = mlx5_get_send_wqe(qp, 0);
4493 break;
4494
4495 default:
4496 break;
4497 }
4498
4499 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4500 int uninitialized_var(sz);
4501
4502 err = set_data_inl_seg(qp, wr, seg, &sz);
4503 if (unlikely(err)) {
4504 mlx5_ib_warn(dev, "\n");
4505 *bad_wr = wr;
4506 goto out;
4507 }
Eli Cohene126ba92013-07-07 17:25:49 +03004508 size += sz;
4509 } else {
4510 dpseg = seg;
4511 for (i = 0; i < num_sge; i++) {
4512 if (unlikely(dpseg == qend)) {
4513 seg = mlx5_get_send_wqe(qp, 0);
4514 dpseg = seg;
4515 }
4516 if (likely(wr->sg_list[i].length)) {
4517 set_data_ptr_seg(dpseg, wr->sg_list + i);
4518 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4519 dpseg++;
4520 }
4521 }
4522 }
4523
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004524 qp->next_fence = next_fence;
4525 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq, fence,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004526 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004527skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004528 if (0)
4529 dump_wqe(qp, idx, size);
4530 }
4531
4532out:
4533 if (likely(nreq)) {
4534 qp->sq.head += nreq;
4535
4536 /* Make sure that descriptors are written before
4537 * updating doorbell record and ringing the doorbell
4538 */
4539 wmb();
4540
4541 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4542
Eli Cohenada388f2014-01-14 17:45:16 +02004543 /* Make sure doorbell record is visible to the HCA before
4544 * we hit doorbell */
4545 wmb();
4546
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004547 /* currently we support only regular doorbells */
4548 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset, NULL);
4549 /* Make sure doorbells don't leak out of SQ spinlock
4550 * and reach the HCA out of order.
4551 */
4552 mmiowb();
Eli Cohene126ba92013-07-07 17:25:49 +03004553 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03004554 }
4555
4556 spin_unlock_irqrestore(&qp->sq.lock, flags);
4557
4558 return err;
4559}
4560
4561static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4562{
4563 sig->signature = calc_sig(sig, size);
4564}
4565
4566int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4567 struct ib_recv_wr **bad_wr)
4568{
4569 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4570 struct mlx5_wqe_data_seg *scat;
4571 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004572 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4573 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004574 unsigned long flags;
4575 int err = 0;
4576 int nreq;
4577 int ind;
4578 int i;
4579
Haggai Erand16e91d2016-02-29 15:45:05 +02004580 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4581 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4582
Eli Cohene126ba92013-07-07 17:25:49 +03004583 spin_lock_irqsave(&qp->rq.lock, flags);
4584
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004585 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4586 err = -EIO;
4587 *bad_wr = wr;
4588 nreq = 0;
4589 goto out;
4590 }
4591
Eli Cohene126ba92013-07-07 17:25:49 +03004592 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4593
4594 for (nreq = 0; wr; nreq++, wr = wr->next) {
4595 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4596 err = -ENOMEM;
4597 *bad_wr = wr;
4598 goto out;
4599 }
4600
4601 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4602 err = -EINVAL;
4603 *bad_wr = wr;
4604 goto out;
4605 }
4606
4607 scat = get_recv_wqe(qp, ind);
4608 if (qp->wq_sig)
4609 scat++;
4610
4611 for (i = 0; i < wr->num_sge; i++)
4612 set_data_ptr_seg(scat + i, wr->sg_list + i);
4613
4614 if (i < qp->rq.max_gs) {
4615 scat[i].byte_count = 0;
4616 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4617 scat[i].addr = 0;
4618 }
4619
4620 if (qp->wq_sig) {
4621 sig = (struct mlx5_rwqe_sig *)scat;
4622 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4623 }
4624
4625 qp->rq.wrid[ind] = wr->wr_id;
4626
4627 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4628 }
4629
4630out:
4631 if (likely(nreq)) {
4632 qp->rq.head += nreq;
4633
4634 /* Make sure that descriptors are written before
4635 * doorbell record.
4636 */
4637 wmb();
4638
4639 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4640 }
4641
4642 spin_unlock_irqrestore(&qp->rq.lock, flags);
4643
4644 return err;
4645}
4646
4647static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4648{
4649 switch (mlx5_state) {
4650 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4651 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4652 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4653 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4654 case MLX5_QP_STATE_SQ_DRAINING:
4655 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4656 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4657 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4658 default: return -1;
4659 }
4660}
4661
4662static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4663{
4664 switch (mlx5_mig_state) {
4665 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4666 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4667 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4668 default: return -1;
4669 }
4670}
4671
4672static int to_ib_qp_access_flags(int mlx5_flags)
4673{
4674 int ib_flags = 0;
4675
4676 if (mlx5_flags & MLX5_QP_BIT_RRE)
4677 ib_flags |= IB_ACCESS_REMOTE_READ;
4678 if (mlx5_flags & MLX5_QP_BIT_RWE)
4679 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4680 if (mlx5_flags & MLX5_QP_BIT_RAE)
4681 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4682
4683 return ib_flags;
4684}
4685
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004686static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004687 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004688 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03004689{
Eli Cohene126ba92013-07-07 17:25:49 +03004690
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004691 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03004692
Jason Gunthorpee7996a92018-01-29 13:26:40 -07004693 if (!path->port || path->port > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03004694 return;
4695
Leon Romanovskyae59c3f2018-01-12 07:58:39 +02004696 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
4697
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004698 rdma_ah_set_port_num(ah_attr, path->port);
4699 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03004700
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004701 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
4702 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
4703 rdma_ah_set_static_rate(ah_attr,
4704 path->static_rate ? path->static_rate - 5 : 0);
4705 if (path->grh_mlid & (1 << 7)) {
4706 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
4707
4708 rdma_ah_set_grh(ah_attr, NULL,
4709 tc_fl & 0xfffff,
4710 path->mgid_index,
4711 path->hop_limit,
4712 (tc_fl >> 20) & 0xff);
4713 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03004714 }
4715}
4716
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004717static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4718 struct mlx5_ib_sq *sq,
4719 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004720{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004721 void *out;
4722 void *sqc;
4723 int inlen;
4724 int err;
4725
4726 inlen = MLX5_ST_SZ_BYTES(query_sq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004727 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004728 if (!out)
4729 return -ENOMEM;
4730
4731 err = mlx5_core_query_sq(dev->mdev, sq->base.mqp.qpn, out);
4732 if (err)
4733 goto out;
4734
4735 sqc = MLX5_ADDR_OF(query_sq_out, out, sq_context);
4736 *sq_state = MLX5_GET(sqc, sqc, state);
4737 sq->state = *sq_state;
4738
4739out:
4740 kvfree(out);
4741 return err;
4742}
4743
4744static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
4745 struct mlx5_ib_rq *rq,
4746 u8 *rq_state)
4747{
4748 void *out;
4749 void *rqc;
4750 int inlen;
4751 int err;
4752
4753 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004754 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004755 if (!out)
4756 return -ENOMEM;
4757
4758 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
4759 if (err)
4760 goto out;
4761
4762 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
4763 *rq_state = MLX5_GET(rqc, rqc, state);
4764 rq->state = *rq_state;
4765
4766out:
4767 kvfree(out);
4768 return err;
4769}
4770
4771static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
4772 struct mlx5_ib_qp *qp, u8 *qp_state)
4773{
4774 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
4775 [MLX5_RQC_STATE_RST] = {
4776 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4777 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4778 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
4779 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
4780 },
4781 [MLX5_RQC_STATE_RDY] = {
4782 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4783 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4784 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
4785 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
4786 },
4787 [MLX5_RQC_STATE_ERR] = {
4788 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4789 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4790 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
4791 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
4792 },
4793 [MLX5_RQ_STATE_NA] = {
4794 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4795 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4796 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
4797 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
4798 },
4799 };
4800
4801 *qp_state = sqrq_trans[rq_state][sq_state];
4802
4803 if (*qp_state == MLX5_QP_STATE_BAD) {
4804 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4805 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
4806 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
4807 return -EINVAL;
4808 }
4809
4810 if (*qp_state == MLX5_QP_STATE)
4811 *qp_state = qp->state;
4812
4813 return 0;
4814}
4815
4816static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
4817 struct mlx5_ib_qp *qp,
4818 u8 *raw_packet_qp_state)
4819{
4820 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
4821 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
4822 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
4823 int err;
4824 u8 sq_state = MLX5_SQ_STATE_NA;
4825 u8 rq_state = MLX5_RQ_STATE_NA;
4826
4827 if (qp->sq.wqe_cnt) {
4828 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
4829 if (err)
4830 return err;
4831 }
4832
4833 if (qp->rq.wqe_cnt) {
4834 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
4835 if (err)
4836 return err;
4837 }
4838
4839 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
4840 raw_packet_qp_state);
4841}
4842
4843static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
4844 struct ib_qp_attr *qp_attr)
4845{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004846 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03004847 struct mlx5_qp_context *context;
4848 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004849 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03004850 int err = 0;
4851
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004852 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004853 if (!outb)
4854 return -ENOMEM;
4855
majd@mellanox.com19098df2016-01-14 19:13:03 +02004856 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004857 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03004858 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004859 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004860
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004861 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
4862 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
4863
Eli Cohene126ba92013-07-07 17:25:49 +03004864 mlx5_state = be32_to_cpu(context->flags) >> 28;
4865
4866 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03004867 qp_attr->path_mtu = context->mtu_msgmax >> 5;
4868 qp_attr->path_mig_state =
4869 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
4870 qp_attr->qkey = be32_to_cpu(context->qkey);
4871 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
4872 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
4873 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
4874 qp_attr->qp_access_flags =
4875 to_ib_qp_access_flags(be32_to_cpu(context->params2));
4876
4877 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004878 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
4879 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004880 qp_attr->alt_pkey_index =
4881 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004882 qp_attr->alt_port_num =
4883 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03004884 }
4885
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004886 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004887 qp_attr->port_num = context->pri_path.port;
4888
4889 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4890 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
4891
4892 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
4893
4894 qp_attr->max_dest_rd_atomic =
4895 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
4896 qp_attr->min_rnr_timer =
4897 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
4898 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
4899 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
4900 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
4901 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004902
4903out:
4904 kfree(outb);
4905 return err;
4906}
4907
Moni Shoua776a3902018-01-02 16:19:33 +02004908static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
4909 struct ib_qp_attr *qp_attr, int qp_attr_mask,
4910 struct ib_qp_init_attr *qp_init_attr)
4911{
4912 struct mlx5_core_dct *dct = &mqp->dct.mdct;
4913 u32 *out;
4914 u32 access_flags = 0;
4915 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
4916 void *dctc;
4917 int err;
4918 int supported_mask = IB_QP_STATE |
4919 IB_QP_ACCESS_FLAGS |
4920 IB_QP_PORT |
4921 IB_QP_MIN_RNR_TIMER |
4922 IB_QP_AV |
4923 IB_QP_PATH_MTU |
4924 IB_QP_PKEY_INDEX;
4925
4926 if (qp_attr_mask & ~supported_mask)
4927 return -EINVAL;
4928 if (mqp->state != IB_QPS_RTR)
4929 return -EINVAL;
4930
4931 out = kzalloc(outlen, GFP_KERNEL);
4932 if (!out)
4933 return -ENOMEM;
4934
4935 err = mlx5_core_dct_query(dev->mdev, dct, out, outlen);
4936 if (err)
4937 goto out;
4938
4939 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
4940
4941 if (qp_attr_mask & IB_QP_STATE)
4942 qp_attr->qp_state = IB_QPS_RTR;
4943
4944 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
4945 if (MLX5_GET(dctc, dctc, rre))
4946 access_flags |= IB_ACCESS_REMOTE_READ;
4947 if (MLX5_GET(dctc, dctc, rwe))
4948 access_flags |= IB_ACCESS_REMOTE_WRITE;
4949 if (MLX5_GET(dctc, dctc, rae))
4950 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
4951 qp_attr->qp_access_flags = access_flags;
4952 }
4953
4954 if (qp_attr_mask & IB_QP_PORT)
4955 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
4956 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
4957 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
4958 if (qp_attr_mask & IB_QP_AV) {
4959 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
4960 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
4961 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
4962 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
4963 }
4964 if (qp_attr_mask & IB_QP_PATH_MTU)
4965 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
4966 if (qp_attr_mask & IB_QP_PKEY_INDEX)
4967 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
4968out:
4969 kfree(out);
4970 return err;
4971}
4972
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004973int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
4974 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
4975{
4976 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4977 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4978 int err = 0;
4979 u8 raw_packet_qp_state;
4980
Yishai Hadas28d61372016-05-23 15:20:56 +03004981 if (ibqp->rwq_ind_tbl)
4982 return -ENOSYS;
4983
Haggai Erand16e91d2016-02-29 15:45:05 +02004984 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4985 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
4986 qp_init_attr);
4987
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004988 /* Not all of output fields are applicable, make sure to zero them */
4989 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
4990 memset(qp_attr, 0, sizeof(*qp_attr));
4991
Moni Shoua776a3902018-01-02 16:19:33 +02004992 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
4993 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
4994 qp_attr_mask, qp_init_attr);
4995
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004996 mutex_lock(&qp->mutex);
4997
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004998 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
4999 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02005000 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
5001 if (err)
5002 goto out;
5003 qp->state = raw_packet_qp_state;
5004 qp_attr->port_num = 1;
5005 } else {
5006 err = query_qp_attr(dev, qp, qp_attr);
5007 if (err)
5008 goto out;
5009 }
5010
5011 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03005012 qp_attr->cur_qp_state = qp_attr->qp_state;
5013 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
5014 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
5015
5016 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005017 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005018 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005019 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005020 } else {
5021 qp_attr->cap.max_send_wr = 0;
5022 qp_attr->cap.max_send_sge = 0;
5023 }
5024
Noa Osherovich0540d812016-06-04 15:15:32 +03005025 qp_init_attr->qp_type = ibqp->qp_type;
5026 qp_init_attr->recv_cq = ibqp->recv_cq;
5027 qp_init_attr->send_cq = ibqp->send_cq;
5028 qp_init_attr->srq = ibqp->srq;
5029 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005030
5031 qp_init_attr->cap = qp_attr->cap;
5032
5033 qp_init_attr->create_flags = 0;
5034 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
5035 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
5036
Leon Romanovsky051f2632015-12-20 12:16:11 +02005037 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
5038 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
5039 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
5040 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
5041 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
5042 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02005043 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
5044 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02005045
Eli Cohene126ba92013-07-07 17:25:49 +03005046 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5047 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5048
Eli Cohene126ba92013-07-07 17:25:49 +03005049out:
5050 mutex_unlock(&qp->mutex);
5051 return err;
5052}
5053
5054struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
5055 struct ib_ucontext *context,
5056 struct ib_udata *udata)
5057{
5058 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5059 struct mlx5_ib_xrcd *xrcd;
5060 int err;
5061
Saeed Mahameed938fe832015-05-28 22:28:41 +03005062 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005063 return ERR_PTR(-ENOSYS);
5064
5065 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5066 if (!xrcd)
5067 return ERR_PTR(-ENOMEM);
5068
Jack Morgenstein9603b612014-07-28 23:30:22 +03005069 err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005070 if (err) {
5071 kfree(xrcd);
5072 return ERR_PTR(-ENOMEM);
5073 }
5074
5075 return &xrcd->ibxrcd;
5076}
5077
5078int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
5079{
5080 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5081 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
5082 int err;
5083
Jack Morgenstein9603b612014-07-28 23:30:22 +03005084 err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
Leon Romanovskyb0818082018-01-28 11:25:30 +02005085 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03005086 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005087
5088 kfree(xrcd);
Eli Cohene126ba92013-07-07 17:25:49 +03005089 return 0;
5090}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005091
Yishai Hadas350d0e42016-08-28 14:58:18 +03005092static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5093{
5094 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5095 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5096 struct ib_event event;
5097
5098 if (rwq->ibwq.event_handler) {
5099 event.device = rwq->ibwq.device;
5100 event.element.wq = &rwq->ibwq;
5101 switch (type) {
5102 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5103 event.event = IB_EVENT_WQ_FATAL;
5104 break;
5105 default:
5106 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5107 return;
5108 }
5109
5110 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5111 }
5112}
5113
Maor Gottlieb03404e82017-05-30 10:29:13 +03005114static int set_delay_drop(struct mlx5_ib_dev *dev)
5115{
5116 int err = 0;
5117
5118 mutex_lock(&dev->delay_drop.lock);
5119 if (dev->delay_drop.activate)
5120 goto out;
5121
5122 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
5123 if (err)
5124 goto out;
5125
5126 dev->delay_drop.activate = true;
5127out:
5128 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005129
5130 if (!err)
5131 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005132 return err;
5133}
5134
Yishai Hadas79b20a62016-05-23 15:20:50 +03005135static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5136 struct ib_wq_init_attr *init_attr)
5137{
5138 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005139 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005140 __be64 *rq_pas0;
5141 void *in;
5142 void *rqc;
5143 void *wq;
5144 int inlen;
5145 int err;
5146
5147 dev = to_mdev(pd->device);
5148
5149 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005150 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005151 if (!in)
5152 return -ENOMEM;
5153
5154 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
5155 MLX5_SET(rqc, rqc, mem_rq_type,
5156 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
5157 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
5158 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
5159 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
5160 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
5161 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03005162 MLX5_SET(wq, wq, wq_type,
5163 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
5164 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005165 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5166 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
5167 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
5168 err = -EOPNOTSUPP;
5169 goto out;
5170 } else {
5171 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
5172 }
5173 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005174 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03005175 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
5176 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
5177 MLX5_SET(wq, wq, log_wqe_stride_size,
5178 rwq->single_stride_log_num_of_bytes -
5179 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
5180 MLX5_SET(wq, wq, log_wqe_num_of_strides, rwq->log_num_strides -
5181 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES);
5182 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005183 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
5184 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
5185 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
5186 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
5187 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
5188 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02005189 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005190 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02005191 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005192 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
5193 err = -EOPNOTSUPP;
5194 goto out;
5195 }
5196 } else {
5197 MLX5_SET(rqc, rqc, vsd, 1);
5198 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02005199 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
5200 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
5201 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
5202 err = -EOPNOTSUPP;
5203 goto out;
5204 }
5205 MLX5_SET(rqc, rqc, scatter_fcs, 1);
5206 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03005207 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5208 if (!(dev->ib_dev.attrs.raw_packet_caps &
5209 IB_RAW_PACKET_CAP_DELAY_DROP)) {
5210 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
5211 err = -EOPNOTSUPP;
5212 goto out;
5213 }
5214 MLX5_SET(rqc, rqc, delay_drop_en, 1);
5215 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005216 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
5217 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03005218 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005219 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5220 err = set_delay_drop(dev);
5221 if (err) {
5222 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
5223 err);
5224 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
5225 } else {
5226 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
5227 }
5228 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005229out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03005230 kvfree(in);
5231 return err;
5232}
5233
5234static int set_user_rq_size(struct mlx5_ib_dev *dev,
5235 struct ib_wq_init_attr *wq_init_attr,
5236 struct mlx5_ib_create_wq *ucmd,
5237 struct mlx5_ib_rwq *rwq)
5238{
5239 /* Sanity check RQ size before proceeding */
5240 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
5241 return -EINVAL;
5242
5243 if (!ucmd->rq_wqe_count)
5244 return -EINVAL;
5245
5246 rwq->wqe_count = ucmd->rq_wqe_count;
5247 rwq->wqe_shift = ucmd->rq_wqe_shift;
5248 rwq->buf_size = (rwq->wqe_count << rwq->wqe_shift);
5249 rwq->log_rq_stride = rwq->wqe_shift;
5250 rwq->log_rq_size = ilog2(rwq->wqe_count);
5251 return 0;
5252}
5253
5254static int prepare_user_rq(struct ib_pd *pd,
5255 struct ib_wq_init_attr *init_attr,
5256 struct ib_udata *udata,
5257 struct mlx5_ib_rwq *rwq)
5258{
5259 struct mlx5_ib_dev *dev = to_mdev(pd->device);
5260 struct mlx5_ib_create_wq ucmd = {};
5261 int err;
5262 size_t required_cmd_sz;
5263
Noa Osherovichccc87082017-10-17 18:01:13 +03005264 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
5265 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005266 if (udata->inlen < required_cmd_sz) {
5267 mlx5_ib_dbg(dev, "invalid inlen\n");
5268 return -EINVAL;
5269 }
5270
5271 if (udata->inlen > sizeof(ucmd) &&
5272 !ib_is_udata_cleared(udata, sizeof(ucmd),
5273 udata->inlen - sizeof(ucmd))) {
5274 mlx5_ib_dbg(dev, "inlen is not supported\n");
5275 return -EOPNOTSUPP;
5276 }
5277
5278 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
5279 mlx5_ib_dbg(dev, "copy failed\n");
5280 return -EFAULT;
5281 }
5282
Noa Osherovichccc87082017-10-17 18:01:13 +03005283 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03005284 mlx5_ib_dbg(dev, "invalid comp mask\n");
5285 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03005286 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
5287 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
5288 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
5289 return -EOPNOTSUPP;
5290 }
5291 if ((ucmd.single_stride_log_num_of_bytes <
5292 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
5293 (ucmd.single_stride_log_num_of_bytes >
5294 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
5295 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
5296 ucmd.single_stride_log_num_of_bytes,
5297 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
5298 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
5299 return -EINVAL;
5300 }
5301 if ((ucmd.single_wqe_log_num_of_strides >
5302 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
5303 (ucmd.single_wqe_log_num_of_strides <
5304 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES)) {
5305 mlx5_ib_dbg(dev, "Invalid log num strides (%u. Range is %u - %u)\n",
5306 ucmd.single_wqe_log_num_of_strides,
5307 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
5308 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
5309 return -EINVAL;
5310 }
5311 rwq->single_stride_log_num_of_bytes =
5312 ucmd.single_stride_log_num_of_bytes;
5313 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
5314 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
5315 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005316 }
5317
5318 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
5319 if (err) {
5320 mlx5_ib_dbg(dev, "err %d\n", err);
5321 return err;
5322 }
5323
5324 err = create_user_rq(dev, pd, rwq, &ucmd);
5325 if (err) {
5326 mlx5_ib_dbg(dev, "err %d\n", err);
5327 if (err)
5328 return err;
5329 }
5330
5331 rwq->user_index = ucmd.user_index;
5332 return 0;
5333}
5334
5335struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
5336 struct ib_wq_init_attr *init_attr,
5337 struct ib_udata *udata)
5338{
5339 struct mlx5_ib_dev *dev;
5340 struct mlx5_ib_rwq *rwq;
5341 struct mlx5_ib_create_wq_resp resp = {};
5342 size_t min_resp_len;
5343 int err;
5344
5345 if (!udata)
5346 return ERR_PTR(-ENOSYS);
5347
5348 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5349 if (udata->outlen && udata->outlen < min_resp_len)
5350 return ERR_PTR(-EINVAL);
5351
5352 dev = to_mdev(pd->device);
5353 switch (init_attr->wq_type) {
5354 case IB_WQT_RQ:
5355 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
5356 if (!rwq)
5357 return ERR_PTR(-ENOMEM);
5358 err = prepare_user_rq(pd, init_attr, udata, rwq);
5359 if (err)
5360 goto err;
5361 err = create_rq(rwq, pd, init_attr);
5362 if (err)
5363 goto err_user_rq;
5364 break;
5365 default:
5366 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
5367 init_attr->wq_type);
5368 return ERR_PTR(-EINVAL);
5369 }
5370
Yishai Hadas350d0e42016-08-28 14:58:18 +03005371 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005372 rwq->ibwq.state = IB_WQS_RESET;
5373 if (udata->outlen) {
5374 resp.response_length = offsetof(typeof(resp), response_length) +
5375 sizeof(resp.response_length);
5376 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5377 if (err)
5378 goto err_copy;
5379 }
5380
Yishai Hadas350d0e42016-08-28 14:58:18 +03005381 rwq->core_qp.event = mlx5_ib_wq_event;
5382 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005383 return &rwq->ibwq;
5384
5385err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03005386 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005387err_user_rq:
Maor Gottliebfe248c32017-05-30 10:29:14 +03005388 destroy_user_rq(dev, pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005389err:
5390 kfree(rwq);
5391 return ERR_PTR(err);
5392}
5393
5394int mlx5_ib_destroy_wq(struct ib_wq *wq)
5395{
5396 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5397 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5398
Yishai Hadas350d0e42016-08-28 14:58:18 +03005399 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005400 destroy_user_rq(dev, wq->pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005401 kfree(rwq);
5402
5403 return 0;
5404}
5405
Yishai Hadasc5f90922016-05-23 15:20:53 +03005406struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
5407 struct ib_rwq_ind_table_init_attr *init_attr,
5408 struct ib_udata *udata)
5409{
5410 struct mlx5_ib_dev *dev = to_mdev(device);
5411 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
5412 int sz = 1 << init_attr->log_ind_tbl_size;
5413 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
5414 size_t min_resp_len;
5415 int inlen;
5416 int err;
5417 int i;
5418 u32 *in;
5419 void *rqtc;
5420
5421 if (udata->inlen > 0 &&
5422 !ib_is_udata_cleared(udata, 0,
5423 udata->inlen))
5424 return ERR_PTR(-EOPNOTSUPP);
5425
Maor Gottliebefd7f402016-10-27 16:36:40 +03005426 if (init_attr->log_ind_tbl_size >
5427 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
5428 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
5429 init_attr->log_ind_tbl_size,
5430 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
5431 return ERR_PTR(-EINVAL);
5432 }
5433
Yishai Hadasc5f90922016-05-23 15:20:53 +03005434 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5435 if (udata->outlen && udata->outlen < min_resp_len)
5436 return ERR_PTR(-EINVAL);
5437
5438 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
5439 if (!rwq_ind_tbl)
5440 return ERR_PTR(-ENOMEM);
5441
5442 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005443 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005444 if (!in) {
5445 err = -ENOMEM;
5446 goto err;
5447 }
5448
5449 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
5450
5451 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
5452 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
5453
5454 for (i = 0; i < sz; i++)
5455 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
5456
5457 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
5458 kvfree(in);
5459
5460 if (err)
5461 goto err;
5462
5463 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
5464 if (udata->outlen) {
5465 resp.response_length = offsetof(typeof(resp), response_length) +
5466 sizeof(resp.response_length);
5467 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5468 if (err)
5469 goto err_copy;
5470 }
5471
5472 return &rwq_ind_tbl->ib_rwq_ind_tbl;
5473
5474err_copy:
5475 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
5476err:
5477 kfree(rwq_ind_tbl);
5478 return ERR_PTR(err);
5479}
5480
5481int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
5482{
5483 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
5484 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
5485
5486 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
5487
5488 kfree(rwq_ind_tbl);
5489 return 0;
5490}
5491
Yishai Hadas79b20a62016-05-23 15:20:50 +03005492int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
5493 u32 wq_attr_mask, struct ib_udata *udata)
5494{
5495 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5496 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5497 struct mlx5_ib_modify_wq ucmd = {};
5498 size_t required_cmd_sz;
5499 int curr_wq_state;
5500 int wq_state;
5501 int inlen;
5502 int err;
5503 void *rqc;
5504 void *in;
5505
5506 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
5507 if (udata->inlen < required_cmd_sz)
5508 return -EINVAL;
5509
5510 if (udata->inlen > sizeof(ucmd) &&
5511 !ib_is_udata_cleared(udata, sizeof(ucmd),
5512 udata->inlen - sizeof(ucmd)))
5513 return -EOPNOTSUPP;
5514
5515 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
5516 return -EFAULT;
5517
5518 if (ucmd.comp_mask || ucmd.reserved)
5519 return -EOPNOTSUPP;
5520
5521 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005522 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005523 if (!in)
5524 return -ENOMEM;
5525
5526 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
5527
5528 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
5529 wq_attr->curr_wq_state : wq->state;
5530 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
5531 wq_attr->wq_state : curr_wq_state;
5532 if (curr_wq_state == IB_WQS_ERR)
5533 curr_wq_state = MLX5_RQC_STATE_ERR;
5534 if (wq_state == IB_WQS_ERR)
5535 wq_state = MLX5_RQC_STATE_ERR;
5536 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
5537 MLX5_SET(rqc, rqc, state, wq_state);
5538
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005539 if (wq_attr_mask & IB_WQ_FLAGS) {
5540 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
5541 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
5542 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
5543 mlx5_ib_dbg(dev, "VLAN offloads are not "
5544 "supported\n");
5545 err = -EOPNOTSUPP;
5546 goto out;
5547 }
5548 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5549 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
5550 MLX5_SET(rqc, rqc, vsd,
5551 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
5552 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005553
5554 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5555 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
5556 err = -EOPNOTSUPP;
5557 goto out;
5558 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005559 }
5560
Majd Dibbiny23a69642017-01-18 15:25:10 +02005561 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
5562 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
5563 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5564 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03005565 MLX5_SET(rqc, rqc, counter_set_id,
5566 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005567 } else
5568 pr_info_once("%s: Receive WQ counters are not supported on current FW\n",
5569 dev->ib_dev.name);
5570 }
5571
Yishai Hadas350d0e42016-08-28 14:58:18 +03005572 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005573 if (!err)
5574 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
5575
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005576out:
5577 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005578 return err;
5579}