blob: 4b08472cf0ba2b915f154475809aacb2273f40e3 [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"
Eli Cohene126ba92013-07-07 17:25:49 +030039
40/* not supported currently */
41static int wq_signature;
42
43enum {
44 MLX5_IB_ACK_REQ_FREQ = 8,
45};
46
47enum {
48 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
49 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
50 MLX5_IB_LINK_TYPE_IB = 0,
51 MLX5_IB_LINK_TYPE_ETH = 1
52};
53
54enum {
55 MLX5_IB_SQ_STRIDE = 6,
Eli Cohene126ba92013-07-07 17:25:49 +030056};
57
58static const u32 mlx5_ib_opcode[] = {
59 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020060 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030061 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
62 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
63 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
64 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
65 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
66 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
67 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
68 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030069 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030070 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
71 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
72 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
73};
74
Erez Shitritf0313962016-02-21 16:27:17 +020075struct mlx5_wqe_eth_pad {
76 u8 rsvd0[16];
77};
Eli Cohene126ba92013-07-07 17:25:49 +030078
Alex Veskereb49ab02016-08-28 12:25:53 +030079enum raw_qp_set_mask_map {
80 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020081 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030082};
83
Alex Vesker0680efa2016-08-28 12:25:52 +030084struct mlx5_modify_raw_qp_param {
85 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030086
87 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang7d29f342016-12-01 13:43:16 +020088 u32 rate_limit;
Alex Veskereb49ab02016-08-28 12:25:53 +030089 u8 rq_q_ctr_id;
Alex Vesker0680efa2016-08-28 12:25:52 +030090};
91
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030092static void get_cqs(enum ib_qp_type qp_type,
93 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
94 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
95
Eli Cohene126ba92013-07-07 17:25:49 +030096static int is_qp0(enum ib_qp_type qp_type)
97{
98 return qp_type == IB_QPT_SMI;
99}
100
Eli Cohene126ba92013-07-07 17:25:49 +0300101static int is_sqp(enum ib_qp_type qp_type)
102{
103 return is_qp0(qp_type) || is_qp1(qp_type);
104}
105
106static void *get_wqe(struct mlx5_ib_qp *qp, int offset)
107{
108 return mlx5_buf_offset(&qp->buf, offset);
109}
110
111static void *get_recv_wqe(struct mlx5_ib_qp *qp, int n)
112{
113 return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
114}
115
116void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n)
117{
118 return get_wqe(qp, qp->sq.offset + (n << MLX5_IB_SQ_STRIDE));
119}
120
Haggai Eranc1395a22014-12-11 17:04:14 +0200121/**
122 * mlx5_ib_read_user_wqe() - Copy a user-space WQE to kernel space.
123 *
124 * @qp: QP to copy from.
125 * @send: copy from the send queue when non-zero, use the receive queue
126 * otherwise.
127 * @wqe_index: index to start copying from. For send work queues, the
128 * wqe_index is in units of MLX5_SEND_WQE_BB.
129 * For receive work queue, it is the number of work queue
130 * element in the queue.
131 * @buffer: destination buffer.
132 * @length: maximum number of bytes to copy.
133 *
134 * Copies at least a single WQE, but may copy more data.
135 *
136 * Return: the number of bytes copied, or an error code.
137 */
138int mlx5_ib_read_user_wqe(struct mlx5_ib_qp *qp, int send, int wqe_index,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200139 void *buffer, u32 length,
140 struct mlx5_ib_qp_base *base)
Haggai Eranc1395a22014-12-11 17:04:14 +0200141{
142 struct ib_device *ibdev = qp->ibqp.device;
143 struct mlx5_ib_dev *dev = to_mdev(ibdev);
144 struct mlx5_ib_wq *wq = send ? &qp->sq : &qp->rq;
145 size_t offset;
146 size_t wq_end;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200147 struct ib_umem *umem = base->ubuffer.umem;
Haggai Eranc1395a22014-12-11 17:04:14 +0200148 u32 first_copy_length;
149 int wqe_length;
150 int ret;
151
152 if (wq->wqe_cnt == 0) {
153 mlx5_ib_dbg(dev, "mlx5_ib_read_user_wqe for a QP with wqe_cnt == 0. qp_type: 0x%x\n",
154 qp->ibqp.qp_type);
155 return -EINVAL;
156 }
157
158 offset = wq->offset + ((wqe_index % wq->wqe_cnt) << wq->wqe_shift);
159 wq_end = wq->offset + (wq->wqe_cnt << wq->wqe_shift);
160
161 if (send && length < sizeof(struct mlx5_wqe_ctrl_seg))
162 return -EINVAL;
163
164 if (offset > umem->length ||
165 (send && offset + sizeof(struct mlx5_wqe_ctrl_seg) > umem->length))
166 return -EINVAL;
167
168 first_copy_length = min_t(u32, offset + length, wq_end) - offset;
169 ret = ib_umem_copy_from(buffer, umem, offset, first_copy_length);
170 if (ret)
171 return ret;
172
173 if (send) {
174 struct mlx5_wqe_ctrl_seg *ctrl = buffer;
175 int ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
176
177 wqe_length = ds * MLX5_WQE_DS_UNITS;
178 } else {
179 wqe_length = 1 << wq->wqe_shift;
180 }
181
182 if (wqe_length <= first_copy_length)
183 return first_copy_length;
184
185 ret = ib_umem_copy_from(buffer + first_copy_length, umem, wq->offset,
186 wqe_length - first_copy_length);
187 if (ret)
188 return ret;
189
190 return wqe_length;
191}
192
Eli Cohene126ba92013-07-07 17:25:49 +0300193static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
194{
195 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
196 struct ib_event event;
197
majd@mellanox.com19098df2016-01-14 19:13:03 +0200198 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
199 /* This event is only valid for trans_qps */
200 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
201 }
Eli Cohene126ba92013-07-07 17:25:49 +0300202
203 if (ibqp->event_handler) {
204 event.device = ibqp->device;
205 event.element.qp = ibqp;
206 switch (type) {
207 case MLX5_EVENT_TYPE_PATH_MIG:
208 event.event = IB_EVENT_PATH_MIG;
209 break;
210 case MLX5_EVENT_TYPE_COMM_EST:
211 event.event = IB_EVENT_COMM_EST;
212 break;
213 case MLX5_EVENT_TYPE_SQ_DRAINED:
214 event.event = IB_EVENT_SQ_DRAINED;
215 break;
216 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
217 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
218 break;
219 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
220 event.event = IB_EVENT_QP_FATAL;
221 break;
222 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
223 event.event = IB_EVENT_PATH_MIG_ERR;
224 break;
225 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
226 event.event = IB_EVENT_QP_REQ_ERR;
227 break;
228 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
229 event.event = IB_EVENT_QP_ACCESS_ERR;
230 break;
231 default:
232 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
233 return;
234 }
235
236 ibqp->event_handler(&event, ibqp->qp_context);
237 }
238}
239
240static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
241 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
242{
243 int wqe_size;
244 int wq_size;
245
246 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300247 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300248 return -EINVAL;
249
250 if (!has_rq) {
251 qp->rq.max_gs = 0;
252 qp->rq.wqe_cnt = 0;
253 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300254 cap->max_recv_wr = 0;
255 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300256 } else {
257 if (ucmd) {
258 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
259 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
260 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
261 qp->rq.max_post = qp->rq.wqe_cnt;
262 } else {
263 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
264 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
265 wqe_size = roundup_pow_of_two(wqe_size);
266 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
267 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
268 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300269 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300270 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
271 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300272 MLX5_CAP_GEN(dev->mdev,
273 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300274 return -EINVAL;
275 }
276 qp->rq.wqe_shift = ilog2(wqe_size);
277 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
278 qp->rq.max_post = qp->rq.wqe_cnt;
279 }
280 }
281
282 return 0;
283}
284
Erez Shitritf0313962016-02-21 16:27:17 +0200285static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300286{
Andi Shyti618af382013-07-16 15:35:01 +0200287 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300288
Erez Shitritf0313962016-02-21 16:27:17 +0200289 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300290 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300291 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300292 /* fall through */
293 case IB_QPT_RC:
294 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200295 max(sizeof(struct mlx5_wqe_atomic_seg) +
296 sizeof(struct mlx5_wqe_raddr_seg),
297 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
298 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300299 break;
300
Eli Cohenb125a542013-09-11 16:35:22 +0300301 case IB_QPT_XRC_TGT:
302 return 0;
303
Eli Cohene126ba92013-07-07 17:25:49 +0300304 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300305 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200306 max(sizeof(struct mlx5_wqe_raddr_seg),
307 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
308 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300309 break;
310
311 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200312 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
313 size += sizeof(struct mlx5_wqe_eth_pad) +
314 sizeof(struct mlx5_wqe_eth_seg);
315 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300316 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200317 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300318 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300319 sizeof(struct mlx5_wqe_datagram_seg);
320 break;
321
322 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300323 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300324 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
325 sizeof(struct mlx5_mkey_seg);
326 break;
327
328 default:
329 return -EINVAL;
330 }
331
332 return size;
333}
334
335static int calc_send_wqe(struct ib_qp_init_attr *attr)
336{
337 int inl_size = 0;
338 int size;
339
Erez Shitritf0313962016-02-21 16:27:17 +0200340 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300341 if (size < 0)
342 return size;
343
344 if (attr->cap.max_inline_data) {
345 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
346 attr->cap.max_inline_data;
347 }
348
349 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200350 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN &&
351 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
352 return MLX5_SIG_WQE_SIZE;
353 else
354 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300355}
356
Eli Cohen288c01b2016-10-27 16:36:45 +0300357static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
358{
359 int max_sge;
360
361 if (attr->qp_type == IB_QPT_RC)
362 max_sge = (min_t(int, wqe_size, 512) -
363 sizeof(struct mlx5_wqe_ctrl_seg) -
364 sizeof(struct mlx5_wqe_raddr_seg)) /
365 sizeof(struct mlx5_wqe_data_seg);
366 else if (attr->qp_type == IB_QPT_XRC_INI)
367 max_sge = (min_t(int, wqe_size, 512) -
368 sizeof(struct mlx5_wqe_ctrl_seg) -
369 sizeof(struct mlx5_wqe_xrc_seg) -
370 sizeof(struct mlx5_wqe_raddr_seg)) /
371 sizeof(struct mlx5_wqe_data_seg);
372 else
373 max_sge = (wqe_size - sq_overhead(attr)) /
374 sizeof(struct mlx5_wqe_data_seg);
375
376 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
377 sizeof(struct mlx5_wqe_data_seg));
378}
379
Eli Cohene126ba92013-07-07 17:25:49 +0300380static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
381 struct mlx5_ib_qp *qp)
382{
383 int wqe_size;
384 int wq_size;
385
386 if (!attr->cap.max_send_wr)
387 return 0;
388
389 wqe_size = calc_send_wqe(attr);
390 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
391 if (wqe_size < 0)
392 return wqe_size;
393
Saeed Mahameed938fe832015-05-28 22:28:41 +0300394 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300395 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300396 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300397 return -EINVAL;
398 }
399
Erez Shitritf0313962016-02-21 16:27:17 +0200400 qp->max_inline_data = wqe_size - sq_overhead(attr) -
401 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300402 attr->cap.max_inline_data = qp->max_inline_data;
403
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200404 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN)
405 qp->signature_en = true;
406
Eli Cohene126ba92013-07-07 17:25:49 +0300407 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
408 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300409 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800410 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
411 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300412 qp->sq.wqe_cnt,
413 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300414 return -ENOMEM;
415 }
Eli Cohene126ba92013-07-07 17:25:49 +0300416 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300417 qp->sq.max_gs = get_send_sge(attr, wqe_size);
418 if (qp->sq.max_gs < attr->cap.max_send_sge)
419 return -ENOMEM;
420
421 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300422 qp->sq.max_post = wq_size / wqe_size;
423 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300424
425 return wq_size;
426}
427
428static int set_user_buf_size(struct mlx5_ib_dev *dev,
429 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200430 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200431 struct mlx5_ib_qp_base *base,
432 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300433{
434 int desc_sz = 1 << qp->sq.wqe_shift;
435
Saeed Mahameed938fe832015-05-28 22:28:41 +0300436 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300437 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300438 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300439 return -EINVAL;
440 }
441
442 if (ucmd->sq_wqe_count && ((1 << ilog2(ucmd->sq_wqe_count)) != ucmd->sq_wqe_count)) {
443 mlx5_ib_warn(dev, "sq_wqe_count %d, sq_wqe_count %d\n",
444 ucmd->sq_wqe_count, ucmd->sq_wqe_count);
445 return -EINVAL;
446 }
447
448 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
449
Saeed Mahameed938fe832015-05-28 22:28:41 +0300450 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300451 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300452 qp->sq.wqe_cnt,
453 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300454 return -EINVAL;
455 }
456
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300457 if (attr->qp_type == IB_QPT_RAW_PACKET ||
458 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200459 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
460 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
461 } else {
462 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
463 (qp->sq.wqe_cnt << 6);
464 }
Eli Cohene126ba92013-07-07 17:25:49 +0300465
466 return 0;
467}
468
469static int qp_has_rq(struct ib_qp_init_attr *attr)
470{
471 if (attr->qp_type == IB_QPT_XRC_INI ||
472 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
473 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
474 !attr->cap.max_recv_wr)
475 return 0;
476
477 return 1;
478}
479
Eli Cohen2f5ff262017-01-03 23:55:21 +0200480static int first_med_bfreg(void)
Eli Cohenc1be5232014-01-14 17:45:12 +0200481{
482 return 1;
483}
484
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200485enum {
486 /* this is the first blue flame register in the array of bfregs assigned
487 * to a processes. Since we do not use it for blue flame but rather
488 * regular 64 bit doorbells, we do not need a lock for maintaiing
489 * "odd/even" order
490 */
491 NUM_NON_BLUE_FLAME_BFREGS = 1,
492};
493
Eli Cohenb037c292017-01-03 23:55:26 +0200494static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
495{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200496 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200497}
498
499static int num_med_bfreg(struct mlx5_ib_dev *dev,
500 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200501{
502 int n;
503
Eli Cohenb037c292017-01-03 23:55:26 +0200504 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
505 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200506
507 return n >= 0 ? n : 0;
508}
509
Eli Cohenb037c292017-01-03 23:55:26 +0200510static int first_hi_bfreg(struct mlx5_ib_dev *dev,
511 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200512{
513 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200514
Eli Cohenb037c292017-01-03 23:55:26 +0200515 med = num_med_bfreg(dev, bfregi);
516 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200517}
518
Eli Cohenb037c292017-01-03 23:55:26 +0200519static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
520 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300521{
Eli Cohene126ba92013-07-07 17:25:49 +0300522 int i;
523
Eli Cohenb037c292017-01-03 23:55:26 +0200524 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
525 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200526 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300527 return i;
528 }
529 }
530
531 return -ENOMEM;
532}
533
Eli Cohenb037c292017-01-03 23:55:26 +0200534static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
535 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300536{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200537 int minidx = first_med_bfreg();
Eli Cohene126ba92013-07-07 17:25:49 +0300538 int i;
539
Eli Cohenb037c292017-01-03 23:55:26 +0200540 for (i = first_med_bfreg(); i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200541 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300542 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200543 if (!bfregi->count[minidx])
544 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300545 }
546
Eli Cohen2f5ff262017-01-03 23:55:21 +0200547 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300548 return minidx;
549}
550
Eli Cohenb037c292017-01-03 23:55:26 +0200551static int alloc_bfreg(struct mlx5_ib_dev *dev,
552 struct mlx5_bfreg_info *bfregi,
Eli Cohen2f5ff262017-01-03 23:55:21 +0200553 enum mlx5_ib_latency_class lat)
Eli Cohene126ba92013-07-07 17:25:49 +0300554{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200555 int bfregn = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300556
Eli Cohen2f5ff262017-01-03 23:55:21 +0200557 mutex_lock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300558 switch (lat) {
559 case MLX5_IB_LATENCY_CLASS_LOW:
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200560 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200561 bfregn = 0;
562 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300563 break;
564
565 case MLX5_IB_LATENCY_CLASS_MEDIUM:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200566 if (bfregi->ver < 2)
567 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200568 else
Eli Cohenb037c292017-01-03 23:55:26 +0200569 bfregn = alloc_med_class_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300570 break;
571
572 case MLX5_IB_LATENCY_CLASS_HIGH:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200573 if (bfregi->ver < 2)
574 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200575 else
Eli Cohenb037c292017-01-03 23:55:26 +0200576 bfregn = alloc_high_class_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300577 break;
578 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200579 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300580
Eli Cohen2f5ff262017-01-03 23:55:21 +0200581 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300582}
583
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200584void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300585{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200586 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200587 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200588 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300589}
590
591static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
592{
593 switch (state) {
594 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
595 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
596 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
597 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
598 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
599 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
600 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
601 default: return -1;
602 }
603}
604
605static int to_mlx5_st(enum ib_qp_type type)
606{
607 switch (type) {
608 case IB_QPT_RC: return MLX5_QP_ST_RC;
609 case IB_QPT_UC: return MLX5_QP_ST_UC;
610 case IB_QPT_UD: return MLX5_QP_ST_UD;
611 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
612 case IB_QPT_XRC_INI:
613 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
614 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200615 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Moni Shouac32a4f22018-01-02 16:19:32 +0200616 case MLX5_IB_QPT_DCI: return MLX5_QP_ST_DCI;
Eli Cohene126ba92013-07-07 17:25:49 +0300617 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300618 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200619 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300620 case IB_QPT_MAX:
621 default: return -EINVAL;
622 }
623}
624
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300625static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
626 struct mlx5_ib_cq *recv_cq);
627static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
628 struct mlx5_ib_cq *recv_cq);
629
Eli Cohenb037c292017-01-03 23:55:26 +0200630static int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200631 struct mlx5_bfreg_info *bfregi, int bfregn,
632 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300633{
Eli Cohenb037c292017-01-03 23:55:26 +0200634 int bfregs_per_sys_page;
635 int index_of_sys_page;
636 int offset;
637
638 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
639 MLX5_NON_FP_BFREGS_PER_UAR;
640 index_of_sys_page = bfregn / bfregs_per_sys_page;
641
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200642 if (dyn_bfreg) {
643 index_of_sys_page += bfregi->num_static_sys_pages;
644 if (bfregn > bfregi->num_dyn_bfregs ||
645 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
646 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
647 return -EINVAL;
648 }
649 }
Eli Cohenb037c292017-01-03 23:55:26 +0200650
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200651 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200652 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300653}
654
majd@mellanox.com19098df2016-01-14 19:13:03 +0200655static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev,
656 struct ib_pd *pd,
657 unsigned long addr, size_t size,
658 struct ib_umem **umem,
659 int *npages, int *page_shift, int *ncont,
660 u32 *offset)
661{
662 int err;
663
664 *umem = ib_umem_get(pd->uobject->context, addr, size, 0, 0);
665 if (IS_ERR(*umem)) {
666 mlx5_ib_dbg(dev, "umem_get failed\n");
667 return PTR_ERR(*umem);
668 }
669
Majd Dibbiny762f8992016-10-27 16:36:47 +0300670 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200671
672 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
673 if (err) {
674 mlx5_ib_warn(dev, "bad offset\n");
675 goto err_umem;
676 }
677
678 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
679 addr, size, *npages, *page_shift, *ncont, *offset);
680
681 return 0;
682
683err_umem:
684 ib_umem_release(*umem);
685 *umem = NULL;
686
687 return err;
688}
689
Maor Gottliebfe248c32017-05-30 10:29:14 +0300690static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
691 struct mlx5_ib_rwq *rwq)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300692{
693 struct mlx5_ib_ucontext *context;
694
Maor Gottliebfe248c32017-05-30 10:29:14 +0300695 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
696 atomic_dec(&dev->delay_drop.rqs_cnt);
697
Yishai Hadas79b20a62016-05-23 15:20:50 +0300698 context = to_mucontext(pd->uobject->context);
699 mlx5_ib_db_unmap_user(context, &rwq->db);
700 if (rwq->umem)
701 ib_umem_release(rwq->umem);
702}
703
704static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
705 struct mlx5_ib_rwq *rwq,
706 struct mlx5_ib_create_wq *ucmd)
707{
708 struct mlx5_ib_ucontext *context;
709 int page_shift = 0;
710 int npages;
711 u32 offset = 0;
712 int ncont = 0;
713 int err;
714
715 if (!ucmd->buf_addr)
716 return -EINVAL;
717
718 context = to_mucontext(pd->uobject->context);
719 rwq->umem = ib_umem_get(pd->uobject->context, ucmd->buf_addr,
720 rwq->buf_size, 0, 0);
721 if (IS_ERR(rwq->umem)) {
722 mlx5_ib_dbg(dev, "umem_get failed\n");
723 err = PTR_ERR(rwq->umem);
724 return err;
725 }
726
Majd Dibbiny762f8992016-10-27 16:36:47 +0300727 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300728 &ncont, NULL);
729 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
730 &rwq->rq_page_offset);
731 if (err) {
732 mlx5_ib_warn(dev, "bad offset\n");
733 goto err_umem;
734 }
735
736 rwq->rq_num_pas = ncont;
737 rwq->page_shift = page_shift;
738 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
739 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
740
741 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
742 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
743 npages, page_shift, ncont, offset);
744
745 err = mlx5_ib_db_map_user(context, ucmd->db_addr, &rwq->db);
746 if (err) {
747 mlx5_ib_dbg(dev, "map failed\n");
748 goto err_umem;
749 }
750
751 rwq->create_type = MLX5_WQ_USER;
752 return 0;
753
754err_umem:
755 ib_umem_release(rwq->umem);
756 return err;
757}
758
Eli Cohenb037c292017-01-03 23:55:26 +0200759static int adjust_bfregn(struct mlx5_ib_dev *dev,
760 struct mlx5_bfreg_info *bfregi, int bfregn)
761{
762 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
763 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
764}
765
Eli Cohene126ba92013-07-07 17:25:49 +0300766static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
767 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200768 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300769 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200770 struct mlx5_ib_create_qp_resp *resp, int *inlen,
771 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300772{
773 struct mlx5_ib_ucontext *context;
774 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200775 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200776 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200777 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300778 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200779 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200780 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200781 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300782 __be64 *pas;
783 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300784 int err;
785
786 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
787 if (err) {
788 mlx5_ib_dbg(dev, "copy failed\n");
789 return err;
790 }
791
792 context = to_mucontext(pd->uobject->context);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200793 if (ucmd.flags & MLX5_QP_FLAG_BFREG_INDEX) {
794 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
795 ucmd.bfreg_index, true);
796 if (uar_index < 0)
797 return uar_index;
798
799 bfregn = MLX5_IB_INVALID_BFREG;
800 } else if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL) {
801 /*
802 * TBD: should come from the verbs when we have the API
803 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200804 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200805 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200806 }
Leon Romanovsky051f2632015-12-20 12:16:11 +0200807 else {
Eli Cohenb037c292017-01-03 23:55:26 +0200808 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_HIGH);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200809 if (bfregn < 0) {
810 mlx5_ib_dbg(dev, "failed to allocate low latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200811 mlx5_ib_dbg(dev, "reverting to medium latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200812 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_MEDIUM);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200813 if (bfregn < 0) {
814 mlx5_ib_dbg(dev, "failed to allocate medium latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200815 mlx5_ib_dbg(dev, "reverting to high latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200816 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_LOW);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200817 if (bfregn < 0) {
818 mlx5_ib_warn(dev, "bfreg allocation failed\n");
819 return bfregn;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200820 }
Eli Cohenc1be5232014-01-14 17:45:12 +0200821 }
Eli Cohene126ba92013-07-07 17:25:49 +0300822 }
823 }
824
Eli Cohen2f5ff262017-01-03 23:55:21 +0200825 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200826 if (bfregn != MLX5_IB_INVALID_BFREG)
827 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
828 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300829
Haggai Eran48fea832014-05-22 14:50:11 +0300830 qp->rq.offset = 0;
831 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
832 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
833
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200834 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300835 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200836 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300837
majd@mellanox.com19098df2016-01-14 19:13:03 +0200838 if (ucmd.buf_addr && ubuffer->buf_size) {
839 ubuffer->buf_addr = ucmd.buf_addr;
840 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr,
841 ubuffer->buf_size,
842 &ubuffer->umem, &npages, &page_shift,
843 &ncont, &offset);
844 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200845 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200846 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200847 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300848 }
Eli Cohene126ba92013-07-07 17:25:49 +0300849
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300850 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
851 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300852 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300853 if (!*in) {
854 err = -ENOMEM;
855 goto err_umem;
856 }
Eli Cohene126ba92013-07-07 17:25:49 +0300857
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300858 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
859 if (ubuffer->umem)
860 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
861
862 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
863
864 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
865 MLX5_SET(qpc, qpc, page_offset, offset);
866
867 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200868 if (bfregn != MLX5_IB_INVALID_BFREG)
869 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
870 else
871 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200872 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300873
874 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
875 if (err) {
876 mlx5_ib_dbg(dev, "map failed\n");
877 goto err_free;
878 }
879
880 err = ib_copy_to_udata(udata, resp, sizeof(*resp));
881 if (err) {
882 mlx5_ib_dbg(dev, "copy failed\n");
883 goto err_unmap;
884 }
885 qp->create_type = MLX5_QP_USER;
886
887 return 0;
888
889err_unmap:
890 mlx5_ib_db_unmap_user(context, &qp->db);
891
892err_free:
Al Viro479163f2014-11-20 08:13:57 +0000893 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300894
895err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200896 if (ubuffer->umem)
897 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300898
Eli Cohen2f5ff262017-01-03 23:55:21 +0200899err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200900 if (bfregn != MLX5_IB_INVALID_BFREG)
901 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300902 return err;
903}
904
Eli Cohenb037c292017-01-03 23:55:26 +0200905static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
906 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300907{
908 struct mlx5_ib_ucontext *context;
909
910 context = to_mucontext(pd->uobject->context);
911 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200912 if (base->ubuffer.umem)
913 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200914
915 /*
916 * Free only the BFREGs which are handled by the kernel.
917 * BFREGs of UARs allocated dynamically are handled by user.
918 */
919 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
920 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300921}
922
923static int create_kernel_qp(struct mlx5_ib_dev *dev,
924 struct ib_qp_init_attr *init_attr,
925 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300926 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200927 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300928{
Eli Cohene126ba92013-07-07 17:25:49 +0300929 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300930 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300931 int err;
932
Erez Shitritf0313962016-02-21 16:27:17 +0200933 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
934 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200935 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +0300936 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200937 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200938 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300939
940 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200941 qp->bf.bfreg = &dev->fp_bfreg;
942 else
943 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300944
Eli Cohend8030b02017-02-09 19:31:47 +0200945 /* We need to divide by two since each register is comprised of
946 * two buffers of identical size, namely odd and even
947 */
948 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200949 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +0300950
951 err = calc_sq_size(dev, init_attr, qp);
952 if (err < 0) {
953 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200954 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300955 }
956
957 qp->rq.offset = 0;
958 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200959 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300960
majd@mellanox.com19098df2016-01-14 19:13:03 +0200961 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300962 if (err) {
963 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200964 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300965 }
966
967 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300968 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
969 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300970 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300971 if (!*in) {
972 err = -ENOMEM;
973 goto err_buf;
974 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300975
976 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
977 MLX5_SET(qpc, qpc, uar_page, uar_index);
978 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
979
Eli Cohene126ba92013-07-07 17:25:49 +0300980 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300981 MLX5_SET(qpc, qpc, fre, 1);
982 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300983
Haggai Eranb11a4f92016-02-29 15:45:03 +0200984 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300985 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200986 qp->flags |= MLX5_IB_QP_SQPN_QP1;
987 }
988
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300989 mlx5_fill_page_array(&qp->buf,
990 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300991
Jack Morgenstein9603b612014-07-28 23:30:22 +0300992 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300993 if (err) {
994 mlx5_ib_dbg(dev, "err %d\n", err);
995 goto err_free;
996 }
997
Li Dongyangb5883002017-08-16 23:31:22 +1000998 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
999 sizeof(*qp->sq.wrid), GFP_KERNEL);
1000 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1001 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1002 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1003 sizeof(*qp->rq.wrid), GFP_KERNEL);
1004 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1005 sizeof(*qp->sq.w_list), GFP_KERNEL);
1006 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1007 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001008
1009 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1010 !qp->sq.w_list || !qp->sq.wqe_head) {
1011 err = -ENOMEM;
1012 goto err_wrid;
1013 }
1014 qp->create_type = MLX5_QP_KERNEL;
1015
1016 return 0;
1017
1018err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001019 kvfree(qp->sq.wqe_head);
1020 kvfree(qp->sq.w_list);
1021 kvfree(qp->sq.wrid);
1022 kvfree(qp->sq.wr_data);
1023 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001024 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001025
1026err_free:
Al Viro479163f2014-11-20 08:13:57 +00001027 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001028
1029err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001030 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001031 return err;
1032}
1033
1034static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1035{
Li Dongyangb5883002017-08-16 23:31:22 +10001036 kvfree(qp->sq.wqe_head);
1037 kvfree(qp->sq.w_list);
1038 kvfree(qp->sq.wrid);
1039 kvfree(qp->sq.wr_data);
1040 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001041 mlx5_db_free(dev->mdev, &qp->db);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001042 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001043}
1044
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001045static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001046{
1047 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
Moni Shouac32a4f22018-01-02 16:19:32 +02001048 (attr->qp_type == MLX5_IB_QPT_DCI) ||
Eli Cohene126ba92013-07-07 17:25:49 +03001049 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001050 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001051 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001052 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001053 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001054 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001055}
1056
1057static int is_connected(enum ib_qp_type qp_type)
1058{
1059 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1060 return 1;
1061
1062 return 0;
1063}
1064
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001065static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001066 struct mlx5_ib_qp *qp,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001067 struct mlx5_ib_sq *sq, u32 tdn)
1068{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001069 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001070 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1071
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001072 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001073 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1074 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1075
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001076 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1077}
1078
1079static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1080 struct mlx5_ib_sq *sq)
1081{
1082 mlx5_core_destroy_tis(dev->mdev, sq->tisn);
1083}
1084
1085static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1086 struct mlx5_ib_sq *sq, void *qpin,
1087 struct ib_pd *pd)
1088{
1089 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1090 __be64 *pas;
1091 void *in;
1092 void *sqc;
1093 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1094 void *wq;
1095 int inlen;
1096 int err;
1097 int page_shift = 0;
1098 int npages;
1099 int ncont = 0;
1100 u32 offset = 0;
1101
1102 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1103 &sq->ubuffer.umem, &npages, &page_shift,
1104 &ncont, &offset);
1105 if (err)
1106 return err;
1107
1108 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001109 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001110 if (!in) {
1111 err = -ENOMEM;
1112 goto err_umem;
1113 }
1114
1115 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1116 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001117 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1118 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001119 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1120 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1121 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1122 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1123 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001124 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1125 MLX5_CAP_ETH(dev->mdev, swp))
1126 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001127
1128 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1129 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1130 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1131 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1132 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1133 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1134 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1135 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1136 MLX5_SET(wq, wq, page_offset, offset);
1137
1138 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1139 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1140
1141 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1142
1143 kvfree(in);
1144
1145 if (err)
1146 goto err_umem;
1147
1148 return 0;
1149
1150err_umem:
1151 ib_umem_release(sq->ubuffer.umem);
1152 sq->ubuffer.umem = NULL;
1153
1154 return err;
1155}
1156
1157static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1158 struct mlx5_ib_sq *sq)
1159{
1160 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1161 ib_umem_release(sq->ubuffer.umem);
1162}
1163
1164static int get_rq_pas_size(void *qpc)
1165{
1166 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1167 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1168 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1169 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1170 u32 po_quanta = 1 << (log_page_size - 6);
1171 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1172 u32 page_size = 1 << log_page_size;
1173 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1174 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1175
1176 return rq_num_pas * sizeof(u64);
1177}
1178
1179static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1180 struct mlx5_ib_rq *rq, void *qpin)
1181{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001182 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001183 __be64 *pas;
1184 __be64 *qp_pas;
1185 void *in;
1186 void *rqc;
1187 void *wq;
1188 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1189 int inlen;
1190 int err;
1191 u32 rq_pas_size = get_rq_pas_size(qpc);
1192
1193 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001194 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001195 if (!in)
1196 return -ENOMEM;
1197
1198 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001199 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1200 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001201 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1202 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1203 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1204 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1205 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1206
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001207 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1208 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1209
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001210 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1211 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001212 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1213 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001214 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1215 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1216 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1217 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1218 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1219 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1220
1221 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1222 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1223 memcpy(pas, qp_pas, rq_pas_size);
1224
1225 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1226
1227 kvfree(in);
1228
1229 return err;
1230}
1231
1232static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1233 struct mlx5_ib_rq *rq)
1234{
1235 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1236}
1237
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001238static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1239{
1240 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1241 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1242 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1243}
1244
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001245static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001246 struct mlx5_ib_rq *rq, u32 tdn,
1247 bool tunnel_offload_en)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001248{
1249 u32 *in;
1250 void *tirc;
1251 int inlen;
1252 int err;
1253
1254 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001255 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001256 if (!in)
1257 return -ENOMEM;
1258
1259 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1260 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1261 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1262 MLX5_SET(tirc, tirc, transport_domain, tdn);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001263 if (tunnel_offload_en)
1264 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001265
1266 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1267
1268 kvfree(in);
1269
1270 return err;
1271}
1272
1273static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1274 struct mlx5_ib_rq *rq)
1275{
1276 mlx5_core_destroy_tir(dev->mdev, rq->tirn);
1277}
1278
1279static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001280 u32 *in,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001281 struct ib_pd *pd)
1282{
1283 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1284 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1285 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1286 struct ib_uobject *uobj = pd->uobject;
1287 struct ib_ucontext *ucontext = uobj->context;
1288 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1289 int err;
1290 u32 tdn = mucontext->tdn;
1291
1292 if (qp->sq.wqe_cnt) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001293 err = create_raw_packet_qp_tis(dev, qp, sq, tdn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001294 if (err)
1295 return err;
1296
1297 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1298 if (err)
1299 goto err_destroy_tis;
1300
1301 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001302 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001303 }
1304
1305 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001306 rq->base.container_mibqp = qp;
1307
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001308 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1309 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001310 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1311 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001312 err = create_raw_packet_qp_rq(dev, rq, in);
1313 if (err)
1314 goto err_destroy_sq;
1315
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001316
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001317 err = create_raw_packet_qp_tir(dev, rq, tdn,
1318 qp->tunnel_offload_en);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001319 if (err)
1320 goto err_destroy_rq;
1321 }
1322
1323 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1324 rq->base.mqp.qpn;
1325
1326 return 0;
1327
1328err_destroy_rq:
1329 destroy_raw_packet_qp_rq(dev, rq);
1330err_destroy_sq:
1331 if (!qp->sq.wqe_cnt)
1332 return err;
1333 destroy_raw_packet_qp_sq(dev, sq);
1334err_destroy_tis:
1335 destroy_raw_packet_qp_tis(dev, sq);
1336
1337 return err;
1338}
1339
1340static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1341 struct mlx5_ib_qp *qp)
1342{
1343 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1344 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1345 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1346
1347 if (qp->rq.wqe_cnt) {
1348 destroy_raw_packet_qp_tir(dev, rq);
1349 destroy_raw_packet_qp_rq(dev, rq);
1350 }
1351
1352 if (qp->sq.wqe_cnt) {
1353 destroy_raw_packet_qp_sq(dev, sq);
1354 destroy_raw_packet_qp_tis(dev, sq);
1355 }
1356}
1357
1358static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1359 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1360{
1361 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1362 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1363
1364 sq->sq = &qp->sq;
1365 rq->rq = &qp->rq;
1366 sq->doorbell = &qp->db;
1367 rq->doorbell = &qp->db;
1368}
1369
Yishai Hadas28d61372016-05-23 15:20:56 +03001370static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1371{
1372 mlx5_core_destroy_tir(dev->mdev, qp->rss_qp.tirn);
1373}
1374
1375static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1376 struct ib_pd *pd,
1377 struct ib_qp_init_attr *init_attr,
1378 struct ib_udata *udata)
1379{
1380 struct ib_uobject *uobj = pd->uobject;
1381 struct ib_ucontext *ucontext = uobj->context;
1382 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1383 struct mlx5_ib_create_qp_resp resp = {};
1384 int inlen;
1385 int err;
1386 u32 *in;
1387 void *tirc;
1388 void *hfso;
1389 u32 selected_fields = 0;
1390 size_t min_resp_len;
1391 u32 tdn = mucontext->tdn;
1392 struct mlx5_ib_create_qp_rss ucmd = {};
1393 size_t required_cmd_sz;
1394
1395 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1396 return -EOPNOTSUPP;
1397
1398 if (init_attr->create_flags || init_attr->send_cq)
1399 return -EINVAL;
1400
Eli Cohen2f5ff262017-01-03 23:55:21 +02001401 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001402 if (udata->outlen < min_resp_len)
1403 return -EINVAL;
1404
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001405 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001406 if (udata->inlen < required_cmd_sz) {
1407 mlx5_ib_dbg(dev, "invalid inlen\n");
1408 return -EINVAL;
1409 }
1410
1411 if (udata->inlen > sizeof(ucmd) &&
1412 !ib_is_udata_cleared(udata, sizeof(ucmd),
1413 udata->inlen - sizeof(ucmd))) {
1414 mlx5_ib_dbg(dev, "inlen is not supported\n");
1415 return -EOPNOTSUPP;
1416 }
1417
1418 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1419 mlx5_ib_dbg(dev, "copy failed\n");
1420 return -EFAULT;
1421 }
1422
1423 if (ucmd.comp_mask) {
1424 mlx5_ib_dbg(dev, "invalid comp mask\n");
1425 return -EOPNOTSUPP;
1426 }
1427
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001428 if (ucmd.flags & ~MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
1429 mlx5_ib_dbg(dev, "invalid flags\n");
1430 return -EOPNOTSUPP;
1431 }
1432
1433 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1434 !tunnel_offload_supported(dev->mdev)) {
1435 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001436 return -EOPNOTSUPP;
1437 }
1438
Maor Gottlieb309fa342017-10-19 08:25:56 +03001439 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1440 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1441 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1442 return -EOPNOTSUPP;
1443 }
1444
Yishai Hadas28d61372016-05-23 15:20:56 +03001445 err = ib_copy_to_udata(udata, &resp, min_resp_len);
1446 if (err) {
1447 mlx5_ib_dbg(dev, "copy failed\n");
1448 return -EINVAL;
1449 }
1450
1451 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001452 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001453 if (!in)
1454 return -ENOMEM;
1455
1456 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1457 MLX5_SET(tirc, tirc, disp_type,
1458 MLX5_TIRC_DISP_TYPE_INDIRECT);
1459 MLX5_SET(tirc, tirc, indirect_table,
1460 init_attr->rwq_ind_tbl->ind_tbl_num);
1461 MLX5_SET(tirc, tirc, transport_domain, tdn);
1462
1463 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001464
1465 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1466 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1467
Maor Gottlieb309fa342017-10-19 08:25:56 +03001468 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1469 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1470 else
1471 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1472
Yishai Hadas28d61372016-05-23 15:20:56 +03001473 switch (ucmd.rx_hash_function) {
1474 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1475 {
1476 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1477 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1478
1479 if (len != ucmd.rx_key_len) {
1480 err = -EINVAL;
1481 goto err;
1482 }
1483
1484 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1485 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1486 memcpy(rss_key, ucmd.rx_hash_key, len);
1487 break;
1488 }
1489 default:
1490 err = -EOPNOTSUPP;
1491 goto err;
1492 }
1493
1494 if (!ucmd.rx_hash_fields_mask) {
1495 /* special case when this TIR serves as steering entry without hashing */
1496 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1497 goto create_tir;
1498 err = -EINVAL;
1499 goto err;
1500 }
1501
1502 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1503 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1504 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1505 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1506 err = -EINVAL;
1507 goto err;
1508 }
1509
1510 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1511 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1512 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1513 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1514 MLX5_L3_PROT_TYPE_IPV4);
1515 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1516 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1517 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1518 MLX5_L3_PROT_TYPE_IPV6);
1519
1520 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1521 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) &&
1522 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1523 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))) {
1524 err = -EINVAL;
1525 goto err;
1526 }
1527
1528 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1529 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1530 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1531 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1532 MLX5_L4_PROT_TYPE_TCP);
1533 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1534 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1535 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1536 MLX5_L4_PROT_TYPE_UDP);
1537
1538 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1539 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1540 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1541
1542 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1543 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1544 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1545
1546 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1547 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1548 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1549
1550 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1551 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1552 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1553
1554 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1555
1556create_tir:
1557 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1558
1559 if (err)
1560 goto err;
1561
1562 kvfree(in);
1563 /* qpn is reserved for that QP */
1564 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001565 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001566 return 0;
1567
1568err:
1569 kvfree(in);
1570 return err;
1571}
1572
Eli Cohene126ba92013-07-07 17:25:49 +03001573static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1574 struct ib_qp_init_attr *init_attr,
1575 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1576{
1577 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001578 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001579 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001580 struct mlx5_ib_create_qp_resp resp;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001581 struct mlx5_ib_cq *send_cq;
1582 struct mlx5_ib_cq *recv_cq;
1583 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001584 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001585 struct mlx5_ib_create_qp ucmd;
1586 struct mlx5_ib_qp_base *base;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001587 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001588 u32 *in;
1589 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001590
1591 mutex_init(&qp->mutex);
1592 spin_lock_init(&qp->sq.lock);
1593 spin_lock_init(&qp->rq.lock);
1594
Yishai Hadas28d61372016-05-23 15:20:56 +03001595 if (init_attr->rwq_ind_tbl) {
1596 if (!udata)
1597 return -ENOSYS;
1598
1599 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1600 return err;
1601 }
1602
Eli Cohenf360d882014-04-02 00:10:16 +03001603 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001604 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001605 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1606 return -EINVAL;
1607 } else {
1608 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1609 }
1610 }
1611
Leon Romanovsky051f2632015-12-20 12:16:11 +02001612 if (init_attr->create_flags &
1613 (IB_QP_CREATE_CROSS_CHANNEL |
1614 IB_QP_CREATE_MANAGED_SEND |
1615 IB_QP_CREATE_MANAGED_RECV)) {
1616 if (!MLX5_CAP_GEN(mdev, cd)) {
1617 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1618 return -EINVAL;
1619 }
1620 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1621 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1622 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1623 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1624 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1625 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1626 }
Erez Shitritf0313962016-02-21 16:27:17 +02001627
1628 if (init_attr->qp_type == IB_QPT_UD &&
1629 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1630 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1631 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1632 return -EOPNOTSUPP;
1633 }
1634
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001635 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1636 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1637 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1638 return -EOPNOTSUPP;
1639 }
1640 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1641 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1642 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1643 return -EOPNOTSUPP;
1644 }
1645 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1646 }
1647
Eli Cohene126ba92013-07-07 17:25:49 +03001648 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1649 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1650
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001651 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
1652 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1653 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
1654 (init_attr->qp_type != IB_QPT_RAW_PACKET))
1655 return -EOPNOTSUPP;
1656 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
1657 }
1658
Eli Cohene126ba92013-07-07 17:25:49 +03001659 if (pd && pd->uobject) {
1660 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1661 mlx5_ib_dbg(dev, "copy failed\n");
1662 return -EFAULT;
1663 }
1664
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001665 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1666 &ucmd, udata->inlen, &uidx);
1667 if (err)
1668 return err;
1669
Eli Cohene126ba92013-07-07 17:25:49 +03001670 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1671 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001672 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
1673 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
1674 !tunnel_offload_supported(mdev)) {
1675 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
1676 return -EOPNOTSUPP;
1677 }
1678 qp->tunnel_offload_en = true;
1679 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001680
1681 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
1682 if (init_attr->qp_type != IB_QPT_UD ||
1683 (MLX5_CAP_GEN(dev->mdev, port_type) !=
1684 MLX5_CAP_PORT_TYPE_IB) ||
1685 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
1686 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
1687 return -EOPNOTSUPP;
1688 }
1689
1690 qp->flags |= MLX5_IB_QP_UNDERLAY;
1691 qp->underlay_qpn = init_attr->source_qpn;
1692 }
Eli Cohene126ba92013-07-07 17:25:49 +03001693 } else {
1694 qp->wq_sig = !!wq_signature;
1695 }
1696
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001697 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1698 qp->flags & MLX5_IB_QP_UNDERLAY) ?
1699 &qp->raw_packet_qp.rq.base :
1700 &qp->trans_qp.base;
1701
Eli Cohene126ba92013-07-07 17:25:49 +03001702 qp->has_rq = qp_has_rq(init_attr);
1703 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1704 qp, (pd && pd->uobject) ? &ucmd : NULL);
1705 if (err) {
1706 mlx5_ib_dbg(dev, "err %d\n", err);
1707 return err;
1708 }
1709
1710 if (pd) {
1711 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001712 __u32 max_wqes =
1713 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001714 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1715 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1716 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1717 mlx5_ib_dbg(dev, "invalid rq params\n");
1718 return -EINVAL;
1719 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001720 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001721 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001722 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001723 return -EINVAL;
1724 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001725 if (init_attr->create_flags &
1726 mlx5_ib_create_qp_sqpn_qp1()) {
1727 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1728 return -EINVAL;
1729 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001730 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1731 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001732 if (err)
1733 mlx5_ib_dbg(dev, "err %d\n", err);
1734 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001735 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1736 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001737 if (err)
1738 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001739 }
1740
1741 if (err)
1742 return err;
1743 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001744 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001745 if (!in)
1746 return -ENOMEM;
1747
1748 qp->create_type = MLX5_QP_EMPTY;
1749 }
1750
1751 if (is_sqp(init_attr->qp_type))
1752 qp->port = init_attr->port_num;
1753
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001754 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1755
1756 MLX5_SET(qpc, qpc, st, to_mlx5_st(init_attr->qp_type));
1757 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001758
1759 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001760 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001761 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001762 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1763
Eli Cohene126ba92013-07-07 17:25:49 +03001764
1765 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001766 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001767
Eli Cohenf360d882014-04-02 00:10:16 +03001768 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001769 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001770
Leon Romanovsky051f2632015-12-20 12:16:11 +02001771 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001772 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001773 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001774 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001775 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001776 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001777
Eli Cohene126ba92013-07-07 17:25:49 +03001778 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1779 int rcqe_sz;
1780 int scqe_sz;
1781
1782 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1783 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1784
1785 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001786 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001787 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001788 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001789
1790 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1791 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001792 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001793 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001794 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001795 }
1796 }
1797
1798 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001799 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1800 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001801 }
1802
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001803 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001804
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001805 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001806 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001807 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001808 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001809 if (init_attr->srq &&
1810 init_attr->srq->srq_type == IB_SRQT_TM)
1811 MLX5_SET(qpc, qpc, offload_type,
1812 MLX5_QPC_OFFLOAD_TYPE_RNDV);
1813 }
Eli Cohene126ba92013-07-07 17:25:49 +03001814
1815 /* Set default resources */
1816 switch (init_attr->qp_type) {
1817 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001818 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1819 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1820 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1821 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001822 break;
1823 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001824 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1825 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1826 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001827 break;
1828 default:
1829 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001830 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1831 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001832 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001833 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1834 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001835 }
1836 }
1837
1838 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001839 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001840
1841 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001842 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001843
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001844 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001845
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001846 /* 0xffffff means we ask to work with cqe version 0 */
1847 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001848 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001849
Erez Shitritf0313962016-02-21 16:27:17 +02001850 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1851 if (init_attr->qp_type == IB_QPT_UD &&
1852 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001853 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1854 qp->flags |= MLX5_IB_QP_LSO;
1855 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001856
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001857 if (init_attr->create_flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
1858 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
1859 mlx5_ib_dbg(dev, "scatter end padding is not supported\n");
1860 err = -EOPNOTSUPP;
1861 goto err;
1862 } else if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1863 MLX5_SET(qpc, qpc, end_padding_mode,
1864 MLX5_WQ_END_PAD_MODE_ALIGN);
1865 } else {
1866 qp->flags |= MLX5_IB_QP_PCI_WRITE_END_PADDING;
1867 }
1868 }
1869
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001870 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1871 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001872 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
1873 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
1874 err = create_raw_packet_qp(dev, qp, in, pd);
1875 } else {
1876 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
1877 }
1878
Eli Cohene126ba92013-07-07 17:25:49 +03001879 if (err) {
1880 mlx5_ib_dbg(dev, "create qp failed\n");
1881 goto err_create;
1882 }
1883
Al Viro479163f2014-11-20 08:13:57 +00001884 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001885
majd@mellanox.com19098df2016-01-14 19:13:03 +02001886 base->container_mibqp = qp;
1887 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03001888
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001889 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
1890 &send_cq, &recv_cq);
1891 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1892 mlx5_ib_lock_cqs(send_cq, recv_cq);
1893 /* Maintain device to QPs access, needed for further handling via reset
1894 * flow
1895 */
1896 list_add_tail(&qp->qps_list, &dev->qp_list);
1897 /* Maintain CQ to QPs access, needed for further handling via reset flow
1898 */
1899 if (send_cq)
1900 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
1901 if (recv_cq)
1902 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
1903 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1904 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1905
Eli Cohene126ba92013-07-07 17:25:49 +03001906 return 0;
1907
1908err_create:
1909 if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02001910 destroy_qp_user(dev, pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001911 else if (qp->create_type == MLX5_QP_KERNEL)
1912 destroy_qp_kernel(dev, qp);
1913
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001914err:
Al Viro479163f2014-11-20 08:13:57 +00001915 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001916 return err;
1917}
1918
1919static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1920 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1921{
1922 if (send_cq) {
1923 if (recv_cq) {
1924 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001925 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001926 spin_lock_nested(&recv_cq->lock,
1927 SINGLE_DEPTH_NESTING);
1928 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001929 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001930 __acquire(&recv_cq->lock);
1931 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001932 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001933 spin_lock_nested(&send_cq->lock,
1934 SINGLE_DEPTH_NESTING);
1935 }
1936 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001937 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001938 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001939 }
1940 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001941 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001942 __acquire(&send_cq->lock);
1943 } else {
1944 __acquire(&send_cq->lock);
1945 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001946 }
1947}
1948
1949static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1950 __releases(&send_cq->lock) __releases(&recv_cq->lock)
1951{
1952 if (send_cq) {
1953 if (recv_cq) {
1954 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1955 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001956 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001957 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1958 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001959 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001960 } else {
1961 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001962 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001963 }
1964 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001965 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001966 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001967 }
1968 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001969 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001970 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001971 } else {
1972 __release(&recv_cq->lock);
1973 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001974 }
1975}
1976
1977static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
1978{
1979 return to_mpd(qp->ibqp.pd);
1980}
1981
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001982static void get_cqs(enum ib_qp_type qp_type,
1983 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03001984 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
1985{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001986 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03001987 case IB_QPT_XRC_TGT:
1988 *send_cq = NULL;
1989 *recv_cq = NULL;
1990 break;
1991 case MLX5_IB_QPT_REG_UMR:
1992 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001993 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001994 *recv_cq = NULL;
1995 break;
1996
1997 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02001998 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03001999 case IB_QPT_RC:
2000 case IB_QPT_UC:
2001 case IB_QPT_UD:
2002 case IB_QPT_RAW_IPV6:
2003 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002004 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002005 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2006 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002007 break;
2008
Eli Cohene126ba92013-07-07 17:25:49 +03002009 case IB_QPT_MAX:
2010 default:
2011 *send_cq = NULL;
2012 *recv_cq = NULL;
2013 break;
2014 }
2015}
2016
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002017static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002018 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2019 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002020
Eli Cohene126ba92013-07-07 17:25:49 +03002021static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
2022{
2023 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002024 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002025 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002026 int err;
2027
Yishai Hadas28d61372016-05-23 15:20:56 +03002028 if (qp->ibqp.rwq_ind_tbl) {
2029 destroy_rss_raw_qp_tir(dev, qp);
2030 return;
2031 }
2032
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002033 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2034 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002035 &qp->raw_packet_qp.rq.base :
2036 &qp->trans_qp.base;
2037
Haggai Eran6aec21f2014-12-11 17:04:23 +02002038 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002039 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2040 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002041 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002042 MLX5_CMD_OP_2RST_QP, 0,
2043 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002044 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002045 struct mlx5_modify_raw_qp_param raw_qp_param = {
2046 .operation = MLX5_CMD_OP_2RST_QP
2047 };
2048
Aviv Heller13eab212016-09-18 20:48:04 +03002049 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002050 }
2051 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002052 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002053 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002054 }
Eli Cohene126ba92013-07-07 17:25:49 +03002055
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002056 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2057 &send_cq, &recv_cq);
2058
2059 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2060 mlx5_ib_lock_cqs(send_cq, recv_cq);
2061 /* del from lists under both locks above to protect reset flow paths */
2062 list_del(&qp->qps_list);
2063 if (send_cq)
2064 list_del(&qp->cq_send_list);
2065
2066 if (recv_cq)
2067 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002068
2069 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002070 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002071 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2072 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002073 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2074 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002075 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002076 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2077 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002078
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002079 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2080 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002081 destroy_raw_packet_qp(dev, qp);
2082 } else {
2083 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
2084 if (err)
2085 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2086 base->mqp.qpn);
2087 }
Eli Cohene126ba92013-07-07 17:25:49 +03002088
Eli Cohene126ba92013-07-07 17:25:49 +03002089 if (qp->create_type == MLX5_QP_KERNEL)
2090 destroy_qp_kernel(dev, qp);
2091 else if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02002092 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002093}
2094
2095static const char *ib_qp_type_str(enum ib_qp_type type)
2096{
2097 switch (type) {
2098 case IB_QPT_SMI:
2099 return "IB_QPT_SMI";
2100 case IB_QPT_GSI:
2101 return "IB_QPT_GSI";
2102 case IB_QPT_RC:
2103 return "IB_QPT_RC";
2104 case IB_QPT_UC:
2105 return "IB_QPT_UC";
2106 case IB_QPT_UD:
2107 return "IB_QPT_UD";
2108 case IB_QPT_RAW_IPV6:
2109 return "IB_QPT_RAW_IPV6";
2110 case IB_QPT_RAW_ETHERTYPE:
2111 return "IB_QPT_RAW_ETHERTYPE";
2112 case IB_QPT_XRC_INI:
2113 return "IB_QPT_XRC_INI";
2114 case IB_QPT_XRC_TGT:
2115 return "IB_QPT_XRC_TGT";
2116 case IB_QPT_RAW_PACKET:
2117 return "IB_QPT_RAW_PACKET";
2118 case MLX5_IB_QPT_REG_UMR:
2119 return "MLX5_IB_QPT_REG_UMR";
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002120 case IB_QPT_DRIVER:
2121 return "IB_QPT_DRIVER";
Eli Cohene126ba92013-07-07 17:25:49 +03002122 case IB_QPT_MAX:
2123 default:
2124 return "Invalid QP type";
2125 }
2126}
2127
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002128static struct ib_qp *mlx5_ib_create_dct(struct ib_pd *pd,
2129 struct ib_qp_init_attr *attr,
2130 struct mlx5_ib_create_qp *ucmd)
2131{
2132 struct mlx5_ib_dev *dev;
2133 struct mlx5_ib_qp *qp;
2134 int err = 0;
2135 u32 uidx = MLX5_IB_DEFAULT_UIDX;
2136 void *dctc;
2137
2138 if (!attr->srq || !attr->recv_cq)
2139 return ERR_PTR(-EINVAL);
2140
2141 dev = to_mdev(pd->device);
2142
2143 err = get_qp_user_index(to_mucontext(pd->uobject->context),
2144 ucmd, sizeof(*ucmd), &uidx);
2145 if (err)
2146 return ERR_PTR(err);
2147
2148 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2149 if (!qp)
2150 return ERR_PTR(-ENOMEM);
2151
2152 qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
2153 if (!qp->dct.in) {
2154 err = -ENOMEM;
2155 goto err_free;
2156 }
2157
2158 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
Moni Shoua776a3902018-01-02 16:19:33 +02002159 qp->qp_sub_type = MLX5_IB_QPT_DCT;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002160 MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
2161 MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
2162 MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
2163 MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
2164 MLX5_SET(dctc, dctc, user_index, uidx);
2165
2166 qp->state = IB_QPS_RESET;
2167
2168 return &qp->ibqp;
2169err_free:
2170 kfree(qp);
2171 return ERR_PTR(err);
2172}
2173
2174static int set_mlx_qp_type(struct mlx5_ib_dev *dev,
2175 struct ib_qp_init_attr *init_attr,
2176 struct mlx5_ib_create_qp *ucmd,
2177 struct ib_udata *udata)
2178{
2179 enum { MLX_QP_FLAGS = MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI };
2180 int err;
2181
2182 if (!udata)
2183 return -EINVAL;
2184
2185 if (udata->inlen < sizeof(*ucmd)) {
2186 mlx5_ib_dbg(dev, "create_qp user command is smaller than expected\n");
2187 return -EINVAL;
2188 }
2189 err = ib_copy_from_udata(ucmd, udata, sizeof(*ucmd));
2190 if (err)
2191 return err;
2192
2193 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCI) {
2194 init_attr->qp_type = MLX5_IB_QPT_DCI;
2195 } else {
2196 if ((ucmd->flags & MLX_QP_FLAGS) == MLX5_QP_FLAG_TYPE_DCT) {
2197 init_attr->qp_type = MLX5_IB_QPT_DCT;
2198 } else {
2199 mlx5_ib_dbg(dev, "Invalid QP flags\n");
2200 return -EINVAL;
2201 }
2202 }
2203
2204 if (!MLX5_CAP_GEN(dev->mdev, dct)) {
2205 mlx5_ib_dbg(dev, "DC transport is not supported\n");
2206 return -EOPNOTSUPP;
2207 }
2208
2209 return 0;
2210}
2211
Eli Cohene126ba92013-07-07 17:25:49 +03002212struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002213 struct ib_qp_init_attr *verbs_init_attr,
Eli Cohene126ba92013-07-07 17:25:49 +03002214 struct ib_udata *udata)
2215{
2216 struct mlx5_ib_dev *dev;
2217 struct mlx5_ib_qp *qp;
2218 u16 xrcdn = 0;
2219 int err;
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002220 struct ib_qp_init_attr mlx_init_attr;
2221 struct ib_qp_init_attr *init_attr = verbs_init_attr;
Eli Cohene126ba92013-07-07 17:25:49 +03002222
2223 if (pd) {
2224 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002225
2226 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2227 if (!pd->uobject) {
2228 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2229 return ERR_PTR(-EINVAL);
2230 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2231 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2232 return ERR_PTR(-EINVAL);
2233 }
2234 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002235 } else {
2236 /* being cautious here */
2237 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2238 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2239 pr_warn("%s: no PD for transport %s\n", __func__,
2240 ib_qp_type_str(init_attr->qp_type));
2241 return ERR_PTR(-EINVAL);
2242 }
2243 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002244 }
2245
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002246 if (init_attr->qp_type == IB_QPT_DRIVER) {
2247 struct mlx5_ib_create_qp ucmd;
2248
2249 init_attr = &mlx_init_attr;
2250 memcpy(init_attr, verbs_init_attr, sizeof(*verbs_init_attr));
2251 err = set_mlx_qp_type(dev, init_attr, &ucmd, udata);
2252 if (err)
2253 return ERR_PTR(err);
Moni Shouac32a4f22018-01-02 16:19:32 +02002254
2255 if (init_attr->qp_type == MLX5_IB_QPT_DCI) {
2256 if (init_attr->cap.max_recv_wr ||
2257 init_attr->cap.max_recv_sge) {
2258 mlx5_ib_dbg(dev, "DCI QP requires zero size receive queue\n");
2259 return ERR_PTR(-EINVAL);
2260 }
Moni Shoua776a3902018-01-02 16:19:33 +02002261 } else {
2262 return mlx5_ib_create_dct(pd, init_attr, &ucmd);
Moni Shouac32a4f22018-01-02 16:19:32 +02002263 }
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002264 }
2265
Eli Cohene126ba92013-07-07 17:25:49 +03002266 switch (init_attr->qp_type) {
2267 case IB_QPT_XRC_TGT:
2268 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002269 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002270 mlx5_ib_dbg(dev, "XRC not supported\n");
2271 return ERR_PTR(-ENOSYS);
2272 }
2273 init_attr->recv_cq = NULL;
2274 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2275 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2276 init_attr->send_cq = NULL;
2277 }
2278
2279 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002280 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002281 case IB_QPT_RC:
2282 case IB_QPT_UC:
2283 case IB_QPT_UD:
2284 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002285 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002286 case MLX5_IB_QPT_REG_UMR:
Moni Shouac32a4f22018-01-02 16:19:32 +02002287 case MLX5_IB_QPT_DCI:
Eli Cohene126ba92013-07-07 17:25:49 +03002288 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2289 if (!qp)
2290 return ERR_PTR(-ENOMEM);
2291
2292 err = create_qp_common(dev, pd, init_attr, udata, qp);
2293 if (err) {
2294 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2295 kfree(qp);
2296 return ERR_PTR(err);
2297 }
2298
2299 if (is_qp0(init_attr->qp_type))
2300 qp->ibqp.qp_num = 0;
2301 else if (is_qp1(init_attr->qp_type))
2302 qp->ibqp.qp_num = 1;
2303 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002304 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002305
2306 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 +02002307 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002308 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2309 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002310
majd@mellanox.com19098df2016-01-14 19:13:03 +02002311 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002312
2313 break;
2314
Haggai Erand16e91d2016-02-29 15:45:05 +02002315 case IB_QPT_GSI:
2316 return mlx5_ib_gsi_create_qp(pd, init_attr);
2317
Eli Cohene126ba92013-07-07 17:25:49 +03002318 case IB_QPT_RAW_IPV6:
2319 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002320 case IB_QPT_MAX:
2321 default:
2322 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2323 init_attr->qp_type);
2324 /* Don't support raw QPs */
2325 return ERR_PTR(-EINVAL);
2326 }
2327
Moni Shouab4aaa1f2018-01-02 16:19:31 +02002328 if (verbs_init_attr->qp_type == IB_QPT_DRIVER)
2329 qp->qp_sub_type = init_attr->qp_type;
2330
Eli Cohene126ba92013-07-07 17:25:49 +03002331 return &qp->ibqp;
2332}
2333
Moni Shoua776a3902018-01-02 16:19:33 +02002334static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
2335{
2336 struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);
2337
2338 if (mqp->state == IB_QPS_RTR) {
2339 int err;
2340
2341 err = mlx5_core_destroy_dct(dev->mdev, &mqp->dct.mdct);
2342 if (err) {
2343 mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
2344 return err;
2345 }
2346 }
2347
2348 kfree(mqp->dct.in);
2349 kfree(mqp);
2350 return 0;
2351}
2352
Eli Cohene126ba92013-07-07 17:25:49 +03002353int mlx5_ib_destroy_qp(struct ib_qp *qp)
2354{
2355 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2356 struct mlx5_ib_qp *mqp = to_mqp(qp);
2357
Haggai Erand16e91d2016-02-29 15:45:05 +02002358 if (unlikely(qp->qp_type == IB_QPT_GSI))
2359 return mlx5_ib_gsi_destroy_qp(qp);
2360
Moni Shoua776a3902018-01-02 16:19:33 +02002361 if (mqp->qp_sub_type == MLX5_IB_QPT_DCT)
2362 return mlx5_ib_destroy_dct(mqp);
2363
Eli Cohene126ba92013-07-07 17:25:49 +03002364 destroy_qp_common(dev, mqp);
2365
2366 kfree(mqp);
2367
2368 return 0;
2369}
2370
2371static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2372 int attr_mask)
2373{
2374 u32 hw_access_flags = 0;
2375 u8 dest_rd_atomic;
2376 u32 access_flags;
2377
2378 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2379 dest_rd_atomic = attr->max_dest_rd_atomic;
2380 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002381 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002382
2383 if (attr_mask & IB_QP_ACCESS_FLAGS)
2384 access_flags = attr->qp_access_flags;
2385 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002386 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002387
2388 if (!dest_rd_atomic)
2389 access_flags &= IB_ACCESS_REMOTE_WRITE;
2390
2391 if (access_flags & IB_ACCESS_REMOTE_READ)
2392 hw_access_flags |= MLX5_QP_BIT_RRE;
2393 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2394 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2395 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2396 hw_access_flags |= MLX5_QP_BIT_RWE;
2397
2398 return cpu_to_be32(hw_access_flags);
2399}
2400
2401enum {
2402 MLX5_PATH_FLAG_FL = 1 << 0,
2403 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2404 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2405};
2406
2407static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2408{
2409 if (rate == IB_RATE_PORT_CURRENT) {
2410 return 0;
2411 } else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
2412 return -EINVAL;
2413 } else {
2414 while (rate != IB_RATE_2_5_GBPS &&
2415 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
Saeed Mahameed938fe832015-05-28 22:28:41 +03002416 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
Eli Cohene126ba92013-07-07 17:25:49 +03002417 --rate;
2418 }
2419
2420 return rate + MLX5_STAT_RATE_OFFSET;
2421}
2422
majd@mellanox.com75850d02016-01-14 19:13:06 +02002423static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
2424 struct mlx5_ib_sq *sq, u8 sl)
2425{
2426 void *in;
2427 void *tisc;
2428 int inlen;
2429 int err;
2430
2431 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002432 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002433 if (!in)
2434 return -ENOMEM;
2435
2436 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
2437
2438 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2439 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2440
2441 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2442
2443 kvfree(in);
2444
2445 return err;
2446}
2447
Aviv Heller13eab212016-09-18 20:48:04 +03002448static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
2449 struct mlx5_ib_sq *sq, u8 tx_affinity)
2450{
2451 void *in;
2452 void *tisc;
2453 int inlen;
2454 int err;
2455
2456 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002457 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002458 if (!in)
2459 return -ENOMEM;
2460
2461 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
2462
2463 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2464 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2465
2466 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2467
2468 kvfree(in);
2469
2470 return err;
2471}
2472
majd@mellanox.com75850d02016-01-14 19:13:06 +02002473static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002474 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002475 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002476 u32 path_flags, const struct ib_qp_attr *attr,
2477 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002478{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002479 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002480 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002481 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002482 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2483 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002484
Eli Cohene126ba92013-07-07 17:25:49 +03002485 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002486 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2487 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002488
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002489 if (ah_flags & IB_AH_GRH) {
2490 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002491 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002492 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002493 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002494 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002495 return -EINVAL;
2496 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002497 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002498
2499 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002500 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002501 return -EINVAL;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002502 err = mlx5_get_roce_gid_type(dev, port, grh->sgid_index,
Majd Dibbinyed884512017-01-18 14:10:35 +02002503 &gid_type);
2504 if (err)
2505 return err;
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002506 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02002507 if (qp->ibqp.qp_type == IB_QPT_RC ||
2508 qp->ibqp.qp_type == IB_QPT_UC ||
2509 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
2510 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
2511 path->udp_sport = mlx5_get_roce_udp_sport(dev, port,
2512 grh->sgid_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002513 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Majd Dibbinyed884512017-01-18 14:10:35 +02002514 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002515 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002516 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002517 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2518 path->fl_free_ar |=
2519 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002520 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2521 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2522 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002523 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002524 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002525 }
2526
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002527 if (ah_flags & IB_AH_GRH) {
2528 path->mgid_index = grh->sgid_index;
2529 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002530 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002531 cpu_to_be32((grh->traffic_class << 20) |
2532 (grh->flow_label));
2533 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002534 }
2535
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002536 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002537 if (err < 0)
2538 return err;
2539 path->static_rate = err;
2540 path->port = port;
2541
Eli Cohene126ba92013-07-07 17:25:49 +03002542 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002543 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002544
majd@mellanox.com75850d02016-01-14 19:13:06 +02002545 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2546 return modify_raw_packet_eth_prio(dev->mdev,
2547 &qp->raw_packet_qp.sq,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002548 sl & 0xf);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002549
Eli Cohene126ba92013-07-07 17:25:49 +03002550 return 0;
2551}
2552
2553static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2554 [MLX5_QP_STATE_INIT] = {
2555 [MLX5_QP_STATE_INIT] = {
2556 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2557 MLX5_QP_OPTPAR_RAE |
2558 MLX5_QP_OPTPAR_RWE |
2559 MLX5_QP_OPTPAR_PKEY_INDEX |
2560 MLX5_QP_OPTPAR_PRI_PORT,
2561 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2562 MLX5_QP_OPTPAR_PKEY_INDEX |
2563 MLX5_QP_OPTPAR_PRI_PORT,
2564 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2565 MLX5_QP_OPTPAR_Q_KEY |
2566 MLX5_QP_OPTPAR_PRI_PORT,
2567 },
2568 [MLX5_QP_STATE_RTR] = {
2569 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2570 MLX5_QP_OPTPAR_RRE |
2571 MLX5_QP_OPTPAR_RAE |
2572 MLX5_QP_OPTPAR_RWE |
2573 MLX5_QP_OPTPAR_PKEY_INDEX,
2574 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2575 MLX5_QP_OPTPAR_RWE |
2576 MLX5_QP_OPTPAR_PKEY_INDEX,
2577 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2578 MLX5_QP_OPTPAR_Q_KEY,
2579 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2580 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002581 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2582 MLX5_QP_OPTPAR_RRE |
2583 MLX5_QP_OPTPAR_RAE |
2584 MLX5_QP_OPTPAR_RWE |
2585 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002586 },
2587 },
2588 [MLX5_QP_STATE_RTR] = {
2589 [MLX5_QP_STATE_RTS] = {
2590 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2591 MLX5_QP_OPTPAR_RRE |
2592 MLX5_QP_OPTPAR_RAE |
2593 MLX5_QP_OPTPAR_RWE |
2594 MLX5_QP_OPTPAR_PM_STATE |
2595 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2596 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2597 MLX5_QP_OPTPAR_RWE |
2598 MLX5_QP_OPTPAR_PM_STATE,
2599 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2600 },
2601 },
2602 [MLX5_QP_STATE_RTS] = {
2603 [MLX5_QP_STATE_RTS] = {
2604 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2605 MLX5_QP_OPTPAR_RAE |
2606 MLX5_QP_OPTPAR_RWE |
2607 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002608 MLX5_QP_OPTPAR_PM_STATE |
2609 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002610 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03002611 MLX5_QP_OPTPAR_PM_STATE |
2612 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002613 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
2614 MLX5_QP_OPTPAR_SRQN |
2615 MLX5_QP_OPTPAR_CQN_RCV,
2616 },
2617 },
2618 [MLX5_QP_STATE_SQER] = {
2619 [MLX5_QP_STATE_RTS] = {
2620 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2621 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002622 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002623 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2624 MLX5_QP_OPTPAR_RWE |
2625 MLX5_QP_OPTPAR_RAE |
2626 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002627 },
2628 },
2629};
2630
2631static int ib_nr_to_mlx5_nr(int ib_mask)
2632{
2633 switch (ib_mask) {
2634 case IB_QP_STATE:
2635 return 0;
2636 case IB_QP_CUR_STATE:
2637 return 0;
2638 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2639 return 0;
2640 case IB_QP_ACCESS_FLAGS:
2641 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2642 MLX5_QP_OPTPAR_RAE;
2643 case IB_QP_PKEY_INDEX:
2644 return MLX5_QP_OPTPAR_PKEY_INDEX;
2645 case IB_QP_PORT:
2646 return MLX5_QP_OPTPAR_PRI_PORT;
2647 case IB_QP_QKEY:
2648 return MLX5_QP_OPTPAR_Q_KEY;
2649 case IB_QP_AV:
2650 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2651 MLX5_QP_OPTPAR_PRI_PORT;
2652 case IB_QP_PATH_MTU:
2653 return 0;
2654 case IB_QP_TIMEOUT:
2655 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2656 case IB_QP_RETRY_CNT:
2657 return MLX5_QP_OPTPAR_RETRY_COUNT;
2658 case IB_QP_RNR_RETRY:
2659 return MLX5_QP_OPTPAR_RNR_RETRY;
2660 case IB_QP_RQ_PSN:
2661 return 0;
2662 case IB_QP_MAX_QP_RD_ATOMIC:
2663 return MLX5_QP_OPTPAR_SRA_MAX;
2664 case IB_QP_ALT_PATH:
2665 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2666 case IB_QP_MIN_RNR_TIMER:
2667 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2668 case IB_QP_SQ_PSN:
2669 return 0;
2670 case IB_QP_MAX_DEST_RD_ATOMIC:
2671 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2672 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2673 case IB_QP_PATH_MIG_STATE:
2674 return MLX5_QP_OPTPAR_PM_STATE;
2675 case IB_QP_CAP:
2676 return 0;
2677 case IB_QP_DEST_QPN:
2678 return 0;
2679 }
2680 return 0;
2681}
2682
2683static int ib_mask_to_mlx5_opt(int ib_mask)
2684{
2685 int result = 0;
2686 int i;
2687
2688 for (i = 0; i < 8 * sizeof(int); i++) {
2689 if ((1 << i) & ib_mask)
2690 result |= ib_nr_to_mlx5_nr(1 << i);
2691 }
2692
2693 return result;
2694}
2695
Alex Veskereb49ab02016-08-28 12:25:53 +03002696static int modify_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
2697 struct mlx5_ib_rq *rq, int new_state,
2698 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002699{
2700 void *in;
2701 void *rqc;
2702 int inlen;
2703 int err;
2704
2705 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002706 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002707 if (!in)
2708 return -ENOMEM;
2709
2710 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
2711
2712 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2713 MLX5_SET(rqc, rqc, state, new_state);
2714
Alex Veskereb49ab02016-08-28 12:25:53 +03002715 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2716 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2717 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02002718 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03002719 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2720 } else
2721 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2722 dev->ib_dev.name);
2723 }
2724
2725 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002726 if (err)
2727 goto out;
2728
2729 rq->state = new_state;
2730
2731out:
2732 kvfree(in);
2733 return err;
2734}
2735
2736static int modify_raw_packet_qp_sq(struct mlx5_core_dev *dev,
Bodong Wang7d29f342016-12-01 13:43:16 +02002737 struct mlx5_ib_sq *sq,
2738 int new_state,
2739 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002740{
Bodong Wang7d29f342016-12-01 13:43:16 +02002741 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
2742 u32 old_rate = ibqp->rate_limit;
2743 u32 new_rate = old_rate;
2744 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002745 void *in;
2746 void *sqc;
2747 int inlen;
2748 int err;
2749
2750 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002751 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002752 if (!in)
2753 return -ENOMEM;
2754
2755 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2756
2757 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2758 MLX5_SET(sqc, sqc, state, new_state);
2759
Bodong Wang7d29f342016-12-01 13:43:16 +02002760 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2761 if (new_state != MLX5_SQC_STATE_RDY)
2762 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2763 __func__);
2764 else
2765 new_rate = raw_qp_param->rate_limit;
2766 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002767
Bodong Wang7d29f342016-12-01 13:43:16 +02002768 if (old_rate != new_rate) {
2769 if (new_rate) {
2770 err = mlx5_rl_add_rate(dev, new_rate, &rl_index);
2771 if (err) {
2772 pr_err("Failed configuring rate %u: %d\n",
2773 new_rate, err);
2774 goto out;
2775 }
2776 }
2777
2778 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
2779 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2780 }
2781
2782 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2783 if (err) {
2784 /* Remove new rate from table if failed */
2785 if (new_rate &&
2786 old_rate != new_rate)
2787 mlx5_rl_remove_rate(dev, new_rate);
2788 goto out;
2789 }
2790
2791 /* Only remove the old rate after new rate was set */
2792 if ((old_rate &&
2793 (old_rate != new_rate)) ||
2794 (new_state != MLX5_SQC_STATE_RDY))
2795 mlx5_rl_remove_rate(dev, old_rate);
2796
2797 ibqp->rate_limit = new_rate;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002798 sq->state = new_state;
2799
2800out:
2801 kvfree(in);
2802 return err;
2803}
2804
2805static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002806 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2807 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002808{
2809 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2810 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2811 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002812 int modify_rq = !!qp->rq.wqe_cnt;
2813 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002814 int rq_state;
2815 int sq_state;
2816 int err;
2817
Alex Vesker0680efa2016-08-28 12:25:52 +03002818 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002819 case MLX5_CMD_OP_RST2INIT_QP:
2820 rq_state = MLX5_RQC_STATE_RDY;
2821 sq_state = MLX5_SQC_STATE_RDY;
2822 break;
2823 case MLX5_CMD_OP_2ERR_QP:
2824 rq_state = MLX5_RQC_STATE_ERR;
2825 sq_state = MLX5_SQC_STATE_ERR;
2826 break;
2827 case MLX5_CMD_OP_2RST_QP:
2828 rq_state = MLX5_RQC_STATE_RST;
2829 sq_state = MLX5_SQC_STATE_RST;
2830 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002831 case MLX5_CMD_OP_RTR2RTS_QP:
2832 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002833 if (raw_qp_param->set_mask ==
2834 MLX5_RAW_QP_RATE_LIMIT) {
2835 modify_rq = 0;
2836 sq_state = sq->state;
2837 } else {
2838 return raw_qp_param->set_mask ? -EINVAL : 0;
2839 }
2840 break;
2841 case MLX5_CMD_OP_INIT2INIT_QP:
2842 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002843 if (raw_qp_param->set_mask)
2844 return -EINVAL;
2845 else
2846 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002847 default:
2848 WARN_ON(1);
2849 return -EINVAL;
2850 }
2851
Bodong Wang7d29f342016-12-01 13:43:16 +02002852 if (modify_rq) {
2853 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002854 if (err)
2855 return err;
2856 }
2857
Bodong Wang7d29f342016-12-01 13:43:16 +02002858 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03002859 if (tx_affinity) {
2860 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
2861 tx_affinity);
2862 if (err)
2863 return err;
2864 }
2865
Bodong Wang7d29f342016-12-01 13:43:16 +02002866 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state, raw_qp_param);
Aviv Heller13eab212016-09-18 20:48:04 +03002867 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002868
2869 return 0;
2870}
2871
Eli Cohene126ba92013-07-07 17:25:49 +03002872static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
2873 const struct ib_qp_attr *attr, int attr_mask,
2874 enum ib_qp_state cur_state, enum ib_qp_state new_state)
2875{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002876 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
2877 [MLX5_QP_STATE_RST] = {
2878 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2879 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2880 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
2881 },
2882 [MLX5_QP_STATE_INIT] = {
2883 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2884 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2885 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
2886 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
2887 },
2888 [MLX5_QP_STATE_RTR] = {
2889 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2890 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2891 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
2892 },
2893 [MLX5_QP_STATE_RTS] = {
2894 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2895 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2896 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
2897 },
2898 [MLX5_QP_STATE_SQD] = {
2899 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2900 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2901 },
2902 [MLX5_QP_STATE_SQER] = {
2903 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2904 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2905 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
2906 },
2907 [MLX5_QP_STATE_ERR] = {
2908 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2909 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2910 }
2911 };
2912
Eli Cohene126ba92013-07-07 17:25:49 +03002913 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2914 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02002915 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03002916 struct mlx5_ib_cq *send_cq, *recv_cq;
2917 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03002918 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03002919 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002920 enum mlx5_qp_state mlx5_cur, mlx5_new;
2921 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03002922 int mlx5_st;
2923 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002924 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03002925 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002926
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002927 context = kzalloc(sizeof(*context), GFP_KERNEL);
2928 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03002929 return -ENOMEM;
2930
Moni Shouac32a4f22018-01-02 16:19:32 +02002931 err = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
2932 qp->qp_sub_type : ibqp->qp_type);
Haggai Eran158abf82016-02-29 15:45:04 +02002933 if (err < 0) {
2934 mlx5_ib_dbg(dev, "unsupported qp type %d\n", ibqp->qp_type);
Eli Cohene126ba92013-07-07 17:25:49 +03002935 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002936 }
Eli Cohene126ba92013-07-07 17:25:49 +03002937
2938 context->flags = cpu_to_be32(err << 16);
2939
2940 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
2941 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2942 } else {
2943 switch (attr->path_mig_state) {
2944 case IB_MIG_MIGRATED:
2945 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2946 break;
2947 case IB_MIG_REARM:
2948 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
2949 break;
2950 case IB_MIG_ARMED:
2951 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
2952 break;
2953 }
2954 }
2955
Aviv Heller13eab212016-09-18 20:48:04 +03002956 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2957 if ((ibqp->qp_type == IB_QPT_RC) ||
2958 (ibqp->qp_type == IB_QPT_UD &&
2959 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
2960 (ibqp->qp_type == IB_QPT_UC) ||
2961 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
2962 (ibqp->qp_type == IB_QPT_XRC_INI) ||
2963 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
2964 if (mlx5_lag_is_active(dev->mdev)) {
2965 tx_affinity = (unsigned int)atomic_add_return(1,
2966 &dev->roce.next_port) %
2967 MLX5_MAX_PORTS + 1;
2968 context->flags |= cpu_to_be32(tx_affinity << 24);
2969 }
2970 }
2971 }
2972
Haggai Erand16e91d2016-02-29 15:45:05 +02002973 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002974 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002975 } else if ((ibqp->qp_type == IB_QPT_UD &&
2976 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03002977 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
2978 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
2979 } else if (attr_mask & IB_QP_PATH_MTU) {
2980 if (attr->path_mtu < IB_MTU_256 ||
2981 attr->path_mtu > IB_MTU_4096) {
2982 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
2983 err = -EINVAL;
2984 goto out;
2985 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002986 context->mtu_msgmax = (attr->path_mtu << 5) |
2987 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03002988 }
2989
2990 if (attr_mask & IB_QP_DEST_QPN)
2991 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
2992
2993 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002994 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002995
2996 /* todo implement counter_index functionality */
2997
2998 if (is_sqp(ibqp->qp_type))
2999 context->pri_path.port = qp->port;
3000
3001 if (attr_mask & IB_QP_PORT)
3002 context->pri_path.port = attr->port_num;
3003
3004 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003005 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03003006 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003007 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03003008 if (err)
3009 goto out;
3010 }
3011
3012 if (attr_mask & IB_QP_TIMEOUT)
3013 context->pri_path.ackto_lt |= attr->timeout << 3;
3014
3015 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02003016 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
3017 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03003018 attr->alt_port_num,
3019 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
3020 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03003021 if (err)
3022 goto out;
3023 }
3024
3025 pd = get_pd(qp);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003026 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
3027 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003028
3029 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
3030 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
3031 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
3032 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
3033
3034 if (attr_mask & IB_QP_RNR_RETRY)
3035 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
3036
3037 if (attr_mask & IB_QP_RETRY_CNT)
3038 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
3039
3040 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
3041 if (attr->max_rd_atomic)
3042 context->params1 |=
3043 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
3044 }
3045
3046 if (attr_mask & IB_QP_SQ_PSN)
3047 context->next_send_psn = cpu_to_be32(attr->sq_psn);
3048
3049 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
3050 if (attr->max_dest_rd_atomic)
3051 context->params2 |=
3052 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
3053 }
3054
3055 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
3056 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
3057
3058 if (attr_mask & IB_QP_MIN_RNR_TIMER)
3059 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
3060
3061 if (attr_mask & IB_QP_RQ_PSN)
3062 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
3063
3064 if (attr_mask & IB_QP_QKEY)
3065 context->qkey = cpu_to_be32(attr->qkey);
3066
3067 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3068 context->db_rec_addr = cpu_to_be64(qp->db.dma);
3069
Mark Bloch0837e862016-06-17 15:10:55 +03003070 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3071 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
3072 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003073
3074 /* Underlay port should be used - index 0 function per port */
3075 if (qp->flags & MLX5_IB_QP_UNDERLAY)
3076 port_num = 0;
3077
Alex Veskereb49ab02016-08-28 12:25:53 +03003078 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03003079 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03003080 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03003081 }
3082
Eli Cohene126ba92013-07-07 17:25:49 +03003083 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3084 context->sq_crq_size |= cpu_to_be16(1 << 4);
3085
Haggai Eranb11a4f92016-02-29 15:45:03 +02003086 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
3087 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03003088
3089 mlx5_cur = to_mlx5_state(cur_state);
3090 mlx5_new = to_mlx5_state(new_state);
Moni Shouac32a4f22018-01-02 16:19:32 +02003091 mlx5_st = to_mlx5_st(ibqp->qp_type == IB_QPT_DRIVER ?
3092 qp->qp_sub_type : ibqp->qp_type);
Eli Cohen07c91132013-10-24 12:01:01 +03003093 if (mlx5_st < 0)
Eli Cohene126ba92013-07-07 17:25:49 +03003094 goto out;
3095
majd@mellanox.com427c1e72016-01-14 19:13:07 +02003096 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
3097 !optab[mlx5_cur][mlx5_new])
3098 goto out;
3099
3100 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03003101 optpar = ib_mask_to_mlx5_opt(attr_mask);
3102 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003103
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003104 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3105 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03003106 struct mlx5_modify_raw_qp_param raw_qp_param = {};
3107
3108 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03003109 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03003110 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03003111 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
3112 }
Bodong Wang7d29f342016-12-01 13:43:16 +02003113
3114 if (attr_mask & IB_QP_RATE_LIMIT) {
3115 raw_qp_param.rate_limit = attr->rate_limit;
3116 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
3117 }
3118
Aviv Heller13eab212016-09-18 20:48:04 +03003119 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03003120 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003121 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02003122 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03003123 }
3124
Eli Cohene126ba92013-07-07 17:25:49 +03003125 if (err)
3126 goto out;
3127
3128 qp->state = new_state;
3129
3130 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003131 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003132 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003133 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03003134 if (attr_mask & IB_QP_PORT)
3135 qp->port = attr->port_num;
3136 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003137 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003138
3139 /*
3140 * If we moved a kernel QP to RESET, clean up all old CQ
3141 * entries and reinitialize the QP.
3142 */
3143 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003144 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003145 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3146 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003147 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003148
3149 qp->rq.head = 0;
3150 qp->rq.tail = 0;
3151 qp->sq.head = 0;
3152 qp->sq.tail = 0;
3153 qp->sq.cur_post = 0;
3154 qp->sq.last_poll = 0;
3155 qp->db.db[MLX5_RCV_DBR] = 0;
3156 qp->db.db[MLX5_SND_DBR] = 0;
3157 }
3158
3159out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003160 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003161 return err;
3162}
3163
Moni Shouac32a4f22018-01-02 16:19:32 +02003164static inline bool is_valid_mask(int mask, int req, int opt)
3165{
3166 if ((mask & req) != req)
3167 return false;
3168
3169 if (mask & ~(req | opt))
3170 return false;
3171
3172 return true;
3173}
3174
3175/* check valid transition for driver QP types
3176 * for now the only QP type that this function supports is DCI
3177 */
3178static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
3179 enum ib_qp_attr_mask attr_mask)
3180{
3181 int req = IB_QP_STATE;
3182 int opt = 0;
3183
3184 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3185 req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
3186 return is_valid_mask(attr_mask, req, opt);
3187 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
3188 opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
3189 return is_valid_mask(attr_mask, req, opt);
3190 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3191 req |= IB_QP_PATH_MTU;
3192 opt = IB_QP_PKEY_INDEX;
3193 return is_valid_mask(attr_mask, req, opt);
3194 } else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
3195 req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
3196 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
3197 opt = IB_QP_MIN_RNR_TIMER;
3198 return is_valid_mask(attr_mask, req, opt);
3199 } else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
3200 opt = IB_QP_MIN_RNR_TIMER;
3201 return is_valid_mask(attr_mask, req, opt);
3202 } else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
3203 return is_valid_mask(attr_mask, req, opt);
3204 }
3205 return false;
3206}
3207
Moni Shoua776a3902018-01-02 16:19:33 +02003208/* mlx5_ib_modify_dct: modify a DCT QP
3209 * valid transitions are:
3210 * RESET to INIT: must set access_flags, pkey_index and port
3211 * INIT to RTR : must set min_rnr_timer, tclass, flow_label,
3212 * mtu, gid_index and hop_limit
3213 * Other transitions and attributes are illegal
3214 */
3215static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3216 int attr_mask, struct ib_udata *udata)
3217{
3218 struct mlx5_ib_qp *qp = to_mqp(ibqp);
3219 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3220 enum ib_qp_state cur_state, new_state;
3221 int err = 0;
3222 int required = IB_QP_STATE;
3223 void *dctc;
3224
3225 if (!(attr_mask & IB_QP_STATE))
3226 return -EINVAL;
3227
3228 cur_state = qp->state;
3229 new_state = attr->qp_state;
3230
3231 dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
3232 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
3233 required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
3234 if (!is_valid_mask(attr_mask, required, 0))
3235 return -EINVAL;
3236
3237 if (attr->port_num == 0 ||
3238 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
3239 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3240 attr->port_num, dev->num_ports);
3241 return -EINVAL;
3242 }
3243 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
3244 MLX5_SET(dctc, dctc, rre, 1);
3245 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
3246 MLX5_SET(dctc, dctc, rwe, 1);
3247 if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
3248 if (!mlx5_ib_dc_atomic_is_supported(dev))
3249 return -EOPNOTSUPP;
3250 MLX5_SET(dctc, dctc, rae, 1);
3251 MLX5_SET(dctc, dctc, atomic_mode, MLX5_ATOMIC_MODE_DCT_CX);
3252 }
3253 MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
3254 MLX5_SET(dctc, dctc, port, attr->port_num);
3255 MLX5_SET(dctc, dctc, counter_set_id, dev->port[attr->port_num - 1].cnts.set_id);
3256
3257 } else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
3258 struct mlx5_ib_modify_qp_resp resp = {};
3259 u32 min_resp_len = offsetof(typeof(resp), dctn) +
3260 sizeof(resp.dctn);
3261
3262 if (udata->outlen < min_resp_len)
3263 return -EINVAL;
3264 resp.response_length = min_resp_len;
3265
3266 required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
3267 if (!is_valid_mask(attr_mask, required, 0))
3268 return -EINVAL;
3269 MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
3270 MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
3271 MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
3272 MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
3273 MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
3274 MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);
3275
3276 err = mlx5_core_create_dct(dev->mdev, &qp->dct.mdct, qp->dct.in,
3277 MLX5_ST_SZ_BYTES(create_dct_in));
3278 if (err)
3279 return err;
3280 resp.dctn = qp->dct.mdct.mqp.qpn;
3281 err = ib_copy_to_udata(udata, &resp, resp.response_length);
3282 if (err) {
3283 mlx5_core_destroy_dct(dev->mdev, &qp->dct.mdct);
3284 return err;
3285 }
3286 } else {
3287 mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
3288 return -EINVAL;
3289 }
3290 if (err)
3291 qp->state = IB_QPS_ERR;
3292 else
3293 qp->state = new_state;
3294 return err;
3295}
3296
Eli Cohene126ba92013-07-07 17:25:49 +03003297int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3298 int attr_mask, struct ib_udata *udata)
3299{
3300 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3301 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Haggai Erand16e91d2016-02-29 15:45:05 +02003302 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003303 enum ib_qp_state cur_state, new_state;
3304 int err = -EINVAL;
3305 int port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003306 enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
Eli Cohene126ba92013-07-07 17:25:49 +03003307
Yishai Hadas28d61372016-05-23 15:20:56 +03003308 if (ibqp->rwq_ind_tbl)
3309 return -ENOSYS;
3310
Haggai Erand16e91d2016-02-29 15:45:05 +02003311 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3312 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3313
Moni Shouac32a4f22018-01-02 16:19:32 +02003314 if (ibqp->qp_type == IB_QPT_DRIVER)
3315 qp_type = qp->qp_sub_type;
3316 else
3317 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3318 IB_QPT_GSI : ibqp->qp_type;
3319
Moni Shoua776a3902018-01-02 16:19:33 +02003320 if (qp_type == MLX5_IB_QPT_DCT)
3321 return mlx5_ib_modify_dct(ibqp, attr, attr_mask, udata);
Haggai Erand16e91d2016-02-29 15:45:05 +02003322
Eli Cohene126ba92013-07-07 17:25:49 +03003323 mutex_lock(&qp->mutex);
3324
3325 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
3326 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
3327
Achiad Shochat2811ba52015-12-23 18:47:24 +02003328 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
3329 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
3330 ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
3331 }
3332
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003333 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
3334 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
3335 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
3336 attr_mask);
3337 goto out;
3338 }
3339 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Moni Shouac32a4f22018-01-02 16:19:32 +02003340 qp_type != MLX5_IB_QPT_DCI &&
3341 !ib_modify_qp_is_ok(cur_state, new_state, qp_type, attr_mask, ll)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003342 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3343 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003344 goto out;
Moni Shouac32a4f22018-01-02 16:19:32 +02003345 } else if (qp_type == MLX5_IB_QPT_DCI &&
3346 !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
3347 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3348 cur_state, new_state, qp_type, attr_mask);
3349 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003350 }
Eli Cohene126ba92013-07-07 17:25:49 +03003351
3352 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003353 (attr->port_num == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003354 attr->port_num > dev->num_ports)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003355 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3356 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003357 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003358 }
Eli Cohene126ba92013-07-07 17:25:49 +03003359
3360 if (attr_mask & IB_QP_PKEY_INDEX) {
3361 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003362 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02003363 dev->mdev->port_caps[port - 1].pkey_table_len) {
3364 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
3365 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003366 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003367 }
Eli Cohene126ba92013-07-07 17:25:49 +03003368 }
3369
3370 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003371 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003372 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
3373 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
3374 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003375 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003376 }
Eli Cohene126ba92013-07-07 17:25:49 +03003377
3378 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003379 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003380 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
3381 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
3382 attr->max_dest_rd_atomic);
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 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
3387 err = 0;
3388 goto out;
3389 }
3390
3391 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
3392
3393out:
3394 mutex_unlock(&qp->mutex);
3395 return err;
3396}
3397
3398static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
3399{
3400 struct mlx5_ib_cq *cq;
3401 unsigned cur;
3402
3403 cur = wq->head - wq->tail;
3404 if (likely(cur + nreq < wq->max_post))
3405 return 0;
3406
3407 cq = to_mcq(ib_cq);
3408 spin_lock(&cq->lock);
3409 cur = wq->head - wq->tail;
3410 spin_unlock(&cq->lock);
3411
3412 return cur + nreq >= wq->max_post;
3413}
3414
3415static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
3416 u64 remote_addr, u32 rkey)
3417{
3418 rseg->raddr = cpu_to_be64(remote_addr);
3419 rseg->rkey = cpu_to_be32(rkey);
3420 rseg->reserved = 0;
3421}
3422
Erez Shitritf0313962016-02-21 16:27:17 +02003423static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
3424 struct ib_send_wr *wr, void *qend,
3425 struct mlx5_ib_qp *qp, int *size)
3426{
3427 void *seg = eseg;
3428
3429 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
3430
3431 if (wr->send_flags & IB_SEND_IP_CSUM)
3432 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
3433 MLX5_ETH_WQE_L4_CSUM;
3434
3435 seg += sizeof(struct mlx5_wqe_eth_seg);
3436 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3437
3438 if (wr->opcode == IB_WR_LSO) {
3439 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003440 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr.start);
Erez Shitritf0313962016-02-21 16:27:17 +02003441 u64 left, leftlen, copysz;
3442 void *pdata = ud_wr->header;
3443
3444 left = ud_wr->hlen;
3445 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003446 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02003447
3448 /*
3449 * check if there is space till the end of queue, if yes,
3450 * copy all in one shot, otherwise copy till the end of queue,
3451 * rollback and than the copy the left
3452 */
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003453 leftlen = qend - (void *)eseg->inline_hdr.start;
Erez Shitritf0313962016-02-21 16:27:17 +02003454 copysz = min_t(u64, leftlen, left);
3455
3456 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3457
3458 if (likely(copysz > size_of_inl_hdr_start)) {
3459 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3460 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3461 }
3462
3463 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3464 seg = mlx5_get_send_wqe(qp, 0);
3465 left -= copysz;
3466 pdata += copysz;
3467 memcpy(seg, pdata, left);
3468 seg += ALIGN(left, 16);
3469 *size += ALIGN(left, 16) / 16;
3470 }
3471 }
3472
3473 return seg;
3474}
3475
Eli Cohene126ba92013-07-07 17:25:49 +03003476static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
3477 struct ib_send_wr *wr)
3478{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003479 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3480 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3481 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003482}
3483
3484static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3485{
3486 dseg->byte_count = cpu_to_be32(sg->length);
3487 dseg->lkey = cpu_to_be32(sg->lkey);
3488 dseg->addr = cpu_to_be64(sg->addr);
3489}
3490
Artemy Kovalyov31616252017-01-02 11:37:42 +02003491static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03003492{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003493 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
3494 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03003495}
3496
3497static __be64 frwr_mkey_mask(void)
3498{
3499 u64 result;
3500
3501 result = MLX5_MKEY_MASK_LEN |
3502 MLX5_MKEY_MASK_PAGE_SIZE |
3503 MLX5_MKEY_MASK_START_ADDR |
3504 MLX5_MKEY_MASK_EN_RINVAL |
3505 MLX5_MKEY_MASK_KEY |
3506 MLX5_MKEY_MASK_LR |
3507 MLX5_MKEY_MASK_LW |
3508 MLX5_MKEY_MASK_RR |
3509 MLX5_MKEY_MASK_RW |
3510 MLX5_MKEY_MASK_A |
3511 MLX5_MKEY_MASK_SMALL_FENCE |
3512 MLX5_MKEY_MASK_FREE;
3513
3514 return cpu_to_be64(result);
3515}
3516
Sagi Grimberge6631812014-02-23 14:19:11 +02003517static __be64 sig_mkey_mask(void)
3518{
3519 u64 result;
3520
3521 result = MLX5_MKEY_MASK_LEN |
3522 MLX5_MKEY_MASK_PAGE_SIZE |
3523 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003524 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003525 MLX5_MKEY_MASK_EN_RINVAL |
3526 MLX5_MKEY_MASK_KEY |
3527 MLX5_MKEY_MASK_LR |
3528 MLX5_MKEY_MASK_LW |
3529 MLX5_MKEY_MASK_RR |
3530 MLX5_MKEY_MASK_RW |
3531 MLX5_MKEY_MASK_SMALL_FENCE |
3532 MLX5_MKEY_MASK_FREE |
3533 MLX5_MKEY_MASK_BSF_EN;
3534
3535 return cpu_to_be64(result);
3536}
3537
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003538static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003539 struct mlx5_ib_mr *mr)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003540{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003541 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003542
3543 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003544
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003545 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003546 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003547 umr->mkey_mask = frwr_mkey_mask();
3548}
3549
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003550static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003551{
3552 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003553 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003554 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003555}
3556
Artemy Kovalyov31616252017-01-02 11:37:42 +02003557static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003558{
3559 u64 result;
3560
Artemy Kovalyov31616252017-01-02 11:37:42 +02003561 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02003562 MLX5_MKEY_MASK_FREE;
3563
3564 return cpu_to_be64(result);
3565}
3566
Artemy Kovalyov31616252017-01-02 11:37:42 +02003567static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003568{
3569 u64 result;
3570
3571 result = MLX5_MKEY_MASK_FREE;
3572
3573 return cpu_to_be64(result);
3574}
3575
Noa Osherovich56e11d62016-02-29 16:46:51 +02003576static __be64 get_umr_update_translation_mask(void)
3577{
3578 u64 result;
3579
3580 result = MLX5_MKEY_MASK_LEN |
3581 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003582 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003583
3584 return cpu_to_be64(result);
3585}
3586
Artemy Kovalyov31616252017-01-02 11:37:42 +02003587static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02003588{
3589 u64 result;
3590
Artemy Kovalyov31616252017-01-02 11:37:42 +02003591 result = MLX5_MKEY_MASK_LR |
3592 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003593 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003594 MLX5_MKEY_MASK_RW;
3595
3596 if (atomic)
3597 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003598
3599 return cpu_to_be64(result);
3600}
3601
3602static __be64 get_umr_update_pd_mask(void)
3603{
3604 u64 result;
3605
Artemy Kovalyov31616252017-01-02 11:37:42 +02003606 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003607
3608 return cpu_to_be64(result);
3609}
3610
Eli Cohene126ba92013-07-07 17:25:49 +03003611static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Maor Gottlieb578e7262016-10-27 16:36:37 +03003612 struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003613{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003614 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003615
3616 memset(umr, 0, sizeof(*umr));
3617
Haggai Eran968e78d2014-12-11 17:04:11 +02003618 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3619 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3620 else
3621 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3622
Artemy Kovalyov31616252017-01-02 11:37:42 +02003623 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
3624 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
3625 u64 offset = get_xlt_octo(umrwr->offset);
3626
3627 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
3628 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
3629 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003630 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02003631 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3632 umr->mkey_mask |= get_umr_update_translation_mask();
3633 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
3634 umr->mkey_mask |= get_umr_update_access_mask(atomic);
3635 umr->mkey_mask |= get_umr_update_pd_mask();
3636 }
3637 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
3638 umr->mkey_mask |= get_umr_enable_mr_mask();
3639 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
3640 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003641
3642 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003643 umr->flags |= MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003644}
3645
3646static u8 get_umr_flags(int acc)
3647{
3648 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3649 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3650 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3651 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003652 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003653}
3654
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003655static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3656 struct mlx5_ib_mr *mr,
3657 u32 key, int access)
3658{
3659 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3660
3661 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003662
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003663 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003664 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003665 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003666 /* KLMs take twice the size of MTTs */
3667 ndescs *= 2;
3668
3669 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003670 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3671 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3672 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3673 seg->len = cpu_to_be64(mr->ibmr.length);
3674 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003675}
3676
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003677static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003678{
3679 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003680 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003681}
3682
3683static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
3684{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003685 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003686
Eli Cohene126ba92013-07-07 17:25:49 +03003687 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02003688 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02003689 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003690
Haggai Eran968e78d2014-12-11 17:04:11 +02003691 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003692 if (umrwr->pd)
3693 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
3694 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
3695 !umrwr->length)
3696 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
3697
3698 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003699 seg->len = cpu_to_be64(umrwr->length);
3700 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003701 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003702 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003703}
3704
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003705static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3706 struct mlx5_ib_mr *mr,
3707 struct mlx5_ib_pd *pd)
3708{
3709 int bcount = mr->desc_size * mr->ndescs;
3710
3711 dseg->addr = cpu_to_be64(mr->desc_map);
3712 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3713 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3714}
3715
Eli Cohene126ba92013-07-07 17:25:49 +03003716static __be32 send_ieth(struct ib_send_wr *wr)
3717{
3718 switch (wr->opcode) {
3719 case IB_WR_SEND_WITH_IMM:
3720 case IB_WR_RDMA_WRITE_WITH_IMM:
3721 return wr->ex.imm_data;
3722
3723 case IB_WR_SEND_WITH_INV:
3724 return cpu_to_be32(wr->ex.invalidate_rkey);
3725
3726 default:
3727 return 0;
3728 }
3729}
3730
3731static u8 calc_sig(void *wqe, int size)
3732{
3733 u8 *p = wqe;
3734 u8 res = 0;
3735 int i;
3736
3737 for (i = 0; i < size; i++)
3738 res ^= p[i];
3739
3740 return ~res;
3741}
3742
3743static u8 wq_sig(void *wqe)
3744{
3745 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
3746}
3747
3748static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
3749 void *wqe, int *sz)
3750{
3751 struct mlx5_wqe_inline_seg *seg;
3752 void *qend = qp->sq.qend;
3753 void *addr;
3754 int inl = 0;
3755 int copy;
3756 int len;
3757 int i;
3758
3759 seg = wqe;
3760 wqe += sizeof(*seg);
3761 for (i = 0; i < wr->num_sge; i++) {
3762 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
3763 len = wr->sg_list[i].length;
3764 inl += len;
3765
3766 if (unlikely(inl > qp->max_inline_data))
3767 return -ENOMEM;
3768
3769 if (unlikely(wqe + len > qend)) {
3770 copy = qend - wqe;
3771 memcpy(wqe, addr, copy);
3772 addr += copy;
3773 len -= copy;
3774 wqe = mlx5_get_send_wqe(qp, 0);
3775 }
3776 memcpy(wqe, addr, len);
3777 wqe += len;
3778 }
3779
3780 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
3781
3782 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
3783
3784 return 0;
3785}
3786
Sagi Grimberge6631812014-02-23 14:19:11 +02003787static u16 prot_field_size(enum ib_signature_type type)
3788{
3789 switch (type) {
3790 case IB_SIG_TYPE_T10_DIF:
3791 return MLX5_DIF_SIZE;
3792 default:
3793 return 0;
3794 }
3795}
3796
3797static u8 bs_selector(int block_size)
3798{
3799 switch (block_size) {
3800 case 512: return 0x1;
3801 case 520: return 0x2;
3802 case 4096: return 0x3;
3803 case 4160: return 0x4;
3804 case 1073741824: return 0x5;
3805 default: return 0;
3806 }
3807}
3808
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003809static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
3810 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02003811{
Sagi Grimberg142537f2014-08-13 19:54:32 +03003812 /* Valid inline section and allow BSF refresh */
3813 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
3814 MLX5_BSF_REFRESH_DIF);
3815 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
3816 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003817 /* repeating block */
3818 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
3819 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
3820 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003821
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003822 if (domain->sig.dif.ref_remap)
3823 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02003824
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003825 if (domain->sig.dif.app_escape) {
3826 if (domain->sig.dif.ref_escape)
3827 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
3828 else
3829 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02003830 }
3831
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003832 inl->dif_app_bitmask_check =
3833 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02003834}
3835
3836static int mlx5_set_bsf(struct ib_mr *sig_mr,
3837 struct ib_sig_attrs *sig_attrs,
3838 struct mlx5_bsf *bsf, u32 data_size)
3839{
3840 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
3841 struct mlx5_bsf_basic *basic = &bsf->basic;
3842 struct ib_sig_domain *mem = &sig_attrs->mem;
3843 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02003844
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003845 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02003846
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003847 /* Basic + Extended + Inline */
3848 basic->bsf_size_sbs = 1 << 7;
3849 /* Input domain check byte mask */
3850 basic->check_byte_mask = sig_attrs->check_mask;
3851 basic->raw_data_size = cpu_to_be32(data_size);
3852
3853 /* Memory domain */
3854 switch (sig_attrs->mem.sig_type) {
3855 case IB_SIG_TYPE_NONE:
3856 break;
3857 case IB_SIG_TYPE_T10_DIF:
3858 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
3859 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
3860 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
3861 break;
3862 default:
3863 return -EINVAL;
3864 }
3865
3866 /* Wire domain */
3867 switch (sig_attrs->wire.sig_type) {
3868 case IB_SIG_TYPE_NONE:
3869 break;
3870 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02003871 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003872 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003873 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03003874 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02003875 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003876 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003877 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003878 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003879 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003880 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02003881 } else
3882 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
3883
Sagi Grimberg142537f2014-08-13 19:54:32 +03003884 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003885 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02003886 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003887 default:
3888 return -EINVAL;
3889 }
3890
3891 return 0;
3892}
3893
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003894static int set_sig_data_segment(struct ib_sig_handover_wr *wr,
3895 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003896{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003897 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
3898 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003899 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003900 u32 data_len = wr->wr.sg_list->length;
3901 u32 data_key = wr->wr.sg_list->lkey;
3902 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003903 int ret;
3904 int wqe_size;
3905
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003906 if (!wr->prot ||
3907 (data_key == wr->prot->lkey &&
3908 data_va == wr->prot->addr &&
3909 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003910 /**
3911 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003912 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02003913 * So need construct:
3914 * ------------------
3915 * | data_klm |
3916 * ------------------
3917 * | BSF |
3918 * ------------------
3919 **/
3920 struct mlx5_klm *data_klm = *seg;
3921
3922 data_klm->bcount = cpu_to_be32(data_len);
3923 data_klm->key = cpu_to_be32(data_key);
3924 data_klm->va = cpu_to_be64(data_va);
3925 wqe_size = ALIGN(sizeof(*data_klm), 64);
3926 } else {
3927 /**
3928 * Source domain contains signature information
3929 * So need construct a strided block format:
3930 * ---------------------------
3931 * | stride_block_ctrl |
3932 * ---------------------------
3933 * | data_klm |
3934 * ---------------------------
3935 * | prot_klm |
3936 * ---------------------------
3937 * | BSF |
3938 * ---------------------------
3939 **/
3940 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
3941 struct mlx5_stride_block_entry *data_sentry;
3942 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003943 u32 prot_key = wr->prot->lkey;
3944 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003945 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
3946 int prot_size;
3947
3948 sblock_ctrl = *seg;
3949 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
3950 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
3951
3952 prot_size = prot_field_size(sig_attrs->mem.sig_type);
3953 if (!prot_size) {
3954 pr_err("Bad block size given: %u\n", block_size);
3955 return -EINVAL;
3956 }
3957 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
3958 prot_size);
3959 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
3960 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
3961 sblock_ctrl->num_entries = cpu_to_be16(2);
3962
3963 data_sentry->bcount = cpu_to_be16(block_size);
3964 data_sentry->key = cpu_to_be32(data_key);
3965 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003966 data_sentry->stride = cpu_to_be16(block_size);
3967
Sagi Grimberge6631812014-02-23 14:19:11 +02003968 prot_sentry->bcount = cpu_to_be16(prot_size);
3969 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003970 prot_sentry->va = cpu_to_be64(prot_va);
3971 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003972
Sagi Grimberge6631812014-02-23 14:19:11 +02003973 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
3974 sizeof(*prot_sentry), 64);
3975 }
3976
3977 *seg += wqe_size;
3978 *size += wqe_size / 16;
3979 if (unlikely((*seg == qp->sq.qend)))
3980 *seg = mlx5_get_send_wqe(qp, 0);
3981
3982 bsf = *seg;
3983 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
3984 if (ret)
3985 return -EINVAL;
3986
3987 *seg += sizeof(*bsf);
3988 *size += sizeof(*bsf) / 16;
3989 if (unlikely((*seg == qp->sq.qend)))
3990 *seg = mlx5_get_send_wqe(qp, 0);
3991
3992 return 0;
3993}
3994
3995static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003996 struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02003997 u32 length, u32 pdn)
3998{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003999 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02004000 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004001 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02004002
4003 memset(seg, 0, sizeof(*seg));
4004
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004005 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03004006 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02004007 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004008 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02004009 MLX5_MKEY_BSF_EN | pdn);
4010 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02004011 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004012 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
4013}
4014
4015static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02004016 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02004017{
4018 memset(umr, 0, sizeof(*umr));
4019
4020 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004021 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02004022 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
4023 umr->mkey_mask = sig_mkey_mask();
4024}
4025
4026
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004027static int set_sig_umr_wr(struct ib_send_wr *send_wr, struct mlx5_ib_qp *qp,
Sagi Grimberge6631812014-02-23 14:19:11 +02004028 void **seg, int *size)
4029{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004030 struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
4031 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004032 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02004033 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02004034 int region_len, ret;
4035
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004036 if (unlikely(wr->wr.num_sge != 1) ||
4037 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004038 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
4039 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02004040 return -EINVAL;
4041
4042 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004043 region_len = wr->wr.sg_list->length;
4044 if (wr->prot &&
4045 (wr->prot->lkey != wr->wr.sg_list->lkey ||
4046 wr->prot->addr != wr->wr.sg_list->addr ||
4047 wr->prot->length != wr->wr.sg_list->length))
4048 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02004049
4050 /**
4051 * KLM octoword size - if protection was provided
4052 * then we use strided block format (3 octowords),
4053 * else we use single KLM (1 octoword)
4054 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02004055 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02004056
Artemy Kovalyov31616252017-01-02 11:37:42 +02004057 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02004058 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4059 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4060 if (unlikely((*seg == qp->sq.qend)))
4061 *seg = mlx5_get_send_wqe(qp, 0);
4062
Artemy Kovalyov31616252017-01-02 11:37:42 +02004063 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02004064 *seg += sizeof(struct mlx5_mkey_seg);
4065 *size += sizeof(struct mlx5_mkey_seg) / 16;
4066 if (unlikely((*seg == qp->sq.qend)))
4067 *seg = mlx5_get_send_wqe(qp, 0);
4068
4069 ret = set_sig_data_segment(wr, qp, seg, size);
4070 if (ret)
4071 return ret;
4072
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004073 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02004074 return 0;
4075}
4076
4077static int set_psv_wr(struct ib_sig_domain *domain,
4078 u32 psv_idx, void **seg, int *size)
4079{
4080 struct mlx5_seg_set_psv *psv_seg = *seg;
4081
4082 memset(psv_seg, 0, sizeof(*psv_seg));
4083 psv_seg->psv_num = cpu_to_be32(psv_idx);
4084 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004085 case IB_SIG_TYPE_NONE:
4086 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004087 case IB_SIG_TYPE_T10_DIF:
4088 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
4089 domain->sig.dif.app_tag);
4090 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02004091 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02004092 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02004093 pr_err("Bad signature type (%d) is given.\n",
4094 domain->sig_type);
4095 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004096 }
4097
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03004098 *seg += sizeof(*psv_seg);
4099 *size += sizeof(*psv_seg) / 16;
4100
Sagi Grimberge6631812014-02-23 14:19:11 +02004101 return 0;
4102}
4103
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004104static int set_reg_wr(struct mlx5_ib_qp *qp,
4105 struct ib_reg_wr *wr,
4106 void **seg, int *size)
4107{
4108 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
4109 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
4110
4111 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
4112 mlx5_ib_warn(to_mdev(qp->ibqp.device),
4113 "Invalid IB_SEND_INLINE send flag\n");
4114 return -EINVAL;
4115 }
4116
4117 set_reg_umr_seg(*seg, mr);
4118 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4119 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4120 if (unlikely((*seg == qp->sq.qend)))
4121 *seg = mlx5_get_send_wqe(qp, 0);
4122
4123 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
4124 *seg += sizeof(struct mlx5_mkey_seg);
4125 *size += sizeof(struct mlx5_mkey_seg) / 16;
4126 if (unlikely((*seg == qp->sq.qend)))
4127 *seg = mlx5_get_send_wqe(qp, 0);
4128
4129 set_reg_data_seg(*seg, mr, pd);
4130 *seg += sizeof(struct mlx5_wqe_data_seg);
4131 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
4132
4133 return 0;
4134}
4135
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004136static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
Eli Cohene126ba92013-07-07 17:25:49 +03004137{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004138 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004139 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4140 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4141 if (unlikely((*seg == qp->sq.qend)))
4142 *seg = mlx5_get_send_wqe(qp, 0);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004143 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004144 *seg += sizeof(struct mlx5_mkey_seg);
4145 *size += sizeof(struct mlx5_mkey_seg) / 16;
4146 if (unlikely((*seg == qp->sq.qend)))
4147 *seg = mlx5_get_send_wqe(qp, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004148}
4149
4150static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
4151{
4152 __be32 *p = NULL;
4153 int tidx = idx;
4154 int i, j;
4155
4156 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
4157 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
4158 if ((i & 0xf) == 0) {
4159 void *buf = mlx5_get_send_wqe(qp, tidx);
4160 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
4161 p = buf;
4162 j = 0;
4163 }
4164 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
4165 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
4166 be32_to_cpu(p[j + 3]));
4167 }
4168}
4169
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004170static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
4171 struct mlx5_wqe_ctrl_seg **ctrl,
Eli Cohen6a4f1392014-12-02 12:26:18 +02004172 struct ib_send_wr *wr, unsigned *idx,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004173 int *size, int nreq)
4174{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004175 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
4176 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004177
4178 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
4179 *seg = mlx5_get_send_wqe(qp, *idx);
4180 *ctrl = *seg;
4181 *(uint32_t *)(*seg + 8) = 0;
4182 (*ctrl)->imm = send_ieth(wr);
4183 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
4184 (wr->send_flags & IB_SEND_SIGNALED ?
4185 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
4186 (wr->send_flags & IB_SEND_SOLICITED ?
4187 MLX5_WQE_CTRL_SOLICITED : 0);
4188
4189 *seg += sizeof(**ctrl);
4190 *size = sizeof(**ctrl) / 16;
4191
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03004192 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004193}
4194
4195static void finish_wqe(struct mlx5_ib_qp *qp,
4196 struct mlx5_wqe_ctrl_seg *ctrl,
4197 u8 size, unsigned idx, u64 wr_id,
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004198 int nreq, u8 fence, u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004199{
4200 u8 opmod = 0;
4201
4202 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
4203 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02004204 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004205 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004206 if (unlikely(qp->wq_sig))
4207 ctrl->signature = wq_sig(ctrl);
4208
4209 qp->sq.wrid[idx] = wr_id;
4210 qp->sq.w_list[idx].opcode = mlx5_opcode;
4211 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
4212 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
4213 qp->sq.w_list[idx].next = qp->sq.cur_post;
4214}
4215
4216
Eli Cohene126ba92013-07-07 17:25:49 +03004217int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
4218 struct ib_send_wr **bad_wr)
4219{
4220 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
4221 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004222 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02004223 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02004224 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004225 struct mlx5_wqe_data_seg *dpseg;
4226 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02004227 struct mlx5_bf *bf;
Eli Cohene126ba92013-07-07 17:25:49 +03004228 int uninitialized_var(size);
Haggai Erand16e91d2016-02-29 15:45:05 +02004229 void *qend;
Eli Cohene126ba92013-07-07 17:25:49 +03004230 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03004231 unsigned idx;
4232 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004233 int num_sge;
4234 void *seg;
4235 int nreq;
4236 int i;
4237 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004238 u8 fence;
4239
Haggai Erand16e91d2016-02-29 15:45:05 +02004240 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4241 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
4242
4243 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004244 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02004245 qend = qp->sq.qend;
4246
Eli Cohene126ba92013-07-07 17:25:49 +03004247 spin_lock_irqsave(&qp->sq.lock, flags);
4248
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004249 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4250 err = -EIO;
4251 *bad_wr = wr;
4252 nreq = 0;
4253 goto out;
4254 }
4255
Eli Cohene126ba92013-07-07 17:25:49 +03004256 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04004257 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03004258 mlx5_ib_warn(dev, "\n");
4259 err = -EINVAL;
4260 *bad_wr = wr;
4261 goto out;
4262 }
4263
Eli Cohene126ba92013-07-07 17:25:49 +03004264 num_sge = wr->num_sge;
4265 if (unlikely(num_sge > qp->sq.max_gs)) {
4266 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03004267 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03004268 *bad_wr = wr;
4269 goto out;
4270 }
4271
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004272 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
4273 if (err) {
4274 mlx5_ib_warn(dev, "\n");
4275 err = -ENOMEM;
4276 *bad_wr = wr;
4277 goto out;
4278 }
Eli Cohene126ba92013-07-07 17:25:49 +03004279
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004280 if (wr->opcode == IB_WR_LOCAL_INV ||
4281 wr->opcode == IB_WR_REG_MR) {
4282 fence = dev->umr_fence;
4283 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
4284 } else if (wr->send_flags & IB_SEND_FENCE) {
4285 if (qp->next_fence)
4286 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
4287 else
4288 fence = MLX5_FENCE_MODE_FENCE;
4289 } else {
4290 fence = qp->next_fence;
4291 }
4292
Eli Cohene126ba92013-07-07 17:25:49 +03004293 switch (ibqp->qp_type) {
4294 case IB_QPT_XRC_INI:
4295 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03004296 seg += sizeof(*xrc);
4297 size += sizeof(*xrc) / 16;
4298 /* fall through */
4299 case IB_QPT_RC:
4300 switch (wr->opcode) {
4301 case IB_WR_RDMA_READ:
4302 case IB_WR_RDMA_WRITE:
4303 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004304 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4305 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004306 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004307 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4308 break;
4309
4310 case IB_WR_ATOMIC_CMP_AND_SWP:
4311 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03004312 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03004313 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
4314 err = -ENOSYS;
4315 *bad_wr = wr;
4316 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004317
4318 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03004319 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
4320 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004321 set_linv_wr(qp, &seg, &size);
Eli Cohene126ba92013-07-07 17:25:49 +03004322 num_sge = 0;
4323 break;
4324
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004325 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004326 qp->sq.wr_data[idx] = IB_WR_REG_MR;
4327 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
4328 err = set_reg_wr(qp, reg_wr(wr), &seg, &size);
4329 if (err) {
4330 *bad_wr = wr;
4331 goto out;
4332 }
4333 num_sge = 0;
4334 break;
4335
Sagi Grimberge6631812014-02-23 14:19:11 +02004336 case IB_WR_REG_SIG_MR:
4337 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004338 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004339
4340 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
4341 err = set_sig_umr_wr(wr, qp, &seg, &size);
4342 if (err) {
4343 mlx5_ib_warn(dev, "\n");
4344 *bad_wr = wr;
4345 goto out;
4346 }
4347
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004348 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4349 fence, MLX5_OPCODE_UMR);
Sagi Grimberge6631812014-02-23 14:19:11 +02004350 /*
4351 * SET_PSV WQEs are not signaled and solicited
4352 * on error
4353 */
4354 wr->send_flags &= ~IB_SEND_SIGNALED;
4355 wr->send_flags |= IB_SEND_SOLICITED;
4356 err = begin_wqe(qp, &seg, &ctrl, wr,
4357 &idx, &size, nreq);
4358 if (err) {
4359 mlx5_ib_warn(dev, "\n");
4360 err = -ENOMEM;
4361 *bad_wr = wr;
4362 goto out;
4363 }
4364
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004365 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02004366 mr->sig->psv_memory.psv_idx, &seg,
4367 &size);
4368 if (err) {
4369 mlx5_ib_warn(dev, "\n");
4370 *bad_wr = wr;
4371 goto out;
4372 }
4373
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004374 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4375 fence, MLX5_OPCODE_SET_PSV);
Sagi Grimberge6631812014-02-23 14:19:11 +02004376 err = begin_wqe(qp, &seg, &ctrl, wr,
4377 &idx, &size, nreq);
4378 if (err) {
4379 mlx5_ib_warn(dev, "\n");
4380 err = -ENOMEM;
4381 *bad_wr = wr;
4382 goto out;
4383 }
4384
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004385 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02004386 mr->sig->psv_wire.psv_idx, &seg,
4387 &size);
4388 if (err) {
4389 mlx5_ib_warn(dev, "\n");
4390 *bad_wr = wr;
4391 goto out;
4392 }
4393
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004394 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4395 fence, MLX5_OPCODE_SET_PSV);
4396 qp->next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004397 num_sge = 0;
4398 goto skip_psv;
4399
Eli Cohene126ba92013-07-07 17:25:49 +03004400 default:
4401 break;
4402 }
4403 break;
4404
4405 case IB_QPT_UC:
4406 switch (wr->opcode) {
4407 case IB_WR_RDMA_WRITE:
4408 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004409 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4410 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004411 seg += sizeof(struct mlx5_wqe_raddr_seg);
4412 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4413 break;
4414
4415 default:
4416 break;
4417 }
4418 break;
4419
Eli Cohene126ba92013-07-07 17:25:49 +03004420 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02004421 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
4422 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
4423 err = -EPERM;
4424 *bad_wr = wr;
4425 goto out;
4426 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07004427 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02004428 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004429 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004430 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004431 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4432 if (unlikely((seg == qend)))
4433 seg = mlx5_get_send_wqe(qp, 0);
4434 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004435 case IB_QPT_UD:
4436 set_datagram_seg(seg, wr);
4437 seg += sizeof(struct mlx5_wqe_datagram_seg);
4438 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004439
Erez Shitritf0313962016-02-21 16:27:17 +02004440 if (unlikely((seg == qend)))
4441 seg = mlx5_get_send_wqe(qp, 0);
4442
4443 /* handle qp that supports ud offload */
4444 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4445 struct mlx5_wqe_eth_pad *pad;
4446
4447 pad = seg;
4448 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4449 seg += sizeof(struct mlx5_wqe_eth_pad);
4450 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4451
4452 seg = set_eth_seg(seg, wr, qend, qp, &size);
4453
4454 if (unlikely((seg == qend)))
4455 seg = mlx5_get_send_wqe(qp, 0);
4456 }
4457 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004458 case MLX5_IB_QPT_REG_UMR:
4459 if (wr->opcode != MLX5_IB_WR_UMR) {
4460 err = -EINVAL;
4461 mlx5_ib_warn(dev, "bad opcode\n");
4462 goto out;
4463 }
4464 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004465 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Maor Gottlieb578e7262016-10-27 16:36:37 +03004466 set_reg_umr_segment(seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
Eli Cohene126ba92013-07-07 17:25:49 +03004467 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4468 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4469 if (unlikely((seg == qend)))
4470 seg = mlx5_get_send_wqe(qp, 0);
4471 set_reg_mkey_segment(seg, wr);
4472 seg += sizeof(struct mlx5_mkey_seg);
4473 size += sizeof(struct mlx5_mkey_seg) / 16;
4474 if (unlikely((seg == qend)))
4475 seg = mlx5_get_send_wqe(qp, 0);
4476 break;
4477
4478 default:
4479 break;
4480 }
4481
4482 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4483 int uninitialized_var(sz);
4484
4485 err = set_data_inl_seg(qp, wr, seg, &sz);
4486 if (unlikely(err)) {
4487 mlx5_ib_warn(dev, "\n");
4488 *bad_wr = wr;
4489 goto out;
4490 }
Eli Cohene126ba92013-07-07 17:25:49 +03004491 size += sz;
4492 } else {
4493 dpseg = seg;
4494 for (i = 0; i < num_sge; i++) {
4495 if (unlikely(dpseg == qend)) {
4496 seg = mlx5_get_send_wqe(qp, 0);
4497 dpseg = seg;
4498 }
4499 if (likely(wr->sg_list[i].length)) {
4500 set_data_ptr_seg(dpseg, wr->sg_list + i);
4501 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4502 dpseg++;
4503 }
4504 }
4505 }
4506
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004507 qp->next_fence = next_fence;
4508 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq, fence,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004509 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004510skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004511 if (0)
4512 dump_wqe(qp, idx, size);
4513 }
4514
4515out:
4516 if (likely(nreq)) {
4517 qp->sq.head += nreq;
4518
4519 /* Make sure that descriptors are written before
4520 * updating doorbell record and ringing the doorbell
4521 */
4522 wmb();
4523
4524 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4525
Eli Cohenada388f2014-01-14 17:45:16 +02004526 /* Make sure doorbell record is visible to the HCA before
4527 * we hit doorbell */
4528 wmb();
4529
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004530 /* currently we support only regular doorbells */
4531 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset, NULL);
4532 /* Make sure doorbells don't leak out of SQ spinlock
4533 * and reach the HCA out of order.
4534 */
4535 mmiowb();
Eli Cohene126ba92013-07-07 17:25:49 +03004536 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03004537 }
4538
4539 spin_unlock_irqrestore(&qp->sq.lock, flags);
4540
4541 return err;
4542}
4543
4544static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4545{
4546 sig->signature = calc_sig(sig, size);
4547}
4548
4549int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4550 struct ib_recv_wr **bad_wr)
4551{
4552 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4553 struct mlx5_wqe_data_seg *scat;
4554 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004555 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4556 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004557 unsigned long flags;
4558 int err = 0;
4559 int nreq;
4560 int ind;
4561 int i;
4562
Haggai Erand16e91d2016-02-29 15:45:05 +02004563 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4564 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4565
Eli Cohene126ba92013-07-07 17:25:49 +03004566 spin_lock_irqsave(&qp->rq.lock, flags);
4567
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004568 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4569 err = -EIO;
4570 *bad_wr = wr;
4571 nreq = 0;
4572 goto out;
4573 }
4574
Eli Cohene126ba92013-07-07 17:25:49 +03004575 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4576
4577 for (nreq = 0; wr; nreq++, wr = wr->next) {
4578 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4579 err = -ENOMEM;
4580 *bad_wr = wr;
4581 goto out;
4582 }
4583
4584 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4585 err = -EINVAL;
4586 *bad_wr = wr;
4587 goto out;
4588 }
4589
4590 scat = get_recv_wqe(qp, ind);
4591 if (qp->wq_sig)
4592 scat++;
4593
4594 for (i = 0; i < wr->num_sge; i++)
4595 set_data_ptr_seg(scat + i, wr->sg_list + i);
4596
4597 if (i < qp->rq.max_gs) {
4598 scat[i].byte_count = 0;
4599 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4600 scat[i].addr = 0;
4601 }
4602
4603 if (qp->wq_sig) {
4604 sig = (struct mlx5_rwqe_sig *)scat;
4605 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4606 }
4607
4608 qp->rq.wrid[ind] = wr->wr_id;
4609
4610 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4611 }
4612
4613out:
4614 if (likely(nreq)) {
4615 qp->rq.head += nreq;
4616
4617 /* Make sure that descriptors are written before
4618 * doorbell record.
4619 */
4620 wmb();
4621
4622 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4623 }
4624
4625 spin_unlock_irqrestore(&qp->rq.lock, flags);
4626
4627 return err;
4628}
4629
4630static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4631{
4632 switch (mlx5_state) {
4633 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4634 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4635 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4636 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4637 case MLX5_QP_STATE_SQ_DRAINING:
4638 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4639 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4640 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4641 default: return -1;
4642 }
4643}
4644
4645static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4646{
4647 switch (mlx5_mig_state) {
4648 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4649 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4650 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4651 default: return -1;
4652 }
4653}
4654
4655static int to_ib_qp_access_flags(int mlx5_flags)
4656{
4657 int ib_flags = 0;
4658
4659 if (mlx5_flags & MLX5_QP_BIT_RRE)
4660 ib_flags |= IB_ACCESS_REMOTE_READ;
4661 if (mlx5_flags & MLX5_QP_BIT_RWE)
4662 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4663 if (mlx5_flags & MLX5_QP_BIT_RAE)
4664 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4665
4666 return ib_flags;
4667}
4668
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004669static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004670 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004671 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03004672{
Eli Cohene126ba92013-07-07 17:25:49 +03004673
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004674 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03004675
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04004676 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004677 rdma_ah_set_port_num(ah_attr, path->port);
4678 if (rdma_ah_get_port_num(ah_attr) == 0 ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02004679 rdma_ah_get_port_num(ah_attr) > ibdev->num_ports)
Eli Cohene126ba92013-07-07 17:25:49 +03004680 return;
4681
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004682 rdma_ah_set_port_num(ah_attr, path->port);
4683 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03004684
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004685 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
4686 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
4687 rdma_ah_set_static_rate(ah_attr,
4688 path->static_rate ? path->static_rate - 5 : 0);
4689 if (path->grh_mlid & (1 << 7)) {
4690 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
4691
4692 rdma_ah_set_grh(ah_attr, NULL,
4693 tc_fl & 0xfffff,
4694 path->mgid_index,
4695 path->hop_limit,
4696 (tc_fl >> 20) & 0xff);
4697 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03004698 }
4699}
4700
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004701static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4702 struct mlx5_ib_sq *sq,
4703 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004704{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004705 void *out;
4706 void *sqc;
4707 int inlen;
4708 int err;
4709
4710 inlen = MLX5_ST_SZ_BYTES(query_sq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004711 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004712 if (!out)
4713 return -ENOMEM;
4714
4715 err = mlx5_core_query_sq(dev->mdev, sq->base.mqp.qpn, out);
4716 if (err)
4717 goto out;
4718
4719 sqc = MLX5_ADDR_OF(query_sq_out, out, sq_context);
4720 *sq_state = MLX5_GET(sqc, sqc, state);
4721 sq->state = *sq_state;
4722
4723out:
4724 kvfree(out);
4725 return err;
4726}
4727
4728static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
4729 struct mlx5_ib_rq *rq,
4730 u8 *rq_state)
4731{
4732 void *out;
4733 void *rqc;
4734 int inlen;
4735 int err;
4736
4737 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004738 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004739 if (!out)
4740 return -ENOMEM;
4741
4742 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
4743 if (err)
4744 goto out;
4745
4746 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
4747 *rq_state = MLX5_GET(rqc, rqc, state);
4748 rq->state = *rq_state;
4749
4750out:
4751 kvfree(out);
4752 return err;
4753}
4754
4755static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
4756 struct mlx5_ib_qp *qp, u8 *qp_state)
4757{
4758 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
4759 [MLX5_RQC_STATE_RST] = {
4760 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4761 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4762 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
4763 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
4764 },
4765 [MLX5_RQC_STATE_RDY] = {
4766 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4767 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4768 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
4769 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
4770 },
4771 [MLX5_RQC_STATE_ERR] = {
4772 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4773 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4774 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
4775 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
4776 },
4777 [MLX5_RQ_STATE_NA] = {
4778 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4779 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4780 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
4781 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
4782 },
4783 };
4784
4785 *qp_state = sqrq_trans[rq_state][sq_state];
4786
4787 if (*qp_state == MLX5_QP_STATE_BAD) {
4788 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4789 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
4790 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
4791 return -EINVAL;
4792 }
4793
4794 if (*qp_state == MLX5_QP_STATE)
4795 *qp_state = qp->state;
4796
4797 return 0;
4798}
4799
4800static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
4801 struct mlx5_ib_qp *qp,
4802 u8 *raw_packet_qp_state)
4803{
4804 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
4805 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
4806 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
4807 int err;
4808 u8 sq_state = MLX5_SQ_STATE_NA;
4809 u8 rq_state = MLX5_RQ_STATE_NA;
4810
4811 if (qp->sq.wqe_cnt) {
4812 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
4813 if (err)
4814 return err;
4815 }
4816
4817 if (qp->rq.wqe_cnt) {
4818 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
4819 if (err)
4820 return err;
4821 }
4822
4823 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
4824 raw_packet_qp_state);
4825}
4826
4827static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
4828 struct ib_qp_attr *qp_attr)
4829{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004830 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03004831 struct mlx5_qp_context *context;
4832 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004833 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03004834 int err = 0;
4835
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004836 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004837 if (!outb)
4838 return -ENOMEM;
4839
majd@mellanox.com19098df2016-01-14 19:13:03 +02004840 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004841 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03004842 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004843 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004844
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004845 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
4846 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
4847
Eli Cohene126ba92013-07-07 17:25:49 +03004848 mlx5_state = be32_to_cpu(context->flags) >> 28;
4849
4850 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03004851 qp_attr->path_mtu = context->mtu_msgmax >> 5;
4852 qp_attr->path_mig_state =
4853 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
4854 qp_attr->qkey = be32_to_cpu(context->qkey);
4855 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
4856 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
4857 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
4858 qp_attr->qp_access_flags =
4859 to_ib_qp_access_flags(be32_to_cpu(context->params2));
4860
4861 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004862 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
4863 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004864 qp_attr->alt_pkey_index =
4865 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004866 qp_attr->alt_port_num =
4867 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03004868 }
4869
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004870 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004871 qp_attr->port_num = context->pri_path.port;
4872
4873 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4874 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
4875
4876 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
4877
4878 qp_attr->max_dest_rd_atomic =
4879 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
4880 qp_attr->min_rnr_timer =
4881 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
4882 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
4883 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
4884 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
4885 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004886
4887out:
4888 kfree(outb);
4889 return err;
4890}
4891
Moni Shoua776a3902018-01-02 16:19:33 +02004892static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
4893 struct ib_qp_attr *qp_attr, int qp_attr_mask,
4894 struct ib_qp_init_attr *qp_init_attr)
4895{
4896 struct mlx5_core_dct *dct = &mqp->dct.mdct;
4897 u32 *out;
4898 u32 access_flags = 0;
4899 int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
4900 void *dctc;
4901 int err;
4902 int supported_mask = IB_QP_STATE |
4903 IB_QP_ACCESS_FLAGS |
4904 IB_QP_PORT |
4905 IB_QP_MIN_RNR_TIMER |
4906 IB_QP_AV |
4907 IB_QP_PATH_MTU |
4908 IB_QP_PKEY_INDEX;
4909
4910 if (qp_attr_mask & ~supported_mask)
4911 return -EINVAL;
4912 if (mqp->state != IB_QPS_RTR)
4913 return -EINVAL;
4914
4915 out = kzalloc(outlen, GFP_KERNEL);
4916 if (!out)
4917 return -ENOMEM;
4918
4919 err = mlx5_core_dct_query(dev->mdev, dct, out, outlen);
4920 if (err)
4921 goto out;
4922
4923 dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);
4924
4925 if (qp_attr_mask & IB_QP_STATE)
4926 qp_attr->qp_state = IB_QPS_RTR;
4927
4928 if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
4929 if (MLX5_GET(dctc, dctc, rre))
4930 access_flags |= IB_ACCESS_REMOTE_READ;
4931 if (MLX5_GET(dctc, dctc, rwe))
4932 access_flags |= IB_ACCESS_REMOTE_WRITE;
4933 if (MLX5_GET(dctc, dctc, rae))
4934 access_flags |= IB_ACCESS_REMOTE_ATOMIC;
4935 qp_attr->qp_access_flags = access_flags;
4936 }
4937
4938 if (qp_attr_mask & IB_QP_PORT)
4939 qp_attr->port_num = MLX5_GET(dctc, dctc, port);
4940 if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
4941 qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
4942 if (qp_attr_mask & IB_QP_AV) {
4943 qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
4944 qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
4945 qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
4946 qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
4947 }
4948 if (qp_attr_mask & IB_QP_PATH_MTU)
4949 qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
4950 if (qp_attr_mask & IB_QP_PKEY_INDEX)
4951 qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
4952out:
4953 kfree(out);
4954 return err;
4955}
4956
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004957int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
4958 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
4959{
4960 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4961 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4962 int err = 0;
4963 u8 raw_packet_qp_state;
4964
Yishai Hadas28d61372016-05-23 15:20:56 +03004965 if (ibqp->rwq_ind_tbl)
4966 return -ENOSYS;
4967
Haggai Erand16e91d2016-02-29 15:45:05 +02004968 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4969 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
4970 qp_init_attr);
4971
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004972 /* Not all of output fields are applicable, make sure to zero them */
4973 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
4974 memset(qp_attr, 0, sizeof(*qp_attr));
4975
Moni Shoua776a3902018-01-02 16:19:33 +02004976 if (unlikely(qp->qp_sub_type == MLX5_IB_QPT_DCT))
4977 return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
4978 qp_attr_mask, qp_init_attr);
4979
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004980 mutex_lock(&qp->mutex);
4981
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004982 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
4983 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004984 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
4985 if (err)
4986 goto out;
4987 qp->state = raw_packet_qp_state;
4988 qp_attr->port_num = 1;
4989 } else {
4990 err = query_qp_attr(dev, qp, qp_attr);
4991 if (err)
4992 goto out;
4993 }
4994
4995 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03004996 qp_attr->cur_qp_state = qp_attr->qp_state;
4997 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
4998 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
4999
5000 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03005001 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03005002 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03005003 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03005004 } else {
5005 qp_attr->cap.max_send_wr = 0;
5006 qp_attr->cap.max_send_sge = 0;
5007 }
5008
Noa Osherovich0540d812016-06-04 15:15:32 +03005009 qp_init_attr->qp_type = ibqp->qp_type;
5010 qp_init_attr->recv_cq = ibqp->recv_cq;
5011 qp_init_attr->send_cq = ibqp->send_cq;
5012 qp_init_attr->srq = ibqp->srq;
5013 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03005014
5015 qp_init_attr->cap = qp_attr->cap;
5016
5017 qp_init_attr->create_flags = 0;
5018 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
5019 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
5020
Leon Romanovsky051f2632015-12-20 12:16:11 +02005021 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
5022 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
5023 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
5024 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
5025 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
5026 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02005027 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
5028 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02005029
Eli Cohene126ba92013-07-07 17:25:49 +03005030 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
5031 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
5032
Eli Cohene126ba92013-07-07 17:25:49 +03005033out:
5034 mutex_unlock(&qp->mutex);
5035 return err;
5036}
5037
5038struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
5039 struct ib_ucontext *context,
5040 struct ib_udata *udata)
5041{
5042 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5043 struct mlx5_ib_xrcd *xrcd;
5044 int err;
5045
Saeed Mahameed938fe832015-05-28 22:28:41 +03005046 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03005047 return ERR_PTR(-ENOSYS);
5048
5049 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
5050 if (!xrcd)
5051 return ERR_PTR(-ENOMEM);
5052
Jack Morgenstein9603b612014-07-28 23:30:22 +03005053 err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005054 if (err) {
5055 kfree(xrcd);
5056 return ERR_PTR(-ENOMEM);
5057 }
5058
5059 return &xrcd->ibxrcd;
5060}
5061
5062int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
5063{
5064 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
5065 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
5066 int err;
5067
Jack Morgenstein9603b612014-07-28 23:30:22 +03005068 err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03005069 if (err) {
5070 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
5071 return err;
5072 }
5073
5074 kfree(xrcd);
5075
5076 return 0;
5077}
Yishai Hadas79b20a62016-05-23 15:20:50 +03005078
Yishai Hadas350d0e42016-08-28 14:58:18 +03005079static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
5080{
5081 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
5082 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
5083 struct ib_event event;
5084
5085 if (rwq->ibwq.event_handler) {
5086 event.device = rwq->ibwq.device;
5087 event.element.wq = &rwq->ibwq;
5088 switch (type) {
5089 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
5090 event.event = IB_EVENT_WQ_FATAL;
5091 break;
5092 default:
5093 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
5094 return;
5095 }
5096
5097 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
5098 }
5099}
5100
Maor Gottlieb03404e82017-05-30 10:29:13 +03005101static int set_delay_drop(struct mlx5_ib_dev *dev)
5102{
5103 int err = 0;
5104
5105 mutex_lock(&dev->delay_drop.lock);
5106 if (dev->delay_drop.activate)
5107 goto out;
5108
5109 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
5110 if (err)
5111 goto out;
5112
5113 dev->delay_drop.activate = true;
5114out:
5115 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005116
5117 if (!err)
5118 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005119 return err;
5120}
5121
Yishai Hadas79b20a62016-05-23 15:20:50 +03005122static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
5123 struct ib_wq_init_attr *init_attr)
5124{
5125 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02005126 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005127 __be64 *rq_pas0;
5128 void *in;
5129 void *rqc;
5130 void *wq;
5131 int inlen;
5132 int err;
5133
5134 dev = to_mdev(pd->device);
5135
5136 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005137 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005138 if (!in)
5139 return -ENOMEM;
5140
5141 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
5142 MLX5_SET(rqc, rqc, mem_rq_type,
5143 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
5144 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
5145 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
5146 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
5147 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
5148 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03005149 MLX5_SET(wq, wq, wq_type,
5150 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
5151 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005152 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5153 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
5154 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
5155 err = -EOPNOTSUPP;
5156 goto out;
5157 } else {
5158 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
5159 }
5160 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005161 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03005162 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
5163 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
5164 MLX5_SET(wq, wq, log_wqe_stride_size,
5165 rwq->single_stride_log_num_of_bytes -
5166 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
5167 MLX5_SET(wq, wq, log_wqe_num_of_strides, rwq->log_num_strides -
5168 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES);
5169 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005170 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
5171 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
5172 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
5173 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
5174 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
5175 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02005176 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005177 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02005178 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005179 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
5180 err = -EOPNOTSUPP;
5181 goto out;
5182 }
5183 } else {
5184 MLX5_SET(rqc, rqc, vsd, 1);
5185 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02005186 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
5187 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
5188 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
5189 err = -EOPNOTSUPP;
5190 goto out;
5191 }
5192 MLX5_SET(rqc, rqc, scatter_fcs, 1);
5193 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03005194 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5195 if (!(dev->ib_dev.attrs.raw_packet_caps &
5196 IB_RAW_PACKET_CAP_DELAY_DROP)) {
5197 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
5198 err = -EOPNOTSUPP;
5199 goto out;
5200 }
5201 MLX5_SET(rqc, rqc, delay_drop_en, 1);
5202 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03005203 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
5204 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03005205 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005206 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
5207 err = set_delay_drop(dev);
5208 if (err) {
5209 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
5210 err);
5211 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
5212 } else {
5213 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
5214 }
5215 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005216out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03005217 kvfree(in);
5218 return err;
5219}
5220
5221static int set_user_rq_size(struct mlx5_ib_dev *dev,
5222 struct ib_wq_init_attr *wq_init_attr,
5223 struct mlx5_ib_create_wq *ucmd,
5224 struct mlx5_ib_rwq *rwq)
5225{
5226 /* Sanity check RQ size before proceeding */
5227 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
5228 return -EINVAL;
5229
5230 if (!ucmd->rq_wqe_count)
5231 return -EINVAL;
5232
5233 rwq->wqe_count = ucmd->rq_wqe_count;
5234 rwq->wqe_shift = ucmd->rq_wqe_shift;
5235 rwq->buf_size = (rwq->wqe_count << rwq->wqe_shift);
5236 rwq->log_rq_stride = rwq->wqe_shift;
5237 rwq->log_rq_size = ilog2(rwq->wqe_count);
5238 return 0;
5239}
5240
5241static int prepare_user_rq(struct ib_pd *pd,
5242 struct ib_wq_init_attr *init_attr,
5243 struct ib_udata *udata,
5244 struct mlx5_ib_rwq *rwq)
5245{
5246 struct mlx5_ib_dev *dev = to_mdev(pd->device);
5247 struct mlx5_ib_create_wq ucmd = {};
5248 int err;
5249 size_t required_cmd_sz;
5250
Noa Osherovichccc87082017-10-17 18:01:13 +03005251 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
5252 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005253 if (udata->inlen < required_cmd_sz) {
5254 mlx5_ib_dbg(dev, "invalid inlen\n");
5255 return -EINVAL;
5256 }
5257
5258 if (udata->inlen > sizeof(ucmd) &&
5259 !ib_is_udata_cleared(udata, sizeof(ucmd),
5260 udata->inlen - sizeof(ucmd))) {
5261 mlx5_ib_dbg(dev, "inlen is not supported\n");
5262 return -EOPNOTSUPP;
5263 }
5264
5265 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
5266 mlx5_ib_dbg(dev, "copy failed\n");
5267 return -EFAULT;
5268 }
5269
Noa Osherovichccc87082017-10-17 18:01:13 +03005270 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03005271 mlx5_ib_dbg(dev, "invalid comp mask\n");
5272 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03005273 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
5274 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
5275 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
5276 return -EOPNOTSUPP;
5277 }
5278 if ((ucmd.single_stride_log_num_of_bytes <
5279 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
5280 (ucmd.single_stride_log_num_of_bytes >
5281 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
5282 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
5283 ucmd.single_stride_log_num_of_bytes,
5284 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
5285 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
5286 return -EINVAL;
5287 }
5288 if ((ucmd.single_wqe_log_num_of_strides >
5289 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
5290 (ucmd.single_wqe_log_num_of_strides <
5291 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES)) {
5292 mlx5_ib_dbg(dev, "Invalid log num strides (%u. Range is %u - %u)\n",
5293 ucmd.single_wqe_log_num_of_strides,
5294 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
5295 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
5296 return -EINVAL;
5297 }
5298 rwq->single_stride_log_num_of_bytes =
5299 ucmd.single_stride_log_num_of_bytes;
5300 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
5301 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
5302 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005303 }
5304
5305 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
5306 if (err) {
5307 mlx5_ib_dbg(dev, "err %d\n", err);
5308 return err;
5309 }
5310
5311 err = create_user_rq(dev, pd, rwq, &ucmd);
5312 if (err) {
5313 mlx5_ib_dbg(dev, "err %d\n", err);
5314 if (err)
5315 return err;
5316 }
5317
5318 rwq->user_index = ucmd.user_index;
5319 return 0;
5320}
5321
5322struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
5323 struct ib_wq_init_attr *init_attr,
5324 struct ib_udata *udata)
5325{
5326 struct mlx5_ib_dev *dev;
5327 struct mlx5_ib_rwq *rwq;
5328 struct mlx5_ib_create_wq_resp resp = {};
5329 size_t min_resp_len;
5330 int err;
5331
5332 if (!udata)
5333 return ERR_PTR(-ENOSYS);
5334
5335 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5336 if (udata->outlen && udata->outlen < min_resp_len)
5337 return ERR_PTR(-EINVAL);
5338
5339 dev = to_mdev(pd->device);
5340 switch (init_attr->wq_type) {
5341 case IB_WQT_RQ:
5342 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
5343 if (!rwq)
5344 return ERR_PTR(-ENOMEM);
5345 err = prepare_user_rq(pd, init_attr, udata, rwq);
5346 if (err)
5347 goto err;
5348 err = create_rq(rwq, pd, init_attr);
5349 if (err)
5350 goto err_user_rq;
5351 break;
5352 default:
5353 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
5354 init_attr->wq_type);
5355 return ERR_PTR(-EINVAL);
5356 }
5357
Yishai Hadas350d0e42016-08-28 14:58:18 +03005358 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005359 rwq->ibwq.state = IB_WQS_RESET;
5360 if (udata->outlen) {
5361 resp.response_length = offsetof(typeof(resp), response_length) +
5362 sizeof(resp.response_length);
5363 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5364 if (err)
5365 goto err_copy;
5366 }
5367
Yishai Hadas350d0e42016-08-28 14:58:18 +03005368 rwq->core_qp.event = mlx5_ib_wq_event;
5369 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005370 return &rwq->ibwq;
5371
5372err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03005373 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005374err_user_rq:
Maor Gottliebfe248c32017-05-30 10:29:14 +03005375 destroy_user_rq(dev, pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005376err:
5377 kfree(rwq);
5378 return ERR_PTR(err);
5379}
5380
5381int mlx5_ib_destroy_wq(struct ib_wq *wq)
5382{
5383 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5384 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5385
Yishai Hadas350d0e42016-08-28 14:58:18 +03005386 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005387 destroy_user_rq(dev, wq->pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005388 kfree(rwq);
5389
5390 return 0;
5391}
5392
Yishai Hadasc5f90922016-05-23 15:20:53 +03005393struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
5394 struct ib_rwq_ind_table_init_attr *init_attr,
5395 struct ib_udata *udata)
5396{
5397 struct mlx5_ib_dev *dev = to_mdev(device);
5398 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
5399 int sz = 1 << init_attr->log_ind_tbl_size;
5400 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
5401 size_t min_resp_len;
5402 int inlen;
5403 int err;
5404 int i;
5405 u32 *in;
5406 void *rqtc;
5407
5408 if (udata->inlen > 0 &&
5409 !ib_is_udata_cleared(udata, 0,
5410 udata->inlen))
5411 return ERR_PTR(-EOPNOTSUPP);
5412
Maor Gottliebefd7f402016-10-27 16:36:40 +03005413 if (init_attr->log_ind_tbl_size >
5414 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
5415 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
5416 init_attr->log_ind_tbl_size,
5417 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
5418 return ERR_PTR(-EINVAL);
5419 }
5420
Yishai Hadasc5f90922016-05-23 15:20:53 +03005421 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5422 if (udata->outlen && udata->outlen < min_resp_len)
5423 return ERR_PTR(-EINVAL);
5424
5425 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
5426 if (!rwq_ind_tbl)
5427 return ERR_PTR(-ENOMEM);
5428
5429 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005430 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005431 if (!in) {
5432 err = -ENOMEM;
5433 goto err;
5434 }
5435
5436 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
5437
5438 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
5439 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
5440
5441 for (i = 0; i < sz; i++)
5442 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
5443
5444 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
5445 kvfree(in);
5446
5447 if (err)
5448 goto err;
5449
5450 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
5451 if (udata->outlen) {
5452 resp.response_length = offsetof(typeof(resp), response_length) +
5453 sizeof(resp.response_length);
5454 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5455 if (err)
5456 goto err_copy;
5457 }
5458
5459 return &rwq_ind_tbl->ib_rwq_ind_tbl;
5460
5461err_copy:
5462 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
5463err:
5464 kfree(rwq_ind_tbl);
5465 return ERR_PTR(err);
5466}
5467
5468int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
5469{
5470 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
5471 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
5472
5473 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
5474
5475 kfree(rwq_ind_tbl);
5476 return 0;
5477}
5478
Yishai Hadas79b20a62016-05-23 15:20:50 +03005479int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
5480 u32 wq_attr_mask, struct ib_udata *udata)
5481{
5482 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5483 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5484 struct mlx5_ib_modify_wq ucmd = {};
5485 size_t required_cmd_sz;
5486 int curr_wq_state;
5487 int wq_state;
5488 int inlen;
5489 int err;
5490 void *rqc;
5491 void *in;
5492
5493 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
5494 if (udata->inlen < required_cmd_sz)
5495 return -EINVAL;
5496
5497 if (udata->inlen > sizeof(ucmd) &&
5498 !ib_is_udata_cleared(udata, sizeof(ucmd),
5499 udata->inlen - sizeof(ucmd)))
5500 return -EOPNOTSUPP;
5501
5502 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
5503 return -EFAULT;
5504
5505 if (ucmd.comp_mask || ucmd.reserved)
5506 return -EOPNOTSUPP;
5507
5508 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005509 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005510 if (!in)
5511 return -ENOMEM;
5512
5513 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
5514
5515 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
5516 wq_attr->curr_wq_state : wq->state;
5517 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
5518 wq_attr->wq_state : curr_wq_state;
5519 if (curr_wq_state == IB_WQS_ERR)
5520 curr_wq_state = MLX5_RQC_STATE_ERR;
5521 if (wq_state == IB_WQS_ERR)
5522 wq_state = MLX5_RQC_STATE_ERR;
5523 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
5524 MLX5_SET(rqc, rqc, state, wq_state);
5525
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005526 if (wq_attr_mask & IB_WQ_FLAGS) {
5527 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
5528 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
5529 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
5530 mlx5_ib_dbg(dev, "VLAN offloads are not "
5531 "supported\n");
5532 err = -EOPNOTSUPP;
5533 goto out;
5534 }
5535 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5536 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
5537 MLX5_SET(rqc, rqc, vsd,
5538 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
5539 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005540
5541 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5542 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
5543 err = -EOPNOTSUPP;
5544 goto out;
5545 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005546 }
5547
Majd Dibbiny23a69642017-01-18 15:25:10 +02005548 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
5549 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
5550 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5551 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03005552 MLX5_SET(rqc, rqc, counter_set_id,
5553 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005554 } else
5555 pr_info_once("%s: Receive WQ counters are not supported on current FW\n",
5556 dev->ib_dev.name);
5557 }
5558
Yishai Hadas350d0e42016-08-28 14:58:18 +03005559 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005560 if (!err)
5561 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
5562
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005563out:
5564 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005565 return err;
5566}