blob: 5c7ce9bd466e11f597235d9be69a2b3e8b087a14 [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{
496 return get_num_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
497}
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
Eli Cohenb037c292017-01-03 23:55:26 +0200584static void 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;
Eli Cohene126ba92013-07-07 17:25:49 +0300616 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300617 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200618 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300619 case IB_QPT_MAX:
620 default: return -EINVAL;
621 }
622}
623
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300624static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
625 struct mlx5_ib_cq *recv_cq);
626static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
627 struct mlx5_ib_cq *recv_cq);
628
Eli Cohenb037c292017-01-03 23:55:26 +0200629static int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
630 struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300631{
Eli Cohenb037c292017-01-03 23:55:26 +0200632 int bfregs_per_sys_page;
633 int index_of_sys_page;
634 int offset;
635
636 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
637 MLX5_NON_FP_BFREGS_PER_UAR;
638 index_of_sys_page = bfregn / bfregs_per_sys_page;
639
640 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
641
642 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300643}
644
majd@mellanox.com19098df2016-01-14 19:13:03 +0200645static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev,
646 struct ib_pd *pd,
647 unsigned long addr, size_t size,
648 struct ib_umem **umem,
649 int *npages, int *page_shift, int *ncont,
650 u32 *offset)
651{
652 int err;
653
654 *umem = ib_umem_get(pd->uobject->context, addr, size, 0, 0);
655 if (IS_ERR(*umem)) {
656 mlx5_ib_dbg(dev, "umem_get failed\n");
657 return PTR_ERR(*umem);
658 }
659
Majd Dibbiny762f8992016-10-27 16:36:47 +0300660 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200661
662 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
663 if (err) {
664 mlx5_ib_warn(dev, "bad offset\n");
665 goto err_umem;
666 }
667
668 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
669 addr, size, *npages, *page_shift, *ncont, *offset);
670
671 return 0;
672
673err_umem:
674 ib_umem_release(*umem);
675 *umem = NULL;
676
677 return err;
678}
679
Maor Gottliebfe248c32017-05-30 10:29:14 +0300680static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
681 struct mlx5_ib_rwq *rwq)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300682{
683 struct mlx5_ib_ucontext *context;
684
Maor Gottliebfe248c32017-05-30 10:29:14 +0300685 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
686 atomic_dec(&dev->delay_drop.rqs_cnt);
687
Yishai Hadas79b20a62016-05-23 15:20:50 +0300688 context = to_mucontext(pd->uobject->context);
689 mlx5_ib_db_unmap_user(context, &rwq->db);
690 if (rwq->umem)
691 ib_umem_release(rwq->umem);
692}
693
694static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
695 struct mlx5_ib_rwq *rwq,
696 struct mlx5_ib_create_wq *ucmd)
697{
698 struct mlx5_ib_ucontext *context;
699 int page_shift = 0;
700 int npages;
701 u32 offset = 0;
702 int ncont = 0;
703 int err;
704
705 if (!ucmd->buf_addr)
706 return -EINVAL;
707
708 context = to_mucontext(pd->uobject->context);
709 rwq->umem = ib_umem_get(pd->uobject->context, ucmd->buf_addr,
710 rwq->buf_size, 0, 0);
711 if (IS_ERR(rwq->umem)) {
712 mlx5_ib_dbg(dev, "umem_get failed\n");
713 err = PTR_ERR(rwq->umem);
714 return err;
715 }
716
Majd Dibbiny762f8992016-10-27 16:36:47 +0300717 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300718 &ncont, NULL);
719 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
720 &rwq->rq_page_offset);
721 if (err) {
722 mlx5_ib_warn(dev, "bad offset\n");
723 goto err_umem;
724 }
725
726 rwq->rq_num_pas = ncont;
727 rwq->page_shift = page_shift;
728 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
729 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
730
731 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
732 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
733 npages, page_shift, ncont, offset);
734
735 err = mlx5_ib_db_map_user(context, ucmd->db_addr, &rwq->db);
736 if (err) {
737 mlx5_ib_dbg(dev, "map failed\n");
738 goto err_umem;
739 }
740
741 rwq->create_type = MLX5_WQ_USER;
742 return 0;
743
744err_umem:
745 ib_umem_release(rwq->umem);
746 return err;
747}
748
Eli Cohenb037c292017-01-03 23:55:26 +0200749static int adjust_bfregn(struct mlx5_ib_dev *dev,
750 struct mlx5_bfreg_info *bfregi, int bfregn)
751{
752 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
753 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
754}
755
Eli Cohene126ba92013-07-07 17:25:49 +0300756static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
757 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200758 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300759 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200760 struct mlx5_ib_create_qp_resp *resp, int *inlen,
761 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300762{
763 struct mlx5_ib_ucontext *context;
764 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200765 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200766 int page_shift = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300767 int uar_index;
768 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200769 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200770 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200771 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300772 __be64 *pas;
773 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300774 int err;
775
776 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
777 if (err) {
778 mlx5_ib_dbg(dev, "copy failed\n");
779 return err;
780 }
781
782 context = to_mucontext(pd->uobject->context);
783 /*
784 * TBD: should come from the verbs when we have the API
785 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200786 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
787 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200788 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200789 else {
Eli Cohenb037c292017-01-03 23:55:26 +0200790 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_HIGH);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200791 if (bfregn < 0) {
792 mlx5_ib_dbg(dev, "failed to allocate low latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200793 mlx5_ib_dbg(dev, "reverting to medium latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200794 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_MEDIUM);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200795 if (bfregn < 0) {
796 mlx5_ib_dbg(dev, "failed to allocate medium latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200797 mlx5_ib_dbg(dev, "reverting to high latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200798 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_LOW);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200799 if (bfregn < 0) {
800 mlx5_ib_warn(dev, "bfreg allocation failed\n");
801 return bfregn;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200802 }
Eli Cohenc1be5232014-01-14 17:45:12 +0200803 }
Eli Cohene126ba92013-07-07 17:25:49 +0300804 }
805 }
806
Eli Cohenb037c292017-01-03 23:55:26 +0200807 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200808 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Eli Cohene126ba92013-07-07 17:25:49 +0300809
Haggai Eran48fea832014-05-22 14:50:11 +0300810 qp->rq.offset = 0;
811 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
812 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
813
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200814 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300815 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200816 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300817
majd@mellanox.com19098df2016-01-14 19:13:03 +0200818 if (ucmd.buf_addr && ubuffer->buf_size) {
819 ubuffer->buf_addr = ucmd.buf_addr;
820 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr,
821 ubuffer->buf_size,
822 &ubuffer->umem, &npages, &page_shift,
823 &ncont, &offset);
824 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200825 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200826 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200827 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300828 }
Eli Cohene126ba92013-07-07 17:25:49 +0300829
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300830 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
831 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300832 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300833 if (!*in) {
834 err = -ENOMEM;
835 goto err_umem;
836 }
Eli Cohene126ba92013-07-07 17:25:49 +0300837
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300838 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
839 if (ubuffer->umem)
840 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
841
842 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
843
844 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
845 MLX5_SET(qpc, qpc, page_offset, offset);
846
847 MLX5_SET(qpc, qpc, uar_page, uar_index);
Eli Cohenb037c292017-01-03 23:55:26 +0200848 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200849 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300850
851 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
852 if (err) {
853 mlx5_ib_dbg(dev, "map failed\n");
854 goto err_free;
855 }
856
857 err = ib_copy_to_udata(udata, resp, sizeof(*resp));
858 if (err) {
859 mlx5_ib_dbg(dev, "copy failed\n");
860 goto err_unmap;
861 }
862 qp->create_type = MLX5_QP_USER;
863
864 return 0;
865
866err_unmap:
867 mlx5_ib_db_unmap_user(context, &qp->db);
868
869err_free:
Al Viro479163f2014-11-20 08:13:57 +0000870 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300871
872err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200873 if (ubuffer->umem)
874 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300875
Eli Cohen2f5ff262017-01-03 23:55:21 +0200876err_bfreg:
Eli Cohenb037c292017-01-03 23:55:26 +0200877 free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300878 return err;
879}
880
Eli Cohenb037c292017-01-03 23:55:26 +0200881static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
882 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300883{
884 struct mlx5_ib_ucontext *context;
885
886 context = to_mucontext(pd->uobject->context);
887 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200888 if (base->ubuffer.umem)
889 ib_umem_release(base->ubuffer.umem);
Eli Cohenb037c292017-01-03 23:55:26 +0200890 free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300891}
892
893static int create_kernel_qp(struct mlx5_ib_dev *dev,
894 struct ib_qp_init_attr *init_attr,
895 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300896 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200897 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300898{
Eli Cohene126ba92013-07-07 17:25:49 +0300899 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300900 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300901 int err;
902
Erez Shitritf0313962016-02-21 16:27:17 +0200903 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
904 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200905 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +0300906 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200907 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200908 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300909
910 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200911 qp->bf.bfreg = &dev->fp_bfreg;
912 else
913 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300914
Eli Cohend8030b02017-02-09 19:31:47 +0200915 /* We need to divide by two since each register is comprised of
916 * two buffers of identical size, namely odd and even
917 */
918 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200919 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +0300920
921 err = calc_sq_size(dev, init_attr, qp);
922 if (err < 0) {
923 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200924 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300925 }
926
927 qp->rq.offset = 0;
928 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200929 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300930
majd@mellanox.com19098df2016-01-14 19:13:03 +0200931 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300932 if (err) {
933 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200934 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300935 }
936
937 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300938 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
939 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300940 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300941 if (!*in) {
942 err = -ENOMEM;
943 goto err_buf;
944 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300945
946 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
947 MLX5_SET(qpc, qpc, uar_page, uar_index);
948 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
949
Eli Cohene126ba92013-07-07 17:25:49 +0300950 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300951 MLX5_SET(qpc, qpc, fre, 1);
952 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300953
Haggai Eranb11a4f92016-02-29 15:45:03 +0200954 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300955 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200956 qp->flags |= MLX5_IB_QP_SQPN_QP1;
957 }
958
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300959 mlx5_fill_page_array(&qp->buf,
960 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300961
Jack Morgenstein9603b612014-07-28 23:30:22 +0300962 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300963 if (err) {
964 mlx5_ib_dbg(dev, "err %d\n", err);
965 goto err_free;
966 }
967
Eli Cohene126ba92013-07-07 17:25:49 +0300968 qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wrid), GFP_KERNEL);
969 qp->sq.wr_data = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wr_data), GFP_KERNEL);
970 qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof(*qp->rq.wrid), GFP_KERNEL);
971 qp->sq.w_list = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.w_list), GFP_KERNEL);
972 qp->sq.wqe_head = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wqe_head), GFP_KERNEL);
973
974 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
975 !qp->sq.w_list || !qp->sq.wqe_head) {
976 err = -ENOMEM;
977 goto err_wrid;
978 }
979 qp->create_type = MLX5_QP_KERNEL;
980
981 return 0;
982
983err_wrid:
Eli Cohene126ba92013-07-07 17:25:49 +0300984 kfree(qp->sq.wqe_head);
985 kfree(qp->sq.w_list);
986 kfree(qp->sq.wrid);
987 kfree(qp->sq.wr_data);
988 kfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +0200989 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300990
991err_free:
Al Viro479163f2014-11-20 08:13:57 +0000992 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300993
994err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +0300995 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300996 return err;
997}
998
999static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1000{
Eli Cohene126ba92013-07-07 17:25:49 +03001001 kfree(qp->sq.wqe_head);
1002 kfree(qp->sq.w_list);
1003 kfree(qp->sq.wrid);
1004 kfree(qp->sq.wr_data);
1005 kfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001006 mlx5_db_free(dev->mdev, &qp->db);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001007 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001008}
1009
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001010static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001011{
1012 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
1013 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001014 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001015 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001016 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001017 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001018 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001019}
1020
1021static int is_connected(enum ib_qp_type qp_type)
1022{
1023 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1024 return 1;
1025
1026 return 0;
1027}
1028
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001029static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001030 struct mlx5_ib_qp *qp,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001031 struct mlx5_ib_sq *sq, u32 tdn)
1032{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001033 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001034 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1035
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001036 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001037 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1038 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1039
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001040 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1041}
1042
1043static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1044 struct mlx5_ib_sq *sq)
1045{
1046 mlx5_core_destroy_tis(dev->mdev, sq->tisn);
1047}
1048
1049static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1050 struct mlx5_ib_sq *sq, void *qpin,
1051 struct ib_pd *pd)
1052{
1053 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1054 __be64 *pas;
1055 void *in;
1056 void *sqc;
1057 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1058 void *wq;
1059 int inlen;
1060 int err;
1061 int page_shift = 0;
1062 int npages;
1063 int ncont = 0;
1064 u32 offset = 0;
1065
1066 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1067 &sq->ubuffer.umem, &npages, &page_shift,
1068 &ncont, &offset);
1069 if (err)
1070 return err;
1071
1072 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001073 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001074 if (!in) {
1075 err = -ENOMEM;
1076 goto err_umem;
1077 }
1078
1079 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1080 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1081 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1082 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1083 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1084 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1085 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
1086
1087 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1088 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1089 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1090 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1091 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1092 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1093 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1094 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1095 MLX5_SET(wq, wq, page_offset, offset);
1096
1097 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1098 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1099
1100 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1101
1102 kvfree(in);
1103
1104 if (err)
1105 goto err_umem;
1106
1107 return 0;
1108
1109err_umem:
1110 ib_umem_release(sq->ubuffer.umem);
1111 sq->ubuffer.umem = NULL;
1112
1113 return err;
1114}
1115
1116static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1117 struct mlx5_ib_sq *sq)
1118{
1119 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1120 ib_umem_release(sq->ubuffer.umem);
1121}
1122
1123static int get_rq_pas_size(void *qpc)
1124{
1125 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1126 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1127 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1128 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1129 u32 po_quanta = 1 << (log_page_size - 6);
1130 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1131 u32 page_size = 1 << log_page_size;
1132 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1133 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1134
1135 return rq_num_pas * sizeof(u64);
1136}
1137
1138static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1139 struct mlx5_ib_rq *rq, void *qpin)
1140{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001141 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001142 __be64 *pas;
1143 __be64 *qp_pas;
1144 void *in;
1145 void *rqc;
1146 void *wq;
1147 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1148 int inlen;
1149 int err;
1150 u32 rq_pas_size = get_rq_pas_size(qpc);
1151
1152 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001153 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001154 if (!in)
1155 return -ENOMEM;
1156
1157 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001158 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1159 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001160 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1161 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1162 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1163 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1164 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1165
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001166 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1167 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1168
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001169 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1170 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1171 MLX5_SET(wq, wq, end_padding_mode,
Maor Gottlieb01581fb2016-01-28 17:51:49 +02001172 MLX5_GET(qpc, qpc, end_padding_mode));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001173 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1174 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1175 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1176 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1177 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1178 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1179
1180 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1181 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1182 memcpy(pas, qp_pas, rq_pas_size);
1183
1184 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1185
1186 kvfree(in);
1187
1188 return err;
1189}
1190
1191static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1192 struct mlx5_ib_rq *rq)
1193{
1194 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1195}
1196
1197static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1198 struct mlx5_ib_rq *rq, u32 tdn)
1199{
1200 u32 *in;
1201 void *tirc;
1202 int inlen;
1203 int err;
1204
1205 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001206 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001207 if (!in)
1208 return -ENOMEM;
1209
1210 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1211 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1212 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1213 MLX5_SET(tirc, tirc, transport_domain, tdn);
1214
1215 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1216
1217 kvfree(in);
1218
1219 return err;
1220}
1221
1222static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1223 struct mlx5_ib_rq *rq)
1224{
1225 mlx5_core_destroy_tir(dev->mdev, rq->tirn);
1226}
1227
1228static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001229 u32 *in,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001230 struct ib_pd *pd)
1231{
1232 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1233 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1234 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1235 struct ib_uobject *uobj = pd->uobject;
1236 struct ib_ucontext *ucontext = uobj->context;
1237 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1238 int err;
1239 u32 tdn = mucontext->tdn;
1240
1241 if (qp->sq.wqe_cnt) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001242 err = create_raw_packet_qp_tis(dev, qp, sq, tdn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001243 if (err)
1244 return err;
1245
1246 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1247 if (err)
1248 goto err_destroy_tis;
1249
1250 sq->base.container_mibqp = qp;
1251 }
1252
1253 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001254 rq->base.container_mibqp = qp;
1255
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001256 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1257 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001258 err = create_raw_packet_qp_rq(dev, rq, in);
1259 if (err)
1260 goto err_destroy_sq;
1261
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001262
1263 err = create_raw_packet_qp_tir(dev, rq, tdn);
1264 if (err)
1265 goto err_destroy_rq;
1266 }
1267
1268 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1269 rq->base.mqp.qpn;
1270
1271 return 0;
1272
1273err_destroy_rq:
1274 destroy_raw_packet_qp_rq(dev, rq);
1275err_destroy_sq:
1276 if (!qp->sq.wqe_cnt)
1277 return err;
1278 destroy_raw_packet_qp_sq(dev, sq);
1279err_destroy_tis:
1280 destroy_raw_packet_qp_tis(dev, sq);
1281
1282 return err;
1283}
1284
1285static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1286 struct mlx5_ib_qp *qp)
1287{
1288 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1289 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1290 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1291
1292 if (qp->rq.wqe_cnt) {
1293 destroy_raw_packet_qp_tir(dev, rq);
1294 destroy_raw_packet_qp_rq(dev, rq);
1295 }
1296
1297 if (qp->sq.wqe_cnt) {
1298 destroy_raw_packet_qp_sq(dev, sq);
1299 destroy_raw_packet_qp_tis(dev, sq);
1300 }
1301}
1302
1303static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1304 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1305{
1306 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1307 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1308
1309 sq->sq = &qp->sq;
1310 rq->rq = &qp->rq;
1311 sq->doorbell = &qp->db;
1312 rq->doorbell = &qp->db;
1313}
1314
Yishai Hadas28d61372016-05-23 15:20:56 +03001315static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1316{
1317 mlx5_core_destroy_tir(dev->mdev, qp->rss_qp.tirn);
1318}
1319
1320static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1321 struct ib_pd *pd,
1322 struct ib_qp_init_attr *init_attr,
1323 struct ib_udata *udata)
1324{
1325 struct ib_uobject *uobj = pd->uobject;
1326 struct ib_ucontext *ucontext = uobj->context;
1327 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1328 struct mlx5_ib_create_qp_resp resp = {};
1329 int inlen;
1330 int err;
1331 u32 *in;
1332 void *tirc;
1333 void *hfso;
1334 u32 selected_fields = 0;
1335 size_t min_resp_len;
1336 u32 tdn = mucontext->tdn;
1337 struct mlx5_ib_create_qp_rss ucmd = {};
1338 size_t required_cmd_sz;
1339
1340 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1341 return -EOPNOTSUPP;
1342
1343 if (init_attr->create_flags || init_attr->send_cq)
1344 return -EINVAL;
1345
Eli Cohen2f5ff262017-01-03 23:55:21 +02001346 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001347 if (udata->outlen < min_resp_len)
1348 return -EINVAL;
1349
1350 required_cmd_sz = offsetof(typeof(ucmd), reserved1) + sizeof(ucmd.reserved1);
1351 if (udata->inlen < required_cmd_sz) {
1352 mlx5_ib_dbg(dev, "invalid inlen\n");
1353 return -EINVAL;
1354 }
1355
1356 if (udata->inlen > sizeof(ucmd) &&
1357 !ib_is_udata_cleared(udata, sizeof(ucmd),
1358 udata->inlen - sizeof(ucmd))) {
1359 mlx5_ib_dbg(dev, "inlen is not supported\n");
1360 return -EOPNOTSUPP;
1361 }
1362
1363 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1364 mlx5_ib_dbg(dev, "copy failed\n");
1365 return -EFAULT;
1366 }
1367
1368 if (ucmd.comp_mask) {
1369 mlx5_ib_dbg(dev, "invalid comp mask\n");
1370 return -EOPNOTSUPP;
1371 }
1372
1373 if (memchr_inv(ucmd.reserved, 0, sizeof(ucmd.reserved)) || ucmd.reserved1) {
1374 mlx5_ib_dbg(dev, "invalid reserved\n");
1375 return -EOPNOTSUPP;
1376 }
1377
1378 err = ib_copy_to_udata(udata, &resp, min_resp_len);
1379 if (err) {
1380 mlx5_ib_dbg(dev, "copy failed\n");
1381 return -EINVAL;
1382 }
1383
1384 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001385 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001386 if (!in)
1387 return -ENOMEM;
1388
1389 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1390 MLX5_SET(tirc, tirc, disp_type,
1391 MLX5_TIRC_DISP_TYPE_INDIRECT);
1392 MLX5_SET(tirc, tirc, indirect_table,
1393 init_attr->rwq_ind_tbl->ind_tbl_num);
1394 MLX5_SET(tirc, tirc, transport_domain, tdn);
1395
1396 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1397 switch (ucmd.rx_hash_function) {
1398 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1399 {
1400 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1401 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1402
1403 if (len != ucmd.rx_key_len) {
1404 err = -EINVAL;
1405 goto err;
1406 }
1407
1408 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1409 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1410 memcpy(rss_key, ucmd.rx_hash_key, len);
1411 break;
1412 }
1413 default:
1414 err = -EOPNOTSUPP;
1415 goto err;
1416 }
1417
1418 if (!ucmd.rx_hash_fields_mask) {
1419 /* special case when this TIR serves as steering entry without hashing */
1420 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1421 goto create_tir;
1422 err = -EINVAL;
1423 goto err;
1424 }
1425
1426 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1427 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1428 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1429 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1430 err = -EINVAL;
1431 goto err;
1432 }
1433
1434 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1435 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1436 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1437 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1438 MLX5_L3_PROT_TYPE_IPV4);
1439 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1440 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1441 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1442 MLX5_L3_PROT_TYPE_IPV6);
1443
1444 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1445 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) &&
1446 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1447 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))) {
1448 err = -EINVAL;
1449 goto err;
1450 }
1451
1452 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1453 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1454 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1455 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1456 MLX5_L4_PROT_TYPE_TCP);
1457 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1458 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1459 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1460 MLX5_L4_PROT_TYPE_UDP);
1461
1462 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1463 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1464 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1465
1466 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1467 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1468 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1469
1470 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1471 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1472 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1473
1474 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1475 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1476 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1477
1478 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1479
1480create_tir:
1481 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1482
1483 if (err)
1484 goto err;
1485
1486 kvfree(in);
1487 /* qpn is reserved for that QP */
1488 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001489 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001490 return 0;
1491
1492err:
1493 kvfree(in);
1494 return err;
1495}
1496
Eli Cohene126ba92013-07-07 17:25:49 +03001497static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1498 struct ib_qp_init_attr *init_attr,
1499 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1500{
1501 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001502 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001503 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001504 struct mlx5_ib_create_qp_resp resp;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001505 struct mlx5_ib_cq *send_cq;
1506 struct mlx5_ib_cq *recv_cq;
1507 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001508 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001509 struct mlx5_ib_create_qp ucmd;
1510 struct mlx5_ib_qp_base *base;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001511 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001512 u32 *in;
1513 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001514
1515 mutex_init(&qp->mutex);
1516 spin_lock_init(&qp->sq.lock);
1517 spin_lock_init(&qp->rq.lock);
1518
Yishai Hadas28d61372016-05-23 15:20:56 +03001519 if (init_attr->rwq_ind_tbl) {
1520 if (!udata)
1521 return -ENOSYS;
1522
1523 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1524 return err;
1525 }
1526
Eli Cohenf360d882014-04-02 00:10:16 +03001527 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001528 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001529 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1530 return -EINVAL;
1531 } else {
1532 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1533 }
1534 }
1535
Leon Romanovsky051f2632015-12-20 12:16:11 +02001536 if (init_attr->create_flags &
1537 (IB_QP_CREATE_CROSS_CHANNEL |
1538 IB_QP_CREATE_MANAGED_SEND |
1539 IB_QP_CREATE_MANAGED_RECV)) {
1540 if (!MLX5_CAP_GEN(mdev, cd)) {
1541 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1542 return -EINVAL;
1543 }
1544 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1545 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1546 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1547 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1548 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1549 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1550 }
Erez Shitritf0313962016-02-21 16:27:17 +02001551
1552 if (init_attr->qp_type == IB_QPT_UD &&
1553 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1554 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1555 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1556 return -EOPNOTSUPP;
1557 }
1558
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001559 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1560 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1561 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1562 return -EOPNOTSUPP;
1563 }
1564 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1565 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1566 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1567 return -EOPNOTSUPP;
1568 }
1569 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1570 }
1571
Eli Cohene126ba92013-07-07 17:25:49 +03001572 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1573 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1574
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001575 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
1576 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1577 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
1578 (init_attr->qp_type != IB_QPT_RAW_PACKET))
1579 return -EOPNOTSUPP;
1580 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
1581 }
1582
Eli Cohene126ba92013-07-07 17:25:49 +03001583 if (pd && pd->uobject) {
1584 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1585 mlx5_ib_dbg(dev, "copy failed\n");
1586 return -EFAULT;
1587 }
1588
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001589 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1590 &ucmd, udata->inlen, &uidx);
1591 if (err)
1592 return err;
1593
Eli Cohene126ba92013-07-07 17:25:49 +03001594 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1595 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001596
1597 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
1598 if (init_attr->qp_type != IB_QPT_UD ||
1599 (MLX5_CAP_GEN(dev->mdev, port_type) !=
1600 MLX5_CAP_PORT_TYPE_IB) ||
1601 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
1602 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
1603 return -EOPNOTSUPP;
1604 }
1605
1606 qp->flags |= MLX5_IB_QP_UNDERLAY;
1607 qp->underlay_qpn = init_attr->source_qpn;
1608 }
Eli Cohene126ba92013-07-07 17:25:49 +03001609 } else {
1610 qp->wq_sig = !!wq_signature;
1611 }
1612
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001613 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1614 qp->flags & MLX5_IB_QP_UNDERLAY) ?
1615 &qp->raw_packet_qp.rq.base :
1616 &qp->trans_qp.base;
1617
Eli Cohene126ba92013-07-07 17:25:49 +03001618 qp->has_rq = qp_has_rq(init_attr);
1619 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1620 qp, (pd && pd->uobject) ? &ucmd : NULL);
1621 if (err) {
1622 mlx5_ib_dbg(dev, "err %d\n", err);
1623 return err;
1624 }
1625
1626 if (pd) {
1627 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001628 __u32 max_wqes =
1629 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001630 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1631 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1632 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1633 mlx5_ib_dbg(dev, "invalid rq params\n");
1634 return -EINVAL;
1635 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001636 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001637 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001638 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001639 return -EINVAL;
1640 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001641 if (init_attr->create_flags &
1642 mlx5_ib_create_qp_sqpn_qp1()) {
1643 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1644 return -EINVAL;
1645 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001646 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1647 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001648 if (err)
1649 mlx5_ib_dbg(dev, "err %d\n", err);
1650 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001651 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1652 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001653 if (err)
1654 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001655 }
1656
1657 if (err)
1658 return err;
1659 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001660 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001661 if (!in)
1662 return -ENOMEM;
1663
1664 qp->create_type = MLX5_QP_EMPTY;
1665 }
1666
1667 if (is_sqp(init_attr->qp_type))
1668 qp->port = init_attr->port_num;
1669
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001670 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1671
1672 MLX5_SET(qpc, qpc, st, to_mlx5_st(init_attr->qp_type));
1673 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001674
1675 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001676 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001677 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001678 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1679
Eli Cohene126ba92013-07-07 17:25:49 +03001680
1681 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001682 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001683
Eli Cohenf360d882014-04-02 00:10:16 +03001684 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001685 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001686
Leon Romanovsky051f2632015-12-20 12:16:11 +02001687 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001688 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001689 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001690 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001691 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001692 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001693
Eli Cohene126ba92013-07-07 17:25:49 +03001694 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1695 int rcqe_sz;
1696 int scqe_sz;
1697
1698 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1699 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1700
1701 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001702 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001703 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001704 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001705
1706 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1707 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001708 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001709 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001710 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001711 }
1712 }
1713
1714 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001715 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1716 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001717 }
1718
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001719 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001720
1721 if (qp->sq.wqe_cnt)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001722 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001723 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001724 MLX5_SET(qpc, qpc, no_sq, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001725
1726 /* Set default resources */
1727 switch (init_attr->qp_type) {
1728 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001729 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1730 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1731 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1732 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001733 break;
1734 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001735 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1736 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1737 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001738 break;
1739 default:
1740 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001741 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1742 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001743 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001744 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1745 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001746 }
1747 }
1748
1749 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001750 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001751
1752 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001753 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001754
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001755 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001756
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001757 /* 0xffffff means we ask to work with cqe version 0 */
1758 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001759 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001760
Erez Shitritf0313962016-02-21 16:27:17 +02001761 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1762 if (init_attr->qp_type == IB_QPT_UD &&
1763 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001764 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1765 qp->flags |= MLX5_IB_QP_LSO;
1766 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001767
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001768 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1769 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001770 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
1771 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
1772 err = create_raw_packet_qp(dev, qp, in, pd);
1773 } else {
1774 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
1775 }
1776
Eli Cohene126ba92013-07-07 17:25:49 +03001777 if (err) {
1778 mlx5_ib_dbg(dev, "create qp failed\n");
1779 goto err_create;
1780 }
1781
Al Viro479163f2014-11-20 08:13:57 +00001782 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001783
majd@mellanox.com19098df2016-01-14 19:13:03 +02001784 base->container_mibqp = qp;
1785 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03001786
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001787 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
1788 &send_cq, &recv_cq);
1789 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1790 mlx5_ib_lock_cqs(send_cq, recv_cq);
1791 /* Maintain device to QPs access, needed for further handling via reset
1792 * flow
1793 */
1794 list_add_tail(&qp->qps_list, &dev->qp_list);
1795 /* Maintain CQ to QPs access, needed for further handling via reset flow
1796 */
1797 if (send_cq)
1798 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
1799 if (recv_cq)
1800 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
1801 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1802 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1803
Eli Cohene126ba92013-07-07 17:25:49 +03001804 return 0;
1805
1806err_create:
1807 if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02001808 destroy_qp_user(dev, pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001809 else if (qp->create_type == MLX5_QP_KERNEL)
1810 destroy_qp_kernel(dev, qp);
1811
Al Viro479163f2014-11-20 08:13:57 +00001812 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001813 return err;
1814}
1815
1816static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1817 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1818{
1819 if (send_cq) {
1820 if (recv_cq) {
1821 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001822 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001823 spin_lock_nested(&recv_cq->lock,
1824 SINGLE_DEPTH_NESTING);
1825 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001826 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001827 __acquire(&recv_cq->lock);
1828 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001829 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001830 spin_lock_nested(&send_cq->lock,
1831 SINGLE_DEPTH_NESTING);
1832 }
1833 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001834 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001835 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001836 }
1837 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001838 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001839 __acquire(&send_cq->lock);
1840 } else {
1841 __acquire(&send_cq->lock);
1842 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001843 }
1844}
1845
1846static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1847 __releases(&send_cq->lock) __releases(&recv_cq->lock)
1848{
1849 if (send_cq) {
1850 if (recv_cq) {
1851 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1852 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001853 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001854 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1855 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001856 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001857 } else {
1858 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001859 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001860 }
1861 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001862 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001863 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001864 }
1865 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001866 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001867 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001868 } else {
1869 __release(&recv_cq->lock);
1870 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001871 }
1872}
1873
1874static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
1875{
1876 return to_mpd(qp->ibqp.pd);
1877}
1878
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001879static void get_cqs(enum ib_qp_type qp_type,
1880 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03001881 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
1882{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001883 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03001884 case IB_QPT_XRC_TGT:
1885 *send_cq = NULL;
1886 *recv_cq = NULL;
1887 break;
1888 case MLX5_IB_QPT_REG_UMR:
1889 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001890 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001891 *recv_cq = NULL;
1892 break;
1893
1894 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02001895 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03001896 case IB_QPT_RC:
1897 case IB_QPT_UC:
1898 case IB_QPT_UD:
1899 case IB_QPT_RAW_IPV6:
1900 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001901 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001902 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
1903 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001904 break;
1905
Eli Cohene126ba92013-07-07 17:25:49 +03001906 case IB_QPT_MAX:
1907 default:
1908 *send_cq = NULL;
1909 *recv_cq = NULL;
1910 break;
1911 }
1912}
1913
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001914static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03001915 const struct mlx5_modify_raw_qp_param *raw_qp_param,
1916 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001917
Eli Cohene126ba92013-07-07 17:25:49 +03001918static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1919{
1920 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001921 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001922 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03001923 int err;
1924
Yishai Hadas28d61372016-05-23 15:20:56 +03001925 if (qp->ibqp.rwq_ind_tbl) {
1926 destroy_rss_raw_qp_tir(dev, qp);
1927 return;
1928 }
1929
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001930 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
1931 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001932 &qp->raw_packet_qp.rq.base :
1933 &qp->trans_qp.base;
1934
Haggai Eran6aec21f2014-12-11 17:04:23 +02001935 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001936 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
1937 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001938 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03001939 MLX5_CMD_OP_2RST_QP, 0,
1940 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001941 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03001942 struct mlx5_modify_raw_qp_param raw_qp_param = {
1943 .operation = MLX5_CMD_OP_2RST_QP
1944 };
1945
Aviv Heller13eab212016-09-18 20:48:04 +03001946 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001947 }
1948 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02001949 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02001950 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02001951 }
Eli Cohene126ba92013-07-07 17:25:49 +03001952
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001953 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
1954 &send_cq, &recv_cq);
1955
1956 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1957 mlx5_ib_lock_cqs(send_cq, recv_cq);
1958 /* del from lists under both locks above to protect reset flow paths */
1959 list_del(&qp->qps_list);
1960 if (send_cq)
1961 list_del(&qp->cq_send_list);
1962
1963 if (recv_cq)
1964 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001965
1966 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001967 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03001968 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
1969 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001970 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
1971 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03001972 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001973 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1974 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03001975
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001976 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
1977 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001978 destroy_raw_packet_qp(dev, qp);
1979 } else {
1980 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
1981 if (err)
1982 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
1983 base->mqp.qpn);
1984 }
Eli Cohene126ba92013-07-07 17:25:49 +03001985
Eli Cohene126ba92013-07-07 17:25:49 +03001986 if (qp->create_type == MLX5_QP_KERNEL)
1987 destroy_qp_kernel(dev, qp);
1988 else if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02001989 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001990}
1991
1992static const char *ib_qp_type_str(enum ib_qp_type type)
1993{
1994 switch (type) {
1995 case IB_QPT_SMI:
1996 return "IB_QPT_SMI";
1997 case IB_QPT_GSI:
1998 return "IB_QPT_GSI";
1999 case IB_QPT_RC:
2000 return "IB_QPT_RC";
2001 case IB_QPT_UC:
2002 return "IB_QPT_UC";
2003 case IB_QPT_UD:
2004 return "IB_QPT_UD";
2005 case IB_QPT_RAW_IPV6:
2006 return "IB_QPT_RAW_IPV6";
2007 case IB_QPT_RAW_ETHERTYPE:
2008 return "IB_QPT_RAW_ETHERTYPE";
2009 case IB_QPT_XRC_INI:
2010 return "IB_QPT_XRC_INI";
2011 case IB_QPT_XRC_TGT:
2012 return "IB_QPT_XRC_TGT";
2013 case IB_QPT_RAW_PACKET:
2014 return "IB_QPT_RAW_PACKET";
2015 case MLX5_IB_QPT_REG_UMR:
2016 return "MLX5_IB_QPT_REG_UMR";
2017 case IB_QPT_MAX:
2018 default:
2019 return "Invalid QP type";
2020 }
2021}
2022
2023struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
2024 struct ib_qp_init_attr *init_attr,
2025 struct ib_udata *udata)
2026{
2027 struct mlx5_ib_dev *dev;
2028 struct mlx5_ib_qp *qp;
2029 u16 xrcdn = 0;
2030 int err;
2031
2032 if (pd) {
2033 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002034
2035 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2036 if (!pd->uobject) {
2037 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2038 return ERR_PTR(-EINVAL);
2039 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2040 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2041 return ERR_PTR(-EINVAL);
2042 }
2043 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002044 } else {
2045 /* being cautious here */
2046 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2047 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2048 pr_warn("%s: no PD for transport %s\n", __func__,
2049 ib_qp_type_str(init_attr->qp_type));
2050 return ERR_PTR(-EINVAL);
2051 }
2052 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002053 }
2054
2055 switch (init_attr->qp_type) {
2056 case IB_QPT_XRC_TGT:
2057 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002058 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002059 mlx5_ib_dbg(dev, "XRC not supported\n");
2060 return ERR_PTR(-ENOSYS);
2061 }
2062 init_attr->recv_cq = NULL;
2063 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2064 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2065 init_attr->send_cq = NULL;
2066 }
2067
2068 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002069 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002070 case IB_QPT_RC:
2071 case IB_QPT_UC:
2072 case IB_QPT_UD:
2073 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002074 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002075 case MLX5_IB_QPT_REG_UMR:
2076 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2077 if (!qp)
2078 return ERR_PTR(-ENOMEM);
2079
2080 err = create_qp_common(dev, pd, init_attr, udata, qp);
2081 if (err) {
2082 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2083 kfree(qp);
2084 return ERR_PTR(err);
2085 }
2086
2087 if (is_qp0(init_attr->qp_type))
2088 qp->ibqp.qp_num = 0;
2089 else if (is_qp1(init_attr->qp_type))
2090 qp->ibqp.qp_num = 1;
2091 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002092 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002093
2094 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 +02002095 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002096 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2097 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002098
majd@mellanox.com19098df2016-01-14 19:13:03 +02002099 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002100
2101 break;
2102
Haggai Erand16e91d2016-02-29 15:45:05 +02002103 case IB_QPT_GSI:
2104 return mlx5_ib_gsi_create_qp(pd, init_attr);
2105
Eli Cohene126ba92013-07-07 17:25:49 +03002106 case IB_QPT_RAW_IPV6:
2107 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002108 case IB_QPT_MAX:
2109 default:
2110 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2111 init_attr->qp_type);
2112 /* Don't support raw QPs */
2113 return ERR_PTR(-EINVAL);
2114 }
2115
2116 return &qp->ibqp;
2117}
2118
2119int mlx5_ib_destroy_qp(struct ib_qp *qp)
2120{
2121 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2122 struct mlx5_ib_qp *mqp = to_mqp(qp);
2123
Haggai Erand16e91d2016-02-29 15:45:05 +02002124 if (unlikely(qp->qp_type == IB_QPT_GSI))
2125 return mlx5_ib_gsi_destroy_qp(qp);
2126
Eli Cohene126ba92013-07-07 17:25:49 +03002127 destroy_qp_common(dev, mqp);
2128
2129 kfree(mqp);
2130
2131 return 0;
2132}
2133
2134static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2135 int attr_mask)
2136{
2137 u32 hw_access_flags = 0;
2138 u8 dest_rd_atomic;
2139 u32 access_flags;
2140
2141 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2142 dest_rd_atomic = attr->max_dest_rd_atomic;
2143 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002144 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002145
2146 if (attr_mask & IB_QP_ACCESS_FLAGS)
2147 access_flags = attr->qp_access_flags;
2148 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002149 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002150
2151 if (!dest_rd_atomic)
2152 access_flags &= IB_ACCESS_REMOTE_WRITE;
2153
2154 if (access_flags & IB_ACCESS_REMOTE_READ)
2155 hw_access_flags |= MLX5_QP_BIT_RRE;
2156 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2157 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2158 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2159 hw_access_flags |= MLX5_QP_BIT_RWE;
2160
2161 return cpu_to_be32(hw_access_flags);
2162}
2163
2164enum {
2165 MLX5_PATH_FLAG_FL = 1 << 0,
2166 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2167 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2168};
2169
2170static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2171{
2172 if (rate == IB_RATE_PORT_CURRENT) {
2173 return 0;
2174 } else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
2175 return -EINVAL;
2176 } else {
2177 while (rate != IB_RATE_2_5_GBPS &&
2178 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
Saeed Mahameed938fe832015-05-28 22:28:41 +03002179 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
Eli Cohene126ba92013-07-07 17:25:49 +03002180 --rate;
2181 }
2182
2183 return rate + MLX5_STAT_RATE_OFFSET;
2184}
2185
majd@mellanox.com75850d02016-01-14 19:13:06 +02002186static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
2187 struct mlx5_ib_sq *sq, u8 sl)
2188{
2189 void *in;
2190 void *tisc;
2191 int inlen;
2192 int err;
2193
2194 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002195 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002196 if (!in)
2197 return -ENOMEM;
2198
2199 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
2200
2201 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2202 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2203
2204 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2205
2206 kvfree(in);
2207
2208 return err;
2209}
2210
Aviv Heller13eab212016-09-18 20:48:04 +03002211static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
2212 struct mlx5_ib_sq *sq, u8 tx_affinity)
2213{
2214 void *in;
2215 void *tisc;
2216 int inlen;
2217 int err;
2218
2219 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002220 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002221 if (!in)
2222 return -ENOMEM;
2223
2224 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
2225
2226 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2227 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2228
2229 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2230
2231 kvfree(in);
2232
2233 return err;
2234}
2235
majd@mellanox.com75850d02016-01-14 19:13:06 +02002236static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002237 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002238 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002239 u32 path_flags, const struct ib_qp_attr *attr,
2240 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002241{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002242 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002243 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002244 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002245 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2246 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002247
Eli Cohene126ba92013-07-07 17:25:49 +03002248 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002249 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2250 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002251
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002252 if (ah_flags & IB_AH_GRH) {
2253 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002254 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002255 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002256 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002257 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002258 return -EINVAL;
2259 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002260 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002261
2262 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002263 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002264 return -EINVAL;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002265 err = mlx5_get_roce_gid_type(dev, port, grh->sgid_index,
Majd Dibbinyed884512017-01-18 14:10:35 +02002266 &gid_type);
2267 if (err)
2268 return err;
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002269 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Achiad Shochat2811ba52015-12-23 18:47:24 +02002270 path->udp_sport = mlx5_get_roce_udp_sport(dev, port,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002271 grh->sgid_index);
2272 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Majd Dibbinyed884512017-01-18 14:10:35 +02002273 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002274 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002275 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002276 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2277 path->fl_free_ar |=
2278 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002279 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2280 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2281 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002282 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002283 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002284 }
2285
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002286 if (ah_flags & IB_AH_GRH) {
2287 path->mgid_index = grh->sgid_index;
2288 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002289 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002290 cpu_to_be32((grh->traffic_class << 20) |
2291 (grh->flow_label));
2292 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002293 }
2294
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002295 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002296 if (err < 0)
2297 return err;
2298 path->static_rate = err;
2299 path->port = port;
2300
Eli Cohene126ba92013-07-07 17:25:49 +03002301 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002302 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002303
majd@mellanox.com75850d02016-01-14 19:13:06 +02002304 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2305 return modify_raw_packet_eth_prio(dev->mdev,
2306 &qp->raw_packet_qp.sq,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002307 sl & 0xf);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002308
Eli Cohene126ba92013-07-07 17:25:49 +03002309 return 0;
2310}
2311
2312static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2313 [MLX5_QP_STATE_INIT] = {
2314 [MLX5_QP_STATE_INIT] = {
2315 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2316 MLX5_QP_OPTPAR_RAE |
2317 MLX5_QP_OPTPAR_RWE |
2318 MLX5_QP_OPTPAR_PKEY_INDEX |
2319 MLX5_QP_OPTPAR_PRI_PORT,
2320 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2321 MLX5_QP_OPTPAR_PKEY_INDEX |
2322 MLX5_QP_OPTPAR_PRI_PORT,
2323 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2324 MLX5_QP_OPTPAR_Q_KEY |
2325 MLX5_QP_OPTPAR_PRI_PORT,
2326 },
2327 [MLX5_QP_STATE_RTR] = {
2328 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2329 MLX5_QP_OPTPAR_RRE |
2330 MLX5_QP_OPTPAR_RAE |
2331 MLX5_QP_OPTPAR_RWE |
2332 MLX5_QP_OPTPAR_PKEY_INDEX,
2333 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2334 MLX5_QP_OPTPAR_RWE |
2335 MLX5_QP_OPTPAR_PKEY_INDEX,
2336 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2337 MLX5_QP_OPTPAR_Q_KEY,
2338 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2339 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002340 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2341 MLX5_QP_OPTPAR_RRE |
2342 MLX5_QP_OPTPAR_RAE |
2343 MLX5_QP_OPTPAR_RWE |
2344 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002345 },
2346 },
2347 [MLX5_QP_STATE_RTR] = {
2348 [MLX5_QP_STATE_RTS] = {
2349 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2350 MLX5_QP_OPTPAR_RRE |
2351 MLX5_QP_OPTPAR_RAE |
2352 MLX5_QP_OPTPAR_RWE |
2353 MLX5_QP_OPTPAR_PM_STATE |
2354 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2355 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2356 MLX5_QP_OPTPAR_RWE |
2357 MLX5_QP_OPTPAR_PM_STATE,
2358 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2359 },
2360 },
2361 [MLX5_QP_STATE_RTS] = {
2362 [MLX5_QP_STATE_RTS] = {
2363 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2364 MLX5_QP_OPTPAR_RAE |
2365 MLX5_QP_OPTPAR_RWE |
2366 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002367 MLX5_QP_OPTPAR_PM_STATE |
2368 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002369 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03002370 MLX5_QP_OPTPAR_PM_STATE |
2371 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002372 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
2373 MLX5_QP_OPTPAR_SRQN |
2374 MLX5_QP_OPTPAR_CQN_RCV,
2375 },
2376 },
2377 [MLX5_QP_STATE_SQER] = {
2378 [MLX5_QP_STATE_RTS] = {
2379 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2380 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002381 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002382 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2383 MLX5_QP_OPTPAR_RWE |
2384 MLX5_QP_OPTPAR_RAE |
2385 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002386 },
2387 },
2388};
2389
2390static int ib_nr_to_mlx5_nr(int ib_mask)
2391{
2392 switch (ib_mask) {
2393 case IB_QP_STATE:
2394 return 0;
2395 case IB_QP_CUR_STATE:
2396 return 0;
2397 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2398 return 0;
2399 case IB_QP_ACCESS_FLAGS:
2400 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2401 MLX5_QP_OPTPAR_RAE;
2402 case IB_QP_PKEY_INDEX:
2403 return MLX5_QP_OPTPAR_PKEY_INDEX;
2404 case IB_QP_PORT:
2405 return MLX5_QP_OPTPAR_PRI_PORT;
2406 case IB_QP_QKEY:
2407 return MLX5_QP_OPTPAR_Q_KEY;
2408 case IB_QP_AV:
2409 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2410 MLX5_QP_OPTPAR_PRI_PORT;
2411 case IB_QP_PATH_MTU:
2412 return 0;
2413 case IB_QP_TIMEOUT:
2414 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2415 case IB_QP_RETRY_CNT:
2416 return MLX5_QP_OPTPAR_RETRY_COUNT;
2417 case IB_QP_RNR_RETRY:
2418 return MLX5_QP_OPTPAR_RNR_RETRY;
2419 case IB_QP_RQ_PSN:
2420 return 0;
2421 case IB_QP_MAX_QP_RD_ATOMIC:
2422 return MLX5_QP_OPTPAR_SRA_MAX;
2423 case IB_QP_ALT_PATH:
2424 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2425 case IB_QP_MIN_RNR_TIMER:
2426 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2427 case IB_QP_SQ_PSN:
2428 return 0;
2429 case IB_QP_MAX_DEST_RD_ATOMIC:
2430 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2431 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2432 case IB_QP_PATH_MIG_STATE:
2433 return MLX5_QP_OPTPAR_PM_STATE;
2434 case IB_QP_CAP:
2435 return 0;
2436 case IB_QP_DEST_QPN:
2437 return 0;
2438 }
2439 return 0;
2440}
2441
2442static int ib_mask_to_mlx5_opt(int ib_mask)
2443{
2444 int result = 0;
2445 int i;
2446
2447 for (i = 0; i < 8 * sizeof(int); i++) {
2448 if ((1 << i) & ib_mask)
2449 result |= ib_nr_to_mlx5_nr(1 << i);
2450 }
2451
2452 return result;
2453}
2454
Alex Veskereb49ab02016-08-28 12:25:53 +03002455static int modify_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
2456 struct mlx5_ib_rq *rq, int new_state,
2457 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002458{
2459 void *in;
2460 void *rqc;
2461 int inlen;
2462 int err;
2463
2464 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002465 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002466 if (!in)
2467 return -ENOMEM;
2468
2469 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
2470
2471 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2472 MLX5_SET(rqc, rqc, state, new_state);
2473
Alex Veskereb49ab02016-08-28 12:25:53 +03002474 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2475 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2476 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02002477 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03002478 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2479 } else
2480 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2481 dev->ib_dev.name);
2482 }
2483
2484 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002485 if (err)
2486 goto out;
2487
2488 rq->state = new_state;
2489
2490out:
2491 kvfree(in);
2492 return err;
2493}
2494
2495static int modify_raw_packet_qp_sq(struct mlx5_core_dev *dev,
Bodong Wang7d29f342016-12-01 13:43:16 +02002496 struct mlx5_ib_sq *sq,
2497 int new_state,
2498 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002499{
Bodong Wang7d29f342016-12-01 13:43:16 +02002500 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
2501 u32 old_rate = ibqp->rate_limit;
2502 u32 new_rate = old_rate;
2503 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002504 void *in;
2505 void *sqc;
2506 int inlen;
2507 int err;
2508
2509 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002510 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002511 if (!in)
2512 return -ENOMEM;
2513
2514 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2515
2516 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2517 MLX5_SET(sqc, sqc, state, new_state);
2518
Bodong Wang7d29f342016-12-01 13:43:16 +02002519 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2520 if (new_state != MLX5_SQC_STATE_RDY)
2521 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2522 __func__);
2523 else
2524 new_rate = raw_qp_param->rate_limit;
2525 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002526
Bodong Wang7d29f342016-12-01 13:43:16 +02002527 if (old_rate != new_rate) {
2528 if (new_rate) {
2529 err = mlx5_rl_add_rate(dev, new_rate, &rl_index);
2530 if (err) {
2531 pr_err("Failed configuring rate %u: %d\n",
2532 new_rate, err);
2533 goto out;
2534 }
2535 }
2536
2537 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
2538 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2539 }
2540
2541 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2542 if (err) {
2543 /* Remove new rate from table if failed */
2544 if (new_rate &&
2545 old_rate != new_rate)
2546 mlx5_rl_remove_rate(dev, new_rate);
2547 goto out;
2548 }
2549
2550 /* Only remove the old rate after new rate was set */
2551 if ((old_rate &&
2552 (old_rate != new_rate)) ||
2553 (new_state != MLX5_SQC_STATE_RDY))
2554 mlx5_rl_remove_rate(dev, old_rate);
2555
2556 ibqp->rate_limit = new_rate;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002557 sq->state = new_state;
2558
2559out:
2560 kvfree(in);
2561 return err;
2562}
2563
2564static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002565 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2566 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002567{
2568 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2569 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2570 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002571 int modify_rq = !!qp->rq.wqe_cnt;
2572 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002573 int rq_state;
2574 int sq_state;
2575 int err;
2576
Alex Vesker0680efa2016-08-28 12:25:52 +03002577 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002578 case MLX5_CMD_OP_RST2INIT_QP:
2579 rq_state = MLX5_RQC_STATE_RDY;
2580 sq_state = MLX5_SQC_STATE_RDY;
2581 break;
2582 case MLX5_CMD_OP_2ERR_QP:
2583 rq_state = MLX5_RQC_STATE_ERR;
2584 sq_state = MLX5_SQC_STATE_ERR;
2585 break;
2586 case MLX5_CMD_OP_2RST_QP:
2587 rq_state = MLX5_RQC_STATE_RST;
2588 sq_state = MLX5_SQC_STATE_RST;
2589 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002590 case MLX5_CMD_OP_RTR2RTS_QP:
2591 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002592 if (raw_qp_param->set_mask ==
2593 MLX5_RAW_QP_RATE_LIMIT) {
2594 modify_rq = 0;
2595 sq_state = sq->state;
2596 } else {
2597 return raw_qp_param->set_mask ? -EINVAL : 0;
2598 }
2599 break;
2600 case MLX5_CMD_OP_INIT2INIT_QP:
2601 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002602 if (raw_qp_param->set_mask)
2603 return -EINVAL;
2604 else
2605 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002606 default:
2607 WARN_ON(1);
2608 return -EINVAL;
2609 }
2610
Bodong Wang7d29f342016-12-01 13:43:16 +02002611 if (modify_rq) {
2612 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002613 if (err)
2614 return err;
2615 }
2616
Bodong Wang7d29f342016-12-01 13:43:16 +02002617 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03002618 if (tx_affinity) {
2619 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
2620 tx_affinity);
2621 if (err)
2622 return err;
2623 }
2624
Bodong Wang7d29f342016-12-01 13:43:16 +02002625 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state, raw_qp_param);
Aviv Heller13eab212016-09-18 20:48:04 +03002626 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002627
2628 return 0;
2629}
2630
Eli Cohene126ba92013-07-07 17:25:49 +03002631static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
2632 const struct ib_qp_attr *attr, int attr_mask,
2633 enum ib_qp_state cur_state, enum ib_qp_state new_state)
2634{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002635 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
2636 [MLX5_QP_STATE_RST] = {
2637 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2638 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2639 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
2640 },
2641 [MLX5_QP_STATE_INIT] = {
2642 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2643 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2644 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
2645 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
2646 },
2647 [MLX5_QP_STATE_RTR] = {
2648 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2649 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2650 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
2651 },
2652 [MLX5_QP_STATE_RTS] = {
2653 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2654 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2655 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
2656 },
2657 [MLX5_QP_STATE_SQD] = {
2658 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2659 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2660 },
2661 [MLX5_QP_STATE_SQER] = {
2662 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2663 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2664 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
2665 },
2666 [MLX5_QP_STATE_ERR] = {
2667 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2668 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2669 }
2670 };
2671
Eli Cohene126ba92013-07-07 17:25:49 +03002672 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2673 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02002674 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03002675 struct mlx5_ib_cq *send_cq, *recv_cq;
2676 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03002677 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03002678 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002679 enum mlx5_qp_state mlx5_cur, mlx5_new;
2680 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03002681 int mlx5_st;
2682 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002683 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03002684 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002685
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002686 context = kzalloc(sizeof(*context), GFP_KERNEL);
2687 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03002688 return -ENOMEM;
2689
Eli Cohene126ba92013-07-07 17:25:49 +03002690 err = to_mlx5_st(ibqp->qp_type);
Haggai Eran158abf82016-02-29 15:45:04 +02002691 if (err < 0) {
2692 mlx5_ib_dbg(dev, "unsupported qp type %d\n", ibqp->qp_type);
Eli Cohene126ba92013-07-07 17:25:49 +03002693 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002694 }
Eli Cohene126ba92013-07-07 17:25:49 +03002695
2696 context->flags = cpu_to_be32(err << 16);
2697
2698 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
2699 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2700 } else {
2701 switch (attr->path_mig_state) {
2702 case IB_MIG_MIGRATED:
2703 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2704 break;
2705 case IB_MIG_REARM:
2706 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
2707 break;
2708 case IB_MIG_ARMED:
2709 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
2710 break;
2711 }
2712 }
2713
Aviv Heller13eab212016-09-18 20:48:04 +03002714 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2715 if ((ibqp->qp_type == IB_QPT_RC) ||
2716 (ibqp->qp_type == IB_QPT_UD &&
2717 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
2718 (ibqp->qp_type == IB_QPT_UC) ||
2719 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
2720 (ibqp->qp_type == IB_QPT_XRC_INI) ||
2721 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
2722 if (mlx5_lag_is_active(dev->mdev)) {
2723 tx_affinity = (unsigned int)atomic_add_return(1,
2724 &dev->roce.next_port) %
2725 MLX5_MAX_PORTS + 1;
2726 context->flags |= cpu_to_be32(tx_affinity << 24);
2727 }
2728 }
2729 }
2730
Haggai Erand16e91d2016-02-29 15:45:05 +02002731 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002732 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002733 } else if ((ibqp->qp_type == IB_QPT_UD &&
2734 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03002735 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
2736 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
2737 } else if (attr_mask & IB_QP_PATH_MTU) {
2738 if (attr->path_mtu < IB_MTU_256 ||
2739 attr->path_mtu > IB_MTU_4096) {
2740 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
2741 err = -EINVAL;
2742 goto out;
2743 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002744 context->mtu_msgmax = (attr->path_mtu << 5) |
2745 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03002746 }
2747
2748 if (attr_mask & IB_QP_DEST_QPN)
2749 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
2750
2751 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002752 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002753
2754 /* todo implement counter_index functionality */
2755
2756 if (is_sqp(ibqp->qp_type))
2757 context->pri_path.port = qp->port;
2758
2759 if (attr_mask & IB_QP_PORT)
2760 context->pri_path.port = attr->port_num;
2761
2762 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002763 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03002764 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002765 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03002766 if (err)
2767 goto out;
2768 }
2769
2770 if (attr_mask & IB_QP_TIMEOUT)
2771 context->pri_path.ackto_lt |= attr->timeout << 3;
2772
2773 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002774 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
2775 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002776 attr->alt_port_num,
2777 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
2778 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03002779 if (err)
2780 goto out;
2781 }
2782
2783 pd = get_pd(qp);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002784 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2785 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002786
2787 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
2788 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
2789 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
2790 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
2791
2792 if (attr_mask & IB_QP_RNR_RETRY)
2793 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
2794
2795 if (attr_mask & IB_QP_RETRY_CNT)
2796 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
2797
2798 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2799 if (attr->max_rd_atomic)
2800 context->params1 |=
2801 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
2802 }
2803
2804 if (attr_mask & IB_QP_SQ_PSN)
2805 context->next_send_psn = cpu_to_be32(attr->sq_psn);
2806
2807 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2808 if (attr->max_dest_rd_atomic)
2809 context->params2 |=
2810 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
2811 }
2812
2813 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
2814 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
2815
2816 if (attr_mask & IB_QP_MIN_RNR_TIMER)
2817 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
2818
2819 if (attr_mask & IB_QP_RQ_PSN)
2820 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
2821
2822 if (attr_mask & IB_QP_QKEY)
2823 context->qkey = cpu_to_be32(attr->qkey);
2824
2825 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2826 context->db_rec_addr = cpu_to_be64(qp->db.dma);
2827
Mark Bloch0837e862016-06-17 15:10:55 +03002828 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2829 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
2830 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002831
2832 /* Underlay port should be used - index 0 function per port */
2833 if (qp->flags & MLX5_IB_QP_UNDERLAY)
2834 port_num = 0;
2835
Alex Veskereb49ab02016-08-28 12:25:53 +03002836 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03002837 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03002838 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03002839 }
2840
Eli Cohene126ba92013-07-07 17:25:49 +03002841 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2842 context->sq_crq_size |= cpu_to_be16(1 << 4);
2843
Haggai Eranb11a4f92016-02-29 15:45:03 +02002844 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
2845 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03002846
2847 mlx5_cur = to_mlx5_state(cur_state);
2848 mlx5_new = to_mlx5_state(new_state);
2849 mlx5_st = to_mlx5_st(ibqp->qp_type);
Eli Cohen07c91132013-10-24 12:01:01 +03002850 if (mlx5_st < 0)
Eli Cohene126ba92013-07-07 17:25:49 +03002851 goto out;
2852
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002853 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
2854 !optab[mlx5_cur][mlx5_new])
2855 goto out;
2856
2857 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03002858 optpar = ib_mask_to_mlx5_opt(attr_mask);
2859 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002860
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002861 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2862 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03002863 struct mlx5_modify_raw_qp_param raw_qp_param = {};
2864
2865 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03002866 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03002867 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03002868 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
2869 }
Bodong Wang7d29f342016-12-01 13:43:16 +02002870
2871 if (attr_mask & IB_QP_RATE_LIMIT) {
2872 raw_qp_param.rate_limit = attr->rate_limit;
2873 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
2874 }
2875
Aviv Heller13eab212016-09-18 20:48:04 +03002876 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03002877 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002878 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002879 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03002880 }
2881
Eli Cohene126ba92013-07-07 17:25:49 +03002882 if (err)
2883 goto out;
2884
2885 qp->state = new_state;
2886
2887 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002888 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002889 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002890 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03002891 if (attr_mask & IB_QP_PORT)
2892 qp->port = attr->port_num;
2893 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002894 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03002895
2896 /*
2897 * If we moved a kernel QP to RESET, clean up all old CQ
2898 * entries and reinitialize the QP.
2899 */
2900 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002901 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002902 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
2903 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002904 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002905
2906 qp->rq.head = 0;
2907 qp->rq.tail = 0;
2908 qp->sq.head = 0;
2909 qp->sq.tail = 0;
2910 qp->sq.cur_post = 0;
2911 qp->sq.last_poll = 0;
2912 qp->db.db[MLX5_RCV_DBR] = 0;
2913 qp->db.db[MLX5_SND_DBR] = 0;
2914 }
2915
2916out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002917 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03002918 return err;
2919}
2920
2921int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2922 int attr_mask, struct ib_udata *udata)
2923{
2924 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2925 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Haggai Erand16e91d2016-02-29 15:45:05 +02002926 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03002927 enum ib_qp_state cur_state, new_state;
2928 int err = -EINVAL;
2929 int port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002930 enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
Eli Cohene126ba92013-07-07 17:25:49 +03002931
Yishai Hadas28d61372016-05-23 15:20:56 +03002932 if (ibqp->rwq_ind_tbl)
2933 return -ENOSYS;
2934
Haggai Erand16e91d2016-02-29 15:45:05 +02002935 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
2936 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
2937
2938 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
2939 IB_QPT_GSI : ibqp->qp_type;
2940
Eli Cohene126ba92013-07-07 17:25:49 +03002941 mutex_lock(&qp->mutex);
2942
2943 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
2944 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
2945
Achiad Shochat2811ba52015-12-23 18:47:24 +02002946 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
2947 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
2948 ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
2949 }
2950
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002951 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
2952 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
2953 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
2954 attr_mask);
2955 goto out;
2956 }
2957 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Haggai Erand16e91d2016-02-29 15:45:05 +02002958 !ib_modify_qp_is_ok(cur_state, new_state, qp_type, attr_mask, ll)) {
Haggai Eran158abf82016-02-29 15:45:04 +02002959 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
2960 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03002961 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002962 }
Eli Cohene126ba92013-07-07 17:25:49 +03002963
2964 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002965 (attr->port_num == 0 ||
Haggai Eran158abf82016-02-29 15:45:04 +02002966 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports))) {
2967 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
2968 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03002969 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002970 }
Eli Cohene126ba92013-07-07 17:25:49 +03002971
2972 if (attr_mask & IB_QP_PKEY_INDEX) {
2973 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03002974 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02002975 dev->mdev->port_caps[port - 1].pkey_table_len) {
2976 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
2977 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002978 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002979 }
Eli Cohene126ba92013-07-07 17:25:49 +03002980 }
2981
2982 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002983 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002984 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
2985 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
2986 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002987 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002988 }
Eli Cohene126ba92013-07-07 17:25:49 +03002989
2990 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002991 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002992 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
2993 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
2994 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002995 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002996 }
Eli Cohene126ba92013-07-07 17:25:49 +03002997
2998 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
2999 err = 0;
3000 goto out;
3001 }
3002
3003 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
3004
3005out:
3006 mutex_unlock(&qp->mutex);
3007 return err;
3008}
3009
3010static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
3011{
3012 struct mlx5_ib_cq *cq;
3013 unsigned cur;
3014
3015 cur = wq->head - wq->tail;
3016 if (likely(cur + nreq < wq->max_post))
3017 return 0;
3018
3019 cq = to_mcq(ib_cq);
3020 spin_lock(&cq->lock);
3021 cur = wq->head - wq->tail;
3022 spin_unlock(&cq->lock);
3023
3024 return cur + nreq >= wq->max_post;
3025}
3026
3027static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
3028 u64 remote_addr, u32 rkey)
3029{
3030 rseg->raddr = cpu_to_be64(remote_addr);
3031 rseg->rkey = cpu_to_be32(rkey);
3032 rseg->reserved = 0;
3033}
3034
Erez Shitritf0313962016-02-21 16:27:17 +02003035static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
3036 struct ib_send_wr *wr, void *qend,
3037 struct mlx5_ib_qp *qp, int *size)
3038{
3039 void *seg = eseg;
3040
3041 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
3042
3043 if (wr->send_flags & IB_SEND_IP_CSUM)
3044 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
3045 MLX5_ETH_WQE_L4_CSUM;
3046
3047 seg += sizeof(struct mlx5_wqe_eth_seg);
3048 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3049
3050 if (wr->opcode == IB_WR_LSO) {
3051 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003052 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr.start);
Erez Shitritf0313962016-02-21 16:27:17 +02003053 u64 left, leftlen, copysz;
3054 void *pdata = ud_wr->header;
3055
3056 left = ud_wr->hlen;
3057 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003058 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02003059
3060 /*
3061 * check if there is space till the end of queue, if yes,
3062 * copy all in one shot, otherwise copy till the end of queue,
3063 * rollback and than the copy the left
3064 */
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003065 leftlen = qend - (void *)eseg->inline_hdr.start;
Erez Shitritf0313962016-02-21 16:27:17 +02003066 copysz = min_t(u64, leftlen, left);
3067
3068 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3069
3070 if (likely(copysz > size_of_inl_hdr_start)) {
3071 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3072 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3073 }
3074
3075 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3076 seg = mlx5_get_send_wqe(qp, 0);
3077 left -= copysz;
3078 pdata += copysz;
3079 memcpy(seg, pdata, left);
3080 seg += ALIGN(left, 16);
3081 *size += ALIGN(left, 16) / 16;
3082 }
3083 }
3084
3085 return seg;
3086}
3087
Eli Cohene126ba92013-07-07 17:25:49 +03003088static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
3089 struct ib_send_wr *wr)
3090{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003091 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3092 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3093 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003094}
3095
3096static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3097{
3098 dseg->byte_count = cpu_to_be32(sg->length);
3099 dseg->lkey = cpu_to_be32(sg->lkey);
3100 dseg->addr = cpu_to_be64(sg->addr);
3101}
3102
Artemy Kovalyov31616252017-01-02 11:37:42 +02003103static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03003104{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003105 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
3106 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03003107}
3108
3109static __be64 frwr_mkey_mask(void)
3110{
3111 u64 result;
3112
3113 result = MLX5_MKEY_MASK_LEN |
3114 MLX5_MKEY_MASK_PAGE_SIZE |
3115 MLX5_MKEY_MASK_START_ADDR |
3116 MLX5_MKEY_MASK_EN_RINVAL |
3117 MLX5_MKEY_MASK_KEY |
3118 MLX5_MKEY_MASK_LR |
3119 MLX5_MKEY_MASK_LW |
3120 MLX5_MKEY_MASK_RR |
3121 MLX5_MKEY_MASK_RW |
3122 MLX5_MKEY_MASK_A |
3123 MLX5_MKEY_MASK_SMALL_FENCE |
3124 MLX5_MKEY_MASK_FREE;
3125
3126 return cpu_to_be64(result);
3127}
3128
Sagi Grimberge6631812014-02-23 14:19:11 +02003129static __be64 sig_mkey_mask(void)
3130{
3131 u64 result;
3132
3133 result = MLX5_MKEY_MASK_LEN |
3134 MLX5_MKEY_MASK_PAGE_SIZE |
3135 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003136 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003137 MLX5_MKEY_MASK_EN_RINVAL |
3138 MLX5_MKEY_MASK_KEY |
3139 MLX5_MKEY_MASK_LR |
3140 MLX5_MKEY_MASK_LW |
3141 MLX5_MKEY_MASK_RR |
3142 MLX5_MKEY_MASK_RW |
3143 MLX5_MKEY_MASK_SMALL_FENCE |
3144 MLX5_MKEY_MASK_FREE |
3145 MLX5_MKEY_MASK_BSF_EN;
3146
3147 return cpu_to_be64(result);
3148}
3149
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003150static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003151 struct mlx5_ib_mr *mr)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003152{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003153 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003154
3155 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003156
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003157 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003158 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003159 umr->mkey_mask = frwr_mkey_mask();
3160}
3161
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003162static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003163{
3164 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003165 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003166 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003167}
3168
Artemy Kovalyov31616252017-01-02 11:37:42 +02003169static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003170{
3171 u64 result;
3172
Artemy Kovalyov31616252017-01-02 11:37:42 +02003173 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02003174 MLX5_MKEY_MASK_FREE;
3175
3176 return cpu_to_be64(result);
3177}
3178
Artemy Kovalyov31616252017-01-02 11:37:42 +02003179static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003180{
3181 u64 result;
3182
3183 result = MLX5_MKEY_MASK_FREE;
3184
3185 return cpu_to_be64(result);
3186}
3187
Noa Osherovich56e11d62016-02-29 16:46:51 +02003188static __be64 get_umr_update_translation_mask(void)
3189{
3190 u64 result;
3191
3192 result = MLX5_MKEY_MASK_LEN |
3193 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003194 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003195
3196 return cpu_to_be64(result);
3197}
3198
Artemy Kovalyov31616252017-01-02 11:37:42 +02003199static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02003200{
3201 u64 result;
3202
Artemy Kovalyov31616252017-01-02 11:37:42 +02003203 result = MLX5_MKEY_MASK_LR |
3204 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003205 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003206 MLX5_MKEY_MASK_RW;
3207
3208 if (atomic)
3209 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003210
3211 return cpu_to_be64(result);
3212}
3213
3214static __be64 get_umr_update_pd_mask(void)
3215{
3216 u64 result;
3217
Artemy Kovalyov31616252017-01-02 11:37:42 +02003218 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003219
3220 return cpu_to_be64(result);
3221}
3222
Eli Cohene126ba92013-07-07 17:25:49 +03003223static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Maor Gottlieb578e7262016-10-27 16:36:37 +03003224 struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003225{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003226 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003227
3228 memset(umr, 0, sizeof(*umr));
3229
Haggai Eran968e78d2014-12-11 17:04:11 +02003230 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3231 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3232 else
3233 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3234
Artemy Kovalyov31616252017-01-02 11:37:42 +02003235 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
3236 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
3237 u64 offset = get_xlt_octo(umrwr->offset);
3238
3239 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
3240 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
3241 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003242 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02003243 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3244 umr->mkey_mask |= get_umr_update_translation_mask();
3245 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
3246 umr->mkey_mask |= get_umr_update_access_mask(atomic);
3247 umr->mkey_mask |= get_umr_update_pd_mask();
3248 }
3249 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
3250 umr->mkey_mask |= get_umr_enable_mr_mask();
3251 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
3252 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003253
3254 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003255 umr->flags |= MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003256}
3257
3258static u8 get_umr_flags(int acc)
3259{
3260 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3261 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3262 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3263 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003264 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003265}
3266
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003267static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3268 struct mlx5_ib_mr *mr,
3269 u32 key, int access)
3270{
3271 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3272
3273 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003274
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003275 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003276 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003277 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003278 /* KLMs take twice the size of MTTs */
3279 ndescs *= 2;
3280
3281 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003282 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3283 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3284 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3285 seg->len = cpu_to_be64(mr->ibmr.length);
3286 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003287}
3288
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003289static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003290{
3291 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003292 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003293}
3294
3295static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
3296{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003297 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003298
Eli Cohene126ba92013-07-07 17:25:49 +03003299 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02003300 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02003301 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003302
Haggai Eran968e78d2014-12-11 17:04:11 +02003303 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003304 if (umrwr->pd)
3305 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
3306 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
3307 !umrwr->length)
3308 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
3309
3310 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003311 seg->len = cpu_to_be64(umrwr->length);
3312 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003313 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003314 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003315}
3316
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003317static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3318 struct mlx5_ib_mr *mr,
3319 struct mlx5_ib_pd *pd)
3320{
3321 int bcount = mr->desc_size * mr->ndescs;
3322
3323 dseg->addr = cpu_to_be64(mr->desc_map);
3324 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3325 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3326}
3327
Eli Cohene126ba92013-07-07 17:25:49 +03003328static __be32 send_ieth(struct ib_send_wr *wr)
3329{
3330 switch (wr->opcode) {
3331 case IB_WR_SEND_WITH_IMM:
3332 case IB_WR_RDMA_WRITE_WITH_IMM:
3333 return wr->ex.imm_data;
3334
3335 case IB_WR_SEND_WITH_INV:
3336 return cpu_to_be32(wr->ex.invalidate_rkey);
3337
3338 default:
3339 return 0;
3340 }
3341}
3342
3343static u8 calc_sig(void *wqe, int size)
3344{
3345 u8 *p = wqe;
3346 u8 res = 0;
3347 int i;
3348
3349 for (i = 0; i < size; i++)
3350 res ^= p[i];
3351
3352 return ~res;
3353}
3354
3355static u8 wq_sig(void *wqe)
3356{
3357 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
3358}
3359
3360static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
3361 void *wqe, int *sz)
3362{
3363 struct mlx5_wqe_inline_seg *seg;
3364 void *qend = qp->sq.qend;
3365 void *addr;
3366 int inl = 0;
3367 int copy;
3368 int len;
3369 int i;
3370
3371 seg = wqe;
3372 wqe += sizeof(*seg);
3373 for (i = 0; i < wr->num_sge; i++) {
3374 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
3375 len = wr->sg_list[i].length;
3376 inl += len;
3377
3378 if (unlikely(inl > qp->max_inline_data))
3379 return -ENOMEM;
3380
3381 if (unlikely(wqe + len > qend)) {
3382 copy = qend - wqe;
3383 memcpy(wqe, addr, copy);
3384 addr += copy;
3385 len -= copy;
3386 wqe = mlx5_get_send_wqe(qp, 0);
3387 }
3388 memcpy(wqe, addr, len);
3389 wqe += len;
3390 }
3391
3392 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
3393
3394 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
3395
3396 return 0;
3397}
3398
Sagi Grimberge6631812014-02-23 14:19:11 +02003399static u16 prot_field_size(enum ib_signature_type type)
3400{
3401 switch (type) {
3402 case IB_SIG_TYPE_T10_DIF:
3403 return MLX5_DIF_SIZE;
3404 default:
3405 return 0;
3406 }
3407}
3408
3409static u8 bs_selector(int block_size)
3410{
3411 switch (block_size) {
3412 case 512: return 0x1;
3413 case 520: return 0x2;
3414 case 4096: return 0x3;
3415 case 4160: return 0x4;
3416 case 1073741824: return 0x5;
3417 default: return 0;
3418 }
3419}
3420
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003421static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
3422 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02003423{
Sagi Grimberg142537f2014-08-13 19:54:32 +03003424 /* Valid inline section and allow BSF refresh */
3425 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
3426 MLX5_BSF_REFRESH_DIF);
3427 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
3428 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003429 /* repeating block */
3430 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
3431 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
3432 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003433
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003434 if (domain->sig.dif.ref_remap)
3435 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02003436
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003437 if (domain->sig.dif.app_escape) {
3438 if (domain->sig.dif.ref_escape)
3439 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
3440 else
3441 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02003442 }
3443
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003444 inl->dif_app_bitmask_check =
3445 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02003446}
3447
3448static int mlx5_set_bsf(struct ib_mr *sig_mr,
3449 struct ib_sig_attrs *sig_attrs,
3450 struct mlx5_bsf *bsf, u32 data_size)
3451{
3452 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
3453 struct mlx5_bsf_basic *basic = &bsf->basic;
3454 struct ib_sig_domain *mem = &sig_attrs->mem;
3455 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02003456
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003457 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02003458
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003459 /* Basic + Extended + Inline */
3460 basic->bsf_size_sbs = 1 << 7;
3461 /* Input domain check byte mask */
3462 basic->check_byte_mask = sig_attrs->check_mask;
3463 basic->raw_data_size = cpu_to_be32(data_size);
3464
3465 /* Memory domain */
3466 switch (sig_attrs->mem.sig_type) {
3467 case IB_SIG_TYPE_NONE:
3468 break;
3469 case IB_SIG_TYPE_T10_DIF:
3470 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
3471 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
3472 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
3473 break;
3474 default:
3475 return -EINVAL;
3476 }
3477
3478 /* Wire domain */
3479 switch (sig_attrs->wire.sig_type) {
3480 case IB_SIG_TYPE_NONE:
3481 break;
3482 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02003483 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003484 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003485 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03003486 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02003487 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003488 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003489 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003490 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003491 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003492 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02003493 } else
3494 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
3495
Sagi Grimberg142537f2014-08-13 19:54:32 +03003496 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003497 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02003498 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003499 default:
3500 return -EINVAL;
3501 }
3502
3503 return 0;
3504}
3505
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003506static int set_sig_data_segment(struct ib_sig_handover_wr *wr,
3507 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003508{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003509 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
3510 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003511 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003512 u32 data_len = wr->wr.sg_list->length;
3513 u32 data_key = wr->wr.sg_list->lkey;
3514 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003515 int ret;
3516 int wqe_size;
3517
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003518 if (!wr->prot ||
3519 (data_key == wr->prot->lkey &&
3520 data_va == wr->prot->addr &&
3521 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003522 /**
3523 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003524 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02003525 * So need construct:
3526 * ------------------
3527 * | data_klm |
3528 * ------------------
3529 * | BSF |
3530 * ------------------
3531 **/
3532 struct mlx5_klm *data_klm = *seg;
3533
3534 data_klm->bcount = cpu_to_be32(data_len);
3535 data_klm->key = cpu_to_be32(data_key);
3536 data_klm->va = cpu_to_be64(data_va);
3537 wqe_size = ALIGN(sizeof(*data_klm), 64);
3538 } else {
3539 /**
3540 * Source domain contains signature information
3541 * So need construct a strided block format:
3542 * ---------------------------
3543 * | stride_block_ctrl |
3544 * ---------------------------
3545 * | data_klm |
3546 * ---------------------------
3547 * | prot_klm |
3548 * ---------------------------
3549 * | BSF |
3550 * ---------------------------
3551 **/
3552 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
3553 struct mlx5_stride_block_entry *data_sentry;
3554 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003555 u32 prot_key = wr->prot->lkey;
3556 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003557 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
3558 int prot_size;
3559
3560 sblock_ctrl = *seg;
3561 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
3562 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
3563
3564 prot_size = prot_field_size(sig_attrs->mem.sig_type);
3565 if (!prot_size) {
3566 pr_err("Bad block size given: %u\n", block_size);
3567 return -EINVAL;
3568 }
3569 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
3570 prot_size);
3571 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
3572 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
3573 sblock_ctrl->num_entries = cpu_to_be16(2);
3574
3575 data_sentry->bcount = cpu_to_be16(block_size);
3576 data_sentry->key = cpu_to_be32(data_key);
3577 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003578 data_sentry->stride = cpu_to_be16(block_size);
3579
Sagi Grimberge6631812014-02-23 14:19:11 +02003580 prot_sentry->bcount = cpu_to_be16(prot_size);
3581 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003582 prot_sentry->va = cpu_to_be64(prot_va);
3583 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003584
Sagi Grimberge6631812014-02-23 14:19:11 +02003585 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
3586 sizeof(*prot_sentry), 64);
3587 }
3588
3589 *seg += wqe_size;
3590 *size += wqe_size / 16;
3591 if (unlikely((*seg == qp->sq.qend)))
3592 *seg = mlx5_get_send_wqe(qp, 0);
3593
3594 bsf = *seg;
3595 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
3596 if (ret)
3597 return -EINVAL;
3598
3599 *seg += sizeof(*bsf);
3600 *size += sizeof(*bsf) / 16;
3601 if (unlikely((*seg == qp->sq.qend)))
3602 *seg = mlx5_get_send_wqe(qp, 0);
3603
3604 return 0;
3605}
3606
3607static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003608 struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02003609 u32 length, u32 pdn)
3610{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003611 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003612 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003613 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02003614
3615 memset(seg, 0, sizeof(*seg));
3616
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003617 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003618 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003619 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003620 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02003621 MLX5_MKEY_BSF_EN | pdn);
3622 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003623 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003624 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
3625}
3626
3627static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003628 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003629{
3630 memset(umr, 0, sizeof(*umr));
3631
3632 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003633 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003634 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
3635 umr->mkey_mask = sig_mkey_mask();
3636}
3637
3638
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003639static int set_sig_umr_wr(struct ib_send_wr *send_wr, struct mlx5_ib_qp *qp,
Sagi Grimberge6631812014-02-23 14:19:11 +02003640 void **seg, int *size)
3641{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003642 struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
3643 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003644 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003645 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02003646 int region_len, ret;
3647
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003648 if (unlikely(wr->wr.num_sge != 1) ||
3649 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003650 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
3651 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02003652 return -EINVAL;
3653
3654 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003655 region_len = wr->wr.sg_list->length;
3656 if (wr->prot &&
3657 (wr->prot->lkey != wr->wr.sg_list->lkey ||
3658 wr->prot->addr != wr->wr.sg_list->addr ||
3659 wr->prot->length != wr->wr.sg_list->length))
3660 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02003661
3662 /**
3663 * KLM octoword size - if protection was provided
3664 * then we use strided block format (3 octowords),
3665 * else we use single KLM (1 octoword)
3666 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02003667 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02003668
Artemy Kovalyov31616252017-01-02 11:37:42 +02003669 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003670 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3671 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3672 if (unlikely((*seg == qp->sq.qend)))
3673 *seg = mlx5_get_send_wqe(qp, 0);
3674
Artemy Kovalyov31616252017-01-02 11:37:42 +02003675 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02003676 *seg += sizeof(struct mlx5_mkey_seg);
3677 *size += sizeof(struct mlx5_mkey_seg) / 16;
3678 if (unlikely((*seg == qp->sq.qend)))
3679 *seg = mlx5_get_send_wqe(qp, 0);
3680
3681 ret = set_sig_data_segment(wr, qp, seg, size);
3682 if (ret)
3683 return ret;
3684
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003685 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02003686 return 0;
3687}
3688
3689static int set_psv_wr(struct ib_sig_domain *domain,
3690 u32 psv_idx, void **seg, int *size)
3691{
3692 struct mlx5_seg_set_psv *psv_seg = *seg;
3693
3694 memset(psv_seg, 0, sizeof(*psv_seg));
3695 psv_seg->psv_num = cpu_to_be32(psv_idx);
3696 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003697 case IB_SIG_TYPE_NONE:
3698 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003699 case IB_SIG_TYPE_T10_DIF:
3700 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
3701 domain->sig.dif.app_tag);
3702 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02003703 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003704 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02003705 pr_err("Bad signature type (%d) is given.\n",
3706 domain->sig_type);
3707 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02003708 }
3709
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003710 *seg += sizeof(*psv_seg);
3711 *size += sizeof(*psv_seg) / 16;
3712
Sagi Grimberge6631812014-02-23 14:19:11 +02003713 return 0;
3714}
3715
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003716static int set_reg_wr(struct mlx5_ib_qp *qp,
3717 struct ib_reg_wr *wr,
3718 void **seg, int *size)
3719{
3720 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
3721 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
3722
3723 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
3724 mlx5_ib_warn(to_mdev(qp->ibqp.device),
3725 "Invalid IB_SEND_INLINE send flag\n");
3726 return -EINVAL;
3727 }
3728
3729 set_reg_umr_seg(*seg, mr);
3730 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3731 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3732 if (unlikely((*seg == qp->sq.qend)))
3733 *seg = mlx5_get_send_wqe(qp, 0);
3734
3735 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
3736 *seg += sizeof(struct mlx5_mkey_seg);
3737 *size += sizeof(struct mlx5_mkey_seg) / 16;
3738 if (unlikely((*seg == qp->sq.qend)))
3739 *seg = mlx5_get_send_wqe(qp, 0);
3740
3741 set_reg_data_seg(*seg, mr, pd);
3742 *seg += sizeof(struct mlx5_wqe_data_seg);
3743 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
3744
3745 return 0;
3746}
3747
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003748static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
Eli Cohene126ba92013-07-07 17:25:49 +03003749{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003750 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003751 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3752 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3753 if (unlikely((*seg == qp->sq.qend)))
3754 *seg = mlx5_get_send_wqe(qp, 0);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003755 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003756 *seg += sizeof(struct mlx5_mkey_seg);
3757 *size += sizeof(struct mlx5_mkey_seg) / 16;
3758 if (unlikely((*seg == qp->sq.qend)))
3759 *seg = mlx5_get_send_wqe(qp, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003760}
3761
3762static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
3763{
3764 __be32 *p = NULL;
3765 int tidx = idx;
3766 int i, j;
3767
3768 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
3769 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
3770 if ((i & 0xf) == 0) {
3771 void *buf = mlx5_get_send_wqe(qp, tidx);
3772 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
3773 p = buf;
3774 j = 0;
3775 }
3776 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
3777 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
3778 be32_to_cpu(p[j + 3]));
3779 }
3780}
3781
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003782static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
3783 struct mlx5_wqe_ctrl_seg **ctrl,
Eli Cohen6a4f1392014-12-02 12:26:18 +02003784 struct ib_send_wr *wr, unsigned *idx,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003785 int *size, int nreq)
3786{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003787 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
3788 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003789
3790 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
3791 *seg = mlx5_get_send_wqe(qp, *idx);
3792 *ctrl = *seg;
3793 *(uint32_t *)(*seg + 8) = 0;
3794 (*ctrl)->imm = send_ieth(wr);
3795 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
3796 (wr->send_flags & IB_SEND_SIGNALED ?
3797 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
3798 (wr->send_flags & IB_SEND_SOLICITED ?
3799 MLX5_WQE_CTRL_SOLICITED : 0);
3800
3801 *seg += sizeof(**ctrl);
3802 *size = sizeof(**ctrl) / 16;
3803
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003804 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003805}
3806
3807static void finish_wqe(struct mlx5_ib_qp *qp,
3808 struct mlx5_wqe_ctrl_seg *ctrl,
3809 u8 size, unsigned idx, u64 wr_id,
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003810 int nreq, u8 fence, u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003811{
3812 u8 opmod = 0;
3813
3814 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
3815 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02003816 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003817 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003818 if (unlikely(qp->wq_sig))
3819 ctrl->signature = wq_sig(ctrl);
3820
3821 qp->sq.wrid[idx] = wr_id;
3822 qp->sq.w_list[idx].opcode = mlx5_opcode;
3823 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
3824 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
3825 qp->sq.w_list[idx].next = qp->sq.cur_post;
3826}
3827
3828
Eli Cohene126ba92013-07-07 17:25:49 +03003829int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
3830 struct ib_send_wr **bad_wr)
3831{
3832 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
3833 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003834 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02003835 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02003836 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003837 struct mlx5_wqe_data_seg *dpseg;
3838 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02003839 struct mlx5_bf *bf;
Eli Cohene126ba92013-07-07 17:25:49 +03003840 int uninitialized_var(size);
Haggai Erand16e91d2016-02-29 15:45:05 +02003841 void *qend;
Eli Cohene126ba92013-07-07 17:25:49 +03003842 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003843 unsigned idx;
3844 int err = 0;
3845 int inl = 0;
3846 int num_sge;
3847 void *seg;
3848 int nreq;
3849 int i;
3850 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003851 u8 fence;
3852
Haggai Erand16e91d2016-02-29 15:45:05 +02003853 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3854 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
3855
3856 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003857 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02003858 qend = qp->sq.qend;
3859
Eli Cohene126ba92013-07-07 17:25:49 +03003860 spin_lock_irqsave(&qp->sq.lock, flags);
3861
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003862 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
3863 err = -EIO;
3864 *bad_wr = wr;
3865 nreq = 0;
3866 goto out;
3867 }
3868
Eli Cohene126ba92013-07-07 17:25:49 +03003869 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04003870 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03003871 mlx5_ib_warn(dev, "\n");
3872 err = -EINVAL;
3873 *bad_wr = wr;
3874 goto out;
3875 }
3876
Eli Cohene126ba92013-07-07 17:25:49 +03003877 num_sge = wr->num_sge;
3878 if (unlikely(num_sge > qp->sq.max_gs)) {
3879 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03003880 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03003881 *bad_wr = wr;
3882 goto out;
3883 }
3884
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003885 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
3886 if (err) {
3887 mlx5_ib_warn(dev, "\n");
3888 err = -ENOMEM;
3889 *bad_wr = wr;
3890 goto out;
3891 }
Eli Cohene126ba92013-07-07 17:25:49 +03003892
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003893 if (wr->opcode == IB_WR_LOCAL_INV ||
3894 wr->opcode == IB_WR_REG_MR) {
3895 fence = dev->umr_fence;
3896 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
3897 } else if (wr->send_flags & IB_SEND_FENCE) {
3898 if (qp->next_fence)
3899 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
3900 else
3901 fence = MLX5_FENCE_MODE_FENCE;
3902 } else {
3903 fence = qp->next_fence;
3904 }
3905
Eli Cohene126ba92013-07-07 17:25:49 +03003906 switch (ibqp->qp_type) {
3907 case IB_QPT_XRC_INI:
3908 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03003909 seg += sizeof(*xrc);
3910 size += sizeof(*xrc) / 16;
3911 /* fall through */
3912 case IB_QPT_RC:
3913 switch (wr->opcode) {
3914 case IB_WR_RDMA_READ:
3915 case IB_WR_RDMA_WRITE:
3916 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003917 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
3918 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03003919 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003920 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
3921 break;
3922
3923 case IB_WR_ATOMIC_CMP_AND_SWP:
3924 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03003925 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03003926 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
3927 err = -ENOSYS;
3928 *bad_wr = wr;
3929 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003930
3931 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03003932 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
3933 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003934 set_linv_wr(qp, &seg, &size);
Eli Cohene126ba92013-07-07 17:25:49 +03003935 num_sge = 0;
3936 break;
3937
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003938 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003939 qp->sq.wr_data[idx] = IB_WR_REG_MR;
3940 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
3941 err = set_reg_wr(qp, reg_wr(wr), &seg, &size);
3942 if (err) {
3943 *bad_wr = wr;
3944 goto out;
3945 }
3946 num_sge = 0;
3947 break;
3948
Sagi Grimberge6631812014-02-23 14:19:11 +02003949 case IB_WR_REG_SIG_MR:
3950 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003951 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003952
3953 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
3954 err = set_sig_umr_wr(wr, qp, &seg, &size);
3955 if (err) {
3956 mlx5_ib_warn(dev, "\n");
3957 *bad_wr = wr;
3958 goto out;
3959 }
3960
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003961 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
3962 fence, MLX5_OPCODE_UMR);
Sagi Grimberge6631812014-02-23 14:19:11 +02003963 /*
3964 * SET_PSV WQEs are not signaled and solicited
3965 * on error
3966 */
3967 wr->send_flags &= ~IB_SEND_SIGNALED;
3968 wr->send_flags |= IB_SEND_SOLICITED;
3969 err = begin_wqe(qp, &seg, &ctrl, wr,
3970 &idx, &size, nreq);
3971 if (err) {
3972 mlx5_ib_warn(dev, "\n");
3973 err = -ENOMEM;
3974 *bad_wr = wr;
3975 goto out;
3976 }
3977
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003978 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02003979 mr->sig->psv_memory.psv_idx, &seg,
3980 &size);
3981 if (err) {
3982 mlx5_ib_warn(dev, "\n");
3983 *bad_wr = wr;
3984 goto out;
3985 }
3986
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003987 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
3988 fence, MLX5_OPCODE_SET_PSV);
Sagi Grimberge6631812014-02-23 14:19:11 +02003989 err = begin_wqe(qp, &seg, &ctrl, wr,
3990 &idx, &size, nreq);
3991 if (err) {
3992 mlx5_ib_warn(dev, "\n");
3993 err = -ENOMEM;
3994 *bad_wr = wr;
3995 goto out;
3996 }
3997
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003998 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02003999 mr->sig->psv_wire.psv_idx, &seg,
4000 &size);
4001 if (err) {
4002 mlx5_ib_warn(dev, "\n");
4003 *bad_wr = wr;
4004 goto out;
4005 }
4006
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004007 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4008 fence, MLX5_OPCODE_SET_PSV);
4009 qp->next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004010 num_sge = 0;
4011 goto skip_psv;
4012
Eli Cohene126ba92013-07-07 17:25:49 +03004013 default:
4014 break;
4015 }
4016 break;
4017
4018 case IB_QPT_UC:
4019 switch (wr->opcode) {
4020 case IB_WR_RDMA_WRITE:
4021 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004022 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4023 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004024 seg += sizeof(struct mlx5_wqe_raddr_seg);
4025 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4026 break;
4027
4028 default:
4029 break;
4030 }
4031 break;
4032
Eli Cohene126ba92013-07-07 17:25:49 +03004033 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02004034 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
4035 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
4036 err = -EPERM;
4037 *bad_wr = wr;
4038 goto out;
4039 }
Haggai Erand16e91d2016-02-29 15:45:05 +02004040 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004041 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004042 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004043 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4044 if (unlikely((seg == qend)))
4045 seg = mlx5_get_send_wqe(qp, 0);
4046 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004047 case IB_QPT_UD:
4048 set_datagram_seg(seg, wr);
4049 seg += sizeof(struct mlx5_wqe_datagram_seg);
4050 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004051
Erez Shitritf0313962016-02-21 16:27:17 +02004052 if (unlikely((seg == qend)))
4053 seg = mlx5_get_send_wqe(qp, 0);
4054
4055 /* handle qp that supports ud offload */
4056 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4057 struct mlx5_wqe_eth_pad *pad;
4058
4059 pad = seg;
4060 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4061 seg += sizeof(struct mlx5_wqe_eth_pad);
4062 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4063
4064 seg = set_eth_seg(seg, wr, qend, qp, &size);
4065
4066 if (unlikely((seg == qend)))
4067 seg = mlx5_get_send_wqe(qp, 0);
4068 }
4069 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004070 case MLX5_IB_QPT_REG_UMR:
4071 if (wr->opcode != MLX5_IB_WR_UMR) {
4072 err = -EINVAL;
4073 mlx5_ib_warn(dev, "bad opcode\n");
4074 goto out;
4075 }
4076 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004077 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Maor Gottlieb578e7262016-10-27 16:36:37 +03004078 set_reg_umr_segment(seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
Eli Cohene126ba92013-07-07 17:25:49 +03004079 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4080 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4081 if (unlikely((seg == qend)))
4082 seg = mlx5_get_send_wqe(qp, 0);
4083 set_reg_mkey_segment(seg, wr);
4084 seg += sizeof(struct mlx5_mkey_seg);
4085 size += sizeof(struct mlx5_mkey_seg) / 16;
4086 if (unlikely((seg == qend)))
4087 seg = mlx5_get_send_wqe(qp, 0);
4088 break;
4089
4090 default:
4091 break;
4092 }
4093
4094 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4095 int uninitialized_var(sz);
4096
4097 err = set_data_inl_seg(qp, wr, seg, &sz);
4098 if (unlikely(err)) {
4099 mlx5_ib_warn(dev, "\n");
4100 *bad_wr = wr;
4101 goto out;
4102 }
4103 inl = 1;
4104 size += sz;
4105 } else {
4106 dpseg = seg;
4107 for (i = 0; i < num_sge; i++) {
4108 if (unlikely(dpseg == qend)) {
4109 seg = mlx5_get_send_wqe(qp, 0);
4110 dpseg = seg;
4111 }
4112 if (likely(wr->sg_list[i].length)) {
4113 set_data_ptr_seg(dpseg, wr->sg_list + i);
4114 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4115 dpseg++;
4116 }
4117 }
4118 }
4119
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004120 qp->next_fence = next_fence;
4121 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq, fence,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004122 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004123skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004124 if (0)
4125 dump_wqe(qp, idx, size);
4126 }
4127
4128out:
4129 if (likely(nreq)) {
4130 qp->sq.head += nreq;
4131
4132 /* Make sure that descriptors are written before
4133 * updating doorbell record and ringing the doorbell
4134 */
4135 wmb();
4136
4137 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4138
Eli Cohenada388f2014-01-14 17:45:16 +02004139 /* Make sure doorbell record is visible to the HCA before
4140 * we hit doorbell */
4141 wmb();
4142
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004143 /* currently we support only regular doorbells */
4144 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset, NULL);
4145 /* Make sure doorbells don't leak out of SQ spinlock
4146 * and reach the HCA out of order.
4147 */
4148 mmiowb();
Eli Cohene126ba92013-07-07 17:25:49 +03004149 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03004150 }
4151
4152 spin_unlock_irqrestore(&qp->sq.lock, flags);
4153
4154 return err;
4155}
4156
4157static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4158{
4159 sig->signature = calc_sig(sig, size);
4160}
4161
4162int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4163 struct ib_recv_wr **bad_wr)
4164{
4165 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4166 struct mlx5_wqe_data_seg *scat;
4167 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004168 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4169 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004170 unsigned long flags;
4171 int err = 0;
4172 int nreq;
4173 int ind;
4174 int i;
4175
Haggai Erand16e91d2016-02-29 15:45:05 +02004176 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4177 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4178
Eli Cohene126ba92013-07-07 17:25:49 +03004179 spin_lock_irqsave(&qp->rq.lock, flags);
4180
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004181 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4182 err = -EIO;
4183 *bad_wr = wr;
4184 nreq = 0;
4185 goto out;
4186 }
4187
Eli Cohene126ba92013-07-07 17:25:49 +03004188 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4189
4190 for (nreq = 0; wr; nreq++, wr = wr->next) {
4191 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4192 err = -ENOMEM;
4193 *bad_wr = wr;
4194 goto out;
4195 }
4196
4197 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4198 err = -EINVAL;
4199 *bad_wr = wr;
4200 goto out;
4201 }
4202
4203 scat = get_recv_wqe(qp, ind);
4204 if (qp->wq_sig)
4205 scat++;
4206
4207 for (i = 0; i < wr->num_sge; i++)
4208 set_data_ptr_seg(scat + i, wr->sg_list + i);
4209
4210 if (i < qp->rq.max_gs) {
4211 scat[i].byte_count = 0;
4212 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4213 scat[i].addr = 0;
4214 }
4215
4216 if (qp->wq_sig) {
4217 sig = (struct mlx5_rwqe_sig *)scat;
4218 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4219 }
4220
4221 qp->rq.wrid[ind] = wr->wr_id;
4222
4223 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4224 }
4225
4226out:
4227 if (likely(nreq)) {
4228 qp->rq.head += nreq;
4229
4230 /* Make sure that descriptors are written before
4231 * doorbell record.
4232 */
4233 wmb();
4234
4235 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4236 }
4237
4238 spin_unlock_irqrestore(&qp->rq.lock, flags);
4239
4240 return err;
4241}
4242
4243static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4244{
4245 switch (mlx5_state) {
4246 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4247 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4248 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4249 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4250 case MLX5_QP_STATE_SQ_DRAINING:
4251 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4252 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4253 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4254 default: return -1;
4255 }
4256}
4257
4258static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4259{
4260 switch (mlx5_mig_state) {
4261 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4262 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4263 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4264 default: return -1;
4265 }
4266}
4267
4268static int to_ib_qp_access_flags(int mlx5_flags)
4269{
4270 int ib_flags = 0;
4271
4272 if (mlx5_flags & MLX5_QP_BIT_RRE)
4273 ib_flags |= IB_ACCESS_REMOTE_READ;
4274 if (mlx5_flags & MLX5_QP_BIT_RWE)
4275 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4276 if (mlx5_flags & MLX5_QP_BIT_RAE)
4277 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4278
4279 return ib_flags;
4280}
4281
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004282static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004283 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004284 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03004285{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004286 struct mlx5_core_dev *dev = ibdev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004287
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004288 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03004289
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04004290 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004291 rdma_ah_set_port_num(ah_attr, path->port);
4292 if (rdma_ah_get_port_num(ah_attr) == 0 ||
4293 rdma_ah_get_port_num(ah_attr) > MLX5_CAP_GEN(dev, num_ports))
Eli Cohene126ba92013-07-07 17:25:49 +03004294 return;
4295
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004296 rdma_ah_set_port_num(ah_attr, path->port);
4297 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03004298
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004299 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
4300 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
4301 rdma_ah_set_static_rate(ah_attr,
4302 path->static_rate ? path->static_rate - 5 : 0);
4303 if (path->grh_mlid & (1 << 7)) {
4304 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
4305
4306 rdma_ah_set_grh(ah_attr, NULL,
4307 tc_fl & 0xfffff,
4308 path->mgid_index,
4309 path->hop_limit,
4310 (tc_fl >> 20) & 0xff);
4311 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03004312 }
4313}
4314
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004315static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4316 struct mlx5_ib_sq *sq,
4317 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004318{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004319 void *out;
4320 void *sqc;
4321 int inlen;
4322 int err;
4323
4324 inlen = MLX5_ST_SZ_BYTES(query_sq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004325 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004326 if (!out)
4327 return -ENOMEM;
4328
4329 err = mlx5_core_query_sq(dev->mdev, sq->base.mqp.qpn, out);
4330 if (err)
4331 goto out;
4332
4333 sqc = MLX5_ADDR_OF(query_sq_out, out, sq_context);
4334 *sq_state = MLX5_GET(sqc, sqc, state);
4335 sq->state = *sq_state;
4336
4337out:
4338 kvfree(out);
4339 return err;
4340}
4341
4342static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
4343 struct mlx5_ib_rq *rq,
4344 u8 *rq_state)
4345{
4346 void *out;
4347 void *rqc;
4348 int inlen;
4349 int err;
4350
4351 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004352 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004353 if (!out)
4354 return -ENOMEM;
4355
4356 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
4357 if (err)
4358 goto out;
4359
4360 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
4361 *rq_state = MLX5_GET(rqc, rqc, state);
4362 rq->state = *rq_state;
4363
4364out:
4365 kvfree(out);
4366 return err;
4367}
4368
4369static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
4370 struct mlx5_ib_qp *qp, u8 *qp_state)
4371{
4372 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
4373 [MLX5_RQC_STATE_RST] = {
4374 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4375 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4376 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
4377 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
4378 },
4379 [MLX5_RQC_STATE_RDY] = {
4380 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4381 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4382 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
4383 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
4384 },
4385 [MLX5_RQC_STATE_ERR] = {
4386 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4387 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4388 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
4389 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
4390 },
4391 [MLX5_RQ_STATE_NA] = {
4392 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4393 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4394 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
4395 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
4396 },
4397 };
4398
4399 *qp_state = sqrq_trans[rq_state][sq_state];
4400
4401 if (*qp_state == MLX5_QP_STATE_BAD) {
4402 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4403 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
4404 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
4405 return -EINVAL;
4406 }
4407
4408 if (*qp_state == MLX5_QP_STATE)
4409 *qp_state = qp->state;
4410
4411 return 0;
4412}
4413
4414static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
4415 struct mlx5_ib_qp *qp,
4416 u8 *raw_packet_qp_state)
4417{
4418 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
4419 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
4420 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
4421 int err;
4422 u8 sq_state = MLX5_SQ_STATE_NA;
4423 u8 rq_state = MLX5_RQ_STATE_NA;
4424
4425 if (qp->sq.wqe_cnt) {
4426 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
4427 if (err)
4428 return err;
4429 }
4430
4431 if (qp->rq.wqe_cnt) {
4432 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
4433 if (err)
4434 return err;
4435 }
4436
4437 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
4438 raw_packet_qp_state);
4439}
4440
4441static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
4442 struct ib_qp_attr *qp_attr)
4443{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004444 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03004445 struct mlx5_qp_context *context;
4446 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004447 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03004448 int err = 0;
4449
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004450 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004451 if (!outb)
4452 return -ENOMEM;
4453
majd@mellanox.com19098df2016-01-14 19:13:03 +02004454 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004455 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03004456 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004457 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004458
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004459 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
4460 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
4461
Eli Cohene126ba92013-07-07 17:25:49 +03004462 mlx5_state = be32_to_cpu(context->flags) >> 28;
4463
4464 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03004465 qp_attr->path_mtu = context->mtu_msgmax >> 5;
4466 qp_attr->path_mig_state =
4467 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
4468 qp_attr->qkey = be32_to_cpu(context->qkey);
4469 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
4470 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
4471 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
4472 qp_attr->qp_access_flags =
4473 to_ib_qp_access_flags(be32_to_cpu(context->params2));
4474
4475 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004476 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
4477 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004478 qp_attr->alt_pkey_index =
4479 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004480 qp_attr->alt_port_num =
4481 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03004482 }
4483
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004484 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004485 qp_attr->port_num = context->pri_path.port;
4486
4487 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4488 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
4489
4490 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
4491
4492 qp_attr->max_dest_rd_atomic =
4493 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
4494 qp_attr->min_rnr_timer =
4495 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
4496 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
4497 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
4498 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
4499 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004500
4501out:
4502 kfree(outb);
4503 return err;
4504}
4505
4506int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
4507 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
4508{
4509 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4510 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4511 int err = 0;
4512 u8 raw_packet_qp_state;
4513
Yishai Hadas28d61372016-05-23 15:20:56 +03004514 if (ibqp->rwq_ind_tbl)
4515 return -ENOSYS;
4516
Haggai Erand16e91d2016-02-29 15:45:05 +02004517 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4518 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
4519 qp_init_attr);
4520
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004521 /* Not all of output fields are applicable, make sure to zero them */
4522 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
4523 memset(qp_attr, 0, sizeof(*qp_attr));
4524
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004525 mutex_lock(&qp->mutex);
4526
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004527 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
4528 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004529 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
4530 if (err)
4531 goto out;
4532 qp->state = raw_packet_qp_state;
4533 qp_attr->port_num = 1;
4534 } else {
4535 err = query_qp_attr(dev, qp, qp_attr);
4536 if (err)
4537 goto out;
4538 }
4539
4540 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03004541 qp_attr->cur_qp_state = qp_attr->qp_state;
4542 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
4543 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
4544
4545 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03004546 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03004547 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03004548 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03004549 } else {
4550 qp_attr->cap.max_send_wr = 0;
4551 qp_attr->cap.max_send_sge = 0;
4552 }
4553
Noa Osherovich0540d812016-06-04 15:15:32 +03004554 qp_init_attr->qp_type = ibqp->qp_type;
4555 qp_init_attr->recv_cq = ibqp->recv_cq;
4556 qp_init_attr->send_cq = ibqp->send_cq;
4557 qp_init_attr->srq = ibqp->srq;
4558 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03004559
4560 qp_init_attr->cap = qp_attr->cap;
4561
4562 qp_init_attr->create_flags = 0;
4563 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
4564 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
4565
Leon Romanovsky051f2632015-12-20 12:16:11 +02004566 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
4567 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
4568 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
4569 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
4570 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
4571 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02004572 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
4573 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02004574
Eli Cohene126ba92013-07-07 17:25:49 +03004575 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
4576 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
4577
Eli Cohene126ba92013-07-07 17:25:49 +03004578out:
4579 mutex_unlock(&qp->mutex);
4580 return err;
4581}
4582
4583struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
4584 struct ib_ucontext *context,
4585 struct ib_udata *udata)
4586{
4587 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4588 struct mlx5_ib_xrcd *xrcd;
4589 int err;
4590
Saeed Mahameed938fe832015-05-28 22:28:41 +03004591 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03004592 return ERR_PTR(-ENOSYS);
4593
4594 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
4595 if (!xrcd)
4596 return ERR_PTR(-ENOMEM);
4597
Jack Morgenstein9603b612014-07-28 23:30:22 +03004598 err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004599 if (err) {
4600 kfree(xrcd);
4601 return ERR_PTR(-ENOMEM);
4602 }
4603
4604 return &xrcd->ibxrcd;
4605}
4606
4607int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
4608{
4609 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
4610 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
4611 int err;
4612
Jack Morgenstein9603b612014-07-28 23:30:22 +03004613 err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004614 if (err) {
4615 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
4616 return err;
4617 }
4618
4619 kfree(xrcd);
4620
4621 return 0;
4622}
Yishai Hadas79b20a62016-05-23 15:20:50 +03004623
Yishai Hadas350d0e42016-08-28 14:58:18 +03004624static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
4625{
4626 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
4627 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
4628 struct ib_event event;
4629
4630 if (rwq->ibwq.event_handler) {
4631 event.device = rwq->ibwq.device;
4632 event.element.wq = &rwq->ibwq;
4633 switch (type) {
4634 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
4635 event.event = IB_EVENT_WQ_FATAL;
4636 break;
4637 default:
4638 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
4639 return;
4640 }
4641
4642 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
4643 }
4644}
4645
Maor Gottlieb03404e82017-05-30 10:29:13 +03004646static int set_delay_drop(struct mlx5_ib_dev *dev)
4647{
4648 int err = 0;
4649
4650 mutex_lock(&dev->delay_drop.lock);
4651 if (dev->delay_drop.activate)
4652 goto out;
4653
4654 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
4655 if (err)
4656 goto out;
4657
4658 dev->delay_drop.activate = true;
4659out:
4660 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004661
4662 if (!err)
4663 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004664 return err;
4665}
4666
Yishai Hadas79b20a62016-05-23 15:20:50 +03004667static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
4668 struct ib_wq_init_attr *init_attr)
4669{
4670 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02004671 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004672 __be64 *rq_pas0;
4673 void *in;
4674 void *rqc;
4675 void *wq;
4676 int inlen;
4677 int err;
4678
4679 dev = to_mdev(pd->device);
4680
4681 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004682 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004683 if (!in)
4684 return -ENOMEM;
4685
4686 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
4687 MLX5_SET(rqc, rqc, mem_rq_type,
4688 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
4689 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
4690 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
4691 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
4692 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
4693 wq = MLX5_ADDR_OF(rqc, rqc, wq);
4694 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
4695 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
4696 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
4697 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
4698 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
4699 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
4700 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
4701 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
4702 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02004703 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004704 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02004705 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004706 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
4707 err = -EOPNOTSUPP;
4708 goto out;
4709 }
4710 } else {
4711 MLX5_SET(rqc, rqc, vsd, 1);
4712 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02004713 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
4714 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
4715 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
4716 err = -EOPNOTSUPP;
4717 goto out;
4718 }
4719 MLX5_SET(rqc, rqc, scatter_fcs, 1);
4720 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03004721 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4722 if (!(dev->ib_dev.attrs.raw_packet_caps &
4723 IB_RAW_PACKET_CAP_DELAY_DROP)) {
4724 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
4725 err = -EOPNOTSUPP;
4726 goto out;
4727 }
4728 MLX5_SET(rqc, rqc, delay_drop_en, 1);
4729 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004730 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
4731 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03004732 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004733 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4734 err = set_delay_drop(dev);
4735 if (err) {
4736 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
4737 err);
4738 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
4739 } else {
4740 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
4741 }
4742 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004743out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03004744 kvfree(in);
4745 return err;
4746}
4747
4748static int set_user_rq_size(struct mlx5_ib_dev *dev,
4749 struct ib_wq_init_attr *wq_init_attr,
4750 struct mlx5_ib_create_wq *ucmd,
4751 struct mlx5_ib_rwq *rwq)
4752{
4753 /* Sanity check RQ size before proceeding */
4754 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
4755 return -EINVAL;
4756
4757 if (!ucmd->rq_wqe_count)
4758 return -EINVAL;
4759
4760 rwq->wqe_count = ucmd->rq_wqe_count;
4761 rwq->wqe_shift = ucmd->rq_wqe_shift;
4762 rwq->buf_size = (rwq->wqe_count << rwq->wqe_shift);
4763 rwq->log_rq_stride = rwq->wqe_shift;
4764 rwq->log_rq_size = ilog2(rwq->wqe_count);
4765 return 0;
4766}
4767
4768static int prepare_user_rq(struct ib_pd *pd,
4769 struct ib_wq_init_attr *init_attr,
4770 struct ib_udata *udata,
4771 struct mlx5_ib_rwq *rwq)
4772{
4773 struct mlx5_ib_dev *dev = to_mdev(pd->device);
4774 struct mlx5_ib_create_wq ucmd = {};
4775 int err;
4776 size_t required_cmd_sz;
4777
4778 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4779 if (udata->inlen < required_cmd_sz) {
4780 mlx5_ib_dbg(dev, "invalid inlen\n");
4781 return -EINVAL;
4782 }
4783
4784 if (udata->inlen > sizeof(ucmd) &&
4785 !ib_is_udata_cleared(udata, sizeof(ucmd),
4786 udata->inlen - sizeof(ucmd))) {
4787 mlx5_ib_dbg(dev, "inlen is not supported\n");
4788 return -EOPNOTSUPP;
4789 }
4790
4791 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
4792 mlx5_ib_dbg(dev, "copy failed\n");
4793 return -EFAULT;
4794 }
4795
4796 if (ucmd.comp_mask) {
4797 mlx5_ib_dbg(dev, "invalid comp mask\n");
4798 return -EOPNOTSUPP;
4799 }
4800
4801 if (ucmd.reserved) {
4802 mlx5_ib_dbg(dev, "invalid reserved\n");
4803 return -EOPNOTSUPP;
4804 }
4805
4806 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
4807 if (err) {
4808 mlx5_ib_dbg(dev, "err %d\n", err);
4809 return err;
4810 }
4811
4812 err = create_user_rq(dev, pd, rwq, &ucmd);
4813 if (err) {
4814 mlx5_ib_dbg(dev, "err %d\n", err);
4815 if (err)
4816 return err;
4817 }
4818
4819 rwq->user_index = ucmd.user_index;
4820 return 0;
4821}
4822
4823struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
4824 struct ib_wq_init_attr *init_attr,
4825 struct ib_udata *udata)
4826{
4827 struct mlx5_ib_dev *dev;
4828 struct mlx5_ib_rwq *rwq;
4829 struct mlx5_ib_create_wq_resp resp = {};
4830 size_t min_resp_len;
4831 int err;
4832
4833 if (!udata)
4834 return ERR_PTR(-ENOSYS);
4835
4836 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4837 if (udata->outlen && udata->outlen < min_resp_len)
4838 return ERR_PTR(-EINVAL);
4839
4840 dev = to_mdev(pd->device);
4841 switch (init_attr->wq_type) {
4842 case IB_WQT_RQ:
4843 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
4844 if (!rwq)
4845 return ERR_PTR(-ENOMEM);
4846 err = prepare_user_rq(pd, init_attr, udata, rwq);
4847 if (err)
4848 goto err;
4849 err = create_rq(rwq, pd, init_attr);
4850 if (err)
4851 goto err_user_rq;
4852 break;
4853 default:
4854 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
4855 init_attr->wq_type);
4856 return ERR_PTR(-EINVAL);
4857 }
4858
Yishai Hadas350d0e42016-08-28 14:58:18 +03004859 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004860 rwq->ibwq.state = IB_WQS_RESET;
4861 if (udata->outlen) {
4862 resp.response_length = offsetof(typeof(resp), response_length) +
4863 sizeof(resp.response_length);
4864 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4865 if (err)
4866 goto err_copy;
4867 }
4868
Yishai Hadas350d0e42016-08-28 14:58:18 +03004869 rwq->core_qp.event = mlx5_ib_wq_event;
4870 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004871 return &rwq->ibwq;
4872
4873err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03004874 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004875err_user_rq:
Maor Gottliebfe248c32017-05-30 10:29:14 +03004876 destroy_user_rq(dev, pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004877err:
4878 kfree(rwq);
4879 return ERR_PTR(err);
4880}
4881
4882int mlx5_ib_destroy_wq(struct ib_wq *wq)
4883{
4884 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4885 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4886
Yishai Hadas350d0e42016-08-28 14:58:18 +03004887 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004888 destroy_user_rq(dev, wq->pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004889 kfree(rwq);
4890
4891 return 0;
4892}
4893
Yishai Hadasc5f90922016-05-23 15:20:53 +03004894struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
4895 struct ib_rwq_ind_table_init_attr *init_attr,
4896 struct ib_udata *udata)
4897{
4898 struct mlx5_ib_dev *dev = to_mdev(device);
4899 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
4900 int sz = 1 << init_attr->log_ind_tbl_size;
4901 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
4902 size_t min_resp_len;
4903 int inlen;
4904 int err;
4905 int i;
4906 u32 *in;
4907 void *rqtc;
4908
4909 if (udata->inlen > 0 &&
4910 !ib_is_udata_cleared(udata, 0,
4911 udata->inlen))
4912 return ERR_PTR(-EOPNOTSUPP);
4913
Maor Gottliebefd7f402016-10-27 16:36:40 +03004914 if (init_attr->log_ind_tbl_size >
4915 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
4916 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
4917 init_attr->log_ind_tbl_size,
4918 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
4919 return ERR_PTR(-EINVAL);
4920 }
4921
Yishai Hadasc5f90922016-05-23 15:20:53 +03004922 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4923 if (udata->outlen && udata->outlen < min_resp_len)
4924 return ERR_PTR(-EINVAL);
4925
4926 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
4927 if (!rwq_ind_tbl)
4928 return ERR_PTR(-ENOMEM);
4929
4930 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004931 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03004932 if (!in) {
4933 err = -ENOMEM;
4934 goto err;
4935 }
4936
4937 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
4938
4939 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
4940 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
4941
4942 for (i = 0; i < sz; i++)
4943 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
4944
4945 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
4946 kvfree(in);
4947
4948 if (err)
4949 goto err;
4950
4951 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
4952 if (udata->outlen) {
4953 resp.response_length = offsetof(typeof(resp), response_length) +
4954 sizeof(resp.response_length);
4955 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4956 if (err)
4957 goto err_copy;
4958 }
4959
4960 return &rwq_ind_tbl->ib_rwq_ind_tbl;
4961
4962err_copy:
4963 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4964err:
4965 kfree(rwq_ind_tbl);
4966 return ERR_PTR(err);
4967}
4968
4969int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
4970{
4971 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
4972 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
4973
4974 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4975
4976 kfree(rwq_ind_tbl);
4977 return 0;
4978}
4979
Yishai Hadas79b20a62016-05-23 15:20:50 +03004980int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
4981 u32 wq_attr_mask, struct ib_udata *udata)
4982{
4983 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4984 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4985 struct mlx5_ib_modify_wq ucmd = {};
4986 size_t required_cmd_sz;
4987 int curr_wq_state;
4988 int wq_state;
4989 int inlen;
4990 int err;
4991 void *rqc;
4992 void *in;
4993
4994 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4995 if (udata->inlen < required_cmd_sz)
4996 return -EINVAL;
4997
4998 if (udata->inlen > sizeof(ucmd) &&
4999 !ib_is_udata_cleared(udata, sizeof(ucmd),
5000 udata->inlen - sizeof(ucmd)))
5001 return -EOPNOTSUPP;
5002
5003 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
5004 return -EFAULT;
5005
5006 if (ucmd.comp_mask || ucmd.reserved)
5007 return -EOPNOTSUPP;
5008
5009 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005010 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005011 if (!in)
5012 return -ENOMEM;
5013
5014 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
5015
5016 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
5017 wq_attr->curr_wq_state : wq->state;
5018 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
5019 wq_attr->wq_state : curr_wq_state;
5020 if (curr_wq_state == IB_WQS_ERR)
5021 curr_wq_state = MLX5_RQC_STATE_ERR;
5022 if (wq_state == IB_WQS_ERR)
5023 wq_state = MLX5_RQC_STATE_ERR;
5024 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
5025 MLX5_SET(rqc, rqc, state, wq_state);
5026
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005027 if (wq_attr_mask & IB_WQ_FLAGS) {
5028 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
5029 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
5030 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
5031 mlx5_ib_dbg(dev, "VLAN offloads are not "
5032 "supported\n");
5033 err = -EOPNOTSUPP;
5034 goto out;
5035 }
5036 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5037 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
5038 MLX5_SET(rqc, rqc, vsd,
5039 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
5040 }
5041 }
5042
Majd Dibbiny23a69642017-01-18 15:25:10 +02005043 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
5044 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
5045 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5046 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03005047 MLX5_SET(rqc, rqc, counter_set_id,
5048 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005049 } else
5050 pr_info_once("%s: Receive WQ counters are not supported on current FW\n",
5051 dev->ib_dev.name);
5052 }
5053
Yishai Hadas350d0e42016-08-28 14:58:18 +03005054 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005055 if (!err)
5056 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
5057
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005058out:
5059 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005060 return err;
5061}