blob: 45b9aba599c892292c277a5cb67f71c52ae863ab [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/module.h>
34#include <rdma/ib_umem.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020035#include <rdma/ib_cache.h>
Haggai Abramovskycfb5e082016-01-14 19:12:57 +020036#include <rdma/ib_user_verbs.h>
Yishai Hadasc2e53b22017-06-08 16:15:08 +030037#include <linux/mlx5/fs.h>
Eli Cohene126ba92013-07-07 17:25:49 +030038#include "mlx5_ib.h"
Eli Cohene126ba92013-07-07 17:25:49 +030039
40/* not supported currently */
41static int wq_signature;
42
43enum {
44 MLX5_IB_ACK_REQ_FREQ = 8,
45};
46
47enum {
48 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
49 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
50 MLX5_IB_LINK_TYPE_IB = 0,
51 MLX5_IB_LINK_TYPE_ETH = 1
52};
53
54enum {
55 MLX5_IB_SQ_STRIDE = 6,
Eli Cohene126ba92013-07-07 17:25:49 +030056};
57
58static const u32 mlx5_ib_opcode[] = {
59 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020060 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030061 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
62 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
63 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
64 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
65 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
66 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
67 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
68 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030069 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030070 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
71 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
72 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
73};
74
Erez Shitritf0313962016-02-21 16:27:17 +020075struct mlx5_wqe_eth_pad {
76 u8 rsvd0[16];
77};
Eli Cohene126ba92013-07-07 17:25:49 +030078
Alex Veskereb49ab02016-08-28 12:25:53 +030079enum raw_qp_set_mask_map {
80 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020081 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030082};
83
Alex Vesker0680efa2016-08-28 12:25:52 +030084struct mlx5_modify_raw_qp_param {
85 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030086
87 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang7d29f342016-12-01 13:43:16 +020088 u32 rate_limit;
Alex Veskereb49ab02016-08-28 12:25:53 +030089 u8 rq_q_ctr_id;
Alex Vesker0680efa2016-08-28 12:25:52 +030090};
91
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030092static void get_cqs(enum ib_qp_type qp_type,
93 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
94 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
95
Eli Cohene126ba92013-07-07 17:25:49 +030096static int is_qp0(enum ib_qp_type qp_type)
97{
98 return qp_type == IB_QPT_SMI;
99}
100
Eli Cohene126ba92013-07-07 17:25:49 +0300101static int is_sqp(enum ib_qp_type qp_type)
102{
103 return is_qp0(qp_type) || is_qp1(qp_type);
104}
105
106static void *get_wqe(struct mlx5_ib_qp *qp, int offset)
107{
108 return mlx5_buf_offset(&qp->buf, offset);
109}
110
111static void *get_recv_wqe(struct mlx5_ib_qp *qp, int n)
112{
113 return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
114}
115
116void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n)
117{
118 return get_wqe(qp, qp->sq.offset + (n << MLX5_IB_SQ_STRIDE));
119}
120
Haggai Eranc1395a22014-12-11 17:04:14 +0200121/**
122 * mlx5_ib_read_user_wqe() - Copy a user-space WQE to kernel space.
123 *
124 * @qp: QP to copy from.
125 * @send: copy from the send queue when non-zero, use the receive queue
126 * otherwise.
127 * @wqe_index: index to start copying from. For send work queues, the
128 * wqe_index is in units of MLX5_SEND_WQE_BB.
129 * For receive work queue, it is the number of work queue
130 * element in the queue.
131 * @buffer: destination buffer.
132 * @length: maximum number of bytes to copy.
133 *
134 * Copies at least a single WQE, but may copy more data.
135 *
136 * Return: the number of bytes copied, or an error code.
137 */
138int mlx5_ib_read_user_wqe(struct mlx5_ib_qp *qp, int send, int wqe_index,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200139 void *buffer, u32 length,
140 struct mlx5_ib_qp_base *base)
Haggai Eranc1395a22014-12-11 17:04:14 +0200141{
142 struct ib_device *ibdev = qp->ibqp.device;
143 struct mlx5_ib_dev *dev = to_mdev(ibdev);
144 struct mlx5_ib_wq *wq = send ? &qp->sq : &qp->rq;
145 size_t offset;
146 size_t wq_end;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200147 struct ib_umem *umem = base->ubuffer.umem;
Haggai Eranc1395a22014-12-11 17:04:14 +0200148 u32 first_copy_length;
149 int wqe_length;
150 int ret;
151
152 if (wq->wqe_cnt == 0) {
153 mlx5_ib_dbg(dev, "mlx5_ib_read_user_wqe for a QP with wqe_cnt == 0. qp_type: 0x%x\n",
154 qp->ibqp.qp_type);
155 return -EINVAL;
156 }
157
158 offset = wq->offset + ((wqe_index % wq->wqe_cnt) << wq->wqe_shift);
159 wq_end = wq->offset + (wq->wqe_cnt << wq->wqe_shift);
160
161 if (send && length < sizeof(struct mlx5_wqe_ctrl_seg))
162 return -EINVAL;
163
164 if (offset > umem->length ||
165 (send && offset + sizeof(struct mlx5_wqe_ctrl_seg) > umem->length))
166 return -EINVAL;
167
168 first_copy_length = min_t(u32, offset + length, wq_end) - offset;
169 ret = ib_umem_copy_from(buffer, umem, offset, first_copy_length);
170 if (ret)
171 return ret;
172
173 if (send) {
174 struct mlx5_wqe_ctrl_seg *ctrl = buffer;
175 int ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
176
177 wqe_length = ds * MLX5_WQE_DS_UNITS;
178 } else {
179 wqe_length = 1 << wq->wqe_shift;
180 }
181
182 if (wqe_length <= first_copy_length)
183 return first_copy_length;
184
185 ret = ib_umem_copy_from(buffer + first_copy_length, umem, wq->offset,
186 wqe_length - first_copy_length);
187 if (ret)
188 return ret;
189
190 return wqe_length;
191}
192
Eli Cohene126ba92013-07-07 17:25:49 +0300193static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
194{
195 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
196 struct ib_event event;
197
majd@mellanox.com19098df2016-01-14 19:13:03 +0200198 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
199 /* This event is only valid for trans_qps */
200 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
201 }
Eli Cohene126ba92013-07-07 17:25:49 +0300202
203 if (ibqp->event_handler) {
204 event.device = ibqp->device;
205 event.element.qp = ibqp;
206 switch (type) {
207 case MLX5_EVENT_TYPE_PATH_MIG:
208 event.event = IB_EVENT_PATH_MIG;
209 break;
210 case MLX5_EVENT_TYPE_COMM_EST:
211 event.event = IB_EVENT_COMM_EST;
212 break;
213 case MLX5_EVENT_TYPE_SQ_DRAINED:
214 event.event = IB_EVENT_SQ_DRAINED;
215 break;
216 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
217 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
218 break;
219 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
220 event.event = IB_EVENT_QP_FATAL;
221 break;
222 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
223 event.event = IB_EVENT_PATH_MIG_ERR;
224 break;
225 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
226 event.event = IB_EVENT_QP_REQ_ERR;
227 break;
228 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
229 event.event = IB_EVENT_QP_ACCESS_ERR;
230 break;
231 default:
232 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
233 return;
234 }
235
236 ibqp->event_handler(&event, ibqp->qp_context);
237 }
238}
239
240static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
241 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
242{
243 int wqe_size;
244 int wq_size;
245
246 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300247 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300248 return -EINVAL;
249
250 if (!has_rq) {
251 qp->rq.max_gs = 0;
252 qp->rq.wqe_cnt = 0;
253 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300254 cap->max_recv_wr = 0;
255 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300256 } else {
257 if (ucmd) {
258 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
259 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
260 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
261 qp->rq.max_post = qp->rq.wqe_cnt;
262 } else {
263 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
264 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
265 wqe_size = roundup_pow_of_two(wqe_size);
266 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
267 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
268 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300269 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300270 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
271 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300272 MLX5_CAP_GEN(dev->mdev,
273 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300274 return -EINVAL;
275 }
276 qp->rq.wqe_shift = ilog2(wqe_size);
277 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
278 qp->rq.max_post = qp->rq.wqe_cnt;
279 }
280 }
281
282 return 0;
283}
284
Erez Shitritf0313962016-02-21 16:27:17 +0200285static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300286{
Andi Shyti618af382013-07-16 15:35:01 +0200287 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300288
Erez Shitritf0313962016-02-21 16:27:17 +0200289 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300290 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300291 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300292 /* fall through */
293 case IB_QPT_RC:
294 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200295 max(sizeof(struct mlx5_wqe_atomic_seg) +
296 sizeof(struct mlx5_wqe_raddr_seg),
297 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
298 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300299 break;
300
Eli Cohenb125a542013-09-11 16:35:22 +0300301 case IB_QPT_XRC_TGT:
302 return 0;
303
Eli Cohene126ba92013-07-07 17:25:49 +0300304 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300305 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200306 max(sizeof(struct mlx5_wqe_raddr_seg),
307 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
308 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300309 break;
310
311 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200312 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
313 size += sizeof(struct mlx5_wqe_eth_pad) +
314 sizeof(struct mlx5_wqe_eth_seg);
315 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300316 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200317 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300318 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300319 sizeof(struct mlx5_wqe_datagram_seg);
320 break;
321
322 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300323 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300324 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
325 sizeof(struct mlx5_mkey_seg);
326 break;
327
328 default:
329 return -EINVAL;
330 }
331
332 return size;
333}
334
335static int calc_send_wqe(struct ib_qp_init_attr *attr)
336{
337 int inl_size = 0;
338 int size;
339
Erez Shitritf0313962016-02-21 16:27:17 +0200340 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300341 if (size < 0)
342 return size;
343
344 if (attr->cap.max_inline_data) {
345 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
346 attr->cap.max_inline_data;
347 }
348
349 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200350 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN &&
351 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
352 return MLX5_SIG_WQE_SIZE;
353 else
354 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300355}
356
Eli Cohen288c01b2016-10-27 16:36:45 +0300357static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
358{
359 int max_sge;
360
361 if (attr->qp_type == IB_QPT_RC)
362 max_sge = (min_t(int, wqe_size, 512) -
363 sizeof(struct mlx5_wqe_ctrl_seg) -
364 sizeof(struct mlx5_wqe_raddr_seg)) /
365 sizeof(struct mlx5_wqe_data_seg);
366 else if (attr->qp_type == IB_QPT_XRC_INI)
367 max_sge = (min_t(int, wqe_size, 512) -
368 sizeof(struct mlx5_wqe_ctrl_seg) -
369 sizeof(struct mlx5_wqe_xrc_seg) -
370 sizeof(struct mlx5_wqe_raddr_seg)) /
371 sizeof(struct mlx5_wqe_data_seg);
372 else
373 max_sge = (wqe_size - sq_overhead(attr)) /
374 sizeof(struct mlx5_wqe_data_seg);
375
376 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
377 sizeof(struct mlx5_wqe_data_seg));
378}
379
Eli Cohene126ba92013-07-07 17:25:49 +0300380static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
381 struct mlx5_ib_qp *qp)
382{
383 int wqe_size;
384 int wq_size;
385
386 if (!attr->cap.max_send_wr)
387 return 0;
388
389 wqe_size = calc_send_wqe(attr);
390 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
391 if (wqe_size < 0)
392 return wqe_size;
393
Saeed Mahameed938fe832015-05-28 22:28:41 +0300394 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300395 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300396 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300397 return -EINVAL;
398 }
399
Erez Shitritf0313962016-02-21 16:27:17 +0200400 qp->max_inline_data = wqe_size - sq_overhead(attr) -
401 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300402 attr->cap.max_inline_data = qp->max_inline_data;
403
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200404 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN)
405 qp->signature_en = true;
406
Eli Cohene126ba92013-07-07 17:25:49 +0300407 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
408 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300409 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800410 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
411 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300412 qp->sq.wqe_cnt,
413 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300414 return -ENOMEM;
415 }
Eli Cohene126ba92013-07-07 17:25:49 +0300416 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300417 qp->sq.max_gs = get_send_sge(attr, wqe_size);
418 if (qp->sq.max_gs < attr->cap.max_send_sge)
419 return -ENOMEM;
420
421 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300422 qp->sq.max_post = wq_size / wqe_size;
423 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300424
425 return wq_size;
426}
427
428static int set_user_buf_size(struct mlx5_ib_dev *dev,
429 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200430 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200431 struct mlx5_ib_qp_base *base,
432 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300433{
434 int desc_sz = 1 << qp->sq.wqe_shift;
435
Saeed Mahameed938fe832015-05-28 22:28:41 +0300436 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300437 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300438 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300439 return -EINVAL;
440 }
441
442 if (ucmd->sq_wqe_count && ((1 << ilog2(ucmd->sq_wqe_count)) != ucmd->sq_wqe_count)) {
443 mlx5_ib_warn(dev, "sq_wqe_count %d, sq_wqe_count %d\n",
444 ucmd->sq_wqe_count, ucmd->sq_wqe_count);
445 return -EINVAL;
446 }
447
448 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
449
Saeed Mahameed938fe832015-05-28 22:28:41 +0300450 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300451 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300452 qp->sq.wqe_cnt,
453 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300454 return -EINVAL;
455 }
456
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300457 if (attr->qp_type == IB_QPT_RAW_PACKET ||
458 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200459 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
460 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
461 } else {
462 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
463 (qp->sq.wqe_cnt << 6);
464 }
Eli Cohene126ba92013-07-07 17:25:49 +0300465
466 return 0;
467}
468
469static int qp_has_rq(struct ib_qp_init_attr *attr)
470{
471 if (attr->qp_type == IB_QPT_XRC_INI ||
472 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
473 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
474 !attr->cap.max_recv_wr)
475 return 0;
476
477 return 1;
478}
479
Eli Cohen2f5ff262017-01-03 23:55:21 +0200480static int first_med_bfreg(void)
Eli Cohenc1be5232014-01-14 17:45:12 +0200481{
482 return 1;
483}
484
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200485enum {
486 /* this is the first blue flame register in the array of bfregs assigned
487 * to a processes. Since we do not use it for blue flame but rather
488 * regular 64 bit doorbells, we do not need a lock for maintaiing
489 * "odd/even" order
490 */
491 NUM_NON_BLUE_FLAME_BFREGS = 1,
492};
493
Eli Cohenb037c292017-01-03 23:55:26 +0200494static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
495{
Yishai Hadas31a78a52017-12-24 16:31:34 +0200496 return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200497}
498
499static int num_med_bfreg(struct mlx5_ib_dev *dev,
500 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200501{
502 int n;
503
Eli Cohenb037c292017-01-03 23:55:26 +0200504 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
505 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200506
507 return n >= 0 ? n : 0;
508}
509
Eli Cohenb037c292017-01-03 23:55:26 +0200510static int first_hi_bfreg(struct mlx5_ib_dev *dev,
511 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200512{
513 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200514
Eli Cohenb037c292017-01-03 23:55:26 +0200515 med = num_med_bfreg(dev, bfregi);
516 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200517}
518
Eli Cohenb037c292017-01-03 23:55:26 +0200519static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
520 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300521{
Eli Cohene126ba92013-07-07 17:25:49 +0300522 int i;
523
Eli Cohenb037c292017-01-03 23:55:26 +0200524 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
525 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200526 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300527 return i;
528 }
529 }
530
531 return -ENOMEM;
532}
533
Eli Cohenb037c292017-01-03 23:55:26 +0200534static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
535 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300536{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200537 int minidx = first_med_bfreg();
Eli Cohene126ba92013-07-07 17:25:49 +0300538 int i;
539
Eli Cohenb037c292017-01-03 23:55:26 +0200540 for (i = first_med_bfreg(); i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200541 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300542 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200543 if (!bfregi->count[minidx])
544 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300545 }
546
Eli Cohen2f5ff262017-01-03 23:55:21 +0200547 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300548 return minidx;
549}
550
Eli Cohenb037c292017-01-03 23:55:26 +0200551static int alloc_bfreg(struct mlx5_ib_dev *dev,
552 struct mlx5_bfreg_info *bfregi,
Eli Cohen2f5ff262017-01-03 23:55:21 +0200553 enum mlx5_ib_latency_class lat)
Eli Cohene126ba92013-07-07 17:25:49 +0300554{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200555 int bfregn = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300556
Eli Cohen2f5ff262017-01-03 23:55:21 +0200557 mutex_lock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300558 switch (lat) {
559 case MLX5_IB_LATENCY_CLASS_LOW:
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200560 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200561 bfregn = 0;
562 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300563 break;
564
565 case MLX5_IB_LATENCY_CLASS_MEDIUM:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200566 if (bfregi->ver < 2)
567 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200568 else
Eli Cohenb037c292017-01-03 23:55:26 +0200569 bfregn = alloc_med_class_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300570 break;
571
572 case MLX5_IB_LATENCY_CLASS_HIGH:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200573 if (bfregi->ver < 2)
574 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200575 else
Eli Cohenb037c292017-01-03 23:55:26 +0200576 bfregn = alloc_high_class_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300577 break;
578 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200579 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300580
Eli Cohen2f5ff262017-01-03 23:55:21 +0200581 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300582}
583
Yishai Hadas4ed131d2017-12-24 16:31:35 +0200584void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300585{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200586 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200587 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200588 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300589}
590
591static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
592{
593 switch (state) {
594 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
595 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
596 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
597 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
598 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
599 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
600 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
601 default: return -1;
602 }
603}
604
605static int to_mlx5_st(enum ib_qp_type type)
606{
607 switch (type) {
608 case IB_QPT_RC: return MLX5_QP_ST_RC;
609 case IB_QPT_UC: return MLX5_QP_ST_UC;
610 case IB_QPT_UD: return MLX5_QP_ST_UD;
611 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
612 case IB_QPT_XRC_INI:
613 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
614 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200615 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
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,
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200630 struct mlx5_bfreg_info *bfregi, int bfregn,
631 bool dyn_bfreg)
Eli Cohene126ba92013-07-07 17:25:49 +0300632{
Eli Cohenb037c292017-01-03 23:55:26 +0200633 int bfregs_per_sys_page;
634 int index_of_sys_page;
635 int offset;
636
637 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
638 MLX5_NON_FP_BFREGS_PER_UAR;
639 index_of_sys_page = bfregn / bfregs_per_sys_page;
640
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200641 if (dyn_bfreg) {
642 index_of_sys_page += bfregi->num_static_sys_pages;
643 if (bfregn > bfregi->num_dyn_bfregs ||
644 bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
645 mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
646 return -EINVAL;
647 }
648 }
Eli Cohenb037c292017-01-03 23:55:26 +0200649
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200650 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
Eli Cohenb037c292017-01-03 23:55:26 +0200651 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300652}
653
majd@mellanox.com19098df2016-01-14 19:13:03 +0200654static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev,
655 struct ib_pd *pd,
656 unsigned long addr, size_t size,
657 struct ib_umem **umem,
658 int *npages, int *page_shift, int *ncont,
659 u32 *offset)
660{
661 int err;
662
663 *umem = ib_umem_get(pd->uobject->context, addr, size, 0, 0);
664 if (IS_ERR(*umem)) {
665 mlx5_ib_dbg(dev, "umem_get failed\n");
666 return PTR_ERR(*umem);
667 }
668
Majd Dibbiny762f8992016-10-27 16:36:47 +0300669 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200670
671 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
672 if (err) {
673 mlx5_ib_warn(dev, "bad offset\n");
674 goto err_umem;
675 }
676
677 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
678 addr, size, *npages, *page_shift, *ncont, *offset);
679
680 return 0;
681
682err_umem:
683 ib_umem_release(*umem);
684 *umem = NULL;
685
686 return err;
687}
688
Maor Gottliebfe248c32017-05-30 10:29:14 +0300689static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
690 struct mlx5_ib_rwq *rwq)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300691{
692 struct mlx5_ib_ucontext *context;
693
Maor Gottliebfe248c32017-05-30 10:29:14 +0300694 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
695 atomic_dec(&dev->delay_drop.rqs_cnt);
696
Yishai Hadas79b20a62016-05-23 15:20:50 +0300697 context = to_mucontext(pd->uobject->context);
698 mlx5_ib_db_unmap_user(context, &rwq->db);
699 if (rwq->umem)
700 ib_umem_release(rwq->umem);
701}
702
703static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
704 struct mlx5_ib_rwq *rwq,
705 struct mlx5_ib_create_wq *ucmd)
706{
707 struct mlx5_ib_ucontext *context;
708 int page_shift = 0;
709 int npages;
710 u32 offset = 0;
711 int ncont = 0;
712 int err;
713
714 if (!ucmd->buf_addr)
715 return -EINVAL;
716
717 context = to_mucontext(pd->uobject->context);
718 rwq->umem = ib_umem_get(pd->uobject->context, ucmd->buf_addr,
719 rwq->buf_size, 0, 0);
720 if (IS_ERR(rwq->umem)) {
721 mlx5_ib_dbg(dev, "umem_get failed\n");
722 err = PTR_ERR(rwq->umem);
723 return err;
724 }
725
Majd Dibbiny762f8992016-10-27 16:36:47 +0300726 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300727 &ncont, NULL);
728 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
729 &rwq->rq_page_offset);
730 if (err) {
731 mlx5_ib_warn(dev, "bad offset\n");
732 goto err_umem;
733 }
734
735 rwq->rq_num_pas = ncont;
736 rwq->page_shift = page_shift;
737 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
738 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
739
740 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
741 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
742 npages, page_shift, ncont, offset);
743
744 err = mlx5_ib_db_map_user(context, ucmd->db_addr, &rwq->db);
745 if (err) {
746 mlx5_ib_dbg(dev, "map failed\n");
747 goto err_umem;
748 }
749
750 rwq->create_type = MLX5_WQ_USER;
751 return 0;
752
753err_umem:
754 ib_umem_release(rwq->umem);
755 return err;
756}
757
Eli Cohenb037c292017-01-03 23:55:26 +0200758static int adjust_bfregn(struct mlx5_ib_dev *dev,
759 struct mlx5_bfreg_info *bfregi, int bfregn)
760{
761 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
762 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
763}
764
Eli Cohene126ba92013-07-07 17:25:49 +0300765static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
766 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200767 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300768 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200769 struct mlx5_ib_create_qp_resp *resp, int *inlen,
770 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300771{
772 struct mlx5_ib_ucontext *context;
773 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200774 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200775 int page_shift = 0;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200776 int uar_index = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300777 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200778 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200779 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200780 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300781 __be64 *pas;
782 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300783 int err;
784
785 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
786 if (err) {
787 mlx5_ib_dbg(dev, "copy failed\n");
788 return err;
789 }
790
791 context = to_mucontext(pd->uobject->context);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200792 if (ucmd.flags & MLX5_QP_FLAG_BFREG_INDEX) {
793 uar_index = bfregn_to_uar_index(dev, &context->bfregi,
794 ucmd.bfreg_index, true);
795 if (uar_index < 0)
796 return uar_index;
797
798 bfregn = MLX5_IB_INVALID_BFREG;
799 } else if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL) {
800 /*
801 * TBD: should come from the verbs when we have the API
802 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200803 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200804 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200805 }
Leon Romanovsky051f2632015-12-20 12:16:11 +0200806 else {
Eli Cohenb037c292017-01-03 23:55:26 +0200807 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_HIGH);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200808 if (bfregn < 0) {
809 mlx5_ib_dbg(dev, "failed to allocate low latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200810 mlx5_ib_dbg(dev, "reverting to medium latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200811 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_MEDIUM);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200812 if (bfregn < 0) {
813 mlx5_ib_dbg(dev, "failed to allocate medium latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200814 mlx5_ib_dbg(dev, "reverting to high latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200815 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_LOW);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200816 if (bfregn < 0) {
817 mlx5_ib_warn(dev, "bfreg allocation failed\n");
818 return bfregn;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200819 }
Eli Cohenc1be5232014-01-14 17:45:12 +0200820 }
Eli Cohene126ba92013-07-07 17:25:49 +0300821 }
822 }
823
Eli Cohen2f5ff262017-01-03 23:55:21 +0200824 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200825 if (bfregn != MLX5_IB_INVALID_BFREG)
826 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
827 false);
Eli Cohene126ba92013-07-07 17:25:49 +0300828
Haggai Eran48fea832014-05-22 14:50:11 +0300829 qp->rq.offset = 0;
830 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
831 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
832
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200833 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300834 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200835 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300836
majd@mellanox.com19098df2016-01-14 19:13:03 +0200837 if (ucmd.buf_addr && ubuffer->buf_size) {
838 ubuffer->buf_addr = ucmd.buf_addr;
839 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr,
840 ubuffer->buf_size,
841 &ubuffer->umem, &npages, &page_shift,
842 &ncont, &offset);
843 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200844 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200845 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200846 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300847 }
Eli Cohene126ba92013-07-07 17:25:49 +0300848
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300849 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
850 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300851 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300852 if (!*in) {
853 err = -ENOMEM;
854 goto err_umem;
855 }
Eli Cohene126ba92013-07-07 17:25:49 +0300856
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300857 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
858 if (ubuffer->umem)
859 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
860
861 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
862
863 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
864 MLX5_SET(qpc, qpc, page_offset, offset);
865
866 MLX5_SET(qpc, qpc, uar_page, uar_index);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200867 if (bfregn != MLX5_IB_INVALID_BFREG)
868 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
869 else
870 resp->bfreg_index = MLX5_IB_INVALID_BFREG;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200871 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300872
873 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
874 if (err) {
875 mlx5_ib_dbg(dev, "map failed\n");
876 goto err_free;
877 }
878
879 err = ib_copy_to_udata(udata, resp, sizeof(*resp));
880 if (err) {
881 mlx5_ib_dbg(dev, "copy failed\n");
882 goto err_unmap;
883 }
884 qp->create_type = MLX5_QP_USER;
885
886 return 0;
887
888err_unmap:
889 mlx5_ib_db_unmap_user(context, &qp->db);
890
891err_free:
Al Viro479163f2014-11-20 08:13:57 +0000892 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300893
894err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200895 if (ubuffer->umem)
896 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300897
Eli Cohen2f5ff262017-01-03 23:55:21 +0200898err_bfreg:
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200899 if (bfregn != MLX5_IB_INVALID_BFREG)
900 mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300901 return err;
902}
903
Eli Cohenb037c292017-01-03 23:55:26 +0200904static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
905 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300906{
907 struct mlx5_ib_ucontext *context;
908
909 context = to_mucontext(pd->uobject->context);
910 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200911 if (base->ubuffer.umem)
912 ib_umem_release(base->ubuffer.umem);
Yishai Hadas1ee47ab2017-12-24 16:31:36 +0200913
914 /*
915 * Free only the BFREGs which are handled by the kernel.
916 * BFREGs of UARs allocated dynamically are handled by user.
917 */
918 if (qp->bfregn != MLX5_IB_INVALID_BFREG)
919 mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300920}
921
922static int create_kernel_qp(struct mlx5_ib_dev *dev,
923 struct ib_qp_init_attr *init_attr,
924 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300925 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200926 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300927{
Eli Cohene126ba92013-07-07 17:25:49 +0300928 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300929 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300930 int err;
931
Erez Shitritf0313962016-02-21 16:27:17 +0200932 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
933 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200934 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +0300935 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200936 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200937 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300938
939 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200940 qp->bf.bfreg = &dev->fp_bfreg;
941 else
942 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300943
Eli Cohend8030b02017-02-09 19:31:47 +0200944 /* We need to divide by two since each register is comprised of
945 * two buffers of identical size, namely odd and even
946 */
947 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200948 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +0300949
950 err = calc_sq_size(dev, init_attr, qp);
951 if (err < 0) {
952 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200953 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300954 }
955
956 qp->rq.offset = 0;
957 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200958 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300959
majd@mellanox.com19098df2016-01-14 19:13:03 +0200960 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300961 if (err) {
962 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200963 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300964 }
965
966 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300967 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
968 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300969 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300970 if (!*in) {
971 err = -ENOMEM;
972 goto err_buf;
973 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300974
975 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
976 MLX5_SET(qpc, qpc, uar_page, uar_index);
977 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
978
Eli Cohene126ba92013-07-07 17:25:49 +0300979 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300980 MLX5_SET(qpc, qpc, fre, 1);
981 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300982
Haggai Eranb11a4f92016-02-29 15:45:03 +0200983 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300984 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200985 qp->flags |= MLX5_IB_QP_SQPN_QP1;
986 }
987
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300988 mlx5_fill_page_array(&qp->buf,
989 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300990
Jack Morgenstein9603b612014-07-28 23:30:22 +0300991 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300992 if (err) {
993 mlx5_ib_dbg(dev, "err %d\n", err);
994 goto err_free;
995 }
996
Li Dongyangb5883002017-08-16 23:31:22 +1000997 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
998 sizeof(*qp->sq.wrid), GFP_KERNEL);
999 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
1000 sizeof(*qp->sq.wr_data), GFP_KERNEL);
1001 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1002 sizeof(*qp->rq.wrid), GFP_KERNEL);
1003 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
1004 sizeof(*qp->sq.w_list), GFP_KERNEL);
1005 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
1006 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001007
1008 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
1009 !qp->sq.w_list || !qp->sq.wqe_head) {
1010 err = -ENOMEM;
1011 goto err_wrid;
1012 }
1013 qp->create_type = MLX5_QP_KERNEL;
1014
1015 return 0;
1016
1017err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +10001018 kvfree(qp->sq.wqe_head);
1019 kvfree(qp->sq.w_list);
1020 kvfree(qp->sq.wrid);
1021 kvfree(qp->sq.wr_data);
1022 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001023 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001024
1025err_free:
Al Viro479163f2014-11-20 08:13:57 +00001026 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001027
1028err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001029 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001030 return err;
1031}
1032
1033static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1034{
Li Dongyangb5883002017-08-16 23:31:22 +10001035 kvfree(qp->sq.wqe_head);
1036 kvfree(qp->sq.w_list);
1037 kvfree(qp->sq.wrid);
1038 kvfree(qp->sq.wr_data);
1039 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001040 mlx5_db_free(dev->mdev, &qp->db);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001041 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001042}
1043
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001044static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001045{
1046 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
1047 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001048 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001049 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001050 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001051 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001052 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001053}
1054
1055static int is_connected(enum ib_qp_type qp_type)
1056{
1057 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1058 return 1;
1059
1060 return 0;
1061}
1062
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001063static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001064 struct mlx5_ib_qp *qp,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001065 struct mlx5_ib_sq *sq, u32 tdn)
1066{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001067 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001068 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1069
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001070 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001071 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1072 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1073
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001074 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1075}
1076
1077static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1078 struct mlx5_ib_sq *sq)
1079{
1080 mlx5_core_destroy_tis(dev->mdev, sq->tisn);
1081}
1082
1083static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1084 struct mlx5_ib_sq *sq, void *qpin,
1085 struct ib_pd *pd)
1086{
1087 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1088 __be64 *pas;
1089 void *in;
1090 void *sqc;
1091 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1092 void *wq;
1093 int inlen;
1094 int err;
1095 int page_shift = 0;
1096 int npages;
1097 int ncont = 0;
1098 u32 offset = 0;
1099
1100 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1101 &sq->ubuffer.umem, &npages, &page_shift,
1102 &ncont, &offset);
1103 if (err)
1104 return err;
1105
1106 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001107 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001108 if (!in) {
1109 err = -ENOMEM;
1110 goto err_umem;
1111 }
1112
1113 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1114 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001115 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1116 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001117 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1118 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1119 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1120 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1121 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001122 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1123 MLX5_CAP_ETH(dev->mdev, swp))
1124 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001125
1126 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1127 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1128 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1129 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1130 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1131 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1132 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1133 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1134 MLX5_SET(wq, wq, page_offset, offset);
1135
1136 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1137 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1138
1139 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1140
1141 kvfree(in);
1142
1143 if (err)
1144 goto err_umem;
1145
1146 return 0;
1147
1148err_umem:
1149 ib_umem_release(sq->ubuffer.umem);
1150 sq->ubuffer.umem = NULL;
1151
1152 return err;
1153}
1154
1155static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1156 struct mlx5_ib_sq *sq)
1157{
1158 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1159 ib_umem_release(sq->ubuffer.umem);
1160}
1161
1162static int get_rq_pas_size(void *qpc)
1163{
1164 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1165 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1166 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1167 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1168 u32 po_quanta = 1 << (log_page_size - 6);
1169 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1170 u32 page_size = 1 << log_page_size;
1171 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1172 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1173
1174 return rq_num_pas * sizeof(u64);
1175}
1176
1177static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1178 struct mlx5_ib_rq *rq, void *qpin)
1179{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001180 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001181 __be64 *pas;
1182 __be64 *qp_pas;
1183 void *in;
1184 void *rqc;
1185 void *wq;
1186 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1187 int inlen;
1188 int err;
1189 u32 rq_pas_size = get_rq_pas_size(qpc);
1190
1191 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001192 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001193 if (!in)
1194 return -ENOMEM;
1195
1196 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001197 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1198 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001199 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1200 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1201 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1202 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1203 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1204
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001205 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1206 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1207
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001208 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1209 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001210 if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
1211 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001212 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1213 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1214 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1215 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1216 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1217 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1218
1219 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1220 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1221 memcpy(pas, qp_pas, rq_pas_size);
1222
1223 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1224
1225 kvfree(in);
1226
1227 return err;
1228}
1229
1230static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1231 struct mlx5_ib_rq *rq)
1232{
1233 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1234}
1235
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001236static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1237{
1238 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1239 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1240 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1241}
1242
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001243static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001244 struct mlx5_ib_rq *rq, u32 tdn,
1245 bool tunnel_offload_en)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001246{
1247 u32 *in;
1248 void *tirc;
1249 int inlen;
1250 int err;
1251
1252 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001253 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001254 if (!in)
1255 return -ENOMEM;
1256
1257 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1258 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1259 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1260 MLX5_SET(tirc, tirc, transport_domain, tdn);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001261 if (tunnel_offload_en)
1262 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001263
1264 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1265
1266 kvfree(in);
1267
1268 return err;
1269}
1270
1271static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1272 struct mlx5_ib_rq *rq)
1273{
1274 mlx5_core_destroy_tir(dev->mdev, rq->tirn);
1275}
1276
1277static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001278 u32 *in,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001279 struct ib_pd *pd)
1280{
1281 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1282 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1283 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1284 struct ib_uobject *uobj = pd->uobject;
1285 struct ib_ucontext *ucontext = uobj->context;
1286 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1287 int err;
1288 u32 tdn = mucontext->tdn;
1289
1290 if (qp->sq.wqe_cnt) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001291 err = create_raw_packet_qp_tis(dev, qp, sq, tdn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001292 if (err)
1293 return err;
1294
1295 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1296 if (err)
1297 goto err_destroy_tis;
1298
1299 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001300 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001301 }
1302
1303 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001304 rq->base.container_mibqp = qp;
1305
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001306 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1307 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001308 if (qp->flags & MLX5_IB_QP_PCI_WRITE_END_PADDING)
1309 rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001310 err = create_raw_packet_qp_rq(dev, rq, in);
1311 if (err)
1312 goto err_destroy_sq;
1313
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001314
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001315 err = create_raw_packet_qp_tir(dev, rq, tdn,
1316 qp->tunnel_offload_en);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001317 if (err)
1318 goto err_destroy_rq;
1319 }
1320
1321 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1322 rq->base.mqp.qpn;
1323
1324 return 0;
1325
1326err_destroy_rq:
1327 destroy_raw_packet_qp_rq(dev, rq);
1328err_destroy_sq:
1329 if (!qp->sq.wqe_cnt)
1330 return err;
1331 destroy_raw_packet_qp_sq(dev, sq);
1332err_destroy_tis:
1333 destroy_raw_packet_qp_tis(dev, sq);
1334
1335 return err;
1336}
1337
1338static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1339 struct mlx5_ib_qp *qp)
1340{
1341 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1342 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1343 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1344
1345 if (qp->rq.wqe_cnt) {
1346 destroy_raw_packet_qp_tir(dev, rq);
1347 destroy_raw_packet_qp_rq(dev, rq);
1348 }
1349
1350 if (qp->sq.wqe_cnt) {
1351 destroy_raw_packet_qp_sq(dev, sq);
1352 destroy_raw_packet_qp_tis(dev, sq);
1353 }
1354}
1355
1356static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1357 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1358{
1359 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1360 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1361
1362 sq->sq = &qp->sq;
1363 rq->rq = &qp->rq;
1364 sq->doorbell = &qp->db;
1365 rq->doorbell = &qp->db;
1366}
1367
Yishai Hadas28d61372016-05-23 15:20:56 +03001368static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1369{
1370 mlx5_core_destroy_tir(dev->mdev, qp->rss_qp.tirn);
1371}
1372
1373static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1374 struct ib_pd *pd,
1375 struct ib_qp_init_attr *init_attr,
1376 struct ib_udata *udata)
1377{
1378 struct ib_uobject *uobj = pd->uobject;
1379 struct ib_ucontext *ucontext = uobj->context;
1380 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1381 struct mlx5_ib_create_qp_resp resp = {};
1382 int inlen;
1383 int err;
1384 u32 *in;
1385 void *tirc;
1386 void *hfso;
1387 u32 selected_fields = 0;
1388 size_t min_resp_len;
1389 u32 tdn = mucontext->tdn;
1390 struct mlx5_ib_create_qp_rss ucmd = {};
1391 size_t required_cmd_sz;
1392
1393 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1394 return -EOPNOTSUPP;
1395
1396 if (init_attr->create_flags || init_attr->send_cq)
1397 return -EINVAL;
1398
Eli Cohen2f5ff262017-01-03 23:55:21 +02001399 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001400 if (udata->outlen < min_resp_len)
1401 return -EINVAL;
1402
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001403 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001404 if (udata->inlen < required_cmd_sz) {
1405 mlx5_ib_dbg(dev, "invalid inlen\n");
1406 return -EINVAL;
1407 }
1408
1409 if (udata->inlen > sizeof(ucmd) &&
1410 !ib_is_udata_cleared(udata, sizeof(ucmd),
1411 udata->inlen - sizeof(ucmd))) {
1412 mlx5_ib_dbg(dev, "inlen is not supported\n");
1413 return -EOPNOTSUPP;
1414 }
1415
1416 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1417 mlx5_ib_dbg(dev, "copy failed\n");
1418 return -EFAULT;
1419 }
1420
1421 if (ucmd.comp_mask) {
1422 mlx5_ib_dbg(dev, "invalid comp mask\n");
1423 return -EOPNOTSUPP;
1424 }
1425
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001426 if (ucmd.flags & ~MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
1427 mlx5_ib_dbg(dev, "invalid flags\n");
1428 return -EOPNOTSUPP;
1429 }
1430
1431 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1432 !tunnel_offload_supported(dev->mdev)) {
1433 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001434 return -EOPNOTSUPP;
1435 }
1436
Maor Gottlieb309fa342017-10-19 08:25:56 +03001437 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1438 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1439 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1440 return -EOPNOTSUPP;
1441 }
1442
Yishai Hadas28d61372016-05-23 15:20:56 +03001443 err = ib_copy_to_udata(udata, &resp, min_resp_len);
1444 if (err) {
1445 mlx5_ib_dbg(dev, "copy failed\n");
1446 return -EINVAL;
1447 }
1448
1449 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001450 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001451 if (!in)
1452 return -ENOMEM;
1453
1454 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1455 MLX5_SET(tirc, tirc, disp_type,
1456 MLX5_TIRC_DISP_TYPE_INDIRECT);
1457 MLX5_SET(tirc, tirc, indirect_table,
1458 init_attr->rwq_ind_tbl->ind_tbl_num);
1459 MLX5_SET(tirc, tirc, transport_domain, tdn);
1460
1461 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001462
1463 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1464 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1465
Maor Gottlieb309fa342017-10-19 08:25:56 +03001466 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1467 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1468 else
1469 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1470
Yishai Hadas28d61372016-05-23 15:20:56 +03001471 switch (ucmd.rx_hash_function) {
1472 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1473 {
1474 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1475 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1476
1477 if (len != ucmd.rx_key_len) {
1478 err = -EINVAL;
1479 goto err;
1480 }
1481
1482 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1483 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1484 memcpy(rss_key, ucmd.rx_hash_key, len);
1485 break;
1486 }
1487 default:
1488 err = -EOPNOTSUPP;
1489 goto err;
1490 }
1491
1492 if (!ucmd.rx_hash_fields_mask) {
1493 /* special case when this TIR serves as steering entry without hashing */
1494 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1495 goto create_tir;
1496 err = -EINVAL;
1497 goto err;
1498 }
1499
1500 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1501 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1502 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1503 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1504 err = -EINVAL;
1505 goto err;
1506 }
1507
1508 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1509 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1510 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1511 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1512 MLX5_L3_PROT_TYPE_IPV4);
1513 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1514 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1515 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1516 MLX5_L3_PROT_TYPE_IPV6);
1517
1518 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1519 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) &&
1520 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1521 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))) {
1522 err = -EINVAL;
1523 goto err;
1524 }
1525
1526 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1527 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1528 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1529 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1530 MLX5_L4_PROT_TYPE_TCP);
1531 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1532 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1533 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1534 MLX5_L4_PROT_TYPE_UDP);
1535
1536 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1537 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1538 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1539
1540 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1541 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1542 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1543
1544 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1545 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1546 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1547
1548 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1549 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1550 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1551
1552 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1553
1554create_tir:
1555 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1556
1557 if (err)
1558 goto err;
1559
1560 kvfree(in);
1561 /* qpn is reserved for that QP */
1562 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001563 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001564 return 0;
1565
1566err:
1567 kvfree(in);
1568 return err;
1569}
1570
Eli Cohene126ba92013-07-07 17:25:49 +03001571static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1572 struct ib_qp_init_attr *init_attr,
1573 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1574{
1575 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001576 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001577 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001578 struct mlx5_ib_create_qp_resp resp;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001579 struct mlx5_ib_cq *send_cq;
1580 struct mlx5_ib_cq *recv_cq;
1581 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001582 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001583 struct mlx5_ib_create_qp ucmd;
1584 struct mlx5_ib_qp_base *base;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001585 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001586 u32 *in;
1587 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001588
1589 mutex_init(&qp->mutex);
1590 spin_lock_init(&qp->sq.lock);
1591 spin_lock_init(&qp->rq.lock);
1592
Yishai Hadas28d61372016-05-23 15:20:56 +03001593 if (init_attr->rwq_ind_tbl) {
1594 if (!udata)
1595 return -ENOSYS;
1596
1597 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1598 return err;
1599 }
1600
Eli Cohenf360d882014-04-02 00:10:16 +03001601 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001602 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001603 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1604 return -EINVAL;
1605 } else {
1606 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1607 }
1608 }
1609
Leon Romanovsky051f2632015-12-20 12:16:11 +02001610 if (init_attr->create_flags &
1611 (IB_QP_CREATE_CROSS_CHANNEL |
1612 IB_QP_CREATE_MANAGED_SEND |
1613 IB_QP_CREATE_MANAGED_RECV)) {
1614 if (!MLX5_CAP_GEN(mdev, cd)) {
1615 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1616 return -EINVAL;
1617 }
1618 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1619 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1620 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1621 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1622 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1623 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1624 }
Erez Shitritf0313962016-02-21 16:27:17 +02001625
1626 if (init_attr->qp_type == IB_QPT_UD &&
1627 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1628 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1629 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1630 return -EOPNOTSUPP;
1631 }
1632
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001633 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1634 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1635 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1636 return -EOPNOTSUPP;
1637 }
1638 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1639 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1640 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1641 return -EOPNOTSUPP;
1642 }
1643 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1644 }
1645
Eli Cohene126ba92013-07-07 17:25:49 +03001646 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1647 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1648
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001649 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
1650 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1651 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
1652 (init_attr->qp_type != IB_QPT_RAW_PACKET))
1653 return -EOPNOTSUPP;
1654 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
1655 }
1656
Eli Cohene126ba92013-07-07 17:25:49 +03001657 if (pd && pd->uobject) {
1658 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1659 mlx5_ib_dbg(dev, "copy failed\n");
1660 return -EFAULT;
1661 }
1662
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001663 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1664 &ucmd, udata->inlen, &uidx);
1665 if (err)
1666 return err;
1667
Eli Cohene126ba92013-07-07 17:25:49 +03001668 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1669 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001670 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
1671 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
1672 !tunnel_offload_supported(mdev)) {
1673 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
1674 return -EOPNOTSUPP;
1675 }
1676 qp->tunnel_offload_en = true;
1677 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001678
1679 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
1680 if (init_attr->qp_type != IB_QPT_UD ||
1681 (MLX5_CAP_GEN(dev->mdev, port_type) !=
1682 MLX5_CAP_PORT_TYPE_IB) ||
1683 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
1684 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
1685 return -EOPNOTSUPP;
1686 }
1687
1688 qp->flags |= MLX5_IB_QP_UNDERLAY;
1689 qp->underlay_qpn = init_attr->source_qpn;
1690 }
Eli Cohene126ba92013-07-07 17:25:49 +03001691 } else {
1692 qp->wq_sig = !!wq_signature;
1693 }
1694
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001695 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1696 qp->flags & MLX5_IB_QP_UNDERLAY) ?
1697 &qp->raw_packet_qp.rq.base :
1698 &qp->trans_qp.base;
1699
Eli Cohene126ba92013-07-07 17:25:49 +03001700 qp->has_rq = qp_has_rq(init_attr);
1701 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1702 qp, (pd && pd->uobject) ? &ucmd : NULL);
1703 if (err) {
1704 mlx5_ib_dbg(dev, "err %d\n", err);
1705 return err;
1706 }
1707
1708 if (pd) {
1709 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001710 __u32 max_wqes =
1711 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001712 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1713 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1714 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1715 mlx5_ib_dbg(dev, "invalid rq params\n");
1716 return -EINVAL;
1717 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001718 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001719 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001720 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001721 return -EINVAL;
1722 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001723 if (init_attr->create_flags &
1724 mlx5_ib_create_qp_sqpn_qp1()) {
1725 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1726 return -EINVAL;
1727 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001728 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1729 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001730 if (err)
1731 mlx5_ib_dbg(dev, "err %d\n", err);
1732 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001733 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1734 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001735 if (err)
1736 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001737 }
1738
1739 if (err)
1740 return err;
1741 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001742 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001743 if (!in)
1744 return -ENOMEM;
1745
1746 qp->create_type = MLX5_QP_EMPTY;
1747 }
1748
1749 if (is_sqp(init_attr->qp_type))
1750 qp->port = init_attr->port_num;
1751
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001752 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1753
1754 MLX5_SET(qpc, qpc, st, to_mlx5_st(init_attr->qp_type));
1755 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001756
1757 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001758 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001759 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001760 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1761
Eli Cohene126ba92013-07-07 17:25:49 +03001762
1763 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001764 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001765
Eli Cohenf360d882014-04-02 00:10:16 +03001766 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001767 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001768
Leon Romanovsky051f2632015-12-20 12:16:11 +02001769 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001770 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001771 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001772 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001773 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001774 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001775
Eli Cohene126ba92013-07-07 17:25:49 +03001776 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1777 int rcqe_sz;
1778 int scqe_sz;
1779
1780 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1781 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1782
1783 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001784 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001785 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001786 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001787
1788 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1789 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001790 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001791 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001792 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001793 }
1794 }
1795
1796 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001797 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1798 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001799 }
1800
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001801 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001802
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001803 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001804 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001805 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001806 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001807 if (init_attr->srq &&
1808 init_attr->srq->srq_type == IB_SRQT_TM)
1809 MLX5_SET(qpc, qpc, offload_type,
1810 MLX5_QPC_OFFLOAD_TYPE_RNDV);
1811 }
Eli Cohene126ba92013-07-07 17:25:49 +03001812
1813 /* Set default resources */
1814 switch (init_attr->qp_type) {
1815 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001816 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1817 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1818 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1819 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001820 break;
1821 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001822 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1823 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1824 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001825 break;
1826 default:
1827 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001828 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1829 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001830 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001831 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1832 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001833 }
1834 }
1835
1836 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001837 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001838
1839 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001840 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001841
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001842 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001843
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001844 /* 0xffffff means we ask to work with cqe version 0 */
1845 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001846 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001847
Erez Shitritf0313962016-02-21 16:27:17 +02001848 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1849 if (init_attr->qp_type == IB_QPT_UD &&
1850 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001851 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1852 qp->flags |= MLX5_IB_QP_LSO;
1853 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001854
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001855 if (init_attr->create_flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
1856 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
1857 mlx5_ib_dbg(dev, "scatter end padding is not supported\n");
1858 err = -EOPNOTSUPP;
1859 goto err;
1860 } else if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1861 MLX5_SET(qpc, qpc, end_padding_mode,
1862 MLX5_WQ_END_PAD_MODE_ALIGN);
1863 } else {
1864 qp->flags |= MLX5_IB_QP_PCI_WRITE_END_PADDING;
1865 }
1866 }
1867
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001868 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1869 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001870 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
1871 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
1872 err = create_raw_packet_qp(dev, qp, in, pd);
1873 } else {
1874 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
1875 }
1876
Eli Cohene126ba92013-07-07 17:25:49 +03001877 if (err) {
1878 mlx5_ib_dbg(dev, "create qp failed\n");
1879 goto err_create;
1880 }
1881
Al Viro479163f2014-11-20 08:13:57 +00001882 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001883
majd@mellanox.com19098df2016-01-14 19:13:03 +02001884 base->container_mibqp = qp;
1885 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03001886
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001887 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
1888 &send_cq, &recv_cq);
1889 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1890 mlx5_ib_lock_cqs(send_cq, recv_cq);
1891 /* Maintain device to QPs access, needed for further handling via reset
1892 * flow
1893 */
1894 list_add_tail(&qp->qps_list, &dev->qp_list);
1895 /* Maintain CQ to QPs access, needed for further handling via reset flow
1896 */
1897 if (send_cq)
1898 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
1899 if (recv_cq)
1900 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
1901 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1902 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1903
Eli Cohene126ba92013-07-07 17:25:49 +03001904 return 0;
1905
1906err_create:
1907 if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02001908 destroy_qp_user(dev, pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001909 else if (qp->create_type == MLX5_QP_KERNEL)
1910 destroy_qp_kernel(dev, qp);
1911
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001912err:
Al Viro479163f2014-11-20 08:13:57 +00001913 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001914 return err;
1915}
1916
1917static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1918 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1919{
1920 if (send_cq) {
1921 if (recv_cq) {
1922 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001923 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001924 spin_lock_nested(&recv_cq->lock,
1925 SINGLE_DEPTH_NESTING);
1926 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001927 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001928 __acquire(&recv_cq->lock);
1929 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001930 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001931 spin_lock_nested(&send_cq->lock,
1932 SINGLE_DEPTH_NESTING);
1933 }
1934 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001935 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001936 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001937 }
1938 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001939 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001940 __acquire(&send_cq->lock);
1941 } else {
1942 __acquire(&send_cq->lock);
1943 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001944 }
1945}
1946
1947static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1948 __releases(&send_cq->lock) __releases(&recv_cq->lock)
1949{
1950 if (send_cq) {
1951 if (recv_cq) {
1952 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1953 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001954 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001955 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1956 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001957 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001958 } else {
1959 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001960 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001961 }
1962 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001963 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001964 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001965 }
1966 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001967 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001968 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001969 } else {
1970 __release(&recv_cq->lock);
1971 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001972 }
1973}
1974
1975static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
1976{
1977 return to_mpd(qp->ibqp.pd);
1978}
1979
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001980static void get_cqs(enum ib_qp_type qp_type,
1981 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03001982 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
1983{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001984 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03001985 case IB_QPT_XRC_TGT:
1986 *send_cq = NULL;
1987 *recv_cq = NULL;
1988 break;
1989 case MLX5_IB_QPT_REG_UMR:
1990 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001991 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001992 *recv_cq = NULL;
1993 break;
1994
1995 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02001996 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03001997 case IB_QPT_RC:
1998 case IB_QPT_UC:
1999 case IB_QPT_UD:
2000 case IB_QPT_RAW_IPV6:
2001 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002002 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002003 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2004 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002005 break;
2006
Eli Cohene126ba92013-07-07 17:25:49 +03002007 case IB_QPT_MAX:
2008 default:
2009 *send_cq = NULL;
2010 *recv_cq = NULL;
2011 break;
2012 }
2013}
2014
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002015static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002016 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2017 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002018
Eli Cohene126ba92013-07-07 17:25:49 +03002019static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
2020{
2021 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002022 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002023 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002024 int err;
2025
Yishai Hadas28d61372016-05-23 15:20:56 +03002026 if (qp->ibqp.rwq_ind_tbl) {
2027 destroy_rss_raw_qp_tir(dev, qp);
2028 return;
2029 }
2030
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002031 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2032 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002033 &qp->raw_packet_qp.rq.base :
2034 &qp->trans_qp.base;
2035
Haggai Eran6aec21f2014-12-11 17:04:23 +02002036 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002037 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2038 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002039 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002040 MLX5_CMD_OP_2RST_QP, 0,
2041 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002042 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03002043 struct mlx5_modify_raw_qp_param raw_qp_param = {
2044 .operation = MLX5_CMD_OP_2RST_QP
2045 };
2046
Aviv Heller13eab212016-09-18 20:48:04 +03002047 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002048 }
2049 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002050 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002051 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002052 }
Eli Cohene126ba92013-07-07 17:25:49 +03002053
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002054 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2055 &send_cq, &recv_cq);
2056
2057 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2058 mlx5_ib_lock_cqs(send_cq, recv_cq);
2059 /* del from lists under both locks above to protect reset flow paths */
2060 list_del(&qp->qps_list);
2061 if (send_cq)
2062 list_del(&qp->cq_send_list);
2063
2064 if (recv_cq)
2065 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002066
2067 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002068 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002069 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2070 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002071 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2072 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002073 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002074 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2075 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002076
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002077 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2078 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002079 destroy_raw_packet_qp(dev, qp);
2080 } else {
2081 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
2082 if (err)
2083 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2084 base->mqp.qpn);
2085 }
Eli Cohene126ba92013-07-07 17:25:49 +03002086
Eli Cohene126ba92013-07-07 17:25:49 +03002087 if (qp->create_type == MLX5_QP_KERNEL)
2088 destroy_qp_kernel(dev, qp);
2089 else if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02002090 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002091}
2092
2093static const char *ib_qp_type_str(enum ib_qp_type type)
2094{
2095 switch (type) {
2096 case IB_QPT_SMI:
2097 return "IB_QPT_SMI";
2098 case IB_QPT_GSI:
2099 return "IB_QPT_GSI";
2100 case IB_QPT_RC:
2101 return "IB_QPT_RC";
2102 case IB_QPT_UC:
2103 return "IB_QPT_UC";
2104 case IB_QPT_UD:
2105 return "IB_QPT_UD";
2106 case IB_QPT_RAW_IPV6:
2107 return "IB_QPT_RAW_IPV6";
2108 case IB_QPT_RAW_ETHERTYPE:
2109 return "IB_QPT_RAW_ETHERTYPE";
2110 case IB_QPT_XRC_INI:
2111 return "IB_QPT_XRC_INI";
2112 case IB_QPT_XRC_TGT:
2113 return "IB_QPT_XRC_TGT";
2114 case IB_QPT_RAW_PACKET:
2115 return "IB_QPT_RAW_PACKET";
2116 case MLX5_IB_QPT_REG_UMR:
2117 return "MLX5_IB_QPT_REG_UMR";
2118 case IB_QPT_MAX:
2119 default:
2120 return "Invalid QP type";
2121 }
2122}
2123
2124struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
2125 struct ib_qp_init_attr *init_attr,
2126 struct ib_udata *udata)
2127{
2128 struct mlx5_ib_dev *dev;
2129 struct mlx5_ib_qp *qp;
2130 u16 xrcdn = 0;
2131 int err;
2132
2133 if (pd) {
2134 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002135
2136 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2137 if (!pd->uobject) {
2138 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2139 return ERR_PTR(-EINVAL);
2140 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2141 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2142 return ERR_PTR(-EINVAL);
2143 }
2144 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002145 } else {
2146 /* being cautious here */
2147 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2148 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2149 pr_warn("%s: no PD for transport %s\n", __func__,
2150 ib_qp_type_str(init_attr->qp_type));
2151 return ERR_PTR(-EINVAL);
2152 }
2153 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002154 }
2155
2156 switch (init_attr->qp_type) {
2157 case IB_QPT_XRC_TGT:
2158 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002159 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002160 mlx5_ib_dbg(dev, "XRC not supported\n");
2161 return ERR_PTR(-ENOSYS);
2162 }
2163 init_attr->recv_cq = NULL;
2164 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2165 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2166 init_attr->send_cq = NULL;
2167 }
2168
2169 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002170 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002171 case IB_QPT_RC:
2172 case IB_QPT_UC:
2173 case IB_QPT_UD:
2174 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002175 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002176 case MLX5_IB_QPT_REG_UMR:
2177 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2178 if (!qp)
2179 return ERR_PTR(-ENOMEM);
2180
2181 err = create_qp_common(dev, pd, init_attr, udata, qp);
2182 if (err) {
2183 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2184 kfree(qp);
2185 return ERR_PTR(err);
2186 }
2187
2188 if (is_qp0(init_attr->qp_type))
2189 qp->ibqp.qp_num = 0;
2190 else if (is_qp1(init_attr->qp_type))
2191 qp->ibqp.qp_num = 1;
2192 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002193 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002194
2195 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 +02002196 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002197 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2198 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002199
majd@mellanox.com19098df2016-01-14 19:13:03 +02002200 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002201
2202 break;
2203
Haggai Erand16e91d2016-02-29 15:45:05 +02002204 case IB_QPT_GSI:
2205 return mlx5_ib_gsi_create_qp(pd, init_attr);
2206
Eli Cohene126ba92013-07-07 17:25:49 +03002207 case IB_QPT_RAW_IPV6:
2208 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002209 case IB_QPT_MAX:
2210 default:
2211 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2212 init_attr->qp_type);
2213 /* Don't support raw QPs */
2214 return ERR_PTR(-EINVAL);
2215 }
2216
2217 return &qp->ibqp;
2218}
2219
2220int mlx5_ib_destroy_qp(struct ib_qp *qp)
2221{
2222 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2223 struct mlx5_ib_qp *mqp = to_mqp(qp);
2224
Haggai Erand16e91d2016-02-29 15:45:05 +02002225 if (unlikely(qp->qp_type == IB_QPT_GSI))
2226 return mlx5_ib_gsi_destroy_qp(qp);
2227
Eli Cohene126ba92013-07-07 17:25:49 +03002228 destroy_qp_common(dev, mqp);
2229
2230 kfree(mqp);
2231
2232 return 0;
2233}
2234
2235static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2236 int attr_mask)
2237{
2238 u32 hw_access_flags = 0;
2239 u8 dest_rd_atomic;
2240 u32 access_flags;
2241
2242 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2243 dest_rd_atomic = attr->max_dest_rd_atomic;
2244 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002245 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002246
2247 if (attr_mask & IB_QP_ACCESS_FLAGS)
2248 access_flags = attr->qp_access_flags;
2249 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002250 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002251
2252 if (!dest_rd_atomic)
2253 access_flags &= IB_ACCESS_REMOTE_WRITE;
2254
2255 if (access_flags & IB_ACCESS_REMOTE_READ)
2256 hw_access_flags |= MLX5_QP_BIT_RRE;
2257 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2258 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2259 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2260 hw_access_flags |= MLX5_QP_BIT_RWE;
2261
2262 return cpu_to_be32(hw_access_flags);
2263}
2264
2265enum {
2266 MLX5_PATH_FLAG_FL = 1 << 0,
2267 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2268 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2269};
2270
2271static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2272{
2273 if (rate == IB_RATE_PORT_CURRENT) {
2274 return 0;
2275 } else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
2276 return -EINVAL;
2277 } else {
2278 while (rate != IB_RATE_2_5_GBPS &&
2279 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
Saeed Mahameed938fe832015-05-28 22:28:41 +03002280 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
Eli Cohene126ba92013-07-07 17:25:49 +03002281 --rate;
2282 }
2283
2284 return rate + MLX5_STAT_RATE_OFFSET;
2285}
2286
majd@mellanox.com75850d02016-01-14 19:13:06 +02002287static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
2288 struct mlx5_ib_sq *sq, u8 sl)
2289{
2290 void *in;
2291 void *tisc;
2292 int inlen;
2293 int err;
2294
2295 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002296 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002297 if (!in)
2298 return -ENOMEM;
2299
2300 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
2301
2302 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2303 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2304
2305 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2306
2307 kvfree(in);
2308
2309 return err;
2310}
2311
Aviv Heller13eab212016-09-18 20:48:04 +03002312static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
2313 struct mlx5_ib_sq *sq, u8 tx_affinity)
2314{
2315 void *in;
2316 void *tisc;
2317 int inlen;
2318 int err;
2319
2320 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002321 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002322 if (!in)
2323 return -ENOMEM;
2324
2325 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
2326
2327 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2328 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2329
2330 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2331
2332 kvfree(in);
2333
2334 return err;
2335}
2336
majd@mellanox.com75850d02016-01-14 19:13:06 +02002337static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002338 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002339 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002340 u32 path_flags, const struct ib_qp_attr *attr,
2341 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002342{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002343 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002344 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002345 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002346 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2347 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002348
Eli Cohene126ba92013-07-07 17:25:49 +03002349 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002350 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2351 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002352
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002353 if (ah_flags & IB_AH_GRH) {
2354 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002355 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002356 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002357 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002358 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002359 return -EINVAL;
2360 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002361 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002362
2363 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002364 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002365 return -EINVAL;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002366 err = mlx5_get_roce_gid_type(dev, port, grh->sgid_index,
Majd Dibbinyed884512017-01-18 14:10:35 +02002367 &gid_type);
2368 if (err)
2369 return err;
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002370 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Majd Dibbiny2b621852017-10-30 14:23:14 +02002371 if (qp->ibqp.qp_type == IB_QPT_RC ||
2372 qp->ibqp.qp_type == IB_QPT_UC ||
2373 qp->ibqp.qp_type == IB_QPT_XRC_INI ||
2374 qp->ibqp.qp_type == IB_QPT_XRC_TGT)
2375 path->udp_sport = mlx5_get_roce_udp_sport(dev, port,
2376 grh->sgid_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002377 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Majd Dibbinyed884512017-01-18 14:10:35 +02002378 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002379 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002380 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002381 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2382 path->fl_free_ar |=
2383 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002384 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2385 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2386 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002387 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002388 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002389 }
2390
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002391 if (ah_flags & IB_AH_GRH) {
2392 path->mgid_index = grh->sgid_index;
2393 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002394 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002395 cpu_to_be32((grh->traffic_class << 20) |
2396 (grh->flow_label));
2397 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002398 }
2399
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002400 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002401 if (err < 0)
2402 return err;
2403 path->static_rate = err;
2404 path->port = port;
2405
Eli Cohene126ba92013-07-07 17:25:49 +03002406 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002407 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002408
majd@mellanox.com75850d02016-01-14 19:13:06 +02002409 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2410 return modify_raw_packet_eth_prio(dev->mdev,
2411 &qp->raw_packet_qp.sq,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002412 sl & 0xf);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002413
Eli Cohene126ba92013-07-07 17:25:49 +03002414 return 0;
2415}
2416
2417static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2418 [MLX5_QP_STATE_INIT] = {
2419 [MLX5_QP_STATE_INIT] = {
2420 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2421 MLX5_QP_OPTPAR_RAE |
2422 MLX5_QP_OPTPAR_RWE |
2423 MLX5_QP_OPTPAR_PKEY_INDEX |
2424 MLX5_QP_OPTPAR_PRI_PORT,
2425 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2426 MLX5_QP_OPTPAR_PKEY_INDEX |
2427 MLX5_QP_OPTPAR_PRI_PORT,
2428 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2429 MLX5_QP_OPTPAR_Q_KEY |
2430 MLX5_QP_OPTPAR_PRI_PORT,
2431 },
2432 [MLX5_QP_STATE_RTR] = {
2433 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2434 MLX5_QP_OPTPAR_RRE |
2435 MLX5_QP_OPTPAR_RAE |
2436 MLX5_QP_OPTPAR_RWE |
2437 MLX5_QP_OPTPAR_PKEY_INDEX,
2438 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2439 MLX5_QP_OPTPAR_RWE |
2440 MLX5_QP_OPTPAR_PKEY_INDEX,
2441 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2442 MLX5_QP_OPTPAR_Q_KEY,
2443 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2444 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002445 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2446 MLX5_QP_OPTPAR_RRE |
2447 MLX5_QP_OPTPAR_RAE |
2448 MLX5_QP_OPTPAR_RWE |
2449 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002450 },
2451 },
2452 [MLX5_QP_STATE_RTR] = {
2453 [MLX5_QP_STATE_RTS] = {
2454 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2455 MLX5_QP_OPTPAR_RRE |
2456 MLX5_QP_OPTPAR_RAE |
2457 MLX5_QP_OPTPAR_RWE |
2458 MLX5_QP_OPTPAR_PM_STATE |
2459 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2460 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2461 MLX5_QP_OPTPAR_RWE |
2462 MLX5_QP_OPTPAR_PM_STATE,
2463 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2464 },
2465 },
2466 [MLX5_QP_STATE_RTS] = {
2467 [MLX5_QP_STATE_RTS] = {
2468 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2469 MLX5_QP_OPTPAR_RAE |
2470 MLX5_QP_OPTPAR_RWE |
2471 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002472 MLX5_QP_OPTPAR_PM_STATE |
2473 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002474 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03002475 MLX5_QP_OPTPAR_PM_STATE |
2476 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002477 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
2478 MLX5_QP_OPTPAR_SRQN |
2479 MLX5_QP_OPTPAR_CQN_RCV,
2480 },
2481 },
2482 [MLX5_QP_STATE_SQER] = {
2483 [MLX5_QP_STATE_RTS] = {
2484 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2485 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002486 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002487 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2488 MLX5_QP_OPTPAR_RWE |
2489 MLX5_QP_OPTPAR_RAE |
2490 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002491 },
2492 },
2493};
2494
2495static int ib_nr_to_mlx5_nr(int ib_mask)
2496{
2497 switch (ib_mask) {
2498 case IB_QP_STATE:
2499 return 0;
2500 case IB_QP_CUR_STATE:
2501 return 0;
2502 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2503 return 0;
2504 case IB_QP_ACCESS_FLAGS:
2505 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2506 MLX5_QP_OPTPAR_RAE;
2507 case IB_QP_PKEY_INDEX:
2508 return MLX5_QP_OPTPAR_PKEY_INDEX;
2509 case IB_QP_PORT:
2510 return MLX5_QP_OPTPAR_PRI_PORT;
2511 case IB_QP_QKEY:
2512 return MLX5_QP_OPTPAR_Q_KEY;
2513 case IB_QP_AV:
2514 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2515 MLX5_QP_OPTPAR_PRI_PORT;
2516 case IB_QP_PATH_MTU:
2517 return 0;
2518 case IB_QP_TIMEOUT:
2519 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2520 case IB_QP_RETRY_CNT:
2521 return MLX5_QP_OPTPAR_RETRY_COUNT;
2522 case IB_QP_RNR_RETRY:
2523 return MLX5_QP_OPTPAR_RNR_RETRY;
2524 case IB_QP_RQ_PSN:
2525 return 0;
2526 case IB_QP_MAX_QP_RD_ATOMIC:
2527 return MLX5_QP_OPTPAR_SRA_MAX;
2528 case IB_QP_ALT_PATH:
2529 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2530 case IB_QP_MIN_RNR_TIMER:
2531 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2532 case IB_QP_SQ_PSN:
2533 return 0;
2534 case IB_QP_MAX_DEST_RD_ATOMIC:
2535 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2536 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2537 case IB_QP_PATH_MIG_STATE:
2538 return MLX5_QP_OPTPAR_PM_STATE;
2539 case IB_QP_CAP:
2540 return 0;
2541 case IB_QP_DEST_QPN:
2542 return 0;
2543 }
2544 return 0;
2545}
2546
2547static int ib_mask_to_mlx5_opt(int ib_mask)
2548{
2549 int result = 0;
2550 int i;
2551
2552 for (i = 0; i < 8 * sizeof(int); i++) {
2553 if ((1 << i) & ib_mask)
2554 result |= ib_nr_to_mlx5_nr(1 << i);
2555 }
2556
2557 return result;
2558}
2559
Alex Veskereb49ab02016-08-28 12:25:53 +03002560static int modify_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
2561 struct mlx5_ib_rq *rq, int new_state,
2562 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002563{
2564 void *in;
2565 void *rqc;
2566 int inlen;
2567 int err;
2568
2569 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002570 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002571 if (!in)
2572 return -ENOMEM;
2573
2574 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
2575
2576 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2577 MLX5_SET(rqc, rqc, state, new_state);
2578
Alex Veskereb49ab02016-08-28 12:25:53 +03002579 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2580 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2581 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02002582 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03002583 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2584 } else
2585 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2586 dev->ib_dev.name);
2587 }
2588
2589 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002590 if (err)
2591 goto out;
2592
2593 rq->state = new_state;
2594
2595out:
2596 kvfree(in);
2597 return err;
2598}
2599
2600static int modify_raw_packet_qp_sq(struct mlx5_core_dev *dev,
Bodong Wang7d29f342016-12-01 13:43:16 +02002601 struct mlx5_ib_sq *sq,
2602 int new_state,
2603 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002604{
Bodong Wang7d29f342016-12-01 13:43:16 +02002605 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
2606 u32 old_rate = ibqp->rate_limit;
2607 u32 new_rate = old_rate;
2608 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002609 void *in;
2610 void *sqc;
2611 int inlen;
2612 int err;
2613
2614 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002615 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002616 if (!in)
2617 return -ENOMEM;
2618
2619 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2620
2621 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2622 MLX5_SET(sqc, sqc, state, new_state);
2623
Bodong Wang7d29f342016-12-01 13:43:16 +02002624 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2625 if (new_state != MLX5_SQC_STATE_RDY)
2626 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2627 __func__);
2628 else
2629 new_rate = raw_qp_param->rate_limit;
2630 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002631
Bodong Wang7d29f342016-12-01 13:43:16 +02002632 if (old_rate != new_rate) {
2633 if (new_rate) {
2634 err = mlx5_rl_add_rate(dev, new_rate, &rl_index);
2635 if (err) {
2636 pr_err("Failed configuring rate %u: %d\n",
2637 new_rate, err);
2638 goto out;
2639 }
2640 }
2641
2642 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
2643 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2644 }
2645
2646 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2647 if (err) {
2648 /* Remove new rate from table if failed */
2649 if (new_rate &&
2650 old_rate != new_rate)
2651 mlx5_rl_remove_rate(dev, new_rate);
2652 goto out;
2653 }
2654
2655 /* Only remove the old rate after new rate was set */
2656 if ((old_rate &&
2657 (old_rate != new_rate)) ||
2658 (new_state != MLX5_SQC_STATE_RDY))
2659 mlx5_rl_remove_rate(dev, old_rate);
2660
2661 ibqp->rate_limit = new_rate;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002662 sq->state = new_state;
2663
2664out:
2665 kvfree(in);
2666 return err;
2667}
2668
2669static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002670 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2671 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002672{
2673 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2674 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2675 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002676 int modify_rq = !!qp->rq.wqe_cnt;
2677 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002678 int rq_state;
2679 int sq_state;
2680 int err;
2681
Alex Vesker0680efa2016-08-28 12:25:52 +03002682 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002683 case MLX5_CMD_OP_RST2INIT_QP:
2684 rq_state = MLX5_RQC_STATE_RDY;
2685 sq_state = MLX5_SQC_STATE_RDY;
2686 break;
2687 case MLX5_CMD_OP_2ERR_QP:
2688 rq_state = MLX5_RQC_STATE_ERR;
2689 sq_state = MLX5_SQC_STATE_ERR;
2690 break;
2691 case MLX5_CMD_OP_2RST_QP:
2692 rq_state = MLX5_RQC_STATE_RST;
2693 sq_state = MLX5_SQC_STATE_RST;
2694 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002695 case MLX5_CMD_OP_RTR2RTS_QP:
2696 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002697 if (raw_qp_param->set_mask ==
2698 MLX5_RAW_QP_RATE_LIMIT) {
2699 modify_rq = 0;
2700 sq_state = sq->state;
2701 } else {
2702 return raw_qp_param->set_mask ? -EINVAL : 0;
2703 }
2704 break;
2705 case MLX5_CMD_OP_INIT2INIT_QP:
2706 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002707 if (raw_qp_param->set_mask)
2708 return -EINVAL;
2709 else
2710 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002711 default:
2712 WARN_ON(1);
2713 return -EINVAL;
2714 }
2715
Bodong Wang7d29f342016-12-01 13:43:16 +02002716 if (modify_rq) {
2717 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002718 if (err)
2719 return err;
2720 }
2721
Bodong Wang7d29f342016-12-01 13:43:16 +02002722 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03002723 if (tx_affinity) {
2724 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
2725 tx_affinity);
2726 if (err)
2727 return err;
2728 }
2729
Bodong Wang7d29f342016-12-01 13:43:16 +02002730 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state, raw_qp_param);
Aviv Heller13eab212016-09-18 20:48:04 +03002731 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002732
2733 return 0;
2734}
2735
Eli Cohene126ba92013-07-07 17:25:49 +03002736static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
2737 const struct ib_qp_attr *attr, int attr_mask,
2738 enum ib_qp_state cur_state, enum ib_qp_state new_state)
2739{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002740 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
2741 [MLX5_QP_STATE_RST] = {
2742 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2743 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2744 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
2745 },
2746 [MLX5_QP_STATE_INIT] = {
2747 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2748 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2749 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
2750 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
2751 },
2752 [MLX5_QP_STATE_RTR] = {
2753 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2754 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2755 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
2756 },
2757 [MLX5_QP_STATE_RTS] = {
2758 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2759 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2760 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
2761 },
2762 [MLX5_QP_STATE_SQD] = {
2763 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2764 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2765 },
2766 [MLX5_QP_STATE_SQER] = {
2767 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2768 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2769 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
2770 },
2771 [MLX5_QP_STATE_ERR] = {
2772 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2773 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2774 }
2775 };
2776
Eli Cohene126ba92013-07-07 17:25:49 +03002777 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2778 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02002779 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03002780 struct mlx5_ib_cq *send_cq, *recv_cq;
2781 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03002782 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03002783 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002784 enum mlx5_qp_state mlx5_cur, mlx5_new;
2785 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03002786 int mlx5_st;
2787 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002788 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03002789 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002790
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002791 context = kzalloc(sizeof(*context), GFP_KERNEL);
2792 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03002793 return -ENOMEM;
2794
Eli Cohene126ba92013-07-07 17:25:49 +03002795 err = to_mlx5_st(ibqp->qp_type);
Haggai Eran158abf82016-02-29 15:45:04 +02002796 if (err < 0) {
2797 mlx5_ib_dbg(dev, "unsupported qp type %d\n", ibqp->qp_type);
Eli Cohene126ba92013-07-07 17:25:49 +03002798 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002799 }
Eli Cohene126ba92013-07-07 17:25:49 +03002800
2801 context->flags = cpu_to_be32(err << 16);
2802
2803 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
2804 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2805 } else {
2806 switch (attr->path_mig_state) {
2807 case IB_MIG_MIGRATED:
2808 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2809 break;
2810 case IB_MIG_REARM:
2811 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
2812 break;
2813 case IB_MIG_ARMED:
2814 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
2815 break;
2816 }
2817 }
2818
Aviv Heller13eab212016-09-18 20:48:04 +03002819 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2820 if ((ibqp->qp_type == IB_QPT_RC) ||
2821 (ibqp->qp_type == IB_QPT_UD &&
2822 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
2823 (ibqp->qp_type == IB_QPT_UC) ||
2824 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
2825 (ibqp->qp_type == IB_QPT_XRC_INI) ||
2826 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
2827 if (mlx5_lag_is_active(dev->mdev)) {
2828 tx_affinity = (unsigned int)atomic_add_return(1,
2829 &dev->roce.next_port) %
2830 MLX5_MAX_PORTS + 1;
2831 context->flags |= cpu_to_be32(tx_affinity << 24);
2832 }
2833 }
2834 }
2835
Haggai Erand16e91d2016-02-29 15:45:05 +02002836 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002837 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002838 } else if ((ibqp->qp_type == IB_QPT_UD &&
2839 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03002840 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
2841 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
2842 } else if (attr_mask & IB_QP_PATH_MTU) {
2843 if (attr->path_mtu < IB_MTU_256 ||
2844 attr->path_mtu > IB_MTU_4096) {
2845 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
2846 err = -EINVAL;
2847 goto out;
2848 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002849 context->mtu_msgmax = (attr->path_mtu << 5) |
2850 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03002851 }
2852
2853 if (attr_mask & IB_QP_DEST_QPN)
2854 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
2855
2856 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002857 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002858
2859 /* todo implement counter_index functionality */
2860
2861 if (is_sqp(ibqp->qp_type))
2862 context->pri_path.port = qp->port;
2863
2864 if (attr_mask & IB_QP_PORT)
2865 context->pri_path.port = attr->port_num;
2866
2867 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002868 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03002869 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002870 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03002871 if (err)
2872 goto out;
2873 }
2874
2875 if (attr_mask & IB_QP_TIMEOUT)
2876 context->pri_path.ackto_lt |= attr->timeout << 3;
2877
2878 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002879 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
2880 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002881 attr->alt_port_num,
2882 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
2883 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03002884 if (err)
2885 goto out;
2886 }
2887
2888 pd = get_pd(qp);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002889 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2890 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002891
2892 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
2893 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
2894 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
2895 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
2896
2897 if (attr_mask & IB_QP_RNR_RETRY)
2898 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
2899
2900 if (attr_mask & IB_QP_RETRY_CNT)
2901 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
2902
2903 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2904 if (attr->max_rd_atomic)
2905 context->params1 |=
2906 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
2907 }
2908
2909 if (attr_mask & IB_QP_SQ_PSN)
2910 context->next_send_psn = cpu_to_be32(attr->sq_psn);
2911
2912 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2913 if (attr->max_dest_rd_atomic)
2914 context->params2 |=
2915 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
2916 }
2917
2918 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
2919 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
2920
2921 if (attr_mask & IB_QP_MIN_RNR_TIMER)
2922 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
2923
2924 if (attr_mask & IB_QP_RQ_PSN)
2925 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
2926
2927 if (attr_mask & IB_QP_QKEY)
2928 context->qkey = cpu_to_be32(attr->qkey);
2929
2930 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2931 context->db_rec_addr = cpu_to_be64(qp->db.dma);
2932
Mark Bloch0837e862016-06-17 15:10:55 +03002933 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2934 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
2935 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002936
2937 /* Underlay port should be used - index 0 function per port */
2938 if (qp->flags & MLX5_IB_QP_UNDERLAY)
2939 port_num = 0;
2940
Alex Veskereb49ab02016-08-28 12:25:53 +03002941 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03002942 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03002943 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03002944 }
2945
Eli Cohene126ba92013-07-07 17:25:49 +03002946 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2947 context->sq_crq_size |= cpu_to_be16(1 << 4);
2948
Haggai Eranb11a4f92016-02-29 15:45:03 +02002949 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
2950 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03002951
2952 mlx5_cur = to_mlx5_state(cur_state);
2953 mlx5_new = to_mlx5_state(new_state);
2954 mlx5_st = to_mlx5_st(ibqp->qp_type);
Eli Cohen07c91132013-10-24 12:01:01 +03002955 if (mlx5_st < 0)
Eli Cohene126ba92013-07-07 17:25:49 +03002956 goto out;
2957
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002958 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
2959 !optab[mlx5_cur][mlx5_new])
2960 goto out;
2961
2962 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03002963 optpar = ib_mask_to_mlx5_opt(attr_mask);
2964 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002965
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002966 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2967 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03002968 struct mlx5_modify_raw_qp_param raw_qp_param = {};
2969
2970 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03002971 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03002972 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03002973 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
2974 }
Bodong Wang7d29f342016-12-01 13:43:16 +02002975
2976 if (attr_mask & IB_QP_RATE_LIMIT) {
2977 raw_qp_param.rate_limit = attr->rate_limit;
2978 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
2979 }
2980
Aviv Heller13eab212016-09-18 20:48:04 +03002981 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03002982 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002983 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002984 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03002985 }
2986
Eli Cohene126ba92013-07-07 17:25:49 +03002987 if (err)
2988 goto out;
2989
2990 qp->state = new_state;
2991
2992 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002993 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002994 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002995 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03002996 if (attr_mask & IB_QP_PORT)
2997 qp->port = attr->port_num;
2998 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002999 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03003000
3001 /*
3002 * If we moved a kernel QP to RESET, clean up all old CQ
3003 * entries and reinitialize the QP.
3004 */
3005 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02003006 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03003007 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
3008 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02003009 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003010
3011 qp->rq.head = 0;
3012 qp->rq.tail = 0;
3013 qp->sq.head = 0;
3014 qp->sq.tail = 0;
3015 qp->sq.cur_post = 0;
3016 qp->sq.last_poll = 0;
3017 qp->db.db[MLX5_RCV_DBR] = 0;
3018 qp->db.db[MLX5_SND_DBR] = 0;
3019 }
3020
3021out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03003022 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03003023 return err;
3024}
3025
3026int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3027 int attr_mask, struct ib_udata *udata)
3028{
3029 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3030 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Haggai Erand16e91d2016-02-29 15:45:05 +02003031 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03003032 enum ib_qp_state cur_state, new_state;
3033 int err = -EINVAL;
3034 int port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02003035 enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
Eli Cohene126ba92013-07-07 17:25:49 +03003036
Yishai Hadas28d61372016-05-23 15:20:56 +03003037 if (ibqp->rwq_ind_tbl)
3038 return -ENOSYS;
3039
Haggai Erand16e91d2016-02-29 15:45:05 +02003040 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3041 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
3042
3043 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
3044 IB_QPT_GSI : ibqp->qp_type;
3045
Eli Cohene126ba92013-07-07 17:25:49 +03003046 mutex_lock(&qp->mutex);
3047
3048 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
3049 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
3050
Achiad Shochat2811ba52015-12-23 18:47:24 +02003051 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
3052 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
3053 ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
3054 }
3055
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003056 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
3057 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
3058 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
3059 attr_mask);
3060 goto out;
3061 }
3062 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Haggai Erand16e91d2016-02-29 15:45:05 +02003063 !ib_modify_qp_is_ok(cur_state, new_state, qp_type, attr_mask, ll)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003064 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3065 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003066 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003067 }
Eli Cohene126ba92013-07-07 17:25:49 +03003068
3069 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003070 (attr->port_num == 0 ||
Haggai Eran158abf82016-02-29 15:45:04 +02003071 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports))) {
3072 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3073 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003074 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003075 }
Eli Cohene126ba92013-07-07 17:25:49 +03003076
3077 if (attr_mask & IB_QP_PKEY_INDEX) {
3078 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003079 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02003080 dev->mdev->port_caps[port - 1].pkey_table_len) {
3081 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
3082 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003083 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003084 }
Eli Cohene126ba92013-07-07 17:25:49 +03003085 }
3086
3087 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003088 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003089 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
3090 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
3091 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003092 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003093 }
Eli Cohene126ba92013-07-07 17:25:49 +03003094
3095 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003096 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003097 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
3098 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
3099 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003100 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003101 }
Eli Cohene126ba92013-07-07 17:25:49 +03003102
3103 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
3104 err = 0;
3105 goto out;
3106 }
3107
3108 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
3109
3110out:
3111 mutex_unlock(&qp->mutex);
3112 return err;
3113}
3114
3115static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
3116{
3117 struct mlx5_ib_cq *cq;
3118 unsigned cur;
3119
3120 cur = wq->head - wq->tail;
3121 if (likely(cur + nreq < wq->max_post))
3122 return 0;
3123
3124 cq = to_mcq(ib_cq);
3125 spin_lock(&cq->lock);
3126 cur = wq->head - wq->tail;
3127 spin_unlock(&cq->lock);
3128
3129 return cur + nreq >= wq->max_post;
3130}
3131
3132static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
3133 u64 remote_addr, u32 rkey)
3134{
3135 rseg->raddr = cpu_to_be64(remote_addr);
3136 rseg->rkey = cpu_to_be32(rkey);
3137 rseg->reserved = 0;
3138}
3139
Erez Shitritf0313962016-02-21 16:27:17 +02003140static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
3141 struct ib_send_wr *wr, void *qend,
3142 struct mlx5_ib_qp *qp, int *size)
3143{
3144 void *seg = eseg;
3145
3146 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
3147
3148 if (wr->send_flags & IB_SEND_IP_CSUM)
3149 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
3150 MLX5_ETH_WQE_L4_CSUM;
3151
3152 seg += sizeof(struct mlx5_wqe_eth_seg);
3153 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3154
3155 if (wr->opcode == IB_WR_LSO) {
3156 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003157 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr.start);
Erez Shitritf0313962016-02-21 16:27:17 +02003158 u64 left, leftlen, copysz;
3159 void *pdata = ud_wr->header;
3160
3161 left = ud_wr->hlen;
3162 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003163 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02003164
3165 /*
3166 * check if there is space till the end of queue, if yes,
3167 * copy all in one shot, otherwise copy till the end of queue,
3168 * rollback and than the copy the left
3169 */
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003170 leftlen = qend - (void *)eseg->inline_hdr.start;
Erez Shitritf0313962016-02-21 16:27:17 +02003171 copysz = min_t(u64, leftlen, left);
3172
3173 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3174
3175 if (likely(copysz > size_of_inl_hdr_start)) {
3176 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3177 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3178 }
3179
3180 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3181 seg = mlx5_get_send_wqe(qp, 0);
3182 left -= copysz;
3183 pdata += copysz;
3184 memcpy(seg, pdata, left);
3185 seg += ALIGN(left, 16);
3186 *size += ALIGN(left, 16) / 16;
3187 }
3188 }
3189
3190 return seg;
3191}
3192
Eli Cohene126ba92013-07-07 17:25:49 +03003193static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
3194 struct ib_send_wr *wr)
3195{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003196 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3197 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3198 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003199}
3200
3201static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3202{
3203 dseg->byte_count = cpu_to_be32(sg->length);
3204 dseg->lkey = cpu_to_be32(sg->lkey);
3205 dseg->addr = cpu_to_be64(sg->addr);
3206}
3207
Artemy Kovalyov31616252017-01-02 11:37:42 +02003208static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03003209{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003210 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
3211 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03003212}
3213
3214static __be64 frwr_mkey_mask(void)
3215{
3216 u64 result;
3217
3218 result = MLX5_MKEY_MASK_LEN |
3219 MLX5_MKEY_MASK_PAGE_SIZE |
3220 MLX5_MKEY_MASK_START_ADDR |
3221 MLX5_MKEY_MASK_EN_RINVAL |
3222 MLX5_MKEY_MASK_KEY |
3223 MLX5_MKEY_MASK_LR |
3224 MLX5_MKEY_MASK_LW |
3225 MLX5_MKEY_MASK_RR |
3226 MLX5_MKEY_MASK_RW |
3227 MLX5_MKEY_MASK_A |
3228 MLX5_MKEY_MASK_SMALL_FENCE |
3229 MLX5_MKEY_MASK_FREE;
3230
3231 return cpu_to_be64(result);
3232}
3233
Sagi Grimberge6631812014-02-23 14:19:11 +02003234static __be64 sig_mkey_mask(void)
3235{
3236 u64 result;
3237
3238 result = MLX5_MKEY_MASK_LEN |
3239 MLX5_MKEY_MASK_PAGE_SIZE |
3240 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003241 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003242 MLX5_MKEY_MASK_EN_RINVAL |
3243 MLX5_MKEY_MASK_KEY |
3244 MLX5_MKEY_MASK_LR |
3245 MLX5_MKEY_MASK_LW |
3246 MLX5_MKEY_MASK_RR |
3247 MLX5_MKEY_MASK_RW |
3248 MLX5_MKEY_MASK_SMALL_FENCE |
3249 MLX5_MKEY_MASK_FREE |
3250 MLX5_MKEY_MASK_BSF_EN;
3251
3252 return cpu_to_be64(result);
3253}
3254
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003255static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003256 struct mlx5_ib_mr *mr)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003257{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003258 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003259
3260 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003261
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003262 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003263 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003264 umr->mkey_mask = frwr_mkey_mask();
3265}
3266
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003267static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003268{
3269 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003270 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003271 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003272}
3273
Artemy Kovalyov31616252017-01-02 11:37:42 +02003274static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003275{
3276 u64 result;
3277
Artemy Kovalyov31616252017-01-02 11:37:42 +02003278 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02003279 MLX5_MKEY_MASK_FREE;
3280
3281 return cpu_to_be64(result);
3282}
3283
Artemy Kovalyov31616252017-01-02 11:37:42 +02003284static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003285{
3286 u64 result;
3287
3288 result = MLX5_MKEY_MASK_FREE;
3289
3290 return cpu_to_be64(result);
3291}
3292
Noa Osherovich56e11d62016-02-29 16:46:51 +02003293static __be64 get_umr_update_translation_mask(void)
3294{
3295 u64 result;
3296
3297 result = MLX5_MKEY_MASK_LEN |
3298 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003299 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003300
3301 return cpu_to_be64(result);
3302}
3303
Artemy Kovalyov31616252017-01-02 11:37:42 +02003304static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02003305{
3306 u64 result;
3307
Artemy Kovalyov31616252017-01-02 11:37:42 +02003308 result = MLX5_MKEY_MASK_LR |
3309 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003310 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003311 MLX5_MKEY_MASK_RW;
3312
3313 if (atomic)
3314 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003315
3316 return cpu_to_be64(result);
3317}
3318
3319static __be64 get_umr_update_pd_mask(void)
3320{
3321 u64 result;
3322
Artemy Kovalyov31616252017-01-02 11:37:42 +02003323 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003324
3325 return cpu_to_be64(result);
3326}
3327
Eli Cohene126ba92013-07-07 17:25:49 +03003328static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Maor Gottlieb578e7262016-10-27 16:36:37 +03003329 struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003330{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003331 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003332
3333 memset(umr, 0, sizeof(*umr));
3334
Haggai Eran968e78d2014-12-11 17:04:11 +02003335 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3336 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3337 else
3338 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3339
Artemy Kovalyov31616252017-01-02 11:37:42 +02003340 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
3341 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
3342 u64 offset = get_xlt_octo(umrwr->offset);
3343
3344 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
3345 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
3346 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003347 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02003348 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3349 umr->mkey_mask |= get_umr_update_translation_mask();
3350 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
3351 umr->mkey_mask |= get_umr_update_access_mask(atomic);
3352 umr->mkey_mask |= get_umr_update_pd_mask();
3353 }
3354 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
3355 umr->mkey_mask |= get_umr_enable_mr_mask();
3356 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
3357 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003358
3359 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003360 umr->flags |= MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003361}
3362
3363static u8 get_umr_flags(int acc)
3364{
3365 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3366 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3367 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3368 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003369 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003370}
3371
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003372static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3373 struct mlx5_ib_mr *mr,
3374 u32 key, int access)
3375{
3376 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3377
3378 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003379
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003380 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003381 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003382 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003383 /* KLMs take twice the size of MTTs */
3384 ndescs *= 2;
3385
3386 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003387 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3388 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3389 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3390 seg->len = cpu_to_be64(mr->ibmr.length);
3391 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003392}
3393
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003394static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003395{
3396 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003397 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003398}
3399
3400static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
3401{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003402 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003403
Eli Cohene126ba92013-07-07 17:25:49 +03003404 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02003405 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02003406 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003407
Haggai Eran968e78d2014-12-11 17:04:11 +02003408 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003409 if (umrwr->pd)
3410 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
3411 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
3412 !umrwr->length)
3413 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
3414
3415 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003416 seg->len = cpu_to_be64(umrwr->length);
3417 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003418 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003419 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003420}
3421
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003422static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3423 struct mlx5_ib_mr *mr,
3424 struct mlx5_ib_pd *pd)
3425{
3426 int bcount = mr->desc_size * mr->ndescs;
3427
3428 dseg->addr = cpu_to_be64(mr->desc_map);
3429 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3430 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3431}
3432
Eli Cohene126ba92013-07-07 17:25:49 +03003433static __be32 send_ieth(struct ib_send_wr *wr)
3434{
3435 switch (wr->opcode) {
3436 case IB_WR_SEND_WITH_IMM:
3437 case IB_WR_RDMA_WRITE_WITH_IMM:
3438 return wr->ex.imm_data;
3439
3440 case IB_WR_SEND_WITH_INV:
3441 return cpu_to_be32(wr->ex.invalidate_rkey);
3442
3443 default:
3444 return 0;
3445 }
3446}
3447
3448static u8 calc_sig(void *wqe, int size)
3449{
3450 u8 *p = wqe;
3451 u8 res = 0;
3452 int i;
3453
3454 for (i = 0; i < size; i++)
3455 res ^= p[i];
3456
3457 return ~res;
3458}
3459
3460static u8 wq_sig(void *wqe)
3461{
3462 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
3463}
3464
3465static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
3466 void *wqe, int *sz)
3467{
3468 struct mlx5_wqe_inline_seg *seg;
3469 void *qend = qp->sq.qend;
3470 void *addr;
3471 int inl = 0;
3472 int copy;
3473 int len;
3474 int i;
3475
3476 seg = wqe;
3477 wqe += sizeof(*seg);
3478 for (i = 0; i < wr->num_sge; i++) {
3479 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
3480 len = wr->sg_list[i].length;
3481 inl += len;
3482
3483 if (unlikely(inl > qp->max_inline_data))
3484 return -ENOMEM;
3485
3486 if (unlikely(wqe + len > qend)) {
3487 copy = qend - wqe;
3488 memcpy(wqe, addr, copy);
3489 addr += copy;
3490 len -= copy;
3491 wqe = mlx5_get_send_wqe(qp, 0);
3492 }
3493 memcpy(wqe, addr, len);
3494 wqe += len;
3495 }
3496
3497 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
3498
3499 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
3500
3501 return 0;
3502}
3503
Sagi Grimberge6631812014-02-23 14:19:11 +02003504static u16 prot_field_size(enum ib_signature_type type)
3505{
3506 switch (type) {
3507 case IB_SIG_TYPE_T10_DIF:
3508 return MLX5_DIF_SIZE;
3509 default:
3510 return 0;
3511 }
3512}
3513
3514static u8 bs_selector(int block_size)
3515{
3516 switch (block_size) {
3517 case 512: return 0x1;
3518 case 520: return 0x2;
3519 case 4096: return 0x3;
3520 case 4160: return 0x4;
3521 case 1073741824: return 0x5;
3522 default: return 0;
3523 }
3524}
3525
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003526static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
3527 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02003528{
Sagi Grimberg142537f2014-08-13 19:54:32 +03003529 /* Valid inline section and allow BSF refresh */
3530 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
3531 MLX5_BSF_REFRESH_DIF);
3532 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
3533 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003534 /* repeating block */
3535 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
3536 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
3537 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003538
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003539 if (domain->sig.dif.ref_remap)
3540 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02003541
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003542 if (domain->sig.dif.app_escape) {
3543 if (domain->sig.dif.ref_escape)
3544 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
3545 else
3546 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02003547 }
3548
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003549 inl->dif_app_bitmask_check =
3550 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02003551}
3552
3553static int mlx5_set_bsf(struct ib_mr *sig_mr,
3554 struct ib_sig_attrs *sig_attrs,
3555 struct mlx5_bsf *bsf, u32 data_size)
3556{
3557 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
3558 struct mlx5_bsf_basic *basic = &bsf->basic;
3559 struct ib_sig_domain *mem = &sig_attrs->mem;
3560 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02003561
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003562 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02003563
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003564 /* Basic + Extended + Inline */
3565 basic->bsf_size_sbs = 1 << 7;
3566 /* Input domain check byte mask */
3567 basic->check_byte_mask = sig_attrs->check_mask;
3568 basic->raw_data_size = cpu_to_be32(data_size);
3569
3570 /* Memory domain */
3571 switch (sig_attrs->mem.sig_type) {
3572 case IB_SIG_TYPE_NONE:
3573 break;
3574 case IB_SIG_TYPE_T10_DIF:
3575 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
3576 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
3577 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
3578 break;
3579 default:
3580 return -EINVAL;
3581 }
3582
3583 /* Wire domain */
3584 switch (sig_attrs->wire.sig_type) {
3585 case IB_SIG_TYPE_NONE:
3586 break;
3587 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02003588 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003589 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003590 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03003591 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02003592 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003593 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003594 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003595 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003596 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003597 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02003598 } else
3599 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
3600
Sagi Grimberg142537f2014-08-13 19:54:32 +03003601 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003602 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02003603 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003604 default:
3605 return -EINVAL;
3606 }
3607
3608 return 0;
3609}
3610
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003611static int set_sig_data_segment(struct ib_sig_handover_wr *wr,
3612 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003613{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003614 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
3615 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003616 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003617 u32 data_len = wr->wr.sg_list->length;
3618 u32 data_key = wr->wr.sg_list->lkey;
3619 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003620 int ret;
3621 int wqe_size;
3622
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003623 if (!wr->prot ||
3624 (data_key == wr->prot->lkey &&
3625 data_va == wr->prot->addr &&
3626 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003627 /**
3628 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003629 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02003630 * So need construct:
3631 * ------------------
3632 * | data_klm |
3633 * ------------------
3634 * | BSF |
3635 * ------------------
3636 **/
3637 struct mlx5_klm *data_klm = *seg;
3638
3639 data_klm->bcount = cpu_to_be32(data_len);
3640 data_klm->key = cpu_to_be32(data_key);
3641 data_klm->va = cpu_to_be64(data_va);
3642 wqe_size = ALIGN(sizeof(*data_klm), 64);
3643 } else {
3644 /**
3645 * Source domain contains signature information
3646 * So need construct a strided block format:
3647 * ---------------------------
3648 * | stride_block_ctrl |
3649 * ---------------------------
3650 * | data_klm |
3651 * ---------------------------
3652 * | prot_klm |
3653 * ---------------------------
3654 * | BSF |
3655 * ---------------------------
3656 **/
3657 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
3658 struct mlx5_stride_block_entry *data_sentry;
3659 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003660 u32 prot_key = wr->prot->lkey;
3661 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003662 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
3663 int prot_size;
3664
3665 sblock_ctrl = *seg;
3666 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
3667 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
3668
3669 prot_size = prot_field_size(sig_attrs->mem.sig_type);
3670 if (!prot_size) {
3671 pr_err("Bad block size given: %u\n", block_size);
3672 return -EINVAL;
3673 }
3674 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
3675 prot_size);
3676 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
3677 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
3678 sblock_ctrl->num_entries = cpu_to_be16(2);
3679
3680 data_sentry->bcount = cpu_to_be16(block_size);
3681 data_sentry->key = cpu_to_be32(data_key);
3682 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003683 data_sentry->stride = cpu_to_be16(block_size);
3684
Sagi Grimberge6631812014-02-23 14:19:11 +02003685 prot_sentry->bcount = cpu_to_be16(prot_size);
3686 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003687 prot_sentry->va = cpu_to_be64(prot_va);
3688 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003689
Sagi Grimberge6631812014-02-23 14:19:11 +02003690 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
3691 sizeof(*prot_sentry), 64);
3692 }
3693
3694 *seg += wqe_size;
3695 *size += wqe_size / 16;
3696 if (unlikely((*seg == qp->sq.qend)))
3697 *seg = mlx5_get_send_wqe(qp, 0);
3698
3699 bsf = *seg;
3700 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
3701 if (ret)
3702 return -EINVAL;
3703
3704 *seg += sizeof(*bsf);
3705 *size += sizeof(*bsf) / 16;
3706 if (unlikely((*seg == qp->sq.qend)))
3707 *seg = mlx5_get_send_wqe(qp, 0);
3708
3709 return 0;
3710}
3711
3712static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003713 struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02003714 u32 length, u32 pdn)
3715{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003716 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003717 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003718 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02003719
3720 memset(seg, 0, sizeof(*seg));
3721
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003722 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003723 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003724 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003725 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02003726 MLX5_MKEY_BSF_EN | pdn);
3727 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003728 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003729 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
3730}
3731
3732static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003733 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003734{
3735 memset(umr, 0, sizeof(*umr));
3736
3737 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003738 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003739 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
3740 umr->mkey_mask = sig_mkey_mask();
3741}
3742
3743
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003744static int set_sig_umr_wr(struct ib_send_wr *send_wr, struct mlx5_ib_qp *qp,
Sagi Grimberge6631812014-02-23 14:19:11 +02003745 void **seg, int *size)
3746{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003747 struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
3748 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003749 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003750 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02003751 int region_len, ret;
3752
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003753 if (unlikely(wr->wr.num_sge != 1) ||
3754 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003755 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
3756 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02003757 return -EINVAL;
3758
3759 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003760 region_len = wr->wr.sg_list->length;
3761 if (wr->prot &&
3762 (wr->prot->lkey != wr->wr.sg_list->lkey ||
3763 wr->prot->addr != wr->wr.sg_list->addr ||
3764 wr->prot->length != wr->wr.sg_list->length))
3765 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02003766
3767 /**
3768 * KLM octoword size - if protection was provided
3769 * then we use strided block format (3 octowords),
3770 * else we use single KLM (1 octoword)
3771 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02003772 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02003773
Artemy Kovalyov31616252017-01-02 11:37:42 +02003774 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003775 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3776 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3777 if (unlikely((*seg == qp->sq.qend)))
3778 *seg = mlx5_get_send_wqe(qp, 0);
3779
Artemy Kovalyov31616252017-01-02 11:37:42 +02003780 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02003781 *seg += sizeof(struct mlx5_mkey_seg);
3782 *size += sizeof(struct mlx5_mkey_seg) / 16;
3783 if (unlikely((*seg == qp->sq.qend)))
3784 *seg = mlx5_get_send_wqe(qp, 0);
3785
3786 ret = set_sig_data_segment(wr, qp, seg, size);
3787 if (ret)
3788 return ret;
3789
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003790 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02003791 return 0;
3792}
3793
3794static int set_psv_wr(struct ib_sig_domain *domain,
3795 u32 psv_idx, void **seg, int *size)
3796{
3797 struct mlx5_seg_set_psv *psv_seg = *seg;
3798
3799 memset(psv_seg, 0, sizeof(*psv_seg));
3800 psv_seg->psv_num = cpu_to_be32(psv_idx);
3801 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003802 case IB_SIG_TYPE_NONE:
3803 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003804 case IB_SIG_TYPE_T10_DIF:
3805 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
3806 domain->sig.dif.app_tag);
3807 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02003808 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003809 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02003810 pr_err("Bad signature type (%d) is given.\n",
3811 domain->sig_type);
3812 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02003813 }
3814
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003815 *seg += sizeof(*psv_seg);
3816 *size += sizeof(*psv_seg) / 16;
3817
Sagi Grimberge6631812014-02-23 14:19:11 +02003818 return 0;
3819}
3820
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003821static int set_reg_wr(struct mlx5_ib_qp *qp,
3822 struct ib_reg_wr *wr,
3823 void **seg, int *size)
3824{
3825 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
3826 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
3827
3828 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
3829 mlx5_ib_warn(to_mdev(qp->ibqp.device),
3830 "Invalid IB_SEND_INLINE send flag\n");
3831 return -EINVAL;
3832 }
3833
3834 set_reg_umr_seg(*seg, mr);
3835 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3836 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3837 if (unlikely((*seg == qp->sq.qend)))
3838 *seg = mlx5_get_send_wqe(qp, 0);
3839
3840 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
3841 *seg += sizeof(struct mlx5_mkey_seg);
3842 *size += sizeof(struct mlx5_mkey_seg) / 16;
3843 if (unlikely((*seg == qp->sq.qend)))
3844 *seg = mlx5_get_send_wqe(qp, 0);
3845
3846 set_reg_data_seg(*seg, mr, pd);
3847 *seg += sizeof(struct mlx5_wqe_data_seg);
3848 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
3849
3850 return 0;
3851}
3852
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003853static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
Eli Cohene126ba92013-07-07 17:25:49 +03003854{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003855 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003856 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3857 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3858 if (unlikely((*seg == qp->sq.qend)))
3859 *seg = mlx5_get_send_wqe(qp, 0);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003860 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003861 *seg += sizeof(struct mlx5_mkey_seg);
3862 *size += sizeof(struct mlx5_mkey_seg) / 16;
3863 if (unlikely((*seg == qp->sq.qend)))
3864 *seg = mlx5_get_send_wqe(qp, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003865}
3866
3867static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
3868{
3869 __be32 *p = NULL;
3870 int tidx = idx;
3871 int i, j;
3872
3873 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
3874 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
3875 if ((i & 0xf) == 0) {
3876 void *buf = mlx5_get_send_wqe(qp, tidx);
3877 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
3878 p = buf;
3879 j = 0;
3880 }
3881 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
3882 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
3883 be32_to_cpu(p[j + 3]));
3884 }
3885}
3886
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003887static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
3888 struct mlx5_wqe_ctrl_seg **ctrl,
Eli Cohen6a4f1392014-12-02 12:26:18 +02003889 struct ib_send_wr *wr, unsigned *idx,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003890 int *size, int nreq)
3891{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003892 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
3893 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003894
3895 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
3896 *seg = mlx5_get_send_wqe(qp, *idx);
3897 *ctrl = *seg;
3898 *(uint32_t *)(*seg + 8) = 0;
3899 (*ctrl)->imm = send_ieth(wr);
3900 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
3901 (wr->send_flags & IB_SEND_SIGNALED ?
3902 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
3903 (wr->send_flags & IB_SEND_SOLICITED ?
3904 MLX5_WQE_CTRL_SOLICITED : 0);
3905
3906 *seg += sizeof(**ctrl);
3907 *size = sizeof(**ctrl) / 16;
3908
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003909 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003910}
3911
3912static void finish_wqe(struct mlx5_ib_qp *qp,
3913 struct mlx5_wqe_ctrl_seg *ctrl,
3914 u8 size, unsigned idx, u64 wr_id,
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003915 int nreq, u8 fence, u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003916{
3917 u8 opmod = 0;
3918
3919 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
3920 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02003921 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003922 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003923 if (unlikely(qp->wq_sig))
3924 ctrl->signature = wq_sig(ctrl);
3925
3926 qp->sq.wrid[idx] = wr_id;
3927 qp->sq.w_list[idx].opcode = mlx5_opcode;
3928 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
3929 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
3930 qp->sq.w_list[idx].next = qp->sq.cur_post;
3931}
3932
3933
Eli Cohene126ba92013-07-07 17:25:49 +03003934int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
3935 struct ib_send_wr **bad_wr)
3936{
3937 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
3938 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003939 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02003940 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02003941 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003942 struct mlx5_wqe_data_seg *dpseg;
3943 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02003944 struct mlx5_bf *bf;
Eli Cohene126ba92013-07-07 17:25:49 +03003945 int uninitialized_var(size);
Haggai Erand16e91d2016-02-29 15:45:05 +02003946 void *qend;
Eli Cohene126ba92013-07-07 17:25:49 +03003947 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003948 unsigned idx;
3949 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003950 int num_sge;
3951 void *seg;
3952 int nreq;
3953 int i;
3954 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003955 u8 fence;
3956
Haggai Erand16e91d2016-02-29 15:45:05 +02003957 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3958 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
3959
3960 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003961 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02003962 qend = qp->sq.qend;
3963
Eli Cohene126ba92013-07-07 17:25:49 +03003964 spin_lock_irqsave(&qp->sq.lock, flags);
3965
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003966 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
3967 err = -EIO;
3968 *bad_wr = wr;
3969 nreq = 0;
3970 goto out;
3971 }
3972
Eli Cohene126ba92013-07-07 17:25:49 +03003973 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04003974 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03003975 mlx5_ib_warn(dev, "\n");
3976 err = -EINVAL;
3977 *bad_wr = wr;
3978 goto out;
3979 }
3980
Eli Cohene126ba92013-07-07 17:25:49 +03003981 num_sge = wr->num_sge;
3982 if (unlikely(num_sge > qp->sq.max_gs)) {
3983 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03003984 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03003985 *bad_wr = wr;
3986 goto out;
3987 }
3988
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003989 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
3990 if (err) {
3991 mlx5_ib_warn(dev, "\n");
3992 err = -ENOMEM;
3993 *bad_wr = wr;
3994 goto out;
3995 }
Eli Cohene126ba92013-07-07 17:25:49 +03003996
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003997 if (wr->opcode == IB_WR_LOCAL_INV ||
3998 wr->opcode == IB_WR_REG_MR) {
3999 fence = dev->umr_fence;
4000 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
4001 } else if (wr->send_flags & IB_SEND_FENCE) {
4002 if (qp->next_fence)
4003 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
4004 else
4005 fence = MLX5_FENCE_MODE_FENCE;
4006 } else {
4007 fence = qp->next_fence;
4008 }
4009
Eli Cohene126ba92013-07-07 17:25:49 +03004010 switch (ibqp->qp_type) {
4011 case IB_QPT_XRC_INI:
4012 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03004013 seg += sizeof(*xrc);
4014 size += sizeof(*xrc) / 16;
4015 /* fall through */
4016 case IB_QPT_RC:
4017 switch (wr->opcode) {
4018 case IB_WR_RDMA_READ:
4019 case IB_WR_RDMA_WRITE:
4020 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004021 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4022 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004023 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004024 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4025 break;
4026
4027 case IB_WR_ATOMIC_CMP_AND_SWP:
4028 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03004029 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03004030 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
4031 err = -ENOSYS;
4032 *bad_wr = wr;
4033 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004034
4035 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03004036 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
4037 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03004038 set_linv_wr(qp, &seg, &size);
Eli Cohene126ba92013-07-07 17:25:49 +03004039 num_sge = 0;
4040 break;
4041
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004042 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004043 qp->sq.wr_data[idx] = IB_WR_REG_MR;
4044 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
4045 err = set_reg_wr(qp, reg_wr(wr), &seg, &size);
4046 if (err) {
4047 *bad_wr = wr;
4048 goto out;
4049 }
4050 num_sge = 0;
4051 break;
4052
Sagi Grimberge6631812014-02-23 14:19:11 +02004053 case IB_WR_REG_SIG_MR:
4054 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004055 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004056
4057 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
4058 err = set_sig_umr_wr(wr, qp, &seg, &size);
4059 if (err) {
4060 mlx5_ib_warn(dev, "\n");
4061 *bad_wr = wr;
4062 goto out;
4063 }
4064
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004065 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4066 fence, MLX5_OPCODE_UMR);
Sagi Grimberge6631812014-02-23 14:19:11 +02004067 /*
4068 * SET_PSV WQEs are not signaled and solicited
4069 * on error
4070 */
4071 wr->send_flags &= ~IB_SEND_SIGNALED;
4072 wr->send_flags |= IB_SEND_SOLICITED;
4073 err = begin_wqe(qp, &seg, &ctrl, wr,
4074 &idx, &size, nreq);
4075 if (err) {
4076 mlx5_ib_warn(dev, "\n");
4077 err = -ENOMEM;
4078 *bad_wr = wr;
4079 goto out;
4080 }
4081
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004082 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02004083 mr->sig->psv_memory.psv_idx, &seg,
4084 &size);
4085 if (err) {
4086 mlx5_ib_warn(dev, "\n");
4087 *bad_wr = wr;
4088 goto out;
4089 }
4090
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004091 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4092 fence, MLX5_OPCODE_SET_PSV);
Sagi Grimberge6631812014-02-23 14:19:11 +02004093 err = begin_wqe(qp, &seg, &ctrl, wr,
4094 &idx, &size, nreq);
4095 if (err) {
4096 mlx5_ib_warn(dev, "\n");
4097 err = -ENOMEM;
4098 *bad_wr = wr;
4099 goto out;
4100 }
4101
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004102 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02004103 mr->sig->psv_wire.psv_idx, &seg,
4104 &size);
4105 if (err) {
4106 mlx5_ib_warn(dev, "\n");
4107 *bad_wr = wr;
4108 goto out;
4109 }
4110
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004111 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4112 fence, MLX5_OPCODE_SET_PSV);
4113 qp->next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004114 num_sge = 0;
4115 goto skip_psv;
4116
Eli Cohene126ba92013-07-07 17:25:49 +03004117 default:
4118 break;
4119 }
4120 break;
4121
4122 case IB_QPT_UC:
4123 switch (wr->opcode) {
4124 case IB_WR_RDMA_WRITE:
4125 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004126 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4127 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004128 seg += sizeof(struct mlx5_wqe_raddr_seg);
4129 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4130 break;
4131
4132 default:
4133 break;
4134 }
4135 break;
4136
Eli Cohene126ba92013-07-07 17:25:49 +03004137 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02004138 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
4139 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
4140 err = -EPERM;
4141 *bad_wr = wr;
4142 goto out;
4143 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07004144 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02004145 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004146 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004147 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004148 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4149 if (unlikely((seg == qend)))
4150 seg = mlx5_get_send_wqe(qp, 0);
4151 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004152 case IB_QPT_UD:
4153 set_datagram_seg(seg, wr);
4154 seg += sizeof(struct mlx5_wqe_datagram_seg);
4155 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004156
Erez Shitritf0313962016-02-21 16:27:17 +02004157 if (unlikely((seg == qend)))
4158 seg = mlx5_get_send_wqe(qp, 0);
4159
4160 /* handle qp that supports ud offload */
4161 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4162 struct mlx5_wqe_eth_pad *pad;
4163
4164 pad = seg;
4165 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4166 seg += sizeof(struct mlx5_wqe_eth_pad);
4167 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4168
4169 seg = set_eth_seg(seg, wr, qend, qp, &size);
4170
4171 if (unlikely((seg == qend)))
4172 seg = mlx5_get_send_wqe(qp, 0);
4173 }
4174 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004175 case MLX5_IB_QPT_REG_UMR:
4176 if (wr->opcode != MLX5_IB_WR_UMR) {
4177 err = -EINVAL;
4178 mlx5_ib_warn(dev, "bad opcode\n");
4179 goto out;
4180 }
4181 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004182 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Maor Gottlieb578e7262016-10-27 16:36:37 +03004183 set_reg_umr_segment(seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
Eli Cohene126ba92013-07-07 17:25:49 +03004184 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4185 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4186 if (unlikely((seg == qend)))
4187 seg = mlx5_get_send_wqe(qp, 0);
4188 set_reg_mkey_segment(seg, wr);
4189 seg += sizeof(struct mlx5_mkey_seg);
4190 size += sizeof(struct mlx5_mkey_seg) / 16;
4191 if (unlikely((seg == qend)))
4192 seg = mlx5_get_send_wqe(qp, 0);
4193 break;
4194
4195 default:
4196 break;
4197 }
4198
4199 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4200 int uninitialized_var(sz);
4201
4202 err = set_data_inl_seg(qp, wr, seg, &sz);
4203 if (unlikely(err)) {
4204 mlx5_ib_warn(dev, "\n");
4205 *bad_wr = wr;
4206 goto out;
4207 }
Eli Cohene126ba92013-07-07 17:25:49 +03004208 size += sz;
4209 } else {
4210 dpseg = seg;
4211 for (i = 0; i < num_sge; i++) {
4212 if (unlikely(dpseg == qend)) {
4213 seg = mlx5_get_send_wqe(qp, 0);
4214 dpseg = seg;
4215 }
4216 if (likely(wr->sg_list[i].length)) {
4217 set_data_ptr_seg(dpseg, wr->sg_list + i);
4218 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4219 dpseg++;
4220 }
4221 }
4222 }
4223
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004224 qp->next_fence = next_fence;
4225 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq, fence,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004226 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004227skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004228 if (0)
4229 dump_wqe(qp, idx, size);
4230 }
4231
4232out:
4233 if (likely(nreq)) {
4234 qp->sq.head += nreq;
4235
4236 /* Make sure that descriptors are written before
4237 * updating doorbell record and ringing the doorbell
4238 */
4239 wmb();
4240
4241 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4242
Eli Cohenada388f2014-01-14 17:45:16 +02004243 /* Make sure doorbell record is visible to the HCA before
4244 * we hit doorbell */
4245 wmb();
4246
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004247 /* currently we support only regular doorbells */
4248 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset, NULL);
4249 /* Make sure doorbells don't leak out of SQ spinlock
4250 * and reach the HCA out of order.
4251 */
4252 mmiowb();
Eli Cohene126ba92013-07-07 17:25:49 +03004253 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03004254 }
4255
4256 spin_unlock_irqrestore(&qp->sq.lock, flags);
4257
4258 return err;
4259}
4260
4261static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4262{
4263 sig->signature = calc_sig(sig, size);
4264}
4265
4266int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4267 struct ib_recv_wr **bad_wr)
4268{
4269 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4270 struct mlx5_wqe_data_seg *scat;
4271 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004272 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4273 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004274 unsigned long flags;
4275 int err = 0;
4276 int nreq;
4277 int ind;
4278 int i;
4279
Haggai Erand16e91d2016-02-29 15:45:05 +02004280 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4281 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4282
Eli Cohene126ba92013-07-07 17:25:49 +03004283 spin_lock_irqsave(&qp->rq.lock, flags);
4284
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004285 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4286 err = -EIO;
4287 *bad_wr = wr;
4288 nreq = 0;
4289 goto out;
4290 }
4291
Eli Cohene126ba92013-07-07 17:25:49 +03004292 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4293
4294 for (nreq = 0; wr; nreq++, wr = wr->next) {
4295 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4296 err = -ENOMEM;
4297 *bad_wr = wr;
4298 goto out;
4299 }
4300
4301 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4302 err = -EINVAL;
4303 *bad_wr = wr;
4304 goto out;
4305 }
4306
4307 scat = get_recv_wqe(qp, ind);
4308 if (qp->wq_sig)
4309 scat++;
4310
4311 for (i = 0; i < wr->num_sge; i++)
4312 set_data_ptr_seg(scat + i, wr->sg_list + i);
4313
4314 if (i < qp->rq.max_gs) {
4315 scat[i].byte_count = 0;
4316 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4317 scat[i].addr = 0;
4318 }
4319
4320 if (qp->wq_sig) {
4321 sig = (struct mlx5_rwqe_sig *)scat;
4322 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4323 }
4324
4325 qp->rq.wrid[ind] = wr->wr_id;
4326
4327 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4328 }
4329
4330out:
4331 if (likely(nreq)) {
4332 qp->rq.head += nreq;
4333
4334 /* Make sure that descriptors are written before
4335 * doorbell record.
4336 */
4337 wmb();
4338
4339 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4340 }
4341
4342 spin_unlock_irqrestore(&qp->rq.lock, flags);
4343
4344 return err;
4345}
4346
4347static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4348{
4349 switch (mlx5_state) {
4350 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4351 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4352 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4353 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4354 case MLX5_QP_STATE_SQ_DRAINING:
4355 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4356 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4357 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4358 default: return -1;
4359 }
4360}
4361
4362static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4363{
4364 switch (mlx5_mig_state) {
4365 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4366 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4367 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4368 default: return -1;
4369 }
4370}
4371
4372static int to_ib_qp_access_flags(int mlx5_flags)
4373{
4374 int ib_flags = 0;
4375
4376 if (mlx5_flags & MLX5_QP_BIT_RRE)
4377 ib_flags |= IB_ACCESS_REMOTE_READ;
4378 if (mlx5_flags & MLX5_QP_BIT_RWE)
4379 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4380 if (mlx5_flags & MLX5_QP_BIT_RAE)
4381 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4382
4383 return ib_flags;
4384}
4385
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004386static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004387 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004388 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03004389{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004390 struct mlx5_core_dev *dev = ibdev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004391
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004392 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03004393
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04004394 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004395 rdma_ah_set_port_num(ah_attr, path->port);
4396 if (rdma_ah_get_port_num(ah_attr) == 0 ||
4397 rdma_ah_get_port_num(ah_attr) > MLX5_CAP_GEN(dev, num_ports))
Eli Cohene126ba92013-07-07 17:25:49 +03004398 return;
4399
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004400 rdma_ah_set_port_num(ah_attr, path->port);
4401 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03004402
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004403 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
4404 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
4405 rdma_ah_set_static_rate(ah_attr,
4406 path->static_rate ? path->static_rate - 5 : 0);
4407 if (path->grh_mlid & (1 << 7)) {
4408 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
4409
4410 rdma_ah_set_grh(ah_attr, NULL,
4411 tc_fl & 0xfffff,
4412 path->mgid_index,
4413 path->hop_limit,
4414 (tc_fl >> 20) & 0xff);
4415 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03004416 }
4417}
4418
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004419static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4420 struct mlx5_ib_sq *sq,
4421 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004422{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004423 void *out;
4424 void *sqc;
4425 int inlen;
4426 int err;
4427
4428 inlen = MLX5_ST_SZ_BYTES(query_sq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004429 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004430 if (!out)
4431 return -ENOMEM;
4432
4433 err = mlx5_core_query_sq(dev->mdev, sq->base.mqp.qpn, out);
4434 if (err)
4435 goto out;
4436
4437 sqc = MLX5_ADDR_OF(query_sq_out, out, sq_context);
4438 *sq_state = MLX5_GET(sqc, sqc, state);
4439 sq->state = *sq_state;
4440
4441out:
4442 kvfree(out);
4443 return err;
4444}
4445
4446static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
4447 struct mlx5_ib_rq *rq,
4448 u8 *rq_state)
4449{
4450 void *out;
4451 void *rqc;
4452 int inlen;
4453 int err;
4454
4455 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004456 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004457 if (!out)
4458 return -ENOMEM;
4459
4460 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
4461 if (err)
4462 goto out;
4463
4464 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
4465 *rq_state = MLX5_GET(rqc, rqc, state);
4466 rq->state = *rq_state;
4467
4468out:
4469 kvfree(out);
4470 return err;
4471}
4472
4473static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
4474 struct mlx5_ib_qp *qp, u8 *qp_state)
4475{
4476 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
4477 [MLX5_RQC_STATE_RST] = {
4478 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4479 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4480 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
4481 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
4482 },
4483 [MLX5_RQC_STATE_RDY] = {
4484 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4485 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4486 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
4487 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
4488 },
4489 [MLX5_RQC_STATE_ERR] = {
4490 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4491 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4492 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
4493 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
4494 },
4495 [MLX5_RQ_STATE_NA] = {
4496 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4497 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4498 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
4499 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
4500 },
4501 };
4502
4503 *qp_state = sqrq_trans[rq_state][sq_state];
4504
4505 if (*qp_state == MLX5_QP_STATE_BAD) {
4506 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4507 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
4508 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
4509 return -EINVAL;
4510 }
4511
4512 if (*qp_state == MLX5_QP_STATE)
4513 *qp_state = qp->state;
4514
4515 return 0;
4516}
4517
4518static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
4519 struct mlx5_ib_qp *qp,
4520 u8 *raw_packet_qp_state)
4521{
4522 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
4523 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
4524 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
4525 int err;
4526 u8 sq_state = MLX5_SQ_STATE_NA;
4527 u8 rq_state = MLX5_RQ_STATE_NA;
4528
4529 if (qp->sq.wqe_cnt) {
4530 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
4531 if (err)
4532 return err;
4533 }
4534
4535 if (qp->rq.wqe_cnt) {
4536 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
4537 if (err)
4538 return err;
4539 }
4540
4541 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
4542 raw_packet_qp_state);
4543}
4544
4545static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
4546 struct ib_qp_attr *qp_attr)
4547{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004548 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03004549 struct mlx5_qp_context *context;
4550 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004551 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03004552 int err = 0;
4553
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004554 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004555 if (!outb)
4556 return -ENOMEM;
4557
majd@mellanox.com19098df2016-01-14 19:13:03 +02004558 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004559 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03004560 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004561 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004562
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004563 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
4564 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
4565
Eli Cohene126ba92013-07-07 17:25:49 +03004566 mlx5_state = be32_to_cpu(context->flags) >> 28;
4567
4568 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03004569 qp_attr->path_mtu = context->mtu_msgmax >> 5;
4570 qp_attr->path_mig_state =
4571 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
4572 qp_attr->qkey = be32_to_cpu(context->qkey);
4573 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
4574 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
4575 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
4576 qp_attr->qp_access_flags =
4577 to_ib_qp_access_flags(be32_to_cpu(context->params2));
4578
4579 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004580 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
4581 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004582 qp_attr->alt_pkey_index =
4583 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004584 qp_attr->alt_port_num =
4585 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03004586 }
4587
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004588 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004589 qp_attr->port_num = context->pri_path.port;
4590
4591 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4592 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
4593
4594 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
4595
4596 qp_attr->max_dest_rd_atomic =
4597 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
4598 qp_attr->min_rnr_timer =
4599 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
4600 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
4601 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
4602 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
4603 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004604
4605out:
4606 kfree(outb);
4607 return err;
4608}
4609
4610int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
4611 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
4612{
4613 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4614 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4615 int err = 0;
4616 u8 raw_packet_qp_state;
4617
Yishai Hadas28d61372016-05-23 15:20:56 +03004618 if (ibqp->rwq_ind_tbl)
4619 return -ENOSYS;
4620
Haggai Erand16e91d2016-02-29 15:45:05 +02004621 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4622 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
4623 qp_init_attr);
4624
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004625 /* Not all of output fields are applicable, make sure to zero them */
4626 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
4627 memset(qp_attr, 0, sizeof(*qp_attr));
4628
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004629 mutex_lock(&qp->mutex);
4630
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004631 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
4632 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004633 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
4634 if (err)
4635 goto out;
4636 qp->state = raw_packet_qp_state;
4637 qp_attr->port_num = 1;
4638 } else {
4639 err = query_qp_attr(dev, qp, qp_attr);
4640 if (err)
4641 goto out;
4642 }
4643
4644 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03004645 qp_attr->cur_qp_state = qp_attr->qp_state;
4646 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
4647 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
4648
4649 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03004650 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03004651 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03004652 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03004653 } else {
4654 qp_attr->cap.max_send_wr = 0;
4655 qp_attr->cap.max_send_sge = 0;
4656 }
4657
Noa Osherovich0540d812016-06-04 15:15:32 +03004658 qp_init_attr->qp_type = ibqp->qp_type;
4659 qp_init_attr->recv_cq = ibqp->recv_cq;
4660 qp_init_attr->send_cq = ibqp->send_cq;
4661 qp_init_attr->srq = ibqp->srq;
4662 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03004663
4664 qp_init_attr->cap = qp_attr->cap;
4665
4666 qp_init_attr->create_flags = 0;
4667 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
4668 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
4669
Leon Romanovsky051f2632015-12-20 12:16:11 +02004670 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
4671 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
4672 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
4673 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
4674 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
4675 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02004676 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
4677 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02004678
Eli Cohene126ba92013-07-07 17:25:49 +03004679 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
4680 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
4681
Eli Cohene126ba92013-07-07 17:25:49 +03004682out:
4683 mutex_unlock(&qp->mutex);
4684 return err;
4685}
4686
4687struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
4688 struct ib_ucontext *context,
4689 struct ib_udata *udata)
4690{
4691 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4692 struct mlx5_ib_xrcd *xrcd;
4693 int err;
4694
Saeed Mahameed938fe832015-05-28 22:28:41 +03004695 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03004696 return ERR_PTR(-ENOSYS);
4697
4698 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
4699 if (!xrcd)
4700 return ERR_PTR(-ENOMEM);
4701
Jack Morgenstein9603b612014-07-28 23:30:22 +03004702 err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004703 if (err) {
4704 kfree(xrcd);
4705 return ERR_PTR(-ENOMEM);
4706 }
4707
4708 return &xrcd->ibxrcd;
4709}
4710
4711int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
4712{
4713 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
4714 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
4715 int err;
4716
Jack Morgenstein9603b612014-07-28 23:30:22 +03004717 err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004718 if (err) {
4719 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
4720 return err;
4721 }
4722
4723 kfree(xrcd);
4724
4725 return 0;
4726}
Yishai Hadas79b20a62016-05-23 15:20:50 +03004727
Yishai Hadas350d0e42016-08-28 14:58:18 +03004728static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
4729{
4730 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
4731 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
4732 struct ib_event event;
4733
4734 if (rwq->ibwq.event_handler) {
4735 event.device = rwq->ibwq.device;
4736 event.element.wq = &rwq->ibwq;
4737 switch (type) {
4738 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
4739 event.event = IB_EVENT_WQ_FATAL;
4740 break;
4741 default:
4742 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
4743 return;
4744 }
4745
4746 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
4747 }
4748}
4749
Maor Gottlieb03404e82017-05-30 10:29:13 +03004750static int set_delay_drop(struct mlx5_ib_dev *dev)
4751{
4752 int err = 0;
4753
4754 mutex_lock(&dev->delay_drop.lock);
4755 if (dev->delay_drop.activate)
4756 goto out;
4757
4758 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
4759 if (err)
4760 goto out;
4761
4762 dev->delay_drop.activate = true;
4763out:
4764 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004765
4766 if (!err)
4767 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004768 return err;
4769}
4770
Yishai Hadas79b20a62016-05-23 15:20:50 +03004771static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
4772 struct ib_wq_init_attr *init_attr)
4773{
4774 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02004775 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004776 __be64 *rq_pas0;
4777 void *in;
4778 void *rqc;
4779 void *wq;
4780 int inlen;
4781 int err;
4782
4783 dev = to_mdev(pd->device);
4784
4785 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004786 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004787 if (!in)
4788 return -ENOMEM;
4789
4790 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
4791 MLX5_SET(rqc, rqc, mem_rq_type,
4792 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
4793 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
4794 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
4795 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
4796 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
4797 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03004798 MLX5_SET(wq, wq, wq_type,
4799 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
4800 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Noa Osherovichb1383aa2017-10-29 13:59:45 +02004801 if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
4802 if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
4803 mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
4804 err = -EOPNOTSUPP;
4805 goto out;
4806 } else {
4807 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
4808 }
4809 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004810 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03004811 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
4812 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
4813 MLX5_SET(wq, wq, log_wqe_stride_size,
4814 rwq->single_stride_log_num_of_bytes -
4815 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
4816 MLX5_SET(wq, wq, log_wqe_num_of_strides, rwq->log_num_strides -
4817 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES);
4818 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004819 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
4820 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
4821 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
4822 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
4823 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
4824 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02004825 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004826 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02004827 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004828 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
4829 err = -EOPNOTSUPP;
4830 goto out;
4831 }
4832 } else {
4833 MLX5_SET(rqc, rqc, vsd, 1);
4834 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02004835 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
4836 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
4837 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
4838 err = -EOPNOTSUPP;
4839 goto out;
4840 }
4841 MLX5_SET(rqc, rqc, scatter_fcs, 1);
4842 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03004843 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4844 if (!(dev->ib_dev.attrs.raw_packet_caps &
4845 IB_RAW_PACKET_CAP_DELAY_DROP)) {
4846 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
4847 err = -EOPNOTSUPP;
4848 goto out;
4849 }
4850 MLX5_SET(rqc, rqc, delay_drop_en, 1);
4851 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004852 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
4853 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03004854 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004855 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4856 err = set_delay_drop(dev);
4857 if (err) {
4858 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
4859 err);
4860 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
4861 } else {
4862 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
4863 }
4864 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004865out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03004866 kvfree(in);
4867 return err;
4868}
4869
4870static int set_user_rq_size(struct mlx5_ib_dev *dev,
4871 struct ib_wq_init_attr *wq_init_attr,
4872 struct mlx5_ib_create_wq *ucmd,
4873 struct mlx5_ib_rwq *rwq)
4874{
4875 /* Sanity check RQ size before proceeding */
4876 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
4877 return -EINVAL;
4878
4879 if (!ucmd->rq_wqe_count)
4880 return -EINVAL;
4881
4882 rwq->wqe_count = ucmd->rq_wqe_count;
4883 rwq->wqe_shift = ucmd->rq_wqe_shift;
4884 rwq->buf_size = (rwq->wqe_count << rwq->wqe_shift);
4885 rwq->log_rq_stride = rwq->wqe_shift;
4886 rwq->log_rq_size = ilog2(rwq->wqe_count);
4887 return 0;
4888}
4889
4890static int prepare_user_rq(struct ib_pd *pd,
4891 struct ib_wq_init_attr *init_attr,
4892 struct ib_udata *udata,
4893 struct mlx5_ib_rwq *rwq)
4894{
4895 struct mlx5_ib_dev *dev = to_mdev(pd->device);
4896 struct mlx5_ib_create_wq ucmd = {};
4897 int err;
4898 size_t required_cmd_sz;
4899
Noa Osherovichccc87082017-10-17 18:01:13 +03004900 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
4901 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004902 if (udata->inlen < required_cmd_sz) {
4903 mlx5_ib_dbg(dev, "invalid inlen\n");
4904 return -EINVAL;
4905 }
4906
4907 if (udata->inlen > sizeof(ucmd) &&
4908 !ib_is_udata_cleared(udata, sizeof(ucmd),
4909 udata->inlen - sizeof(ucmd))) {
4910 mlx5_ib_dbg(dev, "inlen is not supported\n");
4911 return -EOPNOTSUPP;
4912 }
4913
4914 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
4915 mlx5_ib_dbg(dev, "copy failed\n");
4916 return -EFAULT;
4917 }
4918
Noa Osherovichccc87082017-10-17 18:01:13 +03004919 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03004920 mlx5_ib_dbg(dev, "invalid comp mask\n");
4921 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03004922 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
4923 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
4924 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
4925 return -EOPNOTSUPP;
4926 }
4927 if ((ucmd.single_stride_log_num_of_bytes <
4928 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
4929 (ucmd.single_stride_log_num_of_bytes >
4930 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
4931 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
4932 ucmd.single_stride_log_num_of_bytes,
4933 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
4934 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
4935 return -EINVAL;
4936 }
4937 if ((ucmd.single_wqe_log_num_of_strides >
4938 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
4939 (ucmd.single_wqe_log_num_of_strides <
4940 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES)) {
4941 mlx5_ib_dbg(dev, "Invalid log num strides (%u. Range is %u - %u)\n",
4942 ucmd.single_wqe_log_num_of_strides,
4943 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
4944 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
4945 return -EINVAL;
4946 }
4947 rwq->single_stride_log_num_of_bytes =
4948 ucmd.single_stride_log_num_of_bytes;
4949 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
4950 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
4951 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004952 }
4953
4954 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
4955 if (err) {
4956 mlx5_ib_dbg(dev, "err %d\n", err);
4957 return err;
4958 }
4959
4960 err = create_user_rq(dev, pd, rwq, &ucmd);
4961 if (err) {
4962 mlx5_ib_dbg(dev, "err %d\n", err);
4963 if (err)
4964 return err;
4965 }
4966
4967 rwq->user_index = ucmd.user_index;
4968 return 0;
4969}
4970
4971struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
4972 struct ib_wq_init_attr *init_attr,
4973 struct ib_udata *udata)
4974{
4975 struct mlx5_ib_dev *dev;
4976 struct mlx5_ib_rwq *rwq;
4977 struct mlx5_ib_create_wq_resp resp = {};
4978 size_t min_resp_len;
4979 int err;
4980
4981 if (!udata)
4982 return ERR_PTR(-ENOSYS);
4983
4984 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4985 if (udata->outlen && udata->outlen < min_resp_len)
4986 return ERR_PTR(-EINVAL);
4987
4988 dev = to_mdev(pd->device);
4989 switch (init_attr->wq_type) {
4990 case IB_WQT_RQ:
4991 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
4992 if (!rwq)
4993 return ERR_PTR(-ENOMEM);
4994 err = prepare_user_rq(pd, init_attr, udata, rwq);
4995 if (err)
4996 goto err;
4997 err = create_rq(rwq, pd, init_attr);
4998 if (err)
4999 goto err_user_rq;
5000 break;
5001 default:
5002 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
5003 init_attr->wq_type);
5004 return ERR_PTR(-EINVAL);
5005 }
5006
Yishai Hadas350d0e42016-08-28 14:58:18 +03005007 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005008 rwq->ibwq.state = IB_WQS_RESET;
5009 if (udata->outlen) {
5010 resp.response_length = offsetof(typeof(resp), response_length) +
5011 sizeof(resp.response_length);
5012 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5013 if (err)
5014 goto err_copy;
5015 }
5016
Yishai Hadas350d0e42016-08-28 14:58:18 +03005017 rwq->core_qp.event = mlx5_ib_wq_event;
5018 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03005019 return &rwq->ibwq;
5020
5021err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03005022 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005023err_user_rq:
Maor Gottliebfe248c32017-05-30 10:29:14 +03005024 destroy_user_rq(dev, pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005025err:
5026 kfree(rwq);
5027 return ERR_PTR(err);
5028}
5029
5030int mlx5_ib_destroy_wq(struct ib_wq *wq)
5031{
5032 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5033 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5034
Yishai Hadas350d0e42016-08-28 14:58:18 +03005035 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005036 destroy_user_rq(dev, wq->pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005037 kfree(rwq);
5038
5039 return 0;
5040}
5041
Yishai Hadasc5f90922016-05-23 15:20:53 +03005042struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
5043 struct ib_rwq_ind_table_init_attr *init_attr,
5044 struct ib_udata *udata)
5045{
5046 struct mlx5_ib_dev *dev = to_mdev(device);
5047 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
5048 int sz = 1 << init_attr->log_ind_tbl_size;
5049 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
5050 size_t min_resp_len;
5051 int inlen;
5052 int err;
5053 int i;
5054 u32 *in;
5055 void *rqtc;
5056
5057 if (udata->inlen > 0 &&
5058 !ib_is_udata_cleared(udata, 0,
5059 udata->inlen))
5060 return ERR_PTR(-EOPNOTSUPP);
5061
Maor Gottliebefd7f402016-10-27 16:36:40 +03005062 if (init_attr->log_ind_tbl_size >
5063 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
5064 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
5065 init_attr->log_ind_tbl_size,
5066 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
5067 return ERR_PTR(-EINVAL);
5068 }
5069
Yishai Hadasc5f90922016-05-23 15:20:53 +03005070 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5071 if (udata->outlen && udata->outlen < min_resp_len)
5072 return ERR_PTR(-EINVAL);
5073
5074 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
5075 if (!rwq_ind_tbl)
5076 return ERR_PTR(-ENOMEM);
5077
5078 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005079 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005080 if (!in) {
5081 err = -ENOMEM;
5082 goto err;
5083 }
5084
5085 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
5086
5087 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
5088 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
5089
5090 for (i = 0; i < sz; i++)
5091 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
5092
5093 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
5094 kvfree(in);
5095
5096 if (err)
5097 goto err;
5098
5099 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
5100 if (udata->outlen) {
5101 resp.response_length = offsetof(typeof(resp), response_length) +
5102 sizeof(resp.response_length);
5103 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5104 if (err)
5105 goto err_copy;
5106 }
5107
5108 return &rwq_ind_tbl->ib_rwq_ind_tbl;
5109
5110err_copy:
5111 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
5112err:
5113 kfree(rwq_ind_tbl);
5114 return ERR_PTR(err);
5115}
5116
5117int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
5118{
5119 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
5120 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
5121
5122 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
5123
5124 kfree(rwq_ind_tbl);
5125 return 0;
5126}
5127
Yishai Hadas79b20a62016-05-23 15:20:50 +03005128int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
5129 u32 wq_attr_mask, struct ib_udata *udata)
5130{
5131 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5132 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5133 struct mlx5_ib_modify_wq ucmd = {};
5134 size_t required_cmd_sz;
5135 int curr_wq_state;
5136 int wq_state;
5137 int inlen;
5138 int err;
5139 void *rqc;
5140 void *in;
5141
5142 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
5143 if (udata->inlen < required_cmd_sz)
5144 return -EINVAL;
5145
5146 if (udata->inlen > sizeof(ucmd) &&
5147 !ib_is_udata_cleared(udata, sizeof(ucmd),
5148 udata->inlen - sizeof(ucmd)))
5149 return -EOPNOTSUPP;
5150
5151 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
5152 return -EFAULT;
5153
5154 if (ucmd.comp_mask || ucmd.reserved)
5155 return -EOPNOTSUPP;
5156
5157 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005158 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005159 if (!in)
5160 return -ENOMEM;
5161
5162 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
5163
5164 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
5165 wq_attr->curr_wq_state : wq->state;
5166 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
5167 wq_attr->wq_state : curr_wq_state;
5168 if (curr_wq_state == IB_WQS_ERR)
5169 curr_wq_state = MLX5_RQC_STATE_ERR;
5170 if (wq_state == IB_WQS_ERR)
5171 wq_state = MLX5_RQC_STATE_ERR;
5172 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
5173 MLX5_SET(rqc, rqc, state, wq_state);
5174
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005175 if (wq_attr_mask & IB_WQ_FLAGS) {
5176 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
5177 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
5178 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
5179 mlx5_ib_dbg(dev, "VLAN offloads are not "
5180 "supported\n");
5181 err = -EOPNOTSUPP;
5182 goto out;
5183 }
5184 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5185 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
5186 MLX5_SET(rqc, rqc, vsd,
5187 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
5188 }
Noa Osherovichb1383aa2017-10-29 13:59:45 +02005189
5190 if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
5191 mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
5192 err = -EOPNOTSUPP;
5193 goto out;
5194 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005195 }
5196
Majd Dibbiny23a69642017-01-18 15:25:10 +02005197 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
5198 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
5199 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5200 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03005201 MLX5_SET(rqc, rqc, counter_set_id,
5202 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005203 } else
5204 pr_info_once("%s: Receive WQ counters are not supported on current FW\n",
5205 dev->ib_dev.name);
5206 }
5207
Yishai Hadas350d0e42016-08-28 14:58:18 +03005208 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005209 if (!err)
5210 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
5211
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005212out:
5213 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005214 return err;
5215}